Tag Archives: Breakthrough Technology

PHP Compound Data Type

PHP consist of two compound types, arrays and objects.

Arrays

Arrays are special types of variables that can store more than one value. Arrays are useful when you need to store related variables data under one variable name.

An array can be declare in fallowing way:

$arr-name [key]= value;

In the above statement
arr_name is the name of the array.
Key is any string or a non negative integer. if you want to modify an array, it is best not to specify a key and to leave the square bracket empty.
Value is the data that is held in the array.

To understand the concept arrays, consider the fallowing codes:

<?php
$arr_name[0]="My";
$arr_name[1]="name";
$arr_name[2]="is";
$arr_name[3]="Rishi";
echo "$arr_name[0] $arr_name[1] $arr_name[2] $arr_name[3] $arr_name[4]";
?>

Output of the above code is: My name is Rishi

Objects

Objects are are the building blocks of a programming language . An object is an entity that can hold data and specify what need to be done with the data. Most of the internal working of an object are hidden from the code that uses them. Objects are a bundle of variable and functions that are derived from a class. To understand the concept of object you should be familiar with the fallowing terminology

Object orientation

The programming concept that revolves around objects.An object is an instance of a class.

Class.

A class is an entity that exibit certain behavior and expose certain attributes to retain state.

Properties

The character of an object defined by the class to which it belongs.

Methods

Task that can be performed by object on the data they contain.

Juggling Data Types

PHP does not require the developer to absolutely specify a data type for a variable at the time ofdeclaration. In PHP you need not worry about data type mismatches in an expression. Consider the fallowing example:

<?php
 $myVal = 7+ "2 is my number";
 echo $myVal;
?>

The output will be: 9.

In most other language ,the preceding code will not be valid. In PHP this statement is valid, and it also generates the expected output. This is because in PHP a variable doesn’t have fixed data type assign to it and can store any data type. The result of an expression is calculated on the basic of the operator and not on the basic of the operands. Examine the fallowing code for a better understanding of juggling data type:

<?php
  $a = "Rishi";
  $a +=2; // The += operator is use to add the initial value assigned to
// the variable to the number specified.r>  echo?>

The output will be : 2

In the above example ,$a contains a value that is of the string data type. In the next line, the += operator changes the initial variable assigned to $a to 2.

Consider another example where the .= operator is used to concatenate string:

<?php
 $a = "Rishi";r>  $a .=2; //The .= operator is use to concatenate two string .
 echo $a; //The value of $a is now Rishi2 
?>

The output will be: Rishi2

The . = operator tells the PHP that the number 2 is a string even though it is not specified in single quotes or double quotes. As a result it concatenates the initial value with the new value of $a, and the output is Rishi2.

Constants

A constant is a name that identifies a simple value. By nature, a constant is the opposite of a variable because the value of a constant remains fixed and doesn’t change when the script is executed. Constant have the fallowing characteristics:
– A constant is case sensitive by default.
– By convention, constant are expressed in uppercase letters.
– A consent can begin with either an underscore or with a letter, fallowed by any number of underscore, letters or numbers.

There are several other differences between variable and constants:

– A constant name never begins with a dollar sign .
– After a constant is set, it is not possible to to redefine or undefined it.
– A constant can contain only scalar data type like : Boolean, integer, float and string.
– A constant can be accessed and use anywhere in a script regardless of variable scoping rules.

Defining constants

A simple way to define a constant by using the define ( ) function. After you define a constant, you can read it’s value anytime you want by simply using it’s name. To read a constant value you can use the constant ( ) function. To obtained a list of all defined coinstants, you can use the get-defined-constants function.
In the fallowing code, a constant ADMIN_MAIL is defined with the value “contatc@a2zwebhelp.com”. Notice that constants are case sensitive, and the value assigned to the constant ADMIN_MAIL doesn’t appear when you try to print the value of the constant admin_mail:

<?php
    define ("ADMIN_MAIL" ,"contatc@a2zwebhelp.com");
  echo MIN_MAIL;
