This is my summary of the first section of the w3 school PHP Tutorial.

PHP is a language and the program to run the language, it works server side.

You can’t run PHP in a browser, you have to run it in a local server like XAMPP, or a remote server like webFaction.

Macs come with PHP already, but for pc you can install it.

give your file a .php ending.

You make php tags like so:

Your php goes here


Your php goes here

You can have html and css in your .php file.

Terminate your statements with a semi-colon.

$x = 5 + 5;

Single line comments:

# comment
// comment

multi-line, or partial line

/* Your 
comment */
$x = 5 /* + 15 */ + 5;

You declare a variable with the $. It must be alpha-numeric, must start with a letter or underscore, not a number, and they are case sensitive meaning if the cases change the variable does as well.

$_good = 4;
%9illegal = "bad";

print with echo

echo "Hello world";
print "hello";

concatenate with the period.

echo "Hello"." "."John";
# prints 'Hello John'

PHP is loosely typed.

global = outside of functions accessible within php. stored in an array called


You can access them like so:

$x = 5;
echo $GLOBALS;
// prints [5]

local = inside a function, only accessible there.

static = isn’t deleted after the function runs.

function myTest() {
static $x = 0;
echo $x;

myTest(); // prints 0
myTest(); // prints 1
myTest(); // prints 2

There are 8 types in PHP

b-insofar is a mnemonic

function NameOfClass()

var_dump(value) returns the type and value.

String functions:

strlen(string) returns the character count of the string.

str_word_count() returns the number of words in the string.

strive() reverses the string.

strpos(first, second) returns the index of the second argument in the first.

str_replace() replaces occurrences of the first argument with second argument in third argument.

The type of a variable can change after being set. This can lead to unexpected bugs.

is_int(value) returns true if value is an integer, false if not.

is_float(value) returns true if value is a float, false if not.

is_finite(value) returns true if value is lower magnitude than the overflows, false if not.

is_infinite(value) opposite of is_finite(value).

is_nan(value) returns true when an invalid calculation was assigned such as acos(8);.

is_numeric(value) returns true if the value is a number or a numeric string, false if neither.

(int)value, (integer)value, intval(value) will all cast the value into an integer.

  • If you cast a non-numberic string to an integer, it will be set to 0, otherwise it will cast the value to an integer, while ignoring decimal values.


define("constantName", "constantValue", false);
echo constantName;
# prints constantValue
  • This method takes the constantName string and converts it to a usable constant which does not require a dollar sign like variables and whose value cannot change. The constantValue can be any type. If you set the last value to true, then the constant becomes case insensitive. Constants are global, you can use them inside functions.


Most of the operators are fairly standard. I am going to make not of the unusual ones.

  • ** is for exponents. echo 10 ** 3; will yield 1000.
  • Provides standard assignment operators.
  • Since PHP is “loosely typed” echo "100" == 100; will yield true.
  • === enforces “strong type” by only yielding true if both the type and value are the same. echo "100" === 100; will yield false. This is called “identity”.
  • !== is the opposite of ===.
  • <> is an alternative way to write !=.
  • <=> the strangest one, this is called the “spaceship” operator in PHP7 and later. Returns -1 if the left side is lower, 0 if they are equal, 1 if the right side is lower.
  • You can write “and” operator as and or &&.
  • You can write “or” operators as or or ||.
  • PHP has a “xor” operator: xor.
  • You concatenate with a . such as echo "Hello ".$name;
  • Array equality == checks if the two arrays have the same values regardless of type in the same order, or in the case of arrays with key value pairs, it returns true if the values are the same regardless of order and type.
  • Array identity === checks if the two arrays have the same value, same type, in the same order.
  • Ternary operator included: $x = $val1 ? $val2 : $val3.
  • NULL coalescing: ?? returns.


PHP supports if elsif and else along with switch statements.

if (condition) {}

PHP if statements can accept optional values. It will return true if the variable has a value and false if it does not.

if (NULL) {
// This will not execute.
} else {
// This will execute

If multiple if and elsif statements are true, then the first true condition will be run and the others will no longer execute.

switch($itemToCheck) {
case "value":
echo "itemToCheck == value";
case "other":
echo "itemToCheck == other";
echo "itemToCheck wasn't known;

while , do...while, for, and foreach .

while (condition) {
echo "hi";
//base case;


do {
code to be executed;
base case;
} while (condition is true);

for loop

for (init counter; test counter; increment counter) {
code to be executed;
# Example
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";

foreach single values

foreach ($array as $value) {
echo "$value <br>";

foreach key values

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");foreach($age as $x => $val) {
echo "$x = $val<br>";


PHP has built in functions

Function parameters only require a name. You can interpolate just by putting the variable inside the string. The $ signals to PHP to check if there is a variable in the string. If the incorrect type is passed as an argument, PHP converts it to the required type with an approximated value. For example, PHP converts NULL to “”.

function functionName($paramName, $paramName2) {
echo "$paramName Refsnes. <br>";

You can also return values. In PHP 7 and above you can specify types.

function addNumbers(int $a, int $b) {
return $a + $b;

You can also give default values

function setHeight(int $minheight = 50) {
echo "The height is : $minheight <br>";

If we call the above function like so:

echo addNumber(5, "5 days");
# 10

Moving from left to right, if a non-number non-space occurs before a number, then this will crash.

echo addNumber(5, "j 5 days");
# Fatal error: Uncaught TypeError: Argument 2 passed to addNumbers() must be of the type integer, string given,
echo addNumber(5, " 5 days");
# 10

You can use the declare(strict_types=int); in order to force a crash when types don’t match. It must be put as the first statement in the php tags. int must be either 1 to enforce type matching with a crash, and 0 to not enforce type safety. The default if not included is 0.

<?php declare(strict_types=1);

You can also enforce the type of the return value too in PHP7! You probably will only want to use strict_types for debugging and reading other people’s code because users don’t actually really like crashes.

function addNumbers(float $a, float $b) : int {


$cars = array("Volvo", "BMW", "Toyota"); # single values
$shoes = array(); # empty init
echo count($cars); # 3
echo $cars[0]; # Volvo
# key value array or (behaves like an ordered dictionary)
$age = array("Peter"=>"35", "Ben"=>"37");

You can matrices as well.

  • sort($array) - sort arrays in ascending order
  • rsort($array) - sort arrays in descending order
  • asort($array) - sort associative arrays in ascending order, according to the value
  • ksort($array) - sort associative arrays in ascending order, according to the key
  • arsort($array) - sort associative arrays in descending order, according to the value
  • krsort($array) - sort associative arrays in descending order, according to the key

If a non-array is passed to any of these functions, the program will crash.

built in variables that are available in all PHP scopes.


PHP automatically creates a key value array called $GLOBALS which stores all the global variable (declared on the global scope) names as keys and corresponding values as values.If you try to access a key value pair that wasn’t set, it will return NULL.

  • $_SERVER

A key value array that holds server information such as with keys: ‘PHP_SELF’, ‘SERVER_NAME’, ‘HTTP_HOST’, ‘HTTP_REFERER’, ‘HTTP_USER_AGENT, ‘SCRIPT_NAME’. More can be found here.


used to collect data after submitting a form. For example, if there is a form input that has name="inputName" then you can access the user inputted value, with $_REQUEST['inputName']. Creates a key value array.

  • $_POST

Same as $_REQUEST but it only applies to $_POST requests. Private, good for passwords and confidential information.

  • $_GET

Same as the $_REQUEST but it only applies to $_GET requests. GET requests displays the contents in the url. They are not secret so don’t use a get request for passwords. Also get requests have a 2000 character limit.