PHP String Functions – Complete Guide for Beginners (with Examples)
In this tutorial, you’ll learn how PHP string functions work with easy examples. We’ll show you how...
Control structures in PHP allow developers to control the flow of code execution. In This tutorial, we will discuss about conditional statements, loops, and jump statements in PHP with examples.
In PHP, control structures determine how the code will execute depending on conditions and loop for repeated content. To make code more dynamic, the code structure is most important for making conditions, execute code repeatedly, and stop the code execution.
Common types include conditional statements, looping structures, and jump statements.
Executes code if the condition is true.
<?php
if (condition) {
// Code to be executed if condition is true
}
?>
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
}
?>
In this example, we are checking the age of a person using the $age variable, which is set to 20.If $age is greater than 18, the code inside the if block will execute. However, if $age is set to 16, the condition will be false, so the if block will not run.
Execute If block of code when condition is true, else block will be execute when the condition is false.
<?php
if (condition) {
// Code to be executed if condition is true
}else{
// Code to be executed if condition is false
}
?>
<?php
$age = 16;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>
In this example, we are checking the age of a person using the $age variable, which is set to 16.If $age is greater than 18, the code inside the if block will execute. However, if $age is set to 20, the condition will be false, so the else block will execute.
Checks multiple conditions.
<?php
if (condition) {
// Code to be executed if condition is true
} elseif (condition) {
// Code to be executed if condition is false and else if condition is true
} else {
// Code to be executed if both condition is false.
}
?>
<?php
$marks = 75;
if ($marks >= 90) {
echo "Grade: A";
} elseif ($marks >= 75) {
echo "Grade: B";
} else {
echo "Grade: C";
}
?>
In this example,We are checking student’s marks and assigns a grade. If $marks are 90 or above, it prints Grade: A; if they are 75 or above (but less than 90), it prints Grade: B; otherwise, it prints Grade: C.
Useful when comparing a single variable with multiple values.
<?php
switch (variable) {
case value1:
// Code to execute if variable == value1
break;
case value2:
// Code to execute if variable == value2
break;
...
default:
// Code to execute if none of the cases match
}
?>
<?php
$day = "Tuesday";
switch ($day) {
case "Monday":
echo "Start of the week.";
break;
case "Tuesday":
echo "Second day of the week.";
break;
default:
echo "Another day.";
}
?>
In this example,We are checking the $day. If it’s Monday, it prints Start of the week.; if it’s Tuesday, it prints Second day of the week.; otherwise, it prints Another day.
In PHP, looping statements allow you to execute a block of code repeatedly until a specific condition becomes false. They are very useful for tasks such as iterating over arrays, printing sequences, or performing repetitive operations efficiently.
Executes the code as long as the condition is true.When the condition becomes false code execution will be stopped
<?php
while (condition) {
// Code to be executed
}
?>
<?php
$i = 1;
while ($i <= 5) {
echo $i . " ";
$i++;
}
?>
In PHP, the do...while loop executes a block of code at least once, and then keeps repeating it as long as the condition evaluates to true.its different from a normal while loop, where the condition is checked before executing the block.
<?php
do {
// Code to be executed
} while (condition);
?>
<?php
$number = 1;
do {
echo "Number: $number <br />";
$number++;
} while ($number <= 5);
?>
In above example, do...while Loop prints numbers 1 to 5. Before check the condition, it runs first, then checks the condition $number <= 5 before repeating.
In PHP, the for loop is used when you know in advance how many times you want to execute a block of code.
<?php
for (initialization; condition; increment/decrement) {
// code to be executed
}
?>
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i <br />";
}
?>
In above example, for Loop starts with $i = 1 , and will run as long as $i <= 5. After each iteration, $i increases by 1. and this way it prints number 1 to 5
In PHP, the foreach loop is used to iterate over arrays. Without any counter, it automatically goes through each element and execute block of code inside the loop for every time.
<?php
foreach ($array as $key => $value) {
// code to be executed
}
?>
<?php
$fruits = ["Apple", "Banana", "Mango"];
foreach ($fruits as $fruit) {
echo "Fruit: $fruit <br />";
}
?>
In PHP, Jump statements are used to change the normal flow of program execution. They allow you to jump out of loops, skip iterations, or stop script execution.
The break statement exits from a loop or switch statement and stop the execution of the loop.
The continue statement skips the current iteration and moves to the next iteration in the loop.
The exit or die statement terminates the execution of the code immediately. But difference it that, With die statement, you can optionally provide a message or status code.
In PHP, control structures are essential for controlling the flow of a program. They allow you to make decisions using conditional statements (if, if...else, switch), repeat tasks efficiently with loops (for, while, do...while, foreach), and manage program flow using jump statements (break, continue, exit, die). In short, the structures is key to writing dynamic, flexible, and efficient PHP programs.
In this tutorial, you’ll learn how PHP string functions work with easy examples. We’ll show you how...
Control structures in PHP allow developers to control the flow of code execution. In This tutorial,...
Learn the basics of PHP with an introduction of variables, data types, and operators to create dynam...
Basic introduction about what is php, benifits of php and how it runs on server and why it's widely...