Categories
PHP Programming Language

PHP foreach

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:

foreach (iterable_expression as $value)
    statement
foreach (iterable_expression as $key => $value)
    statement

The first form traverses the iterable given by iterable_expression. On each iteration, the value of the current element is assigned to $value.

The second form will additionally assign the current element’s key to the $key variable on each iteration.

<?php
foreach (array(1, 2, 3, 4) as &$value) {
    $value = $value * 2;
}
?>
Categories
PHP

PHP Arrays

An array stores multiple values in one single variable:

Example:

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

What is an Array?

An array is a special variable, which can hold more than one value at a time.

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The solution is to create an array!

An array can hold many values under a single name, and you can access the values by referring to an index number.


Create an Array in PHP

In PHP, the array() function is used to create an array:

array();

In PHP, there are three types of arrays:

  • Indexed arrays – Arrays with a numeric index
  • Associative arrays – Arrays with named keys
  • Multidimensional arrays – Arrays containing one or more arrays

Get The Length of an Array – The count() Function

The count() function is used to return the length (the number of elements) of an array:

Example:

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
Categories
PHP

preg_replace

preg_replace — Perform a regular expression search and replace

Description

preg_replace ( string|array $pattern , string|array $replacement , string|array $subject [, int $limit = -1 [, int &$count = null ]] ) : string|array|null

Examples

Example #1 Using backreferences followed by numeric literals

<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
?>

The above example will output:

April1,2003
Categories
PHP

PHP str_replace

str_replace — Replace all occurrences of the search string with the replacement string

Description


str_replace ( array|string $search , array|string $replace , string|array $subject [, int &$count = null ] ) : string|array

If you don’t need fancy replacing rules (like regular expressions), you should use this function instead of preg_replace().

This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

Examples

Example #1 Basic str_replace() examples

<?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

// Provides: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>