PHP is a very popular and used Computer Language mostly for building web based software applications. It is an interpreted language, meaning that every time a request is made to run a PHP Script, the interpreter translates the human readable code into machine code. Since PHP 5.5, the human readable code can be precompiled into bytecode and stored in shared memory, to be reused by all other requests, making PHP to execute very fast. For this to happen, you need to have OP Cache extension installed.

Refer to this articles to help you have a ready to use, High Performance, OP Cache ready PHP Interpreter along with a Nginx Web Server and a Maria DB Server for you to start learning how to write PHP Enterprise Class Software Applications:

 

A typical PHP Script file looks like this:

<?php

//This prints Hello World in the Browser
echo "Hello World!";

//This prints Hello World in the Browser too
print_r('Hello World!');

//Assign a value to a variable
$helloWorld = 'Hello World!';

//Print in the Browser the content of the variable
echo $helloWorld;


?>

 Refer to PHP Basic Syntax for more on this.

A computer program is a set of lines of code where you tell the computer what it needs to do.

PHP is a programming language where you use human friendly language statements to tell the computer what to do.

Basically PHP (and all other computer programs) has these 3 main parts:

Get Input Data from User or other Source.

Input data from User is most likely coming from a filled form. Typical Html Form looks like this:

<html>
<head></head>
<body>
<form name="user" action="backend-enpoint-url" method="post">
    <input type="text" value="" name="firstName">
    <input type="text" value="" name="lastName">
    <input type="submit" value="save">
</form>
</body>

</html>

Input Data from other Source is mostly coming in the request body as a json string. Other common format is xml string.

{
	"id": 3,
	"lineItemsId": "12154",
	"notes": "I have this idea 4"
}

 

For a Html Form Request:

//When the form is submitted and the form method is Post,  PHP gets the input values from the Web Server in a Global Variable $_POST
//Here we assign these $_POST values to these variables for better handling
$formInputFirstName = $_POST['firstName'];
$formInputLastName = $_POST['lastName'];

This Data is read from the Request and stored in variables for further processing

 

Process Request.

All statements in PHP (and all other languages) can be grouped into:

Assignment Instructions:

// "take the value from this and assign it to a single scalar variable"
$formInputFirstName = $_POST['firstName'];
$formInputLastName = $_POST['lastName'];

// "take the value from this and assign it to an array variable"
$userName['firstName'] = $_POST['firstName'];
$userName['lastName'] = $_POST['lastName'];

// "take the value from this and assign it to an object property"
$zebra = new Animal();
$zebra->setSpecies($_POST['firstName'] . $_POST['lastname']);

 Refer to PHP Assignment Operators and to PHP Operators for more on this.

 

 Control Statements:

The most common ones are for compare and for iteration:

/*
 * Compare
 */
$a = 1;
$b = 2;

if ($a == $b) {
    //Do this if its true
} else {
    //Do this if its false
}

if ($a < $b) {
    //Do this if its true
} elseif ($a == $b) {
    //Do this if its true
} else {
    //Do this iif none is true
}


/*
 * Iterate
 */
$c = ['a', 'b', 'c', 'd'];

foreach ($c as $letter) {
    //Do this for each one of the elements in the $c array
}

The if statement is a bifurcation in the flow of the code. It evaluates the condition and it continues the execution in the section that correspond.

The foreach statement is one of the few different kinds of loops. It basically iterates over each one of the elements of an Array or the properties in an Object and executes the statements in it.

When writing a PHP program, you will use these statements to process the input Data, apply business logic based on evaluating conditions, maybe store the Data and or its result in some physical device like a Database in a Hard Drive for later use and transform the Data and or the Results into a Response.

Refer to PHP Control Structures for more on this.

 

 Presentation Statements:

When the transformed Data or the Results obtained after applying some sort of Business logic to it is ready to be sent to the User most commonly these statements are used:

$a = 1;
$b = 2;

$result = ((3 * $a) + ($b/2))/100 ;

echo "The Resulting percentage is " . $result . "%\n";

$f = "The resulting percentage: %e was obtained evaluating from %d and %d";
echo sprintf($f, $result, $a, $b);

Refer to PHP Print Functions for more on this.

 

Return Response Data to User via Web Browser / Console or send it to other Source.

If the Response is sent back to the User's browser we ask PHP to instruct the Web Server to respond with a Html Response Page displaying embedded data and result using PHP tags:

<html>
<head></head>
<body>
<h1>Results:</h1>
<p>Input Data A: <?php echo $a; ?></p>
<p>Input Data B: <?php echo $b; ?></p>
<p>Result Percentage: <?php echo $result; ?></p>
</body>
</html>