Welcome back to the “Introduction to PHP” tutorial series! In the previous blog post, we set up our development environment and got everything ready for PHP coding. If you’d like to read the previous blog click here Now, let’s dive into the world of PHP programming and explore its basic syntax. By the end of this blog post, you’ll write your first PHP script. Let’s get started!
1. PHP Tags
PHP code is embedded within special tags in an HTML file. The standard way to indicate PHP code is by using the following tags:
<?php
// Your PHP code goes here
?>
You can place the PHP code anywhere in an HTML file. When the server processes the page, it will execute the PHP code between the opening <?php
and closing ?>
tags.
2. Your First PHP Script
Let’s create a simple “Hello, World!” PHP script to ensure everything is working correctly:
- Open your chosen text editor or IDE.
- Navigate to the “php-tutorial” folder in the “htdocs” directory, where you set up your development environment.
- Create a new file named “hello.php.”
- Add the following PHP code to the “hello.php” file:
<?php
echo "Hello, World!";
?>
- Save the file and open your web browser.
- Enter the following URL: http://localhost/php-tutorial/hello.php
Congratulations! You’ve just written and executed your first PHP script. You should see the text “Hello, World!” displayed on the page.
3. PHP Comments
Comments are essential for code readability and understanding. In PHP, you can add single-line comments using double forward slashes, like this:
// This is a single-line comment
For multi-line comments, you can use slash-asterisk to start the comment block and asterisk-slash to end it:
/*
This is a multi-line
comment.
*/
4. Variables and Data Types
In PHP, variables are used to store data. Variable names start with the dollar sign ($) followed by the name. PHP is loosely typed, meaning you don’t need to specify the data type explicitly.
<?php
$name = "John Doe"; // A string variable
$age = 30; // An integer variable
$price = 19.99; // A floating-point variable
$isStudent = true; // A boolean variable
?>
5. Outputting Variables
To display the value of a variable or any other output, you can use the echo
or print
statement:
<?php
$name = "Alice";
echo "Hello, " . $name . "!"; // Output: Hello, Alice!
?>
You can also directly embed variables within double-quoted strings:
<span class="hljs-meta"><?php</span>
<span class="hljs-variable">$name</span> = <span class="hljs-string">"Bob"</span>;
<span class="hljs-keyword">echo</span> <span class="hljs-string">"Hello, <span class="hljs-subst">$name</span>!"</span>; <span class="hljs-comment">// Output: Hello, Bob!</span>
<span class="hljs-meta">?></span>
Conclusion
You have successfully learned the basics of PHP syntax, written your first PHP script, and explored variables and data types. As you continue with this tutorial series, you’ll dive deeper into PHP’s capabilities and learn how to build dynamic and interactive web applications.
In the next blog post, we’ll cover operators, conditional statements, and loops in PHP. So stay tuned for more exciting PHP programming! Happy coding!