You are on page 1of 20

PHP Day 3

http://www.php.net

Geshan Manandhar
Developer,
Young Innovations Pvt. Limited
www.geshanmanandhar.com
GeshanManandhar.com 1
PHP Strings
►A string is series of characters.
► In PHP, a character is the same as a byte, which is
exactly 256 different characters possible.
<?php
$s=“I am a string”;
$s2=‘I am also a string”;
print $s.”---”.$s2;
?>

GeshanManandhar.com 2
PHP Strings
► Another Example
<?php

$beer = 'Heineken';
echo "<br>$beer's taste is great.";
// works, "'" is an invalid character for varnames
echo "<br>He drank some $beers.";
// won't work, 's' is a valid character for varnames

echo "<br>He drank some ${beer}s."; // works


echo "<br>He drank some {$beer}s."; // works

?>
GeshanManandhar.com 3
Important String Functions
► explode (string $delimiter, string $string)
► nl2br ( string $string )
► strcmp ( string $str1, string $str2 )
► strlen ( string $string )
► strtolower ( string $str )
► substr ( string $string, int $start [, int $length] )
► trim ( string $str )
► Example code at
day03\prog22_string_functions.php
GeshanManandhar.com 4
Array
► In computer science an array is a data
structure consisting of a group of elements
that are accessed by indexing.
► In most programming languages each
element has the same data type and the
array occupies a contiguous area of storage.
► Most programming languages have a built-
in array data type.

GeshanManandhar.com 5
Array
?php
$fruits_1 = array ("Apple", "Mango", "Banana");
$fruits_2 = array (
0 => "Appple",
1 => "Mango",
2 => "Banana"
);

$fruits_3[] = "Apple";
$fruits_3[] = "Mango";
$fruits_3[] = "Banana";
?> //Program code: day03\prog23_array_more.php
GeshanManandhar.com 6
Multi-Dimensional Array
<?php
$shop = array( array( Title => "rose",
Price => 1.25,
Number => 15
),
array( Title => "daisy",
Price => 0.75,
Number => 25,
),
array( Title => "orchid",
Price => 1.15,
Number => 7
)
);
?> //full code at day03\prog24_array_multi.php
GeshanManandhar.com 7
Important Array Function: asort
► asort($array) – Sort an array and maintain index
association
<?php
$fruits = array ("d" => "lemon", "a" => "orange",
"b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
GeshanManandhar.com 8
Important Array Function: push/pop
► array_push($array, ► array_pop($array)
element1,…) <?php
<?php $stack = array("orange", 
$stack = array("orange", "banana", "apple", "raspb
"banana"); erry");
array_push($stack, $fruit = array_pop($stack
"apple", "raspberry"); );
print_r($stack);
print_r($stack); ?>
?>
GeshanManandhar.com 9
Important Array Function: search
► array_search($array, “item1”, “item2”…);
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green',
 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>

GeshanManandhar.com 10
Important Array Function : rand
► array_random ($array, no_of_entries);
<?php
$sentence = "Pick one or more random entries out
of an array";
$input = explode(" ",$sentence);
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo "<br>";
echo $input[$rand_keys[1]] . "\n";
?>

GeshanManandhar.com 11
Important Array Function : reverse
► array_reverse($array)
<?php
$input = array("php", 4.0, "green", "red");
$result = array_reverse($input);
$result_keyed = array_reverse($input, true);
?>

GeshanManandhar.com 12
Important Array Function : merge
► array_merge($array1, $array2…);
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green",
"shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
GeshanManandhar.com 13
Important Array Function: keys
► array_keys($array, param)
<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

$array = array("blue", "red", "green", "blue",


"blue");
print_r(array_keys($array, "blue"));
?>

GeshanManandhar.com 14
Date in php
► Code Description
► d Day of the month with leading zeros
► D Day of the week as a three-letter abbreviation
► F Name of the month
► h Hour from 01to 12
► H Hour from 00 to 23
► g Hour from 1 to 12(no leading zeroes)
► G Hour from 0 to 23(no leading zeroes)
► i Minutes
► j Day of the month with no leading zeroes
► l Day of the week
► m Month number from 01 to 12
► M Abbreviated month name (Jan, Feb…)
► n Month number from 1 to 23(no leading zeroes)
► s Seconds 00 to 59
► S Ordinal suffix for day of the month (1st, 2nd, 3rd)
► y Year as two digits
► Y Year as four digits
► z Day of the year from 0 to 365

GeshanManandhar.com 15
Some Date examples
► <?php
// Assuming today is: March 10th, 2008, 5:16:18 pm
$today = date("F j, Y, g:i a"); // March 10, 2008, 5:16 pm
$today = date("m.d.y"); // 03.10.08
$today = date("j, n, Y"); // 10, 3, 2008
$today = date("Ymd");  // 20080310
$today = date('h-i-s, j-m-y, it is w Day z '); 
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // It is the 10th day.
$today = date("D M j G:i:s T Y");
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');
$today = date("H:i:s"); // 17:16:17
?>

GeshanManandhar.com 16
Important Debugging Functions
► print_r
 Prints human-readable information about a
variable
► var_dump
 Dumps information about a variable
► die() or exit()
 Dumps information about a variable

GeshanManandhar.com 17
The more you dig in the more you
find out
► Findout other built in functions in PHP
yourself.
► Search at www.php.net for more.

GeshanManandhar.com 18
Questions
► Put forward your queries.

GeshanManandhar.com 19
Lets get rolling
► Print today’s date like Today is: December
18, 2008 and time is 12:10:10 PM.
► Reverse this sentence $str= “PHP learn to
want I “ into a meaningful sentence, with
use of array_reverse.
► “Learning PHP is not that easy and not that
difficult” – print it in lower and upper case
with its string length.
GeshanManandhar.com 20

You might also like