
PHP is a server-side programming language. To parse PHP code set up a local environment such as XAMPP to run Apache server PHP on your local machine. Use MySQL database with PHP to build data-driven dynamic web pages. Know how algorithms work for data search and sorting, counting, etc. Construct a sturdy CMS with the most secure authentication. In PHP Programming Basics, embed PHP code into an HTML page and output dynamic text. Review the basics – PHP data types, such as variables, strings, and arrays. Different control structures in PHP, such as expressions and loops, give control over how the code executes. Even though PHP has many built-in functions, you can define your own custom functions. The code becomes reusable and helps prevent bugs. Interact with databases and build complicated reusable applications in PHP5 using PHP OOP. Develop secure PHP web applications by addressing issues to prevent attacks.
Address security issues such as XSS, Injections, working with files, passwords, PHP configurations, etc. OOP or object-oriented programming in PHP is the concept of class and objects with properties and methods. The class is associated with the definition and the object with the value. Interact with the database using the OOP code.
Contents
Server Side PHP Programming Basics

Environment Setup – XAMPP
Install local servers – either XAMPP, | MAMP (Mac) or WAMP (Windows) or LAMP (Linux). After the installation run MySQL.

Download XAMPP as shown in fig 1. Click on Manage Servers in the menu bar. Next Start MySQL Database and Apache Web Server fig 2 till the status shows ‘Running’ and the ‘red’ icon turns ‘green’. Access your local server by typing 127.0.0.1 or localhost in the browser.


To write PHP codes, we will use Brackets – a free open source code editor fig 3.1 & 3.2. After writing codes in PHP save it as a .php extension and then choose your ‘XAMPP folder’ (located in Applications for a Mac) to save it to ‘
Variables, Arrays, Loops, and Conditional in PHP Programming Basics.


In PHP, the variable starts with the $ sign, followed by the name of the variable. They store values such as numeric, characters, character strings, or memory addresses which are used in the program. Fig 4.1 is the variables.php file where a simple PHP code is written showing variables – string, character, numerical and float and echo is used to output the result fig 3.2 & fig 4.2.
localhost/sapCanvas_demo/variables.php in the browser (fig 3.2 & 4.2) shows:
- variables.php – is the name of the program.
- sapCanvas_demo – is the folder which saves your program file variables.php
- localhost – is the Xampp directory that you use to access the server.


Use the print_r(); by replacing echo ” ” ; fig 5.1 to get the same output fig 5.2.
Array
An array is generated using an array() function in PHP. The three types of arrays in PHP are –


Indexed or Numeric Array
An array with a numeric index where values are stored linearly fig 6.1 & 6.2.


Associated Arrays
An array with a string index where each value can be assigned a specific key fig 7.1 & 7.2.


Multidimensional Arrays
An array which contains single or multiple arrays within it and can be accessed with multiple indices fig 8.1 & 8.2.
Loops
Loops in PHP execute the same block of code a definite number of times. Four loop types are:


For Loop
run through a block of code a specified number of times fig 9.1 & 9.2.
for (init; condition; increment) { code to be executed; }


For each Loop
run through a block of code for each element in an array fig 10.1 & 10.2.
foreach ($array as $value) { code to be executed; }


while loop − runs through a block of code if and as long as a specified condition is true fig 11.1 & 11.2.
while (condition) { code to be executed; }


Do While Loop
runs through a block of code once and then repeats the loop as long as a special condition is true fig 12.1 & 12.2.
do { code to be executed; } while (condition);
Conditionals
Conditionals serve as an aid to decision making allowing PHP programmers to evaluate different conditions during a program on whether these conditions evaluate to true or false. These conditions and the actions associated with them are called a Conditional Statement. The different types of Conditional Statements are:


if Statement
The if Statement: In an if Statement output will appear only when Condition must be true fig 13.1 & 13.2.
if (expression){ Statement… }


if-else Statement
The if-else Statement: In an if-else Statement output will display in both the condition (if
if (expression{ Statement… } else{ Statement… }


if-elseif-else Statement
The if-
if(expression){ Statement… }elseif(expression){ Statement… }else{ Statement… }


Switch Statement
The Switch Statement: is similar to a series of if statements on the same expression fig 16.1 & 16.2.
switch(expression){ case: Statement… default: Statement… }


PHP Operators
PHP operators are symbols used to perform operations fig 17.1 & 17.2. The different types of operators are:
Arithmetic Operators (+, -, /); Array Operators ( ==, ===, != ); Assignment Operators (=, +=, *=); Comparison Operators (<>, < >); Conditional Operators (‘if’ and ‘else’); Increment/Decrement Operators (++$x, $x++, $x—); Logical Operators (and, or, &&); Spaceship Operators ($x < $y, $x != $y, $x >= $y); String Operators ( . , .=).


Functions & Methods
A function is a block of statements that is used repeatedly in a program. PHP has a lot of built-in functions fig 18.1 & 18.2. With PHP we can create our own. functions. When a function is created outside a class or an object it is called a function. When you create a function inside a class, it is a Method. A function gets executed by a call to action
function functionName() { code to be executed; }
PHP Documents
Read about PHP documentation by clicking on PHP_docs.
PHP Programming Basics – Regular Expressions
The commonly used Regular Expression functions in PHP are:


preg_match – this function performs a pattern match on a string fig 19.1 & 19.2. It returns a value true if the match is found. If not then it returns a value false.


preg_split – this function performs a pattern match on a string fig 20.1 & 20.2. It splits the results into a numeric array.


preg_replace – this function performs a pattern match on a string fig 21.1 & 21.2. The specified text replaces the match.
Read more about Regular Expressions by clicking on Regex.
Form Handling

HTML Form shows input fields for name, email and submit button fig 22.1.

In the display, user enters his name, email and clicks the ‘submit’ button fig 22.2.

Then the form data is processed by sending it to PHP file ‘greetings.php’ fig 23.1.

With ‘echo the variables’ the submitted data is displayed fig 23.2 for ‘greetings.php’. Note that the submission is with the ‘POST’ method. Alternatively, the ‘GET’ method also produces the same result.