What is PHP?
PHP (Hypertext Preprocessor) is a widely used server-side scripting language that is especially suited for web development. It’s embedded within HTML, allowing developers to create dynamic web pages and interact with databases. PHP code is executed on the server, generating HTML content that is then sent to the client’s web browser.
Setting Up PHP Environment
Before you start writing PHP code, you need to set up a local development environment on your computer. This environment includes a web server (like Apache), a database server (like MySQL), and PHP itself. There are several software packages available for setting up such an environment, such as XAMPP, MAMP, or WAMP, which provide all the necessary components in a single installation.
Once you have your environment set up, create a new file with a .php
extension. You can use any text editor of your choice, such as Notepad, Sublime Text, or Visual Studio Code.
Writing Your First PHP Script
Let’s begin by writing a simple “Hello, World!” example to get familiar with PHP syntax.
phpCopy code<?php
echo "Hello, World!";
?>
In this script:
<?php ... ?>
tags enclose PHP code.echo
is a PHP statement used to output text. Here, it outputs the string “Hello, World!”.;
marks the end of a statement in PHP.
Save this code in a file named hello.php
. Now, open your web browser and navigate to http://localhost/hello.php
(or wherever you saved the file in your local server directory). You should see “Hello, World!” displayed in your browser.
Understanding PHP Syntax
PHP syntax is similar to other programming languages like C, Java, and Perl. Some key points to remember:
- PHP code is enclosed within
<?php ... ?>
tags. - Statements in PHP end with a semicolon (
;
). - PHP is not case-sensitive, but it’s a good practice to follow a consistent naming convention for variables and functions.
- Comments in PHP can be single-line (
//
) or multi-line (/* ... */
).
Variables and Data Types
In PHP, variables are used to store data values. Variable names start with a dollar sign ($
). PHP supports various data types:
- Integer: Whole numbers without decimals.
- Float (or Double): Numbers with decimals.
- String: Sequence of characters enclosed within single (
'
) or double ("
) quotes. - Boolean: Represents true or false.
- Array: Stores multiple values in a single variable.
- Object: Instances of user-defined classes.
- NULL: Represents a variable with no value.
phpCopy code<?php
$num = 10; // Integer
$pi = 3.14; // Float
$name = "John"; // String
$is_valid = true; // Boolean
$colors = array("Red", "Green", "Blue"); // Array
$person = new stdClass(); // Object
$null_var = null; // NULL
?>
Outputting Variables
You can output variables using the echo
or print
statements.
phpCopy code<?php
$name = "Alice";
echo "Hello, $name!"; // Outputs: Hello, Alice!
?>
Conclusion
In this lesson, you’ve learned the basics of PHP, including setting up the environment, writing your first PHP script, understanding PHP syntax, declaring variables, and outputting variables. In the next lesson, we’ll dive deeper into control structures and functions in PHP.