PHP String Functions
PHP String Functions
Reverses a string
strrev()
Syntax: echo strrev("Hello world!");
echo strlen("Pakistan!");
echo str_word_count("Pakistan Punjab!");
echo strrev("Pakistan Punjab!");
echo strpos("Pakistan Punjab!", "Punjab");
echo str_replace("Pakistan ", "Punjab!", "Hello Pakistan!");
echo strtolower("Pakistan Punjab");
echo strtoupper("Pakistan Punjab");
echo lcfirst("Pakistan Punjab!");
echo ucfirst("Pakistan Punjab!");
//Convert the first character of each word to uppercase:
echo ucwords("Pakistan Punjab");
//Return "world" from the string:
echo substr("Pakistan Punjab",6);
echo substr("Pakistan Punjab",0,10)."<br>";
echo substr("Pakistan Punjab",1,8)."<br>";
echo substr("Pakistan Punjab",0,5)."<br>";
echo substr("Pakistan Punjab",6,6)."<br>";
echo "<br>";
$arr = array('Pakistan','is','Beautiful','Country!’);
echo implode(" ",$arr)."<br>";
$str = "Pakistan is beautiful country.";
print_r (explode(" ",$str));
Array ( [0] => Pakistan [1] => is [2] => beautiful [3] => country.)
// break limit expload
$str = 'Lahore,Karachi,Peshawer,Quitta';
// zero limit
print_r(explode(',',$str,0));
print "<br>";
// positive limit
print_r(explode(',',$str,2));
print "<br>";
// negative limit
print_r(explode(',',$str,-1));
// case-sensitive constant name default(false)
define("GREETING", "Welcome to abc.com!",true);
echo GREETING;
PHP Function
display();
?>
PHP Function with Arguments
pdata("Ali", "25");
pdata("Aslam", "35");
pdata("Kamran", "21");
?>
PHP Default Value Argument
value(450);
value();
value(300);
value(500);
?>
PHP Functions - Returning values
The global keyword is used to access a global variable from within a function. To
do this, use the global keyword before the variables (inside the function):
Example
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
PHP The static Keyword
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes
we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static
keyword when you first declare the variable:
Then, each time the function is called, that variable will still have the information it contained from the
last time the function was called.
Note: The variable is still local to the function.
Example
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
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 superglobal variables are:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
Methods of Sending Information to
Server
A web browser communicates with the server typically using one of
the two HTTP (Hypertext Transfer Protocol) methods — GET and
POST. Both methods pass the information differently and have
different advantages and disadvantages, as described below.
The GET Method
In GET method the data is sent as URL parameters that are usually
strings of name and value pairs separated by ampersands (&). In
general, a URL with GET data will look like this:
http://www.example.com/action.php?name=john&age=24
The bold parts in the URL are the GET parameters and the italic parts
are the value of those parameters. More than one parameter=value
can be embedded in the URL by concatenating with ampersands
(&). One can only send simple text data via GET method.
PHP provides the superglobal variable $_GET to access all the
information sent either through the URL or submitted through an
HTML form using the method="get".
Advantages and Disadvantages of
Using the GET Method
Since the data sent by the GET method are displayed in the URL, it is
possible to bookmark the page with specific query string values.
The GET method is not suitable for passing sensitive information such
as the username and password, because these are fully visible in the
URL query string as well as potentially stored in the client browser's
memory as a visited page.
Because the GET method assigns data to a server environment
variable, the length of the URL is limited. So, there is a limitation for
the total data to be sent.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example of PHP GET method</title>
</head>
<body>
<?php
if(isset($_GET["name"])){
echo "<p>Hi, " . $_GET["name"] . "</p>";}?>
<form method="get" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label for="inputName">Name:</label>
<input type="text" name="name" id="inputName">
<input type="submit" value="Submit">
</form>
</body>
PHP $_POST is widely used to collect form data after submitting an HTML form
with method="post". $_POST is also widely used to pass variables.
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 <form> tag. In this example, we
point to the 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 $_POST to collect the value of the input field:
Example