1 / 17

PHP Functions and Date Functions

PHP Functions and Date Functions. I. PHP Functions. The real power of PHP comes from its functions. In PHP, there are more than 700 built-in functions. A function is just a name we give to a block of code that can be executed whenever we need it.

justus
Télécharger la présentation

PHP Functions and Date Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Functions and Date Functions

  2. I. PHP Functions The real power of PHP comes from its functions. In PHP, there are more than 700 built-in functions. A function is just a name we give to a block of code that can be executed whenever we need it. To keep the script from being executed when the page loads, you can put it into a function. You may call a function from anywhere within a page.

  3. 1. Create PHP Functions function functionName(){code to be executed;} • A function will be executed by a call to the function. • Syntax : • PHP function guidelines: • Give the function a name that reflects what the function does • The function name can start with a letter or underscore (not a number) • Always start function with the keyword function • Remember that function's code must be between the "{" and the "}" • When using function, be sure you spell the function name correctly .

  4. Ex. <html> <body> <?php function Deliver(){ echo "We deliver quantity, not quality!<br />"; } echo “Between quantity and quality,which one u deliver?<br />"; Deliver(); echo "Well, thanks for ur answer by! <br />"; echo "and remember... <br />"; Deliver(); ?> </body> </html>

  5. Result : Between quantity and quality,which one u deliver? We deliver quantity, not quality! Well, thanks for ur answer by! and remember... We deliver quantity, not quality! Naming Your Function The most important point to consider when naming your functions is that the name should be short but descriptive. A few restrictions follow : Your function cannot have the same name as an existing function. Your function name can contain only letters, digits, and underscores. Your function name cannot begin with a number digit. PHP does not support function overloading, so your function cannot have the same name as any built-in function or an existing user-defined function.

  6.  The following function names are legal: name() name2() name_three() _namefour()  These names are illegal: sf5name() name-six() fopen() Note that although $name is not a valid name for a function, a function call like $name( );

  7. 2. Functions - Parameters To add more functionality to a function, we can add parameters. A parameter is just like a variable. Parameters are specified after the function name, inside the parentheses. Ex1 : function have one parameter <?php function myGreeting($firstName){ echo "Hello there ". $firstName . "!<br />"; } myGreeting("Jack"); myGreeting("Ahmed"); myGreeting("Julie"); myGreeting("Charles"); ?>

  8. Result : Hello there Jack! Hello there Ahmed! Hello there Julie! Hello there Charles! Hello there Jack Black! Hello there Ahmed Zewail! Ex2. function have two parameters <?php function myGreeting($firstName, $lastName){ echo "Hello there ". $firstName ." ". $lastName ."!<br />"; } myGreeting("Jack", "Black"); myGreeting("Ahmed", "Zewail"); ?> Result :

  9. 3. PHP Functions - Returning Values $myVar = somefunction(); when the function is used and finishes executing, it sort of changes from being a function name into being a value. To capture this value you can set a variable equal to the function. Something like: The keyword return stops the execution of a function. When a function ends because either all statements have been executed or the keyword return is used, execution return is to the statement after the function call.

  10. Ex. <html> <body> <?php function mySum($numX, $numY){ $total = $numX + $numY; return $total; } $myNumber = 0; echo "Before the function, myNumber = ". $myNumber ."<br />"; $myNumber = mySum(3, 4); // Store the result of mySum in $myNumber echo "After the function, myNumber = " . $myNumber ."<br />"; ?> </body> </html>

  11. Result : Before the function, myNumber = 0 After the function, myNumber = 7 When we first print out the value of $myNumber it is still set to the original value of 0. However, when we set $myNumber equal to the function mySum, $myNumber is set equal to mySum's result. In this case, the result was 3 + 4 = 7, which was successfully stored into $myNumber and displayed in the second echo statement!

  12. II. Date Functions date(format,timestamp); The PHP date() function is used to format a time and/or date. The PHP date() function formats a timestamp to a more readable date and time. A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred. Syntax :

  13. Ex. <html> <body> <h3>Using Data Function</h3> <hr color=”darkblue” size=”1”> <?php echo date("Y/m/d") . "<br />"; echo date("Y.m.d") . "<br />“; echo date("Y-m-d"); ?> </body> </html>

  14. 1. Format the Date • The required format parameter in the date() function specifies how to format the date/time. • Here are some characters that can be used: • d - Represents the day of the month (01 to 31) • m - Represents a month (01 to 12) • Y - Represents a year (in four digits) • Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting.

  15. 2. Adding a Timestamp mktime(hour,minute,second,month,day,year,is_dst) The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used. The mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified. Syntax for mktime() :

  16. Ex. <html> <body> <?php $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); echo "Tomorrow is ".date("Y/m/d", $tomorrow); ?> </body> </html>

  17. THE END

More Related