PHP MySQL tutorial

PHP Syntax

PHP script can be written anywhere inside a HTML document. It can be inserted between HTML or JavaScript tags.

PHP tags

The most commonly used PHP tag style starts with <?php and ends with ?>

<?php
// Block of PHP code
?>

Short-open tags

If you are using short-open tags then ensure short_open_tag is enabled and set to ON in configuration file php.ini.

<?
// Block of PHP code
?>

Comments in PHP code

Comments are used to describe the code, which is only intended to be read by human beings. Comments do not get compiled or executed. The purpose of comments is to help others understand or remind you later what has been implemented and the purpose of the block of code. There are two types of comments in PHP

  • Single-line Comment
  • Multi-line Comment

<?php
	// This is an example of single-line comment.
	/*
	This is an example of multi-line comment 
		   Which covers multiple lines
	*/
?>

Download the code Run the code

Case sensitivity

  • The keywords (like if, while, for, echo, etc.), function names and classes are NOT case sensitive in PHP.
  • Variable names ARE case sensitive.

Let’s look at the below example, where all the usage of echo is correct.

<?php
	echo "Welcome to the world of PHP<br>";
	ECHO "Welcome to the world of PHP<br>";
	eCHo "Welcome to the world of PHP<br>";
?>

Download the code Run the code

Now, in the below example, variable name is case-sensitive and displays the value of first echo statement only.

<?php
	$KH_Name = "John";
	echo "My name is " .  $KH_Name . "<br>";  // Output: My name is John
	echo "My name is " .  $KH_NAME . "<br>";  // Output: Notice: Undefined variable: KH_NAME
	echo "My name is " .  $kh_name . "<br>";  // Output: Notice: Undefined variable: kh_name
?>

Download the code Run the code

Statement termination by semicolons

In PHP, any statement which is an expression, is terminated by semicolon (;). In the above example, each echo statement is terminated with a semicolon.

<<< PHP TutorialPHP Variables >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .