Wednesday, November 21, 2012

PHP in-built functions

PHP Basic Functions
 
Function Description
array() Creates an array
array_push() Inserts one or more elements to the end of an array
shuffle() Shuffles an array
sizeof() Alias of count()
sort() Sorts an array
count() Counts elements in an array, or properties in an object
uasort() Sorts an array with a user-defined function and maintain index association
date() Formats a local time/date
getdate() Returns an array that contains date and time information for a Unix timestamp
mktime() Returns the Unix timestamp for a date
strftime() Formats a local time/date according to locale settings
strptime() Parses a time/date generated with strftime()
time() Returns the current time as a Unix timestamp
chdir() Changes the current directory
dir() Opens a directory handle and returns an object
addcslashes() Returns a string with backslashes in front of the specified characters
addslashes() Returns a string with backslashes in front of predefined characters
echo() Outputs strings
explode() Breaks a string into an array
fprintf() Writes a formatted string to a specified output stream
htmlentities() Converts characters to HTML entities
htmlspecialchars_decode() Converts some predefined HTML entities to characters
htmlspecialchars() Converts some predefined characters to HTML entities
implode() Returns a string from the elements of an array
trim() Strips whitespace from the left side of a string
md5() Calculates the MD5 hash of a string
nl2br() Inserts HTML line breaks in front of each newline in a string
print() Outputs a string
printf() Outputs a formatted string
sprintf() Writes a formatted string to a variable
str_repeat() Repeats a string a specified number of times
str_replace() Replaces some characters in a string (case-sensitive)
str_shuffle() Randomly shuffles all characters in a string
str_split() Splits a string into an array
str_word_count() Count the number of words in a string
strchr() Finds the first occurrence of a string inside another string (alias of strstr())
strcmp() Compares two strings (case-sensitive)
strip_tags() Strips HTML and PHP tags from a string
stripcslashes() Unquotes a string quoted with addcslashes()
stripslashes() Unquotes a string quoted with addslashes()
stripos() Returns the position of the first occurrence of a string inside another string (case-insensitive)
strtolower() Converts a string to lowercase letters
strtoupper() Converts a string to uppercase letters
substr() Returns a part of a string
substr_compare() Compares two strings from a specified start position (binary safe and optionally case-sensitive)
substr_count() Counts the number of times a substring occurs in a string
substr_replace() Replaces a part of a string with another string
ucfirst() Converts the first character of a string to uppercase
ucwords() Converts the first character of each word in a string to uppercase
wordwrap() Wraps a string to a given number of characters
mail() Allows you to send emails directly from a script
 
PHP MySQL Functions
Function Description
mysql_affected_rows() Returns the number of affected rows in the previous MySQL operation
mysql_change_user() Deprecated. Changes the user of the current MySQL connection
mysql_client_encoding() Returns the name of the character set for the current connection
mysql_close() Closes a non-persistent MySQL connection
mysql_connect() Opens a non-persistent MySQL connection
mysql_create_db() Deprecated. Creates a new MySQL database. Use mysql_query() instead
mysql_data_seek() Moves the record pointer
mysql_db_name() Returns a database name from a call to mysql_list_dbs()
mysql_db_query() Deprecated. Sends a MySQL query. Use mysql_select_db() and mysql_query() instead
mysql_drop_db() Deprecated. Deletes a MySQL database. Use mysql_query() instead
mysql_errno() Returns the error number of the last MySQL operation
mysql_error() Returns the error description of the last MySQL operation
mysql_escape_string() Deprecated. Escapes a string for use in a mysql_query. Use mysql_real_escape_string() instead
mysql_fetch_array() Returns a row from a recordset as an associative array and/or a numeric array
mysql_fetch_assoc() Returns a row from a recordset as an associative array
mysql_fetch_field() Returns column info from a recordset as an object
mysql_fetch_lengths() Returns the length of the contents of each field in a result row
mysql_fetch_object() Returns a row from a recordset as an object
mysql_fetch_row() Returns a row from a recordset as a numeric array
mysql_field_flags() Returns the flags associated with a field in a recordset
mysql_field_len() Returns the maximum length of a field in a recordset
mysql_field_name() Returns the name of a field in a recordset
mysql_field_seek() Moves the result pointer to a specified field
mysql_field_table() Returns the name of the table the specified field is in
mysql_field_type() Returns the type of a field in a recordset
mysql_free_result() Free result memory
mysql_get_client_info() Returns MySQL client info
mysql_get_host_info() Returns MySQL host info
mysql_get_proto_info() Returns MySQL protocol info
mysql_get_server_info() Returns MySQL server info
mysql_info() Returns information about the last query
mysql_insert_id() Returns the AUTO_INCREMENT ID generated from the previous INSERT operation
mysql_list_dbs() Lists available databases on a MySQL server
mysql_list_fields() Deprecated. Lists MySQL table fields. Use mysql_query() instead
mysql_list_processes() Lists MySQL processes
mysql_list_tables() Deprecated. Lists tables in a MySQL database. Use mysql_query() instead
mysql_num_fields() Returns the number of fields in a recordset
mysql_num_rows() Returns the number of rows in a recordset
mysql_pconnect() Opens a persistent MySQL connection
mysql_ping() Pings a server connection or reconnects if there is no connection
mysql_query() Executes a query on a MySQL database
mysql_real_escape_string() Escapes a string for use in SQL statements
mysql_result() Returns the value of a field in a recordset
mysql_select_db() Sets the active MySQL database
mysql_stat() Returns the current system status of the MySQL server
mysql_tablename() Deprecated. Returns the table name of field. Use mysql_query() instead
mysql_thread_id() Returns the current thread ID
mysql_unbuffered_query() Executes a query on a MySQL database (without fetching / buffering the result)
 

