Week 1: Introduction to PHP
Day 1: What is PHP?
Overview of PHP:
- PHP stands for “PHP: Hypertext Preprocessor.”
- It’s a widely-used open-source scripting language, especially suited for web development.
- PHP scripts are executed on the server, and the result is returned to the browser as plain HTML.
History and Purpose:
- Created by Rasmus Lerdorf in 1994.
- Initially used for tracking visits to his online resume.
- Over time, it evolved to support web forms, databases, and dynamic content generation.
Uses in Web Development:
- Server-side scripting: Handling forms, generating dynamic page content, sending and receiving cookies.
- Command-line scripting: Performing tasks like sending emails, generating files, etc.
- Writing desktop applications.
Example:<?php echo "Hello, World!"; ?>
Day 2: Setting Up the Environment
Installing a Local Server:
- XAMPP: Cross-platform, easy to install, comes with Apache, MySQL, PHP, and Perl.
- MAMP: For macOS, includes Apache, MySQL, and PHP.
- WAMP: For Windows, includes Apache, MySQL, and PHP.
Steps to Install XAMPP:
- Download XAMPP from Apache Friends.
- Follow the installation instructions for your operating system.
- Start the XAMPP Control Panel and ensure Apache and MySQL are running.
Writing and Running Your First PHP Script:
- Create a new file named
index.php
. - Add the following code:
<?php echo "Hello, World!"; ?>
- Save the file in the
htdocs
directory (inside the XAMPP installation folder). - Open your browser and navigate to
http://localhost/index.php
.
PHP Syntax and Basic Commands:
- PHP scripts start with
<?php
and end with?>
. - Statements end with a semicolon
;
. - Comments:
- Single-line:
//
or#
- Multi-line:
/* */
Day 3-5: Basics of PHP
Variables and Data Types:
- Variables: Start with a
$
sign, followed by the name.
$text = "Hello, PHP!";
$number = 10;
$float = 10.5;
$is_true = true;
$array = array("apple", "banana", "cherry");
$object = (object) ['property' => 'value'];
- Data Types: String, integer, float, boolean, array, object, null.
Operators:
- Arithmetic Operators:
+
,-
,*
,/
,%
- Assignment Operators:
=
,+=
,-=
,*=
,/=
- Comparison Operators:
==
,!=
,>
,<
,>=
,<=
- Logical Operators:
&&
,||
,!
- Increment/Decrement Operators:
++
,--
Example:
Day 6-7: Control Structures
Conditional Statements:
- If Statement:
0) {
echo “Positive number”;
}
?>
- If-Else Statement:
- If-Elseif-Else Statement:
- Switch Statement:
Loops:
- While Loop:
- Do-While Loop:
<?php $i = 1; do { echo "The number is: $i <br>"; $i++; } while ($i <= 5); ?>
- For Loop:
[mycode]
<?php
for ($i = 1; $i <= 5; $i++) {
echo “The number is: $i <br>”;
}
?>
[/mycode]
- Foreach Loop:
[mycode]
<?php
$colors = array(“red”, “green”, “blue”, “yellow”);
foreach ($colors as $color)
{
echo “$color <br>”;
}
?>
[/mycode]
Do you have any questions about the overview of PHP?