PHP $_REQUEST - PowerPoint PPT Presentation

About This Presentation
Title:

PHP $_REQUEST

Description:

Details Notes of PHP Basic to Advanced PHP 5 PHP 5 DATE /TIME FUNCTION, How to Create a user defined function in php PSK Technolgies – PowerPoint PPT presentation

Number of Views:104

less

Transcript and Presenter's Notes

Title: PHP $_REQUEST


1
O
PHP Notes Basic To Advanced
PSK TECHNOLOGIES PVT.LTD IT COMPANY ADDRESS
PLOT NO 780 ,NEEAR DURGA MATA TEMPLE KATOL ROAD
CHAONI NAGPUR-13
http//www.pskitservices.com/ Contact
9975288300
2
PHP 5 DATE /TIME FUNCTION
Function Description
checkdate() Validates a Gregorian date
date_add() Adds days, months, years, hours, minutes, and seconds to a date
date_create_from_format() Returns a new DateTime object formatted according to a specified format
date_create_from_format() Returns a new DateTime object formatted according to a specified format
date_date_set() Sets a new date
date_default_timezone_get() Returns the default timezone used by all date/time functions
date_default_timezone_set() Sets the default timezone used by all date/time functions
date_diff() Returns the difference between two dates
https//www.pskitservices.com contact no. -
9975288300
3
date_format() Returns a date formatted according to a specified format
date_get_last_errors() Returns the warnings/errors found in a date string
date_interval_create_from_date_string() Sets up a DateInterval from the relative parts of the string
date_interval_format() Formats the interval
date_isodate_set() Sets the ISO date
date_modify() Modifies the timestamp
date_offset_get() Returns the timezone offset
date_parse() Returns an associative array with detailed info about a specified date
date_parse_from_format() Returns an associative array with detailed info about a specified date, according to a specified format
date_sub() Subtracts days, months, years, hours, minutes, and seconds from a date
https//www.pskitservices.com Contact no. -
9975288300
4
date_sun_info() Returns an array containing info about sunset/sunrise and twilight begin/end, for a specified day and location
date_sunrise() Returns the sunrise time for a specified day and location
date_sunset() Returns the sunset time for a specified day and location
date_time_set() Sets the time
date_timestamp_get() Returns the Unix timestamp
date_timestamp_set() Sets the date and time based on a Unix timestamp
date_timezone_get() Returns the time zone of the given DateTime object
date_timezone_set() Sets the time zone for the DateTime object
date() Formats a local date and time
getdate() Returns date/time information of a timestamp or the current local date/time
https//www.pskitservices.com Contact no. -
9975288300
5
gmdate() Formats a GMT/UTC date and time
gmmktime() Returns the Unix timestamp for a GMT date
gmstrftime() Formats a GMT/UTC date and time according to locale settings
idate() Formats a local time/date as integer
localtime() Returns the local time
microtime() Returns the current Unix timestamp with microseconds
mktime() Returns the Unix timestamp for a date
strftime() Formats a local time and/or date according to locale settings
strptime() Parses a time/date generated with strftime()
strtotime() Parses an English textual datetime into a Unix timestamp
https//www.pskitservices.com Contact no. -
9975288300
6
timezone_abbreviations_list() Returns an associative array containing dst, offset, and the timezone name
timezone_identifiers_list() Returns an indexed array with all timezone identifiers
timezone_location_get() Returns location information for a specified timezone
timezone_name_from_ abbr() Returns the timezone name from abbreviation
timezone_name_get() Returns the name of the timezone
timezone_offset_get() Returns the timezone offset from GMT
timezone_open() Creates new DateTimeZone object
timezone_transitions_get() Returns all transitions for the timezone
timezone_version_get() Returns the version of the timezone db
https//www.pskitservices.com Contact no. -
9975288300
7
The real power of PHP comes from its functions
it has more than 1000 built-in functions. PHP
User Defined Functions Besides the built-in PHP
functions, we can create our own functions. A
function is a block of statements that can be
used repeatedly in a program. A function will not
execute immediately when a page loads. A function
will be executed by a call to the function.
https//www.pskitservices.com Contact no. -
9975288300
8
Create a user defined function in php
Constant Description
DATE_ATOM Atom (example 2005-08-15T1613030000)
DATE_COOKIE HTTP Cookies (example Sun, 14 Aug 2005 161303 UTC)
DATE_ISO8601 ISO-8601 (example 2005-08-14T1613030000)
DATE_RFC822 RFC 822 (example Sun, 14 Aug 2005 161303 UTC)
DATE_RFC850 RFC 850 (example Sunday, 14-Aug-05 161303 UTC)
https//www.pskitservices.com Contact no. -
9975288300
9
PHP Global Variables - Superglobals Several
predefined variables in PHP are "superglobals",
which means that they are always accessible,
regardless of scope - and you can access them
from any function, class or file without having
to do anything special. The PHP super global
variables are GLOBALS _SERVER _REQUEST
_POST _GET _FILES _ENV _COOKIE _S
ESSION This chapter will explain some of the
superglobals, and the rest will be explained in
later chapters.
https//www.pskitservices.com Contact no. -
9975288300
10
PHP GLOBALS GLOBALS is a PHP super global
variable which is used to access global variables
from anywhere in the PHP script (also from within
functions or methods). PHP stores all global
variables in an array called GLOBALSindex. The
index holds the name of the variable. The example
below shows how to use the super global variable
GLOBALS Example lt?php x 75 y
25 function addition() GLOBALS'z'
GLOBALS'x' GLOBALS'y' addition()
echo z ?gt Run example In the example above,
since z is a variable present within the GLOBALS
array, it is also accessible from outside the
function! ________________________________________

