php: a guide
february 20, 2026
installation
getting started
After you have installed php you can start a server using:
php -S localhost:8000
and you can navigate it using http://localhost:8000/filename.php
Perks and Quirks of php
- Its case sensitive
format
php is defined inside a html format but as a tag
<?php *code here* ?>
the file name for a php porgram is .php
heres a example code:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>
variables
They are defined using $ keyword
The good thing is that php handles the types and we dont have to define it.
$a = 20;
$b = 30;
$sum = $a - $b;
echo $sum;
-10
data types
- String
- Integer
- Float
- Boolean
- Array
- NULL
$a = "Hi This is a String";
echo var_dump($a);
$b = "2232";
echo var_dump($b);
string(19) "Hi This is a String" string(4) "2232"
comments
just like css
/* comment */
conditionals
if
if (condition) {
then do this;
}
if-else
if (condition) {
then do this;
} else {
otherwise do this;
}
examples
marks
$marks = 80;
if ($marks >= 60) {
echo "You have passed!";
} else {
echo "You have failed.";
}
You have passed!
loops
for loops
for ($i=1;$i<=5;$i++) {
echo $i;
}
12345
for each (arrays)
foreach ($array as $value) {
/* code here */
}
string functions
| Function | Description |
|---|---|
strlen() | Returns the length of a string (i.e. total number of characters) |
str_word_count() | Counts the number of words in a string |
strrev() | Reverses a string |
strpos() | Searches for specific text and returns position of first match or false |
str_replace() | Replaces some characters with other characters in a string |
substr() | Returns a part of a string |
strtolower() | Converts a string to lowercase |
substr_count() | Counts the number of times a substring occurs in a string |
ucwords() | Converts the first character of each word to uppercase |
trim() | Removes whitespace and predefined characters from both sides of a string |
$str = " This is a example string ";
echo "Original string:".$str."\n";
/* Functions */
echo "Length: ".strlen($str)."\n";
echo "Word Count: ".str_word_count($str)."\n";
echo "Reverse: ".strrev($str)."\n";
echo "Find text: ".strpos($str, "is")."\n";
echo "Replace: ".str_replace("example","php",$str)."\n";
echo "lowercase: ".strtolower($str)."\n";
Original string: This is a example string Length: 30 Word Count: 5 Reverse: gnirts elpmaxe a si sihT Find text: 5 Replace: This is a php string lowercase: this is a example string
arrays
/* only values */
$a = array(1, 5, 4, 9, 0);
/* keys and values */
$marks = array("english" => "75", "Maths" => "70", "Physics" => "80");
$a = array(1, 5, 4, 9, 8);
$b=0;
foreach ($a as $c) {
$b=$b+$c;
}
echo $b;
27
$marks = array("english" => "69", "Maths" => "70", "Physics" => "80");
if ($marks["english"] == 69) {
echo "You have scored ".$marks["english"]."(nice) in english";
} else {
echo "You have scored ".$marks["english"]." in english";
}
You have scored 69(nice) in english
functions
function($arg) {
/* code */
}
function sum(int $x, int $y) {
$z = $x + $y;
return $z;
}
echo "5 + 8 = ".sum(5, 8);
5 + 8 = 13