PHP is a powerful scripting language designed for web development but also used as a general-purpose programming language.
Setting Up a PHP File
All PHP files should have a .php file extension. A PHP file normally contains HTML tags, and some PHP scripting code. Below is a simple PHP file:
<!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>
Comments in PHP
Comments in PHP are used to leave notes or to prevent the execution of code without deleting it. PHP supports several ways of commenting:
// This is a single-line comment # This is also a single-line comment /* This is a multiple-lines comment block that spans over multiple lines */
Variables in PHP
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. Here's an example:
<?php $txt = "Hello world!"; $x = 5; $y = 10.5; ?>
PHP Echo and Print Statements
Echo and print are more or less the same. They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. Echo can take multiple parameters (although such usage is rare) while print can take one argument. Echo is marginally faster than print. Here's an example:
<?php echo "<h2>PHP is Fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This ", "string ", "was ", "made ", "with multiple parameters."; ?>
PHP Data Types
PHP supports the following data types: String, Integer, Float (floating point numbers - also called double), Boolean, Array, Object, NULL, and Resource.
Here's an example of PHP string:
<?php $x = "Hello world!"; $y = 'Hello world!'; echo $x; echo "<br>"; echo $y; ?>
This is a basic introduction to PHP. PHP is a vast language with many features like conditional statements, loops, functions, error handling, form handling, file handling, and more. I recommend checking out some comprehensive PHP tutorials or documentation for more advanced topics.
PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing