PHP Basics Introduction to PHP Programming Presented by: Shahabaj Khan
Introduction to PHP • What is PHP? - PHP: Hypertext Preprocessor - Server-side scripting language - Widely used for web development • Why use PHP? - Easy to learn - Open-source - Cross-platform
Variables in PHP $variable_name = value; Example: $name = "John"; $age = 25; Variables start with $, are case-sensitive, and do not require explicit type declaration.
Data Types in PHP Common Data Types: • String • Integer • Float • Boolean • Array • Object
Control Structures in PHP Conditional Statements: if (condition) { // code to execute if true } elseif (condition) { // code for elseif } else { // code to execute if false } Loops: • while • for • foreach
Functions in PHP Definition: function functionName() { // code to execute } Example: function sayHello() { echo "Hello, World!"; }
Working with Forms in PHP HTML Form Example: <form method="POST" action="submit.php"> Name: <input type="text" name="name"> <input type="submit"> </form> PHP Script to Handle Form: $name = $_POST["name"]; echo "Hello, " . $name;
PHP and MySQL Connecting to a Database: $conn = mysqli_connect("hostname", "username", "password", "database"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }