Part-7 Prac 2: Study and demonstrate php syntax, data type, variable, function, array, superglobal variable and form (Self-learning exercise using this link: https://www.w3schools.com/php/).



What is PHP?

    Image result for php image
  • PHP is an acronym for "PHP: Hypertext Preprocessor" . 
  • PHP is a widely-used, open source scripting language  

  • PHP scripts are executed on the server
  • PHP is free to download and use

PHP is an amazing and popular language!
It is powerful enough to be at the core of the biggest blogging system on the web (WordPress)!
It is deep enough to run the largest social network (Facebook)!
It is also easy enough to be a beginner's first server side language!
Syntax :

A PHP script can be placed anywhere in the document. A PHP script starts with <?php and ends with?>
<?php
// PHP code goes here
?>
Note: PHP statements end with a semicolon (;).

Data type :

Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
  1.     String
  2.     Integer
  3.     Float (floating point numbers - also called double)
  4.     Boolean
  5.     Array
  6.     Object
  7.     NULL
  8.     Resource

Code :
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5985;
var_dump($x);
?>
</body>
</html>

Output :

Variable :

In PHP, a variable starts with the $ sign, followed by the name of the variable. A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for PHP variables:
  1.     A variable starts with the $ sign, followed by the name of the variable
  2.     A variable name must start with a letter or the underscore character
  3.     A variable name cannot start with a number
  4.     A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  5.     Variable names are case-sensitive ($age and $AGE are two different variables)
Code :
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "Myself";
echo "I love " . $txt . "!";
?>
</body>
</html>

Output :
     I love Myself

Function :

The real power of PHP comes from its functions. PHP has more than 1000 built-in functions, and in addition you can create your own custom functions.
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.

Code :
<!DOCTYPE html>
<html>
<body>
<?php
function writeMsg() {
    echo "Hello world";
}
writeMsg();
?>
</body>
</html>

Output :
      Hello world

Array :

An array stores multiple values in one single variable. In PHP, the array() function is used to create an array.
In PHP, there are three types of arrays:

  1.     Indexed arrays - Arrays with a numeric index
  2.     Associative arrays - Arrays with named keys
  3.     Multidimensional arrays - Arrays containing one or more arrays

Code :
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Apple", "Banana", "Orange", "Grapes");
echo count($cars);
?>
</body>
</html>

Output :
     4


Superglobals  variable :

Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always available in all scopes.
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

Code :
<!DOCTYPE html>
<html>
<body>
<?php
$a = 20;
$b = 15;
function addition() {
    $GLOBALS['c'] = $GLOBALS['a'] - $GLOBALS['b'];
}
addition();
echo $c;
?>
</body>
</html>

Output :
    5

Comments

Popular posts from this blog

Part-6 Prac 5 :Create Hello World program using Node.js.