PHP - Introduction

"PHP: Hypertext Preprocessor"

  1. PHP is a server scripting language 

  2. Used to make dynamic and interactive Web pages


PHP - Installation

PHP Setup: http://php.net/manual/en/install.php

Typically Installation:

  • install a web server
  • install PHP
  • install a database, such as MySQL

 


PHP - Basic Syntax

 

PHP syntax basics:

  • All keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.
  • All variable names are case-sensitive.

Example:

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php

// single-line comment​  1
# single-line comment 2
/* Multiple-lines comment - Line 1

Multiple-lines comment - Line 2

Multiple-lines comment - Line 3
*/​
echo "Hello World!";
?>

<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?> ​

</body>
</html>

Note: PHP statements end with a semicolon (;). 


PHP - Variables

PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive ($age and $AGE are two different variables)
  • Automatically converts the variable to the correct data type, depending on its value.

Example:

<!DOCTYPE html>
<html>
<body>

<?php
$txt = "Hello World!";
$x = 5;
$y = 10.5;

echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
echo "This is $txt!";​
echo "This is " . $txt . "!";​
echo $x + $y;

?>

<?php
$x = 5; // global scope

function myTest() {
    // using x inside this function will generate an error
    echo "<p>Variable x inside function is: $x</p>";

myTest();

echo "<p>Variable x outside function is: $x</p>";

?>

<?php
function myTest() {
    $x = 5; // local scope
    echo "<p>Variable x inside function is: $x</p>";

myTest();

// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";

?>

<?php

//Global variables can be accessed inside function using global keyward
$x = 5;
$y = 10;

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

myTest();
echo $y; // outputs 15

?>

<?php
$x = 5;
$y = 10;

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


myTest();
echo $y; // outputs 15

?>

<?php
function myTest() {
    static $x = 0;
    echo $x;
    $x++;
}

myTest();
myTest();
myTest();

?>
</body>
</html>

Variable scopes:

  • Local

    • A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function
  • Global

    • A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function
    • All global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.
  • Static

    • All of its variables are deleted when a function is completed/executed. However, sometimes we want a local variable NOT to be deleted, for that, we have to use static keyword.

 


PHP - Echo vs Print

 

  Echo Print
Return value Has no return value Has a return value of 1
Parameters / Argumnets Can take multiple parameters (although such usage is rare Can take one argument
Speed Echo is marginally faster than print.  

 

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>

<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

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

<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>


PHP - Data Types

PHP data types:

  • String

  • Integer

    • Non-decimal number between -2,147,483,648 and 2,147,483,647​​​
    • Must have at least one digit
    • Must not have a decimal point
    • Can be either positive or negative
    • Can be specified in three formats:
      • decimal (10-based),
      • hexadecimal (16-based - prefixed with 0x) or
      • octal (8-based - prefixed with 0)
  • Float (aka double)

    • ​​Number with a decimal point or a number in exponential form​
  • Boolean

    • TRUE or FALSE​

  • Array

    • ​​Stores multiple values
  • Object

    • ​​​Stores data and information on how to process that data.​
  • NULL

    • ​​Has no value assigned to it.​
    • If a variable is created without a value, it is automatically assigned a value of NULL.
  • Resource

    • Reference to functions and resources external
    • This is not an actual data type
    • e.g. database call

var_dump() function returns the data type and value

<?php 
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>"; 
echo $y;
?> 

<?php 
$x = 5985;
var_dump($x);
?>

<?php 
$x = 10.365;
var_dump($x);
?>

<?php 
$x = true; 
$y = false;
?>

<?php 
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>

<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>