PHP Syntax

Let's learn some basic PHP syntax so you can unleash your inner code magician and write some powerful dynamic applications!

Opening and Closing PHP Tags

Consider PHP tags as the doors to another dimension: the PHP dimension. In this new realm, we have the power to command the server and bend it to our will.

To open this interdimensional doorway, we use the <?php tag. It's like the key that unlocks the realm of PHP for us. Here's an example:

<?php
// Your PHP code goes here
?>

The closing tag ?> tells the server, "Okay, I've had my fun. You can relax now. We're done with PHP."

However, should you find yourself enveloped in a PHP-only file, you may leave out the closing tag. It's like living inside the PHP dimension, there's no need to close the door because you're not coming out!

<?php
// Your PHP code goes here

Echo

Now that you've entered the realm of PHP, how do you communicate with the outside world? This is where the echo command comes in handy!

You can think of echo as PHP dimension's messenger. It will relay your messages from the PHP realm to the HTML universe.

Here's echo in action:

<?php

echo "Hello, HTML Universe!";

?>

Sending your message across the universe with echo is great; however, if you are only outputting a single line there's an even quicker way to do this using the following syntax:

<?= "Hello again, HTML Universe!"; ?>

Consider <?= ?> as a teleportation device, instantly teleporting your messages from PHP dimension to the HTML universe, without even needing to utter echo. It's shorthand syntax for <?php echo. The snippet above is equivalent to this:

<?php echo "Hello again, HTML Universe!"; ?>

So there you go! With echo and <?= ?> in your arsenal, you'll never lose touch with the HTML universe, no matter how deep you journey into the realm of PHP!

PHP Comments

In your PHP journey, you might find yourself needing to leave behind notes or explanations - whispers for your future self, or other explorers venturing into your code. That's where comments come in!

PHP supports several types of comments. Let's take a look at them!

Single-line Comments

A single-line comment is like a quiet murmur, only audible to those who are close enough to hear it. You start it with '//' and it ends when the line ends:

<?php
// This is a single-line comment
echo "Hello, HTML Universe!";
?>

Multi-line Comments

A multi-line comment is like a whispering ghost, it can carry a long message across multiple lines. You start it with '/' and end it with '/':

<?php
/* This is a multi-line comment
   It continues onto this line */
echo "Hello again, HTML Universe!";
?>

Comments will never be displayed on the screen. They serve as guides not only for others who might wander into your code but also for your future self who might revisit this path.

Adding comments can make your code more understandable and maintainable. They can be used to explain the purpose of a block of code, describe what individual functions do, provide details about complex algorithms, or even leave notes on why certain coding decisions were made.

Therefore, although adding comments might seem like extra work, it's a good habit that pays dividends in the long run. It's your way of ensuring that your code speaks and tells its story, even when you're not there to narrate it!

Previous Next

Written © 2024 Written Developer Tutorials and Posts.

𝕏