Press "Enter" to skip to content

PHP Syntax

PHP (Hypertext Preprocessor) is a scripting language that is primarily used for web development. Here are some of the basic syntax rules for writing PHP code:

  1. PHP code is typically embedded in HTML pages and is enclosed in special PHP tags: <?php and ?>. Code outside of these tags is treated as regular HTML.

  2. PHP statements end with a semicolon (;).

  3. Variables are declared using the dollar sign ($) followed by the variable name. For example, $name = "Arv";.

  4. Strings can be enclosed in single quotes (‘ ‘) or double quotes (” “). For example, $greeting = 'Hello'; or $message = "It's a beautiful day!";.

  5. Comments can be added to PHP code using either two forward slashes (//) or a forward slash and an asterisk (/* */).

  6. Control structures, such as if statements and loops, are used to control the flow of execution in a PHP program.

  7. PHP has a number of built-in functions, such as echo, print, strlen, strpos, substr, date, and many others, that can be used to perform various tasks.

Here is an example of a simple PHP program that demonstrates some of these syntax rules:

<!DOCTYPE html>
	<title>PHP Syntax Example</title>
		$name = "John";
		$age = 25;
		$greeting = "Hello, " . $name . "! You are " . $age . " years old.";
		echo $greeting;

In this example, we declare two variables, $name and $age, and use them to create a string, $greeting, using the concatenation operator (the period or dot). We then use the echo function to output the string to the web page.