PHP for Beginners: Basics, Syntax and Variables

Tags for this post: , , ,

PHP is powerful computer language for making dynamic and interactive Web pages. It is a server-side scripting language. This means that PHP code is executed on web server, and the HTML result is returned to the browser. PHP files should have a file extension of “.php”, “.phpml”, “.php5″, or “.phps” and can contain text, HTML tags and scripts. If the file has a .html extension, the PHP code may not be executed.

Basics, Syntax and Variables

A PHP scripting block always starts with <?php and ends with ?>. This scripting block can be placed anywhere in the document. A PHP file normally contains HTML tags along with some PHP scripting code. For example:

Each line of code in PHP must end with a semicolon which is a separator and is used to distinguish one set of instructions from another. In PHP, there are two basic statements to output text: echo and print.

In PHP, // is used for making a single-line comment, while /* and */ is used for block comment. The block comment can be single-line or multi-line:

Variables are used for storing values, like text strings, numbers or arrays. All variables in PHP start with a dollar sign ($) symbol and are declared as follows:

<?php $variable_name = value; ?>

Once a PHP variable is declared, it can be used over and over again in the script. Variables are case-sensitive, meaning that variable $name is not the same as variable $Name or $NAME. A variable does not need to be declared before setting it to a value. PHP sets the data type, depending upon the value. There are some rules when naming variables:

Rule 1: A variable name must start with a letter or underscore (“_”);
Rule 2: A variable name must only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and “_”).

The following example shows the declaration of a string and number variable:

<?php
          $text_string = "string";
          $count = 10;
?>

String variables are used for variables that contain characters. A string can be used directly in function or it can be stored in a variable. The following example shows the declaration of a string variable:

<?php
          $msg = "Welcome to the Addictive Designs";
          echo $msg;
?>

This produces the following result:

Welcome to the Addictive Desings

Source from PHPpro Quick Guide

7 Responses

  1. John said on

    Much appreciated for the information and share!

    • ljc kredyt said on

      Hii all. I agree with you – 100% +. Thank you for the information. John.

  2. iPage Reviews said on

    Sorry for my bad english. Thank you so much for your good post. Your post helped me in my college assignment, If you can provide me more details please email me.
    My site is about Cheap Hosting.

  3. Love what you are doing with the blog man!

  4. Nancy said on

    Thanks for the share!
    Nancy.R

  5. Very well written article. It will be valuable to anybody who usess it, as well as yours truly :) . Keep up the good work – for sure i will check out more posts.

  6. I am really enjoying the theme/design of your weblog. Do you ever run into any web browser compatibility problems? A few of my blog readers have complained about my blog not operating correctly in Explorer but looks great in Chrome. Do you have any ideas to help fix this issue?