Skip to main content

Study and demonstrate php syntax, data type, variable, function, array, superglobal variable and form

What Is PHP?
PHP is a scripting language designed to fill the gap between SSI (Server Side Includes) and Perl, intended largely for the web environment. Basically, PHP allows a static web document to become dynamic. "PHP" is a recursive acronym that stands for "PHP: Hypertext Pre-processor". PHP pre-processes hypertext documents. Because of this, the pages can change before the user sees them, based on conditions. This can be used to write something to the page, create a table with a number of rows equal to the number of times the user has visited, or integrate the web page with a web database, such as MySQL.
A PHP script can be placed anywhere in the document. A PHP script starts with <?php  and ends with ?> 
SYNTAX:
<?php
// PHP code goes here
?>
Example:
<?php
Echo “Hello World” ;
?>
PHP Data Types
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource
1

PHP Variables

Variables are "containers" for storing information.

Creating (Declaring) PHP Variables. In PHP, a variable starts with the sign, followed by the name of the variable:

Ex. <?php
$txt = 
"Hello world!";
$x = 
5;
$y = 
10.5;
?>

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
 Rules for 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).


PHP Functions
There are Two types of Creating a Function:
1)     In-Built Function:  PHP has over 1000 built-in functions that can be called directly, from within a script, to perform a specific task.
There are many inbuilt function like:
·        String Function :  strtolower(); , strtoupper ();, strlen();, trim();, ltrim();, rtrim();, etc.
·       Numeric Function: abs();, round();, floor();, ceil();, max();, etc;
·       Array Function: array();, sizeof();, sort();, in_array();, list();, arrar_sum();, array_shift();, array_pop();, etc.
·       Directory Function: getcwd();, mkdir();, rmdir();, is_file();, is_dir();, file_size();, copy();, etc.
  
User Defined Function: Besides the built-in PHP functions, it is possible to create your own functions.
  • A function is a block of statements that can be used repeatedly in a program.
  • A function will not execute automatically when a page loads.
  • A function will be executed by a call to the function.

Create a User Defined Function in PHP

A user-defined function declaration starts with the word Function:

Syntax:

function functionName() {
    code to be executed;
}
Ex. <?php
function Hello() {
    
echo "Hello world!";
}
Hello(); 
// call the function
?>
PHP Array
An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
An array can hold many values under a single name, and you can access the values by referring to an index numbe


Create an Array in PHP
In PHP, the Array() function is used to create an array:
In PHP, there are three types of arrays:
  • Indexed arrays - Arrays with a numeric index
  • Associative arrays - Arrays with named keys. Associative arrays are arrays that use named keys that you assign to them.
  • Multidimensional arrays - Arrays containing one or more arrays. A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.
PHP SuperGlobal Variables
Some 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 PHP superglobal variables are:
  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION

Comments