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/).
- Get link
- X
- Other Apps
What is PHP?
- 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!
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!
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:
- String
- Integer
- Float (floating point numbers - also called double)
- Boolean
- Array
- Object
- NULL
- 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:
- 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)
<!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:
- Indexed arrays - Arrays with a numeric index
- Associative arrays - Arrays with named keys
- 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
- Get link
- X
- Other Apps
Comments
Post a Comment