This function returns a string with backslashes added before certain characters.
Syntax: addcslashes(string,characters);
characters – the characters before which a backslash is to be added.
Example inputs:
1.
$str1=" my program";
echo $str1;
echo "
";
echo addcslashes($str1,"r");
echo "
";
echo addcslashes($str1,"r,m");
2.
$str1=" my program";
echo $str1;
echo "
";
echo addcslashes($str1,"a...p");
In this case, a backslash is added before every character that falls between a and p in the alphabetical sequence.
3.
$str1=" 121232342434";
echo $str1;
echo "
";
echo addcslashes($str1,"1...3");
Example outputs:
1.
my program my p\rog\ram \my p\rog\ra\m
2.
my program \my \pr\o\gr\a\m
3.
121232342434 \1\2\1\2\3\2\34\24\34
Showing posts with label examples. Show all posts
Showing posts with label examples. Show all posts
Thursday, September 18, 2008
Saturday, September 13, 2008
trim() function
The trim() function could be used to remove certain pre-defined characters from both ends of a string.
Syntax: trim(string, [list of characters]);
string- the string in which the predefined characters are to be removed.
list of characters.
\0 for NULL
\t for TAB
\n for new line
“ ” for whitespace
If the list of characters are not specified, all pre-defined characters would be removed.
Syntax: trim(string, [list of characters]);
string- the string in which the predefined characters are to be removed.
list of characters.
\0 for NULL
\t for TAB
\n for new line
“ ” for whitespace
If the list of characters are not specified, all pre-defined characters would be removed.
Example inputs
1.
$first_name = “James ”;
echo $first_name.”
”;
trim($first_name);
echo first_name;
In the output, the second echo function prints $first_name that has one less space.
$first_name = “James ”;
echo $first_name.”
”;
trim($first_name);
echo first_name;
In the output, the second echo function prints $first_name that has one less space.
2.
$name = “John
”;
echo $name.”
”;
trim($name);
echo $name;
In this case, the second time $name is printed, the new line character is absent.
Wednesday, September 10, 2008
Data types in PHP
In PHP the data type of a variable is not set by the programmer. PHP decides the data type of variables after interpreting the web page. Data Types in PHP include:
Numeric data type
String data type
Array data type
Numeric data type
String data type
Array data type
Numeric Data types
1. Integer Data Type
Integer data types contain whole number values. Integer values range from -32767 to 32768.
1. Integer Data Type
Integer data types contain whole number values. Integer values range from -32767 to 32768.
2. Doubles
Double contains floating point numbers.
Double contains floating point numbers.
String Data types
A String data type is used to contain textual information or letters. The value is assigned within quotes as shown below.
A String data type is used to contain textual information or letters. The value is assigned within quotes as shown below.
Array Data Type
An array is a variable that contains several variables of the same data type.
Each element in an array can be accessed using an id or index.
Arrays can be classified into:
Numeric arrays
Associative arrays
Multi-dimensional arrays
Numeric arrays
Associative arrays
Multi-dimensional arrays
Numeric arrays
Elements in a numeric array are stored with a numeric index.
We could either assign the index manually or let PHP to assign it.
The IDs could be used in your PHP code to access the elements in the array.
Elements in a numeric array are stored with a numeric index.
We could either assign the index manually or let PHP to assign it.
The IDs could be used in your PHP code to access the elements in the array.
Associative Arrays
In associative arrays, each ID is associated with a value. That is, you can use the values themselves as keys and assign values to them.
There are two ways of creating an associative array as shown in the example.Multi-dimensional Arrays.
In multi-dimensional arrays, an array element can have another array as its value and these values can in turn have arrays as their values.
Multi-dimensional arrays can be two-dimensional or three-dimensional.
While, two-dimensional arrays can be perceived as having a row and a column, three-dimensional arrays have one more dimension. That is, a three dimensional array has many two-dimensional arrays.
In multi-dimensional arrays, an array element can have another array as its value and these values can in turn have arrays as their values.
Multi-dimensional arrays can be two-dimensional or three-dimensional.
While, two-dimensional arrays can be perceived as having a row and a column, three-dimensional arrays have one more dimension. That is, a three dimensional array has many two-dimensional arrays.
EXAMPLES:
1.
Assigning indices manually
$student[1] = ?Maria?;
$student[2] = ?John?;
$student[3] = ?Mathew?;?
Assigning indices manually
$student[1] = ?Maria?;
$student[2] = ?John?;
$student[3] = ?Mathew?;?
2.
Assigning IDs automatically?
$students = {?Maria?,John?,?Mathew?};?
In this case, the indices are automatically assigned by PHP.??
3.
echo $students[0];
echo ?
?;
echo $students[2];????
Let us use an associative array to store the names and salaries of a few employees:?
One way of creating an associative array:
$salary = {?James?=>2000, ?Maira?=>5000, ?Mathew?=>3000};?
You can also create an associative array like this:?
$salary[?James?] = ?2000;
$salary[?Maria?] = ?5000;
$salary[?Mathew?] = ?3000?;
You could access elements of associative arrays as:
echo ?James?s salary:?;
echo ?
?;
echo $salary[?James?];???
Let?s assume that you need to store the names, IDs and ages of a few employees in an array. You can do that by using a two-dimensional array.
$employee = array{array(?John?, 223,42),
array(?Daisy?,443,50),
array(?Mathew?,990,37)
};?
You can easily access the values of the arrays as follows:?
echo ?Name:?;
echo $employee[0][0];
echo ?
?;
echo ?ID:?;
echo $employee[0][1];
echo ?
?;
echo ?Age:?;
echo $employee[0][2];?
A multi-dimensional array can also be an array of associative arrays.
That is, the above array can also be created like this:?
$employee = array{array(Name=>?John?,???????????????????? ID=>223,Age=>42),
array(Name=>?Daisy?,???????????????????? ID=>443,Age=>50),
array(Name=>?Mathew?,???????????????????? ID=>990,Age=>37)
}?
In this case, you may access the
array values like this:?
echo ?Name:?;
echo $employee[2][?Name?];
echo ?
?;
echo ?ID:?;
echo $employee[2][?ID?];
echo ?
?;
echo ?Age:?
echo $employee[2][?Age?];
To understand three-dimensional arrays better, let us assume that we need to store the details of employees in different departments in a single array.
We could do that as follows:?
$shop = array
(
array(
array("John", 223, 42),
array("daisy", 443, 50),
array("Mathew", 990, 37)
??????? ),
array(
array("Smith", 390, 45),
array("Clive", 230, 52),
array("Stephen", 900, 32)
???????? ),
array(array("Chris", 553, 35),
array("Natasha", 725, 45),
array("Jack", 617, 47)
??????? )
);?
Each element of the above three-dimensional array is a two-dimensional array. So you could store all employees under a category in a two-dimensional array ie., in an element of the three-dimensional array.?
The array elements could be accessed as follows:
echo $employee[0][0][0];
echo ?
?;
echo $employee[0][0][1];
echo ?
?;
echo $employee[0][0][2];
echo ?James?s salary:?;
echo ?
?;
echo $salary[?James?];???
Let?s assume that you need to store the names, IDs and ages of a few employees in an array. You can do that by using a two-dimensional array.
$employee = array{array(?John?, 223,42),
array(?Daisy?,443,50),
array(?Mathew?,990,37)
};?
You can easily access the values of the arrays as follows:?
echo ?Name:?;
echo $employee[0][0];
echo ?
?;
echo ?ID:?;
echo $employee[0][1];
echo ?
?;
echo ?Age:?;
echo $employee[0][2];?
A multi-dimensional array can also be an array of associative arrays.
That is, the above array can also be created like this:?
$employee = array{array(Name=>?John?,???????????????????? ID=>223,Age=>42),
array(Name=>?Daisy?,???????????????????? ID=>443,Age=>50),
array(Name=>?Mathew?,???????????????????? ID=>990,Age=>37)
}?
In this case, you may access the
array values like this:?
echo ?Name:?;
echo $employee[2][?Name?];
echo ?
?;
echo ?ID:?;
echo $employee[2][?ID?];
echo ?
?;
echo ?Age:?
echo $employee[2][?Age?];
To understand three-dimensional arrays better, let us assume that we need to store the details of employees in different departments in a single array.
We could do that as follows:?
$shop = array
(
array(
array("John", 223, 42),
array("daisy", 443, 50),
array("Mathew", 990, 37)
??????? ),
array(
array("Smith", 390, 45),
array("Clive", 230, 52),
array("Stephen", 900, 32)
???????? ),
array(array("Chris", 553, 35),
array("Natasha", 725, 45),
array("Jack", 617, 47)
??????? )
);?
Each element of the above three-dimensional array is a two-dimensional array. So you could store all employees under a category in a two-dimensional array ie., in an element of the three-dimensional array.?
The array elements could be accessed as follows:
echo $employee[0][0][0];
echo ?
?;
echo $employee[0][0][1];
echo ?
?;
echo $employee[0][0][2];
Sunday, September 7, 2008
The print Function
The print function is used to print one or more variable in the output.
Syntax: print variablename;
Example inputs:
1.
print “smith”;
2.
$name = “Jones”;
print $name;
3.
$name=”Natasha”;
print “My name is $name”;
print “
”
print “What is yours?”;
Example outputs:
1.
Smith
2.
Jones
3.
My name is Natasha
What is yours?
Syntax: print variablename;
Example inputs:
1.
print “smith”;
2.
$name = “Jones”;
print $name;
3.
$name=”Natasha”;
print “My name is $name”;
print “
”
print “What is yours?”;
Example outputs:
1.
Smith
2.
Jones
3.
My name is Natasha
What is yours?
Labels:
display,
echo,
examples,
output,
php,
print,
programming,
programming with php,
syntax
Subscribe to:
Posts (Atom)