Welcome to the PHP Guide


Programming Languages

Latest in Tech

Zettonia Education

PHP Quickstart

PHP is a backend scripting language that is widely used on web platforms. It is the backbone of platforms such as Wordpress and Drupal, so it is still a valuable language to learn.

PHP is a strictly for web scripting language, and hence basic knowledge of HTML/CSS is preferred
To use PHP, we need to set up a LAMP stack, which is a webserver/database/frontend stack.
We will use XAMPP, which comes with Apache Server, PHPMyadmin for MySQL Databasing.

Follow the link below to download XAMPP.

Download XAMPP

The PHP Syntax && Ecosystem

Jump to:

Data Types  Variables  Conditional Statements  Loops  Functions  composer 

PHP Basics

Data Types

PHP datatypes.

  • Integers : Whole numbers
  • Float : Numbers with decimals
  • String : Text
  • Boolean : True or False
Data Structures
  • Arrays : array[]
  • associative_array : array(["key" =>"value"],...)

Output to browser

<?php echo "Hello World" ?>

Comments

// Single-line comments are denoted with the two slashed //. This line will be ignored by the C# compiler.
/*
Multi-line comments
are denoted by /* and */
*/

Variables

Declaring variables: It's kinda like Algebra

<?
$m1 = "Hello";
$m2 = "World";
echo $m1;
echo $m2;
?>

Variables with math operations: Here comes to Algebra

<?
$var_1 = 20;
$var_2 = 30;
$total = $var_1 + $var_2;
echo $total;
?>

Variables with Arrays and Associative Arrays

<?php
$my_array = array(10,20,30);
$total = $my_array[0]+$my_array[1]+$my_array[2]
echo "The sum of ".$my_array[0].$my_array[1].$my_array[2]."is".$total;
?>
<?php
$my_cat = array(
   "name"=>"Mr. Fluffy",
   "age"=>20,
);
echo "My cat's name is " . my_cat["name"];
?>

If Else Conditional Statements:

Comparison Operators Meaning
== Is equal to
> More than
< Less than
>= More than or equal to
<= Less than or equal to
!= Not Equal to
Boolean Operators Meaning
&& and
|| or
! not
If, Else If, Else:
<?php
$you_are = "hungry"
$food_type = "pizza"

// if you are not hungry, go home
// else if you are hungry and hungry for pizza, go to pizza restaurant
// else go look for other restaurants

if ($you_are != "hungry"){
     echo "Go Home";
} else if ($you_are == "hungry && $food_type=="pizza"){
     echo "Go to Pizza Restaurant";
} else {
     echo "Look for other restaurants";

}
?>
Switch Case:
<?php
$my_cat = array(
   "name"=>"Mr. Fluffy",
   "age"=>20,
);
switch ($my_cat["name"]){
case "Mr.Fluffy":
     echo "Hello Mr. Fluffy";
     break;
case "Mr.Pinky":
     echo "Hello Mr. Pinky";
     break;
case default:
     echo "Your cat is neither Mr. FLuffy nor Mr. Pinky";
     break;
// There may be more cases to add
/* break will break you out of this switch/case
and will continue with the next block of code
*/
} ?>

Loops: do something over a range or while something is true

<?php
// Try these codes out
for ( $i=0 ;$<10 ; i++){
     echo $i;
}

$my_array = array("Hello","There",10,20,30);
for ($i=0 ;$<10 ; i++){
     echo my_array[i];
}

$my_animals = array("Cat","Dog","Rabbit");
foreach ($my_animals as $value){
     echo $value;
}
?>

<?php
// Try these codes out
$hungry = True;
while ($hungry == True){
     echo "I will have some pizza.";
     echo "Eating...";
     echo "Done eating. Not hungry any more!";
     $hungry = False;
     // when we change hungry to False, we will exit the while loops
     // we can use break here too, to break out of the while loop
{
echo "We are out of the while loop...";
?>

Functions: Reusable blocks of codes

function are_you_hungry($a,$b){
     // a is the parsed data. we can use "a" within this function. We will parse True or False
     if ($a == False){
          return "Go Home";
     } else if ($a == True and $b =="pizza"){
          return "Go to Pizza Restaurant";
     } else{
          return "Find some other restaurant";
     }
}
// Calling the function with hugry,food_type
$result = are_you_hungry(True,"pizza");
echo $result;
?>


Composer

Composer is a package manager for PHP. It manages dependencies much like NPM for NodeJS.

Dependencies are stored in a folder called "vendor". You can then call and import libraries from the "vendor" folder in PHP.

Ready to dig deeper into PHP?

Zettonia Education Programmes