0% found this document useful (0 votes)
13 views

PHP Basics

PHP is a widely-used server-side scripting language for web development, known for its compatibility with HTML and databases. The document covers basic PHP syntax, control structures, functions, form handling, and database connections, providing a foundational guide for beginners. Learning PHP enables the creation of dynamic and interactive websites.

Uploaded by

raghad mustafa
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

PHP Basics

PHP is a widely-used server-side scripting language for web development, known for its compatibility with HTML and databases. The document covers basic PHP syntax, control structures, functions, form handling, and database connections, providing a foundational guide for beginners. Learning PHP enables the creation of dynamic and interactive websites.

Uploaded by

raghad mustafa
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP Basics: A Beginner's Guide

Introduction to PHP PHP (Hypertext Preprocessor) is a popular server-side scripting language


used for web development. It is widely used to create dynamic and interactive websites.

Why Learn PHP?


• Open-source and free to use
• Works well with HTML and databases like MySQL
• Supports various frameworks such as Laravel and CodeIgniter
• Used by many websites, including Facebook and WordPress

Setting Up PHP To run PHP, install:


• XAMPP (Windows, Mac) or MAMP (Mac) – includes Apache, MySQL, and PHP
• PHP CLI – for running PHP scripts in the terminal
To check if PHP is installed, run:
php -v

Basic PHP Syntax PHP scripts start with <?php and end with ?>:
<?php
echo "Hello, World!";
?>

Save the file as index.php and run it in a browser through a local server.

Variables and Data Types


<?php
$name = "John"; // String
$age = 25; // Integer
$price = 10.99; // Float
$is_valid = true; // Boolean
?>

Control Structures If-Else Statement


<?php
$score = 80;
if ($score >= 50) {
echo "Pass";
} else {
echo "Fail";
}
?>
Loops
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i <br>";
}
?>

Functions in PHP
<?php
function greet($name) {
return "Hello, $name!";
}

echo greet("Alice");
?>

Working with Forms


<form method="POST" action="process.php">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>

Processing Form Data


<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
echo "Hello, $username!";
}
?>

Connecting to a Database (MySQL Example)


<?php
$conn = new mysqli("localhost", "root", "", "test_db");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";


?>

Conclusion PHP is a powerful language for web development. Learning the basics will help you
create dynamic websites and applications.
Happy Coding!

You might also like