Php beginners guide

Introduction

PHP is a popular server-side scripting language designed for web development. It's embedded in HTML and used to manage dynamic content, databases, and more.

Variables

In PHP, variables start with the $ sign followed by the variable name:


$color = 'red';
$age = 30;
  

Data Types

PHP supports various data types including integers, float, string, array, and objects:


$int = 42;          // Integer
$float = 42.0;      // Float
$string = 'Hello';  // String
$array = array(1, 2, 3); // Array
  

Functions

Functions in PHP are blocks of reusable code:


function add($x, $y) {
    return $x + $y;
}
  

Conditions and Loops

PHP supports conditional statements and loops:


if ($x > 10) {
    echo 'x is greater than 10';
} elseif ($x == 10) {
    echo 'x is 10';
} else {
    echo 'x is less than 10';
}

for ($i = 0; $i < 5; $i++) {
    echo $i;
}

$y = 5;
while ($y > 0) {
    echo $y;
    $y--;
}
  

Arrays

Arrays in PHP can contain multiple values:


$colors = array('red', 'green', 'blue');
echo $colors[1]; // Outputs 'green'
  

Form Handling

PHP is widely used for form handling and submission:


$name = $_POST['name'];
echo 'Hello, ' . $name;