Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Programming Language

Similar presentations


Presentation on theme: "Web Programming Language"— Presentation transcript:

1 Web Programming Language
Chapter 5

2 PHP Functions and Objects
Array Functions Mathematical Functions Date and Time Functions Defining Your Own Functions include and require Objects in PHP Creating a Calendar

3 Functions Functions are a key part of coding;
Less typing Simpler syntax Fewer errors Faster loading & execution Simpler Logic Without functions, code will quickly grow to a state that is difficult to maintain PHP has hundreds of functions built in

4 Function Syntax <?php echo strrev(“.dlroW olleH”); str_repeat(“Hip”, 2); $result = strtoupper(“hooray!”); echo ucfirst(strtolower(“jOhN”)); ?>

5 Array Functions – array()
Used to create an array <?php $arr = array(“ant”, “bird”, “cat”, “dog”); $arr2 = array(“uk”=>”United Kingdom”, “th”=>”Thailand”, “kh”=>”Cambodia”, “za”=>”South Africa”); ?>

6 Array Functions – print_r()
Outputs array in human readable form <?php print_r($arr); echo “<br>”; print_r($arr2); ?>

7 Array Functions – array_count_values()
Counts how many times each value occurs in the array, and returns results in a new array. <?php $arr = array("ant", "bird", "ant", "dog"); print_r(array_count_values($arr)); ?>

8 Array Functions – array_diff() & array_intersect()
Finds differences and similarities between 2 arrays. <?php $arr = array("ant", "bird", "cat", "dog"); $arr2 = array("ant", "bird", "ant", "dog"); print_r(array_diff($arr, $arr2)); ?> <?php $arr = array("ant", "bird", "cat", "dog"); $arr2 = array("ant", "bird", "ant", "dog"); print_r(array_intersect($arr, $arr2)); ?>

9 Array Functions – array_flip()
Flips keys and values of an array. <?php $arr = array("uk"=>"United Kingdom", "th"=>"Thailand", "kh"=>"Cambodia", "za"=>"South Africa"); print_r(array_flip($arr)); ?>

10 Array Functions – in_array() & array_key_exists()
Checks if a value or key is in the array. <?php $arr = array("uk"=>"United Kingdom", "th"=>"Thailand", "kh"=>"Cambodia", "za"=>"South Africa"); if(in_array("Thailand", $arr)) { echo "That exists!"; } ?> <?php $arr = array("uk"=>"United Kingdom", "th"=>"Thailand", "kh"=>"Cambodia", "za"=>"South Africa"); if(array_key_exists("th", $arr)) { echo "That exists!"; } ?>

11 Array Functions – array_reverse()
Can you guess what it does? <?php $arr = array("ant", "bird", "cat", "dog"); print_r(array_reverse($arr)); ?>

