When starting with PHP, itβs important to understand its basic syntax. This is the foundation of every PHP project youβll build.
PHP code must be written inside special tags so the server can identify it.
<?php
echo "Hello, World!";
?>
π Explanation:
<?php ... ?> β The standard PHP tag.echo is used to output text to the browser.Comments are ignored by the PHP engine but help developers understand the code.
<?php
// Single-line comment
# Another single-line comment
/*
Multi-line comment
This can span multiple lines
*/
?>
π Explanation:
// β Single-line comment.# β Another way to write single-line comments./* ... */ β Multi-line comments.Variables are used to store data. They always start with a $ sign.
<?php
$name = "John"; // String
$age = 25; // Integer
$is_student = true; // Boolean
echo "My name is $name and I am $age years old.";
?>
π Explanation:
$name, $age, $is_student are variables.My name is John and I am 25 years old.Constants are like variables, but their value cannot be changed once defined.
<?php
// Defining a constant
define("SITE_NAME", "My PHP Blog");
// Using the constant
echo "Welcome to " . SITE_NAME;
// Trying to change value (Not allowed)
// SITE_NAME = "Another Blog"; β Error
?>
π Explanation:
define("SITE_NAME", "My PHP Blog") β Creates a constant.$ sign.β Now you know the basics of PHP syntax: tags, comments, variables, and constants.