Unit 1 Iwd
Unit 1 Iwd
Introduction to PHP
Topics
1.1 Introduction to Static and Dynamic Websites
1.2 Introduction to PHP and it’s History
1.3 Basic PHP syntax and file structure
1.4 Output statements: echo and print
1.5 PHP variables and value types
1.6 PHP Constants and magic constants
1.7 PHP Operators and their precedence:
i. Arithmetic operators
ii. Increment-decrement operators
iii. Assignment operators
iv. Logical operators
v. Bitwise operators
vi. Comparison operators
1.8 Decision-making statements: if statement, if-else statement, else-if clause, switch-case
statement, the ? operator
1.9 Loops: while loop, for loop, foreach loop, nesting loops
1.10 Break and continue statements
1.1
Introduction to static and Dynamic websites
Static website
• A static website is made up of webpages
created using HTML,CSS and Javascript etc.
Dynamic website
• Dynamic website is a collection of dynamic
web pages whose content changes
dynamically.
• Static website
https://www.geeksforgeeks.org/static-vs-dynamic-website/
• Dynamic website
What is PHP?
• 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
• Output can also be in the form of images, PDF files, flash movies, etc.
History of PHP
• Timeline for different major versions of PHP:
1.3
Basic PHP syntax and File structure
• Comments in php
A comment in PHP code is a line that is not
read/executed as part of the program. Its only
purpose is to be read by someone who is
looking at the code.
• <html> <body>
<?php
// This is a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body></html>
• Result is :- 10
Working of PHP
• Type php code in any text editor..
• echo vs print:
– echo has no return value, while print has a return value of 1
– echo is marginally faster than print
– echo can take multiple parameters, while print takes only
one
The PHP print Statement
• The print statement can be used with or
without parentheses: print or print().
Display Text
• The following example shows how to output
text with the print command (notice that the
text can contain HTML markup):
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
<?php
$txt1 = "Learn PHP";
$txt2 = ”My Sql";
$x = 5;
$y = 4;
print "<h2>" . $txt1 . "</h2>";
print "Study PHP and " . $txt2 . "<br>";
print $x + $y;
?>
Result:- Learn PHP
Study PHP and My Sql
9
• Display Variables
• The following example shows how to output text and variables with the echo statement:
<?php
$txt1 = "Learn PHP";
$txt2 = “My Sql";
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP and " . $txt2 . "<br>";
echo $x + $y;
?>
• Result:- Learn PHP
Study PHP and My Sql
9
1.5 PHP Variable and value types
Variables are "containers" for storing information.
• Creating (Declaring) PHP Variables
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).
Output Variables
• The PHP echo statement is often used to
output data to the screen.
<? php
$txt = “Computer Game";
echo "I like $txt!";
?>
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
• Result:- 9
$x++; // Increments $x
}
test(); // First function call
?>
1.6 PHP Constants and magic constants
• Constants are like variables, except that once they are
defined, they cannot be changed or undefined.
<?php
// case-sensitive constant name
define("HOME", "Welcome!");
echo HOME;
?>
Result: Welcome!
<?php
// case-insensitive constant name
define("HOME", "Welcome!", true);
echo home;
?>
Result: Welcome!
• const keyword
The const keyword defines constants at compile
time. It is a language construct, not a function.
The constant defined using const keyword
are case-sensitive.
<?php
const MESSAGE="Hello PHP";
echo MESSAGE;
?>
Result: Hello PHP
• Constants are Global
Constants are automatically global and can be used
across the entire script.
<?php
define("GREAT", "INDIA");
function myTest() {
echo GREAT;
}
myTest();
?>
Result: INDIA
__LINE__
• It returns the current line number of the file,
where this constant is used.
<?php
echo "<h3>Example for __LINE__</h3>";
// print Your current line number i.e;4
echo "You are at line number " . __LINE__ . "<br><br>";
?>
O/P:
Example for __LINE__
You are at line number 4
__FILE__
• This magic constant return the full path of the
executed file with the name of the file.
<?php
echo "The file name is : “ . __FILE__;
?>
O/P:
The file name is : C:\xampp\htdocs\test.php
__DIR__
The directory of the file.
<?php
echo __DIR__;
?>
O/P:
C:\xampp\htdocs
__FUNCTION__
If inside a function, the function name is returned.
<?php
function myValue()
{
return __FUNCTION__;
}
echo myValue();
?>
O/P: myValue
__CLASS__
If used inside a class, the class name is returned.
<?php
class Fruits {
public function myValue(){
return __CLASS__;
}
}
$kiwi = new Fruits();
echo $kiwi->myValue();
?>
O/P: Fruits
__METHOD__
If used inside a function that belongs to a class, both
class and function name is returned.
<?php
class Fruits {
public function myValue() {
return __METHOD__;
}
}
$kiwi = new Fruits();
echo $kiwi->myValue();
?>
O/P:
Fruits::myValue
__NAMESPACE__
If used inside a namespace, the name of the
namespace is returned.
<?php
namespace myArea;
function myValue()
{
return __NAMESPACE__;
}
?>
<?php
echo myValue();
?> O/P: myArea
Data types
Variables can store data of different types, and
different data types can do different things.
• PHP Integer
• PHP Float
• PHP Boolean
• PHP NULL Value
• PHP String
• PHP Array
• PHP Object
R.P.Vasava
PHP integer
• An integer data type is a non-decimal number
between -2,147,483,648 and 2,147,483,647.
Rules for integers:
• An integer must have at least one digit
• An integer must not have a decimal point
• An integer can be either positive or negative
• Integers can be specified in: decimal (base 10),
hexadecimal (base 16), octal (base 8), or
binary (base 2) notation
R.P.Vasava
• PHP integer
• In the following example
$x is an integer.
• The var_dump() function
is a built-in function of
PHP.
• The PHP var_dump()
function returns the data
type and value of the
variable.
• In case of string ,it also
includes the size of the
string passed inside the
function.
• Syntax:
var_dump(var1,var2….);
R.P.Vasava
PHP Float
• A float (floating point number) is a number with a
decimal point or a number in exponential form.
• In the following example $x is a float. The PHP
var_dump() function returns the data type and value:
R.P.Vasava
PHP Boolean
• A Boolean represents two possible states: TRUE
or FALSE.
Ex: $x = true;
$y = false;
<?php <?php
$x=10; $x=TRUE;
$y=20; Echo $x;
var_dump($x>$y);
?> o/p: 1
o/p: bool(false)
R.P.Vasava
R.P.Vasava
PHP String
• A string is a sequence of characters, like "Hello
world!".
• A string can be any text inside quotes. You can use
single or double quotes:
<?php
$x = "Hello world!";
var_dump($x);
?>
R.P.Vasava
PHP Array
• An array stores multiple values in one single variable.
• In the following example $cars is an array. The PHP
var_dump() function returns the data type and value:
R.P.Vasava
PHP Object
• Classes and objects are the two main aspects of object-
oriented programming.
• A class is a template for objects, and an object is an instance
of a class.
• When the individual objects are created, they inherit all the
properties and behaviors from the class, but each object will
have different values for the properties.
• Let's assume we have a class named Car. A Car can have
properties like model, color, etc. We can define variables like
$model, $color, and so on, to hold the values of these
properties.
• When the individual objects (Volvo, BMW, Toyota, etc.) are
created, they inherit all the properties and behaviors from the
class, but each object will have different values for the
properties.
• If you create a __construct() function, PHP will automatically
call this function when you create an object from a class.
R.P.Vasava
R.P.Vasava
PHP Operators
• Operators are used to perform operations on variables
and values.
R.P.Vasava
• Arithmetic operator
<?php
$a = 10;
$b = 3;
// Addition
echo $a + $b . "<br>"; // Output: 13
// Subtraction
echo $a - $b . "<br>"; // Output: 7
// Multiplication
echo $a * $b . "<br>"; // Output: 30
// Division
echo $a / $b . "<br>"; // Output: 3.3333333333333
// Modulus
echo $a % $b . "<br>"; // Output: 1
// Exponentiation
echo $a ** $b . "<br>"; // Output: 1000
?> R.P.Vasava
• Logical operators
• The PHP logical operators are used to combine
conditional statements.
R.P.Vasava
• Logical AND
<?php
$x = 100;
$y = 50;
if ($x == 100 and $y == 50)
{
echo "Hello world!";
}
?>
• Logical OR
<?php
$x = 100;
$y = 50;
R.P.Vasava
• Logical AND (&&)
<?php
$x = 100;
$y = 50;
R.P.Vasava
• Logical OR(||)
<?php
$x = 100;
$y = 50;
<?php
$x = 100;
if (!($x == 90))
{
echo "Hello world!";
}
?>
o/p:Hello world!
R.P.Vasava
• Logical XOR
<?php
$x = 100;
$y = 50;
R.P.Vasava
Assignment operators
• The PHP assignment operators are used with
numeric values to write a value to a variable.
• The basic assignment operator in PHP is "=". It
means that the left operand gets set to the
value of the assignment expression on the
right.
R.P.Vasava
• Assignment operator
R.P.Vasava
• Ex: x = y
<?php
$x = 10;
echo $x;
?>
o/p: 10
R.P.Vasava
Ex: x += y
<?php
$x = 20;
$x += 100;
echo $x;
?>
o/p: 120
R.P.Vasava
• Ex: x -= y
<?php
$x = 50;
$x -= 30;
echo $x;
?>
o/p: 20
R.P.Vasava
• Ex: x *= y
<?php
$x = 5;
$x *= 6;
echo $x;
?>
o/p: 30
R.P.Vasava
• Ex: x /= y
<?php
$x = 10;
$x /= 5;
echo $x;
?>
o/p: 2
R.P.Vasava
• Ex: x %= y
<?php
$x = 15;
$x %= 4;
echo $x;
?>
o/p: 3
R.P.Vasava
• Comparison Operators
The PHP comparison operators are used to
compare two values (number or string):
R.P.Vasava
Equal ==
<?php <?php
$x = 100; $x = 100;
$y = 100; $y = “100”;
R.P.Vasava
Identical ===
<?php
$x = 100;
$y = "100";
var_dump($x === $y); // returns false because types are not equal
?>
o/p: bool(false)
:It checks whether two variables have the same value and
are of the same type.
R.P.Vasava
<?php <?php
$x = 100; $x = 100;
$y = 50; $y = “100”;
o/p: bool(false)
R.P.Vasava
<?php
$x = 100;
$y = "100";
var_dump($x !== $y); // returns true because types are not equal
?>
o/p: bool(true)
:In this example, even though $x and $y have the same value, they are
of different types (integer vs. string), so !== returns true.
R.P.Vasava
Greater than >
<?php
$x = 100;
$y = 50;
o/p: bool(true)
R.P.Vasava
<?php
$x = 10;
$y = 50;
o/p: bool(true)
R.P.Vasava
Greater than or equal to >=
<?php
$x = 50;
$y = 50;
o/p: bool(true)
R.P.Vasava
<?php
$x = 50;
$y = 50;
o/p: bool(true)
R.P.Vasava
Increment / Decrement Operators
• The PHP increment operators are used to increment
a variable's value.
• The PHP decrement operators are used to decrement
a variable's value.
R.P.Vasava
++$x Pre-increment
<?php
$x = 10;
echo $x ."</br>";
echo ++$x;
?>
o/p: 10
11
R.P.Vasava
$x++ Post-increment
<?php
$x = 10;
echo $x."</br>";
echo $x++."</br>";
echo $x;
?>
o/p: 10
10
11
R.P.Vasava
--$x Pre-decrement
<?php
$x = 10;
echo $x."</br>";
echo --$x;
?>
o/p: 10
9
R.P.Vasava
$x-- Post-decrement
<?php
$x = 10;
echo $x; o/p: 10
echo "</br>"; 10
9
echo $x--;
echo "</br>";
echo $x;
?>
R.P.Vasava
Example:
<?php
$x = 5;
$y = 10;
$z = ++$x + $y-- - $x++ * --$y;
echo $z;
?>
o/p: -32
R.P.Vasava
• Bitwise operators
The Bitwise operators is used to perform bit-level
operations on the operands. The operators are first
converted to bit-level and then calculation is performed on
the operands.
R.P.Vasava
| (Bitwise OR)
Bitwise OR operator takes two numbers as operands and does OR
on every bit of two numbers. The result of OR is 1 any of the two
bits is 1.
^ (Bitwise XOR )
This is also known as Exclusive OR operator. Bitwise XOR takes
two numbers as operands and does XOR on every bit of two
numbers.
R.P.Vasava
~ (Bitwise Complement)
Add 1 to the given number and change the sign
R.P.Vasava
<?php
$x = 5;
o/p:
$y = 3;
// Bitwise AND
Bitwise & of 5 and 3 is 1
$z = $x & $y;
echo "Bitwise & of 5 and 3 is“ . $z . "<br>";
// Bitwise OR Bitwise | of 5 and 3 is 7
$z = $x | $y;
echo "Bitwise | of 5 and 3 is“ . $z . "<br>";
// Bitwise XOR Bitwise ^ of 5 and 3 is 6
$z = $x ^ $y;
echo "Bitwise ^ of 5 and 3 is" . $z . "<br>";
// Bitwise NOT Bitwise ~ of 5 is -6
$z = ~$x;
echo "Bitwise ~ of 5 is“ . $z. "<br>";
// Bitwise Left shift 5 << 1 will be 10
$y = 1;
$z = $x << $y;
echo "5 << 1 will be“ . $z . "<br>";
// Bitwise Right shift 5 >> 1 will be 2
$z = $x >> $y;
R.P.Vasava
echo "5 >> 1 will be“ . $z . "<br>"; ?>
R.P.Vasava
String operators
There are two string operators.
R.P.Vasava
• Concatenation
<?php
$txt1 = "Hello"; o/p: Hello world!
$txt2 = "world!";
echo $txt1 . $txt2;
?>
R.P.Vasava
• Concatenation assignment
<?php
o/p: Hello world!
$txt1 = "Hello";
$txt2 = " world!";
$txt1 .= $txt2;
echo $txt1;
?>
R.P.Vasava
• Conditional (Ternary) Operator
The ternary operator is a simplified conditional
operator like if / else.
Syntax:
condition ? <expression if true> : <expression if false>
<?php
$age = 20;
print ($age >= 18) ? "Adult" : "Not Adult";
?>
o/p:Adult
R.P.Vasava
– gettype( ) function:
• Syntax: gettype(variable_name);
• Example: $x = 2.5;
echo gettype($x); // Output: double
– var_dump( ) function:
• Syntax: var_dump(variable_name);
• Example: $x = 10;
var_dump($x); // Output: int(10) datatype(value)
• Example: $y = “hi”;
var_dump($y); // Output: string(2) “hi”
datatype(length)value
R.P.Vasava
• Changing/Setting a Variable’s Data Type
2 ways to change/set a variable’s datatype:
-settype( ) function:
• Syntax: settype(variable_name, datatype_name);
• Example: $x = 3.566;
settype($x, “integer”); // $x converted from float to integer
echo $x; // Output: 3
-Type Casting:
• Syntax: variable_name = (datatype_name) another_variable_name;
• Example: $x = 2.53;
$y = (int)$x;
echo $y; // Output: 2
$x temporarily converted to integer and copied to $y
R.P.Vasava
• Ex:
<?php
$a = "123xyz";
settype($a, "integer"); // $a is now integer
echo $a;
?> o/p: 123
• Ex:
<?php
$a = 32;
echo var_dump($a) . "<br>";
?>
o/p: int(32)
R.P.Vasava
• Ex
<?php
$a = 3;
echo gettype($a) . "<br>";
?>
o/p: integer
R.P.Vasava
Casting
• If you want to change type temporary,i.e want
to use inside an expression you can use type
casting
• Syntax: (type)$variable
where type is a type of variable you wish to
cast to.
Ex: $var1=10.9;
echo (int)$var1; //$var1 is now set to 10(int)
R.P.Vasava
Operator Precedence in PHP
• Precedence of operators decides the order of execution of
operators in an expression.
• Following is the list of operators and their precedence with 1
being the highest:
Precedence Operator(s) Associativity Precedence Operator(s) Associativity
1 ** Right 10 ^ Left
2 ++ -- ~ n/a 11 | Left
3 ! n/a 12 && Left
4 * / % Left 13 || Left
5 + - . Left 14 ?: Left
6 << >> Left 15 = Right
7 < <= > >= n/a 16 and Left
8 == != n/a 17 xor Left
9 & Left R.P.Vasava 18 or Left
Unit-I
1.8 Decision-making statements:
if statement,
if-else statement,
else-if clause,
switch-case statement,
the ? operator
1.9 Loops: while loop, for loop, foreach loop
1.10 Break and continue statements
PHP Conditional Statements
•
<?php
$n=12;
if ($n%2==0)
{
echo “Even number";
}
else
{
echo “Odd number";
}
?>
o/p: Even number
• PHP - The if...elseif....else Statement
Use the if....elseif...else statement to specify a new
condition to test, if the first condition is false.
• Syntax
if (condition) {
code to be executed if condition is true;
}
elseif (condition)
{
code to be executed if condition is true;
}
else
{
code to be executed if condition is false;
}
<?php
$marks=90;
if ($marks>=85 && $marks<=100)
{ echo "AA"; }
else if ($marks>=75 && $marks<=84)
{ echo "AB"; }
else if ($marks>=65 && $marks<=74)
{ echo "BB"; }
else if ($marks>=55 && $marks<=64)
{ echo "BC"; }
else if ($marks>=45 && $marks<=54)
{ echo "CC"; }
else if ($marks>=40 && $marks<=44)
{ echo "CD"; }
else if ($marks>=35 && $marks<=39)
{ echo "DD"; }
else
{ echo "FF"; }
?>
O/P: AA
• The PHP switch Statement
• Use the switch statement to select one of many blocks of
code to be executed.
• Syntax:-
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
<?php
$favcolor = "red";
switch ($favcolor)
{
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
o/p:Your favorite color is red!
• PHP Loops
In PHP, we have the following looping statements:
while - loops through a block of code as long as the
specified condition is true
do...while - loops through a block of code once, and
then repeats the loop as long as the specified
condition is true
for - loops through a block of code a specified
number of times
foreach - loops through a block of code for each
element in an array
<?php
$x = 1;
while($x <= 5)
{
echo "The number is: $x <br>";
$x++;
}
?> O/P:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
do
{
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
O/P:
?> The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
O/P: The number is: 6
FOR LOOP
• PHP for loops execute a block of code a specified number of
times.
• The for loop is used when you know in advance how many
times the script should run.
• SYNTAX:-
for (init counter; test counter; increment counter)
{
code to be executed;
}
• init counter: Initialize the loop counter value
• test counter: Evaluated for each loop iteration. If it
evaluates to TRUE, the loop continues. If it evaluates to
FALSE, the loop ends.
• increment counter: Increases the loop counter value
<?php
for ($x = 0; $x <= 10; $x++)
{
echo "The number is: $x <br>";
O/P:-
} The number is: 0
?> The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
The PHP foreach Loop
• The foreach loop works only on arrays, and is
used to loop through each key/value pair in an
array.
• Syntax
foreach ($array as $value)
{
code to be executed;
}
•
<?php
//declare array
$colors = array("red", "green", "blue", "yellow");
// access array elements using foreach loop
foreach ($colors as $value)
{
echo "$value <br>";
}
?>
o/p:-red
green
blue
yellow
Print both the key and the value from the $age array:
<?php
$age = array("Peter"=>"35", “Nick"=>"37", "Joe"=>"43");
while($x < 5)
{
if ($x == 3)
{
break;
}
echo "The number is: $x <br>";
$x++;
}
o/p:The number is: 1
?> The number is: 2
<?php
$x = 0;
while($x < 8)
{
if ($x == 4)
{
$x++; O/P:
continue; The number is: 0
The number is: 1
} The number is: 2
echo "The number is: $x <br>"; The number is: 3
$x++; The number is: 5
The number is: 6
} The number is: 7
?>
<?php
$stack = array('first', 'second', 'third', 'fourth', 'fifth');
foreach($stack AS $v)
{
if($v == 'second')continue;
if($v == 'fourth')break;
echo $v.'<br>';
}
?>
o/p:- first
third
Important Points
• ‘while’ and ‘for’ loops are Entry-controlled loops, but
‘do…while’ is an Exit-controlled loop.
=>
For loop Foreach loop
1. The iteration is clearly 1. The iteration is hidden
visible.
2. The foreach loop is used
2. The for loop is used as a for arrays and
general purpose loop. collections.
(1)
<?php
// Using a for loop
echo "Numbers from 1 to 10 using for loop:";
for ($i = 1; $i <= 10; $i++)
{
echo $i . " ";
}
echo "<br>";
(3)
<?php
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i; // Add the current number to the sum
}
echo "The sum of numbers from 1 to 10 is: $sum";
?>
(4)
<?php
$number = 7;
if ($number % 2 == 0)
{
echo "The number $number is even";
} else
{
echo "The number $number is odd";
}
?>
(5)
<?php
$number = 15;
function isPrime($num) // Function to check if the number is prime
{
if ($num <= 1)
{
return false; // 0 and 1 are not prime numbers
}
for ($i = 2; $i <= sqrt($num); $i++) // Check for factors from 2 to the square root of the number
{ if ($num % $i == 0)
{ return false; // Number is divisible by $i, so it's not prime
}
} return true; // If no factors were found, it's a prime number
}
// Check if the number is prime
if (isPrime($number))
{ echo "The number $number is prime"; }
else
{ echo "The number $number is not prime"; }
?>
(6)
<?php
$num1 = 10;
$num2 = 5;
$operation = 'add'; // Change to 'subtract', 'multiply', or 'divide' to test other operations
echo "The result of $operation $num1 and $num2 is: ";
if ($operation == 'add')
{ echo $num1 + $num2;
} elseif ($operation == 'subtract')
{ echo $num1 - $num2;
} elseif ($operation == 'multiply')
{ echo $num1 * $num2;
} elseif ($operation == 'divide')
{
if ($num2 != 0)
{ echo $num1 / $num2; }
else
{ echo "Error: Division by zero is not allowed!"; }
} else { echo "Invalid operation!";}
?>
(7)
(7) // Check if the sum of the digits raised to the
<?php power of 'digits' equals the original number
// Function to check if a number is Armstrong return $sum == $num;
function isArmstrong($num) }
{
// Find the number of digits in the number // Input: Number to check
$digits = strlen((string)$num); $number = 153; // You can change this number
$sum = 0; // Initialize sum to 0 to test others
$temp = $num; // Store the original number in
temp // Check if the number is Armstrong
// Loop to calculate the sum of digits raised to the if (isArmstrong($number)) {
power of the number of digits echo "$number is an Armstrong number";
while ($temp > 0) } else {
{ echo "$number is not an Armstrong number";
// Get the last digit of the number }
$digit = $temp % 10; ?>
// Add the digit raised to the power of 'digits'
$sum += pow($digit, $digits);
// Remove the last digit from the number
$temp = (int)($temp / 10);
}
(8)
<?php
// Function to check if a number is prime
function isPrime($num)
{
// Check if the number is less than or equal to 1 (not prime)
if ($num <= 1)
{
return false;
}
// Check for factors from 2 to the square root of the number
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
return false; // The number is divisible by $i, so it's not prime
}
}
return true; // If no factors were found, it's a prime number
}
echo "Prime numbers between 1 and 100 are:\n";
for ($num = 1; $num <= 100; $num++) {
if (isPrime($num)) {
echo $num . "\n"; // Print the prime number
}
}
?>
(9)
<?php
function reverseNumber($num) // Function to reverse a number
{
$reversed = 0;
// Loop until the number becomes 0
while ($num > 0) {
// Extract the last digit of the number
$digit = $num % 10;
// Add the digit to the reversed number and shift the digits
$reversed = ($reversed * 10) + $digit;
// Remove the last digit from the original number
$num = (int)($num / 10);
}
return $reversed;
}
$number = 123; // Change this number to test with others
echo "The reversed number of $number is: " . reverseNumber($number) . "\n";
?>
(10)
<?php
$num = 10;
while ($num >= 1)
{
echo $num . "\n";
$num--;
}
?>
(11)
<?php
// Number of terms you want in the Fibonacci series
$terms = 10;
// Initialize the first two numbers of the Fibonacci sequence
$a = 0;
$b = 1;
echo "Fibonacci Series up to $terms terms: <br>";
(13)
<?php
$number = 5; // You can change this to any number
// Loop to display the multiplication table
echo "Multiplication Table for $number:<br>";
for ($i = 1; $i <= 10; $i++)
{
echo "$number x $i = " . ($number * $i) . "<br>";
}
?>
(14) <?php
$number = -5; // Replace this value with the number you want to check
// Check if the number is positive, negative, or zero
if ($number > 0)
{
echo "The number $number is positive.";
} elseif ($number < 0)
{
echo "The number $number is negative.";
} else {
echo "The number is zero.";
}
?>