PHP functions

A function is a block of code that can be executed whenever required. A function will be executed by a call to the function.
You may call a function from anywhere within a page. Give the function a name that reflects what the function does. The function name can start with a letter or underscore but not a number.
Syntax
function functionName()
{
code to be executed
}
Creating a simple function
<?php
function hello(){
echo “This is my first simple function”;
}
//call php function
hello();
?>

PHP function with parameters
<?php
function myHello($firstname)
{
echo “Namaste “. $firstName . “!<br />”;
}
//call function
myHello(“Ram”);
?>
Display:
Namaste Ram !
Another example
<?php
function addNumber($num1, $num2)
{
$num3= $num1+$num2;
echo “The sum is”.$num3;
}
//function call
addNumber(15,25);
?>

Conditional Statements

There are three types of conditional statements. They are as follows:
  1. if statement
  2. if else statement
  3. switch statement

1) if statement :
This statement is used to execute some code only if a specified condition is true.
Syntax:
if(condition)
{
write statement here
}

 2) if….else statement:
If a condition is true, one code is executed and another code if the condition is false.
Syntax:
if(condition)
{
write statement here
}
else if(condition)
{
write statement here
}
else
{
write statement here
}

3) switch statement:
This statement is used to select one of many blocks of code to be executed.
Syntax:
switch(variable)
{
case 1:
write statement to be executed
break;
case 2:
write statement to be executed
break;
case 3:
write statement to be executed
break;
default:
write statement to be executed
}

Examples
 1) if statement
<?php
$day=”Fri”;
if($day==”Fri”)
{
echo  “Have a nice weekend!”;
}
?>

 2) if….else statement
<?php
$day=”Fri”;
if($day==”Fri”)
{
echo  “Have a nice weekend”;
}
else
{
echo “Have a nice day”;
}
?>

 3) switch statement
<?php
$x=0;
switch($x)
{
case 1:
echo “Number 1”;
break;
case 2:
echo  “Number 2”;
break;
case 3:
echo “Number 3”;
break;
default:
echo “No number between 1 to 3”;
}
?>

PHP Loops

There are four types of loops in PHP. They are as follows:
1) while loop
2) do…while loop
3) for loop
4) foreach loop

 1) while loop:
Loops through a block of codes while a specified condition is true.
Example:
<?php
$i=1;
while($i<=5)
{
echo  “The number is ”.$i.”<br/>”;
$i++;
}
?>

2) do…while loop:
Loops through a block of code once and then repeats the loop as long as a specified condition is true.
Example:
<?php
$i=1;
do{
$i++;
echo “The number is ”.$i.”<br/>”;
} while($i<=5);
?>

3)for loop:
Loops through a block of code a specified number of items.
Example:
<?php
for($i=1;$i<=5;$i++)
{
echo “The number is ”.$i.”<br/>”;
}
?>

