Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Main PDF 4 - PHP Predefined Functions

This document provides an overview of PHP predefined functions, including their objectives and usage. It covers functions for constants, file inclusion, mathematical operations, array manipulation, string manipulation, and date handling. Each section includes syntax, examples, and descriptions of various functions to enhance PHP programming skills.

Uploaded by

Charles Uy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Main PDF 4 - PHP Predefined Functions

This document provides an overview of PHP predefined functions, including their objectives and usage. It covers functions for constants, file inclusion, mathematical operations, array manipulation, string manipulation, and date handling. Each section includes syntax, examples, and descriptions of various functions to enhance PHP programming skills.

Uploaded by

Charles Uy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Applications Development and

Emerging Technologies
MODULE 4

PHP Predefined Functions


PHP Predefined Functions

Objectives
• To know how to include separate PHP code in the main
page for code enhancement.
• To be familiar with the use of common predefined function
such as define, include, and require.
• To use different available mathematical function for
manipulating numbers.
• To use number_format() function for number readability
purpose.
• To apply different function for Array Manipulation using the
function unset(), explode(), and implode().
• To manipulate string data using PHP string manipulation
function.
• To manipulate date with format using date function and
date format available.
PHP Predefined Functions

Using Constant

define() functions
used to declare constants. A constant can only be assigned a
scalar value, like a string or a number. A constant’s value
cannot be changed.
Syntax:
define(‘NAME’,’value’);

Example: Output:
PHP Predefined Functions

Including Files

You can separate your PHP file and embed it to your html by
using PHP include functions.

Include functions Description


include Includes and evaluates the specified file.
Generate a warning on failure message if file not
found.
require Performs the same way as the include function.
Generate a fatal error message if file not found
stopping the script at that point.
include_once Same as include function except it includes the file
only once.
require_once
Syntax: Same as require function except it includes the file
include(“file”);only once.
PHP Predefined Functions

Including Files

Most of the developers used include functions for their header


and footer. Also some use this to write their database
connection and so on.

You may write the file with an extension name of .inc rather than
.php to serve as a fragment of your program code.

In some scripts, a file might be included more than once,


causing function redefinitions, variable reassignments, and
other possible problems.
Syntax:
include(“filename.inc”);
include_once(“filename.inc”);
require(“filename.inc”);
require_once(“filename.inc”);
PHP Predefined Functions

Including Files: include()function (file not found)

Example:

Output:
PHP Predefined Functions

Including Files: require() function (file not found)

Example:

Output:
PHP Predefined Functions

Including Files: include()function (file exists)

Example: header.inc

Output:

Same output using require function


PHP Predefined Functions

Mathematical Function:
rand() function
- used to generate random integers
- syntax
int rand(void)
int rand(int $min, int $max)
ceil() function
- returns the next highest integer by rounding the value upwards
- syntax
float ceil(float $value)
floor() function
- returns the next lowest integer by rounding the value downwards
- syntax
float floor(float $value)
PHP Predefined Functions

Mathematical Function:
min() function
- Return the smallest value
- syntax
mixed min(array $values)
mixed min(mixed $values1, mixed $values2[,mixed $...])

max() function
- Return the highest value
- syntax
mixed max(array $values)
mixed max(mixed $values1, mixed $values2[,mixed $...])
PHP Predefined Functions

Mathematical Function:
rand(), ceil(), floor(), min(), max()

Example: Output:
PHP Predefined Functions

Mathematical Function:
number_format() function
- Format a number with grouped thousand
- syntax
string number_format (
float $number
[, int $decimals = 0 ] )

string number_format (
float $number ,
int $decimals = 0 ,
string $dec_point = '.' ,
string $thousands_sep = ',' )
PHP Predefined Functions

Mathematical Function: number_format function

Example: Output:
PHP Predefined Functions

Function for Array Manipulation:


unset function
- destroys the specified variable
- syntax:
void unset ( mixed $var [, mixed $... ] )
explode function
- split a string by string
- syntax:
array explode ( string $delimiter ,
string $string [, int $limit ] )
implode function
- join array elements to form a string
- syntax:
string implode ( string $glue , array $pieces )
string implode ( array $pieces )
PHP Predefined Functions

Function for Array Manipulation:


unset(), explode(), implode()

Example:

Output:
PHP Predefined Functions

Function for String Manipulation:


strlen function
- return the value length of a string
- syntax:
int strlen (string $string)
strpos function
- find the position of the first occurrence of a substring in a given
string
- syntax:
int strpos ( string $haystack ,
mixed $needle [, int $offset = 0 ] )
strrev function
- reverse a given string
- syntax:
string strrev ( string $string )
PHP Predefined Functions

Function for String Manipulation:


strtolower function
- converts string to lowercase
- syntax:
string strtolower ( string $str )
strtoupper function
- converts string to uppercase
- syntax:
string strtoupper ( string $str )
substr function
- returns part of a given string
- syntax:
string substr ( string $string ,
int $start [, int $length ] )
PHP Predefined Functions

Function for String Manipulation:


strlen(), strpos(), strrev(), strtolower(),
strtoupper(), substr()

Example:

Output:
PHP Predefined Functions

Function for String Manipulation:


