Saturday, April 1, 2023

PHP Tutorial

 

PHP Tutorial

PHP tutorial

PHP tutorial for beginners and professionals provides in-depth knowledge of PHP scripting language. Our PHP tutorial will help you to learn PHP scripting language easily.

This PHP tutorial covers all the topics of PHP such as introduction, control statements, functions, array, string, file handling, form handling, regular expression, date and time, object-oriented programming in PHP, math, PHP MySQL, PHP with Ajax, PHP with jQuery and PHP with XML.

What is PHP

PHP is an open-source, interpreted, and object-oriented scripting language that can be executed at the server-side. PHP is well suited for web development. Therefore, it is used to develop web applications (an application that executes on the server and generates the dynamic page.).

PHP was created by Rasmus Lerdorf in 1994 but appeared in the market in 1995. PHP 7.4.0 is the latest version of PHP, which was released on 28 November. Some important points need to be noticed about PHP are as followed:

  • PHP stands for Hypertext Preprocessor.
  • PHP is an interpreted language, i.e., there is no need for compilation.
  • PHP is faster than other scripting languages, for example, ASP and JSP.
  • PHP is a server-side scripting language, which is used to manage the dynamic content of the website.
  • PHP can be embedded into HTML.
  • PHP is an object-oriented language.
  • PHP is an open-source scripting language.
  • PHP is simple and easy to learn language.
What is PHP

Why use PHP

PHP is a server-side scripting language, which is used to design the dynamic web applications with MySQL database.

  • It handles dynamic content, database as well as session tracking for the website.
  • You can create sessions in PHP.
  • It can access cookies variable and also set cookies.
  • It helps to encrypt the data and apply validation.
  • PHP supports several protocols such as HTTP, POP3, SNMP, LDAP, IMAP, and many more.
  • Using PHP language, you can control the user to access some pages of your website.
  • As PHP is easy to install and set up, this is the main reason why PHP is the best language to learn.
  • PHP can handle the forms, such as - collect the data from users using forms, save it into the database, and return useful information to the user. For example - Registration form.

PHP Features

PHP is very popular language because of its simplicity and open source. There are some important features of PHP given below:

PHP Features

Performance:

PHP script is executed much faster than those scripts which are written in other languages such as JSP and ASP. PHP uses its own memory, so the server workload and loading time is automatically reduced, which results in faster processing speed and better performance.

Open Source:

PHP source code and software are freely available on the web. You can develop all the versions of PHP according to your requirement without paying any cost. All its components are free to download and use.

Familiarity with syntax:

PHP has easily understandable syntax. Programmers are comfortable coding with it.

Embedded:

PHP code can be easily embedded within HTML tags and script.

Platform Independent:

PHP is available for WINDOWS, MAC, LINUX & UNIX operating system. A PHP application developed in one OS can be easily executed in other OS also.

Database Support:

PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.

Error Reporting -

PHP has predefined error reporting constants to generate an error notice or warning at runtime. E.g., E_ERROR, E_WARNING, E_STRICT, E_PARSE.

Loosely Typed Language:

PHP allows us to use a variable without declaring its datatype. It will be taken automatically at the time of execution based on the type of data it contains on its value.

Web servers Support:

PHP is compatible with almost all local servers used today like Apache, Netscape, Microsoft IIS, etc.

Security:

PHP is a secure language to develop the website. It consists of multiple layers of security to prevent threads and malicious attacks.

Control:

Different programming languages require long script or code, whereas PHP can do the same work in a few lines of code. It has maximum control over the websites like you can make changes easily whenever you want.

A Helpful PHP Community:

It has a large community of developers who regularly updates documentation, tutorials, online help, and FAQs. Learning PHP from the communities is one of the significant benefits.

Web Development

PHP is widely used in web development nowadays. PHP can develop dynamic websites easily. But you must have the basic the knowledge of following technologies for web development as well.

  • HTML
  • CSS
  • JavaScript
  • Ajax
  • XML and JSON
  • jQuery

Prerequisite

Before learning PHP, you must have the basic knowledge of HTML, CSS, and JavaScript. So, learn these technologies for better implementation of PHP.

HTML - HTML is used to design static webpage.

CSS - CSS helps to make the webpage content more effective and attractive.

JavaScript - JavaScript is used to design an interactive website.

Audience

Our PHP tutorial is designed to help beginners and professionals. This PHP tutorial will help those who are unaware about the concepts of PHP but have basic knowledge of computer programming.

Problem

We assure you that you will not find any problem in this PHP tutorial. But if there is any mistake or error, please post the error in the contact form.

Install PHP

To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP) software stack. It is available for all operating systems. There are many AMP options available in the market that are given below:

  • WAMP for Windows
  • LAMP for Linux
  • MAMP for Mac
  • SAMP for Solaris
  • FAMP for FreeBSD
  • XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some other components too such as FileZilla, OpenSSL, Webalizer, Mercury Mail, etc.

If you are on Windows and don't want Perl and other features of XAMPP, you should go for WAMP. In a similar way, you may use LAMP for Linux and MAMP for Macintosh.

Download and Install WAMP Server

