<?php
echo strlen("Hello world!"); // outputs 12
?>

 

<?php
echo str_word_count("Hello world!"); // outputs 2
?>

 

<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>

 

<?php
echo strpos("Hello world!", "world"); // outputs 6.  If no match is found, it will return FALSE.
?>

 

<?php
echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!
?>

 

<?php
define("GREETING", "Welcome to W3Schools.com!");  // syntax: define(namevaluecase-insensitive)
echo GREETING;
?>

 

operation

===  $x === $y return true if $x is equal to $y, and they are of the same type

<>    $x <> $y    return true if $x is not equal to $y

!==   $x !== $y   return true if $x is not equal to $y, or they are not of the same

 

if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if this condition is true;
} else {
    code to be executed if all conditions are false;
}

e.g:

<?php
$t = date("H");

if ($t < "10") {
    echo "Have a good morning!";
} elseif ($t < "20") {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}
?>

 

foreach ($array as $value) {
    code to be executed;
}

e.g:

<?php 
$colors = array("red", "green", "blue", "yellow"); 

foreach ($colors as $value) {
    echo "$value <br>";
}
?>

 

 

<?php
function setHeight($minheight = 50) {
    echo "The height is : $minheight <br>";
}

setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>

 

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

 

 

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);  //output 3
?>

 

PHP Associative Arrays:associative arrays are arrays that use named keys that you assigin to them

<?php

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}
?>

 

Sort Funcitions For Arrays 

sort() -- sort arrays in ascending order

rsort() -- sort arrays in descending order

asort() -- sort associative arrays in ascending order, according to the value

ksort() -- sort associative arrays in ascending order, according to the key

arsort() -- sort associative arrays in descending order, according to the value

krsort() -- sort associative arrays in descending order, according to the key

 

Superglobals: PHP Global Variables

Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

The PHO superglobal variables are: 

$GLOBALS  $_SERVER  $_REQUEST  $_POST  $_GET  $_FILES  $_ENV  $_COOKIE  $_SESSION

<?php 
echo $_SERVER['PHP_SELF'];  // Returns the filename of the currency executing script
echo "<br>";
echo $_SERVER['SERVER_NAME'];  // Returns the IP address of the host server
echo "<br>";
echo $_SERVER['HTTP_HOST'];  // Returns the host header from the current request
echo "<br>";
echo $_SERVER['HTTP_REFERER'];  // Returns the complete URL of the current page
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];  // Return the URI of the current page
?>

$_SERVER['GATEWAY_INTERFACE']  returns the version of the Common Gateway Interface the server is using

$_SERVER['SERVER_ADDR']  returns the IP address of the host server

$_SERVER['SERVER_SOFTWARE'] returns the server identification string

$_SERVER['SERVER_PROTOCOL'] returns the name and reversion of the information protocol

$_SERVER['REQUEST_METHOD'] return the request method used to access the page

$_SERVER['QUERY_STRING'] return the query string if the page is accessed via a query string

$_SERVER['HTTP_ACCEPT'] return the Accept header from the current request

$_SERVER['HTTPP_ACCEPT_CHARSET'] return the Accept_Charset header from the current request

$_SERVER['HTTP_HOST'] return the host header from the current request

$_SERVER['HTTP_REFERER'] return the complete URL of the current page

$_SERVER['HTTPS'] is the script queried through a secure HTTP protocol

$_SERVER['REMOTE_ADDR'] return the IP address from where the user is viewing the current page

$_SERVER['REMOTE_HOST'] return the host name from where the user is viewing the current page

$_SERVER['REMOTE_PORT'] return the port being used on the user's machine to communicate with the web server

$_SERVER['SCRIPT_FILENAME'] return the absolute pathname of the currently executing script

$_SERVER['SERVER_ADMIN'] return the value given to the SERVER_ADMIN directive in the web server configuration file

$_SERVER['SERVER_PORT'] return the port on the server machine being used by the web server for communication

$_SERVER['SERVER_SIGNATURE'] return the server version and virtual host name which are used by the web server for communication

$_SERVER['PATH_TRANSLATED'] return the file system based path to the current script

$_SERVER['SCRIPT_NAME'] return the path of current script

$_SERVER['SCRIPT_URI'] return the URI of the current page

 

PHP $_REQUEST is used to collect data after submitting an HTML form 

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_REQUEST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

</body>
</html>

 

PHP $_POST is used to collect form data after submitting an HTML form with method = "post",$_POST is also widely used to pass variables

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_POST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

</body>
</html>

 

PHP $_GET is used to collect form data after submitting an HTML form with method="get".$_GET can also collect data sent in the URI

<html>
<body>

<?php 
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>

</body>
</html>