Loading...
Friday 27 March 2020

PHP Basic Syntax and Tags

Basic PHP Syntax

  • Syntax is way or structure in which statements are written in computer program
  • PHP Syntax is simple and easy to understand
  • Standard PHP Syntax is, program start with

Escaping to PHP

The way to separate a HTML code from PHP code is called Escaping to PHP , There are different ways to do this. Few methods are already set and few other methods like Short-Open and ASP style tags need to change the configuration in php.ini file. HTML.

Four methods / ways or syntax to write php code

PHP code can be embedded any where in HTML , we can write code before , in head section , in body section or after closing html tag. We can write php code in separate php only file. Following are the four way to write php code:
  1. Canonical PHP Tags
  2. JavaScript Style
  3. Short Opening Tags
  4. Asp Style tags

Canonical PHP Style Tags

This is standard syntax of php, and all servers support this syntax by default. Hence it is recommended way of php coding. According to this syntax php code starts with <?php and ends with ?>
Example:
<?php
echo "Welcoe to PHP";
?>

JavaScript Style Tags

This syntax is removed from php 7, According to this syntax php code start with <script language="php"> and ends with </script>
Example:
  <script language="php">
  echo "welcome to php";
  </script>

Short Opening Tags

This syntax is not recommended because , mostly its setting is off in php.ini file, so if you don't have access to php.ini for changing configuration, php short opening tags will not work, so avoid using this syntax
Example:
<?= "Welcome to php" ?>

Asp Style tags

Same like short opening tags , asp style tags are mostly off in php.ini , so not recommended way to write php code
Example:
<%
echo "Welcome to PHP";
%>


Comments in PHP

Single Line Comments

There are two ways of single line comment
Either Begin with(#)
Or backslash(//)
Example:
<?php

# This is the single line comment

# This is the next line comment

// This is also a single line comment.

?>

Multi-line comments

Multi-line comments start with /* and ends with */
 Example:
<?php
 
/* 

This is a comment with multiline 

Developer : sanjeev rai 

view : Multiline Comments Demo 

*/

?>

0 Comments:

Post a Comment

 
TOP