ucfirst() function
- Make a string’s first character uppercase
- syntax:
string ucfirst( string $str )
ucwords() function
- converts string to uppercase
- syntax:
string ucwords ( string $str )
trim() function
- stripped white spaces or other characters from the beginning
and end of a string.
- syntax:
string trim ( string $str [, string $charlist ] )
PHP Predefined Functions

Function for String Manipulation:


ltrim() function
- strip white spaces or other characters from the beginning of a
string.
- syntax:
string ltrim ( string $str [, string $charlist ] )
rtrim() function
- strip white spaces or other characters from the end of a string.
- syntax:
string rtrim ( string $str [, string $charlist ] )
strip_tags() function
- strip HTML and PHP tags from a string.
- syntax:
string strip_tags ( string $str [, string
$allowable_tags ] )
PHP Predefined Functions

Function for String Manipulation:


ucfirst(), ucwords(), trim(), ltrim(), rtrim(), strip_tags()

Output:

Example:
PHP Predefined Functions

Function for String Manipulation:


ucfirst(), ucwords(), trim(), ltrim(), rtrim(), strip_tags()

Output:

Example:
PHP Predefined Functions

Date Manipulation: date() Function


- used to format a local time or date
- returns a string formatted according to the given format string.
- syntax:
string date ( string $format [, int $timestamp = time() ] )

Format Description Example returned


character values
DAY
d Day of the month, 2 digits with leading zeros 01 to 31
D A textual representation of a day, three letters Mon through Sun
j Day of the month without leading zeros 1 to 31
l A full textual representation of the day of the Sunday through
week Saturday
N ISO-8601 numeric representation of the day of 1 (for Monday)
the week (added in PHP 5.1.0) through 7 (for Sunday)
PHP Predefined Functions

Date Manipulation: date() Function

Format Description Example returned


character values
S English ordinal suffix for the day of the month, 2 st, nd, rd or th. Works
characters well with j
w Numeric representation of the day of the week 0 (for Sunday) through
6 (for Saturday)
z The day of the year (starting from 0) 0 through 365
WEEK
W ISO-8601 week number of year, weeks starting Example: 42 (the 42nd
on Monday week in the year)
PHP Predefined Functions

Date Manipulation: date() Function

Format Description Example returned


character values
MONTH
F A full textual representation of a month, such as January through
January or March December
m Numeric representation of a month, with 01 through 12
leading zeros
M A short textual representation of a month, three Jan through Dec
letters
n Numeric representation of a month, without 1 through 12
leading zeros
t Number of days in the given month 28 through 31
PHP Predefined Functions

Date Manipulation: date() Function

Format Description Example returned


character values
YEAR
L Whether it's a leap year 1 if it is a leap year, 0
otherwise.
o ISO-8601 year number. This has the same value Examples: 1999 or
as Y, except that if the ISO week number (W) 2003
belongs to the previous or next year, that year is
used instead.
Y A full numeric representation of a year, 4 digits Examples: 1999 or
2003
y A two digit representation of a year Examples: 99 or 03
PHP Predefined Functions

Date Manipulation: date() Function

Format Description Example returned


character values
TIME
a Lowercase Ante meridiem and Post meridiem am or pm
A Uppercase Ante meridiem and Post meridiem AM or PM
B Swatch Internet time 000 through 999
g 12-hour format of an hour without leading zeros 1 through 12
G 24-hour format of an hour without leading zeros 0 through 23
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
u Microseconds Example: 654321
PHP Predefined Functions

Date Manipulation: date() Function

Format Description Example returned


character values
TIMEZONE
e Timezone identifier Examples: UTC, GMT,
Atlantic/Azores
I Whether or not the date is in daylight saving 1 if Daylight Saving
time Time, 0 otherwise.
O Difference to Greenwich time (GMT) in hours Example: +0200
P Difference to Greenwich time (GMT) with colon Example: +02:00
between hours and minutes
T Timezone abbreviation Examples: EST, MDT
...
Z Timezone offset in seconds. The offset for -43200 through
timezones west of UTC is always negative, and 50400
for those east of UTC is always positive.
PHP Predefined Functions

Date Manipulation: date() Function

Format Description Example returned


character values
FULL DATE/TIME
c ISO 8601 date 2004-02-
12T15:19:21+00:00
r » RFC 2822 formatted date Example: Thu, 21 Dec
2000 16:01:07 +0200
U Seconds since the Unix Epoch (January 1 1970 See also time()
00:00:00 GMT)

http://php.net/docs.php
PHP Predefined Functions

Date Manipulation: date() Function

Example:

Output:
PHP Predefined Functions

Date Manipulation: mktime() function


- get the unix timestamp (January 1, 1970) for a given date. (with strict
notice)
- same as time() function (without strict notice)
- syntax:
int mktime ([ int $hour = date("H")
[, int $minute = date("i")
[, int $second = date("s")
[, int $month = date("n")
[, int $day = date("j")
[, int $year = date("Y")
[, int $is_dst = -1 ]]]]]]] )
PHP Predefined Functions

Date Manipulation: strtotime() function


- parse any English textual datetime description into a Unix timestamp
- syntax:
int strtotime ( string $time
[, int $now = time() ] )

You might also like