4) foreach loop:
Loops through a block of code for each element in an array.
Example:
<?php
$x=array(“one”,”two”,”three”);
foreach($x as $value)
{
echo $x.”<br/>”;
}
?>

PHP Operators

S.no Operator Operation
1 Addition (+) 4+5=9
2 Subtraction (-) 10-2=8
3 Multiplication (*) 2*4=8
4 Division (/) 15/5=3
5 Modulus (%) 5%2=1, 10%8=2, 10%2=0
6 Increment (++) x=5 x++ is equivalent to x=x+1 x=6
7 Decrement (--) x=5 x—is equivalent to x=x-1 x=4

Variables in PHP

Variables are used for storing a values, like text strings, numbers or arrays. When a variable is declared, it can be used over and over again in your script. All variables in PHP start with a ‘$’sign symbol.
$var_name=value;

For example:

<?php
$txt=”Hello world”;
echo $txt;
?>

Concatenation Operator

The concatenation operator(.) is used to put two string values together.

For example:

<?php
$txt1=”Hello World!”;
$txt2=”Have a nice day!”;
echo $txt1.” “.$txt2;
?>

Comments in PHP

Comments are given so that it shouldn’t get displayed in the front-end but the programmer should always knew the idea behind the coding.

Single line comment
<?php
// This is a comment
?>

Multi-line comments
<?php
/* This is a comment block.
This is a comment block.
This is a comment block.*/
?>

CSS Vertical Menu

Css Vertical Menu can be build using following code :
Vertical Menu #1
Code
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Css Simple Menu</title>
<style type=”text/css”>
#vmenu{
margin:0px;
padding:0px;
}

CSS Multilevel menu (Horizontal Multilevel Menu)

To create a multilevel menu in CSS, one should use the nesting of unordered list.
The below given is the simple multi- level menu.

Css Simple Menu

Simple one level menu can be created using unordered list with the help of CSS properties.
Below given is a very simple examples:

CSS List

CSS allows you to customize the lists that can be made
with HTML  to the extent that even images can be used as bullet points for unordered lists.
CSS List Style Type
If you want to use something different from the default numbering of ordered lists, or the bullets/discs of unordered lists, then all you have to do is choose a different style for your lists. CSS allows you to select from a wide variety of different list item shapes.
Unordered list styles: square, circle, disc (default), and none
Ordered list styles: upper-alpha, lower-alpha, upper-roman, lower-roman, decimal (default), and none
Code:

CSS Margins and Padding

Margin and padding are the two most commonly used properties for spacing-out elements. A margin is the space outside of the element, whereas padding is the space inside the element.
The four sides of an element can also be set individually. margin-top, margin-right, margin-bottom, margin-left, padding-top, padding-right, padding-bottom and padding-left are the self-explanatory properties you can use.

Css Layers(z-index)

It refers to apply the z-index property to elements that overlap with each other. The z-index property when used in conjuction with the position property, enable you to specify which element should appear on top in the event of an overlap.

Overflow property

To control the overflow of the fixed box.There are four types of overflow property and they are: visible(default), hidden, scroll and auto.

visible overflow:
If you don’t set the overflow property at all the default is visible.
For eg:
.box{
overflow:visible;
height:20px;
}

Css Positions

You can put any HTML element at wherever position in the page or absolute based on its parent element.

Relative Positioning:
It changes the position of the HTML element relative to where it normally appears
For example:
<div style=”position:relative;left:80px; top:2px; background-color:yellow;”>
This div has relative positioning
</div>

Css cursors

This allows the type of cursor that should be displayed to the user.

Css Dimensions

The following are the Css dimensions used.

height: sets the height of an element
max-height: sets the maximum height of an element
max-width: sets the maximum width of an element
min-height: sets the minimum height
min-width: set the minimum width
width: sets width
line-height: set the height of a line of text

Css Outlines

An outline is a line that is drawn around elements, outside the border edge, to make the element “standout”;
The outline properties specifies the style, color and width of an outline.
outline
outline-color
outline-style
outline-width

CSS Border

Css allows you to set styles for the border of any HTML element. It also provides you with a way of setting border styles for one or more sides of an element.
Setting Borders on all sides.
border-width
border-style
border-color