Practical program exercise 7 PHP Create & Execute Variables for 12th computer applications
7 .PHP Create & Execute Variables
Variable in PHP
- PHP has variables like other programing languages such as C, C++ and Python etc.
- Variables are the storage location, which can store the values for the later manipulations in the program.
- The variable in PHP begins with a dollar ($) symbol and the assignment activity implemented using “=” operator, finally the statement ends with semi colon “;”
- The semicolon indicates the end of statement.
- The main advantage of the PHP variable declaration is, it does not requires to specify the data type keyword separately such as int, char, float, double or string etc.
Operators in PHP
- Operator is a symbol which is used to perform mathematical and logical operations in the programing languages.
Arithmetic operators
- The arithmetic operators in PHP perform general arithmetical operations, such as addition, subtraction, multiplication and division etc.
Assignment Operators
- Assignment operators are performed with numeric values to store a value to a variable.
- The default assignment operator is “=”. This operator sets the left side operant value of expression to right side variable
Increment and Decrement Operators
- Increment and decrement operators are used to perform the task of increasing or decreasing variable’s value.
- This operator is mostly used during iterations in the program logics.
PHP Function
- PHP server side scripting language Functions and array concepts are very important to solve the many complex problems in real world.
- In most of the programming language, a block of segment in a program that perform specific operations like (Insert, Execute, Delete, Calculate, etc.).
- This are known as Functions.
- A Function is a type of sub routine or procedure in a program.
- A Function will be executed by a call to the Function and the Function returns any data type values or NULL value to called Function in the part of respective program.
Pre-defined or system or built in function
- PHP has a wide collection of built-in functions that can be called directly from with in a script, to perform a specific task.
- These built in function are what make PHP a very efficient and productive. Scripting language no installation is required to use these functions.
CA – 7 PHP – CREATE & EXECUTE VARIABLES
QUESTION:
Write a PHP code to demonstrate the usage of global and static variables in PHP.
AIM:
To create and execute a PHP Variables Example program
PROCEDURE:
- Start Xampp server (Apache)
- Go to virtual path folder (C:\xampp\htdocs)
- Create test.php file and type the program
- Execute the program on your Web browser using by this URL link (http://localhost/test.php)
PROGRAM:
<html>
<body>
<?php
$a = 25;
$b = “Hello”;
$c = 5.7;
echo “Number is : “.$a.”<br>”;
echo “String is : “.$b.”<br>”;
echo “Float value : “.$c.”<br>”;
$txt = “INDIA! <br>”;
echo “I love $txt”;
$x = 2;
$y = 2;
echo $x + $y;
function demo()
{
echo “<p>Variable x inside function is:$x</p>”;
}
demo();
echo “<p>Variable x outside function is: $x</p>”;
function myTest()
{
static $a = 0;
echo $a;
$a++;
}
myTest();
echo “<br>”;
myTest();
echo “<br>”;
myTest();
echo “<br>”;
?>
</body>
<html>
OUTPUT:
Number is : 25
String is : Hello
Float value : 5.7
I love INDIA!
4
Notice: Undefined variable: x in C:\xampp\htdocs\var.php on line 17
Variable x inside function is:
Variable x outside function is: 2
0
1
2
RESULT:
The Output was verified successfully