Practical No.
1
1. Printing “Hello” using PHP
<html>
<body>
<?php
echo "Hello!";
?>
</body>
</html>
2. Embedding PHP within HTML
<html>
<body>
<h1>
<?php
echo "Hello World!";
?>
</h1>
</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;
?>
Using print statement
<?php
//Displaying strings
print "Hello, Welcome to PHP Programming";
print "<br/>";
//Displaying strings as multiple arguments
print "Hello, PHP";
print "<br/>";
//Displaying variables
$s = "Hello, PHP";
$x = 10;
$y = 20;
print $s . "<br/>";
print $x . " + " . $y . " = ";
print $x + $y;
?>
5. print r() in PHP
<?php // string variable
$var1 = "Hello PHP"; // integer variable
$var2 = 101; // array variable
$arr= array('0' => "Welcome", '1' => "to", '2' => "PHP");
// printing the variables
print_r($var1);
echo "<br/>";
print_r($var2);
echo "<br/>";
?>
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();
?>
10. Special Type
Null
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
11. Type Juggling
<?php
$var1 = 1;
$var2 = "20";
$var3 = $var1 + $var2; // $var3 is now an integer
echo "Value of var3: $var3<br>";
$var1 = $var1 + 1.3; // $var1 is now a float
echo "Value of var1 after addition: $var1<br>";
// Explicitly cast strings to integers
to suppress warnings
$var1 = 5 * (int)"10 small birds";
// $var1 is integer (50)
echo "Value of var1 after multiplication:
$var1<br>";
$var1 = 5 + (int)"10 Small puppies";
// $var1 is integer (15)
echo "Value of var1 after addition: $var1<br>";
// Display $var1
?>
12. Type Casting
i) <?php
$var1 = 10; // $var1 is assigned the integer value 10.
echo "Value of var1 (integer): $var1<br>";
$var2 = (double) $var1; // $var2 is assigned the double value 10.0.
echo "Value of var2 (double): $var2<br>";
$var3 = (string) $var1; // $var3 is assigned the string value "10".
echo "Value of var3 (string): $var3<br>";
$var1 = (boolean) $var1; // $var1 is assigned the boolean value.
echo "Value of var1 (boolean): " . ($var1 ? 'true' : 'false') . "<br>";
?>
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
?>
14. Operator Precedence and Associativity
<?php
$n1 = 10;
$n2 = 5;
$n3 = 2;
$ans = $n1 + $n2 * $n3;
echo "$n1 + $n2 * $n3 =
$ans</br>";
$ans = ($n1 + $n2) * $n3;
echo "($n1 + $n2) * $n3 = $ans</br>";
?>
15. Constants
sss<?php
define("HELLO","Hello PHP");
echo HELLO, "<br>";
?>