PHP syntax and variables

PHP is a popular scripting language used to create dynamic web pages. This tutorial will help you understand the basics of PHP, including its syntax, how to add comments, and how to use variables.

1. Introduction to PHP

PHP stands for “Hypertext Preprocessor.” It is a server-side language, which means it runs on the server and generates HTML to be displayed in the browser. PHP is embedded within HTML code and is executed on the server.


2. Basic PHP Syntax

PHP code is placed inside <?php ... ?> tags. Here’s a simple example:

<!DOCTYPE html>
<html>
<body>

<?php
echo "Hello, World!";
?>

</body>
</html>
  • <?php ... ?>: This tells the server to interpret the code inside as PHP.
  • echo "Hello, World!";: This prints “Hello, World!” to the web page.

Note: Each PHP statement ends with a semicolon (;).


3. PHP Comments

Comments are lines of text within your code that are not executed. They are useful for adding notes and explanations. PHP supports two types of comments:

  • Single-line comments: Use // or #.
  <?php
  // This is a single-line comment
  # This is also a single-line comment
  echo "Hello, World!";
  ?>
  • Multi-line comments: Use /* ... */.
  <?php
  /* This is a multi-line comment
     that spans multiple lines */
  echo "Hello, World!";
  ?>

4. PHP Variables

Variables are used to store data. In PHP, a variable starts with a dollar sign ($), followed by the variable name.

Creating Variables:

<?php
$txt = "Hello, World!";
$number = 5;
?>
  • $txt: A variable that stores the string “Hello, World!”.
  • $number: A variable that stores the number 5.

Using Variables:

<?php
$txt = "Hello, World!";
$number = 5;

echo $txt; // Outputs: Hello, World!
echo "<br>"; // Outputs a line break
echo $number; // Outputs: 5
?>

Variable Naming Rules:

  • Variable names must start with a letter or an underscore (_).
  • Variable names can only contain letters, numbers, and underscores.
  • Variable names are case-sensitive ($txt and $TXT are different variables).

String Concatenation:

To combine (concatenate) strings, use the dot (.) operator.

<?php
$txt1 = "Hello,";
$txt2 = "World!";
echo $txt1 . " " . $txt2; // Outputs: Hello, World!
?>

5. Practical Example: Greeting Message

Let’s create a simple PHP script that greets a user by name.

<!DOCTYPE html>
<html>
<body>

<?php
$name = "Alice"; // Variable storing the user's name
echo "Hello, " . $name . "!"; // Outputs: Hello, Alice!
?>

</body>
</html>

In this example, the variable $name holds the user’s name “Alice”, and echo prints the greeting message.


Summary

In this tutorial, we covered the basics of PHP:

  1. Syntax: Embedding PHP code within HTML.
  2. Comments: Adding notes using single-line and multi-line comments.
  3. Variables: Storing and using data with variables.

By practicing these basics, you’ll be on your way to creating dynamic web pages with PHP. Continue exploring more advanced topics and examples to enhance your PHP skills. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top