In this article PHP Syntax and Variables we give the information about Variables are used to store data. Variables in PHP start with a $ sign.
PHP Syntax and Variables:
To understand the basics of PHP it is most important to know its syntax and usage of variables.
Syntax of PHP
PHP is written with HTML. PHP code is always written inside <?php … ?> tags.
Example of PHP syntax:
<?php
//this is a php code
echo “Hello world!”;
?>
Output:
Hello world!
Using PHP with HTML:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo “Learning PHP is fun!”;
?>
</body>
</html>
Output:
Learning PHP is fun!
//This is a single line comment
Multi-line comment:
<?
This is a multi-line
There is a comment.
?>
PHP Variables
Variables are used to store data. Variables in PHP start with a $ sign.
Features of PHP Variables:
- Variables start with $ sign.
- The variable name must start with a letter (a-z, A-Z) or an underscore (_).
- Variable names are case sensitive (e.g., $Name and $name are different).
Example:
<?php
$name = “Rajveer”;
$age = 15;
echo “My name is $name and my age is $age years.”;
?>
Output:
My name is Rajveer and I am 15 years old.
Use of variables in php
Addition of numbers:
<?php
$a = 10;
$b = 20;
$sum = $a + $b;
echo “Addition of two numbers: $sum”;
?>
Output:
Addition of two numbers: 30
Text with variables:
<?php
$greeting = “Hello”;
$name = “Sumit”;
echo “$greeting, $name!”;
?>
Output:
Hello, Sumit!
Scope of variables in PHP
Where variables can be used depends on their scope.
Local Scope: Variables are declared inside the function.
<?php
function test() {
$x = 10; // Local Scope
echo $x;
}
test();
?>
Global Scope: Variables are declared outside the function.
<?php
$y = 20; // Global Scope
function test() {
global $y;
echo $y;
}
test();
?>
Static Scope: Static variables retain their value even when the function is called repeatedly.
<?php
function test() {
static $count = 0;
$count++;
echo $count;
}
test(); // 1
test(); // 2
?>
Conclusion
PHP’s syntax is simple and the use of variables provides flexibility. Once you understand this, you can dive deeper into PHP, such as conditionals and loops.
PHP Tags and Delimiters
PHP tags and delimiters are used to determine where PHP code begins and ends. It helps to embed PHP with HTML.
PHP Tags
Different types of tags can be used to write PHP code in HTML.
a) Standard PHP Tags
These are the most common and recommended tags.
<?php
// PHP code will be written here
?>
Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo “This is a standard PHP tag.”;
?>
</body>
</html>
Output:
This is a standard tag of PHP.
b) Short Open Tags
Use of short tags <? … ?>. This will only work if short_open_tag is enabled on the server.
<?
echo “This is a short tag.”;
?>
Note: Using short tags is not recommended as it is not enabled on all servers.
c) ASP-Style Tags
This is part of the old coding style, which used <% … %>.
<?
echo “This is an ASP style tag.”;
?>
Note: This is not available by default in PHP and is now best avoided.
d) Short Echo Tags
These are written as <?= … ?> and are a simplified form of standard PHP tags.
<?= “This is a short echo tag.”; ?>
Output:
This is short echo tag.
Note: This is supported by default in PHP 5.4 and later.
PHP Delimiters
Tags in PHP are used to mark the beginning and end of code.
Delimiters with PHP Tags
- Start delimiter: <?php or other tags.
- End Delimiter: ?>
Example:
<?php
echo “Using PHP delimiters.”;
?>
Embedding PHP in HTML
PHP tags are used to write PHP code inside HTML.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>Mixing PHP and HTML</h1>
<?php
echo “This is PHP code inside HTML.”;
?>
</body>
</html>
Output:
This is PHP code inside HTML.
Use of Multiple PHP Blocks
You can use multiple PHP tags in the same HTML page.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>Multiple blocks of PHP</h1>
<?php
echo “First PHP block.”;
?>
<p>This is HTML text.</p>
<?php
echo “Second PHP block.”;
?>
</body>
</html>
Output:
First PHP block.
This is HTML text.
Second PHP block.
Closing Tags PHP
?> tag is used at the end of PHP code.
But, if the file contains only PHP code, it is not necessary to use the ?> closing tag.
Example (without closing tag):
<?php
echo “This is a single PHP file.”;
// closing tag is not required
Correct use of PHP tags
- Always use standard tags (<?php … ?>).
- Avoid using short tags or ASP-style tags.
- Keep PHP blocks separated to make the code better readable.
Conclusion
PHP tags and delimiters help separate PHP code from HTML in web development. Using the correct tags and delimiters ensures that the code works correctly on all servers.
PHP Data Types:
It is essential to have proper knowledge of variables and their data types as well as the scope of variables in PHP. It makes PHP code better and effective.
Example:
<?php
$name = “Yashraj”;
$age = 10;
echo “My name is $name and my age is $age years.”;
?>
Output:
My name is Yashraj and I am 10 years old.
Data Types in PHP
PHP is a loosely-typed language, which means you don’t need to declare the data type of a variable. PHP automatically detects the data type of the variable.
Main data types in PHP:
data type description example
String text data $text = “Hello”;
Integer $number = 100;
Float decimal number $price = 10.5;
Boolean true/false $isAvailable = true;
Array stores multiple values $colors = array(“red”, “green”, “blue”);
Object custom data type (see example below)
NULL No value $value = NULL;
Examples of data types:
1.String:
<?php
$text = “This is a string.”;
echo $text;
?>
Integer:
<?php
$num = 50;
echo “This number is: $num”;
?>
Float:
<?php
$price = 99.99;
echo “This price is: ₹$price”;
?>
Boolean:
<?php
$isAvailable = true;
echo $isAvailable ? “Available.” : “Not available.”;
?>
Array:
<?php
$colors = array(“red”, “green”, “blue”);
echo “First color: ” . $colors[0];
?>
Object:
<?php
class Car {
public $model;
public function __construct($model) {
$this->model = $model;
}
}
$car = new Car(“Tesla”);
echo “Car Model: ” . $car->model;
?>
NULL:
<?php
$value = NULL;
echo “This variable is NULL.”;
?>
Variable Scope in PHP
The scope of a variable determines in which part of the code the variable is available. There are mainly four types of scopes of variables in PHP:
a) Local Scope
Variables which are declared inside a function are called local variables.
They are available only inside that function.
<?php
function test() {
$x = 10; // local variable
echo “Value of local variable: $x”;
}
test();
// echo $x; // error: $x is not available here
?>
b) Global Scope
Variables that are declared outside the function are global variables.
Global keyword is used to use them inside the function.
<?php
$y = 20; // global variable
function test() {
global $y;
echo “Value of global variable: $y”;
}
test();
?>
c) Static Scope
Static variables retain their value even if the function terminates.
This prevents the value from being reset every time the function is called.
<?php
function counter() {
static $count = 0; // static variable
$count++;
echo “Count: $count <br>”;
}
counter(); //1 counter(); // 2 counter(); //3
?>
d) Super globals
There are some special variables in PHP which are available everywhere.
Example: $_POST, $_GET, $_SESSION, $_COOKIE
Example (using super globals):
<!DOCTYPE html>
<html>
<body>
<form method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’];?>”>
Name: <input type=”text” name=”name”>
<input type=”submit”>
</form>
<?php
if ($_SERVER[“REQUEST_METHOD”] == “POST”) {
$name = $_POST[‘name’];
echo “My Name is: $name”;
}
?>
</body>
</html>
Conclusion
Using variables in PHP is simple, but it is essential to understand their scope and data types. This makes the code better and organized.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining