Mastering PHP: From Basics to Building Dynamic Web Applications

0 0 0 0 0

Chapter 3: Variables, Data Types & Control Flow

🔹 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:

  • Declaring and using variables
  • Understanding PHP's dynamic data types
  • Using operators for computation and logic
  • Writing conditions and control structures
  • Looping through data

🔹 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:

  • Start with $
  • Are case-sensitive
  • Must begin with a letter or underscore

🔹 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)



Back

FAQs


1. What is PHP used for?

PHP is primarily used for creating dynamic web pages and server-side applications such as login systems, e-commerce platforms, and CMSs.

2. Is PHP still relevant in 2025?

Absolutely. PHP continues to power most of the web and is essential in WordPress, Laravel, and web hosting environments.

3. Do I need to install anything to run PHP?

Yes — you can install XAMPP, MAMP, Laragon, or PHP CLI for local development.

4. What databases work with PHP?

MySQL is the most commonly used with PHP, but it also supports PostgreSQL, SQLite, and others.

5. What’s the difference between PHP and JavaScript?

PHP is a server-side scripting language, while JavaScript is primarily client-side, running in the browser.

6. Can I use PHP with HTML?

Yes PHP is often embedded inside HTML to create dynamic pages

7. What is a PHP file extension?

PHP files have the .php extension and are executed on the server.

8. How do I send data from a form to PHP?

Use method="POST" or method="GET" in your form and access data in PHP using $_POST or $_GET.

9. Is PHP good for building APIs?

Yes — PHP can be used to build RESTful APIs, especially with frameworks like Laravel or Slim.

10. What are some popular PHP frameworks?

Laravel, Symfony, CodeIgniter, Zend, and Slim are among the most used PHP frameworks.