The default file extension for PHP files is “.php
“.
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function “echo
” to output the text “Hello World!” on a web page:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
$
sign, followed by the name of the variable$age
and $AGE
are two different variables)$txt = "W3Schools.com";
echo "I love " . $txt . "!";
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
$x = "Hello world!";
$y = 'Hello world!';
var_dump($x);
echo "<br>";
var_dump($y);
PHP divides the operators in the following groups:
+ | Addition | $x + $y | Sum of $x and $y | Try it » |
– | Subtraction | $x – $y | Difference of $x and $y | Try it » |
* | Multiplication | $x * $y | Product of $x and $y | Try it » |
/ | Division | $x / $y | Quotient of $x and $y | Try it » |
% | Modulus | $x % $y | Remainder of $x divided by $y | Try it » |
** | Exponentiation | $x ** $y | Result of raising $x to the $y’th power |
In PHP we have the following conditional statements:
if
statement – executes some code if one condition is trueif...else
statement – executes some code if a condition is true and another code if that condition is falseif...elseif...else
statement – executes different codes for more than two conditionsswitch
statement – selects one of many blocks of code to be executedif (condition) {
code to be executed if this condition is true;
} elseif (condition) {
// code to be executed if first condition is false and this condition is true;
} else {
// code to be executed if all conditions are false;
}
In PHP, we have the following loop types:
while
– loops through a block of code as long as the specified condition is truedo...while
– loops through a block of code once, and then repeats the loop as long as the specified condition is truefor
– loops through a block of code a specified number of timesforeach
– loops through a block of code for each element in an array$i = 1;
while ($i < 6) {
echo $i;
$i++;
}
Besides the built-in PHP functions, it is possible to create your own functions.
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
The readfile()
function reads a file and writes it to the output buffer.
Assume we have a text file called “webdictionary.txt”, stored on the server, that looks like this:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
The PHP code to read the file and write it to the output buffer is as follows (the readfile()
function returns the number of bytes read on success):
<!DOCTYPE html>
<html>
<body>
<?php
echo readfile(“webdictionary.txt”);
?>
</body>
</html>
Follow me on Instagram