PHP (Hypertext Preprocessor) is a popular open-source server-side scripting language specifically designed for web development.
- It can be embedded into HTML, making it easy to create dynamic and interactive web pages.
- PHP documents end with the extension
.php
. - PHP scripts are executed on the server, and the result is sent to the client’s browser as plain HTML.
Key Features:
- Simple and easy to learn.
- It can be used to handle forms, session management.
- Integrates seamlessly with various databases like MySQL, PostgreSQL, etc.
- Supports a wide range of web servers, including Apache and Nginx.
- Extensive support for various protocols such as HTTP, SMTP, IMAP, etc.
- Large community and extensive documentation.
PHP Syntax:
Basic PHP Syntax Example:
<?php
$variable = "Hello, World!";
echo $variable;
?>
In this example:
- $variable is a PHP variable.
- echo is used to output the content of the variable.
• PHP code is embedded within HTML using PHP tags. The basic PHP tag is <?php....?>
.
Example:
<!DOCTYPE html>
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<?php
echo "Hello, World!";
?>
</body>
</html>
In this example, the PHP code <?php echo "Hello World!"; ?>
is embedded within HTML to output “Hello, World!” to the browser.
Comments in PHP:
Comments are used to explain the code and make it more readable and are not executed by the PHP engine. They help make the code more readable and maintainable.
Types of Comments:
- Single-line Comments: Use
//
or#
- Multi-line Comments: Enclose comments within
/* … */
Examples:
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multi-line comment
that spans multiple lines
*/
echo "Hello, World!"; // This will output "Hello, World!"
?>
Output in PHP:
The echo
and print
statements are used to output data to the browser.
- echo:
- Can take multiple parameters (though using multiple parameters is not common).
- It is faster than print because it does not return any value.
- print:
- Can only take one argument and returns a value of 1, making it slightly slower than echo.
- It is slower than echo because it always returns 1.
Examples:
<?php
echo "Hello, World!"; // Outputs: Hello, World!
print "Hello, World!"; // Outputs: Hello, World!
?>
Variables in PHP:
Variables in PHP are used to store data.
Rules for Variable Declaration in PHP:
- All variables in PHP must start with a dollar sign ($).
- Variable names are case-sensitive.
- It must start with a letter or underscore.
- Variable names cannot contain spaces.
- You cannot use PHP reserved keywords as variable names.
Examples:
<?php
$greeting = "Hello, World!";
$age = 25;
$price = 19.99;
$is_logged_in = true;
echo $greeting; // Outputs: Hello, World!
echo $age; // Outputs: 25
?>
Variable Scope:
- Local Variables:
- A local variable is a variable declared within a function or a block in PHP. It is accessible only within the scope of that function or block, and it is not available outside of it.
- Global Variables:
- A global variable is a variable declared outside any function or class and is accessible anywhere in the script.
Constants in PHP:
Constants are like variables except that once they are defined, they cannot be changed or undefined.
- They do not use the
$
sign and are defined using thedefine()
function or theconst
keyword (since PHP 7).
Examples:
<?php
// Using define()
define("SITE_NAME", "My Website");
echo SITE_NAME; // Outputs: My Website
// Using const (since PHP 7)
const DB_HOST = "localhost";
echo DB_HOST; // Outputs: localhost
?>