PHP Introduction For Beginners
This article targets computer programmers who want to learn PHP web development from scratch. I will try my best to make understand the basics of PHP programming language, and after you finish, you will be able to install PHP on windows and start coding.
This article requires a basic understanding of client/server applications, HTML, and how the Internet works.
If you read my articles before or watched my courses, Then you probably know that I like to make everything simple, So simplicity also will be the theme of this article.
So Using the simplest terms and simplest examples, I will try to make PHP very easy for you. If you are ready, Let’s Start!
What is PHP?
PHP is an open source, general-purpose programming language oriented for web development. Other words, It’s a Server Side WEB PROGRAMMING LANGUAGE. (assuming that you know the difference between server side and client side as a prerequisite).
PHP Main Features
|Open source and free to use||Perhaps this is the most important feature. It’s FREE!|
|Multi-platform||PHP can be used in many operating systems, such as Windows, Mac OS X, or Linux. PHP is supported by most of the web servers today, including Apache and IIS.|
|Interpreted language||Unlike the C or C++ languages, in which code needs to be compiled to run on computers, PHP code is interpreted at the time it is used.|
|Procedural programming support||PHP allows to you employ procedure and function call programming paradigms.|
|Object-oriented||PHP allows the use of object-oriented programming concepts like inheritance, polymorphism, and abstraction.|
|Non-strongly typed||There’s no need to specify the data type for variable declaration. Type is automatically detected at runtime.|
|Predefined super-global variables||A set of variables whose names start with a _ that can be accessed along the entire script execution.
Example: $_GET, $_SESSION, and $_SERVER.
|Database support extensions||PHP supports a wide range of databases, such as MySQL and PostgreSQL.|
|Text processing||PHP has a set of useful text-processing features such as regular expressions, or XML documents accessing and parsing.|
|Non-HTML output capabilities||PHP can generate images, PDF files, or XHTML text on the fly, and save them in the file system.|
|Error Handling||You can through exception when an error occurs during script execution. This exception can be caught to avoid application crashing.|
Installing PHP on Windows
This section is optional if you had already install PHP on your computer, you could skip learning the basics section directly if you want.
- The computer should have a Windows operating system installed and running.
- IIS (Internet Information Services) should be installed and configured.
If you would like to learn more about IIS, Check my courses:
Setting up IIS on Windows 10
This is a small video from My course Introduction To Web Server IIS that shows you how to install IIS on Windows 10 (It’s the same for older windows versions)
Steps Described in the video:
- Open the Control Panel and click on the Programs category link.
- After the Programs dialog is displayed, click Turn Windows features on or off to show the Windows Features dialog box.
- In the Windows Features dialog box, click on the Internet Information Services. (Read The Important notice below)
- Click OK to begin the installation process.
You may need to restart your PC after the installation.
You must also select the CGI entry under World Wide Web Services | Application Development Features because PHP uses CGI.
When the process finishes, run your web browser and navigate to http://127.0.0.1 (localhost) to test the installation.
The browser should display the page shown in the following figure.
The fastest and easiest way to install PHP on Windows is by using the Microsoft Web Platform Installer, which automates the process of installing and configuring PHP in the target system or any other modules.
Check out this video to learn more about the web platform installer
After installing the web platform installer, Just open it and search for PHP. and install it. (very easy)
After the installation finished, you can find the PHP folder probably in C:\Program Files\PHP\v7.0
Note: This path may be different if you install 32 bit (86) version or if you select another PHP version.
Navigate to your PHP directory and open the php.ini file.
We are going to edit some values in this files as follows:
- Uncomment and set FastCGI.impersonate = 1. The FastCGI IIS extension supports the ability to impersonate security tokens coming from the calling client, allowing IIS to define the security context under which the request will run.
- Uncomment and set cgi.fix_pathinfo = 0. This setting indicates to PHP that PATH_TRANSLATED will be set to SCRIPT_FILENAME).
- Set cgi.force_redirect = 0.
- Set open_basedir to point to the directory where the content of the website is located (typically C:\inetpub\wwwroot).
- Uncomment and set extension_dir to point to the directory where PHP extensions reside (typically extension_dir = “./ext”).
- Uncomment and set error_log=”php_errors.log”. This is useful to deal with troubleshooting.
- Uncomment every line that corresponds to a Windows extension DLL needed by PHP, as shown below:
Adding PHP to the Environment variables
Now we will add PHP to our Windows environment variables to make the PHP engine available for execution.
The following small video will show you how:
Testing your Web server and PHP
create a text file named phpinfo.php and save it into the website root folder (commonly C:\inetpub\wwwroot). The file should contain the following code:
<?php phpinfo(); ?>
Launch your web browser, type http://127.0.0.1/phpinfo.php. You must see the following web page:
So Now we have finished installing PHP and we are ready to learn all PHP basics so you can start programming.
PHP Tutorial For Beginners – PHP basics
What is a PHP script?
A script is a text file saved with a .php extension. It contains PHP programming code.
It can be pure PHP script or it can be embedded in HTML page but the page will always have a .php extension.
The .php extension is necessary for the filename so that the file can be recognized by the PHP engine as a PHP script so that it processes it.
PHP script has the following syntax:
<?php // php code ?>
Also, We can embed PHP code into an HTML file. In this case, the syntax for the script is the same as the code displayed in the previous code listing, but it is inside HTML statements.
The following code shows an example of PHP embedded into HTML.
<html> <head> </head> <body> Hello, Today: <?php echo date("l F jS \of Y"); ?>; </body> </html>
PHP sample scripts
Example1: Basic Hello World
Create a text file and add the following PHP script:
<?php echo 'Hello World from PHP basics tutorial'; ?>
Save the file as hello.php in your wwwroot directory, then open your browser and go to “http://127.0.0.1/hello.php”, you will see something like this:
Example2: Displaying the current date
<?php echo 'Today is '; echo date("l F jS \of Y"); >;
Example3: Calling HTML from PHP sample
<?php echo "<html>\n<head>\n<title>HTML from PHP</title>\n</head>\n<body>\n<h1>This is an HTML page generated by calling the code from a PHP script</h1>\n</body>\n</html>"; ?>;
Explanation: In General, the “echo” command is used to write what’s between the quotes to the browser windows, but in the last example It renders what’s inside (The HTML code) and shows the output on the screen as shown in the picture below:
Now if you want to know what PHP has sent to your browser, just press F12 while opening your page and open the inspector as shown below:
PHP Basics – Variables
Like any programming language, the main way to store data in a PHP program is by using variables.
A variable is an object that can hold data dynamically while the program is running, meaning that data stored in variables can change according to the program needs during the execution flow.
How to use and declare variables
Firstly you have to know that Variable declaration, and using them in PHP, should comply with the following prerequisites:
- Variable names are denoted with a leading dollar sign ($).
- Variable names must begin with a letter or underscore character Only.
- Characters like +, -, %, (, ), ., and & cannot be used in variable names.
- Variables are not strictly typed; so you cannot know in advance whether it will be used to store a number or a string.
- Conversion is done automatically.
- We assign variables with the = operator, placing the variable on the left side and the expression to be evaluated on the right.
|Integer||Numbers with no decimal point|
|Double||Floating point numbers, ex: 1.31313 or 34.5|
|Boolean||True or False|
|String||Sequences of characters like ‘First Name’|
|Array||A named and indexed collection of values|
|Object||Instances of classes (used in object-oriented programming)|
|Resource||External resources such as database connections|
Here are some examples of declaring variables in PHP:
$Length = 3 + 0.14159; # decimal $Age= 4; # Integer $Name= "Hassan aboul hassan"; # String $arr_values = array("element1","element2"); # array $IsOn = TRUE; # Boolean $null_value = NULL; # Null
This is was a small overview of the PHP programming language and how to install on windows.