... ArdiLand Institute of Technology PHP Programming Language – Beginner to Advanced Guide | Ardiland Institute of Technology
540-440-1540‬
USD ($)
$
United States Dollar
Br
Ethiopian Birr

PHP Programming Language – Beginner to Advanced Guide

Created by Adugna Asrat in Quick Notes 27 Mar 2025
Share

🚀 What is PHP?

PHP stands for Hypertext Preprocessor. It is a server-side scripting language designed for web development but also used as a general-purpose language.

✅ Runs on the server and generates dynamic HTML
✅ Open-source and easy to learn
✅ Powers websites like Facebook, WordPress, Wikipedia
✅ Often used with MySQL for backend development


🧱 1. PHP Syntax and Basics

How PHP Code Looks

PHP code is embedded within HTML using <?php ?> tags.

<?php

echo "Hello, Ardiland!";

?>

To run PHP, you need a local server (like XAMPP, WAMP) or upload to a web server.


Variables in PHP

Variables in PHP start with a $ symbol. No need to declare types.

<?php

$name = "Maya";

$age = 21;

?>

$name is a string
$age is an integer


📊 2. Data Types in PHP

Common data types include:

  • Integer ($age = 25;)

  • Float ($price = 19.99;)

  • String ($name = "Maya";)

  • Boolean ($isActive = true;)

  • Array ($fruits = ["apple", "banana"];)

  • Null ($x = null;)


🔁 3. Conditional Statements and Loops

Conditional Statement

<?php

if ($age >= 18) {

    echo "Adult";

} else {

    echo "Minor";

}

?>

Loop – For Example

<?php

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

    echo $i;

}

?>

While Loop

<?php

$i = 0;

while ($i < 5) {

    echo $i;

    $i++;

}

?>


🧠 4. Functions

Functions in PHP are defined using the function keyword.

<?php

function greet($name) {

    return "Hello, " . $name;

}

echo greet("Christian");

?>


📋 5. Arrays

Indexed Array

<?php

$colors = ["red", "green", "blue"];

echo $colors[0]; // red

?>

Associative Array

<?php

$user = ["name" => "Maya", "age" => 22];

echo $user["name"]; // Maya

?>


🗃️ 6. Forms and $_GET / $_POST

HTML + PHP Form Handling Example:

<form method="post" action="welcome.php">

  Name: <input type="text" name="name">

  <input type="submit">

</form>

welcome.php:

<?php

$name = $_POST['name'];

echo "Welcome, " . $name;

?>

$_GET is used when method="get"
$_POST is used when method="post"


🧰 7. Connecting PHP to MySQL

Step-by-step:

  1. Create a database in phpMyAdmin

  2. Use mysqli_connect() to connect

<?php

$conn = mysqli_connect("localhost", "root", "", "test_db");

if (!$conn) {

    die("Connection failed: " . mysqli_connect_error());

}

echo "Connected successfully!";

?>

✅ Use mysqli_query() to run SELECT/INSERT/UPDATE queries
✅ Always sanitize user input to prevent SQL injection


🔐 8. Security Best Practices

✅ Use htmlspecialchars() to prevent XSS
✅ Always validate form data
✅ Hash passwords with password_hash()
✅ Never trust user input directly in SQL


🧑‍💻 9. PHP in the Real World

PHP is used in:

  • WordPress CMS

  • E-commerce platforms like WooCommerce, Magento

  • Web portals, membership systems

  • RESTful APIs (with Slim, Laravel)

  • Server-side scripting and automation

Comments (0)

Share

Share this post with others