All posts by manisha

PHP Variables

Introducing Variables

What is Variable?

Variables are known to hold data that can change during runtime of a program. The following are few important factor about PHP variables
– A PHP variable should declared using a dollar sign followed by variable name.
– A variable name can start with an underscore or with a letter.

– PHP variables are case sensitive .So, $Web  is not the same as $web.

Naming convention

Similar to other programming language such as C and C++, PHP also fallows a naming convention that should be used while declaring variables. Consider the fallowing example

$myVal = "My First Variable";

The above examples shows the naming convention used in PHP to declare a variable:
– The dollar sign: In PHP, each variable that you declared should begin with a dollar sign.
– Variable name: It is always advisable to choose meaningful variable names. Which will helpdevelopers to understand why the variable was declared and where it can be used. For example, if you use the variable name $u for User name and $e for User email address then  you may misinterpret it at a latter stage. So, when you start using PHP or any other programming languages  you should use meaningful variable names.
– Operator : The equal sign is an operator that assigns a value to the variables.
– Data type (value assigned to a variable):  The string “My First Variable” is the value that assign to the variable $myVal.

Variables are assigned using values. These value can be of Boolean, integer, float, or string type. Oncedeclared, variables can be referenced in a script.

Introduction to PHP

What is PHP?

PHP stands for Hypertext Preprocessor. PHP is one of the first growing, free and platform independent server side scripting language. PHP is very easy to understand and user friendly programming language. The first version of PHP was released in 1994 by Rasmus Lerdof.

Sample PHP code

<?php
    echo "This is my first line.";
?>

Lets examine this code line by line:

1. The first line is consist of opening of tag, <?php. And it is mandatory.

2. The second line has echo statement, which is used to display text in browser when we run the php file. All string in PHP should pass between single quote or double quote. The second line ends with a semicolon. You must use semicolon after each statement ends. The semicolon is known as the terminator in PHP. Otherwise it will display error when you run the code.

3. The third line consist of the closing tag, ?>.The closing tag marks the end of the PHP code and it ismandatory.

How to Embed PHP in HTML

Lets discuss how to embed PHP codes in HTML page.

<!DOCTYPE html>
<html lang="en">
<head>
<title>My First PHP Page</title>
<meta charset="utf-8">
</head>
<body>
<?php
   echo "This is my First PHP Page.";
?>
</body>
</html>

PHP parse error

A parse error is come across when the code has syntax errors in it .For example, each line of PHP code should end with a semicolon. If you don’t specify a semicolon, you will get parse error, and the code will not be executed. Similarly, you will get a parse error when you forgot to uses double quotes for strings. Apart for this, there are several others options that might display parse error. The PHP parser tells the exert line on which it encounters a parse error. So you can easily find the mistakes and rectify it.

The following code will display error, because it is missing the semicolon termination.

<?php
    echo "This is my first line."
?>

Escaping PHP Code

Escaping code is the process of overlooking instances of special characters in the code. The following code will display error. To avoid the error you can use an escape character (\) before double quote.

Wrong Code

<?php
  echo "Do you think, "You like php?"";
?>

Correct Code

<?php
  echo "Do you think, \"You like php?\"";
?>

Comment in PHP code

Comments are an primary part of any program. They are statement that are not executed along with the code. They help developers remember and recall why they have used a particular code block in a script.
There are four ways of specifying comments in PHP code:

<!-- You can put comment here -->

This is an HTML comment. Comments stated like this will be ignored by the browserand will not be interpreted.

//Put comment here.

This kinds of comments is commonly used within PHP codes.

#Put comment here.

The hash character can also be used to specify comments within PHP code.

/* 
Put comment here 
Here you can comment multiple lines    
*/

HTML Special Characters

The table display the list of HTML Special Characters with Number and Name. Though in different character set the special characters looks like box or different shapes it is a best practices to use number or name in place of special characters.

Character Entity Number Entity Name
&#34; &quot;
&#39; &apos;
& &#38; &amp;
< &#60; &lt;
> &#62; &gt;
¡ &#161; &iexcl;
© &#169; &copy;
® &#174; &reg;
&trade; &#153;
« &#171; &laquo;
» &#187; &raquo;
° &#176; &deg;
± &#177; &plusmn;
&#182; &para;
¼ &#188; &frac14;
½ &#189; &frac12;
¾ &#190; &frac34;
× &#215; &times;
÷ &#247; &divide;
¢ &#162; &cent;
£ &#163; &pound;
¤ &#164; &curren;
¥ &#165; &yen;

