Purpose Function Result
PHP VERSION echo phpversion(); 5.4.45-0+deb7u14
USING ECHO echo "My first PHP script!"; My first PHP script!
USING REFERENCE $txt = "PHP";
echo "I love $txt!";
I love PHP!
keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.
SINGLE LINE COMMENT \\single line comment starts with
MULTIPLE LINE COMMENT \*multiple line comment starts and ends with an inverse*\
Declaring Variables $x = 5;
$y = 4;
echo $x + $y;
9
GET VARIABLE TYPE $x = 5;
var_dump($x);
int(5)
GLOBAL VARIABLES- use variables outside the function (V1) $x = 5;
$y = 10;

function myTest() {
global $x, $y;
$y = $x + $y;
}

myTest();
echo $y;
15
GLOBAL VARIABLES- use variables outside the function (V2) $x = 5;
$y = 10;

function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}

myTest();
echo $y;
15
STATIC KEYWORD: do not delete used variables function myTest() {
static $x = 0;
echo $x;
$x++;
}

myTest();
myTest();
myTest();
0
1
2
Echo/Print Statement $txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

echo "<-h2->" . $txt1 . "<-/h2->";
echo "Study PHP at " . $txt2 . "<-br->";
echo $x + $y;

Learn PHP

Study PHP at W3Schools.com
9
String Data Type - character count $x = "Hello world!";
$y = 'Hello world!';

var_dump($x);
echo "<-br->";
var_dump($y);
string(12) "Hello world!"
string(12) "Hello world!"
Array Data Type - multiple values in one variable $cars = array("Volvo","BMW","Toyota");
var_dump($cars);
array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(6) "Toyota" }
Class(Movie) vs Objects(Rating, Runtime etc) class Movie {
public $Rating;
public $Runtime;
public function __construct($Rating, $Runtime) {
$this->Rating = $Rating;
$this->Runtime = $Runtime;
}
public function message() {
return "This movie is rated " . $this->Rating . " and the movie runtime is" . $this->Runtime . "!";
}
}

$myMovie = new Movie("MA", "201min");
var_dump($myMovie);
object(Movie)#1 (2) { ["Rating"]=> string(2) "MA" ["Runtime"]=> string(6) "201min" }
Double quoted string: Converts variable $x = "John"; echo "Hello $x"; Hello John
Single quoted strings: Does not convert variable $x = "John"; echo 'Hello $x'; Hello $x
Return the length of the string echo strlen("Hello world!"); 12
Count the number of word in the string echo str_word_count("Hello world!"); 2
Search for the text in the string (The first character position in a string is 0 (not 1).) echo strpos("Hello world!", "world"); 6
Return string in upper case (strtoupper($x);) $x = "Hello World!";
echo strtoupper($x);
HELLO WORLD!
Return string in lower case (strtolower($x);) $x = "Hello World!";
echo strtolower($x);
hello world!
Replace word in string $x = "Hello World!";
echo str_replace("World", "Dolly", $x);
Hello Dolly!
Reverse a String $x = "Hello World!";
echo strrev($x);
!dlroW olleH
Remove Whitespace $x = " Hello World! ";
echo trim($x);
echo "";
echo "";
Hello World!
Convert String into Array $x = "Hello World!");
$y = explode(" ", $x);

print_r($y);
Array ( [0] => Hello [1] => World! )
String Concatenation (combine) $x = "Hello";
$y = "World";
$z = $x ." ". $y; OR "$x $y";
echo $z;
Hello World
Slicing Strings Start the slice at index 6 (count from 0) and end the slice 5 positions later (count from 1)
$x = "Hello World!";
echo substr($x, 6, 5);
World
Slice to the End $x = "Hello World!";
echo substr($x, 6);
World!
Slice From the End $x = "Hello World!";
echo substr($x, -5, 3);
orl