?>

The output will be: contatc@a2zwebhelp.com

PHP Data Handling

Handling data

Every programming language has its own set of data types that help developers to write programs. A data type is a classification of data . This could mean categorizing the the type of data as an amount of money, time or percentage. In PHP the data type can be broadly classified as scalar and compounddata types.

Scalar data type

Four type of data comes under the category of scalar data type: Boolean, integer, float and string.

Boolean

The Boolean data type can either be true or false. The numeric representation of the Boolean data type is 1 for true and 0 for false. The the fallowing example uses this data type:

<?php
$myVal = True; // Boolean variable type.
?>

Integer

An integer data type consist of numbers. You can use both negative or positive numbers like […-2,-1,0,1,2,3,…]. The size of an integer in PHP solely depends on the operating system that you are using. typically , the size of an integer can be up to 2 billion. In the fallowing code, $myVal is aninteger type variable with a value of 9:

<?php
 $myVal =9; //Is an integer.
?>

Float

A float is a data type that is also use for storing numbers. Usually floats are use to express decimal numbers, you can use the integer data type for non decimal numbers. Floats are also known as doubles or real numbers and have a precision of up 14 decimal places. In the fallowing code ,$floatVal is a float type variable and has a value of 9.98765:

<?php
$ floatVal = 9.98765 // Is a float type variable
?>

String

A string can be best defined as a series of characters. In PHP, a character is nothing but a byte. There are 256 different types of characters that can be used in strings. There is no maximum limit specification in relation to strings to PHP; therefore you need not worry about a very long string .There are two ways to specify a string discussed on fallowing sections.

Single quoted

You can use single quotes to specify a simple string. How ever when a string is enclosed in single quotes and it also contains some text enclosed in double quotes, you must use the backslash escape character to escape the single quotes. In other wards when you don’t want to parser to treat single quotes as syntax and display them as part of the output of the code, you need o use a back slash beforethe single quotes.

Lets consider a simple example to understand how single quotes are used to specify a string:

<?php
      echo 'This is my First String'; // will print the string on the screen.
?>

Now consider an example where double quotes are used within single quotes and single quotes is represent in the text enclosed in double quotes:

echo 'Rishi says: "I'm doing well "'; , //will display error.

When you try to execute this statement, an error message will appear on the screen. This is because when you use a single quote within double quotes, you need to escape the single quote with a backslash. In the fallowing statement is written correctly:

echo 'Rishi says: "I\'m doing well "'; , //is Correct now.

The output will be Rishi says: “I’m doing well”.

Another thing that you should remember when using single quoted string is that such string don’t support variable expansion: If you use a name of variable within single quote and try to print the value of a variable, the name of the variable will be printed instead of its value. Consider the fallowing example:

<?php
  $name='Rishi';
  echo 'Hello $name';
?>

The output will be Hello $name.
The correct syntax is

<?php
  $name="Rishi";
  echo "Hello $name";
?>

The output will be Hello Rishi.

Double quoted

A double quoted strings supports several other escape characters as follows
– \n: new line
– \r: carriage
– \t: Horizontal tab
– \\: backslash
– \$: Dollar sign
– \”: Double quote

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 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 Tables

Tables are defined with the <table> tag. A table is divided into rows (<tr>), and each row is divided into cells (<td>). The cell can contain text, images, lists, paragraphs, forms, horizontal rules, tables, etc.

Example:

<table>
<tr>
<td>&nbsp;</td>
</tr>
</table>

Tables and the Attribute:

Border: If you do not specify a border attribute the table will be displayed without any borders. Sometimes this can be useful, but most of the time, you want the borders to show.
To display a table with borders, you will have to use the border attribute:

<table border="1">
 <tr>
   <td>&nbsp;</td>
 </tr>
</table>

Width: You can specify the width of the table either in percentage or pixel.

<table border="1" width="100%" >
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>

Cellspacing/ Cellpadding:

<table border="1" width="100%" 
cellspacing="0" cellpadding="0">
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>