- PHP: Hypertext Preprocessor
- created by Rasmus Lerdorf in 1994
- executed on server and sent to browser as plain HTML
- generate pages/files dynamically
- access files on a server
- collect data from web forms
- send emails
- track web traffic with cookies
- restrict access to website
- encrypt data
-
use semicolons to end line ;
-
Case sensitivity $color $Color $COLOR (all treated as separate variables) BUT keywords, function and classes names are case-insensitive
-
Comments
// This is a single line comment
# This is also a single line comment
-
dont need to be declared before adding a value to them (php automatically assigns correct data type) CASE SENSITIVE
<?php $var_name = value; ?>
- value cannot be changed after it is set
- do not need a leading dollar sign ($)
- can be accessed regardless of scope
- values can only be strings and numbers
eg. database username and password, website's base URL, company name, etc.
<?php
// Defining constant
define("USER_EMAIL", "[email protected]");
// Using constant
echo 'User email is - ' . USER_EMAIL;
?>
Almost the same! Both can display anything that can be displayed to the browser, such as string, numbers, variables values, the results of expressions etc.
<?php
echo "has no return value, can take multiple parameters";
print "can only take one argument, and always returns 1 print";
?>
<?php
"Hello World";
?>
<?php
$integer = 5985;
?>
<?php
$float = 10.365;
?>
<?php
$var1 = true;
$var2 = false;
?>
<?php
$beerTypes = array("lager","IPA","saison");
?>
<?php
class Pet {
Pulic $species;
public $color;
public $size;
public function __construct($color, $size) {
$this->color = $color;
$this->model = $size;
}
public function message() {
return "My pet is a " $this->species "and is " . $this->color . " and" . $this->size . "!";
}
}
$myPet = new Pet("armadillo", "brown", "small");
echo $myPet -> message();
echo "<br>";
$myPet = new Pet("feral cat", "calico", "large");
?>
<?php
$message = "Hello world!";
$message = null;
var_dump($message);
?>
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
Operator | Description | Example |
---|---|---|
+ | Addition | $x + $y |
- | Subtraction | $x - $y |
* | Multiplication | $x * $y |
/ | Division | $x / $y |
% | Modulus | $x % $y |
* | Multiplication | $x * $y |
Operator | Description | Example |
---|---|---|
= | Assign | $x = $y |
+= | Add and assign | $x += $y |
-= | Subtract and assign | $x -= $y |
*= | Multiply and assign | $x *= $y |
/= | Divide and assign quotient | $x /= $y |
%= | Divide and assign modulus | $x %= $y |
Operator | Description | Example |
---|---|---|
== | Equal | $x == $y |
=== | Identical | $x === $y |
!= | Not equal | $x != $y |
<> | Not equal | $x <> $y |
!== | Not identical | $x !== $y |
< | Less than | $x < $y |
> | Greater than | $x > $y |
>= | Greater than or equal to | $x >= $y |
<= | Less than or equal to | $x <= $y |
Operator | Description | Effect |
---|---|---|
++$x | Pre-increment | Increments $x by one, then returns $x |
$x++ | Post-increment | Returns $x, then increments $x by one |
--$x | Pre-decrement | Decrements $x by one, then returns $x |
$x-- | Post-decrement | Returns $x, then decrements $x by one |
Operator | Description | Example |
---|---|---|
and | And | $x and $y |
or | Or | $x or $y |
xor | Xor | $x xor $y |
&& | And | $x && $y |
! | Not | !$x |
Operator | Description | Example |
---|---|---|
. | Concatenation | $str1 . $str2 |
.= | Concatenation assignment | $str1 .= $str2 |
Operator | Description | Example |
---|---|---|
+ | Union | $x + $y |
== | Equality | $x == $y |
=== | Identity | $x === $y |
!= | Inequality | $x != $y |
<> | Inequality | $x <> $y |
!== | Non-identity | $x !== $y |
<?php
$age = 17;
//$age=(int)readline("enter age: ");
if ($age>19){
echo "User is 19 or older" . "<br>";
}
else{
echo "User is under 19" . "<br>";
}
?>
<?php
// Defining function
function registrationMessage(){
echo "Your registration was successful!";
}
// Calling function
registrationMessage()
?>
<?php
$miles = 35;
function convertMilestoKM($miles){
$output = $miles * 1.609344;
echo $miles . " miles equals " . $output . " kilometers";
}
convertMilestoKM($miles);
?>
Indexed array — An array with a numeric key.
<?php
$apples = array("gala", "fuji", "McIntosh");
?>
Associative array — An array where each key has its own specific value.
<?php
$heightInFeet = array("Shrek"=> 8, "donkey"=> 4, "Fiona"=> 5.10, "Lord Farquad"=>4.6);
var_dump ($heightInFeet);
?>
Multidimensional array — An array containing one or more arrays within itself.
<?php
$guests = array(
array(
"name" => "Shrek",
"age" => "400",
),
array(
"name" => "Fiona",
"age" => "30",
),
array(
"name" => "Lord Farquad",
"age" => "569",
)
);
echo "Shrek is " . $guests[0]["age"] . " years old.";
?>
while loops:
<?php
$i = 20; //start countdown at 20
while($i > 0) { //while i is greater than 0
$i--; //decrement
echo "The countdown is at " . $i . "<br>";
}
?>
for loops:
<?php
//Write a program to count backwards from 20 that stops at 0
function backFromTwenty(){
for($i=20; $i>=0; $i--){
echo $i . " ";
}
}
backFromTwenty()
?>