Practical No 1 - PHP
Practical No 1 - PHP
1
1. Printing “Hello” using PHP
<html>
<body>
<?php
echo "Hello!";
?>
</body>
</html>
3. PHP comments
a) Single line comments b) Multiline comments
<html> <html>
<body> <body>
<?php <?php
//This is single line comment /*
#This is also single line comment This is a multi line comment
echo "Hello World!"; block
?> that spans over more than one
</body> line
</html> */
echo "Hello World!";
?>
</body>
</html>
4. The PHP echo and print
Using echo statement
<?php
//Displaying strings
echo "Hello, Welcome to PHP Programming";
echo "<br/>";
//Displaying strings as multiple arguments
echo "Hello", " Welcome", " PHP";
echo "<br/>";
//Displaying variables
$s = "Hello, PHP";
$x = 10;
$y = 20;
echo $s . "<br/>";
echo $x . " + " . $y . " = ";
echo $x + $y;
?>
6. Variables in PHP
<html>
<body>
<?php
// Declaration of variables
$txt = "Hello World!";
$number = 10;
// Display variables value
echo $txt;
echo "<br>";
echo $number;
?>
</body>
</html>
7. Variable Scope
a) Local Variable
<?php
$num = 20; // Global variable
function local_var() {
$num = 10;
echo "local num =
$num<br/>";
}
local_var();
//echo "num outside local_var() = $num<br/>";
echo "Variable num outside local_var() = $num<br/>";
?>
b) Global Variable
<?php
//declare global variable
$num = 20;
function local_var() {
global $num;
echo "Access global variable
within function $num<br/>";
}
local_var();
//echo "global variable outside local_var()=$num<br/>";
echo "global num variable of local var()=$num<br/>";
?>
c) Static Variable
<?php
function static_var() {
static $x=1;
$y=1;
$x++;
$y++;
echo "x=$x <br/>";
echo "y=$y <br/>";
} static_var();
static_var();
static_var(); ?>
8. PHP $ and $$ variables
<?php
$x="Hello";
$$x="PHP";
echo $x."<br/>";
echo $$x."<br/>";
echo $Hello;
?>
9. Data Types
a) Scalar type (Integer, float, string, Boolean)
<?php
$a=11;
$b=11.22;
$c = "Hello PHP";
$d = True;
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
?>
b) Compound type
i) Array
a) <?php
$intArray = array(10, 20, 30);
echo "First Element: $intArray[0] <br/>";
echo "Second Element: $intArray[1] <br/>";
echo "Third Element: $intArray[2] <br/>";
?>
b) <?php
$a = [10, 20, 30, 40];
print_r($a);
?>
ii) Object
<?php
class Vehicle
{
function car()
{
echo "City Honda";
}
}
$obj1 = new Vehicle;
$obj1->car();
?>
ii) <?php
$count = "5";
echo gettype($count) . "<br>";
settype($count, 'int');
echo gettype($count) . "<br>";
?>
13. Expression and Operators
<?php
$a = 10;
$b = 20;
$c = "30";
$arr1 = [1, 2];
$arr2 = [3, 4];
echo $a + $b . "<br>"; // Arithmetic operator
echo ($a -= 5) . "<br>"; // Assignment operator
echo ($a < $b) . "<br>"; // Comparison operator
echo (++$a) . "<br>"; // Increment operator
echo (($a > 5 && $b < 50) ? 1 : 0) . "<br>"; // Logical operator
echo $a . intval($c) . "<br>"; // Concatenate $a with $c as integer
print_r($arr1 + $arr2); echo "<br>"; // Array operators
echo ($a > $b ? "Yes" : "No") . "<br>"; // Conditional operator
echo ($a <=> $b) . "<br>"; // Spaceship operator
echo ($a & $b) . "<br>"; // Bitwise operator
?>
15. Constants
sss<?php
define("HELLO","Hello PHP");
echo HELLO, "<br>";
?>