Introduction to PHP
PHP stands for Personal Home Page, but is now known as PHP: Hypertext Preprocessor. It is a general-purpose server-side scripting language that is widely used in web development for tasks such as form handling, file processing, and database access.
Why Choose PHP?
Interpreted Language: PHP is an interpreted language, meaning that PHP code is executed by a web server.
Open Source: It is an open-source, free product that is highly customizable.
Versatile: PHP can be used for various tasks like processing HTML forms, managing files on a server, and connecting to databases.
Basic PHP Syntax
- php code is written in normal text files between <?php and ?> tags
- A PHP file normally contains HTML tags.
- You can have more than one php script in our page
- PHP statements terminated with ;
PHP execution
Server Determination: The server identifies PHP scripts by the .php file extension.
Request Handling: When a browser requests a .php file, the web server calls the PHP processor.
PHP Execution: The server executes the PHP code, then substitutes the output into the HTML.
Client Output: The resulting HTML page is sent to the client. The PHP code itself is never visible to the user, only the output.
NOTE: user never sees the PHP code, only the output in the
page
**
Embedding HTML in PHP Using include
PHP provides constructs such as include and require to embed HTML or other PHP files into the current script. Here's the difference:
include: If the file doesn't exist, PHP will generate a warning but the script will continue.
require: If the file is missing, PHP will stop executing the script and generate a fatal error.
The echo language construct
echo and print are more or less the same. They are both used to output data to the screen.
The print statement can be used with or without parentheses: print or print().
The differences are small:
– echo has no return value while print has a return value of 1 so it can be used in expressions.
$result = echo "Hello"; // ❌ This will cause an error
$result = print "Hello"; // ✅ Works because print returns 1
echo $result; // Output: 1
Since print returns 1, it can be used inside expressions.
– echo can take multiple parameters (although such usage is rare) while print can take one argument.
Using Multiple Arguments in echo
echo "Hello", ", ", "World!"; // Output: Hello, World!
Invalid Usage (Multiple Arguments Not Allowed)
print "Hello", "World"; // ❌ Error: print() only takes one argument
– echo is marginally faster than print.
echo is faster because it does not return anything.
print is slower because it returns 1
✅ When Performance Matters (Use echo)
echo "Fast output";
✅ When You Need a Return Value (Use print)
if (print "Hello") {
echo " - Print returned 1";
}
When to Use echo vs print?
Feature | echo |
print |
---|---|---|
Return Value? | ❌ No | ✅ Returns 1 |
Multiple Arguments? | ✅ Yes | ❌ No |
Speed | 🚀 Faster | 🐌 Slower |
Use in Expressions? | ❌ No | ✅ Yes |
PHP Variables
- All PHP variables should start with a
$
.
php
$name = "Esraa";
- Php variables are global and can be accessed from any php script in same page
$fruit = "apple";
echo $fruit; // Outputs: apple
- There are no type declarations
$number = 10; // Integer
$name = "Esraa"; // String
- Variable names are case sensitive.
$Fruit = "apple"; // Different variable than $fruit
$fruit = "banana"; // Will overwrite the previous one
- An unassigned (unbound) variable has the value NULL
$var; // NULL by default
var_dump($var); // Outputs: NULL
- The unset language construct sets a variable to NULL
$fruit = "apple";
unset($fruit);
var_dump($fruit); // Outputs: NULL
- The IsSet language construct is used to determine whether a variable is NULL , ex :IsSet($fruit)
$fruit = "apple";
if (isset($fruit)) {
echo "The variable is set!";
} else {
echo "The variable is not set!";
}
- If an unbound variable is used in an expression, NULL is forced to a value that is dictated by the context of the use. If the context specifies a number, NULL is coerced to 0; if the context specifies a string, NULL is coerced to the empty string.
$var; // NULL
echo $var + 10; // Outputs: 10 (NULL is coerced to 0)
echo $var . "hello"; // Outputs: hello (NULL is coerced to empty string)
Looking Ahead:
In the next article, we’ll dive into another important topic—functions and arrays in PHP. You’ll learn how to define and use functions effectively and explore how arrays can be leveraged to manage and manipulate data. This is a key area that will help you build more dynamic and organized PHP applications.
Stay tuned, and don't miss it!
Top comments (0)