Chucks out interactive webpages. Kinda like the Webpack server. Both environments are on Repl.it and come with basic examples, check it out.
Like the JavaScript console in DevTools.
PHP requires each statement to be terminated with a semi-colon.
Variables are declared by prefacing the name with the $
sigil, like $name = "isabella"
Namely strings and arrays.
echo count(array(1, 2, 3)); // 3
echo strlen("isa"); // 3
If you want to dive right in and handle strings without too much fuss, use double quotes.
Reassigments of names and strings work the same way in PHP as in JavaScript.
$movie = "Matrix";
$secondMovie = $movie;
echo $secondMovie; // "Matrix"
$movie = "Tarzan";
echo $movie; // "Tarzan"
echo $secondMovie; // "Matrix"
You can assign strings by reference. Something that can't be done in JavaScript. In JavaScript, you can only assign objects and arrays by reference.
$orange = "orange";
$citrus =& $orange;
echo $citrus; // "orange"
$orange = "manolo";
echo $citrus; // "manolo"
Continously concatenate strings with .=
$full_name = "Sandy";
$full_name .= "Topsvy";
echo $full_name; // "Sandy Topsvy"
Double quoted strings in PHP function like JavaScript template strings.
$bar = "bar";
echo "foo".$bar; // "foobar"
echo "foo$bar"; // "foobar" // When the `$` sigil is found, PHP automatically parses is as a variable
echo "foo$bars"; // "foobar" // Error, variable $bars not found
echo "foo${bar}s"; // "foobars"
Two ways to create arrays.
$with_function = array("PHP", "popcorn", 555.55);
$with_short = ["PHP", "popcorn", 555.55];
Join elements in an array with implode()
. Works like Array.prototype.join()
;
$message = ["a", " b", "c"];
echo implode("-", $message); // a-b-c
To print the array
$nums = [7, 201, 33, 88, 91];
echo print_r($nums);
Access an array with [] notation
$fruits = ["mangoes", "oranges"];
$citrus = $fruits[1];
Mutate an array with [] notation
$fruits = ["mangoes", "oranges"];
$fruits[1] = "peaches";
$fruits[2] = "plums";
echo implode(", ", $fruits); // "mangoes, peaches, plums"
Push to an array
$fruits = ["mangoes", "oranges"];
array_push($fruits, "lemons"); // "mangoes, oranges, lemons"
Pop from an array
$fruits = ["mangoes", "oranges"];
array_pop($fruits);
echo implode(', ', $fruits); //"mangoes"
Unshift to an array
$fruits = ["mangoes", "oranges"];
array_unshift($fruits, "lemons"); // "lemons, mangoes, oranges"
Shift from an array
$fruits = ["mangoes", "oranges"];
array_shift($fruits);
echo implode(', ', $fruits); //"oranges"
Access nested arrays
$fruits = ["mangoes", ["peaches", "plums"]];
echo $fruits[1][1]; //"plums"
Associative arrays are the PHP equivalent of JavaScript objects. Like ordered arrays, you can print the array with print_r to see the keys and values.
$class = ["name" => "4C", "teacher" => "Miss Chai", "num_students" => 12];
print_r($class);
Adding key value pairs to associative arrays is similar to using JavaScript's bracket notation. Changing pre-existing values works the same way too.
$test_scores = ["Alex" => 67, "Jon" => 89];
$test_scores["Candice"] = 78;
$test_scores["Alex"] = 99;
Remove a property and its associated value from an associative array with unset.
$fruits = ["mango" => "sweet", "lemon" => "sour"];
unset($fruits["lemon"]);
You can also use unset with the indexes of Ordered Arrays
$fruits = ["mango", "kiwi"];
unset($fruits[0]);
print_r($fruits); // Array([1] => kiwi)
Join arrays with +
or array_merge();
When you join Ordered Arrays, you are joining arrays that share indexes.
Here both mango and spinach have index 0.
PHP will discard items from the second array that shared keys/index of the first array.
Instead, to achieve the same effect as JavaScript's .concat(), use array_merge();
$fruits = ["mango", "kiwi"];
$veggies = ["spinach", "kale", "sprouts"];
$combined = $fruits + $veggies; //["mango", "kiwi", "sprouts"]
array_merge($fruits, $veggies) //["mango", "kiwi", "spinach", "kale", "sprouts"]
When joining associative arrays with similar keys, the + operator discards the key value pair from the second array, while aray_merge() overites the key value pair from the first array.
$class_1a = ["jon" => 16];
$class_2b = ["jon" => 23];
$combined = $class_1a + $class_2b;
print_r($combined); // Array([jon] => 16)
$merged = array_merge($class_1a, $class_2b);
print_r($merged); // Array([jon] => 23)
In PHP, ordered and associative arrays are assigned by value.
$fruits = ["mango"];
$desserts = $fruits;
$desserts[0] = "crepes";
print_r($fruits); // Array([0] => "mango")
print_r($desserts); // Array([0] => "crepes")
To assign by reference we use =&
$fruits = ["durian" => "smelly"];
$desserts =& $fruits;
$desserts["durian"] = "awesome";
print_r($fruits); Array([durian] => "awesome")
print_r($desserts); Array([durian] => "awesome")
Functions accept arguments by copy. This is the default. To accept by reference, the parameter has to be prefaced with &.
$fruits = ["mango" => "sweet"];
function add_fruit($fruits) {
$fruits["apples"] = "crunchy";
}
add_fruit($fruits);
print_r($fruits); // Array([mango] => "sweet")
function really_add_fruit(&$fruits) {
$fruits["apples"] = "crunchy";
}
really_add_fruit($fruits);
print_r($fruits); // Array([mango] => "sweet", [apple] => "crunchy")
When declaring properties on a class, declare them with $, like $color
When accessing the properties in an instance, do not use $, like color
<?php
class Beverage {
public $temperature, $color, $opacity;
}
$tea = new Beverage();
$tea -> temperature ="hot";
echo $tea -> temperature; //"hot"
Methods in PHP classes are declared the same way as in JavaScript. 'this' referenced inside a method is prefaced with $
<?php
class Beverage {
public $temperature, $color, $opacity;
function getInfo() {
return "This beverage is ".$this->color.".";
}
}
$soda = new Beverage();
$soda -> color = "black";
echo $soda->getInfo(); // "This beverage is black."
You can assign a value to a class property when declaring the property
<?php
class Beverage {
public $temperature, $color, $opacity;
public $flavour = "bitter";
}
$tea = new Beverage();
echo $tea -> flavour; // "bitter"
You can use __construct
to instantiate an instace with values for declared properties.
If a property already has a value, __construct
will override this value.
<?php
class Beverage {
public $temperature, $color, $opacity;
public $flavour = "bitter";
function __construct() {
$this->flavour = "sweet";
}
}
$tea = new Beverage();
echo $tea -> flavour; // "sweet"
Arguments/ parameters passed into __construct
have to be prefaced with $
<?php
class Beverage {
public $temperature, $color, $opacity;
function __construct($temperature, $color) {
$this->temperature = $temperature;
$this->color = $color;
}
}
$tea = new Beverage('cold', 'red');
echo $tea -> temperature; // "cold"
Inheritance works the same way in PHP as in JavaScript
<?php
class Beverage {
public $temperature;
function getInfo() {
return "This beverage is $this->temperature.";
}
}
class Milk extends Beverage {
function __construct() {
$this -> temperature = "cold";
}
}
Protected properties can only be accessed from sub classes.
<?php
class Beverage {
protected $opacity;
function __construct($opacity) {
$this->opacity = $opacity;
}
}
class Soda extends Beverage {
public $fizz;
function __construct($fizz) {
$this->fizz = $fizz;
}
function setOpacity() {
$this->opacity = "transparent";
echo "Opacity set to $this->opacity";
}
}
$sprite = new Soda("very fizzy");
$sprite -> setOpacity();
echo "\nSprite is $sprite->fizz."; // "Sprite is very fizzy"
echo "\nSprite is $sprite->opacity"; // Error, attempt to access protected property.
Unlike Javascript template strings, you cannot call functions or methods while constructing strings
<?php
class Beverage {
protected $opacity;
}
class Soda extends Beverage {
function setOpacity($opacity) {
$this->opacity = $opacity;
}
function getOpacity() {
return $this->opacity;
}
}
$sprite = new Soda();
$sprite -> setOpacity("transparent");
$spriteOpacity = $sprite->getOpacity();
echo "\nSprite is $spriteOpacity";
echo "\nSprite is $sprite->getOpacity()"; // Error, unidentified property $getOpacity
Like JavaScript, your classes don't have to have constructors.
<?php
class Beverage {
protected $opacity;
}
class Soda extends Beverage {
function setOpacity() {
$this->opacity = "transparent";
}
function getOpacity() {
return $this->opacity;
}
}
$sprite = new Soda();
$sprite->setOpacity();
$spriteOpacity = $sprite -> getOpacity();
echo $spriteOpacity;
But if the parent class has a constructor while the child class does not, you have to fulfill the argument parameter requirements of the parent class constructor
<?php
class Beverage {
protected $opacity;
function __construct($opacity) {
$this->opacity = $opacity;
}
}
class Soda extends Beverage {}
$sprite = new Soda(); //Error, too few arguments to function Beverage::__construct()