Unit 10 Note 06 php basics
Unit 10 Note 06 php basics
<?php echo "Hello, World!" ; // This will display "Hello, World!" in the browser ?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<?php $name = "John" ;
echo "<h1>Hello, $name!</h1>" ; // This will display "Hello,
John!" on the page ?>
</body>
</html>
Conclusion:
PHP is a powerful tool for web developers. It can handle a wide range of tasks, from
displaying dynamic content to interacting with databases and managing sessions. Its
ability to integrate with HTML and databases makes it a key player in the development
of modern web applications.
Variables
in PHP, variables can hold data of different types. Here are the basic types of variables
in PHP:
Scalar Types:
• Integer: A whole number, positive or negative, without a decimal point.
$age = 25 ; // Integer
• Float (or Double): A number that can have a decimal point.
php
Compound Types:
Special Types:
PHP Comments
Comments are used to add notes in the code. They are not executed and are used for
documentation, explanation, or debugging purposes.
• Single-line Comments:
• These are used for short comments that fit on one line.
• You can use // or #.
// This is a single-line comment $x = 5 ; // Assign value to x # This is also a
single-line comment
• Multi-line Comments:
• These are used for longer comments that span multiple lines.
• You use /* to begin and */ to end the comment.
The scope of a variable in PHP determines where it can be accessed. PHP has different
types of variable scopes:
Global Scope:
• A variable declared outside any function has a global scope and is
accessible throughout the entire script (except inside functions, unless
explicitly referenced).
• To access a global variable inside a function, you need to use
the global keyword or use the super global variable $GLOBALS.
$x = 10 ; // Global variable
function show()
{ global $x ; // Accessing the global variable
echo $x ; // Output: 10 }
show ();
Local Scope:
• A variable declared inside a function is said to have a local scope and
can only be accessed within that function.
function test()
{ $y = 5 ; // Local variable
echo $y ; // Output: 5 }
test ();
echo $y ; // Error: Undefined variable
Static Variables:
• A static variable inside a function maintains its value between function
calls. It's useful when you want the function to "remember" its state
across calls without using global variables.
• A static variable is declared with the static keyword.
function counter() {
static $count = 0 ; // Static variable
$count ++; echo $count ; // Output: 1, 2, 3... }
Loops are used in PHP to execute a block of code multiple times. There are three main
types of loops in PHP:
while Loop
do-while Loop
for Loop
Each loop has its specific use cases depending on how many times you need to repeat
the code and the conditions required.
while Loop
The while loop repeats a block of code as long as the specified condition is true. It
checks the condition before executing the code block.
Syntax:
Example:
<?php $count = 1 ;
while ( $count <= 5 )
{ echo "Count is: $count<br>" ;
$count ++; }
?>
Explanation:
The do-while loop is similar to the while loop, but the difference is that it executes the
code block at least once before checking the condition. The condition is
checked after the code block is executed.
do { // Code to be executed }
while (condition);
• The code inside the do block is executed once before checking the condition.
<?php $count = 1 ;
do {
echo "Count is: $count<br>" ; $count ++; }
while ( $count <= 5 ); ?>
Output:
Explanation:
• Even if the condition is false initially, the code will execute once.
• The loop continues as long as $count is less than or equal to 5, printing and
incrementing the value.
3. for Loop
The for loop is used when you know in advance how many times the code should be
executed. It is typically used for iterating over a fixed number of iterations.
Syntax:
Example:
Explanation:
Comparison of Loops
• while Loop:
• The condition is checked before executing the loop.
• The loop may not run at all if the condition is false initially.
• do-while Loop:
• The loop is guaranteed to run at least once, as the condition is
checked after the loop execution.
• Ideal when you need the code to run at least once, regardless of the
condition.
• for Loop:
• Best suited for count-controlled loops, where you know the number of
iterations beforehand.
• Combines initialization, condition checking, and increment/decrement
in a single line.
Summary:
Conditional operators in PHP are used to evaluate expressions and control the flow of
the program based on the evaluation result. The most common control structure is
the if statement, which allows the program to execute certain code only if a specific
condition is true.
PHP if Statement
The if statement checks a condition and, if the condition is true, it executes the block
of code inside it.
Syntax:
Example:
<?php $age = 18 ;
if ( $age >= 18 )
{ echo "You are an adult!" ; } ?>
Output:
Explanation:
• The condition $age >= 18 is true, so the message "You are an adult!" is printed.
if-else Statement
The if-else statement provides an alternative block of code to execute when the
condition is false.
Example:
<?php $age = 16 ;
if ( $age >= 18 )
{
echo "You are an adult!" ; }
else {
echo "You are not an adult!" ; } ?>
Output:
Explanation:
• The condition $age >= 18 is false, so the else block executes and prints "You
are not an adult!".
if-elseif-else Ladder
Syntax:
Example:
<?php $age = 25 ;
if ( $age < 18 ) { echo "You are a minor." ; }
elseif ( $age >= 18 && $age <= 65 ) { echo "You are an adult." ; }
else { echo "You are a senior." ; } ?>
Output:
Explanation:
• The first condition $age < 18 is false, so it moves to the next condition.
• The second condition $age >= 18 && $age <= 65 is true, so the message "You
are an adult." is printed.
The ternary operator is a shorthand for the if-else statement. It evaluates a condition
and returns one of two values based on whether the condition is true or false.
Syntax:
Example:
Output:
Explanation:
• The condition $age >= 18 is true, so the string "Adult" is returned and printed.
Comparison operators are used to compare two values or expressions. These operators
return a Boolean value (true or false) based on the comparison.
• ==: Equal to
• ===: Identical (equal in value and type)
• !=: Not equal to
• !==: Not identical
• <: Less than
• >: Greater than
• <=: Less than or equal to
• >=: Greater than or equal to
Example:
<?php $x = 10 ; $y = 20 ;
if ( $x == $y ) {
echo "x is equal to y" ; }
else {
echo "x is not equal to y" ; } ?>
x is not equal to y
Explanation:
• The condition $x == $y checks if $x and $y are equal. Since they are not,
the else block is executed.
Logical operators are used to combine multiple conditions. The most common logical
operators are:
<?php $age = 22 ;
$has_permission = true ;
if ( $age >= 18 && $has_permission )
{ echo "Access granted." ; }
else
{ echo "Access denied." ; } ?>
Output:
Access granted.
Explanation:
• The condition $age >= 18 && $has_permission checks if both conditions are
true. Since both are true, the message "Access granted." is printed.
The switch statement is an alternative to if-elseif-else when you need to check a single
variable against multiple values. It evaluates an expression and matches it
with case values.
Syntax:
switch (expression) {
case value1: // Code to be executed if expression is equal to value1 break ;
case value2: // Code to be executed if expression is equal to value2 break ;
default : // Code to be executed if no match is found }
Example:
<?php $day = 3 ;
switch ( $day ) {
case 1 : echo "Monday" ; break ;
case 2 : echo "Tuesday" ; break ;
case 3 : echo "Wednesday" ; break ;
default : echo "Invalid day" ; } ?>
Output:
Wednesday
Explanation:
• The switch statement checks the value of $day and matches it with the
corresponding case value. Since $day is 3, it prints "Wednesday".
These conditional structures allow you to control the flow of your program, making it
respond to different inputs or situations.