https//www.pskitservices.com Contact no. -
9975288300
11
PHP _SERVER _SERVER is a PHP super global
variable which holds information about headers,
paths, and script locations. The example below
shows how to use some of the elements in
_SERVER Example lt?php echo _SERVER'PHP_SELF'
echo "ltbrgt"echo _SERVER'SERVER_NAME'echo 
"ltbrgt"echo _SERVER'HTTP_HOST'echo "ltbrgt"e
cho _SERVER'HTTP_REFERER'echo "ltbrgt"echo _
SERVER'HTTP_USER_AGENT'echo "ltbrgt"echo _SER
VER'SCRIPT_NAME'?gt
https//www.pskitservices.com Contact no. -
9975288300
12
PHP _REQUEST PHP _REQUEST is used to collect
data after submitting an HTML form. The example
below shows a form with an input field and a
submit button. When a user submits the data by
clicking on "Submit", the form data is sent to
the file specified in the action attribute of the
ltformgt tag. In this example, we point to this
file itself for processing form data. If you wish
to use another PHP file to process form data,
replace that with the filename of your choice.
Then, we can use the super global variable
_REQUEST to collect the value of the input
field
https//www.pskitservices.com Contact no. -
9975288300
13
PHP _GET PHP _GET can also be used to collect
form data after submitting an HTML form with
method"get". _GET can also collect data sent in
the URL. Assume we have an HTML page that
contains a hyperlink with parameters lthtmlgt ltbodygt
lta href"test_get.php?subjectPHPwebW3schools.
com"gtTest GETlt/agt lt/bodygt lt/htmlgt When a user
clicks on the link "Test GET", the parameters
"subject" and "web" are sent to "test_get.php",
and you can then access their values in
"test_get.php" with _GET. The example below
shows the code in "test_get.php" echo "Study " .
_GET'subject' . " at " . _GET'web' ?gt lt/b
odygt lt/htmlgt
https//www.pskitservices.com Contact no. -
9975288300
14
Exceptions are used to change the normal flow of
a script if a specified error occurs. What is an
Exception? With PHP 5 came a new object oriented
way of dealing with errors. Exception handling is
used to change the normal flow of the code
execution if a specified error (exceptional)
condition occurs. This condition is called an
exception. This is what normally happens when an
exception is triggered The current code state
is savedThe code execution will switch to a
predefined (custom) exception handler
function Depending on the situation, the handler
may then resume the execution from the saved code
state, terminate the script execution or continue
the script from a different location in the
code We will show different error handling
methods Basic use of Exceptions Creating a
custom exception handler Multiple
exceptions Re-throwing an exception Setting a top
level exception handler Note Exceptions should
only be used with error conditions, and should
not be used to jump to another place in the code
at a specified point.
https//www.pskitservices.com Contact no. -
9975288300
15
Basic Use of Exceptions When an exception is
thrown, the code following it will not be
executed, and PHP will try to find the matching
"catch" block. If an exception is not caught, a
fatal error will be issued with an "Uncaught
Exception" message. Lets try to throw an
exception without catching it lt?php//create
function with an exceptionfunction
checkNum(number)   if(numbergt1)     throw
new Exception("Value must be 1 or
below")    return true//trigger
exceptioncheckNum(2)?gt
https//www.pskitservices.com Contact no. -
9975288300
16
The code above will get an error like this Fatal
error Uncaught exception 'Exception'with
message 'Value must be 1 or below' in
C\webfolder\test.php6Stack trace 0
C\webfolder\test.php(12)checkNum(28) 1 main
thrown in C\webfolder\test.php on line 6 Try,
throw and catch To avoid the error from the
example above, we need to create the proper code
to handle an exception. Proper exception code
should include Try - A function using an
exception should be in a "try" block. If the
exception does not trigger, the code will
continue as normal. However if the exception
triggers, an exception is "thrown" Throw - This
is how you trigger an exception. Each "throw"
must have at least one "catch" Catch - A "catch"
block retrieves an exception and creates an
object containing the exception information
https//www.pskitservices.com Contact no. -
9975288300
17
Lets try to trigger an exception with valid
code lt?php //create function with an
exception function checkNum(number)
if(numbergt1) throw new Exception("Value
must be 1 or below") return true
//trigger exception in a "try" block try
checkNum(2) //If the exception is thrown, this
text will not be shown echo 'If you see this,
the number is 1 or below' //catch
exception catch(Exception e) echo 'Message
' .e-gtgetMessage() ?gt The code above will get
an error like this Message Value must be 1 or
below
18
Creating a Custom Exception Class To create a
custom exception handler you must create a
special class with functions that can be called
when an exception occurs in PHP. The class must
be an extension of the exception class. The
custom exception class inherits the properties
from PHP's exception class and you can add custom
functions to it. Lets create an exception
class lt?php class customException extends
Exception public function errorMessage()
//error message errorMsg 'Error on line
'.this-gtgetLine().' in '.this-gtgetFile()
.' ltbgt'.this-gtgetMessage().'lt/bgt is not a valid
E-Mail address' return errorMsg
email "someone_at_example...com"
https//www.pskitservices.com Contact no. -
9975288300
19
try //check if if(filter_var(email,
FILTER_VALIDATE_EMAIL) FALSE) //throw
exception if email is not valid throw new
customException(email) catch
(customException e) //display custom
message echo e-gterrorMessage() ?gt The new
class is a copy of the old exception class with
an addition of the errorMessage() function. Since
it is a copy of the old class, and it inherits
the properties and methods from the old class, we
can use the exception class methods like
getLine() and getFile() and getMessage(). Example
explained The code above throws an exception and
catches it with a custom exception class The
customException() class is created as an
extension of the old exception class. This way it
inherits all methods and properties from the old
exception class The errorMessage() function is
created. This function returns an error message
if an e-mail address is invalid The email
variable is set to a string that is not a valid
e-mail address The "try" block is executed and an
exception is thrown since the e-mail address is
invalid The "catch" block catches the exception
and displays the error message
https//www.pskitservices.com Contact no. -
9975288300
20
Multiple Exceptions It is possible for a script
to use multiple exceptions to check for multiple
conditions. It is possible to use several
if..else blocks, a switch, or nest multiple
exceptions. These exceptions can use different
exception classes and return different error
messages lt?php class customException extends
Exception public function errorMessage()
//error message errorMsg 'Error on line
'.this-gtgetLine().' in '.this-gtgetFile()
.' ltbgt'.this-gtgetMessage().'lt/bgt is not a valid
E-Mail address' return errorMsg
email "someone_at_example.com" try
//check if if(filter_var(email,
FILTER_VALIDATE_EMAIL) FALSE) //throw
exception if email is not valid throw new
customException(email)
https//www.pskitservices.com Contact no. -
9975288300
21
//check for "example" in mail address
if(strpos(email, "example") ! FALSE)
throw new Exception("email is an example
e-mail") catch (customException e)
echo e-gterrorMessage() catch(Exception e)
echo e-gtgetMessage() ?gt Example
explained The code above tests two conditions
and throws an exception if any of the conditions
are not met 1. The customException() class is
created as an extension of the old exception
class. This way it inherits all methods and
properties from the old exception class 2. The
errorMessage() function is created. This function
returns an error message if an e-mail address is
invalid.
https//www.pskitservices.com Contact no. -
9975288300
22
OUR SOFTWARE COURSES
https//www.pskitservices.com Contact no. -
9975288300
23
OUR HARDWARE SERVICES
https//www.pskitservices.com Contact no. -
9975288300
24
OUR SERVICE COURSES
25
PSK TECHNOLOGIES PVT.LTD IT COMPANY
Follow us on
https//www.pskitservices.com Contact No. -
9975288300
Write a Comment
User Comments (0)
About PowerShow.com