Click me to download WAMP server

Download and Install LAMP Server

Click me to download LAMP server

Download and Install MAMP Server

Click me to download MAMP server

Download and Install XAMPP Server

Click me to download XAMPP server

How to install XAMPP server on windows

We will learn how to install the XAMPP server on windows platform step by step. Follow the below steps and install the XAMPP server on your system.

Step 1: Click on the above link provided to download the XAMPP server according to your window requirement.

Install PHP

Step 2: After downloading XAMPP, double click on the downloaded file and allow XAMPP to make changes in your system. A window will pop-up, where you have to click on the Next button.

Install PHP

Step 3: Here, select the components, which you want to install and click Next.

Install PHP

Step 4: Choose a folder where you want to install the XAMPP in your system and click Next.

Install PHP

Step 5: Click Next and move ahead.

Install PHP

Step 6: XAMPP is ready to install, so click on the Next button and install the XAMPP.

Install PHP

Step 7: A finish window will display after successful installation. Click on the Finish button.

Install PHP

Step 8: Choose your preferred language.

Install PHP

Step 9: XAMPP is ready to use. Start the Apache server and MySQL and run the php program on the localhost.

How to run PHP programs on XAMPP, see in the next tutorial.

Install PHP

Step 10: If no error is shown, then XAMPP is running successfully.

Install PHP

How to run PHP code in XAMPP

Generally, a PHP file contains HTML tags and some PHP scripting code. It is very easy to create a simple PHP example. To do so, create a file and write HTML tags + PHP code and save this file with .php extension.

Note: PHP statements ends with semicolon (;).

All PHP code goes between the php tag. It starts with <?php and ends with ?>. The syntax of PHP tag is given below:

  1. <?php   
  2. //your code here  
  3. ?>  

Let's see a simple PHP example where we are writing some text using PHP echo command.

File: first.php

  1. <!DOCTYPE>  
  2. <html>  
  3. <body>  
  4. <?php  
  5. echo "<h2>Hello First PHP</h2>";  
  6. ?>  
  7. </body>  
  8. </html>  

Output:

Hello First PHP

How to run PHP programs in XAMPP

How to run PHP programs in XAMPP PHP is a popular backend programming language. PHP programs can be written on any editor, such as - Notepad, Notepad++, Dreamweaver, etc. These programs save with .php extension, i.e., filename.php inside the htdocs folder.

For example - p1.php.

As I'm using window, and my XAMPP server is installed in D drive. So, the path for the htdocs directory will be "D:\xampp\htdocs".

PHP program runs on a web browser such as - Chrome, Internet Explorer, Firefox, etc. Below some steps are given to run the PHP programs.

Step 1: Create a simple PHP program like hello world.

  1. <?php      
  2.     echo "Hello World!";  
  3. ?>  

Step 2: Save the file with hello.php name in the htdocs folder, which resides inside the xampp folder.

Note: PHP program must be saved in the htdocs folder, which resides inside the xampp folder, where you installed the XAMPP. Otherwise it will generate an error - Object not found.

Step 3: Run the XAMPP server and start the Apache and MySQL.

Step 4: Now, open the web browser and type localhost http://localhost/hello.php on your browser window.

Step 5: The output for the above hello.php program will be shown as the screenshot below:

run PHP code in XAMPP

Most of the time, PHP programs run as a web server module. However, PHP can also be run on CLI (Command Line Interface).

PHP Case Sensitivity

In PHP, keyword (e.g., echo, if, else, while), functions, user-defined functions, classes are not case-sensitive. However, all variable names are case-sensitive.

In the below example, you can see that all three echo statements are equal and valid:

  1. <!DOCTYPE>  
  2. <html>  
  3.     <body>  
  4.         <?php  
  5.             echo "Hello world using echo </br>";  
  6.             ECHO "Hello world using ECHO </br>";  
  7.             EcHo "Hello world using EcHo </br>";  
  8.         ?>  
  9.     </body>  
  10. </html>  

Output:

Hello world using echo
Hello world using ECHO
Hello world using EcHo

Look at the below example that the variable names are case sensitive. You can see the example below that only the second statement will display the value of the $color variable. Because it treats $color, $ColoR, and $COLOR as three different variables:

  1. <html>  
  2.     <body>  
  3.         <?php  
  4.             $color = "black";  
  5.             echo "My car is ". $ColoR ."</br>";  
  6.             echo "My dog is ". $color ."</br>";  
  7.             echo "My Phone is ". $COLOR ."</br>";  
  8.         ?>  
  9.     </body>  
  10. </html>  

Output:

Notice: Undefined variable: ColoR in D:\xampp\htdocs\program\p2.php on line 8
My car is
My dog is black

Notice: Undefined variable: COLOR in D:\xampp\htdocs\program\p2.php on line 10
My Phone is

Only $color variable has printed its value, and other variables $ColoR and $COLOR are declared as undefined variables. An error has occurred in line 5 and line 7.

No comments:

Post a Comment

HTML

  HTML Tutorial HTML tutorial  or HTML 5 tutorial provides basic and advanced concepts of HTML. Our HTML tutorial is developed for beginners...