HTML Colors

You can define HTML colors as string, number or hexadecimal value. But it is always recommended to define the value as hexadecimal. All browser supports hexadecimal color codes.

RGB Colors:

RGB stands for Red, Green and Blue. The HTML format for a RGB value is rgb (Value of RED, Value of GREEN, Value of BLUE). The value range are from 0 to 255. rgb(0,0,0) stand for Black color and rgb(255,255,255) stands for White color.

<body bgcolor="rgb(255,0,0)"> - Returns Red color
<body bgcolor="rgb(0,255,0)"> - Returns Green color
<body bgcolor="rgb(0,0,255)"> - Returns Blue color
<body bgcolor="rgb(255,255,255)"> - Returns White color
<body bgcolor="rgb(0,0,0)"> - Returns Black color

Hexadecimal Colors:

A hexadecimal color code starts with a hash(#) and followed by 6 alpha numeric value. Like #000000 stands for Black color and #FFFFFF stands for White color. The First 2 letter stands for Red, next 2 forGreen and last 2 letter for Blue color. The range is from Zero (0) to F. The Hexadecimal color values are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F total 16 digits.

The Hexadecimal colors created with a simple formula.

(First digit x 16) + Second digit = RGB Color value

If you write #000000 then as per the formula (0x16 + 0) = 0 so it Will be rgb(0,0,0) and it is black in color.

If you write #FFFFFF then as per the formula ((15 x16) +15) = 255 so the result will be rgb (255,255,255) and it is white in color.

So, with the help of the above formula you can convert any hexadecimal color code to RGB.

<body bgcolor="#FF0000"> - Returns Red color
<body bgcolor="#00FF00"> - Returns Green color
<body bgcolor="#0000FF"> - Returns Blue color
<body bgcolor="#FFFFFF)"> - Returns White color
<body bgcolor="#000000"> - Returns Black color

Convert RBG Color to Hexadecimal Color:

RED GREEN BBLUE Result
#
<bgcolor=”#FFFFFF”>
<bgcolor=”rgb(255,255,255)”>

Convert Hexadecimal Color to RGB Color :

RED GREEN BBLUE
#
<bgcolor=”#FFFFFF”>
<bgcolor=”rgb(255,255,255)”>

HTML Meta Tags

HTML Meta Tages can be used to specify additional metadata about a document, such as author,publication date, expiration date, page description, keywords, or other information not provided through the other header elements and attributes. Because of their generic nature, meta elements specify associative key-value pairs.

Meta elements can specify HTTP headers which should be sent before the actual content when the HTML page is served from Web server to client

Example

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />

Adding Meta Tags

You can add metadata to your web pages by placing <meta> tags between the <head> and </head>tags. The can include the following attributes:

Attribute Description
Name Name for the property. Can be anything. Examples include, keywords, description, author, revised, generator etc.
content Specifies the property’s value.
scheme Specifies a scheme to use to interpret the property’s value (as declared in the content attribute).
http-equiv Used for http response message headers. For example http-equiv can be used to refreshthe page or to set a cookie. Values include content-type, expires, refresh and set-cookie.

More Examples

This meta element defines a description of your page:

<meta name="description" content="Web Tutorials on HTML, CSS">

This meta element defines keywords for your page:

<meta name="keywords" content="HTML,DHTML,CSS,XML,XHTML,JavaScript" />

The date and time after which the document should be considered expired:
Web robots may delete expired documents from a search engine, or schedule a revisit.
Dates must be given in RFC850 format, in GMT. E.g. (META tag):

<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT" />

The HTTP content type may be extended to give the character set.

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-2022-JP" />

Specifies the default scripting language in a document:

<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript" />

Specifies the default style sheet language for a document:

<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css" />

Declare the natural language of the document:

<META HTTP-EQUIV="Content-Language" CONTENT="en-GB" />

Specifies a delay in seconds before the browser automatically reloads the document:

<META HTTP-EQUIV="Refresh" CONTENT="3;URL=http://www.some.org/some.html"/>

Refresh the page every 5 seconds:

<meta http-equiv="refresh" content="5" />

Specifies the named window of the current page; can be used to stop a page appearing in a frame with many (not all) browsers:

<META HTTP-EQUIV="Window-target" CONTENT="_top" />

Controls Web robots on a per-page basis:

<META NAME="ROBOTS" CONTENT="NOINDEX, FOLLOW" />