12 Array Functions – count()
How many elements in the array – aliased by size_of() <?php $arr = array("ant", "bird", "cat", "dog"); if(is_array($arr)) { $size = count($arr); //$size == } ?>

13 Array Functions – shuffle()
Randomise the order of elements in the array <?php $arr = array("ant", "bird", "cat", "dog"); shuffle($arr); print_r($arr); ?>

14 Array Functions - Sorting
There are several ways to sort an array – by key, by value, ascending, descending… Name Sort By Order Maintain Key=>Value Association sort() Value Low to high No rsort() High to low asort() Yes arsort() ksort() Key krsort()

15 Array Functions - Sorting
Examples of each sorting function. <?php $arr = array("uk"=>"United Kingdom", "th"=>"Thailand", "kh"=>"Cambodia", "za"=>"South Africa"); sort($arr); print_r($arr); ?>

16 Mathematical Functions
There are lots of mathematical functions abs() – returns absolute value of a number ceil(), floor() and round() - ceil rounds a number up, floor rounds a number down, round() rounds to a specified number of decimal places log() and exp() – calculates natural log, and conversely the exponent max() and min() – return highest or lowest value in an array pi() – returns pi to 14 decimal places pow(x,y) – calculates x to the power y sqrt() – returns the square root rand() – returns a random number (no longer needing to seed the random number)

17 Trigonometry? Name Performs cos() Cosine cosh() Hyperbolic Cosine
acos() Arc Cosine acosh() Inverse Hyperbolic Cosine sin() Sine sinh() Hyperbolic Sine asin() Arc Sine asinh() Inverse Hyperbolic Sine tan() Tangent tanh() Hyperbolic Tangent atan() Arc Tangent atanh() Inverse Hyperbolic Tangent

18 Date & Time Functions Remember that time related functions in PHP are running on the server – which may be located somewhere else from the client machine! The time() function returns an integer, for the number of seconds since the Unix Epoch – midnight on January 1st 1970 in UTC timezone. By 2016, there had been 1.4 billion seconds! PHP offers some functions which make managing time more convenient

19 Date & Time Functions The date() function reformats the timestamp in many different ways, for example; <?php echo date("d/m/Y") . "<br>"; //outputs the date for example:- 24/07/ echo date("h:i:sA") . "<br>"; //outputs the time for example:- 01:09:58PM echo date("l jS M, Y") . "<br>"; //outputs a date for example:- Sunday 24th July, ?> A full list of the different modifying parameters is given on the next slide

20 Date and Time Functions
Character Description Example d 2 digit day of the month 01 to 31 D 3 letter text representation of day Sun to Sat j Day of month without leading zero 1 to 31 l Full day name Sunday to Saturday N Number of day of the week 1 (Monday), to 7 (Sunday) S 2 character suffix for day of the month st, th, nd, or rd z Day of the year 0-365 W Week number in the year 0-52 F Name of the month January – December m Number of the month 01-12 M 3 letter representation of the month Jan – Dec n Number of month, without leading zero 1-12 t Number of days in the month 28,29,30,31 L Boolean 1 if a leap year, 0 if not. 1 or 0 Y 4 digit representation of year 2016 or 2001 y 2 digit representation of year 16 or 01 a Lower case am or pm am or pm A Upper case AM or PM AM or PM g Hour, in 12 hour format G Hour, in 24 hour format 0-23 h Hour, in 12 hour format, with leading zero H Hour, in 24 hour format, with leading zero 00-23 i Minutes 00-59 s Seconds T Timezone abbreviation EST, GMT Date and Time Functions

21 Defining Your Own Functions
The general syntax is: function functionname(parameter1, parameter2) { //Statements }

22 Returning a Value A value can be returned from the function:
<?php function average($v1, $v2, $v3) { return ($v1 + $v2 + $v3)/3; } ?>

23 Including a Library of Functions
Over time you may wish to move your functions to a separate file, which can be included in your project: <?php include “functions.php”; ?>

24 include & require As your code grows it is a good idea to separate different parts of the page into different files <?php include “functions.php”; include “header.php”; include “navigation.php”; include “side-bar.php”; // Main Content Here include “footer.php”; ?> Consider with this – if you wanted to make a change to the navigation – you would only need to make a change in one place and it would update every page that includes it.

25 include and require The difference is when a resource can’t be found – include will return a warning, while require will return a fatal error.

26 Objects in PHP PHP also supports OOP
<?php class fraction { public $num, $denom; function display() { echo $this->num . “/” . $this->denom; } } ?>

27 Objects in PHP The arrow operator is used to access members
<?php $f1 = new fraction; $f1->num = 1; $f1->denom = 2; $f1->display(); ?> Constructor functions can be created <?php function __construct() { $this->num = $this->denom = 1; } ?>

28 Calendar Time! This chapter includes a tutorial for creating a simple, basic calendar. Once you’ve built the basic calendar, use CSS to make it look more attractive. Add links to allow the user to move from month to month. This calendar offers a month view, extend it to add a week view and a day view that users can use to get more details on their screen – make sure your calendar is user friendly, so users can understand how to use it without being taught!


Download ppt "Web Programming Language"

Similar presentations


Ads by Google