Introduction to php
php is a widely-used open-source server-side scripting language designed especially for web development and can be embedded into HTML.
What is php?â
- php stands for php: Hypertext Preprocessor (a recursive acronym)
- php is a server-side scripting language
- php scripts are executed on the server
- php is free to download and use
- php files have the extension
.php
What Can php Do?â
- php can generate dynamic page content
- php can create, open, read, write, delete, and close files on the server
- php can collect form data
- php can send and receive cookies
- php can add, delete, modify data in your database
- php can be used to control user-access
- php can encrypt data
Why php?â
- php runs on various platforms (Windows, Linux, Unix, macOS, etc.)
- php is compatible with almost all servers used today (Apache, Nginx, IIS, etc.)
- php supports a wide range of databases (MySQL, PostgreSQL, SQLite, etc.)
- php is free â download it from the official php resource: php.net
- php is easy to learn and runs efficiently on the server side
A Basic php Scriptâ
A php script starts with <?php and ends with ?>:
<?php
echo "Hello, World!";
?>
Output:
Hello, World!
php Syntaxâ
php code is executed on the server and the result is returned to the browser as plain HTML. A php file normally contains HTML tags along with php scripting code.
<!DOCTYPE html>
<html>
<body>
<h1>My First php Page</h1>
<?php
echo "Hello from php!";
?>
</body>
</html>
Note: php statements end with a semicolon (;). The closing tag ?> automatically implies a semicolon, so it is not required on the last line.
php Commentsâ
Comments are used to explain code and are ignored by the php engine:
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a
multi-line comment
*/
echo "php Comments Example";
?>
php Case Sensitivityâ
In php, keywords (e.g. if, else, while, echo, etc.) and user-defined functions are NOT case-sensitive. However, variable names ARE case-sensitive.
<?php
ECHO "Hello World!<br>"; // valid
echo "Hello World!<br>"; // valid
Echo "Hello World!<br>"; // valid
$color = "red";
echo $color; // outputs: red
echo $Color; // undefined variable error
?>
How php Worksâ
- The user sends a request to the web server
- The web server passes the
.phpfile to the php interpreter - php executes the script and generates HTML output
- The server sends the HTML back to the user's browser
- The browser renders the HTML page
php is processed entirely on the server side â the client never sees your php code, only the resulting HTML.