Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🔹 1. Introduction
Now that you’ve set up your PHP environment and learned
basic syntax, it’s time to learn how to store, process, and control
data using PHP.
This chapter will walk you through:
🔹 2. Variables in PHP
A variable in PHP is a container used to store data — like
numbers, strings, arrays, etc.
✅ Syntax:
$variableName
= value;
✅ Example:
$name
= "Alice";
$age
= 28;
$price
= 99.99;
✅ PHP variables:
🔹 3. Data Types in PHP
PHP is loosely typed, meaning variables can change
types automatically.
✅ Primary Data Types:
Data Type |
Example |
String |
$name =
"John"; |
Integer |
$age = 30; |
Float |
$price =
99.99; |
Boolean |
$isValid =
true; |
Array |
$colors =
array("red", "blue"); |
Object |
Created from
a class |
NULL |
$x = NULL; |
✅ Use var_dump($variable) to
check the value and type of a variable.
🔹 4. Operators in PHP
✅ Arithmetic Operators:
$a
= 10;
$b
= 2;
echo
$a + $b; // 12
Operator |
Name |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus |
✅ Assignment Operators:
$x
= 10;
$x
+= 5; // $x is now 15
✅ Comparison Operators:
$a
== $b; // equal
$a
!= $b; // not equal
$a
> $b; // greater than
✅ Logical Operators:
$a
&& $b; // and
$a
|| $b; // or
!$a; // not
🔹 5. Conditional
Statements
✅ if, else, elseif:
$score
= 75;
if($score
>= 90){
echo "A grade";
}
elseif($score >= 75){
echo "B grade";
}
else {
echo "C grade";
}
✅ switch:
$role
= "admin";
switch($role){
case "admin":
echo "Welcome Admin!";
break;
case "editor":
echo "Welcome Editor!";
break;
default:
echo "Unknown role.";
}
✅ Use switch when checking one
variable against many values.
🔹 6. Loops in PHP
✅ for Loop:
for($i
= 0; $i < 5; $i++){
echo "Line $i<br>";
}
✅ while Loop:
$i
= 0;
while($i
< 5){
echo "$i ";
$i++;
}
✅ do...while Loop:
$i
= 0;
do
{
echo "$i ";
$i++;
}
while($i < 5);
✅ foreach Loop (Best for Arrays):
$fruits
= ["Apple", "Banana", "Mango"];
foreach($fruits
as $fruit){
echo "$fruit<br>";
}
🔹 7. Break and Continue
✅ break:
for($i
= 0; $i < 10; $i++){
if($i == 5) break;
echo "$i ";
}
✅ continue:
for($i
= 0; $i < 10; $i++){
if($i == 5) continue;
echo "$i ";
}
✅ Summary Table: Chapter 2
Concept |
Example |
Variable |
$name =
"John"; |
Data Type |
int, string,
array |
Operator |
$a + $b, $a ==
$b |
if Statement |
if($a >
$b){} |
switch |
switch($role){} |
for Loop |
for($i = 0;
$i < 5; $i++) |
foreach |
foreach($arr
as $val) |
PHP is primarily used for creating dynamic web pages and server-side applications such as login systems, e-commerce platforms, and CMSs.
✅ Absolutely. PHP continues to power most of the web and is essential in WordPress, Laravel, and web hosting environments.
Yes — you can install XAMPP, MAMP, Laragon, or PHP CLI for local development.
MySQL
is the most commonly used with PHP, but it also supports PostgreSQL,
SQLite, and others.
PHP is a server-side scripting language, while JavaScript is primarily client-side, running in the browser.
✅ Yes — PHP is often embedded inside HTML to create dynamic pages
PHP
files have the .php extension and are executed on the server.
Use method="POST" or method="GET" in your form and access data in PHP using $_POST or $_GET.
✅ Yes — PHP can be used to build RESTful APIs, especially with frameworks like Laravel or Slim.
Laravel, Symfony, CodeIgniter, Zend, and Slim are among the most used PHP frameworks.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)