- A basic functional PHP script looks something like this:
<html>
<head>
<title>
My First PHP Script
</title>
</head>
<?php
Echo date ('J F Y');
?>
</html>
The script above contains both HTML and PHP code. The code between "<?php" and "?>" is PHP code that is first passed through the web server before being displayed onto the web browser. - The "echo" command is used to send output to the browser. The example below prints the statements under the quotes the same way a normal HTML file would. We use PHP to display the text as shown:
<?php
echo "This is My First PHP Script."
<? - Variables in PHP are used to store and retrieve data and values by declaring them using the dollar sign as shown below:
$Name = David;
In PHP, variables can be used even before they are declared. A variable can be declared and used anywhere in PHP code. - These are statements used to evaluate and execute conditional statements using the "if" statement. The example below shows how the "if" statement can be used to check a certain criteria and output the result if the case is true:
$Name= David;
If ($Number>5) {
Echo "$Name is greater than 5";
}
The above echo statement will only display when the number is greater than 5. This statement can be used to evaluate both number and word values. - Loop statements are used to repeat code blocks when a certain condition is met. The example below shows how to use the "while" loop to display the square of numbers from 1 to 5:
$count=1;
While ($count <=50 {
4square = $count * $count;
echo "$count squared is $square ";
$count++;
}
Loops can also be performed using the "do" and "for" loop statements for varying conditions that meet or fail to meet certain criteria. - Functions are used to simplify code use and maintenance. A sample code looks like this:
Function tax($amount) {
$total = $amount 8 2;
Return $total;
The code above can be called each time for each product.
PHP Script Syntax
The "echo" Command
PHP Variables
Conditional Statements
Loop Statements
PHP Functions
SHARE