Friday 15 March 2013

PHP Built-in Functions

Some of the extensively used built-in php functions are:-

1. Explode


Split a string by string.
array explode ( string $delimiter , string $string [, int $limit ] );
explode() examples

<?php
//
$pizza
$pieces
echo
echo

// Example 2
$data
list($user,
 $data);
echo
echo

?>

2. Implode


implode — Join array elements with a string.
string implode ( string $glue , array $pieces );
implode() example
<?php

$array
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

?>

3. Print_r


Prints human-readable information about a variable.
print_r() example

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y',
 'z'));
print_r ($a);
?>

4. Preg_replace


Perform a regular expression search and replace.
String preg_replace ($pattern,$replacement,$string);
Using indexed arrays with preg_replace()


<?php
$string
$patterns = array();
$patterns[0]
$patterns[1] = '/brown/';
$patterns[2]
$replacements = array();
$replacements[2]
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>

5. Substr


Return part of a string.
string substr ( string $string , int $start [, int $length ] );
Using a negative start
 

<?php
$rest
$rest = substr("abcdef", -2);    // returns "ef"

$rest = substr("abcdef", -3, 1); // returns "d"
?>

6. Array


Create an array.
array array ([ mixed $... ] );

array() example
<?php
$fruits
    "fruits"  => array("a" => "orange", "b" => "banana", "c" => "app
le"),
    "numbers" => array(1, 2, 3, 4, 5, 6),
    "holes"   => array("first", 5 => "second", "third")
);
?>

7. Include, Include_once, require, require_once


The include() statement includes and evaluates the specified file.
 include ('vars.php');

The include_once() statement includes and evaluates the
specified file during the execution of the script. This is a behavior
similar to the include()statement, with the only difference being
that if the code from a file has already been included, it will not be
included again. As the name suggests, it will be included just once.

require() is identical to include() except upon failure it will also
produce a fatal E_COMPILE_ERROR level error. In other words, it will
halt the script whereas include() only emits a warning (E_WARNING)
which allows the script to continue.

The require_once() statement is identical to require() or inclide()
except PHP will check if the file has already been included, and if
so, not include (require) it again.

PHP $_GET Function


The built-in $_GET function is used to collect values from a form sent with
method="get".

Information sent from a form with the GET method is visible to everyone
(it will be displayed in the browser's address bar) and has limits on the
amount of information to send.

Example

<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

When the user clicks the "Submit" button, the URL sent to the server
could look something like this:

http://yourdomainname.com/welcome.php?fname=Peter&age=37

The "welcome.php" file can now use the $_GET function to collect form
data (the names of the form fields will automatically be the keys in the
$_GET array):

Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

When to use method="get"?

When using method="get" in HTML forms, all variable names and values
are displayed in the URL.

Note: This method should not be used when sending passwords or other
sensitive information!

However, because the variables are displayed in the URL, it is possible to
bookmark the page. This can be useful in some cases.

Note: The get method is not suitable for very large variable values. It
should not be used with values exceeding 2000 characters.

PHP $_POST Function


The built-in $_POST function is used to collect values from a form sent
with method="post".

Information sent from a form with the POST method is invisible to others
and has no limits on the amount of information to send.

Note: However, there is an 8 Mb max size for the POST method, by
default (can be changed by setting the post_max_size in the php.ini file).

Example

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

When the user clicks the "Submit" button, the URL will look like this:

http://yourdomainname.com/welcome.php

The "welcome.php" file can now use the $_POST function to collect form
data (the names of the form fields will automatically be the keys in the
$_POST array):

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

No comments:

Post a Comment