Uppercase the first character of each word in a string

  • ≪ ucfirst
  • vfprintf ≫
  • PHP Manual
  • String 함수 목록
  • Uppercase the first character of each word in a string

ucwords

(PHP 4, PHP 5, PHP 7)

ucwords — Uppercase the first character of each word in a string

설명

string ucwords ( string $str [, string $delimiters = " \t\r\n\f\v" ] )

Returns a string with the first character of each word in str capitalized, if that character is alphabetic.

The definition of a word is any string of characters that is immediately after any character listed in the delimiters parameter (By default these are: space, form-feed, newline, carriage return, horizontal tab, and vertical tab).

인수

str

The input string.

delimiters

The optional delimiters contains the word separator characters.

반환값

Returns the modified string.

변경점

버전 | 설명
5.4.32, 5.5.16 | Added the delimiters parameter.

예제

Example #1 ucwords() example

<?php
$foo = 'hello world!';
$foo = ucwords($foo);             // Hello World!

$bar = 'HELLO WORLD!';
$bar = ucwords($bar);             // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>

Example #2 ucwords() example with custom delimiter

<?php
$foo = 'hello|world!';
$bar = ucwords($foo);             // Hello|world!

$baz = ucwords($foo, "|");        // Hello|World!
?>

주의

Note: 이 함수는 바이너리 안전입니다.

참고

  • strtoupper() - 문자열을 대문자로 만듭니다
  • strtolower() - 문자열을 소문자로 만듭니다
  • ucfirst() - Make a string's first character uppercase
  • mb_convert_case() - Perform case folding on a string

User Contributed Notes

almino dot melo at gmail dot com 01-Mar-2016 05:36
ucwords for human names in Brazil.
ucwords personalizada para nomes proprios brasileiros.

<?php
    /**
     * ucwords for human names in Brazil
     * Edit from http://php.net/manual/pt_BR/function.ucwords.php#112795
     * @param string $str
     * @param array $delimiters
     * @param array $exceptions Exceptions are words you don't want converted
     * @return string
     */
    function name($str, $delimiters = array(
        " ",
        "-",
        ".",
        "'",
        "O'",
        "Mc",
    ), $exceptions = array(
        "de",
        "do",
        "da",
        "dos",
        "das",
    )) {
        $result = '';

        foreach ($delimiters as $delimiter) {
            # If string has a delimiter
            if (strstr($str, $delimiter)) {

                $ucfirst = array();
                # Apply ucfirst to every word
                foreach (explode($delimiter, mb_strtolower($str)) as $word) {
                    $word = mb_convert_case($word, MB_CASE_TITLE);

                    # Working with exceptions
                    if (in_array(mb_strtoupper($word), $exceptions)) {
                        $word = mb_strtoupper($word);
                    } elseif (in_array(mb_strtolower($word), $exceptions)) {
                        $word = mb_strtolower($word);
                    } elseif (preg_match('/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/', mb_strtoupper($word))) {
                        # Is roman numerals? # http://stackoverflow.com/a/267405/437459
                        $word = mb_strtoupper($word);
                    }

                    $ucfirst[] = $word;
                }

                # string's first character uppercased
                $result = implode($delimiter, $ucfirst);
            }
        }

        return $result;
    }
?>
Lech 01-Sep-2015 11:36
This will correct capitalisation in names taking note of special capitalisation for Mc..., Mac..., and O'... Other special cases, of which I am not aware, can be added easily.

This is just a slight improvement on "deepdene at email dot com"'s name_case function... Thank you for original function.

<?php

function name_case($name) {
    if( !$name ) return $name;
    $newname = strtoupper($name[0]);
    $break = false;
    for( $i=1; $i<strlen($name); ++$i ) {
      $subed = substr($name, $i, 1);
      if( ord($subed) > 64 && ord($subed) < 123 || ord($subed) > 48 && ord($subed) < 58 ) {
        if( $break ) {
          $newname .= strtoupper($subed);
        }
        elseif( $i > 1 && in_array(substr($newname, $i-2, 2), array('Mc', 'O\'')) || $i > 2 && in_array(substr($newname, $i-3, 3), array('Mac')) ) {
          $newname .= strtoupper($subed);
        }
        else {
          $newname .= strtolower($subed);
        }
        $break = false;
      }
      else {
        // not a letter - a boundary
        $newname .= $subed;
        $break = true;
      }
    }
    return $newname;

?>
coder22 at nycap dot rr dot com 05-Jun-2015 01:26
After a while of trial and error it appears that ucwords does not convert a string that is already in all caps to the ucwords format as inidcated in the beginning of the function manual.

for example,
$a = my text
$b = ucwords($a)
echo $b = My Text.

However:
$a = MY TEXT;
$b = ucwords($a)
echo $b = MY TEXT

if the string is already uppercase, the function will not use strtolower then ucwords which is interpreted up above.  I just wanted people to know that.  I could not figure out what was going on.
Alex Milkovskyi 13-May-2015 06:22
Convert string to in camel-case, useful for class name patterns:
<?php
/**
   * Convert string to in camel-case, useful for class name patterns.
   *
   * @param $string
   *   Target string.
   *
   * @return string
   *   Camel-case string.
   */
function toCamelCase($string){
    $string = str_replace('-', ' ', $string);
    $string = str_replace('_', ' ', $string);
    $string = ucwords(strtolower($string));
    $string = str_replace(' ', '', $string);
    return $string;
}
?>

Example:
toCamelCase(make_mE camel-case pLEase) will return:
MakeMeCamelCasePlease
Florian 20-Feb-2015 05:03
to convert first letters in a firstname like: "jean-pierre" to "Jean-Pierre":

I simply replace the '-' with a vertical tabulation, because the ucwords() function replace the letter just after it.

so, I use the sentence:
$result=str_replace(chr(11),'-',ucwords(strtolower(str_replace('-',chr(11),$firstname))));
OneOfOne 19-Oct-2013 05:46
This function will work on anything, not just words separated by spaces. (eg: 'ac/bc/dc,xc zz' => 'Ac/Bc/Dc,Xc Zz')

function ucwords_all($str) {
    return preg_replace_callback('/\b(\w)/', function($m) {
        return strtoupper($m[0]);
    }, $str);
}
sunamak at odeva dot com 14-Aug-2013 09:30
function ucwords_tr($str) {
    return mb_convert_case(str_replace(array('i', 'I'),    array('?', 'ı'), $str), MB_CASE_TITLE, 'UTF-8');
}
antoniomax at antoniomax dot com 24-Jul-2013 03:04
Para formatar nomes em pt-br:

<?php

    function titleCase($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc"), $exceptions = array("de", "da", "dos", "das", "do", "I", "II", "III", "IV", "V", "VI"))
    {
        /*
         * Exceptions in lower case are words you don't want converted
         * Exceptions all in upper case are any words you don't want converted to title case
         *   but should be converted to upper case, e.g.:
         *   king henry viii or king henry Viii should be King Henry VIII
         */
        $string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
        foreach ($delimiters as $dlnr => $delimiter) {
            $words = explode($delimiter, $string);
            $newwords = array();
            foreach ($words as $wordnr => $word) {
                if (in_array(mb_strtoupper($word, "UTF-8"), $exceptions)) {
                    // check exceptions list for any words that should be in upper case
                    $word = mb_strtoupper($word, "UTF-8");
                } elseif (in_array(mb_strtolower($word, "UTF-8"), $exceptions)) {
                    // check exceptions list for any words that should be in upper case
                    $word = mb_strtolower($word, "UTF-8");
                } elseif (!in_array($word, $exceptions)) {
                    // convert to uppercase (non-utf8 only)
                    $word = ucfirst($word);
                }
                array_push($newwords, $word);
            }
            $string = join($delimiter, $newwords);
       }//foreach
       return $string;
    }

?>

Usage:

<?php
    $s = 'SAO JOAO DOS SANTOS';
    $v = titleCase($s); // 'Sao Joao dos Santos'
?>
ATOLYE15 06-Jun-2013 06:11
This solution is a proper use of capitalize first letters of "TURKISH" sentence by ATOLYE15 crew.

function ucwordstr($sentence)
{
        $letters = array('I','?','C','?','U','O','?');
        $replace = array('ı','i','c','?','u','o','?');

        $sentence = mb_strtolower(str_replace($letters,$replace,$sentence),"UTF-8");

        $words = array();

        foreach(explode(" ",$sentence) as $word)
        {
            $first = str_replace($replace,$letters,mb_substr($word, 0, 1, "UTF-8"));
            $other = mb_substr($word,1,strlen($word)-1,"UTF-8");

            $words[] = $first.$other;
        }

        $sentence = implode(" ",$words);

        return ucwords($sentence);
}
hrvoj3e at gmail dot com 30-Apr-2013 11:44
UTF-8 Title Case that works for me even with hyphens involved!

$str = 'đaa?aa-AAAaaa, BBbb';

$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");

echo($str): 'đaa?aa-Aaaaaa, Bbbb'
Luca Borrione luca -a email -d c_o_m 11-Sep-2012 12:41
Features:
- multi byte compatible
- handles multiple delimiters

<?php
function ucwords_specific ($string, $delimiters = '', $encoding = NULL)
{
    if ($encoding === NULL) { $encoding = mb_internal_encoding();}

    if (is_string($delimiters))
    {
        $delimiters =  str_split( str_replace(' ', '', $delimiters));
    }

    $delimiters_pattern1 = array();
    $delimiters_replace1 = array();
    $delimiters_pattern2 = array();
    $delimiters_replace2 = array();
    foreach ($delimiters as $delimiter)
    {
        $uniqid = uniqid();
        $delimiters_pattern1[]   = '/'. preg_quote($delimiter) .'/';
        $delimiters_replace1[]   = $delimiter.$uniqid.' ';
        $delimiters_pattern2[]   = '/'. preg_quote($delimiter.$uniqid.' ') .'/';
        $delimiters_replace2[]   = $delimiter;
    }

    // $return_string = mb_strtolower($string, $encoding);
    $return_string = $string;
    $return_string = preg_replace($delimiters_pattern1, $delimiters_replace1, $return_string);

    $words = explode(' ', $return_string);

    foreach ($words as $index => $word)
    {
        $words[$index] = mb_strtoupper(mb_substr($word, 0, 1, $encoding), $encoding).mb_substr($word, 1, mb_strlen($word, $encoding), $encoding);
    }

    $return_string = implode(' ', $words);

    $return_string = preg_replace($delimiters_pattern2, $delimiters_replace2, $return_string);

    return $return_string;
}
?>

Params:
1. string: The string being converted
2. delimiters: a string with all wanted delimiters written one after the other e.g. "-'"
3. encoding: Is the character encoding. If it is omitted, the internal character encoding value will be used.

Example Usage:
<?php
mb_internal_encoding('UTF-8');
$string = "JEAN-PAUL d'artagnan ??-oA-eI hello - world";
echo ucwords_specific( mb_strtolower($string, 'UTF-8'), "-'");
?>

Output:
Jean-Paul D'Artagnan ??-Oa-Ei Hello - World
qeremy [atta] gmail [dotta] com 28-Feb-2012 02:09
A proper Turkish solution;

<?php
function ucfirst_turkish($str) {
    $tmp = preg_split("//u", $str, 2, PREG_SPLIT_NO_EMPTY);
    return mb_convert_case(
        str_replace("i", "?", $tmp[0]), MB_CASE_TITLE, "UTF-8").
        $tmp[1];
}

function ucwords_turkish($str) {
    return preg_replace("/(\\w+)/ue", "ucfirst_turkish('\\\\1').'$2'", $str);
}

$str = "iyilik guzelL?K ?eker ";
echo ucwords($str) ."\\n";   // Iyilik GuzelL?K ?eker
echo ucwords_turkish($str); // ?yilik GuzelL?K ?eker
?>
haijerome at gmail dot com 21-Nov-2011 06:14
Thanks a lot brother.

I tested it with a few variations. It works perfectly. Its really great and simple usage of the existing functions. It would be glad to all PHP folks and good to PHP if these kind of functions will be in PHP library on upcoming releases.

<?php

$name1 = "mark-yves robert";
$name2 = "mark-yves robert-bryan";

echo '<br/>Name 1 (mark-yves robert) =>'.
ucwordspecific($name1,'-'); //returns Mark-Yves Robert

echo '<br/>Name 2 (mark-yves robert-bryan)

=>'.ucwordspecific($name2,'-');
//returns Mark-Yves Robert-Bryan

function ucwordspecific($str,$delimiter){
$delimiter_space = '- ';
return str_replace($delimiter_space,$delimiter,ucwords

(str_replace($delimiter,$delimiter_space,$str)));
}

?>

Proud to be a PHP enthusiast always :-)
Anonymous 20-Nov-2011 08:06
Make the first letter of any word in capital even they are separated by hyphen.

 Eg : donze pierre-yves      => Donze Pierre-Yves
        joye-cagnard frederic => Joye-Cagnard Frederic

<?php

 $str = "donze pierre-yves";
 echo ucwordsHyphen($str); /*returns Donze Pierre-Yves*/

function ucwordsHyphen($str){
    return str_replace('- ','-',ucwords(str_replace('-','- ',$str)));
}
?>

 Thanks.
haijerome at gmail dot com 12-Nov-2011 08:38
Recently i got a request from a client to make the first letter of any word in capital even they are separated by hyphen.

Eg : donze pierre-yves      => Donze Pierre-Yves
       joye-cagnard frederic => Joye-Cagnard Frederic

ucwords("donze pierre-yves") can make it display
Donze Pierre-yves but actually i need even the first letter of the second word to be in capital.

So in order to get this i have just written a function below and it works fine. But i hope there would be a better and easier way to do this if so kindly share.

<?php

$str = "donze pierre-yves";
echo ucwordsHyphen($str); /*returns Donze Pierre-Yves*/

function ucwordsHyphen($str){
                    $converted_str = "";
                    $str_array = explode(" ",$str);
                    foreach($str_array as $key=>$value):
                        if(strpos($value, '-')):
                             $value = str_replace("-", " ", $value);
                             $value = ucwords(strtolower($value));
                             $value = str_replace(" ", "-", $value);
                        else:
                            $value = ucwords(strtolower($value));
                        endif;
                        $converted_str .= " ".$value;
                    endforeach;
                   return $converted_str;
            }
 ?>

Thanks.
And Proud to be a PHP Programmer always :-)
ahmet363 at gmail dot com 16-Oct-2011 08:14
Turkish character with the ucwords function...

<?php
function ucwords_tr($gelen){

  $sonuc='';

  $kelimeler=explode(" ", $gelen);

  foreach ($kelimeler as $kelime_duz){

    $kelime_uzunluk=strlen($kelime_duz);
    $ilk_karakter=mb_substr($kelime_duz,0,1,'UTF-8');

    if($ilk_karakter=='C' or $ilk_karakter=='c'){

      $ilk_karakter='C';

    }elseif ($ilk_karakter=='?' or $ilk_karakter=='?') {

      $ilk_karakter='?';

    }elseif($ilk_karakter=='I' or $ilk_karakter=='ı'){

      $ilk_karakter='I';

    }elseif ($ilk_karakter=='?' or $ilk_karakter=='i'){

      $ilk_karakter='?';

    }elseif ($ilk_karakter=='O' or $ilk_karakter=='o'){

      $ilk_karakter='O';

    }elseif ($ilk_karakter=='?' or $ilk_karakter=='?'){

      $ilk_karakter='?';

    }elseif ($ilk_karakter=='U' or $ilk_karakter=='u'){

      $ilk_karakter='U';

    }else{

      $ilk_karakter=strtoupper($ilk_karakter);

    }

    $digerleri=mb_substr($kelime_duz,1,$kelime_uzunluk,'UTF-8');
    $sonuc.=$ilk_karakter.kucuk_yap($digerleri).' ';

  }

  $son=trim(str_replace('  ', ' ', $sonuc));
  return $son;

}

function kucuk_yap($gelen){

  $gelen=str_replace('C', 'c', $gelen);
  $gelen=str_replace('?', '?', $gelen);
  $gelen=str_replace('I', 'ı', $gelen);
  $gelen=str_replace('?', 'i', $gelen);
  $gelen=str_replace('O', 'o', $gelen);
  $gelen=str_replace('?', '?', $gelen);
  $gelen=str_replace('U', 'u', $gelen);
  $gelen=strtolower($gelen);

  return $gelen;
}

echo ucwords_tr('?EKardi ?EM??YE ?ELENO OMER'); // ?ekardi ?em?iye ?eleno Omer
echo ucwords_tr('?EKER iMSAK ?OLEN'); // ?eker ?msak ?olen
Philippe 30-Sep-2011 02:54
Because I needed a function that did the same as the function proper in Excel I created this little function :

<?php
function proper($string){

    $string = strtolower($string);

    $lettersAndNumbers = 'abcdefghijklmnopqrstuvwxyz01234565789';

    $string[0] = strtoupper($string[0]);

    for ($index = 1;$index < strlen($string);$index++){
      if (strpos($lettersAndNumbers, $string[$index]) === false){
        if ($index +1 < strlen($string)){
          $string[$index+1] = strtoupper($string[$index+1]);
          $index++;
        }
      }
    }

    return($string);

  }
?>
gabe at fijiwebdesign dot com 24-Aug-2011 08:49
Another ucwords function with custom separator.

<?php
function ucwords_custom($str, $sep)
{
    return preg_replace_callback('/' . preg_quote($sep) . './i', create_function(
        '$match',
        'return strtoupper($match[0][1]);'
    ), $str);
}
?>

or simpler with PHP5.3

<?php
function ucwords_custom2($str, $sep = ' ')
{
    return preg_replace_callback('/' . preg_quote($sep) . './i', function($match) {
        return strtoupper($match[0][1]);
    }, $str);
}
?>
Sebstian Espinosa 05-Aug-2011 01:16
Is more simple to use str_replace to use ucwords without using space as separator like:

<?php

function ucwordsEx($str, $separator){

      $str = str_replace($separator, " ", $str);
      $str = ucwords(strtolower($str));
      $str = str_replace(" ", $separator, $str);
      return $str;

}
/*
Example:
*/
echo ucwordsEx("HELLO-my-NAME-iS-maNolO", "-");
/*
Prints: "Hello My Name Is Manolo"
*/

?>
jmarois at ca dot ibm dot com 12-Feb-2010 05:51
My quick and dirty ucname (Upper Case Name) function.

<?php
//FUNCTION

function ucname($string) {
    $string =ucwords(strtolower($string));

    foreach (array('-', '\'') as $delimiter) {
      if (strpos($string, $delimiter)!==false) {
        $string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
      }
    }
    return $string;
}
?>
<?php
//TEST

$names =array(
  'JEAN-LUC PICARD',
  'MILES O\'BRIEN',
  'WILLIAM RIKER',
  'geordi la forge',
  'bEvErly CRuSHeR'
);
foreach ($names as $name) { print ucname("{$name}\n"); }

//PRINTS:
/*
Jean-Luc Picard
Miles O'Brien
William Riker
Geordi La Forge
Beverly Crusher
*/
?>

You can add more delimiters in the for-each loop array if you want to handle more characters.
Kovik 27-Dec-2009 04:37
ucwords() only uses whitespace as a delimiter. I found that I needed the ability to use other delimiters for denoting words.

Example: Transforming "content-type" into "Content-Type".

<?php
/**
 * Capitalize all words
 * @param string Data to capitalize
 * @param string Word delimiters
 * @return string Capitalized words
 */
function capitalizeWords($words, $charList = null) {
    // Use ucwords if no delimiters are given
    if (!isset($charList)) {
        return ucwords($words);
    }

    // Go through all characters
    $capitalizeNext = true;

    for ($i = 0, $max = strlen($words); $i < $max; $i++) {
        if (strpos($charList, $words[$i]) !== false) {
            $capitalizeNext = true;
        } else if ($capitalizeNext) {
            $capitalizeNext = false;
            $words[$i] = strtoupper($words[$i]);
        }
    }

    return $words;
}
?>

Testing:
<?php
echo capitalizeWords('this is a test');  // This Is A Test
echo capitalizeWords('this-is-a-test');  // This-is-a-test
echo capitalizeWords('this is a test', ' ');  // This Is A Test
echo capitalizeWords('this-is-a-test', '-');  // This-Is-A-Test
echo capitalizeWords('this-is a-test', ' ');  // This-is A-test
echo capitalizeWords('this-is a-test', '-');  // This-Is a-Test
echo capitalizeWords('this-is a-test', ' -');  // This-Is A-Test
?>
robert at broofa dot com 08-Jul-2009 03:42
Some recipes for switching between underscore and camelcase naming:

<?php
// underscored to upper-camelcase
// e.g. "this_method_name" -> "ThisMethodName"
preg_replace('/(?:^|_)(.?)/e',"strtoupper('$1')",$string);

// underscored to lower-camelcase
// e.g. "this_method_name" -> "thisMethodName"
preg_replace('/_(.?)/e',"strtoupper('$1')",$string);

// camelcase (lower or upper) to underscored
// e.g. "thisMethodName" -> "this_method_name"
// e.g. "ThisMethodName" -> "this_method_name"
strtolower(preg_replace('/([^A-Z])([A-Z])/', "$1_$2", $string));
?>

Of course these aren't 100% symmetric.  For example...
  * this_is_a_string -> ThisIsAString -> this_is_astring
  * GetURLForString -> get_urlfor_string -> GetUrlforString
Greg S 26-Apr-2009 06:16
I did the same thing as Catalin, but for French names.

Here's what I'm doing :

For each word (not considering the single-quote as a word boundary character) :
- Put the word in lower case
- If the word is "de", return, else, put the first letter in upper-case
- See if the second character of the word is a single-quote
- Yes ? Put the next char in upper case
- And if the char before the single quote is D, put it back to lower case (-> d)

This complies with the French rules for capitalization in names.

Sample results :
-d'Afoo Bar
-de Foo Bar
-O'Foo Bar
-Foo'bar

<?php
function my_ucwords($s) {
        $s = preg_replace_callback("/(\b[\w|']+\b)/s", fixcase_callback, $s);

        return $s;

    }

    function fixcase_callback($word) {

        $word = $word[1];

        $word = strtolower($word);

        if($word == "de")
            return $word;

        $word = ucfirst($word);

        if(substr($word,1,1) == "'") {
            if(substr($word,0,1) == "D") {
                $word = strtolower($word);
            }
            $next = substr($word,2,1);
            $next = strtoupper($next);
            $word = substr_replace($word, $next, 2, 1);
        }

        return $word;
    }
?>
vista_ at live dot se 17-Nov-2008 11:02
<?php
      if(!function_exists('mb_ucwords'))
      {
            function mb_ucwords($str)
            {
                  return mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
            }
      }
?>
dyer85 at gmail dot com 07-Nov-2008 12:33
When attempting to adopt a solution similar to Catalin's post (20-Oct-2008 10:14), I ran into some additional problems. Just a heads up, Catalin's wasn't capitalizing correctly when a name like "O'reilley" appeared at the start of the string or a new line. Also, it doesn't account for locale-sensitivity.

I also needed to recognize additional Irish surnames, such as "McArthur/MacArthur" or "FitzGerald". I also didn't want to misinterpret French as an Irish surname, so, "S'il vous plait" shouldn't result in the "i" in "S'il" capitalized.

I modified Catalin's version, but it's still not perfect. This version happened to suit my needs, so be sure to assess your own needs before using.

<?php

function my_ucwords($str)
{
    $str = ucwords($str);

    // Not perfect
    return preg_replace(
        '/
            (?: ^ | \\b )         # assertion: beginning of string or a word boundary
            ( O\' | Ma?c | Fitz)  # attempt to match Irish surnames
            ( [^\W\d_] )          # match next char; we exclude digits and _ from \w
        /xe',
        "'\$1' . strtoupper('\$2')",
        $str);
}

?>
strazds at gmail dot com 01-Nov-2008 11:45
ucwords for UTF-8 strings:

<?php
function mb_ucwords($str) {
    $str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
    return ($str);
}
?>
Catalin 20-Oct-2008 04:14
I have looked for a work-around that would upper-case letter after an ' too.

For example,

ucword("o'lunney's");

would output "O'lunney's" and I wanted it to output "O'Lunney's".

Here is my function:

<?php
function my_ucwords($string) {
        $string = ucwords(strtolower($string));
        $string = preg_replace_callback("/( [ a-zA-Z]{1}')([a-zA-Z0-9]{1})/s",create_function('$matches','return $matches[1].strtoupper($matches[2]);'),$string);
        return $string;
}
?>

Hope it helps someone.
Alex 27-Aug-2008 06:43
A modified sentenceNormalizer by gregomm

Features:
1- Removes duplicated question marks, exclamations and periods
2- Capitalize first letter of a sentence.
3- Split sentences not only with "." but also with "?" and "!"
4- Puts a white space at the end of each sentence
5- Retains newlines

--removed from orginal function--
undestand the meaning of "¡" and "¿" in languages like spanish.
undestand the htmlentitity version of this simbols.
--removed from orginal function--

<?php
function sentenceNormalizer($sentence_split) {
    $sentence_split = preg_replace(array('/[!]+/','/[?]+/','/[.]+/'),
                                   array('!','?','.'),$sentence_split);

    $textbad = preg_split("/(\!|\.|\?|\n)/", $sentence_split,-1,PREG_SPLIT_DELIM_CAPTURE);
    $newtext = array();
    $count = sizeof($textbad);

    foreach($textbad as $key => $string) {
        if (!empty($string)) {
            $text = trim($string, ' ');
            $size = strlen($text);

            if ($size > 1){
                $newtext[] = ucfirst(strtolower($text));
            }
                elseif ($size == 1) {
                    $newtext[] = ($text == "\n") ? $text : $text . ' ';
                }
        }
    }

    return implode($newtext);
}
?>
majd87 at gmail dot com 06-Aug-2008 03:58
I modified Q1712's code (below) to use regular expressions
instead of characters to properly capitalize words that fall
directly after an unwanted character. See his post for details.

This version allows me to use it around html elements, etc.

<?php
    function my_ucwords($string){

        $invalid_characters = array('"',
                                    '\(',
                                    '\[',
                                    '\/',
                                    '<.*?>',
                                    '<\/.*?>');

        foreach($invalid_characters as $regex){
            $string = preg_replace('/('.$regex.')/','$1 ',$string);
        }

        $string=ucwords($string);

        foreach($invalid_characters as $regex){
            $string = preg_replace('/('.$regex.') /','$1',$string);
        }

        return $string;
    }
?>

Moreover, to get a proper title case, i combine it with this function:

This Function is obtained from:
http://codesnippets.joyent.com/posts/show/716

<?php
    function title_case($title) {
        $smallwordsarray = array(
            'of','a','the','and','an','or','nor','but','is','if','then',
'else','when',
            'at','from','by','on','off','for','in','out',
'over','to','into','with'
        );

        $words = explode(' ', $title);
        foreach ($words as $key => $word)
        {
            if ($key == 0 or !in_array($word, $smallwordsarray))
            $words[$key] = $this->my_ucwords(strtolower($word));
        }

        $newtitle = implode(' ', $words);
        return $newtitle;
    }
?>
Hope you find it useful.
gregomm at gmail dot com 23-May-2008 09:38
An improved of ucsentence. In fact its a function to avoid ugly data entry. It's based on code taken from this page. Can be inproved for better performance, of course...

Features:
1- removes duplicated question marks, an exclamations
2- Capitalize first letter of a sentence.
3- split sentences not only with "." but also with "?" and "!"
4- Puts a white space at the begining of each sentence
5- undestand the meaning of "¡" and "¿" in languages like spanish.
6- undestand the htmlentitity version of this simbols.

    function sentenceNormalizer( $sentence_split ) {

        $sentence_split = preg_replace("/[!]+/","!",$sentence_split);
        $sentence_split = preg_replace("/[¡]+/","¡",$sentence_split);
        $sentence_split = preg_replace("/[?]+/","?",$sentence_split);
        $sentence_split = preg_replace("/[¿]+/","¿",$sentence_split);

        $textbad = preg_split("/(\<[a-zA-Z0-9-]*\>".
"\!(\s)?|\.(\s)?|\?(\s)?|¿(\s)?|¡(\s)?".
"|&iquest;(\s)?|&iexcl;(\s)?)/",
         $sentence_split,-1,PREG_SPLIT_DELIM_CAPTURE);
        $newtext = array();
        $count = sizeof($textbad);

        $prevStr ="";

        for ($i = 0; $i < $count; $i++){

            $text = trim($textbad[$i]);
            $size = strlen($text);

            if ($size>1){

                $sentencegood=ucfirst(strtolower($text));

                if ($i>0 && $prevStr != '¿' && $prevStr != '¡' && $prevStr !="&iquest;" && $prevStr !="&iexcl;"){
                    $sentencegood =" ".$sentencegood;
                }
                $newtext[] = $sentencegood;
                $prevStr =$text;

            }elseif($size==1 ){

                if ($i>0 && ($text == '¿' || $text == '¡' || $prevStr =="&iquest;" || $prevStr =="&iexcl;") ){
                    $newtext[] =" ".$text;
                }else{
                    $newtext[] =$text;
                }
                $prevStr =$text;
            }

        }

        $textgood = implode($newtext);
        return $textgood;
    }
NoName 12-Mar-2008 09:03
For strings with diactrical marks (umlauts, etc.), consider mb_convert_case.
blake at goinoutwest dot com 30-Jan-2008 02:14
Relating to the mb_ucwords() function posted by Anonymous.  In order for this to actually be multi-byte compliant, you would also need to use mb_substr() and mb_strlen() instead of substr and strlen respectively.

Here it is corrected and extended even further to allow multiple word separators and a list of exceptions to correct after title casing. It's a bit tedious and inelegant, but things frequently are when dealing with human languages.

function mb_ucwords($str) {
    $exceptions = array();
    $exceptions['Hp'] = 'HP';
    $exceptions['Ibm'] = 'IBM';
    $exceptions['Gb'] = 'GB';
    $exceptions['Mb'] = 'MB';
    $exceptions['Cd'] = 'CD';
    $exceptions['Dvd'] = 'DVD';
    $exceptions['Usb'] = 'USB';
    $exceptions['Mm'] = 'mm';
    $exceptions['Cm'] = 'cm';
    //    etc.

    $separator = array(" ","-","+");

    $str = mb_strtolower(trim($str));
    foreach($separator as $s){
        $word = explode($s, $str);

        $return = "";
        foreach ($word as $val){
            $return .= $s . mb_strtoupper($val{0}) . mb_substr($val,1,mb_strlen($val)-1);
        }
        $str = mb_substr($return, 1);
    }

    foreach($exceptions as $find=>$replace){
        if (mb_strpos($return, $find) !== false){
            $return = str_replace($find, $replace, $return);
        }
    }
    return mb_substr($return, 1);
}
Anonymous 16-Dec-2007 12:09
Function to do what ucwords is intended to do - just correctly also for international char sets:

function mb_ucwords($s)
{
    $s = mb_strtolower(trim($s));
    $w = explode(" ", $s);

    $return = "";
    foreach ($w as $val)
    {
        $return .= " " . mb_strtoupper($val{0}) . substr($val,1,strlen($val)-1);
    }
    return trim($return);
}

Building on an earlier snippet here.
kendsnyder at gmail dot com 30-Jul-2007 10:02
Here is a function to capitalize a last name, accounting for hyphens, apostrophes, "Mc" and "Mac":

<?php
function CapitalizeLastName($name) {
    $name = strtolower($name);
    $name = join("'", array_map('ucwords', explode("'", $name)));
    $name = join("-", array_map('ucwords', explode("-", $name)));
    $name = join("Mac", array_map('ucwords', explode("Mac", $name)));
    $name = join("Mc", array_map('ucwords', explode("Mc", $name)));
    return $name;
}
?>

I speed tested it against functions that used preg_replace() with an "e" modifier, preg_replace_callback(), and a character-by-character parsing.  Unexpectedly, this function using join(), array_map() and explode() was fastest.
Q1712 at online dot ms 05-May-2007 09:49
ucwords() only excepts whitespace in front of a word, although some chars like '"' or '(' normally have no space between them and the following word:
<?php
$title = 'ELVIS "THE KING" PRESLEY - (LET ME BE YOUR) TEDDY BEAR';
echo ucwords(strtolower($title));
?>
prints: Elvis "the King" Presley - (let Me Be Your) Teddy Bear

To avoid this i use a small function adding and deleting blanks behind these chars, and using ucwords() in between:

<?php
function my_ucwords($string)
  {
    $noletters='"([/'; //add more if u need to
    for($i=0; $i<strlen($noletters); $i++)
      $string = str_replace($noletters[$i], $noletters[$i].' ', $string);
    $string=ucwords($string);
    for($i=0; $i<strlen($noletters); $i++)
      $string = str_replace($noletters[$i].' ', $noletters[$i], $string);
    return $string;
  }

$title = 'ELVIS "THE KING" PRESLEY - (LET ME BE YOUR) TEDDY BEAR';
echo my_ucwords(strtolower($title));
?>

prints: Elvis "The King" Presley - (Let Me Be Your) Teddy Bear
barnaby ritchley at exeye dot co dot uk 03-Jan-2007 05:53
A very easy way to convert to title case:

function titleCase($string)
     {
     return ucwords(strtolower($string));
     }

$myString = "SOME TEXT";

echo titleCase($myString);

//will print, "My Text"
max at phoenixweb dot it 15-Sep-2006 04:35
I have rewritten a UCSMART function adding a feature to translate special ASCII char (windows occidental ascii charset):

You can edit/add/delete char by use the first two string (be carefull to preserve the order of the string).

Enjoy!

<?
$ASCII_SPC_MIN = "aaaaaaæceeeeiiiiðnooooouuuuyy??";
$ASCII_SPC_MAX = "AAAAAAÆCEEEEIIIIÐNOOOOOUUUUY???";

function str2upper($text) {
    global $ASCII_SPC_MIN,$ASCII_SPC_MAX;
    return strtr(strtoupper($text),$ASCII_SPC_MIN,$ASCII_SPC_MAX);
}
function str2lower($text) {
    global $ASCII_SPC_MIN,$ASCII_SPC_MAX;
    return strtr(strtolower($text),$ASCII_SPC_MAX,$ASCII_SPC_MIN);
}
function ucsmart($text) {
    global $ASCII_SPC_MIN;
    return preg_replace(
        '/([^a-z'.$ASCII_SPC_MIN.']|^)([a-z'.$ASCII_SPC_MIN.'])/e',
        '"$1".str2upper("$2")',
        str2lower($text)
    );
}
?>

Massimiliano Cuttini
marco at hotelsandgo dot com 09-Sep-2006 06:35
ucwords that works also with apices: '
for example: "aquila d'abruzzo" became "Aquila d'Abruzzo"
The second part after the // comment can removed! Is optimized for italian language (leave lowercase articles, prepositions, conjunctions) but easily changeable. Notice che "'s"!! for example "hotel 2000's" becames "Hotel 2000's".

    function my_ucwords($s)
        {
        $a=strtolower($s);
        $s=ucfirst($a);
        for($x=0; $x<strlen($s)-1; $x++)
            if(!ctype_alpha($s[$x])) $s[$x+1]=strtoupper($s[$x+1]);

        //Lascia minuscoli articoli, preposizioni, congiunzioni
        $minuscole=array("il", "lo", "la", "i", "gli", "le",                //ARTICOLI DETERMINATIVI
                 "un", "uno", "una",                        //ARTICOLI INDETERMINATIVI
                 "e",  "d", "l", "s", "un",                    //CONGIUNZIONI e CONTRATTI
                 "di", "a", "da", "in", "con", "su", "per", "tra", "fra",    //PREPOSIZIONI SEMPLICI
                 "del", "dello", "della", "dei", "degli", "delle",        //PREPOSIZIONI ARTICOLATE
                 "a", "al", "allo", "alla", "ai", "agli", "alle",
                 "da", "dal", "dallo", "dalla", "dai", "dagli", "dalle",
                 "in", "nel", "nello", "nella", "nei", "negli", "nelle",
                 "con", "col", "collo", "colla", "coi", "cogli", "colle",
                 "su", "sul", "sullo", "sulla", "sui", "sugli", "sulle",
                 "per", "pel", "pei");

        foreach($minuscole as $value)
            {
            $pos=strpos($a, $value);
            if( ( $pos>0 && $pos<strlen($s)-1 && !ctype_alpha($a[$pos-1]) && !ctype_alpha($a[$pos+1]) )    //CARATTERE IN MEZZO
            ||  ( $pos==strlen($s)-1 && !ctype_alpha($a[$pos-1]) ) )                    //CASO PARTICOLARE: carattere in fondo. x es: "hotel 2000's"
                $s[$pos]=strtolower($s[$pos]);
            }

        return $s;
        }

    function my_ucwords_essential($s)
        {
        $a=strtolower($s);
        $s=ucfirst($a);
        for($x=0; $x<strlen($s)-1; $x++)
            if(!ctype_alpha($s[$x])) $s[$x+1]=strtoupper($s[$x+1]);

        return $s;
        }

Can be rewritten better, I Know
philip at fcknet dot dk 08-Aug-2006 02:11
Before noticing this function I made the following function (that does the same as ucwords):

<?php
function firstUpper($string)
{
    $string = str_replace(array("Æ","Ø","A"), array("æ","ø","a"), strtolower($string));
    $ord = explode(" ", $string);

    $return = "";
    foreach ($ord as $val)
    {

        $return .= " " . str_replace(array("æ","ø","a"), array("Æ","Ø","A"), strtoupper($val{0})) . substr($val,1,strlen($val)-1);
    }
    return $return;
}
?>

It also converts Danish letters without using the setlocale function.
starmonkey [at] evolove [dot] net 05-Aug-2006 05:17
Simple helper function to walk through a nested array of strings and upper case them:

<?php
/**
 * Helper function to convert an array of strings to upper case words
 */
function _capitalize($input) {
    if(is_array($input)) {
        // recurse through array elements (using a reference)
        foreach($input as &$value) {
            $value = _capitalize($value);
        }
        return $input;
    } elseif(is_string($input)) {
        // process this string
        return ucwords($input);
    } else {
        // all other data types, leave alone
        return $input;
    }
}
?>
Ismet Togay 31-May-2006 02:07
Response to arif:

We do not need that long functions. In order to make ucwords() worked properly in Turkish words that contain speacial characters, we can use the following command in our php codes:

setlocale(LC_ALL, 'tr_TR');

This will set locale to Turkish.
lev at phpfox dot com 06-May-2006 10:44
In the function ucsmart() posted by ieure at php dot net on 04-Dec-2005 11:57, I found a similar problem in this function to what he found in igua's.

<?php
function ucsmart($text)
{
   return preg_replace('/([^a-z]|^)([a-z])/e', '"$1".strtoupper("$2")',
                       strtolower($text));
}
?>

"igua's code adds a backslash in front of the first single quote for me. This doesn't alter the content in any way other than changing case."

Actually, it did end up changing the content for me (php 5.0.4) in the way that this function escapes a single quotation (apostrophe) in the MIDDLE of a word.

For example:

who's online?

Became:

Who\'s Online?

The fix is simple however, and merely requires fine-tuning the regular expression:

<?php
function ucsmart($text)
{
   return preg_replace('/([^a-z\']|^)([a-z])/e', '"$1".strtoupper("$2")',
                       strtolower($text));
}
?>

(note: while previewing this note before adding it, I am noticing php's website is not correctly displaying the change I made as I wrote it. After the first a-z in the expression, the single quotation should be escaped... If it isn't you will get a parse error! And apoligies if my text here is colored as php code; not my fault!)

This will not escape a single quotation mark which occurs in the middle of a word... Though, you may find that might need to add other characters inside the regular expression if you use other special characters inside your words and if you get funky output.

It's a great expression though! Simple, yet very powerful. Kudos!
24-Dec-2005 04:34
"ieure at php dot net", your idea is pure poetry!

The function below will standardize the capitalization on people's names and the titles of reports and essays . You may need to adapt the lists in "$all_uppercase" and "$all_lowercase" to suit the data that you are working with.

function my_ucwords($str, $is_name=false) {
   // exceptions to standard case conversion
   if ($is_name) {
       $all_uppercase = '';
       $all_lowercase = 'De La|De Las|Der|Van De|Van Der|Vit De|Von|Or|And';
   } else {
       // addresses, essay titles ... and anything else
       $all_uppercase = 'Po|Rr|Se|Sw|Ne|Nw';
       $all_lowercase = 'A|And|As|By|In|Of|Or|To';
   }
   $prefixes = 'Mc';
   $suffixes = "'S";

   // captialize all first letters
   $str = preg_replace('/\\b(\\w)/e', 'strtoupper("$1")', strtolower(trim($str)));

   if ($all_uppercase) {
       // capitalize acronymns and initialisms e.g. PHP
       $str = preg_replace("/\\b($all_uppercase)\\b/e", 'strtoupper("$1")', $str);
   }
   if ($all_lowercase) {
       // decapitalize short words e.g. and
       if ($is_name) {
           // all occurences will be changed to lowercase
           $str = preg_replace("/\\b($all_lowercase)\\b/e", 'strtolower("$1")', $str);
       } else {
           // first and last word will not be changed to lower case (i.e. titles)
           $str = preg_replace("/(?<=\\W)($all_lowercase)(?=\\W)/e", 'strtolower("$1")', $str);
       }
   }
   if ($prefixes) {
       // capitalize letter after certain name prefixes e.g 'Mc'
       $str = preg_replace("/\\b($prefixes)(\\w)/e", '"$1".strtoupper("$2")', $str);
   }
   if ($suffixes) {
       // decapitalize certain word suffixes e.g. 's
       $str = preg_replace("/(\\w)($suffixes)\\b/e", '"$1".strtolower("$2")', $str);
   }
   return $str;
}

// A name example
print my_ucwords("MARIE-LOU VAN DER PLANCK-ST.JOHN", true);
// Output: Marie-Lou van der Planc-St.John

// A title example
print my_ucwords("to be or not to be");
// Output: "To Be or Not to Be"
ieure at php dot net 04-Dec-2005 11:57
Whoa guys, tone things down a bit here. No need to loop and implode. This is a one-line solution:

function ucsmart($text)
{
    return preg_replace('/([^a-z]|^)([a-z])/e', '"$1".strtoupper("$2")',
                        strtolower($text));
}

igua's code adds a backslash in front of the first single quote for me. This doesn't alter the content in any way other than changing case.
gothicbunny at hotmail dot com 09-Nov-2005 01:16
Here is a simple, yet winded, opposite to ucwords.

<?php
/*
    # lcwords v1.000
    # Convert the first word character to lowercase (opposite to ucwords)
    # input string
    # return string
*/
function lcwords($string)
{
    /* Some temporary variables */
    #loop variable
    $a = 0;
    #store all words in this array to be imploded and returned
    $string_new = array();
    #create array of all words
    $string_exp = explode(" ",$string);
    foreach($string_exp as $astring)
    {
        for($a=0;$a<strlen($astring);$a++)
        {
            #check that the character we are at {pos $a} is a word
            #i.e. if the word was !A the code would fail at !
            #then loop to the next character and succeed at A
            #check at character position $a
            if(preg_match("'\w'",$astring[$a]))
            {
                $astring[$a] = strtolower($astring[$a]);
                #end the loop
                break;
            }
        }
        $string_new[] = $astring;
    }
    #recreate the string from array components using space deliminator
    return implode(" ",$string_new);
}
?>

Of course a simplier way would be to use a callback, but I like working with long code :)
21-Oct-2005 01:14
Here's a  piece that allows you to use the contents of a directory..  capitalizes the words and make links.. this particular example splits file names at _ and only selects file with .htm extensions (thought you could use any extension and call it using  include()  or soom such)
ie my_file_name.htm will produce
<a href="my_file_name.htm">My File Name</a>

<?php
$path = "/home/path/to/your/directory";
   $mydir = dir($path);
   while(($file = $mydir->read()) !== false) {
     if(substr($file, -4)=='.htm'){
    $trans = array("_" => " ", ".htm" => ""); // creates the editing array
    $newlist = strtr($file, $trans); // edits using editing array
    echo "<a href=\"".$file."\">".ucwords($newlist)."</a><br>";
     }
    }
   ?>
radley25 at nospam dot spamcop dot net 05-Jul-2005 05:06
In response to joshuamallory at yahoo dot com:

Using CSS to fix a PHP fault is not the ideal way to solve a problem. CSS is browser dependent and can only be used when the data is presented in a web page. A better fix would be something like this:

<?php
function better_ucwords($string) {
   $string = ucwords($string);
   $string = preg_replace('#[\\/][a-z]#e', "strtoupper('$0')", $string);
   return $string;
}
?>
igua no-spam at coveruniverse dot com 08-Mar-2005 01:30
The code posted by neil doesn't fully do what is wanted. Try adding some more question marks at the end and it will return a not wanted string.

Below code will uppercase all your words regardless of the delimiter.

<?php
$text = "What?No 'delimiters',shit \"happens\" here.this solves all problems???";
preg_match_all('/[A-Za-z]+|[^A-Za-z]+/', $text, $data);
for ($i = 0; $i < count($data[0]); $i++) {
  $data[0][$i] = ucfirst($data[0][$i]);
}
$text = implode("", $data[0]);
print $text;
?>
joshuamallory at yahoo dot com 15-Nov-2004 05:08
If you want to format a string like...

<?php
    $string = "computer programming/repair";
    print ucwords($string);
?>

Output: Computer Programming/repair

Notice the word after the slash (Programming/repair) isn't capitalized. To fix this, use CSS...

<?php
    $string = "computer programming/repair";
    print '<p style="text-transform:capitalize">';
    print ucwords($string);
    print '<p>';
?>
babel - nospamplease - sympatico - ca 11-Feb-2004 05:26
Correction to the code of firewire at itsyourdomain dot com:

preg_replace_callback('/\b(\w)(\w+)?/',
   create_function('$a',
   'return strtoupper($a[1]) . ((sizeof($a) > 2 ) ?
       strtolower($a[2]) : "");'),
    'p.s.: hello.this is my string.');

Will work with punctuation as well as spaces.
deepdene at email dot com 10-Dec-2002 08:20
A function knowing about name case (i.e. caps on McDonald etc)

function name_case($name)
{
    $newname = strtoupper($name[0]);
    for ($i=1; $i < strlen($name); $i++)
    {
        $subed = substr($name, $i, 1);
        if (((ord($subed) > 64) && (ord($subed) < 123)) ||
            ((ord($subed) > 48) && (ord($subed) < 58)))
        {
            $word_check = substr($name, $i - 2, 2);
            if (!strcasecmp($word_check, 'Mc') || !strcasecmp($word_check, "O'"))
            {
                $newname .= strtoupper($subed);
            }
            else if ($break)
            {

                $newname .= strtoupper($subed);
            }
            else
            {
                $newname .= strtolower($subed);
            }
             $break=0;
        }
        else
        {
            // not a letter - a boundary
             $newname .= $subed;
            $break=1;
        }
    }
    return $newname;
}
19-Jan-2002 03:14
This seems to be what people want:

function uc_all($string) {
    $temp = preg_split('/(\W)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
    foreach ($temp as $key=>$word) {
        $temp[$key] = ucfirst(strtolower($word));
    }
    return join ('', $temp);
}

[ed note: fixed the code to be correct]
Julienc at psychologie-fr dot com 04-Nov-2001 04:06
Its still possible to clean a bit more the previous sample:

$string=strtolower($string); $break=true;
for ($i=0; $i < strlen($string); $i++) { $subed=$string[$i];
if (((ord($subed) > 64) && (ord($subed) < 123)) || ((ord($subed) > 48) && (ord($subed) < 58))) {
if ($break) { $string[$i] = strtoupper($subed); }
$break=false; } else { $break=true; }
}

- Julien
neil at no-spam-ents24 dot com 21-Mar-2001 02:10
The code posted above by Joerg Krause only works for a string which ends with one of the delimiters. A possible fix is:

<?php
$text = "What?No delimiters,shit happens here.this solves all problems.";
preg_match_all("/(\w+[,. ?])+/U", $text, $words);
preg_match("/(\w+)$/", $text, $lastword);
$words[0][] = $lastword;
foreach($words[0] as $part) $uwords[] = ucfirst($part);
$text = implode("", $uwords);
echo $text;
?>


Write a formatted string to a stream

  • ≪ ucwords
  • vprintf ≫
  • PHP Manual
  • String 함수 목록
  • Write a formatted string to a stream

vfprintf

(PHP 5, PHP 7)

vfprintf — Write a formatted string to a stream

설명

int vfprintf ( resource $handle , string $format , array $args )

Write a string produced according to format to the stream resource specified by handle.

Operates as fprintf() but accepts an array of arguments, rather than a variable number of arguments.

인수

handle

format

See sprintf() for a description of format.

args

반환값

Returns the length of the outputted string.

예제

Example #1 vfprintf(): zero-padded integers

<?php
if (!($fp = fopen('date.txt', 'w')))
    return;

vfprintf($fp, "%04d-%02d-%02d", array($year, $month, $day));
// will write the formatted ISO date to date.txt
?>

참고

  • printf() - 형식화한 문자열을 출력
  • sprintf() - 형식화한 문자열을 반환
  • sscanf() - 문자열을 형식에 따라 해석
  • fscanf() - Parses input from a file according to a format
  • vsprintf() - Return a formatted string
  • number_format() - Format a number with grouped thousands

User Contributed Notes

There are no user contributed notes for this page.


Output a formatted string

  • ≪ vfprintf
  • vsprintf ≫
  • PHP Manual
  • String 함수 목록
  • Output a formatted string

vprintf

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

vprintf — Output a formatted string

설명

int vprintf ( string $format , array $args )

Display array values as a formatted string according to format (which is described in the documentation for sprintf()).

Operates as printf() but accepts an array of arguments, rather than a variable number of arguments.

인수

format

See sprintf() for a description of format.

args

반환값

Returns the length of the outputted string.

예제

Example #1 vprintf(): zero-padded integers

<?php
vprintf("%04d-%02d-%02d", explode('-', '1988-8-1')); // 1988-08-01
?>

참고

  • printf() - 형식화한 문자열을 출력
  • sprintf() - 형식화한 문자열을 반환
  • vsprintf() - Return a formatted string

User Contributed Notes

steve at stevelockwood dot net 19-Dec-2014 05:42
If, instead of an array, you pass an object PHP will automatically cast the object as an array so you can use it directly in vprintf.
<?php
 $object = new stdClass();
 $object->Property1 = 'Value 1';
 $object->Property2 = 'Value 2';
vprintf('%-20s %-20s', $object);

/* will output
 Value 1              Value 2
*/
?>
Chris 19-Oct-2012 02:49
Another way to display arrays is use an array_walk(). This can be useful inline echo/print where a foreach wouldn't work, e.g.

<?php
echo "These errors: ", (unset)array_walk($msgs, function($a) { echo "<p>$a</p>"; } ), "must be corrected.";
?>
taken from &#34;Php Phrasebook&#34; 16-Jun-2008 07:55
<?php
$string = 'The site runs on PHP '.phpversion();
preg_match('/php ((\d)\.\d\.\d+)/i',$string,$matches);
print_r($matches);
vprintf('Match: %s<br /> Version %s; Major:%d.',$matches);
?>

output:
Array ( [0] => PHP 5.2.5 [1] => 5.2.5 [2] => 5 )
Match: PHP 5.2.5 Version 5.2.5; Major:5.

For preg_match:

If matches  is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1]  will have the text that matched the first captured parenthesized subpattern, and so on.
badcop666 at hotmail dot com 29-Nov-2007 02:38
For blocks of text, sprintf() is slow according to my tests.

Also, having the mapping between place-holders and the list of actual variables or datastructures often makes this code difficult to read. But the printf() family are widely supported and have a huge range of nice features. Performance is a cold mistress though!

From an ease-of-reading and maintenance, debugging point of view, I much prefer HEREDOC and "...{$variable}..." methods.

For a block of HTML markup with place holders, the fastest by far was:-

?>
<div> markup etc<?= $variable ?>more markup
<?

My tests comprised 20 runs of a loop of 1 million iterations with output buffering, ditching the buffer on each loop.

The timings ranged from average 2.1msec/million repetitions for the <?= $var ?> method up to 7.6msec/million using printf().

I'll try some benchmarking tools too, since I just wrote this myself and it could be introducing bias, but they've run on dev servers with low load.

Hopefully interesting.
tehjosh at gamingg dot net 30-Jul-2007 04:19
To toolofthesystem at gmail dot com:

You don't need to use output buffering with vprintf() because you can use vsprintf(), which has the same functionality as vprintf(), except that it returns the resulting string instead of outputting it.
toolofthesystem at gmail dot com 27-Mar-2007 05:42
This function comes useful sometimes when trying to list information returned from MySQL:

function print_sql($query,$printf){
    $sql_sql = mysql_query($query);
    while($sql = mysql_fetch_row($sql_sql)){
        vprintf($printf,$sql);
    }
}

Unfortunately, this seems to sneak its way past output buffering when I tried creating an argument to allow it to be contained in a returned string... either that or I didn't do it right.
caleb at tekhawk dot com 29-Nov-2006 11:19
i know that you can use %1$s or %3$s to select the first or third string but how can you or can you use array names to select it

something like %'user'$s $'email'$s

i tend to add things to my databases over time and this could save loads of recoding


Return a formatted string

  • ≪ vprintf
  • wordwrap ≫
  • PHP Manual
  • String 함수 목록
  • Return a formatted string

vsprintf

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

vsprintf — Return a formatted string

설명

string vsprintf ( string $format , array $args )

Operates as sprintf() but accepts an array of arguments, rather than a variable number of arguments.

인수

format

See sprintf() for a description of format.

args

반환값

Return array values as a formatted string according to format (which is described in the documentation for sprintf()).

예제

Example #1 vsprintf(): zero-padded integers

<?php
print vsprintf("%04d-%02d-%02d", explode('-', '1988-8-1')); // 1988-08-01
?>

참고

  • sprintf() - 형식화한 문자열을 반환
  • vprintf() - Output a formatted string

User Contributed Notes

spectrumcat 28-Sep-2016 07:51
Instead of inventing own functions in case you'd like to use array keys as placeholder names and replace corresponding array values in a string, just use the str_replace:

$string = 'Hello %name!';
$data = array(
  '%name' => 'John'
);

$greeting = str_replace(array_keys($data), array_values($data), $string);
dee jay simple zero07 at geemail dawt co 18-Jan-2013 02:03
Using a heredoc with vprintf:

<?php
$string = <<<THESTRING
I like the state of %1\$s <br />
I picked: %2\$d as a number, <br />
I also picked %2\$d as a number again <br />
%3\$s<br />
THESTRING;

$returnText = vprintf(  $string, array('Oregon','7','I Love Oregon')  );

echo $returnText;
?>
Josef Kufner 21-Nov-2012 08:51
<?php
/**
 * Like vsprintf, but accepts $args keys instead of order index.
 * Both numeric and strings matching /[a-zA-Z0-9_-]+/ are allowed.
 *
 * Example: vskprintf('y = %y$d, x = %x$1.1f', array('x' => 1, 'y' => 2))
 * Result:  'y = 2, x = 1.0'
 *
 * $args also can be object, then it's properties are retrieved
 * using get_object_vars().
 *
 * '%s' without argument name works fine too. Everything vsprintf() can do
 * is supported.
 *
 * @author Josef Kufner <jkufner(at)gmail.com>
 */
function vksprintf($str, $args)
{
    if (is_object($args)) {
        $args = get_object_vars($args);
    }
    $map = array_flip(array_keys($args));
    $new_str = preg_replace_callback('/(^|[^%])%([a-zA-Z0-9_-]+)\$/',
            function($m) use ($map) { return $m[1].'%'.($map[$m[2]] + 1).'$'; },
            $str);
    return vsprintf($new_str, $args);
}
?>
thomas att familie dash flori dot de 31-Oct-2012 08:46
Here is my example for named placeholders. It uses python like named placeholders except that it only allows /[a-z][a-zA-Z0-9_]/ for names.

<?php

/**
 * Returns a formatted string. Accepts named arguments.
 * @param string $format
 * @param array $args
 * @return string
 **/
function vsprintfn($format, $args) {
    // search format patterns
    preg_match_all('/((?:^|[^%])(?:%%)*)%(\([a-z][a-zA-Z0-9_]*\))?((\+|-)?(0| |\'.)?-?[0-9\.]*[bcdeEufFgGosxX])/', $format, $matches);
    // determine the order of the arguments
    $j = 0;
    $order = array();
    foreach ($matches[0] as $i => $match) {
        if ($matches[2][$i] == '') {
            $key = $j++;
        } else {
            $key = substr($matches[2][$i],1,-1);
        }
        $order[] = $key;
    }
    // prepare the data array for vsprintf in the given order
    $data = array();
    foreach ($order as $key) {
        if (isset($args[$key])) {
            $data[] = $args[$key];
        }
    }
    // replace named format patterns with default format patterns
    $format = preg_replace('/((?:^|[^%])(?:%%)*)%(\([a-z][a-zA-Z0-9_]*\))((\+|-)?(0| |\'.)?-?[0-9\.]*[bcdeEufFgGosxX])/', '$1%$3', $format);
    // return formatted string
    return vsprintf($format, $data);
}

$exampleData = array(0=>2.2314123123,'test'=>2.1234883);
echo vsprintfn('%%2.5f = %2.5f', $exampleData) . "\n";
echo vsprintfn('%%(test)09.5f = %(test)\'%9.5f', $exampleData) . "\n";

?>
strata_ranger at hotmail dot com 29-Jun-2010 08:15
Here's a simple variation on vsprintf() suitable for use with database queries where the results were retrieved as an associative array keyed by column names.

<?php

function dbsprintf($format, $fields, $row)
// $format - sprintf() compatible format string
// $row - Array containing key/value pairs of data
// $fields - Array containing key names (from $row) that are to be used as arguments
{
  // Loop through $fields and insert the corresponding values from $row
  foreach($fields as &$value)
  {
    $value = $row[$value];
  } unset($value);

  // Format the string and return
  return vsprintf($format, $fields);
}

// Some data
$row = Array('id' => '12', 'name' => 'World');

// Outputs "Hello World!"
echo dbsprintf('Hello, %s!', Array('name'), $row);
// Outputs "Hello 12!"
echo dbsprintf('Hello, %s!', Array('id'), $row);

?>
jeppe dot dyrby at gmail dot com 04-Mar-2009 01:52
Heres a new version of the vnsprintf function, i call it dsprintf, but that should matter.

<?php
function dsprintf() {
  $data = func_get_args(); // get all the arguments
  $string = array_shift($data); // the string is the first one
  if (is_array(func_get_arg(1))) { // if the second one is an array, use that
    $data = func_get_arg(1);
  }
  $used_keys = array();
  // get the matches, and feed them to our function
  $string = preg_replace('/\%\((.*?)\)(.)/e',
    'dsprintfMatch(\'$1\',\'$2\',\$data,$used_keys)',$string);
  $data = array_diff_key($data,$used_keys); // diff the data with the used_keys
  return vsprintf($string,$data); // yeah!
}

function dsprintfMatch($m1,$m2,&$data,&$used_keys) {
  if (isset($data[$m1])) { // if the key is there
    $str = $data[$m1];
    $used_keys[$m1] = $m1; // dont unset it, it can be used multiple times
    return sprintf("%".$m2,$str); // sprintf the string, so %s, or %d works like it should
  } else {
    return "%".$m2; // else, return a regular %s, or %d or whatever is used
  }
}
$str = "Hello, %(place)s, how is it hanning at %(place)s? %s works just as well";
$find = array(
  'place' => 'world',
  'sprintf',
  'not used'
);
echo dsprintf($str, $find);
// 'Hello, world, how is it hanning at world? sprintf works just as well'
?>
www dot wesley at gmail dot com 16-Jun-2008 10:56
vnsprintf is equal to vsprintf except for associative, signed or floating keys.

vnsprintf supports for example "%assocKey$05d", "%-2$'+10s" and "%3.2$05u", vsprintf doesn't

vnsprintf( '%2$d', $array) [2nd value] is equal to vsprintf( '%2$d', $array) [2nd value]
vnsprintf( '%+2$d', $array) [key = 2] is equal to vnsprintf( '%2.0$d', $array) [key = 2]
vnsprintf( '%+2$d', $array) [key = 2] is different of vsprintf( '%+2$d', $array) [unsupported]

When you use signed or floating keys, vnsprintf searchs for the signed truncated key of the original array

Note¹: vnsprintf does not support for example "%someKeyf" (floating number, key = someKey) or "%+03d" (signed decimal number, key = 3), you should use "%someKey$f" or "%+03$d" respectively.
Note²: "%+03d" (or "%1$+03d") will be interpreted as signed zero-padded decimal number

<?php
function vnsprintf( $format, array $data)
{
    preg_match_all( '/ (?<!%) % ( (?: [[:alpha:]_-][[:alnum:]_-]* | ([-+])? [0-9]+ (?(2) (?:\.[0-9]+)? | \.[0-9]+ ) ) ) \$ [-+]? \'? .? -? [0-9]* (\.[0-9]+)? \w/x', $format, $match, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
    $offset = 0;
    $keys = array_keys($data);
    foreach ( $match as &$value )
    {
        if ( ( $key = array_search( $value[1][0], $keys) ) !== FALSE || ( is_numeric( $value[1][0]) && ( $key = array_search( (int)$value[1][0], $keys) ) !== FALSE ) ) {
            $len = strlen( $value[1][0]);
            $format = substr_replace( $format, 1 + $key, $offset + $value[1][1], $len);
            $offset -= $len - strlen( $key);
        }
    }
    return vsprintf( $format, $data);
}

$examples = array(
    2.8=>'positiveFloat',    // key = 2 , 1st value
    -3=>'negativeInteger',    // key = -3 , 2nd value
    'my_name'=>'someString'    // key = my_name , 3rd value
);

echo vsprintf( "%%my_name\$s = '%my_name\$s'\n", $examples);    // [unsupported]
echo vnsprintf( "%%my_name\$s = '%my_name\$s'\n", $examples);    // output : "someString"

echo vsprintf( "%%2.5\$s = '%2.5\$s'\n", $examples);        // [unsupported]
echo vnsprintf( "%%2.5\$s = '%2.5\$s'\n", $examples);        // output : "positiveFloat"

echo vsprintf( "%%+2.5\$s = '%+2.5\$s'\n", $examples);        // [unsupported]
echo vnsprintf( "%%+2.5\$s = '%+2.5\$s'\n", $examples);        // output : "positiveFloat"

echo vsprintf( "%%-3.2\$s = '%-3.2\$s'\n", $examples);        // [unsupported]
echo vnsprintf( "%%-3.2\$s = '%-3.2\$s'\n", $examples);        // output : "negativeInteger"

echo vsprintf( "%%2\$s = '%2\$s'\n", $examples);            // output : "negativeInteger"
echo vnsprintf( "%%2\$s = '%2\$s'\n", $examples);            // output : [= vsprintf]

echo vsprintf( "%%+2\$s = '%+2\$s'\n", $examples);        // [unsupported]
echo vnsprintf( "%%+2\$s = '%+2\$s'\n", $examples);        // output : "positiveFloat"

echo vsprintf( "%%-3\$s = '%-3\$s'\n", $examples);        // [unsupported]
echo vnsprintf( "%%-3\$s = '%-3\$s'\n", $examples);        // output : "negativeInteger"
?>
Roadster 01-Oct-2006 04:44
Please note: The same functionality (sortof) can be attained between version 4.0.4 and 4.1.0 using call_user_func_array.

Example:

call_user_func_array("sprintf", $arg)

First element of $arg is the format. This rescued me in a situation where version 4.1.0 wasn't available.
jon at ardentcreative dot co dot uk 17-Feb-2006 12:56
This can be used for quick and dirty internationalization:

<?php
$GLOBALS['strings']['example'] = "There are %d people.";

// Loads a phrase from the translations list in lang/$lang/phrases.php
function t() {
    $args = func_get_args();
    $nArgs = func_num_args();

    $phrase = array_shift($args);
    $nArgs--;

    include_once("../lang/" . lang() . "/phrases.php");
    if (isset($GLOBALS['strings'][$phrase])) {
        return vsprintf($GLOBALS['strings'][$phrase], $args);
    } else {
        return '<span style="color: #ff0000">Untranslated string: ' . $phrase . '</span>';
    }
}
?>
toneee at g mail dot com 19-Dec-2005 06:27
I found this function to be useful for formatting sql queries.

I do something like this:

function sql_build($template, $params = array()) {
  global $sql_templates;
  if (isset($sql_templates[$template])) {
    $sql = vsprintf($sql_templates[$template], $params);
    return $sql;
  }
  return false;
}

// Fetch list of contacts, for a given section id
$sql_templates['contacts_by_section'] = <<<ENDSQL
select
  id,
  name,
  email,
  address,
  photo_id
from
  contacts
where
  section_id = %d
ENDSQL;

You also give yourself an added layer of security on the sql due to the sprintf formatting. For example, using %d will not allow any sql injection for that parameter.
tbS dot P dot A dot M at S dot U dot K dot Staylorbarstow dot com 01-Mar-2005 08:49
Simple but useful routine:

<?php
function vsprintf_iter($fmt,$data) {
    if (!is_array($data)) return false;
    $ret = '';
    foreach ($data as $d) {
        $ret .= vsprintf($fmt,$d);
    }
    return $ret;
}
?>
jed at NOSPAM dot jed dot bz 23-May-2004 03:48
vsprintf() accepts arrays with any keys, so the array_shift() technique is unnecessary when writing a printf-type function. Any parameters you require are easily unset from the array you retrieve with func_get_args():

<?php

function mysprintf($format) {
    $args = func_get_args();
    unset($args[0]); /* get rid of "$format" */
    return vsprintf($format, $args);
}

/* I use this technique in production code as follows: */
function logf($target, $string) {
    $args = func_get_args();
    unset($args[0], $args[1]);
    fprintf($GLOBALS['config']['logtargets'][$target],
        "[%s] %s\n", date('H:i'), wordwrap(vsprintf($string, $args), 75, '\n\r '));
}

/* e.g.:
    logf(DEBUG, "Oops! %s", mysql_error());
 */

?>

array_shift() and other costly array operations aren't required, as far as I know. I could be wrong.
samviseNOSPAM at hobbitonNOSPAM dot it 02-Jul-2002 03:55
i wrote a short function that make use of vsprintf. It is useful in first coding/debugging of php scripts, because it is called like a simple printf and it display in italic (you can change it of course ;-) ) every debug messages, making it easy to remove them when your code is ready.

here goes :

<?php
function printd() {
        $debug_array=func_get_args();
        $debug_numargs=func_num_args();
        $debug_fmt=$debug_array[0];
        for ( $i = 0 ; $i < $debug_numargs ; $i++ )
                $debug_array[$i] = $debug_array[$i+1];
        $debug_text=vsprintf($debug_fmt, $debug_array);
        printf("--> debug <i>%s</i>\n",
                $debug_text);
}
?>


Wraps a string to a given number of characters

  • ≪ vsprintf
  • 변경점 ≫
  • PHP Manual
  • String 함수 목록
  • Wraps a string to a given number of characters

wordwrap

(PHP 4 >= 4.0.2, PHP 5, PHP 7)

wordwrap — Wraps a string to a given number of characters

설명

string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )

Wraps a string to a given number of characters using a string break character.

인수

str

The input string.

width

The number of characters at which the string will be wrapped.

break

The line is broken using the optional break parameter.

cut

If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example). When FALSE the function does not split the word even if the width is smaller than the word width.

반환값

Returns the given string wrapped at the specified length.

예제

Example #1 wordwrap() example

<?php
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");

echo $newtext;
?>

위 예제의 출력:

The quick brown fox<br />
jumped over the lazy<br />
dog.

Example #2 wordwrap() example

<?php
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", true);

echo "$newtext\n";
?>

위 예제의 출력:

A very
long
wooooooo
ooooord.

Example #3 wordwrap() example

<?php
$text = "A very long woooooooooooooooooord. and something";
$newtext = wordwrap($text, 8, "\n", false);

echo "$newtext\n";
?>

위 예제의 출력:

A very
long
woooooooooooooooooord.
and
something

참고

  • nl2br() - 문자열의 모든 줄바꿈 앞에 HTML 줄바꿈 태그를 삽입
  • chunk_split() - 문자열을 작은 조각으로 나눕니다

User Contributed Notes

simbiat at bk dot ru 08-Oct-2016 06:41
Some people use wordwrap as long text cutter. I have a slightly different approach for that. The function does not wrap the text, but returns the position where to cut. It preserves HTML tags (does not count them) and strips br tags and whitespaces at the end. I am looking into a way prevent HTML tags corruption which can happen at this time and also support BB tags like code and quote, so that they won't be counted as well. When I do, I'll post an updated version of the code.

<?php
function txtcut($string, $length) {
    $tag = false;
    $chars = 0;
    $position = 0;
    $split = str_split($string);
    foreach ($split as $char) {
        $position++;
        if ($char == "<") {
            $tag = true;
            continue;
        }
        if ($char == ">") {
            $tag = false;
            continue;
        }
        if ($tag == true) {
            continue;
        }
        $chars++;
        if ($chars >= $length) {
            if ($char == " " || $char == "\r" || $char == "\n") {
                $position--;
                break;
            }
        }
    }
    if ($position == strlen($string)) {
        return strlen($string);
    } else {
        $string = substr($string, 0, $position);
        if (substr($string, -4, 4) == "<br>") {
            $string = rtrim(substr($string, 0, -4));
        }
        return strlen($string);
    }
}
?>
kozimbek at mail dot ru 02-May-2015 10:17
After searching and being tired of many non-working mb_wordwrap functions at many places, I finally created a really simple and working solution

<?php
function mb_wordwrap($string, $limit)
{
    $string = strip_tags($string); //Strip HTML tags off the text
    $string = html_entity_decode($string); //Convert HTML special chars into normal text
    $string = str_replace(array("\r", "\n"), "", $string); //Also cut line breaks
    if(mb_strlen($string, "UTF-8") <= $limit) return $string; //If input string's length is no more than cut length, return untouched
    $last_space = mb_strrpos(mb_substr($string, 0, $limit, "UTF-8"), " ", 0, "UTF-8"); //Find the last space symbol position

    return mb_substr($string, 0, $last_space, "UTF-8").' ...'; //Return the string's length substracted till the last space and add three points
}
?>

The function simply searches the last space symbol in the range and returns the string cut till that position. No iterations, no regular expressions and no buffer overload. Tested with large Russian texts and works perfectly.
zac dot hester at gmail dot com 12-Mar-2015 11:28
I recently ran into the issue discussed by another contributor to this function (frans-jan at van-steenbeek dot R-E-M-O-V-E dot net).  The problem appeared to be how wordwrap() was treating white space.  Instead of writing my own version of wordwrap(), I discovered that the "break" parameter is not only used as the inserted string, but also used to detect the existing wrap delimiters (e.g. line endings).  If you can manage to "normalize" the wrap delimiters in your original string, you don't need to try to work-around the function wrapping at seemingly odd places (like immediately after one short word).  As one quick-and-dirty way to get wordwrap() to play nicely with most use-cases, I did this:

<?php
$break = strpos( $content, "\r" ) === false ? "\n" : "\r\n";
$content = wordwrap( $content, 78, $break );
?>

I also tend to normalize multi-line strings (if my OCD is acting up).  You would typically perform this conversion _before_ sending it off to wordwrap().

<?php
//quick and simple, but clobbers old-style Mac line-endings
$content = str_replace( "\r", '', $content );

//slower, but works with everything
$content = preg_replace( "/(\r\n|\r)/", "\n", $content );

//now, wordwrap() will behave exactly as expected
$content = wordwrap( $content, 78, "\n" );
?>
Alhadis 08-Jan-2015 09:30
For those interested in wrapping text to fit a width in *pixels* (instead of characters), you might find the following function useful; particularly for line-wrapping text over dynamically-generated images.

If a word is too long to squeeze into the available space, it'll hyphenate it as needed so it fits the container. This operates recursively, so ridiculously long words or names (e.g., URLs or this guy's signature - http://en.wikipedia.org/wiki/Wolfe+585,_Senior) will still keep getting broken off after they've passed the fourth or fifth lines, or whatever.

<?php

    /**
     * Wraps a string to a given number of pixels.
     *
     * This function operates in a similar fashion as PHP's native wordwrap function; however,
     * it calculates wrapping based on font and point-size, rather than character count. This
     * can generate more even wrapping for sentences with a consider number of thin characters.
     *
     * @static $mult;
     * @param string $text - Input string.
     * @param float $width - Width, in pixels, of the text's wrapping area.
     * @param float $size - Size of the font, expressed in pixels.
     * @param string $font - Path to the typeface to measure the text with.
     * @return string The original string with line-breaks manually inserted at detected wrapping points.
     */
    function pixel_word_wrap($text, $width, $size, $font){

        #    Passed a blank value? Bail early.
        if(!$text) return $text;

        #    Check if imagettfbbox is expecting font-size to be declared in points or pixels.
        static $mult;
        $mult    =    $mult ?: version_compare(GD_VERSION, '2.0', '>=') ? .75 : 1;

        #    Text already fits the designated space without wrapping.
        $box    =    imagettfbbox($size * $mult, 0, $font, $text);
        if($box[2] - $box[0] / $mult < $width)    return $text;

        #    Start measuring each line of our input and inject line-breaks when overflow's detected.
        $output        =    '';
        $length        =    0;

        $words        =    preg_split('/\b(?=\S)|(?=\s)/', $text);
        $word_count    =    count($words);
        for($i = 0; $i < $word_count; ++$i){

            #    Newline
            if(PHP_EOL === $words[$i])
                $length    =    0;

            #    Strip any leading tabs.
            if(!$length) $words[$i]    =    preg_replace('/^\t+/', '', $words[$i]);

            $box    =    imagettfbbox($size * $mult, 0, $font, $words[$i]);
            $m        =    $box[2] - $box[0] / $mult;

            #    This is one honkin' long word, so try to hyphenate it.
            if(($diff = $width - $m) <= 0){
                $diff    =    abs($diff);

                #    Figure out which end of the word to start measuring from. Saves a few extra cycles in an already heavy-duty function.
                if($diff - $width <= 0)    for($s = strlen($words[$i]); $s; --$s){
                    $box    =    imagettfbbox($size * $mult, 0, $font, substr($words[$i], 0, $s) . '-');
                    if($width > ($box[2] - $box[0] / $mult) + $size){
                        $breakpoint    =    $s;
                        break;
                    }
                }

                else{
                    $word_length    =    strlen($words[$i]);
                    for($s = 0; $s < $word_length; ++$s){
                        $box    =    imagettfbbox($size * $mult, 0, $font, substr($words[$i], 0, $s+1) . '-');
                        if($width < ($box[2] - $box[0] / $mult) + $size){
                            $breakpoint    =    $s;
                            break;
                        }
                    }
                }

                if($breakpoint){
                    $w_l    =    substr($words[$i], 0, $s+1) . '-';
                    $w_r    =    substr($words[$i],     $s+1);

                    $words[$i]    =    $w_l;
                    array_splice($words, $i+1, 0, $w_r);
                    ++$word_count;
                    $box    =    imagettfbbox($size * $mult, 0, $font, $w_l);
                    $m        =    $box[2] - $box[0] / $mult;
                }
            }

            #    If there's no more room on the current line to fit the next word, start a new line.
            if($length > 0 && $length + $m >= $width){
                $output    .=    PHP_EOL;
                $length    =    0;

                #    If the current word is just a space, don't bother. Skip (saves a weird-looking gap in the text).
                if(' ' === $words[$i]) continue;
            }

            #    Write another word and increase the total length of the current line.
            $output    .=    $words[$i];
            $length +=    $m;
        }

        return $output;
    };

?>
phil_marmotte at yahoo dot fr 13-Jun-2014 10:13
Another Word wrap from left or right :

public static function myWordWrap ($string, $length=3, $wrap=',', $from='left') {
        if ($from=='left') $txt=wordwrap($string, $length, $wrap, true);
            if ($from=='right') {
                $m = strlen($string)%$length;
                if ($m < strlen($string))
                    $txt = substr($string,0,$m).$wrap.wordwrap(substr($string,$m),$length, $wrap, true);
                else
                    $txt = $string;
            }

            return $txt;
       }
maikuolan at gmail dot com 11-Apr-2013 03:23
(Re: kouber at php dot net).

Testing out your function, I can confirm that it works, and it works very well.

However, others that intend to use your function need to be aware that if they use it in conjunction with unverified data (such as raw user input from $_POST, $_GET, etcetera), they are creating potential attack vectors that can be exploited by hackers via script requests containing malicious code. This is because your function is using the preg_replace function in conjunction with the "e" flag (in order to allow the chunk_split bit to execute), which can allow execution of arbitrary code.

Solution: If there is any possibility that $str may contain unverified data (such as raw user input), ensure that the contents of $str is sanitized (such as by using htmlentities/htmlspecialchars/etc) prior to sending it to wrap($str,...).

Not a criticism; I intend to use your function, because I like it. However, just posting this as a note to other users that may not be aware of the importance of data sanitation.
ytwguru at gmail dot com 02-May-2012 08:01
I've seen a few solutions to the same problem.  Here's a solution to breaking long text with a space.  In this case it breaks after matching 27 characters.

$content = preg_replace("/([^\s]{27})/", "$1 ", $content);

You can probably write a function for this...
ju1ius 18-Feb-2012 12:58
Another solution to utf-8 safe wordwrap, unsing regular expressions.
Pretty good performance and works in linear time.

<?php
function utf8_wordwrap($string, $width=75, $break="\n", $cut=false)
{
  if($cut) {
    // Match anything 1 to $width chars long followed by whitespace or EOS,
    // otherwise match anything $width chars long
    $search = '/(.{1,'.$width.'})(?:\s|$)|(.{'.$width.'})/uS';
    $replace = '$1$2'.$break;
  } else {
    // Anchor the beginning of the pattern with a lookahead
    // to avoid crazy backtracking when words are longer than $width
    $pattern = '/(?=\s)(.{1,'.$width.'})(?:\s|$)/uS';
    $replace = '$1'.$break;
  }
  return preg_replace($search, $replace, $string);
}
?>
Of course don't forget to use preg_quote on the $width and $break parameters if they come from untrusted input.
paul at preinheimer dot com 26-Sep-2011 01:04
If your goal is to allow a browser to word-wrap nicely, while maintaining a fluid layout, you'll want to break with: &#8203

It's a zero width space character: http://en.wikipedia.org/wiki/Zero-width_space

This way the browser can break where it needs to, without adding a visual distraction of spaces within the text.

e.g.
<?php
//Using &#8203; here allows the browser to break the line of text, without the visual distraction.
$line = wordwrap($line, 10, "&#8203;", true);
?>

#PROTIP: leave a comment, so the next person to read your code has a clue what you've done.
info at hsdn dot org 08-Jul-2011 05:41
Wordwrap with UTF-8 supports, returns as array.

<?php

function mb_wordwrap_array($string, $width)
{
    if (($len = mb_strlen($string, 'UTF-8')) <= $width)
    {
        return array($string);
    }

    $return = array();
    $last_space = FALSE;
    $i = 0;

    do
    {
        if (mb_substr($string, $i, 1, 'UTF-8') == ' ')
        {
            $last_space = $i;
        }

        if ($i > $width)
        {
            $last_space = ($last_space == 0) ? $width : $last_space;

            $return[] = trim(mb_substr($string, 0, $last_space, 'UTF-8'));
            $string = mb_substr($string, $last_space, $len, 'UTF-8');
            $len = mb_strlen($string, 'UTF-8');
            $i = 0;
        }

        $i++;
    }
    while ($i < $len);

    $return[] = trim($string);

    return $return;
}

?>
altin_bardhi at yahoo dot co dot uk 28-Jan-2011 01:03
Here I have come out with a possibly very useful wordwrap code snippet.

Apparently what this piece of code does is: it takes the entered text and looks for words longer than the defined '$chunk_length' if it finds any, it splits the long words and then it concatenates the whole string back to a new string with longer words separated by a dash character in this case.

After it has accomplished this task it then inserts an HTML line break after a specified '$line_length' (Depending on your containers width requirements)

<?php

//Start function explode_ wrap
function explode_wrap($text, $chunk_length, $line_length){

//Explode all the words separated by spaces in a string
$string_chunks = explode(' ', $text);

// Get each split word from the array $sring_chunks_array => key => value
foreach ($string_chunks as $chunk => $value) {

if(strlen($value) >= $chunk_length){

//Split the chunks/words which are longer than $chunk_length
$new_string_chunks[$chunk] = chunk_split($value, $chunk_length, ' - ');

}else {

//Do not split the normal length words
$new_string_chunks[$chunk] = $value;

}

}//End foreach loop

//Concatenate back the all the words
$new_text=implode(' ', $new_string_chunks);

return wordwrap($new_text, $line_length, '<br />');

}//End function

?>
nbenitezl[arroba]gmail[dot]com 02-Dec-2010 01:56
Hi, this function is like wordwrap but it ignores html tags, it works like wordwrap when called with fourth parameter as true. It's based on a function I find here but improved to closer match the output of wordwrap (i.e. removed spaces at start of line) and also to improve performance.

Hope it can be useful for you :-)
<?php
function htmlwrap(&$str, $maxLength, $char='<br />'){
    $count = 0;
    $newStr = '';
    $openTag = false;
    $lenstr = strlen($str);
    for($i=0; $i<$lenstr; $i++){
        $newStr .= $str{$i};
        if($str{$i} == '<'){
            $openTag = true;
            continue;
        }
        if(($openTag) && ($str{$i} == '>')){
            $openTag = false;
            continue;
        }
        if(!$openTag){
            if($str{$i} == ' '){
                if ($count == 0) {
                    $newStr = substr($newStr,0, -1);
                    continue;
                } else {
                    $lastspace = $count + 1;
                }
            }
            $count++;
            if($count==$maxLength){
                if ($str{$i+1} != ' ' && $lastspace && ($lastspace < $count)) {
                    $tmp = ($count - $lastspace)* -1;
                    $newStr = substr($newStr,0, $tmp) . $char . substr($newStr,$tmp);
                    $count = $tmp * -1;
                } else {
                    $newStr .= $char;
                    $count = 0;
                }
                $lastspace = 0;
            }
        }
    }

    return $newStr;
}
?>
Ivan Lisnik 11-Jun-2010 12:26
here is my example of UTF-8 wordwrap
<?php
function wordwrap($str, $width, $break)
{
    $return = '';
    $br_width = mb_strlen($break, 'UTF-8');
    for($i = 0, $count = 0; $i < mb_strlen($str, 'UTF-8'); $i++, $count++)
    {
        if (mb_substr($str, $i, $br_width, 'UTF-8') == $break)
        {
            $count = 0;
            $return .= mb_substr($str, $i, $br_width, 'UTF-8');
            $i += $br_width - 1;
        }

        if ($count > $width)
        {
            $return .= $break;
            $count = 0;
        }

        $return .= mb_substr($str, $i, 1, 'UTF-8');
    }

    return $return;
}
?>
kouber at php dot net 27-May-2010 11:34
If you need a function to cut only words longer than N characters, feel free to use the function below:

<?php
function wrap($str, $width=75, $break="\n") {
  return preg_replace('#(\S{'.$width.',})#e', "chunk_split('$1', ".$width.", '".$break."')", $str);
}

echo wrap('aaaa bbb cc dddd-dddd', 3, ' ');
?>

The above example will output: "aaa a  bbb  cc ddd d-d ddd "
ojs-hp at web dot de 30-Oct-2009 07:06
After I got some problems with my function to convert a BB-text into HTML. Long words didn't really fit into the layout and only wordwarp() also added breaks to words which would fit into the layout or destroy the other HTML-tags....
So this is my solution. Only words with strlen() >= 40 are edited with wordwarp().

<?php
function bb2html($bb) {
        $words= explode(' ', $bb); // string to array
    foreach ($words as $word) {
        $break = 0;
        for ($i = 0; $i < strlen($word); $i++) {
            if ($break >= 40) {
                $word= wordwrap($word, 40, '-<br>', true); //add <br> every 40 chars
                $break = 0;
            }
            $break++;

        }
        $newText[] = $word; //add word to array
    }
    $bb = implode(' ', $newText); //array to string
    return $bb;
}
?>
Marcin Dobruk [zuku3000 at yahoo dot co dot uk] 15-Jun-2009 11:21
Word wrap from left to right (standard) and from right to left.

<?php
function myWordWrap ($string, $length=3, $wrap=',', $from='left') {
    if ($from=='left') $txt=wordwrap($string, $length, $wrap, true);
    if ($from=='right') {
        // string to array
        $arr_l=array();
        for ($a=0;strlen($string)>$a;$a++) $arr_l[$a]=$string{$a};
        // reverse array
        $arr_r=array_reverse($arr_l);
        // array to string
        $string_r='';
        foreach ($arr_r as $arr_line => $arr) $string_r.=$arr;
        // add wrap to reverse string
        $string_r=wordwrap($string_r, $length, $wrap, true);
        // reverse string to array
        $arr_r=array();
        for ($a=0;strlen($string_r)>$a;$a++) $arr_r[]=$string_r{$a};
        // reverse array again
        $arr_l=array_reverse($arr_r);
        // string with wrap
        $txt='';
        foreach ($arr_l as $arr_line => $arr) $txt.=$arr;
        }
    return $txt;
    }
?>
r dot hartung at roberthartung dot de 23-Mar-2009 01:26
I was wondering about my CMS to break up code but only non-HTML code - I didn't found anything so I came up with this little solution:

<?php
function wordWrapIgnoreHTML($string, $length = 45, $wrapString = "\n")
   {
     $wrapped = '';
     $word = '';
     $html = false;
     $string = (string) $string;
     for($i=0;$i<strlen($string);$i+=1)
     {
       $char = $string[$i];

       /** HTML Begins */
       if($char === '<')
       {
         if(!empty($word))
         {
           $wrapped .= $word;
           $word = '';
         }

         $html = true;
         $wrapped .= $char;
       }

       /** HTML ends */
       elseif($char === '>')
       {
         $html = false;
         $wrapped .= $char;
       }

       /** If this is inside HTML -> append to the wrapped string */
       elseif($html)
       {
         $wrapped .= $char;
       }

       /** Whitespace characted / new line */
       elseif($char === ' ' || $char === "\t" || $char === "\n")
       {
         $wrapped .= $word.$char;
         $word = '';
       }

       /** Check chars */
       else
       {
         $word .= $char;

         if(strlen($word) > $length)
         {
           $wrapped .= $word.$wrapString;
           $word = '';
         }
       }
     }

    if($word !== ''){
        $wrapped .= $word;
    }

     return $wrapped;
   }

$str = '<a href="http://www.example.de">';
$str .= 'Test-STRRRRRRRRRRRRRRRRRRIIIIIIIIIIIIIIIIIIIIIIING</a>';
$str .= '<!-- COMMENT_INSIDE_ANOTHER';
$str .= '_LONG_STRING //-->';

echo wordWrapIgnoreHTML($str, 25, '[BREAK]');

// Output: <a href="http://www.roberthartung.de">
// Test-STRRRRRRRRRRRRRRRRRRI[BREAK]
// IIIIIIIIIIIIIIIIIIIIIING
// </a><!-- ... //-->
?>

[NOTE BY danbrown AT php DOT net: Contains a bug fix provided by (jaw AT condidact DOT dk) on 02-APR-09 to address an issue where "the last word in a non-html-wrapped string will be omitted."]
Matt at newbiewebdevelopment dot idk 04-Mar-2009 10:53
My version of multibyte wordwrap

<?php
function mb_wordwrap($string, $width=75, $break="\n", $cut = false) {
    if (!$cut) {
        $regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$width.',}\b#U';
    } else {
        $regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$width.'}#';
    }
    $string_length = mb_strlen($string,'UTF-8');
    $cut_length = ceil($string_length / $width);
    $i = 1;
    $return = '';
    while ($i < $cut_length) {
        preg_match($regexp, $string,$matches);
        $new_string = $matches[0];
        $return .= $new_string.$break;
        $string = substr($string, strlen($new_string));
        $i++;
    }
    return $return.$string;
}

$mb_string = "こんにちは";//Hello in Japanese
$cut_mb_string = mb_wordwrap($mb_string,1," ",true); //こ ん に ち は
print($cut_mb_string);
?>
php at maranelda dot org 16-Sep-2008 01:17
Anyone attempting to write a text email client should be aware of the following:

<?php

$a = "some text that must wrap nice";

$a = wordwrap($a, 9);

echo $a;

//  some text
//  that must
//  wrap nice

$a = wordwrap($a, 9);

echo $a;

//  some text
//  that
//  must
//  wrap
//  nice

?>

Subsequent uses of wordwrap() on already wrapped text will take the end-of-line characters into account when working out line length, thus reading each line that just fit nicely the first time around as being one character too long the second. This can be a problem when preparing a text email that contains (eg.) a forwarded email which has already been word-wrapped.

Solutions below which explode() the text on end-of-lines and wordwrap() the resulting strings separately take care of this nicely.
$del=' at '; 'sanneschaap' dot $del dot 'gmail dot com' 17-Apr-2008 10:41
These functions let you wrap strings comparing to their actual displaying width of proportional font. In this case Arial, 11px. Very handy in some cases since CSS3 is not yet completely supported. 100 strings = ~5 ms

My old sheep word wrap function (posted at the bottom of this page, is kinda old dated and this one is faster and more accurate).

<?php
//the width of the biggest char @
$fontwidth = 11;

//each chargroup has char-ords that have the same proportional displaying width
$chargroup[0] = array(64);
$chargroup[1] = array(37,87,119);
$chargroup[2] = array(65,71,77,79,81,86,89,109);
$chargroup[3] = array(38,66,67,68,72,75,78,82,83,85,88,90);
$chargroup[4] = array(35,36,43,48,49,50,51,52,53,54,55,56,57,60,61,62,63, 69,70,76,80,84,95,97,98,99,100,101,103,104,110,111,112, 113,115,117,118,120,121,122,126);
$chargroup[5] = array(74,94,107);
$chargroup[6] = array(34,40,41,42,45,96,102,114,123,125);
$chargroup[7] = array(44,46,47,58,59,91,92,93,116);
$chargroup[8] = array(33,39,73,105,106,108,124);

//how the displaying width are compared to the biggest char width
$chargroup_relwidth[0] = 1; //is char @
$chargroup_relwidth[1] = 0.909413854;
$chargroup_relwidth[2] = 0.728241563;
$chargroup_relwidth[3] = 0.637655417;
$chargroup_relwidth[4] = 0.547069272;
$chargroup_relwidth[5] = 0.456483126;
$chargroup_relwidth[6] = 0.36589698;
$chargroup_relwidth[7] = 0.275310835;
$chargroup_relwidth[8] = 0.184724689;

//build fast array
$char_relwidth = null;
for ($i=0;$i<count($chargroup);$i++){
    for ($j=0;$j<count($chargroup[$i]);$j++){
        $char_relwidth[$chargroup[$i][$j]] = $chargroup_relwidth[$i];
    }
}

//get the display width (in pixels) of a string
function get_str_width($str){
    global $fontwidth,$char_relwidth;
    $result = 0;
    for ($i=0;$i<strlen($str);$i++){
        $result += $char_relwidth[ord($str[$i])];
    }
    $result = $result * $fontwidth;
    return $result;
}

//truncates a string at a certain displaying pixel width
function truncate_str_at_width($str, $width, $trunstr='...'){
    global $fontwidth,$char_relwidth;
    $trunstr_width = get_str_width($trunstr);
    $width -= $trunstr_width;
    $width = $width/$fontwidth;
    $w = 0;
    for ($i=0;$i<strlen($str);$i++){
        $w += $char_relwidth[ord($str[$i])];
        if ($w > $width)
            break;
    }
    $result = substr($str,0,$i).$trunstr;
    return $result;
    // texas is the reason rules at 10am :)
}
?>
joachim 16-Apr-2008 11:42
There seems to be a difference between php 5.1 and 5.2 in how wordwrap counts characters (all on Mac OSX 10.5.2):

/Applications/MAMP/bin/php5/bin/php --version
PHP 5.1.6 (cli) (built: Sep  8 2006 10:25:04)

/Applications/MAMP/bin/php5/bin/php -r 'echo wordwrap("In aller Freundschaft (50)_UT", 20) . "\n";'
In aller
Freundschaft
(50)_UT

php --version
PHP 5.2.5 (cli) (built: Feb 20 2008 12:30:47)

php -r 'echo wordwrap("In aller Freundschaft (50)_UT", 20) . "\n";'
In aller
Freundschaft (50)_UT
Peter 19-Dec-2006 02:00
The main concern when you have a text in a cell is for long words that drags the cell margins. This function will break words in a text that have more then $nr characters using the "-" char.

<?php
function processtext($text,$nr=10)
    {
        $mytext=explode(" ",trim($text));
        $newtext=array();
        foreach($mytext as $k=>$txt)
        {
            if (strlen($txt)>$nr)
            {
                $txt=wordwrap($txt, $nr, "-", 1);
            }
            $newtext[]=$txt;
        }
        return implode(" ",$newtext);
    }
?>
Edward 18-Oct-2006 03:12
I needed a function to justify the text - not just wrap it. I came up with this:

<?php
function justify($text, $width, $break) {
        $marker = "__$%@random#$()__";

        // lines is an array of lines containing the word-wrapped text
        $wrapped = wordwrap($text, $width, $marker);
        $lines = explode($marker, $wrapped);

        $result = "";
        foreach ($lines as $line_index=>$line) {
                $line = trim($line);

                $words = explode(" ", $line);
                $words = array_map("trim", $words);
                $wordcount = count($words);
                $wordlength = strlen(implode("", $words));

                if (3*$wordlength < 2*$width) {
                        // don't touch lines shorter than 2/3 * width
                        continue;
                }

                $spaces = $width - $wordlength;

                $index = 0;
                do {
                        $words[$index] = $words[$index] . " ";
                        $index = ($index + 1) % ($wordcount - 1);
                        $spaces--;
                } while ($spaces>0);

                $lines[$line_index] = implode("", $words);
        }

        return implode($break, $lines);
}
?>
Dave Lozier - dave at fusionbb.com 30-Nov-2005 09:48
If you'd like to break long strings of text but avoid breaking html you may find this useful. It seems to be working for me, hope it works for you. Enjoy. :)

<?php
    function textWrap($text) {
        $new_text = '';
        $text_1 = explode('>',$text);
        $sizeof = sizeof($text_1);
        for ($i=0; $i<$sizeof; ++$i) {
            $text_2 = explode('<',$text_1[$i]);
            if (!empty($text_2[0])) {
                $new_text .= preg_replace('#([^\n\r .]{25})#i', '\\1  ', $text_2[0]);
            }
            if (!empty($text_2[1])) {
                $new_text .= '<' . $text_2[1] . '>';
            }
        }
        return $new_text;
    }
?>
frans-jan at van-steenbeek dot R-E-M-O-V-E dot net 16-Nov-2005 03:17
Using wordwrap is usefull for formatting email-messages, but it has a disadvantage: line-breaks are often treated as whitespaces, resulting in odd behaviour including lines wrapped after just one word.

To work around it I use this:

<?php
 function linewrap($string, $width, $break, $cut) {
  $array = explode("\n", $string);
  $string = "";
  foreach($array as $key => $val) {
   $string .= wordwrap($val, $width, $break, $cut);
   $string .= "\n";
  }
  return $string;
 }
?>

I then use linewrap() instead of wordwrap()

hope this helps someone
bruceboughton @ google mail 05-May-2005 12:08
I found that wordwrap deletes the spaces it wraps on. If you want to break up a string which doesn't consist of words, you may find this behaviour undesirable, as I did when trying to wordwrap a Regular Expression to 80 characters (for display along with test string, matches, etc.).

To preserve the spaces and still achieve a consistent cut length, you need to replace spaces with a suitable one-character replacement. I chose the ASCII non-printing character SUB (ASCII #26; some old telephone code meaning substitute):

<?php
$regex= str_replace(' ', chr(26), $regex);
$regex= wordwrap($regex, 80, '<br />', TRUE);
$regex= str_replace(chr(26), ' ', $regex);
?>

(Of course, you need to replace 80 with your column length and '<br />' with your break string)


변경점

이 확장의 클래스/함수/메소드에 다음과 같은 변경이 이루어졌습니다.

Version | Function | Description
7.1.0 | substr_count | Support for negative offsets and lengths has been added.
7.0.0 | setlocale | Support for the category parameter passed as a string has been removed. Only LC_* constants can be used as of this version.
 | substr | If string is equal to start characters long, an empty string will be returned. Prior to this version, FALSE was returned in this case.
6.0.0 | chr | 입력으로 codepoint를 사용합니다.
5.6.0 | substr_compare | length may now be 0.
5.5.1 | hex2bin | A warning is thrown if the input string is invalid hexadecimal string.
5.4.32, 5.5.16 | ucwords | Added the delimiters parameter.
5.4.4 | hex2bin | A warning is thrown if the input string is of odd length. In PHP 5.4.0 the string was silently accepted, but the last byte was truncated.
5.4.0 | htmlspecialchars_decode | The constants ENT_HTML401, ENT_XML1, ENT_XHTML and ENT_HTML5 were added.
 | number_format | This function now supports multiple bytes in dec_point and thousands_sep. Only the first byte of each separator was used in older versions.
5.3.0 | crypt | 시스템에서 지원하지 않는 MD5 암호화, 표준 DES, 확장 DES, Blowfish 알고리즘에 대해서 PHP의 구현을 가집니다.
 | nl2br | 선택적인 is_xhtml 인수 추가.
 | setlocale | This function now throws an E_DEPRECATED notice if a string is passed to the category parameter instead of one of the LC_* constants.
 | stristr | 선택적인 before_needle 인수 추가
 | strstr | 선택적인 before_needle 인수 추가.
5.2.5 | addcslashes | 회피 시퀀스 \v와 \f 추가
5.2.3 | htmlentities | double_encode 인수 추가.
 | htmlspecialchars | double_encode 인수 추가.
5.2.2 - 5.2.6 | substr | If the start parameter indicates the position of a negative truncation or beyond, false is returned. Other versions get the string from start.
5.1.0 | explode | 음수 limit 지원 추가
 | md5_file | 스트림 API를 사용합니다. 즉, md5_file('http://example.com/..')처럼 래퍼를 사용할 수 있습니다.
 | sha1_file | 스트림 API를 사용합니다. 즉, sha1_file('http://example.com/..')처럼 래퍼를 사용할 수 있습니다.
 | str_word_count | charlist 인수 추가
 | substr_compare | Added the possibility to use a negative offset.
 | substr_count | Added the offset and the length parameters
5.0.0 | html_entity_decode | 멀티바이트 문자셋 지원 추가.
 | md5 | raw_output 인수 추가.
 | md5_file | raw_output 인수 추가
 | sha1 | raw_output 인수 추가.
 | sha1_file | raw_output 인수 추가
 | str_ireplace | count 인수 추가.
 | str_replace | count 인수 추가.
 | strip_tags | strip_tags는 바이너리 안전입니다
4.3.3 | str_replace | 이 함수의 동작이 바뀌었습니다. 이전 버전에서는 search와 replace 인수를 배열로 사용할 때, search 인덱스가 비어 있을 경우 replace 배열의 내부 포인트를 증가하지 않고 지나치는 버그가 있었습니다. 이는 PHP 4.3.3에서 수정되었으므로, 이 버그에 의존하고 있던 모든 스크립트는 이전의 이상한 동작을 재현하려면 호출 전에 빈 search 값을 제거해야 합니다.
4.3.0 | implode | glue 인수가 선택적이 되었습니다.
 | str_rot13 | 이 함수의 동작을 고쳤습니다. 고치기 전에는, 참조로 전달한 것처럼 str도 변경되었습니다.
 | strcspn | start와 length 추가
 | strip_tags | HTML 주석은 항상 제거됩니다
 | stristr | stristr은 바이너리 안전합니다.
 | strrchr | 이 함수는 바이너리 안전입니다.
 | strspn | start와 length 인수 추가
 | strstr | strstr이 바이너리 안전하게 되었습니다.
4.2.3 | strcoll | win32에서 작동합니다.
4.1.0 | htmlentities | charset 인수 추가.
 | htmlspecialchars | charset 인수 추가.
 | ltrim | charlist 인수 추가.
 | rtrim | charlist 인수 추가.
4.0.6 | sprintf | 인수 넘버링/교환 추가.
4.0.5 | nl2br | nl2br은 XHTML 호환입니다. 이전의 모든 버전은 줄바꿈 앞에 '<br />' 대신 '<br>'를 삽입했습니다.
 | str_replace | 대부분의 인수가 array를 받습니다.
4.0.3 | htmlentities | quote_style 인수 추가.
 | parse_str | arr 인수 추가
4.0.1 | explode | limit 인수 추가
4.0.0 | strip_tags | allowable_tags 인수 추가

User Contributed Notes

There are no user contributed notes for this page.


Variable and Type Related Extensions

  • 배열
    • 소개
    • 설치/설정
    • 예약 상수
    • 배열 함수 목록
  • 클래스/객체 — 클래스/객체 정보
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • 클래스/객체 함수 목록
  • Classkit
    • 소개
    • 설치/설정
    • 예약 상수
    • Classkit 함수 목록
  • Ctype — Character type checking
    • 소개
    • 설치/설정
    • 예약 상수
    • Ctype 함수 목록
  • Data Structures
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • Collection — The Collection interface
    • Hashable — The Hashable interface
    • Sequence — The Sequence interface
    • Vector — The Vector class
    • Deque — The Deque class
    • Map — The Map class
    • Pair — The Pair class
    • Set — The Set class
    • Stack — The Stack class
    • Queue — The Queue class
    • PriorityQueue — The PriorityQueue class
  • Filter — Data Filtering
    • 소개
    • 설치/설정
    • Types of filters
    • 예약 상수
    • 예제
    • Filter 함수 목록
  • Function Handling
    • 소개
    • 설치/설정
    • 예약 상수
    • Function handling 함수 목록
  • Quickhash
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • QuickHashIntSet — The QuickHashIntSet class
    • QuickHashIntHash — The QuickHashIntHash class
    • QuickHashStringIntHash — The QuickHashStringIntHash class
    • QuickHashIntStringHash — The QuickHashIntStringHash class
  • Reflection
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • Extending
    • Reflection — The Reflection class
    • ReflectionClass — The ReflectionClass class
    • ReflectionZendExtension — The ReflectionZendExtension class
    • ReflectionExtension — The ReflectionExtension class
    • ReflectionFunction — The ReflectionFunction class
    • ReflectionFunctionAbstract — The ReflectionFunctionAbstract class
    • ReflectionMethod — The ReflectionMethod class
    • ReflectionObject — The ReflectionObject class
    • ReflectionParameter — The ReflectionParameter class
    • ReflectionProperty — The ReflectionProperty class
    • ReflectionType — The ReflectionType class
    • ReflectionGenerator — The ReflectionGenerator class
    • Reflector — The Reflector interface
    • ReflectionException — The ReflectionException class
  • Variable handling
    • 소개
    • 설치/설정
    • 예약 상수
    • Variable handling 함수 목록

User Contributed Notes

There are no user contributed notes for this page.


배열

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 배열 함수 목록
    • array_change_key_case — 배열 안의 모든 키를 변경
    • array_chunk — 배열을 조각으로 나누기
    • array_column — Return the values from a single column in the input array
    • array_combine — 키를 위한 배열과 값을 위한 배열을 사용하여 배열을 생성
    • array_count_values — 배열 값의 수를 셉니다
    • array_diff_assoc — 추가적인 인덱스 확인과 함께 배열 차이를 계산
    • array_diff_key — Computes the difference of arrays using keys for comparison
    • array_diff_uassoc — Computes the difference of arrays with additional index check which is performed by a user supplied callback function
    • array_diff_ukey — Computes the difference of arrays using a callback function on the keys for comparison
    • array_diff — 배열 차이를 계산
    • array_fill_keys — Fill an array with values, specifying keys
    • array_fill — 값으로 배열 채우기
    • array_filter — 콜백 함수를 사용하여 배열 원소를 필터
    • array_flip — 배열 안의 모든 키를 각 키의 연관 값과 교체
    • array_intersect_assoc — 인덱스 검사과 함께 배열의 교집합을 계산
    • array_intersect_key — Computes the intersection of arrays using keys for comparison
    • array_intersect_uassoc — Computes the intersection of arrays with additional index check, compares indexes by a callback function
    • array_intersect_ukey — Computes the intersection of arrays using a callback function on the keys for comparison
    • array_intersect — 배열의 교집합을 계산
    • array_key_exists — 주어진 키와 인덱스가 배열에 존재하는지 확인
    • array_keys — 배열의 모든 키를 반환
    • array_map — Applies the callback to the elements of the given arrays
    • array_merge_recursive — 두개 이상의 배열을 재귀적으로 병합
    • array_merge — 하나 이상의 배열을 병합
    • array_multisort — 여러 배열이나 다차원 배열 정렬
    • array_pad — 지정한 길이만큼 특정 값으로 배열 채우기
    • array_pop — 배열의 마지막 원소 빼내기
    • array_product — Calculate the product of values in an array
    • array_push — 배열의 끝에 하나 이상의 원소를 넣는다
    • array_rand — 배열에서 하나 이상의 임의 원소를 가져옴
    • array_reduce — 콜백 함수를 사용하여 배열을 반복적으로 단일 값으로 축소
    • array_replace_recursive — Replaces elements from passed arrays into the first array recursively
    • array_replace — Replaces elements from passed arrays into the first array
    • array_reverse — 원소를 역순으로 가지는 배열을 반환
    • array_search — 주어진 값으로 배열을 검색하여 성공시 해당하는 키를 반환
    • array_shift — 배열의 맨 앞에 있는 원소를 시프트
    • array_slice — 배열의 일부를 추출
    • array_splice — 배열의 일부를 삭제하고, 그 위치를 다른 내용으로 대체
    • array_sum — 배열 값들의 합을 계산
    • array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function
    • array_udiff_uassoc — Computes the difference of arrays with additional index check, compares data and indexes by a callback function
    • array_udiff — 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
    • array_uintersect_assoc — Computes the intersection of arrays with additional index check, compares data by a callback function
    • array_uintersect_uassoc — Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
    • array_uintersect — Computes the intersection of arrays, compares data by a callback function
    • array_unique — 배열에서 중복된 값을 제거
    • array_unshift — 배열의 맨 앞에 하나 이상의 원소를 첨가
    • array_values — 배열의 모든 값을 반환
    • array_walk_recursive — Apply a user function recursively to every member of an array
    • array_walk — 배열의 각 원소에 대해서 특정 함수를 적용
    • array — 배열 생성
    • arsort — 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
    • asort — 배열을 정렬하고 인덱스 상관 관계를 유지
    • compact — 변수와 그 값을 가지는 배열 생성
    • count — 배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다
    • current — 배열의 현재 원소를 반환
    • each — 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진
    • end — 배열 내부 포인터가 마지막 원소를 가리키게 설정
    • extract — 배열에서 현재 심볼 테이블로 변수를 입력
    • in_array — 값이 배열 안에 존재하는지 확인
    • key_exists — 별칭: array_key_exists
    • key — 배열에서 키를 가져옵니다
    • krsort — 키에 의한 배열 역순 정렬
    • ksort — 키에 의한 배열 정렬
    • list — 배열처럼 변수에 할당
    • natcasesort — "자연순" 알고리즘으로 대소문자를 구분하지 않고 배열 정렬
    • natsort — "자연순" 알고리즘으로 배열 정렬
    • next — 배열의 내부 배열 포인터를 전진
    • pos — 별칭: current
    • prev — 내부 배열 포인터를 후진
    • range — 원소의 범위를 가지는 배열 생성
    • reset — 배열의 내부 포인터를 첫 원소로 설정
    • rsort — 역순으로 배열 정렬
    • shuffle — 배열을 섞습니다
    • sizeof — 별칭: count
    • sort — 배열 정렬
    • uasort — 사용자 정의 비교 함수로 배열을 정렬하고 인덱스 연관성을 유지
    • uksort — 사용자 정의 비교 함수를 사용하여 키에 의한 배열 정렬
    • usort — 사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬

User Contributed Notes

Anonymous 23-Oct-2014 04:36
And which php version where added short array creating form:
$myArr = [];

?
gratcypalma at gmail dot com 09-Sep-2013 06:57
<?php
function foo() {
    return array('name' => 'palma', 'old' => 23, 'language' => 'PHP');
}
/* 1. PHP < 5.4.0 */
$a = foo();
var_dump($a['name']);

/* 2. Works ini PHP >= 5.4.0 */

var_dump(foo()['name']);

/*
When i run second method on PHP 5.3.8 i will be show error message "PHP Fatal error:  Can't use method return value in write context"

http://www.php.net/manual/en/migration54.new-features.php
*/
spereversev at envionsoftware dot com 10-Jul-2012 07:29
<?php
function array_mask(array $array, array $keys) {
    return array_intersect_key( $array, array_fill_keys( $keys, 0 ) );
}
?>

Might be helpful to take a part of associative array containing given keys, for example, from a $_REQUEST array

array_mask($_REQUEST, array('name', 'email'));
info at curtinsNOSPAMcreations dot com 05-Nov-2010 04:49
Another way to create a multidimensional array that looks a lot cleaner is to use json_decode.  (Note that this probably adds a touch of overhead, but it sure does look nicer.)  You can of course add as many levels and as much formatting as you'd like to the string you then decode.  Don't forget that json requires " around values, not '!!  (So, you can't enclose the json string with " and use ' inside the string.)

As an example:

<?php
    $myarray['blah'] = json_decode('[
        {"label":"foo","name":"baz"},
        {"label":"boop","name":"beep"}
    ]',true);

print_r($myarray)
?>
returns:

Array
(
    [blah] => Array
        (
            [0] => Array
                (
                    [label] => foo
                    [name] => baz
                )

            [1] => Array
                (
                    [label] => boop
                    [name] => beep
                )
        )
)
dragos dot rusu at NOSPAM dot bytex dot ro 07-Mar-2010 03:15
If an array item is declared with key as NULL, array key will automatically be converted to empty string '', as follows:

<?php
$a = array(
    NULL => 'zero',
    1    => 'one',
    2    => 'two');

// This will show empty string for key associated with "zero" value
var_dump(array_keys($a));

// Array elements are shown
reset($a);
while( key($a) !== NULL )
{
  echo key($a) . ": ".current($a) . "<br>";// PHP_EOL
  next($a);
}

// Array elements are not shown
reset($a);
while( key($a) != NULL ) // '' == null   => no iteration will be executed
{
  echo key($a) . ": ".current($a) . "<br>";// PHP_EOL
  next($a);
}
John Marc 15-Feb-2010 10:30
Be careful when adding elements to a numeric array.
I wanted to store some info about some items from a database and decided to use the record id as a key.

<?php
$key=3000000000;
$DATA[$key]=true;
?>

This will create an array of 30 million elements and chances are, you will use up all memory with these 2 lines

<?php
$key=3000000000;
$DATA["$key"]=true;
?>

This on the other hand will force the array to be an associative array and will only create the one element
sunear at gmail dot com 04-Aug-2009 07:45
Made this function to delete elements in an array;

<?php

function array_del_elm($input_array, $del_indexes) {
    if (is_array($del_indexes)) {
        $indexes = $del_indexes;
    } elseif(is_string($del_indexes)) {
        $indexes = explode($del_indexes, " ");
    } elseif(is_numeric($del_indexes)) {
        $indexes[0] = (integer)$del_indexes;
    } else return;
    $del_indexes = null;

    $cur_index = 0;
    if (sort($indexes)) for($i=0; $i<count($input_array); $i++) {
        if ($i == $indexes[$cur_index]) {
            $cur_index++;
            if ($cur_index == count($indexes)) return $output_array;
            continue;
        }
        $output_array[] = $input_array[$i];
    }
    return $output_array;
}

?>

but then i saw the methods of doing the same by Tyler Bannister & Paul, could see that theirs were faster, but had floors regarding deleting multiple elements thus support of several ways of giving parameters. I combined the two methods to this to this:

<?php

function array_del_elm($target_array, $del_indexes) {
    if (is_array($del_indexes)) {
        $indexes = $del_indexes;
    } elseif(is_string($del_indexes)) {
        $indexes = explode($del_indexes, " ");
    } elseif(is_numeric($del_indexes)) {
        $indexes[0] = (integer)$del_indexes;
    } else return;
    unset($del_indexes);

    for($i=0; $i<count($indexes); $i++) {
        unset($target_array[$indexes[$i]]);
    }
    return $target_array;
}

?>

Fast, compliant and functional ;)
Tyler Bannister 11-Jun-2009 03:59
To delete an individual array element use the unset function

For example:

<?PHP
    $arr = array( "A", "B", "C" );
    unset( $arr[1] );
    // now $arr = array( "A", "C" );
?>

Unlink is for deleting files.
thomasdecaux at ebuildy dot com 15-May-2009 10:14
To browse a simple array:

<?php

foreach ($myArray AS $myItem)
{

}

?>

To browse an associative array:

<?php

foreach ($myArray AS $key=>$value)
{

}

?>

http://www.ebuildy.com
macnimble at gmail dot com 27-Mar-2009 03:59
Converting a linear array (like a mysql record set) into a tree, or multi-dimensional array can be a real bugbear. Capitalizing on references in PHP, we can 'stack' an array in one pass, using one loop, like this:

<?php
# array_stack()
# Original idea from:
# http://www.ideashower.com/our_solutions/
#   create-a-parent-child-array-structure-in-one-pass/
function array_stack (&$a, $p = '@parent', $c = '@children')
{
  $l = $t = array();
  foreach ($a AS $key => $val):
    if (!$val[$p]) $t[$key] =& $l[$key];
    else $l[$val[$p]][$c][$key] =& $l[$key];
    $l[$key] = (array)$l[$key] + $val;
  endforeach;
  return $a = array('tree' => $t, 'leaf' => $l);
}

# Example:
$node = array();
$node[1] = array('@parent' => 0, 'title' => 'I am node 1.');
#     ^-----------------------v Link @parent value to key.
$node[2] = array('@parent' => 1, 'title' => 'I am node 2.');
$node[3] = array('@parent' => 2, 'title' => 'I am node 3.');
$node[4] = array('@parent' => 1, 'title' => 'I am node 4.');
$node[5] = array('@parent' => 4, 'title' => 'I am node 5.');

array_stack($node);

$node['leaf'][1]['title'] = 'I am node one.';
$node['leaf'][2]['title'] = 'I am node two.';
$node['leaf'][3]['title'] = 'I am node three.';
$node['leaf'][4]['title'] = 'I am node four.';
$node['leaf'][5]['title'] = 'I am node five.';

echo '<pre>',print_r($node['tree'],TRUE),'</pre>';
?>

Note that there's no parameter checking on the array value, but this is only to keep the function size small. One could easily a quick check in there to make sure the $a parameter was in fact an array.

Hope you find it useful. Huge thanks to Nate Weiner of IdeaShower.com for providing the original function I built on.
andyd273 at gmail dot com 24-Feb-2009 10:12
A small correction to Endel Dreyer's PHP array to javascript array function. I just changed it to show keys correctly:

            function array2js($array,$show_keys)
            {
                $dimensoes = array();
                $valores = array();

                $total = count ($array)-1;
                $i=0;
                foreach($array as $key=>$value){
                    if (is_array($value)) {
                        $dimensoes[$i] = array2js($value,$show_keys);
                        if ($show_keys) $dimensoes[$i] = '"'.$key.'":'.$dimensoes[$i];
                    } else {
                        $dimensoes[$i] = '"'.addslashes($value).'"';
                        if ($show_keys) $dimensoes[$i] = '"'.$key.'":'.$dimensoes[$i];
                    }
                    if ($i==0) $dimensoes[$i] = '{'.$dimensoes[$i];
                    if ($i==$total) $dimensoes[$i].= '}';
                    $i++;
                }
                return implode(',',$dimensoes);
            }
Anonymous 26-Nov-2008 07:36
@jorge at andrade dot cl
This variant is faster:
<?php
function array_avg($array,$precision=2){
    if(!is_array($array))
        return 'ERROR in function array_avg(): this is a not array';

    foreach($array as $value)
        if(!is_numeric($value))
            return 'ERROR in function array_avg(): the array contains one or more non-numeric values';

    $cuantos=count($array);
  return round(array_sum($array)/$cuantos,$precision);
}
?>
webmaster at infoproducts dot x10hosting dot com 11-Oct-2008 04:13
New value can also be added to the array as shown below.
$theVariable["google"] = "http//google.com";
or
$theVariable["1"] = "http//google.com";
Jack A 07-Oct-2008 05:14
Note that arrays are not allowed in class constants and trying to do so will throw a fatal error.
contact at greyphoenix dot biz 01-Jul-2008 06:36
<?php
//Creating a multidimensional array

$theVariable = array("Search Engines" =>
array (
    0=> "http//google.com",
    1=> "http//yahoo.com",
    2=> "http//msn.com/"),

"Social Networking Sites" =>
array (
    0 => "http//www.facebook.com",
    1 => "http//www.myspace.com",
    2 => "http//vkontakte.ru",)
);

echo "The first array value is " . $theVariable['Search Engines'][0];
?>

-- Output--
The first array value is http://google.com
applegrew at rediffmail dot com 28-May-2008 05:23
For newbies like me.

Creating new arrays:-
//Creates a blank array.
$theVariable = array();

//Creates an array with elements.
$theVariable = array("A", "B", "C");

//Creating Associaive array.
$theVariable = array(1 => "http//google.com", 2=> "http://yahoo.com");

//Creating Associaive array with named keys
$theVariable = array("google" => "http//google.com", "yahoo"=> "http://yahoo.com");

Note:
New value can be added to the array as shown below.
$theVariable[] = "D";
$theVariable[] = "E";


소개

이 함수들은 배열을 다양한 방법으로 소통하고 조작하게 합니다. 배열은 변수 묶음을 저장하고, 관리하고, 연산하는데 필수적입니다.

간단한 배열과 다차원 배열을 지원하며, 사용자 생성과 다른 함수에 의하여 만들어지는 경우가 있습니다. 데이터베이스 질의에서 배열을 생성하는 특정 데이터베이스 다루기 함수가 있으며, 많은 함수가 배열을 반환합니다.

매뉴얼 배열 섹션을 참고하여 배열에 대한 자세한 설명과 PHP에서 어떻게 구현되고 사용되는지 확인하십시오. 다른 방법으로 배열을 조작하는 방법을 배열 연산자에서 참고하십시오.

User Contributed Notes

There are no user contributed notes for this page.


설치

이 함수들은 설치하지 않아도 사용할 수 있습니다; PHP 코어의 일부입니다.

User Contributed Notes

Kip 15-May-2014 07:54
To write the shorthand version, you must have PHP 5.5 or greater.

<?php
$arr = array('long', 'version');
$arr2 = ['short', 'version'];

$arr3 = array_merge($arr, $arr2, ['array', 'three', '.']);


예약 상수

아래 상수들은 PHP 코어의 일부로, 항상 사용할 수 있습니다.

CASE_LOWER (integer)
CASE_LOWER는 array_change_key_case() 함수에서 배열 키를 소문자로 변환하는데 사용된다. 또한 이 상수는 array_change_key_case()의 기본값이다.
CASE_UPPER (integer)
CASE_UPPER는 array_change_key_case() 함수에서 배열 키를 대문자로 변환하는데 사용된다.

정렬 순서 플래그:

SORT_ASC (integer)
SORT_ASC는 array_multisort() 함수에서 오름차순 정렬에 사용된다.
SORT_DESC (integer)
SORT_DESC는 array_multisort() 함수에서 내림차순 정렬에 사용된다.

정렬 형태 플래그: 여러 정렬함수에 의해 사용됨.

SORT_REGULAR (integer)
SORT_REGULAR는 아이템들을 통상적인 방법으로 비교하는데 사용됨.
SORT_NUMERIC (integer)
SORT_NUMERIC는 아이템들을 수치로서 비교하는데 사용됨.
SORT_STRING (integer)
SORT_STRING는 아이템들을 문자열로서 비교하는데 사용됨.
SORT_LOCALE_STRING (integer)
SORT_LOCALE_STRING는 현재 로케일에 따라서 아이템을 문자열로 비교할 때 사용합니다. PHP 4.4.0과 5.0.2에서 추가되었습니다.

COUNT_NORMAL (integer)
COUNT_RECURSIVE (integer)
EXTR_OVERWRITE (integer)
EXTR_SKIP (integer)
EXTR_PREFIX_SAME (integer)
EXTR_PREFIX_ALL (integer)
EXTR_PREFIX_INVALID (integer)
EXTR_PREFIX_IF_EXISTS (integer)
EXTR_IF_EXISTS (integer)
EXTR_REFS (integer)

User Contributed Notes

There are no user contributed notes for this page.


배열 함수 목록

참고

참고: is_array(), explode(),
implode(), split(),
preg_split(), unset().

Table of Contents

  • array_change_key_case — 배열 안의 모든 키를 변경
  • array_chunk — 배열을 조각으로 나누기
  • array_column — Return the values from a single column in the input array
  • array_combine — 키를 위한 배열과 값을 위한 배열을 사용하여 배열을 생성
  • array_count_values — 배열 값의 수를 셉니다
  • array_diff_assoc — 추가적인 인덱스 확인과 함께 배열 차이를 계산
  • array_diff_key — Computes the difference of arrays using keys for comparison
  • array_diff_uassoc — Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_diff_ukey — Computes the difference of arrays using a callback function on the keys for comparison
  • array_diff — 배열 차이를 계산
  • array_fill_keys — Fill an array with values, specifying keys
  • array_fill — 값으로 배열 채우기
  • array_filter — 콜백 함수를 사용하여 배열 원소를 필터
  • array_flip — 배열 안의 모든 키를 각 키의 연관 값과 교체
  • array_intersect_assoc — 인덱스 검사과 함께 배열의 교집합을 계산
  • array_intersect_key — Computes the intersection of arrays using keys for comparison
  • array_intersect_uassoc — Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_ukey — Computes the intersection of arrays using a callback function on the keys for comparison
  • array_intersect — 배열의 교집합을 계산
  • array_key_exists — 주어진 키와 인덱스가 배열에 존재하는지 확인
  • array_keys — 배열의 모든 키를 반환
  • array_map — Applies the callback to the elements of the given arrays
  • array_merge_recursive — 두개 이상의 배열을 재귀적으로 병합
  • array_merge — 하나 이상의 배열을 병합
  • array_multisort — 여러 배열이나 다차원 배열 정렬
  • array_pad — 지정한 길이만큼 특정 값으로 배열 채우기
  • array_pop — 배열의 마지막 원소 빼내기
  • array_product — Calculate the product of values in an array
  • array_push — 배열의 끝에 하나 이상의 원소를 넣는다
  • array_rand — 배열에서 하나 이상의 임의 원소를 가져옴
  • array_reduce — 콜백 함수를 사용하여 배열을 반복적으로 단일 값으로 축소
  • array_replace_recursive — Replaces elements from passed arrays into the first array recursively
  • array_replace — Replaces elements from passed arrays into the first array
  • array_reverse — 원소를 역순으로 가지는 배열을 반환
  • array_search — 주어진 값으로 배열을 검색하여 성공시 해당하는 키를 반환
  • array_shift — 배열의 맨 앞에 있는 원소를 시프트
  • array_slice — 배열의 일부를 추출
  • array_splice — 배열의 일부를 삭제하고, 그 위치를 다른 내용으로 대체
  • array_sum — 배열 값들의 합을 계산
  • array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc — Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_udiff — 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
  • array_uintersect_assoc — Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc — Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
  • array_uintersect — Computes the intersection of arrays, compares data by a callback function
  • array_unique — 배열에서 중복된 값을 제거
  • array_unshift — 배열의 맨 앞에 하나 이상의 원소를 첨가
  • array_values — 배열의 모든 값을 반환
  • array_walk_recursive — Apply a user function recursively to every member of an array
  • array_walk — 배열의 각 원소에 대해서 특정 함수를 적용
  • array — 배열 생성
  • arsort — 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • asort — 배열을 정렬하고 인덱스 상관 관계를 유지
  • compact — 변수와 그 값을 가지는 배열 생성
  • count — 배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다
  • current — 배열의 현재 원소를 반환
  • each — 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진
  • end — 배열 내부 포인터가 마지막 원소를 가리키게 설정
  • extract — 배열에서 현재 심볼 테이블로 변수를 입력
  • in_array — 값이 배열 안에 존재하는지 확인
  • key_exists — 별칭: array_key_exists
  • key — 배열에서 키를 가져옵니다
  • krsort — 키에 의한 배열 역순 정렬
  • ksort — 키에 의한 배열 정렬
  • list — 배열처럼 변수에 할당
  • natcasesort — "자연순" 알고리즘으로 대소문자를 구분하지 않고 배열 정렬
  • natsort — "자연순" 알고리즘으로 배열 정렬
  • next — 배열의 내부 배열 포인터를 전진
  • pos — 별칭: current
  • prev — 내부 배열 포인터를 후진
  • range — 원소의 범위를 가지는 배열 생성
  • reset — 배열의 내부 포인터를 첫 원소로 설정
  • rsort — 역순으로 배열 정렬
  • shuffle — 배열을 섞습니다
  • sizeof — 별칭: count
  • sort — 배열 정렬
  • uasort — 사용자 정의 비교 함수로 배열을 정렬하고 인덱스 연관성을 유지
  • uksort — 사용자 정의 비교 함수를 사용하여 키에 의한 배열 정렬
  • usort — 사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬

User Contributed Notes

nicoolasens at gmail dot com 06-Nov-2015 07:15
/*to change an index without rewriting the whole table and leave at the same place.
*/
<?php
function change_index(&$tableau, $old_key, $new_key) {
    $changed = FALSE;
    $temp = 0;
    foreach ($tableau as $key => $value) {
        switch ($changed) {
            case FALSE :
                //creates the new key and deletes the old
                if ($key == $old_key) {
                    $tableau[$new_key] = $tableau[$old_key];
                    unset($tableau[$old_key]);
                    $changed = TRUE;
                }
                break;

            case TRUE :
                //moves following keys
                if ($key != $new_key){
                $temp= $tableau[$key];
                unset($tableau[$key]);
                $tableau[$key] = $temp;
                break;
                }
                else {$changed = FALSE;} //stop
        }
    }
    array_values($tableau); //free_memory
}

//Result :
$tableau = array(1, 2 , 3, 4,5, 6, 7, 8, 9, 10);
$res = print_r($tableau, TRUE);
$longueur = strlen($res) -1;
echo "Old array :\n" . substr($res, 8, $longueur) . "\n" ;

change_index ($tableau, 2, 'number 2');
$res = print_r($tableau, TRUE);
$longueur = strlen($res) -10;
echo "New array :\n" . substr($res, 8, $longueur) . "\n" ;

/*
Old array :
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)

New array :
    [0] => 1
    [1] => 2
    [numero 2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
*/
?>
callmeanaguma at gmail dot com 18-Jan-2013 08:30
If you need to flattern two-dismensional array with single values assoc subarrays, you could use this function:

<?php
function arrayFlatten($array) {
        $flattern = array();
        foreach ($array as $key => $value){
            $new_key = array_keys($value);
            $flattern[] = $value[$new_key[0]];
        }
        return $flattern;
}
?>
kolkabes at googlemail dot com 05-May-2012 09:31
Short function for making a recursive array copy while cloning objects on the way.

<?php
function arrayCopy( array $array ) {
        $result = array();
        foreach( $array as $key => $val ) {
            if( is_array( $val ) ) {
                $result[$key] = arrayCopy( $val );
            } elseif ( is_object( $val ) ) {
                $result[$key] = clone $val;
            } else {
                $result[$key] = $val;
            }
        }
        return $result;
}
?>
dave at davidhbrown dot us 24-Sep-2011 08:57
While PHP has well over three-score array functions, array_rotate is strangely missing as of PHP 5.3. Searching online offered several solutions, but the ones I found have defects such as inefficiently looping through the array or ignoring keys.

The following array_rotate() function uses array_merge and array_shift to reliably rotate an array forwards or backwards, preserving keys. If you know you can trust your $array to be an array and $shift to be between 0 and the length of your array, you can skip the function definition and use just the return expression in your code.

<?php
function array_rotate($array, $shift) {
    if(!is_array($array) || !is_numeric($shift)) {
        if(!is_array($array)) error_log(__FUNCTION__.' expects first argument to be array; '.gettype($array).' received.');
        if(!is_numeric($shift)) error_log(__FUNCTION__.' expects second argument to be numeric; '.gettype($shift)." `$shift` received.");
        return $array;
    }
    $shift %= count($array); //we won't try to shift more than one array length
    if($shift < 0) $shift += count($array);//handle negative shifts as positive
    return array_merge(array_slice($array, $shift, NULL, true), array_slice($array, 0, $shift, true));
}
?>
A few simple tests:
<?php
$array=array("foo"=>1,"bar"=>2,"baz"=>3,4,5);

print_r(array_rotate($array, 2));
print_r(array_rotate($array, -2));
print_r(array_rotate($array, count($array)));
print_r(array_rotate($array, "4"));
print_r(array_rotate($array, -9));
?>
oliverSPAMMENOT at e-geek dot com dot au 23-Mar-2010 04:28
Function to pretty print arrays and objects. Detects object recursion and allows setting a maximum depth. Based on arraytostring and u_print_r from the print_r function notes. Should be called like so:

<?php
egvaluetostring($value)   //no max depth, or
egvaluetostring($value, $max_depth)   //max depth set

function egvaluetostring($value, $max_depth, $key = NULL, $depth = 0, $refChain = array()) {
  if($depth > 0)
    $tab = str_repeat("\t", $depth);
  $text .= $tab . ($key !== NULL ? $key . " => " : "");

  if (is_array($value) || is_object($value)) {
    $recursion = FALSE;
    if (is_object($value)) {
      foreach ($refChain as $refVal) {
        if ($refVal === $value) {
          $recursion = TRUE;
          break;
        }
      }
      array_push($refChain, $value);
    }

    $text .= (is_array($value) ? "array" : "object") . " ( ";

    if ($recursion) {
      $text .= "*RECURSION* ";
    }
    elseif (isset($max_depth) && $depth >= $max_depth) {
      $text .= "*MAX DEPTH REACHED* ";
    }
    else {
      if (!empty($value)) {
        $text .= "\n";
        foreach ($value as $child_key => $child_value) {
          $text .= egvaluetostring($child_value, $max_depth, (is_array($value) ? "[" : "") . $child_key . (is_array($value) ? "]" : ""), $depth+1, $refChain) . ",\n";
        }
        $text .= "\n" . $tab;
      }
    }

    $text .= ")";

    if (is_object($value)) {
      array_pop($refChain);
    }
  }
  else {
    $text .= "$value";
  }

  return $text;
}
?>
seva dot lapsha at gmail dot com 03-Aug-2009 03:22
Arrays are good, but inapplicable when dealing with huge amounts of data.

I'm working on rewriting some array functions to operate with plain Iterators - map, reduce, walk, flip et cetera are already there.

In addition I'm going to implement simulation of comprehensions (generators) in PHP (http://en.wikipedia.org/wiki/List_comprehension).

See the source code, examples and documentation at http://code.google.com/p/php-iterator-utils/
renatonascto at gmail dot com 17-Sep-2008 08:11
Big arrays use a lot of memory possibly resulting in memory limit errors. You can reduce memory usage on your script by destroying them as soon as you´re done with them. I was able to get over a few megabytes of memory by simply destroying some variables I didn´t use anymore.
You can view the memory usage/gain by using the funcion memory_get_usage(). Hope this helps!
cyberchrist at futura dot net 15-Oct-2007 03:33
Lately, dealing with databases, I've been finding myself needing to know if one array, $a, is a proper subset of $b.

Mathematically, this is asking (in set theory) [excuse the use of u and n instead of proper Unicode):

( A u B ) n ( ~ B )

What this does is it first limits to known values, then looks for anything outside of B but in the union of A and B (which would be those things in A which are not also in B).

If any value exists in this set, then A is NOT a proper subset of B, because a value exists in A but not in B.  For A to be a proper subset, all values in A must be in B.

I'm sure this could easily be done any number of ways but this seems to work for me.  It's not got a lot of error detection such as sterilizing inputs or checking input types.

// bool array_subset( array, array )
// Returns true if $a is a proper subset of $b, returns false otherwise.

function array_subset( $a, $b )
{
    if( count( array_diff( array_merge($a,$b), $b)) == 0 )
        return true;
    else
        return false;
}
mo dot longman at gmail dot com 31-Jul-2007 08:27
to 2g4wx3:
i think better way for this is using JSON, if you have such module in your PHP. See json.org.

to convert JS array to JSON string: arr.toJSONString();
to convert JSON string to PHP array: json_decode($jsonString);

You can also stringify objects, numbers, etc.
ob at babcom dot biz 28-Aug-2006 01:23
Here is a function to find out the maximum depth of a multidimensional array.

<?php
// return depth of given array
// if Array is a string ArrayDepth() will return 0
// usage: int ArrayDepth(array Array)

function ArrayDepth($Array,$DepthCount=-1,$DepthArray=array()) {
  $DepthCount++;
  if (is_array($Array))
    foreach ($Array as $Key => $Value)
      $DepthArray[]=ArrayDepth($Value,$DepthCount);
  else
    return $DepthCount;
  foreach($DepthArray as $Value)
    $Depth=$Value>$Depth?$Value:$Depth;
  return $Depth;
}
?>


배열 안의 모든 키를 변경

  • ≪ 배열 함수 목록
  • array_chunk ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열 안의 모든 키를 변경

array_change_key_case

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

array_change_key_case — 배열 안의 모든 키를 변경

설명

array array_change_key_case ( array $input [, int $case ] )

input에서 모든 키를 소문자나 대문자로 바꾼 배열을 반환합니다. 숫자 인덱스는 그대로 남습니다.

인수

input

작업할 배열

case

CASE_UPPER나 CASE_LOWER(기본값)

반환값

키를 소문자나 대문자로 바꾼 배열을 반환합니다. input이 배열이 아니면 false를 반환합니다.

오류/예외

input이 배열이 아니면 E_WARNING이 발생합니다.

예제

Example #1 array_change_key_case() 예제

<?php
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>

위 예제의 출력:

Array
(
    [FIRST] => 1
    [SECOND] => 4
)

주의

Note:

이 함수가 작동하는 배열에 동일한 인덱스가 존재하면(예. "keY"와 "kEY"), 나중의 값이 다른 인덱스를 덮어씌웁니다.

User Contributed Notes

ahmadz 26-Oct-2016 02:49
function convert_to_key_case($data, $case){

        $condition = ($case == 'lowercase')? 'lowercase' : 'uppercase';
        $test = [];
        foreach($data as  $key => $value) {
            if ($condition == 'lowercase') {
                $test[strtolower($key)] = $value;
            } else {
                $test[strtoupper($key)] = $value;
            }
        }
    return $test;
}

$input = ['test1'=>1, 'Test2' => 2, 'TeSt3' => 3];

print_r(convert_to_key_case($input, 'uppercase'));
print_r(convert_to_key_case($input, 'lowercase'));

OUTPUT

Array
(
 [TEST1] => 1
 [TEST2] => 2
 [TEST3] => 3
)

Array (
 [test1] => 1
 [test2] => 2
 [test3] => 3
 )
oulala at yandex dot com 10-Feb-2016 11:12
Recursive LowerCase First Letter in array key

if(!function_exists("mb_lcfirstKeys")) {
    function mb_lcfirstKeys($data) {
        $res = [];
        foreach ($data as $key => $value) {
            $newKey = mb_lcfirst($key);
            $res[$newKey] = is_array($value) ? mb_lcfirstKeys($value) : $value;
        }
        return $res;
    }
}
xsaero00 26-Apr-2014 02:33
Here is the most compact way to lower case keys in a multidimensional array

function array_change_key_case_recursive($arr)
{
    return array_map(function($item){
        if(is_array($item))
            $item = array_change_key_case_recursive($item);
        return $item;
    },array_change_key_case($arr));
}
NetPanther 22-Sep-2012 10:27
Use this to capitalize first letter of all array keys:

<?php

function ucfirstKeys(&$data)
{
  foreach ($data as $key => $value)
  {
    // Convert key
    $newKey = ucfirst($key);

    // Change key if needed
    if ($newKey != $key)
    {
      unset($data[$key]);
      $data[$newKey] = $value;
    }

    // Handle nested arrays
    if (is_array($value))
    {
      ucfirstKeys($data[$key]);
    }
  }
}

$test = array('foo' => 'bar', 'moreFoo' => array('more' => 'foo'));
ucfirstKeys($test);
print_r($test);

?>

Result:
Array ( [MoreFoo] => Array ( [More] => foo ) [Foo] => bar )
qeremy [atta] gmail [dotta] com 29-Feb-2012 02:15
Unicode example;

<?php
function array_change_key_case_unicode($arr, $c = CASE_LOWER) {
    $c = ($c == CASE_LOWER) ? MB_CASE_LOWER : MB_CASE_UPPER;
    foreach ($arr as $k => $v) {
        $ret[mb_convert_case($k, $c, "UTF-8")] = $v;
    }
    return $ret;
}

$arr = array("FirSt" => 1, "ya?" => "Oil", "?ekER" => "sugar");
print_r(array_change_key_case($arr, CASE_UPPER));
print_r(array_change_key_case_unicode($arr, CASE_UPPER));
?>

Array
(
    [FIRST] => 1
    [YA?] => Oil
    [?EKER] => sugar
)
Array
(
    [FIRST] => 1
    [YA?] => Oil
    [?EKER] => sugar
)
JOE 20-Oct-2011 08:13
Script to change case of sub-arrays keys to lower . It works well for me. hope it helps.

<?php
$array=array("First"=>"val1","SecoNd"=>"val2",
"third"=>array("third-INner1"=>"val3.1"),
"fourth"=>array("FouthINNer1"=>"val4.1","FourthINNER2"=>"val4.2"));
print_r($array);
$key=array_keys($array);
foreach($key as $ki)
{
    $klower=strtolower($ki);
    $val=$array[$ki];
    if(is_array($val))
    {
        foreach($val as $kinner=>$vinner)
        {

            $tl=strtolower($kinner);
            unset($val[$kinner]);
            $val[$tl]=$vinner;
        }
    }
    unset($array[$ki]);
    $array[$klower]=$val;
}
echo "<br> After Changing Key to-LowerCase : <br>";
print_r($array);

?>
Lus Henrique Pessoa 24-Sep-2010 06:03
<?php

// improving the previous script
function ack_r3(&$array, $case=CASE_LOWER, $flag_rec=false)
    {
      $array = array_change_key_case($array, $case);
      if ( $flag_rec ) {
        foreach ($array as $key => $value) {
            if ( is_array($value) ) {
                ack_r3($array[$key], $case, true);
            }
        }
      }
    }

// same of array_change_key_case
$arr = array('ID' => 1, 'NAME'=> 'Luis', 'Contact' => array('PHONE' => '3010-7148', 'E-MAIL' => 'luis@net.com') );
    ack_r3($arr, CASE_LOWER,false);
    var_dump($arr);

    $arr = array('ID' => 1, 'NAME'=> 'Luis', 'Contact' => array('PHONE' => '3010-7148', 'E-MAIL' => 'luis@net.com') );
    ack_r3($arr, CASE_LOWER,true);
    var_dump($arr);

?>

outputs:

array(3) {
  ["id"]=>
  int(1)
  ["name"]=>
  string(4) "Luis"
  ["contact"]=>
  array(2) {
    ["PHONE"]=>
    string(9) "3010-7148"
    ["E-MAIL"]=>
    string(12) "luis@net.com"
  }
}
array(3) {
  ["id"]=>
  int(1)
  ["name"]=>
  string(4) "Luis"
  ["contact"]=>
  array(2) {
    ["phone"]=>
    string(9) "3010-7148"
    ["e-mail"]=>
    string(12) "luis@net.com"
  }
}
Luis Henrique Pessoa 23-Sep-2010 06:25
Script to change case of sub-arrays keys:

<?php

    function arrKey2Lower(&$arrVals) {
        foreach( $arrVals as $key => $item ) {
            $key2 = strtolower($key);
            if ( $key2 != $key) {
                unset($arrVals[$key]);
                $arrVals[$key2]=$item;
                $key=$key2;
            }

            if ( is_array($item) ) { arrKey2Lower($arrValores[$key]); }
        }
    }

    $arr = array('ID' => 1, 'NAME'=> 'Luis', 'Contact' => array('PHONE' => '3010-7148', 'E-MAIL' => 'luis@net.com') );
    arrKey2Lower($arr);
    var_dump($arr);

?>

outputs:

array(3) {
  ["id"]=>
  int(1)
  ["name"]=>
  string(4) "Luis"
  ["contact"]=>
  array(2) {
    ["phone"]=>
    string(9) "3010-7148"
    ["e-mail"]=>
    string(12) "luis@net.com"
  }
}
info at microweb dot lt 29-Nov-2009 11:07
Simple function to change multidimensional array's all values to uppercase. If you would like to change to lowercase then change "mb_strtoupper" to  "mb_strtolower". It works perfect for me ;)

<?php
function change_case_recursive($arr){
    foreach ($arr as $key=>$val){
        if (!is_array($arr[$key])){
            $arr[$key]=mb_strtoupper($arr[$key]);
        }else{
            $arr[$key]=change_case_recursive($arr[$key]);
        }
    }
    return $arr;
}
?>
david at ramaboo dot com 02-Feb-2009 12:22
Same as array_change_key_case only with the values. This should really be part of PHP!

<?php
/**
 * @brief Returns an array with all values lowercased or uppercased.
 * @return array Returns an array with all values lowercased or uppercased.
 * @param object $input The array to work on
 * @param int $case [optional] Either \c CASE_UPPER or \c CASE_LOWER (default).
 */
function array_change_value_case(array $input, $case = CASE_LOWER) {
    switch ($case) {
        case CASE_LOWER:
            return array_map('strtolower', $input);
            break;
        case CASE_UPPER:
            return array_map('strtoupper', $input);
            break;
        default:
            trigger_error('Case is not valid, CASE_LOWER or CASE_UPPER only', E_USER_ERROR);
            return false;
    }
}
?>
charles 13-Jan-2009 08:38
<?php
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>
anon at ymous dot com 02-Dec-2008 12:19
Below is a recursive version of this function.

<?php
    /**
     * A recursive array_change_key_case function.
     * @param array $input
     * @param integer $case
     */
    function array_change_key_case_recursive($input, $case = null){
        if(!is_array($input)){
            trigger_error("Invalid input array '{$array}'",E_USER_NOTICE); exit;
        }
        // CASE_UPPER|CASE_LOWER
        if(null === $case){
            $case = CASE_LOWER;
        }
        if(!in_array($case, array(CASE_UPPER, CASE_LOWER))){
            trigger_error("Case parameter '{$case}' is invalid.", E_USER_NOTICE); exit;
        }
        $input = array_change_key_case($input, $case);
        foreach($input as $key=>$array){
            if(is_array($array)){
                $input[$key] = array_change_key_case_recursive($array, $case);
            }
        }
        return $input;
    }
?>
andreas dot schuhmacher87 at googlemail dot com 15-Apr-2008 03:01
multibyte and multi-dimensional-array support, have fun!

<?php
    define('ARRAY_KEY_FC_LOWERCASE', 25); //FOO => fOO
    define('ARRAY_KEY_FC_UPPERCASE', 20); //foo => Foo
    define('ARRAY_KEY_UPPERCASE', 15); //foo => FOO
    define('ARRAY_KEY_LOWERCASE', 10); //FOO => foo
    define('ARRAY_KEY_USE_MULTIBYTE', true); //use mutlibyte functions

    /**
    * change the case of array-keys
    *
    * use: array_change_key_case_ext(array('foo' => 1, 'bar' => 2), ARRAY_KEY_UPPERCASE);
    * result: array('FOO' => 1, 'BAR' => 2)
    *
    * @param    array
    * @param    int
    * @return     array
    */
    function array_change_key_case_ext(array $array, $case = 10, $useMB = false, $mbEnc = 'UTF-8') {
        $newArray = array();

        //for more speed define the runtime created functions in the global namespace

        //get function
        if($useMB === false) {
            $function = 'strToUpper'; //default
            switch($case) {
                //first-char-to-lowercase
                case 25:
                    //maybee lcfirst is not callable
                    if(!function_exists('lcfirst'))
                        $function = create_function('$input', '
                            return strToLower($input[0]) . substr($input, 1, (strLen($input) - 1));
                        ');
                    else $function = 'lcfirst';
                    break;

                //first-char-to-uppercase
                case 20:
                    $function = 'ucfirst';
                    break;

                //lowercase
                case 10:
                    $function = 'strToLower';
            }
        } else {
            //create functions for multibyte support
            switch($case) {
                //first-char-to-lowercase
                case 25:
                    $function = create_function('$input', '
                        return mb_strToLower(mb_substr($input, 0, 1, \'' . $mbEnc . '\')) .
                            mb_substr($input, 1, (mb_strlen($input, \'' . $mbEnc . '\') - 1), \'' . $mbEnc . '\');
                    ');

                    break;

                //first-char-to-uppercase
                case 20:
                    $function = create_function('$input', '
                        return mb_strToUpper(mb_substr($input, 0, 1, \'' . $mbEnc . '\')) .
                            mb_substr($input, 1, (mb_strlen($input, \'' . $mbEnc . '\') - 1), \'' . $mbEnc . '\');
                    ');

                    break;

                //uppercase
                case 15:
                    $function = create_function('$input', '
                        return mb_strToUpper($input, \'' . $mbEnc . '\');
                    ');
                    break;

                //lowercase
                default: //case 10:
                    $function = create_function('$input', '
                        return mb_strToLower($input, \'' . $mbEnc . '\');
                    ');
            }
        }

        //loop array
        foreach($array as $key => $value) {
            if(is_array($value)) //$value is an array, handle keys too
                $newArray[$function($key)] = array_change_key_case_ex($value, $case, $useMB);
            elseif(is_string($key))
                $newArray[$function($key)] = $value;
            else $newArray[$key] = $value; //$key is not a string
        } //end loop

        return $newArray;
    }
?>
dot dot dot dot dot alexander at gmail dot com 30-Jan-2008 06:22
Basically this is the function if your version is lower than 4.2.0
<?php
if ( !defined('CASE_LOWER') )define('CASE_LOWER', 0);
if ( !defined('CASE_UPPER') )define('CASE_UPPER', 1);

if(!function_exists("array_change_key_case")){
    function array_change_key_case($input, $case=0){
        if(!is_array($input))return FALSE;
        $product = array();
        foreach($input as $key => $value){
            if($case){ //Upper
                $key2 = (  (is_string($key)) ? (strtoupper($key)) : ($key)  );
                $product[$key2] = $value;
            }
            else{ //Lower
                $key2 = (  (is_string($key)) ? (strtolower($key)) : ($key)  );
                $product[$key2] = $value;
            }
        }
        return $product;
    }/* endfunction array_change_key_case */
}/* endfunction exists array_change_key_case*/
?>
john at doe dot com 26-Sep-2007 09:04
<?php
function array_change_value_case($input, $case = CASE_LOWER)
{
    $aRet = array();

    if (!is_array($input))
    {
        return $aRet;
    }

    foreach ($input as $key => $value)
    {
        if (is_array($value))
        {
            $aRet[$key] = array_change_value_case($value, $case);
            continue;
        }

        $aRet[$key] = ($case == CASE_UPPER ? strtoupper($value) : strtolower($value));
    }

    return $aRet;
}
?>
cm at gameswelt dot de 10-Aug-2007 01:11
I just changed the code a little bit so you havent got a code that repeats itself.

<?php

function array_change_key_case_secure($array = array(), $case = CASE_UPPER){
    $secure = array();
    $functionWrap = array(CASE_UPPER => 'strtoupper',
                            CASE_LOWER => 'strtolower');

    foreach($array as $key => $val){
        if(isset($functionWrap[$key])){
            $key = $functionWrap[$case]($key);
            $secure[$key][] = $val;
        } else {
            die('Not a known Type');
        }
    }

    foreach($secure as $key => $val){
        if(count($secure[$key]) == 1){
            $secure[$key] = $val[0];
        }
    }

    return $secure;
}

$myArray = array('A' => 'Hello',
                    'B' => 'World',
                    'a' => 'how are you?');

print_r($myArray);
$myArray = array_change_key_case_secure($myArray);
print_r($myArray);

/*
Array
(
    [A] => Hello
    [B] => World
    [a] => how are you?
)
Array
(
    [A] => Array
        (
            [0] => Hello
            [1] => how are you?
        )

    [B] => World
)
*/
aidan at php dot net 02-Jun-2004 05:06
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat


배열을 조각으로 나누기

  • ≪ array_change_key_case
  • array_column ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열을 조각으로 나누기

array_chunk

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

array_chunk — 배열을 조각으로 나누기

설명

array array_chunk ( array $input , int $size [, bool $preserve_keys ] )

배열을 size 크기 조각으로 나눕니다. 마지막 조각은 size 원소보다 적은 조각을 가질 수 있습니다.

인수

input

작동할 배열

size

각 조각의 크기

preserve_keys

TRUE로 설정하면 키를 보존합니다. 기본값은 FALSE로 조각 숫자로 다시 인덱스합니다.

반환값

0으로 시작하는 다차원 숫자 인덱스 배열을 반환합니다. 각 차원은 size 원소를 가집니다.

오류/예외

size가 1보다 작으면 E_WARNING이 발생하고 NULL을 반환합니다.

예제

Example #1 array_chunk() 예제

<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>

위 예제의 출력:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )

)
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [2] => c
            [3] => d
        )

    [2] => Array
        (
            [4] => e
        )

)

User Contributed Notes

suisuiruyi at aliyun dot com 03-Jun-2016 06:02
chunk array vertically

$arr    = range(1, 19);
function array_chunk_vertical($arr, $percolnum){
    $n = count($arr);
    $mod    = $n % $percolnum;
    $cols   = floor($n / $percolnum);
    $mod ? $cols++ : null ;
    $re     = array();
    for($col = 0; $col < $cols; $col++){
        for($row = 0; $row < $percolnum; $row++){
            if($arr){
                $re[$row][]   = array_shift($arr);
            }
        }
    }
    return $re;
}
$result = array_chunk_vertical($arr, 6);
foreach($result  as $row){
    foreach($row as $val){
        echo '['.$val.']';
    }
    echo '<br/>';
}
/*
[1][7][13][19]
[2][8][14]
[3][9][15]
[4][10][16]
[5][11][17]
[6][12][18]
 */
normiridium at gmail dot com 17-Apr-2016 07:55
A breakdown by groups with excess:

function array_chunk_greedy($arr, $count){
    $arr = array_chunk($arr, $count);
    if(($k = count($arr)-1) > 0){
        if(count($arr[$k]) < $count){
            $arr[$k-1] = array_merge($arr[$k-1], $arr[$k]);
            unset($arr[$k]);
        }
    }
    return $arr;
}

$arr = range(1, 13);
$arr = array_chunk_greedy($arr, 4);

print_r($arr);

[1,2,3,4,5,6,7,8,9,10,11,12,13] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13]

More examples:

[1,2,3,4,5,6,7,8,9,10,11,12] —> [1,2,3,4] [5,6,7,8] [9,10,11,12]
[1,2,3,4,5,6,7,8,9,10,11,12,13] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13,14]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13,14,15]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] —> [1,2,3,4] [5,6,7,8] [9,10,11,12] [13,14,15,16]

Example report:

$arr = range(1, 45);
$arr = array_chunk_lazy($arr, 10);

$arr = array_map(function($sub_value) {
            return implode('<br>', $sub_value);
        }, $arr);

$title = '<h2>title</h2>';
$arr = $title.implode($title, $arr).$title;

echo $arr;
dustin at fivetechnology dot com 11-Mar-2016 03:22
Had need to chunk an object which implemented ArrayAccess Iterator Countable.  array_chunk wouldn't do it.  Should work for any list of things

<?php
   $listOfThings = array(1,2,3,4,5,6,7,8,9,10,11,12,13);
   print_r(chunk_iterable($listOfThings, 4);

  function chunk_iterable($listOfThings, $size) {
      $chunk = 0;
      $chunks = array_fill(0, ceil(count($listOfThings) / $size) - 1, array());
      $index = 0;
      foreach($listOfThings as $thing) {
        if ($index && $index % $size == 0) $chunk++;
        $chunks[$chunk][] = $thing;
        $index++;
      }
      return $chunks;
  }
?>
siddharthundare at gmail dot com 28-Sep-2015 06:00
<table>
<tr>
<?php

$array_chunkdata = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);
$chunk  = array_chunk($array_chunkdata,5);
$rev_counter = 2;

function for_chunk($chunk_data){

    echo "<td><table>";

      foreach($chunk_data as $key => $chunk_value)
      {
        echo "<tr><td>";
        echo $chunk_value;
        echo "</td></tr>";
      }
      echo "</table></td>";

}

foreach($chunk as $key => $chunk_data)
{
    if($rev_counter%2==0)
    {
      for_chunk($chunk_data);
    }
    else
    {
      $chunk_data =  array_reverse($chunk_data);

      for_chunk($chunk_data);

    }
  $rev_counter++;
}
?>
</tr>
</table>
/*
Output:
1
2
3
4
5

10
9
8
7
6

11
12
13
14
15

20
19
18
17
16

21
22
23
24
25
*/
Anonymous 20-May-2014 07:51
Couldn't get the array_chunk_values() working, so ended up with this implementation:

<?php
function array_chunk_columns($data, $num_columns) {
  $n = count($data);
  $per_column = floor($n / $num_columns);
  $rest = $n % $num_columns;

  $columns = array();
  $index = 0;
  for ($i = 0; $i < $num_columns; $i++) {
    // Add an extra item to each column while the column number is less than the
    // remainder.
    $add_rest = ($rest && ($i < $rest)) ? 1 : 0;
    $number = $per_column + $add_rest;
    $columns[] = array_slice($data, $index, $number);
    $index += $number;
  }

  return $columns;
}

?>
stratboy 16-Sep-2013 07:22
Hi, I've made  a function to split an array into chunks based on columns wanted. For example:

<?php $a = array(1,2,3,4,5,6,7,8); ?>

goal (say, for 3 columns):

<?php
array(
   array(1,2,3),
   array(4,5,6),
   array(7,8)
);
?>

<?php
function get_array_columns($array, $columns){

        $columns_map = array();
        for($i=0; $i<$columns; $i++){ $columns_map[] = 0; }//init columns

        //create map
        $count = count($array);
        $position = 0;
        while($count > 0){
            $columns_map[$position]++;
            $position = ($position < $columns-1) ? ++$position : 0;
            $count--;
        }

        //chunk the array based on map
        $chunked = array();
        foreach($columns_map as $map){
            $chunked[] = array_splice($array,0,$map);
        }

        return $chunked;
    }//end get_array_columns
?>
nate at ruggfamily dot com 20-Jul-2010 06:00
If you just want to grab one chunk from an array, you should use array_slice().
dead dot screamer at seznam dot cz 27-Jan-2009 11:28
This function can be used to reverse effect of <?php array_Chunk()?>.
<?php
function array_Unchunk($array)
{
    return call_User_Func_Array('array_Merge',$array);
}

header('Content-Type: text/plain');
$array=array(
    array(
            'Black Canyon City',
            'Chandler',
            'Flagstaff',
            'Gilbert',
            'Glendale',
            'Globe',
    ),
    array(
            'Mesa',
            'Miami',
            'Phoenix',
            'Peoria',
            'Prescott',
            'Scottsdale',
    ),
    array(
            'Sun City',
            'Surprise',
            'Tempe',
            'Tucson',
            'Wickenburg',
    ),
);
var_Dump(array_Unchunk($array));
?>

Output:
array(17) {
  [0]=>
  string(17) "Black Canyon City"
  [1]=>
  string(8) "Chandler"
  [2]=>
  string(9) "Flagstaff"
  [3]=>
  string(7) "Gilbert"
  [4]=>
  string(8) "Glendale"
  [5]=>
  string(5) "Globe"
  [6]=>
  string(4) "Mesa"
  [7]=>
  string(5) "Miami"
  [8]=>
  string(7) "Phoenix"
  [9]=>
  string(6) "Peoria"
  [10]=>
  string(8) "Prescott"
  [11]=>
  string(10) "Scottsdale"
  [12]=>
  string(8) "Sun City"
  [13]=>
  string(8) "Surprise"
  [14]=>
  string(5) "Tempe"
  [15]=>
  string(6) "Tucson"
  [16]=>
  string(10) "Wickenburg"
}
Rasmus Schultz (http://mindplay.dk) 23-Jan-2009 10:59
Unfortunately, this function only accepts real arrays, not iterable objects... For that, you need this function:

<?php

function break_array($array, $page_size) {

  $arrays = array();
  $i = 0;

  foreach ($array as $index => $item) {
    if ($i++ % $page_size == 0) {
      $arrays[] = array();
      $current = & $arrays[count($arrays)-1];
    }
    $current[] = $item;
  }

  return $arrays;

}

?>
OIS 10-Jun-2008 02:31
Response to azspot at gmail dot com function partition.

$columns = 3;
$citylist = array('Black Canyon City', 'Chandler', 'Flagstaff', 'Gilbert', 'Glendale', 'Globe', 'Mesa', 'Miami', 'Phoenix', 'Peoria', 'Prescott', 'Scottsdale', 'Sun City', 'Surprise', 'Tempe', 'Tucson', 'Wickenburg');
print_r(array_chunk($citylist, ceil(count($citylist) / $columns)));

Output:
Array
(
    [0] => Array
        (
            [0] => Black Canyon City
            [1] => Chandler
            [2] => Flagstaff
            [3] => Gilbert
            [4] => Glendale
            [5] => Globe
        )

    [1] => Array
        (
            [0] => Mesa
            [1] => Miami
            [2] => Phoenix
            [3] => Peoria
            [4] => Prescott
            [5] => Scottsdale
        )

    [2] => Array
        (
            [0] => Sun City
            [1] => Surprise
            [2] => Tempe
            [3] => Tucson
            [4] => Wickenburg
        )

)
azspot at gmail dot com 09-May-2007 01:53
Tried to use an example below (#56022) for array_chunk_fixed that would "partition" or divide an array into a desired number of split lists -- a useful procedure for "chunking" up objects or text items into columns, or partitioning any type of data resource. However, there seems to be a flaw with array_chunk_fixed — for instance, try it with a nine item list and with four partitions. It results in 3 entries with 3 items, then a blank array.

So, here is the output of my own dabbling on the matter:

<?php

function partition( $list, $p ) {
    $listlen = count( $list );
    $partlen = floor( $listlen / $p );
    $partrem = $listlen % $p;
    $partition = array();
    $mark = 0;
    for ($px = 0; $px < $p; $px++) {
        $incr = ($px < $partrem) ? $partlen + 1 : $partlen;
        $partition[$px] = array_slice( $list, $mark, $incr );
        $mark += $incr;
    }
    return $partition;
}

$citylist = array( "Black Canyon City", "Chandler", "Flagstaff", "Gilbert", "Glendale", "Globe", "Mesa", "Miami",
                   "Phoenix", "Peoria", "Prescott", "Scottsdale", "Sun City", "Surprise", "Tempe", "Tucson", "Wickenburg" );
print_r( partition( $citylist, 3 ) );

?>

Array
(
    [0] => Array
        (
            [0] => Black Canyon City
            [1] => Chandler
            [2] => Flagstaff
            [3] => Gilbert
            [4] => Glendale
            [5] => Globe
        )

    [1] => Array
        (
            [0] => Mesa
            [1] => Miami
            [2] => Phoenix
            [3] => Peoria
            [4] => Prescott
            [5] => Scottsdale
        )

    [2] => Array
        (
            [0] => Sun City
            [1] => Surprise
            [2] => Tempe
            [3] => Tucson
            [4] => Wickenburg
        )

)
22-Mar-2006 01:19
Here my array_chunk_values( ) with values distributed by lines (columns are balanced as much as possible) :

<?php
    function array_chunk_vertical($data, $columns) {
        $n = count($data) ;
        $per_column = floor($n / $columns) ;
        $rest = $n % $columns ;

        // The map
        $per_columns = array( ) ;
        for ( $i = 0 ; $i < $columns ; $i++ ) {
            $per_columns[$i] = $per_column + ($i < $rest ? 1 : 0) ;
        }

        $tabular = array( ) ;
        foreach ( $per_columns as $rows ) {
            for ( $i = 0 ; $i < $rows ; $i++ ) {
                $tabular[$i][ ] = array_shift($data) ;
            }
        }

        return $tabular ;
    }

    header('Content-Type: text/plain') ;

    $data = array_chunk_vertical(range(1, 31), 7) ;
    foreach ( $data as $row ) {
        foreach ( $row as $value ) {
            printf('[%2s]', $value) ;
        }
        echo "\r\n" ;
    }

    /*
        Output :

        [ 1][ 6][11][16][20][24][28]
        [ 2][ 7][12][17][21][25][29]
        [ 3][ 8][13][18][22][26][30]
        [ 4][ 9][14][19][23][27][31]
        [ 5][10][15]
    */
?>
magick dit crow ot gmail dit com 16-Oct-2005 06:50
Mistake key did not do what I thought. A patch.

function array_bucket($array,$bucket_size)// bucket filter
{
    if (!is_array($array)) return false;
    $buckets=array_chunk($array,$bucket_size);// chop up array into bucket size units
    $I=0;
    foreach ($buckets as $bucket)
    {
        $new_array[$I++]=array_sum($bucket)/count($bucket);
    }
    return $new_array;// return new array
}
magick dit crow ot gmail dit com 15-Oct-2005 07:58
This function takes each few elements of an array and averages them together. It then places the averages in a new array. It is used to smooth out data. For example lets say you have a years worth of hit data to a site and you want to graph it by the week. Then use a bucket of 7 and graph the functions output.

function array_bucket($array, $bucket_size)  // bucket filter
{
    if (!is_array($array)) return false; // no empty arrays
    $buckets=array_chunk($array,$bucket_size);  // chop up array into bucket size units
    foreach ($buckets as $bucket) $new_array[key($buckets])=array_sum($bucket)/count($bucket);
    return $new_array;  // return new smooth array
}
webmaster at cafe-clope dot net 21-Aug-2005 01:27
based on the same syntax, useful about making columns :

<?php
function array_chunk_fixed($input, $num, $preserve_keys = FALSE) {
    $count = count($input) ;
    if($count)
        $input = array_chunk($input, ceil($count/$num), $preserve_keys) ;
    $input = array_pad($input, $num, array()) ;
    return $input ;
}

$array = array(1, 2, 3, 4, 5) ;
print_r(array_chunk($array, 2)) ;
print_r(array_chunk_fixed($array, 2)) ;
?>

---- array_chunk : fixed number of sub-items ----
Array(
    [0] => Array(
            [0] => 1
            [1] => 2
        )
    [1] => Array(
            [0] => 3
            [1] => 4
        )

    [2] => Array(
            [0] => 5
        )
)

---- array_chunk : fixed number of columns ----
Array(
    [0] => Array(
            [0] => 1
            [1] => 2
            [2] => 3
        )
    [1] => Array(
            [0] => 4
            [1] => 5
        )
)
phpm at nreynolds dot me dot uk 17-Dec-2004 01:21
array_chunk() is helpful when constructing tables with a known number of columns but an unknown number of values, such as a calendar month. Example:

<?php

$values = range(1, 31);
$rows = array_chunk($values, 7);

print "<table>\n";
foreach ($rows as $row) {
    print "<tr>\n";
    foreach ($row as $value) {
        print "<td>" . $value . "</td>\n";
    }
    print "</tr>\n";
}
print "</table>\n";

?>

Outputs:

1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

The other direction is possible too, with the aid of a function included at the bottom of this note. Changing this line:
  $rows = array_chunk($values, 7);

To this:
  $rows = array_chunk_vertical($values, 7);

Produces a vertical calendar with seven columns:

1 6  11 16 21 26 31
2 7  12 17 22 27
3 8  13 18 23 28
4 9  14 19 24 29
5 10 15 20 25 30

You can also specify that $size refers to the number of rows, not columns:
  $rows = array_chunk_vertical($values, 7, false, false);

Producing this:

1 8  15 22 29
2 9  16 23 30
3 10 17 24 31
4 11 18 25
5 12 19 26
6 13 20 27
7 14 21 28

The function:

<?php

function array_chunk_vertical($input, $size, $preserve_keys = false, $size_is_horizontal = true)
{
    $chunks = array();

    if ($size_is_horizontal) {
        $chunk_count = ceil(count($input) / $size);
    } else {
        $chunk_count = $size;
    }

    for ($chunk_index = 0; $chunk_index < $chunk_count; $chunk_index++) {
        $chunks[] = array();
    }

    $chunk_index = 0;
    foreach ($input as $key => $value)
    {
        if ($preserve_keys) {
            $chunks[$chunk_index][$key] = $value;
        } else {
            $chunks[$chunk_index][] = $value;
        }

        if (++$chunk_index == $chunk_count) {
            $chunk_index = 0;
        }
    }

    return $chunks;
}

?>
mick at vandermostvanspijk dot nl 07-Apr-2004 12:02
[Editors note: This function was based on a previous function by gphemsley at nospam users dot sourceforge.net]

For those of you that need array_chunk() for PHP < 4.2.0, this function should do the trick:

<?php
if (!function_exists('array_chunk')) {
    function array_chunk( $input, $size, $preserve_keys = false) {
        @reset( $input );

        $i = $j = 0;

        while( @list( $key, $value ) = @each( $input ) ) {
            if( !( isset( $chunks[$i] ) ) ) {
               $chunks[$i] = array();
            }

            if( count( $chunks[$i] ) < $size ) {
                if( $preserve_keys ) {
                    $chunks[$i][$key] = $value;
                    $j++;
                } else {
                    $chunks[$i][] = $value;
                }
            } else {
                $i++;

                if( $preserve_keys ) {
                    $chunks[$i][$key] = $value;
                    $j++;
                } else {
                    $j = 0;
                    $chunks[$i][$j] = $value;
                }
            }
        }

        return $chunks;
    }
}
?>


Return the values from a single column in the input array

  • ≪ array_chunk
  • array_combine ≫
  • PHP Manual
  • 배열 함수 목록
  • Return the values from a single column in the input array

array_column

(PHP 5 >= 5.5.0, PHP 7)

array_column — Return the values from a single column in the input array

설명

array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )

array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.

인수

input

A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is provided, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the __get() and __isset() magic methods.

column_key

The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array).

index_key

The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name.

반환값

Returns an array of values representing a single column from the input array.

변경점

버전 | 설명
7.0.0 | Added the ability for the input parameter to be an array of objects.

예제

Example #1 Get the column of first names from a recordset

<?php
// Array representing a possible record set returned from a database
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);

$first_names = array_column($records, 'first_name');
print_r($first_names);
?>

위 예제의 출력:

Array
(
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
)

Example #2 Get the column of last names from a recordset, indexed by the "id" column

<?php
// Using the $records array from Example #1
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>

위 예제의 출력:

Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)

Example #3 Get the column of usernames from the public "username" property of an object

<?php

class User
{
    public $username;

    public function __construct(string $username)
    {
        $this->username = $username;
    }
}

$users = [
    new User('user 1'),
    new User('user 2'),
    new User('user 3'),
];

print_r(array_column($users, 'username'));
?>

위 예제의 출력:

Array
(
    [0] => user 1
    [1] => user 2
    [2] => user 3
)

Example #4 Get the column of names from the private "name" property of an object using the magic __get() method.

<?php

class Person
{
    private $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function __get($prop)
    {
        return $this->$prop;
    }

    public function __isset($prop) : bool
    {
        return isset($this->$prop);
    }
}

$people = [
    new Person('Fred'),
    new Person('Jane'),
    new Person('John'),
];

print_r(array_column($people, 'name'));
?>

위 예제의 출력:

Array
(
    [0] => Fred
    [1] => Jane
    [2] => John
)
If __isset() is not provided, then an empty array will be returned.

참고

  • » Recommended userland implementation for PHP lower than 5.5

User Contributed Notes

Carlos Granados 01-Oct-2016 12:41
Here's a neat little snippet for filtering a set of records based on a the value of a column:

<?php

function dictionaryFilterList(array $source, array $data, string $column) : array
{
    $new     = array_column($data, $column);
    $keep     = array_diff($new, $source);

    return array_intersect_key($data, $keep);
 }

// Usage:

$users = [
    ['first_name' => 'Jed', 'last_name' => 'Lopez'],
    ['first_name' => 'Carlos', 'last_name' => 'Granados'],
    ['first_name' => 'Dirty', 'last_name' => 'Diana'],
    ['first_name' => 'John', 'last_name' => 'Williams'],
    ['first_name' => 'Betty', 'last_name' => 'Boop'],
    ['first_name' => 'Dan', 'last_name' => 'Daniels'],
    ['first_name' => 'Britt', 'last_name' => 'Anderson'],
    ['first_name' => 'Will', 'last_name' => 'Smith'],
    ['first_name' => 'Magic', 'last_name' => 'Johnson'],
];

var_dump(dictionaryFilterList(['Dirty', 'Dan'], $users, 'first_name'));

// Outputs:
[
    ['first_name' => 'Jed', 'last_name' => 'Lopez'],
    ['first_name' => 'Carlos', 'last_name' => 'Granados'],
    ['first_name' => 'John', 'last_name' => 'Williams'],
    ['first_name' => 'Betty', 'last_name' => 'Boop'],
    ['first_name' => 'Britt', 'last_name' => 'Anderson'],
    ['first_name' => 'Will', 'last_name' => 'Smith'],
    ['first_name' => 'Magic', 'last_name' => 'Johnson']
]

?>
nino at recgr dot com 27-Sep-2016 05:21
array_column implementation that works on multidimensional arrays (not just 2-dimensional):

<?php
function array_column_recursive(array $haystack, $needle) {
    $found = [];
    array_walk_recursive($haystack, function($value, $key) use (&$found, $needle) {
        if ($key == $needle)
            $found[] = $value;
    });
    return $found;
}

Taken from https://github.com/NinoSkopac/array_column_recursive
miguelfzarth at gmail dot com 18-Aug-2016 11:02
<?php
# for PHP < 5.5
# AND it works with arrayObject AND array of objects

if (!function_exists('array_column')) {
    function array_column($array, $columnKey, $indexKey = null)
    {
        $result = array();
        foreach ($array as $subArray) {
            if (is_null($indexKey) && array_key_exists($columnKey, $subArray)) {
                $result[] = is_object($subArray)?$subArray->$columnKey: $subArray[$columnKey];
            } elseif (array_key_exists($indexKey, $subArray)) {
                if (is_null($columnKey)) {
                    $index = is_object($subArray)?$subArray->$indexKey: $subArray[$indexKey];
                    $result[$index] = $subArray;
                } elseif (array_key_exists($columnKey, $subArray)) {
                    $index = is_object($subArray)?$subArray->$indexKey: $subArray[$indexKey];
                    $result[$index] = is_object($subArray)?$subArray->$columnKey: $subArray[$columnKey];
                }
            }
        }
        return $result;
    }
}
?>
greensea 06-Jul-2016 11:42
<?php
if (!function_exists('array_column')) {
    function array_column($input, $column_key, $index_key = NULL) {
        if (!is_array($input)) {
            trigger_error(__FUNCTION__ . '() expects parameter 1 to be array, ' . gettype($input) . ' given', E_USER_WARNING);
            return FALSE;
        }

        $ret = array();
        foreach ($input as $k => $v) {
            $value = NULL;
            if ($column_key === NULL) {
                $value = $v;
            }
            else {
                $value = $v[$column_key];
            }

            if ($index_key === NULL || !isset($v[$index_key])) {
                $ret[] = $value;
            }
            else {
                $ret[$v[$index_key]] = $value;
            }
        }

        return $ret;
    }
}
?>
benjam 26-Mar-2016 02:20
Note that this function will return the last entry when possible keys are duplicated.

<?php

$array = array(
    array(
        '1-1',
        'one',
        'one',
    ),
    array(
        '1-2',
        'two',
        'one',
    ),
);

var_dump(array_column($array, $value = 0, $index = 1));
var_dump(array_column($array, $value = 0, $index = 2));

// returns:
/*

array (size=2)
  'one' => string '1-1' (length=3)
  'two' => string '1-2' (length=3)

array (size=1)
  'one' => string '1-2' (length=3)

*/
?>
Nolan chou 27-Feb-2016 02:21
if (!function_exists('array_column'))
{
    function array_column($input, $column_key=null, $index_key=null)
    {
        $result = array();
        $i = 0;
        foreach ($input as $v)
        {
            $k = $index_key === null || !isset($v[$index_key]) ? $i++ : $v[$index_key];
            $result[$k] = $column_key === null ? $v : (isset($v[$column_key]) ? $v[$column_key] : null);
        }
        return $result;
    }
}
Anonymous 13-Feb-2016 01:45
I added a little more functionality to the more popular answers here to support the $index_key parameter for PHP < 5.5

<?php
// for php < 5.5
if (!function_exists('array_column')) {
    function array_column($input, $column_key, $index_key = null) {
        $arr = array_map(function($d) use ($column_key, $index_key) {
            if (!isset($d[$column_key])) {
                return null;
            }
            if ($index_key !== null) {
                return array($d[$index_key] => $d[$column_key]);
            }
            return $d[$column_key];
        }, $input);

        if ($index_key !== null) {
            $tmp = array();
            foreach ($arr as $ar) {
                $tmp[key($ar)] = current($ar);
            }
            $arr = $tmp;
        }
        return $arr;
    }
}
?>
kaspar dot wilbuer at web dot de 01-Feb-2016 07:59
If you need to extract more than one column from an array, you can use array_intersect_key on each element, like so:

function array_column_multi(array $input, array $column_keys) {
    $result = array();
    $column_keys = array_flip($column_keys);
    foreach($input as $key => $el) {
        $result[$key] = array_intersect_key($el, $column_keys);
    }
    return $result;
}
antonfedonjuk at gmail dot com 22-Jul-2015 03:26
My version is closer to the original than http://github.com/ramsey/array_column
<?php
/**
 * Provides functionality for array_column() to projects using PHP earlier than
 * version 5.5.
 * @copyright (c) 2015 WinterSilence (http://github.com/WinterSilence)
 * @license MIT
 */
if (!function_exists('array_column')) {
    /**
     * Returns an array of values representing a single column from the input
     * array.
     * @param array $array A multi-dimensional array from which to pull a
     *     column of values.
     * @param mixed $columnKey The column of values to return. This value may
     *     be the integer key of the column you wish to retrieve, or it may be
     *     the string key name for an associative array. It may also be NULL to
     *     return complete arrays (useful together with index_key to reindex
     *     the array).
     * @param mixed $indexKey The column to use as the index/keys for the
     *     returned array. This value may be the integer key of the column, or
     *     it may be the string key name.
     * @return array
     */
    function array_column(array $array, $columnKey, $indexKey = null)
    {
        $result = array();
        foreach ($array as $subArray) {
            if (!is_array($subArray)) {
                continue;
            } elseif (is_null($indexKey) && array_key_exists($columnKey, $subArray)) {
                $result[] = $subArray[$columnKey];
            } elseif (array_key_exists($indexKey, $subArray)) {
                if (is_null($columnKey)) {
                    $result[$subArray[$indexKey]] = $subArray;
                } elseif (array_key_exists($columnKey, $subArray)) {
                    $result[$subArray[$indexKey]] = $subArray[$columnKey];
                }
            }
        }
        return $result;
    }
}
?>
till at etill dot net 29-May-2015 12:32
Some remarks not included in the official documentation.

1) array_column does not support 1D arrays, in which case an empty array is returned.

2) The $column_key is zero-based.

3) If $column_key extends the valid index range an empty array is returned.
marianbucur17 at yahoo dot com 18-May-2015 03:19
If array_column is not available you can use the following function, which also has the $index_key parameter:

if (!function_exists('array_column')) {
    function array_column($array, $column_key, $index_key = null)
    {
        return array_reduce($array, function ($result, $item) use ($column_key, $index_key)
        {
            if (null === $index_key) {
                $result[] = $item[$column_key];
            } else {
                $result[$item[$index_key]] = $item[$column_key];
            }

            return $result;
        }, []);
    }
}
mohanrajnr at gmail dot com 06-May-2015 04:50
if array_column does not exist the below solution will work.

if(!function_exists("array_column"))
{

    function array_column($array,$column_name)
    {

        return array_map(function($element) use($column_name){return $element[$column_name];}, $array);

    }

}
robbieaverill[at]gmail.com 24-Mar-2015 04:51
Another option for older PHP versions (pre 5.5.0) is to use array_walk():

<?php
$array = array(
  array('some' => 'var', 'foo' => 'bar'),
  array('some' => 'var', 'foo' => 'bar'),
  array('some' => 'var', 'foo' => 'bar')
);

array_walk($array, function(&$value, $key, $return) {
  $value = $value[$return];
}, 'foo');

print_r($array);

// Array
// (
//     [0] => bar
//     [1] => bar
//     [2] => bar
// )

?>
kiler129 @ nowhere 10-Jan-2015 11:31
Please note this function accepts 2D-arrays ONLY, and silently returns empty array when non-array argument is provided.

Code:
class testObject {
    public $a = 123;
}
$testArray = [new testObject(), new testObject(), new testObject()];
$result = array_column($testArray, 'a')); //array(0) { }
hypxm at qq dot com 09-Dec-2014 03:36
a simple solution:

function arrayColumn(array $array, $column_key, $index_key=null){
        if(function_exists('array_column ')){
            return array_column($array, $column_key, $index_key);
        }
        $result = [];
        foreach($array as $arr){
            if(!is_array($arr)) continue;

            if(is_null($column_key)){
                $value = $arr;
            }else{
                $value = $arr[$column_key];
            }

            if(!is_null($index_key)){
                $key = $arr[$index_key];
                $result[$key] = $value;
            }else{
                $result[] = $value;
            }

        }

        return $result;
    }
coviex 01-Dec-2014 01:53
Value for existing key in the resulting array is rewritten with new value if it exists in another source sub-array.
myles at smyl dot es 23-Nov-2014 11:52
This didn't work for me recursively and needed to come up with a solution.

Here's my solution to the function:

if ( ! function_exists( 'array_column_recursive' ) ) {
    /**
     * Returns the values recursively from columns of the input array, identified by
     * the $columnKey.
     *
     * Optionally, you may provide an $indexKey to index the values in the returned
     * array by the values from the $indexKey column in the input array.
     *
     * @param array $input     A multi-dimensional array (record set) from which to pull
     *                         a column of values.
     * @param mixed $columnKey The column of values to return. This value may be the
     *                         integer key of the column you wish to retrieve, or it
     *                         may be the string key name for an associative array.
     * @param mixed $indexKey  (Optional.) The column to use as the index/keys for
     *                         the returned array. This value may be the integer key
     *                         of the column, or it may be the string key name.
     *
     * @return array
     */
    function array_column_recursive( $input = NULL, $columnKey = NULL, $indexKey = NULL ) {

        // Using func_get_args() in order to check for proper number of
        // parameters and trigger errors exactly as the built-in array_column()
        // does in PHP 5.5.
        $argc   = func_num_args();
        $params = func_get_args();
        if ( $argc < 2 ) {
            trigger_error( "array_column_recursive() expects at least 2 parameters, {$argc} given", E_USER_WARNING );

            return NULL;
        }
        if ( ! is_array( $params[ 0 ] ) ) {
            // Because we call back to this function, check if call was made by self to
            // prevent debug/error output for recursiveness :)
            $callers = debug_backtrace();
            if ( $callers[ 1 ][ 'function' ] != 'array_column_recursive' ){
                trigger_error( 'array_column_recursive() expects parameter 1 to be array, ' . gettype( $params[ 0 ] ) . ' given', E_USER_WARNING );
            }

            return NULL;
        }
        if ( ! is_int( $params[ 1 ] )
             && ! is_float( $params[ 1 ] )
             && ! is_string( $params[ 1 ] )
             && $params[ 1 ] !== NULL
             && ! ( is_object( $params[ 1 ] ) && method_exists( $params[ 1 ], '__toString' ) )
        ) {
            trigger_error( 'array_column_recursive(): The column key should be either a string or an integer', E_USER_WARNING );

            return FALSE;
        }
        if ( isset( $params[ 2 ] )
             && ! is_int( $params[ 2 ] )
             && ! is_float( $params[ 2 ] )
             && ! is_string( $params[ 2 ] )
             && ! ( is_object( $params[ 2 ] ) && method_exists( $params[ 2 ], '__toString' ) )
        ) {
            trigger_error( 'array_column_recursive(): The index key should be either a string or an integer', E_USER_WARNING );

            return FALSE;
        }
        $paramsInput     = $params[ 0 ];
        $paramsColumnKey = ( $params[ 1 ] !== NULL ) ? (string) $params[ 1 ] : NULL;
        $paramsIndexKey  = NULL;
        if ( isset( $params[ 2 ] ) ) {
            if ( is_float( $params[ 2 ] ) || is_int( $params[ 2 ] ) ) {
                $paramsIndexKey = (int) $params[ 2 ];
            } else {
                $paramsIndexKey = (string) $params[ 2 ];
            }
        }
        $resultArray = array();
        foreach ( $paramsInput as $row ) {
            $key    = $value = NULL;
            $keySet = $valueSet = FALSE;
            if ( $paramsIndexKey !== NULL && array_key_exists( $paramsIndexKey, $row ) ) {
                $keySet = TRUE;
                $key    = (string) $row[ $paramsIndexKey ];
            }
            if ( $paramsColumnKey === NULL ) {
                $valueSet = TRUE;
                $value    = $row;
            } elseif ( is_array( $row ) && array_key_exists( $paramsColumnKey, $row ) ) {
                $valueSet = TRUE;
                $value    = $row[ $paramsColumnKey ];
            }

            $possibleValue = array_column_recursive( $row, $paramsColumnKey, $paramsIndexKey );
            if ( $possibleValue ) {
                $resultArray = array_merge( $possibleValue, $resultArray );
            }

            if ( $valueSet ) {
                if ( $keySet ) {
                    $resultArray[ $key ] = $value;
                } else {
                    $resultArray[ ] = $value;
                }
            }
        }

        return $resultArray;
    }
}
WARrior 03-Oct-2013 09:25
You can also use array_map fucntion if you haven't array_column().

example:

$a = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    )
);

array_column($a, 'last_name');

becomes

array_map(function($element){return $element['last_name'];}, $a);


키를 위한 배열과 값을 위한 배열을 사용하여 배열을 생성

  • ≪ array_column
  • array_count_values ≫
  • PHP Manual
  • 배열 함수 목록
  • 키를 위한 배열과 값을 위한 배열을 사용하여 배열을 생성

array_combine

(PHP 5, PHP 7)

array_combine — 키를 위한 배열과 값을 위한 배열을 사용하여 배열을 생성

설명

array array_combine ( array $keys , array $values )

keys 배열에서의 값들을 키로 사용하고 values 배열에서의 값들을 해당 값들로 사용하여 array를 생성합니다.

인수

keys

키로 사용할 배열. 키로 적합하지 않은 값은 string으로 변환됩니다.

values

값으로 사용할 배열

반환값

결합된 array를 반환합니다. 각 배열의 원소 수가 일치하지 않거나 배열이 비어 있으면 FALSE를 반환합니다.

오류/예외

keys와 values가 비어있거나 원소 수가 일치하지 않으면 E_WARNING이 발생합니다.

예제

Example #1 간단한 array_combine() 예제

<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

위 예제의 출력:

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)

참고

  • array_merge() - 하나 이상의 배열을 병합
  • array_walk() - 배열의 각 원소에 대해서 특정 함수를 적용
  • array_values() - 배열의 모든 값을 반환

User Contributed Notes

elite1228 at 163 dot com 03-Jun-2016 06:57
$item = array();
array_map(
    function($element) use($column_name,$key,&$item){
        $item[$element[$key]] = $element[$column_name];
    },
    $data
);
welcome at el hyphen mustafa 30-Nov-2015 06:46
Further to loreiorg's script
in order to preserve duplicate keys when combining arrays.

I have modified the script to use a closure instead of create_function

Reason: see security issue flagged up in the documentation concerning create_function

<?php

function array_combine_($keys, $values){
    $result = array();

    foreach ($keys as $i => $k) {
     $result[$k][] = $values[$i];
     }

    array_walk($result, function(&$v){
     $v = (count($v) == 1) ? array_pop($v): $v;
     });

    return $result;
}

?>
info at maisuma dot jp 04-Mar-2015 10:00
array_combine() returns NULL instrad of FALSE, when non-array parameters are given (issuing warning).

<?php
  //NULL
  var_dump(array_combine('string',array(42)));
?>
ilanfir at gmail dot com 15-Feb-2015 07:58
I recently had to flip an array and group the elements by value, this snippet will do that:
<?php
function flipAndGroup($input) {
    $outArr = array();
    array_walk($input, function($value, $key) use (&$outArr) {
        if(!isset($outArr[$value]) || !is_array($outArr[$value])) {
            $outArr[$value] = [];
        }
        $outArr[$value][] = $key;
    });
    return $outArr;
}
?>

Example:
<?php
$users_countries = array(
    'username1' => 'US',
    'user2' => 'US',
    'newuser' => 'GB'
);
print_r(flipAndGroup($users_countries));
?>

Returns:
Array
(
    [US] => Array
        (
            [0] => username1
            [1] => user2
        )

    [GB] => Array
        (
            [0] => newuser
        )
)
ismorodin at hotmail dot com 05-Jun-2014 12:48
Function eliminates the error Throws E_WARNING if the number of elements in keys and values ??does not match.

function arrCombine($arr1 = array(),$arr2 = array()){
            if(is_array($arr1) && is_array($arr2)):
                $cntArr1 = count($arr1);
                $cntArr2 = count($arr2);
                $difference = max($cntArr1,$cntArr2) - min($cntArr1,$cntArr2);
                if($cntArr1 > $cntArr2):
                    for ($i=1;$i<=$difference;$i++){
                        $arr2[$cntArr2+$i] = $cntArr2 + 1;
                    }
                    return array_combine($arr1,$arr2);
                elseif($cntArr1 < $cntArr2):
                    for ($i=1;$i<=$difference;$i++){
                        $arr1[$cntArr1+$i] = count($arr1) + 1;
                    }
                    return array_combine($arr1,$arr2);
                else:
                    return array_combine($arr1,$arr2);
                endif;
            endif;
    }
$array = [1,4,5,6,7,8];
$array2 = ['john','smith'];

var_dump( arrCombine($array,$array2 ));

array (size=6)
  1 => string 'john' (length=4)
  4 => string 'smith' (length=5)
  5 => int 3
  6 => int 3
  7 => int 3
  8 => int 3

var_dump( arrCombine($array2,$array) );

array (size=6)
  'john' => int 1
  'smith' => int 4
  3 => int 5
  4 => int 6
  5 => int 7
  6 => int 8
loureirorg at gmail dot com 14-Mar-2013 07:48
If two keys are the same, the second one prevails.

Example:
<?php
print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
    [a] => 2
    [b] => 3
)

But if you need to keep all values, you can use the function below:

<?php
function array_combine_($keys, $values)
{
    $result = array();
    foreach ($keys as $i => $k) {
        $result[$k][] = $values[$i];
    }
    array_walk($result, create_function('&$v', '$v = (count($v) == 1)? array_pop($v): $v;'));
    return    $result;
}

print_r(array_combine_(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
    [a] => Array
        (
            [0] => 1
            [1] => 2
        )

    [b] => 3
)
aaron at aaronholmes dot net 23-Jan-2013 01:54
You can create an array hashset from a flat array, storing both keys and values, with array_combine(). This works with duplicate values in the array too.

<?php
$flat_array = array(
        'one',
        'two',
        'three',
        'three',
        'four',
        'one'
);

$set = array_combine($flat_array, $flat_array);

var_dump($set);
?>

This outputs the following:

array(4) {
    ["one"]=>
        string(3) "one"
    ["two"]=>
        string(3) "two"
    ["three"]=>
        string(5) "three"
    ["four"]=>
        string(4) "four"
}
g.REMOVETHIS.vincendon AT vithemis.com 18-Jul-2012 06:41
I was looking for a function that could combine an array to multiple one, for my MySQL GROUP_CONCAT() query, so I made this function.

<?php
    function array_combine_array(array $keys)
    {
        $arrays = func_get_args();
        $keys = array_shift($arrays);

        /* Checking if arrays are on the same model (array('INDEX'=> array()) or array()) */
        $check = count(array_unique(array_map('is_array',array_map('current',$arrays)))) === 1;
        if (!$check) { trigger_error('Function array_combine_array() expects all parameters to be same type array or array of array',E_USER_NOTICE); return array(); }

        /* Checking the model of arrays, array('INDEX' => array()) or Array() */
        $assocArray = is_array(array_shift(array_map('current',$arrays)));

        /* If empty $Keys is given, we fill an empty array */
        if (empty($keys)) $keys = array_keys(array_fill(0,max(($assocArray) ? array_map('count',array_map('current',$arrays)) : array_map('count',$arrays)),'foo'));

        /* Init */
        $ret=array();$i=0;
        /* Cycling on each keys values, making an offset for each */
        foreach($keys as $v)
        {
            /* Cycling on arrays */
            foreach ($arrays as $k)
            {
                if ($assocArray)
                {
                    /* Getting the index of the element */
                    $key = key($k);
                    /* If the offset exists, we place it */
                    $ret[$v][$key] = isset($k[$key][$i]) ? $k[$key][$i]:false;
                }
                /* Making the array with auto-made index */
                else
                    $ret[$v][] = isset($k[$i]) ? $k[$i]: false;
            }
            /* Getting the next offset */
            $i++;
        }
        return $ret;
    }

    /* Examples */
    $r = array(1,2,4,10);

    $a1 = array('one','two','four','ten');
    $a2 = array('un','deux','quatre','dix');
    $a3 = array('uno','dos','quatro','diez');

     print_r(array_combine_array($r,array('english' => $a1),array('french' => $a2),array('spanish' => $a3))); /* Associative index, associative subarray indexes */
     print_r(array_combine_array($r,$a1,array('french' => $a2),array('spanish' => $a3))); /* Ouputs Error */
     print_r(array_combine_array($r,$a1,$a2,$a3)); /* Associative index, auto-made subarray indexes */
     print_r(array_combine_array(array(),array('english' => $a1),array('french' => $a2),array('spanish' => $a3))); /* Auto-made index, associative subarray indexes */
?>
dejiakala at gmail dot com 27-Oct-2011 12:32
I needed to read CSV files into associative arrays with column headers as keys. Then I ran into a problem when you have empty columns at the end of a row because array_combine returns false if both arrays don't have the same number of elements. This function based on quecoder at gmail's combine_arr() below allowed me to pad either array or not when parsing my CSVs to arrays.
$a is the array of header columns and $b is an array of the current row retrieved with fgetcsv()

<?php

function array_combine_special($a, $b, $pad = TRUE) {
    $acount = count($a);
    $bcount = count($b);
    // more elements in $a than $b but we don't want to pad either
    if (!$pad) {
        $size = ($acount > $bcount) ? $bcount : $acount;
        $a = array_slice($a, 0, $size);
        $b = array_slice($b, 0, $size);
    } else {
        // more headers than row fields
        if ($acount > $bcount) {
            $more = $acount - $bcount;
            // how many fields are we missing at the end of the second array?
            // Add empty strings to ensure arrays $a and $b have same number of elements
            $more = $acount - $bcount;
            for($i = 0; $i < $more; $i++) {
                $b[] = "";
            }
        // more fields than headers
        } else if ($acount < $bcount) {
            $more = $bcount - $acount;
            // fewer elements in the first array, add extra keys
            for($i = 0; $i < $more; $i++) {
                $key = 'extra_field_0' . $i;
                $a[] = $key;
            }

        }
    }

    return array_combine($a, $b);
}
?>
Dan LaManna 18-Sep-2011 05:12
I needed a function that truncated extra values, and only went as far as keys without throwing a warning as array_combine does.

<?php
function safeArrayCombine($keys, $values) {
    $combinedArray = array();

    for ($i=0, $keyCount = count($keys); $i < $keyCount; $i++) {
         $combinedArray[$keys[$i]] = $values[$i];
    }

    return $combinedArray;
}
?>
zequez at gmail dot com 28-Mar-2011 12:51
If two keys are the same, the second one prevails.
Example:
<?php
print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
    [a] => 2
    [b] => 3
)
claude dot pache at gmail dot com 12-May-2010 07:04
array_combine() has a strange bug/misfeature (as of PHP 5.3.2): There is no logical reason for <? array_combine(array(), array()) ?> throwing a warning and returning FALSE, instead of returning <? array() ?> (see http://bugs.php.net/bug.php?id=34857). Here is a quick workaround:
<?php
function array_real_combine($a, $b)
{
    return $a===array() && $b===array() ? array() : array_combine($a, $b);
}
?>
bradentkeith at dot dontspam dot gmail dot com 01-Apr-2010 09:20
I needed a function that would take keys from one unequal array and combine them with the values of another. Real life application:
Select 4 product types.
Each product has a serial.
There are 4 sets of products.

    <?php
        function array_combine2($arr1, $arr2) {
            $count1 = count($arr1);
            $count2 = count($arr2);
            $numofloops = $count2/$count1;

            $i = 0;
            while($i < $numofloops){
                $arr3 = array_slice($arr2, $count1*$i, $count1);
                $arr4[] = array_combine($arr1,$arr3);
                $i++;
            }

            return $arr4;
        }
    ?>

Input:
Array
(
    [0] => SMART Board
    [1] => Projector
    [2] => Speakers
    [3] => Splitter
)
, Array
(
    [0] => serial to smart board1
    [1] => serial to projector 1
    [2] => serial to speakers 1
    [3] => serials to splitter 1
    [4] => serials to smart board 2
    [5] => serials to projector 2
    [6] => serials to speakers 2
    [7] => serials to splitter 2
)

Array
(
    [0] => Array
        (
            [SMART Board] => serial to smart board1
            [Projector] => serial to projector 1
            [Speakers] => serial to speakers 1
            [Splitter] => serials to splitter 1
        )

    [1] => Array
        (
            [SMART Board] => serials to smart board 2
            [Projector] => serials to projector 2
            [Speakers] => serials to speakers 2
            [Splitter] => serials to splitter 2
        )

)
xavier at api-meal dot eu 23-Sep-2009 12:37
<?php
/**
 * Return alternatives defined by values of each parameters.
 *
 * Exemple :
 *
 * array_alternatives(array('foo','bar'), array('baz', 'qux'));
 * array(
 *     array('foo', 'baz'),
 *     array('bar', 'baz'),
 *     array('foo', 'qux'),
 *     array('bar', 'qux'),
 * );
 *
 * array_alternatives(array('a'), array('simple-minded'), array('solution'));
 * array(
 *     array('a', 'simple-minded', 'solution')
 * );
 *
 * array_alternatives(array('a'), array('red', 'blue'), array('car'));
 * array(
 *     array('a', 'red',  'car'),
 *     array('a', 'blue', 'car'),
 * );
 *
 * @param array $first_element
 * @param array $second_element
 * @return array
 * @author Xavier Barbosa
 */
function array_alternatives(array $first_element, array $second_element)
{
    $lists = func_get_args();
    $total_lists = func_num_args();

    for($i=0; $i<$total_lists; $i++)
    {
        $list =& $lists[$i];
        if (is_array($list) === FALSE)
            throw new Exception("Parameter $i is not an array.");
        if (count($list) === 0)
            throw new Exception("Parameter $i has no element.");
        unset($list);
    }

    // Initialize our alternatives
    $alternatives = array();
    foreach($lists[0] as &$value)
    {
        array_push($alternatives, array($value));
        unset($value);
    }
    unset($lists[0]);

    // Process alternatives
    for($i=1; $i<$total_lists; $i++)
    {
        $list =& $lists[$i];

        $new_alternatives = array();
        foreach($list as &$value)
        {
            foreach($alternatives as $_)
            {
                array_push($_, $value);
                array_push($new_alternatives, $_);
            }
        }

        // Rotate references, it's cheaper than copy array like `$alternatives = $new_alternatives;`
        $alternatives =& $new_alternatives;
        unset($new_alternatives, $list, $lists[$i]);
    }

    return $alternatives;
}
?>
quecoder at gmail 26-Aug-2008 09:00
<?php
// If they are not of same size, here is solution:

$abbreviations = array("AL", "AK", "AZ", "AR", "TX", "CA");
$states = array("Alabama", "Alaska", "Arizona", "Arkansas");
function combine_arr($a, $b)
{
    $acount = count($a);
    $bcount = count($b);
    $size = ($acount > $bcount) ? $bcount : $acount;
    $a = array_slice($a, 0, $size);
    $b = array_slice($b, 0, $size);
    return array_combine($a, $b);
}
$combined = combine_arr($abbreviations, $states);
print_r($combined);

//  Output
//  Array ( [AL] => Alabama [AK] => Alaska [AZ] => Arizona
//  [AR] => Arkansas )
?>
J.D.D. 09-Aug-2008 10:31
This may be obvious, but I don't see anything about it on the manual page, so a friendly warning...  The array you are using as keys must have all unique values.  If not, array elements get dropped.

<?php
$arr_notUnique = array('one' , 'one' , 'two');
$arr_b = array('red' , 'green' , 'blue');

$arr_combo = array_combine($arr_notUnique, $arr_b);
?>

Results:  Array ( [one] => green [two] => blue )

NOT:  Array ( [one] => red [one] => green [two] => blue )
Mike Jean 19-Mar-2008 06:59
Khaly's PHP4 code below does not work correctly in all cases. Consider when your array consists of floats:

<?php

$okay = array(0, 10, 20, 30);
$not_okay = array(0, 0.5, 1, 1.5);

$foo = array_combine($okay, $okay);
$bar = array_combine($not_okay, $not_okay);

/*

Results:

$foo = {
  [0]=> int(0)
  [10]=> int(10)
  [20]=> int(20)
  [30]=> int(30)
}

$bar = {
  [0]=> float(0.5)
  [1]=> float(1.5)
}

*/

?>

What can you do? In my case, I was just zipping up some select-box options, so I converted everything in my floats to strings.
Khaly 04-Oct-2007 11:11
This is the function for PHP4 :

<?php

function array_combine($arr1,$arr2) {
   $out = array();
   foreach($arr1 as $key1 => $value1)    {
    $out[$value1] = $arr2[$key1];
   }
   return $out
}

?>
neoyahuu at yahoo dot com 20-Mar-2007 05:36
Some tips for merging same values in an array

<?php
$array1 = array(1,2,3,4,5,6,7,8,9,10,11,12);
$array2 = array(1,2,3,13);

$merged = array_merge($array1,$array2);

// output normal array_merge
echo '<pre>After array_merge :
';
print_r($merged);
echo '</pre>';

// do double flip for merging values in an array
$merged = array_flip($merged);
$merged = array_flip($merged);

// Output after
echo '<pre>After Double Flip :
';
print_r($merged);
echo '</pre>';
?>

Output ::

After array_merge :
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 1
    [13] => 2
    [14] => 3
    [15] => 13
)

After Double Flip :
Array
(
    [12] => 1
    [13] => 2
    [14] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [15] => 13
)


배열 값의 수를 셉니다

  • ≪ array_combine
  • array_diff_assoc ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열 값의 수를 셉니다

array_count_values

(PHP 4, PHP 5, PHP 7)

array_count_values — 배열 값의 수를 셉니다

설명

array array_count_values ( array $input )

array_count_values()는 input 배열값을 키로 사용하고 input 배열의 각 값의 빈도수를 값으로 갖는 배열을 반환한다.

인수

input

값의 수를 셀 배열

반환값

input에서 값을 키로 사용하고, 그 수를 값으로 사용하는 연관 배열을 반환합니다.

오류/예외

string이나 integer가 아닌 모든 원소에 대해서 E_WARNING이 발생합니다.

예제

Example #1 array_count_values() 예제

<?php
$array = array (1, "hello", 1, "world", "hello");
print_r(array_count_values ($array));
?>

위 예제의 출력:

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)

참고

  • count() - 배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다
  • array_unique() - 배열에서 중복된 값을 제거
  • array_values() - 배열의 모든 값을 반환
  • count_chars() - 문자열 안에 사용한 문자에 대한 정보를 반환

User Contributed Notes

dhairya lakhera 17-Feb-2016 06:33
$array = array(1, true, 1, true, false,NULL);
var_dump(array_count_values($array));

//Warning: array_count_values(): Can only count STRING and INTEGER values!
jakubplus at gmail dot com 07-Apr-2014 08:36
In order to apply array_map with callback checking for localised values like city name, country name, you have to provide some sort of comparison array.

Here's an example of array_count_values for Polish city names. Just switch array_keys / array_values in order to obtain lowercase / uppercase.

$arr = array('Warsaw', 'Warsaw', 'Wrocław', 'Pozna?', 'KrakOw', 'waRsaw', 'Gda?sk', 'pozna?', 'WROCŁAW', 'krakow', 'GDA?SK');
$lowertoupperpolish = array(
    '?' => '?',
    '?' => '?',
    '?' => '?',
    'ł' => 'Ł',
    '?' => '?',
    'o' => 'O',
    '?' => '?',
    '?' => '?',
    '?' => '?',
);
function lowers($n) {
    global $lowertoupperpolish;
    return strtolower(str_replace(array_values($lowertoupperpolish), array_keys($lowertoupperpolish), $n));
}
$result = array_count_values(array_map('lowers', $arr));

print_r($result); ...prints:

Array ( [warsaw] => 3 [wrocław] => 2 [pozna?] => 2 [krakow] => 2 [gda?sk] => 2 )
Dominic Vonk 14-Dec-2013 01:11
The case-insensitive version:

<?php
function array_count_values_ci($array) {
    $newArray = array();
    foreach ($array as $values) {
        if (!array_key_exists(strtolower($values), $newArray)) {
            $newArray[strtolower($values)] = 0;
        }
        $newArray[strtolower($values)] += 1;
    }
    return $newArray;
}
?>
szczepan.krolgmail.c0m 17-Jan-2010 08:21
Here is a Version with one or more arrays, which have similar values in it:
Use $lower=true/false to ignore/set case Sensitiv.

<?php

$ar1[] = array("red","green","yellow","blue");
$ar1[] = array("green","yellow","brown","red","white","yellow");
$ar1[] = array("red","green","brown","blue","black","yellow");
#$ar1= array("red","green","brown","blue","black","red","green"); // Possible with one or multiple Array

$res = array_icount_values ($ar1);
print_r($res);

function array_icount_values($arr,$lower=true) {
     $arr2=array();
     if(!is_array($arr['0'])){$arr=array($arr);}
     foreach($arr as $k=> $v){
      foreach($v as $v2){
      if($lower==true) {$v2=strtolower($v2);}
      if(!isset($arr2[$v2])){
          $arr2[$v2]=1;
      }else{
           $arr2[$v2]++;
           }
    }
    }
    return $arr2;
}
/*
Will print:
Array
(
    [red] => 3
    [green] => 3
    [yellow] => 4
    [blue] => 2
    [brown] => 2
    [white] => 1
    [black] => 1
)
*/
?>
bluej100@gmail 25-Mar-2008 10:37
byron at byronrode dot co dot za, here are some benchmarks.

<?php
$haystack = Array();
for ($i = 0; $i < 1000000; $i++) {
  $haystack[] = rand(1, 2000);
}

$needle = rand(1, 2000);

echo "__array_count_values()__\n";
$start = microtime(true);
$startmem = memory_get_usage();
$counts = array_count_values($haystack);
$mem = memory_get_usage()-$startmem;
echo 'Count:'.$counts[$needle]."\n";
echo 'Time:'.(microtime(true) - $start)."\n";
echo 'Memory:'.$mem."\n\n";

echo "__array_keys()__\n";
$start = microtime(true);
$startmem = memory_get_usage();
$keys = array_keys($haystack, $needle);
$mem = memory_get_usage()-$startmem;
echo 'Count:'.count($keys)."\n";
echo 'Time:'.(microtime(true) - $start)."\n";
echo 'Memory:'.$mem."\n\n";

echo '__$needle_array[]__'."\n";
$start = microtime(true);
$startmem = memory_get_usage();
$x = count($haystack);
for($i = 0; $i < $x; $i++){
  if($haystack[$i] == $needle){
    $needle_array[] = $haystack[$i];
  }
}
$mem = memory_get_usage()-$startmem;
$number_of_instances = count($needle_array);
echo 'Count:'.$number_of_instances."\n";
echo 'Time:'.(microtime(true) - $start)."\n";
echo 'Memory:'.$mem."\n\n";

echo '__$number_of_instances++__'."\n";
$start = microtime(true);
$startmem = memory_get_usage();
$x = count($haystack);
$number_of_instances = 0;
for($i = 0; $i < $x; $i++){
  if($haystack[$i] == $needle){
    $number_of_instances++;
  }
}
$mem = memory_get_usage()-$startmem;
echo 'Count:'.$number_of_instances."\n";
echo 'Time:'.(microtime(true) - $start)."\n";
echo 'Memory:'.$mem."\n\n";
?>

[www]mytemp$ php array_count_test.php
__array_count_values()__
Count:515
Time:0.0607650279999
Memory:120328

__array_keys()__
Count:515
Time:0.0869138240814
Memory:33016

__$needle_array[]__
Count:515
Time:0.259949922562
Memory:24792

__$number_of_instances++__
Count:515
Time:0.258481025696
Memory:0

However, when you use an array of strings by calling md5(rand(1, 2000)), the performance boosts become less significant:

__array_count_values()__
Count:499
Time:0.491794109344
Memory:184328

__array_keys()__
Count:499
Time:0.36399102211
Memory:30072

__$needle_array[]__
Count:499
Time:0.568728923798
Memory:22104

__$number_of_instances++__
Count:499
Time:0.574353933334
Memory:0

Results are similar for string->string haystacks with foreach traversal.
Hayley Watson 13-Mar-2008 08:37
Yet Another case-insensitive version of array_count_values()

<?php
$ar = array('J. Karjalainen', 'J. Karjalainen', 60, '60', 'J. Karjalainen', 'j. karjalainen', 'Fastway', 'FASTWAY', 'Fastway', 'fastway', 'YUP');

$ar = array_count_values(array_map('strtolower', $ar));
?>
majerm at gmail dot com 18-Jun-2007 04:50
<?
function array_icount_values($array) {
    $ret_array = array();
    foreach($array as $value) $ret_array[strtolower($value)]++;
    return $ret_array;
}

$ar = array('J. Karjalainen', 'J. Karjalainen', 60, '60', 'J. Karjalainen', 'j. karjalainen', 'Fastway', 'FASTWAY', 'Fastway', 'fastway', 'YUP');
$ar = array_icount_values($ar);
?>

this prints:

Array
(
    [j. karjalainen] => 4
    [60] => 2
    [fastway] => 4
    [yup] => 1
)
rabies dot dostojevski at gmail dot com 21-Feb-2007 10:40
I couldn't find a function for counting the values with case-insensitive matching, so I wrote a quick and dirty solution myself:

<pre><?php
function array_icount_values($array) {
    $ret_array = array();
    foreach($array as $value) {
        foreach($ret_array as $key2 => $value2) {
            if(strtolower($key2) == strtolower($value)) {
                $ret_array[$key2]++;
                continue 2;
            }
        }
        $ret_array[$value] = 1;
    }
    return $ret_array;
}

$ar = array('J. Karjalainen', 'J. Karjalainen', 60, '60', 'J. Karjalainen', 'j. karjalainen', 'Fastway', 'FASTWAY', 'Fastway', 'fastway', 'YUP');
$ar2 = array_count_values($ar); // Normal matching
$ar = array_icount_values($ar); // Case-insensitive matching
print_r($ar2);
print_r($ar);
?></pre>

This prints:

Array
(
    [J. Karjalainen] => 3
    [60] => 2
    [j. karjalainen] => 1
    [Fastway] => 2
    [FASTWAY] => 1
    [fastway] => 1
    [YUP] => 1
)
Array
(
    [J. Karjalainen] => 4
    [60] => 2
    [Fastway] => 4
    [YUP] => 1
)

I don't know how efficient it is, but it seems to work. Needed this function in one of my scripts and thought I would share it.
coda at bobandgeorge dot com 14-Oct-2005 02:52
alwaysdrunk's comment only works if you can trust the client web browser. Using this function doesn't validate that every necessary field exists -- only that every field that was submitted has a value in it. Thus if an attacker wished to force a null value into one of the fields, he could (rather easily) construct a modified form without the field and submit THAT.

Besides, you really ought to be validating each field anyway if you're taking user input.
[Mr.A] 06-Mar-2005 11:59
I find a very simple solution to count values in multidimentional arrays (example for 2 levels) :

foreach ($array as $a) {
  foreach ($a as $b) {
    $count_values[$b]++;
  }
}
pmarcIatIgeneticsImedIharvardIedu 30-Jan-2003 02:42
array_count_values function does not work on multidimentional arrays.
If $score[][] is a bidimentional array, the command
"array_count_values ($score)" return the error message "Warning: Can only count STRING and INTEGER values!".
manuzhai (AT) php (DOT) net 16-Nov-2002 03:45
You might use serialize() to serialize your objects before analyzing their frequency. :)


추가적인 인덱스 확인과 함께 배열 차이를 계산

  • ≪ array_count_values
  • array_diff_key ≫
  • PHP Manual
  • 배열 함수 목록
  • 추가적인 인덱스 확인과 함께 배열 차이를 계산

array_diff_assoc

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

array_diff_assoc — 추가적인 인덱스 확인과 함께 배열 차이를 계산

설명

array array_diff_assoc ( array $array1 , array $array2 [, array $... ] )

array1를 array2에 비교하고 차이를 반환합니다. array_diff()와 달리 배열 키를 비교에 사용합니다.

인수

array1

비교할 원본 배열

array2

비교할 대상 배열

...

추가 대상 배열

반환값

다른 배열에 존재하지 않는 array1의 모든 값을 가지는 array를 반환합니다.

예제

Example #1 array_diff_assoc() 예제

이 예제에서 "a" => "green" 쌍이 두 배열에 모두 존재하므로 함수에서 출력하지 않습니다. 이와 달리, 0 =>> "red" 쌍은 출력하는데, 두번째 인수의 "red"는 키가 1이기 때문입니다.

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>

위 예제의 출력:

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

Example #2 array_diff_assoc() 예제

key => value 쌍에서 두 값은 (string) $elem1 === (string) $elem2 일 때만 동일하다고 간주합니다. 즉, 엄격한 확인을 하므로 문자열 표현이 동일해야 합니다.

<?php
$array1 = array(0, 1, 2);
$array2 = array("00", "01", "2");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>

위 예제의 출력:

Array
(
    [0] => 0
    [1] => 1
    )

주의

Note: 이 함수는 n차원 배열의 1차원만 확인합니다. 물론, 깊은 차원은 array_diff_assoc($array1[0], $array2[0]);처럼 확인할 수 있습니다.

참고

  • array_diff() - 배열 차이를 계산
  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산

User Contributed Notes

francois-php at ceonizme dot fr 29-Aug-2014 03:27
to chinello at gmail dot com
I've had to use your function but it showed that the use of isset can't differentiate the NULL values from not setted values.

Here's a version that takes care of this subtility.

<?php
function array_diff_assoc_recursive($array1, $array2)
{
    $difference = NULL;
    foreach($array1 as $key => $value)
    {
        if(is_array($value))
        {
            if(!array_key_exists($key, $array2))
            {
                $difference[$key] = $value;
            }
            elseif(!is_array($array2[$key]))
            {
                $difference[$key] = $value;
            }
            else
            {
                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                if($new_diff != FALSE)
                {
                    $difference[$key] = $new_diff;
                }
            }
        }
        elseif(!array_key_exists($key, $array2) || $array2[$key] != $value)
        {
            $difference[$key] = $value;
        }
    }
    return !isset($difference) ? 0 : $difference;
}
?>

Hope that helps
Francois
shadow_games at abv dot bg 12-Jun-2014 12:45
there is a functiont that i searched long enough now i have created it so someone else to find it if he need it ;]
<?php
    function compare_two_object_recursive($object_1, $object_2, $object_1_Identifier = false, $object_2_Identifier = false){
        $object1 = (array)$object_1;
        $object2 = (array)$object_2;
        $object3 = array();

        $o1i = $object_1_Identifier ? $object_1_Identifier : 1;
        $o2i = $object_2_Identifier ? $object_2_Identifier : 2;

        foreach($object1 as $key => $value){
            if(is_object($object1[$key])){
                $object1[$key] = (array)$object1[$key];
                $object2[$key] = (array)$object2[$key];
                $object3[$key] = (object)compare_two_object_recursive($object1[$key], $object2[$key], $o1i, $o2i);
            }elseif(is_array($object1[$key])){
                $object3[$key] = compare_two_object_recursive($object1[$key], $object2[$key], $o1i, $o2i);
            }else{
                if($object1[$key] == $object2[$key]){
                    $object3[$key]['comparison_status'] = "SAME";
                }else{
                    $object3[$key]['comparison_status'] = "NOT THE SAME";
                    $object3[$key][$o1i] = $object1[$key];
                    $object3[$key][$o2i] = $object2[$key];
                }
            }
        }
        return $object3;
    }
?>
dev5er6 at gmail dot com 13-Apr-2014 01:01
Works more like the original function:

<?php

function array_diff_assoc_recursive ( )
{
    $args = func_get_args ( );
    $diff = array ( );
    foreach ( array_shift ( $args ) as $key => $val )
    {
        for ( $i = 0, $j = 0, $tmp = array ( $val ) , $count = count ( $args ); $i < $count; $i++ )
            if ( is_array ( $val ) )
                if ( !isset ( $args[$i][$key] ) || !is_array ( $args[$i][$key] ) || empty( $args[$i][$key] ) )
                    $j++;
                else
                    $tmp[] = $args[$i][$key];
            elseif ( ! array_key_exists ( $key, $args[$i] ) || $args[$i][$key] !== $val )
                $j++;
        if ( is_array ( $val ) )
        {
            $tmp = call_user_func_array ( __FUNCTION__, $tmp );
            if ( ! empty ( $tmp ) ) $diff[$key] = $tmp;
            elseif ( $j == $count ) $diff[$key] = $val;
        }
        elseif ( $j == $count && $count ) $diff[$key] = $val;
    }

    return $diff;
}

?>
telefoontoestel at hotmail dot com 04-Feb-2014 06:11
an earlier post for recursive array_diff_assoc failed because isset returned false on an array element containing a null value. I updated the code so it compares null values too.

<?php
function array_diff_assoc_recursive($array1, $array2)
{
    foreach($array1 as $key => $value)
    {
        if(is_array($value))
        {
              if(!isset($array2[$key]))
              {
                  $difference[$key] = $value;
              }
              elseif(!is_array($array2[$key]))
              {
                  $difference[$key] = $value;
              }
              else
              {
                  $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                  if($new_diff != FALSE)
                  {
                        $difference[$key] = $new_diff;
                  }
              }
          }
          elseif(!array_key_exists($key, $array2) || $array2[$key] != $value)
          {
              $difference[$key] = $value;
          }
    }
    return !isset($difference) ? 0 : $difference;
}
?>
Michael Richey 04-May-2013 10:43
If you're looking for a true array_diff_assoc, comparing arrays to determine the difference between two, finding missing values from both, you can use this along with array_merge.

$a = array('a'=>1,'b'=>2,'c'=>3);
$b = array('a'=>1,'b'=>2,'d'=>4);
print_r(array_diff_assoc($a,$b));
// returns:
array
(
    [c] => 3
)

print_r(array_diff_assoc($b,$a));
// returns
array
(
    [d] => 4
)

print_r(array_merge(array_diff_assoc($a,$b),array_diff_assoc($b,$a)));
// returns
array
(
    [c] => 3
    [d] => 4
)
Giosh 15-Mar-2013 08:15
The array_diff_assoc_array from "chinello at gmail dot com" (and others) will not work for arrays with null values. That's because !isset is true when an array key doesn't exists or is set to null.

(sorry for the changed indent-style)
<?php
function array_diff_assoc_recursive($array1, $array2) {
    $difference=array();
    foreach($array1 as $key => $value) {
        if( is_array($value) ) {
            if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
                $difference[$key] = $value;
            } else {
                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                if( !empty($new_diff) )
                    $difference[$key] = $new_diff;
            }
        } else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) {
            $difference[$key] = $value;
        }
    }
    return $difference;
}
?>

And here an example (note index 'b' in the output):
<?php
$a1=array( 'a' => 0, 'b' => null, 'c' => array( 'd' => null ) );
$a2=array( 'a' => 0, 'b' => null );

var_dump( array_diff_assoc_recursive( $a1, $a2 ) );
var_dump( chinello_array_diff_assoc_recursive( $a1, $a2 ) );
?>
array(1) {
  ["c"]=>
  array(1) {
    ["d"]=>
    NULL
  }
}

array(2) {
  ["b"]=>
  NULL
  ["c"]=>
  array(1) {
    ["d"]=>
    NULL
  }
}
news_yodpeirs at thoftware dot de 12-Jul-2011 01:25
A quite simple (yet not very efficient) way to compare the first level of arrays which have values that are not strings:
array_map('unserialize',array_diff_assoc(array_map('serialize',$arr1),array_map('serialize',$arr2)))
Might be useful for debugging (that's what I use it for).
jrajpu10 at gmail dot com 25-Oct-2008 09:57
array_diff_assoc can also be used to find the duplicates in an array

<?php
$arr = array('1','2','3','4','3','2','5');
$uniques = array_unique($arr);
// array_diff will not work here, array_diff_assoc works as it takes the key // in account.
$dups = array_diff_assoc($arr, $uniques);

print_r($dups);
?>

Note: The index of the $dups is not in strict sequential order as expected by C programmer.
cedric at daneel dot net 21-May-2007 03:01
To diff between n-dimensional array, juste use this :

<?php
function array_diff_values($tab1, $tab2)
    {
    $result = array();
    foreach($tab1 as $values) if(! in_array($values, $tab2)) $result[] = $values;
    return $result;
    }
?>
contact at pascalopitz dot com 11-Apr-2007 01:14
The direction of the arguments does actually make a difference:

<?php
$a = array(
    'x' => 'x',
    'y' => 'y',
    'z' => 'z',
    't' => 't',
);

$b = array(
    'x' => 'x',
    'y' => 'y',
    'z' => 'z',
    't' => 't',
    'g' => 'g',
);

print_r(array_diff_assoc($a, $b));
print_r(array_diff_assoc($b, $a));
?>

echoes:

Array
(
)
Array
(
    [g] => g
)
chinello at gmail dot com 19-Mar-2007 07:33
The following will recursively do an array_diff_assoc, which will calculate differences on a multi-dimensional level.  This not display any notices if a key don't exist and if error_reporting is set to E_ALL:

<?php
function array_diff_assoc_recursive($array1, $array2)
{
    foreach($array1 as $key => $value)
    {
        if(is_array($value))
        {
              if(!isset($array2[$key]))
              {
                  $difference[$key] = $value;
              }
              elseif(!is_array($array2[$key]))
              {
                  $difference[$key] = $value;
              }
              else
              {
                  $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                  if($new_diff != FALSE)
                  {
                        $difference[$key] = $new_diff;
                  }
              }
          }
          elseif(!isset($array2[$key]) || $array2[$key] != $value)
          {
              $difference[$key] = $value;
          }
    }
    return !isset($difference) ? 0 : $difference;
}
?>

[NOTE BY danbrown AT php DOT net: This is a combination of efforts from previous notes deleted.  Contributors included (Michael Johnson), (jochem AT iamjochem DAWT com), (sc1n AT yahoo DOT com), and (anders DOT carlsson AT mds DOT mdh DOT se).]
Alexander Podgorny 30-May-2006 11:30
NOTE: the diff_array also removes all the duplicate values that match to the values in the second array:

<?php
    $array1 = array("a","b","c","a","a");
    $array2 = array("a");

    $diff = array_diff($array1,$array2);

    // yields: array("b","c") the duplicate "a" values are removed
?>
benjamin at moonfactory dot co dot jp 12-Jan-2005 06:56
Hi all,
For php versions < 4.3...

<?php
/**
 * array_diff_assoc for version < 4.3
 **/
if (!function_exists('array_diff_assoc'))
{
    function array_diff_assoc($a1, $a2)
    {
        foreach($a1 as $key => $value)
        {
            if(isset($a2[$key]))
            {
                if((string) $value !== (string) $a2[$key])
                {
                     $r[$key] = $value;
                }
            }else
            {
                $r[$key] = $value;
            }
        }
        return $r ;
    }
}

?>
carl at thep dot lu dot se 09-May-2003 11:55
To unset elements in an array if you know the keys but not the values, you can do:

<?php
$a = array("foo", "bar", "baz", "quux");
$b = array(1, 3); // Elements to get rid of

foreach($b as $e)
  unset($a[$e]);
?>

Of course this makes most sense if $b has many elements or is dynamically generated.


Computes the difference of arrays using keys for comparison

  • ≪ array_diff_assoc
  • array_diff_uassoc ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the difference of arrays using keys for comparison

array_diff_key

(PHP 5 >= 5.1.0, PHP 7)

array_diff_key — Computes the difference of arrays using keys for comparison

설명

array array_diff_key ( array $array1 , array $array2 [, array $... ] )

Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

인수

array1

The array to compare from

array2

An array to compare against

...

More arrays to compare against

반환값

Returns an array containing all the entries from array1 whose keys are not present in any of the other arrays.

예제

Example #1 array_diff_key() example

The two keys from the key => value pairs are considered equal only if (string) $key1 === (string) $key2 . In other words a strict type check is executed so the string representation must be the same.

<?php
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

var_dump(array_diff_key($array1, $array2));
?>

위 예제의 출력:

array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}

주의

Note:

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_key($array1[0], $array2[0]);.

참고

  • array_diff() - 배열 차이를 계산
  • array_udiff() - 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산
  • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_diff_ukey() - Computes the difference of arrays using a callback function on the keys for comparison
  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_key() - Computes the intersection of arrays using keys for comparison
  • array_intersect_ukey() - Computes the intersection of arrays using a callback function on the keys for comparison

User Contributed Notes

rhallsey at yahoo dot com 13-Sep-2014 09:45
Here's a simple function that returns true if all keys in the first array are found in the second array, and false if they aren't.

function same_keys ($a1, $a2) {
    $same = false;
    if (!array_diff_key($a1, $a2)) {
        $same = true;
        foreach ($a1 as $k => $v) {
            if (is_array($v) && !same_keys($v, $a2[$k])) {
                $same = false;
                break;
            }
        }
    }
    return $same;
}

To check if two arrays have the same structure, ignoring values, execute the function twice, the second time with the arguments reversed.
gk at anuary dot com 30-Jun-2013 12:40
Improved recursive version.

<?php
/**
 * @author Gajus Kuizinas <gk@anuary.com>
 * @version 1.0.0 (2013 03 19)
 */
function array_diff_key_recursive (array $arr1, array $arr2) {
    $diff = array_diff_key($arr1, $arr2);
    $intersect = array_intersect_key($arr1, $arr2);

    foreach ($intersect as $k => $v) {
        if (is_array($arr1[$k]) && is_array($arr2[$k])) {
            $d = array_diff_key_recursive($arr1[$k], $arr2[$k]);

            if ($d) {
                $diff[$k] = $d;
            }
        }
    }

    return $diff;
}
?>

An up to date version is maintained at https://github.com/gajus/flow/blob/master/flow.inc.php#L337.
division-par-zero at zilon dot net 03-Oct-2008 04:02
you can use this function for return the difference of two array !

<?php
function array_unique_diff_key ($array1, $array2)
{
  if (is_array($array1) && is_array($array2))
    return array_diff_key($array1, $array2) + array_diff_key($array2, $array1);
  else if (is_array($array1)) return $array1;
  else if (is_array($array2)) return $array2;
  else return array();
}
?>
Ashton 02-Apr-2008 10:33
To return the unique elements (those with a key that exists only once in either array but not in both) try:
function array_unique_diff ($array1, $array2)
{
  array_merge(array_diff_key($array1, $array2), array_diff_key($array2, $array1));
}

Example:
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

  array_diff_key($array1, $array2)

returns

  array ( 'red' => 2, 'purple' => 4 )

  array_diff_key($array2, $array1)

returns

  array ( 'yellow' => 7, 'cyan' => 8, )

  array_unique_diff($array1, $array2);

returns

  array ( 'red' => 2, 'purple' => 4, 'yellow' => 7, 'cyan' => 8, )
sjungwirth at matrix-consultants dot com 28-Feb-2008 09:08
I needed something a little different where maybe even the keys in multidimensional arrays don't match up. Setting $assoc to false will cause only to check for missing keys, otherwise it compares values as well. This was also based on '2ge at 2ge dot us' function

<?php

function n_array_diff_assoc ($a1, $a2, $assoc=true) {
    $r = array();
    if(is_array(current($a1))):
        foreach($a1 as $k => $v):
            if(isset($a2[$k])):
                $diff = n_array_diff($a1[$k], $a2[$k], $assoc);
                if (!empty($diff)):
                    $r[$k] = $diff;
                endif;
            else:
                $r[$k] = $v;
            endif;
        endforeach;
    else:
        $r = $assoc ? array_diff_assoc($a1, $a2) : array_diff_key($a1, $a2);
    endif;
    return $r;
}
?>
coder at gs dot com 16-Feb-2008 11:00
The PHP4 version below works only unidirectionally. If you switch the arrays around i.e. (ar2, ar1) you get different results than (ar1, ar2).
AiFiLTr0 [at host] invyl [dot] ath.cx 29-Nov-2007 02:56
The recursive function suggested by '2ge at 2ge dot us' will provide you with empty arrays if there's no diff.
This variant of the function cleans up empty arrays and fixes a bug in the first suggested version. It works 100%
.
<?php
function array_diff_key_recursive ($a1, $a2) {
        foreach($a1 as $k => $v) {
            //$r[$k] = is_array($v) ? $this->array_diff_key_recursive($a1[$k], $a2[$k]) : array_diff_key($a1, $a2);
        if (is_array($v))
            {
            $r[$k]=$this->array_diff_key_recursive($a1[$k], $a2[$k]);
            }else
            {
            $r=array_diff_key($a1, $a2);
            }

        if (is_array($r[$k]) && count($r[$k])==0)
            {
            unset($r[$k]);
            }
        }
        return $r;
    }
?>
2ge at 2ge dot us 07-Mar-2006 08:28
Hello, if you need diff key of n-dimensional arrays here is nice solution:
<?php
function n_array_diff ($a1, $a2) {
        foreach($a1 as $k => $v) {
            $r[$k] = is_array($v) ? n_array_diff($a1[$k], $a2[$k]) : array_diff_key($a1, $a2);
        }
        return $r;
}
?>
it will print everything, what is missing in $a2.
vlad_mustafin at ukr dot net 13-Jan-2006 04:39
One more alternative variant :)
<?
if (!function_exists('array_diff_key')) {
    function array_diff_key() {
        $argCount   = func_num_args();
        $diff_arg_prefix = 'diffArg';
        $diff_arg_names = array();
        for ($i=0; $i < $argCount; $i++) {
            $diff_arg_names[$i] = 'diffArg'.$i;
            $$diff_arg_names[$i] = array_keys((array)func_get_arg($i));
        }
        $diffArrString = '';
        if (!empty($diff_arg_names)) $diffArrString =  '$'.implode(', $', $diff_arg_names);
        eval("\$result = array_diff(".$diffArrString.");");
        return $result;
    }
}
?>
ampf at egp dot up dot pt 25-Nov-2005 09:55
Well, you could implement in the code something more powerfull:

http://www.php.net/manual/en/function.array-diff.php#31364
maxence at pontapreta dot net 27-May-2005 07:38
Seems to be a great function, especially for n-dimensions arrays. The only problem is that I cannot find it in php 5.0.3 and 5.0.4. Does it really exist ?! :(

[20:27:05][maxence@conurb] ~/test2/php-5.0.4$ grep PHP_FUNCTION * -r | grep -i array_diff_key
[20:27:09][maxence@conurb] ~/test2/php-5.0.4$


Computes the difference of arrays with additional index check which is performed by a user supplied callback function

  • ≪ array_diff_key
  • array_diff_ukey ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the difference of arrays with additional index check which is performed by a user supplied callback function

array_diff_uassoc

(PHP 5, PHP 7)

array_diff_uassoc — Computes the difference of arrays with additional index check which is performed by a user supplied callback function

설명

array array_diff_uassoc ( array $array1 , array $array2 [, array $... ], callable $key_compare_func )

Compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are used in the comparison.

Unlike array_diff_assoc() an user supplied callback function is used for the indices comparison, not internal function.

인수

array1

The array to compare from

array2

An array to compare against

...

More arrays to compare against

key_compare_func

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )

반환값

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

예제

Example #1 array_diff_uassoc() example

The "a" => "green" pair is present in both arrays and thus it is not in the output from the function. Unlike this, the pair 0 => "red" is in the output because in the second argument "red" has key which is 1.

<?php
function key_compare_func($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b)? 1:-1;
}

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);
?>

위 예제의 출력:

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

The equality of 2 indices is checked by the user supplied callback function.

주의

Note:

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_diff_uassoc($array1[0], $array2[0], "key_compare_func");.

참고

  • array_diff() - 배열 차이를 계산
  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산
  • array_udiff() - 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
  • array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_uintersect() - Computes the intersection of arrays, compares data by a callback function
  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions

User Contributed Notes

MD. ABU SAYEM 11-Mar-2014 03:12
//array_diff_uassoc for multidimenssional array
$color_1=array('a'=>'white','b'=>'red','c'=>array('a'=>'green','b'=>'blue','c'=>'yellow'));
$color_2=array('a'=>'white','b'=>'red','c'=>array('a'=>'white','b'=>'red','c'=>'yellow'));

function key_compare($a,$b){
    if($a===$b)        return 0;
    return($a>$b)?1:-1;
}
print_r(array_diff_uassoc($color_1['c'], $color_2['c'],"key_compare"));

//Output
 Array
(
    [a] => green
    [b] => blue
)


Computes the difference of arrays using a callback function on the keys for comparison

  • ≪ array_diff_uassoc
  • array_diff ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the difference of arrays using a callback function on the keys for comparison

array_diff_ukey

(PHP 5 >= 5.1.0, PHP 7)

array_diff_ukey — Computes the difference of arrays using a callback function on the keys for comparison

설명

array array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func )

Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

Unlike array_diff_key() a user supplied callback function is used for the indices comparison, not internal function.

인수

array1

The array to compare from

array2

An array to compare against

...

More arrays to compare against

key_compare_func

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )

반환값

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

예제

Example #1 array_diff_ukey() example

<?php
function key_compare_func($key1, $key2)
{
    if ($key1 == $key2)
        return 0;
    else if ($key1 > $key2)
        return 1;
    else
        return -1;
}

$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

var_dump(array_diff_ukey($array1, $array2, 'key_compare_func'));
?>

위 예제의 출력:

array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}

주의

Note:

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_ukey($array1[0], $array2[0], 'callback_func');.

참고

  • array_diff() - 배열 차이를 계산
  • array_udiff() - 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산
  • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_diff_key() - Computes the difference of arrays using keys for comparison
  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_key() - Computes the intersection of arrays using keys for comparison
  • array_intersect_ukey() - Computes the intersection of arrays using a callback function on the keys for comparison

User Contributed Notes

There are no user contributed notes for this page.


배열 차이를 계산

  • ≪ array_diff_ukey
  • array_fill_keys ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열 차이를 계산

array_diff

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

array_diff — 배열 차이를 계산

설명

array array_diff ( array $array1 , array $array2 [, array $ ... ] )

array1을 array2에 비교하고 차이를 반환합니다.

인수

array1

비교할 원본 배열

array2

비교할 대상 배열

...

추가 대상 배열

반환값

다른 배열에 존재하지 않는 array1의 모든 값을 가지는 array를 반환합니다.

예제

Example #1 array_diff() 예제

<?php
$array1 = array ("a" => "green", "red", "blue", "red");
$array2 = array ("b" => "green", "yellow", "red");
$result = array_diff ($array1, $array2);

print_r($result);
?>

$array1 안에서 여러 값들의 차이가 발생한다면 모두 같은 방식으로 처리합니다. 출력은:

Array
(
    [1] => blue
)

주의

Note:

두 구성요소가 동치라고 볼수 있는 때는 (string) $elem1 === (string) $elem2 일때 만이다. In words: when the string representation is the same.

Note:

이 함수는 n-차원의 배열 중 한 차원만 검사한다는 것에 주의한다. 물론 array_diff($array1[0], $array2[0]); 을 사용하여 더 깊은 차원들을 검사할수 있다.

Warning

이 함수는 PHP 4.0.4에서 제대로 동작하지 않았다!

참고

  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산
  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산

User Contributed Notes

emeka dot echeruo at gmail dot com 11-May-2016 12:49
Resubmitting... the update for takes into account comparison issues
Computes the difference of all the arrays
<?php

/**
 * array_diffs a€" Computes the difference of all the arrays
 *
 * @param array
 *
 *    array1 - The array to compare from and against
 *    array2 - The array to compare from and against
 *    array(n) - More arrays to compare from and against
 *
 * @return array Returns all the arrays that do contains entries that cannot be matched in any of the arrays.
 */

function array_diffs() {
    $count = func_num_args();
    if($count < 2) {
        trigger_error('Must provide at least 2 arrays for comparison.');
    }
    $check = array();
    $out = array();
     //resolve comparison issue
    $func = function($a, $b) {
        $dbl = function($i, $d) {
            $e = 0.00001;
            if(abs($i-$d) < $e) {
                return true;
            }
            return false;
        };
        if((gettype($a) == 'integer' && gettype($b['value']) == 'double') || (gettype($a) == 'double' && gettype($b['value']) == 'integer')) {
            if((gettype($a) == 'integer' && $dbl((double) $a, $b['value'])) || (gettype($b['value']) == 'integer' && $dbl((double) $b['value'], $a))) {
                return true;
            }
        } elseif((gettype($a) == 'double') && (gettype($b['value']) == 'double')) {
            return $dbl($a,$b['value']);
        } elseif($a == $b['value']) {
            return true;
        }
        return false;
    };
    for($i = 0; $i < $count; $i++) {
        if(!is_array(func_get_arg($i))) {
            trigger_error('Parameters must be passed as arrays.');
        }
        foreach(func_get_arg($i) as $key => $value) {
            if(is_numeric($key) && is_string($value)) {
                if(array_key_exists($value, $check) && $func($value, $check[$value])) {
                    $check[$value]['count'] = $check[$value]['count'] + 1;
                } else {
                    $check[$value]['value'] = $value;
                    $check[$value]['count'] = 1;
                }
            } elseif(is_numeric($key) && (is_bool($value) || is_null($value) || is_numeric($value) || is_object($value) || is_resource($value))) {
                $update = false;
                foreach($check as $check_key => $check_value) {
                    if(is_numeric($key) && (is_bool($check_value['value']) || is_null($check_value['value']) || is_numeric($check_value['value']) || is_object($check_value['value']) || is_resource($check_value['value'])) && $func($value, $check_value)) {
                        $update = true;
                        $check[$check_key]['count'] = $check[$check_key]['count'] + 1;
                    }
                }
                if(!$update) {
                    $check[] = array('value' => $value, 'count' => 1);
                }
            } else {
                if(array_key_exists($key, $check) && $func($value, $check[$key])) {
                    $check[$key]['count'] = $check[$key]['count'] + 1;
                } else {
                    $check[$key]['value'] = $value;
                    $check[$key]['count'] = 1;
                }
            }
        }
    }
    foreach($check as $check_key => $check_value) {
        if($check_value['count'] == 1) {
           for ($i = 0; $i < $count; $i++) {
                foreach(func_get_arg($i) as $key => $value) {
                    if(is_numeric($key) && is_string($value) && ($value == (string) $check_key)) {
                        $out[$i][$key] = $value;
                    } elseif(is_numeric($key) && ($check_value['value'] == $value)) {
                        $out[$i][$key] = $value;
                    } elseif(is_string($key) && ($check_value['value'] == $value)) {
                        $out[$i][$key] = $value;
                    }
                }
            }
        }
    }
    return $out;
}

?>
emeka at echeruo at gmail dot com 30-Apr-2016 08:10
Resubmitting... the update for takes into account comparison issues

Computes the difference of all the arrays

<?php

/**
 * array_diffs — Computes the difference of all the arrays
 *
 * @param array
 *
 *    array1 - The array to compare from and against
 *    array2 - The array to compare from and against
 *    array(n) - More arrays to compare from and against
 *
 * @return array Returns all the arrays that do contains entries that cannot be matched in any of the arrays.
 */

function array_diffs() {
    $count = func_num_args();
    if($count < 2) {
        trigger_error('Must provide at least 2 arrays for comparison.');
    }
    $check = array();
    $out = array();
     //resolve comparison issues in PHP
    $func = function($a, $b) {
        if(gettype($a) == 'integer' && gettype($b['value']) == 'double' || gettype($a) == 'double' && gettype($b['value']) == 'integer') {
            if(gettype($a) == 'integer') {
                if((double) $a == $b['value']) {
                    return true;
                }
            } else {
                if((double) $b['value'] == $a) {
                    return true;
                }
            }
        } elseif(gettype($a) == 'double' && gettype($b['value']) == 'double') {
            $epsilon = 0.00001;
            if(abs($a - $b['value']) < $epsilon) {
                return true;
            }
        } else {
            if($a == $b['value']) {
                return true;
            }
        }
        return false;
    };
    for ($i = 0; $i < $count; $i++) {
        if(!is_array(func_get_arg($i))) {
            trigger_error('Parameters must be passed as arrays.');
        }
        foreach(func_get_arg($i) as $key => $value) {
            if(is_numeric($key) && is_string($value)) {
                if(array_key_exists($value, $check) && $func($value, $check[$value])) {
                    $check[$value]['count'] = $check[$value]['count'] + 1;
                } else {
                    $check[$value]['value'] = $value;
                    $check[$value]['count'] = 1;
                }
            } elseif(is_numeric($key) && (is_bool($value) || is_null($value) || is_numeric($value) || is_object($value) || is_resource($value))) {
                $update = false;
                foreach($check as $check_key => $check_value) {
                    if(is_numeric($key) && (is_bool($check_value['value']) || is_null($check_value['value']) || is_numeric($check_value['value']) || is_object($check_value['value']) || is_resource($check_value['value']))) {
                        if($func($value, $check_value)) {
                            $update = true;
                            $check[$check_key]['count'] = $check[$check_key]['count'] + 1;
                        }
                    }
                }
                if(!$update) {
                    $check[] = array('value' => $value, 'count' => 1);
                }
            } else {
                if(array_key_exists($key, $check) && $func($value, $check[$key])) {
                    $check[$key]['count'] = $check[$key]['count'] + 1;
                } else {
                    $check[$key]['value'] = $value;
                    $check[$key]['count'] = 1;
                }
            }
        }
    }
    foreach($check as $check_key => $check_value) {
        if($check_value['count'] == 1) {
            for ($i = 0; $i < $count; $i++) {
                foreach(func_get_arg($i) as $key => $value) {
                    if(is_numeric($key) && is_string($value)) {
                        if($value == (string) $check_key) {
                            $out[$i][$key] = $value;
                        }
                    } elseif(is_numeric($key) && (is_bool($value) || is_null($value) || is_numeric($value) || is_object($value) || is_resource($value))) {
                        if(is_numeric($key) && (is_bool($check_value['value']) || is_null($check_value['value']) || is_numeric($check_value['value']) || is_object($check_value['value']) || is_resource($check_value['value']))) {
                            if($check_value['value'] == $value) {
                                $out[$i][$key] = $value;
                            }
                        }
                    } else {
                        if($key == $check_key) {
                            $out[$i][$key] = $value;
                        }
                    }
                }
            }
        }
    }
    return $out;
}

?>
true === $gmail[&#39;a3diti&#39;] 01-Mar-2016 06:59
This function comes in handy if we want to compare two array values only

For example we want to make a quick check on some required $_POST parameters:

function required_array($required_array_values, $dynamic_array_values){
  return (count(array_diff(
            $required_array_values,
            $dynamic_array_values
          )) > 0) ? false : true;
}

// we expect $_POST['change_password'][new, old, repeat]
$required_params = required_array(
        array('old','new','repeat'),
        array_keys($_POST['change_password'])
      );

if( !$requred_params  ){
  die('error: all parameters are required!');
}else{
  echo 'good to go';
}
az_zankapfel_org 29-Feb-2016 10:20
For case-insensitive array_diff use:

$result = array_udiff($array1, $array2, 'strcasecmp');

(Found on stackoverflow)
Anonymous 05-May-2015 03:50
I always wanted something like this to avoid listing all the files and folders you want to exclude in a project directory.

function array_preg_diff($a, $p) {
    foreach ($a as $i => $v)
        if (preg_match($p, $v))
            unset($a[$i]);
    return $a;
}

$relevantFiles = array_preg_diff(scandir('somedir'), '/^\./');

instead of

$relevantFiles = array_diff(scandir('somedir'), array('.', '..', '.idea', '.project));
Anonymous 29-Jan-2015 01:00
If you're not getting a count(array_diff($a1,$a2))>0 with something similar to the following arrays should use the php.net/array_diff_assoc function instead.

$a1 = Array
(
    [0] => id
    [1] => id_1
    [2] => id_2
)

$a2 = Array
(
    [0] => id
    [1] => id_2
    [2] => id_1
)
wrey75 at gmail dot com 04-Dec-2014 07:27
The difference is made only on the first level. If you want compare 2 arrays, you can use the code available at https://gist.github.com/wrey75/c631f6fe9c975354aec7 (including a class with an function to patch the array)

Here the basic function:

function my_array_diff($arr1, $arr2) {
        $diff = array();

        // Check the similarities
        foreach( $arr1 as $k1=>$v1 ){
            if( isset( $arr2[$k1]) ){
                $v2 = $arr2[$k1];
                if( is_array($v1) && is_array($v2) ){
                    // 2 arrays: just go further...
                    // .. and explain it's an update!
                    $changes = self::diff($v1, $v2);
                    if( count($changes) > 0 ){
                        // If we have no change, simply ignore
                        $diff[$k1] = array('upd' => $changes);
                    }
                    unset($arr2[$k1]); // don't forget
                }
                else if( $v2 === $v1 ){
                    // unset the value on the second array
                    // for the "surplus"
                    unset( $arr2[$k1] );
                }
                else {
                    // Don't mind if arrays or not.
                    $diff[$k1] = array( 'old' => $v1, 'new'=>$v2 );
                    unset( $arr2[$k1] );
                }
            }
            else {
                // remove information
                $diff[$k1] = array( 'old' => $v1 );
            }
        }

        // Now, check for new stuff in $arr2
        reset( $arr2 ); // Don't argue it's unnecessary (even I believe you)
        foreach( $arr2 as $k=>$v ){
            // OK, it is quite stupid my friend
            $diff[$k] = array( 'new' => $v );
        }
        return $diff;
    }
vickyssj7 at gmail dot com 19-Oct-2014 06:12
This is quite a simple example to compute the difference between array values without any recursive function:--
<?php

    function array_idiff($a,$b) {

        $store = array();
        foreach($a as $key => $values) {
            if(!isset($b[$key]) || $b[$key] !== $values){

                $store[] = strtolower($values);

            }
        }
        return $store;
    }
    $arr1 = array('this','that','those','they','them');
    $arr2 = array('this','that','them');
    echo "<pre>";
    print_r(array_idiff($arr1,$arr2));
MD dot ABU SAYEM=>sayem at asterisbd dot com 11-Mar-2014 11:21
//array_diff function compare first array against all of the array and return the elements with index that are not present in either of the array.
<?php
$array1 = array("a" => "green", "red", "c"=>"blue", "red","cyan",2,3,"white");
$array2 = array("b" => "green", "yellow", "red");
$array3 = array("b" => "green", "yellow", "white",4);
$array4 = array("b" => "green", "yellow", "blue",2);
$result = array_diff($array1, $array2,$array3,$array4);
echo "<pre>";
print_r($result);
?>
//outputs
Array
(
    [3] => cyan
    [5] => 3
)
Anonymous 06-Aug-2013 10:56
Please note that array_diff cannot compare with an undefined array. For example:

<?php
$array1 = array ("ABC", "DEF");

$Result = $array_diff( $array1, $array2);

var_dump($Result);
?>

will output NULL NULL. If your script causes an undefined array (like mine did) it can be hard to find the mistake.

Greetings
Jeppe Utzon 08-Nov-2012 11:28
If you just need to know if two arrays' values are exactly the same (regardless of keys and order), then instead of using array_diff, this is a simple method:

<?php

function identical_values( $arrayA , $arrayB ) {

    sort( $arrayA );
    sort( $arrayB );

    return $arrayA == $arrayB;
}

// Examples:

$array1 = array( "red" , "green" , "blue" );
$array2 = array( "green" , "red" , "blue" );
$array3 = array( "red" , "green" , "blue" , "yellow" );
$array4 = array( "red" , "yellow" , "blue" );
$array5 = array( "x" => "red" , "y" =>  "green" , "z" => "blue" );

identical_values( $array1 , $array2 );  // true
identical_values( $array1 , $array3 );  // false
identical_values( $array1 , $array4 );  // false
identical_values( $array1 , $array5 );  // true

?>

The function returns true only if the two arrays contain the same number of values and each value in one array has an exact duplicate in the other array. Everything else will return false.

my alternative method for evaluating if two arrays contain (all) identical values:

<?php

sort($a); $sort(b); return $a == $b;

?>

may be slightly faster (10-20%) than this array_diff method:

<?php

return ( count( $a ) == count( $b ) && !array_diff( $a , $b ) ? true : false );

?>

but only when the two arrays contain the same number of values and then only in some cases. Otherwise the latter method will be radically faster due to the use of a count() test before the array_diff().

Also, if the two arrays contain a different number of values, then which method is faster will depend on whether both arrays need to be sorted or not. Two times sort() is a bit slower than one time array_diff(), but if one of the arrays have already been sorted, then you only have to sort the other array and this will be almost twice as fast as array_diff().

Basically: 2 x sort() is slower than 1 x array_diff() is slower than 1 x sort().
devel-takethisout-oper dot web at gmail dot com 18-Oct-2012 12:31
Suppose you have a CMS which saves a list of phone numbers for each user. They are stored like this:

$phones['Home'] = '(555)555-1234';
$phones['Office'] = '(555)555-2222';

You only want to save to the database if the list of items has changed. Including if the ORDER of them has changed.

<?php
// Compares two arrays- keys and values (as strings) must match.
// Keys and order must be the same to be equal.
function arraysEqual($arr1, $arr2){
    if(count($arr1) != count($arr2)){
        return(FALSE);
    }else{
        $arrStr1 = serialize($arr1);
        $arrStr2 = serialize($arr2);
        if( strcmp($arrStr1, $arrStr2)==0 ){
            return(TRUE);
        }else{
            return(FALSE);
        }
    }
}
?>
michiel thalen 07-Apr-2012 11:22
The fact that it will use the string representation causes alot of problems with php 5.4.

When you will have an array inside your elements, it will throw a notice error.
It will throw huge amounts of notices depending on how many items you have.

The same problem you will notice when checking with objects inside your array. It will cause catchable fatal errors.
This is also the case in php 5.3

I'm going to send a bug report, because I think php should handle this.
ballestermendez at gmail dot com 16-Mar-2012 08:40
If the element number returned is 0, this function NOT return a valid array, return NULL.
merlyn dot tgz at gmail dot com 15-Mar-2012 07:08
There is more fast implementation of array_diff, but with some limitations. If you need compare two arrays of integers or strings you can use such function:

    public static function arrayDiffEmulation($arrayFrom, $arrayAgainst)
    {
        $arrayAgainst = array_flip($arrayAgainst);

        foreach ($arrayFrom as $key => $value) {
            if(isset($arrayAgainst[$value])) {
                unset($arrayFrom[$key]);
            }
        }

        return $arrayFrom;
    }

It is ~10x faster than array_diff

php > $t = microtime(true);$a = range(0,25000); $b = range(15000,500000); $c = array_diff($a, $b);echo microtime(true) - $t;
4.4335179328918
php > $t = microtime(true);$a = range(0,25000); $b = range(15000,500000); $c = arrayDiffEmulation($a, $b);echo microtime(true) - $t;
0.37219095230103
wladimir camargo 25-Jul-2011 07:23
This is my simple function do compare two arrays. It adds the deleted values from the original array.

<?php
function array_diff_($old_array,$new_array) {
        foreach($new_array as $i=>$l){
                if($old_array[$i] != $l){
                        $r[$i]=$l;
                }
        }

        //adding deleted values
        foreach($old_array as $i=>$l){
                if(!$new_array[$i]){
                        $r[$i]="";
                }
        }
        return $r;
}
?>
eugeny dot yakimovitch at gmail dot com 30-Dec-2010 12:17
Note that array_diff is not equivalent to

<?php
function fullArrayDiff($left, $right)
{
    return array_diff(array_merge($left, $right), array_intersect($left, $right));
}
?>

since it is a set-theoretical complement as in

http://en.wikipedia.org/wiki/Complement_(set_theory)
javierchinapequeno at yahoo dot es 21-Dec-2010 08:21
Hi, I´d like to give a piece of advice to all who need to use this function to compare two arrays that have a great quantity of elements. You should sort both arrays first before comparing, it will work faster.
Thanks
thefrox at gmail dot com 30-Jun-2010 02:28
Multidimensional array_diff

<?php
echo '<pre>';
$bdd['80395']= array('80396','80397','80398','777');
$folder['80395']= array('80396','80397','666','80398','154223');
$folder['80397']= array('34','35','36','45','57');

echo '<hr>';

function multidimensional_array_diff($a1,$a2)
{
  $r = array();

 foreach ($a2 as $key => $second)
 {
      foreach ($a1 as $key => $first)
      {

            if (isset($a2[$key]))
            {
                foreach ($first as $first_value)
                {
                    foreach ($second as $second_value)
                    {
                        if ($first_value == $second_value)
                        {
                            $true = true;
                            break;
                        }
                    }
                    if (!isset($true))
                    {

                        $r[$key][] = $first_value;
                    }
                    unset($true);
                }
            }
            else
            {
                $r[$key] = $first;
            }
      }
 }
  return $r;
}
print_r(single_diff_assoc($folder,$bdd));
?>

RESULT :

Array
(
    [80395] => Array
        (
            [0] => 666
            [1] => 154223
        )

    [80397] => Array
        (
            [0] => 34
            [1] => 35
            [2] => 36
            [3] => 45
            [4] => 57
        )

)
pikiou at somethinggooglerelated dot com 13-Nov-2009 01:24
With previous solutions handling multi-dimensional arrays or objects through serialization, if compared variables contain references at some point, these will be serialized and stand as such after the diff function.
Here is a safer solution :
<?php
function array_diff_no_cast(&$ar1, &$ar2) {
   $diff = Array();
   foreach ($ar1 as $key => $val1) {
      if (array_search($val1, $ar2) === false) {
         $diff[$key] = $val1;
      }
   }
   return $diff;
}
?>

Example:
<?php
$referenced = Array(1,1);
$array1 = Array(&$referenced, Array(2,3));
$array2 = Array(Array(1,1), Array(4,5));
$result = array_diff_no_cast($array1, $array2);

print_r($result);   //Outputs Array(1 => Array(2,3))
//And $referenced stands unchanged (not serialized)
?>
vojtech dot hordejcuk at gmail dot com 03-Nov-2009 04:15
Based on one lad's code, I created following function for creating something like HTML diff. I hope it will be useful.

<?php
private function diff ($old, $new)
{
  $old = preg_replace ('/ +/', ' ', $old);
  $new = preg_replace ('/ +/', ' ', $new);

  $lo = explode ("\n", trim ($old) . "\n");
  $ln = explode ("\n", trim ($new) . "\n");
  $size = max (count ($lo), count ($ln));

  $equ = array_intersect ($lo, $ln);
  $ins = array_diff ($ln, $lo);
  $del = array_diff ($lo, $ln);

  $out = '';

  for ($i = 0; $i < $size; $i++)
  {
    if (isset ($del [$i]))
    {
      $out .= '<p><del>' . $del [$i] . '</del></p>';
    }

    if (isset ($equ [$i]))
    {
      $out .= '<p>' . $equ [$i] . '</p>';
    }

    if (isset ($ins [$i]))
    {
      $out .= '<p><ins>' . $ins [$i] . '</ins></p>';
    }
  }

  return $out;
}
?>
Anonymous 15-Oct-2009 08:55
Hi!
I tried hard to find a solution to a problem I'm going to explain here, and after have read all the array functions and possibilities, I had to create what I think should exist on next PHP releases.

What I needed, it's some kind of Difference, but working with two arrays and modifying them at time, not returning an array as a result with the diference itself.

So, as an example:

A = 1,2,3
B = 2,3,4

should NOT be:

C = 1,4

but:

A = 1
B = 4

so basically, I wanted to delete coincidences on both arrays.

Now, I've some actions to do, and I know wich one I've to do with the values from one array or another.
With the normal DIFF I can't, because if I've an array like C=1,4, I dont know if I've to do the Action_A with 1 or with 4, but I really know that everything in A, will go to the Action_A and everithing in B, will go to Action_B. So same happens with 4, don't know wich action to apply...

So I created this:

<?php
function array_diff_ORG_NEW(&$org, &$new, $type='VALUES'){
    switch($type){
        case 'VALUES':
            $int = array_values(array_intersect($org, $new)); //C = A ^ B
            $org = array_values(array_diff($org, $int)); //A' = A - C
            $new= array_values(array_diff($new, $int)); //B' = B - C
            break;
        case 'KEYS':
            $int = array_values(array_intersect_key($org, $new)); //C = A ^ B
            $org = array_values(array_diff_key($org, $int)); //A' = A - C
            $new= array_values(array_diff_key($new, $int)); //B' = B - C
            break;
    }
}
?>

This cute, works by reference, and modifies the arrays deleting coincidences on both, and leaving intact the non coincidences.

So a call to this will be somethin' like:

<?php
$original = array(1,2,3);
$new = array(2,3,4);

array_diff_ORG_NEW($original, $new, 'VALUES');
?>

And HERE, I'll have my arrays as I wanted:

$original = 1
$new = 4

Now, why I use it precisely?

Imagine you've some "Events" and some users you select when create the event, can "see" this event you create. So you "share" the event with some users. Ok?

Imagine you created and Event_A, and shared with users 1,2,3.

Now you want to modify the event, and you decide to modify the users to share it. Imagine you change it to users 2,3,4.

(numbers are users ID).

So you can manage when you are going to modify, to have an array with the IDs in DDBB ($original), and then, have another array with ID's corresponding to the users to share after modifying ($new). Wich ones you've to DELETE from DDBB, and wich ones do you've to INSERT?

If you do a simple difference or somehow, you get somethin' like C=1,4.
You have no clue on wich one you've to insert or delete.

But on this way, you can know it, and that's why:

- What keeps on $original, it's somethin not existing in $new at the beggining. So you know that all what you've inside $original, have to be deleted from DDBB because what you did in the modifying process, it's to unselect those users keeping in $original.
- What keeps on $new, it's something not existing in $original at the beggining. Wich means that in the modifying process you added some new users. And those have to be inserted in DDBB. So, everything keeping inside $new, have to be inserted in the DDBB.

Conclusion:

- Remaining in $original --> delete from DB.
- Remaining in $new --> insert into DB.

And that's all!

I hope you find it useful, and I encourage PHP "makers", to add in a not distant future, somethin' like this one natively, because I'm shure that I'm not the first one needing something like this.

Best regards all,

Light.
firegun at terra dot com dot br 24-Jun-2009 07:52
Hello guys,

I´ve been looking for a array_diff that works with recursive arrays, I´ve tried the ottodenn at gmail dot com function but to my case it doesn´t worked as expected, so I made my own. I´ve haven´t tested this extensively, but I´ll explain my scenario, and this works great at that case :D

We got 2 arrays like these:

<?php
$aArray1['marcie'] = array('banana' => 1, 'orange' => 1, 'pasta' => 1);
$aArray1['kenji'] = array('apple' => 1, 'pie' => 1, 'pasta' => 1);

$aArray2['marcie'] = array('banana' => 1, 'orange' => 1);
?>

As array_diff, this function returns all the items that is in aArray1 and IS NOT at aArray2, so the result we should expect is:

<?php
$aDiff['marcie'] = array('pasta' => 1);
$aDiff['kenji'] = array('apple' => 1, 'pie' => 1, 'pasta' => 1);
?>

Ok, now some comments about this function:
 - Different from the PHP array_diff, this function DON´T uses the === operator, but the ==, so 0 is equal to '0' or false, but this can be changed with no impacts.
 - This function checks the keys of the arrays, array_diff only compares the values.

I realy hopes that this could help some1 as I´ve been helped a lot with some users experiences. (Just please double check if it would work for your case, as I sad I just tested to a scenario like the one I exposed)

<?php
function arrayRecursiveDiff($aArray1, $aArray2) {
    $aReturn = array();

    foreach ($aArray1 as $mKey => $mValue) {
        if (array_key_exists($mKey, $aArray2)) {
            if (is_array($mValue)) {
                $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
                if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
            } else {
                if ($mValue != $aArray2[$mKey]) {
                    $aReturn[$mKey] = $mValue;
                }
            }
        } else {
            $aReturn[$mKey] = $mValue;
        }
    }

    return $aReturn;
}
?>
pyerre 12-Sep-2008 03:20
be careful kids, this function can be tricky
<?php
$tab1=array(0=>"a");
$tab2=array(0=>"a",1=>"b");

print_r(array_diff($tab1,$tab2));
?>
gives
Array
(
)
Tim Trefren 04-Apr-2008 05:05
Here's a little wrapper for array_diff - I found myself needing to iterate through the edited array, and I didn't need to original keys for anything.

<?php
function arrayDiff($array1, $array2){
    # This wrapper for array_diff rekeys the array returned
    $valid_array = array_diff($array1,$array2);

    # reinstantiate $array1 variable
    $array1 = array();

    # loop through the validated array and move elements to $array1
    # this is necessary because the array_diff function returns arrays that retain their original keys
    foreach ($valid_array as $valid){
        $array1[] = $valid;
        }
    return $array1;
    }
?>
merlinyoda at dorproject dot net 28-Mar-2008 07:11
As touched on in kitchin's comment of 19-Jun-2007 03:49 and nilsandre at gmx dot de's comment of 17-Jul-2007 10:45, array_diff's behavior may be counter-intuitive if you aren't thinking in terms of set theory.

array_diff() returns a *mathematical* difference (a.k.a. subtraction) of elements in array A that are in array B and *not* what elements are different between the arrays (i.e. those that elements that are in either A or B but aren't in both A and B).

Drawing one of those Ven diagrams or Euler diagrams may help with visualization...

As far as a function for returning what you may be expecting, here's one:

<?php
function array_xor ($array_a, $array_b) {
    $union_array = array_merge($array_a, $array_b);
    $intersect_array = array_intersect($array_a, $array_b);
    return array_diff($union_array, $intersect_array)
}
?>
asdf at asdf dot com 15-Aug-2007 02:21
Even tough the description of this function's behavior has changed it
does not appear as tough the function's behavior has changed.  Below is some test code and its output:

<?php

    $a = array('1','2','3','4','5');
    $b = array('1','a','b','c','d','e');
    $c = array('2','f','g','h','i','j');

    print_r(array_diff($a, $b, $c));

?>

Array
(
    [2] => 3
    [3] => 4
    [4] => 5
)
nilsandre at gmx dot de 17-Jul-2007 06:45
Again, the function's description is misleading right now. I sought a function, which (mathematically) computes A - B, or, written differently, A \ B. Or, again in other words, suppose

A := {a1, ..., an} and B:= {a1, b1, ... , bm}

=> array_diff(A,B) = {a2, ..., an}

array_diff(A,B) returns all elements from A, which are not elements of B (= A without B).

You should include this in the documentation more precisely, I think.
mr dot kschan at gmail dot com 13-Jul-2007 06:16
just comment ... i don't know whether the following implementation of array_diff is having a much better meaning to the function name.

function ary_diff( $ary_1, $ary_2 ) {
  // compare the value of 2 array
  // get differences that in ary_1 but not in ary_2
  // get difference that in ary_2 but not in ary_1
  // return the unique difference between value of 2 array
  $diff = array();

  // get differences that in ary_1 but not in ary_2
  foreach ( $ary_1 as $v1 ) {
    $flag = 0;
    foreach ( $ary_2 as $v2 ) {
      $flag |= ( $v1 == $v2 );
      if ( $flag ) break;
    }
    if ( !$flag ) array_push( $diff, $v1 );
  }

  // get difference that in ary_2 but not in ary_1
  foreach ( $ary_2 as $v2 ) {
    $flag = 0;
    foreach ( $ary_1 as $v1 ) {
      $flag |= ( $v1 == $v2 );
      if ( $flag ) break;
    }
    if ( !$flag && !in_array( $v2, $diff ) ) array_push( $diff, $v2 );
  }

  return $diff;
}

i think array_diff should return the difference between the array independent of the order of passing the parameter.
kitchin 19-Jun-2007 11:49
Until recently, the description said:
"array_diff() returns an array containing all the values of array1 that are not present in any of the other arguments. Note that keys are preserved."

Now it says:
"Compares array1 against array2 and returns the difference."

Now it's not clear what the optional arguments after the first two do.  Also, the difference is not symmetric in its arguments (that is array_intersect).
gilthans at NOgmailSPAM dot com 13-Jun-2007 07:11
I needed a function to only remove the element the amount of times he appears in the second array. In other words, if you have Array(1, 1, 2) and Array(1), the return value should be Array(1, 2).
So I built this function right here:

<?php
function array_diff_once(){
    if(($args = func_num_args()) < 2)
        return false;
    $arr1 = func_get_arg(0);
    $arr2 = func_get_arg(1);
    if(!is_array($arr1) || !is_array($arr2))
        return false;
    foreach($arr2 as $remove){
        foreach($arr1 as $k=>$v){
            if((string)$v === (string)$remove){ //NOTE: if you need the diff to be STRICT, remove both the '(string)'s
                unset($arr1[$k]);
                break; //That's pretty much the only difference from the real array_diff :P
            }
        }
    }
    //Handle more than 2 arguments
    $c = $args;
    while($c > 2){
        $c--;
        $arr1 = array_diff_once($arr1, func_get_arg($args-$c+1));
    }
    return $arr1;
}
$arr1 = Array("blue", "four"=>4, "color"=>"red", "blue", "green", "green", "name"=>"jon", "green");
$arr2 = Array("4", "red", "blue", "green");
print_r(array_diff_once($arr1, $arr2));
?>
This prints:
Array ( [1] => blue [3] => green [name] => jon [4] => green )

Note that it removes the elements left to right, opposite to what you might expect; in my case the order of elements had no importance. Fixing that would require a small variation.
tim at php dot user 05-Jun-2007 05:06
The description is wrong, array_diff() returns an array consisting of all elements in $array1 that are not in $array2. The example shows this.

Thats how it works on my php anyway.
ahigerd at stratitec dot com 01-Feb-2007 11:30
An earlier comment suggested using array_merge() to reindex the array. While this will work, array_values() is about 30-40% faster and accomplishes the same task.
03-Aug-2006 09:30
array_diff provides a handy way of deleting array elements by their value, without having to unset it by key, through a lengthy foreach loop and then having to rekey the array.

<?php

//pass value you wish to delete and the array to delete from
function array_delete( $value, $array)
{
    $array = array_diff( $array, array($value) );
    return $array;
}
?>
Colin 25-Jul-2006 06:44
Undocumented return:  it appears this will return NULL if an error occurs (e.g., an argument is not an array) and is not caught.

<?
@array_diff(NULL, array(1));
@array_diff(array(1), NULL);
@array_diff();
?>

All return NULL.  (Note the "@")
ben dot lancaster at design-ontap dot co dot uk 08-Jul-2006 01:57
One common caveat of this function is that if the arrays match, an empty array is return, not a strict boolean. E.g.:

<?php
$array1 = $array2 = array('a','b','c');

var_dump(array_diff($array1,$array2));
/*
 *Returns:
 * array(0) {
 * }
 */
?>
pavlicic at NOSPAM dot hotmail dot com 09-Mar-2006 10:55
<?php

// first array
$vid_player = $vm->getVideosByPlayer($player);

// second array
$vid_playlist = $vm->getVideosByPlaylist($playlist);

// this will not work...
$vid_player = array_diff($vid_player, $vid_playlist);

// but if you do this first...
$videos = array();
foreach ( $vid_player as $player )
{
    if ( $vid_playlist != null )
    {
        foreach ( $vid_playlist as $video )
        {
            if ( $player->id == $video->id )
                $videos[] = $player;
        }
    }
}

// this will work...
$vid_player = array_diff($vid_player, $videos);

?>
The first array_diff() compares two arrays only to find out that all the objects are unique!
Jappie 22-Jan-2006 07:40
Sorry for the bug in my last comment (probably rightfully removed by the admins).
If you want to compare more than 2 arrays, or don't know how many arrays need to be compared, this is your function:

<?php
# An extention to array_diff:
# It returns an array of all values not present in all arrays given. If '$strict' is true,
# it returns all values not present or not in the same order in all arrays given. The
# arrays to compare must be placed in another array, which is used as argument '$arrays'.
# Returns false if the '$arrays' is invalid.
function array_rdiff ($arrays, $strict = false) {
    # check if argument is valid.
    if (!is_array ($arrays))
        return false;
    foreach ($arrays as $array)
        if (!is_array ($array))
            return false;

    # set working variables
    $diff    = array ();
    $amount  = count ($arrays);
    $needles = array_shift ($arrays);

    # compare
    for ($n = 0; $n < $amount; $n++) {
        for ($m = 0; $needles[$m]; $m++) {
            $found     = true;
            $positions = array ($m);
            foreach ($arrays as $haystack) {
                if (($pos = array_search ($needles[$m], $haystack)) === false)
                    $found = false;
                if ($strict)
                    $positions[] = $pos;
            }
            if (!$found)
                $diff[] = $needle;
            elseif ($strict && (count (array_unique ($positions)) > 1))
                $diff[] = $needle;
        }
        $arrays[] = $needles;
        $needles  = array_shift ($arrays);
    }
    return array_unique ($diff);
}
?>
Simon Riget at paragi.dk 22-Jan-2006 02:27
A simple multidimentional key aware array_diff function.

<?php
function arr_diff($a1,$a2){
  foreach($a1 as $k=>$v){
    unset($dv);
    if(is_int($k)){
      // Compare values
      if(array_search($v,$a2)===false) $dv=$v;
      else if(is_array($v)) $dv=arr_diff($v,$a2[$k]);
      if($dv) $diff[]=$dv;
    }else{
      // Compare noninteger keys
      if(!$a2[$k]) $dv=$v;
      else if(is_array($v)) $dv=arr_diff($v,$a2[$k]);
      if($dv) $diff[$k]=$dv;
    }
  }
  return $diff;
}
?>

This function meets my immidiate needs but I'm shure it can be improved.
doug at NOSPAM dot thrutch dot co dot uk 06-Jan-2006 01:22
After spending half an hour scratching my head wondering why this function wasn't working I realised I had the arguments the wrong way round!

I needed to remove the contents of $array1 from $array2 so I tried:

<?php
$diff    = array_diff($members1, $members2);
?>

WRONG!! A quick swap around and things worked smoothly...

<?php
$diff    = array_diff($members2, $members1);
?>

Hope this saves someone a bit of bother
air at multi dot fi 04-Jan-2006 12:41
A small thing that caused me trouble today, wich I don't see listed on this page is that array_diff keeps the placing for the uniqe values, and removes the duplicated. This gives us empty fields in the array, wich caused me a lot of trouble. The solutions was simply to use array_merge() around the array_diff.

For example:
$array1 = array('blue', 'red', 'green');
$array2 = array('red');

array_diff($array1, $array2);

Will give us:
------
Array
(
   [0] => red
   [1] =>
   [2] => green
)

But if we use:

array_merge(array_diff($array1, $array2));

We will get:
------
Array
(
   [0] => red
   [1] => green
)
Viking Coder 03-Jan-2006 06:07
To anybody wanting a double-sided array_diff - mentioned by rudigier at noxx dot at. Remember, array_diff gives you everything in the first array that isn't in the subsequent arrays.

$array1=array('blue','red','green');
$array2=array('blue','yellow','green');

array_merge(array_diff($array1, $array2),array_diff($array2, $array1));

Result
------
Array
(
    [0] => red
    [1] => yellow
)
white_phoenix at ru dot ru 15-Dec-2005 08:37
To: effectpenguin at antarctida dot ru
Re: interesting effect
<?php
function arraycpy(&$target,&$array)
{
 if (!is_array($target)) {$target = array();}
 foreach($array as $k=>$v) {if ($k != "GLOBALS") {$target[$k] = $v;}}
}
arraycpy($old,$GLOBALS);
// some actions with variables:
$homer = "beer";
arraycpy($new,$GLOBALS);
$diff = array_diff($new,$old);
var_dump($diff);
?>
array(1) {
  ["homer"]=>
  string(4) "beer"
}
Windows NT WPX_NB 5.1 build 2600 PHP/5.0.4
rafmav 20-Apr-2005 01:24
Here is a few functions to do a fast diff between two arrays in a few lines.
You can use it with other functions described in the function array_merge : array_merge_replace from an other user, and two functions using it : array_merge_diff and array_merge_diff_reverse.
Note that the keys are preserved!
<?
// returns a two dimensions array with the deleted data
// and the added data
function array_diff_both($new,$old)
{
    $del=array_diff_assoc($old,$new);
    $add=array_diff_assoc($new,$old);
    return $diff=array("del"=>$del, "add"=>$add);
}

// returns a two dimensions array with the equal data,
// deleted data and the added data
function array_diff_all($arr_new,$arr_old)
{
    $arr_equ=array_intersect_assoc($arr_new,$arr_old);
    $arr_del=array_diff_assoc($arr_old,$arr_new);
    $arr_add=array_diff_assoc($arr_new,$arr_old);
    return $diff=array("equ"=>$arr_equ, "del"=>$arr_del, "add"=>$arr_add);
}
?>
j dot j dot d dot mol at ewi dot tudelft dot nl 31-Dec-2004 09:34
Here is some code to take the difference of two arrays. It allows custom modifications like prefixing with a certain string (as shown) or custom compare functions.

<?php
 // returns all elements in $all which are not in $used in O(n log n) time.
  // elements from $all are prefixed with $prefix_all.
  // elements from $used are prefixed with $prefix_used.
  function filter_unused( $all, $used, $prefix_all = "", $prefix_used = "" ) {
      $unused = array();

      // prefixes are not needed for sorting
      sort( $all );
      sort( $used );

      $a = 0;
      $u = 0;

      $maxa = sizeof($all)-1;
      $maxu = sizeof($used)-1;

      while( true ) {
          if( $a > $maxa ) {
              // done; rest of $used isn't in $all
              break;
          }
          if( $u > $maxu ) {
              // rest of $all is unused
              for( ; $a <= $maxa; $a++ ) {
                  $unused[] = $all[$a];
              }
              break;
          }

          if( $prefix_all.$all[$a] > $prefix_used.$used[$u] ) {
              // $used[$u] isn't in $all?
              $u++;
              continue;
          }

          if( $prefix_all.$all[$a] == $prefix_used.$used[$u] ) {
              // $all[$a] is used
              $a++;
              $u++;
              continue;
          }

          $unused[] = $all[$a];

          $a++;
      }

      return $unused;
  }
?>
19-Nov-2003 01:54
From the page:
Note:  Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);

I've found a way to bypass that. I had 2 arrays made of arrays.
I wanted to extract from the first array all the arrays not found in the second array. So I used the serialize() function:

<?php
function my_serialize(&$arr,$pos){
  $arr = serialize($arr);
}

function my_unserialize(&$arr,$pos){
  $arr = unserialize($arr);
}

 //make a copy
$first_array_s = $first_array;
$second_array_s = $second_array;

// serialize all sub-arrays
array_walk($first_array_s,'my_serialize');
array_walk($second_array_s,'my_serialize');

// array_diff the serialized versions
$diff = array_diff($first_array_s,$second_array_s);

// unserialize the result
array_walk($diff,'my_unserialize');

// you've got it!
print_r($diff);
?>
r dot kirschke at gmx dot net 24-Mar-2003 10:26
Are you looking for a function which returns an edit script (a set of insert and delete instructions on how to change one array into another)? At least, that's what I hoped to find here, so here's some code based on http://www.cs.arizona.edu/people/gene/PAPERS/diff.ps :

<?php
function diff_rek(&$a1,&$a2,$D,$k,&$vbck)
{
 $x=$vbck[$D][$k]; $y=$x-$k;
 if ($D==0)
 {
  if ($x==0) return array(array(),array());
  else
  return array(array_slice($a1,0,$x),array_fill(0,$x,"b"));
 }
 $x2=$vbck[$D-1][$k+1];
 $y2=$vbck[$D-1][$k-1]-($k-1);
 $xdif=$x-$x2; $ydif=$y-$y2;
 $l=min($x-$x2,$y-$y2);
 $x=$x-$l;
 $y=$y-$l;
 if ($x==$x2)
 {
   $res=diff_rek($a1,$a2,$D-1,$k+1,$vbck);
   array_push($res[0],$a2[$y-1]);
   array_push($res[1],"2");
   if ($l>0)
   {
    $res[0]=array_merge($res[0],array_slice($a2,$y,$l));
    $res[1]=array_merge($res[1],array_fill(0,$l,"b"));
   }
 }
 else
 {
   $res=diff_rek($a1,$a2,$D-1,$k-1,$vbck);
   array_push($res[0],$a1[$x-1]);
   array_push($res[1],"1");
   if ($l>0)
   {
    $res[0]=array_merge($res[0],array_slice($a1,$x,$l));
    $res[1]=array_merge($res[1],array_fill(0,$l,"b"));
   }
 }
 return $res;
}

function arr_diff(&$a1,&$a2)
{
 $max=70;
 $c1=count($a1);
 $c2=count($a2);
 $v[1]=0;
 for ($D=0; $D<=$max; $D++)
 {
   for ($k=-$D; $k<=$D; $k=$k+2)
  {
   if (($k==-$D) || ($k!=$D && $v[$k-1]<$v[$k+1]))
    $x=$v[$k+1];
   else
    $x=$v[$k-1]+1;
   $y=$x-$k;
   while (($x<$c1)&&($y<$c2)&&($a1[$x]==$a2[$y]))
    {
     $x++;
     $y++;
    }
    $v[$k]=$x;
    if (($x>=$c1)&&($y>=$c2))
   {
    $vbck[$D]=$v;
    return diff_rek($a1,$a2,$D,$c1-$c2,$vbck);
   };
  }
  $vbck[$D]=$v;
 };
 return -1;
}
?>

This works on arrays of all elements for which the operator "==" is defined.
arr_dif($a1,$a2) returns an array of two arrays:
$result[0] = array of elements from $a1 and $a2
$result[1] = array of chars - one for each element from $result[0]:
 "1" : The corresponding element is from $a1
 "2" : The corresponding element is from $a2
 "b" : The correspondig element is from both source arrays
The function returns -1, when the number of different elements is greater than $max

Example:
$a1=array("hello","world");
$a2=array("good","bye","world");
=> arr_diff($a1,$a2) = array(array("hello","good","bye","world"), array("1","2","2","b"));
csaba2000 at yahoo dot com 25-Feb-2003 10:58
<?php
function array_key_diff($ar1, $ar2) {  // , $ar3, $ar4, ...
    // returns copy of array $ar1 with those entries removed
    // whose keys appear as keys in any of the other function args
    $aSubtrahends = array_slice(func_get_args(),1);
    foreach ($ar1 as $key => $val)
        foreach ($aSubtrahends as $aSubtrahend)
            if (array_key_exists($key, $aSubtrahend))
                unset ($ar1[$key]);
    return $ar1;
}

$a = array("c" => "catty", "b" => "batty", "a" => "aunty", 5 => 4, 2.9 => 7, 11, "n" => "nutty");
$b = array(9, "d" => "ditty", "b" => "bratty", "a" => null, 10, 13);
$c = array_key_diff ($a, $b, array(5 => 6));
?>

$c is then equivalent to array('c' => 'catty', 6 => 11, 'n' => 'nutty')

Csaba Gabor from New York
ds2u at the hotmail dot com 24-Feb-2003 01:22
Yes you can get rid of gaps/missing keys by using:

<?php
$result = array_values(array_diff($array1,$array2));
?>

But to drop the storage of void spaces (actually a line feed) which are irritatingly indexed when reading from files - just use difference:

<?php
$array = array ();
$array[0] = "\n";
$result = array_diff($result,$array);
?>

dst
drNOSPAMtdiggersSPAMNO at hotmail dot com 06-Aug-2002 10:10
array_diff does not have buggy behavior as described above.  The problem stems from calling array_diff() each time in the loop, therefore regererating a new array with an index always at the beginning EVERY time, so each will always pick the first entry.  This is not buggy, but in fact what you've told the program to do :)  The solution is not as much a solution, but properly instructing the program what to do!

Cheers!
TheoDiggers
SeanECoates at !donotspam!yahoo dot ca 16-Oct-2001 07:43
I just came upon a really good use for array_diff(). When reading a dir(opendir;readdir), I _rarely_ want "." or ".." to be in the array of files I'm creating. Here's a simple way to remove them:

<?php
 $someFiles = array();
 $dp = opendir("/some/dir");
 while($someFiles[] = readdir($dp));
 closedir($dp);

 $removeDirs = array(".","..");
 $someFiles = array_diff($someFiles, $removeDirs);

 foreach($someFiles AS $thisFile) echo $thisFile."\n";
?>

S


Fill an array with values, specifying keys

  • ≪ array_diff
  • array_fill ≫
  • PHP Manual
  • 배열 함수 목록
  • Fill an array with values, specifying keys

array_fill_keys

(PHP 5 >= 5.2.0, PHP 7)

array_fill_keys — Fill an array with values, specifying keys

설명

array array_fill_keys ( array $keys , mixed $value )

Fills an array with the value of the value parameter, using the values of the keys array as keys.

인수

keys

Array of values that will be used as keys. Illegal values for key will be converted to string.

value

Value to use for filling

반환값

Returns the filled array

예제

Example #1 array_fill_keys() example

<?php
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a);
?>

위 예제의 출력:

Array
(
    [foo] => banana
    [5] => banana
    [10] => banana
    [bar] => banana
)

참고

  • array_fill() - 값으로 배열 채우기
  • array_combine() - 키를 위한 배열과 값을 위한 배열을 사용하여 배열을 생성

User Contributed Notes

ntd at entidi dot it 24-Oct-2015 04:04
To remove arbitrary keys from an associative array:

<?php

function nuke_keys($keys, $array) {
    return array_diff_key($array, array_fill_keys($keys, 0));
}

$array = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$keys  = array('red', 'purple');

print_r(nuke_keys($keys, $array));
?>

The above snippet will return:

Array
(
    [blue] => 1
    [green] => 3
)
taylanaktepe at yahoo dot com 21-Jul-2014 06:46
$keys = array(1, 2, 3);

// Fill it with value.
$keys = array_fill_keys($keys, 'banana');
print_r($keys);

// Fill it different value.
$apples = array_fill_keys(array_keys($keys), 'apple');
print_r($apples);

// Output:
Array (
 [1] => banana
 [2] => banana
 [3] => banana
)
Array (
 [1] => apple
 [2] => apple
 [3] => apple
)
atul dot kr_singh at hotmail dot com 04-Jan-2013 08:39
If an associative array is used as the second parameter of array_fill_keys, then the associative array will be appended in all the values of the first array.
e.g.
<?php
$array1 = array(
    "a" => "first",
    "b" => "second",
    "c" => "something",
    "red"
);

$array2 = array(
    "a" => "first",
    "b" => "something",
    "letsc"
);

print_r(array_fill_keys($array1, $array2));
?>

The output will be
Array(
    [first] => Array(
        [a] => first,
        [b] => something,
        [0] => letsc
    ),
    [second] => Array(
        [a] => first,
        [b] => something,
        [0] => letsc
    ),
    [something] => Array(
        [a] => first,
        [b] => something,
        [0] => letsc
    ),
    [red] => Array(
        [a] => first,
        [b] => something,
        [0] => letsc
    )
)
sergli at nigma dot ru 25-May-2012 08:53
<?php
$a = array("1");

var_dump(array_fill_keys($a, "test"));
?>

array(1) {
  [1]=>
  string(4) "test"
}

now string key "1" become an integer value 1, be careful.
matrebatre 20-Jun-2008 04:28
This function does the same as:
<?php
$array = array_combine($keys,array_fill(0,count($keys),$value));
?>
phydeaux 14-May-2008 07:26
Scratchy's version still doesn't work like the definition describes.  Here's one that can take a mixed variable as the second parameter, defaulting to an empty string if it's not specified.  Don't know if this is exactly how the function works in later versions but it's at least a lot closer.

function array_fill_keys($target, $value = '') {
    if(is_array($target)) {
        foreach($target as $key => $val) {
            $filledArray[$val] = is_array($value) ? $value[$key] : $value;
        }
    }
    return $filledArray;
}

This works for either strings or numerics, so if we have

$arr1 = array(0 => 'abc', 1 => 'def');
$arr2 = array(0 => 452, 1 => 128);
$arr3 = array(0 => 'foo', 1 => 'bar');

then

array_fill_keys($arr1,$arr2)
returns: [abc] => 452, [def] => 128

array_fill_keys($arr1,0)
returns: [abc] => 0, [def] => 0

array_fill_keys($arr2,$arr3)
returns: [452] => foo, [128] => bar

array_fill_keys($arr3,'BLAH')
returns: [foo] => BLAH, [bar] => BLAH

and array_fill_keys($arr1)
returns: [abc] =>, [def] =>
Scratchy 02-May-2008 11:18
RE: bananasims at hotmail dot com

I also needed a work around to not having a new version of PHP and wanting my own keys. bananasims code doesn't like having an array as the second parameter...

Here's a slightly modified version than can handle 2 arrays as inputs:

//we want these values to be keys
$arr1 = (0 => "abc", 1 => "def");
/we want these values to be values
$arr2 = (0 => 452, 1 => 128);

function array_fill_keys($keyArray, $valueArray) {
    if(is_array($keyArray)) {
        foreach($keyArray as $key => $value) {
            $filledArray[$value] = $valueArray[$key];
        }
    }
    return $filledArray;
}

array_fill_keys($arr1, $arr2);

returns:
abc => 452, def =>128
bananasims at hotmail dot com 19-Dec-2006 02:03
Some of the versions do not have this function.
I try to write it myself.
You may refer to my script below

function array_fill_keys($array, $values) {
    if(is_array($array)) {
        foreach($array as $key => $value) {
            $arraydisplay[$array[$key]] = $values;
        }
    }
    return $arraydisplay;
}


값으로 배열 채우기

  • ≪ array_fill_keys
  • array_filter ≫
  • PHP Manual
  • 배열 함수 목록
  • 값으로 배열 채우기

array_fill

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

array_fill — 값으로 배열 채우기

설명

array array_fill ( int $start_index , int $num , mixed $value )

배열에 value 인수 값으로 num 엔트리를 채웁니다. 키는 start_index 인수에서 시작합니다.

인수

start_index

반환하는 배열의 첫 인덱스

num

삽입할 원소 수

value

채우는 데 사용할 값

반환값

채운 배열을 반환합니다.

오류/예외

num가 1보다 작으면 E_WARNING이 발생합니다.

예제

Example #1 array_fill() 예제

<?php
$a = array_fill(5, 6, 'banana');
$b = array_fill(-2, 2, 'pear');
print_r($a);
print_r($b);
?>

위 예제의 출력:

Array
(
    [5]  => banana
    [6]  => banana
    [7]  => banana
    [8]  => banana
    [9]  => banana
    [10] => banana
)
Array
(
    [-2] => pear
    [0] => pear
)

주의

음수 키에 관한 자세한 설명은 매뉴얼 배열 섹션을 참고하십시오.

참고

  • array_fill_keys() - Fill an array with values, specifying keys
  • str_repeat() - 문자열을 반복
  • range() - 원소의 범위를 가지는 배열 생성

User Contributed Notes

anatoliy at ukhvanovy dot name 16-Jun-2014 07:00
If you need negative indices:
<?php
$b = array_fill(-2, 4, 'pear');//this is not what we want
$c = array_fill_keys(range(-2,1),'pear');//these are negative indices
print_r($b);
print_r($c);
?>
Here is result of the code above:
Array
(
    [-2] => pear
    [0] => pear
    [1] => pear
    [2] => pear
)
Array
(
    [-2] => pear
    [-1] => pear
    [0] => pear
    [1] => pear
)
mchljnk at NOSPAM dot gmail dot com 10-Nov-2013 12:07
Using objects with array_fill may cause unexpected results. Consider the following:

<?php
class Foo {
   public $bar = "banana";
}

//fill an array with objects
$array = array_fill(0, 2, new Foo());

var_dump($array);
/*
array(2) {
  [0]=>
  object(Foo)#1 (1) {
    ["bar"]=>
    string(6) "banana"
  }
  [1]=>
  object(Foo)#1 (1) {
    ["bar"]=>
    string(6) "banana"
  }
} */

//now we change the attribute of the object stored in index 0
//this actually changes the attribute for EACH object in the ENTIRE array
$array[0]->bar = "apple";

var_dump($array);
/*
array(2) {
  [0]=>
  object(Foo)#1 (1) {
    ["bar"]=>
    string(5) "apple"
  }
  [1]=>
  object(Foo)#1 (1) {
    ["bar"]=>
    string(5) "apple"
  }
}
 */
?>

Objects are filled in the array BY REFERENCE. They are not copied for each element in the array.
miguelxpain at gmail dot com 25-Feb-2012 03:04
I made this function named "array_getMax" that returns te maximum value and index, from array:

<?php
//using array_search_all by helenadeus at gmail dot com

function array_search_all($needle, $haystack)
{#array_search_match($needle, $haystack) returns all the keys of the values that match $needle in $haystack

    foreach ($haystack as $k=>$v) {

        if($haystack[$k]==$needle){

           $array[] = $k;
        }
    }
    return ($array);

}

function array_getMax($array){

    $conteo=array_count_values($array);

    if( count($conteo)==1 ){//returns full array when all values are the same.
        return  $array;
    }

    arsort($array);

    //$antValue=null;
    $maxValue=null;
    $keyValue=null;
    foreach($array as $key=>$value){
        if($maxValue==null){
            $maxValue=$value;
            $keyValue=$key;
            break;
        }
    }

    $resultSearch=array_search_all($maxValue, $array);

   return  array_fill_keys($resultSearch, $maxValue);

}

//example
  $arreglo=array('e1'=>99,'e2'=>'99','e3'=>1,'e4'=>1,'e5'=>98);

    var_dump(array_getMax($arreglo));

//output
/*
array(2) {
  ["e1"]=>
  int(99)
  ["e2"]=>
  int(99)
}
*/
?>

I hope some one find this usefull
csst0266 at cs dot uoi dot gr 11-Aug-2004 07:32
This is what I recently did to quickly create a two dimensional array (10x10), initialized to 0:

<?php
  $a = array_fill(0, 10, array_fill(0, 10, 0));
?>

This should work for as many dimensions as you want, each time passing to array_fill() (as the 3rd argument) another array_fill() function.
25-Aug-2002 10:59
array_fill() cannot be used to setup only missing keys in an array. This  may be necessary for example before using implode()  on a sparse filled array.
The solution is to use this function:

<?php
function array_setkeys(&$array, $fill = NULL) {
  $indexmax = -1;
  for (end($array); $key = key($array); prev($array)) {
    if ($key > $indexmax)
      $indexmax = $key;
  }
  for ($i = 0; $i <= $indexmax; $i++) {
    if (!isset($array[$i]))
      $array[$i] = $fill;
  }
  ksort($array);
}
?>

This is usefull in some situations where you don't know which key index was filled and you want to preserve the association between a positioned field in an imploded array and the key index when exploding it.


콜백 함수를 사용하여 배열 원소를 필터

  • ≪ array_fill
  • array_flip ≫
  • PHP Manual
  • 배열 함수 목록
  • 콜백 함수를 사용하여 배열 원소를 필터

array_filter

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

array_filter — 콜백 함수를 사용하여 배열 원소를 필터

설명

array array_filter ( array $input [, callback $callback ] )

array_filter()는 input 배열안의 각값에 callback 함수에 전달하는 작업을 반복한다. callback 함수가 true를 반환하면, input의 현재값이 결과 배열로 반환된다. 배열 키들은 보존된다.

인수

input

반복할 배열

callback

사용할 콜백 함수

callback을 지정하지 않으면, input에서 FALSE와 같은(논리형으로 변환 참고) 모든 원소는 제거됩니다.

반환값

필터한 배열을 반환합니다.

예제

Example #1 array_filter() 예제

<?php
function odd($var)
{
    return ($var & 1);
}

function even($var) {
    return (!($var & 1));
}

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);

echo "홀수:\n";
print_r(array_filter($array1, "odd"));
echo "짝수:\n";
print_r(array_filter($array2, "even"));
?>

위 예제의 출력:

Odd :
Array
(
    [a] => 1
    [c] => 3
    [e] => 5
)
Even:
Array
(
    [0] => 6
    [2] => 8
    [4] => 10
    [6] => 12
)

Example #2 callback 없는 array_filter()

<?php

$entry = array(
             0 => 'foo',
             1 => 'false',
             2 => -1,
             3 => null,
             4 => ''
          );

print_r(array_filter($entry));
?>

위 예제의 출력:

Array
(
    [0] => foo
    [1] => -1
)

주의

Caution

콜백 함수에서 배열을 변경할 때(원소 추가, 삭제, unset 등), 이 함수의 동작은 정의되지 않습니다.

참고

  • array_map() - Applies the callback to the elements of the given arrays
  • array_reduce() - 콜백 함수를 사용하여 배열을 반복적으로 단일 값으로 축소
  • array_walk() - 배열의 각 원소에 대해서 특정 함수를 적용

User Contributed Notes

yuzhen huang 16-May-2015 10:49
from php  5.3,  we use anonymous function as second argument:

$a = array('a'=>1, 'b'=>2, 'c'=>false, 'd'=>0);
$b = array_filter($a, function($v){return $v !== 0;});
var_dump($b);

output:
array(3) {
  'a' =>
  int(1)
  'b' =>
  int(2)
  'c' =>
  bool(false)
}
njt1982 at yahoo dot com 30-Apr-2015 10:18
If you have an array of KV pairs and you want all the items where a value is X, you dont need to make a callback for array_filter. You can use array_intersect:

<?php
print_r(array_intersect(
  array(
    'a' => 1,
    'b' => 1,
    'c' => 1,
    'd' => 2,
    'e' => 2,
    'f' => 2,
  ),
  array(1)
));

Array
(
    [a] => 1
    [b] => 1
    [c] => 1
)
?>

The advantage of this approach is you can pass variables into the second array without needing to worry about variable scope and function parameters for array_filter.
manuelworks at gmail dot com 17-Apr-2015 12:34
I needed to get the complete array element of a multilevel array where the values of some elements where 'x' or 'y'. Kind of a WHERE  AND in a MySQL Query..

SO if you need to filter an array where 'X' = Y and 'a' = b, here you go.

<?php
/*
 * getArrayElement() by @MmO12
 *
 * SELECT an element of  an $array...
 * *$array = array( 0 => array('index1'  => 'a', 'index2' => 'b', 'value' => 'hello'),
                1 => array('index1'  => 'a', 'index2' => 'c', 'value' => 'bye'));
*
 * WHERE its $indexs values  are equal to...
 * * $indexs = array('index1' => 'a', 'index2'=>'b');
 */
function getArrayElement($array, $indexs, $justvalsplease = false){
    $newarray = false;
    //verificamos el array
    if(is_array($array) && count($array)>0){

        //verify indexs and get # of indexs
        if(is_array($indexs) && count($indexs)>0) $ninds = count($indexs);
        else return false;

        //search for coincidences
        foreach(array_keys($array) as $key){

            //index value coincidence counter.
            $count = 0;

            //for each index we search
            foreach($indexs as $indx => $val){

                //if index value is equal then counts
                if($array[$key][$indx] == $val){
                    $count++;
                }
            }
            //if indexes match, we get the array elements :)
            if($count == $ninds){

                //if you only need the vals of the first coincidence
                //witch was my case by the way...
                if($justvalsplease) return $array[$key];
                else $newarray[$key] = $array[$key];
            }
        }
    }
  return $newarray;
}
?>

/*******
Example
********/
<?php
   //array
    $array = array( 0 => array('index1'  => 'a', 'index2' => 'b', 'value' => 'hello'),
                  1 => array('index1'  => 'a', 'index2' => 'c', 'value' => 'bye'),
                  2 => array('index1'  => 'b', 'index2' => 'a', 'value' => 'hi'));

    //indexes
    $indexs = array('index1' => 'a', 'index2'=>'b');

    //aaand we get the element we want.
    $element = getArrayElement($array, $indexs);

        print_r($element);

    /***prints ***/
    Array
    (
        [0] => Array
            (
                [index1] => a
                [index2] => b
                [value] => hello
            )

    )?>

IF you want only the array inside the elements just set $justvalsplease true like this
<?php
     $element = getArrayElement($array, $indexs,true);
?>

And you will get just the element content
<?php
Array
(
    [index1] => a
    [index2] => b
    [value] => hello
)
?>
no at mail dot com 27-Feb-2015 12:34
Infinite shame on PHP that array_filter does not transparently work for both arrays and associative arrays. Why in the name of god an array and an associative array return both the same type ?. Unfortunately I can't decide the language to program in otherwise I would move to a more consistent language.
robbieaverill at gmail dot com 22-Jan-2015 07:32
If you want to use array_filter with a class method as the callback, you can use a psuedo type callback like this:

<?php
class Test
{
    public function doFilter($array)
    {
        return array_filter($array, array($this, 'callbackMethodName'));
    }

    protected function callbackMethodName($element)
    {
        return $element % 2 === 0;
    }
}

$example = new Test;
print_r($example->doFilter(range(1, 10)));
?>

Will return even numbers.
iancudanielc () gmail ! com 09-Jan-2015 05:56
If you want to pass the key to the callback function before PHP 5.6.0 (when the flag parameter wasn't implemented):

<?php

$result = array_filter(array_keys($array), 'is_int');

?>
m dot abbasian at outlook dot com 18-Oct-2014 05:58
If you want a quick way to Find Numbers ( remove NULL, FALSE and Empty Strings (""), all Strings) but leave values of 0 (zero)
eg:
<?php
/*
 Find Numbers (removes all NULL, FALSE and Empty Strings, all Strings) but leaves 0 (zero) values
*/

$result = array_filter( array( "0",0,1,2,3,'text' ) , 'is_numeric' );

var_dump($result);
/*
  array (size=5)
  0 => string '0' (length=1)
  1 => int 0
  2 => int 1
  3 => int 2
  4 => int 3
*/
?>
manwe at inversion dot pl 21-Sep-2014 09:33
array_filter remove also FALSE and 0. To remove only NULL's use:

$af = [1, 0, 2, null, 3, 6, 7];

function is_not_null($val){
    return !is_null($val);
}
$af = array_filter($af, 'is_not_null');
marc dot gray at gmail dot com 29-Apr-2014 07:44
My favourite use of this function is converting a string to an array, trimming each line and removing empty lines:

<?php
$array = array_filter(array_map('trim', explode("\n", $string)), 'strlen');
?>

Although it states clearly that array keys are preserved, it's important to note this includes numerically indexed arrays. You can't use a for loop on $array above without processing it through array_values() first.
Anonymous 02-Apr-2014 07:26
<?php
function arrayFilter($input, $callback = null) {
    foreach($input as $key => &$value) {
        if(is_array($value)) {
            $value = arrayFilter($value, $callback);
            if(is_array($value)) {
                if((bool)$value === false) unset($input[$key]);
            }
            else {
                if((bool)($callback ? $callback($value) : $value) === false) unset($input[$key]);
            }
        }
        else {
            if((bool)($callback ? $callback($value) : $value) === false) unset($input[$key]);
        }
    }
    return $input;
}
?>
avl 04-Nov-2013 09:25
nice trick:

$array_out = array_filter($array_in, function($var) use($array_other) {
            return in_array($var, $array_other) ? true : false;
});
qeremy [atta] gmail [dotta] com 03-Oct-2013 12:12
Here is key-passed array_filter function.

<?php
function arrayfilter(array $array, callable $callback = null) {
    if ($callback == null) {
        $callback = function($key, $val) {
            return (bool) $val;
        };
    }
    $return = array();
    foreach ($array as $key => $val) {
        if ($callback($key, $val)) {
            $return[$key] = $val;
        }
    }
    return $return;
}

$test_array = array('foo', 'a' => 'the a', 'b' => 'the b', 11 => 1101, '', null, false, 0);

$array = arrayfilter($test_array, function($key, $val) {
   return is_string($key);
});
print_r($array);
/*
Array
(
    [a] => the a
    [b] => the b
)
*/

$array = arrayfilter($test_array);
print_r($array);
/*
Array
(
    [0] => foo
    [a] => the a
    [b] => the b
    [11] => 1101
)
*/
?>
tomasz at trejderowski dot pl 24-Sep-2013 05:16
Read "callback" parameter note with understanding (as well as "converting to boolean" chapter). Keep in midn, that 0, both:

* integer: 0 and
* float: 0.00

evaluates to boolean FALSE! And therefore all array nodes, that have such value WILL ALSO BE FILTERED by array_filter(), with default call back. Unless you provide your own callback function, that will (for example) filter only empty strings and NULLs, but leave "zeros" untouched.

Some people (including me) might be surprised to find this out.
keksnicoh at googlemail dot com 12-Aug-2013 08:01
Usefull invertation of array_filter
<?php
$data = array(...)
$noArrayData = array_filter_inverse($data, 'is_array');

/**
 * Inverts the result of the filter callback.
 * @param array $data
 * @param callable $f
 * @return array
 */
function array_filter_inverse(array $data, callable $f)
{
    return array_filter($data, function() use(&$f) {
        return ! call_user_func_array($f, func_get_args());
    });
}
?>
pavian at gmail dot com 25-Jul-2013 07:04
Regarding to strlen() related notes, if you want to filter NULL, FALSE and empty strings from the array, just omit callback function, it does the same trick (but easier).
<?php

  $clean = array_filter($array);

?>
Alix Axel 10-May-2013 09:56
If you need this function to return falsy results (like "0"), yet discard truly empty ones (that have a 0 string length, like null, false, ''), you can use strlen() as the callback:

<?php
array_filter($array, 'strlen');
?>
espertalhao04 at hotmail dot com 05-Apr-2013 08:58
breich at reich-consulting dot net  made an awesome function, but it has a poor performance, and is bloated.

here is a shorter version:
<?php
function array_remove_keys($array, $keys = array(), $callback=null) {
    if(empty($array) || (! is_array($array))) {
        return $array;
    }

    if(is_string($keys) && !is_array($keys=explode(',',$keys)))
    {
        return $array;
    }

    foreach($keys as $key) {
        $key=trim($key,' ');
        if(!$callback || !$callback($array[$key],$key))
            unset($array[$key]);
    }
    return $array;
}
?>

i decided to let here stay 2 conditions, even though they can be rewritten to 1.

unsetting is faster than array_diff_keys.

sometimes, people separate strings like 'a, b, c' or 'a , b , c'...

i added support for those, since they are essentially the same list.

i also added a callback option, which you specify a function and it will give the value and the key to it.

p.s.: php uses short-circuit evaluation. example:
!$callback || !$callback($array[$key],$key)

if $callback is empty, it doesn't try to run $callback().
why?
because !$callback || !$callback($array[$key],$key) will be true, no matter the value of !$callback().
so, to save resources and time, php jumps if !$callback is true
g dot kuizinas at anuary dot com 26-Mar-2013 08:10
<?php
function array_filter_recursive ($data) {
    $original = $data;

    $data = array_filter($data);

    $data = array_map(function ($e) {
        return is_array($e) ? array_filter_recursive($e) : $e;
    }, $data);

    return $original === $data ? $data : array_filter_recursive($data);
}

$data = ['a' => 0, 'b' => [], 'c' => [[]], 'd' => [[[[]]]], 'e' => 'foo', 'f' => [[['a']]], [true], [[],['a'], [true, false]]];

$data = array_filter_recursive($data);
?>
tx at tshwarelo-leaka dot co dot za 26-Mar-2013 06:26
To get rid of all white space in an array without looping.
<?php
   $array = array(5, "   ", 2, NULL, 13, "", 7, "\n", 4, "\t");
   print_r($array);
   $result = array_filter($array, create_function('$a','return preg_match("#\S#", $a);'));
   print_r($result);
?>
Array
(
    [0] => 5
    [1] =>
    [2] => 2
    [3] =>
    [4] => 13
    [5] =>
    [6] => 7
    [7] =>

    [8] => 4
    [9] =>
)
Array
(
    [0] => 5
    [2] => 2
    [4] => 13
    [6] => 7
    [8] => 4
)
Anonymous 14-Jan-2013 02:52
If you want a quick way to remove NULL, FALSE and Empty Strings (""), but leave values of 0 (zero), you can use the standard php function strlen as the callback function:
eg:
<?php

// removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values
$result = array_filter( $array, 'strlen' );

?>
jtreminio at gmail dot com 05-Oct-2012 04:11
You can use array_filter from within a class to access a protected method from that same class:

<?php

class Bar {
    public function foo()
    {
        $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);

        print_r(array_filter($array1, array($this, 'naz')));
    }

    protected function baz($var)
    {
        return($var & 1);
    }
}

$bar = new Bar();
$bar->foo();
?>
John Erck: erck0006 at junkyo dot gmail dot com 05-Apr-2012 05:09
<?php
// ARRAY FILTER RECURSIVE USING CLASS, STATIC METHOD, AND ANONYMOUS CALLBACK FUNCTION
// NOTE THAT THE CALLBACK HAS ACCESS TO BOTH THE KEY AND VALUE

// THE CLASS (FOR YOU TO COPY)
class ArrayUtil
{
    public static function FilterRecursive(Array $source, $fn)
    {
        $result = array();
        foreach ($source as $key => $value)
        {
            if (is_array($value))
            {
                $result[$key] = self::FilterRecursive($value, $fn);
                continue;
            }
            if ($fn($key, $value))
            {
                $result[$key] = $value; // KEEP
                continue;
            }
        }
        return $result;
    }
}

// EXAMPLE ANONYMOUS CALLBACK FUNCTION
$fn = function ($key, $value)
{
    if (strpos($key, 'drop') !== FALSE)
    {
        return FALSE; // DROP
    }
    return TRUE; // KEEP
};

// EXAMPLE PRE FILTER TEST DATA
$preFilter = array(
    'a' => 'one',
    'b' => array(
        'example_drop' => 'filter me out',
        'example_keep' => 'keep me',
    ),
    'c' => 'three',
);

// EXAMPLE USAGE CODE
echo '// print_r($preFilter);' . "\n";
print_r($preFilter);
$postFilter = ArrayUtil::FilterRecursive($preFilter, $fn);
echo "\n";
echo '// print_r($postFilter);' . "\n";
print_r($postFilter);

/* OUTPUT OPEN
// print_r($preFilter);
Array
(
    [a] => one
    [b] => Array
        (
            [example_drop] => filter me out
            [example_keep] => keep me
        )

    [c] => three
)

// print_r($postFilter);
Array
(
    [a] => one
    [b] => Array
        (
            [example_keep] => keep me
        )

    [c] => three
)
OUTPUT CLOSE */
spam dot 2011 at rebell dot at 21-Nov-2011 11:28
If you have not noticed already - array_filter() can be used to remove empty elements, since an empty string considered "false", if you not specify a callback

Keep in mind, that this will remove also some other values - so if you want a quick "remove empty elements from array" this function will be fine, as long as you dont have anything to keep, which casts to "false"
lisachenko dot it at HUMAN dot gmail dot com 22-Jul-2011 09:54
You can access the current key of array by passing a reference to array into callback function and call key() and next() method in the callback function:
<?php
$data = array('first' => 1, 'second' => 2, 'third' => 3);
$data = array_filter($data, function ($item) use (&$data) {
    echo "Filtering key ", key($data), '<br>', PHP_EOL;
    next($data);
    return false;
});
?>

However be careful with array internal pointer or use reset() method before calling array_filter().
breich at reich-consulting dot net 15-Dec-2010 08:19
I built the following array_remove_keys() function to
remove one or more keys from an array.

<?php

function array_remove_keys($array, $keys = array()) {

    // If array is empty or not an array at all, don't bother
    // doing anything else.
    if(empty($array) || (! is_array($array))) {
        return $array;
    }

    // If $keys is a comma-separated list, convert to an array.
    if(is_string($keys)) {
        $keys = explode(',', $keys);
    }

    // At this point if $keys is not an array, we can't do anything with it.
    if(! is_array($keys)) {
        return $array;
    }

    // array_diff_key() expected an associative array.
    $assocKeys = array();
    foreach($keys as $key) {
        $assocKeys[$key] = true;
    }

    return array_diff_key($array, $assocKeys);
}

// Example:
$data = array(
    'name' => 'Brian',
    'address1' => '98 Market St.',
    'address2' => 'N/A'
);

// Output before array_remove_keys()
var_dump($data);

// Remove address2 key.
$data = array_remove_keys($data, 'address2');

// Output after array_remove_keys()
var_dump($data);

/* Output:

array(3) {
  ["name"]=>
  string(5) "Brian"
  ["address1"]=>
  string(13) "98 Market St."
  ["address2"]=>
  string(3) "N/A"
}
array(2) {
  ["name"]=>
  string(5) "Brian"
  ["address1"]=>
  string(13) "98 Market St."
}
*/
?>
rolf at example dot com 08-Nov-2010 08:06
Here is how you could easily delete a specific value from an array with array_filter:

<?php
$array = array (1, 3, 3, 5, 6);
$my_value = 3;
$filtered_array = array_filter($array, function ($element) use ($my_value) { return ($element != $my_value); } );
print_r($filtered_array);
?>

output:

Array
(
    [0] => 1
    [3] => 5
    [4] => 6
)
Peter Robinett 12-Aug-2010 05:09
Because array_filter() preserves keys, you should consider the resulting array to be an associative array even if the original array had integer keys for there may be holes in your sequence of keys. This means that, for example, json_encode() will convert your result array into an object instead of an array. Call array_values() on the result array to guarantee json_encode() gives you an array.
acid24 at gmail dot com 26-Jul-2010 02:58
A function that allows filtering an array by keys:

<?php
function array_filter_key( $input, $callback ) {
    if ( !is_array( $input ) ) {
        trigger_error( 'array_filter_key() expects parameter 1 to be array, ' . gettype( $input ) . ' given', E_USER_WARNING );
        return null;
    }

    if ( empty( $input ) ) {
        return $input;
    }

    $filteredKeys = array_filter( array_keys( $input ), $callback );
    if ( empty( $filteredKeys ) ) {
        return array();
    }

    $input = array_intersect_key( array_flip( $filteredKeys ), $input );

    return $input;
}

?>

Example:

<?php
$input = array_flip( range( 'a', 'z' ) );

$consonants = array_filter_key( $arr, function( $elem ) {
    $vowels = "aeiou";
    return strpos( $vowels, strtolower( $elem ) ) === false;
} );
?>

Outputs:

array(21) {
  ["b"]=>
  int(1)
  ["c"]=>
  int(2)
  ["d"]=>
  int(3)
  ["f"]=>
  int(5)
  ["g"]=>
  int(6)
  ["h"]=>
  int(7)
  ["j"]=>
  int(9)
  ["k"]=>
  int(10)
  ["l"]=>
  int(11)
  ["m"]=>
  int(12)
  ["n"]=>
  int(13)
  ["p"]=>
  int(15)
  ["q"]=>
  int(16)
  ["r"]=>
  int(17)
  ["s"]=>
  int(18)
  ["t"]=>
  int(19)
  ["v"]=>
  int(21)
  ["w"]=>
  int(22)
  ["x"]=>
  int(23)
  ["y"]=>
  int(24)
  ["z"]=>
  int(25)
}
Ant P. 13-Mar-2010 05:58
If you're using filter_input_array, the values will be null on failure and anything else on success. Because array_filter by default removes false, 0 and "" you need to do extra work like this:
<?php
$input_array = filter_input_array(INPUT_GET, array(
  'var1' => FILTER_VALIDATE_BOOLEAN,
  'var2' => FILTER_VALIDATE_INT
));

array_filter($input_array, function($a) { return !is_null($a) });
?>
webdesign at blackbyrd dot biz 08-Oct-2009 10:00
Here's a function that will filter a multi-demensional array. This filter will return only those items that match the $value given

<?php
    /*
     * filtering an array
     */
    function filter_by_value ($array, $index, $value){
        if(is_array($array) && count($array)>0)
        {
            foreach(array_keys($array) as $key){
                $temp[$key] = $array[$key][$index];

                if ($temp[$key] == $value){
                    $newarray[$key] = $array[$key];
                }
            }
          }
      return $newarray;
    }
?>

Example:

<?php
$results = array(
   0 => array('key1' => '1', 'key2' => 2, 'key3' => 3),
   1 => array('key1' => '12', 'key2' => 22, 'key3' => 32)
);

$nResults = filter_by_value($results, 'key2', '2');
?>

Output :

array(
    0 => array('key1' => '1', 'key2' => 2, 'key3' => 3)
);
mewsterus at yahoo dot com 14-Sep-2009 07:13
Here's an easy way to get a combination of keys and values, such that if you don't specify a key you will get the value, and if you do specify a key you will get the key:

<?php
$array = array('One' => 'First', 'Second', 'Third', 'Four' => 'Fourth', 'Fifth');

var_dump(array_keys($array));

$names = array_filter(array_keys($array), 'is_string') + array_values($array);
ksort($array);

var_dump($names);
?>

Outputs:

array(5) {
  [0]=>string(3) "One"
  [1]=>int(0)
  [2]=>int(1)
  [3]=>string(4) "Four"
  [4]=>int(2)
}
array(5) {
  [0]=>string(3) "One"
  [1]=>string(6) "Second"
  [2]=>string(5) "Third"
  [3]=>string(4) "Four"
  [4]=>string(5) "Fifth"
}

Without using ksort, the keys appear before the fallback values, instead of inline and appearing like they replace them, however the keys are intact (which is why ksort works) so it's only execution order.
mchargue at usc dot edu 07-Aug-2009 11:34
Wanting to pass an additional to parameter to the callback function?  This worked for me, there's probably another way to accomplish this task but just so you see how it can be done.  (I actually used this technique to strip old dates out of an array) :

<?php
//define in global scope so functions can access
$var_to_pass = null;

function myfilter($input_var_outer,$param) {

    global $var_to_pass;
    $var_to_pass = $param;

    function mycallback($input_var_inner) {
      global $var_to_pass;
      return ($input_var_inner>$var_to_pass) ? true : false;
    }

    $return_arr = array_filter($input_var_outer,'mycallback');
    //re-key if you want
    $return_arr = array_merge(array(),$return_arr);
    return $return_arr;

}

$min = 5;
$a = array(1,3,5,7,9);

//remove elements from array that are not greater than $min
$a = myfilter($a,$min);

echo "<pre>";
print_r($a);
echo "</pre>";
?>

--

Output:
Array
(
    [0] => 7
    [1] => 9
)
chrisstocktonaz at gmail dot com 30-Apr-2009 06:34
I use the following to see if a array consist of scalar values or null, but of course you could mix it up using any of the is_ functions.

<?php
if(count($data) !== count(array_filter($data, 'is_scalar') + array_filter($data, 'is_null'))) {
  throw new Exception('Array did not consist of scalar and null values');
}
?>
niehztog 30-Dec-2008 10:27
In case you are interested (like me) in filtering out elements with certain key-names, array_filter won't help you. Instead you can use the following:

<?php
$arr = array( 'element1' => 1, 'element2' => 2, 'element3' => 3, 'element4' => 4 );
$filterOutKeys = array( 'element1', 'element4' );

$filteredArr = array_diff_key( $arr, array_flip( $filterOutKeys ) )
?>

Result will be something like this:
['element2'] => 2
['element3'] => 3
romain dot lamarche at gmail dot com 11-Dec-2008 03:39
This function filters an array and remove all null values recursively.

<?php
  function array_filter_recursive($input)
  {
    foreach ($input as &$value)
    {
      if (is_array($value))
      {
        $value = array_filter_recursive($value);
      }
    }

    return array_filter($input);
  }
?>

Or with callback parameter (not tested) :

<?php
  function array_filter_recursive($input, $callback = null)
  {
    foreach ($input as &$value)
    {
      if (is_array($value))
      {
        $value = array_filter_recursive($value, $callback);
      }
    }

    return array_filter($input, $callback);
  }
?>
darren at dazwin dot com 08-Oct-2008 08:44
Regarding comment about trimming empty strings, the code posted will get into an infinite loop if the array is reduced to zero elements. The following might be better:

<?php
function array_trim($array) {
    while (!empty($array) and strlen(reset($array)) === 0) {
        array_shift($array);
    }
    while (!empty($array) and strlen(end($array)) === 0) {
        array_pop($array);
    }
    return $array;
}
?>
sami 04-Oct-2008 08:11
Yes, it may remove NULLS, but it also removes anything that factors to a FALSE as well; like FALSE and ZERO. :/
quecoder at gmail 27-Aug-2008 12:00
// my implementation for array_filter

function my_array_filter($array,$function,$preserve=true)
        {
            $return = array();
            foreach ($array as $k=>$v)
                {
                    if($function($v)==true) $return[$k]=$v;
                }
                if($preserve) return $return;
                else return array_values($return);
        }

function odd($value)
        {
            return ($value & 1);
        }

$oddonly = array (1,2,3,4,5,6,7,8,9);
print_r(my_array_filter($oddonly,'odd',1));

//output
//Array ( [0] => 1 [2] => 3 [4] => 5 [6] => 7 [8] => 9 )

//Khaled Mohammed
WASD 29-Jul-2008 01:53
You can easily delete all NULL elements from array with following statement:

<?php
$arr = array_filter($arr);
?>
Martin 04-Sep-2007 07:56
This function trims empty strings from the beginning and end of an array.
It's useful when outputing plaintext files on a page and you want to skip empty lines at the beginning and end, but not within the text.

<?php
function array_trim($array) {
    while (strlen(reset($array)) === 0) {
        array_shift($array);
    }
    while (strlen(end($array)) === 0) {
        array_pop($array);
    }
    return $array;
}
?>

You might want to trim each element too.
Hayley Watson 30-Aug-2007 12:49
Just a simplification of my function from last month.

<?php
function partition($input, $callback=null)
{
    if(is_null($callback))
        $true = array_filter($input);
    else
        $true = array_filter($input, $callback);

    $false = array_diff_key($input, $true);
}
?>
leon at darkk dot net dot ru 22-May-2007 09:15
Here is a way to get customizable filter

<?php
function blablabla() {
    ....
    $new = getNewUidls();
    class UidlFilter {
        function UidlFilter($uidls) { $this->uidls = $uidls; }
        function filter($metamsg) { return in_array($metamsg['uidl'], $this->uidls); }
    }
    $msglist = array_filter($msglist, array(new UidlFilter($new), 'filter');
    ....
}
?>
Fladnag - bahatest at ifrance dot com 22-Oct-2006 06:19
If you have a form with multiple checkbox having ID element as value for selection in a list, you probably have a SQL request like :
$req="SELECT ... WHERE ID IN (".implode(',', array_keys($choices)).")";
without quote before and after choices keys because they are numeric values... but in fact, they can be string values, and a SQL injection problem.

with array_filter, you can easily filter bad values :
<?php
    $choices=array('A'=>'on', -1=>'on', 0=>'on', 1=>'on', 12=>'on', "1)or 1=1--"=>'on');
    print_r($choices);
    $choices=array_filter(array_keys($choices), 'is_numeric');
    print_r($choices);
?>
will print :
<?php
Array
(
    [A] => on
    [-1] => on
    [0] => on
    [1] => on
    [12] => on
    [1)or 1=1--] => on
)
Array
(
    [1] => -1
    [2] => 0
    [3] => 1
    [4] => 12
)
?>
ydotzhangatwriwindberdotorg 17-Jan-2006 10:57
I have written a function that will filter an array by the frequency of
element value in the array.  This may be useful to some people.

<?php
/////////////////////////////////////////////////////////////////
//    Filter an array by value freguebcy
//    Input: $array
//    cut-off: $frequency (>=1)
//    result option option: 1=$frequency and higher
//          0=$frequency only
//          -1=$frequency and lower
/////////////////////////////////////////////////////////////////
function filter_array($array, $frequency = 2, $include = 1){
    $freg = array_count_values($array);
    if($frequency<1){
       print "** frequency cut-off should be >= 1! **\n";
       return false;
    }
    foreach($freg as $k => $v){
        if($include == 0){
            if($frequency != $v){
                $freg[$k] = 0;
            }
        }elseif($include > 0){
            if($frequency > $v){
                $freg[$k] = 0;
            }
        }else{
            if($frequency < $v){
                $freg[$k] = 0;
            }
        }
    }
    $filtered = array_filter($freg);
    $values = array_keys($filtered);
    return array_intersect($array,$values);
}
?>
xert 26-Apr-2005 02:14
According to a simple test with array_filter($array) and array_diff($array, array('')) is array_filter 2.5 times faster than array_diff when deleting empty entries.
marc dot vanwoerkom at fernuni-hagen dot de 05-Jul-2004 05:09
Some of PHP's array functions play a prominent role in so called functional programming languages, where they show up under a slightly different name:

<?php
  array_filter() -> filter(),
  array_map() -> map(),
  array_reduce() -> foldl() ("fold left")
?>

Functional programming is a paradigm which centers around the side-effect free evaluation of functions. A program execution is a call of a function, which in turn might be defined by many other functions. One idea is to use functions to create special purpose functions from other functions.

The array functions mentioned above allow you compose new functions on arrays.

E.g. array_sum = array_map("sum", $arr).

This leads to a style of programming that looks much like algebra, e.g. the Bird/Meertens formalism.

E.g. a mathematician might state

  map(f o g) = map(f) o map(g)

the so called "loop fusion" law.

Many functions on arrays can be created by the use of the foldr() function (which works like foldl, but eating up array elements from the right).

I can't get into detail here, I just wanted to provide a hint about where this stuff also shows up and the theory behind it.
Maxwel Leite 11-May-2004 05:17
For any type of array. Basead in redshift code.

<?php
function array_clean ($array, $todelete = false, $caseSensitive = false) {
    foreach($array as $key => $value) {
        if(is_array($value)) {
            $array[$key] = array_clean($array[$key], $todelete, $caseSensitive);
        }
        else {
            if($todelete) {
                if($caseSensitive) {
                    if(strstr($value ,$todelete) !== false)
                        unset($array[$key]);
                }
                else {
                    if(stristr($value, $todelete) !== false)
                        unset($array[$key]);
                }
            }
            elseif (empty($value)) {
                unset($array[$key]);
            }
        }
    }
    return $array;
}
?>
Jeremy 29-Dec-2003 10:31
Here is a nice little function which will apply a callback function recursively over a multidimensional array. If the callback function returns false, then it replaces the value of the array with $filtered_ouput. This function gracefully handles objects inside of arrays (and objects within objects within arrays, etc). It is specifically designed for your callback function to process on the array key's (unlike normal array_filter which filters on the values), but it could work on the array values depending on your test criteria (YMMV).

<?PHP

function array_key_filter_multi($array, $callback, $filtered_output = "")
{
   $ret = array();
   foreach($array as $key=>$value) {
       if($callback($key,$value)) {
           if(is_array($value)) {
               $ret[$key] = array_key_filter_multi($value, $callback, $filtered_output);
           }
           elseif(is_object($value)) {
               $ret[$key] = array_key_filter_multi(get_object_vars($value), $callback, $filtered_output);
           }
           else {
               $ret[$key]=$value;
           }
       }
       else {
           $ret[$key]=$filtered_output;
       }
   }
   return $ret;
}

?>

We use this to filter redundant data from debugging output. An example usage is:

<?

$callback_func = create_function('$key, $value', 'return ($key == "db" || $key == "smarty") ? false : true;');
echo "<PRE>" . print_r(array_key_filter_multi($_SESSION, $callback_func, "**filtered by function**"), true) . "</PRE>";

?>

Which filters all keys with "db" or "smarty" as their name (including objects which have a reference to those variables). The output of the above in a test case I did is the following:

Array
(
    [userdata] => Array
        (
            [sid] => a130e675d380e0e9fe47897922d719ac
            [not_in_db] =>
            [user_id] => 1
            [session_id] => 154
            [permissions] => 1
            [username] => tester
        )
    [systemobjects] => Array
        (
            [db] => **filtered by function**
            [smarty] => **filtered by function**
        )
)
redshift at pandora dot be 28-Jun-2003 09:01
Hi all,
Here's a function that will look trough an array, and removes the array member when the search string is found.

<?php
function array_clean ($input, $delete = false, $caseSensitive = false)
    {
    $i = 0;
    while($i < count($input))
        {
        if($delete)
            {
            if($caseSensitive)
                {
                if(!strstr($input[$i] ,$delete))
                    {
                    $return[] = $input[$i];
                    }
                }
                else
                {
                if(!stristr($input[$i], $delete))
                    {
                    $return[] = $input[$i];
                    }
                }
            }
            else
            {
            if(!empty($input[$i]))
                {
                $return[] = $input[$i];
                }
            }
        $i++;
        }
    return $return;
    }
?>

array array_clean(array input [, string needle [, boolean case sensitive]])

if needle is left empty, the function will delete the array members that have no value (this means if it's empty).
NOTE: It rebuilds the array from scratch, so keys begin with 0, like you would create a new array.

Example:
$array = array("John", "Doe", "Macy");
$array = array_clean($array, "doe", false);

print_r($array);
would return:
array
(
    [0] => John
    [1] => Macy
)

Hopes this helps someone :-)
skd2 at ece dot msstate dot edu 15-May-2003 12:24
The following function modifies the supplied array recursively so that filtering is performed on multidimentional arrays as well, while preserving keys.

<?php
function array_cleanse(&$arr){

$temp = array();
reset($arr);
if(count($arr) == 0) return "";

foreach($arr as $key=>$val):

 (is_array($val))? array_cleanse($val) : NULL;
 ($val)? $temp[$key] = $val : NULL;

endforeach;

$arr = $temp;
reset($arr);
}
?>

$arr1 = array('a'=>20,'b'=>array(''),'c'=>array(1,0,2),'d'=>0);
array_cleanse($arr1);
$arr1 will be array('a'=>20,'c'=>array(1,2))

array_filter may not be used as it does not modify the array within itself.
ajohnson at speakeasy dot org 27-Sep-2002 09:42
be careful with the above function "array_delete"'s use of the stristr function, it could be slightly misleading. consider the following:

<?php
function array_delete($array, $filterforsubstring){
    $thisarray = array ();
    foreach($array as $value)
        if(stristr($value, $filterforsubstring)===false && strlen($value)>0)
            $thisarray[] = $value;
    return $thisarray;
}

function array_delete2($array, $filterforstring, $removeblanksflag=0){
    $thisarray = array ();
    foreach($array as $value)
        if(!(stristr($value, $filterforstring) && strlen($value)==strlen($filterforstring))
                && !(strlen($value)==0 && $removeblanksflag))
            $thisarray[] = $value;
    return $thisarray;
}

function array_delete3($array, $filterfor, $substringflag=0, $removeblanksflag=0){
    $thisarray = array ();
    foreach($array as $value)
        if(
            !(stristr($value, $filterfor)
                && ($substringflag || strlen($value)==strlen($filterfor))
            )
            && !(strlen($value)==0 && $removeblanksflag)
        )
            $thisarray[] = $value;
    return $thisarray;
}

$array1 = array ('the OtHeR thang','this', 'that', 'OtHer','', 9, 101, 'fifty', ' oTher', 'otHer ','','other','Other','','other blank things');

echo "<pre>array :\n";
print_r($array1);

$array2=array_delete($array1, "Other");

echo "array_delete(\$array1, \"Other\"):\n";
print_r($array2);

$array2=array_delete2($array1, "Other");

echo "array_delete2(\$array1, \"Other\"):\n";
print_r($array2);

$array2=array_delete2($array1, "Other",1);

echo "array_delete2(\$array1, \"Other\",1):\n";
print_r($array2);

$array2=array_delete3($array1, "Other",1);

echo "array_delete3(\$array1, \"Other\",1):\n";
print_r($array2);

$array2=array_delete3($array1, "Other",0,1);

echo "array_delete3(\$array1, \"Other\",0,1):\n";
print_r($array2);
 ?>
ajohnson at speakeasy dot org 17-Aug-2002 10:04
I was looking for a function to delete values from an array and thought I had found it in array_filter(), however, I *didn't* want the keys to be preserved *and* I needed blank values cleaned out of the array as well. I came up with the following (with help from many of the above examples):

<?php
function array_delete($array, $filterfor){
  $thisarray = array ();
  foreach($array as $value)
    if(stristr($value, $filterfor)===false && strlen($value)>0)
      $thisarray[] = $value;
  return $thisarray;
}

$array1 = array ('OtHeR','this', 'that', 'Other','', 9, 101, 'fifty', 'other','','');

echo "<pre>array :\n";
print_r($array1);

$array2=array_delete($array1, "Other");

echo "filtered:\n";
print_r($array2);
?>


배열 안의 모든 키를 각 키의 연관 값과 교체

  • ≪ array_filter
  • array_intersect_assoc ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열 안의 모든 키를 각 키의 연관 값과 교체

array_flip

(PHP 4, PHP 5, PHP 7)

array_flip — 배열 안의 모든 키를 각 키의 연관 값과 교체

설명

array array_flip ( array $trans )

array_flip()은 뒤집힌 순서의 array를 반환한다. 즉, trans의 키는 값이 되고, trans의 값은 키가 된다.

trans의 값들은 유효한 키가 되어야 한다는 것에 주의한다. 즉, 그 값들은 integer나 string이 될 필요가 있다. 어떤 값이 잘못된 타입을 갖으면 경고메시지가 보일것이다. 그리고 결과적으로 키/값 쌍은 뒤집히지 않을것이다.

같은 값이 여러번 출현하면, 그 값의 제일 마지막 키를 사용할것이고, 나머지 모두 사라지게 될것이다.

인수

trans

뒤집을 키/값 쌍의 배열.

반환값

성공시엔 뒤집은 배열을, 실패시엔 FALSE를 반환합니다.

예제

Example #1 array_flip() 예제

<?php
$trans = array_flip($trans);
$original = strtr($str, $trans);
?>

Example #2 array_flip() 예제 : 충돌

<?php
$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);
print_r($trans);
?>

이제 $trans는:

Array
(
    [1] => b
    [2] => c
)

참고

  • array_values() - 배열의 모든 값을 반환
  • array_keys() - 배열의 모든 키를 반환
  • array_reverse() - 원소를 역순으로 가지는 배열을 반환

User Contributed Notes

info at sabastore dot net 28-Apr-2016 05:57
note :: array_flip is a changer for key and value and a auto unique like array_unique :

<?php
/*
sabastore
 */
$intArray1 = array(-4,1,1,3);
print_r($intArray1);
$intArray1 = array_flip($intArray1);
print_r($intArray1);
?>
Prabhas Gupte 27-Jan-2015 03:48
array_flip() does not retain the data type of values, when converting them into keys. :(

<?php
$arr = array('one' => '1', 'two' => '2', 'three' => '3');
var_dump($arr);
$arr2 = array_flip($arr);
var_dump($arr2);
?>

This code outputs this:
array(3) {
  ["one"]=>
  string(1) "1"
  ["two"]=>
  string(1) "2"
  ["three"]=>
  string(1) "3"
}
array(3) {
  [1]=>
  string(3) "one"
  [2]=>
  string(3) "two"
  [3]=>
  string(5) "three"
}

It is valid expectation that string values "1", "2" and "3" would become string keys "1", "2" and "3".
grimdestripador at hotmail dot com 25-Feb-2014 09:06
<?php
function array_flip_into_subarray($input){
$output = array();
foreach ($input as $key=>$values){
    foreach ($values as $value){
        $output[$value][] = $key;
    }
}
return $output;
}
Tony H 29-Apr-2013 11:59
This function is useful when parsing a CSV file with a heading column, but the columns might vary in order or presence:

<?php

$f = fopen("file.csv", "r");

/* Take the first line (the header) into an array, then flip it
so that the keys are the column name, and values are the
column index. */
$cols = array_flip(fgetcsv($f));

while ($line = fgetcsv($f))
{
    // Now we can reference CSV columns like so:
    $status = $line[$cols['OrderStatus']];
}

?>

I find this better than referencing the numerical array index.
Anonymous 05-Apr-2012 07:17
Similarly, if you want the last value without affecting the pointer, you can do:

<?php

$array = array("one","two","three");

echo next($array); // "two"

$last = array_pop(array_keys(array_flip($array)));

echo $last;  // "three"

echo current($array); // "two"

?>
kjensen at iaff106 dot com 06-Jan-2012 07:23
I needed a way to flip a multidimensional array and came up with this function to accomplish the task.  I hope it helps someone else.

<?php
function multi_array_flip($arrayIn, $DesiredKey, $DesiredKey2=false, $OrigKeyName=false) {
$ArrayOut=array();
foreach ($arrayIn as $Key=>$Value)
    {
        // If there is an original key that need to be preserved as data in the new array then do that if requested ($OrigKeyName=true)
        if ($OrigKeyName) $Value[$OrigKeyName]=$Key;
        // Require a string value in the data part of the array that is keyed to $DesiredKey
        if (!is_string($Value[$DesiredKey])) return false;

        // If $DesiredKey2 was specified then assume a multidimensional array is desired and build it
        if (is_string($DesiredKey2))
        {
            // Require a string value in the data part of the array that is keyed to $DesiredKey2
            if (!is_string($Value[$DesiredKey2])) return false;

            // Build NEW multidimensional array
            $ArrayOut[$Value[$DesiredKey]][$Value[$DesiredKey2]]=$Value;
        }

            // Build NEW single dimention array
        else $ArrayOut[$Value[$DesiredKey]][]=$Value;
    }
return $ArrayOut;
}//end multi_array_flip
?>
Final 16-Dec-2011 01:16
I find this function vey useful when you have a big array and you want to know if a given value is in the array. in_array in fact becomes quite slow in such a case, but you can flip the big array and then use isset to obtain the same result in a much faster way.
h3x 12-Sep-2010 10:11
this function can be used to remove null elements form an array:

<?php
$ar = array(null,'1','2',null,'3',null);
print_r($ar);
/*
result:
Array
(
    [0] =>
    [1] => 1
    [2] => 2
    [3] =>
    [4] => 3
    [5] =>
)
*/

print_r(array_flip(array_flip($ar)));
/*
result:
Array
(
    [1] => 1
    [2] => 2
    [4] => 3
)
*/
?>
Hayley Watson 20-Mar-2009 11:22
Finding the longest string in an array?

<?php
function longest_string_in_array($array)
{
    $mapping = array_combine($array, array_map('strlen', $array));
    return array_keys($mapping, max($mapping));
}
?>

Differences are obvious: returns an array of [i]all[/i] of the longest strings, instead of just picking one arbitrarily. Doesn't do the stripslashing or magic stuff because that's another job for for another function.
dan at aoindustries dot com 07-Mar-2009 09:48
From an algorithmic efficiency standpoint, building an entire array of lengths to then sort to only retrieve the longest value is unnecessary work.  The following should be O(n) instead of O(n log n).  It could also be:

<?php
function get_longest_value($array) {
    // Some don't like to initialize, I do
    $longest = NULL;
    $longestLen = -1;
    foreach ($array $value) {
        $len = strlen($value);
        if($len>$longestLen) {
            $longest = $value;
            $longestLen = $len;
        }
    }
    $longest = str_replace("\r\n", "\n", $longest);
    if (get_magic_quotes_gpc()) { return stripslashes($longest); }
    return $longest;
}
?>
corz at corz dot org 08-Dec-2008 10:36
<?php
/*
    Fun function to return the longest physical *value* from an array.

    Culled from a small script designed to capture the longest $_POST variable,
    usually the textarea, which would then be dumped to a "emergency post dump file".

    corz at corz dot org
*/

$array = array("input" => "submit", "textarea" => "Some long spiel of text\r\na textarea, probably",
                        "another-input" => "make me longer", "and" => "another", "etc" => "etc.");

echo '<!DOCTYPE HTML SYSTEM><html><head><title>long</title></head><body><pre>Longest value: ',
                                            get_longest_value($array),'</pre></body></html>';

function get_longest_value($array) {
    foreach ($array as $key => $value) {
        $lengths[$key] = strlen($value);
    }
    asort($lengths);
    $lengths = array_flip($lengths);
    $longest = str_replace("\r\n", "\n", $array[array_pop($lengths)]);
    if (get_magic_quotes_gpc()) { return stripslashes($longest); }
    return $longest;
}
?>
pinkgothic at gmail dot com 26-Apr-2007 10:37
In case anyone is wondering how array_flip() treats empty arrays:

<?php
print_r(array_flip(array()));
?>

results in:

Array
(
)

I wanted to know if it would return false and/or even chuck out an error if there were no key-value pairs to flip, despite being non-intuitive if that were the case. But (of course) everything works as expected. Just a head's up for the paranoid.
snaury at narod dot ru 23-Nov-2004 08:21
When you do array_flip, it takes the last key accurence for each value, but be aware that keys order in flipped array will be in the order, values were first seen in original array. For example, array:

    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 3
    [5] => 2
    [6] => 1
    [7] => 1
    [8] => 3
    [9] => 3

After flipping will become:
(first seen value -> first key)

    [1] => 7
    [2] => 5
    [3] => 9

And not anything like this:
(last seen value -> last key)

    [2] => 5
    [1] => 7
    [3] => 9

In my application I needed to find five most recently commented entries. I had a sorted comment-id => entry-id array, and what popped in my mind is just do array_flip($array), and I thought I now would have last five entries in the array as most recently commented entry => comment pairs. In fact it wasn't (see above, as it is the order of values used). To achieve what I need I came up with the following (in case someone will need to do something like that):

First, we need a way to flip an array, taking the first encountered key for each of values in array. You can do it with:

  $array = array_flip(array_unique($array));

Well, and to achieve that "last comments" effect, just do:

  $array = array_reverse($array, true);
  $array = array_flip(array_unique($array));
  $array = array_reverse($array, true);

In the example from the very beginning array will become:

    [2] => 5
    [1] => 7
    [3] => 9

Just what I (and maybe you?) need. =^_^=
znailz at yahoo dot com 05-Aug-2003 11:42
I know a lot of people want a function to remove a key by value from an array. I saw solutions that iterate(!) though the whole array comparing value by value and then unsetting that value's key. PHP has a built-in function for pretty much everything (heard it will even cook you breakfast), so if you think "wouldn't it be cool if PHP had a function to do that...", odds are it already has. Check out this example. It takes a value, gets all keys for that value if it has duplicates, unsets them all, and returns a reindexed array.

<?php
$arr = array(11,12,13,12);        // sample array
$arr = array_flip($arr);
unset($arr[12]);
$arr = array(array_keys($arr));
?>

$arr contains:

Array
(
    [0] => Array
        (
            [0] => 11
            [1] => 13
        )
?>

)


인덱스 검사과 함께 배열의 교집합을 계산

  • ≪ array_flip
  • array_intersect_key ≫
  • PHP Manual
  • 배열 함수 목록
  • 인덱스 검사과 함께 배열의 교집합을 계산

array_intersect_assoc

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

array_intersect_assoc — 인덱스 검사과 함께 배열의 교집합을 계산

설명

array array_intersect_assoc ( array $array1 , array $array2 [, array $ ... ] )

array_intersect_assoc()는 모든 인수에 존재하는 array1의 모든 값을 포함하는 배열을 반환한다. 이 함수는 array_intersect()과 달리 비교시에 키가 사용된다는것에 주의한다.

인수

array1

확인할 기준 값을 가진 배열.

array2

비교할 값의 배열.

array

비교할 배열의 가변 목록.

반환값

모든 인수에 존재하는 array1의 모든 값을 가지는 연관 배열을 반환합니다.

예제

Example #1 array_intersect_assoc() 예제코드

<?php
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result_array = array_intersect_assoc($array1, $array2);
print_r($result_array);
?>

위 예제의 출력:

Array
(
    [a] => green
)

위 예제에서 "a" => "green"쌍만 두 배열 안에서 존재하고 그 값들만 반환한다는것을 확인할수 있다. "red" 값은 $array1안에서 그 키가 0이고 $array2안에서는 1이기 때문에 반환하지 않는다.

key => value쌍의 두 값이 동치라고 볼수 있는 때는 (string) $elem1 === (string) $elem2일 때 뿐이다. 달리 말하면 엄격한 타입 검사는 문자열 표현이 동일해야 한다는것이다.

참고

  • array_intersect() - 배열의 교집합을 계산
  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
  • array_diff() - 배열 차이를 계산
  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산

User Contributed Notes

Whitend 04-Aug-2012 05:08
One of the ways to get intersection of two arrays is as follows:

<?php
function arrayIntersect( $primary_array, $secondary_array ) {

    if ( !is_array( $primary_array ) || !is_array( $secondary_array ) ) {
        return false;
    }

    if ( !empty( $primary_array ) ) {

        foreach( $primary_array as $key => $value ) {

            if ( !isset( $secondary_array[$key] ) ) {
                unset( $primary_array[$key] );
            } else {
                if ( serialize( $secondary_array[$key] ) != serialize( $value ) ) {
                    unset( $primary_array[$key] );
                }
            }

        }

        return $primary_array;

    } else {
        return array();
    }

}
?>

It would pay attention to both keys and values even if values would be arrays as well. One important note is that if value of $primary_array is yet another array, its order of key & value pairs becomes important for matching.
StR: str at strgt dot cjb dot net 14-Oct-2005 02:34
To find the keys that are in 2 arrays, without caring of the values:

This is a function i needed, and it also returns the values of the first array

<?php
function array_key_intersect(&$a, &$b) {
  $array = array();
  while (list($key,$value) = each($a)) {
    if (isset($b[$key]))
      $array[$key] = $value;
  }
  return $array;
}
?>
vandor at progressive dot hu 14-Dec-2004 08:26
# ket tomb kulcs alapu metszetet szamitja ki, az erteken is ellenorzest vegez
#
# calculating (key base) intersection of arrays (value are checked)
# use: $result_array = array_intersect_assoc($a1, $a2, $a3,..);
#
##
# original code by [jochem at iamjochem dawt com] : array_diff_keys()

<?php
function array_intersect_assoc() {

    $args = func_get_args();
    $res = $args[0];

    for ($i=1;$i<count($args);$i++) {
        if (!is_array($args[$i])) {continue;}

        foreach ($res as $key => $data) {
            if ( (!array_key_exists($key, $args[$i])) || ( (isset($args[$i][$key])) && ($args[$i][$key] !== $res[$key]) ) ) {
                unset($res[$key]);
            }
        }
    }

    return $res;
}

?>

<?php
$array1 = array("a"=>"green", "b"=>"brown", "c"=>"blue", "red");
$array2 = array("a"=>"green", "b"=>"yellow", "c"=>"yellow", "red");
$a = array('a'=>'green', 'b'=>'brown', 'c'=>'yellow');
$b = array('a'=>'green', 'b'=>'brown', 'c'=>'yellow', 'e'=>'yellow');

$result_array = array_intersect_assoc($a, $b, $array1, $array2);
?>

$args array will look like:

(
    0 =>
        (
            a => green
            b => brown
            c => yellow
        )

    1 =>
        (
            a => green
            b => brown
            c => yellow
            e => yellow
        )

    2 =>
        (
            a => green
            b => brown
            c => blue
            0 => red
        )

    3 =>
        (
            a => green
            b => yellow
            c => yellow
            0 => red
        )

)

$result_array will look like:

(
    a => green
)


Computes the intersection of arrays using keys for comparison

  • ≪ array_intersect_assoc
  • array_intersect_uassoc ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the intersection of arrays using keys for comparison

array_intersect_key

(PHP 5 >= 5.1.0, PHP 7)

array_intersect_key — Computes the intersection of arrays using keys for comparison

설명

array array_intersect_key ( array $array1 , array $array2 [, array $... ] )

array_intersect_key() returns an array containing all the entries of array1 which have keys that are present in all the arguments.

인수

array1

The array with master keys to check.

array2

An array to compare keys against.

...

A variable list of arrays to compare.

반환값

Returns an associative array containing all the entries of array1 which have keys that are present in all arguments.

예제

Example #1 array_intersect_key() example

<?php
$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

var_dump(array_intersect_key($array1, $array2));
?>

위 예제의 출력:

array(2) {
  ["blue"]=>
  int(1)
  ["green"]=>
  int(3)
}

In our example you see that only the keys 'blue' and 'green' are present in both arrays and thus returned. Also notice that the values for the keys 'blue' and 'green' differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of array1.

The two keys from the key => value pairs are considered equal only if (string) $key1 === (string) $key2 . In other words a strict type check is executed so the string representation must be the same.

참고

  • array_diff() - 배열 차이를 계산
  • array_udiff() - 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산
  • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_diff_key() - Computes the difference of arrays using keys for comparison
  • array_diff_ukey() - Computes the difference of arrays using a callback function on the keys for comparison
  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_ukey() - Computes the intersection of arrays using a callback function on the keys for comparison

User Contributed Notes

Reed Silver 03-Mar-2015 08:38
If you want an array that has no key value pairs added from the second array:

$new = array_intersect_key($b, $a) + $a;
github.com/xmarcos 12-May-2014 02:47
[Editor's note: changed array_merge_recursive() to array_replace_recursive() to fix the script]

Here is a better way to merge settings using some defaults as a whitelist.

<?php

$defaults = [
    'id'            => 123456,
    'client_id'     => null,
    'client_secret' => null,
    'options'       => [
        'trusted' => false,
        'active'  => false
    ]
];

$options = [
    'client_id'       => 789,
    'client_secret'   => '5ebe2294ecd0e0f08eab7690d2a6ee69',
    'client_password' => '5f4dcc3b5aa765d61d8327deb882cf99', // ignored
    'client_name'     => 'IGNORED',                          // ignored
    'options'         => [
        'active' => true
    ]
];

var_dump(
    array_replace_recursive($defaults,
        array_intersect_key(
            $options, $defaults
        )
    )
);

?>

Output:

array (size=4)
    'id'            => int 123456
    'client_id'     => int 789
    'client_secret' => string '5ebe2294ecd0e0f08eab7690d2a6ee69' (length=32)
    'options'       =>
        array (size=2)
            'trusted' => boolean false
            'active'  => boolean true
pixelf3hler at visualize-me dot de 28-Sep-2013 05:08
in case you came here looking for a function that returns an array containing the values of `all` arrays with intersecting keys:
<?php
   function array_merge_on_key($key, $array1, $array2) {
      $arrays = array_slice(func_get_args(), 1);
      $r = array();
      foreach($arrays as &$a) {
         if(array_key_exists($key, $a)) {
            $r[] = $a[$key];
            continue;
         }
      }
      return $r;
   }
   // example:
   $array1 = array("id" => 12, "name" => "Karl");
   $array2 = array("id" => 4, "name" => "Franz");
   $array3 = array("id" => 9, "name" => "Helmut");
   $array4 = array("id" => 10, "name" => "Kurt");

   $result = array_merge_on_key("id", $array1, $array2, $array3, $array4);

   echo implode(",", $result); // => 12,4,9,10
?>
vladas dot dirzys at gmail dot com 13-Aug-2012 05:03
Simple key white-list filter:

<?php
$arr = array('a' => 123, 'b' => 213, 'c' => 321);
$allowed = array('b', 'c');

print_r(array_intersect_key($arr, array_flip($allowed)));
?>

Will return:
Array
(
    [b] => 213
    [c] => 321
)
pgl at yoyo dot org 18-Jul-2011 05:01
Note that the order of the keys in the returned array is the same as the order of the keys in the source array. eg:

<?php
$array = array(
    'two'   => 'a',
    'three' => 'b',
    'one'   => 'c',
    );

$keyswant = array(
    'one'       => '',
    'three'     => '',
    );

print_r(array_intersect_key($array, $keyswant));

?>

Shows:

Array
(
    [three] => b
    [one] => c
)
chrisbloom7 at gmail dot com 11-Nov-2009 07:23
Regarding php at keithtylerdotcom solution to emulate

<?php
$z = someFuncReturningAnArray()['some_key'];
?>

His recommended solution will still return an array. To get the value of a single key in an array returned by a function, simply add implode() to the recipe:

<?php
function someFuncReturningAnArray() {
  return array(
    'a' => 'b',
    'c' => 'd',
    'e' => 'f',
    'g' => 'h',
    'i' => 'j'
  );
}

//traditional way
$temp = someFuncReturningAnArray();
$b = $temp['a'];
echo print_r($b, 1) . "\n----------\n";

//keithtylerdotcom one-line method
$b = array_intersect_key(someFuncReturningAnArray(), array('a'=>''));
echo print_r($b, 1) . "\n----------\n";

//better one line method
$b = implode('', array_intersect_key(someFuncReturningAnArray(), array('a'=>'')));
echo print_r($b, 1) . "\n----------\n";
?>
markus dot kappe at dix dot at 24-Sep-2009 01:43
<?php
    /**
     * calculates intersection of two arrays like array_intersect_key but recursive
     *
     * @param  array/mixed  master array
     * @param  array        array that has the keys which should be kept in the master array
     * @return array/mixed  cleand master array
     */
    function myIntersect($master, $mask) {
        if (!is_array($master)) { return $master; }
        foreach ($master as $k=>$v) {
            if (!isset($mask[$k])) { unset ($master[$k]); continue; } // remove value from $master if the key is not present in $mask
            if (is_array($mask[$k])) { $master[$k] = $this->myIntersect($master[$k], $mask[$k]); } // recurse when mask is an array
            // else simply keep value
        }
        return $master;
    }
?>
pdemaziere at gmail dot com 23-Feb-2009 04:52
Just a simple script if you want to use one array, which contains only zeros and ones, as mask for another one (both arrays must have the same size of course). $outcome is an array that contains only those values from $source where $mask is equal to 1.

<?php
$outcome = array_values(array_intersect_key( array_values($source), array_filter(array_values($mask)) ));
?>

PS: the array_values() function is necessary to ensure that both arrays have the same numbering/keys, otherwise your masking does not behave as you expect.

Enjoy!
CBWhiz at gmail dot com 04-Jan-2008 11:04
I have found the following helpful:
<?PHP
function array_merge_default($default, $data) {
        $intersect = array_intersect_key($data, $default); //Get data for which a default exists
        $diff = array_diff_key($default, $data); //Get defaults which are not present in data
        return $diff + $intersect; //Arrays have different keys, return the union of the two
}
?>
It's use is like both of the functions it uses, but keeps defaults and _only_ defaults. It's designed for key arrays, and i'm not sure how it will work on numeric indexed arrays.

Example:
<?PHP
$default = array(
 "one" => 1,
 "two" => 2
);
$untrusted = array(
 "one" => 42,
 "three" => 3
);
var_dump(array_merge_default($default, $untrusted));

array(2) {
  ["two"]=>
  int(2)
  ["one"]=>
  int(42)
}

?>
Rod Byrnes 06-May-2007 06:10
Here is a faster version than those shown below, with optimisation for the case when only two arrays are passed. In my tests with a 10000 item first array and a 5000 item second array (run 20 times) this function ran in 1.89 seconds compared with 2.66 for the version posted by dak. For a three array case, same as above but with the third array containing 3333 values, the timing is 3.25 for this version compared with 3.7 for dak's version.

<?php
if (!function_exists('array_intersect_key'))
{
  function array_intersect_key($isec, $keys)
  {
    $argc = func_num_args();
    if ($argc > 2)
    {
      for ($i = 1; !empty($isec) && $i < $argc; $i++)
      {
        $arr = func_get_arg($i);
        foreach (array_keys($isec) as $key)
        {
          if (!isset($arr[$key]))
          {
            unset($isec[$key]);
          }
        }
      }
      return $isec;
    }
    else
    {
      $res = array();
      foreach (array_keys($isec) as $key)
      {
        if (isset($keys[$key]))
        {
          $res[$key] = $isec[$key];
        }
      }
      return $res;
    }
  }
}
?>
17-Jul-2006 02:31
Here it is a more obvious way to implement the function:

if (!function_exists('array_intersect_key')) {
    function array_intersect_key()
    {
        $arrs = func_get_args();
        $result = array_shift($arrs);
        foreach ($arrs as $array) {
            foreach ($result as $key => $v) {
                if (!array_key_exists($key, $array)) {
                    unset($result[$key]);
                }
            }
        }
        return $result;
   }
}
Anton Backer 31-Mar-2006 08:49
Jesse: no, array_intersect_key does not accomplish the same thing as what you posted:

array_flip (array_intersect (array_flip ($a), array_flip ($b)))

because when the array is flipped, values become keys. having duplicate values is not a problem, but having duplicate keys is. array_flip resolves it by keeping only one of the duplicates and discarding the rest. by the time you start intersecting, you've already lost information.
aidan at php dot net 29-May-2005 05:51
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat


Computes the intersection of arrays with additional index check, compares indexes by a callback function

  • ≪ array_intersect_key
  • array_intersect_ukey ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the intersection of arrays with additional index check, compares indexes by a callback function

array_intersect_uassoc

(PHP 5, PHP 7)

array_intersect_uassoc — Computes the intersection of arrays with additional index check, compares indexes by a callback function

설명

array array_intersect_uassoc ( array $array1 , array $array2 [, array $... ], callable $key_compare_func )

array_intersect_uassoc() returns an array containing all the values of array1 that are present in all the arguments. Note that the keys are used in the comparison unlike in array_intersect().

인수

array1

Initial array for comparison of the arrays.

array2

First array to compare keys against.

...

Variable list of array arguments to compare values against.

key_compare_func

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )

반환값

Returns the values of array1 whose values exist in all of the arguments.

예제

Example #1 array_intersect_uassoc() example

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");

print_r(array_intersect_uassoc($array1, $array2, "strcasecmp"));
?>

위 예제의 출력:

Array
(
    [b] => brown
)

참고

  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
  • array_intersect_key() - Computes the intersection of arrays using keys for comparison
  • array_intersect_ukey() - Computes the intersection of arrays using a callback function on the keys for comparison

User Contributed Notes

swapniltiwhane at gmail dot com 16-Jul-2013 09:04
$a = array(
        array(
                'id' => 15,
                'name' => 'John',
                'color' => 'red',
        ),
        array(
                'id' => 22,
                'name' => 'Elise',
                'color' => 'blue',
        ),
        array(
                'id' => 30,
                'name' => 'Mark',
                'color' => 'red',
        ),
);

$b = array(
        array(
                'id' => 15,
                'name' => 'Nancy',
                'color' => 'black',
        ),
        array(
                'id' => 20,
                'name' => 'Elise',
                'color' => 'blue',
        ),
        array(
                'id' => 30,
                'name' => 'Mark',
                'color' => 'red',
        ),
        array(
                'id' => 40,
                'name' => 'John',
                'color' => 'orange',
        ),
);

function compare_ids($a, $b)
{
    return ((bool)($a['id'] - $b['id']));
}

print_r(array_intersect_uassoc($a, $b, "compare_ids"));


Computes the intersection of arrays using a callback function on the keys for comparison

  • ≪ array_intersect_uassoc
  • array_intersect ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the intersection of arrays using a callback function on the keys for comparison

array_intersect_ukey

(PHP 5 >= 5.1.0, PHP 7)

array_intersect_ukey — Computes the intersection of arrays using a callback function on the keys for comparison

설명

array array_intersect_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func )

array_intersect_ukey() returns an array containing all the values of array1 which have matching keys that are present in all the arguments.

인수

array1

Initial array for comparison of the arrays.

array2

First array to compare keys against.

...

Variable list of array arguments to compare keys against.

key_compare_func

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )

반환값

Returns the values of array1 whose keys exist in all the arguments.

예제

Example #1 array_intersect_ukey() example

<?php
function key_compare_func($key1, $key2)
{
    if ($key1 == $key2)
        return 0;
    else if ($key1 > $key2)
        return 1;
    else
        return -1;
}

$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));
?>

위 예제의 출력:

array(2) {
  ["blue"]=>
  int(1)
  ["green"]=>
  int(3)
}

In our example you see that only the keys 'blue' and 'green' are present in both arrays and thus returned. Also notice that the values for the keys 'blue' and 'green' differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of array1.

참고

  • array_diff() - 배열 차이를 계산
  • array_udiff() - 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산
  • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_diff_key() - Computes the difference of arrays using keys for comparison
  • array_diff_ukey() - Computes the difference of arrays using a callback function on the keys for comparison
  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_key() - Computes the intersection of arrays using keys for comparison

User Contributed Notes

gk at anuary dot com 01-Oct-2013 01:52
"array_intersect_ukey" will not work if $key_compare_func is using regular expression to perform comparison. "_array_intersect_ukey" fully implements the "array_intersect_ukey" interface and handles properly boolean comparison. However, the native implementation should be preferred for efficiency reasons.

<?php
$key_compare_func = function ($a, $b) {
    var_dump('a=' . $a. '; b=' . $b);

    if (strpos($b, '/') === 0) {
        return preg_match($b, $a) > 0 ? 0 : -1;
    }

    if ($a == $b) {
        $weight = 0;
    } else if ($a > $b) {
        $weight = 1;
    } else {
        $weight = -1;
    }

    var_dump('literal comparison: ' . $weight);

    return $weight;
};

$foo = ['aab' => '', 'bbb' => '', 'ccb' => '', 'abb' => ''];
$bar = ['/[a-z]b[a-z]/' => ''];
$buz = ['/c[a-z][a-z]/' => ''];

echo PHP_EOL . 'array_intersect_ukey' . PHP_EOL . PHP_EOL;

$subject = array_intersect_ukey ($foo, $bar, $buz, $key_compare_func);

echo PHP_EOL;

var_dump($subject);

echo PHP_EOL . '_array_intersect_ukey' . PHP_EOL . PHP_EOL;

$subject = _array_intersect_ukey($foo, $bar, $buz, $key_compare_func);

echo PHP_EOL;

var_dump($subject);

/**
 * @author Gajus Kuizinas <gk@anuary.com>
 * @version 1.0.0 (2013 09 30)
 * @url https://gist.github.com/gajus/271ad5f36337a32a184c
 */
function _array_intersect_ukey (array $arr1, array $arr2, $key_compare_func) {
    $arr_matched = [];
    $arr_unmatched = [];

    $args = func_get_args();

    $key_compare_func = end($args);

    foreach ($arr1 as $k1 => $v1) {
        foreach ($arr2 as $k2 => $v2) {
            $diff = $key_compare_func($k1, $k2);

            //var_dump('k1=' . $k1 . ', k2=' . $k2 . ', diff=' . $diff);

            if ($diff === 0) {
                $arr_matched[$k1] = $v1;
            } else {
                $arr_unmatched[$k1] = $v1;
            }
        }
    }

    if (count($args) <= 3) {
        return $arr_matched;
    }

    array_splice($args, 0, 2, [$arr_unmatched]);

    return array_merge($arr_matched, call_user_func_array('_array_intersect_ukey', $args));
}

/*
array_intersect_ukey

string(12) "a=bbb; b=aab"
string(21) "literal comparison: 1"
string(12) "a=bbb; b=ccb"
string(22) "literal comparison: -1"
string(12) "a=abb; b=bbb"
string(22) "literal comparison: -1"
string(12) "a=aab; b=abb"
string(22) "literal comparison: -1"
string(22) "a=aab; b=/[a-z]b[a-z]/"
string(22) "a=abb; b=/[a-z]b[a-z]/"
string(22) "a=abb; b=/c[a-z][a-z]/"

array(0) {
}

_array_intersect_ukey

string(22) "a=aab; b=/[a-z]b[a-z]/"
string(22) "a=bbb; b=/[a-z]b[a-z]/"
string(22) "a=ccb; b=/[a-z]b[a-z]/"
string(22) "a=abb; b=/[a-z]b[a-z]/"
string(22) "a=aab; b=/c[a-z][a-z]/"
string(22) "a=ccb; b=/c[a-z][a-z]/"

array(3) {
  ["bbb"]=>
  string(0) ""
  ["abb"]=>
  string(0) ""
  ["ccb"]=>
  string(0) ""
}*/
?>


배열의 교집합을 계산

  • ≪ array_intersect_ukey
  • array_key_exists ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 교집합을 계산

array_intersect

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

array_intersect — 배열의 교집합을 계산

설명

array array_intersect ( array $array1 , array $array2 [, array $ ... ] )

array_intersect()는 모든 인수에 존재하는 array1의 모든 값을 포함하는 배열을 반환한다. 키는 보존된다는 것에 주의할것.

인수

array1

확인할 주 값을 가진 배열.

array2

값을 비교할 배열.

array

비교할 배열의 가변 목록.

반환값

모든 인수에 값이 존재하는 array1의 값을 가지는 배열을 반환합니다.

예제

Example #1 array_intersect() 예제

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>

위 예제의 출력:

Array
(
    [a] => green
    [0] => red
)

주의

Note: 두 구성요소가 동치라고 볼수 있는 때는 (string) $elem1 === (string) $elem2 일때 만이다. In words: when the string representation is the same.

참고

  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_diff() - 배열 차이를 계산
  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산

User Contributed Notes

turabgarip at gmail dot com 30-Oct-2016 12:02
The reason that this function is considerably slow; is because it's comparing not only the values cross wise; but it also compares the array's own elements too.

For example you have two arrays like;

<?php
$arr1 = array('green', 'blue', 'black');
$arr2 = array('yellow', 'red', 'cyan');

array_intersect($arr1, $arr2);
?>

Intersection will start to compare from the first three colors like; "blue" to "green", "black" to "blue"; that are, the elements of the first array only, and yes, comparison order is arbitrary, but always in the same order.

It will do the same for the second array too.
juniorand at gmail dot com 07-Dec-2015 10:19
If you just need confirmation that there intersection (and will not use the return of the intersection). Just only returns a boolean.

<?php
    function eventsCodeIntersect()
    {
        $eventsCod = array(211, 220, 244);
        $eventsIntersect = array(220, 244);

        return (bool) array_intersect($array1, $array2);
    }
?>

In this case, the return is true.
deekay 02-Dec-2015 05:25
I ran into a problem (PHP 5.6) with array_intersect(), though i had no idea it was it at the time : using it properly created an array to string conversion notice, through this type of code (and with both array variables verified) :

<?php
if(empty($var = array_intersect($array1, $array2))) {
?>

Rewriting its supposed behaviour, even in a rather simple way, removed the notice. Not sure what caused the function to be unhappy but here's my code that works properly :

<?php
$var = [];
foreach($array1 as $v) {
   if(in_array($v, $var)) continue; // value already found
   if(in_array($v, $array2)) $var[] = $v;
}
if(empty($var)) {
?>
matang dot dave at gmail dot com 17-Jun-2015 11:34
Take care of value types while using array_intersect function as there is no option for strict type check as in in_array function.

$array1 = array(true,2);
$array2 = array(1, 2, 3, 4, 5, 6);

var_dump(array_intersect($array1, $array2));

result is :
array(2) {
        [0] =>  bool(true)
        [1]  =>  int(2)
}
info at iridsystem dot it 22-May-2015 03:16
This function is able to sort an array based on another array that contains the order of occurrence. The values that are not present will be transferred into the end of the resultant.
Questa funzione permette di ordinare i valori di un array ($tosort) basandosi sui valori contenuti in un secondo array ($base), i valori non trovati verranno posizionati alla fine dell'ordinamento senza un'ordine specifico.

<?
$base= array('one', 'two', 'three');

$tosort= array('a'=>'two', 'b'=>'three', 'c'=>'five', 'd'=>'one', 'and'=>'four', 'f'=>'one');

uasort($tosort, function($key1, $key2) use ($base) {
  $a1=array_search($key1, $base);
  $a2=array_search($key2, $base);

  if ( $a1===false && $a2===false ) { return 0; }
  else if ( $a1===false && $a2 !== false) { return 1; }
  else if ( $a1!==false && $a2 === false) {return -1;}

  if( $a1 > $a2 ) { return 1; }
  else if ( $a1 < $a2 ) { return -1; }
  else if ( $a1 == $a2 ) { return 0; }
});
var_dump($tosort);
/*
the resulting of $tosort
array
  'd' => string 'one' (length=3)
  'f' => string 'one' (length=3)
  'a' => string 'two' (length=3)
  'b' => string 'three' (length=3)
  'c' => string 'five' (length=6)
  'e' => string 'four' (length=7)
*/
Gabriel
?>
yuval at visualdomains dot com 17-May-2015 10:17
Using isset to achieve this, is many times faster:

<?php

 $m = range(1,1000000);
  $s = [2,4,6,8,10];

// Use array_intersect to return all $m values that are also in $s
  $tstart = microtime(true);
  print_r (array_intersect($m,$s));
  $tend = microtime(true);
  $time = $tend - $tstart;
  echo "Took $time";

 // Use array_flip and isset to return all $m values that are also in $s
  $tstart = microtime(true);
  $f = array_flip($s);
/* $f now looks like this:
(
    [2] => 0
    [4] => 1
    [6] => 2
    [8] => 3
    [10] => 4
)
*/
// $u will hold the intersected values
  $u = [];
  foreach ($m as $v) {
    if (isset($f[$v])) $u[] = $v;
  }
  print_r ($u);
   $tend = microtime(true);
  $time = $tend - $tstart;
  echo "Took $time";
?>

Results:

Array
(
    [1] => 2
    [3] => 4
    [5] => 6
    [7] => 8
    [9] => 10
)
Took 4.7170009613037
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
    [4] => 10
)
Took 0.056024074554443

array_intersect: 4.717
array_flip+isset: 0.056
tim at zeroedin dot com 28-Jan-2014 02:30
Note... this function does not seem intuitive for doing intersection of flat arrays, in the sense that an intersection are common values between. This is an issue if you are doing a for loop over the results of an intersect function, as shown below, wherein the for loop iterates over something different depending on order.

Below is example of a function whioch I think works correctly,  the output from original function, and new function.

function array_value_mutual_intersection($array1, $array2)
{
    $hashMap = array();
    $output = array();
    foreach($array1 as $item)
        $hashMap[$item] = '';
    foreach($array2 as $item)
        if(isset($hashMap[$item]))
            array_push($output, $item);
    return $output;
}

$a = ['one', 'two', 'three', 'four'];
$b = ['three', 'two'];

echo "Original array a = " . json_encode($a) . "\n";
echo "Original array b = " . json_encode($b) . "\n";

echo "PHP array_intersect says interesection of (a,b) is: " . json_encode(array_intersect($a,$b)) . "\n";
echo "PHP array_intersect says interesection of (b,a) is: " . json_encode(array_intersect($b,$a)) . "\n\n";

echo "My intersect function says intersection of (a,b) is: " . json_encode(array_value_mutual_intersection($a, $b)) . "\n";
echo "My intersect function says intersection of (b,a) is: " . json_encode(array_value_mutual_intersection($b, $a)) . "\n\n";

-------output------

$ php test.php
Original array a = ["one","two","three","four"]
Original array b = ["three","two"]
PHP array_intersect says interesection of (a,b) is: {"1":"two","2":"three"}
PHP array_intersect says interesection of (b,a) is: ["three","two"]

My intersect function says intersection of (a,b) is: ["three","two"]
My intersect function says intersection of (b,a) is: ["two","three"]
juliotorres1604 at hotmail dot com 21-Nov-2013 11:34
<?php
// Function to intersect n arrays

function getIntersect($arrays){
        $totalArrays = count($arrays);
        if($totalArrays >= 2){
                $arrayTmp =  $arrays[0];
                for ($i = 1; $i < $totalArrays; $i++) {
                    $arrayTmp = array_intersect($arrayTmp, $arrays[$i]);
                }
                return $arrayTmp;
        }else{
            return FALSE;
        }
    }

// Example:
$arrayTmp[0] = array('0'=>0, '1'=>7, '2'=>5);
$arrayTmp[1] = array('0'=>7, '1'=>5, '2'=>0, '3'=>15);
$arrayTmp[2] = array('0'=>3, '1'=>0, '2'=>7, '3'=>4);

$intersectArray = getIntersect($arrayTmp);
print_r($arrayFinal);
?>

The above example will output:

Array
(
    [0] => 0
    [1] => 7
)
meihao at 126 dot com 21-Nov-2013 05:56
<?php
$a=array(1,2,'3',4);
$b=array('1',2,3);

var_dump($a,$b);

result:
array (size=3)
  0 => int 1
  1 => int 2
  2 => string '3' (length=1)

?>
Mike Block 05-Mar-2013 06:27
I bench-marked some uses of array_intersect and can't believe how slow it is. This isn't as elaborate, but handles most cases and is much faster:

<?php
/**
examines two arrays and returns the intersected arrays with matching keys (ignores duplicate keys)
*/
function simple_array_intersect($a,$b) {
    $a_assoc = $a != array_values($a);
    $b_assoc = $b != array_values($b);
    $ak = $a_assoc ? array_keys($a) : $a;
    $bk = $b_assoc ? array_keys($b) : $b;
    $out = array();
    for ($i=0;$i<sizeof($ak);$i++) {
        if (in_array($ak[$i],$bk)) {
            if ($a_assoc) {
                $out[$ak[$i]] = $a[$ak[$i]];
            } else {
                $out[] = $ak[$i];
            }
        }
    }
    return $out;
}

?>

You can try this out with this:

<?php
// create a large array (simple)
$first = array();
for ($i=500;$i<500000;$i++) {
    $first[] = $i;
}
// create a smaller array (associative)
$second = array();
for ($i=499990;$i<500000;$i++) {
    $second[$i] = rand();
}
echo microtime(true)."\n";
// built-in function
print_r(array_intersect($first,$second));
echo microtime(true)."\n";
// favour simple array as match
print_r(simple_array_intersect($first,$second));
echo microtime(true)."\n";
// favour associative keys for match
print_r(simple_array_intersect($second,$first));
echo microtime(true)."\n";

?>
caffinated 10-Nov-2012 09:13
If you're looking for a relatively easy way to strictly intersect keys and values recursively without array key reordering, here's a simple recursive function:

<?php
function array_intersect_recursive($array1, $array2)
{
  foreach($array1 as $key => $value)
  {
    if (!isset($array2[$key]))
    {
      unset($array1[$key]);
    }
    else
    {
      if (is_array($array1[$key]))
      {
        $array1[$key] = array_intersect_recursive($array1[$key], $array2[$key]);
      }
      elseif ($array2[$key] !== $value)
      {
        unset($array1[$key]);
      }
    }
  }
  return $array1;
}
?>
dml at nm dot ru 20-May-2011 02:06
The built-in function returns wrong result when input arrays have duplicate values.
Here is a code that works correctly:

<?php
function array_intersect_fixed($array1, $array2) {
    $result = array();
    foreach ($array1 as $val) {
      if (($key = array_search($val, $array2, TRUE))!==false) {
         $result[] = $val;
         unset($array2[$key]);
      }
    }
    return $result;
}
?>
faharanik 30-Jun-2010 04:30
Here's my approach to intersection returning only the values present in all the arrays.
Note that each array must not contain duplicate values.
I don't know how effective it actually is, but perhaps it could help.

<?php
// $arrays - Array of arrays to intersect.

function calculate_intersection($arrays)
    {
    $intersection = Array();

    for($checked_item = 0; $checked_item < count($arrays[0]); $checked_item++)
        {
        $occurrence = 1;

        for($compared_array = 1; $compared_array < count($arrays); $compared_array++)
            {
            for($compared_item = 0; $compared_item < count($arrays[$compared_array]); $compared_item++)
                {
                if($arrays[0][$checked_item] == $arrays[$compared_array][$compared_item])
                    {
                    $occurrence++;

                    if($occurrence == count($arrays))
                        {
                        $intersection[] = $arrays[0][$checked_item];
                        }
                    }
                }
            }
        }

    return $intersection;
    }
?>
Yohann 12-Apr-2010 01:36
I used array_intersect in order to sort an array arbitrarly:

<?php
$a = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'height', 'nine', 'ten');
$b = array('four', 'one', 'height', 'five')
var_dump(array_intersect($a, $b);
?>

will output:

0 => 'one'
1 => 'four'
2 => 'five'
3 => 'height'

i hope this can help...
james -at- bandit.co -dot- nz 08-Apr-2010 03:07
I needed an array_intersect that would delete the intersecting values from the original array. Voila:

<?php
// array_intersect that splits the needle array into two - one filled with "intersected" results, and one filled with the remainder
function array_intersect_split(&$needle, $haystack, $preserve_keys = false) {
    if(!is_array($needle) || !is_array($haystack)) return false;
    $new_arr = array();
    foreach($needle as $key => $value) {
        if(($loc = array_search($value, $haystack))!==false) {
            if(!$preserve_keys) $new_arr[] = $value;
            else $new_arr[$key] = $value;
            unset($needle[$key]);
        }
    }
    return $new_arr;
}
?>
189780 at gmail dot com 02-Jan-2010 04:26
Actually array_intersect finds the dublicate values, here is my approach which is 5 times faster than built-in function array_intersect().. Give a try..

<?php
function my_array_intersect($a,$b)
{
        for($i=0;$i<sizeof($a);$i++)
        {
                $m[]=$a[$i];
        }
        for($i=0;$i<sizeof($a);$i++)
        {
                $m[]=$b[$i];
        }
        sort($m);
        $get=array();
        for($i=0;$i<sizeof($m);$i++)
        {
                if($m[$i]==$m[$i+1])
                $get[]=$m[$i];
        }
        return $get;
}
?>

Barı? CUHADAR
189780@gmail.com
Shawn Pyle 13-Aug-2009 06:29
array_intersect handles duplicate items in arrays differently. If there are duplicates in the first array, all matching duplicates will be returned. If there are duplicates in any of the subsequent arrays they will not be returned.

<?php
array_intersect(array(1,2,2),array(1,2,3)); //=> array(1,2,2)
array_intersect(array(1,2,3),array(1,2,2)); //=> array(1,2)
?>
gary 21-Jun-2009 09:57
i wrote this one to get over the problem i found in getting strings intersected instead of arrays as there is no function in php.

<?php
function matched_main_numbers($string, $string2)
{
$string = "04 16 17 20 29";
$arr1 = explode(" ", $string);

$string2 = "45 34 04 29 16";
$arr2 = explode(" ", $string2);

$array = array_intersect($arr1, $arr2);
$comma_separated = implode($array);

$str = $comma_separated;

$balls = "$comma_separated";
$matched_balls = chunk_split($balls,2," ");
$matched_balls =" $matched_balls";

$number_of_matched_main_balls = strlen($str);
$number_of_matched_main_balls = ($number_of_matched_main_balls/2);
$numbers = "You matched $number_of_matched_main_balls main balls";

return $numbers;

}
?>
Oto Brglez 10-Mar-2009 04:02
If you wish to create intersection with arrays that are empty. Than the result of intersection is empty array.

If you wish to change this. I sugest that you do this.
It simply "ignores" empty arrays. Before loop use 1st array.

<?php

$a = array();
$a[] = 1;
$a[] = 2;
$a[] = 3;

$b = array();
$b[] = 4;
$b[] = 5;
$b[] = 1;

$c = array();
$c[] = 1;
$c[] = 5;
$d = array();

$kb=array('a','b','c','d');

$out = $a;
foreach($kb as $k){
    if(!empty(${$k})) $out = array_intersect($out,${$k});
};
print_r($out);
// The result is array

// The result is empty array
print_r(array_intersect($a,$b,$c,$d));

?>
karl at libsyn dot com 30-Jan-2009 06:58
Given a multidimensional array that represents AND/OR relationships (example below), you can use a recursive function with array_intersect() to see if another array matches that set of relationships.

For example: array( array( 'red' ), array( 'white', 'blue' ) ) represents "red OR ( white AND blue )".  array( 'red', array( 'white', 'blue' ) ) would work, too, BTW.

If I have array( 'red' ) and I want to see if it matches the AND/OR array, I use the following function.  It returns the matched array,
but can just return a boolean if that's all you need:

<?php
$needle = array( array( 'red' ), array( 'white', 'blue' ) );
$haystack = array( 'red' );

function findMatchingArray( $needle, $haystack ) {
    foreach( $needle as $element ) {
        $test_element = (array) $element;
        if( count( $test_element ) == count( array_intersect( $test_element, $haystack ) ) ) {
            return $element;
        }

    }
    return false;
}
?>

Pretty tough to describe what I needed it to do, but it worked.  I don't know if anyone else out there needs something like this, but hope this helps.
Esfandiar -- e.bandari at gmail dot com 10-Aug-2008 09:44
Regarding array union:  Here is a faster version array_union($a, $b)

But it is not needed!  See below.

<?php
                                        //  $a = 1 2 3 4
    $union =                            //  $b =   2   4 5 6
        array_merge(
            $a,
            array_diff($b, $a)          //               5 6
        );                              //  $u = 1 2 3 4 5 6
?>

You get the same result with $a + $b.

N.B. for associative array the results of $a+$b and $b+$a are different, I think array_diff_key is used.

Cheers, E
stuart at horuskol dot co dot uk 08-Jul-2008 12:57
A clearer example of the key preservation of this function:

<?php

$array1 = array(2, 4, 6, 8, 10, 12);
$array2 = array(1, 2, 3, 4, 5, 6);

var_dump(array_intersect($array1, $array2));
var_dump(array_intersect($array2, $array1));

?>

yields the following:

array(3) {
  [0]=> int(2)
  [1]=> int(4)
  [2]=> int(6)
}

array(3) {
  [1]=> int(2)
  [3]=> int(4)
  [5]=> int(6)
}

This makes it important to remember which way round you passed the arrays to the function if these keys are relied on later in the script.
Malte 10-Feb-2008 07:34
Extending the posting by Terry from 07-Feb-2006 04:42:

If you want to use this function with arrays which have sometimes the same value several times, it won't be checked if they're existing in the second array as much as in the first.
So I delete the value in the second array, if it's found there:

<?php
$firstarray = array(1, 1, 2, 3, 4, 1);
$secondarray = array(4, 1, 6, 5, 4, 1);

//array_intersect($firstarray, $secondarray): 1, 1, 1, 4

foreach ($firstarray as $key=>$value){
    if (!in_array($value,$secondarray)){
        unset($firstarray[$key]);
    }else{
        unset($secondarray[array_search($value,$secondarray)]);
    }
}

//$firstarray: 1, 1, 4

?>
aaron 17-Dec-2006 06:36
this one will work with associative arrays.  also an overwrite function to only replace those elements in the first array.

<?php

function array_union()
{
    if (func_num_args() < 2) { return; }
    $arrays = func_get_args();
    $outputArray = array_shift($arrays);
    $remaining = count($arrays);
    for ($i=0; $i<$remaining; $i++)
    {
        $nextArray = $arrays[$i];
        foreach ($nextArray as $key=>$value)
        {
            $outputArray[$key] = $value;
        }
    }
    return $outputArray;
}

function array_overwrite()
{
    if (func_num_args() < 2) { return; }
    $arrays = func_get_args();
    $outputArray = array_shift($arrays);
    $remaining = count($arrays);
    for ($i=0; $i<$remaining; $i++)
    {
        $nextArray = $arrays[$i];
        foreach ($nextArray as $key=>$value)
        {
            if (array_key_exists($key, $outputArray)) {    $outputArray[$key] = $value; }
        }
    }
    return $outputArray;
}

?>
Niels 20-Sep-2006 01:53
Here is a array_union($a, $b):

<?php
                                        //  $a = 1 2 3 4
    $union =                            //  $b =   2   4 5 6
        array_merge(
            array_intersect($a, $b),    //         2   4
            array_diff($a, $b),         //       1   3
            array_diff($b, $a)          //               5 6
        );                              //  $u = 1 2 3 4 5 6
?>
nthitz at gmail dot com 09-Jun-2006 06:09
I did some trials and if you know the approximate size of the arrays then it would seem to be a lot faster to do this <?php array_intersect($smallerArray, $largerArray); ?> Where $smallerArray is the array with lesser items. I only tested this with long strings but I would imagine that it is somewhat universal.
terry(-at-)shuttleworths(-dot-)net 07-Feb-2006 04:42
I couldn't get array_intersect to work with two arrays of identical objects, so I just did this:

foreach ($firstarray as $key=>$value){
    if (!in_array($value,$secondarray)){
        unset($firstarray[$key]);
    }
}

This leaves $firstarray as the intersection.

Seems to work fine & reasonably quickly.
tom p 05-Nov-2005 03:54
If you store a string of keys in a database field and want to match them to a static array of values, this is a quick way to do it without loops:

<?

$vals = array("Blue","Green","Pink","Yellow");
$db_field = "0,2,3";

echo implode(", ", array_flip(array_intersect(array_flip($vals), explode(",", $db_field))));

// will output "Blue, Pink, Yellow"

?>
sapenov at gmail dot com 10-Jun-2005 10:11
If you need to supply arbitrary number of arguments
to array_intersect() or other array function,
use following function:

$full=call_user_func_array('array_intersect', $any_number_of_arrays_here);
SETS INTERSECTION 18-May-2005 08:19
$a = array(1,2,3,4,5,2,6,1);  /* repeated elements --> $a is not a set */
$b = array(0,2,4,6,8,5,7,9,2,1);  /* repeated elements --> $b is not a set */

$ua = array_merge(array_unique($a));  /* now, $a is a set */
$ub = array_merge(array_unique($b));  /* now, $b is a set */

$intersect = array_merge(array_intersect($ua,$ub));

Note: 'array_merge' removes blank spaces in the arrays.
Note: order doesn't matter.

In one line:

$intersect_a_b = array_merge(array_intersect(array_merge(array_unique($a)), array_merge(array_unique($b))));

Additions/corrections wellcome...

gRiNgO
drew at iws dot co dot nz 22-Apr-2005 05:04
Just a handy tip.

If you want to produce an array from two seperate arrays on their intersects, here you go:

<?

$a = array("branches","E_SHOP");
$b = array("E_SHOP","Webdirector_1_0");

print join("/",array_merge(array_diff($a, $b), array_intersect($a, $b), array_diff($b, $a)));

?>

Gives you:

/branches/E_SHOP/Webdirectory_1_0
blu at dotgeek dot org 15-Oct-2004 02:34
Note that array_intersect and array_unique doesnt work well with multidimensional arrays.
If you have, for example,

<?php

$orders_today[0] = array('John Doe', 'PHP Book');
$orders_today[1] = array('Jack Smith', 'Coke');

$orders_yesterday[0] = array('Miranda Jones', 'Digital Watch');
$orders_yesterday[1] = array('John Doe', 'PHP Book');
$orders_yesterday[2] = array('Z? da Silva', 'BMW Car');

?>

and wants to know if the same person bought the same thing today and yesterday and use array_intersect($orders_today, $orders_yesterday) you'll get as result:

<?php

Array
(
    [0] => Array
        (
            [0] => John Doe
            [1] => PHP Book
        )

    [1] => Array
        (
            [0] => Jack Smith
            [1] => Coke
        )

)

?>

but we can get around that by serializing the inner arrays:
<?php

$orders_today[0] = serialize(array('John Doe', 'PHP Book'));
$orders_today[1] = serialize(array('Jack Smith', 'Coke'));

$orders_yesterday[0] = serialize(array('Miranda Jones', 'Digital Watch'));
$orders_yesterday[1] = serialize(array('John Doe', 'PHP Book'));
$orders_yesterday[2] = serialize(array('Z? da Silva', 'Uncle Tungsten'));

?>

so that array_map("unserialize", array_intersect($orders_today, $orders_yesterday)) will return:

<?php

Array
(
    [0] => Array
        (
            [0] => John Doe
            [1] => PHP Book
        )

)

?>

showing us who bought the same thing today and yesterday =)

[]s
tompittlik at disfinite dot net 24-Jun-2004 03:27
Just a small mod to ben's code to make it work properly:

<?php

if(sort(array_unique($b + $a)) === sort($b))
 // $a is legit
}

?>

This is useful for checking for illegal characters in a username.
t dot wiltzius at insightbb dot com 24-Jun-2004 06:33
I needed to compare an array with associative keys to an array that contained some of the keys to the associative array. Basically, I just wanted to return only a few of the entries in the original array, and the keys to the entries I wanted were stored in another array. This is pretty straightforward (although complicated to explain), but I couldn't find a good function for comparing values to keys. So I wrote this relatively straightforward one:

<?php

function key_values_intersect($values,$keys) {
   foreach($keys AS $key) {
      $key_val_int[$key] = $values[$key];
      }
   return $key_val_int;
   }

$big = array("first"=>2,"second"=>7,"third"=>3,"fourth"=>5);
$subset = array("first","third");

print_r(key_values_intersect($big,$subset));

?>

This will return:

Array ( [first] => 2 [third] => 3 )
anbolb at boltblue dot com 09-Jan-2004 10:11
This is also handy for testing an array for one of a series of acceptable elements. As a simple example, if you're expecting the query string to contain one of, say, user_id, order_id or item_id, to find out which one it is you could do this:

<?php
    $valid_ids = array ('user_id', 'item_id', 'order_id');
    if ($id = current (array_intersect ($valid_ids, array_keys ($_GET))))
    {
        // do some stuff with it
    }
    else
        // error - invalid id passed, or none at all
?>

...which could be useful for constructing an SQL query, or some other situation where testing for them one by one might be too clumsy.
ben at kazez dot com 09-Dec-2003 07:49
To check whether an array $a is a subset of array $b, do the following:

<?php
if(array_unique($b + $a) === $b)
//...
?>

Actually, PHP ought to have a function that does this for you. But the above example works.
Alessandro Ranellucci alex at primafila dot net 16-Jul-2003 03:35
array_intersect($array1, $array2);
returns the same as:
array_diff($array1, array_diff($array1, $array2));
david at audiogalaxy dot com 10-Apr-2001 01:54
Note that array_intersect() considers the type of the array elements when it compares them.

If array_intersect() doesn't appear to be working, check your inputs using var_dump() to make sure you're not trying to intersect an array of integers with an array of strings.


주어진 키와 인덱스가 배열에 존재하는지 확인

  • ≪ array_intersect
  • array_keys ≫
  • PHP Manual
  • 배열 함수 목록
  • 주어진 키와 인덱스가 배열에 존재하는지 확인

array_key_exists

(PHP 4 >= 4.0.7, PHP 5, PHP 7)

array_key_exists — 주어진 키와 인덱스가 배열에 존재하는지 확인

설명

bool array_key_exists ( mixed $key , array $search )

array_key_exists()는 주어진 key가 배열 안에서 설정되어 있으면 TRUE를 반환한다. key는 배열 인덱스로 사용할수 있는 어떤값이든 될수 있다.

인수

key

확인할 값.

search

확일할 키를 가진 배열.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
5.3.0 | 객체에 대하여 작동하지 않습니다. 이 경우 property_exists()를 사용해야 합니다.

예제

Example #1 array_key_exists() 예제

<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
}
?>

Note: PHP 4.0.6에서 이 함수의 이름은 key_exists()이다.

Example #2 array_key_exists() 대 isset()

배열 키에 해당하는 값이 NULL일 때, isset()은 TRUE를 반환하지 않지만, array_key_exists()는 TRUE를 반환합니다.

<?php
$search_array = array('first' => null, 'second' => 4);

// false 반환
isset($search_array['first']);

// true 반환
array_key_exists('first', $search_array);
?>

참고

  • isset() - 설정된 변수인지 확인
  • array_keys() - 배열의 모든 키를 반환
  • in_array() - 값이 배열 안에 존재하는지 확인
  • property_exists() - 객체나 클래스가 프로퍼티를 가졌는지 확인

User Contributed Notes

zildo 04-Oct-2016 11:49
This function will look for a key in multidimensional arrays, and return the paths to all the results found, or return false if none have been found.

<?php

function array_multi_key_exists($key, $array, $first=true, &$return=array()){

if($first){

while($ret = array_multi_key_exists($key, &$array, false)){
$r[]=$ret;
}

if(empty($r[0])){
return $r[0]=array();
}else{
return $r;
}

}

foreach($array as $k => $v){

if($k === $key && !(is_array($v))){

unset($array[$k]);
array_unshift($return, $k);
return $return;

}elseif($k === $key && is_array($v)){

if(empty($v)){
unset($array[$k]);
}else{

if(array_multi_key_exists($key, &$array[$k], false, $return)){

array_unshift($return, $k);
return $return;

}

}

array_unshift($return, $k);
unset($array[$k]);
return $return;

}else{

if(is_array($v)){

if(array_multi_key_exists($key, &$array[$k], false, $return)){

array_unshift($return, $k);
return $return;

}

}

}

}

return false;

}

?>

The next function takes the array of paths returned by array_multi_key_exists as parameter and returns the corresponding values.

<?php

function array_multi_values($paths, $array){

$return = array();

foreach($paths as $n => $path){
$val = $array;
foreach($path as $dim => $key){

$val = $val[$key];

}

$return[$n] = $val;
unset($val);

}

return $return;

}

?>

This next function takes the paths array returned by array_multi_key_exists, and unsets the corresponding values of the original array, returning the clipped array.

<?php

function array_multi_clip($paths, $array, $key=0, $path=false){

while($path==false){

foreach($paths as $path){
array_multi_clip($paths, &$array, 0, $path);
}
return $array;
}

$n = count($path);

if($key == $n-1){
unset($array[$path[$key]]);
return $array;
}else{

array_multi_clip($paths, &$array[$path[$key]], ++$key, $path);
}

}

?>

Examples:

<?php

$array = array(

'colours' => array(

'red' => array(

'veg' => 'tomato',
'fruit' => 'strawberry',
'green' => 'colourblind',

),

'green' => array(

'green' => 'envy',
'unripe' => 'veggies',

),

'blue' => 'smurfs',

),

);

$key = 'green';

$paths = array_multi_key_exists($key, $array);
print_r($paths);

?>

The result will be an array of paths:

Array
(
    [0] => Array
        (
            [0] => colours
            [1] => red
            [2] => green
        )

    [1] => Array
        (
            [0] => colours
            [1] => green
            [2] => green
        )

    [2] => Array
        (
            [0] => colours
            [1] => green
        )

)

As it can be seen, if one of the results is an array which again contains the key searched, both will show up in the results.

Example of array_multi_values:

<?php

$values = array_multi_values($paths, $array);
print_r($values);

?>

Result:

<?php

Array
(
    [0] => colourblind
    [1] => envy
    [2] => Array
        (
            [green] => envy
            [unripe] => veggies
        )

)

?>

Example of array_multi_clip():

<?php

$clipped = array_multi_clip($paths, $array);
print_r($clipped);

?>

Result:

<?php
Array
(
    [colours] => Array
        (
            [red] => Array
                (
                    [veg] => tomato
                    [fruit] => strawberry
                )

            [blue] => smurfs
        )

)

?>
Venkata Subbaraju 03-Nov-2015 02:42
array_key_exists perfect for $_GET .

As there will be browser level restriction on length of URL , number of keys in $_GET global variable will be very limited . So array_key_exists will not cause any performance overhead in this context . isset function can be conveniently ignored while handling $_GET variable.

But for $_POST , the max array size has to be configure in server PHP configuration . So cannot rely on array_key_exists for $_POST if max array size is not optimized .

Here is an example with array_key_exists switching between content-types :

if(array_key_exists('format',$_GET))
{
              if($_GET['format']=="json")
              {
                      header('Content-Type: application/json');
              }
              else{
                     //handle any other format whitelisted for the application
              }
}
else {
               error_log("format parameter missing . using default html format");
               header('Content-Type: text/html');
}
p dot allgeier at web dot de 19-Aug-2015 07:36
If u want to check if a key exists either in the array or in any subarray try the following:

<?php

function requireKey($needle, array $array)
    {
        foreach ($array as $key => $value) {
            if ($key === $needle) return $value;
            if (is_array($value)) {
                if ($x = $this->requireKey($key, $value)) return $x;
            }
        }
        return false;
    }

?>

(( return $value for recursive array search, return true for recursive array key exists ))
ellert at vankoperen dot nl 14-Oct-2014 04:49
While working with large JSON datastructures I encountered an increasing need to check the presence of certain data all over the place.
This led to the function below that lets you simply access the data, and returns it if available, null if not.
Note that it uses a variable number of parameters, you can go as deep into a nested structure as you want.

<?php
function safe_array_access($ar){
    $numargs = func_num_args();
    $arg_list = func_get_args();
    $aritterator = $ar;
    for($i = 1; $i < $numargs; $i++){
        if (isset($aritterator[$arg_list[$i]]) || array_key_exists($arg_list[$i], $aritterator)){
            $aritterator = $aritterator[$arg_list[$i]];
        }else{
            return(false);
        }
    }
    return($aritterator);
}
?>
Usage:
Instead of  $a['b']['c'] use safe_array_access($a, 'b', 'c');
mc dot watras at gmail dot com 25-Jul-2014 05:12
array_key_exists doesn't work with objects implementing ArrayAccess interface. It also ignores possible __get() method in such objects, despite the fact it accepts object as a second parameter. It works only with 'real' properties.

<?php
class A implements ArrayAccess {
    public $data;
    public function offsetExists($offset) {
        return isset($this->data[$offset]);
    }
    public function __get($property) {
        return $this->data[$property];
    }
}

$a = new A();
$a->data['somekey'] = 1;
var_dump(array_key_exists('somekey', $a)); // returns false
var_dump(array_key_exists('data', $a));  // returns true
?>
jakub dot lopuszanski at nasza-klasa dot pl 01-Apr-2014 07:45
It's not mentioned directly in this spec, but implementing ArrayAccess intereface is unfortunately not enough to make array_key_exists work as expected :(
In other words array_key_exists and ArrayAccess::offsetExists is not the same thing :/
Sandro Alves Peres 19-Jun-2013 08:30
<?php
/*
    It also works on objects
    ***************************************
*/

class PHP
{

    private $compiler;
    protected $architecture;
    public $version;
    public $system = "Linux";

}

$php = new PHP();

$a = array_key_exists('compiler', $php);     # false
$b = array_key_exists('architecture', $php); # false
$c = array_key_exists('version', $php);      # true
$d = array_key_exists('system', $php);       # true

var_dump( $a, $b, $c, $d );

?>
imaginary at friend dot com 10-Apr-2012 01:37
An even simpler case-insensitive alternative to array_key_exists():

<?php

array_key_exists(strtolower($key), array_change_key_case($search));
Nik Tang 05-Mar-2012 10:22
If you want to take the performance advantage of isset() while keeping the NULL element correctly detected, use this:

if (isset(..) || array_key_exists(...))
{
...
}

Benchmark (100000 runs):
array_key_exists() : 205 ms
is_set() : 35ms
isset() || array_key_exists() : 48ms

Note:
The code for this check is very fast, so you shouldn't warp the code into a single function like below, because the overhead of calling a function dominates the overall performance.

function array_check(...)
{
    return (isset(..) || array_key_exists(...))
}
Rudi 10-Nov-2011 11:10
I've got a new take on the multi key function I would like to share.

<?php
function array_multi_key_exists(array $arrNeedles, array $arrHaystack, $blnMatchAll=true){
    $blnFound = array_key_exists(array_shift($arrNeedles), $arrHaystack);

    if($blnFound && (count($arrNeedles) == 0 || !$blnMatchAll))
        return true;

    if(!$blnFound && count($arrNeedles) == 0 || $blnMatchAll)
        return false;

    return array_multi_key_exists($arrNeedles, $arrHaystack, $blnMatchAll);
}
?>

Hope you'll find it usefull.
glitch dot mr at gmail dot com 01-Oct-2011 01:15
As you might know, isset() is actually working like @$variable===NULL. As the result, it doesn't actually catch variables set to NULL. If you want to check if variable is set (even to NULL), you can use array_key_exists on $GLOBALS, like there.

<?php
   $me = null;
   $se = 1;
   unset($se);
// $he is not set

if(array_key_exists('me', $GLOBALS)) echo "\$me exists\n";
if(array_key_exists('se', $GLOBALS)) echo "\$se exists\n";
if(array_key_exists('he', $GLOBALS)) echo "\$he exists\n";

?>

In this case, only $me will be detected as $se was removed when code was running and $he was never set.
Anonymous 16-Jun-2011 10:07
I just want to note that array_key_exists() can be extremely slow for large (>200 keys) arrays.  Use isset($array($key)) instead!  My program ran in 3 minutes instead of 2 hours after switching to isset()!
manhon824 at gmail dot com 18-May-2011 06:29
I took hours for me to debug, and I finally recognized that,

You have to reset the $array before using array_key_exists
reset($array);
array_key_exists($needle,$array);

Or you will get no reply.
tech at signhere envy dot de 18-Jan-2011 11:23
Hey, this function is able to rename a key inside an array.

If the key to be replaced doesn't exist inside the array, or the new key already exists in the array, the function will return FALSE.
Otherwise, the array with the renamed key will be returned.

Hope this will be useful for someone.
<?php
function array_rename_key($array, $key, $newkey){
    if( ! array_key_exists($key, $array) || array_key_exists($newkey, $array)) return FALSE;
    $uid = uniqid(''); //To clearly identify the Element.
    $preserve_value = $array[$key]; //Keep the Value
    $array[$key] = $uid; //Overwrite Value with ID
    $array = array_flip($array); //Flip the Array keys and values
    $array[$uid] = $newkey; //Set Value of the ID with new Key.
    $array = array_flip($array); //Everything back in Place.
    $array[$newkey] = $preserve_value;
    return $array;
}
?>
jens dot hoevenaars at gmail dot com 07-Nov-2010 12:21
I created this function that uses array key exist to compare a form and a table to see if something has changed.

This can be very helpfull if you need to update a table record from a form but you do not want to display all table fields.

<?php
function($data_from_db, $form_data) {
 $data = $data_from_db;
 $keys = array_keys($data);

    for($i = 0; $i < count($data); $i++) {
        if(!array_key_exists($keys[$i], $form_data)) {
            $dbobject->$keys[$i] = $data[$keys[$i]];
        } else {
            $dbobject->$keys[$i] = $form_data[$keys[$i]];
        }
    }
    return $dbobject;
}
?>

you can then use the dbobject to update the table.
moandsimon at btinternet dot com 26-Apr-2010 10:56
This uses array_key_exists.

You have a multidimensional array of the form:

$rowsoriginal[] = array('field_wrkvolmin_value' => 216, 'field_wrkvolmax_value' => 1000);
$rowsoriginal[] = array('field_wrkvolmin_value' => 27, 'field_wrkvolmax_value' => 216);

Using print_r this will look like:

Array ( [0] => Array ( [field_wrkvolmin_value] => 216 [field_wrkvolmax_value] => 1000 ) [1] => Array ( [field_wrkvolmin_value] => 27 [field_wrkvolmax_value] => 216 ) )

This can be used to create a table by iterating over the rows that looks like this:

field_wrkvolmin_value          field_wrkvolmax_value
           216                                      1000
            27                                       216

when $rowsoriginal contain a fixed but unknown amount of values.

If you want to process this in an automatic way without knowing the keys etc, into a multidimensional array of the form:

$rowstemp = array('field_wrkvolmin_value' => array(216, 27), 'field_wrkvolmax_value' => array(1000, 216));

Using print_r this will look like:

Array ( [field_wrkvolmin_value] => Array ( [0] => 216 [1] => 27 ) [field_wrkvolmax_value] => Array ( [0] => 1000 [1] => 216 ) )

This can be used to iterate over the rows of a table to create a table in the form of:

field_wrkvolmin_value            216            27
field_wrkvolmax_value           1000           216

To do this you can use the following looping and conditional structure, using array_key_exists():

<?php
$rowstemp = array();
foreach ($rowsoriginal as $row) {
    foreach ($row as $key => $value) {
        if (array_key_exists($key, $rowstemp)) {
            $rowstemp[$key][] = $value;
        }
        else {
            $valuestemp = array($value);
            $rowstemp[$key] = $valuestemp;
        }
    }
}
?>
gmdebby at gmail dot com 14-Jan-2010 02:44
A little function which take an array as keys

<?php
//note the s in the function name (keys)
function array_keys_exists($array,$keys) {
    foreach($keys as $k) {
        if(!isset($array[$k])) {
        return false;
        }
    }
    return true;
}
?>
//useful to validate a form for example
<form>
    <input type="text" name="field1" /><br />
    <input type="text" name="field2" /><br />
    <input type="text" name="field3" /><br />
    <input type="text" name="field4" /><br />
    <input type="text" name="field5" /><br />
</form>
<?php
if(!array_keys_exists($_POST,
array("field1","field2","field3","field4","field5")
)) {
    //some fields are missing, dont do anything (maybe hacking)
} else {
    //code ...
}
?>
PHPEric 05-Aug-2009 03:51
Very simple case-insensitive array_key_exists:

bool (in_array(strtolower($needle), array_map('strtolower', array_keys($haystack))))
tom at edgedesigns dot org 21-Jul-2009 02:10
The multi_array_key_exists() function posted by alishahnovin at hotmail dot com [which has since been removed] does not always return the expected result.

This modified version does.

<?php
/**
 * multi_array_key_exists function.
 *
 * @param mixed $needle The key you want to check for
 * @param mixed $haystack The array you want to search
 * @return bool
 */
function multi_array_key_exists( $needle, $haystack ) {

    foreach ( $haystack as $key => $value ) :

        if ( $needle == $key )
            return true;

        if ( is_array( $value ) ) :
             if ( multi_array_key_exists( $needle, $value ) == true )
                return true;
             else
                 continue;
        endif;

    endforeach;

    return false;
}

?>
webmaster at oehoeboeroe dot nl 04-May-2009 10:09
The way array_key_exists handles null, float, boolean, and 'integer-representing string' keys is inconsistent in itself and, in the case of bool and float, with the way these are converted when used as array offset.

<?php
$array = array(null => 1, false => 2, true => 3, 4.6 => 4, "08" => 5, "8" => 6);
var_export($array);

echo "\nnull is " . (array_key_exists(null, $array) ? '' : 'not ') . "a key.\n";
echo 'false is ' . (array_key_exists(false, $array) ? '' : 'not ') . "a key.\n";
echo 'true is ' . (array_key_exists(true, $array) ? '' : 'not ') . "a key.\n";
echo '4.6 is ' . (array_key_exists(4.6, $array) ? '' : 'not ') . "a key.\n";
echo '"08" is ' . (array_key_exists("08", $array) ? '' : 'not ') . "a key.\n";
echo '"8" is ' . (array_key_exists("8", $array) ? '' : 'not ') . "a key.\n";
?>

Output:

array (
  '' => 1,
  0 => 2,
  1 => 3,
  4 => 4,
  '08' => 5,
  8 => 6,
)
null is a key.
false is not a key.
true is not a key.
4.6 is not a key.
"08" is a key.
"8" is a key.

Well, and you get this warning three times (on the bools and the float, but not on the null):

Warning:  array_key_exists() [function.array-key-exists]: The first argument should be either a string or an integer in /var/www/php/test.php on line 6
csaba at alum dot mit dot edu 20-Jan-2009 02:13
In some functions cacheing can be extremely useful, and this is especially true in recursive functions, and all the more so in doubly recursive functions.  One way to effect cacheing is to have a static array in the function as shown below.  This will typically be useful when a function has one or two arguments and is heavily called.  Example:

<?php
function nk($n, $k) {
  // n choose k, ensuring integer math
  if ($k > $n-$k) $k = $n - $k;         // (n,k) = (n,n-k)
  if ($k<=1) return ($k==1) ? $n : !$k; // (n,1) = n; (n,0)=1; (n,-#) = 0

  static $aNK = array();                // caching section
  if ($aNK[$n][$k]) return $aNK[$n][$k];// if answer already computed => done

  // else compute answer, cache it, and return
  return ($aNK[$n][$k] = nk($n-1, $k) + nk($n-1, $k-1)); }

$n = 20;
$k = 9;
$nk = nk($n,$k);
print "nk($n, $k) = $nk";
?>

For functions of two arguments, using a two dimensional array seems to be faster than combining arguments a la "$n $k", and the form of testing shown above is also faster than array_key_exists.  However, to avoid the use of array_key_exists, special consideration must be given to empty values (several prior notes cover this) - the example above works because no cached value can be 0.

Csaba Gabor from Vienna
John 19-Jan-2009 06:27
Here is a little function for case sensitivity to elaborate on what was said by MarkL from ##php (Freenode) and mmanning at mdanderson dot org from this page:

<?php
// Case sensitive version of array_key_exists() using preg_match()
function array_ikey_exists($key,$arr)
{
    if(preg_match("/".$key."/i", join(",", array_keys($arr))))
        return true;
    else
        return false;
}
?>

Not that anyone else couldn't have written this, but a concept like this strengthens reusability.  :)

Also, I've been running into issues with escaping for Regex, so I decided to give something like this a shot:

<?php
function array_ikey_exists($key,$arr)
{
    $e = 0; //$key = addslashes($key);
    if(is_array($arr) && $arr !==array())
    {
        foreach($arr as $k => $v)
        {
            if(strtolower($k) == strtolower($key))
                $e++;
        }
        if($e>0)
            return true;
        else
            return false;
    }
    else
        return false;
}
?>

You could addslashes() to escape; it's just another approach.
david at lostair dot com 07-Dec-2008 09:07
It is important to realise that:

  isset($array ['index'])

Does not act the same as:

  array_key_exists('index', $array)

This is because if an array value is set to NULL

$array['index'] = NULL;

Then isset will return FALSE and array_key_exists will return TRUE.

It is important to realise this!
mmanning at mdanderson dot org 21-Oct-2008 05:13
Something to keep in mind is that array_key_exists is case sensitive.  So two things I'd like to say:

1. Could there be an additional option added on to the array_key_exists command so you can specify the case to be insensitive?

2. Here is how you can do this test in one command:

   if( preg_match("/<key>/i", join(",", array_keys(<array>))) ){
      <Do something>
      }

But it would be a lot nicer to just have an additional option on the array_key_exists.  Like so:

   array_key_exists(<array>, ["i"] );

Ok - so why did this come up at all?  I wrote a routine to convert any whacky UppERcAsE kind of lettering to just be lowercase letters. (ex: FgColorS becomes fgcolors and so forth.)  So I never had to guess what the user was putting in to the program.  There were two particular items (fgColors and bgColors) which I wanted to test against.  If they were not supplied, my program would supply them.  The above was the easiest way I could come up with to do this.  :-)
Karim Ratib 19-Aug-2008 02:09
Here's a function to return a reference to the first array element that has a given key. The code works for multidimensional arrays:

<?php
function &array_find_element_by_key($key, &$form) {
  if (array_key_exists($key, $form)) {
    $ret =& $form[$key];
    return $ret;
  }
  foreach ($form as $k => $v) {
    if (is_array($v)) {
      $ret =& array_find_element_by_key($key, $form[$k]);
      if ($ret) {
        return $ret;
      }
    }
  }
  return FALSE;
}
?>
jacobsingh at gmail dot com 17-Jun-2008 12:54
I saw some examples above for array_keys_exist() or functions to see if multiple keys exist in a given array and return false if any of them don't.

Here is a simpler way to do this:

<?php

function array_keys_exist($keys,$array) {
    if (count (array_intersect($keys,array_keys($array))) == count($keys)) {
        return true;
    }
}

$array = array ('filename' => 'myfile', 'filesize' => 1234, 'filepath' => '/tmp/myfile');
$keys = array('filename','filesize','filepath');

echo array_keys_exist($keys,$array);
//returns true

$keys[] = "somethingelse";

echo array_keys_exist($keys,$array);
//Returns false

?>
mankyd at gmail dot com 28-May-2008 07:29
You'll notice several notes on this page stating that isset() is significantly faster than array_key_exists(). This may be true except for one small hitch. isset() will return false for arrays keys that have there value set to NULL, which is therefore not entirely accurate.

Example:

<?php
$foo = array();
$foo['bar'] = NULL;

var_dump(isset($foo['bar']));
var_dump(array_key_exists('bar', $foo));
?>

will output:
bool(false)
bool(true)

Be aware of this!
bplessingerMONKEYleapfroginteractive.com 30-Apr-2008 06:22
I noticed that the function for recursion broke the ability to use this on objects, so I added another check to also allow it to work for objects.

<?php

function array_key_exists_r($needle, $haystack)
{
    $result = array_key_exists($needle, $haystack);
    if ($result)
        return $result;
    foreach ($haystack as $v)
    {
        if (is_array($v) || is_object($v))
            $result = array_key_exists_r($needle, $v);
        if ($result)
        return $result;
    }
    return $result;
}
?>
tmont 29-Apr-2008 11:54
The argument of array_key_exists() vs. isset() came up in the workplace today, so I conducted a little benchmark to see which is faster:

<?php
    // one-dimensional arrays
    $array = array_fill(0,50000,'tommy is the best!');
    $arraykeyexists_result = array();

    $start = microtime(true);
    for ($i = 0; $i < 100000; $i++) {
        if (array_key_exists($i,$array)) {
            $arraykeyexists_result[] = 1;
        }
        else {
            $arraykeyexists_result[] = 0;
        }
    }
    $arrtime = round(microtime(true)-$start,3);

    $start = microtime(true);
    for ($i = 0; $i < 100000; $i++) {
        if (isset($array[$i])) {
            $arraykeyexists_result[] = 1;
        }
        else {
            $arraykeyexists_result[] = 0;
        }
    }
    $istime = round(microtime(true)-$start,3);

    $totaltime = $arrtime+$istime;
    $arrpercentage = round(100*$arrtime/$totaltime,3);
    $ispercentage = round(100*$istime/$totaltime,3);

    echo "array_key_exists(): $arrtime [$arrpercentage%] seconds\n";
    echo "isset():            $istime [$ispercentage%] seconds\n";

?>

On Windows, the output is similar to

array_key_exists(): 0.504 [82.895%] seconds
isset():            0.104 [17.105%] seconds

On Mac or Linux, isset() is faster but only by a factor of approximately 1.5.
packard_bell_nec at hotmail dot com 31-Jan-2008 06:46
You can check whether a variable is defined by using array_key_exists()!
First, you may ask that no reserved array (would be called $LOCALS) is predefined in function scope (contrast to reserved array $GLOBALS in global scope. To solve it, you can use compact().
Then, you may ask that why property_exists() cannot be used. This is because no reserved function is predefined to create OBJECT containing variables and their values, and no reserved function is predefined to import variables into the current symbol table from an OBJECT. In addition, property_exists() breaks the naming convention of reserved function.
Finally, I show how to check whether a variable is defined by using array_key_exists():
<?php
function too(){
$roo = array_key_exists('foo', compact('foo'));
echo ($roo?'1':'0').'<br/>';
$foo = null;
$roo = array_key_exists('foo', compact('foo'));
echo ($roo?'1':'0').'<br/>';
}
too();
?>
The output will be:
0<br/>
1<br/>
wolf550e at gmail dot com 27-Sep-2007 09:51
array_key_exists(), at least in 5.2.4, passes the array by value. I conclude this from seeing performance worsen as the array to search got bigger. isset() doesn't have this problem.
diogoshaw at gmail dot com 15-Sep-2007 08:58
this function very good to use if you need to verify many variables:

<?php
function array_key_exists_r($keys, $search_r) {
    $keys_r = split('\|',$keys);
    foreach($keys_r as $key)
    if(!array_key_exists($key,$search_r))
    return false;
    return true;
}
?>

e.g.

<?php
if(array_key_exists_r('login|user|passwd',$_GET)) {
// login
} else {
// other
}
?>

works for me, enjoy.
dg shaw.
sj-b at hotmail dot de 01-Aug-2007 06:14
i dont like how empty() works.
an integer with value 0 or a boolean wth
value false (same like zero) counts as
empty too.

[code]function r_empty (&$check)
{
    if (!isset($check)) return true;
    if ($check == NULL) return true;
    return false;
}[/code]

that is a good replacement for
both functions for me.
Lucknut dot xbl at googlemail dot com 18-Jul-2007 07:44
I found this function very good to use if your want your urls like index.php?login or index.php?register
e.g.
<?php
if( array_key_exists( 'home',$_GET ) ) {
    echo "Home - its where the heart is.";
} else if( array_key_exists( 'login',$_GET ) ) {
    echo "Login code here!";
} else if( array_key_exists( 'register',$_GET ) ) {
    echo "Register code here!";
} else {
    echo "Home - its where the heart is.";
}
?>
david at madole dot net 06-Jul-2007 05:11
Regarding performance differences between isset() and array_key_exists(), the differences may be there, but the function are not always interchangable.

Note that when $a[1] = null then isset($a[1]) == false but array_key_exists(1, $a) == true
eidberger at jakota dot de 11-Jun-2007 10:14
Just wondered why array_key_exists() makes me a cpu-load of 85% while isset() only needs 35%.

Not a big thing for one time execution, but in my case it have to check 1-dimensional array with ~ 15.000 entries 100 times a second. My code checks a big array for existing entrys and updates them, if needed.

Hopes it helps somebody. Notice that on many other functions, which makes coding more comfortable at the cost of speed.
php at ianco dot co dot uk 09-Apr-2007 10:58
array_key_exists is case sensitive (at least in PHP 4.3.9). To make a case-insensitive comparison you could use strtolower on both sides.
inker2576 at yahoo dot com 07-Mar-2007 05:01
Further research on this has turned up that the performance problems are a known, confirmed bug in PHP 5.1.x, and have been fixed in PHP builds after September 2006.  You can find the bug report here:  http://bugs.php.net/bug.php?id=38812

However, just because it's a fixed bug doesn't really change the conclusion.  If you're writing a script and there's any chance it could be used on a PHP 5.1.x server, you should still avoid this function and use isset() or some other kind of test if you want it to run efficiently.
serkan yersen 07-Feb-2007 01:01
marzetti.marco,
I fixed your function it's is more optimized and working better now.

function regex_array_keys($arr, $pattern){
   $results[] = false;

   if(!is_array($arr))
       return false;

   foreach($arr as $key => $val){
         if(!is_array($key))
       if(preg_match($pattern,$key))
              array_push($results,$key);
   }

   return $results;
}
Matt 01-Dec-2006 10:50
mikael dot knutsson at gmail dot com:
I don't think it does, at least in PHP5?

For example, this outputs bool(false):

$ar = array ( 'outter' => array ( 'inner' => 1 ) );
var_dump(array_key_exists('inner', $ar));

So it doesn't actually check the inner array for the key 'inner'.
mikael dot knutsson at gmail dot com 25-Nov-2006 01:05
When dealing with multi-dimensional arrays, this function checks through all keys in the array, including the "child arrays" unlike the array_keys( array, $search ) function which would only check and return from the first level of keys.

Took me a couple of minutes to figure out what was wrong and I hope it helps some people when looking for the right function.


배열의 모든 키를 반환

  • ≪ array_key_exists
  • array_map ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 모든 키를 반환

array_keys

(PHP 4, PHP 5, PHP 7)

array_keys — 배열의 모든 키를 반환

설명

array array_keys ( array $input [, mixed $search_value [, bool $strict ]] )

array_keys()는 input 배열에서 숫자와 문자열의 키를 반환한다.

선택 인수 search_value가 지정되면, 그 값에 대한 키만 반환한다. 그렇지 않으면, input의 모든 키를 반환한다. PHP 5부터, strict 인수를 사용해서 자료형을 포함하여 비교(===)할 수 있습니다.

인수

input

반환할 키를 가진 배열.

search_value

지정하면, 이 값들을 가진 키만 반환합니다.

strict

PHP 5부터, 이 인수는 검색 중에 엄격한 비교(===)를 하도록 합니다.

반환값

input에 있는 모든 키의 배열을 반환합니다.

예제

Example #1 array_keys() 예제

<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));

$array = array("color" => array("blue", "red", "green"),
               "size"  => array("small", "medium", "large"));
print_r(array_keys($array));
?>

위 예제의 출력:

Array
(
    [0] => 0
    [1] => color
)
Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)
Array
(
    [0] => color
    [1] => size
)

참고

  • array_values() - 배열의 모든 값을 반환
  • array_key_exists() - 주어진 키와 인덱스가 배열에 존재하는지 확인

User Contributed Notes

ayyappan dot ashok at gmail dot com 19-Apr-2016 03:18
Post By  Sven (59892) has to be changed

$array = array('first'=>'111', 'second'=>'222', 'third'=>'333');

$rarray = array_keys($array);

print array_shift($rarray); // first

print array_pop($rarray); //thrid

print array_shift($rarray); //second

print array_pop($rarray); // no result

Code below is not valid from 5.4.0

print array_shift(array_keys($array)); Throws Strict Standards Error

Sven code works fine till 5.3.29. From 5.4.0  the standards have been changed and results too differ.

Note :
Strict Standards :  Only variables should be passed by reference

Please have look of the code in different versions

http://sandbox.onlinephpfunctions.com/code/24b5fddf14b635f1e37db69a7edffc2cbbed55e1

http://sandbox.onlinephpfunctions.com/code/f695e8f81e906b4f062b66cf9b3b83b6b620464c
Robert C. 12-Feb-2016 11:22
Keys from multi dimensional array to simple array

Want to traverse an multi dimensional array and get the keys back in a single dimensional array? This will do the trick:

<?php

    public function array_walk_keys($array, $parentKey = null, &$flattened_array = null)
    {
        if(!is_array($array))
            return $array;

        foreach( $array as $key => $val ) {
            $flattenedKeysArray[] = $key;

            if(is_array($val))
                array_walk_keys($val, $key, $flattenedKeysArray);
        }

        return $flattenedKeysArray;
    }
Paul Hirsch 31-Mar-2015 01:31
It is worth noting that array_keys does not maintain the data-type of the keys when mapping them to a new array.  This created an issue with in_array and doing  a lookup on characters from a string.  NOTE:  my lookup $array has a full map of numbers and characters - upper and lower - to do an simple faux encryption with.

<?php
$array = array(
     'e' => 'ieio'
    ,'1' => 'one'
    ,'2' => 'two'
    ,'0' => 'zero'
);
var_dump($array);
$keys = array_keys($array);
var_dump($keys);

$string = '1e0';
for ($i = 0; $i < strlen($string); $i++) {
    if (in_array($string[$i],$keys,'strict')) echo 'dude ';
    else echo 'sweet ';
}
?>

Outputs:
array (size=4)
  'e' => string 'ieio' (length=4)
  1 => string 'one' (length=3)
  2 => string 'two' (length=3)
  0 => string 'zero' (length=4)

array (size=4)
  0 => string 'e' (length=1)
  1 => int 1
  2 => int 2
  3 => int 0

sweet dude sweet

----
expected to see:
dude dude dude
Ian (maxianos at hotmail dot com) 09-Mar-2014 12:41
There's a lot of multidimensional array_keys function out there, but each of them only merges all the keys in one flat array.

Here's a way to find all the keys from a multidimensional  array while keeping the array structure. An optional MAXIMUM DEPTH parameter can be set for testing purpose in case of very large arrays.

NOTE: If the sub element isn't an array, it will be ignore.

<?php
function array_keys_recursive($myArray, $MAXDEPTH = INF, $depth = 0, $arrayKeys = array()){
       if($depth < $MAXDEPTH){
            $depth++;
            $keys = array_keys($myArray);
            foreach($keys as $key){
                if(is_array($myArray[$key])){
                    $arrayKeys[$key] = array_keys_recursive($myArray[$key], $MAXDEPTH, $depth);
                }
            }
        }

        return $arrayKeys;
    }
?>

EXAMPLE:
input:
array(
    'Player' => array(
        'id' => '4',
        'state' => 'active',
    ),
    'LevelSimulation' => array(
        'id' => '1',
        'simulation_id' => '1',
        'level_id' => '1',
        'Level' => array(
            'id' => '1',
            'city_id' => '8',
            'City' => array(
                'id' => '8',
                'class' => 'home',
            )
        )
    ),
    'User' => array(
        'id' => '48',
        'gender' => 'M',
        'group' => 'user',
        'username' => 'Hello'
    )
)

output:
array(
    'Player' => array(),
    'LevelSimulation' => array(
        'Level' => array(
            'City' => array()
        )
    ),
    'User' => array()
)
zammit dot andrew at gmail dot com 16-Nov-2013 01:06
If an array is empty (but defined), or the $search_value is not found in the array, an empty array is returned (not false, null, or -1). This may seem intuitive, especially given the documentation says an array is returned, but I needed to sanity test to be sure:

<?php

$emptyArray = array();
var_dump(array_keys($emptyArray,99)); // array (size=0) \ empty

$filledArray = array(11,22,33,42);
var_dump(array_keys($filledArray,99)); // array (size=0) \ empty

?>
phpnet at holodyn dot com 24-Oct-2013 02:17
Since 5.4 STRICT standards dictate that you cannot wrap array_keys in a function like array_shift that attempts to reference the array.

Invalid:
echo array_shift( array_keys( array('a' => 'apple') ) );

Valid:
$keys = array_keys( array('a' => 'apple') );
echo array_shift( $keys );

But Wait! Since PHP (currently) allows you to break a reference by wrapping a variable in parentheses, you can currently use:

echo array_shift( ( array_keys( array('a' => 'apple') ) ) );

However I would expect in time the PHP team will modify the rules of parentheses.
anthony at berglas dot org 03-Apr-2012 02:32
It should be noted that the inverse function to keys (which converts keys to values) is array_count_values (which converts values to keys).  This is needed to use things like array_intersect_key.  Could go in several places.  Took me a while to figure it out.
Na&#39;ven Enigma 10-Mar-2012 04:41
I was trying to figure out how to normalize an array with numerical keys.  Since I was doing for() for a lot of things, but only replacing it if the conditions were right, I wound up with off ball arrays I couldn't access.  That being said, I looked for a method of normalizing the array and couldn't find one, so I built my own.  I'm not sure how to go about making it recursive, but I didn't need that feature for my own, so I just went without recursion.

//This will take array([5] => "test1", [4] => "test2", [9] => "test3") into array([0] => "test1", [1] => "test2", [2] => "test3") so you can access it easier.
        function normalize_array($array){
          $newarray = array();
          $array_keys = array_keys($array);
          $i=0;
          foreach($array_keys as $key){
           $newarray[$i] = $array[$key];

          $i++;
          }
          return $newarray;
        }
qeremy [atta] gmail [dotta] com 06-Mar-2012 02:50
Simple ways to prefixing arrays;

<?php
function array_keys_prefix($arr, $pref = "") {
    $rarr = array();
    foreach ($arr as $key => $val) {
        $rarr[$pref.$key] = $val;
    }
    return $rarr;
}

function array_keys_prefix_multi($arr, $pref = "") {
    $rarr = array();
    foreach ($arr as $key => $val) {
        $rarr[] = array_keys_prefix($val, $pref);
    }
    return $rarr;
}

// $a = array("foo" => "FOO", "bar" => "BAR", "baz" => "BAZ"); // or
$a = array("foo" => "FOO", "bar" => "BAR", "baz" => array(1,2,3));
print_r(array_keys_prefix($a, "my_"));

// db fetch...
$products = array(
    array("id" => 1, "name" => "Foo"),
    array("id" => 2, "name" => "Bar")
);
print_r(array_keys_prefix_multi($products, "product_"));
?>

Array
(
    [my_foo] => FOO
    [my_bar] => BAR
    [my_baz] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)
Array
(
    [0] => Array
        (
            [product_id] => 1
            [product_name] => Foo
        )

    [1] => Array
        (
            [product_id] => 2
            [product_name] => Bar
        )

)
izzecold at live dot de 10-Jan-2012 06:21
<?php

/* A Function created by myself for checking multiple array keys
For Example u got an Array like $_SESSION and u wanna know if the keys 'user','pass','email' and 'type' exists.

*/

function mKeyChecker($arr,$keys=array()) {
    if(count($keys) > 1) {
        $valid_keys = 0;
        foreach($keys as $key) {
            if(array_key_exists($key,$arr)) $valid_keys++;
        }
        if($valid_keys == count($keys)) {
            return true;
        } else {
            return false;
        }
    } else if(count($keys) == 1) {
        if(array_key_exists($key[0],$arr)) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

// Execution Example

if(mKeyChecker($_SESSION,array('id','user','email','type'))) {
    echo "is!";
} else {
    echo "not!";
}

?>
craig1231 at hotmail dot com 22-Dec-2011 12:41
A needed a function to find the keys which contain part of a string, not equalling a string...

<?php
function array_keys_contain($input, $search_value, $strict = false)
    {
        $tmpkeys = array();

        $keys = array_keys($input);

        foreach ($keys as $k)
        {
            if ($strict && strpos($k, $search_value) !== FALSE)
                $tmpkeys[] = $k;
            elseif (!$strict && stripos($k, $search_value) !== FALSE)
                $tmpkeys[] = $k;
        }

        return $tmpkeys;
    }
?>
el dot quick at gmail dot com 23-Sep-2011 05:23
Sorry for my english...

I wrote a function to get keys of arrays recursivelly...

<?php
    function recursive_keys($input, $search_value = null){

        $output = ($search_value !== null ? array_keys($input, $search_value) : array_keys($input)) ;
        foreach($input as $sub){
            if(is_array($sub)){
                $output = ($search_value !== null ? array_merge($output, recursive_keys($sub, $search_value)) : array_merge($output, recursive_keys($sub))) ;
            }
        }
        return $output ;
    }
?>

I hope it will be usefull

Regards
pat dot leblanc at gmail dot com 29-Aug-2011 11:05
It's worth noting that if you have keys that are long integer, such as '329462291595', they will be considered as such on a 64bits system, but will be of type string on a 32 bits system.

for example:
<?php

$importantKeys = array('329462291595' =>null, 'ZZ291595' => null);

foreach(array_keys($importantKeys) as $key){
    echo gettype($key)."\n";
}

?>

will return on a 64 bits system:
<?php
    integer
    string
?>

but on a 32 bits system:
<?php
    string
    string
?>

I hope it will save someone the huge headache I had :)
ferrerna at gmail dot com 23-Jan-2009 06:14
Here's a function I needed to collapse an array, in my case from a database query.  It takes an array that contains key-value pairs and returns an array where they are actually the key and value.

<?php

function array_collapse($arr, $x, $y) {
    $carr = array();
    while ($el = current($arr)) {
        $carr[ $el[$x] ] = $el[$y];
        next($arr);
    }
    return $carr;
}

?>

Example usage (pseudo-database code):

<?php

$query = db_query('SELECT name, value FROM properties');

$result = db_returnAll($query);

/* This will return an array like so:

[
   ['name' -> 'color', 'value' -> 'blue'],
   ['name' -> 'style', 'value' -> 'wide-format'],
   ['name' -> 'weight', 'value' -> 3.6],
   ['name' -> 'name', 'value' -> 'Waerdthing']
]

*/

$propArr = array_collapse($result, 'name', 'value');

/* Now this array looks like:

[
   ['color' -> 'blue'],
   ['style' -> 'wide-format'],
   ['weight' -> 3.6],
   ['name' -> 'Waerdthing'],

*/

?>

I found this handy for using with json_encode and am using it for my project http://squidby.com
nodarinodo at mail dot ru 03-Dec-2008 08:35
<?php
    //It's a way to get keys from values )
    $es = array("is My FullName"=>"nodar chkuaselidze (nodarinodo)", "You Are" => "I don't know", "Is My Friend" => "ruxadze");
    foreach(array_values($es) as $ess){
        echo $ess." =>";

    for($i =0; $i < count(array_keys($es, $ess)); $i++){
            echo reset(array_keys($es, $ess))."<BR>";
        } }
?>
neil at 11 out of 10 24-Jul-2008 09:10
<?php

/*
 * This function will return the keys of elements in the
 * haystack where the value is found in array needle
 */

function array_value_intersect_keys( $array_haystack, $array_needle ){
    $intersected = array_intersect( $array_haystack, $array_needle );
    return array_keys( $intersected );
}

// usage

$array_haystack = array( 1 => 2, 2 => 5, 'red' => 8, 9 => 14 );

$array_needle = array( 2, 8 );

$array_keys_of_intersecting_values = array_value_intersect_keys( $array_haystack, $array_needle );

print_r( $array_keys_of_intersecting_values );
?>

returns
Array
(
    [0] => 1
    [1] => red
)
Hayley Watson 05-Oct-2007 03:43
An alternative to RQuadling at GMail dot com's array_remove() function:

<?php
function array_remove(array $array, $value, $strict=false)
{
    return array_diff_key($array, array_flip(array_keys($array, $value, $strict)));
}
?>
creator at mindcreations dot com 20-Mar-2007 11:12
This function will extract keys from a multidimensional array

<?php
function multiarray_keys($ar) {

    foreach($ar as $k => $v) {
        $keys[] = $k;
        if (is_array($ar[$k]))
            $keys = array_merge($keys, multiarray_keys($ar[$k]));
    }
    return $keys;
}
?>

Example code:

<?php
$array = array("color" => array("1stcolor" => "blue", "2ndcolor" => "red", "3rdcolor" => "green"),
               "size"  => array("small", "medium", "large"));

echo "<pre>";
print_r($array);
echo "</pre>";

echo "<pre>";
print_r(multiarray_keys($array));
echo "</pre>";
?>

Example output:

Array
(
    [color] => Array
        (
            [1stcolor] => blue
            [2ndcolor] => red
            [3rdcolor] => green
        )

    [size] => Array
        (
            [0] => small
            [1] => medium
            [2] => large
        )

)

Array
(
    [0] => color
    [1] => 1stcolor
    [2] => 2ndcolor
    [3] => 3rdcolor
    [4] => size
    [5] => 0
    [6] => 1
    [7] => 2
)
jochem 18-Feb-2006 01:13
might be worth noting in the docs that not all associative (string) keys are a like, output of the follow bit of code demonstrates - might be a handy introduction to automatic typecasting in php for some people (and save a few headaches):

<?php
$r = array("0"=>"0","1"=>"1","" =>"2"," "=>"3");
echo 'how php sees this array: array("0"=>"0","1"=>"1","" =>"2"," "=>"3")',"\n-----------\n";
var_dump($r); print_r($r); var_export($r);
echo "\n-----------\n",'var_dump("0","1",""," ") = ',"\n-----------\n";
var_dump("0","1",""," ");
?>

OUTPUTS:

how php sees this array: array("0"=>"0","1"=>"1","" =>"2"," "=>"3")
-----------
array(4) {
  [0]=>
  string(1) "0"
  [1]=>
  string(1) "1"
  [""]=>
  string(1) "2"
  [" "]=>
  string(1) "3"
}
Array
(
    [0] => 0
    [1] => 1
    [] => 2
    [ ] => 3
)
array (
  0 => '0',
  1 => '1',
  '' => '2',
  ' ' => '3',
)
-----------
var_dump("0","1",""," ") =
-----------
string(1) "0"
string(1) "1"
string(0) ""
string(1) " "
Sven (bitcetera.com) 19-Dec-2005 03:43
Here's how to get the first key, the last key, the first value or the last value of a (hash) array without explicitly copying nor altering the original array:

<?php
  $array = array('first'=>'111', 'second'=>'222', 'third'=>'333');

  // get the first key: returns 'first'
  print array_shift(array_keys($array));

  // get the last key: returns 'third'
  print array_pop(array_keys($array));

  // get the first value: returns '111'
  print array_shift(array_values($array));

  // get the last value: returns '333'
  print array_pop(array_values($array));
?>
vesely at tana dot it 09-Dec-2005 10:56
The position of an element.

One can apply array_keys twice to get the position of an element from its key. (This is the reverse of the function by cristianDOTzuddas.) E.g., the following may output "yes, we have bananas at position 0".

<?php
$a = array("banana" => "yellow", "apple" = "red");
$k = get_some_fruit();
if (isset($a[$k]))
{
   list($pos) = array_keys(array_keys($a), $k);
   print "yes, we have {$k}s at position $pos\n";
}
?>

Not amazingly efficient, but I see no better alternative.
ru dot dy at gmx dot net 14-Aug-2005 06:20
I was looking for a function that simply unset a variable amout of values from a one-dimensional array by key. I ended up with this (returns the array itself if no further parameter than the array is given, false with no params - does not change the source array)

usage: array_remove(array $input [, mixed key ...])

<?php

  function array_remove() {
    if ($stack = func_get_args()) {
      $input = array_shift($stack);
      foreach ($stack as $key) {
        unset($input[$key]);
      }
      return $input;
    }
    return false;
  }

?>

Test:

<?php
  $a = array('a'=>'fun', 'b'=>3.14, 'sub'=> array('1', '2', '3'), 'd'=>'what', 'e' => 'xample', 5 => 'x');
  print_r($a);
  print_r(array_remove($a, 'd', 'b', 5, 'sub'));
?>

Output:

Array
(
    [a] => fun
    [b] => 3.14
    [sub] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [d] => what
    [e] => xample
    [5] => x
)
Array
(
    [a] => fun
    [e] => xample
)

Hope this helps someone.
webmaster [at] baz-x [dot] at 30-Jul-2005 12:43
I was looking for a function that deletes either integer keys or string keys (needed for my caching).
As I didn't find a function I came up with my own solution.
I didn't find the propiest function to post to so I will post it here, hope you find it useful.

<?php

function array_extract($array, $extract_type = 1)
{
    foreach ( $array as $key => $value )
    {
        if ( $extract_type == 1 && is_string($key) )
        {
            // delete string keys
            unset($array[$key]);
        }
        elseif ( $extract_type == 2 && is_int($key) )
        {
            // delete integer keys
            unset($array[$key]);
        }
    }

    return $array;
}

?>

You can of course define constants to have a nicer look, I have chosen these: EXTR_INT = 1; EXTR_STRING = 2
EXTR_INT will return an array where keys are only integer while
EXTR_STRING will return an array where keys are only string

Have fun with it.
sip at email dot ee 22-Aug-2003 02:33
Note, that using array_key_exists() is rather inefficient. The overhead associated with calling a function makes it slower, than using isset($array[$key]), instead of array_key_exists($key, $array)
using isset() is usually about 1.3 times faster, according to my tests.
rodrigo at NOSPAM dot dhweb dot com dot br 05-Feb-2003 01:39
[Editor's note: For a complete solution to the printing of complex structures or hashes, see the PEAR::Var_Dump package: http://pear.php.net/package-info.php?pacid=103 , use "pear install Var_Dump" to get it]

This function will print all the keys of a multidimensional array in html tables.
It will help to debug when you don?t have control of depths.

<?php
function show_keys($ar){

   echo "<table width='100%' border='1' bordercolor='#6699CC' cellspacing='0' cellpadding='5'><tr valign='top'>";

      foreach ($ar as $k => $v ) {

         echo "<td align='center' bgcolor='#EEEEEE'>
           <table border='2' cellpadding='3'><tr><td bgcolor='#FFFFFF'><font face='verdana' size='1'>
              " . $k . "
           </font></td></tr></table>";

           if (is_array($ar[$k])) {
              show_keys ($ar[$k]);
         }

         echo "</td>";

      }

   echo "</tr></table>";

}

// Multidimensional array ->
$arvore = array();
$arvore['1'] = array();
$arvore['1']['1.1'] = array('1.1.1', '1.1.2', '1.1.3');
$arvore['1']['1.2'] = array('1.2.1', '1.2.2', '1.2.3');
$arvore['1']['1.3'] = array('1.3.1', '1.3.2', '1.3.3');
$arvore['2'] = array();
$arvore['2']['2.1'] = array('2.1.1', '2.1.2', '2.1.3');
$arvore['2']['2.2'] = array('2.2.1', '2.2.2', '2.2.3');
$arvore['2']['2.3'] = array('2.3.1', '2.3.2', '2.3.3');
$arvore['3'] = array();
$arvore['3']['3.1'] = array('3.1.1', '3.1.2', '3.1.3');
$arvore['3']['3.2'] = array('3.2.1', '3.2.2', '3.2.3');
$arvore['3']['3.3'] = array('3.3.1', '3.3.2'=>array('3.3.2.1', '3.3.2.2'), '3.3.3');
// <-

show_keys($arvore);
?>
glennh at webadept dot net 13-Nov-2002 01:03
All the cool notes are gone from the site.

Here's an example of how to get all the variables passed to your program using the method on this page. This prints them out so you can see what you are doing.

<?php
while (list($key, $value) = each
(${"HTTP_".$REQUEST_METHOD."_VARS"}))
{
        echo $key." = ".$value." ";
}
?>


Applies the callback to the elements of the given arrays

  • ≪ array_keys
  • array_merge_recursive ≫
  • PHP Manual
  • 배열 함수 목록
  • Applies the callback to the elements of the given arrays

array_map

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

array_map — Applies the callback to the elements of the given arrays

설명

array array_map ( callable $callback , array $array1 [, array $... ] )

array_map() returns an array containing all the elements of array1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()

인수

callback

Callback function to run for each element in each array.

array1

An array to run through the callback function.

...

Variable list of array arguments to run through the callback function.

반환값

Returns an array containing all the elements of array1 after applying the callback function to each one.

예제

Example #1 array_map() example

<?php
function cube($n)
{
    return($n * $n * $n);
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>

This makes $b have:

Array
(
    [0] => 1
    [1] => 8
    [2] => 27
    [3] => 64
    [4] => 125
)

Example #2 array_map() using a lambda function (as of PHP 5.3.0)

<?php
$func = function($value) {
    return $value * 2;
};

print_r(array_map($func, range(1, 5)));
?>
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
    [4] => 10
)

Example #3 array_map() - using more arrays

<?php
function show_Spanish($n, $m)
{
    return("The number $n is called $m in Spanish");
}

function map_Spanish($n, $m)
{
    return(array($n => $m));
}

$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");

$c = array_map("show_Spanish", $a, $b);
print_r($c);

$d = array_map("map_Spanish", $a , $b);
print_r($d);
?>

위 예제의 출력:

// printout of $c
Array
(
    [0] => The number 1 is called uno in Spanish
    [1] => The number 2 is called dos in Spanish
    [2] => The number 3 is called tres in Spanish
    [3] => The number 4 is called cuatro in Spanish
    [4] => The number 5 is called cinco in Spanish
)

// printout of $d
Array
(
    [0] => Array
        (
            [1] => uno
        )

    [1] => Array
        (
            [2] => dos
        )

    [2] => Array
        (
            [3] => tres
        )

    [3] => Array
        (
            [4] => cuatro
        )

    [4] => Array
        (
            [5] => cinco
        )

)

Usually when using two or more arrays, they should be of equal length because the callback function is applied in parallel to the corresponding elements. If the arrays are of unequal length, shorter ones will be extended with empty elements to match the length of the longest.

An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function

Example #4 Creating an array of arrays

<?php
$a = array(1, 2, 3, 4, 5);
$b = array("one", "two", "three", "four", "five");
$c = array("uno", "dos", "tres", "cuatro", "cinco");

$d = array_map(null, $a, $b, $c);
print_r($d);
?>

위 예제의 출력:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => one
            [2] => uno
        )

    [1] => Array
        (
            [0] => 2
            [1] => two
            [2] => dos
        )

    [2] => Array
        (
            [0] => 3
            [1] => three
            [2] => tres
        )

    [3] => Array
        (
            [0] => 4
            [1] => four
            [2] => cuatro
        )

    [4] => Array
        (
            [0] => 5
            [1] => five
            [2] => cinco
        )

)

The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys.

Example #5 array_map() - with string keys

<?php
$arr = array("stringkey" => "value");
function cb1($a) {
    return array ($a);
}
function cb2($a, $b) {
    return array ($a, $b);
}
var_dump(array_map("cb1", $arr));
var_dump(array_map("cb2", $arr, $arr));
var_dump(array_map(null,  $arr));
var_dump(array_map(null, $arr, $arr));
?>

위 예제의 출력:

array(1) {
  ["stringkey"]=>
  array(1) {
    [0]=>
    string(5) "value"
  }
}
array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(5) "value"
    [1]=>
    string(5) "value"
  }
}
array(1) {
  ["stringkey"]=>
  string(5) "value"
}
array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(5) "value"
    [1]=>
    string(5) "value"
  }
}

참고

  • array_filter() - 콜백 함수를 사용하여 배열 원소를 필터
  • array_reduce() - 콜백 함수를 사용하여 배열을 반복적으로 단일 값으로 축소
  • array_walk() - 배열의 각 원소에 대해서 특정 함수를 적용
  • callback형에 대한 정보

User Contributed Notes

mcmurphy510 at gmail dot com 20-May-2016 12:43
If you are using Namespaces, the array_map callback expects the Fully Qualified NameSpace to be used.

For example, this won't work:
<?php
namespace Test;
function mapping_function($var)  {
    ...
}
array_map('mapping_function', $array);  //won't find 'mapping_function' here.
?>

This, however, will work:
<?php
namespace Test;
function mapping_function($var)  {
    ...
}
array_map('Test\mapping_function', $array);  //using FQNS.
?>
shabeerksbr at hotmail dot com 23-Oct-2015 04:50
Array
(
    [result] => Array
        (
            [0] => Array
                (
                    [child] => Array
                        (
                            [5629378c2bad8cac1900002a] => Array
                                (
                                    [_id] => MongoId Object
                                        (
                                            [$id] => 5629378c2bad8cac19000029
                                        )

                                    [name] => Category 1
                                    [hasChild] => 0
                                )

                            [562937c02bad8ca41900002a] => Array
                                (
                                    [_id] => MongoId Object
                                        (
                                            [$id] => 562937c02bad8ca419000029
                                        )

                                    [name] => Category 1
                                    [hasChild] => 0
                                )

                            [562937dd2bad8c9c1900002b] => Array
                                (
                                    [_id] => MongoId Object
                                        (
                                            [$id] => 562937dd2bad8c9c1900002a
                                        )

                                    [name] => Category 1
                                    [hasChild] => 0
                                )

                        )

                )

        )

    [ok] => 1
)

Trim above code neatly using
print_r(array_map(null, $result['result'][0]['child']));

Array
(
    [5629378c2bad8cac1900002a] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 5629378c2bad8cac19000029
                )

            [name] => Category 1
            [hasChild] => 0
        )

    [562937c02bad8ca41900002a] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 562937c02bad8ca419000029
                )

            [name] => Category 1
            [hasChild] => 0
        )

    [562937dd2bad8c9c1900002b] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 562937dd2bad8c9c1900002a
                )

            [name] => Category 1
            [hasChild] => 0
        )

)
Mahn 21-Oct-2015 02:10
You may be looking for a method to extract values of a multidimensional array on a conditional basis (i.e. a mixture between array_map and array_filter) other than a for/foreach loop. If so, you can take advantage of the fact that 1) the callback method on array_map returns null if no explicit return value is specified (as with everything else) and 2) array_filter with no arguments removes falsy values.

So for example, provided you have:

<?php
$data = [
    [
        "name" => "John",
        "smoker" => false
    ],
    [
        "name" => "Mary",
        "smoker" => true
    ],
    [
        "name" => "Peter",
        "smoker" => false
    ],
    [
        "name" => "Tony",
        "smoker" => true
    ]
];
?>

You can extract the names of all the non-smokers with the following one-liner:

<?php
$names = array_filter(array_map(function($n) { if(!$n['smoker']) return $n['name']; }, $data));
?>

It's not necessarily better than a for/foreach loop, but the occasional one-liner for trivial tasks can help keep your code cleaner.
info at katoba dot de 02-Oct-2015 09:08
Maybe usefull: replace a string in all array-elements
<?php
$hayStack = ["Book :id","Car :id","Cat :id"];

$replaced = array_map(function($element) use ($replace){
            return str_replace(":id",$replace,$element);
        },$hayStack);

print_r($replaced);

?>
nelf86 at gmail dot com 13-Aug-2015 08:10
Here is how to perform an operation on some of the elements of an array:

<?php
$an_array = array(
    'item1' => 0,
    'item2' => 0,
    'item3' => 0,
    'item4' => 0,
    'item5' => 0,
);

$items_to_modify = array('item1', "item3");

 array_map(function ($value) use (&$an_array ) {
     $an_array [$value] = (boolean)$an_array [$value];   //example operation:
 }, $items_to_modify);

?>

This will take the original array and perform an action only on items specified on the second array items. Use of & symbol in the use statement makes the array_map access the variable as a reference in an outer scope.

This makes code easily extendable.
CertaiN 25-Mar-2015 02:15
Simplest array_map_recursive() implemention.

<?php
function array_map_recursive(callable $func, array $array) {
    return filter_var($array, \FILTER_CALLBACK, ['options' => $func]);
}
?>
mark at grooveshark dot com 20-Jan-2015 01:44
If you're looking for a way to get a specific set of key values (ala "pluck") you can just use array_column([['id' => 1]], 'id').
wapinet at mail dot ru 15-Jul-2014 10:54
PHP 5.5.14
<?php
$columns = range(1, 100000);
$time = microtime(true);

/*
0.067003011703491 sec
24903680 byte
 */
array_map(function ($value) {
    return 'p.' . $value;
}, $columns);

/*
0.042001962661743 sec
19398656 byte
 */
foreach ($columns as $key => $column) {
    $columns[$key] = 'p.' . $column;
}

/*
0.05500316619873 sec
10747904 byte
 */
array_walk($columns, function (&$value, $key) {
    $value = 'p.' . $value;
});

/*
0.0260009765625 sec
10747904 byte
 */
foreach ($columns as &$column) {
    $column = 'p.' . $column;
}

echo microtime(true) - $time . "\n" . memory_get_peak_usage(true) . "\n";
JacobA dot Barber at yahoo dot com 08-May-2014 01:27
Here is an example for php 5.4 and greater, using a closure:
<?php
$myList = [1,2,3,4];

$mappedList = array_map(function($value){
    return $value * 2;
}, $myList);

var_dump($mappedList);

array(4) {
  [0]=>int(2)
  [1]=>int(4)
  [2]=>int(6)
  [3]=>int(8)
}
?>
This is useful when you don't want/need to define a function just so it can be used with the map function.
ian_channing at hotmail dot com 12-Feb-2014 08:10
I wanted a nice way to convert an array with x values as keys and y values as values e.g. array(x => y,...) into x,y co-ordinates e.g. array(array(x,y), ...).

You can do:

<?php
foreach ($waveform as $key => $waveformPoint) {
    $coordinates[] = array($key, $waveformPoint);
}
?>

But array_map with a null callback gives you a nicer way:

<?php
$coordinates = array_map(null, array_keys($waveform), array_values($waveform));
?>
williamprogphp at yahoo dot com dot br 31-Jan-2014 09:54
A function to map an array working also with its key.

<?php
    function array_mapk($callback, $array) {
        $newArray = array();
        foreach ($array as $k => $v) {
            $newArray[$k] = call_user_func($callback, $k, $v);
        }
        return $newArray;
    }
?>

Usage:

<?php
    $dados = array();
    $dados['cep'] = "32340-070";
    $dados['initial'] = 100;
    $dados['final'] = 300;
    $dados['processed'] = 1;

    echo "update table set " . implode(", ", array_mapk(function ($k, $v) { return "{$k} = '" . mysql_real_escape_string($v) . "'"; }, $dados));
?>

Cheers.
elfe1021 at gmail dot com 13-Dec-2013 11:44
Find an interesting thing that in array_map's callable function, late static binding does not work:
<?php
class A {
    public static function foo($name) {
        return 'In A: '.$name;
    }

    public static function test($names) {
        return array_map(function($n) {return static::foo($n);}, $names);
    }
}

class B extends A{
    public static function foo($name) {
        return 'In B: '.$name;
    }
}

$result = B::test(['alice', 'bob']);
var_dump($result);
?>

the result is:
array (size=2)
  0 => string 'In A: alice' (length=11)
  1 => string 'In A: bob' (length=9)

if I change A::test to
<?php
    public static function test($names) {
        return array_map([get_called_class(), 'foo'], $names);
    }
?>

Then the result is as expected:
array (size=2)
  0 => string 'In B: alice' (length=11)
  1 => string 'In B: bob' (length=9)
lukasz dot mordawski at gmail dot com 03-Dec-2013 07:55
Let's assume we have following situation:

<?php
class MyFilterClass {
    public function filter(array $arr) {
        return array_map(function($value) {
            return $this->privateFilterMethod($value);
        });
    }

    private function privateFilterMethod($value) {
        if (is_numeric($value)) $value++;
        else $value .= '.';
    }
}
?>

This will work, because $this inside anonymous function (unlike for example javascript) is the instance of MyFilterClass inside which we called it.
I hope this would be useful for anyone.
CertaiN 23-Aug-2013 05:52
The most memory-efficient array_map_recursive().

<?php
function array_map_recursive(callable $func, array $arr) {
    array_walk_recursive($arr, function(&$v) use ($func) {
        $v = $func($v);
    });
    return $arr;
}
?>
contato at williamsantana dot com dot br 30-Jul-2013 09:42
In case of you need to recursively bypass a function over the itens of an array, you can use it

<?php
    function array_map_recursive($callback, $array) {
        foreach ($array as $key => $value) {
            if (is_array($array[$key])) {
                $array[$key] = array_map_recursive($callback, $array[$key]);
            }
            else {
                $array[$key] = call_user_func($callback, $array[$key]);
            }
        }
        return $array;
    }
?>

-----------------------------------------------------------------------

<?php
    $strings = array(
        'The',
        array(
            'quick',
            'fox',
            array(
                'brown',
                'jumps',
                array(
                    'over',
                    array(
                        'the',
                        array(
                            'lazy',
                            array(
                                'dog'
                            )
                        )
                    )
                )
            )
        )
    );

    print_r($strings);
    $hashedString = array_map_recursive('md5', $strings);
    print_r($hashedString);
?>

------------------------------------------------------------------------
Testing it, you'll obtain

<?php

/* Original array */

array (
  0 => 'The',
  1 =>
  array (
    0 => 'quick',
    1 => 'fox',
    2 =>
    array (
      0 => 'brown',
      1 => 'jumps',
      2 =>
      array (
        0 => 'over',
        1 =>
        array (
          0 => 'the',
          1 =>
          array (
            0 => 'lazy',
            1 =>
            array (
              0 => 'dog',
            ),
          ),
        ),
      ),
    ),
  ),
);

/* Recursived array */
array (
  0 => 'a4704fd35f0308287f2937ba3eccf5fe',
  1 =>
  array (
    0 => '1df3746a4728276afdc24f828186f73a',
    1 => '2b95d1f09b8b66c5c43622a4d9ec9a04',
    2 =>
    array (
      0 => '6ff47afa5dc7daa42cc705a03fca8a9b',
      1 => '55947829059f255e4ba2f536a2ae99fe',
      2 =>
      array (
        0 => '3b759a9ca80234563d87672350659b2b',
        1 =>
        array (
          0 => '8fc42c6ddf9966db3b09e84365034357',
          1 =>
          array (
            0 => '0ffe34b4e04c2b282c5a388b1ad8aa7a',
            1 =>
            array (
              0 => '06d80eb0c50b49a509b49f2424e8c805',
            ),
          ),
        ),
      ),
    ),
  ),
);

?>

Hope it helps you.

Cheers.
jessiedeer at hotmail dot com 16-Jul-2013 07:34
array_map becomes interesting and faster than foreach when used with existing PHP functions.

Example:

$arr1 = array(1, 2, 3, 4);
$arr2 = array(4, 3, 2, 1);

// array with min values for each key
print_r(array_map("min", $arr1, $arr2));

Result: Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 1 )

// array with max values for each key
print_r(array_map("max", $arr1, $arr2));

Result: Array ( [0] => 4 [1] => 3 [2] => 3 [3] => 4 )
jessiedeer at hotmail dot com 16-Jul-2013 07:27
array_map becomes interesting and faster than foreach when used with existing PHP functions.

Example:

$arr1 = array(1, 2, 3, 4);
$arr2 = array(4, 3, 2, 1);

// array with min values for each key
print_r(array_map("min", $arr1, $arr2));

Result: Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 1 )

// array with max values for each key
print_r(array_map("max", $arr1, $arr2));

Result: Array ( [0] => 4 [1] => 3 [2] => 3 [3] => 4 )
Anonymous 06-Jul-2013 10:21
Simple yet effective associative version of map that accepts arbitrary number of arrays.
Instead of elements, result from each() get passed as function arguments.

<?php
function array_map_assoc(){
    if(func_num_args() < 2) throw new \BadFuncionCallException('Missing parameters');
    $args = func_get_args();
    $callback = $args[0];
    if(!is_callable($callback)) throw new \InvalidArgumentException('First parameter musst be callable');
    $arrays = array_slice($args, 1);
    array_walk($arrays, function(&$a){
        $a = (array)$a;
        reset($a);
    });
    $results = array();
    $max_length = max(array_map('count', $arrays));
    $arrays = array_map(function($pole) use ($max_length){
        return array_pad($pole, $max_length, null);
    }, $arrays);
    for($i=0; $i < $max_length; $i++){
        $elements = array();
        foreach($arrays as &$v){
            $elements[] = each($v);
        }
        unset($v);
        $out = call_user_func_array($callback, $elements);
        if($out === null) continue;
        $val = isset($out[1]) ? $out[1] : null;
        if(isset($out[0])){
            $results[$out[0]] = $val;
        }else{
            $results[] = $val;
        }
    }
    return $results;
}

//USAGE:
$days = array('Sun' => 'Sunday', 'Mon' => 'Monday', 'Tue' => 'Tuesday', 'Wed' => 'Wednesday', 'Thu' => 'Thursday', 'Fri' => 'Friday', 'Sat' => 'Saturday');
$nums = array('First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh');

$result = array_map_assoc(function($a, $b){
    return array($b['value'], $a['key']);
}, $days, $nums);

var_dump($result);

//RESULT:
array (size=7)
  'First' => string 'Sun' (length=3)
  'Second' => string 'Mon' (length=3)
  'Third' => string 'Tue' (length=3)
  'Fourth' => string 'Wed' (length=3)
  'Fifth' => string 'Thu' (length=3)
  'Sixth' => string 'Fri' (length=3)
  'Seventh' => string 'Sat' (length=3)
hrvoj3e at gmail dot com 30-Apr-2013 11:38
Why not use

mb_convert_case($str, MB_CASE_TITLE, "UTF-8");

Works for me! :)
James 16-Mar-2013 02:07
array_map() can be applied to assoc arrays without affecting the keys
shakespeare32 at gmail dot com 20-Feb-2013 04:01
Why not an array of callbacks?

<?php
function array_maps($callbacks, $array) {
    if (!$callbacks) { return $array; }

    if (!is_array($callbacks) && is_string($callbacks) && function_exists($callbacks)) {
        return array_map($callbacks, $array);
    }

    foreach($callbacks as $callback) {
        if (function_exists($callback)) {
        $array = array_map($callback, $array);
        }
    }

    return $array;
}
?>
godek dot maciek at gmail dot com 19-Apr-2012 04:01
I came up with a convenient syntax for method application, particularly useful inside the array_map.
For instance, if you want to
array_map(function($object, $arg1) { return $object->method($arg1, "arg2"); }, $objects, $args1);

it is possible to provide a shorthand:
global $_; // necessary inside a function, unfortunately

array_map($_->method($_, "arg2"), $objects, $args1);

Here's the implementation (works with 5.3)

function m_caller($method) {
  $args = func_get_args();
  array_shift($args);
  return function($object) use($method, $args) {
    global $_;
    $local_args = func_get_args();
    array_shift($args);
    if(!is_object($object)) {
      // error
    }
    $reflection = new ReflectionMethod(get_class($object), $method);
    foreach($args as $key => $arg) {
      if($arg === $_) {
        $args[$key] = array_shift($local_args);
      }
    }
    return $reflection->invokeArgs($object, array_merge($args, $local_args));
  };
}

class MethodCaller {
  public function __call($name, $arguments) { return call_user_func_array('m_caller', array_merge(array($name), $arguments)); }
  public function __get($name) { return m_caller($name); }
};

$_ = new MethodCaller();
qeremy 07-Mar-2012 07:35
An alternative for recursive mapping;

<?php
function array_map_recursive($fn, $arr) {
    $rarr = array();
    foreach ($arr as $k => $v) {
        $rarr[$k] = is_array($v)
            ? array_map_recursive($fn, $v)
            : $fn($v); // or call_user_func($fn, $v)
    }
    return $rarr;
}

function sqr($x) {
    return "$x ^ 2 = ". ($x * $x);
}

$a = array(1, 2, 3, array(4, array(5)));
$b = array_map_recursive("sqr", $a);
print_r($b);
?>

Array
(
    [0] => 1 ^ 2 = 1
    [1] => 2 ^ 2 = 4
    [2] => 3 ^ 2 = 9
    [3] => Array
        (
            [0] => 4 ^ 2 = 16
            [1] => Array
                (
                    [0] => 5 ^ 2 = 25
                )

        )

)
gmail.com@mspreij 27-Feb-2012 07:48
Hope I'm not late to the party, here's my function to apply array_map to the *keys* of an array.
Extra array arguments will be used for the callback function's parameters just like with array_map, with the difference that a string is also allowed: it will just be used to create an array of appropriate length with as each value that string. Arrays are left alone (and will be padded with nulls by array_map as needed).

<?php

//_________________________________________________
// array_map_keys($callback, $array, [$args, ..]) /
function array_map_keys($callback, $array /* [, $args ..] */) {
  $args = func_get_args();
  if (! is_callable($callback)) trigger_error("first argument (callback) is not a valid function", E_USER_ERROR);
  if (! is_array($array)) trigger_error("second argument must be an array", E_USER_ERROR);
  $args[1] = array_keys($array);
  // If any additional arguments are not arrays, assume that value is wanted for every $array item.
  // array_map() will pad shorter arrays with Null values
  for ($i=2; $i < count($args); $i++) {
    if (! is_array($args[$i])) {
      $args[$i] = array_fill(0, count($array), $args[$i]);
    }
  }
  return array_combine(call_user_func_array('array_map', $args), $array);
}

// Some examples:

$arr = array('foo'=>123, 'bar'=>456);

// simply uppercase keys:
var_dump(array_map_keys('strtoupper', $arr));
// or..
var_dump(array_map_keys(function($input) {return strtoupper($input);}, $arr));
// >> array(2) { ["FOO"]=>int(123) , ["BAR"]=> int(456) }

// Add a prefix 'myvar_':
var_dump(array_map_keys(function($input, $prefix) {return $prefix.$input;}, $arr, 'myvar_'));
// >> array(2) { ["myvar_foo"]=>int(123) , ["myvar_bar"]=>int(456) }

// Apart from the (static string) prefix, we also number them:
$arr = array('foo'=>123, 'bar'=>456, 'bazz'=>789, 'yadda'=>'0AB');
var_dump(array_map_keys(function($input, $middle, $number) {return $number.':'.$middle.$input;}, $arr, 'myvar_', range(1, count($arr))));
// >> array(4) { ["1:myvar_foo"]=>int(123) , ["2:myvar_bar"]=>int(456) , ["3:myvar_bazz"]=>int(789) , ["4:myvar_yadda"]=>string(3) "0AB" }

?>
php/hotblocks/nl 28-Nov-2011 02:00
Note that the $arr argument has to be an array, not just a Traversable/Iterator.

For instance this won't work:

<?php

$documents = $mongo->db->collection->find();
// $documents is Traversable by foreach

$ids = array_map(function($document) {
  return $document['_id'];
}, $objects);
// $ids will now be NULL, because $documents wasn't an Array

?>

A solution is to first use iterator_to_array():

<?php

$ids = array_map(function($document) {
  return $document['_id'];
}, iterator_to_array($objects));
// $ids will now be an array of ['_id']s

?>

But this is not very efficient: two cycles instead of one. Another solution is to use foreach: one cycle and a lot of freedom (and in the same scope).
gordon dot mcvey at ntlworld dot com 28-Jan-2011 02:11
You can use array_map with PHP native functions as well as user functions.  This is very handy if you need to sanitize arrays.

<?php

$integers = array_map ('intval', $integers);
$safeStrings = array_map ('mysql_real_escape_string', $unsafeStrings);

?>
virtual dot greg at gmail dot com 05-Mar-2010 12:07
PHP 5.3 enables us to use inline anonymous functions with array_map, cleaning up the syntax slightly.

<?php
$data = array(
        array('id' => 1, 'name' => 'Bob', 'position' => 'Clerk'),
        array('id' => 2, 'name' => 'Alan', 'position' => 'Manager'),
        array('id' => 3, 'name' => 'James', 'position' => 'Director')
);

$names = array_map(
        function($person) { return $person['name']; },
        $data
);

print_r($names);
?>

This was possible (although not recommended) in prior versions of PHP 5, via create_function().

<?php
$names = array_map(
        create_function('$person', 'return $person["name"];'),
        $data
);
?>

You're less likely to catch errors in the latter version because the code is passed as string arguments.

These are alternatives to using a foreach:

<?php
$names = array();

foreach ($data as $row) {
        $names[] = $row['name'];
}
?>
kelly m 17-Feb-2010 11:28
I realize this function is easy enough to make, but this is a faster version (twice the speed) of [a function] which I find incredibly useful.

<?php
function array_pluck($key, $input) {
    if (is_array($key) || !is_array($input)) return array();
    $array = array();
    foreach($input as $v) {
        if(array_key_exists($key, $v)) $array[]=$v[$key];
    }
    return $array;
}
?>

Usage:

<?php $ids = array_pluck('id', $users); ?>
onassar at gmail dot com 14-Oct-2009 04:06
Fixed a bug with array recursion.

<?php
        /**
         * arrayMap function. Customized array_map function which preserves keys/associate array indexes. Note that this costs a descent amount more memory (eg. 1.5k per call)
         *
         * @access public
         * @param callback $callback Callback function to run for each element in each array.
         * @param mixed $arr1 An array to run through the callback function.
         * @param array $array Variable list of array arugments to run through the callback function.
         * @return array Array containing all the elements of $arr1 after applying the callback function to each one, recursively, maintain keys.
         */
        function arrayMap($callback,$arr1) {
            $results       =    array();
            $args          =    array();
            if(func_num_args()>2)
                $args          =    (array) array_shift(array_slice(func_get_args(),2));
            foreach($arr1 as $key=>$value) {
                $temp    =    $args;
                array_unshift($temp,$value);
                if(is_array($value)) {
                    array_unshift($temp,$callback);
                    $results[$key]    =    call_user_func_array(array('self','arrayMap'),$temp);
                } else {
                    $results[$key]    =    call_user_func_array($callback,$temp);
                }
            }
            return $results;
        }
?>
onassar at gmail dot com 12-Oct-2009 12:33
Wrote up my own key preservation function for array mapping. It allows n arguments to be passed, and should be easy enough to follow if you need to make any mods. If you've got any thoughts let me know.

<?php
        /**
         * arrayMap function. Customized array_map function which preserves keys/associate array indexes.
         *
         * @access public
         * @param callback $callback Callback function to run for each element in each array.
         * @param mixed $arr1 An array to run through the callback function.
         * @param array $array Variable list of array arugments to run through the callback function.
         * @return array Array containing all the elements of $arr1 after applying the callback function to each one, recursively, maintain keys.
         */
        function arrayMap($callback,$arr1) {
            $results       =    array();
            $args          =    array();
            if(func_num_args()>2)
                $args          =    (array) array_shift(array_slice(func_get_args(),2));
            foreach($arr1 as $key=>$value) {
                if(is_array($value)) {
                    array_unshift($args,$value);
                    array_unshift($args,$callback);
                    $results[$key]    =    call_user_func_array(array('self','arrayMap'),$args);
                }
                else {
                    array_unshift($args,$value);
                    $results[$key]    =    call_user_func_array($callback,$args);
                }
            }
            return $results;
        }
?>
galenjr at gmail dot com 15-Jun-2009 08:19
Another way to array_map htmlentities with a specific quote style is to create a function that does it and map that function

<?php

function map_entities( $str ) {
    return htmlentities( $str, ENT_QUOTES );
}
$good_array = array_map ( 'map_entities', $bad_array );

?>
radist-hack at yandex dot ru 01-Nov-2008 10:37
To transpose rectangular two-dimension array, use the following code:

array_unshift($array, null);
$array = call_user_func_array("array_map", $array);

If you need to rotate rectangular two-dimension array on 90 degree, add the following line before or after (depending on the rotation direction you need) the code above:
$array = array_reverse($array);

Here is example:

<?php
$a = array(
  array(1, 2, 3),
  array(4, 5, 6));
array_unshift($a, null);
$a = call_user_func_array("array_map", $a);
print_r($a);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 4
        )

    [1] => Array
        (
            [0] => 2
            [1] => 5
        )

    [2] => Array
        (
            [0] => 3
            [1] => 6
        )

)
Heero 16-Oct-2008 07:02
You can easily remove all HTML tags from $_GET or $_POST variables using something like this:

<?php

$_POST = array_map('strip_tags', $_POST);
$_GET = array_map('strip_tags', $_GET);

?>

This is useful when you don't want to parse HTML.
stijnleenknegt at gmail dot com 23-Jul-2008 01:17
If you want to pass an argument like ENT_QUOTES to htmlentities, you can do the follow.

<?php
$array = array_map( 'htmlentities' , $array, array_fill(0 , count($array) , ENT_QUOTES) );
?>

The third argument creates an equal sized array of $array filled with the parameter you want to give with your callback function.
GUI 26-Jun-2008 04:48
The following takes an array of objects, and returns the result of calling a member function on each object. So if I have an array of objects that all have a getName() method, calling array_map_objects("getName", $thingies) will return the array filled with the getName() value for each object.

<?php
function array_map_objects($member_function, $array) {
    $values = array();

    if(is_string($member_function) && is_array($array)) {
        $callback = create_function('$e', 'return call_user_func(array($e, "' . $member_function .'"));');
        $values = array_map($callback, $array);
    }

    return $values;
}
?>
moester at gmail dot com 02-Apr-2008 10:21
Wish this was built in.  Mimics Ruby and Prototype's array pluck function.  Returns specific key/column from an array of objects.

<?php
function array_pluck($key, $array)
{
    if (is_array($key) || !is_array($array)) return array();
    $funct = create_function('$e', 'return is_array($e) && array_key_exists("'.$key.'",$e) ? $e["'. $key .'"] : null;');
    return array_map($funct, $array);
}

// usage:

$a = array(array("id"=>10, "name"=>"joe"), array("id"=>11, "name"=>"bob"));

$ids = array_pluck("id", $a);        // == array(10,11)
$names = array_pluck("name", $a);    // == array("joe", "bob")

//works on non-keyed arrays also:

$a = array(array(3,4), array(5,6));
$col2 = array_pluck(1,$a);            // == array(4,6) (grab 2nd column of data)

?>
chreekat 13-Mar-2008 12:48
I was miffed that array_map didn't have a way to pass values *and* keys to the callback, but then I realized I could do this:

function callback($k, $v) { ... }

array_map( "callback", array_keys($array), $array);
jo at ho dot nl 17-Feb-2008 11:10
Could also use things like...

array_keys(); and array_values(); offcourse...

However it's just an example off recursion via this function..
Which I found pretty handy at times dealing with arrays..

could also use:

<?php
call_user_func(array($this, __FUNCTION), $args);
?>

or

<?php
call_user_fuc_array(array($this, __FUNCTION__), $array);
?>

or

<?php
class{

   public function __construct($arg){
       if(is_array($arg)){
            new self($arg);
       }
       else{
           echo $arg.'<br/>'.PHP_EOL;
       }
   }
}
?>

Anyway.. plenty off examples..
It was just an idea for others...
loaded67 at hotmail dot com 08-Feb-2008 11:59
this function is really nice for recursion in php!!!

example in a class:

<?php
class test{

    //private $container = array();

    final public function add($key, $value){
         /* recursion */
         if(is_array($value)){
               array_map(array($this, __FUNCTION__), array_keys($value), array_values($value));
         }
         /* procedural */
         else{
             echo $key.' => '.$value.'<br/>'.PHP_EOL;
             // do stuff...
             // if(!isset($this->container[$key])){
             //       $this->container[$key] = $value;
             // }
             //else{  // trigger_error() xor throw new Exception?
             //         echo 'allready exists!<br/>'.PHP_EOL;
             //}
         }
    }
}
//
$array = array (
                               'one'   => 'value1',
                               'two'   => 'value2',
                               'three' => 'value3'
                            );

$t = new test;
$t->add($array);
?>

you could easiely do this without a class too offcourse!
used in php 5.2.5
pmf 23-Jan-2008 04:02
This function behaves exactly like array_map but additionally does not reject non-array arguments. Instead, it transforms them with the array_fill function to a constant valued array of required length according to the other array arguments (if any) and executes the original array_map function.

<?php

function array_map2() {
    $args = func_get_args();

    $callback = array_shift($args);

    $args = array_map(
            create_function('$a,$max','return is_array($a)? $a: array_fill(0,$max,$a);'),
            $args,array_fill(0,count($args),array_reduce($args,
            create_function('$v,$w','return max($v,is_array($w)? count($w): 1);'))));

    array_unshift($args,$callback);

    return call_user_func_array("array_map",$args);
}

?>

Example:

<?php

$get = "first=value1&second=value2&third=value3";

print_r(array_map2("explode","=",explode("&",$get)));

?>

would print out:

<?php

Array
(
    [0] => Array
        (
            [0] => first
            [1] => value1
        )

    [1] => Array
        (
            [0] => second
            [1] => value2
        )

    [2] => Array
        (
            [0] => third
            [1] => value3
        )
)

?>

/pmf
henrique at webcoder dot com dot br 01-Nov-2007 05:02
Adding method support to function by Andref (multidimensionalArrayMap).

function array_map_r( $func, $arr )
{
    $newArr = array();

    foreach( $arr as $key => $value )
    {
        $newArr[ $key ] = ( is_array( $value ) ? array_map_r( $func, $value ) : ( is_array($func) ? call_user_func_array($func, $value) : $func( $value ) ) );
    }

    return $newArr;
}

array_map_r('function', array());
or
array_map_r(array('class', 'method'), array());
bturchik at iponweb dot net 19-Jul-2007 04:46
Maybe this one will be useful for someone:

function array_map_helper($mapper, $array) {
    $mapper = preg_replace('/^return (.*?);$/', '$1', trim($mapper));
    $result = array();
    if (preg_match('/(\(?)(.*?)\s*=>\s*(.*?)(\)?)$/', $mapper, $matches)) {
        list($full_found, $array_open, $left, $right, $array_close) = $matches;
        if ($array_open && $array_close) {
            $mapper = '$result[] = array' . $full_found . ';';
        } else {
            $mapper = '$result[' . $left . '] = ' . $right . ';';
        }
    } else {
        $mapper = '$result[] = ' . $mapper . ';';
    }

    foreach ($array as $key => $value) {
        eval($mapper);
    }

    return $result;
}

should be used like:

$array = array(array('foo' => 11, 'bar' => 22),
               array('foo' => 111, 'bar' => 222),
               array('foo' => 1111, 'bar' => 2222));
$mapped = array_map_helper('$value["foo"] => $value["bar"]', $array);

var_dump will give

array(3) {
  [11]=>
  int(22)
  [111]=>
  int(222)
  [1111]=>
  int(2222)
}

or

$mapped = array_map_helper('$value["foo"]', $array);

var_dump will give

array(3) {
  [0]=>
  int(11)
  [1]=>
  int(111)
  [2]=>
  int(1111)
}

or

$mapped = array_map_helper('$value["foo"] + $value["bar"] . " at position $key"', $array);

var_dump will give

array(3) {
  [0]=>
  string(16) "33 at position 0"
  [1]=>
  string(17) "333 at position 1"
  [2]=>
  string(18) "3333 at position 2"
}
andref dot dias at pronus dot eng dot br 24-Oct-2006 09:14
A recursive way to handle multidimensional arrays:

<?php

function multidimensionalArrayMap( $func, $arr )
    {
    $newArr = array();
    foreach( $arr as $key => $value )
        {
        $newArr[ $key ] = ( is_array( $value ) ? multidimensionalArrayMap( $func, $value ) : $func( $value ) );
        }
    return $newArr;
    }

?>
pcdinh at phpvietnam dot net 18-Mar-2006 05:50
Hi benjaminhill,

You can apply a method of a instantiated class to array_maps as follows:

class Maths {
    function addOne($input) {
        return ($input + 1);
    }
}
$maths = new Maths();
$sum = array_map(array($maths, \\\'addOne\\\'), array(1, 2));
// where $maths is the object which has been instantiated before and addOne is its method without its own parameters
var_dump($sum);

The code fragment will return:

array
  0 => 2
  1 => 3

However, I love a syntax like this:

$sum = array_map($maths->addOne($this), array(1, 2));

where $this should be interpreted as each values extracted from the subsequent array, which in this case is array(1, 2).

This syntax reminds me of Javascript syntax.

PHP\\\'s callback mechanism should be improved.
26-Aug-2005 03:57
Here's a function, very helpfull to me, that allows you to map your callback on mixed args.

<?php
function array_smart_map($callback) {
    // Initialization
    $args = func_get_args() ;
    array_shift($args) ; // suppressing the callback
    $result = array() ;

    // Validating parameters
    foreach($args as $key => $arg)
        if(is_array($arg)) {
            // the first array found gives the size of mapping and the keys that will be used for the resulting array
            if(!isset($size)) {
                $keys = array_keys($arg) ;
                $size = count($arg) ;
            // the others arrays must have the same dimension
            } elseif(count($arg) != $size) {
                return FALSE ;
            }
            // all keys are suppressed
            $args[$key] = array_values($arg) ;
        }

    // doing the callback thing
    if(!isset($size))
        // if no arrays were found, returns the result of the callback in an array
        $result[] = call_user_func_array($callback, $args) ;
    else
        for($i=0; $i<$size; $i++) {
            $column = array() ;
            foreach($args as $arg)
                $column[] = ( is_array($arg) ? $arg[$i] : $arg ) ;
            $result[$keys[$i]] = call_user_func_array($callback, $column) ;
        }

    return $result ;

}
?>

Trying with :

<?php
// $_GET is ?foo=bar1-bar2-bar3&bar=foo1
print_r(array_smart_map('explode', '-', $_GET)) ;
?>

Returns :

array(
    [foo] => array(
        0 => bar1
        1 => bar2
        2 => bar3
    )

    [bar] => array(
        1 => foo1
    )
)
david dot tulloh at infaze dot com dot au 07-Jul-2005 01:53
You can pass values to array_map by reference, essentially allowing you to use it as you would array_walk with multiple arrays as parameters.

A trivial example:
<?php
$a = array(1,2,3,4,5);
$add_func = create_function('&$x, $y', '$x+=$y;');
array_map($add_func, $a, $a);
print_r($a);
?>
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
    [4] => 10
)
Vinicius Cubas Brand 23-Mar-2005 02:31
The following function does exaclty the same thing of array_map. However, maintains the same index of the input arrays

<?php
    function array_map_keys($param1,$param2,$param3=NULL)
    {
        $res = array();

        if ($param3 !== NULL)
        {
            foreach(array(2,3) as $p_name)
            {
                if (!is_array(${'param'.$p_name}))
                {
                    trigger_error(__FUNCTION__.'(): Argument #'.$p_name.' should be an array',E_USER_WARNING);
                    return;
                }
            }
            foreach($param2 as $key => $val)
            {
                $res[$key] = call_user_func($param1,$param2[$key],$param3[$key]);
            }
        }
        else
        {
            if (!is_array($param2))
            {
                trigger_error(__FUNCTION__.'(): Argument #2 should be an array',E_USER_WARNING);
                return;
            }
            foreach($param2 as $key => $val)
            {
                $res[$key] = call_user_func($param1,$param2[$key]);
            }
        }
        return $res;
    }
?>

For instance:

<?php
    $arr1 = array(
        '3' => 'a',
        '4' => 'b',
        '5' => 'c'
        );

    $arr2 = array(
        '3' => 'd',
        '4' => 'e',
        '5' => 'f'
        );

    $arr3 = array_map_keys(create_function('$a,$b','return $a.$b;'),$arr1,$arr2);

    print_r($arr3);

?>

The result will be:

Array
(
    [3] => ad
    [4] => be
    [5] => cf
)
endofyourself at yahoo dot com 20-Feb-2005 08:29
If you need to call a static method from array_map, this will NOT work:

<?PHP
array_map('myclass::myMethod' , $value);
?>

Instead, you need to do this:

<?PHP
array_map( array('myclass','myMethod') , $value);
?>

It is helpful to remember that this will work with any PHP function which expects a callback argument.
nd0 at gmx dot de 02-Jul-2004 01:42
array_map works also fine with create_function:

<?php
$a = array(1, 2, 3, 4, 5);
$b = array_map(create_function('$n', 'return $n*$n*$n;'), $a);
print_r($b);
?>

if you want to manipulate the elements of the array, instead to on a copy,
than take a look at array_walk:

<?php
$a = array(1, 2, 3, 4, 5);
array_walk($a, create_function('&$n', '$n = $n*$n*$n;'));
print_r($a);
?>

The Result of both is:

Array
(
    [0] => 1
    [1] => 8
    [2] => 27
    [3] => 64
    [4] => 125
)
stephen at mu dot com dot au 07-Jan-2003 07:02
A note when doing something allong the lines of:

<?php
class foo {
  var $var;
  function bar() {
     array_map(array($this, "baz"), array(1,2,3));
  }

  function baz($arg) {
    $this->var = $this->var + $arg;
  }
}
?>

This will *not* work as expected. You need to pass $this by reference as with:

array_map(array(&$this, "baz"), array(1,2,3));

or you'll be making a copy of the object each time, changing a value, then throwing the result away.


두개 이상의 배열을 재귀적으로 병합

  • ≪ array_map
  • array_merge ≫
  • PHP Manual
  • 배열 함수 목록
  • 두개 이상의 배열을 재귀적으로 병합

array_merge_recursive

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

array_merge_recursive — 두개 이상의 배열을 재귀적으로 병합

설명

array array_merge_recursive ( array $array1 [, array $... ] )

array_merge_recursive()는 하나 이상의 배열 원소들을 병합하여 이전 배열의 끝에 다른 배열값들을 추가하고 이 배열을 반환한다.

입력되는 배열에 같은 문자열 키가 있으면, 이 키에 대한 값을 배열에 같이 병합시키고, 그리고 배열을 값으로 갖는 경우 다른 배열 안의 같은 이름의 배열에 그 값들을 병합할 수 있도록 이 동작을 재귀적으로 수행해 나간다. 하지만, 배열이 같은 숫자 키를 갖는다면, 뒤에 오는 값은 원래 값을 덮어쓰지 않고 추가될것이다.

인수

array1

병합할 초기 배열.

array

재귀 병합할 배열 목록 변수.

반환값

서로 원소를 병합한 값의 배열.

예제

Example #1 array_merge_recursive() 예제

<?php
$ar1 = array ("color" => array ("favorite" => "red"), 5);
$ar2 = array (10, "color" => array ("favorite" => "green", "blue"));
$result = array_merge_recursive ($ar1, $ar2);
print_r($result);
?>

위 예제의 출력:

Array
(
    [color] => Array
        (
            [favorite] => Array
                (
                    [0] => red
                    [1] => green
                )

            [0] => blue
        )

    [0] => 5
    [1] => 10
)

참고

  • array_merge() - 하나 이상의 배열을 병합

User Contributed Notes

karl (dot) hepler (at) gmail (dot) com 08-Oct-2016 05:24
I ran into a fairly unique situation where array_merge_recursive ALMOST did what I wanted, but NOT QUITE. I read through all of the comments, and I didn't find anything that really helped me. I saw a lot of functions submitted that were just trying to recreate array_replace_recursive. This is not that.

Take a look at the code and try it out. Hopefully it helps someone in need!

class Arr
{
    /**
     * Merge multiple arrays.
     *
     * This is similar to, but slightly different than array_merge_recursive.
     * The main difference is that it will merge like keys in subarrays,
     * instead of simply pushing them into the output array.
     *
     * @param  array ...$array
     *
     * @return array
     */
    public static function merge()
    {
        /** Initialize the output array */
        $merged = [];

        /** Go through each argument */
        foreach (func_get_args() as $array) {
            /** Go through each key/value of this array */
            foreach ($array as $key => $value) {
                /**
                 * If this key isn't set on merged,
                 * then it needs to be set for the first time
                 */
                if (! isset($merged[$key])) {
                    /**
                     * Before we can set it, we must make sure
                     * to dive into this value if it is an array,
                     * so that all of its children will be processed.
                     */
                    if (is_array($value)) {
                        $value = static::merge($value);
                    }

                    /**
                     * Now that we're happy with this value,
                     * and we're sure that, if it is an array,
                     * all of its children have been processed,
                     * let's go ahead and set it.
                     */
                    $merged[$key] = $value;

                    /** We can skip the rest of the loop */
                    continue;
                }

                /**
                 * We're here because we want to set a key
                 * that is already set on merged. We don't want
                 * to overwrite anything - we want to add to it.
                 * So, we need to make sure that we're working with an array.
                 */
                if (! is_array($merged[$key])) {
                    $merged[$key] = [$merged[$key]];
                }

                /**
                 * Before we push the value onto the array,
                 * we need to check to see if it is an array itself.
                 * If it is, then we need to make sure all of its children
                 * are processed. We do this by merging all of the children
                 * together. This is where it differs from array_merge_recursive,
                 * which would simply push the children onto the end of the array.
                 */
                if (is_array($value)) {
                    $value = forward_static_call_array([Arr::class, 'merge'], $value);
                }

                /** Now we're ready to push the value into merged */
                $merged[$key][] = $value;
            }
        }

        /** Return the merged array */
        return $merged;
    }
}
fantomx1 at gmail dot com 27-Jan-2016 08:35
I little bit improved daniel's and gabriel's contribution to behave more like original array_merge function to append numeric keys instead of overwriting them and added usefull option of specifying which elements to merge as you more often than not need to merge only specific part of array tree, and some parts of array just need  to let overwrite previous. By specifying helper element mergeWithParent=true, that section of array  will be merged, otherwise latter array part will override former. First level of array behave as classic array_merge.

        function array_merge_recursive_distinct ( array &$array1, array &$array2 )
    {
        static $level=0;
        $merged = [];
        if (!empty($array2["mergeWithParent"]) || $level == 0) {
            $merged = $array1;
        }

        foreach ( $array2 as $key => &$value )
        {
            if (is_numeric($key)) {
                $merged [] = $value;
            } else {
                $merged[$key] = $value;
            }

            if ( is_array ( $value ) && isset ( $array1 [$key] ) && is_array ( $array1 [$key] )
            ) {
                $level++;
                $merged [$key] = array_merge_recursive_distinct($array1 [$key], $value);
                $level--;
            }
        }
        unset($merged["mergeWithParent"]);
        return $merged;
    }
Anonymous 01-Dec-2015 06:45
Proper simplistic example:

<?php

$Array1 = [1, 2];
$Array2 = [3, 4];

echo implode("<br>", array_merge_recursive($Array1, $Array2));

?>

Output:
1
2
3
4
Orhan POLAT 18-Jun-2015 11:24
Merging arrays recursively some problem about existing keys, so that i wrote the function above like this:

function merge($array0, $array1) {
  // Result
  $merged = array();

  foreach (func_get_args() as $array) {
   // Check incoming argument is array
   if (is_array($array)) {
    foreach ($array as $key => $value) {
     // Check there is an array with the key: $key
     if (isset($merged[$key])) {
      // Check the value is array
      if (is_array($value)) {
       // So we must merge current value with the existing array
       $merged[$key] = call_user_func_array(__FUNCTION__, $merged[$key], $value);
      } else {
       if (!is_array($merged[$key])) {
        // If the existing array with the key: $key not an array
        // We make it an array
        $merged[$key] = array($merged[$key]);
       }

       // And add the current value
       $merged[$key][] = $value;
      }
     } else {
      // If not exists make the array
      $merged[$key] = $value;
     }
    }
   }
  }

  return $merged;
 }
Anonymous 18-Mar-2015 02:00
Attention.
(used PHP 5.4. XAMPP)

Values with numeric keys are always appended.   The index of the merge array is determined by the startindex of the first array. Value4s with numeric keys seems always appended.
If a key looks like an integer, array_merge_recursive will interpret the string as an number.

In the example  $arr['farbe']['22'] = 'grun' wil be found after array_merge_recursive in $res['farbe'][33]

$ar1 = array("farbe" => array (31 => 'holla', "rot", "favorit" => "gelb2"), "favorit" => "gelb1", 5);
$ar2 = array(10, "farbe" => array ('22' => "grun",  "favorit" => 2, "blau"));
$result = array_merge_recursive ($ar1, $ar2);
var_dump($result);
echo('<hr>');

// var_dump
array(4) {
  ["farbe"]=>
  array(5) {
    [31]=>
    string(5) "holla"
    [32]=>
    string(3) "rot"
    ["favorit"]=>
    array(2) {
      [0]=>
      string(5) "gelb2"
      [1]=>
      int(2)
    }
    [33]=>
    string(5) "grun"
    [34]=>
    string(4) "blau"
  }
  ["favorit"]=>
  string(5) "gelb1"
  [0]=>
  int(5)
  [1]=>
  int(10)
}
php at metehanarslan dot com 13-Jun-2014 07:52
Sometimes you need to modify an array with another one here is my approach to replace an array's content recursively with delete opiton. Here i used "::delete::" as reserved word to delete items.

<?php
$person= array(
    "name" => "Metehan",
    "surname"=>"Arslan",
    "age"=>27,
    "mail"=>"hidden",
    "favs" => array(
        "language"=>"php",
        "planet"=>"mercury",
        "city"=>"istanbul")
);

$newdata = array(
    "age"=>28,
    "mail"=>"::delete::",
    "favs" => array(
        "language"=>"js",
        "planet"=>"mercury",
        "city"=>"shanghai")
);

print_r(array_overlay($person,$newdata));
// result: Array ( [name] => Metehan [surname] => Arslan [age] => 28 [favs] => Array ( [language] => js [planet] => mercury [city] => shanghai ) )

function array_overlay($a1,$a2)
{
    foreach($a1 as $k => $v) {
        if ($a2[$k]=="::delete::"){
            unset($a1[$k]);
            continue;
        };
        if(!array_key_exists($k,$a2)) continue;
        if(is_array($v) && is_array($a2[$k])){
            $a1[$k] = array_overlay($v,$a2[$k]);
        }else{
            $a1[$k] = $a2[$k];
        }

    }
    return $a1;
}
?>
cezarion at cezarion dot net 09-Apr-2014 09:22
An updated version of  array_merge_recursive without overwriting numeric keys from martyniuk :
function array_merge_recursive_new()
 {
    $arrays = func_get_args();
        $base = array_shift($arrays);

        foreach ($arrays as $array) {
            reset($base); //important
            while (list($key, $value) = @each($array)) {
                if (is_array($value) && @is_array($base[$key])) {
                    $base[$key] = array_merge_recursive_new($base[$key], $value);
                } else {
                    if(isset($base[$key]) && is_int($key)) {
                      $key++;
                    }
                    $base[$key] = $value;
                }
            }
        }

    return $base;
  }
robert dot schlak at alcatel-lucent dot com 03-Jul-2012 01:39
The presence of NULLs; here is an example of the issue and a fix. Although it may not be apparent, if using array_merge_recursive in a loop to combine results from a database query or some other function, you can corrupt your result when NULLs are present in the data. I discovered this when migrating from an Oracle DB to a MySQL DB. I had to match the array structure returned from the PHP function calling the DB and got bit. The array_walk call fixed this for me.
This is a simple example that lacks any DB calls and looping. Assume your array had the DB column names (first, last, and age) and you needed to combine the data in a multi-dimensional array in which the column name is an array key with all rows beneath it. The corruption occurs in $a3. If using element position 2, one could create the fictitious 'pete johnson' because of the collapsing of elements.

<?php
print "<pre>Show corruption\\n";
$a1 = array('first'=>'bob', 'last'=>'jones', 'age'=>'48');
$a2 = array('first'=>'sam', 'last'=>'smith', 'age'=>'41');
$a3 = array('first'=>'pete', 'last'=>null, 'age'=>'3');
$a4 = array('first'=>'joe', 'last'=>'johnson', 'age'=>'33');
$a5 = array_merge_recursive($a1,$a2,$a3,$a4);
print_r($a5);

print "Show Fix\\n";
$a1 = array('first'=>'bob', 'last'=>'jones', 'age'=>'48');
$a2 = array('first'=>'sam', 'last'=>'smith', 'age'=>'41');
$a3 = array('first'=>'pete', 'last'=>null, 'age'=>'3');
array_walk($a3, 'null_to_empty');
$a4 = array('first'=>'joe', 'last'=>'johnson', 'age'=>'33');
$a5 = array_merge_recursive($a1,$a2,$a3,$a4);
print_r($a5);
print "</pre>\\n";

function null_to_empty(&$item) {

  $item = is_null($item) ? '' : $item;
}
?>
martyniuk dot vasyl at gmail dot com 24-Dec-2011 06:10
This is my version of array_merge_recursive without overwriting numeric keys:
<?php
function array_merge_recursive_new() {

        $arrays = func_get_args();
        $base = array_shift($arrays);

        foreach ($arrays as $array) {
            reset($base); //important
            while (list($key, $value) = @each($array)) {
                if (is_array($value) && @is_array($base[$key])) {
                    $base[$key] = array_merge_recursive_new($base[$key], $value);
                } else {
                    $base[$key] = $value;
                }
            }
        }

        return $base;
    }
?>
walf 26-May-2011 07:23
There are a lot of examples here for recursion that are meant to behave more like array_merge() but they don't get it quite right or are fairly customised. I think this version is most similar, takes more than 2 arguments and can be renamed in one place:

<?php

function array_merge_recursive_simple() {

    if (func_num_args() < 2) {
        trigger_error(__FUNCTION__ .' needs two or more array arguments', E_USER_WARNING);
        return;
    }
    $arrays = func_get_args();
    $merged = array();
    while ($arrays) {
        $array = array_shift($arrays);
        if (!is_array($array)) {
            trigger_error(__FUNCTION__ .' encountered a non array argument', E_USER_WARNING);
            return;
        }
        if (!$array)
            continue;
        foreach ($array as $key => $value)
            if (is_string($key))
                if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key]))
                    $merged[$key] = call_user_func(__FUNCTION__, $merged[$key], $value);
                else
                    $merged[$key] = $value;
            else
                $merged[] = $value;
    }
    return $merged;
}

$a1 = array(
    88 => 1,
    'foo' => 2,
    'bar' => array(4),
    'x' => 5,
    'z' => array(
        6,
        'm' => 'hi',
    ),
);
$a2 = array(
    99 => 7,
    'foo' => array(8),
    'bar' => 9,
    'y' => 10,
    'z' => array(
        'm' => 'bye',
        11,
    ),
);
$a3 = array(
    'z' => array(
        'm' => 'ciao',
    ),
);
var_dump(array_merge($a1, $a2, $a3));
var_dump(array_merge_recursive_simple($a1, $a2, $a3));
var_dump(array_merge_recursive($a1, $a2, $a3));
?>

gives:

array(7) {              array(7) {              array(7) {
  int(1)                  int(1)                  int(1)
  ["foo"]=>               ["foo"]=>               ["foo"]=>
  array(1) {              array(1) {              array(2) {
    [0]=>                   [0]=>                   [0]=>
    int(8)                  int(8)                  int(2)
  }                       }                         [1]=>
  ["bar"]=>               ["bar"]=>                 int(8)
  int(9)                  int(9)                  }
  ["x"]=>                 ["x"]=>                 ["bar"]=>
  int(5)                  int(5)                  array(2) {
  ["z"]=>                 ["z"]=>                   [0]=>
  array(1) {              array(3) {                int(4)
    ["m"]=>                 [0]=>                   [1]=>
    string(4) "ciao"        int(6)                  int(9)
  }                         ["m"]=>               }
  [1]=>                     string(4) "ciao"      ["x"]=>
  int(7)                    [1]=>                 int(5)
  ["y"]=>                   int(11)               ["z"]=>
  int(10)                 }                       array(3) {
}                         [1]=>                     [0]=>
                          int(7)                    int(6)
                          ["y"]=>                   ["m"]=>
                          int(10)                   array(3) {
                        }                             [0]=>
                                                      string(2) "hi"
                                                      [1]=>
                                                      string(3) "bye"
                                                      [2]=>
                                                      string(4) "ciao"
                                                    }
                                                    [1]=>
                                                    int(11)
                                                  }
                                                  [1]=>
                                                  int(7)
                                                  ["y"]=>
                                                  int(10)
                                                }
andyidol at gmail dot com 11-Feb-2011 04:14
Here's my function to recursively merge two arrays with overwrites. Nice for merging configurations.

<?php

function MergeArrays($Arr1, $Arr2)
{
  foreach($Arr2 as $key => $Value)
  {
    if(array_key_exists($key, $Arr1) && is_array($Value))
      $Arr1[$key] = MergeArrays($Arr1[$key], $Arr2[$key]);

    else
      $Arr1[$key] = $Value;

  }

  return $Arr1;

}

?>
lsiq at papotam com 01-Oct-2010 10:17
<?php

//  this function merges an array with the $_SESSION
//  if you omit second parameter it merges to the root
//  if you give one 'path' array it is merged deeply

function arr2sess($arr, $path=array()){
   if (!is_array($arr)) return false;
   foreach($arr as $k => $v){
      if(is_array($arr[$k])) {
          $path[] = $k;
          arr2sess($arr[$k], $path);
      }else{
          $ref = &$_SESSION;
          if($path){
              foreach($path as $val){
                  if(!$ref[$val]) $ref[$val] = array();
                  $ref = &$ref[$val];
              }
          }
          $ref[$k] = $v;
      }
   }
}

session_start();
$x = array(k1=>12, k2=>array(kxx=>'forget me', kyy=>'I was allways here'));     // do you have any of this on $_SESSION
$rpl = array(k2=>array(kxx=>'I am a replaced value',kzz=>'I am a new value'));
arr2sess($x, array('deep','deep','in_session'));                                // you can use this way
arr2sess($x);                                                                   // or this
arr2sess($rpl);                                                                 // to merge parts with the $_SESSION

$w = array(120, q=>array(199,100));                                             // works the same way on numeric keys
arr2sess($w, array('one','two'));
arr2sess($w);

echo '<pre>';
print_r($_SESSION);

?>
mark dot roduner at gmail dot com 14-Feb-2010 08:34
<?php
/**
 * Merges any number of arrays / parameters recursively, replacing
 * entries with string keys with values from latter arrays.
 * If the entry or the next value to be assigned is an array, then it
 * automagically treats both arguments as an array.
 * Numeric entries are appended, not replaced, but only if they are
 * unique
 *
 * calling: result = array_merge_recursive_distinct(a1, a2, ... aN)
**/

function array_merge_recursive_distinct () {
  $arrays = func_get_args();
  $base = array_shift($arrays);
  if(!is_array($base)) $base = empty($base) ? array() : array($base);
  foreach($arrays as $append) {
    if(!is_array($append)) $append = array($append);
    foreach($append as $key => $value) {
      if(!array_key_exists($key, $base) and !is_numeric($key)) {
        $base[$key] = $append[$key];
        continue;
      }
      if(is_array($value) or is_array($base[$key])) {
        $base[$key] = array_merge_recursive_distinct($base[$key], $append[$key]);
      } else if(is_numeric($key)) {
        if(!in_array($value, $base)) $base[] = $value;
      } else {
        $base[$key] = $value;
      }
    }
  }
  return $base;
}
?>
drvali at hotmail dot com 06-Oct-2009 06:31
<?php
/**
 * Merges any number of arrays of any dimensions, the later overwriting
 * previous keys, unless the key is numeric, in whitch case, duplicated
 * values will not be added.
 *
 * The arrays to be merged are passed as arguments to the function.
 *
 * @access public
 * @return array Resulting array, once all have been merged
 */
function array_merge_replace_recursive() {
    // Holds all the arrays passed
    $params = & func_get_args ();

    // First array is used as the base, everything else overwrites on it
    $return = array_shift ( $params );

    // Merge all arrays on the first array
    foreach ( $params as $array ) {
        foreach ( $array as $key => $value ) {
            // Numeric keyed values are added (unless already there)
            if (is_numeric ( $key ) && (! in_array ( $value, $return ))) {
                if (is_array ( $value )) {
                    $return [] = $this->array_merge_replace_recursive ( $return [$$key], $value );
                } else {
                    $return [] = $value;
                }

            // String keyed values are replaced
            } else {
                if (isset ( $return [$key] ) && is_array ( $value ) && is_array ( $return [$key] )) {
                    $return [$key] = $this->array_merge_replace_recursive ( $return [$$key], $value );
                } else {
                    $return [$key] = $value;
                }
            }
        }
    }

    return $return;
}

$a = array (
    "a" => 1,
    "b" => 2,
    'foo',
    'bar'
);
$b = array (
    "a" => 2,
    "c" => 3,
    'foo'
);

$c = array_merge_replace_recursive ( $a, $b );
print_r ( $a );
print_r ( $b );
print_r ( $c );
?>

Output:
Array
(
    [a] => 1
    [b] => 2
    [0] => foo
    [1] => bar
)
Array
(
    [a] => 2
    [c] => 3
    [0] => foo
)
Array
(
    [a] => 2
    [b] => 2
    [0] => foo
    [1] => bar
    [c] => 3
)
gabriel dot sobrinho at gmail dot com 14-Jul-2009 04:56
I refactored the Daniel's function and I got it:

<?php
/**
 * array_merge_recursive does indeed merge arrays, but it converts values with duplicate
 * keys to arrays rather than overwriting the value in the first array with the duplicate
 * value in the second array, as array_merge does. I.e., with array_merge_recursive,
 * this happens (documented behavior):
 *
 * array_merge_recursive(array('key' => 'org value'), array('key' => 'new value'));
 *     => array('key' => array('org value', 'new value'));
 *
 * array_merge_recursive_distinct does not change the datatypes of the values in the arrays.
 * Matching keys' values in the second array overwrite those in the first array, as is the
 * case with array_merge, i.e.:
 *
 * array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value'));
 *     => array('key' => array('new value'));
 *
 * Parameters are passed by reference, though only for performance reasons. They're not
 * altered by this function.
 *
 * @param array $array1
 * @param array $array2
 * @return array
 * @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
 * @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
 */
function array_merge_recursive_distinct ( array &$array1, array &$array2 )
{
  $merged = $array1;

  foreach ( $array2 as $key => &$value )
  {
    if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) )
    {
      $merged [$key] = array_merge_recursive_distinct ( $merged [$key], $value );
    }
    else
    {
      $merged [$key] = $value;
    }
  }

  return $merged;
}
?>

This fix the E_NOTICE when the the first array doesn't have the key and the second array have a value which is a array.
jonnybergstrom at googles mail domain dot comm 27-Jun-2009 05:30
This function didn't work for me - or it didn't do what I thought it would. So I wrote the below function, which merges two arrays, and returns the resulting array. The base array is the left one ($a1), and if a key is set in both arrays, the right value has precedence. If a value in the left one is an array and also an array in the right one, the function calls itself (recursion). If the left one is an array and the right one exists but is not an array, then the right non-array-value will be used.

*Any key only appearing in the right one will be ignored*
 - as I didn't need values appearing only in the right in my implementation, but if you want that you could make some fast fix.

function array_merge_recursive_leftsource(&$a1, &$a2) {
    $newArray = array();
    foreach ($a1 as $key => $v) {
        if (!isset($a2[$key])) {
            $newArray[$key] = $v;
            continue;
        }

        if (is_array($v)) {
            if (!is_array($a2[$key])) {
                $newArray[$key] = $a2[$key];
                continue;
            }
            $newArray[$key] = array_merge_recursive_leftsource($a1[$key], $a2[$key]);
            continue;
        }

        $newArray[$key] = $a2[$key];
    }
    return $newArray;
}
michiel at synetic dot nl 22-May-2009 10:54
A small improvement upon the previously posted array_merge_recursive_distinct functions (based on daniel's version). This implementation preserves the parameter input from the original, you can pass an infinite amount of array's to merge.

<?php
function &array_merge_recursive_distinct()
{
    $aArrays = func_get_args();
    $aMerged = $aArrays[0];

    for($i = 1; $i < count($aArrays); $i++)
    {
        if (is_array($aArrays[$i]))
        {
            foreach ($aArrays[$i] as $key => $val)
            {
                if (is_array($aArrays[$i][$key]))
                {
                    $aMerged[$key] = is_array($aMerged[$key]) ? PR::array_merge_recursive_distinct($aMerged[$key], $aArrays[$i][$key]) : $aArrays[$i][$key];
                }
                else
                {
                    $aMerged[$key] = $val;
                }
            }
        }
    }

    return $aMerged;
}
?>
daniel at danielsmedegaardbuus dot dk 19-Mar-2009 11:18
<?php
/**
 * array_merge_recursive does indeed merge arrays, but it converts values with duplicate
 * keys to arrays rather than overwriting the value in the first array with the duplicate
 * value in the second array, as array_merge does. I.e., with array_merge_recursive,
 * this happens (documented behavior):
 *
 * array_merge_recursive(array('key' => 'org value'), array('key' => 'new value'));
 *     => array('key' => array('org value', 'new value'));
 *
 * array_merge_recursive_distinct does not change the datatypes of the values in the arrays.
 * Matching keys' values in the second array overwrite those in the first array, as is the
 * case with array_merge, i.e.:
 *
 * array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value'));
 *     => array('key' => array('new value'));
 *
 * Parameters are passed by reference, though only for performance reasons. They're not
 * altered by this function.
 *
 * @param array $array1
 * @param mixed $array2
 * @return array
 * @author daniel@danielsmedegaardbuus.dk
 */
function &array_merge_recursive_distinct(array &$array1, &$array2 = null)
{
  $merged = $array1;

  if (is_array($array2))
    foreach ($array2 as $key => $val)
      if (is_array($array2[$key]))
        $merged[$key] = is_array($merged[$key]) ? array_merge_recursive_distinct($merged[$key], $array2[$key]) : $array2[$key];
      else
        $merged[$key] = $val;

  return $merged;
}
?>
miniscalope at gmail dot com 04-Feb-2009 12:56
I would merge 2 arrays but keep the values unique in the result array.
I hope this will help...
<?php

function array_merge_recursive_unique($array1, $array2) {

    foreach($array2 AS $k => $v) {
        if(!isset($array1[$k]))
        {
            $array1[$k] = $v;
        }
        else
        {
            if(!is_array($v)){
                if(is_array($array1[$k]))
                {
                    if(!in_array($v,$array1[$k]))
                    {
                        $array1[$k][] = $v;
                    }
                }
                else
                {
                    if($array1[$k] != $v)
                        $array1[$k] = array($array1[$k], $v);
                 }
            }
            else
            {
                $array1[$k] =    array_merge_recursive_unique($array1,$array2[$k]);
            }

        }

    }
      unset($k, $v);
      return $array1;
}

?>
remy dot damour at -please-no-spam-laposte dot net 21-Jan-2009 03:28
If what you want is merge all values of your array that are arrays themselves to get a resulting array of depth one, then you're more looking for array_flatten function.

Unfortunately I did not find such native function in php, here is the one I wrote:

<?php
/**
 * Flatten an array so that resulting array is of depth 1.
 * If any value is an array itself, it is merged by parent array, and so on.
 *
 * @param array $array
 * @param bool $preserver_keys OPTIONAL When true, keys are preserved when mergin nested arrays (=> values with same key get overwritten)
 * @return array
 */
function array_flatten($array, $preserve_keys = false)
{
    if (!$preserve_keys) {
        // ensure keys are numeric values to avoid overwritting when array_merge gets called
        $array = array_values($array);
    }

    $flattened_array = array();
    foreach ($array as $k => $v) {
        if (is_array($v)) {
            $flattened_array = array_merge($flattened_array, call_user_func(__FUNCTION__, $v, $preserve_keys));
        } elseif ($preserve_keys) {
            $flattened_array[$k] = $v;
        } else {
            $flattened_array[] = $v;
        }
    }
    return $flattened_array;
}

// example
$a = array ('k1' => 'a', 'k2' => array('k1' => 'b', 'k4' => array('k3' => 'c')));
var_export(array_flatten($a)); // output: array(0 => 'a', 1 => 'b', 2 => 'c')
var_export(array_flatten($a, true)); // output: array('k1' => 'b', 'k3' => 'c') // first 'k1' value gets overwritten by nested 'k1' value
?>
phil dot kemmeter at gmail dot com 08-Dec-2008 07:45
I've edit this version even a little bit more, so that the function does not override any values, but inserts them at a free key in the array:

function my_array_merge ($arr,$ins) {
    if(is_array($arr))
    {
        if(is_array($ins)) foreach($ins as $k=>$v)
        {
            if(isset($arr[$k])&&is_array($v)&&is_array($arr[$k]))
            {
                $arr[$k] = my_array_merge($arr[$k],$v);
            }
            else {
                // This is the new loop :)
                while (isset($arr[$k]))
                    $k++;
                $arr[$k] = $v;
            }
        }
    }
    elseif(!is_array($arr)&&(strlen($arr)==0||$arr==0))
    {
        $arr=$ins;
    }
    return($arr);
}

Example:

$array1 = array(
    100 => array(30),
    200 => array(20, 30)
);

$array2 = array(
    100 => array(40),
    201 => array(60, 30)
);

print_r(my_array_merge($array1,$array2));

Output with array_merge_recursive:
Array
(
    [0] => Array
        (
            [0] => 30
        )

    [1] => Array
        (
            [0] => 20
            [1] => 30
        )

    [2] => Array
        (
            [0] => 40
        )

)
This is not the result, I expect from a MERGE-Routine...

Output with the current function:

Array
(
    [100] => Array
        (
            [0] => 30
            [1] => 40
        )

    [200] => Array
        (
            [0] => 20
            [1] => 30
        )

)

This is what I want :)
scott dot clark at vizioninteractive dot com 28-Aug-2008 10:55
Needed some way to fuse two arrays together and found a function here (below from thomas) and decided to update it even further to be a little more smart.

<?php
function my_array_merge ($arr,$ins)
    {
        if(is_array($arr))
            {
                if(is_array($ins)) foreach($ins as $k=>$v)
                    {
                        if(isset($arr[$k])&&is_array($v)&&is_array($arr[$k]))
                            {
                                $arr[$k] = my_array_merge($arr[$k],$v);
                            }
                        else $arr[$k] = $v;
                    }
            }
        elseif(!is_array($arr)&&(strlen($arr)==0||$arr==0))
            {
                $arr=$ins;
            }
        return($arr);
    }
?>
thomas at n-o-s-p-a-m dot thoftware dot de 05-May-2008 03:50
This is a simple, three line approach.

Short description: If one of the Arguments isn't an Array, first Argument is returned. If an Element is an Array in both Arrays, Arrays are merged recursively, otherwise the element in $ins will overwrite the element in $arr (regardless if key is numeric or not). This also applys to Arrays in $arr, if the Element is scalar in $ins (in difference to the previous approach).

  function array_insert($arr,$ins) {
    # Loop through all Elements in $ins:
    if (is_array($arr) && is_array($ins)) foreach ($ins as $k => $v) {
      # Key exists in $arr and both Elemente are Arrays: Merge recursively.
      if (isset($arr[$k]) && is_array($v) && is_array($arr[$k])) $arr[$k] = array_insert($arr[$k],$v);
      # Place more Conditions here (see below)
      # ...
      # Otherwise replace Element in $arr with Element in $ins:
      else $arr[$k] = $v;
    }
    # Return merged Arrays:
    return($arr);
  }

In Addition to felix dot ospald at gmx dot de in my opinion there is no need to compare keys with type-casting, as a key always is changed into an integer if it could be an integer. Just try

$a = array('1'=>'1');
echo gettype(key($a));

It will echo 'integer'. So for having Integer-Keys simply appended instead of replaced, add the Line:

  elseif (is_int($k)) $arr[] = $v;

A Condition I used is:

  elseif (is_null($v)) unset($arr[$k]);

So a NULL-Value in $ins will unset the correspondig Element in $arr (which is different to setting it to NULL!). This may be another Addition to felix dot ospald at gmx dot de: The absolute correct way to check for a Key existing in an Array is using array_key_exists() (not needed in the current context, as isset() is combined with is_array()). array_key_exists() will return TRUE even if the Value of the Element is NULL.

And the last one: If you want to use this approach for more than 2 Arrays, simply use this:

  function array_insert_mult($arr) {
    # More than 1 Argument: Append all Arguments.
    if (func_num_args() > 1) foreach (array_slice(func_get_args(),1) as $ins) $arr = array_insert($arr,$ins);
    # Return merged Arrays:
    return($arr);
  }

And if you worry about maintaining References: Simply use $ins[$k] instead of $v when assigning a Value/using a Value as Argument.
felix dot ospald at gmx dot de 26-Feb-2008 08:23
If you desire correct and performant behaviour (in contrast to the other postings) use this code. It works as documented above.
If you want that keys are always perserved and not appended or renumbered if they are numeric comment out the "if (((string) $key) === ((string) intval($key)))" case.
@spambegone at cratemedia dot com: using empty is not right way to check if an item is in the array use isset!

<?php

function array_merge_recursive2($array1, $array2)
{
    $arrays = func_get_args();
    $narrays = count($arrays);

    // check arguments
    // comment out if more performance is necessary (in this case the foreach loop will trigger a warning if the argument is not an array)
    for ($i = 0; $i < $narrays; $i ++) {
        if (!is_array($arrays[$i])) {
            // also array_merge_recursive returns nothing in this case
            trigger_error('Argument #' . ($i+1) . ' is not an array - trying to merge array with scalar! Returning null!', E_USER_WARNING);
            return;
        }
    }

    // the first array is in the output set in every case
    $ret = $arrays[0];

    // merege $ret with the remaining arrays
    for ($i = 1; $i < $narrays; $i ++) {
        foreach ($arrays[$i] as $key => $value) {
            if (((string) $key) === ((string) intval($key))) { // integer or string as integer key - append
                $ret[] = $value;
            }
            else { // string key - megre
                if (is_array($value) && isset($ret[$key])) {
                    // if $ret[$key] is not an array you try to merge an scalar value with an array - the result is not defined (incompatible arrays)
                    // in this case the call will trigger an E_USER_WARNING and the $ret[$key] will be null.
                    $ret[$key] = array_merge_recursive2($ret[$key], $value);
                }
                else {
                    $ret[$key] = $value;
                }
            }
        }
    }

    return $ret;
}

// Examples:

print_r(array_merge_recursive2(array('A','B','C' => array(1,2,3)), array('D','C' => array(1,4))));
/*
Array
(
    [0] => A
    [1] => B
    [C] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 1
            [4] => 4
        )
    [2] => D
)*/

print_r(array_merge_recursive2(array('A','B','0' => array(1,2,3)), array('D','0' => array(1,array(4)))));
/*
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )
    [1] => B
    [2] => Array
        (
            [0] => 1
            [1] => Array
                (
                    [0] => 4
                )
        )
)*/

print_r(array_merge_recursive2(array('A' => array('A' => 1)), array('A' => array('A' => array(2)))));
/*
Warning: Argument #1 is not an array - trying to merge array with scalar! Returning null! in ... on line ...
*/

var_dump(array_merge_recursive2(array('A' => array('A' => 2)), array('A' => array('A' => null))));
/*
array(1) { ["A"]=>  array(1) { ["A"]=>  NULL } }
*/

?>
spambegone at cratemedia dot com 12-Feb-2008 11:06
I've tried these array_merge_recursive functions without much success. Maybe it's just me but they don't seem to actually go more than one level deep? As with all things, its usually easier to write your own, which I did and it seems to work just the way I wanted. Anyways, my function hasn't been tested extensively, but it's a simple function, so in hopes that this might be useful to someone else I'm sharing.

Also, the PHP function array_merge_recursive() didn't work for my purposes because it didn't overwrite the values like I needed it to. You know how it works, it just turns it into an array with multiple values... not helpful if your code is expecting one string.

function array_merge_recursive_unique($array1, $array2) {

    // STRATEGY
    /*
    Merge array1 and array2, overwriting 1st array values with 2nd array
    values where they overlap. Use array1 as the base array and then add
    in values from array2 as they exist.

    Walk through each value in array2 and see if a value corresponds
    in array1. If it does, overwrite with second array value. If it's an
    array, recursively execute this function and return the value. If it's
    a string, overwrite the value from array1 with the value from array2.

    If a value exists in array2 that is not found in array1, add it to array1.
    */

    // LOOP THROUGH $array2
    foreach($array2 AS $k => $v) {

        // CHECK IF VALUE EXISTS IN $array1
        if(!empty($array1[$k])) {
            // IF VALUE EXISTS CHECK IF IT'S AN ARRAY OR A STRING
            if(!is_array($array2[$k])) {
                // OVERWRITE IF IT'S A STRING
                $array1[$k]=$array2[$k];
            } else {
                // RECURSE IF IT'S AN ARRAY
                $array1[$k] = array_merge_recursive_unique($array1[$k], $array2[$k]);
            }
        } else {
            // IF VALUE DOESN'T EXIST IN $array1 USE $array2 VALUE
            $array1[$k]=$v;
        }
    }
    unset($k, $v);

    return $array1;
}
paha at paha dot hu 12-Mar-2007 09:02
In this version the values are overwritten only if they are not an array.  If the value is an array, its elements will be merged/overwritten:

// array_merge_recursive which override value with next value.
// based on: http://www.php.net/manual/hu/function.array-merge-recursive.php 09-Dec-2006 03:38
function array_merge_recursive_unique($array0, $array1)
{
    $arrays = func_get_args();
    $remains = $arrays;

    // We walk through each arrays and put value in the results (without
    // considering previous value).
    $result = array();

    // loop available array
    foreach($arrays as $array) {

        // The first remaining array is $array. We are processing it. So
        // we remove it from remaing arrays.
        array_shift($remains);

        // We don't care non array param, like array_merge since PHP 5.0.
        if(is_array($array)) {
            // Loop values
            foreach($array as $key => $value) {
                if(is_array($value)) {
                    // we gather all remaining arrays that have such key available
                    $args = array();
                    foreach($remains as $remain) {
                        if(array_key_exists($key, $remain)) {
                            array_push($args, $remain[$key]);
                        }
                    }

                    if(count($args) > 2) {
                        // put the recursion
                        $result[$key] = call_user_func_array(__FUNCTION__, $args);
                    } else {
                        foreach($value as $vkey => $vval) {
                            $result[$key][$vkey] = $vval;
                        }
                    }
                } else {
                    // simply put the value
                    $result[$key] = $value;
                }
            }
        }
    }
    return $result;
}
randallgirard at hotmail dot com 26-Nov-2006 10:18
I wrote the following for merging arrays, in my project mainly for configuration... Thought someone else might find it usefull.

function array_merge_recursive_keys( $first, $second, $greedy=false) {
   $inter = array_intersect_assoc(array_keys($first), array_keys($second)); # shaired keys
# the idea next, is to strip and append from $second into $first
   foreach ( $inter as $key ) {
   # recursion if both are arrays
      if ( is_array($first[$key]) && is_array($second[$key]) ) {
         $first[$key] = array_merge_recursive_keys($first[$key], $second[$key]);
      }
   # non-greedy array merging:
      else if ( is_array($first[$key] && !$greedy ) ) {
         $first[$key][] = $second[$key];
      }
      else if ( is_array($second[$key]) && !$greedy ) {
         $second[$key][] = $first[$key];
         $first[$key] = $second[$key];
      }
   # overwrite...
      else {
         $first[$key] = $second[$key];
      }
      unset($second[$key]);
   }
# merge the unmatching keys onto first
   return array_merge($first, $second);
}
thiago dot mata at yahoo dot com dot br 30-Sep-2006 09:25
<?php
    function array_merge_recursive_keep_keys( $arrElement1 , $arrElement2 , $intCount = 0 )
    {

        $arrNew = array();

        $arrElement1Keys = array_keys( $arrElement1 );
        $arrElement2Keys = array_keys( $arrElement2 );

        $arrDifKeys1 = array_diff( $arrElement1Keys, $arrElement2Keys );
        $arrDifKeys2 = array_diff( $arrElement2Keys, $arrElement1Keys );
        $arrInter     = array_intersect( $arrElement1Keys , $arrElement2Keys );

        foreach( $arrDifKeys1 as $strKey1)
        {
            $arrNew[ $strKey1 ] = $arrElement1[ $strKey1 ];
        }
        foreach( $arrDifKeys2 as $strKey2)
        {
            $arrNew[ $strKey2 ] = $arrElement2[ $strKey2 ];
        }
        foreach( $arrInter as $strInterKey )
        {
            if( is_array( $arrElement1[ $strInterKey ] ) && is_array( $arrElement2[ $strInterKey ] ) )
            {
                $intCount++;
                $arrNew[ $strInterKey ] = array_merge_recursive_keep_keys( $arrElement1[ $strInterKey ] , $arrElement2[ $strInterKey ] , $intCount );
            }
            elseif( is_array( $arrElement1[ $strInterKey ] ) || is_array( $arrElement2[ $strInterKey ] ) )
            {
                $arrNew[ $strInterKey ][]    =  $arrElement1[ $strInterKey ];
                $arrNew[ $strInterKey ][]    =  $arrElement2[ $strInterKey ];
            }
            else
            {
                $arrNew[ $strInterKey ] = array();
                $arrNew[ $strInterKey ][] = $arrElement1[ $strInterKey ];
                $arrNew[ $strInterKey ][] = $arrElement2[ $strInterKey ];
            }
        }
        return $arrNew;
    }
?>
smilingrasta 07-Jul-2006 07:04
This function tends to reindex arrays, which is not  mentioned in the function description.

I just tried to run that function on a three dimensional array, containing errormessages.
The first dim. contains the severity of the error ('warn', 'crit') the second dim the linenumber (numerical) and the third one consists of errormessages

<?php
# Array printout:
Array
 (
   [warn] => Array  // severity (associative)
    (
      [2] => Array  // linenumber (numerical)
       (
         [0] => "Category does not exist"
         [1] => "Manufacturer does not exist"
       )
    )
 )

?>

If i now merge two or more of those arrays using array_merge_recursive(), the linenumbers are not conserved. Instead of, they are all renumbered, starting with 0.

Just thought anyone may want to know about that. :)
regards, smilingrasta
manicdepressive at mindless dot com 22-Jun-2004 04:30
Please be aware that under circumstances where you have
both the key and value common between the two arrays at a given node,
array_merge_recursive() will behave differently if that value is NULL,
as opposed to a non-null value.

i.e., I expected the results of the first two sections below to
have the same structure, but they don't.
If this might apply to you, please see for yourself.

<pre><?php

$a1 = array('a'=>'b');
$a2 = array('a'=>'b');
$a3 = array_merge_recursive($a1,$a2);
var_export($a3);
echo "\n\n";

$a1 = array('a'=>NULL);
$a2 = array('a'=>NULL);
$a3 = array_merge_recursive($a1,$a2);
var_export($a3);
echo "\n\n";

$a1 = array('a'=>'b');
$a2 = array('a'=>NULL);
$a3 = array_merge_recursive($a1,$a2);
var_export($a3);
echo "\n\n";

?></pre>

This behavior also occurs if the value is the empty array.
In fact, in the above example, interchanging the empty array with
any and all occurences of NULL will yield the same result.

    code till dawn!  -mark
brian at vermonster dot com 25-May-2004 11:44
Here is a fairly simple function that replaces while recursing.

<?php
/**
 * array_merge_recursive2()
 *
 * Similar to array_merge_recursive but keyed-valued are always overwritten.
 * Priority goes to the 2nd array.
 *
 * @static yes
 * @public yes
 * @param $paArray1 array
 * @param $paArray2 array
 * @return array
 */
function array_merge_recursive2($paArray1, $paArray2)
{
    if (!is_array($paArray1) or !is_array($paArray2)) { return $paArray2; }
    foreach ($paArray2 AS $sKey2 => $sValue2)
    {
        $paArray1[$sKey2] = array_merge_recursive2(@$paArray1[$sKey2], $sValue2);
    }
    return $paArray1;
}

?>

Examples:
<?php

$array1 = array(
    'liquids' => array(
        'water' => array('cold', 'fizzy', 'clean')
        ,'beer' => 'warm'
    )
);

$array2 = array(
    'liquids' => array(
        'water' => 'hot'
        ,'milk' => 'wet'
    )
);

$result1 = array_merge_recursive2($array1, $array2);
$result2 = array_merge_recursive2($array2, $array1);
?>

Result 1 is:
Array
(
    [liquids] => Array
        (
            [water] => hot
            [beer] => warm
            [milk] => wet
        )
)

Result 2 is:
Array
(
    [liquids] => Array
        (
            [water] => Array
                (
                    [0] => cold
                    [1] => fizzy
                    [2] => clean
                )

            [milk] => wet
            [beer] => warm
        )
)
paska at kios dot sk 04-Mar-2004 11:18
This emulates replace of $_REQUEST according to variable_order=GPC.
<?
    function array_merge_replace($array, $newValues) {
        foreach ($newValues as $key => $value ) {
            if (is_array($value)) {
                if (!isset($array[$key])) {
                    $array[$key] = array();
                }
                $array[$key] = array_merge_replace($array[$key], $value);
            } else {
                $array[$key] = $value;
            }
        }
        return $array;
    }

    $_REQUEST = array_merge_replace($_REQUEST, $_GET);
    $_REQUEST = array_merge_replace($_REQUEST, $_POST);
    $_REQUEST = array_merge_replace($_REQUEST, $_COOKIE);
?>

Useful with stripping backslashes at beginning of main include file:
<?
if (get_magic_quotes_gpc() == 1) {

    function stripMagicSlashes($element) {
        if (is_array($element)) {
            return array_map("stripMagicSlashes", $element);
        } else {
            return stripslashes($element);
        }
    }

    function array_merge_replace($array, $newValues) {
        foreach ($newValues as $key => $value ) {
            if (is_array($value)) {
                if (!isset($array[$key])) {
                    $array[$key] = array();
                }
                $array[$key] = array_merge_replace($array[$key], $value);
            } else {
                $array[$key] = $value;
            }
        }
        return $array;
    }

    $_GET    = array_map("stripMagicSlashes", $_GET);
    $_POST   = array_map("stripMagicSlashes", $_POST);
    $_COOKIE = array_map("stripMagicSlashes", $_COOKIE);

    $_REQUEST = array_merge_replace($_REQUEST, $_GET);
    $_REQUEST = array_merge_replace($_REQUEST, $_POST);
    $_REQUEST = array_merge_replace($_REQUEST, $_COOKIE);

}

$GLOBALS['stripped'] = true;
?>

Based on examples from users from this site.


하나 이상의 배열을 병합

  • ≪ array_merge_recursive
  • array_multisort ≫
  • PHP Manual
  • 배열 함수 목록
  • 하나 이상의 배열을 병합

array_merge

(PHP 4, PHP 5, PHP 7)

array_merge — 하나 이상의 배열을 병합

설명

array array_merge ( array $array1 [, array $array2 [, array $... ]] )

하나 이상의 배열의 원소들을 병합하고 앞의 배열의 끝에 배열값들을 추가한다. 그 결과 배열을 반환한다.

입력되는 배열이 같은 문자열 키를 갖는다면, 그 키에 대해서 나중에 온 값이 이전에 오는 키에 대한 값을 덮어쓸것이다. 하지만, 그 배열이 숫자 키를 포함하면, 나중 값은 원래 값을 덮어쓰지 않고, 뒤에 추가될것이다.

하나의 배열만 주어지고, 그 배열이 숫자 인덱스로 되어 있으면, 키를 연속적으로 재인덱스합니다.

인수

array1

병합할 초기 배열.

array

재귀 병합할 배열 목록 변수.

반환값

결과 배열을 반환합니다.

예제

Example #1 array_merge() 예제

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

위 예제의 출력:

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

Example #2 간단한 array_merge() 예제

<?php
$array1 = array();
$array2 = array(1 => "data");
$result = array_merge($array1, $array2);
?>

숫자 키는 다른 숫자로 재부여된다는것을 잊지 말것!

Array
(
    [0] => data
)

배열을 온전히 보존하기를 원하고 그들 배열에 서로를 추가하고자 한다면(이전 키를 덮어쓰지 않음), + 연산자를 사용한다:

<?php
$array1 = array();
$array2 = array(1 => "data");
$result = $array1 + $array2;
?>

숫자 키는 보존될것이고 따라서 그 조합이 남겨진다.

Array
(
    [1] => data
)

Warning

array_merge()의 동작은 PHP 5에서 바뀌었습니다. PHP 4와는 달리, array_merge()는 array형 변수만 받습니다. 그러나, 자료형 변환으로 다른 자료형을 사용할 수 있습니다. 아래 예제를 참고하십시오.

Example #3 array_merge() PHP 5 예제

<?php
$beginning = 'foo';
$end = array(1 => 'bar');
$result = array_merge((array)$beginning, (array)$end);
print_r($result);
?>

위 예제의 출력:

Array
(
    [0] => foo
    [1] => bar
)

참고

  • array_merge_recursive() - 두개 이상의 배열을 재귀적으로 병합
  • array_combine() - 키를 위한 배열과 값을 위한 배열을 사용하여 배열을 생성
  • 배열 연산자

User Contributed Notes

jcmarchi at gmail dot comfive 20-May-2016 01:06
It is not officially documented but it is summarily important information for everyone to know: neither array_merge or array_merge_recursive functions will function correctly if non-array objects  are used as parameters.

You would probably expect these functions to ignore non-array elements, right? However, if one parameter is null it PHP will not only return null for the entire function but will also (not always) raise an error, such as :

    [ Warning: array_merge(): Argument #x is not an array... ]

This error message won't appear if the defective variable is an empty array (an empty array is still an array), but it will result in an undesirable incomplete Array.

There are several solutions for this problem by validating the Arrays before use them in these functions, but the most efficient way is to enforce the element as an array directly in the function itself. I.e.:

$merged = array_merge( (array)$first_array, (array)$second_array );
arifulin at gmail dot com 15-Mar-2016 06:10
public function mergeArrays($arrays, $field)
    {
        //take array in arrays for retrive structure after merging
        $clean_array = current($arrays);
        foreach ($clean_array as $i => $value) {
            $clean_array[$i]='';
        }

        $merged_array = [];
        $name = '';
        foreach ($arrays as $array){
            $array = array_filter($array); //clean array from empty values
            if ($name == $array[$field]) {
                $merged_array[$name] = array_merge($merged_array[$name], $array);
                $name = $array[$field];
            } else {
                $name = $array[$field];
                $merged_array[$name] = $array;
            }
        }
        //have to be cleaned from array 'field' signs to return original structure of arrays
        foreach ($merged_array as $array){
            $ready_array[] = array_merge($clean_array, $array);
        }

        return $ready_array;
    }
kristopolous at yahoo dot com 27-Feb-2016 07:46
I constantly forget the direction of array_merge so this is partially for me and partially for people like me.

array_merge is a non-referential non-inplace right-reduction. For different ctrl-f typers, it's reduce-right, side-effect free, idempotent, and non in-place.

ex:

$a = array_merge(['k' => 'a'], ['k' => 'b']) => ['k' => 'b']
array_merge(['z' => 1], $a) => does not modify $a but returns ['k' => 'b', 'z' => 1];

Hopefully this helps people that constant look this up such as myself.
info at eastghost dot com 05-Sep-2015 10:37
We noticed array_merge is relatively slower than manually extending an array:

given:
$arr_one[ 'x' => 1, 'y' => 2 ];
$arr_two[ 'a' => 10, 'b' => 20 ];

the statement:
$arr_three = array_merge( $arr_one, $arr_two );
is routinely 200usec slower than:

$arr_three = $arr_one;
foreach( $arr_two as $k => $v ) { $arr_three[ $k ] = $v; }

200usec didn't matter...until we started combining huge arrays.

PHP 5.6.x
Anonymous 29-Jun-2015 06:48
As PHP 5.6 you can use array_merge + "splat" operator to reduce a bidimensonal array to a simple array:

<?php
$data = [[1, 2], [3], [4, 5]];
print_r(array_merge(... $data)); // [1, 2, 3, 4, 5];
?>
gj@php 01-Jul-2014 11:59
i did a small benchmark (on  PHP 5.3.3) comparing:
* using array_merge on numerically indexed arrays
* using a basic double loop to merge multiple arrays

the performance difference is huge:

<?php

require_once("./lib/Timer.php");

function method1($mat){
    $all=array();
    foreach($mat as $arr){
        $all=array_merge($all,$arr);
    }
    return $all;
}

function method2($mat){
    $all=array();
    foreach($mat as $arr){
        foreach($arr as $el){
            $all[]=$el;
        }
    }
    return $all;
}

function tryme(){
    $y=250; //#arrays
    $x=200; //size per array
    $mat=array();
    //build big matrix
    for($i=0;$i<$y;$i++){
        for($j=0;$j<$x;$j++){
            $mat[$i][$j]=rand(0,1000);
        }
    }

    $t=new Timer();
    method1($mat);
    $t->displayTimer();

    $t=new Timer();
    method2($mat);
    $t->displayTimer();

/*
output:
Script runtime: 2.36782 secs. Script runtime: 0.02137 sec.
*/

}

tryme();

?>

So that's more than a factor 100!!!!!
Angel I 12-Jun-2014 10:57
An addition to what Julian Egelstaff above wrote - the array union operation (+) is not doing an array_unique - it will just not use the keys that are already defined in the left array.  The difference between union and merge can be seen in an example like this:

<?php
$arr1['one'] = 'one';
$arr1['two'] = 'two';

$arr2['zero'] = 'zero';
$arr2['one'] = 'three';
$arr2['two'] = 'four';

$arr3 = $arr1 + $arr2;
var_export( $arr3 );
# array ( 'one' => 'one', 'two' => 'two', 'zero' => 'zero', )

$arr4 = array_merge( $arr1, $arr2 );
var_export( $arr4 );
# array ( 'one' => 'three', 'two' => 'four', 'zero' => 'zero', )
?>
php at metehanarslan dot com 11-Jun-2014 07:49
Sometimes you need to modify an array with another one here is my approach to replace an array's content recursively with delete opiton. Here i used "::delete::" as reserved word to delete items.

<?php
$person= array(
    "name" => "Metehan",
    "surname"=>"Arslan",
    "age"=>27,
    "mail"=>"hidden",
    "favs" => array(
        "language"=>"php",
        "planet"=>"mercury",
        "city"=>"istanbul")
);

$newdata = array(
    "age"=>28,
    "mail"=>"::delete::",
    "favs" => array(
        "language"=>"js",
        "planet"=>"mercury",
        "city"=>"shanghai")
);

print_r(array_overlay($person,$newdata));
// result: Array ( [name] => Metehan [surname] => Arslan [age] => 28 [favs] => Array ( [language] => js [planet] => mercury [city] => shanghai ) )

function array_overlay($a1,$a2)
{
    foreach($a1 as $k => $v) {
        if ($a2[$k]=="::delete::"){
            unset($a1[$k]);
            continue;
        };
        if(!array_key_exists($k,$a2)) continue;
        if(is_array($v) && is_array($a2[$k])){
            $a1[$k] = array_overlay($v,$a2[$k]);
        }else{
            $a1[$k] = $a2[$k];
        }

    }
    return $a1;
}
?>
rajaimranqamer at gmail dot com 28-May-2014 05:26
to get unique value from multi dimensional array use this instead of array_unique(), because array_unique() does not work on multidimensional:
array_map("unserialize", array_unique(array_map("serialize", $array)));
Hope this will help someone;
Example
$a=array(array('1'),array('2'),array('3'),array('4));
$b=array(array('2'),array('4'),array('6'),array('8));
$c=array_merge($a,$b);
then write this line to get unique values
$c=array_map("unserialize", array_unique(array_map("serialize", $c)));
print_r($c);
vladas dot dirzys at gmail dot com 21-Aug-2013 06:11
To combine several results (arrays):

<?php
$results = array(
    array(
        array('foo1'),
        array('foo2'),
    ),
    array(
        array('bar1'),
        array('bar2'),
    ),
);

$rows = call_user_func_array('array_merge', $results);
print_r($rows);
?>

The above example will output:

Array
(
    [0] => Array
        (
            [0] => foo1
        )

    [1] => Array
        (
            [0] => foo2
        )

    [2] => Array
        (
            [0] => bar1
        )

    [3] => Array
        (
            [0] => bar2
        )

)

However, example below helps to preserve numeric keys:

<?php
$results = array(
    array(
        123 => array('foo1'),
        456 => array('foo2'),
    ),
    array(
        321 => array('bar1'),
        654 => array('bar2'),
    ),
);

$rows = array();
foreach ($results as &$result) {
    $rows = $rows + $result; // preserves keys
}
print_r($rows);
?>

The above example will output:
Array
(
    [123] => Array
        (
            [0] => foo1
        )

    [456] => Array
        (
            [0] => foo2
        )

    [321] => Array
        (
            [0] => bar1
        )

    [654] => Array
        (
            [0] => bar2
        )
)
Janez R. 16-May-2013 05:38
Please note that -1 is a numeric key and will be reindexed.
Less obvious numeric keys are also '-1' (becomes -1) and -1.5 (becomes -1).

'-1a', however, is not a numeric key and you could exploit the fact that '-1a' == -1 evaluates to true in php (just a hint). But as this is not very efficient, operator + may be a better option.

<?php
$arr = array(-1=> "-1", '-2'=> "'-2'", -3.5=> "-3.5", '-4a'=> "'-4a'");
print_r(array(
    $arr,
    array_merge($arr,array()),
    '-1a' == -1,
));
?>

Result:

Array
(
    [0] => Array
        (
            [-1] => -1
            [-2] => '-2'
            [-3] => -3.5
            [-4a] => '-4a'
        )

    [1] => Array
        (
            [0] => -1
            [1] => '-2'
            [2] => -3.5
            [-4a] => '-4a'
        )

    [2] => 1
)
Gemorroj 14-May-2012 05:37
The function behaves differently with numbers more than PHP_INT_MAX
<?php
$arr1 = array('1234567898765432123456789' => 'dd');
$arr2 = array('123456789876543212345678' => 'ddd', '35' => 'xxx');
var_dump(array_merge($arr1, $arr2));
//
$arr1 = array('1234' => 'dd');
$arr2 = array('12345' => 'ddd', '35' => 'xxx');
var_dump(array_merge($arr1, $arr2));
?>
result:
array(3) {
  ["1234567898765432123456789"]=>
  string(2) "dd"
  ["123456789876543212345678"]=>
  string(3) "ddd"
  [0]=>
  string(3) "xxx"
}
array(3) {
  [0]=>
  string(2) "dd"
  [1]=>
  string(3) "ddd"
  [2]=>
  string(3) "xxx"
}
izhar_aazmi at gmail dot com 29-Feb-2012 05:54
The problem that the array keys are reindexed, and further not working on array values of type object or other mixed types.

Simply, I did this:

<?php
foreach($new as $k => $v)
{
    $old[$k] = $v;
}
// This will overwrite all values in OLD with any existing
// matching value in NEW
// And keep all non matching values from OLD intact.
// No reindexing, and complete overwrite
/// Working with all kind of data
?>
alejandro dot anv at gmail dot com 20-Feb-2012 08:26
WARNING: numeric subindexes are lost when merging arrays.

Check this example:

$a=array('abc'=>'abc','def'=>'def','123'=>'123','xyz'=>'xyz');
echo "a=";print_r($a);
$b=array('xxx'=>'xxx');
echo "b=";print_r($b);
$c=array_merge($a,$b);
echo "c=";print_r($c);

The result is this:

c=Array
(
    [abc] => abc
    [def] => def
    [0] => 123
    [xyz] => xyz
    [xxx] => xxx
)
njp AT o2 DOT pl 22-Nov-2011 01:45
Uniques values merge for array_merge function:

<?php
function array_unique_merge() {

        $variables = '$_'.implode(',$_',array_keys(func_get_args()));

        $func = create_function('$tab', ' list('.$variables.') = $tab; return array_unique(array_merge('.$variables.'));');

        return $func(func_get_args());
    }
?>

Doesn't work on objects.

Smarter way for uniques values merge array:

<?php
function array_unique_merge() {

       return array_unique(call_user_func_array('array_merge', func_get_args()));
   }
?>

Example 1:

$a = array(1,2);
$b = array(2,3,4);

array_unique_merge($a,$b);

Return:
array(1,2,3,4);

Example 2:

$a = array(1,2);
$b = array(2,3,4);
$c = array(2,3,4,5);

array_unique_merge($a,$b);

Return:
array(1,2,3,4,5);

Doesn't work on objects.
w_barath at hotmail dot com 05-Oct-2011 01:42
I keep seeing posts for people looking for a function to replace numeric keys.

No function is required for this, it is default behavior if the + operator:

<?php
$a=array(1=>"one", "two"=>2);
$b=array(1=>"two", "two"=>1, 3=>"three", "four"=>4);

print_r($a+$b);
?>

Array
(
    [1] => one
    [two] => 2
    [3] => three
    [four] => 4
)

How this works:

The + operator only adds unique keys to the resulting array.  By making the replacements the first argument, they naturally always replace the keys from the second argument, numeric or not! =)
craig ala craigatx.com 26-Oct-2010 07:25
Reiterating the notes about casting to arrays, be sure to cast if one of the arrays might be null:

<?php
header("Content-type:text/plain");
$a = array('zzzz', 'xxxx');
$b = array('mmmm','nnnn');

echo "1 ==============\r\n";
print_r(array_merge($a, $b));

echo "2 ==============\r\n";
$b = array();
print_r(array_merge($a, $b));

echo "3 ==============\r\n";
$b = null;
print_r(array_merge($a, $b));

echo "4 ==============\r\n";
$b = null;
print_r(array_merge($a, (array)$b));

echo "5 ==============\r\n";
echo is_null(array_merge($a, $b)) ? 'Result is null' : 'Result is not null';
?>

Produces:

1 ==============
Array
(
    [0] => zzzz
    [1] => xxxx
    [2] => mmmm
    [3] => nnnn
)
2 ==============
Array
(
    [0] => zzzz
    [1] => xxxx
)
3 ==============
4 ==============
Array
(
    [0] => zzzz
    [1] => xxxx
)
5 ==============
Result is null
nzujev [AT] gmail [DOT] com 03-Oct-2010 11:28
Be ready to surprise like this one.
array_merge renumbers numeric keys even if key is as string.
keys '1' & '2' will be updated to 0 & 1, but '1.1' & '1.2' remain the same, but they are numeric too (is_numeric('1.1') -> true)

It's better to use '+' operator or to have your own implementation for array_merge.

<?php

$x1 = array (
    '1'     => 'Value 1',
    '1.1'   => 'Value 1.1',
);

$x2 = array (
    '2'     => 'Value 2',
    '2.1'   => 'Value 2.1',
);

$x3 = array_merge( $x1, $x2 );

echo '<pre>NOT as expected: '. print_r( $x3, true ) .'</pre>';

$x3 = $x1 + $x2;

echo '<pre>As expected: '. print_r( $x3, true ) .'</pre>';

?>

NOT as expected: Array
(
    [0] => Value 1
    [1.1] => Value 1.1
    [1] => Value 2
    [2.1] => Value 2.1
)
As expected: Array
(
    [1] => Value 1
    [1.1] => Value 1.1
    [2] => Value 2
    [2.1] => Value 2.1
)
zagadka at evorg dot dk 20-Aug-2010 02:54
Note that if you use + to merge array in order to preserve keys, that in case of duplicates the values from the left array in the addition is used.
jpakin at gmail dot com 11-Jun-2010 08:46
I had a hard time using array_merge with large datasets. By the time my web framework was in memory there wasn't enough space to have multiple copies of my dataset. To fix this I had to remove any functions which operated on the data set and made a copy in memory (pass by value).

I realize the available memory to an application instance is modifiable, but I didn't think I should have to set it below the default 16mb for a web app!

Unfortunately, a number of php functions pass by value internally, so I had to write my own merge function. This passes by reference, utilizes a fast while loop (thus doesn't need to call count() to get an upper boundary, also a php pass by value culprit), and unsets the copy array (freeing memory as it goes).

<?php

function mergeArrays(&$sourceArray, &$copyArray){
        //merge copy array into source array
                $i = 0;
        while (isset($copyArray[$i])){
            $sourceArray[] = $copyArray[$i];
            unset($copyArray[$i]);
            $i++;
        }
    }
?>

This fixed the problem. I would love to know if there is an even faster, more efficient way. Simply adding the two arrays won't work since there is an overlap of index.
nekroshorume at gmail dot com 18-Mar-2010 07:31
be aware there is a slight difference:

<?php
$array1 = array('lang'=>'js','method'=>'GET');
$array2 = array('lang'=>'php','browser'=>'opera','method'=>'POST');
?>

BETWEEN:

<?php array_merge($array2,$array1); ?>
outputs: Array ( [lang] => js [browser] => opera [method] => GET )

notice that the repeated keys will be overwritten by the ones of $array1, maintaining those, because it is the array that is being merged

AND

<?php array_merge($array1,$array2); ?>
outputs: Array ( [lang] => php [method] => POST [browser] => opera )

here the oposite takes place: array1 will have its elements replaced by those of array2.
riddlemd at gmail dot com 22-Jan-2010 09:43
Apparently there is a shorthand that works very much like array_merge. I figured I would leave a comment here, sense I saw it used in code but found nothing about it from google searches.

<?php
$array = array(
 'name' => 'John Doe'
);
$array += array(
 'address' => 'someplace somestreet'
);

echo $array['address']; // Outputs 'someplace somestreet'
?>

A difference from merge_array, it does NOT overwrite existing keys, regardless of key type.
allankelly 02-Oct-2009 05:36
More on the union (+) operator:
the order of arrays is important and does not agree in my test below
with the other posts. The 'unique' operation preserves the initial key-value and discards later duplicates.

PHP 5.2.6-2ubuntu4.2

<?php
$a1=array('12345'=>'a', '23456'=>'b', '34567'=>'c', '45678'=>'d');
$a2=array('34567'=>'X');
$a3=$a1 + $a2;
$a4=$a2 + $a1;
print('a1:'); print_r($a1);
print('a2:'); print_r($a2);
print('a3:'); print_r($a3);
print('a4:'); print_r($a4);
?>

a1:Array
(
    [12345] => a
    [23456] => b
    [34567] => c
    [45678] => d
)
a2:Array
(
    [34567] => X
)
a3:Array
(
    [12345] => a
    [23456] => b
    [34567] => c
    [45678] => d
)
a4:Array
(
    [34567] => X
    [12345] => a
    [23456] => b
    [45678] => d
)
Tudor 27-Sep-2009 10:10
array_merge will merge numeric keys in array iteration order, not in increasing numeric order. For example:

<?php
$a = array(0=>10, 1=>20);  // same as array(10, 20);
$b = array(0=>30, 2=>50, 1=>40);
?>

array_merge($a, $b) will be array(10, 20, 30, 50, 40) and not array(10, 20, 30, 40, 50).
Julian Egelstaff 16-Aug-2009 10:02
More about the union operator (+)...

The "array_unique" that gets applied, is actually based on the keys, not the values.  So if you have multiple values with the same key, only the last one will be preserved.

<?php

$array1[0] = "zero";
$array1[1] = "one";

$array2[0] = 0;
$array1[1] = 1;

$array3 = $array1 + $array2;

// array3 will look like:
// array(0=>0, 1=>1)
// ie: beware of the latter keys overwriting the former keys

?>
Julian Egelstaff 31-Jul-2009 08:48
In some situations, the union operator ( + ) might be more useful to you than array_merge.  The array_merge function does not preserve numeric key values.  If you need to preserve the numeric keys, then using + will do that.

ie:

<?php

$array1[0] = "zero";
$array1[1] = "one";

$array2[1] = "one";
$array2[2] = "two";
$array2[3] = "three";

$array3 = $array1 + $array2;

//This will result in::

$array3 = array(0=>"zero", 1=>"one", 2=>"two", 3=>"three");

?>

Note the implicit "array_unique" that gets applied as well.  In some situations where your numeric keys matter, this behaviour could be useful, and better than array_merge.

--Julian
enaeseth at gmail dot com 20-Jul-2009 11:27
In both PHP 4 and 5, array_merge preserves references in array values. For example:

<?php
$foo = 12;
$array = array("foo" => &$foo);
$merged_array = array_merge($array, array("bar" => "baz"));
$merged_array["foo"] = 24;
assert($foo === 24); // works just fine
?>
jerry at gii dot co dot jp 12-Mar-2009 04:35
There was a previous note that alluded to this, but at least in version 5.2.4 array_merge will return NULL if any of the arguments are NULL. This bit me with $_POST when none of the controls were successful.
carrox at inbox dot lv 01-Sep-2008 01:41
Usage of operand '+' for merging arrays:

<?php

$a=array(
'a'=>'a1',
'b'=>'a2',
'a3',
'a4',
'a5');

$b=array('b1',
'b2',
'a'=>'b3',
'b4');

$a+=$b;

print_r($a);

?>

output:
Array
(
    [a] => a1
    [b] => a2
    [0] => a3
    [1] => a4
    [2] => a5
    [3] => b5
)

numeric keys of elements of array B what not presented in array A was added.

<?php

$a=array('a'=>'a1','b'=>'a2','a3','a4','a5');
$b=array(100=>'b1','b2','a'=>'b3','b4');

$a+=$b;

print_r($a);

?>

output:

   [a] => a1
    [b] => a2
    [0] => a3
    [1] => a4
    [2] => a5
    [100] => b1
    [101] => b2
    [102] => b4

autoindex for array B started from 100, these keys not present in array A, so this elements was added to array A
ron at ronfolio dot com 12-Aug-2008 03:44
I needed a function similar to ian at fuzzygroove's array_interlace, but I need to pass more than two arrays.

Here's my version, You can pass any number of arrays and it will interlace and key them properly.

<?php
function array_interlace() {
    $args = func_get_args();
    $total = count($args);

    if($total < 2) {
        return FALSE;
    }

    $i = 0;
    $j = 0;
    $arr = array();

    foreach($args as $arg) {
        foreach($arg as $v) {
            $arr[$j] = $v;
            $j += $total;
        }

        $i++;
        $j = $i;
    }

    ksort($arr);
    return array_values($arr);
}
?>

Example usage:
<?php
$a = array('a', 'b', 'c', 'd');
$b = array('e', 'f', 'g');
$c = array('h', 'i', 'j');
$d = array('k', 'l', 'm', 'n', 'o');

print_r(array_interlace($a, $b, $c, $d));
?>

result:

Array
(
    [0] => a
    [1] => e
    [2] => h
    [3] => k
    [4] => b
    [5] => f
    [6] => i
    [7] => l
    [8] => c
    [9] => g
    [10] => j
    [11] => m
    [12] => d
    [13] => n
    [14] => o
)

Let me know if you improve on it.
david_douard at hotmail dot com 02-Jul-2008 11:41
To avoid REINDEXING issues,

use + operator :

array_merge(
          array("truc" => "salut"),
          array("machin" => "coucou")
        )

returns

array(2)
{
[0] => string() "salut"
[1] => string() "coucou"
}

whereas

array("truc" => "salut") + array("machin" => "coucou")

returns

array(2)
{
["truc"] => string() "salut"
["machin"] => string() "coucou"
}
bishop 04-Apr-2008 11:02
The documentation is a touch misleading when it says: "If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way."  Even with two arrays, the resulting array is re-indexed:

[bishop@predator staging]$ cat array_merge.php
<?php
$a = array (23 => 'Hello', '42' => 'World');
$a = array_merge(array (0 => 'I say, '), $a);
var_dump($a);
?>
[bishop@predator staging]$ php-5.2.5 array_merge.php
array(3) {
  [0]=>
  string(7) "I say, "
  [1]=>
  string(5) "Hello"
  [2]=>
  string(5) "World"
}
[bishop@predator staging]$ php-4.4.7 array_merge.php
array(3) {
  [0]=>
  string(7) "I say, "
  [1]=>
  string(5) "Hello"
  [2]=>
  string(5) "World"
}
no_one at nobody dot com 03-Mar-2008 05:31
PHP is wonderfully decides if an array key could be a number, it is a number!  And thus wipes out the key when you array merge.   Just a warning.

$array1['4000'] = 'Grade 1 Widgets';
$array1['4000a'] = 'Grade A Widgets';
$array2['5830'] = 'Grade 1 Thing-a-jigs';
$array2['HW39393'] = 'Some other widget';

var_dump($array1);
var_dump($array2);

//results in...
array(2) {
  [4000]=>
  string(15) "Grade 1 Widgets"
  ["4000a"]=>
  string(15) "Grade A Widgets"
}
array(2) {
  [5830]=>
  string(20) "Grade 1 Thing-a-jigs"
  ["HW39393"]=>
  string(17) "Some other widget"
}

var_dump(array_merge($array1,$array2));
//results in...
array(4) {
  [0]=>
  string(15) "Grade 1 Widgets"
  ["4000a"]=>
  string(15) "Grade A Widgets"
  [1]=>
  string(20) "Grade 1 Thing-a-jigs"
  ["HW39393"]=>
  string(17) "Some other widget"
}
Hayley Watson 04-Nov-2007 09:38
As has already been noted before, reindexing arrays is most cleanly performed by the array_values() function.
clancyhood at gmail dot com 22-Jul-2007 01:52
Similar to Jo I had a problem merging arrays (thanks for that Jo you kicked me out of my debugging slumber) - array_merge does NOT act like array_push, as I had anticipated

<?php

$array = array('1', 'hello');
array_push($array, 'world');

var_dump($array);

// gives '1', 'hello', 'world'

$array = array('1', 'hello');
array_merge($array, array('world'));

// gives '1', 'hello'

$array = array('1', 'hello');
$array = array_merge($array, array('world'));

// gives '1', 'hello', 'world'

?>

hope this helps someone
Joe at neosource dot com dot au 04-Jun-2007 08:51
I found the "simple" method of adding arrays behaves differently as described in the documentation in PHP v5.2.0-10.

$array1 + $array2 will only combine entries for keys that don't already exist.

Take the following example:

$ar1 = array('a', 'b');
$ar2 = array('c', 'd');
$ar3 = ($ar1 + $ar2);
print_r($ar3);

Result:

Array
(
    [0] => a
    [1] => b
)

Where as:

$ar1 = array('a', 'b');
$ar2 = array('c', 'd');
$ar3 = array_merge($ar1, $ar2);
print_r($ar3);

Result:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

Hope this helps someone.
RQuadling at GMail dot com 21-May-2007 02:05
For asteddy at tin dot it and others who are trying to merge arrays and keep the keys, don't forget the simple + operator.

Using the array_merge_keys() function (with a small mod to deal with multiple arguments), provides no difference in output as compared to +.

<?php
$a = array(-1 => 'minus 1');
$b = array(0 => 'nought');
$c = array(0 => 'nought');
var_export(array_merge_keys($a,$b));
var_export($a + $b);
var_export(array_merge_keys($a,$b,$c));
var_export($a + $b + $c);
?>

results in ...

array (  -1 => 'minus 1',  0 => 'nought',)
array (  -1 => 'minus 1',  0 => 'nought',)
array (  -1 => 'minus 1',  0 => 'nought',)
array (  -1 => 'minus 1',  0 => 'nought',)
Marce! 02-Mar-2007 01:21
I have been searching for an in-place merge function, but couldn't find one. This function merges two arrays, but leaves the order untouched.
Here it is for all others that want it:

function inplacemerge($a, $b) {
  $result = array();
  $i = $j = 0;
  if (count($a)==0) { return $b; }
  if (count($b)==0) { return $a; }
  while($i < count($a) && $j < count($b)){
    if ($a[$i] <= $b[$j]) {
      $result[] = $a[$i];
      if ($a[$i]==$b[$j]) { $j++; }
      $i++;
    } else {
      $result[] = $b[$j];
      $j++;
    }
  }
  while ($i<count($a)){
    $result[] = $a[$i];
    $i++;
  }
  while ($j<count($b)){
    $result[] = $b[$j];
    $j++;
  }
  return $result;
}
ahigerd at stratitec dot com 24-Jan-2007 03:06
An earlier comment mentioned that array_splice is faster than array_merge for inserting values. This may be the case, but if your goal is instead to reindex a numeric array, array_values() is the function of choice. Performing the following functions in a 100,000-iteration loop gave me the following times: ($b is a 3-element array)

array_splice($b, count($b)) => 0.410652
$b = array_splice($b, 0) => 0.272513
array_splice($b, 3) => 0.26529
$b = array_merge($b) => 0.233582
$b = array_values($b) => 0.151298
zspencer at zacharyspencer dot com 09-Nov-2006 05:55
I noticed the lack of a function that will safely merge two arrays without losing data due to duplicate keys but different values.

So I wrote a quicky that would offset duplicate keys and thus preserve their data. of course, this does somewhat mess up association...

<?php
$array1=array('cats'=>'Murder the beasties!', 'ninjas'=>'Use Ninjas to murder cats!');
$array2=array('cats'=>'Cats are fluffy! Hooray for Cats!', 'ninjas'=>'Ninas are mean cat brutalizers!!!');
$array3=safe_array_merge($array1, $array2);
print_r($array3)
/*
Array {
cats => Murder the beasties!,
ninjas => Use ninjas to murder cats!,

?>
cats_0 => Cats are fluffy! Hooray for Cats!,
ninjas_0 => Ninjas are mean cat brutalizers!!!
}

function safe_array_merge ()
{
    $args = func_get_args();
    $result=array();
    foreach($args as &$array)
    {
        foreach($array as $key=>&$value)
        {
            if(isset($result[$key]))
            {
                $continue=TRUE;
                $fake_key=0;
                while($continue==TRUE)
                {
                    if(!isset($result[$key.'_'.$fake_key]))
                    {
                        $result[$key.'_'.$fake_key]=$value;
                        $continue=FALSE;
                    }
                    $fake_key++;
                }
            }
            else
            {
                $result[$key]=$value;
            }
        }
    }
    return $result;
}
?>
01-Nov-2006 03:47
A more efficient array_merge that preserves keys, truly accepts an arbitrary number of arguments, and saves space on the stack (non recursive):
<?php
function array_merge_keys(){
    $args = func_get_args();
    $result = array();
    foreach($args as &$array){
        foreach($array as $key=>&$value){
            $result[$key] = $value;
        }
    }
    return $result;
}
?>
BigueNique at yahoo dot ca 29-Sep-2006 04:10
Needed an quick array_merge clone that preserves the keys:

<?php
// function array_join
// merges 2 arrays preserving the keys,
// even if they are numeric (unlike array_merge)
// if 2 keys are identical, the last one overwites
// the existing one, just like array_merge
// merges up to 10 arrays, minimum 2.
function array_join($a1, $a2, $a3=null, $a4=null, $a5=null, $a6=null, $a7=null, $a8=null, $a9=null, $a10=null) {
    $a=array();
    foreach($a1 as $key=>$value) $a[$key]=$value;
    foreach($a2 as $key=>$value) $a[$key]=$value;
    if (is_array($a3)) $a=array_join($a,$a3,$a4,$a5,$a6,$a7,$a8,$a9,$a10);
    return $a;
}
?>
poison 12-Sep-2006 02:45
You can use array_slice() in combination with array_merge() to insert values into an array like this:

<?php
$test=range(0, 10);
$index=2;
$data="---here---";
$result=array_merge(array_slice($test, 0, $index), array($data), array_slice($test, $index));
var_dump($result);
?>
dercsar at gmail dot com 04-Sep-2006 10:34
array_merge() overwrites ALL numerical indexes. No matter if you have non-numerical indexes or more than just one array.
It reindexes them all. Period.

(Only tried in 4.3.10)
anonyme 20-Jul-2006 03:02
I don't think that the comment on + operator for array in array_merge page, was understandable, this is just a little test to know exactly what's happend.

<?php
//test code for (array)+(array) operator
$a1 = array( '10', '11' , '12' , 'a' => '1a', 'b' => '1b');
$a2 = array( '20', '21' , '22' , 'a' => '2a', 'c' => '2c');

$a = $a1 + $a2;
print_r( $a );
//result: Array ( [0] => 10
//                [1] => 11
//                [2] => 12
//                [a] => 1a
//                [b] => 1b
//                [c] => 2c )

$a = $a2 + $a1;
print_r( $a );
//result: Array ( [0] => 20
//                [1] => 21
//                [2] => 22
//                [a] => 2a
//                [c] => 2c
//                [b] => 1b )

?>
chris at luethy dot net 13-Jul-2006 02:36
Do not use this to set the $_SESSION variable.

$_SESSION = array_merge( $_SESSION, $another_array );

will break your $_SESSION until the end of the execution of that page.
hs at nospam dot magnum-plus dot com 29-Dec-2005 11:50
The same result as produced by snookiex_at_gmail_dot_com's function
can be achieved with the 'one-liner'
<?php
$array1=array(1,2,3);
$array2=array('a','b','c');

$matrix = array_map(null, $array1, $array2);
?>
(see documentation of array_map).
The difference here is, that the shorter array gets filled with empty values.
kaigillmann at gmxpro dot net 02-Nov-2005 12:56
If you need to merge two arrays without having multiple entries, try this:

<?php
function array_fusion($ArrayOne, $ArrayTwo)
{
    return array_unique(array_merge($ArrayOne, $ArrayTwo));
}
?>
php at moechofe dot com 20-Jul-2005 01:58
<?php

  /*
  * array_deep_merge
  *
  * array array_deep_merge ( array array1 [, array array2 [, array ...]] )
  *
  * Like array_merge
  *
  *   array_deep_merge() merges the elements of one or more arrays together so
  * that the values of one are appended to the end of the previous one. It
  * returns the resulting array.
  *   If the input arrays have the same string keys, then the later value for
  * that key will overwrite the previous one. If, however, the arrays contain
  * numeric keys, the later value will not overwrite the original value, but
  * will be appended.
  *   If only one array is given and the array is numerically indexed, the keys
  * get reindexed in a continuous way.
  *
  * Different from array_merge
  *   If string keys have arrays for values, these arrays will merge recursively.
  */
  function array_deep_merge()
  {
    switch( func_num_args() )
    {
      case 0 : return false; break;
      case 1 : return func_get_arg(0); break;
      case 2 :
        $args = func_get_args();
        $args[2] = array();
        if( is_array($args[0]) and is_array($args[1]) )
        {
          foreach( array_unique(array_merge(array_keys($args[0]),array_keys($args[1]))) as $key )
          if( is_string($key) and is_array($args[0][$key]) and is_array($args[1][$key]) )
            $args[2][$key] = array_deep_merge( $args[0][$key], $args[1][$key] );
          elseif( is_string($key) and isset($args[0][$key]) and isset($args[1][$key]) )
            $args[2][$key] = $args[1][$key];
          elseif( is_integer($key) and isset($args[0][$key]) and isset($args[1][$key]) ) {
            $args[2][] = $args[0][$key]; $args[2][] = $args[1][$key]; }
          elseif( is_integer($key) and isset($args[0][$key]) )
            $args[2][] = $args[0][$key];
          elseif( is_integer($key) and isset($args[1][$key]) )
            $args[2][] = $args[1][$key];
          elseif( ! isset($args[1][$key]) )
            $args[2][$key] = $args[0][$key];
          elseif( ! isset($args[0][$key]) )
            $args[2][$key] = $args[1][$key];
          return $args[2];
        }
        else return $args[1]; break;
      default :
        $args = func_get_args();
        $args[1] = array_deep_merge( $args[0], $args[1] );
        array_shift( $args );
        return call_user_func_array( 'array_deep_merge', $args );
        break;
    }
  }

  /*
  * test
  */
  $a = array(
    0,
    array( 0 ),
    'integer' => 123,
    'integer456_merge_with_integer444' => 456,
    'integer789_merge_with_array777' => 789,
    'array' => array( "string1", "string2" ),
    'array45_merge_with_array6789' => array( "string4", "string5" ),
    'arraykeyabc_merge_with_arraykeycd' => array( 'a' => "a", 'b' => "b", 'c' => "c" ),
    'array0_merge_with_integer3' => array( 0 ),
    'multiple_merge' => array( 1 ),
  );

  $b = array(
    'integer456_merge_with_integer444' => 444,
    'integer789_merge_with_array777' => array( 7,7,7 ),
    'array45_merge_with_array6789' => array( "string6", "string7", "string8", "string9" ),
    'arraykeyabc_merge_with_arraykeycd' => array( 'c' => "ccc", 'd' => "ddd" ),
    'array0_merge_with_integer3' => 3,
    'multiple_merge' => array( 2 ),
  );

  $c = array(
    'multiple_merge' => array( 3 ),
  );

  echo "<pre>".htmlentities(print_r( array_deep_merge( $a, $b, $c ), true))."</pre>";

?>
ntpt at centrum dot cz 14-Jul-2005 03:44
Old behavior of array_merge can be restored by simple  variable type casting like this

array_merge((array)$foo,(array)$bar);

works good in php 5.1.0 Beta 1, not tested in other versions

seems that empty or not set variables are casted to empty arrays
Alec Solway 20-Jan-2005 07:05
Note that if you put a number as a key in an array, it is eventually converted to an int even if you cast it to a string or put it in quotes.

That is:

$arr["0"] = "Test";
var_dump( key($arr) );

will output int(0).

This is important to note when merging because array_merge will append values with a clashing int-based index instead of replacing them. This kept me tied up for hours.
nospam at veganismus dot ch 11-Jan-2005 03:38
Someone posted a function with the note:
"if u need to overlay a array that holds defaultvalues with another that keeps the relevant data"

<?
//about twice as fast but the result is the same.
//note: the sorting will be messed up!
function array_overlay($skel, $arr) {
    return $arr+$skel;
}

//example:
$a = array("zero","one","two");
$a = array_overlay($a,array(1=>"alpha",2=>NULL));
var_dump($a);
/* NULL is ignored so the output is:
array(3) {
  [1]=>
  string(5) "alpha"
  [0]=>
  string(4) "zero"
  [2]=>
  string(3) "two"
}
*/
?>
Frederick.Lemasson{AT}kik-it.com 23-Dec-2004 11:57
if you generate form select from an array, you probably want to keep your array keys and order intact,
if so you can use ArrayMergeKeepKeys(), works just like array_merge :

array ArrayMergeKeepKeys ( array array1 [, array array2 [, array ...]])

but keeps the keys even if of numeric kind.
enjoy

<?

$Default[0]='Select Something please';

$Data[147]='potato';
$Data[258]='banana';
$Data[54]='tomato';

$A=array_merge($Default,$Data);

$B=ArrayMergeKeepKeys($Default,$Data);

echo '<pre>';
print_r($A);
print_r($B);
echo '</pre>';

Function ArrayMergeKeepKeys() {
      $arg_list = func_get_args();
      foreach((array)$arg_list as $arg){
          foreach((array)$arg as $K => $V){
              $Zoo[$K]=$V;
          }
      }
    return $Zoo;
}

//will output :

Array
(
    [0] => Select Something please
    [1] => potato
    [2] => banana
    [3] => tomato
)
Array
(
    [0] => Select Something please
    [147] => potato
    [258] => banana
    [54] => tomato
)

?>
bcraigie at bigfoot dot com 24-Aug-2004 09:37
It would seem that array_merge doesn't do anything when one array is empty (unset):

<?php //$a is unset
$b = array("1" => "x");

$a = array_merge($a, $b});

//$a is still unset.
?>

to fix this omit $a if it is unset:-

<?php
if(!IsSet($a)) {
    $a = array_merge($b);
} else {
  $a = array_merge($a, $b);
}
?>

I don't know if there's a better way.
frankb at fbis dot net 25-Feb-2004 01:02
to merge arrays and preserve the key i found the following working with php 4.3.1:

<?php
$array1 = array(1 => "Test1", 2 => "Test2");
$array2 = array(3 => "Test3", 4 => "Test4");

$array1 += $array2;
?>

dont know if this is working with other php versions but it is a simple and fast way to solve that problem.
rcarvalhoREMOVECAPS at clix dot pt 03-Nov-2003 09:43
While searching for a function that would renumber the keys in a array, I found out that array_merge() does this if the second parameter is null:

Starting with array $a like:

<?php
Array
(
    [5] => 5
    [4] => 4
    [2] => 2
    [9] => 9
)
?>

Then use array_merge() like this:

<?php
$a = array_merge($a, null);
?>

Now the $a array has bee renumbered, but maintaining the order:

<?php
Array
(
    [0] => 5
    [1] => 4
    [2] => 2
    [3] => 9
)
?>

Hope this helps someone :-)
loner at psknet dot !NOSPAM!com 30-Sep-2003 12:00
I got tripped up for a few days when I tried to merge a (previously serialized) array into a object. If it doesn't make sense, think about it... To someone fairly new, it could... Anyway, here is what I did:
(It's obviously not recursive, but easy to make that way)
<?php
function array_object_merge(&$object, $array) {
    foreach ($array as $key => $value)
        $object->{$key} = $value;
}
?>

Simple problem, but concevibly easy to get stuck on.
04-Sep-2003 12:10
For those who are getting duplicate entries when using this function, there is a very easy solution:

wrap array_unique() around array_merge()

cheers,

k.
tobias_mik at hotmail dot com 25-Jul-2003 02:14
This function merges any number of arrays and maintains the keys:

<?php
function array_kmerge ($array) {
 reset($array);
 while ($tmp = each($array))
 {
  if(count($tmp['value']) > 0)
  {
   $k[$tmp['key']] = array_keys($tmp['value']);
   $v[$tmp['key']] = array_values($tmp['value']);
  }
 }
 while($tmp = each($k))
 {
  for ($i = $start; $i < $start+count($tmp['value']); $i ++)$r[$tmp['value'][$i-$start]] = $v[$tmp['key']][$i-$start];
  $start = count($tmp['value']);
 }
 return $r;
}
?>


여러 배열이나 다차원 배열 정렬

  • ≪ array_merge
  • array_pad ≫
  • PHP Manual
  • 배열 함수 목록
  • 여러 배열이나 다차원 배열 정렬

array_multisort

(PHP 4, PHP 5, PHP 7)

array_multisort — 여러 배열이나 다차원 배열 정렬

설명

bool array_multisort ( array $arr [, mixed $arg [, mixed $... ]] )

array_multisort()는 여러 배열을 한번에 정렬하거나, 1 이상의 차원을 가진 배열을 정렬할 수 있습니다.

연관(string)키는 유지되지만, 숫자 키는 재인덱스됩니다.

인수

arr

정렬할 array.

arg

선택적인 다른 array이나, 이전 array 인수에 적용할 정렬 옵션: SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING.

...

추가적인 arg들.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 여러 배열 정렬하기

<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);
?>

이 예제에서, 정렬 후에 첫번째 배열은 0, 10, 100, 100을 가집니다. 두번째 배열은 4, 1, 2, 3을 가집니다. 두번째 배열의 원소는 첫번째 배열의 원소 순서에 해당하므로, (100과 100)도 정렬되어졌습니다.

array(4) {
  [0]=> int(0)
  [1]=> int(10)
  [2]=> int(100)
  [3]=> int(100)
}
array(4) {
  [0]=> int(4)
  [1]=> int(1)
  [2]=> int(2)
  [3]=> int(3)
}

Example #2 다차원 배열 정렬하기

<?php
$ar = array(
       array("10", 11, 100, 100, "a"),
       array(   1,  2, "2",   3,   1)
      );
array_multisort($ar[0], SORT_ASC, SORT_STRING,
                $ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>

이 예제에서, 정렬 후에 첫번째 배열은 "10", 100, 100, 11, "a"로 바뀝니다. (문자열 오름차순으로 정렬) 두번째 배열은 1, 3, "2", 2, 1을 가집니다. (숫자 내림차순으로 정렬)

array(2) {
  [0]=> array(5) {
    [0]=> string(2) "10"
    [1]=> int(100)
    [2]=> int(100)
    [3]=> int(11)
    [4]=> string(1) "a"
  }
  [1]=> array(5) {
    [0]=> int(1)
    [1]=> int(3)
    [2]=> string(1) "2"
    [3]=> int(2)
    [4]=> int(1)
  }
}

Example #3 데이터베이스 결과 정렬하기

이 예제에서, data 배열의 각 원소는 테이블 안의 한 행을 표현합니다. 이런 자료집합은 전형적인 데이터베이스 기록입니다.

예제 데이터:

volume | edition
-------+--------
    67 |       2
    86 |       1
    85 |       6
    98 |       2
    86 |       6
    67 |       7

데이터 배열을 data이라 부릅니다. 일반적인 예를 들면, mysql_fetch_assoc()로 반복하여 얻을 수 있습니다.

<?php
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
?>

이 예제에서, volume 내림차순, edition 오름차순으로 정렬합니다.

행의 배열을 가지고 있지만, array_multisort()는 열의 배열을 요구하므로, 아래 코드를 사용해서 열을 얻어서, 정렬을 수행합니다.

<?php
// 열 목록 얻기
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}

// volume 내림차순, edition 오름차순으로 데이터를 정렬
// 공통 키를 정렬하기 위하여 $data를 마지막 인수로 추가
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>

자료집합은 정렬되어, 다음과 같이 보여집니다:

volume | edition
-------+--------
    98 |       2
    86 |       1
    86 |       6
    85 |       6
    67 |       2
    67 |       7

Example #4 대소문자 구분 없는 정렬

SORT_STRING과 SORT_REGULAR 모두 대소문자를 구분하므로, 대문자로 시작하는 문자열이 소문자로 시작하는 문자열보다 앞에 나옵니다.

대소문자 구분 없이 검색을 수행하려면, 정렬 순서를 원 배열의 소문자 사본으로 이루어지게 해야 합니다.

<?php
$array = array('Alpha', 'atomic', 'Beta', 'bank');
$array_lowercase = array_map('strtolower', $array);

array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);

print_r($array);
?>

위 예제의 출력:

Array
(
    [0] => Alpha
    [1] => atomic
    [2] => bank
    [3] => Beta
)

User Contributed Notes

faugeron dot loic at gmail dot com 17-May-2016 04:14
Improving Example #3 (Sorting database results): using <?php array_column ?> (PHP >= 5.5) and <?php call_user_func_array ?> it becomes possible to build your sortings (sorting by one or many fields):

<?php

$data = [
    [
        'id' => '168ac7f8-c918-4e99-90ee-5d7590fe61ce',
        'name' => 'Arthur Dent',
    ],
    [
        'id' => 'e3ad45ee-7cae-4cca-bd7b-2eb6b57b6457',
        'name' => 'Ford Prefect',
    ],
    [
        'id' => 'a426aef2-19e2-412a-8339-5458cf6ae416',
        'name' => 'Trillian Astra',
    ],
];
$sortings = [
    [
        'field' => 'id',
        'direction' => SORT_DESC,
    ],
];

$args = [];
$key = 0;
foreach ($sortings as $sorting) {
    $args[$key] = array_column($data, $sorting['field']);
    $args[$key + 1] = $sorting['direction'];
    $key += 2;
}
$args[] = $data;
call_user_func_array('array_multisort', $args);

// $data is now sorted by ID in descending order.
?>
matt at bosc dot io 06-May-2016 06:02
One-liner function to sort multidimensionnal array by key, thank's to array_column

<?php

array_multisort (array_column($array, 'key'), SORT_DESC, $array);

?>
intsystem88 at gmail dot com 25-Oct-2015 10:03
See (rus): https://intsystem.org/coding/sortirovka-mnogomernyh-massivov-po-klyuchu-na-php/

Simple example:

<?php
$data=array(
    array('text'=>'str1', 'year'=>'2010', 'author'=>10),
    array('text'=>'str2', 'year'=>'2011', 'author'=>10),
    array('text'=>'str3', 'year'=>'2009', 'author'=>20),
    array('text'=>'str4', 'year'=>'2010', 'author'=>30),
    array('text'=>'str5', 'year'=>'2010', 'author'=>20),
    array('text'=>'str6', 'year'=>'2011', 'author'=>10),
    array('text'=>'str7', 'year'=>'2011', 'author'=>20),
    array('text'=>'str8', 'year'=>'2009', 'author'=>20),
);

$data_year=array();

foreach($data as $key=>$arr){
    $data_year[$key]=$arr['year'];
}

for($i=0; $i<100000; $i++){
    $data_tmp=$data;
    array_multisort($data_year, SORT_NUMERIC, $data_tmp);
}
?>
nospam at nospam dot com 24-Oct-2015 07:30
Super easy and simple way to sort a keyed multiarray while maintaining all associative keys, including numeric!

Preserves the original multiarray order if the sorting values are equal.

<?php
// sorts multiarray by a subarray value while preserving all keys, also preserves original order when the sorting values match
function maSort($ma = '', $sortkey = '', $sortorder = 1) { // sortorder: 1=asc, 2=desc
  if ($ma && is_array($ma) && $sortkey) { // confirm inputs
    foreach ($ma as $k=>$a) $temp["$a[$sortkey]"][$k] = $a; // temp ma with sort value, quotes convert key to string in case numeric float
    if ($sortorder == 2) { // descending
      krsort($temp);
    } else { // ascending
      ksort($temp);
    }
    $newma = array(); // blank output multiarray to add to
    foreach ($temp as $sma) $newma += $sma; // add sorted arrays to output array
    unset($ma, $sma, $temp); // release memory
    return $newma;
  }
}
?>
matt at wennersten dot org 29-Jul-2015 06:09
Many users have contributed nifty wrapper functions for generalizing array_multisort. However, some of us may just want a simple example that illustrates how to sort an arbitrary multi-dimensional array, such as a database result set, on a named field.

Consider an array of voice recordings (Clip objects) that are stored on a file system, and referenced via a database table:

    $fetchClipQuery = "SELECT * from voiceclips";
    if ( !$result = $this->db->query($fetchClipQuery) ) {
            $errormessage = $this->db->errno;
            $errormessage .= $this->db->error;
        } else {
            while($row = $result->fetch_assoc()){
                $clip = new Clip();
                $clip->populate($row['clipURL']);
                $clips[] = array("speaker"=>$row['speaker'],"duration"=>$row['duration'],"clip"=>$clip);
            }

//we now have an array called clips[] that looks like this:

array("speaker"=>"Obama","duration"=>"22:00","clip"=>...<some arbitrary object>...);

        //suppose we want to sort the array by speaker
        $sortkeyname = "speaker";
        //do the sort
        $sortkeyarray = array();
        foreach ($clips as $key => $row){
            $sortkeyarray[$key] = $row[$sortkeyname];
        }
        array_multisort($sortkeyarray, SORT_DESC, $clips);

           //suppose we want to sort the array by duration
        $sortkeyname = "duration";
        //do the sort
        $sortkeyarray = array();
        foreach ($clips as $key => $row){
            $sortkeyarray[$key] = $row[$sortkeyname];
        }
        array_multisort($sortkeyarray, SORT_DESC, $clips);

...etc.
jcharpentier at darkmira dot fr 24-Feb-2014 02:11
There is a lack of precision for the second example :

Example #2 Sorting multi-dimensional array

The explanation is :
"In this example, [...] The second will contain 1, 3, "2", 2, 1 (sorted as numbers, in descending order). "
This could be misunderstood cause a sort as numbers in descending order will be 1, 1, "2", 2, 3.

My proposal is as follows (in a best english should be great ^^) :
"In this example, [...] The second will contain 1, 3, "2", 2, 1 (sorted as well as first one, except for values 3 and "2" sorted as numbers, in descending order).  Because they are corresponding to the identical entries in the first array (100 and 100) which couldn't be sorted at first time."
Robert C 09-Jan-2014 09:11
Hi,

I would like to see the next code snippet to be added to http://nl3.php.net/array_multisort

Purpose: Sort a 2-dimensional array on some key(s)

Advantage of function:
- uses PHP's array_multisort function for sorting;
- it prepares the arrays (needed by array_multisort) for you;
- allows the sort criteria be passed as a separate array (It is possible to use sort order and flags.);
- easy to set/overwrite the way strings are sorted (case insensitive instead of case sensitive, which is PHP's default way of sorting);
- performs excellent

function MultiSort($data, $sortCriteria, $caseInSensitive = true)
{
  if( !is_array($data) || !is_array($sortCriteria))
    return false;
  $args = array();
  $i = 0;
  foreach($sortCriteria as $sortColumn => $sortAttributes)
  {
    $colList = array();
    foreach ($data as $key => $row)
    {
      $convertToLower = $caseInSensitive && (in_array(SORT_STRING, $sortAttributes) || in_array(SORT_REGULAR, $sortAttributes));
      $rowData = $convertToLower ? strtolower($row[$sortColumn]) : $row[$sortColumn];
      $colLists[$sortColumn][$key] = $rowData;
    }
    $args[] = &$colLists[$sortColumn];

    foreach($sortAttributes as $sortAttribute)
    {
      $tmp[$i] = $sortAttribute;
      $args[] = &$tmp[$i];
      $i++;
     }
  }
  $args[] = &$data;
  call_user_func_array('array_multisort', $args);
  return end($args);
}

Usage:

//Fill an array with random test data
define('MAX_ITEMS', 15);
define('MAX_VAL', 20);
for($i=0; $i < MAX_ITEMS; $i++)
  $data[] = array('field1' => rand(1, MAX_VAL), 'field2' => rand(1, MAX_VAL), 'field3' => rand(1, MAX_VAL) );

//Set the sort criteria (add as many fields as you want)
$sortCriteria =
  array('field1' => array(SORT_DESC, SORT_NUMERIC),
       'field3' => array(SORT_DESC, SORT_NUMERIC)
  );

//Call it like this:
$sortedData = MultiSort($data, $sortCriteria, true);
Patrick Adrichem 17-Oct-2013 05:08
If you do not have PHP 5.4 installed yet and you cannot use SORT_NATURAL. This function sorts arrays natural multi-dimensional based on key value

this function can be used for arrays as

array ( name => array( key => value ) )
and

array( name => value ).

arrays as array( name => array( key => value), name => value) are not supported.

<?php
        static function natcasesortRecursive(&$aArray)
        {
            $bHasArrays = false;
            foreach ($aArray as $sKey => &$mValue)
            {
                if (true === is_array($mValue))
                {
                    self::natcasesortRecursive($mValue);
                    $bHasArrays = true;
                }
            }
            if (true === $bHasArrays)
            {
                uksort($aArray, 'strnatcasecmp');
            }
            else
            {
                natcasesort($aArray);
            }
        }
?>
Magento-User 12-Oct-2013 12:25
When sorting an array of (complex) objects, this function can give you a "Fatal error:  Nesting level too deep" since it directly compares elements in later arrays if the elements in earlier ones compare equal. This can be worked around with the Flag-Parameter:
<?php
$sortKeys = array_map($extractKey, $lotsOfComplexObjects);
array_multisort($sortKeys, $lotsOfComplexObjects, SORT_ASC, SORT_NUMERIC);
?>
I'm replacing an 'uasort()'-call which is significantly slower since it leads to a lot of calls to the comparison-function but most of the objects involved are recursive.

If this 'trick' gives a wrong order,  you need a better key.
srijishks at gmail dot com 23-Apr-2013 03:24
The size should be same for all sub arrays
plugboard at web dot de 04-Sep-2012 11:56
multisort an Array of Objects:

example object [$object with array of objects]: (class: test)
----------------------------------

test Object (
  [Artikel] => Array (
      [0] => test Object (
            [id] => 1
            [title] => CCCC
         )
      [1] => test Object (
            [id] => 2
            [title] => AAAA
         )
      [2] => test Object (
            [id] => 3
            [title] => DDDD
         )
      [3] => test Object (
            [id] => 4
            [title] => BBBB
         )
   )
)

----------------------------------

Simple PHP function: sort_arr_of_obj()

<?php
// --------------------------------------

/*
* -------- function arguments --------
*   $array ........ array of objects
*   $sortby ....... the object-key to sort by
*   $direction ... 'asc' = ascending
* --------
*/

function sort_arr_of_obj($array, $sortby, $direction='asc') {

    $sortedArr = array();
    $tmp_Array = array();

    foreach($array as $k => $v) {
        $tmp_Array[] = strtolower($v->$sortby);
    }

    if($direction=='asc'){
        asort($tmp_Array);
    }else{
        arsort($tmp_Array);
    }

    foreach($tmp_Array as $k=>$tmp){
        $sortedArr[] = $array[$k];
    }

    return $sortedArr;

}

// --------------------------------------
?>

example call:
----------------------------------

<?php

$sorted->Artikel = sort_arr_of_obj($object->Artikel,'title','asc');

?>

example result: $sorted (class: test)
----------------------------------

test Object (
  [Artikel] => Array (
      [0] => test Object (
            [id] => 2
            [title] => AAAA
         )
      [1] => test Object (
            [id] => 4
            [title] => BBBB
         )
      [2] => test Object (
            [id] => 1
            [title] => CCCC
         )
      [3] => test Object (
            [id] => 3
            [title] => DDDD
         )

   )
)

-------------------------

:)
rnacken at gmail dot com 12-Feb-2012 08:59
When I was working on a search engine, that had to order the results in PHP by multiple arguments, I got stuck on the issue of multisort erasing your (numeral) indexes for a while. Sometimes, it is important to keep these indexes intact. In my case, the indexes were IDs and the values were a percentage of how relevant the object was, considering an earlier query.
e.g: $searchResult = (23 => 0.3,
                    102 => 0.5,
                    11 => 0.5,
                    340 => 0.5,
                    10 => 0.9);

I wanted to use array_multisort to first sort DESC on the IDs, and then on the values DESC. i.e. I wanted to show the highest values first, but in case of two (or more) objects with the same value, the higher ID would be shown first.
e.g: $searchResult = (10 => 0.9,
                   340 => 0.5,
                   102 => 0.5,
                   11 => 0.5,
                   23 => 0.3);

The easiest way to do this, I think, is:
<?php
    // create a 2-deep array with the values and keys of $searchResult
    $array = array(
               $searchResult,
               array_keys($searchResult)
    );
    // use multisort, first on the values, then on the keys. This will erase the indexes in the $searchResult array
    array_multisort($array[0], SORT_DESC,  $array[1], SORT_DESC);
    // get the ordered keys back in the $searchResult array
    $searchResult = array_combine($array[1], $array[0]);
    unset($array); //clear some memory
?>
nick ([AT]) nickyost ([DOT]) com 16-Sep-2011 12:36
USort function can be used to sort multidimensional arrays with almost no work whatsoever by using the individual values within the custom sort function.

This function passes the entire child element even if it is not a string. If it is an array, as would be the case in multidimensional arrays, it will pass the whole child array as one parameter.

Therefore, do something elegant like this:

<?php
     // Sort the multidimensional array
     usort($results, "custom_sort");
     // Define the custom sort function
     function custom_sort($a,$b) {
          return $a['some_sub_var']>$b['some_sub_var'];
     }
?>

This does in 4 lines what other functions took 40 to 50 lines to do. This does not require you to create temporary arrays or anything. This is, for me, a highly preferred solution over this function.

Hope it helps!
info at ichier dot de 28-Jul-2011 05:59
if you want to sort an array by columns, this is the function to do it.

<?php
function array_csort() {        //coded by Ichier2003
    $args = func_get_args();
    $marray = array_shift($args);
    $msortline = 'return(array_multisort(';
    foreach ($args as $arg) {
        $i++;
        if (is_string($arg)) {
            foreach ($marray as $row) {
                $sortarr[$i][] = $row[$arg];
            }
        } else {
            $sortarr[$i] = $arg;
        }
        $msortline .= '$sortarr['.$i.'],';
    }
    $msortline .= '$marray));';
    eval($msortline);
    return $marray;
}
?>
thierryzo at hotmail dot fr 29-Jun-2011 11:27
//A very simple way to sort arrays with this kind of structure.

<?php
    $myArray =array(
        array("NUMCIE" => "001","REF" => "RXL","COLOR" => "RED","L1" => 4),
        array("NUMCIE" => "001","REF" => "RXL","COLOR" => "BLUE","L1" => 6),
        array("NUMCIE" => "001","REF" => "RHQ","COLOR" => "RED","L1" => 4),
        array("NUMCIE" => "002","REF" => "RXL","COLOR" => "YELLOW","L1" => 8));

    foreach($myArray as $c=>$key) {
        $sort_numcie[] = $key['NUMCIE'];
        $sort_ref[] = $key['REF'];
        $sort_color[] = $key['COLOR'];
    }

    array_multisort($sort_numcie, SORT_ASC, $sort_ref, SORT_STRING, $myArray);
    print_r($myArray);

?>

//Result array

Array
(
    [0] => Array
        (
            [NUMCIE] => 001
            [REF] => RHQ
            [COLOR] => RED
            [L1] => 4
        )

    [1] => Array
        (
            [NUMCIE] => 001
            [REF] => RXL
            [COLOR] => BLUE
            [L1] => 6
        )

    [2] => Array
        (
            [NUMCIE] => 001
            [REF] => RXL
            [COLOR] => RED
            [L1] => 4
        )

    [3] => Array
        (
            [NUMCIE] => 002
            [REF] => RXL
            [COLOR] => YELLOW
            [L1] => 8
        )

)
Jaak Tamre 22-Mar-2011 03:42
I had a problem with sorting SimpleXMLElement list. I found a nice short solution.

I have an array of ticket objects: $ticketList = $xml->Tickets;

<?php
// Sort by ValidOnDate
$tickets = array();
$valid = array();
foreach ($ticketList as $row) {
    $tickets[] = $row;
    $valid[] = DateHandler::parseDate($row->ValidOnDate);
}
array_multisort($valid, SORT_DESC, $tickets);
?>

Just make an array of object items you want to sort by and a new array for the same objects which will be reordered like the first array.

Ps: my own parseDate(date) returns unix timestamp for different date formats.
jimpoz at jimpoz dot com 21-Oct-2010 11:26
I came up with an easy way to sort database-style results. This does what example 3 does, except it takes care of creating those intermediate arrays for you before passing control on to array_multisort().

<?php
function array_orderby()
{
    $args = func_get_args();
    $data = array_shift($args);
    foreach ($args as $n => $field) {
        if (is_string($field)) {
            $tmp = array();
            foreach ($data as $key => $row)
                $tmp[$key] = $row[$field];
            $args[$n] = $tmp;
            }
    }
    $args[] = &$data;
    call_user_func_array('array_multisort', $args);
    return array_pop($args);
}
?>

The sorted array is now in the return value of the function instead of being passed by reference.

<?php
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);

// Pass the array, followed by the column names and sort flags
$sorted = array_orderby($data, 'volume', SORT_DESC, 'edition', SORT_ASC);
?>
zequez at gmail dot com 07-May-2010 12:19
Easiest way I find out to sort an entire multidimensional array by one element of it:

<?php
$multiArray = Array(
    Array("id" => 1, "name" => "Defg"),
    Array("id" => 2, "name" => "Abcd"),
    Array("id" => 3, "name" => "Bcde"),
    Array("id" => 4, "name" => "Cdef"));
$tmp = Array();
foreach($multiArray as &$ma)
    $tmp[] = &$ma["name"];
array_multisort($tmp, $multiArray);
foreach($multiArray as &$ma)
    echo $ma["name"]."<br/>";

/* Outputs
    Abcd
    Bcde
    Cdef
    Defg
*/
?>

^-^
alexander dot v at zend dot com 20-Jan-2010 12:42
array_multisort works normally in php 5.3, but it forces arguments to be references.

It doesn't make differences for common array_multisort() usage, but makes "problems" for sorting variable number of arrays where call_user_func_array() function is involved.

So all sorting arrays have to be collected into new one as a references to array variables:

<?php
$sortArgs = array();

for (...) {
    ...
    $sortArgs[] = &$valuesArray;
    ...
}

call_user_func_array('array_multisort', $sortArgs);
?>

This (requiring arguments to be a reference) is not actually a problem since source array will not be sorted otherwise.

Important note!
Don't forget to destroy $valuesArray variable if you use it over each array_multisort() argument processing iteration.
If you don't do it, all array_multisort() arguments will contain the same array:

<?php
for (...) {
    ...
    $sortArgs[] = &$valuesArray;
    unset($valuesArray);
    ...
}
?>

And the last important thing :)
Collect sorting arrays somewhere. PHP 5.3 will transfer reference into value (when $valuesArray is destroyed) and you will get "Parameter 1 to array_multisort() expected to be a reference, value given" warning again otherwise.

Final code should look like this:

<?php
$sortArgs = array();
$sortFieldValues = array();

for (...) {
    ...
    $sortFieldValues[] = &$valuesArray;
    $sortArgs[] = &$valuesArray;
    unset($valuesArray);
    ...
}

call_user_func_array('array_multisort', $sortArgs);
?>
d_inkubus at yahoo dot com 22-Dec-2009 12:33
Update to array_msort()--in PHP 5.3 it won't work.

The solution is to replace the array_merge($params, $order) with the following code snippet:

<?php
$order=(array)$order;
foreach($order as $order_element)
{
    $params[]=&$order_element;
}
?>

Full code to array_msort():

<?php
function array_msort($array, $cols)
{
    $colarr = array();
    foreach ($cols as $col => $order)
    {
        $colarr[$col] = array();
        foreach ($array as $k => $row)
        {
            $colarr[$col]['_'.$k] = strtolower($row[$col]);
        }
    }
    $params = array();
    foreach ($cols as $col => $order)
    {

        $params[] =&$colarr[$col];
        $order=(array)$order;
        foreach($order as $order_element)
        {
            //pass by reference, as required by php 5.3
            $params[]=&$order_element;
        }
    }
    call_user_func_array('array_multisort', $params);
    $ret = array();
    $keys = array();
    $first = true;
    foreach ($colarr as $col => $arr)
    {
        foreach ($arr as $k => $v)
        {
            if ($first)
            {
                $keys[$k] = substr($k,1);
            }
            $k = $keys[$k];

            if (!isset($ret[$k]))
            {
                $ret[$k] = $array[$k];
            }

            $ret[$k][$col] = $array[$k][$col];
        }
        $first = false;
    }
    return $ret;
}
?>

For further reference, see these bug reports:
http://bugs.php.net/bug.php?id=49069
http://bugs.php.net/bug.php?id=49241
s dot i dot g at gmx dot com 10-Jul-2009 03:21
simple dns mx record sorting

<?php
//getting domain name
list($user, $domain) = split('[@]', $email);
getmxrr($domain, $mx_server, $mx_weight);

//sorting
for($i = 0; $i < count($mx_server); $i++)
{
    $mx[$i] = array
    (
      "server" => $mx_server[$i],
      "weight" => $mx_weight[$i],
    );
}
array_multisort($mx_weight, SORT_ASC, $mx_server, SORT_ASC, $mx);

//display result
print_r($mx);

//usage
$server = $mx[0]["server"];
?>
cagret at gmail dot com 21-Jun-2009 12:38
Update to array_msort(): instead of eval() using call_user_func_array(). If we get column sorting via $_GET it would be a serious security hole.

Now it also accepts multiple options for each column:

<?php
$arr2 = array_msort($arr1, array('name'=>array(SORT_DESC,SORT_REGULAR), 'cat'=>SORT_ASC));
?>

Btw. this function is automatically case-insensitive, as you can see use of strtolower().

<?php
function array_msort($array, $cols)
{
    $colarr = array();
    foreach ($cols as $col => $order) {
        $colarr[$col] = array();
        foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
    }
    $params = array();
    foreach ($cols as $col => $order) {
        $params[] =& $colarr[$col];
        $params = array_merge($params, (array)$order);
    }
    call_user_func_array('array_multisort', $params);
    $ret = array();
    $keys = array();
    $first = true;
    foreach ($colarr as $col => $arr) {
        foreach ($arr as $k => $v) {
            if ($first) { $keys[$k] = substr($k,1); }
            $k = $keys[$k];
            if (!isset($ret[$k])) $ret[$k] = $array[$k];
            $ret[$k][$col] = $array[$k][$col];
        }
        $first = false;
    }
    return $ret;

}
?>
cagret at gmail dot com 20-Jun-2009 09:16
A more inuitive way of sorting multidimensional arrays using array_msort() in just one line, you don't have to divide the original array into per-column-arrays:

<?php

$arr1 = array(
    array('id'=>1,'name'=>'aA','cat'=>'cc'),
    array('id'=>2,'name'=>'aa','cat'=>'dd'),
    array('id'=>3,'name'=>'bb','cat'=>'cc'),
    array('id'=>4,'name'=>'bb','cat'=>'dd')
);

$arr2 = array_msort($arr1, array('name'=>SORT_DESC, 'cat'=>SORT_ASC));

debug($arr1, $arr2);

arr1:
    0:
        id: 1 (int)
        name: aA (string:2)
        cat: cc (string:2)
    1:
        id: 2 (int)
        name: aa (string:2)
        cat: dd (string:2)
    2:
        id: 3 (int)
        name: bb (string:2)
        cat: cc (string:2)
    3:
        id: 4 (int)
        name: bb (string:2)
        cat: dd (string:2)
arr2:
    2:
        id: 3 (int)
        name: bb (string:2)
        cat: cc (string:2)
    3:
        id: 4 (int)
        name: bb (string:2)
        cat: dd (string:2)
    0:
        id: 1 (int)
        name: aA (string:2)
        cat: cc (string:2)
    1:
        id: 2 (int)
        name: aa (string:2)
        cat: dd (string:2)

function array_msort($array, $cols)
{
    $colarr = array();
    foreach ($cols as $col => $order) {
        $colarr[$col] = array();
        foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
    }
    $eval = 'array_multisort(';
    foreach ($cols as $col => $order) {
        $eval .= '$colarr[\''.$col.'\'],'.$order.',';
    }
    $eval = substr($eval,0,-1).');';
    eval($eval);
    $ret = array();
    foreach ($colarr as $col => $arr) {
        foreach ($arr as $k => $v) {
            $k = substr($k,1);
            if (!isset($ret[$k])) $ret[$k] = $array[$k];
            $ret[$k][$col] = $array[$k][$col];
        }
    }
    return $ret;

}

?>
m dot michalczyk at gmail dot com 16-May-2009 12:45
Here is useful example based on za at byza dot it solution to sort multidimensional objects by any dimension.
za at byza dot it
<?php
/* Example structure */
class person{
    function __construct($firstName, $lastName, $title, $position){
        $this->firstName = $firstName;
        $this->lastName = $lastName;
        $this->title = new title($title);
        $this->position = new position($position);
    }
}
class title{
    function __construct($name){
        $this->name = $name;
    }

}
class position{
    function __construct($name){
        $this->name = $name;
    }

}

$array[] = new person('Piotr', 'Sobiepanek', 'b', 'b');
$array[] = new person('Piotr', 'Kowalski', 'b', 'a');
$array[] = new person('Piotr', 'Michalski', 'a', 'a');
$array[] = new person('Jozef', 'Smietana', 'a', 'b');
$array[] = new person('Jozef', 'Cmietana', 'a', 'b');
$array[] = new person('Marcin', 'Kondraciuk', 'c', 'b');
$array[] = new person('Maksym', 'Kondraciuk', 'c', 'd');
$array[] = new person('Ambrozy', 'Kondraciuk', 'c', 'd');
$array[] = new person('Alojzy', 'Kondraciuk', 'c', 'd');

array_sort($array, 'title->name', 'position->name', 'lastName');
print_r($array);

/* Source */

function hod(&$base, $path){
    $keys = explode("->", $path);
    $keys[0] = str_replace('$', '', $keys[0]);
    $expression = '$ret = ';
    $expression.= '$';
    foreach ($keys as $key){
        if (++$licz == 1){
            $expression.= 'base->';
        } else {
            $expression.= $key.'->';
        }
    }
    $expression = substr($expression, 0, -2);
    $expression.= ';';
    eval($expression);
    return $ret;
}

function array_sort_func($a,$b=NULL) {
   static $keys;
   if($b===NULL) return $keys=$a;
   foreach($keys as $k) {
      if($k[0]=='!') {
         $k=substr($k,1);
         if(hod($a, '$a->'.$k)!==hod($b, '$b->'.$k)) {
            return strcmp(hod($b, '$b->'.$k),hod($a, '$a->'.$k));
         }
      }
      else if(hod($a, '$a->'.$k)!==hod($b, '$b->'.$k)) {
         return strcmp(hod($a, '$a->'.$k),hod($b, '$b->'.$k));
      }
   }
   return 0;
}

function array_sort(&$array) {
   if(!$array) return $keys;
   $keys=func_get_args();
   array_shift($keys);
   array_sort_func($keys);
   usort($array,"array_sort_func");
}
?>
za at byza dot it 27-Apr-2009 07:08
I wrote a function to sort a multidimensional array by multiple columns in ascending and descending order.

Sample array:

<?php
$my_array=array(
   array('id'=>1, 'surname'=>'rossi', 'name'=>'mario', 'group'=>'admin'),
   array('id'=>2, 'surname'=>'rossi', 'name'=>'giovanni', 'group'=>'user'),
   array('id'=>3, 'surname'=>'verdi', 'name'=>'luigi', 'group'=>'user'),
   array('id'=>4, 'surname'=>'verdi', 'name'=>'franco', 'group'=>'guest'),
   array('id'=>5, 'surname'=>'bianchi', 'name'=>'mario', 'group'=>'guest')
);
?>

Example: <?php array_sort($my_array,'!group','surname'); ?>

Output: sort the array DESCENDING by group and then ASCENDING by surname. Notice the use of ! to reverse the sort order. Rows order will be: 2,3,5,4,1

The function code:

<?php
function array_sort_func($a,$b=NULL) {
   static $keys;
   if($b===NULL) return $keys=$a;
   foreach($keys as $k) {
      if(@$k[0]=='!') {
         $k=substr($k,1);
         if(@$a[$k]!==@$b[$k]) {
            return strcmp(@$b[$k],@$a[$k]);
         }
      }
      else if(@$a[$k]!==@$b[$k]) {
         return strcmp(@$a[$k],@$b[$k]);
      }
   }
   return 0;
}

function array_sort(&$array) {
   if(!$array) return $keys;
   $keys=func_get_args();
   array_shift($keys);
   array_sort_func($keys);
   usort($array,"array_sort_func");
}
?>
mech.cx 28-Mar-2009 09:04
I was (as near everyone here :-) looking to sort 2-dimensional arrays by certain fields in the associative sub-arrays.
What I didn't like about the documentation examples is that you need to loop through the input array to create sub arrays first, then use those in the function call.

"php a-t-the-r-a-t-e chir.ag" (http://www.php.net/manual/en/function.array-multisort.php#60401) wrote a quite cunning wrapper function, I rewrote it slightly, changing variable names and adding comments (for my sanity :-) mostly.
One snag I found: the input array is passed to array_multisort as last argument, but the changed array is not the one that is returned. Passing it by reference fixed that. This seems to be caused by the whole thing sitting inside the call_user_func_array, as shown below.

<?php

$points = array(1, 5, 2, 2);
$names  = array('peter', 'mike', 'john Zoo', 'john Ab');

$source = array (
  array ( 'points' => 1, 'name' => 'Peter'),
  array ( 'points' => 5, 'name' => 'Mike'),
  array ( 'points' => 2, 'name' => 'John Zoo'),
  array ( 'points' => 2, 'name' => 'John Ab')
);

call_user_func_array('array_multisort', array($points, SORT_DESC, SORT_NUMERIC, $names, SORT_ASC, SORT_STRING, $source)); // doesn't work
print_r($source);
call_user_func_array('array_multisort', array($points, SORT_DESC, SORT_NUMERIC, $names, SORT_ASC, SORT_STRING, &$source)); // works!
print_r($source);

// Call like arrayColumnSort('points', SORT_DESC, SORT_NUMERIC, 'name', SORT_ASC, SORT_STRING, $source);

// Slightly adapted from http://www.php.net/manual/en/function.array-multisort.php#60401

// arrayColumnSort(string $field, [options, ], string $field2, [options, ], .... , $array) /

//____________________
// arrayColumnSort() /
function arrayColumnSort() {
  $args  = func_get_args();
  $array = array_pop($args);
  if (! is_array($array)) return false;
  // Here we'll sift out the values from the columns we want to sort on, and put them in numbered 'subar' ("sub-array") arrays.
  //   (So when sorting by two fields with two modifiers (sort options) each, this will create $subar0 and $subar3)
  foreach($array as $key => $row) // loop through source array
    foreach($args as $akey => $val) // loop through args (fields and modifiers)
      if(is_string($val))             // if the arg's a field, add its value from the source array to a sub-array
        ${"subar$akey"}[$key] = $row[$val];
  // $multisort_args contains the arguments that would (/will) go into array_multisort(): sub-arrays, modifiers and the source array
  $multisort_args = array();
  foreach($args as $key => $val)
    $multisort_args[] = (is_string($val) ? ${"subar$key"} : $val);
  $multisort_args[] = &$array;   // finally add the source array, by reference
  call_user_func_array("array_multisort", $multisort_args);
  return $array;
}

?>
frank at overdrunk dot net 14-Jan-2009 11:38
I had a function to make a sort on a 2D array and I wanted to sort an array using a column that usualy contains numeric values but also strings.

Lets say we have this array :

Array (
  [0] => Array ( "name" = "12000" ),
  [1] => Array ( "name" = "113" ),
  [2] => Array ( "name" = "test 01" ),
  [3] => Array ( "name" = "15000 tests" ),
  [4] => Array ( "name" = "45" ),
  [5] => Array ( "name" = "350" ),
  [6] => Array ( "name" = "725" ),
  [7] => Array ( "name" = "hello" )
}

SORT_STRING whould have returned me this :

Array ( // Numeric values are not correctly sorted
  [0] => Array ( "name" = "113" ),
  [1] => Array ( "name" = "12000" ),
  [2] => Array ( "name" = "15000 tests" ),
  [3] => Array ( "name" = "350" ),
  [4] => Array ( "name" = "45" ),
  [5] => Array ( "name" = "725" ),
  [6] => Array ( "name" = "hello" ),
  [7] => Array ( "name" = "test 01" )
}

SORT_NUMERIC would have returned me this :

Array ( // String values are not sorted, just in the same order
  [0] => Array ( "name" = "test 01" ),
  [1] => Array ( "name" = "hello" ),
  [2] => Array ( "name" = "45" ),
  [3] => Array ( "name" = "113" ),
  [4] => Array ( "name" = "350" ),
  [5] => Array ( "name" = "725" ),
  [6] => Array ( "name" = "12000" ),
  [7] => Array ( "name" = "15000 tests" ),
}

So I've made this hybrid code which combines the best of both worlds by merging content sorted either way according to the first caracter of the string:

<?php
/**
 * Sorts an array according to a specified column
 * Params : array  $table
 *          string $colname
 *          bool   $numeric
 **/
function sort_col($table, $colname) {
  $tn = $ts = $temp_num = $temp_str = array();
  foreach ($table as $key => $row) {
    if(is_numeric(substr($row[$colname], 0, 1))) {
      $tn[$key] = $row[$colname];
      $temp_num[$key] = $row;
    }
    else {
      $ts[$key] = $row[$colname];
      $temp_str[$key] = $row;
    }
  }
  unset($table);

  array_multisort($tn, SORT_ASC, SORT_NUMERIC, $temp_num);
  array_multisort($ts, SORT_ASC, SORT_STRING, $temp_str);
  return array_merge($temp_num, $temp_str);
}
?>

It would return something like this :

Array (
  [2] => Array ( "name" = "45" ),
  [3] => Array ( "name" = "113" ),
  [4] => Array ( "name" = "350" ),
  [5] => Array ( "name" = "725" ),
  [6] => Array ( "name" = "12000" ),
  [7] => Array ( "name" = "15000 tests" ),
  [1] => Array ( "name" = "hello" ),
  [0] => Array ( "name" = "test 01" ),
}
Zbigniew Heintze http://bigweb.pl 27-Nov-2008 08:36
<?php
/**
 * Sort DB result
 *
 * @param array $data Result of sql query as associative array
 *
 * Rest of parameters are optional
 * [, string $name  [, mixed $name or $order  [, mixed $name or $mode]]]
 * $name string - column name i database table
 * $order integer - sorting direction ascending (SORT_ASC) or descending (SORT_DESC)
 * $mode integer - sorting mode (SORT_REGULAR, SORT_STRING, SORT_NUMERIC)
 *
 * <code>
 * <?php
 * // You can sort data by several columns e.g.
 * $data = array();
 * for ($i = 1; $i <= 10; $i++) {
 *     $data[] = array( 'id' => $i,
 *                      'first_name' => sprintf('first_name_%s', rand(1, 9)),
 *                      'last_name' => sprintf('last_name_%s', rand(1, 9)),
 *                      'date' => date('Y-m-d', rand(0, time()))
 *                  );
 * }
 * $data = sortDbResult($data, 'date', SORT_DESC, SORT_NUMERIC, 'id');
 * printf('<pre>%s</pre>', print_r($data, true));
 * $data = sortDbResult($data, 'last_name', SORT_ASC, SORT_STRING, 'first_name', SORT_ASC, SORT_STRING);
 * printf('<pre>%s</pre>', print_r($data, true));
 * ?>
 * </code>
 *
 * @return array $data - Sorted data
 */
function sortDbResult(array $data /*$name, $order, $mode*/) {
    $_argList = func_get_args();
    $_data = array_shift($_argList);
    if (empty($_data)) {
        return $_data;
    }
    $_max = count($_argList);
    $_params = array();
    $_cols = array();
    $_rules = array();
    for ($_i = 0; $_i < $_max; $_i += 3)
    {
        $_name = (string) $_argList[$_i];
        if (!in_array($_name, array_keys(current($_data)))) {
            continue;
        }
        if (!isset($_argList[($_i + 1)]) || is_string($_argList[($_i + 1)])) {
            $_order = SORT_ASC;
            $_mode = SORT_REGULAR;
            $_i -= 2;
        } else if (3 > $_argList[($_i + 1)]) {
            $_order = SORT_ASC;
            $_mode = $_argList[($_i + 1)];
            $_i--;
        } else {
            $_order = $_argList[($_i + 1)] == SORT_ASC ? SORT_ASC : SORT_DESC;
            if (!isset($_argList[($_i + 2)]) || is_string($_argList[($_i + 2)])) {
                $_mode = SORT_REGULAR;
                $_i--;
            } else {
                $_mode = $_argList[($_i + 2)];
            }
        }
        $_mode = $_mode != SORT_NUMERIC
                    ? $_argList[($_i + 2)] != SORT_STRING ? SORT_REGULAR : SORT_STRING
                    : SORT_NUMERIC;
        $_rules[] = array('name' => $_name, 'order' => $_order, 'mode' => $_mode);
    }
    foreach ($_data as $_k => $_row) {
        foreach ($_rules as $_rule) {
            if (!isset($_cols[$_rule['name']])) {
                $_cols[$_rule['name']] = array();
                $_params[] = &$_cols[$_rule['name']];
                $_params[] = $_rule['order'];
                $_params[] = $_rule['mode'];
            }
            $_cols[$_rule['name']][$_k] = $_row[$_rule['name']];
        }
    }
    $_params[] = &$_data;
    call_user_func_array('array_multisort', $_params);
    return $_data;
}
?>
oOo at OoO dot com 10-Nov-2008 09:58
Since the manual doesn't specify so, I want to point out that the SORT_NUMERIC flag does also work correctly if the values are floating point numbers.
Just don't forget to also add the respective SORT_ASC or SORT_DESC flag :)
matt at idizinc dot com 23-Oct-2008 08:47
I looked on some forms for an answer to this simple problem and couldn't find one so I came up with a solution that may help in some situations.

How do you sort an array by a field in that array and resolve numeric ties randomly?

Code:
<?php

foreach($list as $temp_list)
{
   $sort_aux[] = ($temp_list['column_to_sort_by']+(rand(1, 9)/10));
}
array_multisort($sort_aux, SORT_NUMERIC, $list);

?>

Example:

$list[]=array('name'=>'Tom', 'score'=>3);
$list[]=array('name'=>'Sam', 'score'=>3);
$list[]=array('name'=>'Joey', 'score'=>1);

Explanation:
I took an existing example found above that shows how to sort an array by one of it's columns/fields.
I just added: "+(rand(1,9)/10)" To randomly add .1 through .9 to their score to resolve the tie. (Obviously this specific example only works if you're sorting by an integer... so you may need to modify it to suit your needs.)

Hope this helps someone.
isp dot php at spspft dot de 19-Oct-2008 02:44
I would like to report a kind of confusion that arose with the message

Warning: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of array_multisort(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file...

from a line like this:

array_multisort (&$keyarr, &$arr );// sort against this keys

This message is not easily switched off by changing the error reporting level because it's produced at parsinig time -- not execution time.

I think this message is misleading because the arguments are passed by reference ANYWAY in array_multisort.

Anybody encountering this message should know that nothing has to be done, except deleting the ampersands (&).
I was tricked by this message because of couse I wanted to have the *sorted* array back. And couldn't find the ini file nor the declaration of array_multisort.
I think in this description of array_multisort the call by reference should be listed in the definition.

Hope this helps someone
seiffs_at_centrum-dot-cz 18-Aug-2008 05:53
There have to be two corrections to the php_multisort($data,$keys)

    // Sort Expression
    $i=0;
    $sort='';  //here
    foreach ($keys as $k){
      if($i>0){$sort.=',';}
      $sort.='$cols[\''.$k['key'].'\']'; //and here
      if($k['sort']){$sort.=',SORT_'.strtoupper($k['sort']);}
      if($k['type']){$sort.=',SORT_'.strtoupper($k['type']);}
      $i++;
    }
php {dot} net [arrt] sebble.com 11-May-2008 11:16
I was requiring a PHP function a sort my array data as part of an SQL interpreter for PHP arrays. This is the code I came up with. and works wonderfully.

I hope this helps somebody. If anyone uses this, let me know what you think, if there are any problems with it...

<?

##                ##
##  PHPMultiSort  ##
##                ##
// Takes:
//        $data,  multidim array
//        $keys,  array(array(key=>col1, sort=>desc), array(key=>col2, type=>numeric))

function php_multisort($data,$keys){

  // List As Columns
  foreach ($data as $key => $row) {
    foreach ($keys as $k){
      $cols[$k['key']][$key] = $row[$k['key']];
    }
  }
  // List original keys
  $idkeys=array_keys($data);
  // Sort Expression
  $i=0;
  foreach ($keys as $k){
    if($i>0){$sort.=',';}
    $sort.='$cols['.$k['key'].']';
    if($k['sort']){$sort.=',SORT_'.strtoupper($k['sort']);}
    if($k['type']){$sort.=',SORT_'.strtoupper($k['type']);}
    $i++;
  }
  $sort.=',$idkeys';
  // Sort Funct
  $sort='array_multisort('.$sort.');';
  eval($sort);
  // Rebuild Full Array
  foreach($idkeys as $idkey){
    $result[$idkey]=$data[$idkey];
  }
  return $result;
}

###############

// Example Data

$_DATA['table1'][] = array("name" => "Sebastian", "age" => 18, "male" => true);
$_DATA['table1'][] = array("name" => "Lawrence",  "age" => 16, "male" => true);
$_DATA['table1'][] = array("name" => "Olivia",    "age" => 10, "male" => false);
$_DATA['table1'][] = array("name" => "Dad",       "age" => 50, "male" => true);
$_DATA['table1'][] = array("name" => "Mum",       "age" => 40, "male" => false);
$_DATA['table1'][] = array("name" => "Sebastian", "age" => 56, "male" => true);
$_DATA['table1'][] = array("name" => "Lawrence",  "age" => 19, "male" => true);
$_DATA['table1'][] = array("name" => "Olivia",    "age" => 24, "male" => false);
$_DATA['table1'][] = array("name" => "Dad",       "age" => 10, "male" => true);
$_DATA['table1'][] = array("name" => "Mum",       "age" => 70, "male" => false);

###############

$res=php_multisort($_DATA['table1'], array(array('key'=>'name'),array('key'=>'age','sort'=>'desc')))
var_dump($res);

/*
array(10) {
  [8]=>
  array(3) {
    ["name"]=>
    string(3) "Dad"
    ["age"]=>
    int(10)
    ["male"]=>
    bool(true)
  }
  [3]=>
  array(3) {
    ["name"]=>
    string(3) "Dad"
    ["age"]=>
    int(50)
    ["male"]=>
    bool(true)
  }
  [1]=>
  array(3) {
    ["name"]=>
    string(8) "Lawrence"
    ["age"]=>
    int(16)
    ["male"]=>
    bool(true)
  }
  [6]=>
  array(3) {
    ["name"]=>
    string(8...
*/
?>
joopm at hotmail dot com 10-Apr-2008 01:50
@ scott at bartoncomputer dot com

You could also reference the array (if you wanted the original array in the object sorted):

array_multisort($position, SORT_DESC, &$clsVar->data);

Regards, Chr.
glenn at freebsd dot nl 29-Nov-2007 07:49
To sort a nested array by column (key/index):

Example: two entries in the nested array:

firstarray[0]['adres'] = "adres1"
firstarray[0]['email'] = "email2"

firstarray[1]['adres'] = "adres2"
firstarray[1]['email'] = "email1"

-----------------------------------------

Without code: output would put adres1/email2 on top

-----------------------------------------

foreach($firstarray as $sortarray)
{
$column[] = $sortarray['email'];
array_multisort($column, SORT_ASC, $firstarray);
}

-----------------------------------------

With code: output would put adres2/email1 on top
scott at bartoncomputer dot com 03-May-2007 07:18
I didn't see this noted anywhere, so I figured I'd put in a little comment regarding arrays located inside classes. For instance:

class abc
{
   var $data;
}

The following code does not act as expected:

$clsVar =& new abc();

foreach ($clsVar->data as $key => $row)
{
   $position[$key]  = $key;
}

array_multisort($position, SORT_DESC, $clsVar->data);

While I realize this could much easily be acheived using ksort(), this is merely a much more simple example of this behaviour. The exerpt above comes from a much more complicated sort using multi-scripted arrays.

Anyway the only way I could find to get around the behaviour of multisort not sorting the referenced class-array was to make a copy of it as below:

$clsVar =& new abc();
$newData = $clsVar->data;

foreach ($newData as $key => $row)
{
   $position[$key]  = $key;
}

array_multisort($position, SORT_DESC, $newData);

Now newData will contain the sorted array as expected.

Hopefully this helps someone else!
10-Apr-2007 12:01
<?php

    $strDeger = 'aaaa|bbbb|cccc';

    $arrBol = explode('|',$strDeger);

    array_multisort($arrBol, SORT_DESC);

    for($i = 0; $i <= count($arrBol); $i++) {

        echo $arrBol[$i].'<br />';

    }

?>
vermon7 27-Feb-2007 10:23
When using array_multisort() on copies of arrays, it is changing all the copies, even if you modify the copy before using array_multisort().

I've avoided this bug by serializing a copy of array before calling array_multisort, and unserializg it after array_multisort() Look at the code:

<?php

$records_copy = serialize($records) ;
array_multisort ( $records[$sort_field] , $records[$sort2_field] ) ;
$records_copy = unserialize($records_copy) ;

?>
Jon L. -- intel352 [AT] gmail [DOT] com 26-Feb-2007 05:27
This is my solution for a dynamic multisort, using POST values. This doesn't account for a need to sort by multiple columns at once, but could be modified for that purpose.

<?php
    /**
    * @desc You really should validate the posted sort direction against a list of valid possibilities.
    *         Options are SORT_ASC, SORT_DESC, etc, as shown in the documentation for array_multisort
    */
    $sort['direction'] = $_POST['sort_direction'] ? $_POST['sort_direction'] : 'SORT_ASC';
    $sort['field']       = $_POST['sort_field'] ? $_POST['sort_field'] : 'value';

    $array_to_sort = array();
    $array_to_sort['TestCase1'] = array('name'=>'Test1','value'=>'218');
    $array_to_sort['TestCase2'] = array('name'=>'Test2','value'=>'10');
    $array_to_sort['TestCase3'] = array('name'=>'Test3','value'=>'64');

    /**
    * @desc Build columns using the values, for sorting in php
    */
    $sort_arr = array();
    foreach($array_to_sort AS $uniqid => $row){
        foreach($row AS $key=>$value){
            $sort_arr[$key][$uniqid] = $value;
        }
    }

    print '<b>Before sorting</b>: <br> <pre>';
    print_r($array_to_sort);
    print '</pre>';

    if($sort['direction']){
        array_multisort($sort_arr[$sort['field']], constant($sort['direction']), $array_to_sort);
    }

    print '<b>After sorting</b>: <br> <pre>';
    print_r($array_to_sort);
    print '</pre>';

?>

This example prints out:

Before sorting:

Array
(
    [TestCase1] => Array
        (
            [name] => Test1
            [value] => 218
        )

    [TestCase2] => Array
        (
            [name] => Test2
            [value] => 10
        )

    [TestCase3] => Array
        (
            [name] => Test3
            [value] => 64
        )

)

After sorting:

Array
(
    [TestCase2] => Array
        (
            [name] => Test2
            [value] => 10
        )

    [TestCase3] => Array
        (
            [name] => Test3
            [value] => 64
        )

    [TestCase1] => Array
        (
            [name] => Test1
            [value] => 218
        )

)
01-Dec-2006 03:58
casting the parameter arrays for array_multisort seem to make the sorting ineffective?

for example:-
<?
        foreach((array)$report_files as $report_files_i)
        {
            $file_stat = stat($report_files_i);
            $report_files_x[] = array(
                'filename'      => $report_files_i
                ,'basename'     => basename($report_files_i)
                ,'ctime'        => date("D, M j, Y",$file_stat['ctime'])
                ,'size'         => $file_stat['size']
                );

            $basename_i[] = strtolower(basename($report_files_i)); // case insensitive

        }
        array_multisort($basename_i, SORT_ASC, $report_files_x);
?>
The above works but if you change the last time to :-
<?
        array_multisort((array)$basename_i, SORT_ASC, (array)$report_files_x);
?>
...adding the (array) cast doesn't sort the main array ...
brettz9 throu gh yah 14-Sep-2006 09:04
Often, one may have a group of arrays which have parallel data that need to be kept associated with each other (e.g., the various attribute values of a group of elements might be stored in their own arrays). Using array_multisort as is, by specifying additional fields, it is possible, as in the documentation example cited below, that this association will be lost.

To take this example set of data from the documentation:
<?php
$ar1 = array("10", 100, 100, "a");
$ar2 = array(1, 3, "2", 1);
?>

The example goes on to sort it this way:
<?php
array_multisort($ar1, $ar2);
?>

In this case, although the "10" remains associated with the first '1' after being sorted, the "2" and '3' are reversed from their original order.

In order to sort by one field only (yet still have the other array(s) being correspondingly sorted), one can use array_keys (which makes an array out of the keys) to ensure that no further sub-sorting is performed. This works because array_keys is making an array for which no duplicates can exist (since keys will be unique), and thus, the subsequent fields will have no relevance as far as subsorting.

So, using the above data, we can perform this sort instead:
<?php
$ar3 = array_keys($ar1);
array_multisort($ar1, $ar3, $ar2);
?>

which, when $ar1 and $ar2 are dumped gives:

array(4) {
  [0]=> string(2) "10"
  [1]=> string(1) "a"
  [2]=> int(100)
  [3]=> int(100)
}
array(4) {
  [0]=>  int(1)
  [1]=>  int(1)
  [2]=>  int(3)
  [3]=>  string(1) "2"
}
ricardo 04-Sep-2006 02:47
Hi,

Modded the function from KES,
goals:
- Object oriented
- string comparision using naturalordening

code:
<?
class HtmlTable{
    var $sortorder;
    var $rows;

//row adding stuf and constructor removed

    function sort($sortorder){
        if(is_array($sortorder)){
            $this->sortorder=$sortorder;
            usort($this->rows,array(&$this,'sort_compare'));
        }
    }
    function sort_compare($a,$b){//sort function
        $result=0;
        foreach($this->sortorder as $key=>$value){
            $result=strnatcmp($a[$key],$b[$key]);
            if($result==0)continue;
            if($value=='desc')$result=$result*-1;
            break;
        }
        return $result;
    }
}
?>
LPChip 28-Aug-2006 03:04
I was looking for a way to dynamically multisort my array.

By dynamically I mean that its not static what column will be sorted and if its ASC or DESC, and the ability to have more than one sorts.

This is the way a database would allow you to do.

The best way to dynamically do this, is by using eval.

The code below is partly what I used. (eg, I left out where the arrays were made and stuff, but the important part is here.)

<?

$orderby_arr = array("col1 ASC";"col2 DESC");

// prepare multisort using eval
        $eval_sort  = "array_multisort(";

        if ($orderby !="") {

            $orderby_arr_c = count($orderby_arr);

            for ($orderby_walk=0; $orderby_walk < $orderby_arr_c; $orderby_walk++) {
                $pos = strpos($orderby_arr[$orderby_walk], " ");
                $orderby_col = substr($orderby_arr[$orderby_walk], 0, $pos);
                $orderby_type = substr($orderby_arr[$orderby_walk], $pos+1);

                $eval_sort .= "\$this->OrderBy[$orderby_col]" . ", SORT_$orderby_type,";
            }
        }

        $eval_sort .= " \$this->Current_Query);";

        // if there's an array, sort it.
        if ($this->Current_Query_m != -1) eval($eval_sort);
?>
RQuadling at GMail dot com 07-Aug-2006 02:53
Extending KES's example (http://www.php.net/manual/en/function.array-multisort.php#68452) to look like array_multisort().

NOTE: Fully commented code is available at http://rquadling.php1h.com (sorry for the ads).

The syntax is the same as array_multisort().

You also have 3 additional parameters you can use:

AMC_SORT_STRING_CASELESS to sort the strings case insensitively.
AMC_LOSE_ASSOCIATION (the default behaviour) to lose the associations for the array.
AMC_KEEP_ASSOCIATION to keep the associations for the array.

Other than that, these function work together JUST like array_multisort but sorts using column(s) without the need to first extract the columns into individual arrays.

<?php
define ('AMC_SORT_STRING_CASELESS', SORT_STRING + 1);
define ('AMC_LOSE_ASSOCIATION', 1001);
define ('AMC_KEEP_ASSOCIATION', 1002);
define ('AMC_SORT_ORDER', 1003);
define ('AMC_SORT_TYPE', 1004);

function array_multisort_column(array &$a_data, $m_mixed1)
    {
    $a_Args = func_get_args();
    $i_Args = func_num_args();
    $GLOBALS['a_AMC_ordering'] = array();
    $a_Columns = array_keys(reset($a_data));
    $b_KeepAssociation = False;
    for($i_Arg = 1 ; $i_Arg < $i_Args ; )
        {
        if (in_array($a_Args[$i_Arg], $a_Columns))
            {
            $s_Column = $a_Args[$i_Arg];
            $GLOBALS['a_AMC_ordering'][$a_Args[$i_Arg]] = array
                (
                AMC_SORT_ORDER => SORT_ASC,
                AMC_SORT_TYPE => SORT_REGULAR,
                );
            while
                (
                isset($a_Args[$i_Arg + 1]) &&
                in_array
                    (
                    $a_Args[$i_Arg + 1],
                    array
                        (
                        AMC_KEEP_ASSOCIATION,
                        AMC_LOSE_ASSOCIATION,
                        AMC_SORT_STRING_CASELESS,
                        SORT_ASC,
                        SORT_DESC
                        SORT_NUMERIC,
                        SORT_REGULAR,
                        SORT_STRING,
                        ),
                    True
                    )
                )
                {
                if (in_array($a_Args[$i_Arg + 1], array(SORT_ASC, SORT_DESC), True))
                    {
                    $GLOBALS['a_AMC_ordering'][$s_Column][AMC_SORT_ORDER] = $a_Args[$i_Arg + 1];
                    }
                elseif (in_array($a_Args[$i_Arg + 1], array(SORT_REGULAR, SORT_NUMERIC, SORT_STRING, AMC_SORT_STRING_CASELESS), True))
                    {
                    $GLOBALS['a_AMC_ordering'][$s_Column][AMC_SORT_TYPE] = $a_Args[$i_Arg + 1];
                    }
                elseif (AMC_KEEP_ASSOCIATION == $a_Args[$i_Arg + 1])
                    {
                    $b_KeepAssociation = True;
                    }
                ++$i_Arg;
                }
            }
        ++$i_Arg;
        }
    $s_Sorter = ($b_KeepAssociation ? 'uasort' : 'usort');
    $b_Result = $s_Sorter($a_data, 'array_multisort_column_cmp');
    unset($GLOBALS['a_AMC_ordering']);
    return $b_Result;
    }

function array_multisort_column_cmp(array &$a_left, array &$a_right)
    {
    $i_Result = 0;
    foreach($GLOBALS['a_AMC_ordering'] as $s_Column => $a_ColumnData)
        {
        switch ($a_ColumnData[AMC_SORT_TYPE])
            {
            case SORT_NUMERIC :
                $i_ColumnCompareResult =
                    ((intval($a_left[$s_Column]) == intval($a_right[$s_Column]))
                    ?
                        0
                    :
                        ((intval($a_left[$s_Column]) < intval($a_right[$s_Column]))
                        ?
                            -1
                        :
                            1
                        )
                    );
                break;
            case SORT_STRING :
                $i_ColumnCompareResult = strcmp((string)$a_left[$s_Column], (string)$a_right[$s_Column]);
                break;
            case AMC_SORT_STRING_CASELESS :
                $i_ColumnCompareResult = strcasecmp((string)$a_left[$s_Column], (string)$a_right[$s_Column]);
                break;
            case SORT_REGULAR :
            default :
                $i_ColumnCompareResult =
                    (($a_left[$s_Column] == $a_right[$s_Column])
                    ?
                        0
                    :
                        (($a_left[$s_Column] < $a_right[$s_Column])
                        ?
                            -1
                        :
                            1
                        )
                    );
                break;
            }
        if (0 == $i_ColumnCompareResult)
            {
            continue;
            }
        $i_Result = $i_ColumnCompareResult * (($a_ColumnData[AMC_SORT_ORDER] == SORT_DESC) ? -1 : 1);
        break;
        }
    return $i_Result;
    }
?>
KES http://kes.net.ua 27-Jul-2006 02:30
<?
//sort by second column then first one
$orderBy=array('0'=>'desc', 'first'=>'asc');

function KES_cmp($a, $b) {
  global $orderBy;
  $result= 0;
  foreach( $orderBy as $key => $value ) {
    if( $a[$key] == $b[$key] ) continue;
    $result= ($a[$key] < $b[$key])? -1 : 1;
    if( $value=='desc' ) $result= -$result;
    break;
    }
  return $result;
  }

$result= array();
$result[]= array( 'first'=>6, 2);
$result[]= array( 'first'=>3, 2);
$result[]= array( 'first'=>1, 3);
$result[]= array( 'first'=>1, 2);
$result[]= array( 'first'=>6, 1);

print "<b>Source</b>";
print_r($result);

usort($result, 'KES_cmp');
print "<b>Result</b>";
print_r($result);
?>
KES 27-Jul-2006 01:35
It is very handy to have function, which sort like this:

$arrayToSort[]= array(0 => ".",               "type" => "dir");
$arrayToSort[]= array(0 => "qf",              "type" => "file");
$arrayToSort[]= array(0 => "..",              "type" => "dir");
$arrayToSort[]= array(0 => "text.txt",      "type" => "file");
$arrayToSort[]= array(0 => "hello",          "type" => "dir");

//first sort by the column 'type', then sort by the column '0'
$howToSort= array('type'=> 'asc', 0=> 'desc');

multisort($arrayToSort, $howToSort);

The result:
0           |       type
----------------------------
.           | dir
..           | dir
hello       | dir
text.txt    | file
qf          | file
scott - evolove - net - work it out 23-Mar-2006 04:51
A very simple way to sort an array of associative arrays by some value is to use usort.

I needed to sort an array of 20 data structures by their 'distance' value:

Array
(
    [0] => Array
        (
            [blahblah] => blahblah
            [distance] => 6
        )

    [1] => Array
        (
          you get the idea....

Here's the code:

--------------------
usort($results, "distributor_compare");

/**
 * usort callback
 */
function distributor_compare($a, $b) {
    $adist = intval($a['distance']);
    $bdist = intval($b['distance']);

    if ($adist == $bdist) {
     return 0;
     }
     return ($adist < $bdist) ? -1 : 1;
}
--------------------
peter dot graham at tcat dot ac dot uk 16-Feb-2006 04:16
Many thanks to ?AlberT at SuperAlberT dot it? for his useful and elegant function. I have made some small alterations so it doesn?t use pass-by-reference as this throws up warnings in newer versions of php4.

<?php

function array_key_multi_sort($arr, $l , $f='strnatcasecmp')
{
    usort($arr, create_function('$a, $b', "return $f(\$a['$l'], \$b['$l']);"));
    return($arr);
}

?>
mail at theopensource dot com 31-Jan-2006 08:34
I wanted to share with you a function that I created to make the array_multisort process much easier for myself... There was some interesting things that I encountered and I will post that in the comments.

I created this function so that all I have to do is tell it what column I want to sort through in a one level deep multidimensional array.  You can Try this code in your browser to view the results

ex/
<?php

//Here is an array example
$test[0]['name'] = "David";
$test[0]['age'] = 28;
$test[1]['name'] = "Dennis";
$test[1]['age'] = 23;
$test[2]['name'] = "Joseph";
$test[2]['age'] = 42;

//Here is the Function

function sortmddata($array, $by, $order, $type){

//$array: the array you want to sort
//$by: the associative array name that is one level deep
////example: name
//$order: ASC or DESC
//$type: num or str

$sortby = "sort$by"; //This sets up what you are sorting by

$firstval = current($array); //Pulls over the first array

$vals = array_keys($firstval); //Grabs the associate Arrays

foreach ($vals as $init){
    $keyname = "sort$init";
    $$keyname = array();
}
//This was strange because I had problems adding
//Multiple arrays into a variable variable
//I got it to work by initializing the variable variables as arrays
//Before I went any further

foreach ($array as $key => $row) {

foreach ($vals as $names){
    $keyname = "sort$names";
    $test = array();
    $test[$key] = $row[$names];
    $$keyname = array_merge($$keyname,$test);

}

}

//This will create dynamic mini arrays so that I can perform
//the array multisort with no problem
//Notice the temp array... I had to do that because I
//cannot assign additional array elements to a
//varaiable variable

if ($order == "DESC"){
if ($type == "num"){
array_multisort($$sortby,SORT_DESC, SORT_NUMERIC,$array);
} else {
array_multisort($$sortby,SORT_DESC, SORT_STRING,$array);
}
} else {
if ($type == "num"){
array_multisort($$sortby,SORT_ASC, SORT_NUMERIC,$array);
} else {
array_multisort($$sortby,SORT_ASC, SORT_STRING,$array);
}
}

//This just goed through and asks the additional arguments
//What they are doing and are doing variations of
//the multisort

return $array;
}

//Now to test it

$test = sortmddata($test,'age','ASC','num');

print_r ($test);

//This will return
//Array (
//[0] => Array ([name] => Dennis [age] => 23 )
//[1] => Array ( [name] => David [age] => 28 )
//[2] => Array ( [name] => Joseph [age] => 42 )
//)

?>

There you go... please let me know what you think if you like.
php a-t-the-r-a-t-e chir.ag 05-Jan-2006 11:10
Re: phu at kungphu, 19-Dec-2005 11:36

asort($test) will not let me specify which columns to sort ASC/DESC, NUMERIC/STRING etc.

I have data similar to what you specified. Now I want to sort $test by points DESC and name ASC. Here's my function that does it, based on suggestions on this page. It uses array_multisort (and hence acts just like it: preserving string-keys etc.)

<?php

  function arrayColumnSort()
  {
    $n = func_num_args();
    $ar = func_get_arg($n-1);
    if(!is_array($ar))
      return false;

    for($i = 0; $i < $n-1; $i++)
      $col[$i] = func_get_arg($i);

    foreach($ar as $key => $val)
      foreach($col as $kkey => $vval)
        if(is_string($vval))
          ${"subar$kkey"}[$key] = $val[$vval];

    $arv = array();
    foreach($col as $key => $val)
      $arv[] = (is_string($val) ? ${"subar$key"} : $val);
    $arv[] = $ar;

    call_user_func_array("array_multisort", $arv);
    return $ar;
  }

  $test["pete"]['points']=1;
  $test["pete"]['name']='Peter';

  $test["mike"]['points']=5;
  $test["mike"]['name']='Mike';

  $test["zoo"]['points']=2;
  $test["zoo"]['name']='John Zoo';

  $test["ab"]['points']=2;
  $test["ab"]['name']='John Ab';

  $test1 = $test;

  asort($test1);

  $test2 = arrayColumnSort("points", SORT_DESC, SORT_NUMERIC, "name", SORT_ASC, SORT_STRING, $test);

  print_r($test1); // asort
  print_r($test2); // arrayColumnSort

?>

Output from asort:

Array
(
    [pete] => Array
        (
            [points] => 1
            [name] => Peter
        )

    [ab] => Array
        (
            [points] => 2
            [name] => John Ab
        )

    [zoo] => Array
        (
            [points] => 2
            [name] => John Zoo
        )

    [mike] => Array
        (
            [points] => 5
            [name] => Mike
        )

)

Output from arrayColumnSort:

Array
(
    [mike] => Array
        (
            [points] => 5
            [name] => Mike
        )

    [ab] => Array
        (
            [points] => 2
            [name] => John Ab
        )

    [zoo] => Array
        (
            [points] => 2
            [name] => John Zoo
        )

    [pete] => Array
        (
            [points] => 1
            [name] => Peter
        )

)
phu at kungphu 19-Dec-2005 08:36
Notepad's example using asort/arsort will -not- work unless 'points' is defined -before- 'name'.

Running the posted code sorts by 'name', which was not the point of the previous posts.  Defining 'points' as 'apoints' also did not work; however, defining 'points' first yields correct sorting:

$test[0]['points']=1;
$test[0]['name']='Peter';

$test[1]['points']=5;
$test[1]['name']='Mike';

$test[2]['points']=2;
$test[2]['name']='John';

asort($test);

It appears asort uses the first defined element to sort a multidimensional array.
notepad at codewalkers dot com 10-Dec-2005 01:33
for you guys trying to sort scores on an associative multi-dimensional array, why are you creating your own functions?

<?php

$test[0]['name']='Peter';
$test[0]['points']=1;

$test[1]['name']='Mike';
$test[1]['points']=5;

$test[2]['name']='John';
$test[2]['points']=2;

asort($test);
// or even arsort();

?>

the above seems to work for me...
Shmee 30-Sep-2005 03:46
RWC?s code works very well until you get a ?point? value greater then ten.  This is because the strcmp() function will return that 2 > 10 and 2 > 11 ? 2 > 19 and so on. To compare number values over ten try a sort like this:

$test = multi_sort($test, $key = 'points');

function multi_sort($array, $akey)
{
  function compare($a, $b)
  {
     global $key;
     if ($a[$key]>$b[$key]){
         $varcmp = "1";
         return $varcmp;
     }
     elseif ($a[$key]<$b[$key]){
         $varcmp = "-1";
         return $varcmp;
     }
     elseif ($a[$key]==$b[$key]){
         $varcmp = "0";
         return $varcmp;
     }
  }
  usort($array, "compare");
  return $array;
}
RWC 25-Sep-2005 10:45
This is the simpler version of the function by AlberT.

A lot of times you have got an array like this:

$test[0]['name']='Peter';
$test[0]['points']=1;

$test[1]['name']='Mike';
$test[1]['points']=5;

$test[2]['name']='John';
$test[2]['points']=2;

You just want to sort on the index in the second dimension, ie. on points in the above example.

You can use the function below and call it like this:

$test = multi_sort($test, $key = 'points');

function multi_sort($array, $akey)
{
  function compare($a, $b)
  {
     global $key;
     return strcmp($a[$key], $b[$key]);
  }
  usort($array, "compare");
  return $array;
}

Note: to be able to use $key in the compare function, it can not simply be passed as a parameter. It has to be declared global and set somewhere outside of compare().
AlberT at SuperAlberT dot it 18-Jul-2005 03:59
a nice piece of code to do an "array_key_multi_sort()" is the following:

<?php
/**
 * orders a multidimentional array on the base of a label-key
 *
 * @param $arr, the array to be ordered
 * @param $l the "label" identifing the field
 * @param $f the ordering function to be used,
 *    strnatcasecmp() by default
 * @return  TRUE on success, FALSE on failure.
 */
function array_key_multi_sort(&$arr, $l , $f='strnatcasecmp') {
        return usort($arr, create_function('$a, $b', "return $f(\$a['$l'], \$b['$l']);"));
}
?>
kencomer at kencomer dot com 13-Jun-2005 08:48
If your data is in an associative array that you would need to separate into columns such as those retrieved with your favorite flavor of _fetch_array() in order to be able to use multisort(), you should consider using uasort() or usort() as an alternative. Here is an example of how this could be beneficial:

(key is employee number which remains intact; rows are sorted by surname, firstname)

<?php
function namecmp( $row1,$row2 )
{
    $first = strcmp($row1['surname'], $row2['surname']) ;
    if ( $first )
        return $first ;
    else
        return strcmp($row1['name'], $row2['name']) ;
}

      $test = array( '11122202' => array('empno'=>11122202,'name'=>'geezer'
                ,'surname'=>'schmidt','age'=>96,'sex'=>'male')
            ,'11122204' => array('empno'=>11122204,'name'=>'coed'
                ,'surname'=>'beaujolais','age'=>18,'sex'=>'female')
            ,'11122206' => array('empno'=>11122206,'name'=>'immortal'
                ,'surname'=>'ramos','age'=>21,'sex'=>'male')
            ,'11122208' => array('empno'=>11122208,'name'=>'babyface'
                ,'surname'=>'brown','age'=>1,'sex'=>'male')
            ,'11122210' => array('empno'=>11122210,'name'=>'exjock'
                ,'surname'=>'gatti','age'=>48,'sex'=>'male')
            ,'11122212' => array('empno'=>11122212,'name'=>'jailbait'
                ,'surname'=>'muhammed','age'=>15,'sex'=>'female') )
            ;
echo "<pre>" ;
print_r( $test ) ;
uasort($test,'namecmp') ;
echo "----sorting----<br \>";
print_r( $test ) ;
echo "</pre>" ;

?>

Array
(
    [11122202] => Array
        (
            [empno] => 11122202
            [name] => geezer
            [surname] => schmidt
            [age] => 96
            [sex] => male
        )

    [11122204] => Array
        (
            [empno] => 11122204
            [name] => coed
            [surname] => beaujolais
            [age] => 18
            [sex] => female
        )

    [11122206] => Array
        (
            [empno] => 11122206
            [name] => immortal
            [surname] => ramos
            [age] => 21
            [sex] => male
        )

    [11122208] => Array
        (
            [empno] => 11122208
            [name] => babyface
            [surname] => brown
            [age] => 1
            [sex] => male
        )

    [11122210] => Array
        (
            [empno] => 11122210
            [name] => exjock
            [surname] => gatti
            [age] => 48
            [sex] => male
        )

    [11122212] => Array
        (
            [empno] => 11122212
            [name] => jailbait
            [surname] => muhammed
            [age] => 15
            [sex] => female
        )

)
----sorting----
Array
(
    [11122204] => Array
        (
            [empno] => 11122204
            [name] => coed
            [surname] => beaujolais
            [age] => 18
            [sex] => female
        )

    [11122208] => Array
        (
            [empno] => 11122208
            [name] => babyface
            [surname] => brown
            [age] => 1
            [sex] => male
        )

    [11122210] => Array
        (
            [empno] => 11122210
            [name] => exjock
            [surname] => gatti
            [age] => 48
            [sex] => male
        )

    [11122212] => Array
        (
            [empno] => 11122212
            [name] => jailbait
            [surname] => muhammed
            [age] => 15
            [sex] => female
        )

    [11122206] => Array
        (
            [empno] => 11122206
            [name] => immortal
            [surname] => ramos
            [age] => 21
            [sex] => male
        )

    [11122202] => Array
        (
            [empno] => 11122202
            [name] => geezer
            [surname] => schmidt
            [age] => 96
            [sex] => male
        )

)
stevec at NO-/ dot /-SPAM dot qiguang dot net 13-Apr-2005 05:19
Be careful when using array_multisort() on copies of arrays, as you might end up changing the original array.  Given the following code:

<?php
    $test1 = array(4,3,2,1);
    $test2 = $test1;
    $test3 = array('a', 'b', 'c', 'd');

    array_multisort($test2, SORT_ASC, $test3);

    echo 'test1:';
    print_r($test1);
    echo 'test2:';
    print_r($test2);
    echo 'test3:';
    print_r($test3);
?>

You would expect:

test1:Array
(
    [0] => 4
    [1] => 3
    [2] => 2
    [3] => 1
)
test2:Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
test3:Array
(
    [0] => d
    [1] => c
    [2] => b
    [3] => a
)

However, if you run the code, you actually get:

test1:Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
test2:Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
test3:Array
(
    [0] => d
    [1] => c
    [2] => b
    [3] => a
)

Note that the original ($test1) ends up being sorted even though it was never called by array_multisort().  To work around this, insert a statement to modify the copy ($test2) before calling array_multisort() on it.  The following code will produce the expected "correct" results:

<?php
    $test1 = array(4,3,2,1);
    $test2 = $test1;
    $test3 = array('a', 'b', 'c', 'd');

    $test2[0] = $test2[0];                // fix
    array_multisort($test2, SORT_ASC, $test3);

    echo 'test1:';
    print_r($test1);
    echo 'test2:';
    print_r($test2);
    echo 'test3:';
    print_r($test3);
?>

This seems to be a resurrection of the closed bug #8130.  Also, someone reported this behavior in bug #32031, but it was incorrectly labeled "bogus" in reference to bug #25359, which is a different issue.
joao at intrasystems dot com dot br 07-Apr-2005 09:27
Exemple of sorting multi-dimensional arrays by one of it's fields:

$result[0]['nome']='Joao';
$result[0]['order']=5;
$result[1]['nome']='Pedro';
$result[1]['order']=1;
$result[2]['nome']='Marcelo';
$result[2]['order']=3;

foreach($result as $res)
     $sortAux[] = $res['order'];

array_multisort($sortAux, SORT_ASC, $result);

print_r($result);

produces:

Array
(
    [0] => Array
        (
            [nome] => Pedro
            [order] => 1
        )

    [1] => Array
        (
            [nome] => Marcelo
            [order] => 3
        )

    [2] => Array
        (
            [nome] => Joao
            [order] => 5
        )

)
Toni 10-Nov-2004 12:30
An example to sort an array by 3 criteria:

$recordset is an array of results from a query to a database, that I sort using $matrix as auxiliary array. First numeric, Second and Third alphabetically.

<?php
for($idx=0;$idx<$toShow;$idx++){
    $matrix[0][$id]=$recordset[$id][0];
    $matrix[1][$id]=$recordset[$id][1];
    $matrix[2][$id]=$recordset[$id][10];
}

array_multisort($matrix[0], SORT_DESC, SORT_NUMERIC,$matrix[1], SORT_STRING, SORT_ASC,$matrix[2], SORT_STRING, SORT_ASC);
?>
meddle at dzygn.com 05-Oct-2004 04:40
If you want to sort a multidomensional array by key name you cannot use array_multisort. ie: for an array named $archivos that prints like this:

Array
(
    [0] => Array
        (
            [index] => 0
            [name] => test
        )

    [1] => Array
        (
            [index] => 0
            [name] => watertaxi.jpg
        )

    [2] => Array
        (
            [index] => 0
            [name] => 2_0003.JPG
        )

    [3] => Array
        (
            [index] => 0
            [name] => 24A_0025.JPG
        )

    [4] => Array
        (
            [index] => 1
            [name] => _CIMG3501.JPG
        )

)

If I wanted to order by "name" I'd use:

function comparar($a, $b) {
        return strnatcasecmp($a["name"], $b["name"]);
}
usort($archivos, "comparar");

This function performs a case insensitive string comparison using a "natural order" algorithm (strnatcasecmp), resulting in:

Array
(
    [0] => Array
        (
            [index] => 0
            [name] => 2_0003.JPG
        )

    [1] => Array
        (
            [index] => 0
            [name] => 24A_0025.JPG
        )

    [2] => Array
        (
            [index] => 0
            [name] => test
        )

    [3] => Array
        (
            [index] => 0
            [name] => watertaxi.jpg
        )

    [4] => Array
        (
            [index] => 1
            [name] => _CIMG3501.JPG
        )

)
kat dot n0spam at audiogalaxy dot com 24-Aug-2001 07:25
If you're having problems with array_multisort changing variables in global space when it is called inside a function and you're not passing in the function parameters by reference you can alleviate the problem by calling array_multisort with its parameters by reference.

array_multisort(&$a, SORT_DESC, &$b);

More details here in my bug report:
http://www.php.net/bugs.php?id=12936


지정한 길이만큼 특정 값으로 배열 채우기

  • ≪ array_multisort
  • array_pop ≫
  • PHP Manual
  • 배열 함수 목록
  • 지정한 길이만큼 특정 값으로 배열 채우기

array_pad

(PHP 4, PHP 5, PHP 7)

array_pad — 지정한 길이만큼 특정 값으로 배열 채우기

설명

array array_pad ( array $input , int $pad_size , mixed $pad_value )

array_pad()는 pad_value 값을 pad_size 로 설정된 길이만큼을 채워넣은 input의 복사본을 반환한다. pad_size가 양수이면 배열의 오른쪽에 채워지고, 음수이면 왼쪽에 채워지게 된다. pad_size의 절대값이 input의 길이보다 작거나 같으면 패딩(padding)은 일어나지 않는다. 한번에 1048576 원소까지 추가할 수 있습니다.

인수

input

값을 채울 초기 배열.

pad_size

배열의 새 크기.

pad_value

input이 pad_size보다 작으면 채울 값.

반환값

지정한 pad_size만큼 pad_value로 채운 input의 사본을 반환합니다. pad_size가 양수이면 배열 오른쪽에 채우고, 음수이면 왼쪽에 채웁니다. pad_size의 절대값이 input의 크기보다 작거나 같으면 채우지 않습니다.

예제

Example #1 array_pad() 예제

<?php
$input = array(12, 10, 9);

$result = array_pad($input, 5, 0);
// result is array(12, 10, 9, 0, 0)

$result = array_pad($input, -7, -1);
// result is array(-1, -1, -1, -1, 12, 10, 9)

$result = array_pad($input, 2, "noop");
// not padded
?>

참고

  • array_fill() - 값으로 배열 채우기
  • range() - 원소의 범위를 가지는 배열 생성

User Contributed Notes

slava-san at mail dot ru 06-Jul-2013 07:58
// insert element to array
function array_insert(&$arr, $pos, $new_el=null) {
    $arraypad = array_pad($arr, count($arr)+1, 0);
    for ($i=count($arr)-1; $i>=$pos; $i--) {
        $arr[$i+1] = $arr[$i];
        if ($i == $pos) {
            $arr[$i] = $new_el;
        }
    }
}

$digits = array();
$digits[0] = 0;
$digits[1] = 1;
$digits[2] = 2;
$digits[3] = 3;
$digits[4] = 4;
$digits[5] = 5;
echo "was: "; var_dump($digits);

array_insert($digits, 3, 100);
echo "new: "; var_dump($digits);
sonu50imedbvu at gmail dot com(Sonu Jaiswal) 12-Jan-2011 09:53
Just an info about the value of "$pad_size" ,

If we set the value of "$pad_size" from -3 to 3,

It will produce the output like:

<?php
$result = array_pad($input, -3, "noop");
//result is array(12, 10, 9)

$result = array_pad($input, 3, "noop");
//result is array(12, 10, 9)
?>

means array will remain the same.
tugla 18-Dec-2008 06:23
Beware, if you try to pad an associative array using numeric keys, your keys will be re-numbered.

<?php
$a = array('size'=>'large', 'number'=>20, 'color'=>'red');
print_r($a);
print_r(array_pad($a, 5, 'foo'));

// use timestamps as keys
$b = array(1229600459=>'large', 1229604787=>20, 1229609459=>'red');
print_r($b);
print_r(array_pad($b, 5, 'foo'));
?>

yields this:
------------------
Array
(
    [size] => large
    [number] => 20
    [color] => red
)
Array
(
    [size] => large
    [number] => 20
    [color] => red
    [0] => foo
    [1] => foo
)
Array
(
    [1229600459] => large
    [1229604787] => 20
    [1229609459] => red
)
Array
(
    [0] => large
    [1] => 20
    [2] => red
    [3] => foo
    [4] => foo
)
hk, StrApp Bussiness Solutions 08-Jan-2007 11:15
A simple example for array_pad()

the syntax is as follows: array_pad(array(), (+/-)int, value)

where "array" is the array to which the value is to be added,

"(+/-) int" is a value that decides the length of the array(it should be greater than the length of the array.
if its a negative number then the value will be added at the left of the array else it will be added to the right.

"values" denotes the value to be added to the array

lets try an example:

<?php

$digits = array();
$digits[0] = 1;
$digits[1] = 2;
$digits[2] = 3;
$arraypad = array_pad($digits, -4, "0");
print_r($arraypad);

?>

output:

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
oaev at mail dot ru 22-Oct-2004 08:48
Easy way to get an array contains 5 random numbers from 0 to 9:

$rand_arr = array_rand( array_pad( array(), 10, 1 ), 5 );
29-Feb-2004 06:00
One way to initialize a 20x20 multidimensional array.

<?php
$a = array();
$b = array();
$b = array_pad($b,20,0);
$a = array_pad($a,20,$b);
?>
mwwaygoo at hotmail dot com 16-Jan-2004 05:02
little older, a little wiser.

ksort() will order the array back into its normal order again
so:

<?php
$myArr = array(2 => 'two', 4 => 'four');

$newArr = array_pad(array(), 6, 'FILLED');
$newArr =$myArr+$newArr;
ksort($newArr);
?>

Will give :
Array ( [0] => FILLED [1] => FILLED [2] => two [3] => FILLED [4] => four [5] => FILLED )
goffrie at sympatico dot ca 24-Mar-2003 02:06
To daarius - you mean you have...

[2]=>"two"
[3]=>"three"

and you want...

[0]=>"FILLED"
[1]=>"FILLED"
[2]=>"two"
[3]=>"three"
[4]=>"FILLED"
[5]=>"FILLED"

If so, then the following code...

<?php
$array = array(2 => "two", 3 => "three");
$array = array_pad($array, count($array)+2, "FILLED");
$num = -(count($array)+2);
$array = array_pad($array, $num, "FILLED");
print_r($array);
?>

will return:
Array ( [0] => FILLED [1] => FILLED [2] => two [3] => three [4] => FILLED [5] => FILLED )
The ordering should be okay,...
mwwaygoo at hotmail dot com 19-Sep-2002 06:39
OR you could do this

<?php
$myArr = array(2 => 'three', 3 => 'four');

$newArr = array_pad(array(), 4, 'FILLED');
$newArr =$myArr+$newArr;
?>

This gives your desired result BUT the ordering is a little wierd, because of the order they were added. Indexes are okay though and that is what you wanted.

print_r($newArr) outputs
Array ( [2] => three [3] => four [0] => FILLED [1] => FILLED )

hope this helps
daarius at hotmail dot com 23-Jul-2002 04:36
yes that is true. But, if the index of the array is 2=two, 3=three

and i want 4 more keys to be filled. But, not just filled anywhere, but i want to maintain the key index.

so, i would like to have 0=FILLED, 1=FILLED ... 4=FILLED, 5=FILLED

now i got 4 more keys padded with my string.

We can do this "if" we know the missing keys, but if we dont, then it would be nice for array_pad() or perhaps some new function to do this?

obviously we can achive this by looping through the array using array_key_exists(), and if you dont find the key, simply create + fill it.
regards,
Daarius...
scott*hurring.com 20-Jul-2002 01:20
to the previous commenter -- if you read the manual entry, you'd see that a negative pad_size will put the pad values at the front of the array.


배열의 마지막 원소 빼내기

  • ≪ array_pad
  • array_product ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 마지막 원소 빼내기

array_pop

(PHP 4, PHP 5, PHP 7)

array_pop — 배열의 마지막 원소 빼내기

설명

mixed array_pop ( array &$array )

array_pop()은 array의 마지막 값을 빼내고 그 값을 반환하며, array의 길이를 원소 하나 만큼 줄인다. array가 비어있으면 (또는 배열이 아니면) NULL을 반환한다. 배열이 아닌 것으로 호출하면, 경고가 발생합니다.

Note: 이 함수는 사용 후에 입력 배열의 array 포인터를 reset()합니다.

인수

array

값을 얻을 배열.

반환값

array의 마지막 값을 반환합니다. array가 비어있으면(또는 배열이 아니면) NULL을 반환합니다.

예제

Example #1 array_pop() 예제

<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);
?>

이 코드 수행후, $stack은 3개의 원소를 갖게될것이다:

Array
(
    [0] => orange
    [1] => banana
    [2] => apple
)

그리고 raspberry가 $fruit에 할당됩니다.

참고

  • array_push() - 배열의 끝에 하나 이상의 원소를 넣는다
  • array_shift() - 배열의 맨 앞에 있는 원소를 시프트
  • array_unshift() - 배열의 맨 앞에 하나 이상의 원소를 첨가

User Contributed Notes

Anonymous 07-Nov-2015 01:02
Let's "do stuff" with array_pop()

<?php

$dependecyInjection = ['globalState' => 'isHorrible'];

$workQueue = array_reverse($workQueue); // O(n)
// While we have stuff to do..... = [[ {function}, $params.... ], [], []...]
while (!empty($workQueue)) {

  // O(1) : And resize array. (maybe also memory size ?)
  $work = array_pop($workQueue);

  // If not `muted`, invoke the worker function....
  if (isset($work[0])) {
    $processor = array_shift($work);
    if ($moreWork = $processor->__invoke($work, $dependecyInjection)) { // <== Do stuff.
      // O(n) : add new stuff to the END of array ! (FIFO)
      $outputCommands += array_reverse($moreWork);
    }
  }
}
Anonymous 28-Feb-2014 05:21
Strict Standards will be thrown out if you put exploded array in array_pop:
<?php
$a = array_pop(explode(",", "a,b,c"));
echo $a;
?>

You will see:
PHP Strict Standards:  Only variables should be passed by reference in - on line 2

Strict Standards: Only variables should be passed by reference in - on line 2
c

Notice that, you should assign a variable for function explode, then pass the variable reference into array_pop to avoid the Strict Standard warning.
elad dot yosifon at gmail dot com 11-Aug-2013 03:45
Notice:
the complexity of array_pop() is O(1).
the complexity of array_shift() is O(n).
array_shift() requires a re-index process on the array, so it has to run over all the elements and index them.
qeremy 07-Mar-2012 08:26
For the sake of array_unshift()
:)

<?php
function array_unpop(&$arr) {
    $args = func_get_args(); unset($args[0]);
    $tarr = array();
    foreach ($args as $arg) {
        $tarr[] = $arg;
    }
    $arr = array_merge($arr, $tarr);
}

$queue = array("orange", "banana");
array_unpop($queue, "apple", "raspberry");
print_r($queue);
?>

Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)
Igor Feghali 04-Feb-2009 06:18
Beware that array_pop() returns a copy of the element (even if the array element is a reference). That means you will get different results on PHP 4 and 5 when dealing with objects.

<?php
$a = new stdClass();
$a->foo = 'bar';

$arr = array(&$a);
$b = array_pop($arr);

$b->foo = 'baz';

var_dump($a);
?>

Output of PHP 5.2.8:
object(stdClass)#1 (1) {
  ["foo"]=>
  string(3) "baz"
}

Output of PHP 4.4.9:
object(stdClass)(1) {
  ["foo"]=>
  string(3) "bar"
}

For more information please refer to:
http://br2.php.net/manual/en/language.oop5.references.php
mcgroovin at gmail dot com 23-Oct-2008 09:13
I wrote a simple function to perform an intersect on multiple (unlimited) arrays.

Pass an array containing all the arrays you want to compare, along with what key to match by.

<?php
function multipleArrayIntersect($arrayOfArrays, $matchKey)
{
    $compareArray = array_pop($arrayOfArrays);

    foreach($compareArray AS $key => $valueArray){
        foreach($arrayOfArrays AS $subArray => $contents){
            if (!in_array($compareArray[$key][$matchKey], $contents)){
                unset($compareArray[$key]);
            }
        }
    }

    return $compareArray;
}
?>
sonetti at hotmail dot com 05-Feb-2008 10:15
@smp_info
I think you are still tired. What would be wrong with:

<?php
$array = array('one', 'two', 'three', 'four');

//pop the last element off
array_pop($array);

//$array == array('one', 'two', 'three');
?>

As the documentation clearly notes, array_pop() not only returns the last element, but actually removes it from the array wich is passed by reference. Calling array_diff is a waste of resources.
Orsi 10-Jan-2008 04:05
Hi,

Here is a simple function which delete one element from the array (with value):
<?php
/*
* This function deletes the given element from a one-dimension array
* Parameters: $array:    the array (in/out)
*             $deleteIt: the value which we would like to delete
*             $useOldKeys: if it is false then the function will re-index the array (from 0, 1, ...)
*                          if it is true: the function will keep the old keys
* Returns true, if this value was in the array, otherwise false (in this case the array is same as before)
*/
function deleteFromArray(&$array, $deleteIt, $useOldKeys = FALSE)
{
    $tmpArray = array();
    $found = FALSE;
    foreach($array as $key => $value)
    {
        if($value !== $deleteIt)
        {
            if(FALSE === $useOldKeys)
            {
                $tmpArray[] = $value;
            }
            else
            {
                $tmpArray[$key] = $value;
            }
        }
        else
        {
            $found = TRUE;
        }
    }

    $array = $tmpArray;

    return $found;
}
?>

Maybe it will help somebody...
doyley3731 at gmail dot com 31-Oct-2007 01:37
I had a problem when using this function because my array was made up entirley of numbers, so I have made my own function.  Hopefully it will be useful to somebody.

function array_trim_end($array){

$num=count($array);
$num=$num-1;
unset($array[$num]);

return $array;
}
rmondragon at gmail dot com 07-Jun-2005 11:03
In a previous example ...
<?php
function array_trim ( $array, $index ) {
   if ( is_array ( $array ) ) {
     unset ( $array[$index] );
     array_unshift ( $array, array_shift ( $array ) );
     return $array;
     }
   else {
     return false;
     }
   }
?>

This have a problem. if u unset the last value and then use
<?
array_unshift ( $array, array_shift ( $array ) );
?>

will return a :  Array ( [0] => )
so u can fix it using...

<?php
if (count($array) > 0) array_unshift ( $values, array_shift ( $values ) );
?>

good luck ;)
15-Dec-2004 05:29
strrchr is a lot more useful than the other example using array_pop for finding the extension of a file. For example:

<?php
$ext = strrchr($filename, ".");
?>

$ext will contain the extension of the file, including a ".", if the file has an extension, and FALSE if the file has no extension. If the file has multiple extensions, such as "filename.tar.gz", then this construction will just return the last extension.
eddie at metafoundry dot com 25-Nov-2004 05:35
Quick way to get the extension from a file name using array_pop:

$ext = array_pop(explode(".",$filename));
30-Mar-2004 10:55
A function to delete an array value that recalculates the index ( its very short and easy to understand ).
Hope this might help someone...

<?php
/* Usage:
    $array : Array
    $indey : Integer

    The value of $array at the index $index will be
    deleted by the function.
*/
function array_trim ( $array, $index ) {
   if ( is_array ( $array ) ) {
      unset ( $array[$index] );
      array_unshift ( $array, array_shift ( $array ) );
      return $array;
      }
   else {
      return false;
      }
   }
?>
ryan8613(at)hotmail(dot)com 08-Jun-2003 03:10
A function that may help some out, considering it's pretty much the one mentioned previously...

<?php
function array_trim($arr, $indice) {
        if(!isset($indice)) {
                $indice = count($arr)-1;
        }
        unset($arr[$indice]);
        array_shift($arr);
        return $arr;
}
?>

It cuts the given index value off of the array, but without the shift, if  the 'index' value isn't given, it cuts off the end value.
01-Mar-2003 02:16
alex.chacon@terra.com
Hi
Here there is a function that delete a elemente from a array and re calculate indexes

<?php
function eliminarElementoArreglo ($array, $indice)
{
    if (array_key_exists($indice, $array))
    {
        $temp = $array[0];
        $array[0] = $array[$indice];
        $array[$indice] = $temp;
        array_shift($array);

        //reacomodamos ?ndices
        for ($i = 0 ; $i < $indice ; $i++)
        {
            $dummy = $array[$i];
            $array[$i] = $temp;
            $temp = $dummy;
        }
    }
    return $array;
}
?>


Calculate the product of values in an array

  • ≪ array_pop
  • array_push ≫
  • PHP Manual
  • 배열 함수 목록
  • Calculate the product of values in an array

array_product

(PHP 5 >= 5.1.0, PHP 7)

array_product — Calculate the product of values in an array

설명

number array_product ( array $array )

array_product() returns the product of values in an array.

인수

array

The array.

반환값

Returns the product as an integer or float.

변경점

버전 | 설명
5.3.6 | The product of an empty array is now 1, when before this function would return 0 for an empty array.

예제

Example #1 array_product() examples

<?php

$a = array(2, 4, 6, 8);
echo "product(a) = " . array_product($a) . "\n";
echo "product(array()) = " . array_product(array()) . "\n";

?>

위 예제의 출력:

product(a) = 384
product(array()) = 1

User Contributed Notes

Anonymous 20-Jun-2016 04:22
This function can be used to test if all values in an array of booleans are TRUE.

Consider:

<?php

function outbool($test)
{
    return (bool) $test;
}

$check[] = outbool(TRUE);
$check[] = outbool(1);
$check[] = outbool(FALSE);
$check[] = outbool(0);

$result = (bool) array_product($check);
// $result is set to FALSE because only two of the four values evaluated to TRUE

?>

The above is equivalent to:

<?php

$check1 = outbool(TRUE);
$check2 = outbool(1);
$check3 = outbool(FALSE);
$check4 = outbool(0);

$result = ($check1 && $check2 && $check3 && $check4);

?>

This use of array_product is especially useful when testing an indefinite number of booleans and is easy to construct in a loop.
Jimmy PHP 30-Apr-2014 04:55
array_product() can be used to implement a simple boolean AND search

<?php
$args = array('first_name'=>'Bill','last_name'=>'Buzzard');
$values[] = array('first_name'=>'Brenda','last_name'=>'Buzzard');
$values[] = array('first_name'=>'Victor','last_name'=>'Vulture');
$values[] = array('first_name'=>'Bill','last_name'=>'Blue Jay');
$values[] = array('first_name'=>'Bill','last_name'=>'Buzzard');

$result = search_for($values,$args);
var_dump($result);exit;

function search_for($array,$args) {
    $results = array();
    foreach ($array as $row) {
        $found = false;
        $hits = array();
        foreach ($row as $k => $v) {
            if (array_key_exists($k,$args)) $hits[$k] = ($args[$k] == $v);
        }

        $found = array_product($hits);
        if (!in_array($row,$results) && true == $found) $results[] = $row;
    }

    return $results;
}
?>

Output:

array (size=1)
  0 =>
    array (size=2)
      'first_name' => string 'Bill' (length=4)
      'last_name' => string 'Buzzard' (length=7)
Marcel G 28-Sep-2010 05:36
You can use array_product to calculate the factorial of n:
<?php
function factorial( $n )
{
  if( $n < 1 ) $n = 1;
  return array_product( range( 1, $n ));
}
?>

If you need the factorial without having array_product available, here is one:
<?php
function factorial( $n )
{
  if( $n < 1 ) $n = 1;
  for( $p++; $n; ) $p *= $n--;
  return $p;
}
?>
pqpqpq at wanadoo dot nl 17-Jan-2007 01:32
An observation about the _use_ of array_product with primes:

$a=$arrayOfSomePrimes=(2,3,11);
              // 2 being the first prime (these days)

$codeNum=array_product($a); // gives 66 (== 2*3*11)

echo "unique product(\$a) = " . array_product($a) . "\n";

The 66 can (only) be split into its original primes,
which can be transformed into their place in the row of primes (2,3,5,7,11,13,17,19...)  giving (1,2,3,4,5,6,7,8...)

The 66 gives the places {1,2,5} in the row of primes. The number "66" is unique as a code for {1,2,5}

So you can define the combination of table-columns {1,2,5} in "66". The bigger the combination, the more efficient in memory/transmission, the less in calculation.
Andre D 07-Aug-2006 10:56
This function can be used to test if all values in an array of booleans are TRUE.

Consider:

<?php

function outbool($test)
{
    return (bool) $test;
}

$check[] = outbool(TRUE);
$check[] = outbool(1);
$check[] = outbool(FALSE);
$check[] = outbool(0);

$result = (bool) array_product($check);
// $result is set to FALSE because only two of the four values evaluated to TRUE

?>

The above is equivalent to:

<?php

$check1 = outbool(TRUE);
$check2 = outbool(1);
$check3 = outbool(FALSE);
$check4 = outbool(0);

$result = ($check1 && $check2 && $check3 && $check4);

?>

This use of array_product is especially useful when testing an indefinite number of booleans and is easy to construct in a loop.


배열의 끝에 하나 이상의 원소를 넣는다

  • ≪ array_product
  • array_rand ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 끝에 하나 이상의 원소를 넣는다

array_push

(PHP 4, PHP 5, PHP 7)

array_push — 배열의 끝에 하나 이상의 원소를 넣는다

설명

int array_push ( array &$array , mixed $var [, mixed $... ] )

array_push()는 array를 스택으로 취급하고, array 끝에 전달되어진 변수를 넣는다. array의 길이는 집어넣은 변수의 수만큼 증가한다. 다음과 같은 효과를 갖는다:

<?php
$array[] = $var;
?>
각 var에 대해 반복된다.

Note: array_push()를 하나의 원소를 넣는 데 사용한다면, $array[] = 을 사용하는 것이 좋습니다. 함수 호출의 오버헤드가 없기 때문입니다.

Note: array_push()에 첫번째 인수가 배열이 아니면 경고가 발생합니다. 이는 새 배열이 생성될 때 $var[] 동작과 다릅니다.

인수

array

입력 배열.

var

넣을 값.

반환값

배열에 새로 추가된 원소의 수를 반환한다.

예제

Example #1 array_push() 예제

<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);
?>

위 예제의 출력:

Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)

참고

  • array_pop() - 배열의 마지막 원소 빼내기
  • array_push()
  • array_unshift() - 배열의 맨 앞에 하나 이상의 원소를 첨가

User Contributed Notes

colecooper2005 at icloud dot com 07-Nov-2016 03:44
When developing a pocketmine plugin, a good way to add stuff to a YAML table is

$table=$this->config->get("Table");
array_push($table, "New Value for table");
$this->config->set("Table", $table);
raat1979 at gmail dot com 23-Apr-2016 11:39
Unfortunately array_push returns the new number of items in the array
It does not give you the key of the item you just added, in numeric arrays you could do -1, you do however need to be sure that no associative key exists as that would break the assumption

It would have been better if array_push would have returned the key of the item just added like the below function
(perhaps a native variant would be a good idea...)

<?php

if(!function_exists('array_add')){
    function array_add(array &$array,$value /*[, $...]*/){
        $values = func_get_args();     //get all values
        $values[0]= &$array;        //REFERENCE!
        $org=key($array);              //where are we?
        call_user_func_array('array_push',$values);
        end($array);                 // move to the last item
        $key = key($array);         //get the key of the last item
        if($org===null){
            //was at eof, added something, move to it
            return $key;
        }elseif($org<(count($array)/2)){ //somewhere in the middle +/- is fine
            reset($array);
            while (key($array) !== $org) next($List);
        }else{
            while (key($array) !== $org) prev($List);
        }
        return $key;
    }
}
echo "<pre>\n";
$pr = array('foo'=>'bar','bar'=>'foo');
echo "Taken array;";
print_r($pr);

echo "\npush 1 returns ".array_push($pr,1)."\n";
echo "------------------------------------\n";
$pr = array('foo'=>'bar','bar'=>'foo');
echo "\npush 2 returns ".array_push($pr,1,2)."\n";
echo "------------------------------------\n";
$pr = array('foo'=>'bar','bar'=>'foo');
echo "\n add 1 returns ".array_add($pr,2)."\n\n";
echo "------------------------------------\n";
$pr = array('foo'=>'bar','bar'=>'foo');
echo "\n add 2 returns ".array_add($pr,1,2)."\n\n";
echo "<pre/>\n\n";
?>
Outputs:
Taken array;Array
(
    [foo] => bar
    [bar] => foo
)

push 1 returns 3
------------------------------------

push 2 returns 4
------------------------------------

 add 1 returns 0

------------------------------------

 add 2 returns 1
mrgreen dot webpost at gmail dot com 09-Mar-2016 11:11
Rodrigo de Aquino asserted that instead of using array_push to append to an associative array you can instead just do...

        $data[$key] = $value;

...but this is actually not true. Unlike array_push and even...

        $data[] = $value;

...Rodrigo's suggestion is NOT guaranteed to append the new element to the END of the array. For instance...

        $data['one'] = 1;
        $data['two'] = 2;
        $data['three'] = 3;
        $data['four'] = 4;

...might very well result in an array that looks like this...

       [ "four" => 4, "one" => 1, "three" => 3, "two" => 2 ]

I can only assume that PHP sorts the array as elements are added to make it easier for it to find a specified element by its key later. In many cases it won't matter if the array is not stored internally in the same order you added the elements, but if, for instance, you execute a foreach on the array later, the elements may not be processed in the order you need them to be.

If you want to add elements to the END of an associative array you should use the unary array union operator (+=) instead...

       $data['one'] = 1;
       $data += [ "two" => 2 ];
       $data += [ "three" => 3 ];
       $data += [ "four" => 4 ];

You can also, of course, append more than one element at once...

       $data['one'] = 1;
       $data += [ "two" => 2, "three" => 3 ];
       $data += [ "four" => 4 ];

Note that like array_push (but unlike $array[] =) the array must exist before the unary union, which means that if you are building an array in a loop you need to declare an empty array first...

       $data = [];
       for ( $i = 1; $i < 5; $i++ ) {
              $data += [ "element$i" => $i ];
       }

...which will result in an array that looks like this...

      [ "element1" => 1, "element2" => 2, "element3" => 3, "element4" => 4 ]
golddragon007 25-Mar-2015 07:51
I did a performance check, and I saw, if you push more than one value it can be faster the array push, that the normal $array[] version.

Case 1: $array[] = something;
Case 2: array_push($array, $value);
Case 3: array_push($array, $value1, $value2, $value3 [...]); $values are definied
Case 4: array_push($array, $value1, $value2, $value3 [...]); $values are definied, when $array is not empty
Case 5: Case1 + Case 3
Case 6: Result array contains some value (Case 4)
Case 7: Result array contains same value as the push array (Case 4)
-----------------------------------------------------------------------------------------------------------
~~~~~~~~~~~~ Case 1 ~~~~~~~~~~~~
Times: 0.0310 0.0300 0.0290 0.0340 0.0400 0.0440 0.0480 0.0550 0.0570 0.0570
Min: 0.0290
Max: 0.0570
Avg: 0.0425
~~~~~~~~~~~~ Case 2 ~~~~~~~~~~~~
Times: 0.3890 0.3850 0.3770 0.4110 0.4020 0.3980 0.4020 0.4060 0.4130 0.4200
Min: 0.3770
Max: 0.4200
Avg: 0.4003
~~~~~~~~~~~~ Case 3 ~~~~~~~~~~~~
Times: 0.0200 0.0220 0.0240 0.0340 0.0360 0.0410 0.0460 0.0500 0.0520 0.0520
Min: 0.0200
Max: 0.0520
Avg: 0.0377
~~~~~~~~~~~~ Case 4 ~~~~~~~~~~~~
Times: 0.0200 0.0250 0.0230 0.0260 0.0330 0.0390 0.0460 0.0510 0.0520 0.0520
Min: 0.0200
Max: 0.0520
Avg: 0.0367
~~~~~~~~~~~~ Case 5 ~~~~~~~~~~~~
Times: 0.0260 0.0250 0.0370 0.0360 0.0390 0.0440 0.0510 0.0520 0.0530 0.0560
Min: 0.0250
Max: 0.0560
Avg: 0.0419
~~~~~~~~~~~~ Case 6 ~~~~~~~~~~~~
Times: 0.0340 0.0280 0.0370 0.0410 0.0450 0.0480 0.0560 0.0580 0.0580 0.0570
Min: 0.0280
Max: 0.0580
Avg: 0.0462
~~~~~~~~~~~~ Case 7 ~~~~~~~~~~~~
Times: 0.0290 0.0270 0.0350 0.0410 0.0430 0.0470 0.0540 0.0540 0.0550 0.0550
Min: 0.0270
Max: 0.0550
Avg: 0.044

Tester code:
// Case 1
    $startTime = microtime(true);
    $array = array();
    for ($x = 1; $x <= 100000; $x++)
    {
        $array[] = $x;
    }
    $endTime = microtime(true);

// Case 2
    $startTime = microtime(true);
    $array = array();
    for ($x = 1; $x <= 100000; $x++)
    {
        array_push($array, $x);
    }
    $endTime = microtime(true);

// Case 3
    $result = array();
    $array2 = array(&$result)+$array;
    $startTime = microtime(true);
    call_user_func_array("array_push", $array2);
    $endTime = microtime(true);

// Case 4
    $result = array();
    for ($x = 1; $x <= 100000; $x++)
    {
        $result[] = $x;
    }
    $array2 = array(&$result)+$array;
    $startTime = microtime(true);
    call_user_func_array("array_push", $array2);
    $endTime = microtime(true);

// Case 5
    $result = array();
    $startTime = microtime(true);
    $array = array(&$result);
    for ($x = 1; $x <= 100000; $x++)
    {
        $array[] = $x;
    }
    $endTime = microtime(true);

// Case 6
    $result = array(1,2,3,4,5,6);
    $startTime = microtime(true);
    $array = array(&$result);
    for ($x = 1; $x <= 100000; $x++)
    {
        $array[] = $x;
    }
    $endTime = microtime(true);

// Case 7
    $result = array();
    for ($x = 1; $x <= 100000; $x++)
    {
        $result[] = $x;
    }
    $startTime = microtime(true);
    $array = array(&$result);
    for ($x = 1; $x <= 100000; $x++)
    {
        $array[] = $x;
    }
    $endTime = microtime(true);
Rob 10-Sep-2014 05:59
<?php
//Push same var into array multiple times
if(!function_exists('m_push')){

    function m_push($array, $var, $count){
        for ($i = 1; $i <= $count; $i++)
        {
            $array[] = $var;
        }
        return $array;
    }

}
?>
flobee 20-Nov-2013 10:25
Be warned using $array "+=" array(1,2,3) or union operations (http://php.net/manual/en/language.operators.array.php)

I think it worked in the past or i havent test it good enough. :-/
(once it worked, once [] was faster than array_push, the past :-D ):

php -r '$a = array(1,2); $a += array(3,4); print_r($a);'
Array (
    [0] => 1
    [1] => 2
)
php -r '$a = array(1,2); $b = array(3,4);$c = $a + $b; print_r($c);'
Array (
    [0] => 1
    [1] => 2
)
php -r '$a = array(1,2); $b = array(2=>3,3=>4);$c = $a + $b; print_r($c);'
Array (
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
hacfi 03-Jul-2013 11:57
I noticed that under certain circumstances it can be faster to use

 <?php
call_user_func_array("array_push", array('x' => &$array1) + array_values($array2));
 ?>

than

 <?php
foreach ($array2 as $key => $value) {
    $array1[] = $value;
}
 ?>
chris lanx 11-Jun-2013 09:07
i wanted to do some string manipulation on a specific field from an sql result set and then append the row to the end of an array like this.

[0] =>
    [one] => [val1]
    [two] => [val2]
    [three] => [val3]
    [four] => [val4]
[0] =>
    [one] => [val1]
    [two] => [val2]
    [three] => [val3]
    [four] => [val4]

~I tried array push and couldnt get it sorted, So what i did was this.

while ($row = mysql_fetch_assoc($query)) {

    $row['five'] = str_replace('.','',$row['place']);
    $results[] = $row;
}

resulting in

[0] =>
    [one] => [val1]
    [two] => [val2]
    [three] => [val3]
    [four] => [val4]
    [five] => [val5]
[0] =>
    [one] => [val1]
    [two] => [val2]
    [three] => [val3]
    [four] => [val4]
    [five] => [val5]
Rodrigo de Aquino 30-Mar-2012 12:24
If you're going to use array_push() to insert a "$key" => "$value" pair into an array, it can be done using the following:

    $data[$key] = $value;

It is not necessary to use array_push.
Chicna 21-Mar-2012 07:14
I found a simple way to have an "array_push_array" function, without the references problem when we want to use call_user_func_array(), hope this help :

function array_push_array(array &$array)
{
    $numArgs = func_num_args();
    if(2 > $numArgs)
    {
      trigger_error(sprintf('%s: expects at least 2 parameters, %s given', __FUNCTION__, $numArgs), E_USER_WARNING);
      return false;
    }

    $values = func_get_args();
    array_shift($values);

    foreach($values as $v)
    {
      if(is_array($v))
      {
        if(count($v) > 0)
        {
          foreach($v as $w)
          {
            $array[] = $w;
          }
        }
      }
      else
      {
        $array[] = $v;
      }
    }

    return count($array);
}
Szorstki 27-Dec-2011 11:35
With multidimensional arrays it's easy too (somebody posted a sample, but it isn't useful for beginners). Here is a piece of code that should explain everything and much more:

<?php
//two classes of objects which will be in multidimensional array
class Org {
    public $name;
    public function __construct($name) {
        $this->name = $name;
    }
}
class Org2 {
    public $name;
    public function __construct($name) {
        $this->name = $name;
    }
}

//main array
$arr = array();

//creating first subarray manually
//the subarray name must be like first class above
$arr['Org'] = array();

//creating second subarray for objects of selected class
//in another way
$x=2222;        //only for creating the sample object
$arr[get_class(new Org2("wtf".$x))] = array();

//pushing some new objects to (sub)arrays in (main)array
for ($i=0; $i<3; $i++) {
    $org1 = new Org("tst".$i);
    $org2 = new Org2("wtf".$i);
    array_push($arr[get_class($org1)], $org1);
    array_push($arr[get_class($org2)], $org2);
}

//printing everything
foreach ($arr as $key=>$value) {
    $counter = 0;
    echo "=====".$key." (key) | value: ".$value."<br/>";
    foreach ($value as $key2=>$value2) {
        if($value2 == null) {
            //echo "counter: ".$counter." | key: ".$key2;
            unset($arr[$key][$key2]);
        } else {
            print_r($value2);
            echo " [".$counter."]  [".$key2."]<br/>";
        }
        $counter++;
    }
}

//the additional, second part
echo "<br/>--------<br/><br/>";
//deleting one of the objects from selected (sub)array
unset($arr['Org'][1]);

//printing everything again (look at the counter values)
foreach ($arr as $key=>$value) {
    $counter = 0;
    echo "=====".$key." (key) | value: ".$value."<br/>";
    foreach ($value as $key2=>$value2) {
        if($value2 == null) {
            //echo "counter: ".$counter." | key: ".$key2;
            unset($arr[$key][$key2]);
        } else {
            print_r($value2);
            echo " [".$counter."]  [".$key2."]<br/>";
        }
        $counter++;
    }
}
?>

What we get in browser/console:
=====Org (key) | value: Array
Org Object ( [name] => tst0 ) [0] [0]
Org Object ( [name] => tst1 ) [1] [1]
Org Object ( [name] => tst2 ) [2] [2]
=====Org2 (key) | value: Array
Org2 Object ( [name] => wtf0 ) [0] [0]
Org2 Object ( [name] => wtf1 ) [1] [1]
Org2 Object ( [name] => wtf2 ) [2] [2]

--------

=====Org (key) | value: Array
Org Object ( [name] => tst0 ) [0] [0]
Org Object ( [name] => tst2 ) [1] [2]
=====Org2 (key) | value: Array
Org2 Object ( [name] => wtf0 ) [0] [0]
Org2 Object ( [name] => wtf1 ) [1] [1]
Org2 Object ( [name] => wtf2 ) [2] [2]
yuri 30-Nov-2011 07:00
If you want to put an element to a specific position in an array, try this function.

<?php

function array_put_to_position(&$array, $object, $position, $name = null)
{
        $count = 0;
        $return = array();
        foreach ($array as $k => $v)
        {
                // insert new object
                if ($count == $position)
                {
                        if (!$name) $name = $count;
                        $return[$name] = $object;
                        $inserted = true;
                }
                // insert old object
                $return[$k] = $v;
                $count++;
        }
        if (!$name) $name = $count;
        if (!$inserted) $return[$name];
        $array = $return;
        return $array;
}
?>

Example :

<?php
$a = array(
 'a' => 'A',
 'b' => 'B',
 'c' => 'C',
);

print_r($a);
array_put_to_position($a, 'G', 2, 'g');
print_r($a);

/*
Array
(
    [a] => A
    [b] => B
    [c] => C
)
Array
(
    [a] => A
    [b] => B
    [g] => G
    [c] => C
)
*/
?>
aosojnik at gmail dot com 23-Nov-2009 11:56
If you want to preserve the keys in the array, use the following:

<?php
function array_pshift(&$array) {
    $keys = array_keys($array);
    $key = array_shift($keys);
    $element = $array[$key];
    unset($array[$key]);
    return $element;
}
?>
rarioj at gmail dot com 05-Oct-2009 03:44
This function "Returns the new number of elements in the array."

To find out the last index, use:

<?php
$count = array_push($array, $value);
$last_index = array_pop(array_keys($array));
?>
Anonymous 20-Sep-2009 11:00
If you need to push the elements of an array onto the end of another, simply use array_splice():

array_splice($array, count($array), 0, $otherArray);
helpmepro1 at gmail dot com 13-Jan-2009 10:43
elegant php array combinations algorithm

<?

//by Shimon Dookin

function get_combinations(&$lists,&$result,$stack=array(),$pos=0)
{
 $list=$lists[$pos];
 if(is_array($list))
  foreach($list as $word)
  {
   array_push($stack,$word);
   if(count($lists)==count($stack))
    $result[]=$stack;
   else
    get_combinations($lists,$result,$stack,$pos+1);
   array_pop($stack);
  }
}

$wordlists= array( array("shimon","doodkin") , array("php programmer","sql programmer","mql metatrader programmer") );

get_combinations($wordlists,$combinations);

echo '<xmp>';
print_r($combinations);

?>
wesleys at opperschaap dot net 10-Oct-2008 09:58
A function which mimics push() from perl, perl lets you push an array to an array: push(@array, @array2, @array3). This function mimics that behaviour.

<?php

function array_push_array(&$arr) {
    $args = func_get_args();
    array_shift($args);

    if (!is_array($arr)) {
        trigger_error(sprintf("%s: Cannot perform push on something that isn't an array!", __FUNCTION__), E_USER_WARNING);
        return false;
    }

    foreach($args as $v) {
        if (is_array($v)) {
            if (count($v) > 0) {
                array_unshift($v, &$arr);
                call_user_func_array('array_push',  $v);
            }
        } else {
            $arr[] = $v;
        }
    }
    return count($arr);
}

$arr = array(0);
$arr2  = array(6,7,8);
printf("%s\n", array_push_array($arr, array(),array(1,2,3,4,5), $arr2));
print_r($arr);

# error..
$arr = "test";
printf("%s\n", array_push_array($arr, array(),array(1,2,3,4,5), $arr2));

?>
willdemaine at gmail dot com 07-Aug-2008 04:00
If you're adding multiple values to an array in a loop, it's faster to use array_push than repeated [] = statements that I see all the time:

<?php
class timer
{
        private $start;
        private $end;

        public function timer()
        {
                $this->start = microtime(true);
        }

        public function Finish()
        {
                $this->end = microtime(true);
        }

        private function GetStart()
        {
                if (isset($this->start))
                        return $this->start;
                else
                        return false;
        }

        private function GetEnd()
        {
                if (isset($this->end))
                        return $this->end;
                else
                        return false;
        }

        public function GetDiff()
        {
                return $this->GetEnd() - $this->GetStart();
        }

        public function Reset()
        {
                $this->start = microtime(true);
        }

}

echo "Adding 100k elements to array with []\n\n";
$ta = array();
$test = new Timer();
for ($i = 0; $i < 100000; $i++)
{
        $ta[] = $i;
}
$test->Finish();
echo $test->GetDiff();

echo "\n\nAdding 100k elements to array with array_push\n\n";
$test->Reset();
for ($i = 0; $i < 100000; $i++)
{
        array_push($ta,$i);
}
$test->Finish();
echo $test->GetDiff();

echo "\n\nAdding 100k elements to array with [] 10 per iteration\n\n";
$test->Reset();
for ($i = 0; $i < 10000; $i++)
{
        $ta[] = $i;
        $ta[] = $i;
        $ta[] = $i;
        $ta[] = $i;
        $ta[] = $i;
        $ta[] = $i;
        $ta[] = $i;
        $ta[] = $i;
        $ta[] = $i;
        $ta[] = $i;
}
$test->Finish();
echo $test->GetDiff();

echo "\n\nAdding 100k elements to array with array_push 10 per iteration\n\n";
$test->Reset();
for ($i = 0; $i < 10000; $i++)
{
        array_push($ta,$i,$i,$i,$i,$i,$i,$i,$i,$i,$i);
}
$test->Finish();
echo $test->GetDiff();
?>

Output

$ php5 arraypush.php
X-Powered-By: PHP/5.2.5
Content-type: text/html

Adding 100k elements to array with []

0.044686794281006

Adding 100k elements to array with array_push

0.072616100311279

Adding 100k elements to array with [] 10 per iteration

0.034690141677856

Adding 100k elements to array with array_push 10 per iteration

0.023932933807373
bxi at apparoat dot nl 23-May-2008 01:29
I've done a small comparison between array_push() and the $array[] method and the $array[] seems to be a lot faster.

<?php
$array = array();
for ($x = 1; $x <= 100000; $x++)
{
    $array[] = $x;
}
?>
takes 0.0622200965881 seconds

and

<?php
$array = array();
for ($x = 1; $x <= 100000; $x++)
{
    array_push($array, $x);
}
?>
takes 1.63195490837 seconds

so if your not making use of the return value of array_push() its better to use the $array[] way.

Hope this helps someone.
alexander dot williamson at gmail dot com 27-Mar-2008 01:06
This will work to solve the associative array issues:

$aValues[$key] = $value;

Where $key is a unique identifier and $value is the value to be stored. Since the $key works off a string or number, if you already have a $key with the same value as an existing $key, the element will be overwritten.

e.g.

$aValues["one"] = "value of one";
$aValues["two"] = "different value of two!";

gives:
array([one] => "value of one", [two] => "value of two");

but will be overwritten when using the same key (one):

$aValues["one"] = "value of one";
$aValues["one"] = "different value of two!";

will give:

array([one] => "different value of two!");

3686
darkimmortal at dkimmortal dot com 18-Feb-2008 08:31
I wrote this function for use with 'latest scores' on a quiz site, but it has many potential uses.

<?php
/**
* @desc array_push and removes elements from the beginning of the array until it is within limit
* @param    array   Array to push on to
* @param    mixed   Passed to array push as 2nd parameter
* @param    int     Limit (default = 10)
*
* @return   array   New array
*/
function array_push_limit($array,$add,$limit=10){
    array_push($array, $add);
    do {
        array_shift($array);
        $size=count($array);
    } while($size > $limit);

    return $array;
}
?>
----------
EXAMPLE:
----------
<?php
    $array=array(1, -5, 23, -66, 33, 54, 3);
    print_r(array_push_limit($array, "HELLO", 4));
?>
----------
OUTPUT:
----------
Array
(
    [0] => 33
    [1] => 54
    [2] => 3
    [3] => HELLO
)
zbde00 at hotmail dot com 20-Jul-2007 02:59
A very good function to remove a element from array
function array_del($str,&$array)
{
    if (in_array($str,$array)==true)
    {

        foreach ($array as $key=>$value)
        {
            if ($value==$str) unset($array[$key]);
        }
    }
}
Marc Bernet 15-May-2006 06:18
A small and basic implementation of a stack without using an array.

class node
{
        var $elem;
        var    $next;
}
class stack
{
    var $next;
    function pop()
    {
        $aux=$this->next->elem;
        $this->next=$this->next->next;
        return $aux;
    }
    function push($obj)
    {
        $nod=new node;
        $nod->elem=$obj;
        $nod->next=$this->next;
        $this->next=$nod;
    }
    function stack()
    {
        $this->next=NULL;
    }
}
richard dot udo at gmail dot com 09-Feb-2006 12:05
Just a typo i think but the code below will actually produce

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => Array
        (
            [0] => d
            [1] => e
            [2] => f
        )
)
egingell at sisna dot com 28-Jan-2006 03:06
If you push an array onto the stack, PHP will add the whole array to the next element instead of adding the keys and values to the array. If this is not what you want, you're better off using array_merge() or traverse the array you're pushing on and add each element with $stack[$key] = $value.

<?php

$stack = array('a', 'b', 'c');
array_push($stack, array('d', 'e', 'f'));
print_r($stack);

?>
The above will output this:
Array (
  [0] => a
  [1] => b
  [2] => c
  [3] => Array (
     [0] => a
     [1] => b
     [2] => c
  )
)
steve at webthoughts d\ot ca 13-Nov-2005 01:02
Further Modification on the array_push_associative function
1.  removes seemingly useless array_unshift function that generates php warning
2.  adds support for non-array arguments

<?
// Append associative array elements
function array_push_associative(&$arr) {
   $args = func_get_args();
   foreach ($args as $arg) {
       if (is_array($arg)) {
           foreach ($arg as $key => $value) {
               $arr[$key] = $value;
               $ret++;
           }
       }else{
           $arr[$arg] = "";
       }
   }
   return $ret;
}

$items = array("here" => "now");
$moreitems = array("this" => "that");

$theArray = array("where" => "do we go", "here" => "we are today");
echo array_push_associative($theArray, $items, $moreitems, "five") . ' is the size of $theArray.<br />';

echo "<pre>";
print_r($theArray);
echo "</pre>";

?>

Yields:

4 is the size of $theArray.
Array
(
    [where] => do we go
    [here] => now
    [this] => that
    [five] =>
)
ludvig dot ericson at gmail dot com 31-Oct-2005 02:32
Previous comment was not fully imitating the array_push behaviour,
1) does not return number of items pushed
2) can only handle one array to push

> Revised associative_push function with absolute reference of arg1 array; left unchanged if arg2 is empty.

<?php
// Append associative array elements
function array_push_associative(&$arr) {
    $args = func_get_args();
    array_unshift($args); // remove &$arr argument
    foreach ($args as $arg) {
        if (is_array($arg)) {
            foreach ($arg as $key => $value) {
                $arr[$key] = $value;
                $ret++;
            }
        }
    }

    return $ret;
}

$theArray = array();
echo array_push_associative($theArray, $items, $moreitems) . ' items added to $theArray.';
?>
john 19-Oct-2005 10:33
A variation of kamprettos' associative array push:

// append associative array elements
function associative_push($arr, $tmp) {
  if (is_array($tmp)) {
    foreach ($tmp as $key => $value) {
      $arr[$key] = $value;
    }
    return $arr;
  }
  return false;
}

$theArray = array();
$theArray = associative_push($theArray, $items);
kamprettos at yahoo dot com Teguh Iskanto 19-Sep-2005 02:33
Looking for a way to push data into an associative array and frustrated to know that array_push() can't do the job ?

here's my Scenario :
-------------------
I need to relate system command output into an associative array like these :

[sge@digital_db work]$ /usr/local/apache/htdocs/work/qhost.sh -h t1 -F | awk '{if(NR>4) print $1}' | sed  's/hl://g'
arch=lx24-amd64
num_proc=2.000000
mem_total=3.808G
swap_total=3.907G
virtual_total=7.715G
load_avg=0.000000
load_short=0.000000
load_medium=0.000000
load_long=0.000000
mem_free=3.510G
swap_free=3.907G
virtual_free=7.417G
mem_used=305.242M
swap_used=0.000
virtual_used=305.242M
cpu=0.000000
np_load_avg=0.000000
np_load_short=0.000000
np_load_medium=0.000000
np_load_long=0.000000

how I did it :
<? php

# get into the system command output
$assoc_cmd =`$work_dir/qhost.sh -h $host_resource -F | awk '{if(NR>4) print $1}'| sed  's/hl://g' ` ;

# split the "\n" character
$assoc_row = explode("\n", chop($assoc_cmd));

# get the index row
$idx_row  = count($assoc_row) - 1 ;

# initialize the associative array
$host_res_array = array();

for ($i = 0 ; $i<= $idx_row ; $i++)
        {
                # get params & values
                list($host_param,$host_val) = explode("=",$assoc_row[$i]);

                # populate / push data to assoc array
                $host_res_array[$host_param]= $host_val ;
        }

echo "<pre> Architecture : </pre>\n" ;
echo $host_res_array['arch'] ;
echo "<pre> Mem Total    : </pre>\n" ;
echo $host_res_array['mem_tot'];

?>

Hope this helps ! :)
bk at quicknet dot nl 04-Sep-2005 10:51
Add elements to an array before or after a specific index or key:

<?php

/**
 * @return array
 * @param array $src
 * @param array $in
 * @param int|string $pos
*/
function array_push_before($src,$in,$pos){
    if(is_int($pos)) $R=array_merge(array_slice($src,0,$pos), $in, array_slice($src,$pos));
    else{
        foreach($src as $k=>$v){
            if($k==$pos)$R=array_merge($R,$in);
            $R[$k]=$v;
        }
    }return $R;
}

/**
 * @return array
 * @param array $src
 * @param array $in
 * @param int|string $pos
*/
function array_push_after($src,$in,$pos){
    if(is_int($pos)) $R=array_merge(array_slice($src,0,$pos+1), $in, array_slice($src,$pos+1));
    else{
        foreach($src as $k=>$v){
            $R[$k]=$v;
            if($k==$pos)$R=array_merge($R,$in);
        }
    }return $R;
}

// Examples:

$src=array("A","B","C");
$in=array("X","Y");

var_dump(array_push_before($src,$in,1));
/* array_push_before, no-key array
array(5) {
  [0]=>
  string(1) "A"
  [1]=>
  string(1) "X"
  [2]=>
  string(1) "Y"
  [3]=>
  string(1) "B"
  [4]=>
  string(1) "C"
}*/

var_dump(array_push_after($src,$in,1));
/* array_push_after, no-key array
array(5) {
  [0]=>
  string(1) "A"
  [1]=>
  string(1) "B"
  [2]=>
  string(1) "X"
  [3]=>
  string(1) "Y"
  [4]=>
  string(1) "C"
}*/

$src=array('a'=>"A",'b'=>"B",'c'=>"C");
$in=array('x'=>"X",'y'=>"Y");

var_dump(array_push_before($src,$in,1));
/* array_push_before, key array, before index insert
array(5) {
  ["a"]=>
  string(1) "A"
  ["x"]=>
  string(1) "X"
  ["y"]=>
  string(1) "Y"
  ["b"]=>
  string(1) "B"
  ["c"]=>
  string(1) "C"
}*/

var_dump(array_push_before($src,$in,'b'));
/* array_push_before, key array, before key insert
array(5) {
  ["a"]=>
  string(1) "A"
  ["x"]=>
  string(1) "X"
  ["y"]=>
  string(1) "Y"
  ["b"]=>
  string(1) "B"
  ["c"]=>
  string(1) "C"
}*/

var_dump(array_push_after($src,$in,1));
/* array_push_after, key array, after index insert
array(5) {
  ["a"]=>
  string(1) "A"
  ["b"]=>
  string(1) "B"
  ["x"]=>
  string(1) "X"
  ["y"]=>
  string(1) "Y"
  ["c"]=>
  string(1) "C"
}*/

var_dump(array_push_after($src,$in,'b'));
/* array_push_after, key array, after key insert
array(5) {
  ["a"]=>
  string(1) "A"
  ["b"]=>
  string(1) "B"
  ["x"]=>
  string(1) "X"
  ["y"]=>
  string(1) "Y"
  ["c"]=>
  string(1) "C"
}*/

?>
ciprian dot amariei at gmail com 30-Aug-2005 11:47
regarding the speed of oneill's solution to insert a value into a non-associative array,  I've done some tests and I found that it behaves well if you have a small array and more insertions, but for a huge array and a little insersions I sugest  using this function:

function array_insert( &$array, $index, $value ) {
   $cnt = count($array);

   for( $i = $cnt-1; $i >= $index; --$i ) {
       $array[ $i + 1 ] = $array[ $i ];
   }
   $array[$index] = $value;
}

or if you are a speed adicted programmer (same situation: big array, few insertions) use this:

array_splice ( $array, $offset, 0, $item );

item may also be an array of values ;).
Phil Davies 18-Jul-2005 12:51
As someone pointed out the array_push() function returns the count of the array not the key of the new element. As it was the latter function i required i wrote this very simple replacement.

function array_push2(&$array,$object,$key=null){
    $keys = array_keys($array);
    rsort($keys);
    $newkey = ($key==null)?$keys[0]+1:$key;
    $array[$newkey] = $object;
    return $newkey;
}
oneill at c dot dk 03-Jun-2005 10:50
To insert a value into a non-associative array, I find this simple function does the trick:

function insert_in_array_pos($array, $pos, $value)
{
  $result = array_merge(array_slice($array, 0 , $pos), array($value), array_slice($array,  $pos));
  return $result;
}

Seems an awful lot simpler than the iterative solutions given above...
aaron dot hawley at uvm dot edu 27-May-2005 08:36
Skylifter notes on 20-Jan-2004 that the [] empty bracket notation does not return the array count as array_push does.  There's another difference between array_push and the recommended empty bracket notation.

Empy bracket doesn't check if a variable is an array first as array_push does.  If array_push finds that a variable isn't an array it prints a Warning message if E_ALL error reporting is on.

So array_push is safer than [], until further this is changed by the PHP developers.
josh at digitalfruition dot com 14-Feb-2005 07:17
Note that array_push() will, as described, return the COUNT of the array after adding a new item, not necessarily the INDEX of that new item:

<?php
$array = array(3 => 'three', 5 => 'five');

echo "\$array = ";
print_r($array);
echo "\n\n";

$to_push = array(1,2,4,);
foreach($to_push as $var)
{
    echo "calling array_push(\$array,$var); retval is ";
    echo array_push($array,$var);
    echo "\n";
}

echo "\$array = ";
print_r($array);
?>

The output of above is:

$array = Array
(
    [3] => three
    [5] => five
)

calling array_push($array,1); retval is 4
calling array_push($array,2); retval is 5
calling array_push($array,4); retval is 6
$array = Array
(
    [3] => three
    [5] => five
    [7] => seven
    [8] => 1
    [9] => 2
    [10] => 4
)

Notice how when array_push($array,1) was called, the new element has a key of 8 but array_push() returns 4.
andrew at cgipro dot com 03-Feb-2005 12:18
Need a real one-liner for adding an element onto a new array name?

$emp_list_bic = $emp_list + array(c=>"ANY CLIENT");

CONTEXT...
drewdeal: this turns out to be better and easier than array_push()
patelbhadresh: great!... so u discover new idea...
drewdeal: because you can't do:   $emp_list_bic = array_push($emp_list, c=>"ANY CLIENT");
drewdeal: array_push returns a count and affects current array.. and does not support set keys!
drewdeal: yeah. My one-liner makes a new array as a derivative of the prior array
aron 24-Feb-2004 11:48
The problem with array_push is that it is pass by value.  If you are dealing with objects whose inner state may change at any time, you need a push and pop who return the actual objects, rather than copies of them.
After some difficulty and board assistance, I have these methods.  I've tested them, and they seem to work fine.

<?php
function push(&$array, &$object){
    $array[] =& $object;
}
function & pop(&$array){
    return array_pop($array);
}

// [Test Code]
class TestObject{
    var $value = 0;
    function getValue(){
        return $this->value;
    }
    function setValue($mixed){
        $this->value = $mixed;
    }
}
$myarr = array();
$tmp =& new TestObject();
$tmp2 =& new TestObject();
$tmp->setValue(2);
$tmp2->setValue(3);

push($myarr, $tmp);
push($myarr, $tmp2);
$tmp->setValue(4);
$tmp2->setValue(6);
$val = pop($myarr);
print "popped value: ".$val->getValue()."<br />";

print "values in internal array: <br />";
foreach ($myarr as $key=>$value){
    print "key: $key, object: $value, value: ";
    print  $value->getValue()."<br />";
}
// [/TestCode]
?>
skiflyer 21-Jan-2004 01:05
However, don't forget that array_push() does more than [], it also performs a count and returns the value.

Modifying your code ever so slightly (see below), this puts array_push in the lead (not suprisingly).  So my conclusion would be that if I care about the number of elements in the array, then I'd use array_push(), if I don't (which is usually the case), then I'd use the [] method.

Results...
[] method: 0.34943199
push method: 0.31505919
difference: -0.03437280

Modified section of code...
$s_test_begin = FullMicroTime();
for($i = 0; $i <= 50000; $i++) { $num_tot = array_push($test2, $i); }
$s_test_end = FullMicroTime();

$f_test_begin = FullMicroTime();
for($i = 0; $i <= 50000; $i++) { $test[] = $i; $num_tot = count($test); }
$f_test_end = FullMicroTime();
daevid at daevid dot com 17-Feb-2003 05:38
Sadly, array_push() does not create an array if the array doesn't exist.  So if you're pushing the first element onto an array, you need to check and create it manually...

<?php
if ( !is_array($myArray) ) $myArray= array();
array_push($myArray, $myElement);
?>
bart at framers dot nl 27-Sep-2001 01:16
Array_push also works fine with multidimensional arrays. Just make sure the element is defined as an array first.

<?php
$array["element"][$element]["element"] = array();
array_push ($array["element"][$element]["element"], "banana");
?>


배열에서 하나 이상의 임의 원소를 가져옴

  • ≪ array_push
  • array_reduce ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열에서 하나 이상의 임의 원소를 가져옴

array_rand

(PHP 4, PHP 5, PHP 7)

array_rand — 배열에서 하나 이상의 임의 원소를 가져옴

설명

mixed array_rand ( array $input [, int $num_req ] )

array_rand()은 배열 안에서 하나 이상의 임의 원소를 가져올 때 가장 유용합니다.

인수

input

입력 배열.

num_req

가져올 원소 수를 지정합니다 - 지정하지 않으면, 기본값은 1입니다.

반환값

오직 한개의 원소만 꺼낼때에는 array_rand()는 임의 원소의 키를 반환하며, 여러 원소를 꺼낼때에는 임의 원소들의 키에 대한 배열을 반환한다. 이 함수는 배열에서 임의 키는 물론 값들을 꺼낼수 있다.

예제

Example #1 array_rand() 예제

<?php
srand((float) microtime() * 10000000);
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>

주의

Note: PHP 4.2.0부터 srand()나 mt_srand()를 이용한 난수값 생성기 초기화를 할 필요가 없습니다. 자동적으로 이루어집니다.

참고

  • shuffle() - 배열을 섞습니다

User Contributed Notes

divinity76 at gmail dot com 15-Oct-2016 10:59
if you're looking for a cryptographically secure variant (as of speaking, php uses mt_rand), i made this. (limitations: it has no $num parameter. it requires php>=7)

<?php
function csarray_rand(array $arr){
    $keys=array_keys($arr);
    $count=count($keys);
    if($count===0){
        //Contrary to documentation, PHP 4 and 5 and 7, up to php 7.1.0alpha3 does not actually return any warning when the array is empty and $num=NULL (and further contradicting the documentation, its NULL by default, not 1, and they are treated differently, 1 gives error, NULL does not.) ... 7.1.0beta1 does throw an error however... follow their example?
        if(version_compare(PHP_VERSION,'7.1.0beta1','>=')){
            trigger_error('Warning: csarray_rand(): Array is empty',E_USER_ERROR);
        }
        return NULL;
    }
    $csrand=random_int(0,count($keys)-1);
    return $keys[$csrand];
}
?>
mike at eastghost dot com 04-Jun-2015 06:13
Verified 2015-06-03 -- this function produces VERY uneven "random" distribution.
Wirek 16-Apr-2015 08:43
I agree with Sebmil (http://php.net/manual/en/function.array-rand.php#105265) that "array_rand()" produces weird and very uneven random distribution (as of my local PHP 5.3.8 and my public host's PHP 5.2.17).
Unfortunately, I haven't got any access either to a server with the latest PHP version. My info is for those of you who like to check things for themselves and who don't believe all of the official statements in the docs.
I've made a simple adjustment of his test code like this:
<?php
$s=1;    // Start value
$c=50;    // Count / End value
$test=array_fill($s, $c, 0);
$ts=microtime(true);
for($i=0; $i<5000000; $i++){
    $idx=mt_rand($s, $c);    // Try it with rand() - simpler but more evenly distributed than mt_rand()
    $test[$idx]++;
}
$te=microtime(true);
$te=($te-$ts)*1000.0;    // Loop time in miliseconds

asort($test);
echo "Test mt_rand() in ".$te." ms: <br/>\n";
foreach($test as $k=>$v) echo "$k :\t$v <br/>\n";
?>

And it appears to me that simple "$idx=rand(0, count($test)-1);" is much better than "$idx=array_rand($test, 1);".
And what's more the simpler and a bit slower (0 ms up to total 712.357 ms at 5 mln cycles) "rand()" is better than "mt_rand()" in simple everyday use cases because it is more evenly distributed (difference least vs. most often numbers: ca. 0.20-1.28 % for "rand()" vs. ca. 1.43-1.68 % for "mt_rand()").
Try it for yourself... although it depends on your software and hardware configuration, range of numbers to choose from (due to random patterns), number of cycles in the loop, and temporary (public) server load as well.
compolom at gmail dot com 14-Feb-2015 01:17
/*
useful function to sort an array randomly
*/
function sort_array_rand($array) {
    $result = array();
    $y = sizeof($array);
    $x = 0;
    while ($x < pow($y, 2)) {
        $result[] = $array[array_rand($array)];
        $x++;
    }
    return array_unique($result);
}
Tmy Adeson 08-Jan-2015 11:29
<?php
// You can do this, programmatically, using the following code ... Test It Yourself (TIY)

$colors=array(
    'gray','red','orange','green',
    '#B2CC80','#CC6699','#B89470','#99CCFF',
);//colors array

$c1=sizeof($colors)-1;//get position of the last element within the colors array

//    -- OR, if you prefer --
//$c1=count($colors)-1;//get position of the last element within the colors array

//pick a color at random from the colors array - 'Mersenne Twister based'
$color=$colors[mt_rand(0,$c1)];

echo<<<HERE
<!DOCTYPE html>
<html>
 <head>
   <meta http-equiv="refresh" content="2">
 </head>
 <body>
   <div style="width:400px; background-color:$color; border:1px; text-align:center;" >
     $color
   </div>
 </body>
</html>
HERE;
?>
yopai 08-Jan-2015 10:21
@Sebmil :
You say this function "has a strange randomness [, because] the 31st one is always by far the less occuring (by about 10% less than others)."

That's right (at least under linux, PHP 5.3). And it's also visible when calling array_rand with 1 as second parameter.

After checking the code, and testing it, I concluded that you have to call srand() at each iteration of your loop.

To be simple, a rand() call is made <n> times when the <n>th key is returned;
I suppose there is a flaw in the pseudorandom number generator (PRNG)  that php uses; somehow the generation has a frequency of 31 (when a random number is sufficiently low, then the 31st after it will be more higher).

Reinitializing the PRNG each time you enter the loop make the problem disappear (and is cheaper, in terms of time, than using shuffle).

In other words, the following code :
for ($i = 0; $i < 10000; ++$i)
{
    /* reinitialize the PRNG --> */ srand();
    $tirage_tab = array_rand($valeurs, 10);
    foreach($tirage_tab as $key => $value)
    {
        $proba[$valeurs[$value]]++;
    }
}

doesn't have the strange behaviour you noticed. No PRNG is perfectly random, so taking the habit of calling srand() from time to time is still a good practice if you rely on a lot of random numbers.
BenedictLemieux at gmail dot com 14-Sep-2014 12:31
Two things always bugged me with array_rand().

First is its name. It does not sound clear enough whether it gives key or values.

Second and more importantly is its erratic randomness, which is already well documented.

That is why I came up with these two simple functions:

<?php
function random_key($array){
    $keys=array_keys($array);
    return $keys[mt_rand(0, count($keys) - 1)];
}
function random_value($array){
    $values=array_values($array);
    return $values[mt_rand(0, count($values) - 1)];
}
?>

They both work well with any kind of arrays, do not alter the original one like shuffle, are giving more realistic random results, and their names are self describing.

The main drawback is that, as opposed to array_rand, they only gives one element, but at least that is clear from their name. I do believe easy to make random_keys and random_values.
marco at behnke dot biz 12-Sep-2014 10:52
Is there a difference in randomness if you use shuffle instead?

<?php
function array_shuffle($input, $num_of_results) {
    shuffle($input);
    return array_chunk($input, $num_of_results, true)[0];
}

$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
var_dump(array_shuffle($input, 2));

/*
array(2) {
  [0]=>
  string(3) "Neo"
  [1]=>
  string(8) "Morpheus"
}
*/
?>

And yes, I know that my example returns a portion of the array and not just the key numbers
john at brahy dot com 08-May-2014 08:00
if you want random elements from an array, this worked pretty well for me.

<?php
//using shuffle randomizes the order of elements

function get_random_elements( $array,$limit = 0 ) {

    shuffle($array);

    if ( $limit > 0 ) {
        $array = array_splice($array, 0, $limit);
    }
    return $array;
}
?>
nospamplease at mail dot com 13-Aug-2013 11:18
Random choice with a closure.

$randomChoice  = function($array) {return $array[array_rand($array)];};

$names = ['Dexter', 'Esther', 'David', 'Richard', 'Rachel', 'Belinda'];

echo $randomChoice($names);
Jordan Doyle 19-May-2013 06:49
Note: array_rand uses the libc generator, which is slower and less-random than Mersenne Twister.

<?php
    $a = ['http://php.net/', 'http://google.com/', 'http://bbc.co.uk/'];

    $website = $a[mt_rand(0, count($a) - 1)];
?>

This is a better alternative.
Anonymous 13-Jul-2012 11:42
It doesn't explicitly say it in the documentation, but PHP won't pick the same key twice in one call.
qeremy 07-Mar-2012 10:18
An example for getting random value from arrays;

<?php
function array_random($arr, $num = 1) {
    shuffle($arr);

    $r = array();
    for ($i = 0; $i < $num; $i++) {
        $r[] = $arr[$i];
    }
    return $num == 1 ? $r[0] : $r;
}

$a = array("apple", "banana", "cherry");
print_r(array_random($a));
print_r(array_random($a, 2));
?>

cherry
Array
(
    [0] => banana
    [1] => apple
)

And example for getting random value from assoc arrays;

<?php
function array_random_assoc($arr, $num = 1) {
    $keys = array_keys($arr);
    shuffle($keys);

    $r = array();
    for ($i = 0; $i < $num; $i++) {
        $r[$keys[$i]] = $arr[$keys[$i]];
    }
    return $r;
}

$a = array("a" => "apple", "b" => "banana", "c" => "cherry");
print_r(array_random_assoc($a));
print_r(array_random_assoc($a, 2));
?>

Array
(
    [c] => cherry
)
Array
(
    [a] => apple
    [b] => banana
)
Sebmil 05-Aug-2011 09:18
Looks like this function has a strange randomness.

If you take any number of elements in an array which has 40..100 elements, the 31st one is always by far the less occuring (by about 10% less than others).

I tried this piece of code at home (PHP Version 5.3.2-1ubuntu4.9) and on my server (PHP Version 5.2.17), unfortunately i haven't any server with the last version here :

<?php
$valeurs = range(1, 40);
$proba = array_fill(1, 40, 0);
for ($i = 0; $i < 10000; ++$i)
{
    $tirage_tab = array_rand($valeurs, 10);
    foreach($tirage_tab as $key => $value)
    {
        $proba[$valeurs[$value]]++;
    }
}

asort($proba);
echo "Proba : <br/>\n";
foreach($proba as $key => $value)
{
    echo "$key : $value<br/>\n";
}
?>

In every try, the number of occurrences change a bit but the 31 is always far less (around 2200) than the others (2400-2600). I tried with 50 and 100 elements, no change. I tried with more or less elements to pick (second parameter to array_rand), same result. If you pick only one element it's even worse : 31 has half the result of the others.

For this particular case, i recommend shuffling the array and taking the nth first elements, in this test it's 60% faster and the statistics are ok.
Anonymous 02-Oct-2009 10:04
If the array elements are unique, and are all integers or strings, here is a simple way to pick $n random *values* (not keys) from an array $array:

<?php array_rand(array_flip($array), $n); ?>
info at pavliga dot com 28-Dec-2008 10:52
If you want get unique range:

<?php
$n = 15;

$data = range(1, 20);
$rand = array_rand($data,$n);

for($i=0; $i<$n; $i++)
{
echo $rand[$i]."<br>";
}

?>
admin at jeremyzaretski dot com 15-Dec-2008 05:11
<?php
// a foreach friendly version of array_rand
function Select_Random_Indices($source_array, $count = 1)
{
    if($count > 0)
    {
        if($count == 1)
        {
            $result = array(array_rand($source_array, $count));
        }
        else
        {
            $result = array_rand($source_array, $count);
        }
    }
    else
    {
        $result = array();
    }

    return $result;
}

// using the above function to pick random values instead of entries
function Select_Random_Entries($source_array, $count = 1)
{
    $result = array();
    $index_array = Select_Random_Indices($source_array, $count);

    foreach($index_array as $index)
    {
        $result[$index] = $source_array[$index];
    }

    return $result;
}
?>
herodesh [-at_] gmail [-dot-] com 05-Sep-2007 12:36
this is to generate a random selection from an array with array_rand preety nice, can be used to generate random passwords or anything:

$my_array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "0", "1", "2", "3", "4", "5");
        for ($i=0; $i<=10; $i++)
        {
            $random = array_rand($my_array);
                        //this generates the random number from the array
            $parola .= $my_array[$random];
                        //here we will display the exact charachter from the array
        }
        echo $parola; // printing result
Frederick Lemasson aka djassper at gmail 04-Dec-2006 09:57
To select a random Value (not a Key) from a Multi-Dimentionnal array I made a recursive function : array_multi_rand()

the following exemple randomly selects an url from a multidimentionnal array :

<?

$Expos['Google']['Science']='news.google.fr/news?topic=t';
$Expos['Google']['Economie']='news.google.fr/news?topic=b';
$Expos['Google']['Sante']='news.google.fr/news?topic=m';
$Expos['Yahoo']='fr.news.yahoo.com';
$Expos['Events']['LogicielLibre']='agendadulibre.org';
$Expos['MyBlog']='www.kik-it.com';

function array_multi_rand($Zoo){
    $Boo=array_rand($Zoo);
    if(is_array($Zoo[$Boo])){
        return array_multi_rand($Zoo[$Boo]);
    }else{
        return $Zoo[$Boo];
    }
}

echo(array_multi_rand($Expos));

?>
JS 04-Sep-2006 12:07
I wanted to write something that picks a random entry from a 1column-MySQL database - simply Post Of The Moment (potm). I know there surly are many better ways to do it, but I`m rather new to PHP :)  Anyway, it`s simple and no-problem working code.
Of course I assume your DB exists and you always have something in it.

@$link = MySQL_Connect("localhost", "username", "password"); //connect to mysql
mySQL_Select_DB("database"); //..to DB
@$potms = MySQL_Query("SELECT * FROM potm"); //now we get all from our table and store it
MySQL_Close($link); //there`s no need for connection, so we should close it

$potm_array = ''; //sets variables to "zero" values
$i = 0;
while ($entry = MySQL_Fetch_Array($potms)) //now we go through our DB
       {
         $potm_array[$i] = $entry; //our temporary array from which we will random pick a field key
         $i++; //now we increment our field key
       }

$potm_id = array_rand($potw_array); //picks a random key from array
$potm = $potm_array[$potm_id]['name_of_the_field']; //now we have stored our Post Of The Moment in $potm

..hope this helps
dragonfly at dragonflyeye dot net 07-Jul-2006 04:57
Well, this is interesting.   I don't see anyone else commenting on this, so just in case you were planning to use this function like I was, be prepared: array_rand does not handle multidimensional arrays.  It just ends up returning a list of the X-axis values without the Y-axis arrays.  Bummer.  I'm going to have to find another way to do what I wanted.
trukin at gmail dot com 16-Mar-2006 10:23
Modify of last note:
<?php
if (!function_exists('array_rand')) {
    function array_rand($array, $lim=1) {
        mt_srand((double) microtime() * 1000000);
        for($a=0; $a<=$lim; $a++){
            $num[] = mt_srand(0, count($array)-1);
        }
        return @$num;
    }
}
?>

mt_rand generates a better random number, and with the limit.
Will-ster 02-Jan-2006 01:18
This is something I have been playing with for quite awhile. I'm very new to php, but i finally got it to work. it's a function that will take and array[$arrquo] and find a particular keyword[$find] in the different elements of the array then take those elements that posess that keyword and display them at random

<?php
function popbyword($arrquo,$find)
{
$newarr = array('');
 foreach($arrquo as $line)
 {
  if( strstr( $line, $find ) )
  {
    array_push($newarr, $line);

  }
 }
srand((double)microtime()*1000000);
$rquote = array_rand($newarr);
echo $newarr[$rquote];
}

popbyword($images, 'Albert');
?>

In my case I had this huge array of quotes with 90 some elements. I was able to find certain keywords in those elements then ONLY display the elements that had those keywords. NEAT! Maybe only because I'm new.
bjcffnet at gmail dot com 30-Aug-2005 01:29
As wazaawazaa600 at msn dot com pointed out, a multi-dimensional array doesn't work with this function. So, I hope I can help someone with this :)

<?php
/**
 * Returns a number of random elements from an array.
 *
 * It returns the number (specified in $limit) of elements from
 * $array. The elements are returned in a random order, exactly
 * as it was passed to the function. (So, it's safe for multi-
 * dimensional arrays, aswell as array's where you need to keep
 * the keys)
 *
 * @author Brendan Caffrey  <bjcffnet at gmail dot com>
 * @param  array  $array  The array to return the elements from
 * @param  int    $limit  The number of elements to return from
 *                            the array
 * @return array  The randomized array
 */
function array_rand_keys($array, $limit = 1) {
    $count = @count($array)-1;

    // Sanity checks
    if ($limit == 0 || !is_array($array) || $limit > $count) return array();
    if ($count == 1) return $array;

    // Loop through and get the random numbers
    for ($x = 0; $x < $limit; $x++) {
        $rand = rand(0, $count);

        // Can't have double randoms, right?
        while (isset($rands[$rand])) $rand = rand(0, $count);

        $rands[$rand] = $rand;
    }

    $return = array();
    $curr = current($rands);

    // I think it's better to return the elements in a random
    // order, which is why I'm not just using a foreach loop to
    // loop through the random numbers
    while (count($return) != $limit) {
        $cur = 0;

        foreach ($array as $key => $val) {
            if ($cur == $curr) {
                $return[$key] = $val;

                // Next...
                $curr = next($rands);
                continue 2;
            } else {
                $cur++;
            }
        }
    }

    return $return;
}
?>
alexkropivko at(dog) yandex dot ru 16-Jun-2005 03:21
There was a mistake at "Paul Hodel (paul at ue dot com dot br) 17-Apr-2003 04:40":
String
echo $new_input = $input[$v];

have to be:
echo $new_input[] = $input[$v];
maxnamara at yahoo dot com 13-Mar-2005 12:22
<?php
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");

function my_array_rand($input,$i=2){
srand((float) microtime() * 10000000);

$rand_keys = array_rand($input, $i);

/*
print $input[$rand_keys[0]] . "\n";
print $input[$rand_keys[1]] . "\n";
*/

$res = array();

if($i > 1){

for($a=0;$a<$i;$a++){

    $res[] = $input[$rand_keys[$a]];

}

}
else{

    $res[] = $input[$rand_keys];

}

return $res;
}

$a = my_array_rand($input,3);
echo "<pre>";
print_r($a);
echo "</pre>";
?>
yhoko at yhoko dot com 28-Aug-2004 03:16
According to office at at universalmetropolis dot com I have to say that the example is wrong.

<?php
// retrieve one of the options at random from the array
$teamcolours = $teamcolours[rand(0,count($teamcolours))];
?>

The count() function will return the number of items in the array, that's the last index + 1. So if there's 2 items in the array, count() will return 2 but the indices are 0 and 1. Now since rand(x,y) randomizes between x and y inclusively the index from the above example may be out of bounds. Thus you have to subtract 1 from the count:

<?php
    // Get random item
    $teamcolours = $teamcolours[rand(0,count($teamcolours)-1)];
?>
jpinedo 02-Nov-2003 10:15
An array of arrays example:

<?php
$banners[0]['imagen']="imagen0.gif";
$banners[0]['url']="www.nosenada.tal";

$banners[1]['imagen']="imagen1.gif";
$banners[1]['url']="www.nose.tal";

$banners[2]['imagen']="imagen2.gif";
$banners[2]['url']="pagina.html";

$banners[3]['imagen']="imagen3.jpg";
$banners[3]['url']="../pagina.php";

$id_banner = array_rand($banners);

echo  "Archivo:--".$banners[$id_banner]['imagen']. "<br />\n";
echo  "URL:-----".$banners[$id_banner]['url']. "<br />\n";
?>
asarnoNOSPAM@interbaun DOT com 24-Jun-2003 08:06
It is correct that using array_rand() with num_req=1 will return an integer and not an array, but why get so complicated with getting just the one value.  The K.I.S.S. method would suggest to do it this way:

<?
srand((double)microtime() * 10000000);
$originalArray = array("red", "blue", "green", "brown",
"cyan", "magenta", "purle", "cheezy");
$pickOne = array_rand($originalArray, 1);
$aRandomSelection = $originalArray[$pickOne ];
echo "$aRandomSelection was the random selection made";
?>

You only need to use the foreach if the num_req >=2. In those cases the array_rand() function will return an array of random elements which are a subset of the original array.  When num_req = 1, the array_rand() function returns an integer that signifies a randomly picked key of the original array.  Hope this clarifies things ... it works for me.
Paul Hodel (paul at ue dot com dot br) 17-Apr-2003 10:40
If you trying to get a randon array just use that... it's easier! And you have no repeats...

<?

srand ((float) microtime() * 10000000);

$input = array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank");

$keys = array_rand ($input, sizeof($input));

while (list($k, $v) = each($keys))
{
    echo $new_input = $input[$v];
}

?>
scandar at home dot se 13-Apr-2003 06:58
Note that the int num_req parameter is the required number of element to randomly select. So if your array has 3 element and num_req=4 then array_rand() will not return anything since it is impossible to select 4 random elements out of an array that only contains 3 elements. Many people think that they will get 3 elements returned but that is of course not the case.
mickoz[at]parodius[dot]com 02-Dec-2002 05:28
For those of you thinking that it does not work for num_req = 1, it is because it return a variable and not an array.  This mainly cause some problem with people using foreach.

The correct way to handle this is explained by that example:

<?php
$some_array = array("blah","bleh","foo","lele");

$nb_value = 1;

srand ((float) microtime() * 10000000);
$rand_keys = array_rand($some_array, $nb_value);

if(!is_array($rand_keys))
{
  $rand_keys = array($rand_keys);
}

print_r($rand_keys); // verify here the array of keys
echo "\n<BR>";
?>

// You can then correctly use the foreach, as it require an array to work
// If you use foreach with one element, it won't work.

<?php
$random_array = array();

foreach($rand_keys as $value)
{
  array_push($random_array, $some_array[$value]);
}

print_r($random_array);
?>
josh at 3io dot com 15-Jun-2002 12:20
I modified fake_array_rand to always only return 1 element, and did some benchmarks against calling array_rand with the second parameter as 1.  I ran 100 samples for each function for each number of elements and took the average result.  While the internal array_rand is faster for a small number of elements, it scales very poorly.

1 elements: 2.0619630813599E-05 sec. for array_rand,8.4352493286133E-05 sec. for fake_array_rand
10 elements: 2.1675825119019E-05 sec. for array_rand,8.427619934082E-05 sec. for fake_array_rand
100 elements: 2.9319524765015E-05 sec. for array_rand,8.4599256515503E-05 sec. for fake_array_rand
1000 elements: 0.0001157283782959 sec. for array_rand,8.5572004318237E-05 sec. for fake_array_rand
10000 elements: 0.0016669762134552 sec. for array_rand,8.5201263427734E-05 sec. for fake_array_rand
100000 elements: 0.015599734783173 sec. for array_rand,8.5580348968506E-05 sec. for fake_array_rand
1000000 elements: 0.18011983394623 sec. for array_rand,8.6690187454224E-05 sec. for fake_array_rand

<?php
function fake_array_rand ($array)
{
        $count = count ($array);
        # Help keep the number generator random :)
        $randval and usleep ("0.$randval");

        # Seed the random number generator
        # Generate a random number
        srand ((double) microtime() * 10000000);
        $randval = rand();

        # Use the random value to 'pick' an entry from the array
        # Count the number of times that the entry is picked
        ++$index[$randval % $count];

        return $array[$randval % $count];
}
?>
uvm at sun dot he dot net 10-Jul-2001 03:09
If you're just trying to draw a random subset of n elements from an array, it seems more effecient to do something like this:

<?php
function draw_rand_array($array,$draws)
{
        $lastIndex = count($array) - 1;
        $returnArr = array();
        while($draws > 1)
        {
                $rndIndex = rand(0,$lastIndex);
                array_push($returnArr,array_splice($array,$rndIndex,1));
                $draws--;
                $lastIndex--;
        }

        return $returnArr;
}
?>

No messing with indexes when you're done... you just have an array with the elements you're looking for in it.


콜백 함수를 사용하여 배열을 반복적으로 단일 값으로 축소

  • ≪ array_rand
  • array_replace_recursive ≫
  • PHP Manual
  • 배열 함수 목록
  • 콜백 함수를 사용하여 배열을 반복적으로 단일 값으로 축소

array_reduce

(PHP 4 >= 4.0.5, PHP 5, PHP 7)

array_reduce — 콜백 함수를 사용하여 배열을 반복적으로 단일 값으로 축소

설명

mixed array_reduce ( array $input , callback $function [, int $initial ] )

array_reduce()는 input 배열의 원소들에게 function 함수를 반복적으로 적용하여, 그 배열을 단일 값으로 축소시킨다.

인수

input

입력 배열.

function

콜백 함수.

initial

선택적인 initial을 설정하면, 처리의 시작부분에서 그 값이 사용되거나, 배열이 비어있는 경우에 최종 결과값으로 사용될 것이다.

반환값

결과 값을 반환합니다.

배열이 비어있고 initial이 없으면, array_reduce()는 NULL을 반환합니다.

예제

Example #1 array_reduce() 예제

<?php
function rsum($v, $w)
{
    $v += $w;
    return $v;
}

function rmul($v, $w)
{
    $v *= $w;
    return $v;
}

$a = array(1, 2, 3, 4, 5);
$x = array();
$b = array_reduce($a, "rsum");
$c = array_reduce($a, "rmul", 10);
$d = array_reduce($x, "rsum", 1);
?>

위 코드는 $b가 15를 가지고, $c는 1200 (= 10*1*2*3*4*5)을 가지고, $d는 1을 가지게 한다.

참고

  • array_filter() - 콜백 함수를 사용하여 배열 원소를 필터
  • array_map() - Applies the callback to the elements of the given arrays
  • array_unique() - 배열에서 중복된 값을 제거
  • array_count_values() - 배열 값의 수를 셉니다

User Contributed Notes

directrix1 at gmail dot com 04-Nov-2015 01:39
So, if you were wondering how to use this where key and value are passed in to the function. I've had success with the following (this example generates formatted html attributes from an associative array of attribute => value pairs):

<?php

    // Attribute List
    $attribs = [
        'name' => 'first_name',
        'value' => 'Edward'
    ];

    // Attribute string formatted for use inside HTML element
    $formatted_attribs = array_reduce(
        array_keys($attribs),                       // We pass in the array_keys instead of the array here
        function ($carry, $key) use ($attribs) {    // ... then we 'use' the actual array here
            return $carry . ' ' . $key . '="' . htmlspecialchars( $attribs[$key] ) . '"';
        },
        ''
    );

echo $formatted_attribs;

?>

This will output:
 name="first_name" value="Edward"
cwu at nolo dot com 19-Sep-2015 04:34
The single value returned by array_reduce() can be an array -- as illustrated in the following example:
<?php
# calculate the average of an array
function calculate_sum_and_count($sum_and_count, $item)
{
  list($sum, $count) = $sum_and_count;
  $sum += $item;
  $count += 1;
  return [$sum, $count];
}

$a = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$initial_sum_and_count = [0, 0];
list($sum, $count) = array_reduce($a, "calculate_sum_and_count", $initial_sum_and_count);
echo $sum / $count;
?>
aiadfaris at yahoo dot de 30-May-2014 05:16
notice to function array_reduce()
I suppose the function rsum in the example 1 so it is not correct,
but
$ v + = $ w;
will output 15
aiadfaris at yahoo dot de 30-May-2014 05:14
notice to function array_reduce()
I suppose the function rsum in the example 1 so it is not correct,
but
$ v + = $ w;
will output 15
magnesium dot oxide dot play+php at gmail dot com 11-Apr-2014 02:27
You can reduce a two-dimensional array into one-dimensional using array_reduce and array_merge. (PHP>=5.3.0)

<?php

$two_dimensional = array();
$two_dimensional['foo'] = array(1, 2, 3);
$two_dimensional['bar'] = array(4, 5, 6);

$one_dimensional = array_reduce($two_dimensional, 'array_merge', array());
# becomes array(1, 2, 3, 4, 5, 6)
Altreus 26-Feb-2014 11:30
You can effectively ignore the fact $result is passed into the callback by reference. Only the return value of the callback is accounted for.

<?php

$arr = [1,2,3,4];

var_dump(array_reduce(
    $arr,
    function(&$res, $a) { $res += $a; },
    0
));

# NULL

?>

<?php

$arr = [1,2,3,4];

var_dump(array_reduce(
    $arr,
    function($res, $a) { return $res + $a;  },
    0
));

# int(10)
?>

Be warned, though, that you *can* accidentally change $res if it's not a simple scalar value, so despite the examples I'd recommend not writing to it at all.
kon 03-Dec-2012 10:12
Walking down related object's properties using array_reduce:

<?php
  $a=new stdClass;
  $a->b=new stdClass;
  $a->b->c="Hello World!\n";

  $reductionPath=array("b","c");

  print_r(
    array_reduce(
      $reductionPath,
      function($result, $item){
        return $result->$item;
      },
      $a
    )
  );
?>
ruslan dot zavackiy at gmail dot com 06-Jul-2012 05:29
If you want something elegant in your code, when dealing with reducing array, just unshift first element, and use it as initial, because if you do not do so, you will + first element with first element:

<?php
$arr = array(
    array('min' => 1.5456, 'max' => 2.28548, 'volume' => 23.152),
    array('min' => 1.5457, 'max' => 2.28549, 'volume' => 23.152),
    array('min' => 1.5458, 'max' => 2.28550, 'volume' => 23.152),
    array('min' => 1.5459, 'max' => 2.28551, 'volume' => 23.152),
    array('min' => 1.5460, 'max' => 2.28552, 'volume' => 23.152),
);

$initial = array_shift($arr);

$t = array_reduce($arr, function($result, $item) {
    $result['min'] = min($result['min'], $item['min']);
    $result['max'] = max($result['max'], $item['max']);
    $result['volume'] += $item['volume'];

    return $result;
}, $initial);
?>
php at keith tyler dot com 19-Apr-2010 12:30
If you do not provide $initial, the first value used in the iteration is NULL. This is not a problem for callback functions that treat NULL as an identity (e.g. addition), but is a problem for cases when NULL is not identity (such as boolean context).

Compare:

<?php
function andFunc($a, $b) {
  return $a && $b;
}
$foo = array(true, true, true);
var_dump(array_reduce($foo, "andFunc"));
?>

returns false! One would expect that it would return true because `true && true && true == true`!

Adding diagnostic output to andFunc() shows that the first call to andFunc is with the arguments (NULL, true). This resolves to false (as `(bool) null == false`) and thereby corrupts the whole reduction.

So in this case I have to set `$initial = true` so that the first call to andFunc() will be (true, true). Now, if I were doing, say, orFunc(), I would have to set `$initial = false`. Beware.

Note that the "rmul" case in the example sneakily hides this defect! They use an $initial of 10 to get `10*1*2*3*4*5 = 12000`. So you would assume that without an initial, you would get `1200/10 = 120 = 1*2*3*4*5`. Nope! You get big fat zero, because `int(null)==0`, and `0*1*2*3*4*5 = 0`!

I don't honestly see why array_reduce starts with a null argument. The first call to the callback should be with arguments ($initial[0],$initial[1]) [or whatever the first two array entries are], not (null,$initial[0]). That's what one would expect from the description.

Incidentally this also means that under the current implementation you will incur `count($input)` number of calls to the callback, not `count($input) - 1` as you might expect.
Hayley Watson 23-Oct-2007 11:49
To make it clearer about what the two parameters of the callback are for, and what "reduce to a single value" actually means (using associative and commutative operators as examples may obscure this).

The first parameter to the callback is an accumulator where the result-in-progress is effectively assembled. If you supply an $initial value the accumulator starts out with that value, otherwise it starts out null.
The second parameter is where each value of the array is passed during each step of the reduction.
The return value of the callback becomes the new value of the accumulator. When the array is exhausted, array_reduce() returns accumulated value.

If you carried out the reduction by hand, you'd get something like the following lines, every one of which therefore producing the same result:
<?php
array_reduce(array(1,2,3,4), 'f',         99             );
array_reduce(array(2,3,4),   'f',       f(99,1)          );
array_reduce(array(3,4),     'f',     f(f(99,1),2)       );
array_reduce(array(4),       'f',   f(f(f(99,1),2),3)    );
array_reduce(array(),        'f', f(f(f(f(99,1),2),3),4) );
f(f(f(f(99,1),2),3),4)
?>

If you made function f($v,$w){return "f($v,$w)";} the last line would be the literal result.

A PHP implementation might therefore look something like this (less details like error checking and so on):
<?php
function array_reduce($array, $callback, $initial=null)
{
    $acc = $initial;
    foreach($array as $a)
        $acc = $callback($acc, $a);
    return $acc;
}
?>
yuki [dot] kodama [at] gmail [dot] com 31-Jan-2007 05:56
This code will reduce array deeply.

<?php
function print_s($s) {
    return is_null($s) ? "NULL" : (is_array($s) ? "Array" : ($s ? "TRUE" : "FALSE"));
}
function r_and_dp($a, $b) {
    echo "phase1:" . print_s($a) . "," . print_s($b) . "<br>\n";
    if(is_array($a)) {
        $a = array_reduce($a, "r_and_dp");
    }
    if(is_array($b)) {
        $b = array_reduce($b, "r_and_dp");
    }
    echo "phase2:" . print_s($a) . "," . print_s($b) . "<br>\n";
    $a = is_null($a) ? TRUE : $a;
    $b = is_null($b) ? TRUE : $b;
    echo "phase3:" . print_s($a) . "," . print_s($b) . "<br>\n";
    return $a && $b;
}
$bools = array(TRUE, array(FALSE, TRUE), TRUE);
echo print_s(array_reduce($bools, "r_and_dp")) . "<br>\n";

// result: FALSE
?>

When using boolean, you have to carefully set an "initial" argument.

<?php
function r_or_dp($a, $b) {
    if(is_array($a)) {
        $a = array_reduce($a, "r_or_dp");
    }
    if(is_array($b)) {
        $b = array_reduce($b, "r_or_dp");
    }
    return (is_null($a) ? FALSE : $a) || (is_null($b) ? FALSE : $b);
}
?>
bdechka at yahoo dot ca 10-Jan-2007 09:57
The above code works better this way.

<?php
function reduceToTable($html, $p) {
   $html .= "<TR><TD><a href=\"$p.html\">$p</a></td></tr>\n";
   return $html;
}

$list = Array("page1", "page2", "page3");

$tab = array_reduce($list, "reduceToTable");
echo "<table>".$tab . "</table>\n";
?>
Seanj.jcink.com 05-Jan-2006 03:23
The code posted below by bishop to count the characters of an array is simply... erm... well useless to me...

$array=Array("abc","de","f");
strlen(implode("",$array)); //6

works; and is much smaller. Probably much faster too.


Replaces elements from passed arrays into the first array recursively

  • ≪ array_reduce
  • array_replace ≫
  • PHP Manual
  • 배열 함수 목록
  • Replaces elements from passed arrays into the first array recursively

array_replace_recursive

(PHP 5 >= 5.3.0, PHP 7)

array_replace_recursive — Replaces elements from passed arrays into the first array recursively

설명

array array_replace_recursive ( array $array1 , array $array2 [, array $... ] )

array_replace_recursive() replaces the values of array1 with the same values from all the following arrays. If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as is. If several arrays are passed for replacement, they will be processed in order, the later array overwriting the previous values.

array_replace_recursive() is recursive : it will recurse into arrays and apply the same process to the inner value.

When the value in array1 is scalar, it will be replaced by the value in array2, may it be scalar or array. When the value in array1 and array2 are both arrays, array_replace_recursive() will replace their respective value recursively.

인수

array1

The array in which elements are replaced.

array2

The array from which elements will be extracted.

...

Optional. More arrays from which elements will be extracted.

반환값

Returns an array, or NULL if an error occurs.

예제

Example #1 array_replace_recursive() example

<?php
$base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), );
$replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));

$basket = array_replace_recursive($base, $replacements);
print_r($basket);

$basket = array_replace($base, $replacements);
print_r($basket);
?>

위 예제의 출력:

Array
(
    [citrus] => Array
        (
            [0] => pineapple
        )

    [berries] => Array
        (
            [0] => blueberry
            [1] => raspberry
        )

)
Array
(
    [citrus] => Array
        (
            [0] => pineapple
        )

    [berries] => Array
        (
            [0] => blueberry
        )

)

Example #2 array_replace_recursive() and recursive behavior

<?php
$base = array('citrus' => array("orange") , 'berries' => array("blackberry", "raspberry"), 'others' => 'banana' );
$replacements = array('citrus' => 'pineapple', 'berries' => array('blueberry'), 'others' => array('litchis'));
$replacements2 = array('citrus' => array('pineapple'), 'berries' => array('blueberry'), 'others' => 'litchis');

$basket = array_replace_recursive($base, $replacements, $replacements2);
print_r($basket);

?>

위 예제의 출력:

Array
(
    [citrus] => Array
        (
            [0] => pineapple
        )

    [berries] => Array
        (
            [0] => blueberry
            [1] => raspberry
        )

    [others] => litchis
)

참고

  • array_replace() - Replaces elements from passed arrays into the first array
  • array_merge_recursive() - 두개 이상의 배열을 재귀적으로 병합

User Contributed Notes

oliver dot coleman at gmail dot com 26-Nov-2014 05:20
If you came here looking for a function to recursively find and replace (scalar) values in an array (also recurses through objects):

<?php
define('RECURSIVE_REPLACE_MARKER', 'recursive_replace_r47yr74yr7623t74r3645236rtvghdcbnsgxbt67g5e21873891');
/**
 * Recursively replaces scalar values in objects and arrays. The passed array or object is altered in place.
 * @param mixed $data An object or array.
 * @param mixed $find The scalar value to find.
 * @param mixed $replace The value to replace with (need not be a scalar).
 */
function recursive_replace(&$data, $find, $replace){
  if(is_array($data)) {
    if (!isset($data[RECURSIVE_REPLACE_MARKER])) {
      $data[RECURSIVE_REPLACE_MARKER] = TRUE;
      foreach($data as $key=>$val) {
        if(is_array($data[$key]) || is_object($data[$key])) {
          recursive_replace($data[$key], $find, $replace);
        }
        else{
          if($data[$key] === $find) {
            $data[$key] = $replace;
          }
        }
      }
      unset($data[RECURSIVE_REPLACE_MARKER]);
    }
  }
  elseif (is_object($data)) {
    if (!isset($data->RECURSIVE_REPLACE_MARKER)) {
      $data->RECURSIVE_REPLACE_MARKER = TRUE;
      foreach($data as $key=>$val) {
        if(is_array($data->$key) || is_object($data->$key)){
          recursive_replace($data->$key, $find, $replace);
        }else{
          if($data->$key === $find) {
            $data->$key = $replace;
          }
        }
      }
      unset($data->RECURSIVE_REPLACE_MARKER);
    }
  }
}
?>
msahagian at dotink dot org 13-Jul-2012 08:33
This is a fairly concise version which does not rely on traditional recursion:

<?php
    function array_replace_recursive($base, $replacements)
    {
        foreach (array_slice(func_get_args(), 1) as $replacements) {
            $bref_stack = array(&$base);
            $head_stack = array($replacements);

            do {
                end($bref_stack);

                $bref = &$bref_stack[key($bref_stack)];
                $head = array_pop($head_stack);

                unset($bref_stack[key($bref_stack)]);

                foreach (array_keys($head) as $key) {
                    if (isset($key, $bref) && is_array($bref[$key]) && is_array($head[$key])) {
                        $bref_stack[] = &$bref[$key];
                        $head_stack[] = $head[$key];
                    } else {
                        $bref[$key] = $head[$key];
                    }
                }
            } while(count($head_stack));
        }

        return $base;
    }
?>
Gregor at der-meyer dot de 30-Jul-2009 03:13
Nice that this function finally found its was to the PHP core! If you want to use it also with older PHP versions before 5.3.0, you can define it this way:

<?php
if (!function_exists('array_replace_recursive'))
{
  function array_replace_recursive($array, $array1)
  {
    function recurse($array, $array1)
    {
      foreach ($array1 as $key => $value)
      {
        // create new key in $array, if it is empty or not an array
        if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key])))
        {
          $array[$key] = array();
        }

        // overwrite the value in the base array
        if (is_array($value))
        {
          $value = recurse($array[$key], $value);
        }
        $array[$key] = $value;
      }
      return $array;
    }

    // handle the arguments, merge one by one
    $args = func_get_args();
    $array = $args[0];
    if (!is_array($array))
    {
      return $array;
    }
    for ($i = 1; $i < count($args); $i++)
    {
      if (is_array($args[$i]))
      {
        $array = recurse($array, $args[$i]);
      }
    }
    return $array;
  }
}
?>

I called this function array_merge_recursive_overwrite() in my older projects, but array_replace_recursive() sounds quite better while they do the same.

If you implemented such a compatible function before and don't want to refactor all your code, you can update it with the following snippet to use the native (and hopefully faster) implementation of PHP 5.3.0, if available. Just start your function with these lines:

<?php
  // as of PHP 5.3.0 array_replace_recursive() does the work for us
  if (function_exists('array_replace_recursive'))
  {
    return call_user_func_array('array_replace_recursive', func_get_args());
  }
?>
kyle [dot] florence [@t] gmail [dot] com 15-Jul-2009 01:16
This might help out people who don't have 5.3 running:

<?php

// Joins two or more arrays together recursively; key/value pairs of the first
// array are replaced with key/value pairs from the subsequent arrays.  Any
// key/value pair not present in the first array is added to the final array
function array_join()
{
    // Get array arguments
    $arrays = func_get_args();

    // Define the original array
    $original = array_shift($arrays);

    // Loop through arrays
    foreach ($arrays as $array)
    {
        // Loop through array key/value pairs
        foreach ($array as $key => $value)
        {
            // Value is an array
            if (is_array($value))
            {
                // Traverse the array; replace or add result to original array
                $original[$key] = array_join($original[$key], $array[$key]);
            }

            // Value is not an array
            else
            {
                // Replace or add current value to original array
                $original[$key] = $value;
            }
        }
    }

    // Return the joined array
    return $original;
}

?>


Replaces elements from passed arrays into the first array

  • ≪ array_replace_recursive
  • array_reverse ≫
  • PHP Manual
  • 배열 함수 목록
  • Replaces elements from passed arrays into the first array

array_replace

(PHP 5 >= 5.3.0, PHP 7)

array_replace — Replaces elements from passed arrays into the first array

설명

array array_replace ( array $array1 , array $array2 [, array $... ] )

array_replace() replaces the values of array1 with values having the same keys in each of the following arrays. If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as is. If several arrays are passed for replacement, they will be processed in order, the later arrays overwriting the previous values.

array_replace() is not recursive : it will replace values in the first array by whatever type is in the second array.

인수

array1

The array in which elements are replaced.

array2

The array from which elements will be extracted.

...

More arrays from which elements will be extracted. Values from later arrays overwrite the previous values.

반환값

Returns an array, or NULL if an error occurs.

예제

Example #1 array_replace() example

<?php
$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");

$basket = array_replace($base, $replacements, $replacements2);
print_r($basket);
?>

위 예제의 출력:

Array
(
    [0] => grape
    [1] => banana
    [2] => apple
    [3] => raspberry
    [4] => cherry
)

참고

  • array_replace_recursive() - Replaces elements from passed arrays into the first array recursively
  • array_merge() - 하나 이상의 배열을 병합

User Contributed Notes

ali dot sweden19 at yahoo dot com 25-Jan-2016 11:42
Here is a simple array_replace_keys function:

/**
     * This function replaces the keys of an associate array by those supplied in the keys array
     *
     * @param $array target associative array in which the keys are intended to be replaced
     * @param $keys associate array where search key => replace by key, for replacing respective keys
     * @return  array with replaced keys
     */
    private function array_replace_keys($array, $keys)
    {
        foreach ($keys as $search => $replace) {
            if ( isset($array[$search])) {
                $array[$replace] = $array[$search];
                unset($array[$search]);
            }
        }

        return $array;
    }

// Test Drive

print_r(array_replace_keys(['one'=>'apple', 'two'=>'orange'], ['one'=>'ett', 'two'=>'tvo']);
// Output
array(
'ett'=>'apple',
'tvo'=>'orange'
)
projacore at gmail dot com 27-Jul-2015 04:45
You can also use:

<?php
$myarray = [
"Orange",
"572" => "Banana",
"omg" => "Chili",
"nevermind" => "mango"
];

$myarray[0] = "NO-Orange";
$myarray["572"] = "BANANAPHONE!!!";
$myarray["omg"] = "NO-Chili";

print_r($myarray);

?>

RESULT:
Array
(
    [0] => NO-Orange
    [572] => BANANAPHONE!!!
    [omg] => NO-Chili
    [nevermind] => mango
)

with regards
Anonymous 09-May-2015 04:17
The documentation is wrongly phrased: "array_replace() replaces the values of array1"  No replacing is done. A new array is created which looks like the one that would have resulted from the described replacement.

If you want to augment the set of indices in an array, use
       array_to_be_modified += array_with_indices_to_add;
lm713 15-Oct-2014 10:40
If the arrays are associative (that is, their keys are strings), then I believe this function is identical to (the older) array_merge.
kyberprizrak 15-Jun-2014 10:08
if(!function_exists('array_replace'))
{
  function array_replace()
  {
    $args = func_get_args();
    $num_args = func_num_args();
    $res = array();
    for($i=0; $i<$num_args; $i++)
    {
      if(is_array($args[$i]))
      {
        foreach($args[$i] as $key => $val)
        {
          $res[$key] = $val;
        }
      }
      else
      {
        trigger_error(__FUNCTION__ .'(): Argument #'.($i+1).' is not an array', E_USER_WARNING);
        return NULL;
      }
    }
    return $res;
  }
}
marvin_elia at web dot de 18-Mar-2014 08:45
Simple function to replace array keys. Note you have to manually select wether existing keys will be overrided.

/**
  * @param array $array
  * @param array $replacements
  * @param boolean $override
  * @return array
  */
function array_replace_keys(array $array, array $replacements, $override = false) {
    foreach ($replacements as $old => $new) {
        if(is_int($new) || is_string($new)){
            if(array_key_exists($old, $array)){
                if(array_key_exists($new, $array) && $override === false){
                    continue;
                }
                $array[$new] = $array[$old];
                unset($array[$old]);
            }
        }
    }
    return $array;
}
gmastro77 at gmail dot com 11-Apr-2013 02:05
In some cases you might have a structured array from the database and one
of its nodes goes like this;

<?php
# a random node structure
$arr    = array(
    'name'  => 'some name',
    'key2'  => 'value2',
    'title' => 'some title',
    'key4'  => 4,
    'json'  => '[1,0,1,1,0]'
);

# capture these keys values into given order
$keys   = array( 'name', 'json', 'title' );
?>

Now consider that you want to capture $arr values from $keys.
Assuming that you have a limitation to display the content into given keys
order, i.e. use it with a vsprintf, you could use the following

<?php
# string to transform
$string = "<p>name: %s, json: %s, title: %s</p>";

# flip keys once, we will use this twice
$keys   = array_flip( $keys );

# get values from $arr
$test   = array_intersect_key( $arr, $keys );

# still not good enough
echo vsprintf( $string, $test );
// output --> name: some name, json: some title, title: [1,0,1,1,0]

# usage of array_replace to get exact order and save the day
$test   = array_replace( $keys, $test );

# exact output
echo vsprintf( $string, $test );
// output --> name: some name, json: [1,0,1,1,0], title: some title

?>

I hope that this will save someone's time.
steelpandrummer 14-Aug-2012 06:19
<?php
// we wanted the output of only selected array_keys from a big array from a csv-table
// with different order of keys, with optional suppressing of empty or unused values

$values = array
(
    'Article'=>'24497',
    'Type'=>'LED',
    'Socket'=>'E27',
    'Dimmable'=>'',
    'Wattage'=>'10W'
);

$keys = array_fill_keys(array('Article','Wattage','Dimmable','Type','Foobar'), ''); // wanted array with empty value

$allkeys = array_replace($keys, array_intersect_key($values, $keys));    // replace only the wanted keys

$notempty = array_filter($allkeys, 'strlen'); // strlen used as the callback-function with 0==false

print '<pre>';
print_r($allkeys);
print_r($notempty);

/*
Array
(
    [Article] => 24497
    [Wattage] => 10W
    [Dimmable] =>
    [Type] => LED
    [Foobar] =>
)
Array
(
    [Article] => 24497
    [Wattage] => 10W
    [Type] => LED
)
*/
?>
sun at drupal dot org 27-May-2011 08:42
Instead of calling this function, it's often faster and simpler to do this instead:

<?php
$array_replaced = $array2 + $array1;
?>

If you need references to stay intact:

<?php
$array2 += $array1;
?>
polecat at p0lecat dot com 29-Nov-2010 07:02
I got hit with a noob mistake. :)

When the function was called more than once, it threw a function redeclare error of course.  The enviroment I was coding in never called it more than once but I caught it in testing and here is the fully working revision.  A simple logical step was all that was needed.

With PHP 5.3 still unstable for Debian Lenny at this time and not knowing if array_replace would work with multi-dimensional arrays, I wrote my own.  Since this site has helped me so much, I felt the need to return the favor. :)

<?php
        // Polecat's Multi-dimensional array_replace function
        // Will take all data in second array and apply to first array leaving any non-corresponding values untouched and intact
        function polecat_array_replace( array &$array1, array &$array2 ) {
            // This sub function is the iterator that will loop back on itself ad infinitum till it runs out of array dimensions
            if(!function_exists('tier_parse')){
                function tier_parse(array &$t_array1, array&$t_array2) {
                    foreach ($t_array2 as $k2 => $v2) {
                        if (is_array($t_array2[$k2])) {
                            tier_parse($t_array1[$k2], $t_array2[$k2]);
                        } else {
                            $t_array1[$k2] = $t_array2[$k2];
                        }
                    }
                    return $t_array1;
                }
            }

            foreach ($array2 as $key => $val) {
                if (is_array($array2[$key])) {
                    tier_parse($array1[$key], $array2[$key]);
                } else {
                    $array1[$key] = $array2[$key];
                }
            }
            return $array1;
        }
?>

[I would also like to note] that if you want to add a single dimensional array to a multi, all you must do is pass the matching internal array key from the multi as the initial argument as such:

<?php
$array1 = array( "berries" => array( "strawberry" => array( "color" => "red", "food" => "desserts"), "dewberry" = array( "color" => "dark violet", "food" => "pies"), );

$array2 = array( "food" => "wine");

$array1["berries"]["dewberry"] = polecat_array_replace($array1["berries"]["dewberry"], $array2);
?>

This is will replace the value for "food" for "dewberry" with "wine".

The function will also do the reverse and add a multi to a single dimensional array or even a 2 tier array to a 5 tier as long as the heirarchy tree is identical.

I hope this helps atleast one person for all that I've gained from this site.
polecat at p0lecat dot com 29-Nov-2010 06:57
I would like to add to my previous note about my polecat_array_replace function that if you want to add a single dimensional array to a multi, all you must do is pass the matching internal array key from the multi as the initial argument as such:

$array1 = array( "berries" => array( "strawberry" => array( "color" => "red", "food" => "desserts"), "dewberry" = array( "color" => "dark violet", "food" => "pies"), );

$array2 = array( "food" => "wine");

$array1["berries"]["dewberry"] = polecat_array_replace($array1["berries"]["dewberry"], $array2);

This is will replace the value for "food" for "dewberry" with "wine".

The function will also do the reverse and add a multi to a single dimensional array or even a 2 tier array to a 5 tier as long as the heirarchy tree is identical.

I hope this helps atleast one person for all that I've gained from this site.
mail at romansklenar dot cz 11-Dec-2009 04:01
To get exactly same result like in PHP 5.3, the foreach loop in your code should look like:

<?php
...
$count = func_num_args();

for ($i = 1; $i < $count; $i++) {
   ...
}
...
?>

Check on this code:

<?php
$base = array('id' => NULL, 'login' => NULL, 'credit' => NULL);
$arr1 = array('id' => 2, 'login' => NULL, 'credit' => 5);
$arr2 = array('id' => NULL, 'login' => 'john.doe', 'credit' => 100);
$result = array_replace($base, $arr1, $arr2);

/*
correct output:

array(3) {
   "id" => NULL
   "login" => string(8) "john.doe"
   "credit" => int(100)
}

your output:

array(3) {
   "id" => int(2)
   "login" => NULL
   "credit" => int(5)
}
*/
?>

Function array_replace "replaces elements from passed arrays into the first array" -- this means replace from top-right to first, then from top-right - 1 to first, etc, etc...
tufan dot oezduman at googlemail dot com 06-Nov-2009 01:19
a little enhancement to dyer85 at gmail dot com's function below:
<?php
if (!function_exists('array_replace'))
{
  function array_replace( array &$array, array &$array1, $filterEmpty=false )
  {
    $args = func_get_args();
    $count = func_num_args()-1;

    for ($i = 0; $i < $count; ++$i) {
      if (is_array($args[$i])) {
        foreach ($args[$i] as $key => $val) {
            if ($filterEmpty && empty($val)) continue;
            $array[$key] = $val;
        }
      }
      else {
        trigger_error(
          __FUNCTION__ . '(): Argument #' . ($i+1) . ' is not an array',
          E_USER_WARNING
        );
        return NULL;
      }
    }

    return $array;
  }
}
?>

this will allow you to "tetris-like" merge arrays:

<?php

$a= array(
    0 => "foo",
    1 => "",
    2 => "baz"
);
$b= array(
    0 => "",
    1 => "bar",
    2 => ""
);

print_r(array_replace($a,$b, true));

?>
results in:
Array
(
    [0] => foo
    [1] => bar
    [2] => baz
)


원소를 역순으로 가지는 배열을 반환

  • ≪ array_replace
  • array_search ≫
  • PHP Manual
  • 배열 함수 목록
  • 원소를 역순으로 가지는 배열을 반환

array_reverse

(PHP 4, PHP 5, PHP 7)

array_reverse — 원소를 역순으로 가지는 배열을 반환

설명

array array_reverse ( array $array [, bool $preserve_keys ] )

입력 array를 받아서 원소를 역순으로 가지는 새 배열을 반환합니다.

인수

array

입력 배열.

preserve_keys

TRUE로 지정하면 키를 보존합니다.

반환값

역순 배열을 반환합니다.

변경점

버전 | 설명
4.0.3 | preserve_keys 인수 추가.

예제

Example #1 array_reverse() 예제

<?php
$input  = array("php", 4.0, array("green", "red"));
$result = array_reverse($input);
$result_keyed = array_reverse($input, true);
?>

$result와 $result_keyed가 동일한 원소를 가지지만, 키가 다릅니다. $result와 $result_keyed의 출력은:

Array
(
    [0] => Array
        (
            [0] => green
            [1] => red
        )

    [1] => 4
    [2] => php
)
Array
(
    [2] => Array
        (
            [0] => green
            [1] => red
        )

    [1] => 4
    [0] => php
)

참고

  • array_flip() - 배열 안의 모든 키를 각 키의 연관 값과 교체

User Contributed Notes

atulmvAThotmailDOTcoDOTuk 29-Aug-2012 08:48
Here are a couple of routines to swap the order of two elements in an array

<?php

function array_swap_forward($arr,$elem)
{
$ndx = array_search($elem,$arr) - 1;
$b4 = array_slice($arr,0,$ndx);
$mid = array_reverse(array_slice($arr,$ndx,2));
$after = array_slice($arr,$ndx + 2);

return array_merge($b4,$mid,$after);
}

function array_swap_back($arr,$elem)
{
$ndx = array_search($elem,$arr);
$b4 = array_slice($arr,0,$ndx);
$mid = array_reverse(array_slice($arr,$ndx,2));
$after = array_slice($arr,$ndx + 2);

return array_merge($b4,$mid,$after);
}

$arr =array('a','b','c','d','e','f');
print_r(array_swap_forward($arr,'c'));
echo '<br>';
print_r(array_swap_back($arr,'c'));
?>
alfbennett at gmail dot com 06-Apr-2010 10:41
Needed to just reverse keys. Don't flog me if there is a better way. This was a simple solution.

<?php
function array_reverse_keys($ar){
    return array_reverse(array_reverse($ar,true),false);
}
?>
rahulavhad at yahoo dot com 30-Dec-2000 05:31
This code can help in recursive reversing of the array...

<?php
$arr1 = array(2,1,array(5,2,1,array(9,8,7)),5,0);
$arr1 = array_reverse($arr1);

function Reverse_Array($array)
{    $index = 0;
    foreach ($array as $subarray)
    {    if (is_array($subarray))
        {    $subarray = array_reverse($subarray);
            $arr = Reverse_Array($subarray);
            $array[$index] = $arr;
        }
        else {$array[$index] = $subarray;}
        $index++;
    }
    return $array;
}

$arr2 = Reverse_Array($arr1);
?>


주어진 값으로 배열을 검색하여 성공시 해당하는 키를 반환

  • ≪ array_reverse
  • array_shift ≫
  • PHP Manual
  • 배열 함수 목록
  • 주어진 값으로 배열을 검색하여 성공시 해당하는 키를 반환

array_search

(PHP 4 >= 4.0.5, PHP 5, PHP 7)

array_search — 주어진 값으로 배열을 검색하여 성공시 해당하는 키를 반환

설명

mixed array_search ( mixed $needle , array $haystack [, bool $strict ] )

haystack에서 needle을 찾습니다.

인수

needle

찾는 값.

Note:

needle이 문자열이면, 대소문자를 구분하여 비교합니다.

haystack

배열.

strict

세번째 인수 strict를 TRUE로 설정하면, array_search() 함수는 haystack에서 needle의 자료형도 확인합니다.

반환값

배열에서 needle을 찾으면 키를 반환하고, 아니면 FALSE를 반환합니다.

haystack에서 needle을 두 번 이상 발견하면, 처음 매치한 키를 반환합니다. 매치하는 모든 값에 대해 키를 반환받으려면, array_keys()에 선택적인 search_value 인수를 사용하십시오.

Warning

이 함수는 논리 FALSE를 반환하지만, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

변경점

버전 | 설명
4.2.0 | PHP 4.2.0 이전에, array_search()는 실패시에 FALSE 대신 NULL을 반환했습니다.

예제

Example #1 array_search() 예제

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;
?>

참고

  • array_keys() - 배열의 모든 키를 반환
  • array_values() - 배열의 모든 값을 반환
  • array_key_exists() - 주어진 키와 인덱스가 배열에 존재하는지 확인
  • in_array() - 값이 배열 안에 존재하는지 확인

User Contributed Notes

stocki dot r at gmail dot com 11-Oct-2016 05:26
Be careful, PHP will cast float keys to integers:
nl1.php.net/manual/en/language.types.array.php

<?php
$array = array(
    0.9 => 'abc'
);
$key = array_search('abc', $array); // $key = 0;
?>

Use string keys instead:

<?php
$array = array(
    '0.9' => 'abc'
);
$key = array_search('abc', $array); // $key = '0.9';
?>
hyder7nh at gmail dot com 23-Dec-2015 05:34
When using array_search in if statements with array elements that use integers for keys, if the key [0] matches, the statement would always return false.

This is because array_search returns the key of the value that matches with the first argument. If statements check if the return value is true or false(boolean), 0 is boolean for false.

This problem shouldn't occur with key names that are strings.

<?php

//$f would be 0, because key [0] matches the first argument
$f = array_search('photo', array('photo', 'audio'));

//$f statement would fall to else condition because $f is 0 and 0 is false
if($f==true)
{
    echo 'found<br/>';
}
else
{
    echo 'not found<br/>';
}

if(array_search('photo', array('photo')))
{
    echo 'FOUND';
}
else
{
    echo 'NOT FOUND';
}

Use the === operator to get around this problem.
maciej at speccode dot com 18-Aug-2015 02:58
FYI, remember that strict mode is something that might save you hours.

If you're searching for a string and you have a "true" boolean on the way - you will get it as result (first occurrence). Example below:

<?php

$arr = [
    'foo'    => 'bar',
    'abc'    => 'def',
    'bool'   => true,
    'target' => 'xyz'
];

var_dump( array_search( 'xyz', $arr ) ); //bool
var_dump( array_search( 'xyz', $arr, true ) ); //target

?>
raat1979 at gmail dot com 09-Jul-2015 06:15
I needed to search for sequences of values in an array,
In addition I needed to search from a given offset.
Unfortunately for neither I could find a function to do this.

This Is how I did it,
The function returns the position where position 0 of the needle matches
<?php
function array_find($needle,array $haystack,$offset=0,$length=null,$strict = false,$returnAll = false,$resultsBeforeStart=false){
    if(count($haystack)===0) return false; //empty haystack never contain a needle
    if($offset>count($haystack)) return false; //looking higher then the top of the haystack will never find anything
    if($offset!==0)    $haystack = array_slice($haystack, $offset, $length , true);
    if(is_array($needle) && count($needle)===1) $needle = array_pop($needle);
    if(!is_array($needle)){
        $matches = array_keys($haystack,$needle,$strict);
    }else{
        $needleKeys = array_keys($needle); //becomes array(0=>1,1=>4);
        $needlePositions = array();
        foreach($needleKeys as $key=>$pos){
            $needlePartPositions = array_keys($haystack,$needle[$pos],$strict);
            if(count($needlePartPositions)===0) return false;
            foreach($needlePartPositions as $fkey=>$fval){
                //we correct the found fosition based on the position in the needle, as a result
                //all matches set at the match position relative to start of needle
                $matchStart = $fval-$pos;
                if($matchStart >= 0 && ($matchStart>=$offset || $resultsBeforeStart)){
                    if(!isset($needlePositions[$matchStart])){
                        $needlePositions[$matchStart] = 1;
                    }else{
                        $needlePositions[$matchStart]++;
                    }
                }

            }
        }
        $matches = array_keys($needlePositions,count($needle),true);            //find all matches for which the position count matches the needle count
    }
    if($matches === false) return false;
    if(count($matches)===0) return false;
    if($returnAll) return $matches;
    return array_shift($matches);

}

$needle1 = array(1=>'D',4=>'G');
$needle2 = array('D','E');
$needle3 = array(1=>'D',2=>'E');

                // 0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25
$haystack = array('A','B','C','D','E','F','G','H','I','J','H','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                //26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  55  46  47  48  49  50  51
                  'A','B','C','D','E','F','G','H','I','J','H','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                //52  53  54  55  56      57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76
                  'A','B','C','D','E',    'G','H','I','J','H','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

var_dump(array_find($needle1,$haystack,0,null,true,true));
var_dump(array_find($needle1,$haystack,29,null,true,true));
var_dump(array_find($needle1,$haystack,29,null,true,true,true));

var_dump(array_find($needle2,$haystack,0,null,true,true));
var_dump(array_find($needle2,$haystack,55,null,true,false));
var_dump(array_find($needle2,$haystack,56,null,true,false,true));

?>
// D is at position 3 and 29 but they are matched at position 1 of the needle
// position 0 of the needle would therefore be at 2 and 28
array(2) {
  [0]=>  int(2)
  [1]=>  int(28)
}

//no match after position 29 that is after the start position
bool(false)

// items in the needle are matched at position 29 but needle starts at 1, $resultsBeforeStart was true so this one is included
array(1) {
  [0]=>  int(28)
}

// D,E found at 3 positions
array(3) {
  [0]=>  int(3)
  [1]=>  int(29)
  [2]=>  int(55)
}

//search for D,E starting at position 55 finds 1 match
int(55)

//search for D,E starting at position 56 finds NO matches, even if $resultsBeforeStart is set to true because the needle starts at position 0
bool(false)
superbueno at gmail dot com 01-May-2015 08:19
Like many others, === failed for a 0-indexed occurrence.  My solution was to test if an integer returned:

if ( is_int ( array_search($value, $array) ) ) return true;
emanuel dot tavares at connix dot com dot br 20-Mar-2015 12:47
Hi!

I've tried to use array_search to fill an array with registers from a database seach with this:

$registros = 0;

while ( $row = $this->result->fetch_array() )
{
      if ( !array_search($row[cd_subsecao], $this->listaSecoes) )
                  $registros = array_push($this->listaSecoes, $row[cd_subsecao]);
}

But the result was:

Array
(
    [0] => obr
    [1] => obr
    [2] => obr
    [3] => a10
    [4] => obr
    [5] => obr
    [6] => obr
)

It was caused by because the first element has repetitions.

Then I've tried to substitute the !array_search.... for array_search(...) == FALSE, but it didn't worked too.

The solution was to use the "===":

$registros = 0;

while ( $row = $this->result->fetch_array() )
{
      if ( array_search($row[cd_subsecao], $this->listaSecoes) === FALSE)
                  $registros = array_push($this->listaSecoes, $row[cd_subsecao]);
}

It returned:

Array
(
    [0] => obr
    [1] => a10
)
xfoxawy at gmail dot com 02-Feb-2015 08:30
in (PHP 5 >= 5.5.0) you don't have to write your own function to search through a multi dimensional array

ex :

$userdb=Array
(
    (0) => Array
        (
            (uid) => '100',
            (name) => 'Sandra Shush',
            (url) => 'urlof100'
        ),

    (1) => Array
        (
            (uid) => '5465',
            (name) => 'Stefanie Mcmohn',
            (pic_square) => 'urlof100'
        ),

    (2) => Array
        (
            (uid) => '40489',
            (name) => 'Michael',
            (pic_square) => 'urlof40489'
        )
);

simply u can use this

$key = array_search(40489, array_column($userdb, 'uid'));
nan at designpromote dot co dot uk 17-Jan-2015 01:45
$array = ['a', 'b', 'c'];
$key = array_search('a', $array); //$key = 0
if ($key)
{
//even a element is found in array, but if (0) means false
//...
}

//the correct way
if (false !== $key)
{
//....
}

It's what the document stated "may also return a non-Boolean value which evaluates to FALSE."
podarokua at gmail dot com 17-Oct-2014 01:45
Here is a method for getting all keys, containing searched value, recursively

  /**
   * Recursive array search.
   *
   * See http://php.net/manual/en/function.array-search.php#91365
   *
   * @param $needle
   *   The searched value.
   * @param $haystack
   *   The array.
   *
   * @return bool|int|string
   *   Array of keys, containing values or FALSE if not found.
   */
  private function ras($needle, $haystack) {
    $keys = array();
    foreach ($haystack as $key => $value) {
      if ($needle === $value OR (is_array($value) && $this->ras(
            $needle,
            $value
          ) !== FALSE)
      ) {
        $keys[] = $key;
      }
    }
    if (!empty($keys)) {
      return $keys;
    }

    return FALSE;
  }
marvin dot petker at borntocreate dot de 06-Sep-2014 01:08
Be aware of using array_search() in conditional statements.

$foo = array("one", "two");
if( $key = array_search("one", $foo) ){
  echo "Found";
}
else{
  echo "Not found";
}
//Ouput: Not found

"Not found" will be outputted beacause the returned key is 0 and that will be interpreted as false in conditional statements.
dk at danilok dot com 22-Aug-2014 03:26
I needed a way to return the value of a single specific key, thus:

<?php

function recursive_return_array_value_by_key($needle, $haystack){
    $return = false;
    foreach($haystack as $key => $val){
        if(is_array($val)){
            $return = recursive_return_array_value_by_key($needle, $val);
        }
        else if($needle === $key){
            return "$val\n";
        }
    }
    return $return;
}

?>
rAthur 02-Apr-2014 05:36
If you're not running an older version of PHP (many servers are still on PHP 5.3), you can replace the function by this :

<?php

function s($needle,$array)
{
    foreach($array as $key => $value)
        if ($value==$needle)
        return $key;
    return false;
}

echo s( 'c', array('a', 'b', 'c', 'd') ); // returns 2
echo s( 'f', array('a', 'b', 'c', 'd') ); // returns false

?>
rAthur 02-Apr-2014 05:35
If you're not running an older version of PHP (many servers are still on PHP 5.3), you can replace the function by this :

<?php

function s($needle,$array)
{
    foreach($array as $key => $value)
        if ($value==$needle)
        return $key;
    return false;
}

echo s( 'c', array('a', 'b', 'c', 'd') ); // returns 2
echo s( 'f', array('a', 'b', 'c', 'd') ); // returns false

?>
rAthur 02-Apr-2014 05:35
If you're not running an older version of PHP (many servers are still on PHP 5.3), you can replace the function by this :

<?php

function s($needle,$array)
{
    foreach($array as $key => $value)
        if ($value==$needle)
        return $key;
    return false;
}

echo s( 'c', array('a', 'b', 'c', 'd') ); // returns 2
echo s( 'f', array('a', 'b', 'c', 'd') ); // returns false

?>
sunelbe at gmail dot com 21-Sep-2012 09:19
hey i have a easy multidimensional array search function

<?php
function search($array, $key, $value)
{
    $results = array();

    if (is_array($array))
    {
        if (isset($array[$key]) && $array[$key] == $value)
            $results[] = $array;

        foreach ($array as $subarray)
            $results = array_merge($results, search($subarray, $key, $value));
    }

    return $results;
}
?>
sneskid at hotmail dot com 16-Mar-2012 05:50
I had an array of arrays and needed to find the key of an element by comparing actual reference.
Beware that even with strict equality (===) php will equate arrays via their elements recursively, not by a simple internal pointer check as with class objects. The === can be slow for massive arrays and also crash if they contain circular references.

This function performs reference sniffing in order to return the key for an element that is exactly a reference of needle.

<?php
function array_ref_search(&$v, array &$s)
{
    if(is_object($v)){ return array_search($v, $s, true); }
    foreach($s as $rK => &$rV)
    { // reference sniff
        $tV = $v;
        if( ($rV === ($v = 1)) && ($rV === ($v = 0)) ){
        $v = $tV; return $rK; }
        $v = $tV;
    }
    return false; // use null for php < 4.2.0
}

$list   = array();
$list['A'] = &$valA; $list['B'] = &$valB;

$valA = 1; $valB = 1;
echo 'array_ref_search: ', array_ref_search($valB, $list), '</br>'; // key 'B'
echo 'array_search:     ', array_search($valB, $list, true), '</br>'; // key 'A'

$valA = array(1,2,3); $valB = array(1,2,3);
echo 'array_ref_search: ', array_ref_search($valB, $list), '</br>'; // key 'B'
echo 'array_search:     ', array_search($valB, $list, true), '</br>'; // key 'A' because ($valA === $valB) is true by elements

$valB[] = &$valB; // circular reference
echo 'array_ref_search: ', array_ref_search($valB, $list), '</br>'; // key 'B'
echo 'array_search:     ', array_search($valB, $list, true), '</br>'; // crash because ($valB === $valB) causes infinite loop
?>
revoke 11-Oct-2011 01:12
Better solution of multidimensional searching.

<?php
function multidimensional_search($parents, $searched) {
  if (empty($searched) || empty($parents)) {
    return false;
  }

  foreach ($parents as $key => $value) {
    $exists = true;
    foreach ($searched as $skey => $svalue) {
      $exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] == $svalue);
    }
    if($exists){ return $key; }
  }

  return false;
}

$parents = array();
$parents[] = array('date'=>1320883200, 'uid'=>3);
$parents[] = array('date'=>1320883200, 'uid'=>5);
$parents[] = array('date'=>1318204800, 'uid'=>5);

echo multidimensional_search($parents, array('date'=>1320883200, 'uid'=>5)); // 1
?>
stooshie at gmail dot com 06-Oct-2011 11:51
Example of a recursive binary search that returns the index rather than boolean.
<?php
// returns the index of needle in haystack
function binSearch($needle, $haystack)
{
    // n is only needed if counting depth of search
    global $n;
    $n++;
    // get the length of passed array
    $l = count($haystack);
    // if length is 0, problem
    if($l <= 0)
    {
        return -1;
    }
    // get the mid element
    $m = (($l+($l%2))/2);
    // if mid >= length (e.g. l=1)
    if($m >= $l)
    {
        $m = $m-1;
    }
    // get the indexed element to compare to the passed element and branch accordingly
    $compare = $haystack[$m];
    switch(true)
    {
        case($compare>$needle):
        {
            // recurse on the lower half
            $new_haystack = array_slice($haystack, 0, $m);
            $c = count($new_haystack);
            $r = binSearch($needle, $new_haystack);
            // return current index - (length of lower half - found index in lower half)
            return $m - ($c - $r);
            break;
        }
        case($compare<$needle):
        {
            // recurse on the upper half
            $new_haystack = array_slice($haystack, $m, ($l-$m));
            $c = count($new_haystack);
            $r = binSearch($needle, $new_haystack);
            // return current position + found index in upper half
            return $m + $r;
            break;
        }
        case($compare==$needle):
        {
            // found it, so return index
            return $m;
            break;
        }
    }
}
?>
Dave C 27-Sep-2011 10:18
Here is a version of binary search that is done via recursion instead of iteration.  Remember that your data needs to be presorted!

<?php
static function Bin_Search(&$needle, &$haystack, $start, $end) {
        if($end < $start)
        {
            return false;
        }

        $mid = (int)(($end - $start) / 2) + $start;

        if($haystack[$mid] > $needle)
        {
            return Bin_Search($needle, $haystack, $start, $mid - 1);
        }
        else if($haystack[$mid] < $needle)
        {
            return Bin_Search($needle, $haystack, $mid + 1, $end);
        }
        else
        {
            return true;
        }
}
?>
stefano@takys dot it 14-Jan-2011 10:58
for searching case insensitive better this:

<?php
array_search(strtolower($element),array_map('strtolower',$array));
?>
robert at robert-gonzalez dot com 22-Sep-2010 09:15
I needed a way to find the parent hierarchy of a multidimensional array. Being the rogue that I am, I got to coding before searching the manual and came up with two little functions that will return a parent stack for a first find and a complete parent stack, similar in nature to the solution presented by jette at nerdgirl dot dk without all the extra stuff or use of eval(). ;)

<?php
/**
 * Gets the parent stack of a string array element if it is found within the
 * parent array
 *
 * This will not search objects within an array, though I suspect you could
 * tweak it easily enough to do that
 *
 * @param string $child The string array element to search for
 * @param array $stack The stack to search within for the child
 * @return array An array containing the parent stack for the child if found,
 *               false otherwise
 */
function getParentStack($child, $stack) {
    foreach ($stack as $k => $v) {
        if (is_array($v)) {
            // If the current element of the array is an array, recurse it and capture the return
            $return = getParentStack($child, $v);

            // If the return is an array, stack it and return it
            if (is_array($return)) {
                return array($k => $return);
            }
        } else {
            // Since we are not on an array, compare directly
            if ($v == $child) {
                // And if we match, stack it and return it
                return array($k => $child);
            }
        }
    }

    // Return false since there was nothing found
    return false;
}

/**
 * Gets the complete parent stack of a string array element if it is found
 * within the parent array
 *
 * This will not search objects within an array, though I suspect you could
 * tweak it easily enough to do that
 *
 * @param string $child The string array element to search for
 * @param array $stack The stack to search within for the child
 * @return array An array containing the parent stack for the child if found,
 *               false otherwise
 */
function getParentStackComplete($child, $stack) {
    $return = array();
    foreach ($stack as $k => $v) {
        if (is_array($v)) {
            // If the current element of the array is an array, recurse it
            // and capture the return stack
            $stack = getParentStackComplete($child, $v);

            // If the return stack is an array, add it to the return
            if (is_array($stack) && !empty($stack)) {
                $return[$k] = $stack;
            }
        } else {
            // Since we are not on an array, compare directly
            if ($v == $child) {
                // And if we match, stack it and return it
                $return[$k] = $child;
            }
        }
    }

    // Return the stack
    return empty($return) ? false: $return;
}

// TESTING
$array = array(
    'balloon' => array(
        'red' => array(1 => 'Love', 'Valentine', 'Heart',),
        'green' => array(1 => 'Summertime', 'Hope',),
    ),
    'ribbon' => array(
        'yellow' => array(2 => 'Welcome',),
        'red' => array(3 => 'Love', 'Love',),
    ),
);

$s = getParentStack('Love', $array);
$c = getParentStackComplete('Love', $array);
var_dump($s, $c);
?>

Output:

array
  'balloon' =>
    array
      'red' =>
        array
          1 => string 'Love' (length=4)

array
  'balloon' =>
    array
      'red' =>
        array
          1 => string 'Love' (length=4)
  'ribbon' =>
    array
      'red' =>
        array
          3 => string 'Love' (length=4)
          4 => string 'Love' (length=4)
hanan dot ali dot shaikh at googlemail dot com 17-Sep-2010 10:14
In this code I write a code to find next and previous element of an array using current element of that array. Let suppose if we are in element 9 and have to access its next and previous element then this code be helpful for someone.

<?php
$myArray = array(4,5,7,9,10,11,13,19,25);

$currentElement = 9;
$firstElement = current($myArray);
$lastElement = $myArray[sizeof($myArray)-1];

$currentKey = array_search($currentElement, $myArray);
$currentValue = $myArray[$currentKey];

$previousValue = "";
$nextValue = "";
if($currentElement!=$lastElement){
    $nextKey = $currentKey + 1;
    $nextValue = $myArray[$nextKey];
}

if($currentElement!=$firstElement){
    $previousKey = $currentKey - 1;
    $previousValue = $myArray[$previousKey];
}

echo $previousValue."--".$currentValue."--".$nextValue;

?>

Thanks,
Hanan Ali
codeslinger at compsalot dot com 02-Oct-2009 09:22
one thing to be very aware of is that array_search() will fail if the needle is a string and the array itself contains values that are mixture of numbers and strings.  (or even a string that looks like a number)

The problem is that unless you specify "strict" the match is done using ==    and in that case any string will match a numeric value of zero which is not what you want.

-----

also, php can lookup an index pretty darn fast.  for many scenarios, it is practical to maintain multiple arrays, one in which the index of the array is the search key and the normal array that contains the data.

<?php

  $normal[$index] = array('key'=>$key, 'data'=>'foo');
  $inverse[$key] = $index;

  //very fast lookup, this beats any other kind of search

  if (array_key_exists($key, $inverse))
  {
    $index = $inverse[$key];
    return $normal[$index];
  }

?>
temporal dot pl at gmail dot com 05-Sep-2009 12:57
Sometimes you need to find a given value in a sorted array or - if not found - detect the place where it should be. After that you can for example split the array into two halves, the  greater and the smaller one.

greenmr, dennis.decoene and php at celerondude had all posted very good binary search functions but these functions all return false if the needle was not found in the haystack. I've tweaked greenmr's code a little:

<?php
function Array_BinarySearch( $needle, $haystack, $comparator , &$probe )
{
    $high = Count( $haystack ) -1;
    $low = 0;

    while ( $high >= $low )
    {
        $probe = Floor( ( $high + $low ) / 2 );
        $comparison = $comparator( $haystack[$probe], $needle );
        if ( $comparison < 0 )
        {
            $low = $probe +1;
        }
        elseif ( $comparison > 0 )
        {
            $high = $probe -1;
        }
        else
        {
            return true;
        }
    }
    //The loop ended without a match
    //Compensate for needle greater than highest haystack element
    if($comparator($haystack[count($haystack)-1], $needle) < 0)
    {
        $probe = count($haystack);
    }
    return false;
}
?>

Now, the function returns true if it finds something and false otherwise. If a needle was found, then $probe will contain it's position. Otherwise, $probe will contain position of where the needle would be if it were there :). This is possible because we pass $probe by reference.

Example:

<?php
//ultra-simple comparator :)
function CompareNumbers($obj, $needle)
{
    return $obj - $needle;
}

//use examples
$testArr = array(10, 20, 30, 40, 50);
$res = Array_BinarySearch(30, $testArr, 'CompareNumbers', $probe);
echo (int)$res.' '.$probe.'<br />';
//output is: 1 2 - found at position 2

$res = Array_BinarySearch(45, $testArr, 'CompareNumbers', $probe);
echo (int)$res.' '.$probe.'<br />';
//output is: 0 4 - not found, but it would be at position 4 (between 40 and 45)

$res = Array_BinarySearch(-3, $testArr, 'CompareNumbers', $probe);
echo (int)$res.' '.$probe.'<br />';
//output is: 0 0 - not found, but it would be at position 0 (before 10)

$res = Array_BinarySearch(300, $testArr, 'CompareNumbers', $probe);
echo (int)$res.' '.$probe.'<br />';
//output is: 0 5 - not found, but it would be at position 5 (after 50; note, that count($haystack) == 5)
?>

See original greenmr's note for additional details about usage of this binary search: http://php.net/manual/en/function.array-search.php#89413
Nguyen KimKha 23-Aug-2009 07:18
You can remove some values from array, by using unset() and array_search().

<?php

$friends = array( 'Bob', 'Ann', 'Peter' ); // Two persons named 'Bob'
$find = 'Bob';
$key = array_search( $find, $friends ); // Find key of given value
if ($key != NULL || $key !== FALSE) {
    unset($friends[$key]); // remove key from array
}

// Now, $friends = array( 'Ann', 'Peter');

?>
buddel 08-Jun-2009 05:13
the recursive function by tony have a small bug. it failes when a key is 0

here is the corrected version of this helpful function:

<?php
function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}
?>
tony dot peter at wanadoo dot fr 11-May-2009 09:53
A simple recursive array_search function :

<?php
function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value))) {
            return $current_key;
        }
    }
    return false;
}
?>
n-regen 05-May-2009 07:36
If you only know a part of a value in an array and want to know the complete value, you can use the following function:
<?php
function array_find($needle, $haystack)
{
   foreach ($haystack as $item)
   {
      if (strpos($item, $needle) !== FALSE)
      {
         return $item;
         break;
      }
   }
}
?>
The function returns the complete first value of $haystack that contains $needle.
helenadeus at gmail dot com 25-Jan-2009 10:05
I was trying to use array_search to retrieve all the values that match a given needle, but it turns out only the first match key is returned. I built this little function, which works just like array_search, but returns all the keys that match a given needle instead. The output is an array.

<?php

$haystack = array('a','b','a','b');

$needle = 'a';

print_r(array_search_all($needle, $haystack));

//Output will be
// Array
// (
//         [0]=>1
//         [1]=>3
// )

function array_search_all($needle, $haystack)
{#array_search_match($needle, $haystack) returns all the keys of the values that match $needle in $haystack

    foreach ($haystack as $k=>$v) {

        if($haystack[$k]==$needle){

           $array[] = $k;
        }
    }
    return ($array);

}

?>
azaozz, gmail 21-Dec-2008 02:23
Expanding on the comment by hansen{}cointel.de:

When searching for a string and the array contains 0 (zero), the string is casted to (int) by the type-casting which is always 0 (perhaps the opposite is the proper behaviour, the array value 0 should have been casted to string). That produces unexpected results if strict comparison is not used:

<?php
$a = array(0, "str1", "str2", "str3");
echo "
str1 = ".array_search("str1", $a).",
str2 = ".array_search("str2", $a).",
str3 = ".array_search("str3", $a).",

str1 strict = ".array_search("str1", $a, true).",
str2 strict = ".array_search("str2", $a, true).",
str3 strict = ".array_search("str3", $a, true);
?>

This will return:
str1 = 0, str2 = 0, str3 = 0, str1 strict = 1, str2 strict = 2, str3 strict = 3
dcez at land dot ru 30-Apr-2008 10:15
Simple way to get variable name by using array_search function:

<?php

function varname($var){

    return (isset($var))? array_search($var, $GLOBALS) : false;

}

$boogie = 'tonight';

echo varname($boogie);

?>
andreas dot damm at maxmachine dot de 04-Apr-2008 01:07
Combining syntax of array_search() and functionality of array_keys() to get all key=>value associations of an array with the given search-value:
<?php
function array_search_values( $m_needle, $a_haystack, $b_strict = false){
    return array_intersect_key( $a_haystack, array_flip( array_keys( $a_haystack, $m_needle, $b_strict)));
}
?>

Usage:
<?php
$array1 = array( 'pre'=>'2', 1, 2, 3, '1', '2', '3', 'post'=>2);
print_r( array_search_values( '2', $array1));
print_r( array_search_values( '2', $array1, true));
print_r( array_search_values( 2, $array1, true));
?>

Will return:
array(4) {
    ["pre"] =>
    string(1) "2"
    [1] =>
    int(2)
    [4] =>
    string(1) "2"
    ["post"] =>
    int(2)
}
array(2) {
    ["pre"] =>
    string(1) "2"
    [4] =>
    string(1) "2"
}
array(2) {
    [1] =>
    int(2)
    ["post"] =>
    int(2)
}
kermes [at] thesevens [dot] net 12-Sep-2007 12:09
A variation of previous searches that returns an array of keys that match the given value:

<?php
function array_ksearch($array, $str)
{
    $result = array();
    for($i = 0; $i < count($array); next($array), $i++)
        if(strtolower(current($array)) == strtolower($str))
            array_push($result, key($array);

    return $result;
}
?>

Usage would be as follows:
<?php
$testArray = array('one' => 'test1', 'two' => 'test2', 'three' => 'test1', 'four' => 'test2', 'five' => 'test1');
    print_r(array_ksearch($testArray, 'test1'));
?>
robertark at gmail dot com 02-Sep-2007 08:20
A better array_isearch would be to store all results in an array, then return the KEYS stored in $found, such as:

<?php
function array_isearch($str, $array){
  $found = array();
  foreach ($array as $k => $v)
      if (strtolower($v) == strtolower($str)) $found[] = $k;
  return $found;
}
?>

To use, simply have an array to search from then search it, for example:

<?php

function array_isearch($str, $array) {
  $found = array();
  foreach($array as $k => $v)
    if(strtolower($v) == strtolower($str)) $found[] = $k;
  return $found;
}

$stored = "these are an array";
$stored = explode(" ", $stored);

$compare = array("these", "are", "some", "results", "stored", "in", "an", "array");
foreach($stored as $store) {
  $results = array_isearch($store, $compare);
  foreach($results as $key => $result)
    echo "Key: ".$results[$key]."<br />Found: ".$compare[$result]."<br />";
}

?>

Hope this helps :-)

-Rob
php5 site builder 23-May-2007 07:14
If you encounter a situation where condition test is failing on the result of either array_search or in_array, even when using "===" and "!==", make sure to set $strict = true in your array_search() or in_array() function call.

A situation such as :

$arTemp[0] = 1;
$arTemp[1] = 0;
$arTemp[2] = 3;
$arTemp[3] = 5;
$sTempTest = 'BLAH';

$bResult = in_array($sTempTest,$arTemp);
$bResult2 = array_search($sTempTest,$arTemp);

var_dump($bResult);
var_dump($bResult2);

will result in :

boolean true
int 1

Using :

$bResult = in_array($sTempTest,$arTemp,true);
$bResult2 = array_search($sTempTest,$arTemp,true);

will yield :
boolean false
boolean false

This is necessary in any instance where you have an array value equal to the integer zero.  As soon as you put the zero in quotes or double quotes (a string), the evaluation works with in_array & array_search without the $strict parameter being set.
elvenone at gmail dot com 27-Jan-2007 02:10
<?php
                // Search an array in reverse order.
        function array_reverse_search($value, $array) {
             for($i = sizeof($array)-1; $i>=0; $i--) {
                if ($array[$i] == $value) return $i;
             }
            return -1;
        }
?>
dmitry dot polushkin at gmail dot com 22-Oct-2006 07:03
To get the key of the found search value, use:
<?php
$a = array('a', 'b', 'c');
echo array_search(array_search('c', $a), array_keys($a));
?>
mark dot php at mhudson dot net 12-Sep-2006 02:49
I was going to complain bitterly about array_search() using zero-based indexes, but then I realized I should be using in_array() instead.

// if ( isset( $_GET['table'] ) and array_search( $_GET['table'], $valid_tables) ) {  // BAD: fails on first[0] element
// if ( isset( $_GET['table'] ) and ( FALSE !== array_search( $_GET['table'], $valid_tables) ) ) { OK: but wasteful and convoluted
   if ( isset( $_GET['table'] ) and in_array( $_GET['table'], $valid_tables) ) { // BETTER

The essence is this: if you really want to know the location of an element in an array, then use array_search, else if you only want to know whether that element exists, then use in_array()
RichGC 20-Mar-2006 02:54
To expand on previous comments, here are some examples of
where using array_search within an IF statement can go
wrong when you want to use the array key thats returned.

Take the following two arrays you wish to search:

<?php
$fruit_array = array("apple", "pear", "orange");
$fruit_array = array("a" => "apple", "b" => "pear", "c" => "orange");

if ($i = array_search("apple", $fruit_array))
//PROBLEM: the first array returns a key of 0 and IF treats it as FALSE

if (is_numeric($i = array_search("apple", $fruit_array)))
//PROBLEM: works on numeric keys of the first array but fails on the second

if ($i = is_numeric(array_search("apple", $fruit_array)))
//PROBLEM: using the above in the wrong order causes $i to always equal 1

if ($i = array_search("apple", $fruit_array) !== FALSE)
//PROBLEM: explicit with no extra brackets causes $i to always equal 1

if (($i = array_search("apple", $fruit_array)) !== FALSE)
//YES: works on both arrays returning their keys
?>
congaz at yahoo dot dk 10-Mar-2006 04:38
Search a multi-dimensional array on keys!
-------------------------------------------

I needed to search dynamically in a multi-dimen array on keys. I came up with this little neat function. It is so amazingly simple, that I actually didn't think it would work - but it does...

mixed array_searchMultiOnKeys(array, array);

<?php
function array_searchMultiOnKeys($multiArray, $searchKeysArray) {
    // Iterate through searchKeys, making $multiArray smaller and smaller.
    foreach ($searchKeysArray as $keySearch) {
        $multiArray = $multiArray[$keySearch];
        $result = $multiArray;
    }

    // Check $result.
    if (is_array($multiArray)) {
        // An array was found at the end of the search. Return true.
        $result = true;
    }
    else if ($result == '') {
        // There was nothing found at the end of the search. Return false.
        $result = false;
    }

    return $result;
// End of function,
}

// --- Test array_searchMultiOnKeys ---
$multiArray['webpages']['downloads']['music'] = 1;
$multiArray['webpages']['downloads']['pressmaterial'] = 5;
$multiArray['webpages']['links'] = 7;

array_searchMultiOnKeys($multiArray, array('webpages', 'links')); // returns 7.
array_searchMultiOnKeys($multiArray, array('webpages', 'downloads')); // returns true.
array_searchMultiOnKeys($multiArray, array('webpages', 'downloads', 'software')); // returns false.

?>

$multiArray / $searchKeysArray can be any size.

Happy hacking...
09-Feb-2006 03:26
may be good to take note of PHP's mind-boggling 'fuzzy' (vulgo "magic type-casting") comparison features not only in using the results, but also in the search, too:
<?php
$a=array("a","b",0,"c","d");
echo "a: ".array_search("a",$a);
echo "b: ".array_search("b",$a);
echo "c: ".array_search("c",$a);
echo "d: ".array_search("d",$a);
echo "0: ".array_search("0",$a);
echo "x: ".array_search("x",$a);
echo "1: ".array_search("1",$a);
?>
will result in:
a: 0, b: 1, c: 2, d: 2, 0: 2, x: 2, 1: false

as from "c" on, the first match found in $a is "0", as any string compared to an int is automatically cast to (int)0.
22-Oct-2004 02:01
There is no function to count the occurences of needle in haystack, so I made my own one...

<?php
function array_match($needle, $haystack)
{
    if( !is_array($haystack) ) return false;

    $i = 0;
    while( (in_array( $needle, $haystack )) != FALSE )
    {
        $i++;
        $haystack[array_search($needle, $haystack)] = md5($needle);
        reset($haystack);
    }

    return $i;
}
?>

I know it's a bit crappy, but don't ask me too much, I'm still only 13... ;)
dennis dot decoene at removthis dot moveit dot be 17-Jan-2004 07:41
It has been said before: array_search is VERY slow. Everyone knows binary search is fast by design. Here is an implementation.

<?php
$arr=array(1,3,5,7,9,10,11,13);
$searchfor = 6;
echo binsearch($searchfor, $arr);

/**
 * @return integer
 * @param var $needle
 * @param array $haystack
 * @desc Feed a sorted array to $haystack and a value to search for to $needle.
             It will return false if not found or the index where it was found.
             This function is superfast. Try an array with 50.000 elements and search for something,
             you will be amazed.
*/
function binsearch($needle, $haystack)
{
    $high = count($haystack);
    $low = 0;

    while ($high - $low > 1){
        $probe = ($high + $low) / 2;
        if ($haystack[$probe] < $needle){
            $low = $probe;
        }else{
            $high = $probe;
        }
    }

    if ($high == count($haystack) || $haystack[$high] != $needle) {
        return false;
    }else {
        return $high;
    }
}
?>
php at celerondude dot com 12-Nov-2003 12:07
I think array_search uses serial search because they binary search function i wrote here seems to do a better job for records that are not always at the beginning of the array.

Here it is

<?php
function binarySearch ( $a, $t, $l, $r )
{
    if($t<$a[$l]||$t>$a[$r])return NULL;
    while ( $l < $r )
    {
        $m=intval($l+$r)/2;
        if($a[$m]==$t)return $m;
        elseif($t<$a[$m])$r=$m-1;
        elseif($t>$a[$m])$l = $m + 1;
    }
    if($t==$a[$r])
    return $r;
    return NULL;
}
?>

usage:
binarySearch ( array, target, left range, right range );

if your array is a multidimensional array, simply change the comparison method. :)
cue at openxbox dot com 10-Jun-2003 04:50
If you are using the result of array_search in a condition statement, make sure you use the === operator instead of == to test whether or not it found a match.  Otherwise, searching through an array with numeric indicies will result in index 0 always getting evaluated as false/null.  This nuance cost me a lot of time and sanity, so I hope this helps someone.  In case you don't know what I'm talking about, here's an example:

<?php
$code = array("a", "b", "a", "c", "a", "b", "b"); // infamous abacabb mortal kombat code :-P

// this is WRONG
while (($key = array_search("a", $code)) != NULL)
{
 // infinite loop, regardless of the unset
 unset($code[$key]);
}

// this is _RIGHT_
while (($key = array_search("a", $code)) !== NULL)
{
 // loop will terminate
 unset($code[$key]);
}
?>
richard at richard-sumilang dot com 16-Apr-2003 12:59
<?php
/**
     *    Search an array recursivly
     *
     *    This function will search an array recursivly
     *    till it finds what it is looking for. An array
     *    within an array within an array within array
     *    is all good :-)
     *
     *    @author        Richard Sumilang    <richard@richard-sumilang.com>
     *    @param        string    $needle        What are you searching for?
     *    @param        array    $haystack    What you want to search in
     *    @return        boolean
     *    @access        public
     */
    function array_search_r($needle, $haystack){
        foreach($haystack as $value){
            if(is_array($value))
                $match=array_search_r($needle, $value);
            if($value==$needle)
                $match=1;
            if($match)
                return 1;
        }
        return 0;
    }
?>
swbrown at ucsd dot edu 02-May-2002 10:18
Be absolutely sure to check that your code that uses array_search now checks for 'false' too if you upgrade to PHP 4.2.0!

I was using array_search in my page authentication routines and this change had the fun side-effect of causing my code to always think a user had full permissions!  It was letting anyone click through to our installation of phpMyAdmin.  Not good indeed!


배열의 맨 앞에 있는 원소를 시프트

  • ≪ array_search
  • array_slice ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 맨 앞에 있는 원소를 시프트

array_shift

(PHP 4, PHP 5, PHP 7)

array_shift — 배열의 맨 앞에 있는 원소를 시프트

설명

mixed array_shift ( array &$array )

array_shift()는 array의 첫번째 값을 이동시키고 그 값을 반환하고, array를 한 원소만큼 줄이고 모든 원소를 아래로 이동시킨다. 모든 숫자 배열 키는 리터럴 키가 접근되지 않은 동안 영(0)부터 카운트하기 위해 변경된다.

Note: 이 함수는 사용 후에 입력 배열의 array 포인터를 reset()합니다.

인수

array

입력 배열.

반환값

시프트한 배열을 반환하거나, array가 비어있거나 배열이 아닌 경우 NULL을 반환합니다.

예제

Example #1 array_shift() 예제

<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);
?>

위 예제의 출력:

Array
(
    [0] => banana
    [1] => apple
    [2] => raspberry
)

그리고 orange가 $fruit에 할당될것이다.

참고

  • array_unshift() - 배열의 맨 앞에 하나 이상의 원소를 첨가
  • array_push() - 배열의 끝에 하나 이상의 원소를 넣는다
  • array_pop() - 배열의 마지막 원소 빼내기

User Contributed Notes

mah dot di at live dot com 07-Mar-2016 11:35
This removeAdd function, the first argument shift your array then unshif the second argument to your array. first argument is an array and second argument can be int or str.

<?php
function removeAdd ($arr, $newer){
    $a = array_shift($arr);
    $b = array_unshift($arr, $newer);
    foreach ($arr as $value){
        echo $value."<br />";
    }
}

$a = array(1,2,3,4,5,6);
foreach ($a as $current){
    echo $current."<br />";
}
echo "<hr />";
removeAdd($a, 0);
?>

OUTPUT:
1
2
3
4
5
6
_______

0
2
3
4
5
6
michaeljanikk at gmail dot com 14-May-2014 10:26
To remove an element from the MIDDLE of an array (similar to array_shift, only instead of removing the first element, we want to remove an element in the middle, and shift all keys that follow down one position)

Note that this only works on enumerated arrays.

<?php
$array = array('a', 'b', 'c', 'd', 'e', 'e');
/*
array(6) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "e"
}
*/

$indexToRemove = 2;
unset($array[$indexToRemove]);
$array = array_slice($array, 0);

/*
array(5) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "d"
  [3]=>
  string(1) "e"
  [4]=>
  string(1) "e"
}
*/
?>

I hope this helps someone!
drum_inc at yahoo dot com 24-Aug-2013 02:27
Assignment in line, does not remove the element.

$first = array_shift( $arr = array( 0 => '1st', 2 => '2nd', 3 => '3rd') );
print_r( $first );
print_r( $arr );

Output:
1st
Array
(
    [0] => 1st
    [2] => 2nd
    [3] => 3rd
)
elad dot yosifon at gmail dot com 11-Aug-2013 03:47
Notice:
the complexity of array_pop() is O(1).
the complexity of array_shift() is O(n).
array_shift() requires a re-index process on the array, so it has to run over all the elements and index them.
info at chihoang dot de 20-Apr-2011 06:46
This doesn't work with a 2 dimensional array. With 2 dimensional array I do this:

<?php
$backup = $arr;
$first = array_shift ( $backup );
?>
ar at xonix dot ch 14-Aug-2010 12:54
If you need the first or last entry of an array, then this could help you.

<?php
function array_last_entry($arr){
    if(!is_array($arr))
        return;

    if(empty($arr))
        return;

    return end($arr);
}

function array_first_entry($arr){
    if(!is_array($arr))
        return;

    if(empty($arr))
        return;

    reset($arr);
    return current($arr);
}

$arr = array( '5' => 'five', '3' => 'three', '8' => 'eight',);

echo 'last entry: '.array_last_entry($arr).'<br>';

echo 'first entry: '.array_first_entry($arr).'<br>';

echo 'alternative output:<br>';

echo 'last entry: '.$arr[count($arr)-1];

echo '<br>first entry: '.$arr[0];
?>

The output will look like:
last entry: eight
first entry: five
alternative output:
last entry:
first entry:

As you can see, if you have to handle arrays with non-continuous indexes, these functions may be very helpful.
patrick at pwfisher dot com 22-Aug-2009 05:23
Here's a utility function to parse command line arguments.

<?php
/**
 * CommandLine class
 *
 * @package             Framework
 */
/**
 * Command Line Interface (CLI) utility class.
 *
 * @author              Patrick Fisher <patrick@pwfisher.com>
 * @since               August 21, 2009
 * @package             Framework
 * @subpackage          Env
 */
class CommandLine {

    /**
     * PARSE ARGUMENTS
     *
     * [pfisher ~]$ echo "<?php
     * >     include('CommandLine.php');
     * >     \$args = CommandLine::parseArgs(\$_SERVER['argv']);
     * >     echo "\n", '\$out = '; var_dump(\$args); echo "\n";
     * > ?>" > test.php
     *
     * [pfisher ~]$ php test.php plain-arg --foo --bar=baz --funny="spam=eggs" --alsofunny=spam=eggs \
     * > 'plain arg 2' -abc -k=value "plain arg 3" --s="original" --s='overwrite' --s
     *
     * $out = array(12) {
     *   [0]                => string(9) "plain-arg"
     *   ["foo"]            => bool(true)
     *   ["bar"]            => string(3) "baz"
     *   ["funny"]          => string(9) "spam=eggs"
     *   ["alsofunny"]      => string(9) "spam=eggs"
     *   [1]                => string(11) "plain arg 2"
     *   ["a"]              => bool(true)
     *   ["b"]              => bool(true)
     *   ["c"]              => bool(true)
     *   ["k"]              => string(5) "value"
     *   [2]                => string(11) "plain arg 3"
     *   ["s"]              => string(9) "overwrite"
     * }
     *
     * @author              Patrick Fisher <patrick@pwfisher.com>
     * @since               August 21, 2009
     * @see                 http://www.php.net/manual/en/features.commandline.php
     *                      #81042 function arguments($argv) by technorati at gmail dot com, 12-Feb-2008
     *                      #78651 function getArgs($args) by B Crawford, 22-Oct-2007
     * @usage               $args = CommandLine::parseArgs($_SERVER['argv']);
     */
    public static function parseArgs($argv){

        array_shift($argv);
        $out                            = array();

        foreach ($argv as $arg){

            // --foo --bar=baz
            if (substr($arg,0,2) == '--'){
                $eqPos                  = strpos($arg,'=');

                // --foo
                if ($eqPos === false){
                    $key                = substr($arg,2);
                    $value              = isset($out[$key]) ? $out[$key] : true;
                    $out[$key]          = $value;
                }
                // --bar=baz
                else {
                    $key                = substr($arg,2,$eqPos-2);
                    $value              = substr($arg,$eqPos+1);
                    $out[$key]          = $value;
                }
            }
            // -k=value -abc
            else if (substr($arg,0,1) == '-'){

                // -k=value
                if (substr($arg,2,1) == '='){
                    $key                = substr($arg,1,1);
                    $value              = substr($arg,3);
                    $out[$key]          = $value;
                }
                // -abc
                else {
                    $chars              = str_split(substr($arg,1));
                    foreach ($chars as $char){
                        $key            = $char;
                        $value          = isset($out[$key]) ? $out[$key] : true;
                        $out[$key]      = $value;
                    }
                }
            }
            // plain-arg
            else {
                $value                  = $arg;
                $out[]                  = $value;
            }
        }
        return $out;
    }
}
?>
regs at voidship dot net 03-Nov-2008 09:50
Using array_shift over larger array was fairly slow.  It sped up as the array shrank, most likely as it has to reindex a smaller data set.

For my purpose, I used array_reverse, then array_pop, which doesn't need to reindex the array and will preserve keys if you want it to (didn't matter in my case).

Using direct index references, i.e., array_test[$i], was fast, but direct index referencing + unset for destructive operations was about the same speed as array_reverse and array_pop.  It also requires sequential numeric keys.
chris {at} w3style {dot} co {dot} uk 06-Oct-2008 03:06
As pointed out earlier, in PHP4, array_shift() modifies the input array by-reference, but it doesn't return the first element by reference.  This may seem like very unexpected behaviour.  If you're working with a collection of references (in my case XML Nodes) this should do the trick.

<?php

/**
 * This function exhibits the same behaviour is array_shift(), except
 * it returns a reference to the first element of the array instead of a copy.
 *
 * @param array &$array
 * @return mixed
 */
function &array_shift_reference(&$array)
{
  if (count($array) > 0)
  {
    $key = key($array);
    $first =& $array[$key];
  }
  else
  {
    $first = null;
  }
  array_shift($array);
  return $first;
}

class ArrayShiftReferenceTest extends UnitTestCase
{

  function testFunctionRemovesFirstElementOfNumericallyIndexedArray()
  {
    $input = array('foo', 'bar');
    array_shift_reference($input);
    $this->assertEqual(array('bar'), $input, '%s: The array should be shifted one element left');
  }

  function testFunctionRemovesFirstElementOfAssociativeArray()
  {
    $input = array('x' => 'foo', 'y' => 'bar');
    array_shift_reference($input);
    $this->assertEqual(array('y' => 'bar'), $input, '%s: The array should be shifted one element left');
  }

  function testFunctionReturnsReferenceToFirstElementOfNumericallyIndexedArray()
  {
    $foo = 'foo';
    $input = array(&$foo, 'bar');
    $first =& array_shift_reference($input);
    $this->assertReference($foo, $first, '%s: The return value should reference the first array element');
  }

  function testFunctionReturnsReferenceToFirstElementOfAssociativeArray()
  {
    $foo = 'foo';
    $input = array('x' => &$foo, 'y' => 'bar');
    $first =& array_shift_reference($input);
    $this->assertReference($foo, $first, '%s: The return value should reference the first array element');
  }

  function testFunctionReturnsNullIfEmptyArrayPassedAsInput()
  {
    $input = array();
    $first = array_shift_reference($input);
    $this->assertNull($first, '%s: Array has no first element so NULL should be returned');
  }

}

?>
sggoyal at gmail dot com 08-Sep-2008 07:08
// To Change order of Array by Saurabh Goyal
    function change_array_order($table,$order)
    {
       //init the new table
       $new_table = array();
       foreach($order as $colname)
       {
          $new_table[$colname] = $table[$colname];
       }
       return $new_table;
    }

if array value like:-
$row = array('usr_id'=>'23','usr_name'=>'Saurabh', 'usr_surname'=>'Goyal','usr_firstname'=>'Saurabh');

//you want change order & show only particular field
change_array_order($row,array('usr_name','usr_firstname',
                                            'usr_surname'));

Regard's

Saurabh Goyal
http://sggoyal.blogspot.com
nospam at dyce dot losethisbit dot com 02-Jul-2008 02:15
Just a useful version which returns a simple array with the first key and value. Porbably a better way of doing it, but it works for me ;-)

<?php

function array_kshift(&$arr)
{
  list($k) = array_keys($arr);
  $r  = array($k=>$arr[$k]);
  unset($arr[$k]);
  return $r;
}

// test it on a simple associative array
$arr = array('x'=>'ball','y'=>'hat','z'=>'apple');

print_r($arr);
print_r(array_kshift($arr));
print_r($arr);

?>

Output:

Array
(
    [x] => ball
    [y] => hat
    [z] => apple
)
Array
(
    [x] => ball
)
Array
(
    [y] => hat
    [z] => apple
)
Maikel 19-Feb-2008 06:43
In response to nando_f at nothingsimple dot com

The example is correct,  array_shift do an unset to first element  because the parameter is passed by reference
Ben 03-Oct-2007 06:17
baughmankr at appstate dot edu, I think this is more efficient.

<?php
function array_shorten($arr)
{
  list($k) = array_keys($arr);
  unset($arr[$k]);
  return $arr;
}
?>
baughmankr at appstate dot edu 18-Sep-2007 11:04
I needed to remove the first set of keys and values from an associative array.  Had to write this function:

function shortenArray($_arr)
{
    $i=1;
    $_shorter=array();
    foreach ($_arr as $k => $v)
    {
        if ($i != 1)
        {
            $_shorter[$k] = $v;
        }
        $i++;
    }
    return $_shorter;
}
dmhouse at gmail dot com 08-Aug-2007 10:54
If you want a version of array_shift() that works non-destructively (i.e., an easy function to grab the first element of the array without modifying the array), try reset().
Traps 09-Jul-2007 07:52
For those that may be trying to use array_shift() with an array containing references (e.g. working with linked node trees), beware that array_shift() may not work as you expect: it will return a *copy* of the first element of the array, and not the element itself, so your reference will be lost.

The solution is to reference the first element before removing it with array_shift():

<?php

// using only array_shift:
$a = 1;
$array = array(&$a);
$b =& array_shift($array);
$b = 2;
echo "a = $a, b = $b<br>"; // outputs a = 1, b = 2

// solution: referencing the first element first:
$a = 1;
$array = array(&$a);
$b =& $array[0];
array_shift($array);
$b = 2;
echo "a = $a, b = $b<br>"; // outputs a = 2, b = 2

?>
C_Prevost at myob 01-May-2007 01:52
no, it demonstrates quite well that it removes the first element in the original array, updating the keys, and that it also returns the original first element.
richard at happymango dot me dot uk 12-Apr-2007 03:09
If you want to loop through an array, removing its values one at a time using array_shift() but also want the key as well, try this.

<?php

while($key = key($array))
{
     $value = array_shift($array);
     //code goes here
}

?>

its like foreach but each time the value is removed from the array so it eventually ends up empty

<?php

//example below

$airports = array
(
    "LGW" => "London Gatwick",
    "LHR" => "London Heathrow",
    "STN" => "London Stanstead"
);

echo count($airports)." Airport in the array<br /><br />";

while($key = key($airports))
{
    $value = array_shift($airports);
    echo $key." is ".$value."<br />";
}

echo "<br />".count($airports)." Airport left in the array";

?>

Example Outputs:

3 Airport in the array

LGW is London Gatwick
LHR is London Heathrow
STN is London Stanstead

0 Airport left in the array
alreece45 at yahoo dot com 10-Aug-2006 05:13
I haven't really read into it, but if you're complaining about a change in PHP 5.0.5 that made it so you couldn't do:

<?php

$val = array_shift(preg_split());

?>

or

<?php

$val = array_shit(function_that_returns_array);

?>

Then you're not using this function correctly. This function's argument is supposed to be a pointer to a variable. It then modifies that variable and returns a value. When you specify a function, php CAN NOT modify the return value of that function. It should be common sense but apparently its not.

Also, on a efficiency note, you might want to consider using another function such as reset or perhaps making your own function such as below:

<?php

function first_element($array) {

return reset($array);

}

?>

Unless of course for some reason you need to save the microseconds this takes.

}
bmr at ediweb dot org 31-May-2006 04:27
If the array has non-numerical keys, array_shift extracts the first element, whichever is the key, and recompute the numerical keys, if there are any. Ie :

$array = array("c" => "ccc", 0 => "aaa", "d" => "ddd", 5 => "bbb");
$first = array_shift($array);
echo '$first = ' . $first . ', $array = ' . var_export($array, true);

will display :

$first = ccc, $array = array ( 0 => 'aaa', 'd' => 'ddd', 1 => 'bbb', )

It means that array_shift works with associative arrays too, and leaves the keys unchanged if they are non-numerical.
20-Sep-2005 04:57
<?php

//----------------------------------------------------------
// The combination of array_shift/array_unshift
// greatly simplified a function I created for
// generating relative paths. Before I found them
// the algorithm was really squirrely, with multiple
// if tests, length calculations, nested loops, etc.
// Great functions.
//----------------------------------------------------------

function create_relative_path($inSourcePath, $inRefPath)
{
    // break strings at slashes
    $s_parts            = explode('/', $inSourcePath);
    $r_parts            = explode('/', $inRefPath);

    // delete items up to the first non-equal part
    while ($s_parts[0] === $r_parts[0])
    {
        array_shift($s_parts);
        array_shift($r_parts);
    }

    // add wild card to r_parts for each remaining
    // item of s_parts
    while ($s_parts[0])
    {
        array_unshift($r_parts, '..');
        array_shift($s_parts);
    }

    return implode('/', $r_parts);
}

//----------------------------------------------------------
// Example:
//     Given a source path $sp generates the relative
//     location of $rp. $sp could be assigned using
//     $_SERVER['PHP_SELF'] but it's hardcoded for
//     the example.
//----------------------------------------------------------
$sp = '/WebServer/Documents/MyBigProject/php/project_script.php';
$rp = '/WebServer/Documents/MyLibraries/lib_script.php';

// plugging them into the function
$rel_path = create_relative_path($sp, $rp);

// yeilds
'../../../MyLibraries/lib_script.php'

// and it could be used like
include_once(create_relative_path($_SERVER['PHP_SELF'], $rp));
lukasz dot dywicki DEL at gmail dot com 27-Jul-2005 02:48
Im using this function to browse arrays from database. For example data:
<?php
$data = array(
      array('row 1-cell 1','row 1-cell 2'),
      array('row 2-cell 1','row 2-cell 2'),
      array('row 3-cell 1','row 3-cell 2'),
);

while($row=array_shift($data)) {
      echo $row[0];
}
?>
Output:
row 1-cell 1
row 2-cell 1
row 3-cell 1
arturo {dot} ronchi {at} gmail {dot} com 20-Apr-2005 03:24
Here is a little function if you would like to get the top element and rotate the array afterwards.

function array_rotate(&$arr)
{
  $elm = array_shift($arr);
  array_push($arr, $elm);
  return $elm;
}
09-Feb-2005 01:27
This function will save the key values of an array, and it will work in lower versions of PHP:

<?php

function array_shift2(&$array){
    reset($array);
    $key = key($array);
    $removed = $array[$key];
    unset($array[$key]);
    return $removed;
}

?>
James McGuigan 14-Dec-2004 08:26
while(array_shift()) can be used to process multiple arrays and/or database results in a single loop. The || short circuts and only evaluates the first statement until it runs out of data.

It can help to reduce duplicated code (the rule is code once and once only).

Note that each ($row = ) statement much be encased in ()'s otherwise you will get funny results. If you use two array_shift($array) statements and forget the ()'s, you will repeatedly get the first element of the first array for the for the count of the $array.

<?php

require_once('class.db.php');

$sql = "SELECT title FROM links";
$result = mysql_query($sql, $db->connection);

$defaults = array(
     array('title' => 'None'),
     array('title' => 'Unknown')
);

while ( ($row = mysql_fetch_assoc($result))
     || ($row = array_shift($defaults)))
{
  echo $row['title'] . "<br>";
}

?>

This will print out (depending on database contents):
Title1
Title2
Title3
...
None
Unknown


배열의 일부를 추출

  • ≪ array_shift
  • array_splice ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 일부를 추출

array_slice

(PHP 4, PHP 5, PHP 7)

array_slice — 배열의 일부를 추출

설명

array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys ]] )

array_slice()는 offset 과 length 인수에 따라 array로부터 일련의 배열 원소를 반환한다.

인수

array

입력 배열.

offset

offset이 음수가 아니면, 일련의 값은 array의 그 offset 수부터 시작할것이다. offset이 음수이면, 일련의 값은 array의 끝에서부터 그 수만큼 떨어진 곳에서부터 시작한다.

length

length가 주어지고 양수이면, 일련의 값은 그 수만큼의 배열 원소가 될것이다. length가 주어지고 음수이면, 배열의 끝에서부터 그 수만큼의 배열원소가 될것이다. 이 인수가 생략되면, 일련의 값은 offset에서부터 array 끝까지의 모든 원소를 가지게 된다.

preserve_keys

array_slice()는 기본값으로 배열 인덱스를 초기화하여 다시 정렬합니다. preserve_keys를 TRUE로 설정하여 이 동작을 바꿀 수 있습니다.

반환값

조각을 반환합니다.

변경점

버전 | 설명
5.0.2 | 선택적인 preserve_keys 인수 추가.

예제

Example #1 array_slice() 예제

<?php
$input = array ("a", "b", "c", "d", "e");

$output = array_slice ($input, 2);      // returns "c", "d", and "e"
$output = array_slice ($input, -2, 1);  // returns "d"
$output = array_slice ($input, 0, 3);   // returns "a", "b", and "c"

// 배열 키의 차이에 주의
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>

위 예제의 출력:

Array
(
    [0] => c
    [1] => d
)
Array
(
    [2] => c
    [3] => d
)

참고

  • array_splice() - 배열의 일부를 삭제하고, 그 위치를 다른 내용으로 대체
  • unset() - 주어진 변수를 제거

User Contributed Notes

SomeGuy 24-Jun-2016 06:17
Thank to taylorbarstow here the function with the unset feature.
<?php
function array_slice_assoc(&$array,$keys,$unset = true) {
    $return = array_intersect_key($array, array_flip($keys));
    if ($unset) {
        foreach ($keys as $value) {
            unset($array[$value]);
        }
    }
    return $return;
}
?>
barakpinch at gmail dot com 22-May-2016 03:37
array_slice($array, $start, $length)

about the array_slices's $length argument:
 a positive decimal number is rounded down (e.g. 1.5 -> 1), a negative decimal is rounded up (e.g. -0.5 -> 0)
kansey 12-Aug-2015 06:32
To save the sort order of a numeric index in the array. Version php =>5.5.26
/*
Example
*/

$arr = array( "1" =>2, "2" =>3 , "3" =>5 );

print_r(array_slice($arr,1,null,true));

/*
Result

Array
(
[2] => 3
[3] => 5
)
*/
richardpq at gmail dot com 02-Aug-2015 06:24
@robert_johnson is incorrect, is not that php 5.5 preserver the keys is that the documentation is very clear:

"...will reorder and reset the numeric array indices" so the key here is NUMERIC array indices, therefor:
<?php
$test = array(
    1=>'hello 1',
    2=>'hello 2',
    3=>'hello 3',
    4=>'hello 4',
    5=>'hello 5',
    6=>'hello 6',
    7=>'hello 7',
    8=>'hello 8'
);
print_r( array_slice($test,2,5) );
?>
Will print:
Array
(
    [0] => hello 3
    [1] => hello 4
    [2] => hello 5
    [3] => hello 6
    [4] => hello 7
)
kezzyhko at gmail dot com 27-Jul-2015 03:49
( If $offset is negative and abs($length)>count($length) ) or ( $offset>count($array) ) etc, array_slice returns empty array. For example:

<?php

$array = array(
    'a'=>'1',
    'b'=>'2',
    'c'=>'3',
    'd'=>'4',
    'e'=>'5',
    'f'=>'6',
    'g'=>'7',
    'h'=>'8'
);

print_r( array_slice($test, 2, -10) );
print_r( array_slice($test, 10, 1) );
print_r( array_slice($test, 10, 15) );

//prints
//Array()
//Array()
//Array()

?>
fraterjan at gmail dot com 18-Oct-2014 07:14
To make sure numerical key values for associative arrays are not converted to integers set $preserve_keys = true.

Example:
$slice = array_slice($array, 0, 3, $preserve_keys = true);
robert dot johnson at icap dot com 12-Sep-2014 06:58
array_slice() seems to work on associative arrays and preserves the keys.  I just ran this test on PHP 5.5:

<?php
$test = array(
    'a'=>'hello 1',
    'r'=>'hello 2',
    'h'=>'hello 3',
    'w'=>'hello 4',
    't'=>'hello 5',
    'n'=>'hello 6',
    'k'=>'hello 7',
    'b'=>'hello 8');
print_r( array_slice($test,2,5) );
?>

Output:
Array
(
    [h] => hello 3
    [w] => hello 4
    [t] => hello 5
    [n] => hello 6
    [k] => hello 7
)
info [at] saiptek.com 12-Mar-2014 10:04
Here is analog to this function. Hope that it helps

function filter_array($input_array, $filter_from, $filter_to)
        {
            $output_array = array();

            foreach ($input_array as $key => $value)
            {
                if($key>=$filter_from && $key<=$filter_to)
                {
                    $output_array[] = $value;
                }
                elseif ($key>$filter_to)
                {
                    //if the key is larger than what we want to filter
                    //it is useless to continue looping
                    break;
                }
                else
                {
                    //if the key is smaller than what we expect
                    continue;
                }

            }

            return $output_array;
        }

// you can add more security defenses for the variables - e.g. if it is string or something like that
Ray.Paseur often uses Gmail 06-Jun-2013 11:44
<?php
// CHOP $num ELEMENTS OFF THE FRONT OF AN ARRAY
// RETURN THE CHOP, SHORTENING THE SUBJECT ARRAY
function array_chop(&$arr, $num)
{
    $ret = array_slice($arr, 0, $num);
    $arr = array_slice($arr, $num);
    return $ret;
}
gary at wits dot sg 11-Jun-2011 08:44
I've found this useful.
The following is a function used to shuffle a very specific part of the array, by providing start and end index.

<?php
function array_shuffle_section(&$a, $s, $e) {
    $head = array_slice($a, 0, $s);
    $body = array_slice($a, $s, $e-$s+1);
    $tail = array_slice($a, $e+1);

    shuffle($body);
    $a = array_merge($head,$body,$tail);
}
?>
Nathan - thefiscster510 at gmail dot com 29-Mar-2011 07:38
If you want to remove a specified entry from an array i made this mwethod...

<?php
$array = array("Entry1","entry2","entry3");

$int = 3; //Number of entries in the array
$int2 = 0; //Starter array spot... it will begine its search at 0.
$del_num = 1; //Represents the second entry in the array... which is the one we will happen to remove this time... i.e. 0 = first entry, 1 = second entry, 2 = third...........

$newarray = array(); //Empty array that will be the new array minus the specified entry...
print_r($array) . "<br>";    //print original array contents
print_r($newarray). "<br>"; //print the new empty array

do
{
$user = $array[$int2];
$key = array_search($user, $array);
if ($key == $del_num)
{

}
else
{
$newarray[] = $array[$int2];
}

$int2 = $int2 + 1;
} while ($int2 < $int);

print_r($newarray). "<br>"; //print the new array

?>
delew 11-Feb-2011 10:17
just a little tip.
to preserve keys without providing length: use NULL

array_slice($array, $my_offset, NULL, true);
xananax at yelostudio dot com 03-Feb-2011 04:23
<?php
/**
 * Reorders an array by keys according to a list of values.
 * @param array $array the array to reorder. Passed by reference
 * @param array $list the list to reorder by
 * @param boolean $keepRest if set to FALSE, anything not in the $list array will be removed.
 * @param boolean $prepend if set to TRUE, will prepend the remaining values instead of appending them
 * @author xananax AT yelostudio DOT com
 */
function array_reorder(array &$array,array $list,$keepRest=TRUE,$prepend=FALSE,$preserveKeys=TRUE){
    $temp = array();
    foreach($list as $i){
        if(isset($array[$i])){
            $tempValue = array_slice(
                $array,
                array_search($i,array_keys($array)),
                1,
                $preserveKeys
            );
            $temp[$i] = array_shift($tempValue);
            unset($array[$i]);
        }
    }
    $array = $keepRest ?
        ($prepend?
            $array+$temp
            :$temp+$array
        )
        : $temp;
}

/** exemple ** /
$a = array(
    'a'    =>    'a',
    'b'    =>    'b',
    'c'    =>    'c',
    'd'    =>    'd',
    'e'    =>    'e'
);
$order = array('c','b','a');

array_reorder($a,$order,TRUE);
echo '<pre>';
print_r($a);
echo '</pre>';
/** exemple end **/
?>
joel dot a dot villarreal at gmail dot com 20-Oct-2010 12:41
An implementation of array_slice that do not resets the cursor.

<?php
function get_from_array($arr, $start, $length)
{
    $sliced = array();
    foreach ($arr as $k => $v)
    {
        if ($start <= $k && $k <= $start + $length - 1)
        {
            $sliced[] = $v;
            if (count($sliced) == $length) break;
        }
    }
    return $sliced;
}
?>

Cheers,
Joel A. Villarreal Bertoldi
yuwas at ht dot cx 04-Apr-2010 03:54
By combining this with str_split() and implode(),slices can also be excerpted from strings with the following simple function:
<?php
function str_slice($string,$offset,$length=999,$preserve_keys=False){
  if( $length == 999 ){ $length = strlen($string)-$offset };
  $slice_arr = array_slice(str_split($string),$offset,$length,$preserve_keys);
  return implode($slice_arr);
};
?>

Perhaps there's a better way to preset $length.
jamie at jamiechong dot ca 19-Oct-2009 01:04
A flexible array_split function:

<?php
// split the given array into n number of pieces
function array_split($array, $pieces=2)
{
    if ($pieces < 2)
        return array($array);
    $newCount = ceil(count($array)/$pieces);
    $a = array_slice($array, 0, $newCount);
    $b = array_split(array_slice($array, $newCount), $pieces-1);
    return array_merge(array($a),$b);
}

// Examples:
$a = array(1,2,3,4,5,6,7,8,9,10);
array_split($a, 2);    // array(array(1,2,3,4,5), array(6,7,8,9,10))
array_split($a, 3);    // array(array(1,2,3,4), array(5,6,7), array(8,9,10))
array_split($a, 4);    // array(array(1,2,3), array(4,5,6), array(7,8), array(9,10))

?>
jamon at clearsightdesign dot com 11-Apr-2009 02:46
I was trying to find a good way to find the previous several and next several results from an array created in a MySQL query. I found that most MySQL solutions to this problem were complex. Here is a simple function that returns the previous and next rows from the array.

<?php

/*
** function array_surround by Jamon Holmgren of ClearSight Design
** Version 1.0 - 4/10/2009
** Please direct comments and questions to my first name at symbol clearsightdesign.com
**
** Returns an array with only the $before and $after number of results
** This is set to work best with MySQL data results
** Use this to find the rows immediately before and after a particular row, as many as you want
**
** Example usage:
**   $mysql_ar is an array of results from a MySQL query and the current id is $cur_id
**   We want to get the row before this one and five rows afterward
**
** $near_rows = array_surround($mysql_ar, "id", $cur_id, 1, 5)
**
**   Previous row is now $near_rows[-1]
**   Current row is now $near_rows[0]
**   Next row is $near_rows[1] ... etc
**   If there is no previous row, $near_rows[-1] will not be set...test for it with is_array($near_rows[-1])
**
*/
function array_surround($src_array, $field, $value, $before = 1, $after = 1) {
    if(is_array($src_array)) {
        // reset all the keys to 0 through whatever in case they aren't sequential
        $new_array = array_values($src_array);
        // now loop through and find the key in array that matches the criteria in $field and $value
        foreach($new_array as $k => $s) {
            if($s[$field] == $value) {
                // Found the one we wanted
                $ck = $k; // put the key in the $ck (current key)
                break;
            }
        }
        if(isset($ck)) { // Found it!
            $result_start = $ck - $before; // Set the start key
            $result_length = $before + 1 + $after; // Set the number of keys to return
            if($result_start < 0) { // Oops, start key is before first result
                $result_length = $result_length + $result_start; // Reduce the number of keys to return
                $result_start = 0; // Set the start key to the first result
            }
            $result_temp = array_slice($new_array, $result_start, $result_length); // Slice out the results we want
            // Now we have an array, but we want array[-$before] to array[$after] not 0 to whatever.
            foreach($result_temp as $rk => $rt) { // set all the keys to -$before to +$after
                $result[$result_start - $ck + $rk] = $rt;
            }
            return $result;
        } else { // didn't find it!
            return false;
        }
    } else { // They didn't send an array
        return false;
    }
}

?>

I hope you find this useful! I welcome constructive criticism or comments or of course praise ;) -- just e-mail me.

- Jamon Holmgren
Mr. P 14-Nov-2008 11:11
Note that offset is not the same thing as key. Offset always starts at 0, while keys might be any number.

So this:

<?php print_r(array_slice(array(0 => 0, 5 => 5, 13 => 13),1)); ?>

will result in this:
Array
(
    [0] => 5
    [1] => 13
)
s0i0m at dreamevilconcepts dot com 12-Sep-2008 04:53
Using the varname function referenced from the array_search page, submitted by dcez at land dot ru. I created a multi-dimensional array splice function. It's usage is like so:

$array['admin'] = array('blah1', 'blah2');
$array['voice'] = array('blah3', 'blah4');
array_cut('blah4', $array);

...Would strip blah4 from the array, no matter where the position of it was in the array ^^ Returning this...

Array ( [admin] => Array ( [0] => blah1 [1] => blah2 ) [voice] => Array ( [0] => blah3 ) )

Here is the code...

<?php

  function varname ($var)
  {
    // varname function by dcez at land dot ru
    return (isset($var)) ? array_search($var, $GLOBALS) : false;
  }

  function array_cut($needle, $haystack)
  {
    foreach ($haystack as $k => $v)
    {
      for ($i=0; $i<count($v); $i++)
        if ($v[$i] === $needle)
        {
          return array_splice($GLOBALS[varname($haystack)][$k], $i, 1);
          break; break;
        }
    }

?>

Check out dreamevilconcept's forum for more innovative creations!
ted.devito at 9gmail9 dot 99com 03-May-2008 06:21
based on worldclimb's arem(), here is a recursive array value removal tool that can work with multidimensional arrays.

function remove_from_array($array,$value){
    $clear = true;
    $holding=array();

    foreach($array as $k => $v){
        if (is_array($v)) {
            $holding [$k] = remove_from_array ($v, $value);
            }
        elseif ($value == $v) {
            $clear = false;
            }
        elseif($value != $v){
            $holding[$k]=$v; // removes an item by combing through the array in order and saving the good stuff
        }
    }
    if ($clear) return $holding; // only pass back the holding array if we didn't find the value
}
worldclimb at 99gmail99 dot com 21-Mar-2008 09:51
array_slice can be used to remove elements from an array but it's pretty simple to use a custom function.

One day array_remove() might become part of PHP and will likely be a reserved function name, hence the unobvious choice for this function's names.

<?
function arem($array,$value){
    $holding=array();
    foreach($array as $k => $v){
        if($value!=$v){
            $holding[$k]=$v;
        }
    }
    return $holding;
}

function akrem($array,$key){
    $holding=array();
    foreach($array as $k => $v){
        if($key!=$k){
            $holding[$k]=$v;
        }
    }
    return $holding;
}

$lunch = array('sandwich' => 'cheese', 'cookie'=>'oatmeal','drink' => 'tea','fruit' => 'apple');
echo '<pre>';
print_r($lunch);
$lunch=arem($lunch,'apple');
print_r($lunch);
$lunch=akrem($lunch,'sandwich');
print_r($lunch);
echo '</pre>';
?>

(remove 9's in email)
aexchecker at yahoo dot com 04-Oct-2007 05:39
<?php
/**
 * @desc
 * Combines two arrays by inserting one into the other at a given position then
 * returns the result.
 *
 * @since   2007/10/04
 * @version v0.7 2007/10/04 18:47:52
 * @author  AexChecker <AexChecker@yahoo.com>
 * @param   array $source
 * @param   array $destination
 * @param   int [optional] $offset
 * @param   int [optional] $length
 * @return  array
 */
function array_insert($source, $destination, $offset = NULL, $length = NULL) {
    if (!is_array($source) || empty($source)) {
        if (is_array($destination) && !empty($destination)) {
            return $destination;
        }
        return array();
    }
    if (is_null($offset)) {
        return array_merge($destination, $source);
    }
    $offset = var2int($offset);
    if (is_null($length)) {
        if ($offset === 0) {
            return array_merge($source, array_slice($destination, 1));
        }
        if ($offset === -1) {
            return array_merge(array_slice($destination, 0, -1), $source);
        }
        return array_merge(
            array_slice($destination, 0, $offset),
            $source,
            array_slice($destination, ++$offset)
        );
    }
    if ($offset === 0) {
        return array_merge($source, array_slice($destination, $length));
    }
    $destination_count = count($destination);
    $length = var2int($length);
    if ($offset > 0) {
        if ($destination_count - $offset < 1) {
            return array_merge($destination, $source);
        }
    } else{
        if (($t = $destination_count + $offset) < 1) {
            return array_merge($source, $destination);
        }
        $offset = $t;
    }
    if ($length > 0) {
        $length+= $offset;
    } elseif ($length < 0 && !($length * -1 < $destination_count)) {
        return $source;
    } else {
        $length = $offset;
    }
    return array_merge(
        array_slice($destination, 0, $offset),
        $source,
        array_slice($destination, $length)
    );
}
?>
phpnotasp at gmail dot com 16-Jul-2007 11:42
It should be noted that this function does NOT modify the original array. So if you need to array_pop() or array_shift() without modifying the original array, you can use array_slice().

<?php

$input = array('a', 'b', 'c');
$output = array_slice($input, 1);

print_r($output);
print_r($input);

/*
Array
(
    [0] => b
    [1] => c
)
Array
(
    [0] => a
    [1] => b
    [2] => c
)
*/
?>
cpa at NOSPAM dot conceptivator dot com 07-Jun-2007 02:15
'gportlock at gembiz dot co dot uk' has an error in his limitText function. It simply takes a text string, then cuts off the first X words and returns the rest of the string. I believe the intended use is to return only the first X words and cut off the rest.

The correct version should be (notice the inserted 0 offset):
<?php
function limitText( $text, $wordCount )
{
    $wordArray = explode(" ", $text);
    array_splice($wordArray, 0, $wordCount);
    return implode( " ", $wordArray );
}
?>
gportlock at gembiz dot co dot uk 24-May-2007 04:29
This function returns a text string that is limited by the word count. This funtion is particularly useful for paid advertising where you pay by the word.

function limitText( $text, $wordCount ){

        $wordArray = explode(" ", $text);
        array_splice($wordArray, $wordCount);
        return implode( " ", $wordArray );
}
14-Mar-2007 05:09
I noticed that some other people made supportive functions for maintaining numeric keys for PHP versions less than 5.0.2. So here is my version of it.

<?php

//Slice an array but keep numeric keys
function narray_slice($array, $offset, $length) {

    //Check if this version already supports it
    if (str_replace('.', '', PHP_VERSION) >= 502)
       return array_slice($array, $offset, $length, true);

    foreach ($array as $key => $value) {

        if ($a >= $offset && $a - $offset <= $length)
            $output_array[$key] = $value;
        $a++;

    }

    return $output_array;

}

?>
aflavio at gmail dot com 01-Mar-2007 10:43
/**
    * Remove a value from a array
    * @param string $val
    * @param array $arr
    * @return array $array_remval
    */
    function array_remval($val, &$arr)
    {
          $array_remval = $arr;
          for($x=0;$x<count($array_remval);$x++)
          {
              $i=array_search($val,$array_remval);
              if (is_numeric($i)) {
                  $array_temp  = array_slice($array_remval, 0, $i );
                $array_temp2 = array_slice($array_remval, $i+1, count($array_remval)-1 );
                $array_remval = array_merge($array_temp, $array_temp2);
              }
          }
          return $array_remval;
    }

$stack=Array('apple','banana','pear','apple', 'cherry', 'apple');
array_remval("apple", $stack);

//output: Array('banana','pear', 'cherry')
19-Dec-2006 06:10
The version check on "ps at b1g dot de" function fails on my copy of PHP.  My Version of PHP is "4.3.10-18", and it ends up checking 4310 <=> 502.
Since we are looking for a version over 4.1.0, we cas use version_compare.

<?php
    // PHP >= 5.0.2 is able to do this itself
    if(function_exists('version_compare') and version_compare(PHP_VERSION, '5.0.2') >= 0) {
      return array_slice($array, $offset, $length, true);
    }
?>
ps at b1g dot de 03-Nov-2006 11:44
The following function is the same as array_slice with preserve_keys=true, but it works with PHP versions < 5.0.2.
When PHP >= 5.0.2 is available, the function uses the faster PHP-own array_slice-function with preserve_keys=true, otherwise it uses its own  implementation.

<?php
/**
 * array_slice with preserve_keys for every php version
 *
 * @param array $array Input array
 * @param int $offset Start offset
 * @param int $length Length
 * @return array
 */
function array_slice_preserve_keys($array, $offset, $length = null)
{
    // PHP >= 5.0.2 is able to do this itself
    if((int)str_replace('.', '', phpversion()) >= 502)
        return(array_slice($array, $offset, $length, true));

    // prepare input variables
    $result = array();
    $i = 0;
    if($offset < 0)
        $offset = count($array) + $offset;
    if($length > 0)
        $endOffset = $offset + $length;
    else if($length < 0)
        $endOffset = count($array) + $length;
    else
        $endOffset = count($array);

    // collect elements
    foreach($array as $key=>$value)
    {
        if($i >= $offset && $i < $endOffset)
            $result[$key] = $value;
        $i++;
    }

    // return
    return($result);
}
?>

Good for backwards compatibility I hope somebody might find this useful.
david at bagnara dot org 19-Oct-2006 05:42
I was trying to pass an argument list through the constructors. I tried various things such as func_get_args(). My conclusion is to pass the args to the constructor as an array. Each constructor can remove the fields it wants and pass the array on.

Using the following prototype, each child class can have any number of parameters added to the beginning of the class constructor and the rest passed onto the parent.

If the default value is desired for an argument, just pass NULL.

This could possibly be better done with array_shift or the like.

<?php

class aChild extends aParent
{
    // TODO customise this list for this class
    public
        $a, $b, $c;

    function __construct( $args = array() )
    {
        //set up default values for this class
        // TODO customise this list for this class
        $default = array( "a-def", "b-def", "c-def" ) ;
        // now overwrite the default with non NULL args
        foreach( $args as $key=>$val )
        {
            // more args than needed?
            if( !isset( $default[$key] ) )
            {
                break;
            }
            // this arg not null
            if( isset( $val ) )
            {
                $default[$key] = $val ;
            }
        }
        // set this to the new values
        // TODO customise this list for this class
        list( $this->a, $this->b, $this->c ) = $default ;
        // take off the ones we used
        $args = array_slice( $args, count( $default ) ) ;
        parent::__construct( $args ) ;
    }
}

$x = new aChild( array( "aChild a", NULL, "aChild c", NULL, "aParent second", "aParent third" ) ) ;
?>
06-May-2006 09:21
If you specify the fourth argument (to not reassign the keys), then there appears to be no way to get the function to return all values to the end of the array. Assigning -0 or NULL or just putting two commas in a row won't return any results.
taylorbarstow at the google mail service 07-Apr-2006 11:01
Array slice function that works with associative arrays (keys):

function array_slice_assoc($array,$keys) {
    return array_intersect_key($array,array_flip($keys));
}
andreasblixt (at) msn (dot) com 06-Sep-2005 06:53
<?php
    // Combines two arrays by inserting one into the other at a given position then returns the result
    function array_insert($src, $dest, $pos) {
        if (!is_array($src) || !is_array($dest) || $pos <= 0) return FALSE;
        return array_merge(array_slice($dest, 0, $pos), $src, array_slice($dest, $pos));
    }
?>
ssb45 at cornell dot edu 28-Jul-2005 04:20
In reply to jenny at jennys dot info:

Here is a much easier way to find the $offset of a $key in an $array:

$offset = array_search($key, array_keys($array));
fanfatal at fanfatal dot pl 09-Jul-2005 12:09
Hmm ... i wrote an usefull function whitch is such like strpos but it works on arrays ;]

<?php
/*
 *    Find position of first occurrence of a array
 *
 *    @param array $haystack
 *    @param array $needle
 *    @return int
 *    @author FanFataL
 */
function array_pos($haystack, $needle) {
    $size = count($needle);
    $sizeh = count($haystack);
    if($size > $sizeh) return false;

    $scale = $sizeh - $size + 1;

    for($i = 0; $i < $scale; $i++)
        if($needle === array_slice($haystack, $i, $size))
            return $i;

    return false;
}

// Sample:
$a = array('aa','bb','cc','dd','ee');
$b = array('cc','dd');
$pos = array_pos($a, $b);
?>

Greatings ;-)
...
liz at matrixmailing dot com 06-Jun-2005 11:16
For those with PHP < 5.0.2, and have a number as your array key, to avoid having the key reset with array_slice, add a blank character to the beginning or end of the key.
<?

$array[" ".$key] = $value;

?>
bishop 08-Dec-2004 10:58
Sometimes you need to pick certain non-integer and/or non-sequential keys out of an array. Consider using the array_pick() implementation below to pull specific keys, in a specific order, out of a source array:

<?php

$a = array ('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
$b = array_pick($a, array ('d', 'b'));

// now:
// $a = array ('a' => 1, 'c' => '3');
// $b = array ('d' => 4, 'b' => '2');

function &array_pick(&$array, $keys)
{
    if (! is_array($array)) {
        trigger_error('First parameter must be an array', E_USER_ERROR);
        return false;
    }

    if (! (is_array($keys) || is_scalar($keys))) {
        trigger_error('Second parameter must be an array of keys or a scalar key', E_USER_ERROR);
        return false;
    }

    if (is_array($keys)) {
        // nothing to do
    } else if (is_scalar($keys)) {
        $keys = array ($keys);
    }

    $resultArray = array ();
    foreach ($keys as $key) {
        if (is_scalar($key)) {
            if (array_key_exists($key, $array)) {
                $resultArray[$key] = $array[$key];
                unset($array[$key]);
            }
        } else {
            trigger_error('Supplied key is not scalar', E_USER_ERROR);
            return false;
        }
    }

    return $resultArray;
}

?>
pies at sputnik dot pl 18-Sep-2004 06:29
My shot at Dams's array_slice_key() implementation:

function array_slice_key($array, $offset, $len=-1){

    if (!is_array($array))
        return FALSE;

    $length = $len >= 0? $len: count($array);
    $keys = array_slice(array_keys($array), $offset, $length);
    foreach($keys as $key) {
        $return[$key] = $array[$key];
    }

    return $return;
}
Samuele at norsam dot org 05-Apr-2004 06:44
Note that if $offset+$length>count($array) then resulting array will NOT be filled with empty elements at his end, so it is not sure that it will have exactly $length elements. Example:
<?php
$a=Array(7,32,11,24,65); // count($a) is 5
$b=array_slice($a,2,4);  // 2+4=6, and 6>count($a)
print_r($b);
?>
will return a 3-elements array:
  Array
  (
      [0] => 11
      [1] => 24
      [2] => 65
  )
23-Feb-2004 11:47
Use unset() to delete a associative array.

Ex:
<?php

$item['chaise'] = array ('qty' => 1,
                        'desc' => 'Chaise bercante 10"',
                        'avail' => 10);

$item['divan'] = array ('qty' => 1,
                        'desc' => 'Divan brun laitte"',
                        'avail' => 10);

if (isset($item['chaise'])) {
        ++$item['chaise']['qty'];
        }

unset($item['divan']);

foreach ($item as $s) {
        echo "<br />Commande " . $s['qty'] . " " . $s['desc'];
}

?>
jenny at jennys dot info 22-Feb-2004 07:12
Here's a function which returns the array offset based on the array key.  This is useful if you'd like to use array_slice to get all keys/values after key "foo".

<?
function array_offset($array, $offset_key) {
  $offset = 0;
  foreach($array as $key=>$val) {
    if($key == $offset_key)
      return $offset;
    $offset++;
  }
  return -1;
}

$array = array('foo'=>'foo', 'bar'=>'bar', 'bash'=>'bash', 'quux'=>'quux');
print_r($array);
// Prints the following:
// Array
// (
//     [foo] => foo
//     [bar] => bar
//     [bash] => bash
//     [quux] => quux
// )

$offset = array_offset($array,'bar');
// $offset now contains '1'
$new = array_slice($array,$offset+1);
print_r($new);
// Prints the following:
// Array
// (
//     [bash] => bash
//     [quux] => quux
// )
?>
webmaster_nospam at wavesport dot com 13-Nov-2002 01:48
This function may surprise you if you use arbitrary numeric values for keys, i.e.

<?php
//create an array
$ar = array('a'=>'apple', 'b'=>'banana', '42'=>'pear', 'd'=>'orange');

print_r($ar);
// print_r describes the array as:
// Array
// (
//    [a] => apple
//    [b] => banana
//    [42] => pear
//    [d] => orange
// )

//use array_slice() to extract the first three elements
$new_ar = array_slice($ar, 0, 3);

print_r($new_ar);
// print_r describes the new array as:
// Array
// (
//    [a] => apple
//    [b] => banana
//    [0] => pear
// )
?>

The value 'pear' has had its key reassigned from '42' to '0'.

When $ar is initially created the string '42' is automatically type-converted by array() into an integer.  array_slice() and array_splice() reassociate string keys from the passed array to their values in the returned array but numeric keys are reindexed starting with 0.
t dot oddy at ic dot ac dot uk 25-Apr-2002 03:47
[Editor's Note:
It is easier to do the same thing using array_values()
]
array_slice() can be used to "re-index" an array to start from key 0.  For example, unpack creates an array with keys starting from 1;

<?php
var_dump(unpack("C*","AB"));
?>

produces

<?php
array(2) {
  [1]=>
  int(65)
  [2]=>
  int(66)
}
?>

and

<?php
var_dump(array_slice(unpack("C*","AB"),0));
?>

give you

<?php
array(2) {
  [0]=>
  int(65)
  [1]=>
  int(66)
}
?>
developer at i-space dot org 03-Feb-2002 05:22
remember that array_slice returns an array with the current element. you must use array_slice($array, $index+1) if you want to get the next elements.
richardgere at jippii dot fi 27-Jan-2002 06:14
The same thing, written by a maladroit :)

<?php
function array_slice2( $array, $offset, $length = 0 )
{
  if( $offset < 0 )
    $offset = sizeof( $array ) + $offset;

  $length = ( !$length ? sizeof( $array ) : ( $length < 0 ? sizeof( $array ) - $length : $length + $offset ) );

  for( $i = $offset; $i < $length; $i++ )
    $tmp[] = $array[$i];

  return $tmp;
}
?>


배열의 일부를 삭제하고, 그 위치를 다른 내용으로 대체

  • ≪ array_slice
  • array_sum ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 일부를 삭제하고, 그 위치를 다른 내용으로 대체

array_splice

(PHP 4, PHP 5, PHP 7)

array_splice — 배열의 일부를 삭제하고, 그 위치를 다른 내용으로 대체

설명

array array_splice ( array &$input , int $offset [, int $length [, mixed $replacement ]] )

input 배열에서 offset과 length로 정해진 대로 배열원소를 제거하고, 인수가 주어진다면 replacement 배열의 원소로 그 부분을 대체한다.

input의 숫자 키는 유지되지 않습니다.

Note: replacement가 배열이 아니면, 자료형 변환이 일어납니다. (즉, (array) $parameter) 이로 인해 replacement에 객체를 사용할 때, 결과를 예측할 수 없습니다.

인수

input

입력 배열.

offset

offset이 양수이면 삭제될 부분의 시작은 input 배열의 시작에서부터의 offset에서 시작된다. offset이 음수이면 input 배열의 끝에서부터의 옵셋 수에서 시작된다.

length

length가 생략되면, offset에서 배열의 끝까지의 모든 원소를 삭제된다. length가 주어지고 양수이면, 그 수만큼의 원소가 삭제될것이다. length가 주어지고 음수이면, 삭제되는 부분의 끝은 배열의 끝에서부터 그 수만큼의 원소가 될것이다. 팁: replacement가 주어져있고 offset에서 배열 끝까지의 모든 원소를 삭제하려면 length 인수에 count($input)을 사용하도록 한다.

replacement

replacement 배열이 주어지면, 제거된 원소들은 이 배열의 원소들로 대체된다.

offset과 length가 아무것도 삭제되지 않도록 주어지면, replacement 배열의 원소들을 offset에 의해 설정된 위치에 끼워넣는다. replacement 배열의 키는 유지되지 않습니다.

replacement가 하나의 원소이고, 그 원소가 배열 자체가 아니라면 array()를 넣을 필요는 없다.

반환값

추출된 원소로 구성된 배열을 반환한다.

예제

Example #1 array_splice() 예제

<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
//          "blue", "black", "maroon")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
//          "blue", "purple", "yellow");
?>

Example #2 array_splice() 예제

다음 구분은 $input의 값을 동일한 방법으로 변경합니다:

<?php
array_push($input, $x, $y);
array_splice($input, count($input), 0, array($x, $y));
array_pop($input);
array_splice($input, -1);
array_shift($input);
array_splice($input, 0, 1);
array_unshift($input, $x, $y);
array_splice($input, 0, 0, array($x, $y));
$input[$x] = $y; // for arrays where key equals offset
array_splice($input, $x, 1, $y);
?>

참고

  • array_slice() - 배열의 일부를 추출
  • unset() - 주어진 변수를 제거
  • array_merge() - 하나 이상의 배열을 병합

User Contributed Notes

gilberg_vrn 25-Apr-2016 11:45
array_splice with preserve keys

<?php
function array_splice_preserve_keys(&$array, $from, $length = null) {
    $result = array_slice($array, $from, $length, true);
    $array = array_slice($array, $from + $length, null, true);

    return $result;
}
?>

Example:

<?php
$array = [
    1 => 'a',
    2 => 'b',
    26 => 'z'
];

var_dump(array_splice_preserve_keys($array, 0, 1), $array);
/**
 * array(1) {
 *  [1]=>
 *   string(1) "a"
 *  }
 * array(2) {
 *  [2]=>
 *  string(1) "b"
 *  [26]=>
 *  string(1) "z"
 * }
 */
?>
vitospericolato at gmail dot com 08-Apr-2016 05:54
To remove elements from an array, based on array values:

<?php
$i_to_remove=array();

foreach($array_to_prune as $i=>$value){
  if(cond_to_delete($value)) $i_to_remove[]=$i;
}
foreach($i_to_remove as $j=>$i)
  array_splice($array_to_prune,$i-$j,1);

?>
StanE 22-Oct-2015 05:50
array_splice() does not preserve numeric keys. The function posted by "weikard at gmx dot de" won't do that either because array_merge() does not preserve numeric keys either.

Use following function instead:

<?php
function arrayInsert($array, $position, $insertArray)
{
    $ret = [];

    if ($position == count($array)) {
        $ret = $array + $insertArray;
    }
    else {
        $i = 0;
        foreach ($array as $key => $value) {
            if ($position == $i++) {
                $ret += $insertArray;
            }

            $ret[$key] = $value;
        }
    }

    return $ret;
}
?>

Example:
<?php
$a = [
    295 => "Hello",
    58 => "world",
];

$a = arrayInsert($a, 1, [123 => "little"]);

/*
Output:
Array
(
    [295] => Hello
    [123] => little
    [58] => world
)
*/
?>

It preserves numeric keys. Note that the function does not use a reference to the original array but returns a new array (I see absolutely no reason how the performance would be increased by using a reference when modifying an array through PHP script code).
guillaume dot lacourt at gmail dot com 20-Jun-2015 10:30
Using array_splice when you traverse array with internal pointer's function reset the array, eg:

<?php
end($arrOfData);
$last = key($arrOfData);
reset($arrOfData);
while (($data = current($arrOfData))) {
  if ($last === key($arrOfData)) {
    array_splice($arrOfData, $last, 1);
   // current($arrOfData) => first value of $arrOfData
  }
}
Anonymous 11-Sep-2014 10:07
<?php
function array_slice2( $array, $offset, $length = 0 )
{
  if( $offset < 0 )
    $offset = sizeof( $array ) + $offset;

  $length = ( !$length ? sizeof( $array ) : ( $length < 0 ? sizeof( $array ) - $length : $length + $offset ) );

  for( $i = $offset; $i < $length; $i++ )
    $tmp[] = $array[$i];

  return $tmp;
}
?>
antrik 10-Jul-2014 04:20
Prompted by dire need, and inspired by some of the existing notes, I came up with this:

/* Like array_splice(), but preserves the key(s) of the replacement array. */
function array_splice_assoc(&$input, $offset, $length = 0, $replacement = array()) {
  $tail = array_splice($input, $offset);
  $extracted = array_splice($tail, 0, $length);
  $input += $replacement + $tail;
  return $extracted;
};

Apart from preserving the keys, it behaves just like the regular array_splice() for all cases I could think of.

So for example the regular array_splice()

$input = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' =>6);
print_r(array_splice($input, -4, 3, array('foo1' => 'bar', 'foo2' => 'baz')));
print_r($input);

will give:

Array
(
    [c] => 3
    [d] => 4
    [e] => 5
)
Array
(
    [a] => 1
    [b] => 2
    [0] => bar
    [1] => baz
    [f] => 6
)

But with array_splice_assoc()

$input = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' =>6);
print_r(array_splice_assoc($input, -4, 3, array('foo1' => 'bar', 'foo2' => 'baz')));
print_r($input);

we get:

Array
(
    [c] => 3
    [d] => 4
    [e] => 5
)
Array
(
    [a] => 1
    [b] => 2
    [foo1] => bar
    [foo2] => baz
    [f] => 6
)

A typical use case would be replacing an element identified by a particular key, which we could achieve with:

$input = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' =>6);
array_splice_assoc($input, array_search('d', array_keys($input)), 1, array('foo' => 'bar'));
print_r($input);

giving us:

Array
(
    [a] => 1
    [b] => 2
    [c] => 3
    [foo] => bar
    [e] => 5
    [f] => 6
)
thom 02-Jan-2014 11:31
Maybe it will help someone else: I was trying to strip off the last part of an array using this section, more or less as follows:

<?php array_splice($array, $offset); ?>

Now it could occur in my code that <?php $offset === 0 ?>, in which case the array is returned as-is and not, as you might expect, an empty array because everything is stripped off. Obviously it is not really useful anyway to "strip off everything", but I was reminded of that the hard way and this may spare someone some time, hopefully.
royanee at yahoo dot com 26-Jan-2013 02:03
When trying to splice an associative array into another, array_splice is missing two key ingredients:
  - a string key for identifying the offset
  - the ability to preserve keys in the replacement array

This is primarily useful when you want to replace an item in an array with another item, but want to maintain the ordering of the array without rebuilding the array one entry at a time.

<?php
function array_splice_assoc(&$input, $offset, $length, $replacement) {
        $replacement = (array) $replacement;
        $key_indices = array_flip(array_keys($input));
        if (isset($input[$offset]) && is_string($offset)) {
                $offset = $key_indices[$offset];
        }
        if (isset($input[$length]) && is_string($length)) {
                $length = $key_indices[$length] - $offset;
        }

        $input = array_slice($input, 0, $offset, TRUE)
                + $replacement
                + array_slice($input, $offset + $length, NULL, TRUE);
}

$fruit = array(
        'orange' => 'orange',
        'lemon' => 'yellow',
        'lime' => 'green',
        'grape' => 'purple',
        'cherry' => 'red',
);

// Replace lemon and lime with apple
array_splice_assoc($fruit, 'lemon', 'grape', array('apple' => 'red'));

// Replace cherry with strawberry
array_splice_assoc($fruit, 'cherry', 1, array('strawberry' => 'red'));
?>

Note: I have not tested this with negative offsets and lengths.
mrsohailkhan at gmail dot com 21-Oct-2011 09:34
array_splice, split an array into 2 arrays. The returned arrays is the 2nd argument actually and the used array e.g $input here contains the 1st argument of array, e.g

<?php
$input = array("red", "green", "blue", "yellow");
print_r(array_splice($input, 3)); // Array ( [0] => yellow )
print_r($input); //Array ( [0] => red [1] => green [2] => blue )
?>

if you want to replace any array value do simple like that,

first search the array index you want to replace

<?php $index = array_search('green', $input);// index = 1 ?>

and then use it as according to the definition

<?php
array_splice($input, $index, 1, array('mygreeen')); //Array ( [0] => red [1] => mygreeen [2] => blue [3] => yellow )
?>

so here green is replaced by mygreen.

here 1 in array_splice above represent the number of items to be replaced. so here start at index '1' and replaced only one item which is 'green'
news_yodpeirs at thoftware dot de 20-Jul-2011 05:20
Sometimes you may want to insert one array into another and just work on with the resulting array. array_splice() doesn't support this, as the resulting array isn't the returned value but the first argument, which is changed by reference.

Therefore you may use the following function, which inserts array $ins in array $src at position $pos. $rep can be used if $ins shouldn't be just inserted, but should replace some existing elements (the number of elements to be replaced is given in $rep).

<?php
function array_insert($src,$ins,$pos,$rep=0) {
  array_splice($src,$pos,$rep,$ins);
  return($src);
}
?>
charette dot s at gmail 21-Jul-2010 05:26
If you want to append null values wrap them in an array:

<?php

    $a = array('Hey', 'hey', 'my', 'my');
    array_splice($a, 1, 0, null);
    print_r($a);

?>
Array
(
    [0] => Hey
    [1] => hey
    [2] => my
    [3] => my
)

<?php

    $b = array('Hey', 'hey', 'my', 'my');
    array_splice($b, 1, 0, array(null));
    print_r($b);

?>
Array
(
    [0] => Hey
    [1] =>
    [2] => hey
    [3] => my
    [4] => my
)
news_yodpeirs at thoftware dot de 01-Apr-2010 10:32
Splicing with NULL as replacement may result in unexpected behavior too. Typecasting NULL into an array results in an empty array (as "(array)NULL" equals "array()"). That means, instead of creating an element with value NULL just no new element ist created (just as if there was no replacement specified).

If you want the splicing to create a new element with value NULL you have to use "array(NULL)" instead of NULL.

You should expect this if you read the explanation carefully, but just as objects are considered as a special case for replacement, NULL should be too.

The explanation of replacement better should read: "If replacement is just one element it is not necessary to put array() around it, unless the element is an array itself, an object or NULL."

And the note better should be: "If replacement is not an array, it will be typecast to one (i.e. (array) $parameter). This may result in unexpected behavior when using an object or NULL replacement."

jmtc
daniele centamore 02-Aug-2009 04:48
just useful functions to move an element using array_splice.

<?php

// info at danielecentamore dot com

// $input  (Array) - the array containing the element
// $index (int) - the index of the element you need to move

function moveUp($input,$index) {
      $new_array = $input;

       if((count($new_array)>$index) && ($index>0)){
                 array_splice($new_array, $index-1, 0, $input[$index]);
                 array_splice($new_array, $index+1, 1);
             }

       return $new_array;
}

function moveDown($input,$index) {
       $new_array = $input;

       if(count($new_array)>$index) {
                 array_splice($new_array, $index+2, 0, $input[$index]);
                 array_splice($new_array, $index, 1);
             }

       return $new_array;
 }

$input = array("red", "green", "blue", "yellow");

$newinput = moveUp($input, 2);
// $newinput is array("red", "blue", "green", "yellow")

$input = moveDown($newinput, 1);
// $input is array("red", "green", "blue", "yellow")

?>
strata_ranger at hotmail dot com 01-Jul-2009 08:53
Should you want a similar function for splicing strings together, here is a rough equivalent:

<?php
function str_splice($input, $offset, $length=null, $splice='')
{
  $input = (string)$input;
  $splice = (string)$splice;
  $count = strlen($input);

  // Offset handling (negative values measure from end of string)
  if ($offset<0) $offset = $count + $offset;

  // Length handling (positive values measure from $offset; negative, from end of string; omitted = end of string)
  if (is_null($length)) $length = $count;
  elseif ($length < 0)  $length = $count-$offset+$length;

  // Do the splice
  return substr($input, 0, $offset) . $splice . substr($input, $offset+$length);
}

$string = "The fox jumped over the lazy dog.";

// Outputs "The quick brown fox jumped over the lazy dog."
echo str_splice($string, 4, 0, "quick brown ");

?>

Obviously this is not for cases where all you need to do is a simple search-and-replace.
dead dot screamer at seznam dot cz 12-Feb-2009 02:28
I need <?php array_Splice()?> function, that use array keys instead of order (offset and length) because of associated arrays, and this is result:

<?php
/**
 *    first variation
 *
 *    $input is input array
 *    $start is index of slice begin
 *    $end is index of slice end, if this is null, $replacement will be inserted (in the same way as original array_Slice())
 *indexes of $replacement are preserved in both examples
 */
function array_KSplice1(&$input, $start, $end=null, $replacement=null)
{
    $keys=array_Keys($input);
    $values=array_Values($input);
    if($replacement!==null)
    {
        $replacement=(array)$replacement;
        $rKeys=array_Keys($replacement);
        $rValues=array_Values($replacement);
    }

    $start=array_Search($start,$keys,true);
    if($start===false)
        return false;
    if($end!==null)
    {
        $end=array_Search($end,$keys,true);
        // if $end not found, exit
        if($end===false)
            return false;
        // if $end is before $start, exit
        if($end<$start)
            return false;
        // index to length
        $end-=$start-1;
    }

    // optional arguments
    if($replacement!==null)
    {
        array_Splice($keys,$start,$end,$rKeys);
        array_Splice($values,$start,$end,$rValues);
    }
    else
    {
        array_Splice($keys,$start,$end);
        array_Splice($values,$start,$end);
    }

    $input=array_Combine($keys,$values);

    return $input;
}

/**
 *    second variation
 *
 *    $input is input array
 *    $start is index of slice begin
 *    $length is length of slice, what will be replaced, if is zero, $replacement will be inserted (in the same way as original array_Slice())
 */
function array_KSplice2(&$input, $start, $length=0, $replacement=null)
{
    $keys=array_Keys($input);
    $values=array_Values($input);
    if($replacement!==null)
    {
        $replacement=(array)$replacement;
        $rKeys=array_Keys($replacement);
        $rValues=array_Values($replacement);
    }

    $start=array_Search($start,$keys,true);
    if($start===false)
        return false;

    // optional arguments
    if($replacement!==null)
    {
        array_Splice($keys,$start,$length,$rKeys);
        array_Splice($values,$start,$length,$rValues);
    }
    else
    {
        array_Splice($keys,$start,$length);
        array_Splice($values,$start,$length);
    }

    $input=array_Combine($keys,$values);

    return $input;
}

$array=range(1,10);
var_Dump(array_KSplice1($array,3,3,array(100=>101,102,103,104)));

$array=range(1,10);
var_Dump(array_KSplice2($array,3,3,array(100=>101,102,103,104)));

?>

Both examples output:
array(11) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [100]=>
  int(101)
  [101]=>
  int(102)
  [102]=>
  int(103)
  [103]=>
  int(104)
  [6]=>
  int(7)
  [7]=>
  int(8)
  [8]=>
  int(9)
  [9]=>
  int(10)
}
madmax at max-worlds dot net 24-Sep-2008 03:23
Note:  If replacement is not an array, it will be typecast to one (i.e. (array) $parameter). This may result in unexpected behavior when using an object replacement .

Example :

<?php
class A()
{
    private $a;
    private $b;
    public function __construct()
    {
        $this->a = "foo";
        $this->b = "bar";
    }
}

$array = array();
array_splice($array, 0, 0, new A());
print_r($array);
?>

Outputs :

Array : Array
{
    [0] => foo
    [1] => bar
}

Solution : Enforce the array() on the object.

<?php
array_splice($array, 0, 0, array(new Object());
?>

Source : http://bugs.php.net/bug.php?id=44485
loushou - life dot 42 at gmail dot com 28-Jul-2008 01:39
i miss posted the actual function...
here is the real one lol

<?php

function q_sort(&$Info, $Index, $Left, $Right)
{
  echo "memory usage <b>".memory_get_usage()."</b><br/>\n";
  $L_hold = $Left;
  $R_hold = $Right;
  $Pivot = $Left;
  $PivotValue = $Info[$Left];
  while ($Left < $Right)
  {
    while (($Info[$Right][$Index] >= $PivotValue[$Index]) && ($Left < $Right))
      $Right--;
    if ($Left != $Right)
    {
      $Info[$Left] = $Info[$Right];
      $Left++;
    }
    while (($Info[$Left][$Index] <= $PivotValue[$Index]) && ($Left < $Right))
      $Left++;
    if ($Left != $Right)
    {
      $Info[$Right] = $Info[$Left];
      $Right--;
    }
  }
  $Info[$Left] = $PivotValue;
  $Pivot = $Left;
  $Left = $L_hold;
  $Right = $R_hold;
  if ($Left < $Pivot)
    q_sort($Info, $Index, $Left, $Pivot-1);
  if ($Right > $Pivot)
    q_sort($Info, $Index, $Pivot+1, $Right);
}

?>
Francis 27-Apr-2008 09:27
Do you need to sort a 2D array on just one of its variables while trying to preserve somewhat the original order?

<?php

function sort_2d_array($array, $position, $order = "ASC"){
  if (!is_array($array)) return $array;
  if (count($array) < 2) return $array;
  $new = array($array[0]);
  for ($cnt = 1; $cnt <= count($array) - 1; $cnt++){
    $stop = 0;
    $splice = 0;
    for ($newcnt = 0; $newcnt <= count($new) - 1; $newcnt++){
      if ($stop == 0){
        if ($order == "ASC")
        if ($array[$cnt][$position] < $new[$newcnt][$position]){
          $splice = $newcnt;
          $stop = 1;
        } // splice position for ASC
        if ($order == "DESC")
        if ($array[$cnt][$position] > $new[$newcnt][$position]){
          $splice = $newcnt;
          $stop = 1;
        } // splice position for DESC
      } // stop vying for position
    } // cycle through new array to find position
    if ($stop == 0){
      $new[] = $array[$cnt];
    } else {
      array_splice($new, $splice, 0, array($array[$cnt]));
    } // splice into new array while keeping somewhat the original order
  } // cycle through original array
  return $new;
} // sort_2d_array

?>

Application Example: In-House Search Engine
Here we are trying to find the word apple in the website by sort of the most recent occurances first, but the number of occurances first.

We've already sorted the mysql output by the date desc and have counted the no of occurances and have placed those in an array for the final query.

I've used this function to further sort the occurances but somewhat keep the original mysql sort order.

Key
[0] Record number
     [0] Record ID
     [1] Source Table
     [2] No of Occurances Pinged

  ---------------------------

[0]
     [0] 24530
     [1] Blogs
     [2] 1

[1]
     [0] 24400
     [1] Blogs
     [2] 1

[2]
     [0] 24240
     [1] Blogs
     [2] 4

[3]
     [0] 243422
     [1] Classifieds
     [2] 1

[4]
     [0] 243100
     [1] Classifieds
     [2] 1

After running...

<?php
sort_2d_array($array, 2, "DESC");
?>

We have...

[0]
     [0] 24240
     [1] Blogs
     [2] 4

[1]
     [0] 24530
     [1] Blogs
     [2] 1

[2]
     [0] 24400
     [1] Blogs
     [2] 1

[3]
     [0] 243422
     [1] Classifieds
     [2] 1

[4]
     [0] 243100
     [1] Classifieds
     [2] 1

Might be useful to someone...
pauljamescampbell at gmail dot com 06-Feb-2008 03:46
Here's my own take on an array slice method that preserves keys from an associative array.

<?php
/**
 * Array slice function that preserves associative keys
 *
 * @function associativeArraySlice
 *
 * @param  Array  $array  Array to slice
 * @param  Integer  $start
 * @param  Integer  $end
 *
 * @return  Array
 */
function associativeArraySlice($array, $start, $end) {
    // Method param restrictions
    if($start < 0) $start = 0;
    if($end > count($array)) $end = count($array);

    // Process vars
    $new = Array();
    $i = 0;

    // Loop
    foreach($array as $key => $value) {
        if($i >= $start && $i < $end) {
            $new[$key] = $value;
        }
        $i++;
    }
    return($new);
}
?>
mip at ycn dot com 08-Mar-2007 02:37
Ever wounder what array_splice is doing to your references, then try this little script and see the output.

<?php

$a = "a";
$b = "b";
$c = "c";
$d = "d";
$arr = array();
$arr[] =& $a;
$arr[] =& $b;
$arr[] =& $c;
array_splice($arr,1,0,array($d));
$sec_arr = array();
$sec_arr[] =& $d;
array_splice($arr,1,0,$sec_arr);

$arr[0] = "test"; // should be $a
$arr[3] = "test2"; // should be $b
$arr[1] = "this be d?"; // should be $d
$arr[2] = "or this be d?"; // should be $d
var_dump($arr);
var_dump($a);
var_dump($b);
var_dump($d);
?>

The output will be (PHP 4.3.3):

array(5) {
  [0]=>
  &string(4) "test"
  [1]=>
  &string(10) "this be d?"
  [2]=>
  string(13) "or this be d?"
  [3]=>
  &string(5) "test2"
  [4]=>
  &string(1) "c"
}
string(4) "test"
string(5) "test2"
string(10) "this be d?"

So array_splice is reference safe, but you have to be careful about the generation of the replacement array.

have fun, cheers!
ahigerd at stratitec dot com 24-Jan-2007 03:07
A comment on array_merge mentioned that array_splice is faster than array_merge for inserting values. This may be the case, but if your goal is instead to reindex a numeric array, array_values() is the function of choice. Performing the following functions in a 100,000-iteration loop gave me the following times: ($b is a 3-element array)

array_splice($b, count($b)) => 0.410652
$b = array_splice($b, 0) => 0.272513
array_splice($b, 3) => 0.26529
$b = array_merge($b) => 0.233582
$b = array_values($b) => 0.151298
bdjumakov at gmail dot com 31-Aug-2006 09:11
Someone might find this function usefull. It just takes a given element from the array and moves it before given element into the same array.

<?php
function array_move($which, $where, $array)
{
    $tmp  = array_splice($array, $which, 1);
    array_splice($array, $where, 0, $tmp);
    return $array;
}
?>
Paul 12-Feb-2006 07:06
In PHP 4.3.10, at least, it seems that elements that are inserted as part of the replacement array are inserted BY REFERENCE (that is, as though with the =& rather than = assignment operation). So if your replacement array contains elements that references to variables that you can also access via other variable name, then this will be true of the elements in the final array too.

In particular, this means that it is safe to use array_splice() on arrays of objects, as you won't be creating copies of the objects (as it is so easy to do in PHP 4).
weikard at gmx dot de 15-Sep-2005 02:53
You cannot insert with array_splice an array with your own key. array_splice will always insert it with the key "0".

<?php
// [DATA]
$test_array = array (
  row1 => array (col1 => 'foobar!', col2 => 'foobar!'),
  row2 => array (col1 => 'foobar!', col2 => 'foobar!'),
  row3 => array (col1 => 'foobar!', col2 => 'foobar!')
);

// [ACTION]
array_splice ($test_array, 2, 0, array ('rowX' => array ('colX' => 'foobar2')));
echo '<pre>'; print_r ($test_array); echo '</pre>';
?>

[RESULT]

Array (
    [row1] => Array (
            [col1] => foobar!
            [col2] => foobar!
        )

    [row2] => Array (
            [col1] => foobar!
            [col2] => foobar!
        )

    [0] => Array (
            [colX] => foobar2
        )

    [row3] => Array (
            [col1] => foobar!
            [col2] => foobar!
        )
)

But you can use the following function:

function array_insert (&$array, $position, $insert_array) {
  $first_array = array_splice ($array, 0, $position);
  $array = array_merge ($first_array, $insert_array, $array);
}

<?php
// [ACTION]

array_insert ($test_array, 2, array ('rowX' => array ('colX' => 'foobar2')));
echo '<pre>'; print_r ($test_array); echo '</pre>';
?>

[RESULT]

Array (
    [row1] => Array (
            [col1] => foobar!
            [col2] => foobar!
        )

    [row2] => Array (
            [col1] => foobar!
            [col2] => foobar!
        )

    [rowX] => Array (
            [colX] => foobar2
        )

    [row3] => Array (
            [col1] => foobar!
            [col2] => foobar!
        )
)

[NOTE]

The position "0" will insert the array in the first position (like array_shift). If you try a position higher than the langth of the array, you add it to the array like the function array_push.
csaba at alum dot mit dot edu 13-Aug-2005 11:31
Appending arrays
If you have an array $a2 whose values you would like to append to an array $a1 then four methods you could use are listed below in order of increasing time.  The last two methods took significantly more time than the first two.  The most surprising lesson is that using the & incurs a time hit.

<?php
foreach ($a2 as $elem) $a1[]=$elem;
foreach ($a2 as &$elem) $a1[]=$elem;
array_splice ($a1, count($a1), 0, $a2);
$a1 = array_merge($a1, $a2);
?>

Csaba Gabor from Vienna
randomdestination at gmail dot com 22-Jul-2005 10:52
To split an associative array based on it's keys, use this function:

<?php
function &array_split(&$in) {
    $keys = func_get_args();
    array_shift($keys);

    $out = array();
    foreach($keys as $key) {
        if(isset($in[$key]))
            $out[$key] = $in[$key];
        else
            $out[$key] = null;
        unset($in[$key]);
    }

    return $out;
}
?>

Example:
<?php
$testin = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
$testout =& array_split($testin, 'a', 'b', 'c');

print_r($testin);
print_r($testout);
?>

Will print:

Array
(
    [d] => 4
)
Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)

Hope this helps anyone!
gideon at i6developments dot com 18-Jul-2004 06:45
array_splice dynamically updates the total number of entries into the array. So for instance I had a case where I needed to insert a value into every 4th entry of the array from the back. The problem was when it added the first, because the total number was dynamically updated, it would only add after the 3rd then the 2nd and so one. The solution I found is to track the number of inserts which were done and account for them dynamically.

Code:
<?php
    $modarray = array_reverse($mili);
    $trig=1;
        foreach($modarray as $rubber => $glue) {
            if($rubber!="<BR>") {
            $i++;
            $b++;
            if ($i==4) {
                $trig++;
                if($trig<=2) {
                array_splice($modarray,$b,0,"<BR>");
                }elseif($trig>=3){
                array_splice($modarray,$b+($trig-2),0,"<BR>");
                }
                $i=0;
                };
                };
        };
    $fixarray = array_reverse($modarray);

?>
rolandfoxx at yahoo dot com 30-Mar-2004 09:16
Be careful, array_splice does not behave like you might expect should you try to pass it an object as the replacement argument.  Consider the following:

<?php
//Very truncated
class Tree {
  var $childNodes

  function addChild($offset, $node) {
    array_splice($this->childNodes, $offset, 0, $node);
    //...rest of function
  }

}

class Node {
  var $stuff
  ...
}

$tree = new Tree();
// ...set 2 nodes using other functions...
echo (count($tree->childNodes)); //Gives 2
$newNode = new Node();
// ...set node attributes here...
$tree->addChild(1, $newNode);
echo(count($tree->childNodes)); //Expect 3?  wrong!
?>

In this case, the array has a number of items added to it equal to the number of attributes in the new Node object and the values thereof  I.e, if your Node object has 2 attributes with values "foo" and "bar", count($tree->childNodes) will now return 4, with the items "foo" and "bar" added to it.  I'm not sure if this qualifies as a bug, or is just a byproduct of how PHP handles objects.

Here's a workaround for this problem:
function array_insertobj(&$array, $offset, $insert) {
  $firstPart = array_slice($array, 0, $offset);
  $secondPart = array_slice($array, $offset);
  $insertPart = array($insert);
  $array = array_merge($firstPart, $insertPart, $secondPart);
}

Note that this function makes no allowances for when $offset equals the first or last index in the array.  That's because array_unshift and array_push work just fine in those cases.  It's only array_splice that can trip you up.  Obviously, this is kinda tailor-made for arrays with numeric keys when you don't really care what said keys are, but i'm sure you could adapt it for associative arrays if you needed it.
kbrown at horizon dot sk dot ca 11-Aug-2003 09:40
[ Editor's Note: If you're not concerned with the indexes being contiguously numbered (such as for an associative array) then unset($ar[$ind]); will accomplish the same as the code below without requiring splice/splice/merge.  If contiguous numbering IS a concern (such as for indexed arrays), you can still save time by using: unset($ar[$ind]); $ar = array_values($ar); ]

Removing elements from arrays

This works better - much quicker

<?php
$ar = array("einstein", "bert", "colin", "descartes", "renoir");
$a = array_slice($ar, 0, $ind);
$b = array_slice($ar, $ind + 1);
$ar = array_merge($a, $b);
?>
plintus at smtp dot ru 14-Apr-2003 10:59
key-safe:

<?php
function array_kslice ($array, $offset, $length = 0) {
$k = array_slice (array_keys ($array), $offset, $length);
$v = array_slice (array_values ($array), $offset, $length);
for ($i = 0; $i < count ($k); $i ++) $r[$k[$i]] = $v[$i];
return $r;
}
?>

smth like this. hope you like it more than versions above :)
tsunaquake DOESNTLIKESPAM @ wp DOT pl 11-Nov-2002 07:56
It is possible to use a string instead of offset, eg if you want to deletre the entry $myArray['entry'] then you can simply do it like this:

<?php
array_splice($myArray, 'entry', 1);
?>

Note that you can use unset($myArray['entry']) as well but then, it doesn't enable you to remove more than one entry and it doesn't replace anything in the array, if that's what you intend to do.
19-Jun-2002 04:14
Please note that array_splice() 's second argument is an OFFSET and not an INDEX.

Lets say you want to
$array_of_items = array ('nothing','myitem','hisitem','heritem');
$sid = array_search('myitem',$array_of_items);
echo $sid; /* prints out 1, since index element 1 is "myitem" */

Now, lets say we want to remove that "myitem" from the array:

<?php
$array_of_items = array_splice($array_of_items,(1+$sid),1);
?>

Notice how you have to add a one to the $sid variable?  That is because offset item 1 is "nothing" and since $sid is currently 1 (the index of "myitem"), we add 1 more to it to find out
 its OFFSET.

DO NOT DO THIS:
 $array_of_items = array_splice($array_of_items,$sid,1);
paule at cs dot tamu dot edu 12-Jun-2002 06:59
to kokos@lac.lviv.ua:

Good point about the code not doing what you expected.

The failure to check for the insert case like you pointed out is not a bug, however. I didn't add code to handle that because the key of such an added index is more or less undefined in an unordered associative array. Put another way, if your array is associative and not auto-indexed, you most likely care enough about your keys to want to set them explicitly.
paule at cs dot tamu dot edu 07-Jun-2002 08:09
After reading KoKos' post above, I thought that the code I posted right before his should do what he wanted. However, my original post neglected to note the little "Tip" in the documentation above, about a single element replacement.

If one changes the lines in my code above that says:

<?php
                if(is_array($replacement))
                    foreach($replacement as $r_key=>$r_value)
                        $new_array[$r_key]=$r_value;
?>

to instead say:

<?php
                if(is_string($replacement))
                    $new_array[$key]=$replacement;
                elseif(is_array($replacement))
                    foreach($replacement as $r_key=>$r_value)
                        $new_array[$r_key]=$r_value;
?>

that will solve the problem.

Sorry for the omission.
kokos at lac dot lviv dot ua 07-Jun-2002 05:26
It may seem obvious from the above posts, but cost me a bit of
braindamage to figure this out...

Contrary to the equivalence noted on this page
$input[$x] = $y   <==>   array_splice ($input, $x, 1, $y)
array_splice() will not always work as expected,
even provided that you have only INTEGER keys!

The following code:
   $t=array('a','b','c','d','e');
   var_dump($t);

<?php
   unset($t[0],$t[1],$t[3]);
   $t[0]='f';
   var_dump($t);

   array_splice($t,0,1,'g');
   var_dump($t);
?>

Will produce:
array(5) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
}
array(3) {
  [2]=>
  string(1) "c"
  [4]=>
  string(1) "e"
  [0]=>
  string(1) "f"
}
array(3) {
  [0]=>
  string(1) "g"
  [1]=>
  string(1) "e"
  [2]=>
  string(1) "f"
}

Note the position of $t[0] in the second call to var_dump().
And of course, array_splice() left it intact, changing $t[2] instead.
This is because it operates the _offset_, not the _index_.  :)
I think that "equivalence note" should be considered buggy.  ;)))

Best wishes.
KoKos.
jrhardytwothousandtwo at yahoo dot com 15-Feb-2002 08:54
A reference is made to INSERT'ing into an array here with array_splice, however its not explained very well.  I hope this example will help others find what took me days to research.

<?php
$original_array = array(1,2,3,4,5);
$insert_into_key_position = 3;
$item_to_insert = "blue";

$returned = array_splice($original_array, $insert_into_key_position, 0, $item_to_insert);

// $original_array will now show:

// 1,2,3,blue,4,5
?>

Remember that you are telling the array to insert the element into the KEY position.  Thus the elements start with key 0 and so on 0=>1, 1=>2, 2=>3, 3=>blue, 4=>4, 5=>5.  And walla, you've inserted.  I can't say if this is of any value for named keys, or multidimensional arrays.  However it does work for single dimensional arrays.

$returned should be an empty array as nothing was returned.  This would have substance if you were doing a replace instead.
leingang AT math DOT rutgers DOT edu 16-Jan-2002 04:32
array_splice resets the internal pointer of $input.  In fact, many array functions do this.  Caveat programmor!


배열 값들의 합을 계산

  • ≪ array_splice
  • array_udiff_assoc ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열 값들의 합을 계산

array_sum

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

array_sum — 배열 값들의 합을 계산

설명

number array_sum ( array $array )

array_sum()은 배열 값들의 합을 반환합니다.

인수

array

입력 배열.

반환값

값들의 합을 정수나 소수로 반환합니다.

변경점

버전 | 설명
4.2.1 | PHP 4.2.1 이전 버전에서는 전달되는 배열 자체를 변경해서 문자열을 숫자로 변경했었다. (그 값에 따라 대부분 0으로 변환된다)

예제

Example #1 array_sum() 예제

<?php
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "\n";

$b = array("a" => 1.2, "b" => 2.3,"c" => 3.4);
echo "sum(b) = " . array_sum($b) . "\n";
?>

위 예제의 출력:

sum(a) = 20
sum(b) = 6.9

User Contributed Notes

didatus at dynarize dot de 21-Sep-2014 06:21
If you want to check if there are for example only strings in an array, you can use a combination of array_sum and array_map like this:

<?php

function only_strings_in_array($arr) {
    return array_sum(array_map('is_string', $arr)) == count($arr);
}

$arr1 = array('one', 'two', 'three');
$arr2 = array('foo', 'bar', array());
$arr3 = array('foo', array(), 'bar');
$arr4 = array(array(), 'foo', 'bar');

var_dump(
    only_strings_in_array($arr1),
    only_strings_in_array($arr2),
    only_strings_in_array($arr3),
    only_strings_in_array($arr4)
);
?>

This will give you the following result:
bool(true)
bool(false)
bool(false)
bool(false)
hdeus at yahoo dot com 06-Oct-2008 04:01
Here is how you can multiply two arrays in the form of matrixes using a bit of matrix algebra (M*M).
By calling the function multiplyMatrix, you will be multiplying two sparse matrixes (zeros need not be included in the array for the operation to be performed).

<?php
$M = array(
0=>array(1=>1,4=>1),
1=>array(2=>1,3=>1),
3=>array(1=>1),
4=>array(5=>1),
5=>array(6=>1)
);

$M1 = multiplyMatrix($M, $M); //multiplying $M by itself

echo '<pre>';print_r($M1);echo '</pre>';

function multiplyMatrix($M1, $M2)
    {
#Helena F Deus, Oct 06, 2008
##Multiply two matrixes; $M1 and $M2 can be sparse matrixes, the indexes on both should match
        if(is_file($M1)) {$matrix1 = unserialize(file_get_contents($M1));}
        else $matrix1 = $M1;

        #transpose M2
        $M2t = transpose($M2);

        foreach ($M2t as $row=>$tmp) {
            ##sum the result of the value in the col multiplied by the value in the vector on the corresponding row

                foreach ($M1 as $row1=>$tmp1) {

                    $multiply[$row1] = array_rproduct($tmp,$tmp1);

                    if(!$multiply[$row1]){
                          exit;
                        }
                }

                foreach ($multiply as $row1=>$vals) {

                    $sum[$row][$row1]=array_sum($vals);
                }

        }

    $r=transpose($sum);

    return ($r);
    }

function transpose($M)
{
foreach ($M as $row=>$cols) {

            foreach ($cols as $col=>$value) {
                 if($value)
                 $Mt[$col][$row]=$value;
            }
        }
        ksort($Mt);

return ($Mt);
}

function array_rproduct($a1, $a2)
{

    foreach ($a1 as $line=>$cols) {
        $a3[$line] = $a1[$line]*$a2[$line];
        foreach ($a2 as $line2=>$cols2) {
            $a3[$line2] = $a1[$line2]*$a2[$line2];
        }
    }
    ksort($a3);

    return ($a3);

}

?>
herenvardo at gmail dot com 25-Nov-2006 12:28
I'm not sure if something similar already exists, but I needed it so I made it:
<?php
  /* Performs a pitagoric sum of the elements in $arr
   The pitagoric sum of a set of values is the square root of
   the sum of the sqare power of each value. So, for a, b, c
   it's sqrt(a^2 + b^2 + c^2) */
  /* If any element of $arr is an array itself, the array_sum
   will be used. Alternatively, the values could be used by
   recursion. Returns the integer part (floor) */
  function array_pitag_sum($arr) {
    if(is_array($arr) {
      $ret = 0;
      foreach($arr as $i) {
        if(is_array($i)) {
          $s = array_sum($i);
          $ret += $s*$s;
        } else {
          $ret += $i*$i;
        }
      }
      return floor(sqrt($ret));
    } else {
      return $arr;
    }
  }
?>
punchto at hotmail dot com 16-Mar-2005 04:06
Microsoft Excel - SUMIF()

function sumif($array,$criteria,$sum_array){
  if(is_array($array) && is_array($sum_array) && trim($criteria)!= ""){
    $array_count = (count($array) < count($sum_array)) ? count($array):count($sum_array);
    for($i=0;$i<$array_count;$i++){
      if(ereg("^<",$criteria)){
        $value = ereg_replace("^<","",$criteria);
        $result += $array[$i] < $value ? $sum_array[$i]:0;
      }
      elseif(ereg("^>",$criteria)){
        $value = ereg_replace("^>","",$criteria);
        $result += $array[$i] > $value ? $sum_array[$i]:0;
      }
      else{
        $value = $criteria;
        $result += $array[$i] == $value ? $sum_array[$i]:0;
      }

    }
    return $result ? $result:0;
  }
}
ncheung at maine dot rr dot com 08-Feb-2005 03:02
For clarity, array indices containing boolean values such as TRUE and FALSE are added up as though they are 1 and 0 respectively.
mucello NOO SPAM @ weatherimages dOt org 23-Oct-2003 12:44
If you want to find the AVERAGE of the values in your array, use the sum and count functions together.  For example, let's say your array is $foo and you want the average...

<?php
$average_of_foo = array_sum($foo) / count($foo);
?>
drverieevil at REMOVEMEasenne dot org 30-Jul-2003 12:14
If some array elements arent integers, function will change them to integers (content of array will not change) type and then sum them.

Example:
<?php
$foo[] = "12";
$foo[] = 10;
$foo[] = "bar";
$foo[] = "summer";
echo array_sum ($foo); //same as echo "22";
?>


Computes the difference of arrays with additional index check, compares data by a callback function

  • ≪ array_sum
  • array_udiff_uassoc ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the difference of arrays with additional index check, compares data by a callback function

array_udiff_assoc

(PHP 5, PHP 7)

array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function

설명

array array_udiff_assoc ( array $array1 , array $array2 [, array $... ], callable $value_compare_func )

Computes the difference of arrays with additional index check, compares data by a callback function.

Note: Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_udiff_assoc($array1[0], $array2[0], "some_comparison_func");.

인수

array1

The first array.

array2

The second array.

value_compare_func

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )

반환값

array_udiff_assoc() returns an array containing all the values from array1 that are not present in any of the other arguments. Note that the keys are used in the comparison unlike array_diff() and array_udiff(). The comparison of arrays' data is performed by using an user-supplied callback. In this aspect the behaviour is opposite to the behaviour of array_diff_assoc() which uses internal function for comparison.

예제

Example #1 array_udiff_assoc() example

<?php
class cr {
    private $priv_member;
    function cr($val)
    {
        $this->priv_member = $val;
    }

    static function comp_func_cr($a, $b)
    {
        if ($a->priv_member === $b->priv_member) return 0;
        return ($a->priv_member > $b->priv_member)? 1:-1;
    }
}

$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);

$result = array_udiff_assoc($a, $b, array("cr", "comp_func_cr"));
print_r($result);
?>

위 예제의 출력:

Array
(
    [0.1] => cr Object
        (
            [priv_member:private] => 9
        )

    [0.5] => cr Object
        (
            [priv_member:private] => 12
        )

    [0] => cr Object
        (
            [priv_member:private] => 23
        )
)

In our example above you see the "1" => new cr(4) pair is present in both arrays and thus it is not in the output from the function.

참고

  • array_diff() - 배열 차이를 계산
  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산
  • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_udiff() - 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
  • array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_uintersect() - Computes the intersection of arrays, compares data by a callback function
  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions

User Contributed Notes

tmquang6805 at yahoo dot com 25-May-2014 06:54
Difference between array_udiff_assoc() and array_diff_assoc()

- array_udiff_assoc(): Compares the DATA with the user supplied callback function
- array_diff_assoc(): Compares the INDEX with the user supplied callback function
countzero1981 at gmail dot com 03-Sep-2013 08:32
What is the difference between array_udiff_assoc and array_diff_uassoc?


Computes the difference of arrays with additional index check, compares data and indexes by a callback function

  • ≪ array_udiff_assoc
  • array_udiff ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the difference of arrays with additional index check, compares data and indexes by a callback function

array_udiff_uassoc

(PHP 5, PHP 7)

array_udiff_uassoc — Computes the difference of arrays with additional index check, compares data and indexes by a callback function

설명

array array_udiff_uassoc ( array $array1 , array $array2 [, array $... ], callable $value_compare_func , callable $key_compare_func )

Computes the difference of arrays with additional index check, compares data and indexes by a callback function.

Note that the keys are used in the comparison unlike array_diff() and array_udiff().

인수

array1

The first array.

array2

The second array.

value_compare_func

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
key_compare_func

The comparison of keys (indices) is done also by the callback function key_compare_func. This behaviour is unlike what array_udiff_assoc() does, since the latter compares the indices by using an internal function.

반환값

Returns an array containing all the values from array1 that are not present in any of the other arguments.

예제

Example #1 array_udiff_uassoc() example

<?php
class cr {
    private $priv_member;
    function cr($val)
    {
        $this->priv_member = $val;
    }

    static function comp_func_cr($a, $b)
    {
        if ($a->priv_member === $b->priv_member) return 0;
        return ($a->priv_member > $b->priv_member)? 1:-1;
    }

    static function comp_func_key($a, $b)
    {
        if ($a === $b) return 0;
        return ($a > $b)? 1:-1;
    }
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);

$result = array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key"));
print_r($result);
?>

위 예제의 출력:

Array
(
    [0.1] => cr Object
        (
            [priv_member:private] => 9
        )

    [0.5] => cr Object
        (
            [priv_member:private] => 12
        )

    [0] => cr Object
        (
            [priv_member:private] => 23
        )
)

In our example above you see the "1" => new cr(4) pair is present in both arrays and thus it is not in the output from the function. Keep in mind that you have to supply 2 callback functions.

주의

Note: Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_udiff_uassoc($array1[0], $array2[0], "data_compare_func", "key_compare_func");.

참고

  • array_diff() - 배열 차이를 계산
  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산
  • array_udiff() - 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
  • array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_uintersect() - Computes the intersection of arrays, compares data by a callback function
  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions

User Contributed Notes

There are no user contributed notes for this page.


데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산

  • ≪ array_udiff_uassoc
  • array_uintersect_assoc ≫
  • PHP Manual
  • 배열 함수 목록
  • 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산

array_udiff

(PHP 5, PHP 7)

array_udiff — 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산

설명

array array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func )

데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산합니다. 데티터 비교에 내부함수를 사용하는 array_diff() 와는 다릅니다.

인수

array1

첫번째 배열.

array2

두번째 배열.

value_compare_func

비교 콜백 함수.

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )

반환값

다른 배열 인자에 존재하지 않는 array1의 모든값을 리턴합니다.

예제

Example #1 stdClass 객체를 이용한 array_udiff() 예제

<?php
// 비교할 배열들
$array1 = array(new stdclass, new stdclass,
                new stdclass, new stdclass,
               );

$array2 = array(
                new stdclass, new stdclass,
               );

// 각각 오브젝트의 프로퍼티 마다 값 세팅.
$array1[0]->width = 11; $array1[0]->height = 3;
$array1[1]->width = 7;  $array1[1]->height = 1;
$array1[2]->width = 2;  $array1[2]->height = 9;
$array1[3]->width = 5;  $array1[3]->height = 7;

$array2[0]->width = 7;  $array2[0]->height = 5;
$array2[1]->width = 9;  $array2[1]->height = 2;

function compare_by_area($a, $b) {
    $areaA = $a->width * $a->height;
    $areaB = $b->width * $b->height;

    if ($areaA < $areaB) {
        return -1;
    } elseif ($areaA > $areaB) {
        return 1;
    } else {
        return 0;
    }
}

print_r(array_udiff($array1, $array2, 'compare_by_area'));
?>

위 예제의 출력:

Array
(
    [0] => stdClass Object
        (
            [width] => 11
            [height] => 3
        )

    [1] => stdClass Object
        (
            [width] => 7
            [height] => 1
        )

)

Example #2 DateTime 객체를 이용한 array_udiff() 예제

<?php
class MyCalendar {
    public $free = array();
    public $booked = array();

    public function __construct($week = 'now') {
        $start = new DateTime($week);
        $start->modify('Monday this week midnight');
        $end = clone $start;
        $end->modify('Friday this week midnight');
        $interval = new DateInterval('P1D');
        foreach (new DatePeriod($start, $interval, $end) as $freeTime) {
            $this->free[] = $freeTime;
        }
    }

    public function bookAppointment(DateTime $date, $note) {
        $this->booked[] = array('date' => $date->modify('midnight'), 'note' => $note);
    }

    public function checkAvailability() {
        return array_udiff($this->free, $this->booked, array($this, 'customCompare'));
    }

    public function customCompare($free, $booked) {
        if (is_array($free)) $a = $free['date'];
        else $a = $free;
        if (is_array($booked)) $b = $booked['date'];
        else $b = $booked;
        if ($a == $b) {
            return 0;
        } elseif ($a > $b) {
            return 1;
        } else {
            return -1;
        }
    }
}

// 주간 스케쥴 달력 생성
$myCalendar = new MyCalendar;

// 이주의 스케쥴 예약
$myCalendar->bookAppointment(new DateTime('Monday this week'), "Cleaning GoogleGuy's apartment.");
$myCalendar->bookAppointment(new DateTime('Wednesday this week'), "Going on a snowboarding trip.");
$myCalendar->bookAppointment(new DateTime('Friday this week'), "Fixing buggy code.");

// 예약일 대비 가용일 체크
echo "I'm available on the following days this week...\n\n";
foreach ($myCalendar->checkAvailability() as $free) {
    echo $free->format('l'), "\n";
}
echo "\n\n";
echo "I'm busy on the following days this week...\n\n";
foreach ($myCalendar->booked as $booked) {
    echo $booked['date']->format('l'), ": ", $booked['note'], "\n";
}
?>

위 예제의 출력:

I'm available on the following days this week...

Tuesday
Thursday

I'm busy on the following days this week...

Monday: Cleaning GoogleGuy's apartment.
Wednesday: Going on a snowboarding trip.
Friday: Fixing buggy code.

주의

Note: 이 함수는 n차원 배열의 1차 배열만 체크하므로 주의하기 바랍니다. 물론 깊은 차원의 체크는 array_udiff($array1[0], $array2[0], "data_compare_func"); 로 가능합니다.

참고

  • array_diff() - 배열 차이를 계산
  • array_diff_assoc() - 추가적인 인덱스 확인과 함께 배열 차이를 계산
  • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_uintersect() - Computes the intersection of arrays, compares data by a callback function
  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions

User Contributed Notes

napcoder 27-Jul-2016 05:38
Note that the compare function is used also internally, to order the arrays and choose which element compare against in the next round.

If your compare function is not really comparing (ie. returns 0 if elements are equals, 1 otherwise), you will receive an unexpected result.
b4301775 at klzlk dot com 15-Jun-2011 08:09
Quick example for using array_udiff to do a multi-dimensional diff

Returns values of $arr1 that are not in $arr2

<?php
$arr1 = array( array('Bob', 42), array('Phil', 37), array('Frank', 39) );

$arr2 = array( array('Phil', 37), array('Mark', 45) );

$arr3 = array_udiff($arr1, $arr2, create_function(
    '$a,$b',
    'return strcmp( implode("", $a), implode("", $b) ); ')
    );

print_r($arr3);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => Bob
            [1] => 42
        )

    [2] => Array
        (
            [0] => Frank
            [1] => 39
        )

)
1

Hope this helps someone
jared 07-Jul-2009 09:27
Note that php does the string conversion *before* sending the values to the callback function.
adam dot jorgensen dot za at gmail dot com 08-Oct-2008 03:51
It is not stated, by this function also diffs array1 to itself, removing any duplicate values...
grantwparks at gmail dot com 31-Dec-2007 04:15
Re: "convoluted"

I think the point being made is that array_udiff() can be used not only for comparisons between homogenous arrays, as in your example (and definitely the most common need), but it can be used to compare heterogeneous arrays, too.

Consider:

<?php
function compr_1($a, $b) {
    $aVal = is_array($a) ? $a['last_name'] : $a;
    $bVal = is_array($b) ? $b['last_name'] : $b;
    return strcasecmp($aVal, $bVal);
}

$aEmployees = array(
    array('last_name'  => 'Smith',
            'first_name' => 'Joe',
            'phone'      => '555-1000'),
    array('last_name'  => 'Doe',
            'first_name' => 'John',
            'phone'      => '555-2000'),
    array('last_name'  => 'Flagg',
            'first_name' => 'Randall',
            'phone'      => '666-1000')
    );

$aNames = array('Doe', 'Smith', 'Johnson');

$result = array_udiff($aEmployees, $aNames, "compr_1");

print_r($result);
?>

Allowing me to get the "employee" that's not in the name list:

Array ( [2] => Array ( [last_name] => Flagg [first_name] => Randall [phone] => 666-1000 ) )

Something interesting to note, is that the two arguments to the compare function don't correspond to array1 and array2.  That's why there has to be logic in it to handle that either of the arguments might be pointing to the more complex employee array.  (Found this out the hard way.)
Colin 02-Aug-2006 10:15
I think the example given here using classes is convoluting things too much to demonstrate what this function does.

array_udiff() will walk through array_values($a) and array_values($b) and compare each value by using the passed in callback function.

To put it another way, array_udiff() compares $a[0] to $b[0], $b[1], $b[2], and $b[3] using the provided callback function.  If the callback returns zero for any of the comparisons then $a[0] will not be in the returned array from array_udiff().  It then compares $a[1] to $b[0], $b[1], $b[2], and $b[3].  Then, finally, $a[2] to $b[0], $b[1], $b[2], and $b[3].

For example, compare_ids($a[0], $b[0]) === -5 while compare_ids($a[1], $b[1]) === 0.  Therefore, $a[1] is not returned from array_udiff() since it is present in $b.

<?
$a = array(
        array(
                'id' => 10,
                'name' => 'John',
                'color' => 'red',
        ),
        array(
                'id' => 20,
                'name' => 'Elise',
                'color' => 'blue',
        ),
        array(
                'id' => 30,
                'name' => 'Mark',
                'color' => 'red',
        ),
);

$b = array(
        array(
                'id' => 15,
                'name' => 'Nancy',
                'color' => 'black',
        ),
        array(
                'id' => 20,
                'name' => 'Elise',
                'color' => 'blue',
        ),
        array(
                'id' => 30,
                'name' => 'Mark',
                'color' => 'red',
        ),
        array(
                'id' => 40,
                'name' => 'John',
                'color' => 'orange',
        ),
);

function compare_ids($a, $b)
{
    return ($a['id'] - $b['id']);
}
function compare_names($a, $b)
{
    return strcmp($a['name'], $b['name']);
}

$ret = array_udiff($a, $b, 'compare_ids');
var_dump($ret);

$ret = array_udiff($b, $a, 'compare_ids');
var_dump($ret);

$ret = array_udiff($a, $b, 'compare_names');
var_dump($ret);
?>

Which returns the following.

In the first return we see that $b has no entry in it with an id of 10.
<?
array(1) {
  [0]=>
  array(3) {
    ["id"]=>
    int(10)
    ["name"]=>
    string(4) "John"
    ["color"]=>
    string(3) "red"
  }
}
?>

In the second return we see that $a has no entry in it with an id of 15 or 40.
<?
array(2) {
  [0]=>
  array(3) {
    ["id"]=>
    int(15)
    ["name"]=>
    string(5) "Nancy"
    ["color"]=>
    string(5) "black"
  }
  [3]=>
  array(3) {
    ["id"]=>
    int(40)
    ["name"]=>
    string(4) "John"
    ["color"]=>
    string(6) "orange"
  }
}
?>

In third return we see that all names in $a are in $b (even though the entry in $b whose name is 'John' is different, the anonymous function is only comparing names).
<?
array(0) {
}
?>
dmhouse at gmail dot com 20-Jan-2005 10:44
Very easy way of achieving a case-insensitive version of array_diff (or indeed array_diff_assoc, array_intersect or any of these types of functions which have a similar function that takes a callback function as one of their parameters):

array_udiff($array1, $array2, 'strcasecmp');

This works because strcasecmp() compares two strings case-insensitively, as compared to the array_diff() which compares two strings by using the == operator, which is case-sensitive.
aidan at php dot net 28-May-2004 04:11
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat


Computes the intersection of arrays with additional index check, compares data by a callback function

  • ≪ array_udiff
  • array_uintersect_uassoc ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the intersection of arrays with additional index check, compares data by a callback function

array_uintersect_assoc

(PHP 5, PHP 7)

array_uintersect_assoc — Computes the intersection of arrays with additional index check, compares data by a callback function

설명

array array_uintersect_assoc ( array $array1 , array $array2 [, array $... ], callable $value_compare_func )

Computes the intersection of arrays with additional index check, compares data by a callback function.

Note that the keys are used in the comparison unlike in array_uintersect(). The data is compared by using a callback function.

인수

array1

The first array.

array2

The second array.

value_compare_func

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )

반환값

Returns an array containing all the values of array1 that are present in all the arguments.

예제

Example #1 array_uintersect_assoc() example

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");

print_r(array_uintersect_assoc($array1, $array2, "strcasecmp"));
?>

위 예제의 출력:

Array
(
    [a] => green
)

참고

  • array_uintersect() - Computes the intersection of arrays, compares data by a callback function
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions

User Contributed Notes

There are no user contributed notes for this page.


Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions

  • ≪ array_uintersect_assoc
  • array_uintersect ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions

array_uintersect_uassoc

(PHP 5, PHP 7)

array_uintersect_uassoc — Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions

설명

array array_uintersect_uassoc ( array $array1 , array $array2 [, array $... ], callable $value_compare_func , callable $key_compare_func )

Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions.

인수

array1

The first array.

array2

The second array.

value_compare_func

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
key_compare_func

Key comparison callback function.

반환값

Returns an array containing all the values of array1 that are present in all the arguments.

예제

Example #1 array_uintersect_uassoc() example

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");

print_r(array_uintersect_uassoc($array1, $array2, "strcasecmp", "strcasecmp"));
?>

위 예제의 출력:

Array
(
    [a] => green
    [b] => brown
)

참고

  • array_uintersect() - Computes the intersection of arrays, compares data by a callback function
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function

User Contributed Notes

aidan at php dot net 31-Aug-2004 12:52
Please note, the correct function synopsis is:

array array_uintersect_uassoc ( array array1, array array2 [, array ...], callback data_compare_func, callback key_compare_func)

You may specifiy as many arrays as you like, but the last two parameters must be valid call back functions.


Computes the intersection of arrays, compares data by a callback function

  • ≪ array_uintersect_uassoc
  • array_unique ≫
  • PHP Manual
  • 배열 함수 목록
  • Computes the intersection of arrays, compares data by a callback function

array_uintersect

(PHP 5, PHP 7)

array_uintersect — Computes the intersection of arrays, compares data by a callback function

설명

array array_uintersect ( array $array1 , array $array2 [, array $... ], callable $value_compare_func )

Computes the intersection of arrays, compares data by a callback function.

인수

array1

The first array.

array2

The second array.

value_compare_func

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )

반환값

Returns an array containing all the values of array1 that are present in all the arguments.

예제

Example #1 array_uintersect() example

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");

print_r(array_uintersect($array1, $array2, "strcasecmp"));
?>

위 예제의 출력:

Array
(
    [a] => green
    [b] => brown
    [0] => red
)

참고

  • array_intersect() - 배열의 교집합을 계산
  • array_intersect_assoc() - 인덱스 검사과 함께 배열의 교집합을 계산
  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions

User Contributed Notes

Nate at RuggFamily dot com 02-Feb-2007 09:03
I want to stress that in the user function, you do need to return either a 1 or a -1 properly; you cannot simply return 0 if the results are equal and 1 if they are not.

The following code is incorrect:

<?php
function myfunction($v1,$v2)
{
if ($v1===$v2)
    {
    return 0;
    }
return 1;
}

$a1=array(1, 2, 4);
$a2=array(1, 3, 4);
print_r(array_uintersect($a1,$a2,"myfunction"));
?>

This code is correct:

<?php
function myfunction($v1,$v2)
{
if ($v1===$v2)
    {
    return 0;
    }
if ($v1 > $v2) return 1;
return -1;
}
$a1=array(1, 2, 4);
$a2=array(1, 3, 4);
print_r(array_uintersect($a1,$a2,"myfunction"));
?>


배열에서 중복된 값을 제거

  • ≪ array_uintersect
  • array_unshift ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열에서 중복된 값을 제거

array_unique

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

array_unique — 배열에서 중복된 값을 제거

설명

array array_unique ( array $array [, int $sort_flags ] )

입력 array를 취해서 중복값이 없는 새로운 배열을 반환한다.

키는 보존된다는 것에 유의한다. array_unique()는 우선 문자열로 취급되는 값들을 정렬하고, 모든 값중 첫번째로 만나는 키를 보유하게 될것이고, 다음의 모든 키들은 무시될것이다. 이 말의 의미는 정렬되지 않은 array 의 첫번째 관련 값의 키가 유지된다는 것이 아니다.

Note: 두 구성요소가 동치라고 볼수 있는 때는 (string) $elem1 === (string) $elem2 일때 만이다. In words: when the string representation is the same. 첫번째 원소가 사용될것이다.

인수

array

입력 배열.

sort_flags

선택적인 두번째 인수 sort_flags는 다음 값을 사용해서 정렬 순서를 변경합니다:

정렬 형식 플래그는:

  • SORT_REGULAR - 보통 비교 (자료형 변환 없음)
  • SORT_NUMERIC - 수치 비교
  • SORT_STRING - 문자열로 비교
  • SORT_LOCALE_STRING - 현재 로케일에 기반한 문자열로 비교. PHP 4.4.0과 PHP 5.0.2에서 추가. PHP 6 이전에, setlocale()로 바꿀 수 있는 시스템 로케일을 사용합니다. PHP 6부터, i18n_loc_set_default() 함수를 사용해야 합니다.

반환값

필터한 배열을 반환합니다.

변경점

버전 | 설명
5.2.9 | 선택적인 sort_flags 추가.

예제

Example #1 array_unique() 예제

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>

위 예제의 출력:

Array
(
    [a] => green
    [0] => red
    [1] => blue
)

Example #2 array_unique()와 자료형

<?php
$input = array(4, "4", "3", 4, 3, "3");
$result = array_unique($input);
var_dump($result);
?>

위 예제의 출력:

array(2) {
  [0] => int(4)
  [2] => string(1) "3"
}

User Contributed Notes

zoolyka at gmail dot com 19-Jan-2016 07:48
I found the simplest way to "unique" multidimensional arrays as follows:

<?php

$array = array(
    'a' => array(1, 2),
    'b' => array(1, 2),
    'c' => array(2, 2),
    'd' => array(2, 1),
    'e' => array(1, 1),
);

$array = array_map('json_encode', $array);
$array = array_unique($array);
$array = array_map('json_decode', $array);

print_r($array);

?>

As you can see "b" will be removed without any errors or notices.
tim at heuer dot nz 12-Oct-2015 07:57
I created a function for doing array_unique with a custom in_array function. So, if elements are considered in the array passed to this function, the array returned won't include elements that are considered equal and only return the values from the first array that are not considered in the array:

<?php

function uniqueBy(array $array, callable $in_array_func) {
    $result = array();
    foreach ($array as $key => $item) {
        if (!$in_array_func($item, $result)) {
            $result[$key] = $item;
        }
    }
    return $result;
}

?>

Example usage:

<?php

$in_array_func = function($item, $array) {
    $fromBower = function($item) {
        $result = substr($item, strpos($item, 'bower_components'));
        return $result;
    };
    foreach ($array as $myItem) {
        if ($fromBower($item) == $fromBower($myItem)) {
            return true;
        }
    }
    return false;
};

// ...
$jss = uniqueBy($jss, $in_array_func);
?>
Ludovico Grossi 19-Dec-2014 11:01
[Editor's note: please note that this will not work well with non-scalar values in the array. Array keys can not be arrays themselves, nor streams, resources, etc. Flipping the array causes a change in key-name]

You can do a super fast version of array_unique directly in PHP, even faster than the other solution posted in the comments!

Compared to the built in function it is 20x faster! (2x faster than the solution in the comments).

<?php
function superfast_array_unique($array) {
    return array_keys(array_flip($array));
}
?>

This works faster for small and big arrays.
Ghanshyam Katriya(anshkatriya at gmail) 09-Dec-2014 07:15
Create multidimensional array unique for any single key index.
e.g I want to create multi dimentional unique array for specific code

Code :
My array is like this,

<?php
$details = array(
    0 => array("id"=>"1", "name"=>"Mike",    "num"=>"9876543210"),
    1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"),
    2 => array("id"=>"1", "name"=>"Mathew",  "num"=>"784581254"),
);
?>

You can make it unique for any field like id, name or num.

I have develop this function for same :
<?php
function unique_multidim_array($array, $key) {
    $temp_array = array();
    $i = 0;
    $key_array = array();

    foreach($array as $val) {
        if (!in_array($val[$key], $key_array)) {
            $key_array[$i] = $val[$key];
            $temp_array[$i] = $val;
        }
        $i++;
    }
    return $temp_array;
}
?>

Now, call this function anywhere from your code,

something like this,
<?php
$details = unique_multidim_array($details,'id');
?>

Output will be like this :
<?php
$details = array(
    0 => array("id"=>"1","name"=>"Mike","num"=>"9876543210"),
    1 => array("id"=>"2","name"=>"Carissa","num"=>"08548596258"),
);
?>
mostafatalebi at rocketmail dot com 13-Dec-2013 11:38
If you find the need to get a sorted array without it preserving the keys, use this code which has worked for me:

<?php

$array = array("hello", "fine", "good", "fine", "hello", "bye");

$get_sorted_unique_array = array_values(array_unique($array));

?>

The above code returns an array which is both unique and sorted from zero.
Felipe CWB 12-Dec-2013 01:48
array_unique only compares with the string,
But to compare with various types of variables I used like this:

<?php
/**
* master ArrayUnique
* now compare all types
* @param array $array
* @return array
*/
function arrayUnique($array)
{
    foreach ($array as $k => $v) {
        foreach ($array as $k2 => $v2) {
            if (($v2 == $v) && ($k != $k2)) {
                unset($array[$k]);
            }
        }
    }
    return $array;
}

$a = [];

$a[] = (object)['nome' => 'Bob'];
$a[] = (object)['nome' => 'Peter'];
$a[] = (object)['nome' => 'Jhon'];
$a[] = (object)['nome' => 'James'];
$a[] = (object)['nome' => 'David'];
$a[] = (object)['nome' => 'Bob']; // repeated

if ($a[0] == $a[5]) { // true
    echo 'yes';
} else {
    echo 'not';
}

echo "\n\n";

$a = arrayUnique($a);

print_r($a);
?>
CertaiN 29-Nov-2013 11:58
Let's implement array_unique_callback() !

Function:
<?php

function array_unique_callback(array $arr, callable $callback, $strict = false) {
    return array_filter(
        $arr,
        function ($item) use ($strict, $callback) {
            static $haystack = array();
            $needle = $callback($item);
            if (in_array($needle, $haystack, $strict)) {
                return false;
            } else {
                $haystack[] = $needle;
                return true;
            }
        }
    );
}

?>

Usage:
<?php

$companies = array(
    0 => array(
        'name' => 'Foo - Ltd.',
        'phone' => 'XXX-YYY-ZZZ',
        'category' => 'supplyment',
    ),
    1 => array(
        'name' => 'Bar - Ltd.',
        'phone' => 'xxx-yyy-zzz',
        'category' => 'supplyment',
    ),
    2 => array(
        'name' => 'Baz - Ltd.',
        'phone' => 'AAA-BBB-CCC',
        'category' => 'alcohol',
    ),
);

$companies = array_unique_callback(
    $companies,
    function ($company) {
        return $company['category'];
    }
);

print_r($companies);

?>

Result:

Array
(
    [0] => Array
        (
            [name] => Foo - Ltd.
            [phone] => XXX-YYY-ZZZ
            [category] => supplyment
        )

    [2] => Array
        (
            [name] => Baz - Ltd.
            [phone] => AAA-BBB-CCC
            [category] => alcohol
        )

)
Tim S. 20-Jun-2013 04:14
I had to write a bunch of results to an array with a high probability of duplicates.
The array was written to the file in chunks, using a for loop. It occurred to me that I often had blank lines in my file.

Because this function preserve keys, I had gaps in my numeric keys. If you don't want to preserve keys, like me, you can use the following function.

<?php
function array_unique_ignore_keys(&$array, $sort_flags = SORT_STRING) {
    return array_values(array_unique($array, $sort_flags));
}
?>
bx16soupapes at gmail dot com 12-Jun-2013 04:55
Another form to make an array unique (manual):

This is my array

Array
(
    [0] => Array
        (
            [0] => 40665
            [1] => 40665
            [2] => 40665
            [3] => 40665
            [4] => 40666
            [5] => 40666
            [6] => 40666
            [7] => 40666
            [8] => 40667
            [9] => 40667
            [10] => 40667
            [11] => 40667
            [12] => 40667
            [13] => 40668
            [14] => 40668
            [15] => 40668
            [16] => 40668
            [17] => 40668
            [18] => 40669
            [19] => 40669
            [20] => 40670
            [21] => 40670
            [22] => 40670
            [23] => 40670
            [24] => 40671
            [25] => 40671
            [26] => 40671
            [27] => 40671
            [28] => 40671
        )

    [1] => Array
        (
            [0] => 40672
            [1] => 40672
            [2] => 40672
            [3] => 40672
        )

)

this is my script:

    $anterior = 0;
    foreach($item as $array_key => $array_value)
    {
            echo "<li>$array_key";
            echo "<ul>";
            foreach($array_value as $xarray_key => $xarray_value){
                if($xarray_value != $anterior) {
                    echo "<li>$xarray_key => $xarray_value";
                    $item_nuevo[$array_key][] = $xarray_value;    // or to use the same key number $item_nuevo[$array_key][$xarray_key] = $xarray_value;
                }
                $anterior = $xarray_value;
            }
            echo "</ul>";
    }

result:

0
    0 => 40665
    4 => 40666
    8 => 40667
    13 => 40668
    18 => 40669
    20 => 40670
    24 => 40671

1
    0 => 40672

saludos desde chile.
yosef_cool_ha at hotmail dot com 01-Apr-2013 06:45
if you wish to unique the arrays recusively(multi dimensional) no matter how deep hope this function can help you:

function array_unique_recusive($arr){
foreach($arr as $key=>$value)
if(gettype($value)=='array')
    $arr[$key]=array_unique_recusive($value);
return array_unique($arr,SORT_REGULAR);
}
sashasimkin at gmail dot com 25-Apr-2012 10:26
My object unique function:

<?php
function object_unique( $obj ){
    $objArray = (array) $obj;

    $objArray = array_intersect_assoc( array_unique( $objArray ), $objArray );

    foreach( $obj as $n => $f ) {
        if( !array_key_exists( $n, $objArray ) ) unset( $obj->$n );
    }

    return $obj;
}
?>

And these code:

<?php
class Test{
    public $pr0 = 'string';
    public $pr1 = 'string1';
    public $pr2 = 'string';
    public $pr3 = 'string2';
}

$obj = new Test;

var_dump( object_unique( $obj ) );
?>

returns:
object(Test)[1]
  public 'pr0' => string 'string' (length=6)
  public 'pr1' => string 'string1' (length=7)
  public 'pr3' => string 'string2' (length=7)
gma (at) qoob (dot) gr 12-Nov-2011 07:07
Lets say that you want to capture unique values from multidimensional arrays and flatten them in 0 depth.

i.e.
<?php
$tmp = array( 'a' => array( 1,2,3,4 ), 'b' => array( 'c' => array( 4,5,6,7 ) ) );
?>

will return with array_flat( $tmp ) --> array( 1,2,3,4,5,6,7 );

I hope that the function will help someone

<?php
/**
 * @params      : $a            array           the recursion array
 *              : $s            array           storage array
 *              : $l            integer         the depth level
 *
 */
if( !function_exists( 'array_flat' ) )
{
    function array_flat( $a, $s = array( ), $l = 0 )
    {
        # check if this is an array
        if( !is_array( $a ) )                           return $s;

        # go through the array values
        foreach( $a as $k => $v )
        {
            # check if the contained values are arrays
            if( !is_array( $v ) )
            {
                # store the value
                $s[ ]       = $v;

                # move to the next node
                continue;

            }

            # increment depth level
            $l++;

            # replace the content of stored values
            $s              = array_flat( $v, $s, $l );

            # decrement depth level
            $l--;

        }

        # get only unique values
        if( $l == 0 ) $s = array_values( array_unique( $s ) );

        # return stored values
        return $s;

    } # end of function array_flat( ...

}
?>
Friendly Code 01-Aug-2011 12:48
I required a function that removed a specific duplicate entry from an array and ignoring all others so came up with this:

<?php
function specified_array_unique($array, $value)
{
    $count = 0;

    foreach($array as $array_key => $array_value)
    {
        if ( ($count > 0) && ($array_value == $value) )
        {
            unset($array[$array_key]);
        }

        if ($array_value == $value) $count++;
    }

    return array_filter($array);
}
?>
0cool.f 23-May-2011 12:54
Hope this can help...

<?php
function array_unique_key_group($array) {
    if(!is_array($array))
        return false;

    $temp = array_unique($array);
    foreach($array as $key => $val) {
        $i = array_search($val,$temp);
        if(!empty($i) && $key != $i) {
            $temp[$i.','.$key] = $temp[$i];
            unset($temp[$i]);
        }
    }
    return $temp;
}
?>

this function return an array that is unique, but preserve every key for the element...
sorry for bad english I'm italian...

$array['asd'] = 'value';
$array['lol'] = 'value';
$array['foo'] = 'val';
$array['bar'] = 'val';

var_dump(array_unique_key_group($array));
// will be output
array(2) { ["asd,lol"]=> string(5) "value" ["foo,bar"]=> string(3) "val" }
mykelscappin at gmail dot com 31-Mar-2011 02:18
I made an array_unique function to handle deep arrays:

<?php
    function array_unique_deep($array) {
        $values=array();
        //ideally there would be some is_array() testing for $array here...
        foreach ($array as $part) {
            if (is_array($part)) $values=array_merge($values,array_unique_deep($part));
            else $values[]=$part;
        }
        return array_unique($values);
    }

    $test=array(123,456,array(789,123,array(456),789),array(123));
    print_r(array_unique_deep($test));

?>

outputs

Array
(
    [0] => 123
    [1] => 456
    [2] => 789
)
michiel ed thalent nl 19-Jun-2010 02:04
If you use SORT_NUMERIC on this kind of filtering it will be significantly faster.
However the array_flip method still is twice as fast.
Anonymous 16-Jun-2010 02:46
It's often faster to use a foreache and array_keys than array_unique:

    <?php

    $max = 1000000;
    $arr = range(1,$max,3);
    $arr2 = range(1,$max,2);
    $arr = array_merge($arr,$arr2);

    $time = -microtime(true);
    $res1 = array_unique($arr);
    $time += microtime(true);
    echo "deduped to ".count($res1)." in ".$time;
    // deduped to 666667 in 32.300781965256

    $time = -microtime(true);
    $res2 = array();
    foreach($arr as $key=>$val) {
        $res2[$val] = true;
    }
    $res2 = array_keys($res2);
    $time += microtime(true);
    echo "<br />deduped to ".count($res2)." in ".$time;
    // deduped to 666667 in 0.84372591972351

    ?>
regeda at inbox dot ru 12-Apr-2010 05:11
recursive array unique for multiarrays

<?php
function super_unique($array)
{
  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

  foreach ($result as $key => $value)
  {
    if ( is_array($value) )
    {
      $result[$key] = super_unique($value);
    }
  }

  return $result;
}
?>
brendel at krumedia dot de 24-Feb-2010 08:47
Prior to 5.2.9 you may create a list of unique objects this way:

<?php
for (; ; ) {
  // ...
  $uniqueObjectList[spl_object_hash($myObject)] = $myObject;
}
?>
amri [ at t] dhstudio dot eu 18-Dec-2009 11:36
I searched how to show only the duplicate elements from array, but failed.
Here is my solution:

<?php
function arrayDuplicate($array)
{
return array_unique(array_diff_assoc($array1,array_unique($array1)));
};
?>

Example:
<?php
$arr1 = array('foo', 'bar', 'xyzzy', '&', 'xyzzy',
'baz', 'bat', '|', 'xyzzy', 'plugh',
'xyzzy', 'foobar', '|', 'plonk', 'xyzzy',
'apples', '&', 'xyzzy', 'oranges', 'xyzzy',
'pears','foobar');

$result=arrayDuplicate($arr1);
print_r($result);exit;
?>

Output:

Array
(
[4] => xyzzy
[12] => |
[16] => &
[21] => foobar
)
Youssef Omar 16-Dec-2009 08:09
This is how to merge 2 comma separated lists with unique value only.

<?php
  $list1 = "4444, 5555, 6666";
  $list2 = "4444, 5555, 7777";

  // combine both lists with unique values only
  $list3 = implode("," , array_unique(array_merge(explode(",",$list1),explode(",", $list2))));

  echo $list3;
?>

The result is: 4444,5555,6666,7777
steve at visual77 dot com 27-May-2009 01:41
One way to use array_unique() to handle the multidimensional array issue is to use an md5 hash of the serialized array as the key. I've seen some solutions listed here that stored the serialized string, but that requires unserialization, and moving data back and forth between two states for little reason is just a bad idea. Instead, you could try this method:

<?php
$values = array();

foreach($data as $d) {
    $values[md5(serialize($d))] = $d;
}

sort($values);
?>

This will serialize and hash the value, and store the value with the hash as they key. This will ensure that each piece is only stored once, as the second attempt to store will just overwrite the first attempt with identical data.

The sort() at the end is a bit unnecessary, but I had to use it on my project, because the function I was passing the array to couldn't handle keys that weren't valid XML node names (and some md5 hash strings will start with numbers, which is invalid for XML).
dirk dot avery a t gmail 30-Apr-2009 05:45
Although array_unique is not intended to work with multi-dimensional arrays, it does on 5.2.9.  However, it does not for 5.2.5.  Beware.
Ultimater at gmail dot com 21-Apr-2009 03:26
Let's say you have
<?php
$v=array("blue","blue","blue","blue");
if($v[0]==$v[1] && $v[1]==$v[2] && $v[2]==$v[3])
echo "Y"; else echo "N";//Y
?>

It works but the if-statement gets a bit messy if the number of array elements gets massive.
I find it is easier to use array_unique like so:
<?php
$v=array("blue","blue","blue","blue");
echo sizeof(array_unique($v))==1?"Y":"N";//Y
?>
inithello at gmail dot com 12-Mar-2009 08:47
I noticed there was no way to tell array_unique() to ignore certain duplicated keys, so I wrote the following. I imagine there's half a dozen more efficient ways to do this, but here goes:

<?php
$array = array('foo', 'bar', 'xyzzy', '&', 'xyzzy',
               'baz', 'bat', '|', 'xyzzy', 'plugh',
               'xyzzy', 'foobar', '|', 'plonk', 'xyzzy',
               'apples', '&', 'xyzzy', 'oranges', 'xyzzy',
               'pears');

$ignore_values = array('|', '&');

print_r(make_unique($array, $ignore_values));

function make_unique($array, $ignore)
{
    while($values = each($array))
    {
        if(!in_array($values[1], $ignore))
        {
            $dupes = array_keys($array, $values[1]);
            unset($dupes[0]);
            foreach($dupes as $rmv)
            {
                unset($array[$rmv]);
            }
        }
    }
    return $array;
}
?>
OUTPUT:
Array
(
    [0] => foo
    [1] => bar
    [2] => xyzzy
    [3] => &
    [5] => baz
    [6] => bat
    [7] => |
    [9] => plugh
    [11] => foobar
    [12] => |
    [13] => plonk
    [15] => apples
    [16] => &
    [18] => oranges
    [20] => pears
)
serg dot podtynnyi at gmail dot com 06-Feb-2009 08:21
//Remove duplicates from a text files and dump result in one file for example: emails list, links list etc

<?php

$data1 = file("data1.txt");
$data2 = file("data2.txt");

file_put_contents('unique.txt', implode('', array_unique(array_merge($data1,$data2))));
?>
jusvalceanu - SPAM at SPAM - yahoo dot com 06-Nov-2008 11:23
so .... my problem was multidimensional sort.

<?php
      $new = array();
      $exclude = array("");
      for ($i = 0; $i<=count($attribs)-1; $i++) {
         if (!in_array(trim($attribs[$i]["price"]) ,$exclude)) { $new[] = $attribs[$i]; $exclude[] = trim($attribs[$i]["price"]); }
      }

?>

Array $attribs is an array contaning arrays. Each array in the $attrib array consists in multiple fields (ex: name, lenght, price, etc.) to be more simpler in speech think that $attrib is the array resulted by a search sql query done by a visitator on your online shoopping website ... (so ... each array in the $attrib is a product :P) if you want to sort only the uniq results use the above or use this:

<?php

   /* Our Array of products */
   $attribs[] = array(
                           "name"         => "Test Product 1",
                           "length"     => "42 cm",
                           "weight"     => "0,5 kg",
                           "price"     => "10 $",
                           "stock"     => "100",
                        );

   $attribs[] = array(
                           "name"         => "Test Product 2",
                           "length"     => "42 cm",
                           "weight"     => "1,5 kg",
                           "price"     => "10 $",
                           "stock"     => "200",
                        );

    /* The nice stuff */

      $new = array();
      $exclude = array("");
      for ($i = 0; $i<=count($attribs)-1; $i++) {
         if (!in_array(trim($attribs[$i]["price"]) ,$exclude)) { $new[] = $attribs[$i]; $exclude[] = trim($attribs[$i]["price"]); }
      }

      print_r($new); // $new is our sorted array

?>

Have fun tweaking this ;)) i know you will ;))

From Romania With Love
florian at box dot net 08-Oct-2008 10:29
I came across one limitation of array_unique: it doesn't work properly if you have arrays inside your main array.

The reason is that to compare two values, the function tests if (string) $value1 == (string) $value2. So if $value1 and $value2 are both arrays, the function will evaluate the test to 'Array' == 'Array', and decide that the $values are repeated even if the arrays are different.

So a work around is to find a better conversion of an array to a string, which can be done with json:

<?php
print "define an array with repeated scalar '1' and repeated 'array(1)':";
$a_not_unique = array(
    'a' => 1,
    'b' => 1,
    'c' => 2,
    'd' => array(1),
    'e' => array(1),
    'f' => array(2),
);
print_r($a_not_unique);

print "try to use simply array_unique, which will not work since it exludes 'array(2)':";
$a_unique_wrong = array_unique($a_not_unique);
print_r($a_unique_wrong);

print "convert to json before applying array_unique, and convert back to array, which will successfully keep 'array(2)':";
$a_unique_right = $a_not_unique;
array_walk($a_unique_right, create_function('&$value,$key', '$value = json_encode($value);'));
$a_unique_right = array_unique($a_unique_right);
array_walk($a_unique_right, create_function('&$value,$key', '$value = json_decode($value, true);'));
print_r($a_unique_right);
?>

Results:
define an array with repeated scalar '1' and repeated 'array(1)':
Array
(
    [a] => 1
    [b] => 1
    [c] => 2
    [d] => Array
        (
            [0] => 1
        )

    [e] => Array
        (
            [0] => 1
        )

    [f] => Array
        (
            [0] => 2
        )
)

try to use simply array_unique, which will not work since it exludes 'array(2)':
Array
(
    [a] => 1
    [c] => 2
    [d] => Array
        (
            [0] => 1
        )
)

convert to json before applying array_unique, and convert back to array, which will successfully keep 'array(2)':
Array
(
    [a] => 1
    [c] => 2
    [d] => Array
        (
            [0] => 1
        )

    [f] => Array
        (
            [0] => 2
        )
)
quecoder at gmail 26-Aug-2008 07:30
another method to get unique values is :

<?php
$alpha=array('a','b','c','a','b','d','e','f','f');

$alpha= array_keys(array_count_values($alpha));

print_r($alpha);
?>

Output:
Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f )
soapergem at gmail dot com 14-Aug-2008 11:39
Here's another solution for returning an array that only includes repeated values. There is one given below but it only works on numerically indexed arrays; this one is more comprehensive since I used the foreach iterator. Also, this one preserves keys--in that the returned result contains a distinct list of repeats (storing only the first instance it encounters of each duplicate value).

<?php

function array_repeated($array)
{
    if ( !is_array($array) ) {
        return false;
    }

    $duplicates = array();
    foreach ( $array as $key => $val ) {
        end($array);
        $k = key($array);
        $v = current($array);

        while ( $k !== $key ) {
            if ( $v === $val ) {
                $duplicates[$key] = $v;
                                break;
            }

            $v = prev($array);
            $k = key($array);
        }
    }

    return $duplicates;
}

?>
Dorphalsig 28-Jul-2008 06:47
I had a problem with array_unique and multidimensional arrays ... Maybe there's a better way to do this, but this will work for any dimensional arrays.

<?php
function arrayUnique($myArray)
{
    if(!is_array($myArray))
           return $myArray;

    foreach ($myArray as &$myvalue){
        $myvalue=serialize($myvalue);
    }

    $myArray=array_unique($myArray);

    foreach ($myArray as &$myvalue){
        $myvalue=unserialize($myvalue);
    }

    return $myArray;

}
?>
PHP Expert 14-Apr-2008 02:34
Case insensitive for PHP v4.x and up.

<?php

function in_iarray($str, $a) {
    foreach ($a as $v) {
        if (strcasecmp($str, $v) == 0) {
            return true;
        }
    }
    return false;
}

function array_iunique($a) {
    $n = array();
    foreach ($a as $k => $v) {
        if (!in_iarray($v, $n)) {
            $n[$k]=$v;
        }
    }
    return $n;
}

$input = array("aAa","bBb","cCc","AaA","ccC","ccc","CCC","bBB","AAA","XXX");
$result = array_iunique($input);
print_r($result);

/*
Array
(
    [0] => aAa
    [1] => bBb
    [2] => cCc
    [9] => XXX
)
*/
?>
Ray dot Paseur at SometimesUsesGmail dot com 02-Mar-2008 04:46
I needed to identify email addresses in a data table that were replicated, so I wrote the array_not_unique() function:

<?php

function array_not_unique($raw_array) {
    $dupes = array();
    natcasesort($raw_array);
    reset ($raw_array);

    $old_key    = NULL;
    $old_value    = NULL;
    foreach ($raw_array as $key => $value) {
        if ($value === NULL) { continue; }
        if ($old_value == $value) {
            $dupes[$old_key]    = $old_value;
            $dupes[$key]        = $value;
        }
        $old_value    = $value;
        $old_key    = $key;
    }
return $dupes;
}

$raw_array     = array();
$raw_array[1]    = 'abc@xyz.com';
$raw_array[2]    = 'def@xyz.com';
$raw_array[3]    = 'ghi@xyz.com';
$raw_array[4]    = 'abc@xyz.com'; // Duplicate

$common_stuff    = array_not_unique($raw_array);
var_dump($common_stuff);
?>
php_array_unique [at] peterwarnock.com 19-Dec-2007 08:20
To clarify the note above Example #1, the function appears to cast the elements to strings for comparison and then return the type of the first unique element encountered.

<?php
$input = array(0, 2, "2", 3, "9", 9);
$result = array_unique($input);
var_dump($result);
?>

array(4) {
  [0]=>
  int(0)
  [1]=>
  int(2)
  [3]=>
  int(3)
  [4]=>
  string(1) "9"
}
mnbayazit 28-Oct-2007 11:18
Case insensitive; will keep first encountered value.

<?php

function array_iunique($array) {
    $lowered = array_map('strtolower', $array);
    return array_intersect_key($array, array_unique($lowered));
}

?>
xjazey at hotmail dot com 11-Sep-2007 08:50
This is a fast solution to remove duplicate values from an array when it contains only strings or only integers:

<?php

$array = array(
  0 => 'Yellow',
  1 => 'Green',
  2 => 'Yellow',
  3 => 'Blue',
  4 => 'Yellow',
);

$uniq = array_keys(array_flip($array));

var_export($uniq);

/* Output:
array (
  0 => 'Yellow',
  1 => 'Green',
  2 => 'Blue',
)
*/

?>
webmaster at jukkis dot net 30-Jul-2007 01:08
Another way to 'unique column' an array, in this case an array of objects:
Keep the desired unique column values in a static array inside the callback function for array_filter.

Example:
<?php
/* example object */
class myObj {
  public $id;
  public $value;
  function __construct( $id, $value ) {
    $this->id = $id;
    $this->value = $value;
  }
}

/* callback function */
function uniquecol( $obj ) {
  static $idlist = array();

  if ( in_array( $obj->id, $idlist ) )
    return false;

  $idlist[] = $obj->id;
  return true;
}

/* a couple of arrays with second array having an element with same id as the first */
$list  = array( new myObj( 1, 1  ), new myObj( 2, 100 ) );
$list2 = array( new myObj( 1, 10 ), new myObj( 3, 100 ) );
$list3 = array_merge( $list, $list2 );

$unique = array_filter( $list3, 'uniquecol' );
print_r( $list3 );
print_r( $unique );

?>

In addition, use array_merge( $unique ) to reindex.
arr1 01-Nov-2006 05:59
Just to note that array_unique, treats null values as none unique values. So if your using array_unique to detect duplicate values it will also detect multiple null values.
Ome_Henk 27-Oct-2006 09:16
For people looking at the flip flip method for getting unique values in a simple array. This is the absolute fastest method:

<?php
$unique = array_keys(array_flip($array));
?>

It's marginally faster as:
<?php
$unique = array_merge(array_flip(array_flip($array)));
?>

And it's marginally slower as:
<?php
$unique array_flip(array_flip($array)); // leaves gaps
?>

It's still about twice as fast or fast as array_unique.

This tested on several different machines with 100000 random arrays. All machines used a version of PHP5.
geuis dot teses at gmail dot com 09-Oct-2006 07:27
Here's the shortest line of code I could find/create to remove all duplicate entries from an array and then reindex the keys.

<?php

// Fruits, vegetables, and other food:
$var = array('apple','banana','carrot','cat','dog','egg','eggplant','fish');

$var = array_values(array_unique($var));
?>
keneks at gmail dot com 30-Sep-2006 04:01
Taking the advantage of array_unique, here is a simple function to check if an array has duplicate values.

It simply compares the number of elements between the original array and the array_uniqued array.

<?php

function array_has_duplicates(array $array)
{
    $uniq = array_unique($array);
    return count($uniq) != count($array);
}

?>
uditsawhney at yahoo dot com 15-Jul-2006 09:14
<?php

//Fn for array_unique column-wise for multi-dimensioanl array without losing keys | Start
function array_uniquecolumn($arr)
{
    $rows   = sizeof($arr);
    $columns = sizeof($arr[0]);

    $columnkeys = array_keys($arr[0]);

    for($i=0; $i<$columns; $i++)
    {
        for($j=0;$j<$rows;$j++)
        {
            for($k = $j+1; $k<$rows; $k++)
            {
                if($arr[$j][$columnkeys[$i]] == $arr[$k][$columnkeys[$i]])
                    $arr[$k][$columnkeys[$i]] = "";
            }
        }

    }

return ($arr);

}
//Fn for array_unique column-wise for multi-dimensioanl array without losing keys | Stop

$arrUGCourse[]= array(  "CTR" => "1",

                        "UGCOURSE"=>"ABC",

                        "TSINITIATE"=>"540",

                        "COUNT"=>"34",

                        "ENTRY_DT"=>"2006-05-01",

                        "CUMULATIVE"=> 44);

$arrUGCourse[]= array(  "CTR" => "2",

                        "UGCOURSE"=>"ABC",

                        "TSINITIATE"=>"5401",

                        "COUNT"=>"341",

                        "ENTRY_DT"=>"2006-05-11",

                        "CUMULATIVE"=> 44);

print_r(array_uniquecolumn($arrUGCourse));

?>
MoD 14-Apr-2006 06:02
The shortest way i found to remove duplicate array from a column,
For example if you parse Multiple XML sources, you can remove duplicate items that contain the same link.

<?PHP
function        remove_duplicate($array, $field)
{
  foreach ($array as $sub)
    $cmp[] = $sub[$field];
  $unique = array_unique($cmp);
  foreach ($unique as $k => $rien)
    $new[] = $array[$k];
  return $new;
}
?>
agarcia at rsn dot com dot co 31-Mar-2006 09:41
This is a script for multi_dimensional arrays

<?php
function remove_dup($matriz) {
    $aux_ini=array();
    $entrega=array();
    for($n=0;$n<count($matriz);$n++)
    {
        $aux_ini[]=serialize($matriz[$n]);
    }
    $mat=array_unique($aux_ini);
    for($n=0;$n<count($matriz);$n++)
    {

            $entrega[]=unserialize($mat[$n]);

    }
    return $entrega;
}
?>
mcmeijer at yahoo dot com 27-Jan-2006 02:18
This is a recursive arrayUnique function for arrays of any dimension. (tested with 4-dimensional array)
The line '$newArray=deleteEmpty($newArray);' is optional and removes empty keys and values
<?php
function arrayUnique($myArray)
    {
    $newArray = Array();
    if (is_array($myArray))
        {
        foreach($myArray as $key=>$val)
            {
            if (is_array($val))
                {
                $val2 = arrayUnique($val);
                }
            else
                {
                $val2 = $val;
                $newArray=array_unique($myArray);
                $newArray=deleteEmpty($newArray);
                break;
                }
            if (!empty($val2))
                {
                $newArray[$key] = $val2;
                }
            }
        }
    return ($newArray);
    }

function deleteEmpty($myArray)
    {
    $retArray= Array();
    foreach($myArray as $key=>$val)
        {
        if (($key<>"") && ($val<>""))
            {
            $retArray[$key] = $val;
            }
        }
    return $retArray;
    }
?>
memandeemail at gmail dot com 03-Jan-2006 07:47
Problem:
I have loaded an array with the results of a database
query.  The Fields are 'FirstName' and 'LastName'.

I would like to find a way to contactenate the two
fields, and then return only unique values for the
array.  For example, if the database query returns
three instances of a record with the FirstName John
and the LastName Smith in two distinct fields, I would
like to build a new array that would contain all the
original fields, but with John Smith in it only once.
Thanks for: Colin Campbell

Solution:

<?php
/**
 * The same thing than implode function, but return the keys so
 *
 * <code>
 * $_GET = array('id' => '4587','with' => 'key');
 * ...
 * echo shared::implode_with_key('&',$_GET,'='); // Resultado: id=4587&with=key
 * ...
 * </code>
 *
 * @param string $glue Oque colocar entre as chave => valor
 * @param array $pieces Valores
 * @param string $hifen Separar chave da array do valor
 * @return string
 * @author memandeemail at gmail dot com
 */
function implode_with_key($glue = null, $pieces, $hifen = ',') {
  $return = null;
  foreach ($pieces as $tk => $tv) $return .= $glue.$tk.$hifen.$tv;
  return substr($return,1);
}

/**
 * Return unique values from a tree of values
 *
 * @param array $array_tree
 * @return array
 * @author memandeemail at gmail dot com
 */
function array_unique_tree($array_tree) {
  $will_return = array(); $vtemp = array();
  foreach ($array_tree as $tkey => $tvalue) $vtemp[$tkey] = implode_with_key('&',$tvalue,'=');
  foreach (array_keys(array_unique($vtemp)) as $tvalue) $will_return[$tvalue] = $array_tree[$tvalue];
  return $will_return;
}

$problem = array_fill(0,3,
array('FirstName' => 'John', 'LastName' => 'Smith')
);

$problem[] = array('FirstName' => 'Davi', 'LastName' => 'S. Mesquita');
$problem[] = array('FirstName' => 'John', 'LastName' => 'Tom');

print_r($problem);

print_r(array_unique_tree($problem));
?>
passtschu AT freenet DOT de 21-Sep-2005 04:20
array_unique for multidimensional arrays. similar to the DISTINCT in SQL function.
the function can group, sum and count keys

<?PHP
/*
$array - nothing to say
$group_keys - columns which have to be grouped - can be STRING or ARRAY (STRING, STRING[, ...])
$sum_keys - columns which have to be summed - can be STRING or ARRAY (STRING, STRING[, ...])
$count_key - must be STRING - count the grouped keys
*/
function array_distinct ($array, $group_keys, $sum_keys = NULL, $count_key = NULL){
  if (!is_array ($group_keys)) $group_keys = array ($group_keys);
  if (!is_array ($sum_keys)) $sum_keys = array ($sum_keys);

  $existing_sub_keys = array ();
  $output = array ();

  foreach ($array as $key => $sub_array){
    $puffer = NULL;
    #group keys
    foreach ($group_keys as $group_key){
      $puffer .= $sub_array[$group_key];
    }
    $puffer = serialize ($puffer);
    if (!in_array ($puffer, $existing_sub_keys)){
      $existing_sub_keys[$key] = $puffer;
      $output[$key] = $sub_array;
    }
    else{
      $puffer = array_search ($puffer, $existing_sub_keys);
      #sum keys
      foreach ($sum_keys as $sum_key){
        if (is_string ($sum_key)) $output[$puffer][$sum_key] += $sub_array[$sum_key];
      }
      #count grouped keys
      if (!array_key_exists ($count_key, $output[$puffer])) $output[$puffer][$count_key] = 1;
      if (is_string ($count_key)) $output[$puffer][$count_key]++;
    }
  }
  return $output;
}
?>
muddmonkey@harveyMcoldotedu 17-Aug-2005 10:44
If you're doing numeric arrays etc. I found flip-flip to work much better than array_unique:

<?PHP
    function microtime_float(){ //timing
       list($usec, $sec) = explode(" ", microtime());
       return ((float)$usec + (float)$sec);
    }

    //make an arry and fill it up
    $final=array();
    for($i=0;$i<50000;$i++){
        $final[]=$i%13; //make sure there are some dupes
    }
    //try array unique
    $start1 = microtime_float();
    array_unique($final);
    $stop1=microtime_float();
    echo($stop1-$start1.'<br>');
    //try my flip-flip
    $start2=microtime_float();
    array_flip(array_flip($final));
    $stop2=microtime_float();
    echo($stop2-$start2);
?>

Running this with only ints in the array (as above) I get runtimes such as:
1.6195669174194 (using unique)
0.017037868499756 (using flip flip)
which is two orders of magnitude faster!

Appending a string:
($final[]='test'.$i%13;)
gives:
0.42909598350525 (using unique)
0.023258924484253 (using flip-flip)
Which is not AS great, but still 20x faster than unique.

In both cases the flip-flip seems to use less memory than the unique.

Granted the flip-flip doesn't work for all cases, but if you're doing simple stuff like this, the flip-flip will give you better run times.

~JF
memandeemail at gmail dot com 15-Apr-2005 10:46
<?php
/**
 * Removes duplicate keys from an array
 *
 * @param array $array
 * @return array
 */
function array_unique_key($array) {
    $result = array();
    foreach (array_unique(array_keys($array)) as $tvalue) {
        $result[$tvalue] = $array[$tvalue];
    }
    return $result;
}
?>
Ric 05-Apr-2005 06:44
A very simple way of getting rid of duplicate entries and re-indexing with key starting at 0:

<?php

$temp = array_unique($main);
$uniq = array_values($temp);

?>
lucas.bickel AT purplehaze DOT ch 26-Oct-2004 05:39
I quite like the following code for making multidimensional arrays unique:

<?php
foreach ($arrAddressList AS $key => $arrAddress) {
    $arrAddressList[$key] = serialize($arrAddress);
}
$arrAddressList = array_unique($arrAdressList);
foreach ($arrAddressList AS $key => $strAddress) {
    $arrAddressList[$key] = unserialize($strAddress);
}
?>

This gets me a unique array while not minding wether the the original array contains arrays or just strings (or whatever...).
wernerlistas at terra dot com dot br 16-Jun-2004 05:50
Following the code copies of a little function I've wrote that actually works with multidimensional arrays.
It also resets the array indexes.

<?php
if ( !function_exists( "arrayUnique" ) ){
    function arrayUnique ( $rArray ){
        $rReturn = array ();
        while ( list( $key, $val ) = each ( $rArray ) ){
            if ( !in_array( $val, $rReturn ) )
            array_push( $rReturn, $val );
        }
        return $rReturn;
    }
}
?>
csaba at alum dot mit dot edu 10-Jun-2004 04:17
The following is an efficient, adaptable implementation of array_unique which always retains the first key having a given value:

<?php
function array_unique2(&$aray) {
    $aHash = array();
    foreach ($aray as $key => &$val) if (@$aHash[$val]++) unset ($aray[$key]);
}
?>

It is also adaptable to multi dimensional arrays.  For example, if your array is a sequence of (multidimensional) points, then in place of @$aHash[$val]++ you could use @$aHash[implode("X",$val)]++
If you want to not have holes in your array, you can do an array_merge($aray) at the end.

Csaba Gabor
patrikG at home dot net 11-Mar-2004 04:05
If you need to have the keys of the duplicates in an array returned, you may find this function useful:

<?php
function unique_events($array){
    //checks $array for duplicate values and returns an
        //array containing the keys of duplicates
    $count= array_intersect_assoc($array, array_flip( array_count_values($array)));
    foreach($array as $key=>$value){
        if (in_array($value,$count)){
            $return[$value][]=$key;
        }
    }
    return $return;
}
?>

Example:

Input:
Array
(
    [0] => 44
    [1] => 23
    [2] => 23
    [3] => 23
    [4] => 9
    [5] => 9
    [6] => 9
    [7] => 9
    [8] => 9
    [9] => 9
    [10] => 9
    [11] => 9
)

Function returns:
Array
(
    [23] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [9] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
            [3] => 7
            [4] => 8
            [5] => 9
            [6] => 10
            [7] => 11
        )

)
kay_rules at yahoo dot com 24-May-2003 11:28
this function will return an array with unique value and proper key increment start from 0.

<?php
/*******************************/
function my_array_unique($somearray){
    $tmparr = array_unique($somearray);
    $i=0;
    foreach ($tmparr as $v) {
        $newarr[$i] = $v;
        $i++;
    }
    return $newarr;
}
/********************************/
?>

eg:

<?php
$foo_arr[0] ='aa'
$foo_arr[1] ='bb'
$foo_arr[2] ='cc'
$foo_arr[3] ='bb'
$foo_arr[4] ='aa'
$foo_arr[5] ='dd'
?>

normal array_unique will return:

<?php
$foo_arr[0] ='aa';
$foo_arr[1] ='bb';
$foo_arr[2] ='cc';
$foo_arr[3] ='';
$foo_arr[4] ='';
$foo_arr[5] ='dd'
?>

my_array_unique will return:

<?php
$foo_arr[0] ='aa';
$foo_arr[1] ='bb';
$foo_arr[2] ='cc';
$foo_arr[3] ='dd'
?>
martin at lucas-smith dot co dot uk 27-Jan-2003 02:12
To get a list of the duplicated values in an array, array_unique isn't much help. Instead, use array_filter in conjunction with a callback function, as below:

<?php
$checkKeysUniqueComparison = create_function('$value','if ($value > 1) return true;');
$result = array_keys (array_filter (array_count_values($array), $checkKeysUniqueComparison));
?>

These two lines therefore will create $result, an array of duplicated values in the array $array, once each. E.g. the array
$array = array ("a", "b", "a", "b", "x", "y", "z", "x");
gives the result
Array([0] => a [1] => b [2] => x)
deigo at swirve dot NOSPAM dot com 23-Sep-2002 07:04
Before I found the mysql distinct I had to make a nicer array from the keys/values that I got from array_unique so.

<?php
$groups=array_unique($groups);
$newgroup[0]=reset($groups);
for ($x=1;$x<sizeof($groups);$x++)
{
  $newgroup[$x]=next($groups);
}
?>
php at hp-site dot dk 29-Aug-2002 08:15
Try this:
array_flip(array_flip($array));

It gives the same result as the old array_unique()
29-Aug-2002 11:39
<?php
$truc = array("l810u00","l810u00","l810q00");
$machin = array_unique($truc);
for($i=0;$i < count($machin) ; $i++){
print $machin[$i]."
";
}
?>
result :
l810u00

This is not strange: $machin (as returned by array unique), contains "l810u00" either in key[0] or key[1] but not both (the key depends on the ersion of PHP), and "l810q00" in key[2].
The returned array has TWO elements so count($machin)==2.
The returned array has a hole in it, and you're not displaying its full content. You could verify it by using this display loop instead:
foreach($machine as $key=>$value){
print '[' . $key . '] => ' . $value . '
";
}
result:
[0] => l810q00
[2] => l810u00
(the first line may display [1] instead of [0] for PHP 4.0.1p3, but you'll get the same order of values and two lines, as expected). When calling array_values() on the result, you're building a new array with the same values in the same order, but with renumbered keys (without holes in numeric keys).
spunk at dasspunk dot NOSPAM dot com 14-Nov-2001 01:52
I needed a way of retaining the original array's keys in the new, unique array. I came up with this. It works for my purposes but may need refinement.

<?php
function my_array_unique($somearray)
{
    asort($somearray);
    reset($somearray);
    $currentarrayvar = current($somearray);
    foreach ($somearray as $key=>$var)
    {
        if (next($somearray) != $currentarrayvar)
        {
            $uniquearray[$key] = $currentarrayvar;
            $currentarrayvar = current($somearray);
        }
    }
    reset($uniquearray);
    return $uniquearray;
}
?>
az at top-webdesign dot de 10-Jul-2001 10:00
Attention!
If you use array_unique be aware of data-types! (I spent hours of debugging because of that ...).

For example, if you've got an array containing a '3' as number and another '3' as string it won't be eliminated by array_unique.

An Example where this can happen, without really thinking about it:

I've got an article-list with product-numbers where the third and fourth digit is the code for the producer. So I read in the file an process it line by line and put each producer-code into an array:
------------------------------
<?php
$i=0;
while($line = fgets($csv, 10000) {
// splitting the line, product_no is the first part:

$data = explode(";", $line);

// putting the producer_code into an array:

$producer_id[$i] = trim(substr($data[0], 2, 2));

// make a special exception:

if(trim(substr($data[0], 2, 2)) == 40) {
$producer_id[$j] = '30';
}

// in the above line if you leave the 30 without the ''
// array_unique won't work!

$i++;
}

$producer_ids = array_values(array_unique($producer_id));
?>
-------------------------------
Result is to have all producer-ID's in an array without dupes.
rein at velt dot net 21-Apr-2001 09:44
Following code copies unique values from MyArray to TempArray.
Then copies non-empty elements from TempArray to UniqueArray.

Not the most elegant solution, but it works.

<?php
$TempArray = array_unique($MyArray);
while (list($index,$data)=each($TempArray)) {
      if (isempty($data)) {
          $UniqueArray[$index]=$data;
      }
}
?>
jllamas at bal dot com dot mx 26-Sep-2000 06:25
It seems that array_unique creates an exact copy of the original array and then elimitates duplicate values. It does NOT change the "internal references" of the array. For example:

<?php
    $test_alfa = array();
    $test_alfa[0] = "aa";
    $test_alfa[1] = "aa";
    $test_alfa[2] = "aa";
    $test_alfa[3] = "bb";
    $test_alfa[4] = "aa";
    $test_alfa[5] = "bb";
    $test_alfa[6] = "cc";
    $test_alfa[7] = "bb";

    $test_beta= array_unique($test_alfa);
    $numValues = count($test_beta);
    for ($i = 0 ; $i <= 7 ; $i++)
       echo("test_beta[$i] = $test_beta[$i] <br>");
    echo ("Number of elements in test_beta = $numValues ");
?>
will give you the following output:

test_beta[0] =
test_beta[1] = aa
test_beta[2] =
test_beta[3] =
test_beta[4] =
test_beta[5] = bb
test_beta[6] = cc
test_beta[7] =
Number of elements in test_beta = 3

The point is that you won't get the output you'd expect if you think that the values of the non duplicate elements are located in the first three array locations.

<?php
    $numValues = count($test_beta);
    for ($i=0;$i<=$numValues; $i++)
       echo("test_beta[$i] = $test_beta[$i] <br>");
    echo ("Number of elements in test_beta = $numValues ");
?>

will give you:

test_beta[0] =
test_beta[1] = aa
test_beta[2] =
Number of elements in test_beta = 3

Hope that saves u some debugging time!


배열의 맨 앞에 하나 이상의 원소를 첨가

  • ≪ array_unique
  • array_values ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 맨 앞에 하나 이상의 원소를 첨가

array_unshift

(PHP 4, PHP 5, PHP 7)

array_unshift — 배열의 맨 앞에 하나 이상의 원소를 첨가

설명

int array_unshift ( array &$array , mixed $var [, mixed $... ] )

array_unshift()는 전달된 원소들을 array의 앞에 첨가한다. 원소 목록이 모두 첨가되면, 첨가된 원소들은 같은 순서로 남아있게 된다는 것에 유의한다. 모든 숫자 배열 키들은 리터럴 키들이 건드러지지 않는 동안에는 제로부터 카운트를 시작할수 있도록 변경될것이다.

인수

array

입력 배열.

var

앞에 붙일 변수.

반환값

array안의 새로운 원소의 수를 반환한다.

예제

Example #1 array_unshift() 예제

<?php
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
?>

위 예제의 출력:

Array
(
    [0] => apple
    [1] => raspberry
    [2] => orange
    [3] => banana
)

참고

  • array_shift() - 배열의 맨 앞에 있는 원소를 시프트
  • array_push() - 배열의 끝에 하나 이상의 원소를 넣는다
  • array_pop() - 배열의 마지막 원소 빼내기

User Contributed Notes

Richard Akindele 17-Mar-2016 03:31
Another way to tack something to the beginning of an array is with array_merge().

$plans = array('AARP'=>'Senior', 'AAA'=>'Automobile Club');

$plans = array_merge(array("BAR"=>"Best Available Rate"),  $plans);
daniel at smallboxcms dot com 17-Apr-2015 05:18
Anonymous' associative version wasn't working for me, but it did with this small tweak:

function array_unshift_assoc(&$arr, $key, $val)
{
    $arr = array_reverse($arr, true);
    $arr[$key] = $val;
    $arr = array_reverse($arr, true);
    return $arr;
}
Anonymous 19-Nov-2011 06:44
Sahn's example almost works but has a small error. Try it like this if you need to prepend something to the array without the keys being reindexed and/or need to prepend a key value pair, you can use this short function:

<?php
function array_unshift_assoc(&$arr, $key, $val)
{
    $arr = array_reverse($arr, true);
    $arr[$key] = $val;
    return = array_reverse($arr, true);
}
?>
sergei at gmx dot net 04-Oct-2007 05:49
You can preserve keys and unshift an array with numerical indexes in a really simple way if you'll do the following:

<?php
$someArray=array(224=>'someword1', 228=>'someword2', 102=>'someword3', 544=>'someword3',95=>'someword4');

$someArray=array(100=>'Test Element 1 ',255=>'Test Element 2')+$someArray;
?>

now the array looks as follows:

array(
100=>'Test Element 1 ',
255=>'Test Element 2'
224=>'someword1',
228=>'someword2',
102=>'someword3',
544=>'someword3',
95=>'someword4'
);
amschroeder at gmail dot com 26-Mar-2007 06:13
This becomes a nice little problem if you index your arrays out of order (while manually sorting).  For example:

<?php
$recordMonths[3] = '8/%/2006';
$recordMonths[4] = '7/%/2004';
$recordMonths[0] = '3/%/2007';
$recordMonths[1] = '2/%/2007';
$recordMonths[5] = '12/%/2000';
$recordMonths[6] = '11/%/2000';
$recordMonths[7] = '10/%/2000';
$recordMonths[2] = '1/%/2007';

for($i = 0; $i < count($recordMonths); $i++)
{
    $singleMonth = $recordMonths[$i];
    echo "singleMonth: $singleMonth <br />";
}
array_unshift($recordMonths,'%');
for($i = 0; $i < count($recordMonths); $i++)
{
    $singleMonth = $recordMonths[$i];
    echo "singleMonth: $singleMonth <br />";
}
?>

Produces:

singleMonth: 3/%/2007
singleMonth: 2/%/2007
singleMonth: 1/%/2007
singleMonth: 8/%/2006
singleMonth: 7/%/2004
singleMonth: 12/%/2000
singleMonth: 11/%/2000
singleMonth: 10/%/2000
singleMonth: %
singleMonth: 8/%/2006
singleMonth: 7/%/2004
singleMonth: 3/%/2007
singleMonth: 2/%/2007
singleMonth: 12/%/2000
singleMonth: 11/%/2000
singleMonth: 10/%/2000
singleMonth: 1/%/2007

It reindexes them based on the order they were created.  It seems like if an array has all numeric indexes, then it should reindex them based on the order of their index.  Just my opinion...
John Brooking 03-Jun-2006 03:54
I had a need tonight to convert a numeric array from 1-based to 0-based, and found that the following worked just fine due to the "side effect" of renumbering:

<?php
   array_unshift( $myArray, array_shift( $myArray ));
?>
php at electricsurfer dot com 27-Feb-2004 03:20
even simpler unshifting of a reference !
<?php
/**
 * @return int
 * @param $array array
 * @param $value mixed
 * @desc Prepend a reference to an element to the beginning of an array. Renumbers numeric keys, so $value is always inserted to $array[0]
 */
function array_unshift_ref(&$array, &$value)
{
   $return = array_unshift($array,'');
   $array[0] =& $value;
   return $return;
}
?>
lagroue 09-Nov-2003 01:46
Last version of PHP deprecated unshifting of a reference.
You can use this function instead :

<?php
function array_unshift1 (& $ioArray, $iValueWrappedInAnArray) {
    $lNewArray = false;
    foreach (array_keys ($ioArray) as $lKey)
        $lNewArray[$lKey+1] = & $ioArray[$lKey];
    $ioArray = array (& $iValueWrappedInAnArray[0]);
    if ($lNewArray)
        foreach (array_keys ($lNewArray) as $lKey)
             $ioArray[] = & $lNewArray[$lKey];
    return count($ioArray);
}

// before last PHP (now generates a deprecation warning)
array_unshift ($a, &$v);
// since last PHP (caution, there is a wrapping array !!)
array_unshift1 ($a, array (&$v));
?>
chris dot NoThxSpam dot given at hp dot com 23-Jul-2003 09:17
If you need to change the name of a key without changing its position in the array this function may be useful.

<?php
function array_key_change($Old, $New, $In, $NewVal=NULL) {
        $Temp = array();
        while(isset($Temp[$Old]) == false) {
                list($k, $v) = each($In);
                $Temp[$k] = $v;
                unset($In[$k]);
        }
        if($NewVal == NULL) {
                $NewVal = $Temp[$Old];
        }
        unset($Temp[$Old]);
        $Temp = array_reverse($Temp);
        $In = array_merge(array($New=>$NewVal), $In);
        while(list($k,$v) = each($Temp)) {
                $In = array_merge(array($k=>$v), $In);
        }
        return($In);
}
?>
rsmith_NOSPAM_ at _NOSPAM_unitec dot ac dot nz 31-Jul-2002 04:00
array_merge() will also reindex (see array_merge() manual entry), but the '+' operator won't, so...

<?php
$arrayone=array("newkey"=>"newvalue") + $arrayone;
?>

does the job.
robert dot wills at fuzzbrain dot uklinux dot net 07-Feb-2002 03:02
Actually this problem with the keys getting reindexed only happens when the keys are numerical:

<?php

$a = array("f"=>"five", "s" =>"six", "t" =>
        "twenty");

print_r($a);
echo "\n";
foreach($a as $key=>$val)
{
    echo "k: $key v: $val \n";
}

array_unshift($a, "zero");
print_r($a);
echo "\n";
foreach($a as $key=>$val)
{
    echo "k: $key v: $val \n";
}
?>

Array
(
    [f] => five
    [s] => six
    [t] => twenty
)

k: f v: five
k: s v: six
k: t v: twenty
Array
(
    [0] => zero
    [f] => five
    [s] => six
    [t] => twenty
)

k: 0 v: zero
k: f v: five
k: s v: six
k: t v: twenty
sahn at hmc dot edu 27-Jul-2001 09:21
If you need to prepend something to the array without the keys being reindexed and/or need to prepend a key value pair, you can use this short function:

<?php
function array_unshift_assoc(&$arr, $key, $val)
{
    $arr = array_reverse($arr, true);
    $arr[$key] = $val;
    $arr = array_reverse($arr, true);
    return count($arr);
}
?>


배열의 모든 값을 반환

  • ≪ array_unshift
  • array_walk_recursive ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 모든 값을 반환

array_values

(PHP 4, PHP 5, PHP 7)

array_values — 배열의 모든 값을 반환

설명

array array_values ( array $input )

array_values()는 input 배열에서 모든 값들과 숫자로된 인덱스의 배열을 반환한다.

인수

input

배열.

반환값

값의 인덱스 배열을 반환합니다.

예제

Example #1 array_values() 예제

<?php
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
?>

위 예제의 출력:

Array
(
    [0] => XL
    [1] => gold
)

참고

  • array_keys() - 배열의 모든 키를 반환

User Contributed Notes

biziclop at vipmail dot hu 01-May-2014 08:47
Remember, array_values() will ignore your beautiful numeric indexes, it will renumber them according tho the 'foreach' ordering:

<?php
$a = array(
 3 => 11,
 1 => 22,
 2 => 33,
);
$a[0] = 44;

print_r( array_values( $a ));
==>
Array(
  [0] => 11
  [1] => 22
  [2] => 33
  [3] => 44
)
?>
heber dot gentilin at gmail dot com 22-Aug-2013 03:43
/**
* Return the new array from the offset index
*/
function array_values_from($array, $offset_index = 0) {
    if (!is_array($array))
        return null;
    $index = (int)$offset_index;
    foreach($array as $i => $value)
        $array_return[$index++] = $value;
    return $array_return;
}
Certainist 22-Apr-2013 12:51
Non-recursive simplest array_flatten.

<?php

function array_flatten($arr) {
    $arr = array_values($arr);
    while (list($k,$v)=each($arr)) {
        if (is_array($v)) {
            array_splice($arr,$k,1,$v);
            next($arr);
        }
    }
    return $arr;
}

?>
info at djdb dot be 29-Jan-2013 09:45
<?php
$array = array("size" => "XL", "color" => "gold","x" => " ","y" => "gold","z" => "");
print_r(array_values($array));
?>
wil result:
Array ( [0] => XL [1] => gold [2] => [3] => gold [4] => )
kapilgopinath at gmail dot com 09-Mar-2012 03:57
extract all values from a multi dimesnsional array or a nexted json object
function array_keys_multi($array,&$vals)
{
    foreach ($array as $key => $value) {

        if (is_array($value)) {

            array_keys_multi($value,$vals);

        }else{

            $vals[] = $value;
        }
    }

    return $vals;
}
a dot ross at amdev dot eu 30-May-2011 09:21
<?php
/**
 * Flattens an array, or returns FALSE on fail.
 */
function array_flatten($array) {
  if (!is_array($array)) {
    return FALSE;
  }
  $result = array();
  foreach ($array as $key => $value) {
    if (is_array($value)) {
      $result = array_merge($result, array_flatten($value));
    }
    else {
      $result[$key] = $value;
    }
  }
  return $result;
}
?>
abimaelrc 10-May-2011 02:39
This is another way to get value from a multidimensional array, but for versions of php >= 5.3.x
<?php
/**
 * Get all values from specific key in a multidimensional array
 *
 * @param $key string
 * @param $arr array
 * @return null|string|array
 */
function array_value_recursive($key, array $arr){
    $val = array();
    array_walk_recursive($arr, function($v, $k) use($key, &$val){
        if($k == $key) array_push($val, $v);
    });
    return count($val) > 1 ? $val : array_pop($val);
}

$arr = array(
    'foo' => 'foo',
    'bar' => array(
        'baz' => 'baz',
        'candy' => 'candy',
        'vegetable' => array(
            'carrot' => 'carrot',
        )
    ),
    'vegetable' => array(
        'carrot' => 'carrot2',
    ),
    'fruits' => 'fruits',
);

var_dump(array_value_recursive('carrot', $arr)); // array(2) { [0]=> string(6) "carrot" [1]=> string(7) "carrot2" }
var_dump(array_value_recursive('apple', $arr)); // null
var_dump(array_value_recursive('baz', $arr)); // string(3) "baz"
var_dump(array_value_recursive('candy', $arr)); // string(5) "candy"
var_dump(array_value_recursive('pear', $arr)); // null
?>
aowie1 at gmail dot com 19-Jan-2011 09:23
I needed a function that recursively went into each level of the array to order (only the indexed) arrays... and NOT flatten the whole thing.

Solution:
<?php
function array_values_recursive($arr){
        $arr = array_values($arr);
        foreach($arr as $key => $val)
            if(array_values($val) === $val)
                $arr[$key] = array_values_recursive($val);

        return $arr;
}
?>
geo dot artemenko at gmail dot com 04-May-2010 09:37
same array_flatten function, compressed and preserving keys.

function array_flatten($a,$f=array()){
  if(!$a||!is_array($a))return '';
  foreach($a as $k=>$v){
    if(is_array($v))$f=array_flatten($v,$f);
    else $f[$k]=$v;
  }
  return $f;
}
Carsten Milkau 01-Nov-2009 01:46
Note that in a multidimensional array, each element may be identified by a _sequence_ of keys, i.e. the keys that lead towards that element. Thus "preserving keys" may have different interpretations. Ivan's function for example creates a two-dimensional array preserving the last two keys. Other functions below create a one-dimensional array preserving the last key. For completeness, I will add a function that merges the key sequence by a given separator and a function that preserves the last n keys, where n is arbitrary.

<?php
/*
 * Flattening a multi-dimensional array into a
 * single-dimensional one. The resulting keys are a
 * string-separated list of the original keys:
 *
 * a[x][y][z] becomes a[implode(sep, array(x,y,z))]
 */

function array_flatten_sep($sep, $array) {
  $result = array();
  $stack = array();
  array_push($stack, array("", $array));

  while (count($stack) > 0) {
    list($prefix, $array) = array_pop($stack);

    foreach ($array as $key => $value) {
      $new_key = $prefix . strval($key);

      if (is_array($value))
        array_push($stack, array($new_key . $sep, $value));
      else
        $result[$new_key] = $value
    }
  }

  return $result;
}

/*
 * Flattening a multi-dimensional array into an
 * n-dimensional one. The last n keys of each element are
 * preserved. If this results in ambiguities, results are
 * undefined.
 *
 * a[x_1][x_2]...[x_m]  becomes  a[x_{m-n+1}]...[x_m]
 */
function array_flatten_n($array, $n) {
  $result = array();
  $stack = array();
  array_push($stack, array(array(), $array));

  while (count($stack) > 0) {
    list($prefix, $array) = array_pop($stack);

    foreach ($array as $key => $value) {
      if (is_array($value)) {
        $new_prefix = array_values($prefix);
        array_push($new_prefix, $key);
        if (count($new_prefix) >= n)
          array_shift($new_prefix);

        array_push($stack, array($new_prefix, $value));
      } else {
        $array = $result;
        foreach ($prefix as $pkey) {
           if (!is_array($array[$pkey]))
             $array[$pkey] = array();
           $array = $array[$pkey];
        }
        $array[$key] = $value;
      }
    }
  }

  return $result;
}
?>
karl dot rixon at gmail dot com 18-Sep-2009 11:57
A modification of wellandpower at hotmail.com's function to perform array_values recursively. This version will only re-index numeric keys, leaving associative array indexes alone.

<?php

function array_values_recursive($array) {
    $temp = array();
    foreach ($array as $key => $value) {
        if (is_numeric($key)) {
            $temp[] = is_array($value) ? array_values_recursive($value) : $value;
        } else {
            $temp[$key] = is_array($value) ? array_values_recursive($value) : $value;
        }
    }
    return $temp;
}

?>
chrysb at gmail dot com 24-Sep-2008 09:34
If you are looking for a way to count the total number of times a specific value appears in array, use this function:

<?php
function array_value_count ($match, $array)
{
    $count = 0;

    foreach ($array as $key => $value)
    {
        if ($value == $match)
        {
            $count++;
        }
    }

    return $count;
}
?>

This should really be a native function of PHP.
madhamster 12-Jun-2008 01:18
Good function, if you want to acces associative array element by position:

<?php

$array = array('fruit'=>'apple', 'juice'=>'orange', 'color'=>'lime');

$array = array_values($array);

echo $array[2];

?>
rene dot zak at post dot cz 24-Apr-2008 10:05
<?php
$array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => ' ',
    'fruit4' => ' ',
    'fruit5' => 'apple');

    for ($i = 0; $i <  count($array); $i++) {
    $key=key($array);
    $val=$array[$key];
    if ($val<> ' ') {
       echo $key ." = ".  $val ." <br> ";
       }
     next($array);
    }

/*
fruit1 = apple
fruit2 = orange
fruit5 = apple
*/
?>
bluej100 at gmail dot com 07-Sep-2007 07:10
Most of the array_flatten functions don't allow preservation of keys. Mine allows preserve, don't preserve, and preserve only strings (default).

<?
// recursively reduces deep arrays to single-dimensional arrays
// $preserve_keys: (0=>never, 1=>strings, 2=>always)
function array_flatten($array, $preserve_keys = 1, &$newArray = Array()) {
  foreach ($array as $key => $child) {
    if (is_array($child)) {
      $newArray =& array_flatten($child, $preserve_keys, $newArray);
    } elseif ($preserve_keys + is_string($key) > 1) {
      $newArray[$key] = $child;
    } else {
      $newArray[] = $child;
    }
  }
  return $newArray;
}

// Tests

$array = Array(
  'A' => Array(
    1 => 'foo',
    2 => Array(
      'a' => 'bar'
    )
  ),
  'B' => 'baz'
);

echo 'var_dump($array);'."\n";
var_dump($array);
echo 'var_dump(array_flatten($array, 0));'."\n";
var_dump(array_flatten($array, 0));
echo 'var_dump(array_flatten($array, 1));'."\n";
var_dump(array_flatten($array, 1));
echo 'var_dump(array_flatten($array, 2));'."\n";
var_dump(array_flatten($array, 2));
?>
deceze at gmail dot YesThatsGoogleMail dot com 03-Sep-2007 09:31
Please note that 'wellandpower at hotmail.com's recursive merge doesn't work. Here's the fixed version:

<?php

function array_values_recursive($array) {
   $flat = array();

   foreach ($array as $value) {
           if (is_array($value)) $flat = array_merge($flat, array_values_recursive($value));
           else $flat[] = $value;
   }
   return $flat;
}

?>
warmo_at_o2_dot_pl 22-Mar-2007 07:42
@Yassin Ezbakhe <yassin88 at gmail dot com>
When we have to flatten multidimensional array of strings or numbers this method could be much faster.
Inconvenience of this method is, that its speed depends on size of strings/numbers, which array contains - bigger strings, lower efficiency.
Conclusion: Use this method for small amount of data in arrays (less than 500B per element in my case) which have many dimensions, in other case, use Yassin Ezbakhe method.

<?php

function md_implode($array, $glue = '')
{
    if (is_array ($array))
    {
        $output = '';
        foreach ($array as $v)
        {
            $output .= md_implode($v, $glue);
        }
        return $output;
    }
    else
    {
        return $array.$glue;
    }
}

function md_array_flatten($md_array)
{
    $flat_array = explode ('#|#',md_implode($md_array,'#|#')); // "#|#" is a sample delimiter
    array_pop($flat_array); // to remove last empty element
    return $flat_array;
}

//Usage:
$flat_array = md_array_flatten($some_md_array)

?>
ahigerd at stratitec dot com 24-Jan-2007 03:07
A comment on array_merge mentioned that array_splice is faster than array_merge for inserting values. This may be the case, but if your goal is instead to reindex a numeric array, array_values() is the function of choice. Performing the following functions in a 100,000-iteration loop gave me the following times: ($b is a 3-element array)

array_splice($b, count($b)) => 0.410652
$b = array_splice($b, 0) => 0.272513
array_splice($b, 3) => 0.26529
$b = array_merge($b) => 0.233582
$b = array_values($b) => 0.151298
wellandpower at hotmail.com 29-Aug-2006 02:56
The function here flatterns an entire array and was not the behaviour I expected from a function of this name.

I expected the function to flattern every sub array so that all the values were aligned and it would return an array with the same dimensions as the imput array, but as per array_values() adjusting the keys rater than removing them.

In order to do this, you will want this function:

function array_values_recursive($array) {
   $temp = array();

   foreach ($array as $value) {
           if(is_array($value)) { $temp[] = array_values_recursive($value); }
           else { $temp[] = $value; }
   }
   return $temp;
}

Hopefully this will assist.
wizglins at gmx dot ch 13-Apr-2006 09:21
In case you want to replace all keys in multiarrays by integers starting at 0, the following function might help.

<?php

function numerieren($array)
{
$array_v = array_values($array);
$count_v = count($array_v);

for ($i=0; $i<$count_v; $i++)
  if (is_array($array_v[$i]))
    $array_v[$i] = numerieren($array_v[$i]);
return $array_v;
}

?>
Yassin Ezbakhe <yassin88 at gmail dot com> 01-Sep-2005 01:28
<?php
/**********************************************
 *
 *   PURPOSE: Flatten a deep multidimensional array into a list of its
 *   scalar values
 *
 *   array array_values_recursive (array array)
 *
 *   WARNING: Array keys will be lost
 *
 *********************************************/

function array_values_recursive($array)
{
    $arrayValues = array();

    foreach ($array as $value)
    {
        if (is_scalar($value) OR is_resource($value))
        {
             $arrayValues[] = $value;
        }
        elseif (is_array($value))
        {
             $arrayValues = array_merge($arrayValues, array_values_recursive($value));
        }
    }

    return $arrayValues;
}
?>

This function is an improved and faster version of the one posted by <27-Apr-2004 09:47>
28-Apr-2004 03:47
<?php
/**
   flatten an arbitrarily deep multidimensional array
   into a list of its scalar values
   (may be inefficient for large structures)
   (will infinite recurse on self-referential structures)
   (could be extended to handle objects)
*/
function array_values_recursive($ary)
{
   $lst = array();
   foreach( array_keys($ary) as $k ){
      $v = $ary[$k];
      if (is_scalar($v)) {
         $lst[] = $v;
      } elseif (is_array($v)) {
         $lst = array_merge( $lst,
            array_values_recursive($v)
         );
      }
   }
   return $lst;
}
?>

code till dawn!  -mark meves!
nopy at users dot sourceforge dot net 24-Oct-2003 12:36
Just a warning that re-indexing an array by array_values() may cause you to reach the memory limit unexpectly.

For example, if your PHP momory_limits is 8MB,
 and says there's a BIG array $bigArray which allocate 5MB of memory.

Doing this will cause PHP exceeds the momory limits:

<?php
  $bigArray = array_values( $bigArray );
?>

It's because array_values() does not re-index $bigArray directly,
it just re-index it into another array, and assign to itself later.
mailseppel at gmx dot de 04-Oct-2002 01:10
Remember, that the following way of fetching data from a mySql-Table will do exactly the thing as carl described before: An array, which data may be accessed both by numerical and DB-ID-based Indexes:

<?php
$row = mysql_fetch_array($db_result, $db_link);
?>

Hope I haven't misunderstood anything here.. :)
carl at thep.lu.se 30-Jan-2002 12:59
Indeed you can, and that's what's so great about it. I have, for instance, a function that returns the results of a database query as an array. I want to keep the order that the entries were returned in, but at the same time I want to be able to access them _either_ by the position _or_ by some other index (such as some sort of ID in the database, gotten from elsewhere). In this case, I can make the function return an array from id to [array of values], and by a simple call to array_values() this is transformed into an array indexed from 0 to count()-1. Useful.


Apply a user function recursively to every member of an array

  • ≪ array_values
  • array_walk ≫
  • PHP Manual
  • 배열 함수 목록
  • Apply a user function recursively to every member of an array

array_walk_recursive

(PHP 5, PHP 7)

array_walk_recursive — Apply a user function recursively to every member of an array

설명

bool array_walk_recursive ( array &$array , callable $callback [, mixed $userdata = NULL ] )

Applies the user-defined callback function to each element of the array. This function will recurse into deeper arrays.

인수

array

The input array.

callback

Typically, callback takes on two parameters. The array parameter's value being the first, and the key/index second.

Note:

If callback needs to be working with the actual values of the array, specify the first parameter of callback as a reference. Then, any changes made to those elements will be made in the original array itself.

userdata

If the optional userdata parameter is supplied, it will be passed as the third parameter to the callback.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 array_walk_recursive() example

<?php
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');

function test_print($item, $key)
{
    echo "$key holds $item\n";
}

array_walk_recursive($fruits, 'test_print');
?>

위 예제의 출력:

a holds apple
b holds banana
sour holds lemon

You may notice that the key 'sweet' is never displayed. Any key that holds an array will not be passed to the function.

참고

  • array_walk() - 배열의 각 원소에 대해서 특정 함수를 적용
  • callback형에 대한 정보

User Contributed Notes

KH 02-Nov-2016 02:43
Example for the use of $userdata:

$userdata is the third parameter of the callback function.
The first parameter is $item, the second is $key, and the third one is $userdata.

<?php
$sweet = array('a' => 'Apfel', 'b' => 'Banane');
$fruits = array('suß' => $sweet, 'sauer' => 'Zitrone');

function test_print($item, $key, $info)
{
    echo "$key beinhaltet $item $info\n";
}

array_walk_recursive($fruits, 'test_print', '  ... Bio');
?>

generates

a beinhaltet Apfel ... Bio
b beinhaltet Banane ... Bio
sauer beinhaltet Zitrone ... Bio
php at genjo dot fr 14-Mar-2015 02:33
I use RecursiveIteratorIterator with parameter CATCH_GET_CHILD to iterate on leafs AND nodes instead of array_walk_recursive function :

<?php
// Iteration on leafs AND nodes
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($candidate), RecursiveIteratorIterator::CATCH_GET_CHILD) as $key => $value) {
    echo 'My node ' . $key . ' with value ' . $value . PHP_EOL;
}
?>
robin leffmann 28-Jan-2015 12:48
A simple solution for walking a nested array to obtain the last set value of a specified key:

<?php

$key = 'blah';
$val = null;
array_walk_recursive( $your_array,
                      function($v, $k, $u) { if($k === $u[0]) $u[1] = $v; },
                      [$key ,&$val] );

echo "$key = $val";

?>
gk at anuary dot com 08-Mar-2014 06:29
array_walk_recursive itself cannot unset values. Even though you can pass array by reference, unsetting the value in the callback will only unset the variable in that scope.

<?php
/**
 * http://uk1.php.net/array_walk_recursive implementation that is used to remove nodes from the array.
 *
 * @param array The input array.
 * @param callable $callback Function must return boolean value indicating whether to remove the node.
 * @return array
 */
function walk_recursive_remove (array $array, callable $callback) {
    foreach ($array as $k => $v) {
        if (is_array($v)) {
            $array[$k] = walk_recursive_remove($v, $callback);
        } else {
            if ($callback($v, $k)) {
                unset($array[$k]);
            }
        }
    }

    return $array;
}
?>

An up to date implementation of the above function can be looked up from https://github.com/gajus/marray/blob/master/src/marray.php#L116.
Anonymous 21-Nov-2013 07:42
since PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.
Rodrigo Guariento 23-Aug-2013 10:27
Simple array_walk_recursive:

// var example
$myArray = Array(
  Array('keyA1' => '    textA1 ', 'keyA2' => '  textA2     '),
  Array('keyB1' => '    textB1 ', 'sub' =>
        Array('keyB1_sub1' => '      textB1_sub1   '),
          Array('keyB1_sub2' => '      textB1_sub2   ')
      ),
  Array('keyC1' => '    textC1 ', 'keyC2' => '  textC2     '),
  Array('keyD1' => '    textD1 ', 'keyD2' => '  textD2     '),
  Array('keyE1' => '    textE1 ', 'keyE2' => '  textE2     ')
);

// function for "trim" (or your function, use same structure)
function trimming($data) {
  if (gettype($data) == 'array')
    return array_map("trimming", $data);
  else
    return trim($data);
}

// get array
$myArray = array_map("trimming", $myArray);

// show array trimmed
var_dump($myArray);

/*
RESULT

array (size=5)
  0 =>
    array (size=2)
      'keyA1' => string 'textA1' (length=6)
      'keyA2' => string 'textA2' (length=6)
  1 =>
    array (size=3)
      'keyB1' => string 'textB1' (length=6)
      'sub' =>
        array (size=1)
          'keyB1_sub1' => string 'textB1_sub1' (length=11)
      0 =>
        array (size=1)
          'keyB1_sub2' => string 'textB1_sub2' (length=11)
  2 =>
    array (size=2)
      'keyC1' => string 'textC1' (length=6)
      'keyC2' => string 'textC2' (length=6)
  3 =>
    array (size=2)
      'keyD1' => string 'textD1' (length=6)
      'keyD2' => string 'textD2' (length=6)
  4 =>
    array (size=2)
      'keyE1' => string 'textE1' (length=6)
      'keyE2' => string 'textE2' (length=6)

*/
none at of dot your dot biz 03-Jun-2013 07:50
Since this is only mentioned in the footnote of the output of one of the examples, I feel it should be spelled out:

* THIS FUNCTION ONLY VISITS LEAF NODES *

That is to say that if you have a tree of arrays with subarrays of subarrays, only the plain values at the leaves of the tree will be visited by the callback function.  The callback function isn't ever called for a nodes in the tree that subnodes (i.e., a subarray).  This has the effect as to make this function unusable for most practical situations.
chris at willowsconsulting dot ie 23-Oct-2012 07:11
To convert all values of an array in UTF8, do this:

<?php

function convert_before_json(&$item, &$key)
{
   $item=utf8_encode($item);
}

array_walk_recursive($your_array,"convert_before_json");

?>
cyranix at cyranix dot com 29-Oct-2011 04:45
I needed to add or modify values in an array with unknown structure. I was hoping to use array_walk_recursive for the task, but because I was also adding new nodes I came up with an alternate solution.

<?php

    /**
     * Sets key/value pairs at any depth on an array.
     * @param $data an array of key/value pairs to be added/modified
     * @param $array the array to operate on
     */
    function setNodes($data, &$array)
    {
        $separator = '.'; // set this to any string that won't occur in your keys
        foreach ($data as $name => $value) {
            if (strpos($name, $separator) === false) {
                // If the array doesn't contain a special separator character, just set the key/value pair.
                // If $value is an array, you will of course set nested key/value pairs just fine.
                $array[$name] = $value;
            } else {
                // In this case we're trying to target a specific nested node without overwriting any other siblings/ancestors.
                // The node or its ancestors may not exist yet.
                $keys = explode($separator, $name);
                // Set the root of the tree.
                $opt_tree =& $array;
                // Start traversing the tree using the specified keys.
                while ($key = array_shift($keys)) {
                    // If there are more keys after the current one...
                    if ($keys) {
                        if (!isset($opt_tree[$key]) || !is_array($opt_tree[$key])) {
                            // Create this node if it doesn't already exist.
                            $opt_tree[$key] = array();
                        }
                        // Redefine the "root" of the tree to this node (assign by reference) then process the next key.
                        $opt_tree =& $opt_tree[$key];
                    } else {
                        // This is the last key to check, so assign the value.
                        $opt_tree[$key] = $value;
                    }
                }
            }
        }
    }

?>

Sample usage:

<?php

$x = array();
setNodes(array('foo' => 'bar', 'baz' => array('quux' => 42, 'hup' => 101)), $x);
print_r($x); // $x has the same structure as the first argument
setNodes(array('jif.snee' => 'hello world', 'baz.quux.wek' => 5), $x);
print_r($x); // added $x['jif']['snee'] and modified $x['baz']['quux'] to be array('wek' => 5)

?>
ghoffman at salientdigital dot com 14-Oct-2011 07:29
If you are wanting to change the values of an existing multi-dimensional array, as it says above in the note, you need to specify the first argument as a reference. All that means is, be sure to precede the $item variable with an ampersand (&) as in the good_example below.

Unfortunately the PHP example given doesn't do this. It actually took me a while to figure out why my function wasn't changing the original array, even though I was passing by reference.

Here's the tip: Don't return any value from the function! Just change the value of $item that you passed in by reference. This is rather counter-intuitive since the vast majority of functions return a value.

<?php
// array_walk_recursive fails to change your array unless you pass by reference.
// Don't return values from your filter function, even though it's quite logical at a glance!
function bad_example($item,$key){
   if($key=='test'){
       return 'PHP Rocks';  // Don't do it
   }else{
      return $item;  // Don't do this either
   }
}

// array_walk_recursive pass-by-reference example
function good_example(&$item,$key){
   if($key=='test'){
        $item='PHP Rocks'; // Do This!
   }
}

$arr = array('a'=>'1','b'=>'2','test'=>'Replace This');

array_walk_recursive($arr,'bad_example');
var_dump($arr);
/**
 * no errors, but prints...
 * array('a'=>'1','b'=>'2','test'=>'Replace This');
 */

array_walk_recursive($arr,'good_example');
var_dump($arr);
/**
 * prints...
 * array('a'=>'1','b'=>'2','test'=>'PHP Rocks');
 */

?>

Returning a value from your function does work if you pass by reference and modify $item before you return, but you will eat up memory very, very fast if you try it, even on an example as small as the one here.

One other silly thing you might try first is something like this:

<?php
// Resist the urge to do this, it doesn't work.
$filtered = array_walk_recursive($unfiltered,'filter_function');
?>

Of course, $filtered is just TRUE afterwards, not the filtered results you were wanting. Oh, it ran your function recursively alright, but changed all the values in the local function scope only and returns a boolean as the documentation states.
rob at yurkowski dot net 26-Oct-2010 08:16
If you don't really particularly care about the keys of an array, you can capture all values quite simply:

<?php

$sample = array('dog' => 'woof', 'cat' => array('angry' => 'hiss', 'happy' => 'purr'), 'aardvark' => 'kssksskss');
$output = array();

// Push all $val onto $output.
array_walk_recursive($sample, create_function('$val, $key, $obj', 'array_push($obj, $val);'), &output);

// Printing
echo nl2br(print_r($output, true));

/*
* Array
* (
*  [0] => woof
*  [1] => hiss
*  [2] => purr
*  [3] => kssksskss
* )
*/
?>

[EDIT BY danbrown AT php DOT net: In a note added by 'FaustoFilho' on 17-MAY-2011, the following information was appended to this note.

[If you intend to use this] "function to extract the last key value of an array, don't forget to insert a currency sign ($) signal before '$output' at 7th line.

This value must be a variable, and if you forgot to assign this signal, your code won't work, displaying an error like this:

Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /path/to/script.php on line 7."]
bradbeattie at gmail dot com 27-Aug-2010 08:18
The description says "If funcname needs to be working with the actual values of the array, specify the first parameter of funcname as a reference." This isn't necessarily helpful as the function you're calling might be built in (e.g. trim or strip_tags). One option would be to create a version of these like so.

<?php
    function trim_by_reference(&$string) {
        $string = trim($string);
    }
?>

The downside to this approach is that you need to create a wrapper function for each function you might want to call. Instead, we can use PHP 5.3's inline function syntax to create a new version of array_walk_recursive.

<?php
    /**
     * This function acts exactly like array_walk_recursive, except that it pretends that the function
     * its calling replaces the value with its result.
     *
     * @param $array The first value of the array will be passed into $function as the primary argument
     * @param $function The function to be called on each element in the array, recursively
     * @param $parameters An optional array of the additional parameters to be appeneded to the function
     *
     * Example usage to alter $array to get the second, third and fourth character from each value
     *     array_walk_recursive_referential($array, "substr", array("1","3"));
     */
    function array_walk_recursive_referential(&$array, $function, $parameters = array()) {
        $reference_function = function(&$value, $key, $userdata) {
            $parameters = array_merge(array($value), $userdata[1]);
            $value = call_user_func_array($userdata[0], $parameters);
        };
        array_walk_recursive($array, $reference_function, array($function, $parameters));
    }
?>

The advantage here is that we only explicitly define one wrapper function instead of potentially dozens.
amoffat at amoffat dot com 05-Jun-2008 02:15
<?
function my_array_map() {
    $args = func_get_args();
    $arr = array_shift($args);

    foreach ($args as $fn) {
        $nfn = create_function('&$v, $k, $fn', '$v = $fn($v);');
        array_walk_recursive($arr, $nfn, $fn);
    }
    return $arr;
}
?>

takes an array as the first argument, and functions as the other arguments.  it applies those functions recursively to the array
JW 15-Mar-2008 02:59
This function has a serious bug, which is still not fixed as of the PHP 5.2.5 release. After you call it, it can accidentally modify your original array. Save yourself hours of frustration by reading on.

The bug is here: http://bugs.php.net/bug.php?id=42850, and it looks like it will be fixed for 5.3.

If the array that you walk contains other array elements, they will be turned into references. This will happen even if the callback function doesn't take its first argument by reference, and doesn't do anything to the values.

For example, try this:
<?php
$data = array ('key1' => 'val1', 'key2' => array('key3' => 'val3'));
function foo($item, $key){}
var_dump($data);
?>

The original array has no references. Now try this:
<?php
array_walk_recursive($data,'foo');
var_dump($data);
?>

Now key2 is a reference, not just an array. So if you do this:
<?php
function test($item){$item['key2'] = array();}
test($data);
var_dump($data);
?>

you will see that test modifies $data, even though it shouldn't.

One workaround is to immediately make a deep copy of the array after calling array_walk_recursive, like this:
<?php
function array_duplicate($input) {
  if (!is_array($input)) return $input;
  $output = array();
  foreach ($input as $key => $value) {
    $output[$key] = array_duplicate($value);
  }
  return $output;
}
array_walk_recursive($data,'foo');
$data = array_duplicate($data);
var_dump($data);
?>

After doing that, the references are gone.
ik at paulkaspers dot nl 15-Oct-2006 10:46
To egingell at sisna dot com:

There is a small bug in your function, the following line should be changed:
From: if ($value != $saved_value || $saved_key != $key) {
Into: if ($value !== $saved_value || $saved_key !== $key) {

It's a nice function, because I was searching for something to change the keys of a multiple dimension array.
gabrielu at hotmail dot com 21-Feb-2006 10:13
I decided to add to the previous PHP 4 compatible version of array_walk_recursive() so that it would work within a class and as a standalone function.  Both instances are handled by the following function which I modified from omega13a at sbcglobal dot net.

The following example is for usage within a class.  To use as a standalone function take it out of the class and rename it.  (Example: array_walk_recursive_2)

<?php
class A_Class {

function array_walk_recursive(&$input, $funcname, $userdata = '') {
  if(!function_exists('array_walk_recursive')) {
    if(!is_callable($funcname))
      return false;

    if(!is_array($input))
      return false;

    foreach($input as $key=>$value) {
      if(is_array($input[$key])) {
        if(isset($this)) {
          eval('$this->' . __FUNCTION__ . '($input[$key], $funcname, $userdata);');
        } else {
          if(@get_class($this))
            eval(get_class() . '::' . __FUNCTION__ . '($input[$key], $funcname, $userdata);');
          else
            eval(__FUNCTION__ . '($input[$key], $funcname, $userdata);');
        }
      } else {
        $saved_value = $value;

        if(is_array($funcname)) {
          $f = '';
          for($a=0; $a<count($funcname); $a++)
            if(is_object($funcname[$a])) {
              $f .= get_class($funcname[$a]);
            } else {
              if($a > 0)
                $f .= '::';
              $f .= $funcname[$a];
            }
          $f .= '($value, $key' . (!empty($userdata) ? ', $userdata' : '') . ');';
          eval($f);
        } else {
          if(!empty($userdata))
            $funcname($value, $key, $userdata);
          else
            $funcname($value, $key);
        }

        if($value != $saved_value)
          $input[$key] = $value;
      }
    }
    return true;
  } else {
    array_walk_recursive($input, $funcname, $userdata);
  }
}

function kv_addslashes(&$v, $k) {
  $v = addslashes($v);
}
}
?>

Usage:
<?php
$arr = array(
  'a' => '"Hello World"',
  'b' => "'Hello World'",
  'c' => "Hello 'Worl\"d",
  'd' => array(
    'A' => 'H"e"l"l"o" "W"o"r"l"d'
    )
  );

$class = new A_Class();
$class->array_walk_recursive($arr, array(&$class, 'kv_addslashes'));
print_r($arr);
?>
omega13a at sbcglobal dot net 22-Dec-2005 01:21
This is a peice of code I wrote that appears to create this function for PHP 4.

<?php
if (!function_exists('array_walk_recursive'))
{
    function array_walk_recursive(&$input, $funcname, $userdata = "")
    {
        if (!is_callable($funcname))
        {
            return false;
        }

        if (!is_array($input))
        {
            return false;
        }

        foreach ($input AS $key => $value)
        {
            if (is_array($input[$key]))
            {
                array_walk_recursive($input[$key], $funcname, $userdata);
            }
            else
            {
                $saved_value = $value;
                if (!empty($userdata))
                {
                    $funcname($value, $key, $userdata);
                }
                else
                {
                    $funcname($value, $key);
                }

                if ($value != $saved_value)
                {
                    $input[$key] = $value;
                }
            }
        }
        return true;
    }
}
?>

Please note it is a conditionaly set function and will have to be put before any call to it.

If there is anything wrong with it, please email me.


배열의 각 원소에 대해서 특정 함수를 적용

  • ≪ array_walk_recursive
  • array ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 각 원소에 대해서 특정 함수를 적용

array_walk

(PHP 4, PHP 5, PHP 7)

array_walk — 배열의 각 원소에 대해서 특정 함수를 적용

설명

bool array_walk ( array &$array , callback $funcname [, mixed $userdata ] )

array 배열의 각 원소에 사용자 정의 함수 function을 적용한다.

array_walk()는 array의 내부적인 배열 포인터에 의해 영향을 받지 않는다. array_walk()는 포인터 위치에 상관없이 전체 배열를 가로지를것이다.

인수

array

입력 배열.

funcname

일반적으로, funcname는 두 인수를 받습니다. 첫번째는 array 인수의 값이고, 두번째는 키/인덱스입니다.

Note:

funcname이 배열의 실제 값으로 동작해야 한다면, funcname의 첫번째 인수를 참조로 설정합니다. 그러면 이러한 원소에 미치는 변경이 원래 배열에도 적용됩니다.

사용자가 콜백 함수에서 array 자체를 변경할 수 없습니다. 즉, 원소를 추가/삭제, 원소를 unset하는 등. array_walk()에 적용된 배열이 변경되면, 이 함수의 동작은 정의되지 않고, 예측할 수 없습니다.

userdata

선택적인 userdata 인수가 제공되면, funcname 콜백의 세번째 인수로 넘겨집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

오류/예외

funcname 함수가 주어진 것보다 더 많은 인수를 요구하면 array_walk()가 funcname을 호출할 때마다 E_WARNING 등급의 오류가 발생한다. 이런 경고는 array_walk() 호출에 PHP 오류 연산자 @를 덧붙이거나, error_reporting()을 사용해서 은폐할 수 있다.

변경점

버전 | 설명
4.0.0 | funcname에 key와 userdata를 전달하는 기능이 추가.

예제

Example #1 array_walk() 예제

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

function test_alter(&$item1, $key, $prefix)
{
    $item1 = "$prefix: $item1";
}

function test_print($item2, $key)
{
    echo "$key. $item2<br />\n";
}

echo "Before ...:\n";
array_walk($fruits, 'test_print');

array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";

array_walk($fruits, 'test_print');
?>

위 예제의 출력:

Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple

참고

  • array_walk_recursive() - Apply a user function recursively to every member of an array
  • iterator_apply() - Call a function for every element in an iterator
  • create_function() - Create an anonymous (lambda-style) function
  • list() - 배열처럼 변수에 할당
  • each() - 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진
  • call_user_func_array() - Call a callback with an array of parameters
  • array_map() - Applies the callback to the elements of the given arrays
  • callback형에 대한 정보
  • foreach

User Contributed Notes

fantomx1 at gmail dot com 23-Aug-2016 05:08
Since array_walk cannot modify / change / reindex keys as already mentioned, i provide this small wrapping function which accomplishes passing array reference and index using closures , "use" keyword.

function indexArrayByElement($array, $element)
{
    $arrayReindexed = [];
    array_walk(
        $array,
        function ($item, $key) use (&$arrayReindexed, $element) {
            $arrayReindexed[$item[$element]] = $item;
        }
    );
    return $arrayReindexed;
}
fred 30-Dec-2015 06:01
Correction for the speed test from zlobnygrif.

<?php
// Test results
$array1 = test('array_walk');
$array2 = test('array_walk_list_each');
$array3 = test('array_walk_foreach1');
$array4 = test('array_walk_foreach2');

// Check arrays for equal
var_dump($array1 == $array2, $array1 == $array3, $array1 == $array4);

// Test function 1
function array_walk_list_each(&$array, $function, $userData = null) {
   /* make sure we walk the array each time */
    reset($array);
    while ( list($key, $value) = each($array) )
        $function($array[$key], $key, $userData);
}

// Test function 2
function array_walk_foreach1(&$array, $function, $userData = null) {
    foreach ($array as $key => &$value )
        $function($value, $key, $userData);
}

// Test function 3
function array_walk_foreach2(&$array, $function, $userData = null) {
    foreach ($array as $key => $value )
        $function($array[$key], $key, $userData);
}

function some_function(&$value, $key, $userData) {
    $value = "$key => $userData";
}

function test($function, $count = 10000, $arrayElements = 1000) {
    echo $function, ' ... ';
    $array = array_fill(0, $arrayElements, "some text value");

    $timer = microtime(true);
    for( $i = 0; ++$i < $count; )
        /* change data for each $i */
        $function($array, 'some_function', 'some user data ' . $i);
    printf("%.3f sec\n", microtime(true) - $timer);

    return $array;
}
P-Body Germany 24-Oct-2014 08:52
/* You can use array_walk to secure your php-code and avoid it against php/html-injection. This sample secures incoming user-data: */

<?php
array_walk($_POST, 'secureInput');
array_walk($_GET, 'secureInput');
array_walk($_REQUEST, 'secureInput');
array_walk($_FILES, 'secureInput);

function secureInput(&$value, $key)
{
    $value = trim($value);
    // removes blank space at beginning an end
    $value = strip_tags($value);
    // removes all HTML- an PHP-commands
    }
?>

If you want to allow some HTML- or PHP-commands, use this:

    $value = strip_tags($value, '<p><br>');
 // removes all HTML- an PHP-commands, excepted <p></p> and <br /> Tags.

NOTE: it is important to add the "&" infront of $value in the function! Otherwhise the function won't remove the HTML- and PHP-code.
ezhacher at gmail dot com 27-Aug-2014 09:45
Calling an array Walk inside a class

If the class is static:
array_walk($array, array('self', 'walkFunction'));
or
array_walk($array, array('className', 'walkFunction'));

Otherwise:
array_walk($array, array($this, 'walkFunction'));
chaley at brtransport dot com 16-Jun-2014 09:46
There is a note about 3 years ago regarding using this for trimming. array_map() may be cleaner for this. I haven't checked the time/resource impact:

$result = array_map("trim", $array);
Arseny Nikonian 19-Mar-2014 09:38
If the project often change the datatype of the array.
Если в проекте нужно часто менять тип данных в массиве.
<?php
/**
 * Callback-функция (для array_walk) преобразует тип данных элементов массива.
 * @param mixed $item Значение элемента обрабатываемого массива.
 * @param int|string $key Ключ элемента массива.
 * @param string $type Тип данных, в который преобразуем элементы массива.
 */
function convertTypeValueArray(&$item, $key, $type)
{
    $arValidType = array("boolean", "bool", "integer", "int", "float", "string", "array", "object", "null");
    $type = strtolower($type);
    if(in_array($type, $arValidType))
    {
        settype($item, $type);
    }
}

$arTest = array("", 1, " 2", 3.1, "4.2", " 5 ");
array_walk($arTest, "convertTypeValueArray", "int");
var_dump($arTest); // array(6) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(5) }
?>
zlobnygrif at gmail dot com 17-Jul-2013 02:41
Some speed tests

<?php
// Test results
$array1 = test('array_walk');
$array2 = test('array_walk_list_each');
$array3 = test('array_walk_foreach1');
$array4 = test('array_walk_foreach2');

// Check arrays for equal
var_dump($array1 == $array2, $array1 == $array3, $array1 == $array4);

// Test function 1
function array_walk_list_each(&$array, $function, $userData = null) {
    while ( list($key, $value) = each($array) )
        $function($array[$key], $key, $userData);
}

// Test function 2
function array_walk_foreach1(&$array, $function, $userData = null) {
    foreach ($array as $key => &$value )
        $function($value, $key, $userData);
}

// Test function 3
function array_walk_foreach2(&$array, $function, $userData = null) {
    foreach ($array as $key => $value )
        $function($array[$key], $key, $userData);
}

function some_function(&$value, $key, $userData) {
    $value = "$key => $userData";
}

function test($function, $count = 10000, $arrayElements = 1000) {
    echo $function, ' ... ';
    $array = array_fill(0, $arrayElements, "some text value");

    $timer = microtime(true);
    for( $i = 0; ++$i < $count; )
        $function($array, 'some_function', 'some user data');
    printf("%.3f sec\n", microtime(true) - $timer);

    return $array;
}
?>

Output (PHP 5.4.9-4ubuntu2.2 (cli) (built: Jul 15 2013 18:24:39))
=========================
array_walk ... 13.572 sec
array_walk_list_each ... 0.027 sec
array_walk_foreach1 ... 15.356 sec
array_walk_foreach2 ... 17.416 sec
bool(true)
bool(true)
bool(true)

Output (PHP 5.5.0 (cli) (built: Jul 16 2013 17:59:42) - same server)
=========================
array_walk ... 4.776 sec
array_walk_list_each ... 0.006 sec
array_walk_foreach1 ... 4.482 sec
array_walk_foreach2 ... 5.166 sec
bool(true)
bool(true)
bool(true)

PHP 5.5 array_walk looks pretty good but list each is more and more quickly...
brian at access9 dot net 25-May-2013 07:03
array_walk does not work on SplFixedArray objects:
<?php
$array = new SplFixedArray(2);
$array[0] = 'test_1';
$array[1] = 'test_2';

array_walk($array, function(&$val){
    $val .= '__';
    return $val;
});
foreach ($array as $a) {
    echo "$a\n";
}
?>

result is:
test_1
test_2
gold[at]evolved.net.nz 28-Mar-2013 07:08
For all those people trying to shoe-horn trim() into array_walk() and have found all these tricks to work around the issue with array_walk() passing 2 parameters to the callback...

Check out array_map().

http://php.net/array_map

It's all sorts of win.

For the record. I'm one of these people and after 15 years of php development I'm pleased to say that there's still things I'm learning. :)  I just found out about array_map() myself...
espertalhao04 at hotmail dot com 26-Dec-2012 08:35
here is a simple and yet easy to use implementation of this function.
the 'original' function has the problem that you can't unset a value.
with my function, YOU CAN!

<?php
function array_walk_protected(&$a,$s,$p=null)
{
    if(!function_exists($s)||!is_array($a))
    {
        return false;
    }

    foreach($a as $k=>$v)
    {
        if(call_user_func_array($s,array(&$a[$k],$k,$p))===false)
        {
            unset($a[$k]);
        }
    }
}

function get_name(&$e,$i,$p)
{
    echo "$i: $e<br>";
    return false;
}

$m=array('d'=>'33','Y'=>55);

array_walk_protected($m,'get_name');

var_dump($m); //returns array(0) { }
?>

i called it array_walk_protected because it is protected against the unexpected behavior of unsetting the value with the original function.

to delete an element, simply return false!!!
nothing else is needed!
unsetting $e, under your created function, will keep the same array as-is, with no changes!

by the way, the function returns false if $a is not array or $s is not a string!

limitations: it only can run user defined functions.
i hope you like it!
selimacar at hotmail dot com 11-Jun-2012 10:34
####### I intuitively tried to use array_walk function with PHP built-in function "abs($var)" but it did not work and got some warning message

<?php
$values = array(-5,2,3,-8,4,-1,0);
array_walk($values, "abs");
?>

=> Warning: abs() expects exactly 1 parameter, 2 given in ...

###### Then I wrote this function :

<?php
function myAbs(&$val){
    $val =  abs($val);
}
array_walk($values, "myAbs");
?>

##### It works!
manuscle at gmail dot com 06-Dec-2011 04:16
example with closures, checking and deleting value in array:

<?php
$array = array('foo' => 'bar', 'baz' => 'bat');

array_walk($array, function($val,$key) use(&$array){
    if ($val == 'bar') {
        unset($array[$key]);
    }
});

var_dump($array);
alex_stanhope at hotmail dot com 26-Oct-2011 10:16
I wanted to walk an array and reverse map it into a second array.  I decided to use array_walk because it should be faster than a reset,next loop or foreach(x as &$y) loop.

<?php
$output = array();
array_walk($input, 'gmapmark_reverse', $output);

function gmapmark_reverse(&$item, $index, &$target) {
    $target[$item['form_key']] = $index;
}
?>

In my debugger I can see that $target is progressively updated, but when array_walk returns, $output is empty.  If however I use a (deprecated) call-by-reference:

<?php
array_walk($input, 'gmapmark_reverse', &$output);
?>

$output is returned correctly.  Unfortunately there's not an easy way to suppress the warnings:

<?php
@array_walk($input, 'gmapmark_reverse', &$output);
?>

doesn't silence them.  I've designed a workaround using a static array:

<?php
$reverse = array();
array_walk($input, 'gmapmark_reverse');
// call function one last time to get target array out, because parameters don't work
$reverse = gmapmark_reverse($reverse);

function gmapmark_reverse(&$item, $index = 0) {
  static $target;
  if (!$target) {
    $target = array();
  }
  if (isset($item['form_key'])) {
    $target[$item['form_key']] = $index;
  }
  return($target);
}
?>
tufanbarisyildirim at gmail dot com 05-Oct-2011 03:15
Filter an array by using key.

<?php
    $product_1 = 'test';
    $product_2 = 'test 2';

    function array_key_filter($array,$callback = 'trim')
    {
        $filtered = array();
        array_walk($array,function ($degeri,$degisken_adi) use (&$filtered,$callback)
        {
            if($callback($degisken_adi))
            {
                $filtered[$degisken_adi] =  $degeri;
            }
        });

        return $filtered;
    }

    #using

    $degiskenler = array_key_filter(get_defined_vars(),function($key)
    {
        return strpos($key,'product_') === 0;
    });

    print_r($degiskenler);
?>
output:

Array
(
    [product_1] => test
    [product_2] => test 2
)
Maxim 11-Jul-2011 09:23
Note that using array_walk with intval is inappropriate.
There are many examples on internet that suggest to use following code to safely escape $_POST arrays of integers:
<?php
array_walk($_POST['something'],'intval'); // does nothing in PHP 5.3.3
?>
It works in _some_ older PHP versions (5.2), but is against specifications. Since intval() does not modify it's arguments, but returns modified result, the code above has no effect on the array and will leave security hole in your website.

You can use following instead:
<?php
$_POST['something'] = array_map(intval,$_POST['something']);
?>
@jfredys 23-Mar-2011 02:07
I was looking for trimming all the elements in an array, I found this as the simplest solution:

<?php
array_walk($ids, create_function('&$val', '$val = trim($val);'));
?>
http://alex.moutonking.com/wordpress 16-Feb-2011 07:49
For completeness one has to mention the possibility of using this function with PHP 5.3 closures:

<?php
$names = array("D\'Artagnan", "Aramis", "Portos");
array_walk($names, function(&$n) {
  $n = stripslashes($n);
});
?>

The trap with array_walk being it doesn't return the array, instead it's modified by reference.
op_adept at yahoo dot co dot uk 02-Dec-2010 12:20
Prefix array values with keys and retrieve as a glued string, the original array remains unchanged. I used this to create some SQL queries from arrays.

<?php

function array_implode_prefix($outer_glue, $arr, $inner_glue, $prefix=false){
    array_walk( $arr , "prefix", array($inner_glue, $prefix) );
    return implode($outer_glue, $arr);
}

function prefix(&$value, $key, array $additional){
    $inner_glue = $additional[0];
    $prefix = isset($additional[1])? $additional[1] : false;
    if($prefix === false) $prefix = $key;

    $value = $prefix.$inner_glue.$value;
}

//Example 1:
$order_by = array("3"=>"ASC", "2"=>"DESC", "7"=>"ASC");
echo array_implode_prefix(",", $order_by, " ");
//Output: 3 ASC,2 DESC,7 ASC

//Example 2:
$columns = array("product_id", "category_id", "name", "description");
$table = "product";

echo array_implode_prefix(", ", $columns, ".", $table);
//Output:product.product_id, product.category_id, product.name, product.description

//Example 3 (function prefix) won't really be used on its own
$pre= "vacation";
$value = "lalaland";
prefix($value, $pre, array("."));
echo $value;

//Output: vacation.lalaland

?>
matthew at codenaked dot org 02-Sep-2010 08:24
Using lambdas you can create a handy zip function to zip together the keys and values of an array. I extended it to allow you to pass in the "glue" string as the optional userdata parameter. The following example is used to zip an array of email headers:

<?php

/**
 * Zip together the keys and values of an array using the provided glue
 *
 * The values of the array are replaced with the new computed value
 *
 * @param array $data
 * @param string $glue
 */
function zip(&$data, $glue=': ')
{
    if(!is_array($data)) {
        throw new InvalidArgumentException('First parameter must be an array');
    }

    array_walk($data, function(&$value, $key, $joinUsing) {
        $value = $key . $joinUsing . $value;
    }, $glue);
}

$myName = 'Matthew Purdon';
$myEmail = 'matthew@example.com';
$from = "$myName <$myEmail>";

$headers['From'] = $from;
$headers['Reply-To'] = $from;
$headers['Return-path'] = "<$myEmail>";
$headers['X-Mailer'] = "PHP" . phpversion() . "";
$headers['Content-Type'] = 'text/plain; charset="UTF-8"';

zip($headers);

$headers = implode("\n", $headers);
$headers .= "\n";

echo $headers;

/*
From: Matthew Purdon <matthew@example.com>
Reply-To: Matthew Purdon <matthew@example.com>
Return-path: <matthew@example.com>
X-Mailer: PHP5.3.2
Content-Type: text/plain; charset="UTF-8"
*/
?>
arekandrei at yandex dot ru 03-Jan-2010 04:30
You can use lambda function as a second parameter:

<?php
array_walk($myArray, function(&$value, $key){
    // if you want to change array values then "&" before the $value is mandatory.
});
?>

Example (multiply positive values by two):

<?php
$myArray = array(1, 2, 3, 4, 5);

array_walk($myArray, function(&$value, $index){
    if ($value > 0) $value *= 2;
});
?>
rustamabd at gmail dot com 13-Dec-2009 03:32
Don't forget about the array_map() function, it may be easier to use!

Here's how to lower-case all elements in an array:

<?php
    $arr = array_map('strtolower', $arr);
?>
erelsgl at gmail dot com 04-Mar-2009 12:40
If you want to unset elements from the callback function, maybe what you really need is array_filter.
jab_creations_-at_-yahoo_-dot-_com 27-Nov-2008 01:08
Unfortunately I spent a lot of time trying to permanently apply the effects of a function to an array using the array_walk function when instead array_map was what I wanted. Here is a very simple though effective example for those who may be getting overly frustrated with this function...

<?php
$fruits = array("Lemony & Fresh","Orange Twist","Apple Juice");

print_r($fruits);
echo '<br />';

function name_base($key)
{
 $name2 = str_replace(" ", "_", $key);
 $name3 = str_replace("&", "and", $name2);
 $name4 = strtolower($name3);
 echo $name4.'<br />';
 return $name4;
}
echo '<br />';

$test = array_map('name_base', $fruits);
$fruits_fixed = $test;
echo '<br />';
print_r($fruits_fixed);
?>
peterzuzek AT gmail DOT com 16-Nov-2008 06:04
I had some problems using this function - it didn't want to apply PHP-defined functions. So I decided to write my own - here it is. I had to use some generic-programming skills, didn't really checked the speed (I think it could be slow)... I believe it could be much better, but I don't know, how - well, I guess multiple array support and recursion would be nice. So?

Prototype:
bool arrayWalk(array &$arry, callback $callback, mixed $params=false)

<?php

    function arrayWalk(&$arry, $callback, $params=false) {
        $P=array(""); // parameters
        $a=""; // arguement string :)

        if($params !== false) { // add parameters
            if(is_array($params)) { // multiple additional parameters
                foreach($params as $par)
                    { $P[]=$par; }
            }
            else // just one additional
                { $P[]=$params; }
        }

        for( // create the arguement string
            $i=0; isset($P[$i]); ++$i
        )
            { $a.='$'.chr($i + 97).', '; } // random argument names

        $a=substr($a, 0, -2); // to get rid of the last comma and two spaces

        $func=create_function($a, 'return '.$callback.'('.$a.');'); // the generic function

        if(is_callable($func)) {
            for( // cycle through array
                $i=0; isset($arry[$i]); ++$i
            ) {
                $P[0]=$arry[$i]; // first element must be the first argument - array value
                $arry[$i] = call_user_func_array($func, $P); // assign the new value obtained by the generic function
            }
        }
        else
            { return false; } // failure - function not callable

        return true; // success!
    } // arrayWalk()

?>

One big problem I've noticed so far - for example, if you wanted to use str_replace on the array, you'd fail - simply because of the arguement order of str_replace, where the string modified is the third arguement, not the first as arrayWalk requires.

So, still some work left...
diyism 31-Oct-2008 08:18
When i pass the third parameter by reference in php5.2.5,
happened this: Warning: Call-time pass-by-reference has been deprecated - argument passed by value...

And to set allow_call_time_pass_reference to true in php.ini won't work, according to http://bugs.php.net/bug.php?id=19699   thus to work around:

<?php
array_walk($arrChnOut, create_function('&$v, $k, $arr_rtn', 'if ($k{0}!="_") {$arr_rtn[0]["_".$v[\'ID\']]=$v; unset($arr_rtn[0][$k]);}'), array(&$arrChnOut));
?>
mike >> mike AT debitage.com 06-Mar-2008 09:01
<?php
   /**
    *    class.ArrayTool.php lets you search an array based on key => value pairs
    *
    *   @version 1.0
    *   @ 1-11-2008
    *   @author Mike Volmar
    *
    *    Object for converting between array key and value
    *
    */

class ArrayTool {

    var $mydata = array();
    var $flag = 0;
    var $results;

    function ArrayTool(){

    }

    function tellAll(){
        print_r($this->mydata);
    }

    function setArray($data){
        $this->mydata = $data;
    }

    function getKey($input){
        foreach($this->mydata as $key => $value){
            if(($this->flag == 0)&&($input == $value)){
                $this->results = $key;
                $this->flag = 1;
            }
        }
        $this->flag = 0;
        return $this->results;
    }

    function getValue($input){
        foreach($this->mydata as $key => $value){
            if(($this->flag == 0)&&($input == $key)){
                $this->results = $value;
                $this->flag = 1;
            }
        }
        $this->flag = 0;
        return $this->results;
    }

}

?>
jerk at yoosic dot de 21-Dec-2006 05:05
if you want to modify every value of an multidimensional array use this function used here:

<?php

$array = array (1=>1, 2=> 2, 3 => array(1=>11, 2=>12, 3=>13));
$text = "test";

function modarr(&$array, $text) {
        foreach ($array as $key => $arr) {
                if(is_array($arr)) $res[$key] = modarr(&$arr,$text);
                // modification function here
                else $res[$key] = $arr.$text;
                }
        return $res;
}

$erg = modarr($array, $text);

print_r($erg);

?>

result will be_

<?php
Array ( [1] => 1test [2] => 2test [3] => Array ( [1] => 11test [2] => 12test [3] => 13test ) )
?>
nihaopaul at nihaopaul dot com 06-May-2006 12:59
no sure if this should go under array-walk but it does what i need, it searches a multidimensionial array by using an array to walk it, it either returns a value or an array.

<?php
function walker($walk, $array) {
    if (count($walk) >0) {
        foreach($array as $key => $value) {
            if ($key == $walk[0]) {
                if (is_array($value)) {
                    unset($walk[0]);
                    return walker(array_values($walk), $value);
                } else {
                    if (isset($value)) {
                        if (count($walk) == 1) {
                            return $value;
                        } else {
                            return 0;
                        }
                    } else {
                        return 0;
                    }
                }
            }
        }
        return 0;
    } else {
        return $array;
    }
}
?>
ludvig dot ericson at gmail dot com 21-Nov-2005 03:09
In response to 'ibolmo', this is an extended version of string_walk, allowing to pass userdata (like array_walk) and to have the function edit the string in the same manner as array_walk allows, note now though that you have to pass a variable, since PHP cannot pass string literals by reference (logically).

<?php
function string_walk(&$string, $funcname, $userdata = null) {
    for($i = 0; $i < strlen($string); $i++) {
        # NOTE: PHP's dereference sucks, we have to do this.
        $hack = $string{$i};
        call_user_func($funcname, &$hack, $i, $userdata);
        $string{$i} = $hack;
    }
}

function yourFunc($value, $position) {
    echo $value . ' ';
}

function yourOtherFunc(&$value, $position) {
    $value = str_rot13($value);
}

# NOTE: We now need this ugly $x = hack.
string_walk($x = 'interesting', 'yourFunc');
// Ouput: i n t e r e s t i n g

string_walk($x = 'interesting', 'yourOtherFunc');
echo $x;
// Output: vagrerfgvat
?>

Also note that calling str_rot13() directly on $x would be much faster ;-) just a sample.
ibolmo 18-Nov-2005 04:53
If anyone is interested to implement the array_walk functionality to a string. I've made this handy function. Note that this can be easily extended for any type of purpose. I've used this to convert from a string of bytes to a hex string then back from hex to a byte string.
<?php
function string_walk($string,$funcname)
{
    for($i=0;$i<strlen($string);$i++) {
        call_user_func($funcname,$string{$i});
    }
}

function yourFunc($val)
{
    echo $val.' ';
}

string_walk('interesting','yourFunc');
//ouput: i n t e r e s t i n g
?>
Andrzej Martynowicz at gmail dot com 19-Sep-2005 03:03
It can be very useful to pass the third (optional) parameter by reference while modifying it permanently in callback function. This will cause passing modified parameter to next iteration of array_walk(). The exaple below enumerates items in the array:

<?php
function enumerate( &$item1, $key, &$startNum ) {
   $item1 = $startNum++ ." $item1";
}

$num = 1;

$fruits = array( "lemon", "orange", "banana", "apple");
array_walk($fruits, 'enumerate', $num );

print_r( $fruits );

echo '$num is: '. $num ."\n";
?>

This outputs:

Array
(
    [0] => 1 lemon
    [1] => 2 orange
    [2] => 3 banana
    [3] => 4 apple
)
$num is: 1

Notice at the last line of output that outside of array_walk() the $num parameter has initial value of 1. This is because array_walk() does not take the third parameter by reference.. so what if we pass the reference as the optional parameter..

<?php
$num = 1;

$fruits = array( "lemon", "orange", "banana", "apple");
array_walk($fruits, 'enumerate', &$num ); // reference here

print_r( $fruits );

echo '$num is: '. $num ."\n";
echo "we've got ". ($num - 1) ." fruits in the basket!";
?>

This outputs:
Array
(
    [0] => 1 lemon
    [1] => 2 orange
    [2] => 3 banana
    [3] => 4 apple
)
$num is: 5
we've got 4 fruits in the basket!

Now $num has changed so we are able to count the items (without calling count() unnecessarily).

As a conclusion, using references with array_walk() can be powerful toy but this should be done carefully since modifying third parameter outside the array_walk() is not always what we want.
el_porno at web dot de 27-May-2005 12:03
You want to get rid of the whitespaces users add in your form fields...?
Simply use...:

class SomeVeryImportantClass
{
...
    public function mungeFormData(&$data)
    {
        array_walk($data, array($this, 'munge'));
    }

    private function munge(&$value, &$key)
    {
        if(is_array($value))
        {
            $this->mungeFormData($value);
        }
        else
        {
            $value = trim($value);
        }
    }
...
}

so...
$obj = new SomeVeryImportantClass;
$obj->mungeFormData($_POST);
___
eNc
Enlightened One 08-Apr-2005 08:17
Beware that "array ($this, method)" construct. If you're wanting to alter members of the "$this" object inside "method" you should construct the callback like this:

  $callback[] = &$this;
  $callback[] = method;
  array_walk ($input, $callback);

Creating your callback using the array() method as suggested by "appletalk" results in a copy of $this being passed to method, not the original object, therefor any changes made to the object by method will be lost when array_walk() returns. While you could construct the callback with "array(&$this, method)", I believe this relies on the deprecated runtime pass-by-reference mechanism which may be removed in future releases of PHP. Better to not create a dependence on that feature now than having to track it down and fix it in the future.
05-Nov-2004 08:22
If array_walk_recursive() is not present and you want to apply htmlentities() on each array element you can use this:

<?php
function array_htmlentities(&$elem)
{
  if (!is_array($elem))
  {
    $elem=htmlentities($elem);
  }
  else
  {
    foreach ($elem as $key=>$value)
      $elem[$key]=array_htmlentities($value);
  }
  return $elem;
} // array_htmlentities()
?>

If you want to output an array with print_r() and you have html in it this function is very helpful.
bisqwit at iki dot fi 04-Sep-2004 01:54
It's worth nothing that array_walk can not be used to change keys in the array.
The function may be defined as (&$value, $key) but not (&$value, &$key).
Even though PHP does not complain/warn, it does not modify the key.


배열 생성

  • ≪ array_walk
  • arsort ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열 생성

array

(PHP 4, PHP 5, PHP 7)

array — 배열 생성

설명

array array ([ mixed $... ] )

배열을 생성합니다. 배열이 무엇인지에 관한 자세한 정보는 배열형 섹션을 읽어보십시오.

인수

...

"index => values" 구문은 콤마(,)로 구분되고, 인덱스와 값을 정의한다. 인덱스는 문자열이나 숫자형이 될수 있다. 인덱스가 생략되면, 0부터 시작되는 정수 인덱스가 자동적으로 생성된다. 인덱스가 정수이면, 다음에 생성되는 인덱스는 더 큰 정수 index + 1 이 될것이다. 두개의 동일한 인덱스가 생성되었을 경우, 마지막 값이 처음 값을 덮어쓰게 된다는 것에 주의한다.

마지막 배열 원소 뒤에 콤마를 붙이는 것은 일반적이지 않지만, 유효한 문법입니다.

반환값

인수들의 배열을 반환합니다. 인수는 => 연산자로 인덱스를 지정할 수 있습니다. 배열이 무엇인지에 관해서는 배열형 섹션을 읽어보십시오.

예제

다음 예제 코드는 2차원 배열을 생성하는 방법, 연관배열에 대한 키를 설정하는 방법, 일반적인 배열의 숫자 인덱스를 건너뛰는 방법을 보여준다.

Example #1 array() 예제

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

Example #2 자동 인덱스 array()

<?php
$array = array(1, 1, 1, 1,  1, 8 => 1,  4 => 1, 19, 3 => 13);
print_r($array);
?>

위 예제의 출력:

Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 13
    [4] => 1
    [8] => 1
    [9] => 19
)

인덱스 '3'이 두번 정의되었고, 마지막 값인 13이 유지된다는 것에 유의한다. 인덱스 4는 인덱스 8 이후에 정의되었고, 더 큰 인덱스가 8이기 때문에, 다음에 생성된 인덱스( 값 19)는 9가 된다.

다음 예제는 1-기반 배열을 생성합니다.

Example #3 1-기반 인덱스 array()

<?php
$firstquarter  = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
?>

위 예제의 출력:

Array
(
    [1] => January
    [2] => February
    [3] => March
)

펄처럼, 배열 값을 겹따옴표 안에서 접근할 수 있습니다. 그러나, PHP에서는 배열을 중괄호로 감싸야 합니다.

Example #4 겹따옴표 안에서 배열 접근하기

<?php

$foo = array('bar' => 'baz');
echo "Hello {$foo['bar']}!"; // Hello baz!

?>

주의

Note:

array()는 일반 함수가 아니라 문자 배열을 표현하는 언어 구조입니다.

참고

  • array_pad() - 지정한 길이만큼 특정 값으로 배열 채우기
  • list() - 배열처럼 변수에 할당
  • count() - 배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다
  • range() - 원소의 범위를 가지는 배열 생성
  • foreach
  • 배열형

User Contributed Notes

ayyappan dot ashok at gmail dot com 18-Apr-2016 03:26
The code bellow by dave@wp.pl:

$var_name='var';
$var=array(0,1,2,3,4);
echo $$var_name[2];

//prints NOTHING instead 2.
// prints 2 with PHP version 7.01 onwards

Note :
This results 2 with PHP version 7.01 onwards. Other versions throws a notice error
majkel 21-Apr-2015 06:13
During initialization php parses every value as expression. One can declare variables, do calculations, or even manipulate "current" array

<?php

$arr['A'] = [
    &$arr, // circular reference
    'key' => 'val',
    $arr['B'] = [ // declare array, insert key and then value
        'a' => 'b',
    ],
    ucfirst(strtolower('SOME TEXT')),
    true ? 'TRUE' : 'FALSE', // evaluate expression
    $x  = 3, // declare variable
    $x *= 3, // perform calculations
];

var_dump($arr);

// Output
/*
array(2) {
  'B' => array(1) {
    'a' => string(1) "b"
  }
  'A' => array(7) {
    [0] => array(2) {
      'B' => array(1) {
        ...
      }
      'A' => &array
    }
    'key' => string(3) "val"
    [1] => array(1) {
      'a' => string(1) "b"
    }
    [2] => string(9) "Some text"
    [3] => string(4) "TRUE"
    [4] => int(3)
    [5] => int(9)
  }
}
*/
?>
dave at wp dot pl 28-Feb-2015 12:55
The code bellow:

$var_name='var';
$var=array(0,1,2,3,4);
echo $$var_name[2];

prints NOTHING instead 2.
ole dot aanensen at gmail dot com 10-Jun-2014 05:13
As of PHP 5.4.x you can now use 'short syntax arrays' which eliminates the need of this function.

Example #1 'short syntax array'
<?php
    $a = [1, 2, 3, 4];
    print_r($a);
?>

The above example will output:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Example #2 'short syntax associative array'
<?php
    $a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];
    print_r($a);
?>

The above example will output:
Array
(
    [one] => 1
    [two] => 2
    [three] => 3
    [four] => 4
)
sergei dot solomonov at gmail dot com 02-Oct-2013 05:53
Consider:
file inc1.php
-------------
<?php
return 'key';

file inc2.php
-------------
<?php
return 'value';

Test:
<?php
$a = [
    include 'inc1.php' => include 'inc2.php'
];
var_dump($a);

/* It works!!!
array(1) {
  'key' =>
  string(5) "value"
}
*/
qeremy 07-Mar-2012 11:50
Some tricky functions;

<?php
function is_array_assoc($arr) {
    if (is_array($arr)) {
        foreach ($arr as $k => $v) {
            if (is_string($k) || (is_int($k) && $k < 0)) {
                return 1;
            }
        }
        return 0;
    }
    return -1;
}

function is_array_multi($arr) {
    return is_array($arr)
        ? (count($arr) != count($arr, COUNT_RECURSIVE) ? 1 : 0)
        : -1;
}

$arr[] = array("foo", "bar", 1.09);
$arr[] = array("red", "yellow", 1 => "foo");
$arr[] = array("red", "yellow", -1 => "foo");
$arr[] = array("x" => array("red", "yellow"), "y" => array("one", "two"));
$arr[] = array();
$arr[] = "s";

foreach ($arr as $a) {
    echo is_array_assoc($a) ."\n";
}
echo "\n";
foreach ($arr as $a) {
    echo is_array_multi($a) ."\n";
}
?>

0
0
1
1
0
-1

0
0
0
1
0
-1

<?php
function array_count($arr) {
    $r = 0;
    foreach ($arr as $k => $v) {
        if (is_array($v)) {
            $r++;
        }
    }
    return $r;
}

function array_count_all($arr, $r = 0) {
    foreach ($arr as $k => $v) {
        if (is_array($v)) {
            $r = array_count_all($v, $r);
            $r++;
        }
    }
    return $r;
}

$a = array("foo", "bar", 1.09, array(1,2,3), array("a" => "aaa"));
echo array_count($a); // 2

$a = array(
    "foo", "bar", 1.09,
    "x" => array(1,2,3),
    array("a" => "aaa", "b" => array(), "c" => array(""), "d" => array("")),
    "e" => array(""),
    "z" => array(""),
    "q" => array("", array()),
    "w" => array("lorem", 1 => "impsum", 18, "dolor", array("last as 11th array"))
);
echo array_count_all($a); // 11
?>
r0h4rd at gmail dot com 06-Feb-2011 11:59
"Quick and dirty" class to get an offset of multidimensional array by given path (sorry, that without comments).

<?php

    class ArrayAsPathException extends Exception {}

    class ArrayAsPath {
        protected
            $data = array(),
            $separator = '.';

        public function __construct (array $data = array()) {
            $this->data = $data;
        }

        public function set ($value, $path = null) {
            if (!isset($path)) {
                $this->data = $value;
            }

            $separator = $this->separator;
            $pathtoken = strtok($path, $separator);

            $code = '';
            $pices = '[\''.$pathtoken.'\']';
            while ($pathtoken !== false) {
                if (($pathtoken = strtok($separator)) !== false) {
                    $code .= 'if (!isset($this->data'.$pices.')) $this->data'.$pices.' = array(); ';
                    $pices .= '[\''.$pathtoken.'\']';
                } else {
                    $code .= 'return $this->data'.$pices.' = $value;';
                }
            }

            return eval($code);
        }

        public function get ($path = '', $default = null) {
            $result = $this->data;
            $separator = $this->separator;
            $pathtoken = strtok($path, $separator);

            while ($pathtoken !== false) {
                if (!isset($result[$pathtoken]) || is_string($result)) {
                    if (isset($default)) {
                        return $default;
                    }

                    throw new ArrayAsPathException ('Can\'t find "'.$pathtoken.'" in "'.$path.'"');
                }

                $result = $result[$pathtoken];
                $pathtoken = strtok($separator);
            }

            return $result ? $result : $default;
        }

        public function has ($path) {
            $result = $this->data;
            $separator = $this->separator;
            $pathtoken = strtok($path, $separator);

            while ($pathtoken !== false) {
                if (!isset($result[$pathtoken]) || is_string($result)) {
                    return false;
                }

                $result = $result[$pathtoken];
                $pathtoken = strtok($separator);
            }

            return true;
        }

        public function setSepatator ($separator) {
            $this->separator = $separator;
        }

        public function getSeparator ($separator) {
            return $this->separator;
        }
    }

?>

Code:
<?php

    $params = new ArrayAsPath;
    $params->set(array(
        'foo' => array(
            'bar' => array(
                'item' => 'Value'
            )
        )
    ));

    try {
        $params->set('test', 'foo.bar.far.new');
        printf(
            'Array:<pre>%s</pre>
            foo.bar.item:<pre>%s</pre>
            foo.bar.far:<pre>%s</pre>
            foo.bar.far.new<pre>%s</pre>',
            var_export($params->get(), true),
            var_export($params->get('foo.bar.item'), true),
            var_export($params->get('foo.bar.far'), true),
            var_export($params->get('foo.bar.far.new'), true)
        );
    } catch (ArrayAsPathException $e) {
        echo 'Oops! It seems that something is wrong. '.$e->getMessage();
    }

?>

Will display:
Array:
array (
  'foo' =>
  array (
    'bar' =>
    array (
      'item' => 'Value',
      'far' =>
      array (
        'new' => 'test',
      ),
    ),
  ),
)

foo.bar.item:
'Value'

foo.bar.far:
array (
  'new' => 'test',
)

foo.bar.far.new:
'test'
eugene at ultimatecms dot co dot za 03-Mar-2010 03:29
This is a useful way to get foreign key variables from a specific sql table.
This function can be used to include all relevant data from all relating tables:

<?php
function get_string_between($string, $start, $end){
         $string = " ".$string;
         $ini = strpos($string,$start);
         if($ini == 0)
                return $tbl;
         $ini += strlen($start);
         $len = strpos($string,$end,$ini) - $ini;
         return substr($string,$ini,$len);
}

function get_foreign_keys($tbl) {
        $query = query_getrow("SHOW CREATE TABLE ".mysql_escape_string($tbl));

        $dat = explode('CONSTRAINT',$query[1]);
        foreach($dat as $d => $a) {
                if(strpos($a,"FOREIGN KEY"))
                $data['keys'][] = array($tbl,get_string_between($a,"` FOREIGN KEY (`","`) REFERENCES"));
        }

        foreach($dat as $d => $a) {
                if(strpos($a,"REFERENCE"))
                $data['references'][] = explode('` (`',get_string_between($a,"REFERENCES `","`) ON"));
        }
        return $data;
}

//Example code:

        $data = get_foreign_keys('task_table');
        echo '<pre>';
        print_r($data);
        echo '</pre>';

?>

// $query[1] outputs:

CREATE TABLE `task_table` (
  `task_id` int(64) NOT NULL auto_increment,
  `ticket_id` int(64) NOT NULL,
  `task_type` varchar(64) NOT NULL,
  `comment` text,
  `assigned_to` int(11) default NULL,
  `dependant` int(64) default NULL,
  `resolved` int(1) default NULL,
  PRIMARY KEY  (`task_id`),
  KEY `ticket_id` (`ticket_id`,`dependant`),
  KEY `assigned_to` (`assigned_to`),
  KEY `task_dependant` (`dependant`),
  CONSTRAINT `task_table_ibfk_1` FOREIGN KEY (`ticket_id`) REFERENCES `tickets_table` (`ticket_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `task_table_ibfk_2` FOREIGN KEY (`assigned_to`) REFERENCES `contact_table` (`contact_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `task_table_ibfk_3` FOREIGN KEY (`dependant`) REFERENCES `task_table` (`task_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1

// $data outputs:
Array (
    [keys] => Array
        (
            [0] => Array
                (
                    [0] => task_table
                    [1] => ticket_id
                )

            [1] => Array
                (
                    [0] => task_table
                    [1] => assigned_to
                )

            [2] => Array
                (
                    [0] => task_table
                    [1] => dependant
                )
        )

    [references] => Array
        (
            [0] => Array
                (
                    [0] => tickets_table
                    [1] => ticket_id
                )

            [1] => Array
                (
                    [0] => contact_table
                    [1] => contact_id
                )

            [2] => Array
                (
                    [0] => task_table
                    [1] => task_id
                )
        )
)
kamil at navikam dot pl 03-Jun-2009 12:42
Easy function to unarray an array :-)
It will make $array['something'] => $something.
Usefull for making code more clear.

example of use:
<?
function unarray($row) {
    foreach($row as $key => $value) {
        global $$key;
        $$key = $value;
    }
}

$sql = mysql_query("SELECT * FROM `pracownicy`");
while ($row = mysql_fetch_assoc($sql)) {
    unarray($row);
    echo $idpracownika.'<br>'; //instead of $row['idpracownika']
}
?>
Marcel G 28-Apr-2009 12:49
I encountered the following but didn't see it documented/reported anywhere so I'll just mentioned it here.

As written in the manual:
"When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1".

This generated index has always the largest integer used as a key so far. So adding $a[5] = 'foo'; after an $a[10] = 'bar'; will not force the next generated index to be 6 but to be 11 as 10 was the highest index encountered until here.

Anyway, the following can happen:
<?php
$max_int = 2147483647; // Max value for integer on a 32-bit system
$arr = array();

$arr[1] = 'foo'; // New generated index will be 2
$arr[ $max_int ] = 'bar'; // Caution: Next generated index will be -2147483648 due to the integer overflow!
$arr[0] = 'bar'; // The highest value should be 2147483648 but due to the i-overflow it is -2147483648 so current index 0 is larger. The new generated index therefore is 1!
$arr[]  = 'failure'; // Warning: Cannot add element to the array as the next element is already occupied.
?>
alwynlobo at gmail dot com 03-Mar-2009 08:03
Here's an example on how to create a multi-dimensional array...(without using ArrayObject)

<?php
$characters = array(array(array(
                array ( name=>"name 1",
                        occupation=>"Developer",
                        age=>30,
                        specialty=>"Java" ),
                array ( name=>"name 2",
                        occupation=>"Programmer",
                        age=>24,
                        specialty=>"C++" ),
                array ( name=>"name 3",
                        occupation=>"Designer",
                        age=>63,
                        specialty=>"Javascript" )))
);

print $characters[0][0][2][occupation];
?>
jonberglund at gmail dot com 07-Jul-2008 06:25
The following function (similar to one above) will render an array as a series of HTML select options (i.e. "<option>...</option>"). The problem with the one before is that there was no way to handle <optgroup>, so this function solves that issue.

function arrayToSelect($option, $selected = '', $optgroup = NULL)
{
    $returnStatement = '';

    if ($selected == '') {
        $returnStatement .= '<option value="" selected="selected">Select one...</option>';
    }

    if (isset($optgroup)) {
        foreach ($optgroup as $optgroupKey => $optgroupValue) {
            $returnStatement .= '<optgroup label="' . $optgroupValue . '">';

            foreach ($option[$optgroupKey] as $optionKey => $optionValue) {
                if ($optionKey == $selected) {
                    $returnStatement .= '<option selected="selected" value="' . $optionKey . '">' . $optionValue . '</option>';
                } else {
                    $returnStatement .= '<option value="' . $optionKey . '">' . $optionValue . '</option>';
                }
            }

            $returnStatement .= '</optgroup>';
        }
    } else {
        foreach ($option as $key => $value) {
            if ($key == $selected) {
                $returnStatement .= '<option selected="selected" value="' . $key . '">' . $value . '</option>';
            } else {
                $returnStatement .= '<option value="' . $key . '">' . $value . '</option>';
            }
        }
    }

    return $returnStatement;
}

 So, for example, I needed to render a list of states/provinces for various countries in a select field, and I wanted to use each country name as an <optgroup> label. So, with this function, if only a single array is passed to the function (i.e. "arrayToSelect($stateList)") then it will simply spit out a bunch of "<option>" elements. On the other hand, if two arrays are passed to it, the second array becomes a "key" for translating the first array.

Here's a further example:

$countryList = array(
    'CA'        => 'Canada',
    'US'        => 'United States');

$stateList['CA'] = array(
    'AB'        => 'Alberta',
    'BC'        => 'British Columbia',
    'AB'        => 'Alberta',
    'BC'        => 'British Columbia',
    'MB'        => 'Manitoba',
    'NB'        => 'New Brunswick',
    'NL'        => 'Newfoundland/Labrador',
    'NS'        => 'Nova Scotia',
    'NT'        => 'Northwest Territories',
    'NU'        => 'Nunavut',
    'ON'        => 'Ontario',
    'PE'        => 'Prince Edward Island',
    'QC'        => 'Quebec',
    'SK'        => 'Saskatchewan',
    'YT'        => 'Yukon');

$stateList['US'] = array(
    'AL'        => 'Alabama',
    'AK'        => 'Alaska',
    'AZ'        => 'Arizona',
    'AR'        => 'Arkansas',
    'CA'        => 'California',
    'CO'        => 'Colorado',
    'CT'        => 'Connecticut',
    'DE'        => 'Delaware',
    'DC'        => 'District of Columbia',
    'FL'        => 'Florida',
    'GA'        => 'Georgia',
    'HI'        => 'Hawaii',
    'ID'        => 'Idaho',
    'IL'        => 'Illinois',
    'IN'        => 'Indiana',
    'IA'        => 'Iowa',
    'KS'        => 'Kansas',
    'KY'        => 'Kentucky',
    'LA'        => 'Louisiana',
    'ME'        => 'Maine',
    'MD'        => 'Maryland',
    'MA'        => 'Massachusetts',
    'MI'        => 'Michigan',
    'MN'        => 'Minnesota',
    'MS'        => 'Mississippi',
    'MO'        => 'Missouri',
    'MT'        => 'Montana',
    'NE'        => 'Nebraska',
    'NV'        => 'Nevada',
    'NH'        => 'New Hampshire',
    'NJ'        => 'New Jersey',
    'NM'        => 'New Mexico',
    'NY'        => 'New York',
    'NC'        => 'North Carolina',
    'ND'        => 'North Dakota',
    'OH'        => 'Ohio',
    'OK'        => 'Oklahoma',
    'OR'        => 'Oregon',
    'PA'        => 'Pennsylvania',
    'RI'        => 'Rhode Island',
    'SC'        => 'South Carolina',
    'SD'        => 'South Dakota',
    'TN'        => 'Tennessee',
    'TX'        => 'Texas',
    'UT'        => 'Utah',
    'VT'        => 'Vermont',
    'VA'        => 'Virginia',
    'WA'        => 'Washington',
    'WV'        => 'West Virginia',
    'WI'        => 'Wisconsin',
    'WY'        => 'Wyoming');

...

<select name="state" id="state"><?php echo arrayToSelect($stateList,'',$countryList) ?></select>
<select name="country" id="country"><?php echo arrayToSelect($countryList,'US') ?></select>
sebasg37 at gmail dot com 12-Feb-2008 08:32
Recursive function similar to print_r for describing anidated arrays in html <ol>. Maybe it's useful for someone.

<?php
function describeAnidatedArray($array)
{
    $buf = '';
    foreach($array as $key => $value)
    {
        if(is_array($value))
        {
            $buf .= '<ol>' . describeAnidatedArray($value) . '</ol>';
        }
        else
            $buf .= "<li>$value</li>";
    }
    return $buf;
}

// Example:
$array = array("a", "b", "c", array("1", "2", array("A", "B")), array("3", "4"), "d");

echo describeAnidatedArray($array);
?>

output:

# a
# b
# c

   1. 1
   2. 2
         1. A
         2. B

   1. 3
   2. 4

# d
jon hohle 16-Nov-2007 02:12
Here are shorter versions of sam barrow's functions. From a PHP perspective these are O(1) (without getting into what's going on in the interpreter), instead of O(n).

<?php
function randomKey(array $array)
{
    return randomElement(array_keys($array));
}

function randomElement(array $array)
{
    if (count($array) === 0)
    {
        trigger_error('Array is empty.',  E_USER_WARNING);
        return null;
    }

    $rand = mt_rand(0, count($array) - 1);
    $array_keys = array_keys($array);

    return $array[$array_keys[$rand]];
}
?>
sam barrow 13-Nov-2007 07:46
check out these functions - retrieves a random element or key of an array (numeric or string indexes, doesn't matter).

function randomKey(array $array)
    {
        global $mod, $plg, $sec ;

        $rand = rand(0, count($array) - 1) ;

        $counter = 0 ;
        foreach (array_keys($array) as $key)
            {
                if ($counter++ == $rand)
                    {
                        return $key ;
                    }
            }

        trigger_error('Array is empty.', E_USER_WARNING) ;
    }

function randomElement(array $array)
    {
        global $mod, $plg, $sec ;

        $rand = rand(0, count($array) - 1) ;

        $counter = 0 ;
        foreach ($array as $value)
            {
                if ($counter++ == $rand)
                    {
                        return $value ;
                    }
            }

        trigger_error('Array is empty.', E_USER_WARNING) ;
    }
Mike Mackintosh 30-Oct-2007 06:38
If you need to add an object as an array key, for example an object from Simple XML Parser, you can use the following.

File.XML
<?xml version="1.0" encoding="UTF-8"?>
<Settings type="General">
  <Setting name="SettingName">This This This</Setting>
</Settings>

The script:
<?php
$raw = $xml =  new SimpleXMLElement('File.XML');
foreach($raw->Setting as $A => $B)
{
    // Set Array From XML
    $Setting[(string) $B['name']] = (string)  $B[0];
}
?>
By telling the key to read the object as a string, it will let you set it.

Hope this helps someone out!
webmaster at phpemailformprocessor dot com 07-Aug-2006 06:48
When using an array to create a list of keys and values for a select box generator which will consist of states I found using "NULL" as an index and ""(empty value) as a value to be useful:

<?php

$states = array(
    0    => 'Select a State',
    NULL => '',
    1    => 'AL - Alabama',
    2    => 'AK - Alaska',
    # And so on ...
);

$select = '<select name="state" id="state" size="1">'."\r\n";

foreach($states as $key => $value){
    $select .= "\t".'<option value="'.$key.'">' . $value.'</option>'."\r\n";
}

$select .= '</select>';

echo $select;

?>

 This will print out:

<select name="state" id="state" size="1">
    <option value="0">Select a State</option>
    <option value=""></option>
    <option value="1">AL - Alabama</option>
    <option value="2">AK - Alaska</option>
    # And so on ...

</select>

Now a user has a blank value to select if they later decide to not provide their address in the form. The first two options will return TRUE when checked against the php function - EMPTY() after the form is submitted when processing the form
jupiter at nospam dot com 02-Jun-2006 12:33
<?php

// changes any combination of multiarray elements and subarrays
// into a consistent 2nd level multiarray, tries to preserves keys
function changeMultiarrayStructure($multiarray, $asc = 1) {
  if ($asc == 1) {  // use first subarrays for new keys of arrays
    $multiarraykeys = array_reverse($multiarray, true);
  } else {  // use the last array keys
    $multiarraykeys = $multiarray;  // use last subarray keys
  }  // end array reordering
  $newarraykeys = array();  // establish array
  foreach ($multiarraykeys as $arrayvalue) {  // build new array keys
    if (is_array($arrayvalue)) {  // is subarray an array
      $newarraykeys = array_keys($arrayvalue) + $newarraykeys;
    }  // if count(prevsubarray)>count(currentarray), extras survive
  }  // end key building loop
  foreach ($multiarray as $newsubarraykey => $arrayvalue) {
    if (is_array($arrayvalue)) {  // multiarray element is an array
      $i = 0;  // start counter for subarray key
      foreach ($arrayvalue as $subarrayvalue) {  // access subarray
        $newmultiarray[$newarraykeys[$i]][$newsubarraykey] = $subarrayvalue;
        $i++;  // increase counter
      }  // end subarray loop
    } else {  // multiarray element is a value
      foreach ($newarraykeys as $newarraykey) {  // new subarray keys
        $newmultiarray[$newarraykey][$newsubarraykey] = $arrayvalue;
      }  // end loop for array variables
    }  // end conditional
  }  // end new multiarray building loop
  return $newmultiarray;
}

// will change
$old = array('a'=>1,'b'=>array('e'=>2,'f'=>3),'c'=>array('g'=>4),'d'=>5);
// to
$new = array('e'=>array('a'=>1,'b'=>2,'c'=>4,'d'=>5),
  'f'=>array('a'=>1,'b'=>3,'d'=>5));

// note: if $asc parameter isn't default, last subarray keys used

?>

The new key/value assignment pattern is clearer with bigger arrays.
I use this to manipulate input/output data from my db. Enjoy.
bill at carneyco dot com 03-Feb-2006 05:42
I wanted to be able to control the flow of data in a loop instead of just building tables with it or having to write 500 select statements for single line items. This is what I came up with thanks to the help of my PHP brother in FL. Hope someone else gets some use out it.
<?

//set array variable
$results = array();

//talk to the db
$query = "SELECT * FROM yourtable";
$result = mysql_query($query) or die(mysql_error());

//count the rows and fields
$totalRows = mysql_num_rows($result);
$totalFields = mysql_num_fields($result);

//start the loop
for ( $i = 0; $i < $totalRows; ++$i ) {

//make it 2 dim in case you change your order
  $results[$i] = mysql_fetch_array($result);

//call data at will controlling the loop with the array
echo $results[your_row_id]['your_field_name']; }

//print the entire array to see what lives where
print_r($results);  ?>
php 07-Jan-2006 12:30
This function converts chunks of a string in an array:

function array_str($str, $len) {
  $newstr = '';
  for($i = 0; $i < strlen($str); $i++) {
    $newstr .= substr($str, $i, $len);
  }
  return $newstr;
}

use it as:

$str = "abcdefghilmn";
echo "<table width=\"100%\">\n";
foreach(array_str($str, 4) as $chunk) {
  echo "<tr><td>".$chunk."</td></tr>\n";
}
echo "</table>";

this prints:

------
abcd
------
efgh
------
ilmn
------

It don't use regular expressions. Please add this function to php :)
matthiasDELETETHIS at ansorgs dot de 31-May-2005 11:52
How to use array() to create an array of references rather than of copies? (Especially needed when dealing with objects.) I played around somewhat and found a solution: place & before the parameters of array() that shall be references. My PHP version is 4.3.10.

Demonstration:

<?php
$ref1 = 'unchanged';
$ref2 = & $ref1;

$array_of_copies = array($ref1, $ref2);
print_r($array_of_copies); // prints: Array ( [0] => unchanged [1] => unchanged )
$array_of_copies[0] = 'changed'; // $ref1 = 'changed'; is not equivalent, as it was _copied_ to the array
print_r($array_of_copies); // prints: Array ( [0] => changed [1] => unchanged )

$array_of_refs = array(& $ref1, & $ref2); // the difference: place & before arguments
print_r($array_of_refs); // prints: Array ( [0] => unchanged [1] => unchanged )
$array_of_refs[0] = 'changed'; // $ref1 = 'changed'; is equivalent as $array_of_refs[0] references $ref1
print_r($array_of_refs, true); // prints: Array ( [0] => changed [1] => changed )
?>
aissatya at yahoo dot com 16-May-2005 10:48
<?php

$foo = array('bar' => 'baz');
echo "Hello {$foo['bar']}!"; // Hello baz!

?>
<?php
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
?>
<?php
$fruits = array (
   "fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"),
   "numbers" => array(1, 2, 3, 4, 5, 6),
   "holes"  => array("first", 5 => "second", "third")
);
?>
brian at blueeye dot us 22-Apr-2005 02:34
If you need, for some reason, to create variable Multi-Dimensional Arrays, here's a quick function that will allow you to have any number of sub elements without knowing how many elements there will be ahead of time. Note that this will overwrite an existing array value of the same path.

<?php
// set_element(array path, mixed value)
function set_element(&$path, $data) {
    return ($key = array_pop($path)) ? set_element($path, array($key=>$data)) : $data;
}
?>

For example:

<?php
echo "<pre>";
$path = array('base', 'category', 'subcategory', 'item');
$array = set_element($path, 'item_value');
print_r($array);
echo "</pre>";
?>

Will display:
Array
(
    [base] => Array
        (
            [category] => Array
                (
                    [subcategory] => Array
                        (
                            [item] => item_value
                        )
                )
        )
)
mortoray at ecircle-ag dot com 18-Feb-2005 02:35
Be careful if you need to use mixed types with a key of 0 in an array, as several distinct forms end up being the same key:

$a = array();
$a[null] = 1;
$a[0] = 2;
$a['0'] = 3;
$a["0"] = 4;
$a[false] = 5;
$a[0.0] = 6;
$a[''] = 7;
$a[] = 8;

print_r( $a );

This will print out only 3 values: 6, 7, 8.
rdude at fuzzelish dot com 17-Feb-2005 10:35
If you are creating an array with a large number of static items, you will find serious performance differences between using the array() function and the $array[] construct. For example:
<?
 // Slower method
 $my_array = array(1, 2, 3, ? 500);

 // Faster method
 $my_array[] = 1;
 $my_array[] = 2;
 $my_array[] = 3;
 ?
 $my_array[] = 500;
?>
phpm at nreynolds dot me dot uk 11-Jan-2005 05:24
This helper function creates a multi-dimensional array. For example, creating a three dimensional array measuring 10x20x30: <?php $my_array = multi_dim(10, 20, 30); ?>

<?php

function multi_dim()
{
    $fill_value = null;

    for ($arg_index = func_num_args() - 1; $arg_index >= 0; $arg_index--) {
        $dim_size = func_get_arg($arg_index);
        $fill_value = array_fill(0, $dim_size, $fill_value);
    }

    return $fill_value;
}

?>
baZz 17-Oct-2003 12:27
Chek this out!!!. Suppose that you want to create an array like the following:
<?php
  $arr1 = (
    0 => array ("customer"=>"Client 1","Item a"),
    1 => array ("customer"=>"Client 2","Item b")
  );
?>
Seems prety easy, but what if you want to generate it dinamically woops!!!.  Imagine that you have a file with thousands of lines and each line is a purchase order from diferent clients:
<?php
/*function to add elements*/
function addArray(&$array, $id, $var)
{
    $tempArray = array( $var => $id);
    $array = array_merge ($array, $tempArray);
}
/*The same as above but the element is an array*/
function addArrayArr(&$array, $var, &$array1)
{
    $tempArray = array($var => $array1);
    $array = array_merge ($array, $tempArray);
}
/*labels of our array or heders of the file*/
$keyarr = array("customer","item");
/*info that may you read from a file line 1 and 2*/
$valarr0 = array("Client 1","Item a");
$valarr1 = array("Client 2","Item b");

$numofrows = 2;/*In our case is just two lines*/
$tmpArray = array();
for($i = 0; $i < $numofrows; $i++){
  $tmp = "valarr$i";
  $tmpvar = ${$tmp};/*Using var of vars tricky tricky*/
  foreach( $keyarr as $key=>$value){
    addArray($tmparr,$tmpvar[$key],$value);
  }
  addArrayArr($finalarr,$i,$tmparr);
} /*voila all it's perfectly ordered on finalarr*/

/*Here we just print the info but you can insert it into a database*/
echo "Customer: ".$finalarr[0]["customer"]."<br>";
echo "Item: ".$finalarr[0]["item"]."<br>";
echo "Customer: ".$finalarr[1]["customer"]."<br>";
echo "Item: ".$finalarr[1]["item"]."<br>";
?>

The lines above should print something like:
Customer: Client 1
Item: Item a
Customer: Client 2
Item: Item b
I hope someone find this useful.
TCross1 at hotmail dot com 03-Sep-2003 09:38
here is the sort of "textbook" way to output the contents of an array which avoids using foreach() and allows you to index & iterate through the array as you see fit:

<?php

$arrayName = array("apples", "bananas", "oranges", "pears");
$arrayLength = count($arrayName);

for ($i = 0; $i < $arrayLength; $i++){
    echo "arrayName at[" . $i . "] is: [" .$arrayName[$i] . "]<br>\n";
}

?>

enjoy!

-tim
10-May-2003 12:53
Similarly to a comment by stlawson at sbcglobal dot net on this page:
http://www.php.net/basic-syntax.instruction-separation

It is usually advisable to define your arrays like this:
$array = array(
     'foo',
     'bar',
);

Note the comma after the last element - this is perfectly legal. Moreover,
it's best to add that last comma so that when you add new elements to the
array, you don't have to worry about adding a comma after what used to be
the last element.

<?php
$array = array(
     'foo',
     'bar',
     'baz',
);
?>
marcel at labor-club dot de 25-Feb-2003 04:58
i tried to find a way to create BIG multidimensional-arrays. but the notes below only show the usage of it, or the creation of small arrays like $matrix=array('birne', 'apfel', 'beere');

for an online game, i use a big array (50x80) elements.
it's no fun, to write the declaration of it in the ordinary way.

here's my solution, to create an 2d-array, filled for example with raising numbers.

<?php
$matrix=array();
$sx=30;
$sy=40;
$i=1;
for ($y=0; $y<$sy; $y++)
{
    array_push($matrix,array());
    for ($x=0; $x<$sx; $x++)
    {
        array_push($matrix[$y],array());
        $matrix[$x][$y]=$i;
        $i++;
    }
}
?>

if there is a better way, plz send an email. i always want to learn more php!
MadLogic at Paradise dot net dot nz 31-Oct-2002 11:39
Heres a simple yet intelligent way of setting an array, grabbing the values from the array using a loop.

<?php
$ary = array("1"=>'One','Two',"3"=>'Three');
$a = '0'; $b = count($ary);
while ($a <= $b) {
  $pr = $ary[$a];
  print "$pr<br>";
  $a++;
}
?>
mads at __nospam__westermann dot dk 23-Oct-2002 04:39
In PHP 4.2.3 (and maybe earlier versions) arrays with numeric indexes may be initialized to start at a specific index and then automatically increment the index. This will save you having to write the index in front of every element for arrays that are not zero-based.

The code:

<?php
                  $a = array
        (
            21    => 1,
            2,
            3,
        );
    print '<pre>';
    print_r($a);
    print '</pre>';
?>

will print:

<?php
Array
(
    [21] => 1
    [22] => 2
    [23] => 3
)
?>
John 18-Jul-2002 08:48
Be careful not to create an array on top of an already existing variable:

<?php
$name = "John";
$name['last'] = "Doe";
?>

$name becomes "Dohn" since 'last' evaluates to the 0th position of $name.
Same is true for multi-arrays.
jay at ezlasvegas dot net 21-Apr-2002 01:21
If you want to create an array of a set size and you have PHP4, use
array_pad(array(), $SIZE, $INITIAL_VALUE); This can be handy if you wish
to initialize a bunch of variables at once:

list($Var1, $Var2, etc) = array_pad(array(), $NUMBER_OF_VARS,
$INITIAL_VALUE);

Jay Walker
Las Vegas Hotel Associate
http://www.ezlasvegas.net
jjm152 at hotmail dot com 11-Mar-2002 03:22
The easiest way to "list" the values of either a normal 1 list array or a multi dimensional array is to use a foreach() clause.

Example for 1 dim array:

<?php
   $arr = array( 1, 2, 3, 4, 5 );
   foreach ( $arr as $val ) {
       echo "Value: $Val\n";
      }
?>

For multi dim array:

<?php
     $arr = array( 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four, 5 => 'five');
      foreach ( $arr as $key => $value ) {
       echo "Key: $key, Value: $value\n";
      }
?>

This is quite possibly the easiest way i've found to iterate through an array.
tobiasquinteiro at ig dot com dot br 29-Jan-2002 09:25
<?
        // This is a small script that shows how to use an multiple array
        for($x = 0;$x < 10;$x++){
                for($y = 0;$y < 10;$y++){
                        $mat[$x][$y] = "$x,$y";
                }
        }

        for($x = 0;$x < count($mat);$x++){
                for($y = 0;$y < count($mat[$x]);$y++){
                        echo    "mat[$x][$y]: " .
                                $mat[$x][$y] . " ";
                }
                echo "\n";
        }
?>
joshua dot e at usa dot net 24-May-2001 08:12
Here's a cool tip for working with associative arrays-
Here's what I was trying to accomplish:

I wanted to hit a DB, and load the results into an associative array, since I only had key/value pairs returned. I loaded them into an array, because I wanted to manipulate the data further after the DB select, but I didn't want to hit the DB more than necessary.

Here's how I did it:

<?php
//assume db connectivity
//load it all into the associative array
$sql = "SELECT key,value FROM table";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)) {
$myArray[$row[0]] = $row[1];
}
//now we expand it
while(list($key,$value) = each($myArray)) {
echo "$key : $value";
}
?>

I found this to be super efficient, and extremely cool.
slicky at newshelix dot com 20-Mar-2001 10:57
Notice that you can also add arrays to other arrays with the $array[] "operator" while the dimension doesn't matter.

Here's an example:
$x[w][x] = $y[y][z];
this will give you a 4dimensional assosiative array.
$x[][] = $y[][];
this will give you a 4dimensional non assosiative array.

So let me come to the point. This get interessting for shortening things up. For instance:

<?php
        foreach ($lines as $line){
            if(!trim($line)) continue;
            $tds[] = explode("$delimiter",$line);
        }
?>
rubein at earthlink dot net 26-Sep-2000 08:07
Multidimensional arrays are actually single-dimensional arrays nested inside other single-dimensional arrays.

$array[0] refers to element 0 of $array
$array[0][2] refers to element 2 of element 0 of $array.

If an array was initialized like this:

$array[0] = "foo";
$array[1][0] = "bar";
$array[1][1] = "baz";
$array[1][2] = "bam";

then:
is_array($array) = TRUE
is_array($array[0]) = FALSE
is_array($array[1]) = TRUE
count($array) = 2 (elements 0 and 1)
count($array[1] = 3 (elements 0 thru 2)

This can be really useful if you want to return a list of arrays that were stored in a file or something:

$array[0] = unserialize($somedata);
$array[1] = unserialize($someotherdata);

if $somedata["foo"] = 42 before it was serialized previously, you'd now have this:
$array[0]["foo"] = 42


배열을 내림차순 정렬하고 인덱스의 상관관계를 유지

  • ≪ array
  • asort ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지

arsort

(PHP 4, PHP 5, PHP 7)

arsort — 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지

설명

bool arsort ( array &$array [, int $sort_flags ] )

이 함수는 배열 인덱스가 그 배열의 원소와 상관관계를 유지할수 있도록 배열을 정렬한다.

이 함수는 주로 실제 원소의 정렬이 중요한 연관 배열을 정렬할 때 이용한다.

인수

array

입력 배열.

sort_flags

선택적인 sort_flags 인수로 정렬 방법을 바꿀 수 있습니다. 자세한 내용은 sort()를 참고하십시오.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 arsort() 예제

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
arsort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

위 예제의 출력:

a = orange
d = lemon
b = banana
c = apple

fruits는 알파벳 내림 차순으로 정렬되었고, 각 원소와 연관된 인덱스가 유지되었다.

  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • rsort() - 역순으로 배열 정렬
  • ksort() - 키에 의한 배열 정렬
  • sort() - 배열 정렬

User Contributed Notes

sebas2day 07-May-2012 11:52
If you are dealing with a multidimensional array you want to sort, then this might be helpfull:

<?php
function array_sort($arr){
    if(empty($arr)) return $arr;
    foreach($arr as $k => $a){
        if(!is_array($a)){
            arsort($arr); // could be any kind of sort
            return $arr;
        }else{
            $arr[$k] = array_sort($a);
        }
    }
    return $arr;
}
?>
FatBat 13-Oct-2011 10:40
Needed to get the index of the max/highest value in an assoc array.
max() only returned the value, no index, so I did this instead.

<?php
reset($x);   // optional.
arsort($x);
$key_of_max = key($x);   // returns the index.
?>
jordancdarwin at googlemail dot com 15-Dec-2007 11:21
A lot of people seem to trip up on this and ask me questions as to debugging. Bear in mind that this returns boolean, and does not return an array of affected items.

$array = array("One"=>1, "Three" => 3,"Two" =>2);
print_r(asort($array));

If successful, will return 1, and error if there is a string used. Useful to note so then people stop asking me :D
rodders_plonker at yahoo dot com 22-Aug-2000 03:43
I was having trouble with the arsort() function on an older version of PHP which was returning an error along the lines of 'wrong perameter count for function arsort' when I tried to use a flag for numeric sorting (2/SORT_NUMERIC).
I figured, as I only wanted to sort integers, I could pad numbers from the left to a specific length with 0's (using the lpad function provided by improv@magma.ca in the notes at http://www.php.net/manual/ref.strings.php).
A string sort then correctly sorts numerically (i.e. {30,2,10,21} becomes {030,021,010,002} not {30,21,2,10}) when echoing the number an (int)$string_name hides the leading 0's.

Made my day :).

Rodders.
morgan at anomalyinc dot com 25-Nov-1999 04:30
If you need to sort a multi-demension array, for example, an array such as

$TeamInfo[$TeamID]["WinRecord"]
$TeamInfo[$TeamID]["LossRecord"]
$TeamInfo[$TeamID]["TieRecord"]
$TeamInfo[$TeamID]["GoalDiff"]
$TeamInfo[$TeamID]["TeamPoints"]

and you have say, 100 teams here, and want to sort by "TeamPoints":

first, create your multi-dimensional array. Now, create another, single dimension array populated with the scores from the first array, and with indexes of corresponding team_id... ie
$foo[25] = 14
$foo[47] = 42
or whatever.
Now, asort or arsort the second array.
Since the array is now sorted by score or wins/losses or whatever you put in it, the indices are all hoopajooped.
If you just walk through the array, grabbing the index of each entry, (look at the asort example. that for loop does just that) then the index you get will point right back to one of the values of the multi-dimensional array.
Not sure if that's clear, but mail me if it isn't...
-mo


배열을 정렬하고 인덱스 상관 관계를 유지

  • ≪ arsort
  • compact ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열을 정렬하고 인덱스 상관 관계를 유지

asort

(PHP 4, PHP 5, PHP 7)

asort — 배열을 정렬하고 인덱스 상관 관계를 유지

설명

bool asort ( array &$array [, int $sort_flags ] )

이 함수는 배열의 인덱스가 그 배열의 원소와 상관관계를 유지할수 있도록 배열을 정렬한다. 이 함수는 주로 실제 원소의 정렬이 중요한 연관 배열을 정렬할 경우 사용된다.

인수

array

입력 배열.

sort_flags

선택적인 sort_flags 인수로 정렬 방법을 바꿀 수 있습니다. 자세한 내용은 sort()를 참고하십시오.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 asort() 예제

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

위 예제의 출력:

c = apple
b = banana
d = lemon
a = orange

fruits는 알파벳 순으로 정렬하고, 각 원소와 연관된 인덱스가 유지된다.

참고

  • arsort() - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • sort() - 배열 정렬
  • ksort() - 키에 의한 배열 정렬
  • rsort() - 역순으로 배열 정렬

User Contributed Notes

aditycse at gmail dot com 07-Aug-2015 09:41
/*
 * Name : Aditya Mehrotra
 * Email: aditycse@gmail.com
 */
//Example for sorting by values for an alphanumeric array also having case-sensitive data
$exampleArray1 = $exampleArray2 = array(
    0 => 'example1',
    1 => 'Example10',
    2 => 'example12',
    3 => 'Example2',
    4 => 'example3',
    5 => 'EXAMPLE10',
    6 => 'example10'
);

//default sorting
asort($exampleArray1);

// alphanumeric with case-sensitive data sorting by values
asort($exampleArray2, SORT_STRING | SORT_FLAG_CASE | SORT_NATURAL);

//output of defaut sorting
print_r($exampleArray1);
/*
 * output of default sorting
  Array
  (
  [5] => EXAMPLE10
  [1] => Example10
  [3] => Example2
  [0] => example1
  [6] => example10
  [2] => example12
  [4] => example3
  )
 */

print_r($exampleArray2);
/*
 * output of alphanumeric with case-sensitive data sorting by values
 Array
(
    [0] => example1
    [3] => Example2
    [4] => example3
    [5] => EXAMPLE10
    [1] => Example10
    [6] => example10
    [2] => example12
)
 */
stephen [ at ] brooksie-net [ dot ] co [ dot ] uk 02-Mar-2013 01:37
It is obviously necessary to log the keys used as so we don't overwrite previous entries in the array ;o)

N.B. this also effects the timings, but still faster than the alternatives :

     * int : 0.00159    (ms)
     * str : 0.00092    (ms)

<?php
private function _keySort(Array $data)
    {
        $keys = array();

        foreach($data as $row) {

            $keyIncrement =
                (!isset($keys[$row[$this->_orderField]]))
                    ? $keys[$row[$this->_orderField]] = 0
                    : ++$keys[$row[$this->_orderField]];

            $tempArray[$row[$this->_orderField].$keyIncrement] = $row;
        }

        if ($this->_orderDirection == 'DESC') {
            krsort($tempArray, SORT_NATURAL | SORT_FLAG_CASE );
        } else {
            ksort($tempArray, SORT_NATURAL | SORT_FLAG_CASE);
        }

        return $tempArray;

    }
?>
nick ([AT]) nickyost ([DOT]) com 16-Sep-2011 12:32
This function can be used to sort multidimensional arrays with almost no work whatsoever by using the individual values within the custom sort function.

This function passes the entire child element even if it is not a string. If it is an array, as would be the case in multidimensional arrays, it will pass the whole child array as one parameter.

Therefore, do something elegant like this:

<?php
     // Sort the multidimensional array
     usort($results, "custom_sort");
     // Define the custom sort function
     function custom_sort($a,$b) {
          return $a['some_sub_var']>$b['some_sub_var'];
     }
?>

This does in 4 lines what other functions took 40 to 50 lines to do. This does not require you to create temporary arrays or anything. This is, for me, a highly preferred solution.

Hope it helps!
Anon 07-May-2011 02:37
Case insensitive sort in one line:

<?php

array_multisort(array_map('strtolower', $array), $array);

?>
rick at toewsweb dot net 29-Apr-2011 01:40
On sorting by value first, then by key (cf., 2008-01-31 notes by mike at clear-link dot com):

What occurred to me to solve this problem was to extract the keys and values into separate arrays, then use array_multisort to get the desired order:

Ex:
<?php
$kvpairs = array('noun' => 'thought', 'animal' => 'fish', 'abstract' => 'thought', 'food' => 'fish', 'verb' => 'fish');
print "before:\n";
print_r($kvpairs);

// Essentially, one line of code is all that's needed for the sort:
array_multisort(array_values($kvpairs), array_keys($kvpairs), $kvpairs);

print "after:\n";
print_r($kvpairs);
?>

before:
Array
(
    [noun] => thought
    [animal] => fish
    [abstract] => thought
    [food] => fish
    [verb] => fish
)
after:
Array
(
    [animal] => fish
    [food] => fish
    [verb] => fish
    [abstract] => thought
    [noun] => thought
)

Of course, array_multisort allows you to specify sort order (SORT_ASC, SORT_DESC) and sort type (SORT_REGULAR, SORT_STRING, SORT_NUMERIC) for each array you pass it.
Eran 22-Apr-2009 02:15
i made this sample multi dimensional array sort to someone who needed to read lines from a text file instead of using sql - order by statement.

the idea is to take the key and sort(order) the specific column and then order the entire of the array as the selected column asort returned.

<?php
/**
 *
 * This sample is reading text data
 * and perform a sort to a 2 dimensional array
 * just like a normal sql do to "order by asc"
 *
 */

$foo = array();

/*
# SORT_REGULAR - compare items normally (don't change types)
# SORT_NUMERIC - compare items numerically
# SORT_STRING - compare items as strings
*/
$sort_by = SORT_REGULAR;

/*
# 0 - order by name column
# 1 - order by age column
# 2 - order by rank column
# 3 - order by color column
*/
$order_by = 1;

// source file
$line_of_text[1] = 'a1name|f2age|h3rank|jcolor';
$line_of_text[2] = 'b1name|d2age|i3rank|k4color';
$line_of_text[0] = 'c1name|e2age|g3rank|l4color';

// make array
for ($x=0; $x<=2; $x++)
{
    $line = explode('|',$line_of_text[$x]);

    // save it by coulmns otherwise it will saved like rows
    for ($i=0; $i<=3; $i++) {
          $foo[$i][$x] = $line[$i];
    }
}

// get the key order
$a = $foo[$order_by];

// sort
asort($a, $sort_by);

// start print
echo "<table cellpudding=0 cellspacing=0 border=1>\n";
        echo "<tr>\n";
          echo "<td>key</td>\n";
          echo "<td>name</td>\n";
          echo "<td>age</td>\n";
          echo "<td>rank</td>\n";
          echo "<td>color</td>\n";
        echo "</tr>\n";

// print by key order
foreach ($a as $k => $v) {
    echo "<tr>\n";
      echo "<td>$k</td>\n";
      // you can print here a for loop (0 to num of columns[=3])
      echo "<td>".$foo[0][$k]."</td>\n";
      echo "<td>".$foo[1][$k]."</td>\n";
      echo "<td>".$foo[2][$k]."</td>\n";
      echo "<td>".$foo[3][$k]."</td>\n";
    echo "</tr>\n";
}
echo "</table>\n";
?>
bakatamas at freemail dot hu 15-Feb-2008 11:34
Dear mike at clear-link dot com!

I needed a case insensitive version of your function. It is still not perfect as I have to do something with country specific characters (ouo?uea?iOUO?UEA?I)

function akisort(&$array,$valrev=false,$keyrev=false)
    {
    if ($valrev)
        {
        arsort($array);
        }
        else
        {
        asort($array);
        };
    $vals = array_count_values($array);
    $i = 0;
    foreach ($vals AS $val=>$num)
        {
        $first = array_splice($array,0,$i);
        $tmp = array_splice($array,0,$num);
        $tmp2 = array();
        foreach($tmp as $key => $value)
            {
            $tmp2[] = $key;
            $number = $value;
            };
        natcasesort($tmp2);
        reset($tmp2);
        print_r($tmp2);
        unset($tmp);
        foreach($tmp2 as $key => $value)
            {
            $tmp[$value] = $number;
            };
        if($keyrev)
            {
            $tmp = array_reverse($tmp, true);
            };
        $array = array_merge($first,$tmp,$array);
        unset($tmp);
        $i = $i+$num;
        };
};
mike at clear-link dot com 01-Feb-2008 12:56
Small typo in the aksort function I just submitted. Here's the entire thing again, with the correction noted:

<?php
function aksort(&$array,$valrev=false,$keyrev=false) {
  if ($valrev) { arsort($array); } else { asort($array); }
  $vals = array_count_values($array);
    $i = 0;
    foreach ($vals AS $val=>$num) {
        $first = array_splice($array,0,$i);
        $tmp = array_splice($array,0,$num);
        if ($keyrev) { krsort($tmp); } else { ksort($tmp); }
        $array = array_merge($first,$tmp,$array);
        unset($tmp);
        $i = $i+$num;
        // Fixed from previous post: $i = $num;
    }
}
?>
mike at clear-link dot com 01-Feb-2008 12:23
For a recent project I needed to sort an associative array by value first, and then by key if a particular value appeared multiple times. I wrote this function to accomplish the task. Note that the parameters default to sort ascending on both keys and values, but allow granular control over each.

<?php
function aksort(&$array,$valrev=false,$keyrev=false) {
  if ($valrev) { arsort($array); } else { asort($array); }
    $vals = array_count_values($array);
    $i = 0;
    foreach ($vals AS $val=>$num) {
        $first = array_splice($array,0,$i);
        $tmp = array_splice($array,0,$num);
        if ($keyrev) { krsort($tmp); } else { ksort($tmp); }
        $array = array_merge($first,$tmp,$array);
        unset($tmp);
        $i = $num;
    }
}

// Example
$tmp = array('ca'=>1,'cb'=>2,'ce'=>1,'pa'=>2,'pe'=>1);

// Standard asort
asort($tmp);
print_r($tmp);

// Sort value ASC, key ASC
aksort($tmp);
print_r($tmp);

// Sort value DESC, key ASC
aksort($tmp,true);
print_r($tmp);

// Sort value DESC, key DESC
aksort($tmp,true,true);
print_r($tmp);

// Results
Array
(
    [pe] => 1
    [ca] => 1
    [ce] => 1
    [cb] => 2
    [pa] => 2
)
Array
(
    [ca] => 1
    [ce] => 1
    [pe] => 1
    [cb] => 2
    [pa] => 2
)
Array
(
    [cb] => 2
    [pa] => 2
    [ca] => 1
    [ce] => 1
    [pe] => 1
)
Array
(
    [pa] => 2
    [cb] => 2
    [pe] => 1
    [ce] => 1
    [ca] => 1
)
mzvarik at gmail dot com 16-Jan-2008 06:29
I noticed that my function mentioned earlier is very misleading - somebody please delete that note!

This is how you sort:
<?php
setlocale (LC_ALL, 'czech');
$array = array("a", "?", "c");
usort ($array, 'strcoll');
print_r($array);
?>
przemekkus [at] interia [dot] pl 02-Nov-2007 03:34
Function written by a dot brandon at chello dot nl has an error  - wrong variable name. It should be:

if($rev) arsort($named_hash,$flags=0) ;

instead of

if($reverse) arsort($named_hash,$flags=0) ;
a dot brandon at chello dot nl 26-Jun-2007 04:18
I use this for quasi-SQL orderby. Loosely based on smileaf. Any good for you nerds?

<?

function named_records_sort($named_recs, $order_by, $rev=false, $flags=0)
{// Create 1-dimensional named array with just
 // sortfield (in stead of record) values
    $named_hash = array();
     foreach($named_recs as $key=>$fields)
             $named_hash["$key"] = $fields[$order_by];

 // Order 1-dimensional array,
 // maintaining key-value relations
    if($reverse) arsort($named_hash,$flags=0) ;
    else asort($named_hash, $flags=0);

 // Create copy of named records array
 // in order of sortarray
    $sorted_records = array();
    foreach($named_hash as $key=>$val)
           $sorted_records["$key"]= $named_recs[$key];

return $sorted_records;} // named_recs_sort()

function show_sorted_records($named_recs, $order_by, $rev=false, $flags=0)
{$sorted_records=named_records_sort($named_recs, $order_by, $rev, $flags);
foreach($sorted_records as $name=>$fields)
  {echo "<b>$name</b>   ";
   foreach($fields as $field=>$val)
          echo "$field = $val "; echo "<br>";}
} // show_sorted_records()

$girl_friends=array();
$girl_friends["Anna"]=
array("born"=>'1989-08-22',"cupsize"=>'B-',"IQ"=>105, "daddy"=>'rich');
$girl_friends["Zoe"]
=array("born"=>'1978-03-11',"cupsize"=>'C#',"IQ"=>130, "daddy"=>'poor');
$girl_friends["Lilly"]
=array("born"=>'1985-06-16',"cupsize"=>'DD',"IQ"=>90, "daddy"=>'nasty');

$order_by="cupsize"; echo "And the winners are: <br>";
show_sorted_records($girl_friends, $order_by, true);

?>
gunnar at taljaren dot se 14-Jun-2007 10:00
for ($i=0;$i<5;$i++)
   $values[] = $i;
asort($values);

works, but
for ($i=0;$i<5;$i++)
   $values[$i] =$i;
asort($values);
doesn't!
smileaf at smileaf dot org 25-Jan-2007 03:34
Ok I was mistaken, after re-reading the previous post the ".$key" is important. What caused the sorting issue for me wasn't that at all. But rather something else.
doing an: asort($records, $flags); before returning fixes the sorting problems.
The sorting problem I was refearing to causes a character based sorting done on numeric data.
so instead of:
1
2
3
...
10
12
20
It was returned back as
1
10
12
2
20
3
...
basically what I was trying to fix in the first place.
richard at happymango dot me dot uk 23-Nov-2006 12:52
This is a fixed version of the same function I posted below. Now it will handle duplicate entries in the sorted field. EG: If there were two records that had the name Heathrow it would still work.

<?php

function record_sort($records, $field, $reverse=false)
{
    $hash = array();

    foreach($records as $key => $record)
    {
        $hash[$record[$field].$key] = $record;
    }

    ($reverse)? krsort($hash) : ksort($hash);

    $records = array();

    foreach($hash as $record)
    {
        $records []= $record;
    }

    return $records;
}

?>
richard at happymango dot me dot uk 22-Nov-2006 12:35
This is a function to sort an indexed 2D array by a specified sub array key, either ascending or descending.

It is usefull for sorting query results from a database by a particular field after the query has been returned

This function can be quite greedy. It recreates the array as a hash to use ksort() then back again

By default it will sort ascending but if you specify $reverse as true it will return the records sorted descending

<?php

function record_sort($records, $field, $reverse=false)
{
    $hash = array();

    foreach($records as $record)
    {
        $hash[$record[$field]] = $record;
    }

    ($reverse)? krsort($hash) : ksort($hash);

    $records = array();

    foreach($hash as $record)
    {
        $records []= $record;
    }

    return $records;
}

// Example below

$airports = array
(
    array( "code" => "LHR", "name" => "Heathrow" ),
    array( "code" => "LGW", "name" => "Gatwick" ),
);

printf("Before: <pre>%s</pre>", print_r($airports, true));

$airports = record_sort($airports, "name");

printf("After: <pre>%s</pre>", print_r($airports, true));

?>

Example Outputs:

Before: Array
(
    [0] => Array ( [code] => LHR, [name] => Heathrow )
    [1] => Array ( [code] => LGW, [name] => Gatwick )
)

After: Array
(
    [0] => Array ( [code] => LGW, [name] => Gatwick )
    [1] => Array ( [code] => LHR, [name] => Heathrow )
)
php at web-in-time dot com 05-Sep-2006 11:47
acecream's function works fine, especially with the spectre extension.

nevertheless sometimes the index values have to be kept. To achieve this, just replace:

$sorted_arr[] = $array[$arr_key];

with:

$sorted_arr[$arr_key] = $array[$arr_key];
rojaro 24-Jun-2004 04:38
Advanced sort array by second index function, which produces ascending (default) or descending output and uses optionally natural case insensitive sorting (which can be optionally case sensitive as well).
Only the first two arguments are required.

<?php

function sabsi ($array, $index, $order='asc', $natsort=FALSE, $case_sensitive=FALSE) {
  if(is_array($array) && count($array)>0) {
    foreach(array_keys($array) as $key) $temp[$key]=$array[$key][$index];
    if(!$natsort) ($order=='asc')? asort($temp) : arsort($temp);
    else {
      ($case_sensitive)? natsort($temp) : natcasesort($temp);
      if($order!='asc') $temp=array_reverse($temp,TRUE);
    }
    foreach(array_keys($temp) as $key) (is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
    return $sorted;
  }
  return $array;
}

?>
spectre at hellfish dot NOSPAM dot org 28-Apr-2003 06:54
that works nicely, tho it breaks the result-array up if one or more of arrays indexes are deleted before sorting. this one should fix it up:

change:
for ($i = 0; $i < sizeof($array); $i++) {

to:
foreach ($array as $i => $k) {
mbevan at marginsoftware dot com 03-Dec-2002 10:25
Nevermind... use my last note as a quick tip: if you wish to keep the keys, use asort() and arsort() in place of sort() and rsort().
01-Aug-2002 03:48
Sorry, my last post had a typo:
// unnecessary backslashes break create_function, oops.
  if ( is_string($var) ) $var = "\'$var\'";
//it should be:
  if ( is_string($var) ) $var = "'$var'";

-- FIXED and TESTED -- :)

Similar to above but for an array of arrays instead of an array of objects.

<?php
function aasort($x,$var,$cmp='strcasecmp'){
  if ( is_string($var) ) $var = "'$var'";
  uasort($x,
    create_function('$a,$b',
      'return '.$cmp.'( $a['.$var.'],$b['.$var.']);')
  );
  return $x;
}
?>
phzzzt .a.t. acm .d.o.t. org 01-Aug-2002 03:32
Similar to above but for an array of arrays instead of an array of objects.

<?php
function aasort($x,$var,$cmp='strcasecmp'){
  if ( is_string($var) ) $var = "\'$var\'";
  uasort($x,
    create_function('$a,$b',
      'return '.$cmp.'( $a['.$var.'],$b['.$var.']);')
  );
  return $x;
}
?>
salchicha at cable dot net dot co 03-Apr-2002 10:23
Here's one I whipped up to allow you to sort an array of a specific class by a member or function:

<?php
// Sort a class by one of its members (even lowercase!!!)
function casort($arr, $var) {
   $tarr = array();
   $rarr = array();
   for($i = 0; $i < count($arr); $i++) {
      $element = $arr[$i];
      $tarr[] = strtolower($element->{$var});
   }

   reset($tarr);
   asort($tarr);
   $karr = array_keys($tarr);
   for($i = 0; $i < count($tarr); $i++) {
      $rarr[] = $arr[intval($karr[$i])];
   }

   return $rarr;
}
?>

It works very well. For example, I have a Room class with members title, isActive(), date, etc. I can sort an array by casort($rooms, "title") or casort($rooms, "isActive()") and it'll work.
rcwang at cmu dot edu 03-Mar-2002 02:42
Here's my version of sorting multi-dimensional array by 2nd index.
Feel free to change the code to suit your needs.

<?php
function aSortBySecondIndex($multiArray, $secondIndex) {
    while (list($firstIndex, ) = each($multiArray))
        $indexMap[$firstIndex] = $multiArray[$firstIndex][$secondIndex];
    asort($indexMap);
    while (list($firstIndex, ) = each($indexMap))
        if (is_numeric($firstIndex))
            $sortedArray[] = $multiArray[$firstIndex];
        else $sortedArray[$firstIndex] = $multiArray[$firstIndex];
    return $sortedArray;
}
?>
freeman at generalresources dot com 04-May-2001 01:51
The asortbyindex($sortarray, $index) looks like sort not asort. The key of the $sortarray was changed.
sweetland at whoadammit dot com 15-Aug-2000 09:02
Here's a little routine I whipped up to sort multi-dimensional arrays:
<?php
/**
 ** comesafter ($s1, $s2)
 **
 ** Returns 1 if $s1 comes after $s2 alphabetically, 0 if not.
 **/

function comesafter ($s1, $s2) {
        /**
         ** We don't want to overstep the bounds of one of the strings and segfault,
         ** so let's see which one is shorter.
         **/

        $order = 1;

        if (strlen ($s1) > strlen ($s2)) {
                $temp = $s1;
                $s1 = $s2;
                $s2 = $temp;
                $order = 0;
        }

        for ($index = 0; $index < strlen ($s1); $index++) {
                /**
                 ** $s1 comes after $s2
                 **/

                if ($s1[$index] > $s2[$index]) return ($order);

                /**
                 ** $s1 comes before $s2
                 **/

                if ($s1[$index] < $s2[$index]) return (1 - $order);
        }

        /**
         ** Special case in which $s1 is a substring of $s2
         **/

        return ($order);
}

/**
 ** asortbyindex ($sortarray, $index)
 **
 ** Sort a multi-dimensional array by a second-degree index. For instance, the 0th index
 ** of the Ith member of both the group and user arrays is a string identifier. In the
 ** case of a user array this is the username; with the group array it is the group name.
 ** asortby
 **/

function asortbyindex ($sortarray, $index) {
        $lastindex = count ($sortarray) - 1;
        for ($subindex = 0; $subindex < $lastindex; $subindex++) {
                $lastiteration = $lastindex - $subindex;
                for ($iteration = 0; $iteration < $lastiteration;    $iteration++) {
                        $nextchar = 0;
                        if (comesafter ($sortarray[$iteration][$index], $sortarray[$iteration + 1][$index])) {
                                $temp = $sortarray[$iteration];
                                $sortarray[$iteration] = $sortarray[$iteration + 1];
                                $sortarray[$iteration + 1] = $temp;
                        }
                }
        }
        return ($sortarray);
}
?>

It's a bit long with all the comments, but I hope it helps.
bwuhlman at tallships dot ca 03-Aug-2000 12:01
Well, actually, asort has *two* annoying features.

It works perfectly well sorting hashes (or associative arrays, as you might have it), but doggedly refuses to sort regular arrays maintaining index assocation. Kind've makes sense, but the docs don't explicitly say you can't do it.

Urgggh.
jacko at kring dot co dot uk 25-Feb-2000 08:26
asort has one anoying feature, it ignores any default or implicit order in the data.  i.e. if two elements of an array contain "banana" then it is not garanteed that the first will still be the first after the sort.
This makes the Burrows-Wheeler block sort a bit of a pain to impliment, with a trailing string having to be appended to all strings before sorting, and removed after sorting. To maintain the so called "banana" order.


변수와 그 값을 가지는 배열 생성

  • ≪ asort
  • count ≫
  • PHP Manual
  • 배열 함수 목록
  • 변수와 그 값을 가지는 배열 생성

compact

(PHP 4, PHP 5, PHP 7)

compact — 변수와 그 값을 가지는 배열 생성

설명

array compact ( mixed $varname [, mixed $... ] )

변수와 그 값을 가지는 배열을 생성합니다.

이들 각각에 대해, compact() 는 현재 심볼 테이블에서 그 이름을 갖는 변수를 찾고, 그 변수명이 키가 되고 변수의 내용은 그 키에 대한 값이 될수 있도록 출력 배열에 추가한다. 짧게 말해서, 이 함수는 extract()과 반대이다.

선언되지 않은 모든 문자열은 단순히 무시합니다.

인수

varname

compact()는 가변 인수를 가집니다. 각 인수는 변수명을 가지는 문자열이거나, 변수명의 배열일 수 있습니다. 배열은 그 안에 변수명을 가지는 다른 배열을 포함할 수 있습니다; compact()는 재귀적으로 다룹니다.

반환값

모든 변수를 추가한 결과 배열을 반환합니다.

예제

Example #1 compact() 예제

<?php
$city  = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";

$location_vars = array("city", "state");

$result = compact("event", "nothing_here", $location_vars);
?>

위 예제의 출력:

Array
(
    [event] => SIGGRAPH
    [city] => San Francisco
    [state] => CA
)

주의

Note: Gotcha

PHP의 자동 전역 배열은 함수 안에서 가변 변수로 사용할 수 없기에, 자동 전역 배열은 compact()에 전달할 수 없습니다.

참고

  • extract() - 배열에서 현재 심볼 테이블로 변수를 입력

User Contributed Notes

jmarkmurph at yahoo dot com 26-Jan-2016 10:45
So compact('var1', 'var2') is the same as saying array('var1' => $var1, 'var2' => $var2) as long as $var1 and $var2 are set.
piedone at pyrocenter dot hu 15-Apr-2011 03:00
A quick way of compacting all local variables:

<?php
$localVariables = compact(array_keys(get_defined_vars()));
?>

This is useful if you want to return all local variables from a function/method or you want to pass all local variables to one. A valid example would be to use this with application hooks/events (if you want the called hook to be able to modify everything in the caller), but otherwise use with care (as methods should be used through their declared interface).
Robc 20-Jan-2011 12:16
The description says that compact is the opposite of extract() but it is important to understand that it does not completely reverse extract().  In particluar compact() does not unset() the argument variables given to it (and that extract() may have created).  If you want the individual variables to be unset after they are combined into an array then you have to do that yourself.
tatarynowicz at gmail dot com 11-Jan-2011 12:17
Please note that compact() will _not_ issue a warning if the specified variable name is undefined.
M Spreij 24-May-2007 03:10
Can also handy for debugging, to quickly show a bunch of variables and their values:

<?php
print_r(compact(explode(' ', 'count acw cols coldepth')));
?>

gives

Array
(
    [count] => 70
    [acw] => 9
    [cols] => 7
    [coldepth] => 10
)


배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다

  • ≪ compact
  • current ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다

count

(PHP 4, PHP 5, PHP 7)

count — 배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다

설명

int count ( mixed $var [, int $mode ] )

배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다.

객체는 SPL을 설치했다면, Countable 인터페이스를 가질 경우에 count()를 사용할 수 있습니다. 이 인터페이스는 정확히 하나의 메쏘드 count()을 가지며, count() 함수의 반환값을 반환합니다.

매뉴얼 배열 섹션을 참고하여 배열이 PHP에서 어떻게 구현되고 사용되는지 확인하십시오.

인수

var

배열.

mode

선택적인 mode 인수를 COUNT_RECURSIVE(또는 1)로 설정하면, count()는 배열을 재귀적으로 셉니다. 이는 다차원 배열의 원소를 셀 경우 유용한 경우가 있습니다. mode 기본값은 0입니다. count()는 무한 재귀를 검출하지 못합니다.

반환값

var 안에 있는 원소 수를 반환합니다. 보통 array이며, 다른 자료형은 하나의 요소만 가집니다.

var가 배열이나 Countable 인터페이스를 가진 객체가 아니라면, 1을 반환합니다. 한가지 예외는, var가 NULL일 경우에 0을 반환합니다.

Caution

count()는 설정하지 않은 변수에 대하여 0을 반환하지만, 빈 배열에 대해서도 0을 반환합니다. 변수를 설정했는지 여부는 isset()을 사용하시오.

버전 | 설명
4.2.0 | 선택적인 mode 인수가 추가되었습니다.

예제

Example #1 count() 예제

<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count ($a);
// $result == 3

$b[0] = 7;
$b[5] = 9;
$b[10] = 11;
$result = count ($b);
// $result == 3;

$result = count(null);
// $result == 0

$result = count(false);
// $result == 1
?>

Example #2 재귀적인 count() 예제

<?php
$food = array( 'fruits'  => array('orange', 'banana', 'apple'),
               'veggie'  => array('carrot', 'collard','pea'));

// 재귀 count
echo count($food,COUNT_RECURSIVE);  // 8 출력

// 보통 count
echo count($food);                  // 2 출력

?>

참고

  • is_array() - 변수가 배열인지 확인
  • isset() - 설정된 변수인지 확인
  • strlen() - 문자열 길이를 얻습니다

User Contributed Notes

ayyappan dot ashok at gmail dot com 18-Apr-2016 04:27
/** Count of objects */

 class ABC{ }
 $objecto = new ABC();
 $total = count((array)$object); //Returns Zero.
 $total = count($object); //Returns One
 echo $total;
ThisIsNotImportant 18-Dec-2015 12:10
About 2d arrays, you have many way to count elements :

<?php
$MyArray = array ( array(1,2,3),
                   1,
                   'a',
                   array('a','b','c','d') );

// All elements
echo count($MyArray ,COUNT_RECURSIVE);  // output 11 (9 values + 2 arrays)

// First level elements
echo count($MyArray );                  // output 4 (2 values+ 2 arrays)

// Both level values, but only values
echo(array_sum(array_map('count',$MyArray ))); //output 9 (9 values)

// Only second level values
echo (count($MyArray ,COUNT_RECURSIVE)-count($MyArray )); //output 7 ((all elements) - (first elements))
?>
pied-pierre 05-Jun-2015 02:27
A function of one line to find the number of elements that are not arrays, recursively :

function count_elt($array, &$count=0){
  foreach($array as $v) if(is_array($v)) count_elt($v,$count); else ++$count;
  return $count;
}
JumpIfBelow 05-May-2015 11:18
As I see in many codes, don't use count to iterate through array.
Onlyranga says you could declare a variable to store it before the for loop.
I agree with his/her approach, using count in the test should be used ONLY if you have to count the size of the array for each loop.

You can do it in the for loop too, so you don't have to "search" where the variable is set.
e.g.
<?php
    $array = [1, 5, 'element'];
    for($i = 0, $c = count($array); $i < $c; $i++)
        var_dump($array[$i]);
?>
onlyranga at gmail dot com 28-Apr-2014 11:14
[Editor's note: array at from dot pl had pointed out that count() is a cheap operation; however, there's still the function call overhead.]

If you want to run through large arrays don't use count() function in the loops , its a over head in performance,  copy the count() value into a variable and use that value in loops for a better performance.

Eg:

// Bad approach

for($i=0;$i<count($some_arr);$i++)
{
    // calculations
}

// Good approach

$arr_length = count($some_arr);
for($i=0;$i<$arr_length;$i++)
{
    // calculations
}
Gerd Christian Kunze 11-Dec-2013 09:56
Get maxWidth and maxHeight of a two dimensional array..?

Note:
1st dimension = Y (height)
2nd dimension = X (width)
e.g. rows and cols in database result arrays

<?php
$TwoDimensionalArray = array( 0 => array( 'key' => 'value', ...), ... );
?>

So for Y (maxHeight)
<?php
$maxHeight = count( $TwoDimensionalArray )
?>

And for X (maxWidth)
<?php
$maxWidth = max( array_map( 'count',  $TwoDimensionalArray ) );
?>

Simple? ;-)
danny at dannymendel dot com 13-Jun-2007 10:14
I actually find the following function more useful when it comes to multidimension arrays when you do not want all levels of the array tree.

// $limit is set to the number of recursions
<?php
function count_recursive ($array, $limit) {
    $count = 0;
    foreach ($array as $id => $_array) {
        if (is_array ($_array) && $limit > 0) {
            $count += count_recursive ($_array, $limit - 1);
        } else {
            $count += 1;
        }
    }
    return $count;
}
?>
alexandr at vladykin dot pp dot ru 08-Nov-2006 01:28
My function returns the number of elements in array for multidimensional arrays subject to depth of array. (Almost COUNT_RECURSIVE, but you can point on which depth you want to plunge).

<?php
  function getArrCount ($arr, $depth=1) {
      if (!is_array($arr) || !$depth) return 0;

     $res=count($arr);

      foreach ($arr as $in_ar)
         $res+=getArrCount($in_ar, $depth-1);

      return $res;
  }
?>


배열의 현재 원소를 반환

  • ≪ count
  • each ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 현재 원소를 반환

current

(PHP 4, PHP 5, PHP 7)

current — 배열의 현재 원소를 반환

설명

mixed current ( array &$array )

모든 배열은 "현재" 원소를 가리키는 내부 포인터를 갖는데, 이 내부 포인터는 배열에 삽입되어진 첫번째 원소로 초기화된다.

인수

array

배열.

반환값

current() 함수는 단순히 현재 내부 포인터에서 가리키고 있는 배열 원소의 값을 반환한다. 어떤 방법으로든 그 포인터를 움직이지 않는다. 내부 포인터가 원소 목록의 끝에서 벗어난 곳을 가리키면, current()는 FALSE를 반환한다.

Warning

이 함수는 논리 FALSE를 반환하지만, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

예제

Example #1 current()의 용례와 관련 함수

<?php
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport);    // $mode = 'bike';
$mode = current($transport); // $mode = 'bike';
$mode = prev($transport);    // $mode = 'foot';
$mode = end($transport);     // $mode = 'plane';
$mode = current($transport); // $mode = 'plane';

$arr = array();
var_dump(current($arr)); // bool(false)

$arr = array(array());
var_dump(current($arr)); // array(0) { }
?>

주의

Note: 배열의 끝과 boolean FALSE 원소를 구별할 수 없습니다. FALSE 원소를 가지는 배열을 올바르게 따라가려면, each()를 참고하십시오.

참고

  • end() - 배열 내부 포인터가 마지막 원소를 가리키게 설정
  • key() - 배열에서 키를 가져옵니다
  • each() - 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진
  • prev() - 내부 배열 포인터를 후진
  • reset() - 배열의 내부 포인터를 첫 원소로 설정
  • next() - 배열의 내부 배열 포인터를 전진

User Contributed Notes

jodevweb at gmail dot com 16-Sep-2016 11:50
As Sergey stated a long time ago in this notes, when adding to the array using the brackets syntax, current won't return the last added value. Instead of moving the pointer with end(), you simply can get the last value with <?php $my_array[count($my_array) - 1] ?> (only applies to a non-associative array).
Vasily Yudin (st-2 at mail dot ru) 12-Nov-2014 05:51
If you do current() after using uset() on foreach statement, you can get FALSE in PHP version 5.2.4 and above.
There is example:
<?php
$prices = array(
    0 => '1300990',
    1 => '500',
    2 => '600'
);
foreach($prices as $key => $price){
    if($price < 1000){
        unset($prices[$key]);
    }
}

var_dump(current($prices)); // bool(false)
?>
If you do unset() without foreach? all will be fine.
<?php
$prices = array(
    0 => '1300990',
    1 => '500',
    2 => '600'
);
unset($prices[1]);
unset($prices[2]);

var_dump(current($prices));
?>
Pratip Ghosh 14-May-2014 02:55
If we unset any element from an array, and then try the current function, I noted it returned FALSE. To overcome this limitation, you can use array_values function to re-order the tree.
strate at yandex dot com 30-Oct-2013 04:11
Note, that you can pass array by expression, not only by reference (as described in doc).

<?php
var_dump( current( array(1,2,3) ) ); // (int) 1
?>
michael at squiloople dot com 11-Jan-2012 04:44
current() also works on objects:

<?php

  echo current((object) array('one', 'two')); // Outputs: one

?>
aefxx 15-May-2008 05:39
A simple copy function that not only copies the given array but ensures the copy's pointer is set to the exact same position:

<?php
function array_copy(&array)
{
    $key = key($array);
    $copy = $array;

    while (($copy_key = key($copy)) !== NULL) {
        if ($copy_key == $key) break;
        next($copy);
    }

    return $copy;
}
?>

That's all ... bye.
gregory at gregory dot net 28-Feb-2008 06:07
It took me a while to figure this out, but there is a more consistent way to figure out whether you really went past the end of the array, than using each().

You see, each() gets the value BEFORE advancing the pointer, and next() gets the value AFTER advancing the pointer. When you are implementing the Iterator interface, therefore, it's a real pain in the behind to use each().

And thus, I give you the solution:
To see if you've blown past the end of the array, use key($array) and see if it returns NULL. If it does, you're past the end of the array -- keys can't be null in arrays.

Nifty, huh? Here's how I implemented the Iterator interface in one of my classes:

<?php

/**
 * DbRow file
 * @package PalDb
 */

/**
 * This class lets you use Db rows and object-relational mapping functionality.
 */

class DbRow implements Iterator
{
    /**
     * The DbResult object that gave us this row through fetchDbRows
     * @var DbResult
     */
    protected $result;

    /**
     * The fields of the row
     * @var $fields
     */
    protected $fields;

    /**
     * Constructor
     *
     * @param PDOStatement $stmt
     *  The PDO statement object that this result uses
     * @param DbResult $result
     *  The result that produced this row through fetchDbRows
     */
    function __construct($result)
    {
        $this->result = $result;
    }

    /**
     * Get the DbResult object that gave us this row through fetchDbRows
     * @return DbResult
     *
     * @return unknown
     */
    function getResult()
    {
        return $this->result;
    }

    function __set(
        $name,
        $value)
    {
        $this->fields[$name] = $value;
    }

    function __get(
        $name)
    {
        if (isset($this->fields[$name]))
            return $this->fields[$name];
        else
            return null;
    }

    /**
     * Iterator implementation - rewind
     */
    function rewind()
    {
        $this->beyondLastField = false;
        return reset($this->fields);
    }

    function valid()
    {
        return !$this->beyondLastField;
    }

    function current()
    {
        return current($this->fields);
    }

    function key()
    {
        return key($this->fields);
    }

    function next()
    {
        $next = next($this->fields);
        $key = key($this->fields);
        if (isset($key)) {
            return $next[1];
        } else {
            $this->beyondLastField = true;
            return false; // doesn't matter what we return here, see valid()
        }
    }

    private $beyondLastField = false;
};

Hope this helps someone.
vaclav dot sir at gmail dot com 13-Aug-2007 06:23
To that "note": You won't be able to distinguish the end of an array from a boolean FALSE element, BUT you can distinguish the end from a NULL value of the key() function.

Example:
<?php
if (key($array) === null) {
    echo "You are in the end of the array.";
} else {
    echo "Current element: " . current($array);
}
?>
mdeng at kabenresearch dot com 24-Apr-2004 08:04
For large array(my sample was 80000+ elements), if you want to traverse the array in sequence, using array index $a[$i] could be very inefficient(very slow). I had to switch to use current($a).
vitalib at 012 dot net dot il 02-Dec-2003 11:10
Note that by copying an array its internal pointer is lost:

<?php
$myarray = array(0=>'a', 1=>'b', 2=>'c');
next($myarray);
print_r(current($myarray));
echo '<br>';
$a = $myarray;
print_r(current($a));
?>

Would output 'b' and then 'a' since the internal pointer wasn't copied. You can cope with that problem using references instead, like that:

<?php
$a =& $myarray;
?>
retestro_REMOVE at SPAM_esperanto dot org dot il 02-Mar-2003 03:31
The docs do not specify this, but adding to the array using the brackets syntax:
     <?php $my_array[] = $new_value; ?>
will not advance the internal pointer of the array. therefore, you cannot use current() to get the last value added or key() to get the key of the most recently added element.

You should do an end($my_array) to advance the internal pointer to the end ( as stated in one of the notes on end() ), then

    <?php
     $last_key = key($my_array);  // will return the key
     $last_value = current($my_array);  // will return the value
    ?>

If you have no need in the key, $last_value = end($my_array) will also do the job.

- Sergey.


배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진

  • ≪ current
  • end ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진

each

(PHP 4, PHP 5, PHP 7)

each — 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진

설명

array each ( array &$array )

배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진합니다.

each()를 실행한 후에, 배열 커서는 배열의 다음 원소를 가르키거나, 배열의 끝이라면 마지막 원소 뒤를 가르킵니다. each를 사용해서 배열을 다시 따라가려면 reset()해야 합니다.

인수

array

입력 배열.

반환값

array 배열에서 현재의 키와 값 쌍을 반환하고 배열 커서를 전진시킨다. 이 키/값 쌍은 4개 원소를 갖는 배열을 반환한다. 0, 1, key, value 이다. 원소 0과 key는 배열 원소의 키 이름을 포함하고, 1과 value는 데이터를 포함한다.

배열의 내부 포인터가 배열의 끝을 벗어나면, each()는 FALSE를 반환한다.

예제

Example #1 each() 예제

<?php
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>

$bar는 현재 다음과 같은 key/value 쌍을 포함한다:

Array
(
    [1] => bob
    [value] => bob
    [0] => 0
    [key] => 0
)

<?php
$foo = array("Robert" => "Bob", "Seppo" => "Sepi");
$bar = each($foo);
print_r($bar);
?>

$bar는 현재 다음과 같은 key/value 쌍을 포함한다:

Array
(
    [1] => Bob
    [value] => Bob
    [0] => Robert
    [key] => Robert
)

each()는 배열을 따라가기 위해 일반적으로 list()와 같이 사용한다. 여기에 예제가 있습니다:

Example #2 each()로 배열 따라가기

<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');

reset($fruit);
while (list($key, $val) = each($fruit)) {
    echo "$key => $val\n";
}
?>

위 예제의 출력:

a => apple
b => banana
c => cranberry

Caution

배열을 다른 변수로 지정하는 것은 원래의 배열 포인터를 초기화 하는것이기 때문에, 위 예제코드는 루프안에서 $fruit를 다른 변수로 지정하게 되면 무한 루프를 유발할수 있다.

Warning

each()는 객체를 받아들이지만, 기대하지 않은 결과를 반환할 수 있습니다. 그러므로 객체 프로퍼티를 each()로 반복하는 일은 권장하지 않습니다.

참고

  • key() - 배열에서 키를 가져옵니다
  • list() - 배열처럼 변수에 할당
  • current() - 배열의 현재 원소를 반환
  • reset() - 배열의 내부 포인터를 첫 원소로 설정
  • next() - 배열의 내부 배열 포인터를 전진
  • prev() - 내부 배열 포인터를 후진
  • foreach
  • 객체 반복

User Contributed Notes

ayyappan dot ashok at gmail dot com 18-Apr-2016 06:21
/* Iterating using objects via each */

class SAI
{
   public function __toString()
   {
          return __CLASS__;
   }
}
$a = new SAI();
$b = new SAI();
$c = new SAI();
$d = new SAI();
$e = new SAI();

$objarray = array($a,$b,$c,$d,$e);
reset($objarray);
while (list($key, $val) = each($objarray))
{
    echo "$key => $val\n";
}
//Results
0 => SAI
1 => SAI
2 => SAI
3 => SAI
4 => SAI

Warning:
each() will also accept objects, but may return unexpected results. It's therefore not recommended to iterate though object properties with each().
nino dot skopac at gmail dot com 02-Jul-2015 10:30
<?php
function each_v2($arr) {
    // same as each() but when it hits end of array, it resets it
    static $i = 0;

    if (isset($arr[$i])) {
        // exists¸
        return $arr[$i++];
    } else {
        // reset $i to 0 and repeat
        $i = 0;
        return each_v2($arr);
    }
}
?>
gaviel 18-Mar-2011 12:53
Ok Here's one for iterating multidimensional array .. using foreach

    <?php
        $members = array(
                            "member1" => array (
                                                "First Name" => "Robert",
                                                "Last Name" => "Burton",
                                                "Age" => "20"
                                                ),
                            "member2" => array (
                                                "First Name" => "Cheska",
                                                "Last Name" => "Vladesk",
                                                "Age" => "21"
                                                ),
                            "member3" => array (
                                                "First Name" => "Gino",
                                                "Last Name" => "Marley",
                                                "Age" => "19"
                                                ),
                            "member4" => array (
                                                "First Name" => "Jake",
                                                "Last Name" => "White",
                                                "Age" => "16"
                                                ),
                            );
        $dataSetCount = count($members);
        echo "<h1>There are $dataSetCount members</h1>";

        $i = 0;
        foreach ($members as $each_members) {
            $i++;
            echo "<h2>Member $i</h2>";
                foreach ($each_members as $position => $details) {
                    echo "<b>$position</b>" . ": " . $details . "<br />";
                }
            }
    ?>
massimo dot modica at tin dot it 03-Feb-2010 11:36
If you forget to reset the array before each(), the same code may give different results with different php versions.

<?php

$a = array(1,2,3);

foreach ($a AS $k => $v) $a[$k] = 2*$v;

while(list($k2, $v2) = each($a)) { echo($v2."\n"); }

?>

In PHP 5.2.0:

2
4
6

In PHP 5.2.6:

4
6
php at omit dot ianco dot co dot uk 13-Apr-2008 03:10
I wanted to be able to add to an array while looping through it. foreach does not allow this because it is using a secret copy of the array. each makes this possible (tested on PHP 4).
<?php
$shopping_list = array('oysters', 'caviare');
reset ($shopping_list);
while (list($key, $value) = each ($shopping_list)) {
    if ($value == 'oysters') $shopping_list[] = 'champagne';
    elseif ($value == 'champagne') $shopping_list[] = 'ice';
}
print_r($shopping_list);
// Array ( [0] => oysters [1] => caviare [2] => champagne [3] => ice )
?>
wolfeym38 at yahoo dot com 12-Jun-2007 08:28
Regarding speed of foreach vs while(list) =each
I wrote a benchmark script and the results are that clearly foreach is faster. MUCH faster. Even with huge arrays (especially with huge arrays). I tested with sizes 100,000. 1,000,000 and 10,000,000. To do the test with 10 million i had to set my memory limit real high, it was close to 1gb by the time it actually worked. Anyways,

<?php
function getDiff($start, $end) {
    $s = explode(' ', $start);
    $stot = $s[1] + $s[0];
    $e = explode(' ', $end);
    $etot = $e[1] + $e[0];
    return $etot - $stot;
}

$lim=10000000;
$arr = array();
for ($i=0; $i<$lim; $i++) {
    $arr[$i] = $i/2;
}

$start = microtime();
foreach ($arr as $key=>$val);

$end = microtime();
echo "time for foreach = " . getDiff($start, $end) . ".\n";

reset($arr);
$start = microtime();
while (list($key, $val) = each($arr));
$end = microtime();
echo "time list each = " . getDiff($start, $end) . ".\n";
?>

here are some of my results: with 1,000,000
time for foreach = 0.0244591236115.
time list each = 0.158002853394.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 0.0245339870453.
time list each = 0.154260158539.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 0.0269000530243.
time list each = 0.157305955887.

then with 10,000,000:
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 1.96586894989.
time list each = 14.1371650696.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 2.02504014969.
time list each = 13.7696218491.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 2.0246758461.
time list each = 13.8425710201.

by the way, these results are with php 5.2 i believe, and a linux machine with 3gb of ram and 2.8ghz dual core pentium
michael k (name der redaktion bekannt.) 10-May-2007 09:36
If you want to iterate over a two-dimensional, sparse array, and want to  first display every first element, then every second and so on, you can use this code:

 $fruits = array ( "fruits"  => array ( "a" => "orange",
                                      "b" => "banana",
                                      "c" => "apple"
                                    ),
                 "numbers" => array ( 1,
                                      2,
                                      3,
                                      4,
                                      5,
                                      6
                                    ),
                 "holes"   => array (      "first",
                                      5 => "second",
                                           "third",
                                     10 => "fourth",
                                    )
               );

$done = False;
while ($done == False) {
       $done = True;

       // Important: &$val has to be a reference (use the &),
       // if you don't, the internal counter of $val will be
       // re-initialized each time and you loop over the first elements
       // for eternity.

       foreach($fruits as $key => &$val) {

               if (list($inner_key, $inner_val) = each(&$val)) {
                       $done = False;
                       echo "$key : : $inner_key => $inner_val  <br>  \n";
               }

       }
}

NOTE: this is just a quick hack, if you know a better way, post it!
sjoerd-php at linuxonly dot nl 31-Mar-2006 03:04
Use foreach instead of while, list and each. Foreach is:
- easier to read
- faster
- not influenced by the array pointer, so it does not need reset().

It works like this:
<?php
$arr = array('foo', 'bar');
foreach ($arr as $value) {
    echo "The value is $value.";
}

$arr = array('key' => 'value', 'foo' => 'bar');
foreach ($arr as $key => $value) {
    echo "Key: $key, value: $value";
}
?>
matthew at mhalls dot net 05-Dec-2005 12:58
To panania at 3ringwebs dot com:

If you know for certain that you are only receiving one row, the while becomes redundant. To shorten your code:

$strSQL = "SELECT * FROM table WHERE id=1";
$RecordsetSelect = $db->runQuery ($strSQL);
list($key, $val) = mysql_fetch_row($RecordsetSelect);
echo "$key => $val\n";
mysql_free_result($RecordsetSelect);

With only one row being returned this is more elegant a solution, but just being nit-picky in essence. It also shows another quick way of using list.
amby2 at izh dot com 24-Nov-2005 06:55
I've found a compact way to cycle through an associative array using for statement (not while, as it has been done in the most of examples below):

<?php

for (reset($array); list($key) = each($array);) {
  echo $key;
  echo $array[$key];
}

?>

or

<?php

for (reset($array); list($key, $value) = each($array);) {
  echo $key;
  echo $value;
  echo $array[$key];
}

?>

You hardly forget to add reset($array) code line using such construction.
Sopinon 23-Jul-2005 02:17
If you want to display the hole structure (tree) of your array, then you can use this recursive solution.

<?PHP
$tree= "";
array_tree($your_array);
echo $tree;

// Recursive Function
function array_tree($array, $index=0){
    global $tree;
    $space="";
    for ($i=0;$i<$index;$i++){
        $space .= "     ";
    }
    if(gettype($array)=="array"){
        $index++;
        while (list ($x, $tmp) = each ($array)){
            $tree .= $space."$x => $tmp\n";
            array_tree($tmp, $index);
        }
    }
}
?>
james at gogo dot co dot nz 02-Nov-2004 04:29
It's worth noting that references to an array don't have thier own array pointer, and taking a reference to an array doesn't reset it's array pointer, so this works as you would expect it would by eaching the first three items of the array, rather than the first item 3 times.

<?php
  $x = array(1,2,3);

  print_r(each($x));
  echo "\n";

  $y =& $x;
  print_r(each($y));
  echo "\n";

  $z =& $y;
  print_r(each($z));
  echo "\n";
?>
wodzuY2k at anronet dot pl 04-Aug-2002 09:41
This function will help you dump any variable into XML structure.

        //dump var into simple XML structure
        function var_dump_xml($tagname,$variable,$level=0)
         {
            for($i=0;$i<$level;$i++) $marg.=' ';
            if (eregi('^[0-9].*$',$tagname)) $tagname='tag_'.$tagname; //XML tag cannot start with [0-9] character
            if (is_array($variable))
             {
                echo $marg."<$tagname>\n";
                while (list ($key, $val) = each ($variable))  var_dump_xml($key,$val,$level+1);
                echo $marg."</$tagname>\n";
             }
            elseif (strlen($variable)>0)
             {
                 echo $marg."<$tagname>".htmlspecialchars($variable)."</$tagname>\n";
             };
         };

        /*
        example:

        $myVar = array("name"=>"Joe", "age"=>"26", "children"=>array("Ann","Michael"));
        var_dump_xml("myVarTag",$myVar);
        */
Gillis at dancrea dot com 26-May-2002 07:13
I wrote a short and pretty simple script to search through associative arrays for some value in the values, heres a simplifyed example of it:

<?php

$foo['bob'] = "bob is ugly";
$foo['bill'] = "bill is rich";
$foo['barbie'] = "barbie is cute";
$search = "rich";

echo "searching the array foo for $search:<br>";
reset ($foo);
while (list ($key, $val) = each ($foo)) {
if (preg_match ("/$search/i", $val)) {
    print "A match was found in $key.<br />";
} else {
    print "A match was not found in $key.<br />";
}
}

?>

will output:
Searching the array foo for rich:
A match was not found in bob
A match was found in bill
A match was not found in barbie
13-Feb-2002 01:10
I usually work a lot with 2D arrays. Since I've had some trouble traversing them correctly maybe someone out there also experienced those problems and can use this one.

It's based on a 2D-array called $array[$x][$y]. At some (but not necessarily all) (x,y) there is a value I want to reach. Note that I do not know beforehand the ranges of $x or $y (that is their highest and lowest values).

while (list ($x, $tmp) = each ($array)) {
   while (list ($y, $val) = each ($tmp)) {
      echo "$x, $y, $val";
   }
}

The answer for each (x,y) pair can thus be (providng, of course those values where in your array beforehand):

1, 1, 2
2, 2, 0
3, 1, 1
5, 2, 2
5, 1, 2

Note that only the (x,y) pairs with a corresponding value is shown.

Hang in there
Jon Egil Strand
NTNU
tk at turtle-entertainment dot de 29-Jan-2001 08:33
Be sure to use the integrated functions "unset();" or "reset();" - many people forget this and wonder about the created output!
kris at angelanthony dot com 25-Oct-2000 10:03
Remember to use "reset()" if you iterate over an array with "each()" more than once!  Example:

while(list($key,$value) = each($array)){
 // code here
}

NOW the internal pointer on $array is at the end of the array, and another attempt at an iteration like the one above will result in zero executions of the code within the "while" block.  You MUST call "reset($array)" to reset the internal array pointer before iterating over the array again from the first element.


배열 내부 포인터가 마지막 원소를 가리키게 설정

  • ≪ each
  • extract ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열 내부 포인터가 마지막 원소를 가리키게 설정

end

(PHP 4, PHP 5, PHP 7)

end — 배열 내부 포인터가 마지막 원소를 가리키게 설정

설명

mixed end ( array &$array )

end()는 array의 내부 포인터를 마지막 원소로 이동시키고 마지막 원소의 값을 반환한다.

인수

array

배열.

반환값

마지막 원소의 값을 반환하거나, 빈 배열에서는 FALSE를 반환합니다.

예제

Example #1 end() 예제

<?php

$fruits = array('apple', 'banana', 'cranberry');

echo end($fruits); // cranberry

?>

참고

  • current() - 배열의 현재 원소를 반환
  • each() - 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진
  • prev() - 내부 배열 포인터를 후진
  • reset() - 배열의 내부 포인터를 첫 원소로 설정
  • next() - 배열의 내부 배열 포인터를 전진

User Contributed Notes

Omolewa stephen 24-Oct-2016 03:16
I found using pathinfo very useful in getting extension of  a file.
<? Php
  $filename = dictionary.txt;
   $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)) ;

echo $ext; // txt
?>
goran 15-May-2016 02:16
In case you need add separator at end

$numbers = array('1','2','3','4');
        foreach ($numbers as $number){
            echo $number;
            if (end($numbers) != $number){
                echo ', ';
            }
        }
ivijan dot stefan at gmail dot com 09-May-2014 01:47
I found that the function end() is the best for finding extensions  on file name. This function cleans backslashes and takes the extension of a file.

<?php
private function extension($str){
    $str=implode("",explode("\\",$str));
    $str=explode(".",$str);
    $str=strtolower(end($str));
     return $str;
}

// EXAMPLE:
$file='name-Of_soMe.File.txt';
echo extension($file); // txt
?>

Very simple.
Jason 18-Mar-2013 07:37
$filename = 'somefile.jpg';

php v5.4 does not support the following statement.
echo end(explode(".", $filename)); // return jpg string

instead you have to split into 2 statements
$file = explode(".", $filename);
echo end ($file);
jorge at REMOVETHIS-2upmedia dot com 01-Mar-2012 10:20
If all you want is the last item of the array without affecting the internal array pointer just do the following:

<?php

function endc( $array ) { return end( $array ); }

$items = array( 'one', 'two', 'three' );
$lastItem = endc( $items ); // three
$current = current( $items ); // one
?>

This works because the parameter to the function is being sent as a copy, not as a reference to the original variable.
franz at develophp dot org 02-Dec-2010 12:06
It's interesting to note that when creating an array with numeric keys in no particular order, end() will still only return the value that was the last one to be created. So, if you have something like this:

    <?php
    $a = array();
    $a[1] = 1;
    $a[0] = 0;
    echo end($a);
    ?>

This will print "0".
laurence at crazypillstudios dot com 07-Jun-2010 11:12
this is a function to move items in an array up or down in the array. it is done by breaking the array into two separate arrays and then have a loop creates the final array adding the item we want to move when the counter is equal to the new position we established the array key, position and direction were passed via a query string

<?php
//parameters
//$array- the array you are modifying
//$keytomove - the key of the item you wish to move
//$pos - the current position of the item: used a count($array) function
//and then loop with incrementing integer to add the position to the up //or down button
//$dir - the direction you want to move it - "up"/"dn"

function change_pos($array, $keytomove, $pos, $dir){
    //count the original number of rows
    $count = count($array);
    //set the integer we will use to place the moved item
    if($dir=="up"){
        if($pos==1){
            //if the item is already first and we try moving it up
            //we send it to the end of the array
            $change = $count;
        }else{
            //anywhere else it just moves back one closer to the start of the array
            $change = $pos-1;
        }
    }
    //do the same for the down button
    if($dir=="dn"){
        if($pos==$count){
            $change = 1;
        }else{
            $change = $pos+1;
        }
    }
    //copy the element that you wish to move
    $move = $array[$keytomove];
    //delete the original from the main array
    unset($array[$keytomove]);
    //create an array of the names of the values we
    //are not moving
    $preint = 1;
    foreach($array as $c){
        $notmoved["{$preint}"] = $c['name'];
    $preint++;
    }
    //loop through all the elements
    $int = 1;
    while($int<=$count){
        //dynamically change the key of the unmoved item as we increment the counter
        $notmovedkey = $notmoved["$int"];
        //when the counter is equal to the position we want
        //to place the moved entry we pump it into a new array
        if($int==$change){
            $neworder["{$keytomove}"] = $move;
        }
        //add all the other array items if the position number is not met and
        //resume adding them once the moved item is written
        if($contkey!=""){
            $neworder["{$notmovedkey}"] = $array["{$notmovedkey}"];
        }
    $int++;
    }

    return($neworder);
}
?>

This is not too elegant but it works.
Sam Yong - hellclanner at live dot com 24-Aug-2009 02:05
Take note that end() does not recursively set your multiple dimension arrays' pointer to the end.

Take a look at the following:
<?php

// create the array for testing
$a = array();
$i = 0;
while($i++ < 3){
$a[] = array(dechex(crc32(mt_rand())),dechex(crc32('lol'.mt_rand())));
}

// show the array tree
echo '<pre>';var_dump($a);

// set the pointer of $a to the end
end($a);

// get the current element of $a
var_dump(current($a));
// get the current element of the current element of $a
var_dump(current(current($a)));

?>

You will notice that you probably get something like this:

array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(8) "764d8d20"
    [1]=>
    string(8) "85ee186d"
  }
  [1]=>
  array(2) {
    [0]=>
    string(8) "c8c72466"
    [1]=>
    string(8) "a0fdccb2"
  }
  [2]=>
  array(2) {
    [0]=>
    string(8) "3463a31b"
    [1]=>
    string(8) "589f6b63"
  }
}

array(2) {
  [0]=>
  string(8) "3463a31b"
  [1]=>
  string(8) "589f6b63"
}

string(8) "3463a31b"

The array elements' pointer are still at the first one as current. So do take note.
jasper at jtey dot com 11-Jul-2006 09:48
This function returns the value at the end of the array, but you may sometimes be interested in the key at the end of the array, particularly when working with non integer indexed arrays:

<?php
// Returns the key at the end of the array
function endKey($array){
 end($array);
 return key($array);
}
?>

Usage example:
<?php
$a = array("one" => "apple", "two" => "orange", "three" => "pear");
echo endKey($a); // will output "three"
?>
ken at expitrans dot com 27-Oct-2005 06:02
Please note that from version 5.0.4 ==> 5.0.5 that this function now takes an array. This will possibly break some code for instance:

<?php

echo ">> ".end(array_keys(array('x' => 'y')))."\n";

?>

which will return "Fatal error: Only variables can be passed by reference" in version <= 5.0.4 but not in 5.0.5.

If you run into this problem with nested function calls, then an easy workaround is to assign the result from array_keys (or whatever function) to an intermediary variable:

<?php

$x = array_keys(array('x' => 'y'));
echo ">> ".end($x)."\n";

?>
29-Aug-2002 03:34
If you need to get a reference on the first or last element of an array, use these functions because reset() and end() only return you a copy that you cannot dereference directly:

<?php
function first(&$array) {
if (!is_array($array)) return &$array;
if (!count($array)) return null;
reset($array);
return &$array[key($array)];
}

function last(&$array) {
if (!is_array($array)) return &$array;
if (!count($array)) return null;
end($array);
return &$array[key($array)];
}
?>
29-Aug-2002 03:17
When adding an element to an array, it may be interesting to know with which key it was added. Just adding an element does not change the current position in the array, so calling key() won't return the correct key value; you must first position at end() of the array:

<?php
function array_add(&$array, $value) {
$array[] = $value; // add an element
end($array); // important!
return key($array);
}
?>


배열에서 현재 심볼 테이블로 변수를 입력

  • ≪ end
  • in_array ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열에서 현재 심볼 테이블로 변수를 입력

extract

(PHP 4, PHP 5, PHP 7)

extract — 배열에서 현재 심볼 테이블로 변수를 입력

설명

int extract ( array $var_array [, int $extract_type [, string $prefix ]] )

배열에서 현재 심볼 테이블로 변수를 가져옵니다.

extract()는 각 키가 유효한 변수명이 되는지 확인합니다. 심볼 테이블에 존재하는 변수와의 충돌 여부도 확인합니다.

인수

var_array

연관 배열. 변수명을 키로, 변수값을 값으로 취급한다. extract_type과 prefix 인수에 따라 각 키/값 쌍에 대해서 현재 심볼 테이블안에 변수를 생성한다.

연관 배열을 사용해야 합니다. 숫자 인덱스 배열은 EXTR_PREFIX_ALL이나 EXTR_PREFIX_INVALID를 사용하지 않는 한 결과를 생성하지 않습니다.

extract_type

유효하지않은/숫자 키와 충돌인 경우를 취급하는 방법은 extract_type에 의해 결정된다. 다음 값들 중 하나가 될 수 있다:

EXTR_OVERWRITE
충돌이 발생하면, 기존 변수를 덮어쓴다.
EXTR_SKIP
충돌이 발생하면, 기존 변수를 덮어쓰지 않는다. variable.
EXTR_PREFIX_SAME
충돌이 발생하면, prefix를 변수명 앞에 첨가한다.
EXTR_PREFIX_ALL
prefix를 모든 변수명 앞에 첨가한다.
EXTR_PREFIX_INVALID
유효하지 않은/숫자 변수명 앞에만 prefix를 첨가한다.
EXTR_IF_EXISTS
현재 심볼 테이블에 이미 존재하는 변수만 덮어쓴다. 그렇지 않으면 아무것도 하지 않는다. 이 플래그는 유효한 변수 목록을 정의하고 이 변수들만 추출하는데 유용하다. 예를 들어, 이런 변수는 $_REQUEST에서 정의된 변수들이다.
EXTR_PREFIX_IF_EXISTS
현재 심볼 테이블에 앞첨가된 버전의 같은 변수가 존재할때만 앞첨가된 변수명을 생성한다.
EXTR_REFS
변수를 참조로써 추출한다. 입력된 변수 값이 var_array 인수의 값을 참조한다는 의미를 갖는다. 이 플래그는 그 자체로나 다른 플래그와 OR 연산하여 extract_type에서 사용할수 있다.

extract_type가 설정되지 않으면, EXTR_OVERWRITE가 설정되어 있다고 가정한다.

prefix

prefix는 extract_type이 EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, EXTR_PREFIX_IF_EXISTS일 경우에만 요구된다. 덧붙인 결과가 유효한 변수명이 아니면, 심볼 테이블에 입력되지 않는다. Prefix는 밑줄 문자로 배열 키와 자동으로 분리됩니다.

반환값

심볼 테이블에 성공적으로 입력된 변수의 수를 반환한다.

변경점

버전 | 설명
4.3.0 | EXTR_REFS 추가.
4.2.0 | EXTR_IF_EXISTS와 EXTR_PREFIX_IF_EXISTS 추가.
4.0.5 | 추출한 변수의 수를 반환합니다. EXTR_PREFIX_INVALID 추가. EXTR_PREFIX_ALL이 숫자 변수도 포함합니다.

예제

Example #1 extract() 예제

wddx_deserialize()에서 반환한 연관 배열에 포함되어 있는 변수들을 심볼 테이블로 가져오기 위해서 extract()를 사용할 수 있습니다.

<?php

/* $var_array가 wddx_deserialize에서 반환한
   배열이라고 가정합시다 */

$size = "large";
$var_array = array("color" => "blue",
                   "size"  => "medium",
                   "shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");

echo "$color, $size, $shape, $wddx_size\n";

?>

위 예제의 출력:

blue, large, sphere, medium

$size는 덮어씌어지지 않았다, 왜냐하면 EXTR_PREFIX_SAME를 설정했기 때문이다. 결과적으로 $wddx_size가 생성되었다. EXTR_SKIP가 설정되어 있으면, EXTR_OVERWRITE 는 $size가 "medium" 값을 갖게 하고 EXTR_PREFIX_ALL는 새로운 변수인 $wddx_color, $wddx_size, $wddx_shape를 갖게한다.

주의

Warning

extract()를 사용자 입력($_GET, ...)같은 신뢰할 수 없는 데이터에 사용하지 마십시오. 예를 들어, register_globals에 의존하는 오래된 코드를 실행하기 위해 임시적으로 사용한다면, EXTR_SKIP 등의 덮어쓰지 않는 extract_type을 사용하고, php.ini 안에 정의되어 있는 variables_order와 같은 순서로 추출해야 합니다.

참고

  • compact() - 변수와 그 값을 가지는 배열 생성

User Contributed Notes

bob 19-Jan-2015 05:35
Note that extract() will only create or overwrite variables in the current scope, so
<?
function test(){
 $a=Array('b'=>1,'c'=>2);
 extract($a);
}
test();
exit("$b");
?>
will produce no output, whereas
<?
function test(){
 global $b;
 $a=Array('b'=>1,'c'=>2);
 extract($a);
}
test();
exit("$b");
?>
will output 1.
dmikam 18-Sep-2014 07:18
I have made some tests to compare the speed of next constructions:
<?php

extract($ARRAY);

// vs.

foreach($ARRAY as $key=>$value)
    $$key = $value;
?>

Surprisingly for me extract is 20%-80% slower then foreach construction. I don't really understand why, but it's so.
CertaiN 11-Jan-2014 01:38
[New Version]

Example Usage:
<?php
$_GET['A']['a'] = '  CORRECT(including some spaces)    ';
$_GET['A']['b'] = '  CORRECT(including some spaces)    ';
$_GET['A']['c'] = "Invalid UTF-8 sequence: \xe3\xe3\xe3";
$_GET['A']['d']['invalid_structure'] = 'INVALID';

$_GET['B']['a'] = '  CORRECT(including some spaces)    ';
$_GET['B']['b'] = "Invalid UTF-8 sequence: \xe3\xe3\xe3";
$_GET['B']['c']['invalid_structure'] = 'INVALID';
$_GET['B']["Invalid UTF-8 sequence: \xe3\xe3\xe3"] = 'INVALID';

$_GET['C']['a'] = '  CORRECT(including some spaces)    ';
$_GET['C']['b'] = "Invalid UTF-8 sequence: \xe3\xe3\xe3";
$_GET['C']['c']['invalid_structure'] = 'INVALID';
$_GET['C']["Invalid UTF-8 sequence: \xe3\xe3\xe3"] = 'INVALID';

$_GET['unneeded_item'] = 'UNNEEDED';

var_dump(filter_struct_utf8(INPUT_GET, array(
    'A' => array(
        'a' => '',
        'b' => FILTER_STRUCT_TRIM,
        'c' => '',
        'd' => '',
    ),
    'B' => FILTER_STRUCT_FORCE_ARRAY,
    'C' => FILTER_STRUCT_FORCE_ARRAY | FILTER_STRUCT_TRIM,
)));
?>

Example Result:
array(3) {
  ["A"]=>
  array(4) {
    ["a"]=>
    string(36) "  CORRECT(including some spaces)    "
    ["b"]=>
    string(30) "CORRECT(including some spaces)"
    ["c"]=>
    string(0) ""
    ["d"]=>
    string(0) ""
  }
  ["B"]=>
  array(3) {
    ["a"]=>
    string(36) "  CORRECT(including some spaces)    "
    ["b"]=>
    string(0) ""
    ["c"]=>
    string(0) ""
  }
  ["C"]=>
  array(3) {
    ["a"]=>
    string(30) "CORRECT(including some spaces)"
    ["b"]=>
    string(0) ""
    ["c"]=>
    string(0) ""
  }
}
CertaiN 11-Jan-2014 01:37
[New Version]
This function is very useful for filtering complicated array structure.
Also, Some integer bitmasks and invalid UTF-8 sequence detection are available.

Code:
<?php
/**
 * @param  integer $type    Constant like INPUT_XXX.
 * @param  array   $default Default structure of the specified super global var.
 *                          Following bitmasks are available:
 *  + FILTER_STRUCT_FORCE_ARRAY - Force 1 dimensional array.
 *  + FILTER_STRUCT_TRIM        - Trim by ASCII control chars.
 *  + FILTER_STRUCT_FULL_TRIM   - Trim by ASCII control chars,
 *                                full-width and no-break space.
 * @return array            The value of the filtered super global var.
 */
define('FILTER_STRUCT_FORCE_ARRAY', 1);
define('FILTER_STRUCT_TRIM', 2);
define('FILTER_STRUCT_FULL_TRIM', 4);
function filter_struct_utf8($type, array $default) {
    static $func = __FUNCTION__;
    static $trim = "[\\x0-\x20\x7f]";
    static $ftrim = "[\\x0-\x20\x7f\xc2\xa0\xe3\x80\x80]";
    static $recursive_static = false;
    if (!$recursive = $recursive_static) {
        $types = array(
            INPUT_GET => $_GET,
            INPUT_POST => $_POST,
            INPUT_COOKIE => $_COOKIE,
            INPUT_REQUEST => $_REQUEST,
        );
        if (!isset($types[(int)$type])) {
            throw new LogicException('unknown super global var type');
        }
        $var = $types[(int)$type];
        $recursive_static = true;
    } else {
        $var = $type;
    }
    $ret = array();
    foreach ($default as $key => $value) {
        if ($is_int = is_int($value)) {
            if (!($value | (
                FILTER_STRUCT_FORCE_ARRAY |
                FILTER_STRUCT_FULL_TRIM |
                FILTER_STRUCT_TRIM
            ))) {
                $recursive_static = false;
                throw new LogicException('unknown bitmask');
            }
            if ($value & FILTER_STRUCT_FORCE_ARRAY) {
                $tmp = array();
                if (isset($var[$key])) {
                    foreach ((array)$var[$key] as $k => $v) {
                        if (!preg_match('//u', $k)){
                            continue;
                        }
                        $value &= FILTER_STRUCT_FULL_TRIM | FILTER_STRUCT_TRIM;
                        $tmp += array($k => $value ? $value : '');
                    }
                }
                $value = $tmp;
            }
        }
        if ($isset = isset($var[$key]) and is_array($value)) {
            $ret[$key] = $func($var[$key], $value);
        } elseif (!$isset || is_array($var[$key])) {
            $ret[$key] = null;
        } elseif ($is_int && $value & FILTER_STRUCT_FULL_TRIM) {
            $ret[$key] = preg_replace("/\A{$ftrim}++|{$ftrim}++\z/u", '', $var[$key]);
        } elseif ($is_int && $value & FILTER_STRUCT_TRIM) {
            $ret[$key] = preg_replace("/\A{$trim}++|{$trim}++\z/u", '', $var[$key]);
        } else {
            $ret[$key] = preg_replace('//u', '', $var[$key]);
        }
        if ($ret[$key] === null) {
            $ret[$key] = $is_int ? '' : $value;
        }
    }
    if (!$recursive) {
        $recursive_static = false;
    }
    return $ret;
}
?>
FredLawl 13-May-2013 11:56
It is possible to use this as a way to create public attributes for a class.

<?php
class Foo {

  public function __construct ($array) {

      extract($array, EXTR_REFS);
      foreach ($array as $key => $value) {
          $this->$key = $$key;
          // Do: $this->key = $key; if $key is not a string.
      }

  }

}

$array = array(
    'valueOne'      => 'Test Value 1',
    'valueTwo'      => 'Test Value 2',
    'valueThree'    => 'Test Value 3'
);

$foo = new Foo($array);

// Works
echo $foo->valueOne; // Test Value 1
echo $foo->valueTwo; // Test Value 2

// Does not work!
echo $foo::$valueOne; // Fatal error:  Access to undeclared static property: Test::$valueOne
?>
dotslash.lu at gmail.com 29-Apr-2013 09:55
You can't extract a numeric indexed array(e.g. non-assoc array).
<?php
$a = array(
  1,
  2
);
extract($a);
var_dump(${1});
?>

result:
PHP Notice:  Undefined variable: 1 in /Users/Lutashi/t.php on line 7

Notice: Undefined variable: 1 in /Users/Lutashi/t.php on line 7
NULL
Robc 01-Nov-2011 03:30
When extracting from a row after a database query using for example:

$row = mysql_fetch_array($result, MYSQL_ASSOC)
extract($row);

I find that the resultant variables may not match the variable type in the database.  In particular I have found integers in the database may gettype() to string on the extracted variable.
Rodrigo 16-Jun-2010 06:41
Here's a way to use extract in $_FILES arrays without using register_gloabals on.

I started to use extract a few weeks ago, and my codes hasn't been so clean since then. The use of the arrays $_POST and $_GET is ok, but one missed doublequote causes a lot of trouble.

Besides I teach PHP in a school, and this function has made my examples easier.

<?php
if(isset($_FILES["file"])){
    extract($_FILES);
    extract($file);
    echo $name."<br>";
    echo $tmp_name."<br>";
    echo $size."<br>";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Documento sin t&iacute;tulo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="" method="post" enctype="multipart/form-data" name="form1">
  <p>
    <input type="file" name="file">
</p>
  <p>
    <input type="submit" name="Submit" value="Enviar">
</p>
</form>
</body>
</html>

Hope this can help anyone.
auto493097 at hushmail dot com 23-Feb-2010 07:07
I use XDebug with NetbeansIDE to for analyzing and developing PHP Code. When debugging an extract statement no new variables appeared in the variable's list. Although all variables created by extract could be examined by explicit watch items and single variables appeared as soon as an PHP script makes use of them I am not sure weather it is a wrong configuration, a feature or a bug in XDebug.
danbettles at yahoo dot co dot uk 24-Aug-2009 01:04
When using EXTR_PREFIX_ALL - and probably all the other EXTR_PREFIX_* constants - and a numerically-indexed array, extract() will add an underscore ("_") between the prefix and the index.

<?php

extract(array('foo', 'bar'), EXTR_PREFIX_ALL, 'var');

print_r(get_defined_vars());  // Reveals $var_0 = 'foo' and $var_1 = 'bar'
?>
benjaminATwebbutvecklarnaDOTse 17-Mar-2008 04:15
In the meantime, I'm using this:

// extract alternative
# extracts variables where new value is above the threshold or if old value is on or below the threshold (or var is not defined)
# an associative array is obviously the sane thing to pass
#
# I am absolutely certain someone will find obvious problems or errors with this
# I haven't even tried to compare other values than 0 so if you need to do that and surely finds obvious flaws,
# please mail me, I'd really like to know.
# benjaminATwebbutvecklarnaDOTse

// usage example:
# thrextract(mysql_fetch_assoc(mysql_query("SELECT preset_this,preset_that FROM site_preset WHERE ID = $site_id")));
# thrextract(mysql_fetch_assoc(mysql_query("SELECT preset_this,preset_that FROM category_preset WHERE ID = $category_id")));

function thrextract($arr,$thr = 0){
    foreach($arr as $key => $var){
        global $$key;
        if($var > $thr or $$key <= $thr) $$key = $var;
    }
}
benjaminATwebbutvecklarnaDOTse 17-Mar-2008 03:19
Re: anon at anon dot org, about extract() and null values

Personally I've found use extracting multiple resultsets from db where the latter would overwrite the previous when a variable is not null ( and optionally if its not >0 )

It would be useful if $extract_type was extended on top of these two:

EXTR_OVERWRITE
EXTR_SKIP

with something like this:

EXTR_OVERWRITE_NULL
 - If there is a collision, overwrite the existing variable if it is null

EXTR_OVERWRITE_0
 - Same thing but == 0 or null

EXTR_SKIP_NULL
 - If there is a collision, skip the new variable if the existing is not null

EXTR_SKIP_0
 - Same thing but == 0 or null

Those ought to cover a few good cases that aren't covered now.
Hayley Watson 14-Mar-2008 12:18
Dan O'Donnell's suggestion needs a third requirement to work as described:

c) No other variables are defined - especially variables that contain potentially sensitive information.

Without that condition the difference between extract() and assigning variables by hand (and the resulting security implications) should be obvious.

The only valid security step there is (b) - but you should be doing that anyway.
Dan O'Donnell 21-Apr-2007 09:25
Following up on ktwombley at gmail dot com's post:

Presumably one easy way of dealing with this security issue is to use the EXTR_IF_EXISTS flag and make sure

a) your define acceptable input variables beforehand (i.e. as empty variables)
b) Sanitise any user input to avoid unacceptable variable content.

If you do these two things, then I'm not sure I see the difference between extract($_REQUEST,EXTR_IF_EXISTS); and assigning each of the variables by hand.

I'm not talking here about the idea of storing the variables in a database, just the immediately necessary steps to allow you to use extract on REQUEST arrays with relative safety.
Dutchdavey 13-Mar-2007 09:26
I would draw your attention to the user note at the very end of this page regarding PREFIXES. The user points out that php adds a '_' to your prefixes.
ktwombley at gmail dot com 31-Aug-2006 07:05
It's really easy to open gaping security holes using extract() on $_REQUEST, $_GET, etc. You have to be really sure of what you're doing, and use the proper flags on extract() to avoid clobbering important variables.

For instance, the submission by kake26 at gmail dot com will not only perfectly emulate register globals (that's bad), but it'll store it in a database and recall the same variables every time the script runs (essentially allowing an attacker to attack your script every time it runs via one attack). Oops!

To fix it, you'd have to get creative with flags. Maybe you could use EXTR_PREFIX_ALL instead of EXTR_OVERWRITE, for example. Of course, you should also sanitize the form elements to ensure there's no php code in them, and also to make sure any very important variables aren't in the form data. (like the classic $is_admin = true attack)
nicolas zeh 16-Mar-2006 06:24
This function provides exactly the same functionality as extract except that a parameter was added defining the extract target.
This function can be used if your PHP installation does not support the required Flags or more important if you would like to extract arrays to another destination as to $GLOBALS, i.e. other arrays or objects.
The only difference to extract is that extract_to moves the array pointer of $arr to the end as $arr is passed by reference to support the EXTR_REFS flag.

<?php
    if( !defined('EXTR_PREFIX_ALL') ) define('EXTR_PREFIX_ALL', 3);
    if( !defined('EXTR_PREFIX_INVALID') ) define('EXTR_PREFIX_INVALID', 4);
    if( !defined('EXTR_IF_EXISTS') ) define('EXTR_IF_EXISTS', 5);
    if( !defined('EXTR_PREFIX_IF_EXISTS') ) define('EXTR_PREFIX_IF_EXISTS', 6);
    if( !defined('EXTR_REFS') ) define('EXTR_REFS', 256);

    function extract_to( &$arr, &$to, $type=EXTR_OVERWRITE, $prefix=false ){

        if( !is_array( $arr ) ) return trigger_error("extract_to(): First argument should be an array", E_USER_WARNING );

        if( is_array( $to ) ) $t=0;
        else if( is_object( $to ) ) $t=1;
        else return trigger_error("extract_to(): Second argument should be an array or object", E_USER_WARNING );

        if( $type==EXTR_PREFIX_SAME || $type==EXTR_PREFIX_ALL || $type==EXTR_PREFIX_INVALID || $type==EXTR_PREFIX_IF_EXISTS )
            if( $prefix===false ) return trigger_error("extract_to(): Prefix expected to be specified", E_USER_WARNING );
            else $prefix .= '_';

        $i=0;
        foreach( $arr as $key=>$val ){

            $nkey = $key;
            $isset = $t==1 ? isset( $to[$key] ) : isset( $to->$key );

            if( ( $type==EXTR_SKIP && $isset )
                || ( $type==EXTR_IF_EXISTS && !$isset ) )
                    continue;

            else if( ( $type==EXTR_PREFIX_SAME && $isset )
                || ( $type==EXTR_PREFIX_ALL )
                || ( $type==EXTR_PREFIX_INVALID && !preg_match( '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#', $key ) ) )
                    $nkey = $prefix.$key;

            else if( $type==EXTR_PREFIX_IF_EXISTS )
                if( $isset ) $nkey = $prefix.$key;
                else continue;

            if( !preg_match( '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#', $nkey ) ) continue;

            if( $t==1 )
                if( $type & EXTR_REFS ) $to->$nkey = &$arr[$key];
                else $to->$nkey = $val;
            else
                if( $type & EXTR_REFS ) $to[$nkey] = &$arr[$key];
                else $to[$nkey] = $val;

            $i++;
        }

        return $i;
    }

    // e.g.:
    extract_to( $myarray, $myobject, EXTR_IF_EXISTS );
?>
owk dot ch199_ph at gadz dot org 08-Mar-2006 01:21
And if you want with PHP 5 an easy way to extract $V by reference, try this :
<?php
    foreach ($V as $k => &$v) {
        $$k =& $v;
    }
?>
It can be used to create special kind of "free args" functions that let you choose when you call them the way you send variables, and which ones. They are moreover very fast to call thanks to references :
<?php
    function free_args (&$V) {
        foreach ($V as $k => &$v) {
            $$k =& $v;
        }
        unset ($k);  unset ($v);  unset ($V);

        // be careful that if you need to extract $k, $v or $V variables you should find other names for them in the lines above (ie. $__k, $__v and $__V)
    }

    $huge_text = '...';

    $a = array ('arg1' => 'val1', 'arg2' => &$huge_text); // in this call, only $arg2 will be a true reference in the function
    free_args ($a);
?>
Be warned that you can't write : "<?php free_args (array ('arg1' => 'val1')); ?>" because the array can't be referenced by the function, as it's not yet created when the function starts.
moslehi<atsign>gmail<d0t>c0m 20-Jan-2006 10:32
Experimentally I found that calling extract() also shows the number of keys if the key is set and is not numeric ! Maybe there was a better definition than mine  . Please have a look to this scripts :

<?PHP
$var["i"] = "a";
$var["j"] = "b";
$var["k"] = 1;
echo extract($var); // returns 3
?>

<?PHP
$var2["i"] = "a";
$var2[2] = "b";
$var2[] = 1;
echo extract($var2); // returns 1
?>

(Arash Moslehi)
Csaba at alum dot mit dot edu 27-Nov-2005 08:41
Sometimes you may want to extract only a named subset of the key/value pairs in an array.  This keeps things more orderly and could prevent an unrelated variable from getting clobbered from an errant key.  For example,

$things = 'unsaid';
$REQUEST = array(He=>This, said=>1, my=>is, info=>2, had=>a,
                 very=>3, important=>test, things=>4);
$aVarToExtract = array(my, important, info);
extract (array_intersect_key ($REQUEST, array_flip($aVarToExtract)));

will extract
$my = 'is';
$important = 'test';
$info = 2;

but will leave certain
$things = 'unsaid'

Csaba Gabor from Vienna
NB.  Of course the composite request coming in from a web page is in $_REQUEST.
anon at anon dot org 30-May-2005 09:02
A warning about extract() and null values.

This might be an actual Zend2 Engine bug, but it's bad programming practice, so I'm sharing it here instead.

I often work in envrionments where E_STRICT (which would prevent errors like this) isn't on, and I don't have access to change it. I also use a very simple template class that in a nutshell works like this:

$t = new Template('somefile.php');
$t->title = $title;
$t->body = $body;
$t->display();

display() more or less looks like this:

function display(){
    extract(get_object_vars($this),EXTR_REFS);
    ob_start(); include $this->templateFileName;
    return ob_get_clean();
}

If any of the assigned values are null (let's say that in this case $title wasn't initialized above) it causes the engine to do all sorts of incredibly whacky stuff like certifiably lose track of variables in an incredibly inconsistent way. I traced the problem down to the fact that it's using the EXTR_REFS flag. I assume that in PHP's internal variable storage or reference counting mechanism, that trying to extract null references makes it lose track or count of something or rather.

In a nutshell, if you start getting wierd behavior when using extract() make sure that the array or object you are trying to get variables out of doesn't contain null keys or values!
kake26 at gmail dot com 01-May-2005 05:59
The following is a neat use for extract to store and manipulate large amounts of form data from. I basically loop through the $_POST and implode it seperating the key and value pairs by a space. Then store it in a db, the reversing function basically explodes the string to a array. Then converts the indexed array to a associative array then uses extract to seal the deal and make it easily available within a program. My main reason for sharing these are the fact I make some big web applications that store allot of forum data in a DB and these functions make it very easy to quickly and easily store and recall the data. I've contributed it because I spent many hours creating this code and recall going "I wish someone had previously submitted it to the page notes". Would have saved me allot of time and agony and I'm sure I'm not the only person that could really benefit from it, so I decided to share.

<?php
$stack = array();
foreach ($_POST as $key => $value) {
array_push($stack, $key, $value);
}
// store it
$block = implode(" ",$stack); // yeilds a space delimited string
// insert query to store string in DB here, like the one below
$query = "INSERT INTO `sometable` VALUES('".$seluser."','".addslashes($block)."');";
$result = mysql_query($query) or die("Query failed for block insert: " . mysql_error());
// note $seluser in my case is a user ID associated with that block
// in one of my web apps
?>

The nice thing is with the above we can quickly create a string of key and value pairs from the data the script got. Without really caring what their names are. You know how if register globals are on you say $someformvar rather than $_POST["someformvar"]; , basically the code below reads this previous created block returns it to that state. Sort of like presistant register globals.

<?php
// insert query to grab the previously stored string here
$query = "SELECT * FROM `sometable` WHERE `blockid` = '".addslashes($bid)."';";
$result = mysql_query($query) or die("Query failed read: " . mysql_error());
$sql = mysql_fetch_array($result, MYSQL_ASSOC);
$array = eplode(" ",$sql["data"]);
for ($i = 0; $i < sizeof($array); $i+=2) {
$myassoc[$array[$i]] = isset($array[$i+1])?$array[$i+1]:NULL;
}
extract($myassoc, EXTR_OVERWRITE);
// now you're key and value pairs from $_POST have been restored
// instead of $_POST
?>
pg dot perfection at gmail dot com 14-Mar-2005 11:33
Here is a little example of how an extraction method should look like when it needs to work recursive (work on nested_arrays too)...

Note that this is only an example, it can be done more easily, and more advanced too.

<?php
/**
 * A nested version of the extract () function.
 *
 * @param    array    $array        The array which to extract the variables from
 * @param    int        $type        The type to use to overwrite (follows the same as extract () on PHP 5.0.3
 * @param    string    $prefix        The prefix to be used for a variable when necessary
 */
function extract_nested (&$array, $type = EXTR_OVERWRITE, $prefix = '')
{
    /**
     *  Is the array really an array?
     */
    if (!is_array ($array))
    {
        return trigger_error ('extract_nested (): First argument should be an array', E_USER_WARNING);
    }

    /**
     *  If the prefix is set, check if the prefix matches an acceptable regex pattern
     * (the one used for variables)
     */
    if (!empty ($prefix) && !preg_match ('#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#', $prefix))
    {
        return trigger_error ('extract_nested (): Third argument should start with a letter or an underscore', E_USER_WARNING);
    }

    /**
     * Check if a prefix is necessary. If so and it is empty return an error.
     */
    if (($type == EXTR_PREFIX_SAME || $type == EXTR_PREFIX_ALL || $type == EXTR_PREFIX_IF_EXISTS) && empty ($prefix))
    {
        return trigger_error ('extract_nested (): Prefix expected to be specified', E_USER_WARNING);
    }

    /**
     * Make sure the prefix is oke
     */
    $prefix = $prefix . '_';

    /**
     *  Loop thru the array
     */
    foreach ($array as $key => $val)
    {
        /**
         *  If the key isn't an array extract it as we need to do
         */
        if (!is_array ($array[$key]))
        {
            switch ($type)
            {
                default:
                case EXTR_OVERWRITE:
                    $GLOBALS[$key] = $val;
                break;
                case EXTR_SKIP:
                    $GLOBALS[$key] = isset ($GLOBALS[$key]) ? $GLOBALS[$key] : $val;
                break;
                case EXTR_PREFIX_SAME:
                    if (isset ($GLOBALS[$key]))
                    {
                        $GLOBALS[$prefix . $key] = $val;
                    }
                    else
                    {
                        $GLOBALS[$key] = $val;
                    }
                break;
                case EXTR_PREFIX_ALL:
                    $GLOBALS[$prefix . $key] = $val;
                break;
                case EXTR_PREFIX_INVALID:
                    if (!preg_match ('#^[a-zA-Z_\x7f-\xff]$#', $key{0}))
                    {
                        $GLOBALS[$prefix . $key] = $val;
                    }
                    else
                    {
                        $GLOBALS[$key] = $val;
                    }
                break;
                case EXTR_IF_EXISTS:
                    if (isset ($GLOBALS[$key]))
                    {
                        $GLOBALS[$key] = $val;
                    }
                break;
                case EXTR_PREFIX_IF_EXISTS:
                    if (isset ($GLOBALS[$key]))
                    {
                        $GLOBALS[$prefix . $key] = $val;
                    }
                break;
                case EXTR_REFS:
                    $GLOBALS[$key] =& $array[$key];
                break;
            }
        }
        /**
         *  The key is an array... use the function on that index
         */
        else
        {
            extract_nested ($array[$key], $type, $prefix);
        }
    }
}
?>
Michael Newton 03-Mar-2005 02:23
They say "If the result is not a valid variable name, it is not imported into the symbol table."

What they should say is that if _any_ of the results have invalid names, _none_ of the variables get extracted.

Under 4.3.10 on Windows 2000, I was pulling some mySQL records, but needed to convert two fields into IP addresses:
<?
extract(mysql_fetch_assoc(mysql_query('SELECT * FROM foo')));
extract(mysql_fetch_assoc(mysql_query('SELECT INET_NTOA(bar) AS bar, INET_NTOA(baz) FROM foo')));
?>

I had forgotten the second AS modifier in the SQL query.  Because it couldn't extract a variable called INET_NTOA(baz) into the symbol table, it didn't do either of them.

(BTW I don't normally stack functions up like that!  Just to make a short example!)
22-Feb-2005 04:31
To make this perfectly clear (hopefully), an underscore is always added when the string is prefixed.
extract(array("color" => "blue"),EXTR_PREFIX_ALL,'');// note: prefix is empty
is the same as
$color='_blue';
Aaron Stone 17-Nov-2004 07:44
If you are working porting an older application, and taking the advice above, extracting only _SERVER, _SESSING, _COOKIE, _POST, _GET, you have forgotten to extract _FILES. Putting _FILES last and using EXTR_SKIP doesn't work because the name of the file upload box is already set as a variable containing only the temporary name of the uploaded file from one of the earlier extracts (I haven't tested to see which one specifically, however). A workaround is to put _FILES last and use EXTR_OVERWRITE. This allows extract to replace that temp-name-only variable with the full array of file upload information.
Adam Monsen <adamm at wazamatta dot com> 03-Oct-2004 02:03
As shown in the example, if your 'prefix' is used, a single underscore is added to the name of the extracted variable. Meaning, a prefix of 'p' becomes a prefix of 'p_', so 'blarg' prefixed would be 'p_blarg'.

If you're not sure what variables you've created through extraction, you can call get_defined_vars() to see all defined variables in the current scope.


값이 배열 안에 존재하는지 확인

  • ≪ extract
  • key_exists ≫
  • PHP Manual
  • 배열 함수 목록
  • 값이 배열 안에 존재하는지 확인

in_array

(PHP 4, PHP 5, PHP 7)

in_array — 값이 배열 안에 존재하는지 확인

설명

bool in_array ( mixed $needle , array $haystack [, bool $strict ] )

haystack에서 needle을 찾습니다.

인수

needle

찾을 값.

Note:

needle이 문자열이면, 대소문자를 구분하여 비교합니다.

haystack

배열.

strict

세번째 인수 strict를 TRUE로 설정하면, in_array() 함수는 haystack 안에서 needle의 자료형도 확인합니다.

반환값

needle을 배열에서 찾으면 TRUE를, 아니면 FALSE를 반환합니다.

변경점

버전 | 설명
4.2.0 | needle이 배열일 수 있습니다.

예제

Example #1 in_array() 예제

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
if (in_array("mac", $os)) {
    echo "Got mac";
}
?>

in_array()가 대소문자를 구분하므로 두번째 조건은 실패하고, 위 프로그램은 다음을 출력합니다:

Got Irix

Example #2 in_array()에 strict 예제

<?php
$a = array('1.10', 12.4, 1.13);

if (in_array('12.4', $a, true)) {
    echo "'12.4' found with strict check\n";
}

if (in_array(1.13, $a, true)) {
    echo "1.13 found with strict check\n";
}
?>

위 예제의 출력:

1.13 found with strict check

Example #3 in_array()에 needle로 배열

<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a)) {
    echo "'ph' was found\n";
}

if (in_array(array('f', 'i'), $a)) {
    echo "'fi' was found\n";
}

if (in_array('o', $a)) {
    echo "'o' was found\n";
}
?>

위 예제의 출력:

  'ph' was found
  'o' was found

참고

  • array_search() - 주어진 값으로 배열을 검색하여 성공시 해당하는 키를 반환
  • isset() - 설정된 변수인지 확인
  • array_key_exists() - 주어진 키와 인덱스가 배열에 존재하는지 확인

User Contributed Notes

tjamadeira at gmail dot com 30-Jan-2016 04:17
This function is for search a needle in a multidimensional haystack:

<?php
/**
 * A special function for search in a multidimensional array a needle
 *
 * @param mixed needle The searched variable
 * @param array haystack The array where search
 * @param bool strict It is or it isn't a strict search?
 *
 * @return bool
 **/
function in_array_r($needle, $haystack, $strict = false){
 foreach($haystack as $item){
   if(is_array($item)){
     if(in_array_r($needle, $item, $strict)){
       return true;
     }
   }else{
     if(($strict ? $needle === $item : $needle == $item)){
       return true;
     }
   }
 }
 return false;
}
?>
tjamadeira at gmail dot com 30-Jan-2016 02:19
A recursive function for a search in a multidimensional array using the in_array PHP function.

<?php
/**
 * A special function for search in a multidimensional array a needle
 *
 * This function uses many times the in_array PHP function
 *  to search a needle in a multidimensional haystack.
 *
 * @param mixed needle The searched variable
 * @param array haystack The array where search
 * @param bool strict It is or it isn't a strict search?
 *
 * @return bool
 **/
function in_array_r($needle, $haystack, $strict = false){
  if(in_array(array(), $haystack, $strict)){
    foreach($haystack as $item){
      if(is_array($item)){
        return in_array_r($needle, $item, $strict);
      }else {
        return in_array($needle, $haystack, $strict);
      }
    }
  }else {
    return in_array($needle, $haystack, $strict);
  }
}
?>
splogamurugan at gmail dot com 06-Nov-2015 08:49
var_dump(in_array('invalid', array(0,10,20)));
The above code gives true since the 'invalid' is getting converted to 0 and checked against the array(0,10,20)

but var_dump(in_array('invalid', array(10,20)));  gives 'false' since 0 not there in the array
dazero0 dot ls at gmail dot com 24-Sep-2015 07:43
I would like to add something to beingmrkenny at gmail dot com comparison post. After debugging a system, i discovered a security issue in our system and his post helped me find the problem.

In my additional testing i found out that not matter what you search for in an array, except for 0 and null, you get true as the result if the array contains true as the value.

Examples as php code :

<?php
$a = ['a', 32, true, 'x' => y];
var_dump(in_array(25, $a)); // true, one would expect false
var_dump(in_array('ggg', $a)); // true, one would expect false

var_dump(in_array(0, $a)); // false
var_dump(in_array(null, $a)); // false
?>

Such the best practice in our case is to use strict mode. Which was not so obvious.
Kris Spencer 09-Jun-2015 02:35
if using in_array to evaluate a boolean in a _GET or _POST, be sure to put single quotes around your "haystack values" or you will get a false result.

example of false/incorrect result:
[whatever.php?var=1 -- $_GET['var'] is 1]

if(!in_array($_GET['var'], array(0, 1), true)){
    echo 'not in array';
}
else{
    echo 'is in array';
}

output is "not in array" even though 1 === 1. Same is true without strict evaluation.

example of correct evaluation/result:
[whatever.php?var=1 -- $_GET['var'] is 1]

if(!in_array($_GET['var'], array('0', '1'), true)){
    echo 'not in array';
}
else{
    echo 'is in array';
}

output is now correct; "is in array".
Harry Willis 24-Jan-2015 08:39
Kelvin's case-insensitive in_arrayi is fine if you desire loose typing, but mapping strtolower onto the array will (attempt to) cast all array members to string. If you have an array of mixed types, and you wish to preserve the typing, the following will work:

<?php
function in_array_ci($needle, array $haystack, $strict = false) {
    foreach ($haystack as $element) {
        if (gettype($needle) == 'string' && gettype($element) == 'string') {
            if (!strcasecmp($needle, $element)) {
                return true;
            }
        }
        elseif ($strict) {
            if ($needle === $element) {
                return true;
            }
        }
        else {
            if ($needle == $element) {
                return true;
            }
        }
    }

    return false;
}
?>
Chaya Cooper 30-Sep-2014 07:33
To identify all instances of $needle when both $needle and $haystack are arrays, combine in_array() with a loop through the array  of needles.

I've posted a functional example of it here: http://sandbox.onlinephpfunctions.com/code/930385800c30595129cefb87cfa7eec9d69dce08

$needles = array('John', 'Alex');
$haystack = array('John','Alexander','Kim', 'Michael');

    foreach ($needles as $needle) {
        // Displays var name & value currently searching for)
        echo '$needle: '.$needle;
        if (in_array($needle, $haystack) ) {
            echo ', True; ';
        }
       else {
            echo ', False; ';
        }
    }

Result is:
$needle: John, True; $needle: Alex, False;
Chaya Cooper 30-Sep-2014 07:32
To identify all instances of $needle when both $needle and $haystack are arrays, combine in_array() with a loop through the array  of needles.

I've posted a functional example of it here: http://sandbox.onlinephpfunctions.com/code/930385800c30595129cefb87cfa7eec9d69dce08

$needles = array('John', 'Alex');
$haystack = array('John','Alexander','Kim', 'Michael');

    foreach ($needles as $needle) {
        // Displays var name & value currently searching for)
        echo '$needle: '.$needle;
        if (in_array($needle, $haystack) ) {
            echo ', True; ';
        }
       else {
            echo ', False; ';
        }
    }

Result is:
$needle: John, True; $needle: Alex, False;
i dot pinz1 at gmail dot com 15-May-2014 08:33
I have noted some strange behavior of the in_array() function if you have a hash like
$haystack=array (
'123'=>'some value',
'0125'=>'some value');

If I test now:
if (in_array('23',$haystack)) // here also TRUE is returned

I switched instead to
if(isset($haystack['23'])) // which return correct FALSE

Maybe this helps others too.
what loose checking means 20-Jan-2014 02:03
In a high-voted example, an array is given that contains, amongst other things, true, false and null, against which various variables are tested using in_array and loose checking.

It impossible to receive false as a return value from in_array using loose checking if your arrays contains both the constants true and false. You might understandably trip over this (or the inverse - passing boolean true to check against an array of e.g. non-empty strings), but it's certainly not counter intuitive and makes perfect sense.
manuel at sunred dot org 16-Jan-2014 06:00
If you are looking for a faster in_array function but still need the strict feature with which the type of element will be considered the following simple function might help you.

<?php
function fast_in_array($needle, array $haystack, $strict = false)
{
    $c = count($haystack);
    if($strict !== true)
    {
        for($i = 0; $i < $c; $i++)
            if($haystack[$i] == $needle)
                return true;
    }
    else
    {
        for($i = 0; $i < $c; $i++)
            if($haystack[$i] === $needle)
                return true;
    }
    return false;
}
?>
rolf dot dergham dot public at gmail dot com 13-Jan-2014 10:02
Watch out for this:

<?

print_r( (int) in_array('hello',array( 0 => 0)) );

?>

returns 1

Yes, it seems that is_array thinks that a random string and 0 are the same thing.
Excuse me, that's not loose checking, that's drunken logic.
Or maybe I found a bug?
Artur K 21-Aug-2013 04:52
As an addition to beingmrkennys note:

<?php

var_dump(in_array('012', array('12'))); //returns true
var_dump(in_array('012', array('12'), true)); //returns false

?>

Seems like PHP is making some cast. So use the strict parameter.
Fabian S 01-Aug-2013 11:52
Robin's fast_in_array uses binary search, so the array has to be sorted before, using the regular sort function.
santi dot morel at gmail dot com 23-May-2013 06:43
search in multidimensional array

    function in_multiarray($elem, $array) {
        foreach ($array as $key => $value) {
            if ($value==$elem){
                return true;
            }
            elseif(is_array($value)){
                if($this->in_multiarray($elem, $value))
                        return true;
            }
        }

        return false;
    }
Valerchik 13-May-2013 04:30
Beware when using this function to validate user input:

$a = array('0' => 'Opt 1', '1' => 'Opt 2', '2' => 'Opt 3');
$v = 'sql injection';
var_dump(in_array($v, array_keys($a)));

This will result : true;

array_keys($a) will cast array keys to int instead of string !
then when in_array will compare it will cast  'sql injection' to int 0 !
Beware of this!
justinjohnmathews at gmail dot com 05-Mar-2013 10:34
I think, "in_array" can be used with "range" function to check a number(dynamic value) is in between of other two numbers.

<?php
    $i = 5; // Dynamic value
    if (in_array(range(1, 10), $i)) {
       echo 'Your number is in between of range array';
    }
?>
janis dot janovskis at gmail dot com 29-Feb-2012 08:03
Since sometimes in_array returns strange results - see notes above.
I was able to find value in array by this quite a simple function;
<?php
/**
* $find <mixed> value to find
* $array<array> array to search in
*/

function _value_in_array($array, $find){
 $exists = FALSE;
 if(!is_array($array)){
   return;
}
foreach ($array as $key => $value) {
  if($find == $value){
       $exists = TRUE;
  }
}
  return $exists;
}

// Note
// You can't use wildcards and it does not check variable type
?>
rajeevroy15 at gmail dot com dot com 30-Nov-2011 01:51
is_array function checks only array only and giving incorrect result with multi-dimensional arrays.

Here is a custom function which will give the solution to check Array or Object and Checking of multi-dimensional arrays and objects as well.

<?php

function in_object($val, $obj){

    if($val == ""){
        trigger_error("in_object expects parameter 1 must not empty", E_USER_WARNING);
        return false;
    }
    if(!is_object($obj)){
        $obj = (object)$obj;
    }

    foreach($obj as $key => $value){
        if(!is_object($value) && !is_array($value)){
            if($value == $val){
                return true;
            }
        }else{
            return in_object($val, $value);
        }
    }
    return false;
}
?>
Usage  :
<?php

$array = array("a", "b", "c"=>array("x", "y"=>array("p", "q"=>"r")));

if(in_object("r", $arrX)){
    echo "r is there ";
}else{
    echo "Its not there ";
}
?>
beingmrkenny at gmail dot com 27-Oct-2011 12:35
Loose checking returns some crazy, counter-intuitive results when used with certain arrays. It is completely correct behaviour, due to PHP's leniency on variable types, but in "real-life" is almost useless.

The solution is to use the strict checking option.

<?php

// Example array

$array = array(
    'egg' => true,
    'cheese' => false,
    'hair' => 765,
    'goblins' => null,
    'ogres' => 'no ogres allowed in this array'
);

// Loose checking -- return values are in comments

// First three make sense, last four do not

in_array(null, $array); // true
in_array(false, $array); // true
in_array(765, $array); // true
in_array(763, $array); // true
in_array('egg', $array); // true
in_array('hhh', $array); // true
in_array(array(), $array); // true

// Strict checking

in_array(null, $array, true); // true
in_array(false, $array, true); // true
in_array(765, $array, true); // true
in_array(763, $array, true); // false
in_array('egg', $array, true); // false
in_array('hhh', $array, true); // false
in_array(array(), $array, true); // false

?>
chris at axxis dot gr 27-Sep-2011 04:49
It's funny how PHP has an in_array method, but not an in_object one. Here is a simple function which will check if a value can be found within an object.

<?php
  // in_object method
  // to check if a value in an object exists.
  function in_object($value,$object) {
    if (is_object($object)) {
      foreach($object as $key => $item) {
        if ($value==$item) return $key;
      }
    }
    return false;
  }
?>

This is very useful if an object has been created dynamically (especially from external code, which you don't control, as in an application-plugin, CMS, etc), and you don't know the object's properties.
The above function will return the property, so you will be able to use it in your code later on.

Here is a very good basic example of how useful this function is!

<?php
  class My_Class {
    function __construct($key, $value) {
      $this->$key = $value;
      // As you can see, this is a dynamic class, its properties and values can be unknown...
    }
  }

  function in_object($value,$object) {
    if (is_object($object)) {
      foreach($object as $key => $item) {
        if ($value==$item) return $key;
      }
    }
    return false;
  }

  function manipulate_property($value,$object) {
    if ($property = in_object($value,$object)) {
      // value found. I can now use this property.
      // I can simply echo'it (makes no sense, as I could instead simply echo "value")
      echo "<br />I found the property holding this value: ".$object->$property;
      // or (here comes the good part)
      // change the property
      $object->$property = "This is a changed value!";
      echo "<br />I changed the value to: ".$object->$property;
      // or return it for use in my program flow
      return $property;
    } else {
      echo "Value NOT FOUND!<br />";
      return false;
    }
  }

  // imagine if some function creates the class conditionally...
  if ( 1 == 1) {
    $class = new My_Class("property","Unchanged Value");
  } else {
    $class = new My_Class("property","Some Other Value");
  }

  // now let's check if the value we want exists, and if yes, let's have some fun with it...
  $property = manipulate_property("Unchanged Value",$class);
  if ($property) {
    $my_variable = $class->$property;
    echo "<br />This is my variable now:".$my_variable;
  } else $my_variable = $some_other_variable;
?>

Just run it to see for yourself!
Lea Hayes 05-Aug-2011 02:40
Determine whether an object field matches needle.

Usage Example:
---------------

<?php
$arr = array( new stdClass(), new stdClass() );
$arr[0]->colour = 'red';
$arr[1]->colour = 'green';
$arr[1]->state  = 'enabled';

if (in_array_field('red', 'colour', $arr))
   echo 'Item exists with colour red.';
if (in_array_field('magenta', 'colour', $arr))
   echo 'Item exists with colour magenta.';
if (in_array_field('enabled', 'state', $arr))
   echo 'Item exists with enabled state.';
?>

Output:
--------
Item exists with colour red.
Item exists with enabled state.

<?php
function in_array_field($needle, $needle_field, $haystack, $strict = false) {
    if ($strict) {
        foreach ($haystack as $item)
            if (isset($item->$needle_field) && $item->$needle_field === $needle)
                return true;
    }
    else {
        foreach ($haystack as $item)
            if (isset($item->$needle_field) && $item->$needle_field == $needle)
                return true;
    }
    return false;
}
?>
Ray dot Paseur at Gmail dot com 01-Aug-2011 06:20
Be VERY careful about testing for strings in arrays that might also contain integers.  In this snippet, the range() function turns the numeric values into integers, and loose data typing finds a match with zero.

Outputs:
PHP in_array() FOUND @#%?!!
@#%?!! == 0 AT POSITION 26 BECAUSE OF LOOSE TYPE

<?php // RAY_temp_in_array.php
error_reporting(E_ALL);
echo "<pre>";

// THE ALLOWABLE SELECTIONS
$alpha = range('A','Z');
$alpha = array_merge($alpha, range('0', '9'));

// LOOK FOR A BOGUS SELECTION
$str = '@#%?!!';
if (in_array($str, $alpha))
{
    echo "PHP in_array() FOUND $str ";
}
else
{
    echo "$str IS NOT IN ARRAY";
}

// SEARCH THE ARRAY FOR THE KEY OF THE STRING
$key = array_search($str, $alpha);
echo "<br/>$str == $alpha[$key] AT POSITION $key";

if ($str == 0) echo " BECAUSE OF LOOSE TYPE";
?>
Anonymous 18-Jun-2011 09:16
known issue
when checking a string vs a integer

$testarray = array(0,1,2,3,4);

in_array("bla", $a) returns true
in_array("bla6", $a) returns true
in_array("6bla", $a) returns false

"bla" as a integer value returns the number zero
"bla6" as a integer value returns the number zero
 "6bla" returns the number six as its the only vaild part of that

if you checking for unknown types eg string vs integer
then you would need the strict value to be true.
Joris Meijer 15-Feb-2011 02:31
A good option if your array contains objects and you wish to recursively search it (eg. with in_arrayr), is to use json_encode and json_decode first:

<?php

$array = json_decode(json_encode($array_with_objects), true);

?>

In my opinion this is more elegant than a recursive in_array function which converts the objects into arrays while searching for a key and/or value.
user at NOSPAM at fullservicead dot com 02-Feb-2011 05:52
After reading all other notes about performance,
I made a little test.

fill an array in a loop with random characters,
shuffle it
search on it using 4 methods.

I tried to have haystack as array and string; hence the strstr.

I tried it on 9, 99, 999, 9999 elements and the results
remain in the same order:

9999 elements

Function :  execution time in miliseconds.

strstr : 1.2994079589844
fast_in_array : 1.0655579566956
isset : 0.99128198623657 //Winner, used from another note here.
in_array : 2.9410798549652

One last detail, I moved my functions around in the script to make sure there was no memory caching, reiteration op or whatever, it's all the same. in_array is the slowest and the use of isset($array[$NEEDLE]) the fastest.

I hope it saves somebody else the trouble.
bosdilooda at gmail dot com 21-Jan-2011 09:07
There should be a big red warning sign here because this function behaves in mysterious ways...

let haystack be an array of integers:

$arr = array(1,2,3,4,5);

let $needle be a string of "2foobar"

then

in_array($needle,$arr)

will return true because string ("2foobar") evaluates to integer (2).
ctulek at gmail dot com 30-Aug-2010 08:07
If you have an array like:
$arr = array(0,1,2,3,4,5);

in_array(NULL, $arr) returns true because you have 0 in your array. That is, in_array does not use === for equal check.
Svinto 22-May-2010 08:53
in_array() will always return true if a value in the haystack is of type bool(true).
This always hold except when the needle is of type bool(false).

Code:
<?php
$haystack = array(true);
$needle = "anything except bool(false)";
$result = in_array($needle, $haystack);
var_dump($result); #Will always be true
?>

Solution:
run in_array() with strict set to true.
<?php
in_array($needle, $haystack, true);
?>

Tested on PHP 5.2.6
bogdan AT bogdanconstantinescu DOT com 21-Apr-2010 05:01
If you found yourself in need of a multidimensional array in_array like function you can use the one below. Works in a fair amount of time

<?php

    function in_multiarray($elem, $array)
    {
        $top = sizeof($array) - 1;
        $bottom = 0;
        while($bottom <= $top)
        {
            if($array[$bottom] == $elem)
                return true;
            else
                if(is_array($array[$bottom]))
                    if(in_multiarray($elem, ($array[$bottom])))
                        return true;

            $bottom++;
        }
        return false;
    }
?>
jv at vip dot ie 14-Feb-2010 01:46
If you're working with very large 2 dimensional arrays (eg 20,000+ elements) it's much faster to do this...

<?php
$needle = 'test for this';

$flipped_haystack = array_flip($haystack);

if ( isset($flipped_haystack[$needle]) )
{
  print "Yes it's there!";
}
?>

I had a script that went from 30+ seconds down to 2 seconds (when hunting through a 50,000 element array 50,000 times).

Remember to only flip it once at the beginning of your code though!
natan dot volkovich at gmail dot com 30-Nov-2009 11:10
If you're trying to find out whether or not at least a single value of an array matches a value in your haystack then use "array_intersect" instead of "in_array".

<?php
$needle = array(1,2);
$haystack = array(0,1,2);

echo "in_array: ".(int)in_array($needle, $haystack); // returns 0
echo "array_intersect: ".(int)array_intersect((array)$needle, $haystack); // returns 1
?>
thomas dot sahlin at gmail dot com 05-Oct-2009 09:53
If you're creating an array yourself and then using in_array to search it, consider setting the keys of the array and using isset instead since it's much faster.

<?php

$slow = array('apple', 'banana', 'orange');

if (in_array('banana', $slow))
    print('Found it!');

$fast = array('apple' => 'apple', 'banana' => 'banana', 'orange' => 'orange');

if (isset($fast['banana']))
    print('Found it!');

?>
robin at robinnixon dot com 25-Jul-2009 09:38
This function is five times faster than in_array(). It uses a binary search and should be able to be used as a direct replacement:

<?php
function fast_in_array($elem, $array)
{
   $top = sizeof($array) -1;
   $bot = 0;

   while($top >= $bot)
   {
      $p = floor(($top + $bot) / 2);
      if ($array[$p] < $elem) $bot = $p + 1;
      elseif ($array[$p] > $elem) $top = $p - 1;
      else return TRUE;
   }

   return FALSE;
}
?>
john at dwarven dot co dot uk 01-Jul-2009 01:34
I just struggled for a while with this, although it may be obvious to others.

If you have an array with mixed type content such as:

<?php

 $ary = array (
   1,
   "John",
   0,
   "Foo",
   "Bar"
 );

?>

be sure to use the strict checking when searching for a string in the array, or it will match on the 0 int in that array and give a true for all values of needle that are strings strings.

<?php

var_dump( in_array( 2, $ary ) );

// outputs FALSE

var_dump( in_array( 'Not in there', $ary ) );

// outputs TRUE

var_dump( in_array( 'Not in there', $ary, TRUE ) );

// outputs FALSE

?>
Thingmand 09-Mar-2009 08:58
A little function to use an array of needles:

<?php
function array_in_array($needles, $haystack) {

    foreach ($needles as $needle) {

        if ( in_array($needle, $haystack) ) {
            return true;
        }
    }

    return false;
}
?>
brouwer dot p at gmail dot com 08-Mar-2009 11:55
If made a in_array function that checks if the specified key matches. It works recursivly so it doesn't matter how deep your input array is.
<?php
  function myInArray($array, $value, $key){
    //loop through the array
    foreach ($array as $val) {
      //if $val is an array cal myInArray again with $val as array input
      if(is_array($val)){
        if(myInArray($val,$value,$key))
          return true;
      }
      //else check if the given key has $value as value
      else{
        if($array[$key]==$value)
          return true;
      }
    }
    return false;
  }
?>
Kelvin J 28-Feb-2009 02:04
For a case-insensitive in_array(), you can use array_map() to avoid a foreach statement, e.g.:

<?php
    function in_arrayi($needle, $haystack) {
        return in_array(strtolower($needle), array_map('strtolower', $haystack));
    }
?>
selmand [at] gmail.com 13-Feb-2009 01:38
Removes same text with in_array in a string.

<?

$hizmet="aeg,akai,aeg,arcelik,aeg,arcelik,klima,kombi";

// alots of same stings

$x=explode(",",$hizmet);

$t= array();
$k=0;
for($i=0;$i<sizeof($x);$i++){ // this for remove its
    //echo $x[$i]."\n";
    if(!in_array($x[$i],$t))
    {
        $t[$k]=$x[$i];
        $k++;
    }

}

for($i=0;$i<sizeof($t);$i++){ // rebuilding $hizmet strings.

    echo $t[$i].",";

}

?>
soxred93 at gmail dot com 28-Jan-2009 10:37
Here's a simple little function I wrote that is a case insensitive version of in_array():

<?php
    function in_arrayi( $needle, $haystack ) {
        $found = false;
        foreach( $haystack as $value ) {
            if( strtolower( $value ) == strtolower( $needle ) ) {
                $found = true;
            }
        }
        return $found;
    }
?>
jordigirones at gmail dot com 21-Jan-2009 04:54
function similar to in_array but implements LIKE '<string>%'

<?php
   function in_array_like($referencia,$array){
      foreach($array as $ref){
        if (strstr($referencia,$ref)){
          return true;
        }
      }
      return false;
    }
?>
rhill at xenu-directory dot net 17-Jan-2009 10:05
I found out that in_array will *not* find an associative array within a haystack of associative arrays in strict mode if the keys were not generated in the *same order*:

<?php

$needle = array(
    'fruit'=>'banana', 'vegetable'=>'carrot'
    );

$haystack = array(
    array('vegetable'=>'carrot', 'fruit'=>'banana'),
    array('fruit'=>'apple', 'vegetable'=>'celery')
    );

echo in_array($needle, $haystack, true) ? 'true' : 'false';
// Output is 'false'

echo in_array($needle, $haystack) ? 'true' : 'false';
// Output is 'true'

?>

I had wrongly assumed the order of the items in an associative array were irrelevant, regardless of whether 'strict' is TRUE or FALSE: The order is irrelevant *only* if not in strict mode.
james dot ellis at gmail dot com 30-Oct-2008 06:17
Be aware of oddities when dealing with 0 (zero) values in an array...

This script:
<?php
$array = array('testing',0,'name');
var_dump($array);
//this will return true
var_dump(in_array('foo', $array));
//this will return false
var_dump(in_array('foo', $array, TRUE));
?>

It seems in non strict mode, the 0 value in the array is evaluating to boolean FALSE and in_array returns TRUE. Use strict mode to work around this peculiarity.
This only seems to occur when there is an integer 0 in the array. A string '0' will return FALSE for the first test above (at least in 5.2.6).
james dot randell at hotmail dot co dot uk 16-Sep-2008 11:54
Small method i built for my Array module, after looking through the manual I wanted a small compact way of making a wildcard search through an arrays values, and returning only those that it found.

<?php

    /**
     * Takes a needle and haystack (just like in_array()) and does a wildcard search on it's values.
     *
     * @param    string        $string        Needle to find
     * @param    array        $array        Haystack to look through
     * @result    array                    Returns the elements that the $string was found in
     */
    function find ($string, $array = array ())
    {
        foreach ($array as $key => $value) {
            unset ($array[$key]);
            if (strpos($value, $string) !== false) {
                $array[$key] = $value;
            }
        }
        return $array;
    }
?>
crashrox at gmail dot com 21-Jul-2008 05:34
Recursive in array using SPL

<?php
function in_array_recursive($needle, $haystack) {

    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($haystack));

    foreach($it AS $element) {
        if($element == $needle) {
            return true;
        }
    }

    return false;
}
?>
Martijn Wieringa 19-May-2008 11:20
When using numbers as needle, it gets tricky:

Note this behaviour (3rd statement):

in_array(0, array(42)) = FALSE
in_array(0, array('42')) = FALSE
in_array(0, array('Foo')) = TRUE
in_array('0', array('Foo')) = FALSE
sick949 at hotmail dot com 05-Mar-2008 12:43
A first idea for a function that checks if a text is in a specific column of an array.
It does not use in_array function because it doesn't check via columns.
Its a test, could be much better. Do not use it without test.

<?php

function in_array_column($text, $column, $array)
{
    if (!empty($array) && is_array($array))
    {
        for ($i=0; $i < count($array); $i++)
        {
            if ($array[$i][$column]==$text || strcmp($array[$i][$column],$text)==0) return true;
        }
    }
    return false;
}

?>
guitar king 29-Jan-2008 08:52
In PHP 4, the first argument seems not allowed to be an object. In PHP 5, also objects are allowed as $needle.
info at b1g dot de 02-Aug-2007 07:44
Be careful with checking for "zero" in arrays when you are not in strict mode.
in_array(0, array()) == true
in_array(0, array(), true) == false
Quaquaversal 21-May-2007 05:48
A simple function to type less when wanting to check if any one of many values is in a single array.

<?php
function array_in_array($needle, $haystack) {
    //Make sure $needle is an array for foreach
    if(!is_array($needle)) $needle = array($needle);
    //For each value in $needle, return TRUE if in $haystack
    foreach($needle as $pin)
        if(in_array($pin, $haystack)) return TRUE;
    //Return FALSE if none of the values from $needle are found in $haystack
    return FALSE;
}
?>
musik at krapplack dot de 04-Jun-2006 02:52
I needed a version of in_array() that supports wildcards in the haystack. Here it is:

<?php
function my_inArray($needle, $haystack) {
    # this function allows wildcards in the array to be searched
    foreach ($haystack as $value) {
        if (true === fnmatch($value, $needle)) {
            return true;
        }
    }
    return false;
}

$haystack = array('*krapplack.de');
$needle = 'www.krapplack.de';

echo my_inArray($needle, $haystack); # outputs "true"
?>

Unfortunately, fnmatch() is not available on Windows or other non-POSIX compliant systems.

Cheers,
Thomas
rick at fawo dot nl 09-Apr-2006 05:23
Here's another deep_in_array function, but this one has a case-insensitive option :)
<?
function deep_in_array($value, $array, $case_insensitive = false){
    foreach($array as $item){
        if(is_array($item)) $ret = deep_in_array($value, $item, $case_insensitive);
        else $ret = ($case_insensitive) ? strtolower($item)==$value : $item==$value;
        if($ret)return $ret;
    }
    return false;
}
?>
sandrejev at gmail dot com 22-Feb-2006 04:11
Sorry, that deep_in_array() was a bit broken.

<?php
function deep_in_array($value, $array) {
    foreach($array as $item) {
        if(!is_array($item)) {
            if ($item == $value) return true;
            else continue;
        }

        if(in_array($value, $item)) return true;
        else if(deep_in_array($value, $item)) return true;
    }
    return false;
}
?>
kitchin 05-Feb-2006 03:52
Here's a gotcha, and another reason to always use strict with this function.

$x= array('this');
$test= in_array(0, $x);
var_dump($test); // true

$x= array(0);
$test= in_array('that', $x);
var_dump($test); // true

$x= array('0');
$test= in_array('that', $x);
var_dump($test); // false

It's hard to think of a reason to use this function *without* strict.

This is important for validating user input from a set of allowed values, such as from a <select> tag.
14-Jan-2006 06:44
in_arrayr -- Checks if the value is in an array recursively

Description
bool in_array (mixed needle, array haystack)

<?php
function in_arrayr($needle, $haystack) {
        foreach ($haystack as $v) {
                if ($needle == $v) return true;
                elseif (is_array($v)) return in_arrayr($needle, $v);
        }
        return false;
}
// i think it works
?>
adrian foeder 08-Nov-2005 10:21
hope this function may be useful to you, it checks an array recursively (if an array has sub-array-levels) and also the keys, if wanted:

<?php
function rec_in_array($needle, $haystack, $alsokeys=false)
    {
        if(!is_array($haystack)) return false;
        if(in_array($needle, $haystack) || ($alsokeys && in_array($needle, array_keys($haystack)) )) return true;
        else {
            foreach($haystack AS $element) {
                $ret = rec_in_array($needle, $element, $alsokeys);
            }
        }

        return $ret;
    }
?>
Aragorn5551 at gmx dot de 11-Jun-2005 02:26
If you have a multidimensional array filled only with Boolean values like me, you need to use 'strict', otherwise in_array() will return an unexpected result.

Example:

<?php
$error_arr = array('error_one' => FALSE, 'error_two' => FALSE, array('error_three' => FALSE, 'error_four' => FALSE));

if (in_array (TRUE, $error_arr)) {
   echo 'An error occurred';
}
else {
   echo 'No error occurred';
}
?>

This will return 'An error occurred' although theres no TRUE value inside the array in any dimension. With 'strict' the function will return the correct result 'No error occurred'.

Hope this helps somebody, cause it took me some time to figure this out.
tom at orbittechservices dot com 10-Aug-2002 04:17
I searched the general mailing list and found that in PHP versions before 4.2.0 needle was not allowed to be an array.

Here's how I solved it to check if a value is in_array to avoid duplicates;

<?php
$myArray = array(array('p', 'h'), array('p', 'r'));

$newValue = "q";
$newInsert = array('p','q');

$itBeInThere = 0;
foreach ($myArray as $currentValue) {
  if (in_array ($newValue, $currentValue)) {
    $itBeInThere = 1;
  }
if ($itBeInThere != 1) {
  array_unshift ($myArray, $newInsert);
}
?>


별칭: array_key_exists

  • ≪ in_array
  • key ≫
  • PHP Manual
  • 배열 함수 목록
  • 별칭: array_key_exists

key_exists

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

key_exists — 별칭: array_key_exists()

설명

이 함수는 다음 함수의 별칭입니다: array_key_exists().

User Contributed Notes

There are no user contributed notes for this page.


배열에서 키를 가져옵니다

  • ≪ key_exists
  • krsort ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열에서 키를 가져옵니다

key

(PHP 4, PHP 5, PHP 7)

key — 배열에서 키를 가져옵니다

설명

mixed key ( array &$array )

key()는 현재 배열 위치의 인덱스 요소를 반환합니다.

인수

array

배열.

반환값

인덱스를 반환합니다.

예제

Example #1 key() 예제

<?php
$array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

// 이 순환은 값이 "apple"인
// 모든 연관 배열 키를 출력합니다
while ($fruit_name = current($array)) {
    if ($fruit_name == 'apple') {
        echo key($array).'<br />';
    }
    next($array);
}
?>

위 예제의 출력:

fruit1<br />
fruit4<br />
fruit5<br />

참고

  • current() - 배열의 현재 원소를 반환
  • next() - 배열의 내부 배열 포인터를 전진

User Contributed Notes

vincphil54800 at gmail dot com 29-Sep-2016 07:13
If you want to return the key on an associative array to get the first occurence of this array for exemple and if you don't know the key of the element that you search, it's impossible to find it without create you're own fonction, you can use for example :

/**
     * Retourne la premiere cle/valeur d'un tableau (notament utile pour les tableaux associatifs)
     * @param array $tab un tableau
     * @param int $mode 0 => renvoie la premiere valeur du tableau, 1=> renvoie la premiere cle du tableau
     * @return mixed la premiere occurence d'un tableau
     */
    public static function firstKey($tab,$mode = 0)
    {
        foreach($tab as $key=>$values)
        {
            if(is_array($values))
            {
                self::firstKey($values);
            }
            if($mode == 0)
            {
                return $values;
            }
            elseif($mode == 1)
            {
                return $key;
            }
        }
        return null;
    }
Anonymous 27-Nov-2014 06:14
<?php
function generarCodigo($longitud) {
 $key = '';
 $pattern = '123456789ASKKDOQEMANDOASJDSO';
 $max = strlen($pattern)-1;
 for($i=0;$i < $longitud;$i++) $key .= $pattern{mt_rand(0,$max)};
 return $key;
}

echo generarCodigo(4)
echo "-";
echo generarCodigo(4);
echo "-";
echo generarCodigo(4);
echo "-";
echo generarCodigo(4);
?>
lhardie 24-Apr-2014 01:05
Note that using key($array) in a foreach loop may have unexpected results.

When requiring the key inside a foreach loop, you should use:
foreach($array as $key => $value)

I was incorrectly using:
<?php
foreach($array as $value)
{
  $mykey = key($array);
}
?>

and experiencing errors (the pointer of the array is already moved to the next item, so instead of getting the key for $value, you will get the key to the next value in the array)

CORRECT:
<?php
foreach($array as $key => $value)
{
  $mykey = $key;
}

A noob error, but felt it might help someone else out there.
vinob44 at gmail dot com 23-Oct-2013 08:43
Suppose if the array values are in numbers and numbers contains `0` then the loop will be terminated. To overcome this you can user like this

<?php
$array = array(
    '0' => '5',
    '1' => '2',
    '2' => '0',
    '3' => '3',
    '4' => '1');

// wrong approach

while ($fruit_name = current($array)) {

        echo key($array).'<br />';
       next($array);
}

// the way will be break loop when arra('2'=>0) because its value is '0', while(0) will terminate the loop

// correct approach
while ( ($fruit_name = current($array)) !== FALSE ) {

        echo key($array).'<br />';
       next($array);
}
//this will work properly
?>
FatBat 17-Oct-2011 09:20
Needed to get the index of the max/highest value in an assoc array.
max() only returned the value, no index, so I did this instead.

<?php
reset($x);   // optional.
arsort($x);
$key_of_max = key($x);   // returns the index.
?>


키에 의한 배열 역순 정렬

  • ≪ key
  • ksort ≫
  • PHP Manual
  • 배열 함수 목록
  • 키에 의한 배열 역순 정렬

krsort

(PHP 4, PHP 5, PHP 7)

krsort — 키에 의한 배열 역순 정렬

설명

bool krsort ( array &$array [, int $sort_flags ] )

역순으로 키에 의해 배열을 정렬하고, 키와 데이터의 연관성을 유지한다. 이 함수는 주로 연관배열에 유용하다.

인수

array

입력 배열.

sort_flags

선택적인 sort_flags 인수로 정렬 방법을 바꿀 수 있습니다. 자세한 내용은 sort()를 참고하십시오.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 krsort() 예제

<?php
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

위 예제의 출력:

d = lemon
c = apple
b = banana
a = orange

참고

  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • arsort() - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • ksort() - 키에 의한 배열 정렬
  • sort() - 배열 정렬
  • natsort() - "자연순" 알고리즘으로 배열 정렬
  • rsort() - 역순으로 배열 정렬

User Contributed Notes

08-Aug-2005 03:37
To create a natural reverse sorting by keys, use the following function:

<?php
function natkrsort($array)
{
    $keys = array_keys($array);
    natsort($keys);

    foreach ($keys as $k)
    {
        $new_array[$k] = $array[$k];
    }

    $new_array = array_reverse($new_array, true);

    return $new_array;
}
?>
peter at pmkmedia dot com 07-Nov-2003 10:51
Best deal sorting:

This is a function that will sort an array with integer keys (weight) and float values (cost) and delete 'bad deals' - entries that are more costly than other entries that have greater or equal weight.

Input: an array of unsorted weight/cost pairs
Output: none

function BEST_DEALS($myarray)
{   // most weight for least cost:
    // ? Peter Kionga-Kamau, http://www.pmkmedia.com
    // thanks to Nafeh for the reversal trick
    // free for unrestricted use.
    krsort($myarray, SORT_NUMERIC);
    while(list($weight, $cost) = each($myarray))
    {   // delete bad deals, retain best deals:
        if(!$lastweight)
        {
            $lastweight=$weight;
            $lastcost = $cost;
        }
        else if($cost >= $lastcost) unset($myarray[$weight]);
        else
        {
            $lastweight=$weight;
            $lastcost = $cost;
        }
    }
    ksort($myarray);
}


키에 의한 배열 정렬

  • ≪ krsort
  • list ≫
  • PHP Manual
  • 배열 함수 목록
  • 키에 의한 배열 정렬

ksort

(PHP 4, PHP 5, PHP 7)

ksort — 키에 의한 배열 정렬

설명

bool ksort ( array &$array [, int $sort_flags ] )

키에 의해 배열을 정렬하고, 키와 데이터의 연관성을 유지한다. 이 함수는 주로 연관 배열에 유용하다.

인수

array

입력 배열.

sort_flags

선택적인 sort_flags 인수로 정렬 방법을 바꿀 수 있습니다. 자세한 내용은 sort()를 참고하십시오.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
4.0.0 | 선택적인 sort_flags 인수 추가.

예제

Example #1 ksort() 예제

<?php
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

위 예제의 출력:

a = orange
b = banana
c = apple
d = lemon

참고

  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • arsort() - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • krsort() - 키에 의한 배열 역순 정렬
  • uksort() - 사용자 정의 비교 함수를 사용하여 키에 의한 배열 정렬
  • sort() - 배열 정렬
  • natsort() - "자연순" 알고리즘으로 배열 정렬
  • rsort() - 역순으로 배열 정렬

User Contributed Notes

bimal at sanjaal dot com 26-May-2014 04:33
An example of reverse sorting a domain name by its name.

<?php
$domains = array(
    'sub.domain.com',
    'sub2.domain.com',
);

foreach($domains as $d => $domain)
{
    $chunks = explode('.', $domain);
    krsort($chunks);

    echo "\r\n<br>", implode('/', $chunks);
}

/**
 * Outputs as:
 *
 * com/domain/sub
 * com/domain/sub2
 */
?>
Anonymous 11-Feb-2014 02:43
@thegrandoverseer

you could also use the build-in php array functions to get exactly what you want to have:

<?php
        $seed_array = array('foo'=>'bar', 'someotherkey'=>'whatev', 'bar'=>'baz', 'baz'=>'foo', 'anotherkey'=>'anotherval');
        $keys_array = array('baz', 'foo', 'bar');

        $return_array = array_intersect_key($seed_array, array_flip($keys_array)) + array_diff_key($seed_array, array_flip($keys_array));

?>
Anonymous 13-Aug-2013 10:11
Note that this function will output the given $fields in the order they were added to the data array and not automatically in numerical key order.

To output in ascending key order, you'll need to ksort the array first (or use appropriate natural order sorting, depending on your keys).

For example:
<?php
$data[2] = 'C';
$data[0] = 'A';
$data[1] = 'B';

fputcsv($fh, $data); // outputs: "C,A,B"

ksort($data);
fputcsv($fh, $data); // outputs: "A,B,C"
?>
centraleffects at yahoo dot com 06-Jun-2013 02:46
I wrote this function to sort meta_value in wordpress. I tried a lot of array sorting but neither of them work. But this is not suitable for multidimensional array. This is intended only for wordpress meta_value

The problem is to sort below( the order should be ascending; alphabetically then numerically like A-Z then 0-9):
500-999 users
25-49 users
 All Sizes
1-4 users
5-9 users
10-24 users
250-499 users
1000-4999
5000-9999

The solution:

function array_sort($arr){
    if(is_array($arr)){
        $numeric = array();
        $string = array();
        foreach($arr as $k => $v)
        {
            if(isset($v["meta_value"])){
                 $str = explode(" ",trim($v["meta_value"]));
                 $firstWord  = explode("-",trim($str[0]));
             }else{
                 $str = $v;
                 $firstWord  = explode("-",trim($str));
             }

            $firstWord = $firstWord[0];

            if(is_numeric($firstWord))
            {
                $numeric[(int)$firstWord] = $v;
            }else{
                $string[$firstWord] = $v;
            }
            unset($firstWord);
        }
        ksort($string,SORT_STRING);
        ksort($numeric,SORT_NUMERIC);

        return array_merge((array)$string, (array)$numeric);
    }

    return false;
}

The usage:
$meta =get_post_meta($post_id,$meta_key);
$sorted = array_sort($meta);

The result:
 All Sizes
1-4 users
5-9 users
10-24 users
25-49 users
250-499 users
500-999 users
1000-4999
5000-9999
stephen [ at ] brooksie-net [ dot ] co [ dot ] uk 18-Apr-2013 02:10
ksort and krsort fail to undestand scientific notation, https://bugs.php.net/bug.php?id=43053, therefore when sorting numeric keys, if the key is of the form 0.00001 php will represent it as 1.0E-5.

These methods will assume this to be a string and therefore not organise your array as you may expect.

When using value of this form for array keys use sprintf('%f', 0.00001) to generate the key, for smaller values the precision needs to be included
e.g. sprintf('%0.10f', 0.00000001)
thegrandoverseer 14-Jul-2012 03:11
I wrote this function to sort the keys of an array using an array of keynames, in order.
<?php
/**
 * function array_reorder_keys
 * reorder the keys of an array in order of specified keynames; all other nodes not in $keynames will come after last $keyname, in normal array order
 * @param array &$array - the array to reorder
 * @param mixed $keynames - a csv or array of keynames, in the order that keys should be reordered
 */
function array_reorder_keys(&$array, $keynames){
    if(empty($array) || !is_array($array) || empty($keynames)) return;
    if(!is_array($keynames)) $keynames = explode(',',$keynames);
    if(!empty($keynames)) $keynames = array_reverse($keynames);
    foreach($keynames as $n){
        if(array_key_exists($n, $array)){
            $newarray = array($n=>$array[$n]); //copy the node before unsetting
            unset($array[$n]); //remove the node
            $array = $newarray + array_filter($array); //combine copy with filtered array
        }
    }
}
$seed_array = array('foo'=>'bar', 'someotherkey'=>'whatev', 'bar'=>'baz', 'baz'=>'foo', 'anotherkey'=>'anotherval');
array_reorder_keys($seed_array, 'baz,foo,bar'); //returns array('baz'=>'foo', 'foo'=>'bar', 'bar'=>'baz', 'someotherkey'=>'whatev', 'anotherkey'=>'anotherval' );
?>
jakub dot lopuszanski at nasza-klasa dot pl 14-Apr-2011 05:48
Note that ksort will NOT help you much if numeric and string keys are mixed together.
<?php
$t = array(
  "a"=>"A",
  0=>"A",
  "b"=>"A",
  1=>"A"
);
var_dump($t);
ksort($t);
var_dump($t);
?>

produces (on PHP 5.3.6-4 with Suhosin-Patch) :

array(4) {
  ["a"]=>
  string(1) "A"
  [0]=>
  string(1) "A"
  ["b"]=>
  string(1) "A"
  [1]=>
  string(1) "A"
}

array(4) {
  ["b"]=>
  string(1) "A"
  [0]=>
  string(1) "A"
  ["a"]=>
  string(1) "A"
  [1]=>
  string(1) "A"
}

note that the second array should be sorted by keys, but is even more messed up than the first one!
DavidG 17-Jun-2010 03:47
A nice way to do sorting of a key on a multi-dimensional array without having to know what keys you have in the array first:

<?php
$people = array(
array("name"=>"Bob","age"=>8,"colour"=>"red"),
array("name"=>"Greg","age"=>12,"colour"=>"blue"),
array("name"=>"Andy","age"=>5,"colour"=>"purple"));

var_dump($people);

$sortArray = array();

foreach($people as $person){
    foreach($person as $key=>$value){
        if(!isset($sortArray[$key])){
            $sortArray[$key] = array();
        }
        $sortArray[$key][] = $value;
    }
}

$orderby = "name"; //change this to whatever key you want from the array

array_multisort($sortArray[$orderby],SORT_DESC,$people);

var_dump($people);
?>

Output from first var_dump:

[0]=&gt;
  array(3) {
    ["name"]=&gt;
    string(3) "Bob"
    ["age"]=&gt;
    int(8)
    ["colour"]=&gt;
    string(3) "red"
  }
  [1]=&gt;
  array(3) {
    ["name"]=&gt;

    string(4) "Greg"
    ["age"]=&gt;
    int(12)
    ["colour"]=&gt;
    string(4) "blue"
  }
  [2]=&gt;
  array(3) {
    ["name"]=&gt;
    string(4) "Andy"
    ["age"]=&gt;
    int(5)
    ["colour"]=&gt;

    string(6) "purple"
  }
}

Output from 2nd var_dump:

array(3) {
  [0]=&gt;
  array(3) {
    ["name"]=&gt;
    string(4) "Greg"
    ["age"]=&gt;
    int(12)
    ["colour"]=&gt;
    string(4) "blue"
  }
  [1]=&gt;
  array(3) {
    ["name"]=&gt;

    string(3) "Bob"
    ["age"]=&gt;
    int(8)
    ["colour"]=&gt;
    string(3) "red"
  }
  [2]=&gt;
  array(3) {
    ["name"]=&gt;
    string(4) "Andy"
    ["age"]=&gt;
    int(5)
    ["colour"]=&gt;

    string(6) "purple"
  }

There's no checking on whether your array keys exist, or the array data you are searching on is actually there, but easy enough to add.
serpro at gmail dot com 13-Mar-2009 11:02
Here is a function to sort an array by the key of his sub-array.

<?php

function sksort(&$array, $subkey="id", $sort_ascending=false) {

    if (count($array))
        $temp_array[key($array)] = array_shift($array);

    foreach($array as $key => $val){
        $offset = 0;
        $found = false;
        foreach($temp_array as $tmp_key => $tmp_val)
        {
            if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey]))
            {
                $temp_array = array_merge(    (array)array_slice($temp_array,0,$offset),
                                            array($key => $val),
                                            array_slice($temp_array,$offset)
                                          );
                $found = true;
            }
            $offset++;
        }
        if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
    }

    if ($sort_ascending) $array = array_reverse($temp_array);

    else $array = $temp_array;
}

?>

Example
<?php
$info = array("peter" => array("age" => 21,
                                           "gender" => "male"
                                           ),
                   "john"  => array("age" => 19,
                                           "gender" => "male"
                                           ),
                   "mary" => array("age" => 20,
                                           "gender" => "female"
                                          )
                  );

sksort($info, "age");
var_dump($info);

sksort($info, "age", true);
var_dump($ifno);
?>

This will be the output of the example:

/*DESCENDING SORT*/
array(3) {
  ["peter"]=>
  array(2) {
    ["age"]=>
    int(21)
    ["gender"]=>
    string(4) "male"
  }
  ["mary"]=>
  array(2) {
    ["age"]=>
    int(20)
    ["gender"]=>
    string(6) "female"
  }
  ["john"]=>
  array(2) {
    ["age"]=>
    int(19)
    ["gender"]=>
    string(4) "male"
  }
}

/*ASCENDING SORT*/
array(3) {
  ["john"]=>
  array(2) {
    ["age"]=>
    int(19)
    ["gender"]=>
    string(4) "male"
  }
  ["mary"]=>
  array(2) {
    ["age"]=>
    int(20)
    ["gender"]=>
    string(6) "female"
  }
  ["peter"]=>
  array(2) {
    ["age"]=>
    int(21)
    ["gender"]=>
    string(4) "male"
  }
}
maik dot riechert at animey dot net 12-Aug-2008 08:32
Be careful when using ksort for mixed type keys!!

$a = array(
    'first' => true,
    0       => 'sally',
);

$b = array(
    0       => 'sally',
    'first' => true,
);

ksort($a);
ksort($b);
var_dump($a);
var_dump($b);

Output is:
array(
    0 => 'sally',
    'first' => true,
)

array(
    'first' => true,
    0 => 'sally',
)

If you want same results for both arrays, use:

ksort($a, SORT_STRING);

The reason for that lays in the compare mechanism which would normally just typecast 'first' to an integer or 0 to a string when comparing it to each other. So you have to use SORT_STRING, otherwise you would lose information when 'first' is converted to int.
ssb45 at cornell dot edu 30-Jun-2005 01:58
The function that justin at booleangate dot org provides works well, but be aware that it is not a drop-in replacement for ksort as is.  While ksort sorts the array by reference and returns a status boolean, natksort returns the sorted array, leaving the original untouched.  Thus, you must use this syntax:

$array = natksort($array);

If you want to use the more natural syntax:

$status = natksort($array);

Then use this modified version:

function natksort(&$array) {
    $keys = array_keys($array);
    natcasesort($keys);

    foreach ($keys as $k) {
        $new_array[$k] = $array[$k];
    }

    $array = $new_array;
    return true;
}
justin at booleangate dot org 18-Jan-2005 10:04
Here's a handy function for natural order sorting on keys.

function natksort($array) {
  // Like ksort but uses natural sort instead
  $keys = array_keys($array);
  natsort($keys);

  foreach ($keys as $k)
    $new_array[$k] = $array[$k];

  return $new_array;
}
09-Mar-2002 04:09
here 2 functions to ksort/uksort an array and all its member arrays

function tksort(&$array)
  {
  ksort($array);
  foreach(array_keys($array) as $k)
    {
    if(gettype($array[$k])=="array")
      {
      tksort($array[$k]);
      }
    }
  }

function utksort(&$array, $function)
  {
  uksort($array, $function);
  foreach(array_keys($array) as $k)
    {
    if(gettype($array[$k])=="array")
      {
      utksort($array[$k], $function);
      }
    }
  }
delvach at mail dot com 06-Nov-2001 10:29
A real quick way to do a case-insensitive sort of an array keyed by strings:

uksort($myArray, "strnatcasecmp");
sbarnum at mac dot com 20-Oct-2001 12:54
ksort on an array with negative integers as keys yields some odd results.  Not sure if this is a bad idea (negative key values) or what.


배열처럼 변수에 할당

  • ≪ ksort
  • natcasesort ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열처럼 변수에 할당

list

(PHP 4, PHP 5, PHP 7)

list — 배열처럼 변수에 할당

설명

void list ( mixed $varname [, mixed $... ] )

array()처럼, 이 함수는 실제 함수가 아니고 언어 구조이다. list()는 한번의 조작으로 배열 목록을 지정하기 위해 사용된다.

인수

varname

변수.

반환값

값을 반환하지 않습니다.

예제

Example #1 list() 예제

<?php

$info = array('coffee', 'brown', 'caffeine');

// 모든 변수를 목록화한다
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";

// 그들중 일부를 목록화한다
list($drink, , $power) = $info;
echo "$drink has $power.\n";

// 또는 세번째 값으로만 건너띈다
list( , , $power) = $info;
echo "I need $power!\n";

// list()는 문자열에는 작동하지 않습니다
list($bar) = "abcde";
var_dump($bar); // NULL
?>

Example #2 list() 용례

<table>
 <tr>
  <th>Employee name</th>
  <th>Salary</th>
 </tr>

<?php

$result = mysql_query ("SELECT id, name, salary FROM employees", $conn);
while (list ($id, $name, $salary) = mysql_fetch_row ($result)) {
    echo " <tr>\n" .
          "  <td><a href=\"info.php?id=$id\">$name</a></td>\n" .
          "  <td>$salary</td>\n" .
          " </tr>\n";
}

?>

</table>

Example #3 중첩 list() 사용하기

<?php

list($a, list($b, $c)) = array(1, array(2, 3));

var_dump($a, $b, $c);

?>
int(1)
int(2)
int(3)

Example #4 배열 인덱스로 list() 사용하기

<?php

$info = array('coffee', 'brown', 'caffeine');

list($a[0], $a[1], $a[2]) = $info;

var_dump($a);

?>

다음과 같이 출력된다 (이 원소들의 순서는 list() 문법에서 쓰여진 순서와 반대라는 점에 주의):

array(3) {
  [2]=>
  string(8) "caffeine"
  [1]=>
  string(5) "brown"
  [0]=>
  string(6) "coffee"
}

주의

Warning

list()는 가장 우측 인수에서 시작하는 값들을 지정한다. 일반적인 변수들을 사용하면, 이 동작에 대해서 신경 쓸 필요가 없다. 그러나 인덱스를 갖는 배열에서 사용한다면 왼쪽에서 오른쪽으로 list()에서 쓰여진것과 같이 배열안의 인덱스 순서로 되는것이라고 보통 기대할수 있으나, 그렇지 않다. 역순으로 지정이 된다.

Note:

list()는 숫자 배열에서만 작동하고 0부터 시작하는 숫자 인덱스로 가정한다.

참고

  • each() - 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진
  • array() - 배열 생성
  • extract() - 배열에서 현재 심볼 테이블로 변수를 입력

User Contributed Notes

anthony dot ossent at live dot fr 23-Oct-2016 12:04
a simple example of use to swap two variables :

$a = 'hello';
$b = 'world';

list($a, $b) = [$b, $a];

echo $a . ' ' . $b; //display "world hello"

another example :

function getPosition($x, $y, $z)
{
   // ... some operations like $x++...
   return [$x, $y, $z];
}

list($x, $y, $z) = getPosition($x ,$y, $z);
pemapmodder1970 at gmail dot com 06-Feb-2016 01:08
list() can be used with foreach

<?php
$array = [[1, 2], [3, 4], [5, 6]];

foreach($array as list($odd, $even)){
    echo "$odd is odd; $even is even", PHP_EOL;
}
?>

The output:
===
1 is odd; 2 is even
3 is odd; 4 is even
5 is odd; 6 is even
Dean 11-Dec-2015 04:20
UNDOCUMENTED BEHAVIOR:

    list($a,$b,$c) = null;

in fact works like:

    $a = null; $b = null; $c = null;

...So correspondingly:

    list($rows[]) = null;

Will increment count($rows), just as if you had executed $rows[] = null;

Watch out for this (for example) when retrieving entire tables from a database, e.g.

    while (list($rows[]) = $mysqlresult->fetch_row());

This will leave an extra 'null' entry as the last element of $rows.
Colin Guthrie 20-Jul-2015 10:41
If you want use the undefined behaviour as you might expect it e.g. if you want:

  $b = ['a','b']; list($a, $b) = $b;

to result in $a=='a' and $b=='b', then you can just cast $b to an array (even although it already is) to create a copy. e.g.

  $b = ['a','b']; list($a, $b) = (array)$b;

and get the expected results.
mogwai512 21-Jun-2015 02:54
I see many people offer solutions about the flipped order of the list construct. All you have to do is this:

<?php

$info = array('coffee', 'brown', 'caffeine');

$a = list($a[0], $a[1], $a[2]) = $info;

var_dump($a);

?>

. . . and your info will be in the correct order. You can also just assign a new var to the list and it will still work:

<?php

$info = array('coffee', 'brown', 'caffeine');

$b = list($a[0], $a[1], $a[2]) = $info;

var_dump($b);

?>
mdessaintes at gmail dot com 18-May-2015 05:25
Assignement is from right to left so this result in unexpected behavior:

<?php
$str = ['123456', 'abcdef'];
list($num, $str) = $str;

var_dump($num, $str);
?>

outputs :

string 'a' (length=1)
string 'abcdef' (length=6)
grzeniufication 21-Oct-2014 07:35
The example showing that:

$info = array('kawa', 'br?zowa', 'kofeina');
list($a[0], $a[1], $a[2]) = $info;
var_dump($a);

outputs:
array(3) {
[2]=>
string(8) "kofeina"
[1]=>
string(5) "br?zowa"
[0]=>
string(6) "kawa"
}

One thing to note here is that if you define the array earlier, e.g.:
$a = [0, 0, 0];

the indexes will be kept in the correct order:

array(3) {
  [0]=>
  string(4) "kawa"
  [1]=>
  string(8) "br?zowa"
  [2]=>
  string(7) "kofeina"
}

Thought that it was worth mentioning.
vickyssj7 at gmail dot com 02-Sep-2014 02:21
if we assign array's each value individual key('numeric only'), and use the array indices in list(),, then it output the reverse order of array keys--
BUT THE HIGHER KEY VALUE ("2" in this e.g below) WILL GET THE FIRST PLACE IN THE ARRAY IN RETURN, MEANS IT PUSHES THE VALUE WITH HIGHER KEY IN PLACE OF FIRST KEY VALUE, so it also gives higher key value the first priority while reversing the order of the keys and replacing the lower key value with the higher key value.

        $value = array( 0 => 'low', 2 => 'medium', 1 => 'higher');
        list($a[2], $a[1], $a[0]) = $value;
    var_dump($a);

//Outputs:---
array(3) {
            [0] => string(6) "medium"
            [1]  => string(6) "higher"
            [2]  => string(3) "low"
}
Thanos K. 14-Mar-2014 02:36
Also it seems that it doesn't work as expected with arrays with non numeric keys:

list($k, $l, $m) = array('a' => 'val1', 'b' => 'val2', 'c' => 'val3');

Gives empty variables..
edam 05-Feb-2014 12:18
This doesn't work on associative array.  For example:

    list( $a, $b, $c ) = array( 'a' => 'a', 'b' => 'b', 'c' => 'c' );
    PHP Notice:  Undefined offset: 2 in Command line code on line 1
    PHP Notice:  Undefined offset: 1 in Command line code on line 1
    PHP Notice:  Undefined offset: 0 in Command line code on line 1
john at jbwalker dot com 08-Dec-2013 09:31
The list construct seems to look for a sequential list of indexes rather taking elements in sequence. What that obscure statement means is that if you unset an element, list will not simply jump to the next element and assign that to the variable but will treat the missing element as a null or empty variable:

    $test = array("a","b","c","d");
    unset($test[1]);
    list($a,$b,$c)=$test;
    print "\$a='$a' \$b='$b' \$c='$c'<BR>";

results in:
$a='a' $b='' $c='c'

not:
$a='a' $b='c' $c='d'
megan at voices dot com 11-Sep-2013 12:45
As noted, list() will give an error if the input array is too short. This can be avoided by array_merge()'ing in some default values. For example:

<?php
$parameter = 'name';
list( $a, $b ) = array_merge( explode( '=', $parameter ), array( true ) );
?>

However, you will have to array_merge with an array long enough to ensure there are enough elements (if $parameter is empty, the code above would still error).

An alternate approach would be to use array_pad on the array to ensure its length (if all the defaults you need to add are the same).

<?php
    $parameter = 'bob-12345';
    list( $name, $id, $fav_color, $age ) = array_pad( explode( '-', $parameter ), 4, '' );
    var_dump($name, $id, $fav_color, $age);
/* outputs
string(3) "bob"
string(5) "12345"
string(0) ""
string(0) ""
*/
?>
Arne 31-Jul-2013 10:30
list() will give an error if the input array is too short. This can be avoided by array_merge()'ing in some default values. For example:

<?php
$parameter = 'name';
list( $a, $b ) = array_merge( explode( '=', $parameter ), array( true ) );
?>
Achilles at thegreatwarrior dot com 14-Jul-2013 09:58
Second, when you're using the list() function, you must acknowledge each array element. You could not do this
list($weekday, $month) = $date;

But you can use empty values to ignore elements:
list ($weekday, , $month) = $date;
Matt 26-May-2013 01:40
You can't type check within the list() parameters:

list ( array $var1, $var2 ) = array ( array('one','two'), 'three');

generates a parse error, unexpected 'array'.
srikanth at networthindia dot com 09-Feb-2013 09:34
Note: list cannot assign array cast of object to variables straight away. first you need to convert the object to numeric indexed array.

ex:
list($a, $b, $d) = (array) $abc; // $abc is an object; this will not assign.
list($a, $b, $c) = array_values((array) $abc); // This will work.
svennd 24-Dec-2012 04:08
The list() definition won't throw an error if your array is longer then defined list.
<?php

list($a, $b, $c) = array("a", "b", "c", "d");

var_dump($a); // a
var_dump($b); // b
var_dump($c); // c
?>
chris at chlab dot ch 08-Nov-2012 12:00
The example states the following:
<?php
// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar);
// output: NULL
?>

If the string is in a variable however, it seems using list() will treat the string as an array:
<?php
$string = "abcde";
list($foo) = $string;
var_dump($foo);
// output: string(1) "a"
?>


"자연순" 알고리즘으로 대소문자를 구분하지 않고 배열 정렬

  • ≪ list
  • natsort ≫
  • PHP Manual
  • 배열 함수 목록
  • "자연순" 알고리즘으로 대소문자를 구분하지 않고 배열 정렬

natcasesort

(PHP 4, PHP 5, PHP 7)

natcasesort — "자연순" 알고리즘으로 대소문자를 구분하지 않고 배열 정렬

설명

bool natcasesort ( array &$array )

natcasesort()는 대소문자를 구분하지 않는 natsort()입니다.

이 함수는 키/값 연결을 유지하면서 사람이 하는 순서로 알파벳-숫자 문자열을 정렬하는 알고리즘을 구현합니다. 이 알고리즘을 "자연순"이라고 한다.

인수

array

입력 배열.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 natcasesort() 예제

<?php
$array1 = $array2 = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');

sort($array1);
echo "일반 정렬\n";
print_r($array1);

natcasesort($array2);
echo "\n자연순 정렬 (대소문자 구분 없음)\n";
print_r($array2);
?>

위 예제의 출력:

일반 정렬
Array
(
    [0] => IMG0.png
    [1] => IMG3.png
    [2] => img1.png
    [3] => img10.png
    [4] => img12.png
    [5] => img2.png
)

자연순 정렬
Array
(
    [0] => IMG0.png
    [4] => img1.png
    [3] => img2.png
    [5] => IMG3.png
    [2] => img10.png
    [1] => img12.png
)

추가 정보: Martin Pool의 » 자연순 문자열 정렬 페이지.

참고

  • sort() - 배열 정렬
  • natsort() - "자연순" 알고리즘으로 배열 정렬
  • strnatcmp() - "자연순" 알고리즘을 이용한 문자열 비교
  • strnatcasecmp() - "자연순" 알고리즘을 이용한 대소문자 구분 없는 문자열 비교

User Contributed Notes

w-dot-rosenbach-at-netskill-de 14-Dec-2010 06:45
Sorting UTF-8 by arbitrary order:

<?php
mb_internal_encoding("UTF-8");

class utf_8_german
{
  // everything else is sorted at the end
  static $order = '0123456789AaAaBbCcDdEeFfGgHhIiJjKkLlMm
NnOoOoPpQqRrSsßTtUuUuVvWwXxYyZz';
  static $char2order;

  static function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }

    // lazy init mapping
    if (empty(self::$char2order))
    {
      $order = 1;
      $len = mb_strlen(self::$order);
      for ($order=0; $order<$len; ++$order)
      {
        self::$char2order[mb_substr(self::$order, $order, 1)] = $order;
      }
    }

    $len_a = mb_strlen($a);
    $len_b = mb_strlen($b);
    $max=min($len_a, $len_b);
    for($i=0; $i<$max; ++$i)
    {
      $char_a= mb_substr($a, $i, 1);
      $char_b= mb_substr($b, $i, 1);

      if ($char_a == $char_b) continue;
      $order_a = (isset(self::$char2order[$char_a])) ? self::$char2order[$char_a] : 9999;
      $order_b = (isset(self::$char2order[$char_b])) ? self::$char2order[$char_b] : 9999;

      return ($order_a < $order_b) ? -1 : 1;
    }
    return ($len_a < $len_b) ? -1 : 1;
  }
}

// usage example:

$t = array(
  'Birnen', 'Birne', 'Apfel', 'Apfel',
);

uasort($t, 'utf_8_german::cmp');
echo '$t: <pre>'.htmlspecialchars(print_r($t,true),null,'UTF-8').'</pre>';
?>
shawn at shawnwilkerson dot com 05-May-2009 07:03
I kept getting varied results using natcasesort and sort on mixed arrays -- per the descriptions.

Sometimes simple is better:

A little snippet of code:

<?php                        if($responders->num_rows) {
                            $i=0;
                            while($row= $responders->fetch_assoc()) {
                                $user=getUserName($row['responderID']);
                                $r[$i]= array("sortname"=>strtolower($user),"userName"=>$user, "userID"=>$row['responderID'], "responderID"=>$row['idresponders']);
                                $i++;
                            }
                            sort($r);
                            print_r($r);
                        }

?>

I simply created a lower cased sort field at the front of the result set and then sort by it -- which provides the expected result and leaves the actual needed fields unchanged.

For the curious:  all user information is kept completed in another database (and table) from the content database due to security reasons.  The getUser functions we have written allow us to pull only what is legally allowed without exposing anything else.

This is why a left join or something wasn't used and we have to build a pseudo result array here from both databases.
claude at schlesser dot lu 07-Jan-2009 09:41
Here a function that will natural sort an array by keys with keys that contain special characters.

<?php
function natksort($array)
{
    $original_keys_arr = array();
    $original_values_arr = array();
    $clean_keys_arr = array();

    $i = 0;
    foreach ($array AS $key => $value)
    {
        $original_keys_arr[$i] = $key;
        $original_values_arr[$i] = $value;
        $clean_keys_arr[$i] = strtr($key, "AOUaouEEAEeeeac", "AOUaouEEAEeeeac");
        $i++;
    }

    natcasesort($clean_keys_arr);

    $result_arr = array();

    foreach ($clean_keys_arr AS $key => $value)
    {
        $original_key = $original_keys_arr[$key];
        $original_value = $original_values_arr[$key];
        $result_arr[$original_key] = $original_value;
    }

    return $result_arr;
}
?>

Hope it will be useful to somebody :)
vbAlexDOSMan at Yahoo dot com 12-Sep-2003 10:21
Ulli at Stemmeler dot net:  I remade your function -- it's a little more compact now -- Enjoy...

function ignorecasesort(&$array) {

  /*Make each element it's lowercase self plus itself*/
  /*(e.g. "MyWebSite" would become "mywebsiteMyWebSite"*/
  for ($i = 0; $i < sizeof($array); $array[$i] = strtolower($array[$i]).$array[$i], $i++);

  /*Sort it -- only the lowercase versions will be used*/
  sort($array);

  /*Take each array element, cut it in half, and add the latter half to a new array*/
  /*(e.g. "mywebsiteMyWebSite" would become "MyWebSite")*/
  for ($i = 0; $i < sizeof($array); $i++) {
    $this = $array[$i];
    $array[$i] = substr($this, (strlen($this)/2), strlen($this));
  }
}
dslicer at maine dot rr dot com 03-Jun-2003 03:41
Something that should probably be documented is the fact that both natsort and natcasesort maintain the key-value associations of the array. If you natsort a numerically indexed array, a for loop will not produce the sorted order; a foreach loop, however, will produce the sorted order, but the indices won't be in numeric order. If you want natsort and natcasesort to break the key-value associations, just use array_values on the sorted array, like so:

natcasesort($arr);
$arr = array_values($arr);
tmiller25 at hotmail dot com 26-Apr-2002 04:55
add this loop to the function above if you want items which have the same first characters to be listed in a way that the shorter string comes first.
--------------------
  /* short before longer (e.g. 'abc' should come before 'abcd') */
  for($i=count($array)-1;$i>0;$i--) {
    $str_a = $array[$i  ];
    $str_b = $array[$i-1];
    $cmp_a = strtolower(substr($str_a,0,strlen($str_a)));
    $cmp_b = strtolower(substr($str_b,0,strlen($str_a)));
    if ($cmp_a==$cmp_b && strlen($str_a)<strlen($str_b)) {
      $array[$i]=$str_b; $array[$i-1]=$str_a; $i+=2;
    }
  }
--------------------


"자연순" 알고리즘으로 배열 정렬

  • ≪ natcasesort
  • next ≫
  • PHP Manual
  • 배열 함수 목록
  • "자연순" 알고리즘으로 배열 정렬

natsort

(PHP 4, PHP 5, PHP 7)

natsort — "자연순" 알고리즘으로 배열 정렬

설명

bool natsort ( array &$array )

이 함수는 키/값 연결을 유지하면서 사람이 하는 순서로 알파벳-숫자 문자열을 정렬하는 알고리즘을 구현합니다. 이 알고리즘을 "자연순"이라고 한다. 이 알고리즘과 일반적인 컴퓨터 문자열 정렬 알고리즘(sort() 사용)과의 차이점은 아래 예제에서 확인할 수 있습니다.

인수

array

입력 배열.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 natsort() 예제

<?php
$array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");

sort($array1);
echo "일반 정렬\n";
print_r($array1);

natsort($array2);
echo "\n자연순 정렬\n";
print_r($array2);
?>

위 예제의 출력:

일반 정렬
Array
(
    [0] => img1.png
    [1] => img10.png
    [2] => img12.png
    [3] => img2.png
)

자연순 정렬
Array
(
    [3] => img1.png
    [2] => img2.png
    [1] => img10.png
    [0] => img12.png
)

추가 정보: Martin Pool의 » 자연순 문자열 정렬 페이지.

참고

  • natcasesort() - "자연순" 알고리즘으로 대소문자를 구분하지 않고 배열 정렬
  • strnatcmp() - "자연순" 알고리즘을 이용한 문자열 비교
  • strnatcasecmp() - "자연순" 알고리즘을 이용한 대소문자 구분 없는 문자열 비교

User Contributed Notes

bb7b5b9 at gmail dot com 14-Jan-2016 05:07
This made me waste a lot of my precious youth ... natsort() is buggy if all numbers don't have the same number of decimal places.

(php 5.6.4-4ubuntu6.2)

<?php
$different_decimal_places_in_values = array('D'=>'13.59', '14.6' => '14.6', 'C-' => '14.19');
natsort($a);
var_dump($a);

/*echoes
array(3) {
  'D' =>
  string(5) "13.59"
  '14.6' =>
  string(4) "14.6" <----------- badly ordered
  'C-' =>
  string(5) "14.19"
}*/
?>

While this

<?php
$same_num_decimal_places_in_values = array('D'=>'13.59', '14.6' => '14.60', 'C-' => '14.19'); natsort($a); var_dump($a);

/*echoes
array(3) {
  'D' =>
  string(5) "13.59"
  'C-' =>
  string(5) "14.19"
  '14.6' =>
  string(5) "14.60" <--------- that is the correct position
}
*/

?>
mvs dot php at gmail dot com 20-May-2015 07:12
To naturally sort by array key, the uksort function can be used.

<?php

echo "Sort by keys\n";
$smoothie = array('orange' => 1, 'apple' => 1, 'yogurt' => 4, 'banana' => 4);
print_r($smoothie);
uksort( $smoothie, 'strnatcmp');
print_r($smoothie)

?>

Output:

Sort by keys
Array
(
    [orange] => 1
    [apple]  => 1
    [yogurt] => 4
    [banana] => 4
)
Array
(
    [apple]  => 1
    [banana] => 4
    [orange] => 1
    [yogurt] => 4
)

See http://php.net/manual/en/function.uksort.php for more information about uksort and http://php.net/strnatcmp for usage of strnatcmp.
Johan GENNESSON (php at genjo dot fr) 24-Nov-2010 04:18
Be careful of the new behaviour in 5.2.10 version.
See the following sample:

<?php

$array = array('1 bis', '10 ter', '0 PHP', '0', '01', '01 Ver', '0 ', '1 ', '1');

natsort($array);
echo '<pre>';
print_r($array);
echo '</pre>';
?>

5.2.6-1 will output:
Array
(
    [3] => 0
    [6] => 0
    [2] => 0 OP
    [4] => 01
    [5] => 01 Ver
    [8] => 1
    [7] => 1
    [0] => 1 bis
    [1] => 10 ter
)

5.2.10 will output:
Array
(
    [6] => 0
    [3] => 0
    [8] => 1
    [4] => 01
    [7] => 1
    [5] => 01 Ver
    [0] => 1 bis
    [1] => 10 ter
    [2] => 0 OP
)

Greetings
Christoph 26-Nov-2009 07:45
Note:

The natsort function will sort depending on the operating system, but not depending on either Linux or Windows-based systems

There's a difference when sorting an array which is generated from the filesystem:
Array(
    [0] => ./system/kernel/js/01_ui.core.js
    [1] => ./system/kernel/js/00_jquery-1.3.2.js
    [2] => ./system/kernel/js/02_my.desktop.js
)

natsort($array) will result in two different ways:

Case 1: (Debian)
Array(
    [1] => ./system/kernel/js/00_jquery-1.3.2.js
    [0] => ./system/kernel/js/01_ui.core.js
    [2] => ./system/kernel/js/02_my.desktop.js
)

Case 2: (Debian Kernel, but Ubuntu shadowed php-Version)
Array(
    [0] => ./system/kernel/js/01_ui.core.js
    [2] => ./system/kernel/js/02_my.desktop.js
    [1] => ./system/kernel/js/00_jquery-1.3.2.js
)

...so make sure you've named the files beginning with 01, then it works fine.
ale152 16-Mar-2009 11:00
Note: negatives number.
<?php
$a = array(-5,-2,3,9);
natsort($a);
print_r($a);
?>
Will output:
Array ( [1] => -2 [0] => -5 [2] => 3 [3] => 9 )
AJenbo 15-Jan-2009 10:33
natsort might not act like you would expect with zero padding, heres a quick sample.

<?php
$array = array('09', '8', '10', '009', '011');
natsort($array);
?>
/*
Array
(
    [3] => 009
    [4] => 011
    [0] => 09
    [1] => 8
    [2] => 10
)
*/
wyvern at greywyvern dot com 17-Dec-2008 07:53
There's no need to include your own API code to natsort an associative array by key.  PHP's in-built functions (other than natsort) can do the job just fine:

<?php
  uksort($myArray, "strnatcmp");
?>
lacent at gmail dot com 16-Jan-2007 06:42
there is another rnatsort function lower on the page, but it didn't work in the context i needed it in.

reasoning for this:
sorting naturally via the keys of an array, but needing to reverse the order.

    function rnatsort ( &$array = array() )
    {
        $keys    = array_keys($array);
        natsort($keys);
        $total    = count($keys) - 1;
        $temp1    = array();
        $temp2     = array();

        // assigning original keys to an array with a backwards set of keys, to use in krsort();
        foreach ( $keys as $key )
        {
            $temp1[$total] = $key;
            --$total;
        }

        ksort($temp1);

        // setting the new array, with the order from the krsort() and the values of original array.
        foreach ( $temp1 as $key )
        {
            $temp2[$key] = $array[$key];
        }

        $array = $temp2;
    }
@gmail bereikme 01-Sep-2006 05:06
Here's a handy function to sort an array on 1 or more columns using natural sort:
<?php
// Example: $records = columnSort($records, array('name', 'asc', 'addres', 'desc', 'city', 'asc'));

$globalMultisortVar = array();
function columnSort($recs, $cols) {
    global $globalMultisortVar;
    $globalMultisortVar = $cols;
    usort($recs, 'multiStrnatcmp');
    return($recs);
}

function multiStrnatcmp($a, $b) {
    global $globalMultisortVar;
    $cols = $globalMultisortVar;
    $i = 0;
    $result = 0;
    while ($result == 0 && $i < count($cols)) {
        $result = ($cols[$i + 1] == 'desc' ? strnatcmp($b[$cols[$i]], $a[$cols[$i]]) : $result = strnatcmp($a[$cols[$i]], $b[$cols[$i]]));
        $i+=2;
    }
    return $result;
}

?>

Greetings,

  - John
12-Mar-2006 04:44
The last comment should have been posted in doc about (r)sort( ). Indeed, and unfortunately, ORDER BY *does not* perform natural ordering. So, sometimes we *must* do a SQL request followed by natsort( ).
h3 22-Feb-2006 05:59
This function can be very usefull, but in some cases, like if you want to sort a MySQL query result, it's important to keep in mind that MySQL as built'in sorting functions which are way faster than resorting the result using a complex php algorythm, especially with large arrays.

ex; 'SELECT * FROM `table` ORDER BY columnName ASC, columnName2 DESC'
lil at thedreamersmaze dot spam-me-not dot org 31-Jan-2006 05:38
There's one little thing missing in this useful bit of code posted by mbirth at webwriters dot de:

<?php

function natsort2d(&$aryInput) {
  $aryTemp = $aryOut = array();
  foreach ($aryInput as $key=>$value) {
   reset($value);
   $aryTemp[$key]=current($value);
  }
  natsort($aryTemp);
  foreach ($aryTemp as $key=>$value) {
   $aryOut[$key] = $aryInput[$key];
// --------^^^^ add this if you want your keys preserved!
  }
  $aryInput = $aryOut;
}

?>
natcasesort.too 05-Aug-2005 12:17
I got caught out through naive use of this feature - attempting to sort a list of image filenames from a digital camera, where the filenames are leading zero padded (e.g. DSCF0120.jpg) , will not sort correctly.
Maybe the example could be modified to exhibit this behaviour
(e.g. set array to -img0120.jpg','IMG0.png', 'img0012.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png)
If the example hadn't used images I would have coded it correctly first time around!
phpnet at moritz-abraham dot de 02-Sep-2004 11:09
additional to the code posted by justin at redwiredesign dot com (which I found very usefull) here is a function that sorts complex arrays like this:
<?
$array['test0'] = array('main' =>  'a', 'sub' => 'a');
$array['test2'] = array('main' =>  'a', 'sub' => 'b');
$array['test3'] = array('main' =>  'b', 'sub' => 'c');
$array['test1'] = array('main' =>  'a', 'sub' => 'c');
$array['test4'] = array('main' =>  'b', 'sub' => 'a');
$array['test5'] = array('main' =>  'b', 'sub' => 'b');
?>
or
<?
$array[0] = array('main' =>  1, 'sub' => 1);
$array[2] = array('main' =>  1, 'sub' => 2);
$array[3] = array('main' =>  2, 'sub' => 3);
$array[1] = array('main' =>  1, 'sub' => 3);
$array[4] = array('main' =>  2, 'sub' => 1);
$array[5] = array('main' =>  2, 'sub' => 2);
?>
on one or more columns.

the code
<? $array = array_natsort_list($array,'main','sub'); ?>
will result in $array being sortet like this:
test0,test2,test1,test4,test5,test3
or
0,2,1,4,5,3.

you may even submit more values to the function as it uses a variable parameter list. the function starts sorting on the last and the goes on until the first sorting column is reached.

to me it was very usefull for sorting a menu having submenus and even sub-submenus.

i hope it might help you too.

here is the function:
<?
function array_natsort_list($array) {
    // for all arguments without the first starting at end of list
    for ($i=func_num_args();$i>1;$i--) {
        // get column to sort by
        $sort_by = func_get_arg($i-1);
        // clear arrays
        $new_array = array();
        $temporary_array = array();
        // walk through original array
        foreach($array as $original_key => $original_value) {
            // and save only values
            $temporary_array[] = $original_value[$sort_by];
        }
        // sort array on values
        natsort($temporary_array);
        // delete double values
        $temporary_array = array_unique($temporary_array);
        // walk through temporary array
        foreach($temporary_array as $temporary_value) {
            // walk through original array
            foreach($array as $original_key => $original_value) {
                // and search for entries having the right value
                if($temporary_value == $original_value[$sort_by]) {
                    // save in new array
                    $new_array[$original_key] = $original_value;
                }
            }
        }
        // update original array
        $array = $new_array;
    }
    return $array;
}
?>
xlab AT adaptiveNOSPAMarts DOT net 15-Jan-2004 09:51
Under limited testing, natsort() appears to work well for IP addresses. For my needs, it is far less code than the ip2long()/long2ip() conversion I was using before.
mbirth at webwriters dot de 09-Jan-2004 09:31
For those who want to natsort a 2d-array on the first element of each sub-array, the following few lines should do the job.

<?php

function natsort2d(&$aryInput) {
  $aryTemp = $aryOut = array();
  foreach ($aryInput as $key=>$value) {
    reset($value);
    $aryTemp[$key]=current($value);
  }
  natsort($aryTemp);
  foreach ($aryTemp as $key=>$value) {
    $aryOut[] = $aryInput[$key];
  }
  $aryInput = $aryOut;
}

?>
rasmus at flajm dot com 15-Dec-2003 06:30
To make a reverse function, you can simply:

function rnatsort(&$a){
    natsort($a);
    $a = array_reverse($a, true);
}
justin at redwiredesign dot com 22-Aug-2003 07:10
One of the things I've needed to do lately is apply natural sorting to a complex array, e.g.:

Array
(
    [0] => Array
        (
            [ID] = 4
            [name] = Fred
        )

    [1] => Array
        (
            [ID] = 6
            [name] = Bob
        )
)

where I want to sort the parent array by the child's name. I couldn't see a way of doing this using array_walk, so I've written a simple function to do it. Hopefully someone will find this useful:

/**
 * @return Returns the array sorted as required
 * @param $aryData Array containing data to sort
 * @param $strIndex Name of column to use as an index
 * @param $strSortBy Column to sort the array by
 * @param $strSortType String containing either asc or desc [default to asc]
 * @desc Naturally sorts an array using by the column $strSortBy
 */
function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
{
    //    if the parameters are invalid
    if (!is_array($aryData) || !$strIndex || !$strSortBy)
        //    return the array
        return $aryData;

    //    create our temporary arrays
    $arySort = $aryResult = array();

    //    loop through the array
    foreach ($aryData as $aryRow)
        //    set up the value in the array
        $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];

    //    apply the natural sort
    natsort($arySort);

    //    if the sort type is descending
    if ($strSortType=="desc")
        //    reverse the array
        arsort($arySort);

    //    loop through the sorted and original data
    foreach ($arySort as $arySortKey => $arySorted)
        foreach ($aryData as $aryOriginal)
            //    if the key matches
            if ($aryOriginal[$strIndex]==$arySortKey)
                //    add it to the output array
                array_push($aryResult, $aryOriginal);

    //    return the return
    return $aryResult;
}
flash at minet dot net 12-Aug-2003 07:27
About the reverse natsort.. Maybe simpler to do :

function strrnatcmp ($a, $b) {
    return strnatcmp ($b, $a);
}
anonymous at coward dot net 31-May-2003 10:46
Reverse Natsort:

  function rnatsort($a, $b) {
    return -1 * strnatcmp($a, $b);
  }

  usort($arr, "rnatsort");


배열의 내부 배열 포인터를 전진

  • ≪ natsort
  • pos ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 내부 배열 포인터를 전진

next

(PHP 4, PHP 5, PHP 7)

next — 배열의 내부 배열 포인터를 전진

설명

mixed next ( array &$array )

next()는 current()와 비슷하게 동작하지만 한가지 차이를 갖고 있다. 원소값을 반환하기 전에 내부 배열 포인터를 한칸 다음으로 전진시킨다. 이 말은 다음 배열 값을 반환하고 내부 배열 포인터를 한칸 전진시킨다는 뜻이다.

인수

array

영향 받는 array

반환값

내부 배열 포인터가 가리키는 다음 위치의 배열 값을 반환하거나, 더 이상 원소가 없을 경우에는 FALSE를 반환합니다.

Warning

이 함수는 논리 FALSE를 반환하지만, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

예제

Example #1 next()의 용례와 관련 함수

<?php
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport);    // $mode = 'bike';
$mode = next($transport);    // $mode = 'car';
$mode = prev($transport);    // $mode = 'bike';
$mode = end($transport);     // $mode = 'plane';
?>

주의

Note: 배열의 끝과 boolean FALSE 원소를 구별할 수 없습니다. FALSE 원소를 가지는 배열을 올바르게 따라가려면, each()를 참고하십시오.

  • current() - 배열의 현재 원소를 반환
  • end() - 배열 내부 포인터가 마지막 원소를 가리키게 설정
  • prev() - 내부 배열 포인터를 후진
  • reset() - 배열의 내부 포인터를 첫 원소로 설정
  • each() - 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진

User Contributed Notes

JumpIfBelow 30-Jul-2015 09:45
After dealing with the fact that next() will either returns FALSE when there is no further element in the array or if the element itself is FALSE, I finally find a way to do a has_next() method, which will never fails. You can ignore and/or downvote my last comment, this it will be deleted.
Here is the code that work :
<?php
    function has_next(array $a){
        return next($a) !== false ?: each($a) !== false;
    }
?>
double at dumpit dot de 08-Dec-2009 10:19
PHP: 5.2.10-2ubuntu6.3 (default apt-get installation on actual, see Date, jaunty 9.10 Ubuntu Distro - G33kWoRDs)

Have a look at your array pointer if you copy an array - the pointer will be copied, too.

For example if you got this construct:
<?php
    $array = array('zero','one','two','three','four','five','six','seven');
    $array2 = $array;
    next($array);
    echo key($array);
    echo key($array2);

    // will output:
    // 1
    // 0
?>

But if you copy the array after you've setted the pointer, the pointer will be copied, too:
<?php
    $array = array('zero','one','two','three','four','five','six','seven');
    next($array);
    $array2 = $array;
    echo key($array);
    echo key($array2);

    // will output:
    // 1
    // 1
?>

What's more is, that foreach not resetting the pointer after walk through:
<?php

    $array = array('zero','one','two','three','four','five','six','seven');
    next($array);
    $array2 = array();
    foreach($array AS $key => $value){
        echo $key;
        $array2[$key] = $value;
    }
    echo var_dump(key($array));
    echo key($array2);

    // will output for foreach:
    // 0 1 2 3 4 5 6 7
    // and for the keys
    // NULL
    // 0
?>

The php-functions seems to reset the pointer on the given position after walk through (i don't know the internal handling - there could be used a copy of the array, too):
<?php

    $array = array('zero','one','two','three','four','five','six','seven');
    next($array);
    $array2 = array_values($array);
    echo key($array);
    echo key($array2);

    // will output:
    // 1
    // 0
?>

There are a lot Methods like array_merge($array) that will neither reset the pointer of $array nor copy the pointer to $array2. Have a look on this.
I Hope this was a little helpfull.
ThinkMedical at Gmail dot com 03-Sep-2008 12:27
regarding references with foreach, you can use them directly. Obviating various posts which provide many lines of 'work arounds'.

$array = array(1,2,3,4,5);

foreach($array as &$value)

or use $key

foreach($array as $key => $value)
{
    $array[$key] = '...';
}
darkside at i dot ua 11-Dec-2007 12:36
This class implements simple operations with array

<?php
class Steps {

    private $all;
    private $count;
    private $curr;

    public function __construct () {

      $this->count = 0;

    }

    public function add ($step) {

      $this->count++;
      $this->all[$this->count] = $step;

    }

    public function setCurrent ($step) {

      reset($this->all);
      for ($i=1; $i<=$this->count; $i++) {
        if ($this->all[$i]==$step) break;
        next($this->all);
      }
      $this->curr = current($this->all);

    }

    public function getCurrent () {

      return $this->curr;

    }

    public function getNext () {

      self::setCurrent($this->curr);
      return next($this->all);

    }

  }
?>

usage example:

<?php
   $steps = new Steps();
   $steps->add('one');
   $steps->add('two');
   $steps->add('three');
   $steps->setCurrent('one');
   echo $steps->getCurrent()."<br />";
   echo $steps->getNext()."<br />";
   $steps->setCurrent('two');
   echo $steps->getCurrent()."<br />";
   echo $steps->getNext()."<br />";
?>
gg2005 at gmail dot com 06-Feb-2007 10:32
Don't confuse next with continue!

If you're a Perl developer starting with PHP, you might try to use "next" inside a loop to skip to the next iteration...

i.e.,

foreach ($things as $thing) {
  if (something I don't like about $thing) {
   next;
  }
  blah....
}

The php compiler will take next... but it's not going to work.

Do this instead:
foreach ($things as $thing) {
  if (something I don't like about $thing) {
   continue;
  }
  blah....
}
tino at infeon dot com 13-May-2006 05:48
this may be handy and i didnt know where else to post it.. i need a simple function to cycle through an array i eventually made it into a class so i could have multiple cycles.. if you like it or find it usefull please email me and let me know

class Cycle
{
    var $position;
    var $dataArray;
    var $dataArrayCount;

    function Cycle()
    {
        $this->dataArray = func_get_args();
        $this->dataArrayCount = count($this->dataArray);
    }

    function Display()
    {
        $this->position = (!isset($this->position) || $this->position >= ($this->dataArrayCount - 1)) ? 0 : $this->position += 1;
        return $this->dataArray[$this->position];
    }

}

$bgColor = new Cycle('#000000', '#FFFFFF', '#FF0000');

echo $bgcolor->Display();
//returns #000000
echo $bgcolor->Display();
//returns #FFFFFF
echo $bgcolor->Display();
//returns #FF0000
echo $bgcolor->Display();
//returns #000000
brentimus 28-Apr-2005 07:10
Papipo's function below is usefull in concept but does not work.

"Since you do not pass the array by reference, its pointer is only moved inside the function."

This is true, but the array you are manipulating in your has_next() function will have it's pointer set to the first element, not the same position as the original array. What you want to do is pass the array to the has_next() function via reference. While in the has_next() function, make a copy of the array to work on. Find out the current pointer position of the original array and set the pointer on the working copy of the array to the same element. Then you may test to see if the array has a "next" element.

Try the followig insetad:

<?php
function has_next(&$array)
{
    $A_work=$array;  //$A_work is a copy of $array but with its internal pointer set to the first element.
    $PTR=current($array);
    array_set_pointer($A_work, $PTR);

    if(is_array($A_work))
    {
        if(next($A_work)===false)
            return false;
        else
            return true;
    }
    else
        return false;
}

function array_set_pointer(&$array, $value)
{
    reset($array);
    while($val=current($array))
    {
        if($val==$value)
            break;

        next($array);
    }
}
?>
papipo's gmail account 13-Oct-2004 10:47
I need to know if an array has more items, but without moving array's internail pointer. Thats is, a has_next() function:

<?php
function has_next($array) {
    if (is_array($array)) {
        if (next($array) === false) {
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

$array = array('fruit', 'melon');
if (has_next($array)) {
    echo next($array);
}

// prints 'melon'
?>

Since you do not pass the array by reference, its pointer is only moved inside the function.
Hope that helps.
lukasz at karapuda dot com 18-Aug-2004 07:06
This function will return the previous,next neighbors of an array entry within an associative array. If the specified $key points to the last or first element of the array, the first or last keys of the array will be returned consecutively. This is an improved version of the same function posted earlier.

<?php
function array_neighbor($arr, $key)
{
   $keys = array_keys($arr);
   $keyIndexes = array_flip($keys);

   $return = array();
   if (isset($keys[$keyIndexes[$key]-1])) {
       $return[] = $keys[$keyIndexes[$key]-1];
   }
   else {
       $return[] = $keys[sizeof($keys)-1];
   }

   if (isset($keys[$keyIndexes[$key]+1])) {
       $return[] = $keys[$keyIndexes[$key]+1];
   }
   else {
       $return[] = $keys[0];
   }

   return $return;
}
?>
court shrock 21-May-2004 01:36
This code returns neighbors of the specified key.  The result will be empty if it doesn't have any neighbors.  My approach was to use the order of keys to determine neighbors, which is differnet from just getting the next/previous element in an array.  Feel free to point out stupidities :)

<?php

function array_neighbor($arr, $key)
{
    krsort($arr);
    $keys = array_keys($arr);
    $keyIndexes = array_flip($keys);

    $return = array();
    if (isset($keys[$keyIndexes[$key]-1]))
        $return[] = $keys[$keyIndexes[$key]-1];
    if (isset($keys[$keyIndexes[$key]+1]))
        $return[] = $keys[$keyIndexes[$key]+1];

    return $return;
}

?>
bm at ANTISPAM dot solidwave dot com 17-Apr-2004 06:49
Take care when replacing code using reset()/next() with code using foreach as foreach does not update the array's internal pointer.  This means you cannot, say, use next() to skip an element in foreach loop, or use current() within a function to get a reference to the current element.  You probably have code depending on this internal pointer and replacing it will be more work than you anticipated.

See http://www.php.net/foreach


별칭: current

  • ≪ next
  • prev ≫
  • PHP Manual
  • 배열 함수 목록
  • 별칭: current

pos

(PHP 4, PHP 5, PHP 7)

pos — 별칭: current()

설명

이 함수는 다음 함수의 별칭입니다: current()

User Contributed Notes

vinicius at codemakers dot com dot br 07-May-2014 12:04
<?php
    $countries = array("Brazil", "England", "Japan", "France");

    // Get the current position
    echo pos($countries); // print Brazil

    // Go to next position
    echo next($countries); // print England

    // Current position
    echo pos($countries); //  print England

    // Previous position
    echo prev($countries); // print Brazil

    // End Position
    echo end($countries); // print France

    // Current position
    echo pos($countries); // print France
ranit8085 at gmail dot com 29-Jun-2013 07:03
<?php
$letter = array('a', 'b', 'c', 'd','e','f');
$cur_letter = current($letter);

$arr = array();
var_dump(current($arr));

?>


내부 배열 포인터를 후진

  • ≪ pos
  • range ≫
  • PHP Manual
  • 배열 함수 목록
  • 내부 배열 포인터를 후진

prev

(PHP 4, PHP 5, PHP 7)

prev — 내부 배열 포인터를 후진

설명

mixed prev ( array &$array )

내부 배열 포인터를 후진합니다.

prev()는 내부 배열 포인터를 한칸 전진시키는 대신 후진하는 점을 제외하면 next()처럼 작동합니다.

인수

array

입력 배열.

반환값

내부 배열 포인터가 가리키는 이전 위치의 배열 값을 반환하거나, 더 이상 원소가 없을 경우 FALSE를 반환합니다.

예제

Example #1 prev() 용례와 관련 함수

<?php
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport); // $mode = 'foot';
$mode = next($transport);    // $mode = 'bike';
$mode = next($transport);    // $mode = 'car';
$mode = prev($transport);    // $mode = 'bike';
$mode = end($transport);     // $mode = 'plane';
?>

주의

Warning

이 함수는 논리 FALSE를 반환하지만, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

Note: 배열의 끝과 boolean FALSE 원소를 구별할 수 없습니다. FALSE 원소를 가지는 배열을 올바르게 따라가려면, each()를 참고하십시오.

참고

  • current() - 배열의 현재 원소를 반환
  • end() - 배열 내부 포인터가 마지막 원소를 가리키게 설정
  • next() - 배열의 내부 배열 포인터를 전진
  • reset() - 배열의 내부 포인터를 첫 원소로 설정
  • each() - 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진

User Contributed Notes

MicroVB INC 31-Mar-2014 02:09
Some corrections to the previous function.
- When prev() and next() distance is now calculated properly returning the 'closer' one.
- When exact is false, and an item exists with the key searched for, it now returns that entry instead of next/prev closest
- At ends of the array, sometimes when moving past using next() or prev(), it caused results to die. This was corrected.
- Added field to result stating whether the match was 'exact' ('exact'=>true), or nearest ('exact'=>false)

<?php
function nearest($array, $value, $exact=false) {
    // Initialize Variables
    $next = false;
    $prev = false;
    $return = false;

    $arr_begin = false;
    $arr_end = false;

    $exact_match = false;

    if($exact && isset($array[$value])) {
        // If exact match found, and searching for exact (not nearest), return result.
        $return = Array($value=>$array[$value]);
        $exact_match = true;
    } elseif (!$exact && isset($array[$value])) {
        // If exact match found, and searching for nearest, return result.
        $return = Array($value=>$array[$value]);
        $exact_match = true;
    } else {

        if(!isset($array[$value]) && !$exact) {
            // Insert element that doesn't exist so nearest can be found
            $array[$value] = '-';
        }

        // Sort array so injected record is near relative numerics
        ksort($array); // Sort array on key

        // Set start and end keys
        $arr_begin = key($array);
        end($array);
        $arr_end = key($array);
        reset($array);

        // Loop through array until match is found, then set $prev and $next to the respective keys if exist
        while ( !is_null($key = key($array)) ) {
            $val = current($array);
            if($key == $value) {
                if($value <> $arr_begin) {
                    prev($array); // Get entry before this one
                    $prev = key($array);
                }

                next($array);         // Skip current entry as this was what we were looking for nearest to

                if($value <> $arr_end) {
                    next($array); // Get entry after this one
                    $next = key($array);
                }
                break;
            }
            next($array);
        }

        if($prev && $next) {
            if(($value - $prev) < ($next - $value)) {
                // Previous is closer
                $return = Array($prev=>$array[$prev]);
            } else {
                // Next is closer
                $return = Array($next=>$array[$next]);
            }
        } elseif ($prev || $next) {
            if($prev) {
                // Only Previous exists
                $return = Array($prev=>$array[$prev]);
            } elseif ($next) {
                // Only Next exists
                $return = Array($next=>$array[$next]);
            }
        }
    }

    // Return resulting Array().   $return is false if nothing matches the closest conditions, or if exact is specified and key does not exist.
    if($return) {
        return $return+Array('exact'=>$exact_match);
    } else {
        return false;
    }
}
?>

Just to clarify, this function will return either an Array() containing the $key=>$value pair of the result and 'exact'=>(bool) whether the match was exact or not.

To access the $key=>$value pair, can be done as follows :
<?php

    $result = nearest( ... );  // Just here for clarification of the position for the code that follows.

    if($result) {
        // Output the Key
        echo key($result);

        // Output the Value
        echo $result( key($result) );

        // Output if exact match
        if($result['exact']) {
            echo 'This was an EXACT match';
        } else {
            echo 'This result was found with fuzzy logic';
        }

    } else {

        echo 'No result found';

    }

?>
MicroVB INC 31-Mar-2014 10:17
This function searches for the closest element in an array by key value, and returns the key/value pair, or false if not found.

<?php
function nearest($array, $value, $exact=false) {
        // Initialize Variables
    $next = false;
    $prev = false;
    $return = false;

    if(!isset($array[$value]) && !$exact) {
        // Insert element that doesn't exist so nearest can be found
        $array[$value] = '-';
    }

    if($exact && isset($array[$value])) {
                // If exact match found, and searching for exact (not nearest), return result.
        $return = Array($value=>$array[$value]);
    } else {
            // Sort array so injected record is near relative numerics
        ksort($array); // Sort array on key

                // Loop through array until match is found, then set $prev and $next to the respective keys if exist
        while ( !is_null($key = key($array)) ) {
            $val = current($array);
            if($key == $value) {
                prev($array); // Get entry before this one
                $prev = key($array);
                next($array);         // Skip current entry as this was what we were looking for nearest to
                next($array); // Get entry after this one
                $next = key($array);
                break;
            }
            next($array);
        }

        if($prev && $next) {
            if(($long - $prev) > ($next - $long)) {
                                 // Previous is closer
                $return = Array($prev=>$array[$prev]);
            } else {
                                 // Next is closer
                $return = Array($next=>$array[$next]);
            }
        } elseif ($prev || $next) {
            if($prev) {
                                 // Only Previous exists
                $return = Array($prev=>$array[$prev]);
            } elseif ($next) {
                                 // Only Next exists
                $return = Array($next=>$array[$next]);
            }
        }
    }

        // Return resulting Array().   $return is false if nothing matches the closest conditions, or if exact is specified and key does not exist.
    return $return;
}
?>

Example usage (to lookup the closest color in an array)
<?php
$mycolors= Array(
    5001046=>'Abbey',
    1774596=>'Acadia',
    8171681=>'Acapulco',
    6970651=>'Acorn',
    13238245=>'Aero Blue',
    7423635=>'Affair',
    8803850=>'Afghan Tan',
    13943976=>'Akaroa',
    16777215=>'Alabaster',
    16116179=>'Albescent White',
    10176259=>'Alert Tan',
    30371=>'Allports'
);

// Color to search for in Hex
$color = 'C0C0C0';

// Convert Hex to Long to compare with array() keys
$colorlong = base_convert($color,16,10);

// Check for an exact match
$result = nearest($mycolors, $colorlong, true);
if($result) {
    echo "An exact match was found for #" . $color . " which is called '" . $result[key($result)] . "'";
} else {
    echo "No exact match was found";
}

if(!$result) {
    // Check for closest match
    $result = nearest($mycolors, $colorlong, true);
    if($result) {
        // Match found
        echo "The closest match for #" . $color . " is #" . base_convert(key($result),10,16) . " which is called '" . $result[key($result)] . "'";
    } else {
        // No match found
        echo "No match was found for #" . $color;
    }
}
?>
soapergem at gmail dot com 29-May-2009 09:06
Here's a slight revision to xmlich02's backwards iteration example. The problem with his/her example is that it will halt if any of the array elements are boolean false, while this version will not.

<?php

end($ar);
while ( !is_null($key = key($ar)) ) {
    $val = current($ar);
    echo "{$key} => {$val}\n";
    prev($ar);
}

?>


원소의 범위를 가지는 배열 생성

  • ≪ prev
  • reset ≫
  • PHP Manual
  • 배열 함수 목록
  • 원소의 범위를 가지는 배열 생성

range

(PHP 4, PHP 5, PHP 7)

range — 원소의 범위를 가지는 배열 생성

설명

array range ( mixed $low , mixed $high [, number $step ] )

원소의 범위를 가지는 배열을 생성합니다.

인수

low

낮은 값.

high

높은 값.

step

step 값이 주어지면, 연속적인 원소들의 증가값으로 사용될 것이다. step은 양수로 주어져야 한다. 이 값이 주어지지 않으면, step의 기본값은 1이다.

반환값

low에서 high(포함)까지의 원소를 갖는 배열을 반환한다, low > high 일 경우에는 일련값은 high에서 low까지가 될것이다.

변경점

버전 | 설명
5.0.0 | 선택적인 step 인수 추가.
4.1.0에서 4.3.2 | PHP 버전 4.1.0에서 4.3.2까지는, range()가 수치 문자열을 정수가 아닌 문자열로 봤습니다. 그래서, 문자 연속값으로 사용되었습니다. 예를 들면, "4242"는 "4"로 취급했습니다.
4.1.0 | PHP 4.1.0 이전에는, range()가 증가하는 정수 배열만 생성했습니다. 문자 연속값과 감소하는 배열 지원은 PHP 4.1.0에서 추가되었습니다. 문자 연속값은 길이 1로 제한됩니다. 1을 초과하는 길이가 들어오면, 첫번째 문자만 사용합니다.

예제

Example #1 range() 예제

<?php
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach (range(0, 12) as $number) {
    echo $number;
}

// 5.0.0 부터 step 인수가 추가되었다
// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
foreach (range(0, 100, 10) as $number) {
    echo $number;
}

// 4.1.0 부터 문자 연속값이 지원된다
// array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
foreach (range('a', 'i') as $letter) {
    echo $letter;
}
// array('c', 'b', 'a');
foreach (range('c', 'a') as $letter) {
    echo $letter;
}
?>

참고

  • shuffle() - 배열을 섞습니다
  • array_fill() - 값으로 배열 채우기
  • foreach

User Contributed Notes

luca.favorido ATgmailDOT com 28-Jun-2016 04:58
The function "range" is very useful to get an array of characters as range('C','R') does.

At work, I had to extend the function range($a,$b) to work in this special case: with two uppercase strings $a and $b, it should return all the possible strings between $a and $b.
This could be used for example to get the excel column indexes.
e.g. <?php range('A','AD') ==> array('A','B','C',...,'Z','AA','AB','AC','AD') ?>

So I wrote the function getrange($min,$max) that exactly does this.

<?php

function getcolumnrange($min,$max){
      $pointer=strtoupper($min);
      $output=array();
      while(positionalcomparison($pointer,strtoupper($max))<=0){
         array_push($output,$pointer);
         $pointer++;
      }
      return $output;
}

function positionalcomparison($a,$b){
   $a1=stringtointvalue($a); $b1=stringtointvalue($b);
   if($a1>$b1)return 1;
   else if($a1<$b1)return -1;
   else return 0;
}

/*
* e.g. A=1 - B=2 - Z=26 - AA=27 - CZ=104 - DA=105 - ZZ=702 - AAA=703
*/
function stringtointvalue($str){
   $amount=0;
   $strarra=array_reverse(str_split($str));

   for($i=0;$i<strlen($str);$i++){
      $amount+=(ord($strarra[$i])-64)*pow(26,$i);
   }
   return $amount;
}
?>
qz 20-Jan-2016 08:58
If you're looking to fill an array to get a hash with 0-9 numerical values, using
range(0,9);
is a faster solution compared to
array_fill(0, 10, '');
moficer at host dot sk 16-Jan-2016 12:17
php 5.6.16

<?php
var_export(range('Z', 'a'));

/*
array (
  0 => 'Z',
  1 => '[',
  2 => '\\',
  3 => ']',
  4 => '^',
  5 => '_',
  6 => '`',
  7 => 'a',
)
 */
Alien426 29-Oct-2015 11:09
The function will generate an array of integers even if your numerical parameters are enclosed in quotes.
<?php
var_dump( range('1', '2') ); // outputs  array(2) { [0]=> int(1) [1]=> int(2) }
?>

An easy way to get an array of strings is to map strval() to the range:
<?php
var_dump( array_map('strval', range('1', '2')) ); // outputs  array(2) { [0]=> string(1) "1" [1]=> string(1) "2" }
?>
unicod3 at hotmail dot com 16-Oct-2014 02:43
a function to get column index by letter

function getColumnNumber($char){
    $alphabet = range('a','z');
    $alphabet2 = range('a','z');
    $newAlphabet = $alphabet;
    foreach($alphabet as $k => $r)
    {
        foreach($alphabet2 as $row){
            $newAlphabet[] = $r.$row;
        }
    }
    $key = array_search($char, $newAlphabet);
    return ($key !== false) ? $key : null;
}
lsblsb at gmx dot de 01-May-2014 12:09
I needed a function, that creates a letter range with arbitrary length.
You specify via the $length parameter, how many entries you need.
Logic is analog to the logic of the column-titles in a calc-sheet.

<?php

/**
  * create a letter range with arbitrary length
  * @param int $length
  * @return array
  */
function createLetterRange($length)
{
    $range = array();
    $letters = range('A', 'Z');
    for($i=0; $i<$length; $i++)
    {
        $position = $i*26;
        foreach($letters as $ii => $letter)
        {
            $position++;
            if($position <= $length)
                $range[] = ($position > 26 ? $range[$i-1] : '').$letter;
        }
    }
    return $range;
}
?>
php at keith tyler dot com 27-Feb-2014 07:22
So with the introduction of single-character ranges to the range() function, the internal function tries to be "smart", and (I am inferring from behavior here) apparently checks the type of the incoming values. If one is numeric, including numeric string, then the other is treated as numeric; if it is a non-numeric string, it is treated as zero.

But.

If you pass in a numeric string in such a way that is is forced to be recognized as type string and not type numeric, range() will function quite differently.

Compare:

<?php
echo implode("",range(9,"Q"));
// prints 9876543210

echo implode("",range("9 ","Q"));  //space after the 9
// prints 9:;<=>?@ABCDEFGHIJKLMNOPQ

echo implode("",range("q","9 "));
// prints qponmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:987654
?>

I wouldn't call this a bug, because IMO it is even more useful than the stock usage of the function.
jazzduck AT gmail DOT com 22-Jan-2014 05:32
Despite the line above that says that the $step value should be "given as a positive number," the range() function will in fact correctly handle reversed (decrementing) ranges. For example:

<?php print_r( range( 24, 20 ) ); ?>
Array
(
    [0] => 24
    [1] => 23
    [2] => 22
    [3] => 21
    [4] => 20
)

<?php print_r( range( 20, 11, -3 ) ); ?>
Array
(
    [0] => 20
    [1] => 17
    [2] => 14
    [3] => 11
)

It will actually ignore the sign of the $step argument, and determine whether to increment or decrement based purely on whether $start > $end or $end > $start. For example:

<?php print_r( range( 20, 11, 3 ) ); ?>
Array
(
    [0] => 20
    [1] => 17
    [2] => 14
    [3] => 11
)

<?php print_r( range( 11, 20, -3 ) ); ?>
Array
(
    [0] => 11
    [1] => 14
    [2] => 17
    [3] => 20
)
pyetrosafe at gmail dot com 27-Sep-2013 10:36
To create a simple array or a multidimensional array with defined size and null values??, use this expression:

<?php

$SimpleArray = array_map(function($n) { return null; }, range(1, 3) );
$MultiArray = array_map(function($n) { return array_map(function($n) { return null; }, range(1, 2) ); }, range(1, 3) );

var_dump($SimpleArray);
var_dump($MultiArray);

// And will print:
?>
>>$SimpleArray
array(3) {
  [0]=>  NULL
  [1]=>  NULL
  [2]=>  NULL
}

>>$MultiArray
array(3) {
  [0]=>  array(2) {
    [0]=>    NULL
    [1]=>    NULL
  }
  [1]=>  array(2) {
    [0]=>    NULL
    [1]=>    NULL
  }
  [2]=>  array(2) {
    [0]=>    NULL
    [1]=>    NULL
  }
}

?>
krdr dot mft at gmail dot com 20-Aug-2013 06:48
I've been introduced with range() function not so long ago, and I found that examples about it is somewhat wrong, even inefficient:

<?php
$o = "";
$time_start = microtime(true);
foreach(range(1, 10000) as $val) {
    $o .= $val;
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo 'rangein: '.$time.'<br />';

$o = "";
$time_start = microtime(true);
$a = range(1, 10000);
foreach($a as $val) {
    $o .= $val;
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo 'rangeout: '.$time.'<br />';

?>

Which gives results:

rangein: 0.0025348663330078
rangeout: 0.0019199848175049

In some cases difference is even bigger and proportional to the range generated. I suppose that results of range() are cached/hashed.

Note: execution order does affects execution times, but difference still exists
Ray.Paseur often uses Gmail 07-Jun-2013 12:16
Interestingly, these two statements produce identical 26-character alphabet arrays.

<?php
$arr = range('A',  'Z');
$arr = range('AA', 'ZZ');
gtisza at gmail dot com 25-Apr-2013 07:27
You might expect range($n, $n-1) to be an empty array (as in e.g. Python) but actually PHP will assume a step of -1 if start is larger than end.
Palz 30-Oct-2012 07:59
To create a range array like

Array
(
    [11] => 1
    [12] => 2
    [13] => 3
    [14] => 4
)

combine two range arrays using array_combine:

array_combine(range(11,14),range(1,4))
ktamas77 at gmail dot com 04-Aug-2012 07:59
if you need zero padding, string prefixes or any other masks, then a simple combination of array_map, inline functions and sprintf is your friend.

<?php

$a = array_map(function($n) { return sprintf('sample_%03d', $n); }, range(50, 59) );

print_r($a);

?>

Will result:

Array
(
    [0] => sample_050
    [1] => sample_051
    [2] => sample_052
    [3] => sample_053
    [4] => sample_054
    [5] => sample_055
    [6] => sample_056
    [7] => sample_057
    [8] => sample_058
    [9] => sample_059
)
me at phpscott dot com 24-May-2012 02:37
So, I needed a quick and dirty way to create a dropdown select for hours, minutes and seconds using 2 digit formatting, and to create those arrays of data, I combined range with array merge..

<?php
$prepend = array('00','01','02','03','04','05','06','07','08','09');
$hours     = array_merge($prepend,range(10, 23));
$minutes     = array_merge($prepend,range(10, 59));
$seconds     = $minutes;
?>

Super simple.
dries at volta dot be 09-Feb-2012 08:30
Ever wanted to generate an array with a range of column names for use in Excel file related parsing?
I've wrote a function that starts at the A column and adds column names up until the column you specified.

<?php

/**
 * This function creates an array with column names up until the column
 * you specified.
*/
function createColumnsArray($end_column, $first_letters = '')
{
  $columns = array();
  $length = strlen($end_column);
  $letters = range('A', 'Z');

  // Iterate over 26 letters.
  foreach ($letters as $letter) {
      // Paste the $first_letters before the next.
      $column = $first_letters . $letter;

      // Add the column to the final array.
      $columns[] = $column;

      // If it was the end column that was added, return the columns.
      if ($column == $end_column)
          return $columns;
  }

  // Add the column children.
  foreach ($columns as $column) {
      // Don't itterate if the $end_column was already set in a previous itteration.
      // Stop iterating if you've reached the maximum character length.
      if (!in_array($end_column, $columns) && strlen($column) < $length) {
          $new_columns = createColumnsArray($end_column, $column);
          // Merge the new columns which were created with the final columns array.
          $columns = array_merge($columns, $new_columns);
      }
  }

  return $columns;
}

?>

Usage:

<?php

// Return an array with all column names from A until and with BI.
createColumnsArray('BI');

?>
Aram Kocharyan 28-Feb-2011 04:58
Here's a function to generate ranges from strings:

<?php

/*  Creates an array of integers based on a given range string of format "int - int"
    Eg. range_str('2 - 5'); */
function range_str($str) {
    preg_match('#(\\d+)\\s*-\\s*(\\d+)#', $str, $matches);
    if ( count($matches) == 3 ) {
        return range($matches[1], $matches[2]);
    }
    return FALSE;
}

// Test
$array = range_str(' 2 - 4 ');
print_r($array);

?>

This outputs:

Array
(
    [0] => 2
    [1] => 3
    [2] => 4
)
jay at NOspam dot myd3 dot com 30-Apr-2009 02:54
This is a modified version of thomas' range_string() function. It's simpler, cleaner, and more robust, but it lacks the advanced features his function had, hopefully it will be of assitance to someone.

Examples:

    input: "1, 2, 3, 4, 5, 6" --> output: 1, 2, 3, 4, 5, 6
    input: "1-6" --> output: 1, 2, 3, 4, 5, 6
    input: "1-6" --> output: 1, 2, 3, 4, 5, 6
    input: "1 - -6" --> output: 1, 2, 3, 4, 5, 6
    input: "0 - 0" --> output: 0
    input: "1, 4-6, 2" --> output: 1, 2, 4, 5, 6
    input: "6,3-1" --> output: 1, 2, 3, 6

<?php

define('RANGE_ARRAY_SORT', 1);
define('RANGE_ARRAY', 2);
define('RANGE_STRING_SORT', 3);
define('RANGE_STRING', 4);

function range_string($range_str, $output_type = RANGE_ARRAY_SORT)
{
    // Remove spaces and nother non-essential characters
    $find[]    = "/[^\d,\-]/";
    $replace[] = "";

    // Remove duplicate hyphens
    $find[]    = "/\-+/";
    $replace[] = "-";

    // Remove duplicate commas
    $find[]    = "/\,+/";
    $replace[] = ",";

    $range_str = preg_replace($find, $replace, $range_str);

    // Remove any commas or hypens from the end of the string
    $range_str = trim($range_str,",-");

    $range_out = array();
    $ranges    = explode(",", $range_str);

    foreach($ranges as $range)
    {

        if(is_numeric($range) || strlen($range) == 1)
        {
            // Just a number; add it to the list.
            $range_out[] = (int) $range;
        }
        else if(is_string($range))
        {

            // Is probably a range of values.
           $range_exp = preg_split("/(\D)/",$range,-1,PREG_SPLIT_DELIM_CAPTURE);

            $start = $range_exp[0];
            $end   = $range_exp[2];

            if($start > $end)
            {
                for($i = $start; $i >= $end; $i -= 1)
                {
                    $range_out[] = (int) $i;
                }
            }
            else
            {
                for($i = $start; $i <= $end; $i += 1)
                {
                    $range_out[] = (int) $i;
                }
            }

        }
    }

    switch ($output_type) {
        case RANGE_ARRAY_SORT:
            $range_out = array_unique($range_out);
            sort($range_out);

        case RANGE_ARRAY:
            return $range_out;
            break;

        case RANGE_STRING_SORT:
            $range_out = array_unique($range_out);
            sort($range_out);

        case RANGE_STRING:

        default:
            return implode(", ", $range_out);
            break;
    }
}

// Sample Usage:
$range = range_string("6, 3-1");

?>
ThinkMedical at Gmail dot com 26-Aug-2008 02:11
foreach(range()) whilst efficiant in other languages, such as python, it is not (compared to a for) in php*.

php is a C-inspired language and thus for is entirely in-keeping with the lanuage aethetic to use it

<?php
//efficiant
for($i = $start; $i < $end; $i+=$step)
{
        //do something with array
}

//inefficiant
foreach(range($start, $end, $step) as $i)
{
        //do something with array
}
?>

That the officiant documentation doesnt mention the for loop is strange.

Note however, that in PHP5 foreach is faster than for when iterating without incrementing a variable.

* My tests using microtime and 100 000 iterations consistently (~10 times) show that for is 4x faster than foreach(range()).
captvanhalen at gmail dot com 27-Mar-2008 07:33
Here is a home rolled range() function that uses the step feature for those unfortunate souls who cannot use PHP5:

<?php
function my_range( $start, $end, $step = 1) {

    $range = array();

    foreach (range( $start, $end ) as $index) {

        if (! (($index - $start) % $step) ) {
            $range[] = $index;
        }
    }

    return $range;
}
?>
chris at laflash dot org 10-May-2007 01:47
Quick HTML menus with minimum and maximum sets of years:

<?php
    /*
    ** Quick HTML menus with minimum and maximum sets of years.
    ** @author Chris Charlton <chris@laflash.org>
    ** @license FREE!
    */

    // Years range setup
    $year_built_min = 1900;
    $year_built_max = date("Y");
?>
<select id="yearBuiltMin" size="1">
    <?php // Generate minimum years

        foreach (range($year_built_min, $year_built_max) as $year) { ?>
        <option value="<?php echo($year); ?>"><?php echo($year); ?></option>
        <?php } ?>
</select>

<select id="yearBuiltMax" size="1">
      <?php // Generate max years

        foreach (range($year_built_max, $year_built_min) as $year) { ?>
        <option value="<?php echo($year); ?>"><?php echo($year); ?></option>
        <?php } ?>
</select>
m0sh3 at hotmail dot com 02-Mar-2007 02:46
Here's how i use it to check if array is associative or not:

<?php

if (array_keys($arr)===range(0, sizeof($arr)-1)) {
// not associative array

} else {
// associative array

}

?>
manuel at levante dot de 07-Nov-2006 02:25
<?php
function srange ($s) {
  preg_match_all("/([0-9]{1,2})-?([0-9]{0,2}) ?,?;?/", $s, $a);
  $n = array ();
  foreach ($a[1] as $k => $v) {
    $n  = array_merge ($n, range ($v, (empty($a[2][$k])?$v:$a[2][$k])));
  }
  return ($n);
}

$s = '1-4 6-7 9-10';
print_r(srange($s));
?>

Return:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 6
    [5] => 7
    [6] => 9
    [7] => 10
)
subscription101 at hotmail dot com 08-Jan-2006 03:36
A much simpler way of creating a range of even numbers is by starting with an even number:

<?php

    range(2, 10, 2);

?>
emory underscore smith at hotmail 21-Aug-2005 04:53
since its not stated explicitly above, thought id point out that you arent limited to using integers.

however, be careful when doing so, as you might not get the range you expect!

to illustrate:

<?php
$am = range(500,1600,10);
$fm = range(88.1,107.9,.2);
print_r($am);
print_r($fm);
?>

print_r($am) yields the expected result:

Array
(
    [0] => 500
    [1] => 510
    [2] => 520
    ...
    [109] => 1590
    [110] => 1600
)

print_r($fm), however, falls a bit (1%) short:

Array
(
    [0] => 88.1
    [1] => 88.3
    [2] => 88.5
    ...
    [97] => 107.5
    [98] => 107.7
)

so, if you want to use a non-integral step size params for numeric ranges, be sure to account for fp representation accuracy and error accumulation; a step size of something like pi or 1/10 could spell disaster for a large range. if in doubt, use integral steps and divide ... something like <?php range(88.1,108,.2) ?> might work to recover 107.9, but would not be scalable like, say <?php array_map(create_function('$x','return $x/10;'),range(881,1079,2)) ?>.

-emory
derek at php dot net 08-May-2005 03:13
This should emulate range() a little better.
<?php
function range_wroar($low, $high, $step = 1) {
    $arr = array();
    $step = (abs($step)>0)?abs($step):1;
    $sign = ($low<=$high)?1:-1;
    if(is_numeric($low) && is_numeric($high)) {
        //numeric sequence
        for ($i = (float)$low; $i*$sign <= $high*$sign; $i += $step*$sign)
            $arr[] = $i;
    }    else    {
        //character sequence
        if (is_numeric($low))
            return $this->range($low, 0, $step);
        if (is_numeric($high))
            return $this->range(0, $high, $step);
        $low = ord($low);
        $high = ord($high);
        for ($i = $low; $i*$sign <= $high*$sign; $i += $step*$sign) {

            $arr[] = chr($i);
        }
    }
    return $arr;
}
?>
j dot gizmo at aon dot at 23-Sep-2004 01:23
i figured i'd add some more functionality to the myRange() functions below.
now you can, besides giving a $step parameter,
1. count backwards
2. count with letters
3. give whatever parameter you want, there's nothing (i know of) that will cause an endless loop (try a negative $step for the previous function....)

<?php
function myRange($num1, $num2, $step=1)
{
    if (is_numeric($num1) && is_numeric($num2))
    {
        //we have a numeric range
        $step = ( abs($step)>0 ? abs($step) : 1 ); //make $step positive
        $dir = ($num1<=$num2 ? 1 : -1); //get the direction
        for($i = (float)$num1; $i*$dir <= $num2*$dir; $i += $step*$dir)
        {
            $temp[] = $i;
        }
    }
    else
    {
        //we have a character range
        $num1=ord((string)$num1); //convert to ascii value
        $num2=ord((string)$num2);
        $step = ( abs($step)>0 ? abs($step) : 1 ); //make $step positive
        $dir = ($num1<=$num2 ? 1 : -1); //get direction
        for($i = $num1; $i*$dir <= $num2*$dir; $i += $step*$dir)
        {
            $temp[] = chr($i);
        }
    }
    return $temp;
}

print_r(myRange( 1, 3, 0.5 )); //you can use fractional steps
print_r(myRange( "a", "k", 3 )); //or count letters
print_r(myRange( "5", "9" )); //numbers are detected even if hidden in strtings
print_r(myRange( "!", "%", 1/pi() )); //or mess around with senseless parameters

?>


배열의 내부 포인터를 첫 원소로 설정

  • ≪ range
  • rsort ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열의 내부 포인터를 첫 원소로 설정

reset

(PHP 4, PHP 5, PHP 7)

reset — 배열의 내부 포인터를 첫 원소로 설정

설명

mixed reset ( array &$array )

reset()은 array의 내부 포인터를 첫번째 원소로 되감고, 첫번째 배열 원소의 값을 반환합니다.

인수

array

입력 배열.

반환값

첫번째 배열 원소의 값을 반환하거나, 빈 배열이면 FALSE를 반환합니다.

예제

Example #1 reset() 예제

<?php

$array = array('step one', 'step two', 'step three', 'step four');

// by default, the pointer is on the first element
echo current($array) . "<br />\n"; // "step one"

// skip two steps
next($array);
next($array);
echo current($array) . "<br />\n"; // "step three"

// reset pointer, start again on step one
reset($array);
echo current($array) . "<br />\n"; // "step one"

?>

참고

  • current() - 배열의 현재 원소를 반환
  • each() - 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진
  • end() - 배열 내부 포인터가 마지막 원소를 가리키게 설정
  • next() - 배열의 내부 배열 포인터를 전진
  • prev() - 내부 배열 포인터를 후진

User Contributed Notes

Bartek Ferek 30-Aug-2016 08:16
As for taking first key of an array, it's much more efficient to RESET and then KEY, rather then RESET result of ARRAY_KEYS (as sugested by gardnerjohng at gmail dot com).

<?php
reset($someArray);
echo key($someArray);
?>

This will give the same result but is much much faster. Larger arrays, better performance. Tested on 100-elements long array with 16 times faster results.
arne dot slabbinck at duo dot be 02-Oct-2015 12:43
Info:

Following code gives a strict warning in 5.4.45

      return reset(array_keys($result['node']));

"Strict warning: Only variables should be passed by reference"

So should be:

      $keys = array_keys($result['node']);
      return reset($keys);
arne dot ludwig at posteo dot de 18-Sep-2015 06:22
In response to gardnerjohng's note to retrieve the first _key_ of an array:

To retrieve the first _key_ of an array you can use the combination of reset() and key().

<?php
    $properties = array(
        'colour'   => 'grey',
        'flavour'  => 'rubber',
        'name'     => 'Mouse Ball',
        'texture'  => 'rubbery'
    );

    reset($properties);
    echo key($properties); // => 'colour'
?>

I prefer this solution as you don't have to create the keys array. This should (not measured) improve performance on large arrays.
gardnerjohng at gmail dot com 18-Sep-2014 02:54
If you need an easy way to retrieve the first *key* in an array (instead of the value), it can be done like this:

<?php
    $properties = array(
        'colour'   => 'grey',
        'flavour'  => 'rubber',
        'name'     => 'Mouse Ball',
        'texture'  => 'rubbery'
    );

    #    Will output "colour".
    echo reset(array_keys($properties));
?>
Potentially helpful for configuration files that define a sequence of items as an associative array.
Mladen Janjetovic 16-May-2013 07:07
Note that you can't use pointer here. It will reset the iteration counter in this case.
foreach($array as $key=>&$value) {...}

Use standard foreach instead
foreach($array as $key=>$value) {...}
milo at mdlwebsolutions dot com 04-Apr-2012 07:22
GOTCHA: If your first element is false, you don't know whether it was empty or not.

<?php

$a = array();
$b = array(false, true, true);
var_dump(reset($a) === reset($b)); //bool(true)

?>

So don't count on a false return being an empty array.
kendsnyder at gmail dot com 08-Feb-2010 05:12
Don't use `reset()` to get the first value of an associative array. It works great for true arrays but works unexpectedly on Iterator objects. http://bugs.php.net/bug.php?id=38478
m dot lebkowski+php at gmail dot com 03-Aug-2006 10:58
Colin, there`s a better (IMO) way to solve your problem.
<?
  // ...
  foreach($a as $k => &$d){}   // notice the "&"
  // ...
?>
It`s a new feature in PHP5 to use references in foreach loop. This way PHP isn`t making a copy of the array, so the internal pointer won`t be reset.
Colin 06-Jun-2006 05:11
I had a problem with PHP 5.0.5 somehow resetting a sub-array of an array with no apparent reason.  The problem was in doing a foreach() on the parent array PHP was making a copy of the subarrays and in doing so it was resetting the internal pointers of the original array.

The following code demonstrates the resetting of a subarray:

<?
$a = array(
    'a' => array(
        'A', 'B', 'C', 'D',
    ),
    'b' => array(
        'AA', 'BB', 'CC', 'DD',
    ),
);

// Set the pointer of $a to 'b' and the pointer of 'b' to 'CC'
reset($a);
next($a);
next($a['b']);
next($a['b']);
next($a['b']);

var_dump(key($a['b']));
foreach($a as $k => $d)
{
}
var_dump(key($a['b']));
?>

The result of the two var dumps are 3 and 0, respectively.  Clearly the internal pointer of $a['b'] was reset by doing the foreach loop over $a.

Each time the foreach loop iterated over the 'a' and 'b' keys of $a it made a copy of $a['a'] and $a['b'] into $d which resetted the internal pointers of $a['a'] and $a['b'] despite making no obvious changes.

The solution is instead to iterate over the keys of $a.

<?
foreach(array_keys($a) as $k)
{
}
?>

and using $a[$k] (or creating an alias of $a[$k] as $d and dealing with the consequences of using aliases).

For the curious, I was implementing the Iterator interface on a dummy object and calling a global object to do the actual iteration (also to cope with PHP's lack of C-style pointers which when doing a $a = $b on objects would cause the data in $a to be inconsistent with the data in $b when modified).  Being that I had many dummy objects representing different data sets I chose to store each data set as a subarray contained within the global object.  To make this work each dummy object has to store a key (which can freely be duplicated without problems) that it passes to the global object when rewind, key, current, next, and valid were called on the dummy object.

Unfortunately for me, my key required to be more than just a simple string or number (if it was then it could be used to directly index the subarray of data for that object and problem avoided) but was an array of strings.  Instead, I had to iterate over (with a foreach loop) each subarray and compare the key to a variable stored within the subarray.

So by using a foreach loop in this manner and with PHP resetting the pointer of subarrays it ended up causing an infinite loop.

Really, this could be solved by PHP maintaining internal pointers on arrays even after copying.
27-Feb-2006 10:20
I wrote a nice function, which rotates values of array. Very useful for table rows where you have to rotate colors

<?php
function rotate(&$array) {
    $item = current($array);
    if (!next($array)) reset($array);
    return $item;
}
?>
Alexandre Koriakine 18-Nov-2005 11:09
Also it's good to reset this way the multidimentional arrays:

reset($voo2['moder']);
while (list($key, $value) = each ($voo2['moder'])) {

reset($voo2['moder'][$key]);
while (list($key1, $value1) = each ($voo2['moder'][$key])) {
#do what u want
}

}
leaetherstrip at inbox dot NOSPAMru 17-Oct-2004 03:54
Note that reset() will not affect sub-arrays of multidimensional array.

For example,

<?php
    $arr = array(
        1 => array(2,3,4,5,6),
        2 => array(6,7,8,9,10)
    );

    while(list($i,) = each($arr))
    {
        echo "IN \$arr[$i]<br>";

        while(list($sub_i,$entry) = each($arr[$i]))
        {
            echo "\$arr[$i][$sub_i] = $entry<br>";
        }
    }

    reset($arr);

    // Do the same again
    while(list($i,) = each($arr))
    {
        echo "IN \$arr[$i]<br>";

        while(list($sub_i,$entry) = each($arr[$i]))
        {
            echo "\$arr[$i][$sub_i] = $entry<br>";
        }
    }
?>

will print

IN $arr[1]
$arr[1][0] = 2
$arr[1][1] = 3
$arr[1][2] = 4
$arr[1][3] = 5
$arr[1][4] = 6
IN $arr[2]
$arr[2][0] = 6
$arr[2][1] = 7
$arr[2][2] = 8
$arr[2][3] = 9
$arr[2][4] = 10
IN $arr[1]
IN $arr[2]
kk at shonline dot de 30-Jul-1998 02:22
When used on a scalar or unset value, reset() spews warning messages. This is often a problem when accessing arrays generated from HTML form input data: these are scalar or unset if the user didn't enter sufficient information.

You can silence these error messages by prefixing an @ (at sign) to reset(), but it is better style to protect your reset() and the following array traversal with an if (isset()). Example code:

<?php
if (isset($form_array)) {
  reset($form_array);
  while (list($k, $v) = each($form_array) {
    do_something($k, $v);
  }
}
?>


역순으로 배열 정렬

  • ≪ reset
  • shuffle ≫
  • PHP Manual
  • 배열 함수 목록
  • 역순으로 배열 정렬

rsort

(PHP 4, PHP 5, PHP 7)

rsort — 역순으로 배열 정렬

설명

bool rsort ( array &$array [, int $sort_flags ] )

이 함수는 역순으로 배열을 정렬한다 (높은 것에서 낮은 것으로).

인수

array

입력 배열.

sort_flags

선택적인 인수 sort_flags를 사용하여 정렬 방법을 변경할수 있다. 더 자세한 것은 sort() 를 참고.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 rsort() 예제

<?php
$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

위 예제의 출력:

0 = orange
1 = lemon
2 = banana
3 = apple

fruits는 알파벳 역순으로 정렬되었다.

주의

Note: 이 함수는 array에 새로운 키를 할당합니다. 이 작업은 단순히 키를 재배열할 뿐만 아니라, 이미 할당되어 있는 키를 제거할 수 있습니다.

참고

  • arsort() - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • ksort() - 키에 의한 배열 정렬
  • krsort() - 키에 의한 배열 역순 정렬
  • sort() - 배열 정렬
  • usort() - 사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬

User Contributed Notes

suniafkhami at gmail dot com 20-Feb-2014 11:40
If you are sorting an array from a database result set, such as MySQL for example, another approach could be to have your database sort the result set by using ORDER BY DESC, which would be the equivalent of using rsort() on the resulting array in PHP.

[Edited by moderator for clarity: googleguy at php dot net]
Alex M 28-Jun-2005 04:39
A cleaner (I think) way to sort a list of files into reversed order based on their modification date.

<?php
   $path = $_SERVER[DOCUMENT_ROOT]."/files/";
   $dh = @opendir($path);

   while (false !== ($file=readdir($dh)))
   {
      if (substr($file,0,1)!=".")
         $files[]=array(filemtime($path.$file),$file);   #2-D array
   }
   closedir($dh);

   if ($files)
   {
      rsort($files); #sorts by filemtime

      #done! Show the files sorted by modification date
      foreach ($files as $file)
         echo "$file[0] $file[1]<br>\n";  #file[0]=Unix timestamp; file[1]=filename
   }
?>
pshirkey at boosthardware dot com 14-Jan-2005 10:06
I needed a function that would sort a list of files into reversed order based on their modification date.

Here's what I came up with:

function display_content($dir,$ext){

    $f = array();
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($folder = readdir($dh)) !== false) {
                if (preg_match("/\s*$ext$/", $folder)) {
                    $fullpath = "$dir/$folder";
                    $mtime = filemtime ($fullpath);

                    $ff = array($mtime => $fullpath);
                    $f = array_merge($f, $ff);

            }
                }

            rsort($f, SORT_NUMERIC);

            while (list($key, $val) = each($f)) {
                $fcontents = file($val, "r");
                while (list($key, $val) = each($fcontents))
                    echo "$val\n";
            }

        }
    }

        closedir($dh);
}

Call it like so:

display_content("folder","extension");
ray at non-aol dot com 02-Nov-2004 04:49
Like sort(), rsort() assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys.  This means that it will destroy associative keys.

$animals = array("dog"=>"large",  "cat"=>"medium",  "mouse"=>"small");
print_r($animals);
//Array ( [dog] => large [cat] => medium [mouse] => small )

rsort($animals);
print_r($animals);
//Array ( [0] => small [1] => medium [2] => large )

Use KSORT() or KRSORT() to preserve associative keys.
rnk-php at kleckner dot net 17-Jun-2003 09:37
Apparently rsort does not put arrays with one value back to zero.  If you have an array like: $tmp = array(9 => 'asdf') and then rsort it, $tmp[0] is empty and $tmp[9] stays as is.
slevy1 at pipeline dot com 13-Jun-2001 08:15
I thought rsort was working successfully or on a multi-dimensional array of strings that had first been sorted with usort(). But, I noticed today that the array  was only partially in descending order.  I tried array_reverse on it and that seems to have solved things.


배열을 섞습니다

  • ≪ rsort
  • sizeof ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열을 섞습니다

shuffle

(PHP 4, PHP 5, PHP 7)

shuffle — 배열을 섞습니다

설명

bool shuffle ( array &$array )

이 함수는 배열을 (배열 안의 원소를 임의의 순서로) 섞습니다.

인수

array

배열.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 shuffle() 예제

<?php
$numbers = range(1, 20);
srand((float)microtime() * 1000000);
shuffle($numbers);
foreach ($numbers as $number) {
    echo "$number ";
}
?>

주의

Note: 이 함수는 array에 새로운 키를 할당합니다. 이 작업은 단순히 키를 재배열할 뿐만 아니라, 이미 할당되어 있는 키를 제거할 수 있습니다.

Note: PHP 4.2.0부터 srand()나 mt_srand()를 이용한 난수값 생성기 초기화를 할 필요가 없습니다. 자동적으로 이루어집니다.

참고

  • arsort() - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • ksort() - 키에 의한 배열 정렬
  • rsort() - 역순으로 배열 정렬
  • sort() - 배열 정렬
  • usort() - 사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬

User Contributed Notes

xzero at elite7hackers dot net 30-May-2015 10:09
There is an function which uses native shuffle() but preserves keys, and their order, so at end, only values are shuffled.

<?PHP
/**
 * Array Quake - Give an array good quake so every value will endup with random given space.
 * Keys, and their order are preserved.
 * @author xZero <xzero@elite7hackers.net>
 * @param array $array
 * @return boolean false on failure
 */
function array_quake(&$array) {
    if (is_array($array)) {
        $keys = array_keys($array); // We need this to preserve keys
        $temp = $array;
        $array = NULL;
        shuffle($temp); // Array shuffle
        foreach ($temp as $k => $item) {
            $array[$keys[$k]] = $item;
        }
        return;
    }
    return false;
}

// Example
$numbers = array(
    'ZERO' => 0,
    'ONE' => 1,
    'TWO' => 2,
    'THREE' => 3,
    'FOUR' => 4,
    'FIVE' => 5,
    'SIX' => 6,
    'SEVEN' => 7,
    'EIGHT' => 8,
    'NINE' => 9
);
echo "\nBefore (original):\n";
print_r($numbers);
array_quake($numbers);
echo "\n\nAfter (Array Quake);\n";
print_r($numbers);
echo "\n";
?>

Result example:
Before (original):
Array
(
    [ZERO] => 0
    [ONE] => 1
    [TWO] => 2
    [THREE] => 3
    [FOUR] => 4
    [FIVE] => 5
    [SIX] => 6
    [SEVEN] => 7
    [EIGHT] => 8
    [NINE] => 9
)

After (Array Quake);
Array
(
    [ZERO] => 4
    [ONE] => 2
    [TWO] => 0
    [THREE] => 8
    [FOUR] => 3
    [FIVE] => 6
    [SIX] => 1
    [SEVEN] => 7
    [EIGHT] => 5
    [NINE] => 9
)
peter at removethisplease dot ddcrew dot com 02-Dec-2013 10:55
This seems to do reasonably well as a shuffle() that preserves index assocation:

<?php
function ashuffle (&$arr) {
    uasort($arr, function ($a, $b) {
        return rand(-1, 1);
    });
}
?>

Obviously only works if PHP has closures enabled...
timtrinidad at gmail dot com 08-Aug-2011 09:38
It appears that the suhosin extensions >= 0.9.26 that have the suhosin.mt_srand.ignore and suhosin.srand.ignore settings set to "Off" do not affect shuffle().

In other words, the only way I was able to get pseudo-random and predictable results through shuffle() was to disable the extension.
Eric Anderson 29-Mar-2011 06:33
Copy and paste this script and refresh the page to see the shuffling effect.

<?php
    /**
    * Shuffles and displays cards in a deck
    * @author:     Eric Anderson
    * @filename:   deckofcards.php
    */

    // Create an array of face values
    // and an array of card values
    // then merge them together
    $cards = array_merge(array("J", "Q", "K", "A"), range(2,10)); // 13 cards

    // Shuffle the cards
    shuffle($cards);

    // Create an multidimentional array to hold the 4 suits
    $suits = array(
        'Heart' => array(),
        'Spade' => array(),
        'Diamond' => array(),
        'Club' => array()
        );

    // Add cards to their respective suits
    for($i = 0; $i < count($suits); $i++)
    {
        for($j = 0; $j < count($cards); $j++)
        {
            $suits['Heart'][$j] = $cards[$j]."<span style=color:#FF0000;>&hearts;</span>";
            $suits['Spade'][$j] = $cards[$j]."&spades;";
            $suits['Diamond'][$j] = $cards[$j]."<span style=color:#FF0000;>&diams;</span>";
            $suits['Club'][$j] = $cards[$j]."&clubs;";
        }
    }

    // Create a deck
    $deck = array();

    // Merge the suits into the empty deck array
    $deck = array_merge($deck, $suits);

    // Display the deck to the screen
    echo "<p><b>Deck of cards:</b></p>";
    foreach($deck as $k1 => $v1)
    {
        // Display suit name
        echo "<p>&emsp;$k1's<br />&emsp;{<br />&emsp;&emsp;";
        $acc = 0;

        // Display card value
        foreach($v1 as $k2 => $v2)
        {
            echo "$v2&nbsp";
            $acc++;

            if ($acc == 4)
            {
                echo "<br />&emsp;&emsp;";
                $acc = 0;
            }
        }
        echo "<br />&emsp;}</p>";
    }
?>
andjones at gmail dot com 27-Aug-2010 06:35
Shuffle associative and non-associative array while preserving key, value pairs. Also returns the shuffled array instead of shuffling it in place.

<?php

function shuffle_assoc($list) {
  if (!is_array($list)) return $list;

  $keys = array_keys($list);
  shuffle($keys);
  $random = array();
  foreach ($keys as $key)
    $random[$key] = $list[$key];

  return $random;
}

?>
ezakto at ezakto dot com 25-Jul-2010 08:13
This is a replica of shuffle() but preserving keys (associative and non-associative)

bool kshuffle  (  array &$array  )

<?php

function kshuffle(&$array) {
    if(!is_array($array) || empty($array)) {
        return false;
    }
    $tmp = array();
    foreach($array as $key => $value) {
        $tmp[] = array('k' => $key, 'v' => $value);
    }
    shuffle($tmp);
    $array = array();
    foreach($tmp as $entry) {
        $array[$entry['k']] = $entry['v'];
    }
    return true;
}

$array = array('first' => 0, 'second' => 1, 'third' => 2);
kshuffle($array);
print_r($array); // [second] => 1 [first] => 0 [third] => 2

$array = array('first', 'second', 'third');
kshuffle($array);
print_r($array); // [1] => second [2] => third [0] => first

?>
m0gr14 at gmail dot com 04-Jul-2010 07:39
This function shuffles an associative array recursive.

<?php

/**
* Shuffles an associative array recursive
* @author Mogria <m0gr14@gmail.com>
* @param array $array
* @return array
*
*/

function rec_assoc_shuffle($array)
{
  $ary_keys = array_keys($array);
  $ary_values = array_values($array);
  shuffle($ary_values);
  foreach($ary_keys as $key => $value) {
    if (is_array($ary_values[$key]) AND $ary_values[$key] != NULL) {
      $ary_values[$key] = rec_assoc_shuffle($ary_values[$key]);
    }
    $new[$value] = $ary_values[$key];
  }
  return $new;
}

?>

Example of use:
<?php
$array =
    Array(
        1,
        Array(
            1,
            2,
            3,
            4,
            Array(
                1,
                2,
                3,
                4,
                5
            )
        ),
        2,
        3,
        Array(
            0,
            1,
                Array(
                    1,
                    2,
                    3,
                    4,
                        Array(
                            1,
                            6,
                            5,
                            2,
                            1,
                            3
                        )
                )
        ),
        4,
        5,
        6,
        7
    );
$array = rec_assoc_shuffle($array);
echo '<pre>';
print_r($array);
echo '</pre>';

/*
Returns:
Array
(
    [0] => 6
    [1] => 5
    [2] => Array
        (
            [0] => 1
            [1] => 3
            [2] => Array
                (
                    [0] => 3
                    [1] => 1
                    [2] => 2
                    [3] => 4
                    [4] => 5
                )

            [3] => 2
            [4] => 4
        )

    [3] => 3
    [4] => Array
        (
            [0] => Array
                (
                    [0] => 3
                    [1] => 1
                    [2] => 4
                    [3] => Array
                        (
                            [0] => 5
                            [1] => 1
                            [2] => 3
                            [3] => 6
                            [4] => 1
                            [5] => 2
                        )

                    [4] => 2
                )

            [1] => 1
            [2] => 0
        )

    [5] => 2
    [6] => 1
    [7] => 4
    [8] => 7
)

*/

?>
rick at suggestive dot com 24-Mar-2010 11:19
Many people in SEO need to supply an array and shuffle the results and need the same result each time that page is generated. This is my implementation with a working example:

<?php
function seoShuffle(&$items,$string) {
    mt_srand(strlen($string));
    for ($i = count($items) - 1; $i > 0; $i--){
        $j = @mt_rand(0, $i);
        $tmp = $items[$i];
        $items[$i] = $items[$j];
        $items[$j] = $tmp;
    }
}

$items = array('one','two','three','four','five','six');
$string = 'whatever';

echo '<pre>';
print_r($items);
echo '</pre>';

seoShuffle($items,$string);

echo '<pre>';
print_r($items);
echo '</pre>';
?>
tyler at CompLangs dot com 02-Jan-2010 03:59
Here is a quick function I wrote that generates a random password and uses shuffle() to easily shuffle the order.

<?php
public function randPass($upper = 3, $lower = 3, $numeric = 3, $other = 2) {
    //we need these vars to create a password string
    $passOrder = Array();
    $passWord = '';

    //generate the contents of the password
    for ($i = 0; $i < $upper; $i++) {
        $passOrder[] = chr(rand(65, 90));
    }
    for ($i = 0; $i < $lower; $i++) {
        $passOrder[] = chr(rand(97, 122));
    }
    for ($i = 0; $i < $numeric; $i++) {
        $passOrder[] = chr(rand(48, 57));
    }
    for ($i = 0; $i < $other; $i++) {
        $passOrder[] = chr(rand(33, 47));
    }

    //randomize the order of characters
    shuffle($passOrder);

    //concatenate into a string
    foreach ($passOrder as $char) {
        $passWord .= $char;
    }

    //we're done
    return $passWord;
}
?>
ahmad at ahmadnassri dot com 19-Nov-2009 05:58
shuffle for associative arrays, preserves key=>value pairs.
(Based on (Vladimir Kornea of typetango.com)'s function)

<?php
    function shuffle_assoc(&$array) {
        $keys = array_keys($array);

        shuffle($keys);

        foreach($keys as $key) {
            $new[$key] = $array[$key];
        }

        $array = $new;

        return true;
    }
?>

*note: as of PHP 5.2.10, array_rand's resulting array of keys is no longer shuffled, so we use array_keys + shuffle.
Anonymous 28-Aug-2009 06:18
I needed a simple function two shuffle a two dimensional array. Please note the second level arrays must be indexed using integers, for example $myarray[0]["Name"] and not $myarray["One"]["Name"]. Here is the function:

<?php
function twodshuffle($array)
{
    // Get array length
    $count = count($array);
    // Create a range of indicies
    $indi = range(0,$count-1);
    // Randomize indicies array
    shuffle($indi);
    // Initialize new array
    $newarray = array($count);
    // Holds current index
    $i = 0;
    // Shuffle multidimensional array
    foreach ($indi as $index)
    {
        $newarray[$i] = $array[$index];
        $i++;
    }
    return $newarray;
}
?>

Please note it only works on two dimensional arrays. Here is an example:

<?php
$myarray = array("Google" => array("Name" => "Google", "URL" => "www.google.com", "Usage" => "Googling"), "Yahoo" => array("Name" => "Yahoo", "URL" => "www.yahoo.com", "Usage" => "Yahooing?"), "Ask" => array("Name" => "Ask", "URL" => "www.ask.com", "Usage" => "Asking Jeeves"));

print_r(twodshuffle($myarray));

/* And the result is:
Array ( [0] => Array ( [Name] => Ask [URL] => www.ask.com [Usage] => Asking Jeeves ) [1] => Array ( [Name] => Google [URL] => www.google.com [Usage] => Googling ) [2] => Array ( [Name] => Yahoo [URL] => www.yahoo.com [Usage] => Yahooing? ) )
*/
?>

Hope you find it useful!
aalaap at gmail dot com 08-Jun-2009 02:12
I've been wondering why shuffle() doesn't provide the shuffled array as a return value instead of a bool. I mean, what could possibly go wrong in shuffling elements from an array?

So I use something like this:

<?php
function array_shuffle($array) {
    if (shuffle($array)) {
        return $array;
    } else {
        return FALSE;
    }
}
?>
sivaji2009 at gmail dot com 17-May-2009 11:07
Here i wrote a custom shuffle function which preserves the array index and distributes the array element randomly.

<?php

/*
 * return an array whose elements are shuffled in random order.
 */
function custom_shuffle($my_array = array()) {
  $copy = array();
  while (count($my_array)) {
    // takes a rand array elements by its key
    $element = array_rand($my_array);
    // assign the array and its value to an another array
    $copy[$element] = $my_array[$element];
    //delete the element from source array
    unset($my_array[$element]);
  }
  return $copy;
}

$array = array(
  'a' => 'apple',
  'b' => 'ball',
  'c' => 'cat',
  'd' => 'dog',
  'e' => 'egg',
  'f' => 'fan',
  'g' => 'gun'
);

print_r(custom_shuffle($array));

Array
(
    [c] => cat
    [e] => egg
    [f] => fan
    [a] => apple
    [b] => ball
    [g] => gun
    [d] => dog
)

?>
dirk dot avery a t gmail 30-Apr-2009 05:58
Building on examples by m227 and pineappleclock, here is a function that returns all permutations of each set in the power set of an array of strings (instead of a string).  Thanks for the great examples!

<?php

/*
 Use: $arr = power_perms($in);

 Example:
  $in = array("A","B","C");
  $power_perms = power_perms($in);

 Returns:
  Array
  (
    [0] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )

    [1] => Array
        (
            [0] => A
            [1] => C
            [2] => B
        )

    [2] => Array
        (
            [0] => B
            [1] => A
            [2] => C
        )

    [3] => Array
        (
            [0] => B
            [1] => C
            [2] => A
        )

    [4] => Array
        (
            [0] => C
            [1] => A
            [2] => B
        )

    [5] => Array
        (
            [0] => C
            [1] => B
            [2] => A
        )

    [6] => Array
        (
            [0] => A
            [1] => B
        )

    [7] => Array
        (
            [0] => B
            [1] => A
        )

    [8] => Array
        (
            [0] => B
            [1] => C
        )

    [9] => Array
        (
            [0] => C
            [1] => B
        )

    [10] => Array
        (
            [0] => A
            [1] => C
        )

    [11] => Array
        (
            [0] => C
            [1] => A
        )

    [12] => Array
        (
            [0] => A
        )

    [13] => Array
        (
            [0] => B
        )

    [14] => Array
        (
            [0] => C
        )

  )

*/

function power_perms($arr) {

    $power_set = power_set($arr);
    $result = array();
    foreach($power_set as $set) {
        $perms = perms($set);
        $result = array_merge($result,$perms);
    }
    return $result;
}

function power_set($in,$minLength = 1) {

   $count = count($in);
   $members = pow(2,$count);
   $return = array();
   for ($i = 0; $i < $members; $i++) {
      $b = sprintf("%0".$count."b",$i);
      $out = array();
      for ($j = 0; $j < $count; $j++) {
         if ($b{$j} == '1') $out[] = $in[$j];
      }
      if (count($out) >= $minLength) {
         $return[] = $out;
      }
   }

   //usort($return,"cmp");  //can sort here by length
   return $return;
}

function factorial($int){
   if($int < 2) {
       return 1;
   }

   for($f = 2; $int-1 > 1; $f *= $int--);

   return $f;
}

function perm($arr, $nth = null) {

    if ($nth === null) {
        return perms($arr);
    }

    $result = array();
    $length = count($arr);

    while ($length--) {
        $f = factorial($length);
        $p = floor($nth / $f);
        $result[] = $arr[$p];
        array_delete_by_key($arr, $p);
        $nth -= $p * $f;
    }

    $result = array_merge($result,$arr);
    return $result;
}

function perms($arr) {
    $p = array();
    for ($i=0; $i < factorial(count($arr)); $i++) {
        $p[] = perm($arr, $i);
    }
    return $p;
}

function array_delete_by_key(&$array, $delete_key, $use_old_keys = FALSE) {

    unset($array[$delete_key]);

    if(!$use_old_keys) {
        $array = array_values($array);
    }

    return TRUE;
}
?>
pineappleclock at gmail dot com 22-Jan-2009 07:06
If you want the Power Set (set of all unique subsets) of an array instead of permutations, you can use this simple algorithm:

<?php
/**
* Returns the power set of a one dimensional array,
* a 2-D array.
* array(a,b,c) ->
* array(array(a),array(b),array(c),array(a,b),array(b,c),array(a,b,c))
*/
function powerSet($in,$minLength = 1) {
   $count = count($in);
   $members = pow(2,$count);
   $return = array();
   for ($i = 0; $i < $members; $i++) {
      $b = sprintf("%0".$count."b",$i);
      $out = array();
      for ($j = 0; $j < $count; $j++) {
         if ($b{$j} == '1') $out[] = $in[$j];
      }
      if (count($out) >= $minLength) {
         $return[] = $out;
      }
   }
   return $return;
}
?>
tony at brokerbin dot com 12-Nov-2008 04:26
Here is IMO the simplest and extremely fast way to shuffle an associative array AND keep the key=>value relationship.  However, it ONLY works if there are NO NUMERIC keys AT ALL.  Look into array_merge for the reason why.

<?php

$unshuffled = array('one'=>1,'two'=>2,'three'=>3);

$shuffled = array_merge( array_flip(array_rand($unshuffled,count($unshuffled))),$unshuffled );

?>

peace
Antonio Ognio 06-May-2008 06:42
Another shuffle() implementation that preserves keys, does not use extra memory and perhaps is a bit easier to grasp.

<?php
if (function_exists('shuffle_with_keys')===false) {
  function shuffle_with_keys(&$array) {
    /* Auxiliary array to hold the new order */
    $aux = array();
    /* We work with an array of the keys */
    $keys = array_keys($array);
    /* We shuffle the keys */
    shuffle($keys);
    /* We iterate thru' the new order of the keys */
    foreach($keys as $key) {
      /* We insert the key, value pair in its new order */
      $aux[$key] = $array[$key];
      /* We remove the element from the old array to save memory */
      unset($array[$key]);
    }
    /* The auxiliary array with the new order overwrites the old variable */
    $array = $aux;
  }
}
?>
07-Jun-2006 11:53
Posted below is code that you would expect to work

<?php

$keys = shuffle(array_keys($arr));

foreach ($keys as $key) {

  $arr_elem = $arr[$key];
  // do what you want with the array element

}

?>

This in fact does not work because shuffle returns a boolean true or false. More accurate code using this method would be:

<?php

$keys = array_keys($arr);
shuffle($keys);

foreach ($keys as $key) {

  $arr_elem = $arr[$key];
  // do what you want with the array element

}

?>
bill at SWScripts dot com 22-Sep-2005 06:45
Just a quick note to let people know that shuffle() will work on multidimensional associative arrays provided that the first key is already numeric.

So this array can be shuffled without losing any of the secondary keys or their values:

$array[0]['color'], $array[0]['size'], $array[0]['fabric']
. . .
$array[50]['color'], $array[50]['size'], $array[50]['fabric']

I recently ran up against needing to randomize this array and tried shuffle even though it's not really for associative arrays.

Hope it helps somebody out there.


별칭: count

  • ≪ shuffle
  • sort ≫
  • PHP Manual
  • 배열 함수 목록
  • 별칭: count

sizeof

(PHP 4, PHP 5, PHP 7)

sizeof — 별칭: count()

설명

이 함수는 다음 함수의 별칭입니다: count().

User Contributed Notes

chris at amis dot nl 03-Sep-2013 06:15
I would recommend not using sizeof(). Many programmers expect sizeof() to return the amount of memory allocated. Instead sizeof() -as described above- is an alias for count().

Prevent misinterpretation and use count() instead.
umopdn [at] msn [dawt] com 23-Sep-2008 11:38
a) Always try and use PHP's internal routines to iterate through objects of various types (arrays in most examples below).

Instead of interpreting your code to loop through them, they use their own internal routines which are much faster.

(This is why foreach () will run faster than manual interation)

b) It is _always_ good practice to leave as many static resulting functions outside of loops, having operations that return the exact same piece of data every iteration of the loop is not pretty on resources.

c) I agree with PixEye's remarks on sizeof().  In PHP it is just an alias for the true function count().  It has other meanings logically in other languages rather than the number of elements in an object.  This should be avoided as it may confuse developers transitioning to PHP from other languages.
PixEye 23-Sep-2008 06:09
I am quite surprised about previous posts. Here are my advices:

1/ prefer the count() function instead of sizeOf() as sizeOf() is only an alias of count() and does not mean the same in many other languages based on C (avoid ambiguity).

2/ prefer the powerful forEach() function to iterate over arrays.
communiti.ch 10-Apr-2008 03:55
If your array is "huge"

It is reccomended to set a variable first for this case:

THIS->

$max = sizeof($huge_array);
for($i = 0; $i < $max;$i++)
{
code...
}

IS QUICKER THEN->

for($i = 0; $i < sizeof($huge_array);$i++)
{
code...
}
svanegmond at tinyplanet dot ca 13-Mar-2008 08:12
For people who use this kind of idiom:

for ($i=0; $i<sizeof($array); $i++) {
   ...
}

At least in PHP4, you will notice a large performance improvement if you switch to:

$max = sizeof($array);
for ($i=0; $i<$max; $i++) {
   ...
}

I had an array with a few thousand items in it, each of them a map with 5 key-value pairs, and just for'ing through the array took a second of CPU on a decent machine.


배열 정렬

  • ≪ sizeof
  • uasort ≫
  • PHP Manual
  • 배열 함수 목록
  • 배열 정렬

sort

(PHP 4, PHP 5, PHP 7)

sort — 배열 정렬

설명

bool sort ( array &$array [, int $sort_flags ] )

이 함수는 배열을 정렬한다. 이 함수가 수행되면 원소들은 작은 값에서 큰 값으로 정렬될것이다.

인수

array

입력 배열.

sort_flags

선택적인 두번째 sort_flags 인수는 아래 값을 사용하며, 정렬 방법을 변경하는데 사용된다:

정렬 형식 플래그:

  • SORT_REGULAR - 통상적인 방법으로 원소를 비교 (자료형을 바꾸지 않음)
  • SORT_NUMERIC - 수치적으로 원소를 비교
  • SORT_STRING - 문자열로 원소를 비교
  • SORT_LOCALE_STRING - 현재 로케일에 기반하여 문자열로 원소를 비교. PHP 4.4.0과 5.0.2에서 추가. PHP 6 전에는, setlocale()로 변경할 수 있는 시스템 로케일을 사용합니다. PHP 6부터는, i18n_loc_set_default()를 사용해야 합니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
4.0.0 | sort_flags 인수 추가.

예제

Example #1 sort() 예제

<?php

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

?>

위 예제의 출력:

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

fruits는 알파벳 순으로 정렬되었다.

주의

Note: 이 함수는 array에 새로운 키를 할당합니다. 이 작업은 단순히 키를 재배열할 뿐만 아니라, 이미 할당되어 있는 키를 제거할 수 있습니다.

Note: 대부분의 PHP 정렬 함수와 마찬가지로, sort()는 » 퀵소트를 사용합니다.

Warning

여러 자료형 값을 가지고 있는 배열을 정렬할 때는 조심하십시오. sort()가 예측할 수 없는 결과를 생성할 수 있습니다.

참고

  • arsort() - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • ksort() - 키에 의한 배열 정렬
  • rsort() - 역순으로 배열 정렬
  • usort() - 사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬
  • uksort() - 사용자 정의 비교 함수를 사용하여 키에 의한 배열 정렬
  • array_multisort() - 여러 배열이나 다차원 배열 정렬
  • krsort() - 키에 의한 배열 역순 정렬
  • natsort() - "자연순" 알고리즘으로 배열 정렬
  • natcasesort() - "자연순" 알고리즘으로 대소문자를 구분하지 않고 배열 정렬

User Contributed Notes

Abhishek Banerjee 08-Jun-2016 04:39
EDIT: To the original note by "phpdotnet at m4tt dot co dot uk"
Use array_push instead of $new_array[$k] for some reason it was
giving me string indexes.

Simple function to sort an array by a specific key. Maintains index association.

<?php

function array_sort($array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            array_push($new_array, $array[$k]);
        }
    }

    return $new_array;
}

$people = array(
    12345 => array(
        'id' => 12345,
        'first_name' => 'Joe',
        'surname' => 'Bloggs',
        'age' => 23,
        'sex' => 'm'
    ),
    12346 => array(
        'id' => 12346,
        'first_name' => 'Adam',
        'surname' => 'Smith',
        'age' => 18,
        'sex' => 'm'
    ),
    12347 => array(
        'id' => 12347,
        'first_name' => 'Amy',
        'surname' => 'Jones',
        'age' => 21,
        'sex' => 'f'
    )
);

print_r(array_sort($people, 'age', SORT_DESC)); // Sort by oldest first
print_r(array_sort($people, 'surname', SORT_ASC)); // Sort by surname

/*
Array
(
    [12345] => Array
        (
            [id] => 12345
            [first_name] => Joe
            [surname] => Bloggs
            [age] => 23
            [sex] => m
        )

    [12347] => Array
        (
            [id] => 12347
            [first_name] => Amy
            [surname] => Jones
            [age] => 21
            [sex] => f
        )

    [12346] => Array
        (
            [id] => 12346
            [first_name] => Adam
            [surname] => Smith
            [age] => 18
            [sex] => m
        )

)
Array
(
    [12345] => Array
        (
            [id] => 12345
            [first_name] => Joe
            [surname] => Bloggs
            [age] => 23
            [sex] => m
        )

    [12347] => Array
        (
            [id] => 12347
            [first_name] => Amy
            [surname] => Jones
            [age] => 21
            [sex] => f
        )

    [12346] => Array
        (
            [id] => 12346
            [first_name] => Adam
            [surname] => Smith
            [age] => 18
            [sex] => m
        )

)
*/

?>
totszwai at gail dot com 29-Jan-2016 12:59
For those who wanna go with simple approach without hammering your head too much... This does the job, granted might not be efficient, but is really short. :P

Let $all be a key/value paired array...

$keys = array_keys($all);
sort($keys);
$sorted = array();
foreach($keys as $k) {
       $sorted[$k] = $all[$k];
}
$all = $sorted;
unset($sorted);
aditycse at gmail dot com 07-Aug-2015 07:34
/*
 * Name : Aditya Mehrotra
 * Email: aditycse@gmail.com
 */
//Example for sorting by values for an alphanumeric array also having case-sensitive data
$exampleArray1 = $exampleArray2 = array(
    0 => 'example1',
    1 => 'Example10',
    2 => 'example12',
    3 => 'Example2',
    4 => 'example3',
    5 => 'EXAMPLE10',
    6 => 'example10'
);

//default sorting
asort($exampleArray1);

// alphanumeric with case-sensitive data sorting by values
asort($exampleArray2, SORT_STRING | SORT_FLAG_CASE | SORT_NATURAL);

//output of defaut sorting
print_r($exampleArray1);
/*
 * output of default sorting
  Array
  (
  [5] => EXAMPLE10
  [1] => Example10
  [3] => Example2
  [0] => example1
  [6] => example10
  [2] => example12
  [4] => example3
  )
 */

print_r($exampleArray2);
/*
 * output of alphanumeric with case-sensitive data sorting by values
 Array
(
    [0] => example1
    [3] => Example2
    [4] => example3
    [5] => EXAMPLE10
    [1] => Example10
    [6] => example10
    [2] => example12
)
 */
williamprogphp at[pleaseNOTSPAM] yahoo d 20-Dec-2013 09:15
In order to make some multidimensional quick sort implementation, take advantage of this stuff

<?php
        function quickSortMultiDimensional($array, $chave) {
            if( count( $array ) < 2 ) {
                return $array;
            }
            $left = $right = array( );
            reset( $array );
            $pivot_key    = key( $array );
            $pivot    = array_shift( $array );
            foreach( $array as $k => $v ) {
                if( $v[$chave] < $pivot[$chave] )
                        $left[$k][$chave] = $v[$chave];
                else
                        $right[$k][$chave] = $v[$chave];
            }
            return array_merge(
                                    quickSortMultiDimensional($left, $chave),
                                    array($pivot_key => $pivot),
                                    quickSortMultiDimensional($right, $chave)
            );
        }
?>

I make it using the idea from pageconfig dot com

tks for viewing
me[ at ]szczepan[ dot ]info 27-Dec-2012 09:36
Sorting the keys, but keep the values in order is not possible by just ordering, because it would result in a new array. This is also the solution: Create a new array

<?php
$a = array(9=>"a",8=>"c",5=>"d");

$keys = array_keys($a);
sort($keys);
$result = array_combine($keys, array_values($a));

//Result : array(5=>"a",8=>"c",9=>"d");
?>
Walter Tross 16-Dec-2011 11:38
unless you specify the second argument, "regular" comparisons will be used. I quote from the page on comparison operators:
"If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically."
What this means is that "10" < "1a", and "1a" < "2", but "10" > "2". In other words, regular PHP string comparisons are not transitive.
This implies that the output of sort() can in rare cases depend on the order of the input array:
<?php
function echo_sorted($a)
{
   echo "{$a[0]} {$a[1]} {$a[2]}";
   sort($a);
   echo " => {$a[0]} {$a[1]} {$a[2]}\n";
}
// on PHP 5.2.6:
echo_sorted(array( "10", "1a", "2")); // => 10 1a 2
echo_sorted(array( "10", "2", "1a")); // => 1a 2 10
echo_sorted(array( "1a", "10", "2")); // => 2 10 1a
echo_sorted(array( "1a", "2", "10")); // => 1a 2 10
echo_sorted(array( "2", "10", "1a")); // => 2 10 1a
echo_sorted(array( "2", "1a", "10")); // => 10 1a 2
?>
ajanata at gmail dot com 21-Oct-2011 06:13
This took me longer than it should have to figure out, but if you want the behavior of sort($array, SORT_STRING) (that is, re-indexing the array unlike natcasesort) in a case-insensitive manner, it is a simple matter of doing usort($array, strcasecmp).
holdoffhunger at gmail dot com 18-Jul-2011 06:14
I have discovered an interesting trick with the sort function.  Many coders are using it to sort options for a user, because a computer will only need sorted data in limited cases.  However, every coder wants their sorted data to be usable.  It's very common to find any sorted data online, and to find options, "Sort by Date," "Sort Alphabetically," etc., etc., whether you're looking at an online catalogue or some bulletin board.

The trick is to use a combination of html commenting and str_pad's to do definitive "sort by" functions.  Let me demonstrate with an example: consider two arrays, Sorted_By_Date and Sorted_By_Name.  You want every element to be "<option value=something>Option Name</option", so that you can do a simple foreach statement that prints every option value.  But, sorting these arrays will technically sort just sort whatever you put into "value".  So, here's a neat trick.  Make every entry "<!-- Sort_Value --><option value=something>Option Name</option."  That way, when you sort, it'll have to sort technically by whatever is the Sort_Value.

The greatness of this trick is that you can make as many "sort this data by X" fields as you want!  Just create multiple arrays, each with the same option values and names, but with a different value put in "Sort_Value", which could be a date, a name, a price, etc..  With numbers, you may need to use the str_pad function, to push all of the zeros out to the left, for a proper sort.  For instance, in my code, that looks like...

<?php

                $number_of_versions_sort_value = str_pad($number_of_versions, 20, "0", STR_PAD_LEFT);

?>
alex dot hristov dot 88 at gmail dot com 16-Jun-2011 04:26
As some people have mentioned before sorting a multidimentional array can be a bit tricky. it took me quite a while to get it going but it works as a charm:

<?php
//$order has to be either asc or desc
 function sortmulti ($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) {
        if(is_array($array) && count($array)>0) {
            foreach(array_keys($array) as $key)
            $temp[$key]=$array[$key][$index];
            if(!$natsort) {
                if ($order=='asc')
                    asort($temp);
                else
                    arsort($temp);
            }
            else
            {
                if ($case_sensitive===true)
                    natsort($temp);
                else
                    natcasesort($temp);
            if($order!='asc')
                $temp=array_reverse($temp,TRUE);
            }
            foreach(array_keys($temp) as $key)
                if (is_numeric($key))
                    $sorted[]=$array[$key];
                else
                    $sorted[$key]=$array[$key];
            return $sorted;
        }
    return $sorted;
}
?>
markrose at markrose dot ca 01-Jun-2011 08:54
I've yet to come across a fast way to merge sorted arrays by a user value. Merging the arrays and using PHP's usort isn't efficient, as it doesn't take advantage of the existing sorting. So I wrote my own merge sorted arrays by user value function. The limit parameter can be used be used to limit the number of results (there is no point sorting more results than needed). This sort is stable: values will be taken preferentially from the first, then second, etc., array inside $merge_arrays.

Note that this function will fail if a user value is boolean false. It could be adapted to hold the best value inside an array at some performance penalty. It could also be adapted to comparing strings case-insensitively.

To give you some idea of the performance, my machine will sort out the first 1000 values of 10 sorted arrays of 1000 values each in under 40 ms, using an integer sort field. Not fast by C standards, but a vast improvement over usort'ing 10,000 values.

<?php
function merge_sorted_arrays_by_field ($merge_arrays, $sort_field, $sort_desc = false, $limit = 0)
{
    $array_count = count($merge_arrays);

    // fast special cases...
    switch ($array_count)
    {
        case 0: return array();
        case 1: return $limit ? array_slice(reset($merge_arrays), 0, $limit) : reset($merge_arrays);
    }

    if ($limit === 0)
        $limit = PHP_INT_MAX;

    // rekey merge_arrays array 0->N
    $merge_arrays = array_values($merge_arrays);

    $best_array = false;
    $best_value = false;

    $results = array();

    // move sort order logic outside the inner loop to speed things up
    if ($sort_desc)
    {
        for ($i = 0; $i < $limit; ++$i)
        {
            for ($j = 0; $j < $array_count; ++$j)
            {
                // if the array $merge_arrays[$j] is empty, skip to next
                if (false === ($current_value = current($merge_arrays[$j])))
                    continue;

                // if we don't have a value for this round, or if the current value is bigger...
                if ($best_value === false || $current_value[$sort_field] > $best_value[$sort_field])
                {
                    $best_array = $j;
                    $best_value = $current_value;
                }
            }

            // all arrays empty?
            if ($best_value === false)
                break;

            $results[] = $best_value;
            $best_value = false;
            next($merge_arrays[$best_array]);
        }
    }
    else
    {
        for ($i = 0; $i < $limit; ++$i)
        {
            for ($j = 0; $j < $array_count; ++$j)
            {
                if (false === ($current_value = current($merge_arrays[$j])))
                    continue;

                // if we don't have a value for this round, or if the current value is smaller...
                if ($best_value === false || $current_value[$sort_field] < $best_value[$sort_field])
                {
                    $best_array = $j;
                    $best_value = $current_value;
                }
            }

            // all arrays empty?
            if ($best_value === false)
                break;

            $results[] = $best_value;
            $best_value = false;
            next($merge_arrays[$best_array]);
        }
    }

    return $results;
}
?>
cmarshall at gmx dot de 17-May-2011 12:10
I read up on various problems re: sort() and German Umlaut chars and my head was soon spinning - bug in sort() or not, solution via locale or not, etc. ... (a total newbie here).

The obvious solution for me was quick and dirty: transform the Umlaut chars (present as HTML codes in my case) to their normal equivalent ('a' = 'ae', 'o' = 'oe', 'u' = 'ue', 'ß' = 'ss' etc.), sort the array, then transform back. However there are cases in which a 'Mueller' is really that and does NOT need to be transformed into 'Muller' afterwards. Hence I for example replace the Umlaut itself with it's normal equivalent plus a char not used in the string otherwise (e.g. '_') so that the transfer back to Umlaut would only take place on certain combinations.

Of course any other char instead of '_' can be used as additional char (influencing the sort result). I know that my solution is rough at the edges and may cause other sort problems but it was sufficient for my purpose.

The array '$dat' in this example was filled with German town names (I actually worked with a multiple array ('$dat[][]') but stripped the code down to this as it's easier to understand):

<?php
// START Pre-sorting (Umlaut -> normal letters)
$max = count($dat);
for($totcnt = 0; $totcnt < $max; $totcnt++){
   $dat[$totcnt]=str_replace('&szlig;','ss_',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('&Auml;','Ae_',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('&auml;','ae_',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('&Ouml;','Oe_',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('&ouml;','oe_',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('&Uuml;','Ue_',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('&uuml;','ue_',$dat[$totcnt]);
}
// END Pre-sorting (Umlaut -> normal letters)

// START Sorting //
function compare_towns($a, $b)
{
return strnatcmp($a, $b);
}
usort($dat, 'compare_towns');
// END Sorting //

// START Post-sorting (normal letters -> Umlaut)
for($totcnt = 0; $totcnt < $max; $totcnt++){
   $dat[$totcnt]=str_replace('ss_','&szlig;',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('Ae_','&Auml;',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('ae_','&auml;',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('Oe_','&Ouml;',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('oe_','&ouml;',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('Ue_','&Uuml;',$dat[$totcnt]);
   $dat[$totcnt]=str_replace('ue_','&uuml;',$dat[$totcnt]);
}
// END Post-sorting (normal letters -> Umlaut)
?>
anaz114119 at gmail dot com 24-Dec-2010 11:34
sort from textfile by coloumn
example name||date||time||comments
if you want to sort by date
$column = 2
<?php
function array_sort($array,$column){
 $column = $column-1;
 foreach($array as $line){
 $bits = explode("||",$line);
 $bits ="$bits[$column]**$line";
 $array1[]=$bits;
}
 asort($array1);
 foreach($array1 as $line){
 $bit = explode("**",$line);
 $bit ="$bit[1]";
 $array2[]=$bit;
}
 return$array2;
}
?>
poulou_0 at hotmail dot com 21-Dec-2010 09:59
if you are not interested in high or low case sort

<?php
//where
$sortable_array[$k] = $v2;
//put
$sortable_array[$k] = strtolower($v2);

//and where
$sortable_array[$k] = $v;
//put
$sortable_array[$k] = strtolower($v);
?>
phpdotnet at m4tt dot co dot uk 16-Aug-2010 04:03
Simple function to sort an array by a specific key. Maintains index association.

<?php

function array_sort($array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

$people = array(
    12345 => array(
        'id' => 12345,
        'first_name' => 'Joe',
        'surname' => 'Bloggs',
        'age' => 23,
        'sex' => 'm'
    ),
    12346 => array(
        'id' => 12346,
        'first_name' => 'Adam',
        'surname' => 'Smith',
        'age' => 18,
        'sex' => 'm'
    ),
    12347 => array(
        'id' => 12347,
        'first_name' => 'Amy',
        'surname' => 'Jones',
        'age' => 21,
        'sex' => 'f'
    )
);

print_r(array_sort($people, 'age', SORT_DESC)); // Sort by oldest first
print_r(array_sort($people, 'surname', SORT_ASC)); // Sort by surname

/*
Array
(
    [12345] => Array
        (
            [id] => 12345
            [first_name] => Joe
            [surname] => Bloggs
            [age] => 23
            [sex] => m
        )

    [12347] => Array
        (
            [id] => 12347
            [first_name] => Amy
            [surname] => Jones
            [age] => 21
            [sex] => f
        )

    [12346] => Array
        (
            [id] => 12346
            [first_name] => Adam
            [surname] => Smith
            [age] => 18
            [sex] => m
        )

)
Array
(
    [12345] => Array
        (
            [id] => 12345
            [first_name] => Joe
            [surname] => Bloggs
            [age] => 23
            [sex] => m
        )

    [12347] => Array
        (
            [id] => 12347
            [first_name] => Amy
            [surname] => Jones
            [age] => 21
            [sex] => f
        )

    [12346] => Array
        (
            [id] => 12346
            [first_name] => Adam
            [surname] => Smith
            [age] => 18
            [sex] => m
        )

)
*/

?>
eriewave at hotmail dot com 29-Mar-2010 10:16
If you need to sort an array containing some equivalent values and you want the equivalents to end up next to each other in the overall order (similar to a MySQL's ORDER BY output), rather than breaking the function, do this:

<?php

sort($array, ksort($array))

?>

-When the sort() function finds two equivalents, it will sort them arbitrarily by their key #'s as a second parameter.

-Dirk
Dollar Hauler Admin 12-Mar-2010 07:10
I could never find a way to sort multidimensional arrays with 5+ keys while maintaining the data structure, but here it is:

(you can add an infinite number of keys, but it has to be added manually :\ )

<?php

$array[0]['name']  = 'Chris';
$array[0]['phone'] = '3971095';
$array[0]['year']  = '1978';
$array[0]['address'] = 'Street 1';

$array[1]['name']  = 'Breanne';
$array[1]['phone'] = '3766350';
$array[1]['year']  = '1990';
$array[1]['address'] = 'Street 2';

$array[2]['name']  = 'Dusty';
$array[2]['phone'] = '1541120';
$array[2]['year']  = '1982';
$array[2]['address'] = 'Street 3';

function multisort($array, $sort_by, $key1, $key2=NULL, $key3=NULL, $key4=NULL, $key5=NULL, $key6=NULL){
    // sort by ?
    foreach ($array as $pos =>  $val)
        $tmp_array[$pos] = $val[$sort_by];
    asort($tmp_array);

    // display however you want
    foreach ($tmp_array as $pos =>  $val){
        $return_array[$pos][$sort_by] = $array[$pos][$sort_by];
        $return_array[$pos][$key1] = $array[$pos][$key1];
        if (isset($key2)){
            $return_array[$pos][$key2] = $array[$pos][$key2];
            }
        if (isset($key3)){
            $return_array[$pos][$key3] = $array[$pos][$key3];
            }
        if (isset($key4)){
            $return_array[$pos][$key4] = $array[$pos][$key4];
            }
        if (isset($key5)){
            $return_array[$pos][$key5] = $array[$pos][$key5];
            }
        if (isset($key6)){
            $return_array[$pos][$key6] = $array[$pos][$key6];
            }
        }
    return $return_array;
    }

//usage (only enter the keys you want sorted):

$sorted = multisort($array,'year','name','phone','address');
print_r($sorted);

//output:
Array ( [0] => Array ( [year] => 1978 [name] => Chris [phone] => 3971095 [address] => Street 1 ) [2] => Array ( [year] => 1982 [name] => Dusty [phone] => 1541120 [address] => Street 3 ) [1] => Array ( [year] => 1990 [name] => Breanne [phone] => 3766350 [address] => Street 2 ) )
Brecht Cloetens 24-Nov-2009 12:57
<?php

/**
 * function: array_columns
 * author: Brecht Cloetens
 * params: $a = array() // original array
 *         $c = int() // number of columns
 */
function array_columns(&$a, $c=2)
{
    $m = ceil(count($a)/$c);
    $j = 0;
    for($i=0; $i<$m; $i++) {
        for($k=0; $k<$c; $k++) {
            $key = $i+($m*$k);
            settype($key,'integer');
            if(array_key_exists($key,$a)) {
                $b[$j] = $a[$key];
                $j++;
            }
        }
    }
    $a = $b;
}

$arr = range('a','z');
array_columns($arr,4);
print_r($arr);

?>

Example:
array(1,2,3,4,5) will be converted to array(1,4,2,5,3);

This can be easy if you want to display an array into a specified number of columns.

<table>
    <tr>
        <td>$arr[0] => 1</td>
        <td>$arr[1] => 4</td>
    </tr>
    <tr>
        <td>$arr[2] => 2</td>
        <td>$arr[3] => 5</td>
    </tr>
    <tr>
        <td>$arr[4] => 3</td>
        <td></td>
    </tr>
</table>
Anonymous 17-Nov-2009 03:10
[EDIT BY danbrown AT php DOT net: The code provided by this anonymous author contains bugfixes for and additions to code originally donated by "toenie" on 07-NOV-09.  The original author included the following note: "If you want to sort while using an 2 dimensional array, you can use this script I just wrote for myself. I thought it could be helpful for other people too."]

I also added the ability to sort in ascending and descending order.

$order = "ASC" will sort the array in ascending order
$order = "DESC" will sort the array in descending order

Here is the code:

<?php
    function order_array_num ($array, $key, $order = "ASC")
    {
        $tmp = array();
        foreach($array as $akey => $array2)
        {
            $tmp[$akey] = $array2[$key];
        }

        if($order == "DESC")
        {arsort($tmp , SORT_NUMERIC );}
        else
        {asort($tmp , SORT_NUMERIC );}

        $tmp2 = array();
        foreach($tmp as $key => $value)
        {
            $tmp2[$key] = $array[$key];
        }

        return $tmp2;
    }
?>
petr dot biza at gmail dot com 11-Sep-2009 03:29
Here is a function to sort an array by the key of his sub-array with keep key in top level.

<?php
function sksort(&$array, $subkey="id", $sort_descending=false, $keep_keys_in_sub = false) {
    $temp_array = $array;

    foreach ($temp_array as $key => &$value) {

      $sort = array();
      foreach ($value as $index => $val) {
          $sort[$index] = $val[$subkey];
      }

      asort($sort);

      $keys = array_keys($sort);
      $newValue = array();
      foreach ($keys as $index) {
        if($keep_keys_in_sub)
            $newValue[$index] = $value[$index];
          else
            $newValue[] = $value[$index];
      }

      if($sort_descending)
        $value = array_reverse($newValue, $keep_keys_in_sub);
      else
        $value = $newValue;
    }

    $array = $temp_array;
  }
?>
phpnet at theindy dot net 27-Aug-2009 07:45
In response to the msort function I find that the array_multisort function is a lot faster.

-John
danm68 at gmail dot com 26-Aug-2009 06:59
sort() used with strings doesn't sort just alphabetically. It sorts all upper-case strings alphabetically first and then sorts lower-case strings alphabetically second.
Just in case anyone was as confused as I was and I've never seen this mentioned anywhere.
otobrglez at gmail dot com 18-Feb-2009 09:45
This will select number of unique keys from array and order them in original order.

<?php
/*
$rows - array of records
$st - number of keys that you want to have

By Oto Brglez.

*/

$pom_k = array();
for($i=0; $i<$st;){
    $pom = array_rand($rows,1);
    if(!in_array($pom,$pom_k)){
        $pom_k[] = $pom;
        $i++;
    };
};
sort($pom_k,SORT_NUMERIC);

?>
stepmuel at ee dot ethz dot ch 10-Dec-2008 05:14
A little shorter way to sort an array of objects; with a callback function.

<?php
function objSort(&$objArray,$indexFunction,$sort_flags=0) {
    $indices = array();
    foreach($objArray as $obj) {
        $indeces[] = $indexFunction($obj);
    }
    return array_multisort($indeces,$objArray,$sort_flags);
}

function getIndex($obj) {
    return $obj->getPosition();
}

objSort($objArray,'getIndex');
?>
jalil at measat dot org 15-Nov-2008 10:48
this is an implementation of the complement of
Matthew Hood's objectSort (http://my.php.net/manual/en/function.sort.php#75036), which i found very convenient for sorting objects.

this does the reverse, it sorts according to the key
selected for the object but in reverse order.
and having both sort methods allows consistency and convenience for sorting objects, if speed isn't your major concern.

the only change ( apart form data being reworded as object )  is the use of < instead of > in the original.
you could of couse incorporate all in one routine, but why
complicate matters.

<?php
    function objectRSort(&$object, $key)
    {
        for ($i = count($object) - 1; $i >= 0; $i--)
        {
          $swapped = false;
          for ($j = 0; $j < $i; $j++)
          {
               if ($object[$j]->$key < $object[$j + 1]->$key)
               {
                    $tmp = $object[$j];
                    $object[$j] = $object[$j + 1];
                    $object[$j + 1] = $tmp;
                    $swapped = true;
               }
          }
          if (!$swapped) return;
        }
    }
?>
www at designdetector dot com 09-Sep-2008 04:43
To sort an array of multiple text fields alphabetically you have to make the text lowercase before sorting the array. Otherwise PHP puts acronyms before words. You can see this in my example code. Simply store the original text field at the end of the array line and call it later from there. You can safely ignore the lowercase version which is added to the start of the array line.

<?php
echo '<pre>ORIGINAL DATA:
<br />';

$data = array(
'Saturn|7|8|9|0||',
'Hello|0|1|2|3||',
'SFX|5|3|2|4||',
'HP|9|0|5|6||'
);

print_r($data);

sort($data);
reset($data);

echo '<br />RAW SORT:
<br />';

print_r($data);

for ($c = 0; $c < count($data); $c++) {
    list ($letter,$g1,$g2,$g3,$g4,$end) = explode ('|', $data[$c]);
    $lowercase = strtolower($letter);
    $data2[$c] = array($lowercase,$g1,$g2,$g3,$g4,$letter);
}

sort($data2);
reset($data2);

echo '<br />LOWERCASE SORT:
<br />';

print_r($data2);

echo '</pre>';
?>
matpatnik at hotmail dot com 24-Jan-2008 12:46
This function will sort entity letters eg:&eacute;

I hope that help someone

function sort_entity($array) {
    $total = count($array);
    for ($i=0;$i<$total;$i++) {
        if ($array[$i]{0} == '&') {
            $array[$i] = $array[$i]{1}.$array[$i];
        } else {
            $array[$i] = $array[$i]{0}.$array[$i];
        }
    }
    sort($array);

    for ($i=0;$i<$total;$i++) {
        $array[$i] = substr($array[$i],1);
    }

    return $array;
}
NBS Studio 04-Jul-2007 10:14
This is my way of sorting files into date modified date order. It worked for me!

$dir='topics';
$ext='php5';
$files=scandir($dir);
foreach($files as $fs){
    if(($fs!='.')&&($fs!='..')){
        $fs1.='￢'.filemtime($dir.'/'.$fs).'#'.$fs;
    }
}
$fs2=split('[￢]',$fs1);
arsort($fs2);
foreach($fs2 as $fs3){
    if(eregi($ext,$fs3)){
        $fs4.='￢'.$fs3;
    }
}
$fs5=split('[#]',$fs4);
foreach($fs5 as $fs6){
    if(eregi($ext,$fs6)){
        $fs7.='￢'.$fs6;
    }
}
$fs8=split('[￢]',$fs7);
foreach($fs8 as $fs9){
    $file_list.=$fs9.'
</br>';
}

print $file_list;
sinan at sinaneldem dot com 24-Jun-2007 05:29
here is little script which will merge arrays, remove duplicates and sort it by alphabetical order:

<?php

$array1 = array('apple', 'banana','pear');
$array2 = array('grape', 'pear','orange');

function array_unique_merge_sort($array1, $array2){
$array = array_unique(array_merge($array1, $array2));
sort($array);
foreach ($array as $key => $value) {
    $new[$key] = $value;
}
return $new;
}

print_r (array_unique_merge_sort($array1, $array2));

?>

this will print out:

Array ( [0] => apple [1] => banana [2] => grape [3] => orange [4] => pear )
alishahnovin at hotmail dot com 26-May-2007 01:11
I had a multidimensional array, which needed to be sorted by one of the keys. This is what I came up with...

<?php
function msort($array, $id="id") {
        $temp_array = array();
        while(count($array)>0) {
            $lowest_id = 0;
            $index=0;
            foreach ($array as $item) {
                if ($item[$id]<$array[$lowest_id][$id]) {
                    $lowest_id = $index;
                }
                $index++;
            }
            $temp_array[] = $array[$lowest_id];
            $array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
        }
        return $temp_array;
    }
?>

Ex:

<?php

//oh no, this is not in the ordered by id!!
$data[] = array("item"=>"item 4", "id"=>4);
$data[] = array("item"=>"item 1", "id"=>1);
$data[] = array("item"=>"item 3", "id"=>3);
$data[] = array("item"=>"item 2", "id"=>2);

var_dump(  msort($data)  ); //just msort it!

/* outputs

array
  0 =>
    array
      'item' => 'item 1' (length=6)
      'id' => 1
  1 =>
    array
      'item' => 'item 2' (length=6)
      'id' => 2
  2 =>
    array
      'item' => 'item 3' (length=6)
      'id' => 3
  3 =>
    array
      'item' => 'item 4' (length=6)
      'id' => 4

*/

?>
joris at mangrove dot nl 01-Feb-2007 02:40
Commenting on note http://www.php.net/manual/en/function.sort.php#62311 :

Sorting an array of objects will not always yield the results you desire.

As pointed out correctly in the note above, sort() sorts the array by value of the first member variable. However, you can not always assume the order of your member variables! You must take into account your class hierarchy!

By default, PHP places the inherited member variables on top, meaning your first member variable is NOT the first variable in your class definition!
However, if you use code analyzers or a compile cache, things can be very different. E.g., in eAccelerator, the inherited member variables are at the end, meaning you get different sort results with caching on or off.

Conclusion:
Never use sort on arrays with values of a type other than scalar or array.
alex [at] vkpb [dot] com 26-Jan-2007 12:36
Sorting of an array by a method of inserts.

<?

       function sortByField($multArray,$sortField,$desc=true){
            $tmpKey='';
            $ResArray=array();

            $maIndex=array_keys($multArray);
            $maSize=count($multArray)-1;

            for($i=0; $i < $maSize ; $i++) {

               $minElement=$i;
               $tempMin=$multArray[$maIndex[$i]][$sortField];
               $tmpKey=$maIndex[$i];

                for($j=$i+1; $j <= $maSize; $j++)
                  if($multArray[$maIndex[$j]][$sortField] < $tempMin ) {
                     $minElement=$j;
                     $tmpKey=$maIndex[$j];
                     $tempMin=$multArray[$maIndex[$j]][$sortField];

                  }
                  $maIndex[$minElement]=$maIndex[$i];
                  $maIndex[$i]=$tmpKey;
            }

           if($desc)
               for($j=0;$j<=$maSize;$j++)
                  $ResArray[$maIndex[$j]]=$multArray[$maIndex[$j]];
           else
              for($j=$maSize;$j>=0;$j--)
                  $ResArray[$maIndex[$j]]=$multArray[$maIndex[$j]];

           return $ResArray;
       }

// make array
$array['aaa']=array("name"=>"vasia","order"=>1);
$array['bbb']=array("name"=>"petia","order"=>2);
$array['ccc']=array("name"=>"kolia","order"=>3);
$array['ddd']=array("name"=>"zenia","order"=>4);

// set sort
$SortOrder=0; // desc by default , 1- asc

var_dump(sortByField($array,'order',$SortOrder));

array
  'ddd' =>
    array
      'name' => 'zenia' (length=5)
      'order' => 4
  'aaa' =>
    array
      'name' => 'vasia' (length=5)
      'order' => 1
  'bbb' =>
    array
      'name' => 'petia' (length=5)
      'order' => 2
  'ccc' =>
    array
      'name' => 'kolia' (length=5)
      'order' => 3

?>
g8z at yahoo dot com 15-Jul-2006 01:28
<?php
/**
This sort function allows you to sort an associative array while "sticking" some fields.

$sticky_fields = an array of fields that should not be re-sorted. This is a method of achieving sub-sorts within contiguous groups of records that have common data in some fields.

For example:

$a = array();

$a []= array(
    'name'         => 'Sam',
    'age'         => 23,
    'hire_date'    => '2004-01-01'
);
$a []= array(
    'name'        => 'Sam',
    'age'        => 44,
    'hire_date'    => '2003-03-23'
);
$a []= array(
    'name'        => 'Jenny',
    'age'        => 20,
    'hire_date' => '2000-12-31'
);
$a []= array(
    'name'        => 'Samantha',
    'age'        => 50,
    'hire_date' => '2000-12-14'
);

$sticky_fields = array( 'name' );
print_r( stickysort( $a, 'age', DESC_NUM, $sticky_fields ) );

OUTPUT:

Array
(
    [0] => Array
        (
            [name] => Sam
            [age] => 44
            [hire_date] => 2003-03-23
        )
    [1] => Array
        (
            [name] => Sam
            [age] => 23
            [hire_date] => 2004-01-01
        )
    [2] => Array
        (
            [name] => Jenny
            [age] => 20
            [hire_date] => 2000-12-31
        )
    [3] => Array
        (
            [name] => Samantha
            [age] => 50
            [hire_date] => 2000-12-14
        )
)

Here's why this is the correct output - the "name" field is sticky, so it cannot change its sort order. Thus, the "age" field is only sorted as a sub-sort within records where "name" is identical. Thus, the "Sam" records are reversed, because 44 > 23, but Samantha remains at the bottom, even though her age is 50. This is a way of achieving "sub-sorts" and "sub-sub-sorts" (and so on) within records of identical data for specific fields.

Courtesy of the $5 Script Archive: http://www.tufat.com
**/

define( 'ASC_AZ', 1000 );
define( 'DESC_AZ', 1001 );
define( 'ASC_NUM', 1002 );
define( 'DESC_NUM', 1003 );

function stickysort( $arr, $field, $sort_type, $sticky_fields = array() ) {
    $i = 0;
    foreach ($arr as $value) {
        $is_contiguous = true;
        if(!empty($grouped_arr)) {
            $last_value = end($grouped_arr[$i]);

            if(!($sticky_fields == array())) {
                foreach ($sticky_fields as $sticky_field) {
                    if ($value[$sticky_field] <> $last_value[$sticky_field]) {
                        $is_contiguous = false;
                        break;
                    }
                }
            }
        }
        if ($is_contiguous)
            $grouped_arr[$i][] = $value;
        else
            $grouped_arr[++$i][] = $value;
    }
    $code = '';
    switch($sort_type) {
        case ASC_AZ:
            $code .= 'return strcasecmp($a["'.$field.'"], $b["'.$field.'"]);';
            break;
        case DESC_AZ:
            $code .= 'return (-1*strcasecmp($a["'.$field.'"], $b["'.$field.'"]));';
            break;
        case ASC_NUM:
            $code .= 'return ($a["'.$field.'"] - $b["'.$field.'"]);';
            break;
        case DESC_NUM:
            $code .= 'return ($b["'.$field.'"] - $a["'.$field.'"]);';
            break;
    }

    $compare = create_function('$a, $b', $code);

    foreach($grouped_arr as $grouped_arr_key=>$grouped_arr_value)
        usort ( $grouped_arr[$grouped_arr_key], $compare );

    $arr = array();
    foreach($grouped_arr as $grouped_arr_key=>$grouped_arr_value)
        foreach($grouped_arr[$grouped_arr_key] as $grouped_arr_arr_key=>$grouped_arr_arr_value)
            $arr[] = $grouped_arr[$grouped_arr_key][$grouped_arr_arr_key];

    return $arr;
}
?>
g8z at yahoo dot com 10-Jul-2006 05:58
<?php
/**
This sort function allows you to sort an associative array while "sticking" some fields.

$sticky_fields = an array of fields that should not be re-sorted. This is a method of achieving sub-sorts within contiguous groups of records that have common data in some fields.

Courtesy of the $5 Script Archive: http://www.tufat.com
**/

define( 'ASC_AZ', 1000 );
define( 'DESC_AZ', 1001 );
define( 'ASC_NUM', 1002 );
define( 'DESC_NUM', 1003 );

function stickysort( $arr, $field, $sort_type, $sticky_fields = array() ) {
    $i = 0;
    foreach ($arr as $value) {
        $is_contiguous = true;
        if(!empty($grouped_arr)) {
            $last_value = end($grouped_arr[$i]);

            if(!($sticky_fields == array())) {
                foreach ($sticky_fields as $sticky_field) {
                    if ($value[$sticky_field] <> $last_value[$sticky_field]) {
                        $is_contiguous = false;
                        break;
                    }
                }
            }
        }
        if ($is_contiguous)
            $grouped_arr[$i][] = $value;
        else
            $grouped_arr[++$i][] = $value;
    }
    $code = '';
    switch($sort_type) {
        case ASC_AZ:
            $code .= 'return strcasecmp($a["'.$field.'"], $b["'.$field.'"]);';
            break;
        case DESC_AZ:
            $code .= 'return (-1*strcasecmp($a["'.$field.'"], $b["'.$field.'"]));';
            break;
        case ASC_NUM:
            $code .= 'return ($a["'.$field.'"] - $b["'.$field.'"]);';
            break;
        case DESC_NUM:
            $code .= 'return ($b["'.$field.'"] - $a["'.$field.'"]);';
            break;
    }

    $compare = create_function('$a, $b', $code);

    foreach($grouped_arr as $grouped_arr_key=>$grouped_arr_value)
        usort ( $grouped_arr[$grouped_arr_key], $compare );

    $arr = array();
    foreach($grouped_arr as $grouped_arr_key=>$grouped_arr_value)
        foreach($grouped_arr[$grouped_arr_key] as $grouped_arr_arr_key=>$grouped_arr_arr_value)
            $arr[] = $grouped_arr[$grouped_arr_key][$grouped_arr_arr_key];

    return $arr;
}
?>
Emiliyan at ServicesBG dot Com 29-Mar-2006 04:41
#This is a function that will sort an array...
function sort_by($array,  $keyname = null, $sortby) {
   $myarray = $inarray = array();
   # First store the keyvalues in a seperate array
    foreach ($array as $i => $befree) {
        $myarray[$i] = $array[$i][$keyname];
    }
   # Sort the new array by
    switch ($sortby) {
    case 'asc':
    # Sort an array and maintain index association...
    asort($myarray);
    break;
    case 'arsort':
    # Sort an array in reverse order and maintain index association
    arsort($myarray);
    break;
    case 'natcasesor':
    # Sort an array using a case insensitive "natural order" algorithm
    natcasesort($myarray);
    break;
    }
    # Rebuild the old array
    foreach ( $myarray as $key=> $befree) {
       $inarray[$key] = $array[$key];
    }
    return $inarray;
}
sort_by(); example...
$info = sort_by($myarray, 'name', $use = 'asc');
print_r($info);
ludvig dot ericson at gmail dot com 25-Feb-2006 06:48
A tip for those who like "raul at jimi dot com dot mx" need to preserve keys after changing stuff in the middle of an array:
array_values.

Example:
<?php
$array = array(1, 2, 5, 9, 3);
unset($array[3]); // Remove index 3, which is 9.
$array = array_values($array);
?>

Hint: array_values can be fine for removing keys and reindex them by number instead, too (applies to functions like posix_pwgetuid which returns an associative array, unlike C and others, call array_values on it, and it'll be the same format IIRC.)
jesper at snt dot utwente dot nl 24-Feb-2006 10:26
If you sort an array of objects, the first variable in the object will be used for sorting:

<?php
class foo
{
  var $value; //First variable: Used for sorting
  var $id;

  function foo($i, $v)
  {
     $this->id = $i;
     $this->value = $v;
  }

}

for ($i = 0; $i < 10; $i++)
{
  $bar[] = new foo($i,rand(1,10));
}

// This will sort on value
sort($bar);
print_r($bar);
?>

Compare the piece of code above with the following:

<?php
class foo
{
  var $id; //First variable: Used for sorting
  var $value;

  function foo($i, $v)
  {
     $this->id = $i;
     $this->value = $v;
  }

}

for ($i = 0; $i = 10; $i++)
{
  $bar[] = new foo($i,rand(1,10));
}

// This will sort on id
sort($bar);
print_r($bar);
?>

As you can see the location of declaration of the variables matter!
If you want to sort on both or on a combination of variables, use ksort()
jerome a-t+ yamafoto d*o*t com 22-Feb-2006 06:39
when sorting an array, beware of variable type from elements you put in this array

Example:

$a = 2; // $a is an integer
$b = 'item';
$arr = array($a, $b);
sort($arr);

print_r($arr);

this will output:
$arr[0] = 'item';
$arr[1] = 2;

$a = '2'; // $a is a string
$b = 'item';
$arr = array($a, $b);
sort($arr);

print_r($arr);

this will output:
$arr[0] = '2';
$arr[1] = 'item'

to avoid this problem use:

sort($arr, SORT_STRING)
nm at thenoodleman dot com 31-Jan-2006 02:18
Faster, more effective function:

array_sort (array, ['asc'/'desc'])

Second parameter specifies whether to order ascending or descending. Default is ascending.

function array_sort($array, $type='asc'){
    $result=array();
    foreach($array as $var => $val){
        $set=false;
        foreach($result as $var2 => $val2){
            if($set==false){
                if($val>$val2 && $type=='desc' || $val<$val2 && $type=='asc'){
                    $temp=array();
                    foreach($result as $var3 => $val3){
                        if($var3==$var2) $set=true;
                        if($set){
                            $temp[$var3]=$val3;
                            unset($result[$var3]);
                        }
                    }
                    $result[$var]=$val;
                    foreach($temp as $var3 => $val3){
                        $result[$var3]=$val3;
                    }
                }
            }
        }
        if(!$set){
            $result[$var]=$val;
        }
    }
    return $result;
}

Works for ordering by integers or strings, no need to specify which.

Example:

$array=array('a' => 50, 'b' => 25, 'c' => 75);
print_r(array_sort($array));

Returns:
Array
(
[b] => 25
[a] => 50
[c] => 75
)
james at miicro dot net 24-Jan-2006 07:26
Further to john dot dutcher at highmark dot com's comments - padding the name could cause a problem if you get abnormally long names, it might be better to rebuild the array thus:

Array (
[0] => Array ( [sortname_01] => Dutcher [sortname_02] => F [sortname_03] => John [name] => Dutcher, John F )
[1] => Array ( [sortname_01] => Dutch [sortname_02] => A [sortname_03] => Roger [name] => Dutch, Roger A )
[2] => Array ( [sortname_01] => Dut [sortname_02] => H [sortname_03] => Maurice [name] => Dut, Maurice H )
[3] => Array ( [sortname_01] => Dut [sortname_02] => S [sortname_03] => Mildred [name] => Dut, Mildred S )
)

which should give:

Array (
[0] => Array ( [sortname_01] => Dut [sortname_02] => H [sortname_03] => Maurice [name] => Dut, Maurice H )
[1] => Array ( [sortname_01] => Dut [sortname_02] => S [sortname_03] => Mildred [name] => Dut, Mildred S )
[2] => Array ( [sortname_01] => Dutch [sortname_02] => A [sortname_03] => Roger [name] => Dutch, Roger A )
[3] => Array ( [sortname_01] => Dutcher [sortname_02] => F [sortname_03] => John [name] => Dutcher, John F )
)
raul at jimi dot com dot mx 02-Dec-2005 06:50
I had an array like this:
$arr=array (1,4,3,6,5);

which returns this:
$arr[0]=1
$arr[1]=4
$arr[2]=3
$arr[3]=6
$arr[4]=5

But lets say i remove [2] which is number 3, i get:

$arr[0]=1
$arr[1]=4
$arr[3]=6
$arr[4]=5

And i want to reindex without doing a sort because i dont want to lose the order of the numbers (like a pop in a stack but in the middle of the list), i do this:

$arr=array_chunk($arr,count($arr));
$arr=$arr[0];

the result is:

$arr[0]=1
$arr[1]=4
$arr[2]=6
$arr[3]=5

This can be applied mostly for tree sorting, when you only have the id and the parent values of the node, and you want to have N levels.
james at miicro dot net 19-Jul-2005 11:49
It's useful to know that if you're using this function on a multidimensional array, php will sort the first key, then the second and so on. This is similar to being able to use SQL to order by field1, field2 etc.

So:

Array (
[0] => Array ( [category] => work [name] => Smith )
[1] => Array ( [category] => play [name] => Johnson )
[2] => Array ( [category] => work [name] => Berger )
)

will become:

Array (
[0] => Array ( [category] => play [name] => Johnson )
[1] => Array ( [category] => work [name] => Berger )
[2] => Array ( [category] => work [name] => Smith )
)

Hope it helps someone.
timc at hlyw dot com 18-Feb-2005 01:04
I dig the multi_sort function(s) from above.  But, they don't work for hash arrays.  I added a keys variable to keep track of the key value as the array gets sorted.  Feed back welcome.

<?php
function array_qsort (&$array, $column=0, $order=SORT_ASC, $first=0, $last= -2)
{
  // $array  - the array to be sorted
  // $column - index (column) on which to sort
  //          can be a string if using an associative array
  // $order  - SORT_ASC (default) for ascending or SORT_DESC for descending
  // $first  - start index (row) for partial array sort
  // $last  - stop  index (row) for partial array sort
  // $keys  - array of key values for hash array sort

  $keys = array_keys($array);
  if($last == -2) $last = count($array) - 1;
  if($last > $first) {
   $alpha = $first;
   $omega = $last;
   $key_alpha = $keys[$alpha];
   $key_omega = $keys[$omega];
   $guess = $array[$key_alpha][$column];
   while($omega >= $alpha) {
     if($order == SORT_ASC) {
       while($array[$key_alpha][$column] < $guess) {$alpha++; $key_alpha = $keys[$alpha]; }
       while($array[$key_omega][$column] > $guess) {$omega--; $key_omega = $keys[$omega]; }
     } else {
       while($array[$key_alpha][$column] > $guess) {$alpha++; $key_alpha = $keys[$alpha]; }
       while($array[$key_omega][$column] < $guess) {$omega--; $key_omega = $keys[$omega]; }
     }
     if($alpha > $omega) break;
     $temporary = $array[$key_alpha];
     $array[$key_alpha] = $array[$key_omega]; $alpha++;
     $key_alpha = $keys[$alpha];
     $array[$key_omega] = $temporary; $omega--;
     $key_omega = $keys[$omega];
   }
   array_qsort ($array, $column, $order, $first, $omega);
   array_qsort ($array, $column, $order, $alpha, $last);
  }
}
?>
anthony at ectrolinux dot com 09-Sep-2004 01:39
In a brief addition to the previous poster's message, the ascending sorting order used by PHP directly corresponds to ISO-8859-1 (ASCII). Therefore the character \48 (numeral 0) would be placed before the character \82 (R), which would be placed before the character \110 (n), and so forth.
teunkloosterman at hotmail dot com 30-Aug-2004 10:14
Just to show how it sorts:

<?php
$array = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", "!", "@", "#", "\\\$", "%", "^", "&", "*", "(", ")", "_", "-", "=", "+", "\\\\", "|", ",", "<", ".", ">", "?", "'", "\\\"", "`", "~");
sort($array);
echo implode("", $array);
?>

returns:

 !"#$%&'()*+,-.0123456789<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ
\^_`abcdefghijklmnopqrstuvwxyz|~

note: the result begins with a space
arjan321 at hotmail dot com 31-Jan-2003 09:23
Ik you want to sort case insensitive, use the natcasesort()
phpdotnetNO_SPAM at electronic-strategy dot com 05-Jul-2001 06:34
/*
Small function to Alphabetically sort Multidimensional arrays by index values of an n dimension array.

I have only tested this for sorting an array of up to 6 dimensions by a value within the second dimension. This code is very rough and works for my purposes, but has not been tested beyond my needs.

Although a little clunky and not a mathematical type algorithm, it get's the job done. It theoretically overcomes many of the problems I have seen with multidimensional arrays in that it is possible to specify within the function, not by reference :-(, which index you wish to sort by, no matter how many dimensions down.

call function by assigning it to a new / existing array:

$row_array = multidimsort($row_array);
*/

function multidimsort($array_in) {
     $multiarray = array();
    $array_out = array();
    $loopvalue = 0;

    /* -1 as traversal of array starts from 0, count() starts from 1 */
    $multicount = count($array_in) - 1;

    /* add the indexes you wish to sort array by to a new array in this case index is two levels down, but shouldn't make a difference if it goes further indexes down. (Not tested!) */
    for($i = 0; $i <= $multicount; $i++) {
        array_push($multiarray, $array_in[$i][2]);
        //array_push($multiarray, $array_in[$i][2][4]);
        //array_push($multiarray, $array_in[$i][1][3][7]);
    }

    /* alphabetically sort the new array (Ascending in this case) can chage sort to whatever type you like. Even apply user-defined sort. */
    asort($multiarray);

    /* reset internal pointer to beginning of array after above sort */
    reset($multiarray);

    /* traverse new array of index values and add the corresponding element of the input array to the correct position in the output array */
    while (list ($key, $val) = each ($multiarray)) {

        $array_out[$loopvalue] = $array_in[$key];

        $loopvalue++;
    }

    /* return the output array which is all nicely sorted by the index you wanted! */
    return $array_out;
}
peek at mailandnews dot com 07-Apr-2001 01:06
I ran into the same problem with case insensitive sorting. Actually I think there should be a SORT_STRING_CASE flag but I tried the following:

usort($listing, 'strcasecmp');

This didn't work (why not?), but you can do a proper case insensitive sort like this:

usort($listing, create_function('$a,$b','return strcasecmp($a,$b);'));


사용자 정의 비교 함수로 배열을 정렬하고 인덱스 연관성을 유지

  • ≪ sort
  • uksort ≫
  • PHP Manual
  • 배열 함수 목록
  • 사용자 정의 비교 함수로 배열을 정렬하고 인덱스 연관성을 유지

uasort

(PHP 4, PHP 5, PHP 7)

uasort — 사용자 정의 비교 함수로 배열을 정렬하고 인덱스 연관성을 유지

설명

bool uasort ( array &$array , callback $cmp_function )

이 함수는 배열 인덱스가 그와 관련되어 있는 배열 원소들과의 연관성을 유지하면서 사용자 정의 비교 함수를 사용하여 배열을 정렬한다.

연관 배열을 정렬하면서 실제 원소 순서가 중요할 때 주로 사용합니다.

인수

array

입력 배열.

cmp_function

사용자 정의 비교 함수의 예제는 usort()와 uksort()를 참고하십시오.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • usort() - 사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬
  • uksort() - 사용자 정의 비교 함수를 사용하여 키에 의한 배열 정렬
  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • arsort() - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • ksort() - 키에 의한 배열 정렬
  • rsort() - 역순으로 배열 정렬
  • sort() - 배열 정렬

User Contributed Notes

ayaou dot mohammed at gmail dot com 16-Jun-2016 12:05
// Anonumous functions
uasort($collection, array($this, function($a, $b){
     //logic
}));
php at clement dot hk 05-Mar-2014 01:00
If you want to keep the order when two members compare as equal, use this.
<?php

function stable_uasort(&$array, $cmp_function) {
    if(count($array) < 2) {
        return;
    }
    $halfway = count($array) / 2;
    $array1 = array_slice($array, 0, $halfway, TRUE);
    $array2 = array_slice($array, $halfway, NULL, TRUE);

    stable_uasort($array1, $cmp_function);
    stable_uasort($array2, $cmp_function);
    if(call_user_func($cmp_function, end($array1), reset($array2)) < 1) {
        $array = $array1 + $array2;
        return;
    }
    $array = array();
    reset($array1);
    reset($array2);
    while(current($array1) && current($array2)) {
        if(call_user_func($cmp_function, current($array1), current($array2)) < 1) {
            $array[key($array1)] = current($array1);
            next($array1);
        } else {
            $array[key($array2)] = current($array2);
            next($array2);
        }
    }
    while(current($array1)) {
        $array[key($array1)] = current($array1);
        next($array1);
    }
    while(current($array2)) {
        $array[key($array2)] = current($array2);
        next($array2);
    }
    return;
}

function cmp($a, $b) {
    if($a['n'] == $b['n']) {
        return 0;
    }
    return ($a['n'] > $b['n']) ? -1 : 1;
}

$a = $b = array(
    'a' => array("l" => "A", "n" => 1),
    'b' => array("l" => "B", "n" => 2),
    'c' => array("l" => "C", "n" => 1),
    'd' => array("l" => "D", "n" => 2),
    'e' => array("l" => "E", "n" => 2),
);

uasort($a, 'cmp');
print_r($a);

stable_uasort($b, 'cmp');
print_r($b);
?>

returns

Array
(
    [e] => Array
        (
            [l] => E
            [n] => 2
        )

    [b] => Array
        (
            [l] => B
            [n] => 2
        )

    [d] => Array
        (
            [l] => D
            [n] => 2
        )

    [c] => Array
        (
            [l] => C
            [n] => 1
        )

    [a] => Array
        (
            [l] => A
            [n] => 1
        )

)
Array
(
    [b] => Array
        (
            [l] => B
            [n] => 2
        )

    [d] => Array
        (
            [l] => D
            [n] => 2
        )

    [e] => Array
        (
            [l] => E
            [n] => 2
        )

    [a] => Array
        (
            [l] => A
            [n] => 1
        )

    [c] => Array
        (
            [l] => C
            [n] => 1
        )
)

https://bugs.php.net/bug.php?id=53341
Anonymous 16-Aug-2013 07:35
//this fix the problem of if any of these sort functions evaluates two members as equal then the order is undefined (the sorting is not stable).

$pos=0;
foreach($values as $k => $v)
  $tosort[$k]=array($v,$pos++);
uasort($tosort,function($a, $b) {
  if($a[0] != $b[0])return ($a[0] < $b[0]) ? -1 : 1;
    return ($a[1] < $b[1]) ? -1 : 1;}
);
foreach($tosort as $k => $v)
  $values[$k]=$v[0];
iborodikhin at gmail dot com 22-Nov-2012 05:32
To shuffle assoc array preserving keys just do this:
<?php uasort(
    $array,
    function ($a, $b) {
        return mt_rand(0, 1) > 0 ? 1 : -1;
    }
); ?>
Said Dashuk 12-Oct-2011 07:33
My simple and effective solution for sort multi-dimensional array by any key:

<?php
function sort_by_key ($arr,$key) {
    global $key2sort;
    $key2sort = $key;
    uasort($arr, 'sbk');
    return ($arr);
}
function sbk ($a, $b) {global $key2sort; return (strcasecmp ($a[$key2sort],$b[$key2sort]));}
?>
yannick dot battail at gmail dot com 17-Nov-2010 03:37
An Example using anonymous function.
Anonymous functions make some time the code easier to understand.
<?php
$fruits = array('Orange9','Orange11','Orange10','Orange6','Orange15');
uasort ( $fruits , function ($a, $b) {
            return strnatcmp($a,$b); // or other function/code
        }
    );
print_r($fruits);
?>
returns
Array
(
    [3] => Orange6
    [0] => Orange9
    [2] => Orange10
    [1] => Orange11
    [4] => Orange15
)
magikMaker 19-Oct-2010 11:48
a quick reminder on the syntax if you want to use uasort in a Class or Object:

<?php

// procedural:
uasort($collection, 'my_sort_function');

// Object Oriented
uasort($collection, array($this, 'mySortMethod'));

// Objet Oriented with static method
uasort($collection, array('self', 'myStaticSortMethod'));

?>
phire_sk 05-Oct-2010 11:19
I tried using some of the previous built multisorts, but they weren't working as expected.

So, I made my own Class, and it seems to work wonderfully.

Here is the code:

<?php
/************************************
*    Allows sorting multi-dimensional
*    arrays by a specific key and in
*    asc or desc order
**/
class multiSort
{
    var $key;    //key in your array

    //runs the sort, and returns sorted array
    function run ($myarray, $key_to_sort, $type_of_sort = '')
    {
        $this->key = $key_to_sort;

        if ($type_of_sort == 'desc')
            uasort($myarray, array($this, 'myreverse_compare'));
        else
            uasort($myarray, array($this, 'mycompare'));

        return $myarray;
    }

    //for ascending order
    function mycompare($x, $y)
    {
        if ( $x[$this->key] == $y[$this->key] )
            return 0;
        else if ( $x[$this->key] < $y[$this->key] )
            return -1;
        else
            return 1;
    }

    //for descending order
    function myreverse_compare($x, $y)
    {
        if ( $x[$this->key] == $y[$this->key] )
            return 0;
        else if ( $x[$this->key] > $y[$this->key] )
            return -1;
        else
            return 1;
    }
}
?>
paul at webtop-designs dot com 30-Nov-2009 10:40
Just expanding on php arobase kochira period com's method:

If you are looking to sort a multi-D array by a specific column and have entries in both upper and lower case, simply drop the entries to lowercase before doing the strcmp.

<?php
$dirs = array(
  array('name' => 'First Folder', 'path' => 'sompath'),
  array('name' => 'second folder', 'path' => 'sompath2'),
  array('name' => 'Third Folder', 'path' => 'sompath3')
);

function so($a, $b) {
    return (strcmp (strtolower($a['name']), strtolower($b['name'])));
}
?>
php arobase kochira period com 08-Jun-2008 01:13
Difference between uasort() and usort(), the missing example ...

<?php
  $arr = array ( 10 => array('id' => 'dix',  'aa' => '1010'),
                100 => array('id' => 'cent', 'aa' => '100100'),
                  2 => array('id' => 'deux', 'aa' => '22'),
                  7 => array('id' => 'sept', 'aa' => '77'));

  // id sorting
  function so ($a, $b) { return (strcmp ($a['id'],$b['id']));    }
?>

*** uasort($arr, 'so') output:

<?php Array (
    [100] => Array
        (
            [id] => cent
            [aa] => 100100
        )

    [2] => Array
        (
            [id] => deux
            [aa] => 22
        )

    [10] => Array
        (
            [id] => dix
            [aa] => 1010
        )

    [7] => Array
        (
            [id] => sept
            [aa] => 77
        ))?>

*** usort($arr, 'so') output:

<?php Array (
    [0] => Array
        (
            [id] => cent
            [aa] => 100100
        )

    [1] => Array
        (
            [id] => deux
            [aa] => 22
        )

    [2] => Array
        (
            [id] => dix
            [aa] => 1010
        )

    [3] => Array
        (
            [id] => sept
            [aa] => 77
        ))?>
php at eden2 dot com 17-Jul-2003 01:36
Is it just me, or are the examples below misleading, and actually demonstrating situations that would be more appropriate for usort()?

After trying to make sense of the uasort() description, it sounds like it's more for sorting a 1D array like this:

"john" => "$23.12"
"tim" => "$6.50"
"bob" => "$18.54"

and getting back:

"tim" => "$6.50"
"bob" => "$18.54"
"john" => $23.12"

(assuming, of course, that your sort function is lopping off the $ and evaluating as a number -- which would complicate the use of asort() ;)
naholyr at yahoo dot fr 10-Jan-2003 06:35
You can sort a multidimensionnal array by any of its key with this function:

function multi_sort($array, $key)
{
  $cmp_val="((\$a['$key']>\$b['$key'])?1:
    ((\$a['$key']==\$b['$key'])?0:-1))";
  $cmp=create_function('$a, $b', "return $body;");
  uasort($array, $cmp);
  return $array;
}

example:
$myarray = array(
  array("name"=>"kernighan", "language"=>"c"),
  array("name"=>"lerdorf", "language"=>"php"),
  array("name"=>"Stroustrup", "language"=>"c++"),
  array("name"=>"Gosling", "language"=>"java")
);

multi_sort($myarray, "name") returns:
name=Gosling    language=java
name=Kernighan    language=c
name=Lerdorf    language=php
name=Stroustrup    language=c++
dholmes at jccc d0t net 09-Oct-2002 11:27
Here is a little sort function that actually uses a dynamic callback for usort to do it's thing.

It assumes your data is in the form of:
    $data = array(
            array('ID'=>'6','LAST'=>'Holmes','FIRST'=>'Dan'),
            array('ID'=>'1234','LAST'=>'Smith','FIRST'=>'Agent K'),
            array('ID'=>'2','LAST'=>'Smith','FIRST'=>'Agent J'),
            array('ID'=>'4','LAST'=>'Barney','FIRST'=>'Bob'));

Now, you want to sort on one or more cols, don't you?

masort($data, 'LAST,FIRST');
or
masort($data,array('FIRST','ID'));

Of course you could add a bunch to it (like numeric comparison if appropriate, desc/asc, etc) but it works for me.

function masort(&$data, $sortby){
    if(is_array($sortby)){
        $sortby = join(',',$sortby);
    }

    uasort($data,create_function('$a,$b','$skeys = split(\',\',\''.$sortby.'\');
        foreach($skeys as $key){
            if( ($c = strcasecmp($a[$key],$b[$key])) != 0 ){
                return($c);
            }
        }
        return($c); '));
}
Notice that I am splitting the string in the comparison function? While this is certainly slower, it was the only way I would find to "pass" and "array".  If anyone has a better way, please suggest.  Then inside, we (string) compare the values only moving to the next key if the values are the same...and so on, and so on.
stilgar_cpsNOSPAM at zipmail dot NOSPAMcom dot br 05-Sep-2001 11:30
Use example:

$array[0]['Fator1']=7;
$array[0]['Fator2']="Name";
$array[1]['Fator1']=5;
$array[1]['Fator2']="Name";
$array[2]['Fator1']=7;
$array[2]['Fator2']="NameDiferente";
.....

We want to order by Fator1, then Fator2, then:

function Compare($ar1, $ar2)
{
   if ($ar1['Fator1']<$ar2['Fator1'])
      return -1;
   else if ($ar1['Fator1']>$ar2['Fator1'])
      return 1;
   if ($ar1['Fator2']<$ar2['Fator2'])
      return -1;
   else if ($ar1['Fator2']>$ar2['Fator2'])
      return 1;
   return 0;
}

To sort now, we use:

uasort($array, 'Compare');


사용자 정의 비교 함수를 사용하여 키에 의한 배열 정렬

  • ≪ uasort
  • usort ≫
  • PHP Manual
  • 배열 함수 목록
  • 사용자 정의 비교 함수를 사용하여 키에 의한 배열 정렬

uksort

(PHP 4, PHP 5, PHP 7)

uksort — 사용자 정의 비교 함수를 사용하여 키에 의한 배열 정렬

설명

bool uksort ( array &$array , callback $cmp_function )

uksort()는 사용자 제공 비교 함수를 사용하여 키들에 의해 배열을 정렬할 것이다. 정렬하고자 하는 배열이 흔히 사용되지 않는 방법에 의해 정렬할 필요가 있으면, 이 함수를 사용해야 한다.

인수

array

입력 배열.

cmp_function

콜백 비교 함수.

cmp_function 함수는 array 키로 채워지는 두 개의 인수를 받아야 합니다. 비교 함수는 첫번째 인수가 두번째 함수보다 작거나, 같거나, 크다고 판단될 때 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 uksort() 예제

<?php
function cmp($a, $b)
{
    $a = preg_replace('@^(a|an|the) @', '', $a);
    $b = preg_replace('@^(a|an|the) @', '', $b);
    return strcasecmp($a, $b);
}

$a = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);

uksort($a, "cmp");

foreach ($a as $key => $value) {
    echo "$key: $value\n";
}
?>

위 예제의 출력:

an apple: 3
a banana: 4
the Earth: 2
John: 1

참고

  • usort() - 사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬
  • uasort() - 사용자 정의 비교 함수로 배열을 정렬하고 인덱스 연관성을 유지
  • sort() - 배열 정렬
  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • arsort() - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • ksort() - 키에 의한 배열 정렬
  • natsort() - "자연순" 알고리즘으로 배열 정렬
  • rsort() - 역순으로 배열 정렬

User Contributed Notes

andi at example dot com 24-Feb-2016 07:58
I needed to be able to sort a string that contained numbers eg.

"Slot 1 name"
"Slot 2 name"
"Slot 10 name"

using a normal string compare the "Slot 10 name" would appear before "Slot 2 name" so I wrote little function that will compare a string taking numbers in to consideration. There may be a few edge cases that need to be taken in to consideration.

function strCmpWithNumbers( $a, $b) {
  // Split the string in to words.
  $a = explode(' ',$a);
  $b = explode(' ',$b);
  $loop = 0;
  do {
    // Get the first word from each item
    $ta = Utils::gvfa($a, $loop);
    $tb = Utils::gvfa($b, $loop);

    if (isset($ta)) {
      if (isset($tb)) {
        if (is_numeric($ta)) {
          if ($ta != $tb) {
            return $ta - $tb;
          }
        } else {
          $val = strcasecmp($ta, $tb);
          if ($val != 0) {
            return $val;
          }
        }
      } else {
        return 1; // a is set but b isn't
      }
    } else {
      return isset($b);
    }
    $loop +=1;
  } while (true);
}
Anonymous 05-Oct-2015 10:02
An associative array with known keys can be easily custom sorted using a switch statement in the callback:

NB ksort can be used beforehand to ensure expected results

ksort($array);

uksort($array, function ($a) {
    switch($a) {
        case 'pepperoni':
            return 0;
        case 'beef':
            return 1;
        case 'chicken':
            return 2;
        case 'ham':
            return 3;
        case 'vegetarian':
            return 4;
    }
});
PHPUser 07-Feb-2015 02:32
[Editor's note: the following comment may be factually incorrect]

uksort is only usable in the UK
<?php
if($country=="UK"){
  uksort();
}else{
  echo "You have to live in UK to use uksort().";
}
?>
ingo at example dot test 05-May-2014 10:30
sort with collation, to have umlauts correctly:

uksort($retval, array(Collator::create( 'de_DE' ), 'compare'));
mdsky at web dot de 23-May-2010 08:26
Case insensitive without own function:

uksort($array, "strnatcasecmp");
contacto at hardcode dot com dot ar 29-Jul-2009 03:56
need a case insensitive sort by key function? i did and couldn find it, so:

<?php
function insensitive_uksort($a,$b) {
    return strtolower($a)<strtolower($b);
}
uksort($arr, "insensitive_uksort");
?>
brian dot short at gmail dot com 07-Jul-2009 04:58
If you need to periodically sort by grades (A, A+, D-, etc.), here is a compare function that compares strings by the case-insensitive method, unless it finds a grade, in which case it correctly sorts by putting "plus" grades first, unmarked grades second, and "minus" grades last.

<?php
function cmp($a, $b)
{
   $a = preg_replace('@^(a|an|the) @', '', $a);
   $b = preg_replace('@^(a|an|the) @', '', $b);

   //special code for grades
   if (strpos( $a, "+") !== false || strpos( $b, "+") !== false  ||
       strpos( $a, "-") !== false || strpos( $b, "-") !== false ){

        $substrA = substr($a, 0, 1);
        $substrB = substr($b, 0, 1);

        $modifierA = (strlen($a) == 2) ? substr($a, 1, 1) : "";
        $modifierB = (strlen($b) == 2) ? substr($b, 1, 1) : "";

        if ($substrA == $substrB){
            //figure out plusses and minuses.
            if ($modifierA == "+"){
                return -1;
            } else if ($modifierB == "+"){
                return 1;
            }

            if ($modifierA == "-"){
                return 1;
            } else if ($modifierB == '-'){
                return -1;
            }
        } else {
            return strcasecmp($a, $b);
        }

    }
   return strcasecmp($a, $b);
}

$grades = array(
                 "C+" => 13    ,
                "C" => 10     ,
                "D+" => 8     ,
                "B+" => 7     ,
                "C-" => 6     ,
                "A-" => 5     ,
                "F" => 5      ,
                "B" => 4      ,
                "B-" => 4     ,
                "D" => 3      ,
                "D-" => 3     ,
                "A+" => 1
    );

uksort($grades, "cmp");
?>

result:  Array
(
    [A+] => 1
    [A-] => 5
    [B+] => 7
    [B] => 4
    [B-] => 4
    [C+] => 13
    [C] => 10
    [C-] => 6
    [D+] => 8
    [D] => 3
    [D-] => 3
    [F] => 5
)
Jimomighty 20-Mar-2005 04:30
...

function cmp($a, $b)
{
   if ($a == $b) {
       return 0;
   }
   return ($a < $b) ? -1 : 1;
}

function uksort_tree ( &$array )
{
    // [PHP5] foreach ( $array as &$value )
    foreach ( $array as $key => $value )
    {
       if ( is_array ( $value ) )
       {
            // [PHP5] uksort_tree ( $value );
           uksort_tree ( $array[$key] );
       }
    }

    uksort( $array, "cmp" );
}

uksort_tree( $myEntryArray );

...
aleczapka at gmx dot net 22-Dec-2004 01:35
One remark regarding array_sorter class.
It won't work correctly with eg. dates from mysql like 20041206105350, cause you can't convert such number into integer. To fix it remove intval() from the code. If the variable is a number it will work without converting this to int anyways. Here is the fix.

<?php
....
if ($a == $b)
    return 0;
if ($this->sasc)
    return ($a > $b) ? 1 : -1;
else
    return ($a > $b) ? -1 : 1;
...
?>
jg at delegation dot ca 17-Dec-2004 04:41
To sort dates with uksort:

function datediff($a, $b) {

$a = date('U',$a);
$b = date('U',$b);

if ($a == $b) $r = 0;
else $r = ($a > $b) ? 1: -1;

return $r;
}
aleczapka at gmx dot net 06-Dec-2004 01:27
Here is a small and very fast object to handle sorting of multidimentional arrays by a key.
<?php
/**
* Handles multidimentional array sorting by a key (not recursive)
*
* @author Oliwier Ptak <aleczapka at gmx dot net>
*/
class array_sorter
{
    var $skey = false;
    var $sarray = false;
    var $sasc = true;

    /**
    * Constructor
    *
    * @access public
    * @param mixed $array array to sort
    * @param string $key array key to sort by
    * @param boolean $asc sort order (ascending or descending)
    */
    function array_sorter(&$array, $key, $asc=true)
    {
        $this->sarray = $array;
        $this->skey = $key;
        $this->sasc = $asc;
    }

    /**
    * Sort method
    *
    * @access public
    * @param boolean $remap if true reindex the array to rewrite indexes
    */
    function sortit($remap=true)
    {
        $array = &$this->sarray;
        uksort($array, array($this, "_as_cmp"));
        if ($remap)
        {
            $tmp = array();
            while (list($id, $data) = each($array))
                $tmp[] = $data;
            return $tmp;
        }
        return $array;
    }

    /**
    * Custom sort function
    *
    * @access private
    * @param mixed $a an array entry
    * @param mixed $b an array entry
    */
    function _as_cmp($a, $b)
    {
        //since uksort will pass here only indexes get real values from our array
        if (!is_array($a) && !is_array($b))
        {
            $a = $this->sarray[$a][$this->skey];
            $b = $this->sarray[$b][$this->skey];
        }

        //if string - use string comparision
        if (!ctype_digit($a) && !ctype_digit($b))
        {
            if ($this->sasc)
                return strcasecmp($a, $b);
            else
                return strcasecmp($b, $a);
        }
        else
        {
            if (intval($a) == intval($b))
                return 0;

            if ($this->sasc)
                return (intval($a) > intval($b)) ? -1 : 1;
            else
                return (intval($a) > intval($b)) ? 1 : -1;
        }
    }

}//end of class
?>

Sample $input_array:

Array
(
    [0] => Array
        (
            [id] => 961
            [uid] => 29
            [gid] => 12
            [parent_id] => 147
            [created] => 20041206105350
            [modified] => 20041206110702
        )

    [1] => Array
        (
            [id] => 41
            [uid] => 29
            [gid] => 12
            [parent_id] => 153
            [created] => 20041025154009
            [modified] => 20041206105532
        )

    [2] => Array
        (
            [id] => 703
            [uid] => 29
            [gid] => 12
            [parent_id] => 419
            [created] => 20041025154132
            [modified] => 20041027150259
        )

Example of usage:
<?php
    function multi_sort(&$array, $key, $asc=true)
    {
        $sorter = new array_sorter($array, $key, $asc);
        return $sorter->sortit();
    }
    //sort by parent_id in descending order
    $my_array = multi_sort($input_array, "parent_id", false);
?>

The result array will be:
Array
(

    [0] => Array
        (
            [id] => 703
            [uid] => 29
            [gid] => 12
            [parent_id] => 419
            [created] => 20041025154132
            [modified] => 20041027150259
        )

    [1] => Array
        (
            [id] => 41
            [uid] => 29
            [gid] => 12
            [parent_id] => 153
            [created] => 20041025154009
            [modified] => 20041206105532
        )

    [2] => Array
        (
            [id] => 961
            [uid] => 29
            [gid] => 12
            [parent_id] => 147
            [created] => 20041206105350
            [modified] => 20041206110702
        )
fabriceb at gmx dot net 08-Jul-2004 03:26
(about sorting an array of objects by their properties in a class - inspired by webmaster at zeroweb dot org at usort function)
I'm using classes as an abstraction for querying records in a database and use arrays of objects to store records that have an 1 to n relationship. E.g. a class "family" has family members stored as an array of objects. Each of those objects prepresents a record in a database related to the family (by it's familyId).

To identify members, I'm using their memberId as the key of the array e.g. $family->members[$memberId].
To sort the family members AFTER fetching them with the database query, you can use the functions _objSort and sortMembers which will sort the "members" array by key using it's properties (for space reasons I didn't include the methods used to open the records):
<?php
class familyMember
{
    var $memberId;
    var $familyId;
    var $firstName;
    var $age;
    var $hairColor;
// ...
}

class family
{
    var $familyId;
    var $name;
    var $members = array(); // array of familyMember objects
    var $sortFields = array();
    var $sortDirections = array();
    // ...
    function _objSort(&$a, &$b, $i = 0)
    {
        $field        = $this->sortFields[$i];
        $direction    = $this->sortDirections[$i];

        $diff = strnatcmp($this->details[$a]->$field, $this->details[$b]->$field) * $direction;
        if ($diff == 0 && isset($this->sortFields[++$i]))
        {
            $diff = $this->_objSort($a, $b, $i);
        }

        return $diff;
    }

    function sortMembers($sortFields)
    {
        $i = 0;
        foreach ($sortFields as $field => $direction)
        {
            $this->sortFields[$i] = $field;
            $direction == "DESC" ? $this->sortDirections[$i] = -1 : $this->sortDirections[$i] = 1;
            $i++;
        }

        uksort($this->details, array($this, "_objSort"));

        $this->sortFields = array();
        $this->sortDirections = array();
    }
}
// open a family
$familyId = 5;
$family = new family($familyId);
$family->open(); // this will also fetch all members

// sort members by 3 fields
$family->sortMembers(array("firstName" => "ASC", "age" => "DESC", "hairColor" => "ASC"));
// output all family members
foreach ($family->members as $member)
{
    echo $member->firstName." - ".$member->age." - ".$member->hairColor."<br />";
}
?>

Note that this might not be the fastest thing on earth and it hasn't been tested very much yet but I hope it's useful for someone.
webmaster at kik-it at N0SP4M dot com 09-Feb-2004 07:03
The code below allows you to sort an array_A following array_B keys order, original keys and values remain associated.

<?

//main function
Function SortArrayAKeysLikeArrayBKeys(&$TheArrayToSort){
   uksort($TheArrayToSort,"SortArrayAKeysLikeArrayBKeys_cmp");
}

//the custom compare function
Function SortArrayAKeysLikeArrayBKeys_cmp($a,$b){
  global $TheArrayOrder;
  $PosA=KeyPosInArray($a,$TheArrayOrder);
  $PosB=KeyPosInArray($b,$TheArrayOrder);
  if ($PosA==$PosB){return 0;}else{return ($PosA > $PosB ? 1 : -1);}
}

//where is my key in my array
Function KeyPosInArray($Key,$Array){
   $i=0;
   $Pos=99999999;
   if($Array){
      foreach($Array as $K => $V){
         $i++;
         if($K==$Key){
            $Pos=$i;
            break;
         }
      }
   }
   return $Pos;
}

//the array you want to sort
$AnyArrayToSort['age']='19';
$AnyArrayToSort['ville']='rennes';
$AnyArrayToSort['website']='kik-it.com';
$AnyArrayToSort['region']='bretagne';
$AnyArrayToSort['code_postal']='35200';
$AnyArrayToSort['Nom']='Fred';

//the array with the correct keys/values order
$TheArrayOrder['Nom']='Whatever';
$TheArrayOrder['age']='Anything';
$TheArrayOrder['region']='What u want';
$TheArrayOrder['ville']='Something';
$TheArrayOrder['code_postal']='Nothing';

//before sort
print_r($AnyArrayToSort);
echo "<br>";
//we sort
SortArrayAKeysLikeArrayBKeys($AnyArrayToSort);
echo "<br>";
//after sort
print_r($AnyArrayToSort);
?>

Will print :

Array ( [age] => 19 [ville] => rennes [website] => kik-it.com [region] => bretagne [code_postal] => 35200 [Nom] => Fred )

Array ( [Nom] => Fred [age] => 19 [region] => bretagne [ville] => rennes [code_postal] => 35200 [website] => kik-it.com )

The keys not listed in the $TheArrayOrder will appear at the end of your sorted array (only if Key Pos < 99999999 ;o)
guss at typo dot co dot il 07-Dec-2003 09:18
Regarding the recursive sorting function above:
Genrally speaking, any recursion can be reimplemented using simple iteration. in the specific case, using recursion to compare strings has a huge performance impact while a simple loop would suffice and be faster and more simple.
Recursion is only good if it simplifies your code or your understanding of the concept. the previous example does neither, especially as it does a lot of repetitive things in each iteration, such as asigning the character order constant, rebuilding it into an array and such

For example, the string comparison could be written as such :
function str_compare($a,$b) {
    $order="aA??bBcC&#269;&#268;..."; // longer normally & without that html entities
    $default = strlen($a) - strlen($b);
    $minlen = strlen($a) < strlen($b) ? strlen($a) : strlen($b);
    for ($i = 0; $i < $minlen; $i++) {
        $pos_a=strpos($order,$a[$i]);
        $pos_b=strpos($order,$b[$i]);
        if ($pos_a != $pos_b)
            return $pos_a - $pos_b;
    }
    return $default;
}

Which is much simpler and faster.
Note that the above function will break for characters that are not listed in $order. it should be failry trivial to fix it.
ignatius dot reilly at free dot fr 13-Nov-2003 07:59
To use a more complicated comparison function, one can use a callback to a method of an object instance.
For example the following will take an array $arr whose keys are the same as those of $reference, and reorder $arr so that the keys appear in the same order as in $reference.

class kcmp {
    var $reference ;
    function kcmp( $reference ) {
       $this->reference = $reference ;
    }
    function kcompare( $a, $b ) {
       $keys = array_keys( $this->reference ) ;
       $position_a = array_search( $a, $keys ) ;
       $position_b = array_search( $b, $keys ) ;
       return  $position_a < $position_b ? -1 : 1 ;
    }
}

$reference = array(
    "k2" => "a2",
    "k3" => "a3",
    "k1" => "a1"
) ;
$arr = array(
    "k1" => "b1",
    "k2" => "b2",
    "k3" => "b3"
) ;
print_r( $arr ) ;
uksort( $arr, array( new kcmp( $reference ), "kcompare" ) ) ;
print_r( $arr ) ;


사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬

  • ≪ uksort
  • 클래스/객체 ≫
  • PHP Manual
  • 배열 함수 목록
  • 사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬

usort

(PHP 4, PHP 5, PHP 7)

usort — 사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬

설명

bool usort ( array &$array , callback $cmp_function )

이 함수는 사용자 제공 비교 함수를 사용하여 값들에 의해 배열을 정렬할 것이다. 정렬하고자 하는 배열이 흔히 사용되지 않는 방법에 의해 정렬할 필요가 있으면, 이 함수를 사용해야 한다.

Note:

두 멤버가 같은 것으로 비교되면, 정렬한 배열에서 그들의 순서는 정의되지 않습니다.

Note: 이 함수는 array에 새로운 키를 할당합니다. 이 작업은 단순히 키를 재배열할 뿐만 아니라, 이미 할당되어 있는 키를 제거할 수 있습니다.

인수

array

입력 배열.

cmp_function

비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 크다고 판단되면 0 보다 작거나, 같거나, 큰 정수를 반환해야 한다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
4.1.0 | 새 정렬 알고리즘이 도입되었습니다. cmp_function은 같다고 비교되는 원소의 순서를 유지하지 않습니다.

예제

Example #1 usort() 예제

<?php
function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

$a = array(3, 2, 5, 6, 1);

usort ($a, "cmp");

foreach ($a as $key => $value) {
    echo "$key: $value\n";
}
?>

위 예제의 출력:

0: 1
1: 2
2: 3
3: 5
4: 6

Note:

명백히 이런 흔한 경우에는 sort() 함수가 더욱 적절할것이다.

Example #2 다차원 배열을 사용하는 usort() 예제

<?php
function cmp($a, $b)
{
    return strcmp($a["fruit"], $b["fruit"]);
}

$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";

usort($fruits, "cmp");

while (list($key, $value) = each($fruits)) {
    echo "\$fruits[$key]: " . $value["fruit"] . "\n";
}
?>

다차원 배열을 정렬할 때에는, $a와 $b는 배열의 첫번째 인덱스의 참조를 포함한다.

위 예제의 출력:

$fruits[0]: apples
$fruits[1]: grapes
$fruits[2]: lemons

Example #3 객체의 멤버 함수를 사용하는 usort() 예제

<?php
class TestObj {
    var $name;

    function TestObj($name)
    {
        $this->name = $name;
    }

    /* 정적 비교 함수: */
    static function cmp_obj($a, $b)
    {
        $al = strtolower($a->name);
        $bl = strtolower($b->name);
        if ($al == $bl) {
            return 0;
        }
        return ($al > $bl) ? +1 : -1;
    }
}

$a[] = new TestObj("c");
$a[] = new TestObj("b");
$a[] = new TestObj("d");

usort($a, array("TestObj", "cmp_obj"));

foreach ($a as $item) {
    echo $item->name . "\n";
}
?>

위 예제의 출력:

b
c
d

참고

  • uasort() - 사용자 정의 비교 함수로 배열을 정렬하고 인덱스 연관성을 유지
  • uksort() - 사용자 정의 비교 함수를 사용하여 키에 의한 배열 정렬
  • sort() - 배열 정렬
  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • arsort() - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • ksort() - 키에 의한 배열 정렬
  • natsort() - "자연순" 알고리즘으로 배열 정렬
  • rsort() - 역순으로 배열 정렬

User Contributed Notes

Nsisodia91 at gmail dot com 19-Feb-2016 09:12
You can also sort multi-dimensional array for multiple values like as

<?php
$arr = [
    [
        "name"=> "Sally",
        "nick_name"=> "sal",
        "availability"=> "0",
        "is_fav"=> "0"
    ],
    [
        "name"=> "David",
        "nick_name"=> "dav07",
        "availability"=> "0",
        "is_fav"=> "1"
    ],
    [
        "name"=> "Zen",
        "nick_name"=> "zen",
        "availability"=> "1",
        "is_fav"=> "0"
    ],
    [
        "name"=> "Jackson",
        "nick_name"=> "jack",
        "availability"=> "1",
        "is_fav"=> "1"
    ],
    [
        "name"=> "Rohit",
        "nick_name"=> "rod",
        "availability"=> "0",
        "is_fav"=> "0"
    ],

];

usort($arr,function($a,$b){
    $c = $b['is_fav'] - $a['is_fav'];
    $c .= $b['availability'] - $a['availability'];
    $c .= strcmp($a['nick_name'],$b['nick_name']);
    return $c;
});

print_r($arr);
?>

Output:

Array
(
    [0] => Array
        (
            [name] => Jackson
            [nick_name] => jack
            [availability] => 1
            [is_fav] => 1
        )

    [1] => Array
        (
            [name] => David
            [nick_name] => dav07
            [availability] => 0
            [is_fav] => 1
        )

    [2] => Array
        (
            [name] => Zen
            [nick_name] => zen
            [availability] => 1
            [is_fav] => 0
        )

    [3] => Array
        (
            [name] => Rohit
            [nick_name] => rod
            [availability] => 0
            [is_fav] => 0
        )

    [4] => Array
        (
            [name] => Sally
            [nick_name] => sal
            [availability] => 0
            [is_fav] => 0
        )

)
kostya dot develop at gmail dot com 01-Feb-2016 02:26
Now, in php 7, we can use the spaceship operator. For example lest sort array of words from element string length:

<?php
$sampleArray = ['one', 'two', free', 'fore', 'five'];

usort($sampleArray, function($a, $b) {
      return strlen($a) <=> strlen($b);
});
me at evertpot dot com 17-Jul-2015 08:13
I see many people here (including the official examples) write their callbacks rather clumsily like this:

<?php
function($a, $b) {
  if ($a === $b) {
    return 0;
  } elseif ($a < $b) {
    return -1;
  } else {
    return 1;
  }
}
?>

Or if they are a bit more clever:

<?php
function($a, $b) {

  if ($a === $b) {
    return 0;
  } else {
    return $a < $b ? -1 : 1;
  }
}
?>

But this can by simplified further to:

<?php
function($a, $b) {
  return $a - $b;
}
?>
bob at thecaverlys dot net 04-Dec-2014 12:10
Below is a variation on Example #4 that implements a multi-key natural sort on an associative array and can be called in such a way as to allow reversing the sort order and/or making the comparison case insensitive on a key by key basis.

Test code is included in the file - run it from the command line with: php sortUtils.php

<?php
/**
 *    sortUtils.php
*/

/**
 *    mknatsort() - Multi-Key Natural Sort for associative arrays
 *
 *    Uses the usort() function to perform a natural sort on a multi-dimensional
 *    array on multiple keys. Optionally specifying the sort order for each key
 *    and/or ignoring the case for each key value.
 *
 *    @param array $data_array The array to be sorted.
 *    @param mixed $keys The list of keys to be sorted by. This may be a single
 *        key or an array of keys
 *    @param boolean $reverse Specify whether or not to reverse the sort order. If
 *        there are multiple keys then $reverse may be an array of booleans - one
 *        per key.
 *    @param boolean $ignorecase Specify whether or not to ignore the case when
 *        comparing key values.reverse the sort order. If there are multiple keys
 *        then $ignorecase may be an array of booleans - one per key.
*/
function mknatsort ( &$data_array, $keys, $reverse=false, $ignorecase=false ) {
    // make sure $keys is an array
    if (!is_array($keys)) $keys = array($keys);
    usort($data_array, sortcompare($keys, $reverse, $ignorecase) );
}

function sortcompare($keys, $reverse=false, $ignorecase=false) {
    return function ($a, $b) use ($keys, $reverse, $ignorecase) {
        $cnt=0;
        // check each key in the order specified
        foreach ( $keys as $key ) {
            // check the value for ignorecase and do natural compare accordingly
            $ignore = is_array($ignorecase) ? $ignorecase[$cnt] : $ignorecase;
            $result = $ignore ? strnatcasecmp ($a[$key], $b[$key]) : strnatcmp($a[$key], $b[$key]);
            // check the value for reverse and reverse the sort order accordingly
            $revcmp = is_array($reverse) ? $reverse[$cnt] : $reverse;
            $result = $revcmp ? ($result * -1) : $result;
            // the first key that results in a non-zero comparison determines
            // the order of the elements
            if ( $result != 0 ) break;
            $cnt++;
        }
        return $result;
    };
} // end sortcompare()

// test code that will be skipped if this is included in the web environment
// test with: php sortUtils.php from the command line
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) == 'cli') {

    // the following shows several examples of usage and the resulting output.
    $test[] = array('Status'=>'In Progress', 'comment'=>'This looks good.', 'user_name'=>'John Doe', 'date_modified'=>'06/13/2014 11:20 AM', 'role'=>'Senior Account Manager');
    $test[] = array('Status'=>'In Progress', 'comment'=>'This looks good, please approve.', 'user_name'=>'John Doe', 'date_modified'=>'06/13/2014 11:19 AM', 'role'=>'Account Manager');
    $test[] = array('Status'=>'In Progress', 'comment'=>'This looks good.', 'user_name'=>'Jane Smith', 'date_modified'=>'06/13/2014 11:19 AM', 'role'=>'Senior Account Manager');
    $test[] = array('Status'=>'Returned', 'comment'=>'There is not enough informartion.  Please add the following.', 'user_name'=>'John Doe', 'date_modified'=>'06/13/2014 11:15 AM', 'role'=>'Account Manager');
    $test[] = array('Status'=>'In Progress', 'comment'=>'I am currently reviewing this.', 'user_name'=>'John Doe', 'date_modified'=>'06/13/2014 11:14 AM', 'role'=>'Account Manager');
    echo "Original array: ";
    print_r($test);

    echo "Sorted on date_modified: ";
    mknatsort($test, array('date_modified') );
    print_r($test);

    echo "Sorted on role(reversed) & date_modified: ";
    mknatsort($test, array('role','date_modified'),array(true,false) );
    print_r($test);

    echo "Sorted on role & date_modified(reversed): ";
    mknatsort($test, array('role','date_modified'),array(false,true) );
    print_r($test);
}

?>
Gerald Schneider 09-Dec-2013 04:57
usort returns null when the sorting function is not (yet) defined. The function has to be defined before usort is called, afterwards doesn't work.
me at perochak dot com 07-Aug-2013 05:37
If you want to sort the time based array, then this will be helpfull.

function timesort($array){
            usort($array, function($a, $b)
            {
                $a=strtotime($a);
                $b=strtotime($b);

                if ($a == $b)
                {
                    return 0;
                }
                else if ($a < $b)
                {
                    return -1;
                }
                else {
                    return 1;
                }
            });
}

Input:

$time_array=Array
(
    [1] => 10:00AM
    [2] => 09:00AM
    [3] => 02:00PM
    [4] => 11:00AM
)

Output:
print_r(timesort($time_array));
Array
(
    [0] => 09:00AM
    [1] => 10:00AM
    [2] => 11:00AM
    [3] => 02:00PM
)
Hayley Watson 09-Apr-2013 07:30
As the documentation says, the comparison function needs to return an integer that is either "less than, equal to, or greater than zero". There is no requirement to restrict the value returned to -1, 0, 1.

<?php
usort($array, function($a, $b) {
    if($a->integer_property > $b->integer_property) {
        return 1;
    }
    elseif($a->integer_property < $b->integer_property) {
        return -1;
    }
    else {
        return 0;
    }
});
?>

can be simplified to

<?php
usort($array, function($a, $b) {
    return $a->integer_property - $b->integer_property;
});
?>

This of course applies to any comparison function that calculates an integer "score" for each of its arguments to decide which is "greater".
hexarobi at gmail dot com 22-Feb-2013 06:04
Needed to sort an array of objects based on multiple properties. (ie: Group, then Last Name, then First Name) Doing this in multiple usorts fails because the second usort ends up shuffling the results of the previous usort because order is undefined when criteria are equal. Ended up writing a function that builds a comparison operator by nesting ternary operators for each field, then using that function in usort(). Hopefully it's of use to someone else.

<?php
/*
 * Sorts an array of objects by a list of properties.
 * Each property can have the following options:
 *   'property' can be either a public variable or a method().
 *   'order' can be ASC or DESC
 *   'comparer' can be string, number, or date
 *
 * Example:
 * sortObjectsByProperties($objects, array(
 *       array('property'=>'getPrice()', order=>'ASC', 'comparer'=>'number'),
 *       array('property'=>'getName()', order=>'DESC', 'comparer'=>'string'),
 *       array('property'=>'created_date', order=>'ASC', 'comparer'=>'date'),
 *       array('property'=>'id', order=>'ASC', 'comparer'=>'number'),
 *   ));
 *
 * @author Tyler Chamberlain
 */
function sortObjectsByProperties(&$objects, $fields) {
        $comparerFunctionString = "";
        foreach ($fields as $field) {
            if ($field['comparer'] == 'number') {
                $comparerFunction = "(\$a->{$field['property']} == \$b->{$field['property']} ? 0 : (\$a->{$field['property']} > \$b->{$field['property']} ? 1 : -1))";
            } elseif ($field['comparer'] == 'date') {
                $comparerFunction = "(strtotime(\$a->{$field['property']}) > strtotime(\$b->{$field['property']}) ? 1 : -1)";
            } else {
                $comparerFunction = "strcasecmp(\$a->{$field['property']},\$b->{$field['property']})";
            }
            if ($field['order'] === 'DESC') { $comparerFunction = "-".$comparerFunction; }
            $comparerFunctionString .= "$comparerFunction != 0 ? $comparerFunction : (";
        }
        $comparer = "return ($comparerFunctionString 0 )".str_repeat(")", sizeof($fields)).";";
        usort($objects, create_function('$a,$b', $comparer));
}
?>
Jian Wu 07-Jul-2012 06:16
The usort function actually moves $b up when the callback function returns both 0 and 1.  If you want to preserve the original order in the array, return 1 when $a == $b instead of 0.  Try this code below to see how usort alters the order of the original array when all values are the same.

<?php
class TestObj {
    private $name;
    private $id;

    function TestObj($name, $id) {
        $this->name = $name;
        $this->id = $id;
    }

    static function cmp_obj($a, $b) {
        $al = strtolower($a->name);
        $bl = strtolower($b->name);
        if ($al == $bl) {
            return 0; // return 1 if you don't want $b to go in front of $a
        }
        return ($al > $bl) ? +1 : -1;
    }
}

$a[] = new TestObj("a", 1);
$a[] = new TestObj("a", 2);
$a[] = new TestObj("a", 3);

var_dump($a);
usort($a, array("TestObj", "cmp_obj"));
var_dump($a);
?>
zuallauz at gmail dot com 30-Mar-2012 06:56
When trying to do some custom sorting with objects and an anonymous function it wasn't entirely clear how this usort function works. I think it probably uses a quicksort in the background. Basically it actually moves the $b variable up or down in respect to the $a variable. It does NOT move the $a variable inside the callback function. This is key to getting your logic right in the comparisons.

If you return -1 that moves the $b variable down the array, return 1 moves $b up the array and return 0 keeps $b in the same place.

To test I cut down my code to sorting a simple array from highest priority to lowest.

<?php
$priorities = array(5, 8, 3, 7, 3);

usort($priorities, function($a, $b)
{
    if ($a == $b)
    {
        echo "a ($a) is same priority as b ($b), keeping the same\n";
        return 0;
    }
    else if ($a > $b)
    {
        echo "a ($a) is higher priority than b ($b), moving b down array\n";
        return -1;
    }
    else {
        echo "b ($b) is higher priority than a ($a), moving b up array\n";
        return 1;
    }
});

echo "Sorted priorities:\n";
var_dump($priorities);
?>

Output:

b (8) is higher priority than a (3), moving b up array
b (5) is higher priority than a (3), moving b up array
b (7) is higher priority than a (3), moving b up array
a (3) is same priority as b (3), keeping the same
a (8) is higher priority than b (3), moving b down array
b (8) is higher priority than a (7), moving b up array
b (8) is higher priority than a (5), moving b up array
b (8) is higher priority than a (3), moving b up array
a (5) is higher priority than b (3), moving b down array
a (7) is higher priority than b (5), moving b down array

Sorted priorities:
array(5) {
  [0]=> int(8)
  [1]=> int(7)
  [2]=> int(5)
  [3]=> int(3)
  [4]=> int(3)
}
andi_mclean at ntlworld dot com 05-Feb-2012 12:58
I needed a sort method that would sort strings but take note of any numbers and would compare them as number. I also want to ignore any non alphanumerical characters.

Eg.
Slot 1 Example
Slot 10 Example
Slot 2 Example

Should infact be
Slot 1 Example
Slot 2 Example
Slot 10 Example

<?php
function sort_with_numbers($a , $b) {
    $a = explode(' ',$a);
    $b = explode(' ',$b);
    $size = min(count($a), count($b));
    for($index =0; $index < $size; ++$index) {
        $a1 = ereg_replace("[^A-Za-z0-9]", "",$a[$index]);
        $b1 = ereg_replace("[^A-Za-z0-9]", "",$b[$index]);
        $equal = 0;
        if (is_numeric($a1) && is_numeric($b1)) {
            $equal = $a1 - $b1;
        } else {
            $equal = strcasecmp($a1,$b1);
        }
        if ($equal < 0) {
            return -1;
        }
        if ($equal > 0) {
            return 1;
        }
    }
    return count($a) - count($b);
}
?>
admin at gosenz dot com 05-Dec-2011 05:58
sort multi-dimentional arrays in class with usort

<?php
class ArraySort
{
    private $arr = array();

    public function __construct($arr)
    {
        $this->arr = $arr;
    }

    public function doSort()
    {
        $cmp = function($a, $b)
        {
            //Do Some Comparation
        }
        if(is_array($this->arr))
        {
            usort($this->arr, $cmp);
        }
        return $this->arr;
    }
}
?>
Gok6tm 13-Sep-2011 03:09
this is a new multisort function for sorting on multiple subfield like it will be in sql : 'ORDER BY field1, field2'
number of sort field is undefined
<?php

$array[] = array('soc' => 3, 'code'=>1);
$array[] = array('soc' => 2, 'code'=>1);
$array[] = array('soc' => 1, 'code'=>1);
$array[] = array('soc' => 1, 'code'=>1);
$array[] = array('soc' => 2, 'code'=>5);
$array[] = array('soc' => 1, 'code'=>2);
$array[] = array('soc' => 3, 'code'=>2);

//usage
print_r(multiSort($array, 'soc', 'code'));

function multiSort() {
    //get args of the function
    $args = func_get_args();
    $c = count($args);
    if ($c < 2) {
        return false;
    }
    //get the array to sort
    $array = array_splice($args, 0, 1);
    $array = $array[0];
    //sort with an anoymous function using args
    usort($array, function($a, $b) use($args) {

        $i = 0;
        $c = count($args);
        $cmp = 0;
        while($cmp == 0 && $i < $c)
        {
            $cmp = strcmp($a[ $args[ $i ] ], $b[ $args[ $i ] ]);
            $i++;
        }

        return $cmp;

    });

    return $array;

}
?>

output:
Array
(
    [0] => Array
        (
            [soc] => 1
            [code] => 1
        )

    [1] => Array
        (
            [soc] => 1
            [code] => 1
        )

    [2] => Array
        (
            [soc] => 1
            [code] => 2
        )

    [3] => Array
        (
            [soc] => 2
            [code] => 1
        )

    [4] => Array
        (
            [soc] => 2
            [code] => 5
        )

    [5] => Array
        (
            [soc] => 3
            [code] => 1
        )

    [6] => Array
        (
            [soc] => 3
            [code] => 2
        )

)
Lea Hayes 13-Jul-2011 02:55
<?php
/**
 * Sort array of objects by field.
 *
 * @param array $objects Array of objects to sort.
 * @param string $on Name of field.
 * @param string $order (ASC|DESC)
 */
function sort_on_field(&$objects, $on, $order = 'ASC') {
    $comparer = ($order === 'DESC')
        ? "return -strcmp(\$a->{$on},\$b->{$on});"
        : "return strcmp(\$a->{$on},\$b->{$on});";
    usort($objects, create_function('$a,$b', $comparer));
}
?>
bcsj 28-Jun-2011 08:12
Sorting a multidimensional array by a subkey seem to be one of the bigger challenges when using usort. At least if one want the key to be dynamic. For anyone interested this code seemed to work rather well for me:

<?php

usort($array,array(new cmp($key), "cmp__"));

class cmp {
    var $key;
    function __construct($key) {
        $this->key = $key;
    }

    function cmp__($a,$b) {
        $key = $this->key;
        if ($a[$key] == $b[$key]) return 0;
        return (($a[$key] > $b[$key]) ? 1 : -1);
    }
}

?>
masonswolf+ignorethispart at gmail dot com 13-Jun-2011 07:42
[The second version of a function I wrote], just a little cleaner to use, since the usort call is now folded into the custom 'usortByArrayKey' function.

Seems like this is a popular problem - sorting an array of arrays by the keys of sub-arrays, so I'll go ahead and throw in an approach I haven't seen anyone mention yet. It only works since PHP 5.3 because that's when anonymous functions got added to the language.

<?php
function usortByArrayKey(&$array, $key, $asc=SORT_ASC) {
    $sort_flags = array(SORT_ASC, SORT_DESC);
    if(!in_array($asc, $sort_flags)) throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
    $cmp = function(array $a, array $b) use ($key, $asc, $sort_flags) {
        if(!is_array($key)) { //just one key and sort direction
            if(!isset($a[$key]) || !isset($b[$key])) {
                throw new Exception('attempting to sort on non-existent keys');
            }
            if($a[$key] == $b[$key]) return 0;
            return ($asc==SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
        } else { //using multiple keys for sort and sub-sort
            foreach($key as $sub_key => $sub_asc) {
                //array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
                if(!in_array($sub_asc, $sort_flags)) { $sub_key = $sub_asc; $sub_asc = $asc; }
                //just like above, except 'continue' in place of return 0
                if(!isset($a[$sub_key]) || !isset($b[$sub_key])) {
                    throw new Exception('attempting to sort on non-existent keys');
                }
                if($a[$sub_key] == $b[$sub_key]) continue;
                return ($sub_asc==SORT_ASC xor $a[$sub_key] < $b[$sub_key]) ? 1 : -1;
            }
            return 0;
        }
    };
    usort($array, $cmp);
};
?>

Examples:
1) Sort a multi-dimensional array by a single key, from least to greatest:
    usortByArrayKey($resultset, 'order_id');

2) Sort a multi-dimensional array by a single key, from greatest to least:
    usortByArrayKey($resultset, 'purchase_amount', SORT_DESC);

3) Sort a multi-dimensional array first by one key, but then by a different key when the first one is equal, both from least to greatest:
    usortByArrayKey($resultset, array('product_id', 'price'));

4) Sort a multi-dimensional array by two different keys from least to greatest, and then a third from greatest to least if the first two keys map to the equal values:
    usortByArrayKey($resultset, array('product_id', 'customer_id', 'date'=>SORT_DESC));

5) Sort a multi-dimensional array by multiple keys, all from greatest to least:
    usortByArrayKey($resultset, array('product_id', 'price'), SORT_DESC);

One word of caution, since SORT_ASC and SORT_DESC have actual int values of 4 and 3, respectively, if you need to sort a multi-dimensional array by keys 3 or 4, and you're using an array to pass in your keys, you MUST specify your sort flags (e.g. array(3 => SORT_ASC,...)) to prevent unexpected behavior, instead of relying on the function's second parameter as your default sort flag.
fmaz008 at gmail dot com 29-Sep-2010 11:23
This comment is just to help backward compatibility.

I needed to have a foreach loop using reference just before using the usort function on the same array that was used in the foreach.

Like this:
<?php
public function sortByTitle()
{
        if(empty($this->_arrSubCat))
            return false;

        foreach($this->_arrSubCat as &$cat)
            $cat->sortByTitle();

        usort($this->_arrSubCat, 'strcasecmp');
}
?>

I had this strange bug where the last object of the $this->_arrSubCat array before the sort was alway replaced/overwrited with his string value.

On my machine (PHP 5.3), I had no problem, but when I placed the code on another server, my code crashed with a fatal error (Because I was using a string as an object later in the code)

The solution I found is really simple, and avoid the array to have a reference declared in memory while running the sort function. Just add an unset($cat):

<?php
public function sortByTitle()
{
        if(empty($this->_arrSubCat))
            return false;

        foreach($this->_arrSubCat as &$cat)
            $cat->sortByTitle();

        unset($cat); //No more problem because of the foreach &cat reference.
        usort($this->_arrSubCat, 'strcasecmp');
}
?>

It took me an hour an a half to locate the bug, hope it will save someone some time !
phil at lavin dot me dot uk 01-Aug-2010 08:06
strcmp (or strcasecmp) can be used as a callback function for usort. It will cast your comparison values to string. Thus, you can implement a __toString() method into your classes and use usort without creating a callback function:

<?php
class Foo {
   private $a;
   private $b;

   public function __construct($a, $b) {
      $this->a = $a;
      $this->b = $b;
   }

   public function __toString() {
      return (string)$this->a;
   }
}

$foo = array(new Foo('t', 'b'), new Foo('a', 'c'), new Foo('g', 'd'));

print_r($foo);

usort($foo, 'strcasecmp');

print_r($foo);
?>
xaguilars at gmail dot com 06-May-2010 01:03
I'd like to share with the community my function for sorting an array of arrays or objects containing associative data. This could be used, for example, with a MySQL result.

<?php
function sort_by($field, &$arr, $sorting=SORT_ASC, $case_insensitive=true){
    if(is_array($arr) && (count($arr)>0) && ( ( is_array($arr[0]) && isset($arr[0][$field]) ) || ( is_object($arr[0]) && isset($arr[0]->$field) ) ) ){
        if($case_insensitive==true) $strcmp_fn = "strnatcasecmp";
        else $strcmp_fn = "strnatcmp";

        if($sorting==SORT_ASC){
            $fn = create_function('$a,$b', '
                if(is_object($a) && is_object($b)){
                    return '.$strcmp_fn.'($a->'.$field.', $b->'.$field.');
                }else if(is_array($a) && is_array($b)){
                    return '.$strcmp_fn.'($a["'.$field.'"], $b["'.$field.'"]);
                }else return 0;
            ');
        }else{
            $fn = create_function('$a,$b', '
                if(is_object($a) && is_object($b)){
                    return '.$strcmp_fn.'($b->'.$field.', $a->'.$field.');
                }else if(is_array($a) && is_array($b)){
                    return '.$strcmp_fn.'($b["'.$field.'"], $a["'.$field.'"]);
                }else return 0;
            ');
        }
        usort($arr, $fn);
        return true;
    }else{
        return false;
    }
}
?>
nicolas dot demarque at gmail dot com 10-Apr-2010 08:37
Another way to compare object is to have a compare static function in the class with reccursion ability and have globals or static variables who save the state of comparison, and the order.

It could be a good idea to have a variable with comparison functions name (with the sorting : numeric, alphanumeric, inverse).

<?php
interface Comparable{static function compare($a, $b);}
class myObj implements Comparable{
        var $var1; var $var2; var $var3;
        function __construct($v1, $v2, $v3){
                $this->var1=$v1; $this->var2=$v2; $this->var3=$v3;
        }
        public function __toString() {
                return "v1 ".$this->var1." v2 ".$this->var2." v3 ".$this->var3;
        }
        static function compare($a, $b){
                $sort=$GLOBALS['sorts'][$GLOBALS['sort']];
                echo "$sort\n";
                if($a->$sort == $b->$sort){
                        if($GLOBALS['sorts'][$GLOBALS['sort']+1]){
                                $GLOBALS['sort']++;
                                $res=  call_user_func(array('myObj', 'compare'), $a, $b);
                                $GLOBALS['sort']=0; return $res;
                        }
                        $GLOBALS['sort']=0; return 0;
                }
                $GLOBALS['sort']=0;
                if($a->$sort > $b->$sort)
                        return 1;
                return -1;
        }
}

$objects = array(new myObj("1", "2", "3"), new myObj("2", "2", "2"), new myObj("1", "3", "4"));

/* Tests */
$sorts=Array('var1', 'var2', 'var3'); $sort = 0; usort($objects, array('myObj', 'compare')); echo ("var1 var2 var3\n"); foreach($objects as $o)echo $o."\n";
$sorts=Array('var2', 'var3', 'var1'); $sort = 0; usort($objects, array('myObj', 'compare')); echo ("\nvar2 var3 var1\n"); foreach($objects as $o)echo $o."\n";
$sorts=Array('var2', 'var1', 'var3'); $sort = 0; usort($objects, array('myObj', 'compare')); echo ("\nvar2 var1 var3\n"); foreach($objects as $o)echo $o."\n";

?>
Will Shaver 27-Aug-2009 11:31
An even better implementation of osort [than my original, posted on 24-AUG-09 (since deleted)], allowing for multiple properties and directions.  With php 5.3.0 sorting by properties of an object becomes MUCH simpler.  Note that this uses anonymous functions / closures. Might find reviewing the php docs on that useful.  Look below for examples for previous version of php.

<?php
function osort(&$array, $props)
{
    if(!is_array($props))
        $props = array($props => true);

    usort($array, function($a, $b) use ($props) {
        foreach($props as $prop => $ascending)
        {
            if($a->$prop != $b->$prop)
            {
                if($ascending)
                    return $a->$prop > $b->$prop ? 1 : -1;
                else
                    return $b->$prop > $a->$prop ? 1 : -1;
            }
        }
        return -1; //if all props equal
    });
}
?>

Usage:

<?php
osort($items, array("Color" => true, "Size" => false));
//or
osort($items, "Color");
?>
maarten at xel dot nl 17-Jul-2009 05:45
If you ever have to sort a collection of objects on something like a string (name) present in every object, but want to keep the array of objects intact you might find this useful:

<?php
function naturalSortObjectArray($objectArray) {
        function naturalSortByName($a, $b) {
            return strnatcmp($a->name, $b->name);
        }
        usort($objectArray, "naturalSortByName");
        return $objectArray;
}
?>
huskyr at gmail dot com 28-May-2009 03:04
Remember that functions inside of functions will become part of the global scope too. So, something like:

<?php
    function foo($array) {
        function compare($a,$b) {
            return $a < $b;
        }
        usort($array, "compare");
        return $array;
    }

    function bar($array) {
        function compare($a,$b) {
            return $a > $b;
        }
        usort($array, "compare");
        return $array;
    }

    foo($something);
    bar($something_else);
?>

Will result in a fatal error because you're redeclaring 'compare'. This could be a little strange, e.g. for JavaScript programmers who expect functions in functions to become private outside of the parent scope.
gmail.com - nmcquay 31-Mar-2009 09:25
I wrote a wrapper for usort that lets you use something similar to an SQL ORDER BY clause. It can sort arrays of associative arrays and arrays of objects and I think it would work with some hybrid case.

Example of how the function works:

<?php
$testAry = array(
  array('a' => 1, 'b' => 2, 'c' => 3),
  array('a' => 2, 'b' => 1, 'c' => 3),
  array('a' => 3, 'b' => 2, 'c' => 1),
  array('a' => 1, 'b' => 3, 'c' => 2),
  array('a' => 2, 'b' => 3, 'c' => 1),
  array('a' => 3, 'b' => 1, 'c' => 2)
);

Utility::orderBy($testAry, 'a ASC, b DESC');

//Result:
$testAry = array(
  array('a' => 1, 'b' => 3, 'c' => 2),
  array('a' => 1, 'b' => 2, 'c' => 3),
  array('a' => 2, 'b' => 3, 'c' => 1),
  array('a' => 2, 'b' => 1, 'c' => 3),
  array('a' => 3, 'b' => 2, 'c' => 1),
  array('a' => 3, 'b' => 1, 'c' => 2)
);
?>

To sort an array of objects you would do something like:
Utility::orderBy($objectAry, 'getCreationDate() DESC, getSubOrder() ASC');

This would sort an array of objects that have methods getCreationDate() and getSubOrder().

Here is the function:

<?php
class Utility {
    /*
    * @param array $ary the array we want to sort
    * @param string $clause a string specifying how to sort the array similar to SQL ORDER BY clause
    * @param bool $ascending that default sorts fall back to when no direction is specified
    * @return null
    */
    public static function orderBy(&$ary, $clause, $ascending = true) {
        $clause = str_ireplace('order by', '', $clause);
        $clause = preg_replace('/\s+/', ' ', $clause);
        $keys = explode(',', $clause);
        $dirMap = array('desc' => 1, 'asc' => -1);
        $def = $ascending ? -1 : 1;

        $keyAry = array();
        $dirAry = array();
        foreach($keys as $key) {
            $key = explode(' ', trim($key));
            $keyAry[] = trim($key[0]);
            if(isset($key[1])) {
                $dir = strtolower(trim($key[1]));
                $dirAry[] = $dirMap[$dir] ? $dirMap[$dir] : $def;
            } else {
                $dirAry[] = $def;
            }
        }

        $fnBody = '';
        for($i = count($keyAry) - 1; $i >= 0; $i--) {
            $k = $keyAry[$i];
            $t = $dirAry[$i];
            $f = -1 * $t;
            $aStr = '$a[\''.$k.'\']';
            $bStr = '$b[\''.$k.'\']';
            if(strpos($k, '(') !== false) {
                $aStr = '$a->'.$k;
                $bStr = '$b->'.$k;
            }

            if($fnBody == '') {
                $fnBody .= "if({$aStr} == {$bStr}) { return 0; }\n";
                $fnBody .= "return ({$aStr} < {$bStr}) ? {$t} : {$f};\n";
            } else {
                $fnBody = "if({$aStr} == {$bStr}) {\n" . $fnBody;
                $fnBody .= "}\n";
                $fnBody .= "return ({$aStr} < {$bStr}) ? {$t} : {$f};\n";
            }
        }

        if($fnBody) {
            $sortFn = create_function('$a,$b', $fnBody);
            usort($ary, $sortFn);
        }
    }
}
?>
luke dot semerau at gmail dot com 28-Mar-2009 09:25
If you need to use usort with a key in the calling method, I wrote this as a utility:
<?php

function usort_comparison($obj, $method, $key) {
    $usorter = &new Usort($obj, $method, $key);
    return array($usorter, "sort");
}

class Usort {
    function __construct($obj, $method, $key) {
        $this->obj = $obj;
        $this->method = $method;
        $this->key = $key;
    }
    function sort($a, $b) {
        return call_user_func_array(array($this->obj, $this->method), array($a, $b, $this->key));
    }
}

?>

<?php

require_once("util/usort.php");

class Foo {
    $items = array(FooBar(13), FooBar(2));
    public function sorter() {
        usort($this-items, usort_comparison("Foo", "_cmp", "item"));
    }

    public static function _cmp($a, $b, $key) {
         return strcasecmp($a->$key, $b->$key);
    }

}

class FooBar {
    public $item;
    function __construct($val) {
        $this->item = $val;
    }
}

?>

~ simple example... but in the way I need to use it was the key was used in a switch statement to choose the different member of the object to compare against dynamically (as in, sort by x or y or z)
Anonymous 12-Jan-2009 12:14
To sort reliably on more than one "column" in database-style arrays, handle this within your cmp function where the first field to be sorted returns 0:

<?php

function cmp($a, $b){

 if($a[field_1] == $b[field_1]){

  if($a[field_2] == $b[field_2]){

   return 0;

  }

  elseif($a[field_2] > $b[field_2]){

   return -1;

  }

  elseif($a[field_2] < $b[field_2]){

   return 1;

  }

 }

 elseif($a[field_1] > $b[field_1]){

  return -1;

 }

 elseif($a[field_1] < $b[field_1]){

  return 1;

 }

}

?>

Sorting by doing multiple passes does not seem to be reliable.
jalil at measat dot org 29-Dec-2008 02:37
<?php
/*---------------------------------------------------------
* my own home made easy object sort, which
* i cannot find anywhere on the net
* at all, tut. i can find only complicated ones,
* which brings me a headache.
*
* You can enhance it to return all sorts of error
* conditions if you wish,
* i need nothing but a false return upon failure to sort,
* so that is the only error i have here
*
* Example usage:
*
* INPUT                 -       $objects_array
// an array of objects for sorting

* PARAMETERS    -       property
// the property of the object to be sorted

* OUTPUT                -       $engine
// an engine object created by new,
// with your arrays of objects sorted according to your
// selected property
//( the sorting is done upon sort object creation )
*
* Code Examples
*
* $engine = new objSorter($objects_array);
// sorts on FIRST property -  default

* $engine = new objSorter($objects_array,'property');
// sort on ANY specific property
*--------------------------------------------------------*/

class objSorter
{
var $property;
var $sorted;

    function ObjSorter($objects_array,$property=null)
        {
            $sample    = $objects_array[0];
            $vars    = get_object_vars($sample);

        if (isset($property))
            {
            if (isset($sample->$property))
// make sure requested property is correct for the object
                {
                $this->property = $property;
                usort($objects_array, array($this,'_compare'));
                }
            else
                {
                $this->sorted    = false;
                return;
                }
            }
        else
            {
                list($property,$var)     = each($sample);
                $this->property         = $property;
                usort($objects_array, array($this,'_compare'));
            }

        $this->sorted    = ($objects_array);
        }

    function _compare($apple, $orange)
        {
        $property    = $this->property;
        if ($apple->$property == $orange->$property) return 0;
        return ($apple->$property < $orange->$property) ? -1 : 1;
        }
} // end class
?>

USAGE EXAMPLE
DUMP ORIGINAL
user Object ( [name] => Yunihe Jopoba [age] => 27 [richness] => a?￢ 899,970 )
user Object ( [name] => Janirevi Fakejorumoa [age] => 19 [richness] => a?￢ 219,078 )
user Object ( [name] => Korejowi Mezede [age] => 95 [richness] => a?￢ 3,272 )
user Object ( [name] => Nugoaivu Jongyi [age] => 84 [richness] => a?￢ 94,853 )
user Object ( [name] => Cepomase Buaesukoyua [age] => 32 [richness] => a?￢ 677,180 )
user Object ( [name] => Vejosehona Auva [age] => 20 [richness] => a?￢ 144,540 )
user Object ( [name] => Fewame Wojuvuzo [age] => 69 [richness] => a?￢ 198,231 )
user Object ( [name] => Risuku Zagedobu [age] => 48 [richness] => a?￢ 763,860 )
user Object ( [name] => Fazurada Aayu [age] => 16 [richness] => a?￢ 302,352 )

SORTED by age

<?php
$objects                = new ObjSorter($objects_array,'age');
if ($objects->sorted)
{
$objects_array  = $objects->sorted;
foreach ($objects_array as $key=>$object) { print_r($object); echo "<br />"; }
}
?>

user Object ( [name] => Fazurada Aayu [age] => 16 [richness] => a?￢ 302,352 )
user Object ( [name] => Janirevi Fakejorumoa [age] => 19 [richness] => a?￢ 219,078 )
user Object ( [name] => Vejosehona Auva [age] => 20 [richness] => a?￢ 144,540 )
user Object ( [name] => Yunihe Jopoba [age] => 27 [richness] => a?￢ 899,970 )
user Object ( [name] => Cepomase Buaesukoyua [age] => 32 [richness] => a?￢ 677,180 )
user Object ( [name] => Risuku Zagedobu [age] => 48 [richness] => a?￢ 763,860 )
user Object ( [name] => Fewame Wojuvuzo [age] => 69 [richness] => a?￢ 198,231 )
user Object ( [name] => Nugoaivu Jongyi [age] => 84 [richness] => a?￢ 94,853 )
user Object ( [name] => Korejowi Mezede [age] => 95 [richness] => a?￢ 3,272 )

SORTED by richness

<?php
$objects                = new ObjSorter($objects_array,'richness');
if ($objects->sorted)
{
$objects_array  = $objects->sorted;
foreach ($objects_array as $key=>$object) { print_r($object); echo "<br />"; }
}
?>

user Object ( [name] => Vejosehona Auva [age] => 20 [richness] => a?￢ 144,540 )
user Object ( [name] => Fewame Wojuvuzo [age] => 69 [richness] => a?￢ 198,231 )
user Object ( [name] => Janirevi Fakejorumoa [age] => 19 [richness] => a?￢ 219,078 )
user Object ( [name] => Korejowi Mezede [age] => 95 [richness] => a?￢ 3,272 )
user Object ( [name] => Fazurada Aayu [age] => 16 [richness] => a?￢ 302,352 )
user Object ( [name] => Cepomase Buaesukoyua [age] => 32 [richness] => a?￢ 677,180 )
user Object ( [name] => Risuku Zagedobu [age] => 48 [richness] => a?￢ 763,860 )
user Object ( [name] => Yunihe Jopoba [age] => 27 [richness] => a?￢ 899,970 )
user Object ( [name] => Nugoaivu Jongyi [age] => 84 [richness] => a?￢ 94,853 )

Simulated wrong property request ( dumb property )

<?php
$objects                = new ObjSorter($objects_array,'dumb property');
if ($objects->sorted)
{
$objects_array  = $objects->sorted;
foreach ($objects_array as $key=>$object) { print_r($object); echo "<br />"; }
}
else echo 'DUMB ERROR';
?>

DUMB ERROR
drorasta 22-Jun-2008 07:16
Here's how you can usort() a multidimensional array (like in example #2 above) within a class:

<?php usort($myArray, array("className", "cmp")); ?>
Ariz Jacinto 13-May-2008 07:50
Here's how to use usort() within a class using a static function as a callback (using the example above):

<?php

class MyObj {
    var $name;

    function SampleObj($name){
        $this->name = $name;

        usort($this->name, array(&$this, "compareValues"));

    }

    static function compareValues($a, $b){
        $al = strtolower($a->name);
        $bl = strtolower($b->name);
        if ($al == $bl) {
            return 0;
        }
        return ($al > $bl) ? +1 : -1;
    }
}

?>
Sid_M 06-Nov-2007 02:54
The sample function for having the sort function be a class method has a comment identifying the sorting function as static, however the function does not have the static keyword. In PHP 5 (at least as configured on my server), usort will fail, identifying the method as non-static. Preceding the function keyword with the static keyword fixed the problem.
Tim 13-Nov-2006 12:43
If you want to sort an array of strings by the length of its values, you can write a better cmp-function than manually comparing string lengths with if-statements. Unfortunately, I see this bad technique in other people's code. I prefer using this:

<?php
function length_cmp( $a, $b ) {
    return strlen($a)-strlen($b) ;
}
?>

This function sorts the array in ascending order. If you want to sort descending, simply swap $a and $b either in the parameters or in the subtraction.

-Tim
svenxy AT nospam gmx net 16-Oct-2006 10:58
This will sort subnets correctly:

<?php

$zones =
array('192.168.11',
'192.169.12',
'192.168.13',
'192.167.14',
'192.168.15',
'122.168.16',
'192.168.17'
);

function sort_subnets ($a, $b) {
    $a_arr = explode('.', $a);
    $b_arr = explode('.', $b);
    foreach (range(0,3) as $i) {
        if ( $a_arr[$i] < $b_arr[$i] ) {
            return -1;
        }
        elseif ( $a_arr[$i] > $b_arr[$i] ) {
            return 1;
        }
    }
    return -1;
}

usort($zones, 'sort_subnets');
print '<pre>';
print_r($zones);
print '</pre>';

?>
stephen in koto 06-Apr-2006 07:28
For using usort inside a method in an object, where the callback sort method is in the same object, this works:

<?php usort($arr_to_sort, array($this, "sort_terms_by_criteria")); ?>

If you wish to keep elements in their original or reverse order if they are the same length, just don't return zero for items that compare the same. Return a 1 or -1, as appropriate.
sydney at totoche dot org 16-Jan-2006 10:44
Instead of doing  :

<?php $strc = strcmp( strtolower($a[$f]), strtolower($b[$f]) ); ?>

you could do this :

<?php $strc = strcasecmp( $a[$f], $b[$f] ); ?>

which is more efficient and is does case insensitive comparison according to the current locale.
sean at NOSPAM dot orwd dot com 23-Sep-2005 11:46
In order to get a proper text sorting I have changed the arfsort() function submitted by jonathan(at)inetz(dot)com.

The one line I have changed is:

<?php
$strc = strcmp( $a[$f], $b[$f] );
?>

to

<?php
$strc = strcmp( strtolower($a[$f]), strtolower($b[$f]) );
?>

It can now sort correctly for alphabetization.  Capital letters where getting in the way.

Sean
Jeremy Swinborne 20-Jul-2005 10:56
When I query a DB I usually put my record set inside of a multi-dimentional array.  I finally wrote a program that will allow you to sort your record set by column after you put it in an array.

<?php
$test = array();
$test[0]['name'] = 'jeremy';
$test[0]['email'] = 'lala@example.com';
$test[0]['phone'] = '123-123-1234';
$test[0]['trick'] = 'mezopia';

$test[1]['name'] = 'Amanda';
$test[1]['email'] = 'hot@example.com';
$test[1]['phone'] = '123-123-1235';
$test[1]['trick'] = 'youarecool';

$test[2]['name'] = 'john';
$test[2]['email'] = 'wowee@example.com';
$test[2]['phone'] = '123-123-3333';
$test[2]['trick'] = 'goneinanhour';

print_r(columnSort($test, 'name'));

function columnSort($unsorted, $column) {
    $sorted = $unsorted;
    for ($i=0; $i < sizeof($sorted)-1; $i++) {
      for ($j=0; $j<sizeof($sorted)-1-$i; $j++)
        if ($sorted[$j][$column] > $sorted[$j+1][$column]) {
          $tmp = $sorted[$j];
          $sorted[$j] = $sorted[$j+1];
          $sorted[$j+1] = $tmp;
      }
    }
    return $sorted;
}
?>
dmhouse at gmail dot com 19-Jul-2005 11:09
The easiest way to compare two integers is just to take the second away from the first. For example, say you wanted to sort by an integer property of an object. Your comparison function would look like this:

<?php
function compare_counts($a, $b) {
  return $a->count - $b->count;
}
?>

This works because you don't necessarily have to return -1, 0 or 1, the manual states any integer less than, equal to or greater than 0 is OK.
guenther dot stadler at gmx dot net 30-Mar-2005 11:25
Just something i stumbled about right now:
If the array consists of just one elemtent, the user-function is not called at all.

Although this surely makes sense, think of it if you want to use your order-function for adding extra data to your arrays...
Raveler at telenet dot be 20-May-2004 07:02
The array_alternate_multisort function written by robert below doesn't work. There are several bugs in the code and it doesn't work when sorting by multiple keys because the order of the first key isn't taken into account when sorting by the second key and so on. Also, because robert uses strcasecmp the algorithm doesn't work properly with floats or other variable types. Here's the improved version:

<?php
    function SortArray() {
        $arguments = func_get_args();
        $array = $arguments[0];
        $code = '';
        for ($c = 1; $c < count($arguments); $c += 2) {
            if (in_array($arguments[$c + 1], array("ASC", "DESC"))) {
                $code .= 'if ($a["'.$arguments[$c].'"] != $b["'.$arguments[$c].'"]) {';
                if ($arguments[$c + 1] == "ASC") {
                    $code .= 'return ($a["'.$arguments[$c].'"] < $b["'.$arguments[$c].'"] ? -1 : 1); }';
                }
                else {
                    $code .= 'return ($a["'.$arguments[$c].'"] < $b["'.$arguments[$c].'"] ? 1 : -1); }';
                }
            }
        }
        $code .= 'return 0;';
        $compare = create_function('$a,$b', $code);
        usort($array, $compare);
        return $array;
    }
?>
todor at todor dot net 08-Mar-2004 01:39
To sort multidimentional arrays .... by one key.

<?php
function multi_sort($tab,$key){
    $compare = create_function('$a,$b','if ($a["'.$key.'"] == $b["'.$key.'"]) {return 0;}else {return ($a["'.$key.'"] > $b["'.$key.'"]) ? -1 : 1;}');
    usort($tab,$compare) ;
    return $tab ;
}
?>
arjini at mac dot com 27-Jan-2004 07:05
<?php
/*
This is a class I built to sort parent/child relationships of array elements.
I post this here, because thanks to usort, you can also assign an explicit order
to the elements.

I first built this to sort categories and subcategories.

My database has four fields. category_id, parent_id, category_name, category_position

$rows is an associative array from the database with my categories.
*/
$rows = array( 0=>array('category_id'=>1,'parent_id' =>3,'category_name' =>'Second Child','category_position'=>2),
                    1=>array('category_id' =>2,'parent_id' =>0,'category_name' =>'Second Parent','category_position'=>2),
                    2=>array('category_id' =>3,'parent_id' =>0,'category_name' =>'First Parent','category_position'=>1),
                    3=>array('category_id' =>4,'parent_id' =>0,'category_name' =>'Third Parent','category_position'=>3),
                    4=>array('category_id' =>5,'parent_id' =>3,'category_name' =>'First Child','category_position'=>1),
                    5=>array('category_id' =>6,'parent_id' =>5,'category_name'=>'Second Sub-Child','category_position'=>2),
                    6=>array('category_id' =>7,'parent_id' =>5,'category_name' =>'First Sub-Child','category_position'=>1)
                    );

$ordered = chain('category_id', 'parent_id', 'category_position', $rows);

foreach($ordered as $item)
{
    echo str_repeat('------', $item['indent']).$item['category_name'].'<br>';
}

/*

Output

First Parent
------First Child
------------First Sub-Child
------------Second Sub-Child
------Second Child
Second Parent
Third Parent

You can have it only return a certain "branch" if you specify a root_id.
Aditionally, you can keep it by going to deep by specifying max-level.

Hope someone finds this useful, I sure would have liked to see this here a week ago.

Ah yes... and the code that runs it.
*/

function chain($primary_field, $parent_field, $sort_field, $rows, $root_id=0, $maxlevel=25)
{
    $c = new chain($primary_field, $parent_field, $sort_field, $rows, $root_id, $maxlevel);
    return $c->chain_table;
}

class chain
{
    var $table;
    var $rows;
    var $chain_table;
    var $primary_field;
    var $parent_field;
    var $sort_field;

    function chain($primary_field, $parent_field, $sort_field, $rows, $root_id, $maxlevel)
    {
        $this->rows = $rows;
        $this->primary_field = $primary_field;
        $this->parent_field = $parent_field;
        $this->sort_field = $sort_field;
        $this->buildChain($root_id,$maxlevel);
    }

    function buildChain($rootcatid,$maxlevel)
    {
        foreach($this->rows as $row)
        {
            $this->table[$row[$this->parent_field]][ $row[$this->primary_field]] = $row;
        }
        $this->makeBranch($rootcatid,0,$maxlevel);
    }

    function makeBranch($parent_id,$level,$maxlevel)
    {
        $rows=$this->table[$parent_id];
        foreach($rows as $key=>$value)
        {
            $rows[$key]['key'] = $this->sort_field;
        }

        usort($rows,'chainCMP');
        foreach($rows as $item)
        {
            $item['indent'] = $level;
            $this->chain_table[] = $item;
            if((isset($this->table[$item[$this->primary_field]])) && (($maxlevel>$level+1) || ($maxlevel==0)))
            {
                $this->makeBranch($item[$this->primary_field], $level+1, $maxlevel);
            }
        }
    }
}

function chainCMP($a,$b)
{
    if($a[$a['key']] == $b[$b['key']])
    {
        return 0;
    }
    return($a[$a['key']]<$b[$b['key']])?-1:1;
}

?>
sreid at sea-to-sky dot net 08-Jan-2004 05:22
As the manual says, "If two members compare as equal, their order in the sorted array is undefined." This means that the sort used is not "stable" and may change the order of elements that compare equal.

Sometimes you really do need a stable sort. For example, if you sort a list by one field, then sort it again by another field, but don't want to lose the ordering from the previous field. In that case it is better to use usort with a comparison function that takes both fields into account, but if you can't do that then use the function below. It is a merge sort, which is guaranteed O(n*log(n)) complexity, which means it stays reasonably fast even when you use larger lists (unlike bubblesort and insertion sort, which are O(n^2)).

<?php
function mergesort(&$array, $cmp_function = 'strcmp') {
    // Arrays of size < 2 require no action.
    if (count($array) < 2) return;
    // Split the array in half
    $halfway = count($array) / 2;
    $array1 = array_slice($array, 0, $halfway);
    $array2 = array_slice($array, $halfway);
    // Recurse to sort the two halves
    mergesort($array1, $cmp_function);
    mergesort($array2, $cmp_function);
    // If all of $array1 is <= all of $array2, just append them.
    if (call_user_func($cmp_function, end($array1), $array2[0]) < 1) {
        $array = array_merge($array1, $array2);
        return;
    }
    // Merge the two sorted arrays into a single sorted array
    $array = array();
    $ptr1 = $ptr2 = 0;
    while ($ptr1 < count($array1) && $ptr2 < count($array2)) {
        if (call_user_func($cmp_function, $array1[$ptr1], $array2[$ptr2]) < 1) {
            $array[] = $array1[$ptr1++];
        }
        else {
            $array[] = $array2[$ptr2++];
        }
    }
    // Merge the remainder
    while ($ptr1 < count($array1)) $array[] = $array1[$ptr1++];
    while ($ptr2 < count($array2)) $array[] = $array2[$ptr2++];
    return;
}
?>
webmaster at zeroweb dot org 20-Dec-2003 12:05
Needed a quick, fairly uncluttered way of sorting an array of objects by a certain object attribute, so here's what I came up with. Uses one global array and usort(). Also accepts an optional sort direction argument (CSORT_ASC = sort ascending, CSORT_DESC = sort descending). Use it like this:

(assuming $myarray is your array, "index" is the attribute you want to sort by, and you want to sort in descending order)

      csort($myarray, "index", CSORT_DESC);

Hope this is of use to someone. Probably a better way to pull this off, but this works alright.

<?php
define("CSORT_ASC",     1);
define("CSORT_DESC",     -1);

function csort_cmp(&$a, &$b)
{
    global $csort_cmp;

    if ($a->$csort_cmp['key'] > $b->$csort_cmp['key'])
        return $csort_cmp['direction'];

    if ($a->$csort_cmp['key'] < $b->$csort_cmp['key'])
        return -1 * $csort_cmp['direction'];

    return 0;
}

function csort(&$a, $k, $sort_direction=CSORT_ASC)
{
    global $csort_cmp;

    $csort_cmp = array(
        'key'           => $k,
        'direction'     => $sort_direction
    );

    usort($a, "csort_cmp");

    unset($csort_cmp);
}
?>
skrebbel at operamail dot com 27-Mar-2003 10:59
here's another recursive multisort, however a clean and fast one that is class-based (but works as well outside classes) and does not uglify your global namespace at all. note that strnatcmp is used, but one could use something else of course.

btw, for arrays in which the rows/columns are 'swapped', use array_multisort().

<?php
class Banana {
    var $aData;//the array we want to sort.
    var $aSortkeys;//the order in which we want the array to be sorted.
    function _sortcmp($a, $b, $i=0) {
        $r = strnatcmp($a[$this->aSortkeys[$i]],$b[$this->aSortkeys[$i]]);
        if($r==0) {
            $i++;
            if ($this->aSortkeys[$i]) $r = $this->_sortcmp($a, $b, $i+1);
        }
        return $r;
    }
    function sort() {
        if(count($this->aSortkeys)) {
            usort($this->aData,array($this,"_sortcmp"));
        }
    }
}
$B = new Banana;
$B->aData = array(
    array("name" => "hank", "headsize" => "big", "age" => 32),
    array("name" => "frank", "headsize" => "huge", "age" => 36)
);
$B->aSortkeys = array("age","name");
$B->sort();

?>
sorry for the ugly indenting, but i couldn't get it any better in this note adder thing.
steveNO_SPAM at AXE_MEcontentwatch dot com 31-Dec-2002 11:11
The sort functions do nothing if identical keys exist.  Of course, you shouldn't have identical keys anyway, but this just might save someone else the oodles of time it took me to figure out while using multi-dimentional arrays:

<?php
class myArticleList {
// ... //
  function &getList () {

    // works
        $a = array (
                   "articleList1" => array ( "2", "3" ),
                   "articleList2" => array ( "3" , "4")
                 );
    usort( $a, array( $this, "compareByTitle") );

    // doesn't work
        $b = array (
                   "articleList" => array ( "2", "3" ),
                   "articleList" => array ( "3" , "4")
                 );
    usort( $b, array( $this, "compareByTitle") );
  }

  function compareByTitle( $a, $b ) {
  // ... //
  }
}
?>
jfren484 AT hotmail DOT com 25-Oct-2002 01:37
If you've used ADO before, you may have used the Sort property on a recordset.  It's very powerful - you can add a nice ORDER BY clause after getting the recordset from the database.  It's especially helpful if you want to show a list on a web page and make the column headings links that cause the list to resort on that column.

I wanted to do the same thing with mysql recordsets, but it looks like you have to have the ORDER BY in your query.  I re-worked the example from johan_land at yahoo dot com above for sorting multidimensional arrays.  When I get a mysql recordset, I create an array with all of the records like this:

<?php
$aaRows[]    = array();
if (mysql_num_rows($rsRows) > 0)
    while ($aRow = mysql_fetch_array($rsRows, MYSQL_ASSOC))
        $aaRows[]    = $aRow;
?>

At this point $aaRows is an array of arrays - it's a numeric-indexed array containing records from the recordset, which themselves are associative arrays.  The following code takes the array of records as the first parameter, and an array of fields to sort - each field is an associative array, where 'name' is the db field name, and 'dir' is the direction to sort.  If dir is 'DESC' (case-insensitive), it will sort descending.  Any other value (including not setting one) will cause it to sort ascending.

<?php
function sortRows(&$a_aaRows, $a_aaSortCriteria)
{
    GLOBAL    $g_aaSortArray;

    function compare($a_aRow1, $a_aRow2, $a_lField = 0)
    {
        GLOBAL    $g_aaSortArray;

        $lCompareVal    = 0;

        if ($a_lField < count($g_aaSortArray))
        {
            $sSortFieldName    = $g_aaSortArray[$a_lField]['name'];
            $sSortFieldDir    = $g_aaSortArray[$a_lField]['dir'];

            $vValue1    = eval('return $a_aRow1[' . $sSortFieldName . '];');
            $vValue2    = eval('return $a_aRow2[' . $sSortFieldName . '];');

            if ($vValue1 == $vValue2)
                $lCompareVal    = compare($a_aRow1, $a_aRow2, $a_lField + 1);
            else
            {
                $lCompareVal    =    $vValue1 > $vValue2 ? 1 : -1;
                if (strtolower(substr($sSortFieldDir, 0, 4)) == 'desc')
                    $lCompareVal    = -$lCompareVal;
            }
        }

        return $lCompareVal;
    }

    $g_aaSortArray    = $a_aaSortCriteria;
    usort($a_aaRows, 'compare');
}
?>

When I call it it looks something like this:

<?php
sortRows($aaRows, array(array('name' => 'STATE', 'dir' => 'ASC'), array('name' => 'CITY', 'dir' => 'DESC')));
?>
mkr at binarywerks dot dk 20-Sep-2002 06:29
If you want to sort an array according to another array acting as a priority list, you can use this function.

<?php
function listcmp($a, $b)
{
  global $order;

  foreach($order as $key => $value)
    {
      if($a==$value)
        {
          return 0;
          break;
        }

      if($b==$value)
        {
          return 1;
          break;
        }
    }
}

$order[0] = "first";
$order[1] = "second";
$order[2] = "third";

$array[0] = "second";
$array[1] = "first";
$array[2] = "third";
$array[3] = "fourth";
$array[4] = "second";
$array[5] = "first";
$array[6] = "second";

usort($array, "listcmp");

print_r($array);
?>
prozac at iguanasoft dot com 15-Jul-2002 02:55
Here is a simple example of converting a timestamp date("U") into a date. This sorts by that day and then by string value alphabetically.

I hope it saves someone some time... Happy PHP'in!

<?php
//data to sort
$shared[0]["page_id"] = "2025731470";
$shared[1]["page_id"] = "2025731450";
$shared[2]["page_id"] = "1025731456";
$shared[3]["page_id"] = "1025731460";
$shared[0]["username"] = "larry";
$shared[1]["username"] = "alvin";
$shared[2]["username"] = "garth";
$shared[3]["username"] = "harvy";

//function to convert timestamp to date
function convert_timestamp($timestamp){
    $limit=date("U");
    $limiting=$timestamp-$limit;
    return date ("Ymd", mktime (0,0,$limiting));
}
//comparison function
function cmp ($a, $b) {
    $l=convert_timestamp($a["page_id"]);
    $k=convert_timestamp($b["page_id"]);
    if($k==$l){
        return strcmp($a["username"], $b["username"]);
    }else{
        return strcmp($k, $l);
    }
}

//sort array
usort($shared, "cmp");

//display sorted info
while (list ($key, $value) = each ($shared)) {
    echo "\$shared[$key]: ";
    echo $value["page_id"];
    echo " username: ";
    echo $value["username"];
    echo "<break_tag>";
}
?>

This will output:
$shared[0]: 2025731450   username: alvin
$shared[1]: 2025731470   username: larry
$shared[2]: 1025731456   username: garth
$shared[3]: 1025731460   username: harvy
alex at netflex dot nl 02-Jul-2002 01:10
This function will sort on more then one values, test and have fun

<pre>
<?php

  $array[0]['name'] = "a";
  $array[0]['id'] = 3;

  $array[1]['name'] = "a";
  $array[1]['id'] = 2;

  $array[2]['name'] = "a";
  $array[2]['id'] = 5;

  $array[3]['name'] = "b";
  $array[3]['id'] = 8;

  $array[4]['name'] = "b";
  $array[4]['id'] = 1;

  $array[5]['name'] = "b";
  $array[5]['id'] = 0;

  $array[6]['name'] = "c";
  $array[6]['id'] = 5;

  $array[7]['name'] = "c";
  $array[7]['id'] = 7;

  $array[8]['name'] = "c";
  $array[8]['id'] = 3;

  print_r($array);

  $sort_array[0]['name'] = "name";
  $sort_array[0]['sort'] = "ASC";
  $sort_array[0]['case'] = TRUE;

  $sort_array[1]['name'] = "id";
  $sort_array[1]['case'] = FALSE;

  sortx($array, $sort_array);

  print_r($array);

  function sortx(&$array, $sort = array()) {
    $function = '';
    while (list($key) = each($sort)) {
      if (isset($sort[$key]['case'])&&($sort[$key]['case'] == TRUE)) {
        $function .= 'if (strtolower($a["' . $sort[$key]['name'] . '"])<>strtolower($b["' . $sort[$key]['name'] . '"])) { return (strtolower($a["' . $sort[$key]['name'] . '"]) ';
      } else {
        $function .= 'if ($a["' . $sort[$key]['name'] . '"]<>$b["' . $sort[$key]['name'] . '"]) { return ($a["' . $sort[$key]['name'] . '"] ';
      }
      if (isset($sort[$key]['sort'])&&($sort[$key]['sort'] == "DESC")) {
        $function .= '<';
      } else {
        $function .= '>';
      }
      if (isset($sort[$key]['case'])&&($sort[$key]['case'] == TRUE)) {
        $function .= ' strtolower($b["' . $sort[$key]['name'] . '"])) ? 1 : -1; } else';
      } else {
        $function .= ' $b["' . $sort[$key]['name'] . '"]) ? 1 : -1; } else';
      }
    }
    $function .= ' { return 0; }';
    usort($array, create_function('$a, $b', $function));
  }

?>
</pre>
graham at irwin dot org 16-Jan-2002 05:28
Example 3 above does not work with 4.0.4pl1, unless you write the uasort line as follows:
uasort($a, array ($a[0], "cmp_obj"));

(I assume any instance of the object: a[0] or a[1] or a[2] would work as well)

It does not work at all with associative arrays of objects.
xnoguer at rezebra dot com 25-Sep-2001 08:48
just for debugging purposes, usort does not complain if its argument function is not defined, i.e.:

<?php usort($my_array,"non existant function"); ?>

will not do anything...
josh at wintelcom dot net 25-Aug-2001 03:58
This lets you sort an associative multi-dimensional array by multiple key/field names.  Much similiar to the SQL clause ORDER BY.  Enjoy.

<?php
function cmp ($a, $b) {

    // Populate this array with your values...
    // Below is the SQL equivalent of
    // select * from blah ORDER BY date desc, type asc, name asc

    $vals = array(
        'date' => 'd',
        'type' => 'a',
        'name' => 'a'
    );

    while(list($key, $val) = each($vals)) {

        if($val == "d") {
            if ($a["$key"] > $b["$key"]) {
                return -1;
            }
            if ($a["$key"] < $b["$key"]) {
                return 1;
            }
        }

        if($val == "a") {
            if ($a["$key"] < $b["$key"]) {
                return -1;
            }
            if($a["$key"] > $b["$key"]) {
                return 1;
            }
        }

    }

}
?>
franky at iname dot com 18-Jul-2001 12:08
For sort multi-array by specific index

<?php
function cmp ($a, $b)
{
 global $w_o;
 if ($a[$w_o] == $b[$w_o]) return 0;
 return ($a[$w_o] < $b[$w_o]) ? -1 : 1;
}
# the index is the second element of
# each row

$w_o =1;
usort($my_arry_info,"cmp");
?>
johan_land at yahoo dot com 29-Jun-2001 02:23
These functions will sort an array by more than one element. The elements to sort by is specified in $sortArr. If you eg. want to sort by $destArr[]["sortBy2"][0] you add '["sortBy2"][0]' to $sortArr. Use it as much as you want!! If you modify it, please let me know...

<?php
//The functions
function compare($a, $b) {
    return cmpRec(0, $a, $b);
}

function cmpRec($i, $a, $b) {
    global $sortArr;
    if($i == sizeof($sortArr)) {
        return 0;
    }
    $avalue = '$avalue = $a'.$sortArr[$i].';';
    $bvalue = '$bvalue = $b'.$sortArr[$i].';';
    eval($avalue);
    eval($bvalue);
        if($avalue == $bvalue) {
        return cmpRec($i+1, $a, $b);
    } else {
        return ($avalue > $bvalue) ? (1) : (-1);
    }
}

//For testing the functions
$destArr = array( array( "sortBy1" => 2, "sortBy2" => array( "Fish", "eat seafood" )), array( "sortBy1" => 1, "sortBy2" => array( "Lions", "eat everything" )), array( "sortBy1" => 1, "sortBy2" => array( "Bird", "eat birdfood" )) );
$sortArr = array( '["sortBy1"]', '["sortBy2"][0]' );
print_r($destArr);
usort($destArr, "compare");
reset($destArr);
print_r($destArr);
?>
bo at erichsen dot com 20-Mar-2001 02:16
when using usort to refer to a function inside a class i have succesfully used:

<?php usort($myarray,array($this,"cmp")); ?>
derek at luddite dot net 19-Sep-2000 08:35
Needed a date sort and I didn't know if one was available so I wrote one. Maybe it'll help someone:

<?php
function DateSort($a,$b,$d="-") {
    if ($a == $b) {
        return 0;
    } else {  //Convert into dates and compare
        list($am,$ad,$ay)=split($d,$a);
        list($bm,$bd,$by)=split($d,$b);
        if (mktime(0,0,0,$am,$ad,$ay) < mktime(0,0,0,$bm,$bd,$by)) {
            return -1;
        } else {
            return 1;
        }
    }
}
?>

$d is the delimeter


클래스/객체 정보

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
  • 클래스/객체 함수 목록
    • __autoload — Attempt to load undefined class
    • call_user_method_array — 인수 배열로 사용자 메소드를 호출 [배제됨]
    • call_user_method — 특정 객체의 사용자 메쏘드를 호출 [배제됨]
    • class_alias — Creates an alias for a class
    • class_exists — 클래스가 정의되었는지 확인
    • get_called_class — "Late Static Binding" 클래스명
    • get_class_methods — 클래스 메쏘드명을 얻습니다
    • get_class_vars — 클래스의 기본 프로퍼티를 얻습니다
    • get_class — 객체의 클래스명을 반환
    • get_declared_classes — 선언된 클래스명을 배열로 반환
    • get_declared_interfaces — 선언된 모든 인터페이스의 배열을 반환
    • get_declared_traits — Returns an array of all declared traits
    • get_object_vars — 주어진 객체의 프로퍼티를 얻습니다
    • get_parent_class — 객체나 클래스의 부모 클래스명을 얻습니다
    • interface_exists — 인터페이스가 정의되었는지 확인
    • is_a — 객체가 이 클래스나 부모 클래스 중 하나인지 확인
    • is_subclass_of — 이 클래스가 부모 클래스의 자식인지 확인
    • method_exists — 클래스 메쏘드가 존재하는지 확인
    • property_exists — 객체나 클래스가 프로퍼티를 가졌는지 확인
    • trait_exists — Checks if the trait exists

User Contributed Notes

There are no user contributed notes for this page.


소개

이 함수들은 클래스와 인스턴스 객체에 대한 정보를 얻을 수 있게 합니다. 객체의 클래스명은 물론, 멤버 프로퍼티와 메쏘드도 얻을 수 있습니다. 이 함수들을 사용해서, 객체의 클래스 멤버만이 아니라, 부모(즉, 객체 클래스가 상속한 클래스)도 얻을 수 있습니다.

User Contributed Notes

There are no user contributed notes for this page.


예제

이 예제에서, 먼저 기반 클래스를 정의하고 그 클래스를 확장합니다. 기반 클래스는 일반 야채를 묘사하고, 식용 여부와 색을 가집니다. 서브클래스 Spinach는 요리하는 메쏘드와 요리되었는지 확인하는 메쏘드를 추가합니다.

Example #1 classes.inc

<?php

// 멤버 프로퍼티와 메쏘드를 가지는 기반 클래스
class Vegetable {

   var $edible;
   var $color;

   function Vegetable($edible, $color="green")
   {
       $this->edible = $edible;
       $this->color = $color;
   }

   function is_edible()
   {
       return $this->edible;
   }

   function what_color()
   {
       return $this->color;
   }

} // Vegetable 클래스 끝

// 기반 클래스 확장
class Spinach extends Vegetable {

   var $cooked = false;

   function Spinach()
   {
       $this->Vegetable(true, "green");
   }

   function cook_it()
   {
       $this->cooked = true;
   }

   function is_cooked()
   {
       return $this->cooked;
   }

} // Spinach 클래스 끝

?>

이제 이 클래스로부터 두 객체 인스턴스를 만들고, 클래스 상속을 포함한 정보를 출력합니다. 변수를 깔끔하게 출력하기 위한 몇몇 보조 함수도 정의합니다.

Example #2 test_script.php

<pre>
<?php

include "classes.inc";

// 보조 함수

function print_vars($obj)
{
foreach (get_object_vars($obj) as $prop => $val) {
    echo "\t$prop = $val\n";
}
}

function print_methods($obj)
{
$arr = get_class_methods(get_class($obj));
foreach ($arr as $method) {
    echo "\tfunction $method()\n";
}
}

function class_parentage($obj, $class)
{
if (is_subclass_of($GLOBALS[$obj], $class)) {
    echo "Object $obj belongs to class " . get_class($$obj);
    echo " a subclass of $class\n";
} else {
    echo "Object $obj does not belong to a subclass of $class\n";
}
}

// 두 객체 인스턴스 생성

$veggie = new Vegetable(true, "blue");
$leafy = new Spinach();

// 객체에 대한 정보 출력
echo "veggie: CLASS " . get_class($veggie) . "\n";
echo "leafy: CLASS " . get_class($leafy);
echo ", PARENT " . get_parent_class($leafy) . "\n";

// veggie 프로퍼티 출력
echo "\nveggie: Properties\n";
print_vars($veggie);

// leafy 메쏘드
echo "\nleafy: Methods\n";
print_methods($leafy);

echo "\nParentage:\n";
class_parentage("leafy", "Spinach");
class_parentage("leafy", "Vegetable");
?>
</pre>

위 에제에서 중요한 점은 $leafy 객체가 Vegetable의 서브클래스인 Spinach 클래스의 인스턴스라는 점입니다. 그러므로 스크립트의 마지막 부분은 다음을 출력합니다:

   [...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach a subclass of Vegetable

User Contributed Notes

There are no user contributed notes for this page.


클래스/객체 함수 목록

Table of Contents

  • __autoload — Attempt to load undefined class
  • call_user_method_array — 인수 배열로 사용자 메소드를 호출 [배제됨]
  • call_user_method — 특정 객체의 사용자 메쏘드를 호출 [배제됨]
  • class_alias — Creates an alias for a class
  • class_exists — 클래스가 정의되었는지 확인
  • get_called_class — "Late Static Binding" 클래스명
  • get_class_methods — 클래스 메쏘드명을 얻습니다
  • get_class_vars — 클래스의 기본 프로퍼티를 얻습니다
  • get_class — 객체의 클래스명을 반환
  • get_declared_classes — 선언된 클래스명을 배열로 반환
  • get_declared_interfaces — 선언된 모든 인터페이스의 배열을 반환
  • get_declared_traits — Returns an array of all declared traits
  • get_object_vars — 주어진 객체의 프로퍼티를 얻습니다
  • get_parent_class — 객체나 클래스의 부모 클래스명을 얻습니다
  • interface_exists — 인터페이스가 정의되었는지 확인
  • is_a — 객체가 이 클래스나 부모 클래스 중 하나인지 확인
  • is_subclass_of — 이 클래스가 부모 클래스의 자식인지 확인
  • method_exists — 클래스 메쏘드가 존재하는지 확인
  • property_exists — 객체나 클래스가 프로퍼티를 가졌는지 확인
  • trait_exists — Checks if the trait exists

User Contributed Notes

Skydev 05-Jan-2011 08:40
A small function that allows finding all references to the object. Written in 3 minutes and may be buggy (for ex pass object as reference in some places?)

<?php
function find_ref_obj($object, $obj, $path) {
    if (in_array($obj,$GLOBALS['__REF_CHECKED'],true))
        return false;
    $GLOBALS['__REF_CHECKED'][]=$obj;
    $r = array();
    foreach ((array)$obj as $k => $v) {
        if ($v === $object)
            $r[] = $path . "->$k";
        if (is_object($v)) {
            $t = find_ref_obj($object,$v,$path . "->$k");
            if ($t!==false)
                $r=array_merge($r,$t);
        }
        else if (is_array($v)) {
            $t = find_ref_arr($object,$v,$path . "->$k");
            if ($t!==false)
                $r=array_merge($r,$t);
        }
    }
    if (empty($r))
        return false;
    else
        return $r;
}
function find_ref_arr($object, $arr, $path) {
    if (in_array($arr,$GLOBALS['__REF_CHECKED'],true))
        return false;
    $GLOBALS['__REF_CHECKED'][]=$arr;
    $r = array();
    foreach ($arr as $k => $v) {
        if ($v === $object)
            $r[] = $path . "['$k']";
        if (is_object($v)) {
            $t = find_ref_obj($object,$v,$path . "['$k']");
            if ($t!==false)
                $r=array_merge($r,$t);
        }
        else if (is_array($v)) {
            $t = find_ref_arr($object,$v,$path . "['$k']");
            if ($t!==false)
                $r=array_merge($r,$t);
        }
    }
    if (empty($r))
        return false;
    else
        return $r;
}
function find_references($object) {
    $r = array();
    $GLOBALS['__REF_CHECKED']=array();
    foreach ($GLOBALS as $n => $v)
    if ($n!='__REF_CHECKED')
    if ($n!='GLOBALS') {
        if ($v === $object)
            $r[]=$n;
        if (is_object($v)) {
            $t = find_ref_obj($object,$v,$n);
            if ($t!==false)
                $r=array_merge($r,$t);
        }
        else if (is_array($v)) {
            $t = find_ref_arr($object,$v,$n);
            if ($t!==false)
                $r=array_merge($r,$t);
        }
    }
    unset($GLOBALS['__REF_CHECKED']);
    return $r;
}

function find_refs($object) {
    return implode(', ',find_references($object));
}
?>
Dennis 01-Jul-2010 01:04
We have an array with many objects in a style like

<?php

$step = new StepModel(1); // if the StepModel id is "1"
$demand = $step->getDemand(); // returns DemandModel
$step2 = $demand->getCustomer(); // returns StepModel
$demand2 = $step2->getDemand(); // returns DemandModel

// [ ... ]

?>

$step and $step2 can be the same objects. So we have an recursive array. Now we need to know if $step == $step2 or $step === $step2. In other words: We need to know the php internal resource ids.

Because there is no function in php api, we made the following function.

Be careful: In our case, all objects have as first attribute ["id":protected]. If your objects are different from this, you need to edit $pattern.

Warning: function is very slow and should only be called if it's necessary for debugging reasons:

<?php

/**
 * returns resource- and object-ids of all objects in an array
 *
 * @param array $array
 * @return array
 */
function getObjectInformation(Array $array)
{
    // start output-buffering
    ob_start();

    // create an var_dump of $array
    var_dump($array);

    // save the dump in var $dump
    $dump = ob_get_contents();

    // clean the output-buffer
    ob_end_clean();

    // delete white-spaces
    $dump = str_replace(' ', '', $dump);

    // define the regex-pattern
    // in our case, all objects look like this:
    //
    // object(ClassName)#1(1){
    // ["id":protected]=>
    // string(1)"1"
    $pattern  = '/object\(([a-zA-Z0-9]+)\)#([0-9]+)\([0-9]+\){\\n';
    $pattern .= '\["id":protected\]=>\\n';
    $pattern .= 'string\([0-9]+\)"([v]?[0-9]+)"/im';

    // search for all matches
    preg_match_all($pattern, $dump, $regs);

    // sort all mathes by class name, object id and then resource id
    array_multisort($regs[1], SORT_ASC, SORT_STRING,
                    $regs[3], SORT_ASC, SORT_NUMERIC,
                    $regs[2], SORT_ASC, SORT_NUMERIC);

    // cache the last match
    $lastMatch = array();

    // the return value
    $return = array();

    // loop through the matches
    for ($i = 0; $i < sizeof($regs[0]); $i ++) {

        // check if the current match was not visited before
        if (0 == sizeof($lastMatch) ||
            $regs[1][$i] != $lastMatch[1] ||
            $regs[2][$i] != $lastMatch[2] ||
            $regs[3][$i] != $lastMatch[3]) {

            // save the match in return value
            array_push($return, array($regs[1][$i],
                                      $regs[2][$i],
                                      $regs[3][$i]));
        }

        // save match in last match cache
        $lastMatch[1] = $regs[1][$i];
        $lastMatch[2] = $regs[2][$i];
        $lastMatch[3] = $regs[3][$i];
    }

    // return all matches
    return $return;
}

?>

I know, it's not that elegant but I hope it's useful for a few people.
ahigerd at timeips dot com 20-Dec-2007 04:02
To access an object member with an illegal character in the name, use this syntax:

$obj->{'illegal-property:name()'}

This is particularly relevant with the dynamically-generated classes used by, for instance, database objects and the SoapClient class.
pascal dot poncet at netconsult dot com 13-Oct-2005 04:21
Subject: using "sql_calc_found_rows" in a MySQL query while exploiting result in a PHP db class object.

Hello,

There is a nice function in MySQL that allows to know how many records would have been returned if no "where" clause were set : SQL_CALC_FOUND_ROWS.

If you have create a db object to collect the returned lines, you will be a little perplex when trying to call the result of this function.

Why ?
Simply because the returned field's name is "found_rows()" and obviously it's not possible to call something like :

<?php $result->found_rows() ?>

...as it will try to acces a method, not a property !

Then, the only way to get the right result seems to be the use of a class function, like :

<?php
  $db->query("select found_rows()");
  $count=current(get_object_vars(current($db->result)));
?>

Of course, if somebody found an other way to solve it, like a special syntax (see the one used with curled arrays in a string), I'm really open to discuss.

Good luck,
Pascal
ia [AT] zoznam [DOT] sk 03-Aug-2005 07:55
as for zabmilenko's solution:
wouldn't it be better to create it this way?

<?php
// The base class for all db classes
class DB {
 protected $connectId;
}

// Class for MySQL, which extends base class
class MySQL extends DB {
 function connect () {
  $this->connectId = mysql_connect (...);
 }
}

// Class for PostgreSQL, which extends base class
class pgSQL extends DB {
 function connect () {
  $this->connectId = pg_connect (...);
 }
}

// and then call constructor like this:
$dbName = "MySQL";
$db = new $dbName ( ... );
// ... which creates an object of class MySQL
?>
zabmilenko at hotmail dot com 27-Jun-2005 09:08
((PHP5))

I wanted to dynamically choose an extender for a class.  This took awhile of playing with it but I came up with a solution.  Note that I can't verify how safe it is, but it appears to work for me.  Perhaps someone else can shed light on the details:

<?php

class A { var $value = "Class A\n"; }
class B { var $value = "Class B\n"; }

// Uncomment which extender you want.  You can use variables as well.
// define('__EXTENDER__', 'A');
   define('__EXTENDER__', 'B');

// Use eval to create a wrapper class.
eval('class EXTENDER extends '. __EXTENDER__ . ' { }');

class C extends EXTENDER
{
  function __construct()
  {
     echo $this->value;
  }
}

$t = new C;

?>

Outputs:   Class B

Practical application:  I have a database abstraction system that has individual classes for mysql, pgsql, et al.  I want to be able to create a global db class that extends one of the individual db classes depending on the application configuration.

I know that there are probably much better ways of doing this but I haven't reached that level when it comes to classes.
cjones 03-Mar-2005 10:27
If anyone is interested in looking for a way to dynamically load existing objects into a class, here is what I found very useful.

//---------------------------------------------------------
// Dynamically load External Objects into a class

  function objRef ( &$obj ) {
    eval("\$this->obj_".get_class($obj)." = \$obj;");
  }
//---------------------------------------------------------
// Reference by using: $this->obj_[object Name]->[var|f{}]

Example:

class date {  function date ( ) { $this->date = "March 3rd"; } }
class time {  function time ( ) { $this->time = "12:30pm"; } }

class show {
    function objRef ( &$obj ){
        eval("\$this->obj_".get_class($obj)." = \$obj;");
    }
   function test ( $var ){
       echo "$var".$this->obj_date->date." @ ".$this->obj_time->time;
   }
}

$date = new date;
$time = new time;
$show = new show;
    $show->objRef($date);
    $show->objRef($time);
    $show->test("Time Now => ");

// Output: Time Now => March 3rd @ 12:30pm

I found the prefix 'obj_' before the class name useful because it helped me to automatically identify external object references when scanning through my scripts. You can omit this if you want. Hope this helps someone.
http://sc.tri-bit.com/ StoneCypher 02-Mar-2005 07:25
to covertka at muohio dot edu and pillepop2003 at yahoo dot de:

There's a much easier solution to getting a class' name for working with a factory function.  Let's assume you're doing something like this:

<?php

  function FactoryFunction($whatever, $instancedata) {

    switch ($whatever) {
      case 'stuff'      : return new Stuff($instancedata);
      case 'otherstuff' : return new Otherstuff($instancedata);
    }

  }

?>

Now, consider the named parameter idiom and remember that PHP uses hashes for everything; as a result make the following changes:

<?php

  function FactoryFunction($whatever, $instancedata) {

    switch ($whatever) {

      case 'stuff'      : return array('typeis'=>'stuff',      'instance'=>new Stuff($instancedata));
      case 'otherstuff' : return array('typeis'=>'otherstuff', 'instance'=>new Otherstuff($instancedata));

    }

  }

?>

Nice 'n simple.  It seems that what the original poster wanted was something like C++ static data members; unfortunately as PHP4 has no static variables at all, there would need to be significant language change to support static-like behavior.  If you move to PHP5, the static keyword solves your problem cleanly.
covertka at muohio dot edu 03-Jan-2005 02:27
To pillepop2003 at yahoo dot de:

I have the same issue.  I have a base class that manages database tasks for a number of child classes.  One of the functions in the base class is a find() method that returns instances of the child classes.  Since find() is usually called as a static method, it needs to know the name of the child class.  As you've found, this appears to be impossible to get in an easy fashion.

The only way I've found to get the child class name is to use the debug_traceback() function.  This requires me to have a find() method in every child class, but it does work.

Here's an example:

<?php
  require_once("Application.php");

  class parentClass {
    function find() {
      $className = NULL;
      foreach (debug_backtrace() as $bt) {
        if ($bt['function'] == __FUNCTION__) {
          $className = $bt['class'];
        }
      }

      // here should be some code to find the proper id, let's assume it was id 1
      $id = 1;
      return new $className($id);
    }
  }

  class foo extends parentClass {
    function __construct($id) {
      $this->id = id;
    }

    function find() {
      return parent::find();
    }
  }

  class bar extends parentClass {
    function __construct($id) {
      $this->id = id;
    }

    function find() {
      return parent::find();
    }
  }

  $a = foo::find();
  printf("Type for \$a: %s<br/>\n", get_class($a));
  $b = bar::find();
  printf("Type for \$b: %s<br/>\n", get_class($b));
?>
iparanoid at gmx dot de 04-Aug-2004 08:17
To pillepop2003 at yahoo dot de:

It seems to me if there really is no nice way to get the class name in an un-instanciated class, there is a workaround in PHP5 though using static/class variables.

Example:

<?php

class myFoo
{
    static $__ClassName = __CLASS__;

    static function getClassName()
    {
        return myFoo::$__ClassName;
    }
};

class myFooExtended extends myFoo
{
    function __construct()
    {
        myFooExtended::$__ClassName = __CLASS__;
    };
};

?>

However, you'll need to have at least instanciated an object of the class myFooExtended before calling getClassName or introduce some other initialization (the class variable will need to be set at some point to __CLASS__ in the sub-class).
greg at doutromundo dot com 06-Jul-2004 04:58
As programmers, you're probably more organized than me, but, I do try and maintain some order in my classes and codes and separate them in "packages" as in java.
This helped me keep them organized but caused havok when trying to use them, so what I did was to create a class that handles the loading of classes (which I instanciate in all pages) along with my error handling class all bundled up. This way, I can load my classes with a command similar to
$baseClass->loadClass("package","className"[,"constructor"]);

the function responsible for this has some checking to see if they are loaded and stuff like that...

function loadClass($packageName,$className,$constructor=""){
  // if you dont have a constructor declare any function inside
  // the class
  if ($constructor==""){
    $constructor=$className;
  }
  if(!is_callable(array($className,$constructor))){
    if (defined("CLASS_DIR")){
      $pkg = CLASS_DIR.$packageName."/";
        if (is_dir($pkg)){
          // we have a directory with the package name
          $cls = $pkg.$className.".class.php";
          if(is_file($cls)){
            // we have a file
            include_once($cls);
          }else{
            die("Class <b>$className</b> could not be found in package <b>$packageName</b> , please check your instalation");
          }
        }else{
          die("Package <b>$packageName</b> could not be found, please check your instalation");
        }
      }
    }
}

Just remember to define CLASS_DIR as the physical path for the directories where you packages are...

Hope this comes in handy...

Here's an example of a diretory strucutre...
/var/www/classes/   <- this would be CLASS_DIR
in there I have:
package1/
     name.class.php
     name2.class.php
....

The loadClass would look like: loadClass("package1","name");

Cute and easy
ettinger at consultant dot com 19-Jun-2004 01:59
Re: Looking for an uninstantiated class

# Loads data from a table into a class object
class LFPDataFactory extends LFPObject {
        var $object;
        var $class;
        var $table;
        function LFPDataFactory($args) {
                $this->unpackArgs($args); // assigns locals from $args
                if (in_array(strtolower($this->class), get_declared_classes())) {
                        $this->object = new $this->class;
                        // assemble the columns in the table...
                        // select their values and put them in our new object...
                } else { trigger_error("Class ".$this->class." not found", E_USER_ERROR); }
        }
}
$r = new LFPDataFactory("class=LFPLayout,table=layout");
$new_obj = $r->object; // this is a LFPLayout object.
print_r($new_obj);

This class looks to see if the class exists, then instantiates it -- a declared class is not the same as an instantiated class. As long as LFPLayout exists somewhere in the scripts, get_declared_classes() will find it. Remember strtolower on compare, however.

Why would I do this? Because I have my class layouts the same as their respective tables; the factory then selects the data (making sure that the variables match) and plugs in the data. (I've left out the actual code to do the selection/insertion).
HOC 28-May-2004 06:40
to pillepop2003

Why do u want to know the classname of an non-existant object?

The only possible explanation for this question seems to me u want to know the class before u instantiate the object. Well, this is of no use since u always instantiate a class of ur choice.

When the class is instantiated into an object u can find the class of the object by means of get_class(). This is all u need. In case of inheritance u can use get_class($this) to get the class of the instantiated object. Now u can differentiate according to which class the object belongs to.

e.g.:

<?php
class A{
   function A(){
      $class_of_this = get_class($this);
      echo 'Object is an instance of class '.$class_of_this.' which is the ';
      if(strcmp($class_of_this,'A')==0)
         echo 'parent-class';
      else if(strcmp($class_of_this,'B')==0)
         echo 'child-class';
      echo ".\n";
   }
}

class B extends A{
   function B(){
      $this->A();
   }
}

$object1 = new A();
$object2 = new B();
?>

When u run this code-snippet the output will be:

Object is an instance of class A which is the parent-class.
Object is an instance of class B which is the child-class.
ar at 5mm de 28-Aug-2003 04:59
I missed some kind of function to dynamicly override or extend an Object:

-----------------------------------------
function &extendObj(&$obj, $code) {
    static $num = 0;

    $classname = get_class($obj);
    $newclass = $classname.$num;

    eval('class '.$newclass.' extends '.$classname.' { '.$code.' }');

    $newobj = new $newclass();

    $vars = get_class_vars($classname);
    foreach($vars AS $key=>$value) {
        $newobj->$key = &$obj->$key;
    }

    return $newobj;
}
-----------------------------------------

This creates a new class which extends the old one by the given code parameter, instanciates it and copy all vars from the old obj to the new one.

-----------------------------------------
class testA {
    var $prop = 'a';

    function funcA($val) {
        $this->prop = $val;
    }

    function value() {
        return $this->prop;
    }
}

$obj = new testA();

$newobj = &extendObj(&$obj, 'function addX() { $this->prop .= "x"; }');

$newobj->funcA('abc');
$newobj->addX();
echo $newobj->value();
-----------------------------------------

Results in 'abcx'. You can use the function multiple times and also with class variables. Be carefull, even if $newobj is just a copy of $obj, $obj->value() will return 'abcx', too, because of the & operator: $newobj->$key = &$obj->$key;
zidsu at hotmail dot com 08-Jul-2003 02:24
FYI: if you want to split your class into manageble chunks, what means different files for you, you can put you functoins into includes, and make include() have a return value. Like this:

class Some_class {
  var $value = 3;
  function add_value ($input_param) {
    return include ("path/some_file.php");
  }
}

And your included file:

$input_param += $this->value;
return $input_param;

Then your function call will be:

$instance = new Some_class ();
$instance->add_value (3);

And this will return
6
hopefully :P

Keep in mind though, that the scope in the included file will be identical to the scope the function 'add_value' has.
And if you want to return the outcome, you should also have a return statement made in your include as well.
asommer*at*as-media.com 20-Sep-2002 11:52
Something I found out just now that comes in very handy for my current project:

it is possible to have a class override itself in any method ( including the constructor ) like this:

class a {

..function ha ( ) {
....if ( $some_expr ) {
......$this = new b;
......return $this->ha ( );
....}
....return $something;
..}

}

in this case assuming that class b is already defined and also has the method ha ( )

note that the code after the statement to override itself is still executed but now applies to the new class

i did not find any information about this behaviour anywhere, so i have no clue wether this is supposed to be like this and if it might change... but it opens a few possibilities in flexible scripting!!
einhverfr at not-this-host dot hotmail dot com 14-Sep-2002 09:35
You may find it helpful in complex projects to have namespaces for your classes, and arrange these in a hierarchical manner.   A simple way to do this is to use the filesystem to order your hierarchies and then define a function like this:

function use_namespace($namespace){

require_once("namespaces/$namespace.obj.php");

}

(lack of indentation due to HTML UI for this page)
This requires that all your object libraries end in .obj.php (which I use) but you can modfy it to suit your needs.  To call it you could, for exmaple call:

use_namespace("example");
or if foo is part of example you can call:
use_namespace("example/foo");
justin at quadmyre dot com 19-Aug-2002 06:38
If you want to be able to call an instance of a class from within another class, all you need to do is store a reference to the external class as a property of the local class (can use the constructor to pass this to the class), then call the external method like this:

$this->classref->memberfunction($vars);

or if the double '->' is too freaky for you, how about:

$ref=&$this->classref;
$ref->memberfunction($vars);

This is handy if you write something like a general SQL class that you want member functions in other classes to be able to use, but want to keep namespaces separate. Hope that helps someone.

Justin

Example:

<?php

class class1 {
    function test($var) {
        $result = $var + 2;
        return $result;
    }
}

class class2{
    var $ref_to_class=''; # to be pointer to other class

    function class1(&$ref){ #constructor
        $this->ref_to_class=$ref; #save ref to other class as property of this class
    }

    function test2($var){
        $val = $this->ref_to_class->test($var); #call other class using ref
        return $val;
    }
}

$obj1=new class1;
# obj1 is instantiated.
$obj2=new class2($obj1);
# pass ref to obj1 when instantiating obj2

$var=5;
$result=obj2->test2($var);
# call method in obj2, which calls method in obj1
echo ($result);

?>
gateschris at yahoo dot com 08-Mar-2001 10:59
[Editor's note: If you are trying to do overriding, then you can just interrogate (perhaps in the method itself) about what class (get_class()) the object belongs to, or if it is a subclass of a particular root class.

You can alway refer to the parent overriden method, see the "Classes and Objects" page of the manual and comments/editor's notes therein.]

There is no function to determine if a member belongs to a base class or current class eg:

<?php
class foo {
 function foo () { }
 function a () { }
}

class bar extends foo {
 function bar () { }
 function a () { }
}

lala = new Bar();
?>
------------------
how do we find programmatically if member a now belongs to class Bar or Foo.


Attempt to load undefined class

  • ≪ 클래스/객체 함수 목록
  • call_user_method_array ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • Attempt to load undefined class

__autoload

(PHP 5, PHP 7)

__autoload — Attempt to load undefined class

설명

void __autoload ( string $class )

You can define this function to enable classes autoloading.

인수

class

Name of the class to load

반환값

값을 반환하지 않습니다.

참고

  • spl_autoload_register() - Register given function as __autoload() implementation

User Contributed Notes

Jose G 05-Dec-2014 05:29
You should use include() or require() inside __autoload()
instead of include_once() or require_once().

If you reach __autoload(), then you know the file with the class definition has not been loaded yet.

include() and require() are more efficient than include_once() and require_once().
romi 29-Jun-2014 08:16
<?php
    if(!function_exists('classAutoLoader')){
        function classAutoLoader($class){
            $class=strtolower($class);
            $classFile=$_SERVER['DOCUMENT_ROOT'].'/include/class/'.$class.'.class.php';
            if(is_file($classFile)&&!class_exists($class)) include $classFile;
        }//You have to give the class name and not the class's path for the first parameter in the class_exists()
    }
    spl_autoload_register('classAutoLoader');
?>
Ben 26-Mar-2014 02:56
Guys, this document ( i mean __autoload() ) not mentioned one special situation: if you both use __autoload() and spl_autoload_register(), the __autoload() function will never to be called. Although spl_autoload_register() documentation explained why, i decide to wrote this in case some one get confused and waste all day to figure out why.

Here is some code to verify above:

<?php
function __autoload($class) {
}

function my_loader() {
}

function your_loader() {
}

var_dump ( spl_autoload_functions () );
echo '<br/>';

spl_autoload_register ( 'my_loader' );
spl_autoload_register ( 'your_loader' );

var_dump ( spl_autoload_functions () );
ohcc at 163 dot com 08-Dec-2013 04:12
It is highly recommended not to use the __autoload() function any more. Now the spl_autoload_register() function is what you should consider.Sorry for the mistake in line 6 of my previous note. And below is the corrected PHP code.
<?php
    if(!function_exists('classAutoLoader')){
        function classAutoLoader($class){
            $class=strtolower($class);
            $classFile=$_SERVER['DOCUMENT_ROOT'].'/include/class/'.$class.'.class.php';
            if(is_file($classFile)&&!class_exists($class)) include $classFile;
        }
    }
    spl_autoload_register('classAutoLoader');
?>
ohcc at 163 dot com 08-Dec-2013 03:28
It is highly recommended not to use the __autoload() function any more. Now the spl_autoload_register() function is what you should consider.
<?php
    if(!function_exists('classAutoLoader')){
        function classAutoLoader($class){
            $class=strtolower($class);
            $classFile=$_SERVER['DOCUMENT_ROOT'].'/include/class/'.$class.'.class.php';
            if(is_file($classFile)&&!class_exists($classFile)) include $classFile;
        }
    }
    spl_autoload_register('classAutoLoader');
?>
eragroove at gmail dot com 10-Jul-2013 02:48
If you can keep file name and class name as same, it will be good programming practice. It helps to __autoload function to load file without checking any condition.

function __autoload($class){
  require_once( $class.".php");
}
wojciech.fornal@gmail,com 08-Jun-2013 08:56
keyboardSmasher

You may or may not be right as the file name doesn't necessarily have to reflect a class name it contains (but it's usually considered a good practice). It isn't always a straightforward mapping (look at some PHP frameworks and autoload implementations).

File myBar.php may contain the class:

class Bar {
}

or it even contain the class:

class Foo {
}

Best regards
keyboardSmasher 17-Apr-2013 04:45
qeremy, your code is incorrect.

<?php
include_once("./myClass.php");
include_once("./myFoo.php");
include_once("./myBar.php");

$obj = new myClass();
$foo = new Foo();
$bar = new Bar();
?>

<?php
$foo = new Foo();
$bar = new Bar();
?>

should be:

<?php
$foo = new myFoo();
$bar = new myBar();
?>
qeremy 09-Mar-2012 12:01
Even I have never been using this function, just a simple example in order to explain it;

./myClass.php
<?php
class myClass {
    public function __construct() {
        echo "myClass init'ed successfuly!!!";
    }
}
?>

./index.php
<?php
// we've writen this code where we need
function __autoload($classname) {
    $filename = "./". $classname .".php";
    include_once($filename);
}

// we've called a class ***
$obj = new myClass();
?>

*** At this line, our "./myClass.php" will be included! This is the magic that we're wondering... And you get this result "myClass init'ed successfuly!!!".

So, if you call a class that named as myClass then a file will be included myClass.php if it exists (if not you get an include error normally). If you call Foo, Foo.php will be included, and so on...

And you don't need some code like this anymore;

<?php
include_once("./myClass.php");
include_once("./myFoo.php");
include_once("./myBar.php");

$obj = new myClass();
$foo = new Foo();
$bar = new Bar();
?>

Your class files will be included "automatically" when you call (init) them without these functions: "include, include_once, require, require_once".


인수 배열로 사용자 메소드를 호출 [배제됨]

  • ≪ __autoload
  • call_user_method ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 인수 배열로 사용자 메소드를 호출 [배제됨]

call_user_method_array

(PHP 4 >= 4.0.5, PHP 5)

call_user_method_array — 인수 배열로 사용자 메소드를 호출 [배제됨]

설명

mixed call_user_method_array ( string $method_name , object &$obj , array $params )
Warning

call_user_method_array() 함수는 PHP 4.1.0부터 배제되었습니다.

예제

Example #1 call_user_method_array() 대체

<?php
call_user_func_array(array($obj, $method_name), $params);
call_user_func_array(array(&$obj, $method_name), $params); // PHP 4

참고

  • call_user_func_array() - Call a callback with an array of parameters
  • call_user_func() - Call the callback given by the first parameter

User Contributed Notes

musaatalay dot mobile at gmail dot com 15-Jul-2014 04:57
<?php

class a{

    function b($a,$b,$c){

        echo $a." ".$b." ".$c;

    }

    function c(Array $a, Array $b){

        print_r($a);

        echo "<br />";

        print_r($b);

    }

    function cuf(Array $a, Array $b){

        print_r($a);

        echo "<br />";

        print_r($b);

    }

}

$a = new a;

// ### Just String Params ###

$array = array("Musa ATALAY",":","Software Developer");

$str = NULL;

foreach($array AS $v){
    if(is_string($v)){
        $str.="'".$v."',";
    }else{
        $str.=$v;
    }
}

$str = rtrim($str,",");

$run = "echo \$a->b(".$str.");";

echo "<br />";

eval($run);

$str = NULL;

/*
OUTPUT :

Musa ATALAY : Software Developer
*/

// ### With Array Params ###

$array = array(array("Musa ATALAY",":","Software Developer"),array("Musa ATALAY",":","Software Developer"));

foreach($array AS $i => $v){
    if(is_string($v)){
        $str.="'".$v."',";
    }else{
        $str.="\$array[".$i."],";
    }
}

$str = rtrim($str,",");

$run = "echo \$a->c(".$str.");";

echo "<br />";

eval($run);

/*
 OUTPUT :

Musa ATALAY : Software Developer

Musa ATALAY : Software Developer
*/

?>
wloche at hotmail dot com 15-Jul-2010 10:33
You don't have to write a new function, <?php call_user_func_array(array($obj, $method_name), $params); ?> works pretty fine! (to my mind, 'eval' fucntion should be avoided almost all the time)
brudinie at googlemail dot com 25-Mar-2009 04:23
<?php

/**
 * @param string $func - method name
 * @param object $obj - object to call method on
 * @param boolean|array $params - array of parameters
 */
function call_object_method_array($func, $obj, $params=false){
    if (!method_exists($obj,$func)){
        // object doesn't have function, return null
        return (null);
    }
    // no params so just return function
    if (!$params){
        return ($obj->$func());
    }
    // build eval string to execute function with parameters
    $pstr='';
    $p=0;
    foreach ($params as $param){
        $pstr.=$p>0 ? ', ' : '';
        $pstr.='$params['.$p.']';
        $p++;
    }
    $evalstr='$retval=$obj->'.$func.'('.$pstr.');';
    $evalok=eval($evalstr);
    // if eval worked ok, return value returned by function
    if ($evalok){
        return ($retval);
    } else {
        return (null);
    }
    return (null);
}

?>


특정 객체의 사용자 메쏘드를 호출 [배제됨]

  • ≪ call_user_method_array
  • class_alias ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 특정 객체의 사용자 메쏘드를 호출 [배제됨]

call_user_method

(PHP 4, PHP 5)

call_user_method — 특정 객체의 사용자 메쏘드를 호출 [배제됨]

설명

mixed call_user_method ( string $method_name , object &$obj [, mixed $parameter [, mixed $... ]] )
Warning

call_user_method() 함수는 PHP 4.1.0부터 배제되었습니다.

예제

Example #1 call_user_method() 대체

<?php
call_user_func(array($obj, $method_name), $parameter /* , ... */);
call_user_func(array(&$obj, $method_name), $parameter /* , ... */); // PHP 4
?>

참고

  • call_user_func_array() - Call a callback with an array of parameters
  • call_user_func() - Call the callback given by the first parameter

User Contributed Notes

Chris 24-Jun-2008 04:26
From what i've observed, call_user_func() does everything this funciton does and a little more.  I made a pretty good example call_user_func()'s usage with object instances and figured it might be useful here:

<?php

    /**

        This is a demonstration of 2 neat features of PHP

        *    passing array arguments in as a big array, and using += to assign defaults to missing values
                This would allow for function calls that more closely mimick thoes made in javascript using JSON, with enough work, it could be almost identical using associative arrays
        *    function callbacks within a class to global instances of other classes
                This allows you pass a function callback to an object early on, and hold off its execution until later in the program (say during page outputing after everything has been setup)

    **/

    class Runner {

        public $id;

        public function __construct($id) {
            $this->id = $id;
            echo "constructing " . __CLASS__ . " with id of $id<br />\n";
        }

        public function run($distance = null, $measurement = 'km') {
            if ($distance) {
                echo 'I ran ' . $distance . ' ' . $measurement . '.';
            } else {
                echo 'I ran.';
            }
            echo "({$this->id})<br />\n";
        }
    }

    class Speaker {

        public $id;

        public function __construct($id = 0) {
            $this->id = $id;
            echo "constructing " . __CLASS__ . " with id of $id<br />\n";
        }

        public function speak($statement = 'hello world') {
            echo $statement . "({$this->id})<br />\n";
        }
    }

    class Test {
        protected $runCallback = null;
        protected $speakCallback = null;
        protected $statement;
        protected $distance;

        public function __construct(array $params = array()) {
            echo "constructing " . __CLASS__ . "<br />\n";
            $params += array('speakCallback' => array('Speaker', 'speak'), 'runCallback' => array('Runner', 'run'), 'statement' => 'Hello from ' . __CLASS__ . ' class!', 'distance' => 10);
            foreach($params as $k => $v) {
                $this->$k = $v;
            }
        }

        public function getInstance() {
            return new self(current(func_get_args()));
        }

        public function callRunner() {
            if (is_callable($this->runCallback))
                return call_user_func($this->runCallback, $this->distance);
            else
                throw new Exception("runCallback is not callable\n" . var_export($this->runCallback, true) . "\n");
        }
        public function callSpeaker() {
            if (is_callable($this->speakCallback))
                return call_user_func($this->speakCallback, $this->statement);
            else
                throw new Exception("speakCallback is not callable\n" . var_export($this->speakCallback, true) . "\n");
        }
    }

    $r = new Runner(1);
    $s = new Speaker(2);

    // Note that we're using $s instead of 'Speaker'
    call_user_func(array($s, 'speak'), 'Hello from global!');

    // try out from global with call_user_func_array() to pass args as an array
    call_user_func_array(array($r, 'run'), array(5, 'mi'));

    $Test = new Test(array('runCallback' => array($r, 'run'), 'speakCallback' => array($s, 'speak')));
    $Test->callRunner();
    $Test->callSpeaker();

    $Test = call_user_func(array('Test', 'getInstance'), array('runCallback' => array($r, 'run'), 'distance' => 15));
    // should work as expected
    $Test->callRunner();
    // should throw an error for trying to use this during a static call to Speaker::speak() because of the default
    $Test->callSpeaker();

?>

Hope that's helpful.
ravichandran_11 at yahoo dot co dot in 10-Mar-2008 01:32
<?php
class abc{
   function func($argument)  {
       $argument="It works";
   }
}
$obj=new abc;
$argument_to_be_changed="No it doesn't work";
call_user_method("func", $obj, &$argument_to_be_changed);
echo "Result : ".$argument_to_be_changed;
?>

This code is working. But will through some warning message which you can hide by configuring php.ini
j dot h at h-elektro dot de 05-Feb-2007 09:11
It does not work to use Pointers as Arguments:

<?php
class abc{
   function func(&$argument)  {
       $argument="It works";
   }
}

$obj=new abc;
$argument_to_be_changed="No it doesnt";
call_user_method("func", $obj, $argument_to_be_changed);

echo "Result".$argument_to_be_changed;
?>

The result is: "No it doesnt".

Regards
der Jan
paulo at emd dot com dot br 18-Sep-2000 02:12
This function is very similar to this:

<?php
$method="Print";
$object->$method($param1,$param2);
?>

Note the extra $ after the ->
jmcastagnetto at php dot net 21-Aug-2000 02:04
You can pass a variable number of parameters to a function, use a definition like:

function mymethod ($v1, $v2, $v3="", $v4="")

and then you can pass 2, 3 or 4 parameters. This is explained in the "Functions" section of the manual.

See also the PHP4 functions: func_num_args(), func_get_arg(), and func_get_args(), and examples therein


Creates an alias for a class

  • ≪ call_user_method
  • class_exists ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • Creates an alias for a class

class_alias

(PHP 5 >= 5.3.0, PHP 7)

class_alias — Creates an alias for a class

설명

bool class_alias ( string $original , string $alias [, bool $autoload = TRUE ] )

Creates an alias named alias based on the user defined class original. The aliased class is exactly the same as the original class.

인수

original

The original class.

alias

The alias name for the class.

autoload

Whether to autoload if the original class is not found.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 class_alias() example

<?php

class foo { }

class_alias('foo', 'bar');

$a = new foo;
$b = new bar;

// the objects are the same
var_dump($a == $b, $a === $b);
var_dump($a instanceof $b);

// the classes are the same
var_dump($a instanceof foo);
var_dump($a instanceof bar);

var_dump($b instanceof foo);
var_dump($b instanceof bar);

?>

위 예제의 출력:

bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

참고

  • get_parent_class() - 객체나 클래스의 부모 클래스명을 얻습니다
  • is_subclass_of() - 이 클래스가 부모 클래스의 자식인지 확인

User Contributed Notes

robsonvnasc at gmail dot com 22-Sep-2016 12:30
It also works with Traits!

<?php
trait Foo {}
class_alias("Foo","Bar");
echo trait_exists("Bar") ? 'yes' : 'no';
?>
//yes
stanislav dot eckert at vizson dot de 15-Sep-2015 04:00
class_alias() creates aliases only for user defined classes, not for classes supplied by PHP (PHP will show the warning "First argument of class_alias() must be a name of user defined class"). To create aliases for every kind of classes, use namespaces:

<?php

// Does not work
class_alias("ZipArchive", "myZip");

// Creates class alias "myZip" of class "ZipArchive"
use \ZipArchive as myZip;

?>
petar dot karan dot pk at gmail dot com 06-Feb-2014 11:19
If you need the same function in PHP version < 5.3 you can use this

<?php
function class_alias($original,$alias) {
    $newclass = create_function('','class '.$alias.' extends '.$original.' {}');
    $newclass();
}
?>
mweierophinney at gmail dot com 06-Mar-2013 11:05
class_alias() gives you the ability to do conditional imports.

Whereas the following will not work:

<?php
namespace Component;

if (version_compare(PHP_VERSION, '5.4.0', 'gte')) {
    use My\ArrayObject;
} else {
    use ArrayObject;
}

class Container extends ArrayObject
{
}
?>

the following, using class_alias, will:

<?php
namespace Component;

if (version_compare(PHP_VERSION, '5.4.0', 'lt')) {
    class_alias('My\ArrayObject', 'Component\ArrayObject');
} else {
    class_alias('ArrayObject', 'Component\ArrayObject');
}

class Container extends ArrayObject
{
}
?>

The semantics are slightly different (I'm now indicating that Container extends from an ArrayObject implementation in the same namespace), but the overall idea is the same: conditional imports.
sergey dot karavay at gmail dot com 25-Feb-2013 08:08
At first, you might wonder that:
<?php class A {}; class_alias('A', 'B'); ?>

is equivalent to:
<?php class A {}; class B extends A {}; ?>

class_alias is NOT equivalent to class extending! Private methods/properties are unseen in child classes, but in alias classes they are.
Anonymous 13-Feb-2013 02:42
Like class_alias but for functions:

<?php
function function_alias ($original, $alias) {

  $args = func_get_args();
  assert('count($args) == 2', 'function_alias() require exactly two arguments');
  assert('is_string($original) && is_string($alias)', 'function_alias() require string arguments');

  // valid function name - http://php.net/manual/en/functions.user-defined.php
  assert('preg_match(\'/^[a-zA-Z_\x7f-\xff][\\\\\\\\a-zA-Z0-9_\x7f-\xff]*$/\', $original) > 0',
    "'$original' is not a valid function name");
  assert('preg_match(\'/^[a-zA-Z_\x7f-\xff][\\\\\\\\a-zA-Z0-9_\x7f-\xff]*$/\', $alias) > 0',
    "'$alias' is not a valid function name");

  $aliasNamespace = substr($alias, 0, strrpos($alias, '\\') !== false ? strrpos($alias, '\\') : 0);
  $aliasName = substr($alias, strrpos($alias, '\\') !== false ? strrpos($alias, '\\') + 1 : 0);
  $serializedOriginal = var_export($original, true);

  eval("
    namespace $aliasNamespace {
      function $aliasName () {
        return call_user_func_array($serializedOriginal, func_get_args());
      }
    }
  ");

}

?>

Until hopefully function_alias is added to php...
adam at adamhahn dot com 06-Sep-2011 09:13
Something to note,

If the $original class has not yet been defined or loaded, the auto loader will be invoked in order to try and load it.

If the class for which you are trying to create an alias does not exist, or can not be loaded with the auto loader, you will generate a PHP Warning.
programmer-comfreek at hotmail dot com 15-Aug-2011 06:38
If you defined the class 'original' in a namespace, you will have to specify the namespace(s), too:
<?php
namespace ns1\ns2\ns3;

class A {}

class_alias('ns1\ns2\ns3\A', 'B');
/* or if you want B to exist in ns1\ns2\ns3 */
class_alias('ns1\ns2\ns3\A', 'ns1\ns2\ns3\B');
?>
nicolas dot grekas+php at gmail dot com 31-Dec-2010 10:09
At first, you might wonder that:
<?php class A {}; class_alias('A', 'B'); ?>

is equivalent to:
<?php class A {}; class B extends A {}; ?>

BUT when derivation creates a new class name - that means, you can then instantiate a new kind of objects - aliasing is just what it says: a synonym, so objects instantiated with the aliased name are of the exact same kind of objects instantiated with the non-aliased name.

See this code for example:
<?php
class A {};
class B1 extends A {};
class_alias('A', 'B2');

$b1 = new B1; echo get_class($b1); // prints B1
$b2 = new B2; echo get_class($b2); // prints A !
?>
nicolas dot grekas+php at gmail dot com 30-Dec-2010 11:41
class_alias also works for interfaces!

<?php
interface foo {}
class_alias('foo', 'bar');
echo interface_exists('bar') ? 'yes!' : 'no'; // prints yes!
?>
paul [dot] kotets [at] gmail [dot] com 03-Sep-2009 12:43
This function will appear in PHP 5.3 (at least I can use it with PHP 5.3, build Aug 7 2009 08:21:14)
For older versions of PHP I wrote the next function:

<?php
if (!function_exists('class_alias')) {
    function class_alias($original, $alias) {
        eval('abstract class ' . $alias . ' extends ' . $original . ' {}');
    }
}
?>

Keyword 'abstract' is used for classes, which defines abstract methods.
This function is used in autoload purposes (when I extend classes), so abstract keyword doesn't broke anything for me.


클래스가 정의되었는지 확인

  • ≪ class_alias
  • get_called_class ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 클래스가 정의되었는지 확인

class_exists

(PHP 4, PHP 5, PHP 7)

class_exists — 클래스가 정의되었는지 확인

설명

bool class_exists ( string $class_name [, bool $autoload = true ] )

주어진 클래스가 정의되었는지 확인합니다.

인수

class_name

클래스명. 이름은 대소문자를 구분하지 않습니다.

autoload

__autoload로 호출할 지 여부. 기본값은 TRUE.

반환값

class_name이 정의된 클래스이면 TRUE, 아니면 FALSE를 반환합니다.

변경점

버전 | 설명
5.0.2 | 정의된 인터페이스에 대해서 TRUE를 반환하지 않습니다. interface_exists()를 사용하십시오.
5.0.0 | autoload 인수 추가.

예제

Example #1 class_exists() 예제

<?php
// 클래스를 사용하기 전에 존재하는지 확인합니다
if (class_exists('MyClass')) {
    $myclass = new MyClass();
}

?>

Example #2 autoload 인수 예제

<?php
function __autoload($class)
{
    include($class . '.php');

    // 선언된 클래스를 포함했는지 확인합니다
    if (!class_exists($class, false)) {
        trigger_error("Unable to load class: $class", E_USER_WARNING);
    }
}

if (class_exists('MyClass')) {
    $myclass = new MyClass();
}

?>

참고

  • function_exists() - Return TRUE if the given function has been defined
  • interface_exists() - 인터페이스가 정의되었는지 확인
  • get_declared_classes() - 선언된 클래스명을 배열로 반환

User Contributed Notes

rn at alpha9marketing dot com 07-Apr-2014 02:55
Beware: class_exists is case-INsensitive, as is class instantiation.

php > var_dump(class_exists("DomNode"));
bool(true)
php > var_dump(class_exists("DOMNode"));
bool(true)
php > var_dump(class_exists("DOMNodE"));
bool(true)
php > $x = new DOMNOdE();
php > var_dump(get_class($x));
string(7) "DOMNode"

(tested with PHP 5.5.10 on Linux)

This can cause some headaches in correlating class names to file names, especially on a case-sensitive file system.
spam at wikicms dot org 09-Dec-2013 11:43
Hi guys!
Be careful  and don't forget about second boolean argument $autoload (TRUE by default) when check exists class after spl_autoload_register. Propose short example
file second.php
<?php
class Second {}
?>
file index.php
<?php
class First
{
    function first($class, $bool) {
        spl_autoload_register( function($class) {
            require strtolower($class) . '.php';
        });
        echo class_exists($class, $bool)?'Exist!!!!':'Not exist!';
    }
}

new First($class = 'Second', $bool = true); //Exist!!!!
new First($class = 'Second', $bool = false); //Not exist!
?>
Because __autoload executing much earlier than boolean returned, imho..
giunta dot gaetano at gmail dot com 01-Nov-2013 02:28
If you are using aliasing to import namespaced classes, take care that class_exists will not work using the short, aliased class name - apparently whenever a class name is used as string, only the full-namespace version can be used

use a\namespaced\classname as coolclass;

class_exists( 'coolclass' ) => false
toocoolone at gmail dot com 14-Feb-2012 03:47
I'm running PHP 5.3.4 on Windows 7 and had some difficulty autoloading classes using class_exists(). In my case, when I checked for the class and it didn't exist, class_exists automatically threw a system Exception. I was also throwing my own exception resulting in an uncaught exception.

<?php
/**
 * Set my include path here
 */
$include_path = array( '/include/this/dir', '/include/this/one/too' );
set_include_path( $include_path );
spl_autoload_register();
/**
 * Assuming I have my own custom exception handler (MyException) let's
 * try to see if a file exists.
 */
try {
    if( ! file_exists( 'myfile.php' ) ) {
        throw new MyException('Doh!');
    }
    include( 'myfile.php' );
}
catch( MyException $e ) {
    echo $e->getMessage();
}
/**
 * The above code either includes myfile.php or throws the new MyException
 * as expected. No problem right? The same should be true of class_exists(),
 * right? So then...
 */
$classname = 'NonExistentClass';
try {
    if( ! class_exists( $classname ) ) {
        throw new MyException('Double Doh!');
    }
    $var = new $classname();
}
catch( MyException $e ) {
    echo $e->getMessage();
}
/**
* Should throw a new instance of MyException. But instead I get an
* uncaught LogicException blah blah blah for the default Exception
* class AND MyException. I only catch MyException so we've got on
* uncaught resulting in the dreaded LogicException error.
*/
?>

By registering an additional autoload handler function that did nothing, I was able to stop throwing the extra Exception and only throw my own.

<?php
/**
 * Set my include path here
 */
$include_path = array( '/include/this/dir', '/include/this/one/too' );
set_include_path( $include_path );
spl_autoload_register();
spl_autoload_register( 'myAutoLoad' ); // Add these two and no worries...
function myAutoLoad() {}
/**
 * By registering the additional custom autoload function that does nothing
 * class_exists() returns only boolean and does NOT throw an uncaught Exception
 */
?>

Found this buried in some search results. I don't remember the page URL but if it would have been here it might have saved me some time!
Klaus 28-Apr-2010 01:21
If you recursively load several classes inside an autoload function (or mix manual loading and autoloading), be aware that class_exists() (as well as get_declared_classes()) does not know about classes previously loaded during the *current* autoload invocation.

Apparently, the internal list of declared classes is only updated after the autoload function is completed.
azrael dot com at gmail dot com 12-Dec-2008 01:14
If spl_autoload_register() had been called, then function will try autoload class if it does not exists.

Use instead
<?php
in_array($class_name, get_declared_classes());
?>
Radek @ cz 06-May-2008 03:43
If you want to combat many class includes effectively, define your own autoloader function and spl_autoload_register() that autoloader.
richard at richard-sumilang dot com 27-Mar-2008 09:56
[ >= PHP 5.3]

If you are checking if a class exists that is in a specific namespace then you have to pass in the full path to the class:

echo (class_exists("com::richardsumilang::common::MyClass")) ? "Yes" : "No";
anonymous at somewhere dot tld 17-Jul-2003 09:20
If you have a directory of classes you want to create. (Modules in my instance)... you can do it like that

<?php
if (is_dir($this->MODULE_PATH) && $dh = opendir($this->MODULE_PATH)) {
   while (($file = readdir($dh)) !== false) {
      if (preg_match("/(Mod[a-zA-Z0-9]+).php/", $file, $matches)>0) {
         // include and create the class
         require_once($this->MODULE_PATH."/".$file);
         $modules[] = new $matches[1]();
      }
   }
} else {
   exit;
}
?>

//---
Here the rule is that all modules are on the form
ModModulename.php and that the class has the same name as the file.
The $modules array has all the classes initialized after this code


"Late Static Binding" 클래스명

  • ≪ class_exists
  • get_class_methods ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • "Late Static Binding" 클래스명

get_called_class

(PHP 5 >= 5.3.0, PHP 7)

get_called_class — "Late Static Binding" 클래스명

설명

string get_called_class ( void )

정적 메쏘드가 호출된 클래스명을 얻습니다.

반환값

클래스명을 반환합니다. 클래스 밖에서 호출하면 FALSE를 반환합니다.

예제

Example #1 get_called_class() 사용하기

<?php

class foo {
    static public function test() {
        var_dump(get_called_class());
    }
}

class bar extends foo {
}

foo::test();
bar::test();

?>

위 예제의 출력:

string(3) "foo"
string(3) "bar"

참고

  • get_parent_class() - 객체나 클래스의 부모 클래스명을 얻습니다
  • get_class() - 객체의 클래스명을 반환
  • is_subclass_of() - 이 클래스가 부모 클래스의 자식인지 확인

User Contributed Notes

Safak Ozpinar 24-Sep-2014 04:00
As of PHP 5.5 you can also use "static::class" to get the name of the called class.

<?php
class Bar {
    public static function test() {
        var_dump(static::class);
    }
}

class Foo extends Bar {

}

Foo::test();
Bar::test();
?>

Output:

string(3) "Foo"
string(3) "Bar"
luc at s dot illi dot be 06-Feb-2013 12:09
get_called_class() in closure-scopes:

<?PHP
    ABSTRACT CLASS Base
    {
        protected static $stub = ['baz'];

        //final public function boot()
        static public function boot()
        {
            print __METHOD__.'-> '.get_called_class().PHP_EOL;

            array_walk(static::$stub, function()
            {
                print __METHOD__.'-> '.get_called_class().PHP_EOL;
            });
        }

        public function __construct()
        {
            self::boot();
            print __METHOD__.'-> '.get_called_class().PHP_EOL;

            array_walk(static::$stub, function()
            {
                print __METHOD__.'-> '.get_called_class().PHP_EOL;
            });
        }
    }

    CLASS Sub EXTENDS Base
    {
    }

    // static boot
        Base::boot(); print PHP_EOL;
            // Base::boot        -> Base
            // Base::{closure}    -> Base

        Sub::boot(); print PHP_EOL;
            // Base::boot        -> Sub
            // Base::{closure}    -> Base

        new sub;
            // Base::boot        -> Sub
            // Base::{closure}    -> Base
            // Base->__construct    -> Sub
            // Base->{closure}    -> Sub

    // instance boot
        new sub;
            // Base->boot        -> Sub
            // Base->{closure}    -> Sub
            // Base->__construct    -> Sub
            // Base->{closure}    -> Sub
?>
uebele 06-Apr-2011 06:14
SEE: http://php.net/manual/en/language.oop5.late-static-bindings.php

I think it is worth mentioning on this page, that many uses of the value returned by get_called_function() could be handled with the new use of the old keyword static, as in
<?php
static::$foo;
?>

versus
<?php
$that=get_called_class();
$that::$foo;
?>

I had been using $that:: as my conventional replacement for self:: until my googling landed me the url above.  I have replaced all uses of $that with static with success both as
<?php
static::$foo; //and...
new static();
?>

Since static:: is listed with the limitation: "Another difference is that static:: can only refer to static properties." one may still need to use a $that:: to call static functions; though I have not yet needed this semantic.
webmaster at easy-coding dot de 12-Mar-2010 05:03
if you cannot avoid that carriage return characters will be used as windows linebreaks, you should set

<?php ini_set("auto_detect_line_endings", 1); ?>

otherwise the solutions posted here will fail.
Abhi Beckert 29-Oct-2009 05:41
Beware that this does not behave as expected if your method is not declared as static! For example:

<?php

class foo {
  static public function test() {
    var_dump(get_called_class());
  }

  public function testTwo() {
    var_dump(get_called_class());
  }
}

class bar extends foo {
}

class abc {
  function test() {
    foo::test();
    bar::test();
  }

  function testTwo() {
    foo::testTwo();
    bar::testTwo();
  }
}

echo "basic\n";
foo::test();
bar::test();

echo "basic without static declaration\n";
foo::testTwo();
bar::testTwo();

echo "in a class\n";
$abc = new abc();
$abc->test();

echo "in a class without static declaration\n";
$abc->testTwo();

?>

The result is:

basic
string 'foo'
string 'bar'

basic without static declaration
string 'foo'
string 'bar'

in a class
string 'foo'
string 'bar'

in a class without static declaration
string 'abc'
string 'abc'
php at itronic dot at 07-Aug-2009 03:53
If you call a static getInstance() function to create a instance of a class from another class, this function have to be static, if it is not static the original name of the caller class and not of the current class get returned.

example:

<?php

class a {
  function getXName() {
     return x::getClassName();
  }
  function getXStaticName() {
     return x::getStaticClassName();
  }

}

class b extends a {
}

class x {
  public function getClassName() {
    return get_called_class();
  }
  public static function getStaticClassName() {
    return get_called_class();
  }
}

$a = new a();

$b = new b();

echo $a->getXName(); // will return "a"
echo $b->getXName(); // will return "b"

echo $a->getXStaticName(); // will return "x"
echo $b->getXStaticName(); // will return "x"

?>
a dot cudbard-bell at sussex dot ac dot uk 21-Feb-2009 07:03
Here's a simple way of getting the inheritance tree of a class, no matter which class the function was actually defined in. Will work as a static function method too.

<?php
class A {
    public function get_class_tree(){
        $cur_class = get_called_class();
        do {
            echo $cur_class;
        }
        while($cur_class = get_parent_class($cur_class));
    }
}

class B {

}

class C {

}

$foo = new C();
$foo->get_class_tree();

?>

CBA
danbettles at yahoo dot co dot uk 08-Oct-2008 09:00
It is possible to write a completely self-contained Singleton base class in PHP 5.3 using get_called_class.

<?php

abstract class Singleton {

    protected function __construct() {
    }

    final public static function getInstance() {
        static $aoInstance = array();

        $calledClassName = get_called_class();

        if (! isset ($aoInstance[$calledClassName])) {
            $aoInstance[$calledClassName] = new $calledClassName();
        }

        return $aoInstance[$calledClassName];
    }

    final private function __clone() {
    }
}

class DatabaseConnection extends Singleton {

    protected $connection;

    protected function __construct() {
        // @todo Connect to the database
    }

    public function __destruct() {
        // @todo Drop the connection to the database
    }
}

$oDbConn = new DatabaseConnection();  // Fatal error

$oDbConn = DatabaseConnection::getInstance();  // Returns single instance
?>


클래스 메쏘드명을 얻습니다

  • ≪ get_called_class
  • get_class_vars ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 클래스 메쏘드명을 얻습니다

get_class_methods

(PHP 4, PHP 5, PHP 7)

get_class_methods — 클래스 메쏘드명을 얻습니다

설명

array get_class_methods ( mixed $class_name )

클래스 메쏘드명을 얻습니다.

인수

class_name

클래스명이나 객체 인스턴스

반환값

class_name로 지정한 클래스에 정의되어 있는 메쏘드명의 배열을 반환합니다. 오류가 발생하면, NULL을 반환합니다.

변경점

버전 | 설명
5.0.0 | PHP 5부터, 메쏘드 명을 선언한 대로(대소문자 구분) 반환합니다. PHP 4에서는 소문자였습니다.
4.0.6 | 객체를 지정할 수 있게 되었습니다.

예제

Example #1 get_class_methods() 예제

<?php

class myclass {
    // 생성자
    function myclass()
    {
        return(true);
    }

    // 메쏘드 1
    function myfunc1()
    {
        return(true);
    }

    // 메쏘드 2
    function myfunc2()
    {
        return(true);
    }
}

$class_methods = get_class_methods('myclass');
// 또는
$class_methods = get_class_methods(new myclass());

foreach ($class_methods as $method_name) {
    echo "$method_name\n";
}

?>

위 예제의 출력:

myclass
myfunc1
myfunc2

참고

  • get_class() - 객체의 클래스명을 반환
  • get_class_vars() - 클래스의 기본 프로퍼티를 얻습니다
  • get_object_vars() - 주어진 객체의 프로퍼티를 얻습니다

User Contributed Notes

polarglow06 at gmail dot com 19-Nov-2015 03:09
I have created a very simple test runner using this function

function get_bar($text) {
    $bar = "";
    for($i=1; $i<=strlen($text); $i++) {
        $bar .= "=";
    }
    return $bar;
}
class Tester {
    function __construct() {
        $this->run_tests();
    }
    // run the tests
    function run_tests() {
        print("Tester by Minhajul Anwar \n");
        $class = get_class($this);
        $test_methods = preg_grep('/^test/', get_class_methods($this));
        foreach($test_methods as $method) {
            $start_rep = "test: $class::$method";
            $bar = get_bar($start_rep);
            print("\n$start_rep\n$bar\n");
            $this->$method();
            print("\n");
        }
    }
}

now you just need to write your test class with tegst methods prefixed by 'test', and then just instantiate object of that test class of your, all those tests methods will get run automatically
The drawback is: your test methods must not accept any arguments

an example:
require '../autoload.php';
register_autoload_paths(realpath('./'));

class Test_Test extends Tester {
    function test_something() {
        print("method got executed");
    }
    function testAnotherThing() {
    print("another test method");
    }
}

$Test = new Test_Test();
fschmengler at sgh-it dot eu 08-Jan-2010 01:40
It should be noted that the returned methods are dependant on the current scope. See this example:

<?php
class C
{
    private function privateMethod()
    {

    }
    public function publicMethod()
    {

    }
    public function __construct()
    {
        echo '$this:';
        var_dump(get_class_methods($this));
        echo 'C (inside class):';
        var_dump(get_class_methods('C'));
    }
}
$c = new C;
echo '$c:';
var_dump(get_class_methods($c));
echo 'C (outside class):';
var_dump(get_class_methods('C'));
?>

Output:

$this:
array
  0 => string 'privateMethod' (length=13)
  1 => string 'publicMethod' (length=12)
  2 => string '__construct' (length=11)

C (inside class):
array
  0 => string 'privateMethod' (length=13)
  1 => string 'publicMethod' (length=12)
  2 => string '__construct' (length=11)

$c:
array
  0 => string 'publicMethod' (length=12)
  1 => string '__construct' (length=11)

C (outside class):
array
  0 => string 'publicMethod' (length=12)
  1 => string '__construct' (length=11)
php at stock-consulting dot com 06-Feb-2007 11:40
Note that this function will answer both class AND instance methods ("class methods" are called "static" in PHP). Sort of a little "trap" for people who have in-depth experience with the OO terminology :-)
BoD 15-Mar-2006 04:09
!Concerning PHP5 Only!

If you want to get all methods/functions from a class you can do this with the get_class_methods function.
<?php
    $arrMethodNames = get_class_methods ( $ClassNameOrObject);
?>
However the drawback on this function in PHP5 is that you will NOT receive protected and private methods of a class/object if you are calling the method from another class/object context.

If you want to receive all methods of a given Class in another Class you should use the PHP5 Reflection API. The following source allows to retrieve all methods from a derived class in its (abstract) Base Class.

In the example you need to call the base constructor from the derived classes constructor in order to let the base class know the name of the derived class. Use the "__CLASS__" definition for passing the classname of current class to its base class.

<?php

    // Base Class - Abstract
    abstract class A {

        // Array of ReflectionMethod Objects
        // is being set upon instanciation
        // derived Classes don't need to know about this array
        private $arrMethods;

        // Constructor which MUST be called from derived Class Constructor
        protected function __construct ( $strDerivedClassName ) {
            $oRefl = new ReflectionClass ( $strDerivedClassName );
            if ( is_object($oRefl) ) {
                $this->arrMethods = $oRefl->getMethods();
            }
        }

        // Some abstract function
        abstract protected function D ();

        // Some private function
        private function E() {
        }

        // method to print all class/object methods
        // must be callable from derived classes
        // can be protected if only for internal class use
        public function PrintAllMethods () {
            foreach ( $this->arrMethods as $curReflectionMethod ) {
                echo $curReflectionMethod->getName()."<br>";
            }
        }
    }

    // Derived Class
    class B extends A {

        // Constructor for this class
        // MUST call Base Constructor
        public function __construct () {
            // Call Base Constructor
            parent::__construct(__CLASS__);
        }

        // Some public function
        public function A () {
        }

        // some protected function
        protected function B () {
        }

        // some private function
        private function C() {
        }

        // some abstract method from base class implemented
        protected function D () {
        }
    }

    // Create new B Object
    $b = new B();
    // Print all Methods of this Object/Class
    $b->PrintAllMethods();
?>

In this example output will be:

__construct
A
B
C
D
E
PrintAllMethods

As you can see these are all methods from class B and also all methods from Class A respectivly in order of their declaration. Note that only one method "__construct" and only one method "D" are being shown. This is due to overloading (__construct) and implementing (D) in PHP.

In this example any further method-handling methods should be implemented in the base class as these will be available in derived classes as well. Just make sure that you use the right access specifiers for these additional methods ( private, protected, public )

BoD
jazepstein at greenash dot net dot au 16-Oct-2005 03:37
In PHP4, this function converts its return values to lowercase; but in PHP5, it leaves the return values in their original case. This can cause serious problems when trying to write code that dynamically calls a class method, and that works in both PHP4 and PHP5. This code snippet shows one way of achieving compatibility with both versions:

<?php
// Example variables - these would be dynamic in a real application.
$className = 'SomeClass';
$methodName= 'someMethod';
$args = array('arg1', 'arg2');

// Use array_map() and strtolower() to make all values returned by get_class_methods() lowercase. PHP4 does this anyway - now it will happen regardless of the PHP version being used.
$classMethods = array_map(strtolower, get_class_methods($className));

// in_array() can only handle being given an array.
if (!$classMethods) {
  $classMethods = array();
}

if (in_array(strtolower($methodName), $classMethods)) {
  // call some method
  return call_user_func_array(array($className, $methodName), $args);
}
?>
Oli Filth 11-Apr-2005 05:21
As an extension to onesimus's code below for finding inherited methods, in PHP 5 you can use the Reflection API to find which of these are overriden.

e.g.

<?php
function get_overriden_methods($class)
{
    $rClass = new ReflectionClass($class);
    $array = NULL;

    foreach ($rClass->getMethods() as $rMethod)
    {
        try
        {
            // attempt to find method in parent class
            new ReflectionMethod($rClass->getParentClass()->getName(),
                                $rMethod->getName());
            // check whether method is explicitly defined in this class
            if ($rMethod->getDeclaringClass()->getName()
                == $rClass->getName())
            {
                // if so, then it is overriden, so add to array
                $array[] .=  $rMethod->getName();
            }
        }
        catch (exception $e)
        {    /* was not in parent class! */    }
    }

    return $array;
}
?>
kabatak 20-Feb-2005 02:11
In PHP4, if you need to get_class_methods in their original case. You can use this simple function I created.

// Note: this function assumes that you only have 1 class in 1 file

$file = "path/to/myclass.php"

function file_get_class_methods ($file)
{
    $arr = file($file);
    foreach ($arr as $line)
    {
        if (ereg ('function ([_A-Za-z0-9]+)', $line, $regs))
            $arr_methods[] = $regs[1];
    }
    return $arr_methods;
}
epowell at removethis dot visi dot com 11-Oct-2004 05:47
I've figured out how to get around my issue described below, using the Reflection API.

<?
/* Pass the name of the class, not a declared handler */
function get_public_methods($className) {
    /* Init the return array */
    $returnArray = array();

    /* Iterate through each method in the class */
    foreach (get_class_methods($className) as $method) {

        /* Get a reflection object for the class method */
        $reflect = new ReflectionMethod($className, $method);

        /* For private, use isPrivate().  For protected, use isProtected() */
        /* See the Reflection API documentation for more definitions */
        if($reflect->isPublic()) {
            /* The method is one we're looking for, push it onto the return array */
            array_push($returnArray,$method);
        }
    }

    /* return the array to the caller */
    return $returnArray;
}
?>
onesimus at cox dot net 19-Jun-2004 11:32
This function will return only the methods for the object you indicate. It will strip out the inherited methods.

function get_this_class_methods($class){
    $array1 = get_class_methods($class);
    if($parent_class = get_parent_class($class)){
        $array2 = get_class_methods($parent_class);
        $array3 = array_diff($array1, $array2);
    }else{
        $array3 = $array1;
    }
    return($array3);
}
gk at proliberty dot com 02-Jun-2003 01:16
It is important to note that get_class_methods($class) returns not only methods defined by $class but also the inherited methods.

There does not appear to be any PHP function to determine which methods are inherited and which are defined explicitly by a class.
aldo at cerca dot com 22-Apr-2002 07:18
If you use "get_class_methods" to check if a Method is in a Class remember that the function return lowered name of class methods:

class classPippo
{
        function DummyFunct()
        {
                // Do nothing...
        }
}

$aClassMethods = get_class_methods(classPippo);

$sMethodName = 'DummyFunct';

// This not work...

        if (in_array($sMethodName, $aClassMethods))
        classPippo::DummyFunct();

// This work...

        if (in_array(strtolower($sMethodName), $aClassMethods))
        classPippo::DummyFunct();
matt at zevi dot net 22-Mar-2002 01:46
Win32 only:

It's probably worth noting here that you can't get the methods of an object created by the built-in 'COM' class. ie - this won't work:

$word = new COM('Word.Application');
$methods = get_class_methods(get_class($word));
print_r($methods);

Matt


클래스의 기본 프로퍼티를 얻습니다

  • ≪ get_class_methods
  • get_class ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 클래스의 기본 프로퍼티를 얻습니다

get_class_vars

(PHP 4, PHP 5, PHP 7)

get_class_vars — 클래스의 기본 프로퍼티를 얻습니다

설명

array get_class_vars ( string $class_name )

주어진 클래스의 기본 프로퍼티를 얻습니다.

인수

class_name

클래스명

반환값

클래스의 기본 public 프로퍼티의 연관 배열을 반환합니다. 반환하는 배열 원소는 varname => value 형식입니다.

변경점

버전 | 설명
4.2.0 이전 | 초기화되지 않은 클래스 변수는 get_class_vars()가 반환하지 않습니다.

예제

Example #1 get_class_vars() 예제

<?php

class myclass {

    var $var1; // 기본값이 없습니다...
    var $var2 = "xyz";
    var $var3 = 100;
    private $var4; // PHP 5

    // 생성자
    function myclass() {
        // 몇몇 프로퍼티를 변경
        $this->var1 = "foo";
        $this->var2 = "bar";
        return true;
    }

}

$my_class = new myclass();

$class_vars = get_class_vars(get_class($my_class));

foreach ($class_vars as $name => $value) {
    echo "$name : $value\n";
}

?>

위 예제의 출력:

// PHP 4.2.0 이전
var2 : xyz
var3 : 100

// PHP 4.2.0부터
var1 :
var2 : xyz
var3 : 100

참고

  • get_class_methods() - 클래스 메쏘드명을 얻습니다
  • get_object_vars() - 주어진 객체의 프로퍼티를 얻습니다

User Contributed Notes

flobee 16-Nov-2015 09:43
<?php
class someClass {
    public function toArray() {
        $records = array();

        foreach( $this as $key => $value ) {
                $records[$key] = $value;
        }

        return $records;
    }

}
?>
bof at bof dot de 09-Sep-2012 04:02
I needed to get only the class static variables, leaving out instance variables.

<?php
function get_static_vars($class) {
    $result = array();
    foreach (get_class_vars($class) as $name => $default)
        if (isset($class::$$name))
            $result[$name] = $default;
    return $result;
}
?>

That function returns only the public ones. The same pattern can be used inside a class, then it returns private and protected static variables, too:

<?php
static protected function get_static_vars($class = NULL) {
    if (!isset($class)) $class = get_called_class();
    $result = array();
    foreach (get_class_vars($class) as $name => $default)
        if (isset($class::$$name))
            $result[$name] = $default;
    return $result;
}
?>
Mattias Ahlbck 15-Feb-2012 06:39
get_class_vars_assoc()
- Returns an associative array with name of (parent) class(es) as key(s) and corresponding class vars as sub-arrays. My boilerplate for some crude O/R mapping.

Note: vars re-defined in sub-classes are ignored.

<?php

class GrandClass {
    public $grandVar;
    public $in_grand_and_parent;
    public $in_grand_and_child;

    public static function load() {
        print_r(self::get_class_vars_assoc());
    }

    protected static function get_class_vars_assoc() {
        $called = get_called_class();
        //echo "called: $called \n";
        $classVars[$called] = array_keys(get_class_vars($called));

        $parent = get_parent_class($called);
        while ( $parent !== FALSE ) {
            //echo "parent: $parent \n";
            $classVars[$parent] = array_keys(get_class_vars($parent));
            $classVars[$called] = array_diff($classVars[$called],
                                             $classVars[$parent]);
            if ( isset($prevParentVars) ) {
                $prevParentVars = array_diff($prevParentVars,
                                             $classVars[$parent]);
            }

            $prevParentVars = &$classVars[$parent];
            $parent = get_parent_class($parent);
        }

        return $classVars;
    }
}

class ParentClass extends GrandClass {
    public $in_grand_and_parent;
    public $parentVar;
    public $in_parent_and_child;
}

class ChildClass extends ParentClass {
    public $in_grand_and_child;
    public $in_parent_and_child;
    public $childVar;
}

ChildClass::load();

?>

Array
(
    [ChildClass] => Array
        (
            [2] => childVar
        )

    [ParentClass] => Array
        (
            [1] => parentVar
            [2] => in_parent_and_child
        )

    [GrandClass] => Array
        (
            [0] => grandVar
            [1] => in_grand_and_parent
            [2] => in_grand_and_child
        )

)
ciantic 31-Jan-2012 07:06
I propse following for getting Public members, always:
<?PHP
if (!function_exists("get_public_class_vars")) {
    function get_public_class_vars($class) {
        return get_class_vars($class);
    }
}
if (!function_exists("get_public_object_vars")) {
    function get_public_object_vars($object) {
        return get_object_vars($object);
    }
}
?>

This is to mitigate the problem and a feature that get_object_vars($this) returns private members. Running it simply outside the scope will get the public.

Iterating public members only and their defaults are enormously useful in e.g. in serialization classes such as options where each public member is an serializable that is saved and loaded.
pBakhuis at Gmail dot com 07-Oct-2011 12:08
If you need the variables before the class is included this function does not seem to work.
Use a reflection class to get around this.
http://www.php.net/reflectionclass
ken at verango dot com 04-Oct-2010 01:50
All 3 of get_object_vars, get_class_vars and reflection getDefaultProperties will reveal the name of the array.  For serialization I recommend:

<?php
$cName = get_class($this);
$varTemplate= get_class_vars($cName)
foreach ($varTemplate as $name => $defaultVal) {
  $vars[$name] = $this->$name; // gets actual val.
}
?>

No scan the $vars and create serialization string how you wish.

This protects against erroneous prior deserializing in maintaining the integrity of the class template and ignoring unintended object properties.
ianitsky at gmail dot com 10-Nov-2009 08:53
If you need get the child protected/private vars ignoring the parent vars, use like this:

<?php
class childClass extends parentClass {
    private $login;
    private $password;

    public function __set($key, $val) {
        if ($key == 'password')
            $this->$key = md5($val);
        else
            $this->$key = $val;
    }
}
class parentClass {
    public $name;
    public $email;

    function __construct() {
        $reflection = new ReflectionClass($this);
        $vars = array_keys($reflection->getdefaultProperties());
        $reflection = new ReflectionClass(__CLASS__);
        $parent_vars = array_keys($reflection->getdefaultProperties());

        $my_child_vars = array();
        foreach ($vars as $key) {
            if (!in_array($key, $parent_vars)) {
                $my_child_vars[] = $key;
            }
        }

        print_r($my_child_vars);
    }
}

$child_class = new childClass();
?>
harmor 21-Dec-2008 12:53
So I wanted to get a list of the public parameters in a child class using a static function pre-5.3.0 (< 5.3.0).  In 5.3.0+ you would use the new 'static' like you would 'self' to get the late binding.

<?php
class childClass extends parentClass
{
    public $id;
    public $name;

    public static function getFields()
    {
        return self::_getFields(__CLASS__);
    }

}
abstract class parentClass
{
    public $idInParent;
    public $nameInParent;

    abstract public static function getFields();

    final protected static function _getFields($className)
    {
        $rtn = array();
        foreach (array_keys(get_class_vars($className)) as $var) {
            $rtn[] = $var;
        }
        return $rtn;
    }

}

var_dump(childClass::getFields());
?>

Results:
array(4) {
  [0]=>
  string(2) "id"
  [1]=>
  string(4) "name"
  [2]=>
  string(10) "idInParent"
  [3]=>
  string(12) "nameInParent"
}
artktec at art-k-tec dot com 16-Nov-2007 06:18
There seems to be be a function to get constants missing , i.e. get_class_constants() ... so here is a simple function for you all. Hopefully Zend will include this in the next round as a native php call, without using reflection.

<?php
   function GetClassConstants($sClassName) {
      $oClass = new ReflectionClass($sClassName);
      return $oClass->getConstants());
   }
?>
phpnet at stccorp dot net 15-Jun-2007 02:18
This is one of the best php functions. Look at what you can do

class Object
{
   var $updtFields;//keep track of affected values
   function Object($record="") {
       if (is_array($record))
       {
          $this->updtFields = array();
          foreach(array_keys(get_class_vars(get_class($this))) as $k)
          if (isset($record[$k]))
         {
          $this->$k = $record[$k];
          $this->updtFields[] = $k;
         }
      }
   }//end of arrayToObject

   function toDebug($nl='<br>')
   {
       foreach(array_keys(get_class_vars(get_class($this))) as $k)
       echo "$k = [" . $this->$k . "]{$nl}";
    }//end of toDebug
}

Now you can do really cool things. If you have a form like
<form action="" method="post">
  <input type="text" name="name" />
  <input type="text" name="phone" />
  <input type="submit" />
</form>

and you define your class like this
class Person extends Object{
  var $name; //same same as in the form
  var $phone;
}

when you submmit the form, you can get the data like

$person = new Person($_POST);

//everything in just one line,cool!! Also if you use pear db or adodb when you get data from the database you can do the same thing except use the $row that you get from the database. Remember to ask the result is associative mode.

This is my core Object for everthing I do and it works great.
bernd at tiggerswelt dot net 29-Jan-2007 11:52
If you assign a constant value using the self-scope by default to a variable, get_class_vars() will result in a FATAL error.

Example:

<?PHP

  class Foo {
    const Bar = "error";

    public $Foo = self::Bar;
  }

  print_r(get_class_vars("Foo"));

?>

... but using "Foo::Bar" instead "self::Bar" will work ;)
gizmobits at hotmail dot com 04-Mar-2006 04:48
I wanted a simple ToString() function that was automatic and class independent.  I wanted to dump it into any of several classes and get values quickly.  I wanted to leave it there so I could customize it for each class, so an outside function wasn't suitable.  I came up with this and thought it might be useful.  Have fun!

<?php
  function ToString () {
    $s = "";
    $s .= "<table>\n";
    $s .= "<tr><td colspan=2><hr></td></tr>\n";
    foreach (get_class_vars(get_class($this)) as $name => $value) {
      $s .= "<tr><td>$name:</td><td>" . $this->$name . "</td></tr>\n";
    }
    $s .= "<tr><td colspan=2><hr></td></tr>\n";
    $s .= "</table>\n";
    return $s;
  }

?>
php dot net at sharpdreams dot com 25-Oct-2005 03:25
Contrary to multiple comments throughout the manual, get_class_vars() performed within a class can access any public, protected, and private members.

<?php
class Foo {
   public $x;
   protected $y;
   private $z;
   public function __sleep() {
      return( get_class_vars( __CLASS__ ) );
   }
}
?>

works fine (returns x, y, & z). However, given the same class as above,

<?php
print_r( get_class_vars( "Foo" ) );
?>

will NOT return x, y, & z. Instead it will only return the public members (in our case, z).
alan_k at php dot net 22-Jan-2005 04:23
in PHP5 to get all the vars (including private etc.) use:

$reflection = new ReflectionClass($class);
$defaults = $reflection->getdefaultProperties();
rec at NOSPAM dot instantmassacre dot com 24-Jan-2003 01:23
If you want to retrieve the class vars from within the class itself, use $this.

<?php
class Foo {

    var $a;
    var $b;
    var $c;
    var $d;
    var $e;

    function GetClassVars()
    {
        return array_keys(get_class_vars(get_class($this))); // $this
    }

}

$Foo = new Foo;

$class_vars = $Foo->GetClassVars();

foreach ($class_vars as $cvar)
{
    echo $cvar . "<br />\n";
}
?>

Produces, after PHP 4.2.0, the following:

a
b
c
d
e


객체의 클래스명을 반환

  • ≪ get_class_vars
  • get_declared_classes ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 객체의 클래스명을 반환

get_class

(PHP 4, PHP 5, PHP 7)

get_class — 객체의 클래스명을 반환

설명

string get_class ([ object $object ] )

주어진 object의 클래스명을 얻습니다.

인수

object

확인할 객체

반환값

object 인스턴스의 클래스명을 반환합니다. object가 객체가 아니면 FALSE를 반환합니다.

변경점

버전 | 설명
5.0.0부터 | 클래스명을 원 문자대로 반환합니다.
5.0.0부터 | 객체 메쏘드에서 호출할 때, object 인수는 선택적입니다.

예제

Example #1 get_class() 사용하기

<?php

class foo {
    function name()
    {
        echo "My name is " , get_class($this) , "\n";
    }
}

// 객체 만들기
$bar = new foo();

// 외부 호출
echo "Its name is " , get_class($bar) , "\n";

// 내부 호출
$bar->name();

?>

위 예제의 출력:

Its name is foo
My name is foo

Example #2 슈퍼클래스에서 get_class() 사용하기

<?php

abstract class bar {
    public function __construct()
    {
        var_dump(get_class($this));
        var_dump(get_class());
    }
}

class foo extends bar {
}

new foo;

?>

위 예제의 출력:

string(3) "foo"
string(3) "bar"

참고

  • get_parent_class() - 객체나 클래스의 부모 클래스명을 얻습니다
  • gettype() - 변수의 자료형을 얻습니다
  • is_subclass_of() - 이 클래스가 부모 클래스의 자식인지 확인

User Contributed Notes

RQuadling at GMail dot com 20-Dec-2014 12:09
With regard to getting the class name from a namespaced class name, then using basename() seems to do the trick quite nicely.

<?php
namespace Foo\Bar;

abstract class Baz
{
  public function report()
  {
    echo
      '__CLASS__        ', __CLASS__, ' ', basename(__CLASS__), PHP_EOL,
      'get_called_class ', get_called_class(), ' ', basename(get_called_class()), PHP_EOL;
  }
}

class Snafu extends Baz
{
}

(new Snafu)->report();
?>

produces output of ...

__CLASS__        Foo\Bar\Baz   Baz
get_called_class Foo\Bar\Snafu Snafu
jjanak at webperfection dot net 03-Oct-2014 08:06
>= 5.5

::class
fully qualified class name, instead of get_class

<?php
namespace my\library\mvc;

class Dispatcher {}

print Dispatcher::class; // FQN == my\library\mvc\Dispatcher

$disp = new Dispatcher;

print $disp::class; // parse error
Anonymous 19-Sep-2014 05:55
If you want the path to an file if you have i file structure like this

project -> system -> libs -> controller.php
project -> system -> modules -> foo -> foo.php

and foo() in foo.php extends controller() in controller.php like this

<?PHP
namespace system\modules\foo;

class foo extends \system\libs\controller {
    public function __construct() {
        parent::__construct();
    }
}
?>

and you want to know the path to foo.php in controller() this may help you

<?PHP
namespace system\libs;

class controller {
    protected function __construct() {
        $this->getChildPath();
    }
    protected function getChildPath() {
        echo dirname(get_class($this));
    }
}
?>

<?PHP
$f = new foo();  // system\modules\foo
?>
var23rav at gmail dot com 27-Aug-2014 10:52
class A
{
function __construct(){
        //parent::__construct();
        echo $this->m =  'From constructor A: '.get_class();
        echo $this->m =  'From constructor A:- argument = $this: '.get_class($this);

        echo $this->m =  'From constructor A-parent: '.get_parent_class();
        echo $this->m =  'From constructor A-parent:- argument =  $this: '.get_parent_class($this);
    }
}
class B extends A
{
function __construct(){
        parent::__construct();
        echo $this->m =  'From constructor B: '.get_class();
        echo $this->m =  'From constructor B:- argument =  $this: '.get_class($this);

        echo $this->m =  'From constructor B-parent: '.get_parent_class();
        echo $this->m =  'From constructor B-parent:- argument =  $this: '.get_parent_class($this);
    }
}
$b = new B();
//----------------output--------------------

From constructor A: A
From constructor A:- argument = $this: B
From constructor A-parent:
From constructor A-parent:- argument = $this: A
From constructor B: B
From constructor B:- argument = $this: B
From constructor B-parent: A
From constructor B-parent:- argument = $this: A

Use get_class() to get the name of class  ,it will help you get the class name, in case you extend that class with another class and want to get the name of the class to which object is instance of user get_class($object)

when you create an object of class{$b object of B} which has a super class{Class A}.
uses these code IN Super Class {A}
--------------------------------------------
to get class name B{object instance} :  get_class($this)
to get class name A{super class}  : get_class() or get_parent_class($this)
alastair at propcom dot co dot uk 20-May-2014 05:12
Not mentioned are namespaces. get_class() returns the class in a suitable format to be used to look up the class again, i.e. 'Namespace\Class_Name'.

Explode on '\\' to get the components.
dave at shax dot com 07-Mar-2014 01:36
A lot of people in other comments wanting to get the classname without the namespace. Some weird suggestions of code to do that - not what I would've written! So wanted to add my own way.

<?php
function get_class_name($classname)
{
    if ($pos = strrpos($classname, '\\')) return substr($classname, $pos + 1);
    return $pos;
}
?>

Also did some quick benchmarking, and strrpos() was the fastest too. Micro-optimisations = macro optimisations!

39.0954 ms - preg_match()
28.6305 ms - explode() + end()
20.3314 ms - strrpos()

(For reference, here's the debug code used. c() is a benchmarking function that runs each closure run 10,000 times.)

<?php
c(
    function($class = 'a\b\C') {
        if (preg_match('/\\\\([\w]+)$/', $class, $matches)) return $matches[1];
        return $class;
    },
    function($class = 'a\b\C') {
        $bits = explode('\\', $class);
        return end($bits);
    },
    function($class = 'a\b\C') {
        if ($pos = strrpos($class, '\\')) return substr($class, $pos + 1);
        return $pos;
    }
);
?>
Nanhe Kumar 20-Dec-2013 09:14
<?php
class Parent{
}
class Child extends Parent{
}
$c = new Child();
echo get_class($c) //Child
?>
<?php
class Parent{
  public function getClass(){
     echo get_class();
  }
}
class Child extends Parent{
}
$obj = new Child();
$obj->getClass(); //outputs Parent
?>
<?php
class Parent{
  public function getClass(){
     echo get_class($this);
  }
}
class Child extends Parent{
}
$obj = new Child();
$obj->getClass(); // Parent
?>
emmanuel dot antico at gmail dot com 11-May-2013 04:35
/**
 * Obtains an object class name without namespaces
 */
function get_real_class($obj) {
    $classname = get_class($obj);

    if (preg_match('@\\\\([\w]+)$@', $classname, $matches)) {
        $classname = $matches[1];
    }

    return $classname;
}
macnimble at gmail dot com 18-Mar-2012 04:24
Need a quick way to parse the name of a class when it's namespaced? Try this:

<?php
namespace Engine;
function parse_classname ($name)
{
  return array(
    'namespace' => array_slice(explode('\\', $name), 0, -1),
    'classname' => join('', array_slice(explode('\\', $name), -1)),
  );
}
final class Kernel
{
  final public function __construct ()
  {
    echo '<pre>', print_r(parse_classname(__CLASS__),1), '</pre>';
    // Or this for a one-line method to get just the classname:
    // echo join('', array_slice(explode('\\', __CLASS__), -1));
  }
}
new Kernel();
?>

Outputs:
Array
(
    [namespace] => Array
        (
            [0] => Engine
        )

    [classname] => Kernel
)
mail dot temc at gmail dot com 06-Dec-2011 11:51
People seem to mix up what __METHOD__, get_class($obj) and get_class() do, related to class inheritance.

Here's a good example that should fix that for ever:

<?php

class Foo {
 function doMethod(){
  echo __METHOD__ . "\n";
 }
 function doGetClassThis(){
  echo get_class($this).'::doThat' . "\n";
 }
 function doGetClass(){
  echo get_class().'::doThat' . "\n";
 }
}

class Bar extends Foo {

}

class Quux extends Bar {
 function doMethod(){
  echo __METHOD__ . "\n";
 }
 function doGetClassThis(){
  echo get_class($this).'::doThat' . "\n";
 }
 function doGetClass(){
  echo get_class().'::doThat' . "\n";
 }
}

$foo = new Foo();
$bar = new Bar();
$quux = new Quux();

echo "\n--doMethod--\n";

$foo->doMethod();
$bar->doMethod();
$quux->doMethod();

echo "\n--doGetClassThis--\n";

$foo->doGetClassThis();
$bar->doGetClassThis();
$quux->doGetClassThis();

echo "\n--doGetClass--\n";

$foo->doGetClass();
$bar->doGetClass();
$quux->doGetClass();

?>

OUTPUT:

--doMethod--
Foo::doMethod
Foo::doMethod
Quux::doMethod

--doGetClassThis--
Foo::doThat
Bar::doThat
Quux::doThat

--doGetClass--
Foo::doThat
Foo::doThat
Quux::doThat
ozana at omdesign dot cz 28-Nov-2011 04:53
Simplest way how to gets Class without namespace

<?php
namespace a\b\c\d\e\f;

class Foo {

  public function __toString() {
    $class = explode('\\', __CLASS__);
    return end($class);
  }
}

echo new Foo(); // prints Foo
?>
Michael Richey 31-Oct-2011 09:01
Attempting various singleton base class methods described on this page, I have created a base class and bridge function that allows it to work without get_called_class() if it's not available.  Unlike other methods listed here, I chose not to prevent use of __construct() or __clone().

<?php
abstract class Singleton {
    protected static $m_pInstance;
    final public static function getInstance(){
        $class = static::getClass();
        if(!isset(static::$m_pInstance[$class])) {
            static::$m_pInstance[$class] = new $class;
        }
        return static::$m_pInstance[$class];
    }
    final public static function getClass(){
        return get_called_class();
    }
}

// I don't remember where I found this, but this is to allow php < 5.3 to use this method.
if (!function_exists('get_called_class')) {
    function get_called_class($bt = false, $l = 1) {
        if (!$bt)
            $bt = debug_backtrace();
        if (!isset($bt[$l]))
            throw new Exception("Cannot find called class -> stack level too deep.");
        if (!isset($bt[$l]['type'])) {
            throw new Exception('type not set');
        }
        else
            switch ($bt[$l]['type']) {
                case '::':
                    $lines = file($bt[$l]['file']);
                    $i = 0;
                    $callerLine = '';
                    do {
                        $i++;
                        $callerLine = $lines[$bt[$l]['line'] - $i] . $callerLine;
                    } while (stripos($callerLine, $bt[$l]['function']) === false);
                    preg_match('/([a-zA-Z0-9\_]+)::' . $bt[$l]['function'] . '/', $callerLine, $matches);
                    if (!isset($matches[1])) {
                        // must be an edge case.
                        throw new Exception("Could not find caller class: originating method call is obscured.");
                    }
                    switch ($matches[1]) {
                        case 'self':
                        case 'parent':
                            return get_called_class($bt, $l + 1);
                        default:
                            return $matches[1];
                    }
                // won't get here.
                case '->': switch ($bt[$l]['function']) {
                        case '__get':
                            // edge case -> get class of calling object
                            if (!is_object($bt[$l]['object']))
                                throw new Exception("Edge case fail. __get called on non object.");
                            return get_class($bt[$l]['object']);
                        default: return $bt[$l]['class'];
                    }

                default: throw new Exception("Unknown backtrace method type");
            }
    }
}

class B extends Singleton {
}

class C extends Singleton {
}

$b = B::getInstance();
echo 'class: '.get_class($b);

echo '<br />';

$c = C::getInstance();
echo echo 'class: '.get_class($c);
?>
This returns:
class: b
class: c
me at nwhiting dot com 06-Dec-2010 05:14
Method for pulling the name of a class with namespaces pre-stripped.

<?php
/**
 * Returns the name of a class using get_class with the namespaces stripped.
 * This will not work inside a class scope as get_class() a workaround for
 * that is using get_class_name(get_class());
 *
 * @param  object|string  $object  Object or Class Name to retrieve name

 * @return  string  Name of class with namespaces stripped
 */
function get_class_name($object = null)
{
    if (!is_object($object) && !is_string($object)) {
        return false;
    }

    $class = explode('\\', (is_string($object) ? $object : get_class($object)));
    return $class[count($class) - 1];
}
?>

And for everyone for Unit Test goodiness!

<?php
namespace testme\here;

class TestClass {

    public function test()
    {
       return get_class_name(get_class());
    }
}

class GetClassNameTest extends \PHPUnit_Framework_TestCase
{
    public function testGetClassName()
    {
        $class  = new TestClass();
        $std  = new \stdClass();
        $this->assertEquals('TestClass', get_class_name($class));
        $this->assertEquals('stdClass', get_class_name($std));
        $this->assertEquals('Test', get_class_name('Test'));
        $this->assertFalse(get_class_name(null));
        $this->assertFalse(get_class_name(array()));
        $this->assertEquals('TestClass', $class->test());
    }
}
?>
Aaron 18-Jun-2010 08:14
This can sometimes be used in place of get_called_class(). I used this function in a parent class to get the name of the class that extends it.
ovidiu.bute [at] gmail.com 06-Apr-2010 07:23
If you are using namespaces this function will return the name of the class including the namespace, so watch out if your code does any checks for this. Ex:

namespace Shop;

<?php
class Foo
{
  public function __construct()
  {
     echo "Foo";
  }
}

//Different file

include('inc/Shop.class.php');

$test = new Shop\Foo();
echo get_class($test);//returns Shop\Foo
?>
Tom Brown 17-Sep-2009 08:02
As of 5.3.0 this function seems to have started throwing an E_WARNING error if you pass it a non-object. Supress that with "@" if you are actually checking and handling the false return code.
Lanselot 11-Feb-2009 11:09
Beware if you're omitting the parameter on inherited classes.
It'll return the class name of the method where it's called.

<?php
class A {
    function foo() {
      return get_class();
    }
}
class B extends A {
   function bar() {
      return get_class();
   }
}
$instance = new B();
echo $instance->bar(); //Prints 'B';
echo $instance->foo(); //Prints 'A';
?>
danbettles at yahoo dot co dot uk 08-Oct-2008 08:56
It is possible to write a completely self-contained Singleton base class in PHP 5.3 using the new get_called_class function.  When called in a static method, this function returns the name of the class the call was made against.

<?php

abstract class Singleton {

    protected function __construct() {
    }

    final public static function getInstance() {
        static $aoInstance = array();

        $calledClassName = get_called_class();

        if (! isset ($aoInstance[$calledClassName])) {
            $aoInstance[$calledClassName] = new $calledClassName();
        }

        return $aoInstance[$calledClassName];
    }

    final private function __clone() {
    }
}

class DatabaseConnection extends Singleton {

    protected $connection;

    protected function __construct() {
        // @todo Connect to the database
    }

    public function __destruct() {
        // @todo Drop the connection to the database
    }
}

$oDbConn = new DatabaseConnection();  // Fatal error

$oDbConn = DatabaseConnection::getInstance();  // Returns single instance
?>

Full write-up in Oct 2008: http://danbettles.blogspot.com
Maik 04-Oct-2008 05:36
The code just worked fine with PHP 5.3.
Thanks to you it could be rebuild to work with earlier versions of PHP 5.

I just changed the 'static' to 'self' and die() to an Exception.

<?php
// This one extends Singleton with proper class name
class someSingleton extends Singleton {
    public static function getInstance() {
        parent::$__CLASS__ = __CLASS__;
        parent::getInstance();
    }
}
// This one does the logic
abstract class Singleton {
    protected static $__CLASS__ = __CLASS__;
    protected static $instance = null;

    protected function __construct() {}
    abstract protected function init();

    /**
     * Gets an instance of this singleton. If no instance exists, a new instance is created and returned.
     * If one does exist, then the existing instance is returned.
     */
    public static function getInstance() {
        $class = self::getClass();

        if (self::$instance === null) {
            self::$instance = new $class();
            self::$instance->init();
        }

        return self::$instance;
    }

    /**
     * Returns the classname of the child class extending this class
     *
     * @return string The class name
     */
    private static function getClass() {
        $implementing_class = self::$__CLASS__;
        $original_class = __CLASS__;

        if ($implementing_class === $original_class)
            throw new Exception("You MUST provide a <code>protected static \$__CLASS__ = __CLASS__;</code> statement in your Singleton-class!");

        return $implementing_class;
    }
}
?>
Edward 21-Aug-2008 12:26
The code in my previous comment was not completely correct. I think this one is.

<?
abstract class Singleton {
    protected static $__CLASS__ = __CLASS__;

    protected function __construct() {
    }

    abstract protected function init();

    /**
     * Gets an instance of this singleton. If no instance exists, a new instance is created and returned.
     * If one does exist, then the existing instance is returned.
     */
    public static function getInstance() {
        static $instance;

        $class = self::getClass();

        if ($instance === null) {
            $instance = new $class();
            $instance->init();
        }

        return $instance;
    }

    /**
     * Returns the classname of the child class extending this class
     *
     * @return string The class name
     */
    private static function getClass() {
        $implementing_class = static::$__CLASS__;
        $original_class = __CLASS__;

        if ($implementing_class === $original_class) {
            die("You MUST provide a <code>protected static \$__CLASS__ = __CLASS__;</code> statement in your Singleton-class!");
        }

        return $implementing_class;
    }
}
?>
Edward 20-Aug-2008 02:42
With Late Static Bindings, available as of PHP 5.3.0, it is now possible to implement an abstract Singleton class with minimal overhead in the child classes. Late static bindings are explained here: http://nl2.php.net/manual/en/language.oop5.late-static-bindings.php

In short, it introduces a new 'static::' keyword, that is evaluated at runtime. In the following code I use it to determine the classname of the child Singleton class.

<?
abstract class Singleton {
    protected static $__CLASS__ = __CLASS__;
    protected static $instance;

    protected function __construct() {
        static::$instance = $this;
        $this->init();
    }

    abstract protected function init();

    protected function getInstance() {
        $class = static::getClass();

        if (static::$instance===null) {
            static::$instance = new $class;
        }

        return static::$instance;
    }

    private static function getClass() {
        if (static::$__CLASS__ == __CLASS__) {
            die("You MUST provide a <code>protected static \$__CLASS__ = __CLASS__;</code> statement in your Singleton-class!");
        }

        return static::$__CLASS__;
    }
}
?>

An example Singleton class can then be implemented as follows:

<?
class A extends Singleton {
    protected static $__CLASS__ = __CLASS__; // Provide this in each singleton class.

    protected function someFunction() {
        $instance = static::getInstance();
        // ...
    }
}
?>

Hope this helps you save some time :)
James 22-Jun-2008 11:46
To get round this problem, another option is a factory to create singletons.

<?php
class Factory {
  private static $instances = array();
  public static function getInstance($class_name) {
    if(!array_key_exists($class_name, self::$instances)) {
      self::$instances[$class_name] = new $class_name();
    }
    return self::$instances[$class_name];
  }
}
?>

I guess you could modify this to create singletons from some, and prototypes for the rest.

There's probably some good way to stop people instantiating objects directly. Perhaps by using the backtrace in the constructor of the class to ensure that it was indeed the Factory that created the object.
Anonymous 30-Apr-2008 06:59
I jumped the gun. The following code also working for statics called inside other classes.

<?php
        for( $i=0 ; $i < count($bt) ; $i++)
        {
            if($bt[$i]['function'] == 'getInstance')
            {
                $class = $bt[$i]['class'];

            } else {
                break;
            }
        }
?>
dave dot zap at gmail dot com 30-Apr-2008 06:13
Take care using the backtrace method to find the calling class of a static method, you should step backward through the array and find a match for your getInstance() function. In backtrace the class name you want is not always the last item in the array.

I will not post the whole singleton class here, but have made the following modification to Frederik Krautwald's method (found below)

<?php
$bt = debug_backtrace();
// this method is count($bt)-1) by Frederik will fall over when calling getInstance from within an include file.
//$class = $bt[count($bt) - 1]['class'];

for( $i=count($bt)-1 ; $i > 0 ; $i--)
{
    if($bt[$i]['function'] == 'getInstance')
    {
        $class = $bt[$i]['class'];
        break;
    }
}
?>
Anonymous 12-Apr-2008 11:01
In Perl (and some other languages) you can call some methods in both object and class (aka static) context. I made such a method for one of my classes in PHP5, but found out that static methods in PHP5 do not 'know' the name of the calling subclass', so I use a backtrace to determine it. I don't like hacks like this, but as long as PHP doesn't have an alternative, this is what has to be done:

public function table_name() {
        $result = null;
        if (isset($this)) { // object context
            $result = get_class($this);
        }
        else { // class context
            $result = get_class();
            $trace = debug_backtrace();
            foreach ($trace as &$frame) {
                if (!isset($frame['class'])) {
                    break;
                }
                if ($frame['class'] != $result) {
                    if (!is_subclass_of($frame['class'], $result)) {
                        break;
                    }
                    $result = $frame['class'];
                }
            }
        }
        return $result;
    }
frederic dot sureau at gmail dot com 29-Feb-2008 08:01
@ hek :

The problem is that the method getInstance() in a Singleton must be static, so we cannot use $this because it doesn't exists :(
andregs at NOSPAM dot gmail dot NOSPAM dot com 21-Feb-2008 10:17
After reading the previous comments, this is the best I've done to get the final class name of a subclass:

<?php

class Singleton
{
   private static $_instances = array();
   protected final function __construct(){}

   /**
    * @param string $classname
    * @return Singleton
    */
   protected static function getInstance()
   {
      $classname = func_get_arg(0);
      if (! isset(self::$_instances[$classname]))
      {
         self::$_instances[$classname] = new $classname();
      }
      return self::$_instances[$classname];
   }

}

class Child extends Singleton
{
   /**
    * @return Child
    */
   public static function getInstance()
   {
      return parent::getInstance(get_class());
   }
}

?>

Subclasses must override "getInstance" and cannot override "__construct".
dodgie74 at NOSPAM dot yahoo dot NOSPAM dot co dot uk 09-Sep-2007 02:40
As noted in bug #30934 (which is not actually a bug but a consequence of a design decision), the "self" keyword is bound at compile time. Amongst other things, this means that in base class methods, any use of the "self" keyword will refer to that base class regardless of the actual (derived) class on which the method was invoked. This becomes problematic when attempting to call an overridden static method from within an inherited method in a derived class. For example:

<?php
class Base
{
    protected $m_instanceName = '';

    public static function classDisplayName()
    {
        return 'Base Class';
    }

    public function instanceDisplayName()
    {
        //here, we want "self" to refer to the actual class, which might be a derived class that inherits this method, not necessarily this base class
        return $this->m_instanceName . ' - ' . self::classDisplayName();
    }
}

class Derived extends Base
{
    public function Derived( $name )
    {
        $this->m_instanceName = $name;
    }

    public static function classDisplayName()
    {
        return 'Derived Class';
    }
}

$o = new Derived('My Instance');
echo $o->instanceDisplayName();
?>

In the above example, assuming runtime binding (where the keyword "self" refers to the actual class on which the method was invoked rather than the class in which the method is defined) would produce the output:

My Instance - Derived Class

However, assuming compile-time binding (where the keyword "self" refers to the class in which the method is defined), which is how php works, the output would be:

My Instance - Base Class

The oddity here is that "$this" is bound at runtime to the actual class of the object (obviously) but "self" is bound at compile-time, which seems counter-intuitive to me. "self" is ALWAYS a synonym for the name of the class in which it is written, which the programmer knows so s/he can just use the class name; what the programmer cannot know is the name of the actual class on which the method was invoked (because the method could be invoked on a derived class), which it seems to me is something for which "self" ought to be useful.

However, questions about design decisions aside, the problem still exists of how to achieve behaviour similar to "self" being bound at runtime, so that both static and non-static methods invoked on or from within a derived class act on that derived class. The get_class() function can be used to emulate the functionality of runtime binding for the "self" keyword for static methods:

<?php
class Base
{
    protected $m_instanceName = '';

    public static function classDisplayName()
    {
        return 'Base Class';
    }

    public function instanceDisplayName()
    {
        $realClass = get_class($this);
        return $this->m_instanceName . ' - ' . call_user_func(array($realClass, 'classDisplayName'));
    }
}

class Derived extends Base
{
    public function Derived( $name )
    {
        $this->m_instanceName = $name;
    }

    public static function classDisplayName()
    {
        return 'Derived Class';
    }
}

$o = new Derived('My Instance');
echo $o->instanceDisplayName();
?>

Output:
My Instance - Derived Class

I realise that some people might respond "why don't use just just the class name with ' Class' appended instead of the classDisplayName() method", which is to miss the point. The point is not the actual strings returned but the concept of wanting to use the real class for an overridden static method from within an inherited non-static method. The above is just a simplified version of a real-world problem that was too complex to use as an example.

Apologies if this has been mentioned before.
bramus at bram dot us 20-Aug-2007 05:40
@ Frederik :

<?
$class = $bt[count($bt) - 1]['class'];
?>

should be

<?
$class = $bt[count($bt) - 2]['class'];
?>

;-)
Frederik Krautwald 25-Jun-2007 05:13
Due to PHP 5 engine that permits to get final class in a static called function, and this is a modified version of examples published below.

<?php
abstract class Singleton {
    protected static $_instances = array();

    protected function __construct() {}

    protected static function getInstance() {
        $bt = debug_backtrace();
        $class = $bt[count($bt) - 1]['class'];
        if (!isset(self::$_instances[$class])) {
            self::$_instances[$class] = new $class();
        }
        return self::$_instances[$class];
    }
}
class A extends Singleton {
    public static function getInstance() {
        return parent::getInstance();
    }
}
class B extends Singleton {
    public static function getInstance() {
        return parent::getInstance();
    }
}
class C extends A {
    public static function getInstance() {
        return parent::getInstance();
    }
}

$a = A::getInstance();
$b = B::getInstance();
$c = C::getInstance();

echo "\$a is a " . get_class($a) . "<br />";
echo "\$b is a " . get_class($b) . "<br />";
echo "\$c is a " . get_class($c) . "<br />";
?>

I don't know about if performance would increase if debug_backtrace() is skipped and instead have getInstance() to accept a passed class retrieved by get_class() method as parameter as described in a post below.

By having set getInstance() to protected in the Singleton class, the function is required to be overridden (good OOP practice).

One thing to mention is, that there is no error checking in case $class is null or undefined, which would result in a fatal error. At the moment, though, I can't see how it could happen when the getInstance() is protected, i.e. has to be overridden in a subclass -- but with good coding practice you should always error check.
mightye at gmail dot com 23-Jun-2007 03:20
To: Bryan

In this model it is still workable if your singleton variable is actually an array.  Consider:
<?php
abstract class Singleton {
    protected final static $instances = array();

    protected __construct(){}

    protected function getInstance() {
        $class = get_real_class(); // imaginary function returning the final class name, not the class the code executes from
        if (!isset(self::$instances[$class])) {
            self::$instances[$class] = new $class();
        }
        return self::$instances[$class];
    }
}
class A extends Singleton {
}
class B extends Singleton {
}

$a = A::getInstance();
$b = B::getInstance();

echo "\$a is a " . get_class($a) . "<br />";
echo "\$b is a " . get_class($b) . "<br />";
?>
This would output:
$a is a A
$b is a B

The only alternative as described elsewhere is to make getInstance() protected abstract, accept the class name as an argument, and extend this call with a public final method for every sub-class which uses get_class() in its local object scope and passes it to the superclass.

Or else create a singleton factory like this:
<?php
final class SingletonFactory {
    protected static $instances = array();

    protected getInstance($class) {
        if (!isset(self::$instances[$class])) {
            self::$instances[$class] = new $class();
        }
        return self::$instances[$class];
    }
}
?>
The downside to this of course to this latter model is that the class itself doesn't get to decide if it is a singleton, the calling code gets to decide this instead, and a class that really wants or *needs* to be a singleton has no way to enforce this, not even by making its constructor protected.

Basically these design patterns, and various other meta manipulations (things which operate on the nature of the object, not on the data the object holds) could benefit greatly from knowing exactly what the final type of this object is, and not having native access to this information obligates work-arounds.
Bryan 19-Jun-2007 05:07
janci's Singleton example is all well and good, until you create another class "Bar" which also extends Singleton.

<?php
$single_foo = Foo::getInstance();
$single_bar = Bar::getInstance();
?>

$single_bar will now be an instance of Foo! This is because there is only one copy of the static variable $__instance in the Singleton class. Here is a better Singleton class:

<?php
abstract class Singleton {
    static $_instances = array();

    //Prevent singletons from being instantiated other than via getInstance();
    protected function __construct() {}
    protected function __clone() {}

    static function getInstance($class_name) {
        if(!isset(self::$_instances[$class_name])) {
            self::$_instances[$class_name] = new $class_name();
        }
        return self::$_instances[$class_name];
    }
}?>

Unfortunately, you are still required to define Foo as janci does. I would love to see an example where the only thing you add to the class is "extends Singleton" and you're done, but that seems to be impossible.
janci 30-Apr-2007 09:00
To yicheng zero-four at gmail dot com: Another, maybe better example where finding out the real class (not the class we are in) in static method should be quite usefull is the Singleton pattern.

There is currently no way how to create an abstract Singleton class that could be used just by extending it without the need to change the extended class. Consider this example:
<?php
abstract class Singleton
{
    protected static $__instance = false;

    public static function getInstance()
    {
        if (self::$__instance == false)
        {
            // This acctually is not what we want, $class will always be 'Singleton' :(
            $class = get_class();
            self::$__instance = new $class();
        }
        return self::$__instance;
    }
}

class Foo extends Singleton
{
    // ...
}

$single_foo = Foo::getInstance();
?>
This piece of code will result in a fatal error saying: Cannot instantiate abstract class Singleton in ... on line 11

The best way I figured out how to avoid this requires simple but still a change of the extended (Foo) class:
<?php
abstract class Singleton
{
    protected static $__instance = false;

    protected static function getInstance($class)
    {
        if (self::$__instance == false)
        {
            if (class_exists($class))
            {
                self::$__instance = new $class();
            }
            else
            {
                throw new Exception('Cannot instantiate undefined class [' . $class . ']', 1);
            }
        }
        return self::$__instance;
    }
}

class Foo extends Singleton
{
    // You have to overload the getInstance method in each extended class:
    public static function getInstance()
    {
        return parent::getInstance(get_class());
    }
}

$single_foo = Foo::getInstance();
?>

This is of course nothing horrible, you will propably need to change something in the extended class anyway (at least the constructor access), but still... it is just not as nice as it possibly could be ;)
yicheng zero-four at gmail dot com 27-Apr-2007 09:29
To matsgefvert dot se and Marc,

I'm sorry but I'm still having a very difficult time understanding why you would want this behavior.  I understand that you are generating some code for some persistent layer for database access.

In your code example, Marc, you say that you would like the SomeTable::read() to return "SomeTable" when called from your client.  Yet, at this point you *know* what the class name is.  Even assuming you are generating this code automatically, you can simply generate your SomeTable class as such.

<?php

class PersistedObject {
  //...
  public static function read($table, $key){
    // do some databasey stuff
    // with $table which is the table name
    return $table;
  }
}

class SomeTable extends PersistedObject {
  //...
  // make a copy in the child class
  public static function read($key){
    $table = get_class();
    return parent::read($table, $key);
  }
}

// this will now return "SomeTable"
$someTableObject = SomeTable::read(1234);

?>

I'm still failing to see how this would provide but a convenience for very rare & specific issues, at the cost of non-OOP behavior by allowing static methods to bind to objects instead of classes.

One of the most common uses for get_class() is for debugging lines, for example:

<?php

class PersistedObject {
  //...
  public static function transaction($params){
    // do some stuff
    if ($error_occurred){
      echo "Error : WTFBBQ happened in " . get_class() . "::" . __FUNCTION__ . ".  Time to get some coffee!";
  }
}

class SomeTable extends PersistedObject {
  //...
}

SomeTable::transaction( $someParams );
?>

Now, when an error occurs inside the call to SomeTable::transaction(), we get incorrect error output.
Kerry Kobashi 15-Apr-2007 08:58
It should be noted that class names are represented in lowercase under PHP 4.x.

class MyClass {
   function MyClass() {
   }
   function ShowClass() {
      echo get_class($this);
   }
}

$p = new MyClass();
$p->ShowClass();

Displays:

myclass

If you serialize $p, you will also see that the class name is lower case as well:

$p = new MyClass();
echo serialize($p);

Displays:

O:7:"myclass":1:{s:3:"val";i:100;}

In my case, this is an important point for mapping class names to database table names (and vice versa).
Marc W. 13-Apr-2007 06:40
The previous comment (from matsgefvert dot se) is 100% correct. To further elaborate on the persistence layers example suggested, consider the case where you are dynamically creating inherited objects. For example:
<?php

class PersistedObject {
//...
public static function read($key){
//You will need the table name here to actually read
// from the persistence medium
echo get_class();
}

class SomeTable extends PersistedObject {
//...
}

/*
Somewhere else in our code we would like to call the read method
*/

$someTableObject = SomeTable::read(1234);

?>
Remember that the SomeTable class would be generated on the fly, probably by some DB schema. The problem is that you would actually like the above example to output "SomeTable", but unfortunately, it will output "PersistedObject".
spam at matsgefvert dot se 24-Feb-2007 10:37
I disagree with the notion that the authors of the previous posts are unfamiliar with OOP. Without the ability to know from general (parent) objects what kind of classes we're operating on, it makes things rather difficult, especially in building frameworks that rely heavily on class types (e.g. persistence layers).

The proposed BooBoof::getclass() and CooCoof::getclass() scheme will not work in some particular scenarios since static calls aren't virtual, which means a parent class will always call its own getclass() function instead of the one needed to find out the class name.

The only solution I've found is to pass along a string containing the class name we're working on to the generalized functions. It is however an ugly solution, certainly less than ideal and goes against what OOP and reflection is all about, IMHO.
yicheng zero-four at gmail dot com 18-Jan-2007 09:22
This a response to luke at liveoakinteractive dot com and davidc at php dot net.  Static methods and variables, by definition, are bound to class types not object instances.  You should not need to dynamically find out what class a static method belongs to, since the context of your code should make it quite obvious.  Your questions reveals that you probably don't quite understand OOP quite yet (it took me a while as well).

Luke, the observed behavior from your particular code snippet makes perfect sense when you think about it.  The method getclass() is defined in BooBoof, so the __CLASS__ macro would be bound to BooBoof and defined in relation to the BooBoof class.  The fact that CooCoof is a subclass of BooBoof just means that it gains a shortcut to BooBoof::getclass().  So, in effect, you are really asking (in a convoluted way): "What is the class to which belongs the method call BooBoof::getclass()?"  The correct solution (if you actually want/need to do this) is to simply implement CooCoof::getclass() { return __CLASS__; } inside of the CooCoof definition, and any childclasses that you want to mimic this behavior.  CooCoof::getclass() will have the expected behavior.
makc dot the dot great at gmail dot com 17-Dec-2006 06:29
Check it out:

<?php

class A {
 function s () { return "A::s"; }
 function s1 () { return $this->s(); }
}

class B extends A {
 function s () { return "B::s"; }
}

$a = new A(); echo $a->s1();
echo "<br>";
$b = new B(); echo $b->s1();
echo "<br>";

class C {
 static function s () { return "C::s"; }
 static function s1 () { return self::s(); }
}

class D extends C {
 static function s () { return "D::s"; }
}

echo C::s1();
echo "<br>";
echo D::s1();
echo "<br>";

?>

Output:
A::s
B::s
C::s
C::s

Seems like static function always belong to its class.
luke at liveoakinteractive dot com 06-Dec-2006 04:03
This note is a response to the earlier post by davidc at php dot net. Unfortunately, the solution posted for getting the class name from a static method does not work with inherited classes.

Observe the following:
<?php
class BooBoof {
  public static function getclass() {
    return __CLASS__;
  }

  public function retrieve_class() {
    return get_class($this);
  }
}

class CooCoof extends BooBoof {
}

echo CooCoof::getclass();
// outputs BooBoof

$coocoof = new CooCoof;
echo $coocoof->retrieve_class();
// outputs CooCoof
?>

__CLASS__ and get_class($this) do not work the same way with inherited classes. I have been thus far unable to determine a reliable way to get the actual class from a static method.
benjaminhill at gmail dot com 27-Apr-2006 07:47
More funkyness:

class Parent {
   function displayTableName() {
      echo get_class($this);
      echo get_class();
   }
}

class Child {
   function __construct() {
      $this->displayTableName();
   }
}

Will return
- Child
- Parent

So when they say "the object isn't required in PHP5" - they don't really mean it.
brjann at NOSPAMATALLgmail dot com 16-Nov-2005 09:39
This behavior is unexpected, but good to be aware of

class parentclass {
    public function getClass(){
        echo get_class($this); //using "$this"
    }
}
class child extends parentclass {
}

$obj = new child();
$obj->getClass(); //outputs "child"

class parentclass {
    public function getClass(){
        echo get_class(); //note, no "$this"
    }
}
class child extends parentclass {
}

$obj = new child();
$obj->getClass(); //outputs "parentclass"
wired at evd dot ru 25-Sep-2005 11:25
There is one unexpected bahaviour (for me as least):

<?php

  class parent
  {
    ...

    public function getInstance ($id)
    {
      ...

      print get_class() . "\n" . __CLASS__;

      ...
    }
  }

  class child extends parent
  {
    ...
  }

  child::getInstance(...);
?>

This code will produce:

  parent
  parent

So I can't make "new $className(...)" in getInstance(). The only option is to do a fabric.
kunxin at creaion dot com 25-Jul-2005 02:30
I just migrated from PHP 4 to PHP 5 and noticed that in PHP 5.03 that a lot of code dependent on get_class() and its variants stop working.

It turns out that get_class() and its variants are now case-sensitive.
refrozen dot com 06-Jul-2005 12:01
philip at cornado dot com, it returns the value of the class from which it was called, rather than the instance's name... causing inheritance to result in unexpected returns
MagicalTux at FF.ST 02-Feb-2004 01:11
Note that the constant __CLASS__ is different from get_class($this) :
<?
  class test {
    function whoami() {
      echo "Hello, I'm whoami 1 !\r\n";
      echo "Value of __CLASS__ : ".__CLASS__."\r\n";
      echo "Value of get_class() : ".get_class($this)."\r\n\r\n";
    }
  }
  class test2 extends test {
    function whoami2() {
      echo "Hello, I'm whoami 2 !\r\n";
      echo "Value of __CLASS__ : ".__CLASS__."\r\n";
      echo "Value of get_class() : ".get_class($this)."\r\n\r\n";
      parent::whoami(); // call parent whoami() function
    }
  }
  $test=new test;
  $test->whoami();
  $test2=new test2;
  $test2->whoami();
  $test2->whoami2();
?>

The output is :
Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class() : test

Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class() : test2

Hello, I'm whoami 2 !
Value of __CLASS__ : test2
Value of get_class() : test2

Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class() : test2

In fact, __CLASS__ returns the name of the class the function is in and get_class($this) returns the name of the class which was created.
Dan 30-Jan-2003 07:00
This function does return the class name in lowercase, but that does not seem to make any difference. The code below, although very sloppy, works fine in all of the following configurations.

PHP 4.2.2 on Windows NT5 with Apache 1.3.24
PHP 4.2.1 in Zend Development Environment on box above
PHP 4.2.3 on Linux RedHat 7.3 with Apache 1.3.27

class TeSt {
   var $a;
   var $b = "Fred";

   // Notice the case difference in the constructor name

   function Test() {
      $classname = get_class($this); // $classname = "test"
      $this->ra = get_class_vars($classname);
   }
}
// Next line also works with Test(), TEST(), or test()
$obj = new TeSt();
print_r($obj->ra);

Result :
   Array
   (
       [a] =>
       [b] => Fred
   )
oliver DOT pliquett @mediagear DOT de 13-Aug-2002 05:07
This function can become _VERY_ helpful if you want to return a new object of the same type. See this example:

<?php
class Foo{
    var $name;

    function Foo( $parameter ){
        $this->name = $parameter;
    }

    function whoami() {
        echo "I'm a " . get_class( $this ) ."\n";
    }

    function getNew() {
        $className = get_class( $this );

        // here it happens:
        return new $className ( "world" ) ;
    }
}
class Bar extends Foo {

    function Bar( $name ){
        $this->Foo( $name );
    }

    function welcome() {
        echo "Hello, " . $this->name .  "! \n";
    }
}

// We generate a Bar object:
$myBar = new Bar( "Oliver" );
$myBar->welcome();

//now let's instanciate a new Bar object.
//note: this method is inherited from Foo by Bar!

$baba = $myBar->getNew();

$baba->welcome();
$baba->whoami();

/* Output:
Hello, Oliver!
Hello, world!
I'm a bar
*/
?>
philip at cornado dot com 20-Jun-2002 09:15
As of PHP 4.3.0 the constant __CLASS__ exists and contains the class name.


선언된 클래스명을 배열로 반환

  • ≪ get_class
  • get_declared_interfaces ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 선언된 클래스명을 배열로 반환

get_declared_classes

(PHP 4, PHP 5, PHP 7)

get_declared_classes — 선언된 클래스명을 배열로 반환

설명

array get_declared_classes ( void )

선언된 클래스를 얻습니다.

반환값

현재 스크립트에서 선언된 클래스명을 배열로 반환한다.

Note:

PHP 4.0.1에서는 배열의 첫부분에 세개의 클래스들이 들어가 있다: stdClass (Zend/zend.c에 정의되어있는 클래스), OverloadedTestClass (ext/standard/basic_functions.c에 정의되어있는 클래스), Directory (ext/standard/dir.c에 정의되어있는 클래스)

PHP에 컴파일되거나 적재된 확장이 추가적인 클래스를 가질 수 있는 점에 주의하십시오. 이말의 의미는 그 이름을 사용하는 사용자 선언 클래스를 선언할수 없다는 것이다. 부록의 기선언된 클래스 섹션에서 미리선언된 클래스명 이 있다.

예제

Example #1 get_declared_classes() 예제

<?php
print_r(get_declared_classes());
?>

위 예제의 출력 예시:

Array
(
    [0] => stdClass
    [1] => __PHP_Incomplete_Class
    [2] => Directory
)

참고

  • class_exists() - 클래스가 정의되었는지 확인
  • get_declared_interfaces() - 선언된 모든 인터페이스의 배열을 반환
  • get_defined_functions() - Returns an array of all defined functions

User Contributed Notes

brain at code dot com 14-Feb-2014 05:53
get-declared-classes makes no sense at all, if u maybe, later for production, merge class files in one package file.

lets say: package.php
print_r(get_declared_classes());
class declaredHere { }
print_r(get_declared_classes());

so in this case, the declaredHerr class is defined at the first call of print_r();
because PHP-complier runs a hole file and declare Stuff before running the code.

But (Lovely PHP):
print_r(get_declared_classes());
if(true){
class declaredHere { }
}
print_r(get_declared_classes());
Will print the declaredHere class only in the second print_r.

Its not a Bug it a...
yarco dot wang at gmail dot com 25-Nov-2013 04:32
those above comments are too old.
now, whatever the order is, the output will be the same:

<?php
class Test1
{}

print_r(get_declared_classes());

class Test2
{}

print_r(get_declared_classes());

?>

will output the same result.
dcahh at gmx de 29-Feb-2008 06:23
This function considers only classes and subclasses. Not subsubclasses.

In fact I have code that provides an abstract class and then classes using this abstract class. Further I have subclasses to my concrete classes - which is why my subclasses are not listed within the returned array.
dexen + goofy _ pl 25-Sep-2007 08:32
Summary:
 * in PHP 5.1 class names have case preserved
 * contrary, in PHP 4.4 class names are downcased, withe exception of a few build-in ones

The get_declared_classes() funcition returns the list of names with case preserved, as of PHP 5.1 series (prolly 5.0 too, but i have no way to test it right now). Since PHP generally is caseless in regard to names of classes, this may come at a surprise. Also, this could potentially break older code asssuming downcased list.

Take extra care when  checking for existence of a class. Following example is, potentially, error prone: <?php in_array( $className, $classget_declared_classes() ) ?>

A sure-fire (while slower) way would be to iterate over the array and normalize case to, say, lower:

<?php
$exists = FALSE;
$className = strtolower( $className );
foreach ( get_declared_classes() as $c ) {
    if ( $className === strtolower( $c ) ) {
        $exists = TRUE;
        break;
    }
}?>

Optimization of the above snippet is left as a simple excercise to the reader ;)
-- dexen deVries
matt at mattsoft dot net 21-Dec-2005 11:58
classes can't be unloaded. probably not very practical to implement that in a future version. I wouldn't go out of my way to do it if I were zend. you're better off finding a workaround. it's better programming technique to find a way around having to do that anyway.

http://www.zend.com/zend/week/week223.php#Heading10
22-Mar-2005 06:16
Regarding note of 3-21:

<?php

class myclass {}

$class = 'myclass';
$instance = new $class();

?>

This function could also be used to determine the names of classes defined in a particular file by calling it before and after include. It's hardly a pointless function.
matt-php at DONT-SPAM-ME dot bitdifferent dot com 01-Nov-2004 04:41
The array returned by this function will be in the order the classes were defined / included / required and this order does not appear to change.

For example:

<?PHP

//define classone
class classone { }

//define classtwo
class classtwo { }

//This will show X classes (built-ins, extensions etc) with
//classone and classtwo as the last two elements

print_r(get_declared_classes());

//define classthree
class classthree { }

//...and four
class classfour { }

//Shows the same result as before with class three and four appended
print_r(get_declared_classes());

?>

Output:

Array
(
   [0] => stdClass
   [1] .... other defined classes....
   [10] => classone
   [11] => classtwo
 )

and...

Array
(
   [0] => stdClass
   [1] .... other defined classes....
   [10] => classone
   [11] => classtwo
   [12] => classthree
   [13] => classfour
)
Jazeps Basko 07-Feb-2004 08:52
In PHP5, you don't get declared interfaces by calling this function!!!
To get interfaces you should use get_declared_interfaces(). However, to check if an interface is already defined, you should use class_exists()! This is strange, but PHP team does not think so.
smokey 21-Mar-2003 12:06
you cannot remove them. they are "defined", which happens when the class is being loaded from the parser. you just deleted an instance of a class.


선언된 모든 인터페이스의 배열을 반환

  • ≪ get_declared_classes
  • get_declared_traits ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 선언된 모든 인터페이스의 배열을 반환

get_declared_interfaces

(PHP 5, PHP 7)

get_declared_interfaces — 선언된 모든 인터페이스의 배열을 반환

설명

array get_declared_interfaces ( void )

선언된 인터페이스를 얻습니다.

반환값

현재 스크립트에서 선언된 인터페이스명의 배열을 반환합니다.

예제

Example #1 get_declared_interfaces() 예제

<?php
print_r(get_declared_interfaces());
?>

위 예제의 출력 예시:

Array
(
    [0] => Traversable
    [1] => IteratorAggregate
    [2] => Iterator
    [3] => ArrayAccess
    [4] => reflector
    [5] => RecursiveIterator
    [6] => SeekableIterator
)

참고

  • get_declared_classes() - 선언된 클래스명을 배열로 반환
  • class_implements() - Return the interfaces which are implemented by the given class or interface

User Contributed Notes

There are no user contributed notes for this page.


Returns an array of all declared traits

  • ≪ get_declared_interfaces
  • get_object_vars ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • Returns an array of all declared traits

get_declared_traits

(PHP 5 >= 5.4.0, PHP 7)

get_declared_traits — Returns an array of all declared traits

설명

array get_declared_traits ( void )

인수

이 함수는 인수가 없습니다.

반환값

Returns an array with names of all declared traits in values. Returns NULL in case of a failure.

참고

  • class_uses() - Return the traits used by the given class

User Contributed Notes

There are no user contributed notes for this page.


주어진 객체의 프로퍼티를 얻습니다

  • ≪ get_declared_traits
  • get_parent_class ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 주어진 객체의 프로퍼티를 얻습니다

get_object_vars

(PHP 4, PHP 5, PHP 7)

get_object_vars — 주어진 객체의 프로퍼티를 얻습니다

설명

array get_object_vars ( object $object )

영역에 따라 주어진 object에 접근할 수 있는 비정적 프로퍼티를 얻습니다.

인수

object

객체 인스턴스.

반환값

영역 안에서 지정한 object에 정의된 접근할 수 있는 비정적 프로퍼티의 연관 배열을 반환합니다. 프로퍼티에 값이 할당되어 있지 않으면, NULL 값으로 반환합니다.

변경점

버전 | 설명
4.2.0 이전 | object 인스턴스의 클래스에서 선언된 변수 중, 값이 할당되지 않은 변수는 반환하는 배열에 포함되지 않습니다.

예제

Example #1 get_object_vars()의 사용

<?php

class foo {
    private $a;
    public $b = 1;
    public $c;
    private $d;
    static $e;

    public function test() {
        var_dump(get_object_vars($this));
    }
}

$test = new foo;
var_dump(get_object_vars($test));

$test->test();

?>

위 예제의 출력:

array(2) {
  ["b"]=>
  int(1)
  ["c"]=>
  NULL
}
array(4) {
  ["a"]=>
  NULL
  ["b"]=>
  int(1)
  ["c"]=>
  NULL
  ["d"]=>
  NULL
}

참고

  • get_class_methods() - 클래스 메쏘드명을 얻습니다
  • get_class_vars() - 클래스의 기본 프로퍼티를 얻습니다

User Contributed Notes

beat 08-Nov-2014 08:39
If your object contains a reference, beware that you might get references for all object properties in the array values, thus when changing values in the array, they may change the object too (confirmed and not yet fixed PHP bug https://bugs.php.net/bug.php?id=66961 affecting all PHP versions up to at least 5.6.2 and 5.5.18 and 5.4.34).

This 10-years old comment tried reporting this: http://php.net/manual/en/function.get-object-vars.php#40515 but its exemple was incomplete. See bug report for example.
lee dot howarth dot 90 at gmail dot com 13-Apr-2014 07:48
I find this function very helpful, What i wanted to do was to get the object properties of an exception so i could sanitize them for output.

   try
   {
       throw new Exception( 'Test Exception.', 100 );
   }
   catch( Exception $e )
   {
       var_dump( ( object ) get_object_vars( ( object ) ( new ArrayObject( $e ) ) -> getarrayCopy() ) );
   }

Example return:

object(stdClass)#10 (4) { ["message"]=> string(15) "Test Exception." ["code"]=> int(100) ["file"]=> string(29) "C:\xampp\htdocs\dev\index.php" ["line"]=> int(19) }
rabatto1 at web dot de 04-Feb-2014 01:21
function to convert object (with children) to an (associative) array ... recusive

<?php

// ---------------------------------------------------------
// ----- object_to_array_recusive --- function (PHP) ------
// --------------------------------------------------------
// -- arg1:  $object  =  (PHP Object with Children)
// -- arg2:  $assoc   =  (TRUE / FALSE) - optional
// -- arg3:  $empty   =  ('' or NULL) - optional
// --------------------------------------------------------
// ----- return: Array from Object --- (associative) ------
// --------------------------------------------------------

function object_to_array_recusive ( $object, $assoc=1, $empty='' )
{
    $out_arr = array();
    $assoc = (!empty($assoc)) ? TRUE : FALSE;

    if (!empty($object)) {

        $arrObj = is_object($object) ? get_object_vars($object) : $object;

        $i=0;
        foreach ($arrObj as $key => $val) {
            $akey = ($assoc !== FALSE) ? $key : $i;
              if (is_array($val) || is_object($val)) {
                $out_arr[$key] = (empty($val)) ? $empty : object_to_array_recusive($val);
              }
              else {
                $out_arr[$key] = (empty($val)) ? $empty : (string)$val;
              }
        $i++;
        }

    }

    return $out_arr;
}

// --------------------------------------------------------
// --------------------------------------------------------

?>

Example / Usage:

a) return an associative (recusive) array from object

$new_arr1 = object_to_array_recusive($my_object);
// --- or --- object_to_array_recusive($my_object,TRUE);
// --- or --- object_to_array_recusive($my_object,1);

b) return a numeric  (recusive) array from object (set 2nd argument = FALSE)

$new_arr2 = object_to_array_recusive($my_object,FALSE);

you can pre-set the Array-Value of Empty (Sub-) Objects in 3rd argument ($empty)
// --- eg: NULL --- object_to_array_recusive($my_object,1,NULL);
oom at oscarotero dot com 10-Oct-2013 06:37
You can use call_user_func() to return public variables from inside the class:

class Test {
    protected $protected;
    public $public;
    private $private;

    public function getPublicVars () {
        return call_user_func('get_object_vars', $this);
    }
}

$test = new Test();
var_dump($test->getPublicVars()); // array("public" => NULL)
jozef at mostka dot com 03-Sep-2013 03:59
class A{
    private $priA;
    protected $proA;
    public $pubA;
}

class B extends A{
    private $priB;
    protected $proB;
    public $pubB;

    public function getVars(){
        return array_diff_key(get_object_vars($this), get_class_vars(get_parent_class($this)));
    }
}
$b = new B();
var_dump($b->getVars());

array(3) {
  ["priB"]=>
  NULL
  ["proB"]=>
  NULL
  ["pubB"]=>
  NULL
}
kuko at webperfection dot net 16-Apr-2013 08:09
get_object_vars($this)
php5.3 + it returns only public and protected; NOT private vars
@chozilla 11-Feb-2013 09:35
A simple way to call this function within an Object and to only export the *public* params would be to artificially change the scope - for example with a closure in php 5.3:

<?php
class Example
{
    public $publicSetting = 'public';
    protected $protectedSetting = 'protected';
    private $privateSetting = 'private';

    public function showEverything()
    {
        return get_object_vars($this);
    }

    public function showMyPublicsOnly()
    {
        $me = $this;
        $publics = function() use ($me) {
            return get_object_vars($me);
        };
        return $publics();
    }
}
?>
Fabien Haddadi 30-May-2012 06:42
It seems like there's no function that determines all the *static* variables of a class.

I've come out with this one as I needed it in a project:

<?php
function get_class_static_vars($object) {
     return array_diff(get_class_vars(get_class($object)), get_object_vars($object));
}
?>

It relies on an interesting property: the fact that get_object_vars only returns the non-static variables of an object.
Dionisiy 20-Apr-2012 03:21
Note that before PHP 5.3.9 numeric object variables of unserialized object are not public, and get_object_vars() won't return them:
https://bugs.php.net/bug.php?id=61774
elminster2031 at hotmail dot com 07-Jul-2010 09:30
Also note that this is recursive. For example:

<?php
class Sarah{

    private $Father;
    private $Mother;

    public function __construct(){
        $this->Father    =    NULL; //I don't know Sarah's Father
        $this->Mother    =    NULL; //I don't know Sarah's Mother
    }
}
class John_Connor{

    private $Father;
    private $Mother;

    public function __construct(){
        $this->Father    =    $this; //John went back in time and fathered himself
        $this->Mother    =    new Sarah(); //Sarah was his mom and his mate ewww
    }

    public function showParents(){
        return get_object_vars($this);
    }
}

$John    =    new John_Connor();
var_dump($John->showParents());
?>

You will see this outputs:

array(2) {
  ["Father"]=>
  object(John_Connor)#1 (2) {
    ["Father":"John_Connor":private]=>
    object(John_Connor)#1 (2) {
      ["Father":"John_Connor":private]=>
      *RECURSION*
      ["Mother":"John_Connor":private]=>
      object(Sarah)#2 (2) {
        ["Father":"Sarah":private]=>
        NULL
        ["Mother":"Sarah":private]=>
        NULL
      }
    }
    ["Mother":"John_Connor":private]=>
    object(Sarah)#2 (2) {
      ["Father":"Sarah":private]=>
      NULL
      ["Mother":"Sarah":private]=>
      NULL
    }
  }
  ["Mother"]=>
  object(Sarah)#2 (2) {
    ["Father":"Sarah":private]=>
    NULL
    ["Mother":"Sarah":private]=>
    NULL
  }
}
benny at nospam dot com 26-Dec-2009 10:31
Be aware of the fact that this is scope-sensitive. If you're calling this from an objects own method, then private and protected vars will be outputted as well. Call it from outside the object and the result will most likely be what you want to archive.
huzursuz at mailinator dot com 02-Feb-2009 05:44
better version of conv_obj, based on XML... this converts XML to an array... use it by

<?php
$result = xmlobj2arr(simplexml_load_string($xmlContent));

function xmlobj2arr($Data) {
       if (is_object($Data)) {
               foreach (get_object_vars($Data) as $key => $val) {
                       $ret[$key] = xmlobj2arr($val);
               }
               return $ret;
       } elseif (is_array($Data)) {
               foreach ($Data as $key => $val) {
                       $ret[$key] = xmlobj2arr($val);
               }
               return $ret;
       } else {
               return $Data;
       }
}
?>
jon at websandbox dot net 06-Jul-2008 06:00
If you're using the SPL ArrayObject class (or similar, or the ArrayAccess interface), you may have noticed that it's difficult to loop through an object's properties.

get_object_vars does not return the properties of an ArrayObject.  The only workaround I can see is to use get_class_vars.

<?php
$obj = new ArrayObject();
$obj->foo = "bar";
$obj[] = "1";
$obj[] = "2";
foreach($obj as $key => $value) {
 // will iterate over the array, iterating over "1" and "2", but ignoring $obj->foo
}

var_dump($obj); // dumps the array values, ignores $obj->foo

$a = get_object_vars($obj); // returns an empty array

class Fart extends ArrayObject {
    public $foo = "bar";
}

$obj = new Fart();
$a = get_object_vars($obj); // return an empty array
$a = get_class_vars(get_class($obj)); // returns array("foo"=>"bar"), Yay!

/* The above works with subclasses as well */
class Toot extends Fart {
    public $weeble = "wobble";
}

$a = get_class_vars(get_class($obj));
// returns array("weeble" => "wobble", "foo"=>"bar"), Yay again!

?>

So, to iterate over the properties of an ArrayObject, and not the array values:

<?php

$obj = new Toot(); // which is a subclass of ArrayObject
$props = get_class_vars(get_class($obj));
foreach($props as $prop => $defaultValue) {
  $value = $obj->$prop; // now you have the property name and  its value
}

?>
pardini at gmail dot com 09-May-2008 01:38
get_object_vars() has confusing behaviour when called as get_object_vars($this) or similar: since a method inside a class definition can access private vars, apparently so can get_object_vars(), so it returns private vars as well.

A simple workaround is to define a method that in itself calls get_object_vars, like:

<?php
function getPublicObjectVars($obj) {
  return get_object_vars($obj);
}

class Smth {
  private $notShown = 1;
  public $shown = 2;

  function test() {
    $vars = get_object_vars($this); // includes 'notShown'
    $vars = getPublicObjectVars($this); // only 'shown'
  }

}
?>
biga888 AT gmail 19-Nov-2007 09:48
Just thought I would pass this on.

Working with PHP 5.2.4

In Windows this works by reading the vars from the passed in object and copying them to the vars of the current object.

    public function copyMe(User $user) {
        $varArray = get_object_vars($this);
        $copyVarArray = get_object_vars($user);
        foreach ($varArray as $key=>$value) {
            $this->$key = $copyVarArray[$key];
        }
    }

This did not work in Linux, I had to add another function to return the array of vars. Calling get_array_vars on the passed in object would return an empty array.

        public function copyMe(User $user) {
        $varArray = $this->getArray();
        $copyVarArray = $user->getArray();
        foreach ($varArray as $key=>$value) {
            $this->$key = $copyVarArray[$key];
        }
    }

    public function getArray() {
        return get_object_vars($this);
    }
firemouth at gmail dot com 10-Sep-2007 03:48
This is a slight modification of the previous poster's function.  We ran into a problem using this function when we had a JS array nested inside a JS hash.

Something like this...

myHash = new Hash();
myHash[address] = new Array();

When we threw that at this function, it found the first hash as an object, and then using the previous poster's function, it did not consider the array as an "object."  Instead it gave us a std_object type and we were unable to make any use of it.

The modification we made was adding a check for is_array inside both the is_object and is_array checks when you call the function.  This checks for an array inside either an object or a nested array.

function conv_obj($Data){
    if(is_object($Data)){

        foreach(get_object_vars($Data) as $key=>$val){
            if(is_object($val) || is_array($val)){
                $ret[$key]=conv_obj($val);
            }else{
                $ret[$key]=$val;
            }
        }
        return $ret;
    }elseif(is_array($Data)){
        foreach($Data as $key=>$val){
            if(is_object($val) || is_array($val)){
                $ret[$key]=conv_obj($val);
            }else{
                $ret[$key]=$val;
            }
        }
        return $ret;
    }else{
        return $Data;
    }
}
C.H. 15-Aug-2007 03:03
function conv_obj($Data){
     if(is_object($Data)){
         foreach(get_object_vars($Data) as $key=>$val){
             if(is_object($val)){
                 $ret[$key]=conv_obj($val);
             }else{
                 $ret[$key]=$val;
             }
         }
         return $ret;
     }elseif(is_array($Data)){
         foreach($Data as $key=>$val){
             if(is_object($val)){
                 $ret[$key]=conv_obj($val);
             }else{
                 $ret[$key]=$val;
             }
         }
         return $ret;
     }else{
         return $Data;
     }
 }

Very simple function to convert any Subobject to an array.
Created it while working with Soap.
For me as an beginner with PHP, very useful :o)
thinice at gmail dot com 13-Aug-2007 07:46
To add to my previous comment - the error message should have meant same structure.

As my implementation called for cross-class comparison.
Maikel 24-May-2007 08:13
To follow the code of d11wtq (enquiries AT chriscorbyn.co.uk). I did this function to inspect all properties(public, private, protected) of object.

<?php
header("content-type: text/plain");

// Classes to test!
class OtherClass
{
    private $privateVarOtherClass = 11;
}

class MyClass extends OtherClass
{
    protected $protectedVar = "some";
    public $publicVar = "nk";
    private $privateVar = "algo";
    var $oldStyle;
}

// Dangerous function
function get_properties($obj, $values=false)
{
    $obj_dump  = print_r($obj, 1);
    $matches =  array();
    preg_match_all('/^\s+\[(\w+).*\] => (\w*)/m', $obj_dump, $matches);
    if ($values)
    {
        $output = array();
        foreach ($matches[1] as $key => $property)
        {
            $output[$property] = $matches[2][$key];
        }
        return $output;
    }
    else
    {
        return $matches[1];
    }
}

$instance = new MyClass();

echo "Properties\n";
print_r(get_properties($instance));

echo "Properties and values\n";
print_r(get_properties($instance, true));

?>

Note: remember that static properties are not visible to the object. By that not supported to them
thiago dot henrique dot mata at gmail dot com 21-Feb-2007 08:23
<?php
# How to make a function change the private attributes
# from some object without use serialize functions or
# lose the control of the changes.

/**
 * Parent Class to allow the change of privates attributes
 * Look the abstract function __setAttribute.
 *
 * @author Renan de Lima ( renandelima@gmail.com )
 * @author Thiago Mata ( thiago.henrique.mata@gmail.com )
 * @date 2007-02-21
 */
abstract class father
{
    /**
     * Receive the Aray and try to change the attribute value
     *
     * @param array $arrNewValues
     */
    public function __fromDatabase( $arrNewValues )
    {
        $arrToSet = array_intersect_key( $arrNewValues, get_object_vars( $this ) );
        foreach( $arrToSet as $strAttribute => $mixValue )
        {
            $this->__setAttribute( $strAttribute , $mixValue );
        }
    }

    /**
     * Required method to control the attributes of class
     * @param string $strAttribute
     * @param unknown $mixValue
     */
    abstract protected function __setAttribute( $strAttribute, $mixValue );

}

/**
 * Just a example of a child class using the functionality
 *
 * Note: if you don't wanna to allow the change of some attribute
 * by this method you can just make more complex the __setAttribute function.
 *
 * @author Renan de Lima ( renandelima@gmail.com )
 * @author Thiago Mata ( thiago.henrique.mata@gmail.com )
 * @date 2007-02-21
 */
class son extends father
{

    private $atr = 9;

    /**
     * This is the most simple implementation of the method.
     * This way it's allowed to the parent class change any attribute
     * @param string $strAttribute
     * @param unknown $mixValue
     */
    protected function __setAttribute( $strAttribute, $mixValue )
    {
        $this->{ $strAttribute } = $mixValue;
    }

}

$objSon = new son();
$objSon->__fromDatabase( array( 'atr' => 55 ) );
var_dump( $objSon );
?>
ananda dot putra at gmail dot com 01-Mar-2006 12:49
Hi all, I just wrote a function which dumps all the object propreties and its associations recursively into an array. Here it is..
<?php
function object_to_array($obj) {
        $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
        foreach ($_arr as $key => $val) {
                $val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val;
                $arr[$key] = $val;
        }
        return $arr;
}
?>

Example:
You have an object like this:
fruitsbasket Object
(
    [Fruits] => Array
        (
            [0] => fruits Object
                (
                    [_name] => Mango
                    [_color] => Green
                    [_weight] => 10
                )

            [1] => fruits Object
                (
                    [_name] => Apple
                    [_color] => Red
                    [_weight] => 15
                )

            [2] => fruits Object
                (
                    [_name] => Grape
                    [_color] => Purple
                    [_weight] => 5
                )

        )

    [total_weight] => 30
)

just do:
<?php
$the_array = object_to_array($the_object);
print_r($the_array);
?>

it will produce an array:
Array
(
    [Fruits] => Array
        (
            [0] => Array
                (
                    [_name] => Mango
                    [_color] => Green
                    [_weight] => 10
                )

            [1] => Array
                (
                    [_name] => Apple
                    [_color] => Red
                    [_weight] => 15
                )

            [2] => Array
                (
                    [_name] => Grape
                    [_color] => Purple
                    [_weight] => 5
                )

        )

    [total_weight] => 30
)

I wish function like this could be usefull for you all. :)
support at sascha minus diebel dot de 23-Dec-2005 11:55
Note that get_object_vars() returns the variables of the object not the class. You need to know if your class is extended from a parent class.
d11wtq (enquiries AT chriscorbyn.co.uk) 17-Nov-2005 12:40
Since there's no apparent means of obtaining all the *private* properties in an object I wrote a little function to do it.  Built in support would be much more efficient since mine uses a preg_  search to do this....

<?php

function get_private_properties($obj, $inside=false)
{
    $obj_dump  = print_r($obj, 1);
    preg_match_all('/^\s+\[(\w+):private\]/m', $obj_dump, $matches);
    if ($inside)
    {
        $output = array();
        foreach ($matches[1] as $property)
        {
            $output[$property] = $obj->$property;
            return $output;
        }
    }
    else return $matches[1];
}

?>

So if you run it with the optional second paramter missing you'll just get an array of the variable names that are private inside the class.  This is the only option if you are not inside the actual object and the object has no private properties inherited.

If you run it with the second parameter set to true you will get an associative array with the properties and their corresponding values.  I'd only advise to do that for singletons since you may get errors if there are any private properites in parents/children.
pascal dot poncet at netconsult dot com 13-Oct-2005 04:20
Subject: using "sql_calc_found_rows" in a MySQL query while exploiting result in a PHP db class object.

Hello,

There is a nice function in MySQL that allows to know how many records would have been returned if no "where" clause were set : SQL_CALC_FOUND_ROWS.

If you have create a db object to collect the returned lines, you will be a little perplex when trying to call the result of this function.

Why ?
Simply because the returned field's name is "found_rows()" and obviously it's not possible to call something like :

<?php $result->found_rows() ?>

...as it will try to acces a method, not a property !

Then, the only way to get the right result seems to be the use of a class function, like :

<?php
  $db->query("select found_rows()");
  $count=current(get_object_vars(current($db->result)));
?>

Of course, if somebody found an other way to solve it, like a special syntax (see the one used with curled arrays in a string), I'm really open to discuss.

Good luck,
Pascal
Marc 23-Aug-2005 03:08
If you want to access all properties (private, protected, public) of a class and his base class(es) from outside the object you can take a look in the code below.
You can even reffrence them.

Example:
<?php

// Dummy class to act as parent class.
class Dummy
{
    private        $d1 = 1;
    protected    $d2 = 2;
    public        $d3 = 3;
}

// The class to test on.
class Test extends Dummy
{
    private        $t1 = 11;
    protected    $t2 = 12;
    public        $t3 = 13;
}

// Instance of the test class.
$test =& new Test();

// The propertynames of the test class and his parent.
$nameD1 = "\0Dummy\0d1";    // class Dummy, private      $d1
$nameD2 = "\0*\0d2";        // class Dummy, protected $d2
$nameD3 = "d3";            // class Dummy, public    $d3

$nameT1 = "\0Test\0t1";        // class Test,  private      $t1
$nameT2 = "\0*\0t2";        // class Test,  protected $t2
$nameT3 = "t3";            // class Test,  public    $t3

// Printing all members (private, protected, public) of test class and parent.
// These are the values at construction.
print("Original object:\n");
print("\t\$d1 = ".$test->$nameD1."\n");
print("\t\$d2 = ".$test->$nameD2."\n");
print("\t\$d3 = ".$test->$nameD3."\n");
print("\t\$t1 = ".$test->$nameT1."\n");
print("\t\$t2 = ".$test->$nameT2."\n");
print("\t\$t3 = ".$test->$nameT3."\n");
print("\n");

// Create new values to use as reffrence.
$varD1 = 31;
$varD2 = 32;
$varD3 = 33;

$varT1 = 41;
$varT2 = 42;
$varT3 = 43;

// Reffrence these to the class properties.
$test->$nameD1 =& $varD1;
$test->$nameD2 =& $varD2;
$test->$nameD3 =& $varD3;

$test->$nameT1 =& $varT1;
$test->$nameT2 =& $varT2;
$test->$nameT3 =& $varT3;

// Printing all members (private, protected, public) of test class and parent.
// The values have changed by assigning reffrence to them.
print("Object Changed by reffrence (1):\n");
print("\t\$d1 = ".$test->$nameD1."\n");
print("\t\$d2 = ".$test->$nameD2."\n");
print("\t\$d3 = ".$test->$nameD3."\n");
print("\t\$t1 = ".$test->$nameT1."\n");
print("\t\$t2 = ".$test->$nameT2."\n");
print("\t\$t3 = ".$test->$nameT3."\n");
print("\n");

// Change the original values.
// This will change the class properties.
$varD1 = 61;
$varD1 = 62;
$varD1 = 63;

$varT1 = 71;
$varT1 = 72;
$varT1 = 73;

// Printing all members (private, protected, public) of test class and parent.
// The values have changed because the variables $varXX have been changed.
print("Object Changed by reffrence (2):\n");
print("\t\$d1 = ".$test->$nameD1."\n");
print("\t\$d2 = ".$test->$nameD2."\n");
print("\t\$d3 = ".$test->$nameD3."\n");
print("\t\$t1 = ".$test->$nameT1."\n");
print("\t\$t2 = ".$test->$nameT2."\n");
print("\t\$t3 = ".$test->$nameT3."\n");
print("\n");

// Printing the object using print_r() shows the changes
// have been done on the object.
print("Object Changed (print_r):\n");
print_r($test);
print("\n");

// If you dont now the class propertynames you can get these by casting the
// object to an array. The array keys are the names
$prop = array_keys((array) $test);
print("Getting all class propertynames (print_r)\n");
print_r($prop);

//Result
/*

Original object:
    $d1 = 1
    $d2 = 2
    $d3 = 3
    $t1 = 11
    $t2 = 12
    $t3 = 13

Object Changed by reffrence (1):
    $d1 = 31
    $d2 = 32
    $d3 = 33
    $t1 = 41
    $t2 = 42
    $t3 = 43

Object Changed by reffrence (2):
    $d1 = 63
    $d2 = 32
    $d3 = 33
    $t1 = 73
    $t2 = 42
    $t3 = 43

Object Changed (print_r):
Test Object
(
    [t1:private] => 73
    [t2:protected] => 42
    [t3] => 43
    [d1:private] => 63
    [d2:protected] => 32
    [d3] => 33
)

Getting all class propertynames (print_r)
Array
(
    [0] => Test

*/

?>
stachnik at gmail dot com 02-Aug-2005 06:00
In PHP5 to get an array with all properties (even the private ones) all you have to do is write a public method that returns an array for your class:

public function getArray()
{
  return get_object_vars($this);
}

and then

$myBeautifulArray = $myBeautifulObject->getArray ();

Have BEAUTIFUL day :)
fmmarzoa at librexpresion dot org 02-Nov-2004 08:53
You can still cast the object to an array to get all its members and see its visibility. Example:

<?php

class Potatoe {
    public $skin;
    protected $meat;
    private $roots;

    function __construct ( $s, $m, $r ) {
        $this->skin = $s;
        $this->meat = $m;
        $this->roots = $r;
    }
}

$Obj = new Potatoe ( 1, 2, 3 );

echo "<pre>\n";
echo "Using get_object_vars:\n";

$vars = get_object_vars ( $Obj );
print_r ( $vars );

echo "\n\nUsing array cast:\n";

$Arr = (array)$Obj;
print_r ( $Arr );

?>

This will returns:

Using get_object_vars:
Array
(
    [skin] => 1
)

Using array cast:
Array
(
    [skin] => 1
    [ * meat] => 2
    [ Potatoe roots] => 3
)

As you can see, you can obtain the visibility for each member from this cast. That which seems to be spaces into array keys are '\0' characters, so the general rule to parse keys seems to be:

Public members: member_name
Protected memebers: \0*\0member_name
Private members: \0Class_name\0member_name

I've wroten a obj2array function that creates entries without visibility for each key, so you can handle them into the array as it were within the object:

<?php

function obj2array ( &$Instance ) {
    $clone = (array) $Instance;
    $rtn = array ();
    $rtn['___SOURCE_KEYS_'] = $clone;

    while ( list ($key, $value) = each ($clone) ) {
        $aux = explode ("\0", $key);
        $newkey = $aux[count($aux)-1];
        $rtn[$newkey] = &$rtn['___SOURCE_KEYS_'][$key];
    }

    return $rtn;
}

?>

I've created also a <i>bless</i> function that works similar to Perl's bless, so you can further recast the array converting it in an object of an specific class:

<?php

function bless ( &$Instance, $Class ) {
    if ( ! (is_array ($Instance) ) ) {
        return NULL;
    }

    // First get source keys if available
    if ( isset ($Instance['___SOURCE_KEYS_'])) {
        $Instance = $Instance['___SOURCE_KEYS_'];
    }

    // Get serialization data from array
    $serdata = serialize ( $Instance );

    list ($array_params, $array_elems) = explode ('{', $serdata, 2);
    list ($array_tag, $array_count) = explode (':', $array_params, 3 );
    $serdata = "O:".strlen ($Class).":\"$Class\":$array_count:{".$array_elems;

    $Instance = unserialize ( $serdata );
    return $Instance;
}
?>

With these ones you can do things like:

<?php

define("SFCMS_DIR", dirname(__FILE__)."/..");
require_once (SFCMS_DIR."/Misc/bless.php");

class Potatoe {
    public $skin;
    protected $meat;
    private $roots;

    function __construct ( $s, $m, $r ) {
        $this->skin = $s;
        $this->meat = $m;
        $this->roots = $r;
    }

    function PrintAll () {
        echo "skin = ".$this->skin."\n";
        echo "meat = ".$this->meat."\n";
        echo "roots = ".$this->roots."\n";
    }
}

$Obj = new Potatoe ( 1, 2, 3 );

echo "<pre>\n";
echo "Using get_object_vars:\n";

$vars = get_object_vars ( $Obj );
print_r ( $vars );

echo "\n\nUsing obj2array func:\n";

$Arr = obj2array($Obj);
print_r ( $Arr );

echo "\n\nSetting all members to 0.\n";
$Arr['skin']=0;
$Arr['meat']=0;
$Arr['roots']=0;

echo "Converting the array into an instance of the original class.\n";
bless ( $Arr, Potatoe );

if ( is_object ($Arr) ) {
    echo "\$Arr is now an object.\n";
    if ( $Arr instanceof Potatoe ) {
        echo "\$Arr is an instance of Potatoe class.\n";
    }
}

$Arr->PrintAll();

?>
27-Oct-2004 05:02
actually, it's not entirely true that php5 will only return public members....php5 will return any variable IT HAS ACCESS TO

In other words, if you do a get_class_variables($this) inside a class, you'll get everything - public, private, the whole shebang...really annoying since you can't check to see what's private/public without using reflection
manicdepressive AT mindless DOT com 06-Mar-2004 04:57
more strange, strange behaviour:

if you are trying to deep-copy an object with get_object_vars(), strange behaviour can accidentally clobber your original object properties.  please read very, very carefully:

get_object_vars() may either return references to *or* deep copies of the object's properties *depending on whether that property has been set with the -> operator*.   (this behaviour probably varies per php platform and os so please confirm for yourself.)

furthermore, consider
  $properties = get_object_vars($obj);
normally, unset()ting a reference does not affect the original, i.e. $ref = NULL; is not the same as unset($ref); per the references documentation.  However, if you have this strange references version and you unset() an array element of $properties, it will *SET THE OBJECT PROPERTY TO NULL*, which is not how references normally work.

even stranger behaviour comes into effect that i can only express with an example.  please test this with your version and OS and proceed very carefully:
-->
<?php
echo "<pre>\n";

class Lump
{
   var $size = 'average';

   function & copy()
   {  // return a deep copy
      $copy = new Lump();
      $properties = get_object_vars($this);
      foreach( array_keys( $properties ) as $property ){
         $copy->$property = $properties[$property];  // deep, right?
      }
      return $copy;
   }

}

$lump = new Lump();

$lump->size = 'huge';  // <--- this line changes everything
// comment above line out, and see the difference
// also, try substituting another property for 'size'

$properties = get_object_vars($lump);
$properties['size'] = 'small'; // this behavior varies

echo "after changing the properties array:\n";
var_dump( $lump );
// it's either big or small (never huge) depending on
// whether you commented-out the indicated line

//------------- let's try using our copy() method

$original_lump = new Lump();
$original_lump->size = 'huge'; // this line changes the behaviour
$other_lump =& $original_lump->copy();
unset( $other_lump->size );
echo "after unsetting in copy:\n";
var_dump( $original_lump ); // i'm afraid so -- original value clobbered !

echo "</pre>\n";
?>
code till dawn,
   mark meves
christopher AT NOSPAM AT idealab DOT com 31-Dec-2003 12:12
Hmmm.  A bit embarassing...

It turns out the best way to get references to all of your objects member variables is NOT with the functions I provided before, or with get_object_vars.

Just cast the object to array.

$a=(array)$obj;

# The two following statements are now equivalent and identical
$a["member"]=3;
$obj->member=3;

A very powerful tool, for inspectors and what not.
christopher AT NOSPAM AT idealab DOT com 23-Dec-2003 09:49
Please note that you cannot affect the object via the array values...in other words, the returned array does not contain references to the values within the object, but copies.

If you are making an object inspector or editor, this is not good enough.  So I made the following methods:

METHODS:

function &getVar($obj, $name)
{
        $expr="\$prop=&\$obj->$name;";
        eval($expr);
        return $prop;
}

function &getObjectVars($obj)
{
        $result=array();
        $vars=get_object_vars($obj);
        foreach ($vars as $var => $value)
        {
                $result[$var]=&getVar(&$obj, $var);
        }
        return $result;
}

[NOTE:  You must pass in a reference to an object, not an object.  Sorry if this  offends PHP'ers, but the distinction of pass-by-value and copy-on-assignment drives me batty (compared to Python, Java, Smalltalk), so I make all my functions pass by value, and force myself to pass in a reference to keep track of what is happening under the hood.]

EXAMPLE:

class Bob
{
        function Bob()
        {
                $this->thing=13;
                $this->other="whatever";
        }

        var $thing;
        var $other;
}

$obj=&new Bob();

# NOTE:  Passing in a reference!
$props=getObjectVars(&$obj);
$props["thing"]=-11;

var_dump($obj);

RESULTS:

object(bob)(2) {
  ["thing"]=>
  &int(-11)
  ["other"]=>
  &string(8) "whatever"
}
jordi at laigu dot net 06-Nov-2003 02:21
In case your object contains again OBJECTS or ARRAYS:

function makeAssoc($res) {
  if (is_object($res)) $res = get_object_vars($res);
  while (list($key, $value) = each($res)) {
    if (is_object($value) || is_array($value)) {
      $res[$key] = makeAssoc($value);
    }
  }
  return $res;
}

Thanks to mark at dreamzpace dot com
markus at emedia-solutions-wolf dot de 06-Mar-2003 09:01
Hi,

I figured out that in prior version to 4.2 the returned array only contains attributes directly in this class, excluding the derived ones from parentclasses.
info at phpken dot de 22-Nov-2002 03:34
hello,

this example will look like all values of vars was set in your class. write a method like the name: dumpClass and then fill in follow code:

$vars = get_object_vars($this);

echo "<b>class vars</b>";

foreach( $vars as $name => $value ) {
    echo "<li>".$name." : ".$value;
}

look at: get_object_vars($this);

andreas v.l
mark at dreamzpace dot com 13-Sep-2002 07:44
In case your object contains again objects (and so on), this function might be useful:

function makeAssoc($res) {
  $res = get_object_vars($res);
  while (list($key, $value) = each($res)) {
    if (is_object($value)) {
      $res[$key] = makeAssoc($value);
    }
  }
  return $res;
}
nick_eby at bonzidev dot com 09-Jul-2002 09:13
Furthermore, variables not declared in the class but set on a given object, will be returned by get_object_vars().

Example, ver. 4.2.1:
<?
class MyTest {
        var $classVar1 = 'Class Var 1';
        var $classVar2;
        var $classVar3;

        function MyTest()
        {
                $this->classVar2 = 'class var 2';
        }
}

$test = new MyTest();
// This var isn't declared in the class
$test->newObjVar = 'foobar';

echo "<pre>";
print_r(get_object_vars($test));
echo "</pre>";
?>

The output is:
Array
(

    [classVar1] => Class Var 1
    [classVar2] => class var 2
    [classVar3] =>
    [newObjVar] => foobar
)

Prior to version 4.2, classVar3 would not be output as it was never assigned a value.
michael at tapinternet dot com 14-Jun-2002 12:32
It seems that get_object_vars will now return properties of an object even if they have no value  - meaning only defined by var $foo in the class declaration.  This is noted behaviour in 4.2.1 which is different from previous versions and hitherto undocumented on this page.
florian at XCLUDETHISgsf dot de 29-Jan-2002 07:02
There is a strange behaviour, not sure whether it is a bug:

if I call
<?
$single_object = $data_array_of_objects[0];
$array_of_objectvars = get_object_vars($single_object);
     foreach($array_of_objectvars as $key => $val) {
     echo(" $key => $val<br>");
     }
?>
I get only _ONE_ line with the $key = first variable name of the object and $val = the values of _ALL_ variables of the object including the first separated by a space.

NOW:
if I call
<?
$single_object = $data_array_of_objects[0];
$array_of_objectvars = get_object_vars($single_object);
     foreach($array_of_objectvars as $key => $val) {
     echo(" $key => $val<br>");
     }
     echo($data_array_of_objects[0]->objectvar1."<br>");
     echo($data_array_of_objects[0]->objectvar2."<br>");
?>

I get a list of $key = $ val as expected, before the other echos' are printed.
It seems to me that get_object_vars works differently when you access a variable in those objects explicitly (as in the echos)


객체나 클래스의 부모 클래스명을 얻습니다

  • ≪ get_object_vars
  • interface_exists ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 객체나 클래스의 부모 클래스명을 얻습니다

get_parent_class

(PHP 4, PHP 5, PHP 7)

get_parent_class — 객체나 클래스의 부모 클래스명을 얻습니다

설명

string get_parent_class ([ mixed $object ] )

객체나 클래스의 부모 클래스명을 획득합니다.

인수

object

확인하는 객체나 클래스명

반환값

object 인스턴스나 클래스명의 부모 클래스명을 반환합니다.

Note:

객체에 부모가 존재하지 않으면 FALSE를 반환합니다.

객체 밖에서 인수 없이 호출하면, FALSE를 반환합니다.

변경점

버전 | 설명
5.1.0 이전 | 객체가 밖에서 인수 없이 호출하면, NULL을 반환하고 경고를 발생했습니다.
5.0.0부터 | 객체 메쏘드에서 호출할 때 object 인수가 선택적이 되었습니다.
4.0.5부터 | object가 문자열이면, 그 이름을 가진 클래스의 부모 클래스명을 반환합니다.

예제

Example #1 get_parent_class() 사용하기

<?php

class dad {
    function dad()
    {
    // implements some logic
    }
}

class child extends dad {
    function child()
    {
        echo "I'm " , get_parent_class($this) , "'s son\n";
    }
}

class child2 extends dad {
    function child2()
    {
        echo "I'm " , get_parent_class('child2') , "'s son too\n";
    }
}

$foo = new child();
$bar = new child2();

?>

위 예제의 출력:

I'm dad's son
I'm dad's son too

참고

  • get_class() - 객체의 클래스명을 반환
  • is_subclass_of() - 이 클래스가 부모 클래스의 자식인지 확인

User Contributed Notes

falundir at gmail dot com 13-Apr-2012 07:27
You can use this function to find common parent of multiple objects or classes.

<?php
/**
 * Returns name of the first (in class hierarchy) common parent class of all provided objects or classes.
 * Returns FALSE when common class is not found.
 *
 * @param mixed $objects Array that can contain objects or class names.
 * @return mixed
 */
function get_first_common_parent($objects) {
    $common_ancestors = null;
    foreach($objects as $object) {
        if (is_object($object)) {
            $class_name = get_class($object);
        } else {
            $class_name = $object;
        }

        $parent_class_names = array();
        $parent_class_name = $class_name;
        do {
            $parent_class_names[] = $parent_class_name;
        } while($parent_class_name = get_parent_class($parent_class_name));

        if ($common_ancestors === null) {
            $common_ancestors = $parent_class_names;
        } else {
            $common_ancestors = array_intersect($common_ancestors, $parent_class_names);
        }
    }

    return reset($common_ancestors);
}
?>

Example:

<?php
class A {
}

    class B extends A {
    }

        class D extends B {
        }

        class E extends B {
        }

    class C extends A {
    }

        class F extends C {
        }

            class G extends F {
            }

class H {
}

//returns "A"
get_first_common_parent(array('G', 'E'));

//returns "F"
get_first_common_parent(array(new G(), 'F'));

//returns false (no common parent)
get_first_common_parent(array('C', 'H'));

//returns false (non-existent class provided)
get_first_common_parent(array(new B(), 'X'));
?>
levu 20-Apr-2011 05:36
I wrote a simple function doing the reverse thing: get the children:

<?php
function get_child($instance, $classname) {
    $class = $classname;
    $t = get_class($instance);
    while (($p = get_parent_class($t)) !== false) {
        if ($p == $class) {
            return $t;
        }
        $t = $p;
    }
    return false;
}

abstract class A {
    function someFunction() {
        return get_child($this, __CLASS__);
    }
}

class B extends A {

}

class C extends B {

}

$c = new C();
echo $c->someFunction(); //displays B

?>
michael at getsprink dot -- com 09-Apr-2009 09:28
This little snippet to get the inheritance tree might be useful to someone.

<?php

header("Content-Type: text/plain;");

class Top {
  public function getParents($class=null, $plist=array()) {
    $class = $class ? $class : $this;
    $parent = get_parent_class($class);
    if($parent) {
      $plist[] = $parent;
      /*Do not use $this. Use 'self' here instead, or you
       * will get an infinite loop. */
      $plist = self::getParents($parent, $plist);
    }
    return $plist;
  }
}

class Middle extends Top {

}

class Bottom extends Middle {

}

$o = new Bottom();
print_r($o->getParents());

?>
ssb45 at cornell dot edu 14-May-2008 05:32
"'If called without parameter outside object' What on earth does that mean?"

There are two places this could be called:
1. From within a member function of an object.  In this case, it may be called with no parameters and will return the parent class of the object owning the member function.  (If the parameter is included, then it will return the parent class of the specified class as normal.)

2. From outside an object (i.e., global or function scope).  In this case, PHP doesn't know what class you're talking about if you don't include a parameter, so it returns FALSE.  (But, of course, it works if you specify the class with the parameter.)
marcus at synchromedia dot co dot uk 16-Apr-2008 06:08
"If called without parameter outside object" What on earth does that mean?

What I can tell you, and that is not documented, is that if the object in question does not have an explicitly declared parent class, it does return boolean false. It doesn't for example return 'stdClass' on the basis that all objects are derived from that.
birkholz at web dot de 07-Oct-2005 02:01
tim at correctclick dot com wrote:
<quote>
A slightly more cryptic but faster get_ancestors function:

<?php
function get_ancestors ($class) {

     for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = $class);
     return $classes;

}
?>
(The second part of the for is implicitly testing for $class != "").  Recursion is considerably slower than looping, so you probably want to use this function.

Hope someone finds it useful.
</quote>

I would prefer this version, because it will create no duplicates:
<?php
function get_ancestors ($class) {
    $classes = array($class);
    while($class = get_parent_class($class)) { $classes[] = $class; }
    return $classes;
}

Greets, Dennis
?>
matt-php at DONT-SPAM-ME dot bitdifferent dot com 01-Nov-2004 04:52
PHP (4 at least, dunno about 5) stores classnames in lower case, so:

<?PHP

class Foo
{
}

class Bar extends Foo
{
}

echo get_parent_class('Bar');

echo "\n";

echo get_parent_class('bar');

?>

will output:

foo
foo
radu dot rendec at ines dot ro 07-Apr-2004 03:44
If the argument obj is a string and the class is not defined, then the function returns FALSE.

If the argument obj is an object created from a class with no ancestors (or a string representing a class with no ancestors), then the function returns FALSE.
tim at correctclick dot com 06-Apr-2003 04:48
A slightly more cryptic but faster get_ancestors function:

function get_ancestors ($class) {

      for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = $class);
      return $classes;

}

(The second part of the for is implicitly testing for $class != "").  Recursion is considerably slower than looping, so you probably want to use this function.

Hope someone finds it useful.
eric dot brison at anakeen dot com 28-Jan-2002 01:14
To return all ancestors class of an object

function get_ancestors_class($classname) {
  $father = get_parent_class($classname);

  if ($father != "") {

    $ancestors = get_ancestors_class($father);
    $ancestors[] = $father;
  }
  return $ancestors;
}

example :
-----------
Class C  {

}

Class B extends C {

}

Class A extends B {

}
print_r (get_ancestors_class("a"));
print_r (get_ancestors_class("b"));

example result :
---------------
Array
(
    [0] => c
    [1] => b
)
Array
(
    [0] => c
)


인터페이스가 정의되었는지 확인

  • ≪ get_parent_class
  • is_a ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 인터페이스가 정의되었는지 확인

interface_exists

(PHP 5 >= 5.0.2, PHP 7)

interface_exists — 인터페이스가 정의되었는지 확인

설명

bool interface_exists ( string $interface_name [, bool $autoload = true ] )

주어진 인터페이스가 정의되었는지 확인합니다.

인수

interface_name

인터페이스명

autoload

__autoload 호출 여부, 기본값은 꺼짐

반환값

주어진 interface_name이 정의되었으면 TRUE, 아니면 FALSE를 반환합니다.

예제

Example #1 interface_exists() 예제

<?php
// 사용하기 전에 인터페이스가 존재하는지 확인
if (interface_exists('MyInterface')) {
    class MyClass implements MyInterface
    {
        // 메쏘드
    }
}

?>

참고

  • class_exists() - 클래스가 정의되었는지 확인

User Contributed Notes

CoR 28-Nov-2015 11:30
Class and Interface share SAME namespace!

class k{}

interface k {}    // Fatal error: Cannot redeclare class k
maxim at inbox dot ru 01-Oct-2012 07:40
If you want to check for included interface and you already register spl autoloader - it will crash. Becassue autoloader trying to load `string` and he doesnt matter is it class or not.
Iv found several ways :
1 - unregister AL - -> check for Ifaces - -> register Autoloader

2 - $ifaces = array_flip(get_declared_interfaces());
 if($ifaces["MyIface"]) // empty // isset .

Interfaces are not bad, you can build correct geomentry of system , with validation by funcs / vars / const .
Also they are good to storage variables <?php
 ROOT::THEMES ; ROOT::LOC ; ?> . Much faster then Define, but you cant put algorithms inside, only complite strings / __file__ / etc.
nils dot rocine at gmail dot com 27-Oct-2011 03:35
A little note on namespaces that may be obvious to some, but was not obvious to me.

Although you can make the below statement when the statement is in the same namespace as the interface/class declaration MyInterface...
<?php
$foo instanceof MyInterface
?>

Making use of the interface_exists, or class_exists functions, you must enter the full namespaced interface name like so (even if the function call is from the same namespace.)
<?php
interface_exists(__NAMESPACE__ . '\MyInterface', false);
?>
andrey at php dot net 24-Oct-2004 01:40
As far as I remember interface_exists() was added in 5.0.2 . In 5.0.0 and 5.0.1 class_exists() used to return TRUE when asked for a existing interface. Starting 5.0.2 class_exists() doesn't do that anymore.


객체가 이 클래스나 부모 클래스 중 하나인지 확인

  • ≪ interface_exists
  • is_subclass_of ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 객체가 이 클래스나 부모 클래스 중 하나인지 확인

is_a

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

is_a — 객체가 이 클래스나 부모 클래스 중 하나인지 확인

설명

bool is_a ( object $object , string $class_name )

주어진 object가 이 클래스나 부모 클래스 중 하나인지 확인합니다.

인수

object

확인할 객체

class_name

클래스명

반환값

객체가 이 클래스나 부모 클래스 중 하나이면 TRUE, 아니면 FALSE를 반환합니다.

변경점

버전 | 설명
5.3.0 | 이 함수는 배제되지 않고, E_STRICT 경고가 발생하지 않습니다.
5.0.0 | is_a() 함수는 instanceof 연산자로 인해 PHP 5에서 배제되었습니다. 이 함수를 호출하면 E_STRICT 경고가 발생합니다.

예제

Example #1 is_a() 예제

<?php
// 클래스 정의
class WidgetFactory
{
  var $oink = 'moo';
}

// 새 객체 생성
$WF = new WidgetFactory();

if (is_a($WF, 'WidgetFactory')) {
  echo "yes, \$WF is still a WidgetFactory\n";
}
?>

Example #2 PHP 5에서 instanceof 연산자 사용하기

<?php
if ($WF instanceof WidgetFactory) {
    echo 'Yes, $WF is a WidgetFactory';
}
?>

참고

  • get_class() - 객체의 클래스명을 반환
  • get_parent_class() - 객체나 클래스의 부모 클래스명을 얻습니다
  • is_subclass_of() - 이 클래스가 부모 클래스의 자식인지 확인

User Contributed Notes

Ronald Locke 30-Sep-2016 11:19
Please note that you have to fully qualify the class name in the second parameter.

A use statement will not resolve namespace dependencies in that is_a() function.

<?php
namespace foo\bar;

class A {};
class B extends A {};
?>

<?php
namespace har\var;

use foo\bar\A;
$foo = new foo\bar\B();

is_a($foo, 'A'); // returns false;
is_a($foo, 'foo\bar\A'); // returns true;
?>

Just adding that note here because all examples are without namespaces.
portugal {at} jawira {dot} com 13-Jun-2015 06:32
I just want to point out that you can replace "is_a()" function with the "instanceof" operator, BUT you must use a variable to pass the class name string.

This will work:
<?php
$object = new \stdClass();
$class_name = '\stdClass';

var_dump(is_a($object, $class_name));     // bool(true)
var_dump(is_a($object, '\stdClass'));     // bool(true)
var_dump($object instanceof $class_name); // bool(true)
?>

While this don't:
<?php
$object = new \stdClass();
var_dump($object instanceof '\stdClass'); // Parse error: syntax error, unexpected ''\stdClass'' (T_CONSTANT_ENCAPSED_STRING)
?>
eitan at mosenkis dot net 12-Jan-2012 10:09
As of PHP 5.3.9, is_a() seems to return false when passed a string for the first argument. Instead, use is_subclass_of() and, if necessary for your purposes, also check if the two arguments are equal, since is_subclass_of('foo', 'foo') will return false, while is_a('foo', 'foo') used to return true.
Aron Budinszky 07-Sep-2011 01:21
Be careful! Starting in PHP 5.3.7 the behavior of is_a() has changed slightly: when calling is_a() with a first argument that is not an object, __autoload() is triggered!

In practice, this means that calling is_a('23', 'User'); will trigger __autoload() on "23". Previously, the above statement simply returned 'false'.

More info can be found here:
https://bugs.php.net/bug.php?id=55475

Whether this change is considered a bug and whether it will be reverted or kept in future versions is yet to be determined, but nevertheless it is how it is, for now...
p dot scheit at zweipol dot net 16-Jan-2007 10:44
At least in PHP 5.1.6 this works as well with Interfaces.

<?php
interface test {
  public function A();
}

class TestImplementor implements test {
  public function A () {
    print "A";
  }
}

$testImpl = new TestImplementor();

var_dump(is_a($testImpl,'test'));
?>

will return true
cesoid at yahoo dot com 06-Oct-2005 04:01
is_a returns TRUE for instances of children of the class.

For example:

class Animal
{}

class Dog extends Animal
{}

$test = new Dog();

In this example is_a($test, "Animal") would evaluate to TRUE as well as is_a($test, "Dog").

This seemed intuitive to me, but did not seem to be documented.


이 클래스가 부모 클래스의 자식인지 확인

  • ≪ is_a
  • method_exists ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 이 클래스가 부모 클래스의 자식인지 확인

is_subclass_of

(PHP 4, PHP 5, PHP 7)

is_subclass_of — 이 클래스가 부모 클래스의 자식인지 확인

설명

bool is_subclass_of ( mixed $object , string $class_name )

주어진 object의 부모 중에 class_name 클래스가 존재하는지 확인합니다.

인수

object

클래스명이나 객체 인스턴스

class_name

클래스명

반환값

object가 class_name의 자식 클래스면 TRUE, 아니면 FALSE를 반환합니다.

변경점

버전 | 설명
5.0.3 | object 인수를 문자열(클래스명)로 지정할 수 있습니다.

예제

Example #1 is_subclass_of() 예제

<?php
// 클래스 정의
class WidgetFactory
{
  var $oink = 'moo';
}

// 자식 클래스 정의
class WidgetFactory_Child extends WidgetFactory
{
  var $oink = 'oink';
}

// 새 객체 생성
$WF = new WidgetFactory();
$WFC = new WidgetFactory_Child();

if (is_subclass_of($WFC, 'WidgetFactory')) {
  echo "yes, \$WFC is a subclass of WidgetFactory\n";
} else {
  echo "no, \$WFC is not a subclass of WidgetFactory\n";
}

if (is_subclass_of($WF, 'WidgetFactory')) {
  echo "yes, \$WF is a subclass of WidgetFactory\n";
} else {
  echo "no, \$WF is not a subclass of WidgetFactory\n";
}

// PHP 5.3.0부터 사용 가능
if (is_subclass_of('WidgetFactory_Child', 'WidgetFactory')) {
  echo "yes, WidgetFactory_Child is a subclass of WidgetFactory\n";
} else {
  echo "no, WidgetFactory_Child is not a subclass of WidgetFactory\n";
}
?>

위 예제의 출력:

yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory
yes, WidgetFactory_Child is a subclass of WidgetFactory

참고

  • get_class() - 객체의 클래스명을 반환
  • get_parent_class() - 객체나 클래스의 부모 클래스명을 얻습니다
  • is_a() - 객체가 이 클래스나 부모 클래스 중 하나인지 확인

User Contributed Notes

nicholas at aquarionics dot com 17-Mar-2009 10:02
This might be useful to someone, so:

If you're using Autoload, you should be aware that this will attempt to autoload $classname if it isn't already loaded. I discovered this when I had something using is_subclass_of inside an error thrown by autoload, which then recursed until it ran out of memory.
Damien Bezborodov 17-Mar-2009 01:55
If you need something similar to is_subclass_of() to determine if a class implements an interface before instantiating it, use reflection:

<?php

interface A_Interface {}
class A implements A_Interface {}

$reflectionA = new ReflectionClass('A');
var_dump(
    $reflectionA->implementsInterface('A_Interface')
);

?>
bool(true)
kostyl_kostyl gav-gav mail point ru 08-Mar-2009 12:08
<?php
interface I {
}
class A implements I {
}
class B extends A  {
}
if (is_subclass_of('A', 'I')) {
    echo 'good<br>';
}
else {
    echo 'bad<br>';
}
if (is_subclass_of('B', 'I')) {
    echo 'good<br>';
}
else {
    echo 'bad<br>';
}
if (is_subclass_of('B', 'A')) {
    echo 'good<br>';
}
else {
    echo 'bad<br>';
}
?>

result:
bad <- you must to describe intermediate class B to be good
good
good
jm 01-Oct-2008 05:02
It would appear that is_subclass_of is case insensitive unlike get_class in php5.
i.e.

<?php
class fooBar {}
class bar extends fooBar {}

assert(get_class(new fooBar()) == "fooBar");
assert(is_subclass_of(new bar(), "foobar") == true);
?>

i run across this while migrating some code from php4 to php5 and the code would only half-the-time break.
Ondra Zizka 13-Oct-2006 02:31
For PHP4:

<?php
/** Returns whether specified class is subclass of the other class. */
function is_subclass($sClass, $sExpectedParentClass){
    do if( $sExpectedParentClass === $sClass ) return true;
    while( false != ($sClass = get_parent_class($sClass)) );
    return false;
}
// Test:
class A {} class B extends A {} class C extends B {} echo (int) is_subclass('C', 'A');
?>
gunniboyh at web dot de 20-May-2006 03:16
is_subclass_of() works also with classes between the class of obj and the superclass.

example:
<?php
class A {};
class B extends A {};
class C extends B {};

$foo=new C();
echo ((is_subclass_of($foo,'A')) ? 'true' : 'false');
?>

echoes 'true' .


클래스 메쏘드가 존재하는지 확인

  • ≪ is_subclass_of
  • property_exists ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 클래스 메쏘드가 존재하는지 확인

method_exists

(PHP 4, PHP 5, PHP 7)

method_exists — 클래스 메쏘드가 존재하는지 확인

설명

bool method_exists ( mixed $object , string $method_name )

주어진 object에 클래스 메쏘드가 존재하는지 확인합니다.

인수

object

객체 인스턴스나 클래스 이름

method_name

메쏘드 이름

반환값

주어진 object에 주어진 method_name이 정의되어 있으면 TRUE, 아니면 FALSE를 반환합니다.

예제

Example #1 method_exists() 예제

<?php
$directory = new Directory('.');
var_dump(method_exists($directory,'read'));
?>

위 예제의 출력:

bool(true)

Example #2 정적 method_exists() 예제

<?php
var_dump(method_exists('Directory','read'));
?>

위 예제의 출력:

bool(true)

참고

  • function_exists() - Return TRUE if the given function has been defined
  • is_callable() - 변수 내용을 함수처럼 호출할 수 있는지 확인

User Contributed Notes

jave [at] issomewhereontheweb [com] 02-Jun-2016 02:15
The function does not care about class existance, so you can use it to check an existance of a method even when class was not declared e.g.
<?php
if( method_exists('THIS_WAS_NOT_DECLARED', 'some_method') )
   echo "it does exist!";
else
   echo "nope, it is not there...";
?>
...raises no errors/warnings... outputs "nope, it is not there..." if class does not exist at all or if it does but the method doesn't.
w3dk 05-Apr-2015 07:38
A couple of the older comments (specifically "jp at function dot fi" and "spam at majiclab dot com") state that is_callable() does not respect a methods visibility when checked outside of that class. ie. That private/protected methods are seen as callable when tested publicly. However, this was a bug (#29210) in early versions of PHP 5 and was fixed (according to the changelog) in PHP 5.0.5 (and/or PHP 5.1.0).

Bug #29210 - Function: is_callable - no support for private and protected classes
http://bugs.php.net/29210

Changelog - Fixed bug #29210 (Function: is_callable - no support for private and protected classes). (Dmitry)
http://php.net/ChangeLog-5.php#5.1.0
charleston olaes at gmail no spaces 10-Oct-2014 01:31
I was wondering if caching the the methods in an array would have a faster lookup. So I ran a very simple benchmark using xdebug_time_index() with 10000 iterations on each statement.

using PHP 5.3.13 btw

<?php
// using an actual instance of an object shows 0.10398316383362 secs
method_exist($object, $method);
?>

<?php
// using a string shows 0.12779307365417 secs
method_exist('ClassName', $method);
?>

<?php
$array = array(/*method names with numeric index*/);
// shows 0.10288906097412 secs
in_array($method, $array);
?>

<?php
$assoc = array( /*method name as key and value*/ );
// shows 0.017536878585815 secs
isset( $assoc[$method] );
?>

From the looks of the results, there is very little difference in method_exist and in_array. Isset seems to the fastest and using a string as the first parameter is the slowest.

Please note that the test was done on multiple methods, not just one, the code presented above is to show the results, not the actual test code that ran. Also, this was tested just out of curiosity and I didn't set up a specific environment or used any profiling tools, and it was not meant to be an official benchmark in anyway.
konzertheld at thedomainistheusersname dot de 15-Feb-2013 02:32
Note that prepending the namespace (if any) is required even if the calling class is in the same namespace:

<?php
namespace test;
class foo {
 public function lookup() {
  // will return false
  return method_exists('bar', 'nonsense_method');
 }
}

class bar {
 public function nonsense_method() {
  // will return true
  return method_exists('test\foo', 'lookup');
 }
}
?>
Niels 11-May-2011 09:45
Just to mention it: both method_exists() and is_callable() return true for inherited methods:

<?php
class ParentClass {

   function doParent() { }
}

class ChildClass extends ParentClass { }

$p = new ParentClass();
$c = new ChildClass();

// all return true
var_dump(method_exists($p, 'doParent'));
var_dump(method_exists($c, 'doParent'));

var_dump(is_callable(array($p, 'doParent')));
var_dump(is_callable(array($c, 'doParent')));
?>
mail at bartrail dot de 25-Apr-2011 07:06
Using method_exists inside an object's __call() method can be very usefull if you want to avoid to get a fatal error because of a limit in function nesting or if you are calling methods that dont exist but need to continue in your application:

<?php
class Something
{

    /**
     * Call a method dynamically
     *
     * @param string $method
     * @param array $args
     * @return mixed
     */
    public function __call($method, $args)
    {
        if(method_exists($this, $method)) {
          return call_user_func_array(array($this, $method), $args);
        }else{
          throw new Exception(sprintf('The required method "%s" does not exist for %s', $method, get_class($this)));
        }
    }

}
?>
phoenix at todofixthis dot com 21-Dec-2010 06:13
As noted [elsewhere] method_exists() does not care about the existence of __call(), whereas is_callable() does:

<?php
class Test {
  public function explicit(  ) {
      // ...
  }

  public function __call( $meth, $args ) {
      // ...
  }
}

$Tester = new Test();

var_export(method_exists($Tester, 'anything')); // false
var_export(is_callable(array($Tester, 'anything'))); // true
?>
uramihsayibok, gmail, com 07-Aug-2010 07:09
It wasn't spelled out but could be inferred: method_exists() also works on interfaces.

<?php

var_dump(method_exists("Iterator", "current"));
// bool(true)

?>
Anonymous 08-May-2010 02:20
If you want to check in a class itself if a method is known you may do so with magic variable __CLASS__

<?php

class A{
  __construct($method){
      return method_exists(__CLASS__,$method);
  }

  private function foo(){

  }
}

$test = new A('foo');
//should return true

?>

You might also use the method describe below with <?php in_array() ?> trick but I consider this one here easier and more readable and well, the way it is intended toi be done ;)
admin ( at ) djokodonev dot com 17-Oct-2009 01:30
Hi,

Here is a useful function that  you can use to check classes methods access e.g whether it is public, private or static or both..

here it goes:

<?php
// Example class
class myClass {

    private $private1;

    static $static1;

    public $public1;

    public function publ() {

    }

    private function priv() {

    }

    private static function privstatic() {

    }

    public static function publstatic() {

    }

    static function mytest() {

    }
}

// The function uses the reflection class that is built into PHP!!!
// The purpose is to determine the type of a certain method that exi
function is_class_method($type="public", $method, $class) {
   // $type = mb_strtolower($type);
    $refl = new ReflectionMethod($class, $method);
    switch($type) {
        case "static":
        return $refl->isStatic();
        break;
        case "public":
        return $refl->isPublic();
        break;
        case "private":
        return $refl->isPrivate();
        break;
    }
}
var_dump(is_class_method("static", "privstatic", "myClass")); // true - the method is  private and also static..
var_dump(is_class_method("private", "privstatic", "myClass")); // true - the method is  private and also static..
var_dump(is_class_method("private", "publstatic", "myClass")); // False the methos is public and also static not private
 // you get the idea.. I hope this helps someone..
?>
florin from syneto net 07-Oct-2009 04:34
This function is case-insensitive (as is PHP) and here is the proof:
<?php
class A {
    public function FUNC() { echo '*****'; }
}

$a = new A();
$a->func(); // *****
var_dump(method_exists($a, 'func')); // bool(true)
?>
jp at function dot fi 30-Apr-2006 11:29
As mentioned before, is_callable and method_exists report all methods callable even if they are private/protected and thus actually not callable. So instead of those functions you may use following work-around which reports methods as supposed to.

<?php
class Foo1 {
  public function bar() {
    echo "I'm private Foo1::bar()";
  }
}

class Foo2 {
  private function bar() {
    echo "I'm public Foo2::bar()";
  }
}

$f1=new Foo1;
$f2=new Foo2;

if(is_callable(array($f1,"bar"))) {
    echo "Foo1::bar() is callable";
} else {
    echo "Foo1::bar() isn't callable";
}
if(is_callable(array($f2,"bar"))) {
    echo "Foo2::bar() is callable";
} else {
    echo "Foo2::bar() isn't callable";
}
if(in_array("bar",get_class_methods($f1))) {
    echo "Foo1::bar() is callable";
} else {
    echo "Foo1::bar() isn't callable";
}
if(in_array("bar",get_class_methods($f2))) {
    echo "Foo2::bar() is callable";
} else {
    echo "Foo2::bar() isn't callable";
}

?>

output
Foo1::bar() is callable (correct)
Foo2::bar() is callable (incorrect)
Foo1::bar() is callable (correct)
Foo2::bar() isn't callable (correct)

?>
seufert at gmail dot com 27-Apr-2006 03:27
Just a note that the behaviour of this function changed between version 5.0.x and 5.1.x when using static member functions

Using this code:
<?php
class a {
    static function test() {return "A";}
}
if(method_exists('a','test'))
    print call_user_func(array('a','test'));
else
    print "Nothing";
?>
PHP 5.1.x returns "A"
PHP 5.0.x returns "Nothing"

Im not sure of a workaround for PHP 5.0.x yet.
spam at majiclab dot com 31-Jan-2006 05:33
Both method_exists() and is_callable() return private and protected functions, which, as mentioned below, causes problems for PHP5/OO programming.  You can use get_class_methods() with either an $instance of a class or the 'ClassName' to get only public functions.
daniel at softel dot jp 10-Jan-2006 04:34
Note that in PHP5, method_exists() will sucessfully find *private* methods. This has some OO/data-hiding ramifications.
jpgiot at nospam ifrance.com 06-May-2004 04:42
a little difference :

to find a method of an object (instance of a class)

<?php
if (method_exists($myinstance,'themethod'))
    echo 'ok';
?>

to find a method of a class (using the class name, not the instance of the class!)

<?php
if (is_callable(array('theclassname','themethod')))
    echo 'ok';
?>
Thomas@ThBeckmann 31-Jan-2003 10:47
Though, as Bejamin noted, it's not possible to use the class name in method_exists within the class definition, get_class_methods delivers the method names for a given class name even inside the class. Thus another workaround for the mentioned problem is to use in_array(<method_name>, get_class_methods(<class_name>))
benjamin_ansbach at web dot de 27-Dec-2002 09:49
if you want to check for a method "inside" of a class use:

method_exists($this, 'function_name')

i was a bit confused 'cause i thought i'm only able to check for a method when i got an object like $object_name = new class_name() with:

method_exists($object_name, 'method_name')

small example for those who didn't understood what i mean ( maybe caused by bad english :) ):

<?php

class a {

    function a() {

        if(method_exists($this, 'test'))
            echo 'a::test() exists!';
        else
            echo 'a::test() doesn\'t exists';

    }

    function test() {

        return true;

    }

}

$b = new a();

?>

the output will be: a::test() exists!

maybe this will help someone
j dot metzger at steptown dot com 16-Jan-2002 12:42
call_user_method uses the same mechanism as a normal method call. So you can get the returned values as well in this way.

$pagetext=call_user_method($method,$object_call);

All information is then in $pagetext.


객체나 클래스가 프로퍼티를 가졌는지 확인

  • ≪ method_exists
  • trait_exists ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • 객체나 클래스가 프로퍼티를 가졌는지 확인

property_exists

(PHP 5 >= 5.1.0, PHP 7)

property_exists — 객체나 클래스가 프로퍼티를 가졌는지 확인

설명

bool property_exists ( mixed $class , string $property )

주어진 property가 지정한 클래스에 존재하는지 확인합니다.

Note:

isset()과 다르게, property_exists()는 프로퍼티가 NULL 값을 가지고 있어도 TRUE를 반환합니다.

인수

class

확인할 클래스명이나 클래스의 객체

property

프로퍼티명

반환값

프로퍼티가 존재하면 TRUE, 존재하지 않으면 FALSE, 오류시엔 NULL을 반환합니다.

변경점

버전 | 설명
5.3.0 | 이 함수는 접근성에 관계 없이 프로퍼티의 존재성을 확인합니다.

예제

Example #1 property_exists() 예제

<?php

class myClass {
    public $mine;
    private $xpto;
    static protected $test;

    static function test() {
        var_dump(property_exists('myClass', 'xpto')); //true
    }
}

var_dump(property_exists('myClass', 'mine'));   //true
var_dump(property_exists(new myClass, 'mine')); //true
var_dump(property_exists('myClass', 'xpto'));   //true, as of PHP 5.3.0
var_dump(property_exists('myClass', 'bar'));    //false
var_dump(property_exists('myClass', 'test'));   //true, as of PHP 5.3.0
myClass::test();

?>

참고

  • method_exists() - 클래스 메쏘드가 존재하는지 확인

User Contributed Notes

saurabh dot agarwal89 at gmail dot com 27-May-2015 08:17
$a = array('a','b'=>'c');
print_r((object) $a);
var_dump( property_exists((object) $a,'0'));
var_dump( property_exists((object) $a,'b'));

OUTPUT:
stdClass Object
(
    [0] => a
    [b] => c
)
bool(false)
bool(true)
g dot gentile at parentesigraffe dot com 05-Mar-2015 06:02
The function behaves differently depending on whether the property has been present in the class declaration, or has been added dynamically, if the variable has been unset()

<?php

class TestClass {

    public $declared = null;

}

$testObject = new TestClass;

var_dump(property_exists("TestClass", "dynamic")); // boolean false, as expected
var_dump(property_exists($testObject, "dynamic")); // boolean false, same as above

$testObject->dynamic = null;
var_dump(property_exists($testObject, "dynamic")); // boolean true

unset($testObject->dynamic);
var_dump(property_exists($testObject, "dynamic")); // boolean false, again.

var_dump(property_exists($testObject, "declared")); // boolean true, as espected

unset($testObject->declared);
var_dump(property_exists($testObject, "declared")); // boolean true, even if has been unset()
ewisuri [gmail] 15-Feb-2014 01:37
As of PHP 5.3.0, calling property_exists from a parent class sees private properties in sub-classes.

<?php
class P {
    public function test_prop($prop) { return property_exists($this, $prop); }
}

class Child extends P {
    private $prop1;
}

$child = new Child();
var_dump($child->test_prop('prop1')); //true, as of PHP 5.3.0
berimbolo 02-Feb-2014 10:44
When you are testing for a property that has been added dynamically, you will need to provide a reference to the instance rather than the class name for the first parameter.

For example, for this code snippet:

class myClass {
public $var;
}

$obj = new myClass;
$obj->new_var = "new";

var_dump(property_exists('myClass', 'new_var') );
var_dump(property_exists($obj, 'new_var') );

Returns:
boolean false
boolean true
Nanhe Kumar 18-Jan-2014 07:39
<?php

class Student {

    protected $_name;
    protected $_email;

    public function __call($name, $arguments) {
        $action = substr($name, 0, 3);
        switch ($action) {
            case 'get':
                $property = '_' . strtolower(substr($name, 3));
                if(property_exists($this,$property)){
                    return $this->{$property};
                }else{
                    echo "Undefined Property";
                }
                break;
            case 'set':
                $property = '_' . strtolower(substr($name, 3));
                if(property_exists($this,$property)){
                    $this->{$property} = $arguments[0];
                }else{
                    echo "Undefined Property";
                }

                break;
            default :
                return FALSE;
        }
    }

}

$s = new Student();
$s->setName('Nanhe Kumar');
$s->setEmail('nanhe.kumar@gmail.com');
echo $s->getName(); //Nanhe Kumar
echo $s->getEmail(); // nanhe.kumar@gmail.com
$s->setAge(10); //Undefined Property
?>
Stefan W 03-Sep-2013 10:23
If you are in a namespaced file, and you want to pass the class name as a string, you will have to include the full namespace for the class name - even from inside the same namespace:

<?
namespace MyNS;

class A {
    public $foo;
}

property_exists("A", "foo");          // false
property_exists("\\MyNS\\A", "foo");  // true
?>


Checks if the trait exists

  • ≪ property_exists
  • Classkit ≫
  • PHP Manual
  • 클래스/객체 함수 목록
  • Checks if the trait exists

trait_exists

(PHP 5 >= 5.4.0, PHP 7)

trait_exists — Checks if the trait exists

설명

bool trait_exists ( string $traitname [, bool $autoload ] )

인수

traitname

Name of the trait to check

autoload

Whether to autoload if not already loaded.

반환값

Returns TRUE if trait exists, FALSE if not, NULL in case of an error.

User Contributed Notes

valerio dot bozzolan at gmail dot com 29-Jan-2016 03:38
What is the default value of $autoload? And in which way traits are autoloaded? Is there something as spl_autoload() for traits?
Lubaev.K 19-Jun-2013 11:20
<?php
trait World {

    private static $instance;
    protected $tmp;

    public static function World()
    {
        self::$instance = new static();
        self::$instance->tmp = get_called_class().' '.__TRAIT__;

        return self::$instance;
    }

}

if ( trait_exists( 'World' ) ) {

    class Hello {
        use World;

        public function text( $str )
        {
            return $this->tmp.$str;
        }
    }

}

echo Hello::World()->text('!!!'); // Hello World!!!


Classkit

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • Classkit 함수 목록
    • classkit_import — Import new class method definitions from a file
    • classkit_method_add — Dynamically adds a new method to a given class
    • classkit_method_copy — Copies a method from class to another
    • classkit_method_redefine — Dynamically changes the code of the given method
    • classkit_method_remove — Dynamically removes the given method
    • classkit_method_rename — Dynamically changes the name of the given method

User Contributed Notes

There are no user contributed notes for this page.


소개

These functions allow the dynamic manipulation of PHP classes, at runtime.

Note:

This extension has been replaced by runkit, which is not limited to class manipulation but has function manipulation, as well.

User Contributed Notes

There are no user contributed notes for this page.


설치

이 확장은 관리되지 않고 죽은 상태로 보입니다. 그러나, 이 확장에 대한 소스 코드는 PECL SVN에 존재합니다: » http://svn.php.net/viewvc/pecl/classkit.

User Contributed Notes

There are no user contributed notes for this page.


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

CLASSKIT_ACC_PRIVATE (int)
Marks the method private
CLASSKIT_ACC_PROTECTED (int)
Marks the method protected
CLASSKIT_ACC_PUBLIC (int)
Marks the method public

User Contributed Notes

There are no user contributed notes for this page.


Classkit 함수 목록

Table of Contents

  • classkit_import — Import new class method definitions from a file
  • classkit_method_add — Dynamically adds a new method to a given class
  • classkit_method_copy — Copies a method from class to another
  • classkit_method_redefine — Dynamically changes the code of the given method
  • classkit_method_remove — Dynamically removes the given method
  • classkit_method_rename — Dynamically changes the name of the given method

User Contributed Notes

There are no user contributed notes for this page.


Import new class method definitions from a file

  • ≪ Classkit 함수 목록
  • classkit_method_add ≫
  • PHP Manual
  • Classkit 함수 목록
  • Import new class method definitions from a file

classkit_import

(PECL classkit >= 0.3)

classkit_import — Import new class method definitions from a file

설명

array classkit_import ( string $filename )

Note: This function cannot be used to manipulate the currently running (or chained) method.

Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

인수

filename

The filename of the class method definitions to import

반환값

Associative array of imported methods

예제

Example #1 classkit_import() example

<?php
// file: newclass.php
class Example {
    function foo() {
        return "bar!\n";
    }
}
?>
<?php
// requires newclass.php (see above)
class Example {
    function foo() {
        return "foo!\n";
    }
}

$e = new Example();

// output original
echo $e->foo();

// import replacement method
classkit_import('newclass.php');

// output imported
echo $e->foo();

?>

위 예제의 출력:

foo!
bar!

참고

  • classkit_method_add() - Dynamically adds a new method to a given class
  • classkit_method_copy() - Copies a method from class to another

User Contributed Notes

There are no user contributed notes for this page.


Dynamically adds a new method to a given class

  • ≪ classkit_import
  • classkit_method_copy ≫
  • PHP Manual
  • Classkit 함수 목록
  • Dynamically adds a new method to a given class

classkit_method_add

(PECL classkit >= 0.1)

classkit_method_add — Dynamically adds a new method to a given class

설명

bool classkit_method_add ( string $classname , string $methodname , string $args , string $code [, int $flags = CLASSKIT_ACC_PUBLIC ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

인수

classname

The class to which this method will be added

methodname

The name of the method to add

args

Comma-delimited list of arguments for the newly-created method

code

The code to be evaluated when methodname is called

flags

The type of method to create, can be CLASSKIT_ACC_PUBLIC, CLASSKIT_ACC_PROTECTED or CLASSKIT_ACC_PRIVATE

Note:

This parameter is only used as of PHP 5, because, prior to this, all methods were public.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 classkit_method_add() example

<?php
class Example {
    function foo() {
        echo "foo!\n";
    }
}

// create an Example object
$e = new Example();

// Add a new public method
classkit_method_add(
    'Example',
    'add',
    '$num1, $num2',
    'return $num1 + $num2;',
    CLASSKIT_ACC_PUBLIC
);

// add 12 + 4
echo $e->add(12, 4);
?>

위 예제의 출력:

16

참고

  • classkit_method_copy() - Copies a method from class to another
  • classkit_method_redefine() - Dynamically changes the code of the given method
  • classkit_method_remove() - Dynamically removes the given method
  • classkit_method_rename() - Dynamically changes the name of the given method
  • create_function() - Create an anonymous (lambda-style) function

User Contributed Notes

Lester 03-Apr-2008 06:54
If you creates methods by using classkit_method_add(), they are not visible in get_class_methods().

class A
{
       public function foo()
       {
            return "foo!\n";
       }
}

classkit_method_redefine('A','bar','','return "bar !\n";',CLASSKIT_ACC_PUBLIC);

print_r(get_class_methods('A'));
// writes only "foo"


Copies a method from class to another

  • ≪ classkit_method_add
  • classkit_method_redefine ≫
  • PHP Manual
  • Classkit 함수 목록
  • Copies a method from class to another

classkit_method_copy

(PECL classkit >= 0.2)

classkit_method_copy — Copies a method from class to another

설명

bool classkit_method_copy ( string $dClass , string $dMethod , string $sClass [, string $sMethod ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

인수

dClass

Destination class for copied method

dMethod

Destination method name

sClass

Source class of the method to copy

sMethod

Name of the method to copy from the source class. If this parameter is omitted, the value of dMethod is assumed.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 classkit_method_copy() example

<?php
class Foo {
    function example() {
        return "foo!\n";
    }
}

class Bar {
    // initially, no methods
}

// copy the example() method from the Foo class to the Bar class, as baz()
classkit_method_copy('Bar', 'baz', 'Foo', 'example');

// output copied function
echo Bar::baz();
?>

위 예제의 출력:

foo!

참고

  • classkit_method_add() - Dynamically adds a new method to a given class
  • classkit_method_redefine() - Dynamically changes the code of the given method
  • classkit_method_remove() - Dynamically removes the given method
  • classkit_method_rename() - Dynamically changes the name of the given method

User Contributed Notes

There are no user contributed notes for this page.


Dynamically changes the code of the given method

  • ≪ classkit_method_copy
  • classkit_method_remove ≫
  • PHP Manual
  • Classkit 함수 목록
  • Dynamically changes the code of the given method

classkit_method_redefine

(PECL classkit >= 0.1)

classkit_method_redefine — Dynamically changes the code of the given method

설명

bool classkit_method_redefine ( string $classname , string $methodname , string $args , string $code [, int $flags = CLASSKIT_ACC_PUBLIC ] )

Note: This function cannot be used to manipulate the currently running (or chained) method.

Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

인수

classname

The class in which to redefine the method

methodname

The name of the method to redefine

args

Comma-delimited list of arguments for the redefined method

code

The new code to be evaluated when methodname is called

flags

The redefined method can be CLASSKIT_ACC_PUBLIC, CLASSKIT_ACC_PROTECTED or CLASSKIT_ACC_PRIVATE

Note:

This parameter is only used as of PHP 5, because, prior to this, all methods were public.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 classkit_method_redefine() example

<?php
class Example {
    function foo() {
        return "foo!\n";
    }
}

// create an Example object
$e = new Example();

// output Example::foo() (before redefine)
echo "Before: " . $e->foo();

// Redefine the 'foo' method
classkit_method_redefine(
    'Example',
    'foo',
    '',
    'return "bar!\n";',
    CLASSKIT_ACC_PUBLIC
);

// output Example::foo() (after redefine)
echo "After: " . $e->foo();
?>

위 예제의 출력:

Before: foo!
After: bar!

참고

  • classkit_method_add() - Dynamically adds a new method to a given class
  • classkit_method_copy() - Copies a method from class to another
  • classkit_method_remove() - Dynamically removes the given method
  • classkit_method_rename() - Dynamically changes the name of the given method

User Contributed Notes

fdrean 03-Sep-2016 09:22
This could lead to revolution in cli mode now that developers can load new code without restarting process.


Dynamically removes the given method

  • ≪ classkit_method_redefine
  • classkit_method_rename ≫
  • PHP Manual
  • Classkit 함수 목록
  • Dynamically removes the given method

classkit_method_remove

(PECL classkit >= 0.1)

classkit_method_remove — Dynamically removes the given method

설명

bool classkit_method_remove ( string $classname , string $methodname )

Note: This function cannot be used to manipulate the currently running (or chained) method.

Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

인수

classname

The class in which to remove the method

methodname

The name of the method to remove

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 classkit_method_remove() example

<?php
class Example {
    function foo() {
        return "foo!\n";
    }

    function bar() {
        return "bar!\n";
    }
}

// Remove the 'foo' method
classkit_method_remove(
    'Example',
    'foo'
);

echo implode(' ', get_class_methods('Example'));

?>

위 예제의 출력:

bar

참고

  • classkit_method_add() - Dynamically adds a new method to a given class
  • classkit_method_copy() - Copies a method from class to another
  • classkit_method_redefine() - Dynamically changes the code of the given method
  • classkit_method_rename() - Dynamically changes the name of the given method

User Contributed Notes

There are no user contributed notes for this page.


Dynamically changes the name of the given method

  • ≪ classkit_method_remove
  • Ctype ≫
  • PHP Manual
  • Classkit 함수 목록
  • Dynamically changes the name of the given method

classkit_method_rename

(PECL classkit >= 0.1)

classkit_method_rename — Dynamically changes the name of the given method

설명

bool classkit_method_rename ( string $classname , string $methodname , string $newname )

Note: This function cannot be used to manipulate the currently running (or chained) method.

Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

인수

classname

The class in which to rename the method

methodname

The name of the method to rename

newname

The new name to give to the renamed method

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 classkit_method_rename() example

<?php
class Example {
    function foo() {
        return "foo!\n";
    }
}

// Rename the 'foo' method to 'bar'
classkit_method_rename(
    'Example',
    'foo',
    'bar'
);

// output renamed function
echo Example::bar();
?>

위 예제의 출력:

foo!

참고

  • classkit_method_add() - Dynamically adds a new method to a given class
  • classkit_method_copy() - Copies a method from class to another
  • classkit_method_redefine() - Dynamically changes the code of the given method
  • classkit_method_remove() - Dynamically removes the given method

User Contributed Notes

There are no user contributed notes for this page.


Character type checking

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • Ctype 함수 목록
    • ctype_alnum — Check for alphanumeric character(s)
    • ctype_alpha — Check for alphabetic character(s)
    • ctype_cntrl — Check for control character(s)
    • ctype_digit — Check for numeric character(s)
    • ctype_graph — Check for any printable character(s) except space
    • ctype_lower — Check for lowercase character(s)
    • ctype_print — Check for printable character(s)
    • ctype_punct — Check for any printable character which is not whitespace or an alphanumeric character
    • ctype_space — Check for whitespace character(s)
    • ctype_upper — Check for uppercase character(s)
    • ctype_xdigit — Check for character(s) representing a hexadecimal digit

User Contributed Notes

There are no user contributed notes for this page.


소개

The functions provided by this extension check whether a character or string falls into a certain character class according to the current locale (see also setlocale()).

When called with an integer argument these functions behave exactly like their C counterparts from ctype.h. It means that if you pass an integer smaller than 256 it will use the ASCII value of it to see if it fits in the specified range (digits are in 0x30-0x39). If the number is between -128 and -1 inclusive then 256 will be added and the check will be done on that.

When called with a string argument they will check every character in the string and will only return TRUE if every character in the string matches the requested criteria. When called with an empty string the result will always be TRUE in PHP < 5.1 and FALSE since 5.1.

Passing anything else but a string or integer will return FALSE immediately.

It should be noted that ctype functions are always preferred over regular expressions, and even to some equivalent "str_*" and "is_*" functions. This is because of the fact that ctype uses a native C library and thus processes significantly faster.

Note:

These functions are not related to the Python "ctypes" library at all. The extension name stems from the ctype.h C header file that their C equivalents are defined in.

This extension also predates Python "ctypes" so any confusion caused by this naming is hardly our fault ...

User Contributed Notes

flashinno at gmail dot com 16-Jan-2015 05:12
pcre from the ver. 5.3 is a part of core PHP, any function exported from .so can't work wih faster than core function


요구 조건

None besides functions from the standard C library which are always available.

User Contributed Notes

There are no user contributed notes for this page.


설치

Beginning with PHP 4.2.0 these functions are enabled by default. For older versions you have to configure and compile PHP with --enable-ctype . You can disable ctype support with --disable-ctype .

PHP 윈도우 버전에서는 이 확장에 대한 지원이 포함되어 있습니다. 이 함수들을 이용하기 위해서 추가로 확장을 읽어들일 필요가 없습니다.

Note: Builtin support for ctype is available with PHP 4.3.0.

User Contributed Notes

php at darkain dot com 02-Oct-2016 06:41
On FreeBSD, this library is NOT included automatically. It must be manually installed. This can be done with the following command:

pkg install php70-ctype


Ctype 함수 목록

Table of Contents

  • ctype_alnum — Check for alphanumeric character(s)
  • ctype_alpha — Check for alphabetic character(s)
  • ctype_cntrl — Check for control character(s)
  • ctype_digit — Check for numeric character(s)
  • ctype_graph — Check for any printable character(s) except space
  • ctype_lower — Check for lowercase character(s)
  • ctype_print — Check for printable character(s)
  • ctype_punct — Check for any printable character which is not whitespace or an alphanumeric character
  • ctype_space — Check for whitespace character(s)
  • ctype_upper — Check for uppercase character(s)
  • ctype_xdigit — Check for character(s) representing a hexadecimal digit

User Contributed Notes

There are no user contributed notes for this page.


Check for alphanumeric character(s)

  • ≪ Ctype 함수 목록
  • ctype_alpha ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for alphanumeric character(s)

ctype_alnum

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_alnum — Check for alphanumeric character(s)

설명

bool ctype_alnum ( string $text )

Checks if all of the characters in the provided string, text, are alphanumeric.

인수

text

The tested string.

반환값

Returns TRUE if every character in text is either a letter or a digit, FALSE otherwise.

예제

Example #1 A ctype_alnum() example (using the default locale)

<?php
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
    if (ctype_alnum($testcase)) {
        echo "The string $testcase consists of all letters or digits.\n";
    } else {
        echo "The string $testcase does not consist of all letters or digits.\n";
    }
}
?>

위 예제의 출력:

The string AbCd1zyZ9 consists of all letters or digits.
The string foo!#$bar does not consist of all letters or digits.

주의

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_alpha() - Check for alphabetic character(s)
  • ctype_digit() - Check for numeric character(s)
  • setlocale() - Set locale information

User Contributed Notes

marcelocamargo at linuxmail dot org 05-Jan-2016 01:42
It is also important to note that the behavior of `ctype_alnum` differs according to the operating system. For UNIX-based operating system, if you pass a value that is not a string (or an overloaded object), independently of the value, it will always result in false. However, if we do the same on Windows, using, for example, -1 as literal (a minus and a number greater than 0), we'll have true as result.

<?php var_dump(ctype_alnum(-1));
// UNIX: bool(false)
// Windows: bool(true)
someone at example dot com 26-Feb-2015 09:49
Both preg_match('/^[a-zA-Z0-9]+$/', $var) and ctype_alnum returns true if you type only numbers (e.g.: 123456).
Anonymous 29-Jan-2013 02:12
Quicktip: If ctype is not enabled by default on your server, replace ctype_alnum($var) with preg_match('/^[a-zA-Z0-9]+$/', $var).
Rory 07-Dec-2009 01:42
Just for the record, Gentoo doesn't include this function by default. You'll have to recompile PHP with the "ctype" USE flag.
thinice at gmail dot com 22-Jul-2009 11:46
ctype_alnum() is a godsend for quick and easy username/data filtering when used in conjunction with str_replace().

Let's say your usernames have dash(-) and underscore(_) allowable and alphanumeric digits as well.

Instead of a regex you can trade a bit of performance for simplicity:

<?php
$sUser = 'my_username01';
$aValid = array('-', '_');

if(!ctype_alnum(str_replace($aValid, '', $sUser))) {
    echo 'Your username is not properly formatted.';
}
?>


Check for alphabetic character(s)

  • ≪ ctype_alnum
  • ctype_cntrl ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for alphabetic character(s)

ctype_alpha

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_alpha — Check for alphabetic character(s)

설명

bool ctype_alpha ( string $text )

Checks if all of the characters in the provided string, text, are alphabetic. In the standard C locale letters are just [A-Za-z] and ctype_alpha() is equivalent to (ctype_upper($text) || ctype_lower($text)) if $text is just a single character, but other languages have letters that are considered neither upper nor lower case.

인수

text

The tested string.

반환값

Returns TRUE if every character in text is a letter from the current locale, FALSE otherwise.

예제

Example #1 A ctype_alpha() example (using the default locale)

<?php
$strings = array('KjgWZC', 'arf12');
foreach ($strings as $testcase) {
    if (ctype_alpha($testcase)) {
        echo "The string $testcase consists of all letters.\n";
    } else {
        echo "The string $testcase does not consist of all letters.\n";
    }
}
?>

위 예제의 출력:

The string KjgWZC consists of all letters.
The string arf12 does not consist of all letters.

주의

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_upper() - Check for uppercase character(s)
  • ctype_lower() - Check for lowercase character(s)
  • setlocale() - Set locale information

User Contributed Notes

Tyrunur 27-May-2009 11:12
It works if you set the locale correctly:

<?php

setLocale(LC_CTYPE, 'FR_fr.UTF-8');

?>

with this change you will get "yes" "yes"


Check for control character(s)

  • ≪ ctype_alpha
  • ctype_digit ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for control character(s)

ctype_cntrl

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_cntrl — Check for control character(s)

설명

bool ctype_cntrl ( string $text )

Checks if all of the characters in the provided string, text, are control characters. Control characters are e.g. line feed, tab, escape.

인수

text

The tested string.

반환값

Returns TRUE if every character in text is a control character from the current locale, FALSE otherwise.

예제

Example #1 A ctype_cntrl() example

<?php
$strings = array('string1' => "\n\r\t", 'string2' => 'arf12');
foreach ($strings as $name => $testcase) {
    if (ctype_cntrl($testcase)) {
        echo "The string '$name' consists of all control characters.\n";
    } else {
        echo "The string '$name' does not consist of all control characters.\n";
    }
}
?>

위 예제의 출력:

The string 'string1' consists of all control characters.
The string 'string2' does not consist of all control characters.

주의

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_print() - Check for printable character(s)

User Contributed Notes

Tor 10-Mar-2013 07:02
Returns true if bytes are in the range of \x00-\x1f or \x7f (del). Returns false if bytes are in the range of \x20-\x7e or \x80-\xff.


Check for numeric character(s)

  • ≪ ctype_cntrl
  • ctype_graph ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for numeric character(s)

ctype_digit

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_digit — Check for numeric character(s)

설명

bool ctype_digit ( string $text )

Checks if all of the characters in the provided string, text, are numerical.

인수

text

The tested string.

반환값

Returns TRUE if every character in the string text is a decimal digit, FALSE otherwise.

변경점

버전 | 설명
5.1.0 | Before PHP 5.1.0, this function returned TRUE when text was an empty string.

예제

Example #1 A ctype_digit() example

<?php
$strings = array('1820.20', '10002', 'wsl!12');
foreach ($strings as $testcase) {
    if (ctype_digit($testcase)) {
        echo "The string $testcase consists of all digits.\n";
    } else {
        echo "The string $testcase does not consist of all digits.\n";
    }
}
?>

위 예제의 출력:

The string 1820.20 does not consist of all digits.
The string 10002 consists of all digits.
The string wsl!12 does not consist of all digits.

Example #2 A ctype_digit() example comparing strings with integers

<?php

$numeric_string = '42';
$integer        = 42;

ctype_digit($numeric_string);  // true
ctype_digit($integer);         // false (ASCII 42 is the * character)

is_numeric($numeric_string);   // true
is_numeric($integer);          // true
?>

주의

Note:

This function expects a string to be useful, so for example passing in an integer may not return the expected result. However, also note that HTML forms will result in numeric strings and not integers. See also the types section of the manual.

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_alnum() - Check for alphanumeric character(s)
  • ctype_xdigit() - Check for character(s) representing a hexadecimal digit
  • is_numeric() - 변수가 수나 수 문자열인지 확인합니다
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_string() - 변수의 자료형이 문자열인지 확인합니다

User Contributed Notes

zorrosNOSPAMwordsman at NOSPAM dot gmail dot com 11-May-2016 09:34
If you want to verify whether or not a variable contains only digits, you can type cast it to a string and back to int and see if the result is identical. Like so:

<?php

// (bool) TRUE if only digits, FALSE otherwise
$isOnlyDigits = (string) (int) $input === (string) $input;

?>

I haven't benchmarked it, but I'm guessing it's significantly faster then regular expressions.
smicheal2 at gmail dot com 07-Oct-2015 10:21
Please note that ctype_digit() will say true for strings such as '00001', which are not technically valid representations of integers, while saying false to strings such as '-1', which are. It's basically a faster version of the regex /^\d+$/. As the name says, it answers the question "does this string contain only digits" literally. It does not answer "is this a valid representation of an integer". If that's what you want, use is_int(filter_var($val, FILTER_VALIDATE_INT)) instead.
error17191 at gmail dot com 03-Oct-2015 10:41
I just wanted to clarify a flaw in the function is_digit() suggested by "info at directwebsolutions dot nl " ..
It returns true in case of negative integers and false in case of strings that contain negative integers .
 example:
is_digit(-10); // returns ture
is_digit('-10'); // returns false
rlerne at gmail dot com 26-Apr-2013 03:04
Interesting to note that you must pass a STRING to this function, other values won't be typecasted (I figured it would even though above explicitly says string $text).

I.E.

<?PHP
$val = 42; //Answer to life
$x = ctype_digit($val);
?>

Will return false, even though, when typecasted to string, it would be true.

<?PHP
$val = '42';
$x = ctype_digit($val);
?>

Returns True.

Could do this too:

<?PHP
$val = 42;
$x = ctype_digit((string) $val);
?>

Which will also return true, as it should.
info at directwebsolutions dot nl 20-May-2012 06:16
All basic PHP functions which i tried returned unexpected results. I would just like to check whether some variable only contains numbers. For example: when i spread my script to the public i cannot require users to only use numbers as string or as integer. For those situation i wrote my own function which handles all inconveniences of other functions and which is not depending on regular expressions. Some people strongly believe that regular functions slow down your script.

The reason to write this function:
1. is_numeric() accepts values like: +0123.45e6 (but you would expect it would not)
2. is_int() does not accept HTML form fields (like: 123) because they are treated as strings (like: "123").
3. ctype_digit() excepts all numbers to be strings (like: "123") and does not validate real integers (like: 123).
4. Probably some functions would parse a boolean (like: true or false) as 0 or 1 and validate it in that manner.

My function only accepts numbers regardless whether they are in string or in integer format.
<?php
    /**
     * Check input for existing only of digits (numbers)
     * @author Tim Boormans <info@directwebsolutions.nl>
     * @param $digit
     * @return bool
     */
    function is_digit($digit) {
        if(is_int($digit)) {
            return true;
        } elseif(is_string($digit)) {
            return ctype_digit($digit);
        } else {
            // booleans, floats and others
            return false;
        }
    }
?>
Skippy 25-Oct-2011 10:44
If you need to check for integers instead of just digits you can supply your own function such as this:

<?php
function ctype_int($text)
{
    return preg_match('/^-?[0-9]+$/', (string)$text) ? true : false;
}
?>
mdsky at web dot de 21-May-2010 09:44
is_numeric gives true by f. ex. 1e3 or 0xf5 too. So it's not the same as ctype_digit, which just gives true when only values from 0 to 9 are entered.
John Saman 05-Feb-2010 02:38
Using is_numeric function is quite faster than ctype_digit.

is_numeric took 0.237 Seconds for one million runs. while ctype_digit took 0.470 Seconds.
strrev xc tod noxeh ta ellij 14-Dec-2009 05:41
ctype_digit() will treat all passed integers below 256 as character-codes. It returns true for 48 through 57 (ASCII '0'-'9') and false for the rest.

ctype_digit(5) -> false
ctype_digit(48) -> true
ctype_digit(255) -> false
ctype_digit(256) -> true

(Note: the PHP type must be an int; if you pass strings it works as expected)
Peter de Pijd 18-Jul-2009 12:53
Note that an empty string is also false:
ctype_digit("") // false
a_p_leeming at hotmail dot com 25-May-2009 03:17
Also note that

<?php ctype_digit("-1");   //false ?>
raul dot 3k at gmail dot com 09-Apr-2009 05:21
The ctype_digit can be used in a simple form to validate a field:
<?php
$field = $_POST["field"];
if(!ctype_digit($field)){
  echo "It's not a digit";
}
?>

Note:
Digits is 0-9


Check for any printable character(s) except space

  • ≪ ctype_digit
  • ctype_lower ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for any printable character(s) except space

ctype_graph

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_graph — Check for any printable character(s) except space

설명

bool ctype_graph ( string $text )

Checks if all of the characters in the provided string, text, creates visible output.

인수

text

The tested string.

반환값

Returns TRUE if every character in text is printable and actually creates visible output (no white space), FALSE otherwise.

예제

Example #1 A ctype_graph() example

<?php
$strings = array('string1' => "asdf\n\r\t", 'string2' => 'arf12', 'string3' => 'LKA#@%.54');
foreach ($strings as $name => $testcase) {
    if (ctype_graph($testcase)) {
        echo "The string '$name' consists of all (visibly) printable characters.\n";
    } else {
        echo "The string '$name' does not consist of all (visibly) printable characters.\n";
    }
}
?>

위 예제의 출력:

The string 'string1' does not consist of all (visibly) printable characters.
The string 'string2' consists of all (visibly) printable characters.
The string 'string3' consists of all (visibly) printable characters.

주의

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_alnum() - Check for alphanumeric character(s)
  • ctype_print() - Check for printable character(s)
  • ctype_punct() - Check for any printable character which is not whitespace or an alphanumeric character

User Contributed Notes

There are no user contributed notes for this page.


Check for lowercase character(s)

  • ≪ ctype_graph
  • ctype_print ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for lowercase character(s)

ctype_lower

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_lower — Check for lowercase character(s)

설명

bool ctype_lower ( string $text )

Checks if all of the characters in the provided string, text, are lowercase letters.

인수

text

The tested string.

반환값

Returns TRUE if every character in text is a lowercase letter in the current locale.

예제

Example #1 A ctype_lower() example (using the default locale)

<?php
$strings = array('aac123', 'qiutoas', 'QASsdks');
foreach ($strings as $testcase) {
    if (ctype_lower($testcase)) {
        echo "The string $testcase consists of all lowercase letters.\n";
    } else {
        echo "The string $testcase does not consist of all lowercase letters.\n";
    }
}
?>

위 예제의 출력:

The string aac123 does not consist of all lowercase letters.
The string qiutoas consists of all lowercase letters.
The string QASsdks does not consist of all lowercase letters.

주의

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_alpha() - Check for alphabetic character(s)
  • ctype_upper() - Check for uppercase character(s)
  • setlocale() - Set locale information

User Contributed Notes

There are no user contributed notes for this page.


Check for printable character(s)

  • ≪ ctype_lower
  • ctype_punct ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for printable character(s)

ctype_print

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_print — Check for printable character(s)

설명

bool ctype_print ( string $text )

Checks if all of the characters in the provided string, text, are printable.

인수

text

The tested string.

반환값

Returns TRUE if every character in text will actually create output (including blanks). Returns FALSE if text contains control characters or characters that do not have any output or control function at all.

예제

Example #1 A ctype_print() example

<?php
$strings = array('string1' => "asdf\n\r\t", 'string2' => 'arf12', 'string3' => 'LKA#@%.54');
foreach ($strings as $name => $testcase) {
    if (ctype_print($testcase)) {
        echo "The string '$name' consists of all printable characters.\n";
    } else {
        echo "The string '$name' does not consist of all printable characters.\n";
    }
}
?>

위 예제의 출력:

The string 'string1' does not consist of all printable characters.
The string 'string2' consists of all printable characters.
The string 'string3' consists of all printable characters.

주의

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_cntrl() - Check for control character(s)
  • ctype_graph() - Check for any printable character(s) except space
  • ctype_punct() - Check for any printable character which is not whitespace or an alphanumeric character

User Contributed Notes

Anonymous 13-Jun-2012 02:59
Only ascii 32 thru 126 (inclusive) are considered printable. Tab (ascii 7), carriage return (ascii 13), linefeed (ascii 10) etc may produce output but are not considered printable.


Check for any printable character which is not whitespace or an alphanumeric character

  • ≪ ctype_print
  • ctype_space ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for any printable character which is not whitespace or an alphanumeric character

ctype_punct

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_punct — Check for any printable character which is not whitespace or an alphanumeric character

설명

bool ctype_punct ( string $text )

Checks if all of the characters in the provided string, text, are punctuation character.

인수

text

The tested string.

반환값

Returns TRUE if every character in text is printable, but neither letter, digit or blank, FALSE otherwise.

예제

Example #1 A ctype_punct() example

<?php
$strings = array('ABasdk!@!$#', '!@ # $', '*&$()');
foreach ($strings as $testcase) {
    if (ctype_punct($testcase)) {
        echo "The string $testcase consists of all punctuation.\n";
    } else {
        echo "The string $testcase does not consist of all punctuation.\n";
    }
}
?>

위 예제의 출력:

The string ABasdk!@!$# does not consist of all punctuation.
The string !@ # $ does not consist of all punctuation.
The string *&$() consists of all punctuation.

주의

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_cntrl() - Check for control character(s)
  • ctype_graph() - Check for any printable character(s) except space

User Contributed Notes

There are no user contributed notes for this page.


Check for whitespace character(s)

  • ≪ ctype_punct
  • ctype_upper ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for whitespace character(s)

ctype_space

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_space — Check for whitespace character(s)

설명

bool ctype_space ( string $text )

Checks if all of the characters in the provided string, text, creates whitespace.

인수

text

The tested string.

반환값

Returns TRUE if every character in text creates some sort of white space, FALSE otherwise. Besides the blank character this also includes tab, vertical tab, line feed, carriage return and form feed characters.

예제

Example #1 A ctype_space() example

<?php
$strings = array('string1' => "\n\r\t", 'string2' => "\narf12", 'string3' => '\n\r\t');
foreach ($strings as $name => $testcase) {
    if (ctype_space($testcase)) {
        echo "The string '$name' consists of all whitespace characters.\n";
    } else {
        echo "The string '$name' does not consist of all whitespace characters.\n";
    }
}
?>

위 예제의 출력:

The string 'string1' consists of all whitespace characters.
The string 'string2' does not consist of all whitespace characters.
The string 'string3' does not consist of all whitespace characters.

주의

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_cntrl() - Check for control character(s)
  • ctype_graph() - Check for any printable character(s) except space
  • ctype_punct() - Check for any printable character which is not whitespace or an alphanumeric character

User Contributed Notes

vlabots at gmail dot com 18-Jun-2014 04:03
thanks to gardnerjohng, but a had some problems with non-breaking spaces in this function.

I added 2 more cases for this:
case 0xC2
case 0xA0

After this modification non-breaking spaces in my test code were successfully detected.
gardnerjohng at hotmail dot com 15-Sep-2009 06:00
A function I wrote last night was fairly flexible in terms of detecting whitespace, and even took into account the pesky non-breaking spaces / zero-width spaces further up the Unicode alphabet.

The benefit here was being able to isolate and identify specific Unicode indices based on their subrange.

<?php
    //    Returns TRUE if the ASCII value of $string matches a registered whitespace character.
    //    *    This includes non-breaking spaces, zero-width spaces, and any unicode values below 32.
    //    *    $string: Character to identify. If string extends past one character, the value
    //        is truncated and only the initial character is examined.
    function is_whitespace($string){
        //    Return FALSE if passed an empty string.
        if($string == "") return FALSE;

        $char    =    ord($string);

        //    Control Characters
        if($char < 33)                        return TRUE;
        if($char > 8191 && $char < 8208)    return TRUE;
        if($char > 8231 && $char < 8240)    return TRUE;

        //    Additional Characters
        switch($char){
            case 160:    // Non-Breaking Space
            case 8287:    // Medium Mathematical Space
                return TRUE;
                break;
        }
        return FALSE;
    }
?>


Check for uppercase character(s)

  • ≪ ctype_space
  • ctype_xdigit ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for uppercase character(s)

ctype_upper

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_upper — Check for uppercase character(s)

설명

bool ctype_upper ( string $text )

Checks if all of the characters in the provided string, text, are uppercase characters.

인수

text

The tested string.

반환값

Returns TRUE if every character in text is an uppercase letter in the current locale.

예제

Example #1 A ctype_upper() example (using the default locale)

<?php
$strings = array('AKLWC139', 'LMNSDO', 'akwSKWsm');
foreach ($strings as $testcase) {
    if (ctype_upper($testcase)) {
        echo "The string $testcase consists of all uppercase letters.\n";
    } else {
        echo "The string $testcase does not consist of all uppercase letters.\n";
    }
}
?>

위 예제의 출력:

The string AKLWC139 does not consist of all uppercase letters.
The string LMNSDO consists of all uppercase letters.
The string akwSKWsm does not consist of all uppercase letters.

주의

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_alpha() - Check for alphabetic character(s)
  • ctype_lower() - Check for lowercase character(s)
  • setlocale() - Set locale information

User Contributed Notes

Anonymous 07-Jan-2016 04:25
// after screwing around with function.ctype-upper
// I found this simple solution

if (strtolower($string) != $string)// its so simple!
{
  echo "uppercase letters found in $string";
}

source: http://stackoverflow.com/questions/4426327/how-to-detect-if-string-contains-1-uppercase-letter-in-php
chris at theothernews dot co dot nz 11-Jun-2014 11:32
Underscores in the string will result in false, so you have to remove them first.


Check for character(s) representing a hexadecimal digit

  • ≪ ctype_upper
  • Data Structures ≫
  • PHP Manual
  • Ctype 함수 목록
  • Check for character(s) representing a hexadecimal digit

ctype_xdigit

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

ctype_xdigit — Check for character(s) representing a hexadecimal digit

설명

bool ctype_xdigit ( string $text )

Checks if all of the characters in the provided string, text, are hexadecimal 'digits'.

인수

text

The tested string.

반환값

Returns TRUE if every character in text is a hexadecimal 'digit', that is a decimal digit or a character from [A-Fa-f] , FALSE otherwise.

예제

Example #1 A ctype_xdigit() example

<?php
$strings = array('AB10BC99', 'AR1012', 'ab12bc99');
foreach ($strings as $testcase) {
    if (ctype_xdigit($testcase)) {
        echo "The string $testcase consists of all hexadecimal digits.\n";
    } else {
        echo "The string $testcase does not consist of all hexadecimal digits.\n";
    }
}
?>

위 예제의 출력:

The string AB10BC99 consists of all hexadecimal digits.
The string AR1012 does not consist of all hexadecimal digits.
The string ab12bc99 consists of all hexadecimal digits.

주의

Note:

-128이상 255이하의 integer를 지정할 경우, 한 문자의 ASCII 값으로 해석합니다. (음수는 256이 더해져 확장 ASCII 영역의 문자가 됩니다) 그 외의 정수는 그 정수를 표현하는 10진 문자열로 해석합니다.

참고

  • ctype_digit() - Check for numeric character(s)

User Contributed Notes

tom at hgmail dot com 14-Jan-2006 09:36
This function shows its usefulness on a web site where a user is asked to entered a hexidecimal color code for a color. To prevent breaking W3C standard and having them enter in "neon-green" or the wrong type of code like 355511235.

In conjunction with strlen()  you could create a function like this:

function check_valid_colorhex($colorCode) {
    // If user accidentally passed along the # sign, strip it off
    $colorCode = ltrim($colorCode, '#');

    if (
          ctype_xdigit($colorCode) &&
          (strlen($colorCode) == 6 || strlen($colorCode) == 3))
               return true;

    else return false;
}


Data Structures

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
  • 예약 상수
  • 예제
  • Collection — The Collection interface
    • Ds\Collection::clear — Removes all values.
    • Ds\Collection::copy — Returns a shallow copy of the collection.
    • Ds\Collection::isEmpty — Returns whether the collection is empty
    • Ds\Collection::toArray — Converts the collection to an array.
  • Hashable — The Hashable interface
    • Ds\Hashable::equals — Determines whether an object is equal to the current instance.
    • Ds\Hashable::hash — Returns a scalar value to be used as a hash value.
  • Sequence — The Sequence interface
    • Ds\Sequence::allocate — Allocates enough memory for a required capacity.
    • Ds\Sequence::apply — Updates all values by applying a callback function to each value.
    • Ds\Sequence::capacity — Returns the current capacity.
    • Ds\Sequence::contains — Determines if the sequence contains given values.
    • Ds\Sequence::filter — Creates a new sequence using a callable to determine which values to include.
    • Ds\Sequence::find — Attempts to find a value's index.
    • Ds\Sequence::first — Returns the first value in the sequence.
    • Ds\Sequence::get — Returns the value at a given index.
    • Ds\Sequence::insert — Inserts values at a given index.
    • Ds\Sequence::join — Joins all values together as a string.
    • Ds\Sequence::last — Returns the last value.
    • Ds\Sequence::map — Returns the result of applying a callback to each value.
    • Ds\Sequence::merge — Returns the result of adding all given values to the sequence.
    • Ds\Sequence::pop — Removes and returns the last value.
    • Ds\Sequence::push — Adds values to the end of the sequence.
    • Ds\Sequence::reduce — Reduces the sequence to a single value using a callback function.
    • Ds\Sequence::remove — Removes and returns a value by index.
    • Ds\Sequence::reverse — Reverses the sequence in-place.
    • Ds\Sequence::reversed — Returns a reversed copy.
    • Ds\Sequence::rotate — Rotates the sequence by a given number of rotations.
    • Ds\Sequence::set — Updates a value at a given index.
    • Ds\Sequence::shift — Removes and returns the first value.
    • Ds\Sequence::slice — Returns a sub-sequence of a given range.
    • Ds\Sequence::sort — Sorts the sequence in-place.
    • Ds\Sequence::sorted — Returns a sorted copy.
    • Ds\Sequence::sum — Returns the sum of all values in the sequence.
    • Ds\Sequence::unshift — Adds values to the front of the sequence.
  • Vector — The Vector class
    • Ds\Vector::allocate — Allocates enough memory for a required capacity.
    • Ds\Vector::apply — Updates all values by applying a callback function to each value.
    • Ds\Vector::capacity — Returns the current capacity.
    • Ds\Vector::clear — Removes all values.
    • Ds\Vector::__construct — Creates a new instance.
    • Ds\Vector::contains — Determines if the vector contains given values.
    • Ds\Vector::copy — Returns a shallow copy of the vector.
    • Ds\Vector::count — Returns the number of values in the collection.
    • Ds\Vector::filter — Creates a new vector using a callable to determine which values to include.
    • Ds\Vector::find — Attempts to find a value's index.
    • Ds\Vector::first — Returns the first value in the vector.
    • Ds\Vector::get — Returns the value at a given index.
    • Ds\Vector::insert — Inserts values at a given index.
    • Ds\Vector::isEmpty — Returns whether the vector is empty
    • Ds\Vector::join — Joins all values together as a string.
    • Ds\Vector::jsonSerialize — Returns a representation that can be converted to JSON.
    • Ds\Vector::last — Returns the last value.
    • Ds\Vector::map — Returns the result of applying a callback to each value.
    • Ds\Vector::merge — Returns the result of adding all given values to the vector.
    • Ds\Vector::pop — Removes and returns the last value.
    • Ds\Vector::push — Adds values to the end of the vector.
    • Ds\Vector::reduce — Reduces the vector to a single value using a callback function.
    • Ds\Vector::remove — Removes and returns a value by index.
    • Ds\Vector::reverse — Reverses the vector in-place.
    • Ds\Vector::reversed — Returns a reversed copy.
    • Ds\Vector::rotate — Rotates the vector by a given number of rotations.
    • Ds\Vector::set — Updates a value at a given index.
    • Ds\Vector::shift — Removes and returns the first value.
    • Ds\Vector::slice — Returns a sub-vector of a given range.
    • Ds\Vector::sort — Sorts the vector in-place.
    • Ds\Vector::sorted — Returns a sorted copy.
    • Ds\Vector::sum — Returns the sum of all values in the vector.
    • Ds\Vector::toArray — Converts the vector to an array.
    • Ds\Vector::unshift — Adds values to the front of the vector.
  • Deque — The Deque class
    • Ds\Deque::allocate — Allocates enough memory for a required capacity.
    • Ds\Deque::apply — Updates all values by applying a callback function to each value.
    • Ds\Deque::capacity — Returns the current capacity.
    • Ds\Deque::clear — Removes all values from the deque.
    • Ds\Deque::__construct — Creates a new instance.
    • Ds\Deque::contains — Determines if the deque contains given values.
    • Ds\Deque::copy — Returns a shallow copy of the deque.
    • Ds\Deque::count — Returns the number of values in the collection.
    • Ds\Deque::filter — Creates a new deque using a callable to determine which values to include.
    • Ds\Deque::find — Attempts to find a value's index.
    • Ds\Deque::first — Returns the first value in the deque.
    • Ds\Deque::get — Returns the value at a given index.
    • Ds\Deque::insert — Inserts values at a given index.
    • Ds\Deque::isEmpty — Returns whether the deque is empty
    • Ds\Deque::join — Joins all values together as a string.
    • Ds\Deque::jsonSerialize — Returns a representation that can be converted to JSON.
    • Ds\Deque::last — Returns the last value.
    • Ds\Deque::map — Returns the result of applying a callback to each value.
    • Ds\Deque::merge — Returns the result of adding all given values to the deque.
    • Ds\Deque::pop — Removes and returns the last value.
    • Ds\Deque::push — Adds values to the end of the deque.
    • Ds\Deque::reduce — Reduces the deque to a single value using a callback function.
    • Ds\Deque::remove — Removes and returns a value by index.
    • Ds\Deque::reverse — Reverses the deque in-place.
    • Ds\Deque::reversed — Returns a reversed copy.
    • Ds\Deque::rotate — Rotates the deque by a given number of rotations.
    • Ds\Deque::set — Updates a value at a given index.
    • Ds\Deque::shift — Removes and returns the first value.
    • Ds\Deque::slice — Returns a sub-deque of a given range.
    • Ds\Deque::sort — Sorts the deque in-place.
    • Ds\Deque::sorted — Returns a sorted copy.
    • Ds\Deque::sum — Returns the sum of all values in the deque.
    • Ds\Deque::toArray — Converts the deque to an array.
    • Ds\Deque::unshift — Adds values to the front of the deque.
  • Map — The Map class
    • Ds\Map::allocate — Allocates enough memory for a required capacity.
    • Ds\Map::apply — Updates all values by applying a callback function to each value.
    • Ds\Map::capacity — Returns the current capacity.
    • Ds\Map::clear — Removes all values.
    • Ds\Map::__construct — Creates a new instance.
    • Ds\Map::copy — Returns a shallow copy of the map.
    • Ds\Map::count — Returns the number of values in the map.
    • Ds\Map::diff — Creates a new map using keys that aren't in another map.
    • Ds\Map::filter — Creates a new map using a callable to determine which pairs to include.
    • Ds\Map::first — Returns the first pair in the map.
    • Ds\Map::get — Returns the value for a given key.
    • Ds\Map::hasKey — Determines whether the map contains a given key.
    • Ds\Map::hasValue — Determines whether the map contains a given value.
    • Ds\Map::intersect — Creates a new map by intersecting keys with another map.
    • Ds\Map::isEmpty — Returns whether the map is empty
    • Ds\Map::jsonSerialize — Returns a representation that can be converted to JSON.
    • Ds\Map::keys — Returns a set of the map's keys.
    • Ds\Map::ksort — Sorts the map in-place by key.
    • Ds\Map::ksorted — Returns a copy, sorted by key.
    • Ds\Map::last — Returns the last pair of the map.
    • Ds\Map::map — Returns the result of applying a callback to each value.
    • Ds\Map::merge — Returns the result of adding all given associations.
    • Ds\Map::pairs — Returns a sequence containing all the pairs of the map.
    • Ds\Map::put — Associates a key with a value.
    • Ds\Map::putAll — Associates all key-value pairs of a traversable object or array.
    • Ds\Map::reduce — Reduces the map to a single value using a callback function.
    • Ds\Map::remove — Removes and returns a value by key.
    • Ds\Map::reverse — Reverses the map in-place.
    • Ds\Map::reversed — Returns a reversed copy.
    • Ds\Map::skip — Returns the pair at a given positional index.
    • Ds\Map::slice — Returns a subset of the map defined by a starting index and length.
    • Ds\Map::sort — Sorts the map in-place by value.
    • Ds\Map::sorted — Returns a copy, sorted by value.
    • Ds\Map::sum — Returns the sum of all values in the map.
    • Ds\Map::toArray — Converts the map to an array.
    • Ds\Map::union — Creates a new map using values from the current instance and another map.
    • Ds\Map::values — Returns a sequence of the map's values.
    • Ds\Map::xor — Creates a new map using keys of either the current instance or of another map, but not of both.
  • Pair — The Pair class
    • Ds\Pair::clear — Removes all values.
    • Ds\Pair::__construct — Creates a new instance.
    • Ds\Pair::copy — Returns a shallow copy of the pair.
    • Ds\Pair::isEmpty — Returns whether the pair is empty
    • Ds\Pair::jsonSerialize — Returns a representation that can be converted to JSON.
    • Ds\Pair::toArray — Converts the pair to an array.
  • Set — The Set class
    • Ds\Set::add — Adds values to the set.
    • Ds\Set::allocate — Allocates enough memory for a required capacity.
    • Ds\Set::capacity — Returns the current capacity.
    • Ds\Set::clear — Removes all values.
    • Ds\Set::__construct — Creates a new instance.
    • Ds\Set::contains — Determines if the set contains all values.
    • Ds\Set::copy — Returns a shallow copy of the set.
    • Ds\Set::count — Returns the number of values in the set.
    • Ds\Set::diff — Creates a new set using values that aren't in another set.
    • Ds\Set::filter — Creates a new set using a callable to determine which values to include.
    • Ds\Set::first — Returns the first value in the set.
    • Ds\Set::get — Returns the value at a given index.
    • Ds\Set::intersect — Creates a new set by intersecting values with another set.
    • Ds\Set::isEmpty — Returns whether the set is empty
    • Ds\Set::join — Joins all values together as a string.
    • Ds\Set::jsonSerialize — Returns a representation that can be converted to JSON.
    • Ds\Set::last — Returns the last value in the set.
    • Ds\Set::merge — Returns the result of adding all given values to the set.
    • Ds\Set::reduce — Reduces the set to a single value using a callback function.
    • Ds\Set::remove — Removes all given values from the set.
    • Ds\Set::reverse — Reverses the set in-place.
    • Ds\Set::reversed — Returns a reversed copy.
    • Ds\Set::slice — Returns a sub-set of a given range.
    • Ds\Set::sort — Sorts the set in-place.
    • Ds\Set::sorted — Returns a sorted copy.
    • Ds\Set::sum — Returns the sum of all values in the set.
    • Ds\Set::toArray — Converts the set to an array.
    • Ds\Set::union — Creates a new set using values from the current instance and another set.
    • Ds\Set::xor — Creates a new set using values in either the current instance or in another set, but not in both.
  • Stack — The Stack class
    • Ds\Stack::allocate — Allocates enough memory for a required capacity.
    • Ds\Stack::capacity — Returns the current capacity.
    • Ds\Stack::clear — Removes all values.
    • Ds\Stack::__construct — Creates a new instance.
    • Ds\Stack::copy — Returns a shallow copy of the stack.
    • Ds\Stack::count — Returns the number of values in the stack.
    • Ds\Stack::isEmpty — Returns whether the stack is empty
    • Ds\Stack::jsonSerialize — Returns a representation that can be converted to JSON.
    • Ds\Stack::peek — Returns the value at the top of the stack.
    • Ds\Stack::pop — Removes and returns the value at the top of the stack.
    • Ds\Stack::push — Pushes values onto the stack.
    • Ds\Stack::toArray — Converts the stack to an array.
  • Queue — The Queue class
    • Ds\Queue::allocate — Allocates enough memory for a required capacity.
    • Ds\Queue::capacity — Returns the current capacity.
    • Ds\Queue::clear — Removes all values.
    • Ds\Queue::__construct — Creates a new instance.
    • Ds\Queue::copy — Returns a shallow copy of the queue.
    • Ds\Queue::count — Returns the number of values in the queue.
    • Ds\Queue::isEmpty — Returns whether the queue is empty
    • Ds\Queue::jsonSerialize — Returns a representation that can be converted to JSON.
    • Ds\Queue::peek — Returns the value at the front of the queue.
    • Ds\Queue::pop — Removes and returns the value at the front of the queue.
    • Ds\Queue::push — Pushes values into the queue.
    • Ds\Queue::toArray — Converts the queue to an array.
  • PriorityQueue — The PriorityQueue class
    • Ds\PriorityQueue::allocate — Allocates enough memory for a required capacity.
    • Ds\PriorityQueue::capacity — Returns the current capacity.
    • Ds\PriorityQueue::clear — Removes all values.
    • Ds\PriorityQueue::__construct — Creates a new instance.
    • Ds\PriorityQueue::copy — Returns a shallow copy of the queue.
    • Ds\PriorityQueue::count — Returns the number of values in the queue.
    • Ds\PriorityQueue::isEmpty — Returns whether the queue is empty
    • Ds\PriorityQueue::jsonSerialize — Returns a representation that can be converted to JSON.
    • Ds\PriorityQueue::peek — Returns the value at the front of the queue.
    • Ds\PriorityQueue::pop — Removes and returns the value with the highest priority.
    • Ds\PriorityQueue::push — Pushes values into the queue.
    • Ds\PriorityQueue::toArray — Converts the queue to an array.

User Contributed Notes

There are no user contributed notes for this page.


소개

Efficient data structures for PHP 7, provided as an alternative to the array.

See » this blog post for benchmarks, discussion and frequently asked questions.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

PHP 7 is required by both the extension and the compatibility polyfill.

User Contributed Notes

There are no user contributed notes for this page.


설치

The easiest way to install the extension is via » PECL

pecl install ds

You can also build directly from source:

# Dependencies you might need to install
# sudo apt-get install git build-essential php7.0-dev

git clone https://github.com/php-ds/extension "php-ds"
cd php-ds

# Build and install the extension
phpize
./configure
make
make install

# Clean up the build files
make clean
phpize --clean

If you're on Windows, you can » download a compiled .dll on PECL

Note:

If you're using Composer, it's highly recommended that you include » php-ds/php-ds in your project so that your code is still functional in an environment where the extension is not installed. The extension will take priority if installed.

User Contributed Notes

There are no user contributed notes for this page.


예제

Example #1 Vector

<?php

$vector = new \Ds\Vector();

$vector->push('a');
$vector->push('b', 'c');

$vector[] = 'd';

print_r($vector);

?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

Example #2 Map

<?php

$map = new \Ds\Map();

$map->put('a', 1);
$map->put('b', 2);

$map['c'] = 3;

print_r($map);

?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

)

User Contributed Notes

There are no user contributed notes for this page.


The Collection interface

(No version information available, might only be in Git)

소개

Collection is the base interface which covers functionality common to all the data structures in this library. It guarantees that all structures are traversable, countable, and can be converted to json using json_encode().

인터페이스 개요

Ds\Collection implements Traversable , Countable , JsonSerializable {
/* 메소드 */
abstract public void clear ( void )
abstract public Ds\Collection copy ( void )
abstract public bool isEmpty ( void )
abstract public array toArray ( void )
}

Table of Contents

  • Ds\Collection::clear — Removes all values.
  • Ds\Collection::copy — Returns a shallow copy of the collection.
  • Ds\Collection::isEmpty — Returns whether the collection is empty
  • Ds\Collection::toArray — Converts the collection to an array.

User Contributed Notes

There are no user contributed notes for this page.


Removes all values.

  • ≪ Collection
  • Ds\Collection::copy ≫
  • PHP Manual
  • Collection
  • Removes all values.

Ds\Collection::clear

(PECL ds >= 1.0.0)

Ds\Collection::clear — Removes all values.

설명

abstract public void Ds\Collection::clear ( void )

Removes all values from the collection.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Collection::clear() example

<?php
$collection = new \Ds\Vector([1, 2, 3]);
print_r($collection);

$collection->clear();
print_r($collection);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Vector Object
(
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a shallow copy of the collection.

  • ≪ Ds\Collection::clear
  • Ds\Collection::isEmpty ≫
  • PHP Manual
  • Collection
  • Returns a shallow copy of the collection.

Ds\Collection::copy

(PECL ds >= 1.0.0)

Ds\Collection::copy — Returns a shallow copy of the collection.

설명

abstract public Ds\Collection Ds\Collection::copy ( void )

Returns a shallow copy of the collection.

인수

이 함수는 인수가 없습니다.

반환값

Returns a shallow copy of the collection.

예제

Example #1 Ds\Collection::copy() example

<?php
$a = new \Ds\Vector([1, 2, 3]);
$b = $a->copy();

$b->push(4);

print_r($a);
print_r($b);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

User Contributed Notes

There are no user contributed notes for this page.


Returns whether the collection is empty

  • ≪ Ds\Collection::copy
  • Ds\Collection::toArray ≫
  • PHP Manual
  • Collection
  • Returns whether the collection is empty

Ds\Collection::isEmpty

(PECL ds >= 1.0.0)

Ds\Collection::isEmpty — Returns whether the collection is empty

설명

abstract public bool Ds\Collection::isEmpty ( void )

Returns whether the collection is empty.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the collection is empty, FALSE otherwise.

예제

Example #1 Ds\Collection::isEmpty() example

<?php
$a = new \Ds\Vector([1, 2, 3]);
$b = new \Ds\Vector();

var_dump($a->isEmpty());
var_dump($b->isEmpty());
?>

위 예제의 출력 예시:

bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Converts the collection to an array.

  • ≪ Ds\Collection::isEmpty
  • Hashable ≫
  • PHP Manual
  • Collection
  • Converts the collection to an array.

Ds\Collection::toArray

(PECL ds >= 1.0.0)

Ds\Collection::toArray — Converts the collection to an array.

설명

abstract public array Ds\Collection::toArray ( void )

Converts the collection to an array.

Note:

Casting to an array is not supported yet.

인수

이 함수는 인수가 없습니다.

반환값

An array containing all the values in the same order as the collection.

예제

Example #1 Ds\Collection::toArray() example

<?php
$collection = new \Ds\Vector([1, 2, 3]);

var_dump($collection->toArray());
?>

위 예제의 출력 예시:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


The Hashable interface

(No version information available, might only be in Git)

소개

Hashable is an interface which allows objects to be used as keys. It's an alternative to spl_object_hash(), which determines an object's hash based on its handle: this means that two objects that are considered equal by an implicit definition would not treated as equal because they are not the same instance.

hash() is used to return a scalar value to be used as the object's hash value, which determines where it goes in the hash table. While this value does not have to be unique, objects which are equal must have the same hash value.

equals() is used to determine if two objects are equal. It's guaranteed that the comparing object will be an instance of the same class as the subject.

인터페이스 개요

Ds\Hashable {
/* 메소드 */
abstract public bool equals ( object $obj )
abstract public mixed hash ( void )
}

Table of Contents

  • Ds\Hashable::equals — Determines whether an object is equal to the current instance.
  • Ds\Hashable::hash — Returns a scalar value to be used as a hash value.

User Contributed Notes

There are no user contributed notes for this page.


Determines whether an object is equal to the current instance.

  • ≪ Hashable
  • Ds\Hashable::hash ≫
  • PHP Manual
  • Hashable
  • Determines whether an object is equal to the current instance.

Ds\Hashable::equals

(PECL ds >= 1.0.0)

Ds\Hashable::equals — Determines whether an object is equal to the current instance.

설명

abstract public bool Ds\Hashable::equals ( object $obj )

Determines whether another object is equal to the current instance.

This method allows objects to be used as keys in structures such as Ds\Map and Ds\Set, or any other lookup structure that honors this interface.

Note:

It's guaranteed that obj is an instance of the same class.

Caution

It's important that objects which are equal also have the same hash value. See Ds\Hashable::hash().

인수

obj

The object to compare the current instance to, which is always an instance of the same class.

반환값

TRUE if equal, FALSE otherwise.

User Contributed Notes

There are no user contributed notes for this page.


Returns a scalar value to be used as a hash value.

  • ≪ Ds\Hashable::equals
  • Sequence ≫
  • PHP Manual
  • Hashable
  • Returns a scalar value to be used as a hash value.

Ds\Hashable::hash

(PECL ds >= 1.0.0)

Ds\Hashable::hash — Returns a scalar value to be used as a hash value.

설명

abstract public mixed Ds\Hashable::hash ( void )

Returns a scalar value to be used as the hash value of the objects.

While the hash value does not define equality, all objects that are equal according to Ds\Hashable::equals() must have the same hash value. Hash values of equal objects don't have to be unique, for example you could just return TRUE for all objects and nothing would break - the only implication would be that hash tables then turn into linked lists because all your objects will be hashed to the same bucket. It's therefore very important that you pick a good hash value, such as an ID or email address.

This method allows objects to be used as keys in structures such as Ds\Map and Ds\Set, or any other lookup structure that honors this interface.

Caution

Do not pick a value that might change within the object, such as a public property. Hash table lookups would fail because the hash has changed.

Caution

All objects that are equal must have the same hash value.

인수

이 함수는 인수가 없습니다.

반환값

A scalar value to be used as this object's hash value.

예제

Example #1 Ds\Hashable::hash() example

<?php
class HashableObject implements \Ds\Hashable
{
    private $name;
    private $email;

    public function __construct($name, $email)
    {
        $this->name  = $name;
        $this->email = $email;
    }

    /**
     * Should return the same value for all equal objects, but doesn't have to
     * be unique. This value will not be used to determine equality.
     */
    public function hash()
    {
        return $this->email;
    }

    /**
     * This determines equality, usually during a hash table lookup to determine
     * if the bucket's key matches the lookup key. The hash has to be equal if
     * the objects are equal, otherwise this determination wouldn't be reached.
     */
    public function equals($obj): bool
    {
        return $this->name  === $obj->name
            && $this->email === $obj->email;
    }
}
?>

User Contributed Notes

There are no user contributed notes for this page.


The Sequence interface

(No version information available, might only be in Git)

소개

A Sequence describes the behaviour of values arranged in a single, linear dimension. Some languages refer to this as a "List". It's similar to an array that uses incremental integer keys, with the exception of a few characteristics:

  • Values will always be indexed as [0, 1, 2, ..., size - 1].
  • Only allowed to access values by index in the range [0, size - 1].

Use cases:

  • Wherever you would use an array as a list (not concerned with keys).
  • A more efficient alternative to SplDoublyLinkedList and SplFixedArray.

인터페이스 개요

Ds\Sequence implements Ds\Collection {
/* 메소드 */
abstract public void allocate ( int $capacity )
abstract public void apply ( callable $callback )
abstract public int capacity ( void )
abstract public bool contains ([ mixed $...values ] )
abstract public Ds\Sequence filter ([ callable $callback ] )
abstract public mixed find ( mixed $value )
abstract public mixed first ( void )
abstract public mixed get ( int $index )
abstract public void insert ( int $index [, mixed $...values ] )
abstract public string join ([ string $glue ] )
abstract public mixed last ( void )
abstract public Ds\Sequence map ( callable $callback )
abstract public Ds\Sequence merge ( mixed $values )
abstract public mixed pop ( void )
abstract public void push ([ mixed $...values ] )
abstract public mixed reduce ( callable $callback [, mixed $initial ] )
abstract public mixed remove ( int $index )
abstract public void reverse ( void )
abstract public Ds\Sequence reversed ( void )
abstract public void rotate ( int $rotations )
abstract public void set ( int $index , mixed $value )
abstract public mixed shift ( void )
abstract public Ds\Sequence slice ( int $index [, int $length ] )
abstract public void sort ([ callable $comparator ] )
abstract public Ds\Sequence sorted ([ callable $comparator ] )
abstract public number sum ( void )
abstract public void unshift ([ mixed $values ] )
}

Table of Contents

  • Ds\Sequence::allocate — Allocates enough memory for a required capacity.
  • Ds\Sequence::apply — Updates all values by applying a callback function to each value.
  • Ds\Sequence::capacity — Returns the current capacity.
  • Ds\Sequence::contains — Determines if the sequence contains given values.
  • Ds\Sequence::filter — Creates a new sequence using a callable to determine which values to include.
  • Ds\Sequence::find — Attempts to find a value's index.
  • Ds\Sequence::first — Returns the first value in the sequence.
  • Ds\Sequence::get — Returns the value at a given index.
  • Ds\Sequence::insert — Inserts values at a given index.
  • Ds\Sequence::join — Joins all values together as a string.
  • Ds\Sequence::last — Returns the last value.
  • Ds\Sequence::map — Returns the result of applying a callback to each value.
  • Ds\Sequence::merge — Returns the result of adding all given values to the sequence.
  • Ds\Sequence::pop — Removes and returns the last value.
  • Ds\Sequence::push — Adds values to the end of the sequence.
  • Ds\Sequence::reduce — Reduces the sequence to a single value using a callback function.
  • Ds\Sequence::remove — Removes and returns a value by index.
  • Ds\Sequence::reverse — Reverses the sequence in-place.
  • Ds\Sequence::reversed — Returns a reversed copy.
  • Ds\Sequence::rotate — Rotates the sequence by a given number of rotations.
  • Ds\Sequence::set — Updates a value at a given index.
  • Ds\Sequence::shift — Removes and returns the first value.
  • Ds\Sequence::slice — Returns a sub-sequence of a given range.
  • Ds\Sequence::sort — Sorts the sequence in-place.
  • Ds\Sequence::sorted — Returns a sorted copy.
  • Ds\Sequence::sum — Returns the sum of all values in the sequence.
  • Ds\Sequence::unshift — Adds values to the front of the sequence.

User Contributed Notes

There are no user contributed notes for this page.


Allocates enough memory for a required capacity.

  • ≪ Sequence
  • Ds\Sequence::apply ≫
  • PHP Manual
  • Sequence
  • Allocates enough memory for a required capacity.

Ds\Sequence::allocate

(PECL ds >= 1.0.0)

Ds\Sequence::allocate — Allocates enough memory for a required capacity.

설명

abstract public void Ds\Sequence::allocate ( int $capacity )

Ensures that enough memory is allocated for a required capacity. This removes the need to reallocate the internal as values are added.

인수

capacity

The number of values for which capacity should be allocated.

Note:

Capacity will stay the same if this value is less than or equal to the current capacity.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Sequence::allocate() example

<?php
$sequence = new \Ds\Vector();
var_dump($sequence->capacity());

$vector->allocate(100);
var_dump($sequence->capacity());
?>

위 예제의 출력 예시:

int(10)
int(100)

User Contributed Notes

There are no user contributed notes for this page.


Updates all values by applying a callback function to each value.

  • ≪ Ds\Sequence::allocate
  • Ds\Sequence::capacity ≫
  • PHP Manual
  • Sequence
  • Updates all values by applying a callback function to each value.

Ds\Sequence::apply

(PECL ds >= 1.0.0)

Ds\Sequence::apply — Updates all values by applying a callback function to each value.

설명

abstract public void Ds\Sequence::apply ( callable $callback )

Updates all values by applying a callback function to each value in the sequence.

인수

callback

mixed callback ( mixed $value )

A callable to apply to each value in the sequence.

The callback should return what the value should be replaced by.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Sequence::apply() example

<?php
$sequence = new \Ds\Vector([1, 2, 3]);
$sequence->apply(function($value) { return $value * 2; });

print_r($sequence);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 2
    [1] => 4
    [2] => 6
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the current capacity.

  • ≪ Ds\Sequence::apply
  • Ds\Sequence::contains ≫
  • PHP Manual
  • Sequence
  • Returns the current capacity.

Ds\Sequence::capacity

(PECL ds >= 1.0.0)

Ds\Sequence::capacity — Returns the current capacity.

설명

abstract public int Ds\Sequence::capacity ( void )

Returns the current capacity.

인수

이 함수는 인수가 없습니다.

반환값

The current capacity.

예제

Example #1 Ds\Sequence::capacity() example

<?php
$sequence = new \Ds\Vector();
var_dump($sequence->capacity());

$sequence->push(...range(1, 50));
var_dump($sequence->capacity());

$sequence[] = "a";
var_dump($sequence->capacity());
?>

위 예제의 출력 예시:

int(10)
int(50)
int(75)

User Contributed Notes

There are no user contributed notes for this page.


Determines if the sequence contains given values.

  • ≪ Ds\Sequence::capacity
  • Ds\Sequence::filter ≫
  • PHP Manual
  • Sequence
  • Determines if the sequence contains given values.

Ds\Sequence::contains

(PECL ds >= 1.0.0)

Ds\Sequence::contains — Determines if the sequence contains given values.

설명

abstract public bool Ds\Sequence::contains ([ mixed $...values ] )

Determines if the sequence contains all values.

인수

values

Values to check.

반환값

FALSE if any of the provided values are not in the sequence, TRUE otherwise.

예제

Example #1 Ds\Sequence::contains() example

<?php
$sequence = new \Ds\Vector(['a', 'b', 'c', 1, 2, 3]);

var_dump($sequence->contains('a'));                // true
var_dump($sequence->contains('a', 'b'));           // true
var_dump($sequence->contains('c', 'd'));           // false

var_dump($sequence->contains(...['c', 'b', 'a'])); // true

// Always strict
var_dump($sequence->contains(1));                  // true
var_dump($sequence->contains('1'));                // false

var_dump($sequece->contains(...[]));               // true
?>

위 예제의 출력 예시:

bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new sequence using a callable to determine which values to include.

  • ≪ Ds\Sequence::contains
  • Ds\Sequence::find ≫
  • PHP Manual
  • Sequence
  • Creates a new sequence using a callable to determine which values to include.

Ds\Sequence::filter

(PECL ds >= 1.0.0)

Ds\Sequence::filter — Creates a new sequence using a callable to determine which values to include.

설명

abstract public Ds\Sequence Ds\Sequence::filter ([ callable $callback ] )

Creates a new sequence using a callable to determine which values to include.

인수

callback

bool callback ( mixed $value )

Optional callable which returns TRUE if the value should be included, FALSE otherwise.

If a callback is not provided, only values which are TRUE (see converting to boolean) will be included.

반환값

A new sequence containing all the values for which either the callback returned TRUE, or all values that convert to TRUE if a callback was not provided.

예제

Example #1 Ds\Sequence::filter() example using callback function

<?php
$sequence = new \Ds\Vector([1, 2, 3, 4, 5]);

var_dump($sequence->filter(function($value) {
    return $value % 2 == 0;
}));
?>

위 예제의 출력 예시:

object(Ds\Vector)#3 (2) {
  [0]=>
  int(2)
  [1]=>
  int(4)
}

Example #2 Ds\Sequence::filter() example without a callback function

<?php
$sequence = new \Ds\Vector([0, 1, 'a', true, false]);

var_dump($sequence->filter());
?>

위 예제의 출력 예시:

object(Ds\Vector)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  string(1) "a"
  [2]=>
  bool(true)
}

User Contributed Notes

There are no user contributed notes for this page.


Attempts to find a value's index.

  • ≪ Ds\Sequence::filter
  • Ds\Sequence::first ≫
  • PHP Manual
  • Sequence
  • Attempts to find a value's index.

Ds\Sequence::find

(PECL ds >= 1.0.0)

Ds\Sequence::find — Attempts to find a value's index.

설명

abstract public mixed Ds\Sequence::find ( mixed $value )

Returns the index of the value, or FALSE if not found.

인수

value

The value to find.

반환값

The index of the value, or FALSE if not found.

Note:

Values will be compared by value and by type.

예제

Example #1 Ds\Sequence::find() example

<?php
$sequence = new \Ds\Vector(["a", 1, true]);

var_dump($sequence->find("a")); // 0
var_dump($sequence->find("b")); // false
var_dump($sequence->find("1")); // false
var_dump($sequence->find(1));   // 1
?>

위 예제의 출력 예시:

int(0)
bool(false)
bool(false)
int(1)

User Contributed Notes

There are no user contributed notes for this page.


Returns the first value in the sequence.

  • ≪ Ds\Sequence::find
  • Ds\Sequence::get ≫
  • PHP Manual
  • Sequence
  • Returns the first value in the sequence.

Ds\Sequence::first

(PECL ds >= 1.0.0)

Ds\Sequence::first — Returns the first value in the sequence.

설명

abstract public mixed Ds\Sequence::first ( void )

Returns the first value in the sequence.

인수

이 함수는 인수가 없습니다.

반환값

The first value in the sequence.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Sequence::first() example

<?php
$sequence = new \Ds\Vector([1, 2, 3]);
var_dump($sequence->first());
?>

위 예제의 출력 예시:

int(1)

User Contributed Notes

There are no user contributed notes for this page.


Returns the value at a given index.

  • ≪ Ds\Sequence::first
  • Ds\Sequence::insert ≫
  • PHP Manual
  • Sequence
  • Returns the value at a given index.

Ds\Sequence::get

(PECL ds >= 1.0.0)

Ds\Sequence::get — Returns the value at a given index.

설명

abstract public mixed Ds\Sequence::get ( int $index )

Returns the value at a given index.

인수

index

The index to access, starting at 0.

반환값

The value at the requested index.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Sequence::get() example

<?php
$sequence = new \Ds\Vector(["a", "b", "c"]);

var_dump($sequence->get(0));
var_dump($sequence->get(1));
var_dump($sequence->get(2));
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

Example #2 Ds\Sequence::get() example using array syntax

<?php
$sequence = new \Ds\Vector(["a", "b", "c"]);

var_dump($sequence[0]);
var_dump($sequence[1]);
var_dump($sequence[2]);
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Inserts values at a given index.

  • ≪ Ds\Sequence::get
  • Ds\Sequence::join ≫
  • PHP Manual
  • Sequence
  • Inserts values at a given index.

Ds\Sequence::insert

(PECL ds >= 1.0.0)

Ds\Sequence::insert — Inserts values at a given index.

설명

abstract public void Ds\Sequence::insert ( int $index [, mixed $...values ] )

Inserts values into the sequence at a given index.

인수

index

The index at which to insert. 0 <= index <= count

Note:

You can insert at the index equal to the number of values.

values

The value or values to insert.

반환값

값을 반환하지 않습니다.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Sequence::insert() example

<?php
$sequence = new \Ds\Vector();

$sequence->insert(0, "e");             // [e]
$sequence->insert(1, "f");             // [e, f]
$sequence->insert(2, "g");             // [e, f, g]
$sequence->insert(0, "a", "b");        // [a, b, e, f, g]
$sequence->insert(2, ...["c", "d"]);   // [a, b, c, d, e, f, g]

var_dump($sequence);
?>

위 예제의 출력 예시:

object(Ds\Vector)#1 (7) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
  [6]=>
  string(1) "g"
}

User Contributed Notes

There are no user contributed notes for this page.


Joins all values together as a string.

  • ≪ Ds\Sequence::insert
  • Ds\Sequence::last ≫
  • PHP Manual
  • Sequence
  • Joins all values together as a string.

Ds\Sequence::join

(PECL ds >= 1.0.0)

Ds\Sequence::join — Joins all values together as a string.

설명

abstract public string Ds\Sequence::join ([ string $glue ] )

Joins all values together as a string using an optional separator between each value.

인수

glue

An optional string to separate each value.

반환값

All values of the sequence joined together as a string.

예제

Example #1 Ds\Sequence::join() example using a separator string

<?php
$sequence = new \Ds\Vector(["a", "b", "c", 1, 2, 3]);

var_dump($sequence->join("|"));
?>

위 예제의 출력 예시:

string(11) "a|b|c|1|2|3"

Example #2 Ds\Sequence::join() example without a separator string

<?php
$sequence = new \Ds\Vector(["a", "b", "c", 1, 2, 3]);

var_dump($sequence->join());
?>

위 예제의 출력 예시:

string(11) "abc123"

User Contributed Notes

There are no user contributed notes for this page.


Returns the last value.

  • ≪ Ds\Sequence::join
  • Ds\Sequence::map ≫
  • PHP Manual
  • Sequence
  • Returns the last value.

Ds\Sequence::last

(PECL ds >= 1.0.0)

Ds\Sequence::last — Returns the last value.

설명

abstract public mixed Ds\Sequence::last ( void )

Returns the last value in the sequence.

인수

이 함수는 인수가 없습니다.

반환값

The last value in the sequence.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Sequence::last() example

<?php
$sequence = new \Ds\Vector([1, 2, 3]);
var_dump($sequence->last());
?>

위 예제의 출력 예시:

int(3)

User Contributed Notes

There are no user contributed notes for this page.


Returns the result of applying a callback to each value.

  • ≪ Ds\Sequence::last
  • Ds\Sequence::merge ≫
  • PHP Manual
  • Sequence
  • Returns the result of applying a callback to each value.

Ds\Sequence::map

(PECL ds >= 1.0.0)

Ds\Sequence::map — Returns the result of applying a callback to each value.

설명

abstract public Ds\Sequence Ds\Sequence::map ( callable $callback )

Returns the result of applying a callback function to each value in the sequence.

인수

callback

mixed callback ( mixed $value )

A callable to apply to each value in the sequence.

The callable should return what the new value will be in the new sequence.

반환값

The result of applying a callback to each value in the sequence.

Note:

The values of the current instance won't be affected.

예제

Example #1 Ds\Sequence::map() example

<?php
$sequence = new \Ds\Vector([1, 2, 3]);

print_r($sequence->map(function($value) { return $value * 2; }));
print_r($sequence);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 2
    [1] => 4
    [2] => 6
)
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the result of adding all given values to the sequence.

  • ≪ Ds\Sequence::map
  • Ds\Sequence::pop ≫
  • PHP Manual
  • Sequence
  • Returns the result of adding all given values to the sequence.

Ds\Sequence::merge

(PECL ds >= 1.0.0)

Ds\Sequence::merge — Returns the result of adding all given values to the sequence.

설명

abstract public Ds\Sequence Ds\Sequence::merge ( mixed $values )

Returns the result of adding all given values to the sequence.

인수

values

A traversable object or an array.

반환값

The result of adding all given values to the sequence, effectively the same as adding the values to a copy, then returning that copy.

Note:

The current instance won't be affected.

예제

Example #1 Ds\Sequence::merge() example

<?php
$sequence = new \Ds\Vector([1, 2, 3]);

var_dump($sequence->merge([4, 5, 6]));
var_dump($sequence);
?>

위 예제의 출력 예시:

object(Ds\Vector)#2 (6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
}
object(Ds\Vector)#1 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns the last value.

  • ≪ Ds\Sequence::merge
  • Ds\Sequence::push ≫
  • PHP Manual
  • Sequence
  • Removes and returns the last value.

Ds\Sequence::pop

(PECL ds >= 1.0.0)

Ds\Sequence::pop — Removes and returns the last value.

설명

abstract public mixed Ds\Sequence::pop ( void )

Removes and returns the last value.

인수

이 함수는 인수가 없습니다.

반환값

The removed last value.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Sequence::pop() example

<?php
$sequence = new \Ds\Vector([1, 2, 3]);

var_dump($sequence->pop());
var_dump($sequence->pop());
var_dump($sequence->pop());
?>

위 예제의 출력 예시:

int(3)
int(2)
int(1)

User Contributed Notes

There are no user contributed notes for this page.


Adds values to the end of the sequence.

  • ≪ Ds\Sequence::pop
  • Ds\Sequence::reduce ≫
  • PHP Manual
  • Sequence
  • Adds values to the end of the sequence.

Ds\Sequence::push

(PECL ds >= 1.0.0)

Ds\Sequence::push — Adds values to the end of the sequence.

설명

abstract public void Ds\Sequence::push ([ mixed $...values ] )

Adds values to the end of the sequence.

인수

values

The values to add.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Sequence::push() example

<?php
$sequence = new \Ds\Vector();

$sequence->push("a");
$sequence->push("b");
$sequence->push("c", "d");
$sequence->push(...["e", "f"]);

print_r($sequence);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)

User Contributed Notes

There are no user contributed notes for this page.


Reduces the sequence to a single value using a callback function.

  • ≪ Ds\Sequence::push
  • Ds\Sequence::remove ≫
  • PHP Manual
  • Sequence
  • Reduces the sequence to a single value using a callback function.

Ds\Sequence::reduce

(PECL ds >= 1.0.0)

Ds\Sequence::reduce — Reduces the sequence to a single value using a callback function.

설명

abstract public mixed Ds\Sequence::reduce ( callable $callback [, mixed $initial ] )

Reduces the sequence to a single value using a callback function.

인수

callback
mixed callback ( mixed $carry , mixed $value )
carry

The return value of the previous callback, or initial if it's the first iteration.

value

The value of the current iteration.

initial

The initial value of the carry value. Can be NULL.

반환값

The return value of the final callback.

예제

Example #1 Ds\Sequence::reduce() with initial value example

<?php
$sequence = new \Ds\Vector([1, 2, 3]);

$callback = function($carry, $value) {
    return $carry * $value;
};

var_dump($sequence->reduce($callback, 5));

// Iterations:
//
// $carry = $initial = 5
//
// $carry = $carry * 1 =  5
// $carry = $carry * 2 = 10
// $carry = $carry * 3 = 30
?>

위 예제의 출력 예시:

int(30)

Example #2 Ds\Sequence::reduce() without an initial value example

<?php
$sequence = new \Ds\Vector([1, 2, 3]);

var_dump($sequence->reduce(function($carry, $value) {
    return $carry + $value + 5;
}));

// Iterations:
//
// $carry = $initial = null
//
// $carry = $carry + 1 + 5 =  6
// $carry = $carry + 2 + 5 = 13
// $carry = $carry + 3 + 5 = 21
?>

위 예제의 출력 예시:

int(21)

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns a value by index.

  • ≪ Ds\Sequence::reduce
  • Ds\Sequence::reverse ≫
  • PHP Manual
  • Sequence
  • Removes and returns a value by index.

Ds\Sequence::remove

(PECL ds >= 1.0.0)

Ds\Sequence::remove — Removes and returns a value by index.

설명

abstract public mixed Ds\Sequence::remove ( int $index )

Removes and returns a value by index.

인수

index

The index of the value to remove.

반환값

The value that was removed.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Sequence::remove() example

<?php
$sequence = new \Ds\Vector(["a", "b", "c"]);

var_dump($sequence->remove(1));
var_dump($sequence->remove(0));
var_dump($sequence->remove(0));
?>

위 예제의 출력 예시:

string(1) "b"
string(1) "a"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Reverses the sequence in-place.

  • ≪ Ds\Sequence::remove
  • Ds\Sequence::reversed ≫
  • PHP Manual
  • Sequence
  • Reverses the sequence in-place.

Ds\Sequence::reverse

(PECL ds >= 1.0.0)

Ds\Sequence::reverse — Reverses the sequence in-place.

설명

abstract public void Ds\Sequence::reverse ( void )

Reverses the sequence in-place.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Sequence::reverse() example

<?php
$sequence = new \Ds\Vector(["a", "b", "c"]);
$sequence->reverse();

print_r($sequence);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => c
    [1] => b
    [2] => a
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a reversed copy.

  • ≪ Ds\Sequence::reverse
  • Ds\Sequence::rotate ≫
  • PHP Manual
  • Sequence
  • Returns a reversed copy.

Ds\Sequence::reversed

(PECL ds >= 1.0.0)

Ds\Sequence::reversed — Returns a reversed copy.

설명

abstract public Ds\Sequence Ds\Sequence::reversed ( void )

Returns a reversed copy of the sequence.

인수

이 함수는 인수가 없습니다.

반환값

A reversed copy of the sequence.

Note:

The current instance is not affected.

예제

Example #1 Ds\Sequence::reversed() example

<?php
$sequence = new \Ds\Vector(["a", "b", "c"]);

print_r($sequence->reversed());
print_r($sequence);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => c
    [1] => b
    [2] => a
)
Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
)

User Contributed Notes

There are no user contributed notes for this page.


Rotates the sequence by a given number of rotations.

  • ≪ Ds\Sequence::reversed
  • Ds\Sequence::set ≫
  • PHP Manual
  • Sequence
  • Rotates the sequence by a given number of rotations.

Ds\Sequence::rotate

(PECL ds >= 1.0.0)

Ds\Sequence::rotate — Rotates the sequence by a given number of rotations.

설명

abstract public void Ds\Sequence::rotate ( int $rotations )

Rotates the sequence by a given number of rotations, which is equivalent to successively calling $sequence->push($sequence->shift()) if the number of rotations is positive, or $sequence->unshift($sequence->pop()) if negative.

인수

rotations

The number of times the sequence should be rotated.

반환값

값을 반환하지 않습니다.. The sequence of the current instance will be rotated.

예제

Example #1 Ds\Sequence::rotate() example

<?php
$sequence = new \Ds\Vector(["a", "b", "c", "d"]);

$sequence->rotate(1);  // "a" is shifted, then pushed.
print_r($sequence);

$sequence->rotate(2);  // "b" and "c" are both shifted, the pushed.
print_r($sequence);
?>

위 예제의 출력 예시:

(
    [0] => b
    [1] => c
    [2] => d
    [3] => a
)
Ds\Vector Object
(
    [0] => d
    [1] => a
    [2] => b
    [3] => c
)

User Contributed Notes

There are no user contributed notes for this page.


Updates a value at a given index.

  • ≪ Ds\Sequence::rotate
  • Ds\Sequence::shift ≫
  • PHP Manual
  • Sequence
  • Updates a value at a given index.

Ds\Sequence::set

(PECL ds >= 1.0.0)

Ds\Sequence::set — Updates a value at a given index.

설명

abstract public void Ds\Sequence::set ( int $index , mixed $value )

Updates a value at a given index.

인수

index

The index of the value to update.

value

The new value.

반환값

값을 반환하지 않습니다.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Sequence::set() example

<?php
$sequence = new \Ds\Vector(["a", "b", "c"]);

$sequence->set(1, "_");
print_r($sequence);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => a
    [1] => _
    [2] => c
)

Example #2 Ds\Sequence::set() example using array syntax

<?php
$sequence = new \Ds\Vector(["a", "b", "c"]);

$sequence[1] = "_";
print_r($sequence);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => a
    [1] => _
    [2] => c
)

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns the first value.

  • ≪ Ds\Sequence::set
  • Ds\Sequence::slice ≫
  • PHP Manual
  • Sequence
  • Removes and returns the first value.

Ds\Sequence::shift

(PECL ds >= 1.0.0)

Ds\Sequence::shift — Removes and returns the first value.

설명

abstract public mixed Ds\Sequence::shift ( void )

Removes and returns the first value.

인수

이 함수는 인수가 없습니다.

반환값

The first value, which was removed.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Sequence::shift() example

<?php
$sequence = new \Ds\Vector(["a", "b", "c"]);

var_dump($sequence->shift());
var_dump($sequence->shift());
var_dump($sequence->shift());
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Returns a sub-sequence of a given range.

  • ≪ Ds\Sequence::shift
  • Ds\Sequence::sort ≫
  • PHP Manual
  • Sequence
  • Returns a sub-sequence of a given range.

Ds\Sequence::slice

(PECL ds >= 1.0.0)

Ds\Sequence::slice — Returns a sub-sequence of a given range.

설명

abstract public Ds\Sequence Ds\Sequence::slice ( int $index [, int $length ] )

Creates a sub-sequence of a given range.

인수

index

The index at which the sub-sequence starts.

If positive, the sequence will start at that index in the sequence. If negative, the sequence will start that far from the end.

length

If a length is given and is positive, the resulting sequence will have up to that many values in it. If the length results in an overflow, only values up to the end of the sequence will be included. If a length is given and is negative, the sequence will stop that many values from the end. If a length is not provided, the resulting sequence will contain all values between the index and the end of the sequence.

반환값

A sub-sequence of the given range.

예제

Example #1 Ds\Sequence::slice() example

<?php
$sequence = new \Ds\Vector(["a", "b", "c", "d", "e"]);

// Slice from 2 onwards
print_r($sequence->slice(2));

// Slice from 1, for a length of 3
print_r($sequence->slice(1, 3));

// Slice from 1 onwards
print_r($sequence->slice(1));

// Slice from 2 from the end onwards
print_r($sequence->slice(-2));

// Slice from 1 to 1 from the end
print_r($sequence->slice(1, -1));
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => c
    [1] => d
    [2] => e
)
Ds\Vector Object
(
    [0] => b
    [1] => c
    [2] => d
)
Ds\Vector Object
(
    [0] => b
    [1] => c
    [2] => d
    [3] => e
)
Ds\Vector Object
(
    [0] => d
    [1] => e
)
Ds\Vector Object
(
    [0] => b
    [1] => c
    [2] => d
)

User Contributed Notes

There are no user contributed notes for this page.


Sorts the sequence in-place.

  • ≪ Ds\Sequence::slice
  • Ds\Sequence::sorted ≫
  • PHP Manual
  • Sequence
  • Sorts the sequence in-place.

Ds\Sequence::sort

(PECL ds >= 1.0.0)

Ds\Sequence::sort — Sorts the sequence in-place.

설명

abstract public void Ds\Sequence::sort ([ callable $comparator ] )

Sorts the sequence in-place, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Sequence::sort() example

<?php
$sequence = new \Ds\Vector([4, 5, 1, 3, 2]);
$sequence->sort();

print_r($sequence);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example #2 Ds\Sequence::sort() example using a comparator

<?php
$sequence = new \Ds\Vector([4, 5, 1, 3, 2]);

$sequence->sort(function($a, $b) {
    return $b <=> $a;
});

print_r($sequence);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a sorted copy.

  • ≪ Ds\Sequence::sort
  • Ds\Sequence::sum ≫
  • PHP Manual
  • Sequence
  • Returns a sorted copy.

Ds\Sequence::sorted

(PECL ds >= 1.0.0)

Ds\Sequence::sorted — Returns a sorted copy.

설명

abstract public Ds\Sequence Ds\Sequence::sorted ([ callable $comparator ] )

Returns a sorted copy, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

Returns a sorted copy of the sequence.

예제

Example #1 Ds\Sequence::sorted() example

<?php
$sequence = new \Ds\Vector([4, 5, 1, 3, 2]);

print_r($sequence->sorted());
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example #2 Ds\Sequence::sorted() example using a comparator

<?php
$sequence = new \Ds\Vector([4, 5, 1, 3, 2]);

$sorted = $sequence->sorted(function($a, $b) {
    return $b <=> $a;
});

print_r($sorted);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the sum of all values in the sequence.

  • ≪ Ds\Sequence::sorted
  • Ds\Sequence::unshift ≫
  • PHP Manual
  • Sequence
  • Returns the sum of all values in the sequence.

Ds\Sequence::sum

(PECL ds >= 1.0.0)

Ds\Sequence::sum — Returns the sum of all values in the sequence.

설명

abstract public number Ds\Sequence::sum ( void )

Returns the sum of all values in the sequence.

Note:

Arrays and objects are considered equal to zero when calculating the sum.

인수

이 함수는 인수가 없습니다.

반환값

The sum of all the values in the sequence as either a float or int depending on the values in the sequence.

예제

Example #1 Ds\Sequence::sum() integer example

<?php
$sequence = new \Ds\Vector([1, 2, 3]);
var_dump($sequence->sum());
?>

위 예제의 출력 예시:

int(6)

Example #2 Ds\Sequence::sum() float example

<?php
$sequence = new \Ds\Vector([1, 2.5, 3]);
var_dump($sequence->sum());
?>

위 예제의 출력 예시:

float(6.5)

User Contributed Notes

There are no user contributed notes for this page.


Adds values to the front of the sequence.

  • ≪ Ds\Sequence::sum
  • Vector ≫
  • PHP Manual
  • Sequence
  • Adds values to the front of the sequence.

Ds\Sequence::unshift

(PECL ds >= 1.0.0)

Ds\Sequence::unshift — Adds values to the front of the sequence.

설명

abstract public void Ds\Sequence::unshift ([ mixed $values ] )

Adds values to the front of the sequence, moving all the current values forward to make room for the new values.

인수

values

The values to add to the front of the sequence.

Note:

Multiple values will be added in the same order that they are passed.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Sequence::unshift() example

<?php
$sequence = new \Ds\Vector([1, 2, 3]);

$sequence->unshift("a");
$sequence->unshift("b", "c");

print_r($sequence);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => b
    [1] => c
    [2] => a
    [3] => 1
    [4] => 2
    [5] => 3
)

User Contributed Notes

There are no user contributed notes for this page.


The Vector class

(No version information available, might only be in Git)

소개

A Vector is a sequence of values in a contiguous buffer that grows and shrinks automatically. It's the most efficient sequential structure because a value's index is a direct mapping to its index in the buffer, and the growth factor isn't bound to a specific multiple or exponent.

Strengths

  • Supports array syntax (square brackets).
  • Uses less overall memory than an array for the same number of values.
  • Automatically frees allocated memory when its size drops low enough.
  • Capacity does not have to be a power of 2.
  • get(),
set(),
push(),
pop() are all O(1).

Weaknesses

  • shift(),
unshift(),
insert() and
remove() are all O(n).

클래스 개요

Ds\Vector implements Ds\Sequence {
/* Constants */
const int MIN_CAPACITY = 10 ;
/* 메소드 */
public void allocate ( int $capacity )
public void apply ( callable $callback )
public int capacity ( void )
public void clear ( void )
public bool contains ([ mixed $...values ] )
public Ds\Vector copy ( void )
public Ds\Vector filter ([ callable $callback ] )
public mixed find ( mixed $value )
public mixed first ( void )
public mixed get ( int $index )
public void insert ( int $index [, mixed $...values ] )
public bool isEmpty ( void )
public string join ([ string $glue ] )
public mixed last ( void )
public Ds\Vector map ( callable $callback )
public Ds\Vector merge ( mixed $values )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public mixed reduce ( callable $callback [, mixed $initial ] )
public mixed remove ( int $index )
public void reverse ( void )
public Ds\Vector reversed ( void )
public void rotate ( int $rotations )
public void set ( int $index , mixed $value )
public mixed shift ( void )
public Ds\Vector slice ( int $index [, int $length ] )
public void sort ([ callable $comparator ] )
public Ds\Vector sorted ([ callable $comparator ] )
public number sum ( void )
public array toArray ( void )
public void unshift ([ mixed $values ] )
}

예약 상수

Ds\Vector::MIN_CAPACITY

Table of Contents

  • Ds\Vector::allocate — Allocates enough memory for a required capacity.
  • Ds\Vector::apply — Updates all values by applying a callback function to each value.
  • Ds\Vector::capacity — Returns the current capacity.
  • Ds\Vector::clear — Removes all values.
  • Ds\Vector::__construct — Creates a new instance.
  • Ds\Vector::contains — Determines if the vector contains given values.
  • Ds\Vector::copy — Returns a shallow copy of the vector.
  • Ds\Vector::count — Returns the number of values in the collection.
  • Ds\Vector::filter — Creates a new vector using a callable to determine which values to include.
  • Ds\Vector::find — Attempts to find a value's index.
  • Ds\Vector::first — Returns the first value in the vector.
  • Ds\Vector::get — Returns the value at a given index.
  • Ds\Vector::insert — Inserts values at a given index.
  • Ds\Vector::isEmpty — Returns whether the vector is empty
  • Ds\Vector::join — Joins all values together as a string.
  • Ds\Vector::jsonSerialize — Returns a representation that can be converted to JSON.
  • Ds\Vector::last — Returns the last value.
  • Ds\Vector::map — Returns the result of applying a callback to each value.
  • Ds\Vector::merge — Returns the result of adding all given values to the vector.
  • Ds\Vector::pop — Removes and returns the last value.
  • Ds\Vector::push — Adds values to the end of the vector.
  • Ds\Vector::reduce — Reduces the vector to a single value using a callback function.
  • Ds\Vector::remove — Removes and returns a value by index.
  • Ds\Vector::reverse — Reverses the vector in-place.
  • Ds\Vector::reversed — Returns a reversed copy.
  • Ds\Vector::rotate — Rotates the vector by a given number of rotations.
  • Ds\Vector::set — Updates a value at a given index.
  • Ds\Vector::shift — Removes and returns the first value.
  • Ds\Vector::slice — Returns a sub-vector of a given range.
  • Ds\Vector::sort — Sorts the vector in-place.
  • Ds\Vector::sorted — Returns a sorted copy.
  • Ds\Vector::sum — Returns the sum of all values in the vector.
  • Ds\Vector::toArray — Converts the vector to an array.
  • Ds\Vector::unshift — Adds values to the front of the vector.

User Contributed Notes

There are no user contributed notes for this page.


Allocates enough memory for a required capacity.

  • ≪ Vector
  • Ds\Vector::apply ≫
  • PHP Manual
  • Vector
  • Allocates enough memory for a required capacity.

Ds\Vector::allocate

(PECL ds >= 1.0.0)

Ds\Vector::allocate — Allocates enough memory for a required capacity.

설명

public void Ds\Vector::allocate ( int $capacity )

Ensures that enough memory is allocated for a required capacity. This removes the need to reallocate the internal as values are added.

인수

capacity

The number of values for which capacity should be allocated.

Note:

Capacity will stay the same if this value is less than or equal to the current capacity.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Vector::allocate() example

<?php
$vector = new \Ds\Vector();
var_dump($vector->capacity());

$vector->allocate(100);
var_dump($vector->capacity());
?>

위 예제의 출력 예시:

int(10)
int(100)

User Contributed Notes

There are no user contributed notes for this page.


Updates all values by applying a callback function to each value.

  • ≪ Ds\Vector::allocate
  • Ds\Vector::capacity ≫
  • PHP Manual
  • Vector
  • Updates all values by applying a callback function to each value.

Ds\Vector::apply

(PECL ds >= 1.0.0)

Ds\Vector::apply — Updates all values by applying a callback function to each value.

설명

public void Ds\Vector::apply ( callable $callback )

Updates all values by applying a callback function to each value in the vector.

인수

callback

mixed callback ( mixed $value )

A callable to apply to each value in the vector.

The callback should return what the value should be replaced by.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Vector::apply() example

<?php
$vector = new \Ds\Vector([1, 2, 3]);
$vector->apply(function($value) { return $value * 2; });

print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 2
    [1] => 4
    [2] => 6
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the current capacity.

  • ≪ Ds\Vector::apply
  • Ds\Vector::clear ≫
  • PHP Manual
  • Vector
  • Returns the current capacity.

Ds\Vector::capacity

(PECL ds >= 1.0.0)

Ds\Vector::capacity — Returns the current capacity.

설명

public int Ds\Vector::capacity ( void )

Returns the current capacity.

인수

이 함수는 인수가 없습니다.

반환값

The current capacity.

예제

Example #1 Ds\Vector::capacity() example

<?php
$vector = new \Ds\Vector();
var_dump($vector->capacity());

$vector->push(...range(1, 50));
var_dump($vector->capacity());

$vector[] = "a";
var_dump($vector->capacity());
?>

위 예제의 출력 예시:

int(10)
int(50)
int(75)

User Contributed Notes

There are no user contributed notes for this page.


Removes all values.

  • ≪ Ds\Vector::capacity
  • Ds\Vector::__construct ≫
  • PHP Manual
  • Vector
  • Removes all values.

Ds\Vector::clear

(PECL ds >= 1.0.0)

Ds\Vector::clear — Removes all values.

설명

public void Ds\Vector::clear ( void )

Removes all values from the vector.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Vector::clear() example

<?php
$vector = new \Ds\Vector([1, 2, 3]);
print_r($vector);

$vector->clear();
print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Vector Object
(
)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new instance.

  • ≪ Ds\Vector::clear
  • Ds\Vector::contains ≫
  • PHP Manual
  • Vector
  • Creates a new instance.

Ds\Vector::__construct

(PECL ds >= 1.0.0)

Ds\Vector::__construct — Creates a new instance.

설명

public Ds\Vector::__construct ([ mixed $values ] )

Creates a new instance, using either a traversable object or an array for the initial values.

인수

values

A traversable object or an array to use for the initial values.

예제

Example #1 Ds\Vector::__construct() example

<?php
$vector = new \Ds\Vector();
var_dump($vector);

$vector = new \Ds\Vector([1, 2, 3]);
var_dump($vector);
?>

위 예제의 출력 예시:

object(Ds\Vector)#2 (0) {
}
object(Ds\Vector)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Determines if the vector contains given values.

  • ≪ Ds\Vector::__construct
  • Ds\Vector::copy ≫
  • PHP Manual
  • Vector
  • Determines if the vector contains given values.

Ds\Vector::contains

(PECL ds >= 1.0.0)

Ds\Vector::contains — Determines if the vector contains given values.

설명

public bool Ds\Vector::contains ([ mixed $...values ] )

Determines if the vector contains all values.

인수

values

Values to check.

반환값

FALSE if any of the provided values are not in the vector, TRUE otherwise.

예제

Example #1 Ds\Vector::contains() example

<?php
$vector = new \Ds\Vector(['a', 'b', 'c', 1, 2, 3]);

var_dump($vector->contains('a'));                // true
var_dump($vector->contains('a', 'b'));           // true
var_dump($vector->contains('c', 'd'));           // false

var_dump($vector->contains(...['c', 'b', 'a'])); // true

// Always strict
var_dump($vector->contains(1));                  // true
var_dump($vector->contains('1'));                // false

var_dump($sequece->contains(...[]));               // true
?>

위 예제의 출력 예시:

bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Returns a shallow copy of the vector.

  • ≪ Ds\Vector::contains
  • Ds\Vector::count ≫
  • PHP Manual
  • Vector
  • Returns a shallow copy of the vector.

Ds\Vector::copy

(PECL ds >= 1.0.0)

Ds\Vector::copy — Returns a shallow copy of the vector.

설명

public Ds\Vector Ds\Vector::copy ( void )

Returns a shallow copy of the vector.

인수

이 함수는 인수가 없습니다.

반환값

Returns a shallow copy of the vector.

예제

Example #1 Ds\Vector::copy() example

<?php
$a = new \Ds\Vector([1, 2, 3]);
$b = $a->copy();

// Updating the copy doesn't affect the original
$b->push(4);

print_r($a);
print_r($b);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of values in the collection.

  • ≪ Ds\Vector::copy
  • Ds\Vector::filter ≫
  • PHP Manual
  • Vector
  • Returns the number of values in the collection.

Ds\Vector::count

(PECL ds >= 1.0.0)

Ds\Vector::count — Returns the number of values in the collection.

See Countable::count()

User Contributed Notes

There are no user contributed notes for this page.


Creates a new vector using a callable to determine which values to include.

  • ≪ Ds\Vector::count
  • Ds\Vector::find ≫
  • PHP Manual
  • Vector
  • Creates a new vector using a callable to determine which values to include.

Ds\Vector::filter

(PECL ds >= 1.0.0)

Ds\Vector::filter — Creates a new vector using a callable to determine which values to include.

설명

public Ds\Vector Ds\Vector::filter ([ callable $callback ] )

Creates a new vector using a callable to determine which values to include.

인수

callback

bool callback ( mixed $value )

Optional callable which returns TRUE if the value should be included, FALSE otherwise.

If a callback is not provided, only values which are TRUE (see converting to boolean) will be included.

반환값

A new vector containing all the values for which either the callback returned TRUE, or all values that convert to TRUE if a callback was not provided.

예제

Example #1 Ds\Vector::filter() example using callback function

<?php
$vector = new \Ds\Vector([1, 2, 3, 4, 5]);

var_dump($vector->filter(function($value) {
    return $value % 2 == 0;
}));
?>

위 예제의 출력 예시:

object(Ds\Vector)#3 (2) {
  [0]=>
  int(2)
  [1]=>
  int(4)
}

Example #2 Ds\Vector::filter() example without a callback function

<?php
$vector = new \Ds\Vector([0, 1, 'a', true, false]);

var_dump($vector->filter());
?>

위 예제의 출력 예시:

object(Ds\Vector)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  string(1) "a"
  [2]=>
  bool(true)
}

User Contributed Notes

There are no user contributed notes for this page.


Attempts to find a value's index.

  • ≪ Ds\Vector::filter
  • Ds\Vector::first ≫
  • PHP Manual
  • Vector
  • Attempts to find a value's index.

Ds\Vector::find

(PECL ds >= 1.0.0)

Ds\Vector::find — Attempts to find a value's index.

설명

public mixed Ds\Vector::find ( mixed $value )

Returns the index of the value, or FALSE if not found.

인수

value

The value to find.

반환값

The index of the value, or FALSE if not found.

Note:

Values will be compared by value and by type.

예제

Example #1 Ds\Vector::find() example

<?php
$vector = new \Ds\Vector(["a", 1, true]);

var_dump($vector->find("a")); // 0
var_dump($vector->find("b")); // false
var_dump($vector->find("1")); // false
var_dump($vector->find(1));   // 1
?>

위 예제의 출력 예시:

int(0)
bool(false)
bool(false)
int(1)

User Contributed Notes

There are no user contributed notes for this page.


Returns the first value in the vector.

  • ≪ Ds\Vector::find
  • Ds\Vector::get ≫
  • PHP Manual
  • Vector
  • Returns the first value in the vector.

Ds\Vector::first

(PECL ds >= 1.0.0)

Ds\Vector::first — Returns the first value in the vector.

설명

public mixed Ds\Vector::first ( void )

Returns the first value in the vector.

인수

이 함수는 인수가 없습니다.

반환값

The first value in the vector.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Vector::first() example

<?php
$vector = new \Ds\Vector([1, 2, 3]);
var_dump($vector->first());
?>

위 예제의 출력 예시:

int(1)

User Contributed Notes

There are no user contributed notes for this page.


Returns the value at a given index.

  • ≪ Ds\Vector::first
  • Ds\Vector::insert ≫
  • PHP Manual
  • Vector
  • Returns the value at a given index.

Ds\Vector::get

(PECL ds >= 1.0.0)

Ds\Vector::get — Returns the value at a given index.

설명

public mixed Ds\Vector::get ( int $index )

Returns the value at a given index.

인수

index

The index to access, starting at 0.

반환값

The value at the requested index.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Vector::get() example

<?php
$vector = new \Ds\Vector(["a", "b", "c"]);

var_dump($vector->get(0));
var_dump($vector->get(1));
var_dump($vector->get(2));
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

Example #2 Ds\Vector::get() example using array syntax

<?php
$vector = new \Ds\Vector(["a", "b", "c"]);

var_dump($vector[0]);
var_dump($vector[1]);
var_dump($vector[2]);
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Inserts values at a given index.

  • ≪ Ds\Vector::get
  • Ds\Vector::isEmpty ≫
  • PHP Manual
  • Vector
  • Inserts values at a given index.

Ds\Vector::insert

(PECL ds >= 1.0.0)

Ds\Vector::insert — Inserts values at a given index.

설명

public void Ds\Vector::insert ( int $index [, mixed $...values ] )

Inserts values into the vector at a given index.

인수

index

The index at which to insert. 0 <= index <= count

Note:

You can insert at the index equal to the number of values.

values

The value or values to insert.

반환값

값을 반환하지 않습니다.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Vector::insert() example

<?php
$vector = new \Ds\Vector();

$vector->insert(0, "e");             // [e]
$vector->insert(1, "f");             // [e, f]
$vector->insert(2, "g");             // [e, f, g]
$vector->insert(0, "a", "b");        // [a, b, e, f, g]
$vector->insert(2, ...["c", "d"]);   // [a, b, c, d, e, f, g]

var_dump($vector);
?>

위 예제의 출력 예시:

object(Ds\Vector)#1 (7) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
  [6]=>
  string(1) "g"
}

User Contributed Notes

There are no user contributed notes for this page.


Returns whether the vector is empty

  • ≪ Ds\Vector::insert
  • Ds\Vector::join ≫
  • PHP Manual
  • Vector
  • Returns whether the vector is empty

Ds\Vector::isEmpty

(PECL ds >= 1.0.0)

Ds\Vector::isEmpty — Returns whether the vector is empty

설명

public bool Ds\Vector::isEmpty ( void )

Returns whether the vector is empty.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the vector is empty, FALSE otherwise.

예제

Example #1 Ds\Vector::isEmpty() example

<?php
$a = new \Ds\Vector([1, 2, 3]);
$b = new \Ds\Vector();

var_dump($a->isEmpty());
var_dump($b->isEmpty());
?>

위 예제의 출력 예시:

bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Joins all values together as a string.

  • ≪ Ds\Vector::isEmpty
  • Ds\Vector::jsonSerialize ≫
  • PHP Manual
  • Vector
  • Joins all values together as a string.

Ds\Vector::join

(PECL ds >= 1.0.0)

Ds\Vector::join — Joins all values together as a string.

설명

public string Ds\Vector::join ([ string $glue ] )

Joins all values together as a string using an optional separator between each value.

인수

glue

An optional string to separate each value.

반환값

All values of the vector joined together as a string.

예제

Example #1 Ds\Vector::join() example using a separator string

<?php
$vector = new \Ds\Vector(["a", "b", "c", 1, 2, 3]);

var_dump($vector->join("|"));
?>

위 예제의 출력 예시:

string(11) "a|b|c|1|2|3"

Example #2 Ds\Vector::join() example without a separator string

<?php
$vector = new \Ds\Vector(["a", "b", "c", 1, 2, 3]);

var_dump($vector->join());
?>

위 예제의 출력 예시:

string(11) "abc123"

User Contributed Notes

There are no user contributed notes for this page.


Returns a representation that can be converted to JSON.

  • ≪ Ds\Vector::join
  • Ds\Vector::last ≫
  • PHP Manual
  • Vector
  • Returns a representation that can be converted to JSON.

Ds\Vector::jsonSerialize

(PECL ds >= 1.0.0)

Ds\Vector::jsonSerialize — Returns a representation that can be converted to JSON.

See JsonSerializable::jsonSerialize()

Note:

You should never need to call this directly.

User Contributed Notes

There are no user contributed notes for this page.


Returns the last value.

  • ≪ Ds\Vector::jsonSerialize
  • Ds\Vector::map ≫
  • PHP Manual
  • Vector
  • Returns the last value.

Ds\Vector::last

(PECL ds >= 1.0.0)

Ds\Vector::last — Returns the last value.

설명

public mixed Ds\Vector::last ( void )

Returns the last value in the vector.

인수

이 함수는 인수가 없습니다.

반환값

The last value in the vector.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Vector::last() example

<?php
$vector = new \Ds\Vector([1, 2, 3]);
var_dump($vector->last());
?>

위 예제의 출력 예시:

int(3)

User Contributed Notes

There are no user contributed notes for this page.


Returns the result of applying a callback to each value.

  • ≪ Ds\Vector::last
  • Ds\Vector::merge ≫
  • PHP Manual
  • Vector
  • Returns the result of applying a callback to each value.

Ds\Vector::map

(PECL ds >= 1.0.0)

Ds\Vector::map — Returns the result of applying a callback to each value.

설명

public Ds\Vector Ds\Vector::map ( callable $callback )

Returns the result of applying a callback function to each value in the vector.

인수

callback

mixed callback ( mixed $value )

A callable to apply to each value in the vector.

The callable should return what the new value will be in the new vector.

반환값

The result of applying a callback to each value in the vector.

Note:

The values of the current instance won't be affected.

예제

Example #1 Ds\Vector::map() example

<?php
$vector = new \Ds\Vector([1, 2, 3]);

print_r($vector->map(function($value) { return $value * 2; }));
print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 2
    [1] => 4
    [2] => 6
)
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the result of adding all given values to the vector.

  • ≪ Ds\Vector::map
  • Ds\Vector::pop ≫
  • PHP Manual
  • Vector
  • Returns the result of adding all given values to the vector.

Ds\Vector::merge

(PECL ds >= 1.0.0)

Ds\Vector::merge — Returns the result of adding all given values to the vector.

설명

public Ds\Vector Ds\Vector::merge ( mixed $values )

Returns the result of adding all given values to the vector.

인수

values

A traversable object or an array.

반환값

The result of adding all given values to the vector, effectively the same as adding the values to a copy, then returning that copy.

Note:

The current instance won't be affected.

예제

Example #1 Ds\Vector::merge() example

<?php
$vector = new \Ds\Vector([1, 2, 3]);

var_dump($vector->merge([4, 5, 6]));
var_dump($vector);
?>

위 예제의 출력 예시:

object(Ds\Vector)#2 (6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
}
object(Ds\Vector)#1 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns the last value.

  • ≪ Ds\Vector::merge
  • Ds\Vector::push ≫
  • PHP Manual
  • Vector
  • Removes and returns the last value.

Ds\Vector::pop

(PECL ds >= 1.0.0)

Ds\Vector::pop — Removes and returns the last value.

설명

public mixed Ds\Vector::pop ( void )

Removes and returns the last value.

인수

이 함수는 인수가 없습니다.

반환값

The removed last value.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Vector::pop() example

<?php
$vector = new \Ds\Vector([1, 2, 3]);

var_dump($vector->pop());
var_dump($vector->pop());
var_dump($vector->pop());
?>

위 예제의 출력 예시:

int(3)
int(2)
int(1)

User Contributed Notes

There are no user contributed notes for this page.


Adds values to the end of the vector.

  • ≪ Ds\Vector::pop
  • Ds\Vector::reduce ≫
  • PHP Manual
  • Vector
  • Adds values to the end of the vector.

Ds\Vector::push

(PECL ds >= 1.0.0)

Ds\Vector::push — Adds values to the end of the vector.

설명

public void Ds\Vector::push ([ mixed $...values ] )

Adds values to the end of the vector.

인수

values

The values to add.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Vector::push() example

<?php
$vector = new \Ds\Vector();

$vector->push("a");
$vector->push("b");
$vector->push("c", "d");
$vector->push(...["e", "f"]);

print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)

User Contributed Notes

There are no user contributed notes for this page.


Reduces the vector to a single value using a callback function.

  • ≪ Ds\Vector::push
  • Ds\Vector::remove ≫
  • PHP Manual
  • Vector
  • Reduces the vector to a single value using a callback function.

Ds\Vector::reduce

(PECL ds >= 1.0.0)

Ds\Vector::reduce — Reduces the vector to a single value using a callback function.

설명

public mixed Ds\Vector::reduce ( callable $callback [, mixed $initial ] )

Reduces the vector to a single value using a callback function.

인수

callback
mixed callback ( mixed $carry , mixed $value )
carry

The return value of the previous callback, or initial if it's the first iteration.

value

The value of the current iteration.

initial

The initial value of the carry value. Can be NULL.

반환값

The return value of the final callback.

예제

Example #1 Ds\Vector::reduce() with initial value example

<?php
$vector = new \Ds\Vector([1, 2, 3]);

$callback = function($carry, $value) {
    return $carry * $value;
};

var_dump($vector->reduce($callback, 5));

// Iterations:
//
// $carry = $initial = 5
//
// $carry = $carry * 1 =  5
// $carry = $carry * 2 = 10
// $carry = $carry * 3 = 30
?>

위 예제의 출력 예시:

int(30)

Example #2 Ds\Vector::reduce() without an initial value example

<?php
$vector = new \Ds\Vector([1, 2, 3]);

var_dump($vector->reduce(function($carry, $value) {
    return $carry + $value + 5;
}));

// Iterations:
//
// $carry = $initial = null
//
// $carry = $carry + 1 + 5 =  6
// $carry = $carry + 2 + 5 = 13
// $carry = $carry + 3 + 5 = 21
?>

위 예제의 출력 예시:

int(21)

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns a value by index.

  • ≪ Ds\Vector::reduce
  • Ds\Vector::reverse ≫
  • PHP Manual
  • Vector
  • Removes and returns a value by index.

Ds\Vector::remove

(PECL ds >= 1.0.0)

Ds\Vector::remove — Removes and returns a value by index.

설명

public mixed Ds\Vector::remove ( int $index )

Removes and returns a value by index.

인수

index

The index of the value to remove.

반환값

The value that was removed.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Vector::remove() example

<?php
$vector = new \Ds\Vector(["a", "b", "c"]);

var_dump($vector->remove(1));
var_dump($vector->remove(0));
var_dump($vector->remove(0));
?>

위 예제의 출력 예시:

string(1) "b"
string(1) "a"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Reverses the vector in-place.

  • ≪ Ds\Vector::remove
  • Ds\Vector::reversed ≫
  • PHP Manual
  • Vector
  • Reverses the vector in-place.

Ds\Vector::reverse

(PECL ds >= 1.0.0)

Ds\Vector::reverse — Reverses the vector in-place.

설명

public void Ds\Vector::reverse ( void )

Reverses the vector in-place.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Vector::reverse() example

<?php
$vector = new \Ds\Vector(["a", "b", "c"]);
$vector->reverse();

print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => c
    [1] => b
    [2] => a
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a reversed copy.

  • ≪ Ds\Vector::reverse
  • Ds\Vector::rotate ≫
  • PHP Manual
  • Vector
  • Returns a reversed copy.

Ds\Vector::reversed

(PECL ds >= 1.0.0)

Ds\Vector::reversed — Returns a reversed copy.

설명

public Ds\Vector Ds\Vector::reversed ( void )

Returns a reversed copy of the vector.

인수

이 함수는 인수가 없습니다.

반환값

A reversed copy of the vector.

Note:

The current instance is not affected.

예제

Example #1 Ds\Vector::reversed() example

<?php
$vector = new \Ds\Vector(["a", "b", "c"]);

print_r($vector->reversed());
print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => c
    [1] => b
    [2] => a
)
Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
)

User Contributed Notes

There are no user contributed notes for this page.


Rotates the vector by a given number of rotations.

  • ≪ Ds\Vector::reversed
  • Ds\Vector::set ≫
  • PHP Manual
  • Vector
  • Rotates the vector by a given number of rotations.

Ds\Vector::rotate

(PECL ds >= 1.0.0)

Ds\Vector::rotate — Rotates the vector by a given number of rotations.

설명

public void Ds\Vector::rotate ( int $rotations )

Rotates the vector by a given number of rotations, which is equivalent to successively calling $vector->push($vector->shift()) if the number of rotations is positive, or $vector->unshift($vector->pop()) if negative.

인수

rotations

The number of times the vector should be rotated.

반환값

값을 반환하지 않습니다.. The vector of the current instance will be rotated.

예제

Example #1 Ds\Vector::rotate() example

<?php
$vector = new \Ds\Vector(["a", "b", "c", "d"]);

$vector->rotate(1);  // "a" is shifted, then pushed.
print_r($vector);

$vector->rotate(2);  // "b" and "c" are both shifted, the pushed.
print_r($vector);
?>

위 예제의 출력 예시:

(
    [0] => b
    [1] => c
    [2] => d
    [3] => a
)
Ds\Vector Object
(
    [0] => d
    [1] => a
    [2] => b
    [3] => c
)

User Contributed Notes

There are no user contributed notes for this page.


Updates a value at a given index.

  • ≪ Ds\Vector::rotate
  • Ds\Vector::shift ≫
  • PHP Manual
  • Vector
  • Updates a value at a given index.

Ds\Vector::set

(PECL ds >= 1.0.0)

Ds\Vector::set — Updates a value at a given index.

설명

public void Ds\Vector::set ( int $index , mixed $value )

Updates a value at a given index.

인수

index

The index of the value to update.

value

The new value.

반환값

값을 반환하지 않습니다.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Vector::set() example

<?php
$vector = new \Ds\Vector(["a", "b", "c"]);

$vector->set(1, "_");
print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => a
    [1] => _
    [2] => c
)

Example #2 Ds\Vector::set() example using array syntax

<?php
$vector = new \Ds\Vector(["a", "b", "c"]);

$vector[1] = "_";
print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => a
    [1] => _
    [2] => c
)

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns the first value.

  • ≪ Ds\Vector::set
  • Ds\Vector::slice ≫
  • PHP Manual
  • Vector
  • Removes and returns the first value.

Ds\Vector::shift

(PECL ds >= 1.0.0)

Ds\Vector::shift — Removes and returns the first value.

설명

public mixed Ds\Vector::shift ( void )

Removes and returns the first value.

인수

이 함수는 인수가 없습니다.

반환값

The first value, which was removed.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Vector::shift() example

<?php
$vector = new \Ds\Vector(["a", "b", "c"]);

var_dump($vector->shift());
var_dump($vector->shift());
var_dump($vector->shift());
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Returns a sub-vector of a given range.

  • ≪ Ds\Vector::shift
  • Ds\Vector::sort ≫
  • PHP Manual
  • Vector
  • Returns a sub-vector of a given range.

Ds\Vector::slice

(PECL ds >= 1.0.0)

Ds\Vector::slice — Returns a sub-vector of a given range.

설명

public Ds\Vector Ds\Vector::slice ( int $index [, int $length ] )

Creates a sub-vector of a given range.

인수

index

The index at which the sub-vector starts.

If positive, the vector will start at that index in the vector. If negative, the vector will start that far from the end.

length

If a length is given and is positive, the resulting vector will have up to that many values in it. If the length results in an overflow, only values up to the end of the vector will be included. If a length is given and is negative, the vector will stop that many values from the end. If a length is not provided, the resulting vector will contain all values between the index and the end of the vector.

반환값

A sub-vector of the given range.

예제

Example #1 Ds\Vector::slice() example

<?php
$vector = new \Ds\Vector(["a", "b", "c", "d", "e"]);

// Slice from 2 onwards
print_r($vector->slice(2));

// Slice from 1, for a length of 3
print_r($vector->slice(1, 3));

// Slice from 1 onwards
print_r($vector->slice(1));

// Slice from 2 from the end onwards
print_r($vector->slice(-2));

// Slice from 1 to 1 from the end
print_r($vector->slice(1, -1));
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => c
    [1] => d
    [2] => e
)
Ds\Vector Object
(
    [0] => b
    [1] => c
    [2] => d
)
Ds\Vector Object
(
    [0] => b
    [1] => c
    [2] => d
    [3] => e
)
Ds\Vector Object
(
    [0] => d
    [1] => e
)
Ds\Vector Object
(
    [0] => b
    [1] => c
    [2] => d
)

User Contributed Notes

There are no user contributed notes for this page.


Sorts the vector in-place.

  • ≪ Ds\Vector::slice
  • Ds\Vector::sorted ≫
  • PHP Manual
  • Vector
  • Sorts the vector in-place.

Ds\Vector::sort

(PECL ds >= 1.0.0)

Ds\Vector::sort — Sorts the vector in-place.

설명

public void Ds\Vector::sort ([ callable $comparator ] )

Sorts the vector in-place, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Vector::sort() example

<?php
$vector = new \Ds\Vector([4, 5, 1, 3, 2]);
$vector->sort();

print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example #2 Ds\Vector::sort() example using a comparator

<?php
$vector = new \Ds\Vector([4, 5, 1, 3, 2]);

$vector->sort(function($a, $b) {
    return $b <=> $a;
});

print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a sorted copy.

  • ≪ Ds\Vector::sort
  • Ds\Vector::sum ≫
  • PHP Manual
  • Vector
  • Returns a sorted copy.

Ds\Vector::sorted

(PECL ds >= 1.0.0)

Ds\Vector::sorted — Returns a sorted copy.

설명

public Ds\Vector Ds\Vector::sorted ([ callable $comparator ] )

Returns a sorted copy, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

Returns a sorted copy of the vector.

예제

Example #1 Ds\Vector::sorted() example

<?php
$vector = new \Ds\Vector([4, 5, 1, 3, 2]);

print_r($vector->sorted());
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example #2 Ds\Vector::sorted() example using a comparator

<?php
$vector = new \Ds\Vector([4, 5, 1, 3, 2]);

$sorted = $vector->sorted(function($a, $b) {
    return $b <=> $a;
});

print_r($sorted);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the sum of all values in the vector.

  • ≪ Ds\Vector::sorted
  • Ds\Vector::toArray ≫
  • PHP Manual
  • Vector
  • Returns the sum of all values in the vector.

Ds\Vector::sum

(PECL ds >= 1.0.0)

Ds\Vector::sum — Returns the sum of all values in the vector.

설명

public number Ds\Vector::sum ( void )

Returns the sum of all values in the vector.

Note:

Arrays and objects are considered equal to zero when calculating the sum.

인수

이 함수는 인수가 없습니다.

반환값

The sum of all the values in the vector as either a float or int depending on the values in the vector.

예제

Example #1 Ds\Vector::sum() integer example

<?php
$vector = new \Ds\Vector([1, 2, 3]);
var_dump($vector->sum());
?>

위 예제의 출력 예시:

int(6)

Example #2 Ds\Vector::sum() float example

<?php
$vector = new \Ds\Vector([1, 2.5, 3]);
var_dump($vector->sum());
?>

위 예제의 출력 예시:

float(6.5)

User Contributed Notes

There are no user contributed notes for this page.


Converts the vector to an array.

  • ≪ Ds\Vector::sum
  • Ds\Vector::unshift ≫
  • PHP Manual
  • Vector
  • Converts the vector to an array.

Ds\Vector::toArray

(PECL ds >= 1.0.0)

Ds\Vector::toArray — Converts the vector to an array.

설명

public array Ds\Vector::toArray ( void )

Converts the vector to an array.

Note:

Casting to an array is not supported yet.

인수

이 함수는 인수가 없습니다.

반환값

An array containing all the values in the same order as the vector.

예제

Example #1 Ds\Vector::toArray() example

<?php
$vector = new \Ds\Vector([1, 2, 3]);

var_dump($vector->toArray());
?>

위 예제의 출력 예시:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Adds values to the front of the vector.

  • ≪ Ds\Vector::toArray
  • Deque ≫
  • PHP Manual
  • Vector
  • Adds values to the front of the vector.

Ds\Vector::unshift

(PECL ds >= 1.0.0)

Ds\Vector::unshift — Adds values to the front of the vector.

설명

public void Ds\Vector::unshift ([ mixed $values ] )

Adds values to the front of the vector, moving all the current values forward to make room for the new values.

인수

values

The values to add to the front of the vector.

Note:

Multiple values will be added in the same order that they are passed.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Vector::unshift() example

<?php
$vector = new \Ds\Vector([1, 2, 3]);

$vector->unshift("a");
$vector->unshift("b", "c");

print_r($vector);
?>

위 예제의 출력 예시:

Ds\Vector Object
(
    [0] => b
    [1] => c
    [2] => a
    [3] => 1
    [4] => 2
    [5] => 3
)

User Contributed Notes

There are no user contributed notes for this page.


The Deque class

(No version information available, might only be in Git)

소개

A Deque (pronounced "deck") is a sequence of values in a contiguous buffer that grows and shrinks automatically. The name is a common abbreviation of "double-ended queue" and is used internally by Ds\Queue.

Two pointers are used to keep track of a head and a tail. The pointers can "wrap around" the end of the buffer, which avoids the need to move other values around to make room. This makes shift and unshift very fast?—? something a Ds\Vector can't compete with.

Accessing a value by index requires a translation between the index and its corresponding position in the buffer: ((head + position) % capacity).

Strengths

  • Supports array syntax (square brackets).
  • Uses less overall memory than an array for the same number of values.
  • Automatically frees allocated memory when its size drops low enough.
  • get(),
set(),
push(),
pop(),
shift(), and
unshift() are all O(1).

Weaknesses

  • Capacity must be a power of 2.
  • insert() and remove() are O(n).

클래스 개요

Ds\Deque implements Ds\Sequence {
/* Constants */
const int MIN_CAPACITY = 8 ;
/* 메소드 */
public void allocate ( int $capacity )
public void apply ( callable $callback )
public int capacity ( void )
public void clear ( void )
public bool contains ([ mixed $...values ] )
public Ds\Deque copy ( void )
public Ds\Deque filter ([ callable $callback ] )
public mixed find ( mixed $value )
public mixed first ( void )
public mixed get ( int $index )
public void insert ( int $index [, mixed $...values ] )
public bool isEmpty ( void )
public string join ([ string $glue ] )
public mixed last ( void )
public Ds\Deque map ( callable $callback )
public Ds\Deque merge ( mixed $values )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public mixed reduce ( callable $callback [, mixed $initial ] )
public mixed remove ( int $index )
public void reverse ( void )
public Ds\Deque reversed ( void )
public void rotate ( int $rotations )
public void set ( int $index , mixed $value )
public mixed shift ( void )
public Ds\Deque slice ( int $index [, int $length ] )
public void sort ([ callable $comparator ] )
public Ds\Deque sorted ([ callable $comparator ] )
public number sum ( void )
public array toArray ( void )
public void unshift ([ mixed $values ] )
}

예약 상수

Ds\Deque::MIN_CAPACITY

Table of Contents

  • Ds\Deque::allocate — Allocates enough memory for a required capacity.
  • Ds\Deque::apply — Updates all values by applying a callback function to each value.
  • Ds\Deque::capacity — Returns the current capacity.
  • Ds\Deque::clear — Removes all values from the deque.
  • Ds\Deque::__construct — Creates a new instance.
  • Ds\Deque::contains — Determines if the deque contains given values.
  • Ds\Deque::copy — Returns a shallow copy of the deque.
  • Ds\Deque::count — Returns the number of values in the collection.
  • Ds\Deque::filter — Creates a new deque using a callable to determine which values to include.
  • Ds\Deque::find — Attempts to find a value's index.
  • Ds\Deque::first — Returns the first value in the deque.
  • Ds\Deque::get — Returns the value at a given index.
  • Ds\Deque::insert — Inserts values at a given index.
  • Ds\Deque::isEmpty — Returns whether the deque is empty
  • Ds\Deque::join — Joins all values together as a string.
  • Ds\Deque::jsonSerialize — Returns a representation that can be converted to JSON.
  • Ds\Deque::last — Returns the last value.
  • Ds\Deque::map — Returns the result of applying a callback to each value.
  • Ds\Deque::merge — Returns the result of adding all given values to the deque.
  • Ds\Deque::pop — Removes and returns the last value.
  • Ds\Deque::push — Adds values to the end of the deque.
  • Ds\Deque::reduce — Reduces the deque to a single value using a callback function.
  • Ds\Deque::remove — Removes and returns a value by index.
  • Ds\Deque::reverse — Reverses the deque in-place.
  • Ds\Deque::reversed — Returns a reversed copy.
  • Ds\Deque::rotate — Rotates the deque by a given number of rotations.
  • Ds\Deque::set — Updates a value at a given index.
  • Ds\Deque::shift — Removes and returns the first value.
  • Ds\Deque::slice — Returns a sub-deque of a given range.
  • Ds\Deque::sort — Sorts the deque in-place.
  • Ds\Deque::sorted — Returns a sorted copy.
  • Ds\Deque::sum — Returns the sum of all values in the deque.
  • Ds\Deque::toArray — Converts the deque to an array.
  • Ds\Deque::unshift — Adds values to the front of the deque.

User Contributed Notes

There are no user contributed notes for this page.


Allocates enough memory for a required capacity.

  • ≪ Deque
  • Ds\Deque::apply ≫
  • PHP Manual
  • Deque
  • Allocates enough memory for a required capacity.

Ds\Deque::allocate

(PECL ds >= 1.0.0)

Ds\Deque::allocate — Allocates enough memory for a required capacity.

설명

public void Ds\Deque::allocate ( int $capacity )

Ensures that enough memory is allocated for a required capacity. This removes the need to reallocate the internal as values are added.

인수

capacity

The number of values for which capacity should be allocated.

Note:

Capacity will stay the same if this value is less than or equal to the current capacity.

Note:

Capacity will always be rounded up to the nearest power of 2.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Deque::allocate() example

<?php
$deque = new \Ds\Deque();
var_dump($deque->capacity());

$deque->allocate(100);
var_dump($deque->capacity());
?>

위 예제의 출력 예시:

int(8)
int(128)

User Contributed Notes

There are no user contributed notes for this page.


Updates all values by applying a callback function to each value.

  • ≪ Ds\Deque::allocate
  • Ds\Deque::capacity ≫
  • PHP Manual
  • Deque
  • Updates all values by applying a callback function to each value.

Ds\Deque::apply

(PECL ds >= 1.0.0)

Ds\Deque::apply — Updates all values by applying a callback function to each value.

설명

public void Ds\Deque::apply ( callable $callback )

Updates all values by applying a callback function to each value in the deque.

인수

callback

mixed callback ( mixed $value )

A callable to apply to each value in the deque.

The callback should return what the value should be replaced by.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Deque::apply() example

<?php
$deque = new \Ds\Deque([1, 2, 3]);
$deque->apply(function($value) { return $value * 2; });

print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => 2
    [1] => 4
    [2] => 6
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the current capacity.

  • ≪ Ds\Deque::apply
  • Ds\Deque::clear ≫
  • PHP Manual
  • Deque
  • Returns the current capacity.

Ds\Deque::capacity

(PECL ds >= 1.0.0)

Ds\Deque::capacity — Returns the current capacity.

설명

public int Ds\Deque::capacity ( void )

Returns the current capacity.

인수

이 함수는 인수가 없습니다.

반환값

The current capacity.

예제

Example #1 Ds\Deque::capacity() example

<?php
$deque = new \Ds\Deque();
var_dump($deque->capacity());

$deque->push(...range(1, 50));
var_dump($deque->capacity());
?>

위 예제의 출력 예시:

int(8)
int(64)

User Contributed Notes

There are no user contributed notes for this page.


Removes all values from the deque.

  • ≪ Ds\Deque::capacity
  • Ds\Deque::__construct ≫
  • PHP Manual
  • Deque
  • Removes all values from the deque.

Ds\Deque::clear

(PECL ds >= 1.0.0)

Ds\Deque::clear — Removes all values from the deque.

설명

public void Ds\Deque::clear ( void )

Removes all values from the deque.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Deque::clear() example

<?php
$deque = new \Ds\Deque([1, 2, 3]);
print_r($deque);

$deque->clear();
print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Deque Object
(
)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new instance.

  • ≪ Ds\Deque::clear
  • Ds\Deque::contains ≫
  • PHP Manual
  • Deque
  • Creates a new instance.

Ds\Deque::__construct

(PECL ds >= 1.0.0)

Ds\Deque::__construct — Creates a new instance.

설명

public Ds\Deque::__construct ([ mixed $values ] )

Creates a new instance, using either a traversable object or an array for the initial values.

인수

values

A traversable object or an array to use for the initial values.

예제

Example #1 Ds\Deque::__construct() example

<?php
$deque = new \Ds\Deque();
var_dump($deque);

$deque = new \Ds\Deque([1, 2, 3]);
var_dump($deque);
?>

위 예제의 출력 예시:

object(Ds\Deque)#2 (0) {
}
object(Ds\Deque)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Determines if the deque contains given values.

  • ≪ Ds\Deque::__construct
  • Ds\Deque::copy ≫
  • PHP Manual
  • Deque
  • Determines if the deque contains given values.

Ds\Deque::contains

(PECL ds >= 1.0.0)

Ds\Deque::contains — Determines if the deque contains given values.

설명

public bool Ds\Deque::contains ([ mixed $...values ] )

Determines if the deque contains all values.

인수

values

Values to check.

반환값

FALSE if any of the provided values are not in the deque, TRUE otherwise.

예제

Example #1 Ds\Deque::contains() example

<?php
$deque = new \Ds\Deque(['a', 'b', 'c', 1, 2, 3]);

var_dump($deque->contains('a'));                // true
var_dump($deque->contains('a', 'b'));           // true
var_dump($deque->contains('c', 'd'));           // false

var_dump($deque->contains(...['c', 'b', 'a'])); // true

// Always strict
var_dump($deque->contains(1));                  // true
var_dump($deque->contains('1'));                // false

var_dump($sequece->contains(...[]));               // true
?>

위 예제의 출력 예시:

bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Returns a shallow copy of the deque.

  • ≪ Ds\Deque::contains
  • Ds\Deque::count ≫
  • PHP Manual
  • Deque
  • Returns a shallow copy of the deque.

Ds\Deque::copy

(PECL ds >= 1.0.0)

Ds\Deque::copy — Returns a shallow copy of the deque.

설명

public Ds\Deque Ds\Deque::copy ( void )

Returns a shallow copy of the deque.

인수

이 함수는 인수가 없습니다.

반환값

A shallow copy of the deque.

예제

Example #1 Ds\Deque::copy() example

<?php
$a = new \Ds\Deque([1, 2, 3]);
$b = $a->copy();

$b->push(4);

print_r($a);
print_r($b);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of values in the collection.

  • ≪ Ds\Deque::copy
  • Ds\Deque::filter ≫
  • PHP Manual
  • Deque
  • Returns the number of values in the collection.

Ds\Deque::count

(PECL ds >= 1.0.0)

Ds\Deque::count — Returns the number of values in the collection.

See Countable::count()

User Contributed Notes

There are no user contributed notes for this page.


Creates a new deque using a callable to determine which values to include.

  • ≪ Ds\Deque::count
  • Ds\Deque::find ≫
  • PHP Manual
  • Deque
  • Creates a new deque using a callable to determine which values to include.

Ds\Deque::filter

(PECL ds >= 1.0.0)

Ds\Deque::filter — Creates a new deque using a callable to determine which values to include.

설명

public Ds\Deque Ds\Deque::filter ([ callable $callback ] )

Creates a new deque using a callable to determine which values to include.

인수

callback

bool callback ( mixed $value )

Optional callable which returns TRUE if the value should be included, FALSE otherwise.

If a callback is not provided, only values which are TRUE (see converting to boolean) will be included.

반환값

A new deque containing all the values for which either the callback returned TRUE, or all values that convert to TRUE if a callback was not provided.

예제

Example #1 Ds\Deque::filter() example using callback function

<?php
$deque = new \Ds\Deque([1, 2, 3, 4, 5]);

var_dump($deque->filter(function($value) {
    return $value % 2 == 0;
}));
?>

위 예제의 출력 예시:

object(Ds\Deque)#3 (2) {
  [0]=>
  int(2)
  [1]=>
  int(4)
}

Example #2 Ds\Deque::filter() example without a callback function

<?php
$deque = new \Ds\Deque([0, 1, 'a', true, false]);

var_dump($deque->filter());
?>

위 예제의 출력 예시:

object(Ds\Deque)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  string(1) "a"
  [2]=>
  bool(true)
}

User Contributed Notes

There are no user contributed notes for this page.


Attempts to find a value's index.

  • ≪ Ds\Deque::filter
  • Ds\Deque::first ≫
  • PHP Manual
  • Deque
  • Attempts to find a value's index.

Ds\Deque::find

(PECL ds >= 1.0.0)

Ds\Deque::find — Attempts to find a value's index.

설명

public mixed Ds\Deque::find ( mixed $value )

Returns the index of the value, or FALSE if not found.

인수

value

The value to find.

반환값

The index of the value, or FALSE if not found.

Note:

Values will be compared by value and by type.

예제

Example #1 Ds\Deque::find() example

<?php
$deque = new \Ds\Deque(["a", 1, true]);

var_dump($deque->find("a")); // 0
var_dump($deque->find("b")); // false
var_dump($deque->find("1")); // false
var_dump($deque->find(1));   // 1
?>

위 예제의 출력 예시:

int(0)
bool(false)
bool(false)
int(1)

User Contributed Notes

There are no user contributed notes for this page.


Returns the first value in the deque.

  • ≪ Ds\Deque::find
  • Ds\Deque::get ≫
  • PHP Manual
  • Deque
  • Returns the first value in the deque.

Ds\Deque::first

(PECL ds >= 1.0.0)

Ds\Deque::first — Returns the first value in the deque.

설명

public mixed Ds\Deque::first ( void )

Returns the first value in the deque.

인수

이 함수는 인수가 없습니다.

반환값

The first value in the deque.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Deque::first() example

<?php
$deque = new \Ds\Deque([1, 2, 3]);
var_dump($deque->first());
?>

위 예제의 출력 예시:

int(1)

User Contributed Notes

There are no user contributed notes for this page.


Returns the value at a given index.

  • ≪ Ds\Deque::first
  • Ds\Deque::insert ≫
  • PHP Manual
  • Deque
  • Returns the value at a given index.

Ds\Deque::get

(PECL ds >= 1.0.0)

Ds\Deque::get — Returns the value at a given index.

설명

public mixed Ds\Deque::get ( int $index )

Returns the value at a given index.

인수

index

The index to access, starting at 0.

반환값

The value at the requested index.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Deque::get() example

<?php
$deque = new \Ds\Deque(["a", "b", "c"]);

var_dump($deque->get(0));
var_dump($deque->get(1));
var_dump($deque->get(2));
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

Example #2 Ds\Deque::get() example using array syntax

<?php
$deque = new \Ds\Deque(["a", "b", "c"]);

var_dump($deque[0]);
var_dump($deque[1]);
var_dump($deque[2]);
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Inserts values at a given index.

  • ≪ Ds\Deque::get
  • Ds\Deque::isEmpty ≫
  • PHP Manual
  • Deque
  • Inserts values at a given index.

Ds\Deque::insert

(PECL ds >= 1.0.0)

Ds\Deque::insert — Inserts values at a given index.

설명

public void Ds\Deque::insert ( int $index [, mixed $...values ] )

Inserts values into the deque at a given index.

인수

index

The index at which to insert. 0 <= index <= count

Note:

You can insert at the index equal to the number of values.

values

The value or values to insert.

반환값

값을 반환하지 않습니다.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Deque::insert() example

<?php
$deque = new \Ds\Deque();

$deque->insert(0, "e");             // [e]
$deque->insert(1, "f");             // [e, f]
$deque->insert(2, "g");             // [e, f, g]
$deque->insert(0, "a", "b");        // [a, b, e, f, g]
$deque->insert(2, ...["c", "d"]);   // [a, b, c, d, e, f, g]

var_dump($deque);
?>

위 예제의 출력 예시:

object(Ds\Deque)#1 (7) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
  [6]=>
  string(1) "g"
}

User Contributed Notes

There are no user contributed notes for this page.


Returns whether the deque is empty

  • ≪ Ds\Deque::insert
  • Ds\Deque::join ≫
  • PHP Manual
  • Deque
  • Returns whether the deque is empty

Ds\Deque::isEmpty

(PECL ds >= 1.0.0)

Ds\Deque::isEmpty — Returns whether the deque is empty

설명

public bool Ds\Deque::isEmpty ( void )

Returns whether the deque is empty.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the deque is empty, FALSE otherwise.

예제

Example #1 Ds\Deque::isEmpty() example

<?php
$a = new \Ds\Deque([1, 2, 3]);
$b = new \Ds\Deque();

var_dump($a->isEmpty());
var_dump($b->isEmpty());
?>

위 예제의 출력 예시:

bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Joins all values together as a string.

  • ≪ Ds\Deque::isEmpty
  • Ds\Deque::jsonSerialize ≫
  • PHP Manual
  • Deque
  • Joins all values together as a string.

Ds\Deque::join

(PECL ds >= 1.0.0)

Ds\Deque::join — Joins all values together as a string.

설명

public string Ds\Deque::join ([ string $glue ] )

Joins all values together as a string using an optional separator between each value.

인수

glue

An optional string to separate each value.

반환값

All values of the deque joined together as a string.

예제

Example #1 Ds\Deque::join() example using a separator string

<?php
$deque = new \Ds\Deque(["a", "b", "c", 1, 2, 3]);

var_dump($deque->join("|"));
?>

위 예제의 출력 예시:

string(11) "a|b|c|1|2|3"

Example #2 Ds\Deque::join() example without a separator string

<?php
$deque = new \Ds\Deque(["a", "b", "c", 1, 2, 3]);

var_dump($deque->join());
?>

위 예제의 출력 예시:

string(11) "abc123"

User Contributed Notes

There are no user contributed notes for this page.


Returns a representation that can be converted to JSON.

  • ≪ Ds\Deque::join
  • Ds\Deque::last ≫
  • PHP Manual
  • Deque
  • Returns a representation that can be converted to JSON.

Ds\Deque::jsonSerialize

(PECL ds >= 1.0.0)

Ds\Deque::jsonSerialize — Returns a representation that can be converted to JSON.

See JsonSerializable::jsonSerialize()

Note:

You should never need to call this directly.

User Contributed Notes

There are no user contributed notes for this page.


Returns the last value.

  • ≪ Ds\Deque::jsonSerialize
  • Ds\Deque::map ≫
  • PHP Manual
  • Deque
  • Returns the last value.

Ds\Deque::last

(PECL ds >= 1.0.0)

Ds\Deque::last — Returns the last value.

설명

public mixed Ds\Deque::last ( void )

Returns the last value in the deque.

인수

이 함수는 인수가 없습니다.

반환값

The last value in the deque.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Deque::last() example

<?php
$deque = new \Ds\Deque([1, 2, 3]);
var_dump($deque->last());
?>

위 예제의 출력 예시:

int(3)

User Contributed Notes

There are no user contributed notes for this page.


Returns the result of applying a callback to each value.

  • ≪ Ds\Deque::last
  • Ds\Deque::merge ≫
  • PHP Manual
  • Deque
  • Returns the result of applying a callback to each value.

Ds\Deque::map

(PECL ds >= 1.0.0)

Ds\Deque::map — Returns the result of applying a callback to each value.

설명

public Ds\Deque Ds\Deque::map ( callable $callback )

Returns the result of applying a callback function to each value in the deque.

인수

callback

mixed callback ( mixed $value )

A callable to apply to each value in the deque.

The callable should return what the new value will be in the new deque.

반환값

The result of applying a callback to each value in the deque.

Note:

The values of the current instance won't be affected.

예제

Example #1 Ds\Deque::map() example

<?php
$deque = new \Ds\Deque([1, 2, 3]);

print_r($deque->map(function($value) { return $value * 2; }));
print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => 2
    [1] => 4
    [2] => 6
)
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the result of adding all given values to the deque.

  • ≪ Ds\Deque::map
  • Ds\Deque::pop ≫
  • PHP Manual
  • Deque
  • Returns the result of adding all given values to the deque.

Ds\Deque::merge

(PECL ds >= 1.0.0)

Ds\Deque::merge — Returns the result of adding all given values to the deque.

설명

public Ds\Deque Ds\Deque::merge ( mixed $values )

Returns the result of adding all given values to the deque.

인수

values

A traversable object or an array.

반환값

The result of adding all given values to the deque, effectively the same as adding the values to a copy, then returning that copy.

Note:

The current instance won't be affected.

예제

Example #1 Ds\Deque::merge() example

<?php
$deque = new \Ds\Deque([1, 2, 3]);

var_dump($deque->merge([4, 5, 6]));
var_dump($deque);
?>

위 예제의 출력 예시:

object(Ds\Deque)#2 (6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
}
object(Ds\Deque)#1 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns the last value.

  • ≪ Ds\Deque::merge
  • Ds\Deque::push ≫
  • PHP Manual
  • Deque
  • Removes and returns the last value.

Ds\Deque::pop

(PECL ds >= 1.0.0)

Ds\Deque::pop — Removes and returns the last value.

설명

public mixed Ds\Deque::pop ( void )

Removes and returns the last value.

인수

이 함수는 인수가 없습니다.

반환값

The removed last value.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Deque::pop() example

<?php
$deque = new \Ds\Deque([1, 2, 3]);

var_dump($deque->pop());
var_dump($deque->pop());
var_dump($deque->pop());
?>

위 예제의 출력 예시:

int(3)
int(2)
int(1)

User Contributed Notes

There are no user contributed notes for this page.


Adds values to the end of the deque.

  • ≪ Ds\Deque::pop
  • Ds\Deque::reduce ≫
  • PHP Manual
  • Deque
  • Adds values to the end of the deque.

Ds\Deque::push

(PECL ds >= 1.0.0)

Ds\Deque::push — Adds values to the end of the deque.

설명

public void Ds\Deque::push ([ mixed $...values ] )

Adds values to the end of the deque.

인수

values

The values to add.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Deque::push() example

<?php
$deque = new \Ds\Deque();

$deque->push("a");
$deque->push("b");
$deque->push("c", "d");
$deque->push(...["e", "f"]);

print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)

User Contributed Notes

There are no user contributed notes for this page.


Reduces the deque to a single value using a callback function.

  • ≪ Ds\Deque::push
  • Ds\Deque::remove ≫
  • PHP Manual
  • Deque
  • Reduces the deque to a single value using a callback function.

Ds\Deque::reduce

(PECL ds >= 1.0.0)

Ds\Deque::reduce — Reduces the deque to a single value using a callback function.

설명

public mixed Ds\Deque::reduce ( callable $callback [, mixed $initial ] )

Reduces the deque to a single value using a callback function.

인수

callback
mixed callback ( mixed $carry , mixed $value )
carry

The return value of the previous callback, or initial if it's the first iteration.

value

The value of the current iteration.

initial

The initial value of the carry value. Can be NULL.

반환값

The return value of the final callback.

예제

Example #1 Ds\Deque::reduce() with initial value example

<?php
$deque = new \Ds\Deque([1, 2, 3]);

$callback = function($carry, $value) {
    return $carry * $value;
};

var_dump($deque->reduce($callback, 5));

// Iterations:
//
// $carry = $initial = 5
//
// $carry = $carry * 1 =  5
// $carry = $carry * 2 = 10
// $carry = $carry * 3 = 30
?>

위 예제의 출력 예시:

int(30)

Example #2 Ds\Deque::reduce() without an initial value example

<?php
$deque = new \Ds\Deque([1, 2, 3]);

var_dump($deque->reduce(function($carry, $value) {
    return $carry + $value + 5;
}));

// Iterations:
//
// $carry = $initial = null
//
// $carry = $carry + 1 + 5 =  6
// $carry = $carry + 2 + 5 = 13
// $carry = $carry + 3 + 5 = 21
?>

위 예제의 출력 예시:

int(21)

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns a value by index.

  • ≪ Ds\Deque::reduce
  • Ds\Deque::reverse ≫
  • PHP Manual
  • Deque
  • Removes and returns a value by index.

Ds\Deque::remove

(PECL ds >= 1.0.0)

Ds\Deque::remove — Removes and returns a value by index.

설명

public mixed Ds\Deque::remove ( int $index )

Removes and returns a value by index.

인수

index

The index of the value to remove.

반환값

The value that was removed.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Deque::remove() example

<?php
$deque = new \Ds\Deque(["a", "b", "c"]);

var_dump($deque->remove(1));
var_dump($deque->remove(0));
var_dump($deque->remove(0));
?>

위 예제의 출력 예시:

string(1) "b"
string(1) "a"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Reverses the deque in-place.

  • ≪ Ds\Deque::remove
  • Ds\Deque::reversed ≫
  • PHP Manual
  • Deque
  • Reverses the deque in-place.

Ds\Deque::reverse

(PECL ds >= 1.0.0)

Ds\Deque::reverse — Reverses the deque in-place.

설명

public void Ds\Deque::reverse ( void )

Reverses the deque in-place.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Deque::reverse() example

<?php
$deque = new \Ds\Deque(["a", "b", "c"]);
$deque->reverse();

print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => c
    [1] => b
    [2] => a
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a reversed copy.

  • ≪ Ds\Deque::reverse
  • Ds\Deque::rotate ≫
  • PHP Manual
  • Deque
  • Returns a reversed copy.

Ds\Deque::reversed

(PECL ds >= 1.0.0)

Ds\Deque::reversed — Returns a reversed copy.

설명

public Ds\Deque Ds\Deque::reversed ( void )

Returns a reversed copy of the deque.

인수

이 함수는 인수가 없습니다.

반환값

A reversed copy of the deque.

Note:

The current instance is not affected.

예제

Example #1 Ds\Deque::reversed() example

<?php
$deque = new \Ds\Deque(["a", "b", "c"]);

print_r($deque->reversed());
print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => c
    [1] => b
    [2] => a
)
Ds\Deque Object
(
    [0] => a
    [1] => b
    [2] => c
)

User Contributed Notes

There are no user contributed notes for this page.


Rotates the deque by a given number of rotations.

  • ≪ Ds\Deque::reversed
  • Ds\Deque::set ≫
  • PHP Manual
  • Deque
  • Rotates the deque by a given number of rotations.

Ds\Deque::rotate

(PECL ds >= 1.0.0)

Ds\Deque::rotate — Rotates the deque by a given number of rotations.

설명

public void Ds\Deque::rotate ( int $rotations )

Rotates the deque by a given number of rotations, which is equivalent to successively calling $deque->push($deque->shift()) if the number of rotations is positive, or $deque->unshift($deque->pop()) if negative.

인수

rotations

The number of times the deque should be rotated.

반환값

값을 반환하지 않습니다.. The deque of the current instance will be rotated.

예제

Example #1 Ds\Deque::rotate() example

<?php
$deque = new \Ds\Deque(["a", "b", "c", "d"]);

$deque->rotate(1);  // "a" is shifted, then pushed.
print_r($deque);

$deque->rotate(2);  // "b" and "c" are both shifted, the pushed.
print_r($deque);
?>

위 예제의 출력 예시:

(
    [0] => b
    [1] => c
    [2] => d
    [3] => a
)
Ds\Deque Object
(
    [0] => d
    [1] => a
    [2] => b
    [3] => c
)

User Contributed Notes

There are no user contributed notes for this page.


Updates a value at a given index.

  • ≪ Ds\Deque::rotate
  • Ds\Deque::shift ≫
  • PHP Manual
  • Deque
  • Updates a value at a given index.

Ds\Deque::set

(PECL ds >= 1.0.0)

Ds\Deque::set — Updates a value at a given index.

설명

public void Ds\Deque::set ( int $index , mixed $value )

Updates a value at a given index.

인수

index

The index of the value to update.

value

The new value.

반환값

값을 반환하지 않습니다.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Deque::set() example

<?php
$deque = new \Ds\Deque(["a", "b", "c"]);

$deque->set(1, "_");
print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => a
    [1] => _
    [2] => c
)

Example #2 Ds\Deque::set() example using array syntax

<?php
$deque = new \Ds\Deque(["a", "b", "c"]);

$deque[1] = "_";
print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => a
    [1] => _
    [2] => c
)

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns the first value.

  • ≪ Ds\Deque::set
  • Ds\Deque::slice ≫
  • PHP Manual
  • Deque
  • Removes and returns the first value.

Ds\Deque::shift

(PECL ds >= 1.0.0)

Ds\Deque::shift — Removes and returns the first value.

설명

public mixed Ds\Deque::shift ( void )

Removes and returns the first value.

인수

이 함수는 인수가 없습니다.

반환값

The first value, which was removed.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Deque::shift() example

<?php
$deque = new \Ds\Deque(["a", "b", "c"]);

var_dump($deque->shift());
var_dump($deque->shift());
var_dump($deque->shift());
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Returns a sub-deque of a given range.

  • ≪ Ds\Deque::shift
  • Ds\Deque::sort ≫
  • PHP Manual
  • Deque
  • Returns a sub-deque of a given range.

Ds\Deque::slice

(PECL ds >= 1.0.0)

Ds\Deque::slice — Returns a sub-deque of a given range.

설명

public Ds\Deque Ds\Deque::slice ( int $index [, int $length ] )

Creates a sub-deque of a given range.

인수

index

The index at which the sub-deque starts.

If positive, the deque will start at that index in the deque. If negative, the deque will start that far from the end.

length

If a length is given and is positive, the resulting deque will have up to that many values in it. If the length results in an overflow, only values up to the end of the deque will be included. If a length is given and is negative, the deque will stop that many values from the end. If a length is not provided, the resulting deque will contain all values between the index and the end of the deque.

반환값

A sub-deque of the given range.

예제

Example #1 Ds\Deque::slice() example

<?php
$deque = new \Ds\Deque(["a", "b", "c", "d", "e"]);

// Slice from 2 onwards
print_r($deque->slice(2));

// Slice from 1, for a length of 3
print_r($deque->slice(1, 3));

// Slice from 1 onwards
print_r($deque->slice(1));

// Slice from 2 from the end onwards
print_r($deque->slice(-2));

// Slice from 1 to 1 from the end
print_r($deque->slice(1, -1));
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => c
    [1] => d
    [2] => e
)
Ds\Deque Object
(
    [0] => b
    [1] => c
    [2] => d
)
Ds\Deque Object
(
    [0] => b
    [1] => c
    [2] => d
    [3] => e
)
Ds\Deque Object
(
    [0] => d
    [1] => e
)
Ds\Deque Object
(
    [0] => b
    [1] => c
    [2] => d
)

User Contributed Notes

There are no user contributed notes for this page.


Sorts the deque in-place.

  • ≪ Ds\Deque::slice
  • Ds\Deque::sorted ≫
  • PHP Manual
  • Deque
  • Sorts the deque in-place.

Ds\Deque::sort

(PECL ds >= 1.0.0)

Ds\Deque::sort — Sorts the deque in-place.

설명

public void Ds\Deque::sort ([ callable $comparator ] )

Sorts the deque in-place, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Deque::sort() example

<?php
$deque = new \Ds\Deque([4, 5, 1, 3, 2]);
$deque->sort();

print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example #2 Ds\Deque::sort() example using a comparator

<?php
$deque = new \Ds\Deque([4, 5, 1, 3, 2]);

$deque->sort(function($a, $b) {
    return $b <=> $a;
});

print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a sorted copy.

  • ≪ Ds\Deque::sort
  • Ds\Deque::sum ≫
  • PHP Manual
  • Deque
  • Returns a sorted copy.

Ds\Deque::sorted

(PECL ds >= 1.0.0)

Ds\Deque::sorted — Returns a sorted copy.

설명

public Ds\Deque Ds\Deque::sorted ([ callable $comparator ] )

Returns a sorted copy, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

Returns a sorted copy of the deque.

예제

Example #1 Ds\Deque::sorted() example

<?php
$deque = new \Ds\Deque([4, 5, 1, 3, 2]);

print_r($deque->sorted());
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example #2 Ds\Deque::sorted() example using a comparator

<?php
$deque = new \Ds\Deque([4, 5, 1, 3, 2]);

$sorted = $deque->sorted(function($a, $b) {
    return $b <=> $a;
});

print_r($sorted);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the sum of all values in the deque.

  • ≪ Ds\Deque::sorted
  • Ds\Deque::toArray ≫
  • PHP Manual
  • Deque
  • Returns the sum of all values in the deque.

Ds\Deque::sum

(PECL ds >= 1.0.0)

Ds\Deque::sum — Returns the sum of all values in the deque.

설명

public number Ds\Deque::sum ( void )

Returns the sum of all values in the deque.

Note:

Arrays and objects are considered equal to zero when calculating the sum.

인수

이 함수는 인수가 없습니다.

반환값

The sum of all the values in the deque as either a float or int depending on the values in the deque.

예제

Example #1 Ds\Deque::sum() integer example

<?php
$deque = new \Ds\Deque([1, 2, 3]);
var_dump($deque->sum());
?>

위 예제의 출력 예시:

int(6)

Example #2 Ds\Deque::sum() float example

<?php
$deque = new \Ds\Deque([1, 2.5, 3]);
var_dump($deque->sum());
?>

위 예제의 출력 예시:

float(6.5)

User Contributed Notes

There are no user contributed notes for this page.


Converts the deque to an array.

  • ≪ Ds\Deque::sum
  • Ds\Deque::unshift ≫
  • PHP Manual
  • Deque
  • Converts the deque to an array.

Ds\Deque::toArray

(PECL ds >= 1.0.0)

Ds\Deque::toArray — Converts the deque to an array.

설명

public array Ds\Deque::toArray ( void )

Converts the deque to an array.

Note:

Casting to an array is not supported yet.

인수

이 함수는 인수가 없습니다.

반환값

An array containing all the values in the same order as the deque.

예제

Example #1 Ds\Deque::toArray() example

<?php
$deque = new \Ds\Deque([1, 2, 3]);

var_dump($deque->toArray());
?>

위 예제의 출력 예시:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Adds values to the front of the deque.

  • ≪ Ds\Deque::toArray
  • Map ≫
  • PHP Manual
  • Deque
  • Adds values to the front of the deque.

Ds\Deque::unshift

(PECL ds >= 1.0.0)

Ds\Deque::unshift — Adds values to the front of the deque.

설명

public void Ds\Deque::unshift ([ mixed $values ] )

Adds values to the front of the deque, moving all the current values forward to make room for the new values.

인수

values

The values to add to the front of the deque.

Note:

Multiple values will be added in the same order that they are passed.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Deque::unshift() example

<?php
$deque = new \Ds\Deque([1, 2, 3]);

$deque->unshift("a");
$deque->unshift("b", "c");

print_r($deque);
?>

위 예제의 출력 예시:

Ds\Deque Object
(
    [0] => b
    [1] => c
    [2] => a
    [3] => 1
    [4] => 2
    [5] => 3
)

User Contributed Notes

There are no user contributed notes for this page.


The Map class

(No version information available, might only be in Git)

소개

A Map is a sequential collection of key-value pairs, almost identical to an array used in a similar context. Keys can be any type, but must be unique. Values are replaced if added to the map using the same key.

Strengths

  • Keys and values can be any type, including objects.
  • Supports array syntax (square brackets).
  • Insertion order is preserved.
  • Performance and memory efficiency is very similar to an array.
  • Automatically frees allocated memory when its size drops low enough.

Weaknesses

  • Can't be converted to an array when objects are used as keys.

클래스 개요

Ds\Map implements Ds\Collection {
/* Constants */
const int MIN_CAPACITY = 16 ;
/* 메소드 */
public void allocate ( int $capacity )
public void apply ( callable $callback )
public int capacity ( void )
public void clear ( void )
public Ds\Map copy ( void )
public Ds\Map diff ( Ds\Map $map )
public Ds\Map filter ([ callable $callback ] )
public Ds\Pair first ( void )
public mixed get ( mixed $key [, mixed $default ] )
public bool hasKey ( mixed $key )
public bool hasValue ( mixed $value )
public Ds\Map intersect ( Ds\Map $map )
public bool isEmpty ( void )
public Ds\Set keys ( void )
public void ksort ([ callable $comparator ] )
public Ds\Map ksorted ([ callable $comparator ] )
public Ds\Pair last ( void )
public Ds\Map map ( callable $callback )
public Ds\Map merge ( mixed $values )
public Ds\Sequence pairs ( void )
public void put ( mixed $key , mixed $value )
public void putAll ( mixed $pairs )
public mixed reduce ( callable $callback [, mixed $initial ] )
public mixed remove ( mixed $key [, mixed $default ] )
public void reverse ( void )
public Ds\Map reversed ( void )
public Ds\Pair skip ( int $position )
public Ds\Map slice ( int $index [, int $length ] )
public void sort ([ callable $comparator ] )
public Ds\Map sorted ([ callable $comparator ] )
public number sum ( void )
public array toArray ( void )
public Ds\Map union ( Ds\Map $map )
public Ds\Sequence values ( void )
public Ds\Map xor ( Ds\Map $map )
}

예약 상수

Ds\Map::MIN_CAPACITY

Table of Contents

  • Ds\Map::allocate — Allocates enough memory for a required capacity.
  • Ds\Map::apply — Updates all values by applying a callback function to each value.
  • Ds\Map::capacity — Returns the current capacity.
  • Ds\Map::clear — Removes all values.
  • Ds\Map::__construct — Creates a new instance.
  • Ds\Map::copy — Returns a shallow copy of the map.
  • Ds\Map::count — Returns the number of values in the map.
  • Ds\Map::diff — Creates a new map using keys that aren't in another map.
  • Ds\Map::filter — Creates a new map using a callable to determine which pairs to include.
  • Ds\Map::first — Returns the first pair in the map.
  • Ds\Map::get — Returns the value for a given key.
  • Ds\Map::hasKey — Determines whether the map contains a given key.
  • Ds\Map::hasValue — Determines whether the map contains a given value.
  • Ds\Map::intersect — Creates a new map by intersecting keys with another map.
  • Ds\Map::isEmpty — Returns whether the map is empty
  • Ds\Map::jsonSerialize — Returns a representation that can be converted to JSON.
  • Ds\Map::keys — Returns a set of the map's keys.
  • Ds\Map::ksort — Sorts the map in-place by key.
  • Ds\Map::ksorted — Returns a copy, sorted by key.
  • Ds\Map::last — Returns the last pair of the map.
  • Ds\Map::map — Returns the result of applying a callback to each value.
  • Ds\Map::merge — Returns the result of adding all given associations.
  • Ds\Map::pairs — Returns a sequence containing all the pairs of the map.
  • Ds\Map::put — Associates a key with a value.
  • Ds\Map::putAll — Associates all key-value pairs of a traversable object or array.
  • Ds\Map::reduce — Reduces the map to a single value using a callback function.
  • Ds\Map::remove — Removes and returns a value by key.
  • Ds\Map::reverse — Reverses the map in-place.
  • Ds\Map::reversed — Returns a reversed copy.
  • Ds\Map::skip — Returns the pair at a given positional index.
  • Ds\Map::slice — Returns a subset of the map defined by a starting index and length.
  • Ds\Map::sort — Sorts the map in-place by value.
  • Ds\Map::sorted — Returns a copy, sorted by value.
  • Ds\Map::sum — Returns the sum of all values in the map.
  • Ds\Map::toArray — Converts the map to an array.
  • Ds\Map::union — Creates a new map using values from the current instance and another map.
  • Ds\Map::values — Returns a sequence of the map's values.
  • Ds\Map::xor — Creates a new map using keys of either the current instance or of another map, but not of both.

User Contributed Notes

There are no user contributed notes for this page.


Allocates enough memory for a required capacity.

  • ≪ Map
  • Ds\Map::apply ≫
  • PHP Manual
  • Map
  • Allocates enough memory for a required capacity.

Ds\Map::allocate

(PECL ds >= 1.0.0)

Ds\Map::allocate — Allocates enough memory for a required capacity.

설명

public void Ds\Map::allocate ( int $capacity )

Allocates enough memory for a required capacity.

인수

capacity

The number of values for which capacity should be allocated.

Note:

Capacity will stay the same if this value is less than or equal to the current capacity.

Note:

Capacity will always be rounded up to the nearest power of 2.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Map::allocate() example

<?php
$map = new \Ds\Map();
var_dump($map->capacity());

$map->allocate(100);
var_dump($map->capacity());
?>

위 예제의 출력 예시:

int(16)
int(128)

User Contributed Notes

There are no user contributed notes for this page.


Updates all values by applying a callback function to each value.

  • ≪ Ds\Map::allocate
  • Ds\Map::capacity ≫
  • PHP Manual
  • Map
  • Updates all values by applying a callback function to each value.

Ds\Map::apply

(PECL ds >= 1.0.0)

Ds\Map::apply — Updates all values by applying a callback function to each value.

설명

public void Ds\Map::apply ( callable $callback )

Updates all values by applying a callback function to each value in the map.

인수

callback

mixed callback ( mixed $key , mixed $value )

A callable to apply to each value in the map.

The callback should return what the value should be replaced by.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Map::apply() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
$map->apply(function($key, $value) { return $value * 2; });

print_r($map);
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 2
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 4
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 6
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns the current capacity.

  • ≪ Ds\Map::apply
  • Ds\Map::clear ≫
  • PHP Manual
  • Map
  • Returns the current capacity.

Ds\Map::capacity

(PECL ds >= 1.0.0)

Ds\Map::capacity — Returns the current capacity.

설명

public int Ds\Map::capacity ( void )

Returns the current capacity.

인수

이 함수는 인수가 없습니다.

반환값

The current capacity.

예제

Example #1 Ds\Map::capacity() example

<?php
$map = new \Ds\Map();
var_dump($map->capacity());
?>

위 예제의 출력 예시:

int(16)

User Contributed Notes

There are no user contributed notes for this page.


Removes all values.

  • ≪ Ds\Map::capacity
  • Ds\Map::__construct ≫
  • PHP Manual
  • Map
  • Removes all values.

Ds\Map::clear

(PECL ds >= 1.0.0)

Ds\Map::clear — Removes all values.

설명

public void Ds\Map::clear ( void )

Removes all values from the map.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Map::clear() example

<?php
$map = new \Ds\Map([
    "a" => 1,
    "b" => 2,
    "c" => 3,
]);
print_r($map);

$map->clear();
print_r($map);
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

)
Ds\Map Object
(
)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new instance.

  • ≪ Ds\Map::clear
  • Ds\Map::copy ≫
  • PHP Manual
  • Map
  • Creates a new instance.

Ds\Map::__construct

(PECL ds >= 1.0.0)

Ds\Map::__construct — Creates a new instance.

설명

public Ds\Map::__construct ([ mixed $...values ] )

Creates a new instance, using either a traversable object or an array for the initial values.

인수

values

A traversable object or an array to use for the initial values.

예제

Example #1 Ds\Map::__construct() example

<?php
$map = new \Ds\Map();
var_dump($map);

$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
var_dump($map);
?>

위 예제의 출력 예시:

object(Ds\Map)#1 (0) {
}
object(Ds\Map)#2 (3) {
  [0]=>
  object(Ds\Pair)#1 (2) {
    ["key"]=>
    string(1) "a"
    ["value"]=>
    int(1)
  }
  [1]=>
  object(Ds\Pair)#3 (2) {
    ["key"]=>
    string(1) "b"
    ["value"]=>
    int(2)
  }
  [2]=>
  object(Ds\Pair)#4 (2) {
    ["key"]=>
    string(1) "c"
    ["value"]=>
    int(3)
  }
}

User Contributed Notes

There are no user contributed notes for this page.


Returns a shallow copy of the map.

  • ≪ Ds\Map::__construct
  • Ds\Map::count ≫
  • PHP Manual
  • Map
  • Returns a shallow copy of the map.

Ds\Map::copy

(PECL ds >= 1.0.0)

Ds\Map::copy — Returns a shallow copy of the map.

설명

public Ds\Map Ds\Map::copy ( void )

Returns a shallow copy of the map.

인수

이 함수는 인수가 없습니다.

반환값

Returns a shallow copy of the map.

예제

Example #1 Ds\Map::copy() example

<?php
$map = new \Ds\Map([
    "a" => 1,
    "b" => 2,
    "c" => 3,
]);

print_r($map->copy());
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of values in the map.

  • ≪ Ds\Map::copy
  • Ds\Map::diff ≫
  • PHP Manual
  • Map
  • Returns the number of values in the map.

Ds\Map::count

(PECL ds >= 1.0.0)

Ds\Map::count — Returns the number of values in the map.

See Countable::count()

User Contributed Notes

There are no user contributed notes for this page.


Creates a new map using keys that aren't in another map.

  • ≪ Ds\Map::count
  • Ds\Map::filter ≫
  • PHP Manual
  • Map
  • Creates a new map using keys that aren't in another map.

Ds\Map::diff

(PECL ds >= 1.0.0)

Ds\Map::diff — Creates a new map using keys that aren't in another map.

설명

public Ds\Map Ds\Map::diff ( Ds\Map $map )

Returns the result of removing all keys from the current instance that are present in a given map.

A \ B = {x ∈ A | x ? B}

인수

map

The map containing the keys to exclude in the resulting map.

반환값

The result of removing all keys from the current instance that are present in a given map.

See Also

  • » Compliment on Wikipedia

예제

Example #1 Ds\Map::diff() example

<?php
$a = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
$b = new \Ds\Map(["b" => 4, "c" => 5, "d" => 6]);

var_dump($a->diff($b));
?>

위 예제의 출력 예시:

object(Ds\Map)#3 (1) {
  [0]=>
  object(Ds\Pair)#4 (2) {
    ["key"]=>
    string(1) "a"
    ["value"]=>
    int(1)
  }
}

User Contributed Notes

There are no user contributed notes for this page.


Creates a new map using a callable to determine which pairs to include.

  • ≪ Ds\Map::diff
  • Ds\Map::first ≫
  • PHP Manual
  • Map
  • Creates a new map using a callable to determine which pairs to include.

Ds\Map::filter

(PECL ds >= 1.0.0)

Ds\Map::filter — Creates a new map using a callable to determine which pairs to include.

설명

public Ds\Map Ds\Map::filter ([ callable $callback ] )

Creates a new map using a callable to determine which pairs to include.

인수

callback

bool callback ( mixed $key , mixed $value )

Optional callable which returns TRUE if the pair should be included, FALSE otherwise.

If a callback is not provided, only values which are TRUE (see converting to boolean) will be included.

반환값

A new map containing all the pairs for which either the callback returned TRUE, or all values that convert to TRUE if a callback was not provided.

예제

Example #1 Ds\Map::filter() example using callback function

<?php
$map = new \Ds\Map(["a", "b", "c", "d", "e"]);

var_dump($map->filter(function($key, $value) {
    return $key % 2 == 0;
}));
?>

위 예제의 출력 예시:

object(Ds\Map)#3 (3) {
  [0]=>
  object(Ds\Pair)#2 (2) {
    ["key"]=>
    int(0)
    ["value"]=>
    string(1) "a"
  }
  [1]=>
  object(Ds\Pair)#4 (2) {
    ["key"]=>
    int(2)
    ["value"]=>
    string(1) "c"
  }
  [2]=>
  object(Ds\Pair)#5 (2) {
    ["key"]=>
    int(4)
    ["value"]=>
    string(1) "e"
  }
}

Example #2 Ds\Map::filter() example without a callback function

<?php
$map = new \Ds\Map(["a" => 0, "b" => 1, "c" => true, "d" => false]);

var_dump($map->filter());
?>

위 예제의 출력 예시:

object(Ds\Map)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  string(1) "a"
  [2]=>
  bool(true)
}

User Contributed Notes

There are no user contributed notes for this page.


Returns the first pair in the map.

  • ≪ Ds\Map::filter
  • Ds\Map::get ≫
  • PHP Manual
  • Map
  • Returns the first pair in the map.

Ds\Map::first

(PECL ds >= 1.0.0)

Ds\Map::first — Returns the first pair in the map.

설명

public Ds\Pair Ds\Map::first ( void )

Returns the first pair in the map.

인수

이 함수는 인수가 없습니다.

반환값

The first pair in the map.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Map::first() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
var_dump($map->first());
?>

위 예제의 출력 예시:

object(Ds\Pair)#2 (2) {
  ["key"]=>
  string(1) "a"
  ["value"]=>
  int(1)
}

User Contributed Notes

There are no user contributed notes for this page.


Returns the value for a given key.

  • ≪ Ds\Map::first
  • Ds\Map::hasKey ≫
  • PHP Manual
  • Map
  • Returns the value for a given key.

Ds\Map::get

(PECL ds >= 1.0.0)

Ds\Map::get — Returns the value for a given key.

설명

public mixed Ds\Map::get ( mixed $key [, mixed $default ] )

Returns the value for a given key, or an optional default value if the key could not be found.

Note:

Keys of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Note:

You can also use array syntax to access values by key, eg. $map["key"].

Caution

Be careful when using array syntax. Scalar keys will be coerced to integers by the engine. For example, $map["1"] will attempt to access int(1), while $map->get("1") will correctly look up the string key.

See Arrays.

인수

key

The key to look up.

default

The optional default value, returned if the key could not be found.

반환값

The value mapped to the given key, or the default value if provided and the key could not be found in the map.

오류/예외

OutOfBoundsException if the key could not be found and a default value was not provided.

예제

Example #1 Ds\Map::get() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

var_dump($map->get("a"));       // 1
var_dump($map->get("d", 10));   // 10 (default used)
?>

위 예제의 출력 예시:

int(1)
int(10)

Example #2 Ds\Map::get() example using array syntax

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

var_dump($map["a"])); // 1
?>

위 예제의 출력 예시:

int(1)

User Contributed Notes

There are no user contributed notes for this page.


Determines whether the map contains a given key.

  • ≪ Ds\Map::get
  • Ds\Map::hasValue ≫
  • PHP Manual
  • Map
  • Determines whether the map contains a given key.

Ds\Map::hasKey

(PECL ds >= 1.0.0)

Ds\Map::hasKey — Determines whether the map contains a given key.

설명

public bool Ds\Map::hasKey ( mixed $key )

Determines whether the map contains a given key.

인수

key

The key to look for.

반환값

Returns TRUE if the key could found, FALSE otherwise.

예제

Example #1 Ds\Map::hasKey() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

var_dump($map->hasKey("a")); // true
var_dump($map->hasKey("e")); // false
?>

위 예제의 출력 예시:

bool(true)
bool(false)

User Contributed Notes

There are no user contributed notes for this page.


Determines whether the map contains a given value.

  • ≪ Ds\Map::hasKey
  • Ds\Map::intersect ≫
  • PHP Manual
  • Map
  • Determines whether the map contains a given value.

Ds\Map::hasValue

(PECL ds >= 1.0.0)

Ds\Map::hasValue — Determines whether the map contains a given value.

설명

public bool Ds\Map::hasValue ( mixed $value )

Determines whether the map contains a given value.

인수

value

The value to look for.

반환값

Returns TRUE if the value could found, FALSE otherwise.

예제

Example #1 Ds\Map::hasValue() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

var_dump($map->hasValue(1)); // true
var_dump($map->hasValue(4)); // false
?>

위 예제의 출력 예시:

bool(true)
bool(false)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new map by intersecting keys with another map.

  • ≪ Ds\Map::hasValue
  • Ds\Map::isEmpty ≫
  • PHP Manual
  • Map
  • Creates a new map by intersecting keys with another map.

Ds\Map::intersect

(PECL ds >= 1.0.0)

Ds\Map::intersect — Creates a new map by intersecting keys with another map.

설명

public Ds\Map Ds\Map::intersect ( Ds\Map $map )

Creates a new map containing the pairs of the current instance whose keys are also present in the given map. In other words, returns a copy of the current instance with all keys removed that are not also in the other map.

A ∩ B = {x : x ∈ A ∧ x ∈ B}

Note:

Values from the current instance will be kept.

인수

map

The other map, containing the keys to intersect with.

반환값

The key intersection of the current instance and another map.

See Also

  • » Intersection on Wikipedia

예제

Example #1 Ds\Map::intersect() example

<?php
$a = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
$b = new \Ds\Map(["b" => 4, "c" => 5, "d" => 6]);

var_dump($a->intersect($b));
?>

위 예제의 출력 예시:

object(Ds\Map)#3 (2) {
  [0]=>
  object(Ds\Pair)#4 (2) {
    ["key"]=>
    string(1) "b"
    ["value"]=>
    int(2)
  }
  [1]=>
  object(Ds\Pair)#5 (2) {
    ["key"]=>
    string(1) "c"
    ["value"]=>
    int(3)
  }
}

User Contributed Notes

There are no user contributed notes for this page.


Returns whether the map is empty

  • ≪ Ds\Map::intersect
  • Ds\Map::jsonSerialize ≫
  • PHP Manual
  • Map
  • Returns whether the map is empty

Ds\Map::isEmpty

(PECL ds >= 1.0.0)

Ds\Map::isEmpty — Returns whether the map is empty

설명

public bool Ds\Map::isEmpty ( void )

Returns whether the map is empty.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the map is empty, FALSE otherwise.

예제

Example #1 Ds\Map::isEmpty() example

<?php
$a = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
$b = new \Ds\Map();

var_dump($a->isEmpty());
var_dump($b->isEmpty());
?>

위 예제의 출력 예시:

bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Returns a representation that can be converted to JSON.

  • ≪ Ds\Map::isEmpty
  • Ds\Map::keys ≫
  • PHP Manual
  • Map
  • Returns a representation that can be converted to JSON.

Ds\Map::jsonSerialize

(PECL ds >= 1.0.0)

Ds\Map::jsonSerialize — Returns a representation that can be converted to JSON.

See JsonSerializable::jsonSerialize()

Note:

You should never need to call this directly.

User Contributed Notes

There are no user contributed notes for this page.


Returns a set of the map's keys.

  • ≪ Ds\Map::jsonSerialize
  • Ds\Map::ksort ≫
  • PHP Manual
  • Map
  • Returns a set of the map's keys.

Ds\Map::keys

(PECL ds >= 1.0.0)

Ds\Map::keys — Returns a set of the map's keys.

설명

public Ds\Set Ds\Map::keys ( void )

Returns a set containing all the keys of the map, in the same order.

인수

이 함수는 인수가 없습니다.

반환값

A Ds\Set containing all the keys of the map.

예제

Example #1 Ds\Map::keys() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
var_dump($map->keys());
?>

위 예제의 출력 예시:

object(Ds\Set)#2 (3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
}

User Contributed Notes

There are no user contributed notes for this page.


Sorts the map in-place by key.

  • ≪ Ds\Map::keys
  • Ds\Map::ksorted ≫
  • PHP Manual
  • Map
  • Sorts the map in-place by key.

Ds\Map::ksort

(PECL ds >= 1.0.0)

Ds\Map::ksort — Sorts the map in-place by key.

설명

public void Ds\Map::ksort ([ callable $comparator ] )

Sorts the map in-place by key, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Map::ksort() example

<?php
$map = new \Ds\Map(["b" => 2, "c" => 3, "a" => 1]);
$map->ksort();

print_r($map);
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

)

Example #2 Ds\Map::ksort() example using a comparator

<?php
$map = new \Ds\Map([1 => "x", 2 => "y", 0 => "z"]);

// Reverse
$map->ksort(function($a, $b) {
    return $b <=> $a;
});

print_r($map);
?>

위 예제의 출력 예시:

Ds\Map Object
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 2
            [value] => y
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => x
        )

    [2] => Ds\Pair Object
        (
            [key] => 0
            [value] => z
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns a copy, sorted by key.

  • ≪ Ds\Map::ksort
  • Ds\Map::last ≫
  • PHP Manual
  • Map
  • Returns a copy, sorted by key.

Ds\Map::ksorted

(No version information available, might only be in Git)

Ds\Map::ksorted — Returns a copy, sorted by key.

설명

public Ds\Map Ds\Map::ksorted ([ callable $comparator ] )

Returns a copy sorted by key, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

Returns a copy of the map, sorted by key.

예제

Example #1 Ds\Map::ksorted() example

<?php
$map = new \Ds\Map(["b" => 2, "c" => 3, "a" => 1]);

print_r($map->ksorted());
?>

위 예제의 출력 예시:

Ds\Map Object
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

)

Example #2 Ds\Map::ksorted() example using a comparator

<?php
$map = new \Ds\Map([1 => "x", 2 => "y", 0 => "z"]);

// Reverse
$sorted = $map->ksorted(function($a, $b) {
    return $b <=> $a;
});

print_r($sorted);
?>

위 예제의 출력 예시:

Ds\Map Object
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 2
            [value] => y
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => x
        )

    [2] => Ds\Pair Object
        (
            [key] => 0
            [value] => z
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns the last pair of the map.

  • ≪ Ds\Map::ksorted
  • Ds\Map::map ≫
  • PHP Manual
  • Map
  • Returns the last pair of the map.

Ds\Map::last

(PECL ds >= 1.0.0)

Ds\Map::last — Returns the last pair of the map.

설명

public Ds\Pair Ds\Map::last ( void )

Returns the last pair of the map.

인수

이 함수는 인수가 없습니다.

반환값

The last pair of the map.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Map::last() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
var_dump($map->last());
?>

위 예제의 출력 예시:

object(Ds\Pair)#2 (2) {
  ["key"]=>
  string(1) "c"
  ["value"]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Returns the result of applying a callback to each value.

  • ≪ Ds\Map::last
  • Ds\Map::merge ≫
  • PHP Manual
  • Map
  • Returns the result of applying a callback to each value.

Ds\Map::map

(PECL ds >= 1.0.0)

Ds\Map::map — Returns the result of applying a callback to each value.

설명

public Ds\Map Ds\Map::map ( callable $callback )

Returns the result of applying a callback function to each value of the map.

인수

callback

mixed callback ( mixed $key , mixed $value )

A callable to apply to each value in the map.

The callable should return what the key will be mapped to in the resulting map.

반환값

The result of applying a callback to each value in the map.

Note:

The keys and values of the current instance won't be affected.

예제

Example #1 Ds\Map::map() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

print_r($map->map(function($key, $value) { return $value * 2; }));
print_r($map);
?>

위 예제의 출력 예시:

(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 2
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 4
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 6
        )

)
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns the result of adding all given associations.

  • ≪ Ds\Map::map
  • Ds\Map::pairs ≫
  • PHP Manual
  • Map
  • Returns the result of adding all given associations.

Ds\Map::merge

(PECL ds >= 1.0.0)

Ds\Map::merge — Returns the result of adding all given associations.

설명

public Ds\Map Ds\Map::merge ( mixed $values )

Returns the result of associating all keys of a given traversable object or array with their corresponding values, combined with the current instance.

Note:

Values of the current instance will be overwritten by those provided where keys are equal.

인수

values

A traversable object or an array.

반환값

The result of associating all keys of a given traversable object or array with their corresponding values, combined with the current instance.

Note:

The current instance won't be affected.

예제

Example #1 Ds\Map::merge() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

print_r($map->merge(["a" => 10, "e" => 50]));
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 10
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

    [3] => Ds\Pair Object
        (
            [key] => e
            [value] => 50
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns a sequence containing all the pairs of the map.

  • ≪ Ds\Map::merge
  • Ds\Map::put ≫
  • PHP Manual
  • Map
  • Returns a sequence containing all the pairs of the map.

Ds\Map::pairs

(PECL ds >= 1.0.0)

Ds\Map::pairs — Returns a sequence containing all the pairs of the map.

설명

public Ds\Sequence Ds\Map::pairs ( void )

Returns a Ds\Sequence containing all the pairs of the map.

인수

이 함수는 인수가 없습니다.

반환값

Ds\Sequence containing all the pairs of the map.

예제

Example #1 Ds\Map::pairs() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

var_dump($map->pairs());
?>

위 예제의 출력 예시:

object(Ds\Map)#8 (3) {
  [0]=>
  object(Ds\Pair)#5 (2) {
    ["key"]=>
    string(1) "a"
    ["value"]=>
    int(1)
  }
  [1]=>
  object(Ds\Pair)#6 (2) {
    ["key"]=>
    string(1) "b"
    ["value"]=>
    int(2)
  }
  [2]=>
  object(Ds\Pair)#7 (2) {
    ["key"]=>
    string(1) "c"
    ["value"]=>
    int(3)
  }
}
p

User Contributed Notes

There are no user contributed notes for this page.


Associates a key with a value.

  • ≪ Ds\Map::pairs
  • Ds\Map::putAll ≫
  • PHP Manual
  • Map
  • Associates a key with a value.

Ds\Map::put

(PECL ds >= 1.0.0)

Ds\Map::put — Associates a key with a value.

설명

public void Ds\Map::put ( mixed $key , mixed $value )

Associates a key with a value, overwriting a previous association if one exists.

Note:

Keys of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Note:

You can also use array syntax to associate values by key, eg. $map["key"] = $value.

Caution

Be careful when using array syntax. Scalar keys will be coerced to integers by the engine. For example, $map["1"] will attempt to access int(1), while $map->get("1") will correctly look up the string key.

See Arrays.

인수

key

The key to associate the value with.

value

The value to be associated with the key.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Map::put() example

<?php
$map = new \Ds\Map();

$map->put("a", 1);
$map->put("b", 2);
$map->put("c", 3);

print_r($map);
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

)

Example #2 Ds\Map::put() example using objects as keys

<?php
class HashableObject implements \Ds\Hashable
{
    /**
     * An arbitrary value to use as the hash value. Does not define equality.
     */
    private $value;

    public function __construct($value)
    {
        $this->value = $value;
    }

    public function hash()
    {
        return $this->value;
    }

    public function equals($obj): bool
    {
        return $this->value === $obj->value;
    }
}

$map = new \Ds\Map();

$obj = new \ArrayIterator([]);

// Using the same instance multiple times will overwrite the previous value.
$map->put($obj, 1);
$map->put($obj, 2);

// Using multiple instances of the same object will create new associations.
$map->put(new \stdClass(), 3);
$map->put(new \stdClass(), 4);

// Using multiple instances of equal hashable objects will overwrite previous values.
$map->put(new \HashableObject(1), 5);
$map->put(new \HashableObject(1), 6);
$map->put(new \HashableObject(2), 7);
$map->put(new \HashableObject(2), 8);

var_dump($map);
?>

위 예제의 출력 예시:

object(Ds\Map)#1 (5) {
  [0]=>
  object(Ds\Pair)#7 (2) {
    ["key"]=>
    object(ArrayIterator)#2 (1) {
      ["storage":"ArrayIterator":private]=>
      array(0) {
      }
    }
    ["value"]=>
    int(2)
  }
  [1]=>
  object(Ds\Pair)#8 (2) {
    ["key"]=>
    object(stdClass)#3 (0) {
    }
    ["value"]=>
    int(3)
  }
  [2]=>
  object(Ds\Pair)#9 (2) {
    ["key"]=>
    object(stdClass)#4 (0) {
    }
    ["value"]=>
    int(4)
  }
  [3]=>
  object(Ds\Pair)#10 (2) {
    ["key"]=>
    object(HashableObject)#5 (1) {
      ["value":"HashableObject":private]=>
      int(1)
    }
    ["value"]=>
    int(6)
  }
  [4]=>
  object(Ds\Pair)#11 (2) {
    ["key"]=>
    object(HashableObject)#6 (1) {
      ["value":"HashableObject":private]=>
      int(2)
    }
    ["value"]=>
    int(8)
  }
}

User Contributed Notes

There are no user contributed notes for this page.


Associates all key-value pairs of a traversable object or array.

  • ≪ Ds\Map::put
  • Ds\Map::reduce ≫
  • PHP Manual
  • Map
  • Associates all key-value pairs of a traversable object or array.

Ds\Map::putAll

(PECL ds >= 1.0.2)

Ds\Map::putAll — Associates all key-value pairs of a traversable object or array.

설명

public void Ds\Map::putAll ( mixed $pairs )

Associates all key-value pairs of a traversable object or array.

Note:

Keys of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

인수

pairs

traversable object or array.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Map::putAll() example

<?php
$map = new \Ds\Map();

$map->putAll([
    "a" => 1,
    "b" => 2,
    "c" => 3,
]);

print_r($map);
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Reduces the map to a single value using a callback function.

  • ≪ Ds\Map::putAll
  • Ds\Map::remove ≫
  • PHP Manual
  • Map
  • Reduces the map to a single value using a callback function.

Ds\Map::reduce

(PECL ds >= 1.0.0)

Ds\Map::reduce — Reduces the map to a single value using a callback function.

설명

public mixed Ds\Map::reduce ( callable $callback [, mixed $initial ] )

Reduces the map to a single value using a callback function.

인수

callback
mixed callback ( mixed $carry , mixed $key , mixed $value )
carry

The return value of the previous callback, or initial if it's the first iteration.

key

The key of the current iteration.

value

The value of the current iteration.

initial

The initial value of the carry value. Can be NULL.

반환값

The return value of the final callback.

예제

Example #1 Ds\Map::reduce() with initial value example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

$callback = function($carry, $key, $value) {
    return $carry * $value;
};

var_dump($map->reduce($callback, 5));

// Iterations:
//
// $carry = $initial = 5
//
// $carry = $carry * 1 =  5
// $carry = $carry * 2 = 10
// $carry = $carry * 3 = 30
?>

위 예제의 출력 예시:

int(30)

Example #2 Ds\Map::reduce() without an initial value example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

var_dump($map->reduce(function($carry, $key, $value) {
    return $carry + $value + 5;
}));

// Iterations:
//
// $carry = $initial = null
//
// $carry = $carry + 1 + 5 =  6
// $carry = $carry + 2 + 5 = 13
// $carry = $carry + 3 + 5 = 21
?>

위 예제의 출력 예시:

int(21)

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns a value by key.

  • ≪ Ds\Map::reduce
  • Ds\Map::reverse ≫
  • PHP Manual
  • Map
  • Removes and returns a value by key.

Ds\Map::remove

(PECL ds >= 1.0.0)

Ds\Map::remove — Removes and returns a value by key.

설명

public mixed Ds\Map::remove ( mixed $key [, mixed $default ] )

Removes and returns a value by key, or return an optional default value if the key could not be found.

Note:

Keys of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Note:

You can also use array syntax to access values by key, eg. $map["key"].

Caution

Be careful when using array syntax. Scalar keys will be coerced to integers by the engine. For example, $map["1"] will attempt to access int(1), while $map->get("1") will correctly look up the string key.

See Arrays.

인수

key

The key to remove.

default

The optional default value, returned if the key could not be found.

반환값

The value that was removed, or the default value if provided and the key could not be found in the map.

오류/예외

OutOfBoundsException if the key could not be found and a default value was not provided.

예제

Example #1 Ds\Map::remove() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

var_dump($map->remove("a"));      //  1
var_dump($map->remove("e", 10));  // 10 (default used)
?>

위 예제의 출력 예시:

int(1)
int(10)

User Contributed Notes

There are no user contributed notes for this page.


Reverses the map in-place.

  • ≪ Ds\Map::remove
  • Ds\Map::reversed ≫
  • PHP Manual
  • Map
  • Reverses the map in-place.

Ds\Map::reverse

(PECL ds >= 1.0.0)

Ds\Map::reverse — Reverses the map in-place.

설명

public void Ds\Map::reverse ( void )

Reverses the map in-place.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Map::reverse() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
$map->reverse();

print_r($map);
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns a reversed copy.

  • ≪ Ds\Map::reverse
  • Ds\Map::skip ≫
  • PHP Manual
  • Map
  • Returns a reversed copy.

Ds\Map::reversed

(PECL ds >= 1.0.0)

Ds\Map::reversed — Returns a reversed copy.

설명

public Ds\Map Ds\Map::reversed ( void )

Returns a reversed copy of the map.

인수

이 함수는 인수가 없습니다.

반환값

A reversed copy of the map.

Note:

The current instance is not affected.

예제

Example #1 Ds\Map::reversed() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

print_r($map->reversed());
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => c
            [value] => 3
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns the pair at a given positional index.

  • ≪ Ds\Map::reversed
  • Ds\Map::slice ≫
  • PHP Manual
  • Map
  • Returns the pair at a given positional index.

Ds\Map::skip

(PECL ds >= 1.0.0)

Ds\Map::skip — Returns the pair at a given positional index.

설명

public Ds\Pair Ds\Map::skip ( int $position )

Returns the pair at a given zero-based position.

인수

position

The zero-based positional index to return.

반환값

Returns the Ds\Pair at the given position.

오류/예외

OutOfRangeException if the position is not valid.

예제

Example #1 Ds\Map::skip() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);

var_dump($map->skip(1));
?>

위 예제의 출력 예시:

object(Ds\Pair)#2 (2) {
  ["key"]=>
  string(1) "b"
  ["value"]=>
  int(2)
}

User Contributed Notes

There are no user contributed notes for this page.


Returns a subset of the map defined by a starting index and length.

  • ≪ Ds\Map::skip
  • Ds\Map::sort ≫
  • PHP Manual
  • Map
  • Returns a subset of the map defined by a starting index and length.

Ds\Map::slice

(PECL ds >= 1.0.0)

Ds\Map::slice — Returns a subset of the map defined by a starting index and length.

설명

public Ds\Map Ds\Map::slice ( int $index [, int $length ] )

Returns a subset of the map defined by a starting index and length.

인수

index

The index at which the range starts.

If positive, the range will start at that index in the map. If negative, the range will start that far from the end.

length

If a length is given and is positive, the resulting map will have up to that many pairs in it. If a length is given and is negative, the range will stop that many pairs from the end. If the length results in an overflow, only pairs up to the end of the map will be included. If a length is not provided, the resulting map will contain all pairs between the index and the end of the map.

반환값

A subset of the map defined by a starting index and length.

예제

Example #1 Ds\Map::slice() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5]);

// Slice from 2 onwards
print_r($map->slice(2)->toArray());

// Slice from 1, for a length of 3
print_r($map->slice(1, 3)->toArray());

// Slice from 1 onwards
print_r($map->slice(1)->toArray());

// Slice from 2 from the end onwards
print_r($map->slice(-2)->toArray());

// Slice from 1 to 1 from the end
print_r($map->slice(1, -1)->toArray());
?>

위 예제의 출력 예시:

Array
(
    [c] => 3
    [d] => 4
    [e] => 5
)
Array
(
    [b] => 2
    [c] => 3
    [d] => 4
)
Array
(
    [b] => 2
    [c] => 3
    [d] => 4
    [e] => 5
)
Array
(
    [d] => 4
    [e] => 5
)
Array
(
    [b] => 2
    [c] => 3
    [d] => 4
)

User Contributed Notes

There are no user contributed notes for this page.


Sorts the map in-place by value.

  • ≪ Ds\Map::slice
  • Ds\Map::sorted ≫
  • PHP Manual
  • Map
  • Sorts the map in-place by value.

Ds\Map::sort

(PECL ds >= 1.0.0)

Ds\Map::sort — Sorts the map in-place by value.

설명

public void Ds\Map::sort ([ callable $comparator ] )

Sorts the map in-place by value, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Map::sort() example

<?php
$map = new \Ds\Map(["a" => 2, "b" => 3, "c" => 1]);
$map->sort();

print_r($map);
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => c
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => a
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => b
            [value] => 3
        )

)

Example #2 Ds\Map::sort() example using a comparator

<?php
$map = new \Ds\Map(["a" => 2, "b" => 3, "c" => 1]);

$map->sort(function($a, $b) {
    return $b <=> $a;
});

print_r($map);
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => b
            [value] => 3
        )

    [1] => Ds\Pair Object
        (
            [key] => a
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 1
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns a copy, sorted by value.

  • ≪ Ds\Map::sort
  • Ds\Map::sum ≫
  • PHP Manual
  • Map
  • Returns a copy, sorted by value.

Ds\Map::sorted

(PECL ds >= 1.0.0)

Ds\Map::sorted — Returns a copy, sorted by value.

설명

public Ds\Map Ds\Map::sorted ([ callable $comparator ] )

Returns a copy, sorted by value using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

Returns a copy of the map, sorted by value.

예제

Example #1 Ds\Map::sort() example

<?php
$map = new \Ds\Map(["a" => 2, "b" => 3, "c" => 1]);

print_r($map->sorted());
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => c
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => a
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => b
            [value] => 3
        )

)

Example #2 Ds\Map::sort() example using a comparator

<?php
$map = new \Ds\Map(["a" => 2, "b" => 3, "c" => 1]);

// Reverse
$sorted = $map->sorted(function($a, $b) {
    return $b <=> $a;
});

print_r($sorted);
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => b
            [value] => 3
        )

    [1] => Ds\Pair Object
        (
            [key] => a
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 1
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns the sum of all values in the map.

  • ≪ Ds\Map::sorted
  • Ds\Map::toArray ≫
  • PHP Manual
  • Map
  • Returns the sum of all values in the map.

Ds\Map::sum

(No version information available, might only be in Git)

Ds\Map::sum — Returns the sum of all values in the map.

설명

public number Ds\Map::sum ( void )

Returns the sum of all values in the map.

Note:

Arrays and objects are considered equal to zero when calculating the sum.

인수

이 함수는 인수가 없습니다.

반환값

The sum of all the values in the map as either a float or int depending on the values in the map.

예제

Example #1 Ds\Map::sum() integer example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
var_dump($map->sum());
?>

위 예제의 출력 예시:

int(6)

Example #2 Ds\Map::sum() float example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2.5, "c" => 3]);
var_dump($map->sum());
?>

위 예제의 출력 예시:

float(6.5)

User Contributed Notes

There are no user contributed notes for this page.


Converts the map to an array.

  • ≪ Ds\Map::sum
  • Ds\Map::union ≫
  • PHP Manual
  • Map
  • Converts the map to an array.

Ds\Map::toArray

(PECL ds >= 1.0.0)

Ds\Map::toArray — Converts the map to an array.

설명

public array Ds\Map::toArray ( void )

Converts the map to an array.

Caution

Maps where non-scalar keys are can't be converted to an array.

Caution

An array will treat all numeric keys as integers, eg. "1" and 1 as keys in the map will only result in 1 being included in the array.

Note:

Casting to an array is not supported yet.

인수

이 함수는 인수가 없습니다.

반환값

An array containing all the values in the same order as the map.

예제

Example #1 Ds\Map::toArray() example

<?php
$map = new \Ds\Map([
    "a" => 1,
    "b" => 2,
    "c" => 3,
]);

var_dump($map->toArray());
?>

위 예제의 출력 예시:

array(3) {
  ["a"]=>
  int(1)
  ["b"]=>
  int(2)
  ["c"]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Creates a new map using values from the current instance and another map.

  • ≪ Ds\Map::toArray
  • Ds\Map::values ≫
  • PHP Manual
  • Map
  • Creates a new map using values from the current instance and another map.

Ds\Map::union

(PECL ds >= 1.0.0)

Ds\Map::union — Creates a new map using values from the current instance and another map.

설명

public Ds\Map Ds\Map::union ( Ds\Map $map )

Creates a new map that contains the pairs of the current instance as well as the pairs of another map.

A ∪ B = {x: x ∈ A ∨ x ∈ B}

Note:

Values of the current instance will be overwritten by those provided where keys are equal.

인수

map

The other map, to combine with the current instance.

반환값

A new map containing all the pairs of the current instance as well as another map.

See Also

  • » Union on Wikipedia

예제

Example #1 Ds\Map::union() example

<?php
$a = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
$b = new \Ds\Map(["b" => 3, "c" => 4, "d" => 5]);

print_r($a->union($b));
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 3
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 4
        )

    [3] => Ds\Pair Object
        (
            [key] => d
            [value] => 5
        )

)

User Contributed Notes

There are no user contributed notes for this page.


Returns a sequence of the map's values.

  • ≪ Ds\Map::union
  • Ds\Map::xor ≫
  • PHP Manual
  • Map
  • Returns a sequence of the map's values.

Ds\Map::values

(PECL ds >= 1.0.0)

Ds\Map::values — Returns a sequence of the map's values.

설명

public Ds\Sequence Ds\Map::values ( void )

Returns a sequence containing all the values of the map, in the same order.

인수

이 함수는 인수가 없습니다.

반환값

A Ds\Sequence containing all the values of the map.

예제

Example #1 Ds\Map::values() example

<?php
$map = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
var_dump($map->values());
?>

위 예제의 출력 예시:

object(Ds\Map)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Creates a new map using keys of either the current instance or of another map, but not of both.

  • ≪ Ds\Map::values
  • Pair ≫
  • PHP Manual
  • Map
  • Creates a new map using keys of either the current instance or of another map, but not of both.

Ds\Map::xor

(PECL ds >= 1.0.0)

Ds\Map::xor — Creates a new map using keys of either the current instance or of another map, but not of both.

설명

public Ds\Map Ds\Map::xor ( Ds\Map $map )

Creates a new map containing keys of the current instance as well as another map, but not of both.

A ? B = {x : x ∈ (A \ B) ∪ (B \ A)}

인수

map

The other map.

반환값

A new map containing keys in the current instance as well as another map, but not in both.

See Also

  • » Symmetric Difference on Wikipedia

예제

Example #1 Ds\Map::xor() example

<?php
$a = new \Ds\Map(["a" => 1, "b" => 2, "c" => 3]);
$b = new \Ds\Map(["b" => 4, "c" => 5, "d" => 6]);

print_r($a->xor($b));
?>

위 예제의 출력 예시:

Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => d
            [value] => 6
        )

)

User Contributed Notes

There are no user contributed notes for this page.


The Pair class

(No version information available, might only be in Git)

소개

A pair is used by Ds\Map to pair keys with values.

클래스 개요

Ds\Pair implements JsonSerializable {
/* 메소드 */
public __construct ([ mixed $key [, mixed $value ]] )
}

Table of Contents

  • Ds\Pair::clear — Removes all values.
  • Ds\Pair::__construct — Creates a new instance.
  • Ds\Pair::copy — Returns a shallow copy of the pair.
  • Ds\Pair::isEmpty — Returns whether the pair is empty
  • Ds\Pair::jsonSerialize — Returns a representation that can be converted to JSON.
  • Ds\Pair::toArray — Converts the pair to an array.

User Contributed Notes

There are no user contributed notes for this page.


Removes all values.

  • ≪ Pair
  • Ds\Pair::__construct ≫
  • PHP Manual
  • Pair
  • Removes all values.

Ds\Pair::clear

(No version information available, might only be in Git)

Ds\Pair::clear — Removes all values.

설명

public void Ds\Pair::clear ( void )

Removes all values from the pair.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Pair::clear() example

<?php
$pair = new \Ds\Pair("a", 1);
print_r($pair);

$pair->clear();
print_r($pair);
?>

위 예제의 출력 예시:

Ds\Pair Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Pair Object
(
)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new instance.

  • ≪ Ds\Pair::clear
  • Ds\Pair::copy ≫
  • PHP Manual
  • Pair
  • Creates a new instance.

Ds\Pair::__construct

(PECL ds >= 1.0.0)

Ds\Pair::__construct — Creates a new instance.

설명

public Ds\Pair::__construct ([ mixed $key [, mixed $value ]] )

Creates a new instance using a given key and value.

인수

key

The key.

value

The value.

User Contributed Notes

There are no user contributed notes for this page.


Returns a shallow copy of the pair.

  • ≪ Ds\Pair::__construct
  • Ds\Pair::isEmpty ≫
  • PHP Manual
  • Pair
  • Returns a shallow copy of the pair.

Ds\Pair::copy

(No version information available, might only be in Git)

Ds\Pair::copy — Returns a shallow copy of the pair.

설명

public Ds\Pair Ds\Pair::copy ( void )

Returns a shallow copy of the pair.

인수

이 함수는 인수가 없습니다.

반환값

Returns a shallow copy of the pair.

예제

Example #1 Ds\Pair::copy() example

<?php
$a = new \Ds\Pair("a", 1);
$b = $a->copy();

$a->key = "x";

print_r($a);
print_r($b);
?>

위 예제의 출력 예시:

Ds\Pair Object
(
    [key] => x
    [value] => 1
)
Ds\Pair Object
(
    [key] => a
    [value] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns whether the pair is empty

  • ≪ Ds\Pair::copy
  • Ds\Pair::jsonSerialize ≫
  • PHP Manual
  • Pair
  • Returns whether the pair is empty

Ds\Pair::isEmpty

(No version information available, might only be in Git)

Ds\Pair::isEmpty — Returns whether the pair is empty

설명

public bool Ds\Pair::isEmpty ( void )

Returns whether the pair is empty.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the pair is empty, FALSE otherwise.

예제

Example #1 Ds\Pair::isEmpty() example

<?php
$a = new \Ds\Pair("a", 1);
$b = new \Ds\Pair();

var_dump($a->isEmpty());
var_dump($b->isEmpty());
?>

위 예제의 출력 예시:

bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Returns a representation that can be converted to JSON.

  • ≪ Ds\Pair::isEmpty
  • Ds\Pair::toArray ≫
  • PHP Manual
  • Pair
  • Returns a representation that can be converted to JSON.

Ds\Pair::jsonSerialize

(PECL ds >= 1.0.0)

Ds\Pair::jsonSerialize — Returns a representation that can be converted to JSON.

See JsonSerializable::jsonSerialize()

Note:

You should never need to call this directly.

User Contributed Notes

There are no user contributed notes for this page.


Converts the pair to an array.

  • ≪ Ds\Pair::jsonSerialize
  • Set ≫
  • PHP Manual
  • Pair
  • Converts the pair to an array.

Ds\Pair::toArray

(PECL ds >= 1.0.0)

Ds\Pair::toArray — Converts the pair to an array.

설명

public array Ds\Pair::toArray ( void )

Converts the pair to an array.

Note:

Casting to an array is not supported yet.

인수

이 함수는 인수가 없습니다.

반환값

An array containing all the values in the same order as the pair.

예제

Example #1 Ds\Pair::toArray() example

<?php
$pair = new \Ds\Pair("a", 1);

var_dump($pair->toArray());
?>

위 예제의 출력 예시:

array(2) {
  ["key"]=>
  string(1) "a"
  ["value"]=>
  int(1)
}

User Contributed Notes

There are no user contributed notes for this page.


The Set class

(No version information available, might only be in Git)

소개

A Set is a sequence of unique values. This implementation uses the same hash table as Ds\Map, where values are used as keys and the mapped value is ignored.

Strengths

  • Values can be any type, including objects.
  • Supports array syntax (square brackets).
  • Insertion order is preserved.
  • Automatically frees allocated memory when its size drops low enough.
  • add(),
remove() and
contains() are all O(1).

Weaknesses

  • Doesn't support
                push(),
                pop(),
                insert(),
                shift(), or
                unshift().
  • get() is O(n) if there are deleted values in the buffer before the accessed index, O(1) otherwise.

클래스 개요

Ds\Set implements Ds\Collection {
/* Constants */
const int MIN_CAPACITY = 16 ;
/* 메소드 */
public void add ([ mixed $...values ] )
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public bool contains ([ mixed $...values ] )
public Ds\Set copy ( void )
public Ds\Set diff ( Ds\Set $set )
public Ds\Set filter ([ callable $callback ] )
public void first ( void )
public mixed get ( int $index )
public Ds\Set intersect ( Ds\Set $set )
public bool isEmpty ( void )
public string join ([ string $glue ] )
public void last ( void )
public Ds\Set merge ( mixed $values )
public mixed reduce ( callable $callback [, mixed $initial ] )
public void remove ([ mixed $...values ] )
public void reverse ( void )
public Ds\Set reversed ( void )
public Ds\Set slice ( int $index [, int $length ] )
public void sort ([ callable $comparator ] )
public Ds\Set sorted ([ callable $comparator ] )
public number sum ( void )
public array toArray ( void )
public Ds\Set union ( Ds\Set $set )
public Ds\Set xor ( Ds\Set $set )
}

예약 상수

Ds\Set::MIN_CAPACITY

Table of Contents

  • Ds\Set::add — Adds values to the set.
  • Ds\Set::allocate — Allocates enough memory for a required capacity.
  • Ds\Set::capacity — Returns the current capacity.
  • Ds\Set::clear — Removes all values.
  • Ds\Set::__construct — Creates a new instance.
  • Ds\Set::contains — Determines if the set contains all values.
  • Ds\Set::copy — Returns a shallow copy of the set.
  • Ds\Set::count — Returns the number of values in the set.
  • Ds\Set::diff — Creates a new set using values that aren't in another set.
  • Ds\Set::filter — Creates a new set using a callable to determine which values to include.
  • Ds\Set::first — Returns the first value in the set.
  • Ds\Set::get — Returns the value at a given index.
  • Ds\Set::intersect — Creates a new set by intersecting values with another set.
  • Ds\Set::isEmpty — Returns whether the set is empty
  • Ds\Set::join — Joins all values together as a string.
  • Ds\Set::jsonSerialize — Returns a representation that can be converted to JSON.
  • Ds\Set::last — Returns the last value in the set.
  • Ds\Set::merge — Returns the result of adding all given values to the set.
  • Ds\Set::reduce — Reduces the set to a single value using a callback function.
  • Ds\Set::remove — Removes all given values from the set.
  • Ds\Set::reverse — Reverses the set in-place.
  • Ds\Set::reversed — Returns a reversed copy.
  • Ds\Set::slice — Returns a sub-set of a given range.
  • Ds\Set::sort — Sorts the set in-place.
  • Ds\Set::sorted — Returns a sorted copy.
  • Ds\Set::sum — Returns the sum of all values in the set.
  • Ds\Set::toArray — Converts the set to an array.
  • Ds\Set::union — Creates a new set using values from the current instance and another set.
  • Ds\Set::xor — Creates a new set using values in either the current instance or in another set, but not in both.

User Contributed Notes

There are no user contributed notes for this page.


Adds values to the set.

  • ≪ Set
  • Ds\Set::allocate ≫
  • PHP Manual
  • Set
  • Adds values to the set.

Ds\Set::add

(PECL ds >= 1.0.0)

Ds\Set::add — Adds values to the set.

설명

public void Ds\Set::add ([ mixed $...values ] )

Adds all given values to the set that haven't already been added.

Note:

Values of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Caution

All comparisons are strict (type and value).

인수

values

Values to add to the set.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Set::add() example using integers

<?php
$set = new \Ds\Set();

$set->add(1);
$set->add(1);
$set->add(2);
$set->add(3);

// Strict comparison would not treat these the same as int(1)
$set->add("1");
$set->add(true);

var_dump($set);
?>

위 예제의 출력 예시:

object(Ds\Set)#1 (5) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  string(1) "1"
  [4]=>
  bool(true)
}

Example #2 Ds\Set::add() example using objects

<?php
class HashableObject implements \Ds\Hashable
{
    /**
     * An arbitrary value to use as the hash value. Does not define equality.
     */
    private $value;

    public function __construct($value)
    {
        $this->value = $value;
    }

    public function hash()
    {
        return $this->value;
    }

    public function equals($obj): bool
    {
        return $this->value === $obj->value;
    }
}

$set = new \Ds\Set();

$obj = new \ArrayIterator([]);

// Adding the same instance multiple times will only add the first.
$set->add($obj);
$set->add($obj);

// Adding multiple instances of the same object will add them all.
$set->add(new \stdClass());
$set->add(new \stdClass());

// Adding multiple instances of equal hashable objects will only add the first.
$set->add(new \HashableObject(1));
$set->add(new \HashableObject(1));
$set->add(new \HashableObject(2));
$set->add(new \HashableObject(2));

var_dump($set);
?>

위 예제의 출력 예시:

object(Ds\Set)#1 (5) {
  [0]=>
  object(ArrayIterator)#2 (1) {
    ["storage":"ArrayIterator":private]=>
    array(0) {
    }
  }
  [1]=>
  object(stdClass)#3 (0) {
  }
  [2]=>
  object(stdClass)#4 (0) {
  }
  [3]=>
  object(HashableObject)#5 (1) {
    ["value":"HashableObject":private]=>
    int(1)
  }
  [4]=>
  object(HashableObject)#6 (1) {
    ["value":"HashableObject":private]=>
    int(2)
  }
}

User Contributed Notes

There are no user contributed notes for this page.


Allocates enough memory for a required capacity.

  • ≪ Ds\Set::add
  • Ds\Set::capacity ≫
  • PHP Manual
  • Set
  • Allocates enough memory for a required capacity.

Ds\Set::allocate

(PECL ds >= 1.0.0)

Ds\Set::allocate — Allocates enough memory for a required capacity.

설명

public void Ds\Set::allocate ( int $capacity )

Allocates enough memory for a required capacity.

인수

capacity

The number of values for which capacity should be allocated.

Note:

Capacity will stay the same if this value is less than or equal to the current capacity.

Note:

Capacity will always be rounded up to the nearest power of 2.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Set::allocate() example

<?php
$set = new \Ds\Set();
var_dump($set->capacity());

$set->allocate(100);
var_dump($set->capacity());
?>

위 예제의 출력 예시:

int(16)
int(128)

User Contributed Notes

There are no user contributed notes for this page.


Returns the current capacity.

  • ≪ Ds\Set::allocate
  • Ds\Set::clear ≫
  • PHP Manual
  • Set
  • Returns the current capacity.

Ds\Set::capacity

(PECL ds >= 1.0.0)

Ds\Set::capacity — Returns the current capacity.

설명

public int Ds\Set::capacity ( void )

Returns the current capacity.

인수

이 함수는 인수가 없습니다.

반환값

The current capacity.

예제

Example #1 Ds\Set::capacity() example

<?php
$set = new \Ds\Set();
var_dump($set->capacity());

$set->push(...range(1, 50));
var_dump($set->capacity());
?>

위 예제의 출력 예시:

int(16)
int(64)

User Contributed Notes

There are no user contributed notes for this page.


Removes all values.

  • ≪ Ds\Set::capacity
  • Ds\Set::__construct ≫
  • PHP Manual
  • Set
  • Removes all values.

Ds\Set::clear

(PECL ds >= 1.0.0)

Ds\Set::clear — Removes all values.

설명

public void Ds\Set::clear ( void )

Removes all values from the set.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Set::clear() example

<?php
$set = new \Ds\Set([1, 2, 3]);
print_r($set);

$set->clear();
print_r($set);
?>

위 예제의 출력 예시:

Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Set Object
(
)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new instance.

  • ≪ Ds\Set::clear
  • Ds\Set::contains ≫
  • PHP Manual
  • Set
  • Creates a new instance.

Ds\Set::__construct

(PECL ds >= 1.0.0)

Ds\Set::__construct — Creates a new instance.

설명

public Ds\Set::__construct ([ mixed $...values ] )

Creates a new instance, using either a traversable object or an array for the initial values.

인수

values

A traversable object or an array to use for the initial values.

예제

Example #1 Ds\Set::__construct() example

<?php
$set = new \Ds\Set();
var_dump($set);

$set = new \Ds\Set([1, 2, 3]);
var_dump($set);
?>

위 예제의 출력 예시:

object(Ds\Set)#1 (0) {
}
object(Ds\Set)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Determines if the set contains all values.

  • ≪ Ds\Set::__construct
  • Ds\Set::copy ≫
  • PHP Manual
  • Set
  • Determines if the set contains all values.

Ds\Set::contains

(PECL ds >= 1.0.0)

Ds\Set::contains — Determines if the set contains all values.

설명

public bool Ds\Set::contains ([ mixed $...values ] )

Determines if the set contains all values.

Note:

Values of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Caution

All comparisons are strict (type and value).

인수

values

Values to check.

반환값

FALSE if any of the provided values are not in the set, TRUE otherwise.

예제

Example #1 Ds\Set::contains() example

<?php
$set = new \Ds\Set([1, 2, 3]);

var_dump($set->contains(1));                // true
var_dump($set->contains(1, 2));             // true
var_dump($set->contains(...[1, 2]));        // true

var_dump($set->contains("1"));              // false
var_dump($set->contains(...[1, 2, 3, 4]));  // false

var_dump($set->contains(...[]));            // true
?>

위 예제의 출력 예시:

bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Returns a shallow copy of the set.

  • ≪ Ds\Set::contains
  • Ds\Set::count ≫
  • PHP Manual
  • Set
  • Returns a shallow copy of the set.

Ds\Set::copy

(PECL ds >= 1.0.0)

Ds\Set::copy — Returns a shallow copy of the set.

설명

public Ds\Set Ds\Set::copy ( void )

Returns a shallow copy of the set.

인수

이 함수는 인수가 없습니다.

반환값

Returns a shallow copy of the set.

예제

Example #1 Ds\Set::copy() example

<?php
$a = new \Ds\Set([1, 2, 3]);
$b = $a->copy();

// Updating the copy doesn't affect the original
$b->add(4);

print_r($a);
print_r($b);
?>

위 예제의 출력 예시:

Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of values in the set.

  • ≪ Ds\Set::copy
  • Ds\Set::diff ≫
  • PHP Manual
  • Set
  • Returns the number of values in the set.

Ds\Set::count

(PECL ds >= 1.0.0)

Ds\Set::count — Returns the number of values in the set.

See Countable::count()

User Contributed Notes

There are no user contributed notes for this page.


Creates a new set using values that aren't in another set.

  • ≪ Ds\Set::count
  • Ds\Set::filter ≫
  • PHP Manual
  • Set
  • Creates a new set using values that aren't in another set.

Ds\Set::diff

(PECL ds >= 1.0.0)

Ds\Set::diff — Creates a new set using values that aren't in another set.

설명

public Ds\Set Ds\Set::diff ( Ds\Set $set )

Creates a new set using values that aren't in another set.

A \ B = {x ∈ A | x ? B}

인수

set

Set containing the values to exclude.

반환값

A new set containing all values that were not in the other set.

See Also

  • » Compliment on Wikipedia

예제

Example #1 Ds\Set::diff() example

<?php
$a = new \Ds\Set([1, 2, 3]);
$b = new \Ds\Set([3, 4, 5]);

var_dump($a->diff($b));
?>

위 예제의 출력 예시:

object(Ds\Set)#3 (2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}

User Contributed Notes

There are no user contributed notes for this page.


Creates a new set using a callable to determine which values to include.

  • ≪ Ds\Set::diff
  • Ds\Set::first ≫
  • PHP Manual
  • Set
  • Creates a new set using a callable to determine which values to include.

Ds\Set::filter

(PECL ds >= 1.0.0)

Ds\Set::filter — Creates a new set using a callable to determine which values to include.

설명

public Ds\Set Ds\Set::filter ([ callable $callback ] )

Creates a new set using a callable to determine which values to include.

인수

callback

bool callback ( mixed $value )

Optional callable which returns TRUE if the value should be included, FALSE otherwise.

If a callback is not provided, only values which are TRUE (see converting to boolean) will be included.

반환값

A new set containing all the values for which either the callback returned TRUE, or all values that convert to TRUE if a callback was not provided.

예제

Example #1 Ds\Set::filter() example using callback function

<?php
$set = new \Ds\Set([1, 2, 3, 4, 5]);

var_dump($set->filter(function($value) {
    return $value % 2 == 0;
}));
?>

위 예제의 출력 예시:

object(Ds\Set)#3 (2) {
  [0]=>
  int(2)
  [1]=>
  int(4)
}

Example #2 Ds\Set::filter() example without a callback function

<?php
$set = new \Ds\Set([0, 1, 'a', true, false]);

var_dump($set->filter());
?>

위 예제의 출력 예시:

object(Ds\Set)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  string(1) "a"
  [2]=>
  bool(true)
}

User Contributed Notes

There are no user contributed notes for this page.


Returns the first value in the set.

  • ≪ Ds\Set::filter
  • Ds\Set::get ≫
  • PHP Manual
  • Set
  • Returns the first value in the set.

Ds\Set::first

(PECL ds >= 1.0.0)

Ds\Set::first — Returns the first value in the set.

설명

public void Ds\Set::first ( void )

Returns the first value in the set.

인수

이 함수는 인수가 없습니다.

반환값

The first value in the set.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Set::first() example

<?php
$set = new \Ds\Set([1, 2, 3]);
var_dump($set->first());
?>

위 예제의 출력 예시:

int(1)

User Contributed Notes

There are no user contributed notes for this page.


Returns the value at a given index.

  • ≪ Ds\Set::first
  • Ds\Set::intersect ≫
  • PHP Manual
  • Set
  • Returns the value at a given index.

Ds\Set::get

(PECL ds >= 1.0.0)

Ds\Set::get — Returns the value at a given index.

설명

public mixed Ds\Set::get ( int $index )

Returns the value at a given index.

인수

index

The index to access, starting at 0.

반환값

The value at the requested index.

오류/예외

OutOfRangeException if the index is not valid.

예제

Example #1 Ds\Set::get() example

<?php
$set = new \Ds\Set(["a", "b", "c"]);

var_dump($set->get(0));
var_dump($set->get(1));
var_dump($set->get(2));
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

Example #2 Ds\Set::get() example using array syntax

<?php
$set = new \Ds\Set(["a", "b", "c"]);

var_dump($set[0]);
var_dump($set[1]);
var_dump($set[2]);
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Creates a new set by intersecting values with another set.

  • ≪ Ds\Set::get
  • Ds\Set::isEmpty ≫
  • PHP Manual
  • Set
  • Creates a new set by intersecting values with another set.

Ds\Set::intersect

(PECL ds >= 1.0.0)

Ds\Set::intersect — Creates a new set by intersecting values with another set.

설명

public Ds\Set Ds\Set::intersect ( Ds\Set $set )

Creates a new set using values common to both the current instance and another set. In other words, returns a copy of the current instance with all values removed that are not in the other set.

A ∩ B = {x : x ∈ A ∧ x ∈ B}

인수

set

The other set.

반환값

The intersection of the current instance and another set.

See Also

  • » Intersection on Wikipedia

예제

Example #1 Ds\Set::intersect() example

<?php
$a = new \Ds\Set([1, 2, 3]);
$b = new \Ds\Set([3, 4, 5]);

var_dump($a->intersect($b));
?>

위 예제의 출력 예시:

object(Ds\Set)#3 (1) {
  [0]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Returns whether the set is empty

  • ≪ Ds\Set::intersect
  • Ds\Set::join ≫
  • PHP Manual
  • Set
  • Returns whether the set is empty

Ds\Set::isEmpty

(PECL ds >= 1.0.0)

Ds\Set::isEmpty — Returns whether the set is empty

설명

public bool Ds\Set::isEmpty ( void )

Returns whether the set is empty.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the set is empty, FALSE otherwise.

예제

Example #1 Ds\Set::isEmpty() example

<?php
$a = new \Ds\Set([1, 2, 3]);
$b = new \Ds\Set();

var_dump($a->isEmpty());
var_dump($b->isEmpty());
?>

위 예제의 출력 예시:

bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Joins all values together as a string.

  • ≪ Ds\Set::isEmpty
  • Ds\Set::jsonSerialize ≫
  • PHP Manual
  • Set
  • Joins all values together as a string.

Ds\Set::join

(PECL ds >= 1.0.0)

Ds\Set::join — Joins all values together as a string.

설명

public string Ds\Set::join ([ string $glue ] )

Joins all values together as a string using an optional separator between each value.

인수

glue

An optional string to separate each value.

반환값

All values of the set joined together as a string.

예제

Example #1 Ds\Set::join() example using a separator string

<?php
$set = new \Ds\Set(["a", "b", "c", 1, 2, 3]);

var_dump($set->join("|"));
?>

위 예제의 출력 예시:

string(11) "a|b|c|1|2|3"

Example #2 Ds\Set::join() example without a separator string

<?php
$set = new \Ds\Set(["a", "b", "c", 1, 2, 3]);

var_dump($set->join());
?>

위 예제의 출력 예시:

string(11) "abc123"

User Contributed Notes

There are no user contributed notes for this page.


Returns a representation that can be converted to JSON.

  • ≪ Ds\Set::join
  • Ds\Set::last ≫
  • PHP Manual
  • Set
  • Returns a representation that can be converted to JSON.

Ds\Set::jsonSerialize

(PECL ds >= 1.0.0)

Ds\Set::jsonSerialize — Returns a representation that can be converted to JSON.

See JsonSerializable::jsonSerialize()

Note:

You should never need to call this directly.

User Contributed Notes

There are no user contributed notes for this page.


Returns the last value in the set.

  • ≪ Ds\Set::jsonSerialize
  • Ds\Set::merge ≫
  • PHP Manual
  • Set
  • Returns the last value in the set.

Ds\Set::last

(PECL ds >= 1.0.0)

Ds\Set::last — Returns the last value in the set.

설명

public void Ds\Set::last ( void )

Returns the last value in the set.

인수

이 함수는 인수가 없습니다.

반환값

The last value in the set.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Set::last() example

<?php
$set = new \Ds\Set([1, 2, 3]);
var_dump($set->last());
?>

위 예제의 출력 예시:

int(3)

User Contributed Notes

There are no user contributed notes for this page.


Returns the result of adding all given values to the set.

  • ≪ Ds\Set::last
  • Ds\Set::reduce ≫
  • PHP Manual
  • Set
  • Returns the result of adding all given values to the set.

Ds\Set::merge

(PECL ds >= 1.0.3)

Ds\Set::merge — Returns the result of adding all given values to the set.

설명

public Ds\Set Ds\Set::merge ( mixed $values )

Returns the result of adding all given values to the set.

인수

values

A traversable object or an array.

반환값

The result of adding all given values to the set, effectively the same as adding the values to a copy, then returning that copy.

Note:

The current instance won't be affected.

예제

Example #1 Ds\Set::merge() example

<?php
$set = new \Ds\Set([1, 2, 3]);

var_dump($set->merge([3, 4, 5]));
var_dump($set);
?>

위 예제의 출력 예시:

object(Ds\Set)#2 (6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
}
object(Ds\Set)#1 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Reduces the set to a single value using a callback function.

  • ≪ Ds\Set::merge
  • Ds\Set::remove ≫
  • PHP Manual
  • Set
  • Reduces the set to a single value using a callback function.

Ds\Set::reduce

(PECL ds >= 1.0.0)

Ds\Set::reduce — Reduces the set to a single value using a callback function.

설명

public mixed Ds\Set::reduce ( callable $callback [, mixed $initial ] )

Reduces the set to a single value using a callback function.

인수

callback
mixed callback ( mixed $carry , mixed $value )
carry

The return value of the previous callback, or initial if it's the first iteration.

value

The value of the current iteration.

initial

The initial value of the carry value. Can be NULL.

반환값

The return value of the final callback.

예제

Example #1 Ds\Set::reduce() with initial value example

<?php
$set = new \Ds\Set([1, 2, 3]);

$callback = function($carry, $value) {
    return $carry * $value;
};

var_dump($set->reduce($callback, 5));

// Iterations:
//
// $carry = $initial = 5
//
// $carry = $carry * 1 =  5
// $carry = $carry * 2 = 10
// $carry = $carry * 3 = 30
?>

위 예제의 출력 예시:

int(30)

Example #2 Ds\Set::reduce() without an initial value example

<?php
$set = new \Ds\Set([1, 2, 3]);

var_dump($set->reduce(function($carry, $value) {
    return $carry + $value + 5;
}));

// Iterations:
//
// $carry = $initial = null
//
// $carry = $carry + 1 + 5 =  6
// $carry = $carry + 2 + 5 = 13
// $carry = $carry + 3 + 5 = 21
?>

위 예제의 출력 예시:

int(21)

User Contributed Notes

There are no user contributed notes for this page.


Removes all given values from the set.

  • ≪ Ds\Set::reduce
  • Ds\Set::reverse ≫
  • PHP Manual
  • Set
  • Removes all given values from the set.

Ds\Set::remove

(PECL ds >= 1.0.0)

Ds\Set::remove — Removes all given values from the set.

설명

public void Ds\Set::remove ([ mixed $...values ] )

Removes all given values from the set, ignoring any that are not in the set.

인수

values

The values to remove.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Set::remove() example

<?php
$set = new \Ds\Set([1, 2, 3, 4, 5]);

$set->remove(1);            // Remove 1
$set->remove(1, 2);         // Can't find 1, but remove 2
$set->remove(...[3, 4]);    // Remove 3 and 4

var_dump($set);
?>

위 예제의 출력 예시:

object(Ds\Set)#1 (1) {
  [0]=>
  int(5)
}

User Contributed Notes

There are no user contributed notes for this page.


Reverses the set in-place.

  • ≪ Ds\Set::remove
  • Ds\Set::reversed ≫
  • PHP Manual
  • Set
  • Reverses the set in-place.

Ds\Set::reverse

(PECL ds >= 1.0.0)

Ds\Set::reverse — Reverses the set in-place.

설명

public void Ds\Set::reverse ( void )

Reverses the set in-place.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Set::reverse() example

<?php
$set = new \Ds\Set(["a", "b", "c"]);
$set->reverse();

print_r($set);
?>

위 예제의 출력 예시:

Ds\Set Object
(
    [0] => c
    [1] => b
    [2] => a
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a reversed copy.

  • ≪ Ds\Set::reverse
  • Ds\Set::slice ≫
  • PHP Manual
  • Set
  • Returns a reversed copy.

Ds\Set::reversed

(PECL ds >= 1.0.0)

Ds\Set::reversed — Returns a reversed copy.

설명

public Ds\Set Ds\Set::reversed ( void )

Returns a reversed copy of the set.

인수

이 함수는 인수가 없습니다.

반환값

A reversed copy of the set.

Note:

The current instance is not affected.

예제

Example #1 Ds\Set::reversed() example

<?php
$set = new \Ds\Set(["a", "b", "c"]);

print_r($set->reversed());
print_r($set);
?>

위 예제의 출력 예시:

Ds\Set Object
(
    [0] => c
    [1] => b
    [2] => a
)
Ds\Set Object
(
    [0] => a
    [1] => b
    [2] => c
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a sub-set of a given range.

  • ≪ Ds\Set::reversed
  • Ds\Set::sort ≫
  • PHP Manual
  • Set
  • Returns a sub-set of a given range.

Ds\Set::slice

(PECL ds >= 1.0.0)

Ds\Set::slice — Returns a sub-set of a given range.

설명

public Ds\Set Ds\Set::slice ( int $index [, int $length ] )

Creates a sub-set of a given range.

인수

index

The index at which the sub-set starts.

If positive, the set will start at that index in the set. If negative, the set will start that far from the end.

length

If a length is given and is positive, the resulting set will have up to that many values in it. If the length results in an overflow, only values up to the end of the set will be included. If a length is given and is negative, the set will stop that many values from the end. If a length is not provided, the resulting set will contain all values between the index and the end of the set.

반환값

A sub-set of the given range.

예제

Example #1 Ds\Set::slice() example

<?php
$set = new \Ds\Set(["a", "b", "c", "d", "e"]);

// Slice from 2 onwards
print_r($set->slice(2));

// Slice from 1, for a length of 3
print_r($set->slice(1, 3));

// Slice from 1 onwards
print_r($set->slice(1));

// Slice from 2 from the end onwards
print_r($set->slice(-2));

// Slice from 1 to 1 from the end
print_r($set->slice(1, -1));
?>

위 예제의 출력 예시:

Ds\Set Object
(
    [0] => c
    [1] => d
    [2] => e
)
Ds\Set Object
(
    [0] => b
    [1] => c
    [2] => d
)
Ds\Set Object
(
    [0] => b
    [1] => c
    [2] => d
    [3] => e
)
Ds\Set Object
(
    [0] => d
    [1] => e
)
Ds\Set Object
(
    [0] => b
    [1] => c
    [2] => d
)

User Contributed Notes

There are no user contributed notes for this page.


Sorts the set in-place.

  • ≪ Ds\Set::slice
  • Ds\Set::sorted ≫
  • PHP Manual
  • Set
  • Sorts the set in-place.

Ds\Set::sort

(PECL ds >= 1.0.0)

Ds\Set::sort — Sorts the set in-place.

설명

public void Ds\Set::sort ([ callable $comparator ] )

Sorts the set in-place, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Set::sort() example

<?php
$set = new \Ds\Set([4, 5, 1, 3, 2]);
$set->sort();

print_r($set);
?>

위 예제의 출력 예시:

Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example #2 Ds\Set::sort() example using a comparator

<?php
$set = new \Ds\Set([4, 5, 1, 3, 2]);

$set->sort(function($a, $b) {
    return $b <=> $a;
});

print_r($set);
?>

위 예제의 출력 예시:

Ds\Set Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a sorted copy.

  • ≪ Ds\Set::sort
  • Ds\Set::sum ≫
  • PHP Manual
  • Set
  • Returns a sorted copy.

Ds\Set::sorted

(PECL ds >= 1.0.0)

Ds\Set::sorted — Returns a sorted copy.

설명

public Ds\Set Ds\Set::sorted ([ callable $comparator ] )

Returns a sorted copy, using an optional comparator function.

인수

comparator

이 비교 함수는 첫번째 인수가 두번째 인수보다 작거나, 같거나, 클 경우에 각각 0보다 작거나, 같거나, 큰 정수를 반환해야 합니다.

int callback ( mixed $a, mixed $b )
Caution

Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

반환값

Returns a sorted copy of the set.

예제

Example #1 Ds\Set::sorted() example

<?php
$set = new \Ds\Set([4, 5, 1, 3, 2]);

print_r($set->sorted());
?>

위 예제의 출력 예시:

Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Example #2 Ds\Set::sorted() example using a comparator

<?php
$set = new \Ds\Set([4, 5, 1, 3, 2]);

$sorted = $set->sorted(function($a, $b) {
    return $b <=> $a;
});

print_r($sorted);
?>

위 예제의 출력 예시:

Ds\Set Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the sum of all values in the set.

  • ≪ Ds\Set::sorted
  • Ds\Set::toArray ≫
  • PHP Manual
  • Set
  • Returns the sum of all values in the set.

Ds\Set::sum

(PECL ds >= 1.0.0)

Ds\Set::sum — Returns the sum of all values in the set.

설명

public number Ds\Set::sum ( void )

Returns the sum of all values in the set.

Note:

Arrays and objects are considered equal to zero when calculating the sum.

인수

이 함수는 인수가 없습니다.

반환값

The sum of all the values in the set as either a float or int depending on the values in the set.

예제

Example #1 Ds\Set::sum() integer example

<?php
$set = new \Ds\Set([1, 2, 3]);
var_dump($set->sum());
?>

위 예제의 출력 예시:

int(6)

Example #2 Ds\Set::sum() float example

<?php
$set = new \Ds\Set([1, 2.5, 3]);
var_dump($set->sum());
?>

위 예제의 출력 예시:

float(6.5)

User Contributed Notes

There are no user contributed notes for this page.


Converts the set to an array.

  • ≪ Ds\Set::sum
  • Ds\Set::union ≫
  • PHP Manual
  • Set
  • Converts the set to an array.

Ds\Set::toArray

(PECL ds >= 1.0.0)

Ds\Set::toArray — Converts the set to an array.

설명

public array Ds\Set::toArray ( void )

Converts the set to an array.

Note:

Casting to an array is not supported yet.

인수

이 함수는 인수가 없습니다.

반환값

An array containing all the values in the same order as the set.

예제

Example #1 Ds\Set::toArray() example

<?php
$set = new \Ds\Set([1, 2, 3]);

var_dump($set->toArray());
?>

위 예제의 출력 예시:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Creates a new set using values from the current instance and another set.

  • ≪ Ds\Set::toArray
  • Ds\Set::xor ≫
  • PHP Manual
  • Set
  • Creates a new set using values from the current instance and another set.

Ds\Set::union

(PECL ds >= 1.0.0)

Ds\Set::union — Creates a new set using values from the current instance and another set.

설명

public Ds\Set Ds\Set::union ( Ds\Set $set )

Creates a new set that contains the values of the current instance as well as the values of another set.

A ∪ B = {x: x ∈ A ∨ x ∈ B}

인수

set

The other set, to combine with the current instance.

반환값

A new set containing all the values of the current instance as well as another set.

See Also

  • » Union on Wikipedia

예제

Example #1 Ds\Set::union() example

<?php
$a = new \Ds\Set([1, 2, 3]);
$b = new \Ds\Set([3, 4, 5]);

var_dump($a->union($b));
?>

위 예제의 출력 예시:

object(Ds\Set)#3 (5) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
}

User Contributed Notes

There are no user contributed notes for this page.


Creates a new set using values in either the current instance or in another set, but not in both.

  • ≪ Ds\Set::union
  • Stack ≫
  • PHP Manual
  • Set
  • Creates a new set using values in either the current instance or in another set, but not in both.

Ds\Set::xor

(PECL ds >= 1.0.0)

Ds\Set::xor — Creates a new set using values in either the current instance or in another set, but not in both.

설명

public Ds\Set Ds\Set::xor ( Ds\Set $set )

Creates a new set containing values in the current instance as well as another set, but not in both.

A ? B = {x : x ∈ (A \ B) ∪ (B \ A)}

인수

set

The other set.

반환값

A new set containing values in the current instance as well as another set, but not in both.

See Also

  • » Symmetric Difference on Wikipedia

예제

Example #1 Ds\Set::xor() example

<?php
$a = new \Ds\Set([1, 2, 3]);
$b = new \Ds\Set([3, 4, 5]);

var_dump($a->xor($b));
?>

위 예제의 출력 예시:

object(Ds\Set)#3 (4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(4)
  [3]=>
  int(5)
}

User Contributed Notes

There are no user contributed notes for this page.


The Stack class

(No version information available, might only be in Git)

소개

A Stack is a "last in, first out" or "LIFO" collection that only allows access to the value at the top of the structure and iterates in that order, destructively.

Uses a Ds\Vector internally.

클래스 개요

Ds\Stack implements Ds\Collection {
/* 메소드 */
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public Ds\Stack copy ( void )
public bool isEmpty ( void )
public mixed peek ( void )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public array toArray ( void )
}

Table of Contents

  • Ds\Stack::allocate — Allocates enough memory for a required capacity.
  • Ds\Stack::capacity — Returns the current capacity.
  • Ds\Stack::clear — Removes all values.
  • Ds\Stack::__construct — Creates a new instance.
  • Ds\Stack::copy — Returns a shallow copy of the stack.
  • Ds\Stack::count — Returns the number of values in the stack.
  • Ds\Stack::isEmpty — Returns whether the stack is empty
  • Ds\Stack::jsonSerialize — Returns a representation that can be converted to JSON.
  • Ds\Stack::peek — Returns the value at the top of the stack.
  • Ds\Stack::pop — Removes and returns the value at the top of the stack.
  • Ds\Stack::push — Pushes values onto the stack.
  • Ds\Stack::toArray — Converts the stack to an array.

User Contributed Notes

There are no user contributed notes for this page.


Allocates enough memory for a required capacity.

  • ≪ Stack
  • Ds\Stack::capacity ≫
  • PHP Manual
  • Stack
  • Allocates enough memory for a required capacity.

Ds\Stack::allocate

(PECL ds >= 1.0.0)

Ds\Stack::allocate — Allocates enough memory for a required capacity.

설명

public void Ds\Stack::allocate ( int $capacity )

Ensures that enough memory is allocated for a required capacity. This removes the need to reallocate the internal as values are added.

인수

capacity

The number of values for which capacity should be allocated.

Note:

Capacity will stay the same if this value is less than or equal to the current capacity.

반환값

값을 반환하지 않습니다.

User Contributed Notes

There are no user contributed notes for this page.


Returns the current capacity.

  • ≪ Ds\Stack::allocate
  • Ds\Stack::clear ≫
  • PHP Manual
  • Stack
  • Returns the current capacity.

Ds\Stack::capacity

(PECL ds >= 1.0.0)

Ds\Stack::capacity — Returns the current capacity.

설명

public int Ds\Stack::capacity ( void )

Returns the current capacity.

인수

이 함수는 인수가 없습니다.

반환값

The current capacity.

User Contributed Notes

There are no user contributed notes for this page.


Removes all values.

  • ≪ Ds\Stack::capacity
  • Ds\Stack::__construct ≫
  • PHP Manual
  • Stack
  • Removes all values.

Ds\Stack::clear

(PECL ds >= 1.0.0)

Ds\Stack::clear — Removes all values.

설명

public void Ds\Stack::clear ( void )

Removes all values from the stack.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Stack::clear() example

<?php
$stack = new \Ds\Stack([1, 2, 3]);
print_r($stack);

$stack->clear();
print_r($stack);
?>

위 예제의 출력 예시:

Ds\Stack Object
(
    [0] => 3
    [1] => 2
    [2] => 1
)
Ds\Stack Object
(
)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new instance.

  • ≪ Ds\Stack::clear
  • Ds\Stack::copy ≫
  • PHP Manual
  • Stack
  • Creates a new instance.

Ds\Stack::__construct

(PECL ds >= 1.0.0)

Ds\Stack::__construct — Creates a new instance.

설명

public Ds\Stack::__construct ([ mixed $values ] )

Creates a new instance, using either a traversable object or an array for the initial values.

인수

values

A traversable object or an array to use for the initial values.

예제

Example #1 Ds\Stack::__construct() example

<?php
$stack = new \Ds\Stack();
print_r($stack);

$stack = new \Ds\Stack([1, 2, 3]);
print_r($stack);
?>

위 예제의 출력 예시:

Ds\Stack Object
(
)
Ds\Stack Object
(
    [0] => 3
    [1] => 2
    [2] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns a shallow copy of the stack.

  • ≪ Ds\Stack::__construct
  • Ds\Stack::count ≫
  • PHP Manual
  • Stack
  • Returns a shallow copy of the stack.

Ds\Stack::copy

(PECL ds >= 1.0.0)

Ds\Stack::copy — Returns a shallow copy of the stack.

설명

public Ds\Stack Ds\Stack::copy ( void )

Returns a shallow copy of the stack.

인수

이 함수는 인수가 없습니다.

반환값

Returns a shallow copy of the stack.

예제

Example #1 Ds\Stack::copy() example

<?php
$a = new \Ds\Stack([1, 2, 3]);
$b = $a->copy();

// Updating the copy doesn't affect the original
$b->push(4);

print_r($a);
print_r($b);
?>

위 예제의 출력 예시:

Ds\Stack Object
(
    [0] => 3
    [1] => 2
    [2] => 1
)
Ds\Stack Object
(
    [0] => 4
    [1] => 3
    [2] => 2
    [3] => 1
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of values in the stack.

  • ≪ Ds\Stack::copy
  • Ds\Stack::isEmpty ≫
  • PHP Manual
  • Stack
  • Returns the number of values in the stack.

Ds\Stack::count

(PECL ds >= 1.0.0)

Ds\Stack::count — Returns the number of values in the stack.

See Countable::count()

User Contributed Notes

There are no user contributed notes for this page.


Returns whether the stack is empty

  • ≪ Ds\Stack::count
  • Ds\Stack::jsonSerialize ≫
  • PHP Manual
  • Stack
  • Returns whether the stack is empty

Ds\Stack::isEmpty

(PECL ds >= 1.0.0)

Ds\Stack::isEmpty — Returns whether the stack is empty

설명

public bool Ds\Stack::isEmpty ( void )

Returns whether the stack is empty.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the stack is empty, FALSE otherwise.

예제

Example #1 Ds\Stack::isEmpty() example

<?php
$a = new \Ds\Stack([1, 2, 3]);
$b = new \Ds\Stack();

var_dump($a->isEmpty());
var_dump($b->isEmpty());
?>

위 예제의 출력 예시:

bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Returns a representation that can be converted to JSON.

  • ≪ Ds\Stack::isEmpty
  • Ds\Stack::peek ≫
  • PHP Manual
  • Stack
  • Returns a representation that can be converted to JSON.

Ds\Stack::jsonSerialize

(PECL ds >= 1.0.0)

Ds\Stack::jsonSerialize — Returns a representation that can be converted to JSON.

See JsonSerializable::jsonSerialize()

Note:

You should never need to call this directly.

User Contributed Notes

There are no user contributed notes for this page.


Returns the value at the top of the stack.

  • ≪ Ds\Stack::jsonSerialize
  • Ds\Stack::pop ≫
  • PHP Manual
  • Stack
  • Returns the value at the top of the stack.

Ds\Stack::peek

(PECL ds >= 1.0.0)

Ds\Stack::peek — Returns the value at the top of the stack.

설명

public mixed Ds\Stack::peek ( void )

Returns the value at the top of the stack, but does not remove it.

인수

이 함수는 인수가 없습니다.

반환값

The value at the top of the stack.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Stack::peek() example

<?php
$stack = new \Ds\Stack();

$stack->push("a");
$stack->push("b");
$stack->push("c");

var_dump($stack->peek());
?>

위 예제의 출력 예시:

string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns the value at the top of the stack.

  • ≪ Ds\Stack::peek
  • Ds\Stack::push ≫
  • PHP Manual
  • Stack
  • Removes and returns the value at the top of the stack.

Ds\Stack::pop

(PECL ds >= 1.0.0)

Ds\Stack::pop — Removes and returns the value at the top of the stack.

설명

public mixed Ds\Stack::pop ( void )

Removes and returns the value at the top of the stack.

인수

이 함수는 인수가 없습니다.

반환값

The removed value which was at the top of the stack.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Stack::pop() example

<?php
$stack = new \Ds\Stack();

$stack->push("a");
$stack->push("b");
$stack->push("c");

var_dump($stack->pop());
var_dump($stack->pop());
var_dump($stack->pop());
?>

위 예제의 출력 예시:

string(1) "c"
string(1) "b"
string(1) "a"

User Contributed Notes

There are no user contributed notes for this page.


Pushes values onto the stack.

  • ≪ Ds\Stack::pop
  • Ds\Stack::toArray ≫
  • PHP Manual
  • Stack
  • Pushes values onto the stack.

Ds\Stack::push

(PECL ds >= 1.0.0)

Ds\Stack::push — Pushes values onto the stack.

설명

public void Ds\Stack::push ([ mixed $...values ] )

Pushes values onto the stack.

인수

values

The values to push onto the stack.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Stack::push() example

<?php
$stack = new \Ds\Stack();

$stack->push("a");
$stack->push("b");
$stack->push("c", "d");
$stack->push(...["e", "f"]);

print_r($stack);
?>

위 예제의 출력 예시:

Ds\Stack Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)

User Contributed Notes

There are no user contributed notes for this page.


Converts the stack to an array.

  • ≪ Ds\Stack::push
  • Queue ≫
  • PHP Manual
  • Stack
  • Converts the stack to an array.

Ds\Stack::toArray

(PECL ds >= 1.0.0)

Ds\Stack::toArray — Converts the stack to an array.

설명

public array Ds\Stack::toArray ( void )

Converts the stack to an array.

Note:

Casting to an array is not supported yet.

인수

이 함수는 인수가 없습니다.

반환값

An array containing all the values in the same order as the stack.

예제

Example #1 Ds\Stack::toArray() example

<?php
$stack = new \Ds\Stack([1, 2, 3]);

var_dump($stack->toArray());
?>

위 예제의 출력 예시:

array(3) {
  [0]=>
  int(3)
  [1]=>
  int(2)
  [2]=>
  int(1)
}

User Contributed Notes

There are no user contributed notes for this page.


The Queue class

(No version information available, might only be in Git)

소개

A Queue is a "first in, first out" or "FIFO" collection that only allows access to the value at the front of the queue and iterates in that order, destructively.

클래스 개요

Ds\Queue implements Ds\Collection {
/* Constants */
const int MIN_CAPACITY = 8 ;
/* 메소드 */
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public Ds\Queue copy ( void )
public bool isEmpty ( void )
public mixed peek ( void )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public array toArray ( void )
}

예약 상수

Ds\Queue::MIN_CAPACITY

Table of Contents

  • Ds\Queue::allocate — Allocates enough memory for a required capacity.
  • Ds\Queue::capacity — Returns the current capacity.
  • Ds\Queue::clear — Removes all values.
  • Ds\Queue::__construct — Creates a new instance.
  • Ds\Queue::copy — Returns a shallow copy of the queue.
  • Ds\Queue::count — Returns the number of values in the queue.
  • Ds\Queue::isEmpty — Returns whether the queue is empty
  • Ds\Queue::jsonSerialize — Returns a representation that can be converted to JSON.
  • Ds\Queue::peek — Returns the value at the front of the queue.
  • Ds\Queue::pop — Removes and returns the value at the front of the queue.
  • Ds\Queue::push — Pushes values into the queue.
  • Ds\Queue::toArray — Converts the queue to an array.

User Contributed Notes

There are no user contributed notes for this page.


Allocates enough memory for a required capacity.

  • ≪ Queue
  • Ds\Queue::capacity ≫
  • PHP Manual
  • Queue
  • Allocates enough memory for a required capacity.

Ds\Queue::allocate

(PECL ds >= 1.0.0)

Ds\Queue::allocate — Allocates enough memory for a required capacity.

설명

public void Ds\Queue::allocate ( int $capacity )

Ensures that enough memory is allocated for a required capacity. This removes the need to reallocate the internal as values are added.

Note:

Capacity will always be rounded up to the nearest power of 2.

인수

capacity

The number of values for which capacity should be allocated.

Note:

Capacity will stay the same if this value is less than or equal to the current capacity.

Note:

Capacity will always be rounded up to the nearest power of 2.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Queue::allocate() example

<?php
$queue = new \Ds\Queue();
var_dump($queue->capacity());

$queue->allocate(100);
var_dump($queue->capacity());
?>

위 예제의 출력 예시:

int(8)
int(128)

User Contributed Notes

There are no user contributed notes for this page.


Returns the current capacity.

  • ≪ Ds\Queue::allocate
  • Ds\Queue::clear ≫
  • PHP Manual
  • Queue
  • Returns the current capacity.

Ds\Queue::capacity

(PECL ds >= 1.0.0)

Ds\Queue::capacity — Returns the current capacity.

설명

public int Ds\Queue::capacity ( void )

Returns the current capacity.

인수

이 함수는 인수가 없습니다.

반환값

The current capacity.

예제

Example #1 Ds\Queue::capacity() example

<?php
$queue = new \Ds\Queue();
var_dump($queue->capacity());

$queue->push(...range(1, 50));
var_dump($queue->capacity());
?>

위 예제의 출력 예시:

int(8)
int(64)

User Contributed Notes

There are no user contributed notes for this page.


Removes all values.

  • ≪ Ds\Queue::capacity
  • Ds\Queue::__construct ≫
  • PHP Manual
  • Queue
  • Removes all values.

Ds\Queue::clear

(PECL ds >= 1.0.0)

Ds\Queue::clear — Removes all values.

설명

public void Ds\Queue::clear ( void )

Removes all values from the queue.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Queue::clear() example

<?php
$queue = new \Ds\Queue([1, 2, 3]);
print_r($queue);

$queue->clear();
print_r($queue);
?>

위 예제의 출력 예시:

Ds\Queue Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Queue Object
(
)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new instance.

  • ≪ Ds\Queue::clear
  • Ds\Queue::copy ≫
  • PHP Manual
  • Queue
  • Creates a new instance.

Ds\Queue::__construct

(PECL ds >= 1.0.0)

Ds\Queue::__construct — Creates a new instance.

설명

public Ds\Queue::__construct ([ mixed $values ] )

Creates a new instance, using either a traversable object or an array for the initial values.

인수

values

A traversable object or an array to use for the initial values.

예제

Example #1 Ds\Queue::__construct() example

<?php
$queue = new \Ds\Queue();
var_dump($queue);

$queue = new \Ds\Queue([1, 2, 3]);
var_dump($queue);
?>

위 예제의 출력 예시:

object(Ds\Queue)#2 (0) {
}
object(Ds\Queue)#2 (3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


Returns a shallow copy of the queue.

  • ≪ Ds\Queue::__construct
  • Ds\Queue::count ≫
  • PHP Manual
  • Queue
  • Returns a shallow copy of the queue.

Ds\Queue::copy

(PECL ds >= 1.0.0)

Ds\Queue::copy — Returns a shallow copy of the queue.

설명

public Ds\Queue Ds\Queue::copy ( void )

Returns a shallow copy of the queue.

인수

이 함수는 인수가 없습니다.

반환값

Returns a shallow copy of the queue.

예제

Example #1 Ds\Queue::copy() example

<?php
$a = new \Ds\Queue([1, 2, 3]);
$b = $a->copy();

// Updating the copy doesn't affect the original
$b->push(4);

print_r($a);
print_r($b);
?>

위 예제의 출력 예시:

Ds\Queue Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Ds\Queue Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of values in the queue.

  • ≪ Ds\Queue::copy
  • Ds\Queue::isEmpty ≫
  • PHP Manual
  • Queue
  • Returns the number of values in the queue.

Ds\Queue::count

(PECL ds >= 1.0.0)

Ds\Queue::count — Returns the number of values in the queue.

See Countable::count()

User Contributed Notes

There are no user contributed notes for this page.


Returns whether the queue is empty

  • ≪ Ds\Queue::count
  • Ds\Queue::jsonSerialize ≫
  • PHP Manual
  • Queue
  • Returns whether the queue is empty

Ds\Queue::isEmpty

(PECL ds >= 1.0.0)

Ds\Queue::isEmpty — Returns whether the queue is empty

설명

public bool Ds\Queue::isEmpty ( void )

Returns whether the queue is empty.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the queue is empty, FALSE otherwise.

예제

Example #1 Ds\Queue::isEmpty() example

<?php
$a = new \Ds\Queue([1, 2, 3]);
$b = new \Ds\Queue();

var_dump($a->isEmpty());
var_dump($b->isEmpty());
?>

위 예제의 출력 예시:

bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Returns a representation that can be converted to JSON.

  • ≪ Ds\Queue::isEmpty
  • Ds\Queue::peek ≫
  • PHP Manual
  • Queue
  • Returns a representation that can be converted to JSON.

Ds\Queue::jsonSerialize

(PECL ds >= 1.0.0)

Ds\Queue::jsonSerialize — Returns a representation that can be converted to JSON.

See JsonSerializable::jsonSerialize()

Note:

You should never need to call this directly.

User Contributed Notes

There are no user contributed notes for this page.


Returns the value at the front of the queue.

  • ≪ Ds\Queue::jsonSerialize
  • Ds\Queue::pop ≫
  • PHP Manual
  • Queue
  • Returns the value at the front of the queue.

Ds\Queue::peek

(PECL ds >= 1.0.0)

Ds\Queue::peek — Returns the value at the front of the queue.

설명

public mixed Ds\Queue::peek ( void )

Returns the value at the front of the queue, but does not remove it.

인수

이 함수는 인수가 없습니다.

반환값

The value at the front of the queue.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Queue::peek() example

<?php
$queue = new \Ds\Queue();

$queue->push("a");
$queue->push("b");
$queue->push("c");

var_dump($queue->peek());
?>

위 예제의 출력 예시:

string(1) "a"

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns the value at the front of the queue.

  • ≪ Ds\Queue::peek
  • Ds\Queue::push ≫
  • PHP Manual
  • Queue
  • Removes and returns the value at the front of the queue.

Ds\Queue::pop

(PECL ds >= 1.0.0)

Ds\Queue::pop — Removes and returns the value at the front of the queue.

설명

public mixed Ds\Queue::pop ( void )

Removes and returns the value at the front of the queue.

인수

이 함수는 인수가 없습니다.

반환값

The removed value which was at the front of the queue.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\Queue::pop() example

<?php
$queue = new \Ds\Queue();

$queue->push("a");
$queue->push("b");
$queue->push("c");

var_dump($queue->pop());
var_dump($queue->pop());
var_dump($queue->pop());
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Pushes values into the queue.

  • ≪ Ds\Queue::pop
  • Ds\Queue::toArray ≫
  • PHP Manual
  • Queue
  • Pushes values into the queue.

Ds\Queue::push

(PECL ds >= 1.0.0)

Ds\Queue::push — Pushes values into the queue.

설명

public void Ds\Queue::push ([ mixed $...values ] )

Pushes values into the queue.

인수

values

The values to push into the queue.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\Queue::push() example

<?php
$queue = new \Ds\Queue();

$queue->push("a");
$queue->push("b");
$queue->push("c", "d");
$queue->push(...["e", "f"]);

print_r($queue);
?>

위 예제의 출력 예시:

Ds\Queue Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)

User Contributed Notes

There are no user contributed notes for this page.


Converts the queue to an array.

  • ≪ Ds\Queue::push
  • PriorityQueue ≫
  • PHP Manual
  • Queue
  • Converts the queue to an array.

Ds\Queue::toArray

(PECL ds >= 1.0.0)

Ds\Queue::toArray — Converts the queue to an array.

설명

public array Ds\Queue::toArray ( void )

Converts the queue to an array.

Note:

Casting to an array is not supported yet.

Note:

This method is not destructive.

인수

이 함수는 인수가 없습니다.

반환값

An array containing all the values in the same order as the queue.

예제

Example #1 Ds\Queue::toArray() example

<?php
$queue = new \Ds\Queue([1, 2, 3]);

var_dump($queue->toArray());
?>

위 예제의 출력 예시:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

User Contributed Notes

There are no user contributed notes for this page.


The PriorityQueue class

(No version information available, might only be in Git)

소개

A PriorityQueue is very similar to a Queue. Values are pushed into the queue with an assigned priority, and the value with the highest priority will always be at the front of the queue.

Implemented using a max heap.

Note:

"First in, first out" ordering is preserved for values with the same priority.

Note:

Iterating over a PriorityQueue is destructive, equivalent to successive pop operations until the queue is empty.

클래스 개요

Ds\PriorityQueue implements Ds\Collection {
/* Constants */
const int MIN_CAPACITY = 8 ;
/* 메소드 */
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public Ds\PriorityQueue copy ( void )
public bool isEmpty ( void )
public mixed peek ( void )
public mixed pop ( void )
public void push ( mixed $value , int $priority )
public array toArray ( void )
}

예약 상수

Ds\PriorityQueue::MIN_CAPACITY

Table of Contents

  • Ds\PriorityQueue::allocate — Allocates enough memory for a required capacity.
  • Ds\PriorityQueue::capacity — Returns the current capacity.
  • Ds\PriorityQueue::clear — Removes all values.
  • Ds\PriorityQueue::__construct — Creates a new instance.
  • Ds\PriorityQueue::copy — Returns a shallow copy of the queue.
  • Ds\PriorityQueue::count — Returns the number of values in the queue.
  • Ds\PriorityQueue::isEmpty — Returns whether the queue is empty
  • Ds\PriorityQueue::jsonSerialize — Returns a representation that can be converted to JSON.
  • Ds\PriorityQueue::peek — Returns the value at the front of the queue.
  • Ds\PriorityQueue::pop — Removes and returns the value with the highest priority.
  • Ds\PriorityQueue::push — Pushes values into the queue.
  • Ds\PriorityQueue::toArray — Converts the queue to an array.

User Contributed Notes

There are no user contributed notes for this page.


Allocates enough memory for a required capacity.

  • ≪ PriorityQueue
  • Ds\PriorityQueue::capacity ≫
  • PHP Manual
  • PriorityQueue
  • Allocates enough memory for a required capacity.

Ds\PriorityQueue::allocate

(PECL ds >= 1.0.0)

Ds\PriorityQueue::allocate — Allocates enough memory for a required capacity.

설명

public void Ds\PriorityQueue::allocate ( int $capacity )

Ensures that enough memory is allocated for a required capacity. This removes the need to reallocate the internal as values are added.

인수

capacity

The number of values for which capacity should be allocated.

Note:

Capacity will stay the same if this value is less than or equal to the current capacity.

Note:

Capacity will always be rounded up to the nearest power of 2.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\PriorityQueue::allocate() example

<?php
$queue = new \Ds\PriorityQueue();
var_dump($queue->capacity());

$queue->allocate(100);
var_dump($queue->capacity());
?>

위 예제의 출력 예시:

int(8)
int(128)

User Contributed Notes

There are no user contributed notes for this page.


Returns the current capacity.

  • ≪ Ds\PriorityQueue::allocate
  • Ds\PriorityQueue::clear ≫
  • PHP Manual
  • PriorityQueue
  • Returns the current capacity.

Ds\PriorityQueue::capacity

(PECL ds >= 1.0.0)

Ds\PriorityQueue::capacity — Returns the current capacity.

설명

public int Ds\PriorityQueue::capacity ( void )

Returns the current capacity.

인수

이 함수는 인수가 없습니다.

반환값

The current capacity.

예제

Example #1 Ds\PriorityQueue::capacity() example

<?php
$queue = new \Ds\PriorityQueue();
var_dump($queue->capacity());
?>

위 예제의 출력 예시:

int(8)

User Contributed Notes

There are no user contributed notes for this page.


Removes all values.

  • ≪ Ds\PriorityQueue::capacity
  • Ds\PriorityQueue::__construct ≫
  • PHP Manual
  • PriorityQueue
  • Removes all values.

Ds\PriorityQueue::clear

(PECL ds >= 1.0.0)

Ds\PriorityQueue::clear — Removes all values.

설명

public void Ds\PriorityQueue::clear ( void )

Removes all values from the queue.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\PriorityQueue::clear() example

<?php
$queue = new \Ds\PriorityQueue();

$queue->push("a",  5);
$queue->push("b", 15);
$queue->push("c", 10);

$queue->clear();
print_r($queue);
?>

위 예제의 출력 예시:

Ds\PriorityQueue Object
(
)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new instance.

  • ≪ Ds\PriorityQueue::clear
  • Ds\PriorityQueue::copy ≫
  • PHP Manual
  • PriorityQueue
  • Creates a new instance.

Ds\PriorityQueue::__construct

(PECL ds >= 1.0.0)

Ds\PriorityQueue::__construct — Creates a new instance.

설명

public Ds\PriorityQueue::__construct ( void )

Creates a new instance.

예제

Example #1 Ds\PriorityQueue::__construct() example

<?php
$queue = new \Ds\PriorityQueue();
var_dump($queue);
?>

위 예제의 출력 예시:

object(Ds\PriorityQueue)#1 (0) {
}

User Contributed Notes

There are no user contributed notes for this page.


Returns a shallow copy of the queue.

  • ≪ Ds\PriorityQueue::__construct
  • Ds\PriorityQueue::count ≫
  • PHP Manual
  • PriorityQueue
  • Returns a shallow copy of the queue.

Ds\PriorityQueue::copy

(PECL ds >= 1.0.0)

Ds\PriorityQueue::copy — Returns a shallow copy of the queue.

설명

public Ds\PriorityQueue Ds\PriorityQueue::copy ( void )

Returns a shallow copy of the queue.

인수

이 함수는 인수가 없습니다.

반환값

Returns a shallow copy of the queue.

예제

Example #1 Ds\PriorityQueue::copy() example

<?php
$queue = new \Ds\PriorityQueue();

$queue->push("a",  5);
$queue->push("b", 15);
$queue->push("c", 10);

print_r($queue->copy());
?>

위 예제의 출력 예시:

Ds\PriorityQueue Object
(
    [0] => b
    [1] => c
    [2] => a
)

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of values in the queue.

  • ≪ Ds\PriorityQueue::copy
  • Ds\PriorityQueue::isEmpty ≫
  • PHP Manual
  • PriorityQueue
  • Returns the number of values in the queue.

Ds\PriorityQueue::count

(PECL ds >= 1.0.0)

Ds\PriorityQueue::count — Returns the number of values in the queue.

See Countable::count()

User Contributed Notes

There are no user contributed notes for this page.


Returns whether the queue is empty

  • ≪ Ds\PriorityQueue::count
  • Ds\PriorityQueue::jsonSerialize ≫
  • PHP Manual
  • PriorityQueue
  • Returns whether the queue is empty

Ds\PriorityQueue::isEmpty

(PECL ds >= 1.0.0)

Ds\PriorityQueue::isEmpty — Returns whether the queue is empty

설명

public bool Ds\PriorityQueue::isEmpty ( void )

Returns whether the queue is empty.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the queue is empty, FALSE otherwise.

예제

Example #1 Ds\PriorityQueue::isEmpty() example

<?php
$a = new \Ds\PriorityQueue();
$b = new \Ds\PriorityQueue();

$a->push("a",  5);
$a->push("b", 15);
$a->push("c", 10);

var_dump($a->isEmpty());
var_dump($b->isEmpty());
?>

위 예제의 출력 예시:

bool(false)
bool(true)

User Contributed Notes

There are no user contributed notes for this page.


Returns a representation that can be converted to JSON.

  • ≪ Ds\PriorityQueue::isEmpty
  • Ds\PriorityQueue::peek ≫
  • PHP Manual
  • PriorityQueue
  • Returns a representation that can be converted to JSON.

Ds\PriorityQueue::jsonSerialize

(PECL ds >= 1.0.0)

Ds\PriorityQueue::jsonSerialize — Returns a representation that can be converted to JSON.

See JsonSerializable::jsonSerialize()

Note:

You should never need to call this directly.

User Contributed Notes

There are no user contributed notes for this page.


Returns the value at the front of the queue.

  • ≪ Ds\PriorityQueue::jsonSerialize
  • Ds\PriorityQueue::pop ≫
  • PHP Manual
  • PriorityQueue
  • Returns the value at the front of the queue.

Ds\PriorityQueue::peek

(PECL ds >= 1.0.0)

Ds\PriorityQueue::peek — Returns the value at the front of the queue.

설명

public mixed Ds\PriorityQueue::peek ( void )

Returns the value at the front of the queue, but does not remove it.

인수

이 함수는 인수가 없습니다.

반환값

The value at the front of the queue.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\PriorityQueue::peek() example

<?php
$queue = new \Ds\PriorityQueue();

$queue->push("a",  5);
$queue->push("b", 15);
$queue->push("c", 10);

var_dump($queue->peek());
?>

위 예제의 출력 예시:

string(1) "b"

User Contributed Notes

There are no user contributed notes for this page.


Removes and returns the value with the highest priority.

  • ≪ Ds\PriorityQueue::peek
  • Ds\PriorityQueue::push ≫
  • PHP Manual
  • PriorityQueue
  • Removes and returns the value with the highest priority.

Ds\PriorityQueue::pop

(PECL ds >= 1.0.0)

Ds\PriorityQueue::pop — Removes and returns the value with the highest priority.

설명

public mixed Ds\PriorityQueue::pop ( void )

Removes and returns the value at the front of the queue, ie. the value with the highest priority.

Note:

Values with equal priority fall back to FIFO (first in first out).

인수

이 함수는 인수가 없습니다.

반환값

The removed value which was at the front of the queue.

오류/예외

UnderflowException if empty.

예제

Example #1 Ds\PriorityQueue::pop() example

<?php
$queue = new \Ds\PriorityQueue();

$queue->push("a",  5);
$queue->push("b", 15);
$queue->push("c", 10);

print_r($queue->pop());
print_r($queue->pop());
print_r($queue->pop());
?>

위 예제의 출력 예시:

string(1) "a"
string(1) "b"
string(1) "c"

User Contributed Notes

There are no user contributed notes for this page.


Pushes values into the queue.

  • ≪ Ds\PriorityQueue::pop
  • Ds\PriorityQueue::toArray ≫
  • PHP Manual
  • PriorityQueue
  • Pushes values into the queue.

Ds\PriorityQueue::push

(PECL ds >= 1.0.0)

Ds\PriorityQueue::push — Pushes values into the queue.

설명

public void Ds\PriorityQueue::push ( mixed $value , int $priority )

Pushes a value with a given priority into the queue.

인수

value

The value to push into the queue.

priority

The priority associated with the value.

반환값

값을 반환하지 않습니다.

예제

Example #1 Ds\PriorityQueue::push() example

<?php
$queue = new \Ds\PriorityQueue();

$queue->push("a",  5);
$queue->push("b", 15);
$queue->push("c", 10);

print_r($queue->pop());
print_r($queue->pop());
print_r($queue->pop());
?>

위 예제의 출력 예시:

string(1) "b"
string(1) "c"
string(1) "a"

User Contributed Notes

There are no user contributed notes for this page.


Converts the queue to an array.

  • ≪ Ds\PriorityQueue::push
  • Filter ≫
  • PHP Manual
  • PriorityQueue
  • Converts the queue to an array.

Ds\PriorityQueue::toArray

(PECL ds >= 1.0.0)

Ds\PriorityQueue::toArray — Converts the queue to an array.

설명

public array Ds\PriorityQueue::toArray ( void )

Converts the queue to an array.

Note:

This method is not destructive.

Note:

Casting to an array is not supported yet.

인수

이 함수는 인수가 없습니다.

반환값

An array containing all the values in the same order as the queue.

예제

Example #1 Ds\PriorityQueue::toArray() example

<?php
$queue = new \Ds\PriorityQueue();

$queue->push("a",  5);
$queue->push("b", 15);
$queue->push("c", 10);

var_dump($queue->toArray());
?>

위 예제의 출력 예시:

array(3) {
  [0]=>
  string(1) "b"
  [1]=>
  string(1) "c"
  [2]=>
  string(1) "a"
}

User Contributed Notes

There are no user contributed notes for this page.


Data Filtering

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • Types of filters
    • Validate filters
    • Sanitize filters
    • Other filters
    • Filter flags
  • 예약 상수
  • 예제
    • Validation
    • Sanitization
  • Filter 함수 목록
    • filter_has_var — Checks if variable of specified type exists
    • filter_id — Returns the filter ID belonging to a named filter
    • filter_input_array — Gets external variables and optionally filters them
    • filter_input — Gets a specific external variable by name and optionally filters it
    • filter_list — Returns a list of all supported filters
    • filter_var_array — Gets multiple variables and optionally filters them
    • filter_var — Filters a variable with a specified filter

User Contributed Notes

There are no user contributed notes for this page.


소개

This extension filters data by either validating or sanitizing it. This is especially useful when the data source contains unknown (or foreign) data, like user supplied input. For example, this data may come from an HTML form.

There are two main types of filtering: validation and sanitization.

Validation is used to validate or check if the data meets certain qualifications. For example, passing in FILTER_VALIDATE_EMAIL will determine if the data is a valid email address, but will not change the data itself.

Sanitization will sanitize the data, so it may alter it by removing undesired characters. For example, passing in FILTER_SANITIZE_EMAIL will remove characters that are inappropriate for an email address to contain. That said, it does not validate the data.

Flags are optionally used with both validation and sanitization to tweak behaviour according to need. For example, passing in FILTER_FLAG_PATH_REQUIRED while filtering an URL will require a path (like /foo in http://example.org/foo) to be present.

User Contributed Notes

There are no user contributed notes for this page.


설치

The filter extension is enabled by default as of PHP 5.2.0. Before this time an experimental PECL extension was used, however, the PECL version is no longer recommended or updated.

User Contributed Notes

There are no user contributed notes for this page.


실행시 설정

이 함수의 작동은 php.ini 설정에 영향을 받습니다.

Filter Configuration Options
이름 | 기본값 | 가변성 | 변경점
filter.default | "unsafe_raw" | PHP_INI_PERDIR | PHP_INI_ALL in filter <= 0.9.4. Available since PHP 5.2.0.
filter.default_flags | NULL | PHP_INI_PERDIR | PHP_INI_ALL in filter <= 0.9.4. Available since PHP 5.2.0.
PHP_INI_* 모드에 대한 상세와 정의는 Where a configuration setting may be set를 참고하십시오.

위 설정 지시어에 대한 간단한 설명입니다.

filter.default string

Filter all $_GET, $_POST, $_COOKIE, $_REQUEST and $_SERVER data by this filter. Original data can be accessed through filter_input().

Accepts the name of the filter you like to use by default. See the existing filter list for the list of the filter names.

Note:

Be careful about the default flags for the default filters. You should explicitly set them to the value you want. For example, to configure the default filter to behave exactly like htmlspecialchars() you need to set them default flags to 0 as shown below.

Example #1 Configuring the default filter to act like htmlspecialchars

filter.default = full_special_chars
filter.default_flags = 0

filter.default_flags integer

Default flags to apply when the default filter is set. This is set to FILTER_FLAG_NO_ENCODE_QUOTES by default for backwards compatibility reasons. See the flag list for the list of all the flag names.

User Contributed Notes

warbaby67 at gmail dot com 15-Dec-2010 07:04
filter.default sets the default FILTER by name.
filter.default_flags sets default FLAGS for the default filter.

The values for filter.default should be a string "name" of a valid FILTER:

"int"      FILTER_VALIDATE_INT
"boolean"  FILTER_VALIDATE_BOOLEAN

The irony of the entire list I typed being blocked by the SPAM filter is not lost on me.  Perhaps I'm not the first.


Types of filters

Table of Contents

  • Validate filters
  • Sanitize filters
  • Other filters
  • Filter flags

User Contributed Notes

There are no user contributed notes for this page.


Validate filters

Listing of filters for validation
ID | Name | Options | Flags | Description
FILTER_VALIDATE_BOOLEAN | "boolean" | default | FILTER_NULL_ON_FAILURE | Returns TRUE for "1", "true", "on" and "yes". Returns FALSE otherwise. If FILTER_NULL_ON_FAILURE is set, FALSE is returned only for "0", "false", "off", "no", and "", and NULL is returned for all non-boolean values.
FILTER_VALIDATE_EMAIL | "validate_email" | default |  | Validates whether the value is a valid e-mail address. In general, this validates e-mail addresses against the syntax in RFC 822, with the exceptions that comments and whitespace folding are not supported.
FILTER_VALIDATE_FLOAT | "float" | default, decimal | FILTER_FLAG_ALLOW_THOUSAND | Validates value as float, and converts to float on success.
FILTER_VALIDATE_INT | "int" | default, min_range, max_range | FILTER_FLAG_ALLOW_OCTAL, FILTER_FLAG_ALLOW_HEX | Validates value as integer, optionally from the specified range, and converts to int on success.
FILTER_VALIDATE_IP | "validate_ip" | default | FILTER_FLAG_IPV4, FILTER_FLAG_IPV6, FILTER_FLAG_NO_PRIV_RANGE, FILTER_FLAG_NO_RES_RANGE | Validates value as IP address, optionally only IPv4 or IPv6 or not from private or reserved ranges.
FILTER_VALIDATE_MAC | "validate_mac_address" | default |  | Validates value as MAC address.
FILTER_VALIDATE_REGEXP | "validate_regexp" | default, regexp |  | Validates value against regexp, a Perl-compatible regular expression.
FILTER_VALIDATE_URL | "validate_url" | default | FILTER_FLAG_SCHEME_REQUIRED, FILTER_FLAG_HOST_REQUIRED, FILTER_FLAG_PATH_REQUIRED, FILTER_FLAG_QUERY_REQUIRED | Validates value as URL (according to » http://www.faqs.org/rfcs/rfc2396), optionally with required components. Beware a valid URL may not specify the HTTP protocol http:// so further validation may be required to determine the URL uses an expected protocol, e.g. ssh:// or mailto:. Note that the function will only find ASCII URLs to be valid; internationalized domain names (containing non-ASCII characters) will fail.

Note:

As of PHP 5.4.11, the numbers +0 and -0 validate as both integers as well as floats (using FILTER_VALIDATE_FLOAT and FILTER_VALIDATE_INT). Before PHP 5.4.11 they only validated as floats (using FILTER_VALIDATE_FLOAT).

When default is set to option, default's value is used if value is not validated.

변경점

버전 | 설명
5.5.0 | Added FILTER_VALIDATE_MAC
5.2.1 | FILTER_VALIDATE_URL now defaults to FILTER_FLAG_SCHEME_REQUIRED and FILTER_FLAG_HOST_REQUIRED.

User Contributed Notes

kizge 30-Jun-2016 05:18
FILTER_VALIDATE_INT first casts its value to string which produces unexpected result for bool and float (https://bugs.php.net/bug.php?id=72490):

<?php

// Prints int(1).
var_dump(filter_var(true, FILTER_VALIDATE_INT));

// ...but this prints bool(false).
var_dump(filter_var(false, FILTER_VALIDATE_INT));

// --------

// Prints bool(false).
var_dump(filter_var(1.1, FILTER_VALIDATE_INT));

// ...but this prints int(0).
var_dump(filter_var(0.0, FILTER_VALIDATE_INT));

// ...but this again is bool(false).
var_dump(filter_var('0.0', FILTER_VALIDATE_INT));

// Also bool(false).
var_dump(filter_var('-0.0', FILTER_VALIDATE_INT));

?>

Live sample: https://3v4l.org/CZW0W

The docs are not clear on how exactly this casting affects the result for certain input values.
sebastian dot piskorski at gmail dot com 05-Jan-2016 09:48
FILTER_VALIDATE_EMAIL not only doesn't support whitespace folding and comments. It only checks Addr-spec part of email address. Otherwise it should mark such address as valid: 'Test Example <test@example.com>' because it is valid according to RFC 822.

Also address "test@localhost" should be valid. Which is mentioned in another note.

You can test it with this code:
<?php

$emails = array(
    'Test Example <test@example.com>',
    'test@localhost',
    'test@localhost.com'
);

foreach ($emails as $email) {
    echo (filter_var($email, FILTER_VALIDATE_EMAIL)) ?
        "[+] Email '$email' is valid\n" :
        "[-] Email '$email' is NOT valid\n";
}
?>

Output for PHP 5.3.21 - 7.0.1 :
[-] Email 'Test Example <test@example.com>' is NOT valid
[-] Email 'test@localhost' is NOT valid
[+] Email 'test@localhost.com' is valid
rsnell at usgs dot gov 26-Nov-2015 02:18
Note that if using FILTER_NULL_ON_FAILURE as a flag with the FILTER_VALIDATE_BOOLEAN id then NULL is no longer returned if the variable name is not set in the external variable array. It will instead return FALSE. In the description is says that when using the FILTER_NULL_ON_FAILURE flag that ' FALSE is returned only for "0", "false", "off", "no", and ""' an makes no mention of this additional state that can also return false. The behavior is mentioned on the filter_input documentation page under Return Values but that is not overly helpful if one is just looking here.

If FILTER_NULL_ON_FAILURE is not used then NULL is returned when the variable name is not set in the external variable array, TRUE is returned for "1", "true", "on" and "yes" and FALSE is returned for everything else.
william at crandell dot ws 20-Sep-2015 05:43
FILTER_VALIDATE_EMAIL on localhost emails will fail except when
used in conjunction with a subdomain such as www.localhost

FYI Sub domains generally are setup outside of the scope of php configurations.

Using the following code:

<?php
$email = "user@localhost";
echo "PHP Version: ".phpversion().'<br>';
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
    echo $email.'<br>';
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}else{
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>

Returns:
PHP Version: 5.5.29 //On MY server
bool(false)

While the following code:

<?php
$email = "user@www.localhost"; //Note the added subdomain www
echo "PHP Version: ".phpversion().'<br>';
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
    echo $email.'<br>';
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}else{
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>

Returns:
PHP Version: 5.5.29 //On MY server
user@www.localhost
string(18) "user@www.localhost"
Lech 06-Sep-2015 05:41
The description for FILTER_VALIDATE_URL seems incorrect/misleading. "Beware a valid URL may not specify the HTTP protocol" implies a valid URL cannot specify the HTTP protocol. I think "Beware a valid URL need not specify..." would be better.
Wrinkled Cheese 10-Jun-2015 12:33
When validating a URL, as documented, the protocol is not validated.  However, it is required to be present.

For example:

I don't expect a protocol to be present.  To validate expected input I have to add a "protocol" as a prefix, and return true or false, and further validate the input.

$r = filter_var(''this.doesnt.matter.so.why.is.it.required://'.$host, FILTER_VALIDATE_URL);
return ($r != '' && $r !== false) ? true : false;
Darth Killer 02-Jun-2015 05:54
Contrary to what documentation implies, the FILTER_NULL_ON_FAILURE seem to affect any validation filter, not just FILTER_VALIDATE_BOOLEAN. I've been using that since PHP 5.2, and as of PHP 5.6.8 it still works. I have no clue if it's a blug or if it is as intended, in which case the documentation needs to be fixed.

When the flag is used on a validation filter other than FILTER_VALIDATE_BOOLEAN, as expected the filter will return NULL instead of FALSE upon failure. This is quite useful when filtering a POST form with filter_input_array(), where you don't want to check what field is invalid and what field is missing. Just check if NULL is among the returned elements and you're done.

<?php
$definition = array(
   'login' => array(
      'filter' => FILTER_VALIDATE_STRING,
      'flags' => FILTER_NULL_ON_FAILURE
   ),
   'pwd' => FILTER_UNSAFE_RAW
);
$form_data = filter_input_array(INPUT_POST, $definition);
if(in_array(null, $form_data, true)) {
   // invalid form
} else {
   // valid form, let's proceed
}
?>

Of course, if you want more precise error messages that approach won't work. But it's still good to know, i believe.
Luuk 25-May-2015 07:23
@2:
$value = " 0 ";
$filtered = filter_var($value, FILTER_VALIDATE_INT);
if($filtered || $filtered === 0)
{
    // validated as an int
}

I think next code is better:

$value = "0";
if(filter_var($value, FILTER_VALIDATE_INT) !== false)
{
  .....
Anonymous 22-May-2015 05:02
FILTER_VALIDATE_FLOAT, decimal option mean decimal notation['.', ','].
maruerru at gmail dot com 27-Mar-2015 08:09
Often I see some code like the following:
$value = "12";
if( filter_var($value, FILTER_VALIDATE_INT) )
{
    // validated as an int
}

The above works as intended, except when $value is "0". In the above case it will be interpreted as FALSE.

For the correct behavior,  you have not only to check if it is equal (==) to false, but also identic (===) to FALSE:
$value = " 0 ";
if( filter_var($value, FILTER_VALIDATE_INT)  === FALSE )
{
    // validated as an int
}

I hope, I could help.
bryanwayb at gmail dot com 22-Mar-2015 01:18
It's good to remember that using filter_var is primarily for filtering input values when doing boolean logic comparisons. Take the following:

$value = "12";
if(filter_var($value, FILTER_VALIDATE_INT))
{
    // validated as an int
}

The above works as intended, except when $value = "0". In which case filter_var returns a 0, aka false when used as a boolean.

For the correct behavior, do a zero check.

$value = " 0 ";
$filtered = filter_var($value, FILTER_VALIDATE_INT);
if($filtered || $filtered === 0)
{
    // validated as an int
}
Bastien 24-Sep-2013 05:05
Rejection of so-called partial domains because of "missing" dot is not following section 2.3.5 of RFC 5321.

It says FQDNs are permitted, and com, org, or va are (well, may be) valids FQDNs. It depends on DNS, not on syntax.

Some TDLs (although few of them) have MX RRs, the for example "abuse@va" is correct.
rowan dot collins at gmail dot com 18-Mar-2013 05:22
Regarding "partial" addresses with no . in the domain part, a comment in the source code (in ext/filter/logical_filters.c) justifies this rejection thus:

     * The regex below is based on a regex by Michael Rushton.
     * However, it is not identical.  I changed it to only consider routeable
     * addresses as valid.  Michael's regex considers a@b a valid address
     * which conflicts with section 2.3.5 of RFC 5321 which states that:
     *
     *   Only resolvable, fully-qualified domain names (FQDNs) are permitted
     *   when domain names are used in SMTP.  In other words, names that can
     *   be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
     *   in Section 5) are permitted, as are CNAME RRs whose targets can be
     *   resolved, in turn, to MX or address RRs.  Local nicknames or
     *   unqualified names MUST NOT be used.
boy at relaxnow dot nl 19-Oct-2012 11:06
FILTER_VALIDATE_URL does not work with URNs, examples of valid URIs according to RFC3986 and if they are accepted by FILTER_VALIDATE_URL:

[PASS] ftp://ftp.is.co.za.example.org/rfc/rfc1808.txt
[PASS] gopher://spinaltap.micro.umn.example.edu/00/Weather/California/Los%20Angeles
[PASS] http://www.math.uio.no.example.net/faq/compression-faq/part1.html
[PASS] mailto:mduerst@ifi.unizh.example.gov
[PASS] news:comp.infosystems.www.servers.unix
[PASS] telnet://melvyl.ucop.example.edu/
[PASS] http://www.ietf.org/rfc/rfc2396.txt
[PASS] ldap://[2001:db8::7]/c=GB?objectClass?one
[PASS] mailto:John.Doe@example.com
[PASS] news:comp.infosystems.www.servers.unix
[FAIL] tel:+1-816-555-1212
[PASS] telnet://192.0.2.16:80/
[FAIL] urn:oasis:names:specification:docbook:dtd:xml:4.1.2
bee kay two at em ee dot com 06-May-2012 01:45
Notably missing is a way to validate text entry as printable,
printable multiline,
or printable and safe (tag free)

FILTER_VALIDATE_TEXT, which validates no special characters
perhaps with FILTER_FLAG_ALLOW_NEWLINE
and FILTER_FLAG_NOTAG to disallow tag starters
Griff 01-Sep-2011 01:28
<< FILTER_VALIDATE_EMAIL allows incomplete e-mail addresses to be validated, for examle john@gmail will validate as a proper e-mail address >>

"Plain" hostnames with no dots are valid in email addresses -
for example, "me@localhost".
php at sethsyberg dot com 07-Apr-2011 10:00
When validating floats, you must use the Identical/Not identical operators for proper validation of zeros:

This will not work as expected:
<?php
$x = 0;
if (!filter_var($x, FILTER_VALIDATE_FLOAT)) {
    echo "$x is a valid float";
} else {
    echo "$x is NOT a valid float";
}
?>

This will work as expected:
<?php
$x = 0;
if (filter_var($x, FILTER_VALIDATE_FLOAT)!== false) {
    echo "$x is a valid float";
} else {
    echo "$x is NOT a valid float";
}
?>
chastell at chastell dot net 15-Mar-2011 04:54
example@example is a perfectly valid email address - I use chastell@localhost and chastell@devielle (my computer's name) email addresses all the time and they get delivered just fine.
php dot net at piskvor dot org 11-Feb-2011 05:57
FILTER_VALIDATE_EMAIL is discarding valid e-mail addresses containing IDN. Since there are real, live IDNs on the Internet, that means the filtered output is too strict, leading to false negatives.

Punycode-encoded IDN addresses pass the filter correctly; so before checking for validity, it is necessary to convert the e-mail address to punycode.
Clifton 05-Jan-2011 05:00
FILTER_VALIDATE_EMAIL does NOT allow incomplete e-mail addresses to be validated as mentioned by Tomas.

Using the following code:

<?php
$email = "clifton@example"; //Note the .com missing
echo "PHP Version: ".phpversion().'<br>';
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
    echo $email.'<br>';
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}else{
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>

Returns:
PHP Version: 5.2.14 //On MY server, may be different depending on which version you have installed.
bool(false)

While the following code:

<?php
$email = "clifton@example.com"; //Note the .com added
echo "PHP Version: ".phpversion().'<br>';
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
    echo $email.'<br>';
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}else{
    var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>

Returns:
PHP Version: 5.2.14 //On MY server, may be different depending on which version you have installed.
clifton@example.com
string(16) "clifton@example.com"

This feature is only available for PHP Versions (PHP 5 >= 5.2.0) according to documentation. So make sure your version is correct.

Cheers,
Clifton


Sanitize filters

List of filters for sanitization
ID | Name | Flags | Description
FILTER_SANITIZE_EMAIL | "email" |  | Remove all characters except letters, digits and !#$%&'*+-=?^_`{|}~@.[].
FILTER_SANITIZE_ENCODED | "encoded" | FILTER_FLAG_STRIP_LOW, FILTER_FLAG_STRIP_HIGH, FILTER_FLAG_STRIP_BACKTICK, FILTER_FLAG_ENCODE_LOW, FILTER_FLAG_ENCODE_HIGH | URL-encode string, optionally strip or encode special characters.
FILTER_SANITIZE_MAGIC_QUOTES | "magic_quotes" |  | Apply addslashes().
FILTER_SANITIZE_NUMBER_FLOAT | "number_float" | FILTER_FLAG_ALLOW_FRACTION, FILTER_FLAG_ALLOW_THOUSAND, FILTER_FLAG_ALLOW_SCIENTIFIC | Remove all characters except digits, +- and optionally .,eE.
FILTER_SANITIZE_NUMBER_INT | "number_int" |  | Remove all characters except digits, plus and minus sign.
FILTER_SANITIZE_SPECIAL_CHARS | "special_chars" | FILTER_FLAG_STRIP_LOW, FILTER_FLAG_STRIP_HIGH, FILTER_FLAG_STRIP_BACKTICK, FILTER_FLAG_ENCODE_HIGH | HTML-escape '"<>& and characters with ASCII value less than 32, optionally strip or encode other special characters.
FILTER_SANITIZE_FULL_SPECIAL_CHARS | "full_special_chars" | FILTER_FLAG_NO_ENCODE_QUOTES, | Equivalent to calling htmlspecialchars() with ENT_QUOTES set. Encoding quotes can be disabled by setting FILTER_FLAG_NO_ENCODE_QUOTES. Like htmlspecialchars(), this filter is aware of the default_charset and if a sequence of bytes is detected that makes up an invalid character in the current character set then the entire string is rejected resulting in a 0-length string. When using this filter as a default filter, see the warning below about setting the default flags to 0.
FILTER_SANITIZE_STRING | "string" | FILTER_FLAG_NO_ENCODE_QUOTES, FILTER_FLAG_STRIP_LOW, FILTER_FLAG_STRIP_HIGH, FILTER_FLAG_STRIP_BACKTICK, FILTER_FLAG_ENCODE_LOW, FILTER_FLAG_ENCODE_HIGH, FILTER_FLAG_ENCODE_AMP | Strip tags, optionally strip or encode special characters.
FILTER_SANITIZE_STRIPPED | "stripped" |  | Alias of "string" filter.
FILTER_SANITIZE_URL | "url" |  | Remove all characters except letters, digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=.
FILTER_UNSAFE_RAW | "unsafe_raw" | FILTER_FLAG_STRIP_LOW, FILTER_FLAG_STRIP_HIGH, FILTER_FLAG_STRIP_BACKTICK, FILTER_FLAG_ENCODE_LOW, FILTER_FLAG_ENCODE_HIGH, FILTER_FLAG_ENCODE_AMP | Do nothing, optionally strip or encode special characters. This filter is also aliased to FILTER_DEFAULT.

Warning

When using one of these filters as a default filter either through your ini file or through your web server's configuration, the default flags is set to FILTER_FLAG_NO_ENCODE_QUOTES. You need to explicitly set filter.default_flags to 0 to have quotes encoded by default. Like this:

Example #1 Configuring the default filter to act like htmlspecialchars

filter.default = full_special_chars
filter.default_flags = 0

변경점

버전 | 설명
5.2.11/5.3.1 | Slashes (/) are removed by FILTER_SANITIZE_EMAIL. Prior they were retained.

User Contributed Notes

AntonioPrimera 04-Aug-2016 10:12
Please be aware that when using filter_var() with FILTER_SANITIZE_NUMBER_FLOAT and FILTER_SANITIZE_NUMBER_INT the result will be a string, even if the input value is actually a float or an int.

Use FILTER_VALIDATE_FLOAT and FILTER_VALIDATE_INT, which will convert the result to the expected type.
Ruth 03-Jun-2016 11:59
This is an example for a form having a group of checkboxes besides a lot of other inputs:

$args = array(
    'details' => array(
        'name' => 'details[]',
        'filter' => FILTER_SANITIZE_STRING,
        'flags'  => FILTER_REQUIRE_ARRAY
    )
    // ,
    // a lot of other stuff here (all the form's elements)
);
$myInputs = filter_input_array( INPUT_GET, $args, true );

where the checkboxes are (their form is in a page that reloads itself):

<label>
    <input type="checkbox" name="details[]" value="A" <?php if (in_array( "A", $myInputs['details'] )) { echo 'checked'; } ?>>
    Show A
</label>
<label>
    <input type="checkbox" name="details[]" value="B" <?php if (in_array( "B", $myInputs['details'] )) { echo 'checked'; } ?>>
    Show B
</label>
<label>
    <input type="checkbox" name="details[]" value="C" <?php if (in_array( "C", $myInputs['details'] )) { echo 'checked'; } ?>>
    Show C
</label>
<label>
    <input type="checkbox" name="details[]" value="D" <?php if (in_array( "D", $myInputs['details'] )) { echo 'checked'; } ?>>
    Show D
</label>
Willscrlt 10-Mar-2016 07:11
To include multiple flags, simply separate the flags with vertical pipe symbols.

For example, if you want to use filter_var() to sanitize $string with FILTER_SANITIZE_STRING and pass in FILTER_FLAG_STRIP_HIGH and FILTER_FLAG_STRIP_LOW, just call it like this:

$string = filter_var($string, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_LOW);

The same goes for passing a flags field in an options array in the case of using callbacks.

$var = filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS,
array('flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_ENCODE_HIGH));

Thanks to the Brain Goo blog at popmartian.com/tipsntricks/for this info.
Anonymous 23-Oct-2015 12:56
FILTER_SANITIZE_STRING doesn't behavior the same as strip_tags function.    strip_tags allows less than symbol inferred from context, FILTER_SANITIZE_STRING strips regardless.
<?php
$smaller = "not a tag < 5";
echo strip_tags($smaller);    // -> not a tag < 5
echo filter_var ( $smaller, FILTER_SANITIZE_STRING); // -> not a tag
?>
david dot drakulovski at gmail dot com 26-Feb-2014 04:54
Here is a simpler and a better presented ASCII list for the <32 or 127> filters
(if wikipedia confused the hell out of you):

http://www.danshort.com/ASCIImap/
galvao at galvao dot eti dot br 02-Mar-2013 03:04
Just to clarify, since this may be unknown for a lot of people:

ASCII characters above 127 are known as "Extended" and they represent characters such as greek letters and accented letters in latin alphabets, used in languages such as pt_BR.

A good ASCII quick reference (aside from the already mentioned Wikipedia article) can be found at: http://www.asciicodes.com/
Anonymous 16-Feb-2013 04:42
Support for FILTER_SANITIZE_FULL_SPECIAL_CHARS was added from version 5.3.3
googlybash24 at aol dot com 20-Sep-2012 02:39
Remember to trim() the $_POST before your filters are applied:

<?php

// We trim the $_POST data before any spaces get encoded to "%20"

// Trim array values using this function "trim_value"
function trim_value(&$value)
{
    $value = trim($value);    // this removes whitespace and related characters from the beginning and end of the string
}
array_filter($_POST, 'trim_value');    // the data in $_POST is trimmed

$postfilter =    // set up the filters to be used with the trimmed post array
    array(
            'user_tasks'                        =>    array('filter' => FILTER_SANITIZE_STRING, 'flags' => !FILTER_FLAG_STRIP_LOW),    // removes tags. formatting code is encoded -- add nl2br() when displaying
            'username'                            =>    array('filter' => FILTER_SANITIZE_ENCODED, 'flags' => FILTER_FLAG_STRIP_LOW),    // we are using this in the url
            'mod_title'                            =>    array('filter' => FILTER_SANITIZE_ENCODED, 'flags' => FILTER_FLAG_STRIP_LOW),    // we are using this in the url
        );

$revised_post_array = filter_var_array($_POST, $postfilter);    // must be referenced via a variable which is now an array that takes the place of $_POST[]
echo (nl2br($revised_post_array['user_tasks']));    //-- use nl2br() upon output like so, for the ['user_tasks'] array value so that the newlines are formatted, since this is our HTML <textarea> field and we want to maintain newlines
?>
googlybash24 at aol dot com 13-Sep-2012 05:39
This should help with most simple "textarea" fields in post forms.

Removing user html tags while maintaining text formatting such as newlines and carriage returns involves using the FILTER_SANITIZE_STRING filter ID with the flag !FILTER_FLAG_STRIP_LOW. The formatting text (the low ASCII values under decimal 32) are encoded because of the included FILTER_FLAG_ENCODE_LOW flag, but you are now preventing these from being removed. When you want to display the value on the page back in its intended format, use nl2br() so the encoded newlines are formatted properly on the page.

This example cleans $_POST data from a textarea field with the name "user_tasks" on a previous html form, stripping tags but maintaining formatting (at least for newlines):

<?php
$postfilter =
    array(
            'user_tasks'    =>    array('filter' => FILTER_SANITIZE_STRING, 'flags' => !FILTER_FLAG_STRIP_LOW),    // removes tags. formatting code is encoded -- add nl2br() when displaying
        );

$revised_post_array = filter_input_array(INPUT_POST, $postfilter);    // must be referenced via a variable which is now an array that takes the place of $_POST[]
echo (nl2br($revised_post_array['user_tasks']));    // here we use nl2br() for the displayed value, for the ['user_tasks'] array value so that the newlines are formatted
?>
adellemfrank at hotmail dot com 10-Jul-2012 04:07
A good list of which ASCII characters are < 32 and > 127 can be found at: http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
scamber256 at hotmail dot de 06-Aug-2011 11:37
Just a hint I tested,

You can obtain all the chars <32 (so newline and c.return), by using not operator > !FILTER_FLAG_STRIP_LOW as the last argument.

Example:
filter_input(INPUT_GET,'test',FILTER_SANITIZE_STRING,!FILTER_FLAG_STRIP_LOW);

The filter keeps working as before removing anything else as before apart from FILTER_FLAG_STRIP_LOW.
Just filter those "bad" chars <32 manually you don't want.
Dmitry Snytkine 11-Apr-2011 01:17
Beware that FILTER_FLAG_STRIP_LOW strips NEWLINE and TAG and CARRIAGE RETURN chars. If you have a form that accepts user input in plaintext format, all the submitted text will lose all the line breaks, making it appear all on one line. This basically renders this filter useless for parsing user-submitted text, even in plain text.
marcus at synchromedia dot co dot uk 27-Nov-2009 10:07
It's not entirely clear what the LOW and HIGH ranges are. LOW is characters below 32, HIGH is those above 127, i.e. outside the ASCII range.

<?php
$a = "\tcafe\n";
//This will remove the tab and the line break
echo filter_var($a, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
//This will remove the e.
echo filter_var($a, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
?>


Other filters

List of miscellaneous filters
ID | Name | Options | Flags | Description
FILTER_CALLBACK | "callback" | callable function or method |  | Call user-defined function to filter data.

User Contributed Notes

Vufi 29-Jan-2016 04:50
Are you also struggling with how to send arguments into the FILTER_CALLBACK.

Lets say you like to have a collection of custom filters that you can use in filter_var, filter_var_array and so on, but you want be able to call the new filters with different arguments, as you can in filter_var.

The easiest way to do this is to make a new class with all your custom filters. Then construct the class with your arguments inside filter_var argument array and enter the method in your class you like to run after.

If you send the argument as an array it is easy to make default values. But you can pass as many arguments you want and in any type, with this solution.

Example:
<?php
/**
 * Class CustomFilters
 * Custom filters for filter_var, filter_var_array, filter_input, filter_input_array
 */
class CustomFilters {
    private $options = array();

    public function __construct(array $options=array()){
        $this->options = $options;
    }

    /**
     * Returns the default options merged with the construction options
     * @param array $defaults
     * @return array
     */
    private function get_options(array $defaults){
        return array_merge($defaults, $this->options);
    }

    /**
     * Checks if a value is in a range
     * @param mixed $val Value provided by filter_var
     * @return mixed
     */
    public function FILTER_STEP_RANGE($val){
        $options = $this->get_options(
            // default options
            array(
                "min_range" => 1,
                "max_range" => 10,
                "step"      => 1,
                "default"   => null,    // Value to return on fail
                "strict"    => false,   // Check value for correct type
                "cast"      => false    // Cast the value in a certain type
            )
        );

        if(in_array( $val, range($options["min_range"], $options["max_range"], $options["step"]), $options["strict"])){

            // Return default if the value cant be cast as the right type
            if( $options["cast"] && !settype($val, $options["cast"])) {
                return $options["default"];
            }

            return $val;
        }else{
            return $options["default"];
        }
    }

    /**
     * Checks if a value is in array
     * @param mixed $val Value provided by filter_var
     * @return mixed
     */
    public function FILTER_ENUM($val){
        $options = $this->get_options(
        // default options
            array(
                "values"  => array(),
                "strict"    => false, // Value to return on fail
                "default"   => null,  // Check value for correct type
                "cast"      => false  // Cast the value in a certain type
            )
        );

        if(in_array($val, $options["values"], $options["strict"])){

            // Return default if the value cant be cast as the right type
            if( $options["cast"] && !settype($val, $options["cast"])){
                return $options["default"];
            }

            return $val;
        }else{
            return $options["default"];
        }
    }
}

$data = array(
    "range1" => "200",
    "range2" => 25,
    "enum" => "yes"
);

$args = array(
    "range1" => array(
        "filter" => FILTER_CALLBACK,
        "options" => array(
            new CustomFilters(array( "min_range" => 10, "max_range" => 600, "step" => 10, "default" => 120,
                "cast" => "integer")),
            'FILTER_STEP_RANGE'
            )
        ),

    "range2" => array(
        "filter" => FILTER_CALLBACK,
        "options" => array(
            new CustomFilters(array( "min_range" => 0, "max_range" => 1, "step" => .1, "default" => .5,
                "cast" => "float")),
            'FILTER_STEP_RANGE'
        )
    ),
    "enum" => array(
        "filter" => FILTER_CALLBACK,
        "options" => array(
            new CustomFilters(array( "values" => array("yes", "no", "Yes","No"), "cast" => "string")),
            'FILTER_ENUM'
        )
    )
);

var_dump( filter_var_array($data, $args) );
?>

Returns:
array(3) {
  ["range1"] => int(200)
  ["range2"] => float(0.5)
  ["enum"] => string(3) "yes"
}
christof at himalayasystemsNOSPAM dot be 11-Aug-2015 07:47
The supplied callback function may also be a class method. (since 5.4.0 ?)
To use a method you need to set the options param to an array with two values: the first is the object and the second is the method name.

<?php
/**
 * @property-read $language
 */
class GetInputStore {
    private $allowed_languages = array('en', 'fr', 'de', 'nl' /*, ... */);
    private $language;

    public function __construct(){
        $this->language = filter_input(INPUT_GET, 'language', FILTER_CALLBACK, array('options' => array($this, 'get_language_code')));
    }

    public function __get($name){
         switch($name){
             case 'language' : return $this->language;
             default : throw new Exception("The GetInputStore class doesn't support GET param \"$name\"");
         }
    }

    private function get_language_code($code){
        if(in_array($code, $this->allowed_languages)){
            return $code;
        } else {
            return 'en';
        }
    }
}
?>
thatindividual dot zhang at gmail dot com 23-Sep-2014 04:05
Here is an example, since I cannot find one through out php.net"

<?php

/**
 * Strip whitespace (or other non-printable characters) from the beginning and end of a string
 * @param string $value
 */
function trimString($value)
{
    return trim($value);
}

$loginname = filter_input(INPUT_POST, 'loginname', FILTER_CALLBACK, array('options' => 'trimString'));


Filter flags

List of filter flags
ID | Used with | Description
FILTER_FLAG_STRIP_LOW | FILTER_SANITIZE_ENCODED, FILTER_SANITIZE_SPECIAL_CHARS, FILTER_SANITIZE_STRING, FILTER_UNSAFE_RAW | Strips characters that have a numerical value <32.
FILTER_FLAG_STRIP_HIGH | FILTER_SANITIZE_ENCODED, FILTER_SANITIZE_SPECIAL_CHARS, FILTER_SANITIZE_STRING, FILTER_UNSAFE_RAW | Strips characters that have a numerical value >127.
FILTER_FLAG_STRIP_BACKTICK | FILTER_SANITIZE_ENCODED, FILTER_SANITIZE_SPECIAL_CHARS, FILTER_SANITIZE_STRING, FILTER_UNSAFE_RAW | Strips backtick characters.
FILTER_FLAG_ALLOW_FRACTION | FILTER_SANITIZE_NUMBER_FLOAT | Allows a period (.) as a fractional separator in numbers.
FILTER_FLAG_ALLOW_THOUSAND | FILTER_SANITIZE_NUMBER_FLOAT, FILTER_VALIDATE_FLOAT | Allows a comma (,) as a thousands separator in numbers.
FILTER_FLAG_ALLOW_SCIENTIFIC | FILTER_SANITIZE_NUMBER_FLOAT | Allows an e or E for scientific notation in numbers.
FILTER_FLAG_NO_ENCODE_QUOTES | FILTER_SANITIZE_STRING | If this flag is present, single (') and double (") quotes will not be encoded.
FILTER_FLAG_ENCODE_LOW | FILTER_SANITIZE_ENCODED, FILTER_SANITIZE_STRING, FILTER_SANITIZE_RAW | Encodes all characters with a numerical value <32.
FILTER_FLAG_ENCODE_HIGH | FILTER_SANITIZE_ENCODED, FILTER_SANITIZE_SPECIAL_CHARS, FILTER_SANITIZE_STRING, FILTER_SANITIZE_RAW | Encodes all characters with a numerical value >127.
FILTER_FLAG_ENCODE_AMP | FILTER_SANITIZE_STRING, FILTER_SANITIZE_RAW | Encodes ampersands (&).
FILTER_NULL_ON_FAILURE | FILTER_VALIDATE_BOOLEAN | Returns NULL for unrecognized boolean values.
FILTER_FLAG_ALLOW_OCTAL | FILTER_VALIDATE_INT | Regards inputs starting with a zero (0) as octal numbers. This only allows the succeeding digits to be 0-7.
FILTER_FLAG_ALLOW_HEX | FILTER_VALIDATE_INT | Regards inputs starting with 0x or 0X as hexadecimal numbers. This only allows succeeding characters to be a-fA-F0-9.
FILTER_FLAG_IPV4 | FILTER_VALIDATE_IP | Allows the IP address to be in IPv4 format.
FILTER_FLAG_IPV6 | FILTER_VALIDATE_IP | Allows the IP address to be in IPv6 format.
FILTER_FLAG_NO_PRIV_RANGE | FILTER_VALIDATE_IP | Fails validation for the following private IPv4 ranges: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Fails validation for the IPv6 addresses starting with FD or FC.
FILTER_FLAG_NO_RES_RANGE | FILTER_VALIDATE_IP | Fails validation for the following reserved IPv4 ranges: 0.0.0.0/8, 169.254.0.0/16, 127.0.0.0/8 and 240.0.0.0/4. Fails validation for the following reserved IPv6 ranges: ::1/128, ::/128, ::ffff:0:0/96 and fe80::/10.
FILTER_FLAG_SCHEME_REQUIRED | FILTER_VALIDATE_URL | Requires the URL to contain a scheme part.
FILTER_FLAG_HOST_REQUIRED | FILTER_VALIDATE_URL | Requires the URL to contain a host part.
FILTER_FLAG_PATH_REQUIRED | FILTER_VALIDATE_URL | Requires the URL to contain a path part.
FILTER_FLAG_QUERY_REQUIRED | FILTER_VALIDATE_URL | Requires the URL to contain a query string.
FILTER_REQUIRE_SCALAR |  | Requires the value to be scalar.
FILTER_REQUIRE_ARRAY |  | Requires the value to be an array.
FILTER_FORCE_ARRAY |  | If the value is a scalar, it is treated as array with the scalar value as only element.

변경점

버전 | 설명
5.3.2 | FILTER_FLAG_STRIP_BACKTICK has been added.
5.2.10 | FILTER_FLAG_NO_RES_RANGE supports also IPv6 addresses.

User Contributed Notes

php dot net at ewood dot users dot cementhorizon dot com 20-Oct-2010 01:42
In order to filter on loopback addresses try this :

<?php
function FILTER_FLAG_NO_LOOPBACK_RANGE($value) {
    // Fails validation for the following loopback IPv4 range: 127.0.0.0/8
    // This flag does not apply to IPv6 addresses
    return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? $value :
        (((ip2long($value) & 0xff000000) == 0x7f000000) ? FALSE : $value);
}

$var = filter_var('127.0.0.1', FILTER_CALLBACK, array('options' => 'FILTER_FLAG_NO_LOOPBACK_RANGE'));
// Returns FALSE

$var = filter_var('74.125.19.103', FILTER_CALLBACK, array('options' => 'FILTER_FLAG_NO_LOOPBACK_RANGE'));
// Returns '74.125.19.103'

// To filter Private IP ranges and Loopback ranges
$var = filter_var('127.0.0.1', FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)
    && filter_var('127.0.0.1', FILTER_CALLBACK, array('options' => 'FILTER_FLAG_NO_LOOPBACK_RANGE'));
// Returns FALSE
?>

(If someone wants to write in handling for the IPv6 loopback interface at "::1" go for it.)

-Gene Wood


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

INPUT_POST (integer)
POST variables.
INPUT_GET (integer)
GET variables.
INPUT_COOKIE (integer)
COOKIE variables.
INPUT_ENV (integer)
ENV variables.
INPUT_SERVER (integer)
SERVER variables.
INPUT_SESSION (integer)
SESSION variables. (not implemented yet)
INPUT_REQUEST (integer)
REQUEST variables. (not implemented yet)
FILTER_FLAG_NONE (integer)
No flags.
FILTER_REQUIRE_SCALAR (integer)
Flag used to require scalar as input
FILTER_REQUIRE_ARRAY (integer)
Require an array as input.
FILTER_FORCE_ARRAY (integer)
Always returns an array.
FILTER_NULL_ON_FAILURE (integer)
Use NULL instead of FALSE on failure.
FILTER_VALIDATE_INT (integer)
ID of "int" filter.
FILTER_VALIDATE_BOOLEAN (integer)
ID of "boolean" filter.
FILTER_VALIDATE_FLOAT (integer)
ID of "float" filter.
FILTER_VALIDATE_REGEXP (integer)
ID of "validate_regexp" filter.
FILTER_VALIDATE_URL (integer)
ID of "validate_url" filter.
FILTER_VALIDATE_EMAIL (integer)
ID of "validate_email" filter.
FILTER_VALIDATE_IP (integer)
ID of "validate_ip" filter.
FILTER_VALIDATE_MAC (integer)
ID of "validate_mac_address" filter. (Available as of PHP 5.5.0)
FILTER_DEFAULT (integer)
ID of default ("unsafe_raw") filter. This is equivalent to FILTER_UNSAFE_RAW.
FILTER_UNSAFE_RAW (integer)
ID of "unsafe_raw" filter.
FILTER_SANITIZE_STRING (integer)
ID of "string" filter.
FILTER_SANITIZE_STRIPPED (integer)
ID of "stripped" filter.
FILTER_SANITIZE_ENCODED (integer)
ID of "encoded" filter.
FILTER_SANITIZE_SPECIAL_CHARS (integer)
ID of "special_chars" filter.
FILTER_SANITIZE_EMAIL (integer)
ID of "email" filter.
FILTER_SANITIZE_URL (integer)
ID of "url" filter.
FILTER_SANITIZE_NUMBER_INT (integer)
ID of "number_int" filter.
FILTER_SANITIZE_NUMBER_FLOAT (integer)
ID of "number_float" filter.
FILTER_SANITIZE_MAGIC_QUOTES (integer)
ID of "magic_quotes" filter.
FILTER_CALLBACK (integer)
ID of "callback" filter.
FILTER_FLAG_ALLOW_OCTAL (integer)
Allow octal notation (0[0-7]+) in "int" filter.
FILTER_FLAG_ALLOW_HEX (integer)
Allow hex notation (0x[0-9a-fA-F]+) in "int" filter.
FILTER_FLAG_STRIP_LOW (integer)
Strip characters with ASCII value less than 32.
FILTER_FLAG_STRIP_HIGH (integer)
Strip characters with ASCII value greater than 127.
FILTER_FLAG_ENCODE_LOW (integer)
Encode characters with ASCII value less than 32.
FILTER_FLAG_ENCODE_HIGH (integer)
Encode characters with ASCII value greater than 127.
FILTER_FLAG_ENCODE_AMP (integer)
Encode &.
FILTER_FLAG_NO_ENCODE_QUOTES (integer)
Don't encode ' and ".
FILTER_FLAG_EMPTY_STRING_NULL (integer)
(No use for now.)
FILTER_FLAG_ALLOW_FRACTION (integer)
Allow fractional part in "number_float" filter.
FILTER_FLAG_ALLOW_THOUSAND (integer)
Allow thousand separator (,) in "number_float" filter.
FILTER_FLAG_ALLOW_SCIENTIFIC (integer)
Allow scientific notation (e, E) in "number_float" filter.
FILTER_FLAG_PATH_REQUIRED (integer)
Require path in "validate_url" filter.
FILTER_FLAG_QUERY_REQUIRED (integer)
Require query in "validate_url" filter.
FILTER_FLAG_IPV4 (integer)
Allow only IPv4 address in "validate_ip" filter.
FILTER_FLAG_IPV6 (integer)
Allow only IPv6 address in "validate_ip" filter.
FILTER_FLAG_NO_RES_RANGE (integer)
Deny reserved addresses in "validate_ip" filter.
FILTER_FLAG_NO_PRIV_RANGE (integer)
Deny private addresses in "validate_ip" filter.

User Contributed Notes

sixd at php dot net 23-Dec-2010 07:37
Rasmus added FILTER_SANITIZE_FULL_SPECIAL_CHARS to PHP 5.3.3.  See
http://svn.php.net/viewvc/php/php-src/branches/PHP_5_3/
ext/filter/filter.c?r1=297245&r2=297244&pathrev=297245
His earlier commit to the PHP source code trunk commented that this "Added the full htmlspecialchars() functionality which includes utf-8 validation as a default filter."


예제

Table of Contents

  • Validation
  • Sanitization

User Contributed Notes

There are no user contributed notes for this page.


Validation

Example #1 Validating email addresses with filter_var()

<?php
$email_a = 'joe@example.com';
$email_b = 'bogus';

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_a) email address is considered valid.";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_b) email address is considered valid.";
}
?>

위 예제의 출력:

This (joe@example.com) email address is considered valid.

Example #2 Validating IP addresses with filter_var()

<?php
$ip_a = '127.0.0.1';
$ip_b = '42.42';

if (filter_var($ip_a, FILTER_VALIDATE_IP)) {
    echo "This (ip_a) IP address is considered valid.";
}
if (filter_var($ip_b, FILTER_VALIDATE_IP)) {
    echo "This (ip_b) IP address is considered valid.";
}
?>

위 예제의 출력:

This (ip_a) IP address is considered valid.

Example #3 Passing options to filter_var()

<?php
$int_a = '1';
$int_b = '-1';
$int_c = '4';
$options = array(
    'options' => array(
                      'min_range' => 0,
                      'max_range' => 3,
                      )
);
if (filter_var($int_a, FILTER_VALIDATE_INT, $options) !== FALSE) {
    echo "This (int_a) integer is considered valid (between 0 and 3).\n";
}
if (filter_var($int_b, FILTER_VALIDATE_INT, $options) !== FALSE) {
    echo "This (int_b) integer is considered valid (between 0 and 3).\n";
}
if (filter_var($int_c, FILTER_VALIDATE_INT, $options) !== FALSE) {
    echo "This (int_c) integer is considered valid (between 0 and 3).\n";
}

$options['options']['default'] = 1;
if (($int_c = filter_var($int_c, FILTER_VALIDATE_INT, $options)) !== FALSE) {
    echo "This (int_c) integer is considered valid (between 0 and 3) and is $int_c.";
}
?>

위 예제의 출력:

This (int_a) integer is considered valid (between 0 and 3).
This (int_c) integer is considered valid (between 0 and 3) and is 1.

User Contributed Notes

andreykp at gmail dot com 15-Jan-2016 06:38
Do not use FILTER_VALIDATE_EMAIL in production!
It's not compatible with mail standarts
ghazouanijs at gmail dot com 21-Jan-2015 12:49
invalid email skip this filter
example : gnix@lineone.netsteve.gynes@lane4.co.uk
makicar5 at gmail dot com 21-Jan-2014 12:28
Email example is correct, in other examples just add dollar sign:

 echo "This (ip_a) IP address is considered valid.";
 echo "This (ip_a) IP address is considered valid.";

Change to:

 echo "This ($ip_a) IP address is considered valid.";
 echo "This ($ip_a) IP address is considered valid.";

same for
 int_a, int_b, int_c
to $int_a, $int_b, $int_c

Don't use single quotes in this example!
brokendocs at php dot net 08-Nov-2013 10:50
all the above documentation is slightly incorrect, the output is not reading the variables in -

code:

$email_a = 'joe@example.com';
$email_b = 'bogus';

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_a) email address is considered valid.";
}

output:

This (email_a) email address is considered valid.

should be output:

This (joe@example.com) email address is considered valid.

same for the IP and integer examples.
Stan 11-Sep-2013 07:02
HP 5.3.3 and 5.2.14 had a bug (http://bugs.php.net/52929) related to FILTER_VALIDATE_EMAIL, which resulted in segfault when validating large values. Simple and safe workaround for this is using strlen() before filter_val(). I'm not sure about 5.3.4 final, but it is written that some 5.3.4-snapshot versions also were affected.

Originally posted here - http://stackoverflow.com/questions/5855811/how-to-validate-an-email-in-php#comment6729146_5855853.


Sanitization

Example #1 Sanitizing and validating email addresses

<?php
$a = 'joe@example.org';
$b = 'bogus - at - example dot org';
$c = '(bogus@example.org)';

$sanitized_a = filter_var($a, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_a, FILTER_VALIDATE_EMAIL)) {
    echo "This (a) sanitized email address is considered valid.\n";
}

$sanitized_b = filter_var($b, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_b, FILTER_VALIDATE_EMAIL)) {
    echo "This sanitized email address is considered valid.";
} else {
    echo "This (b) sanitized email address is considered invalid.\n";
}

$sanitized_c = filter_var($c, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_c, FILTER_VALIDATE_EMAIL)) {
    echo "This (c) sanitized email address is considered valid.\n";
    echo "Before: $c\n";
    echo "After:  $sanitized_c\n";
}
?>

위 예제의 출력:

This (a) sanitized email address is considered valid.
This (b) sanitized email address is considered invalid.
This (c) sanitized email address is considered valid.
Before: (bogus@example.org)
After: bogus@example.org

Example #2 Configuring a default filter

filter.default = full_special_chars
filter.default_flags = 0

User Contributed Notes

Chris 15-Apr-2015 04:42
While it may seem to be good practice to set the defaults in php.ini you should not assume that the end users server has the same settings as your server. Because of this you should not presume that default filtering will work out of the box for the end user.

You should ensure that all filters are declared at the site of use regardless of your own default settings. A move to a new host even for your own personal application may break due to different settings.
zeeshan dot karamat dot abbas at gmail dot com 05-Feb-2015 10:59
If we omit using a filter then PHP by default puts a filter which is FILTER_DEFAULT which will use default filter. Now the question is what is a default filter. A default filter is unsafe_raw which will allow the unsafe raw data passed on to the server. This value is available in php.ini file. It is suggested that a developer should update this value inside php.ini file as under:
filter.default = full_special_chars
filter.default_flags = 0

Whereas in php.ini file above values are by default, set as under:
;filter.default = unsafe_raw
;filter.default_flags =

Above semicolons are commented out lines so surely one needs to remove those semicolons to apply the changes made. If we do not do above things then what will happen. In that case PHP will use default filter which would surely be FILTER_UNSAFE_RAW and one can see that unsafe raw data can then be passed onto server which can make the life a hacker easier.


Filter 함수 목록

Table of Contents

  • filter_has_var — Checks if variable of specified type exists
  • filter_id — Returns the filter ID belonging to a named filter
  • filter_input_array — Gets external variables and optionally filters them
  • filter_input — Gets a specific external variable by name and optionally filters it
  • filter_list — Returns a list of all supported filters
  • filter_var_array — Gets multiple variables and optionally filters them
  • filter_var — Filters a variable with a specified filter

User Contributed Notes

fumble1 at web dot de 12-Aug-2007 07:54
I recommend you to use the FILTER_REQUIRE_SCALAR (or FILTER_REQUIRE_ARRAY) flags, since you can use array-brackets both to access string offsets and array-element -- however, not only this can lead to unexpected behaviour. Look at this example:

<?php
$image = basename(filter_input(INPUT_GET, 'src', FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW));
// further checks
?>

/script.php?src[0]=foobar will cause a warning. :-(
Hence my recommendation:

<?php
$image = basename(filter_input(INPUT_GET, 'src', FILTER_UNSAFE_RAW, FILTER_REQUIRE_SCALAR | FILTER_FLAG_STRIP_LOW));
// further checks
?>
Richard Davey rich at corephp dot co dot uk 13-Jun-2007 08:15
There is an undocumented filter flag for FILTER_VALIDATE_BOOLEAN. The documentation implies that it will return NULL if the value doesn't match the allowed true/false values. However this doesn't happen unless you give it the FILTER_NULL_ON_FAILURE flag like this:

<?php
$value = 'car';
$result = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
?>

In the above $result will equal NULL. Without the extra flag it would equal FALSE, which isn't usually a desired result for this specific filter.
ckroll at rightmedia dot com 08-May-2007 05:02
Beware, the FILTER_SANITIZE_STRING flag functions much like strip_tags, so < will get filtered from input regardless of it's actually part of a tag.  We were getting unexepected results with a graphic library we wrote when trying to print < on a dynamic button.  The url came in something like ?string=%3C (<) but after filter ran it was empty.  To get around this, you could use FILTER_UNSAFE_RAW on that one param.
user 02-Feb-2007 09:15
Below is some code using filter API to restrict access to LAN by IPv4 private address range.

These notes may save someone else a little time:

filter_input_array() is useless for running multiple filters on the same key.
No way to chain or negate filters.

<?php

/* Merciful comment! */
function FILTER_NEGATE_HACK($_){ return (bool)!$_; }

function client_is_private_ipv4(){
  return (filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) &&
            FILTER_NEGATE_HACK(filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE));
}

if (! client_is_private_ipv4())
  exit('This application is restricted to local network users');

?>
vojtech at x dot cz 21-Dec-2006 03:38
Also notice that filter functions are using only the original variable values passed to the script even if you change the value in super global variable ($_GET, $_POST, ...) later in the script.

<?php
echo filter_input(INPUT_GET, 'var'); // print 'something'
echo $_GET['var']; // print 'something'
$_GET['var'] = 'changed';
echo filter_input(INPUT_GET, 'var'); // print 'something'
echo $_GET['var']; // print 'changed'
?>

In fact, external data are duplicated in SAPI before the script is processed and filter functions don't use super globals anymore (as explained in Filter tutorial bellow, section 'How does it work?').
vojtech at x dot cz 21-Dec-2006 02:13
Just to note that "server and env support may not work in all sapi, for filter 0.11.0 or php 5.2.0" as mentioned in Filter tutorial bellow.

The workaround is obvious:
Instead of
<?php
$var = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_DEFAULT);
?>
use
<?php
$var = filter_var(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : NULL, FILTER_DEFAULT);
?>


Checks if variable of specified type exists

  • ≪ Filter 함수 목록
  • filter_id ≫
  • PHP Manual
  • Filter 함수 목록
  • Checks if variable of specified type exists

filter_has_var

(PHP 5 >= 5.2.0, PHP 7)

filter_has_var — Checks if variable of specified type exists

설명

bool filter_has_var ( int $type , string $variable_name )

인수

type

One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.

variable_name

Name of a variable to check.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

dezlov 30-Jul-2015 10:12
For people who are wondering whether "magic_quotes_gpc" has any affect on "filter_input" operation, here is an extract from a PHP source file:

* As the input filter acts on input data before the magic quotes function mangles data, all access through the filter() function will not have any quotes or slashes added - it will be the pure data as send by the browser.

So when using this function, there is no need to strip the slashes when "magic_quotes_gpc" is enabled.
Benjamin 24-Jan-2014 08:25
filter_has_var fixes warning in Netbeans 7.3 when directly accessing $_GET["variable"]

Simple get and post functions I suggest:

function isGet( $value ) {
  if(  filter_has_var( INPUT_GET, $value )  ) {
    return true;
  }
  return false;
}

function get( $name )
{
  return filter_input( INPUT_GET , $name );
}
nanhe dot kumar at gmail dot com 06-Mar-2013 11:15
Through this example i think you can better understand

    if ( !filter_has_var(INPUT_GET, 'email') ) {
        echo "Email Not Found";
    }else{
        echo "Email Found";
    }
    Output

    localhost/nanhe/test.php?email=1 //Email Found
    localhost/nanhe/test.php?email //Email Found
    http://localhost/nanhe/test.php //Email Not Found

Consider on second example

http://localhost/nanhe/test.php
$_GET['email']="info@nanhe.in";
if ( !filter_has_var(INPUT_GET, 'email') ) {
        echo "Email Not Found";
    }else{
        echo "Email Found";
    }
But output will be Email Not Found
Anonymous 21-May-2009 11:28
To note: filter_has_var() is a bit faster than isset()
drm at melp dot nl 16-Jan-2009 10:54
Please note that the function does not check the live array, it actually checks the content received by php:

<?php
$_GET['test'] = 1;
echo filter_has_var(INPUT_GET, 'test') ? 'Yes' : 'No';
?>

would say "No", unless the parameter was actually in the querystring.

Also, if the input var is empty, it will say Yes.


Returns the filter ID belonging to a named filter

  • ≪ filter_has_var
  • filter_input_array ≫
  • PHP Manual
  • Filter 함수 목록
  • Returns the filter ID belonging to a named filter

filter_id

(PHP 5 >= 5.2.0, PHP 7)

filter_id — Returns the filter ID belonging to a named filter

설명

int filter_id ( string $filtername )

인수

filtername

Name of a filter to get.

반환값

ID of a filter on success or FALSE if filter doesn't exist.

참고

  • filter_list() - Returns a list of all supported filters

User Contributed Notes

w0n4b3 at gmail dot com 23-Jun-2011 11:47
[To get a list of filters and their IDs:]

<?php
$filters = filter_list();
foreach($filters as $filter_name) {
    echo $filter_name .": ".filter_id($filter_name) ."<br>";
}
?>
Will result in:
boolean: 258
float: 259
validate_regexp: 272
validate_url: 273
validate_email: 274
validate_ip: 275
string: 513
stripped: 513
encoded: 514
special_chars: 515
unsafe_raw: 516
email: 517
url: 518
number_int: 519
number_float: 520
magic_quotes: 521
callback: 1024


Gets external variables and optionally filters them

  • ≪ filter_id
  • filter_input ≫
  • PHP Manual
  • Filter 함수 목록
  • Gets external variables and optionally filters them

filter_input_array

(PHP 5 >= 5.2.0, PHP 7)

filter_input_array — Gets external variables and optionally filters them

설명

mixed filter_input_array ( int $type [, mixed $definition [, bool $add_empty = true ]] )

This function is useful for retrieving many values without repetitively calling filter_input().

인수

type

One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.

definition

An array defining the arguments. A valid key is a string containing a variable name and a valid value is either a filter type, or an array optionally specifying the filter, flags and options. If the value is an array, valid keys are filter which specifies the filter type, flags which specifies any flags that apply to the filter, and options which specifies any options that apply to the filter. See the example below for a better understanding.

This parameter can be also an integer holding a filter constant. Then all values in the input array are filtered by this filter.

add_empty

Add missing keys as NULL to the return value.

반환값

An array containing the values of the requested variables on success, or FALSE on failure. An array value will be FALSE if the filter fails, or NULL if the variable is not set. Or if the flag FILTER_NULL_ON_FAILURE is used, it returns FALSE if the variable is not set and NULL if the filter fails.

예제

Example #1 A filter_input_array() example

<?php
error_reporting(E_ALL | E_STRICT);
/* data actually came from POST
$_POST = array(
    'product_id'    => 'libgd<script>',
    'component'     => '10',
    'versions'      => '2.0.33',
    'testscalar'    => array('2', '23', '10', '12'),
    'testarray'     => '2',
);
*/

$args = array(
    'product_id'   => FILTER_SANITIZE_ENCODED,
    'component'    => array('filter'    => FILTER_VALIDATE_INT,
                            'flags'     => FILTER_REQUIRE_ARRAY,
                            'options'   => array('min_range' => 1, 'max_range' => 10)
                           ),
    'versions'     => FILTER_SANITIZE_ENCODED,
    'doesnotexist' => FILTER_VALIDATE_INT,
    'testscalar'   => array(
                            'filter' => FILTER_VALIDATE_INT,
                            'flags'  => FILTER_REQUIRE_SCALAR,
                           ),
    'testarray'    => array(
                            'filter' => FILTER_VALIDATE_INT,
                            'flags'  => FILTER_REQUIRE_ARRAY,
                           )

);

$myinputs = filter_input_array(INPUT_POST, $args);

var_dump($myinputs);
echo "\n";
?>

위 예제의 출력:

array(6) {
  ["product_id"]=>
  string(17) "libgd%3Cscript%3E"
  ["component"]=>
  array(1) {
    [0]=>
    int(10)
  }
  ["versions"]=>
  string(6) "2.0.33"
  ["doesnotexist"]=>
  NULL
  ["testscalar"]=>
  bool(false)
  ["testarray"]=>
  array(1) {
    [0]=>
    int(2)
  }
}

변경점

버전 | 설명
5.4.0 | Added add_empty parameter.

주의

Note:

There is no REQUEST_TIME key in INPUT_SERVER array because it is inserted into the $_SERVER later.

참고

  • filter_input() - Gets a specific external variable by name and optionally filters it
  • filter_var_array() - Gets multiple variables and optionally filters them
  • Types of filters

User Contributed Notes

sdupuis at blax dot ca 22-Jan-2014 06:36
Note that although you can provide a default filter for the entire input array there is no way to provide a flag for that filter without building the entire definition array yourself.

So here is a small function that can alleviate this hassle!

<?php
function filter_input_array_with_default_flags($type, $filter, $flags, $add_empty = true) {
    $loopThrough = array();
    switch ($type) {
        case INPUT_GET : $loopThrough = $_GET; break;
        case INPUT_POST : $loopThrough = $_POST; break;
        case INPUT_COOKIE : $loopThrough = $_COOKIE; break;
        case INPUT_SERVER : $loopThrough = $_SERVER; break;
        case INPUT_ENV : $loopThrough = $_ENV; break;
    }

    $args = array();
    foreach ($loopThrough as $key=>$value) {
        $args[$key] = array('filter'=>$filter, 'flags'=>$flags);
    }

    return filter_input_array($type, $args, $add_empty);
}
?>
CertaiN 11-Jan-2014 01:40
[New Version]

Example Usage:
<?php
$_GET['A']['a'] = '  CORRECT(including some spaces)    ';
$_GET['A']['b'] = '  CORRECT(including some spaces)    ';
$_GET['A']['c'] = "Invalid UTF-8 sequence: \xe3\xe3\xe3";
$_GET['A']['d']['invalid_structure'] = 'INVALID';

$_GET['B']['a'] = '  CORRECT(including some spaces)    ';
$_GET['B']['b'] = "Invalid UTF-8 sequence: \xe3\xe3\xe3";
$_GET['B']['c']['invalid_structure'] = 'INVALID';
$_GET['B']["Invalid UTF-8 sequence: \xe3\xe3\xe3"] = 'INVALID';

$_GET['C']['a'] = '  CORRECT(including some spaces)    ';
$_GET['C']['b'] = "Invalid UTF-8 sequence: \xe3\xe3\xe3";
$_GET['C']['c']['invalid_structure'] = 'INVALID';
$_GET['C']["Invalid UTF-8 sequence: \xe3\xe3\xe3"] = 'INVALID';

$_GET['unneeded_item'] = 'UNNEEDED';

var_dump(filter_struct_utf8(INPUT_GET, array(
    'A' => array(
        'a' => '',
        'b' => FILTER_STRUCT_TRIM,
        'c' => '',
        'd' => '',
    ),
    'B' => FILTER_STRUCT_FORCE_ARRAY,
    'C' => FILTER_STRUCT_FORCE_ARRAY | FILTER_STRUCT_TRIM,
)));
?>

Example Result:
array(3) {
  ["A"]=>
  array(4) {
    ["a"]=>
    string(36) "  CORRECT(including some spaces)    "
    ["b"]=>
    string(30) "CORRECT(including some spaces)"
    ["c"]=>
    string(0) ""
    ["d"]=>
    string(0) ""
  }
  ["B"]=>
  array(3) {
    ["a"]=>
    string(36) "  CORRECT(including some spaces)    "
    ["b"]=>
    string(0) ""
    ["c"]=>
    string(0) ""
  }
  ["C"]=>
  array(3) {
    ["a"]=>
    string(30) "CORRECT(including some spaces)"
    ["b"]=>
    string(0) ""
    ["c"]=>
    string(0) ""
  }
}
CertaiN 11-Jan-2014 01:39
[New Version]
This function is very useful for filtering complicated array structure.
Also, Some integer bitmasks and invalid UTF-8 sequence detection are available.

Code:
<?php
/**
 * @param  integer $type    Constant like INPUT_XXX.
 * @param  array   $default Default structure of the specified super global var.
 *                          Following bitmasks are available:
 *  + FILTER_STRUCT_FORCE_ARRAY - Force 1 dimensional array.
 *  + FILTER_STRUCT_TRIM        - Trim by ASCII control chars.
 *  + FILTER_STRUCT_FULL_TRIM   - Trim by ASCII control chars,
 *                                full-width and no-break space.
 * @return array            The value of the filtered super global var.
 */
define('FILTER_STRUCT_FORCE_ARRAY', 1);
define('FILTER_STRUCT_TRIM', 2);
define('FILTER_STRUCT_FULL_TRIM', 4);
function filter_struct_utf8($type, array $default) {
    static $func = __FUNCTION__;
    static $trim = "[\\x0-\x20\x7f]";
    static $ftrim = "[\\x0-\x20\x7f\xc2\xa0\xe3\x80\x80]";
    static $recursive_static = false;
    if (!$recursive = $recursive_static) {
        $types = array(
            INPUT_GET => $_GET,
            INPUT_POST => $_POST,
            INPUT_COOKIE => $_COOKIE,
            INPUT_REQUEST => $_REQUEST,
        );
        if (!isset($types[(int)$type])) {
            throw new LogicException('unknown super global var type');
        }
        $var = $types[(int)$type];
        $recursive_static = true;
    } else {
        $var = $type;
    }
    $ret = array();
    foreach ($default as $key => $value) {
        if ($is_int = is_int($value)) {
            if (!($value | (
                FILTER_STRUCT_FORCE_ARRAY |
                FILTER_STRUCT_FULL_TRIM |
                FILTER_STRUCT_TRIM
            ))) {
                $recursive_static = false;
                throw new LogicException('unknown bitmask');
            }
            if ($value & FILTER_STRUCT_FORCE_ARRAY) {
                $tmp = array();
                if (isset($var[$key])) {
                    foreach ((array)$var[$key] as $k => $v) {
                        if (!preg_match('//u', $k)){
                            continue;
                        }
                        $value &= FILTER_STRUCT_FULL_TRIM | FILTER_STRUCT_TRIM;
                        $tmp += array($k => $value ? $value : '');
                    }
                }
                $value = $tmp;
            }
        }
        if ($isset = isset($var[$key]) and is_array($value)) {
            $ret[$key] = $func($var[$key], $value);
        } elseif (!$isset || is_array($var[$key])) {
            $ret[$key] = null;
        } elseif ($is_int && $value & FILTER_STRUCT_FULL_TRIM) {
            $ret[$key] = preg_replace("/\A{$ftrim}++|{$ftrim}++\z/u", '', $var[$key]);
        } elseif ($is_int && $value & FILTER_STRUCT_TRIM) {
            $ret[$key] = preg_replace("/\A{$trim}++|{$trim}++\z/u", '', $var[$key]);
        } else {
            $ret[$key] = preg_replace('//u', '', $var[$key]);
        }
        if ($ret[$key] === null) {
            $ret[$key] = $is_int ? '' : $value;
        }
    }
    if (!$recursive) {
        $recursive_static = false;
    }
    return $ret;
}
?>
CertaiN 30-Jul-2013 07:22
This function is very useful for filtering complicated array structure.

Code:
<?php
function filter_request($var, $default_structure) {

    $ret = array();

    foreach ($default_structure as $key => $value) {
        if (!isset($var[$key])) {
            $ret[$key] = $value;
        } elseif (is_array($value)) {
            $ret[$key] = filter_request($var[$key], $value);
        } elseif (is_array($var[$key])) {
            $ret[$key] = $value;
        } else {
            $ret[$key] = $var[$key];
        }
    }

    return $ret;

}
?>

Sample Usage:
<?php
$_GET['a']['wrong_structure'] = 'foo';
$_GET['b']['c'] = 'CORRECT';
$_GET['b']['d']['wrong_structure'] = 'bar';
$_GET['unneeded_item'] = 'baz';

var_dump(filter_request($_GET, array(
    'a' => 'DEFAULT',
    'b' => array(
        'c' => 'DEFAULT',
        'd' => 'DEFAULT',
    ),
)));
?>

Sample Result:
array(2) {
  ["a"]=>
  string(21) "DEFAULT"
  ["b"]=>
  array(2) {
    ["c"]=>
    string(12) "CORRECT"
    ["d"]=>
    string(21) "DEFAULT"
  }
}
Anonymous 22-Apr-2010 09:12
Beware: if none of the arguments is set, this function returns NULL, not an array of NULL values.

/* No POST vars set in request
$_POST = array();
*/

$args = array('some_post_var' => FILTER_VALIDATE_INT);
$myinputs = filter_input_array(INPUT_POST, $args);
var_dump($myinputs);

Expected Output: array(1) { ["some_post_var"]=> NULL }

Actual Output: NULL
ville at N0SPAM dot zydo dot com 13-Mar-2010 12:18
While filtering input arrays, be careful of what flags you set besides FILTER_REQUIRE_ARRAY. For example, setting the flags like so:

<?php
$filter = array(
'myInputArr' => array('filter' => FILTER_SANITIZE_STRING,
                      'flags' => array('FILTER_FLAG_STRIP_LOW', 'FILTER_REQUIRE_ARRAY'))
);

$form_inputs = filter_input_array(INPUT_POST, $filter);
?>

.. will result in a blank $form_inputs['myInputArr'] regardless of what $_POST['myInputArr'] contains.
kibblewhite at live dot com 26-Jan-2009 04:35
If you are trying to handling multiple form inputs with same name, then you must assign the `'flags'  => FILTER_REQUIRE_ARRAY` to the definitions entry.

Example, you have a html form as such:
<form>
 <input name="t1[]" value="Some string One" />
 <input name="t1[]" value="Another String Two" />
</form>

Your definitions array will look a little like this:
$args = array(
  't1'    => array(
      'name' => 't1',
      'filter' => FILTER_SANITIZE_STRING,
      'flags'  => FILTER_REQUIRE_ARRAY)
);
kdeloach at gmail dot com 12-Aug-2008 09:42
@iam4webwork

This is not specific to filter_input.  If you have an element in HTML called names[], it can be accessed by calling $_POST['names'].
Kevin 08-Jul-2008 03:37
Looks like filter_input_array isn't aware of changes to the input arrays that were made before calling filter_input_array. Instead, it always looks at the originally submitted input arrays.

So this will not work:

$_POST['my_float_field'] = str_replace(',','.',$_POST['my_float_field']);
$args = array('my_float_field',FILTER_VALIDATE_FLOAT);
$result = filter_input_array(INPUT_POST, $args);
phpnotes dot 20 dot zsh at spamgourmet dot com 10-Sep-2007 07:32
The above example will actually output "NULL" because of the undefined variable doesnotexist - see http://bugs.php.net/bug.php?id=42608.
Sinured 22-Aug-2007 07:10
extract() is a very convenient way of copying all those variables to the local scope. (see http://www.php.net/extract)
iam4webwork at NOSPAM dot hotmail dot com 08-Jun-2007 10:02
The above example raises other questions such as how one would validate an html array.  In the input form each input tag that refers to an html array would be named for example testarray[].  However, after the form is submitted, the syntax for validating the values is different from  the expected $_POST['testarray[]']. Instead one has to drop the braces and validate as follows, assuming that testarray[] is supposed to be an html array of numerical values:

Valid test:

echo '*';
echo filter_input(
INPUT_POST,
'testarray',
FILTER_VALIDATE_INT,
FILTER_REQUIRE_ARRAY
);
echo '*';

But the following is an invalid test that results in 2 consequtive asterisks only!

echo '*';
echo filter_input(INPUT_POST,
'testarray[]',
FILTER_VALIDATE_INT,
FILTER_REQUIRE_ARRAY
);
echo '*';

So, there is a naming inconsistency going on, as after the form is submitted, one has to forget about the original name of the submitted array by dropping its braces. Maybe when the PECL/Filter extension is reviewed again, the great ones might consider making the syntax a little more forgiving.


Gets a specific external variable by name and optionally filters it

  • ≪ filter_input_array
  • filter_list ≫
  • PHP Manual
  • Filter 함수 목록
  • Gets a specific external variable by name and optionally filters it

filter_input

(PHP 5 >= 5.2.0, PHP 7)

filter_input — Gets a specific external variable by name and optionally filters it

설명

mixed filter_input ( int $type , string $variable_name [, int $filter = FILTER_DEFAULT [, mixed $options ]] )

인수

type

One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV.

variable_name

Name of a variable to get.

filter

The ID of the filter to apply. The Types of filters manual page lists the available filters.

If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.

options

Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array.

반환값

Value of the requested variable on success, FALSE if the filter fails, or NULL if the variable_name variable is not set. If the flag FILTER_NULL_ON_FAILURE is used, it returns FALSE if the variable is not set and NULL if the filter fails.

예제

Example #1 A filter_input() example

<?php
$search_html = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_SPECIAL_CHARS);
$search_url = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_ENCODED);
echo "You have searched for $search_html.\n";
echo "<a href='?search=$search_url'>Search again.</a>";
?>

위 예제의 출력 예시:

You have searched for Me &#38; son.
<a href='?search=Me%20%26%20son'>Search again.</a>

참고

  • filter_var() - Filters a variable with a specified filter
  • filter_input_array() - Gets external variables and optionally filters them
  • filter_var_array() - Gets multiple variables and optionally filters them
  • Types of filters

User Contributed Notes

John Smith 22-Nov-2014 01:48
1. The description of the options parameter is misleading. In order to pass the options (e.g. default, min_range and max_range) you must pass an associative array with a key called "options", which itself is an associative array containing option name => option value pairs.

2. The return values section does not mention that if you specify the "default" option then the function will return the specified default value instead of returning FALSE or NULL (when filter fails or variable is absent).
rimelek at rimelek dot hu 20-Oct-2014 05:44
If your $_POST contains an array value:
<?php
$_POST  = array(
    'var' => array('more', 'than', 'one', 'values')
);
?>
you should use FILTER_REQUIRE_ARRAY option:
<?php
var_dump(filter_input(INPUT_POST, 'var', FILTER_DEFAULT , FILTER_REQUIRE_ARRAY));
?>
Otherwise it returns false.
viaujoc at videotron dot ca 04-Oct-2014 01:14
filter_input() does not seem to support multiple values for a single variable name.

Here is the code comparing the behavior of bare $_GET superglobal vs filter_input(INPUT_GET,...):
<?php
print("Bare \$_GET:\n");
var_dump($_GET);
print("filter_input():\n");
var_dump(filter_input(INPUT_GET,"var"));
?>

When calling: /..../script.php?var=123 (there is only one value for variable 'var')
Output is:
Bare $_GET:
array(1) {
  ["var"]=>
  string(3) "123"
}
filter_input():
string(3) "123"

When calling: /..../script.php?var[]=123&var[]=999 (there are two  values for variable 'var')
Output is:
Bare $_GET:
array(1) {
  ["var"]=>
  array(2) {
    [0]=>
    string(3) "123"
    [1]=>
    string(3) "999"
  }
}
filter_input():
bool(false)

As expected, $_GET['var'] became an array. But filter_input() seems to be unable to process multiple values and returns false.
CertaiN 23-May-2014 10:22
This function provides us the extremely simple solution for type filtering.

Without this function...
<?php
if (!isset($_GET['a'])) {
    $a = null;
} elseif (!is_string($_GET['a'])) {
    $a = false;
} else {
    $a = $_GET['a'];
}
$b = isset($_GET['b']) && is_string($_GET['b']) ? $_GET['b'] : '';
?>

With this function...
<?php
$a = filter_input(INPUT_GET, 'a');
$b = (string)filter_input(INPUT_GET, 'b');
?>

Yes, FILTER_REQUIRE_SCALAR seems to be set as a default option.
It's very helpful for eliminating E_NOTICE, E_WARNING and E_ERROR.
This fact should be documented.
descartavel1+php at gmail dot com 21-Apr-2014 03:50
contrary to what is stated here on the comments on thow to use the options for filters, there is no range option or default... in fact, there is not much option AT ALL. It is not mentioned in the manual anywhere, and the provided code on that comment does nothing with php-5.4.4..

<?php
get(GET, 'p', FILTER_VALIDATE_INT, array('options'=>array('default'=>5, 'min_range'=>0, 'max_range'=>9)) );
// ?p=30 => 30
// ?p="123" => 123
// ?p=-23 => -23
// ?p=asdf => null
?>
Stefan Weinzierl 17-Feb-2014 01:31
Here is an example how to work with the options-parameter. Notice the 'options' in the 'options'-Parameter!

<?php
$options=array('options'=>array('default'=>5, 'min_range'=>0, 'max_range'=>9));

$priority=filter_input(INPUT_GET, 'priority', FILTER_VALIDATE_INT, $options);
?>

$priority will be 5 if the priority-Parameter isn't set or out the given range.
akshay dot leadindia at gmail dot com 29-Aug-2013 11:17
The beauty of using this instead of directly using filter_var( $_GET['search'] ) is that you don't need to check if( isset( $_GET['search'] ) ) as if you pass that to filter_var and the key is not set then it will result in a warning. This function simplifies this and will return the relevant result to you (as per your options set) if the key has not been set in the user input.

If the type of filter you are using also supports a 'default' argument then this function will also stuff your missing input key with that value, again saving your efforts
west {:a7} jsausa {:d0t}~ com 29-Nov-2012 05:30
It's worth noting that the names for variables in filter input obey the same rules as variable naming in PHP (must start with an underscore or letter).  We were allowing users to build custom forms but hashing the names to prevent them from putting arbitrary content into the dom.  Turns out the hash function occasionally produced entirely numeric values for the field name... which doesn't work with filter_input but worked fine if you read directly from $_GET, $_POST, or $_REQUEST.  A workaround is to always prefix an underscore to the field name.
chris at chlab dot ch 20-Mar-2012 06:46
To use a class method for a callback function, as usual, provide an array with an instance of the class and the method name.
Example:

<?php
class myValidator
{
  public function username($value)
  {
    // return username or boolean false
  }
}

$myValidator = new myValidator;
$options = array('options' => array($myValidator, 'username'));
$username = filter_input(INPUT_GET, 'username', FILTER_CALLBACK, $options);
var_dump($username);
?>
ss23 at ss23 dot geek dot nz 29-Jul-2010 08:37
Note that this function doesn't (or at least doesn't seem to) actually filter based on the current values of $_GET etc. Instead, it seems to filter based off the original values.
<?php
$_GET['search'] = 'foo'; // This has no effect on the filter_input

$search_html = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_SPECIAL_CHARS);
$search_url = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_ENCODED);
echo "You have searched for $search_html.\n";
echo "<a href='?search=$search_url'>Search again.</a>";
?>

If you need to set a default input value and filter that, use filter_var on your required input variable instead
travismowens at gmail dot com 28-Jul-2010 06:21
I wouldn't recommend people use this function to store their data in a database.  It's best not to encode data when storing it, it's better to store it raw and convert in upon the time of need.

One main reason for this is because if you have a short CHAR(16) field and the text contains encoded characters (quotes, ampersand) you can easily take a 12 character entry which obviously fits, but because of encoding it no longer fits.

Also, while not as common, if you need to use this data in another place, such as a non webpage (perhaps in a desktop app, or to a cell phone SMS or to a pager) the HTML encoded data will appear raw, and now you have to decode the data.

In summary, the best way to architect your system, is to store data as raw, and encode it only the moment you need to.  So this means in your PHP upon doing a SQL query, instead of merely doing an   echo $row['title']  you need to run htmlentities() on your echos, or better yet, an abstract function.
med dot k1987 at yahoo dot com 30-Jun-2010 04:48
Hello,
Does anybody know how to prevent FILTER_SANITIZE_SPECIAL_CHARS from converting the line breaks (\n) into (&#10;).
I'm developing a simple commenting system for my website and I found that the php filter converts \n to &#10; so when using nl2br() there are no line breaks.
help please.
thanks :)
Maksym Karazeev 03-Mar-2009 10:13
Just a tip.

Note how to setup default filter for filter_var_array

When I tried to use filter_var_array and didn't mentioned all array indexes in definition it filtered it with some filter and broke values so using this tip corrected everything

<?php
$def = array_map(create_function('', 'return array("filter"=>FILTER_UNSAFE_RAW);'), $input);
?>
anthony dot parsons at manx dot net 23-Aug-2007 11:10
FastCGI seems to cause strange side-effects with unexpected null values when using INPUT_SERVER and INPUT_ENV with this function. You can use this code to see if it affects your server:
<?php
var_dump($_SERVER);
foreach ( array_keys($_SERVER) as $b ) {
    var_dump($b, filter_input(INPUT_SERVER, $b));
}
echo '<hr>';
var_dump($_ENV);
foreach ( array_keys($_ENV) as $b ) {
    var_dump($b, filter_input(INPUT_ENV, $b));
}
?>
If you want to be on the safe side, using the superglobal $_SERVER and $_ENV variables will always work. You can still use the filter_* functions for Get/Post/Cookie without a problem, which is the important part!


Returns a list of all supported filters

  • ≪ filter_input
  • filter_var_array ≫
  • PHP Manual
  • Filter 함수 목록
  • Returns a list of all supported filters

filter_list

(PHP 5 >= 5.2.0, PHP 7)

filter_list — Returns a list of all supported filters

설명

array filter_list ( void )

반환값

Returns an array of names of all supported filters, empty array if there are no such filters. Indexes of this array are not filter IDs, they can be obtained with filter_id() from a name instead.

예제

Example #1 A filter_list() example

<?php
print_r(filter_list());
?>

위 예제의 출력 예시:

Array
(
    [0] => int
    [1] => boolean
    [2] => float
    [3] => validate_regexp
    [4] => validate_url
    [5] => validate_email
    [6] => validate_ip
    [7] => string
    [8] => stripped
    [9] => encoded
    [10] => special_chars
    [11] => unsafe_raw
    [12] => email
    [13] => url
    [14] => number_int
    [15] => number_float
    [16] => magic_quotes
    [17] => callback
)

User Contributed Notes

larry at weblabsperth dot com dot au 05-Sep-2016 06:42
Output a table of Filter IDs as Integers with corresponding Constant IDs:

<?php

echo '<table>';
echo '<tr>';
echo '<th>Filter Name</th>';
echo '<th>Filter Int ID</th>';
echo '<th>Filter Const ID</th>';
echo '</tr>';

foreach (filter_list() as $id =>$filter) {
    $const = 'FILTER_';
    switch($filter) {
        case 'int':
        case 'boolean':
        case 'float':
            $const .= 'VALIDATE_' . $filter;
        break;

        case 'string':
        case 'stripped':
        case 'encoded':
        case 'special_chars':
        case 'full_special_chars':
        case 'unsafe_raw':
        case 'email':
        case 'url':
        case 'number_int':
        case 'number_float':
        case 'magic_quotes':
            $const .= 'SANITIZE_' . $filter;
        break;
    }

    if (substr($filter,0,8)=='validate' || $filter=='callback')
        $const .= $filter;

    echo '<tr><td>' . $filter . '</td><td align="center">' . filter_id($filter) . '</td><td>' . strtoupper($const) . '</td></tr>';
}

echo '</table>';

?>
vb dot jyoti at gmail dot com 13-Aug-2008 08:57
<?php
echo "<pre>";
print_r(filter_list());
echo "</pre>";
foreach (filter_list() as $key => $value)
{
echo "<br>".$key."=".$value.'='.filter_id($value);
}
?>

0=int=257
1=boolean=258
2=float=259
3=validate_regexp=272
4=validate_url=273
5=validate_email=274
6=validate_ip=275
7=string=513
8=stripped=513
9=encoded=514
10=special_chars=515
11=unsafe_raw=516
12=email=517
13=url=518
14=number_int=519
15=number_float=520
16=magic_quotes=521
17=callback=1024


Gets multiple variables and optionally filters them

  • ≪ filter_list
  • filter_var ≫
  • PHP Manual
  • Filter 함수 목록
  • Gets multiple variables and optionally filters them

filter_var_array

(PHP 5 >= 5.2.0, PHP 7)

filter_var_array — Gets multiple variables and optionally filters them

설명

mixed filter_var_array ( array $data [, mixed $definition [, bool $add_empty = true ]] )

This function is useful for retrieving many values without repetitively calling filter_var().

인수

data

An array with string keys containing the data to filter.

definition

An array defining the arguments. A valid key is a string containing a variable name and a valid value is either a filter type, or an array optionally specifying the filter, flags and options. If the value is an array, valid keys are filter which specifies the filter type, flags which specifies any flags that apply to the filter, and options which specifies any options that apply to the filter. See the example below for a better understanding.

This parameter can be also an integer holding a filter constant. Then all values in the input array are filtered by this filter.

add_empty

Add missing keys as NULL to the return value.

반환값

An array containing the values of the requested variables on success, or FALSE on failure. An array value will be FALSE if the filter fails, or NULL if the variable is not set.

예제

Example #1 A filter_var_array() example

<?php
error_reporting(E_ALL | E_STRICT);
$data = array(
    'product_id'    => 'libgd<script>',
    'component'     => '10',
    'versions'      => '2.0.33',
    'testscalar'    => array('2', '23', '10', '12'),
    'testarray'     => '2',
);

$args = array(
    'product_id'   => FILTER_SANITIZE_ENCODED,
    'component'    => array('filter'    => FILTER_VALIDATE_INT,
                            'flags'     => FILTER_FORCE_ARRAY,
                            'options'   => array('min_range' => 1, 'max_range' => 10)
                           ),
    'versions'     => FILTER_SANITIZE_ENCODED,
    'doesnotexist' => FILTER_VALIDATE_INT,
    'testscalar'   => array(
                            'filter' => FILTER_VALIDATE_INT,
                            'flags'  => FILTER_REQUIRE_SCALAR,
                           ),
    'testarray'    => array(
                            'filter' => FILTER_VALIDATE_INT,
                            'flags'  => FILTER_FORCE_ARRAY,
                           )

);

$myinputs = filter_var_array($data, $args);

var_dump($myinputs);
echo "\n";
?>

위 예제의 출력:

array(6) {
  ["product_id"]=>
  string(17) "libgd%3Cscript%3E"
  ["component"]=>
  array(1) {
    [0]=>
    int(10)
  }
  ["versions"]=>
  string(6) "2.0.33"
  ["doesnotexist"]=>
  NULL
  ["testscalar"]=>
  bool(false)
  ["testarray"]=>
  array(1) {
    [0]=>
    int(2)
  }
}

변경점

버전 | 설명
5.4.0 | Added add_empty parameter.

참고

  • filter_input_array() - Gets external variables and optionally filters them
  • filter_var() - Filters a variable with a specified filter
  • filter_input() - Gets a specific external variable by name and optionally filters it
  • Types of filters

User Contributed Notes

eguvenc at gmail dot com 07-Jun-2009 12:45
<?php
//an example of simply sanitize an array..

$data = array(
                '<b>bold</b>',
                '<script>javascript</script>',
                'P*}i@893746%%%p*.i.*}}|.dw<?php echo "echo works!!";?>');

$myinputs = filter_var_array($data,FILTER_SANITIZE_STRING);

var_dump($myinputs);

//OUTPUT:
//formarray(3) { [0]=> string(4) "bold" [1]=> string(10) "javascript" [2]=> string(26) "P*}i@893746%%%p*.i.*}}|.dw" }
?>


Filters a variable with a specified filter

  • ≪ filter_var_array
  • Function Handling ≫
  • PHP Manual
  • Filter 함수 목록
  • Filters a variable with a specified filter

filter_var

(PHP 5 >= 5.2.0, PHP 7)

filter_var — Filters a variable with a specified filter

설명

mixed filter_var ( mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]] )

인수

variable

Value to filter. Note that scalar values are converted to string internally before they are filtered.

filter

The ID of the filter to apply. The Types of filters manual page lists the available filters.

If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.

options

Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array. For the "callback" filter, callable type should be passed. The callback must accept one argument, the value to be filtered, and return the value after filtering/sanitizing it.

<?php
// for filters that accept options, use this format
$options = array(
    'options' => array(
        'default' => 3, // value to return if the filter fails
        // other options here
        'min_range' => 0
    ),
    'flags' => FILTER_FLAG_ALLOW_OCTAL,
);
$var = filter_var('0755', FILTER_VALIDATE_INT, $options);

// for filter that only accept flags, you can pass them directly
$var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);

// for filter that only accept flags, you can also pass as an array
$var = filter_var('oops', FILTER_VALIDATE_BOOLEAN,
                  array('flags' => FILTER_NULL_ON_FAILURE));

// callback validate filter
function foo($value)
{
    // Expected format: Surname, GivenNames
    if (strpos($value, ", ") === false) return false;
    list($surname, $givennames) = explode(", ", $value, 2);
    $empty = (empty($surname) || empty($givennames));
    $notstrings = (!is_string($surname) || !is_string($givennames));
    if ($empty || $notstrings) {
        return false;
    } else {
        return $value;
    }
}
$var = filter_var('Doe, Jane Sue', FILTER_CALLBACK, array('options' => 'foo'));
?>

반환값

Returns the filtered data, or FALSE if the filter fails.

예제

Example #1 A filter_var() example

<?php
var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));
var_dump(filter_var('http://example.com', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
?>

위 예제의 출력:

string(15) "bob@example.com"
bool(false)

참고

  • filter_var_array() - Gets multiple variables and optionally filters them
  • filter_input() - Gets a specific external variable by name and optionally filters it
  • filter_input_array() - Gets external variables and optionally filters them
  • Types of filters
  • callback형에 대한 정보

User Contributed Notes

alice at deviant dot email 02-May-2016 09:19
Many people, myself included, have found that the FILTER_VALIDATE_EMAIL does not actually properly work.

Below is a wrapper that I believe validates every legal routable address.

<?php

/*******************************************
 *
 * These are the function
 *
 *  check_username is called by check_email
 *  - it compensates for bugs in the php
 *    filter_var function.
 *  - returns boolean
 *
 *  check_email is the function to use.
 *  First argument is string, address to
 *    check
 *  Second argument is optional boolean,
 *    whether or not to use DNS to validate
 *    the domain name. Defaults to true
 *  Returns boolean
 *
 */

function check_username($uname) {
  //Only UTF-8 addresses are legal
  if (iconv('UTF-8', 'UTF-8', $input) != $input) {
      return FALSE;
  }
  //replace all characters above U+007F with letter U for simplicity of checking
  $uname = preg_replace('/[\x{007F}-\x{FFFF}]/u', 'U', $uname);

  //remove comments - only legal in format (comment) at beginning or end of username
  $s[] = '/^\([^\)]*\)/'; $s[] = '/\([^\)]*\)$/';
  $uname = preg_replace($s, '', $uname);
  //make sure we have something left
  if(strlen(trim($uname)) == 0) {
    return FALSE;
  }
  // check for legal dot usage
  if(substr_count($uname, '..') > 0) {
    return FALSE;
  }
  // convert \\ and \" to an A for simplicity
  $s[] = '/[\\\][\\\]/';
  $s[] = '/\\\"/';
  $uname = preg_replace($s, 'A', $uname);
  // check for illegal use of quotes
  if(preg_match('/[^.]+"[^.]+/', $uname)) {
    return FALSE;
  }
  // compensate for characters legal when in quotes
  $uname = preg_replace_callback('/"(.*)"/', function ($m) {
    $s[]="/[ \(\),\:;<>@\[\] ]/";
    return preg_replace($s,'Q',$m[1]);
    }, $uname);
  // check what we have left with filter_var
  return filter_var($uname . '@example.org', FILTER_VALIDATE_EMAIL);
}

function check_email($email, $dns_check=true) {
  $array = explode('@', $email);
  if(count($array) < 2) {
    return FALSE;
  }
  $domain = end($array);
  array_pop($array);
  if(function_exists('idn_to_ascii')) {
    //php filter no workie with unicode characters
    $domain = idn_to_ascii($domain);
  }
  $ipcheck = preg_replace(array('/^\[ipv6\:/i', '/^\[/', '/\]$/'), '', $domain);
  if(filter_var($ipcheck, FILTER_VALIDATE_IP)) {
    // it's an IP address
    if(! filter_var($ipcheck, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
      return FALSE;
    }
  } else {
    // it's a domain name
    //   php bug - FILTER_VALIDATE_EMAIL doesn't like naked TLD
    if(! filter_var('user@a.' . $domain, FILTER_VALIDATE_EMAIL)) {
      return FALSE;
    }
    if($dns_check) {
      if(! dns_get_record($domain)) {
        return FALSE;
      }
    }
  }
  //now check legal username
  return check_username(implode('@', $array));
}
?>
It evaluates the address in two parts, first evaluating the host and if that legal it then evaluates the user name.

If there is a DNS problem *and* the default $dns_check value of true is used, valid will fail. If it is an international domain name, you have to have the php-intl package installed.

Enjoy.
buttflattery at gmail dot com 19-Jan-2016 12:15
FILTER_VALIDATE_URL validates a url like http://www.
amy at anvilzephyr dot com 11-Jan-2016 05:14
It might be wise to trim or sanitize (FILTER_SANITIZE_EMAIL) your email variable before validating to remove spaces:
filter_var(trim($email), FILTER_VALIDATE_EMAIL)
Ant 14-Dec-2015 11:20
Note: filter_var with filter=FILTER_VALIDATE_URL used parse_url function
dan at spiral8 dot net 08-Dec-2015 11:23
Here's a simple test using filter_var with FILTER_VALIDATE_URL.
(If you're using file_get_contents after this you will run into a problem, I was using: PHP 5.5.12 (cli))

<?php
$url = 'a://google.com';

$result = filter_var($url, FILTER_VALIDATE_URL);

if($result){
    echo 'Valid URL'.PHP_EOL;
}

var_dump($result);
?>

The result is:
Valid URL
string(14) "a://google.com"
yoanlin93 at gmail dot com 22-Nov-2015 03:34
Some boolean conversions:

<?php
var_dump(filter_var('oops', FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)));
// NULL

var_dump(filter_var('false', FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)));
// bool(false)

var_dump(filter_var('true', FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)));
// bool(true)

var_dump(filter_var(0, FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)));
// bool(false)

var_dump(filter_var(1, FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)));
// bool(true)

var_dump(filter_var('TRUE', FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)));
// bool(true)

var_dump(filter_var('', FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)));
// bool(false)

var_dump(filter_var('FALSE', FILTER_VALIDATE_BOOLEAN, array('flags' => FILTER_NULL_ON_FAILURE)));
// bool(false)
Anonymous 04-Nov-2015 08:20
FILTER_VALIDATE_URL allows:

filter_var('javascript://comment%0Aalert(1)', FILTER_VALIDATE_URL);

Where the %0A (URL encoded newline), in certain contexts, will split the comment from the JS code.

This can result in an XSS vulnerability.
joe at bloe dot com 14-Jul-2014 12:04
"(comment)localpart@example.com"
is an invalid E-Mail address per RFC5322 (Appendix A.6.3):
"Also, the comments and white space throughout addresses, dates, and message identifiers are all part of the obsolete syntax."
george at NOSPAM dot crownvalleysoftware dot com 01-May-2014 07:08
In the options, "logical disjunction" means "or" - for example:
filter_var($somestring ,FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
Tom 18-Nov-2013 07:23
It is important to note that though the data type of the first parameter of the function is stated as "mixed", this is only one half of the truth.

While it accepts any data type, the first parameter will always be cast to string before being validated or sanitized.

It seems that this function was designed strictly to be used on user input strings. For example: from an online-form. When using it for anything other than that, you may see issues. So read the documentation very carefully!

Especially note that there is an (to date) unresolved issue (#49510) concerning the Boolean filter while using the FILTER_NULL_ON_FAILURE flag. Note that both (string) FALSE and FALSE are not recognized as boolean values and will return NULL (not FALSE as you might expect).

I thus personally suggest that (to date) the best way to take the filter_var()-functions beyond their original purpose (and allow future extension and customization) is to wrap them in your own classes. This will allow you to work-around unexpected behavior on non-string input and add your custom checks, or back-port filters or sanitizers that may be added in later versions of PHP.
(Especially since PHP currently still lacks filters and sanitizers for some of the more exotic HTML5 input types, like "color". Thus there actually is a chance that we may see a need for custom filters or backports at some point in the future.)
CertaiN 01-Aug-2013 07:35
E-mail validator function that supports IPv6 and checking length.
This also supports Japanese old feature phone E-mail address by sending second argument $strict as FALSE.

<?php

function validate_email($email, $strict = true) {
    $dot_string = $strict ?
        '(?:[A-Za-z0-9!#$%&*+=?^_`{|}~\'\\/-]|(?<!\\.|\\A)\\.(?!\\.|@))' :
        '(?:[A-Za-z0-9!#$%&*+=?^_`{|}~\'\\/.-])'
    ;
    $quoted_string = '(?:\\\\\\\\|\\\\"|\\\\?[A-Za-z0-9!#$%&*+=?^_`{|}~()<>[\\]:;@,. \'\\/-])';
    $ipv4_part = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
    $ipv6_part = '(?:[A-fa-f0-9]{1,4})';
    $fqdn_part = '(?:[A-Za-z](?:[A-Za-z0-9-]{0,61}?[A-Za-z0-9])?)';
    $ipv4 = "(?:(?:{$ipv4_part}\\.){3}{$ipv4_part})";
    $ipv6 = '(?:' .
        "(?:(?:{$ipv6_part}:){7}(?:{$ipv6_part}|:))" . '|' .
        "(?:(?:{$ipv6_part}:){6}(?::{$ipv6_part}|:{$ipv4}|:))" . '|' .
        "(?:(?:{$ipv6_part}:){5}(?:(?::{$ipv6_part}){1,2}|:{$ipv4}|:))" . '|' .
        "(?:(?:{$ipv6_part}:){4}(?:(?::{$ipv6_part}){1,3}|(?::{$ipv6_part})?:{$ipv4}|:))" . '|' .
        "(?:(?:{$ipv6_part}:){3}(?:(?::{$ipv6_part}){1,4}|(?::{$ipv6_part}){0,2}:{$ipv4}|:))" . '|' .
        "(?:(?:{$ipv6_part}:){2}(?:(?::{$ipv6_part}){1,5}|(?::{$ipv6_part}){0,3}:{$ipv4}|:))" . '|' .
        "(?:(?:{$ipv6_part}:){1}(?:(?::{$ipv6_part}){1,6}|(?::{$ipv6_part}){0,4}:{$ipv4}|:))" . '|' .
        "(?::(?:(?::{$ipv6_part}){1,7}|(?::{$ipv6_part}){0,5}:{$ipv4}|:))" .
    ')';
    $fqdn = "(?:(?:{$fqdn_part}\\.)+?{$fqdn_part})";
    $local = "({$dot_string}++|(\"){$quoted_string}++\")";
    $domain = "({$fqdn}|\\[{$ipv4}]|\\[{$ipv6}]|\\[{$fqdn}])";
    $pattern = "/\\A{$local}@{$domain}\\z/";
    return preg_match($pattern, $email, $matches) &&
        (
            !empty($matches[2]) && !isset($matches[1][66]) && !isset($matches[0][256]) ||
            !isset($matches[1][64]) && !isset($matches[0][254])
        )
    ;
}

?>
gt at kani dot hu 21-Jun-2013 11:31
I found some addresses that FILTER_VALIDATE_EMAIL rejects, but RFC5321 permits:
<?php
foreach (array(
        'localpart.ending.with.dot.@example.com',
        '(comment)localpart@example.com',
        '"this is v@lid!"@example.com',
        '"much.more unusual"@example.com',
        'postbox@com',
        'admin@mailserver1',
        '"()<>[]:,;@\\"\\\\!#$%&\'*+-/=?^_`{}| ~.a"@example.org',
        '" "@example.org',
    ) as $address) {
    echo "<p>$address is <b>".(filter_var($address, FILTER_VALIDATE_EMAIL) ? '' : 'not')." valid</b></p>";
}
?>
Results:

localpart.ending.with.dot.@example.com is not valid
(comment)localpart@example.com is not valid
"this is v@lid!"@example.com is not valid
"much.more unusual"@example.com is not valid
postbox@com is not valid
admin@mailserver1 is not valid
"()<>[]:,;@\"\\!#$%&'*+-/=?^_`{}| ~.a"@example.org is not valid
" "@example.org is not valid

The documentation does not saying that FILTER_VALIDATE_EMAIL should pass the RFC5321, however you can meet with these examples (especially with the first one). So this is a note, not a bug report.
drew_mirage at hotmail dot com 19-Jun-2013 04:14
One key thing to remember about filtering integers is that the value for the option max_range must be less than or equal to the value of PHP_INT_MAX.

filter_var($someVariable, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => SOME_VALUE_GREATER_THAN_PHP_INT_MAX)));

This will fail even if $someVariable is a valid integer in the expected range.

This can show up when you are attempting to validate a potential key for an unsigned MySQL INT type (whose maximum value is 4294967295) on a 32-bit system, where the value of PHP_INT_MAX is 2147483647.
cabrinosimone at gmail dot com 03-Apr-2013 12:19
Pay attention that the function will not validate "not latin" domains.

if (filter_var('уникум@из.рф', FILTER_VALIDATE_EMAIL)) {
    echo 'VALID';
} else {
    echo 'NOT VALID';
}
alex4home at gmail dot com 10-Jan-2013 09:18
Keep in mind that FILTER_VALIDATE_EMAIL will validate the email address according to standards.
However, giving the fact that organizations are free to restrict the forms of their own email addresses, using ONLY this filter can you a lot of bounces.

gmail, yahoo, hotmail, aol have special rules

For example :
<?php

$email_a = '0hot\'mail_check@hotmail.com';
if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "This (email_a) email address is considered valid.";
   //reported as valid
}

//there can be no  "0hotmail_check@hotmail.com"
//because hotmail will say "Your email address needs to start with a letter. Please try again." even if you remove the '
?>
marcus at synchromedia dot co dot uk 03-Sep-2012 06:13
It's very likely that you actually want to detect all reserved ranges, not just private IPs, and there's another constant for them that should be bitwise-OR'd with it.
<?php
function is_private_ip($ip) {
    return !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
}
?>
Martin L 27-Jul-2012 07:35
FILTER_SANITIZE_EMAIL header injection test.

<?php
$InjString = "\r\n|\n|%0A|%0D|bcc:|to:|cc:|Content-Type:|Mime-Type:|";
echo filter_var($InjString, FILTER_SANITIZE_EMAIL);
?>

||%0A|%0D|bcc|to|cc|Content-Type|Mime-Type|
Andi, info at pragmamx dot org 24-May-2012 03:07
And this is also a valid url

http://example.com/"><script>alert(document.cookie)</script>
keevitaja at gmail dot com 29-Jan-2012 05:05
please note FILTER_VALIDATE_URL passes following url

http://example.ee/sdsf"f
joelhy 06-Jul-2011 11:44
For those looking for private ip checking, there it is:
<?php
 function is_private_ip($ip)
{
     return !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE);
}
?>
Luke America 05-Jun-2011 03:09
And ... if you also want to handle pre-encoded multi-byte international URL's, you can include the additional code here:

<?php

// convert multi-byte international url's by stripping multi-byte chars
$uri = urldecode($uri) . ' ';
$len = mb_strlen($uri);
if ($len !== strlen($uri))
{
        $convmap = array(0x0, 0x2FFFF, 0, 0xFFFF);
        $uri = mb_decode_numericentity($uri, $convmap, 'UTF-8');
}
$uri = trim($uri);

// now, process pre-encoded MBI's
$regex = '#&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);#i';
$uri_test = preg_replace($regex, '$1', htmlentities($uri, ENT_QUOTES, 'UTF-8'));
if ($uri_test != '') {$uri = $uri_test;}

?>
php at maisqi dot com 27-May-2011 04:11
FILTER_VALIDATE_URL does not support internationalized domain name (IDN). Valid or not, no domain name with Unicode chars on it will pass validation.

We can circumvent this with a home grown solutions, but C code is C code, so I've gone for the code bellow, which builds on filter_var().

<?php
$res = filter_var ($uri, FILTER_VALIDATE_URL);
if ($res) return $res;
// Check if it has unicode chars.
$l = mb_strlen ($uri);
if ($l !== strlen ($uri)) {
    // Replace wide chars by "X".
    $s = str_repeat (' ', $l);
    for ($i = 0; $i < $l; ++$i) {
        $ch = mb_substr ($uri, $i, 1);
        $s [$i] = strlen ($ch) > 1 ? 'X' : $ch;
    }
    // Re-check now.
    $res = filter_var ($s, FILTER_VALIDATE_URL);
    if ($res) {    $uri = $res; return 1;    }
}
?>

The logic is simple. A non-ascii char is more than one byte long. We replace every one of those chars by "X" and check again.

An alternative will be to punycode the URI before calling filter_var(), but PHP lacks native support for punycode. I think my approach is effective. Please e-mail me if you think otherwise or see room for improvement.
drtebi at yahoo 25-Nov-2009 08:55
Notice that filter_var with FILTER_VALIDATE_EMAIL does not work if you are trying to get a String from an XML document e.g. via xpath.

I often use XML files as configuration files and use a function that returns a string from the config file via xpath. While this worked fine before 5.2.11, it doesn't anymore (and shouldn't, since it's an XML Element, not a String).

To overcome this problem, $variable can be type-casted:

<?php
$variable = fancyXmlGetFunction('from');
filter_var((String) $variable, FILTER_VALIDATE_EMAIL);
?>
tedivm at tedivm dot com 22-Jun-2009 10:28
How to pass options and flags-

<?php
$options = array();
$options['options']['min_range'] = 1;
$options['options']['max_range'] = 10;
$options['flags'] = FILTER_FLAG_ALLOW_OCTAL;
filter_var(3, FILTER_VALIDATE_INT, $options);
?>
jon dot bertsch at ucop dot edu 24-Mar-2009 04:49
Here's an actual example of the filter syntax with a flag since there doesn't appear to be a one liner for this anywhere:

'hours' => array('filter'=>FILTER_SANITIZE_NUMBER_FLOAT, 'flags' => FILTER_FLAG_ALLOW_FRACTION, 'options'=> '.')
dyer85 at gmail dot com 03-Nov-2008 11:00
Note that when using FILTER_VALIDATE_INT along with the FILTER_FLAG_ALLOW_HEX flag, the string "2f", for example, is not validated successfully, because you must use the "0x" prefix, otherwise, it treats the data as base 10.

The range options are also smart enough to recognize when the boundaries are exceeded in different bases.

Here's an example:

<?php

$foo = '256';
$bar = '0x100';
var_dump(validate_int($foo)); // false, too large
var_dump(validate_int($bar)); // false, too large

function validate_int($input)
{
  return filter_var(
    $input,
    FILTER_VALIDATE_INT,

    // We must pass an associative array
    // to include the range check options.
    array(
      'flags'   => FILTER_FLAG_ALLOW_HEX,
      'options' => array('min_range' => 1, 'max_range' => 0xff)
    )
  );
}

?>
visseraj at gmail dot com 28-Aug-2008 07:31
Here are the other possible flags that you can use:
http://us3.php.net/manual/hu/ref.filter.php
dale dot liszka at gmail dot com 09-Jul-2008 07:15
Here is how to use multiple flags (for those who learn better by example, like me):

<?php
echo "|asdf".chr(9).chr(128)."_123|";
echo "\n";
// "bitwise conjunction" means logic OR / bitwise |
echo filter_var("|asdf".chr(9).chr(128)."_123\n|" ,FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);

/*
Results:
|asdf    ?_123|
|asdf_123|
*/
?>
dale dot liszka at gmail dot com 09-Jul-2008 06:54
Using the FILTER_CALLBACK requires an array to be passed as the options:

<?php
function toDash($x){
   return str_replace("_","-",$x);
}

echo filter_var("asdf_123",FILTER_CALLBACK,array("options"=>"toDash"));
// returns 'asdf-123'
?>
John 26-Jul-2007 09:35
I managed to get this to work with PHP 5.1.6 on CentOS 5 with minor difficulty.

1) Download the PECL filter package
2) Extract the tarball
3) phpize the directory
4) ./configure
5) make
6) filter-0.11.0/logical_filters.c:25:31: error: ext/pcre/php_pcre.h: No such file or directory
7) find / -name php_pcre.h
8) Make sure php-devel is installed
9) Edit filter-0.11.0/logical_filters.c and replace "ext/pcre/php_pcre.h" with the absolute path of php_pcre.h
10) make
11) make install
12) add "extension=filter.so" to php.ini
13) Restart Apache


Function Handling

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • Function handling 함수 목록
    • call_user_func_array — Call a callback with an array of parameters
    • call_user_func — Call the callback given by the first parameter
    • create_function — Create an anonymous (lambda-style) function
    • forward_static_call_array — Call a static method and pass the arguments as array
    • forward_static_call — Call a static method
    • func_get_arg — Return an item from the argument list
    • func_get_args — Returns an array comprising a function's argument list
    • func_num_args — Returns the number of arguments passed to the function
    • function_exists — Return TRUE if the given function has been defined
    • get_defined_functions — Returns an array of all defined functions
    • register_shutdown_function — Register a function for execution on shutdown
    • register_tick_function — Register a function for execution on each tick
    • unregister_tick_function — De-register a function for execution on each tick

User Contributed Notes

There are no user contributed notes for this page.


소개

These functions all handle various operations involved in working with functions.

User Contributed Notes

There are no user contributed notes for this page.


Function handling 함수 목록

Table of Contents

  • call_user_func_array — Call a callback with an array of parameters
  • call_user_func — Call the callback given by the first parameter
  • create_function — Create an anonymous (lambda-style) function
  • forward_static_call_array — Call a static method and pass the arguments as array
  • forward_static_call — Call a static method
  • func_get_arg — Return an item from the argument list
  • func_get_args — Returns an array comprising a function's argument list
  • func_num_args — Returns the number of arguments passed to the function
  • function_exists — Return TRUE if the given function has been defined
  • get_defined_functions — Returns an array of all defined functions
  • register_shutdown_function — Register a function for execution on shutdown
  • register_tick_function — Register a function for execution on each tick
  • unregister_tick_function — De-register a function for execution on each tick

User Contributed Notes

php dot spamguard at pulseforce dot com 28-Jan-2009 03:31
PHP is great in how it allows you to send a variable-length argument list to a function by using the func_* functions. However, unfortunately (and this has been brought up numerous time in PHP bug reports/feature requests), it doesn't support passing arguments by reference which would have greatly simplified processing multiple variables at once by simply doing: somefunction($a, $b, $c) and having them processed, without having to do any additional assignment.

However, I've discovered a way to leverage several features of PHP and created a very good example that shows how you can process multiple variables in one fell swoop!

The usage is (with my example function/class):
"list($a, $b, $c, $d, $e) = TextEscape::escape_multi($a, $b, $c, $d, $e);"

If PHP had ACTUALLY been capable of passing variables by reference when using variable-length argument lists, that could have been reduced to:
"TextEscape::escape_multi($a, $b, $c, $d, $e);"

It's extremely close though, both in simplicity and speed. Just be VERY sure to ALWAYS have the SAME NUMBER AND ORDER of arguments on both sides of that statement.

The use of a static class was just to make the code cleaner and easier to look at. This can be applied to any functions!

Now, onto the code, enjoy the sheer brilliance and enjoy modifying multiple variables at once in just ONE statement! ;-)

<?php

class TextEscape
{
    public static function escape_string($sString)
    {
        /* can be anything, using htmlspecialchars() in this example */
        return htmlspecialchars($sString);
    }

    public static function escape_multi()
    {
        /* get all variables passed to the function */
        $aVariables = func_get_args();

        /* loop through the array, escaping each variable */
        foreach ($aVariables as $iKey => $sValue)
        {
            $aVariables[$iKey] = TextEscape::escape_string($sValue);
        }

        /* return the escaped values */
        return $aVariables;
    }
}

// Create test variables
$a = "A<bar";
$b = "B>bar";
$c = "C<bar";
$d = "D>bar";
$e = "E<bar";

// Variables Before:
// Array
// (
//    [0] => A<bar
//    [1] => B>bar
//    [2] => C<bar
//    [3] => D>bar
//    [4] => E<bar
// )
print_r(array($a, $b, $c, $d, $e));

// Process every variable at once,
// we're exploiting both how list() and
// variable-length argument lists work
// to make the usage this smooth! ;-)
list($a, $b, $c, $d, $e) = TextEscape::escape_multi($a, $b, $c, $d, $e);

// Variables After:
// Array
// (
//    [0] => A&lt;bar
//    [1] => B&gt;bar
//    [2] => C&lt;bar
//    [3] => D&gt;bar
//    [4] => E&lt;bar
// )
print_r(array($a, $b, $c, $d, $e));

?>

That's the CLEAN LOOKING version of the code, which illustrates exactly what is HAPPENING.

I also made a speedfreak edition of the function, but didn't want to post it as the main example since it's harder to read.

But it's faster, much faster! It passes by reference where it can and loops in a more efficient way.

Just replace the above function with this one, the outcome is the same but the speed is better:

<?php

class TextEscape
{
    public static function escape_string(&$sString) // CHANGED: we now pass by reference to the actual text processing function
    {
        /* can be anything, using htmlspecialchars() in this example */
        $sString = htmlspecialchars($sString);
    }

    public static function escape_multi()
    {
        /* get all variables passed to the function */
        $aVariables = func_get_args();

        /* loop through the array, escaping each variable */
        for ($i=(count($aVariables)-1); $i>=0; $i--) // CHANGED: a more efficient for-loop is used instead of a foreach; and for anyone wondering about the counter declaration, this is a common optimization trick which makes it so that the size of the array only has to be checked ONCE, by initializing the counter to the index of the last element (which is size of array minus 1) and then iterating downwards until we reach 0 (the first element)
        {
            TextEscape::escape_string($aVariables[$i]); // CHANGED: this has been changed to modify the variable by reference instead
        }

        /* return the escaped values */
        return $aVariables;
    }
}

?>

Enjoy!
Michael T. McGrew 12-May-2005 06:56
An interesting way to use functions is by putting other things than PHP in them, i.e. HTML CSS. This can be useful if you will be using a format for many pages. Yes, the include function can be used also, but by defining them in functions can make them more portable in a way and keep you from having to make many include files. You could use include() when one will be putting big portions of PHP, HTML, and/or CSS and will be using them in many pages. Then you could use PHP, HTML, and/or CSS in a function when it is smaller portions of code and only want it to be on a few pages.

I have been using include() for HTML and CSS in my early days of PHP coding, but I will be using functions for that a lot more now.

<?php function a() {  ?>
<style type="text/css">
table {
border: 1px dashed black;
background: #95EAFE;
text-align:left;
width:610px;
}
.linkbar {
font-family:sans-serif;
line-height:40px;
top:1px;
text-align:center;
width:200px;
height:10px;
}
</style>
<?php }  ?>

Now doing:
<?php a(); ?>
will return all of the CSS just as if you had wrote it on the actual page, or used include() . The same thing goes for HTML code inside a function. The possibilities are endless...
michael dot bommarito at gmail dot com 30-Jan-2005 08:28
I've developed an easy-to-use hack using these functions to emulate overloaded functions.  It's especially handy for constructors and/or C++ style templating.

Here's a little example to get you going.  This does the trick for most circumstances, and if you need more intelligent parsing, it's not too hard to implement with regex or a more suitable classification scheme.

N.B. Note the lack of whitespace between variable types in case strings.

class Matrix {

...

function Matrix() {
  $args = func_get_args();
  $argmatch = implode(",", array_map('gettype', $args));

  switch( $argmatch ) {
    case 'integer,integer':
      //initialize m x n matrix
      break;

    case 'integer,integer,integer':
      //initialize m x n matrix with constant c
      break;

    case 'integer,integer,float':
      //initialize m x n matrix with constant c
      break;

    case 'array':
      //initialize from array (2D....)
      break;

    case 'array,integer':
      //intialize from array (1D packed with m rows)
      break;

    default:
      //(void) overload?
      //error handling?
      break;
  }
}

...

}
php-note-2003-june-18 at ryandesign dot com 18-Jun-2003 05:22
Xavier's example is rather complicated, and his task would be much more simply accomplished by using classes. Define a base class to do the basic functions open, dump, and close, and create extension classes to override whatever behavior.

class foo {
  function open() {
    // Default functionality for open()
  }
  function dump() {
    // Default functionality for dump()
  }
  function close() {
    // Default functionality for close()
  }
}

class bar extends foo {
  function open() {
    // Override functionality of open()
  }
  // dump() and close() remain as in class foo
}
xmontero at dsitelecom dot com 23-Aug-2002 11:49
You can do some kind of overloading functions in PHP using "function_exists".

Let's suppose you want a script to call plugins that are php files and you want the plugins to "override" behaviours that if not overriden are used by default.

You might for example write a "mySQL table viewer" which displays a table and it "opens a table", "dumps the header", "dumps row-by-row" and then "closes the table".

Let's suppose you want a plugin for "Stocks" which prints negative stocks in red, so you want to override the "dumping row-by-row" to do that behaviour. Instead you do not want to have all the "default behaviour" repeated in the plugin.

You may then do the following:

1) Define a default plugin (ex: "default.php"

2) Write all your functions in default.php to open, dump and close, but add them a suffix:

open_table_default()
dump_header_default()
dump_row_default()
close_table_default()

3) Call your functions with a wrapper: Insetad of this:
open_table() or open_table_default() write this:

plugincall("open_table");

4) Then write a function called plugincall which uses function_exists() to see if you call one or the other function:

function plugincall($desired_function_name)
{

if( function_exists( $desired_function_name))
{
    //Call the plugin
    //Note the $ before the name
    $desired_function_name()
}
else
{
  $desired_function_name = $desired_function_name . "_default";
  if( function_exists($desired_function_name))
  {
      //Call the default
      //Note the $ before the name
      $desired_function_name()
  }
  else
  {
      // Nor the plugin nor the default were defined
  }
}

So, now, if the require_once(plugin.php) contains a function called "open_table()", it will be used. Instaed, "open_table_default()" will be used.

It's not like overloading functions, but it is very useful to write scripts ampliable by other programmers: If they write a function, it is used, if not, the default is used.

See ya!
Xavier Montero.


Call a callback with an array of parameters

  • ≪ Function handling 함수 목록
  • call_user_func ≫
  • PHP Manual
  • Function handling 함수 목록
  • Call a callback with an array of parameters

call_user_func_array

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

call_user_func_array — Call a callback with an array of parameters

설명

mixed call_user_func_array ( callable $callback , array $param_arr )

Calls the callback given by the first parameter with the parameters in param_arr.

인수

callback

The callable to be called.

param_arr

The parameters to be passed to the callback, as an indexed array.

반환값

Returns the return value of the callback, or FALSE on error.

변경점

버전 | 설명
5.3.0 | The interpretation of object oriented keywords like parent and self has changed. Previously, calling them using the double colon syntax would emit an E_STRICT warning because they were interpreted as static.

예제

Example #1 call_user_func_array() example

<?php
function foobar($arg, $arg2) {
    echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {
    function bar($arg, $arg2) {
        echo __METHOD__, " got $arg and $arg2\n";
    }
}

// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));

// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>

위 예제의 출력 예시:

foobar got one and two
foo::bar got three and four

Example #2 call_user_func_array() using namespace name

<?php

namespace Foobar;

class Foo {
    static public function test($name) {
        print "Hello {$name}!\n";
    }
}

// As of PHP 5.3.0
call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));

// As of PHP 5.3.0
call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));

?>

위 예제의 출력 예시:

Hello Hannes!
Hello Philip!

Example #3 Using lambda function

<?php

$func = function($arg1, $arg2) {
    return $arg1 * $arg2;
};

var_dump(call_user_func_array($func, array(2, 4))); /* As of PHP 5.3.0 */

?>

위 예제의 출력:

int(8)

Example #4 Passing values by reference

<?php

function mega(&$a){
    $a = 55;
    echo "function mega \$a=$a\n";
}
$bar = 77;
call_user_func_array('mega',array(&$bar));
echo "global \$bar=$bar\n";

?>

위 예제의 출력:

function mega $a=55
global $bar=55

주의

Note:

Before PHP 5.4, referenced variables in param_arr are passed to the function by reference, regardless of whether the function expects the respective parameter to be passed by reference. This form of call-time pass by reference does not emit a deprecation notice, but it is nonetheless deprecated, and has been removed in PHP 5.4. Furthermore, this does not apply to internal functions, for which the function signature is honored. Passing by value when the function expects a parameter by reference results in a warning and having call_user_func() return FALSE (there is, however, an exception for passed values with reference count = 1, such as in literals, as these can be turned into references without ill effects — but also without writes to that value having any effect —; do not rely in this behavior, though, as the reference count is an implementation detail and the soundness of this behavior is questionable).

Note:

call_user_func()와 call_user_func_array() 등으로 등록한 콜백은 이전 콜백 실행시에 처리하지 않은 예외(uncaught exception)가 존재할 경우 호출되지 않습니다.

참고

  • call_user_func() - Call the callback given by the first parameter
  • callback형에 대한 정보
  • ReflectionFunction::invokeArgs() - Invokes function args
  • ReflectionMethod::invokeArgs() - Invoke args

User Contributed Notes

wriver4 at gmail dot com 02-Jul-2016 12:05
Kris dot Craig at gmail dot com dispatch table does not work currently, but this does.
<?php
define("YOUR_MOTHER", 1);
define("YOUR_FATHER", 2);
define("YOUR_BROTHER", 3);
define("YOUR_SISTER", 4);

class MyFamily
{

    static $dispatch = array(
        YOUR_MOTHER => "mom",
        YOUR_FATHER => "getPrisonInmate",
        YOUR_BROTHER => "replaceName",
        YOUR_SISTER => "lazyGirl");
    static $args = array(
        YOUR_MOTHER => array(),
        YOUR_FATHER => array(
            "55170-054",
            TRUE,
            3),
        YOUR_BROTHER => array(),
        YOUR_SISTER => array());

    function getDispatch($fromwhere)
    {
        return call_user_func_array(array(
            get_class(),
            self::$dispatch[$fromwhere]), self::$args[$fromwhere]);
    }

    function mom()
    {
        return "Mommy loves you!";
    }

    function getPrisonInmate($PrisonerID, $GoodBehavior, $remaining,
            $notes = null)
    {
        //Check prison records for his ID, then....

        if ($GoodBehavior)
        {
            $parole = "APPROVED";
        }
        else
        {
            $parole = "DENIED";
        }

        return "Your father (#$PrisonerID) has $remaining years left in his sentence.  His most recent parole application has been:  $parole";
    }

    function replaceName()
    {
        return "Her name is Sally now.";
    }

    function lazyGirl()
    {
        print "Your sister needs to get out more....";

        //sleep(pow(60, 2) * 18);  //You can sleep later!

        die("Nah, I'm too tired.");
    }

}
print "Status on family member: ".MyFamily::GetDispatch(YOUR_FATHER).'<br><br>';
james dot patrick at lifeshop dot vn 14-Oct-2015 01:00
I've found the solution to resolve my need while writing the str_replace function for processing the multi array as first two arguments of str_replace built-in function(although its pass each array of argument 1 & 2)

<?php
// custom the str_replace function
function p_str_replace($argFind, $agrReplace, $theString) {
  $needle_replace = $theString; // handle the string need to be replace
  array_walk($argFind, function($val, $key) use(&$needle_replace, $agrReplace) {
    $needle_replace = call_user_func_array('str_replace', array($val, $agrReplace[$key], $needle_replace));
  });
  return $needle_replace;
}

// test
$the_str = array(
  'coa' => ':col: :op1: :val: AND :col: :op2: :val:'
);
$ope = array('>=', '<=');
$colsdata = array('date_start', '2015-10-14');

echo p_str_replace(array(
          array(':col:', ':val:'),
          array(':op1:', ':op2:')
        ), array(
            $colsdata,
            $ope
        ), $the_str['coa']);

echo '-------------------------------' . "\n";

// more complexity
$complex = '(:col: :op1: :val: AND :col: :op2: :val:) AND (:col2: :op1: :val2: AND :col2: :op1: :val2:)';

echo p_str_replace(array(
  array(':col:', ':val:'),
  array(':col2:', ':val2:'),
  array(':op1:', ':op2:')
), array(
  array('date_start', '2015-10-01'),
  array('date_end', '2015-10-14'),
  array('>', '<')
), $complex);

?>

Sorry about my bad English :)
Hope this help someone.
Anonymous 11-Aug-2015 06:51
$param_arr may be empty, though it can't be null.

<?php
function foo( $first = 'default1', $second = 'default2' ) {
    echo "first: '$first', second: '$second'\n";
}
call_user_func_array( 'foo', array( 'one', 'two' ) );
call_user_func_array( 'foo', array( 'only one' ) );
call_user_func_array( 'foo', array() );
call_user_func_array( 'foo', null );
?>

Output:
first: 'one', second: 'two'
first: 'only one', second: 'default2'
first: 'default1', second: 'default2'
/* error message or nothing printed depending on version */
admin at torntech dot com 15-Jul-2015 11:06
As of PHP 5.6 you can utilize argument unpacking as an alternative to call_user_func_array, and is often 3 to 4 times faster.

<?php
function foo ($a, $b) {
     return $a + $b;
}

$func = 'foo';
$values = array(1, 2);
call_user_func_array($func, $values);
//returns 3

$func(...$values);
//returns 3
?>

Benchmarks from https://gist.github.com/nikic/6390366
cufa   with 0 args took 0.43453288078308
switch with 0 args took 0.24134302139282
unpack with 0 args took 0.12418699264526
cufa   with 5 args took 0.73408579826355
switch with 5 args took 0.49595499038696
unpack with 5 args took 0.18640494346619
cufa   with 100 args took 5.0327250957489
switch with 100 args took 5.291127204895
unpack with 100 args took 1.2362589836121
stanislav dot eckert at vizson dot de 10-Aug-2014 09:48
Please note, that when calling call_user_func_array() to redirect parameters between inherited classes, you should not use $this, because $this always refers to the class which has been instantiated. The following code even seems to crash PHP (PHP does not report error but the process simply terminates), because the the parameters are redirected only one level up (to class foo_bar2):

<?php

    class foo_bar1
    {
        public function __construct()
        {
            echo __CLASS__ . PHP_EOL;

            if (func_num_args() > 0)
            {
                $constructorArgs = func_get_args();
                call_user_func_array(array($this, 'parent::__construct'), $constructorArgs);
            }
            else
            {
                parent::__construct();
            }
        }
    }

    class foo_bar2 extends foo_bar1
    {
        public function __construct()
        {
            echo __CLASS__ . PHP_EOL;

            if (func_num_args() > 0)
            {
                $constructorArgs = func_get_args();
                call_user_func_array(array($this, 'parent::__construct'), $constructorArgs);
            }
            else
            {
                parent::__construct();
            }
        }
    }

    class foo_bar3 extends foo_bar2
    {
        public function __construct()
        {
            echo __CLASS__ . PHP_EOL;

            if (func_num_args() > 0)
            {
                $constructorArgs = func_get_args();
                call_user_func_array(array($this, 'parent::__construct'), $constructorArgs);
            }
            else
            {
                parent::__construct();
            }
        }
    }

    $f = new foo_bar3("abc");

?>

Instead, use the direct name of the class as string or, better, the magic constant __CLASS__ in call_user_func_array(), like:

    call_user_func_array(array(__CLASS__, 'parent::__construct'), $constructorArgs);

Then the parameters will be correctly redirected to the lowest base class.
jaxxed 10-Jul-2012 01:42
For anyone looking for the means to test for the first parameter before passing to this function, look at the is_callable (http://php.net/manual/en/function.is-callable.php) variable handler.

<?php

$handler = array( 'MyClass', 'MyMethod');
$params = array(1,2,3,4);

if ( is_callable($handler) ) { call_user_func_array( $handler , $params ); }

?>
Brad Proctor 07-Nov-2010 06:07
This function is relatively slow (as of PHP 5.3.3) and if you are calling a method with a known number of parameters it is much faster to call it this way:

$class->{$method}($param1, $param2);

vs

call_user_func_array (array($class, $method), array($param1, $param2));

But if you don't know how many parameters...

The wrapper function below is slightly faster, but the problem now is that you are making two function calls.  One to the wrapper and one to the function.

However, If you are able to take this code out of the function and use it inline it is nearly twice as fast (in most cases) as calling call_user_func_array natively.

<?php
function wrap_call_user_func_array($c, $a, $p) {
    switch(count($p)) {
        case 0: $c->{$a}(); break;
        case 1: $c->{$a}($p[0]); break;
        case 2: $c->{$a}($p[0], $p[1]); break;
        case 3: $c->{$a}($p[0], $p[1], $p[2]); break;
        case 4: $c->{$a}($p[0], $p[1], $p[2], $p[3]); break;
        case 5: $c->{$a}($p[0], $p[1], $p[2], $p[3], $p[4]); break;
        default: call_user_func_array(array($c, $a), $p);  break;
    }
}
?>
Kris dot Craig at gmail dot com 17-Jun-2010 12:28
Many people have wondered how to effectively implement dispatch tables in PHP.  Here's my answer to that (if you'll forgive my creative flair):

<?php

/*
 * Using dispatch tables in PHP.
 *
 * --Kris Craig
 */

define( "YOUR_MOTHER",  1 );
define( "YOUR_FATHER",  2 );
define( "YOUR_BROTHER", 3 );
define( "YOUR_SISTER",  4 );

class MyFamily
{
    static $dispatch = array( YOUR_MOTHER => "Mom", YOUR_FATHER => "GetPrisonInmate", YOUR_BROTHER => "ReplaceName", YOUR_SISTER => "LazyGirl" );
    static $args = array( YOUR_MOTHER => array(), YOUR_FATHER => array( "55170-054", TRUE ), YOUR_BROTHER => array(), YOUR_SISTER => array() );

    function GetDispatch( $fromwhere )
    {
        return call_user_func_array( array( self, self::$dispatch[$fromwhere] ), self::$args[$fromwhere] );
    }

    function Mom()
    {
        return "Mommy loves you!";
    }

    function GetPrisonInmate( $PrisonerID, $GoodBehavior )
    {
        //Check prison records for his ID, then....

        if ( $GoodBehavior )
        {
            $parole = "APPROVED";
        }
        else
        {
            $parole = "DENIED";
        }

        return "Your father (#$PrisonerID) has $remaining years left in his sentence.  His most recent parole application has been:  $parole";
    }

    function ReplaceName()
    {
        return "Her name is Sally now.";
    }

    function LazyGirl()
    {
        print "Your sister needs to get out more....";

        //sleep( pow( 60, 2 ) * 18 );  //You can sleep later!

        die( "Nah, I'm too tired." );
    }
}

print "Status on family member: " . MyFamily::GetDispatch( YOUR_FATHER );

?>
noone at example dot com 22-Apr-2010 12:03
For those of you that have to consider performance: it takes about 3 times as long to call the function this way than via a straight statement, so whenever it is feasible to avoid this method it's a wise idea to do so.

Note that eval() is about 10 times slower than a straight statement to call a function with arguments, so this is definitely a better option than using eval() even if you only consider performance.
aj at ajbrown dot org 23-Dec-2009 04:30
Just a heads up, the second parameter MUST be an array if it's specified,  but that doesn't seem to be enforced until ~5.3.

I just pulled my hair out with an old installation of CakePHP because it was passing NULL instead of an empty array.
mrextreme at freemail dot hu 07-Dec-2009 05:34
If you are using PHP < 5.3 and want to call the parent class' __construct() with a variable parameter list, use this:

<?php
public function __construct()
{
    $vArgs = func_get_args(); // you can't just put func_get_args() into a function as a parameter
    call_user_func_array(array('parent', '__construct'), $vArgs);
}
?>
dmitry dot revenko at businessmedia dot ru 25-Sep-2009 02:28
Just hope this note helps someone (I killed the whole day on issue).

If you use something like this in PHP < 5.3:
<?php call_user_func_array(array($this, 'parent::func'), $args); ?>
Such a script will cause segmentation fault in your webserver.

In 5.3 you should write it:
<?php call_user_func_array('parent::func', $args); ?>
Damin 13-Jun-2009 09:06
Those having the passing by reference issue can use this simple hack.
I´m really not sure WHY this works, but it does, and it does not make use of EVAL or other questionable functions.
<?php
    function executeHook($name, $type='hooks'){
        $args = func_get_args();
        array_shift($args);
        array_shift($args);
        //Rather stupid Hack for the call_user_func_array();
        $Args = array();
        foreach($args as $k => &$arg){
            $Args[$k] = &$arg;
        }
        //End Hack
        $hooks = &$this->$type;
        if(!isset($hooks[$name])) return false;
        $hook = $hooks[$name];
        call_user_func_array($hook, $Args);
    }
?>

All it´s doing is copying the args ($args) into a new array ($Args) by reference, which i would think would be identical to the original array in every way (that matters).

Note the code here is an example of usage. The actual hack is denoted by comments.
If someone knows a better alternative, by all means, i would love to see it.
Freek (at) Gruntjes.net 07-Apr-2009 10:53
I just noticed that when you use this function with parameters that need to be passed by reference it will not work.

<?php
function refFunc(&$var)
{
  $var .= 'bar';
}

$var = 'foo';
?>

call_user_func_array('refFunc', array($var));
echo $var;

will output 'foo' and not  'foobar'. Witch is logical since you are declaring a new variable with array($var) however not so obvious.
thiago dot henrique dot mata at gmail dot com 22-Jul-2008 11:17
<?php
Class Delegate
{
    private $arrInstances = array();

    protected function addObject( $oElement )
    {
        // add one element on the end of the stack  //
        $this->arrInstances[] = $oElement;
    }

    public function __call( $strMethod, $arrParams )
    {
        // for each element in instance //
        foreach( $this->arrInstances as $oElement )
        {
            // get the class of the element //
            $strClass = get_class( $oElement );
            // get all methods of the class  //
            $arrMethods = get_class_methods( $strClass );
            // case the method exists into this class  //
            if( in_array( $strMethod , $arrMethods ) )
            {
                // prepare caller //
                $arrCaller = Array( $strClass , $strMethod );
                // return the result of the method into the object  //
                return call_user_func_array( $arrCaller, $arrParams );
            }
        }
        // any object has the method //
        // throw a exception //
        throw new Exception( " Method " . $strMethod . " not exist in this class " . get_class( $this ) . "." );
    }
}

class Log
{
    public function sayHi()
    {
        print "hi!" . "<br/>\n";
    }

    public function sayMyName()
    {
        print "log" . "<br/>\n";
    }
}

class Other
{
    public function sayHello()
    {
        print "hello there!" . "<br/>\n";
    }

    public function sayMyName()
    {
        print "other" . "<br/>\n";
    }
}

class Example extends Delegate
{
    public function __construct()
    {
        $this->addObject( new Log() );
        $this->addObject( new Other() );
    }
}

$oExample = new Example();
$oExample->sayHi();
$oExample->sayHello();
$oExample->sayMyName();

/*
    hi!<br/>
    hello there!<br/>
    log<br/>
*/
?>
dnhuff at acm.org 07-Jun-2008 08:57
It appears that when PHP executes something like:

$a = array(1,2,3);
$b =& $a[1];

both $b and $a[1] are converted into references to a common value -- makes sense until you transfer that to a call_user_func:

call_user_func_array('foo', $a);

suddenly, inside foo, the second parameter is passed by reference!

And you can't call this wrong, only another subtly of references.

Note it appears that ksort($a) will remove the reference as well as put the elements in key order so you (probably) get what you expect. (see below on the use of a foreach ($a as &v).)
aeolianmeson at 8n54tvv dot blitzeclipse dot com 27-May-2008 05:48
There's a possibility that call_user_func_array(), call_user_func(), and Exception::getTrace() will cause a trace entry to not have the 'file' or 'line' elements.

Dustin Oprea
richard_harrison at rjharrison dot org 11-Apr-2007 11:31
If you are thinking of using call_user_func_array to instantiate an object (see comments below using Reflection) then since v5.1.3 you can use the Reflection::newInstanceArgs() method.

<?php

// arguments you wish to pass to constructor of new object
$args = array('a', 'b');

// class name of new object
$className = 'myCommand';

// make a reflection object
$reflectionObj = new ReflectionClass($className);

// use Reflection to create a new instance, using the $args
$command = $reflectionObj->newInstanceArgs($args);

// this is the same as: new myCommand('a', 'b');
?>
levi at alliancesoftware dot com dot au 08-Feb-2007 11:50
Regarding the comments below about calling parent constructors:

PHP5 with E_STRICT no longer allows calls as below:

<?php
// Causes an error with E_STRICT
call_user_func_array(array('parent', '__construct'), $args);
?>

It gives an error because you are trying to call a nonstatic function as if it was static. The correct syntax is

<?php
// Works fine
call_user_func_array(array($this, 'parent::__construct'), $args);
?>
15-May-2006 11:43
For those wishing to implement call-by-name functionality in PHP, such as implemented e.g. in DB apis, here's a quick-n-dirty version for PHP 5 and up
<?php
/**
 * Call a user function using named instead of positional parameters.
 * If some of the named parameters are not present in the original function, they
 * will be silently discarded.
 * Does no special processing for call-by-ref functions...
 * @param string $function name of function to be called
 * @param array $params array containing parameters to be passed to the function using their name (ie array key)
 */
function call_user_func_named($function, $params)
{
    // make sure we do not throw exception if function not found: raise error instead...
    // (oh boy, we do like php 4 better than 5, don't we...)
    if (!function_exists($function))
    {
        trigger_error('call to unexisting function '.$function, E_USER_ERROR);
        return NULL;
    }
    $reflect = new ReflectionFunction($function);
    $real_params = array();
    foreach ($reflect->getParameters() as $i => $param)
    {
        $pname = $param->getName();
        if ($param->isPassedByReference())
        {
            /// @todo shall we raise some warning?
        }
        if (array_key_exists($pname, $params))
        {
            $real_params[] = $params[$pname];
        }
        else if ($param->isDefaultValueAvailable()) {
            $real_params[] = $param->getDefaultValue();
        }
        else
        {
            // missing required parameter: mark an error and exit
            //return new Exception('call to '.$function.' missing parameter nr. '.$i+1);
            trigger_error(sprintf('call to %s missing parameter nr. %d', $function, $i+1), E_USER_ERROR);
            return NULL;
        }
    }
    return call_user_func_array($function, $real_params);
}
?>
eugene at artprime dot ru 23-Dec-2005 11:59
<?php
  return call_user_func_array(
    array(new ReflectionClass($className), 'newInstance'),
    $functionParameters
  );
?>

Look here: http://www.zend.com/zend/week/week182.php#Heading1
crocodile2u at yandex dot ru 20-Dec-2005 02:06
Here is another version of createObjArray() function written here earlier by taylor.

Believing that using 'eval()' is at least "dirty", I came to the following solution (with a help of panchous - at phpclub dot ru forums ). This solution utilizes the new Reflection API.

<?php
function & createObjArray($type, $args = array()) {
    $reflection = new ReflectionClass($type);
    $output     = call_user_func_array(array(&$reflection, 'newInstance'), $args);
    return $output;
}
?>
Egor 16-Nov-2005 06:35
Note that, despite the name, this does work on builtin functions (and object methods with the array(&$obj, $method) syntax), not just user-defined functions and methods.
rrant (at) gmail (dot) com 10-Nov-2005 12:50
Just an extra for the post of amer at o2 dot pl:

If you need to call the PARENT method:
call_user_func_array(array('parent', 'method'), $args);

With that, if you need to call a constructor and/or add some extra code to the instantiation process:

<?php
function __construct() {
    // Get the arguments
    $args = func_get_args();
    // Initialize parent with arguments
    call_user_func_array(array('parent', '__construct'), $args);
    // ... Your Code Here ...
}
?>

Note that your constructor pass all the arguments to the parent constructor and it doesn't matter how many arguments you pass.

This is pretty useful for constructors with a variable number of arguments.
taylor 02-Jun-2005 02:51
I came up with a better solution to the problem that I solve below with createObjArray that maintains parameter type:

<?php

function createObjArray($type,$args=array()) {
     $paramstr = '';
     for ($i = 0; $i < count($args); $i++) {
           $paramstr .= '$args['.$i.'],';
     }
     $paramstr = rtrim($paramstr,',');

     return eval("return new $type($paramstr);");
}

?>

Would be good to add error checking, but it works.
amer at o2 dot pl 20-Jan-2005 11:44
PLS notice that "patripaq at hotmail dot com" 's code will be valid if B EXTENDS A...
<?php
class B extends A{
...
}
?>
there>>"What I wanted to do is create an object that can manage any number and any kind of parameters."

BUT IT IS NOT A POINT AT ALL

If you need to call just function with parameters:
call_user_func_array('Foo',$args);

If you need to call CLASS method (NOT object):
call_user_func_array(array('class', 'Foo'),$args);

If you need to call OBJECT method:
call_user_func_array(array(&$Object, 'Foo'),$args);

If you need to call method of object of object:
call_user_func_array(array(&$Object->Object, 'Foo'),$args);

If you need to call object method from within the very same object (NOT CLASS!):
call_user_func_array(array(&$this, 'Foo'),args);

The call_user_func_array ITSELF can manage any number and any kind of parameters. It can handle ANY FUNCTION too as it is defined and that maybe partipaq wanted to manage.

What You actually need is object composition not inheritance. Make an instance from arguments.
<?php
...
class B{
   function __construct() {
      $args = func_get_args(); // Get arguments
      $this->OBJ = new A($args);
      call_user_func_array(array(&$this->OBJ, 'A'), $args );
   }
}
?>
Then there can be any number and any type of created object B parameters
james at gogo dot co dot nz 21-Nov-2004 02:19
Be aware the call_user_func_array always returns by value, as demonstrated here...

<?php
    function &foo(&$a)
    {
      return $a;
    }

    $b = 2;
    $c =& call_user_func_array('foo', array(&$b));
    $c++;
    echo $b . ' ' . $c;
?>

outputs "2 3", rather than the expected "3 3".

Here is a function you can use in place of call_user_func_array which returns a reference to the result of the function call.

<?php
    function &ref_call_user_func_array($callable, $args)
    {
        if(is_scalar($callable))
        {
            // $callable is the name of a function
            $call = $callable;
        }
        else
        {
            if(is_object($callable[0]))
            {
                // $callable is an object and a method name
                $call = "\$callable[0]->{$callable[1]}";
            }
            else
            {
                // $callable is a class name and a static method
                $call = "{$callable[0]}::{$callable[1]}";
            }
        }

        // Note because the keys in $args might be strings
        // we do this in a slightly round about way.
        $argumentString = array();
        $argumentKeys = array_keys($args);
        foreach($argumentKeys as $argK)
        {
            $argumentString[] = "\$args[$argumentKeys[$argK]]";
        }
        $argumentString = implode($argumentString, ', ');
        // Note also that eval doesn't return references, so we
        // work around it in this way...
        eval("\$result =& {$call}({$argumentString});");
        return $result;
    }
?>
hong dot nguyen at k-edge dot com 02-Apr-2004 06:37
call_user_func_array can pass parameters as reference:

<?php
call_user_func_array(array(&$obj,$method),array(&$arg1,$arg2,$arg3))
?>

Use it as work-around for "Call-time pass-by-reference has been deprecated".
adamh at densi dot com 31-Jul-2003 08:58
call_user_func_array() is nifty for calling PHP functions which use variable argument length. For example:

<?php
$array = array(
 array("foo", "bar"),
 array("bat", "rat"),
);

$values = call_user_func_array("array_merge", $array);

var_dump($values);
?>

/* output:
array(4) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
  [2]=>
  string(3) "bat"
  [3]=>
  string(3) "rat"
}
*/

The neat feature is that $array could have any number of arrays inside it.


Call the callback given by the first parameter

  • ≪ call_user_func_array
  • create_function ≫
  • PHP Manual
  • Function handling 함수 목록
  • Call the callback given by the first parameter

call_user_func

(PHP 4, PHP 5, PHP 7)

call_user_func — Call the callback given by the first parameter

설명

mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] )

Calls the callback given by the first parameter and passes the remaining parameters as arguments.

인수

callback

The callable to be called.

parameter

Zero or more parameters to be passed to the callback.

Note:

Note that the parameters for call_user_func() are not passed by reference.

Example #1 call_user_func() example and references

<?php
error_reporting(E_ALL);
function increment(&$var)
{
    $var++;
}

$a = 0;
call_user_func('increment', $a);
echo $a."\n";

// You can use this instead
call_user_func_array('increment', array(&$a));
echo $a."\n";
?>

위 예제의 출력:

0
1

반환값

Returns the return value of the callback.

변경점

버전 | 설명
5.3.0 | The interpretation of object oriented keywords like parent and self has changed. Previously, calling them using the double colon syntax would emit an E_STRICT warning because they were interpreted as static.

예제

Example #2 call_user_func() example

<?php
function barber($type)
{
    echo "You wanted a $type haircut, no problem\n";
}
call_user_func('barber', "mushroom");
call_user_func('barber', "shave");
?>

위 예제의 출력:

You wanted a mushroom haircut, no problem
You wanted a shave haircut, no problem

Example #3 call_user_func() using namespace name

<?php

namespace Foobar;

class Foo {
    static public function test() {
        print "Hello world!\n";
    }
}

call_user_func(__NAMESPACE__ .'\Foo::test'); // As of PHP 5.3.0
call_user_func(array(__NAMESPACE__ .'\Foo', 'test')); // As of PHP 5.3.0

?>

위 예제의 출력:

Hello world!
Hello world!

Example #4 Using a class method with call_user_func()

<?php

class myclass {
    static function say_hello()
    {
        echo "Hello!\n";
    }
}

$classname = "myclass";

call_user_func(array($classname, 'say_hello'));
call_user_func($classname .'::say_hello'); // As of 5.2.3

$myobject = new myclass();

call_user_func(array($myobject, 'say_hello'));

?>

위 예제의 출력:

Hello!
Hello!
Hello!

Example #5 Using lambda function with call_user_func()

<?php
call_user_func(function($arg) { print "[$arg]\n"; }, 'test'); /* As of PHP 5.3.0 */
?>

위 예제의 출력:

[test]

주의

Note:

call_user_func()와 call_user_func_array() 등으로 등록한 콜백은 이전 콜백 실행시에 처리하지 않은 예외(uncaught exception)가 존재할 경우 호출되지 않습니다.

참고

  • call_user_func_array() - Call a callback with an array of parameters
  • is_callable() - 변수 내용을 함수처럼 호출할 수 있는지 확인
  • callback형에 대한 정보
  • ReflectionFunction::invoke() - Invokes function
  • ReflectionMethod::invoke() - Invoke

User Contributed Notes

jcastro at eftec dot cl 08-Jun-2016 06:29
Lets say that $a1 is an object and fn is a function then:

echo $a1->fn('hello world');
echo $a1->{"fn"}('hello world');
echo call_user_func(array($a1, 'fn'),'hello world');

Are three valid ways to do the same.
Jordan LeDoux 06-Aug-2015 06:24
I re-ran the test insta outlined 9 years ago to see how things had changed.

Variable functions took 0.014201879501343 seconds.
call_user_func took 0.044676065444946 seconds.
eval took 0.17955899238586 seconds.

As expected, eval() is slowes by a wide margin, and variable functions are fastest. However, 9 years ago there weren't closures, and so I added them to the test for comparison.

closures took 0.012146949768066 seconds.

As you can see, closures ended up being the fastest, but all of these have experienced improvements. I also increased the number of loops per test to 50,000 from 5,000. This was tested with PHP 5.5.9 on Ubuntu LTS.

The original test code was:

<?php

function fa () { return 1; }
function fb () { return 1; }
function fc () { return 1; }

$calla = 'fa';
$callb = 'fb';
$callc = 'fc';

$time = microtime( true );
for( $i = 50000; $i--; ) {
    $x = 0;
    $x += $calla();
    $x += $callb();
    $x += $callc();
    if( $x != 3 ) die( 'Bad numbers' );
}
echo( "Variable functions took " . (microtime( true ) - $time) . " seconds.<br />" );

$time = microtime( true );
for( $i = 50000; $i--; ) {
    $x = 0;
    $x += call_user_func('fa', '');
    $x += call_user_func('fb', '');
    $x += call_user_func('fc', '');
    if( $x != 3 ) die( 'Bad numbers' );
}
echo( "call_user_func took " . (microtime( true ) - $time) . " seconds.<br />" );

$time = microtime( true );
for( $i = 50000; $i--; ) {
    $x = 0;
    eval( '$x += ' . $calla . '();' );
    eval( '$x += ' . $callb . '();' );
    eval( '$x += ' . $callc . '();' );
    if( $x != 3 ) die( 'Bad numbers' );
}
echo( "eval took " . (microtime( true ) - $time) . " seconds.<br />" );

?>

Here is the closures addition.

<?php

$funa = function() { return 1; };
$funb = function() { return 1; };
$func = function() { return 1; };

$time = microtime( true );
for ($i = 50000; $i--; ) {
    $x = 0;
    $x += $funa();
    $x += $funb();
    $x += $func();
    if( $x != 3 ) die( 'Bad numbers' );
}
echo( "closures took " . (microtime( true ) - $time) . " seconds.<br />" );

?>
dexen dot devries at gmail dot com 04-Nov-2011 02:00
In PHP v5.2, you /can/ use call_user_func(array($this, 'parent::SOME_FUNCTION')).

If you don't have custom __autoload() function, you are good to go.

If you do have custom __autoload(), you need to make it `parent' aware. Something like:

<?php
function __autoload($classname) {
  if ($classname === 'parent')
    return;
  else
    //((load class definition))
}
?>

Rationale: PHP 5.2 surprisingly tries to autoload a class named `parent'. However, if you don't do anything in __autoload() for the `parent' class, it'll work just fine.
blakerenton09 at yahoo dot com 06-Jun-2011 01:45
call_user_func may also be used to call a closure or anonymous function that has been passed into a user-defined function.

<?php
function Benchmark()
{
    foreach(func_get_args() as $function)
    {
        $st = microtime(true);
        call_user_func($function);
        $et = microtime(true);
        echo sprintf("Time: %f", $et - $st) . '<br />';
    }
}

Benchmark(function()
{
    for ( $i = 0; $i <= 10000; $i++ )
    { }
},
function()
{
    $i = 0;
    while ( $i <= 10000 )
    {
        $i++;
    }
});
?>

Returns:
Time: 0.001652
Time: 0.001458
romainbessuges at gmail dot com 09-May-2011 02:50
I made a wrapper for call_user_func_array which is really simple and convenient. It takes advantage of the __invoke() magic method :

<?php
class Func {

    public static function fromFunction($name){
        return new Func($name);
    }

    public static function fromClassMethod($class, $name){
        return new Func(array($class, $name));
    }

    public static function fromObjectMethod($object, $name){
        return new Func(array($object, $name));
    }

    public $function;

    public function  __construct($function) {
        $this->function = $function;
    }

    public function __invoke(){
        return call_user_func_array($this->function, func_get_args());
    }
}
?>

Example :

<?php
$func = Func::fromObjectMethod($myArrayObject, "offsetGet");

echo $func(3);
?>
gskluzacek dot nospam at gmail dot com 11-Sep-2010 07:38
if you simply want to dynamically call a method on an object it is not necessary to use call_user_function but instead you can do the following:

<?php

$method_name = "AMethodName";

$obj = new ClassName();

$obj->{$method_name}();

?>

I've used the above so I know it works.

Regards,
-- Greg
moisadoru at gmail dot com 13-May-2010 05:49
Actually, when calling a static method of some class, the params are sent by reference:
<?php
class Foo {
    private $x = 0;
    public function getX(){
        return $this->x;
    }
    public function setX($val){
        $this->x = $val;
    }
}

class Bar {
    static function incStatic(Foo $f){
        $f->setX($f->getX()+1);
        global $foo;
        if($f === $foo) echo '<span style="color: blue">' . __METHOD__ . '() objects are equal</span><br />';
        else  echo '<span style="color: red">' . __METHOD__ . '() objects are not equal</span><br />';
    }
    public function incNonStatic(Foo $f){
        $f->setX($f->getX()+1);
        if($f === $foo) echo '<span style="color: blue">' . __METHOD__ . '() objects are equal</span><br />';
        else  echo '<span style="color: red">' . __METHOD__ . '() objects are not equal</span><br />';
    }
}

function inc(Foo $f){
    $f->setX($f->getX()+1);
    if($f === $foo) echo '<span style="color: blue">' . __FUNCTION__ . '() objects are equal</span><br />';
    else  echo '<span style="color: red">' . __FUNCTION__ . '() objects are not equal</span><br />';
}

$foo = new Foo;
$bar = new Bar;

call_user_func('inc', $foo);
call_user_func('Bar::incStatic', $foo);
call_user_func(array($bar, 'incNonStatic'), $foo);
?>

The results on PHP 5.2.6 (macosx 10.5):

inc() objects are not equal
Bar::incStatic() objects are equal
Bar::incNonStatic() objects are not equal

Hope it helps sombody.
Nitrogen 05-Oct-2009 01:08
A good use for call_user_func(); is for recursive functions.
If you're distributing code, you will often come across users who will rename functions and break the code..
Use this: call_user_func(__FUNCTION__, ... ); inside a function to call itself with whatever parameters you want.

<?php
// example, an extremely simplified factorial calculator..
// it's quite obvious when someone renames the function, it'll spit out an error because it wants to call itself.
function Factorial($i=1) {
  return($i==1?1:$i*Factorial($i-1));
}

// you can give this function whatever name you want, it'll always work, of course if you initially call it using the name you gave it.
function qwertyuiop($i=1) {
  return($i==1?1:$i*call_user_func(__FUNCTION__,$i-1));
}
?>

Just that I didn't see any reference to recursive functions when user_call_func(); really helps.
edgar dot klerks at gmail dot com 17-Mar-2009 04:18
An entirely OO solution to add dynamicly methods to classes, I used in a project:
<?php
class ProductPart {

        protected $data;
        protected $plugins = array();
        function __construct($data){

                $this->data = $data;

        }
        public function register(ProductPlugin $plugin){

                if(!in_array($plugin, $this->plugins)){

                        $this->plugins[$plugin->toString()] = $plugin;
                } else {
                        throw new Exception('Function allready defined');
                }
        }
        public function unregister(ProductPlugin $plugin){
                if(isset($this->plugins[$plugin->toString()])){
                        unset($this->plugins[$plugin->toString()]);
                } else {
                        throw new Exception('No such function');
                }

        }

        protected function __call($method, $args) {
              if(isset($this->plugins[$method])){
                array_unshift(&$args, $this->data);
                array_unshift(&$args, $this);
                return $this->plugins[$method]->run($args[0], $args[1], $args[2]);

              } else {
                throw new Exception('No such function');
              }
        }
} ?>

I simplified the class somewhat for clearity.

With this class, you can dynamicly add and remove classes by calling register or unregister. Register will store the object in an associative array by calling toString (as defined by ProductPlugin) and saving the method under the returned string in the array. (In this case the name of the method the class adds.)

When a method is called, which isn't standard in the object, _call will lookup the called method in the array. If found, __call run the method of the plugin with the provided arguments. I restricted the user provided argument to 1, because I want to force the user to use associative arrays.

Because I chose an array to store my classes, removing a function is quite simple. However the unregister function isn't optimal, I better pass a string instead of a plugin object. I didn't test it yet on performance.

The ProductPlugin class:

<?php

abstract class ProductPlugin {
        protected $name = null;
        abstract public function run($obj, $data, $args);
        public function __construct($data = null) {

                if($this->name === null){
                        throw new Exception('Name must be defined');
                }
                $this->init($data);
        }
        protected function init($data){

        }
        public function toString(){
                return $this->name;
        }
}

?>

And at last some demonstration code:

<?php
$bla = new ProductPart(array('HelloWorld' => 'Hello world'));
$hello = new helloPlugin();
$bla->register($hello);
$bla->HelloWorld();
$bla->unregister($hello);
$bla->HelloWorld();

?>
dead,screamer@seznam,cz 21-Dec-2008 05:50
<?php call_user_func()?> and <?php call_user_func_array()?> hide some errors (notices, warnings and maybe errors), even if is display_errors turned on and error_Reporting is set to E_ALL|E_STRICT
Maxdamantus 04-Nov-2007 07:26
Note that the parser can interpret variables in function syntax, which means you can pass all variables normally (eg: reference).

<?php
function myfunction($string, &$int){
    echo $string."\n";
    $int++;
}
$function_name = "myfunction";
$x = 1336;
$function_name("Hello World", $x);
echo $x;
?>
---
Hello World
1337
Knightnet 07-Sep-2007 02:31
You don't need to use this function to call a variable class function. Instead you can do the following:

$this->{$fnname}();

The example works in PHP 5 from within the class. It is the {} that do the trick.

Regards,
Julian.
lsblsb at gmx dot de 29-Jun-2007 01:55
referring to Mr.KTO (and Bug #24931):
[system: PHP 5.2.0-8] - _doesnt_ pass by reference, at least when using the old syntax with the '&'.

when you pass your object by reference using call_user_func, any changes made on that object within the called function wont effect your "original" object.

you should use mixed call_user_func_array ( callback $function, array $param_arr ) instead or eval() (slower).
Mr.KTO 14-Jun-2007 04:39
To send an object to your function by link, using call_user_func it's not enough (in php4) to define func(&$obj) { ... }

You need: call_user_func("callback", &$myobj)

(in php5 objects are sending by link as default)
jaimz at vertigolabs dot org 27-Oct-2006 07:40
replay to bkfake-php at yahoo dot com's comment:

that's not passing by referance, your explicitly setting the value of $testy's member variable, infact, if you take the & out of your outsidefunc() it still works...
bkfake-php at yahoo dot com 26-Oct-2006 07:18
I have found a case where the parameters ARE passed by reference

class test
{
    function test()
    {
        $this->passme = '';
        call_user_func('outsidefunc',$this);
        // $this->passme is now 'not empty'
        // with 5.1.2
        // not so with php 4.3.10...
    }
}

function outsidefunc(&$testy)
{
     $testy->passme = 'not empty';
}
$test = new test();
echo 'passme = '.$test->passme;
Typer85 at gmail dot com 12-Sep-2006 12:57
I would just like to say at first that I prefer variable function calls over the use of this function.

However I found that at some times, the use of this function is needed in situtations where variable function calls can not be used. And in those same situations, the use of this function is also better than using eval.

The situation I ran into is this:

I wanted to call object methods dynamically from within the object but with arguments given for possible parameters that the method I will call requires.

The parameters themselves are dynamic, meaning I have no prior knowledge of how many their are, their values, or if they even exist.

Also because object methods are dynamic, meaning I have no prior knowledge which object method will actually be called, I can not simply use variable function calls, call the method and pass any parameters.

So what I simply do is call the object method with an array of parameters, something that, to the best of my knowledge, can not be done with variable function calls, since variable function calls, even though they themeselves are dynamic, passing parameters to them is not.
watchout at no dot spam dot please 29-Aug-2006 03:05
> This particular case will not work. The user functions can only be successfully called (as the documentation alludes to), with static calls to class functions.

actually it *does* work on PHP 5.0.2 (tested), and should also work on PHP 4.3.x (untested). Also the documentation does not state that only calls to static class methods are possible, but it says clearly that calls to OBJECT (thats dynamic) methods are possible: http://cz.php.net/manual/en/language.pseudo-types.php (see under type callback)
rlansky at webroot dot com 15-Jul-2006 01:17
Based on the previous posts, it appears that using call_user_func can be serveral times slower than using variable substitution. I think these results are somewhat misleading.

I set up a similar test in which a static method of an object was called repeatedly in a loop. I got similar results to those seen; when calling the method using call_user_func the execution was twice that of calling the method directly. However, I then started adding some "meat" to the method in question. In my case, I found that what was constant was not the percentage change, but rather that there is a fixed cost to using call_user_func.

In my case, this fixed cost was 2 microseconds per call. When executing a method that performs no operations, this is a large percentage of the execution time for that method. However, when using this on a method that actually performs some work, the 2 microsecond cost is almost impossible to measure.

It seems to me that if you want to use call_user_func to call a very fast executing method, and you need to do this thousands of times, then you may want to reconsider. However, if you are using this to call methods that are not executed thousands of times, the small fixed cost of using php call_user_func is probably not an issue.
Maresa 14-Apr-2006 12:41
I tested the same code that insta at citiesunlimited dot com pasted on the following machines:

www1 machine:
OS:  FreeBSD 5.2.1-RELEASE
CPU: 2 x Intel(R) Xeon(TM) CPU 2.66GHz (2657.82-MHz 686-class CPU) with Hyperthreading
MEM: 1073217536 (1023 MB)
PHP 5.1.2 (cli)
PHP 4.4.1 (Web)

www2 machine:
OS:  Linux version 2.6.14-gentoo-r5 Gentoo 3.4.3-r1, ssp-3.4.3-0, pie-8.7.7)
CPU: 2 x Dual Core AMD Opteron(tm) Processor 265 stepping 02 1808.357 MHz
MEM: 2060388k total
PHP 5.1.2 (cli)
PHP Version 4.4.0-pl1-gentoo (web)

dev machine:
OS:  Linux version 2.6.15-gentoo-r1 Gentoo 3.3.5.20050130-r1, ssp-3.3.5.20050130-1, pie-8.7.7.1
CPU: Intel(R) Pentium(R) 4 CPU 2.00GHz stepping 04
MEM: 516384k total,
PHP 4.4.0-pl1-gentoo (cli)
PHP Version 4.4.0-pl1-gentoo (web)

The result are as follows:

www1 - CLI
Variable functions took 0.012186050415 seconds.
call_user_func took 0.0300550460815 seconds.
eval took 0.17235994339 seconds.

www1 - Web
Variable functions took 0.017616 seconds.
call_user_func took 0.034926 seconds.
eval took 0.149618 seconds

www2 - CLI
Variable functions took 0.0065491199493408 seconds.
call_user_func took 0.019452095031738 seconds.
eval took 0.10734891891479 seconds.

www2 - Web
Variable functions took 0.01565 seconds.
call_user_func took 0.02613 seconds.
eval took 0.132258 seconds.

dev - CLI
Variable functions took 0.025176 seconds.
call_user_func took 0.047402 seconds.
eval took 0.168196 seconds.

dev - Web
Variable functions took 0.025465 seconds.
call_user_func took 0.049713 seconds.
eval took 0.20154 seconds.

On www1 - CLI, eval is about 14 times slower than calling function by using variable.
On www1 - Web, eval is about 8.5 times slower (hmm interesting. Perhaps PHP4 is faster calculating eval than PHP5)

On www2 - CLI, eval is about 16 times slower than calling function by using variable.
On www2 - Web, eval is about 8.5 times slower (about same result as www1)

On dev - CLI, eval is about 6.6 times slower than calling function by using variable.
On dev - Web, eval is about 8 times slower (about same result as www1)

On the dev machine, CLI and web version of PHP is the same. and their speed difference between calling function using variable or eval does not differ that much compare to PHP5 VS PHP5
insta at citiesunlimited dot com 06-Feb-2006 09:15
I benchmarked the comparison in speed between variable functions, call_user_func, and eval.  My results are below:

Variable functions took 0.125958204269 seconds.
call_user_func took 0.485446929932 seconds.
eval took 2.78526711464 seconds.

This was run on a Compaq Proliant server, 180MHz Pentium Pro 256MB RAM.  Code is as follows:

<?php

function fa () { return 1; }
function fb () { return 1; }
function fc () { return 1; }

$calla = 'fa';
$callb = 'fb';
$callc = 'fc';

$time = microtime( true );
for( $i = 5000; $i--; ) {
    $x = 0;
    $x += $calla();
    $x += $callb();
    $x += $callc();
    if( $x != 3 ) die( 'Bad numbers' );
}
echo( "Variable functions took " . (microtime( true ) - $time) . " seconds.<br />" );

$time = microtime( true );
for( $i = 5000; $i--; ) {
    $x = 0;
    $x += call_user_func('fa', '');
    $x += call_user_func('fb', '');
    $x += call_user_func('fc', '');
    if( $x != 3 ) die( 'Bad numbers' );
}
echo( "call_user_func took " . (microtime( true ) - $time) . " seconds.<br />" );

$time = microtime( true );
for( $i = 5000; $i--; ) {
    $x = 0;
    eval( '$x += ' . $calla . '();' );
    eval( '$x += ' . $callb . '();' );
    eval( '$x += ' . $callc . '();' );
    if( $x != 3 ) die( 'Bad numbers' );
}
echo( "eval took " . (microtime( true ) - $time) . " seconds.<br />" );

?>
Marco 05-Oct-2005 10:42
>phil at gettcomm dot com
>22-May-2002 04:51
>if you need to get a reference back from a method, you can work around
>call_user_func()'s shortcomings like this:
>
Naaa! Having back a reference is a real problem, but it can be solved by mean of eval(), instead of using call_user_func:

<?php
  class Node {
    var $name;
    var $child;
    function Node ( $name ) { $this->name = $name; }
    function &getChild () { return $this->child; }
  }
  $p = new Node ( 'Father' );
  $c = new Node ( 'Child' );
  $p->child = &$c;

  eval ( "\\$cref = &\\$p->getChild ();" );
  $cref->name = 'Another Child';

  // Prints out 'Another Child'
  echo "****** After eval c = " . $c->name . "\\n\\n";

?>
php at REMOVEMEkennel17 dot co dot uk 09-Aug-2005 05:46
re comment by mw at lanfear dot com

I am using PHP 4.3.2 and that technique works fine here as well.
mw at lanfear dot com 26-Jul-2005 10:49
This function is actually quite useful for calling static methods on classes, which you CANNOT call as:

$v1 = 'MyClass';
$m1 = 'method';
$v1::$m1();           // syntax error -- not permitted.

The following, however, DOES work quite well, and is hopefully slightly faster than eval() ...

$ar = array($v1, $m1);

call_user_func($ar);    // works teh awes0me!!1!

[I am, however, presuming PHP5 here ...]
info at webseiteselberpflegen dot de 18-May-2005 10:00
It seems like call_user_func() can not be used to create Objects via the new Command.

The Following example dosen't work:

<?php
  include_once(class_".$type.".php");
  $object = new call_user_func ('bdv_'.$type);
?>

But this works:

<?php
    include_once(class_".$type.".php");
    $constr = 'bdv_'.$type;
    $object = new $constr();
?>
d at safetypub dot org 15-Mar-2005 05:43
callbacks, this par excellence, goes with set error handler, everywhere you'ren't sure of extensio de intensionibus (animis alii).

<?php

$checkMe = null;
$detail = array();
set_error_handler("imErrant");

array_shift($argv);
if ($argc == 1){
        call_user_func(strtolower($argv[0]));
} else if($argc == 2){
        call_user_func(strtolower($argv[0]), $argv[1]);
}
else
        call_user_func_array(strtolower($argv[0]),array_shift($argv));

if($checkMe)var_export($detail);

function imErrant($no, $str, $file, $line)
{
    global $checkMe, $detail;
    $checkMe = $no;
    $detail = array($str, $file, $line);
}
?>
25-Feb-2005 04:46
<?php
/*
    A very simple event handler dispose and change the latest one I did
*/

class Duke
{
//public:
    var $m_pRaiser;
    var $m_strRaiserFun;
    var $m_objArgs;
//public:
    function Duke(  $pRaiser,
            $strRaiserFun,
            $objArgs )
    {
        $this ->m_pRaiser      = $pRaiser;
        $this ->m_strRaiserFun = $strRaiserFun;
        $this ->m_objArgs      = $objArgs;

    }//end of constructor

}//end of class Duke

class A
{
//protected:
    function OnEventHandler( $sender, $args )//virtual
    {
        $sender ->SayHello();
    }//end of OnEventHandler( $sender, $args )

//public:
    function OnEvent( $pDuke = null )
    {
        if( $pDuke == null )
        {
            call_user_func( array( &$this, "OnEventHandler" ), $this, null );
            return;
        }

        call_user_func( array( $pDuke ->m_pRaiser, $pDuke ->m_strRaiserFun ), $pDuke ->m_pRaiser, $pDuke ->m_objArgs );

    }//end of  OnEvent( $pDuke )

    function SayHello()//virtual
    {
        echo "A::SayHello" . "<br>";
    }//end of SayHello()

}//end of class A

class B extends A
{
//public:
    function SayHello()//override
    {
        echo "B::SayHello" . "<br>";
    }//end of SayHello()

}//end of class B

//////////////////////////////////////////////////////////////////////
//Main import to test
//
$a = &new A();
$b = &new B();

$pDuke = &new Duke( $b, "OnEventHandler", null );

$b ->OnEvent();
$b ->OnEvent( $pDuke );
?>
zxy at estec-jp dot com 24-Feb-2005 11:57
A simple event handler dispose released by useful callback in PHP 4.x

<?php
class Duke
{
//public:
    var $m_pRaiser;
    var $m_strRaiserFun;
    var $m_objArgs;
//public:
    function Duke(  $pRaiser,
            $strRaiserFun,
            $objArgs )
    {
        $this ->m_pRaiser      = $pRaiser;
        $this ->m_strRaiserFun = $strRaiserFun;
        $this ->m_objArgs      = $objArgs;

    }//end of constructor

}//end of class Duke

class A
{
//protected:
    function OnEventHandler( $sender, $args )//virtual
    {
        $sender ->SayHello();
    }//end of OnEventHandler( $sender, $args )

//public:
    function OnEvent( $pDuke )
    {
        call_user_func( array( $pDuke ->m_pRaiser, $pDuke ->m_strRaiserFun ), $pDuke ->m_pRaiser, $pDuke ->m_objArgs );
    }//end of  OnEvent( $pDuke )

    function SayHello()//virtual
    {
        echo "A::SayHello" . "<br>";
    }//end of SayHello()

}//end of class A

class B extends A
{
//public:
    function SayHello()//override
    {
        echo "B::SayHello" . "<br>";
    }//end of SayHello()

}//end of class B

///////////////////////////////////////////////////////////
//Main import to test
//
$a = &new A();
$b = &new B();

$pDuke = &new Duke( $b, "SayHello", null );
$b ->OnEvent( $pDuke );
?>
07-Oct-2004 09:23
If you are trying to instantiate an object from a class whose name is dynamic, you WILL NOT be able to do this with call_user_func() and I do not suggest that you do it with eval(). The way to do it is actually much simpler than i thought. As far as i know this works in PHP 4 and 5:

<?PHP
class foo {
 var $boo;
    function foo( $someVal ) {
     $this->boo = $someVal;
    }
}

$ClassToUse = "foo";
$bar = new $ClassToUse('test');
echo $bar->boo;
?>
arjini at mac dot com 30-Mar-2004 02:04
Some of the wierder examples below confused me, and made me think that the following would work (but it does!).

<?php

class barber{
    function shop($one,$two,$three,$four='quartet'){
        echo $one.','.$two.','.$three.','.$four;
    }
}

$bsq = new barber;
call_user_func(array(&$bsq,'shop'),'one','two','three');

/* Output = one,two,three,quartet */

class bigBarber{
    var $quartet;
    function bigBarber(){
        $this->quartet = 'four';
    }
    function shop($one,$two,$three,$five='quintet'){
        echo $one.','.$two.','.$three.','.$this->quartet.','.$five;
    }
}

$bbsq = new bigBarber();
call_user_func(array(&$bbsq,'shop'),'one','two','three');

/* Output = one,two,three,four,quintet */

?>
Kris Koskelin 23-Jan-2004 12:21
I was trying to use this function to call a method of an already-instantiated object.  I needed to do this with the object itself, not simply call the class' method.

To accomplish this, I really avoided this particular function altogether like this:

<?php
if ( method_exists($my_obj, $action) ){
     return $my_obj->{$action}();
}
?>

I hope someone else finds this useful.  Note that doing this allows you to pass params to the function more-or-less in the same way you would to any other class method.
Michele.Manzato at verona.miz.it 29-Sep-2003 05:49
With overload()ed classes call_user_func_* calls real class methods only. If the method does not exist then PHP does not try with the "__call()" magic method (at least until PHP 4.3.3). See this:

<?php
class A
{
    function A() {}

    function __call($method, $args, &$ret) {
        echo "__call(): You called '{$method}()'<br>\n";
        return true;
    }

    function regular() {
        echo "You called 'regular()'<br>\n";
    }
};
overload("A");

$a = new A;

$a->regular();                          // Works, calls regular()
call_user_func(array(&$a, "regular"));  // Works, calls regular()

$a->hello();                            // Works, calls __call()
call_user_func(array(&$a, "hello"));    // Does NOT work!
?>
Carl 28-Jul-2003 08:53
I had a problem where I wanted to parameterize a callback. The end called was in an external class, but I needed to save some state for the callback that didn't make sense to keep in the original object, as it might change from call to call...

<?php
class foo
{
    function foo()
    {
        $str = "Hello There";

        $str2 = "Carl";

        $that =& new holder($str);

        call_user_func(array(&$that, 'callback'), $str2);
    }
}

class holder
{
    function holder($aParam)
    {
        $this->param = $aParam;
    }

    function callback($aStr)
    {
        echo "A=$this->param, B=$aStr\n";
    }
}
?>
matt at tasonline dot com 23-Nov-2002 09:24
On PHP 4.2.3 (not sure about older releases) you can send objects by reference by sending the reference from call_user_func()

<?php
function myFunction(@$obj)
{
   $obj->doThis('hello');
   return 0;
}

$myObj = new CObject();

call_user_func('myFunction', @$myObj);
?>
phil at gettcomm dot com 22-May-2002 04:51
if you need to get a reference back from a method, you can work around call_user_func()'s shortcomings like this:

<?php

$globalObj;

class tClass
{
    function &instance()
    {
        global $globalObj;
        if(!is_object($globalObj))
        {
            $globalObj = new tClass();
        }
        return $globalObj;
    }
}

$classname = "tClass";
# calling the function this way won't
# return a reference.
$test = &call_user_func(array($classname,"instance"));
# but if we call it again with the instance
# that we just got, it'll be the right
# one
$test = &$test->instance();
# so test is now a reference to $globalObj, just like it should be

# let's verify it:
$test->blah=1;

echo "<pre>";
print_r($test)."\n";
print_r($globalObj);
echo "</pre>";
# there, now it behaves just like you'd expect

?>
dougqh at hotmail dot com 08-Mar-2002 02:16
The previous note I posted had an error in the source code.  That has been corrected in this note.

Note, that returning by reference does not work properly when the function
is called using call_user_func.

This example illustrates the problem...

<?php
$globalVar = 0;

function &staticFunction()
{
   global $globalVar;

   return $globalVar;
}

$result =& call_user_func( "staticFunction" );

$result = 3;

print "result:" . $result . "<br/>\n";
print "globalVar: " . $globalVar . "<br/>\n";

$result2 =& staticFunction();

$result2 = 3;

print "result2: " . $result2 . "<br/>\n";
print "globalVar: " . $globalVar . "<br/>\n";
?>

The above code results in the following output ...
Note that $result is not a reference to $globalVar.

result:0
globalVar: 0
result2: 3
globalVar: 3

Also, the use of call_user_method is now deprecated in favor of passing
array( &$object, $method ) as the function to call_user_func.  This is
reported when error reporting is set to E_ALL in the latest versions of
PHP.
danyloco at yahoo dot com 18-Jul-2001 01:31
I was trying to call multiple functions within a class, and after a brain frying experience this is what came out... hope it helps:

<?php
class Foo {
 function hey1($id){echo "In hey1";}
 function hey2($id){echo "In hey2";}
 #... and so forth ...
 function runtest($id) {
   #the fun part :)
   for($i=1; $i<=2; $i++) {
     $fp = "hey".$i;
     $this->$fp($id);
}
}
}
?>

It worked like a charm :).
andreyhristov at yahoo dot com 22-Aug-2000 03:09
Let say we construct somehow the name of the function. In some cases we can use nested  'switch'  but the alternative is to to use for and parse a string to construct  the  function  name, after than  we also can construct and the parameter(s) for it. It  is like using of eval. I use it in  one of my projects to construct a name of nested array like $ar['1999'['july']['29']['19'], etc.

the problem is in that I'm parsing a string to construct the name. If I've {1:2:3}  the array name  will be $ar['1']['2']['3'] but if i've
{1:2:3:4:5} it will  be $ar['1']['2']['3']['4']['5'].

Eval is the only way (very hard because of characters escaping is needed)  to do it. So this function is of the type of EVAL().
USE IT wherever your script is  'self-modified';


Create an anonymous (lambda-style) function

  • ≪ call_user_func
  • forward_static_call_array ≫
  • PHP Manual
  • Function handling 함수 목록
  • Create an anonymous (lambda-style) function

create_function

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

create_function — Create an anonymous (lambda-style) function

설명

string create_function ( string $args , string $code )

Creates an anonymous function from the parameters passed, and returns a unique name for it.

Caution

This function internally performs an eval() and as such has the same security issues as eval(). Additionally it has bad performance and memory usage characteristics.

If you are using PHP 5.3.0 or newer a native anonymous function should be used instead.

인수

Usually these parameters will be passed as single quote delimited strings. The reason for using single quoted strings, is to protect the variable names from parsing, otherwise, if you use double quotes there will be a need to escape the variable names, e.g. \$avar.

args

The function arguments.

code

The function code.

반환값

Returns a unique function name as a string, or FALSE on error.

예제

Example #1 Creating an anonymous function with create_function()

You can use this function, to (for example) create a function from information gathered at run time:

<?php
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
?>

Or, perhaps to have general handler function that can apply a set of operations to a list of parameters:

Example #2 Making a general processing function with create_function()

<?php
function process($var1, $var2, $farr)
{
    foreach ($farr as $f) {
        echo $f($var1, $var2) . "\n";
    }
}

// create a bunch of math functions
$f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}';
$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);";
$f3 = 'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }';
$farr = array(
    create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),
    create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),
    create_function('$a,$b', $f1),
    create_function('$a,$b', $f2),
    create_function('$a,$b', $f3)
    );

echo "\nUsing the first array of anonymous functions\n";
echo "parameters: 2.3445, M_PI\n";
process(2.3445, M_PI, $farr);

// now make a bunch of string processing functions
$garr = array(
    create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '.
    'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'),
    create_function('$a,$b', '; return "CRCs: " . crc32($a) . ", ".crc32($b);'),
    create_function('$a,$b', '; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";')
    );
echo "\nUsing the second array of anonymous functions\n";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
?>

위 예제의 출력:

Using the first array of anonymous functions
parameters: 2.3445, M_PI
some trig: -1.6291725057799
a hypotenuse: 3.9199852871011
b*a^2 = 4.8103313314525
min(b^2+a, a^2,b) = 8.6382729035898
ln(a)/b = 0.27122299212594

Using the second array of anonymous functions
** "Twas the night" and "Twas brilling and the slithy toves"
** Look the same to me! (looking at the first 3 chars)
CRCs: -725381282, 342550513
similar(a,b) = 11(45.833333333333%)

But perhaps the most common use for of lambda-style (anonymous) functions is to create callback functions, for example when using array_walk() or usort()

Example #3 Using anonymous functions as callback functions

<?php
$av = array("the ", "a ", "that ", "this ");
array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));
print_r($av);
?>

위 예제의 출력:

Array
(
  [0] => the mango
  [1] => a mango
  [2] => that mango
  [3] => this mango
)

an array of strings ordered from shorter to longer

<?php

$sv = array("small", "larger", "a big string", "it is a string thing");
print_r($sv);

?>

위 예제의 출력:

Array
(
  [0] => small
  [1] => larger
  [2] => a big string
  [3] => it is a string thing
)

sort it from longer to shorter

<?php

usort($sv, create_function('$a,$b','return strlen($b) - strlen($a);'));
print_r($sv);

?>

위 예제의 출력:

Array
(
  [0] => it is a string thing
  [1] => a big string
  [2] => larger
  [3] => small
)

참고

  • Anonymous functions

User Contributed Notes

kexianbin at diyism dot com 10-Jun-2014 01:52
I use this create_closure() in my work to seperate callbacks into Classes:
<?php
function create_closure($fun, $args, $uses)
         {$params=explode(',', $args.','.$uses);
          $str_params='';
          foreach ($params as $v)
                  {$v=trim($v, ' &$');
                   $str_params.='\''.$v.'\'=>&$'.$v.', ';
                  }
          return "return function({$args}) use ({$uses}) {{$fun}(array({$str_params}));};";
         }
?>

example:
<?php
$loop->addPeriodicTimer(1, eval(create_closure('pop_message', '$timer', '$cache_key, $n, &$response, &$redis_client')));

function pop_message($params)
         {extract($params, EXTR_REFS);
          $redis_client->ZRANGE($cache_key, 0, $n)
                            ->then(//normal
                                   function($data) use ($cache_key, $n, &$timer, &$response, &$redis_client)
                                   {//...
                                   },
                                   //exception
                                   function ($e) use (&$timer, &$response, &$redis_client)
                                   {//...
                                   }
                                  );
         }
?>
coleyeder at gmail dot com 02-Oct-2013 01:29
Default values works:

<?php

$f = create_function('$a,$b=3', 'var_dump($a, $b);');
$f(1);

// output:

// int(1)
// int(3)

?>
CertaiN 20-Sep-2013 08:49
Best wapper:

<?php

function create_lambda($args, $code) {
    static $func;
    if (!isset($func[$args][$code])) {
        $func[$args][$code] = create_function($args, $code);
    }
    return $func[$args][$code];
}
ionix at NOSPAM dot post dot com 06-Sep-2013 01:56
I experimented a bit in creating a lambda function using a variable amount of arguments. While I couldn't find an efficient way to create a random bit of code, I was able to get it to behave as a front end for existing functions (as well as user-defined of course).

The code required me to use the dreaded eval() function to get it to use a variable amount of parameters, so be sure that all input is thoroughly cleaned if derived from the user.

<?php

    function run_function( $function )
    {
        // Get Arguments, Unset Exisitng Parameter
        $params = func_get_args();
        unset( $params[0] );

        if( ( $count = count( $params ) ) > 0 )
        {
            $args = '$a';
            $inc = 'b';

            // Create Argument String - Formats as '$a, $b, $c' Per Number of Arguments
            for( $x = 1; $x < $count; $x++ )
            {
                $args .= ', $' . $inc;
                $inc++;
            }

            // Create Lambda Function and Format Paramters
            $lambda = create_function( $args, 'return ' . $function . '(' . $args . ');' );
            $params = "'" . implode('\', \'', $params) . "'";

            // Build and Evaluate Function with Parameters
            $eval = '$return = $lambda(' . $params . ');';
            eval($eval);

            return $return;
        }
        else
        {
            return false;
        }
    }
?>

For most applications you would be better off calling the function normally. Perhaps this little proof of concept may find some use if adapted to work by someone who requires it.
CertaiN 25-Aug-2013 03:20
How to save memory-space:

<?php

function create_lambda($args, $code) {
    static $list = array();
    $i = "{$args}\0{$code}";
    if (!isset($list[$i])) {
        $list[$i] = create_function($args, $code);
    }
    return $list[$i];
}
kak dot serpom dot po dot yaitsam at gmail dot com 27-Jun-2012 09:17
Try this to boost performance of your scripts (increase maxCacheSize):

<?php
runkit_function_copy('create_function', 'create_function_native');
runkit_function_redefine('create_function', '$arg,$body', 'return __create_function($arg,$body);');

function __create_function($arg, $body) {
    static $cache = array();
    static $maxCacheSize = 64;
    static $sorter;

    if ($sorter === NULL) {
        $sorter = function($a, $b) {
            if ($a->hits == $b->hits) {
                return 0;
            }

            return ($a->hits < $b->hits) ? 1 : -1;
        };
    }

    $crc = crc32($arg . "\\x00" . $body);

    if (isset($cache[$crc])) {
        ++$cache[$crc][1];
        return $cache[$crc][0];
    }

    if (sizeof($cache) >= $maxCacheSize) {
        uasort($cache, $sorter);
        array_pop($cache);
    }

    $cache[$crc] = array($cb = eval('return function('.$arg.'){'.$body.'};'), 0);
    return $cb;
}
?>
131 dot php at cloudyks dot org 01-Jun-2012 07:54
For those who want to create closure with create_function, don't bother, you can't.
Use create_closure instead :p

<?php
//same thing as create_function, but returns a closure..( 5.4)
function create_closure($args, $body){
  return eval("return function($args){ {$body}  };");
}
?>
Dave H 24-Mar-2011 03:47
The following function is very useful for creating an alias of a user function.
For built-in functions, it is less useful because default values are not available, so function aliases for built-in functions must have all parameters supplied, whether optional or not.

<?php
function create_function_alias($function_name, $alias_name)
{
    if(function_exists($alias_name))
        return false;
    $rf = new ReflectionFunction($function_name);
    $fproto = $alias_name.'(';
    $fcall = $function_name.'(';
    $need_comma = false;

    foreach($rf->getParameters() as $param)
    {
        if($need_comma)
        {
            $fproto .= ',';
            $fcall .= ',';
        }

        $fproto .= '$'.$param->getName();
        $fcall .= '$'.$param->getName();

        if($param->isOptional() && $param->isDefaultValueAvailable())
        {
            $val = $param->getDefaultValue();
            if(is_string($val))
                $val = "'$val'";
            $fproto .= ' = '.$val;
        }
        $need_comma = true;
    }
    $fproto .= ')';
    $fcall .= ')';

    $f = "function $fproto".PHP_EOL;
    $f .= '{return '.$fcall.';}';

    eval($f);
    return true;
}
?>
phenno7 [a_t] yahoo dot com 15-Sep-2010 10:53
The function takes as arguments the tree array, the category id on which i want the retrieval, and the depth level for which i would like the subcategories ids returned.

this saves me a LOT of time on large adjacency list trees.

<?php
$ctg_tree = array(
'1'=>array('parent_id'=>0,
           'children'=>array(
          '2'=>array('parent_id'=>1,
                      'children'=>array(
                      '3'=>array('parent_id'=>2))),
           '4'=>array('parent_id'=>1,
                      'children'=>array(
                      '5'=>array('parent_id'=>4),
                      '6'=>array('parent_id'=>4))))));

function getSubCategories($ctg_tree, $id, $level = null) {

        $getBranchIds =
create_function(
'$tree, $ctg_id, $level = null, $arr = null,'.
' $push = false, $getBranchIds','
            if(!$arr) {
                $arr = array();
            }
            if(intval($level) && $push){
                $level--;
            }

            foreach($tree as $key=>$ctg) {
                if($push) {
                    $arr[] = $key;
                }
                if($ctg_id == $key) {
                    $start = true;
                    $push = true;
                }
if(count($ctg[\'children\']) && (intval($level) || $level === null)) {
$getBranchIds(
$ctg[\'children\'],$ctg_id,$level,&$arr,$push,$getBranchIds
);
                }
                if($start) return $arr;
            }

            return $arr;');

return $getBranchIds(
$ctg_tree, $id, $level, null, false, $getBranchIds
);
    }

print_r(getSubCategories($ctg_tree, 4, 1));
?>

Array
(
    [0] => 5
    [1] => 6
)

print_r(getSubCategories($ctg_tree, 1, 1));

Array
(
    [0] => 2
    [1] => 4
)

<?php
print_r(getSubCategories($ctg_tree, 1));
?>

Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
)
Its Will 01-Sep-2010 05:27
If you create a function that will only be used from an object context (i.e. you want a dynamic method that can then call methods from the original object, still maintaining access to the object's runtime values) then you can use the following functions I have created (ob_lambda_func and ob_lambda) to enable the dynamic function to easily call *public* methods on the object, in their runtime contexts:

<?php
/* the OB Lambda functions allow a lambda function to call a method from its callee's object */
if(!function_exists('ob_lambda_func')){
function ob_lambda_func($method, $args) {
   if((phpversion()+0)<5.1 || (substr(phpversion(),2)+0)<1.1)
       die("\nError: This script requires PHP v5.1.1+!\n");
   $bt=debug_backtrace();
   foreach($bt as $xsp) {
       if(isset($xsp['object'])) {
           if(!method_exists($xsp['object'], $method)) continue;
           return call_user_func_array(array($xsp['object'], $method), $args);
       }
   }
   if(!function_exists($method))
       die("\nOB: Internal Error! ($method)");
   return call_user_func_array($method, $args);
}}
if(!function_exists('ob_lambda')){
function ob_lambda($method) {
   return create_function('','$args=func_get_args(); return ob_lambda_func('.var_export($method, true).',$args);');
}}

/* Usage:
 *   For each callable method, store the results of ob_lambda('method_name')
 *   When a lambda function you call wants to access one of these methods,
 *    you need to pass it the result of ob_lambda for that method, the easiest
 *    way is to have one of the lambda functions arguments accept the result
 *       i.e.:  $myFunc=ob_lambda('myMethod');
 *              $lf1=create_function('$func', '$func( ... args ... )');
 *              $lf1($myFunc);
 *    because create_function prepends a \0 to the function name, it is difficult
 *    to pass the result of ob_lambda to the target function without using an argument
 */
/* here is an example -- lambda function Foo::test can call Foo::Bar and Foo::Baz, and these  */
class Foo {
    private $bar, $baz, $test;
    private $runtimeValue=0;
    function __construct() {
        $this->runtimeValue=rand();
        $this->bar=ob_lambda('Bar');
        $this->baz=ob_lambda('Baz');
        $this->test=create_function(
            '$bar, $baz',
            '$bar("Hello, World!"); $baz();'
        );
    }
    function Test() {
        $fn=$this->test;
        $fn($this->bar, $this->baz);
    }
    function Bar($a) {
        echo "$a: Bar ($this->runtimeValue)\n";
    }
    function Baz() {
        echo "and another! Baz ($this->runtimeValue)\n";
    }
}

$foo=new Foo();
// Foo::Bar("Testing Other Method"); <-- this causes fatal error
//  (using $this when not in object context)
$foo->Test();

?>

This snippet (with test) shows how the lambda function $foo->test can call Foo::Bar and Foo::Baz without an explicit reference to the original object. The methods are run from their object context, and so can access the runtime-modified variable $foo->runtimeValue, which is set to a random number on construction.
The function definition for $foo->test is the following:
function ($bar, $baz) {
    $bar("Hello, World!");
    $baz;
}
As you can see, it is clean and simple, with no obvious artifacts from the use of OB Lambda

The expected output should be:
Hello, World!: Bar (440048505)
and another! Baz (440048505)

Or any other random number in place of 440048505
Make sure, before you change your code, that your target server has PHP 5.1.1+, or these functions won't work and will return an error!
Hope this helps anyone as much as it has me! :)
julius _email _me _at grantzau _dot _com 16-Jul-2010 06:59
To avoid memory problems, when creating a dynamic function within loops, register it as a global variable, and check if it has already been set;

<?php
global $my_func;
if (!isset($my_func))
{
    $my_func = create_function($args, $code);
}

$my_func();
?>

That way you don't end up creating the same function twice (or a couple of million of times, as it happened to me...)
v dot churakov at i2d dot ru 20-Feb-2010 01:56
<?

function is_function( &$mixed )
{
    if ( is_object( $mixed ) ) {
        return ( $mixed instanceof Closure );
    } elseif( is_string( $mixed ) ) {
        return function_exists( $mixed );
    } else {
        return false;
    }
}

function myfunc(){}

$test = 123;
echo  is_function( $test ); //will return false

$test = 'isset';
echo  is_function( $test ); //will return false... it's not work with reserved :( ...

$test = 'myfunc';
echo  is_function( $test ); //will return true

$test = create_function( '', 'echo 123;' );
echo  is_function( $test ); //will return true

$test = function () { echo 123; };
echo  is_function( $test ); //will return true

?>
edgar at goodforall dot eu 02-Jul-2009 04:45
Just a little toy I thought up, I would like to share. Creates an anonymous function, which let you use a class  as a function.

In php 5.3 there is support for real functors  (trough __invoke):

<?php
function createFunctor($className){
        $content = "
                static \$class;
                if(!\$class){
                        \$class = new $className;
                }
                return \$class->run(\$args);
        ";
        $f = create_function('$args', $content);
        return $f;

}
class test {
        public function run($args){
                print $args;
        }
}
$test = createFunctor('test');
$test('hello world');
?>
Stefan Scheld 23-Sep-2008 04:48
Workaround is as follows:

<?php
class Foo {
    function setHandler($handlerName, $handlerCode) {
        $this->handlerFunctions[$handlerName] = create_function('&$self, $dataArr', $handlerCode);
// NOTE: $this not allowed here, using $self as replacement
    }

    function executeHandler($handlerName, $dataArr) {
        if($this->handlerFunctions[$handlerName]) {
                $this->handlerFunctions[$handlerName](&$this, $dataArr);
            }
     }

    function getConstantInfo($what) {
        return($what);
    }
}

$bla = new Foo;

$bla->setHandler("load", 'echo($self->getConstantInfo("name")."\n");');
$bla->executeHandler("load", array("foo" => "bar"));
?>
neo at nowhere dot com 28-Aug-2008 10:56
In response to kkaiser at revolution-records dot net's note, even tho PHP will allow you to use
<?
$myfunc = create_function('$this', $code);
?>
You can NOT use a reference to "$this" inside of the anonymous function, as PHP will complain that you are using a reference to "$this" in a non-object context.

Currently, I have not found a work-around for this...
Alan FUNG 07-Aug-2008 09:24
$f = create_function('','echo "function defined by create_function";');
$f();

result:
function defined by create_function

You may define no return in function body while you are using create_function.
Rene Saarsoo 06-Feb-2008 11:31
Here has been some discussion about the "memory leak" create_function() can create.

What create_function() actually does, is creating an ordinary function with name chr(0).lambda_n where n is some number:

<?php
$f = create_function('', 'return 1;');

function lambda_1() { return 2; }

$g = "lambda_1";
echo $g(); // outputs: 2

$h = chr(0)."lambda_1";
echo $h(); // outputs: 1
?>
colin dot mckinnon at gmail dot com 20-Aug-2007 02:51
In response to koyama at hoge dot org (14-Dec-2000):

This does NOT create a new method - try adding this at the end:

<?php
if (function_exists($h->lamda)) {
   print "Its a function\n";
} else {
   print "No it isnt";
}
?>

It creates a function which $h->lamda points to.

Under PHP4 you could simply add an argument $this which meant it *behaved* like a method (though it existed in global scope) but with PHP5, you can't have a variable named $this in a function (which is rather irksome).

(and methinks the ant-bot challenge is taking the mickey - min(three, four)?  !).
TSE-WebDesign 18-Aug-2007 06:55
Here's how to call a runtime-created function from another runtime-created function:
<?php
        $get_func = create_function('$func', 'return substr($func,1);');
        $get_value = create_function('$index','return pow($index,$index);');
        $another_func = create_function('$a', '$func="\x00"."'.$get_func($get_value).'";return $func($a);');
        echo $another_func(2); # result is 4
?>
kkaiser at revolution-records dot net 13-Apr-2007 10:10
In the process of migrating a PHP4 codebase to PHP5, I ran into a peculiar problem. In the library, every class was derived from a generic class called 'class_container'. 'class_container' contained an array called runtime_functions and a method called class_function that was as follows:

<?php
function class_function($name,$params,$code) {

  $this->runtime_functions[$name] = create_function($params,$code);

}
?>

In a subclass of class_container, there was a function that utilized class_function() to store some custom lambda functions that were self-referential:

<?php
function myfunc($name,$code) {

  $this->class_function($name,'$theobj','$this=&$theobj;'.$code);

}
?>

In PHP4, this worked just fine. The idea was to write blocks of code at the subclass level, such as "echo $this->id;", then simply $MYOBJ->myfunc("go","echo $this->id;"); and later call it like $MYOBJ->runtime_functions["go"]();

It essentially worked exactly like binding anonymous functions to objects in Javascript.

Note how the "$this" keyword had to be manually redefined for the $code block to work.

In PHP5, however, you can't redeclare $this without getting a fatal error, so the code had to be updated to:

<?php
function myfunc($name,$code) {

  $this->class_function($name,'$this',$code);

}
?>

Apparently create_function() allows you to set $this via a function argument, allowing you to bind anonymous functions to instantiated objects. Thought it might be useful to somebody.
Dan D 24-Oct-2006 04:22
Beware when using anonymous functions in PHP as you would in languages like Python, Ruby, Lisp or Javascript.  As was stated previously, the allocated memory is never released; they are not objects in PHP -- they are just dynamically named global functions -- so they don't have scope and are not subject to garbage collection.

So, if you're developing anything remotely reusable (OO or otherwise), I would avoid them like the plague.  They're slow, inefficient and there's no telling if your implementation will end up in a large loop.  Mine ended up in an iteration over ~1 million records and quickly exhasted my 500MB-per-process limit.
Phlyst 06-Oct-2006 02:10
In reply to info at adaniels dot nl:

You may not be able to use __FUNCTION__ in a lambda (thanks for pointing it out; I was having that problem just now), but you can use $GLOBALS to work around it if you're assigning the function to a variable. I reimplemented array_walk_recursive() in PHP4 like this:

<?php
$array_walk_recursive = create_function('&$array, $callback',
    'foreach($array as $element) {
        if(is_array($element)) {
            $funky = $GLOBALS["array_walk_recursive"];
            $funky($element, $callback);
        }
        else {
            $callback($element);
        }
    }');
?>
Josh J 27-Sep-2006 09:45
In regards to the recursion issue by info at adaniels dot nl

Anon function recursion by referencing the function variable in the correct scope.
<?php
$fn2 = create_function('$a', 'echo $a; if ($a < 10) call_user_func($GLOBALS["fn2"], ++$a);');
$fn2(1);
?>
info at adaniels dot nl 11-May-2006 12:42
Note that using __FUNCTION__ in a an anonymous function, will always result '__lambda_func'.

<?php
    $fn = create_function('', 'echo __FUNCTION__;');
    $fn();
    // Result: __lambda_func
    echo $fn;
    // Result: ºlambda_2 (the actual first character cannot be displayed)
?>

This means that a anonymous function can't be used recursively. The following code (recursively counting to 10) results in an error:
<?php
    $fn2 = create_function('$a', 'echo $a; if ($a < 10) call_user_func(__FUNCTION__, $a++);');
    $fn2(1);
    // Warning: call_user_func(__lambda_func) [function.call-user-func]: First argument is expected to be a valid callback in T:/test/test.php(21) : runtime-created function on line 1
?>
josh at janrain dot com 03-Mar-2006 07:21
Beware! This is merely a convenience function that generates a unique name for a regular function. It is *not* a closure or even an anonymous function. It is just a regular function that gets named for you.
Joshua E Cook 20-Jan-2006 01:43
Functions created by create_function() cannot return a value by reference.  The function below creates a function that can.  The arguments are the same as create_function().  Note that these arguments are passed, unmodified, to eval(), so be sure that data passed in is sanitized.

<?php
/**
 * create_ref_function
 * Create an anonymous (lambda-style) function
 * which returns a reference
 * see http://php.net/create_function
 */
function
create_ref_function( $args, $code )
{
    static $n = 0;

    $functionName = sprintf('ref_lambda_%d',++$n);

    $declaration = sprintf('function &%s(%s) {%s}',$functionName,$args,$body);

    eval($declaration);

    return $functionName;
}
?>
boards at gmail dot com 31-Dec-2005 08:18
If you were checking to see if a function is made properly, this would be a better way of checking:

<?php
$fnc = @create_function('$arg1,$arg2,$arg3', 'return true;');
# make that function whatever you want
if (empty($fnc)) {
  die('Could not create function $fnc.');
}

# although, the follow will NOT work
if (empty(create_function('$arg', 'return $arg;'))) {
  die('Could not create anonymous function.');
}
# you would get an error regarding not being able to use a
# return value in writeable context (i.e. a return value is
# a const in C, and the function empty() doesn't use a
# const void* parameter
?>
david [at] davelee.com.au 13-Apr-2005 11:15
<?php
# dynamically create html helper functions which take the args
# $string_contents, $optional_hash_of_options
# and return the contents wrapped in a tag

$html_funcs = Array(
    'table',
    'tr',
    'th',
    'td',
    'div',
    'span',
    'pre',
    'strong',
    'em'
);
$args = '$html, $options=Array()';
$code = '
  $o = "";
  foreach ($options as $a => $b) {
    $o .= " $a=\"$b\"";
  }
  return "<$tag$o>$html</$tag>";
';
foreach ($html_funcs as $key => $tag) {
    ${$tag} = create_function($args, "\$tag = '$tag'; $code");
}

# usage example:

print $table(
  $tr($th('heading').$td('this is the cell content')),
  Array('style'=>'border: 1px solid silver;')
);
?>
MagicalTux at FF.ST 09-Mar-2004 07:25
neo at gothic-chat d0t de wrote :
Beware of memory-leaks, the garbage-collection seems to 'oversee' dynamically created functions!

Not really...

In fact, PHP can not "unassign" functions. So if you create a function, it won't be deleted until the end of the script, even if you unset the variable containing its name.

If you need to change a part of a function everytime you run a loop, think of a way to make a more general function or try using eval :) (functions are made to be re-used. If you need to run your own piece of code once, eval is much better).
neo at gothic-chat d0t de 21-Jan-2004 05:54
Beware of memory-leaks, the garbage-collection seems to 'oversee' dynamically created functions!

I used a function like this to replace special characters in links with their htmlentities:
<?php
$text = preg_replace_callback (
    "/(<(frame src|a href|form action)=\")([^\"]+)(\"[^>]*>)/i",
    create_function (
        '$matches',
        'return $matches[1] . htmlentities ($matches[3]) . $matches[4];'
    ),
    $text);
?>

After 1000 calls, the process used about 5MB more than before. In my situation this boosted up the memory-size of one PHP-process up to over 100MB!

In such cases, better store the function in a global variable.
nospam at fiderallalla dot de 08-Aug-2003 03:39
Sometimes it may be useful to create functions in a dynamic environment
(f. e. in a daemon-like php script).

Normally declaring a function must be done once, which results in the problem,
that in this special case modifying a function wouldn't have an effect until the script is reloaded.

Maybe this code snipplet is useful 4 u.

File: "functions.inc"

<?php
function test($str) {
    echo $str;
}
?>

Dynamic FunctionHandler:

<?php
$FileName = "functions.inc";
$FileHandle = fopen($FileName,"r");
$FileContent = fread($FileHandle,filesize($FileName));
fclose($FileHandle);

preg_match_all("#function\ ?([a-zA-Z0-9-_]*)\ ?\((.*?)\)\ ?\{(.*?)\}#mixse",$FileContent,$Matches);
if ( is_array($Matches) && isset($Matches[0]) && count($Matches[0]) > 0  ) {
     foreach ( $Matches[0] as $key=>$val ) {
        $$Matches[1][$key] = create_function($Matches[2][$key],$Matches[3][$key]);
    }
}
?>

The Test:
<?php echo $test("test"); ?>
.. will echo "test";

Hans Kuhlen
DB on music_ml at yahoo dot com dot ar 02-Apr-2003 01:58
[EDIT by danbrown AT php DOT net: Combined user-corrected post with previous (incorrect) post.]

You can't refer to a class variable from an anonymous function inside a class method using $this.  Anonymous functions don't inherit the method scope.  You'll have to do this:

<?php
class AnyClass {

 var $classVar = 'some regular expression pattern';

 function classMethod() {

   $_anonymFunc = create_function( '$arg1, $arg2', 'if ( eregi($arg2, $arg1) ) { return true; } else { return false; } ' );

   $willWork = $_anonymFunc('some string', $classVar);

 }

}
?>
ben-php at bacarisse dot btinternet dot co dot uk 26-Jul-2001 10:33
A nice technique for building complex search patterns on lists, files or whatever is to build function combining functions like this:

<?php
function _not_($f) {
  return create_function('$x',
    "return !$f(\$x);");
}

function _and_($f, $g) {
  return create_function('$x',
    "return $f(\$x) && $g(\$x);");
}
?>

(similarly for _or_ and others...).  Once you've built your matching primitives you can then build more complex matches into your script.

Unfortunately, as explained in (closed) bug #10721, the function names returned by create_function have a null byte at the front and this causes a parse error.

You can fix the definition like this:

<?php
function _not_($f) {
  $f = substr($f, 1);
  return create_function('$x',
    "return !call_user_func(chr(0).'$f', \$x)");
}
?>

The expression that re-builds the function name avoid the null being literally in the parsed string.  If there is a better fix, please let me know.
x-empt[a_t]ispep.cx 05-Jul-2001 08:41
Create_function enables the ability to change the scope of functions.  You might have a class where it needs to define a GLOBAL function.  This is possible, like:

<?php
        class blah {
                function blah() {
                        $z=create_function('$arg1string','return "function-z-".$arg1string;');
                        $GLOBALS['z']=$z;
                }
        }
        $blah_object=new blah;

        $result=$GLOBALS['z']('Argument 1 String');
        echo $result;
?>

Making a function escape it's defined scope can be useful in many situations.
maxim at maxim dot cx 28-Apr-2001 12:26
for those who want to assign it's own name to a function consider this code:

<?php
$fname = 'hello';

$func = sprintf('
    function %s($v="") {
        Return "$v<BR>";
    }
',
$fname
);

eval($func);
echo $fname('Please print it.... please....');
?>

what it does is,
: Creats a function as a string;
: Replaces the function name with $fname value;
: Converts the string into a REAL php code with eval()
: Calls the function using the variable function as declared before ($fname);

Simple, isn't it?

Can work well as an abstraction layer for portability and/or compatibility purposes

Maxim Maletsky
maxim@maxim.cx // PHPBeginner.com
mrben at free dot fr 21-Feb-2001 05:33
Here is another tricky but usefull techynote, good for adding "plugin" to a existing class :

<?php

class Hoge {
    var $lamda;
    var $text;

    function set($lamda)
    {
        $this->lamda = $lamda;
    }

    function callLamda()
    {
        $func = $this->lamda;
        return $func($this);
    }

    function get()
    {
        return $this->text;
    }
}

$newfunc = create_function('&$class', 'echo $class->get();' );

$h = new Hoge;
$h->text = "Hi there !";
$h->set($newfunc);
$h->callLamda();

?>
koyama at hoge dot org 14-Dec-2000 01:22
How do you use function which is created by create_function() as class method?

<?php
class Hoge {
  var $lamda;
  function set($lamda) {
    $this->lamda = $lamda;
  }
  function callLamda() {
    $func = $this->lamda;
    return $func();
  }
}

$newfunc = create_function('', 'echo "hoge<br>\n";');

$h = new Hoge;
$h->set( $newfunc );
$h->callLamda();
?>

It works fine. :-)


Call a static method and pass the arguments as array

  • ≪ create_function
  • forward_static_call ≫
  • PHP Manual
  • Function handling 함수 목록
  • Call a static method and pass the arguments as array

forward_static_call_array

(PHP 5 >= 5.3.0, PHP 7)

forward_static_call_array — Call a static method and pass the arguments as array

설명

mixed forward_static_call_array ( callable $function , array $parameters )

Calls a user defined function or method given by the function parameter. This function must be called within a method context, it can't be used outside a class. It uses the late static binding. All arguments of the forwarded method are passed as values, and as an array, similarly to call_user_func_array().

인수

function

The function or method to be called. This parameter may be an array, with the name of the class, and the method, or a string, with a function name.

parameter

One parameter, gathering all the method parameter in one array.

Note:

Note that the parameters for forward_static_call_array() are not passed by reference.

반환값

Returns the function result, or FALSE on error.

예제

Example #1 forward_static_call_array() example

<?php

class A
{
    const NAME = 'A';
    public static function test() {
        $args = func_get_args();
        echo static::NAME, " ".join(',', $args)." \n";
    }
}

class B extends A
{
    const NAME = 'B';

    public static function test() {
        echo self::NAME, "\n";
        forward_static_call_array(array('A', 'test'), array('more', 'args'));
        forward_static_call_array( 'test', array('other', 'args'));
    }
}

B::test('foo');

function test() {
        $args = func_get_args();
        echo "C ".join(',', $args)." \n";
    }

?>

위 예제의 출력:

B
B more,args
C other,args

참고

  • forward_static_call() - Call a static method
  • call_user_func() - Call the callback given by the first parameter
  • call_user_func_array() - Call a callback with an array of parameters
  • is_callable() - 변수 내용을 함수처럼 호출할 수 있는지 확인
  • callback형에 대한 정보

User Contributed Notes

nino dot skopac at gmail dot com 30-Jul-2016 11:49
Regarding namespaces:

Be sure to include fully namespaced class path:

<?php
forward_static_call_array(
    array('NAMESPACE\CLASS_NAME', 'STATIC_METHOD'),
    $params
);


Call a static method

  • ≪ forward_static_call_array
  • func_get_arg ≫
  • PHP Manual
  • Function handling 함수 목록
  • Call a static method

forward_static_call

(PHP 5 >= 5.3.0, PHP 7)

forward_static_call — Call a static method

설명

mixed forward_static_call ( callable $function [, mixed $parameter [, mixed $... ]] )

Calls a user defined function or method given by the function parameter, with the following arguments. This function must be called within a method context, it can't be used outside a class. It uses the late static binding.

인수

function

The function or method to be called. This parameter may be an array, with the name of the class, and the method, or a string, with a function name.

parameter

Zero or more parameters to be passed to the function.

반환값

Returns the function result, or FALSE on error.

예제

Example #1 forward_static_call() example

<?php

class A
{
    const NAME = 'A';
    public static function test() {
        $args = func_get_args();
        echo static::NAME, " ".join(',', $args)." \n";
    }
}

class B extends A
{
    const NAME = 'B';

    public static function test() {
        echo self::NAME, "\n";
        forward_static_call(array('A', 'test'), 'more', 'args');
        forward_static_call( 'test', 'other', 'args');
    }
}

B::test('foo');

function test() {
        $args = func_get_args();
        echo "C ".join(',', $args)." \n";
    }

?>

위 예제의 출력:

B
B more,args
C other,args

참고

  • forward_static_call_array() - Call a static method and pass the arguments as array
  • call_user_func_array() - Call a callback with an array of parameters
  • call_user_func() - Call the callback given by the first parameter
  • is_callable() - 변수 내용을 함수처럼 호출할 수 있는지 확인
  • callback형에 대한 정보

User Contributed Notes

jhibbard at gmail dot com 20-Jun-2012 04:55
Example usage via calls outside of the class and within an object:

<?php
/**
 * @author Jonathon Hibbard
 */
class foo {
  # used to verify we're actually setting something..
  private static $value = '';

  /**
   * Simple setter for the static method setValue...
   */
  public static function set($method_identifier, $value_to_pass = '') {
    # make sure we have the right method format...
    # another semi-useful example is like this (useful for REST-like requests...): str_replace(" ", "", ucwords(str_replace("_", " ",  $method_identifier)));
    $static_method = 'set' . ucfirst(strtolower(trim($method_identifier)));

    if(method_exists(__CLASS__, $static_method)) {
      //Note:  this will not work and will throw PHP Parse error:  syntax error, unexpected '::'
      //__CLASS__::$static_method($value_to_pass);

      foo::$static_method($value_to_pass);

      echo "\tCalling forward_static_call with pure string and value param:\n";
      forward_static_call(__CLASS__ . "::" . $static_method, $value_to_pass);

      echo "\tCalling forward_static_call with class, method array and value param:\n";
      forward_static_call(array(__CLASS__, $static_method), $value_to_pass);
    }
  }

  /**
   * Set self::$value to something?
   */
  public static function setValue($value_recieved = '') {
    echo "\t\tsetValue called with param of " . var_export($value_recieved, true) . "!\n";

    echo "\t\tSetting Private 'value'...\n";

    self::$value = $value_recieved;

    echo "\t\tChecking the Private 'value':\n";
    if(!empty(self::$value)) {
      echo "\t\t\tPrivate 'value' was set to '" . self::$value . "' as expected!\n";
    } else {
      echo "\t\t\tPrivate 'value' was not set!\n";
    }

    # Reset...
    self::$value = '';
  }

  /**
   * Create an object and test calling the static method from within this realm...
   */
  public function __construct() {
    echo "\tCalling from within constructor..\n";
    foo::set('value','Something else from within the instance!');
  }
}

echo "\n============ Calling by static method first ============\n";
foo::set('value','Something from outside of the foo class!');

echo "\n============ Calling by static method without a value next ============\n";
foo::set('value');

echo "\n============ Calling by createing an instance next ============\n";
new foo();
?>
arthur dot techarts at gmail dot com 12-Jul-2011 04:39
Example to understand this function and difference with call_user_func:

<?php
class Beer {
    const NAME = 'Beer!';
    public static function printed(){
        echo 'static Beer:NAME = '. static::NAME . PHP_EOL;
    }
}

class Ale extends Beer {
    const NAME = 'Ale!';
    public static function printed(){
        forward_static_call(array('parent','printed'));
        call_user_func(array('parent','printed'));

        forward_static_call(array('Beer','printed'));
        call_user_func(array('Beer','printed'));
    }
}

Ale::printed();
echo '</pre>';
?>


Return an item from the argument list

  • ≪ forward_static_call
  • func_get_args ≫
  • PHP Manual
  • Function handling 함수 목록
  • Return an item from the argument list

func_get_arg

(PHP 4, PHP 5, PHP 7)

func_get_arg — Return an item from the argument list

설명

mixed func_get_arg ( int $arg_num )

Gets the specified argument from a user-defined function's argument list.

This function may be used in conjunction with func_get_args() and func_num_args() to allow user-defined functions to accept variable-length argument lists.

인수

arg_num

The argument offset. Function arguments are counted starting from zero.

반환값

Returns the specified argument, or FALSE on error.

변경점

버전 | 설명
5.3.0 | This function can now be used in parameter lists.
5.3.0 | If this function is called from the outermost scope of a file which has been included by calling include or require from within a function in the calling file, it now generates a warning and returns FALSE.

오류/예외

Generates a warning if called from outside of a user-defined function, or if arg_num is greater than the number of arguments actually passed.

예제

Example #1 func_get_arg() example

<?php
function foo()
{
     $numargs = func_num_args();
     echo "Number of arguments: $numargs\n";
     if ($numargs >= 2) {
         echo "Second argument is: " . func_get_arg(1) . "\n";
     }
}

foo(1, 2, 3);
?>

위 예제의 출력:

Number of arguments: 3
Second argument is: 2

Example #2 func_get_arg() example before and after PHP 5.3

test.php
<?php
function foo() {
    include './fga.inc';
}

foo('First arg', 'Second arg');
?>

fga.inc
<?php

$arg = func_get_arg(1);
var_export($arg);

?>

Output previous to PHP 5.3:

'Second arg'

Output in PHP 5.3 and later:

Warning: func_get_arg():  Called from the global scope - no function
context in /home/torben/Desktop/code/ml/fga.inc on line 3
false

Example #3 func_get_arg() example of byref and byval arguments

<?php
function byVal($arg) {
    echo 'As passed     : ', var_export(func_get_arg(0)), PHP_EOL;
    $arg = 'baz';
    echo 'After change  : ', var_export(func_get_arg(0)), PHP_EOL;
}

function byRef(&$arg) {
    echo 'As passed     : ', var_export(func_get_arg(0)), PHP_EOL;
    $arg = 'baz';
    echo 'After change  : ', var_export(func_get_arg(0)), PHP_EOL;
}

$arg = 'bar';
byVal($arg);
byRef($arg);
?>

Output of the above example in PHP 7:

As passed : 'bar'
After change : 'baz'
As passed : 'bar'
After change : 'baz'

Output of the above example in PHP 5:

As passed : 'bar'
After change : 'bar'
As passed : 'bar'
After change : 'baz'

주의

Note:

이 함수는 인수를 현재 영역(scope)에 의존하기 때문에, 함수의 인수로 사용할 수 없습니다. 이 값을 함수에 넘겨야 한다면, 결과값을 변수에 할당하여 그 변수로 넘겨야 합니다.

Note:

인수가 참조로 전달되면, 인수에 대한 변화는 이 함수가 반환하는 값에 영향을 줍니다.

Note: This function returns a copy of the passed arguments only, and does not account for default (non-passed) arguments.

참고

  • ... syntax in PHP 5.6+
  • func_get_args()
  • func_num_args()

User Contributed Notes

beta3designs [at] gmail [dot] com 06-Jul-2009 03:34
This functions seems so powerful... just when i saw it i thought about writing a fast average of n numbers function so here it is, it is very simple... example usage included.

<?php

//Calculate the average of the numbers given

function avg(){
    $sum = 0;
    for($i = 0; $i < func_num_args(); $i++){
        $sum += func_get_arg($i);
    }
    $avg = $sum / func_num_args();
    return $avg;
}

echo sprintf("%.2f",avg(2,1,2,1,3,4,5,1,3,6));
?>
mw atto lanfear dotto com 07-Dec-2004 11:56
func_get_arg() does not appear to be allowed to be used as a function argument itself within class constructors in PHP 5.0.2 (wonk-ay!!!):

<?php

class ABC
{
    function __construct()
    {
        foreach (func_get_args() as $name => $value)
        {
    echo <<<EOT
    <br/>
    $name : $value <br/>
    <br/>

EOT;
        }
    }
}

class DEF extends ABC
{
    function __construct()
    {
        parent::__construct(func_get_arg(0),
                                    func_get_arg(1),
                                    func_get_arg(2));
    }
}

$def = new DEF(123123, "asdfasdf", "blahblahblah");

?>

The above script generates:

Fatal error: func_get_arg(): Can't be used as a function parameter in c:\Inetpub\wwwroot\phpwasrc\chapter10\xxx.php on line 23

There are, however, no problems when passing these as parameters to regular functions.
anders at ingemann dot fakestuff dot de 04-Jun-2004 03:16
I actually think that there is need for such "do absolutely everything" functions. I use them mostly as tools for rapid prototyping.
And there is a method with which you may be able to pass several strings to a function: ereg();
Another use for such functions is to create little code snippets for other people out there. They won't have to edit the function any longer if they do not use a parameter. They just don't name it when calling the function.
This results in allrounder functions that are very robust in their use.Normally you just have a little code snippet (e.g. ip-blocking snippets). Through this type of programming you have whole functions.
mightye (at) mightye (dot) org 12-Mar-2004 06:45
func_get_arg() returns a *copy* of the argument, to my knowledge there is no way to retrieve references to a variable number of arguments.

I have a module system in my game at http://lotgd.net where I'd like to be able to pass a variable number of arguments to functions in a module, and pass them by reference if the module asks for it by reference, but you can't accept optional parameters as references, nor can you retrieve the reference on a variable number of arguments.  Looks like my modules will have to do with out the ability to accept parameters to their functions by reference.


Returns an array comprising a function's argument list

  • ≪ func_get_arg
  • func_num_args ≫
  • PHP Manual
  • Function handling 함수 목록
  • Returns an array comprising a function's argument list

func_get_args

(PHP 4, PHP 5, PHP 7)

func_get_args — Returns an array comprising a function's argument list

설명

array func_get_args ( void )

Gets an array of the function's argument list.

This function may be used in conjunction with func_get_arg() and func_num_args() to allow user-defined functions to accept variable-length argument lists.

반환값

Returns an array in which each element is a copy of the corresponding member of the current user-defined function's argument list.

변경점

버전 | 설명
5.3.0 | This function can now be used in parameter lists.
5.3.0 | If this function is called from the outermost scope of a file which has been included by calling include or require from within a function in the calling file, it now generates a warning and returns FALSE.

오류/예외

Generates a warning if called from outside of a user-defined function.

예제

Example #1 func_get_args() example

<?php
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs \n";
    if ($numargs >= 2) {
        echo "Second argument is: " . func_get_arg(1) . "\n";
    }
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "Argument $i is: " . $arg_list[$i] . "\n";
    }
}

foo(1, 2, 3);
?>

위 예제의 출력:

Number of arguments: 3
Second argument is: 2
Argument 0 is: 1
Argument 1 is: 2
Argument 2 is: 3

Example #2 func_get_args() example before and after PHP 5.3

test.php
<?php
function foo() {
    include './fga.inc';
}

foo('First arg', 'Second arg');
?>

fga.inc
<?php

$args = func_get_args();
var_export($args);

?>

Output previous to PHP 5.3:

array (
  0 => 'First arg',
  1 => 'Second arg',
)

Output in PHP 5.3 and later:

Warning: func_get_args():  Called from the global scope - no function
context in /home/torben/Desktop/code/ml/fga.inc on line 3
false

Example #3 func_get_args() example of byref and byval arguments

<?php
function byVal($arg) {
    echo 'As passed     : ', var_export(func_get_args()), PHP_EOL;
    $arg = 'baz';
    echo 'After change  : ', var_export(func_get_args()), PHP_EOL;
}

function byRef(&$arg) {
    echo 'As passed     : ', var_export(func_get_args()), PHP_EOL;
    $arg = 'baz';
    echo 'After change  : ', var_export(func_get_args()), PHP_EOL;
}

$arg = 'bar';
byVal($arg);
byRef($arg);
?>

Output of the above example in PHP 7:

As passed : array (
0 => 'bar',
)
After change : array (
0 => 'baz',
)
As passed : array (
0 => 'bar',
)
After change : array (
0 => 'baz',
)

Output of the above example in PHP 5:

As passed : array (
0 => 'bar',
)
After change : array (
0 => 'bar',
)
As passed : array (
0 => 'bar',
)
After change : array (
0 => 'baz',
)

주의

Note:

이 함수는 인수를 현재 영역(scope)에 의존하기 때문에, 함수의 인수로 사용할 수 없습니다. 이 값을 함수에 넘겨야 한다면, 결과값을 변수에 할당하여 그 변수로 넘겨야 합니다.

Note:

인수가 참조로 전달되면, 인수에 대한 변화는 이 함수가 반환하는 값에 영향을 줍니다.

Note: This function returns a copy of the passed arguments only, and does not account for default (non-passed) arguments.

참고

  • ... syntax in PHP 5.6+
  • func_get_arg()
  • func_num_args()
  • ReflectionFunctionAbstract::getParameters()

User Contributed Notes

foxkeys at gmail dot com 18-Mar-2016 03:18
Merge func_get_args() with function defaults
<?php
class utils {
  /**
   * @param mixed[] $args
   * @param ReflectionMethod $reflectionMethod
   *
   * @return array
   */
  public static function mergeArgsWithDefaults( $args, \ReflectionMethod $reflectionMethod ) {
    foreach ( array_slice( $reflectionMethod->getParameters(), count( $args ) ) as $param ) {
      /**
       * @var ReflectionParameter $param
       */
      $args[] = $param->getDefaultValue();
    }
    return $args;
  }
}

class  sampleParent {
  const USER_FILE_TYPE_FILE = 'FILE';
  public function select( $idUserFile = null, $idUserFileType = self::USER_FILE_TYPE_FILE ) {
    echo '[$idUserFile=>' . $idUserFile . ', $idUserFileType=>' . $idUserFileType, ']<br/>' . PHP_EOL;
  }
}

class sample extends sampleParent {
  const USER_FILE_TYPE_IMG = 'IMG';
  public function select( $idUserFile = null, $idUserFileType = self::USER_FILE_TYPE_IMG ) {
    return call_user_func_array( 'parent::select', \utils::mergeArgsWithDefaults( func_get_args(), new ReflectionMethod( __CLASS__, __FUNCTION__ ) ) );
  }
}

$sample1 = new sampleParent();
$sample1->select();//Prints "" / self::USER_FILE_TYPE_FILE
$sample1->select(1);//Prints 1 / self::USER_FILE_TYPE_FILE
$sample1->select(2, 'test 1');//Prints 2 / "test 1"
echo '<br/>' . PHP_EOL;
$sample2 = new sample();
$sample2->select();//Prints "" / self::USER_FILE_TYPE_IMG
$sample2->select(3);//Prints 3 / self::USER_FILE_TYPE_IMG
$sample2->select(4, 'test 2');//Prints 4 / "test 2"
?>
dev at mp3addict dot pw 09-Mar-2014 04:05
This function is as printf() but only calls printf() when the debug_enabled global variable is set to TRUE. Because by this way, you can use debug() instead of echo | printf() and set debug_enabled to false after checking and submitting work into production.

cheers;

function debug () {
    if ($GLOBALS['debug_enabled'] == true) {
        $args        = func_get_args();
        $fmt         = $args[0];
        $printf_args = '';
        for ($i = 1; $i < count($args); $i++) {
            $printf_args .= ",\"{$args[$i]}\"";
        }
        eval("printf(\"{$fmt}\"{$printf_args});");
    }
}
maarten at ba dot be 13-Sep-2012 08:58
it seems that this function only returns a copy and loses it's byref information, use this dirty non-efficient workaround instead:

at the moment of writing it currently returns all of them as references, instead of only the ones who are passed that way...

<?php
function func_get_args_byref() {
        $trace = debug_backtrace();
        return $trace[1]['args'];
}
?>
OpenTechnologist 31-Dec-2011 04:54
please note that optional parameters are not seen/passed by func_get_args(), as well as func_get_arg().

ex:

<?php
function testfunc($optional = 'this argument is optional..') {
    $args = func_get_args();
    var_dump($args);
    echo $optional;
}
?>

test case #1:
testfunc('argument no longer optional..');

result for #1:
array(1) {
    [0]=>  string(20) "argument no longer optional.."
}
argument no longer optional..

test case #2:
testfunc('argument no longer optional..','this is an extra argument');

result for #2:
array(2) {
    [0]=>  string(29) "argument no longer optional.."
    [1]=>  string(25) "this is an extra argument"
}
argument no longer optional..

test case #3: -- RESULTS IN AN EMPTY ARRAY
testfunc();

result for #3:
array(0) {
}
this argument is optional..
red 13-Oct-2011 10:16
I had to pass variable length arguments from one function to another. It seems the only way of doing this is to use call_user_func_array.

<?php

function query(/*query [, $arg1...$argN]*/){
    $query  = call_user_func_array('replaceAndClean', func_get_args());
    $result = mysql_query($query);
    return $result;
}

function replaceAndClean(/*query [, $arg1...$argN]*/){
   $args = func_get_args();
   if(count($args) == 1){
       return $args[0];
   }
   $query = array_shift($args);
   return vsprintf($query, array_map('mysql_real_escape_string', $args));
}

?>

Example:

<?php

// unsave call
query("SELECT FROM foo where bar='".$_POST['bar']."'");

// save call
query("SELECT FROM foo where bar='%d'", $_POST['bar']);

?>
pBakhuis at Gmail dot com 05-Oct-2011 02:30
Please note that you can't use this for recursive functions as you can't pass the parameters to the function again as then they'll be in the form of an array.
mulllhausen 06-Apr-2011 04:49
i use this structure a lot for debugging. i always place the call to 'debugfunc' at the start of any function which i want to debug. the square brackets in the echod output are useful to see if there is accidental whitespace within string variables passed to anyfunc. if anyone can suggest a better way of passing the names of the arguments to debugfunc i would appreciate it. as it is works fine, but its not very universal...

<?php
anyfunc('val1','val2','val3');

function anyfunc($arg1, $arg2, $arg3)
{
    debugfunc(__FUNCTION__, '$arg1, $arg2, $arg3', func_get_args());
    /*do useful non-debugging stuff*/
}
function debugfunc($name, $arg_names, $arg_vals)
{
    echo "begin function [$name]. ";
    $arg_names_array = explode(',', $arg_names);
    foreach($arg_names_array as $k => $v)
    {
        $v = trim($v);
        echo "$v: [$arg_vals[$k]] ";
    }
    echo "\n";
}

//output:
//begin function [anyfunc]. $arg1: [val1] $arg2: [val2] $arg3: [val3]
?>
mitko at edabg dot com 06-Apr-2009 12:48
<?php
/*
This example demonstrate how to use unknown variable arguments by reference.
func_get_args() don't return arguments by reference, but
debug_backtrace() "args" is by reference.
In PHP 5 this have no particular sense, because calling with arguments by reference
is depreciated and produce warning.
*/

class foo {

    var $bar = "default bar";

    function foo(/*variable arguments*/) {
// func_get_args returns copy of arguments
//        $args = func_get_args();
// debug_backtrace returns arguments by reference
        $stack = debug_backtrace();
        $args = array();
        if (isset($stack[0]["args"]))
            for($i=0; $i < count($stack[0]["args"]); $i++)
                $args[$i] = & $stack[0]["args"][$i];
        call_user_func_array(array(&$this, 'bar'), $args);
    }

    function bar($bar = NULL) {
        if (isset($bar))
            $this->bar = & $bar;
    }
}

$global_bar = "bar global";
$foo = & new foo();
echo "foo->bar:    ".$foo->bar."</br>\n";
$foo->bar = "new bar";
echo "global_bar:  ".$global_bar."</br>\n";
/*
Result:
foo->bar:    default bar</br>
global_bar:  bar global</br>
*/

$foo = & new foo(&$global_bar);
echo "foo->bar:    ".$foo->bar."</br>\n";
$foo->bar = "new bar";
echo "global_bar:  ".$global_bar."</br>\n";
/*
Result:
foo->bar:    bar global</br>
global_bar:  new bar</br>
*/

?>
Oto Brglez 12-Mar-2009 03:18
How to create simple sum function that can sum N arguments. Like this:

<?php

function sum(){
    $s=0;
    foreach(func_get_args() as $a) $s+= is_numeric($a)?$a:0;
    return $s;
};

print sum(1,2,3,4,5,6); // will return 21
print sum(3,2,1); // will return 6
print sum(false,array(),5,5); // will return 10

?>
anderson at francotecnologia dot com 27-Aug-2008 08:25
How to create a polymorphic/"overloaded" function

<?php
function select()
{
    $t = '';
    $args = func_get_args();
    foreach ($args as &$a) {
        $t .= gettype($a) . '|';
        $a = mysql_real_escape_string($a);
    }
    if ($t != '') {
        $t = substr($t, 0, - 1);
    }
    $sql = '';
    switch ($t) {
        case 'integer':
            // search by ID
            $sql = "id = {$args[0]}";
            break;
        case 'string':
            // search by name
            $sql = "name LIKE '%{$args[0]}%'";
            break;
        case 'string|integer':
            // search by name AND status
            $sql = "name LIKE '%{$args[0]}%' AND status = {$args[1]}";
            break;
        case 'string|integer|integer':
            // search by name with limit
            $sql = "name LIKE '%{$args[0]}%' LIMIT {$args[1]},{$args[2]}";
            break;
        default:
            // :P
            $sql = '1 = 2';
    }
    return mysql_query('SELECT * FROM table WHERE ' . $sql);
}
$res = select(29); // by ID
$res = select('Anderson'); // by name
$res = select('Anderson', 1); // by name and status
$res = select('Anderson', 0, 5); // by name with limit
?>
kangaroo232002 at yahoo dot co dot uk 26-Aug-2008 11:25
Instead of having to define your arg list twice, and keeping to the good style of initialising your variables in the head of your class, you can use (PHP5):

<?php
class myclass {
     public $value = null;
    public $key = null;
    public $column = null;
    public $table = null;
    public function __construct() {
        $vars = get_class_vars();
        for($i=0; $i<func_num_args();$i++) {
            $this->${$vars[$i]}=func_get_arg($i);
        }
    }
}
?>

which should allow you to set variables while retaining their default values if they are not set (in this case, null), without having to mess around with functions to retain default values so is much neater (just don't change the order you declare your vars!)

<?php
//usage
$c = new myclass("value", "tablekey", "tablecol", "table");
echo $c->key;
//prints 'tablekey'
?>
Anonymous 29-Jan-2008 05:11
I use the following concept for quick "plugin" of multiple argument support.

<?php

function increment($n) {
  $p = func_get_args();
  if (count($p) > 1) {
    return array_map(__FUNCTION__, $p);
  }
  $n =& $p[0];

  return ++$n;
}

list($two, $three, $four) = increment(1,2,3);

?>
tristan dot colombo at laposte dot net 18-Oct-2007 10:03
In order to use the function 'func_get_args()' to instanciate differents type of objects, you must use the Reflection API.
By example, we have two different classes and we want to have an unique function (using an unfixed number of parameters) to create the objects. We create two classes 'a' and 'b' where constructors accept different numbers of arguments.
Class a (class/a.class.php):
<?php
  include_once 'a.class.php';

  class b extends a
  {
    private $param3;

    public function __construct($a, $b, $c)
    {
      $this->param1 = $a;
      $this->param2 = $b;
      $this->param3 = $c;
    }

    public function display()
    {
      echo $this->param1 . ', ' . $this->param2 . ' and ' . $this->param3 . '!<br />';
    }
  }
?>

Class b (class/b.class.php):
<?php

  class a
  {
    private $param1;
    private $param2;

    public function __construct($a, $b)
    {
      $this->param1 = $a;
      $this->param2 = $b;
    }

    public function display()
    {
      echo $this->param1 . ' and ' . $this->param2 . '<br />';
    }
  }
?>

Main program :
<?php

  function classFactory()
  {
    // Retrieve arguments list
    $_args = func_get_args();
    // Delete the first argument which is the class name
    $_className = array_shift($_args);

    // Include the requested class
    include_once 'class/' . $_className . '.class.php';

    // Create Reflection object
    // See : http://www.php.net/manual/en/language.oop5.reflection.php
    $_reflection = new ReflectionClass($_className);

    // Use the Reflection API
    return $_reflection->newInstanceArgs($_args);
  }

  $a = classFactory('a', 'hello', 'world');
  $b = classFactory('b', 'that\'s', 'all', 'folks');

  $a->display();
  $b->display();

?>
Sinured 20-Aug-2007 02:38
It may seem obvious, but if you want your variadic function to at least require one parameter, you can do this instead of checking func_num_args() == 0, which I've seen often:

<?php
function variadic($dummy) {
    $args = func_get_args();
    foreach ($args as $arg) {
        echo "$arg<br />\n";
    }
}
?>

func_get_args() fetches ALL passed parameters, not only those that weren't copied to a local variable.
ario [a] mail [dot] utexas [dot] edu 07-May-2007 11:50
"Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter. If you must pass this value, assign the results to a variable, and pass the variable."

This means that the following code generates an error:

<?php

function foo($list)
{
  echo implode(', ', $list);
}

function foo2()
{
  foo(func_get_args());
}

foo2(1, 2, 3);

?>

However, you can easily get around this by doing the following:

<?php

function foo($list)
{
  echo implode(', ', $list);
}

function foo2()
{
  foo($args = func_get_args());
}

foo2(1, 2, 3);

?>

This captures the context from foo2(), making this legal.  You get the expected output:

"1, 2, 3"
rafagd at gmail dot com 13-Feb-2007 05:53
Sometimes, you may need to dynamic set and get of args...

This function merge array args, so you can dynamic set some args by sending an array arg.

<?php
  function dynamicArgs(/*$arg1, $arg2...$argN*/) {
    $args = func_get_args(); $num  = func_num_args();
    for ($i = 1; $i < $num; $i++) {
      $args[0] = array_merge((array) $args[0], (array) $args[$i]);
    }
    return $args[0];
  }

  var_dump(dynamicArgs('a',array('b','c'),'d',1);
?>

This should output like:

array(5) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  int(1)
}
bew 31-Mar-2006 04:55
A more concise way of expressing my idea from the previous post (I'd forgotten about array_slice()):

<?php
function func_get_default_args($a) {
    $args = array_slice(func_get_args(), 1);
    return array_merge($args, array_slice($a, sizeof($args)));
}

function foo($a = 1, $b = 2, $c = 3) {
    print_r(func_get_default_args(func_get_args(), $a, $b, $c));
}

// prints: Array ( [0] => a [1] => b [2] => 3 )
foo('a', 'b');
?>
Nathan Ostgard 06-Dec-2005 07:14
If you're using PHP5, the variable number of argument functions all return the objects by reference - and not a copy of the object, as this leads you to believe.
T.M. 04-Nov-2004 04:24
Simple function to calculate average value using dynamic arguments:
<?php
function average(){
    return array_sum(func_get_args())/func_num_args();
}
print average(10, 15, 20, 25); // 17.5
?>
volte6 at drunkduck dot com 30-Sep-2004 11:54
For those who have a use for a C style enum() function:

<?php
//*******************************************
// void enum();
// enumerates constants for unique values guarenteed.
function enum()
{
  $i=0;
  $ARG_ARR = func_get_args();
  if (is_array($ARG_ARR))
  {
    foreach ($ARG_ARR as $CONSTANT)
    {
      define ($CONSTANT, ++$i);
    }
  }
}

// USAGE:
enum(ERR_USER_EXISTS, ERR_OLD_POST);

// etc. etc.
//*******************************************
?>

this can be used for error codes etc.
I deliberately skipped the 0 (zero) define, which could be useful for error checking.
daveNO at ovumSPAMdesign dot com 18-Sep-2001 08:29
<?php
// How to simulate named parameters in PHP.
// By Dave Benjamin <dave@ovumdesign.com>

// Turns the array returned by func_get_args() into an array of name/value
// pairs that can be processed by extract().
function varargs($args) {
    $count = count($args);
    for ($i = 0; $i < $count; $i += 2) {
        $result[$args[$i]] = $args[$i + 1];
    }

    return $result;
}

// Example
function test(&$ref1, &$ref2) {
    // Default arguments go here.
    $foo = "oof";

    // Do some magic.
    extract(varargs(func_get_args()));

    echo nl2br("\n\$var1 = $var1");
    echo nl2br("\n\$var2 = $var2");
    echo nl2br("\n\$foo = $foo\n\n");

    // Modify some variables that were passed by reference.
    // Note that func_get_args() doesn't pass references, so they
    // need to be explicitly declared in the function definition.
    $ref1 = 42;
    $ref2 = 84;
}

$a = 5;
$b = 6;

echo nl2br("Before calling test(): \$a = $a\n");
echo nl2br("Before calling test(): \$b = $b\n");

// Try removing the 'foo, "bar"' from the following line.
test($a, $b, var1, "abc", var2, "def", foo, "bar");

echo nl2br("After calling test(): \$a = $a\n");
echo nl2br("After calling test(): \$b = $b\n");
?>
04-Jun-2001 04:44
You can pass a variable number of arguments to a function whilst keeping references intact by using an array. The disadvantage of course, is that the called function needs to be aware that it's arguments are in an array.

<?php
// Prints "hello mutated world"
function mutator($args=null) {
$n=count($args);
while($i<$n) $args[$i++] = "mutated";
}
$a = "hello";
$b = "strange";
$c = "world";
mutator(array($a, &$b, $c));
echo "$a $b $c";
?>


Returns the number of arguments passed to the function

  • ≪ func_get_args
  • function_exists ≫
  • PHP Manual
  • Function handling 함수 목록
  • Returns the number of arguments passed to the function

func_num_args

(PHP 4, PHP 5, PHP 7)

func_num_args — Returns the number of arguments passed to the function

설명

int func_num_args ( void )

Gets the number of arguments passed to the function.

This function may be used in conjunction with func_get_arg() and func_get_args() to allow user-defined functions to accept variable-length argument lists.

반환값

Returns the number of arguments passed into the current user-defined function.

변경점

버전 | 설명
5.3.0 | This function can now be used in parameter lists.
5.3.0 | If this function is called from the outermost scope of a file which has been included by calling include or require from within a function in the calling file, it now generates a warning and returns -1.

오류/예외

Generates a warning if called from outside of a user-defined function.

예제

Example #1 func_num_args() example

<?php
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs\n";
}

foo(1, 2, 3);
?>

위 예제의 출력:

Number of arguments: 3

Example #2 func_num_args() example before and after PHP 5.3

test.php
<?php
function foo() {
    include './fna.php';
}

foo('First arg', 'Second arg');
?>

fna.php
<?php

$num_args = func_num_args();
var_export($num_args);

?>

Output previous to PHP 5.3:

2

Output in PHP 5.3 and later will be something similar to:

Warning: func_num_args():  Called from the global scope - no function
context in /home/torben/Desktop/code/ml/fna.php on line 3
-1

주의

Note:

이 함수는 인수를 현재 영역(scope)에 의존하기 때문에, 함수의 인수로 사용할 수 없습니다. 이 값을 함수에 넘겨야 한다면, 결과값을 변수에 할당하여 그 변수로 넘겨야 합니다.

참고

  • ... syntax in PHP 5.6+
  • func_get_arg()
  • func_get_args()
  • ReflectionFunctionAbstract::getNumberOfParameters()

User Contributed Notes

luisguillermo dot quevedovelez at gmail dot com 13-Mar-2012 04:32
I had defined a function function_name(){ ...} as a drupal callback.
I try to get how many params where passed

I got a Error and my Site falls down

I've replaced func_get_args() instead func_num_args() and my Site was restored.

I conclude you can not use  func_num_args() in callbacks.

Hope it helps.
Dennis Robinson from basnetworks dot net 30-Jun-2009 06:29
This function comes in handy, and I believe is the only solution, when you have an optional parameter that can take any type of data.

For example:

<?php

// $data can be of any type, including null
function my_function($name, $data = null)
{
    if ($data !== null)
    {
        // Do something with $data
        // If you call my_function('something'), this WILL NOT be reached
        // If you call my_function('something', null), this WILL NOT be reached
    }
}

?>

The problem with the above function is that you will never be able to use null as the value for $data.  To fix this, use func_num_args() like so:

<?php

// $data can be of any type, including null
function my_function($name, $data = null)
{
    if (func_num_args() >= 2)
    {
        // Do something with $data
        // If you call my_function('something'), this WILL NOT be reached
        // If you call my_function('something', null), this WILL be reached
    }
}

?>

This solution works because func_num_args() reports exactly how many arguments were passed when the function was called.  It does not take into account when default argument values are used.
jared at ws-db dot com 06-Sep-2005 06:46
Just a note for anyone wondering. This function doesn't include params that have a default value, unless you pass one in to overwrite the default param value. Not sure if that makes sense, so here's an example:

<?php
function helloWorld($ArgA, $ArgB="HelloWorld!") {
  return func_num_args();
}

// The following will return 1
$Returns1 = helloWorld("HelloWorld!");

// The following will return 2
$Returns2 = helloWorld("HelloWorld!", "HowdyWorld!");
?>
andy at arakka dot co dot th 12-May-2005 04:16
func_num_args() can be used in conjunction with named arguments, also. For example:

<?php
function DebugShow( $label, $value ) {
  echo "# " . $label ;
  if ( func_num_args() > 1 ) echo " = " . $value ;
  echo "<br>";
}
?>
ashley at dcs dot warwick dot ac dot uk 17-Apr-2002 01:40
If you want to pass the parameters on intact to another function, use func_get_args and call_user_func_array (careful - this one is only available in recent PHP versions).  For example:

<?php
/* Print an HTML tag.  This accepts a variable number of arguments:
   the first should be the name of the tag, followed by pairs of
   arguments that describe keys and values.  The values are printed
   with surrounding double quote characters.  */
function printTag() {
  $numArgs = func_num_args();
  if ($numArgs < 1) die("printTag given no arguments");

  echo "<" . func_get_arg(0);
  for ($i = 1; $i < $numArgs; $i+=2) {
    echo " " . func_get_arg($i);
    if ($i+1 < $numArgs)
      echo "=\"" . func_get_arg($i+1) . "\"";
  }
  echo ">";

}

/* Print an HTML tag with a newline on the end */
function printTagNL() {
  $args = func_get_args();
  call_user_func_array("printTag", $args);
  echo "\n";
}

printTagNL("input", "type", "hidden", "name", "SORTORDER", "value", $columnNo);
?>
thalis at NOSPAMcs dot pitt dot edu 29-Mar-2002 09:05
The idea of func_get_args() is to construct functions of variable number of parameters like

<?php
function var_param_func(){
    if(func_num_args()==0){
        //do one thing
    }
    if(func_num_args()==1)
        //do another thing
        //get the args with func_get_args()
    }
}
?>


Return TRUE if the given function has been defined

  • ≪ func_num_args
  • get_defined_functions ≫
  • PHP Manual
  • Function handling 함수 목록
  • Return TRUE if the given function has been defined

function_exists

(PHP 4, PHP 5, PHP 7)

function_exists — Return TRUE if the given function has been defined

설명

bool function_exists ( string $function_name )

Checks the list of defined functions, both built-in (internal) and user-defined, for function_name.

인수

function_name

The function name, as a string.

반환값

Returns TRUE if function_name exists and is a function, FALSE otherwise.

Note:

This function will return FALSE for constructs, such as include_once and echo.

예제

Example #1 function_exists() example

<?php
if (function_exists('imap_open')) {
    echo "IMAP functions are available.<br />\n";
} else {
    echo "IMAP functions are not available.<br />\n";
}
?>

주의

Note:

A function name may exist even if the function itself is unusable due to configuration or compiling options (with the image functions being an example).

참고

  • method_exists() - 클래스 메쏘드가 존재하는지 확인
  • is_callable() - 변수 내용을 함수처럼 호출할 수 있는지 확인
  • get_defined_functions() - Returns an array of all defined functions
  • class_exists() - 클래스가 정의되었는지 확인
  • extension_loaded() - Find out whether an extension is loaded

User Contributed Notes

ayyappan dot ashok at gmail dot com 31-Mar-2016 07:03
PHP supports nested function based on certain criteria.

Please look over the code.

function Media(){ }

function Audio()
{
    echo "Plugged Audo 5.1:<br/>";
    function Volume()
    {
    echo "Volume controls:<br/>";
    function Equalizer()
    {
        echo "Equalize Bands:<br/>";
    }
   }
}
//Call to nested functions
Audio();
Volume();
Equalizer();

if(function_exists('Volume')):
echo "TRUE";
else:
echo "FALSE";
endif;

Case 1:  //Result :Works Well
--------
Audio();
Volume();
Equalizer();

Case 2:  //Results Notice Error. Root function Audio must be called first.
--------
Volume();

Case 3:  //Results Error. Root function Volume must be called.
--------
Audio();
Equalizer();

Note :
The nested function should be called based on their order used.
In our example when Audio is not called and instantly when we try to call Volume puts under error.

Even though there is an possibility to use nested functions in PHP. It looks overhead to do so. Better to avoid in logical ground of script.

Tested on PHP 5.5.32
michael at squiloople dot com 01-Sep-2015 05:28
It should be noted that the function_exists check is not relative to the root namespace. This means that the namespace should be appended to the check:

<?php

  namespace test;

  if (!function_exists(__NAMESPACE__ . '\example'))
  {

    function example()
    {

    }

  }

?>
BruceB 22-Aug-2015 12:55
This is not going to go down as you might expect it should (even if you play smart and require/include_once):

<?php
if(function_exists('my_function'))
{
   throw new Exception("'my_function' is already defined!");
}

function my_function()
{
   // Do the work here
}
?>

This, however does work:

<?php
if( ! function_exists('my_function'))
{
   function my_function()
   {
      // Do the work here
   }
}
else
{
   throw new Exception("'my_function' is already defined!");
}
?>

Does it have anything to do with PHP parse/execute phases or global/local scope or those curly brackets or something else, I have no idea, but the latter ugly son works, while the former bombs out claiming that 'my_function' is already defined.

Thought this might save someone a few minutes of debugging time...
b dot g dot dariush at gmail dot com 07-Feb-2014 02:50
function_exists() does not cache its query internally.

by executing the following code

        $funcs = \array_shift(\get_defined_functions());
        $l = new \core\utiles\loadTime;
        $times = 0;
        $l->start();
        for($index = 0; $index<count($funcs); $index++)
        {
            foreach($funcs as $func)
            {
                $times++;
                if(\function_exists($func)) ;
            }
        }
        $s = $l->stop();
        echo "<span style='color:green'>$times</span> took : $s";
        # the output would be
         $> 2365444 took : 0.70324

By executing the

        $funcs = \array_shift(\get_defined_functions());
        $l = new \core\utiles\loadTime;
        $times = 0;
        $l->start();
        static $func_check = array();
        for($index = 0; $index<count($funcs); $index++)
        {
            foreach($funcs as $func)
            {
                $times++;
                if(!isset($func_check[$func]))
                {
                    if(\function_exists($func)) ;
                    $func_check[$func] = 1;
                }
                else $func_check[$func];
            }
        }
        $s = $l->stop();
        echo "<span style='color:green'>$times</span> took : $s";
        # the output would be
        $> 2365444 took : 0.53446

There is a 0.16878 seconds improvement, when you use static array to cache methods existence.
kitchin 25-Sep-2012 09:49
You can use this function to conditionally define functions, see: http://php.net/manual/en/functions.user-defined.php

For instance Wordpress uses it to make functions "pluggable." If a plugin has already defined a pluggable function, then the WP code knows not to try to redefine it.

But function_exists() will always return true unless you wrap any later function definition in a conditional clause, like if(){...}. This is a subtle matter in PHP parsing. Some examples:

<?php
if (function_exists('foo')) {
  print "foo defined\\n";
} else {
  print "foo not defined\\n";
}
function foo() {}

if (function_exists('bar')) {
  print "bar defined\\n";
} else {
  print "defining bar\\n";
  function bar() {}
}
print "calling bar\\n";
bar(); // ok to call function conditionally defined earlier

print "calling baz\\n";
baz(); // ok to call function unconditionally defined later
function baz() {}

qux(); // NOT ok to call function conditionally defined later
if (!function_exists('qux')) {
  function qux() {}
}
?>
Prints:
  foo defined
  defining bar
  calling bar
  calling baz
  PHP Fatal error: Call to undefined function qux()

Any oddities are probably due to the order in which you include/require files.
admin at gk-root dot com 22-Mar-2012 11:51
// If you want to chack if the function is enabled or disable in php.ini you can use this function:

<?php
function func_enabled($func) {
    $disabled = explode(',', ini_get('disable_functions'));
    foreach ($disabled as $disableFunction) {
        $is_disabled[] = trim($disableFunction);
    }
    if (in_array($func,$is_disabled)) {
        $it_is_disabled["m"] = $func.'() has been disabled for security reasons in php.ini';
        $it_is_disabled["s"] = 0;
    } else {
        $it_is_disabled["m"] = $func.'() is allow to use';
        $it_is_disabled["s"] = 1;
    }
    return $it_is_disabled;
}
?>

// An example of how to use:

<?php
$if_is_disabled = func_enabled('exec'); // return Arrey
echo $if_is_disabled["m"]; // return text value
echo '<br/>';
echo $if_is_disabled["s"]; // return 1 or 0
?>
zach at bygeekz dot com 21-Sep-2008 03:20
to php at fluidthoughts dot com

you aren't comparing against false, that if compares against true, so it's going to return that, as false != true
true being 1, True, (bool)True, etc.
false being 0, False, Null, etc
<?php

$foo=null;
if ($foo) { echo 'yay'; } //does not echo yay, because null is not True

$foo=false;
if ($foo) { echo 'yay'; } //does not echo yay, because false is not True

$foo=null;
if (!$foo) { echo 'yay'; } //echoes yay!

$foo=false;
if (!$foo) { echo 'yay'; } //echoes yay!

?>

This..
<?php
    if (!function_exists('')) {
                echo "empty string function doesnt exist as compared as negative\n";
        }

        if (!function_exists(NULL)) {
                echo "NULL function doesnt exist as compared as negative\n";
        }
?>
Works.

Alternatively, to enter upon existance..

Use your code. Since they dont exist, it wont enter..
php at fluidthoughts dot com 19-Mar-2008 07:18
function_exists returns false on NULL and empty string:

<?php
        if (function_exists('')) {
                echo "empty string function exists\n";
        }

        if (function_exists(NULL)) {
                echo "NULL function exists\n";
        }
?>

Neither of the echo statements happen when I run this.
webmaster at mamo-net dot de 22-Sep-2007 02:43
If you use suhosin.executor.func.blacklist instead of disabled_functions in your php.ini, function_exists will return true for a disabled function. I used this to have the same beahviour with suhosin.executor.func.blacklist and disabled_functions:

<?php
function suhosin_function_exists($func) {
    if (extension_loaded('suhosin')) {
        $suhosin = @ini_get("suhosin.executor.func.blacklist");
        if (empty($suhosin) == false) {
            $suhosin = explode(',', $suhosin);
            $suhosin = array_map('trim', $suhosin);
            $suhosin = array_map('strtolower', $suhosin);
            return (function_exists($func) == true && array_search($func, $suhosin) === false);
        }
    }
    return function_exists($func);
}
?>
White-Gandalf 13-Sep-2007 04:26
I stumbled over the same problem as "eddiec" (users not able or not willing to use "_once"-suffixes).

A possible alternative explanation for the behavior:

If a file is included, it is possibly parsed every include-time.(?)
While parsing, every function in global scope is tried to register. THIS gets wrong, when multiple times included, and it produces an error.

If functions are defined within block scopes, their registration seems to be delayed until execution of such a block. Thus, not the function "function_exists" functions wrong, but simply the philosophy of the interpreter produces such results.

Thus, the same effect can be achieved by simply putting block braces around the contents of an include_once file:

if (function_exists('function_in_question')) return;
{
    function function_in_question(...)
    {
        ...
    }
    ...other stuff
}

...which is equivalent to...

if (!function_exists('function_in_question'))
{
    function function_in_question(...)
    {
        ...
    }
    ...other stuff
}
dieter at edarta dot be 16-Feb-2007 12:09
to brooklynphil at hotmail dot com:

Your post is misleading, namely the 3rd and last speedtest you describe is an unfair comparison:

<?php
is_callable('test','test');
?>

The value of the 2nd parameter $syntax_only is 'test' and this evaluates to boolean true. In other words, this is exactly the same as calling the function like this:

<?php
is_callable('test',true);
?>

Of course this will be faster because is_callable only does a very basic syntaxcheck. From the documentation: 'It will only reject simple variables that are not strings, or an array that does not have a valid structure to be used as a callback.'

If you omit this erroneous 3rd test, then according to your examples function_exists is 2 to 4 times faster.

I hope you can see that loop-testing functions is not so simple. :)

rtfm
eddiec at stararcher dot com 09-Feb-2007 06:58
Note that function_exists will return TRUE in the following situation, presumably because the function "testfunc" was defined when the script was PARSED/ITERPRETED, before the function_exists call was made at RUNTIME:

<?php
if (function_exists('testfunc')) return;
function testfunc() { }
?>

So, this construction is not useful for preventing testfunc from being multiply defined if the script is muliply included or required.

However, the following construction DOES work to prevent multiple defines of testfunc:

<?php
if (!function_exists('testfunc')) {
  function testfunc() { }
}
?>

CONTRAST this with similar uses of defined() which is completely runtime evaluated.  These both work:

<?php
if (defined('testfunc_defined')) return;
define('testfunc_defined', 1);
function testfunc() { }
?>

AND...

<?php
if (!defined('testfunc_defined')) {
define('testfunc_defined', 1);
function testfunc() { }
}
brooklynphil at hotmail dot com 30-Jan-2007 08:02
to bob at thethirdshift dot net
regarding is_callable vs function_exists.

using your code
is_callable = TRUE, function_exists = FALSE

Did 10000 is_callables in 0.0443360805511 seconds
Did 10000 function_exists in 0.0111110210419 seconds

then we replace
is_callable(array('test','test'));
with
$callarray = array('test','test'); // place this outside for-loop
is_callable($callarray);

is_callable = TRUE, function_exists = FALSE

Did 10000 is_callables in 0.0314660072327 seconds
Did 10000 function_exists in 0.0120670795441 seconds

then we replace
is_callable(array('test','test'));
with
is_callable('test','test');

is_callable = TRUE, function_exists = FALSE

Did 10000 is_callables in 0.00991606712341 seconds
Did 10000 function_exists in 0.0113790035248 seconds

I hope you can see that loop-testing functions is not so simple. :)
Dan 17-Jul-2006 05:49
I would like to comment on the following post:

A note of caution: function_exists() appears to be case-insensitive (at least as of PHP 4.3.8).  e.g.:

<?php
   function MyCasedFunction() {
       return true;
   }

   // Will return true, even though casing is "wrong"
   if (function_exists("mYcAsEdFuNcTiOn"))
       echo "I see it!";
?>

I believe that function calls itself are case insensitve, so this function is returning a valid truth. PHP doesn't care about cases.
andi at splitbrain dot org 07-Jul-2006 12:48
function_exists will return false for functions disabled with the disable_functions ini directive. However those functions are still declared so trying to define them yourself will fail.

<?php
if(!function_exists('readfile')){
  function readfile($file){
    $handle=@fopen($cache,"r");
    echo @fread($handle,filesize($file));
    @fclose($handle);
  }
}
?>

The above will issue a "Cannot redeclare readfile()" fatal error if readfile was disabled with disable_functions.
neelam_ab2003 at yahoo dot co dot in 11-May-2006 09:06
<?php
/*PHP doesn't Support nested functions. I have tried following in PHP_VERSION - 5.1.2*/

function A(){}

function B(){
    function C(){
        function D(){}
    }
}

IsFunctionExist('A');
IsFunctionExist('B');
IsFunctionExist('C');
IsFunctionExist('D');

function IsFunctionExist($funcName){
    echo function_exists($funcName)?" $funcName exist <br>":" $funcName doesn't exist <br>";
}
?>

/*O U T P U T
A exist
B exist
C doesn't exist
D doesn't exist
*/
chaumo 16-Jul-2005 02:46
to avoid direct calls this can be better than function_exists
in the parent file:
<?php
define("IN_MODULE",true);
?>
and in the target file:
<?php
if(!defined("IN_MODULE")) die("Can't access the file directly");
?>
fili at fili dot nl 08-Jun-2005 06:24
To prevent direct calls to included files i use the following technique.

In the main file create an empty function with a random name. Like so:

<?php
function hjudejdjiwe() { return true; }
?>

Then check for the existence of this function within your include:

<?php
if (!function_exists('hjudejdjiwe')) { die('!'); }
?>

Simple but effective.
bob at thethirdshift dot net 23-Jun-2004 06:55
I, too, was wondering whether is_callable or function exists is faster when checking class methods.  So, I setup the following test:

<?php
function doTimes($start, $end)
  {
    $start_time = explode (" ", $start);
    $start_time = $start_time[1] + $start_time[0];
    $end_time = explode (" ", $end);
    $end_time = $end_time[1] + $end_time[0];
    $time = $end_time - $start_time;
    return $time;
  }

class test
  {
      function test()
      {
          return true;
      }
  }

$callableIsTrue = false;
$startIsCallable = microtime();
for($i = 0; $i < 10000; $i++)
  {
      if(is_callable(array('test', 'test'))) { $callableIsTrue = true; }
  }
$endIsCallable = microtime();

$existsIsTrue = false;
$startExists = microtime();
for($i = 0; $i < 10000; $i++)
  {
      if(function_exists('test::test')) { $existsIsTrue = true; }
  }
$endExists = microtime();

$timeIsCallable = doTimes($startIsCallable, $endIsCallable);
$timeExists     = doTimes($startExists, $endExists);

echo "<b>is_callable = ".($callableIsTrue ? "TRUE" : "FALSE")."</b>, \n";
echo "<b>function_exists = ".($existsIsTrue ? "TRUE" : "FALSE")."</b><br>\n";

echo "<br>Did 10000 is_callables in ".$timeIsCallable." seconds";
echo "<br>Did 10000 function_exists in ".$timeExists." seconds";
?>

This gives the output :

is_callable = TRUE, function_exists = FALSE

Did 10000 is_callables in 0.0640790462494 seconds
Did 10000 function_exists in 0.0304429531097 seconds

So the fact that function_exists is twice as fast is slightly over shadowed by the fact that it doesn't work on class methods, at least not as far as I can tell.
ckrack at i-z dot de 09-Mar-2004 09:22
i was wondering whether is_callable or function exists is faster when checking class methods.

is_callable(array('foo', 'bar'));
function_exists('foo::bar');

my results when doing each operation 10000 times with a simple test class were the following:

is_callable: 0.28671383857727 seconds
function_exists: 0.14569997787476 seconds

(following tests have proved this to be true).

thus you can see, function_exists is twice as fast as is_callable.
breadman 30-Jul-2003 02:17
Functions within a function are better off as anonymous returns from create_function(), unless you want to be able to call it elsewhere.

However, I have used this in skinning:  I use alert_box() to display certain errors, like a faulty SQL query.  This simply calls display_alert(), which is defined in my skin scripts.  However, alert_box() is sometimes called before I know which skin to load, so it has its own functionality which it uses if function_exists('display_alert') returns false.
dshearin at excite dot com 09-Jul-2003 12:15
This can be used to conditionally define a user function. In this sense, it can act as a sort of inline include_once().

For example, suppose you have a function A that calls function B. B is only used inside function A and is never called from anywhere else in the script. It's logical (and perfectly legal in PHP) to define B inside of A's definition, like so:

<?php
function A($inputArray)
{
   if (!function_exists('B'))
   {
      function B($item)
      {
           // Do something with $item
         // and return result
          return $result;
      }
   }
   foreach ($inputArray as $nextItem) $outputArray[] = B($nextItem);
   return $outputArray;
}
?>

Without the function_exists test, you would get a fatal error the second time you called A, as PHP would think you were trying to redefine B (not legal in PHP). The placement of the test is also important. Since the if block is executed sequentially, like any other block of code, it must come before any call to the function defined within.


Returns an array of all defined functions

  • ≪ function_exists
  • register_shutdown_function ≫
  • PHP Manual
  • Function handling 함수 목록
  • Returns an array of all defined functions

get_defined_functions

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

get_defined_functions — Returns an array of all defined functions

설명

array get_defined_functions ( void )

Gets an array of all defined functions.

반환값

Returns a multidimensional array containing a list of all defined functions, both built-in (internal) and user-defined. The internal functions will be accessible via $arr["internal"], and the user defined ones using $arr["user"] (see example below).

예제

Example #1 get_defined_functions() example

<?php
function myrow($id, $data)
{
    return "<tr><th>$id</th><td>$data</td></tr>\n";
}

$arr = get_defined_functions();

print_r($arr);
?>

위 예제의 출력 예시:

Array
(
    [internal] => Array
        (
            [0] => zend_version
            [1] => func_num_args
            [2] => func_get_arg
            [3] => func_get_args
            [4] => strlen
            [5] => strcmp
            [6] => strncmp
            ...
            [750] => bcscale
            [751] => bccomp
        )

    [user] => Array
        (
            [0] => myrow
        )

)

참고

  • function_exists() - Return TRUE if the given function has been defined
  • get_defined_vars() - 모든 정의된 변수의 배열을 반환
  • get_defined_constants() - 모든 상수와 그 값의 연관 배열을 반환합니다
  • get_declared_classes() - 선언된 클래스명을 배열로 반환

User Contributed Notes

Muneeb Aslam 27-Oct-2015 09:30
This is rather a simple non-confusing script to get the function names linked to its manual page on php.net. Hope it helps someone. Commented script is self explainatory

<?php

    /*declare a variable to php manual of functions.
    change the $lng to the region you want it for,
    i-e en/es/de etc etc */
    $lng = "es";
    $url = "http://www.php.net/manual/".$lng."/function.";

    // get defined functions in a variable (it will be a 2D array)
    $functions = get_defined_functions();

    // Run nested foreach to get the function names
    foreach($functions as $function){
        foreach ($function as $functionName){

            /* Since php manual is using hyphens instead of underscores
            for functions, we will convert underscores to hyphen whereever
            there is one. */
            if(strpos($functionName,"_") !== false){
                $functionForURL = str_replace("_","-",$functionName);
            } else {
                $functionForURL = $functionName;
            }

            /* echo the link */
            echo "<a href='".$url.$functionForURL.".php'>".$functionName."</a><br />";
        }
    }

?>
Muneeb Aslam 27-Oct-2015 04:46
We can also get the function names with this simple foreach

<?php

    //Since its a two dimensional array
    $functions = get_defined_functions();

    //We can use nested foreach to get the function names
    foreach($functions as $function){
         foreach($function as $thisFunc){
             echo $thisFunc . "<br />";
         }
     }

?>
kkuczok at gmail dot com 23-May-2013 05:14
You can list all arguments using ReflectionFunction class. It's not necessary to parse selected files/files as suggested by Nguyet.Duc.

http://php.net/manual/pl/class.reflectionfunction.php

Example:
<?php
function foo(&$bar, $big, $small = 1) {}
function bar($foo) {}
function noparams() {}
function byrefandopt(&$the = 'one') {}

$functions = get_defined_functions();
$functions_list = array();
foreach ($functions['user'] as $func) {
        $f = new ReflectionFunction($func);
        $args = array();
        foreach ($f->getParameters() as $param) {
                $tmparg = '';
                if ($param->isPassedByReference()) $tmparg = '&';
                if ($param->isOptional()) {
                        $tmparg = '[' . $tmparg . '$' . $param->getName() . ' = ' . $param->getDefaultValue() . ']';
                } else {
                        $tmparg.= '&' . $param->getName();
                }
                $args[] = $tmparg;
                unset ($tmparg);
        }
        $functions_list[] = 'function ' . $func . ' ( ' . implode(', ', $args) . ' )' . PHP_EOL;
}
print_r($functions_list);
?>

Output:
Array
(
    [0] => function foo ( &&bar, &big, [$small = 1] )

    [1] => function bar ( &foo )

    [2] => function noparams (  )

    [3] => function byrefandopt ( [&$the = one] )

)
Nguyet.Duc 30-Jul-2011 10:22
get_defined_functions() just returns a list of function names without function arguments. Here is the code to list user-defined function names plus arguments:

<?php
    $content = file_get_contents('example.php');
    preg_match_all("/(function )(\S*\(\S*\))/", $content, $matches);
    foreach($matches[2] as $match) {
        $function[] = "// " . trim($match) . "<br />\n";
    }
    natcasesort($function);
    $functionlist .= "/* Functions in this file */<br />\n";
    $functionlist .= "/**************************/<br />\n\n";
    $functionlist .= implode('', $function);
    echo $functionlist;
?>

Output:

/* Functions in this file */
/**************************/
// add_data($data)
// add_files($list)
// archive($name)
// bzip_file($name)
rob at webdimension dot co dot uk 01-Apr-2011 05:48
A quick way of using this function:

<?php
// ALL USER DEFINED FUNCTIONS
$arr = get_defined_functions();
foreach ($arr['user'] as $key => $value){
echo $value.'<br />';
}
// ALL USER DEFINED FUNCTIONS

// ALL INTERNAL FUNCTIONS
$arr = get_defined_functions();
foreach ($arr['internal'] as $key => $value){
echo $value.'<br />';
}
// ALL INTERNAL FUNCTIONS
?>
ChaosKaizer 10-Jul-2008 04:13
for user defined function

<?php
/**
 * @param string $function_name The user function name, as a string.
 * @return Returns TRUE if function_name  exists and is a function, FALSE otherwise.
 */
function user_func_exists($function_name = 'do_action') {

    $func = get_defined_functions();

    $user_func = array_flip($func['user']);

    unset($func);

    return ( isset($user_func[$function_name]) );
}
?>
strrev xc.noxeh@ellij 04-Jun-2008 02:31
Please note that functions created with create_function() are not returned.
(However that might change in a later version)
spudinski at gmail dot com 01-Apr-2008 03:13
To search for a function.
<html>
<head>
<title>List of all Internal functions</title>
<style type="text/css">
body {
    background-color: #FFFFFF;
    color: #222222;
    font-size: 11px;
    font-family: arial, tahoma;
}
table {
    color: #222222;
    font-size: 11px;
    font-family: arial, tahoma;
}
tr.found {
    background-color: #66EE00;
    font-weight: bold;
}
a:link {
    color: #222222;
}
a:visited {
    color: #CCCCCC;
}
a:active {
    color: #444444;
}
a:hover {
    text-decoration: underline;
}
</style>
</head>
<body>
<p>
    <form method="GET">
    Search: <input type="text" name="search"><br>
    <input type="submit">
    </form>
</p>
<?php
    if (!empty($_GET['search'])) {
        echo '<p>' . '<a href="#' . $_GET['search'] . '">' .
        'Goto ' . $_GET['search'] . '</a>' .
        '<script type="text/javascript">
            window.onload = function() {
                document.location += "#' . $_GET['search'] . '";
                return true;
            }
        </script>
        </p>';
    }
?>
<p>
    <table>
<?php
    $country = 'us';
    $functions = get_defined_functions();
    $functions = $functions['internal'];
    $num = 0;
    foreach($functions as $function) {
        $num++;
        echo '<tr ' . (($_GET['search'] == $function) ? 'class="found"' : '') . '><td>' .
        number_format($num) . '</td><td>' . '<a name="' . $function . '" href="http://' . $country . '.php.net/' .
        $function . '">' . $function . '</a>' . '</td></tr>';
    }
?>
    </table>
</p>
</body>
</html>
berchentreff at berchentreff dot de 31-Mar-2006 02:46
look at here, list all the defined function on your php-Version and give as well formatted output width links onto the php-manual:

<html><head>
<style type="text/css"><!--
li{font-family:Verdana,Arail,sans-serif;width:500px;margin-top:7px;}
a{padding:4px;}
a.a1{font-size:12px;background-color:#CCCCCC;color:#663300;}
a.a1:hover{background-color:#663300;color:#CCCCCC;}
a.a1:visited{background-color:#fff;color:#999;}
a.a1:visited:hover{background-color:#fff;color:#999;}
a.a0{font-size:12px;background-color:#CCCCFF;color:#663399;}
a.a0:hover{background-color:#663399;color:#CCCCFF;}
a.a0:visited{background-color:#ffC;color:#999;}
a.a0:visited:hover{background-color:#ffC;color:#999;}
--></style>
</head><body style="background-color:#999;">
<?php
$arr = get_defined_functions();

foreach($arr as $zeile){
sort($zeile);$s=0;
foreach($zeile as $bzeile){
$s=($s)?0:1;
echo "<li><a class='a".$s."'  href='http://de.php.net/".$bzeile."'>".$bzeile."</a></li>";}
}
?>
</body>
</html>
mIHATESPAMduskis at bates dot edu 21-Nov-2002 11:47
At least with PHP 4.2.3 on a GNU/Linux/Apache platform, get_defined_functions() returns user-defined functions as all-lower case strings regardless of how the functions are capitalized when they are defined.

Threw me for a loop.
peten at spam dot me dot not dot frontiernet dot net 02-Jun-2002 07:28
Here's a useful trick with the get_defined_functions function - show all available functions with a link to the documentation (you can even change the mirror it goes to):

<?php
  // the php mirror
  $php_host = "http://us2.php.net/";

  // the number of cols in our table
  $num_cols = 3;

  $ar = get_defined_functions();
  $int_funct = $ar[internal];
  sort($int_funct);
  $count = count($int_funct);
?>
<html>
 <head>
  <title>
   Available PHP Functions
  </title>
 </head>
 <body>
  <p>
   <?php print $count; ?> functions
    available on
    <?php
      print $_SERVER[SERVER_NAME];
     ?>
   (<a href="<?php print $php_host;?>"
    target="phpwin">php</a>
    version
    <?php print phpversion(); ?>)
  </p>
  <table align="center" border="2">
   <tr>
<?php
  for($i=0;$i<$count;$i++) {
    $doc = $php_host
     . "manual/en/function."
     . strtr($int_funct[$i], "_", "-")
     . ".php";
    print "    <td><a href=\"" . $doc
     . "\" target=\"phpwin\">"
     . $int_funct[$i]
     . "</a></td>\n";
    if(($i > 1)
     && (($i+$num_cols)%$num_cols==($num_cols-1)))
      print "   </tr>\n   <tr>\n";
    }
  for($i=($num_cols-($count%$num_cols));$i>0;$i--)
    print "    <td>&nbsp;</td>\n";
?>
  </table>
 </body>
</html>


Register a function for execution on shutdown

  • ≪ get_defined_functions
  • register_tick_function ≫
  • PHP Manual
  • Function handling 함수 목록
  • Register a function for execution on shutdown

register_shutdown_function

(PHP 4, PHP 5, PHP 7)

register_shutdown_function — Register a function for execution on shutdown

설명

void register_shutdown_function ( callable $callback [, mixed $parameter [, mixed $... ]] )

Registers a callback to be executed after script execution finishes or exit() is called.

Multiple calls to register_shutdown_function() can be made, and each will be called in the same order as they were registered. If you call exit() within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called.

인수

callback

The shutdown callback to register.

The shutdown callbacks are executed as the part of the request, so it's possible to send output from them and access output buffers.

parameter

It is possible to pass parameters to the shutdown function by passing additional parameters.

...

반환값

값을 반환하지 않습니다.

오류/예외

If the passed callback is not callable a E_WARNING level error will be generated.

예제

Example #1 register_shutdown_function() example

<?php
function shutdown()
{
    // This is our shutdown function, in
    // here we can do any last operations
    // before the script is complete.

    echo 'Script executed with success', PHP_EOL;
}

register_shutdown_function('shutdown');
?>

주의

Note:

Working directory of the script can change inside the shutdown function under some web servers, e.g. Apache.

Note:

Shutdown functions will not be executed if the process is killed with a SIGTERM or SIGKILL signal. While you cannot intercept a SIGKILL, you can use pcntl_signal() to install a handler for a SIGTERM which uses exit() to end cleanly.

참고

  • auto_append_file
  • exit() - Output a message and terminate the current script
  • The section on connection handling

User Contributed Notes

xcl_rockman at qq dot com 25-Sep-2015 03:02
for static call
register_shutdown_function(function () {
                demo::shutdownFunc();
});
miguelangel dot nubla at gmail dot com 20-May-2014 09:26
Quick note: As of PHP 5.5.11 this function ignores current namespace and 'use' statements so it is mandatory to use the fully qualified name of the function in the callback parameter
m dot agkopian at gmail dot com 01-May-2014 07:29
Use this small snippet inside your bootstrap in order to always have a way to know reliably what was the last page of your site that the user have visited, without having to use $_SERVER['HTTP_REFERER'].

<?php
session_start();

// Get current page
$current_page = htmlspecialchars($_SERVER['SCRIPT_NAME'], ENT_QUOTES, 'UTF-8');
$current_page .= $_SERVER['QUERY_STRING'] ? '?'.htmlspecialchars($_SERVER['QUERY_STRING'], ENT_QUOTES, 'UTF-8') : '';

// Set previous page at the end
register_shutdown_function(function ($current_page) {
    $_SESSION['previous_page'] = $current_page;
}, $current_page);
?>
Tim 01-Nov-2013 01:35
register_shutdown_function seems to be immune to whatever value was set with set_time_limit or the max_execution_time value defined in php.ini.
<?php
function asdf() {
    echo microtime(true) . '<br>';
    sleep(1);
    echo microtime(true) . '<br>';
    sleep(1);
    echo microtime(true) . '<br>';
}
register_shutdown_function('asdf');
set_time_limit(1);

while(true) {}
?>
The output is three lines.
ealexs at gmail dot com 19-Jul-2013 05:13
If you use register_shutdown_function under fastCGI (using php 5.4.4, Apache 2.2/Debian) and the user clicks cancel request in the browser register_shutdown_function will not be called.
My solution was to use ignore_user_abort(true) to ignore the user abort.
Example (you may test request abort in the browser with and without ignore_user_abort):

<?php

    // use this to make sure the shutdown function will be called
    ignore_user_abort(true);

    $f = fopen("debug.out.txt", "wt");

    function qShutdown()
    {
        global $f;

        // use __DIR__ as the shutdown it's not invoke under the same folder
        file_put_contents(__DIR__. "/qShutdown.txt", "qShutdown called");

        fwrite($f, "I was called\n");
        fclose($f);

        echo "I was called\n";
    }

    register_shutdown_function("qShutdown");

    // do some random stuff

    echo "things were executed\n";
    fwrite($f, "things were executed\n");
    fflush($f);

    $n = 10;
    $i = 0;
    while ($i < $n)
    {
        sleep(1);
        $i++;

        echo "Working {$i} ...\n";
        fwrite($f, "Working {$i} ...\n");
        fflush($f);

        ob_flush();
        flush();
        ob_flush();
    }

    fwrite($f, "die called\n");
    fflush($f);

    die("die called\n");

?>
matteosistisette at gmail dot com 14-Apr-2013 02:42
In PHP 5.1.6 things don't work as described here.

The truth is that connection_status() just always returns 0 whether or not the client has aborted (e.g. the user hit the stop button), and that (consistently with this) the registered shutdown function will only be called on exit, but NOT when the connection is aborted from the user.

That is, PHP does NOT detect when the connection is aborted.
alexyam at live dot com 09-Apr-2012 12:50
When using php-fpm, fastcgi_finish_request() should be used instead of register_shutdown_function() and exit()

For example, under nginx and php-fpm 5.3+, this will make browsers wait 10 seconds to show output:

<?php
    echo "You have to wait 10 seconds to see this.<br>";
    register_shutdown_function('shutdown');
    exit;
    function shutdown(){
        sleep(10);
        echo "Because exit() doesn't terminate php-fpm calls immediately.<br>";
    }
?>

This doesn't:

<?php
    echo "You can see this from the browser immediately.<br>";
    fastcgi_finish_request();
    sleep(10);
    echo "You can't see this form the browser.";
?>
david dot schueler at tel-billig dot de 11-Nov-2010 01:51
I had a problem when forking a child process and accessing the variables by using the "global" keyword in the shutdown function.
So i used another way for getting the variables to the shutdown function: I passed them by reference.

Example:
<?php
function shutdown_function (&$test) {
    echo __FUNCTION__.'(): $test = '.$test."\n";
}

$test = 1;
register_shutdown_function('shutdown_function', &$test);
echo '$test = '.$test."\n";

// do some stuff and change the variable values
$test = 2;

// now the shutdown function gets called
exit(0);
?>

Maybe tis helps someone. (I'm using PHP 5.2.11)
emanueledelgrande ad email dot it 19-Sep-2010 04:53
A lot of useful services may be delegated to this useful trigger.
It is very effective because it is executed at the end of the script but before any object destruction, so all instantiations are still alive.

Here's a simple shutdown events manager class which allows to manage either functions or static/dynamic methods, with an indefinite number of arguments without using any reflection, availing on a internal handling through func_get_args() and call_user_func_array() specific functions:

<?php
// managing the shutdown callback events:
class shutdownScheduler {
    private $callbacks; // array to store user callbacks

    public function __construct() {
        $this->callbacks = array();
        register_shutdown_function(array($this, 'callRegisteredShutdown'));
    }
    public function registerShutdownEvent() {
        $callback = func_get_args();

        if (empty($callback)) {
            trigger_error('No callback passed to '.__FUNCTION__.' method', E_USER_ERROR);
            return false;
        }
        if (!is_callable($callback[0])) {
            trigger_error('Invalid callback passed to the '.__FUNCTION__.' method', E_USER_ERROR);
            return false;
        }
        $this->callbacks[] = $callback;
        return true;
    }
    public function callRegisteredShutdown() {
        foreach ($this->callbacks as $arguments) {
            $callback = array_shift($arguments);
            call_user_func_array($callback, $arguments);
        }
    }
    // test methods:
    public function dynamicTest() {
        echo '_REQUEST array is '.count($_REQUEST).' elements long.<br />';
    }
    public static function staticTest() {
        echo '_SERVER array is '.count($_SERVER).' elements long.<br />';
    }
}
?>

A simple application:

<?php
// a generic function
function say($a = 'a generic greeting', $b = '') {
    echo "Saying {$a} {$b}<br />";
}

$scheduler = new shutdownScheduler();

// schedule a global scope function:
$scheduler->registerShutdownEvent('say', 'hello!');

// try to schedule a dyamic method:
$scheduler->registerShutdownEvent(array($scheduler, 'dynamicTest'));
// try with a static call:
$scheduler->registerShutdownEvent('scheduler::staticTest');

?>

It is easy to guess how to extend this example in a more complex context in which user defined functions and methods should be handled according to the priority depending on specific variables.

Hope it may help somebody.
Happy coding!
clover at fromru dot com 01-Jun-2010 11:17
If you register function that needs to be running last (for example, close database connection) - just register another shutdown function from shutdown function:
<?php
function test1(){
  register_shutdown_function('test_last');
}

function test2(){/*...*/}
function test3(){/*...*/}
function test_last(){/*...*/}

register_shutdown_function('test1');
register_shutdown_function('test2');
register_shutdown_function('test3');
?>

the script will call functions in correct order: test1, test2, test3, test_last
sander @ unity-x 12-May-2010 12:42
simple method to disconnect the client and continue processing:

<?php
function endOutput($endMessage){
    ignore_user_abort(true);
    set_time_limit(0);
    header("Connection: close");
    header("Content-Length: ".strlen($endMessage));
    echo $endMessage;
    echo str_repeat("\r\n", 10); // just to be sure
    flush();
}

// Must be called before any output
endOutput("thank you for visiting, have a nice day');

sleep(100);
mail("you@yourmail.com", "ping", "i'm here");
?>
jawsper at aximax dot nl 20-Apr-2010 02:21
Something found out during testing:

the ini auto_append_file will be included BEFORE the registered function(s) will be called.
Anonymous 30-Mar-2010 06:41
Note that in the various solutions presented here, a call to ob_start() at the begining of a script and a flush at the end will defeat any optimisation attempt to return large files in chunks (custom code or readfile). i.e. don't use ob_start if you are going to return large files you will simply bust the memory limit.
ravenswd at gmail dot com 21-Dec-2009 10:43
You may get the idea to call debug_backtrace or debug_print_backtrace from inside a shutdown function, to trace where a fatal error occurred. Unfortunately, these functions will not work inside a shutdown function.
Ant P. 02-Oct-2009 09:13
As of PHP 5.3.0, the last note about unpredictable working directory also applies to FastCGI, when previously it didn't.
Anonymous 17-Sep-2009 02:29
Note that if the handler function is private static it will never be called! It must be public!
michaeln at associationsplus dawt see eh 28-Aug-2009 11:52
This may seem obvious to many people, but don't register an object's __destruct() function as a shutdown function! In many instances, it will then call the destructor twice, most likely on an already destroyed object! For example, you might get this error:

PHP Warning:  mysqli::close() [<a href='function.mysqli-close'>function.mysqli-close</a>]: Couldn't fetch mysqli in /path/to/file.php

I saw docey's comment from 2006 in this very page where they do just that, and this more often than not cause problems!

It would be wise to look out for old code where someone has done that very thing then too!
Filip Dalge 16-Aug-2009 05:35
The following function register_close_function should reproduce the former php behavior of closing the connection before executing the shutdown handler, based on the code posted by sts at mail dot xubion dot hu. It does not work on any machine.

<?php
function register_close_function($func) {
  register_close_function::$func = $func;
  register_shutdown_function(array("register_close_function", "close"));
  ob_start();
}
class register_close_function {
  // just a container
  static $func;
  function close() {
    header("Connection: close");
    $size = ob_get_length();
    header("Content-Length: $size");
    ob_end_flush();
    flush();
    call_user_func(register_close_function::$func);
  }
}
?>
pgl at yoyo dot org 02-Aug-2009 11:25
You definitely need to be careful about using relative paths in after the shutdown function has been called, but the current working directory doesn't (necessarily) get changed to the web server's ServerRoot - I've tested on two different servers and they both have their CWD changed to '/' (which isn't the ServerRoot).

This demonstrates the behaviour:

<?php
function echocwd() { echo 'cwd: ', getcwd(), "\n"; }

register_shutdown_function('echocwd');
echocwd() and exit;
?>

Outputs:

cwd: /path/to/my/site/docroot/test
cwd: /

NB: CLI scripts are unaffected, and keep their CWD as the directory the script was called from.
Bob 01-Jul-2009 07:21
A note about error management.
If you like to use the boolean 'or' operator for error detection, like this:
<?php
register_shutdown_function('my_shutdown_function') or
    throw new Exception("Could not register shutdown function");
?>
it won't work.
This function always returns void which == false, so the exception will *always* be thrown.
Error reporting is integrated into the function.
cFreed at orange dot fr 14-Mar-2009 06:36
May be obvious for most people, but I spent time to clearly understand something which is only INDIRECTLY mentioned by the manual, so I hope this useful for someones.

Pay attention to the function prototype: the $function argument is a callback one (follow the link for more information).
This means that you must write this argument differently depending on the $function context:
. function-name, as a string, when it is a built-in or user-defined function
. array(class-name, method-name), when it is an object method
SonOfTron 05-Oct-2008 03:24
You can achieve similar results by using auto_append_file in the php.ini or .htaccess file:
(php.ini)
auto_append_file = /my-auto-append-file.php
(.htaccess)
php_value auto_append_file /my-auto-append-file.php
RLK 14-Sep-2008 11:26
Contrary to the the note that "headers are always sent" and some of the comments below - You CAN use header() inside of a shutdown function as you would anywhere else; when headers_sent() is false. You can do custom fatal handling this way:

<?php
ini_set('display_errors',0);
register_shutdown_function('shutdown');

$obj = new stdClass();
$obj->method();

function shutdown()
{
  if(!is_null($e = error_get_last()))
  {
    header('content-type: text/plain');
    print "this is not html:\n\n". print_r($e,true);
  }
}
?>
boutell round-about boutell dhot com 30-Nov-2007 02:37
According to tests shown in this comment on the PHP5 deconstructor page:

http://us3.php.net/manual/en/language.oop5.decon.php#76710

Functions registered with register_shutdown_function are called before deconstructors, at least as of PHP 5.2.1. This contradicts an earlier commenter who claims that objects cannot be utilized in functions called from register_shutdown_function. This is good news for those who want to use register_shutdown_function to work around the (fairly nasty) problems with using objects in a session write handler.
dcez at land dot ru 13-Sep-2007 06:57
This simple function (i gues)  is for measuring page generation time in microseconds, using earlier mentioned register_shutdown_function...

Here is an example:

<?php
set_time_limit(0);

function check_time($time_str, $end = false){

    global $start; //Need some global variable where we will ceep our start time

    list($msec, $sec) = explode(' ', $time_str);
    $result = bcadd($msec, $sec, 10);

    if(!$end){

        $start = $result;
        register_shutdown_function('check_time', microtime(), 'true'); //Function registrates itself only with some parametrs now

    }elseif($end){

        $end = $result;
        echo '<br />Page was generated in '.bcsub($end, $start, 10).' seconds...<br />';

    }

}

check_time(microtime()); //Caling our function once!

//Doing some job which we will measure...

$fd = fopen('users.txt', 'r');
while(!feof($fd)){
    $buffer .= fread($fd, 1024);
}
fclose($fd);

?>

As you see may be its large, but you dont have to call "check_time" function twice, in the start and at the end of the script, as many users usually do...
Fuzika 14-Aug-2007 02:27
Sinured's example of how to use a static method did not work for me.
But this did:

<?php
register_shutdown_function(array('theClass','theStaticMethod'));
?>

Hope this helps someone.

[EDIT by danbrown AT php DOT net: This example did not work for the author of this note because it is written for PHP 5.  The author was using PHP 4.]
Sinured 11-Aug-2007 03:26
You can use a static method of a class as a shutdown function without passing an instance. I think that doesn't make too much sense, however it's possible.

<?php
register_shutdown_function('someClass::someMethod');
?>

If this method is *NOT* static, then PHP will complain about an invalid callback instead of just emitting an E_STRICT error.
buraks78 at gmail dot com 02-Jul-2007 02:51
Read the post regarding the change of working directory to ServerRoot carefully. This means that require_once "path/to/script" (or similar constructs) with relative paths will not work in shutdown functions.
Ldkronos 25-Jan-2007 06:55
It's already been discussed on the zlib page ( http://www.php.net/zlib ), but hasn't been mention here...

When zlib compression is enabled, register_shutdown_function doesn't work properly. Anything output from your shutdown function will not be readable by the browser. Firefox just ignores it. IE6 will show you a blank page.

The workaround that worked for me is to add the following to the top of the script.
ini_set('zlib.output_compression', 0);
docey 06-Nov-2006 09:17
if you want to make use of php5 __construct and _destruct functionality this functions is your rescue.

so say you have a class called 'fiveclass', you need some code like this:

<?php
class fiveclass
{

  function fiveclass()
  {
   return $this->__construct(); // forward php4 to __construct
  }

  function __construct()
  {
    // register __destruct method as shutdown function
    register_shutdown_function(array(&$this, "__destruct"));

   return true;
  }

  function __destruct()
  {
    // finish what where doing;
    close_db();
    flush_output();
    write_cache_away();

   return true;
  }

}

$obj = new fiveclass();
?>

now you have the same functionality. __destruct is automaticly called at the end of your script.
morphling at quick dot cz 01-May-2006 12:14
There is a note "Shutdown function is called during the script shutdown so headers are always already sent.", but my php 5.1 seems to act differently.
Example:
<?php
class Test {

    private $running;

    public function main() {
        $this->running = true;
        ob_start();
        error_reporting(E_ALL);
        register_shutdown_function(array($this, "clean_exit"));
        echo "Hello";
        // triggers E_ERROR
        $fatal->error();

        $this->running = false;
    }

    public function clean_exit() {
        if ($this->running) {
            header("Location: error.php");
        }
    }
}
$t = new Test();
$t->main();
?>
This example redirects you on error.php, this could be a simple way to handle E_ERROR.
michael at onlinecity dot dk 20-Apr-2006 10:07
Note that under Windows 2003/IIS6 and PHP5 5.1.2 ISAPI the register_shutdown_function executes under IUSR_, regardless of what user the script executes under.
This means that even if you use Application Pools and NETWORK SERVICE as user, shutdown functions will run under the default IIS user.
dweingart at pobox dot com 13-Mar-2006 05:53
I have discovered a change in behavior from PHP 5.0.4 to PHP 5.1.2 when using a shutdown function in conjunction with an output buffering callback.

In PHP 5.0.4 (and earlier versions I believe) the shutdown function is called after the output buffering callback.

In PHP 5.1.2 (not sure when the change occurred) the shutdown function is called before the output buffering callback.

Test code:
<?php
function ob_callback($buf) {
    $buf .= '<li>' . __FUNCTION__ .'</li>';
    return $buf;
}

function shutdown_func() {
    echo '<li>' . __FUNCTION__ .'</li>';
}

ob_start('ob_callback');
register_shutdown_function('shutdown_func');
echo '<ol>';
?>

PHP 5.0.4:

1. ob_callback
2. shutdown_func

PHP 5.1.2:

1. shutdown_func
2. ob_callback
me at thomaskeller dot biz 11-Mar-2006 11:27
Well, it might be obvious, but one should remember that one cannot send any HTTP header in the shutdown callback.

Something like

<?php

function redirect()
{
     header("Location: myuri.php");
}

register_shutdown_function("redirect");

// do something useful here

?>

doesn't work and PHP sends out the popular "headers already sent" warning.

I tried to set a redirection target somewhere in the script, but wanted to make sure that it was only set/executed at the very end of the script, since my custom redirect function also cleaned any output buffers at that point. Well, no luck here =)
kenneth dot kalmer at gmail dot com 27-Jan-2006 06:26
I performed two tests on the register_shutdown_function() to see under what conditions it was called, and if a can call a static method from a class. Here are the results:

<?php
/**
 * Tests the shutdown function being able to call a static methods
 */
class Shutdown
{
    public static function Method ($mixed = 0)
    {
        // we need absolute
        $ap = dirname (__FILE__);
        $mixed = time () . " - $mixed\n";
        file_put_contents ("$ap/shutdown.log", $mixed, FILE_APPEND);
    }
}
// 3. Throw an exception
register_shutdown_function (array ('Shutdown', 'Method'), 'throw');
throw new Exception ('bla bla');

// 2. Use the exit command
//register_shutdown_function (array ('Shutdown', 'Method'), 'exit');
//exit ('exiting here...')

// 1. Exit normally
//register_shutdown_function (array ('Shutdown', 'Method'));
?>

To test simply leave one of the three test lines uncommented and execute. Executing bottom-up yielded:

1138382480 - 0
1138382503 - exit
1138382564 - throw

HTH
sezer yalcin 27-Dec-2005 06:11
re:codeslinger at compsalot dot com

fork() is actually creating 2 processes from one. So there is no surprise register_shutdown_function will be executed per each process.

I think you have reported this as a bug and now in php 5.1.0 when you exit from child process, it does not execute it. Well, what are you going to do if you have something to register for forked process?

I hope php will be more stable near soon!

codeslinger at compsalot dot com
03-Feb-2005 09:22
Here is a nice little surprise to keep in mind...

If you register a shutdown function for your main program.  And then you fork() a child.

Guess What?
When the child exits it will run the code that was intended for the main program.  This can be a really bad thing  ;-)

Happily there is a simple work-around.  All you need to do is to create a global variable such as:

$IamaChild = [TRUE | FALSE];

and have your shutdown function check the value...
http://livejournal.com/~sinde1/ 02-Dec-2005 01:49
If you want to do something with files in function, that registered in register_shutdown_function(), use ABSOLUTE paths to files instead of relative. Because when script processing is complete current working directory chages to ServerRoot (see httpd.conf)
Niels Ganser <php dot comment at depoll dot de> 23-Sep-2005 04:58
Just a quick note: from 5.0.5 on objects will be unloaded _before_ your shutdown function is called which means you can't use previously initiated objects (such as mysqli).

See bug 33772 for more information.
codeslinger at compsalot dot com 03-Feb-2005 06:22
Here is a nice little surprise to keep in mind...

If you register a shutdown function for your main program.  And then you fork() a child.

Guess What?
When the child exits it will run the code that was intended for the main program.  This can be a really bad thing  ;-)

Happily there is a simple work-around.  All you need to do is to create a global variable such as:

$IamaChild = [TRUE | FALSE];

and have your shutdown function check the value...
php at kevin dot offwhite dot net 28-May-2004 11:14
Various commenters have described how this function doesn't work for time-consuming background processing, because the webpage doesn't close the connection.  The workaround using output buffering and header('Connection: close'); seems to work, but the solution I prefer is to put the background processing in a separate script which gets included as a transparent gif.

<?php

  //logger.php -- does the time-consuming log_it() function

  function log_it()
  {
    sleep(12); //simulate long processing
    error_log('log_it() completed');
  }

  error_reporting(0);
  register_shutdown_function('log_it');
  header('Cache-Control: no-cache');
  header('Content-type: image/gif');

  //the size of the unencoded transparent gif
  header('Content-length: 85');

  //1x1 pixel transparent gif
  print base64_decode('R0lGODlhAQABALMAAAAAAIAAAACAA'.
                       'ICAAAAAgIAAgACAgMDAwICAgP8AAA'.
                       'D/AP//AAAA//8A/wD//wBiZCH5BAE'.
                       'AAA8ALAAAAAABAAEAAAQC8EUAOw==');

  flush();
  exit;

?>

@-----------Page you want to add background processing to ---------@
<html>
<body>
<h1>Hello World</h1>

<!-- Call logger.php to do background processing -->
<img src="logger.php" width="1" height="1" border="0" alt="" />

</body>
</html>
@------------------------------------------------------------------@

The only caveat is, of course, if the user has images turned off then the script won't be called.  Tested successfully with IE6, Firefox 0.8, and Opera 7.11
astrolox at lawyersonline dot co dot uk 18-Mar-2004 08:57
When using the register_shutdown_function command in php 4. The registered functions are called in the order that you register them.

This is important to note if you are doing database work using classes that register shutdown functions for themselves.

You must register the shutdown_functions in the order that you want things to shutdown. ( ie the database needs to shutdown last )

Example of what will not work but what you might expect to work :

<?php
class database {

        function database() {
                echo "connect to sql server -- (database :: constructor)<br>\n";
                register_shutdown_function( array( &$this, "phpshutdown" ) );
                $this->connected = 1;
        }

        function do_sql( $sql ) {
                if ( $this->connected == 1 ) {
                        echo "performing sql -- (database :: do_sql)<br>\n";
                } else {
                        echo " ERROR -- CAN NOT PERFORM SQL -- NOT CONNECTED TO SERVER -- (database :: do_sql)<br>\n";
                }
        }

        function phpshutdown() {
                echo "close connection to sql server -- <b>(database :: shutdown)</b><br>\n";
                $this->connected = 0;
        }
}

class table {

        function table( &$database, $name ) {
                $this->database =& $database;
                $this->name = $name;
                echo "read table data using database class -- name=$this->name -- (table :: constructor)<br>\n";
                register_shutdown_function( array( &$this, "phpshutdown" ) );
                $this->database->do_sql( "read table data" );
        }

        function phpshutdown() {
                echo "save changes to database -- name=$this->name -- <b>(table :: shutdown)</b><br>\n";
                $this->database->do_sql( "save table data" );
        }
}

$db =& new database();

$shoppingcard =& new table( &$db, "cart " );
?>

Output of the above example is :-

connect to sql server -- (database :: constructor)
read table data using database class -- name=cart -- (table :: constructor)
performing sql -- (database :: do_sql)
close connection to sql server -- (database :: shutdown)
save changes to database -- name=cart -- (table :: shutdown)
ERROR -- CAN NOT PERFORM SQL -- NOT CONNECTED TO SERVER -- (database :: do_sql)
Jim Smith 16-Mar-2004 01:17
I was trying to figure out how to pass parameters to the register_shutdown_function() since you cannot register a function with parameters and passing through globals is not appropriate. E.g. what I was trying to do was  <? register_shutdown_function("anotherfunction('parameter')") ?>
Turns out, the trick is to use create_function() to create a "function" that calls the desired function with static parameters.

<?php
$funtext="mail('u@ho.com','mail test','sent after shutdown');";
register_shutdown_function(create_function('',$funtext));
?>

Here's another example showing in-line logging and a post-execution version:

Before: in-process logging

<?php
function logit($message) {
   $oF=fopen('TEST.log', 'a');
   fwrite($oF,"$message\n");
   fclose($oF);
   sleep(5);  // so you can see the delay
}
print "loging";
logit("traditional execution");
print "logged";
exit;
?>
After:

<?php
function logit($message) {
   $forlater=create_function('',"loglater('$message');");
   register_shutdown_function($forlater);
}
function loglater($message) {
   $oF=fopen('TEST.log', 'a');
   fwrite($oF,"$message\n");
   fclose($oF);
   sleep(5);  // so you can see the delay
}
print "loging";
logit("delayed execution");
print "logged";
exit;
?>

In the 'before' example, the file is written (and the delay occurs) before the "logged" appears. In the 'after' example, the file is written after execution terminates.

Maybe it would be nice to add a parameter to the register_shutdown_function that does this automatically?
sts at mail dot xubion dot hu 15-Mar-2004 11:56
If you need the old (<4.1) behavior of register_shutdown_function you can achieve the same with "Connection: close" and "Content-Length: xxxx" headers if you know the exact size of the sent data (which can be easily caught with output buffering).
An example:
<?php
header("Connection: close");
ob_start();
phpinfo();
$size=ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
sleep(13);
error_log("do something in the background");
?>

The same will work with registered functions.
According to http spec, browsers should close the connection when they got the amount of data specified in Content-Length header. At least it works fine for me in IE6 and Opera7.
James Wyse (James [at] tastyspoon.com) 12-Feb-2004 05:49
Just a note to say that if your function uses paths relative to the script you are running (ie. for reading files, "../dir/" etc) then your function may not work correctly when registered as a shutdown function. I'm not sure where the 'working dir' IS when running a shutdown function but I find it best to use absolute paths ("/home/www/dir/").
www dot php dot net at spam dot monnard dot org 02-Dec-2003 07:26
Response to => vbwebprofi at gmx dot de 22-Nov-2003 03:12

You have to do a changement on the object to see the difference between = and =&

Here is your code modified to see the difference.
The modification of the code are essentially the var $Value_changed and the function change().
I have tested it under RedHat 9 with PHP 4.2.2

--8<--- cls_TEST.php --------------------------------------------->8--
<?php
class TEST {
    var $Value;
    var $Value_changed;

    // Constructor

    function TEST($in_Value = 0){
        // Register destructor
        register_shutdown_function(array(&$this, '_TEST'));

        $this->Value = $in_Value;
        $this->Value_changed = $in_Value;
        $this->_Log('TEST');
    }

    // Destructor
    function _TEST() {
        $this->_Log('_TEST');
    }

    // Private function
    function _Log($in_Msg) {
        $oF = fopen('/tmp/TEST.log', 'a');
        fwrite($oF, sprintf("%s : %-5s = %d ; %d\n",
                            date('Y-m-d H:i:s', time()),
                            $in_Msg,
                            $this->Value,
                            $this->Value_changed));
        fclose($oF);
    }

    // Function change()
    function change() {
        $this->Value_changed++;
        $this->_Log('change');
    }
}
?>
--8<--- test_TEST.php -------------------------------------------->8--
<?php
    // include class TEST
    include_once('cls_TEST.php');

    echo "New...<br>\n";
    $o1 =  new TEST(1);
    $o2 =& new TEST(2);
    # Strange declaration :
    $o3 &= new TEST(3);

    echo "Sleep...<br>\n";
    sleep(5);

    echo "Change...<br>\n";
    $o1->change();
    $o2->change();
    # The following line doesn't work !
    #$o3->change();

    echo "Sleep...<br>\n";
    sleep(5);

    echo "End.";
?>
--8<-------------------------------------------------------------->8--

It should produce a content like this in the file, specified in the _Log function :

2003-12-02 04:22:55 : TEST  = 1 ; 1
2003-12-02 04:22:55 : TEST  = 2 ; 2
2003-12-02 04:22:55 : TEST  = 3 ; 3
2003-12-02 04:23:01 : change = 1 ; 2
2003-12-02 04:23:01 : change = 2 ; 3
2003-12-02 04:23:06 : _TEST = 1 ; 1
2003-12-02 04:23:06 : _TEST = 2 ; 3
2003-12-02 04:23:06 : _TEST = 3 ; 3
gabe at websaviour dot com 21-Nov-2003 09:09
It should be noted that register_shutdown_function() does not guarantee that the connection will be closed before executing the function.  In particular on Apache 1.3.27 w/ PHP 4.3.0 on OS X as well as Apache 1.3.22 w/ PHP 4.1.2 on Red Hat I have discovered that Apache does not close the connection until after the registered shutdown function has completed execution.  I am not sure what the circumstances that cause this are, but there is a bug at http://bugs.php.net/bug.php?id=20447

This is important, because as far as I can tell, the documented functionality of register_shutdown_function() is the *ONLY* way to achieve anything resembling asynchronous processing with PHP in a web server environment.  In my mind this is a severe limitation, because if you have some processing intensive task that needs to be done and you want to do it in PHP, there is no way to have it occur without an accompanying wait in a browser window.  Sure you can use ignore_user_abort() to ensure that processing doesn't get cancelled by an impatient user (or impatient browser for that matter), but usability is still compromised.
phpmanual at NO_SPHAMnetebb dot com 16-Nov-2003 05:31
Given this code:

<?php
class CallbackClass {
    function CallbackFunction() {
        // refers to $this
    }

    function StaticFunction() {
        // doesn't refer to $this
    }
}

function NonClassFunction() {
}
?>

there appear to be 3 ways to set a callback function in PHP (using register_shutdown_function() as an example):

1: register_shutdown_function('NonClassFunction');

2: register_shutdown_function(array('CallbackClass', 'StaticFunction'));

3: $o =& new CallbackClass();
   register_shutdown_function(array($o, 'CallbackFunction'));

The following may also prove useful:

<?php
class CallbackClass {
    function CallbackClass() {
        register_shutdown_function(array(&$this, 'CallbackFunction')); // the & is important
    }

    function CallbackFunction() {
        // refers to $this
    }
}
?>
jules at sitepointAASASZZ dot com 01-Jul-2003 09:41
If your script exceeds the maximum execution time, and terminates thusly:

Fatal error: Maximum execution time of 20 seconds exceeded in - on line 12

The registered shutdown functions will still be executed.

I figured it was important that this be made clear!
php at spandex dot deleteme dot nildram dot co dot uk 28-Apr-2003 10:43
I've had immense trouble getting any of the examples of emulated destructors to work.  They always seemed to have a copy of the object just after initialisation.  Many people mention that the register_shutdown_function will take a copy of the object rather than a reference... and this can be cured with an ampersand.  If you look in the PEAR docs for the way they emulate destructors you'll find that you also need one before the "new" statement when you create an instance of your object.  There's an editors note above that mentions this too... but I thought I'd collect it all here in one example that really works.  Honest... I'm using it (PHP 4.3.1):

<?php
class Object {
    var $somevar = "foo";

    function Object() {
        $somevar = "bar";
        register_shutdown_function(array(&$this, 'MyDestructor'));
    }

    function MyDestructor() {
        # Do useful destructor stuff here...
    }
}

# Now create the object as follows and then 'MyDestructor'
# will be called on shutdown and will be able to operate on
# the object as it ended up... not as it started!
$my_object =& new Object;
?>
kwazy at php dot net 29-Jan-2003 04:53
One more note to add about register_shutdown_function and objects.

It is possible to self-register the object from the constructor, but even using the syntax:
register_shutdown_function(array(&$this,'shutdown'))

will only result in the function being called with the object in the "just initialized" state, any changes made to the object after construction will not be there when the script actually exits.

But if you use:

<?php
$obj = new object();
register_shutdown_function(array(&$obj,'shutdown'));
?>

the object method 'shutdown' will be called with the current state of the object intact.

[Editor's note: The explanation for this behavior is really simple.
"$obj = new object()" creates a copy of the object which you can refer with $this in the object's constructor.
To solve this "problem" you should use "$obj = &new object()" which assign the reference to the current object.]
24-Oct-2002 06:35
When using CLI ( and perhaps command line without CLI - I didn't test it) the shutdown function doesn't get called if the process gets a SIGINT or SIGTERM. only the natural exit of PHP calls the shutdown function.
To overcome the problem compile the command line interpreter with --enable-pcntl and add this code:

<?php
function sigint()
{
    exit;
}
pcntl_signal(SIGINT, 'sigint');
pcntl_signal(SIGTERM, 'sigint');
?>

This way when the process recieves one of those signals, it quits normaly, and the shutdown function gets called.
Note: using the pcntl function in web server envoirment is considered problematic, so if you are writing a script that runs both from the command line and from the server, you should put some conditional code around that block that identifies wheater this is a command line envoirment or not.
adam at saki dot com dot au 20-Aug-2002 12:38
When using objects the syntax register_shutdown_function(array($object, 'function')) will take a copy of the object at the time of the call.  This means you cannot do this in the constructor and have it correctly destruct objects.  The alternative is to use register_shutdown_function(array(&$object, 'function')) where the ampersand passes the reference and not the copy.  This appears to work fine.
priebe at mi-corporation dot com 14-Mar-2002 05:25
Note that register_shutdown_function() does not work under Apache on Windows platforms.  Your shutdown function will be called, but the connection will not close until the processing is complete.  Zend tells me that this is due to a difference between Apache for *nix and Apache for Windows.

I'm seeing similar behavior under IIS (using php4isapi).


Register a function for execution on each tick

  • ≪ register_shutdown_function
  • unregister_tick_function ≫
  • PHP Manual
  • Function handling 함수 목록
  • Register a function for execution on each tick

register_tick_function

(PHP 4 >= 4.0.3, PHP 5, PHP 7)

register_tick_function — Register a function for execution on each tick

설명

bool register_tick_function ( callable $function [, mixed $arg [, mixed $... ]] )

Registers the given function to be executed when a tick is called.

인수

function

The function name as a string, or an array consisting of an object and a method.

arg

...

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 register_tick_function() example

<?php
declare(ticks=1);

// using a function as the callback
register_tick_function('my_function', true);

// using an object->method
$object = new my_class();
register_tick_function(array(&$object, 'my_method'), true);
?>

변경점

버전 | 설명
5.3.0 | Ticks are now supported on threaded web server modules.

주의

Warning

register_tick_function() should not be used with threaded web server modules with PHP 5.2 or lower.

참고

  • declare
  • unregister_tick_function() - De-register a function for execution on each tick

User Contributed Notes

Ahmad Samiei 31-Aug-2013 12:41
put in in your index to find where your code exited. find die o exit

function shutdown_find_exit()
{
var_dump($GLOBALS['dbg_stack']);
}
register_shutdown_function('shutdown_find_exit');
function write_dbg_stack()
{
$GLOBALS['dbg_stack'] = debug_backtrace();
}
register_tick_function('write_dbg_stack');
declare(ticks=1);
rob dot eyre at gmail dot com 22-Jul-2011 02:19
It looks like register_tick_function() can accept an anonymous function, but unregister_tick_function() cannot
Jeremy 21-Jun-2007 08:03
I tried the following on an IIS 6/PHP 5 machine:

<?php

    set_time_limit(0);

    function profiler($return = false)
    {
        static $m = 0;
        if($return) return $m . " bytes";
        if(($mem = memory_get_usage()) > $m) $m = $mem;
    }

    register_tick_function('profiler');
    declare(ticks = 1);

    $numbers = array();
    for($i=0; $i<1000000; $i++)
    {
        print($i . "<br />");
    }

    print(profiler(true));

?>

...and got the following errors:

1) PHP has encountered an Unhandled Exception Code -1073741674 at 0435EC5A
2) PHP has encountered an Access Violation at 02727891
3) PHP has encountered an Access Violation at 02727879
4) PHP has encountered an Access Violation at 00000000

So far the code hasn't crashed the web server, but like the warning says, "register_tick_function() should not be used with threaded web server modules. Ticks are not working in ZTS mode and may crash your web server."

Strangely enough, the code does execute successfully every once in a while.


De-register a function for execution on each tick

  • ≪ register_tick_function
  • Quickhash ≫
  • PHP Manual
  • Function handling 함수 목록
  • De-register a function for execution on each tick

unregister_tick_function

(PHP 4 >= 4.0.3, PHP 5, PHP 7)

unregister_tick_function — De-register a function for execution on each tick

설명

void unregister_tick_function ( string $function_name )

De-registers the function named by function_name so it is no longer executed when a tick is called.

인수

function_name

The function name, as a string.

반환값

값을 반환하지 않습니다.

참고

  • register_tick_function() - Register a function for execution on each tick

User Contributed Notes

ElectricHeart at ya dot ru 05-May-2016 06:14
You can unregister closure-function:

declare(ticks = 1000);
$startTime = microtime(true);
$tick = true;
$closure = function () use ($startTime, &$tick) {
    if (((microtime(true) - $startTime) > 5) && $tick) {
        $tick = false;
        throw new \Exception('Time to run code left');
    }
};

try {
            register_tick_function($closure);
            //do your code
            $result = 1;
            return $result;
        } catch (\Exception $e) {
            throw $e;
        } finally {
            unregister_tick_function($closure);
}
Greg 27-Jul-2012 09:02
It's not so clear, but, at least as of PHP 5.3.13, you cannot use this inside of the handler itself as it will throw an error:

<?php

declare(ticks=2);

function tick_handler()
{
    unregister_tick_function('tick_handler');
}

register_tick_function('tick_handler');

set_time_limit(0);
usleep(500000);

?>

results in:

warning: unregister_tick_function(): Unable to delete tick function executed at the moment in [filename]

So if you want to unregister it must be done outside of the handler.
rob dot eyre at gmail dot com 22-Jul-2011 02:22
Note that unregister_tick_function() can also accept a callback in the form of an array (either static, like array($className, $methodName) or instance, like array(&$this, $methodName)).

It cannot accept an anonymous function, however.


Quickhash

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
  • 예약 상수
  • 예제
  • QuickHashIntSet — The QuickHashIntSet class
    • QuickHashIntSet::add — This method adds a new entry to the set
    • QuickHashIntSet::__construct — Creates a new QuickHashIntSet object
    • QuickHashIntSet::delete — This method deletes an entry from the set
    • QuickHashIntSet::exists — This method checks whether a key is part of the set
    • QuickHashIntSet::getSize — Returns the number of elements in the set
    • QuickHashIntSet::loadFromFile — This factory method creates a set from a file
    • QuickHashIntSet::loadFromString — This factory method creates a set from a string
    • QuickHashIntSet::saveToFile — This method stores an in-memory set to disk
    • QuickHashIntSet::saveToString — This method returns a serialized version of the set
  • QuickHashIntHash — The QuickHashIntHash class
    • QuickHashIntHash::add — This method adds a new entry to the hash
    • QuickHashIntHash::__construct — Creates a new QuickHashIntHash object
    • QuickHashIntHash::delete — This method deletes am entry from the hash
    • QuickHashIntHash::exists — This method checks whether a key is part of the hash
    • QuickHashIntHash::get — This method retrieves a value from the hash by its key
    • QuickHashIntHash::getSize — Returns the number of elements in the hash
    • QuickHashIntHash::loadFromFile — This factory method creates a hash from a file
    • QuickHashIntHash::loadFromString — This factory method creates a hash from a string
    • QuickHashIntHash::saveToFile — This method stores an in-memory hash to disk
    • QuickHashIntHash::saveToString — This method returns a serialized version of the hash
    • QuickHashIntHash::set — This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist
    • QuickHashIntHash::update — This method updates an entry in the hash with a new value
  • QuickHashStringIntHash — The QuickHashStringIntHash class
    • QuickHashStringIntHash::add — This method adds a new entry to the hash
    • QuickHashStringIntHash::__construct — Creates a new QuickHashStringIntHash object
    • QuickHashStringIntHash::delete — This method deletes am entry from the hash
    • QuickHashStringIntHash::exists — This method checks whether a key is part of the hash
    • QuickHashStringIntHash::get — This method retrieves a value from the hash by its key
    • QuickHashStringIntHash::getSize — Returns the number of elements in the hash
    • QuickHashStringIntHash::loadFromFile — This factory method creates a hash from a file
    • QuickHashStringIntHash::loadFromString — This factory method creates a hash from a string
    • QuickHashStringIntHash::saveToFile — This method stores an in-memory hash to disk
    • QuickHashStringIntHash::saveToString — This method returns a serialized version of the hash
    • QuickHashStringIntHash::set — This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist
    • QuickHashStringIntHash::update — This method updates an entry in the hash with a new value
  • QuickHashIntStringHash — The QuickHashIntStringHash class
    • QuickHashIntStringHash::add — This method adds a new entry to the hash
    • QuickHashIntStringHash::__construct — Creates a new QuickHashIntStringHash object
    • QuickHashIntStringHash::delete — This method deletes am entry from the hash
    • QuickHashIntStringHash::exists — This method checks whether a key is part of the hash
    • QuickHashIntStringHash::get — This method retrieves a value from the hash by its key
    • QuickHashIntStringHash::getSize — Returns the number of elements in the hash
    • QuickHashIntStringHash::loadFromFile — This factory method creates a hash from a file
    • QuickHashIntStringHash::loadFromString — This factory method creates a hash from a string
    • QuickHashIntStringHash::saveToFile — This method stores an in-memory hash to disk
    • QuickHashIntStringHash::saveToString — This method returns a serialized version of the hash
    • QuickHashIntStringHash::set — This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist
    • QuickHashIntStringHash::update — This method updates an entry in the hash with a new value

User Contributed Notes

There are no user contributed notes for this page.


소개

The quickhash extension contains a set of specific strongly-typed classes to deal with specific set and hash implementations.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

This extension requires no additional libraries.

User Contributed Notes

There are no user contributed notes for this page.


설치

PECL 확장 설치에 관한 정보는 매뉴얼의 PECL 확장 설치에 있습니다. 새 릴리즈, 내려받기, 소스파일, 개발자 정보, CHANGELOG 등의 추가 정보는 여기에 있습니다: » http://pecl.php.net/package/quickhash

User Contributed Notes

There are no user contributed notes for this page.


예제

Example #1 Quickhash Example

<?php
$set = new QuickHashIntSet( 1024, QuickHashIntSet::CHECK_FOR_DUPES );
$set->add( 1 );
$set->add( 3 );

var_dump( $set->exists( 3 ) );
var_dump( $set->exists( 4 ) );

$set->saveToFile( "/tmp/test-set.set" );

$newSet = QuickHashIntSet::loadFromFile(
    "/tmp/test-set.set"
);

var_dump( $newSet->exists( 3 ) );
var_dump( $newSet->exists( 4 ) );
?>

위 예제의 출력 예시:

bool(true)
bool(false)
bool(true)
bool(false)

Example #2 Quickhash ArrayAccess Example

<?php
$hash = new QuickHashIntHash( 64 );

// Adding and updating hash entries.
$hash[3] = 145926;
$hash[3] = 1415926;
$hash[2] = 72;

// Checking if keys exist
var_dump( isset( $hash[3] ) );

// Removing hash entries
unset( $hash[2] );

// Retrieving the value stored for a hash
echo $hash[3], "\n";
?>

위 예제의 출력 예시:

bool(true)
1415926

Example #3 Quickhash Iterator Example

<?php
$hash = new QuickHashIntHash( 64 );

// Adding hash entries.
$hash[1] = 145926;
$hash[2] = 1415926;
$hash[3] = 72;
$hash[4] = 712314;
$hash[5] = -4234;

foreach( $hash as $key => $value )
{
    echo $key, ' => ', $value, "\n";
}
?>

위 예제의 출력 예시:

5 => -4234
4 => 712314
1 => 145926
2 => 1415926
3 => 72

Example #4 Quickhash String Values Example

<?php
$hash = new QuickHashIntStringHash( 64 );

// Adding hash entries.
$hash[1] = "one million four hundred fifteen thousand nine hundred twenty six";
$hash->add( 2, "one more" );

foreach( $hash as $key => $value )
{
    echo $key, ' => ', $value, "\n";
}
?>

위 예제의 출력 예시:

1 => one million four hundred fifteen thousand nine hundred twenty six
2 => one more

User Contributed Notes

There are no user contributed notes for this page.


The QuickHashIntSet class

(PECL quickhash >= Unknown)

소개

This class wraps around a set containing integer numbers.

Sets can also be iterated over with foreach as the Iterator interface is implemented as well. The order of which elements are returned in is not guaranteed.

클래스 개요

QuickHashIntSet {
/* Constants */
const integer CHECK_FOR_DUPES = 1 ;
const integer DO_NOT_USE_ZEND_ALLOC = 2 ;
const integer HASHER_NO_HASH = 256 ;
const integer HASHER_JENKINS1 = 512 ;
const integer HASHER_JENKINS2 = 1024 ;
/* Methods */
public bool add ( int $key )
public __construct ( int $size [, int $options ] )
public bool delete ( int $key )
public bool exists ( int $key )
publicint getSize ( void )
public static QuickHashIntSet loadFromFile ( string $filename [, int $size [, int $options ]] )
public static QuickHashIntSet loadFromString ( string $contents [, int $size [, int $options ]] )
public void saveToFile ( string $filename )
public string saveToString ( void )
}

예약 상수

QuickHashIntSet::CHECK_FOR_DUPES

If enabled, adding duplicate elements to a set (through either add() or loadFromFile()) will result in those elements to be dropped from the set. This will take up extra time, so only used when it is required.

QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC

Disables the use of PHP's internal memory manager for internal set structures. With this option enabled, internal allocations will not count towards the memory_limit settings.

QuickHashIntSet::HASHER_NO_HASH

Selects to not use a hashing function, but merely use a modulo to find the bucket list index. This is not faster than normal hashing, and gives more collisions.

QuickHashIntSet::HASHER_JENKINS1

This is the default hashing function to turn the integer hashes into bucket list indexes.

QuickHashIntSet::HASHER_JENKINS2

Selects a variant hashing algorithm.

Table of Contents

  • QuickHashIntSet::add — This method adds a new entry to the set
  • QuickHashIntSet::__construct — Creates a new QuickHashIntSet object
  • QuickHashIntSet::delete — This method deletes an entry from the set
  • QuickHashIntSet::exists — This method checks whether a key is part of the set
  • QuickHashIntSet::getSize — Returns the number of elements in the set
  • QuickHashIntSet::loadFromFile — This factory method creates a set from a file
  • QuickHashIntSet::loadFromString — This factory method creates a set from a string
  • QuickHashIntSet::saveToFile — This method stores an in-memory set to disk
  • QuickHashIntSet::saveToString — This method returns a serialized version of the set

User Contributed Notes

There are no user contributed notes for this page.


This method adds a new entry to the set

  • ≪ QuickHashIntSet
  • QuickHashIntSet::__construct ≫
  • PHP Manual
  • QuickHashIntSet
  • This method adds a new entry to the set

QuickHashIntSet::add

(PECL quickhash >= Unknown)

QuickHashIntSet::add — This method adds a new entry to the set

설명

public bool QuickHashIntSet::add ( int $key )

This method adds a new entry to the set, and returns whether the entry was added. Entries are by default always added unless QuickHashIntSet::CHECK_FOR_DUPES has been passed when the set was created.

인수

key

The key of the entry to add.

반환값

TRUE when the entry was added, and FALSE if the entry was not added.

예제

Example #1 QuickHashIntSet::add() example

<?php
echo "without dupe checking\n";
$set = new QuickHashIntSet( 1024 );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );

echo "\nwith dupe checking\n";
$set = new QuickHashIntSet( 1024, QuickHashIntSet::CHECK_FOR_DUPES );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
?>

위 예제의 출력 예시:

without dupe checking
bool(false)
bool(true)
bool(true)
bool(true)

with dupe checking
bool(false)
bool(true)
bool(true)
bool(false)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new QuickHashIntSet object

  • ≪ QuickHashIntSet::add
  • QuickHashIntSet::delete ≫
  • PHP Manual
  • QuickHashIntSet
  • Creates a new QuickHashIntSet object

QuickHashIntSet::__construct

(PECL quickhash >= Unknown)

QuickHashIntSet::__construct — Creates a new QuickHashIntSet object

설명

public QuickHashIntSet::__construct ( int $size [, int $options ] )

This constructor creates a new QuickHashIntSet. The size is the amount of bucket lists to create. The more lists there are, the less collisions you will have. Options are also supported.

인수

size

The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options

The options that you can pass in are: QuickHashIntSet::CHECK_FOR_DUPES, which makes sure no duplicate entries are added to the set; QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC to not use PHP's internal memory manager as well as one of QuickHashIntSet::HASHER_NO_HASH, QuickHashIntSet::HASHER_JENKINS1 or QuickHashIntSet::HASHER_JENKINS2. These last three configure which hashing algorithm to use. All options can be combined using bitmasks.

반환값

Returns a new QuickHashIntSet object.

예제

Example #1 QuickHashIntSet::__construct() example

<?php
var_dump( new QuickHashIntSet( 1024 ) );
var_dump( new QuickHashIntSet( 1024, QuickHashIntSet::CHECK_FOR_DUPES ) );
var_dump(
    new QuickHashIntSet(
        1024,
        QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC | QuickHashIntSet::HASHER_JENKINS2
    )
);
?>

User Contributed Notes

There are no user contributed notes for this page.


This method deletes an entry from the set

  • ≪ QuickHashIntSet::__construct
  • QuickHashIntSet::exists ≫
  • PHP Manual
  • QuickHashIntSet
  • This method deletes an entry from the set

QuickHashIntSet::delete

(PECL quickhash >= Unknown)

QuickHashIntSet::delete — This method deletes an entry from the set

설명

public bool QuickHashIntSet::delete ( int $key )

This method deletes an entry from the set, and returns whether the entry was deleted or not. Associated memory structures will not be freed immediately, but rather when the set itself is freed.

인수

key

The key of the entry to delete.

반환값

TRUE when the entry was deleted, and FALSE if the entry was not deleted.

예제

Example #1 QuickHashIntSet::delete() example

<?php
$set = new QuickHashIntSet( 1024 );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
var_dump( $set->delete( 4 ) );
var_dump( $set->exists( 4 ) );
var_dump( $set->delete( 4 ) );
?>

위 예제의 출력 예시:

bool(false)
bool(true)
bool(true)
bool(false)
bool(false)

User Contributed Notes

There are no user contributed notes for this page.


This method checks whether a key is part of the set

  • ≪ QuickHashIntSet::delete
  • QuickHashIntSet::getSize ≫
  • PHP Manual
  • QuickHashIntSet
  • This method checks whether a key is part of the set

QuickHashIntSet::exists

(PECL quickhash >= Unknown)

QuickHashIntSet::exists — This method checks whether a key is part of the set

설명

public bool QuickHashIntSet::exists ( int $key )

This method checks whether an entry with the provided key exists in the set.

인수

key

The key of the entry to check for whether it exists in the set.

반환값

Returns TRUE when the entry was found, or FALSE when the entry is not found.

예제

Example #1 QuickHashIntSet::exists() example

<?php
//generate 200000 elements
$array = range( 0, 199999 );
$existingEntries = array_rand( array_flip( $array ), 180000 );
$testForEntries = array_rand( array_flip( $array ), 1000 );
$foundCount = 0;

echo "Creating set: ", microtime( true ), "\n";
$set = new QuickHashIntSet( 100000 );
echo "Adding elements: ", microtime( true ), "\n";
foreach( $existingEntries as $key )
{
     $set->add( $key );
}

echo "Doing 1000 tests: ", microtime( true ), "\n";
foreach( $testForEntries as $key )
{
     $foundCount += $set->exists( $key );
}
echo "Done, $foundCount found: ", microtime( true ), "\n";
?>

위 예제의 출력 예시:

Creating set: 1263588703.0748
Adding elements: 1263588703.0757
Doing 1000 tests: 1263588703.7851
Done, 898 found: 1263588703.7897

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of elements in the set

  • ≪ QuickHashIntSet::exists
  • QuickHashIntSet::loadFromFile ≫
  • PHP Manual
  • QuickHashIntSet
  • Returns the number of elements in the set

QuickHashIntSet::getSize

(PECL quickhash >= Unknown)

QuickHashIntSet::getSize — Returns the number of elements in the set

설명

publicint QuickHashIntSet::getSize ( void )

Returns the number of elements in the set.

인수

key

The key of the entry to add.

반환값

The number of elements in the set.

예제

Example #1 QuickHashIntSet::getSize() example

<?php
$set = new QuickHashIntSet( 8 );
var_dump( $set->add( 2 ) );
var_dump( $set->add( 3 ) );
var_dump( $set->getSize() );
?>

위 예제의 출력 예시:

bool(true)
bool(true)
int(2)

User Contributed Notes

There are no user contributed notes for this page.


This factory method creates a set from a file

  • ≪ QuickHashIntSet::getSize
  • QuickHashIntSet::loadFromString ≫
  • PHP Manual
  • QuickHashIntSet
  • This factory method creates a set from a file

QuickHashIntSet::loadFromFile

(PECL quickhash >= Unknown)

QuickHashIntSet::loadFromFile — This factory method creates a set from a file

설명

public static QuickHashIntSet QuickHashIntSet::loadFromFile ( string $filename [, int $size [, int $options ]] )

This factory method creates a new set from a definition file on disk. The file format consists of 32 bit signed integers packed together in the Endianness that the system that the code runs on uses.

인수

filename

The filename of the file to read the set from.

size

The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options

The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the set, rounded up to the nearest power of two with a maximum limit of 4194304.

반환값

Returns a new QuickHashIntSet.

예제

Example #1 QuickHashIntSet::loadFromFile() example

<?php
$file = dirname( __FILE__ ) . "/simple.set";
$set = QuickHashIntSet::loadFromFile(
    $file,
    QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key,
        $set->exists( $key ) ? 'set' : 'unset'
    );
}
?>

위 예제의 출력 예시:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

User Contributed Notes

There are no user contributed notes for this page.


This factory method creates a set from a string

  • ≪ QuickHashIntSet::loadFromFile
  • QuickHashIntSet::saveToFile ≫
  • PHP Manual
  • QuickHashIntSet
  • This factory method creates a set from a string

QuickHashIntSet::loadFromString

(PECL quickhash >= Unknown)

QuickHashIntSet::loadFromString — This factory method creates a set from a string

설명

public static QuickHashIntSet QuickHashIntSet::loadFromString ( string $contents [, int $size [, int $options ]] )

This factory method creates a new set from a definition in a string. The file format consists of 32 bit signed integers packed together in the Endianness that the system that the code runs on uses.

인수

contents

The string containing a serialized format of the set.

size

The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options

The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the set, rounded up to the nearest power of two automatically limited from 64 to 4194304.

반환값

Returns a new QuickHashIntSet.

예제

Example #1 QuickHashIntSet::loadFromString() example

<?php
$contents = file_get_contents( dirname( __FILE__ ) . "/simple.set" );
$set = QuickHashIntSet::loadFromString(
    $contents,
    QuickHashIntSet::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key,
        $set->exists( $key ) ? 'set' : 'unset'
    );
}
?>

위 예제의 출력 예시:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

User Contributed Notes

There are no user contributed notes for this page.


This method stores an in-memory set to disk

  • ≪ QuickHashIntSet::loadFromString
  • QuickHashIntSet::saveToString ≫
  • PHP Manual
  • QuickHashIntSet
  • This method stores an in-memory set to disk

QuickHashIntSet::saveToFile

(PECL quickhash >= Unknown)

QuickHashIntSet::saveToFile — This method stores an in-memory set to disk

설명

public void QuickHashIntSet::saveToFile ( string $filename )

This method stores an existing set to a file on disk, in the same format that loadFromFile() can read.

인수

filename

The filename of the file to store the hash in.

반환값

값을 반환하지 않습니다.

예제

Example #1 QuickHashIntSet::saveToFile() example

<?php
$set = new QuickHashIntSet( 1024 );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );

$set->saveToFile( '/tmp/test.set' );
?>

User Contributed Notes

There are no user contributed notes for this page.


This method returns a serialized version of the set

  • ≪ QuickHashIntSet::saveToFile
  • QuickHashIntHash ≫
  • PHP Manual
  • QuickHashIntSet
  • This method returns a serialized version of the set

QuickHashIntSet::saveToString

(PECL quickhash >= Unknown)

QuickHashIntSet::saveToString — This method returns a serialized version of the set

설명

public string QuickHashIntSet::saveToString ( void )

This method returns a serialized version of the set in the same format that loadFromString() can read.

반환값

This method returns a string containing a serialized format of the set. Each element is stored as a four byte value in the Endianness that the current system uses.

예제

Example #1 QuickHashIntSet::saveToString() example

<?php
$set = new QuickHashIntSet( 1024 );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );
var_dump( $set->exists( 4 ) );
var_dump( $set->add( 4 ) );

var_dump( $set->saveToString() );
?>

User Contributed Notes

There are no user contributed notes for this page.


The QuickHashIntHash class

(PECL quickhash >= Unknown)

소개

This class wraps around a hash containing integer numbers, where the values are also integer numbers. Hashes are also available as implementation of the ArrayAccess interface.

Hashes can also be iterated over with foreach as the Iterator interface is implemented as well. The order of which elements are returned in is not guaranteed.

클래스 개요

QuickHashIntHash {
/* Constants */
const integer CHECK_FOR_DUPES = 1 ;
const integer DO_NOT_USE_ZEND_ALLOC = 2 ;
const integer HASHER_NO_HASH = 256 ;
const integer HASHER_JENKINS1 = 512 ;
const integer HASHER_JENKINS2 = 1024 ;
/* Methods */
public bool add ( int $key [, int $value ] )
public __construct ( int $size [, int $options ] )
public bool delete ( int $key )
public bool exists ( int $key )
public int get ( int $key )
public int getSize ( void )
public static QuickHashIntHash loadFromFile ( string $filename [, int $options ] )
public static QuickHashIntHash loadFromString ( string $contents [, int $options ] )
public void saveToFile ( string $filename )
public string saveToString ( void )
public bool set ( int $key , int $value )
public bool update ( int $key , int $value )
}

예약 상수

QuickHashIntHash::CHECK_FOR_DUPES

If enabled, adding duplicate elements to a set (through either add() or loadFromFile()) will result in those elements to be dropped from the set. This will take up extra time, so only used when it is required.

QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC

Disables the use of PHP's internal memory manager for internal set structures. With this option enabled, internal allocations will not count towards the memory_limit settings.

QuickHashIntHash::HASHER_NO_HASH

Selects to not use a hashing function, but merely use a modulo to find the bucket list index. This is not faster than normal hashing, and gives more collisions.

QuickHashIntHash::HASHER_JENKINS1

This is the default hashing function to turn the integer hashes into bucket list indexes.

QuickHashIntHash::HASHER_JENKINS2

Selects a variant hashing algorithm.

Table of Contents

  • QuickHashIntHash::add — This method adds a new entry to the hash
  • QuickHashIntHash::__construct — Creates a new QuickHashIntHash object
  • QuickHashIntHash::delete — This method deletes am entry from the hash
  • QuickHashIntHash::exists — This method checks whether a key is part of the hash
  • QuickHashIntHash::get — This method retrieves a value from the hash by its key
  • QuickHashIntHash::getSize — Returns the number of elements in the hash
  • QuickHashIntHash::loadFromFile — This factory method creates a hash from a file
  • QuickHashIntHash::loadFromString — This factory method creates a hash from a string
  • QuickHashIntHash::saveToFile — This method stores an in-memory hash to disk
  • QuickHashIntHash::saveToString — This method returns a serialized version of the hash
  • QuickHashIntHash::set — This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist
  • QuickHashIntHash::update — This method updates an entry in the hash with a new value

User Contributed Notes

There are no user contributed notes for this page.


This method adds a new entry to the hash

  • ≪ QuickHashIntHash
  • QuickHashIntHash::__construct ≫
  • PHP Manual
  • QuickHashIntHash
  • This method adds a new entry to the hash

QuickHashIntHash::add

(PECL quickhash >= Unknown)

QuickHashIntHash::add — This method adds a new entry to the hash

설명

public bool QuickHashIntHash::add ( int $key [, int $value ] )

This method adds a new entry to the hash, and returns whether the entry was added. Entries are by default always added unless QuickHashIntHash::CHECK_FOR_DUPES has been passed when the hash was created.

인수

key

The key of the entry to add.

value

The optional value of the entry to add. If no value is specified, 1 will be used.

반환값

TRUE when the entry was added, and FALSE if the entry was not added.

예제

Example #1 QuickHashIntHash::add() example

<?php
echo "without dupe checking\n";
$hash = new QuickHashIntHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, 22 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, 12 ) );

echo "\nwith dupe checking\n";
$hash = new QuickHashIntHash( 1024, QuickHashIntHash::CHECK_FOR_DUPES );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, 78 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, 9 ) );

echo "\ndefault value\n";
var_dump( $hash->add( 5 ) );
var_dump( $hash->get( 5 ) );
?>

위 예제의 출력 예시:

without dupe checking
bool(false)
bool(false)
bool(true)
bool(true)
int(22)
bool(true)

with dupe checking
bool(false)
bool(false)
bool(true)
bool(true)
int(78)
bool(false)

default value
bool(true)
int(1)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new QuickHashIntHash object

  • ≪ QuickHashIntHash::add
  • QuickHashIntHash::delete ≫
  • PHP Manual
  • QuickHashIntHash
  • Creates a new QuickHashIntHash object

QuickHashIntHash::__construct

(PECL quickhash >= Unknown)

QuickHashIntHash::__construct — Creates a new QuickHashIntHash object

설명

public QuickHashIntHash::__construct ( int $size [, int $options ] )

This constructor creates a new QuickHashIntHash. The size is the amount of bucket lists to create. The more lists there are, the less collisions you will have. Options are also supported.

인수

size

The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 64 to 4194304.

options

The options that you can pass in are: QuickHashIntHash::CHECK_FOR_DUPES, which makes sure no duplicate entries are added to the hash; QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC to not use PHP's internal memory manager as well as one of QuickHashIntHash::HASHER_NO_HASH, QuickHashIntHash::HASHER_JENKINS1 or QuickHashIntHash::HASHER_JENKINS2. These last three configure which hashing algorithm to use. All options can be combined using bitmasks.

반환값

Returns a new QuickHashIntHash object.

예제

Example #1 QuickHashIntHash::__construct() example

<?php
var_dump( new QuickHashIntHash( 1024 ) );
var_dump( new QuickHashIntHash( 1024, QuickHashIntHash::CHECK_FOR_DUPES ) );
var_dump(
    new QuickHashIntHash(
        1024,
        QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC | QuickHashIntHash::HASHER_JENKINS2
    )
);
?>

User Contributed Notes

There are no user contributed notes for this page.


This method deletes am entry from the hash

  • ≪ QuickHashIntHash::__construct
  • QuickHashIntHash::exists ≫
  • PHP Manual
  • QuickHashIntHash
  • This method deletes am entry from the hash

QuickHashIntHash::delete

(PECL quickhash >= Unknown)

QuickHashIntHash::delete — This method deletes am entry from the hash

설명

public bool QuickHashIntHash::delete ( int $key )

This method deletes an entry from the hash, and returns whether the entry was deleted or not. Associated memory structures will not be freed immediately, but rather when the hash itself is freed.

Elements can not be deleted when the hash is used in an iterator. The method will not throw an exception, but simply return FALSE like would happen with any other deletion failure.

인수

key

The key of the entry to delete.

반환값

TRUE when the entry was deleted, and FALSE if the entry was not deleted.

예제

Example #1 QuickHashIntHash::delete() example

<?php
$hash = new QuickHashIntHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, 5 ) );
var_dump( $hash->delete( 4 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->delete( 4 ) );
?>

위 예제의 출력 예시:

bool(false)
bool(true)
bool(true)
bool(false)
bool(false)

User Contributed Notes

There are no user contributed notes for this page.


This method checks whether a key is part of the hash

  • ≪ QuickHashIntHash::delete
  • QuickHashIntHash::get ≫
  • PHP Manual
  • QuickHashIntHash
  • This method checks whether a key is part of the hash

QuickHashIntHash::exists

(PECL quickhash >= Unknown)

QuickHashIntHash::exists — This method checks whether a key is part of the hash

설명

public bool QuickHashIntHash::exists ( int $key )

This method checks whether an entry with the provided key exists in the hash.

인수

key

The key of the entry to check for whether it exists in the hash.

반환값

Returns TRUE when the entry was found, or FALSE when the entry is not found.

예제

Example #1 QuickHashIntHash::exists() example

<?php
//generate 200000 elements
$array = range( 0, 199999 );
$existingEntries = array_rand( array_flip( $array ), 180000 );
$testForEntries = array_rand( array_flip( $array ), 1000 );
$foundCount = 0;

echo "Creating hash: ", microtime( true ), "\n";
$hash = new QuickHashIntHash( 100000 );
echo "Adding elements: ", microtime( true ), "\n";
foreach( $existingEntries as $key )
{
     $hash->add( $key, 56 );
}

echo "Doing 1000 tests: ", microtime( true ), "\n";
foreach( $testForEntries as $key )
{
     $foundCount += $hash->exists( $key );
}
echo "Done, $foundCount found: ", microtime( true ), "\n";
?>

위 예제의 출력 예시:

Creating hash: 1263588703.0748
Adding elements: 1263588703.0757
Doing 1000 tests: 1263588703.7851
Done, 898 found: 1263588703.7897

User Contributed Notes

There are no user contributed notes for this page.


This method retrieves a value from the hash by its key

  • ≪ QuickHashIntHash::exists
  • QuickHashIntHash::getSize ≫
  • PHP Manual
  • QuickHashIntHash
  • This method retrieves a value from the hash by its key

QuickHashIntHash::get

(PECL quickhash >= Unknown)

QuickHashIntHash::get — This method retrieves a value from the hash by its key

설명

public int QuickHashIntHash::get ( int $key )

This method retrieves a value from the hash by its key.

인수

key

The key of the entry to add.

반환값

The value if the key exists, or NULL if the key wasn't part of the hash.

예제

Example #1 QuickHashIntHash::get() example

<?php
$hash = new QuickHashIntHash( 8 );
var_dump( $hash->get( 1 ) );

var_dump( $hash->add( 2 ) );
var_dump( $hash->get( 2 ) );

var_dump( $hash->add( 3, 5 ) );
var_dump( $hash->get( 3 ) );
?>

위 예제의 출력 예시:

bool(false)
bool(true)
int(1)
bool(true)
int(5)

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of elements in the hash

  • ≪ QuickHashIntHash::get
  • QuickHashIntHash::loadFromFile ≫
  • PHP Manual
  • QuickHashIntHash
  • Returns the number of elements in the hash

QuickHashIntHash::getSize

(PECL quickhash >= Unknown)

QuickHashIntHash::getSize — Returns the number of elements in the hash

설명

public int QuickHashIntHash::getSize ( void )

Returns the number of elements in the hash.

인수

key

The key of the entry to add.

반환값

The number of elements in the hash.

예제

Example #1 QuickHashIntHash::getSize() example

<?php
$hash = new QuickHashIntHash( 8 );
var_dump( $hash->add( 2 ) );
var_dump( $hash->add( 3, 5 ) );
var_dump( $hash->getSize() );
?>

위 예제의 출력 예시:

bool(true)
bool(true)
int(2)

User Contributed Notes

There are no user contributed notes for this page.


This factory method creates a hash from a file

  • ≪ QuickHashIntHash::getSize
  • QuickHashIntHash::loadFromString ≫
  • PHP Manual
  • QuickHashIntHash
  • This factory method creates a hash from a file

QuickHashIntHash::loadFromFile

(PECL quickhash >= Unknown)

QuickHashIntHash::loadFromFile — This factory method creates a hash from a file

설명

public static QuickHashIntHash QuickHashIntHash::loadFromFile ( string $filename [, int $options ] )

This factory method creates a new hash from a definition file on disk. The file format consists of a signature 'QH\0x11\0', the number of elements as a 32 bit signed integer in system Endianness, followed by 32 bit signed integers packed together in the Endianness that the system that the code runs on uses. For each hash element there are two 32 bit signed integers stored. The first of each element is the key, and the second is the value belonging to the key. An example could be:

Example #1 QuickHash IntHash file format

00000000  51 48 11 00 02 00 00 00  01 00 00 00 01 00 00 00  |QH..............|
00000010  03 00 00 00 09 00 00 00                           |........|
00000018

Example #2 QuickHash IntHash file format

header signature ('QH'; key type: 1; value type: 1; filler: \0x00)
00000000  51 48 11 00

number of elements:
00000004  02 00 00 00

data string:
00000000  01 00 00 00 01 00 00 00  03 00 00 00 09 00 00 00

key/value 1 (key = 1, value = 1)
01 00 00 00  01 00 00 00

key/value 2 (key = 3, value = 9)
03 00 00 00  09 00 00 00

인수

filename

The filename of the file to read the hash from.

options

The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the hash, rounded up to the nearest power of two with a maximum limit of 4194304.

반환값

Returns a new QuickHashIntHash.

예제

Example #3 QuickHashIntHash::loadFromFile() example

<?php
$file = dirname( __FILE__ ) . "/simple.hash";
$hash = QuickHashIntHash::loadFromFile(
    $file,
    QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key,
        $hash->exists( $key ) ? 'set' : 'unset'
    );
}
?>

위 예제의 출력 예시:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

User Contributed Notes

There are no user contributed notes for this page.


This factory method creates a hash from a string

  • ≪ QuickHashIntHash::loadFromFile
  • QuickHashIntHash::saveToFile ≫
  • PHP Manual
  • QuickHashIntHash
  • This factory method creates a hash from a string

QuickHashIntHash::loadFromString

(PECL quickhash >= Unknown)

QuickHashIntHash::loadFromString — This factory method creates a hash from a string

설명

public static QuickHashIntHash QuickHashIntHash::loadFromString ( string $contents [, int $options ] )

This factory method creates a new hash from a definition in a string. The file format consists of 32 bit signed integers packed together in the Endianness that the system that the code runs on uses. For each element there are two 32 bit signed integers stored. The first of each element is the key, and the second is the value belonging to the key.

인수

contents

The string containing a serialized format of the hash.

options

The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the hash, rounded up to the nearest power of two with a maximum limit of 4194304.

반환값

Returns a new QuickHashIntHash.

예제

Example #1 QuickHashIntHash::loadFromString() example

<?php
$contents = file_get_contents( dirname( __FILE__ ) . "/simple.hash" );
$hash = QuickHashIntHash::loadFromString(
    $contents,
    QuickHashIntHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key,
        $hash->exists( $key ) ? 'set' : 'unset'
    );
}
?>

위 예제의 출력 예시:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

User Contributed Notes

There are no user contributed notes for this page.


This method stores an in-memory hash to disk

  • ≪ QuickHashIntHash::loadFromString
  • QuickHashIntHash::saveToString ≫
  • PHP Manual
  • QuickHashIntHash
  • This method stores an in-memory hash to disk

QuickHashIntHash::saveToFile

(PECL quickhash >= Unknown)

QuickHashIntHash::saveToFile — This method stores an in-memory hash to disk

설명

public void QuickHashIntHash::saveToFile ( string $filename )

This method stores an existing hash to a file on disk, in the same format that loadFromFile() can read.

인수

filename

The filename of the file to store the hash in.

반환값

값을 반환하지 않습니다.

예제

Example #1 QuickHashIntHash::saveToFile() example

<?php
$hash = new QuickHashIntHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, 43 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, 52 ) );

$hash->saveToFile( '/tmp/test.hash' );
?>

User Contributed Notes

There are no user contributed notes for this page.


This method returns a serialized version of the hash

  • ≪ QuickHashIntHash::saveToFile
  • QuickHashIntHash::set ≫
  • PHP Manual
  • QuickHashIntHash
  • This method returns a serialized version of the hash

QuickHashIntHash::saveToString

(PECL quickhash >= Unknown)

QuickHashIntHash::saveToString — This method returns a serialized version of the hash

설명

public string QuickHashIntHash::saveToString ( void )

This method returns a serialized version of the hash in the same format that loadFromString() can read.

반환값

This method returns a string containing a serialized format of the hash. Each element is stored as a four byte value in the Endianness that the current system uses.

예제

Example #1 QuickHashIntHash::saveToString() example

<?php
$hash = new QuickHashIntHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, 34 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, 55 ) );

var_dump( $hash->saveToString() );
?>

User Contributed Notes

There are no user contributed notes for this page.


This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

  • ≪ QuickHashIntHash::saveToString
  • QuickHashIntHash::update ≫
  • PHP Manual
  • QuickHashIntHash
  • This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

QuickHashIntHash::set

(PECL quickhash >= Unknown)

QuickHashIntHash::set — This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

설명

public bool QuickHashIntHash::set ( int $key , int $value )

This method tries to update an entry with a new value. In case the entry did not yet exist, it will instead add a new entry. It returns whether the entry was added or update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashIntHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

인수

key

The key of the entry to add or update.

value

The new value to set the entry with.

반환값

2 if the entry was found and updated, 1 if the entry was newly added or 0 if there was an error.

예제

Example #1 QuickHashIntHash::set() example

<?php
$hash = new QuickHashIntHash( 1024 );

echo "Set->Add\n";
var_dump( $hash->get( 46692 ) );
var_dump( $hash->set( 46692, 16091 ) );
var_dump( $hash->get( 46692 ) );

echo "Set->Update\n";
var_dump( $hash->set( 46692, 29906 ) );
var_dump( $hash->get( 46692 ) );
?>

위 예제의 출력 예시:

bool(false)
int(2)
int(16091)
Set->Update
int(1)
int(29906)

User Contributed Notes

There are no user contributed notes for this page.


This method updates an entry in the hash with a new value

  • ≪ QuickHashIntHash::set
  • QuickHashStringIntHash ≫
  • PHP Manual
  • QuickHashIntHash
  • This method updates an entry in the hash with a new value

QuickHashIntHash::update

(PECL quickhash >= Unknown)

QuickHashIntHash::update — This method updates an entry in the hash with a new value

설명

public bool QuickHashIntHash::update ( int $key , int $value )

This method updates an entry with a new value, and returns whether the entry was update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashIntHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

인수

key

The key of the entry to add.

value

The new value to update the entry with.

반환값

TRUE when the entry was found and updated, and FALSE if the entry was not part of the hash already.

예제

Example #1 QuickHashIntHash::update() example

<?php
$hash = new QuickHashIntHash( 1024 );

var_dump( $hash->add( 141421, 173205 ) );
var_dump( $hash->update( 141421, 223606 ) );
var_dump( $hash->get( 141421 ) );
?>

위 예제의 출력 예시:

bool(true)
bool(true)
int(223606)

User Contributed Notes

There are no user contributed notes for this page.


The QuickHashStringIntHash class

(No version information available, might only be in Git)

소개

This class wraps around a hash containing strings, where the values are integer numbers. Hashes are also available as implementation of the ArrayAccess interface.

Hashes can also be iterated over with foreach as the Iterator interface is implemented as well. The order of which elements are returned in is not guaranteed.

클래스 개요

QuickHashStringIntHash {
/* Constants */
const integer CHECK_FOR_DUPES = 1 ;
const integer DO_NOT_USE_ZEND_ALLOC = 2 ;
/* Methods */
public bool add ( string $key , int $value )
public __construct ( int $size [, int $options = 0 ] )
public bool delete ( string $key )
public bool exists ( string $key )
public mixed get ( string $key )
public int getSize ( void )
public static QuickHashStringIntHash loadFromFile ( string $filename [, int $size = 0 [, int $options = 0 ]] )
public static QuickHashStringIntHash loadFromString ( string $contents [, int $size = 0 [, int $options = 0 ]] )
public void saveToFile ( string $filename )
public string saveToString ( void )
public int set ( string $key , int $value )
public bool update ( string $key , int $value )
}

예약 상수

QuickHashStringIntHash::CHECK_FOR_DUPES

If enabled, adding duplicate elements to a set (through either add() or loadFromFile()) will result in those elements to be dropped from the set. This will take up extra time, so only used when it is required.

QuickHashStringIntHash::DO_NOT_USE_ZEND_ALLOC

Disables the use of PHP's internal memory manager for internal set structures. With this option enabled, internal allocations will not count towards the memory_limit settings.

Table of Contents

  • QuickHashStringIntHash::add — This method adds a new entry to the hash
  • QuickHashStringIntHash::__construct — Creates a new QuickHashStringIntHash object
  • QuickHashStringIntHash::delete — This method deletes am entry from the hash
  • QuickHashStringIntHash::exists — This method checks whether a key is part of the hash
  • QuickHashStringIntHash::get — This method retrieves a value from the hash by its key
  • QuickHashStringIntHash::getSize — Returns the number of elements in the hash
  • QuickHashStringIntHash::loadFromFile — This factory method creates a hash from a file
  • QuickHashStringIntHash::loadFromString — This factory method creates a hash from a string
  • QuickHashStringIntHash::saveToFile — This method stores an in-memory hash to disk
  • QuickHashStringIntHash::saveToString — This method returns a serialized version of the hash
  • QuickHashStringIntHash::set — This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist
  • QuickHashStringIntHash::update — This method updates an entry in the hash with a new value

User Contributed Notes

There are no user contributed notes for this page.


This method adds a new entry to the hash

  • ≪ QuickHashStringIntHash
  • QuickHashStringIntHash::__construct ≫
  • PHP Manual
  • QuickHashStringIntHash
  • This method adds a new entry to the hash

QuickHashStringIntHash::add

(No version information available, might only be in Git)

QuickHashStringIntHash::add — This method adds a new entry to the hash

설명

public bool QuickHashStringIntHash::add ( string $key , int $value )

This method adds a new entry to the hash, and returns whether the entry was added. Entries are by default always added unless QuickHashStringIntHash::CHECK_FOR_DUPES has been passed when the hash was created.

인수

key

The key of the entry to add.

value

The value of the entry to add.

반환값

TRUE when the entry was added, and FALSE if the entry was not added.

예제

Example #1 QuickHashStringIntHash::add() example

<?php
echo "without dupe checking\n";
$hash = new QuickHashStringIntHash( 1024 );
var_dump( $hash );
var_dump( $hash->exists( "four" ) );
var_dump( $hash->get( "four" ) );
var_dump( $hash->add( "four", 22 ) );
var_dump( $hash->exists( "four" ) );
var_dump( $hash->get( "four" ) );
var_dump( $hash->add( "four", 12 ) );

echo "\nwith dupe checking\n";
$hash = new QuickHashStringIntHash( 1024, QuickHashStringIntHash::CHECK_FOR_DUPES );
var_dump( $hash );
var_dump( $hash->exists( "four" ) );
var_dump( $hash->get( "four" ) );
var_dump( $hash->add( "four", 78 ) );
var_dump( $hash->exists( "four" ) );
var_dump( $hash->get( "four" ) );
var_dump( $hash->add( "four", 9 ) );
?>

위 예제의 출력 예시:

without dupe checking
object(QuickHashStringIntHash)#1 (0) {
}
bool(false)
bool(false)
bool(true)
bool(true)
int(22)
bool(true)

with dupe checking
object(QuickHashStringIntHash)#2 (0) {
}
bool(false)
bool(false)
bool(true)
bool(true)
int(78)
bool(false)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new QuickHashStringIntHash object

  • ≪ QuickHashStringIntHash::add
  • QuickHashStringIntHash::delete ≫
  • PHP Manual
  • QuickHashStringIntHash
  • Creates a new QuickHashStringIntHash object

QuickHashStringIntHash::__construct

(No version information available, might only be in Git)

QuickHashStringIntHash::__construct — Creates a new QuickHashStringIntHash object

설명

public QuickHashStringIntHash::__construct ( int $size [, int $options = 0 ] )

This constructor creates a new QuickHashStringIntHash. The size is the amount of bucket lists to create. The more lists there are, the less collisions you will have. Options are also supported.

인수

size

The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 64 to 4194304.

options

The options that you can pass in are: QuickHashStringIntHash::CHECK_FOR_DUPES, which makes sure no duplicate entries are added to the hash and QuickHashStringIntHash::DO_NOT_USE_ZEND_ALLOC to not use PHP's internal memory manager.

반환값

Returns a new QuickHashStringIntHash object.

예제

Example #1 QuickHashStringIntHash::__construct() example

<?php
var_dump( new QuickHashStringIntHash( 1024 ) );
var_dump( new QuickHashStringIntHash( 1024, QuickHashStringIntHash::CHECK_FOR_DUPES ) );
?>

User Contributed Notes

There are no user contributed notes for this page.


This method deletes am entry from the hash

  • ≪ QuickHashStringIntHash::__construct
  • QuickHashStringIntHash::exists ≫
  • PHP Manual
  • QuickHashStringIntHash
  • This method deletes am entry from the hash

QuickHashStringIntHash::delete

(No version information available, might only be in Git)

QuickHashStringIntHash::delete — This method deletes am entry from the hash

설명

public bool QuickHashStringIntHash::delete ( string $key )

This method deletes an entry from the hash, and returns whether the entry was deleted or not. Associated memory structures will not be freed immediately, but rather when the hash itself is freed.

Elements can not be deleted when the hash is used in an iterator. The method will not throw an exception, but simply return FALSE like would happen with any other deletion failure.

인수

key

The key of the entry to delete.

반환값

TRUE when the entry was deleted, and FALSE if the entry was not deleted.

예제

Example #1 QuickHashStringIntHash::delete() example

<?php
$hash = new QuickHashStringIntHash( 1024 );
var_dump( $hash->exists( 'four' ) );
var_dump( $hash->add( 'four', 5 ) );
var_dump( $hash->get( 'four' ) );
var_dump( $hash->delete( 'four' ) );
var_dump( $hash->exists( 'four' ) );
var_dump( $hash->get( 'four' ) );
var_dump( $hash->delete( 'four' ) );
?>

위 예제의 출력 예시:

bool(false)
bool(true)
int(5)
bool(true)
bool(false)
bool(false)
bool(false)

User Contributed Notes

There are no user contributed notes for this page.


This method checks whether a key is part of the hash

  • ≪ QuickHashStringIntHash::delete
  • QuickHashStringIntHash::get ≫
  • PHP Manual
  • QuickHashStringIntHash
  • This method checks whether a key is part of the hash

QuickHashStringIntHash::exists

(No version information available, might only be in Git)

QuickHashStringIntHash::exists — This method checks whether a key is part of the hash

설명

public bool QuickHashStringIntHash::exists ( string $key )

This method checks whether an entry with the provided key exists in the hash.

인수

key

The key of the entry to check for whether it exists in the hash.

반환값

Returns TRUE when the entry was found, or FALSE when the entry is not found.

User Contributed Notes

There are no user contributed notes for this page.


This method retrieves a value from the hash by its key

  • ≪ QuickHashStringIntHash::exists
  • QuickHashStringIntHash::getSize ≫
  • PHP Manual
  • QuickHashStringIntHash
  • This method retrieves a value from the hash by its key

QuickHashStringIntHash::get

(No version information available, might only be in Git)

QuickHashStringIntHash::get — This method retrieves a value from the hash by its key

설명

public mixed QuickHashStringIntHash::get ( string $key )

This method retrieves a value from the hash by its key.

인수

key

The key of the entry to add.

반환값

The value if the key exists, or NULL if the key wasn't part of the hash.

예제

Example #1 QuickHashStringIntHash::get() example

<?php
$hash = new QuickHashStringIntHash( 8 );
var_dump( $hash->get( "one" ) );

var_dump( $hash->add( "two", 2 ) );
var_dump( $hash->get( "two" ) );
?>

위 예제의 출력 예시:

bool(false)
bool(true)
int(2)

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of elements in the hash

  • ≪ QuickHashStringIntHash::get
  • QuickHashStringIntHash::loadFromFile ≫
  • PHP Manual
  • QuickHashStringIntHash
  • Returns the number of elements in the hash

QuickHashStringIntHash::getSize

(No version information available, might only be in Git)

QuickHashStringIntHash::getSize — Returns the number of elements in the hash

설명

public int QuickHashStringIntHash::getSize ( void )

Returns the number of elements in the hash.

인수

key

The key of the entry to add.

반환값

The number of elements in the hash.

예제

Example #1 QuickHashStringIntHash::getSize() example

<?php
$hash = new QuickHashStringIntHash( 8 );
var_dump( $hash->add( "two", 2 ) );
var_dump( $hash->add( "three", 5 ) );
var_dump( $hash->getSize() );
?>

위 예제의 출력 예시:

bool(true)
bool(true)
int(2)

User Contributed Notes

There are no user contributed notes for this page.


This factory method creates a hash from a file

  • ≪ QuickHashStringIntHash::getSize
  • QuickHashStringIntHash::loadFromString ≫
  • PHP Manual
  • QuickHashStringIntHash
  • This factory method creates a hash from a file

QuickHashStringIntHash::loadFromFile

(No version information available, might only be in Git)

QuickHashStringIntHash::loadFromFile — This factory method creates a hash from a file

설명

public static QuickHashStringIntHash QuickHashStringIntHash::loadFromFile ( string $filename [, int $size = 0 [, int $options = 0 ]] )

This factory method creates a new hash from a definition file on disk. The file format consists of a signature 'QH\0x21\0', the number of elements as a 32 bit signed integer in system Endianness, an unsigned 32 bit integer containing the number of element data to follow in characters. This element data contains all the strings. The follows another signed 32 bit integer containing the number of bucket lists. After the header and the strings, the elements follow. They are ordered by bucket list so that the keys don't have to be hashed in order to restore the hash. For each bucket list, the following information is stored (all as 32 bit integers): the bucket list index, the number of elements in that list, and then in pairs of two unsigned 32 bit integers the elements, where the first one is the index into the string list containing the keys, and the second one the value. An example could be:

Example #1 QuickHash StringIntHash file format

00000000  51 48 21 00 02 00 00 00  09 00 00 00 40 00 00 00  |QH!.........@...|
00000010  4f 4e 45 00 4e 49 4e 45  00 07 00 00 00 01 00 00  |ONE.NINE........|
00000020  00 00 00 00 00 01 00 00  00 2f 00 00 00 01 00 00  |........./......|
00000030  00 04 00 00 00 03 00 00  00                       |.........|
00000039

Example #2 QuickHash IntHash file format

header signature ('QH'; key type: 2; value type: 1; filler: \0x00)
00000000  51 48 21 00

number of elements:
00000004  02 00 00 00

length of string values (9 characters):
00000008  09 00 00 00

number of hash bucket lists (this is configured for hashes as argument to the
constructor normally, 64 in this case):
0000000C  40 00 00 00

string values:
00000010  4f 4e 45 00 4e 49 4e 45  00

bucket lists:
  bucket list 1 (with key 7, and 1 element):
    header:
    07 00 00 00 01 00 00 00
    elements (key index: 0 ('ONE'), value = 0):
    00 00 00 00 01 00 00 00
  bucket list 2 (with key 0x2f, and 1 element):
    header:
    2f 00 00 00 01 00 00 00
    elements (key index: 4 ('NINE'), value = 3):
    04 00 00 00 03 00 00 00

인수

filename

The filename of the file to read the hash from.

size

The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options

The same options that the class' constructor takes; except that the size option is ignored. It is read from the file format (unlike the QuickHashIntHash and QuickHashIntStringHash classes, where it is automatically calculated from the number of entries in the hash.)

반환값

Returns a new QuickHashStringIntHash.

예제

Example #3 QuickHashStringIntHash::loadFromFile() example

<?php
$file = dirname( __FILE__ ) . "/simple.hash.string";
$hash = QuickHashStringIntHash::loadFromFile(
    $file,
    QuickHashStringIntHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    $i = 48712 + $key * 1631;
    $k = base_convert( $i, 10, 36 );
    echo $k, ' => ', $hash->get( $k ), "\n";
}
?>

위 예제의 출력 예시:

11l4 => 48712
12uf => 50343
143q => 51974
15d1 => 53605
16mc => 55236
17vn => 56867
194y => 58498
1ae9 => 60129
1bnk => 61760
1cwv => 63391
1e66 => 65022
1ffh => 66653
1gos => 68284
1hy3 => 69915
1j7e => 71546
1kgp => 73177

User Contributed Notes

There are no user contributed notes for this page.


This factory method creates a hash from a string

  • ≪ QuickHashStringIntHash::loadFromFile
  • QuickHashStringIntHash::saveToFile ≫
  • PHP Manual
  • QuickHashStringIntHash
  • This factory method creates a hash from a string

QuickHashStringIntHash::loadFromString

(No version information available, might only be in Git)

QuickHashStringIntHash::loadFromString — This factory method creates a hash from a string

설명

public static QuickHashStringIntHash QuickHashStringIntHash::loadFromString ( string $contents [, int $size = 0 [, int $options = 0 ]] )

This factory method creates a new hash from a definition in a string. The format is the same as the one used in "loadFromFile".

인수

contents

The string containing a serialized format of the hash.

size

The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options

The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the hash, rounded up to the nearest power of two with a maximum limit of 4194304.

반환값

Returns a new QuickHashStringIntHash.

예제

Example #1 QuickHashStringIntHash::loadFromString() example

<?php
$contents = file_get_contents( dirname( __FILE__ ) . "/simple.hash.string" );
$hash = QuickHashStringIntHash::loadFromString(
    $contents,
    QuickHashStringIntHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    $i = 48712 + $key * 1631;
    $k = base_convert( $i, 10, 36 );
    echo $k, ' => ', $hash->get( $k ), "\n";
}
?>

위 예제의 출력 예시:

11l4 => 48712
12uf => 50343
143q => 51974
15d1 => 53605
16mc => 55236
17vn => 56867
194y => 58498
1ae9 => 60129
1bnk => 61760
1cwv => 63391
1e66 => 65022
1ffh => 66653
1gos => 68284
1hy3 => 69915
1j7e => 71546
1kgp => 73177

User Contributed Notes

There are no user contributed notes for this page.


This method stores an in-memory hash to disk

  • ≪ QuickHashStringIntHash::loadFromString
  • QuickHashStringIntHash::saveToString ≫
  • PHP Manual
  • QuickHashStringIntHash
  • This method stores an in-memory hash to disk

QuickHashStringIntHash::saveToFile

(No version information available, might only be in Git)

QuickHashStringIntHash::saveToFile — This method stores an in-memory hash to disk

설명

public void QuickHashStringIntHash::saveToFile ( string $filename )

This method stores an existing hash to a file on disk, in the same format that loadFromFile() can read.

인수

filename

The filename of the file to store the hash in.

반환값

값을 반환하지 않습니다.

예제

Example #1 QuickHashStringIntHash::saveToFile() example

<?php
$hash = new QuickHashStringIntHash( 1024 );
var_dump( $hash->add( "fourty three", 42 ) );
var_dump( $hash->add( "fifty two", 52 ) );

$hash->saveToFile( '/tmp/test.hash.string' );
?>

User Contributed Notes

There are no user contributed notes for this page.


This method returns a serialized version of the hash

  • ≪ QuickHashStringIntHash::saveToFile
  • QuickHashStringIntHash::set ≫
  • PHP Manual
  • QuickHashStringIntHash
  • This method returns a serialized version of the hash

QuickHashStringIntHash::saveToString

(No version information available, might only be in Git)

QuickHashStringIntHash::saveToString — This method returns a serialized version of the hash

설명

public string QuickHashStringIntHash::saveToString ( void )

This method returns a serialized version of the hash in the same format that loadFromString() can read.

반환값

This method returns a serialized format of an existing hash, in the same format that loadFromString() can read.

예제

Example #1 QuickHashStringIntHash::saveToString() example

<?php
$hash = new QuickHashStringIntHash( 1024 );
var_dump( $hash->add( "fourty three", 42 ) );
var_dump( $hash->add( "fifty two", 52 ) );

var_dump( $hash->saveToString() );
?>

User Contributed Notes

There are no user contributed notes for this page.


This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

  • ≪ QuickHashStringIntHash::saveToString
  • QuickHashStringIntHash::update ≫
  • PHP Manual
  • QuickHashStringIntHash
  • This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

QuickHashStringIntHash::set

(No version information available, might only be in Git)

QuickHashStringIntHash::set — This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

설명

public int QuickHashStringIntHash::set ( string $key , int $value )

This method tries to update an entry with a new value. In case the entry did not yet exist, it will instead add a new entry. It returns whether the entry was added or update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashStringIntHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

인수

key

The key of the entry to add or update.

value

The value of the entry to add. If a non-string is passed, it will be converted to a string automatically if possible.

반환값

2 if the entry was found and updated, 1 if the entry was newly added or 0 if there was an error.

예제

Example #1 QuickHashStringIntHash::set() example

<?php
$hash = new QuickHashStringIntHash( 1024 );

echo "Set->Add\n";
var_dump( $hash->get( "fourty six thousand six hundred ninety two" ) );
var_dump( $hash->set( "fourty six thousand six hundred ninety two", 16091 ) );
var_dump( $hash->get( "fourty six thousand six hundred ninety two" ) );

echo "Set->Update\n";
var_dump( $hash->set( "fourty six thousand six hundred ninety two", 29906 ) );
var_dump( $hash->get( "fourty six thousand six hundred ninety two" ) );
?>

위 예제의 출력 예시:

Set->Add
bool(false)
int(2)
int(16091)
Set->Update
int(1)
int(29906)

User Contributed Notes

There are no user contributed notes for this page.


This method updates an entry in the hash with a new value

  • ≪ QuickHashStringIntHash::set
  • QuickHashIntStringHash ≫
  • PHP Manual
  • QuickHashStringIntHash
  • This method updates an entry in the hash with a new value

QuickHashStringIntHash::update

(No version information available, might only be in Git)

QuickHashStringIntHash::update — This method updates an entry in the hash with a new value

설명

public bool QuickHashStringIntHash::update ( string $key , int $value )

This method updates an entry with a new value, and returns whether the entry was update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashStringIntHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

인수

key

The key of the entry to add.

value

The new value for the entry. If a non-string is passed, it will be converted to a string automatically if possible.

반환값

TRUE when the entry was found and updated, and FALSE if the entry was not part of the hash already.

예제

Example #1 QuickHashStringIntHash::update() example

<?php
$hash = new QuickHashStringIntHash( 1024 );

$hash->add( 'six', 314159265 );
$hash->add( "a lot", 314159265 );

echo $hash->get( 'six' ), "\n";
echo $hash->get( 'a lot' ), "\n";

var_dump( $hash->update( 'a lot', 314159266 ) );
var_dump( $hash->update( "a lot plus one", 314159999 ) );

echo $hash->get( 'six' ), "\n";
echo $hash->get( 'a lot' ), "\n";
?>

위 예제의 출력 예시:

314159265
314159265
bool(true)
bool(false)
314159265
314159266

User Contributed Notes

There are no user contributed notes for this page.


The QuickHashIntStringHash class

(PECL quickhash >= Unknown)

소개

This class wraps around a hash containing integer numbers, where the values are strings. Hashes are also available as implementation of the ArrayAccess interface.

Hashes can also be iterated over with foreach as the Iterator interface is implemented as well. The order of which elements are returned in is not guaranteed.

클래스 개요

QuickHashIntStringHash {
/* Constants */
const integer CHECK_FOR_DUPES = 1 ;
const integer DO_NOT_USE_ZEND_ALLOC = 2 ;
const integer HASHER_NO_HASH = 256 ;
const integer HASHER_JENKINS1 = 512 ;
const integer HASHER_JENKINS2 = 1024 ;
/* Methods */
public bool add ( int $key , string $value )
public __construct ( int $size [, int $options = 0 ] )
public bool delete ( int $key )
public bool exists ( int $key )
public mixed get ( int $key )
public int getSize ( void )
public static QuickHashIntStringHash loadFromFile ( string $filename [, int $size = 0 [, int $options = 0 ]] )
public static QuickHashIntStringHash loadFromString ( string $contents [, int $size = 0 [, int $options = 0 ]] )
public void saveToFile ( string $filename )
public string saveToString ( void )
public int set ( int $key , string $value )
public bool update ( int $key , string $value )
}

예약 상수

QuickHashIntStringHash::CHECK_FOR_DUPES

If enabled, adding duplicate elements to a set (through either add() or loadFromFile()) will result in those elements to be dropped from the set. This will take up extra time, so only used when it is required.

QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC

Disables the use of PHP's internal memory manager for internal set structures. With this option enabled, internal allocations will not count towards the memory_limit settings.

QuickHashIntStringHash::HASHER_NO_HASH

Selects to not use a hashing function, but merely use a modulo to find the bucket list index. This is not faster than normal hashing, and gives more collisions.

QuickHashIntStringHash::HASHER_JENKINS1

This is the default hashing function to turn the integer hashes into bucket list indexes.

QuickHashIntStringHash::HASHER_JENKINS2

Selects a variant hashing algorithm.

Table of Contents

  • QuickHashIntStringHash::add — This method adds a new entry to the hash
  • QuickHashIntStringHash::__construct — Creates a new QuickHashIntStringHash object
  • QuickHashIntStringHash::delete — This method deletes am entry from the hash
  • QuickHashIntStringHash::exists — This method checks whether a key is part of the hash
  • QuickHashIntStringHash::get — This method retrieves a value from the hash by its key
  • QuickHashIntStringHash::getSize — Returns the number of elements in the hash
  • QuickHashIntStringHash::loadFromFile — This factory method creates a hash from a file
  • QuickHashIntStringHash::loadFromString — This factory method creates a hash from a string
  • QuickHashIntStringHash::saveToFile — This method stores an in-memory hash to disk
  • QuickHashIntStringHash::saveToString — This method returns a serialized version of the hash
  • QuickHashIntStringHash::set — This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist
  • QuickHashIntStringHash::update — This method updates an entry in the hash with a new value

User Contributed Notes

There are no user contributed notes for this page.


This method adds a new entry to the hash

  • ≪ QuickHashIntStringHash
  • QuickHashIntStringHash::__construct ≫
  • PHP Manual
  • QuickHashIntStringHash
  • This method adds a new entry to the hash

QuickHashIntStringHash::add

(PECL quickhash >= Unknown)

QuickHashIntStringHash::add — This method adds a new entry to the hash

설명

public bool QuickHashIntStringHash::add ( int $key , string $value )

This method adds a new entry to the hash, and returns whether the entry was added. Entries are by default always added unless QuickHashIntStringHash::CHECK_FOR_DUPES has been passed when the hash was created.

인수

key

The key of the entry to add.

value

The value of the entry to add. If a non-string is passed, it will be converted to a string automatically if possible.

반환값

TRUE when the entry was added, and FALSE if the entry was not added.

예제

Example #1 QuickHashIntStringHash::add() example

<?php
echo "without dupe checking\n";
$hash = new QuickHashIntStringHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, "twenty two" ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, "twelve" ) );

echo "\nwith dupe checking\n";
$hash = new QuickHashIntStringHash( 1024, QuickHashIntStringHash::CHECK_FOR_DUPES );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, "seventy eight" ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->get( 4 ) );
var_dump( $hash->add( 4, "nine" ) );
?>

위 예제의 출력 예시:

without dupe checking
bool(false)
bool(false)
bool(true)
bool(true)
string(10) "twenty two"
bool(true)

with dupe checking
bool(false)
bool(false)
bool(true)
bool(true)
string(13) "seventy eight"
bool(false)

User Contributed Notes

There are no user contributed notes for this page.


Creates a new QuickHashIntStringHash object

  • ≪ QuickHashIntStringHash::add
  • QuickHashIntStringHash::delete ≫
  • PHP Manual
  • QuickHashIntStringHash
  • Creates a new QuickHashIntStringHash object

QuickHashIntStringHash::__construct

(PECL quickhash >= Unknown)

QuickHashIntStringHash::__construct — Creates a new QuickHashIntStringHash object

설명

public QuickHashIntStringHash::__construct ( int $size [, int $options = 0 ] )

This constructor creates a new QuickHashIntStringHash. The size is the amount of bucket lists to create. The more lists there are, the less collisions you will have. Options are also supported.

인수

size

The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 64 to 4194304.

options

The options that you can pass in are: QuickHashIntStringHash::CHECK_FOR_DUPES, which makes sure no duplicate entries are added to the hash; QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC to not use PHP's internal memory manager as well as one of QuickHashIntStringHash::HASHER_NO_HASH, QuickHashIntStringHash::HASHER_JENKINS1 or QuickHashIntStringHash::HASHER_JENKINS2. These last three configure which hashing algorithm to use. All options can be combined using bitmasks.

반환값

Returns a new QuickHashIntStringHash object.

예제

Example #1 QuickHashIntStringHash::__construct() example

<?php
var_dump( new QuickHashIntStringHash( 1024 ) );
var_dump( new QuickHashIntStringHash( 1024, QuickHashIntStringHash::CHECK_FOR_DUPES ) );
var_dump(
    new QuickHashIntStringHash(
        1024,
        QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC | QuickHashIntStringHash::HASHER_JENKINS2
    )
);
?>

User Contributed Notes

There are no user contributed notes for this page.


This method deletes am entry from the hash

  • ≪ QuickHashIntStringHash::__construct
  • QuickHashIntStringHash::exists ≫
  • PHP Manual
  • QuickHashIntStringHash
  • This method deletes am entry from the hash

QuickHashIntStringHash::delete

(PECL quickhash >= Unknown)

QuickHashIntStringHash::delete — This method deletes am entry from the hash

설명

public bool QuickHashIntStringHash::delete ( int $key )

This method deletes an entry from the hash, and returns whether the entry was deleted or not. Associated memory structures will not be freed immediately, but rather when the hash itself is freed.

Elements can not be deleted when the hash is used in an iterator. The method will not throw an exception, but simply return FALSE like would happen with any other deletion failure.

인수

key

The key of the entry to delete.

반환값

TRUE when the entry was deleted, and FALSE if the entry was not deleted.

예제

Example #1 QuickHashIntStringHash::delete() example

<?php
$hash = new QuickHashIntStringHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, "five" ) );
var_dump( $hash->delete( 4 ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->delete( 4 ) );
?>

위 예제의 출력 예시:

bool(false)
bool(true)
bool(true)
bool(false)
bool(false)

User Contributed Notes

There are no user contributed notes for this page.


This method checks whether a key is part of the hash

  • ≪ QuickHashIntStringHash::delete
  • QuickHashIntStringHash::get ≫
  • PHP Manual
  • QuickHashIntStringHash
  • This method checks whether a key is part of the hash

QuickHashIntStringHash::exists

(PECL quickhash >= Unknown)

QuickHashIntStringHash::exists — This method checks whether a key is part of the hash

설명

public bool QuickHashIntStringHash::exists ( int $key )

This method checks whether an entry with the provided key exists in the hash.

인수

key

The key of the entry to check for whether it exists in the hash.

반환값

Returns TRUE when the entry was found, or FALSE when the entry is not found.

User Contributed Notes

There are no user contributed notes for this page.


This method retrieves a value from the hash by its key

  • ≪ QuickHashIntStringHash::exists
  • QuickHashIntStringHash::getSize ≫
  • PHP Manual
  • QuickHashIntStringHash
  • This method retrieves a value from the hash by its key

QuickHashIntStringHash::get

(PECL quickhash >= Unknown)

QuickHashIntStringHash::get — This method retrieves a value from the hash by its key

설명

public mixed QuickHashIntStringHash::get ( int $key )

This method retrieves a value from the hash by its key.

인수

key

The key of the entry to add.

반환값

The value if the key exists, or NULL if the key wasn't part of the hash.

예제

Example #1 QuickHashIntStringHash::get() example

<?php
$hash = new QuickHashIntStringHash( 8 );
var_dump( $hash->get( 1 ) );

var_dump( $hash->add( 2, "two" ) );
var_dump( $hash->get( 2 ) );

var_dump( $hash->add( 3, 5 ) );
var_dump( $hash->get( 3 ) );
?>

위 예제의 출력 예시:

bool(false)
bool(true)
string(3) "two"
bool(true)
string(1) "5"

User Contributed Notes

There are no user contributed notes for this page.


Returns the number of elements in the hash

  • ≪ QuickHashIntStringHash::get
  • QuickHashIntStringHash::loadFromFile ≫
  • PHP Manual
  • QuickHashIntStringHash
  • Returns the number of elements in the hash

QuickHashIntStringHash::getSize

(PECL quickhash >= Unknown)

QuickHashIntStringHash::getSize — Returns the number of elements in the hash

설명

public int QuickHashIntStringHash::getSize ( void )

Returns the number of elements in the hash.

반환값

The number of elements in the hash.

예제

Example #1 QuickHashIntStringHash::getSize() example

<?php
$hash = new QuickHashIntStringHash( 8 );
var_dump( $hash->add( 2, "two" ) );
var_dump( $hash->add( 3, 5 ) );
var_dump( $hash->getSize() );
?>

위 예제의 출력 예시:

bool(true)
bool(true)
int(2)

User Contributed Notes

There are no user contributed notes for this page.


This factory method creates a hash from a file

  • ≪ QuickHashIntStringHash::getSize
  • QuickHashIntStringHash::loadFromString ≫
  • PHP Manual
  • QuickHashIntStringHash
  • This factory method creates a hash from a file

QuickHashIntStringHash::loadFromFile

(PECL quickhash >= Unknown)

QuickHashIntStringHash::loadFromFile — This factory method creates a hash from a file

설명

public static QuickHashIntStringHash QuickHashIntStringHash::loadFromFile ( string $filename [, int $size = 0 [, int $options = 0 ]] )

This factory method creates a new hash from a definition file on disk. The file format consists of a signature 'QH\0x12\0', the number of elements as a 32 bit signed integer in system Endianness, an unsigned 32 bit integer containing the number of element data to follow in characters. This element data contains all the strings. After the header and the strings, the elements follow in pairs of two unsigned 32 bit integers where the first one is the key, and the second one the index in the element data string. An example could be:

Example #1 QuickHash IntString file format

00000000  51 48 12 00 02 00 00 00  09 00 00 00 4f 4e 45 00  |QH..........ONE.|
00000010  4e 49 4e 45 00 01 00 00  00 00 00 00 00 03 00 00  |NINE............|
00000020  00 04 00 00 00                                    |.....|
00000025

Example #2 QuickHash IntString file format

header signature ('QH'; key type: 1; value type: 2; filler: \0x00)
00000000  51 48 12 00

number of elements:
00000004  02 00 00 00

length of string values (9 characters):
00000008  09 00 00 00

string values:
0000000C  4f 4e 45 00 4e 49 4e 45  00

data string:
00000015  01 00 00 00 00 00 00 00  03 00 00 00 04 00 00 00

key/value 1 (key = 1, string index = 0 ("ONE")):
01 00 00 00  00 00 00 00

key/value 2 (key = 3, string index = 4 ("NINE")):
03 00 00 00  04 00 00 00

인수

filename

The filename of the file to read the hash from.

size

The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options

The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the hash, rounded up to the nearest power of two with a maximum limit of 4194304.

반환값

Returns a new QuickHashIntStringHash.

예제

Example #3 QuickHashIntStringHash::loadFromFile() example

<?php
$file = dirname( __FILE__ ) . "/simple.string.hash";
$hash = QuickHashIntStringHash::loadFromFile(
    $file,
    QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key,
        $hash->exists( $key ) ? 'set' : 'unset'
    );
}
?>

위 예제의 출력 예시:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

User Contributed Notes

There are no user contributed notes for this page.


This factory method creates a hash from a string

  • ≪ QuickHashIntStringHash::loadFromFile
  • QuickHashIntStringHash::saveToFile ≫
  • PHP Manual
  • QuickHashIntStringHash
  • This factory method creates a hash from a string

QuickHashIntStringHash::loadFromString

(PECL quickhash >= Unknown)

QuickHashIntStringHash::loadFromString — This factory method creates a hash from a string

설명

public static QuickHashIntStringHash QuickHashIntStringHash::loadFromString ( string $contents [, int $size = 0 [, int $options = 0 ]] )

This factory method creates a new hash from a definition in a string. The format is the same as the one used in "loadFromFile".

인수

contents

The string containing a serialized format of the hash.

size

The amount of bucket lists to configure. The number you pass in will be automatically rounded up to the next power of two. It is also automatically limited from 4 to 4194304.

options

The same options that the class' constructor takes; except that the size option is ignored. It is automatically calculated to be the same as the number of entries in the hash, rounded up to the nearest power of two with a maximum limit of 4194304.

반환값

Returns a new QuickHashIntStringHash.

예제

Example #1 QuickHashIntStringHash::loadFromString() example

<?php
$contents = file_get_contents( dirname( __FILE__ ) . "/simple.hash" );
$hash = QuickHashIntStringHash::loadFromString(
    $contents,
    QuickHashIntStringHash::DO_NOT_USE_ZEND_ALLOC
);
foreach( range( 0, 0x0f ) as $key )
{
    printf( "Key %3d (%2x) is %s\n",
        $key, $key,
        $hash->exists( $key ) ? 'set' : 'unset'
    );
}
?>

위 예제의 출력 예시:

Key   0 ( 0) is unset
Key   1 ( 1) is set
Key   2 ( 2) is set
Key   3 ( 3) is set
Key   4 ( 4) is unset
Key   5 ( 5) is set
Key   6 ( 6) is unset
Key   7 ( 7) is set
Key   8 ( 8) is unset
Key   9 ( 9) is unset
Key  10 ( a) is unset
Key  11 ( b) is set
Key  12 ( c) is unset
Key  13 ( d) is set
Key  14 ( e) is unset
Key  15 ( f) is unset

User Contributed Notes

There are no user contributed notes for this page.


This method stores an in-memory hash to disk

  • ≪ QuickHashIntStringHash::loadFromString
  • QuickHashIntStringHash::saveToString ≫
  • PHP Manual
  • QuickHashIntStringHash
  • This method stores an in-memory hash to disk

QuickHashIntStringHash::saveToFile

(PECL quickhash >= Unknown)

QuickHashIntStringHash::saveToFile — This method stores an in-memory hash to disk

설명

public void QuickHashIntStringHash::saveToFile ( string $filename )

This method stores an existing hash to a file on disk, in the same format that loadFromFile() can read.

인수

filename

The filename of the file to store the hash in.

반환값

값을 반환하지 않습니다.

예제

Example #1 QuickHashIntStringHash::saveToFile() example

<?php
$hash = new QuickHashIntStringHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, "fourty three" ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, "fifty two" ) );

$hash->saveToFile( '/tmp/test.string.hash' );
?>

User Contributed Notes

There are no user contributed notes for this page.


This method returns a serialized version of the hash

  • ≪ QuickHashIntStringHash::saveToFile
  • QuickHashIntStringHash::set ≫
  • PHP Manual
  • QuickHashIntStringHash
  • This method returns a serialized version of the hash

QuickHashIntStringHash::saveToString

(PECL quickhash >= Unknown)

QuickHashIntStringHash::saveToString — This method returns a serialized version of the hash

설명

public string QuickHashIntStringHash::saveToString ( void )

This method returns a serialized version of the hash in the same format that loadFromString() can read.

반환값

This method returns a string containing a serialized format of the hash. Each element is stored as a four byte value in the Endianness that the current system uses.

예제

Example #1 QuickHashIntStringHash::saveToString() example

<?php
$hash = new QuickHashIntStringHash( 1024 );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 4, "thirty four" ) );
var_dump( $hash->exists( 4 ) );
var_dump( $hash->add( 5, "fifty five" ) );

var_dump( $hash->saveToString() );
?>

User Contributed Notes

There are no user contributed notes for this page.


This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

  • ≪ QuickHashIntStringHash::saveToString
  • QuickHashIntStringHash::update ≫
  • PHP Manual
  • QuickHashIntStringHash
  • This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

QuickHashIntStringHash::set

(PECL quickhash >= Unknown)

QuickHashIntStringHash::set — This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist

설명

public int QuickHashIntStringHash::set ( int $key , string $value )

This method tries to update an entry with a new value. In case the entry did not yet exist, it will instead add a new entry. It returns whether the entry was added or update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashIntStringHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

인수

key

The key of the entry to add or update.

value

The value of the entry to add. If a non-string is passed, it will be converted to a string automatically if possible.

반환값

2 if the entry was found and updated, 1 if the entry was newly added or 0 if there was an error.

예제

Example #1 QuickHashIntStringHash::set() example

<?php
$hash = new QuickHashIntStringHash( 1024 );

echo "Set->Add\n";
var_dump( $hash->get( 46692 ) );
var_dump( $hash->set( 46692, "sixteen thousand ninety one" ) );
var_dump( $hash->get( 46692 ) );

echo "Set->Update\n";
var_dump( $hash->set( 46692, "twenty nine thousand nine hundred six" ) );
var_dump( $hash->get( 46692 ) );
?>

위 예제의 출력 예시:

Set->Add
bool(false)
int(2)
string(27) "sixteen thousand ninety one"
Set->Update
int(1)
string(37) "twenty nine thousand nine hundred six"

User Contributed Notes

There are no user contributed notes for this page.


This method updates an entry in the hash with a new value

  • ≪ QuickHashIntStringHash::set
  • Reflection ≫
  • PHP Manual
  • QuickHashIntStringHash
  • This method updates an entry in the hash with a new value

QuickHashIntStringHash::update

(PECL quickhash >= Unknown)

QuickHashIntStringHash::update — This method updates an entry in the hash with a new value

설명

public bool QuickHashIntStringHash::update ( int $key , string $value )

This method updates an entry with a new value, and returns whether the entry was update. If there are duplicate keys, only the first found element will get an updated value. Use QuickHashIntStringHash::CHECK_FOR_DUPES during hash creation to prevent duplicate keys from being part of the hash.

인수

key

The key of the entry to add.

value

The new value for the entry. If a non-string is passed, it will be converted to a string automatically if possible.

반환값

TRUE when the entry was found and updated, and FALSE if the entry was not part of the hash already.

예제

Example #1 QuickHashIntStringHash::update() example

<?php
$hash->add( 161803398, "--" );
$hash->add( 314159265, "a lot" );

echo $hash->get( 161803398 ), "\n";
echo $hash->get( 314159265 ), "\n";

var_dump( $hash->update( 314159265, "a lot plus one" ) );
var_dump( $hash->update( 314159999, "a lot plus one" ) );

echo $hash->get( 161803398 ), "\n";
echo $hash->get( 314159265 ), "\n";
?>

위 예제의 출력 예시:

--
a lot
bool(true)
bool(false)
--
a lot plus one

User Contributed Notes

There are no user contributed notes for this page.


Reflection

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
  • Extending
  • Reflection — The Reflection class
    • Reflection::export — Exports
    • Reflection::getModifierNames — Gets modifier names
  • ReflectionClass — The ReflectionClass class
    • ReflectionClass::__construct — Constructs a ReflectionClass
    • ReflectionClass::export — Exports a class
    • ReflectionClass::getConstant — Gets defined constant
    • ReflectionClass::getConstants — Gets constants
    • ReflectionClass::getConstructor — Gets the constructor of the class
    • ReflectionClass::getDefaultProperties — Gets default properties
    • ReflectionClass::getDocComment — Gets doc comments
    • ReflectionClass::getEndLine — Gets end line
    • ReflectionClass::getExtension — Gets a ReflectionExtension object for the extension which defined the class
    • ReflectionClass::getExtensionName — Gets the name of the extension which defined the class
    • ReflectionClass::getFileName — Gets the filename of the file in which the class has been defined
    • ReflectionClass::getInterfaceNames — Gets the interface names
    • ReflectionClass::getInterfaces — Gets the interfaces
    • ReflectionClass::getMethod — Gets a ReflectionMethod for a class method.
    • ReflectionClass::getMethods — Gets an array of methods
    • ReflectionClass::getModifiers — Gets modifiers
    • ReflectionClass::getName — Gets class name
    • ReflectionClass::getNamespaceName — Gets namespace name
    • ReflectionClass::getParentClass — Gets parent class
    • ReflectionClass::getProperties — Gets properties
    • ReflectionClass::getProperty — Gets a ReflectionProperty for a class's property
    • ReflectionClass::getShortName — Gets short name
    • ReflectionClass::getStartLine — Gets starting line number
    • ReflectionClass::getStaticProperties — Gets static properties
    • ReflectionClass::getStaticPropertyValue — Gets static property value
    • ReflectionClass::getTraitAliases — Returns an array of trait aliases
    • ReflectionClass::getTraitNames — Returns an array of names of traits used by this class
    • ReflectionClass::getTraits — Returns an array of traits used by this class
    • ReflectionClass::hasConstant — Checks if constant is defined
    • ReflectionClass::hasMethod — Checks if method is defined
    • ReflectionClass::hasProperty — Checks if property is defined
    • ReflectionClass::implementsInterface — Implements interface
    • ReflectionClass::inNamespace — Checks if in namespace
    • ReflectionClass::isAbstract — Checks if class is abstract
    • ReflectionClass::isAnonymous — Checks if class is anonymous
    • ReflectionClass::isCloneable — Returns whether this class is cloneable
    • ReflectionClass::isFinal — Checks if class is final
    • ReflectionClass::isInstance — Checks class for instance
    • ReflectionClass::isInstantiable — Checks if the class is instantiable
    • ReflectionClass::isInterface — Checks if the class is an interface
    • ReflectionClass::isInternal — Checks if class is defined internally by an extension, or the core
    • ReflectionClass::isIterateable — Checks if iterateable
    • ReflectionClass::isSubclassOf — Checks if a subclass
    • ReflectionClass::isTrait — Returns whether this is a trait
    • ReflectionClass::isUserDefined — Checks if user defined
    • ReflectionClass::newInstance — Creates a new class instance from given arguments.
    • ReflectionClass::newInstanceArgs — Creates a new class instance from given arguments.
    • ReflectionClass::newInstanceWithoutConstructor — Creates a new class instance without invoking the constructor.
    • ReflectionClass::setStaticPropertyValue — Sets static property value
    • ReflectionClass::__toString — Returns the string representation of the ReflectionClass object.
  • ReflectionZendExtension — The ReflectionZendExtension class
    • ReflectionZendExtension::__clone — Clone handler
    • ReflectionZendExtension::__construct — Constructor
    • ReflectionZendExtension::export — Export
    • ReflectionZendExtension::getAuthor — Gets author
    • ReflectionZendExtension::getCopyright — Gets copyright
    • ReflectionZendExtension::getName — Gets name
    • ReflectionZendExtension::getURL — Gets URL
    • ReflectionZendExtension::getVersion — Gets version
    • ReflectionZendExtension::__toString — To string handler
  • ReflectionExtension — The ReflectionExtension class
    • ReflectionExtension::__clone — Clones
    • ReflectionExtension::__construct — Constructs a ReflectionExtension
    • ReflectionExtension::export — Export
    • ReflectionExtension::getClasses — Gets classes
    • ReflectionExtension::getClassNames — Gets class names
    • ReflectionExtension::getConstants — Gets constants
    • ReflectionExtension::getDependencies — Gets dependencies
    • ReflectionExtension::getFunctions — Gets extension functions
    • ReflectionExtension::getINIEntries — Gets extension ini entries
    • ReflectionExtension::getName — Gets extension name
    • ReflectionExtension::getVersion — Gets extension version
    • ReflectionExtension::info — Print extension info
    • ReflectionExtension::isPersistent — Returns whether this extension is persistent
    • ReflectionExtension::isTemporary — Returns whether this extension is temporary
    • ReflectionExtension::__toString — To string
  • ReflectionFunction — The ReflectionFunction class
    • ReflectionFunction::__construct — Constructs a ReflectionFunction object
    • ReflectionFunction::export — Exports function
    • ReflectionFunction::getClosure — Returns a dynamically created closure for the function
    • ReflectionFunction::invoke — Invokes function
    • ReflectionFunction::invokeArgs — Invokes function args
    • ReflectionFunction::isDisabled — Checks if function is disabled
    • ReflectionFunction::__toString — To string
  • ReflectionFunctionAbstract — The ReflectionFunctionAbstract class
    • ReflectionFunctionAbstract::__clone — Clones function
    • ReflectionFunctionAbstract::getClosureScopeClass — Returns the scope associated to the closure
    • ReflectionFunctionAbstract::getClosureThis — Returns this pointer bound to closure
    • ReflectionFunctionAbstract::getDocComment — Gets doc comment
    • ReflectionFunctionAbstract::getEndLine — Gets end line number
    • ReflectionFunctionAbstract::getExtension — Gets extension info
    • ReflectionFunctionAbstract::getExtensionName — Gets extension name
    • ReflectionFunctionAbstract::getFileName — Gets file name
    • ReflectionFunctionAbstract::getName — Gets function name
    • ReflectionFunctionAbstract::getNamespaceName — Gets namespace name
    • ReflectionFunctionAbstract::getNumberOfParameters — Gets number of parameters
    • ReflectionFunctionAbstract::getNumberOfRequiredParameters — Gets number of required parameters
    • ReflectionFunctionAbstract::getParameters — Gets parameters
    • ReflectionFunctionAbstract::getReturnType — Gets the specified return type of a function
    • ReflectionFunctionAbstract::getShortName — Gets function short name
    • ReflectionFunctionAbstract::getStartLine — Gets starting line number
    • ReflectionFunctionAbstract::getStaticVariables — Gets static variables
    • ReflectionFunctionAbstract::hasReturnType — Checks if the function has a specified return type
    • ReflectionFunctionAbstract::inNamespace — Checks if function in namespace
    • ReflectionFunctionAbstract::isClosure — Checks if closure
    • ReflectionFunctionAbstract::isDeprecated — Checks if deprecated
    • ReflectionFunctionAbstract::isGenerator — Returns whether this function is a generator
    • ReflectionFunctionAbstract::isInternal — Checks if is internal
    • ReflectionFunctionAbstract::isUserDefined — Checks if user defined
    • ReflectionFunctionAbstract::isVariadic — Checks if the function is variadic
    • ReflectionFunctionAbstract::returnsReference — Checks if returns reference
    • ReflectionFunctionAbstract::__toString — To string
  • ReflectionMethod — The ReflectionMethod class
    • ReflectionMethod::__construct — Constructs a ReflectionMethod
    • ReflectionMethod::export — Export a reflection method.
    • ReflectionMethod::getClosure — Returns a dynamically created closure for the method
    • ReflectionMethod::getDeclaringClass — Gets declaring class for the reflected method.
    • ReflectionMethod::getModifiers — Gets the method modifiers
    • ReflectionMethod::getPrototype — Gets the method prototype (if there is one).
    • ReflectionMethod::invoke — Invoke
    • ReflectionMethod::invokeArgs — Invoke args
    • ReflectionMethod::isAbstract — Checks if method is abstract
    • ReflectionMethod::isConstructor — Checks if method is a constructor
    • ReflectionMethod::isDestructor — Checks if method is a destructor
    • ReflectionMethod::isFinal — Checks if method is final
    • ReflectionMethod::isPrivate — Checks if method is private
    • ReflectionMethod::isProtected — Checks if method is protected
    • ReflectionMethod::isPublic — Checks if method is public
    • ReflectionMethod::isStatic — Checks if method is static
    • ReflectionMethod::setAccessible — Set method accessibility
    • ReflectionMethod::__toString — Returns the string representation of the Reflection method object.
  • ReflectionObject — The ReflectionObject class
    • ReflectionObject::__construct — Constructs a ReflectionObject
    • ReflectionObject::export — Export
  • ReflectionParameter — The ReflectionParameter class
    • ReflectionParameter::allowsNull — Checks if null is allowed
    • ReflectionParameter::canBePassedByValue — Returns whether this parameter can be passed by value
    • ReflectionParameter::__clone — Clone
    • ReflectionParameter::__construct — Construct
    • ReflectionParameter::export — Exports
    • ReflectionParameter::getClass — Get the type hinted class
    • ReflectionParameter::getDeclaringClass — Gets declaring class
    • ReflectionParameter::getDeclaringFunction — Gets declaring function
    • ReflectionParameter::getDefaultValue — Gets default parameter value
    • ReflectionParameter::getDefaultValueConstantName — Returns the default value's constant name if default value is constant or null
    • ReflectionParameter::getName — Gets parameter name
    • ReflectionParameter::getPosition — Gets parameter position
    • ReflectionParameter::getType — Gets a parameter's type
    • ReflectionParameter::hasType — Checks if parameter has a type
    • ReflectionParameter::isArray — Checks if parameter expects an array
    • ReflectionParameter::isCallable — Returns whether parameter MUST be callable
    • ReflectionParameter::isDefaultValueAvailable — Checks if a default value is available
    • ReflectionParameter::isDefaultValueConstant — Returns whether the default value of this parameter is constant
    • ReflectionParameter::isOptional — Checks if optional
    • ReflectionParameter::isPassedByReference — Checks if passed by reference
    • ReflectionParameter::isVariadic — Checks if the parameter is variadic
    • ReflectionParameter::__toString — To string
  • ReflectionProperty — The ReflectionProperty class
    • ReflectionProperty::__clone — Clone
    • ReflectionProperty::__construct — Construct a ReflectionProperty object
    • ReflectionProperty::export — Export
    • ReflectionProperty::getDeclaringClass — Gets declaring class
    • ReflectionProperty::getDocComment — Gets the property doc comment
    • ReflectionProperty::getModifiers — Gets modifiers
    • ReflectionProperty::getName — Gets property name
    • ReflectionProperty::getValue — Gets value
    • ReflectionProperty::isDefault — Checks if property is a default property
    • ReflectionProperty::isPrivate — Checks if property is private
    • ReflectionProperty::isProtected — Checks if property is protected
    • ReflectionProperty::isPublic — Checks if property is public
    • ReflectionProperty::isStatic — Checks if property is static
    • ReflectionProperty::setAccessible — Set property accessibility
    • ReflectionProperty::setValue — Set property value
    • ReflectionProperty::__toString — To string
  • ReflectionType — The ReflectionType class
    • ReflectionType::allowsNull — Checks if null is allowed
    • ReflectionType::isBuiltin — Checks if it is a built-in type
    • ReflectionType::__toString — To string
  • ReflectionGenerator — The ReflectionGenerator class
    • ReflectionGenerator::__construct — Constructs a ReflectionGenerator object
    • ReflectionGenerator::getExecutingFile — Gets the file name of the currently executing generator
    • ReflectionGenerator::getExecutingGenerator — Gets the executing Generator object
    • ReflectionGenerator::getExecutingLine — Gets the currently executing line of the generator
    • ReflectionGenerator::getFunction — Gets the function name of the generator
    • ReflectionGenerator::getThis — Gets the $this value of the generator
    • ReflectionGenerator::getTrace — Gets the trace of the executing generator
  • Reflector — The Reflector interface
    • Reflector::export — Exports
    • Reflector::__toString — To string
  • ReflectionException — The ReflectionException class

User Contributed Notes

Anonymous 26-Aug-2010 06:19
Here is a code snippet for some of us who are just beginning with reflection. I have a simple class below with two properties and two methods. We will use reflection classes to populate the properties dynamically and then print them:

<?php

class A
{
    public $one = '';
    public $two = '';

    //Constructor
    public function __construct()
    {
        //Constructor
    }

    //print variable one
    public function echoOne()
    {
        echo $this->one."\n";
    }

    //print variable two
    public function echoTwo()
    {
        echo $this->two."\n";
    }
}

//Instantiate the object
$a = new A();

//Instantiate the reflection object
$reflector = new ReflectionClass('A');

//Now get all the properties from class A in to $properties array
$properties = $reflector->getProperties();

$i =1;
//Now go through the $properties array and populate each property
foreach($properties as $property)
{
    //Populating properties
    $a->{$property->getName()}=$i;
    //Invoking the method to print what was populated
    $a->{"echo".ucfirst($property->getName())}()."\n";

    $i++;
}

?>


소개

PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions, methods and extensions. Additionally, the reflection API offers ways to retrieve doc comments for functions, classes and methods.

Please note that certain parts of the internal API are missing the necessary code to work with the Reflection extension. E.g., an internal PHP class might be missing reflection data for properties. These few cases are considered bugs, however, so they should be discovered and fixed.

User Contributed Notes

There are no user contributed notes for this page.


예약 상수

Each class describes its own constants.

User Contributed Notes

There are no user contributed notes for this page.


예제

Many examples exist within the Reflection documentation, typically within the __construct documentation for each class.

Example #1 Reflection Example from Shell (a Terminal)

$ php --rf strlen
$ php --rc finfo
$ php --re json
$ php --ri dom

위 예제의 출력 예시:

Function [ <internal:Core> function strlen ] {

  - Parameters [1] {
    Parameter #0 [ <required> $str ]
  }
}

Class [ <internal:fileinfo> class finfo ] {

  - Constants [0] {
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [0] {
  }

  - Methods [4] {
    Method [ <internal:fileinfo, ctor> public method finfo ] {

      - Parameters [2] {
        Parameter #0 [ <optional> $options ]
        Parameter #1 [ <optional> $arg ]
      }
    }

    Method [ <internal:fileinfo> public method set_flags ] {

      - Parameters [1] {
        Parameter #0 [ <required> $options ]
      }
    }

    Method [ <internal:fileinfo> public method file ] {

      - Parameters [3] {
        Parameter #0 [ <required> $filename ]
        Parameter #1 [ <optional> $options ]
        Parameter #2 [ <optional> $context ]
      }
    }

    Method [ <internal:fileinfo> public method buffer ] {

      - Parameters [3] {
        Parameter #0 [ <required> $string ]
        Parameter #1 [ <optional> $options ]
        Parameter #2 [ <optional> $context ]
      }
    }
  }
}

Extension [ <persistent> extension #23 json version 1.2.1 ] {

  - Constants [10] {
    Constant [ integer JSON_HEX_TAG ] { 1 }
    Constant [ integer JSON_HEX_AMP ] { 2 }
    Constant [ integer JSON_HEX_APOS ] { 4 }
    Constant [ integer JSON_HEX_QUOT ] { 8 }
    Constant [ integer JSON_FORCE_OBJECT ] { 16 }
    Constant [ integer JSON_ERROR_NONE ] { 0 }
    Constant [ integer JSON_ERROR_DEPTH ] { 1 }
    Constant [ integer JSON_ERROR_STATE_MISMATCH ] { 2 }
    Constant [ integer JSON_ERROR_CTRL_CHAR ] { 3 }
    Constant [ integer JSON_ERROR_SYNTAX ] { 4 }
  }

  - Functions {
    Function [ <internal:json> function json_encode ] {

      - Parameters [2] {
        Parameter #0 [ <required> $value ]
        Parameter #1 [ <optional> $options ]
      }
    }
    Function [ <internal:json> function json_decode ] {

      - Parameters [3] {
        Parameter #0 [ <required> $json ]
        Parameter #1 [ <optional> $assoc ]
        Parameter #2 [ <optional> $depth ]
      }
    }
    Function [ <internal:json> function json_last_error ] {

      - Parameters [0] {
      }
    }
  }
}

dom

DOM/XML => enabled
DOM/XML API Version => 20031129
libxml Version => 2.7.3
HTML Support => enabled
XPath Support => enabled
XPointer Support => enabled
Schema Support => enabled
RelaxNG Support => enabled

User Contributed Notes

spectrum at bigmir dot net 28-Dec-2010 11:11
Simple table generation using Reflection:

<?php

class A
{
    public    $name = 'Vasiliy';
    public    $text = 'Once upon a time';
}

class B
{
    public    $name = 'Had bought a cat';
    public    $text = 'named Valentine';
}

class grid
{
    public function build(  $dataSource, $headers, $fields  )
    {
        $result = '<table border="1">';
        $result .= $this -> make_head( $headers );

        foreach ($dataSource as $source):
            $class_name = get_class($source);
            if ( $class_name != FALSE ):
                $reflector = new ReflectionClass($source);
                echo 'Class "'. $class_name .'" found.<br />';
                $result .= $this -> make_row( $reflector, $fields, $source );
            endif;
        endforeach;

        $result .= '</table>';
        return $result;
    }

    private function make_head( $headers )
    {
        $result = '<tr>';
        foreach ( $headers as $header):
            $result .= "<th>$header</th>";
        endforeach;
        $result .= '</tr>';
        return $result;
    }

    private function make_row( $reflector, $fields, $source )
    {
        $result = '<tr>';
        foreach ( $fields as $field ):
            if ( $reflector -> hasProperty($field) ):
                $property = $reflector -> getProperty($field);
                $result .= '<td>'. $property -> getValue($source) .'</td>';
            endif;
        endforeach;
        $result .= '</tr>';
        return $result;
    }
}

$A = new A;
$B = new B;
$C = 'Test';

$dataSource = array( $A, $B, $C );
$headers = array( 'H1', 'H2' );
$fields = array( 'name', 'text' );

$grid = new grid;
$table = $grid -> build( $dataSource, $headers, $fields );
echo $table;

?>
Kovin 18-May-2009 11:00
If you want to use method closures and don't have PHP 5.3, perhaps you find useful the following function
<?php
    function get_method_closure($object,$method_name){
        if(method_exists(get_class($object),$method_name)){
            $func            = create_function( '',
                            '
                                $args            = func_get_args();
                                static $object    = NULL;

                                /*
                                * Check if this function is being called to set the static $object, which
                                * containts scope information to invoke the method
                                */
                                if(is_null($object) && count($args) && get_class($args[0])=="'.get_class($object).'"){
                                    $object = $args[0];
                                    return;
                                }

                                if(!is_null($object)){
                                    return call_user_func_array(array($object,"'.$method_name.'"),$args);
                                }else{
                                    return FALSE;
                                }'
                            );

            //Initialize static $object
            $func($object);

            //Return closure
            return $func;
        }else{
            return false;
        }
    }
?>
Here is how you would use it:
<?php
class foo{
    public function bar($foo){
        return strtolower($foo);
    }
}

$foo = new foo();
$f = get_method_closure($foo,'bar');
echo $f("BAR");//Prints 'bar'
?>


Extending

If you want to create specialized versions of the built-in classes (say, for creating colorized HTML when being exported, having easy-access member variables instead of methods or having utility methods), you may go ahead and extend them.

Example #1 Extending the built-in classes

<?php
/**
 * My Reflection_Method class
 */
class My_Reflection_Method extends ReflectionMethod
{
    public $visibility = array();

    public function __construct($o, $m)
    {
        parent::__construct($o, $m);
        $this->visibility = Reflection::getModifierNames($this->getModifiers());
    }
}

/**
 * Demo class #1
 *
 */
class T {
    protected function x() {}
}

/**
 * Demo class #2
 *
 */
class U extends T {
    function x() {}
}

// Print out information
var_dump(new My_Reflection_Method('U', 'x'));
?>

위 예제의 출력 예시:

object(My_Reflection_Method)#1 (3) {
  ["visibility"]=>
  array(1) {
    [0]=>
    string(6) "public"
  }
  ["name"]=>
  string(1) "x"
  ["class"]=>
  string(1) "U"
}
Caution

If you're overwriting the constructor, remember to call the parent's constructor before any code you insert. Failing to do so will result in the following: Fatal error: Internal error: Failed to retrieve the reflection object

User Contributed Notes

khelaz at gmail dot com 28-Apr-2011 08:12
Extending class ReflectionFunction to get source code of function

<?php
class Custom_Reflection_Function extends ReflectionFunction {

    public function getSource() {
        if( !file_exists( $this->getFileName() ) ) return false;

        $start_offset = ( $this->getStartLine() - 1 );
        $end_offset   = ( $this->getEndLine() - $this->getStartLine() ) + 1;

        return join( '', array_slice( file( $this->getFileName() ), $start_offset, $end_offset ) );
    }
}
?>


The Reflection class

(PHP 5, PHP 7)

소개

The reflection class.

클래스 개요

Reflection {
/* 메소드 */
public static string export ( Reflector $reflector [, bool $return = false ] )
public static array getModifierNames ( int $modifiers )
}

Table of Contents

  • Reflection::export — Exports
  • Reflection::getModifierNames — Gets modifier names

User Contributed Notes

apmithani at yahoo dot com 26-Aug-2010 06:05
Here is a code snippet for some of us who are just beginning with reflection. I have a simple class below with two properties and two methods. We will use reflection classes to populate the properties dynamically and then print them:

<?php

class A
{
    public $one = '';
    public $two = '';

    //Constructor
    public function __construct()
    {
        //Constructor
    }

    //print variable one
    public function echoOne()
    {
        echo $this->one."\n";
    }

    //print variable two
    public function echoTwo()
    {
        echo $this->two."\n";
    }
}

//Instantiate the object
$a = new A();

//Instantiate the reflection object
$reflector = new ReflectionClass('A');

//Now get all the properties from class A in to $properties array
$properties = $reflector->getProperties();

$i =1;
//Now go through the $properties array and populate each property
foreach($properties as $property)
{
    //Populating properties
    $a->{$property->getName()}=$i;
    //Invoking the method to print what was populated
    $a->{"echo".ucfirst($property->getName())}()."\n";

    $i++;
}

?>


Exports

  • ≪ Reflection
  • Reflection::getModifierNames ≫
  • PHP Manual
  • Reflection
  • Exports

Reflection::export

(PHP 5, PHP 7)

Reflection::export — Exports

설명

public static string Reflection::export ( Reflector $reflector [, bool $return = false ] )

Exports a reflection.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

reflector

내보낼 reflection

return

TRUE로 설정하면 export를 반환하고, 반대의 경우는 버립니다. FALSE로 설정(기본값)은 반대를 수행합니다.

반환값

return 인수를 TRUE로 설정하면, export를 string으로 반환합니다. 그렇지 않으면 NULL을 반환합니다.

참고

  • Reflection::getModifierNames() - Gets modifier names

User Contributed Notes

There are no user contributed notes for this page.


Gets modifier names

  • ≪ Reflection::export
  • ReflectionClass ≫
  • PHP Manual
  • Reflection
  • Gets modifier names

Reflection::getModifierNames

(PHP 5, PHP 7)

Reflection::getModifierNames — Gets modifier names

설명

public static array Reflection::getModifierNames ( int $modifiers )

Gets modifier names.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

modifiers

The modifiers to get, which is from a numeric value.

반환값

An array of modifier names.

참고

  • Reflection::export() - Exports

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionClass class

(PHP 5, PHP 7)

소개

The ReflectionClass class reports information about a class.

클래스 개요

ReflectionClass implements Reflector {
/* 상수 */
const integer IS_IMPLICIT_ABSTRACT = 16 ;
const integer IS_EXPLICIT_ABSTRACT = 32 ;
const integer IS_FINAL = 64 ;
/* 프로퍼티 */
public $name ;
/* 메소드 */
public __construct ( mixed $argument )
public static string export ( mixed $argument [, bool $return = false ] )
public mixed getConstant ( string $name )
public array getConstants ( void )
public ReflectionMethod getConstructor ( void )
public array getDefaultProperties ( void )
public string getDocComment ( void )
public int getEndLine ( void )
public ReflectionExtension getExtension ( void )
public string getExtensionName ( void )
public string getFileName ( void )
public array getInterfaceNames ( void )
public array getInterfaces ( void )
public ReflectionMethod getMethod ( string $name )
public array getMethods ([ int $filter ] )
public int getModifiers ( void )
public string getName ( void )
public string getNamespaceName ( void )
public ReflectionClass getParentClass ( void )
public array getProperties ([ int $filter ] )
public ReflectionProperty getProperty ( string $name )
public string getShortName ( void )
public int getStartLine ( void )
public array getStaticProperties ( void )
public mixed getStaticPropertyValue ( string $name [, mixed &$def_value ] )
public array getTraitAliases ( void )
public array getTraitNames ( void )
public array getTraits ( void )
public bool hasConstant ( string $name )
public bool hasMethod ( string $name )
public bool hasProperty ( string $name )
public bool implementsInterface ( string $interface )
public bool inNamespace ( void )
public bool isAbstract ( void )
public bool isAnonymous ( void )
public bool isCloneable ( void )
public bool isFinal ( void )
public bool isInstance ( object $object )
public bool isInstantiable ( void )
public bool isInterface ( void )
public bool isInternal ( void )
public bool isIterateable ( void )
public bool isSubclassOf ( string $class )
public bool isTrait ( void )
public bool isUserDefined ( void )
public object newInstance ( mixed $args [, mixed $... ] )
public object newInstanceArgs ([ array $args ] )
public object newInstanceWithoutConstructor ( void )
public void setStaticPropertyValue ( string $name , string $value )
public string __toString ( void )
}

프로퍼티

name

Name of the class. Read-only, throws ReflectionException in attempt to write.

예약 상수

ReflectionClass Modifiers

ReflectionClass::IS_IMPLICIT_ABSTRACT

Indicates class that is abstract because it has some abstract methods.

ReflectionClass::IS_EXPLICIT_ABSTRACT

Indicates class that is abstract because of its definition.

ReflectionClass::IS_FINAL

Indicates final class.

Table of Contents

  • ReflectionClass::__construct — Constructs a ReflectionClass
  • ReflectionClass::export — Exports a class
  • ReflectionClass::getConstant — Gets defined constant
  • ReflectionClass::getConstants — Gets constants
  • ReflectionClass::getConstructor — Gets the constructor of the class
  • ReflectionClass::getDefaultProperties — Gets default properties
  • ReflectionClass::getDocComment — Gets doc comments
  • ReflectionClass::getEndLine — Gets end line
  • ReflectionClass::getExtension — Gets a ReflectionExtension object for the extension which defined the class
  • ReflectionClass::getExtensionName — Gets the name of the extension which defined the class
  • ReflectionClass::getFileName — Gets the filename of the file in which the class has been defined
  • ReflectionClass::getInterfaceNames — Gets the interface names
  • ReflectionClass::getInterfaces — Gets the interfaces
  • ReflectionClass::getMethod — Gets a ReflectionMethod for a class method.
  • ReflectionClass::getMethods — Gets an array of methods
  • ReflectionClass::getModifiers — Gets modifiers
  • ReflectionClass::getName — Gets class name
  • ReflectionClass::getNamespaceName — Gets namespace name
  • ReflectionClass::getParentClass — Gets parent class
  • ReflectionClass::getProperties — Gets properties
  • ReflectionClass::getProperty — Gets a ReflectionProperty for a class's property
  • ReflectionClass::getShortName — Gets short name
  • ReflectionClass::getStartLine — Gets starting line number
  • ReflectionClass::getStaticProperties — Gets static properties
  • ReflectionClass::getStaticPropertyValue — Gets static property value
  • ReflectionClass::getTraitAliases — Returns an array of trait aliases
  • ReflectionClass::getTraitNames — Returns an array of names of traits used by this class
  • ReflectionClass::getTraits — Returns an array of traits used by this class
  • ReflectionClass::hasConstant — Checks if constant is defined
  • ReflectionClass::hasMethod — Checks if method is defined
  • ReflectionClass::hasProperty — Checks if property is defined
  • ReflectionClass::implementsInterface — Implements interface
  • ReflectionClass::inNamespace — Checks if in namespace
  • ReflectionClass::isAbstract — Checks if class is abstract
  • ReflectionClass::isAnonymous — Checks if class is anonymous
  • ReflectionClass::isCloneable — Returns whether this class is cloneable
  • ReflectionClass::isFinal — Checks if class is final
  • ReflectionClass::isInstance — Checks class for instance
  • ReflectionClass::isInstantiable — Checks if the class is instantiable
  • ReflectionClass::isInterface — Checks if the class is an interface
  • ReflectionClass::isInternal — Checks if class is defined internally by an extension, or the core
  • ReflectionClass::isIterateable — Checks if iterateable
  • ReflectionClass::isSubclassOf — Checks if a subclass
  • ReflectionClass::isTrait — Returns whether this is a trait
  • ReflectionClass::isUserDefined — Checks if user defined
  • ReflectionClass::newInstance — Creates a new class instance from given arguments.
  • ReflectionClass::newInstanceArgs — Creates a new class instance from given arguments.
  • ReflectionClass::newInstanceWithoutConstructor — Creates a new class instance without invoking the constructor.
  • ReflectionClass::setStaticPropertyValue — Sets static property value
  • ReflectionClass::__toString — Returns the string representation of the ReflectionClass object.

User Contributed Notes

YoungOfCthulhu 21-Sep-2015 11:20
It is also possible to do reflection from within the class and get the methods for instance.

<?php

class test
{
    private $test = "";

    public function setTest($test)
    {
        $this->test = $test;
        return $this;
    }

    public function getTest()
    {
        return $this->test;
    }

    public function run(){
        $class = new ReflectionClass('test');
        $methods = $class->getMethods();
        var_dump($methods);
    }
}

Called from another php file you get the proper result:
array(3) {
  [0]=>
  &object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(8) "setTest"
    ["class"]=>
    string(4) "test"
  }
  [1]=>
  &object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(8) "getTest"
    ["class"]=>
    string(4) "test"
  }
  [2]=>
  &object(ReflectionMethod)#5 (2) {
    ["name"]=>
    string(3) "run"
    ["class"]=>
    string(4) "test"
  }
}
Anonymous 17-May-2013 03:45
Reflecting an alias will give you a reflection of the resolved class.

<?php

class X {

}

class_alias('X','Y');
class_alias('Y','Z');
$z = new ReflectionClass('Z');
echo $z->getName(); // X

?>
Anonymous 11-Nov-2011 06:58
Unserialized reflection class cause error.

<?php
/**
 * abc
 */
class a{}

$ref = new ReflectionClass('a');
$ref = unserialize(serialize($ref));
var_dump($ref);
var_dump($ref->getDocComment());

// object(ReflectionClass)#2 (1) {
//   ["name"]=>
//   string(1) "a"
// }
// PHP Fatal error:  ReflectionClass::getDocComment(): Internal error: Failed to retrieve the reflection object
?>
danbettles at yahoo dot co dot uk 29-Mar-2009 11:48
To reflect on a namespaced class in PHP 5.3, you must always specify the fully qualified name of the class - even if you've aliased the containing namespace using a "use" statement.

So instead of:

<?php
use App\Core as Core;
$oReflectionClass = new ReflectionClass('Core\Singleton');
?>

You would type:

<?php
use App\Core as Core;
$oReflectionClass = new ReflectionClass('App\Core\Singleton');
?>


Constructs a ReflectionClass

  • ≪ ReflectionClass
  • ReflectionClass::export ≫
  • PHP Manual
  • ReflectionClass
  • Constructs a ReflectionClass

ReflectionClass::__construct

(PHP 5, PHP 7)

ReflectionClass::__construct — Constructs a ReflectionClass

설명

public ReflectionClass::__construct ( mixed $argument )

Constructs a new ReflectionClass object.

인수

argument

Either a string containing the name of the class to reflect, or an object.

반환값

Returns constructed ReflectionClass instance.

오류/예외

Throws ReflectionException if the class to reflect does not exist.

예제

Example #1 Basic usage ReflectionClass

<?php
Reflection::export(new ReflectionClass('Exception'));
?>

위 예제의 출력 예시:

Class [ <internal:Core> class Exception ] {

  - Constants [0] {
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [7] {
    Property [ <default> protected $message ]
    Property [ <default> private $string ]
    Property [ <default> protected $code ]
    Property [ <default> protected $file ]
    Property [ <default> protected $line ]
    Property [ <default> private $trace ]
    Property [ <default> private $previous ]
  }

  - Methods [10] {
    Method [ <internal:Core> final private method __clone ] {
    }

    Method [ <internal:Core, ctor> public method __construct ] {

      - Parameters [3] {
        Parameter #0 [ <optional> $message ]
        Parameter #1 [ <optional> $code ]
        Parameter #2 [ <optional> $previous ]
      }
    }

    Method [ <internal:Core> final public method getMessage ] {
    }

    Method [ <internal:Core> final public method getCode ] {
    }

    Method [ <internal:Core> final public method getFile ] {
    }

    Method [ <internal:Core> final public method getLine ] {
    }

    Method [ <internal:Core> final public method getTrace ] {
    }

    Method [ <internal:Core> final public method getPrevious ] {
    }

    Method [ <internal:Core> final public method getTraceAsString ] {
    }

    Method [ <internal:Core> public method __toString ] {
    }
  }
}

참고

  • ReflectionObject::__construct() - Constructs a ReflectionObject
  • Constructors

User Contributed Notes

danbettles at yahoo dot co dot uk 23-May-2015 03:47
To reflect on a namespaced class in PHP 5.3, you must always specify the fully qualified name of the class - even if you've aliased the containing namespace using a "use" statement.

So instead of:

<?php
use App\Core as Core;
$oReflectionClass = new ReflectionClass('Core\Singleton');
?>

You would type:

<?php
use App\Core as Core;
$oReflectionClass = new ReflectionClass('App\Core\Singleton');
?>
nulled 18-Dec-2013 12:42
if (is_file($classfile))
            require_once $classfile;

if (! class_exists($classname, false))
            exit('ERROR: ' . $classname . ' is not defined as a Class');

The above code is useful to tell if the class was defined.  You could also use another commentors method using Try Catch Exceptions.  But, if you do not use try blocks much, the above function based method works just fine.

From there, you can than call:

$class = new ReflectionClass($classname);

        if (! $class->isSubclassOf('PanelCommon'))
            exit("ERROR: {$classname} must extends PanelCommon");

        if (! $class->isUserDefined())
            exit("ERROR: {$classname} must be user defined and not internal to PHP");

        if (! $class->IsInstantiable())
            exit("ERROR: {$classname} must be IsInstantiable and not an Interface or Abstract class");

        if (! $class->hasMethod('home'))
            exit("ERROR: {$classname} lacks required method/function home()");

Forforth and so on.
me [at] klay [dot] me 29-Oct-2013 08:09
Example of usage:

    public static function getClassData($class)
    {
        // Trying to create a new object of ReflectionClass class
        $class = new ReflectionClass($class);

        $details = sprintf('%s - %s%s%s%s%s%s%s%s',
            $class->getName(),
            $class->isInternal()     ? 'internal class,' : 'user-defined class,',
            $class->isTrait()        ? '  is trait,'  : '',
            $class->isInterface()    ? '  is interface,'  : '',
            $class->isAbstract()     ? '  is abstract,'  : '',
            $class->isFinal()        ? '  is final,'  : '',
            $class->isCloneable()    ? '  is cloneable,'  : '',
            $class->isInstantiable() ? ' is instantiable,'  : '',
            $class->isIterateable()  ? ' is iterable  : ''
        );

        return '<pre class="debug">' . rtrim($details, ',') . '</pre>';
    }
gafisher at griasolutions dot com 30-Jan-2012 10:23
Running the following code on Windows Vista (I know, I know), PHP 5.3.9, the ReflectionClass constructor actually throws a ReflectionException when the desired class cannot be instantiated:

<?php
    try {
        $ReflectedClass = new ReflectionClass('NonExist');
    } catch (LogicException $Exception) {
        die('Not gonna make it in here...');
    } catch (ReflectionException $Exception) {
        die('Your class does not exist!');
    }
?>
cspray at gmail dot com 13-Jun-2011 04:14
Useful to know that if you pass a string into the construct and the class cannot be instantiated for some reason a SPL LogicException will be thrown.

This code was ran on a Mac OS X 10.6.7, AMP, PHP 5.3+

<?php

    //  index.php
    try {
        $ReflectedClass = new ReflectionClass('NonExist');
    } catch (LogicException $logicDuh) {
        print_r($logicDuh);
    }

?>

Will return a deeply nested array full of useful information about the error.
ivo at jansch dot nl 24-Jan-2010 01:16
It's very useful to know that you can also use the ReflectionClass to inspect interfaces, even thouth Interfaces are not classes. Example:

<?php

  interface Edible
  {
    public function eat();
  }

  $refl = new ReflectionClass("Edible");
  $methods = $refl->getMethods();
?>

[Edit by danbrown AT php DOT net - Contains a bugfix by (dbl AT bnet DOT com) on 18-AUG-2010 with the following message: "underline had to be removed for it to work ( new Reflection_Class -> new ReflectionClass )"]


Exports a class

  • ≪ ReflectionClass::__construct
  • ReflectionClass::getConstant ≫
  • PHP Manual
  • ReflectionClass
  • Exports a class

ReflectionClass::export

(PHP 5, PHP 7)

ReflectionClass::export — Exports a class

설명

public static string ReflectionClass::export ( mixed $argument [, bool $return = false ] )

Exports a reflected class.

인수

argument

내보낼 reflection

return

TRUE로 설정하면 export를 반환하고, 반대의 경우는 버립니다. FALSE로 설정(기본값)은 반대를 수행합니다.

반환값

return 인수를 TRUE로 설정하면, export를 string으로 반환합니다. 그렇지 않으면 NULL을 반환합니다.

예제

Example #1 Basic usage of ReflectionClass::export()

<?php
class Apple {
    public $var1;
    public $var2 = 'Orange';

    public function type() {
        return 'Apple';
    }
}
ReflectionClass::export('Apple');
?>

위 예제의 출력 예시:

Class [ <user> class Apple ] {
  @@ php shell code 1-8

  - Constants [0] {
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [2] {
    Property [ <default> public $var1 ]
    Property [ <default> public $var2 ]
  }

  - Methods [1] {
    Method [ <user> public method type ] {
      @@ php shell code 5 - 7
    }
  }
}

참고

  • ReflectionClass::getName() - Gets class name

User Contributed Notes

There are no user contributed notes for this page.


Gets defined constant

  • ≪ ReflectionClass::export
  • ReflectionClass::getConstants ≫
  • PHP Manual
  • ReflectionClass
  • Gets defined constant

ReflectionClass::getConstant

(PHP 5, PHP 7)

ReflectionClass::getConstant — Gets defined constant

설명

public mixed ReflectionClass::getConstant ( string $name )

Gets the defined constant.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

name

Name of the constant.

반환값

Value of the constant.

참고

  • ReflectionClass::getConstants() - Gets constants

User Contributed Notes

aurelien dot tisserand at wavesoftware dot ch 09-Nov-2013 12:27
If the $name constant doesn't exist in the targetted class the function returns bool(false), not empty nor null but false (you to test with "==="

$constFounded = false ;
$this->currentlangClass = new ReflectionClass($langFile);
$this->currentlangClass->getConstant($constant);
if($myConst !== false){
              $constFounded = true ;
}
Bhimsen 01-Jun-2012 02:46
The "getconstant" method can be used to get the value associated with a constant
of a particular class that you are examining.
The following code snippet shows this:

<?php
    class Test{
        const ONE = "Number one";
        const TWO = "Number two";
    }

    $obj = new ReflectionClass( "Test" );
    echo $obj->getconstant( "ONE" )."\n";
    echo $obj->getconstant( "TWO" )."\n";

?>

output:
Number one
Number two


Gets constants

  • ≪ ReflectionClass::getConstant
  • ReflectionClass::getConstructor ≫
  • PHP Manual
  • ReflectionClass
  • Gets constants

ReflectionClass::getConstants

(PHP 5, PHP 7)

ReflectionClass::getConstants — Gets constants

설명

public array ReflectionClass::getConstants ( void )

Gets defined constants from a class.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

An array of constants. Constant name in key, constant value in value.

참고

  • ReflectionClass::getConstant() - Gets defined constant

User Contributed Notes

davide dot renzi at gmail dot com 16-Jul-2014 08:21
If you want to return the constants defined inside a class then you can also define an internal method as follows:

<?php
class myClass {
    const NONE = 0;
    const REQUEST = 100;
    const AUTH = 101;

    // others...

    static function getConstants() {
        $oClass = new ReflectionClass(__CLASS__);
        return $oClass->getConstants();
    }
}
?>
shgninc at gmail dot com 20-Nov-2013 07:00
I use a functions to do somthing base on the class constant name as below. This example maybe helpful for everybody.
<?php
public function renderData($question_type = NULL, $data = array()) {
        $types = array();
        $qt = new ReflectionClass(questionType);
        $types = $qt->getConstants();
        if ($type = array_search($question_type, $types)){
                //.....Do somthing
}
}
?>
djhob1972 at yahoo dot com dot au 16-Dec-2009 02:42
I was trying to determine how to get a var_dump of constants that are within an interface.  Thats right, not using any classes but the interface itself.

Along my travels I found it quite interesting that the ReflectionClass along with a direct call to the interface will also dump its constants.  Perfect!!!!

This was using PHP 5.3.1 and my example as below:-

1st File:

constants.php

<?php
<?php>

interface MyConstants
{
    // --------------------------
    // Programmatic Level
    // --------------------------
    const DEBUG_MODE_ACTIVE       = FALSE;
    const PHP_VERSION_REQUIREMENT = "5.1.2";
}
?>

=======
Second file:
=======

test.php

<?php>
include_once ("constants.php");

$oClass = new ReflectionClass ('MyConstants');
$array = $oClass->getConstants ();
var_dump ($array);
unset ($oClass);
?>

what you would get from the command line:-

?:\???\htdocs\????>php test.php
array(2) {
  ["DEBUG_MODE_ACTIVE"]=> bool(false)
  ["PHP_VERSION_REQUIREMENT"]=> string(5) "5.1.2"

But as you can see this can work quite well to your advantage in many ways so I truely hope this helps someone else with a similar headache in the future to come!

Enjoy!


Gets the constructor of the class

  • ≪ ReflectionClass::getConstants
  • ReflectionClass::getDefaultProperties ≫
  • PHP Manual
  • ReflectionClass
  • Gets the constructor of the class

ReflectionClass::getConstructor

(PHP 5, PHP 7)

ReflectionClass::getConstructor — Gets the constructor of the class

설명

public ReflectionMethod ReflectionClass::getConstructor ( void )

Gets the constructor of the reflected class.

인수

이 함수는 인수가 없습니다.

반환값

A ReflectionMethod object reflecting the class' constructor, or NULL if the class has no constructor.

예제

Example #1 Basic usage of ReflectionClass::getConstructor()

<?php
$class = new ReflectionClass('ReflectionClass');
$constructor = $class->getConstructor();
var_dump($constructor);
?>

위 예제의 출력:

object(ReflectionMethod)#2 (2) {
  ["name"]=>
  string(11) "__construct"
  ["class"]=>
  string(15) "ReflectionClass"
}

참고

  • ReflectionClass::getName() - Gets class name

User Contributed Notes

jochem at drecomm dot nl 13-May-2011 05:13
Old constructors also count as contructor:

<?php

class SomeClass {

    function SomeClass($some_arg) {
    }

}

$refl = new ReflectionClass('SomeClass');

var_dump($refl->isInstantiable()); // bool(true)

echo $refl->getConstructor();

/* OUTPUT:
Method [ <user, ctor> public method SomeClass ] {
  @@ /var/www/vhosts/api.example.com/httpdocs/testRefl.php 5 - 6

  - Parameters [1] {
    Parameter #0 [ <required> $some_arg ]
  }
}
*/

?>

Some more behavior:

<?php

class SomeClass {

   function funcA($arg1, $arg2) {

   }

}

$refl = new ReflectionClass('SomeClass');

var_dump($refl->isInstantiable()); // bool(true)

var_dump($refl->getConstructor()); // NULL

/* --------------- */

class AnotherClass {

   private function __construct() {
   }

   function funcB($arg1, $arg2) {

   }

}

$refl = new ReflectionClass('AnotherClass');

var_dump($refl->isInstantiable()); // bool(false)

echo $refl->getConstructor();
/*
Method [ <user, ctor> private method __construct ] {
  @@ /testRefl.php 22 - 23
}
*/

?>

Tested on PHP 5.2.4
Rob McVey 16-Jul-2010 04:47
Just posting some example code for anyone wanting to mess around with this stuff:

<?php

class Say
{
    private $what_to_say;
    public function __construct($no_default, $word = "Hello World", $options = array('a', 'b'))
    {
        $this->what_to_say = $word;
    }

    public function speak()
    {
        echo $this->what_to_say;
    }
}

$class = new ReflectionClass('Say');

$constructor = $class->getConstructor();

echo $constructor;

/*     OUTPUTS:

Method [ <user, ctor> public method __construct ] {
  @@ /reflect.php 6 - 9

  - Parameters [3] {
    Parameter #0 [ <required> $no_default ]
    Parameter #1 [ <optional> $word = 'Hello World' ]
    Parameter #2 [ <optional> $options = Array ]
  }
}

*/

$parameters = $constructor->getParameters();

var_export($parameters);

/*    OUTPUT:

array (
  0 =>
  ReflectionParameter::__set_state(array(
     'name' => 'no_default',
  )),
  1 =>
  ReflectionParameter::__set_state(array(
     'name' => 'word',
  )),
  2 =>
  ReflectionParameter::__set_state(array(
     'name' => 'options',
  )),
)

*/

$nl = "\n";
echo "$nl\tParameters$nl";
foreach($parameters as $param)
{
    echo "****** $" . $param->name . " ******$nl";
    echo "Nullable:\t\t" . $param->allowsNull() . $nl
         ."Default Value:\t\t";
    echo ($param->isDefaultValueAvailable()) ? $param->getDefaultValue() : "None";
    echo $nl ."Is Array:\t\t";
    echo ($param->isArray()) ? "Yes" : "No";
    echo $nl . "Optional:\t\t";
    echo ($param->isOptional()) ? "Yes" : "No";
    echo $nl;
}

/*    OUTPUT:

    Parameters
****** $no_default ******
Nullable:        1
Default Value:    None
Is Array:        No
Optional:        No
****** $word ******
Nullable:        1
Default Value:    Hello World
Is Array:        No
Optional:        Yes
****** $options ******
Nullable:        1
Default Value:    Array
Is Array:        No
Optional:        Yes

*/

?>

To clarify the possibly confusing behavior of ReflectionParemeter::isArray(), it will return true if the parameter has type hinting:

<?php
...
public function __construct($no_default, $word = "Hello World", array $options = array('a', 'b'))
...
?>

Calling isArray() will now return true for the $options parameter


Gets default properties

  • ≪ ReflectionClass::getConstructor
  • ReflectionClass::getDocComment ≫
  • PHP Manual
  • ReflectionClass
  • Gets default properties

ReflectionClass::getDefaultProperties

(PHP 5, PHP 7)

ReflectionClass::getDefaultProperties — Gets default properties

설명

public array ReflectionClass::getDefaultProperties ( void )

Gets default properties from a class (including inherited properties).

Note:

This method only works for static properties when used on internal classes. The default value of a static class property can not be tracked when using this method on user defined classes.

인수

이 함수는 인수가 없습니다.

반환값

An array of default properties, with the key being the name of the property and the value being the default value of the property or NULL if the property doesn't have a default value. The function does not distinguish between static and non static properties and does not take visibility modifiers into account.

예제

Example #1 ReflectionClass::getDefaultProperties() example

<?php
class Bar {
    protected $inheritedProperty = 'inheritedDefault';
}

class Foo extends Bar {
    public $property = 'propertyDefault';
    private $privateProperty = 'privatePropertyDefault';
    public static $staticProperty = 'staticProperty';
    public $defaultlessProperty;
}

$reflectionClass = new ReflectionClass('Foo');
var_dump($reflectionClass->getDefaultProperties());
?>

위 예제의 출력:

array(5) {
   ["staticProperty"]=>
   string(14) "staticProperty"
   ["property"]=>
   string(15) "propertyDefault"
   ["privateProperty"]=>
   string(22) "privatePropertyDefault"
   ["defaultlessProperty"]=>
   NULL
   ["inheritedProperty"]=>
   string(16) "inheritedDefault"
}

참고

  • ReflectionClass::getProperties() - Gets properties
  • ReflectionClass::getStaticProperties() - Gets static properties
  • ReflectionClass::getProperty() - Gets a ReflectionProperty for a class's property

User Contributed Notes

articice at ua dot fm 17-Mar-2016 09:15
runaurufu is not quite right, get_class_vars() does not return protected params, while this one does.

Thus it's extremely useful when having an abstract parent class and protected properties overriding in children.
For example, I use a class factory and one of the children has some static test methods that still need to output a paramether name, like $this->name, etc. With this example code, one can use static::getNotStaticProperty('name'), but not get_class_vars('name').

Try it:

trait static_reflector {
    /*
     * a purely static function that returns default properties of the non-static instance of the same class
     */
    static protected function getNonStaticProperty($key) {
        $me = get_class();
        $reflectionClass = new \ReflectionClass($me);
        $properties_list = $reflectionClass->getDefaultProperties();
        if (isset($properties_list[$key]))
            return $var_name = $properties_list[$key];
        else throw new RuntimeException("BUG: Unable to reflect non-static property '{$key}' from default properties of class {$me}");
    }
}

class a {

    use \static_reflector;

    protected $key_a = 'test ok';

    public static function test() {
        echo static::getNonStaticProperty('key_a')."\n";

        try {
            print static::getNonStaticProperty('key_b');
            echo "FAIL No exception thrown";
        } catch (RuntimeException $e) {
            echo "OK ".$e->getMessage();
        }

    }
}

echo get_class_vars('a')['key_a'];
a::test();

this will return:
Notice: Undefined index: key_a in ...
test ok
OK BUG: Unable to reflect non-static property 'key_b' from default properties of class a

ps: Yes, this is copied from a unit test.
runaurufu AT gmail.com 04-Aug-2011 10:02
Worth noting that it will not return private parameters of parent class...
so it works exactly as get_class_vars or get_object_vars
captainjester at hotmail dot com 21-Jan-2010 12:20
This will return all properties in a class and any parent classes.  The array will have keys set to the property names and empty values.


Gets doc comments

  • ≪ ReflectionClass::getDefaultProperties
  • ReflectionClass::getEndLine ≫
  • PHP Manual
  • ReflectionClass
  • Gets doc comments

ReflectionClass::getDocComment

(PHP 5 >= 5.1.0, PHP 7)

ReflectionClass::getDocComment — Gets doc comments

설명

public string ReflectionClass::getDocComment ( void )

Gets doc comments from a class.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The doc comment if it exists, otherwise FALSE

예제

Example #1 ReflectionClass::getDocComment() example

<?php
/**
* A test class
*
* @param  foo bar
* @return baz
*/
class TestClass { }

$rc = new ReflectionClass('TestClass');
var_dump($rc->getDocComment())
?>

위 예제의 출력:

string(55) "/**
* A test class
*
* @param  foo bar
* @return baz
*/"

참고

  • ReflectionClass::getName() - Gets class name

User Contributed Notes

geoffreybans at gmail dot com 07-Jan-2016 11:15
This code can help you get the contents of a docBlock in array format beginning with the @symbol and ignoring the (*) asterists.

class Home {

    /**
     *This method loads the homepage
     *@param int $id The user id
     *@throws \Exception If the user id doesn't exist
     *@return void
     */
    public function index( $id)
    {

         #...your code here

      }

}

$object = new Home();

//get the comment string
$comment_string= (new ReflectionClass($object))->getMethod('index')->getdoccomment();

//define the regular expression pattern to use for string matching
$pattern = "#(@[a-zA-Z]+\s*[a-zA-Z0-9, ()_].*)#";

//perform the regular expression on the string provided
preg_match_all($pattern, $comment_string, $matches, PREG_PATTERN_ORDER);

echo "<pre>"; print_r($matches);

//this outputs
Array
(
    [0] => Array
        (
            [0] => @param int $id The user id
            [1] => @throws \Exception If the user id doesn't exist
            [2] => @return void
        )

    [1] => Array
        (
            [0] => @param int $id The user id
            [1] => @throws \Exception If the user id doesn't exist
            [2] => @return void
        )

)

//you can then be able to access the particular string values by index
gabe at fijiwebdesign dot com 24-Aug-2014 08:46
You can also get the docblock definitions for the defined methods of a class as such:

<?php
/**
 * This is an Example class
 */
class Example
{
    /**
     * This is an example function
     */
    public function fn()
    {
        // void
    }
}

$reflector = new ReflectionClass('Example');

// to get the Class DocBlock
echo $reflector->getDocComment()

// to get the Method DocBlock
$reflector->getMethod('fn')->getDocComment();

?>
sun 28-Jun-2014 11:11
Note that \ReflectionClass::getDocComment() ignores all other PHP code and all white-space between the last encountered T_DOC_COMMENT and the class/element definition.

The only exceptions appear to be T_NAMESPACE declarations and T_FUNCTION definitions.

<?php
/**
 * Before namespace.
 */
namespace Foo;

 /**
 * After namespace.
   */

// ^^ contains excessive leading + trailing white-space.
use Bar\Baz;
const FOO = 'BAR';
$ns = 'bar';
# function foo() {}
$a = 2 + 1;
#/** what? */
// ^^ A single-line T_DOC_COMMENT is invisible when commented out.
count(array());

class Foo {
}

$reflector = new \ReflectionClass('Foo\Foo');
var_dump($reflector->getDocComment());
?>
yields, despite all the garbage in between:

string(28) "/**
 * After namespace.
   */"

To sum up:

1. If there are multiple doc comments, the last encountered applies.

2. Removing the "After namespace." docblock yields FALSE.
   (The namespace delimits the scope.)

3. Uncommenting the function definition yields FALSE.
   (The doc comment applies to the function instead.)

4. Despite being an own language construct, the "const" constant declaration does not delimit the scope.

5. Any leading and trailing white-space before and after the T_DOC_COMMENT ("/**...*/") is ignored, but the entire string content within (including all white-space) is consumed literally/verbatim.

[PHP 5.4.29]
uramihsayibok, gmail, com 16-Sep-2010 06:50
According to what I can find in the PHP (5.3.2) source code, getDocComment will return the doc comment as the parser found it.
The doc comment (T_DOC_COMMENT) must begin with a /** - that's two asterisks, not one. The comment continues until the first */. A normal multi-line comment /*...*/ (T_COMMENT) does not count as a doc comment.

The doc comment itself includes those five characters, so <?php substr($doccomment, 3, -2) ?> will get you what's inside. A call to trim() after is recommended.
joe dot scylla at gmail dot com 14-Oct-2009 05:23
If you're using a bytecode cache like eAccelerator this method will return FALSE even if there is a properly formatted Docblock. It looks like the information required by this method gets stripped out by the bytecode cache.


Gets end line

  • ≪ ReflectionClass::getDocComment
  • ReflectionClass::getExtension ≫
  • PHP Manual
  • ReflectionClass
  • Gets end line

ReflectionClass::getEndLine

(PHP 5, PHP 7)

ReflectionClass::getEndLine — Gets end line

설명

public int ReflectionClass::getEndLine ( void )

Gets end line number from a user-defined class definition.

인수

이 함수는 인수가 없습니다.

반환값

The ending line number of the user defined class, or FALSE if unknown.

예제

Example #1 ReflectionClass::getEndLine() example

<?php
// Test Class
class TestClass { }

$rc = new ReflectionClass('TestClass');

echo $rc->getEndLine();
?>

위 예제의 출력:

3

참고

  • ReflectionClass::getStartLine() - Gets starting line number

User Contributed Notes

There are no user contributed notes for this page.


Gets a ReflectionExtension object for the extension which defined the class

  • ≪ ReflectionClass::getEndLine
  • ReflectionClass::getExtensionName ≫
  • PHP Manual
  • ReflectionClass
  • Gets a ReflectionExtension object for the extension which defined the class

ReflectionClass::getExtension

(PHP 5, PHP 7)

ReflectionClass::getExtension — Gets a ReflectionExtension object for the extension which defined the class

설명

public ReflectionExtension ReflectionClass::getExtension ( void )

Gets a ReflectionExtension object for the extension which defined the class.

인수

이 함수는 인수가 없습니다.

반환값

A ReflectionExtension object representing the extension which defined the class, or NULL for user-defined classes.

예제

Example #1 Basic usage of ReflectionClass::getExtension()

<?php
$class = new ReflectionClass('ReflectionClass');
$extension = $class->getExtension();
var_dump($extension);
?>

위 예제의 출력:

object(ReflectionExtension)#2 (1) {
  ["name"]=>
  string(10) "Reflection"
}

참고

  • ReflectionClass::getExtensionName() - Gets the name of the extension which defined the class

User Contributed Notes

There are no user contributed notes for this page.


Gets the name of the extension which defined the class

  • ≪ ReflectionClass::getExtension
  • ReflectionClass::getFileName ≫
  • PHP Manual
  • ReflectionClass
  • Gets the name of the extension which defined the class

ReflectionClass::getExtensionName

(PHP 5, PHP 7)

ReflectionClass::getExtensionName — Gets the name of the extension which defined the class

설명

public string ReflectionClass::getExtensionName ( void )

Gets the name of the extension which defined the class.

인수

이 함수는 인수가 없습니다.

반환값

The name of the extension which defined the class, or FALSE for user-defined classes.

예제

Example #1 Basic usage of ReflectionClass::getExtensionName()

<?php
$class = new ReflectionClass('ReflectionClass');
$extension = $class->getExtensionName();
var_dump($extension);
?>

위 예제의 출력:

string(10) "Reflection"

참고

  • ReflectionClass::getExtension() - Gets a ReflectionExtension object for the extension which defined the class

User Contributed Notes

There are no user contributed notes for this page.


Gets the filename of the file in which the class has been defined

  • ≪ ReflectionClass::getExtensionName
  • ReflectionClass::getInterfaceNames ≫
  • PHP Manual
  • ReflectionClass
  • Gets the filename of the file in which the class has been defined

ReflectionClass::getFileName

(PHP 5, PHP 7)

ReflectionClass::getFileName — Gets the filename of the file in which the class has been defined

설명

public string ReflectionClass::getFileName ( void )

Gets the filename of the file in which the class has been defined.

인수

이 함수는 인수가 없습니다.

반환값

Returns the filename of the file in which the class has been defined. If the class is defined in the PHP core or in a PHP extension, FALSE is returned.

참고

  • ReflectionClass::getExtensionName() - Gets the name of the extension which defined the class

User Contributed Notes

There are no user contributed notes for this page.


Gets the interface names

  • ≪ ReflectionClass::getFileName
  • ReflectionClass::getInterfaces ≫
  • PHP Manual
  • ReflectionClass
  • Gets the interface names

ReflectionClass::getInterfaceNames

(PHP 5 >= 5.2.0, PHP 7)

ReflectionClass::getInterfaceNames — Gets the interface names

설명

public array ReflectionClass::getInterfaceNames ( void )

Get the interface names.

인수

이 함수는 인수가 없습니다.

반환값

A numerical array with interface names as the values.

예제

Example #1 ReflectionClass::getInterfaceNames() example

<?php
interface Foo { }

interface Bar { }

class Baz implements Foo, Bar { }

$rc1 = new ReflectionClass("Baz");

print_r($rc1->getInterfaceNames());
?>

위 예제의 출력 예시:

Array
(
    [0] => Foo
    [1] => Bar
)

참고

  • ReflectionClass::getInterfaces() - Gets the interfaces

User Contributed Notes

pinpin 27-Mar-2011 01:11
It seems the interface names are actually listed in a defined order:
- "extends" takes precedence over "implements" (i.e. first will be interfaces from (implemented in) the parent class (if any), then interfaces implemented in the class itself)
- when multiple interfaces are implemented at one time/level, it can be:
+ from an "implements" : they're listed in the defined order
+ from an "extends" (a class extends another class which implements multiple interfaces; or an interface extends multiple interfaces) : they're listed in REVERSE order

<?php
interface Foo {}
interface Bar {}
interface Other {}
interface Foobar extends Foo, Bar {}
interface Barfoo extends Bar, Foo {}

class Test1 implements Foo, Bar {}
class Test2 implements Bar, Foo {}

class Test3 extends Test1 {}
class Test4 extends Test2 {}

class Test5 extends Test1 implements Other {}

class Test6 implements Foobar, Other {}
class TestO implements Other {}
class Test7 extends TestO implements Barfoo {}

$r=new ReflectionClass('Test1');
print_r($r->getInterfaceNames()); // Foo, Bar

$r=new ReflectionClass('Test2');
print_r($r->getInterfaceNames()); // Bar, Foo

$r=new ReflectionClass('Test3');
print_r($r->getInterfaceNames()); // Bar, Foo

$r=new ReflectionClass('Test4');
print_r($r->getInterfaceNames()); // Foo, Bar

$r=new ReflectionClass('Test5');
print_r($r->getInterfaceNames()); // Bar, Foo, Other

$r=new ReflectionClass('Test6');
print_r($r->getInterfaceNames()); // Foobar, Bar, Foo, Other

$r=new ReflectionClass('Test7');
print_r($r->getInterfaceNames()); // Other, Barfoo, Foo, Bar
?>


Gets the interfaces

  • ≪ ReflectionClass::getInterfaceNames
  • ReflectionClass::getMethod ≫
  • PHP Manual
  • ReflectionClass
  • Gets the interfaces

ReflectionClass::getInterfaces

(PHP 5, PHP 7)

ReflectionClass::getInterfaces — Gets the interfaces

설명

public array ReflectionClass::getInterfaces ( void )

Gets the interfaces.

인수

이 함수는 인수가 없습니다.

반환값

An associative array of interfaces, with keys as interface names and the array values as ReflectionClass objects.

예제

Example #1 ReflectionClass::getInterfaces() example

<?php
interface Foo { }

interface Bar { }

class Baz implements Foo, Bar { }

$rc1 = new ReflectionClass("Baz");

print_r($rc1->getInterfaces());
?>

위 예제의 출력 예시:

Array
(
    [Foo] => ReflectionClass Object
        (
            [name] => Foo
        )

    [Bar] => ReflectionClass Object
        (
            [name] => Bar
        )

)

참고

  • ReflectionClass::getInterfaceNames() - Gets the interface names

User Contributed Notes

There are no user contributed notes for this page.


Gets a ReflectionMethod for a class method.

  • ≪ ReflectionClass::getInterfaces
  • ReflectionClass::getMethods ≫
  • PHP Manual
  • ReflectionClass
  • Gets a ReflectionMethod for a class method.

ReflectionClass::getMethod

(PHP 5, PHP 7)

ReflectionClass::getMethod — Gets a ReflectionMethod for a class method.

설명

public ReflectionMethod ReflectionClass::getMethod ( string $name )

Gets a ReflectionMethod for a class method.

인수

name

The method name to reflect.

반환값

A ReflectionMethod.

오류/예외

A ReflectionException if the method does not exist.

예제

Example #1 Basic usage of ReflectionClass::getMethod()

<?php
$class = new ReflectionClass('ReflectionClass');
$method = $class->getMethod('getMethod');
var_dump($method);
?>

위 예제의 출력:

object(ReflectionMethod)#2 (2) {
  ["name"]=>
  string(9) "getMethod"
  ["class"]=>
  string(15) "ReflectionClass"
}

참고

  • ReflectionClass::getMethods() - Gets an array of methods

User Contributed Notes

Christopher Turner 24-Mar-2011 04:04
If the method doesn't exist within the class that the ReflectionClass is reflecting an Exception is thrown.
Jarrod Nettles 22-Dec-2010 09:58
If you ever need to get the type hint of a parameter in a method use this.

<?php

//Target our class
$reflector = new ReflectionClass('MyClass');

//Get the parameters of a method
$parameters = $reflector->getMethod('FireCannon')->getParameters();

//Loop through each parameter and get the type
foreach($parameters as $param)
{
     //Before you call getClass() that class must be defined!
     echo $param->getClass()->name;
}

?>


Gets an array of methods

  • ≪ ReflectionClass::getMethod
  • ReflectionClass::getModifiers ≫
  • PHP Manual
  • ReflectionClass
  • Gets an array of methods

ReflectionClass::getMethods

(PHP 5, PHP 7)

ReflectionClass::getMethods — Gets an array of methods

설명

public array ReflectionClass::getMethods ([ int $filter ] )

Gets an array of methods for the class.

인수

filter

Filter the results to include only methods with certain attributes. Defaults to no filtering.

Any bitwise disjunction of ReflectionMethod::IS_STATIC,
ReflectionMethod::IS_PUBLIC,
ReflectionMethod::IS_PROTECTED,
ReflectionMethod::IS_PRIVATE,
ReflectionMethod::IS_ABSTRACT,
ReflectionMethod::IS_FINAL,
so that all methods with any of the given
attributes will be returned.

Note: Note that other bitwise operations, for instance ~ will not work as expected. In other words, it is not possible to retrieve all non-static methods, for example.

반환값

An array of ReflectionMethod objects reflecting each method.

예제

Example #1 Basic usage of ReflectionClass::getMethods()

<?php
class Apple {
    public function firstMethod() { }
    final protected function secondMethod() { }
    private static function thirdMethod() { }
}

$class = new ReflectionClass('Apple');
$methods = $class->getMethods();
var_dump($methods);
?>

위 예제의 출력:

array(3) {
  [0]=>
  &object(ReflectionMethod)#2 (2) {
    ["name"]=>
    string(11) "firstMethod"
    ["class"]=>
    string(5) "Apple"
  }
  [1]=>
  &object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(12) "secondMethod"
    ["class"]=>
    string(5) "Apple"
  }
  [2]=>
  &object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(11) "thirdMethod"
    ["class"]=>
    string(5) "Apple"
  }
}

Example #2 Filtering results from ReflectionClass::getMethods()

<?php
class Apple {
    public function firstMethod() { }
    final protected function secondMethod() { }
    private static function thirdMethod() { }
}

$class = new ReflectionClass('Apple');
$methods = $class->getMethods(ReflectionMethod::IS_STATIC | ReflectionMethod::IS_FINAL);
var_dump($methods);
?>

위 예제의 출력:

array(2) {
  [0]=>
  &object(ReflectionMethod)#2 (2) {
    ["name"]=>
    string(12) "secondMethod"
    ["class"]=>
    string(5) "Apple"
  }
  [1]=>
  &object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(11) "thirdMethod"
    ["class"]=>
    string(5) "Apple"
  }
}

참고

  • ReflectionClass::getMethod() - Gets a ReflectionMethod for a class method.
  • get_class_methods() - 클래스 메쏘드명을 얻습니다

User Contributed Notes

tom at r dot je 12-Jun-2014 02:40
ReflectionClass::getMethods() sorts the methods by class (lowest in the inheritance tree first) then by the order they are defined in the class definition:

<?php
class A {
    public function method1() {

    }

    public function method2() {

    }
}

class B extends A {

    public function method3() {

    }

    public function method4() {

    }
}

$class = new ReflectionClass('B');
print_r($class->getMethods());
?>

This will output:

Array
(
    [0] => ReflectionMethod Object
        (
            [name] => method3
            [class] => B
        )

    [1] => ReflectionMethod Object
        (
            [name] => method4
            [class] => B
        )

    [2] => ReflectionMethod Object
        (
            [name] => method1
            [class] => A
        )

    [3] => ReflectionMethod Object
        (
            [name] => method2
            [class] => A
        )

)
x_atrix at yahoo dot com 10-Jul-2013 02:00
Note, for ReflectionClass::getMethods() not all methods in a final class are final, just the ones that have explicit modifier.
If you want to use an and operator for the filter, here is a simple implementation
<?php
final class Apple {
    public function publicMethod() { }
    public final function publicFinalMethod() { }
    protected final function protectedFinalMethod() { }
    private static function privateStaticMethod() { }
}

class MyReflection extends ReflectionClass {
    public function __construct($argument) {
        parent::__construct($argument);
    }

    /**
     * (non-PHPdoc)
     * @see ReflectionClass::getMethods()
     */
    public function getMethods($filter = null, $useAndOperator = true) {
        if ($useAndOperator !== true) {
            return parent::getMethods($filter);
        }

        $methods = parent::getMethods($filter);
        $results = array();

        foreach ($methods as $method) {
            if (($method->getModifiers() & $filter) === $filter) {
                $results[] = $method;
            }
        }

        return $results;
    }
}

$class = new MyReflection('Apple');
$methods = $class->getMethods(ReflectionMethod::IS_FINAL | ReflectionMethod::IS_PUBLIC);
var_dump($methods);

$methods = $class->getMethods(ReflectionMethod::IS_FINAL | ReflectionMethod::IS_PUBLIC, false);
var_dump($methods);
?>

Result:
array(1) {
  [0]=>
  object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(17) "publicFinalMethod"
    ["class"]=>
    string(5) "Apple"
  }
}

array(3) {
  [0]=>
  &object(ReflectionMethod)#5 (2) {
    ["name"]=>
    string(12) "publicMethod"
    ["class"]=>
    string(5) "Apple"
  }
  [1]=>
  &object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(17) "publicFinalMethod"
    ["class"]=>
    string(5) "Apple"
  }
  [2]=>
  &object(ReflectionMethod)#6 (2) {
    ["name"]=>
    string(20) "protectedFinalMethod"
    ["class"]=>
    string(5) "Apple"
  }
}
deminy at deminy dot net 22-Jun-2011 06:24
Method ReflectionClass::getMethods doesn't work constantly across different versions of PHP. For following code piece

<?php
class Dummy implements Iterator
{
    public function current () {}
    public function next () {}
    public function key () {}
    public function valid () {}
    public function rewind () {}
}

$reflection = new ReflectionClass('Dummy');
$aMethods = $reflection->getMethods();
echo '# of methods: ', count($aMethods), "\n";
?>

, it outputs "# of methods: 10" on PHP 5.2.14 and PHP 5.2.17, including all methods defined in the class itself and in the interface no matter if a method has been implemented or overridden; however, it returns "# of methods: 5" on PHP 5.3.5. Based on some other tests did by my colleagues, I assume it also returns "# of methods: 5" on PHP 5.2.10 and PHP 5.3.6.


Gets modifiers

  • ≪ ReflectionClass::getMethods
  • ReflectionClass::getName ≫
  • PHP Manual
  • ReflectionClass
  • Gets modifiers

ReflectionClass::getModifiers

(PHP 5, PHP 7)

ReflectionClass::getModifiers — Gets modifiers

설명

public int ReflectionClass::getModifiers ( void )

Returns a bitfield of the access modifiers for this class.

인수

이 함수는 인수가 없습니다.

반환값

Returns bitmask of modifier constants.

참고

  • ReflectionClass::getProperties() - Gets properties

User Contributed Notes

Nicola Pietroluongo 26-Feb-2015 10:17
It's the amount of memory allocation in bit.
By default many systems allocate 1048576 bit (1 Megabit).

/* Example 1 */

class Test {}
$classTest = new Test();
$reflectionClass = new \ReflectionClass('Test');
$classModifiers = $reflectionClass->getModifiers();
var_export($classModifiers);

//output
1048576

/* Example 2 */

class Test {}
//the constructor is missing
$reflectionClass = new \ReflectionClass('Test');
$classModifiers = $reflectionClass->getModifiers();
var_export($classModifiers);

//output
0


Gets class name

  • ≪ ReflectionClass::getModifiers
  • ReflectionClass::getNamespaceName ≫
  • PHP Manual
  • ReflectionClass
  • Gets class name

ReflectionClass::getName

(PHP 5, PHP 7)

ReflectionClass::getName — Gets class name

설명

public string ReflectionClass::getName ( void )

Gets the class name.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The class name.

예제

Example #1 ReflectionClass::getName() example

<?php
namespace A\B;

class Foo { }

$function = new \ReflectionClass('stdClass');

var_dump($function->inNamespace());
var_dump($function->getName());
var_dump($function->getNamespaceName());
var_dump($function->getShortName());

$function = new \ReflectionClass('A\\B\\Foo');

var_dump($function->inNamespace());
var_dump($function->getName());
var_dump($function->getNamespaceName());
var_dump($function->getShortName());
?>

위 예제의 출력:

bool(false)
string(8) "stdClass"
string(0) ""
string(8) "stdClass"

bool(true)
string(7) "A\B\Foo"
string(3) "A\B"
string(3) "Foo"

참고

  • ReflectionClass::getNamespaceName() - Gets namespace name

User Contributed Notes

There are no user contributed notes for this page.


Gets namespace name

  • ≪ ReflectionClass::getName
  • ReflectionClass::getParentClass ≫
  • PHP Manual
  • ReflectionClass
  • Gets namespace name

ReflectionClass::getNamespaceName

(PHP 5 >= 5.3.0, PHP 7)

ReflectionClass::getNamespaceName — Gets namespace name

설명

public string ReflectionClass::getNamespaceName ( void )

Gets the namespace name.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The namespace name.

예제

Example #1 ReflectionClass::getNamespaceName() example

<?php
namespace A\B;

class Foo { }

$class = new \ReflectionClass('stdClass');

var_dump($class->inNamespace());
var_dump($class->getName());
var_dump($class->getNamespaceName());
var_dump($class->getShortName());

$class = new \ReflectionClass('A\\B\\Foo');

var_dump($class->inNamespace());
var_dump($class->getName());
var_dump($class->getNamespaceName());
var_dump($class->getShortName());
?>

위 예제의 출력:

bool(false)
string(8) "stdClass"
string(0) ""
string(8) "stdClass"

bool(true)
string(7) "A\B\Foo"
string(3) "A\B"
string(3) "Foo"

참고

  • ReflectionClass::getParentClass() - Gets parent class
  • namespaces

User Contributed Notes

francois 14-Aug-2011 01:08
If the object does not belong to a namespace, an empty string is returned


Gets parent class

  • ≪ ReflectionClass::getNamespaceName
  • ReflectionClass::getProperties ≫
  • PHP Manual
  • ReflectionClass
  • Gets parent class

ReflectionClass::getParentClass

(PHP 5, PHP 7)

ReflectionClass::getParentClass — Gets parent class

설명

public ReflectionClass ReflectionClass::getParentClass ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

A ReflectionClass.

참고

  • ReflectionClass::__construct() - Constructs a ReflectionClass

User Contributed Notes

isaac dot z dot foster at gmail dot com 16-Jun-2013 06:02
Quick correction, the code for getting all parent classes below has a "typo", you need to reset the $class variable to the parent class instance, otherwise it just endlessly loops:

        $class = new ReflectionClass('classname');

        $parents = array();

        while ($parent = $class->getParentClass()) {
            $parents[] = $parent->getName();
            $class = $parent;
        }

        echo "Parents: " . implode(", ", $parents);
havelangep at hotmail dot com 16-Dec-2010 10:26
Here is a "replacement" for is_a that will additionally look both into the extended classes and in the implemented interfaces

<?php
/**
     * Check if a class extends or implements a specific class/interface
     * @param string $search The class or interface name to look for
     * @param string $className The class name of the object to compare to
     * @return bool
     */
    function IsExtendsOrImplements( $search, $className ) {
        $class = new ReflectionClass( $className );
        if( false === $class ) {
            return false;
        }
        do {
            $name = $class->getName();
            if( $search == $name ) {
                return true;
            }
            $interfaces = $class->getInterfaceNames();
            if( is_array( $interfaces ) && in_array( $search, $interfaces )) {
                return true;
            }
            $class = $class->getParentClass();
        } while( false !== $class );
        return false;
    }
?>
jochem at drecomm dot nl 18-Nov-2010 01:34
When you want to find all parents (parent, parent of parent, parent of parent's parent and so on) try:

<?php
$class = new ReflectionClass('whatever');

$parents = array();

while ($parent = $class->getParentClass()) {
    $parents[] = $parent->getName();
}

echo "Parents: " . implode(", ", $parents);
?>

ReflectionClass::getParentClass() can return a ReflectionClass object of the parent class or false if no parent.

(PHP Version 5.1.6)


Gets properties

  • ≪ ReflectionClass::getParentClass
  • ReflectionClass::getProperty ≫
  • PHP Manual
  • ReflectionClass
  • Gets properties

ReflectionClass::getProperties

(PHP 5, PHP 7)

ReflectionClass::getProperties — Gets properties

설명

public array ReflectionClass::getProperties ([ int $filter ] )

Retrieves reflected properties.

인수

filter

The optional filter, for filtering desired property types. It's configured using the ReflectionProperty constants, and defaults to all property types.

반환값

An array of ReflectionProperty objects.

예제

Example #1 ReflectionClass::getProperties() filtering example

This example demonstrates usage of the optional filter parameter, where it essentially skips private properties.

<?php
class Foo {
    public    $foo  = 1;
    protected $bar  = 2;
    private   $baz  = 3;
}

$foo = new Foo();

$reflect = new ReflectionClass($foo);
$props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);

foreach ($props as $prop) {
    print $prop->getName() . "\n";
}

var_dump($props);

?>

위 예제의 출력 예시:

foo
bar
array(2) {
  [0]=>
  object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(3) "foo"
    ["class"]=>
    string(3) "Foo"
  }
  [1]=>
  object(ReflectionProperty)#4 (2) {
    ["name"]=>
    string(3) "bar"
    ["class"]=>
    string(3) "Foo"
  }
}

참고

  • ReflectionClass::getProperty() - Gets a ReflectionProperty for a class's property
  • ReflectionProperty
  • ReflectionProperty modifier constants

User Contributed Notes

postmaster at greg0ire dot fr 08-Jul-2014 06:23
The code in the first example actually does get inherited properties with at least php 5.5.9 . I don't know if / when this behavior changed.

Here is the output:
    array(2) {
     [0] =>
     class ReflectionProperty#2 (2) {
       public $name =>
       string(2) "a2"
       public $class =>
       string(2) "AA"
     }
     [1] =>
     class ReflectionProperty#3 (2) {
       public $name =>
       string(2) "a1"
       public $class =>
       string(1) "A"
     }
    }
Yzmir Ramirez 16-Oct-2010 05:53
Looks like you can access public, protected, private variables by casting the object to an array (useful for Unit Testing).  However casting to an array still won't allow you access to protected and private static variables.

In PHP 5.3.0+ use ReflectionProperty::setAccessable(true);

<?php

echo "PHP Version: ".phpversion()."\n";

class Foo {
    public    $foo  = 'public';
    protected $bar  = 'protected';
    private   $baz  = 'private';

    public static    $sfoo  = 'public static';
    protected static $sbar  = 'protected static';
    private static   $sbaz  = 'private static';

    const COO = 'const';

}

$obj = new Foo;

$arr = (array)$obj;

print_r($arr);

echo "Accessing Public Static: ".Foo::$sfoo."\n";
// echo Foo::$sbar."\n"; // Fatal error: Cannot access protected property Foo::$sbar
// echo Foo::$sbaz."\n"; // Fatal error: Cannot access private property Foo::$sbaz
echo "Accessing Constant: ".Foo::COO."\n";
?>

PHP Version: 5.2.12
Array
(
    [foo] => public
    [*bar] => protected
    [Foobaz] => private
)
Accessing Public Static: public static
Accessing Constant: const

PHP Version: 5.1.6
Array
(
    [foo] => public
    [*bar] => protected
    [Foobaz] => private
)
Accessing Public Static: public static
Accessing Constant: const
Jeff Hunter 10-Oct-2009 02:30
It appears the filter parameter accepts a long value that is one of ReflectionProperty::IS_STATIC, ReflectionProperty::IS_PUBLIC, ReflectionProperty::IS_PROTECTED, or ReflectionProperty::IS_PRIVATE as a way to limit the returned properties.  Flags may be combined by adding them together.

Example:

<?php
class Foo {
  public $alpha = 1;
  protected $beta = 2;
  private $gamma = 3;

  public function listProperties() {
    $reflect = new ReflectionObject($this);
    foreach ($reflect->getProperties(ReflectionProperty::IS_PUBLIC + ReflectionProperty::IS_PROTECTED) as $prop) {
      print $prop->getName() . "\n";
    }
  }
}

$foo = new Foo();
$foo->listProperties();
?>

will output:

  alpha
  beta
mcurtis 28-Jan-2009 11:41
It should be noted that the 'filter' parameter in the getProperties(filter) method is expected to be of type long.  Not sure why, but it doesn't function as a way of passing in a string to fetch a subset of properties by string match.
muratyaman at gmail dot com 22-Jan-2009 04:47
Some may find this useful.

<?php
/**
 * Recursive function to get an associative array of class properties by property name => ReflectionProperty() object
 * including inherited ones from extended classes
 * @param string $className Class name
 * @param string $types Any combination of <b>public, private, protected, static</b>
 * @return array
 */
function getClassProperties($className, $types='public'){
    $ref = new ReflectionClass($className);
    $props = $ref->getProperties();
    $props_arr = array();
    foreach($props as $prop){
        $f = $prop->getName();

        if($prop->isPublic() and (stripos($types, 'public') === FALSE)) continue;
        if($prop->isPrivate() and (stripos($types, 'private') === FALSE)) continue;
        if($prop->isProtected() and (stripos($types, 'protected') === FALSE)) continue;
        if($prop->isStatic() and (stripos($types, 'static') === FALSE)) continue;

        $props_arr[$f] = $prop;
    }
    if($parentClass = $ref->getParentClass()){
        $parent_props_arr = getClassProperties($parentClass->getName());//RECURSION
        if(count($parent_props_arr) > 0)
            $props_arr = array_merge($parent_props_arr, $props_arr);
    }
    return $props_arr;
}

//USAGE

class A{
  public $a1;

    function abc(){
        //do something
    }
}

class AA extends A{
    public $a2;

    function edf(){
        //do something
    }
}

class AAA extends AA{
  //may not have extra properties, but may have extra methods
    function ghi(){
        //ok
    }
}

//$ref = new ReflectionClass('AAA'); $props = $ref->getProperties();//This will get no properties!
$props_arr = getClassProperties('AAA', 'public');//Use this
var_dump($props_arr);
/*
OUTPUT on PHP5.2.6:
array
  'a1' =>
    object(ReflectionProperty)[4]
      public 'name' => string 'a1' (length=2)
      public 'class' => string 'AAA' (length=3)
  'a2' =>
    object(ReflectionProperty)[3]
      public 'name' => string 'a2' (length=2)
      public 'class' => string 'AAA' (length=3)

*/

?>
david dot thalmann at gmail dot com 06-Jan-2009 05:19
With PHP 5.3 protected or private properties are easy to access with setAccessible(). However, it's sometimes needed (e.g. Unit Tests) and here is a workaround for getValue():

<?php

$class = new ReflectionClass('SomeClass');
$props = $class->getProperties();
// $propsStatic = $class->getStaticProperties();

$myPrivatePropertyValue = $props['aPrivateProperty'];

?>

Note that it wont work if you access the property directly with getProperty().


Gets a ReflectionProperty for a class's property

  • ≪ ReflectionClass::getProperties
  • ReflectionClass::getShortName ≫
  • PHP Manual
  • ReflectionClass
  • Gets a ReflectionProperty for a class's property

ReflectionClass::getProperty

(PHP 5, PHP 7)

ReflectionClass::getProperty — Gets a ReflectionProperty for a class's property

설명

public ReflectionProperty ReflectionClass::getProperty ( string $name )

Gets a ReflectionProperty for a class's property.

인수

name

The property name.

반환값

A ReflectionProperty.

예제

Example #1 Basic usage of ReflectionClass::getProperty()

<?php
$class = new ReflectionClass('ReflectionClass');
$property = $class->getProperty('name');
var_dump($property);
?>

위 예제의 출력:

object(ReflectionProperty)#2 (2) {
  ["name"]=>
  string(4) "name"
  ["class"]=>
  string(15) "ReflectionClass"
}

참고

  • ReflectionClass::getProperties() - Gets properties

User Contributed Notes

dohpaz42 16-Jun-2015 01:16
Accessing private properties is possible, but care must be taken if that private property was defined lower into the inheritance chain. For example, if class A extends class B, and class B defines a private property called 'foo', getProperty will throw a ReflectionException.

Instead, you can loop over getParentClass until it returns false to look for the private property, at which point you can access and/or modify its value as needed.
eric at naeseth dot com 03-Aug-2011 12:38
If the class doesn't have a property with the given name, a ReflectionException will be raised.


Gets short name

  • ≪ ReflectionClass::getProperty
  • ReflectionClass::getStartLine ≫
  • PHP Manual
  • ReflectionClass
  • Gets short name

ReflectionClass::getShortName

(PHP 5 >= 5.3.0, PHP 7)

ReflectionClass::getShortName — Gets short name

설명

public string ReflectionClass::getShortName ( void )

Gets the short name of the class, the part without the namespace.

인수

이 함수는 인수가 없습니다.

반환값

The class short name.

예제

Example #1 ReflectionClass::getShortName() example

<?php
namespace A\B;

class Foo { }

$function = new \ReflectionClass('stdClass');

var_dump($function->inNamespace());
var_dump($function->getName());
var_dump($function->getNamespaceName());
var_dump($function->getShortName());

$function = new \ReflectionClass('A\\B\\Foo');

var_dump($function->inNamespace());
var_dump($function->getName());
var_dump($function->getNamespaceName());
var_dump($function->getShortName());
?>

위 예제의 출력:

bool(false)
string(8) "stdClass"
string(0) ""
string(8) "stdClass"

bool(true)
string(7) "A\B\Foo"
string(3) "A\B"
string(3) "Foo"

참고

  • ReflectionClass::getName() - Gets class name

User Contributed Notes

There are no user contributed notes for this page.


Gets starting line number

  • ≪ ReflectionClass::getShortName
  • ReflectionClass::getStaticProperties ≫
  • PHP Manual
  • ReflectionClass
  • Gets starting line number

ReflectionClass::getStartLine

(PHP 5, PHP 7)

ReflectionClass::getStartLine — Gets starting line number

설명

public int ReflectionClass::getStartLine ( void )

Get the starting line number.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The starting line number, as an integer.

참고

  • ReflectionClass::getEndLine() - Gets end line

User Contributed Notes

There are no user contributed notes for this page.


Gets static properties

  • ≪ ReflectionClass::getStartLine
  • ReflectionClass::getStaticPropertyValue ≫
  • PHP Manual
  • ReflectionClass
  • Gets static properties

ReflectionClass::getStaticProperties

(PHP 5, PHP 7)

ReflectionClass::getStaticProperties — Gets static properties

설명

public array ReflectionClass::getStaticProperties ( void )

Get the static properties.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The static properties, as an array.

참고

  • ReflectionClass::getStaticPropertyValue() - Gets static property value
  • ReflectionClass::setStaticPropertyValue() - Sets static property value

User Contributed Notes

joao dot felipe dot c dot b at gmail dot com 24-Mar-2016 10:06
getStaticProperties return a set of the property itself. It's diferente from getProperties(ReflectionProperty::IS_STATIC) because it return a set of ReflectionProperty class.
jlennox @ google mail 08-May-2010 07:03
I had the need to recursive merge the results from a subclass with all of it's parents, and this was the resulting code:

<?php
function GetStaticPropertiesRecursive($class) {
    $currentClass = $class;
    $joinedProperties = array();
    do {
        $reflection = new ReflectionClass($class);
        $staticProperties = $reflection->getStaticProperties();
        foreach ($staticProperties as $name => $value) {
            if (is_array($value)) {
                if (isset($joinedProperties[$name]))
                    $joinedProperties[$name] = array_merge($value, $joinedProperties[$name]);
                else
                    $joinedProperties[$name] = $value;
            } else {
                if (isset($joinedProperties[$name]))
                    $joinedProperties[$name][] = $value;
                else
                    $joinedProperties[$name] = array($value);
            }
        }
    } while ($class = get_parent_class($class));
    return $joinedProperties;
}

Using this function:
class base {
    public static $Test = array("foo1", "foo2");
}
class sub extends base {
    public static $Test = "sub";
}

print_r(GetStaticPropertiesRecursive("sub"));
?>

That outputs:
Array
(
    [Test] => Array
        (
            [0] => foo1
            [1] => foo2
            [2] => sub
        )

)

The merge follows the rules of array_merge on duplicate keys.


Gets static property value

  • ≪ ReflectionClass::getStaticProperties
  • ReflectionClass::getTraitAliases ≫
  • PHP Manual
  • ReflectionClass
  • Gets static property value

ReflectionClass::getStaticPropertyValue

(PHP 5 >= 5.1.0, PHP 7)

ReflectionClass::getStaticPropertyValue — Gets static property value

설명

public mixed ReflectionClass::getStaticPropertyValue ( string $name [, mixed &$def_value ] )

Gets the value of a static property on this class.

인수

name

The name of the static property for which to return a value.

def_value

A default value to return in case the class does not declare a static property with the given name. If the property does not exist and this argument is omitted, a ReflectionException is thrown.

반환값

The value of the static property.

예제

Example #1 Basic usage of ReflectionClass::getStaticPropertyValue()

<?php
class Apple {
    public static $color = 'Red';
}

$class = new ReflectionClass('Apple');
var_dump($class->getStaticPropertyValue('color'));
?>

위 예제의 출력:

string(3) "Red"

참고

  • ReflectionClass::getStaticProperties() - Gets static properties
  • ReflectionClass::setStaticPropertyValue() - Sets static property value

User Contributed Notes

Antares 16-Aug-2011 04:11
It seems this method has a security level different from the getStaticProperties method.

If you make two classes A and B looking like

<?php
class A{
        protected static $static_var='foo';

        public function getStatic(){
           $class=new ReflectionClass($this);
           return $class->getStaticPropertyValue('static_var');
        }

        public function getStatic2(){
            $class=new ReflectionClass($this);
            $staticProps=$class->getStaticProperties();
            return $staticProps['static_var'];
        }

        public function __construct(){
              echo $this->getStatic2();
              echo $this->getStatic();
        }
}

class B extends A{
        protected static $static_var='foo2';

}
?>

Then the output will be an exception for the getStatic() call, when the getStatic2() will correctly return 'foo2';
Mauro Gabriel Titimoli 21-Jan-2010 09:05
If you want to change a static property of a variable class...

PHP 5.2
<?php
$reflection = new ReflectionClass($className);
$staticPropertyReference = & $reflection->getStaticPropertyValue($staticPropertyName);

$staticPropertyReference = 'new value';
?>

PHP 5.3
<?php
$className::$$classProperty
?>


Returns an array of trait aliases

  • ≪ ReflectionClass::getStaticPropertyValue
  • ReflectionClass::getTraitNames ≫
  • PHP Manual
  • ReflectionClass
  • Returns an array of trait aliases

ReflectionClass::getTraitAliases

(PHP >= 5.4.0)

ReflectionClass::getTraitAliases — Returns an array of trait aliases

설명

public array ReflectionClass::getTraitAliases ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns an array with new method names in keys and original names (in the format "TraitName::original") in values. Returns NULL in case of an error.

User Contributed Notes

dhairya dot coder at gmail dot com 10-Feb-2016 08:43
trait A {
    public function smallTalk() {
        echo 'a';
    }
    public function bigTalk() {
        echo 'A';
    }
}

class Apple{

    use A {
        A::bigTalk as talk;
    }
}

$obj=new ReflectionClass('Apple');
echo "<pre>";
var_dump($obj->getTraitAliases());
echo "</pre>";


Returns an array of names of traits used by this class

  • ≪ ReflectionClass::getTraitAliases
  • ReflectionClass::getTraits ≫
  • PHP Manual
  • ReflectionClass
  • Returns an array of names of traits used by this class

ReflectionClass::getTraitNames

(PHP >= 5.4.0)

ReflectionClass::getTraitNames — Returns an array of names of traits used by this class

설명

public array ReflectionClass::getTraitNames ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns an array with trait names in values. Returns NULL in case of an error.

User Contributed Notes

emulienfou at gmail dot com 01-Dec-2013 12:55
This remote return only the trait names from the current class.

If your class extends another class using your trait, you can't get the names. However, you can do something like :

<?php
$traitsNames = [];
$recursiveClasses = function ($class) use(&$recursiveClasses, &$traitsNames) {
    if ($class->getParentClass() != false) {
        $recursiveClasses($class->getParentClass());
    }
    else {
        $traitsNames = array_merge($traitsNames, $class->getTraitNames());
    }
};
$recursiveClasses($controllerClass);


Returns an array of traits used by this class

  • ≪ ReflectionClass::getTraitNames
  • ReflectionClass::hasConstant ≫
  • PHP Manual
  • ReflectionClass
  • Returns an array of traits used by this class

ReflectionClass::getTraits

(PHP >= 5.4.0)

ReflectionClass::getTraits — Returns an array of traits used by this class

설명

public array ReflectionClass::getTraits ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns an array with trait names in keys and instances of trait's ReflectionClass in values. Returns NULL in case of an error.

User Contributed Notes

Anonymous 16-Jan-2015 01:03
Note that getTraits will NOT return any traits inherited from a parent.

This is currently viewed as the desired behavior.  See https://bugs.php.net/bug.php?id=61554


Checks if constant is defined

  • ≪ ReflectionClass::getTraits
  • ReflectionClass::hasMethod ≫
  • PHP Manual
  • ReflectionClass
  • Checks if constant is defined

ReflectionClass::hasConstant

(PHP 5 >= 5.1.0, PHP 7)

ReflectionClass::hasConstant — Checks if constant is defined

설명

public bool ReflectionClass::hasConstant ( string $name )

Checks whether the class has a specific constant defined or not.

인수

name

The name of the constant being checked for.

반환값

TRUE if the constant is defined, otherwise FALSE.

예제

Example #1 ReflectionClass::hasConstant() example

<?php
class Foo {
    const c1 = 1;
}

$class = new ReflectionClass("Foo");

var_dump($class->hasConstant("c1"));
var_dump($class->hasConstant("c2"));
?>

위 예제의 출력 예시:

bool(true)
bool(false)

참고

  • ReflectionClass::hasMethod() - Checks if method is defined
  • ReflectionClass::hasProperty() - Checks if property is defined

User Contributed Notes

There are no user contributed notes for this page.


Checks if method is defined

  • ≪ ReflectionClass::hasConstant
  • ReflectionClass::hasProperty ≫
  • PHP Manual
  • ReflectionClass
  • Checks if method is defined

ReflectionClass::hasMethod

(PHP 5 >= 5.1.0, PHP 7)

ReflectionClass::hasMethod — Checks if method is defined

설명

public bool ReflectionClass::hasMethod ( string $name )

Checks whether a specific method is defined in a class.

인수

name

Name of the method being checked for.

반환값

TRUE if it has the method, otherwise FALSE

예제

Example #1 ReflectionClass::hasMethod() example

<?php
Class C {
    public function publicFoo() {
        return true;
    }

    protected function protectedFoo() {
        return true;
    }

    private function privateFoo() {
        return true;
    }

    static function staticFoo() {
        return true;
    }
}

$rc = new ReflectionClass("C");

var_dump($rc->hasMethod('publicFoo'));

var_dump($rc->hasMethod('protectedFoo'));

var_dump($rc->hasMethod('privateFoo'));

var_dump($rc->hasMethod('staticFoo'));

// C should not have method bar
var_dump($rc->hasMethod('bar'));

// Method names are case insensitive
var_dump($rc->hasMethod('PUBLICfOO'));
?>

위 예제의 출력:

bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)

참고

  • ReflectionClass::hasConstant() - Checks if constant is defined
  • ReflectionClass::hasProperty() - Checks if property is defined

User Contributed Notes

flancer64 at gmail dot com 02-Aug-2016 07:50
Annotated methods that are implemented using PHP magic methods are not recognized  by "hasMethod".

<?php
/**
 * @method void annotatedMethod()
 */
class SomeClass
{
    public function __call($name, $arguments)
    {
        echo "this is magic method: $name.\n";
    }

    public function codedMethod()
    {
        echo "this is coded method.\n";
    }
}

$obj = new \SomeClass();
$obj->codedMethod();
$obj->annotatedMethod();

$ref = new ReflectionClass(\SomeClass::class);
echo "SomeClass has 'codedMethod': " . json_encode($ref->hasMethod('codedMethod')) . ".\n";
echo "SomeClass has 'annotatedMethod': " . json_encode($ref->hasMethod('annotatedMethod')) . ".\n";
?>

Output:

this is coded method.
this is magic method: annotatedMethod.
SomeClass has 'codedMethod': true.
SomeClass has 'annotatedMethod': false.
michaelgranados at gmail dot com 19-May-2012 11:47
A way to check if you can call an method over a class:

<?php
function is_public_method(
        /* string */$className,
        /* string */$method
){
    $classInstance = new ReflectionClass($className);
    if ($classInstance->hasMethod($method)) {
        return false;
    }
    $methodInstance = $instance->getMethod($method);
    return $methodInstance->isPublic();
}
?>
phoenix at todofixthis dot com 28-Oct-2010 06:47
Parent methods (regardless of visibility) are also available to a ReflectionObject.  E.g.,

<?php
class ParentObject {
  public function parentPublic(  ) {
  }

  private function parentPrivate(  ) {
  }
}

class ChildObject extends ParentObject {
}

$Instance = new ChildObject();
$Reflector = new ReflectionObject($Instance);

var_dump($Reflector->hasMethod('parentPublic'));  // true
var_dump($Reflector->hasMethod('parentPrivate')); // true
?>
hanguofeng at gmail dot com 20-Oct-2010 06:09
note that even if private method will also be 'has'.


Checks if property is defined

  • ≪ ReflectionClass::hasMethod
  • ReflectionClass::implementsInterface ≫
  • PHP Manual
  • ReflectionClass
  • Checks if property is defined

ReflectionClass::hasProperty

(PHP 5 >= 5.1.0, PHP 7)

ReflectionClass::hasProperty — Checks if property is defined

설명

public bool ReflectionClass::hasProperty ( string $name )

Checks whether the specified property is defined.

인수

name

Name of the property being checked for.

반환값

TRUE if it has the property, otherwise FALSE

예제

Example #1 ReflectionClass::hasProperty() example

<?php
class Foo {
    public    $p1;
    protected $p2;
    private   $p3;

}

$obj = new ReflectionObject(new Foo());

var_dump($obj->hasProperty("p1"));
var_dump($obj->hasProperty("p2"));
var_dump($obj->hasProperty("p3"));
var_dump($obj->hasProperty("p4"));
?>

위 예제의 출력 예시:

bool(true)
bool(true)
bool(true)
bool(false)

참고

  • ReflectionClass::hasConstant() - Checks if constant is defined
  • ReflectionClass::hasMethod() - Checks if method is defined

User Contributed Notes

rwilczek at web-appz dot de 13-Oct-2009 02:40
Note, that this method does not guarantee, that you can get a property with ReflectionClass::getProperty().

ReflectionClass::hasProperty() considers the parent classes (ignoring however, that a private property is not inherited), while ReflectionClass::getProperty() and ReflectionClass::getProperties() don't care about inheritance.

(Tested with PHP 5.3.0)

<?php
class Foo
{
    private $x;
}

class Bar extends Foo
{
    //
}

$foo = new ReflectionClass('Foo');
$bar = new ReflectionClass('Bar');

var_dump($foo->hasProperty('x'); // bool(true)
var_dump($bar->hasProperty('x'); // bool(true)

var_dump(get_class($foo->getProperty('x'))); //string(18) "ReflectionProperty"
try {
    $bar->getProperty('x');
} catch (ReflectionException $e) {
    echo $e->getMessage(); // Property x does not exist
}
?>


Implements interface

  • ≪ ReflectionClass::hasProperty
  • ReflectionClass::inNamespace ≫
  • PHP Manual
  • ReflectionClass
  • Implements interface

ReflectionClass::implementsInterface

(PHP 5, PHP 7)

ReflectionClass::implementsInterface — Implements interface

설명

public bool ReflectionClass::implementsInterface ( string $interface )

Checks whether it implements an interface.

인수

interface

The interface name.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • ReflectionClass::isInterface() - Checks if the class is an interface
  • ReflectionClass::isSubclassOf() - Checks if a subclass
  • interface_exists() - 인터페이스가 정의되었는지 확인
  • Object Interfaces

User Contributed Notes

dhairya dot coder at gmail dot com 11-Feb-2016 12:48
//checks that whether class Fruit implements interface apple or not

interface Apple {

    function taste();
}

class Fruit implements Apple {

    function taste() {
        echo "Seet";
    }
}

$obj=new ReflectionClass('Fruit');
var_dump($obj->implementsInterface('Apple'));  //Here it will checks that whether class Fruit implements interface apple or not


Checks if in namespace

  • ≪ ReflectionClass::implementsInterface
  • ReflectionClass::isAbstract ≫
  • PHP Manual
  • ReflectionClass
  • Checks if in namespace

ReflectionClass::inNamespace

(PHP 5 >= 5.3.0, PHP 7)

ReflectionClass::inNamespace — Checks if in namespace

설명

public bool ReflectionClass::inNamespace ( void )

Checks if this class is defined in a namespace.

인수

이 함수는 인수가 없습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 ReflectionClass::inNamespace() example

<?php
namespace A\B;

class Foo { }

$function = new \ReflectionClass('stdClass');

var_dump($function->inNamespace());
var_dump($function->getName());
var_dump($function->getNamespaceName());
var_dump($function->getShortName());

$function = new \ReflectionClass('A\\B\\Foo');

var_dump($function->inNamespace());
var_dump($function->getName());
var_dump($function->getNamespaceName());
var_dump($function->getShortName());
?>

위 예제의 출력:

bool(false)
string(8) "stdClass"
string(0) ""
string(8) "stdClass"

bool(true)
string(7) "A\B\Foo"
string(3) "A\B"
string(3) "Foo"

참고

  • ReflectionClass::getNamespaceName() - Gets namespace name
  • PHP Namespaces

User Contributed Notes

There are no user contributed notes for this page.


Checks if class is abstract

  • ≪ ReflectionClass::inNamespace
  • ReflectionClass::isAnonymous ≫
  • PHP Manual
  • ReflectionClass
  • Checks if class is abstract

ReflectionClass::isAbstract

(PHP 5, PHP 7)

ReflectionClass::isAbstract — Checks if class is abstract

설명

public bool ReflectionClass::isAbstract ( void )

Checks if the class is abstract.

인수

이 함수는 인수가 없습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 ReflectionClass::isAbstract() example

<?php
class          TestClass { }
abstract class TestAbstractClass { }

$testClass     = new ReflectionClass('TestClass');
$abstractClass = new ReflectionClass('TestAbstractClass');

var_dump($testClass->isAbstract());
var_dump($abstractClass->isAbstract());
?>

위 예제의 출력:

bool(false)
bool(true)

참고

  • ReflectionClass::isInterface() - Checks if the class is an interface
  • Class Abstraction

User Contributed Notes

baptiste at pillot dot fr 10-Oct-2016 09:06
For interfaces and traits :

<?php
interface TestInterface { }
trait     TestTrait { }

$interfaceClass = new ReflectionClass('TestInterface');
$traitClass     = new ReflectionClass('TestTrait');

var_dump($interfaceClass->isAbstract());
var_dump($traitClass->isAbstract());
?>

Using PHP versions 5.4- to 5.6, the above example will output:

bool(false)
bool(true)

Using PHP versions 7.0+, the above example will output:

bool(false)
bool(false)


Checks if class is anonymous

  • ≪ ReflectionClass::isAbstract
  • ReflectionClass::isCloneable ≫
  • PHP Manual
  • ReflectionClass
  • Checks if class is anonymous

ReflectionClass::isAnonymous

(PHP 7)

ReflectionClass::isAnonymous — Checks if class is anonymous

설명

public bool ReflectionClass::isAnonymous ( void )

Checks if a class is an anonymous class.

인수

이 함수는 인수가 없습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 ReflectionClass::isAnonymous() example

<?php
class TestClass {}
$anonClass = new class {};

$normalClass = new ReflectionClass('TestClass');
$anonClass  = new ReflectionClass($anonClass);

var_dump($normalClass->isAnonymous());
var_dump($anonClass->isAnonymous());

?>

위 예제의 출력:

bool(false)
bool(true)

참고

  • ReflectionClass::isFinal() - Checks if class is final

User Contributed Notes

There are no user contributed notes for this page.


Returns whether this class is cloneable

  • ≪ ReflectionClass::isAnonymous
  • ReflectionClass::isFinal ≫
  • PHP Manual
  • ReflectionClass
  • Returns whether this class is cloneable

ReflectionClass::isCloneable

(PHP >= 5.4.0)

ReflectionClass::isCloneable — Returns whether this class is cloneable

설명

public bool ReflectionClass::isCloneable ( void )

Returns whether this class is cloneable.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the class is cloneable, FALSE otherwise.

예제

Example #1 Basic usage of ReflectionClass::isCloneable()

<?php
class NotCloneable {
    public $var1;

    private function __clone() {
    }
}

class Cloneable {
    public $var1;
}

$notCloneable = new ReflectionClass('NotCloneable');
$cloneable = new ReflectionClass('Cloneable');

var_dump($notCloneable->isCloneable());
var_dump($cloneable->isCloneable());
?>

위 예제의 출력:

bool(false)
bool(true)

User Contributed Notes

php at abiusx dot com 19-Aug-2016 01:29
This does not work for many of core-classes, just like most other reflection methods.


Checks if class is final

  • ≪ ReflectionClass::isCloneable
  • ReflectionClass::isInstance ≫
  • PHP Manual
  • ReflectionClass
  • Checks if class is final

ReflectionClass::isFinal

(PHP 5, PHP 7)

ReflectionClass::isFinal — Checks if class is final

설명

public bool ReflectionClass::isFinal ( void )

Checks if a class is final.

인수

이 함수는 인수가 없습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 ReflectionClass::isFinal() example

<?php
class       TestClass { }
final class TestFinalClass { }

$normalClass = new ReflectionClass('TestClass');
$finalClass  = new ReflectionClass('TestFinalClass');

var_dump($normalClass->isFinal());
var_dump($finalClass->isFinal());

?>

위 예제의 출력:

bool(false)
bool(true)

참고

  • ReflectionClass::isAbstract() - Checks if class is abstract
  • Final Keyword

User Contributed Notes

There are no user contributed notes for this page.


Checks class for instance

  • ≪ ReflectionClass::isFinal
  • ReflectionClass::isInstantiable ≫
  • PHP Manual
  • ReflectionClass
  • Checks class for instance

ReflectionClass::isInstance

(PHP 5, PHP 7)

ReflectionClass::isInstance — Checks class for instance

설명

public bool ReflectionClass::isInstance ( object $object )

Checks if an object is an instance of a class.

인수

object

The object being compared to.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 ReflectionClass::isInstance() related examples

<?php
// Example usage
$class = new ReflectionClass('Foo');

if ($class->isInstance($arg)) {
    echo "Yes";
}

// Equivalent to
if ($arg instanceof Foo) {
    echo "Yes";
}

// Equivalent to
if (is_a($arg, 'Foo')) {
    echo "Yes";
}
?>

위 예제의 출력 예시:

Yes
Yes
Yes

참고

  • ReflectionClass::isInterface() - Checks if the class is an interface
  • Type operators (instanceof)
  • Object Interfaces
  • is_a() - 객체가 이 클래스나 부모 클래스 중 하나인지 확인

User Contributed Notes

dhairya lakhera 11-Feb-2016 01:55
class  TestClass { }

$TestObj=new TestClass();

$TestObj_assigned=$TestObj;
$TestObj_Refrenced=&$TestObj;
$TestObj_cloned=clone $TestObj;

$obj=new ReflectionClass('TestClass');

var_dump($obj->isInstance($TestObj));
var_dump($obj->isInstance($TestObj_assigned));
var_dump($obj->isInstance($TestObj_Refrenced));
var_dump($obj->isInstance($TestObj_cloned));


Checks if the class is instantiable

  • ≪ ReflectionClass::isInstance
  • ReflectionClass::isInterface ≫
  • PHP Manual
  • ReflectionClass
  • Checks if the class is instantiable

ReflectionClass::isInstantiable

(PHP 5, PHP 7)

ReflectionClass::isInstantiable — Checks if the class is instantiable

설명

public bool ReflectionClass::isInstantiable ( void )

Checks if the class is instantiable.

인수

이 함수는 인수가 없습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 ReflectionClass::isInstantiable() example

<?php
class C { }

interface iface {
    function f1();
}

class ifaceImpl implements iface {
    function f1() {}
}

abstract class abstractClass {
    function f1() { }
    abstract function f2();
}

class D extends abstractClass {
    function f2() { }
}

class privateConstructor {
    private function __construct() { }
}

$classes = array(
    "C",
    "iface",
    "ifaceImpl",
    "abstractClass",
    "D",
    "privateConstructor",
);

foreach($classes  as $class ) {
    $reflectionClass = new ReflectionClass($class);
    echo "Is $class instantiable?  ";
    var_dump($reflectionClass->IsInstantiable());
}

?>

위 예제의 출력:

Is C instantiable?  bool(true)
Is iface instantiable?  bool(false)
Is ifaceImpl instantiable?  bool(true)
Is abstractClass instantiable?  bool(false)
Is D instantiable?  bool(true)
Is privateConstructor instantiable?  bool(false)

참고

  • ReflectionClass::isInstance() - Checks class for instance

User Contributed Notes

There are no user contributed notes for this page.


Checks if the class is an interface

  • ≪ ReflectionClass::isInstantiable
  • ReflectionClass::isInternal ≫
  • PHP Manual
  • ReflectionClass
  • Checks if the class is an interface

ReflectionClass::isInterface

(PHP 5, PHP 7)

ReflectionClass::isInterface — Checks if the class is an interface

설명

public bool ReflectionClass::isInterface ( void )

Checks whether the class is an interface.

인수

이 함수는 인수가 없습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 Basic usage of ReflectionClass::isInterface()

<?php
interface SomeInterface {
    public function interfaceMethod();
}

$class = new ReflectionClass('SomeInterface');
var_dump($class->isInterface());
?>

위 예제의 출력:

bool(true)

참고

  • ReflectionClass::isInstance() - Checks class for instance

User Contributed Notes

There are no user contributed notes for this page.


Checks if class is defined internally by an extension, or the core

  • ≪ ReflectionClass::isInterface
  • ReflectionClass::isIterateable ≫
  • PHP Manual
  • ReflectionClass
  • Checks if class is defined internally by an extension, or the core

ReflectionClass::isInternal

(PHP 5, PHP 7)

ReflectionClass::isInternal — Checks if class is defined internally by an extension, or the core

설명

public bool ReflectionClass::isInternal ( void )

Checks if the class is defined internally by an extension, or the core, as opposed to user-defined.

인수

이 함수는 인수가 없습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 Basic usage of ReflectionClass::isInternal()

<?php
$internalclass = new ReflectionClass('ReflectionClass');

class Apple {}
$userclass = new ReflectionClass('Apple');

var_dump($internalclass->isInternal());
var_dump($userclass->isInternal());
?>

위 예제의 출력:

bool(true)
bool(false)

참고

  • ReflectionClass::isUserDefined() - Checks if user defined

User Contributed Notes

There are no user contributed notes for this page.


Checks if iterateable

  • ≪ ReflectionClass::isInternal
  • ReflectionClass::isSubclassOf ≫
  • PHP Manual
  • ReflectionClass
  • Checks if iterateable

ReflectionClass::isIterateable

(PHP 5, PHP 7)

ReflectionClass::isIterateable — Checks if iterateable

설명

public bool ReflectionClass::isIterateable ( void )

Checks whether the class is iterateable.

인수

이 함수는 인수가 없습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 ReflectionClass::isIterateable() example

<?php

class IteratorClass implements Iterator {
    public function __construct() { }
    public function key() { }
    public function current() { }
    function next() { }
    function valid() { }
    function rewind() { }
}
class DerivedClass extends IteratorClass { }
class NonIterator { }

function dump_iterateable($class) {
    $reflection = new ReflectionClass($class);
    var_dump($reflection->isIterateable());
}

$classes = array("ArrayObject", "IteratorClass", "DerivedClass", "NonIterator");

foreach ($classes as $class) {
    echo "Is $class iterateable? ";
    dump_iterateable($class);
}
?>

위 예제의 출력:

Is ArrayObject iterateable? bool(true)
Is IteratorClass iterateable? bool(true)
Is DerivedClass iterateable? bool(true)
Is NonIterator iterateable? bool(false)

참고

  • ReflectionClass::__construct() - Constructs a ReflectionClass

User Contributed Notes

There are no user contributed notes for this page.


Checks if a subclass

  • ≪ ReflectionClass::isIterateable
  • ReflectionClass::isTrait ≫
  • PHP Manual
  • ReflectionClass
  • Checks if a subclass

ReflectionClass::isSubclassOf

(PHP 5, PHP 7)

ReflectionClass::isSubclassOf — Checks if a subclass

설명

public bool ReflectionClass::isSubclassOf ( string $class )

Checks if the class is a subclass of a specified class or implements a specified interface.

인수

class

The class name being checked against.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • ReflectionClass::isInterface() - Checks if the class is an interface
  • ReflectionClass::implementsInterface() - Implements interface
  • is_subclass_of() - 이 클래스가 부모 클래스의 자식인지 확인
  • get_parent_class() - 객체나 클래스의 부모 클래스명을 얻습니다

User Contributed Notes

dhairya lakhera 11-Feb-2016 02:22
class A {}
class B {}
class C extends B {}

$obj=new ReflectionClass('C');

var_dump($obj->isSubclassOf ('A')); //boolean false
var_dump($obj->isSubclassOf ('B')); //boolean true


Returns whether this is a trait

  • ≪ ReflectionClass::isSubclassOf
  • ReflectionClass::isUserDefined ≫
  • PHP Manual
  • ReflectionClass
  • Returns whether this is a trait

ReflectionClass::isTrait

(PHP >= 5.4.0)

ReflectionClass::isTrait — Returns whether this is a trait

설명

public bool ReflectionClass::isTrait ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if this is a trait, FALSE otherwise. Returns NULL in case of an error.

User Contributed Notes

There are no user contributed notes for this page.


Checks if user defined

  • ≪ ReflectionClass::isTrait
  • ReflectionClass::newInstance ≫
  • PHP Manual
  • ReflectionClass
  • Checks if user defined

ReflectionClass::isUserDefined

(PHP 5, PHP 7)

ReflectionClass::isUserDefined — Checks if user defined

설명

public bool ReflectionClass::isUserDefined ( void )

Checks whether the class is user-defined, as opposed to internal.

인수

이 함수는 인수가 없습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • ReflectionClass::isInternal() - Checks if class is defined internally by an extension, or the core

User Contributed Notes

dhairya lakhera 11-Feb-2016 02:38
$internalclass = new ReflectionClass('ReflectionClass');

class Apple {}
$userclass = new ReflectionClass('Apple');

var_dump($internalclass->isUserDefined());
var_dump($userclass->isUserDefined());


Creates a new class instance from given arguments.

  • ≪ ReflectionClass::isUserDefined
  • ReflectionClass::newInstanceArgs ≫
  • PHP Manual
  • ReflectionClass
  • Creates a new class instance from given arguments.

ReflectionClass::newInstance

(PHP 5, PHP 7)

ReflectionClass::newInstance — Creates a new class instance from given arguments.

설명

public object ReflectionClass::newInstance ( mixed $args [, mixed $... ] )

Creates a new instance of the class. The given arguments are passed to the class constructor.

인수

args

Accepts a variable number of arguments which are passed to the class constructor, much like call_user_func().

반환값

오류/예외

A ReflectionException if the class constructor is not public.

A ReflectionException if the class does not have a constructor and the args parameter contains one or more parameters.

참고

  • ReflectionClass::newInstanceArgs() - Creates a new class instance from given arguments.
  • ReflectionClass::newInstanceWithoutConstructor() - Creates a new class instance without invoking the constructor.

User Contributed Notes

glen at delfi dot ee 07-Oct-2015 08:19
looks like reflection class newInstance creates in memory representation of code where values are used, so using reference as constructor signature, you can not use this method.

as  the same input if called via new, or new $class works, but not via reflection:

 class a {
     public function __construct(&$a, $c) {
     }
 }

// this works
 $A = new stdClass();
 $a = new a($A, 11);

 // also this works
 $classname = "a";
 $a = new $classname($A, 10);

 // but this fails:
 $r = new ReflectionClass("a");
 $r->newInstance($A, 10);

 PHP Warning:  Parameter 1 to a::__construct() expected to be a reference, value given in reflection.php on line 15

 PHP Warning:  ReflectionClass::newInstance(): Invocation of a's constructor failed in reflection.php on line 15


Creates a new class instance from given arguments.

  • ≪ ReflectionClass::newInstance
  • ReflectionClass::newInstanceWithoutConstructor ≫
  • PHP Manual
  • ReflectionClass
  • Creates a new class instance from given arguments.

ReflectionClass::newInstanceArgs

(PHP 5 >= 5.1.3, PHP 7)

ReflectionClass::newInstanceArgs — Creates a new class instance from given arguments.

설명

public object ReflectionClass::newInstanceArgs ([ array $args ] )

Creates a new instance of the class, the given arguments are passed to the class constructor.

인수

args

The parameters to be passed to the class constructor as an array.

반환값

Returns a new instance of the class.

예제

Example #1 Basic usage of ReflectionClass::newInstanceArgs()

<?php
$class = new ReflectionClass('ReflectionFunction');
$instance = $class->newInstanceArgs(array('substr'));
var_dump($instance);
?>

위 예제의 출력:

object(ReflectionFunction)#2 (1) {
  ["name"]=>
  string(6) "substr"
}

오류/예외

A ReflectionException if the class constructor is not public.

A ReflectionException if the class does not have a constructor and the args parameter contains one or more parameters.

참고

  • ReflectionClass::newInstance() - Creates a new class instance from given arguments.
  • ReflectionClass::newInstanceWithoutConstructor() - Creates a new class instance without invoking the constructor.

User Contributed Notes

kirillsaksin at no-spam dot yandex dot ru 11-Nov-2015 02:59
Hack to properly instantiate class with private constructor:

<?php

class TestClass
{
    private $property;
    private function __construct($argument)
    {
        $this->property = $argument;
    }
}

$ref = new ReflectionClass(TestClass::class);
$instance = $ref->newInstanceWithoutConstructor();
var_dump($instance);
echo PHP_EOL . '------------------------' . PHP_EOL . PHP_EOL;
$constructor = $ref->getConstructor();
$constructor->setAccessible(true);
$constructor->invokeArgs($instance, ['It works!']);
var_dump($instance);

// Output:
// class TestClass#3 (1) {
//   private $property =>
//   NULL
// }
//
// ------------------------
//
// class TestClass#3 (1) {
//   private $property =>
//   string(9) "It works!"
// }

?>
dev at yopmail dot com 05-Jul-2015 12:06
With PHP 5.6, we can use the ... (T_ELLIPSIS) operator

<?php

class Test {
    public function __construct($a, $b) {
        echo $a . ' ' . $b;
    }
}

$args = array(12, 34);
new Test(... $args); // Displays "12 34"

?>
foxbunny 12-Dec-2010 04:23
It should be noted that the the values in the array are mapped to constructor arguments positionally, rather than by name, so using an associative array will not make any difference.
talk at stephensugden dot com 31-Oct-2010 12:17
This is the way I dynamically instantiate objects in my lightweight IoC container

<?php

class SimpleContainer {

  // ...

  // Creates an instance of an object with the provided array of arguments
  protected function instantiate($name, $args=array()){
    if(empty($args))
      return new $name();
    else {
      $ref = new ReflectionClass($name);
      return $ref->newInstanceArgs($args);
    }
  }
  // ...
}
?>

I explicitly do NOT handle the case where a user passes constructor arguments for a constructor-less class, as I this SHOULD fail.
sausage at tehsausage dot com 21-Aug-2010 01:14
Annoyingly, this will throw an exception for classes with no constructor even if you pass an empty array for the arguments. For generic programming you should avoid this function and use call_user_func_array with newInstance.
sarfraznawaz2005 at gmail dot com 15-Dec-2009 08:31
I use reflection class and also detect whether arguments are passed by reference or passed by value
and then initiate/call the method successfully with those arguments:

<?php
    if (count($args) > 1)
    {
        if (method_exists($class_name,  '__construct') === false)
        {
            exit("Constructor for the class <strong>$class_name</strong> does not exist, you should not pass arguments to the constructor of this class!");
        }

        $refMethod = new ReflectionMethod($class_name,  '__construct');
        $params = $refMethod->getParameters();

        $re_args = array();

        foreach($params as $key => $param)
        {
            if ($param->isPassedByReference())
            {
                $re_args[$key] = &$args[$key];
            }
            else
            {
                $re_args[$key] = $args[$key];
            }
        }

        $refClass = new ReflectionClass($class_name);
        $class_instance = $refClass->newInstanceArgs((array) $re_args);
    }
?>
kevinpeno at gmail dot com 16-Sep-2009 05:11
I misunderstood this function to be a sort of setter of Reflection::newInstance() arguments in an array form rather than a creator of new instances itself.

This function is equivilant to call_user_func_array() while Reflection::newInstance() is equivilant to call_user_func()
richardcook at gmail dot com 12-Apr-2009 11:39
the newInstanceArgs function cannot call a class' constructor if it has references in its arguments, so be careful what you pass into it:

<?php
class Foo {
    function __construct (&$arr) {
        $this->arr = &$arr;
    }
    function createInstance () {
        $reflectionClass = new ReflectionClass("Bar");

        return $reflectionClass->newInstanceArgs(array($this, $this->arr));
    }
    function mod($key, $val) {
        $this->arr[$key] = $val;
    }
}

class Bar {
    function __construct (&$foo, &$arr) {
        $this->foo = &$foo;
        $this->arr = &$arr;
    }
    function mod($key, $val) {
        $this->arr[$key] = $val;
    }
}

$arr = array();

$foo = new Foo($arr);

$arr["x"] = 1;

$foo->mod("y", 2);

$bar = $foo->createInstance();

$bar->mod("z", 3);

echo "<pre>";
print_r($arr);
print_r($foo);
print_r($bar);
echo "</pre>";

/*
Output:
Warning: Invocation of Bar's constructor failed in [code path] on line 31

Fatal error: Call to a member function mod() on a non-object in [code path] on line 58
*/
?>
gromit at mailinator dot com 29-Jul-2008 10:37
Be aware that calling the method newInstanceArgs with an empty array will still call the constructor with no arguments. If the class has no constructor then it will generate an exception.

You need to check if a constructor exists before calling this method or use try and catch to act on the exception.


Creates a new class instance without invoking the constructor.

  • ≪ ReflectionClass::newInstanceArgs
  • ReflectionClass::setStaticPropertyValue ≫
  • PHP Manual
  • ReflectionClass
  • Creates a new class instance without invoking the constructor.

ReflectionClass::newInstanceWithoutConstructor

(PHP >= 5.4.0)

ReflectionClass::newInstanceWithoutConstructor — Creates a new class instance without invoking the constructor.

설명

public object ReflectionClass::newInstanceWithoutConstructor ( void )

Creates a new instance of the class without invoking the constructor.

인수

반환값

변경점

버전 | 설명
5.6.0 | All internal classes can now be instantiated except for those declared final.

오류/예외

A ReflectionException if the class is an internal class that cannot be instantiated without invoking the constructor. In PHP 5.6.0 onwards, this exception is limited only to internal classes that are final.

참고

  • ReflectionClass::newInstance() - Creates a new class instance from given arguments.
  • ReflectionClass::newInstanceArgs() - Creates a new class instance from given arguments.

User Contributed Notes

260 at ciemnosc 06-Feb-2016 11:11
sorry for replying to such an old comment, but there's something i wanted to point out.

@  tom at r dot je:
While I agree with what you said in general, there *are* cases where it's not true, and since PHP doesn't allow for multiple constructors there's no other good way around it.

> The problem? It breaks encapsulation.
> An object can exist in the application but may not be able to fulfill its
> responsibility because it's missing dependencies.
> The use of this method makes it possible for an incomplete object
> to exist in the system;
> the object can exist in a state that its author never intended.

If you're using this method from some factory method, to manually init the object in other way that the constructor that, this point is no longer valid.

Consider the example, where you use constructor to set-up the object after getting it from database (e.g. you need to pull some array from other table, basing on some id parameter). But you want to also be able to create object manually (for inserting to the database, for example).
The best thing to do would be to have two different constructors, but since it is not possible in php you need some other way to create the object.

example:

<?php
// this usually would be a static cache in some class, or array returned by method getMeta($id), etc.
$meta = array(1337 => array('key1' => 'value1', 'key2' => 'value2'));

class Test {
    public $id;
    public $data;
    public $meta;

    public function __construct() {
        global $meta;
        if(is_int($this->id)) $this->meta = $meta[$this->id];
    }
    public static function create_empty() {
        $r = new ReflectionClass(__CLASS__);
        return $r->newInstanceWithoutConstructor();
    }
}
echo "emulating PDO::FETCH_CLASS behavior: ";
$t = Test::create_empty();
$t->meta = 1337;
$t->__construct();
var_dump($t);

echo "empty class: ";
$testItem = Test::create_empty();
// ... here you can start setting up the item, e.g. from XML
var_dump($testItem);

$testItem->id = 0;
$testItem->data = "some data";
$testItem->meta = array("somekey" => "somevalue");

echo "after setup:";
var_dump($testItem);
?>

Of course, you can instead make empty constructor, and create some init() method, but then you have to remember to call init() everywhere.
You can also create some other way of adding the item to database, but then you'd have to classes to handle the same data - one for retrieving and other for storing.

If you instead use some factory class (or just a factory method, like in the simplified example above) having a way to create totally empty object is useful. With full factory approach you would first use some TestFactory->prepare() method, then you'd call methods to set what you need, and the factory would set all uninitialized variables to some default values upon calling TestFactory->get() to retrieve prepared object.
tom at r dot je 11-Jun-2013 06:53
It should be made clear that from an OOP theory perspective the use of this method is very bad practice in the same manner as goto, eval and singletons. If you find a need to use it in production code you're almost certainly doing something wrong somewhere. It may occasionally be useful for debugging, but even then hints at poor initial code.

The problem? It breaks encapsulation. An object can exist in the application but may not be able to fulfill its responsibility because it's missing dependencies. The use of this method makes it possible for an incomplete object to exist in the system; the object can exist in a state that its author never intended. This is bad because it will cause unexpected things to happen! A fundamental principle in OOP is that objects are in complete control of their state, the use of this method prevents that guarantee.

n.b. The annotation based "dependency injection" listed below is not a solution or valid use-case for this either because it breaks encapsulation (Among other things!) and the class being constructed needs to know of the container by providing annotations.
me [ata] thomas-lauria.de 28-Apr-2012 03:51
This new Feature enables Annotation based Dependency Injection:
<?php

//dependency to inject
class dep {}

class a {
  /**
   * @inject
   * @var dep
   */
  protected $foo;
}

class b extends a {
  /**
   * @inject
   * @var dep
   */
  protected $bar;

  public function __construct() {
    echo "CONSTRUCTOR CALLED\n";
  }
}

$ref = new ReflectionClass('b');
$inst = $ref->newInstanceWithoutConstructor();

$list = $ref->getProperties();
foreach($list as $prop){
  /* @var $prop ReflectionProperty */
  $prop->getDocComment(); //grep for @inject and the @vars class name
  $prop->setAccessible(true);
  $prop->setValue($inst, new dep());
}
if($const = $ref->getConstructor()) {
  $constName = $const->getName();
  $inst->{$constName}(); //use call_user_func_array($function, $param_arr); for arguments
}

print_r($inst);
print_r($inst->foo); //property still not accessable

The Output:

CONSTRUCTOR CALLED
b Object
(
    [bar:protected] => dep Object
        (
        )

    [foo:protected] => dep Object
        (
        )

)
PHP Fatal error:  Cannot access protected property b::$foo in diTest.php on line 42
oliver at ananit dot de 25-Nov-2011 12:57
If this method is not available in your version of PHP you can use a trick to create an instance without calling the constructor.
Use reflection to get the properties and default values of the class, and create a fake "serialized" string.

<?php
function createInstanceWithoutConstructor($class){
    $reflector = new ReflectionClass($class);
    $properties = $reflector->getProperties();
    $defaults = $reflector->getDefaultProperties();

    $serealized = "O:" . strlen($class) . ":\"$class\":".count($properties) .':{';
    foreach ($properties as $property){
        $name = $property->getName();
        if($property->isProtected()){
                $name = chr(0) . '*' .chr(0) .$name;
            } elseif($property->isPrivate()){
                $name = chr(0)  . $class.  chr(0).$name;
            }
            $serealized .= serialize($name);
            if(array_key_exists($property->getName(),$defaults) ){
                $serealized .= serialize($defaults[$property->getName()]);
            } else {
                $serealized .= serialize(null);
            }
        }
    $serealized .="}";
    return unserialize($serealized);
}
?>

Example:

<?php
class foo
{
    public $a = 10;
    protected $b = 2;
    private $c = "default";
    protected $d;
    public function __construct(){
        $this->a = null;
        $this->b = null;
        $this->c = "constructed";
        $this->d = 42;
    }
}

var_dump(createInstanceWithoutConstructor('foo'));
?>

Output:
object(foo)#6 (4) {
  ["a"]=>
  int(10)
  ["b":protected]=>
  int(2)
  ["c":"foo":private]=>
  string(7) "default"
  ["d":protected]=>
  NULL
}

I hope this can help someone.
Oliver Anan
alejosimon at gmail 06-Sep-2011 03:41
A good first use for this new method.

It implements a transparent parser constructor argument to achieve 99% reusable component.

<?php

use ReflectionClass ;

trait TSingleton
{
    /**
    *    Constructor.
    */
    protected function __construct() {}

    /**
    *    Drop clone singleton objects.
    */
    protected function __clone() {}

    /**
    *    Gets only one instance.
    *
    *    @params Optional multiple values as arguments for the constructor.
    *    @return Object instance from called class.
    */
    public static function getInstance()
    {
        static $instance = null ;

        if ( ! $instance )
        {
            $ref  = new ReflectionClass( get_called_class() ) ;
            $ctor = $ref->getConstructor() ;

            // Thanks PHP 5.4
            $self = $ref->newInstanceWithoutConstructor() ;

            // The magic.
            $ctor->setAccessible( true ) ;
            $instance = $ctor->invokeArgs( $self, func_get_args() ) ;
        }

        return $instance ;
    }
}

?>


Sets static property value

  • ≪ ReflectionClass::newInstanceWithoutConstructor
  • ReflectionClass::__toString ≫
  • PHP Manual
  • ReflectionClass
  • Sets static property value

ReflectionClass::setStaticPropertyValue

(PHP 5 >= 5.1.0, PHP 7)

ReflectionClass::setStaticPropertyValue — Sets static property value

설명

public void ReflectionClass::setStaticPropertyValue ( string $name , string $value )

Sets static property value.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

name

Property name.

value

New property value.

반환값

값을 반환하지 않습니다.

참고

  • ReflectionClass::getStaticPropertyValue() - Gets static property value

User Contributed Notes

Andrew Jackson 01-Apr-2014 07:53
Calling this method on a static property that is not public will return a ReflectionException stating the property does not exist. This is quite misleading as the property is valid.

class test {
    public static $publicProperty = 'public';
    private static $privateProperty = 'private';

    public static function printProperties() {
        echo self::$publicProperty . "\n";
        echo self::$privateProperty . "\n";
    }
}

$reflectedClass = new \ReflectionClass('test');
$reflectedClass->setStaticPropertyValue('publicProperty', 'foo');
$reflectedClass->setStaticPropertyValue( 'privateProperty', 'bar' );

PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Class test does not have a property named privateProperty'

If you retrieve the method using the reflection class getProperty method you can circumnavigate this issue

$reflectedProperty = $reflectedClass->getProperty('privateProperty');
$reflectedProperty->setAccessible(true);
$reflectedProperty = $reflectedProperty->setValue('bar');

test::printProperties();
will echo
foo
bar
chuck at manchuck dot com 13-Jul-2012 10:40
It is important to note that calling ReflectionClass::setStaticPropertyValue will not allow you to add new static properties to a class.


Returns the string representation of the ReflectionClass object.

  • ≪ ReflectionClass::setStaticPropertyValue
  • ReflectionZendExtension ≫
  • PHP Manual
  • ReflectionClass
  • Returns the string representation of the ReflectionClass object.

ReflectionClass::__toString

(PHP 5, PHP 7)

ReflectionClass::__toString — Returns the string representation of the ReflectionClass object.

설명

public string ReflectionClass::__toString ( void )

Returns the string representation of the ReflectionClass object.

인수

이 함수는 인수가 없습니다.

반환값

A string representation of this ReflectionClass instance.

예제

Example #1 ReflectionClass::__toString() example

<?php
$reflectionClass = new ReflectionClass('Exception');
echo $reflectionClass->__toString();
?>

위 예제의 출력:

Class [ <internal:Core> class Exception ] {

  - Constants [0] {
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [7] {
    Property [ <default> protected $message ]
    Property [ <default> private $string ]
    Property [ <default> protected $code ]
    Property [ <default> protected $file ]
    Property [ <default> protected $line ]
    Property [ <default> private $trace ]
    Property [ <default> private $previous ]
  }

  - Methods [10] {
    Method [ <internal:Core> final private method __clone ] {
    }

    Method [ <internal:Core, ctor> public method __construct ] {

      - Parameters [3] {
        Parameter #0 [ <optional> $message ]
        Parameter #1 [ <optional> $code ]
        Parameter #2 [ <optional> $previous ]
      }
    }

    Method [ <internal:Core> final public method getMessage ] {
    }

    Method [ <internal:Core> final public method getCode ] {
    }

    Method [ <internal:Core> final public method getFile ] {
    }

    Method [ <internal:Core> final public method getLine ] {
    }

    Method [ <internal:Core> final public method getTrace ] {
    }

    Method [ <internal:Core> final public method getPrevious ] {
    }

    Method [ <internal:Core> final public method getTraceAsString ] {
    }

    Method [ <internal:Core> public method __toString ] {
    }
  }
}

참고

  • ReflectionClass::export() - Exports a class
  • __toString()

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionZendExtension class

(PHP >= 5.4.0)

소개

클래스 개요

ReflectionZendExtension implements Reflector {
/* 프로퍼티 */
public $name ;
/* 메소드 */
final private void __clone ( void )
public __construct ( string $name )
public static string export ( string $name [, string $return ] )
public string getAuthor ( void )
public string getCopyright ( void )
public string getName ( void )
public string getURL ( void )
public string getVersion ( void )
public string __toString ( void )
}

프로퍼티

name

Name of the extension. Read-only, throws ReflectionException in attempt to write.

Table of Contents

  • ReflectionZendExtension::__clone — Clone handler
  • ReflectionZendExtension::__construct — Constructor
  • ReflectionZendExtension::export — Export
  • ReflectionZendExtension::getAuthor — Gets author
  • ReflectionZendExtension::getCopyright — Gets copyright
  • ReflectionZendExtension::getName — Gets name
  • ReflectionZendExtension::getURL — Gets URL
  • ReflectionZendExtension::getVersion — Gets version
  • ReflectionZendExtension::__toString — To string handler

User Contributed Notes

There are no user contributed notes for this page.


Clone handler

  • ≪ ReflectionZendExtension
  • ReflectionZendExtension::__construct ≫
  • PHP Manual
  • ReflectionZendExtension
  • Clone handler

ReflectionZendExtension::__clone

(PHP >= 5.4.0)

ReflectionZendExtension::__clone — Clone handler

설명

final private void ReflectionZendExtension::__clone ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Constructor

  • ≪ ReflectionZendExtension::__clone
  • ReflectionZendExtension::export ≫
  • PHP Manual
  • ReflectionZendExtension
  • Constructor

ReflectionZendExtension::__construct

(PHP >= 5.4.0)

ReflectionZendExtension::__construct — Constructor

설명

public ReflectionZendExtension::__construct ( string $name )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

name

반환값

User Contributed Notes

There are no user contributed notes for this page.


Export

  • ≪ ReflectionZendExtension::__construct
  • ReflectionZendExtension::getAuthor ≫
  • PHP Manual
  • ReflectionZendExtension
  • Export

ReflectionZendExtension::export

(PHP >= 5.4.0)

ReflectionZendExtension::export — Export

설명

public static string ReflectionZendExtension::export ( string $name [, string $return ] )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

name

return

반환값

User Contributed Notes

There are no user contributed notes for this page.


Gets author

  • ≪ ReflectionZendExtension::export
  • ReflectionZendExtension::getCopyright ≫
  • PHP Manual
  • ReflectionZendExtension
  • Gets author

ReflectionZendExtension::getAuthor

(PHP >= 5.4.0)

ReflectionZendExtension::getAuthor — Gets author

설명

public string ReflectionZendExtension::getAuthor ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Gets copyright

  • ≪ ReflectionZendExtension::getAuthor
  • ReflectionZendExtension::getName ≫
  • PHP Manual
  • ReflectionZendExtension
  • Gets copyright

ReflectionZendExtension::getCopyright

(PHP >= 5.4.0)

ReflectionZendExtension::getCopyright — Gets copyright

설명

public string ReflectionZendExtension::getCopyright ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Gets name

  • ≪ ReflectionZendExtension::getCopyright
  • ReflectionZendExtension::getURL ≫
  • PHP Manual
  • ReflectionZendExtension
  • Gets name

ReflectionZendExtension::getName

(PHP >= 5.4.0)

ReflectionZendExtension::getName — Gets name

설명

public string ReflectionZendExtension::getName ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Gets URL

  • ≪ ReflectionZendExtension::getName
  • ReflectionZendExtension::getVersion ≫
  • PHP Manual
  • ReflectionZendExtension
  • Gets URL

ReflectionZendExtension::getURL

(PHP >= 5.4.0)

ReflectionZendExtension::getURL — Gets URL

설명

public string ReflectionZendExtension::getURL ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Gets version

  • ≪ ReflectionZendExtension::getURL
  • ReflectionZendExtension::__toString ≫
  • PHP Manual
  • ReflectionZendExtension
  • Gets version

ReflectionZendExtension::getVersion

(PHP >= 5.4.0)

ReflectionZendExtension::getVersion — Gets version

설명

public string ReflectionZendExtension::getVersion ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


To string handler

  • ≪ ReflectionZendExtension::getVersion
  • ReflectionExtension ≫
  • PHP Manual
  • ReflectionZendExtension
  • To string handler

ReflectionZendExtension::__toString

(PHP >= 5.4.0)

ReflectionZendExtension::__toString — To string handler

설명

public string ReflectionZendExtension::__toString ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionExtension class

(PHP 5, PHP 7)

소개

The ReflectionExtension class reports information about an extension.

클래스 개요

ReflectionExtension implements Reflector {
/* 프로퍼티 */
public $name ;
/* 메소드 */
final private void __clone ( void )
public __construct ( string $name )
public static string export ( string $name [, string $return = false ] )
public array getClasses ( void )
public array getClassNames ( void )
public array getConstants ( void )
public array getDependencies ( void )
public array getFunctions ( void )
public array getINIEntries ( void )
public string getName ( void )
public string getVersion ( void )
public void info ( void )
public void isPersistent ( void )
public void isTemporary ( void )
public string __toString ( void )
}

프로퍼티

name

Name of the extension, same as calling the ReflectionExtension::getName() method.

Table of Contents

  • ReflectionExtension::__clone — Clones
  • ReflectionExtension::__construct — Constructs a ReflectionExtension
  • ReflectionExtension::export — Export
  • ReflectionExtension::getClasses — Gets classes
  • ReflectionExtension::getClassNames — Gets class names
  • ReflectionExtension::getConstants — Gets constants
  • ReflectionExtension::getDependencies — Gets dependencies
  • ReflectionExtension::getFunctions — Gets extension functions
  • ReflectionExtension::getINIEntries — Gets extension ini entries
  • ReflectionExtension::getName — Gets extension name
  • ReflectionExtension::getVersion — Gets extension version
  • ReflectionExtension::info — Print extension info
  • ReflectionExtension::isPersistent — Returns whether this extension is persistent
  • ReflectionExtension::isTemporary — Returns whether this extension is temporary
  • ReflectionExtension::__toString — To string

User Contributed Notes

Anonymous 21-Mar-2016 12:01
<?php
  // Create an instance of the ReflectionProperty class
  $ext= new ReflectionExtension('standard');

  // Print out basic information
  printf(
      "Name        : %s\n".
      "Version     : %s\n".
      "Functions   : [%d] %s\n".
      "Constants   : [%d] %s\n".
      "INI entries : [%d] %s\n",
      $ext->getName(),
      $ext->getVersion() ? $ext->getVersion() : 'NO_VERSION',
      sizeof($ext->getFunctions()),
      var_export($ext->getFunctions(), 1),
      sizeof($ext->getConstants()),
      var_export($ext->getConstants(), 1),
      sizeof($ext->getINIEntries()),
      var_export($ext->getINIEntries(), 1)
  );
?>
Lubaev.K 21-Jun-2013 12:01
<?php
// The demonstration for the class "ReflectionExtension".

function REData(ReflectionExtension $re, $return=false) {

    defined('UNDEFINED') || define('UNDEFINED','%undefined%');
    $_data = [];

    $_data['getName:'] = $re->getName() ?: UNDEFINED;
    $_data['getVersion:'] = $re->getVersion() ?: UNDEFINED;
    $_data['info:'] = $re->info() ?: UNDEFINED;
    $_data['getClassName:'] = PHP_EOL.implode(", ",$re->getClassNames()) ?: UNDEFINED;
    foreach ($re->getConstants() as $key => $value) $_data['getConstants:'] .= "\n{$key}:={$value}";
    $_data['getDependencies:'] = $re->getDependencies() ?: UNDEFINED;
    $_data['getFunctions:'] = PHP_EOL.implode(", ",array_keys($re->getFunctions())) ?: UNDEFINED;
    $_data['getINIEntries:'] = $re->getINIEntries() ?: UNDEFINED;
    $_data['isPersistent:'] = $re->isPersistent() ?: UNDEFINED;
    $_data['isTemporary:'] = $re->isTemporary() ?: UNDEFINED;

    return print_r($_data, $return);
}

REData( new ReflectionExtension( 'Reflection' ) );
REData( new ReflectionExtension( 'zlib' ) );

// Reflection
// Reflection => enabled
// Version => $Id: 60f1e547a6dd00239162151e701566debdcee660 $
/*
Array
(
    [getName:] => Reflection
    [getVersion:] => $Id: 60f1e547a6dd00239162151e701566debdcee660 $
    [info:] => %undefined%
    [getClassName:] =>
ReflectionException, Reflection, Reflector, ReflectionFunctionAbstract, Reflecti
onFunction, ReflectionParameter, ReflectionMethod, ReflectionClass, ReflectionOb
ject, ReflectionProperty, ReflectionExtension, ReflectionZendExtension
    [getDependencies:] => %undefined%
    [getFunctions:] =>

    [getINIEntries:] => %undefined%
    [isPersistent:] => 1
    [isTemporary:] => %undefined%
)
*/
// zlib
// ZLib Support => enabled
// Stream Wrapper => compress.zlib://
// Stream Filter => zlib.inflate, zlib.deflate
// Compiled Version => 1.2.7
// Linked Version => 1.2.7
// Directive => Local Value => Master Value
// zlib.output_compression => Off => Off
// zlib.output_compression_level => -1 => -1
// zlib.output_handler => no value => no value
/*
Array
(
    [getName:] => zlib
    [getVersion:] => 2.0
    [info:] => %undefined%
    [getClassName:] =>

    [getConstants:] =>
FORCE_GZIP:=31
FORCE_DEFLATE:=15
ZLIB_ENCODING_RAW:=-15
ZLIB_ENCODING_GZIP:=31
ZLIB_ENCODING_DEFLATE:=15
    [getDependencies:] => %undefined%
    [getFunctions:] =>
readgzfile, gzrewind, gzclose, gzeof, gzgetc, gzgets, gzgetss, gzread, gzopen, g
zpassthru, gzseek, gztell, gzwrite, gzputs, gzfile, gzcompress, gzuncompress, gz
deflate, gzinflate, gzencode, gzdecode, zlib_encode, zlib_decode, zlib_get_codin
g_type, ob_gzhandler
    [getINIEntries:] => Array
        (
            [zlib.output_compression] =>
            [zlib.output_compression_level] => -1
            [zlib.output_handler] =>
        )

    [isPersistent:] => 1
    [isTemporary:] => %undefined%
)
*/


Clones

  • ≪ ReflectionExtension
  • ReflectionExtension::__construct ≫
  • PHP Manual
  • ReflectionExtension
  • Clones

ReflectionExtension::__clone

(PHP 5, PHP 7)

ReflectionExtension::__clone — Clones

설명

final private void ReflectionExtension::__clone ( void )

The clone method prevents an object from being cloned. Reflection objects cannot be cloned.

인수

이 함수는 인수가 없습니다.

반환값

No value is returned, if called a fatal error will occur.

참고

  • ReflectionExtension::__construct() - Constructs a ReflectionExtension
  • Object cloning

User Contributed Notes

There are no user contributed notes for this page.


Constructs a ReflectionExtension

  • ≪ ReflectionExtension::__clone
  • ReflectionExtension::export ≫
  • PHP Manual
  • ReflectionExtension
  • Constructs a ReflectionExtension

ReflectionExtension::__construct

(PHP 5, PHP 7)

ReflectionExtension::__construct — Constructs a ReflectionExtension

설명

public ReflectionExtension::__construct ( string $name )

Construct a ReflectionExtension object.

인수

name

Name of the extension.

반환값

A ReflectionExtension object.

예제

Example #1 ReflectionExtension example

<?php
$ext = new ReflectionExtension('Reflection');

printf('Extension: %s (version: %s)', $ext->getName(), $ext->getVersion());
?>

위 예제의 출력 예시:

Extension: Reflection (version: $Revision: 330543 $)

참고

  • ReflectionExtension::info() - Print extension info
  • Constructors

User Contributed Notes

There are no user contributed notes for this page.


Export

  • ≪ ReflectionExtension::__construct
  • ReflectionExtension::getClasses ≫
  • PHP Manual
  • ReflectionExtension
  • Export

ReflectionExtension::export

(PHP 5, PHP 7)

ReflectionExtension::export — Export

설명

public static string ReflectionExtension::export ( string $name [, string $return = false ] )

Exports a reflected extension. The output format of this function is the same as the CLI argument --re [extension].

인수

name

내보낼 reflection

return

TRUE로 설정하면 export를 반환하고, 반대의 경우는 버립니다. FALSE로 설정(기본값)은 반대를 수행합니다.

반환값

return 인수를 TRUE로 설정하면, export를 string으로 반환합니다. 그렇지 않으면 NULL을 반환합니다.

참고

  • ReflectionExtension::info() - Print extension info

User Contributed Notes

There are no user contributed notes for this page.


Gets classes

  • ≪ ReflectionExtension::export
  • ReflectionExtension::getClassNames ≫
  • PHP Manual
  • ReflectionExtension
  • Gets classes

ReflectionExtension::getClasses

(PHP 5, PHP 7)

ReflectionExtension::getClasses — Gets classes

설명

public array ReflectionExtension::getClasses ( void )

Gets a list of classes from an extension.

인수

이 함수는 인수가 없습니다.

반환값

An array of ReflectionClass objects, one for each class within the extension. If no classes are defined, an empty array is returned.

예제

Example #1 ReflectionExtension::getClasses() example

<?php
$ext = new ReflectionExtension('XMLWriter');
var_dump($ext->getClasses());
?>

위 예제의 출력 예시:

array(1) {
  ["XMLWriter"]=>
  &object(ReflectionClass)#2 (1) {
    ["name"]=>
    string(9) "XMLWriter"
  }
}

참고

  • ReflectionExtension::getClassNames() - Gets class names

User Contributed Notes

There are no user contributed notes for this page.


Gets class names

  • ≪ ReflectionExtension::getClasses
  • ReflectionExtension::getConstants ≫
  • PHP Manual
  • ReflectionExtension
  • Gets class names

ReflectionExtension::getClassNames

(PHP 5, PHP 7)

ReflectionExtension::getClassNames — Gets class names

설명

public array ReflectionExtension::getClassNames ( void )

Gets a listing of class names as defined in the extension.

인수

이 함수는 인수가 없습니다.

반환값

An array of class names, as defined in the extension. If no classes are defined, an empty array is returned.

예제

Example #1 ReflectionExtension::getClassNames() example

<?php
$ext = new ReflectionExtension('XMLWriter');
var_dump($ext->getClassNames());
?>

위 예제의 출력 예시:

array(1) {
  [0]=>
  string(9) "XMLWriter"
}

참고

  • ReflectionExtension::getClasses() - Gets classes
  • ReflectionExtension::getName() - Gets extension name

User Contributed Notes

There are no user contributed notes for this page.


Gets constants

  • ≪ ReflectionExtension::getClassNames
  • ReflectionExtension::getDependencies ≫
  • PHP Manual
  • ReflectionExtension
  • Gets constants

ReflectionExtension::getConstants

(PHP 5, PHP 7)

ReflectionExtension::getConstants — Gets constants

설명

public array ReflectionExtension::getConstants ( void )

Get defined constants from an extension.

인수

이 함수는 인수가 없습니다.

반환값

An associative array with constant names as keys.

예제

Example #1 ReflectionExtension::getConstants() example

<?php
$ext = new ReflectionExtension('DOM');

print_r($ext->getConstants());
?>

위 예제의 출력 예시:

Array
(
    [XML_ELEMENT_NODE] => 1
    [XML_ATTRIBUTE_NODE] => 2
    [XML_TEXT_NODE] => 3
    [XML_CDATA_SECTION_NODE] => 4
    [XML_ENTITY_REF_NODE] => 5
    [XML_ENTITY_NODE] => 6
    [XML_PI_NODE] => 7
    [XML_COMMENT_NODE] => 8
    [XML_DOCUMENT_NODE] => 9
    [XML_DOCUMENT_TYPE_NODE] => 10
    [XML_DOCUMENT_FRAG_NODE] => 11
    [XML_NOTATION_NODE] => 12
    [XML_HTML_DOCUMENT_NODE] => 13
    [XML_DTD_NODE] => 14
    [XML_ELEMENT_DECL_NODE] => 15
    [XML_ATTRIBUTE_DECL_NODE] => 16
    [XML_ENTITY_DECL_NODE] => 17
    [XML_NAMESPACE_DECL_NODE] => 18
    [XML_LOCAL_NAMESPACE] => 18
    [XML_ATTRIBUTE_CDATA] => 1
    [XML_ATTRIBUTE_ID] => 2
    [XML_ATTRIBUTE_IDREF] => 3
    [XML_ATTRIBUTE_IDREFS] => 4
    [XML_ATTRIBUTE_ENTITY] => 6
    [XML_ATTRIBUTE_NMTOKEN] => 7
    [XML_ATTRIBUTE_NMTOKENS] => 8
    [XML_ATTRIBUTE_ENUMERATION] => 9
    [XML_ATTRIBUTE_NOTATION] => 10
    [DOM_PHP_ERR] => 0
    [DOM_INDEX_SIZE_ERR] => 1
    [DOMSTRING_SIZE_ERR] => 2
    [DOM_HIERARCHY_REQUEST_ERR] => 3
    [DOM_WRONG_DOCUMENT_ERR] => 4
    [DOM_INVALID_CHARACTER_ERR] => 5
    [DOM_NO_DATA_ALLOWED_ERR] => 6
    [DOM_NO_MODIFICATION_ALLOWED_ERR] => 7
    [DOM_NOT_FOUND_ERR] => 8
    [DOM_NOT_SUPPORTED_ERR] => 9
    [DOM_INUSE_ATTRIBUTE_ERR] => 10
    [DOM_INVALID_STATE_ERR] => 11
    [DOM_SYNTAX_ERR] => 12
    [DOM_INVALID_MODIFICATION_ERR] => 13
    [DOM_NAMESPACE_ERR] => 14
    [DOM_INVALID_ACCESS_ERR] => 15
    [DOM_VALIDATION_ERR] => 16
)

참고

  • ReflectionExtension::getINIEntries() - Gets extension ini entries

User Contributed Notes

There are no user contributed notes for this page.


Gets dependencies

  • ≪ ReflectionExtension::getConstants
  • ReflectionExtension::getFunctions ≫
  • PHP Manual
  • ReflectionExtension
  • Gets dependencies

ReflectionExtension::getDependencies

(PHP 5, PHP 7)

ReflectionExtension::getDependencies — Gets dependencies

설명

public array ReflectionExtension::getDependencies ( void )

Gets dependencies, by listing both required and conflicting dependencies.

인수

이 함수는 인수가 없습니다.

반환값

An associative array with dependencies as keys and either Required, Optional or Conflicts as the values.

예제

Example #1 ReflectionExtension::getDependencies() example

<?php
$dom = new ReflectionExtension('dom');

print_r($dom->getDependencies());
?>

위 예제의 출력 예시:

Array
(
    [libxml] => Required
    [domxml] => Conflicts
)

참고

  • ReflectionClass::getVersion()

User Contributed Notes

There are no user contributed notes for this page.


Gets extension functions

  • ≪ ReflectionExtension::getDependencies
  • ReflectionExtension::getINIEntries ≫
  • PHP Manual
  • ReflectionExtension
  • Gets extension functions

ReflectionExtension::getFunctions

(PHP 5, PHP 7)

ReflectionExtension::getFunctions — Gets extension functions

설명

public array ReflectionExtension::getFunctions ( void )

Get defined functions from an extension.

인수

이 함수는 인수가 없습니다.

반환값

An associative array of ReflectionFunction objects, for each function defined in the extension with the keys being the function names. If no function are defined, an empty array is returned.

예제

Example #1 ReflectionExtension::getFunctions() example

<?php
$dom = new ReflectionExtension('SimpleXML');

print_r($dom->getFunctions());
?>

위 예제의 출력 예시:

Array
(
    [simplexml_load_file] => ReflectionFunction Object
        (
            [name] => simplexml_load_file
        )

    [simplexml_load_string] => ReflectionFunction Object
        (
            [name] => simplexml_load_string
        )

    [simplexml_import_dom] => ReflectionFunction Object
        (
            [name] => simplexml_import_dom
        )

)

참고

  • ReflectionExtension::getClasses() - Gets classes
  • get_extension_funcs() - 모듈 함수명을 배열로 반환

User Contributed Notes

There are no user contributed notes for this page.


Gets extension ini entries

  • ≪ ReflectionExtension::getFunctions
  • ReflectionExtension::getName ≫
  • PHP Manual
  • ReflectionExtension
  • Gets extension ini entries

ReflectionExtension::getINIEntries

(PHP 5, PHP 7)

ReflectionExtension::getINIEntries — Gets extension ini entries

설명

public array ReflectionExtension::getINIEntries ( void )

Get the ini entries for an extension.

인수

이 함수는 인수가 없습니다.

반환값

An associative array with the ini entries as keys, with their defined values as values.

예제

Example #1 ReflectionExtension::getINIEntries() example

<?php
$ext = new ReflectionExtension('mysql');

print_r($ext->getINIEntries());
?>

위 예제의 출력 예시:

Array
(
    [mysql.allow_persistent] => 1
    [mysql.max_persistent] => -1
    [mysql.max_links] => -1
    [mysql.default_host] =>
    [mysql.default_user] =>
    [mysql.default_password] =>
    [mysql.default_port] =>
    [mysql.default_socket] =>
    [mysql.connect_timeout] => 60
    [mysql.trace_mode] =>
    [mysql.allow_local_infile] => 1
    [mysql.cache_size] => 2000
)

참고

  • ini_get_all() - 모든 설정 옵션을 얻습니다
  • ReflectionExtension::getConstants() - Gets constants

User Contributed Notes

There are no user contributed notes for this page.


Gets extension name

  • ≪ ReflectionExtension::getINIEntries
  • ReflectionExtension::getVersion ≫
  • PHP Manual
  • ReflectionExtension
  • Gets extension name

ReflectionExtension::getName

(PHP 5, PHP 7)

ReflectionExtension::getName — Gets extension name

설명

public string ReflectionExtension::getName ( void )

Gets the extensions name.

인수

이 함수는 인수가 없습니다.

반환값

The extensions name.

예제

Example #1 ReflectionExtension::getName() example

<?php
$ext = new ReflectionExtension('mysqli');
var_dump($ext->getName());
?>

위 예제의 출력 예시:

string(6) "mysqli"

참고

  • ReflectionExtension::getClassNames() - Gets class names

User Contributed Notes

There are no user contributed notes for this page.


Gets extension version

  • ≪ ReflectionExtension::getName
  • ReflectionExtension::info ≫
  • PHP Manual
  • ReflectionExtension
  • Gets extension version

ReflectionExtension::getVersion

(PHP 5, PHP 7)

ReflectionExtension::getVersion — Gets extension version

설명

public string ReflectionExtension::getVersion ( void )

Gets the version of the extension.

인수

이 함수는 인수가 없습니다.

반환값

The version of the extension.

예제

Example #1 ReflectionExtension::getVersion() example

<?php
$ext = new ReflectionExtension('mysqli');
var_dump($ext->getVersion());
?>

위 예제의 출력 예시:

string(3) "0.1"

참고

  • ReflectionExtension::info() - Print extension info

User Contributed Notes

There are no user contributed notes for this page.


Print extension info

  • ≪ ReflectionExtension::getVersion
  • ReflectionExtension::isPersistent ≫
  • PHP Manual
  • ReflectionExtension
  • Print extension info

ReflectionExtension::info

(PHP 5, PHP 7)

ReflectionExtension::info — Print extension info

설명

public void ReflectionExtension::info ( void )

Prints out the "phpinfo()" snippet for the given extension.

인수

이 함수는 인수가 없습니다.

반환값

Information about the extension.

예제

Example #1 ReflectionExtension::info() example

<?php
$ext = new ReflectionExtension('mysqli');
$ext->info();
?>

위 예제의 출력 예시:

mysqli

MysqlI Support => enabled
Client API library version => mysqlnd 5.0.5-dev - 081106 - $Revision: 315808 $
Active Persistent Links => 0
Inactive Persistent Links => 0
Active Links => 0
Persistent cache => enabled
put_hits => 0
put_misses => 0
get_hits => 0
get_misses => 0
size => 2000
free_items => 2000
references => 2

Directive => Local Value => Master Value
mysqli.max_links => Unlimited => Unlimited
mysqli.max_persistent => Unlimited => Unlimited
mysqli.allow_persistent => On => On
mysqli.default_host => no value => no value
mysqli.default_user => no value => no value
mysqli.default_pw => no value => no value
mysqli.default_port => 3306 => 3306
mysqli.default_socket => no value => no value
mysqli.reconnect => Off => Off
mysqli.allow_local_infile => On => On
mysqli.cache_size => 2000 => 2000

참고

  • ReflectionExtension::getName() - Gets extension name
  • phpinfo() - Outputs information about PHP's configuration

User Contributed Notes

There are no user contributed notes for this page.


Returns whether this extension is persistent

  • ≪ ReflectionExtension::info
  • ReflectionExtension::isTemporary ≫
  • PHP Manual
  • ReflectionExtension
  • Returns whether this extension is persistent

ReflectionExtension::isPersistent

(PHP >= 5.4.0)

ReflectionExtension::isPersistent — Returns whether this extension is persistent

설명

public void ReflectionExtension::isPersistent ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE for extensions loaded by extension, FALSE otherwise.

참고

  • ReflectionExtension::isTemporary() - Returns whether this extension is temporary

User Contributed Notes

There are no user contributed notes for this page.


Returns whether this extension is temporary

  • ≪ ReflectionExtension::isPersistent
  • ReflectionExtension::__toString ≫
  • PHP Manual
  • ReflectionExtension
  • Returns whether this extension is temporary

ReflectionExtension::isTemporary

(PHP >= 5.4.0)

ReflectionExtension::isTemporary — Returns whether this extension is temporary

설명

public void ReflectionExtension::isTemporary ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE for extensions loaded by dl(), FALSE otherwise.

참고

  • ReflectionExtension::isPersistent() - Returns whether this extension is persistent

User Contributed Notes

There are no user contributed notes for this page.


To string

  • ≪ ReflectionExtension::isTemporary
  • ReflectionFunction ≫
  • PHP Manual
  • ReflectionExtension
  • To string

ReflectionExtension::__toString

(PHP 5, PHP 7)

ReflectionExtension::__toString — To string

설명

public string ReflectionExtension::__toString ( void )

Exports a reflected extension and returns it as a string. This is the same as the ReflectionExtension::export() with the return set to TRUE.

인수

이 함수는 인수가 없습니다.

반환값

Returns the exported extension as a string, in the same way as the ReflectionExtension::export().

참고

  • ReflectionExtension::__construct() - Constructs a ReflectionExtension
  • ReflectionExtension::export() - Export
  • __toString()

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionFunction class

(PHP 5, PHP 7)

소개

The ReflectionFunction class reports information about a function.

클래스 개요

ReflectionFunction extends ReflectionFunctionAbstract implements Reflector {
/* 상수 */
const integer IS_DEPRECATED = 262144 ;
/* 프로퍼티 */
public $name ;
/* 메소드 */
public __construct ( mixed $name )
public static string export ( string $name [, string $return ] )
public Closure getClosure ( void )
public mixed invoke ([ mixed $parameter [, mixed $... ]] )
public mixed invokeArgs ( array $args )
public bool isDisabled ( void )
public string __toString ( void )
/* 상속된 메소드 */
final private void ReflectionFunctionAbstract::__clone ( void )
public ReflectionClass ReflectionFunctionAbstract::getClosureScopeClass ( void )
public object ReflectionFunctionAbstract::getClosureThis ( void )
public string ReflectionFunctionAbstract::getDocComment ( void )
public int ReflectionFunctionAbstract::getEndLine ( void )
public ReflectionExtension ReflectionFunctionAbstract::getExtension ( void )
public string ReflectionFunctionAbstract::getExtensionName ( void )
public string ReflectionFunctionAbstract::getFileName ( void )
public string ReflectionFunctionAbstract::getName ( void )
public string ReflectionFunctionAbstract::getNamespaceName ( void )
public int ReflectionFunctionAbstract::getNumberOfParameters ( void )
public int ReflectionFunctionAbstract::getNumberOfRequiredParameters ( void )
public array ReflectionFunctionAbstract::getParameters ( void )
public ReflectionType ReflectionFunctionAbstract::getReturnType ( void )
public string ReflectionFunctionAbstract::getShortName ( void )
public int ReflectionFunctionAbstract::getStartLine ( void )
public array ReflectionFunctionAbstract::getStaticVariables ( void )
public bool ReflectionFunctionAbstract::hasReturnType ( void )
public bool ReflectionFunctionAbstract::inNamespace ( void )
public bool ReflectionFunctionAbstract::isClosure ( void )
public bool ReflectionFunctionAbstract::isDeprecated ( void )
public bool ReflectionFunctionAbstract::isGenerator ( void )
public bool ReflectionFunctionAbstract::isInternal ( void )
public bool ReflectionFunctionAbstract::isUserDefined ( void )
public bool ReflectionFunctionAbstract::isVariadic ( void )
public bool ReflectionFunctionAbstract::returnsReference ( void )
abstract public void ReflectionFunctionAbstract::__toString ( void )
}

프로퍼티

name

Name of the function. Read-only, throws ReflectionException in attempt to write.

예약 상수

ReflectionFunction Modifiers

ReflectionFunction::IS_DEPRECATED

Indicates deprecated functions.

Table of Contents

  • ReflectionFunction::__construct — Constructs a ReflectionFunction object
  • ReflectionFunction::export — Exports function
  • ReflectionFunction::getClosure — Returns a dynamically created closure for the function
  • ReflectionFunction::invoke — Invokes function
  • ReflectionFunction::invokeArgs — Invokes function args
  • ReflectionFunction::isDisabled — Checks if function is disabled
  • ReflectionFunction::__toString — To string

User Contributed Notes

Lorenz R.S. 12-Aug-2011 10:24
Here is a concise example of ReflectionFunction usage for Parameter Reflection / introspection (e.g. to automatically generate API descriptions)

<?php
$properties = $reflector->getProperties();
$refFunc = new ReflectionFunction('preg_replace');
foreach( $refFunc->getParameters() as $param ){
    //invokes ■ReflectionParameter::__toString
    print $param;
}
?>

prints:

Parameter #0 [ <required> $regex ]
Parameter #1 [ <required> $replace ]
Parameter #2 [ <required> $subject ]
Parameter #3 [ <optional> $limit ]
Parameter #4 [ <optional> &$count ]


Constructs a ReflectionFunction object

  • ≪ ReflectionFunction
  • ReflectionFunction::export ≫
  • PHP Manual
  • ReflectionFunction
  • Constructs a ReflectionFunction object

ReflectionFunction::__construct

(PHP 5, PHP 7)

ReflectionFunction::__construct — Constructs a ReflectionFunction object

설명

public ReflectionFunction::__construct ( mixed $name )

Constructs a ReflectionFunction object.

인수

name

The name of the function to reflect or a closure.

반환값

값을 반환하지 않습니다.

오류/예외

A ReflectionException if the name parameter does not contain a valid function.

변경점

버전 | 설명
5.3.0 | name can now be a closure.

예제

Example #1 ReflectionFunction::__construct() example

<?php
/**
 * A simple counter
 *
 * @return    int
 */
function counter1()
{
    static $c = 0;
    return ++$c;
}

/**
 * Another simple counter
 *
 * @return    int
 */
$counter2 = function()
{
    static $d = 0;
    return ++$d;

};

function dumpReflectionFunction($func)
{
    // Print out basic information
    printf(
        "\n\n===> The %s function '%s'\n".
        "     declared in %s\n".
        "     lines %d to %d\n",
        $func->isInternal() ? 'internal' : 'user-defined',
        $func->getName(),
        $func->getFileName(),
        $func->getStartLine(),
        $func->getEndline()
    );

    // Print documentation comment
    printf("---> Documentation:\n %s\n", var_export($func->getDocComment(), 1));

    // Print static variables if existant
    if ($statics = $func->getStaticVariables())
    {
        printf("---> Static variables: %s\n", var_export($statics, 1));
    }
}

// Create an instance of the ReflectionFunction class
dumpReflectionFunction(new ReflectionFunction('counter1'));
dumpReflectionFunction(new ReflectionFunction($counter2));
?>

위 예제의 출력 예시:

===> The user-defined function 'counter1'
     declared in Z:\reflectcounter.php
     lines 7 to 11
---> Documentation:
 '/**
 * A simple counter
 *
 * @return    int
 */'
---> Static variables: array (
  'c' => 0,
)

===> The user-defined function '{closure}'
     declared in Z:\reflectcounter.php
     lines 18 to 23
---> Documentation:
 '/**
 * Another simple counter
 *
 * @return    int
 */'
---> Static variables: array (
  'd' => 0,
)

참고

  • ReflectionMethod::__construct() - Constructs a ReflectionMethod
  • Constructors

User Contributed Notes

There are no user contributed notes for this page.


Exports function

  • ≪ ReflectionFunction::__construct
  • ReflectionFunction::getClosure ≫
  • PHP Manual
  • ReflectionFunction
  • Exports function

ReflectionFunction::export

(PHP 5, PHP 7)

ReflectionFunction::export — Exports function

설명

public static string ReflectionFunction::export ( string $name [, string $return ] )

Exports a Reflected function.

인수

name

내보낼 reflection

return

TRUE로 설정하면 export를 반환하고, 반대의 경우는 버립니다. FALSE로 설정(기본값)은 반대를 수행합니다.

반환값

return 인수를 TRUE로 설정하면, export를 string으로 반환합니다. 그렇지 않으면 NULL을 반환합니다.

참고

  • ReflectionFunction::invoke() - Invokes function

User Contributed Notes

hytest at gmail dot com 22-Nov-2011 05:04
Example:

<?php
function title($title, $name)
{
    return sprintf("%s. %s\r\n", $title, $name);
}

echo ReflectionFunction::export('title',true);

?>

Output:

Function [ <user> function title ] {
  @@ /path/to/file.php 2 - 5

  - Parameters [2] {
    Parameter #0 [ <required> $title ]
    Parameter #1 [ <required> $name ]
  }
}


Returns a dynamically created closure for the function

  • ≪ ReflectionFunction::export
  • ReflectionFunction::invoke ≫
  • PHP Manual
  • ReflectionFunction
  • Returns a dynamically created closure for the function

ReflectionFunction::getClosure

(PHP >= 5.4.0)

ReflectionFunction::getClosure — Returns a dynamically created closure for the function

설명

public Closure ReflectionFunction::getClosure ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns Closure. Returns NULL in case of an error.

User Contributed Notes

php dot net-bug at spamhereplease dot com 23-Nov-2013 02:34
If you have a callback that is going to be invoked often, you can use this method (and the related method for ReflectProperty) to get a closure that will execute at near native speed.

Some benchmark comparisons with php 5.4.22 on ubuntu:

[function call]      [time]     [%slower]
function():          0.09527      0%
Reflection::Closure: 0.09729      2%
$string():           0.102641     8%
Reflection:          0.197122   107%
call_user_func:      0.214045   125%

[method call]        [time]  [% slower] [%vs func]
->method():          0.103611    0%       9%
Reflection::Closure: 0.1043      1%       9%
$string():           0.109885    6%      15%
Reflection:          0.20451    97%     115%
call_user_func:      0.219205  112%     130%


Invokes function

  • ≪ ReflectionFunction::getClosure
  • ReflectionFunction::invokeArgs ≫
  • PHP Manual
  • ReflectionFunction
  • Invokes function

ReflectionFunction::invoke

(PHP 5, PHP 7)

ReflectionFunction::invoke — Invokes function

설명

public mixed ReflectionFunction::invoke ([ mixed $parameter [, mixed $... ]] )

Invokes a reflected function.

인수

args

The passed in argument list. It accepts a variable number of arguments which are passed to the function much like call_user_func() is.

반환값

Returns the result of the invoked function call.

예제

Example #1 ReflectionFunction::invoke() example

<?php
function title($title, $name)
{
    return sprintf("%s. %s\r\n", $title, $name);
}

$function = new ReflectionFunction('title');

echo $function->invoke('Dr', 'Phil');
?>

위 예제의 출력:

Dr. Phil

주의

Note:

함수가 인수로 참조를 필요로 하면, 주어진 인수 목록에도 참조여야 합니다.

참고

  • ReflectionFunction::export() - Exports function
  • __invoke()
  • call_user_func() - Call the callback given by the first parameter

User Contributed Notes

spark at limao dot com dot br 10-Jun-2011 07:55
I know Reflections classes have a lot of power, but sometimes all we need is to store a annonymus function or even create a simple callback somewhere.

so here it is, the Callback class:
<?php
    class Callback{
        private $name = false;

        public function Callback($obj,$call=false){
            $name = array($obj);
            if($call) $name[] = $call;

            $this->name = $name;
        }
        public function invoke($params=array()){
            return call_user_func_array($this->name,$params);
        }
    }
?>

Usage:
<?php
    function sayName(){
        return "goku";
    }

    $myVar = new Callback("sayName");

    echo "Hi, I am ".$myVar->invoke()."!";

    //also works with methods: new Callback($obj,"method");
?>


Invokes function args

  • ≪ ReflectionFunction::invoke
  • ReflectionFunction::isDisabled ≫
  • PHP Manual
  • ReflectionFunction
  • Invokes function args

ReflectionFunction::invokeArgs

(PHP 5 >= 5.1.0, PHP 7)

ReflectionFunction::invokeArgs — Invokes function args

설명

public mixed ReflectionFunction::invokeArgs ( array $args )

Invokes the function and pass its arguments as array.

인수

args

The passed arguments to the function as an array, much like call_user_func_array() works.

반환값

Returns the result of the invoked function

예제

Example #1 ReflectionFunction::invokeArgs() example

<?php
function title($title, $name)
{
    return sprintf("%s. %s\r\n", $title, $name);
}

$function = new ReflectionFunction('title');

echo $function->invokeArgs(array('Dr', 'Phil'));
?>

위 예제의 출력:

Dr. Phil

Example #2 ReflectionFunction::invokeArgs() with references example

<?php
function get_false_conditions(array $conditions, array &$false_conditions)
{
    foreach ($conditions as $condition) {
        if (!$condition) {
            $false_conditions[] = $condition;
        }
    }
}

$function_ref     = new ReflectionFunction('get_false_conditions');

$conditions       = array(true, false, -1, 0, 1);
$false_conditions = array();

$function_ref->invokeArgs(array($conditions, &$false_conditions));

var_dump($false_conditions);
?>

위 예제의 출력:

array(2) {
  [0]=>
  bool(false)
  [1]=>
  int(0)
}

주의

Note:

함수가 인수로 참조를 필요로 하면, 주어진 인수 목록에도 참조여야 합니다.

참고

  • ReflectionFunction::invoke() - Invokes function
  • ReflectionFunctionAbstract::getNumberOfParameters() - Gets number of parameters
  • __invoke()
  • call_user_func_array() - Call a callback with an array of parameters

User Contributed Notes

Andrea Giammarchi 17-Feb-2008 03:41
I encountered a weird problem with ReflectionFunction, described in ticket 44139 of PHP Bugs.

If for some reason you need to call with invoke, or invokeArgs, a function like array_unshift (that accepts internally the array by reference) you could use this code to avoid the generated warning or fatal error.

<?php
function unshift(){
    $ref        = new ReflectionFunction('array_unshift');
    $arguments    = func_get_args();
    return        $ref->invokeArgs(array_merge(array(&$this->arr), $arguments));
}
?>

I don't know about performances (you can create an array manually too, starting from array(&$this->something) and adding arguments). However, it seems to work correctly without problems, at least until the send by reference will be usable with one single value ...


Checks if function is disabled

  • ≪ ReflectionFunction::invokeArgs
  • ReflectionFunction::__toString ≫
  • PHP Manual
  • ReflectionFunction
  • Checks if function is disabled

ReflectionFunction::isDisabled

(PHP 5, PHP 7)

ReflectionFunction::isDisabled — Checks if function is disabled

설명

public bool ReflectionFunction::isDisabled ( void )

Checks if the function is disabled, via the disable_functions directive.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if it's disable, otherwise FALSE

참고

  • ReflectionFunctionAbstract::isUserDefined() - Checks if user defined
  • disable_functions directive

User Contributed Notes

There are no user contributed notes for this page.


To string

  • ≪ ReflectionFunction::isDisabled
  • ReflectionFunctionAbstract ≫
  • PHP Manual
  • ReflectionFunction
  • To string

ReflectionFunction::__toString

(PHP 5, PHP 7)

ReflectionFunction::__toString — To string

설명

public string ReflectionFunction::__toString ( void )

To string.

인수

이 함수는 인수가 없습니다.

반환값

Returns ReflectionFunction::export()-like output for the function.

예제

Example #1 ReflectionFunction::__toString() example

<?php
function title($title, $name)
{
    return sprintf("%s. %s\r\n", $title, $name);
}

echo new ReflectionFunction('title');
?>

위 예제의 출력 예시:

Function [ <user> function title ] {
  @@ Command line code 1 - 1

  - Parameters [2] {
    Parameter #0 [ <required> $title ]
    Parameter #1 [ <required> $name ]
  }
}

참고

  • ReflectionFunction::export() - Exports function
  • ReflectionClass::clone()
  • __toString()

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionFunctionAbstract class

(PHP 5, PHP 7)

소개

A parent class to ReflectionFunction, read its description for details.

클래스 개요

ReflectionFunctionAbstract implements Reflector {
/* 프로퍼티 */
public $name ;
/* 메소드 */
final private void __clone ( void )
public ReflectionClass getClosureScopeClass ( void )
public object getClosureThis ( void )
public string getDocComment ( void )
public int getEndLine ( void )
public ReflectionExtension getExtension ( void )
public string getExtensionName ( void )
public string getFileName ( void )
public string getName ( void )
public string getNamespaceName ( void )
public int getNumberOfParameters ( void )
public int getNumberOfRequiredParameters ( void )
public array getParameters ( void )
public ReflectionType getReturnType ( void )
public string getShortName ( void )
public int getStartLine ( void )
public array getStaticVariables ( void )
public bool hasReturnType ( void )
public bool inNamespace ( void )
public bool isClosure ( void )
public bool isDeprecated ( void )
public bool isGenerator ( void )
public bool isInternal ( void )
public bool isUserDefined ( void )
public bool isVariadic ( void )
public bool returnsReference ( void )
abstract public void __toString ( void )
}

프로퍼티

name

Name of the function. Read-only, throws ReflectionException in attempt to write.

Table of Contents

  • ReflectionFunctionAbstract::__clone — Clones function
  • ReflectionFunctionAbstract::getClosureScopeClass — Returns the scope associated to the closure
  • ReflectionFunctionAbstract::getClosureThis — Returns this pointer bound to closure
  • ReflectionFunctionAbstract::getDocComment — Gets doc comment
  • ReflectionFunctionAbstract::getEndLine — Gets end line number
  • ReflectionFunctionAbstract::getExtension — Gets extension info
  • ReflectionFunctionAbstract::getExtensionName — Gets extension name
  • ReflectionFunctionAbstract::getFileName — Gets file name
  • ReflectionFunctionAbstract::getName — Gets function name
  • ReflectionFunctionAbstract::getNamespaceName — Gets namespace name
  • ReflectionFunctionAbstract::getNumberOfParameters — Gets number of parameters
  • ReflectionFunctionAbstract::getNumberOfRequiredParameters — Gets number of required parameters
  • ReflectionFunctionAbstract::getParameters — Gets parameters
  • ReflectionFunctionAbstract::getReturnType — Gets the specified return type of a function
  • ReflectionFunctionAbstract::getShortName — Gets function short name
  • ReflectionFunctionAbstract::getStartLine — Gets starting line number
  • ReflectionFunctionAbstract::getStaticVariables — Gets static variables
  • ReflectionFunctionAbstract::hasReturnType — Checks if the function has a specified return type
  • ReflectionFunctionAbstract::inNamespace — Checks if function in namespace
  • ReflectionFunctionAbstract::isClosure — Checks if closure
  • ReflectionFunctionAbstract::isDeprecated — Checks if deprecated
  • ReflectionFunctionAbstract::isGenerator — Returns whether this function is a generator
  • ReflectionFunctionAbstract::isInternal — Checks if is internal
  • ReflectionFunctionAbstract::isUserDefined — Checks if user defined
  • ReflectionFunctionAbstract::isVariadic — Checks if the function is variadic
  • ReflectionFunctionAbstract::returnsReference — Checks if returns reference
  • ReflectionFunctionAbstract::__toString — To string

User Contributed Notes

There are no user contributed notes for this page.


Clones function

  • ≪ ReflectionFunctionAbstract
  • ReflectionFunctionAbstract::getClosureScopeClass ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Clones function

ReflectionFunctionAbstract::__clone

(PHP 5, PHP 7)

ReflectionFunctionAbstract::__clone — Clones function

설명

final private void ReflectionFunctionAbstract::__clone ( void )

Clones a function.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

참고

  • Object cloning

User Contributed Notes

There are no user contributed notes for this page.


Returns the scope associated to the closure

  • ≪ ReflectionFunctionAbstract::__clone
  • ReflectionFunctionAbstract::getClosureThis ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Returns the scope associated to the closure

ReflectionFunctionAbstract::getClosureScopeClass

(PHP >= 5.4.0)

ReflectionFunctionAbstract::getClosureScopeClass — Returns the scope associated to the closure

설명

public ReflectionClass ReflectionFunctionAbstract::getClosureScopeClass ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns the class on success or NULL on failure.

User Contributed Notes

There are no user contributed notes for this page.


Returns this pointer bound to closure

  • ≪ ReflectionFunctionAbstract::getClosureScopeClass
  • ReflectionFunctionAbstract::getDocComment ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Returns this pointer bound to closure

ReflectionFunctionAbstract::getClosureThis

(PHP >= 5.4.0)

ReflectionFunctionAbstract::getClosureThis — Returns this pointer bound to closure

설명

public object ReflectionFunctionAbstract::getClosureThis ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns $this pointer. Returns NULL in case of an error.

User Contributed Notes

There are no user contributed notes for this page.


Gets doc comment

  • ≪ ReflectionFunctionAbstract::getClosureThis
  • ReflectionFunctionAbstract::getEndLine ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets doc comment

ReflectionFunctionAbstract::getDocComment

(PHP 5 >= 5.1.0, PHP 7)

ReflectionFunctionAbstract::getDocComment — Gets doc comment

설명

public string ReflectionFunctionAbstract::getDocComment ( void )

Get a Doc comment from a function.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The doc comment if it exists, otherwise FALSE

참고

  • ReflectionFunctionAbstract::getStartLine() - Gets starting line number

User Contributed Notes

jens dot kohl+php at gmail dot com 26-Mar-2012 08:19
Beware when you try to use the getDocComment() methods with eAccelerator turned on. eAccelerator ≫optimizes≪ your code in a way that it removes any comments from your source. So at runtime you're not able to get any comments and therefor getDocComments return false.
bruno at vault7 dot com 13-Nov-2009 08:09
In order to retrieve a comment with this method, the comment must be imediately before the class, function or method, start with /** and end with */


Gets end line number

  • ≪ ReflectionFunctionAbstract::getDocComment
  • ReflectionFunctionAbstract::getExtension ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets end line number

ReflectionFunctionAbstract::getEndLine

(PHP 5, PHP 7)

ReflectionFunctionAbstract::getEndLine — Gets end line number

설명

public int ReflectionFunctionAbstract::getEndLine ( void )

Get the ending line number.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The ending line number of the user defined function, or FALSE if unknown.

참고

  • ReflectionFunctionAbstract::getStartLine() - Gets starting line number

User Contributed Notes

There are no user contributed notes for this page.


Gets extension info

  • ≪ ReflectionFunctionAbstract::getEndLine
  • ReflectionFunctionAbstract::getExtensionName ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets extension info

ReflectionFunctionAbstract::getExtension

(PHP 5, PHP 7)

ReflectionFunctionAbstract::getExtension — Gets extension info

설명

public ReflectionExtension ReflectionFunctionAbstract::getExtension ( void )

Get the extension information of a function.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The extension information, as a ReflectionExtension object.

참고

  • ReflectionFunctionAbstract::getExtensionName() - Gets extension name

User Contributed Notes

There are no user contributed notes for this page.


Gets extension name

  • ≪ ReflectionFunctionAbstract::getExtension
  • ReflectionFunctionAbstract::getFileName ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets extension name

ReflectionFunctionAbstract::getExtensionName

(PHP 5, PHP 7)

ReflectionFunctionAbstract::getExtensionName — Gets extension name

설명

public string ReflectionFunctionAbstract::getExtensionName ( void )

Get the extensions name.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The extensions name.

참고

  • ReflectionFunctionAbstract::getExtension() - Gets extension info

User Contributed Notes

There are no user contributed notes for this page.


Gets file name

  • ≪ ReflectionFunctionAbstract::getExtensionName
  • ReflectionFunctionAbstract::getName ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets file name

ReflectionFunctionAbstract::getFileName

(PHP 5, PHP 7)

ReflectionFunctionAbstract::getFileName — Gets file name

설명

public string ReflectionFunctionAbstract::getFileName ( void )

Gets the file name from a user-defined function.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The file name.

참고

  • ReflectionFunctionAbstract::getNamespaceName() - Gets namespace name

User Contributed Notes

stein at visibone dot com 05-Jul-2013 07:53
Returns FALSE for an internal function (when isInternal() returns TRUE, e.g. for 'strlen').


Gets function name

  • ≪ ReflectionFunctionAbstract::getFileName
  • ReflectionFunctionAbstract::getNamespaceName ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets function name

ReflectionFunctionAbstract::getName

(PHP 5, PHP 7)

ReflectionFunctionAbstract::getName — Gets function name

설명

public string ReflectionFunctionAbstract::getName ( void )

Get the name of the function.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The name of the function.

참고

  • ReflectionFunctionAbstract::getExtensionName() - Gets extension name
  • ReflectionFunctionAbstract::isUserDefined() - Checks if user defined

User Contributed Notes

There are no user contributed notes for this page.


Gets namespace name

  • ≪ ReflectionFunctionAbstract::getName
  • ReflectionFunctionAbstract::getNumberOfParameters ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets namespace name

ReflectionFunctionAbstract::getNamespaceName

(PHP 5 >= 5.3.0, PHP 7)

ReflectionFunctionAbstract::getNamespaceName — Gets namespace name

설명

public string ReflectionFunctionAbstract::getNamespaceName ( void )

Get the namespace name where the class is defined.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The namespace name.

참고

  • ReflectionFunctionAbstract::getFileName() - Gets file name
  • namespaces

User Contributed Notes

There are no user contributed notes for this page.


Gets number of parameters

  • ≪ ReflectionFunctionAbstract::getNamespaceName
  • ReflectionFunctionAbstract::getNumberOfRequiredParameters ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets number of parameters

ReflectionFunctionAbstract::getNumberOfParameters

(PHP 5 >= 5.0.3, PHP 7)

ReflectionFunctionAbstract::getNumberOfParameters — Gets number of parameters

설명

public int ReflectionFunctionAbstract::getNumberOfParameters ( void )

Get the number of parameters that a function defines, both optional and required.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The number of parameters.

참고

  • ReflectionFunctionAbstract::getNumberOfRequiredParameters() - Gets number of required parameters
  • func_num_args() - Returns the number of arguments passed to the function

User Contributed Notes

Robert Pitt ( LitePHP ) 12-Apr-2010 12:42
Working on a new MVC Application Framework i use this method to check how many arguments are required before calling the sub method!

Example

<?php
        $this->method_args_count = $this->CReflection
            ->getMethod($Route->getMethod())
            ->getNumberOfParameters();
        //Maybe be 5 but if uri is /controller/method/single_param/ we only of 1
        $this->params = $Route->getParams(); //0 in some cases

        if($this->method_args_count > count($this->params))
        {
            $this->difference = ($this->method_args_count - count($this->params));
            for($i=0;$i<=$this->difference;$i++)
            {
                $this->params[] = false;
            }
        }

        //Call the method with correct amount of params
        // but as false for params that have not been passed!
        call_user_func_array(array(new $this->obj,$Route->getMethod()),$this->params);
?>


Gets number of required parameters

  • ≪ ReflectionFunctionAbstract::getNumberOfParameters
  • ReflectionFunctionAbstract::getParameters ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets number of required parameters

ReflectionFunctionAbstract::getNumberOfRequiredParameters

(PHP 5 >= 5.0.3, PHP 7)

ReflectionFunctionAbstract::getNumberOfRequiredParameters — Gets number of required parameters

설명

public int ReflectionFunctionAbstract::getNumberOfRequiredParameters ( void )

Get the number of required parameters that a function defines.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The number of required parameters.

참고

  • ReflectionFunctionAbstract::getNumberOfParameters() - Gets number of parameters

User Contributed Notes

cesar at magic3w dot com 07-Jul-2015 09:45
It's interesting to note that this function will treat optional parameters that come before a required parameter as required too. This is good since it allows you to verify that the function will be receiving enough parameters for the it to work, regardless where they are located.

<?php

class MyTest() {
    public function test($a = null, $b) {}
    public function test2($a = null, $b, $c = null) {}
}

//Create the reflection
$r  = new \ReflectionMethod('MyTest', 'test');
$r2 = new \ReflectionMethod('MyTest', 'test2');

//Verify the numbers
echo 'Test: ' . $r->getNumberOfRequiredParameters()); //Output: 2
echo 'Test2: ' . $r->getNumberOfRequiredParameters()); //Output: 2

?>
sebastian at sebastian-eiweleit dot de 03-Sep-2013 12:03
<?php
namespace ExampleWorld;
// The Class
class helloWorld {
    /* Method with two required arguments */

    public function requiredTwoArguments ( $var1, $var2 ) {
        // Some code ...
    }

    /* Method with two arguments, but just one is required */
    public function requiredOneArgument ( $var1, $var2 = false ) {
        // Some code ...
    }
}

$r = new \ReflectionMethod ( 'ExampleWorld\helloWorld', 'requiredTwoArguments' );
echo $r->getNumberOfRequiredParameters ();

$r = new \ReflectionMethod ( 'ExampleWorld\helloWorld', 'requiredOneArgument' );
echo $r->getNumberOfRequiredParameters ();

// Output: 2 1


Gets parameters

  • ≪ ReflectionFunctionAbstract::getNumberOfRequiredParameters
  • ReflectionFunctionAbstract::getReturnType ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets parameters

ReflectionFunctionAbstract::getParameters

(PHP 5, PHP 7)

ReflectionFunctionAbstract::getParameters — Gets parameters

설명

public array ReflectionFunctionAbstract::getParameters ( void )

Get the parameters as an array of ReflectionParameter.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The parameters, as a ReflectionParameter object.

참고

  • ReflectionFunctionAbstract::getNumberOfParameters() - Gets number of parameters
  • func_get_args() - Returns an array comprising a function's argument list

User Contributed Notes

dabidi at slupca dot pl 27-Oct-2015 10:33
This is part of my private framework that uses reflection.
This function get arguments list from theme method and puts corresponding vars from $_REQUEST ($_GET, $_POST, and $_COOKIE)

<?php
public static function fire_theme_method($class, $method)
{
        $fire_args=array();

        $reflection = new ReflectionMethod($class, $method);

    foreach($reflection->getParameters() AS $arg)
    {
        if($_REQUEST[$arg->name])
        $fire_args[$arg->name]=$_REQUEST[$arg->name];
        else
        $fire_args[$arg->name]=null;
    }

    return call_user_func_array(array($class, $method), $fire_args);
}
?>
For example, if my theme method needs only id, and we get this url:
http://example.com/my_class/my_method/?id=12&some_unwanted_var=123
will be ignored some_unwanted_var

Of course behind this i have .htaccess, autoloader and controller


Gets the specified return type of a function

  • ≪ ReflectionFunctionAbstract::getParameters
  • ReflectionFunctionAbstract::getShortName ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets the specified return type of a function

ReflectionFunctionAbstract::getReturnType

(PHP 7)

ReflectionFunctionAbstract::getReturnType — Gets the specified return type of a function

설명

public ReflectionType ReflectionFunctionAbstract::getReturnType ( void )

Gets the specified return type of a reflected function.

인수

이 함수는 인수가 없습니다.

반환값

Returns a ReflectionType object if a return type is specified, NULL otherwise.

예제

Example #1 ReflectionFunctionAbstract::getReturnType() example

<?php

function to_int($param) : int {
    return (int) $param;
}

$reflection1 = new ReflectionFunction('to_int');
echo $reflection1->getReturnType();

위 예제의 출력:

int

Example #2 Usage on built-in functions

<?php

$reflection2 = new ReflectionFunction('array_merge');

var_dump($reflection2->getReturnType());

위 예제의 출력:

null

This is because many internal functions do not have types specified for their parameters or return values. It is therefore best to avoid using this method on built-in functions.

참고

  • ReflectionFunctionAbstract::hasReturnType() - Checks if the function has a specified return type
  • ReflectionType::__toString() - To string

User Contributed Notes

There are no user contributed notes for this page.


Gets function short name

  • ≪ ReflectionFunctionAbstract::getReturnType
  • ReflectionFunctionAbstract::getStartLine ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets function short name

ReflectionFunctionAbstract::getShortName

(PHP 5 >= 5.3.0, PHP 7)

ReflectionFunctionAbstract::getShortName — Gets function short name

설명

public string ReflectionFunctionAbstract::getShortName ( void )

Get the short name of the function (without the namespace part).

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The short name of the function.

참고

  • ReflectionFunctionAbstract::getNamespaceName() - Gets namespace name
  • namespaces

User Contributed Notes

There are no user contributed notes for this page.


Gets starting line number

  • ≪ ReflectionFunctionAbstract::getShortName
  • ReflectionFunctionAbstract::getStaticVariables ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets starting line number

ReflectionFunctionAbstract::getStartLine

(PHP 5, PHP 7)

ReflectionFunctionAbstract::getStartLine — Gets starting line number

설명

public int ReflectionFunctionAbstract::getStartLine ( void )

Gets the starting line number of the function.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The starting line number.

참고

  • ReflectionFunctionAbstract::getEndLine() - Gets end line number

User Contributed Notes

There are no user contributed notes for this page.


Gets static variables

  • ≪ ReflectionFunctionAbstract::getStartLine
  • ReflectionFunctionAbstract::hasReturnType ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Gets static variables

ReflectionFunctionAbstract::getStaticVariables

(PHP 5, PHP 7)

ReflectionFunctionAbstract::getStaticVariables — Gets static variables

설명

public array ReflectionFunctionAbstract::getStaticVariables ( void )

Get the static variables.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

An array of static variables.

참고

  • ReflectionFunctionAbstract::getParameters() - Gets parameters

User Contributed Notes

Martiros Aghajanyan 11-Oct-2014 10:27
<?php

function test()
{
    static $a = 0, $b = 15;
    $a++;
    $b++;
    return $a;
}

$rf = new ReflectionFunction('test');

// result - Array ( [a] => 0 [b] => 15 )
print_r( $rf->getStaticVariables() );

//call test function and print again static variables
test();

// result - Array ( [a] => 1 [b] => 16 )
print_r( $rf->getStaticVariables() );

?>


Checks if the function has a specified return type

  • ≪ ReflectionFunctionAbstract::getStaticVariables
  • ReflectionFunctionAbstract::inNamespace ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Checks if the function has a specified return type

ReflectionFunctionAbstract::hasReturnType

(PHP 7)

ReflectionFunctionAbstract::hasReturnType — Checks if the function has a specified return type

설명

public bool ReflectionFunctionAbstract::hasReturnType ( void )

Checks whether the reflected function has a return type specified.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the function is a specified return type, otherwise FALSE.

예제

Example #1 ReflectionFunctionAbstract::hasReturnType() example

<?php

function to_int($param) : int {
    return (int) $param;
}

$reflection1 = new ReflectionFunction('to_int');
var_dump($reflection1->hasReturnType());

위 예제의 출력:

bool(true)

Example #2 Usage on built-in functions

<?php

$reflection2 = new ReflectionFunction('array_merge');

var_dump($reflection2->hasReturnType());

위 예제의 출력:

bool(false)

This is because many internal functions do not have types specified for their parameters or return values. It is therefore best to avoid using this method on built-in functions.

참고

  • ReflectionFunctionAbstract::getReturnType() - Gets the specified return type of a function

User Contributed Notes

There are no user contributed notes for this page.


Checks if function in namespace

  • ≪ ReflectionFunctionAbstract::hasReturnType
  • ReflectionFunctionAbstract::isClosure ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Checks if function in namespace

ReflectionFunctionAbstract::inNamespace

(PHP 5 >= 5.3.0, PHP 7)

ReflectionFunctionAbstract::inNamespace — Checks if function in namespace

설명

public bool ReflectionFunctionAbstract::inNamespace ( void )

Checks whether a function is defined in a namespace.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if it's in a namespace, otherwise FALSE

참고

  • ReflectionFunctionAbstract::getNamespaceName() - Gets namespace name
  • namespaces

User Contributed Notes

There are no user contributed notes for this page.


Checks if closure

  • ≪ ReflectionFunctionAbstract::inNamespace
  • ReflectionFunctionAbstract::isDeprecated ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Checks if closure

ReflectionFunctionAbstract::isClosure

(PHP 5 >= 5.3.0, PHP 7)

ReflectionFunctionAbstract::isClosure — Checks if closure

설명

public bool ReflectionFunctionAbstract::isClosure ( void )

Checks whether the reflected function is a Closure.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the function is a Closure, otherwise FALSE.

예제

Example #1 ReflectionFunctionAbstract::isClosure() example

<?php
// Non-closure
$function1 = 'str_replace';
$reflection1 = new ReflectionFunction($function1);
var_dump($reflection1->isClosure());

// Closure
$function2 = function () {};
$reflection2 = new ReflectionFunction($function2);
var_dump($reflection2->isClosure());
?>

위 예제의 출력:

bool(false)
bool(true)

참고

  • ReflectionFunctionAbstract::isGenerator() - Returns whether this function is a generator

User Contributed Notes

There are no user contributed notes for this page.


Checks if deprecated

  • ≪ ReflectionFunctionAbstract::isClosure
  • ReflectionFunctionAbstract::isGenerator ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Checks if deprecated

ReflectionFunctionAbstract::isDeprecated

(PHP 5, PHP 7)

ReflectionFunctionAbstract::isDeprecated — Checks if deprecated

설명

public bool ReflectionFunctionAbstract::isDeprecated ( void )

Checks whether the function is deprecated.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if it's deprecated, otherwise FALSE

예제

Example #1 ReflectionFunctionAbstract::isDeprecated() example

<?php
$rf = new ReflectionFunction('ereg');
var_dump($rf->isDeprecated());
?>

위 예제의 출력:

bool(true)

참고

  • ReflectionFunctionAbstract::getDocComment() - Gets doc comment

User Contributed Notes

There are no user contributed notes for this page.


Returns whether this function is a generator

  • ≪ ReflectionFunctionAbstract::isDeprecated
  • ReflectionFunctionAbstract::isInternal ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Returns whether this function is a generator

ReflectionFunctionAbstract::isGenerator

(PHP 5 >= 5.5.0, PHP 7)

ReflectionFunctionAbstract::isGenerator — Returns whether this function is a generator

설명

public bool ReflectionFunctionAbstract::isGenerator ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the function is generator, FALSE if it is not or NULL on failure.

User Contributed Notes

There are no user contributed notes for this page.


Checks if is internal

  • ≪ ReflectionFunctionAbstract::isGenerator
  • ReflectionFunctionAbstract::isUserDefined ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Checks if is internal

ReflectionFunctionAbstract::isInternal

(PHP 5, PHP 7)

ReflectionFunctionAbstract::isInternal — Checks if is internal

설명

public bool ReflectionFunctionAbstract::isInternal ( void )

Checks whether the function is internal, as opposed to user-defined.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if it's internal, otherwise FALSE

참고

  • ReflectionFunctionAbstract::isUserDefined() - Checks if user defined

User Contributed Notes

There are no user contributed notes for this page.


Checks if user defined

  • ≪ ReflectionFunctionAbstract::isInternal
  • ReflectionFunctionAbstract::isVariadic ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Checks if user defined

ReflectionFunctionAbstract::isUserDefined

(PHP 5, PHP 7)

ReflectionFunctionAbstract::isUserDefined — Checks if user defined

설명

public bool ReflectionFunctionAbstract::isUserDefined ( void )

Checks whether the function is user-defined, as opposed to internal.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if it's user-defined, otherwise false;

참고

  • ReflectionFunctionAbstract::isInternal() - Checks if is internal

User Contributed Notes

There are no user contributed notes for this page.


Checks if the function is variadic

  • ≪ ReflectionFunctionAbstract::isUserDefined
  • ReflectionFunctionAbstract::returnsReference ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Checks if the function is variadic

ReflectionFunctionAbstract::isVariadic

(PHP 5 >= 5.6.0, PHP 7)

ReflectionFunctionAbstract::isVariadic — Checks if the function is variadic

설명

public bool ReflectionFunctionAbstract::isVariadic ( void )

Checks if the function is variadic.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the function is variadic, otherwise FALSE.

User Contributed Notes

There are no user contributed notes for this page.


Checks if returns reference

  • ≪ ReflectionFunctionAbstract::isVariadic
  • ReflectionFunctionAbstract::__toString ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • Checks if returns reference

ReflectionFunctionAbstract::returnsReference

(PHP 5, PHP 7)

ReflectionFunctionAbstract::returnsReference — Checks if returns reference

설명

public bool ReflectionFunctionAbstract::returnsReference ( void )

Checks whether the function returns a reference.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if it returns a reference, otherwise FALSE

참고

  • ReflectionFunctionAbstract::isClosure() - Checks if closure

User Contributed Notes

There are no user contributed notes for this page.


To string

  • ≪ ReflectionFunctionAbstract::returnsReference
  • ReflectionMethod ≫
  • PHP Manual
  • ReflectionFunctionAbstract
  • To string

ReflectionFunctionAbstract::__toString

(PHP 5, PHP 7)

ReflectionFunctionAbstract::__toString — To string

설명

abstract public void ReflectionFunctionAbstract::__toString ( void )

To string.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The string.

참고

  • ReflectionClass::clone()
  • __toString()

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionMethod class

(PHP 5, PHP 7)

소개

The ReflectionMethod class reports information about a method.

클래스 개요

ReflectionMethod extends ReflectionFunctionAbstract implements Reflector {
/* 상수 */
const integer IS_STATIC = 1 ;
const integer IS_PUBLIC = 256 ;
const integer IS_PROTECTED = 512 ;
const integer IS_PRIVATE = 1024 ;
const integer IS_ABSTRACT = 2 ;
const integer IS_FINAL = 4 ;
/* 프로퍼티 */
public $name ;
public $class ;
/* 메소드 */
public __construct ( mixed $class , string $name )
public static string export ( string $class , string $name [, bool $return = false ] )
public Closure getClosure ( object $object )
public ReflectionClass getDeclaringClass ( void )
public int getModifiers ( void )
public ReflectionMethod getPrototype ( void )
public mixed invoke ( object $object [, mixed $parameter [, mixed $... ]] )
public mixed invokeArgs ( object $object , array $args )
public bool isAbstract ( void )
public bool isConstructor ( void )
public bool isDestructor ( void )
public bool isFinal ( void )
public bool isPrivate ( void )
public bool isProtected ( void )
public bool isPublic ( void )
public bool isStatic ( void )
public void setAccessible ( bool $accessible )
public string __toString ( void )
/* 상속된 메소드 */
final private void ReflectionFunctionAbstract::__clone ( void )
public ReflectionClass ReflectionFunctionAbstract::getClosureScopeClass ( void )
public object ReflectionFunctionAbstract::getClosureThis ( void )
public string ReflectionFunctionAbstract::getDocComment ( void )
public int ReflectionFunctionAbstract::getEndLine ( void )
public ReflectionExtension ReflectionFunctionAbstract::getExtension ( void )
public string ReflectionFunctionAbstract::getExtensionName ( void )
public string ReflectionFunctionAbstract::getFileName ( void )
public string ReflectionFunctionAbstract::getName ( void )
public string ReflectionFunctionAbstract::getNamespaceName ( void )
public int ReflectionFunctionAbstract::getNumberOfParameters ( void )
public int ReflectionFunctionAbstract::getNumberOfRequiredParameters ( void )
public array ReflectionFunctionAbstract::getParameters ( void )
public ReflectionType ReflectionFunctionAbstract::getReturnType ( void )
public string ReflectionFunctionAbstract::getShortName ( void )
public int ReflectionFunctionAbstract::getStartLine ( void )
public array ReflectionFunctionAbstract::getStaticVariables ( void )
public bool ReflectionFunctionAbstract::hasReturnType ( void )
public bool ReflectionFunctionAbstract::inNamespace ( void )
public bool ReflectionFunctionAbstract::isClosure ( void )
public bool ReflectionFunctionAbstract::isDeprecated ( void )
public bool ReflectionFunctionAbstract::isGenerator ( void )
public bool ReflectionFunctionAbstract::isInternal ( void )
public bool ReflectionFunctionAbstract::isUserDefined ( void )
public bool ReflectionFunctionAbstract::isVariadic ( void )
public bool ReflectionFunctionAbstract::returnsReference ( void )
abstract public void ReflectionFunctionAbstract::__toString ( void )
}

프로퍼티

name

Method name

class

Class name

예약 상수

ReflectionMethod Modifiers

ReflectionMethod::IS_STATIC

Indicates that the method is static.

ReflectionMethod::IS_PUBLIC

Indicates that the method is public.

ReflectionMethod::IS_PROTECTED

Indicates that the method is protected.

ReflectionMethod::IS_PRIVATE

Indicates that the method is private.

ReflectionMethod::IS_ABSTRACT

Indicates that the method is abstract.

ReflectionMethod::IS_FINAL

Indicates that the method is final.

Table of Contents

  • ReflectionMethod::__construct — Constructs a ReflectionMethod
  • ReflectionMethod::export — Export a reflection method.
  • ReflectionMethod::getClosure — Returns a dynamically created closure for the method
  • ReflectionMethod::getDeclaringClass — Gets declaring class for the reflected method.
  • ReflectionMethod::getModifiers — Gets the method modifiers
  • ReflectionMethod::getPrototype — Gets the method prototype (if there is one).
  • ReflectionMethod::invoke — Invoke
  • ReflectionMethod::invokeArgs — Invoke args
  • ReflectionMethod::isAbstract — Checks if method is abstract
  • ReflectionMethod::isConstructor — Checks if method is a constructor
  • ReflectionMethod::isDestructor — Checks if method is a destructor
  • ReflectionMethod::isFinal — Checks if method is final
  • ReflectionMethod::isPrivate — Checks if method is private
  • ReflectionMethod::isProtected — Checks if method is protected
  • ReflectionMethod::isPublic — Checks if method is public
  • ReflectionMethod::isStatic — Checks if method is static
  • ReflectionMethod::setAccessible — Set method accessibility
  • ReflectionMethod::__toString — Returns the string representation of the Reflection method object.

User Contributed Notes

webseiten dot designer at googlemail dot com 02-Apr-2011 08:53
Note that the public member $class contains the name of the class in which the method has been defined:

<?php
class A {public function __construct() {}}
class B extends A {}

$method = new ReflectionMethod('B', '__construct');
echo $method->class; // prints 'A'
?>
no dot prob at gmx dot net 02-Jun-2006 10:09
I have written a function which returns the value of a given DocComment tag.

Full example:

<?php

header('Content-Type: text/plain');

class Example
{
    /**
     * This is my DocComment!
     *
     * @DocTag: prints Hello World!
     */
    public function myMethod()
    {
        echo 'Hello World!';
    }
}

function getDocComment($str, $tag = '')
{
    if (empty($tag))
    {
        return $str;
    }

    $matches = array();
    preg_match("/".$tag.":(.*)(\\r\\n|\\r|\\n)/U", $str, $matches);

    if (isset($matches[1]))
    {
        return trim($matches[1]);
    }

    return '';
}

$method = new ReflectionMethod('Example', 'myMethod');

// will return Hello World!
echo getDocComment($method->getDocComment(), '@DocTag');

?>

Maybe you can add this functionality to the getDocComment methods of the reflection classes.


Constructs a ReflectionMethod

  • ≪ ReflectionMethod
  • ReflectionMethod::export ≫
  • PHP Manual
  • ReflectionMethod
  • Constructs a ReflectionMethod

ReflectionMethod::__construct

(PHP 5, PHP 7)

ReflectionMethod::__construct — Constructs a ReflectionMethod

설명

public ReflectionMethod::__construct ( mixed $class , string $name )
public ReflectionMethod::__construct ( string $class_method )

Constructs a new ReflectionMethod.

인수

class

Classname or object (instance of the class) that contains the method.

name

Name of the method.

class_method

Class name and method name delimited by ::.

반환값

값을 반환하지 않습니다.

오류/예외

A ReflectionException is thrown if the given method does not exist.

예제

Example #1 ReflectionMethod::__construct() example

<?php
class Counter
{
    private static $c = 0;

    /**
     * Increment counter
     *
     * @final
     * @static
     * @access  public
     * @return  int
     */
    final public static function increment()
    {
        return ++self::$c;
    }
}

// Create an instance of the ReflectionMethod class
$method = new ReflectionMethod('Counter', 'increment');

// Print out basic information
printf(
    "===> The %s%s%s%s%s%s%s method '%s' (which is %s)\n" .
    "     declared in %s\n" .
    "     lines %d to %d\n" .
    "     having the modifiers %d[%s]\n",
        $method->isInternal() ? 'internal' : 'user-defined',
        $method->isAbstract() ? ' abstract' : '',
        $method->isFinal() ? ' final' : '',
        $method->isPublic() ? ' public' : '',
        $method->isPrivate() ? ' private' : '',
        $method->isProtected() ? ' protected' : '',
        $method->isStatic() ? ' static' : '',
        $method->getName(),
        $method->isConstructor() ? 'the constructor' : 'a regular method',
        $method->getFileName(),
        $method->getStartLine(),
        $method->getEndline(),
        $method->getModifiers(),
        implode(' ', Reflection::getModifierNames($method->getModifiers()))
);

// Print documentation comment
printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1));

// Print static variables if existant
if ($statics= $method->getStaticVariables()) {
    printf("---> Static variables: %s\n", var_export($statics, 1));
}

// Invoke the method
printf("---> Invocation results in: ");
var_dump($method->invoke(NULL));
?>

위 예제의 출력 예시:

===> The user-defined final public static method 'increment' (which is a regular method)
     declared in /Users/philip/cvs/phpdoc/test.php
     lines 14 to 17
     having the modifiers 261[final public static]
---> Documentation:
 '/**
     * Increment counter
     *
     * @final
     * @static
     * @access  public
     * @return  int
     */'
---> Invocation results in: int(1)

참고

  • ReflectionMethod::export() - Export a reflection method.
  • Constructors

User Contributed Notes

There are no user contributed notes for this page.


Export a reflection method.

  • ≪ ReflectionMethod::__construct
  • ReflectionMethod::getClosure ≫
  • PHP Manual
  • ReflectionMethod
  • Export a reflection method.

ReflectionMethod::export

(PHP 5, PHP 7)

ReflectionMethod::export — Export a reflection method.

설명

public static string ReflectionMethod::export ( string $class , string $name [, bool $return = false ] )

Exports a ReflectionMethod.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

class

The class name.

name

The name of the method.

return

TRUE로 설정하면 export를 반환하고, 반대의 경우는 버립니다. FALSE로 설정(기본값)은 반대를 수행합니다.

반환값

return 인수를 TRUE로 설정하면, export를 string으로 반환합니다. 그렇지 않으면 NULL을 반환합니다.

참고

  • ReflectionMethod::__construct() - Constructs a ReflectionMethod
  • ReflectionMethod::__toString() - Returns the string representation of the Reflection method object.

User Contributed Notes

There are no user contributed notes for this page.


Returns a dynamically created closure for the method

  • ≪ ReflectionMethod::export
  • ReflectionMethod::getDeclaringClass ≫
  • PHP Manual
  • ReflectionMethod
  • Returns a dynamically created closure for the method

ReflectionMethod::getClosure

(PHP >= 5.4.0)

ReflectionMethod::getClosure — Returns a dynamically created closure for the method

설명

public Closure ReflectionMethod::getClosure ( object $object )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

object

Forbidden for static methods, required for other methods.

반환값

Returns Closure. Returns NULL in case of an error.

User Contributed Notes

okto 14-Aug-2016 07:45
Use method from another class context.

<?php

class A {
    private $var = 'class A';

    public function getVar() {
        return $this->var;
    }

    public function getCl() {
        return function () {
            $this->getVar();
        };
    }
}

class B {
    private $var = 'class B';
}

$a = new A();
$b = new B();

print $a->getVar() . PHP_EOL;

$reflection = new ReflectionClass(get_class($a));
$closure    = $reflection->getMethod('getVar')->getClosure($a);
$get_var_b  = $closure->bindTo($b, $b);

print $get_var_b() . PHP_EOL;

// Output:
// class A
// class B
Denis Doronin 19-Jan-2013 05:05
You can call private methods with getClosure():

<?php

function call_private_method($object, $method, $args = array()) {
    $reflection = new ReflectionClass(get_class($object));
    $closure = $reflection->getMethod($method)->getClosure($object);
    return call_user_func_array($closure, $args);
}

class Example {

    private $x = 1, $y = 10;

    private function sum() {
        print $this->x + $this->y;
    }

}

call_private_method(new Example(), 'sum');

?>

Output is 11.


Gets declaring class for the reflected method.

  • ≪ ReflectionMethod::getClosure
  • ReflectionMethod::getModifiers ≫
  • PHP Manual
  • ReflectionMethod
  • Gets declaring class for the reflected method.

ReflectionMethod::getDeclaringClass

(PHP 5, PHP 7)

ReflectionMethod::getDeclaringClass — Gets declaring class for the reflected method.

설명

public ReflectionClass ReflectionMethod::getDeclaringClass ( void )

Gets the declaring class for the reflected method.

인수

이 함수는 인수가 없습니다.

반환값

A ReflectionClass object of the class that the reflected method is part of.

예제

Example #1 ReflectionMethod::getDeclaringClass() example

<?php
class HelloWorld {

    protected function sayHelloTo($name) {
        return 'Hello ' . $name;
    }

}

$reflectionMethod = new ReflectionMethod(new HelloWorld(), 'sayHelloTo');
var_dump($reflectionMethod->getDeclaringClass());
?>

위 예제의 출력:

object(ReflectionClass)#2 (1) {
  ["name"]=>
  string(10) "HelloWorld"
}

참고

  • ReflectionMethod::isAbstract() - Checks if method is abstract

User Contributed Notes

There are no user contributed notes for this page.


Gets the method modifiers

  • ≪ ReflectionMethod::getDeclaringClass
  • ReflectionMethod::getPrototype ≫
  • PHP Manual
  • ReflectionMethod
  • Gets the method modifiers

ReflectionMethod::getModifiers

(PHP 5, PHP 7)

ReflectionMethod::getModifiers — Gets the method modifiers

설명

public int ReflectionMethod::getModifiers ( void )

Returns a bitfield of the access modifiers for this method.

인수

이 함수는 인수가 없습니다.

반환값

A numeric representation of the modifiers. The modifiers are listed below. The actual meanings of these modifiers are described in the predefined constants.

예제

Example #1 ReflectionMethod::getModifiers() example

<?php
class Testing
{
    final public static function foo()
    {
        return;
    }
    public function bar()
    {
        return;
    }
}

$foo = new ReflectionMethod('Testing', 'foo');

echo "Modifiers for method foo():\n";
echo $foo->getModifiers() . "\n";
echo implode(' ', Reflection::getModifierNames($foo->getModifiers())) . "\n";

$bar = new ReflectionMethod('Testing', 'bar');

echo "Modifiers for method bar():\n";
echo $bar->getModifiers() . "\n";
echo implode(' ', Reflection::getModifierNames($bar->getModifiers()));
?>

위 예제의 출력 예시:

Modifiers for method foo():
261
final public static
Modifiers for method bar():
65792
public

참고

  • Reflection::getModifierNames() - Gets modifier names

User Contributed Notes

There are no user contributed notes for this page.


Gets the method prototype (if there is one).

  • ≪ ReflectionMethod::getModifiers
  • ReflectionMethod::invoke ≫
  • PHP Manual
  • ReflectionMethod
  • Gets the method prototype (if there is one).

ReflectionMethod::getPrototype

(PHP 5, PHP 7)

ReflectionMethod::getPrototype — Gets the method prototype (if there is one).

설명

public ReflectionMethod ReflectionMethod::getPrototype ( void )

Returns the methods prototype.

인수

이 함수는 인수가 없습니다.

반환값

A ReflectionMethod instance of the method prototype.

오류/예외

A ReflectionException exception is thrown if the method does not have a prototype.

예제

Example #1 ReflectionMethod::getPrototype() example

<?php
class Hello {

    public function sayHelloTo($name) {
        return 'Hello ' . $name;
    }

}
class HelloWorld extends Hello {

    public function sayHelloTo($name) {
        return 'Hello world: ' . $name;
    }

}

$reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');
var_dump($reflectionMethod->getPrototype());
?>

위 예제의 출력:

object(ReflectionMethod)#2 (2) {
  ["name"]=>
  string(10) "sayHelloTo"
  ["class"]=>
  string(5) "Hello"
}

참고

  • ReflectionMethod::getModifiers() - Gets the method modifiers

User Contributed Notes

There are no user contributed notes for this page.


Invoke

  • ≪ ReflectionMethod::getPrototype
  • ReflectionMethod::invokeArgs ≫
  • PHP Manual
  • ReflectionMethod
  • Invoke

ReflectionMethod::invoke

(PHP 5, PHP 7)

ReflectionMethod::invoke — Invoke

설명

public mixed ReflectionMethod::invoke ( object $object [, mixed $parameter [, mixed $... ]] )

Invokes a reflected method.

인수

object

The object to invoke the method on. For static methods, pass null to this parameter.

parameter

Zero or more parameters to be passed to the method. It accepts a variable number of parameters which are passed to the method.

반환값

Returns the method result.

오류/예외

A ReflectionException if the object parameter does not contain an instance of the class that this method was declared in.

A ReflectionException if the method invocation failed.

예제

Example #1 ReflectionMethod::invoke() example

<?php
class HelloWorld {

    public function sayHelloTo($name) {
        return 'Hello ' . $name;
    }

}

$reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');
echo $reflectionMethod->invoke(new HelloWorld(), 'Mike');
?>

위 예제의 출력:

Hello Mike

주의

Note:

함수가 인수로 참조를 필요로 하면, 주어진 인수 목록에도 참조여야 합니다.

참고

  • ReflectionMethod::invokeArgs() - Invoke args
  • __invoke()
  • call_user_func() - Call the callback given by the first parameter

User Contributed Notes

dimitriy at remerov dot ru 06-Jun-2012 10:19
This method can be used to call a overwritten public method of a parent class on an child instance
The following code will output "A":

<?php

class A
{
    public function foo()
    {
        return __CLASS__;
    }
}

class B extends A
{
    public function foo()
    {
        return __CLASS__;
    }
}

$b = new B();

$reflection = new ReflectionObject($b);

$parentReflection = $reflection->getParentClass();

$parentFooReflection = $parentReflection->getMethod('foo');

$data = $parentFooReflection->invoke($b);

echo $data;

?>
templargrey at wp dot pl 21-Jul-2011 10:12
Seems that Reflection doesn`t resolve late static bindings - var_dump will show "string 'a' (length=1)".

<?php
class ParentClass { protected static $a = 'a'; static public function init() { return static::$a; } }
class ChildClass extends ParentClass { protected static $a = 'b'; }

$r = new ReflectionClass('ChildClass');
var_dump($r->getMethod('init')->invoke(null));
?>
rojaro at gmail dot com 10-Feb-2011 10:56
Note: If you want to invoke protected or private methods, you'll first have to make them accessible using the setAccessible() method (see http://php.net/reflectionmethod.setaccessible ).


Invoke args

  • ≪ ReflectionMethod::invoke
  • ReflectionMethod::isAbstract ≫
  • PHP Manual
  • ReflectionMethod
  • Invoke args

ReflectionMethod::invokeArgs

(PHP 5 >= 5.1.0, PHP 7)

ReflectionMethod::invokeArgs — Invoke args

설명

public mixed ReflectionMethod::invokeArgs ( object $object , array $args )

Invokes the reflected method and pass its arguments as array.

인수

object

The object to invoke the method on. In case of static methods, you can pass null to this parameter.

args

The parameters to be passed to the function, as an array.

반환값

Returns the method result.

오류/예외

A ReflectionException if the object parameter does not contain an instance of the class that this method was declared in.

A ReflectionException if the method invocation failed.

예제

Example #1 ReflectionMethod::invokeArgs() example

<?php
class HelloWorld {

    public function sayHelloTo($name) {
        return 'Hello ' . $name;
    }

}

$reflectionMethod = new ReflectionMethod('HelloWorld', 'sayHelloTo');
echo $reflectionMethod->invokeArgs(new HelloWorld(), array('Mike'));
?>

위 예제의 출력:

Hello Mike

주의

Note:

함수가 인수로 참조를 필요로 하면, 주어진 인수 목록에도 참조여야 합니다.

참고

  • ReflectionMethod::invoke() - Invoke
  • __invoke()
  • call_user_func_array() - Call a callback with an array of parameters

User Contributed Notes

CodeCoutureXX at gmail dot com 04-Sep-2015 06:12
If you need to call ReflectionMethod::invokeArgs() on a static function you can pass NULL in for the $object parameter.

Example:

<?php
     class myClass {
          public static myStaticFunc($a, $b) {
               return $a + $b;
          }
     }

     $ref = new ReflectionMethod('myClass', 'myStaticFunc');
     echo $ref->invokeArgs(NULL, [12, 7]);
?>

produces the following output:

19
cweiske at cweiske dot de 25-Mar-2011 04:34
Passing arguments by reference works:
<?php $rm->invokeArgs($object, array(&$foo, $bar)); ?>
agent_harris at secure-mail dot biz 13-Feb-2011 04:12
There is a simple workaround for the reference passing problem:
Since the reflection api has to handle all parameters in a generic way it has no chance to guess if you wish to pass data per value or reference.

But it seems that you can also decide to pass a reference from where you call the function or method (not just only by the ampersand prefix in its declaration).

So just do the following; which worked for me:

<?php
//...
$method->invoke($object, $inputValue, &$outputValue);
?>

Since this will only be necessary with arrays and primitive data types it should be acceptable in most cases to know in advance if you need to pass per reference. But it is probably although necessary to keep the ampersand always in the declaration (because of the at least two layers between the actual function and your invoke call).

If this is the expected behavior it will maybe make sense to mention it in the documentation for invoke and invokeArgs.
serg dot smertin at gmail dot com 21-Sep-2010 03:51
We can do black magic, which is useful in templating block calls:

<?php
     $object->__named('methodNameHere', array('arg3' => 'three', 'arg1' => 'one'));

     ...

      /**
       * Pass method arguments by name
       *
       * @param string $method
       * @param array $args
       * @return mixed
       */
      public function __named($method, array $args = array())
      {
        $reflection = new ReflectionMethod($this, $method);

        $pass = array();
        foreach($reflection->getParameters() as $param)
        {
          /* @var $param ReflectionParameter */
          if(isset($args[$param->getName()]))
          {
            $pass[] = $args[$param->getName()];
          }
          else
          {
            $pass[] = $param->getDefaultValue();
          }
        }

        return $reflection->invokeArgs($this, $pass);
      }
?>


Checks if method is abstract

  • ≪ ReflectionMethod::invokeArgs
  • ReflectionMethod::isConstructor ≫
  • PHP Manual
  • ReflectionMethod
  • Checks if method is abstract

ReflectionMethod::isAbstract

(PHP 5, PHP 7)

ReflectionMethod::isAbstract — Checks if method is abstract

설명

public bool ReflectionMethod::isAbstract ( void )

Checks if the method is abstract.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the method is abstract, otherwise FALSE

참고

  • ReflectionMethod::getDeclaringClass() - Gets declaring class for the reflected method.

User Contributed Notes

There are no user contributed notes for this page.


Checks if method is a constructor

  • ≪ ReflectionMethod::isAbstract
  • ReflectionMethod::isDestructor ≫
  • PHP Manual
  • ReflectionMethod
  • Checks if method is a constructor

ReflectionMethod::isConstructor

(PHP 5, PHP 7)

ReflectionMethod::isConstructor — Checks if method is a constructor

설명

public bool ReflectionMethod::isConstructor ( void )

Checks if the method is a constructor.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the method is a constructor, otherwise FALSE

참고

  • ReflectionMethod::__construct() - Constructs a ReflectionMethod
  • ReflectionMethod::isAbstract() - Checks if method is abstract
  • ReflectionMethod::isDestructor() - Checks if method is a destructor

User Contributed Notes

There are no user contributed notes for this page.


Checks if method is a destructor

  • ≪ ReflectionMethod::isConstructor
  • ReflectionMethod::isFinal ≫
  • PHP Manual
  • ReflectionMethod
  • Checks if method is a destructor

ReflectionMethod::isDestructor

(PHP 5, PHP 7)

ReflectionMethod::isDestructor — Checks if method is a destructor

설명

public bool ReflectionMethod::isDestructor ( void )

Checks if the method is a destructor.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the method is a destructor, otherwise FALSE

참고

  • ReflectionMethod::isConstructor() - Checks if method is a constructor

User Contributed Notes

There are no user contributed notes for this page.


Checks if method is final

  • ≪ ReflectionMethod::isDestructor
  • ReflectionMethod::isPrivate ≫
  • PHP Manual
  • ReflectionMethod
  • Checks if method is final

ReflectionMethod::isFinal

(PHP 5, PHP 7)

ReflectionMethod::isFinal — Checks if method is final

설명

public bool ReflectionMethod::isFinal ( void )

Checks if the method is final.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the method is final, otherwise FALSE

참고

  • ReflectionMethod::isStatic() - Checks if method is static

User Contributed Notes

There are no user contributed notes for this page.


Checks if method is private

  • ≪ ReflectionMethod::isFinal
  • ReflectionMethod::isProtected ≫
  • PHP Manual
  • ReflectionMethod
  • Checks if method is private

ReflectionMethod::isPrivate

(PHP 5, PHP 7)

ReflectionMethod::isPrivate — Checks if method is private

설명

public bool ReflectionMethod::isPrivate ( void )

Checks if the method is private.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the method is private, otherwise FALSE

참고

  • ReflectionMethod::isPublic() - Checks if method is public

User Contributed Notes

There are no user contributed notes for this page.


Checks if method is protected

  • ≪ ReflectionMethod::isPrivate
  • ReflectionMethod::isPublic ≫
  • PHP Manual
  • ReflectionMethod
  • Checks if method is protected

ReflectionMethod::isProtected

(PHP 5, PHP 7)

ReflectionMethod::isProtected — Checks if method is protected

설명

public bool ReflectionMethod::isProtected ( void )

Checks if the method is protected.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the method is protected, otherwise FALSE

참고

  • ReflectionMethod::isPrivate() - Checks if method is private

User Contributed Notes

There are no user contributed notes for this page.


Checks if method is public

  • ≪ ReflectionMethod::isProtected
  • ReflectionMethod::isStatic ≫
  • PHP Manual
  • ReflectionMethod
  • Checks if method is public

ReflectionMethod::isPublic

(PHP 5, PHP 7)

ReflectionMethod::isPublic — Checks if method is public

설명

public bool ReflectionMethod::isPublic ( void )

Checks if the method is public.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the method is public, otherwise FALSE

참고

  • ReflectionMethod::isPrivate() - Checks if method is private

User Contributed Notes

There are no user contributed notes for this page.


Checks if method is static

  • ≪ ReflectionMethod::isPublic
  • ReflectionMethod::setAccessible ≫
  • PHP Manual
  • ReflectionMethod
  • Checks if method is static

ReflectionMethod::isStatic

(PHP 5, PHP 7)

ReflectionMethod::isStatic — Checks if method is static

설명

public bool ReflectionMethod::isStatic ( void )

Checks if the method is static.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the method is static, otherwise FALSE

참고

  • ReflectionMethod::isFinal() - Checks if method is final

User Contributed Notes

There are no user contributed notes for this page.


Set method accessibility

  • ≪ ReflectionMethod::isStatic
  • ReflectionMethod::__toString ≫
  • PHP Manual
  • ReflectionMethod
  • Set method accessibility

ReflectionMethod::setAccessible

(PHP 5 >= 5.3.2, PHP 7)

ReflectionMethod::setAccessible — Set method accessibility

설명

public void ReflectionMethod::setAccessible ( bool $accessible )

Sets a method to be accessible. For example, it may allow protected and private methods to be invoked.

인수

accessible

TRUE to allow accessibility, or FALSE.

반환값

값을 반환하지 않습니다.

참고

  • ReflectionMethod::isPrivate() - Checks if method is private
  • ReflectionMethod::isProtected() - Checks if method is protected

User Contributed Notes

dave1010 at gmail dot com 11-May-2011 12:21
This is handy for accessing private methods but remember that things are normally private for a reason! Unit Testing is one (debatable) use case for this.

Example:
<?php
class Foo {
  private function myPrivateMethod() {
    return 7;
  }
}

$method = new ReflectionMethod('Foo', 'myPrivateMethod');
$method->setAccessible(true);

echo $method->invoke(new Foo);
// echos "7"
?>

This works nicely with PHPUnit: http://php.net/manual/en/reflectionmethod.setaccessible.php


Returns the string representation of the Reflection method object.

  • ≪ ReflectionMethod::setAccessible
  • ReflectionObject ≫
  • PHP Manual
  • ReflectionMethod
  • Returns the string representation of the Reflection method object.

ReflectionMethod::__toString

(PHP 5, PHP 7)

ReflectionMethod::__toString — Returns the string representation of the Reflection method object.

설명

public string ReflectionMethod::__toString ( void )

Returns the string representation of the Reflection method object.

인수

이 함수는 인수가 없습니다.

반환값

A string representation of this ReflectionMethod instance.

예제

Example #1 ReflectionMethod::__toString() example

<?php
class HelloWorld {

    public function sayHelloTo($name) {
        return 'Hello ' . $name;
    }

}

$reflectionMethod = new ReflectionMethod(new HelloWorld(), 'sayHelloTo');
echo $reflectionMethod;
?>

위 예제의 출력:

Method [ <user> public method sayHelloTo ] {
  @@ /var/www/examples/reflection.php 16 - 18

  - Parameters [1] {
    Parameter #0 [ <required> $name ]
  }
}

참고

  • ReflectionMethod::export() - Export a reflection method.
  • __toString()

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionObject class

(PHP 5, PHP 7)

소개

The ReflectionObject class reports information about an object.

클래스 개요

ReflectionObject extends ReflectionClass implements Reflector {
/* 상수 */
const integer IS_IMPLICIT_ABSTRACT = 16 ;
const integer IS_EXPLICIT_ABSTRACT = 32 ;
const integer IS_FINAL = 64 ;
/* 프로퍼티 */
public $name ;
/* 메소드 */
public __construct ( object $argument )
public static string export ( string $argument [, bool $return ] )
/* 상속된 메소드 */
public ReflectionClass::__construct ( mixed $argument )
public static string ReflectionClass::export ( mixed $argument [, bool $return = false ] )
public mixed ReflectionClass::getConstant ( string $name )
public array ReflectionClass::getConstants ( void )
public ReflectionMethod ReflectionClass::getConstructor ( void )
public array ReflectionClass::getDefaultProperties ( void )
public string ReflectionClass::getDocComment ( void )
public int ReflectionClass::getEndLine ( void )
public ReflectionExtension ReflectionClass::getExtension ( void )
public string ReflectionClass::getExtensionName ( void )
public string ReflectionClass::getFileName ( void )
public array ReflectionClass::getInterfaceNames ( void )
public array ReflectionClass::getInterfaces ( void )
public ReflectionMethod ReflectionClass::getMethod ( string $name )
public array ReflectionClass::getMethods ([ int $filter ] )
public int ReflectionClass::getModifiers ( void )
public string ReflectionClass::getName ( void )
public string ReflectionClass::getNamespaceName ( void )
public ReflectionClass ReflectionClass::getParentClass ( void )
public array ReflectionClass::getProperties ([ int $filter ] )
public ReflectionProperty ReflectionClass::getProperty ( string $name )
public string ReflectionClass::getShortName ( void )
public int ReflectionClass::getStartLine ( void )
public array ReflectionClass::getStaticProperties ( void )
public mixed ReflectionClass::getStaticPropertyValue ( string $name [, mixed &$def_value ] )
public array ReflectionClass::getTraitAliases ( void )
public array ReflectionClass::getTraitNames ( void )
public array ReflectionClass::getTraits ( void )
public bool ReflectionClass::hasConstant ( string $name )
public bool ReflectionClass::hasMethod ( string $name )
public bool ReflectionClass::hasProperty ( string $name )
public bool ReflectionClass::implementsInterface ( string $interface )
public bool ReflectionClass::inNamespace ( void )
public bool ReflectionClass::isAbstract ( void )
public bool ReflectionClass::isAnonymous ( void )
public bool ReflectionClass::isCloneable ( void )
public bool ReflectionClass::isFinal ( void )
public bool ReflectionClass::isInstance ( object $object )
public bool ReflectionClass::isInstantiable ( void )
public bool ReflectionClass::isInterface ( void )
public bool ReflectionClass::isInternal ( void )
public bool ReflectionClass::isIterateable ( void )
public bool ReflectionClass::isSubclassOf ( string $class )
public bool ReflectionClass::isTrait ( void )
public bool ReflectionClass::isUserDefined ( void )
public object ReflectionClass::newInstance ( mixed $args [, mixed $... ] )
public object ReflectionClass::newInstanceArgs ([ array $args ] )
public object ReflectionClass::newInstanceWithoutConstructor ( void )
public void ReflectionClass::setStaticPropertyValue ( string $name , string $value )
public string ReflectionClass::__toString ( void )
}

프로퍼티

name

Name of the object's class. Read-only, throws ReflectionException in attempt to write.

Table of Contents

  • ReflectionObject::__construct — Constructs a ReflectionObject
  • ReflectionObject::export — Export

User Contributed Notes

kuldipem at gmail dot com 17-Jun-2015 03:01
class a {
    const abc = 1;
    public function __get($key){
        $r = new ReflectionObject($this);
        if($r->hasConstant($key)){ return $r->getConstant($key); }
    }
}
class b {
    public function getA(){
        return new a();
    }
}
$b = new b();
var_dump($b->getA()::abc);
var_dump($b->getA()::def);
marcel dot nolte at noltecomputer dot de 02-Sep-2009 11:42
To simply enlist all methods and properties of an object simply write:

<?php ReflectionObject::export($yourObject); ?>

,which will cause an var_export-like output.


Constructs a ReflectionObject

  • ≪ ReflectionObject
  • ReflectionObject::export ≫
  • PHP Manual
  • ReflectionObject
  • Constructs a ReflectionObject

ReflectionObject::__construct

(PHP 5, PHP 7)

ReflectionObject::__construct — Constructs a ReflectionObject

설명

public ReflectionObject::__construct ( object $argument )

Constructs a ReflectionObject.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

argument

An object instance.

반환값

값을 반환하지 않습니다.

참고

  • ReflectionObject::export() - Export
  • Constructors

User Contributed Notes

There are no user contributed notes for this page.


Export

  • ≪ ReflectionObject::__construct
  • ReflectionParameter ≫
  • PHP Manual
  • ReflectionObject
  • Export

ReflectionObject::export

(PHP 5, PHP 7)

ReflectionObject::export — Export

설명

public static string ReflectionObject::export ( string $argument [, bool $return ] )

Exports a reflection.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

argument

내보낼 reflection

return

TRUE로 설정하면 export를 반환하고, 반대의 경우는 버립니다. FALSE로 설정(기본값)은 반대를 수행합니다.

반환값

return 인수를 TRUE로 설정하면, export를 string으로 반환합니다. 그렇지 않으면 NULL을 반환합니다.

참고

  • ReflectionObject::__construct() - Constructs a ReflectionObject

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionParameter class

(PHP 5, PHP 7)

소개

The ReflectionParameter class retrieves information about function's or method's parameters.

To introspect function parameters, first create an instance of the ReflectionFunction or ReflectionMethod classes and then use their ReflectionFunctionAbstract::getParameters() method to retrieve an array of parameters.

클래스 개요

ReflectionParameter implements Reflector {
/* 프로퍼티 */
public $name ;
/* 메소드 */
public bool allowsNull ( void )
public bool canBePassedByValue ( void )
final private void __clone ( void )
public __construct ( string $function , string $parameter )
public static string export ( string $function , string $parameter [, bool $return ] )
public ReflectionClass getClass ( void )
public ReflectionClass getDeclaringClass ( void )
public ReflectionFunctionAbstract getDeclaringFunction ( void )
public mixed getDefaultValue ( void )
public string getDefaultValueConstantName ( void )
public string getName ( void )
public int getPosition ( void )
public ReflectionType getType ( void )
public bool hasType ( void )
public bool isArray ( void )
public bool isCallable ( void )
public bool isDefaultValueAvailable ( void )
public bool isDefaultValueConstant ( void )
public bool isOptional ( void )
public bool isPassedByReference ( void )
public bool isVariadic ( void )
public string __toString ( void )
}

프로퍼티

name

Name of the parameter. Read-only, throws ReflectionException in attempt to write.

Table of Contents

  • ReflectionParameter::allowsNull — Checks if null is allowed
  • ReflectionParameter::canBePassedByValue — Returns whether this parameter can be passed by value
  • ReflectionParameter::__clone — Clone
  • ReflectionParameter::__construct — Construct
  • ReflectionParameter::export — Exports
  • ReflectionParameter::getClass — Get the type hinted class
  • ReflectionParameter::getDeclaringClass — Gets declaring class
  • ReflectionParameter::getDeclaringFunction — Gets declaring function
  • ReflectionParameter::getDefaultValue — Gets default parameter value
  • ReflectionParameter::getDefaultValueConstantName — Returns the default value's constant name if default value is constant or null
  • ReflectionParameter::getName — Gets parameter name
  • ReflectionParameter::getPosition — Gets parameter position
  • ReflectionParameter::getType — Gets a parameter's type
  • ReflectionParameter::hasType — Checks if parameter has a type
  • ReflectionParameter::isArray — Checks if parameter expects an array
  • ReflectionParameter::isCallable — Returns whether parameter MUST be callable
  • ReflectionParameter::isDefaultValueAvailable — Checks if a default value is available
  • ReflectionParameter::isDefaultValueConstant — Returns whether the default value of this parameter is constant
  • ReflectionParameter::isOptional — Checks if optional
  • ReflectionParameter::isPassedByReference — Checks if passed by reference
  • ReflectionParameter::isVariadic — Checks if the parameter is variadic
  • ReflectionParameter::__toString — To string

User Contributed Notes

fgm at riff dot org 11-May-2008 09:44
The note about the signature of the ReflectionParameter constructor is actually incomplete, at least in 5.2.5: it is possible to use an integer for the second parameter, and the constructor will use it to return the n-th parameter.

This allows you to obtain proper ReflectionParameter objects even when documenting code from extensions which (strangely enough) define several parameters with the same name. The string-based constructor always returns the first parameter with the matching name, whereas the integer-based constructor correctly returns the n-th parameter.

So, in short, this works:
<?php
// supposing the extension defined something like:
// Some_Class::someMethod($a, $x, $y, $x, $y)
$p = new ReflectionParameter(array('Some_Class', 'someMethod'), 4);
// returns the last parameter, whereas
$p = new ReflectionParameter(array('Some_Class', 'someMethod'), 'y');
// always returns the first $y at position 2
?>
killgecNOFSPAM at gmail dot com 25-Jul-2007 02:53
Signature of constructor of ReflectionParameter correctly is:

public function __construct(array/string $function, string $name);

where $function is either a name of a global function, or a class/method name pair.
massimo at mmware dot it 18-Jul-2007 05:58
I found these limitations using class ReflectionParameter from ReflectionFunction with INTERNAL FUNCTIONS (eg print_r, str_replace, ... ) :

1. parameter names don't match with manual: (try example 19.35 with arg "call_user_func" )
2. some functions (eg PCRE function, preg_match etc) have EMPTY parameter names
3. calling getDefaultValue on Parameters will result in Exception "Cannot determine default value for internal functions"


Checks if null is allowed

  • ≪ ReflectionParameter
  • ReflectionParameter::canBePassedByValue ≫
  • PHP Manual
  • ReflectionParameter
  • Checks if null is allowed

ReflectionParameter::allowsNull

(PHP 5, PHP 7)

ReflectionParameter::allowsNull — Checks if null is allowed

설명

public bool ReflectionParameter::allowsNull ( void )

Checks whether the parameter allows NULL.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if NULL is allowed, otherwise FALSE

참고

  • ReflectionParameter::isOptional() - Checks if optional

User Contributed Notes

Geoffrey LAURENT 06-May-2013 12:53
The allowsNull method look if arguments have a type.
If a type is defined, null is allowed only if default value is null.

 <?php
function myfunction ( $param ) {

}

echo (new ReflectionFunction("myfunction"))->getParameters()[0]->allowsNull() ? "true":"false";

?>

Result : true

<?php
function myfunction ( stdClass $param ) {

}

echo (new ReflectionFunction("myfunction"))->getParameters()[0]->allowsNull() ? "true":"false";

?>

Result : false

<?php
function myfunction ( stdClass $param = null ) {

}

echo (new ReflectionFunction("myfunction"))->getParameters()[0]->allowsNull() ? "true":"false";
?>

Result : true


Returns whether this parameter can be passed by value

  • ≪ ReflectionParameter::allowsNull
  • ReflectionParameter::__clone ≫
  • PHP Manual
  • ReflectionParameter
  • Returns whether this parameter can be passed by value

ReflectionParameter::canBePassedByValue

(PHP >= 5.4.0)

ReflectionParameter::canBePassedByValue — Returns whether this parameter can be passed by value

설명

public bool ReflectionParameter::canBePassedByValue ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the parameter can be passed by value, FALSE otherwise. Returns NULL in case of an error.

User Contributed Notes

There are no user contributed notes for this page.


Clone

  • ≪ ReflectionParameter::canBePassedByValue
  • ReflectionParameter::__construct ≫
  • PHP Manual
  • ReflectionParameter
  • Clone

ReflectionParameter::__clone

(PHP 5, PHP 7)

ReflectionParameter::__clone — Clone

설명

final private void ReflectionParameter::__clone ( void )

Clones.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

참고

  • ReflectionParameter::toString()
  • Object cloning

User Contributed Notes

There are no user contributed notes for this page.


Construct

  • ≪ ReflectionParameter::__clone
  • ReflectionParameter::export ≫
  • PHP Manual
  • ReflectionParameter
  • Construct

ReflectionParameter::__construct

(PHP 5, PHP 7)

ReflectionParameter::__construct — Construct

설명

public ReflectionParameter::__construct ( string $function , string $parameter )

Constructs a ReflectionParameter class.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

function

The function to reflect parameters from.

parameter

The parameter.

반환값

값을 반환하지 않습니다.

예제

Example #1 Using the ReflectionParameter class

<?php
function foo($a, $b, $c) { }
function bar(Exception $a, &$b, $c) { }
function baz(ReflectionFunction $a, $b = 1, $c = null) { }
function abc() { }

$reflect = new ReflectionFunction('foo');

echo $reflect;

foreach ($reflect->getParameters() as $i => $param) {
    printf(
        "-- Parameter #%d: %s {\n".
        "   Class: %s\n".
        "   Allows NULL: %s\n".
        "   Passed to by reference: %s\n".
        "   Is optional?: %s\n".
        "}\n",
        $i, // $param->getPosition() can be used from PHP 5.2.3
        $param->getName(),
        var_export($param->getClass(), 1),
        var_export($param->allowsNull(), 1),
        var_export($param->isPassedByReference(), 1),
        $param->isOptional() ? 'yes' : 'no'
    );
}
?>

위 예제의 출력 예시:

Function [ <user> function foo ] {
  @@ /Users/philip/cvs/phpdoc/a 2 - 2

  - Parameters [3] {
    Parameter #0 [ <required> $a ]
    Parameter #1 [ <required> $b ]
    Parameter #2 [ <required> $c ]
  }
}
-- Parameter #0: a {
   Class: NULL
   Allows NULL: true
   Passed to by reference: false
   Is optional?: no
}
-- Parameter #1: b {
   Class: NULL
   Allows NULL: true
   Passed to by reference: false
   Is optional?: no
}
-- Parameter #2: c {
   Class: NULL
   Allows NULL: true
   Passed to by reference: false
   Is optional?: no
}

참고

  • ReflectionFunction::__construct() - Constructs a ReflectionFunction object
  • ReflectionMethod::__construct() - Constructs a ReflectionMethod
  • Constructors

User Contributed Notes

tracid2008 t gmail o com 06-Dec-2011 12:31
You also can use a class instead of a function name. Just use an array like that
<?php
$reflect = new ReflectionParameter(array('className', 'methodName'), 'property');
?>


Exports

  • ≪ ReflectionParameter::__construct
  • ReflectionParameter::getClass ≫
  • PHP Manual
  • ReflectionParameter
  • Exports

ReflectionParameter::export

(PHP 5, PHP 7)

ReflectionParameter::export — Exports

설명

public static string ReflectionParameter::export ( string $function , string $parameter [, bool $return ] )

Exports.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

function

The function name.

parameter

The parameter name.

return

TRUE로 설정하면 export를 반환하고, 반대의 경우는 버립니다. FALSE로 설정(기본값)은 반대를 수행합니다.

반환값

The exported reflection.

참고

  • ReflectionParameter::toString()

User Contributed Notes

There are no user contributed notes for this page.


Get the type hinted class

  • ≪ ReflectionParameter::export
  • ReflectionParameter::getDeclaringClass ≫
  • PHP Manual
  • ReflectionParameter
  • Get the type hinted class

ReflectionParameter::getClass

(PHP 5, PHP 7)

ReflectionParameter::getClass — Get the type hinted class

설명

public ReflectionClass ReflectionParameter::getClass ( void )

Gets the class type hinted for the parameter as a ReflectionClass object.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

A ReflectionClass object.

예제

Example #1 Using the ReflectionParameter class

<?php
function foo(Exception $a) { }

$functionReflection = new ReflectionFunction('foo');
$parameters = $functionReflection->getParameters();
$aParameter = $parameters[0];

echo $aParameter->getClass()->name;
?>

위 예제의 출력:

Exception

참고

  • ReflectionParameter::getDeclaringClass() - Gets declaring class

User Contributed Notes

tom at r dot je 11-May-2012 06:15
ReflectionParameter::getClass() will cause a fatal error (and trigger __autoload) if the class required by the parameter is not defined.

Sometimes it's useful to only know the class name without needing the class to be loaded.

Here's a simple function that will retrieve only the class name without requiring the class to exist:

<?php
function getClassName(ReflectionParameter $param) {
    preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
    return isset($matches[1]) ? $matches[1] : null;
}
?>
infernaz at gmail dot com 31-Jan-2011 09:09
The method returns ReflectionClass object of parameter type class or NULL if none.

<?php

class A {
    function b(B $c, array $d, $e) {
    }
}
class B {
}

$refl = new ReflectionClass('A');
$par = $refl->getMethod('b')->getParameters();

var_dump($par[0]->getClass()->getName());  // outputs B
var_dump($par[1]->getClass());  // note that array type outputs NULL
var_dump($par[2]->getClass());  // outputs NULL

?>


Gets declaring class

  • ≪ ReflectionParameter::getClass
  • ReflectionParameter::getDeclaringFunction ≫
  • PHP Manual
  • ReflectionParameter
  • Gets declaring class

ReflectionParameter::getDeclaringClass

(PHP 5, PHP 7)

ReflectionParameter::getDeclaringClass — Gets declaring class

설명

public ReflectionClass ReflectionParameter::getDeclaringClass ( void )

Gets the declaring class.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

A ReflectionClass object.

예제

Example #1 Getting the class that declared the method

<?php
class Foo
{
    public function bar(\DateTime $datetime)
    {
    }
}

class Baz extends Foo
{
}

$param = new \ReflectionParameter(['Baz', 'bar'], 0);

var_dump($param->getDeclaringClass());

위 예제의 출력:

object(ReflectionClass)#2 (1) {
  ["name"]=>
  string(3) "Foo"
}

참고

  • ReflectionParameter::getClass() - Get the type hinted class

User Contributed Notes

There are no user contributed notes for this page.


Gets declaring function

  • ≪ ReflectionParameter::getDeclaringClass
  • ReflectionParameter::getDefaultValue ≫
  • PHP Manual
  • ReflectionParameter
  • Gets declaring function

ReflectionParameter::getDeclaringFunction

(PHP 5 >= 5.2.3, PHP 7)

ReflectionParameter::getDeclaringFunction — Gets declaring function

설명

public ReflectionFunctionAbstract ReflectionParameter::getDeclaringFunction ( void )

Gets the declaring function.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

A ReflectionFunction object.

참고

  • ReflectionParameter::getDeclaringClass() - Gets declaring class

User Contributed Notes

There are no user contributed notes for this page.


Gets default parameter value

  • ≪ ReflectionParameter::getDeclaringFunction
  • ReflectionParameter::getDefaultValueConstantName ≫
  • PHP Manual
  • ReflectionParameter
  • Gets default parameter value

ReflectionParameter::getDefaultValue

(PHP 5 >= 5.0.3, PHP 7)

ReflectionParameter::getDefaultValue — Gets default parameter value

설명

public mixed ReflectionParameter::getDefaultValue ( void )

Gets the default value of the parameter for a user-defined function or method. If the parameter is not optional a ReflectionException will be thrown.

인수

이 함수는 인수가 없습니다.

반환값

The parameters default value.

예제

Example #1 Getting

<?php
function foo($test, $bar = 'baz')
{
    echo $test . $bar;
}

$function = new ReflectionFunction('foo');

foreach ($function->getParameters() as $param) {
    echo 'Name: ' . $param->getName() . PHP_EOL;
    if ($param->isOptional()) {
        echo 'Default value: ' . $param->getDefaultValue() . PHP_EOL;
    }
    echo PHP_EOL;
}
?>

위 예제의 출력:

Name: test

Name: bar
Default value: baz

주의

Note:

Due to implementation details, it is not possible to get the default value of built-in functions or methods of built-in classes. Trying to do this will result a ReflectionException being thrown.

참고

  • ReflectionParameter::isOptional() - Checks if optional
  • ReflectionParameter::isPassedByReference() - Checks if passed by reference

User Contributed Notes

gmail@asmqb7 16-May-2016 09:57
[Editor's note: fixed on user's request]

Getting `Uncaught ReflectionException: Internal error: Failed to retrieve the default value`?

You have to wrap this inside ->isDefaultValueAvailable().


Returns the default value's constant name if default value is constant or null

  • ≪ ReflectionParameter::getDefaultValue
  • ReflectionParameter::getName ≫
  • PHP Manual
  • ReflectionParameter
  • Returns the default value's constant name if default value is constant or null

ReflectionParameter::getDefaultValueConstantName

(PHP 5 >= 5.4.6, PHP 7)

ReflectionParameter::getDefaultValueConstantName — Returns the default value's constant name if default value is constant or null

설명

public string ReflectionParameter::getDefaultValueConstantName ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns string on success or NULL on failure.

User Contributed Notes

There are no user contributed notes for this page.


Gets parameter name

  • ≪ ReflectionParameter::getDefaultValueConstantName
  • ReflectionParameter::getPosition ≫
  • PHP Manual
  • ReflectionParameter
  • Gets parameter name

ReflectionParameter::getName

(PHP 5, PHP 7)

ReflectionParameter::getName — Gets parameter name

설명

public string ReflectionParameter::getName ( void )

Gets the name of the parameter.

인수

이 함수는 인수가 없습니다.

반환값

The name of the reflected parameter.

참고

  • ReflectionParameter::getValue()

User Contributed Notes

mike 25-May-2015 04:26
FYI, it returns the parameter name without the dollars($) sign at the start.


Gets parameter position

  • ≪ ReflectionParameter::getName
  • ReflectionParameter::getType ≫
  • PHP Manual
  • ReflectionParameter
  • Gets parameter position

ReflectionParameter::getPosition

(PHP 5 >= 5.2.3, PHP 7)

ReflectionParameter::getPosition — Gets parameter position

설명

public int ReflectionParameter::getPosition ( void )

Gets the position of the parameter.

인수

이 함수는 인수가 없습니다.

반환값

The position of the parameter, left to right, starting at position #0.

참고

  • ReflectionParameter::getName() - Gets parameter name

User Contributed Notes

micah at raincross-tech dot com 06-Dec-2010 04:26
Note that the ReflectionFunctionAbstract::getParameters() method will return the parameters in the correct order.


Gets a parameter's type

  • ≪ ReflectionParameter::getPosition
  • ReflectionParameter::hasType ≫
  • PHP Manual
  • ReflectionParameter
  • Gets a parameter's type

ReflectionParameter::getType

(PHP 7)

ReflectionParameter::getType — Gets a parameter's type

설명

public ReflectionType ReflectionParameter::getType ( void )

Gets the associated type of a parameter.

인수

이 함수는 인수가 없습니다.

반환값

Returns a ReflectionType object if a parameter type is specified, NULL otherwise.

예제

Example #1 ReflectionParameter::getType() example

<?php
function someFunction(int $param, $param2) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();
$reflectionType1 = $reflectionParams[0]->getType();
$reflectionType2 = $reflectionParams[1]->getType();

echo $reflectionType1;
var_dump($reflectionType2);

위 예제의 출력 예시:

int
null

참고

  • ReflectionParameter::hasType() - Checks if parameter has a type
  • ReflectionType::__toString() - To string

User Contributed Notes

There are no user contributed notes for this page.


Checks if parameter has a type

  • ≪ ReflectionParameter::getType
  • ReflectionParameter::isArray ≫
  • PHP Manual
  • ReflectionParameter
  • Checks if parameter has a type

ReflectionParameter::hasType

(PHP 7)

ReflectionParameter::hasType — Checks if parameter has a type

설명

public bool ReflectionParameter::hasType ( void )

Checks if the parameter has a type associated with it.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if a type is specified, FALSE otherwise.

예제

Example #1 ReflectionParameter::hasType() example

<?php
function someFunction(string $param, $param2 = null) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();

var_dump($reflectionParams[0]->hasType());
var_dump($reflectionParams[1]->hasType());

위 예제의 출력 예시:

bool(true)
bool(false)

참고

  • ReflectionParameter::getType() - Gets a parameter's type

User Contributed Notes

There are no user contributed notes for this page.


Checks if parameter expects an array

  • ≪ ReflectionParameter::hasType
  • ReflectionParameter::isCallable ≫
  • PHP Manual
  • ReflectionParameter
  • Checks if parameter expects an array

ReflectionParameter::isArray

(PHP 5 >= 5.1.0, PHP 7)

ReflectionParameter::isArray — Checks if parameter expects an array

설명

public bool ReflectionParameter::isArray ( void )

Checks if the parameter expects an array.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if an array is expected, FALSE otherwise.

참고

  • ReflectionParameter::isOptional() - Checks if optional

User Contributed Notes

There are no user contributed notes for this page.


Returns whether parameter MUST be callable

  • ≪ ReflectionParameter::isArray
  • ReflectionParameter::isDefaultValueAvailable ≫
  • PHP Manual
  • ReflectionParameter
  • Returns whether parameter MUST be callable

ReflectionParameter::isCallable

(PHP 5 >= 5.4.0, PHP 7)

ReflectionParameter::isCallable — Returns whether parameter MUST be callable

설명

public bool ReflectionParameter::isCallable ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the parameter is callable, FALSE if it is not or NULL on failure.

User Contributed Notes

me at abiusx dot com 15-Jan-2016 02:18
Apparently this does not return true for callback arguments to many of the internal functions, such as array_map and array_walk.


Checks if a default value is available

  • ≪ ReflectionParameter::isCallable
  • ReflectionParameter::isDefaultValueConstant ≫
  • PHP Manual
  • ReflectionParameter
  • Checks if a default value is available

ReflectionParameter::isDefaultValueAvailable

(PHP 5 >= 5.0.3, PHP 7)

ReflectionParameter::isDefaultValueAvailable — Checks if a default value is available

설명

public bool ReflectionParameter::isDefaultValueAvailable ( void )

Checks if a default value for the parameter is available.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if a default value is available, otherwise FALSE

참고

  • ReflectionParameter::getDefaultValue() - Gets default parameter value
  • ReflectionParameter::getName() - Gets parameter name

User Contributed Notes

php at marcyes dot com 03-Aug-2011 02:37
A quick gotcha that I wasn't aware of, suppose you have a function definition like this:

<?php
function foo(array $bar = array('baz' => ''),$che){}
?>

And you want to check if $bar has a default value:

<?php
$rfunc = new ReflectionFunction('foo');
$rparams = $rfunc->getParams();

echo $rparams[0]->isDefaultValueAvailable() ? 'TRUE' : 'FALSE';
?>

That will echo 'FALSE' because $che has no default value so $bar becomes required and the Reflection interface no long sees $bar's default value of array('baz' => '').

The solution is to give $che a default value also:

<?php
function foo(array $bar = array('baz' => ''),$che = null){}
?>

And then $bar's default value will be visible again.

While I understand why it does this, I still wish there was a way to get the default value without resorting to giving all params after it a default value also.


Returns whether the default value of this parameter is constant

  • ≪ ReflectionParameter::isDefaultValueAvailable
  • ReflectionParameter::isOptional ≫
  • PHP Manual
  • ReflectionParameter
  • Returns whether the default value of this parameter is constant

ReflectionParameter::isDefaultValueConstant

(PHP 5 >= 5.4.6, PHP 7)

ReflectionParameter::isDefaultValueConstant — Returns whether the default value of this parameter is constant

설명

public bool ReflectionParameter::isDefaultValueConstant ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the default value is constant, FALSE if it is not or NULL on failure.

User Contributed Notes

kristianoye at gmail dot com 26-May-2015 06:39
This method returns true only if the parameter's default value is set to a named constant.  e.g.

class Foo {
   const OPTION_MULTIGET = 1;

   //Method 1
   public function setGetOptions($opts=0);

   //Method 2
   public function setMultiGetOptions($opts=Foo::OPTION_MULTIGET);
}

Method 1's first ReflectionParameter will return isDefaultValueConstant() = false (0 is a "constant"/static value but not a named constant) and Method 2's will return isDefaultValueConsntant() = true.


Checks if optional

  • ≪ ReflectionParameter::isDefaultValueConstant
  • ReflectionParameter::isPassedByReference ≫
  • PHP Manual
  • ReflectionParameter
  • Checks if optional

ReflectionParameter::isOptional

(PHP 5 >= 5.0.3, PHP 7)

ReflectionParameter::isOptional — Checks if optional

설명

public bool ReflectionParameter::isOptional ( void )

Checks if the parameter is optional.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the parameter is optional, otherwise FALSE

참고

  • ReflectionParameter::getName() - Gets parameter name

User Contributed Notes

There are no user contributed notes for this page.


Checks if passed by reference

  • ≪ ReflectionParameter::isOptional
  • ReflectionParameter::isVariadic ≫
  • PHP Manual
  • ReflectionParameter
  • Checks if passed by reference

ReflectionParameter::isPassedByReference

(PHP 5, PHP 7)

ReflectionParameter::isPassedByReference — Checks if passed by reference

설명

public bool ReflectionParameter::isPassedByReference ( void )

Checks if the parameter is passed in by reference.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the parameter is passed in by reference, otherwise FALSE

참고

  • ReflectionParameter::getName() - Gets parameter name

User Contributed Notes

There are no user contributed notes for this page.


Checks if the parameter is variadic

  • ≪ ReflectionParameter::isPassedByReference
  • ReflectionParameter::__toString ≫
  • PHP Manual
  • ReflectionParameter
  • Checks if the parameter is variadic

ReflectionParameter::isVariadic

(PHP 5 >= 5.6.0, PHP 7)

ReflectionParameter::isVariadic — Checks if the parameter is variadic

설명

public bool ReflectionParameter::isVariadic ( void )

Checks if the parameter was declared as a variadic parameter.

인수

이 함수는 인수가 없습니다.

반환값

Returns TRUE if the parameter is variadic, otherwise FALSE.

User Contributed Notes

There are no user contributed notes for this page.


To string

  • ≪ ReflectionParameter::isVariadic
  • ReflectionProperty ≫
  • PHP Manual
  • ReflectionParameter
  • To string

ReflectionParameter::__toString

(PHP 5, PHP 7)

ReflectionParameter::__toString — To string

설명

public string ReflectionParameter::__toString ( void )

To string.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

참고

  • ReflectionParameter::export() - Exports
  • __toString()

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionProperty class

(PHP 5, PHP 7)

소개

The ReflectionProperty class reports information about classes properties.

클래스 개요

ReflectionProperty implements Reflector {
/* 상수 */
const integer IS_STATIC = 1 ;
const integer IS_PUBLIC = 256 ;
const integer IS_PROTECTED = 512 ;
const integer IS_PRIVATE = 1024 ;
/* 프로퍼티 */
public $name ;
public $class ;
/* 메소드 */
final private void __clone ( void )
public __construct ( mixed $class , string $name )
public static string export ( mixed $class , string $name [, bool $return ] )
public ReflectionClass getDeclaringClass ( void )
public string getDocComment ( void )
public int getModifiers ( void )
public string getName ( void )
public mixed getValue ([ object $object ] )
public bool isDefault ( void )
public bool isPrivate ( void )
public bool isProtected ( void )
public bool isPublic ( void )
public bool isStatic ( void )
public void setAccessible ( bool $accessible )
public void setValue ( object $object , mixed $value )
public string __toString ( void )
}

프로퍼티

name

Name of the property. Read-only, throws ReflectionException in attempt to write.

class

Name of the class where the property is defined. Read-only, throws ReflectionException in attempt to write.

예약 상수

ReflectionProperty Modifiers

ReflectionProperty::IS_STATIC

Indicates static properties.

ReflectionProperty::IS_PUBLIC

Indicates public properties.

ReflectionProperty::IS_PROTECTED

Indicates protected properties.

ReflectionProperty::IS_PRIVATE

Indicates private properties.

Table of Contents

  • ReflectionProperty::__clone — Clone
  • ReflectionProperty::__construct — Construct a ReflectionProperty object
  • ReflectionProperty::export — Export
  • ReflectionProperty::getDeclaringClass — Gets declaring class
  • ReflectionProperty::getDocComment — Gets the property doc comment
  • ReflectionProperty::getModifiers — Gets modifiers
  • ReflectionProperty::getName — Gets property name
  • ReflectionProperty::getValue — Gets value
  • ReflectionProperty::isDefault — Checks if property is a default property
  • ReflectionProperty::isPrivate — Checks if property is private
  • ReflectionProperty::isProtected — Checks if property is protected
  • ReflectionProperty::isPublic — Checks if property is public
  • ReflectionProperty::isStatic — Checks if property is static
  • ReflectionProperty::setAccessible — Set property accessibility
  • ReflectionProperty::setValue — Set property value
  • ReflectionProperty::__toString — To string

User Contributed Notes

Nanhe Kumar 06-Jan-2014 09:36
<?php
//serialize static properties (class variable)

class Student {

    private $members = array();
    protected $name;
    public static $noOfStudent;

    public function __construct($name = 'Nanhe Kumar') {
        $this->name = $name;
        Student::$noOfStudent++;
    }

    public function __sleep() {
        $vars = get_class_vars(get_class($this));
        foreach ($vars as $key => $val) {
            if (!empty($val))
                $this->members[$key] = $val;
        }
        return array_keys(get_object_vars($this));
    }

    public function __wakeup() {
        foreach ($this->members as $key => $val) {
            $prop = new ReflectionProperty(get_class($this), $key);
            $prop->setValue(get_class($this), $val);
        }
        $this->members = array();
    }

    public function getTotalStudent() {
        return self::$noOfStudent;
    }

}

$so1 = new Student();
$so2 = new Student();
$serialized = serialize($so1);
print_r($serialized); //O:7:"Student":2:{s:16:"Studentmembers";a:1:{s:11:"noOfStudent";i:2;}s:7:"*name";s:11:"Nanhe Kumar";}
$unserialized = unserialize($serialized);
print_r($unserialized); //Student Object ( [members:Student:private] => Array ( ) [name:protected] => Nanhe Kumar )
echo Student::$noOfStudent; //2
?>
james at digitaledgeit dot com dot au 30-Aug-2010 05:30
Please ignore my previous note. I didn't fully understand what the previous notes were referring to as "runtime". I now see they were referring to a slightly different question.

Apologies!
rasmus at mindplay dot dk 30-Jul-2010 02:06
I think a more accurate explanation is this:

The Reflection classes are designed to reflect upon the source code of an application, not on any runtime information.

I think you misunderstand the ReflectionProperty constructor in your example above. The fact that it accepts an object as argument is just a convenience feature - you are actually inspecting the class of that object, not the object itself, so it's basically equivalent to:

<?php

// works fine
$Reflection = new ReflectionProperty(get_class($a), 'a');

// throws exception
$Reflection = new ReflectionProperty(get_class($a), 'foo');

?>

Getting the class of the object you're passing in is implied, since inspecting a defined property is the purpose of this class.

In your example, $a->foo is a dynamic member - it is not defined as a member of class, so there is no defining class reference, line number, default value, etc. - which means, there is nothing to reflect upon.

Clearly this very useful library could use some real documentation...


Clone

  • ≪ ReflectionProperty
  • ReflectionProperty::__construct ≫
  • PHP Manual
  • ReflectionProperty
  • Clone

ReflectionProperty::__clone

(PHP 5, PHP 7)

ReflectionProperty::__clone — Clone

설명

final private void ReflectionProperty::__clone ( void )

Clones.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

참고

  • ReflectionProperty::export() - Export
  • Object cloning

User Contributed Notes

There are no user contributed notes for this page.


Construct a ReflectionProperty object

  • ≪ ReflectionProperty::__clone
  • ReflectionProperty::export ≫
  • PHP Manual
  • ReflectionProperty
  • Construct a ReflectionProperty object

ReflectionProperty::__construct

(PHP 5, PHP 7)

ReflectionProperty::__construct — Construct a ReflectionProperty object

설명

public ReflectionProperty::__construct ( mixed $class , string $name )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

class

The class name, that contains the property.

name

The name of the property being reflected.

반환값

값을 반환하지 않습니다.

오류/예외

Trying to get or set private or protected class property's values will result in an exception being thrown.

예제

Example #1 ReflectionProperty::__construct() example

<?php
class Str
{
    public $length  = 5;
}

// Create an instance of the ReflectionProperty class
$prop = new ReflectionProperty('Str', 'length');

// Print out basic information
printf(
    "===> The%s%s%s%s property '%s' (which was %s)\n" .
    "     having the modifiers %s\n",
        $prop->isPublic() ? ' public' : '',
        $prop->isPrivate() ? ' private' : '',
        $prop->isProtected() ? ' protected' : '',
        $prop->isStatic() ? ' static' : '',
        $prop->getName(),
        $prop->isDefault() ? 'declared at compile-time' : 'created at run-time',
        var_export(Reflection::getModifierNames($prop->getModifiers()), 1)
);

// Create an instance of Str
$obj= new Str();

// Get current value
printf("---> Value is: ");
var_dump($prop->getValue($obj));

// Change value
$prop->setValue($obj, 10);
printf("---> Setting value to 10, new value is: ");
var_dump($prop->getValue($obj));

// Dump object
var_dump($obj);
?>

위 예제의 출력 예시:

===> The public property 'length' (which was declared at compile-time)
     having the modifiers array (
  0 => 'public',
)
---> Value is: int(5)
---> Setting value to 10, new value is: int(10)
object(Str)#2 (1) {
  ["length"]=>
  int(10)
}

Example #2 Getting value from private and protected properties using ReflectionProperty class

<?php

class Foo {
    public $x = 1;
    protected $y = 2;
    private $z = 3;
}

$obj = new Foo;

$prop = new ReflectionProperty('Foo', 'y');
$prop->setAccessible(true); /* As of PHP 5.3.0 */
var_dump($prop->getValue($obj)); // int(2)

$prop = new ReflectionProperty('Foo', 'z');
$prop->setAccessible(true); /* As of PHP 5.3.0 */
var_dump($prop->getValue($obj)); // int(2)

?>

위 예제의 출력 예시:

int(2)
int(3)

참고

  • ReflectionProperty::getName() - Gets property name
  • Constructors

User Contributed Notes

There are no user contributed notes for this page.


Export

  • ≪ ReflectionProperty::__construct
  • ReflectionProperty::getDeclaringClass ≫
  • PHP Manual
  • ReflectionProperty
  • Export

ReflectionProperty::export

(PHP 5, PHP 7)

ReflectionProperty::export — Export

설명

public static string ReflectionProperty::export ( mixed $class , string $name [, bool $return ] )

Exports a reflection.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

argument

내보낼 reflection

name

The property name.

return

TRUE로 설정하면 export를 반환하고, 반대의 경우는 버립니다. FALSE로 설정(기본값)은 반대를 수행합니다.

반환값

참고

  • ReflectionProperty::toString()

User Contributed Notes

There are no user contributed notes for this page.


Gets declaring class

  • ≪ ReflectionProperty::export
  • ReflectionProperty::getDocComment ≫
  • PHP Manual
  • ReflectionProperty
  • Gets declaring class

ReflectionProperty::getDeclaringClass

(PHP 5, PHP 7)

ReflectionProperty::getDeclaringClass — Gets declaring class

설명

public ReflectionClass ReflectionProperty::getDeclaringClass ( void )

Gets the declaring class.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

A ReflectionClass object.

참고

  • ReflectionProperty::getName() - Gets property name

User Contributed Notes

metamarkers at gmail dot com 17-May-2013 03:30
If you're reflecting an object and get the declaring class of a property that's set but wasn't declared in any class, it returns the class of the instance.

<?php

class X {

}

$x = new X();
$x->foo = 'bar';
$reflection = new ReflectionObject($x);
echo $reflection->getProperty('foo')->getDeclaringClass()->getName(); // X

?>


Gets the property doc comment

  • ≪ ReflectionProperty::getDeclaringClass
  • ReflectionProperty::getModifiers ≫
  • PHP Manual
  • ReflectionProperty
  • Gets the property doc comment

ReflectionProperty::getDocComment

(PHP 5 >= 5.1.0, PHP 7)

ReflectionProperty::getDocComment — Gets the property doc comment

설명

public string ReflectionProperty::getDocComment ( void )

Gets the doc comment for a property.

인수

이 함수는 인수가 없습니다.

반환값

The property doc comment.

예제

Example #1 ReflectionProperty::getDocComment() example

<?php
class Str
{
    /**
     * @var int  The length of the string
     */
    public $length = 5;
}

$prop = new ReflectionProperty('Str', 'length');

var_dump($prop->getDocComment());

?>

위 예제의 출력 예시:

string(53) "/**
     * @var int  The length of the string
     */"

참고

  • ReflectionProperty::getModifiers() - Gets modifiers
  • ReflectionProperty::getName() - Gets property name
  • ReflectionProperty::getValue() - Gets value

User Contributed Notes

There are no user contributed notes for this page.


Gets modifiers

  • ≪ ReflectionProperty::getDocComment
  • ReflectionProperty::getName ≫
  • PHP Manual
  • ReflectionProperty
  • Gets modifiers

ReflectionProperty::getModifiers

(PHP 5, PHP 7)

ReflectionProperty::getModifiers — Gets modifiers

설명

public int ReflectionProperty::getModifiers ( void )

Gets the modifiers.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

A numeric representation of the modifiers.

참고

  • ReflectionProperty::isPrivate() - Checks if property is private
  • Reflection::getModifierNames() - Gets modifier names

User Contributed Notes

There are no user contributed notes for this page.


Gets property name

  • ≪ ReflectionProperty::getModifiers
  • ReflectionProperty::getValue ≫
  • PHP Manual
  • ReflectionProperty
  • Gets property name

ReflectionProperty::getName

(PHP 5, PHP 7)

ReflectionProperty::getName — Gets property name

설명

public string ReflectionProperty::getName ( void )

Gets the properties name.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

The name of the reflected property.

참고

  • ReflectionProperty::getValue() - Gets value

User Contributed Notes

There are no user contributed notes for this page.


Gets value

  • ≪ ReflectionProperty::getName
  • ReflectionProperty::isDefault ≫
  • PHP Manual
  • ReflectionProperty
  • Gets value

ReflectionProperty::getValue

(PHP 5, PHP 7)

ReflectionProperty::getValue — Gets value

설명

public mixed ReflectionProperty::getValue ([ object $object ] )

Gets the property's value.

인수

object

If the property is non-static an object must be provided to fetch the property from. If you want to fetch the default property without providing an object use ReflectionClass::getDefaultProperties() instead.

반환값

The current value of the property.

오류/예외

Throws a ReflectionException if the property is inaccessible. You can make a protected or private property accessible using ReflectionProperty::setAccessible().

예제

Example #1 ReflectionProperty::getValue() example

<?php
class Foo {
    public static $staticProperty = 'foobar';

    public $property = 'barfoo';
    protected $privateProperty = 'foofoo';
}

$reflectionClass = new ReflectionClass('Foo');

var_dump($reflectionClass->getProperty('staticProperty')->getValue());
var_dump($reflectionClass->getProperty('property')->getValue(new Foo));

$reflectionProperty = $reflectionClass->getProperty('privateProperty');
$reflectionProperty->setAccessible(true);
var_dump($reflectionProperty->getValue(new Foo));
?>

위 예제의 출력:

string(6) "foobar"
string(6) "barfoo"
string(6) "foofoo"

참고

  • ReflectionProperty::setValue() - Set property value
  • ReflectionProperty::setAccessible() - Set property accessibility
  • ReflectionClass::getDefaultProperties() - Gets default properties
  • ReflectionClass::getStaticPropertyValue() - Gets static property value

User Contributed Notes

sergiy dot sokolenko at gmail dot com 29-Jun-2010 12:48
To allow protected and private properties to be accessed, you should use
ReflectionProperty::setAccessible(bool $accessible):

<?php
/** Class Foo with protected and private members */
class Foo {
    protected $bar = 'barrr!';
    private $baz = 'bazzz!';
}

$reflFoo = new ReflectionClass('Foo');
$reflBar = $reflFoo->getProperty('bar');
$reflBaz = $reflFoo->getProperty('baz');

// Set private and protected members accessible for getValue/setValue
$reflBar->setAccessible(true);
$reflBaz->setAccessible(true);

$foo = new Foo();
echo $reflBar->getValue($foo); // will output "barrr!"
echo $reflBaz->getValue($foo); // will output "bazzz!"

// You can also setValue
$reflBar->setValue($foo, "new value");
echo $reflBar->getValue($foo); // will output "new value"
?>


Checks if property is a default property

  • ≪ ReflectionProperty::getValue
  • ReflectionProperty::isPrivate ≫
  • PHP Manual
  • ReflectionProperty
  • Checks if property is a default property

ReflectionProperty::isDefault

(PHP 5, PHP 7)

ReflectionProperty::isDefault — Checks if property is a default property

설명

public bool ReflectionProperty::isDefault ( void )

Checks whether the property was declared at compile-time, or whether the property was dynamically declared at run-time.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the property was declared at compile-time, or FALSE if it was created at run-time.

예제

Example #1 ReflectionClass::isDefault() example

<?php
class Foo {
    public $bar;
}

$o = new Foo();
$o->bar = 42;
$o->baz = 42;

$ro = new ReflectionObject($o);
var_dump($ro->getProperty('bar')->isDefault());
var_dump($ro->getProperty('baz')->isDefault());
?>

위 예제의 출력:

bool(true)
bool(false)

참고

  • ReflectionProperty::getValue() - Gets value

User Contributed Notes

There are no user contributed notes for this page.


Checks if property is private

  • ≪ ReflectionProperty::isDefault
  • ReflectionProperty::isProtected ≫
  • PHP Manual
  • ReflectionProperty
  • Checks if property is private

ReflectionProperty::isPrivate

(PHP 5, PHP 7)

ReflectionProperty::isPrivate — Checks if property is private

설명

public bool ReflectionProperty::isPrivate ( void )

Checks whether the property is private.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the property is private, FALSE otherwise.

참고

  • ReflectionProperty::isPublic() - Checks if property is public
  • ReflectionProperty::isProtected() - Checks if property is protected
  • ReflectionProperty::isStatic() - Checks if property is static

User Contributed Notes

There are no user contributed notes for this page.


Checks if property is protected

  • ≪ ReflectionProperty::isPrivate
  • ReflectionProperty::isPublic ≫
  • PHP Manual
  • ReflectionProperty
  • Checks if property is protected

ReflectionProperty::isProtected

(PHP 5, PHP 7)

ReflectionProperty::isProtected — Checks if property is protected

설명

public bool ReflectionProperty::isProtected ( void )

Checks whether the property is protected.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the property is protected, FALSE otherwise.

참고

  • ReflectionProperty::isPublic() - Checks if property is public
  • ReflectionProperty::isPrivate() - Checks if property is private
  • ReflectionProperty::isStatic() - Checks if property is static

User Contributed Notes

Ievgen Iefimenko the_boss at bk dot ru 10-Jun-2012 06:44
<?php

/**
* Return 1 if property is public,
* else return void
*/

class Classname{
    private $variable;
}

$obj = new Classname;
$rp = new ReflectionProperty($obj,'variable');
echo $rp->isPrivate();
?>


Checks if property is public

  • ≪ ReflectionProperty::isProtected
  • ReflectionProperty::isStatic ≫
  • PHP Manual
  • ReflectionProperty
  • Checks if property is public

ReflectionProperty::isPublic

(PHP 5, PHP 7)

ReflectionProperty::isPublic — Checks if property is public

설명

public bool ReflectionProperty::isPublic ( void )

Checks whether the property is public.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the property is public, FALSE otherwise.

참고

  • ReflectionProperty::isProtected() - Checks if property is protected
  • ReflectionProperty::isPrivate() - Checks if property is private
  • ReflectionProperty::isStatic() - Checks if property is static

User Contributed Notes

There are no user contributed notes for this page.


Checks if property is static

  • ≪ ReflectionProperty::isPublic
  • ReflectionProperty::setAccessible ≫
  • PHP Manual
  • ReflectionProperty
  • Checks if property is static

ReflectionProperty::isStatic

(PHP 5, PHP 7)

ReflectionProperty::isStatic — Checks if property is static

설명

public bool ReflectionProperty::isStatic ( void )

Checks whether the property is static.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if the property is static, FALSE otherwise.

참고

  • ReflectionProperty::isPublic() - Checks if property is public
  • ReflectionProperty::isProtected() - Checks if property is protected
  • ReflectionProperty::isPrivate() - Checks if property is private

User Contributed Notes

There are no user contributed notes for this page.


Set property accessibility

  • ≪ ReflectionProperty::isStatic
  • ReflectionProperty::setValue ≫
  • PHP Manual
  • ReflectionProperty
  • Set property accessibility

ReflectionProperty::setAccessible

(PHP 5 >= 5.3.0, PHP 7)

ReflectionProperty::setAccessible — Set property accessibility

설명

public void ReflectionProperty::setAccessible ( bool $accessible )

Sets a property to be accessible. For example, it may allow protected and private properties to be accessed.

인수

accessible

TRUE to allow accessibility, or FALSE.

반환값

값을 반환하지 않습니다.

참고

  • ReflectionProperty::isPrivate() - Checks if property is private
  • ReflectionProperty::isProtected() - Checks if property is protected

User Contributed Notes

matthieu at mnapoli dot fr 20-Oct-2012 05:50
Note that the property will only become accessible using the ReflectionProperty class. The property is still private or protected in the class instances.

<?php
class MyClass {
     private $myProperty = true;
}

$class = new ReflectionClass("MyClass");
$property = $class->getProperty("myProperty");
$property->setAccessible(true);

$obj = new MyClass();
echo $property->getValue($obj); // Works
echo $obj->myProperty; // Doesn't work (error)
?>
Yzmir Ramirez 16-Oct-2010 02:31
Have you tried:

<?php

echo "PHP Version: ".phpversion()."\n";

class Foo
{
    private   $bar  = "private";
    protected $bar2 = "protected";
    public    $bar3 = "public";
}

$obj = new Foo;

$arr = (array)$obj;

print_r($arr);
?>

Output:

PHP Version: 5.2.12
Array
(
    [Foobar] => private
    [*bar2] => protected
    [bar3] => public
)

PHP Version: 5.1.6
Array
(
    [Foobar] => private
    [*bar2] => protected
    [bar3] => public
)
Rob McVey 11-Jun-2010 07:11
If you are using < PHP 5.3 and need to get the private attributes and values, you can use this method:

This is what you are doing:

<?php
$obj_with_privates = new MyObject();
$class     = get_class($obj_with_privates);
$vars     = get_object_vars($obj_with_privates);
//will not show private attributes
print_r($vars);

$reflection = new ReflectionClass( $class );
$attributes = $reflection->getProperties();
//still no private access!
print_r($attributes);
?>

This is what you should do:

<?php
$obj_with_privates = new MyObject();

$class         = get_class( $obj_with_privates );
$reflection = new ReflectionClass( $class );
$abstract    = $reflection->getMethods( ReflectionMethod::IS_ABSTRACT );
$priv_attr  = $reflection->getProperties( ReflectionProperty::IS_PRIVATE );
$privates   = array();
$parent     = get_parent_class( $class );
$child         = $class;
$constructor = $reflection->getConstructor();

//If the class has abstract methods you need to implement them
$abstr_methods = "";
if(sizeof($abstr_methods))
{
    foreach($abstract as $method)
    {
        $mname = $method->name;
        $abstr_methods .= "public function $mname(){return false;}";
    }
}

//Convert private attributes to public attributes
if(sizeof($priv_attr))
{
    $parseable = unserialize(str_replace("\0$class\0", "\0*\0", serialize($obj)));
    foreach($priv_attr as $attribute)
    {
        $aname = $attribute->name;
        $privates[$aname] = $parseable->$aname;
    }
}

$temp_child_class = "temp" . str_replace("_", "", "$class");

//You can gain access to protected attributes by extending the target class
$class_def = "
class $temp_child_class extends $class{
    $constructor
    public function reflect_getmyvars(){
        return get_object_vars(\$this);
    }
    $abstr_methods
}
";

//place class definition in memory
eval($class_def);

//generate object from dynamic class
$tcobj =@ new $temp_child_class;
//call the method we added to the object (to access protected vars)
$vars = $tcobj->reflect_getmyvars();

$attribs = array_merge($vars, $privates);

//will now show private attributes
print_r($attribs);
?>


Set property value

  • ≪ ReflectionProperty::setAccessible
  • ReflectionProperty::__toString ≫
  • PHP Manual
  • ReflectionProperty
  • Set property value

ReflectionProperty::setValue

(PHP 5, PHP 7)

ReflectionProperty::setValue — Set property value

설명

public void ReflectionProperty::setValue ( object $object , mixed $value )
public void ReflectionProperty::setValue ( mixed $value )

Sets (changes) the property's value.

인수

object

If the property is non-static an object must be provided to change the property on. If the property is static this parameter is left out and only value needs to be provided.

value

The new value.

반환값

값을 반환하지 않습니다.

오류/예외

Throws a ReflectionException if the property is inaccessible. You can make a protected or private property accessible using ReflectionProperty::setAccessible().

예제

Example #1 ReflectionProperty::setValue() example

<?php
class Foo {
    public static $staticProperty;

    public $property;
    protected $privateProperty;
}

$reflectionClass = new ReflectionClass('Foo');

$reflectionClass->getProperty('staticProperty')->setValue('foo');
var_dump(Foo::$staticProperty);

$foo = new Foo;

$reflectionClass->getProperty('property')->setValue($foo, 'bar');
var_dump($foo->property);

$reflectionProperty = $reflectionClass->getProperty('privateProperty');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($foo, 'foobar');
var_dump($reflectionProperty->getValue($foo));
?>

위 예제의 출력:

string(3) "foo"
string(3) "bar"
string(6) "foobar"

참고

  • ReflectionProperty::getValue() - Gets value
  • ReflectionProperty::setAccessible() - Set property accessibility
  • ReflectionClass::setStaticPropertyValue() - Sets static property value

User Contributed Notes

me at ircmaxell dot om 22-Jun-2011 06:27
You can use ReflectionProperty::setValue to set the value on static properties as well as regular instance properties.  Simply pass null in place of the instance:

<?php
class Foo {
    protected static $bar = null;
    public static function sayBar() {
        echo self::$bar;
    }
}

$r = new ReflectionProperty('Foo', 'bar');
$r->setAccessible(true);
$r->setValue(null, 'foo');

Foo::sayBar(); // "foo"
?>


To string

  • ≪ ReflectionProperty::setValue
  • ReflectionType ≫
  • PHP Manual
  • ReflectionProperty
  • To string

ReflectionProperty::__toString

(PHP 5, PHP 7)

ReflectionProperty::__toString — To string

설명

public string ReflectionProperty::__toString ( void )

To string.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

참고

  • __toString()

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionType class

(PHP 7)

소개

The ReflectionType class reports information about a function's return type.

클래스 개요

ReflectionType {
/* 메소드 */
public bool allowsNull ( void )
public bool isBuiltin ( void )
public string __toString ( void )
}

Table of Contents

  • ReflectionType::allowsNull — Checks if null is allowed
  • ReflectionType::isBuiltin — Checks if it is a built-in type
  • ReflectionType::__toString — To string

User Contributed Notes

There are no user contributed notes for this page.


Checks if null is allowed

  • ≪ ReflectionType
  • ReflectionType::isBuiltin ≫
  • PHP Manual
  • ReflectionType
  • Checks if null is allowed

ReflectionType::allowsNull

(PHP 7)

ReflectionType::allowsNull — Checks if null is allowed

설명

public bool ReflectionType::allowsNull ( void )

Checks whether the parameter allows NULL.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if NULL is allowed, otherwise FALSE

예제

Example #1 ReflectionType::allowsNull() example

<?php
function someFunction(string $param, StdClass $param2 = null) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();

var_dump($reflectionParams[0]->getType()->allowsNull());
var_dump($reflectionParams[1]->getType()->allowsNull());

위 예제의 출력 예시:

bool(false)
bool(true)

참고

  • ReflectionType::isBuiltin() - Checks if it is a built-in type
  • ReflectionType::__toString() - To string
  • ReflectionParameter::getType() - Gets a parameter's type

User Contributed Notes

There are no user contributed notes for this page.


Checks if it is a built-in type

  • ≪ ReflectionType::allowsNull
  • ReflectionType::__toString ≫
  • PHP Manual
  • ReflectionType
  • Checks if it is a built-in type

ReflectionType::isBuiltin

(PHP 7)

ReflectionType::isBuiltin — Checks if it is a built-in type

설명

public bool ReflectionType::isBuiltin ( void )

Checks if the type is a built-in type in PHP.

인수

이 함수는 인수가 없습니다.

반환값

TRUE if it's a built-in type, otherwise FALSE

예제

Example #1 ReflectionType::isBuiltin() example

<?php
class SomeClass {}

function someFunction(string $param, SomeClass $param2, StdClass $param3) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();

var_dump($reflectionParams[0]->getType()->isBuiltin());
var_dump($reflectionParams[1]->getType()->isBuiltin());
var_dump($reflectionParams[2]->getType()->isBuiltin());

위 예제의 출력 예시:

bool(true)
bool(false)
bool(false)

Note that the ReflectionType::isBuiltin() method does not distinguish between internal and custom classes. To make this distinction, the ReflectionClass::isInternal() method should be used on the returned class name.

참고

  • ReflectionType::allowsNull() - Checks if null is allowed
  • ReflectionType::__toString() - To string
  • ReflectionClass::isInternal() - Checks if class is defined internally by an extension, or the core
  • ReflectionParameter::getType() - Gets a parameter's type

User Contributed Notes

There are no user contributed notes for this page.


To string

  • ≪ ReflectionType::isBuiltin
  • ReflectionGenerator ≫
  • PHP Manual
  • ReflectionType
  • To string

ReflectionType::__toString

(PHP 7)

ReflectionType::__toString — To string

설명

public string ReflectionType::__toString ( void )

Gets the parameter type name.

인수

이 함수는 인수가 없습니다.

반환값

Returns the type of the parameter.

예제

Example #1 ReflectionType::__toString() example

<?php
function someFunction(string $param) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParam = $reflectionFunc->getParameters()[0];

echo $reflectionParam->getType();

위 예제의 출력 예시:

string

참고

  • ReflectionType::allowsNull() - Checks if null is allowed
  • ReflectionType::isBuiltin() - Checks if it is a built-in type
  • ReflectionParameter::getType() - Gets a parameter's type

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionGenerator class

(PHP 7)

소개

The ReflectionGenerator class reports information about a generator.

클래스 개요

ReflectionGenerator {
/* 메소드 */
public __construct ( Generator $generator )
public string getExecutingFile ( void )
public Generator getExecutingGenerator ( void )
public int getExecutingLine ( void )
public ReflectionFunctionAbstract getFunction ( void )
public object getThis ( void )
public array getTrace ([ int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT ] )
}

Table of Contents

  • ReflectionGenerator::__construct — Constructs a ReflectionGenerator object
  • ReflectionGenerator::getExecutingFile — Gets the file name of the currently executing generator
  • ReflectionGenerator::getExecutingGenerator — Gets the executing Generator object
  • ReflectionGenerator::getExecutingLine — Gets the currently executing line of the generator
  • ReflectionGenerator::getFunction — Gets the function name of the generator
  • ReflectionGenerator::getThis — Gets the $this value of the generator
  • ReflectionGenerator::getTrace — Gets the trace of the executing generator

User Contributed Notes

There are no user contributed notes for this page.


Constructs a ReflectionGenerator object

  • ≪ ReflectionGenerator
  • ReflectionGenerator::getExecutingFile ≫
  • PHP Manual
  • ReflectionGenerator
  • Constructs a ReflectionGenerator object

ReflectionGenerator::__construct

(PHP 7)

ReflectionGenerator::__construct — Constructs a ReflectionGenerator object

설명

public ReflectionGenerator::__construct ( Generator $generator )

Constructs a ReflectionGenerator object.

인수

generator

A generator object.

반환값

값을 반환하지 않습니다.

예제

Example #1 ReflectionGenerator::__construct() example

<?php

function gen()
{
    yield 1;
}

$gen = gen();

$reflectionGen = new ReflectionGenerator($gen);

echo <<< output
{$reflectionGen->getFunction()->name}
Line: {$reflectionGen->getExecutingLine()}
File: {$reflectionGen->getExecutingFile()}
output;

위 예제의 출력 예시:

gen
Line: 5
File: /path/to/file/example.php

참고

  • ReflectionGenerator::getFunction() - Gets the function name of the generator
  • ReflectionGenerator::getExecutingLine() - Gets the currently executing line of the generator
  • ReflectionGenerator::getExecutingFile() - Gets the file name of the currently executing generator

User Contributed Notes

There are no user contributed notes for this page.


Gets the file name of the currently executing generator

  • ≪ ReflectionGenerator::__construct
  • ReflectionGenerator::getExecutingGenerator ≫
  • PHP Manual
  • ReflectionGenerator
  • Gets the file name of the currently executing generator

ReflectionGenerator::getExecutingFile

(PHP 7)

ReflectionGenerator::getExecutingFile — Gets the file name of the currently executing generator

설명

public string ReflectionGenerator::getExecutingFile ( void )

Get the full path and file name of the currently executing generator.

인수

이 함수는 인수가 없습니다.

반환값

Returns the full path and file name of the currently executing generator.

예제

Example #1 ReflectionGenerator::getExecutingFile() example

<?php

class GenExample
{
    public function gen()
    {
        yield 1;
    }
}

$gen = (new GenExample)->gen();

$reflectionGen = new ReflectionGenerator($gen);

echo "File: {$reflectionGen->getExecutingFile()}";

위 예제의 출력 예시:

File: /path/to/file/example.php

참고

  • ReflectionGenerator::getExecutingLine() - Gets the currently executing line of the generator
  • ReflectionGenerator::getExecutingGenerator() - Gets the executing Generator object

User Contributed Notes

There are no user contributed notes for this page.


Gets the executing Generator object

  • ≪ ReflectionGenerator::getExecutingFile
  • ReflectionGenerator::getExecutingLine ≫
  • PHP Manual
  • ReflectionGenerator
  • Gets the executing Generator object

ReflectionGenerator::getExecutingGenerator

(PHP 7)

ReflectionGenerator::getExecutingGenerator — Gets the executing Generator object

설명

public Generator ReflectionGenerator::getExecutingGenerator ( void )

Get the executing Generator object

인수

이 함수는 인수가 없습니다.

반환값

Returns the currently executing Generator object.

예제

Example #1 ReflectionGenerator::getExecutingGenerator() example

<?php

class GenExample
{
    public function gen()
    {
        yield 1;
    }
}

$gen = (new GenExample)->gen();

$reflectionGen = new ReflectionGenerator($gen);

$gen2 = $reflectionGen->getExecutingGenerator();

var_dump($gen2 === $gen);
var_dump($gen2->current());

위 예제의 출력 예시:

bool(true)
int(1);

참고

  • ReflectionGenerator::getExecutingLine() - Gets the currently executing line of the generator
  • ReflectionGenerator::getExecutingFile() - Gets the file name of the currently executing generator

User Contributed Notes

There are no user contributed notes for this page.


Gets the currently executing line of the generator

  • ≪ ReflectionGenerator::getExecutingGenerator
  • ReflectionGenerator::getFunction ≫
  • PHP Manual
  • ReflectionGenerator
  • Gets the currently executing line of the generator

ReflectionGenerator::getExecutingLine

(PHP 7)

ReflectionGenerator::getExecutingLine — Gets the currently executing line of the generator

설명

public int ReflectionGenerator::getExecutingLine ( void )

Get the currently executing line number of the generator.

인수

이 함수는 인수가 없습니다.

반환값

Returns the line number of the currently executing statement in the generator.

예제

Example #1 ReflectionGenerator::getExecutingLine() example

<?php

class GenExample
{
    public function gen()
    {
        yield 1;
    }
}

$gen = (new GenExample)->gen();

$reflectionGen = new ReflectionGenerator($gen);

echo "Line: {$reflectionGen->getExecutingLine()}";

위 예제의 출력 예시:

Line: 7

참고

  • ReflectionGenerator::getExecutingGenerator() - Gets the executing Generator object
  • ReflectionGenerator::getExecutingFile() - Gets the file name of the currently executing generator

User Contributed Notes

There are no user contributed notes for this page.


Gets the function name of the generator

  • ≪ ReflectionGenerator::getExecutingLine
  • ReflectionGenerator::getThis ≫
  • PHP Manual
  • ReflectionGenerator
  • Gets the function name of the generator

ReflectionGenerator::getFunction

(PHP 7)

ReflectionGenerator::getFunction — Gets the function name of the generator

설명

public ReflectionFunctionAbstract ReflectionGenerator::getFunction ( void )

Enables the function name of the generator to be obtained by returning a class derived from ReflectionFunctionAbstract.

인수

이 함수는 인수가 없습니다.

반환값

Returns a ReflectionFunctionAbstract class. This will be ReflectionFunction for functions, or ReflectionMethod for methods.

예제

Example #1 ReflectionGenerator::getFunction() example

<?php

function gen()
{
    yield 1;
}

$gen = gen();

$reflectionGen = new ReflectionGenerator($gen);

var_dump($reflectionGen->getFunction());

위 예제의 출력 예시:

object(ReflectionFunction)#3 (1) {
  ["name"]=>
  string(3) "gen"
}

참고

  • ReflectionGenerator::getThis() - Gets the $this value of the generator
  • ReflectionGenerator::getTrace() - Gets the trace of the executing generator

User Contributed Notes

There are no user contributed notes for this page.


Gets the $this value of the generator

  • ≪ ReflectionGenerator::getFunction
  • ReflectionGenerator::getTrace ≫
  • PHP Manual
  • ReflectionGenerator
  • Gets the $this value of the generator

ReflectionGenerator::getThis

(PHP 7)

ReflectionGenerator::getThis — Gets the $this value of the generator

설명

public object ReflectionGenerator::getThis ( void )

Get the $this value that the generator has access to.

인수

이 함수는 인수가 없습니다.

반환값

Returns the $this value, or NULL if the generator was not created in a class context.

예제

Example #1 ReflectionGenerator::getThis() example

<?php

class GenExample
{
    public function gen()
    {
        yield 1;
    }
}

$gen = (new GenExample)->gen();

$reflectionGen = new ReflectionGenerator($gen);

var_dump($reflectionGen->getThis());

위 예제의 출력 예시:

object(GenExample)#3 (0) {
}

참고

  • ReflectionGenerator::getFunction() - Gets the function name of the generator
  • ReflectionGenerator::getTrace() - Gets the trace of the executing generator

User Contributed Notes

There are no user contributed notes for this page.


Gets the trace of the executing generator

  • ≪ ReflectionGenerator::getThis
  • Reflector ≫
  • PHP Manual
  • ReflectionGenerator
  • Gets the trace of the executing generator

ReflectionGenerator::getTrace

(PHP 7)

ReflectionGenerator::getTrace — Gets the trace of the executing generator

설명

public array ReflectionGenerator::getTrace ([ int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT ] )

Get the trace of the currently executing generator.

인수

options

The value of options can be any of the following the following flags.

Available options
Option | Description
DEBUG_BACKTRACE_PROVIDE_OBJECT | Default.
DEBUG_BACKTRACE_IGNORE_ARGS | Don't include the argument information for functions in the stack trace.

반환값

Returns the trace of the currently executing generator.

예제

Example #1 ReflectionGenerator::getTrace() example

<?php
function foo() {
    yield 1;
}

function bar()
{
    yield from foo();
}

function baz()
{
    yield from bar();
}

$gen = baz();
$gen->valid(); // start the generator

var_dump((new ReflectionGenerator($gen))->getTrace());

위 예제의 출력 예시:

array(2) {
  [0]=>
  array(4) {
    ["file"]=>
    string(18) "example.php"
    ["line"]=>
    int(8)
    ["function"]=>
    string(3) "foo"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(18) "example.php"
    ["line"]=>
    int(12)
    ["function"]=>
    string(3) "bar"
    ["args"]=>
    array(0) {
    }
  }
}

참고

  • ReflectionGenerator::getFunction() - Gets the function name of the generator
  • ReflectionGenerator::getThis() - Gets the $this value of the generator

User Contributed Notes

There are no user contributed notes for this page.


The Reflector interface

(PHP 5, PHP 7)

소개

Reflector is an interface implemented by all exportable Reflection classes.

클래스 개요

Reflector {
/* 메소드 */
public static string export ( void )
public string __toString ( void )
}

Table of Contents

  • Reflector::export — Exports
  • Reflector::__toString — To string

User Contributed Notes

There are no user contributed notes for this page.


Exports

  • ≪ Reflector
  • Reflector::__toString ≫
  • PHP Manual
  • Reflector
  • Exports

Reflector::export

(PHP 5, PHP 7)

Reflector::export — Exports

설명

public static string Reflector::export ( void )

Exports.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

참고

  • Reflection::__toString()

User Contributed Notes

There are no user contributed notes for this page.


To string

  • ≪ Reflector::export
  • ReflectionException ≫
  • PHP Manual
  • Reflector
  • To string

Reflector::__toString

(PHP 5, PHP 7)

Reflector::__toString — To string

설명

public string Reflector::__toString ( void )

To string.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

참고

  • ReflectionProperty::export() - Export
  • __toString()

User Contributed Notes

There are no user contributed notes for this page.


The ReflectionException class

(PHP 5, PHP 7)

소개

The ReflectionException class.

클래스 개요

ReflectionException extends Exception {
/* 프로퍼티 */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* 상속된 메소드 */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}

User Contributed Notes

There are no user contributed notes for this page.


Variable handling

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • Variable handling 함수 목록
    • boolval — Get the boolean value of a variable
    • debug_zval_dump — Dumps a string representation of an internal zend value to output
    • doubleval — 별칭: floatval
    • empty — 변수가 비어있는지 검사합니다
    • floatval — 변수의 float 값을 얻습니다
    • get_defined_vars — 모든 정의된 변수의 배열을 반환
    • get_resource_type — 자원형을 반환
    • gettype — 변수의 자료형을 얻습니다
    • import_request_variables — GET/POST/쿠키 변수를 전역으로 가져옵니다
    • intval — 변수의 정수값을 얻습니다
    • is_array — 변수가 배열인지 확인
    • is_bool — 변수가 논리형인지 확인
    • is_callable — 변수 내용을 함수처럼 호출할 수 있는지 확인
    • is_double — 별칭: is_float
    • is_float — 변수의 자료형이 소수인지 확인합니다
    • is_int — 변수의 자료형이 정수인지 확인합니다
    • is_integer — 별칭: is_int
    • is_long — 별칭: is_int
    • is_null — 변수가 NULL인지 확인합니다
    • is_numeric — 변수가 수나 수 문자열인지 확인합니다
    • is_object — 변수가 객체인지 확인합니다
    • is_real — 별칭: is_float
    • is_resource — 변수가 자원인지 확인
    • is_scalar — 변수가 스칼라인지 확인
    • is_string — 변수의 자료형이 문자열인지 확인합니다
    • isset — 설정된 변수인지 확인
    • print_r — 변수에 관한 정보를 사람이 읽기 편하게 출력
    • serialize — 값의 저장 표현을 생성
    • settype — 변수의 자료형을 설정
    • strval — 변수의 문자열 값을 얻습니다
    • unserialize — 저장 표현에서 PHP 값을 작성
    • unset — 주어진 변수를 제거
    • var_dump — 변수에 대한 정보를 덤프
    • var_export — 변수를 처리가능한 문자열 표현으로 출력하거나 반환합니다

User Contributed Notes

There are no user contributed notes for this page.


소개

For information on how variables behave, see the Variables entry in the Language Reference section of the manual.

User Contributed Notes

There are no user contributed notes for this page.


실행시 설정

이 함수의 작동은 php.ini 설정에 영향을 받습니다.

변수 설정 옵션
이름 | 기본값 | 변경위치 | 변경점
unserialize_callback_func | NULL | PHP_INI_ALL | PHP 4.2.0부터 사용할 수 있습니다.
PHP_INI_* 모드에 대한 상세와 정의는 Where a configuration setting may be set를 참고하십시오.

위 설정 지시어에 대한 간단한 설명입니다.

unserialize_callback_func string

unserialize() 콜백 함수가 (정의되지 않은 클래스 이름을 인수로) 호출되었을 때, unserializer가 정의되지 않은 클래스를 생성해야 할 수 있습니다. 지정한 함수가 정의되어 있지 않거나, 함수가 존재하지 않는 클래스를 포함/상속하지 못하면 경고를 발생합니다. 콜백 함수가 필요한 경우에만, 이 엔트리를 지정하십시오.

참고: unserialize().

User Contributed Notes

There are no user contributed notes for this page.


Variable handling 함수 목록

Table of Contents

  • boolval — Get the boolean value of a variable
  • debug_zval_dump — Dumps a string representation of an internal zend value to output
  • doubleval — 별칭: floatval
  • empty — 변수가 비어있는지 검사합니다
  • floatval — 변수의 float 값을 얻습니다
  • get_defined_vars — 모든 정의된 변수의 배열을 반환
  • get_resource_type — 자원형을 반환
  • gettype — 변수의 자료형을 얻습니다
  • import_request_variables — GET/POST/쿠키 변수를 전역으로 가져옵니다
  • intval — 변수의 정수값을 얻습니다
  • is_array — 변수가 배열인지 확인
  • is_bool — 변수가 논리형인지 확인
  • is_callable — 변수 내용을 함수처럼 호출할 수 있는지 확인
  • is_double — 별칭: is_float
  • is_float — 변수의 자료형이 소수인지 확인합니다
  • is_int — 변수의 자료형이 정수인지 확인합니다
  • is_integer — 별칭: is_int
  • is_long — 별칭: is_int
  • is_null — 변수가 NULL인지 확인합니다
  • is_numeric — 변수가 수나 수 문자열인지 확인합니다
  • is_object — 변수가 객체인지 확인합니다
  • is_real — 별칭: is_float
  • is_resource — 변수가 자원인지 확인
  • is_scalar — 변수가 스칼라인지 확인
  • is_string — 변수의 자료형이 문자열인지 확인합니다
  • isset — 설정된 변수인지 확인
  • print_r — 변수에 관한 정보를 사람이 읽기 편하게 출력
  • serialize — 값의 저장 표현을 생성
  • settype — 변수의 자료형을 설정
  • strval — 변수의 문자열 값을 얻습니다
  • unserialize — 저장 표현에서 PHP 값을 작성
  • unset — 주어진 변수를 제거
  • var_dump — 변수에 대한 정보를 덤프
  • var_export — 변수를 처리가능한 문자열 표현으로 출력하거나 반환합니다

User Contributed Notes

Kubo2 22-Mar-2015 08:28
If you want to find out the datatype of a variable, use PHP's native gettype() function instead of various custom implementations.
coding-kid at hotmail dot com 03-Apr-2010 05:13
A simple method to check what type input var is:

<code>
class filter{
    /*
     * @var: $types (array): holds all the supported datatypes, their number, and the callback that need to be made
     *                             to check if it's valid.
     */
    private $types = array('string'=> array('number'=> '1', 'callback'=> 'is_string'),
                            'int'=> array('number' => '2', 'callback' => 'is_int'),
                            'array'=> array('number' => '3', 'callback' => 'is_array'),
                            'object'=> array('number' => '4', 'callback' => 'is_object'),
                            'float'=> array('number' => '5', 'callback' => 'is_float'),
                            'bool'=> array('number' => '6', 'callback' => 'is_bool'),
                            'resource'=> array('number' => '7', 'callback' => 'is_resource'),
                            'null'=> array('number' => '8', 'callback' => 'is_null'));

    function __construct(){

    }
    /*
     * @desc: This method checks what type the input variable is
     *
     * @param: $var (mixed): The var that will be checked
     *
     * @param: $output_type (string): How the result will be returned
     *
     * @returns: string or int
     */
    function type($var, $output_type = 'numeric'){

        if(is_string($var)){
            $var_type = array("string", "1");
        } elseif(is_int($var)){
            $var_type = array("int", "2");
        } elseif(is_array($var)){
            $var_type = array("array", "3");
        } elseif(is_object($var)){
            $var_type = array("object", "4");
        } elseif(is_float($var)){
            $var_type = array("float", "5");
        } elseif(is_bool($var)){
            $var_type = array("bool", "6");
        } elseif(is_resource($var)){
            $var_type = array("resource", "7");
        } elseif(is_null($var)){
            $var_type = array("null", "8");
        }
        if($output_type == 'text'){
            return $var_type[0];
        } else{
            return $var_type[1];
        }
    }
}</code>
jfrasca at sheerdev dot com 31-Aug-2005 07:27
I needed a simple function that would reduce any kind of variable to a string or number while retaining some semblance of the data that was stored in the variable. This is what I came up with:

<?
function ReduceVar ($Value) {
    switch (gettype($Value)) {
        case "boolean":
        case "integer":
        case "double":
        case "string":
        case "NULL":
            return $Value;
        case "resource":
            return get_resource_type($Value);
        case "object":
            return ReduceVar(get_object_vars($Value));
        case "array":
            if (count($Value) <= 0)
                return NULL;
            else
                return ReduceVar(reset($Value));
        default:
            return NULL;
    }
}
?>
skelley at diff dot nl 23-Sep-2001 01:55
Sorry to say Mykolas, but your definition would not be correct.

isempty() evaluates to true for NULL, 0, "", false or 'not set' for any variable, object etc. that can be set to a value.

isset() evaluates to true if the variable, object etc. exists at all, whether it is 'empty' or not.

Example:
$foo = 0;
isset($foo); //will evaluate to true.
!empty($foo); //will evaluate to false.

unset($foo);
isset($foo); //will evaluate to false.
!empty($foo); //will evaluate to false.


Get the boolean value of a variable

  • ≪ Variable handling 함수 목록
  • debug_zval_dump ≫
  • PHP Manual
  • Variable handling 함수 목록
  • Get the boolean value of a variable

boolval

(PHP 5 >= 5.5.0, PHP 7)

boolval — Get the boolean value of a variable

설명

boolean boolval ( mixed $var )

Returns the boolean value of var.

인수

var

The scalar value being converted to a boolean.

반환값

The boolean value of var.

예제

Example #1 boolval() examples

<?php
echo '0:        '.(boolval(0) ? 'true' : 'false')."\n";
echo '42:       '.(boolval(42) ? 'true' : 'false')."\n";
echo '0.0:      '.(boolval(0.0) ? 'true' : 'false')."\n";
echo '4.2:      '.(boolval(4.2) ? 'true' : 'false')."\n";
echo '"":       '.(boolval("") ? 'true' : 'false')."\n";
echo '"string": '.(boolval("string") ? 'true' : 'false')."\n";
echo '"0":      '.(boolval("0") ? 'true' : 'false')."\n";
echo '"1":      '.(boolval("1") ? 'true' : 'false')."\n";
echo '[1, 2]:   '.(boolval([1, 2]) ? 'true' : 'false')."\n";
echo '[]:       '.(boolval([]) ? 'true' : 'false')."\n";
echo 'stdClass: '.(boolval(new stdClass) ? 'true' : 'false')."\n";
?>

위 예제의 출력:

0:        false
42:       true
0.0:      false
4.2:      true
"":       false
"string": true
"0":      false
"1":      true
[1, 2]:   true
[]:       false
stdClass: true

참고

  • floatval() - 변수의 float 값을 얻습니다
  • intval() - 변수의 정수값을 얻습니다
  • strval() - 변수의 문자열 값을 얻습니다
  • settype() - 변수의 자료형을 설정
  • is_bool() - 변수가 논리형인지 확인
  • Type juggling

User Contributed Notes

IkkiPhenix 27-Jan-2016 12:16
(sorry for my english)
This function is not necessary.

if ($variable)

       as the same effect than

if (boolval($variable))

at least for integer, float, string, boolean, array types.
raphael.satyago 21-Jan-2015 06:20
function is_true($val, $return_null=false){
    $boolval = ( is_string($val) ? filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) : (bool) $val );
    return ( $boolval===null && !$return_null ? false : $boolval );
}

// Return Values:

is_true(new stdClass);      // true
is_true([1,2]);             // true
is_true([1]);               // true
is_true([0]);               // true
is_true(42);                // true
is_true(-42);               // true
is_true('true');            // true
is_true('on')               // true
is_true('off')              // false
is_true('yes')              // true
is_true('no')               // false
is_true('ja')               // false
is_true('nein')             // false
is_true('1');               // true
is_true(NULL);              // false
is_true(0);                 // false
is_true('false');           // false
is_true('string');          // false
is_true('0.0');             // false
is_true('4.2');             // false
is_true('0');               // false
is_true('');                // false
is_true([]);                // false
Matt M 09-Jan-2015 02:27
To anyone like me who came here looking for a way to turn any value into a 0/1 that will fit into a MySQL boolean (tinyint) field:

<?php
$tinyint = (int) filter_var($valToCheck, FILTER_VALIDATE_BOOLEAN);
?>

tinyint will be 0 (zero) for values like string "false", boolean false, int 0

tinyint will be 1 for values like string "true", boolean true, int 1

Useful if you are accepting data that might be from a language like Javascript that sends string "false" for a boolean false.
Babak Bandpey 13-May-2014 06:14
I believe that the double negation !! can perform the same task with the same result if your PHP is not up2date

var_dump(!!1, !!0, !!"test", !!"");

outputs:
boolean true

boolean false

boolean true

boolean false

May the life be good to you.
Anonymous 31-Dec-2013 02:12
A misspell in v2.0.
:-)

Hack v2.1

<?php

if( ! function_exists('boolval'))
{
    /**
     * Get the boolean value of a variable
     *
     * @param mixed The scalar value being converted to a boolean.
     * @return boolean The boolean value of var.
     */
    function boolval($var)
    {
        return !! $var;
    }
}

?>
info at lomalkin dot ru 15-Mar-2013 08:08
<?

// Hack for old php versions to use boolval()

if (!function_exists('boolval')) {
        function boolval($val) {
                return (bool) $val;
        }
}
?>


Dumps a string representation of an internal zend value to output

  • ≪ boolval
  • doubleval ≫
  • PHP Manual
  • Variable handling 함수 목록
  • Dumps a string representation of an internal zend value to output

debug_zval_dump

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

debug_zval_dump — Dumps a string representation of an internal zend value to output

설명

void debug_zval_dump ( mixed $variable [, mixed $... ] )

Dumps a string representation of an internal zend value to output.

인수

variable

The variable being evaluated.

반환값

값을 반환하지 않습니다.

예제

Example #1 debug_zval_dump() example

<?php
$var1 = 'Hello World';
$var2 = '';

$var2 =& $var1;

debug_zval_dump(&$var1);
?>

위 예제의 출력:

&string(11) "Hello World" refcount(3)

Note: Beware the refcount

The refcount value returned by this function is non-obvious in certain circumstances. For example, a developer might expect the above example to indicate a refcount of 2. The third reference is created when actually calling debug_zval_dump().

This behavior is further compounded when a variable is not passed to debug_zval_dump() by reference. To illustrate, consider a slightly modified version of the above example:

<?php
$var1 = 'Hello World';
$var2 = '';

$var2 =& $var1;

debug_zval_dump($var1); // not passed by reference, this time
?>

위 예제의 출력:

string(11) "Hello World" refcount(1)

Why refcount(1)? Because a copy of $var1 is being made, when the function is called.

This function becomes even more confusing when a variable with a refcount of 1 is passed (by copy/value):

<?php
$var1 = 'Hello World';

debug_zval_dump($var1);
?>

위 예제의 출력:

string(11) "Hello World" refcount(2)

A refcount of 2, here, is extremely non-obvious. Especially considering the above examples. So what's happening?

When a variable has a single reference (as did $var1 before it was used as an argument to debug_zval_dump()), PHP's engine optimizes the manner in which it is passed to a function. Internally, PHP treats $var1 like a reference (in that the refcount is increased for the scope of this function), with the caveat that if the passed reference happens to be written to, a copy is made, but only at the moment of writing. This is known as "copy on write."

So, if debug_zval_dump() happened to write to its sole parameter (and it doesn't), then a copy would be made. Until then, the parameter remains a reference, causing the refcount to be incremented to 2 for the scope of the function call.

참고

  • var_dump() - 변수에 대한 정보를 덤프
  • debug_backtrace() - Generates a backtrace
  • References Explained
  • » References Explained (by Derick Rethans)

User Contributed Notes

Anonymous 18-Apr-2016 06:09
"Fatal error: Call-time pass-by-reference has been removed ..." since PHP 5.something. So the Example #1 is now invalid PHP code.
AmberBlackthorn 23-Oct-2010 02:35
The add of "Call-time pass-by-reference" as E_DEPRECATED makes it impossible to get the real refcount without getting an error, since <?php error_reporting(E_ALL ^ E_DEPRECATED); ?> and even <?php @debug_zval_dump(&$foo); ?> doesn't change anything.


별칭: floatval

  • ≪ debug_zval_dump
  • empty ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 별칭: floatval

doubleval

(PHP 4, PHP 5, PHP 7)

doubleval — 별칭: floatval()

설명

이 함수는 다음 함수의 별칭입니다: floatval().

변경점

버전 | 설명
4.2.0 | doubleval()은 floatval()의 별칭이 되었습니다. 이전에는 doubleval()만 존재했습니다.

User Contributed Notes

There are no user contributed notes for this page.


변수가 비어있는지 검사합니다

  • ≪ doubleval
  • floatval ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수가 비어있는지 검사합니다

empty

(PHP 4, PHP 5, PHP 7)

empty — 변수가 비어있는지 검사합니다

설명

bool empty ( mixed $var )

변수가 비어 있는 것으로 간주되는지 검사합니다.

인수

var

확인할 변수.

Note:

empty()은 변수만 확인하며, 다른 것은 처리 오류를 발생합니다. 다시 말하면, 다음 문장은 실행되지 않습니다: empty(trim($name))

empty()는 변수값이 지정되지 않았을 때 경고를 출력하지 않는 것을 제외하고, (boolean) var의 반대입니다.

반환값

var가 비어 있지 않고, 0이 아닌 값일 경우 FALSE를 반환합니다.

다음 것들이 비어 있는 것으로 간주됩니다:

  • "" (빈 문자열)
  • 0 (정수 0)
  • "0" (문자열 0)
  • NULL
  • FALSE
  • array() (빈 배열)
  • var $var; (클래스 안에서 값 없이 선언된 변수)

변경점

버전 | 설명
PHP 5 | PHP 5부터, 프로퍼티가 없는 객체는 빈 것으로 간주되지 않습니다.
PHP 4 | PHP 4부터, 문자열 "0"은 빈 것으로 간주합니다.

예제

Example #1 간단한 empty() / isset() 비교입니다.

<?php
$var = 0;

// $var가 비어있기 때문에 true로 판단합니다.
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// $var가 지정되어 있기 때문에 true로 판단합니다.
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>

주의

Note: 이것은 함수가 아닌 언어 구조이기 때문에, 가변 함수 방식으로 호출할 수 없습니다.

참고

  • isset() - 설정된 변수인지 확인
  • unset() - 주어진 변수를 제거
  • array_key_exists() - 주어진 키와 인덱스가 배열에 존재하는지 확인
  • count() - 배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다
  • strlen() - 문자열 길이를 얻습니다
  • 자료형 비교표

User Contributed Notes

mcfogw at gmail dot com 08-May-2016 11:40
I'm comparing behavior of `!` and `empty()`, find an undocumented behavior here.

just like cast-to-boolean, `empty()` cares about if SimpleXML object is made from emty tags.

`empty(simplexml_load_string('<root />'))` is TRUE

this behavior begins from php 5.1.0~7, in 5.0.x that's FALSE

proof here => https://3v4l.org/74Tc4
Javier Alfonso 11-Mar-2016 10:05
If you test an element of an array (like $_POST['key]), it test if the key doesn't exist or if it exist if its value is empty and never emit a warning.

For who don't want to test what happen if passed an array element here is my test and result.

<?php
$a = array();
$b = array('key');
$c = array('key' => false);
$d = array('key' => 'La verdad nos hace libres');

echo (empty($a['key'])?'A empty':'A not empty'), ' - ', (empty($b['key'])?'B empty':'B not empty'), ' - ', (empty($c['key'])?'C empty':'C not empty'), ' - ', (empty($d['key'])?'D empty':'D not empty');
?>

And the result is:

    A empty - B empty - C empty - D not empty
jeanbadel at gmail dot com 08-Apr-2015 11:54
Add a empty recursive

function is_empty($field) {
    return array_reduce($field, function($acc, $value){
        if ($acc === false) {
            return false;
        }

        return (is_array($value)) ? is_empty($value) : empty($value);
    });
}
your dot brother dot t at hotmail dot com 26-Dec-2014 06:38
(experienced in PHP 5.6.3) The `empty()` can't evaluate `__get()` results explicitly, so the `empty()` statement bellow always renders true
<?php
class Juice extends Liquid{
   protected $apple;
   protected $orange;
   public function __get($name) {
      return $this->$name;
   }
   public function __construct($apple, $orange) {
      $this->apple = $apple;
      $this->orange = $orange;
   }
}

class Glass {
   protected $liquid;
   public function __get($name) {
      return $name == "liquid" ? $this->liquid : false;
   }
   public function __construct() {
      $this->juice = new Juice(3, 5);
   }
}

$glass = new Glass();
var_dump(empty($this->liquid->apple));

/**
 * The output is:
 * bool(true)
 */
?>

The correct way is to force the evaluation of `__get()` first, by using extra braces around implicit statements like this:
<?php
var_dump(empty(($this->liquid->apple)));

/**
 * The output is:
 * bool(false)
 */
?>

So if you are using packages that utilize object oriented designs and magic methods like `__get()`, it's a good practice to always use double braces for `empty()` calls.
chrisdmiddleton at gmail dot com 10-Sep-2014 05:37
If you want to use empty() to evaluate an expression (not a variable), and you don't have PHP 5.5+, you can do it by wrapping the call to empty in a function, like so:
<?php
function is_empty($var) {

    return empty($var);

}
?>
Then you can do something like
<?php
if(is_empty(NULL)) {
 /* ... */
}
?>
without issue, since the local variable $var is being tested rather than the expression in the function call itself.
turabgarip at gmail dot com 09-May-2014 01:16
Checking if a variable is empty or not when you have more than one will add so many similar lines to your code; and will require re-writing of course. Like for example;

<?php
if (!empty($a))
    $myvar = $a;
elseif (!empty($b))
     $myvar = $b;
//...
?>

If, for example $myvar should be pulled among 5 vars, you need to write it for 5 times. But in the below example, using $myvar = esor($var1, $var2, ...) will set it to first non-empty var.

<?php

function esor()
{
    $arg_num = func_num_args();
    // "No arguments" is empty string
    if (!$arg_num) return '';
    $args = func_get_args();
    for ($i = 0; $i < $arg_num; $i++)
        if (!empty($args[$i]))
            return $args[$i];
    // Not found any filled var?
    return ''; // Empty string is what you get
}

// For example;
mail(esor($settings['admin_mail'], $_ENV['SERVER_ADMIN']), 'Something went wrong', 'Error was foo');

?>

This will make it "one line code" no matter how many vars you have to check. Hope that helps.
Nanhe Kumar 31-Jan-2014 07:17
<?php
/**
* @author :  Nanhe Kumar <nanhe.kumar@gmail.com>
* List of all empty values
**/

$testCase = array(
    1 => '',
    2 => "",
    3 => null,
    4 => array(),
    5 => FALSE,
    6 => NULL,
    7=>'0',
    8=>0,

);

foreach ($testCase as $k => $v) {
    if (empty($v)) {
        echo "<br> $k=>$v is empty";
    }
}
/**
Output
1=> is empty
2=> is empty
3=> is empty
4=>Array is empty
5=> is empty
6=> is empty
7=>0 is empty
8=>0 is empty
**/
?>
bharris at nospam dot epic dot com 12-Nov-2013 05:59
XML::Unserializer produces unexpected results because empty($key) is true when $key = 0.

Example:

<?php
include("/usr/share/php/XML/Unserializer.php");
$xml= <<<EOD
<?xml version="1.0"?>
<groups _type="array">
    <XML_Serializer_Tag _originalKey="0" _type="array">
        <gid _type="integer">10</gid>
    </XML_Serializer_Tag>
    <XML_Serializer_Tag _originalKey="5" _type="array">
        <gid _type="integer">100</gid>
    </XML_Serializer_Tag>
</groups>
EOD;
$uns = new XML_Unserializer();
$res = $uns->unserialize($xml);
$recs = $uns->getUnserializedData();
print_r($recs);
?>

produces

Array
(
    [0] => Array
        (
            [gid] => 10
        )

    [5] => Array
        (
            [gid] => 100
        )
)

while

<?php
include("/usr/share/php/XML/Unserializer.php");
$xml= <<<EOD
<?xml version="1.0"?>
<groups _type="array">
    <XML_Serializer_Tag _originalKey="5" _type="array">
        <gid _type="integer">100</gid>
    </XML_Serializer_Tag>
    <XML_Serializer_Tag _originalKey="0" _type="array">
        <gid _type="integer">10</gid>
    </XML_Serializer_Tag>
</groups>
EOD;
$uns = new XML_Unserializer();
$res = $uns->unserialize($xml);
$recs = $uns->getUnserializedData();
print_r($recs);
?>

produces

Array
(
    [1] => Array
        (
            [gid] => 100
        )

    [6] => Array
        (
            [gid] => 10
        )
)

This is because the "empty" key value of 0 results in a push onto the end of the array, rather than an insertion at key position 0.
martin dot aarhof at gmail dot com 15-Mar-2012 05:38
<?php
$str = '            ';
var_dump(empty($str)); // boolean false
?>

So remember to trim your strings first!

<?php
$str = '        ';
$str = trim($str);
var_dump(empty($str)); // boolean true
?>
qeremy 08-Mar-2012 12:33
Simple solution for: "Fatal error: Can't use function return value in write context in ..."

<?php
function _empty($val) { return empty($val); }
?>
chris dot wisefool at gmail dot com 09-Sep-2011 09:44
Note that checking the existence of a subkey of an array when that subkey does not exist but the parent does and is a string will return false for empty.

i.e.

<?php
$params = array('search'=>'1');
empty($params['search']['filter']); # returns false
?>

This is correct, per the documentation (http://php.net/manual/en/language.types.string.php); quite a bit down the page is the Warning: "Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer." ) I didn't receive a warning but perhaps that's correct too...depends on whether the string -> integer conversion is considered "illegal": "Illegal offset type emits E_NOTICE."

(i.e. since $params['search'] is a string, the 'filter' subscript is converted to 0, so the test becomes empty($params['search'][0]), which is obviously false), but it tripped me up enough to mistakenly file a bug report (which I've since closed).
steven at nevvix dot com 02-May-2011 06:16
When you need to accept these as valid, non-empty values:
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)

<?php
function is_blank($value) {
    return empty($value) && !is_numeric($value);
}
?>

This is similar to Rails' blank? method.
phpsort 21-Jan-2011 08:13
I'm summarising a few points on empty() with inaccessible properties, in the hope of saving others a bit of time. Using PHP 5.3.2.
<?php
class MyClass {
    private $foo = 'foo';
}
$myClass = new MyClass;
echo $myClass->foo;
?>
As expected, this gives "Fatal error: Cannot access private property MyClass::$foo".
But substitute the line
if (empty($myClass->foo)) echo 'foo is empty'; else echo 'foo is not empty';
and we get the misleading result "foo is empty".
There is NO ERROR OR WARNING, so this is a real gotcha. Your code will just go wrong silently, and I would say it amounts to a bug.
If you add two magic functions to the class:
public function __get($var) { return $this->$var; }
public function __isset($var) { return isset($this->$var); }
then we get the expected result. You need both functions.
For empty($myClass->foo), I believe PHP calls __isset, and if that is true returns the result of empty on the result of __get. (Some earlier posts wrongly suggest PHP just returns the negation of __isset).
BUT ...
See the earlier post by php at lanar dot com. I confirm those results, and if you extend the test with isset($x->a->b->c) it appears that __isset is only called for the last property in the chain. Arguably another bug. empty() behaves in the same way. So things are not as clear as we might hope.
See also the note on empty() at
http://uk3.php.net/manual/en/language.oop5.overloading.php
Clear as mud!
christian dot achatz at adventure-php-framework dot org 17-Jan-2011 09:54
Since this special case is not mentioned already: the empty() check does not work for mysqli resources / connections stored within a private variable.

Instead of

if(!empty($this->dbConn->error)){...}

you have to use

$error = $this->dbConn->error;
if(!empty($error)){...}

otherwise the condition will not evaluate to true in case a mysqli error is given for the present connection.
e dot klerks at i-bytes dot nl 08-Nov-2010 03:16
To make an empty function, which only accepts arrays, one can use type-hinting:

<?php
// emptyArray :: [a] -> Bool

function emptyArray(array $xs){
 return empty($xs);
}
?>

Type hinting is a good thing to use in your code, because it makes it more easy to reason about your code. Besides that, it automatically documents the code.
serkons at yahoo dot com 28-Oct-2010 11:01
Hi you can check the status of multiple array or any variable is empty with below code.

<?php
$microtimeref = microtime ( true );
//$variable=null; // false,true,0,''
//$variable = array ('id' => 10, 'name' => 'serkon' );
$variable = array (array (0) );
echo '<pre>';
function getArray($dizi) {
    foreach ( $dizi as $value )
        return $value;
}
function isEmpty($array) {
    if (is_array ( $array )) {
        $dizi = getArray ( $array );
        if (is_array ( $dizi ))
            $ref = isEmpty ( $dizi );
        else
            if (strlen ( $dizi ) >= 1)
                return false;
            else
                return true;
    }
    else
        if (strlen ( $array ) >= 1)
            return false;
        else
            return true;

    if ($ref === false)
        return false;
    else
        return true;
}
$sonuc = isEmpty ( $variable );
var_dump ( $sonuc );
echo "Total time: <b>" . round ( microtime ( true ) - $microtimeref, 4 ) . 's</b><br />';
echo '</pre>';
?>

Response:

bool(false)  // not empty
Total time: 0s
rodolphe dot bodeau at free dot fr 25-Oct-2010 03:37
Be careful, if "0" (zero as a string), 0 (zero as an integer) and -0 (minus zero as an integer) return true, "-0" (minus zero as a string (yes, I already had some customers that wrote -0 into a form field)) returns false. You need to cast your variable before testing it with the empty() function :

<?php
$var = "-0";
echo empty($var);  // returns false
$var = (int) $var; // casts $var as an integer
echo empty($vat);  // returns true
?>
ehsmeng 21-Sep-2010 02:25
I can't use empty() in all situations because '0' is usually not considered empty to me. I did a quick benchmark over the most common ways of testing it. '' == var suffers from '' == 0 is true so that's just there for curiosity.

<?php
    $microtimeref = microtime(true);
    $a = 0;
    $b = 'asd';
    for ($i = 0; $i < 5000000; $i++)
    {
        if (0 == mb_strlen ($b))
        {
            $a++;
        }
    }
    echo "Total time 0 == mb_strlen(var): <b>" . round(microtime(true) - $microtimeref,3) . 's</b><br />';
?>

The results:

Total time 0 == mb_strlen(var): 3.141s
Total time 0 === strlen(var): 2.904s
Total time 0 == strlen(var): 2.878s
Total time '' == var: 1.774s
Total time '' === var: 1.706s
Total time empty(var): 1.496s

Thus '' === var will be my zero length string test.
marko dot crni at gmail dot com 16-Sep-2010 08:06
Calling non existing object property, empty($object->prop), will trigger __isset(), the same way as isset($object->prop) does, but there is one difference. If __isset() returns TRUE, another call to __get() will be made and actual return value will be result of empty() and result of __get().
aidan1103 at yahoo dot com 26-Mar-2010 07:11
empty() should not necessarily return the negation of the __isset() magic function result, if you set a data member to 0, isset() should return true and empty should also return true.  A simpler implementation of the __isset magic function would be:

public function __isset($key) {
  return isset($this->{$key});
}

I don't understand why this isn't included in stdClass and inherited by default.
Janci 24-Aug-2009 05:57
Please note that results of empty() when called on non-existing / non-public variables of a class are a bit confusing if using magic method __get (as previously mentioned by nahpeps at gmx dot de). Consider this example:

<?php
class Registry
{
    protected $_items = array();
    public function __set($key, $value)
    {
        $this->_items[$key] = $value;
    }
    public function __get($key)
    {
        if (isset($this->_items[$key])) {
            return $this->_items[$key];
        } else {
            return null;
        }
    }
}

$registry = new Registry();
$registry->empty = '';
$registry->notEmpty = 'not empty';

var_dump(empty($registry->notExisting)); // true, so far so good
var_dump(empty($registry->empty)); // true, so far so good
var_dump(empty($registry->notEmpty)); // true, .. say what?
$tmp = $registry->notEmpty;
var_dump(empty($tmp)); // false as expected
?>

The result for empty($registry->notEmpty) is a bit unexpeced as the value is obviously set and non-empty. This is due to the fact that the empty() function uses __isset() magic functin in these cases. Although it's noted in the documentation above, I think it's worth mentioning in more detail as the behaviour is not straightforward. In order to achieve desired (expexted?) results, you need to add  __isset() magic function to your class:

<?php
class Registry
{
    protected $_items = array();
    public function __set($key, $value)
    {
        $this->_items[$key] = $value;
    }
    public function __get($key)
    {
        if (isset($this->_items[$key])) {
            return $this->_items[$key];
        } else {
            return null;
        }
    }
    public function __isset($key)
    {
        if (isset($this->_items[$key])) {
            return (false === empty($this->_items[$key]));
        } else {
            return null;
        }
    }
}

$registry = new Registry();
$registry->empty = '';
$registry->notEmpty = 'not empty';

var_dump(empty($registry->notExisting)); // true, so far so good
var_dump(empty($registry->empty)); // true, so far so good
var_dump(empty($registry->notEmpty)); // false, finally!
?>

It actually seems that empty() is returning negation of the __isset() magic function result, hence the negation of the empty() result in the __isset() function above.
denobasis-bozic et yahoo.com 18-Jul-2009 01:54
test if all multiarray's are empty

<?php
function is_multiArrayEmpty($multiarray) {
    if(is_array($multiarray) and !empty($multiarray)){
        $tmp = array_shift($multiarray);
            if(!is_multiArrayEmpty($multiarray) or !is_multiArrayEmpty($tmp)){
                return false;
            }
            return true;
    }
    if(empty($multiarray)){
        return true;
    }
    return false;
}

$testCase = array (
0 => '',
1 => "",
2 => null,
3 => array(),
4 => array(array()),
5 => array(array(array(array(array())))),
6 => array(array(), array(), array(), array(), array()),
7 => array(array(array(), array()), array(array(array(array(array(array(), array())))))),
8 => array(null),
9 => 'not empty',
10 => "not empty",
11 => array(array("not empty")),
12 => array(array(),array("not empty"),array(array()))
);

foreach ($testCase as $key => $case ) {
    echo "$key is_multiArrayEmpty= ".is_multiArrayEmpty($case)."<br>";
}
?>

OUTPUT:
========

0 is_multiArrayEmpty= 1
1 is_multiArrayEmpty= 1
2 is_multiArrayEmpty= 1
3 is_multiArrayEmpty= 1
4 is_multiArrayEmpty= 1
5 is_multiArrayEmpty= 1
6 is_multiArrayEmpty= 1
7 is_multiArrayEmpty= 1
8 is_multiArrayEmpty= 1
9 is_multiArrayEmpty=
10 is_multiArrayEmpty=
11 is_multiArrayEmpty=
12 is_multiArrayEmpty=
mlibazisi mabandla 28-May-2009 11:47
in cases when "0" is not intended to be empty, here is a simple function to safely test for an empty string (or mixed variable):

<?php
function _empty($string){
     $string = trim($string);
     if(!is_numeric($string)) return empty($string);
     return FALSE;
}
?>
emperoruk at dontspam dot hotmail dot com 08-May-2009 05:07
When using the php empty() function to check submitted variables such as $_POST or $_GET, be careful to remember that values 0 (integer) and "0" (string with zero character) are all considered empty. eg. in a simple cms a page ID of zero might be used to indicate that the homepage should be displayed but using the following code:

<?php
if (isset($_GET['pid'] && !empty($_GET['pid']) {
  // assign value to local variable
  $pageID = $_GET['pid'];
} else {
  echo "missing variable 'pageID'";
}
?>

When attempting to display the homepage using a pid of zero the above code will fail.

So as a result i wrote a small function to replace the php empty() function in situations where you want 0 and "0" not to be considered empty.

<?php
function is_empty($var, $allow_false = false, $allow_ws = false) {
    if (!isset($var) || is_null($var) || ($allow_ws == false && trim($var) == "" && !is_bool($var)) || ($allow_false === false && is_bool($var) && $var === false) || (is_array($var) && empty($var))) {
        return true;
    } else {
        return false;
    }
}
?>

This function will allow you to test a variable is empty and considers the following values as empty:

an unset variable -> empty
null -> empty
0 -> NOT empty
"0" -> NOT empty
false -> empty
true -> NOT empty
'string value' -> NOT empty
"    " (white space) -> empty
array() (empty array) -> empty

There are two optional parameters:

$allow_false: setting this to true will make the function consider a boolean value of false as NOT empty. This parameter is false by default.

$allow_ws: setting this to true will make the function consider a string with nothing but white space as NOT empty. This parameter is false by default.

In Testing:

<?php
// an unset variable
echo 'unset variable ($notset) - Empty: ';
echo is_empty($notset) ? 'yes<br />' : 'no<br />';
// NULL variable
echo 'null - Empty: ';
$var = null;
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// integer 0
echo '0 - Empty: ';
$var = 0;
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// string "0"
echo 'string "0" - Empty: ';
$var = "0";
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// boolean value false
echo 'false - Empty: ';
$var = false;
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// allow boolean value false
echo 'false ($allow_false = true) - Empty: ';
$var = false;
echo is_empty($var, true) ? 'yes<br />' : 'no<br />';
// boolean value true
echo 'true - Empty: ';
$var = true;
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// string
echo 'string "foo" - Empty: ';
$var = "foo";
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// white space
echo 'white space "     " - Empty: ';
$var = "    ";
echo is_empty($var) ? 'yes<br />' : 'no<br />';
// allow white space
echo 'white space ($allow_ws = true) "     " - Empty: ';
$var = "    ";
echo is_empty($var, false, true) ? 'yes<br />' : 'no<br />';
// empty array
echo 'empty array - Empty: ';
$var = array();
echo is_empty($var) ? 'yes<br />' : 'no<br />';
?>

the above code outputs the following:

unset variable ($notset) - Empty: yes
null - Empty: yes
0 - Empty: no
string "0" - Empty: no
false - Empty: yes
false ($allow_false = true) - Empty: no
true - Empty: no
string "foo" - Empty: no
white space " " - Empty: yes
white space ($allow_ws = true) " " - Empty: no
empty array - Empty: yes

Hope this code is useful for someone.

Michael
contato at andersonfraga dot net 10-Feb-2009 08:24
<?php

function _empty() {
    foreach(func_get_args() as $args) {
        if( !is_numeric($args) ) {
            if( is_array($args) ) { // Is array?
                if( count($args, 1) < 1 ) return true;
            }
            elseif(!isset($args) || strlen(trim($args)) == 0)
                return true;
            }
        }
    }
    return false;
}

?>
thomas at thomasnoest dot nl 28-Nov-2008 10:54
Note on the selfmade empty function below:

function_exists() returns false on language constructs and empty is a language construct.
Anonymous 20-Aug-2008 11:41
To add on to what anon said, what's happening in john_jian's example seems unusual because we don't see the implicit typecasting going on behind the scenes.  What's really happening is:

$a = '';
$b = 0;
$c = '0';

(int)$a == $b -> true, because any string that's not a number gets converted to 0
$b==(int)$c -> true, because the int in the string gets converted
and
$a==$c -> false, because they're being compared as strings, rather than integers.  (int)$a==(int)$c should return true, however.

Note: I don't remember if PHP even *has* typecasting, much less if this is the correct syntax.  I'm just using something for the sake of examples.
tom at tomwardrop dot com 22-May-2008 02:01
In reply to "admin at ninthcircuit dot info",

Using str_replace is unnecessary. I would encourage the use of trim which would most likely be faster (haven't tested) and easier. Trim also takes care of other white space like line breaks and tabs. Actually, in most of the applications I code, I use a multi-dimensional array map function with trim on the Super Globals such as $_POST, $_GET and $_COOKIE as so far, there hasn't been an instance where I would want any user input to begin or end with whitespace. The good thing about doing this is that you never have to worry about 'trimming' your input which makes your code easier and more reliable (incase you forget to trim some input).
Greg Hartwig 02-May-2008 11:55
David from CodeXplorer:
>> The ONLY reason to use empty() is for code readability. It is the same as an IF/ELSE check.
>> So, don't bother using EMPTY in the real world.

This is NOT true.  empty() will not generate warnings if you're testing against an undefined variable as a simple boolean check will.  On production systems, warnings are usually shut off, but they are often active on development systems.

You could test a flag with
   <?php if ($flagvar)  ... ?>
but this can generate a warning if $flagvar is not set.

Instead of
   <?php if (isset($flagvar) && $flagvar)  ... ?>
you can simply use
   <?php if (!empty($flagvar))  ... ?>

for easy readability without warnings.
David from CodeXplorer 11-Mar-2008 05:29
Mad Hampster did  his test wrong. empty is NOT faster than a simple boolean check. The ONLY reason to use empty() is for code readability. It is the same as an IF/ELSE check. But if you are dealing with intermediate or higher level coders this function has no other benefit.

So, don't bother using EMPTY in the real world.

I ran an array with 5000 simple true/false values through four checks (both types twice) in case of any gain one type might have by going first. These are my results generated one one page request. (PHP5)

0.015328 Time EMPTY
0.014281 Time IF/ELSE
0.015239 Time EMPTY
0.013404 Time IF/ELSE

The page was accessed a couple times to reduce caching effects.
Andrea Giammarchi 31-Jan-2008 04:34
In addiction to Ed comment:
http://uk.php.net/manual/en/function.empty.php#80106

if an instance variable is assigned with an empty value, i.e. false, empty returns true.

<?php
class TestEmpty{
    protected           $empty;
    public  function    __construct(){
        var_dump(empty($this->empty)); // true
        $this->empty = false;
        var_dump(empty($this->empty)); // true
    }
}
new TestEmpty;
?>

I think this is an expected behaviour but at the same time the note about classes variables is too ambiguous.

''var $var; (a variable declared, but without a value in a class)''

Please change them into something like:
''var $var; (a variable undeclared or declared with an empty value in a class)''
jay at w3prodigy dot com 28-Jan-2008 07:59
Also note, that if you have a URI that looks like this:

/page/index.php?query=

performing isset($_GET['query']) will return TRUE. as query is set, though null, in the QUERY.

To counteract this behavior, check isset($_GET['query']) and !empty($_GET['query']) as empty will detect the null value.
Ed 29-Dec-2007 09:08
Also, it doesn't appear to mention in the documentation, if a variable hasn't previously been declared, empty also returns true.

E.g.
var $bar;
empty( $bar ); // declared variable returns true.
empty( $foo ); // undeclared variable also returns true.

The closest the documentation comes to saying this is:
"var $var; (a variable declared, but without a value in a class)"
which isn't really the same, as the variable doesn't necessarily have to be declared first.
MaD HamsteR 12-Dec-2007 04:33
SAME RESULT! But somehow using empty() function is faster for about 10-13%

<?php

$array[] = "";
$array[] = '';
$array[] = 0;
$array[] = "0";
$array[] = NULL;
$array[] = false;
$array[] = array();
$array[] = $var;

foreach($array as $value){
    echo (!empty($value))? 'Not empty!' : 'Empty!';
    echo '<br />'."\r\n";
}

echo '<br />'."\r\n";

foreach($array as $value){
    echo ($value)? 'Not empty!' : 'Empty!';
    echo '<br />'."\r\n";
}

?>
EllisGL 04-Oct-2007 11:48
Here's what I do for the zero issue issue:
if($val == '' && $val !== 0 && $val !== '0')
Antone Roundy 15-Sep-2007 03:55
There's a faster and easier to write method than (isset($a) && strlen($a)) -- isset($a{0}). It evaluates to false if $a is not set or if it has zero length (ie. it's first character is not set). My tests indicate that it's about 33% faster.
rkulla2 at gmail dot com 06-Sep-2007 12:57
Since I didn't like how empty() considers 0 and "0" to be empty (which can easily lead to bugs in your code), and since it doesn't deal with whitespace, i created the following function:

<?php
function check_not_empty($s, $include_whitespace = false)
{
    if ($include_whitespace) {
        // make it so strings containing white space are treated as empty too
        $s = trim($s);
    }
    return (isset($s) && strlen($s)); // var is set and not an empty string ''
}
?>

Instead of saying if (!empty($var)) { // it's not empty } you can just say if (check_not_empty($var)) { // it's not empty }.

If you want strings that only contain whitespace (such as tabs or spaces) to be treated as empty then do: check_not_empty($var, 1)

If you want to check if a string IS empty then do: !check_not_empty($var).

So, whenever you want to check if a form field both exists and contains a value just do: if (check_not_empty($_POST['foo'], 1))

no need to do if (isset() && !empty()) anymore =]
florian.sonner [at] t-online.de 11-Jun-2007 09:06
Since a few people here mentioned that empty will not work with magic-overloading ("__get($var)"):

empty(..) goes the same way as isset(..) do, to check if a property exists. Thus you have to override the magic-function __isset($var) to produce correct results for empty(..) in combination with a magic-overloaded property.
nobody at example dot com 28-Feb-2006 09:06
Re: inerte is my gmail.com username's comment:

While that may be true, those two statements (empty($var), $var == '') are NOT the same. When programming for web interfaces, where a user may be submitting '0' as a valid field value, you should not be using empty().

<?php
    $str = '0';

    // outputs 'empty'
    echo empty($str) ? 'empty' : 'not empty';

    // outputs 'not empty'
    echo $str == '' ? 'empty' : 'not empty';
?>
nahpeps at gmx dot de 19-Aug-2005 12:14
When using empty() on an object variable that is provided by the __get function, empty() will always return true.

For example:

<?php
class foo {

   public function __get($var) {
      if ($var == "bar") {
         return "bar";
      }
   }
}
$object_foo = new foo();
echo '$object_foo->bar is ' . $object_foo->bar;
if (empty($object_foo->bar)) {
   echo '$object_foo->bar seems to be empty';
}
?>

produces the following output:
$object_foo->bar is bar
$object_foo->bar seems to be empty
jmarbas at hotmail dot com 01-Jul-2005 06:10
empty($var) will return TRUE if $var is empty (according to the definition of 'empty' above) AND if $var is not set.

I know that the statement in the "Return Values" section of the manual already says this in reverse:

"Returns FALSE if var has a non-empty and non-zero value."

but I was like "Why is this thing returning TRUE for unset variables???"... oh i see now... Its supposed to return TRUE for unset variables!!!

<?php
  ini_set('error_reporting',E_ALL);
  ini_set('display_errors','1');
  empty($var);
?>
paul at worldwithoutwalls dot co dot uk 22-May-2004 07:09
Note the exceptions when it comes to decimal numbers:

<?php
$a = 0.00;
$b = '0.00';
echo (empty($a)? "empty": "not empty"); //result empty
echo (empty($b)? "empty": "not empty"); //result not empty
//BUT...
$c = intval($b);
echo (empty($c)? "empty": "not empty"); //result empty
?>

For those of you using MySQL, if you have a table with a column of decimal type, when you do a SELECT, your data will be returned as a string, so you'll need to do apply intval() before testing for empty.

e.g.
TABLE t has columns id MEDIUMINT and d DECIMAL(4,2)
and contains 1 row where id=1, d=0.00
<?php
$q = "SELECT * FROM t";
$res = mysql_query($q);
$row = mysql_fetch_assoc($res);
echo (empty($row['d'])? "empty": "not empty"); //result not empty
?>


변수의 float 값을 얻습니다

  • ≪ empty
  • get_defined_vars ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수의 float 값을 얻습니다

floatval

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

floatval — 변수의 float 값을 얻습니다

설명

float floatval ( mixed $var )

var의 float값을 얻습니다.

인수

var

모든 스칼라형. floatval()에 배열이나 객체를 사용할 수 없습니다.

반환값

주어진 변수의 float 값.

예제

Example #1 floatval() 예제

<?php
$var = '122.34343The';
$float_value_of_var = floatval ($var);
echo $float_value_of_var; // 122.34343
?>

참고

  • intval() - 변수의 정수값을 얻습니다
  • strval() - 변수의 문자열 값을 얻습니다
  • settype() - 변수의 자료형을 설정
  • 자료형 조절

User Contributed Notes

movila dot e dot v at gmail dot com 05-Jul-2016 05:05
<?php
       $price = '1.299,00 EUR';
    //$price = 'EUR 1.299,00';
    //$price = '$1,745.09';
    //$price = '$14';
    //$price = '$.14';

    function floatValue($str){
        if(preg_match("/([0-9\.,-]+)/", $str, $match)){
            $value = $match[0];
            if( preg_match("/(\.\d{1,2})$/", $value, $dot_delim) ){
                $value = (float)str_replace(',', '', $value);
            }
            else if( preg_match("/(,\d{1,2})$/", $value, $comma_delim) ){
                $value = str_replace('.', '', $value);
                $value = (float)str_replace(',', '.', $value);
            }
            else
                $value = (int)$value;
        }
        else {
            $value = 0;
        }
        return $value;
    }

    echo floatValue($price);
/*
    1.299,00
    1.299,00
    1,745.09
    14
    0.14
*/
?>
Alexey M 23-Mar-2016 11:28
There is much easier way to deal with formatted numbers:

<?php
$str = '13,232.95';
$var = (double)filter_var($str, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
var_dump($var);
?>
double(13232.95)
zfcb13 at gmail dot com 24-Oct-2015 07:18
More elegant function with selection of decimal point (deafault ,):
<?php
function floatvaldec($v, $dec=',') { return floatval(ereg_replace("," , "." , ereg_replace("[^-0-9$dec]","",$v))); }

// examples:
echo '<br>'.floatvaldec('somthing123.456.789,12Euro') ;
echo '<br>'.floatvaldec('x123,456 789.12 Euro', '.') ;
echo '<br>'.floatvaldec('123.456 789,12$') ;
?>
brewal dot renault at gmail dot com 26-Feb-2014 06:25
This function takes the last comma or dot (if any) to make a clean float, ignoring thousand separator, currency or any other letter :

function tofloat($num) {
    $dotPos = strrpos($num, '.');
    $commaPos = strrpos($num, ',');
    $sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos :
        ((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);

    if (!$sep) {
        return floatval(preg_replace("/[^0-9]/", "", $num));
    }

    return floatval(
        preg_replace("/[^0-9]/", "", substr($num, 0, $sep)) . '.' .
        preg_replace("/[^0-9]/", "", substr($num, $sep+1, strlen($num)))
    );
}

$num = '1.999,369€';
var_dump(tofloat($num)); // float(1999.369)
$otherNum = '126,564,789.33 m²';
var_dump(tofloat($otherNum)); // float(126564789.33)

Demo : http://codepad.org/NW4e9hQH
PapaPinguoin 31-Oct-2011 04:26
To view the very large and very small numbers (eg from a database DECIMAL), without displaying scientific notation, or leading zeros.

FR : Pour afficher les tres grand et tres petits nombres (ex. depuis une base de donnees DECIMAL), sans afficher la notation scientifique, ni les zeros non significatifs.

<?php
function floattostr( $val )
{
    preg_match( "#^([\+\-]|)([0-9]*)(\.([0-9]*?)|)(0*)$#", trim($val), $o );
    return $o[1].sprintf('%d',$o[2]).($o[3]!='.'?$o[3]:'');
}
?>

<?php
echo floattostr("0000000000000001");
echo floattostr("1.00000000000000");
echo floattostr("0.00000000001000");
echo floattostr("0000.00010000000");
echo floattostr("000000010000000000.00000000000010000000000");
echo floattostr("-0000000000000.1");
echo floattostr("-00000001.100000");

// result
// 1
// 1
// 0.00000000001
// 0.0001
// 10000000000.0000000000001
// -0.1
// -1.1

?>
secretr at NOSPAM dot e107 dot org 03-Jan-2011 05:37
setlocale() and floatval() duo could break your DB queries in a very simple way:

<?php
setlocale(LC_ALL, 'bg_BG', 'bgr_BGR');
echo floatval(0.15); // output 0,15
?>

You would need simple workaround like:

<?php
function number2db($value)
{
    $larr = localeconv();
    $search = array(
        $larr['decimal_point'],
        $larr['mon_decimal_point'],
        $larr['thousands_sep'],
        $larr['mon_thousands_sep'],
        $larr['currency_symbol'],
        $larr['int_curr_symbol']
    );
    $replace = array('.', '.', '', '', '', '');

    return str_replace($search, $replace, $value);
}

setlocale(LC_ALL, 'bg_BG', 'bgr_BGR');
$testVal = floatval(0.15); // result 0,15
var_dump($testVal, number2db($testVal));

// Result:
// float(0,15)
// string(4) "0.15"
?>
balai01 at yahoo dot com 13-Oct-2010 12:56
//changes comma to dot if any
//removes any currency symbol and gets the floatVar

$price_fl_point=(preg_replace("/,/",".",$price));
$price_c=floatval(preg_replace("/^[^0-9\.]/","",$price_fl_point));
chris at georgakopoulos dot com 29-Jul-2009 05:12
locale aware floatval:

<?php
function ParseFloat($floatString){
    $LocaleInfo = localeconv();
    $floatString = str_replace($LocaleInfo["mon_thousands_sep"] , "", $floatString);
    $floatString = str_replace($LocaleInfo["mon_decimal_point"] , ".", $floatString);
    return floatval($floatString);
}
?>
ted devito 10-Mar-2009 06:10
i noticed all (well, unless i missed something) the functions working with decimals destroy trailing decimal places. this function restores them in case you want to be able to display a consistent precision for users.

<?php
function decimal($val, $precision = 0) {
    if ((float) $val) :
        $val = round((float) $val, (int) $precision);
        list($a, $b) = explode('.', $val);
        if (strlen($b) < $precision) $b = str_pad($b, $precision, '0', STR_PAD_RIGHT);
        return $precision ? "$a.$b" : $a;
    else : // do whatever you want with values that do not have a float
        return $val;
    endif;
}
?>
info at marc-gutt dot de 27-Aug-2008 12:55
<?php
function floatvalue($value) {
     return floatval(preg_replace('#^([-]*[0-9\.,\' ]+?)((\.|,){1}([0-9-]{1,2}))*$#e', "str_replace(array('.', ',', \"'\", ' '), '', '\\1') . '.\\4'", $value));
}
?>

It is much shorter and able to handle those one, too:
xx,-
xx,--
xx'xxx,xx

After using floatvalue() you can go forward with number_format() as usual.
Michiel 30-Jul-2008 02:21
The last getFloat() function is not completely correct.

1.000.000 and 1,000,000 and its negative variants are not correctly parsed. For the sake of comparing and to make myself clear I use the name parseFloat in stead of getFloat for the new function:

<?php
function parseFloat($ptString) {
            if (strlen($ptString) == 0) {
                    return false;
            }

            $pString = str_replace(" ", "", $ptString);

            if (substr_count($pString, ",") > 1)
                $pString = str_replace(",", "", $pString);

            if (substr_count($pString, ".") > 1)
                $pString = str_replace(".", "", $pString);

            $pregResult = array();

            $commaset = strpos($pString,',');
            if ($commaset === false) {$commaset = -1;}

            $pointset = strpos($pString,'.');
            if ($pointset === false) {$pointset = -1;}

            $pregResultA = array();
            $pregResultB = array();

            if ($pointset < $commaset) {
                preg_match('#(([-]?[0-9]+(\.[0-9])?)+(,[0-9]+)?)#', $pString, $pregResultA);
            }
            preg_match('#(([-]?[0-9]+(,[0-9])?)+(\.[0-9]+)?)#', $pString, $pregResultB);
            if ((isset($pregResultA[0]) && (!isset($pregResultB[0])
                    || strstr($preResultA[0],$pregResultB[0]) == 0
                    || !$pointset))) {
                $numberString = $pregResultA[0];
                $numberString = str_replace('.','',$numberString);
                $numberString = str_replace(',','.',$numberString);
            }
            elseif (isset($pregResultB[0]) && (!isset($pregResultA[0])
                    || strstr($pregResultB[0],$preResultA[0]) == 0
                    || !$commaset)) {
                $numberString = $pregResultB[0];
                $numberString = str_replace(',','',$numberString);
            }
            else {
                return false;
            }
            $result = (float)$numberString;
            return $result;
}
?>

Comparing of float parsing functions with the following function:

<?php
function testFloatParsing() {
    $floatvals = array(
        "22 000,76",
        "22.000,76",
        "22,000.76",
        "22 000",
        "22,000",
        "22.000",
        "22000.76",
        "22000,76",
        "1.022.000,76",
        "1,022,000.76",
        "1,000,000",
        "1.000.000",
        "1022000.76",
        "1022000,76",
        "1022000",
        "0.76",
        "0,76",
        "0.00",
        "0,00",
        "1.00",
        "1,00",
        "-22 000,76",
        "-22.000,76",
        "-22,000.76",
        "-22 000",
        "-22,000",
        "-22.000",
        "-22000.76",
        "-22000,76",
        "-1.022.000,76",
        "-1,022,000.76",
        "-1,000,000",
        "-1.000.000",
        "-1022000.76",
        "-1022000,76",
        "-1022000",
        "-0.76",
        "-0,76",
        "-0.00",
        "-0,00",
        "-1.00",
        "-1,00"
    );

    echo "<table>
        <tr>
            <th>String</th>
            <th>floatval()</th>
            <th>getFloat()</th>
            <th>parseFloat()</th>
        </tr>";

    foreach ($floatvals as $fval) {
        echo "<tr>";
        echo "<td>" . (string) $fval . "</td>";

        echo "<td>" . (float) floatval($fval) . "</td>";
        echo "<td>" . (float) getFloat($fval) . "</td>";
        echo "<td>" . (float) parseFloat($fval) . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}
?>
steve at opilo dot net 21-Jul-2008 03:07
Most of the functions listed here that deal with $ and , are unnecessarily complicated. You can use ereg_replace() to strip out ALL of the characters that will cause floatval to fail in one simple line of code:

<?php $output = floatval(ereg_replace("[^-0-9\.]","",$input)); ?>
leprau at leprau dot de 19-Jul-2007 06:26
For those of you, who are looking for a function that rips the first,
but longest possible float (or at least integer) from a string,
like 123.45 from the string "Price: 123,45$"

If no useable value is found, the function returns false.

Checks for both comma and dot as decimal-separator,
but does not check for 3 digits between thousands,
so 1,234.5 is as valid as 1,23,4.5 (both will return 1234.5)

12,.3 will return 12
1,000,000 will return 1000.0 !

(if thousands separator is defined,
decimals should be defined too ...
in fact I was too lazy to check for that too)

Here you go, and feel free to optimize the function ;)
<?php
function getFloat($pString) {
    if (strlen($pString) == 0) {
            return false;
    }
    $pregResult = array();

    $commaset = strpos($pString,',');
    if ($commaset === false) {$commaset = -1;}

    $pointset = strpos($pString,'.');
    if ($pointset === false) {$pointset = -1;}

    $pregResultA = array();
    $pregResultB = array();

    if ($pointset < $commaset) {
        preg_match('#(([-]?[0-9]+(\.[0-9])?)+(,[0-9]+)?)#', $pString, $pregResultA);
    }
    preg_match('#(([-]?[0-9]+(,[0-9])?)+(\.[0-9]+)?)#', $pString, $pregResultB);
    if ((isset($pregResultA[0]) && (!isset($pregResultB[0])
            || strstr($preResultA[0],$pregResultB[0]) == 0
            || !$pointset))) {
        $numberString = $pregResultA[0];
        $numberString = str_replace('.','',$numberString);
        $numberString = str_replace(',','.',$numberString);
    }
    elseif (isset($pregResultB[0]) && (!isset($pregResultA[0])
            || strstr($pregResultB[0],$preResultA[0]) == 0
            || !$commaset)) {
        $numberString = $pregResultB[0];
        $numberString = str_replace(',','',$numberString);
    }
    else {
        return false;
    }
    $result = (float)$numberString;
    return $result;
}
?>
aa at geb-team dot de 04-Sep-2006 05:03
@pillepop2003 at yahoo dot de

<?php
float('-100.00', array('single_dot_as_decimal' => true)); // whoops, returns -10000
?>

use: "/^[0-9-]*[\.]{1}[0-9-]+$/"
instead of: "/^[0-9]*[\.]{1}[0-9-]+$/"
19-Apr-2005 02:30
you can also use typecasting instead of functions:

(float) $value;
pillepop2003 at yahoo dot de 14-Dec-2004 11:38
Use this snippet to extract any float out of a string. You can choose how a single dot is treated with the (bool) 'single_dot_as_decimal' directive.
This function should be able to cover almost all floats that appear in an european environment.

<?php

    function float($str, $set=FALSE)
    {
        if(preg_match("/([0-9\.,-]+)/", $str, $match))
        {
            // Found number in $str, so set $str that number
            $str = $match[0];

            if(strstr($str, ','))
            {
                // A comma exists, that makes it easy, cos we assume it separates the decimal part.
                $str = str_replace('.', '', $str);    // Erase thousand seps
                $str = str_replace(',', '.', $str);    // Convert , to . for floatval command

                return floatval($str);
            }
            else
            {
                // No comma exists, so we have to decide, how a single dot shall be treated
                if(preg_match("/^[0-9]*[\.]{1}[0-9-]+$/", $str) == TRUE && $set['single_dot_as_decimal'] == TRUE)
                {
                    // Treat single dot as decimal separator
                    return floatval($str);

                }
                else
                {
                    // Else, treat all dots as thousand seps
                    $str = str_replace('.', '', $str);    // Erase thousand seps
                    return floatval($str);
                }
            }
        }

        else
        {
            // No number found, return zero
            return 0;
        }
    }

// Examples

echo float('foo 123,00 bar'); // returns 123.00
echo float('foo 123.00 bar' array('single_dot_as_decimal'=> TRUE)); //returns 123.000
echo float('foo 123.00 bar' array('single_dot_as_decimal'=> FALSE)); //returns 123000
echo float('foo 222.123.00 bar' array('single_dot_as_decimal'=> TRUE)); //returns 222123000
echo float('foo 222.123.00 bar' array('single_dot_as_decimal'=> FALSE)); //returns 222123000

// The decimal part can also consist of '-'
echo float('foo 123,-- bar'); // returns 123.00

?>

Big Up.
Philipp
anonymous at start dot be 16-Jun-2004 04:00
Easier-to-grasp-function for the ',' problem.

<?php
function Getfloat($str) {
  if(strstr($str, ",")) {
    $str = str_replace(".", "", $str); // replace dots (thousand seps) with blancs
    $str = str_replace(",", ".", $str); // replace ',' with '.'
  }

  if(preg_match("#([0-9\.]+)#", $str, $match)) { // search for number that may contain '.'
    return floatval($match[0]);
  } else {
    return floatval($str); // take some last chances with floatval
  }
}

echo Getfloat("$ 19.332,35-"); // will print: 19332.35
?>
vickers at hotpop dot com 27-Jan-2004 12:45
floatval() does not work with "$35,234.43", as it could not handle the '$' and the ','.  The following takes care of all values, such that only numeric and the decimal sign are input into floatval().  (It probably shows I'm an old 'c' guy)...this function only lightly tested.

<?php
function strtflt($str) {
    $il = strlen($str);
    $flt = "";
    $cstr = "";

    for($i=0;$i<$il;$i++) {
        $cstr = substr($str, $i, 1);
        if(is_numeric($cstr) || $cstr == ".")
            $flt = $flt.$cstr;
    }
    return floatval($flt);
}
?>

Richard Vickers
vickers@hotpop.com
Zipi 25-Apr-2003 11:51
This function converts a string to a float no matter is the decimal separator dot (.) or comma (,). It also converts integers correctly. It takes the digits from the beginning of the string and ignores all other characters.

<?php
function floatval($strValue) {
   $floatValue = ereg_replace("(^[0-9]*)(\\.|,)([0-9]*)(.*)", "\\1.\\3", $strValue);
   if (!is_numeric($floatValue)) $floatValue = ereg_replace("(^[0-9]*)(.*)", "\\1", $strValue);
   if (!is_numeric($floatValue)) $floatValue = 0;
   return $floatValue;
  }
?>

-Zipi (Finland)
jason at shadonet dot com 08-Mar-2003 04:07
Instead of using floatval which only appeared in PHP 4.2 you could juse use $variable = (float)$variable

This function doesn't seem to add any functionality that wasn't already there.


모든 정의된 변수의 배열을 반환

  • ≪ floatval
  • get_resource_type ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 모든 정의된 변수의 배열을 반환

get_defined_vars

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

get_defined_vars — 모든 정의된 변수의 배열을 반환

설명

array get_defined_vars ( void )

이 함수는 get_defined_vars()를 호출하는 영역 안에 존재하는 모든 정의된 변수(환경변수, 서버나 유저 정의 변수) 목록을 포함하는 다차원 배열을 반환합니다.

반환값

모든 변수를 가진 다차원 배열.

예제

Example #1 get_defined_vars() 예제

<?php
$b = array(1, 1, 2, 3, 5, 8);

$arr = get_defined_vars();

// $b를 출력
print_r($arr["b"]);

/* PHP 인터프리터의 경로를 출력 (CGI로 사용할 때)
 * 즉, /usr/local/bin/php */
echo $arr["_"];

// 명령줄 인수가 존재한다면 출력
print_r($arr["argv"]);

// 모든 서버 변수를 출력
print_r($arr["_SERVER"]);

// 변수 배열의 모든 키를 출력
print_r(array_keys(get_defined_vars()));
?>

변경점

버전 | 설명
5.0.0 | 반환하는 결과 배열에 $GLOBALS 변수가 포함됩니다.

참고

  • isset() - 설정된 변수인지 확인
  • get_defined_functions() - Returns an array of all defined functions
  • get_defined_constants() - 모든 상수와 그 값의 연관 배열을 반환합니다

User Contributed Notes

sergeyzuzic at mail dot ru 14-Nov-2015 07:35
dirty code sample:

print_r(compact(array_keys(get_defined_vars())));
mail at mkharitonov dot net 02-Jun-2014 04:14
Reference variables are returned by reference (tested on PHP 5.5.11):
<?php
$a = null;
$b = &$a;
get_defined_vars()['b'] = 4;
var_dump($b); // int(4)
?>
ryanlwh at yahoo dot com 03-Apr-2013 02:28
Since get_defined_vars() only gets the variables at the point you call the function, there is a simple way to get the variables defined within the current scope.

<?php
// The very top of your php script
$vars = get_defined_vars();

// Now do your stuff
$foo = 'foo';
$bar = 'bar';

// Get all the variables defined in current scope
$vars = array_diff(get_defined_vars(),$vars);

echo '<pre>';
print_r($vars);
echo '</pre>';
?>
Anonymous 18-Apr-2011 02:32
get_defined_vars() returns ALL the vars (in the current scope), what if you just want all YOUR vars, not PHP's super-globals?

<?php
    var_export(array_diff(get_defined_vars(), array(array())));
?>

Example...

<?php
    $TOP_LEVEL_VAR=1;
    var_export(array_diff(get_defined_vars(), array(array())));
?>

The output (with register_globals off) should be...

array (
  'TOP_LEVEL_VAR' => 1,
)

...it perfectly eliminated all the super-globals, without me having to specify them! (note with register_globals on, the output includes those globals, then TOP_LEVEL_VAR).

Here it is, as a function...(it's the best I could do {I can't call get_defined_vars() inside get_user_defined_vars() cuz of the scope issue}).

<?php
    header('Content-type: text/plain');

    $TOP_LEVEL_VAR=1;

    echo 'register_globals(';
    echo ini_get('register_globals');
    echo ') '.phpversion()."\n";

    var_export(get_user_defined_vars(get_defined_vars()));

    function get_user_defined_vars($vars) {
        return array_diff($vars, array(array()));
    }
?>

Note that originally I had an array of the super-globals I wanted removed from get_defined_vars()'s array, then I noticed even an empty double-array, array(array()), made it give me the correct result. Weird.

This was tested on PHP 5.2.9.
meint at meint dot net 11-Jan-2011 09:46
Please be aware that function_get_vars only returns the variables defined at the point before you call the function_get_vars function, it does not scan the entire function for you, only the lines before you call it.
Johan de Vries 17-Jun-2010 05:09
Note that this only returns things you've used. See http://bugs.php.net/bug.php?id=52110 . So don't expect this to have the $this entry, unless you assign $this to return $this.
donovan at example dot com 21-Oct-2008 06:15
As a note, get_defined_vars() does not return a set of variable references (as I hoped). For example:

<?php

// define a variable
$my_var = "foo";

// get our list of defined variables
$defined_vars = get_defined_vars();

// now try to change the value through the returned array
$defined_vars["my_var"] = "bar";

echo $my_var, "\n";

?>

will output "foo" (the original value). It'd be nice if get_defined_vars() had an optional argument to make them references, but I imagine its a rather specialized request. You can do it yourself (less conveniently) with something like:

<?php

$defined_vars = array();
$var_names = array_keys(get_defined_vars());

foreach ($var_names as $var_name)
{
    $defined_vars[$var_name] =& $$var_name;
}

?>
SyCo 22-Aug-2008 12:52
Here's a very simple function for debugging. It's far from perfect but I find it very handy. It outputs the var value and the var name on a new line. The problem is it'll echo any vars and their name if they share the same value. No big deal when debugging and saves the hassle of writing the HTML and var name when echoing a variable. (ev=echo variable). Using get_defined_vars() inside a function renames the var name to the functions variable so isn't as useful for debugging. Of course, you'll need access to the $GLOBALS array
<?
function ev($variable){
    foreach($GLOBALS as $key => $value){
        if($variable===$value){
            echo '<p>'.$key.' - '.$value.'</p>';
        }
    }
}

$a=0;
ev($a);
$b=0;
ev($b);
$c=0;
ev($c);
?>
Will output
a - 0

a - 0
b - 0

a - 0
b - 0
c - 0
zabmilenko at hotmail dot com 01-Sep-2006 04:32
A little gotcha to watch out for:

If you turn off RegisterGlobals and related, then use get_defined_vars(), you may see something like the following:

<?php
Array
(
    [GLOBALS] => Array
        (
            [GLOBALS] => Array
 *RECURSION*
            [_POST] => Array()
            [_GET] => Array()
            [_COOKIE] => Array()
            [_FILES] => Array()
        )

    [_POST] => Array()
    [_GET] => Array()
    [_COOKIE] => Array()
    [_FILES] => Array()

)
?>

Notice that $_SERVER isn't there.  It seems that php only loads the superglobal $_SERVER if it is used somewhere.  You could do this:

<?php
print '<pre>' . htmlspecialchars(print_r(get_defined_vars(), true)) . '</pre>';
print '<pre>' . htmlspecialchars(print_r($_SERVER, true)) . '</pre>';
?>

And then $_SERVER will appear in both lists.  I guess it's not really a gotcha, because nothing bad will happen either way, but it's an interesting curiosity nonetheless.
lbowerh at adelphia dot net 05-Jun-2004 05:19
Here is a function which generates a debug report for display or email
using get_defined_vars. Great for getting a detailed snapshot without
relying on user input.

<?php
function generateDebugReport($method,$defined_vars,$email="undefined"){
    // Function to create a debug report to display or email.
    // Usage: generateDebugReport(method,get_defined_vars(),email[optional]);
    // Where method is "browser" or "email".

    // Create an ignore list for keys returned by 'get_defined_vars'.
    // For example, HTTP_POST_VARS, HTTP_GET_VARS and others are
    // redundant (same as _POST, _GET)
    // Also include vars you want ignored for security reasons - i.e. PHPSESSID.
    $ignorelist=array("HTTP_POST_VARS","HTTP_GET_VARS",
    "HTTP_COOKIE_VARS","HTTP_SERVER_VARS",
    "HTTP_ENV_VARS","HTTP_SESSION_VARS",
    "_ENV","PHPSESSID","SESS_DBUSER",
    "SESS_DBPASS","HTTP_COOKIE");

    $timestamp=date("m/d/y h:m:s");
    $message="Debug report created $timestamp\n";

    // Get the last SQL error for good measure, where $link is the resource identifier
    // for mysql_connect. Comment out or modify for your database or abstraction setup.
    global $link;
    $sql_error=mysql_error($link);
    if($sql_error){
      $message.="\nMysql Messages:\n".mysql_error($link);
    }
    // End MySQL

    // Could use a recursive function here. You get the idea ;-)
    foreach($defined_vars as $key=>$val){
      if(is_array($val) && !in_array($key,$ignorelist) && count($val) > 0){
        $message.="\n$key array (key=value):\n";
        foreach($val as $subkey=>$subval){
          if(!in_array($subkey,$ignorelist) && !is_array($subval)){
            $message.=$subkey." = ".$subval."\n";
          }
          elseif(!in_array($subkey,$ignorelist) && is_array($subval)){
            foreach($subval as $subsubkey=>$subsubval){
              if(!in_array($subsubkey,$ignorelist)){
                $message.=$subsubkey." = ".$subsubval."\n";
              }
            }
          }
        }
      }
      elseif(!is_array($val) && !in_array($key,$ignorelist) && $val){
        $message.="\nVariable ".$key." = ".$val."\n";
      }
    }

    if($method=="browser"){
      echo nl2br($message);
    }
    elseif($method=="email"){
      if($email=="undefined"){
        $email=$_SERVER["SERVER_ADMIN"];
      }

      $mresult=mail($email,"Debug Report for ".$_ENV["HOSTNAME"]."",$message);
      if($mresult==1){
        echo "Debug Report sent successfully.\n";
      }
      else{
        echo "Failed to send Debug Report.\n";
      }
    }
}
?>
jgettys at gnuvox dot com 23-Feb-2002 04:09
Simple routine to convert a get_defined_vars object to XML.

<?php
function obj2xml($v, $indent='') {
  while (list($key, $val) = each($v)) {
    if ($key == '__attr') continue;
    // Check for __attr
    if (is_object($val->__attr)) {
      while (list($key2, $val2) = each($val->__attr)) {
        $attr .= " $key2=\"$val2\"";
      }
    }
    else $attr = '';
    if (is_array($val) || is_object($val)) {
      print("$indent<$key$attr>\n");
      obj2xml($val, $indent.'  ');
      print("$indent</$key>\n");
    }
    else print("$indent<$key$attr>$val</$key>\n");
  }
}

//Example object
$x->name->first = "John";
$x->name->last = "Smith";
$x->arr['Fruit'] = 'Bannana';
$x->arr['Veg'] = 'Carrot';
$y->customer = $x;
$y->customer->__attr->id='176C4';

$z = get_defined_vars();
obj2xml($z['y']);
?>

will output:
<customer id="176C4">
  <name>
    <first>John</first>
    <last>Smith</last>
  </name>
  <arr>
    <Fruit>Bannana</Fruit>
    <Veg>Carrot</Veg>
  </arr>
</customer>


자원형을 반환

  • ≪ get_defined_vars
  • gettype ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 자원형을 반환

get_resource_type

(PHP 4 >= 4.0.2, PHP 5, PHP 7)

get_resource_type — 자원형을 반환

설명

string get_resource_type ( resource $handle )

주어진 자원의 자료형을 얻습니다.

인수

handle

평가할 자원 핸들.

반환값

주어진 handle이 자원이면, 이 함수는 그 자료형의 문자열 표현을 반환합니다. 자료형을 식별할 수 없으면, 반환값은 문자열 Unknown입니다.

handle이 resource가 아닐 경우, FALSE를 반환하고 오류를 발생합니다.

예제

Example #1 get_resource_type() 예제

<?php
// 출력: mysql link
$c = mysql_connect();
echo get_resource_type($c) . "\n";

// 출력: file
$fp = fopen("foo", "w");
echo get_resource_type($fp) . "\n";

// 출력: domxml document
$doc = new_xmldoc("1.0");
echo get_resource_type($doc->doc) . "\n";
?>

User Contributed Notes

CertaiN 17-Jan-2014 02:04
Try this to know behavior:

<?php
function resource_test($resource, $name) {
    echo
        '[' . $name. ']',
        PHP_EOL,
        '(bool)$resource => ',
        $resource ? 'TRUE' : 'FALSE',
        PHP_EOL,
        'get_resource_type($resource) => ',
        get_resource_type($resource) ?: 'FALSE',
        PHP_EOL,
        'is_resoruce($resource) => ',
        is_resource($resource) ? 'TRUE' : 'FALSE',
        PHP_EOL,
        PHP_EOL
    ;
}

$resource = tmpfile();
resource_test($resource, 'Check Valid Resource');

fclose($resource);
resource_test($resource, 'Check Released Resource');

$resource = null;
resource_test($resource, 'Check NULL');
?>

It will be shown as...

[Check Valid Resource]
(bool)$resource => TRUE
get_resource_type($resource) => stream
is_resoruce($resource) => TRUE

[Check Released Resource]
(bool)$resource => TRUE
get_resource_type($resource) => Unknown
is_resoruce($resource) => FALSE

[Check NULL]
(bool)$resource => FALSE
get_resource_type($resource) => FALSE
Warning:  get_resource_type() expects parameter 1 to be resource, null given in ... on line 10
is_resoruce($resource) => FALSE
avp200681 at gmail dot com 13-Jun-2011 12:11
This function will generate an error and return NULL instead FALSE in the next example:

<?php

$var = 3;
var_dump(get_resource_type($var));

?>
hessodreamy at gmail dot com 29-May-2007 01:07
Furthermore, this function returns 'stream' on php 4.3 on windows & linux.
You if you want to differentiate between a file resource and other stream resources, as stated before stream_get_meta_data() gives you an array which, on my system, contains the following elements which might suggest a file handle:
[wrapper_type] => plainfile
[stream_type] => STDIO

However I've only compared this to a stream from a url, so I include this only as a suggestion.
jdhawk _at_ gmail 05-Oct-2005 10:18
In PHP5 (on Linux), the above example identifies the "file" handler as a 'stream', not 'file'.

This can be particularly worrisome if you're trying to determine the type of stream using this function.

The alternative is to use <?php stream_get_meta_data() ?>, which returns an array containing "stream_type (string) - a label describing the underlying implementation of the stream."


변수의 자료형을 얻습니다

  • ≪ get_resource_type
  • import_request_variables ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수의 자료형을 얻습니다

gettype

(PHP 4, PHP 5, PHP 7)

gettype — 변수의 자료형을 얻습니다

설명

string gettype ( mixed $var )

PHP 변수 var의 자료형을 반환합니다.

Warning

gettype()을 어떤 자료형인지 시험하기 위해 사용하지 마십시오. 반환 문자열은 앞으로 나올 버전에서 바뀔 수 있습니다. 또한, 문자열 비교를 수행하기 때문에 느립니다.

대신, is_* 함수를 사용하십시오.

인수

var

자료형을 확인할 변수

반환값

반환 문자열의 가능한 값은:

  • "boolean"
  • "integer"
  • "double" (역사적인 이유로 float의 경우에도 단순히 "float"가 아닌 "double"을 반환합니다)
  • "string"
  • "array"
  • "object"
  • "resource"
  • "NULL"
  • "unknown type"

예제

Example #1 gettype() 예제

<?php

$data = array(1, 1., NULL, new stdClass, 'foo');

foreach ($data as $value) {
    echo gettype($value), "\n";
}

?>

위 예제의 출력 예시:

integer
double
NULL
object
string

참고

  • settype() - 변수의 자료형을 설정
  • is_array() - 변수가 배열인지 확인
  • is_bool() - 변수가 논리형인지 확인
  • is_float() - 변수의 자료형이 소수인지 확인합니다
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_null() - 변수가 NULL인지 확인합니다
  • is_numeric() - 변수가 수나 수 문자열인지 확인합니다
  • is_object() - 변수가 객체인지 확인합니다
  • is_resource() - 변수가 자원인지 확인
  • is_scalar() - 변수가 스칼라인지 확인
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • function_exists() - Return TRUE if the given function has been defined
  • method_exists() - 클래스 메쏘드가 존재하는지 확인

User Contributed Notes

ddr-2kpp (at) web dot de 10-Jan-2015 09:16
Hi there,

this fastest solution I found to identify a variable type without using gettype function:
<?PHP
function typeof($var) {
  if (is_object($var))
    return "object";

  if (is_resource($var))
    return "resource";

  return (($var === null) ? "null" :
      (((bool)$var === $var) ? "bool" :
       (((float)$var === $var) ? "float" :
        (((int)$var === $var) ? "int" :
         (((string)$var === $var) ? "string" :
              "unknown"
          )
         )
        )
       )
      );
}
?>
Maybe someone has a more elegant solution to check for object and resource types...

The complete script - including several identify- and timing functions follow:
#!/usr/bin/php
<?PHP
function typeof_trinary($var) {
  if (is_object($var))
    return "object";

  if (is_resource($var))
    return "resource";

  return (($var === null) ? "null" :
      (((bool)$var === $var) ? "bool" :
       (((float)$var === $var) ? "float" :
        (((int)$var === $var) ? "int" :
         (((string)$var === $var) ? "string" :
              "unknown"
          )
         )
        )
       )
      );
   }

function typeof_if_query($var) {

  if (is_object($var))
    return "object";

  if (is_resource($var))
    return "resource";

  if ((bool)$var === $var)
      return "bool";

  if ((float)$var === $var)
    return "float";

  if ((int)$var === $var)
    return "int";

  if ((string)$var === $var)
    return "string";

  if (null === $var)
    return "null";

  return "unknown";
}

function typeof_gettype($var) {
    return \gettype($var);
}

$var = 100;

echo "start@ " . \date("Y-m-d H:i:s") . "\n";
echo "\$var = $var - gettype(\$var) = " . gettype($var) . "\n\n";
$time = -\microtime(true);

echo "typeof_if_query(\$var) = " . typeof_if_query($var) . "\n";
$time += \microtime(true);
echo "duration: " . \sprintf("%0.10f",$time) . "\n\n";

$time = -\microtime(true);

echo "typeof_gettype(\$var) = " . typeof_gettype($var) . "\n";
$time += \microtime(true);
echo "duration: " . \sprintf("%0.10f",$time) . "\n\n";

$time = -\microtime(true);

echo "typeof_trinary(\$var) = " . typeof_trinary($var) . "\n";
$time += \microtime(true);
echo "duration: " . \sprintf("%0.10f",$time) . "\n\n";
?>

My console output - try the script several times!:
$ ./gettype.php
start@ 2015-01-10 01:10:37
$var = 100 - gettype($var) = integer

typeof_if_query($var) = int
duration: 0.0000500679

typeof_gettype($var) = integer
duration: 0.0000309944

typeof_trinary($var) = int
duration: 0.0000240803
wojciech dot fornal at gmail dot com 29-Jun-2014 08:55
@langpavel at phpskelet dot org

Doesn't the gettype return the string already? Using get_class seems to be the only "useful" part of that solution. All that can be reduced to somewhere half in terms of the amount of code.
Wilson212 02-Jul-2011 04:20
I did some benchmarking and your method here is alot slower then using the actual gettype(); (Using php 5.3.4)
langpavel at phpskelet dot org 02-Jun-2011 05:20
This function returns string representation of type. This is generalization of get_class(). I try to order is_* tests by density of occurence, but is bad idea to use result of this call in conditions for performance reasons, usefull and better than gettype for debugging messages.
Note, that last line should not be never executed.
Also has no sense to make input parameter optional.

<?php
function get_type($var)
{
    if(is_object($var))
        return get_class($var);
    if(is_null($var))
        return 'null';
    if(is_string($var))
        return 'string';
    if(is_array($var))
        return 'array';
    if(is_int($var))
        return 'integer';
    if(is_bool($var))
        return 'boolean';
    if(is_float($var))
        return 'float';
    if(is_resource($var))
        return 'resource';
    //throw new NotImplementedException();
    return 'unknown';
}
?>
Crysis nerd 19-Dec-2010 03:40
I wanted to compare the type of 2 vars.
1st method: Use this Funktion (gettype) and compare with ==
2nd method: Compare every type per is_[type]
Here a little Benchmark:
(I had to break line twice..)

<?php
$var1 = "Hallo";
$var2 = "Kasekuchen";
$var3 = 3.141526;
$Start = microtime(true);
for($i=0;$i<10000;$i++)
{
if(is_bool($var1)&&is_bool($var2)||is_int($var1)&&
is_int($var2)||is_string($var1)&&is_string($var2)||
is_object($var1)&&is_object($var2)||is_array($var1)
&&is_array($var2))
    {
        //same
    }
    if(is_bool($var3)&&is_bool($var2)||is_int($var3)&&
is_int($var2)||is_string($var3)&&is_string($var2)||
is_object($var3)&&is_object($var2)||is_array($var3)
&&is_array($var2))
    {
        //same
    }
}
$time = microtime(true)-$Start;
echo "Zeit fuer if abfrage:  ".$time."<br>";

$Start = microtime(true);
for($i=0;$i<10000;$i++)
{
    if(gettype($var1)==gettype($var2))
    {
        //same
    }
    if(gettype($var3)==gettype($var2))
    {
        //same
    }
}
$time = microtime(true)-$Start;
echo "Zeit fuer funktion  :  ".$time;
?>

Output:
Zeit fuer if abfrage: 0.0257611274719
Zeit fuer funktion : 0.0139331817627

So the function isnt so slow...

Lukas
PPKu-N0-SPAM-schy at mediasoft-berlin dot de 29-Nov-2010 03:57
After some testing I found a bug in my function "myGetType":
The check for "is_callable" was done before "is_string", so that something like <?php echo myGetType("max"); ?> would output: "function reference" instead of "string"

"is_callable" and "is_string" can't be checked together in this method, so I've removed the check for is_callable because it's a very rare usage case and if it's a valid string the check for is_callable never executes because is_string would be reached first (or vice-versa).

So here is the new function without "is_callable"-Check:
<?php
    /**
     * Returns the type of the var passed.
     *
     * @param mixed $var Variable
     * @return string Type of variable
     */
    function myGetType($var)
    {
        if (is_array($var)) return "array";
        if (is_bool($var)) return "boolean";
        if (is_float($var)) return "float";
        if (is_int($var)) return "integer";
        if (is_null($var)) return "NULL";
        if (is_numeric($var)) return "numeric";
        if (is_object($var)) return "object";
        if (is_resource($var)) return "resource";
        if (is_string($var)) return "string";
        return "unknown type";
    }
?>
SoN9ne at gmail dot com 08-Jun-2010 08:32
This is my work around for the gettype warning. Hope some find it useful.

<?php
    /**
     * Returns the type of the passed var
     * - PHP warns against using gettype(), this is my workaround
     *
     * @param mixed $var
     * @return string
     */
    function myGetType($var)
    {
        if (is_array($var)) return "array";
        if (is_bool($var)) return "boolean";
        if (is_callable($var)) return "function reference";
        if (is_float($var)) return "float";
        if (is_int($var)) return "integer";
        if (is_null($var)) return "NULL";
        if (is_numeric($var)) return "numeric";
        if (is_object($var)) return "object";
        if (is_resource($var)) return "resource";
        if (is_string($var)) return "string";
        return "unknown type";
    }
?>

[EDITOR thiago NOTE: Code has been updated by PPKu-N0-SPAM-schy at mediasoft-berlin dot de]
skatebiker at hotmail dot com 22-Feb-2008 02:51
In some rare cases a class instance object returns false when an object but gettype() returns "object".

<?php
$x = new classvar();

$save = serialize($x);

// ......

$obj = unserialize($save);
// here sometimes is_object() returns FALSE
if (is_object($x) || gettype($x) === "object")
{
   // ... do something
}
?>
andrey at php dot net 17-Jul-2007 07:08
The function returns "unicode" for Unicode strings in PHP6.
sneskid at hotmail dot com 05-Mar-2007 10:56
I wrote my own gettype function by just using the default is_? functions, but it took twice as long as gettype... So I decided to use gettype with a twist.

Taking the warnings about gettype to heart, and depending on your custom needs, it's worthwhile to dynamically test the gettype result with a known variable, and link the result to a predefined result. Like so:

<?php
/*
 dynamically create an array by using known variable types
 link with a predefined value
*/
$R=array();
$R[gettype(.0)]='number';
$R[gettype(0)]='number';
$R[gettype(true)]='boolean';
$R[gettype('')]='string';
$R[gettype(null)]='null';
$R[gettype(array())]='array';
$R[gettype(new stdClass())]='object';

// what is
function wis_($v){
    global $R;
    return $R[gettype($v)];
}

echo wis_('hello') . '<br/>'; // "string"
echo wis_(24) . '<br/>'; // "number"
echo wis_(0.24) . '<br/>'; // "number"
echo wis_(null) . '<br/>'; // "null"
echo wis_($R) . '<br/>'; // "array"
?>
You won't need to worry about changes in gettype's return strings in future versions.
If the result evaluates to false then you know the variable tested is some "other" type.

I also find these useful
<?php
function is_num($v){return (is_int($v) || is_double($v));}
function is_box($v){return (is_array($v)||is_object($v));}

echo is_num(null) . '<br/>'; // false
echo is_num(false) . '<br/>'; // false
echo is_num('123') . '<br/>'; // false
echo is_num(123) . '<br/>'; // true
echo is_num(123.0) . '<br/>'; // true
?>
gilthansNOSPAM at gmail dot com 11-Sep-2005 10:18
NaN and #IND will return double or float on gettype, while some inexistent values, like division by zero, will return as a boolean FALSE. 0 by the 0th potency returns 1, even though it is mathematically indetermined.

<?php
$number = 5/0;
$number2 = sqrt(-3);
$number3 = pow(0, 0);
$number4 = 0/0;

echo $number."<br />";
echo $number2."<br />";
echo $number3."<br />";
echo $number4."<br />";
echo "<br />";
echo gettype($number)."<br />";
echo gettype($number2)."<br />";
echo gettype($number3)."<br />";
echo gettype($number4);
?>

This will return:

-1.#IND
1

boolean
double
integer
boolean

0
1
1
0
PHP Warning: Division by zero in C\test.php on line 2 PHP Warning: Division by zero in C:\test.php on line 5
matt at appstate 16-Dec-2004 08:10
Here is something that had me stumped with regards to gettype and is_object.
Gettype will report an incomplete object as such, whereas is_object will return FALSE.

<?php
if (!is_object($incomplete_obj)) {
   echo 'This variable is not an object, it is a/an ' . gettype($incomplete_obj);
}
?>

Will print:
This variable is not an object, it is a/an object


GET/POST/쿠키 변수를 전역으로 가져옵니다

  • ≪ gettype
  • intval ≫
  • PHP Manual
  • Variable handling 함수 목록
  • GET/POST/쿠키 변수를 전역으로 가져옵니다

import_request_variables

(PHP 4 >= 4.1.0, PHP 5 < 5.4.0)

import_request_variables — GET/POST/쿠키 변수를 전역으로 가져옵니다

설명

bool import_request_variables ( string $types [, string $prefix ] )

GET/POST/쿠키 변수를 전역으로 가져옵니다. register_globals을 사용하지 않지만, 몇몇 변수를 전역에서 사용하고 싶을 때 유용합니다.

SERVER 등의 다른 변수를 전역으로 가져오고 싶다면, extract()를 사용하십시오.

인수

types

types 인수를 사용하여, 어떠한 요청 변수를 가져올지 지정할 수 있습니다. 'G', 'P', 'C' 문자가 GET, POST, 쿠키에 대응합니다. 대소문자는 구별하지 않기 때문에 'g', 'p', 'c,'의 조합도 사용할 수 있습니다. POST는 POST 업로드 파일 정보도 포함합니다.

Note:

문자 순서에 주의하십시오. "gp"를 사용하면, POST 변수가 동일한 이름을 가지는 GET 변수를 덮어씌웁니다. GPC 이외의 모든 문자는 무시합니다.

prefix

전역으로 가져오는 모든 변수의 이름 앞에 붙이는 변수명 prefix입니다. "userid"라는 GET 값을 가지고 prefix에 "pref_"를 넘기면, 전역 변수 이름은 $pref_userid가 됩니다.

Note:

prefix 인수는 선택적이지만, prefix를 지정하지 않거나 빈 문자열을 지정하면, E_NOTICE가 발생합니다. 이는 보안 문제가 발생하기 때문입니다. Notice 오류는 기본 오류 보고 레벨에서는 표시되지 않습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 import_request_variables() 예제

<?php
// GET과 POST 변수를
// "rvar_" prefix로 가져옵니다.
import_request_variables("gp", "rvar_");

echo $rvar_foo;
?>

참고

  • $_REQUEST
  • register_globals
  • 예약 변수
  • extract() - 배열에서 현재 심볼 테이블로 변수를 입력

User Contributed Notes

rustamabd at gmail dot com 13-May-2012 01:45
import_request_variables() is gone from PHP since version 5.4.0. A simple plug-in replacement it extract().

For example:

import_request_variables('gp', 'v_');

Can be replaced with:

extract($_REQUEST, EXTR_PREFIX_ALL|EXTR_REFS, 'v');
samb06 at gmail dot com 16-May-2006 01:09
What i do is have a small script in my header file that takes an array called $input, and loops through the array to extract variables. that way the security hole can be closed, as you specify what variables you would like extracted

$input = array('name' => null, 'age' => 26) ;

// 26 is the default age, if $_GET['age'] is empty or not set

function extract_get()
    {
        global $input ;

        if ($input)
            {
                foreach ($input as $k => $v)
                    {
                        if ($_GET[$k] == '' or $_GET[$k] == NULL)
                            {
                                $GLOBALS[$k] = $v ;
                            }
                        else
                            {
                                $GLOBALS = $_GET[$k] ;
                            }
                    }
            }
    }
jason 08-Jul-2005 09:35
reply to ceo AT l-i-e DOT com:

I don't think it's a risk, as all of your request variables will be tagged with the prefix. As long as you don't prefix any of your internal variables with the same, you should be fine.

If someone tries to access an uninitiated security-related variable like $admin_level through request data, it will get imported as $RV_admin_level.
cornflake4 at gmx dot at 10-Jan-2005 11:52
oops, a typo in my comment:

The last line in the second example (the on using the extract() function) should read:

echo $_GET['var']; # prints 1, so $_GET has been unchanged
ceo AT l-i-e DOT com 10-Dec-2004 09:56
Call me crazy, but it seems to me that if you use this function, even WITH the prefix, then you might as well just turn register_globals back on...

Sooner or later, somebody will find a "hole" with your prefixed variables in an un-initialized variable.

Better to import precisely the variables you need, and initialize anything else properly.
brian at enchanter dot net 07-Dec-2004 02:19
import_request_variables does *not* read from the $_GET, $_POST, or $_COOKIE arrays - it reads the data directly from what was submitted. This is an important distinction if, for example, the server has magic_quotes turned on and you massage the data to run stripslashes on it; if you then use import_request_variables, your variables will still have slashes in them.

In other words: even if you say $_GET=""; $_POST=""; then use import_request_variables, it'll still get all the request data.

If you change the contents of $_GET and you then want to bring this data into global variables, use extract($_GET, EXTR_PREFIX_ALL, "myprefix") instead.


변수의 정수값을 얻습니다

  • ≪ import_request_variables
  • is_array ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수의 정수값을 얻습니다

intval

(PHP 4, PHP 5, PHP 7)

intval — 변수의 정수값을 얻습니다

설명

int intval ( mixed $var [, int $base ] )

지정한 base 값에 따른 var의 integer값을 반환합니다. (기본 진법은 10입니다)

인수

var

정수로 변환할 스칼라 값

base

변환에 사용할 진법 (기본값은 10진법)

반환값

성공시엔 var의 정수 값, 실패시엔 0. 빈 배열과 객체는 0을 반환하고, 비어있지 않은 배열과 객체는 1을 반환합니다.

최대값은 시스템에 의존합니다. 32비트 시스템은 부호 있는 정수의 최대값 범위로 -2147483648에서 2147483647을 가집니다. 그러한 시스템에서 intval('1000000000000') 등은 2147483647을 반환합니다. 64비트 시스템에서 부호 있는 정수의 최대값은 9223372036854775807입니다.

문자열은 대부분 0을 반환하지만, 이는 문자열의 가장 왼쪽 문자들에 의해 결정됩니다. 정수형 변환의 기본 규칙이 적용됩니다.

예제

Example #1 intval() 예제

다음 예제는 32비트 시스템입니다.

echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42
echo intval(042);                     // 34
echo intval('042');                   // 42
echo intval(1e10);                    // 1410065408
echo intval('1e10');                  // 1
echo intval(0x1A);                    // 26
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // 0
echo intval('420000000000000000000'); // 2147483647
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
?>

주의

Note:

var 인수가 문자열이 아니면 base 인수는 아무런 영향을 주지 않습니다.

참고

  • floatval() - 변수의 float 값을 얻습니다
  • strval() - 변수의 문자열 값을 얻습니다
  • settype() - 변수의 자료형을 설정
  • is_numeric() - 변수가 수나 수 문자열인지 확인합니다
  • 자료형 조절
  • BCMath 임의 정밀도 수학 함수

User Contributed Notes

Er.ellison 07-Nov-2015 12:48
i guess one of the important usage of intval is in links , because :
imagine that u have a function that uses get method (for example an integer id ) to retrieve the data from DB ,
so for example if you get "  id=58  " in your link and put that in u'r function that retrieves data from DB , that's fine , it works well
but what if some body put the  "  id=58djkdjsbvjkdsbvjkdbs " in the link ON PURPOSE !
in such situation and condition he will get an Database error
because u'r function retrieves Data from DB
so it good to be use intval in such situations
because if u use that u gonna be sure that what ever that passes from u'r DB related function is just a integer , No text and any strange character !

let me show u by example
$id = 12dfjgdfjgnd;

echo $id  // output will be : 12dfjgdfjgnd
echo intval($id) // output will be  : 12
taylorsarrafian at gmail dot com 11-Jul-2015 01:45
beware:

<?php

   // observe the following
   echo intval( strval( -0.0001 ) ); // 0
   echo intval( strval( -0.00001 ) ); // -1

   // this is because
   echo strval( -0.0001 ); // -.0001
   echo strval( -0.00001 ); // -1.0E-5

   // thus beware when using
   function trunc2_bad( $n ) {
      return intval( strval( $n * 100 ) / 100 );
   }

   // use this instead
   function trunc2_good( $n ) {
      return intval( floatval( strval( $n * 100 )  ) / 100 );
   }

?>
nlong121 at gmail dot com 12-Jul-2014 10:33
As a note using intval() as a check for string comparison will not work

$string="Pork";
$test=intval($string);
echo $test.'=='.$string;
if($string==$test)
    {
    echo '::Test PASS';
    }else{
    echo '::Test FAIL';
    }
if(is_numeric($string))
    {
    echo '::Test PASS';
    }else{
    echo '::Test FAIL';
    }
/*Produces
0==Pork :: Test PASS
is_numeric(Pork) :: Test FAIL
jplevene at netscape dot net 06-Jan-2014 08:40
I have found intval very useful for database security.

If you are passing a value via

- Data from user input
- Data that you have stored RAW on DB (witch is the best way) and you are using on a new sql statment
- Data from unknown/other origin

and the data is supposed to be an integer, I always run it through intval before I put it into the query (or you could use real_escape_string)

This prevents hackers from passing the variable as an SQL statement instead of a number in order to attack your database.
pfreet at gmail dot com 26-Apr-2013 02:27
Do not use intval() when you really want round(). This is due to how PHP handles precision.

echo number_format(8.20*100, 20), "<br />";
echo intval(8.20*100), "<br />";
echo floor(8.20*100), "<br />";
echo round(8.20*100), "<br />";

819.99999999999988631316
819
819
820
espertalhao04 at hotmail dot com 06-Mar-2013 06:58
if you want to take a number from a string, no matter what it may contain, here is a good solution:

<?php
function int($s){return(int)preg_replace('/[^\-\d]*(\-?\d*).*/','$1',$s);}

echo int('j18ugj9hu0gj5hg');
//output: 18
?>
this example returns an int, so it will follow the int rules, and has support for negative values.

<?php
function int($s){return($a=preg_replace('/[^\-\d]*(\-?\d*).*/','$1',$s))?$a:'0';}

echo int('j-1809809808908099878758765ugj9hu0gj5hg');
//output: -1809809808908099878758765
?>

this one returns a string with just the numeric value.
it also supports negative values.

the latter is better when you have a 32 bit system and you want a huge int that is higher than PHP_MAX_INT.
gregrobson at gmail dot com 17-Aug-2012 12:15
I just wanted to be sure that a string was going to be valid if I intval'ed it. This regular expression matches:
0
Any positive integer (no + sign)
Any negative integer (with a - sign)

It does not match a leading 0 or any other character. You are advised to strip out commas, trim the string and check range yourself ;)

<?php
/**
 * Loops through each non-array element and actions a function.
 *
 * @return boolean|integer TRUE if the value matches as an integer, FALSE if
 * an error occured or 0 if not match was found.
 */
public function validate($value)
{
    $value = preg_match('/^(0|(-{0,1}[1-9]\d*))$/', trim($value));

    if ($value == 1)
        return true;
    else
        return $value;
}
?>

If you use intval() now, you are merely changing the data type.
Ken 12-Nov-2011 08:50
Not mentioned elsewhere: intval(NULL) also returns 0.
yves 28-Dec-2010 05:53
The behaviour of intval() is interesting when supplying a base, and you better check your intval base-based expressions, as it is counter-intuitive.
As the example shows
<?php
  intval('42', 8); // => 34
  intval(42, 8);   // => 42 !
?>
PHP considers the 42 as being already an integer, and doesn't apply any conversion. And supplying
<?php
  intval(49, 8);  // => 49 !
?>
produces no error and no warning.
winbill at hotmail dot com 16-Dec-2010 05:31
Be careful :

<?php
$n="19.99";
print intval($n*100); // prints 1998
print intval(strval($n*100)); // prints 1999
?>
jeremy at cfegames dot com 12-Aug-2010 04:41
When retrieving numbers or integers from a MySQL DB, it's best to use intval(), or it will continue to be a string.

<?php
while($row = mysql_fetch_array($check)
{
 $_SESSION['id'] = inval($row['id']);
 $user_id = intval($_SESSION['id']); // Not sure if you need to do it again, but doesn't hurt.
}

is_int($_SESSION['id'])
is_int($user_id)
is_string($_SESSION['id'])
is_string($user_id)

// Output
true
true
false
false
?>
spoon_reloaded at gmail dot com 21-Jun-2009 07:35
Here is a really useful undocumented feature:

You can have it automatically deduce the base of the number from the prefix of the string using the same syntax as integer literals in PHP ("0x" for hexadecimal, "0" for octal, non-"0" for decimal) by passing a base of 0 to intval():

<?php
echo intval("0x1a", 0), "\n"; // hex; prints "26"
echo intval("057", 0), "\n"; // octal; prints "47"
echo intval("42", 0), "\n"; // decimal; prints "42"
?>
bert at nospam dot thinc dot nl 07-Jun-2009 01:16
If you need the reverse function of intval(), the code hereunder might be helpfull.

<?php
function itoa( $ii, $radix=10, $stritoa="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabc" ) {
    if ( $radix > strlen( $stritoa ) )    //    does request makes sense?
        return "";    //    think of your own way to handle this case
    $sign = $ii<0 ? "-": "";
    $ii = abs( $ii );
    $rc = "";
    do {
        $rc .= $stritoa[ $ii % $radix ];
        $ii = floor( $ii / $radix );
        } while ( $ii >0 );
    return $sign . strrev( $rc );
    }
?>
ittasks at gmail dot com 24-Mar-2009 10:47
when converting some optional form values to positive integers and to be on the safe side, you can use this:

<?php $q=intval("0".$_REQUEST['q']); ?>
Seally 28-Aug-2008 11:47
Re: nobodyisperfect88 at hotmail dot de
jay at w3prodigy dot com had it right. I believe it is an issue with the floating point registers on computers, which have a precision of about 17 digits. When you get more precise than that, it rounds.

Since the second example can fit in the register, no rounding occurs, and PHP can truncate the portion after the decimal. However, the system automatically rounds the first example to 1, which is what PHP returns.

(Tested on Windows XP 32bit)
yhoko at yhoko dot com 17-Aug-2008 04:29
Note that this function behaves different in PHP4/5 when parsing NAN-values. Try the following example:

<?php die( "Result = " . intval( NAN ) ); ?>

- In PHP 4 the result is 0
- In PHP 5 the result is 2^31 (running on 32bit)

Yhoko
phpben hood id au 11-Jun-2008 02:17
kris at mha dot ca:

Implicit typing. This is a feature, not a bug.

The == operator will convert your string to an int automatically (in this case) so that it is really comparing 123 and 123, which is of course the same.

If you use the === operator then this will not occur, as it will only match on the same value and type. This works as expected:

<?php

$tmp_array = "123,232,141";

if (intval($tmp_array) === $tmp_array)
{
    echo "'".intval($tmp_array)."' equals '$tmp_array'\n";
}
else
{
    echo "'".intval($tmp_array)."' does not equal '$tmp_array'\n";
}

?>
jay at w3prodigy dot com 25-Mar-2008 09:05
Note:

$int = 0.99999999999999999;
echo intval($int); // returns 1

and

$int = 0.9999999999999999;
echo intval($int); // returns 0
michiel ed thalent nl 12-Dec-2007 02:44
A function to get an integer from everywhere in a string (concatenated or not).

<?php
function str2int($string, $concat = true) {
    $length = strlen($string);
    for ($i = 0, $int = '', $concat_flag = true; $i < $length; $i++) {
        if (is_numeric($string[$i]) && $concat_flag) {
            $int .= $string[$i];
        } elseif(!$concat && $concat_flag && strlen($int) > 0) {
            $concat_flag = false;
        }
    }

    return (int) $int;
}
echo var_dump(str2int('sh12apen11')); // int(12)
echo var_dump(str2int('sh12apen11', false)); // int(1211)
echo var_dump(str2int('shap99en')); // int(99)
echo var_dump(intval('shap99en')); // int(0)
?>
lrdsatyr8 at hotmail dot com 31-Oct-2007 02:09
If you just want to get the integer value of a number without all the hassle, just use intval()... like so:

$a1 = 10.4;
$a2 = -12.5;
$a3 = 44.1238503;

print intval($a1);   // returns 10
print intval($a2);   // returns -12
print intval($a3);   // returns 44

it's that easy!
jazeik at gmail dot com 31-Jul-2007 03:15
@kris:
If you had followed the following links:
"The common rules of integer casting apply." => http://us.php.net/manual/en/language.types.integer.php
"See String conversion to numbers" => http://us.php.net/manual/en/language.types.integer.php

Then you would have found the following: "The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used."

So this behaviour is a feature not a bug.

The following code will work:
<?php

$tmp_array = str_replace(",", "", "123,232,141");

if(intval($tmp_array) == $tmp_array){
    echo intval($tmp_array) . " equals $tmp_array\n";
}else{
    echo intval($tmp_array) . " does not equal $tmp_array\n";
}

?>

Output:
123232141 equals 123232141
vizigr0u at gmail dot com 24-Jun-2007 01:50
intval used on bools returns either 1 or 0
I usually use this to properly set bools in my tables
(assuming you use some kind of int like tinyints as booleans) :
<?php
$myvar = 'right value';
mysql_query('INSERT INTO mytable (activated)
VALUES(' . intval($myvar === 'right value') . ')');
?>
aryel at redtwilightNO dot comSPAM dot brNOT 26-Apr-2007 07:42
Since integers are stored on multiple bytes in the memory in binary format, once an signed long integer reaches the maximum value of 2147483646 (which is the maximum value that 8 bits of memory, size of a long int, can store, binary-wise), atleast on C/C++ the number starts to roll back, going gradually to ...45, ...45 and eventually reaching a negative value. Some REALLY large values will return -1, probably because PHP simply rejects the number, though I'm not sure why.

For further info:
http://www.rwc.uc.edu/koehler/comath/13.html
maciek dot iwanowskis at glasspartnership dot co dot uk 04-Apr-2007 07:36
It seems that if you're trying to cast to integer (or use intval()) on integer of value bigger then 2147483646 it's hard to predict returned value.
27-Apr-2006 07:36
Operating on integers gives puzzling results--which don't seem to have to do with the number of bits...

<?php
echo intval(1e10);  // -1 on my system; 1410065408 in the example
echo intval(1e5); // 100000 (my own example: lower number--works all right)
echo intval(4e9);  // -294967296 ! (my own example: somewhere in the middle)

echo intval(42000000);  // 42000000 (as in the manual; reasonable)
echo intval(420000000000000000000);  // -1 on my system at least. 0 in the example
// ...and yet still odder...
echo intval(4200000000);  // -94967296! (my own example: somewhere in between)
?>
mkamerma at science dot uva dot nl 07-Mar-2006 08:48
As addendum, the "if ($int > 0)" check in the encode function is redundant. It doesn't do anything bad to keep it in since it will always be true when reaching that point, but it's a meaningless conditional this way. It's a remnant from when I tried to write the function in terms of bitshifts, which could lead to negative ints when shifting if the 32nd bit was set (instead of always padding with 0's when using >> it pads with 1's leading to negative ints).
mkamerma at science dot uva dot nl 05-Mar-2006 11:04
When you need to work with integer values that exceed maxint, the following functions may be of use to you - they form a codec pair for integers of variable length rather than fixed length, encoded in a byte as a 7 bit numberal with a 1 bit has-more flag, indicating that the next byte encodes a higher order part of the same number still.

<?php
/* encode integer as 7bit with has-more bit numeral,
    ordered lowest byte first. */
function encode_7bhm($int) {
  if ($int==0) return chr(0); // shortcut
  $ret = "";
  while($int != 0) {
    $high = floor($int / 128);     // overflow for this round
    $low = $int - ($high * 128); // 7 bit numeral
    if ($int > 0) {
      if ($high > 0) { $low = $low + 128; } // has-more flag
      $ret .= chr($low); } //encode
    $int = $high;  // set overflow as next round's number
  }
  return $ret;
}

/* decode a 7bit with has-more bit numeral,
    ordered lowest byte first. */
function decode_7bhm($hmb) {
  $ret = 0;
  $pos = 0;
  $high = 1;
  while($high == 1) {
    $byte = ord(substr($hmb, $pos, 1));
    $high = floor($byte/128); // gets has-more flag
    $low = $byte - ($high*128);
    $ret += $low * pow(128, $pos++); // decode
  }
  return $ret;
}
?>

This codec pair is also quite useful when needing to write ints to files, as this is a low-numeral biased encoding: most of the time this will only require 8 or 16 bit rather than the 32 bits an int will use in fixed-length encoding.

The encoding range:

  1 byte  - 0 through 128 (2^7)
  2 bytes - 129 through 16,384 (2^14)
  3 bytes - 16,385 through 2,097,152 (2^21)
  4 bytes - 2,097,153 through 268,435,456 (2^28)

while indeed a 32 bit encoded variable length integer will be lower than maxint, rather than needing a new 32 bit block to represent higher range only 8 more bits are required to represent this higher number (for completeness the range of representation by bytes 5-8 are listed):

  5 bytes - 268,435,457 through 34,359,738,368 (2^35)
  6 bytes - 343,59,738,369 through 4,398,046,511,104 (2^42)
  7 bytes - 4,398,046,511,105 through 562,949,953,421,312 (2^49)
  8 bytes - 562,949,953,421,313 through 720,57,594,037,927,936 (2^56)

Also for completeness, the function to read a 7 bit with has-more bit from a filepointer:

<?php
// read a 7bhm numeral from file
function read_7bhm($fp) {
  $bytestring = "";
  $high = 1;
  while($high==1) {
    $byte = fread($fp, 1);
    $high = floor(ord($byte)/128); // check for has-more bit
    $bytestring .= $byte;
  }
  return decode_7bhm($bytestring); }
?>
simon at npkk dot cz 30-Jan-2006 05:03
Still have on mind, that if you convert big numbers by adding zero, PHP makes automatic "to a float" conversion, so it is same as floatVal(). So if the number is realy big (over 13 digits), you can lose preciosity. Do not use it for such long numbers, if all bits do matter (IPv6 addresses and similar).
simon at npkk dot cz 30-Jan-2006 03:48
intval() returns maxint on number_in_string, if the result would be bigger than it. But for float returns the signed 32 bit integer with the "same" value:
<?php
$num=-1000;
print($num."\n");
$i_str=sprintf("%u",$num);
print($i_str."\n");
$i1=intval($i_str);
print($i1."\n");
$i2=intval(floatval($i_str));
print($i2."\n");
?>

prints:

-1000
4294966296
2147483647
-1000
anonymous at place dot com 17-Jan-2006 06:37
re: Disturbing issue with intval()

It's probably just good practice to round decimals anyways.  i.e...

$amount = round(19.99 * 100);
$test2 = intVal($amount);
$test3 = intVal("$amount");

echo $test2 . "<br />\n";
echo $test3 . "<br />\n";
Ben Laurienti 16-Jan-2006 10:21
You guys are going to love this.  I found something that I found quite disturbing.

$test1 = intVal(1999);

$amount = 19.99 * 100;
$test2 = intVal($amount);
$test3 = intVal("$amount");

echo $test1 . "<br />\n";
echo $test2 . "<br />\n";
echo $test3 . "<br />\n";

expected output:
1999
1999
1999

actual output
1999
1998
1999

Appears to be a floating point issue, but the number 1999 is the only number that I was able to get to do this.  19.99 is the price of many things, and for our purpose we must pass it as 1999 instead of 19.99.
adspeed.com 29-Jul-2005 05:32
Say you have a string $s="3763328634" to be used as a key into the database, intval() this string will result in a different,smaller number (depends on the machine/OS). To keep the number intact but as an int/long type, do $s +=0; instead.
24-May-2004 05:38
When trying to read 32bit values (32 bit limitation depends on the word size of your machine)  from a string that is represented in hex with the high bit set,
eg. F9833234

intval returns -1. The reason is the sign bit being set. This number is larger than can be saved in a signed int.

The way around this is to read the value in using two calls to intval.

eg.

$val = intval(substr($str,0,4), 16); // read high 16 bit word
$val <<= 16; // shift hi word correct position
$val |= intval(substr($str, 4, 4), 16); //  read low 16 bit word
tuxedobob at mac dot com 07-Feb-2004 01:56
Sometimes intval just won't cut it. For example if you want to use an unsigned 32-bit int and need all 32 bits. Recently, I wrote a little script that took and integer and converted it to an IP address. After realizing I couldn't just mod the whole thing, since the sign bit throws it off (and compensating for that), we ran into a problem where if it was entered into a form, the value somehow wasn't converted to an integer properly, at least not implicitly. The solution for this, and the way I recommend converting a string to an integer, is:

$num = $num + 0;

and PHP will leave your number alone; it'll just know it's a number. Such is the fun of a loosely-typed language. :)
Robin Y. Millette http://rym.waglo.com 15-May-2003 10:29
Rob_Kohr at no_need_to_email dot me dot com
11-Nov-2002 12:24

[snip]

$var=intval("122.5");
//$var==122

This is nice if you want to turn a double into an int automatically rounding down

Hum, I had a bug earlier today, involving ===. Coming from a c++ background, I can't help testing for types. I was using floor() to get an integer from a division by 2, and comparing that to a known integer from a for loop. Well, first I changed the === to == because the test would always be false otherwise. Next, I looked up this function, and converted most of my floor() calls to intval() calls, because I really meant to get an int, and not a float with no decimal part. So I have to disagree with the editor note here. Oh, and I'm comfortably back to using ===.
zak at php dot net 12-Aug-2000 10:38
intval converts doubles to integers by truncating the fractional component of the number.

When dealing with some values, this can give odd results.  Consider the following:

print intval ((0.1 + 0.7) * 10);

This will most likely print out 7, instead of the expected value of 8.

For more information, see the section on floating point numbers in the PHP manual (http://www.php.net/manual/language.types.double.php)

Also note that if you try to convert a string to an integer, the result is often 0.

However, if the leftmost character of a string looks like a valid numeric value, then PHP will keep reading the string until a character that is not valid in a number is encountered.

For example:

"101 Dalmations" will convert to 101

"$1,000,000" will convert to 0 (the 1st character is not a valid start for a number

"80,000 leagues ..." will convert to 80

"1.4e98 microLenats were generated when..." will convert to 1.4e98

Also note that only decimal base numbers are recognized in strings.

"099" will convert to 99, while "0x99" will convert to 0.

One additional note on the behavior of intval.  If you specify the base argument, the var argument should be a string - otherwise the base will not be applied.

For Example:

print intval (77, 8);   // Prints 77
print intval ('77', 8); // Prints 63


변수가 배열인지 확인

  • ≪ intval
  • is_bool ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수가 배열인지 확인

is_array

(PHP 4, PHP 5, PHP 7)

is_array — 변수가 배열인지 확인

설명

bool is_array ( mixed $var )

주어진 변수가 배열인지 확인합니다.

인수

var

평가할 변수.

반환값

var가 array이면 TRUE, 아니면 FALSE를 반환합니다.

예제

Example #1 변수가 배열인지 확인하기

<?php
$yes = array('this', 'is', 'an array');

echo is_array($yes) ? 'Array' : 'not an Array';
echo "\n";

$no = 'this is a string';

echo is_array($no) ? 'Array' : 'not an Array';
?>

위 예제의 출력:

Array
not an Array

참고

  • is_float() - 변수의 자료형이 소수인지 확인합니다
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • is_object() - 변수가 객체인지 확인합니다

User Contributed Notes

marko at newvibrations dot net 20-Oct-2016 09:08
hperrin at gmail dot com
i tested modified version of your code:

<?php
$test = array_fill(0, $size, rand());
$test3 = (object) $test;
?>

Results are quite different!

PHP 5.5:
is_array - execution time is constant over different $size
cast, === - execution time is proportional with $size, for small arrays, it's faster than is_array, but for bigger arrays, it's way slower. Also, bigger memory allocation because casting actually allocates memory

PHP 7.0:
is_array and cast, === have constant execution time over different $size:
is_array  :  0.12564396858215
cast, === :  0.24629783630371

Looks like casting is optimised, but still slower.

Bottom line is: optimisation is not done for special scenario, but for average use and scalability. Don't reinvent wheel.
Rob Thomas 06-Oct-2016 07:40
Please note that the 'cast to array' check is horrendously out of date.

Running that code against PHP 5.6 results in this:

is_array  :  0.93975400924683
cast, === :  1.2425191402435

So, please use 'is_array', not the horrible casting hack.
mt at banita dot de 08-Sep-2016 06:30
I'm using PHP 5.3.10 and the remark from hperrin is not (no longer?) valid

is_array is (now?) faster than if ( (array) $unknown !== $unknown )

my result of hperrins test script

is_array  :  0.61710119247437
cast, === :  1.1551909446716
ari at umbrella dot bz 06-Sep-2016 05:34
hperrin's results have indeed changed in PHP 7. The opposite is now true, is_array is faster than comparison:

is_array  :  0.52148389816284
cast, === :  0.84179711341858

Tested 1000000 iterations.
mbrodala at pagemachine dot de 24-Aug-2016 10:22
Notice that the benchmark results from hperrin at gmail dot com have changed in the meantime:

is_array  :  0.31888604164124
cast, === :  0.58448791503906

(Using PHP 5.6.24, I expect other results with PHP 7)
dmitry dot sushilov at gmail dot com 12-Oct-2014 09:49
function is_associate_array($array)
{
    return $array === array_values($array);
}

or you can add check is_array in functions
joksnet at gmail dot com 09-Oct-2013 07:17
Here is something for compare `is_assoc` functions.

<?php
function is_assoc1($var)
{
    return is_array($var) && array_diff_key($var, array_keys(array_keys($var)));
}

function is_assoc2($var)
{
    return is_array($var) && array_diff_key($var, array_fill(0, count($var), null));
}

$count = 1000000;

$a = array('a' => 'foo', 'b' => 1, 'c' => true);
$b = array('a', 'b', 'c');

$i = $start_time = $end_time = 0;

$start_time = microtime(true);
for ($i = 0; $i < $count; $i++) {
    assert(is_assoc1($a) === true);
    assert(is_assoc1($b) === false);
}
$end_time = microtime(true);
echo 'is_assoc1  :  ' . ($end_time - $start_time) . "\n";

$start_time = microtime(true);
for ($i = 0; $i < $count; $i++) {
    assert(is_assoc2($a) === true);
    assert(is_assoc2($b) === false);
}
$end_time = microtime(true);
echo 'is_assoc2  :  ' . ($end_time - $start_time) . "\n";
?>

It prints something like this:

```
% php is_assoc.php
is_assoc1  :  19.36315202713
is_assoc2  :  18.954080104828
```

The first one is from
skaimauve (http://www.php.net/manual/en/function.is-array.php#89332). The second one is mine.
magentix at gmail dot com 26-Feb-2011 06:12
If you wanted to have 'pure' associative arrays,
you could filter out non-string keys and then compare the count of the original array with the count of the filtered array.

<?php
    // Only validates empty or completely associative arrays
    function is_assoc ($arr) {
        return (is_array($arr) && count(array_filter(array_keys($arr),'is_string')) == count($arr));
    }

    $a = '';
    $b = 'z';
    $c = array();
    $d = array('x','y','z');
    $e = array('x','y','zed'=>'z');
    $f = array('ex'=>'x','why'=>'y','zed'=>'z');

    echo '$a: ' . ( (is_assoc($a)) ? 'true' : 'false' ) ."\n"; // False
    echo '$b: ' . ( (is_assoc($b)) ? 'true' : 'false' ) ."\n"; // False
    echo '$c: ' . ( (is_assoc($c)) ? 'true' : 'false' ) ."\n"; // True
    echo '$d: ' . ( (is_assoc($d)) ? 'true' : 'false' ) ."\n"; // False
    echo '$e: ' . ( (is_assoc($e)) ? 'true' : 'false' ) ."\n"; // False
    echo '$f: ' . ( (is_assoc($f)) ? 'true' : 'false' ) ."\n"; // True
?>

I've tested the point of checking whether the array is empty first.
The overall result was a 200% speed increase when the array was indeed empty,
but an average 10% slow when it had elements inside.

<?php
    /***********************************************************
     *    Benchmarks: checking for empty array vs. not checking  *
     ***********************************************************/

    // With empty check
    function is_assoc ($arr) {
        return (is_array($arr) && (!count($arr) || count(array_filter(array_keys($arr),'is_string')) == count($arr)));
    }

    function test_speed($arr) {
        $t = microtime(true);
        for($i = 0; $i < 100000; ++$i) is_assoc($arr);
        return (microtime(true) - $t);
    }

    echo '$a: ' . test_speed($a) ."\n"; // 0.20597505569458
    echo '$b: ' . test_speed($b) ."\n"; // 0.19199514389038
    echo '$c: ' . test_speed($c) ."\n"; // 0.28803396224976
    echo '$d: ' . test_speed($d) ."\n"; // 0.94685983657837
    echo '$e: ' . test_speed($e) ."\n"; // 0.96698403358459
    echo '$f: ' . test_speed($f) ."\n"; // 0.98052096366882

    // Without empty check
    function is_assoc_2 ($arr) {
        return (is_array($arr) && count(array_filter(array_keys($arr),'is_string')) == count($arr));
    }

    function test_speed_2($arr) {
        $t = microtime(true);
        for($i = 0; $i < 100000; ++$i) is_assoc_2($arr);
        return (microtime(true) - $t);
    }

    echo '$a: ' . test_speed_2($a) ."\n"; // 0.18811202049255 = FASTER:  91.3% of is_assoc
    echo '$b: ' . test_speed_2($b) ."\n"; // 0.18782901763916 = FASTER:  97.8% of is_assoc
    echo '$c: ' . test_speed_2($c) ."\n"; // 0.64437484741211 = SLOWER: 223.7% of is_assoc
    echo '$d: ' . test_speed_2($d) ."\n"; // 0.84632205963135 = FASTER:  89.4% of is_assoc
    echo '$e: ' . test_speed_2($e) ."\n"; // 0.86739897727966 = FASTER:  89.7% of is_assoc
    echo '$f: ' . test_speed_2($f) ."\n"; // 0.87963700294495 = FASTER:  89.7% of is_assoc
?>
JTS 08-Jun-2010 01:22
I would change the order of the comparison, because if it is really an empty array, it is better to stop at that point before doing several 'cpu & memory intensive' function calls.

In the end on a ratio of 3 not empty arrays to 1 empty array computed for 1000000 iterations it needed 10% less time.
Or the other way round:
It needed approx 3% to 4% more time if the array is not empty, but was at least 4 times faster on empty arrays.

Additionally the memory consumption veritably lesser.

<?php
function is_assoc($array) {
    return (is_array($array) && (count($array)==0 || 0 !== count(array_diff_key($array, array_keys(array_keys($array))) )));
}
?>
hperrin at gmail dot com 28-May-2010 07:02
I've found a faster way of determining an array. If you use is_array() millions of times, you will notice a *huge* difference. On my machine, this method takes about 1/4 the time of using is_array().

Cast the value to an array, then check (using ===) if it is identical to the original.

<?php
if ( (array) $unknown !== $unknown ) {
    echo '$unknown is not an array';
} else {
    echo '$unknown is an array';
}
?>

You can use this script to test the speed of both methods.

<pre>
What's faster for determining arrays?

<?php

$count = 1000000;

$test = array('im', 'an', 'array');
$test2 = 'im not an array';
$test3 = (object) array('im' => 'not', 'going' => 'to be', 'an' => 'array');
$test4 = 42;
// Set this now so the first for loop doesn't do the extra work.
$i = $start_time = $end_time = 0;

$start_time = microtime(true);
for ($i = 0; $i < $count; $i++) {
    if (!is_array($test) || is_array($test2) || is_array($test3) || is_array($test4)) {
        echo 'error';
        break;
    }
}
$end_time = microtime(true);
echo 'is_array  :  '.($end_time - $start_time)."\n";

$start_time = microtime(true);
for ($i = 0; $i < $count; $i++) {
    if (!(array) $test === $test || (array) $test2 === $test2 || (array) $test3 === $test3 || (array) $test4 === $test4) {
        echo 'error';
        break;
    }
}
$end_time = microtime(true);
echo 'cast, === :  '.($end_time - $start_time)."\n";

echo "\nTested $count iterations."

?>
</pre>

Prints something like:

What's faster for determining arrays?

is_array  :  7.9920151233673
cast, === :  1.8978719711304

Tested 1000000 iterations.
Anonymous 16-May-2009 11:18
Using empty() in the previous example posted by Anonymous will result in a "Fatal error: Can't use function return value in write context".  I suggest using count() instead:

<?php
function is_assoc($array) {
    return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
}
?>
skaimauve at yahoo dot ca 03-Mar-2009 09:12
Or you could make use of the array_diff_key and array_key function:

<?php

function is_assoc($var)
{
        return is_array($var) && array_diff_key($var,array_keys(array_keys($var)));
}

function test($var)
{
        echo is_assoc($var) ? "I'm an assoc array.\n" : "I'm not an assoc array.\n";
}

// an assoc array
$a = array("a"=>"aaa","b"=>1,"c"=>true);
test($a);

// an array
$b = array_values($a);
test($b);

// an object
$c = (object)$a;
test($c);

// other types
test($a->a);
test($a->b);
test($a->c);

?>

The above code outputs:
I'm an assoc array.
I'm not an assoc array.
I'm not an assoc array.
I'm not an assoc array.
I'm not an assoc array.
I'm not an assoc array.
Matthias Loitsch 03-Dec-2008 08:36
Or you could make use of the array_diff_key and range functions:

<?php

function isVector($var) { return count(array_diff_key($var, range(0, count($var) - 1))) == 0; }

function isAssociative($var) { return !isVector($var); }

?>
jim at akubo dot net 28-Oct-2008 08:02
yousef's example was wrong because is_vector returned true instead of false if the key was found
here is the fixed version (only 2 lines differ)
<?php
function is_vector( &$array ) {
   if ( !is_array($array) || empty($array) ) {
      return -1;
   }
   $next = 0;
   foreach ( $array as $k => $v ) {
      if ( $k !== $next ) return false;
      $next++;
   }
   return true;
}
?>
yousuf at philipz dot com 17-Oct-2008 12:41
alex frase's example is fast but elanthis at awesomeplay dot com's example is faster and Ilgar's modification of alex's code is faulty (the part " || $_array[$k] !== $v"). Also, Ilgar's suggestion of giving a false return value when the variable isnt an array is not suitable in my opinion and i think checking if the array is empty would also be a suitable check before the rest of the code runs.

So here's the modified (is_vector) version

<?php
function is_vector( &$array ) {
   if ( !is_array($array) || empty($array) ) {
      return -1;
   }
   $next = 0;
   foreach ( $array as $k => $v ) {
      if ( $k !== $next ) return true;
      $next++;
   }
   return false;
}
?>

and the modified (alex's is_assoc) version

<?php
function is_assoc($_array) {
    if ( !is_array($_array) || empty($array) ) {
        return -1;
    }
    foreach (array_keys($_array) as $k => $v) {
        if ($k !== $v) {
            return true;
        }
    }
    return false;
}
?>
alex frase 16-Jul-2008 05:05
Yet another simpler, faster is_assoc():

<?php
function is_assoc($array) {
  foreach (array_keys($array) as $k => $v) {
    if ($k !== $v)
      return true;
  }
  return false;
}
?>

In my tests it runs about twice as fast as Michael/Gabriel's array_reduce() method.

(Speaking of which: Gabriel's version doesn't work as written; it reports associative arrays as numeric if only the first key is non-numeric, or if the keys are numeric but ordered backwards.  Michael solves this problem by comparing array_reduce() to count(), but that costs another function call; it also works to just compare to -1 instead of 0, and therefore return -1 as the ternary else from the callback).
Michael 11-Sep-2007 09:37
A slight modification of what's below:

<?php

function is_assoc($array)
{
    return is_array($array) && count($array) !== array_reduce(array_keys($array), 'is_assoc_callback', 0);
}

function is_assoc_callback($a, $b)
{
    return $a === $b ? $a + 1 : 0;
}

?>
gabriel at bumpt dot nothing-here dot net 29-Aug-2007 05:34
Yet another safer, faster way of detecting whether an array is associative.

The principle is: using array reduction on the keys, we verify that each key is numeric and is equal to its rank.

Beware: integer keys that are not in sequence, or are negative, or with "holes", still make an associative array.

<?php
/**
  * @param array $arr
  * @returns boolean
  */
function isNotAssocArray($arr)
{
    return (0 !== array_reduce(
        array_keys($arr),
        create_function('$a, $b', 'return ($b === $a ? $a + 1 : 0);'),
        0
        )
    );
}
?>

Of course, it is still faster if the callback for array_reduce is not an anonymous function:

<?php
function callbackReduceNotArray($a, $b)
{
    return ($b === $a ? $a + 1 : 0);
}
function isVector($arr)
{
    return (0 !== array_reduce(
        array_keys($arr),
        'callbackReduceNotArray',
        0
        )
    );
}
?>
Alfred J Fazio <alfred dot fazio at gmail dot com> 05-Apr-2007 01:32
Yet another associative array test:

<?php
function binary_nand ($a, $b) { return !$a && !$b; }
function binary_nor ($a, $b)  { return !$a || !$b; }

// Returns true if array has elements with non-numeric keys
function is_associative_array ($arr) {
  return is_array($arr) && !empty($arr) && array_reduce(array_map("is_numeric", array_keys($arr)), "binary_nor", true);
}

// Returns true if all elements of array have a non-numeric key
function is_strict_associative_array ($arr) {
  return is_array($arr) && !empty($arr) && array_reduce(array_map("is_numeric", array_keys($arr)), "binary_nand", false);
}
?>
angelo [at] mandato <dot> com 26-Feb-2007 10:32
The is_associative_array() and is_sequential_array() functions posted by 'rjg4013 at rit dot edu' are not accurate.

The functions fail to recognize indexes that are not in sequence or in order.  For example, array(0=>'a', 2=>'b', 1=>'c') and array(0=>'a', 3=>'b', 5=>'c') would be considered as sequential arrays. A true sequential array would be in consecutive order with no gaps in the indices.

The following solution utilizes the array_merge properties. If only one array is given and the array is numerically indexed, the keys get re-indexed in a continuous way.  The result must match the array passed to it in order to truly be a numerically indexed (sequential) array.  Otherwise it can be assumed to be an associative array (something unobtainable in languages such as C).

The following functions will work for PHP >= 4.

<?php
    function is_sequential_array($var)
    {
        return (array_merge($var) === $var && is_numeric( implode( array_keys( $var ) ) ) );
    }

    function is_assoc_array($var)
    {
        return (array_merge($var) !== $var || !is_numeric( implode( array_keys( $var ) ) ) );
    }
?>

If you are not concerned about the actual order of the indices, you can change the comparison to == and != respectively.
jupiter at nospam dot com 30-May-2006 08:42
Will check a Multi-Dimentional Array to any specified level.  This is a fix to 11/16/05 submission, which would break since you must supply a foreach with an array. Beware recursive functions shouldn't go over 100 deep or could break the memory stack on server.

<?php
// checks for multiarray to defined depth level recursively
// original $level must be 2 or more, else will instantly return true
function isDeepMultiArray($multiarray, $level = 2) {  // default is simple multiarray
    if (is_array($multiarray)) {  // confirms array
        if ($level == 1) {  // $level reaches 1 after specified # of recursions
            return true;  // returns true to recursive function conditional
        }  // end conditional
        foreach ($multiarray as $array) {  // goes one level deeper into array
            if (isDeepMultiArray($array, $level - 1)) {  // check subarray
                $message = "I'm a multiarray";  // optional message
                return $message;  // best if $message = true so function returns boolean
            }  // end recursive function
        }  // end loop
    } else {  // not an array at specified level
    return false;  // is also used recursively so can't change to message
    }
}

if (isDeepMultiArray(array(array()), 2)); // beware this returns true eventhough arrays are empty

?>
BTW my notation is consistent with the PEAR manual on coding standards, which is what php.net says to follow. I hope a function like this gets included in PHP6.
jupiter at nospam dot com 30-May-2006 06:15
Simple check for a Multi-Dimentional Array of any depth
<?php
// checks for multiarray (2 or more levels deep)
function isMultiArray($multiarray) {
  if (is_array($multiarray)) {  // confirms array
    foreach ($multiarray as $array) {  // goes one level deeper
      if (is_array($array)) {  // is subarray an array
        return true;  // return will stop function
      }  // end 2nd check
    }  // end loop
  }  // end 1st check
  return false;  // not a multiarray if this far
}
?>
March 30-Mar-2006 03:28
And here is another variation for a function to test if an array is associative. Based on the idea by mot4h.

<?php
function is_associative($array)
{
  if (!is_array($array) || empty($array))
    return false;

  $keys = array_keys($array);
  return array_keys($keys) !== $keys;
}
?>
dan at cain dot sh 11-Dec-2004 02:05
is_array() under PHP 5.0.2 will return FALSE when passed an object descended from the internal class interface ArrayAccess(http://www.php.net/spl) even though said object behaves as an array would in most instances.

I've found the following user function helpful with my own classes and functions that expect array(s) as arguments, but work fine with objects that behave as an array would.

<?php
function is_array_abled(&$x)
{
    return (bool)($x instanceof ArrayAccess or is_array($x));
}
?>
vhermecz at ixpert dot hu 02-Apr-2004 01:58
Mike's function is quite cool, it is just the one, I was searching for. Using range is a great idea! But it's a bit long for me. Here is a shorter version:

<?php
function is_assoc_array($var) {
    if (!is_array($var)) {
        return false;
    }
    return array_keys($var)!==range(0,sizeof($var)-1);
}
?>

Or, if you don't want to type that much:

<?php
function is_assoc($var) {
    return is_array($var) && array_keys($var)!==range(0,sizeof($var)-1);
}
?>


변수가 논리형인지 확인

  • ≪ is_array
  • is_callable ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수가 논리형인지 확인

is_bool

(PHP 4, PHP 5, PHP 7)

is_bool — 변수가 논리형인지 확인

설명

bool is_bool ( mixed $var )

주어진 변수가 논리형인지 확인합니다.

인수

var

평가할 변수.

반환값

var가 boolean이면 TRUE, 아니면 FALSE를 반환합니다.

예제

Example #1 is_bool() 예제

<?php
$a = false;
$b = 0;

// $a는 논리형이기 때문에, true입니다.
if (is_bool($a)) {
    echo "논리형입니다.";
}

// $b는 논리형이 아니기 때문에, true가 아닙니다.
if (is_bool($b)) {
    echo "논리형입니다.";
}
?>

참고

  • is_float() - 변수의 자료형이 소수인지 확인합니다
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • is_object() - 변수가 객체인지 확인합니다
  • is_array() - 변수가 배열인지 확인

User Contributed Notes

Michael Smith 17-Nov-2013 10:17
The function in the last comment also converts the string 'false' to a true boolean. Here's a function that does what I think was intended:

<?php
function toBool($var) {
  if (!is_string($var)) return (bool) $var;
  switch (strtolower($var)) {
    case '1':
    case 'true':
    case 'on':
    case 'yes':
    case 'y':
      return true;
    default:
      return false;
  }
}
vincent at lycoops dot be 28-Apr-2012 09:22
For those of you who think that micro-optimization is important:

It's quicker to strictly compare a value to TRUE and FALSE, than using is_bool(), because it's a function.

<?php
if (is_bool($var)) { // 100% of time
}

if ($var === true || $var === false) { // 50% of time, if $var is true and 75% of time if $var is false.

}
?>
moty66 at gmail dot com 20-Aug-2009 11:18
Here is the function which I use when pasring boolean variables from config files

<?php
function boolVal($var) {
    switch ($var) {
        case $var == true:
        case $var == 1:
        // case $var == '1': // no need for this, because we used
                             // $val == 1 not $var === 1
        case strtolower($var) == 'true':
        case strtolower($var) == 'on':
        case strtolower($var) == 'yes':
        case strtolower($var) == 'y':
            $out = 1;
            break;
        default: $out = 0;
    }

    return $out;
}

?>

I am woundering why php does not have a function to do this

Motaz Abuthiab
sam+nospam at samuellevy dot com 02-Feb-2009 01:30
Just another little function which doesn't exist yet, but I find mighty useful, especially when working with AJAX and APIs.

<?php
/** Checks a variable to see if it should be considered a boolean true or false.
 *     Also takes into account some text-based representations of true of false,
 *     such as 'false','N','yes','on','off', etc.
 * @author Samuel Levy <sam+nospam@samuellevy.com>
 * @param mixed $in The variable to check
 * @param bool $strict If set to false, consider everything that is not false to
 *                     be true.
 * @return bool The boolean equivalent or null
 */
function boolval($in, $strict=false) {
    $out = null;
    // if not strict, we only have to check if something is false
    if (in_array($in,array('false', 'False', 'FALSE', 'no', 'No', 'n', 'N', '0', 'off',
                           'Off', 'OFF', false, 0, null), true)) {
        $out = false;
    } else if ($strict) {
        // if strict, check the equivalent true values
        if (in_array($in,array('true', 'True', 'TRUE', 'yes', 'Yes', 'y', 'Y', '1',
                               'on', 'On', 'ON', true, 1), true)) {
            $out = true;
        }
    } else {
        // not strict? let the regular php bool check figure it out (will
        //     largely default to true)
        $out = ($in?true:false);
    }
    return $out;
}
?>

It may be pretty inefficient, but it does the job.
bb at kingdom dot cjb dot net 27-Jun-2007 11:57
@ emmanuel del grande

You don't need to break when you return...

function bool($var) {
    switch (strtolower($var)) {
        case ("true"): return true;
        case ("false"): return false;
        default: die("whatever you want it to tell");
    }
}
kevin at metalaxe dot com 05-Jun-2007 10:32
@ emanueledelgrande at email dot it

http://us.php.net/manual/en/language.types.type-juggling.php

(bool) or (boolean) is allowed for type casting a variable. No function like intval, etc but the functionality exists.
emanueledelgrande at email dot it 22-May-2007 06:12
I think there's a hole in the PHP typecasting methods:
you have the (int) function, the (float) function and the (string) function, but no function to force a string variable into the boolean type.

It's obvious that forcing unconditionally the type of variables into arrays and objects is inappropriate, but boolean type is the most basic one for each programming language, that's why I guessed that a (bool) function already existed.

Moreover, with the increasing trend of RSS data streaming, the parsing of an XML string into an object often requires to typecast as boolean values the content of XML tags, normally returned as string by the object method get_content().

I wrote the following function, which also uses a "native PHP style" error message:

<?php
// strings tyecasting as boolean values:
function bool($var) {
    switch (strtolower($var)) {
        case ("true"):
            return true;
            break;
        case ("false"):
            return false;
            break;
        default:
            die("<br />\n<b>Warning:</b> Invalid argument supplied for ".__FUNCTION__." function in <b>".__FILE__."</b> on line <b>".__LINE__."</b>: the argument can contain only 'true' or 'false' values as a string.<br />\n");
    }
}
?>

Here it is a small example:

<?php
$xmlResponse = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xmlResponse .= "<Result>";
$xmlResponse .= "<AuthError>false</AuthError>";
$xmlResponse .= "<TransferStatus>true</TransferStatus>";
$xmlResponse .= "</Result>";

if (! $responseDoc = domxml_open_mem($xmlResponse, DOMXML_LOAD_PARSING, $XmlParsingError)) {
    echo "Error while parsing the XML string:<br />".print_r($XmlParsingError, TRUE);
} else {
    $ResultNode = $responseDoc->get_elements_by_tagname('Result');

    $AuthError = $ResultNode[0]->get_elements_by_tagname('AuthError');
    $auth_error = bool($AuthError[0]->get_content());

    $TransferStatus = $ResultNode[0]->get_elements_by_tagname('TransferStatus');
    $transfer_status = bool($TransferStatus[0]->get_content());

    if (! $auth_error) { echo "Auth OK<br />"; } else { echo "Auth error<br />"; }
    if ($transfer_status) { echo "Transfer OK<br />"; } else { echo "Transfer error<br />"; }
}

?>

It would be useful this function to be implemented in the core of PHP5.
rh at richardhoward dot net 22-May-2005 08:17
punkpuke is wrong here; what he means to say is that empty($x) is the opposite of (bool)$x.  is_bool($x) returns true where $x === false.


변수 내용을 함수처럼 호출할 수 있는지 확인

  • ≪ is_bool
  • is_double ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수 내용을 함수처럼 호출할 수 있는지 확인

is_callable

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

is_callable — 변수 내용을 함수처럼 호출할 수 있는지 확인

설명

bool is_callable ( mixed $var [, bool $syntax_only [, string &$callable_name ]] )

변수의 내용이 함수처럼 호출할 수 있는지 확인합니다. 변수가 유효한 함수의 이름을 포함하고 있는지, 혹은 인코드된 객체와 함수 이름을 가지는 배열인지를 간단히 확인할 수 있습니다.

인수

var

문자열 변수에 저장한 함수의 이름이거나, 객체 안의 객체와 메쏘드의 이름일 수 있습니다. 예:

array($SomeObject, 'MethodName')

syntax_only

TRUE로 설정하면, 이 함수는 var가 함수나 메쏘드일 수 있는지만 확인합니다. 이는 단순히 문자열이 아닌 변수나 유효하게 콜백에 사용할 수 있는 구조가 아닌 배열을 거부합니다. 유효한 것은 2 엔트리뿐이며, 첫번째는 객체나 문자열, 두번째는 문자열입니다.

callable_name

"호출할 수 있는 이름"을 받습니다. 아래의 예제에서 이는 "someClass:someMethod"입니다. 그러나 somClass::SomeMethod()가 호출할 수 있는 정적 메쏘드라면, 불가능하다는 점에 주의하십시오.

반환값

var를 호출할 수 있으면 TRUE, 아니면 FALSE를 반환합니다.

예제

<?php
//  변수를 함수처럼 호출할 수 있는지
//  체크하는 방법.

//
//  함수를 포함하는 간단한 변수
//

function someFunction()
{
}

$functionVariable = 'someFunction';

var_dump(is_callable($functionVariable, false, $callable_name));  // bool(true)

echo $callable_name, "\n";  // someFunction

//
//  메쏘드를 포함하는 배열
//

class someClass {

  function someMethod()
  {
  }

}

$anObject = new someClass();

$methodVariable = array($anObject, 'someMethod');

var_dump(is_callable($methodVariable, true, $callable_name));  //  bool(true)

echo $callable_name, "\n";  //  someClass:someMethod

?>

참고

  • function_exists() - Return TRUE if the given function has been defined
  • method_exists() - 클래스 메쏘드가 존재하는지 확인

User Contributed Notes

heah at heahprod dot com 21-Apr-2016 08:48
I don't know if it is a bug but when you test an array with a class and method, is_callable returns true for non static method.

Consider the following code:
<?php

class A
{
    public static function test()
    {
        echo 'test', '<br>';
    }

    public function hello()
    {
        echo 'hello', '<br>';
    }
}

echo "Static #1: call_user_func(array('A', 'test')) => ", call_user_func(array('A', 'test')), '<br>';
echo 'expect is_callable TRUE => ',
    is_callable(array('A', 'test'))
        ? 'TRUE, A::test() is callable statically'
        : 'FALSE, A::test() is not callable statically',
    '<br><br>'
;

echo "Static #2: call_user_func(array('A', 'hello')) => ", call_user_func(array('A', 'hello')), '<br>';
echo 'expect is_callable FALSE => ',
    is_callable(array('A', 'hello'))
        ? 'TRUE, A::hello() is callable statically'
        : 'FALSE, A::hello() is not callable statically',
    '<br><br>'
;

$a = new A();

echo "Instance #1: call_user_func(array(\$a, 'test')) => ", call_user_func(array($a, 'test')), '<br>';
echo 'expect is_callable TRUE => ',
    is_callable(array($a, 'test'))
        ? 'TRUE, $a::test() is callable'
        : 'FALSE, $a::test() is not callable',
    '<br><br>'
;

echo "Instance #2: call_user_func(array(\$a, 'hello')) => ", call_user_func(array($a, 'hello')), '<br>';
echo 'expect is_callable FALSE => ',
    is_callable(array($a, 'hello'))
        ? 'TRUE, $a::hello() is callable'
        : 'FALSE, $a::hello() is not callable',
    '<br><br>'
;

?>

Will output:
Static #1: call_user_func(array('A', 'test')) => test

expect is_callable TRUE => TRUE, A::test() is callable statically

Static #2: call_user_func(array('A', 'hello')) =>
Strict Standards: call_user_func() expects parameter 1 to be a valid callback, non-static method A::hello() should not be called statically in test.php on line 24
hello

expect is_callable FALSE => TRUE, A::hello() is callable statically

Instance #1: call_user_func(array($a, 'test')) => test

expect is_callable TRUE => TRUE, $a::test() is callable

Instance #2: call_user_func(array($a, 'hello')) => hello

expect is_callable TRUE => TRUE, $a::hello() is callable

------------------------------------------------------------

Also note that this works the same for inherited methods (I read other posts suggesting otherwise), and that private methods (static, inherited or both) always return false in any case as expected.

Tested with php 5.6.20.
izharaazmi at gmail dot com 11-Jan-2016 03:50
If the target class has __call() magic function implemented, then is_callable will ALWAYS return TRUE for whatever method you call it.
is_callable does not evaluate your internal logic inside __call() implementation (and this is for good).
Therefore every method name is callable for such classes.

Hence it is WRONG to say (as someone said):
...is_callable will correctly determine the existence of methods made with __call...

Example:
<?php
class TestCallable
{
    public function testing()
    {
          return "I am called.";
    }

    public function __call($name, $args)
    {
        if($name == 'testingOther')
        {
                return call_user_func_array(array($this, 'testing'), $args);
        }
    }
}

$t = new TestCallable();
echo $t->testing();      // Output: I am called.
echo $t->testingOther(); // Output: I am called.
echo $t->working();      // Output: (null)

echo is_callable(array($t, 'testing'));       // Output: TRUE
echo is_callable(array($t, 'testingOther'));  // Output: TRUE
echo is_callable(array($t, 'working'));       // Output: TRUE, expected: FALSE
?>
ffalek at banfi dot pl 14-Mar-2015 02:42
Example how is_callable works:

<?php
$counter = 0;

class TestClassOne {
    function testMethod(){
        global $counter;
        return ++$counter;
    }
    static function testMethodStatic(){
        global $counter;
        return ++$counter;
    }
}

class TestClassTwo {
    function __invoke() {
        global $counter;
        return ++$counter;
    }
}

function test_function() {
    global $counter;
    return ++$counter;
}

$p1 = 'test_function';
$p2 = new TestClassOne();
$p3 = ['TestClassOne', 'testMethodStatic'];
$p4 = [new TestClassOne(), 'testMethod'];
$p5 = new TestClassTwo();
$p6 = function() {
    global $counter;
    return ++$counter;
};

foreach ([$p1, $p2, $p3, $p4, $p5, $p6] as $p) {
    if(is_callable($p)){
        echo 'is invokable: yes '.$p().PHP_EOL;
    } else {
        echo 'is invokable: no'.PHP_EOL;
    }
}
?>

And output is:

is invokable: yes 1
is invokable: no
is invokable: yes 2
is invokable: yes 3
is invokable: yes 4
is invokable: yes 5
wowkise at gmail dot com 07-Mar-2013 07:10
beware calling is_callable on class that has the same method name will return false, i filed a bug report to be included in function description as example

<?php
class test {
 function test() {
 }
}
$class = new test();
var_dump(is_callable([$class, 'test']));
?>

will return false
np dot liamg at gmail dot com 08-Dec-2012 10:52
is_callable() will try __autoload(), if have one.
Athari 13-May-2012 12:58
For closures, the function will return true and $callable_name will be set to "Closure::__invoke".
passivesmoking AT yahoo DOT com 07-Jan-2012 03:06
is_callable doesn't seem able to resolve namespaces.  If you're passing a string, then the string has to include the function's full namespace.

<?php
namespace foo\bar\baz;

function something ()
{
    return (42);
}

var_dump (is_callable ('something')); // false
var_dump (is_callable ('foo\bar\baz\something')); // true
?>

It's easy to forget, but if you just prepend __NAMESPACE__ to your function name strings you should be fine in most cases.
fgm at osinet dot fr 27-Jun-2011 02:03
Note that, for the purpose of this function, an abstract method, although necessarily non-callable since it does not have a body, is still considered to be callable:

<?php
abstract class Foo {
  abstract function bar();
}

echo is_callable(array('Foo', 'bar'));
// display: 1
?>
Andy at txtNation dot com 27-Jun-2011 12:50
Note that is_callable is aware of context, and you can ask it things like parent::__construct from within a child constructor

<?php

    class TestClass extends TestClassParent {

        /** @brief Object initialisation callback
            @returns void */
        public function __construct() {

            # do initialisation

            # ...

            # if we have a parent

            if(is_callable('parent::__construct')) {

                # then bubble up

                parent::__construct();
            }
        }
    }
?>
colin 03-Oct-2010 08:30
I haven't seen anyone note this before, but is_callable will correctly determine the existence of methods made with __call. The method_exists function will not.

Example:
<?php

class Test {

    public function testing($not = false) {
        $not = $not ? 'true' : 'false';
        echo "testing - not: $not<br/>";
    }

    public function __call($name, $args) {
        if(preg_match('/^not([A-Z]\w+)$/', $name, $matches)) {
            $fn_name = strtolower($matches[1]);
            if(method_exists($this, $fn_name)) {
                $args[] = true; // add NOT boolean to args
                return call_user_func_array(array($this, $matches[1]), $args);
            }
        }
        die("No method with name: $name<br/>");
    }

}

$t = new Test();
$t->testing();
$t->notTesting();

echo "exists: ".method_exists($t, 'notTesting').'<br/>';
echo "callable: ".is_callable(array($t, 'notTesting'));

?>

Output:

testing - not: false
testing - not: true
exists:
callable: 1
Constantin Kpplinger 10-Jun-2010 09:16
It seems the only issue left with is_callable() is the disable_functions ini-setting. Apart from that, is_callable() will reliably evaluate whether the passed function or method can be called from within the same context is_callable() was called from, taking visibility and inheritance into account. This includes functions, regular and static methods, magic functions and methods and implemented interfaces (which are regular methods within the implementing class anyway).

<?php
function regular_function() {}

abstract class ParentTest
{
    public function public_parent_method() {}
    protected function protected_parent_method() {}
    public static function static_method() {}
    protected static function protected_static_method() {}
}

class CallableTest extends ParentTest implements Countable
{
    public function __invoke() { } // Introduced in 5.3, see http://php.net/manual/language.oop5.magic.php
    protected function protected_method() { }
    public function is_callable($args)
    {
        return is_callable($args);
    }
    // Countable
    public function count()
    {
        return 1;
    }
}

$o = new CallableTest();

// Regular function:
var_dump(is_callable('regular_function')); // true

// Magic __invoke method:
var_dump(is_callable($o)); // true if PHP >= 5.3, false otherwise

// Countable implementation (regular method really):
var_dump(is_callable(array($o, 'count'))); // true

// Protected method from outside the object's scope:
var_dump(is_callable(array($o, 'protected_method'))); // false

// Protected method from inside the object's scope via public proxy method:
var_dump($o->is_callable(array($o, 'protected_method'))); // true

// Parent's public method
var_dump(is_callable(array($o, 'public_parent_method'))); // true

// Parent's protected method
var_dump(is_callable(array($o, 'protected_parent_method'))); // false

// Parent's protected method via proxy
var_dump($o->is_callable(array($o, 'protected_parent_method'))); // true

// Parent's static public method
var_dump(is_callable('CallableTest::static_method')); // true

// Parent's static protected method
var_dump(is_callable('CallableTest::protected_static_method')); // false

// Parent's static protected method via proxy
var_dump($o->is_callable('CallableTest::protected_static_method')); // true
?>

Tested PHP versions were 5.2.9 on openSUSE 10.3 (x64) and 5.3.1 on Windows Server 2003 (x86).
juand at softwarecubes dot com 21-Feb-2009 08:39
Revised function with static support:

<?php
    /**
    * The is_callable php function only considers methods declared in the class itself, and ignores the parent's.
    * This version considers all of the hierarchy.
    *
    * @param (string|Object) $class_name
    * @param string $method_name
    * @param bool $static the method being tested is static.
    */
    public static function isCallable( $class_name, $method_name, $static = false ){

        if( !is_string( $class_name ) ){
            $class_name = get_class( $class_name );
        }

        // Define Callable
        if( $static ){
            $callable = "{$class_name}::{$method_name}";
        }else{
            $callable = array( $class_name, $method_name );
        }

        // Check class itself
            if( @is_callable( $callable ) === true ){
                if( $method_name == 'setEmailAddressTypeHash' ) {
                    ErrorHandler::preDump($callable);
                }
                return true;
            }

        // Check all parents
            while( $parent_class = get_parent_class( $class_name ) ){
                if( @is_callable( $callable ) === true ){
                    return true;
                }
                $class_name = $parent_class;
            }

        return false;
    }
?>
Rafael M. Salvioni 20-May-2008 06:10
The PHP's function is_callable not verify the visibility of the tested method.

The following function uses the Reflection classes of the PHP5 to check it.

<?php

/**
 * Function is_callback().
 *
 * @param mixed $var Var
 * @return bool
 */
function is_callback($var)
{
    if (is_array($var) && count($var) == 2) {
        $var = array_values($var);
        if ((!is_string($var[0]) && !is_object($var[0])) || (is_string($var[0]) && !class_exists($var[0]))) {
            return false;
        }
        $isObj = is_object($var[0]);
        $class = new ReflectionClass($isObj ? get_class($var[0]) : $var[0]);
        if ($class->isAbstract()) {
            return false;
        }
        try {
            $method = $class->getMethod($var[1]);
            if (!$method->isPublic() || $method->isAbstract()) {
                return false;
            }
            if (!$isObj && !$method->isStatic()) {
                return false;
            }
        } catch (ReflectionException $e) {
            return false;
        }
        return true;
    } elseif (is_string($var) && function_exists($var)) {
        return true;
    }
    return false;
}

?>
rahadotaboulfethatgmail.com 02-Mar-2008 01:44
is_callable generates an [E_STRICT] error if the  tested method cannot be called staticly. (and returns the good value)

I used @is_called
i'm using php 5.2.1
Quis strrev TA omicidio strrev TOD com 14-Nov-2007 12:50
is_callable() does _not_ check wheter this function is disabled by php.ini's disable_functions

use:

<?PHP
function is_disabled($function) {
  $disabled_functions=explode(',',ini_get('disable_functions'));
  return in_array($function, $disabled_functions);
}
?>

I`m running PHP 5.2.4
mw at NO-SPAM hire mason wolf dot com 19-Oct-2007 04:35
As empyone noted, early versions of php 5 incorrectly returned true if is_callable checked on a protected or private method. Later versions of php 5 will now only return true if the method is public and can be called externally. I do not know precisely when this behavior was changed, so you may have to test on your own. But sometime between 5.0.4, which empyone said he was using, and 5.2.4 where I tested it myself, the behavior was modified.
hcblue 24-Aug-2006 10:51
True that method_exists() is faster than is_callable(). However, is_callable() will be able to correctly recognize method calls handled by __call() in PHP 5, while method_exists() will not.
jphp at dsf dot org dot uk 12-Feb-2006 12:38
bob at theshirdshift:

"function_exists" doesn't do this, no, but "method_exists" works fine, and is still faster than is_callable:

<?php
function doTimes($start, $end)
  {
   $start_time = explode (" ", $start);
   $start_time = $start_time[1] + $start_time[0];
   $end_time = explode (" ", $end);
   $end_time = $end_time[1] + $end_time[0];
   $time = $end_time - $start_time;
   return $time;
  }

class test
  {
     function test()
     {
         return true;
     }
  }

$test = new test;

$callableIsTrue = false;
$startIsCallable = microtime();
for($i = 0; $i < 10000; $i++)
  {
     if(is_callable(array('test', 'test'))) { $callableIsTrue = true; }
  }
$endIsCallable = microtime();

$existsIsTrue = false;
$startExists = microtime();
for($i = 0; $i < 10000; $i++)
  {
     if(method_exists('test', 'test')) { $existsIsTrue = true; }
  }
$endExists = microtime();

$timeIsCallable = doTimes($startIsCallable, $endIsCallable);
$timeExists    = doTimes($startExists, $endExists);

echo "<b>is_callable = ".($callableIsTrue ? "TRUE" : "FALSE")."</b>, \n";
echo "<b>method_exists = ".($existsIsTrue ? "TRUE" : "FALSE")."</b><br>\n";

echo "<br>Did 10000 is_callables in ".$timeIsCallable." seconds";
echo "<br>Did 10000 method_exists in ".$timeExists." seconds";
?>

is_callable = TRUE, method_exists = TRUE

Did 10000 is_callables in 0.410346984863 seconds
Did 10000 method_exists in 0.175447940826 seconds
yetanotheruser at hotmail etc 15-Dec-2005 03:47
I have come across a strange oddity in versions around the 4.3.11 mark - I may have missunderstood the purpose of this function but hope this'll be helpful for some.

The point the code below is supposed to illustrate is that in some cases with

    <? $myFunc = Array( $myObj, 'myMethod' ); ?>

    <? is_callable( $myFunc, true, $callMe ); ?>

will return true, and give you $callMe set to myObj::myMethod but calling

    <? $callMe(); ?>

doesn't work... however calling

    <? $myFunc[0]->$myFunc[1](); ?>

seems to work fine..

... the reason all the code is down there is I think this oddity is due to how/the order in which I've instantiated my classes or something...

anyhow... HTH someone! :-)

Code follows:

FILE 1 :
<?
    include('myTools');

    $foo = new myClass();

    print $foo->getMySource();

    class myClass{

        var $flibble = 'wibble';

        function myClass(
            // Initialise loads of stuff.. including..
            $this->tools = new myTools();
        )

        function getMySource(){
            // This just returns the source.. ok, like some HTML to go into an email for example.
            // Some arguments;
            $args   = $this->flibble;
            // Call our Tool that returns the source..
            $source = $this->tools->returnSource( Array ( $this, 'someHTML' ), $args );
            // and return it..
            return ( $source );
        }

        function someHTML($args){
            // Leave PHP
            ?>
            Here is some HTML.. that we want to build outside a PHP block,
            possibly just cos it's tidier in <B>your favourite text editor</B>..
            .. or you want this function to be elsewhere.. for your designers
            to play with etc.. ... incidentally I'd like to say "<?=$args?>" etc.
            <?
            // .. and we're back.
        }
    }

?>

FILE 2:

<?
    /* OK - this is some other big library and a whole load more
       faff but this is the particular function in question... it
       just calls the function it's been asked to and uses an output
       buffer to grab the output and return it as a string rather than
       letting it go to terminal/browser.... useful for grabbing PHP pages
       for spidering, emailing etc. etc. etc..
    */

    class myTools(){

        function returnSource($func, $args){

            if ( is_callable ( $func, true, $callMe ){
                // Start a buffer
                ob_start();
                // Calling the function like this DOESN'T seem to work:
                // ~~~~~~~~
                // "Command not found : myClass::someHTML"
                // $callMe($args);
                // ~~~~~~~~
                // But - what I've discovered is that this behaves fine..
                if ( is_array($func) ){
                    $func[0]->$func[1]($args);
                } else {
                    $func($args);
                }
                // Then we just carry on with our code..
                $rtn =     ob_get_contents();
                ob_clean();
                return ( $rtn );
            } else {
                error_log("Doh!");
            }
        }
    }

?>
empyone at tiscalinet dot it 08-Nov-2005 09:41
To corey at eyewantmedia dot com:

your misunderstanding lies in passing in the naked $object parameter. It is correct for is_callable to return FALSE since you cannot 'call an object', you can only call one of its methods, but you don't specify which one. Hence:

is_callable(array($object, 'some_function'), [true or false], $callable_name)

will yield the correct result.

Notice, though, that a quick test I made (PHP 5.0.4) showed that is_callable incorrectly returns TRUE also if you specify the name of a protected/private method from outside of the context of the defining class, so, as wasti dot redl at gmx dot net pointed out, reflection is the way to go if you want to take visibility into account (which you should for true OOP, IMHO).
wasti dot redl at gmx dot net 09-Oct-2005 01:14
The way to discover whether a method exists in face of a __call is reflection.

It should be mentioned that although array('Test', 'func') is callable according to this function (where func is a public static method of Test), actually calling this construct as $fn() fails.
corey at eyewantmedia dot com 19-Mar-2005 12:39
I've been spending a month on and off trying to figure out why

is_callable($object, [true or false], $varContainingFunctionName)

returned false when it should not have (ie: $object->FunctionName() was callable), I realized I must have misunderstood its purpose. If you find yourself in the same situation, try

function_exists(string functionname)

or

method_exists ( object object, string method_name )

before you rip your hair out :)
mcroghan at digitalkeg dot com 11-Feb-2005 04:36
Be careful when using this function and __call (PHP5).  This function will always report true when using __call.

Need a specific function for the purpose of checking if a class method exists explicitly even when using __call.

Haven't ruled out the possibility of the existence of such a function yet.  So if someone knows of one, please point it out.
bob at thethirdshift dot net 23-Jun-2004 06:54
I, too, was wondering whether is_callable or function exists is faster when checking class methods.  So, I setup the following test:

<?php
function doTimes($start, $end)
  {
    $start_time = explode (" ", $start);
    $start_time = $start_time[1] + $start_time[0];
    $end_time = explode (" ", $end);
    $end_time = $end_time[1] + $end_time[0];
    $time = $end_time - $start_time;
    return $time;
  }

class test
  {
      function test()
      {
          return true;
      }
  }

$callableIsTrue = false;
$startIsCallable = microtime();
for($i = 0; $i < 10000; $i++)
  {
      if(is_callable(array('test', 'test'))) { $callableIsTrue = true; }
  }
$endIsCallable = microtime();

$existsIsTrue = false;
$startExists = microtime();
for($i = 0; $i < 10000; $i++)
  {
      if(function_exists('test::test')) { $existsIsTrue = true; }
  }
$endExists = microtime();

$timeIsCallable = doTimes($startIsCallable, $endIsCallable);
$timeExists     = doTimes($startExists, $endExists);

echo "<b>is_callable = ".($callableIsTrue ? "TRUE" : "FALSE")."</b>, \n";
echo "<b>function_exists = ".($existsIsTrue ? "TRUE" : "FALSE")."</b><br>\n";

echo "<br>Did 10000 is_callables in ".$timeIsCallable." seconds";
echo "<br>Did 10000 function_exists in ".$timeExists." seconds";
?>

This gives the output :

is_callable = TRUE, function_exists = FALSE

Did 10000 is_callables in 0.0640790462494 seconds
Did 10000 function_exists in 0.0304429531097 seconds

So the fact that function_exists is twice as fast is slightly over shadowed by the fact that it doesn't work on class methods, at least not as far as I can tell.


별칭: is_float

  • ≪ is_callable
  • is_float ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 별칭: is_float

is_double

(PHP 4, PHP 5, PHP 7)

is_double — 별칭: is_float()

설명

이 함수는 다음 함수의 별칭입니다: is_float().

User Contributed Notes

There are no user contributed notes for this page.


변수의 자료형이 소수인지 확인합니다

  • ≪ is_double
  • is_int ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수의 자료형이 소수인지 확인합니다

is_float

(PHP 4, PHP 5, PHP 7)

is_float — 변수의 자료형이 소수인지 확인합니다

설명

bool is_float ( mixed $var )

주어진 변수의 자료형이 float인지 확인합니다.

Note:

변수가 숫자나 숫자 문자열(폼 입력 등은 항상 문자열입니다)인지 확인하려면, is_numeric()을 사용해야 합니다.

인수

var

평가할 변수.

반환값

var가 float이면 TRUE 아니면 FALSE를 반환합니다.

예제

Example #1 is_float() 예제

<?php
if(is_float(27.25)) {
 echo "is float\n";
} else {
 echo "is not float\n";
}
var_dump(is_float('abc'));
var_dump(is_float(23));
var_dump(is_float(23.5));
var_dump(is_float(1e7));  // 과학형식
var_dump(is_float(true));
?>

위 예제의 출력:

is float
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)

참고

  • is_bool() - 변수가 논리형인지 확인
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_numeric() - 변수가 수나 수 문자열인지 확인합니다
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • is_array() - 변수가 배열인지 확인
  • is_object() - 변수가 객체인지 확인합니다

User Contributed Notes

ivan dot maruca at gmail dot com 20-Oct-2015 08:31
check if string/numeric is float or not :

<?php
function isFloat($value){
  return ((int)$value != $value) ;
}

isFloat('1.5') ;//return true
isFloat('15');//return false
isFloat('1e16');//return true
isFloat('foo');//return false
isFloat(5.98);//return true
isFloat(5);//return false
?>
Erutan409 at Hotmail dot com 19-May-2015 11:37
Boylett's solution is elegant (http://php.net/manual/en/function.is-float.php#85848), but won't work for long float's or variables that are not explicitly type of 'string' or for long floats that are encased in quotes, making it a string that will be truncated/rounded when cast to a float.  So, further logic must be completed to test for the case.  Take the following example:

<?php

if (!function_exists("test_float")) {

    function test_float($test) {

        if (!is_scalar($test)) {return false;}

        $type = gettype($test);

        if ($type === "float") {
            return true;
        } else {
            return preg_match("/^\\d+\\.\\d+$/", $test) === 1;
        }

    }

}

$test = "3.14159265358979323846264338g32795";

var_dump($test);
var_dump((float)$test);
var_dump($test == (string)(float)$test);
var_dump(test_float($test));

?>

Will produce (32-bit):

string(34) "3.14159265358979323846264338g32795"
float(3.1415926535898)
bool(false)
bool(false)

So far, so good, right?  Yeah, but it's misleading, because the string is so long, that when it's converted to a float, it won't be equivalent to the comparison of the value being cast back into a string .  So the aforementioned short function works.  Look at this next example:

<?php

$test = 3.1415926535897932384626433832795;

var_dump($test);
var_dump((float)$test);
var_dump($test == (string)(float)$test);
var_dump(test_float($test));

?>

Will produce (32-bit):

float(3.1415926535898)
float(3.1415926535898)
bool(false)
bool(true)

Why is it not working now, but the value is truly a float?  Same reasoning as mentioned before.  The float is so long that it's truncated/rounded and doesn't match the comparison being done with the short-hand function.

So, as you can see, more logic should be applied to the variable you're testing.
nonzer0value 27-Mar-2015 05:20
Coercing the value to float and back to string was a neat trick. You can also just add a literal 0 to whatever you're checking.

<?php
function isfloat($value) {
  // PHP automagically tries to coerce $value to a number
  return is_float($value + 0);
}
?>

Seems to work ok:

<?php
isfloat("5.0" + 0);  // true
isfloat("5.0");  // false
isfloat(5 + 0);  // false
isfloat(5.0 + 0);  // false
isfloat('a' + 0);  // false
?>

YMMV
KIVagant at gmail dot com 21-Aug-2012 09:04
Yet another regular expression for float in real life:

<?php
function isTrueFloat($val)
{
    $pattern = '/^[-+]?(((\\\\d+)\\\\.?(\\\\d+)?)|\\\\.\\\\d+)([eE]?[+-]?\\\\d+)?$/';

    return (!is_bool($val) && (is_float($val) || preg_match($pattern, trim($val))));
}
?>

// Matches:
1, -1, 1.0, -1.0, '1', '-1', '1.0', '-1.0', '2.1', '0', 0, ' 0 ', ' 0.1 ', ' -0.0 ', -0.0, 3., '-3.', '.27', .27, '-0', '+4', '1e2', '+1353.0316547', '13213.032468e-13465', '-8E+3', '-1354.98879e+37436'

// Non-matches:
false, true, '', '-', '.a', '-1.a', '.a', '.', '-.', '1+', '1.3+', 'a1', 'e.e', '-e-4', 'e2', '8e', '3,25'
Mitch 13-Jun-2012 05:27
Another RegEx for matching floats:
/^[+-]?(\d*\.\d+([eE]?[+-]?\d+)?|\d+[eE][+-]?\d+)$/

Matches:
1e2
+1353.0316547
13213.032468e-13465
-8E+3
-1354.98879e+37436

Non-matches:
8
e2
-e-4
E
asdf.safd
8e

Enjoy.
Boylett 20-Sep-2008 06:29
If you want to test whether a string is containing a float, rather than if a variable is a float, you can use this simple little function:

function isfloat($f) return ($f == (string)(float)$f);
ckelley at the ca - cycleworks dot com 15-Jul-2008 12:39
Personally, I use an implicit cast:

if( is_float($value+1) ){
    $value=sprintf("%.2f",$value);
}

Which turns 22.0000000 query result into 22.00 for display to users.
kshegunov at gmail dot com 31-Mar-2008 10:32
As celelibi at gmail dot com stated, is_float checks ONLY the type of the variable not the data it holds!

If you want to check if string represent a floating point value use the following regular expression and not is_float(),
or poorly written custom functions.

/^[+-]?(([0-9]+)|([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)|
(([0-9]+|([0-9]*\.[0-9]+|[0-9]+\.[0-9]*))[eE][+-]?[0-9]+))$/
phper 25-Jan-2006 09:08
A better way to check for a certain number of decimal places is to use :

$num_dec_places = 2;
number_format($value,$num_dec_places);


변수의 자료형이 정수인지 확인합니다

  • ≪ is_float
  • is_integer ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수의 자료형이 정수인지 확인합니다

is_int

(PHP 4, PHP 5, PHP 7)

is_int — 변수의 자료형이 정수인지 확인합니다

설명

bool is_int ( mixed $var )

주어진 변수의 자료형이 정수형인지 확인합니다.

Note:

변수가 수나 수 문자열(항상 문자열인 폼 입력 등)인지를 확인하려면, is_numeric()을 사용해야 합니다.

인수

var

평가할 변수.

반환값

var가 integer이면 TRUE, 아니면 FALSE를 반환합니다.

예제

Example #1 is_int() 예제

if (is_int(23)) {
 echo "정수형입니다.\n";
} else {
 echo "정수형이 아닙니다.\n";
}
var_dump(is_int(23));
var_dump(is_int("23"));
var_dump(is_int(23.5));
var_dump(is_int(true));
?>

위 예제의 출력:

정수형입니다.
bool(true)
bool(false)
bool(false)
bool(false)

참고

  • is_bool() - 변수가 논리형인지 확인
  • is_float() - 변수의 자료형이 소수인지 확인합니다
  • is_numeric() - 변수가 수나 수 문자열인지 확인합니다
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • is_array() - 변수가 배열인지 확인
  • is_object() - 변수가 객체인지 확인합니다

User Contributed Notes

Hector (hector-ordonez.com) 25-Jul-2016 09:00
Simon Neaves's answer is incomplete: negative integers will return false.

I recommend checking stackoverflow's answers:
http://stackoverflow.com/questions/6416763/checking-if-a-variable-is-an-integer-in-php
Vasiliy Makogon 08-Apr-2016 07:10
If you want detect integer of float values, which presents as pure int or float, and presents as string values, use this functions:

<?php
function isInteger($val)
{
    if (!is_scalar($val) || is_bool($val)) {
        return false;
    }
    if (is_float($val + 0) && ($val + 0) > PHP_INT_MAX) {
        return false;
    }
    return is_float($val) ? false : preg_match('~^((:?+|-)?[0-9]+)$~', $val);
}

function isFloat($val)
{
    if (!is_scalar($val)) {
        return false;
    }
    return is_float($val + 0);
}

foreach ([
    '11111111111111111', 11111111111111111, // > PHP_INT_MAX - presents in PHP as float
    1, '10', '+1', '1.1', 1.1, .2, 2., '.2', '2.',
    '-2.', '-.2', null, [], true, false, 'string'
] as $value) {
    echo $value . ':' . gettype($value) . ' is Integer? - '  . (isInteger($value) ? 'yes' : 'no') . PHP_EOL;
    echo $value . ':' . gettype($value) . ' is Float? - '  . (isFloat($value) ? 'yes' : 'no') . PHP_EOL;
}
?>
davide dot renzi at gmail dot com 16-Dec-2015 09:32
I've found a faster way of determining an integer.
On my env, this method takes about half the time of using is_int().

Cast the value then check if it is identical to the original.

<?php
if ( (int) $n !== $n ) {
    echo 'not is int';
} else {
    echo 'is int';
}
?>
PHPDev esurf er s >dot 15-Apr-2015 11:32
This will check if something has an acceptable integer value, such as

a string with int value like "1" or "1.0"
a float with int value like 1.0
an int like 12e5

The same as is_numeric, but with integer values.

<?php

    function isIntValued($var) {
        if(is_numeric($var)) {
            $var=(float)$var;
            return ((float)(int)$var)===$var;
        }
        return FALSE;
    }

?>
felix dot b at outlook dot com 16-Mar-2015 11:13
((int)$foo === $foo) does exactly the same as is_int($foo), but is faster performancewise, because PHP has quite an overhead on function calls.

As you can see the expression only returns true if the type (and value) of $foo is the same as int-casted $foo, which is only the case when foo is an int.

This applies to all the other is* functions aswell.
Anonymous 08-Mar-2015 01:20
Sometimes, we need to validate if a DECIMAL returned by a SQL database is an int or not. DECIMAL always return .000 at end of the string.

Then i use:
<?php
function isInteger($input){
{
    if(strpos($input,"."))
        $input=preg_replace("/\.$/","",rtrim(strval($input),"0"));
    return (ctype_digit($input));
}
?>
ing dot alpi at gmail dot com 19-Sep-2013 12:18
Remember that some source of data always give a string data type.
That's the case with cookies and $_GET.
if you need to check against those values, try to convert them into numbers first.
The easiest way of doing so is adding a zero before your var, just like:

<?php
$var = '15'; // ...or $var = $_COOKIE['myvar'] or $var =
$_GET['myvar']...

is_int($var);  // now returns FALSE

$var += 0;

is_int($var);  // now returns TRUE
?>
some at somewhere dot com 02-Jul-2013 12:35
<?php
// check if input is a valid number
// first number must be 1 thru 9, followed by a number 0-9, no decimals
// true for "1", "1000"
// false for "01", "-1", "1.2"
function isInteger($input){
  return preg_match('@^[1-9][0-9]*$@',$input) === 1;
}
?>
petepostma at gmail dot spam dot com 18-Jul-2012 03:38
There is a versa to the vice of this int only type check.

is_int( $integer_type) will only return true, if the TYPE is int, not the value
ctype_digit( $string_type) will only return true if the TYPE is string, and its value is INT

therefore:
 return ( is_int($value) || ctype_digit($value) );
nicolas dot giraud at actiane dot com 27-Jun-2012 06:24
Just a shorter way to check if your variable is an int or a string containing a int without others digit than 0 to 9 :

<?php
$bool = ( !is_int($value) ? (ctype_digit($value)) : true );

$value = 42; //true
$value = '42'; //true
$value = '1e9'; //false
$value = '0155'; //true
$value = 0155; //true
$value = 0xFF; //true while it's just the same as 255
$value = '0xFF'; //false
$value = 'a'; //false
$value = array(); //false
$value = array('5'); //false
$value = array(5); false
$value = ''; //false
$value = NULL; //false

?>

Short & cool :)
arturs at indigo dot lv 04-May-2012 08:31
Sometime needed check if string containing numbers is integer or not. Then I created this small function:
<?php
function is_integer2($v) {
  $i = intval($v);
  if ("$i" == "$v") {
    return TRUE;
  } else {
    return FALSE;
  }
}
?>
andre dot roesti at 7flex dot net 27-Jul-2010 02:51
With this function you can check if every of multiple variables are int. This is a little more comfortable than writing 'is_int' for every variable you've got.

<?php
function are_int ( ) {
    $args = func_get_args ();
    foreach ( $args as $arg )
        if ( ! is_int ( $arg ) )
            return false;
    return true;
}

// Example:
are_int ( 4, 9 ); // true
are_int ( 22, 08, 'foo' ); // false
?>
Robin 27-Mar-2010 05:22
Keep in mind that is_int() operates in signed fashion, not unsigned, and is limited to the word size of the environment php is running in.

In a 32-bit environment:

<?php
is_int( 2147483647 );           // true
is_int( 2147483648 );           // false
is_int( 9223372036854775807 );  // false
is_int( 9223372036854775808 );  // false
?>

In a 64-bit environment:

<?php
is_int( 2147483647 );           // true
is_int( 2147483648 );           // true
is_int( 9223372036854775807 );  // true
is_int( 9223372036854775808 );  // false
?>

If you find yourself deployed in a 32-bit environment where you are required to deal with numeric confirmation of integers (and integers only) potentially breaching the 32-bit span, you can combine is_int() with is_float() to guarantee a cover of the full, signed 64-bit span:

<?php
$small = 2147483647;         // will always be true for is_int(), but never for is_float()
$big = 9223372036854775807;  // will only be true for is_int() in a 64-bit environment

if( is_int($small) || is_float($small) );  // passes in a 32-bit environment
if( is_int($big) || is_float($big) );      // passes in a 32-bit environment
?>
thierryreeuwijk at hotmail dot com 14-Jan-2010 11:04
If you only want integer values like 23 or 0155, or form/string integer values like "23" or "0155" to be valid, this should work just fine.

<?php

    function int($int){

        // First check if it's a numeric value as either a string or number
        if(is_numeric($int) === TRUE){

            // It's a number, but it has to be an integer
            if((int)$int == $int){

                return TRUE;

            // It's a number, but not an integer, so we fail
            }else{

                return FALSE;
            }

        // Not a number
        }else{

            return FALSE;
        }
    }

    print("155".int(155)."<br>");
    print("15.5".int(15.5)."<br>");
    print("\"155\"".int("155")."<br>");
    print("\"15.5\"".int("15.5")."<br>");
    print("\"0155\"".int("0155")."<br>");
    print("\"I'm 155\"".int("I'm 155")."<br>");
    print("\"test\"".int("test")."<br>");
    print("\"\"".int(""));
?>

The above returns:

155        TRUE
15.5       FALSE
"155"      TRUE
"15.5"     FALSE
"0155"     TRUE
"I'm 155"  FALSE
"test"     FALSE
""         FALSE
me at rexxars dot com 16-Sep-2009 02:23
I was looking for the fastest way to check for an unsigned integer which supported large numbers like 4318943448871348 or 0xFFFFFFFF.

Fastest I came up with is this:
<?php
function is_unsigned_int($val) {
    return ctype_digit((string) $value));
}
?>

Will return true on 1515, 0xFFFFFFFF, '3515' and '1365158185855141'.
Will return false on 0.1515, '415.4134' and '-616'.

Be aware though, before PHP 5.1.0 this will return true on an empty string.

According to my benchmarks this is about 30% faster than the regex ^\d+$.
Roberto Rama 21-Jul-2009 08:39
Use this instead if you wanna know if a string is explicitly a number:

<?php

function isint( $mixed )
{
    return ( preg_match( '/^\d*$/'  , $mixed) == 1 );
}

var_export( isint( '123' ) ); //This will return true

var_export( isint( 123 ) ); //This will return true

var_export( isint( 'asd' ) ); //This will return false

var_export( isint( '123asd123' ) ); //This will return false

?>
e dot sand at elisand dot com 09-Apr-2009 05:27
Simon Neaves was close on explaining why his function is perfect choice for testing for an int (as possibly most people would need).  He made some errors on his ctype_digit() output though - possibly a typo, or maybe a bug in his version of PHP at the time.

The correct output for parts of his examples should be:

<?php
var_dump(ctype_digit(23)); //bool(false)
var_dump(ctype_digit("23")); //bool(true)
var_dump(ctype_digit(23.5)); //bool(false)
var_dump(ctype_digit(NULL)); //bool(false)
var_dump(ctype_digit("")); //bool(false)
?>

As you can see, the reason why using *just* ctype_digit() may not always work is because it only returns TRUE when given a string as input - given a number value and it returns FALSE (which may be unexpected).
leonix at motd dot ru 16-Dec-2008 09:08
another_is_int() is almost perfect, but it treats boolean true as int because
1 == (int) true == (string) true == '1'.

Fixed version:

<?php
/**
 * Checks if the given value represents integer
 */
function int_ok($val)
{
    return ($val !== true) && ((string)(int) $val) === ((string) $val);
}
?>
paulo dot koch at g_mail dot com 17-Oct-2008 09:30
Don't over-engineer it.

<?php
function another_is_int($a){
    return ((string) $a) === ((string)(int) $a);
}
?>
Wryel Covo - ryryel[at]gmail[dot]com 14-Oct-2008 08:53
<?php
function onlyNumbers($string){
    //This function removes all characters other than numbers
    //Esta funcao limpa a url e conserva apenas os numeros
    $string = preg_replace("/[^0-9]/", "", $string);
    return (int) $string;
}

echo $test = onlyNumbers("as87d68a6db8a7d686dx8a6dx"); //2147483647
echo "<br/>";
echo onlyNumbers("xn89d9x797d9a8x7-"); //899797987
echo "<br/>";
if(is_int($test)){
    echo "Is int ! - E inteiro !"; // This OK !
} else {
    echo "Not int ! - Nao e inteiro";
}
?>
mark at codedesigner dot nl 26-Jun-2008 03:34
updated version from Simon Neaves

<?php
function isInteger($input){
  return preg_match('@^[-]?[0-9]+$@',$input) === 1;
}
?>

this function checks if the string:
- starts with a - sign (optional)
- ends with 1 or more numeric chars
Julien Picalausa 11-May-2008 11:44
To check if a string ($s) is a representation of an integer (including representations is scientific notation and negative numbers), you can use the following test, provided that you don't expect values that are out of bounds for an integer.

is_numeric($s) && floatval($s) == intval(floatval($s))

If the test returns true, the string is a representation of an integer.
is_numeric (if it works as intended) protects from strings that are not proper numbers.
The comparison filters anything that is non_integer

If, for performance reasons, you want to avoid converting to float twice, it can also be written:
is_numeric($s) && ($f = floatval($s)) == intval($f)

If you plan to get values that cannot be representated with an integer and are ready to deal with a float, you can use floor instead of intval, as long as you are ready to deal with floats. Even so, that method will become unreliable when the precision of the float becomes insufficient for getting to the fractional part of the number
Simon Neaves 29-Apr-2008 08:14
I've found that both that is_int and ctype_digit don't behave quite as I'd expect, so I made a simple function called isInteger which does. I hope somebody finds it useful.

<?php
function isInteger($input){
    return(ctype_digit(strval($input)));
}

var_dump(is_int(23)); //bool(true)
var_dump(is_int("23")); //bool(false)
var_dump(is_int(23.5)); //bool(false)
var_dump(is_int(NULL)); //bool(false)
var_dump(is_int("")); //bool(false)

var_dump(ctype_digit(23)); //bool(true)
var_dump(ctype_digit("23")); //bool(false)
var_dump(ctype_digit(23.5)); //bool(false)
var_dump(ctype_digit(NULL)); //bool(false)
var_dump(ctype_digit("")); //bool(true)

var_dump(isInteger(23)); //bool(true)
var_dump(isInteger("23")); //bool(true)
var_dump(isInteger(23.5)); //bool(false)
var_dump(isInteger(NULL)); //bool(false)
var_dump(isInteger("")); //bool(false)
?>
qlimmax at gmail dot com 07-Apr-2008 07:58
function is_natural($natural,$zero=true)
{
if(ctype_digit($natural))
{
    if($zero)
    {
        $natural_test=(int)$natural;
        if((string)$natural_test !== $natural) return false;
        else return true;
    }
    else return true;
}
else return false;
}

true for ("0","1","2","3",...) false for("-1","01","adS","#@!$%^&*-...","0.7","0,7", "0x12",...)
if $zero=false
true for("0","00","1","01",...) false("-1","#@!$%^&*-...","adS","0.7","0,7", "0x12",...)
tudor at tudorholton dot com 05-Jul-2007 03:27
Please note this from the Integer datatype page:

"The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers. Integer size can be determined from PHP_INT_SIZE, maximum value from PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5."

This is particularly important if you are doing validation of large keys or any number larger than 2,000,000,000 (e.g. telephone numbers)
Ender at soldat dot nl 16-Feb-2006 12:38
Be aware that is_numeric (mentioned in this article as the proper way to validate string numbers) also allows numbers in scientific and hexadecimal annotation. Thus DO NOT USE that function to validate user input that will be used as id number for in a query for example, this could cause mysql errors. Use ctype_digit instead.
ludvig dot ericson at gmail dot com 06-Jan-2006 10:00
I would like to say that is_int() is pretty helpfull when looking for neat proper ways to check functions that return either integers or booleans (false) on failure (strpos, socket_select, etc.)
<?php
function mySelect() {
    global $someSockets;
    $ret = socket_select($someSockets, $o = array(), $e = array(), 0);
    if (!$ret)
        return is_int($ret);
    /* FURTHER PROCESSING HERE */
    return true;    // Return true if the function proceeded as expected.
}
?>
The point of doing this is that if you put this in a while() loopo, you'll break it when the select fails.
<?php
while (mySelect());
?>

Hope you get the point
 - toxik
lclkk at urbanvagabond dot net 16-Sep-2003 04:24
I think the function below is a robust test for integers working on all datatypes. It works by first checking that a number can be evaluated numerically, and then secondly that the integer evaluation matches the original number.

Test cases are included.

<?
function myIsInt ($x) {
    return (is_numeric($x) ? intval($x) == $x : false);
}

function Test($x) {
    echo "$x is " . ( myIsInt($x) ? ('an integer. The integer value is ' . intval($x)) : 'not an integer.');
    echo "\n";
}

echo "These should be integers...\n";
Test(1);
Test(5);
Test(10);
Test(10.0);
Test(20.0);
Test(-20.0);
Test(0+4+4.5+4.5);
Test("10.0");
Test("+14");
Test("-15");
Test("0");

echo "\nThese should not be integers...\n";
Test(true); // watch out, this displays as '1'
Test(false);
Test("moose");
Test("3.5");
Test("-214235.5");
Test(""); // empty string
Test(array(1,2,3));
Test(dir('.')); // object
Test(null);
?>
gabe at websaviour dot com 14-Jul-2003 08:08
Although this can be inferred from the documentation, beware of numeric strings.  I ran into the problem in a MySQL app where I would either SELECT an INT PRIMARY KEY or INSERT a new record and use mysql_insert_id() to get the KEY before continuing onto the new section.

I used is_int() to make sure the subsequent queries wouldn't break when using the key variable.  Unfortunately I failed to realize that while mysql_insert_id() returns an int, mysql_result() always returns a string even if you are SELECTing from an INT field.

Spent at least 30 minutes trying to figure out why existing records weren't getting linked, but new records would link fine.  I ended up using intval() on mysql_result() to make sure subsequent queries still always work.


별칭: is_int

  • ≪ is_int
  • is_long ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 별칭: is_int

is_integer

(PHP 4, PHP 5, PHP 7)

is_integer — 별칭: is_int()

설명

이 함수는 다음 함수의 별칭입니다: is_int().

User Contributed Notes

There are no user contributed notes for this page.


별칭: is_int

  • ≪ is_integer
  • is_null ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 별칭: is_int

is_long

(PHP 4, PHP 5, PHP 7)

is_long — 별칭: is_int()

설명

이 함수는 다음 함수의 별칭입니다: is_int().

User Contributed Notes

There are no user contributed notes for this page.


변수가 NULL인지 확인합니다

  • ≪ is_long
  • is_numeric ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수가 NULL인지 확인합니다

is_null

(PHP 4 >= 4.0.4, PHP 5, PHP 7)

is_null — 변수가 NULL인지 확인합니다

설명

bool is_null ( mixed $var )

주어진 변수가 NULL인지 확인합니다.

인수

var

평가할 변수.

반환값

var가 null이면 TRUE를, 아니라면 FALSE를 반환합니다.

예제

Example #1 is_null() 예제

<?php

error_reporting(E_ALL);

$foo = NULL;
var_dump(is_null($inexistent), is_null($foo));

?>
Notice: Undefined variable: inexistent in ...
bool(true)
bool(true)

참고

  • NULL형
  • isset() - 설정된 변수인지 확인
  • is_bool() - 변수가 논리형인지 확인
  • is_numeric() - 변수가 수나 수 문자열인지 확인합니다
  • is_float() - 변수의 자료형이 소수인지 확인합니다
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • is_object() - 변수가 객체인지 확인합니다
  • is_array() - 변수가 배열인지 확인

User Contributed Notes

contact dot 01834e2c at renegade334 dot me dot uk 25-May-2015 03:57
In PHP 7 (phpng), is_null is actually marginally faster than ===, although the performance difference between the two is far smaller.

PHP 5.5.9
is_null - float(2.2381200790405)
===     - float(1.0024659633636)
=== faster by ~100ns per call

PHP 7.0.0-dev (built: May 19 2015 10:16:06)
is_null - float(1.4121870994568)
===     - float(1.4577329158783)
is_null faster by ~5ns per call
normadize (a) gmail (d) com 08-Jul-2012 07:08
Using === NULL instead of is_null(), is actually useful in loaded server scenarios where you have hundreds or thousands of requests per second. Saving microseconds on a lot of "simple" operations in the entire PHP execution chain usually results in being able to serve more pages per second at the same speed, or lowering your cpu usage. People usually write very bad and slow code.
george at fauxpanels dot com 01-Dec-2008 10:58
See how php parses different values. $var is the variable.

$var        =    NULL    ""    0    "0"    1

strlen($var)    =    0    0    1    1    1
is_null($var)    =    TRUE    FALSE    FALSE    FALSE    FALSE
$var == ""    =    TRUE    TRUE    TRUE    FALSE    FALSE
!$var        =    TRUE    TRUE    TRUE    TRUE    FALSE
!is_null($var)    =    FALSE    TRUE    TRUE    TRUE    TRUE
$var != ""    =    FALSE    FALSE    FALSE    TRUE    TRUE
$var        =    FALSE    FALSE    FALSE    FALSE    TRUE

Peace!
Malfist 01-Jul-2008 04:54
Micro optimization isn't worth it.

You had to do it ten million times to notice a difference, a little more than 2 seconds

$a===NULL; Took: 1.2424390316s
 is_null($a); Took: 3.70693397522s

difference = 2.46449494362
difference/10,000,000 = 0.000000246449494362

The execution time difference between ===NULL and is_null is less than 250 nanoseconds. Go optimize something that matters.
strrev xc.noxeh@ellij 03-Jun-2008 02:42
$var===NULL is much faster than is_null($var) (with the same result)

I did some benchmarking with 10 million iterations:

$a=null;
 isset($a); Took: 1.71841216087s
 $a==NULL; Took: 1.27205181122s
 $a===NULL; Took: 1.2424390316s
 is_null($a); Took: 3.70693397522s
$a=5;
 isset($a); Took: 1.15165400505s
 $a==NULL; Took: 1.41901302338s
 $a===NULL; Took: 1.21655392647s
 is_null($a); Took: 3.78501200676s
error_reporting(E_ALL&~E_NOTICE);
unset($a);
 isset($a); Took: 1.51441502571s
 $a==NULL; Took: 16.5414860249s
 $a===NULL; Took: 16.1273870468s
 is_null($a); Took: 23.1918480396s

Please note, that isset is only included because it gives a good performance in any case; HOWEVER isset is NOT the same, or the opposite.
But you might be able to use isset() instead of null-checking.

You should not use is_null, except when you need a callback-function, or for conformity with is_int, is_float, etc.
ai dot unstmann at combase dot de 14-Jan-2008 09:17
For what I realized is that  is_null($var)  returns exactly the opposite of  isset($var) , except that is_null($var) throws a notice if $var hasn't been set yet.

the following will prove that:

<?php

$quirks = array(null, true, false, 0, 1, '', "\0", "unset");

foreach($quirks as $var) {
    if ($var === "unset") unset($var);

    echo is_null($var) ? 1 : 0;
    echo isset($var) ? 1 : 0;
    echo "\n";
}

?>

this will print out something like:

10    // null
01    // true
01    // false
01    // 0
01    // 1
01    // ''
01    // "\0"
Notice:  Undefined variable: var in /srv/www/htdocs/sandbox/null/nulltest.php on line 8
10    // (unset)

For the major quirky types/values is_null($var) obviously always returns the opposite of isset($var), and the notice clearly points out the faulty line with the is_null() statement. You might want to examine the return value of those functions in detail, but since both are specified to return boolean types there should be no doubt.

A second look into the PHP specs tells that is_null() checks whether a value is null or not. So, you may pass any VALUE to it, eg. the result of a function.
isset() on the other hand is supposed to check for a VARIABLE's existence, which makes it a language construct rather than a function. Its sole porpuse lies in that checking. Passing anything else will result in an error.

Knowing that, allows us to draw the following unlikely conclusion:

isset() as a language construct is way faster, more reliable and powerful than is_null() and should be prefered over is_null(), except for when you're directly passing a function's result, which is considered bad programming practice anyways.
powderz at gmail dot com 12-Aug-2006 06:41
Actually, since a wrapper is going to be written, you can check for your own version null values...if you want to be creative for some reason.

<?php
function isnull($data)
{
  /** only if you need this
  if (is_string($data)) {
    $data = strtolower($data);
  }
  */
  switch ($data) {
    // Add whatever your definition of null is
    // This is just an example
    //-----------------------------
    case 'unknown': // continue
    case 'undefined': // continue
    //-----------------------------
    case 'null': // continue
    case 'NULL': // continue
    case NULL:
      return true;
  }
  // return false by default
  return false;
}
?>
michael at cannonbose dot com 30-Dec-2003 06:42
Regarding avoidance of NULLs in your MySQL queries, why not use  IS NULL and IS NOT NULL in your WHERE clauses.

SELECT *
FROM someDatabase
WHERE someAttribute IS NOT NULL

Cheers,

Michael
uioreanu at hotmail dot com 23-Mar-2001 03:36
Don't try to test
if ($intSomething==NULL) {
 ...
}
use is_null() instead.
The first statement misses 0 values.

Regards,
Calin

[Ed. note: this is because == tests for equivalence of value, but not type. NULL evaluates to
false, as does 0, so NULL == 0 is true--even though 0 is type int and NULL is type null.
You should use either is_null() as noted or ===, which returns true only if its operands are
equal and of the same type.]


변수가 수나 수 문자열인지 확인합니다

  • ≪ is_null
  • is_object ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수가 수나 수 문자열인지 확인합니다

is_numeric

(PHP 4, PHP 5, PHP 7)

is_numeric — 변수가 수나 수 문자열인지 확인합니다

설명

bool is_numeric ( mixed $var )

주어진 변수가 수인지 확인합니다. 수 문자열은 부수적인 부호, 숫자들, 부수적인 소수점 부분과 부수적인 지수부로 이루어집니다. 그러므로 +0123.45e6은 유효한 수 값입니다. 16진 표현(0xFF)도 허용되지만, 부호, 소수점, 지수부가 없어야 합니다.

인수

var

평가할 변수.

반환값

var가 수나 수 문자열이라면 TRUE를, 아니라면 FALSE를 반환합니다.

예제

Example #1 is_numeric() 예제

<?php
$tests = Array(
                "42",
                1337,
                "1e4",
                "not numeric",
                Array(),
                9.1
                );

foreach($tests as $element)
{
        if(is_numeric($element))
        {
                echo "'{$element}' is numeric", PHP_EOL;
        }
        else
        {
                echo "'{$element}' is NOT numeric", PHP_EOL;
        }
}
?>

참고

  • ctype_digit() - Check for numeric character(s)
  • is_bool() - 변수가 논리형인지 확인
  • is_null() - 변수가 NULL인지 확인합니다
  • is_float() - 변수의 자료형이 소수인지 확인합니다
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • is_object() - 변수가 객체인지 확인합니다
  • is_array() - 변수가 배열인지 확인

User Contributed Notes

Vasiliy Makogon 09-Apr-2016 05:22
If you want detect integer of float values, which presents as pure int or float, and presents as string values, use this functions:

<?php
function isInteger($val)
{
    if (!is_scalar($val) || is_bool($val)) {
        return false;
    }
    if (is_float($val + 0) && ($val + 0) > PHP_INT_MAX) {
        return false;
    }
    return is_float($val) ? false : preg_match('~^((?:\+|-)?[0-9]+)$~', $val);
}

function isFloat($val)
{
    if (!is_scalar($val)) {
        return false;
    }
    return is_float($val + 0);
}

foreach ([
    '11111111111111111', 11111111111111111, // > PHP_INT_MAX - presents in PHP as float
    1, '10', '+1', '1.1', 1.1, .2, 2., '.2', '2.',
    '-2.', '-.2', null, [], true, false, 'string'
] as $value) {
    echo $value . ':' . gettype($value) . ' is Integer? - '  . (isInteger($value) ? 'yes' : 'no') . PHP_EOL;
    echo $value . ':' . gettype($value) . ' is Float? - '  . (isFloat($value) ? 'yes' : 'no') . PHP_EOL;
}
?>

Output:

11111111111111111:string is Integer? - no
11111111111111111:string is Float? - yes
1,1111111111111E+16:double is Integer? - no
1,1111111111111E+16:double is Float? - yes
1:integer is Integer? - yes
1:integer is Float? - no
10:string is Integer? - yes
10:string is Float? - no
+1:string is Integer? - yes
+1:string is Float? - no
1.1:string is Integer? - no
1.1:string is Float? - yes
1,1:double is Integer? - no
1,1:double is Float? - yes
0,2:double is Integer? - no
0,2:double is Float? - yes
2:double is Integer? - no
2:double is Float? - yes
.2:string is Integer? - no
.2:string is Float? - yes
2.:string is Integer? - no
2.:string is Float? - yes
-2.:string is Integer? - no
-2.:string is Float? - yes
-.2:string is Integer? - no
-.2:string is Float? - yes
:NULL is Integer? - no
:NULL is Float? - no
Array:array is Integer? - no
Array:array is Float? - no
1:boolean is Integer? - no
1:boolean is Float? - no
:boolean is Integer? - no
:boolean is Float? - no
string:string is Integer? - no
string:string is Float? - no
vikramverma214 at gmail dot com 26-Dec-2015 03:24
check if given string is mobile number

function filterNumber($str)
        {
            //remove non number
            $count = strlen($str);
            $newNumber = "";
            for($i = 0; $i<$count; $i++)
            {
                if(is_numeric($str[$i]))
                    $newNumber .= $str[$i];
            }

            //removes leading 0s
            $newNumber = ltrim($newNumber, '0');

            //remove 91 in starting, 91 is specific to Indian Numbers
            if(strlen($newNumber) == 12)
            {
                if(substr($newNumber, 0, 2) == '91')
                    $newNumber = substr($newNumber, 2);
            }

            //final check for length 10
            if(strlen($newNumber) != 10 )
                return null;
            else
                return $newNumber;
        }
lihatskiym at gmail dot com 30-Sep-2015 08:26
Check that value is whole numeric or is whole integer.
<?php
function whole_numeric($val)
{
    if (is_numeric($val)
        && floor($val) == $val)
    {
        if ((string)$val === (string)0)
            return true;
        elseif(ltrim((string)$val, '0') === (string)$val)
            return true;
    }

    return false;
}

function whole_int($val)
{
    $val = strval($val);
    $val = str_replace('-', '', $val);

    if (ctype_digit($val))
    {
        if ($val === (string)0)
            return true;
        elseif(ltrim($val, '0') === $val)
            return true;
    }

    return false;
}
?>

Result will be like this:

<?php
whole_numeric('-1.0');  // true
whole_numeric(0);        // true
whole_numeric('0');      // true
whole_numeric('00');     // false
whole_numeric(1);        // true
whole_numeric('1');      // true
whole_numeric('1.0');    // true
whole_numeric('1,0');    // false
whole_numeric('1.2');    // false
whole_numeric('001');    // false
whole_numeric(array()); // false

whole_int('-1.0');    // false
whole_int('-1');       // true
whole_int(0);          // true
whole_int('0');        // true
whole_int('00');      // false
whole_int(1);          // true
whole_int('1');         // true
whole_int('1.0');      // false
whole_int('1.2');      // false
whole_int('001');     // false
whole_int(array()); // false
?>
Tim Robert-Fitzgerald 27-Jun-2015 08:17
Be careful when using `is_numeric()` to validate user input and protect against SQL injection. According to wiki.hashphp.org,

"Many people reach for is_numeric() thinking this is a good approach, and it will seem to work just fine. However the problem with is_numeric() isn't that it can't detect an integer, it is that it detects a lot more than just integers. All of the following are valid numbers to is_numeric():

- 1
- 1.123
- 0xFF
- +0123.45e6

See the problem? If we really want just an integer, this function is too broad."

Source: http://wiki.hashphp.org/Validation#Why_is_numeric.28.29_is_bad
Joris Berthelot 14-Feb-2014 08:30
There's no way to check if a string contains a float number so here's a tricky one:

<?php
function isFloat($input)
{
    return is_numeric(preg_replace('/[^-0-9\.]/', null, $input));
}
charlike dot mike at mail dot bg 03-Feb-2014 01:24
I think that is best check solution if u want to create real calculator for example :)

<?php
function is_number($var)
{
    if ($var == (string) (float) $var) {
        return (bool) is_numeric($var);
    }
    if ($var >= 0 && is_string($var) && !is_float($var)) {
        return (bool) ctype_digit($var);
    }
    return (bool) is_numeric($var);
}

is_number(12); // true
is_number(-12); // true
is_number(-12.2); // true
is_number("12"); // true
is_number("-124.3"); // true
is_number(0.8); // true
is_number("0.8"); // true
is_number(0); // true
is_number("0"); // true
is_number(NULL); // false
is_number(true); // false
is_number(false); // false
is_number("324jdas32"); // false
is_number("123-"); // false
is_number(1e7); // true
is_number("1e7"); // true
is_number(0x155); // true
is_number("0x155"); // false
?>
ben at chico dot com 08-Jan-2014 08:20
Apparently NAN (Not A Number) is a number for the sake of is_numeric().

<?php
echo "is ";
if (!is_numeric(NAN))
 echo "not ";
 echo "a number";
?>

Outputs "is a number". So something that is NOT a number (by defintion) is a number...
Je oma. 17-Jun-2013 02:58
oefening 3
#!/usr/bin/php5

<?php
    $tijd = getdate();
    if ($tijd["wday"] == 0) exit;

    $dag   = $tijd["mday" ];
    $maand = $tijd["month"];
    $jaar  = $tijd["year" ];

    $cmd = "tar -czf backup-" . $jaar
        . "_" . $maand . "_" . $dag
        . ".tar /var/log";

    system($cmd);
?>
Oefening 4
#!/usr/bin/php5

<?php
    if ($argc < 2) exit;

    for (;;) {
        if(file_exists($argv[1])) {
            echo "Hoera\n";
            exit;
        }
        sleep(60);
    }
?>
Oefening 5
#!/usr/bin/php5

<?php
  for (;;) {
    system("clear");
    echo "1. kalender\n";
    echo "2. backup\n";
    echo "3. manpage\n";
    echo "4. exit\n";

    $input = readline("maak een keuze: ");
    if ($input == "1") kalender();
    if ($input == "2") backup();
    if ($input == "3") manpage();
    if ($input == "4") exit;
  }

  function kalender() {
    for ($maand = 1; $maand < 13; $maand++) {
      system("clear");
      system("cal -m $maand");
      readline("druk enter om verder te gaan");
    }
  }

  function backup() {
    for (;;) {
      $filename = readline("De naam van je backup: ");
      $dir      = readline("Directory voor backup: ");
      if ((!empty($filename)) && (!empty($dir))) break;
    }
    if (file_exists($dir)) {
      $command = "tar -czf backup-" . $filename
                 . ".tgz " . $dir . " > /dev/null";
      system($command);
    } else {
      echo("De map bestaat niet. \n");
    }
  }

  function manpage() {
    $command = readline("Kies een commando: ");
    system("man " . $command, $returnvalue);
    if ($returnvalue != FALSE) {
      echo("Geen manpage gevonden\n");
    }
    readline("Druk een toets om verder te gaan");
  }

?>
Oefening 6
#!/usr/bin/php5
<?php

  if ($argc < 2) {
    echo "je moet een naam opgeven\n";
    exit;
  }

  $naam = $argv[1];

  $content = file("namen.txt", FILE_IGNORE_NEW_LINES);
  sort($content);

  if (in_array($naam, $content)) {
    foreach($content as $line) {
    echo $line . "\n";
  }
  echo "Het bestand bevat " . count($content) . " namen.\n";
}

?>
jsnell at e-normous dot com 20-Mar-2013 06:38
Note that this function handles leading spaces differently than is_int(), is_float(), is_real(), is_long(), and is_double().

is_numeric(" 12345") = true
is_int(" 12345") = false
is_float(" 12345") = false
is_real(" 12345") = false
is_long(" 12345") = false
is_double(" 12345") = false
jeffvanhees at gmail dot com 20-Jun-2012 02:20
Function for checking all the values in an array for being numeric:

<?php
function is_numeric_array($array) {
    foreach ($array as $key => $value) {
        if (!is_numeric($value)) return false;
    }
    return true;
}

$array_not_numeric = array('1', '2', 3, 'a'); // returns false
$array_numeric = array('1', '2', 3, 4); // returns true
?>
dave dot marr at gmail dot com 31-Jan-2012 01:30
If you want the numerical value of a string, this will return a float or int value:

<?php
function get_numeric($val) {
  if (is_numeric($val)) {
    return $val + 0;
  }
  return 0;
}
?>

Example:
<?php
get_numeric('3'); // int(3)
get_numeric('1.2'); // float(1.2)
get_numeric('3.0'); // float(3)
?>
akam at akameng dor com 23-Jan-2012 06:22
<?php
$d = @$_GET['d'];

/*
assuming that passing value of d in the mysql
$sql = "select * from books where bookid = '$d' "
*/

/*
now check if $d is numeric?
*/

if(!is_numeric($d)){
 $d = preg_replace("/[^0-9]+/", "", $d);
}
echo "Cleared value: $d \n<br />Original Vlaue:".@$_GET['d'];
//then using clear value of $d in the mysql query
$sql = "select * from books where bookid = '$d' ";
?>
Example:
?d=52;d;s;s'2233l'[[22

Outpt:

Cleared value: 52223322
Original Vlaue:52;d;s;s'2233l'[[22
sobolanx at gmail dot com 23-Jan-2011 12:55
Note that the function accepts extremely big numbers and correctly evaluates them.

For example:

<?php
    $v = is_numeric ('58635272821786587286382824657568871098287278276543219876543') ? true : false;

    var_dump ($v);
?>

The above script will output:

bool(true)

So this function is not intimidated by super-big numbers. I hope this helps someone.

PS: Also note that if you write is_numeric (45thg), this will generate a parse error (since the parameter is not enclosed between apostrophes or double quotes). Keep this in mind when you use this function.
Martyn Ranyard 10-Jan-2011 01:14
The documentation does not clarify what happens if you the input is an empty string - it correctly returns false in my experience.  Useful to state these odd cases, for when you see code that checks for an empty string and is_numeric, you can tell it's a waste of a comparison.
Nitrogen 12-Sep-2010 07:40
I find it a little weird that people are having issues with ordinal numbers, it's pretty easy..
Notes are in the commenting, check out the example outputs.

<?php

function ordinal($i='') {
  // a temporary value we can change, and keep the original value.
  $o=$i;

  // suffixes = 0th, 1st, 2nd, third == zeroth, first, second, third
  $s=array('th','st','nd','rd');

  // if input just so happens to be a string, we check to make sure it
  // still holds a numeric value and only acquire the last 2 numbers.
  // if it's not a string, nor an integer, we freak out and say no.
  if(!is_int($o))
    if(ctype_digit($o))
      $o=(int)substr($o,-2,2);
    else
      return(false);

  // basically, if $o is between 11 and 19, we use 'th'
  // otherwise we use the last digit and if it's over
  // 4 then we use 0 (for the $s array index).
  return($i.$s[($o%100>10&&$o%100<20)?0:($o%10<4?$o%10:0)]);
}

var_dump(ordinal(5));

/* Example Outputs of ordinal():
  0th, 1st, 2nd, 3rd, 4th, ..., 9th,
  10th, 11th, 12th, 13th, 14th, ..., 19th,
  110th, 111th, ..., 199th, 200th, 201st.

  ordinal('-1'); returns false because ctype_digit hates anything that
  isn't strictly 0 through 9 and '-' trips it to false.

  ordinal('asdf'); returns false for the exact same reason.

  ordinal(); returns false because it's blank.

  signed integers on a 32-bit system (and the same issue on a 64-bit
  system using 0x7FFFFFFFFFFFFFFF because of two's compliment,
  anything higher will become a negative number):
  ordinal(0x7FFFFFFF  ); returns 2147483647th (which is correct)
  ordinal(0x7FFFFFFF+1); returns false.
*/

?>
purdue at nc dot rr dot com 19-May-2010 01:57
I see a lot of examples below to get the ordinal string for a given number ("1st", "2nd", "3rd", "4th", etc).  If you only need small ordinals (up to "31st" at most), this one-line trick works:

<?php

function ordinal ($i) {
   return date("jS", strtotime("Jan $i"));
}

?>
dustout at gmail dot com 03-Mar-2010 03:38
I had troubles when using this as an is_hex function, if anyone else runs into troubles here is an alternative approach. Not a very efficient one, but it worked quite well for what i needed it for.

<?php
function is_hex($hexValue){
if($hexValue == dechex(hexdec($hexValue)))
        return true;
    return false;
}
?>
Kiam 23-Jul-2009 08:02
empty(trim($test, '+-.,0123456789')) doesn't assure you $test contains a number. It returns TRUE also for a string containing ".+1234.56", which is not a valid number.
codeslinger at compsalot dot com 15-Jul-2009 10:34
if speed is important, the liberal use of regex should be avoided, especially complex ones like those here.

For most purposes, this will be sufficient and has the advantages of both speed and clarity.

$BoolResult = empty(trim($Test, '+-.,0123456789'));

If the $Test contains any character that is not a valid part of a number the remaining string will not be empty.

if you want to be more restrictive of the '+-'  then you can use a separate trim for that and nest it.  Keep in mind that some notations pt the sign to the right of the number instead of to the left.  Depending upon your specific application you can also choose to ignore spaces and allow for 'e' notation.

I also use this approach when testing for other character sets such as Hex.
Magnus Deininger, dma05 at web dot de 07-Jan-2009 03:01
regarding the global vs. american numeral notations, it should be noted that at least in japanese, numbers aren't grouped with an extra symbol every three digits, but rather every four digits (for example 1,0000 instead of 10.000). also nadim's regexen are slightly suboptimal at one point having an unescaped '.' operator, and the whole thing could easily be combined into a single regex (speed and all).

adjustments:

<?php
$eng_or_world = preg_match
  ('/^[+-]?'. // start marker and sign prefix
  '(((([0-9]+)|([0-9]{1,4}(,[0-9]{3,4})+)))?(\\.[0-9])?([0-9]*)|'. // american
  '((([0-9]+)|([0-9]{1,4}(\\.[0-9]{3,4})+)))?(,[0-9])?([0-9]*))'. // world
  '(e[0-9]+)?'. // exponent
  '$/', // end marker
  $str) == 1;
?>

i'm sure this still isn't optimal, but it should also cover japanese-style numerals and it fixed a couple of other issues with the other regexen. it also allows for an exponent suffix, the pre-decimal digits are optional and it enforces using either grouped or ungrouped integer parts. should be easier to trim to your liking too.
jbezorg at gmail dot com 08-Oct-2008 01:50
"$e = (int) substr( $n , -2 );"

What the heck am I thinking.

<?php
function number_suffix2($n)
{
    $s = array( 1->'st' , 2=>'nd' , 3=>'rd' );
    $e = $n % 100;
    $x = $n % 10;

    return $n.( ( $x > 0 && $x < 4 && ( $e < 11 || $e > 13 ) ) ? $s[$x] : 'th' );
}
?>
kendsnyder at gmail dot com 29-Jun-2007 07:49
Be careful when using is_numeric() to escape SQL strings.  is_numeric('0123') returns true but 0123 without quotes cannot be inserted into SQL.  PHP interprets 0123 without quotes as a literal octal number; but SQL just throws a syntax error.

<?php
is_numeric('0123'); // true
is_numeric(0.123); // true
is_numeric('0.123'); // true
is_numeric(123); // true
is_numeric('123'); // true
is_numeric('foo'); // false
?>

Casting a value to float then string and comparing it to the original value cast as string as the same effect as is_numeric but returns false for numeric strings that begin with zero and have no decimal point.  Examples:
<?php
function isDecimalNumber($n) {
  return (string)(float)$n === (string)$n;
}

isDecimalNumber('0123'); // false
isDecimalNumber(0.123); // true
isDecimalNumber('0.123'); // true
isDecimalNumber(123); // true
isDecimalNumber('123'); // true
isDecimalNumber('foo'); // true

?>
Carlos Madeira 02-Jun-2007 07:46
If I may, i think there is a simpler way to determine if a number is odd or even:

if( $number & 1 ) //Just check the last bit!
  echo 'It's odd!';
else
  echo 'It's even!';
Kenn 11-May-2007 08:45
A side note, finding out if an integer is even or odd, given the integer = $i ...

if (($i % 2) == 0) // This will test TRUE if the integer is even.
   {
xxx insert code here xxx
   }
Elwin van Huissteden 27-Mar-2007 01:05
To James holwell:

Maybe your function was more strickt, but profides FALSE to any numeric string that wasnt written in the English/American notition. To enable a person to use the both the English/American and the rest of the world's way:

<?php
  function my_is_numeric($value)
  {
   return (preg_match ("/\A(-){0, 1}([0-9]+)((,|.)[0-9]{3, 3})*((,|.)[0-9]){0, 1}([0-9]*)\z/" ,$value) == 1);
  }
?>

Other than that, i'd recommend using yours, if it works (i havent tested either yours or mine)
By using mine, there might be a slight chance to not being able to do calculations with the numeric string if it's not the English/American way.

(*Note:
-the E/A way of writing 1 million (with decimal for 1/50): 1,000,000.02
-the global way of writing 1 million (with decimal for 1/50): 1.000.000,02
james.holwell( at )exeter.ox.ac.uk 20-Mar-2007 11:07
In reply to www.kigoobe.com, a more strict expression is

<?php
  function my_is_numeric($value)
  {
    return (preg_match ("/^(-){0,1}([0-9]+)(,[0-9][0-9][0-9])*([.][0-9]){0,1}([0-9]*)$/", $value) == 1);
  }
?>

This will not match strings like -6,77.8,8 which are matched by the below expression, and instead requires a single decimal point, with at least one character following, and only permits comma-separation when the right hand side is a triplet.
Stephen 25-Jan-2007 06:31
The documentation is not completely precise here. is_numeric will also return true if the number begins with a decimal point  and/or a space, provided a number follows (rather than a letter or punctuation). So, it doesn't necessarily have to start with a digit.
mariusads::at::helpedia.com 19-Jan-2007 10:56
Test if a number is positive and contains only 0-9:

function is_number($number)
{
    $text = (string)$number;
    $textlen = strlen($text);
    if ($textlen==0) return 0;
    for ($i=0;$i < $textlen;$i++)
    { $ch = ord($text{$i});
       if (($ch<48) || ($ch>57)) return 0;
    }
    return 1;
}

returns

0 : number contain character outside 0-9
1 : valid number.
meagar at gmail dot com 14-Jan-2007 04:52
Miero: Your function doesn't match some special cases: '+1', '-0', '+0', all of which are valid integers.  The easiest and most reliable way to get a definite integer match is with a regular expression:

function is_intval($value) {
     return 1 === preg_match('/^[+-]?[0-9]+$/', $value);
}

This has two "problems" based on your input:  it matches both '00' and '999999999999999999999999999999999' as valid integers.

I'm not sure why you wouldn't want to match "00".  Regardless of whether somebody entered it in a form by accident or on purpose, it /is/ a valid integer, and in most instances you should accept it.

The second value, "999..." is also a valid integer, even if PHPs internal int type isn't precise enough to represent it.
ja at krystof dot org 02-Sep-2006 03:10
Here is a simple function to recognize whether the value is a natural number: (Zero is often exclude from the natural numbers, that's why there's the second parameter.)

<?php
function is_natural($val, $acceptzero = false) {
 $return = ((string)$val === (string)(int)$val);
 if ($acceptzero)
  $base = 0;
 else
  $base = 1;
 if ($return && intval($val) < $base)
  $return = false;
 return $return;
}
?>
moskalyuk at gmail dot com 21-Aug-2006 07:18
is_numeric fails on the hex values greater than LONG_MAX, so having a large hex value parsed through is_numeric would result in FALSE being returned even though the value is a valid hex number
rontarrant at NO_SPAMsympatico dot ca 24-Jul-2006 04:15
Here's an even simpler pair of functions for finding out if a number is odd or even:

function IS_ODD($number) { return($number & 1); }

function IS_EVEN($number) { return(!($number & 1)); }

Test:
$myNumber = 151;

if(IS_ODD($myNumber))
    echo("number is odd\n");
else
    echo("number is NOT odd\n");

if(IS_even($myNumber))
    echo("number is even\n");
else
    echo("number is NOT even\n");

Results:
number is odd
number is NOT even
andrea dot vacondio at gmail dot com 13-Dec-2005 04:52
Two simple functions using is_numeric:

<?php

 function is_odd($num){
     return (is_numeric($num)&($num&1));
 }

 function is_even($num){
     return (is_numeric($num)&(!($num&1)));
 }

 //examples
echo "1: odd? ".(is_odd(1)? "TRUE": "FALSE")."<br />";
//is_numeric(0) returns true
echo "0: odd? ".(is_odd(0)? "TRUE": "FALSE")."<br />";
echo "6: odd? ".(is_odd(6)? "TRUE": "FALSE")."<br />";
echo "\"italy\": odd? ".(is_odd("italy")? "TRUE": "FALSE")."<br />";
echo "null: odd? ".(is_odd(null)? "TRUE": "FALSE")."<br /><br />";
echo "1: even? ".(is_even(1)? "TRUE": "FALSE")."<br />";
echo "0: even? ".(is_even(0)? "TRUE": "FALSE")."<br />";
echo "6: even? ".(is_even(6)? "TRUE": "FALSE")."<br />";
echo "\"italy\": even? ".(is_even("italy")? "TRUE": "FALSE")."<br />";
echo "null: even? ".(is_even(null)? "TRUE": "FALSE")."<br />";

?>
And here is the result:
1: odd? TRUE
0: odd? FALSE
6: odd? FALSE
"italy": odd? FALSE
null: odd? FALSE

1: even? FALSE
0: even? TRUE
6: even? TRUE
"italy": even? FALSE
null: even? FALSE
jamespam at hotmail dot com 08-Aug-2005 11:31
Here's a function to determine if a variable represents a whole number:

function is_whole_number($var){
  return (is_numeric($var)&&(intval($var)==floatval($var)));
}

just simple stuff...
is_whole_number(2.00000000001); will return false
is_whole_number(2.00000000000); will return true
namik at hub-cafe dot net 30-Jul-2005 03:11
I needed a number_suffix function that takes numbers with thousand seperators (using number_format() function).  Note that this doesn't properly handle decimals.

Example:
<?= number_suffix('1,021') ?> returns: 1,021st

Also, increasing the range above the condition statements increases efficiency.  That's almost 20% of the numbers between 0 and 100 that get to end early.

<?
  function number_suffix($number)
  {
    // Validate and translate our input
    if ( is_numeric($number) )
    {
      // Get the last two digits (only once)
      $n = $number % 100;
    } else {
     // If the last two characters are numbers
     if ( preg_match( '/[0-9]?[0-9]$/', $number, $matches ) )
     {
       // Return the last one or two digits
       $n = array_pop($matches);
     } else {
       // Return the string, we can add a suffix to it
       return $number;
     }
    }
    // Skip the switch for as many numbers as possible.
    if ( $n > 3 && $n < 21 )
      return $number . 'th';

    // Determine the suffix for numbers ending in 1, 2 or 3, otherwise add a 'th'
    switch ( $n % 10 )
    {
      case '1': return $number . 'st';
      case '2': return $number . 'nd';
      case '3': return $number . 'rd';
      default:  return $number . 'th';
    }
  }
?>
maninblack00 at mail dot ru 30-Jul-2005 01:23
blazatek at wp dot pl wrote a function to check POST inputs for ASCII-keys or smth like that. there was an error while filtering $varvalue (it was only the value of the last filter 'addslashes')

here's the corrected function:
<?

function is_num ($num) {
      // .....
}

function test_post($tab) {
   $post = array();
   $post = $tab;
   foreach ($post as $varname => $varvalue) {
       echo $tab[$varname];
       if (empty($varvalue)) {
                 $post[$varname] = null;
       }
       elseif (is_num($varvalue)) {
                 $varvalue=trim($varvalue);
                 $varvalue=strip_tags($varvalue);
                 $varvalue=intval($varvalue);
                 $post[$varname]=addslashes($varvalue);
       }
       else {
                 $post[$varname]=NULL;
       }
   }//forech
    return $post;
}
?>

hope that correction is correct ;)
sebu 28-Jul-2005 01:37
Referring to previous post "Be aware if you use is_numeric() or is_float() after using set_locale(LC_ALL,'lang') or set_locale(LC_NUMERIC,'lang')":

This is totally wrong!

This was the example code:

-----
  set_locale(LC_NUMERIC,'fr');
  is_numeric(12.25); // Return False
  is_numeric(12,25); // Return True
  is_float(12.25); //Return False
  is_float(12,25); //Return True
-----

This is nonsense!

- set_locale() does not exist, you must use setlocale() instead
- you have to enclose 12,25 with quotes; otherwise PHP will think that
the function gets _two_ arguments: 12 and 25 (depending on PHP version and setup you may additionally get a PHP warning)
- if you don't enclose 12,25 with quotes the first argument will be the inspected value (12), the second value (25) is discarded. And is_numeric(12) and is_float(12) is always TRUE

Corrected Example:

----
  setlocale(LC_NUMERIC,'fr');
  is_numeric(12.25); // Return True
  is_numeric("12,25"); // Return False
  is_float(12.25); //Return True
  is_float("12,25"); //Return False
----

Remarks:
- is_float(12.25) is _always_ TRUE, 12.25 is a PHP language construct (a "value") and the way PHP interpretes files is definitely _not_ affected by the locale
- is_float("12,25") is _always_ FALSE, since is_float (other than is_numeric): if the argument is a string then is_float() always returns FALSE since it does a strict check for floats

And the corrected example shows: you get the _same_ results for every possible locale, is_numeric() does not depend on the locale.
Titouthat 27-Apr-2005 09:47
This function converts an input string into bool, int or float depending on its content.

<?php
function convert_type( $var )
{
    if( is_numeric( $var ) )
    {
        if( (float)$var != (int)$var )
        {
            return (float)$var;
        }
        else
        {
            return (int)$var;
        }
    }

    if( $var == "true" )    return true;
    if( $var == "false" )    return false;

    return $var;
}
?>

'90' return an int
'90.9' return a float
'true' return a bool
'90.0' return a int
lukesneeringer at gmail dot com 19-Mar-2005 05:32
Regarding renimar at yahoo's function to yield ordinal numbers, the function lacks one thing. It accounts for numbers in the teens only if the number is below 100. If you used this function and gave 212 as the input, it would give 212nd, and not 212th. (Also, checking for numbers between 11 and 13 is sufficient, since 14-19 yield th either way.)

Therefore,
<?php if ($num >= 11 and $num <= 19) ?>
should be changed to...
<?php if ($num % 100>= 11 and $num % 100 <= 13) ?>

It will then work perfectly all the time.

Here's the entire function with the one line changed:
<?php
function ordinalize($num) {
       if (!is_numeric($num))
               return $num;

       if ($num % 100 >= 11 and $num % 100 <= 13)
               return $num."th";
       elseif ( $num % 10 == 1 )
               return $num."st";
       elseif ( $num % 10 == 2 )
               return $num."nd";
       elseif ( $num % 10 == 3 )
               return $num."rd";
       else // $num % 10 == 0, 4-9
               return $num."th";
}
?>
codeslinger at compsalot dot com 18-Feb-2005 06:00
in version 4.3.10  I find the following

".73"  TRUE
"0.73"  TRUE
"+0.73"  TRUE

"+.73"   FALSE

I would not call it a bug, just something to be aware of.

-----

Also be aware that if you give php a huge number and then you convert it to a string you get

"INF"

if you pass that to mySQL etc. you could have a problem...
mdallaire at virtuelcom dot com 18-Nov-2004 01:23
Sometimes, we need to have no letters in the number and is_numeric does not quit the job.
You can try it this ways to make sure of the number format:

    function new_is_unsigned_float($val) {
        $val=str_replace(" ","",trim($val));
        return eregi("^([0-9])+([\.|,]([0-9])*)?$",$val);
    }

    function new_is_unsigned_integer($val) {
        $val=str_replace(" ","",trim($val));
        return eregi("^([0-9])+$",$val);
    }

    function new_is_signed_float($val) {
        $val=str_replace(" ","",trim($val));
        return eregi("^-?([0-9])+([\.|,]([0-9])*)?$",$val);
    }

    function new_is_signed_integer($val) {
        $val=str_replace(" ","",trim($val));
        return eregi("^-?([0-9])+$",$val);
    }

It returns 1 if okay and returns nothing "" if it's bad number formating.
renimar no spam at nospam yahoo dot com 03-May-2004 09:54
A little function to ordinalize numbers using is_numeric() and accounting for the numbers in the teens.

<?php
function ordinalize($num) {

        if (!is_numeric($num))
                return $num;

        if ($num >= 11 and $num <= 19)
                return $num."th";
        elseif ( $num % 10 == 1 )
                return $num."st";
        elseif ( $num % 10 == 2 )
                return $num."nd";
        elseif ( $num % 10 == 3 )
                return $num."rd";
        else
                return $num."th";

}

// Demo
for ($i=1; $i<=25; $i++) {
        print ordinalize($i) . " ";
}

// The loop returns:
// 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th
// 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd
// 23rd 24th 25th

?>
joe at kewlmail dot net 16-Jan-2004 12:12
Here is a simple function that I found usefull for filtering user input into numbers. Basically, it attempts to fix fat fingering. For example:

$userString = "$654.4r5";

function numpass_filter($userString){
    $money = explode(".", $userString);
    //now $money[0] = "$645" and $money[1] = "4r5"

    //next remove all characters save 0 though 9
    //in both elements of the array
    $dollars = eregi_replace("[^0-9]", null, $money[0]);
    $cents = eregi_replace("[^0-9]", null, $money[1]);

    //if there was a decimal in the original string, put it back
    if((string)$cents!=null){
        $cents = "." . $cents;
    }

   $result = $dollars . $cents;

    return($result);
}

The output in this case would be '654.45'.
Please note that this function will work properly unless the user fat fingers an extra decimal in the wrong place.
kouber at saparev dot com 24-Nov-2003 12:05
Note that this function is not appropriate to check if "is_numeric" for very long strings. In fact, everything passed to this function is converted to long and then to a double. Anything greater than approximately 1.8e308 is too large for a double, so it becomes infinity, i.e. FALSE. What that means is that, for each string with more than 308 characters, is_numeric() will return FALSE, even if all chars are digits.

However, this behaviour is platform-specific.

http://www.php.net/manual/en/language.types.float.php

In such a case, it is suitable to use regular expressions:

function is_numeric_big($s=0) {
  return preg_match('/^-?\d+$/', $s);
}
blazatek at wp dot pl 21-Oct-2003 05:05
the best way to check whether
variable is numeric is to check its ascii code :)

function is_num($var)
{
    for ($i=0;$i<strlen($var);$i++)
    {
        $ascii_code=ord($var[$i]);

        if ($ascii_code >=49 && $asci_code <=57)
            continue;
        else
            return false;
    }

        return true;
}
this function can be usefull if you wont to chec eg $_POST

function test_post($tab)
{

        $post = array();
        $post=$tab;
        echo $tab["user_name"];
        foreach ($post as $varname => $varvalue)
        {
        if (empty($varvalue))
        {
            $post[$varname] = null;

        }elseif (is_num($varvalue))
        {
                        $post[$varname]=trim($varvalue);
    $post[$varname]=strip_tags($varvalue);
    $post[$varname]=intval($varvalue);
    $post[$varname]=addslashes($varvalue);
    }else
    {
                  $post[$varname]=NULL;
    }
            }
        }//forech
    return $post;
}
stew_wilde at hotmail dot com 07-Aug-2003 10:25
When using the exec() function in php to execute anther php script, any command line arguments passed the script will lose their type association, regardless of whether they are numeric or not, the same seems to hold true for strings as well.

ie : two scripts test.php:

<?php
$val = trim($argv[1]);
echo is_string($val);
?>

and testwrapper.php:

<?php
$tmp = 5;
exec("php ./test.php ".$tmp);
?>

Executing testwrapper.php on the command line will echo nothing (ie false), and false will be returned regardless of any escaping of parameters or other such attempts to overcome this.  The solution then is to explicitly cast $val in test.php to be an int and then is_numeric will work.  But as stated the same test was performed using a string for $val and the is_string() function and the same thing occurs.  Not the end of the world, but something to be aware of :)
andi_nrw at hotmail dot com 12-Feb-2003 05:25
<?php
/* This function is not useful if you want
to check that someone has filled in only
numbers into a form because for example
4e4 and 444 are both "numeric".

I used a regular expression for this problem
and it works pretty good. Maybe it is a good
idea to write a function and then to use it.

$input_number = "444"; // Answer 1
$input_number = "44 "; // Answer 2
$input_number = "4 4"; // Answer 2
$input_number = "4e4"; // Answer 2
$input_number = "e44"; // Answer 2
$input_number = "e4e"; // Answer 2
$input_number = "abc"; // Answer 2
*/
$input_number = "444";

if (preg_match ("/^([0-9]+)$/", $input_number)) {
     print "Answer 1";
} else {
     print "Answer 2";
}
?>
redy dot r at madagascan dot com 11-Dec-2002 01:54
Be aware if you use is_numeric() or is_float() after using set_locale(LC_ALL,'lang') or set_locale(LC_NUMERIC,'lang')

Example :
If at the beginning of your script, you declare :

<?php
set_locale(LC_NUMERIC,'fr');
?>

and after, you will get this :

<?php
is_numeric(12.25); // Return False
is_numeric(12,25); // Return True
is_float(12.25); //Return False
is_float(12,25); //Return True
?>

Because, for french language the decimal separator is ',' (Comma) instead of '.' (Dot).
eagleflyer2 at lycos dot com 06-Sep-2002 10:02
Hi !
Many of you may have experienced that the 'is_numeric' function seems to fail always when form entries are checked against their variable type. So the function seems to return  'false' even if the form entry was aparently a number or numeric string.

The solution is pretty simple and no subroutines or fancy operations are necessary to make the 'is_numeric' function usable for form entry checks:

Simply strip off all (invisible) characters that may be sent along with the value when submitting a form entry.

Just use the 'trim' function before 'is_numeric'.

Example:

$variable = trim($variable);
if (is_numeric($variable)
{...#do something#...}
else
{...#do something else#...}


변수가 객체인지 확인합니다

  • ≪ is_numeric
  • is_real ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수가 객체인지 확인합니다

is_object

(PHP 4, PHP 5, PHP 7)

is_object — 변수가 객체인지 확인합니다

설명

bool is_object ( mixed $var )

주어진 변수가 객체인지 확인합니다.

인수

var

평가할 변수.

반환값

var가 object면 TRUE를, 아니라면 FALSE를 반환합니다.

예제

Example #1 is_object() 예제

<?php
// 객체에서 배열을 반환하는
// 간단한 함수를 선언합니다.
fucntion get_students($obj)
{
        if(!is_object($obj))
        {
                return(false);
        }

        return($obj->students);
}

// 새 클래스를 선언하고
// 값을 채웁니다.
$obj = new stdClass;
$obj->students = Array('Kalle', 'Ross', 'Felipe');

var_dump(get_students(NULL));
var_dump(get_students($obj));
?>

주의

Note:

클래스 정의가 존재하지 않는 일렬화를 푼 객체에 대해서 FALSE를 반환합니다. (gettype()은 object를 반환합니다)

참고

  • is_bool() - 변수가 논리형인지 확인
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_float() - 변수의 자료형이 소수인지 확인합니다
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • is_array() - 변수가 배열인지 확인

User Contributed Notes

shadow_games at abv dot bg 12-Jun-2014 12:42
here i created one function that i wanted to find but i had never found :X

    function compare_two_object_recursive($object_1, $object_2, $object_1_Identifier = false, $object_2_Identifier = false){
        $object1 = (array)$object_1;
        $object2 = (array)$object_2;
        $object3 = array();

        $o1i = $object_1_Identifier ? $object_1_Identifier : 1;
        $o2i = $object_2_Identifier ? $object_2_Identifier : 2;

        foreach($object1 as $key => $value){
            if(is_object($object1[$key])){
                $object1[$key] = (array)$object1[$key];
                $object2[$key] = (array)$object2[$key];
                $object3[$key] = (object)compare_two_object_recursive($object1[$key], $object2[$key], $o1i, $o2i);
            }elseif(is_array($object1[$key])){
                $object3[$key] = compare_two_object_recursive($object1[$key], $object2[$key], $o1i, $o2i);
            }else{
                if($object1[$key] == $object2[$key]){
                    $object3[$key]['comparison_status'] = "SAME";
                }else{
                    $object3[$key]['comparison_status'] = "NOT THE SAME";
                    $object3[$key][$o1i] = $object1[$key];
                    $object3[$key][$o2i] = $object2[$key];
                }
            }
        }
        return $object3;
    }
mark at not4you dot com 21-Sep-2011 09:23
Unserializes data as returned by the standard PHP serialize() function. If the unserialized object is not an array, it will be converted to one, particularily useful if it returns a __PHP_Incomplete_Class.

<?php
/**
 *
 * @param string $data Serialized data
 *
 * @return array    Unserialized array
 */
function unserialize2array($data) {
    $obj = unserialize($data);
    if(is_array($obj)) return $obj;
    $arr = array();
    foreach($obj as $k=>$v) {
        $arr[$k] = $v;
    }
    unset($arr['__PHP_Incomplete_Class_Name']);
    return $arr;
}
?>
peter dot nagel at portavita dot nl 22-Mar-2011 10:46
Note: is_object(null) returns false

This should actually be part of the input/output specification at the top of this page.
will 20-Mar-2010 11:36
Just discovered:
is_a  (  object $object  ,  string $class_name  )
Which checks if the object is of this class or has this class as one of its parents

Which seems to do what a lot here are trying to replicate
ldean at saleamp dot com 01-Sep-2009 04:16
Use instanceof() to check for a specific type.
Senthryl 17-Mar-2009 03:03
Cleaning it up even more:

<?php
function is_obj(&$object, $className = null, $caseSensitive = true) {
    return is_object($object) && (!is_string($className) || preg_match('/^'.$className.'$/D'.($caseSensitive ? '' : 'i'), get_class($object)));
}
?>
gregdangelo at gmail dot com 14-Apr-2008 09:03
cleaned up peter's code... use only one return statement

function is_obj( &$object, $check=null, $strict=true )
{
$result = false;
  if (is_object($object)) {
      if ($check == null) {
          $result =  true;
      } else {
           $object_name = get_class($object);
           $result =  ($strict === true)?
               ( $object_name == $check ):
               ( strtolower($object_name) == strtolower($check) );
      }
  }
return $result;
}
corychristison[aT-]lavacube(.dot)com 03-Feb-2005 02:06
Thank you victor AT fourstones DOT net.

I have written a function to do what victor has suggested, with the ease of use of is_object. It can be used to replace is_object(), but has an extra field [$check], to compare to a certain name. If $check is left empty, it will just check if &$object is an object.

<?php

function is_obj( &$object, $check=null, $strict=true )
{
    if( $check == null && is_object($object) )
    {
        return true;
    }
    if( is_object($object) )
    {
        $object_name = get_class($object);
        if( $strict === true )
        {
            if( $object_name == $check )
            {
                return true;
            }
        }
        else
        {
            if( strtolower($object_name) == strtolower($check) )
            {
                return true;
            }
        }
    }
}

?>

This could probably be cleaned up, but it's spaced out to be easy to read.
victor AT fourstones DOT net 02-Jan-2005 12:49
er, I don't think that's right, especially if calling from another object instance:

<?

function test_this()
{
    $c2 = new C2();
    $c2->func();
    $c1 = new C1();
    $c1->func();
    C1::func();
}

class C2
{
    function func()
    {
        C1::func();
    }
}

class C1
{
    function func()
    {
        if( isset($this) )
        {
            if( strtolower(get_class($this)) != 'c1' )
                print("oops\n");
            else
                print("this is ok\n" );
        }
        else
        {
            print("static call\n");
        }
    }
}

test_this();
?>

yields:
---------- run-php ----------

oops
this is ok
static call
corychristison[aT-]lavacube(.dot)com 01-Jan-2005 04:47
You can use is_object($this) to detect if the function is being called via instance or procedure.

Example:

<?php

class mrClass {

    function test( )
    {
        if( is_object($this) )
        {
         // do something for instance method
            echo 'this is an instance call <br />' . "\n";
        }
        else
        {
         // do something different for procedural method
            echo 'this is a procedure call <br />' . "\n";
        }
    }

}

$inst = new mrClass();
$inst->test();

mrClass::test();

?>

This would output:
this is an instance call <br />
this is a procedure call <br />

:-) Happy coding!
lbjay can be emailed at reallywow dot com 02-May-2003 09:18
I'm not even sure how to articulate this, so I'm going to just include test code. Maybe someone else will someday wonder the same thing.

<?
    error_reporting(E_ALL);
    class testParent
    {
        var $child;

        function testParent()
        {
            $this->child = new testChild();
        }
    }

    class testChild
    {
        function testChild()
        {
        }
    }

    $parent = new testParent();
    $parent2 = 'foobar';

    print join(',', Array(
        is_object($parent) ? 'yes' : 'no',
        is_object($parent->child) ? 'yes' : 'no',
        is_object($parent2) ? 'yes' : 'no',
        is_object($parent2->child) ? 'yes' : 'no'
    ));

?>

This prints "yes,yes,no,no". Basically this shows that you can use is_object to test if the child object is an object without worrying about an error if the parent object isn't an object either.


별칭: is_float

  • ≪ is_object
  • is_resource ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 별칭: is_float

is_real

(PHP 4, PHP 5, PHP 7)

is_real — 별칭: is_float()

설명

이 함수는 다음 함수의 별칭입니다: is_float().

User Contributed Notes

charlescbe16 at gmail dot com 24-Jul-2010 09:53
is_float — Finds whether a variable is a floatDescriptionbool is_float ( mixed var )

Finds whether the given variable is a float.

example:
<?php
$float=50.97;
if(is_float($float))
{
    echo $float."is a float";
}
else
{
    echo $float. "is not a float";
}
?>

output:
50.97 is a float


변수가 자원인지 확인

  • ≪ is_real
  • is_scalar ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수가 자원인지 확인

is_resource

(PHP 4, PHP 5, PHP 7)

is_resource — 변수가 자원인지 확인

설명

bool is_resource ( mixed $var )

주어진 변수가 자원인지 확인합니다.

인수

var

평가할 변수.

반환값

var가 resource이면 TRUE, 아니면 FALSE를 반환합니다.

예제

Example #1 is_resource() 예제

<?php

$db_link = @mysql_connect('localhost', 'mysql_user', 'mysql_pass');
if (!is_resource($db_link)) {
    die('Can\'t connect : ' . mysql_error());
}

?>

참고

  • 자원형 문서
  • get_resource_type() - 자원형을 반환

User Contributed Notes

CertaiN 17-Jan-2014 02:05
Try this to know behavior:

<?php
function resource_test($resource, $name) {
    echo
        '[' . $name. ']',
        PHP_EOL,
        '(bool)$resource => ',
        $resource ? 'TRUE' : 'FALSE',
        PHP_EOL,
        'get_resource_type($resource) => ',
        get_resource_type($resource) ?: 'FALSE',
        PHP_EOL,
        'is_resoruce($resource) => ',
        is_resource($resource) ? 'TRUE' : 'FALSE',
        PHP_EOL,
        PHP_EOL
    ;
}

$resource = tmpfile();
resource_test($resource, 'Check Valid Resource');

fclose($resource);
resource_test($resource, 'Check Released Resource');

$resource = null;
resource_test($resource, 'Check NULL');
?>

It will be shown as...

[Check Valid Resource]
(bool)$resource => TRUE
get_resource_type($resource) => stream
is_resoruce($resource) => TRUE

[Check Released Resource]
(bool)$resource => TRUE
get_resource_type($resource) => Unknown
is_resoruce($resource) => FALSE

[Check NULL]
(bool)$resource => FALSE
get_resource_type($resource) => FALSE
Warning:  get_resource_type() expects parameter 1 to be resource, null given in ... on line 10
is_resoruce($resource) => FALSE
btleffler [AT] gmail [DOT] com 12-May-2011 10:55
I was recently trying to loop through some objects and convert them to arrays so that I could encode them to json strings.

I was running into issues when an element of one of my objects was a SoapClient. As it turns out, json_encode() doesn't like any resources to be passed to it. My simple fix was to use is_resource() to determine whether or not the variable I was looking at was a resource.

I quickly realized that is_resource() returns false for two out of the 3 resources that are typically in a SoapClient object. If the resource type is 'Unknown' according to var_dump() and get_resource_type(), is_resource() doesn't think that the variable is a resource!

My work around for this was to use get_resource_type() instead of is_resource(), but that function throws an error if the variable you're checking isn't a resource.

So how are you supposed to know when a variable is a resource if is_resource() is unreliable, and get_resource_type() gives errors if you don't pass it a resource?

I ended up doing something like this:

<?php

function isResource ($possibleResource) { return !is_null(@get_resource_type($possibleResource)); }

?>

The @ operator suppresses the errors thrown by get_resource_type() so it returns null if $possibleResource isn't a resource.

I spent way too long trying to figure this stuff out, so I hope this comment helps someone out if they run into the same problem I did.


변수가 스칼라인지 확인

  • ≪ is_resource
  • is_string ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수가 스칼라인지 확인

is_scalar

(PHP 4 >= 4.0.5, PHP 5, PHP 7)

is_scalar — 변수가 스칼라인지 확인

설명

bool is_scalar ( mixed $var )

주어진 변수가 스칼라인지 확인합니다.

스칼라 변수는 integer, float, string, boolean을 포함합니다. array, object, resource는 스칼라가 아닙니다.

Note:

is_scalar()는 resource형 값을 스칼라로 간주하지 않습니다. 자원은 추상적인 자료형으로 현재는 정수에 기반하고 있지만, 이 구현 상세에 의존해선 안되며, 변경될 수 있습니다.

인수

var

평가할 변수.

반환값

var가 스칼라이면 TRUE, 아니면 FALSE를 반환합니다.

예제

Example #1 is_scalar() 예제

<?php
function show_var($var)
{
    if (is_scalar($var)) {
        echo $var;
    } else {
        var_dump($var);
    }
}
$pi = 3.1416;
$proteins = array("hemoglobin", "cytochrome c oxidase", "ferredoxin");

show_var($pi);
show_var($proteins)

?>

위 예제의 출력:

3.1416
array(3) {
  [0]=>
  string(10) "hemoglobin"
  [1]=>
  string(20) "cytochrome c oxidase"
  [2]=>
  string(10) "ferredoxin"
}

참고

  • is_float() - 변수의 자료형이 소수인지 확인합니다
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_numeric() - 변수가 수나 수 문자열인지 확인합니다
  • is_real() - 별칭: is_float
  • is_string() - 변수의 자료형이 문자열인지 확인합니다
  • is_bool() - 변수가 논리형인지 확인
  • is_object() - 변수가 객체인지 확인합니다
  • is_array() - 변수가 배열인지 확인

User Contributed Notes

webmaster at oehoeboeroe dot nl 04-May-2009 09:15
Here's a little function that will test whether a variable can be used as offset to an array.

<?php
function is_offset(&$var) {
    return (is_scalar($var) || is_null($var)) && !is_resource($var);
}
?>

The resource check is currently redundant, but according to the manual that may change in the future.
31-Jul-2006 05:59
Another warning in response to the previous note:
> just a warning as it appears that an empty value is not a scalar.

That statement is wrong--or, at least, has been fixed with a later revision than the one tested.  The following code generated the following output on PHP 4.3.9.

CODE:
<?php
    echo('is_scalar() test:'.EOL);
    echo("NULL: "      . print_R(is_scalar(NULL),     true) . EOL);
    echo("false: "    . print_R(is_scalar(false),   true) . EOL);
    echo("(empty): "  . print_R(is_scalar(''),      true) . EOL);
    echo("0: "         . print_R(is_scalar(0),       true) . EOL);
    echo("'0': "      . print_R(is_scalar('0'),     true) . EOL);
?>

OUTPUT:
is_scalar() test:
NULL:
false: 1
(empty): 1
0: 1
'0': 1

THUS:
   * NULL is NOT a scalar
   * false, (empty string), 0, and "0" ARE scalars
efelch at gmail dot com 17-Aug-2005 11:31
A scalar is a single item or value, compared to things like arrays and objects which have multiple values. This tends to be the standard definition of the word in terms of programming. An integer, character, etc are scalars. Strings are probably considered scalars since they only hold "one" value (the value represented by the characters represented) and nothing else.
Dr K 14-Aug-2005 12:48
Having hunted around the manual, I've not found a clear statement of what makes a type "scalar" (e.g. if some future version of the language introduces a new kind of type, what criterion will decide if it's "scalar"? - that goes beyond just listing what's scalar in the current version.)

In other lanuages, it means "has ordering operators" - i.e. "less than" and friends.

It (-:currently:-) appears to have the same meaning in PHP.
popanowel HAT hotmailZ DOT cum 14-May-2004 09:31
Hi ... for newbees here, I just want to mention that reference and scalar variable aren't the same. A reference is a pointer to a scalar, just like in C or C++.

<? php  // simple reference to scalar

  $a = 2;
  $ref = & $a;

  echo "$a <br> $ref";

?>
this should print out: "2 <br> 2".

Scalar class also exists. Look below:
<? php

  class Object_t {

     var $a;

     function Object_t ()  // constructor
     {
        $this->a = 1;
     }

  }

  $a = new Object_t; // we define a scalar object

  $ref_a = &a;

  echo "$a->a <br> $ref->a";

?>
again, this should echo: "1 <br> 1";

Here is another method isued in OOP to acheive on working only over reference to scalar object. Using this, you won't ever have to  ask yourself if you work on a copy of the scalar or its reference. You will only possess reference to the scalar object. If you want to duplicate the scalar object, you will have to create a function for that purpose that would read by the reference the values and assign them to another scalar of the same type... or an other type, it is as you wish at that moment.
<?php

  class objet_t {
     var $a;

     function object_t
     {
        $this->a = "patate_poil";
     }
  }

   function &get_ref($object_type)
   {
      // here we create a scalar object in memory
      // and we return it by reference to the calling
      // control scope.
      return &new $object_type;
   }

   $ref_object_t = get_ref(object_t);

   echo "$ref_object_t->a <br>";

?>
this should echo: "patate_poit <br>".

The only thing that I try to demonstrate is that scalar variable ARE object in memory while a reference is usualy a variable (scalar object) that contain the address of another scalar object, which contain the informations you want by using the reference.

Good Luck!

otek is popanowel HAT hotmailZ DOT cum
bps7j at yahoNOSPAMo.com 09-Feb-2004 06:56
is_scalar(null) is false.  Apparently a variable needs to have a value to be considered a scalar.


변수의 자료형이 문자열인지 확인합니다

  • ≪ is_scalar
  • isset ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수의 자료형이 문자열인지 확인합니다

is_string

(PHP 4, PHP 5, PHP 7)

is_string — 변수의 자료형이 문자열인지 확인합니다

설명

bool is_string ( mixed $var )

주어진 변수가 문자열인지 확인합니다.

인수

var

평가할 변수.

반환값

var가 string형이면 TRUE를, 아니라면 FALSE를 반환합니다.

예제

Example #1 is_string() 예제

<?php
if (is_string("23")) {
 echo "문자열입니다.\n";
} else {
 echo "문자열이 아닙니다.\n";
}
var_dump(is_string('abc'));
var_dump(is_string("23"));
var_dump(is_string(23.5));
var_dump(is_string(true));
?>

위 예제의 출력:

문자열입니다.
bool(true)
bool(true)
bool(false)
bool(false)

참고

  • is_float() - 변수의 자료형이 소수인지 확인합니다
  • is_int() - 변수의 자료형이 정수인지 확인합니다
  • is_bool() - 변수가 논리형인지 확인
  • is_object() - 변수가 객체인지 확인합니다
  • is_array() - 변수가 배열인지 확인

User Contributed Notes

laszlo dot heredy shift-two gmail etc 28-Feb-2014 06:22
Using is_string() on an object will always return false (even with __toString()).

<?php
class B {
  public function __toString() {
    return "Instances of B() can be treated as a strings!\n";
  }
}

$b = new B();
print($b); //Instances of B() can be treated as a strings!
print(is_string($b) ? 'true' : 'false'); //false
?>
Sammy Moshe 24-Jul-2013 08:43
The cool thing about this function is that it lets you be a lot more specific with your logic than you could have been before. And since I'm all about explicit control, when it comes to code, this one gets me all hot and bothered.

<?php
     //Like this. You could also do the reverse of it.
     if(is_string($variable) ) && !is_array($variable) ){
          //Do something that appeals to your controlling nature.
     }

?>

God bless those php dev guys for thinking of everything.
slicky at newshelix dot com 20-Mar-2001 02:06
Be aware that this function could result into unwanted results if you check single chars in an array.
For instance:
You have a simple string and want to repeat this string evertime it ends.

<?php
for ($i=0; $i < strlen($string);$i++){
    if(!is_string($key[$x]))
        $x = 0;
    echo $key[$x];
    $x++;
}
?>

This will print you out the key, but won't iterate it since $x won't be reset. is_string function will give out true at element in that string, even if it doesn't exist. for instance is_string $key[1000] while your string is just 10chars long.
However this doesn't matter just use $key[$x] == '' instead.
Of course you won't use this to iterate a string, but you might come across something where this get useful, for instance for a en/decrypt engine.


설정된 변수인지 확인

  • ≪ is_string
  • print_r ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 설정된 변수인지 확인

isset

(PHP 4, PHP 5, PHP 7)

isset — 설정된 변수인지 확인

설명

bool isset ( mixed $var [, mixed $var [, $... ]] )

변수가 설정되었는지 검사합니다.

변수를 unset()으로 지우면, 더 이상 설정되어 있지 않습니다. NULL로 지정한 변수를 확인하면 isset()은 FALSE를 반환합니다. NULL 바이트("\0")는 PHP NULL 상수와 다릅니다.

여러 인수가 주어지면, isset()은 모든 변수가 설정되었을 경우에만 TRUE를 반환합니다. 평가는 왼쪽에서 오른쪽으로 이루어지며, 설정되지 않은 변수를 만나면 바로 중단합니다.

인수

var

확인할 변수.

var

다른 변수 ..

...

반환값

var가 존재하면 TRUE, 아니면 FALSE를 반환합니다.

예제

Example #1 isset() 예제

<?php

$var = '';

// TRUE이기에 텍스트를 출력합니다.
if (isset($var)) {
    echo "이 변수는 설정되었으므로 출력합니다.";
}

// 다음 예제는 isset()의 반환값을
// 출력하기 위해서 var_dump를 사용합니다.

$a = "test";
$b = "anothertest";

var_dump(isset($a));     // TRUE
var_dump(isset($a, $b)); // TRUE

unset ($a);

var_dump(isset($a));     // FALSE
var_dump(isset($a, $b)); // FALSE

$foo = NULL;
var_dump(isset($foo));   // FALSE

?>

배열 원소에도 작동합니다:

<?php

$a = array ('test' => 1, 'hello' => NULL);

var_dump(isset($a['test']));             // TRUE
var_dump(isset($a['foo']));              // FALSE
var_dump(isset($a['hello']));            // FALSE

// 'hello' 키는 NULL이기에 정의되지 않은 것으로 판단합니다.
// NULL 키를 확인하려면 다음을 실행하십시오:
var_dump(array_key_exists('hello', $a)); // TRUE

?>

주의

Warning

isset()은 변수만을 받아들이며, 다른 것을 넘기면 해석 오류가 발생합니다. 상수가 정의되었는지 확인하려면 defined() 함수를 사용하십시오.

Note: 이것은 함수가 아닌 언어 구조이기 때문에, 가변 함수 방식으로 호출할 수 없습니다.

참고

  • empty() - 변수가 비어있는지 검사합니다
  • unset() - 주어진 변수를 제거
  • defined() - Checks whether a given named constant exists
  • 자료형 비교표
  • array_key_exists() - 주어진 키와 인덱스가 배열에 존재하는지 확인
  • is_null() - 변수가 NULL인지 확인합니다
  • 오류 제어 @ 연산자

User Contributed Notes

p_ignorethis_lbowers at gmail dot com 06-Oct-2016 04:26
I, too, was dismayed to find that isset($foo) returns false if ($foo == null). Here's an (awkward) way around it.

unset($foo);
if (compact('foo') != array()) {
  do_your_thing();
}

Of course, that is very non-intuitive, long, hard-to-understand, and kludgy. Better to design your code so you don't depend on the difference between an unset variable and a variable with the value null. But "better" only because PHP has made this weird development choice.

In my thinking this was a mistake in the development of PHP. The name ("isset") should describe the function and not have the desciption be "is set AND is not null". If it was done properly a programmer could very easily do (isset($var) || is_null($var)) if they wanted to check for this!

A variable set to null is a different state than a variable not set - there should be some easy way to differentiate. Just my (pointless) $0.02.
ayyappan dot ashok at gmail dot com 24-Mar-2016 04:23
Return Values :
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

<?php
$a=NULL;
$b=FALSE; //The out put was TRUE.
$c=TRUE;
$d='';
$e="";
if(isset($b)):
echo "TRUE";
else:
echo "FALSE";
endif;
?>
Could any one explain me in clarity.
ohcc at 163 dot com 21-Nov-2015 05:59
When a variable is set and its values is null, isset() returns false.

You can use array_key_exists() to check whether a null value variable exists.

<?php
    $wxc = null;
    var_dump( isset( $wxc ) );
    var_dump( array_key_exists( 'wxc', $GLOBALS ) );
?>

Output of the above example:

bool(false) bool(true)
ericg at arty-web-design dot com 25-Jul-2015 07:00
I found something interesting while working with isset() in PHP5.5+

<?php

$a = "foo";
$b = "bar";

var_dump(isset($a, $b)); //returns true

unset($b);

var_dump(isset($a,$b)); // returns false (as expected);

...BUT...
var_dump(!isset($a,$b)); // STILL returns true!
andreasonny83 at gmail dot com 12-May-2015 08:26
Here is an example with multiple parameters supplied

<?php
$var = array();
$var['val1'] = 'test';
$var['val2'] = 'on';

if ( isset( $var['val1'], $var['val2'] ) && $var['val2'] === 'on' ) {
    unset( $var['val1'] );
}
print_r( $var );
?>

This will output:
Array
(
    [val2] => on
)

The following code does the same calling "isset" 2 times:

<?php
$var = array();
$var['val1'] = 'test';
$var['val2'] = 'on';

if ( isset( $var['val1'] ) && isset( $var['val2'] ) && $var['val2'] === 'on' ) {
    unset( $var['val1'] );
}
print_r( $var );
?>
flobee 08-Nov-2013 11:38
KISS: array_key_exists() is often the answer, not isset()
<?php
$array['secure'] = null;
if ( isset( $array['secure'] ) ) {
    // what happens here?
}?>
falkon303 at gmail dot com 03-Oct-2013 08:23
Here is another way to check if you have any $_GET variable set.

<?php

if (sizeof($_GET) > 0)

{echo "There are $_GET variables.";}
 else
{echo "There are no $_GET variables.";};

?>

Alternatively you could use the switch method.
marcio at suhdo dot com 08-Jul-2013 08:08
//thin way to set a variable
$foo  = isset($_POST['foo']) ? $_POST['foo'] : null;
uramihsayibok, gmail, com 26-Apr-2013 08:32
Note that isset() is not recursive as of the 5.4.8 I have available here to test with: if you use it on a multidimensional array or an object it will not check isset() on each dimension as it goes.

Imagine you have a class with a normal __isset and a __get that fatals for non-existant properties. isset($object->nosuch) will behave normally but isset($object->nosuch->foo) will crash. Rather harsh IMO but still possible.

<?php

class FatalOnGet {

    // pretend that the methods have implementations that actually try to do work
    // in this example I only care about the worst case conditions

    public function __get($name) {
        echo "(getting {$name}) ";

        // if property does not exist {
            echo "Property does not exist!";
            exit;
        // }
    }

    public function __isset($name) {
        echo "(isset {$name}?) ";
        // return whether the property exists
        return false;
    }

}

$obj = new FatalOnGet();

// works
echo "Testing if ->nosuch exists: ";
if (isset($obj->nosuch)) echo "Yes"; else echo "No";

// fatals
echo "\nTesting if ->nosuch->foo exists: ";
if (isset($obj->nosuch->foo)) echo "Yes"; else echo "No";

// not executed
echo "\nTesting if ->irrelevant exists: ";
if (isset($obj->irrelevant)) echo "Yes"; else echo "No";

?>

    Testing if ->nosuch exists: No
    Testing if ->nosuch->foo exists: Property does not exist!

Uncomment the echos in the methods and you'll see exactly what happened:

    Testing if ->nosuch exists: (isset nosuch?) No
    Testing if ->nosuch->foo exists: (getting nosuch) Property does not exist!

On a similar note, if __get always returns but instead issues warnings or notices then those will surface.
Hayle Watson 22-Apr-2013 02:39
<?php
return isset($nonexistentarray['foo']);
?>
Returns false without trouble (assuming $nonexistent array does not exist).

<?php
$foo = 17;
return isset($nonexistentarray[$foo]);
?>
Also returns false without trouble.

But, oddly,
<?php
return isset($nonexistentarray[$nonexistentkey]);
?>
While returning false, also triggers a notice regarding the undefined variable "$nonexistentkey".

Some thought shows that this is the right behaviour (we're testing for the existence of an array element, but we need to know which array element we're testing the existence of), but it probably isn't obvious. This implies that:
<?php
$extantarray[null] = 42;
return isset($extantarray[$nonexistentkey]);
?>
Triggers a notice and returns true.
Daniel Klein 29-May-2012 12:49
How to test for a variable actually existing, including being set to null. This will prevent errors when passing to functions.

<?php
// false
var_export(
  array_key_exists('myvar', get_defined_vars())
);

$myvar;
// false
var_export(
  array_key_exists('myvar', get_defined_vars())
);

$myvar = null;
// true
var_export(
  array_key_exists('myvar', get_defined_vars())
);

unset($myvar);
// false
var_export(
  array_key_exists('myvar', get_defined_vars())
);

if (array_key_exists('myvar', get_defined_vars())) {
  myfunction($myvar);
}
?>

Note: you can't turn this into a function (e.g. is_defined($myvar)) because get_defined_vars() only gets the variables in the current scope and entering a function changes the scope.
glitch dot mr at gmail dot com 24-May-2012 03:02
This is very simple implementation of issetor() for PHP. It works like ?? in C#, // in Perl (it can access undefined hash keys without it nevertheless) or ? in CoffeeScript (also can access undefined hash keys too). You shouldn't use directly $_GET['something'] (it can generate E_NOTICEs in case somebody would try hacking your site), but isset($_GET['something']) ? $_GET['something'] : '' is long for many people (for so common operation). This alternative makes using superglobals easier.

<?php
function issetor(&$variable, $or = NULL) {
    return $variable === NULL ? $or : $variable;
}
?>

Now you can do:

<?php
echo issetor($_GET['a']);       // $_GET['a'] or NULL
echo issetor($_GET['b'], '42'); // $_GET['b'] or 42
echo issetor($_GET['c']['d']);  // $_GET['c']['d'] or NULL
?>

This code doesn't generate notices in case variable doesn't exist.
david at thegallagher dot net 09-Mar-2012 11:20
Just to reiterate on what everyone has already said before, you should not use wrapper functions for isset. Using a wrapper function will generate a Notice unless you pass the unset variable by reference, in which case it is the equivalent to writing using $var === null (which is a lot faster). Even if you do pass the variable by reference, you could still get notices using multidimensional arrays where isset() would silently return false.
qeremy 08-Mar-2012 12:36
Simple solution for: "Fatal error: Can't use function return value in write context in ..."

<?php
function _isset($val) { return isset($val); }
?>
john at darven dot co dot uk 14-Nov-2011 11:37
It is worth noting that in order to check for the existence of a key within an array, regardless of it's contents one should use array_key_exists() not isset().

isset() will (correctly) return false if the value of your array key is null, which may be a perfectly valid condition.
gonchalox at gmail dot com 25-Jun-2011 01:33
Useful to check if the variable have some value...specially for GET POST variables

<?php
function isset_or(&$check, $alternate = NULL)
{
    return (isset($check)) ? (empty($check) ? $alternate : $check) : $alternate;
}

function getGETPOST($var)
{
      return isset_or($_GET[$var],isset_or($_POST[$var],"Empty"));
}
?>

Example
echo getGETPOST('na'); //Find the na varriabl by get and post
benallfree at gmail dot com 22-Apr-2011 03:21
isset() returns TRUE if a value is NULL. That seems wrong to me as there is no way to distinguish between a value set to NULL and a truly undefined value.

If you have this problem inside a class, there is a fix:

<?php
class T
{
  function __isset($att)
  {
    $props = get_object_vars($this);
    return array_key_exists($att, $props);
  }
}

$x = new T();
$x->foo_exists = 4;

var_dump(isset($x->foo_exists)); // TRUE
var_dump(isset($x->bar_exists)); // FALSE
?>
[EDITOR thiago NOTE: This snippet has improvements by "Paul Lashbrook"]
Cuong Huy To 23-Mar-2011 02:08
1) Note that isset($var) doesn't distinguish the two cases when $var is undefined, or is null. Evidence is in the following code.

<?php
unset($undefined);
$null = null;
if (true === isset($undefined)){echo 'isset($undefined) === true'} else {echo 'isset($undefined) === false'); // 'isset($undefined) === false'
if (true === isset($null)){echo 'isset($null) === true'} else {echo 'isset($null) === false');              // 'isset($null)      === false'
?>

2) If you want to distinguish undefined variable with a defined variable with a null value, then use array_key_exist

<?php
unset($undefined);
$null = null;

if (true !== array_key_exists('undefined', get_defined_vars())) {echo '$undefined does not exist';} else {echo '$undefined exists';} // '$undefined does not exist'
if (true === array_key_exists('null', get_defined_vars())) {echo '$null exists';} else {echo '$null does not exist';}                // '$null exists'
?>
francois vespa 23-Dec-2010 04:21
Now this is how to achieve the same effect (ie, having isset() returning true even if variable has been set to null) for objects and arrays

<?php

// array

$array=array('foo'=>null);

return isset($array['foo']) || array_key_exists('foo',$array)
  ? true : false ; // return true

return isset($array['inexistent']) || array_key_exists('inexistent',$array)
  ? true : false ; // return false

// static class

class bar

{
  static $foo=null;
}

return isset(bar::$foo) || array_key_exists('foo',get_class_vars('bar'))
  ? true : false ; // return true

return isset(bar::$inexistent) || array_key_exists('inexistent',get_class_vars('bar'))
  ? true : false ; // return false

// object

class bar
{
    public $foo=null;
}

$bar=new bar();

return isset($bar->foo) || array_key_exists('foo',get_object_vars($bar))
  ? true : false ; // return true

return isset($bar->inexistent) || array_key_exists('inexistent',get_object_vars($bar))
  ? true : false ; // return true

// stdClass

$bar=new stdClass;
$bar->foo=null;

return isset($bar->foo) || array_key_exists('foo',get_object_vars($bar))
  ? true : false ; // return true

return isset($bar->inexistent) || array_key_exists('inexistent',get_object_vars($bar))
  ? true : false ; // return true

?>
i [at] nemoden [dot] com 15-Jun-2010 08:48
Simple, but very useful:

<?php
function issetOr($var, $or = false) {
  return isset($var) ? $var : $or;
}
?>

Some examples:
<?php
$a = '1';
$b = '2';
echo issetOr($a,issetOr($b,3)); // 1
?>
<?php
$b = '2';
echo issetOr($a,issetOr($b,3)); // 2
?>
<?php
echo issetOr($a,issetOr($b,3)); // 3
?>
Robert dot VanDell at cbs dot com 08-Jan-2010 11:19
Here's a nice little function that I use everywhere that'll help with setting alternate values so you don't have a bunch of situations like:

<?php
if(isset($a))
{
    $b = $a;
}
else
{
    $b = "default";
}

function isset_or(&$check, $alternate = NULL)
{
    return (isset($check)) ? $check : $alternate;
}

// Example usage:
$first_name = isset_or($_POST['first_name'], "Empty");
$total        = isset_or($row['total'], 0);

?>
Anl zselgin 15-Aug-2009 12:30
Note: Because this is a language construct and not a function, it cannot be called using variable functions.

So why it is under "Variable handling Functions". Maybe there should be some good documentation field for language constructs.
Ashus 08-Dec-2008 10:05
Note that array keys are case sensitive.

<?php
$ar['w'] = true;

var_dump(isset($ar['w']),
      isset($ar['W']));
?>

will report:
bool(true) bool(false)
jwvdveer at gmail dot com 10-Nov-2008 07:29
Here a short note on the function tomek wrote:
Don't use it, because it is still better to use !$var than !is($var).

Some comments on the body of the function:
<?php
function is($var)
{
if (!isset($var)) return false; # Variable is always set... Otherwise PHP would have thrown an error on call.
if ($var!==false) return true; # So, 0, NULL, and some other values may not behave like isNot? And what about the difference between a class and NULL?
return false;
}
?>

The reason why you shall not use this function:
Notice: Undefined variable: {variablename} in {file} on line {__LINE__}

It's me as plain as the nose on your face that the piece of code hasn't been tested with E_NOTICE.

So my advice in this case is: don't use the above function, but simply use !, and functions such like is_null in the situation they are made for.
tomek 26-Oct-2008 03:48
Here's a simple function to test if the variable is set:

<?php
function is($var)
{
if (!isset($var)) return false;
if ($var!==false) return true;
return false;
}
?>

Now instead of very popular (but invalid in many situations):

if (!$var) $var=5;

you can write:

if (!is($var)) $var=5;
a dot schaffhirt at sedna-soft dot de 12-Oct-2008 05:01
You can safely use isset to check properties and subproperties of objects directly. So instead of writing

    isset($abc) && isset($abc->def) && isset($abc->def->ghi)

or in a shorter form

    isset($abc, $abc->def, $abc->def->ghi)

you can just write

    isset ($abc->def->ghi)

without raising any errors, warnings or notices.

Examples
<?php
    $abc = (object) array("def" => 123);
    var_dump(isset($abc));                // bool(true)
    var_dump(isset($abc->def));           // bool(true)
    var_dump(isset($abc->def->ghi));      // bool(false)
    var_dump(isset($abc->def->ghi->jkl)); // bool(false)
    var_dump(isset($def));                // bool(false)
    var_dump(isset($def->ghi));           // bool(false)
    var_dump(isset($def->ghi->jkl));      // bool(false)

    var_dump($abc);                       // object(stdClass)#1 (1) { ["def"] => int(123) }
    var_dump($abc->def);                  // int(123)
    var_dump($abc->def->ghi);             // null / E_NOTICE: Trying to get property of non-object
    var_dump($abc->def->ghi->jkl);        // null / E_NOTICE: Trying to get property of non-object
    var_dump($def);                       // null / E_NOTICE: Trying to get property of non-object
    var_dump($def->ghi);                  // null / E_NOTICE: Trying to get property of non-object
    var_dump($def->ghi->jkl);             // null / E_NOTICE: Trying to get property of non-object
?>
mark dot fabrizio at gmail dot com 16-Sep-2008 04:01
I know this is probably not the recommended way to do this, but it seems to work fine for me. Instead of the normal isset check to extract variables from arrays (like $_REQUEST), you can use the @ prefix to squelch any errors.

For example, instead of:
<?php
$test = isset($_REQUEST['test']) ? $_REQUEST['test'] : null;
?>
you can use:
<?php
$test = @$_REQUEST['test'];
?>

It saves some typing, but doesn't give the opportunity to provide a default value. If 'test' is not an assigned key for $_REQUEST, the assigned value will be null.
mandos78 AT mail from google 29-Jul-2008 11:40
Careful with this function "ifsetfor" by soapergem, passing by reference means that if, like the example $_GET['id'], the argument is an array index, it will be created in the original array (with a null value), thus causing posible trouble with the following code. At least in PHP 5.

For example:

<?php
$a = array();
print_r($a);
ifsetor($a["unsetindex"], 'default');
print_r($a);
?>

will print

Array
(
)
Array
(
    [unsetindex] =>
)

Any foreach or similar will be different before and after the call.
soapergem at gmail dot com 02-Jul-2008 10:34
It is possible to encapsulate isset() calls inside your own functions if you pass them by reference (note the ampersand in the argument list) instead of by value. A prime example would be the heavily-requested "ifsetor" function, which will return a value when it is set, otherwise a default value that the user specifies is used.

<?php

function ifsetor(&$val, $default = null)
{
    return isset($val) ? $val : $default;
}

//    example usage
$id = intval(ifsetor($_GET['id'], 0));

?>
soapergem at gmail dot com 04-Jun-2008 11:19
Below a user by the name of Scott posted an isval() function; I just wanted to point out a revision to his method since it's a bit lengthy for what it does. The trick is to realize that a boolean AND clause will terminate with false as soon as it encounters anything that evaluates to false, and will skip over any remaining checks.

Instead of taking up the space to define isval(), you could just run inline commands for each variable you need to check this:

<?php

$isval = isset($_POST['var']) && !empty($_POST['var']);

?>

Also be warned that if you try to encapsulate this into a function, you might encounter problems. It's meant to stand alone.
anonymousleaf at gmail dot com 25-Feb-2008 07:16
isset expects the variable sign first, so you can't add parentheses or anything.

<?php
    $foo = 1;
    if(isset(($foo))) { // Syntax error at isset((
        $foo = 2;
    }
?>
muratyaman at gmail dot com 07-Feb-2008 01:40
To organize some of the frequently used functions..

<?php

/**
 * Returns field of variable (arr[key] or obj->prop), otherwise the third parameter
 * @param array/object $arr_or_obj
 * @param string $key_or_prop
 * @param mixed $else
 */
function nz($arr_or_obj, $key_or_prop, $else){
  $result = $else;
  if(isset($arr_or_obj)){
    if(is_array($arr_or_obj){
      if(isset($arr_or_obj[$key_or_prop]))
        $result = $arr_or_obj[$key_or_prop];
    }elseif(is_object($arr_or_object))
      if(isset($arr_or_obj->$key_or_prop))
        $result = $arr_or_obj->$key_or_prop;
    }
  }
  return $result;
}

/**
 * Returns integer value using nz()
 */
function nz_int($arr_or_obj, $key_or_prop, $else){
  return intval(nz($arr_or_obj, $key_or_prop, $else));
}

$my_id = nz_int($_REQUEST, 'id', 0);
if($my_id > 0){
  //why?
}
?>
packard_bell_nec at hotmail dot com 25-Dec-2007 08:18
Note: isset() only checks variables as anything else will result in a parse error. In other words, the following will not work: isset(trim($name)).

isset() is the opposite of is_null($var) , except that no warning is generated when the variable is not set.
sam b 13-Nov-2007 07:51
Check out this ifsetor function. If $var is set, do nothing, otherwise $var = $default.

<?php

$name = ifsetor($name, 'default name') ;

function ifsetor(&$var, $default)
    {
        return isset($var) ? $var : $default) ;
    }

?>
contact at scottbyrns dot com 08-Nov-2007 02:51
If you have for example a variable in your URL say url.php?var= and some one types in %00 the variable will pass isset. For post and get variables I wrote this function to filter out varables that are set but empty.

function isval($inp){
    if(isset($inp)){
        $len = strlen($inp);
        if($len > 0){
            return true;
        }
        else{
            return false;
        }
    }
    else{
        return false;
    }
}
petruzanauticoyahoo?com!ar 14-Oct-2007 08:22
I've come up with a little not-so-clean but useful function to avoid checking if a variable is set before reading its value, specially useful for $_REQUEST and the like:

<?php
function toLocal( $source, &$dest, $default = null )
{
    if( isset( $source ) )
        $dest = $source;
    else
        $dest = $default;
}
?>

and then call it this way:
<?php
@toLocal( $_REQUEST['item_id'], $item_id, 0 );
?>

It checks wether the variable is set, copies it to a local variable, and if it wasn't set, it assigns the new variable a default value, all in one line, preventing you to have to always check for isset() before trying to read its value.
Gotta call it with @ because if the variable is not set, then trying to pass it as an argument will yield a warning.

Petruza.
talbutt(at)mail(dot)med(dot)upenn(edu) 13-Aug-2007 05:30
In PHP 5.2.3, really returns true if the variable is set to null.
black__ray at myway dot com 03-Jul-2007 01:40
if(isset($_POST['in_qu']))
          {

          include("qanda/in_qu.php");
          $content.=$message;
          include("qanda/view_qanda.php");
    }
elseif(isset($_GET['rq']))
          {
          include("qanda/add_answer.php");
    }
elseif(isset($_POST['add_answer']))
          {
          include("qanda/in_an.php");
          $content.=$message;
          include("qanda/view_qanda.php");
    }
elseif($_GET['act']== 'v_qanda' && !(isset($_GET['rq'])))
{
include("qanda/view_qanda.php");
}
/*
if(isset($_POST['add_answer']))
beuc at beuc dot net 01-Jan-2007 10:56
Beware that the chk() function below creates the variable or the array index if it didn't existed.

<?php
function chk(&$var) {
  if (!isset($var))
    return NULL;
  else
    return $var;
}

echo '<pre>';
$a = array();
var_dump($a);
chk($a['b']);
var_dump($a);
echo '</pre>';

// Gives:
// array
//   empty
//
// array
//   'b' => null
?>
beuc at beuc dot net 16-Dec-2006 09:35
"empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set."

So essentially
<?php
if (isset($var) && $var)
?>
is the same as
<?php
if (!empty($var))
?>
doesn't it? :)

!empty() mimics the chk() function posted before.
roberto at spadim dot com dot br 04-Dec-2006 07:04
test:

<?php
$qnt=100000;
$k=array();
for ($i=0;$i<$qnt;$i++)
$k[$i]=1;

echo microtime()."\n";
for ($i=0;$i<$qnt;$i++)if(isset($k[$i]));
echo microtime()."\n";
for ($i=0;$i<$qnt;$i++)if(array_key_exists($i,$k));
echo microtime()."\n";
for ($i=0;$i<$qnt;$i++)if($k[$i]==1);
echo microtime()."\n";

?>

the interesting result:
isset is the fastest
randallgirard at hotmail dot com 27-Sep-2006 08:51
The unexpected results of isset has been really frustrating to me. Hence, it doesn't work how you'd think it would, (as documented) a var currently in the scope with a null value will return false.

Heres a quick solution, perhaps there are better ways of going about this, but heres my solution...

<?php
function is_set( $varname, $parent=null ) {
  if ( !is_array( $parent ) && !is_object($parent) ) {
    $parent = $GLOBALS;
  }
  return array_key_exists( $varname, $parent );
}
?>

Hence, $varname should be a mixed value of var's to check for, and $parent can be an array or object, which will default to the GLOBAL scope. See the documentation of array_key_exists for further information.

This will allow to check if a var is in the current scope, object, or array... Whether it's a null, false, true, or any value. It depends on ARRAY_KEY_EXISTS for it's functionality which also works with Objects. Feel free to improve on this anyone ;D
Tee Cee 20-Aug-2006 02:20
In response to 10-Feb-2006 06:02, isset($v) is in all (except possibly buggy) cases equivalent to !is_null($v). And no, it doesn't actually test if a variable is set or not by my definition "$v is set if unset($v) has no effect".

<?php
unset($c); //force $c to be unset
var_dump($a=&$c); // NULL, but this actually sets $a and $c to the 'same' NULL.
var_dump(isset($c)); // bool(false)
var_dump($a = 5); // int(5)
var_dump($c); // int(5)

unset($c);
var_dump($a=&$c); // NULL
var_dump(isset($c)); // bool(false)
unset($c);
var_dump($a = 5); // int(5)
var_dump($c); // NULL
?>

In the following example, we see an alternate method of testing if a variable is actually set or not:
<?php
var_dump(array_key_exists('c',get_defined_vars())); // false
var_dump(isset($c));                                // also false
var_dump($c);                                       // manipulate $c a bit...
var_dump((string)$c);
var_dump(print_r($c,true));
var_dump($a=$c);
var_dump(array_key_exists('c',get_defined_vars())); // ... still false
var_dump($c = NULL);                                // this sets $c
var_dump(array_key_exists('c',get_defined_vars())); // true!
var_dump(isset($c));                                // false; isset() still says it's unset
unset($c);                                          // actually unset it
var_dump(array_key_exists('c',get_defined_vars())); // false
var_dump($a=&$c);
var_dump(array_key_exists('c',get_defined_vars())); // true!
unset($c);                                          // unset it again
var_dump(&$c);                                      // &NULL
var_dump(array_key_exists('c',get_defined_vars())); // true!
?>

Obviously, null values take up space (or they wouldn't show up in get_defined_vars). Also, note that &$v sets $v to NULL if it is unset.
purpleidea 17-Aug-2006 09:13
fyi:
you *cannot* do assignments inside of the isset() function. although you *can* while inside of other functions such as is_null().

<?php
if (isset($var = $_GET['key'])) echo 'whatever'; //this will throw an error :(

if (is_null($var = $_GET['key'])) echo 'whatever'; //this will not :)
?>

hope someone finds this useful.
ludie-at-vibage-punkt-kom 16-Aug-2006 02:26
If you don't want to bother checking every single var with isset or empty, use this function on every var you use:

<?php

function chk( & $var )
{
    if ( !isset($var) )
    {
        return NULL;
    }
    else
    {
        return $var;
    }
}

?>

It takes ANYTHING as argument, and returns the exact same thing, but without Notice if the var doesn't actually exist
21-Jul-2006 04:08
I tried the example posted previously by Slawek:

$foo = 'a little string';
echo isset($foo)?'yes ':'no ', isset($foo['aaaa'])?'yes ':'no ';

He got yes yes, but he didn't say what version of PHP he was using.

I tried this on PHP 5.0.5 and got:  yes no

But on PHP 4.3.5 I got:  yes yes

Apparently, PHP4 converts the the string 'aaaa' to zero and then returns the string character at that position within the string $foo, when $foo is not an array. That means you can't assume you are dealing with an array, even if you used an expression such as isset($foo['aaaa']['bbb']['cc']['d']), because it will return true also if any part is a string.

PHP5 does not do this. If $foo is a string, the index must actually be numeric (e.g. $foo[0]) for it to return the indexed character.
soywiz at php dot net 14-Apr-2006 03:12
Sometimes you have to check if an array has some keys. To achieve it you can use "isset" like this: isset($array['key1'], $array['key2'], $array['key3'], $array['key4'])
You have to write $array all times and it is reiterative if you use same array each time.

With this simple function you can check if an array has some keys:

<?php
function isset_array() {
    if (func_num_args() < 2) return true;
    $args = func_get_args();
    $array = array_shift($args);
    if (!is_array($array)) return false;
    foreach ($args as $n) if (!isset($array[$n])) return false;
    return true;
}
?>

Use: isset_array($array, 'key1', 'key2', 'key3', 'key4')
First parameter has the array; following parameters has the keys you want to check.
kariedoo 09-Mar-2006 07:27
Before:

//ask, if is set
$number = isset($_GET['number']) ? $_GET['number'] : '';
$age = isset($_GET['age']) ? $_GET['age'] : '';
$street = isset($_GET['street']) ? $_GET['street'] : '';

After: --> it's easier to read

//ask, if is set
 $parameter = array('number', 'age', 'street');
 foreach($parameter as $name)
 {
    $$name = isset($_GET[$name]) ? $_GET[$name] : '';
 }
red at iklanumum dot com 10-Feb-2006 07:02
This could be viewed as a philosophy. I wonder why a NULL variabel is being considered FALSE rather than TRUE while in isset, because if the variable has been unset it becomes undefined but a NULL variabel is still defined although it has no value. Or, perhaps, it's based on the memory usage, if it is how about $x="" ? Is empty value use memory too? This leads me to another thinking that the isset isn't have family relationship with unset although both of it are a language construct and have 'set' word :)
Slawek Petrykowski 29-Nov-2005 12:06
<?php
$foo = 'a little string';
echo isset($foo)?'yes ':'no ', isset($foo['aaaa'])?'yes ':'no ';
>

results with unexpected values:
yes yes

Well, it is necessary to check type of $foo first !
Peter Beckman <beckman at purplecow dot com> 21-Sep-2005 09:16
Based on the previous post, I've found this code even more useful:

<?php
function isset_sum(&$var, $val) {
    if (isset($var))  $var += $val;
    else              $var  = $val;
}
?>

Now instead of:

<?php
if (isset($foo[$bar][$baz][$fooz])) $foo[$bar][$baz][$fooz] += $count;
else                                $foo[$bar][$baz][$fooz] = $count;
?>

No more "Undefined variable" warnings, and you save your fingers and sanity!  Thanks to the previous poster for inspiration.
14-Sep-2005 12:41
I don't know if you guys can use this but i find this piece of code pretty useful (for readabillity at least):

function isset_else( $&v, $r )
{
    if( isset( $v ))
        return $v;
    else
        return $r;
}

This way you can go:

$a = 4;

$c += isset_else( $a, 0 );
$c += isset_else( $b, 0 );

echo $c;

Of course, this code would work anyway, but you get the point.
onno at itmaze dot com dot au ##php==owh 12-Aug-2005 09:33
In PHP4, the following works as expected:

<?php
if (isset($obj->thing['key'])) {
  unset($obj->thing['key']) ;
}
?>

In PHP5 however you will get a fatal error for the unset().

The work around is:

<?php
if (is_array($obj->thing) && isset($obj->thing['key'])) {
  unset($obj->thing['key']) ;
}
?>
richard william lee AT gmail 11-Jun-2005 08:38
Just a note on the previous users comments. isset() should only be used for testing if the variable exists and not if the variable containes an empty "" string. empty() is designed for that.

Also, as noted previosuly !empty() is the best method for testing for set non-empty variables.
Andrew Penry 11-May-2005 05:17
The following is an example of how to test if a variable is set, whether or not it is NULL. It makes use of the fact that an unset variable will throw an E_NOTICE error, but one initialized as NULL will not.

<?php

function var_exists($var){
    if (empty($GLOBALS['var_exists_err'])) {
        return true;
    } else {
        unset($GLOBALS['var_exists_err']);
        return false;
    }
}

function var_existsHandler($errno, $errstr, $errfile, $errline) {
   $GLOBALS['var_exists_err'] = true;
}

$l = NULL;
set_error_handler("var_existsHandler", E_NOTICE);
echo (var_exists($l)) ? "True " : "False ";
echo (var_exists($k)) ? "True " : "False ";
restore_error_handler();

?>

Outputs:
True False

The problem is, the set_error_handler and restore_error_handler calls can not be inside the function, which means you need 2 extra lines of code every time you are testing. And if you have any E_NOTICE errors caused by other code between the set_error_handler and restore_error_handler they will not be dealt with properly. One solution:

<?php

function var_exists($var){
   if (empty($GLOBALS['var_exists_err'])) {
       return true;
   } else {
       unset($GLOBALS['var_exists_err']);
       return false;
   }
}

function var_existsHandler($errno, $errstr, $errfile, $errline) {
    $filearr = file($errfile);
    if (strpos($filearr[$errline-1], 'var_exists') !== false) {
        $GLOBALS['var_exists_err'] = true;
        return true;
    } else {
        return false;
    }
}

$l = NULL;
set_error_handler("var_existsHandler", E_NOTICE);
echo (var_exists($l)) ? "True " : "False ";
echo (var_exists($k)) ? "True " : "False ";
is_null($j);
restore_error_handler();

?>

Outputs:
True False
Notice: Undefined variable: j in filename.php on line 26

This will make the handler only handle var_exists, but it adds a lot of overhead. Everytime an E_NOTICE error happens, the file it originated from will be loaded into an array.
phpnet dot 5 dot reinhold2000 at t spamgourmet dot com 10-Apr-2005 05:33
if you want to check whether the user has sent post vars from a form, it is a pain to write something like the following, since isset() does not check for zero-length strings:

if(isset($form_name) && $form_name != '') [...]

a shorter way would be this one:

if($form_name && $form_message) [...]

but this is dirty since you cannot make sure these variables exist and php will echo a warning if you refer to a non-existing variable like this. plus, a string containing "0" will evaluate to FALSE if casted to a boolean.

this function will check one or more form values if they are set and do not contain an empty string. it returns false on the first empty or non-existing post var.

<?
function postvars() {
    foreach(func_get_args() as $var) {
        if(!isset($_POST[$var]) || $_POST[$var] === '') return false;
    }
    return true;
}
?>

example: if(postvars('form_name','form_message')) [...]
yaogzhan at gmail dot com 20-Mar-2005 01:52
in PHP5, if you have

<?PHP
class Foo
{
    protected $data = array('bar' => null);

    function __get($p)
    {
        if( isset($this->data[$p]) ) return $this->data[$p];
    }
}
?>

and
<?PHP
$foo = new Foo;
echo isset($foo->bar);
?>
will always echo 'false'. because the isset() accepts VARIABLES as it parameters, but in this case, $foo->bar is NOT a VARIABLE. it is a VALUE returned from the __get() method of the class Foo. thus the isset($foo->bar) expreesion will always equal 'false'.
codeslinger at compsalot dot com 07-Feb-2005 07:21
according to the docs -- "isset() will return FALSE if testing a variable that has been set to NULL."

That statment is not always correct, sometimes isset() returns TRUE for a NULL value.  But the scenarios are obtuse.  There are a tons of bugs on this subject, all marked as bogus.

Problems occur when NULLs are in named fields of arrays and also when vars are passed by reference.

do lots of testing and code defensively.

is_null()   is your friend...
pianistsk8er at gmail dot com 10-Dec-2004 03:23
This function is very useful while calling to the URL to specify which template to be used on certain parts of your application.

Here is an example...

<?php

    $cat = $_GET['c'];
    $id = $_GET['id'];
    $error = 'templates/error.tpl';

    if( isset($cat))
    {
        if( isset($id))
        {
            $var = 'templates/pics/' . $cat . '-' . $id . '.tpl';
            if (is_file($var))
            {
                include($var);
            }
            else
            {
                include($error);
            }
        }
        else
        {
            $var = 'templates/pics/' . $cat . '.tpl';
            if (is_file($var))
            {
                include($var);
            }
            else
            {
                include($error);
            }
        }
    }
    else
    {
        include('templates/alternative.'.tpl);
    }

?>

You can see several uses of the isset function being used to specify wheter a template is to be called upon or not.  This can easily prevent other generic PHP errors.
jc dot michel at symetrie dot com 15-Nov-2004 11:35
Using
  isset($array['key'])
is useful, but be careful!
using
  isset($array['key']['subkey'])
doesn't work as one could expect, if $array['key'] is a string it seems that 'subkey' is converted to (integer) 0 and $array['key']['subkey'] is evaluated as the first char of the string.
The solution is to use
  is_array($array['key']) && isset($array['key']['subkey'])

Here is a small code to show this:

<?php
$ex = array('one' => 'val1','two' => 'val2');
echo '$ex = ';print_r($ex);
echo "<br />";

echo " isset(\$ex['one']['three']) : ";
if (isset($ex['one']['three']))
    echo 'true';
else
    echo 'false';

echo "<br />";
echo "is_array(\$ex['one']) &&  isset(\$ex['one']['three']) : ";
if (is_array($ex['one']) && isset($ex['one']['three']))
    echo 'true';
else
    echo 'false';
?>

shows:
$ex = Array ( [one] => val1 [two] => val2 )
isset($ex['one']['three']) : true
is_array($ex['one']) && isset($ex['one']['three']) : false
jon 07-Dec-2003 07:19
Since PHP will check cases in order, I often end up using this bit of code:

<?php
if (isset($var) && $var) {
    // do something
}
?>

In short, if you have error reporting on, and $var is not set, PHP will generate an error if you just have:

<?php
if ($var) { // do something }
?>

...but, as noted elsewhere, will return True if set to False in this case:
<?php
if (isset($var)) { // do something }
?>

Checking both to see if $var is set, and that it equals something other than Null or False is something I find very useful a lot of times.  If $var is not set, PHP will never execute the second part of "(isset($var) && $var)", and thus never generate an error either.

This also works very nice for setting variable as well, e.g.:
<?php
$var = (isset($var) && $var) ? $var : 'new value';
?>
flobee at gmx dot net 08-Sep-2003 11:16
just as note: if you want to check variables by boolean value: true or false , "isset" has a different meaning!
<?php
$var=null;
// sample 1
if($var) {
   // if true or another value exept "false" , "null": go on here
   echo "1. var is true or has a value $var<br>";
} else {
   echo "1. var is &quot;false&quot; or &quot;null&quot;<br>";
}

if(!$var) {
   // if false or "null": go on here
   echo "2. var has no value $var<br>";
} else {
   echo "2. var is &quot;false&quot; or &quot;null&quot;<br>";
}

// sample 2
$var =false;
if(isset($var)) {
  // $var is false so it is set to a value and the execution goes here
   echo "3. var has value: $var<br>";
}

$var=null;
if(!isset($var)) {
  // $var is null (does not exist at this time) and the execution goes here
   echo "4. var was not set $var<br>";
}
?>


변수에 관한 정보를 사람이 읽기 편하게 출력

  • ≪ isset
  • serialize ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수에 관한 정보를 사람이 읽기 편하게 출력

print_r

(PHP 4, PHP 5, PHP 7)

print_r — 변수에 관한 정보를 사람이 읽기 편하게 출력

설명

mixed print_r ( mixed $expression [, bool $return ] )

print_r()은 변수에 대한 정보를 사람이 읽을 수 있는 방법으로 표시합니다.

print_r(), var_dump(), var_export()는 PHP 5에서 객체의 protected와 private 프로퍼티도 보여줍니다. 정적 클래스 멤버는 보여주지 않습니다.

print_r()은 배열 포인터를 마지막으로 이동합니다. 처음으로 되돌리려면 reset()을 사용하십시오.

인수

expression

출력할 표현

return

print_r()의 출력을 잡으려면, return 인수를 TRUE로 사용하십시오. print_r()은 출력(기본값)하는 대신, 출력을 반환합니다.

반환값

string, integer, float가 주어지면, 값이 그대로 출력됩니다. array가 주어지면, 키와 원소를 보여주는 형식으로 값을 출력합니다. 비슷한 방식이 object에 사용됩니다.

주의

Note:

return 인수와 함께 사용할 때, 내부적으로 출력 버퍼링을 사용합니다. 그러므로, ob_start() 콜백 함수 안에서는 사용할 수 없습니다.

변경점

버전 | 설명
4.3.0 | return 인수가 추가되었습니다. 이전 버전의 PHP에서 print_r()의 출력을 잡으려면, 출력 제어 함수를 사용하십시오.
4.0.4 | PHP 4.0.4 이전에는, 주어진 array나 object가 직접적/간접적으로 자신에 대한 참조를 가지고 있을 때, print_r()은 영원히 계속되었습니다. 예로써, print_r($GLOBALS)을 들 수 있습니다. $GLOBALS가 자신에 대한 참조를 가지고 있기 때문입니다.

예제

Example #1 print_r() 예제

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

위 예제의 출력:

<pre>
Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)
</pre>

Example #2 return 인수 예제

<?php
$b = array ('m' => 'monkey', 'foo' => 'bar', 'x' => array ('x', 'y', 'z'));
$results = print_r($b, true); // $results가 print_r의 출력을 가집니다.
?>

참고

  • ob_start() - 출력 버퍼링을 켭니다
  • var_dump() - 변수에 대한 정보를 덤프
  • var_export() - 변수를 처리가능한 문자열 표현으로 출력하거나 반환합니다

User Contributed Notes

Anonymous 07-Aug-2016 11:25
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z'), 'd' => 'w');
    echo current($a);//a
    print_r ($a);
    echo current($a);//a ,not w
joseantonio at splitfy dot com 29-Sep-2015 06:01
This is an update from Matt's print_r_reverse() solution that works not only with Arrays, but also with stdClass objects.

I expect someone finds it useful.

<?php
function print_r_reverse($in) {
    $lines = explode("\n", trim($in));
    if ( (trim($lines[0]) != 'Array') && (trim($lines[0]) != 'stdClass Object') ){
        // bottomed out to something that isn't an array or object
        return $in;
    } else {
        // this is an array or stdClass object, lets parse it
        if (preg_match("/(\s{4,})\(/", $lines[1], $match)) {
            // this is a tested array/recursive call to this function
            // take a set of spaces off the beginning
            $spaces = $match[1];
            $spaces_length = strlen($spaces);
            $lines_total = count($lines);
            for ($i = 0; $i < $lines_total; $i++) {
                if (substr($lines[$i], 0, $spaces_length) == $spaces) {
                    $lines[$i] = substr($lines[$i], $spaces_length);
                }
            }
        }
        $class = trim( $lines[0] );
        array_shift($lines); // Array or stdClass Object
        array_shift($lines); // (
        array_pop($lines); // )
        $in = implode("\n", $lines);

        // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
        preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
        $pos = array();
        $previous_key = '';
        $in_length = strlen($in);
        // store the following in $pos:
        // array with key = key of the parsed array's item
        // value = array(start position in $in, $end position in $in)
        foreach ($matches as $match) {
            $key = $match[1][0];
            $start = $match[0][1] + strlen($match[0][0]);
            $pos[$key] = array($start, $in_length);
            if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
            $previous_key = $key;
        }
        // behave differently depending on the type
        switch( $class ){
            case 'Array':
                $ret = array();
                foreach ($pos as $key => $where) {
                    // recursively see if the parsed out value is an array/object too
                    $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
                }
                break;
            case 'stdClass Object':
                $ret = new stdClass();
                foreach ($pos as $key => $where) {
                    // recursively see if the parsed out value is an array/object too
                    $ret->$key = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
                }
                break;
            default:
                throw new Exception( 'Type not allowed' );
        }

        return $ret;
    }
}
?>
nuntius 02-Aug-2015 07:45
This is a happy medium between the overly complex usages and the simplistic. It prints out the debug in a black and green monochrome look.

function formatDebug($value, $log=false) {

        $contents = print_r($value, true);
        $line="\n"
        ."<pre style=\"margin:18px 4px; padding:6px; text-align:left; background:#000; color:#00FF00; font-family:courier;\">";
        .htmlspecialchars($contents,ENT_QUOTES) // Escape HTML special chars
        ."</pre>";

        if($log==true){
            error_log($contents);//Send to log
        }
        return $line;
    }
ntd at entidi dot it 01-Aug-2015 02:43
Another attempt that tries to overcome the memory blowout when the passed in data has mutual recursion.

<?php
function safe_print_r($data, $nesting = 5, $indent = '') {
    if (! is_object($data) && ! is_array($data)) {
        var_dump($data);
    } elseif ($nesting < 0) {
        echo "** MORE **\n";
    } else {
        echo ucfirst(gettype($data)) . " (\n";
        foreach ((array) $data as $k => $v) {
            echo $indent . "\t[$k] => ";
            safe_print_r($v, $nesting - 1, "$indent\t");
        }
        echo "$indent)\n";
    }
}
?>
Sumit 27-May-2015 10:20
I prefer to use
<?php
printf("<pre>%s</pre>",print_r($_POST, true));
?>
Simon Asika 07-Nov-2013 01:06
Here is a print_r() clone but support max level limit.

When we want to print a big object, this will help us get a clean dumping data.

<?php

/**
 * Recrusive print variables and limit by level.
 *
 * @param   mixed  $data   The variable you want to dump.
 * @param   int    $level  The level number to limit recrusive loop.
 *
 * @return  string  Dumped data.
 *
 * @author  Simon Asika (asika32764[at]gmail com)
 * @date    2013-11-06
 */
function print_r_level($data, $level = 5)
{
    static $innerLevel = 1;

    static $tabLevel = 1;

    static $cache = array();

    $self = __FUNCTION__;

    $type       = gettype($data);
    $tabs       = str_repeat('    ', $tabLevel);
    $quoteTabes = str_repeat('    ', $tabLevel - 1);

    $recrusiveType = array('object', 'array');

    // Recrusive
    if (in_array($type, $recrusiveType))
    {
        // If type is object, try to get properties by Reflection.
        if ($type == 'object')
        {
            if (in_array($data, $cache))
            {
                return "\n{$quoteTabes}*RECURSION*\n";
            }

            // Cache the data
            $cache[] = $data;

            $output     = get_class($data) . ' ' . ucfirst($type);
            $ref        = new \ReflectionObject($data);
            $properties = $ref->getProperties();

            $elements = array();

            foreach ($properties as $property)
            {
                $property->setAccessible(true);

                $pType = $property->getName();

                if ($property->isProtected())
                {
                    $pType .= ":protected";
                }
                elseif ($property->isPrivate())
                {
                    $pType .= ":" . $property->class . ":private";
                }

                if ($property->isStatic())
                {
                    $pType .= ":static";
                }

                $elements[$pType] = $property->getValue($data);
            }
        }
        // If type is array, just retun it's value.
        elseif ($type == 'array')
        {
            $output = ucfirst($type);
            $elements = $data;
        }

        // Start dumping datas
        if ($level == 0 || $innerLevel < $level)
        {
            // Start recrusive print
            $output .= "\n{$quoteTabes}(";

            foreach ($elements as $key => $element)
            {
                $output .= "\n{$tabs}[{$key}] => ";

                // Increment level
                $tabLevel = $tabLevel + 2;
                $innerLevel++;

                $output  .= in_array(gettype($element), $recrusiveType) ? $self($element, $level) : $element;

                // Decrement level
                $tabLevel = $tabLevel - 2;
                $innerLevel--;
            }

            $output .= "\n{$quoteTabes})\n";
        }
        else
        {
            $output .= "\n{$quoteTabes}*MAX LEVEL*\n";
        }
    }

    // Clean cache
    if($innerLevel == 1)
    {
        $cache = array();
    }

    return $output;
}// End function

// TEST ------------------------------------

class testClass
{
    protected $a = 'aaa';

    private $b = 'bbb';

    public $c = array(1, 2, ['a', 'b', 'c'], 4);

    static public $d = 'ddd';

    static protected $e = 'eee';
}

$test = new testClass;

$test->testClass = $test;

echo '<pre>' . print_r_level($test, 3) . '</pre>';

?>

will output
-------------------------------------------------------------

testClass Object
(
    [a:protected] => aaa
    [b:testClass:private] => bbb
    [c] => Array
        (
            [0] => 1
            [1] => 2
            [2] => Array
                *MAX LEVEL*

            [3] => 4
        )

    [d:static] => ddd
    [e:protected:static] => eee
    [testClass] =>
        *RECURSION*

)
alex uses khimich.org 21-Oct-2013 06:43
Sometimes, when page is called by ajax and you're not actually feeling convenient to see output of debugging, you may log it in file like shown below:

<?php

// Log superglobals
$g=print_r($_GET,true);
$p=print_r($_POST,true);
$f=print_r($_FILES,true);
$s=print_r($_SESSION,true);

$txt='';
$txt.="/n".'GET: '.$g;
$txt.="/n".'POST: '.$p;
$txt.="/n".'FILES: '.$f;
$txt.="/n".'SESSION: '.$s;

file_put_contents('requests.txt',$txt);

?>
bginfos at web dot de 06-Oct-2013 08:40
function : debug_print

<?php
// --------------------------------------------------------------

function debug_print($var, $nfo='DEBUG', $htm=false, $ret=false) {

    $var_str = print_r($var, true);

    if ($htm !== false) { $var_str = htmlentities($var_str); }

    $outstr = '';

    $outstr = '<p>--------- <strong>'.$nfo.'</strong> ---------</p>'."\n";

    $outstr .= '<pre style="margin:18px 4px; padding:6px; text-align:left; background:#DEDEDE; color:#000099;">'."\n";
    $outstr .= $var_str."\n";
    $outstr .= '</pre>'."\n";

    if ($ret !== false) { $result = $outstr; }
        else { print $outstr; $result = true; }

    return $result;
}

// --------------------------------------------------------------
?>

usage:

<?php
// --------------------------------------------

$data = array('some', 'example', 'test');

// --- direct output ---
debug_print($data);

// --- returned output ---
$debug_str = debug_print($data, 'some text', false, true);
print $debug_str;

// --------------------------------------------
?>
preda dot vlad at yahoo dot com 22-Mar-2013 06:40
print_r(), just like var_dump() does NOT cast an object, not even if it has a __toString() method - which is normal.

<?php
class A {
    public function __toString() {
        return 'In class A';
    }
}
$a = new A;
echo $a; // In class A
print_r($a); // A Object()
// you can simulate the echo by casting it manually
print_r((string)$a);  // In class A
ReanimationXP at g m a i l dot com 22-Jul-2012 09:49
If you're like me and just want an exact version of print_r() except formatted to HTML without a bunch of bs, here you go.

The output is identical to print_r() but renders in HTML.

<?php
// Displays a multi-dimensional array as a HTML unordered lists.
function displayTree($array) {
     $newline = "<br>";
     foreach($array as $key => $value) {    //cycle through each item in the array as key => value pairs
         if (is_array($value) || is_object($value)) {        //if the VALUE is an array, then
            //call it out as such, surround with brackets, and recursively call displayTree.
             $value = "Array()" . $newline . "(<ul>" . displayTree($value) . "</ul>)" . $newline;
         }
        //if value isn't an array, it must be a string. output its' key and value.
        $output .= "[$key] => " . $value . $newline;
     }
     return $output;
}
?>
henzeberkheij at gmail dot com 29-Jan-2012 12:10
print_r is used for debug purposes. Yet I had some classes where I just wanted the values coming out of the database, not all the other crap. thus i wrote the following function. If your class has an toArray function, that one will be called otherwise it will return the object as is. print_neat_classes_r is the function that should be called!

<?php
print_neat_classes_r($array, $return=false){
        return print_r(self::neat_class_print_r($array), $return);
    }

function do_print_r($array, $return=false){
        if(is_object($array) && method_exists($array, 'toArray')){
            return $array->toArray();
        }else if(is_array($array)){
            foreach($array as $key=>$obj){
                $array[$key] = self::do_print_r($obj, $return);
            }
            return $array;
        }else{
            return $array;
        }
    }
?>
eithed 21-Oct-2011 11:25
Bear in mind that print_r actually reserves some memory to do something - so, for example thing like:

<?php
function shutdown_handler()
{
    file_put_contents($_SERVER['DOCUMENT_ROOT']."/log.txt", print_r($_REQUEST, true));
}

ini_set('memory_limit', '1M');
register_shutdown_function("shutdown_handler");

$array = array();
while(true)
    $array[] = new stdClass();
?>

will just not work. Try using var_export, and presto, everything is fine.
jwood at JPL dot gov (w/ Bug fixed) 11-Aug-2011 01:04
<?php

// Displays a multi-dimensional array as a HTML List (Tree structure).
function displayTree($var) {
     $newline = "\n";
     foreach($var as $key => $value) {
         if (is_array($value) || is_object($value)) {
             $value = $newline . "<ul>" . displayTree($value) . "</ul>";
         }

         if (is_array($var)) {
             if (!stripos($value, "<li class=")) {
                $output .= "<li class=\"file\">" . $value . "</li>" . $newline;
             }
             else {
                $output .= $value . $newline;
             }

         }
         else { // is_object
            if (!stripos($value, "<li class=")) {
               $value = "<ul><li class=\"file\">" . $value . "</li></ul>" . $newline;
            }

            $output .= "<li class=\"folder\">" . $key . $value . "</li>" . $newline;
         }

     }

     return $output;
}
?>
///////////////////

Also need to add this to your style sheet (and get the necessary images...):

li.folder
 {
 list-style-image: url(../img/folder.gif);
 }

li.file
 {
 list-style-image: url(../img/file.gif);
 }

///////////////////

Example Usage:

<?php
     ...

    echo "<ul>";
     echo displayTree($array);
     echo "</ul>";
 ?>
Robert Heine (joomla-jquery-internet.de) 18-Apr-2011 04:09
Since i use print_r for debugging a lot, i always use the following function in my scripts, which makes life a lot easier :-)

<?php
if (!function_exists("preprint")) {
    function preprint($s, $return=false) {
        $x = "<pre>";
        $x .= print_r($s, 1);
        $x .= "</pre>";
        if ($return) return $x;
        else print $x;
    }
}
?>

-------------------

USAGE:  preprint($x);   <- where $x = array().

-------------------

The optional 2. parameter can be set to "true", so the html-code is returned.

Have fun,
machuidel 03-Feb-2011 01:01
The following will output an array in a PHP parsable format:

<?php
function serialize_array(&$array, $root = '$root', $depth = 0)
{
        $items = array();

        foreach($array as $key => &$value)
        {
                if(is_array($value))
                {
                        serialize_array($value, $root . '[\'' . $key . '\']', $depth + 1);
                }
                else
                {
                        $items[$key] = $value;
                }
        }

        if(count($items) > 0)
        {
                echo $root . ' = array(';

                $prefix = '';
                foreach($items as $key => &$value)
                {
                        echo $prefix . '\'' . $key . '\' => \'' . addslashes($value) . '\'';
                        $prefix = ', ';
                }

                echo ');' . "\n";
        }
}
?>
Alexander 19-Mar-2010 02:20
A simple function to send the output of print_r to firebug.
The script creates a dummy console object with a log method for when firebug is disabled/not available.

<?php
function debug ($data) {
    echo "<script>\r\n//<![CDATA[\r\nif(!console){var console={log:function(){}}}";
    $output    =    explode("\n", print_r($data, true));
    foreach ($output as $line) {
        if (trim($line)) {
            $line    =    addslashes($line);
            echo "console.log(\"{$line}\");";
        }
    }
    echo "\r\n//]]>\r\n</script>";
}
?>
liamtoh6 at hotmail dot com 03-Feb-2010 07:01
I add this function to the global scope on just about every project I do, it makes reading the output of print_r() in a browser infinitely easier.

<?php
function print_r2($val){
        echo '<pre>';
        print_r($val);
        echo  '</pre>';
}
?>

It also makes sense in some cases to add an if statement to only display the output in certain scenarios, such as:

if(debug==true)
if($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
jeremy dot ruten at gmail dot com 27-Sep-2009 04:16
Here's an array_depth() function that calculates the depth of an array using the indentation in the output of print_r():

<?php

function array_depth($array) {
    $max_indentation = 1;

    $array_str = print_r($array, true);
    $lines = explode("\n", $array_str);

    foreach ($lines as $line) {
        $indentation = (strlen($line) - strlen(ltrim($line))) / 4;

        if ($indentation > $max_indentation) {
                $max_indentation = $indentation;
        }
    }

    return ceil(($max_indentation - 1) / 2) + 1;
}

?>

It's better than writing a recursive function to do the same thing, as print_r() handles the problem of infinitely-deep arrays for you quite nicely.
Matt 14-Sep-2009 09:53
Here is another version that parses the print_r() output. I tried the one posted, but I had difficulties with it. I believe it has a problem with nested arrays. This handles nested arrays without issue as far as I can tell.

<?php
function print_r_reverse($in) {
    $lines = explode("\n", trim($in));
    if (trim($lines[0]) != 'Array') {
        // bottomed out to something that isn't an array
        return $in;
    } else {
        // this is an array, lets parse it
        if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
            // this is a tested array/recursive call to this function
            // take a set of spaces off the beginning
            $spaces = $match[1];
            $spaces_length = strlen($spaces);
            $lines_total = count($lines);
            for ($i = 0; $i < $lines_total; $i++) {
                if (substr($lines[$i], 0, $spaces_length) == $spaces) {
                    $lines[$i] = substr($lines[$i], $spaces_length);
                }
            }
        }
        array_shift($lines); // Array
        array_shift($lines); // (
        array_pop($lines); // )
        $in = implode("\n", $lines);
        // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
        preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
        $pos = array();
        $previous_key = '';
        $in_length = strlen($in);
        // store the following in $pos:
        // array with key = key of the parsed array's item
        // value = array(start position in $in, $end position in $in)
        foreach ($matches as $match) {
            $key = $match[1][0];
            $start = $match[0][1] + strlen($match[0][0]);
            $pos[$key] = array($start, $in_length);
            if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
            $previous_key = $key;
        }
        $ret = array();
        foreach ($pos as $key => $where) {
            // recursively see if the parsed out value is an array too
            $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
        }
        return $ret;
    }
}

?>
Federico Bricker 30-Jun-2009 01:02
Here is a nice version of print_r that prints things with colors and tables.
I've called it print_nice

<?php
function print_nice($elem,$max_level=10,$print_nice_stack=array()){
    if(is_array($elem) || is_object($elem)){
        if(in_array(&$elem,$print_nice_stack,true)){
            echo "<font color=red>RECURSION</font>";
            return;
        }
        $print_nice_stack[]=&$elem;
        if($max_level<1){
            echo "<font color=red>nivel maximo alcanzado</font>";
            return;
        }
        $max_level--;
        echo "<table border=1 cellspacing=0 cellpadding=3 width=100%>";
        if(is_array($elem)){
            echo '<tr><td colspan=2 style="background-color:#333333;"><strong><font color=white>ARRAY</font></strong></td></tr>';
        }else{
            echo '<tr><td colspan=2 style="background-color:#333333;"><strong>';
            echo '<font color=white>OBJECT Type: '.get_class($elem).'</font></strong></td></tr>';
        }
        $color=0;
        foreach($elem as $k => $v){
            if($max_level%2){
                $rgb=($color++%2)?"#888888":"#BBBBBB";
            }else{
                $rgb=($color++%2)?"#8888BB":"#BBBBFF";
            }
            echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">';
            echo '<strong>'.$k."</strong></td><td>";
            print_nice($v,$max_level,$print_nice_stack);
            echo "</td></tr>";
        }
        echo "</table>";
        return;
    }
    if($elem === null){
        echo "<font color=green>NULL</font>";
    }elseif($elem === 0){
        echo "0";
    }elseif($elem === true){
        echo "<font color=green>TRUE</font>";
    }elseif($elem === false){
        echo "<font color=green>FALSE</font>";
    }elseif($elem === ""){
        echo "<font color=green>EMPTY STRING</font>";
    }else{
        echo str_replace("\n","<strong><font color=red>*</font></strong><br>\n",$elem);
    }
}
?>
Bob 08-May-2009 12:19
Here is a function that formats the output of print_r as a expandable/collapsable tree list using HTML and JavaScript.
<?php
function print_r_tree($data)
{
    // capture the output of print_r
    $out = print_r($data, true);

    // replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;">
    $out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iUe',"'\\1<a href=\"javascript:toggleDisplay(\''.(\$id = substr(md5(rand().'\\0'), 0, 7)).'\');\">\\2</a><div id=\"'.\$id.'\" style=\"display: none;\">'", $out);

    // replace ')' on its own on a new line (surrounded by whitespace is ok) with '</div>
    $out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);

    // print the javascript function toggleDisplay() and then the transformed output
    echo '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n$out";
}
?>
Pass it a multidimensional array or object and each sub-array/object will be hidden and replaced by a html link that will toggle its display on and off.
Its quick and dirty, but great for debugging the contents of large arrays and objects.
Note: You'll want to surround the output with <pre></pre>
sebasg37 at gmail dot com 26-Jan-2009 06:35
If you have to catch the output without showing it at all at first (for example, if you want to append the print_r output to a file), you can do this:

<?php

ob_start();
print_r( $some_array );
$output = ob_get_clean();

// if you want to append the print_r output of $some_array to, let's say, log.txt:

file_put_contents( 'log.txt', file_get_contents( 'log.txt' ) . $output )

?>
afisher8 at cox dot net 17-Dec-2008 05:53
I use this all the time when debugging objects, but when you have a very large object with big arrays of sub-objects, it's easy to get overwhelmed with the large amount of output....sometimes you don't want to see absolutely every sub-object.

I made this function to debug objects while "hiding" sub-objects of certain types.  This also color codes the output for a more readable printout.

<?php

function wtf($var, $arrayOfObjectsToHide=null, $fontSize=11)
{
    $text = print_r($var, true);

    if (is_array($arrayOfObjectsToHide)) {

        foreach ($arrayOfObjectsToHide as $objectName) {

            $searchPattern = '#('.$objectName.' Object\n(\s+)\().*?\n\2\)\n#s';
            $replace = "$1<span style=\"color: #FF9900;\">--&gt; HIDDEN - courtesy of wtf() &lt;--</span>)";
            $text = preg_replace($searchPattern, $replace, $text);
        }
    }

    // color code objects
    $text = preg_replace('#(\w+)(\s+Object\s+\()#s', '<span style="color: #079700;">$1</span>$2', $text);
    // color code object properties
    $text = preg_replace('#\[(\w+)\:(public|private|protected)\]#', '[<span style="color: #000099;">$1</span>:<span style="color: #009999;">$2</span>]', $text);

    echo '<pre style="font-size: '.$fontSize.'px; line-height: '.$fontSize.'px;">'.$text.'</pre>';
}

// example usage:
wtf($myBigObject, array('NameOfObjectToHide_1', 'NameOfObjectToHide_2'));

?>
vista_ at live dot se 04-Dec-2008 05:31
Do you think it is difficult to see what print_r() returns?

<?php
   echo '<pre>';
   print_r($array);
   echo '</pre>';
?>

And if you have to write this many times in a page, use this:

<?php
   function preint_r($array)
   {
      echo '<pre>';
      print_r($array);
      echo '</pre>';
   }
?>
kurt krueckeberg 28-Oct-2008 07:53
This is an alternative for printing arrays. It bolds array values.

<?php
function print_array(&$a, $str = "")
{
    if ($str[0]) {
        echo "$str =";
    }
    echo ' array( ';
    foreach ($a as $k => $v) {

        echo "[$k]".' => ';

        if (is_array($v)) {
            print_array($v);
        }
        else {
               echo "<strong>$a[$k]</strong> ";
        }
    }
    echo ')  ';
}
$a1 = array ("apples", "oranges", "pears");
$a2 = array ("potatos", "green beans", "squash");
$a3 = array ("euros", "dollars", "pesos", "dinars");
$test = range(1, 9);
array_push($test, array ("fruit" => $a1, "vegies" => $a2, "money" => $a3));
print_array($test, "test");
?>
schizo do not spamme duckie at gmail dot com 07-Oct-2008 02:00
my take on the highlighted markupped debug function:

<?php
/**
 * print_array()
 * Does a var_export of the array and returns it between <pre> tags
 *
 * @param mixed $var any input you can think of
 * @return string HTML
 */
function print_array($var)
{
    $input =var_export($var,true);
    $input = preg_replace("! => \n\W+ array \(!Uims", " => Array ( ", $input);
    $input = preg_replace("!array \(\W+\),!Uims", "Array ( ),", $input);
    return("<pre>".str_replace('><?', '>', highlight_string('<'.'?'.$input, true))."</pre>");
}
?>
brenden at beagleproductions dot com 28-Aug-2008 10:58
Many developers have submitted bugs to the PHP development team with regards to print_r showing protected and private properties of objects (PHP 5).  This is not a bug; sensitive information (ex. database connection object) should be  encapsulated within a private member function of your class.
admin at swivelgames dot com 17-Aug-2008 10:44
I was having problems using print_r because I didn't like the fact that if tags where included in the array it would still be parsed by the browsers.

Heres a simple fix for anyone who is having the same problem as I did. This will output your text properly for viewing through the browser instead of the browser's "view source" or CLI.

Script:
<?php

     $MyArray[0]="<div align='center'>My Text</div>";

     echo "<pre>".htmlspecialchars(print_r($MyArray,true))."</pre>";

?>

Output:
<pre>Array
(
    [0] =&gt; &lt;div align='center'&gt;My Text&lt;/div&gt;
)
</pre>
Anonymous 28-Jun-2008 10:21
I have written a nice debugging function.
This function handles arrays beautifully.
<?php
//Debug variables, $i and $k are for recursive use
function DebugDisplayVar($input, $name = "Debug", $i = "0", $k = array("Error")){
    if(is_array($input))
    {    foreach ($input as $i => $value){
            $temp = $k;
            $temp[] = $i;
            DebugDisplayVar($value, $name, $i, $temp);}
    }else{//if not array
        echo "$".$name;//[$k]
        foreach ($k as $i => $value){
            if($value !=="Error"){echo "[$value]";}
        }
        echo " = $input<br>";
}    }

//outputs
Debug[0] = value
Debug[1] = another value
ect...

?>
janci 13-May-2008 11:49
You cannot use print_r(), var_dump() nor var_export() to get static member variables of a class. However, in PHP5 you can use Reflection classes for this:

<?php

$reflection = new ReflectionClass('Static');
print_r($reflection->getStaticProperties());

?>
tylmann at corvent dot ch 28-Mar-2008 11:07
There is a library to create nice output of variables, arrays, hash-tables and even objects. It is great for developing/debugging and looks very much better than any print_r output.

Usage:
<?php
debug::show($myVar, 'caption');
?>

You can download it for free at http://sourceforge.net/projects/phpcorestdfuncs

For an example take a look at http://demo.corvent.ch/stdfuncs/
helpful at maybe dot com 07-Mar-2008 05:13
Another slight modification to the previous post to allow for empty array elements

added the following lines after the first preg_match block
<?php
        } else if ($expecting == 2 && preg_match('/^\[(.+?)\] \=\>$/', $trim, $matches)) { // array element
            // the array element is blank
            list ($fullMatch, $key) = $matches;
            $topArray[$key] = $element;
        }

?>
anon at anon dot com 29-Feb-2008 07:30
A slight modification to the previous post to allow for arrays containing mutli line strings. haven't fully tested it with everything, but seems to work great for the stuff i've done so far.

<?php

function print_r_reverse(&$output)
{
    $expecting = 0; // 0=nothing in particular, 1=array open paren '(', 2=array element or close paren ')'
    $lines = explode("\n", $output);
    $result = null;
    $topArray = null;
    $arrayStack = array();
    $matches = null;
    while (!empty($lines) && $result === null)
    {
        $line = array_shift($lines);
        $trim = trim($line);
        if ($trim == 'Array')
        {
            if ($expecting == 0)
            {
                $topArray = array();
                $expecting = 1;
            }
            else
            {
                trigger_error("Unknown array.");
            }
        }
        else if ($expecting == 1 && $trim == '(')
        {
            $expecting = 2;
        }
        else if ($expecting == 2 && preg_match('/^\[(.+?)\] \=\> (.+)$/', $trim, $matches)) // array element
        {
            list ($fullMatch, $key, $element) = $matches;
            if (trim($element) == 'Array')
            {
                $topArray[$key] = array();
                $newTopArray =& $topArray[$key];
                $arrayStack[] =& $topArray;
                $topArray =& $newTopArray;
                $expecting = 1;
            }
            else
            {
                $topArray[$key] = $element;
            }
        }
        else if ($expecting == 2 && $trim == ')') // end current array
        {
            if (empty($arrayStack))
            {
                $result = $topArray;
            }
            else // pop into parent array
            {
                // safe array pop
                $keys = array_keys($arrayStack);
                $lastKey = array_pop($keys);
                $temp =& $arrayStack[$lastKey];
                unset($arrayStack[$lastKey]);
                $topArray =& $temp;
            }
        }
        // Added this to allow for multi line strings.
    else if (!empty($trim) && $expecting == 2)
    {
        // Expecting close parent or element, but got just a string
        $topArray[$key] .= "\n".$line;
    }
        else if (!empty($trim))
        {
            $result = $line;
        }
    }

    $output = implode(n, $lines);
    return $result;
}

/**
 * @param string $output : The output of a multiple print_r calls, separated by newlines
 * @return mixed[] : parseable elements of $output
 */
function print_r_reverse_multiple($output)
{
    $result = array();
    while (($reverse = print_r_reverse($output)) !== NULL)
    {
        $result[] = $reverse;
    }
    return $result;
}

$output = '
Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
            [3] => Array
            (
                [nest] => yes
                [nest2] => Array
                (
                    [nest] => some more
                    asffjaskkd
                )
                [nest3] => o rly?
            )
        )
)

some extra stuff
';
var_dump(print_r_reverse($output), $output);

?>

This should output

array(3) {
  ["a"]=>
  string(5) "apple"
  ["b"]=>
  string(6) "banana"
  ["c"]=>
  array(4) {
    [0]=>
    string(1) "x"
    [1]=>
    string(1) "y"
    [2]=>
    string(1) "z"
    [3]=>
    array(3) {
      ["nest"]=>
      string(3) "yes"
      ["nest2"]=>
      array(1) {
        ["nest"]=>
        string(40) "some more
                    asffjaskkd"
      }
      ["nest3"]=>
      string(6) "o rly?"
    }
  }
}
string(18) "nsome extra stuffn"

Added:
else if (!empty($trim) && $expecting == 2)
{
    // Expecting close parent or element, but got just a string
    $topArray[$key] .= "\n".$line;
}
ario at mail dot utexas dot [education - 3 letters] 19-Feb-2008 09:34
This is a better print_r reverse algorithm, that works with arbitrary nested arrays.  Anything else it treats as strings.  The second function allows you to take a string with multiple print_r results concatenated, and returns the result of parsing each of them.

<?php

/**
 * @param string &$output : The output of a print_r call; this parameter is DESTRUCTIVE, and will be set to the remainder
 *                             of $output which is not parsed.
 * @return mixed : the first parseable element of $output
 */
function print_r_reverse(&$output)
{
    $expecting = 0; // 0=nothing in particular, 1=array open paren '(', 2=array element or close paren ')'
    $lines = explode("\n", $output);
    $result = null;
    $topArray = null;
    $arrayStack = array();
    $matches = null;
    while (!empty($lines) && $result === null)
    {
        $line = array_shift($lines);
        $trim = trim($line);
        if ($trim == 'Array')
        {
            if ($expecting == 0)
            {
                $topArray = array();
                $expecting = 1;
            }
            else
            {
                trigger_error("Unknown array.");
            }
        }
        else if ($expecting == 1 && $trim == '(')
        {
            $expecting = 2;
        }
        else if ($expecting == 2 && preg_match('/^\[(.+?)\] \=\> (.+)$/', $trim, $matches)) // array element
        {
            list ($fullMatch, $key, $element) = $matches;
            if (trim($element) == 'Array')
            {
                $topArray[$key] = array();
                $newTopArray =& $topArray[$key];
                $arrayStack[] =& $topArray;
                $topArray =& $newTopArray;
                $expecting = 1;
            }
            else
            {
                $topArray[$key] = $element;
            }
        }
        else if ($expecting == 2 && $trim == ')') // end current array
        {
            if (empty($arrayStack))
            {
                $result = $topArray;
            }
            else // pop into parent array
            {
                // safe array pop
                $keys = array_keys($arrayStack);
                $lastKey = array_pop($keys);
                $temp =& $arrayStack[$lastKey];
                unset($arrayStack[$lastKey]);
                $topArray =& $temp;
            }
        }
        else if (!empty($trim))
        {
            $result = $line;
        }
    }

    $output = implode(n, $lines);
    return $result;
}

/**
 * @param string $output : The output of a multiple print_r calls, separated by newlines
 * @return mixed[] : parseable elements of $output
 */
function print_r_reverse_multiple($output)
{
    $result = array();
    while (($reverse = print_r_reverse($output)) !== NULL)
    {
        $result[] = $reverse;
    }
    return $result;
}

$output = '
Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
            [3] => Array
            (
                [nest] => yes
                [nest2] => Array
                (
                    [nest] => some more
                )
                [nest3] => o rly?
            )
        )
)

some extra stuff
';
var_dump(print_r_reverse($output), $output);

?>

The above example will output:

array(3) {
  ["a"]=>
  string(5) "apple"
  ["b"]=>
  string(6) "banana"
  ["c"]=>
  array(4) {
    [0]=>
    string(1) "x"
    [1]=>
    string(1) "y"
    [2]=>
    string(1) "z"
    [3]=>
    array(3) {
      ["nest"]=>
      string(3) "yes"
      ["nest2"]=>
      array(1) {
        ["nest"]=>
        string(9) "some more"
      }
      ["nest3"]=>
      string(6) "o rly?"
    }
  }
}
string(20) "
some extra stuff
"
Kokos 31-Jan-2008 01:52
A simple function that will output an array more easily to read than print_r();

<?php
function echo_array($array,$return_me=false){
    if(is_array($array) == false){
        $return = "The provided variable is not an array.";
    }else{
        foreach($array as $name=>$value){
            if(is_array($value)){
                $return .= "";
                $return .= "['<b>$name</b>'] {<div style='margin-left:10px;'>\n";
                $return .= echo_array($value,true);
                $return .= "</div>}";
                $return .= "\n\n";
            }else{
                if(is_string($value)){
                    $value = "\"$value\"";
                }
                $return .= "['<b>$name</b>'] = $value\n\n";
            }
        }
    }
    if($return_me == true){
        return $return;
    }else{
        echo "<pre>".$return."</pre>";
    }
}
?>
bart at mediawave dot nl 31-Oct-2007 01:27
Here's a PHP version of print_r which can be tailored to your needs. Shows protected and private properties of objects and detects recursion (for objects only!). Usage:

void u_print_r ( mixed $expression [, array $ignore] )

Use the $ignore parameter to provide an array of property names that shouldn't be followed recursively.

<?php

function u_print_r($subject, $ignore = array(), $depth = 1, $refChain = array())
{
    if ($depth > 20) return;
    if (is_object($subject)) {
        foreach ($refChain as $refVal)
            if ($refVal === $subject) {
                echo "*RECURSION*\n";
                return;
            }
        array_push($refChain, $subject);
        echo get_class($subject) . " Object ( \n";
        $subject = (array) $subject;
        foreach ($subject as $key => $val)
            if (is_array($ignore) && !in_array($key, $ignore, 1)) {
                echo str_repeat(" ", $depth * 4) . '[';
                if ($key{0} == "\0") {
                    $keyParts = explode("\0", $key);
                    echo $keyParts[2] . (($keyParts[1] == '*')  ? ':protected' : ':private');
                } else
                    echo $key;
                echo '] => ';
                u_print_r($val, $ignore, $depth + 1, $refChain);
            }
        echo str_repeat(" ", ($depth - 1) * 4) . ")\n";
        array_pop($refChain);
    } elseif (is_array($subject)) {
        echo "Array ( \n";
        foreach ($subject as $key => $val)
            if (is_array($ignore) && !in_array($key, $ignore, 1)) {
                echo str_repeat(" ", $depth * 4) . '[' . $key . '] => ';
                u_print_r($val, $ignore, $depth + 1, $refChain);
            }
        echo str_repeat(" ", ($depth - 1) * 4) . ")\n";
    } else
        echo $subject . "\n";
}

?>

Example:

<?php

class test {

    public $var1 = 'a';
    protected $var2 = 'b';
    private $var3 = 'c';
    protected $array = array('x', 'y', 'z');

}

$test = new test();
$test->recursiveRef = $test;
$test->anotherRecursiveRef->recursiveRef = $test;
$test->dont->follow = 'me';

u_print_r($test, array('dont'));

?>

Will produce:

test Object (
    [var1] => a
    [var2:protected] => b
    [var3:private] => c
    [array:protected] => Array (
        [0] => x
        [1] => y
        [2] => z
    )
    [recursiveRef] => *RECURSION*
    [anotherRecursiveRef] => stdClass Object (
        [recursiveRef] => *RECURSION*
    )
)
php at deboom dot biz 21-Sep-2007 02:17
If you need to import an print_R output back to an array you could use this.

This could also be (ab)used to convert a object into a array...

<?php
function object2array($printr) {
        $newarray = array();
        $a[0] = &$newarray;
        if (preg_match_all('/^\s+\[(\w+).*\] => (.*)\n/m', $printr, $match)) {
            foreach ($match[0] as $key => $value) {
                (int)$tabs = substr_count(substr($value, 0, strpos($value, "[")), "        ");
                if ($match[2][$key] == 'Array' || substr($match[2][$key], -6) == 'Object') {
                    $a[$tabs+1] = &$a[$tabs][$match[1][$key]];
                }
                else {
                    $a[$tabs][$match[1][$key]] = $match[2][$key];
                }
            }
        }
        return $newarray;
    }
?>
motin at demomusic dot nu 20-Jun-2007 01:01
This works around the hacky nature of print_r in return mode (using output buffering for the return mode to work is hacky...):

<?php
/**
  * An alternative to print_r that unlike the original does not use output buffering with
  * the return parameter set to true. Thus, Fatal errors that would be the result of print_r
  * in return-mode within ob handlers can be avoided.
  *
  * Comes with an extra parameter to be able to generate html code. If you need a
  * human readable DHTML-based print_r alternative, see http://krumo.sourceforge.net/
  *
  * Support for printing of objects as well as the $return parameter functionality
  * added by Fredrik Wollsen (fredrik dot motin at gmail), to make it work as a drop-in
  * replacement for print_r (Except for that this function does not output
  * paranthesises around element groups... ;) )
  *
  * Based on return_array() By Matthew Ruivo (mruivo at gmail)
  * (http://se2.php.net/manual/en/function.print-r.php#73436)
  */
function obsafe_print_r($var, $return = false, $html = false, $level = 0) {
    $spaces = "";
    $space = $html ? "&nbsp;" : " ";
    $newline = $html ? "<br />" : "\n";
    for ($i = 1; $i <= 6; $i++) {
        $spaces .= $space;
    }
    $tabs = $spaces;
    for ($i = 1; $i <= $level; $i++) {
        $tabs .= $spaces;
    }
    if (is_array($var)) {
        $title = "Array";
    } elseif (is_object($var)) {
        $title = get_class($var)." Object";
    }
    $output = $title . $newline . $newline;
    foreach($var as $key => $value) {
        if (is_array($value) || is_object($value)) {
            $level++;
            $value = obsafe_print_r($value, true, $html, $level);
            $level--;
        }
        $output .= $tabs . "[" . $key . "] => " . $value . $newline;
    }
    if ($return) return $output;
      else echo $output;
}
?>

Built on a function earlier posted in these comments as stated in the Doc comment. Cheers! /Fredrik (Motin)
mrasnika at users dot sourceforge dot net 26-Apr-2007 08:30
We can all agree that print_r() output is very spartan looking. The debug data needs to be organized better, and presented in a graceful way. In the era of Web 2.0 it is somewhat strange to use plain text to dump information. A DHTML powered informatiion dumping tool will be quite better - like the the open-source alternative of print_r(); -- Krumo (http://krumo.sourceforge.net).

It renders the output using DHTML and collapsible nodes, it's layout is "skinable" and you can change it to fit your aesthetic taste. Krumo makes the output "human-readable" for real :) Plus it is compliant with both PHP4 and PHP5. Plus it detects "reference recursion". Plus you can use it to dump all various sort of data like debug back-traces, the superglobals ($_SERVER, $_ENV, $_REQUEST, $_COOKIE, $_GET, $_POST, $_SESSION), all the included files, all the declared classes, all the declared constants, all your PHP settings, all your php.ini values (if it is readable), all the loaded extensions, all the HTTP request headers, all the declared interfaces (for PHP5), all the file paths from INCLUDE_PATH, all the values of any particular INI file. Additionally it is designed to be easy to use - for example you can disable all the Krumo dumps instead of cleaning your code out of all print_r()'s and var_dump()'s. Anyway, if you check the site (http://krumo.sourceforge.net), you can found a lot of examples, demonstrations, documentation and all sort of helpful information.
Enthusiastic PHPers 22-Mar-2007 09:03
We had an interesting problem dumping an object that
contained embedded HTML. The application makes use
of buffer manipulation functions, so print_r's 2nd argument
wasn't helpful. Here is how we solved the problem:

$object = new ObjectContainingHTML();
$savedbuffercontents = ob_get_clean();
print_r($object);
$print_r_output = ob_get_clean();
ob_start();
echo $savedbuffercontents;
echo htmlentities($print_r_output);
Matthew Ruivo (mruivo at gmail) 23-Feb-2007 07:47
For those of you needing to print an array within a buffer callback function, I've created this quick function. It simply returns the array as a readable string rather than printing it. You can even choose whether to return it in normal text-mode or HTML. It's recursive, so multi-dimensial arrays are supported. I hope someone finds this useful!

<?php

    function return_array($array, $html = false, $level = 0) {
        $space = $html ? "&nbsp;" : " ";
        $newline = $html ? "<br />" : "\n";
        for ($i = 1; $i <= 6; $i++) {
            $spaces .= $space;
        }
        $tabs = $spaces;
        for ($i = 1; $i <= $level; $i++) {
            $tabs .= $spaces;
        }
        $output = "Array" . $newline . $newline;
        foreach($array as $key => $value) {
            if (is_array($value)) {
                $level++;
                $value = return_array($value, $html, $level);
                $level--;
            }
            $output .= $tabs . "[" . $key . "] => " . $value . $newline;
        }
        return $output;
    }

?>
markus (at) maxxmaster (dot) org 17-Jan-2007 03:11
i've been using a similar idea like Sawey's for quite a long time,
but was always disappointed that you don't see the name of the
variable passed to the function (eg calling it many times, you don't
know which value was provided by a certain variable at a certain time).
so i combined sawey's idea with one found on php.net to solve that problem.
for shellscripting the function only uses <pre>-tags when called in the web.

<?php
/* how print_r should be ;) */
function my_r(&$var, $scope=false, $label=false){

  if (is_string($scope) && $label==false) $label=$scope;
  if (!is_array($scope)) $scope = &$GLOBALS;

  $origin = $var;
  $var = $checker = "very_weird_value#".rand()."#isnt_it";
  $vname = false;

  foreach ($scope as $key => $value){
    if ($value === $checker){ $vname = "\$".$key; }
    if (is_array($value) && $key!="GLOBALS"){
      if ($pfad=aver($var,$value)){
        $vname = "\$${key}[\"". implode("\"][\"",$pfad)."\"]";
      }
    }
    if (is_object($value)){
      if ($pfad=aver($var,get_object_vars($value))){
        if (sizeof($pfad)<2) $vname = "\$${key}->".$pfad[0];
        else{
          $vname ="\$${key}->".$pfad[0];
          $vname.="[\"".implode("\"][\"", array_slice($pfad,1))."\"]";
        }
      }
    }
    if ($vname) break;
  }
  $var = $origin;

  if ($_SERVER["SERVER_NAME"] && !isset($_SERVER["TERM"])) echo "<pre>";
  if ($vname){
    echo $vname;
    if ($label) echo " #" . $label . "# ";
    else echo ": ";
  }else{
    if ($label) echo "$label: ";
  }
  print_r($var);
  if ($_SERVER["SERVER_NAME"] && !isset($_SERVER["TERM"])) echo "</pre>";
}

/* this function is needed to check in multidimensional arrays
if the value of interest exists. if yes, the path is returned */
function aver($needle,$haystack) { // array value exists recursive
  foreach($haystack as $key=>$val) {
    if(is_array($val) && $needle != $val && $key != "GLOBALS") {
      if($foo=aver($needle,$val)) return array_merge(array($key),$foo);
    }elseif($val === $needle) return array($key);
  }
  return false;
}
?>
SaWey @ localdomain 15-Jan-2007 08:44
I've seen a lot of tries to give an easy way to output the "print_r" info in a decent way in you favorite browser.

Here's mine:

echo ("<pre>");
print_r("-->here goes your data to output<--");
echo ("<pre>");

I personaly think this is the easyest way.
Grz
Dmitry Kochin <dco at mail dot ru> 08-Sep-2006 10:20
Sometimes print_r produces large output, especially when the data hierarchy is too deep. It is very difficult to analyze the dump about 1Mb length.

It would be great to have some way to fold arrays and objects and to look deeper into hierarchy only on demand.

Here is the solution. Just pass the print_r output to debug_var function:

debug_var('title', print_r($var, true));

and it will produce nice html with folding option.

<?php
  ob_start();
?>
Locals: Array
(
    [arr] => Array
        (
            [0] => Bon Object
                (
                    [n] => id
                    [v] => 1
                    [dv] =>
                    [dn] =>
                )

        )

    [b] => Bon Object
        (
            [n] => id
            [v] => 1
            [dv] =>
            [dn] =>
        )

    [k] => 0
    [row] => Array
        (
            [aid] => 1
            [bonus] => spell.id: 125;
            [req] =>
            [brcache] =>
            [auto] => 0
        )

    [sp] =>
)
<?php
  $str = ob_get_contents();
  ob_end_clean();

  debug_var('locals', $str);

function debug_var($name,$data)
{
    $captured = preg_split("/\r?\n/",$data);
    print "<script>function toggleDiv(num){
      var span = document.getElementById('d'+num);
      var a = document.getElementById('a'+num);
      var cur = span.style.display;
      if(cur == 'none'){
        a.innerHTML = '-';
        span.style.display = 'inline';
      }else{
        a.innerHTML = '+';
        span.style.display = 'none';
      }
    }</script>";
    print "<b>$name</b>\n";
    print "<pre>\n";
    foreach($captured as $line)
    {
        print debug_colorize_string($line)."\n";
    }
    print "</pre>\n";
}

function next_div($matches)
{
  static $num = 0;
  ++$num;
  return "$matches[1]<a id=a$num href=\"javascript: toggleDiv($num)\">+</a><span id=d$num style=\"display:none\">(";
}

/**
* colorize a string for pretty display
*
* @access private
* @param $string string info to colorize
* @return string HTML colorized
* @global
*/
function debug_colorize_string($string)
{
    $string = preg_replace("/\[(\w*)\]/i", '[<font color="red">$1</font>]', $string);
    $string = preg_replace_callback("/(\s+)\($/", 'next_div', $string);
    $string = preg_replace("/(\s+)\)$/", '$1)</span>', $string);
    /* turn array indexes to red */
    /* turn the word Array blue */
    $string = str_replace('Array','<font color="blue">Array</font>',$string);
    /* turn arrows graygreen */
    $string = str_replace('=>','<font color="#556F55">=></font>',$string);
    return $string;
}
?>

This example uses ideas from this article:
http://www.zend.com/zend/tut/tutorial-DebugLib.php
ohira (atto) web. de 13-Apr-2006 04:01
Bases on thbley´s sript i use this one to log some actions.
It will return a tabbed like string which you can output or whatever.

Input fields like "Password" will not be shown.

<?php

function print_r_string($arr,$first=true,$tab=0)
{
    $output = "";
    $tabsign = ($tab) ? str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$tab) : '';
    if ($first) $output .= "<pre><br>\n";
    foreach($arr as $key => $val)
    {
        switch (gettype($val))
        {
            case "array":
                $output .= $tabsign."[".htmlspecialchars($key)."] = array(".count($val).")<br>\n".$tabsign."(<br>\n";
                $tab++;
                $output .= print_r_string($val,false,$tab);
                $tab--;
                $output .= $tabsign.")<br>\n";
            break;
            case "boolean":
                $output .= $tabsign."[".htmlspecialchars($key)."] bool = '".($val?"true":"false")."'<br>\n";
            break;
            case "integer":
                $output .= $tabsign."[".htmlspecialchars($key)."] int = '".htmlspecialchars($val)."'<br>\n";
            break;
            case "double":
                $output .= $tabsign."[".htmlspecialchars($key)."] double = '".htmlspecialchars($val)."'<br>\n";
            break;
            case "string":
                $output .= $tabsign."[".htmlspecialchars($key)."] string = '".((stristr($key,'passw')) ? str_repeat('*', strlen($val)) : htmlspecialchars($val))."'<br>\n";
            break;
            default:
                $output .= $tabsign."[".htmlspecialchars($key)."] unknown = '".htmlspecialchars(gettype($val))."'<br>\n";
            break;
        }
    }
    if ($first) $output .= "</pre><br>\n";
    return $output;
}

echo print_r_string(array($_POST,$_GET)); // for Example
?>
reinder at fake-address dot com 31-Jan-2006 06:09
I always use this function in my code, because most of my functions return an Array or Boolean :

<?php

function printr ( $object , $name = '' ) {

    print ( '\'' . $name . '\' : ' ) ;

    if ( is_array ( $object ) ) {
        print ( '<pre>' )  ;
        print_r ( $object ) ;
        print ( '</pre>' ) ;
    } else {
        var_dump ( $object ) ;
    }

}

?>

( print_r gives no output on FALSE and that can be annoying! )
thbley at gmail dot com 16-Nov-2005 09:10
Here is a print_r that produces xml:
(now you can expand/collapse the nodes in your browser)

<?php
header('Content-Type: text/xml; CHARSET=ks_c_5601-1987');
echo print_r_xml($some_var);

function print_r_xml($arr,$first=true) {
  $output = "";
  if ($first) $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<data>\n";
  foreach($arr as $key => $val) {
    if (is_numeric($key)) $key = "arr_".$key; // <0 is not allowed
    switch (gettype($val)) {
      case "array":
        $output .= "<".htmlspecialchars($key)." type='array' size='".count($val)."'>".
          print_r_xml($val,false)."</".htmlspecialchars($key).">\n"; break;
      case "boolean":
        $output .= "<".htmlspecialchars($key)." type='bool'>".($val?"true":"false").
          "</".htmlspecialchars($key).">\n"; break;
      case "integer":
        $output .= "<".htmlspecialchars($key)." type='integer'>".
          htmlspecialchars($val)."</".htmlspecialchars($key).">\n"; break;
      case "double":
        $output .= "<".htmlspecialchars($key)." type='double'>".
          htmlspecialchars($val)."</".htmlspecialchars($key).">\n"; break;
      case "string":
        $output .= "<".htmlspecialchars($key)." type='string' size='".strlen($val)."'>".
          htmlspecialchars($val)."</".htmlspecialchars($key).">\n"; break;
      default:
        $output .= "<".htmlspecialchars($key)." type='unknown'>".gettype($val).
          "</".htmlspecialchars($key).">\n"; break;
    }
  }
  if ($first) $output .= "</data>\n";
  return $output;
}

?>
warhog at warhog dot net 12-Aug-2005 02:01
For very long arrays I have written a little function which formats an array quite nice and uses javascript for browsing it like a tree. The function is very customizable with the $style parameter.
For me it's of great use for browsing large array's, for example when those are used in language-files in some script and so on. It may even be used in "real" scripts for the "real" front-end, cause the tree can very easily be styled (look at the function or the outputted source and you'll see what i mean).

Here's the function:

<?php

function print_r_html($arr, $style = "display: none; margin-left: 10px;")
{ static $i = 0; $i++;
  echo "\n<div id=\"array_tree_$i\" class=\"array_tree\">\n";
  foreach($arr as $key => $val)
  { switch (gettype($val))
    { case "array":
        echo "<a onclick=\"document.getElementById('";
        echo array_tree_element_$i."').style.display = ";
        echo "document.getElementById('array_tree_element_$i";
        echo "').style.display == 'block' ?";
        echo "'none' : 'block';\"\n";
        echo "name=\"array_tree_link_$i\" href=\"#array_tree_link_$i\">".htmlspecialchars($key)."</a><br />\n";
        echo "<div class=\"array_tree_element_\" id=\"array_tree_element_$i\" style=\"$style\">";
        echo print_r_html($val);
        echo "</div>";
      break;
      case "integer":
        echo "<b>".htmlspecialchars($key)."</b> => <i>".htmlspecialchars($val)."</i><br />";
      break;
      case "double":
        echo "<b>".htmlspecialchars($key)."</b> => <i>".htmlspecialchars($val)."</i><br />";
      break;
      case "boolean":
        echo "<b>".htmlspecialchars($key)."</b> => ";
        if ($val)
        { echo "true"; }
        else
        { echo "false"; }
        echo  "<br />\n";
      break;
      case "string":
        echo "<b>".htmlspecialchars($key)."</b> => <code>".htmlspecialchars($val)."</code><br />";
      break;
      default:
        echo "<b>".htmlspecialchars($key)."</b> => ".gettype($val)."<br />";
      break; }
    echo "\n"; }
  echo "</div>\n"; }

?>

The function as it is now does not support the $return parameter as print_r does and will create an endless loop like print_r did in php-versions < 4.0.3 when there is an element which contains a reference to a variable inside of the array to print out :-/

I've tested it with PHP 5.0.6 and PHP 4.2.3 - no problems except those already mentioned.

please e-mail me if you've got a solution for the problems i've mentioned, i myself are not able to solve them 'cause i don't know how the hell i can find out whether a variable is a reference or not.
nicky dot weber at siner dot de 11-Jun-2005 11:13
Print arrays formatted for a browser easily:

<?php
function print_html_r( $aData ) {
    echo nl2br( eregi_replace( " ", " ", print_r( $data, TRUE ) ) );
}
?>
general at NOSPAMbugfoo dot com 20-Jan-2005 10:23
You can't use print_r($var, TRUE) inside a function which is a callback from ob_start() or you get the following error:
Fatal error: print_r(): Cannot use output buffering in output buffering display handlers


값의 저장 표현을 생성

  • ≪ print_r
  • settype ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 값의 저장 표현을 생성

serialize

(PHP 4, PHP 5, PHP 7)

serialize — 값의 저장 표현을 생성

설명

string serialize ( mixed $value )

값의 저장 표현을 생성합니다.

이는 PHP 값을 저장하거나 전달할 때 자료형이나 구조를 잃어버리지 않게 합니다.

일렬화된 문자열을 다시 PHP 변수로 만드려면, unserialize()를 사용합니다.

인수

value

일렬화할 값. serialize()는 resource형을 제외한 모든 형을 다룹니다. 자기 자신을 참조하는 배열을 serialize()할 수도 있습니다. 배열/객체 안의 순환 참조도 일렬화하여 저장됩니다. 그 외의 다른 참조는 읽어버립니다.

객체를 일렬화할 때, 일렬화 전에 __sleep 멤버 함수를 호출합니다. 이로써 일렬화하기 전에 객체를 마지막으로 정리할 수 있습니다. 마찬가지로, unserialize()을 사용하여 객체를 되돌릴 때는 __wakeup 멤버 함수를 호출합니다.

Note:

객체의 private 멤버는 이름 앞에 클래스 이름을 덧붙입니다; protected 멤버는 이름 앞에 '*'를 덧붙입니다. 이런 덧붙인 값은 모두 null 바이트를 가집니다.

반환값

value의 어디에라도 저장할 수 있는 바이트 스트림 표현을 가지는 문자열을 반환합니다.

예제

Example #1 serialize() 예제

<?php
// $session_data는 현재 사용자의 세션 정보를 다차원 배열로
// 가집니다. 요청의 마지막에 이것을 데이터베이스에 저장하기
// 위해서 serialize()를 사용합니다.

$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn,
      "UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!odbc_execute($stmt, &$sqldata)) {
    $stmt = odbc_prepare($conn,
     "INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!odbc_execute($stmt, &$sqldata)) {
        /* 무언가 잘못되었을때 할 일.. */
    }
}
?>

변경점

버전 | 설명
4.0.7 | 객체 일렬화 처리가 고정되었습니다.

주의

Note:

PHP 내부 객체를 일렬화할 수 없습니다.

참고

  • unserialize() - 저장 표현에서 PHP 값을 작성
  • Serializing Objects

User Contributed Notes

MiChAeLoKGB 23-May-2015 02:28
I did some testing to see the speed differences between serialize and json_encode, and my results with 250 iterations are:

PHP serialized in 0.0651714730263 seconds average
JSON encoded in 0.0254955434799 seconds average
json_encode() was roughly 155.62% faster than serialize()
Test took 27.2039430141 seconds with 300 iretations.

PHP serialized in 0.0564563179016 seconds average
JSON encoded in 0.0249140485128 seconds average
json_encode() was roughly 126.60% faster than serialize()
Test took 24.4148340225 seconds with 300 iretations.

From all my tests it looks like json_encode is on average about 120% faster (sometimes it gets to about 85% and sometimes to 150%).

Here is the PHP code you can run on your server to try it out:

<?php

// fillArray function myde by Peter Bailey
function fillArray($depth, $max){
    static $seed;
    if (is_null($seed)){
        $seed = array('a', 2, 'c', 4, 'e', 6, 'g', 8, 'i', 10);
    }
    if ($depth < $max){
        $node = array();
        foreach ($seed as $key){
            $node[$key] = fillArray( $depth + 1, $max );
        }
        return $node;
    }
    return 'empty';
}

function testSpeed($testArray, $iterations = 100){

    $json_time = array();
    $serialize_time = array();
    $test_start = microtime(true);

    for ($x = 1; $x <= $iterations; $x++){
        $start = microtime(true);
        json_encode($testArray);
        $json_time[] = microtime(true) - $start;

        $start = microtime(true);
        serialize($testArray);
        $serialize_time[] = microtime(true) - $start;
    }

    $test_lenght = microtime(true) - $test_start;
    $json_average = array_sum($json_time) / count($json_time);
    $serialize_average = array_sum($serialize_time) / count($serialize_time);

    $result = "PHP serialized in ".$serialize_average." seconds average<br>";
    $result .= "JSON encoded in ".$json_average." seconds average<br>";

    if ($json_average < $serialize_average){
        $result .= "json_encode() was roughly ".number_format( ($serialize_average / $json_average - 1 ) * 100, 2 )."% faster than serialize()<br>";
    } else if ( $serializeTime < $jsonTime ){
        $result .= "serialize() was roughly ".number_format( ($json_average / $serialize_average - 1 ) * 100, 2 )."% faster than json_encode()<br>";
    } else {
        $result .= "No way!<br>";
    }

    $result .= "Test took ".$test_lenght." seconds with ".$iterations." iterations.";

    return $result;

}

// Change the number of iterations (250) to lower if you exceed your maximum execution time
echo testSpeed(fillArray(0, 5), 250);

?>
Anonymous 14-Apr-2015 10:57
For everyone facing the conversion nightmare another Anonymous described here in an older comment (and he took the words out of my mouth), this T-SQL script might be of great help: https://github.com/mjohnsonperl/tsqlphpunserialize/tree/master.

Please do not follow the path of evil by dumping serialized data into relational databases.
tuxedobob 05-Jun-2014 01:36
When using serialize() to convert, say, an array to a string to pass via HTML forms, you will likely run into issues with quoting. This is because serialize() puts values in double quotes. The simplest solution is to quote your HTML form value with single quotes rather than double quotes. (This *is* allowed, according to W3C specs.)

So, instead of:

<?php
$arr = serialize($some_array);
?>
<input type="hidden" name="array" value="$arr" />

you would want to use:

<?php
$arr = serialize($some_array);
?>
<input type="hidden" name="array" value='$arr' />
Nanhe Kumar 27-Dec-2013 02:57
Note : You cannot save the state of a static variable by serializing.
<?php
/*
 * student.class.php
 */
class Student {

    public $name;
    private $roll;
    protected $age;
    static $class;

    public function __construct() {
        $this->name = "Nanhe Kumar";
        $this->roll = 1;
        $this->age = 16;
        Student::$class = "10+2";
    }

}
?>
<?php
/**
 * serialize.php
 */
include_once("student.class.php");

//Serialize
$so = new Student();
$serialized = serialize($so);
file_put_contents("test.txt", $serialized);
echo $serialized; //O:7:"Student":3:{s:4:"name";s:11:"Nanhe Kumar";s:13:"Studentroll";i:1;s:6:"*age";i:16;}
?>
<?php
/**
 * unserialize.php
 */
include_once("student.class.php");
$content = file_get_contents("test.txt");
$unserialized = unserialize($content);
print_r($unserialized);//Student Object ( [name] => Nanhe Kumar [roll:Student:private] => 1 [age:protected] => 16 )
?>
frost at easycast dot ru 24-Sep-2013 07:56
Closures cannot be serialized:
<?php
$func = function () {echo 'hello!';};
$func(); // prints "hello!"

$result = serialize($func);  // Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed'
?>
allan666 at gmail.com 07-Mar-2013 10:28
Oddly, if you serialize a class that was previously unserialized, the class of the variable changes to string... Example:

$R = unserialize($serialized_object);
$R->method(); // this is ok
$str = serialize($R);
echo(get_class($R));

this will output "string"!!!!! whereas if the first line was

$R = new my_class();

it would output "my_class"!

I don't know if that is a bug, but the manual is not clear about that! (somehow $R in serialize($R) is being passed by reference, since it changes class).
nh at ngin dot de 22-Jan-2013 06:59
Serializing floating point numbers leads to weird precision offset errors:

<?php

echo round(96.670000000000002, 2);
// 96.67

echo serialize(round(96.670000000000002, 2));
// d:96.670000000000002;

echo serialize(96.67);
// d:96.670000000000002;

?>

Not only is this wrong, but it adds a lot of unnecessary bulk to serialized data. Probably better to use json_encode() instead (which apparently is faster than serialize(), anyway).
Andrew B 05-Sep-2012 06:42
When you serialize an array the internal pointer will not be preserved. Apparently this is the expected behavior but was a bit of a gotcha moment for me. Copy and paste example below.

<?php
//Internal Pointer will be 2 once variables have been assigned.
$array = array();
$array[] = 1;
$array[] = 2;
$array[] = 3;

//Unset variables. Internal pointer will still be at 2.
unset($array[0]);
unset($array[1]);
unset($array[2]);

//Serialize
$serializeArray = serialize($array);

//Unserialize
$array = unserialize($serializeArray);

//Add a new element to the array
//If the internal pointer was preserved, the new array key should be 3.
//Instead the internal pointer has been reset, and the new array key is 0.
$array[] = 4;

//Expected Key - 3
//Actual Key - 0
echo "<pre>" , print_r($array, 1) , "</pre>";
?>
Anonymous 29-Feb-2012 04:44
Please! please! please! DO NOT serialize data and place it into your database. Serialize can be used that way, but that's missing the point of a relational database and the datatypes inherent in your database engine. Doing this makes data in your database non-portable, difficult to read, and can complicate queries. If you want your application to be portable to other languages, like let's say you find that you want to use Java for some portion of your app that it makes sense to use Java in, serialization will become a pain in the buttocks. You should always be able to query and modify data in the database without using a third party intermediary tool to manipulate data to be inserted.

I've encountered this too many times in my career, it makes for difficult to maintain code, code with portability issues, and data that is it more difficult to migrate to other RDMS systems, new schema, etc. It also has the added disadvantage of making it messy to search your database based on one of the fields that you've serialized.

That's not to say serialize() is useless. It's not... A good place to use it may be a cache file that contains the result of a data intensive operation, for instance. There are tons of others... Just don't abuse serialize because the next guy who comes along will have a maintenance or migration nightmare.
pgl at yoyo dot org 03-Feb-2011 06:13
Another suggestion for coping with binary data in serialize()d variables is just to base64_encode() those fields before serializing. It will increase the size of the variable, but not by too much.
travis at travishegner dot com 02-Mar-2010 09:37
If serializing objects to be stored into a postgresql database, the 'null byte' injected for private and protected members throws a wrench into the system. Even pg_escape_bytea() on the value, and storing the value as a binary type fails under certain circumstances.

For a dirty work around:
<?php

$serialized_object = serialize($my_object);
$safe_object = str_replace("\0", "~~NULL_BYTE~~", $serialized_object);

?>

this allows you to store the object in a readable text format as well. When reading the data back:

<?php

$serialized_object = str_replace("~~NULL_BYTE~~", "\0", $safe_object);
$my_object = unserialize($serialized_object);

?>

The only gotcha's with this method is if your object member names or values may somehow contain the odd "~~NULL_BYTE~~" string. If that is the case, then str_replace() to a string that you are guaranteed not to have any where else in the string that serialize() returns.
Also remember to define the class before calling unserialize().

If you are storing session data into a postgresql database, then this workaround is an absolute must, because the $data passed to the session's write function is already serialized.

Thanks,
Travis Hegner
Anonymous 21-Jan-2010 06:23
you should really use mysql_real_escape_string() for escaping (serialized) strings that got thrown into a query (visit php.net/mysql_real_escape_string for further information)
donotmail at me dot com 02-Sep-2009 08:16
NOTE: php's serialize does not properly serialize arrays with which a slice of the array is a reference to the array itself, observe:

<?php
$a = array();
$a[0] = "blah";
$a[1] =& $a;

$a[1][0] = "pleh"; // $a[0] === "pleh"

$b = unserialize(serialize($a));

// $b[0] == "pleh", $b[1][0] == "pleh"

$b[1][0] = "blah";
?>

now $b[1][0] == "blah", but $b[0] == "pleh"
after serializing and unserializing, slice 1 is no longer a reference to the array itself... I have found no way around this problem... even manually modifying the serialized string from
'a:2:{i:0;s:4:"pleh";i:1;a:2:{i:0;s:4:"pleh";i:1;R:3;}}'
to
'a:2:{i:0;s:4:"pleh";i:1;R:1;}'

to force the second slice to be a reference to the first element of the serialization (the array itself), it seemed to work at first glance, but then unreferences it when you alter it again, observe:

<?php
    $testser = 'a:2:{i:0;s:4:"pleh";i:1;R:1;}';

    $tmp = unserialize($testser);

    print_r($tmp);

    print "\n-----------------------\n";

    $tmp[1][0] = "blah";

    print_r($tmp);

?>

outputs:
Array
(
    [0] => pleh
    [1] => Array
 *RECURSION*
)

-----------------------
Array
(
    [0] => pleh
    [1] => Array
        (
            [0] => blah
            [1] => Array
                (
                    [0] => pleh
                    [1] => Array
 *RECURSION*
                )

        )

)
Alessandro Segala 17-Jul-2009 05:18
I needed to serialize an array to store it inside a database.
I was looking for a fast, simple way to do serialization, and I came out with 2 options: serialize() or json_encode().

I ran some benchmarks to see which is the faster, and, surprisingly, I found that serialize() is always between 46% and 96% SLOWER than json_encode().
So, if you don't need to serialize objects and have the json extension available, consider using it instead of serialize().
barbuj at NOSPAM dot yahoo dot com 14-Nov-2007 11:19
In my specific situation, I wanted to be able to pass some data from page to page, but without relying on a session variable. The answer I came up with was to serialize() the object in question, pass it on to the next page as a hidden form field, then unserialize() it. When ALL class variables are public, this worked fine. However, if there was at least one private/protected variable, the code no longer worked as expected ("Fatal error: Call to a member function display() on a non-object in page2.php on line 4")

As others have already mentioned, private/protected class variables will not behave nicely (private variables are prefixed by class_name + &#65533;, while protected variables are only prefixed by &#65533; - when looking at the page source using Firefox). Internet Explorer does NOT display the extra character between the class name and variable name, but the code still doesn't work as one would expect.

Suppose you have a simple class:

testclass.php
=============
<?php
class TestClass {
  var $i = 1;

  function display() {
    echo "i=" . $this->i;
  }
?>

page1.php
=========
<?php
  require_once 'testclass.php';
  $tc = new TestClass;
  $tc->display();
?>
<form method = "post" action = "page2.php">
<input type = "hidden" name = "str" value = "<?php echo htmlspecialchars( serialize( $tc ) ); ?>">
<input type = "submit">
</form>

page2.php
=========
<?php
  require_once 'testclass.php';
  $tc = unserialize( stripslashes( htmlspecialchars_decode( $_POST["str"] ) ) );
  $tc->display();
?>

The fix, suggested by evulish on #php/irc.dal.net, is to replace htmlspecialchars()/htmlspecialchars_decode() by base64_encode()/base64_decode. The code becomes:

page1.php
=========
<input type = "hidden" name = "str" value = "<?php echo base64_encode( serialize( $tc ) ); ?>">

page2.php
=========
<?php
  $tc = unserialize( base64_decode( $_POST["str"] ) );
?>

Hope this will help someone...
Jeex 06-Nov-2007 11:59
It may be worth noting that, depending on the size of the object you are serializing, the serialize function can take up a lot of memory.

If your script isn't working as expected, your serialize call may have pushed the memory usage over the limit set by memory_limit in php.ini.

More info on memory limits here: http://www.php.net/manual/en/ini.core.php
friday13 at ig dot com dot br 19-Jun-2007 04:15
I have problem to use serialize function with hidden form field and the resolution was use htmlentities.

Ex.:

<?

$lista = array( 'pera', 'maca', 'laranja' );

print "< input type='hidden' name='teste' value='htmlentities( serialize( $lista ) )'" >";

?>
Ates Goral 18-Jul-2006 08:59
Corrections/clarifications to  "Anatomy of a serialize()'ed value":

All strings appear inside quotes. This applies to string values, object class names and array key names. For example:

s:3:"foo"
O:7:"MyClass":1:{...
a:2:{s:3:"bar";i:42;...

Object property names and values are delimited by semi-colons, not colons. For example:

O:7:"MyClass":2:{s:3:"foo";i:10;s:3:"bar";i:20}

Double/float values are represented as:

d:0.23241446
egingell at sisna dot com 16-May-2006 02:12
<?
/*
Anatomy of a serialize()'ed value:

 String
 s:size:value;

 Integer
 i:value;

 Boolean
 b:value; (does not store "true" or "false", does store '1' or '0')

 Null
 N;

 Array
 a:size:{key definition;value definition;(repeated per element)}

 Object
 O:strlen(object name):object name:object size:{s:strlen(property name):property name:property definition;(repeated per property)}

 String values are always in double quotes
 Array keys are always integers or strings
    "null => 'value'" equates to 's:0:"";s:5:"value";',
    "true => 'value'" equates to 'i:1;s:5:"value";',
    "false => 'value'" equates to 'i:0;s:5:"value";',
    "array(whatever the contents) => 'value'" equates to an "illegal offset type" warning because you can't use an
    array as a key; however, if you use a variable containing an array as a key, it will equate to 's:5:"Array";s:5:"value";',
     and
    attempting to use an object as a key will result in the same behavior as using an array will.
*/
?>
MC_Gurk at gmx dot net 03-Jan-2006 04:44
If you are going to serialie an object which contains references to other objects you want to serialize some time later, these references will be lost when the object is unserialized.
The references can only be kept if all of your objects are serialized at once.
That means:

$a = new ClassA();
$b = new ClassB($a); //$b containes a reference to $a;

$s1=serialize($a);
$s2=serialize($b);

$a=unserialize($s1);
$b=unserialize($s2);

now b references to an object of ClassA which is not $a. $a is another object of Class A.

use this:
$buf[0]=$a;
$buf[1]=$b;
$s=serialize($buf);
$buf=unserialize($s);
$a=$buf[0];
$b=$buf[1];

all references are intact.
Alexander Podgorny 03-Dec-2005 08:47
Here is an example of a base class to implement object persistence using serialize and unserialize:

<?php
class Persistent
{
    var $filename;

    /**********************/
    function Persistent($filename)
    {
        $this->filename = $filename;
        if(!file_exists($this->filename)) $this->save();
    }
    /**********************/
    function save()
    {
        if($f = @fopen($this->filename,"w"))
        {
            if(@fwrite($f,serialize(get_object_vars($this))))
            {
                @fclose($f);
            }
            else die("Could not write to file ".$this->filename." at Persistant::save");
        }
        else die("Could not open file ".$this->filename." for writing, at Persistant::save");

    }
    /**********************/
    function open()
    {
        $vars = unserialize(file_get_contents($this->filename));
        foreach($vars as $key=>$val)
        {
            eval("$"."this->$key = $"."vars['"."$key'];");
        }
    }
    /**********************/
}

?>

When an object is extended from this one it can be easily saved and re-opened using it's own methods as follows:

<?

class foo extends Persistent
{
   var $counter;
   function inc()
   {
       $this->counter++;
   }
}

$fooObj = new $foo;
$foo->open();
print $foo->counter; // displays incrementing integer as page reloads
$foo->inc();
$foo->save();

?>
stephen dot adamson at smius dot com 01-Nov-2005 06:40
If you are serializing an object to store it in the database, using __sleep() can save you some space. The following code will not store empty (null) variables in the serialized string. For my purposes this saved a lot of space, since some of the member variables would not be given values.

function __sleep()
{
    $allVars = get_object_vars($this);
    $toReturn = array();
    foreach(array_keys($allVars) as $name)
    {
        if (isset($this->$name))
        {
            $toReturn[] = $name;
        }
    }
    return $toReturn;
}
wiart at yahoo dot com 16-Dec-2004 12:40
Warning: on 64 bits machines,  if you use a long string only composed of numbers as a key in an array and serialize/unserialize it, you can run into problems:

an example code:
$arr["20041001103319"] = 'test';
var_dump( $arr);
$arr_in_str = serialize($arr);
print "Now result is: $arr_in_str<BR />";
$final_arr = unserialize($arr_in_str);
print "The final unserialized array:<BR />";
var_dump($final_arr);

The result:
array(1) { [20041001103319]=>  string(4) "test" }
Now result is: a:1:{i:20041001103319;s:4:"test";}
The final unserialized array:
array(1) { [683700183]=> string(4) "test" }

As you can see, the original array :
$arr["20041001103319"] = "test"

after serialize/unserialize is:
$arr[683700183] = "test"

As you can see,  the key has changed ...

Apparently a problem of implicit casting + integer overflow (I posted a PHP bug report: http://bugs.php.net/bug.php?id=31117)

I tested with the latest 4.3.10 compiled on my laptop (32 bits, Mandrake 9.1) --> no such problem. But compiled on AMD 64 bits (Red Hat Taroon), the problem is present.

Hope this will help some of you to not loose almost a whole day of debugging ;-)
paul at moveNOtoSPAMiceland DOT com 18-Nov-2004 05:29
I was trying to submit a serialized array through a hidden form field using POST and was having a lot of trouble with the quotes. I couldn't figure out a way to escape the quotes in the string so that they'd show up right inside the form, so only the characters up to the first set of quotes were being sent.

My solution was to base64_encode() the string, put that in the hidden form field, and send that through the POST method. Then I decoded it (using base64_decode()) on the other end. This seemed to solve the problem.
hfuecks at phppatterns dot com 22-Apr-2004 05:20
Regarding serializing PHP data types to Javascript, following Ivans note below, theres an example at http://www.tekool.net/php/js_serializer/.

The basic serialization looks good although, in its current form, it works on the basis of generating Javascript source which a browser executes as a page loads. Using Javascripts eval() the same can be done with strings containing Javascript if youre working with something like XMLHTTPRequest
pli9 at itsa dot ucsf dot edu 01-Feb-2003 08:05
I have also written some code for importing serialized PHP data into PERL and then writing it back into PHP.  I think the similar library posted above is actually more robust for a few select cases, but mine is more compact and a little easier to follow.  I'd really like comments if anyone finds this useful or has improvements.  Please credit me if you use my code.

http://www.hcs.harvard.edu/~pli/code/serialPHP.pm
MarkRoedel at letu dot edu 22-Feb-2000 07:58
A call to serialize() appears to mess with the array's internal pointer.  If you're going to be walking through your array after serializing it, you'll want to make a call to reset() first.


변수의 자료형을 설정

  • ≪ serialize
  • strval ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수의 자료형을 설정

settype

(PHP 4, PHP 5, PHP 7)

settype — 변수의 자료형을 설정

설명

bool settype ( mixed &$var , string $type )

var 변수의 자료형을 type로 설정합니다.

인수

var

변환할 변수

type

사용할 수 있는 type 값:

  • "boolean" (혹은, PHP 4.2.0부터, "bool")
  • "integer" (혹은, PHP 4.2.0부터, "int")
  • "float" (PHP 4.2.0부터만 가능, 이전의 버전은 배제된 표현 "double"을 사용)
  • "string"
  • "array"
  • "object"
  • "null" (PHP 4.2.0부터)

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 settype() 예제

<?php
$foo = "5bar"; // string
$bar = true;   // boolean

settype($foo, "integer"); // $foo는 이제 5   (integer)
settype($bar, "string");  // $bar는 이제 "1" (string)
?>

주의

Note:

"ini"의 최대값은 PHP_INT_MAX입니다.

참고

  • gettype() - 변수의 자료형을 얻습니다
  • 자료형 변환
  • 자료형 조절

User Contributed Notes

Special Notes 26-Feb-2012 09:00
Note that you can't use this to convert a string 'true' or 'false' to a boolean variable true or false as a string 'false' is a boolean true. The empty string would be false instead...

<?php
$var = "true";
settype($var, 'bool');
var_dump($var); // true

$var = "false";
settype($var, 'bool');
var_dump($var); // true as well!

$var = "";
settype($var, 'bool');
var_dump($var); // false
?>
Chris Sullins 19-Apr-2010 08:59
settype() has some really strange, potentially buggy behavior.

As noted by Michael Benedict, using settype() on a variable will initialize that variable.  What is stranger is that using settype() on an uninitialized variable that you are treating as an array or object will also initialize the variable.  So:

<?php
settype($foo->bar,"integer"); // stdClass Object ( [test] => 0 )
?>

This works for a chain of any length: $foo->bar['baz']->etc

Next we look at what happens if $foo is already set.

<?php
$foo = false;
settype($foo->bar,"integer"); // stdClass Object ( [test] => 0 )
?>

In and of itself, this wouldn't be problematic.  It might even make sense.  But in all other cases where $foo is defined, even if (boolean) $foo === false, it will throw an error unless $foo->bar is valid (i.e. $foo is an object already).

<?php
$foo = true;
settype($foo->bar,"integer"); // Notice: Trying to get property of non-object
?>
NWdev 17-Nov-2008 06:38
In trying to convert an array of strings to an array of ints,
I attempted to use settype with array_walk.

<?php
//$numArray is generated by another process
$numArray = array('13','14','33');

var_dump($numArray);

//my conversion function
function str_to_int($val){
  //remember: settype($x, 'int') returns boolean (1=success, 0=failure)
  //--> so return $x to return new value
    settype($val,'int');
    echo "<br />gettype = ".gettype($val)."<br />";
    return $val;
}

array_walk($numArray,'str_to_int');

var_dump($numArray);
?>

The var_dumps both return the following:
<?php
array(3) { [0]=> string(2) "13" [1]=> string(2) "14" [2]=> string(2) "33" }
?>

The gettype echo will show the value as an integer.

So it seems that settype($val,'int') makes the conversion,
but the function return value remains a string.
Since settype returns a boolean, using
<?php $val = settype($val, 'int'); ?>
is not a option.

I resolved my array value conversion using this instead:
<?php
$numArray =
      array_map(create_function('$value', 'return (int)$value;'),$numArray);
?>
Thanks to the posting here:
http://usrportage.de/archives/
808-Convert-an-array-of-strings-into-an-array-of-integers.html

Perhaps this will save someone else spinning wheels a bit.

Also thanks to robin at barafranca dot com for
pointing out the boolean return value of settype.
mtinsley at dallasairmotive dot com 22-Jun-2008 10:36
Yes, just look for the ampersand (&) in the function signature. Here you see:

bool settype  ( mixed &$var  , string $type  )

There is an & before the first parameter ($var). This means the variable is passed in by reference. So the function is working with the original variable and not a copied local version. You will see this in other php functions such as asort();

References Explained: http://us3.php.net/manual/en/language.references.php
robin at barafranca dot com 05-Mar-2008 01:20
Just a quick note, as this caught me out very briefly:

settype() returns bool, not the typecasted variable - so:

$blah = settype($blah, "int"); // is wrong, changes $blah to 0 or 1
settype($blah, "int"); // is correct

Hope this helps someone else who makes a mistake.. ;)
ludvig dot ericson gmail.dot com 09-Apr-2006 05:36
To matt:
This function accepts a paremeter, which does not imply you using hardcoded stuff, instead you can let the user choose! \o/

As a part of a framework or something.

Plus, you can probably call this with call_user_func
matt at mattsoft dot net 07-Dec-2005 06:49
using (int) insted of the settype function works out much better for me. I have always used it. I personally don't see where settype would ever come in handy.
Michael Benedict 29-Oct-2005 07:55
note that settype() will initialize an undefined variable.  Therefore, if you want to preserve type and value, you should wrap the settype() call in a call to isset().

<?php
settype($foo, "integer");
echo("|$foo|");
?>

prints "|0|", NOT "||".

To get the latter, use:
<?php
if(isset($foo)) settype($foo, "integer");
echo("|$foo|");
?>
25-Oct-2005 03:30
James Reiher (IL) writes:
23-Feb-2005 06:50

$agentnum = "007";
$agentnum = settype($agentnum, "int");
echo $agentnum; // will show up as 1 instead of 7!

James, the return value of settype function is boolean, 1 if succsess.
Correct code: $success=settype($agentnum, "int");
The $agentnum is now 7 (not 007 or not 1)!
nospamplease at veganismus dot ch 22-Jul-2005 04:38
you must note that this function will not set the type permanently! the next time you set the value of that variable php will change its type as well.
reinier_deblois at hotmail dot com 13-Mar-2005 06:18
Instead of settype you could use:
<?php

$int=593;  // $int is a integer

$int.="";   // $int is now a string
James Reiher (IL) 23-Feb-2005 06:50
I had a problem with PHP destroying the value of my integer with leading zeros as follows:

$agentnum = "007";
$agentnum = settype($agentnum, "int");
echo $agentnum; // will show up as 1 instead of 7!

Oddly enough, this works fine, (at least for PHP 4.3):
$agentnumber = "007";
$agentnumber += 0; // convert $number to numeric type
echo $agentnumber; // will now show up as 7!

If you do this for gods sake leave a comment on the line because its definitely not by-the-book coding. Another commentor here has used regular expressions to weed out the leading zeros, so I know its not the only solution.

I also tried the equivelant of:
$agentnum = "007";
$agentnum = (int)$agentnumber;
echo $agentnum;

But the result is a nonsense number, probably by using the concatenation of the ASCII codes as the integer.
memandeemail at gmail dot com 09-Dec-2004 02:17
/**
    * @return bool
    * @param array[byreference] $values
    * @desc Convert an array or any value to Escalar Object [not tested in large scale]
    */
    function setobject(&$values) {
        $values = (object) $values;
        foreach ($values as $tkey => $val) {
            if (is_array($val)) {
                setobject($val);
                $values->$tkey = $val;
            }
        }
        return (bool) $values;
    }
23-Nov-2004 10:34
in PHP3 converting a string to any number results in the value becoming 0.  To check if a string represents a number try this:
<PRE>
$test = "0001";
$testcp = $test;
settype($testcp,"double");

if (strval($testcp) === $test) {
   echo("\$test is a number");
} else {
   echo ("\$test is not a number");
}
</PRE>
sdibb at myway dot com 07-Sep-2003 07:03
Using settype is not the best way to convert a string into an integer, since it will strip the string wherever the first non-numeric character begins.  The function intval($string) does the same thing.

If you're looking for a security check, or to strip non-numeric characters (such as cleaning up phone numbers or ZIP codes),  try this instead:

<?
     $number=ereg_replace("[^0-9]","",$number);
?>
ns at canada dot com 06-May-2000 01:38
This settype() behaviour seems consistent to me. Quoting two sections from the manual:

"When casting from a scalar or a string variable to an array, the variable will become the first element of the array: "
<pre>
2 $var = 'ciao';
3 $arr = (array) $var;
4 echo $arr[0];  // outputs 'ciao'
</pre>

And if (like your code above) you do a settype on an empty variable, you'll end up with a one element array with an empty (not unset!) first element. So appeanding to it will start appending at index 1. As for why reset() doesn't do anything:

"When you assign a value to an array variable using empty brackets, the value will be added onto the end of the array."

It doesn't matter where the array counter is; values are added at the end, not at the counter.


변수의 문자열 값을 얻습니다

  • ≪ settype
  • unserialize ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수의 문자열 값을 얻습니다

strval

(PHP 4, PHP 5, PHP 7)

strval — 변수의 문자열 값을 얻습니다

설명

string strval ( mixed $var )

변수의 문자열 값을 얻습니다. 문자열로 변환에 대한 자세한 정보는 string 문서를 참고하십시오.

인수

var

string으로 변환할 변수

var는 모든 스칼라형이 될 수 있습니다. strval()에 배열이나 객체를 사용할 수 없습니다.

반환값

var의 string 값.

예제

Example #1 PHP5의 __toString 마법 메쏘드를 사용하는 strval() 예제

<?php
class StrValTest
{
        public function __toString()
        {
                return __CLASS__;
        }
}

// 'StrValTest' 출력
echo strval(new StrValTest);
?>

참고

  • floatval() - 변수의 float 값을 얻습니다
  • intval() - 변수의 정수값을 얻습니다
  • settype() - 변수의 자료형을 설정
  • 자료형 조절

User Contributed Notes

Mark Clements 02-Jun-2016 10:17
Some notes about how this function has changed over time, with regards the following statement:

> You cannot use strval() on arrays or on objects that
> do not implement the __toString() method.

== Arrays ==

In PHP 5.3 and below, strval(array(1, 2, 3)) would return the string "Array" without any sort of error occurring.

From 5.4 and above, the return value is unchanged but you will now get a notice-level error: "Array to string conversion".

== Objects ==

For objects that do not implement __toString(), the behaviour has varied:

PHP 4: "Object"
PHP 5 < 5.2: "Object id #1" (number obviously varies)
PHP >= 5.2: Catchable fatal error:  Object of class X could not be converted to string
Hayley Watson 22-Aug-2007 05:53
As of PHP 5.2, strval() will return the string value of an object, calling its __toString() method to determine what that value is.
NyctoFixer at gmail dot com 11-Jun-2007 09:19
As of PHP 5.1.4 (I have not tested it in later versions), the strval function does not attempt to invoke the __toString method when it encounters an object. This simple wrapper function will handle this circumstance for you:

<?

/**
 * Returns the string value of a variable
 *
 * This differs from strval in that it invokes __toString if an object is given
 * and the object has that method
 */
function stringVal ($value)
{
    // We use get_class_methods instead of method_exists to ensure that __toString is a public method
    if (is_object($value) && in_array("__toString", get_class_methods($value)))
        return strval($value->__toString());
    else
        return strval($value);
}

?>
kendsnyder+phpnet at gmail dot com 02-Jun-2007 07:08
The only way to convert a large float to a string is to use printf('%0.0f',$float); instead of strval($float); (php 5.1.4).

// strval() will lose digits around pow(2,45);
echo pow(2,50); // 1.1258999068426E+015
echo (string)pow(2,50); // 1.1258999068426E+015
echo strval(pow(2,50)); // 1.1258999068426E+015

// full conversion
printf('%0.0f',pow(2,50)); // 112589906846624
echo sprintf('%0.0f',pow(2,50)); // 112589906846624
portos_ze_retour at hotmail dot fr 10-Mar-2006 05:15
In complement to Tom Nicholson's contribution, here is the french version (actually it's possible to change the language, but you should check the syntax ;) )

function int_to_words($x) {
   global $nwords;

   if(!is_numeric($x))
     $w = '#';
   else if(fmod($x, 1) != 0)
     $w = '#';
   else {
     if($x < 0) {
         $w = $nwords['minus'].' ';
         $x = -$x;
     } else
         $w = '';
     // ... now $x is a non-negative integer.

     if($x < 21)  // 0 to 20
         $w .= $nwords[$x];
     else if($x < 100) {  // 21 to 99
         $w .= $nwords[10 * floor($x/10)];
         $r = fmod($x, 10);
         if($r > 0)
           $w .= '-'. $nwords[$r];
     } else if($x < 1000) {  // 100 to 999
         $w .= $nwords[floor($x/100)] .' '.$nwords['hundred'];
         $r = fmod($x, 100);
         if($r > 0)
           $w .= ' '.$nwords['separator'].' '. int_to_words($r);
     } else if($x < 1000000) {  // 1000 to 999999
         $w .= int_to_words(floor($x/1000)) .' '.$nwords['thousand'];
         $r = fmod($x, 1000);
         if($r > 0) {
           $w .= ' ';
           if($r < 100)
               $w .= $nwords['separator'].' ';
           $w .= int_to_words($r);

         }
     } else {    //  millions
         $w .= int_to_words(floor($x/1000000)) .' '.$nwords['million'];
         $r = fmod($x, 1000000);
         if($r > 0) {
           $w .= ' ';
           if($r < 100)
               $word .= $nwords['separator'].' ';
           $w .= int_to_words($r);
         }
     }
   }
   return $w;
}

 // Usage in English
$nwords = array( "zero", "one", "two", "three", "four", "five", "six", "seven",
                   "eight", "nine", "ten", "eleven", "twelve", "thirteen",
                   "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
                   "nineteen", "twenty", 30 => "thirty", 40 => "forty",
                   50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty",
                   90 => "ninety" , "hundred" => "hundred", "thousand"=> "thousand", "million"=>"million",
                   "separator"=>"and", "minus"=>"minus");

echo 'There are currently '. int_to_words(-120223456) . ' members logged on.<br>';

 //Utilisation en Francais
$nwords = array( "zero", "un", "deux", "trois", "quatre", "cinq", "six", "sept",
                   "huit", "neuf", "dix", "onze", "douze", "treize",
                   "quatorze", "quinze", "seize", "dix-sept", "dix-huit",
                   "dix-neuf", "vingt", 30 => "trente", 40 => "quarante",
                   50 => "cinquante", 60 => "soixante", 70 => "soixante-dix", 80 => "quatre-vingt",
                   90 => "quatre-vingt-dix" , "hundred" => "cent", "thousand"=> "mille", "million"=>"million",
                   "separator"=>"", "minus"=>"moins");

echo 'Il y a actuellement '. int_to_words(-120223456) . ' membres connectes.<br>';
anthony dot parsons at manx dot net 09-Jan-2006 06:59
If you have to compare object variables like this be careful not to make a typo, or you could end up calling __set() -
<?php
/* Does what you'd expect it to */
if ( $user->password == $user2->password )

/* Doesn't */
if ( $user->password = $user2->password )
?>

To avoid that ever happening, do it like this:
<?php
if ( strval($user->password) == $user2->password )
?>
php at ianco dot co dot uk 07-Oct-2005 01:36
I can't help being surprised that

(string)"0" == (string)"0.00"

evaluates to true. It's the same with strval and single quotes.
=== avoids it.

Why does it matter? One of my suppliers, unbelievably, uses 0 to mean standard discount and 0.00 to mean no discount in their stock files.
Steve Ball 09-Sep-2005 04:18
It seems that one is being treated as an unsigned large int (32 bit), and the other as a signed large int (which has rolled over/under).

2326201276 - (-1968766020) =  4294967296.
Tom Nicholson 28-Apr-2004 06:13
If you want to convert an integer into an English word string, eg. 29 -> twenty-nine, then here's a function to do it.

Note on use of fmod()
  I used the floating point fmod() in preference to the % operator, because % converts the operands to int, corrupting values outside of the range [-2147483648, 2147483647]

I haven't bothered with "billion" because the word means 10e9 or 10e12 depending who you ask.

The function returns '#' if the argument does not represent a whole number.

<?php
$nwords = array( "zero", "one", "two", "three", "four", "five", "six", "seven",
                   "eight", "nine", "ten", "eleven", "twelve", "thirteen",
                   "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
                   "nineteen", "twenty", 30 => "thirty", 40 => "forty",
                   50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty",
                   90 => "ninety" );

function int_to_words($x) {
   global $nwords;

   if(!is_numeric($x))
      $w = '#';
   else if(fmod($x, 1) != 0)
      $w = '#';
   else {
      if($x < 0) {
         $w = 'minus ';
         $x = -$x;
      } else
         $w = '';
      // ... now $x is a non-negative integer.

      if($x < 21)   // 0 to 20
         $w .= $nwords[$x];
      else if($x < 100) {   // 21 to 99
         $w .= $nwords[10 * floor($x/10)];
         $r = fmod($x, 10);
         if($r > 0)
            $w .= '-'. $nwords[$r];
      } else if($x < 1000) {   // 100 to 999
         $w .= $nwords[floor($x/100)] .' hundred';
         $r = fmod($x, 100);
         if($r > 0)
            $w .= ' and '. int_to_words($r);
      } else if($x < 1000000) {   // 1000 to 999999
         $w .= int_to_words(floor($x/1000)) .' thousand';
         $r = fmod($x, 1000);
         if($r > 0) {
            $w .= ' ';
            if($r < 100)
               $w .= 'and ';
            $w .= int_to_words($r);
         }
      } else {    //  millions
         $w .= int_to_words(floor($x/1000000)) .' million';
         $r = fmod($x, 1000000);
         if($r > 0) {
            $w .= ' ';
            if($r < 100)
               $word .= 'and ';
            $w .= int_to_words($r);
         }
      }
   }
   return $w;
}

?>

Usage:
<?php
echo 'There are currently '. int_to_words($count) . ' members logged on.';
?>


저장 표현에서 PHP 값을 작성

  • ≪ strval
  • unset ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 저장 표현에서 PHP 값을 작성

unserialize

(PHP 4, PHP 5, PHP 7)

unserialize — 저장 표현에서 PHP 값을 작성

설명

mixed unserialize ( string $str )

unserialize()은 하나의 일렬화된 변수를 받아서 PHP 값으로 변환합니다.

인수

str

일렬화된 문자열.

변수가 객체로 unserialize되고, 성공적으로 객체로 재구성되면, PHP는 자동적으로 __wakeup() 멤버 함수를 호출합니다. (존재한다면)

Note: unserialize_callback_func 지시어

일렬화를 푸는 도중에 미정의된 클래스가 생성될 때 호출할 콜백 함수를 설정할 수 있습니다. (미완성된 object "__PHP_Incomplete_Class"를 방지합니다) php.ini, ini_set(), .htaccess 등을 이용해서 'unserialize_callback_func'를 정의하십시오. 미정의 클래스가 생성되어야 할 때마다 호출됩니다. 이 기능을 끄려면 설정을 비워두면 됩니다.

반환값

변환된 값이 반환되며, boolean, integer, float, string, array, object가 될 수 있습니다.

주어진 문자열에서 일렬화를 풀 수 없을 때는 FALSE를 반환하고 E_NOTICE가 발생합니다.

변경점

버전 | 설명
4.2.0 | unserialize_callback_func 지시어를 사용할 수 있게 되었습니다.

예제

Example #1 unserialize() 예제

<?php
// 여기서는, 데이터베이스에서 가져온 문자열로부터 $session_data
// 배열에 세션 데이터를 읽어들일 때 unserialize()를 사용합니다.
// 이 예제는 serialize()에 설명된 예제를 보완합니다.

$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn, "SELECT data FROM sessions WHERE id = ?");
$sqldata = array($_SERVER['PHP_AUTH_USER']);
if (!odbc_execute($stmt, &$sqldata) || !odbc_fetch_into($stmt, &$tmp)) {
    // excute나 fetch가 실패하면, 빈 배열로 초기화합니다.
    $session_data = array();
} else {
    // 여기서는 일렬화된 데이터를 $tmp[0]에 가집니다.
    $session_data = unserialize($tmp[0]);
    if (!is_array($session_data)) {
        // 무언가 잘못되면, 빈 배열로 초기화합니다.
        $session_data = array();
    }
}
?>

Example #2 unserialize_callback_func 예제

<?php
$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';

// unserialize_callback_func 지시어는 PHP 4.2.0부터 사용할 수 있습니다.
ini_set('unserialize_callback_func', 'mycallback'); // 콜백 함수를 설정

function mycallback($classname)
{
    // 단지 클래스를 정의하는 파일을 포함합니다.
    // 어떤 클래스 정의가 필요한가는 $classname에서 얻을 수 있습니다.
}
?>

주의

Warning

오류가 발생했을 때와 일렬화된 FALSE 값을 복구했을 때, 둘 다 FALSE를 반환합니다. str를 serialize(false)와 비교하거나, E_NOTICE 발생을 확인해서 이러한 특별한 경우를 처리할 수 있습니다.

참고

  • serialize() - 값의 저장 표현을 생성

User Contributed Notes

m.m.j.kronenburg 07-Sep-2016 06:52
You can use the following code to use the php 7 unserialize function in php 5.3 and upwards. This adds the $option argument.

<?php

namespace
{

/**
 * PHP 7 unserialize function for PHP 5.3 upwards.
 * Added the $option argument (allowed_classes).
 * See php unserialize manual for more detail.
 **/
function php7_unserialize($str, $options = array())
{
  if(version_compare(PHP_VERSION, '7.0.0', '>='))
  { return unserialize($str, $options); }

  $allowed_classes = isset($options['allowed_classes']) ?
    $options['allowed_classes'] : true;
  if(is_array($allowed_classes) || !$allowed_classes)
  {
    $str = preg_replace_callback(
      '/(?=^|:)(O|C):\d+:"([^"]*)":(\d+):{/',
      function($matches) use ($allowed_classes)
      {
        if(is_array($allowed_classes) &&
          in_array($matches[2], $allowed_classes))
        { return $matches[0]; }
        else
        {
          return $matches[1].':22:"__PHP_Incomplete_Class":'.
            ($matches[3] + 1).
            ':{s:27:"__PHP_Incomplete_Class_Name";'.
            serialize($matches[2]);
        }
      },
      $str
    );
  }
  unset($allowed_classes);
  return unserialize($str);
}

} // namespace

namespace my_name_space
{
  /**
   * Use the new php7 unserialize in your namespace without
   * renaming all unserialize(...) function calls to
   * php7_unserialize(...).
   **/
  function unserialize($str, $options = array())
  { return php7_unserialize($str, $options); }
}

?>
smilesrg at gmail dot com 27-Aug-2015 02:28
I faced with error when serializing/deserializing an object. The error looks like
Notice: unserialize(): Error at offset 2 of 52 bytes in file.php on line 130

and found solution here: http://davidwalsh.name/php-serialize-unserialize-issues

The safe way to serialize and unserialize:

//to safely serialize
$serialized = base64_encode(serialize($var));

//to unserialize...
$unserialized = unserialize(base64_decode($var));
chris at pollett dot org 03-Jul-2015 12:28
When you serialize an object of a class from a particular namespace, the namespace is recorded as part of the serialization. If you decide to change this namespace's name, it can be hard to read in old serialized objects. I.e., suppose you had serialized an object of type foo\A, you change the namespace of your project to goo but otherwise leave the class definition of A unchanged. You would like to be able to unserialize the object as goo\A, instead unserialization will only create a partial object. To fix this in the case where you don't have nested objects in your class definition, you can use the following simple rename function:
/**
 * Used to change the namespace of a serialized php object (assumes doesn't
 * have nested subobjects)
 *
 * @param string $class_name new fully qualified name with namespace
 * @param string $object_string serialized object
 *
 * @return string serialized object with new name
 */
function renameSerializedObject($class_name, $object_string)
{
    /*  number of digits in the length of name of the object needs to be
        less than 12 digits (probably more like 4) for this to work.
    */
    $name_length = intval(substr($object_string, 2, 14));
    $name_space_info_length = strlen("O:".$name_length.":") +
        $name_length + 2; // 2 for quotes;
    $object_string = 'O:' .
        strlen($class_name) . ':"'. $class_name.'"' .
        substr($object_string, $name_space_info_length);
    return $object_string;
}
ErnestV 03-Aug-2013 10:05
Just a note - if the serialized string contains a reference to a class that cannot be instantiated (e.g. being abstract) PHP will immediately die with a fatal error. If the unserialize() statement is preceded with a '@' to avoid cluttering the logs with warns or notices there will be absolutely no clue as to why the script stopped working. Cost me a couple of hours...
Ray.Paseur often uses Gmail 26-Jul-2013 10:41
In the Classes and Objects docs, there is this: In order to be able to unserialize() an object, the class of that object needs to be defined.

Prior to PHP 5.3, this was not an issue.  But after PHP 5.3 an object made by SimpleXML_Load_String() cannot be serialized.  An attempt to do so will result in a run-time failure, throwing an exception.  If you store such an object in $_SESSION, you will get a post-execution error that says this:

Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in [no active file]:0 Stack trace: #0 {main} thrown in [no active file] on line 0

The entire contents of the session will be lost.  Hope this saves someone some time!

<?php // RAY_temp_ser.php
error_reporting(E_ALL);
session_start();
var_dump($_SESSION);
$_SESSION['hello'] = 'World';
var_dump($_SESSION);

// AN XML STRING FOR TEST DATA
$xml = '<?xml version="1.0"?>
<families>
  <parent>
    <child index="1" value="Category 1">Child One</child>
  </parent>
</families>';

// MAKE AN OBJECT (GIVES SimpleXMLElement)
$obj = SimpleXML_Load_String($xml);

// STORE THE OBJECT IN THE SESSION
$_SESSION['obj'] = $obj;
suman dot jis at gmail dot com 13-Mar-2012 06:12
I was getting unserialize()  Error at offset error.

If you face similar problem  then use the following procedure

$auctionDetails = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $dataArr[$i]['auction_details'] );
$auctionDetails = unserialize($auctionDetails);
frank at interactinet dot com 30-Aug-2011 06:32
When dealing with sessions, try session_decode($data) instead of unserialize($data).
walf 23-Aug-2011 05:22
a replacement for unserialize that returns whether it worked and populates the unserialized variable by reference:
<?php
function funserialize($serialized, &$into) {
    static $sfalse;
    if ($sfalse === null)
        $sfalse = serialize(false);
    $into = @unserialize($serialized);
    return $into !== false || rtrim($serialized) === $sfalse;//whitespace at end of serialized var is ignored by PHP
}

$s_foo = 'b:0;';
var_dump(funserialize($s_foo, $foo), $foo);

$s_bar = 'bar';
var_dump(funserialize($s_bar, $bar), $bar);

$s_foo = 'a:0:{};';
var_dump(funserialize($s_foo, $foo), $foo);

?>
gives:

bool(true)
bool(false)

bool(false)
bool(false)

bool(true)
array(0) {
}
MBa 02-Aug-2011 12:49
To check if a string is serialized:

$blSerialized=(@unserialize($sText)||$sText=='b:0;');
chris at colourlovers dot com 17-Jun-2011 02:31
Anyone having trouble serializing data with SimpleXMLElement objects stored within it, check this out:

This will traverse $data looking for any children which are instances of SimpleXMLElement, and will run ->asXML() on them, turning them into a string and making them serializable. Other data will be left alone.

<?php
function exportNestedSimpleXML($data) {
    if (is_scalar($data) === false) {
        foreach ($data as $k => $v) {
            if ($v instanceof SimpleXMLElement) {
                $v = str_replace("&#13;","\r",$v->asXML());
            } else {
                $v = exportNestedSimpleXML($v);
            }

            if (is_array($data)) {
                $data[$k] = $v;
            } else if (is_object($data)) {
                $data->$k = $v;
            }
        }
    }

    return $data;
}

$data = array (
    "baz" => array (
        "foo" => new stdClass(),
        "int" => 123,
        "str" => "asdf",
        "bar" => new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><foo>bar</foo>'),
    )
);

var_dump($data);
/*array(1) {
  ["baz"]=>
  array(4) {
    ["foo"]=>
    object(stdClass)#3 (0) {
    }
    ["int"]=>
    int(123)
    ["str"]=>
    string(4) "asdf"
    ["bar"]=>
    object(SimpleXMLElement)#4 (1) {
      [0]=>
      string(3) "bar"
    }
  }
}*/

var_dump(exportNestedSimpleXML($data));
/*array(1) {
  ["baz"]=>
  array(4) {
    ["foo"]=>
    object(stdClass)#3 (0) {
    }
    ["int"]=>
    int(123)
    ["str"]=>
    string(4) "asdf"
    ["bar"]=>
    string(54) "<?xml version="1.0" encoding="UTF-8"?>
<foo>bar</foo>
"
  }
}
*/
?>
daniel at fourstaples dot com 24-Mar-2010 05:46
Here's a simple function to get the class of a serialized string (that is, the type of object that will be returned if it's unserialized):

<?php
function get_serial_class($serial) {
    $types = array('s' => 'string', 'a' => 'array', 'b' => 'bool', 'i' => 'int', 'd' => 'float', 'N;' => 'NULL');

    $parts = explode(':', $serial, 4);
    return isset($types[$parts[0]]) ? $types[$parts[0]] : trim($parts[2], '"');
}
?>

I use this when saving a serialized object to a cookie, to make sure it is the right type when I go to unserialize it.

The type names are the same format/case as you would see if you did a var_dump().
Fagzal 09-Mar-2010 07:10
To all who have problem with quoting and slashes when storing serialized data in MySQL: you are probably doing it wrong.

Use e.g. PDO with placeholders and the blob column type, and it will Just Work.
arbie samong 10-Sep-2009 04:42
__PHP_Incomplete_Class Object Demystified

1. First take note of the output. A simple example:

__PHP_Incomplete_Class Object (
[__PHP_Incomplete_Class_Name] => SomeObject1
[obj1property1] => somevalue1 [obj1property2] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => SomeObject2 [obj2property1] => somevalue1 [obj2property2] => Array (
['key1'] => somevalue3, ['key2'] => somevalue4 ) ) )

2. We analyze this and break it down.
__PHP_Incomplete_Class Object tells you there is an object that needs to be declared somehow.
__PHP_Incomplete_Class_Name simply tells you the expected class name. It is just one of the properties for now.

So we have:
a) an unknown object that has a class name SomeObject1 (first class)
b) it has 2 properties, namely obj1property1 and obj2property2
c) obj2property2 is itself an object whose class name is SomeObject2 (the second class)
d) SomeObject2 has two properties, obj2property1 and obj2property2
e) obj2property2 is an array that contains two elements

3. Now that we have an idea of the structure, we shall create class definitions based from it. We will just create properties for now, methods are not required as a minimum.

<?php
class SomeObject1 {
        public $obj1property1;
        public $obj1property2;
}
class SomeObject2 {
        public $obj2property1;
        public $obj2property2;
}
?>

4. Have that accessible to your script and it will solve the __PHP_Incomplete_Class Object problem as far as the output is concerned. Now you will have:

SomeObject1 ( [obj1property1] => somevalue1 [obj1property2] => SomeObject2 ( [obj2property1] => somevalue1 [obj2property2] => Array ( ['key1'] => somevalue3, ['key2'] => somevalue4 ) ) )

As you will notice, __PHP_Incomplete_Class Object is gone and replaced by the class name. The property __PHP_Incomplete_Class_Name is also removed.

5. As for the array property obj2property2, we can directly access that and just assume that it is an array and loop through it:

<?php

// this will be SomeObject1
$data = unserialize($serialized_data);

// this will be SomeObject2
$data2 = $data->obj1property2();

foreach($data2->obj2property2 as $key => $value):
         print $key.' : '. $value .'<br>';
endforeach;

?>

Outputs:
key1 : somevalue3
key2 : somevalue4

That's it. You can add more methods on the class declarations for the given properties, provided you keep your original output as basis for the data types.
w dot laurencine at teknoa dot net 07-Apr-2009 02:54
When dealing with a string which contain "\r", it seems that the length is not evaluated correctly. The following solves the problem for me :

<?php
// remove the \r caracters from the $unserialized string
$unserialized = str_replace("\r","",$unserialized);

// and then unserialize()
unserialize($unserialized);
?>
chris AT cmbuckley DOT co DOT uk 14-Aug-2008 03:47
As mentioned in the notes, unserialize returns false in the event of an error and for boolean false. Here is the first solution mentioned, without using error handling:

<?php
function isSerialized($str) {
    return ($str == serialize(false) || @unserialize($str) !== false);
}

var_dump(isSerialized('s:6:"foobar";')); // bool(true)
var_dump(isSerialized('foobar'));        // bool(false)
var_dump(isSerialized('b:0;'));          // bool(true)
?>
Ates Goral 04-May-2007 08:02
If instead of using JSON, you'd like to stick with PHP-style serialization, here's some JavaScript code I posted at http://magnetiq.com for serializing JavaScript objects in PHP fashion:

/* Returns the class name of the argument or undefined if
   it's not a valid JavaScript object.
*/
function getObjectClass(obj)
{
    if (obj && obj.constructor && obj.constructor.toString)
    {
        var arr = obj.constructor.toString().match(
            /function\s*(\w+)/);

        if (arr && arr.length == 2)
        {
            return arr[1];
        }
    }

    return undefined;
}

/* Serializes the given argument, PHP-style.

   The type mapping is as follows:

   JavaScript Type    PHP Type
   ---------------    --------
   Number             Integer or Decimal
   String             String
   Boolean            Boolean
   Array              Array
   Object             Object
   undefined          Null

   The special JavaScript object null also becomes PHP Null.
   This function may not handle associative arrays or array
   objects with additional properties well.
*/
function phpSerialize(val)
{
    switch (typeof(val))
    {
    case "number":
        return (Math.floor(val) == val ? "i" : "d") + ":" +
            val + ";";
    case "string":
        return "s:" + val.length + ":\"" + val + "\";";
    case "boolean":
        return "b:" + (val ? "1" : "0") + ";";
    case "object":
        if (val == null)
        {
            return "N;";
        }
        else if ("length" in val)
        {
            var idxobj = { idx: -1 };

            return "a:" + val.length + ":{" + val.map(
                function (item)
                {
                    this.idx++;

                    var ser = phpSerialize(item);

                    return ser ?
                        phpSerialize(this.idx) + ser :
                        false;
                }, idxobj).filter(
                function (item)
                {
                    return item;
                }).join("") + "}";
        }
        else
        {
            var class_name = getObjectClass(val);

            if (class_name == undefined)
            {
                return false;
            }

            var props = new Array();

            for (var prop in val)
            {
                var ser = phpSerialize(val[prop]);

                if (ser)
                {
                    props.push(phpSerialize(prop) + ser);
                }
            }
            return "O:" + class_name.length + ":\"" +
                class_name + "\":" + props.length + ":{" +
                props.join("") + "}";
        }
    case "undefined":
        return "N;";
    }

    return false;
}

On the client side, you can pass in a complex (nested) JavaScript object to the phpSerialize function to get a PHP-style serialized representation. This string can be posted back and directly passed to the unserialize function to yield a representation of the complex object in PHP realm. Use of this technique requires caution on security matters.
BenBE at omorphia dot de 02-Mar-2007 03:20
When trying to serialize or unserialize recursive arrays or otherwise linked data you might find the undocumented R data type quite useful.

If you want a array like the one produced with
<?
$a = array();
$a[0] =& $a;
?>
serialized you can store it using a string simular to this one:
<?
$a = unserialize("a:1:{i:0;R:1;}");
?>

Both sources will make $a hold an array that self-references itself in index 0.

The argument for R is the index of the created sub-variable of the serialize-string beginning with 1.
double at dumpit dot com 19-Dec-2006 02:09
This little function will check whether the serialized string is well formed.

PHP < 6 because i'd heard changes will be made in this php-intern function,
maybe it could be edited easy for it.

<?php

function wd_check_serialization( $string, &$errmsg )
{

    $str = 's';
    $array = 'a';
    $integer = 'i';
    $any = '[^}]*?';
    $count = '\d+';
    $content = '"(?:\\\";|.)*?";';
    $open_tag = '\{';
    $close_tag = '\}';
    $parameter = "($str|$array|$integer|$any):($count)" . "(?:[:]($open_tag|$content)|[;])";
    $preg = "/$parameter|($close_tag)/";
    if( !preg_match_all( $preg, $string, $matches ) )
    {
        $errmsg = 'not a serialized string';
        return false;
    }
    $open_arrays = 0;
    foreach( $matches[1] AS $key => $value )
    {
        if( !empty( $value ) && ( $value != $array xor $value != $str xor $value != $integer ) )
        {
            $errmsg = 'undefined datatype';
            return false;
        }
        if( $value == $array )
        {
            $open_arrays++;
            if( $matches[3][$key] != '{' )
            {
                $errmsg = 'open tag expected';
                return false;
            }
        }
        if( $value == '' )
        {
            if( $matches[4][$key] != '}' )
            {
                $errmsg = 'close tag expected';
                return false;
            }
            $open_arrays--;
        }
        if( $value == $str )
        {
            $aVar = ltrim( $matches[3][$key], '"' );
            $aVar = rtrim( $aVar, '";' );
            if( strlen( $aVar ) != $matches[2][$key] )
            {
                $errmsg = 'stringlen for string not match';
                return false;
            }
        }
        if( $value == $integer )
        {
            if( !empty( $matches[3][$key] ) )
            {
                $errmsg = 'unexpected data';
                return false;
            }
            if( !is_integer( (int)$matches[2][$key] ) )
            {
                $errmsg = 'integer expected';
                return false;
            }
        }
    }
    if( $open_arrays != 0 )
    {
        $errmsg = 'wrong setted arrays';
        return false;
    }
    return true;
}

?>
Are Pedersen 09-Aug-2006 10:46
Be aware that if useing serialize/unserialize in a serverfarm with both 32bit and 64bit servers you can get unexpected results.

Ex: if you serialize an integer with value of 2147483648 on a 64bit system and then unserialize it on a 32bit system you will get the value -2147483648 instead. This is because an integer on 32bit cannot be above 2147483647 so it wraps.
chad 0x40 herballure 0x2e com 05-May-2006 06:08
When unserializing in PHP5 (behavior observed with 5.1.2), __autoload() will be checked first, and unserialize_callback_func called only if __autoload failed to load the class definition.
martin dot goldinger at netserver dot ch 15-Aug-2005 01:48
When you use sessions, its very important to keep the sessiondata small, due to low performance with unserialize. Every class shoud extend from this class. The result will be, that no null Values are written to the sessiondata. It will increase performance.

<?
class BaseObject
{
    function __sleep()
    {
        $vars = (array)$this;
        foreach ($vars as $key => $val)
        {
            if (is_null($val))
            {
                unset($vars[$key]);
            }
        }
        return array_keys($vars);
    }
};
?>
Chris Hayes (chris at hypersites dot com) 23-Oct-2004 06:27
In reply to the earlier post about having to include object definitions *before* using unserialize.  There is a workaround for this.

When an object is serialized, the first bit of the string is actually the name of the class.  When an unknown object is unserialized, this is maintained as a property.  So if you serialize it again, you get back the exact same string as if you'd serialized the original object.  Basically, to cut to the point...

If you use

$_SESSION['my_object'] = unserialize(serialize($_SESSION['my_object']))

then you get back an object of the correct type, even if the session had originally loaded it as an object of type stdClass.
aderyn at nowhere dot tld 30-Oct-2003 07:02
A quick note:
If you store a serialized object in a session, you have to include the class _before_ you initialize (session_start()) the session.


주어진 변수를 제거

  • ≪ unserialize
  • var_dump ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 주어진 변수를 제거

unset

(PHP 4, PHP 5, PHP 7)

unset — 주어진 변수를 제거

설명

void unset ( mixed $var [, mixed $var [, mixed $... ]] )

unset()은 주어진 변수를 파괴합니다.

함수 안에서 unset()의 행동은 어떠한 변수를 제거하려 하는지에 따라 달라집니다.

함수 안에서 전역 변수를 unset()하면, 로컬 변수만 파괴됩니다. 호출한 환경에서 변수는 unset()을 호출하기 전과 같은 값을 유지합니다.

<?php
function destroy_foo()
{
    global $foo;
    unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo;
?>

위 예제의 출력:

bar

전역 변수를 함수 안에서 unset()하려면, $GLOBALS 배열을 사용할 수 있습니다:

<?php
function foo()
{
    unset($GLOBALS['bar']);
}

$bar = "something";
foo();
?>

함수 안에서 *참조로 넘겨진* 변수를 unset()하면, 로컬 변수만이 파괴됩니다. 호출한 함수에서 변수는 unset()을 호출하기 전과 동일한 값을 유지합니다.

<?php
function foo(&$bar)
{
    unset($bar);
    $bar = "blah";
}

$bar = 'something';
echo "$bar\n";

foo($bar);
echo "$bar\n";
?>

위 예제의 출력:

something
something

함수 안에서 정적 변수를 unset()하면, unset()은 함수의 나머지 문맥에서만 변수를 제거합니다. 이어지는 호출은 변수의 이전 값을 복구합니다.

<?php
function foo() {
    static $bar;
    $bar++;
    echo "Before unset: $bar, ";
    unset($bar);
    $bar = 23;
    echo "after unset: $bar\n";
}

foo();
foo();
foo();
?>

위 예제의 출력:

Befor unset: 1, after unset: 23
Befor unset: 2, after unset: 23
Befor unset: 3, after unset: 23

인수

var

제거할 변수

var

다른 변수 ..

...

반환값

값을 반환하지 않습니다.

변경점

버전 | 설명
4.0.0 | unset()은 표현이 되었습니다. (PHP 3에서, unset()은 항상 1을 반환했습니다)

예제

Example #1 unset() 예제

<?php
// 하나의 변수 파괴
unset($foo);

// 배열의 한 원소 파괴
unset($bar['quux']);

// 하나 이상의 변수 파괴
unset($foo1, $foo2, $foo3);
?>

Example #2 (unset) 캐스팅 사용하기

<?php
$name = 'Felipe';

var_dump((unset) $name);
?>

주의

Note: 이것은 함수가 아닌 언어 구조이기 때문에, 가변 함수 방식으로 호출할 수 없습니다.

Note:

현 문맥에서 보이는 객체 프로퍼티도 제거할 수 있습니다.

Note:

PHP 5부터 객체 메쏘드 안에서 $this를 제거할 수 없습니다.

참고

  • isset() - 설정된 변수인지 확인
  • empty() - 변수가 비어있는지 검사합니다
  • array_splice() - 배열의 일부를 삭제하고, 그 위치를 다른 내용으로 대체

User Contributed Notes

Phantasma_9 at yahoo dot com 09-Aug-2016 03:17
This is probably trivial but there is no error for unsetting a non-existing variable.
magnesium dot oxide dot play+php at gmail dot com 20-Mar-2014 05:10
You can unset superglobals like $GLOBALS, $_GET etc., but causing an unususal behavior (as of PHP 5.3.3).

1) unsetting of superglobals is done globally, i.e. unsetting inside the function affects GLOBALLY.

2) Recreation of unset'ed superglobals can be done (recreated valiables are superglobals), but original functionality (in $GLOBALS, $_SESSION ...) has lost.

<?php

function foo(){
  unset($GLOBALS);
}

function bar(){
  var_dump($GLOBALS);
}

foo();
bar(); //issues E_NOTICE ($GLOBALS not defined)
$GLOBALS=3;
bar(); //displays int(3)

?>
Anonymous 30-Sep-2013 12:18
This is a bug or i missing something?
<?php
$a = function() use (&$a){
  unset($a); // all good?
};

$b = function() use (&$b){
  $b = null; // Fatal error: Cannot destroy active lambda function
};

var_dump($a(), $b());

?>
morfeu_gd at hotmail dot com 11-Apr-2013 01:07
To unset entire object/array recursively:

<?php
function limpar_vetor_objeto(&$elemento){
    static $nivel_recursao=-1;
    $nivel_recursao++;
    if(is_array($elemento)){
        foreach($elemento as $key => $valor){
            print_r(str_repeat("\t", $nivel_recursao)."Deletando do vetor o elemento $key\r\n");
            limpar_vetor_objeto($valor);
            unset($valor);
        }
        unset($elemento);
    }
    else if(is_object($elemento)){
        foreach($elemento as $key => $valor){
            print_r(str_repeat("\t", $nivel_recursao)."Deletando do objeto a propriedade $key\r\n");
            limpar_vetor_objeto($valor);
            unset($valor);
        }
        unset($elemento);
    }
    $nivel_recursao--;
}
?>
Hayley Watson 06-Mar-2013 06:41
You don't need to check that a variable is set before you unset it.
<?php
unset($a);
?>
is harmless.
<?php
if(isset($a)) {
    unset($a);
}
?>
is pointless complication.

This doesn't apply to properties of objects that have __isset() methods that visibly change object state or __unset() methods that don't properly check their arguments or have extra side effects.

The latter case means that __unset shouldn't do more than what it says on the tin, and also has the responsibility for checking (possibly using __isset()) that what it's being asked to do makes sense.

The former case is just plain bad design.
tigercat at aol dot com 10-Oct-2012 04:55
The combination of "global" and "unset" in functions can lead to some unexpected results.  This is because the "global" function creates a reference to a variable at the time it's executed, so a variable can be deleted out from under a "global $my_variable" declaration in a function.  Accessing data that's been deleted with an obsolete reference is usually a bad thing; in some languages it can generate a machine address fault.

<?php
$my_global_var = "old data";
f1();

function f1() // example of invalid variable reference use
{
  global $my_global_var;   // creates reference to global variable
  f2(); // recreates global variable, so reference is now invalid

// bad...
  echo $my_global_var;     // outputs "old data" (from invalid memory???)

// good...
  global $my_global_var;  // reestablish reference to new global variable
  echo $my_global_var;     // outputs "new data" as expected
}

function f2() // recreate global variable
{
  unset($GLOBALS['my_global_var']); // this syntax works with all variable types including arrays
  global $my_global_var; // must do this after unset to access new global variable
  $my_global_var = "new data";
}
?>
pfreilly at umd dot umich dot edu 30-Sep-2011 01:56
Despite much searching, I have not yet found an explanation as to how one can manually free resources from variables, not so much objects, in PHP.  I have also seen many comments regarding the merits and demerits of unset() versus setting a variable to null.  Thus, here are the results of some benchmarks performed comparing unset() of numerous variables to setting them to null (with regards to memory usage and processing time):

10 variables:
Unset:
Memory Usage: 296
Time Elapsed: 1.0013580322266E-5

Null set:
Memory Usage: 1736
Time Elapsed: 5.9604644775391E-6

50 variables:
Unset:
Memory Usage: 296
Time Elapsed: 3.6001205444336E-5

Null set:
Memory Usage: 8328
Time Elapsed: 3.2901763916016E-5

100 variables:
Unset:
Memory Usage: 296
Time Elapsed: 5.6982040405273E-5

Null set:
Memory Usage: 15928
Time Elapsed: 5.8174133300781E-5

1000 variables:
Unset:
Memory Usage: 296
Time Elapsed: 0.00041294097900391

Null set:
Memory Usage: 168096
Time Elapsed: 0.00067591667175293

10000 variables:
Unset:
Memory Usage: 296
Time Elapsed: 0.0042569637298584

Null set:
Memory Usage: 1650848
Time Elapsed: 0.0076930522918701

100000 variables:
Unset:
Memory Usage: 296
Time Elapsed: 0.042603969573975

Null set:
Memory Usage: 16249080
Time Elapsed: 0.087724924087524

300000 variables:
Unset:
Memory Usage: 296
Time Elapsed: 0.13177299499512

Null set:
Memory Usage: 49796320
Time Elapsed: 0.28617882728577

Perhaps my test code for the null set was flawed, but despite that possibility it is simple to see that unset() has minimal processing time impact, and no apparent memory usage impact (unless the values returned by memory_get_usage() are flawed).  If you truly care about the ~4 microseconds saved over <50 variables, more power to you.  Otherwise, use unset() to minimize script impact on your system.
Note: Tested on PHP 5.3.8 installed via RPM on Fedora 14
Metasansana 22-May-2011 06:19
A simple function to unset unwanted keys in an array:

<?php

        $a = array("A"=>1, "B"=>2, "C"=>"a");

        foreach($a as $key=>&$value){
            if(is_string($value){
              unset($a[$key]);
            }
         }

        print_r($a);

?>

This should print:  A=1 B=2.
edouard dot berge at gmail dot com 24-Sep-2010 06:09
Warning!

When unset from an array, if you unset all elements, the array is always set

$tab=array('A'=>1,'B'=>2);
unset($tab['A']);
unset($tab['B']);
echo isset($tab)." ".count($tab);

output: 1 0
Andreas 24-Sep-2010 02:51
You can not unset a numeric key of an array, if key is a string. See this example:

// Create a simple array with 3 different key types
$test[1] = array(
    10 => array('apples'),
    "20" => array('bananas'),
    '30' => array('peaches')
);
$test[2] = (array) json_decode(json_encode($test[1]));
$test[3] = (array) (object) $test[1];
// array form a stdClass object
$testClass = new stdClass();
$testClass->{10} =  array('apples');
$testClass->{"20"} =  array('bananas');
$test[4] = (array) $testClass[6];

echo "<pre>";
foreach($test as $testNum => $arr) {

    echo "\nTest: " . $testNum  . " \n";
    var_dump($arr);

    foreach($arr as $key => $fruit) {
        echo "key: " . $key . "\n";
        echo "key exists: ";
        var_dump(array_key_exists(strval($key), $arr));
        echo "typeof key is: " . gettype($key) . "\n";

        unset($arr[$key]);
    }
    var_dump($arr);
    echo "\n" . str_repeat("-", 80);
}
echo "</pre>";

And here is the output:

Test: 1
array(3) {
  [10]=>
  array(1) {
    [0]=>
    string(6) "apples"
  }
  [20]=>
  array(1) {
    [0]=>
    string(7) "bananas"
  }
  [30]=>
  array(1) {
    [0]=>
    string(7) "peaches"
  }
}
key: 10
key exists: bool(true)
typeof key is: integer
key: 20
key exists: bool(true)
typeof key is: integer
key: 30
key exists: bool(true)
typeof key is: integer
array(0) {
}

--------------------------------------------------------------
Test: 2
array(3) {
  ["10"]=>
  array(1) {
    [0]=>
    string(6) "apples"
  }
  ["20"]=>
  array(1) {
    [0]=>
    string(7) "bananas"
  }
  ["30"]=>
  array(1) {
    [0]=>
    string(7) "peaches"
  }
}
key: 10
key exists: bool(false)
typeof key is: string
key: 20
key exists: bool(false)
typeof key is: string
key: 30
key exists: bool(false)
typeof key is: string
array(3) {
  ["10"]=>
  array(1) {
    [0]=>
    string(6) "apples"
  }
  ["20"]=>
  array(1) {
    [0]=>
    string(7) "bananas"
  }
  ["30"]=>
  array(1) {
    [0]=>
    string(7) "peaches"
  }
}

--------------------------------------------------------------
Test: 3
array(3) {
  [10]=>
  array(1) {
    [0]=>
    string(6) "apples"
  }
  [20]=>
  array(1) {
    [0]=>
    string(7) "bananas"
  }
  [30]=>
  array(1) {
    [0]=>
    string(7) "peaches"
  }
}
key: 10
key exists: bool(true)
typeof key is: integer
key: 20
key exists: bool(true)
typeof key is: integer
key: 30
key exists: bool(true)
typeof key is: integer
array(0) {
}

--------------------------------------------------------------
Test: 4
array(2) {
  ["10"]=>
  array(1) {
    [0]=>
    string(6) "apples"
  }
  ["20"]=>
  array(1) {
    [0]=>
    string(7) "bananas"
  }
}
key: 10
key exists: bool(false)
typeof key is: string
key: 20
key exists: bool(false)
typeof key is: string
array(2) {
  ["10"]=>
  array(1) {
    [0]=>
    string(6) "apples"
  }
  ["20"]=>
  array(1) {
    [0]=>
    string(7) "bananas"
  }
}

--------------------------------------------------------------

Fix the problem with a rebuild of the array:
$oldArray = $array();
$array = array();
foreach($oldArray as $key => $item) {
    $array[intval($key)] = $item;
}
j dot vd dot merwe at enovision dot net 26-Aug-2010 02:08
A sample how to unset array elements from an array result coming from a mysql request. In this sample it is checking if a file exists and removes the row from the array if it not exists.

<?php
$db->set_query("select * from documents where document_in_user = 0"); //1
$documents = $db->result_to_array($db->get_result()); //1

foreach ($documents as $key => $row) { //2

    $file     = "uploads/".rawurldecode($row['document_name']);

    if ( file_exists ( $file ) == FALSE ) {
         unset($documents[$key]);  //3
    }
}

$documents = array_values($documents); // reindex the array (4)
?>

variables:
mysql table = documents,
array = $documents
array key (index) = $key
array row (record sort of speak) = $row

explanation:

1.
it gets the array from the table (mysql)

2.
foreach goes through the array $documents

3.
unset if record does not exist

4.
the array_values($documents) reindexes the $documents array, for otherwise you might end up in trouble when your  process will start expecting an array starting with key ($key) 0 (zero).
Anonymous 02-Jul-2010 04:20
further I realized that an object, when getting detroyed, does care about destroying variable in object space visibility but not those in local visibility, be aware of the found pattern:

<?php
class release_test{
  private $buffer;
  private $other_object;
  public function __construct(){
    $this->other_object=new other_object_class();
  }
  public function __destruct(){
    //note that you always have to unset class objects, in order to get the resources released
    unset($this->other_object);
  }
  public allocate_mem_A(){
    $this->buffer=file("/tmp/bigfile");
  }
  public allocate_mem_B(){
    $buffer=file("/tmp/bigfile");
  }
  public allocate_mem_C(){
    $buffer=file("/tmp/bigfile");
    unset($buffer);
  }
  public allocate_mem_D(){
    $this->other_buffer=file("/tmp/bigfile");
  }
}

//this does not lead to a resource problem
$A=new release_test();
$A->allocate_mem_A();
$A->__destruct();
unset($A);

//this DOES lead to a resource problem
$B=new release_test();
$B->allocate_mem_B();
$B->__destruct();
unset($B);

//this does not lead to a resource problem
$C=new release_test();
$C->allocate_mem_C();
$C->__destruct();
unset($C);

//this does not lead to a resource problem
$D=new release_test();
$D->allocate_mem_D();
$D->__destruct();
unset($D);
?>
alex at bartl dot net 01-Jul-2010 07:11
unset($class_object) does not release resources allocated by the object. If used in loops, which create and destroy objects, that might easily lead to a resource problem. Explicitly call the destructor to circumvent the problem.

<?php
//this causes a resource problem
for($f=0;$f<10000;$f++){
  $my_class_object=new my_class_name();
  unset($my_class_object);
}

//this releases the resources allocate by the object
for($f=0;$f<10000;$f++){
  $my_class_object=new my_class_name();
  $my_class_object->__destruct();
  unset($my_class_object);
}
?>
phpmanual at kennel17 dot co dot uk 26-Feb-2010 05:55
Note that PHP 4 will generate a warning if you try to unset an array index that doesn't exist and whose parent doesn't exist.

Example:

<?php

  $foo = array();

  unset($foo['Bar']['Baz']);

?>

RESULT: "Notice:  Undefined index:  Bar"

On PHP5 no error is raised, which seems to me like the correct behaviour.

Note that using unset($foo['Bar']) in the above example does not generate a warning in either version.

(Tested on 4.4.9 and 5.2.4)
obsidianproject.co.uk 10-Sep-2009 03:41
A quick note on (unset) vs unset().

<?php
$a="test";
unset($a);
var_dump($a);
?>
will return

Notice: Undefined variable: a
NULL

<?php
$a="test";
var_dump((unset) $a);
?>
will return

NULL

When using unset() php will throw an E_NOTICE if you try and access the variable after it has been unset, (unset) won't.

This has been tested in PHP 5.3.0
David Sawyer, Metaface Development 24-Apr-2009 06:26
Here's a simple function to remove a variable from an HTTP Query String:

<?php

function unset_query_string_var($varname,$query_string) {
    $query_array = array();
    parse_str($query_string,$query_array);
    unset($query_array[$varname]);
    $query_string = http_build_query($query_array);
    return $query_string;
}

?>

----------------------------

Example Usage #1:
Let's say you want to remove the variable "fruit" from $page_url below...

<?php
$page_url = "http://www.example.com/script.php?fruit=apple&color=red";
$url_data = parse_url($page_url);
$query_string = unset_query_string_var("fruit",$url_data['query']);
$page_url_new = $url_data['scheme'].'://'.$url_data['host'].$url_data['path'];
if (!empty($query_string)) $page_url_new .= '?'.$query_string;
$page_url_new .= $url_data['fragment'];
print $page_url_new;
?>

Output:
http://www.example.com/script.php?color=red

----------------------------

Example Usage #2:
To remove the variable "q" from the query string of the current page, try something like...

<?php

$page_url = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER['PHP_SELF'];
$query_string = unset_query_string_var("q",$_SERVER["QUERY_STRING"]);
if (!empty($query_string)) $page_url .= '?'.$query_string;

?>
macnimble at gmail dot com 26-Mar-2009 11:39
Two ways of unsetting values within an array:

<?php
# remove by key:
function array_remove_key ()
{
  $args  = func_get_args();
  return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
# remove by value:
function array_remove_value ()
{
  $args = func_get_args();
  return array_diff($args[0],array_slice($args,1));
}

$fruit_inventory = array(
  'apples' => 52,
  'bananas' => 78,
  'peaches' => 'out of season',
  'pears' => 'out of season',
  'oranges' => 'no longer sold',
  'carrots' => 15,
  'beets' => 15,
);

echo "<pre>Original Array:\n",
     print_r($fruit_inventory,TRUE),
     '</pre>';

# For example, beets and carrots are not fruits...
$fruit_inventory = array_remove_key($fruit_inventory,
                                    "beets",
                                    "carrots");
echo "<pre>Array after key removal:\n",
     print_r($fruit_inventory,TRUE),
     '</pre>';

# Let's also remove 'out of season' and 'no longer sold' fruit...
$fruit_inventory = array_remove_value($fruit_inventory,
                                      "out of season",
                                      "no longer sold");
echo "<pre>Array after value removal:\n",
     print_r($fruit_inventory,TRUE),
     '</pre>';
?>
lion_cat at mail ru 12-Nov-2008 11:22
about unset for arrays

if you unset the last array member
$ar[0]==2
$ar[1]==7
$ar[2]==9

unset ($ar[2])

after addition a new member by $ar[]=7,

you will get
$ar[0]==2
$ar[1]==7
$ar[3]==7,

So, unset has no effect to internal array counter!!!
thijs NOSPACE putman at gmail dot com 05-Nov-2008 02:17
"It is possible to unset even object properties visible in current context"

Although the note is clear, in my case it required a little example to grasp its full impact:

If your object uses overloaded properties (using the "__set()" magic method) and you unset one of your "real" properties, the "__set()" method will take over if you try to assign a value to this property again.
In most cases, the "__set()" method is very much limited in the properties it can set. In my case, I only allow "__set()" to change a limited, predefined, set of properties. Most of them through their "public" name instead of their actual name...

This generates a somewhat counter-intuitive situation: If you unset a variable, you expect its value to become "null", which remains true. You also expect to be able to set it again to another value.
In the example described above this is not the case: The "__get()" method is invoked and, in my case, doesn't allow the property to be set again!
nox at oreigon dot de 05-Aug-2008 09:24
if you try to unset an object, please be careful about references.

Objects will only free their resources and trigger their __destruct method when *all* references are unsetted.
Even when they are *in* the object... sigh!

<?php

class A {
  function __destruct() {
    echo "cYa later!!\n";
  }
}

$a = new A();
$a -> a = $a;
#unset($a); # Just uncomment, and you'll see

echo "No Message ... hm, what now?\n";
unset($a -> a);
unset($a);

echo "Finally that thing is gone\n";

?>

Of course the object completely dies at the end of the script.
stacionari at gmail dot com 17-Mar-2008 10:41
Sometimes you need to assigne values to an array index in some loop (if, while, foreach etc.) but you wish to set starting index key to some number greater then zero (lets say 5). One idea how to do this is:

<?php
    $values = array(5, 10, 15, 100);  //array of values that we wish to add to our new array

    $myArray = array(4=>0);   //sets starting key to be 4 and assigns some value (lets say 0)
    unset($myArray[4]);   //delete this index key, but preserves further enumeration

    foreach($values as $value){
        $myArray[] = $value;   //asign values to our array
    }

    print_r($myArray);

/* Output:

Array ( [5] => 5 [6] => 10 [7] => 15 [8] => 100 )

*/

?>
pauljamescampbell at gmail dot com 15-Mar-2008 02:22
Here's my variation on the slightly dull unset method. It throws in a bit of 80's Stallone action spice into the mix. Enjoy!

<?php
/**
 * function rambo (first blood)
 *
 * Completely and utterly destroys everything, returning the kill count of victims
 *
 * @param    It don't matter, it's Rambo baby
 * @return    Integer    Body count (but any less than 500 and it's not really worth mentioning)
 */
function rambo() {

    // Get the victims and initiate that body count status
    $victims = func_get_args();
    $body_count = 0;

    // Kill those damn punks
    foreach($victims as $victim) {
        if($death_and_suffering = @unset($victim)) {
            $body_count++;
        }
    }

    // How many kills did Rambo tally up on this mission?
    return($body_count);
}
?>
Kai Kunstmann 10-Mar-2008 02:09
Since unset() is a language construct, it cannot be passed anything other than a variable. It's sole purpose is to "unset" this variable, ie. to remove it from the current scope and destroy it's associated data. This is true especially for reference variables, where not the actual value is destroyed but the reference to that value. This is why you can't wrap 'unset()' in a user defined function: You would either unset a copy of the data if the parameter is passed by value, or you would just unset the reference variable within the functions scope if the parameter is passed by reference. There is no workaround for that, as you cannot pass 'scope' to a function in PHP. Such a function can only work for variables that exist in a common or global scope (compare 'unset($_GLOBALS[variable])').

I don't know how PHP handles garbage collection internally, but I guess this behavior can result in a huge memory leak: if a value variable goes out of scope with a second variable still holding a reference to the in-memory value, then unsetting that reference would still hold the value in memory but potentially unset the last reference to that in-memory data, hence: occupied memory that is rendered useless as you cannot reference it anymore.
levitating at gmail dot com 07-Jan-2008 01:19
>> shame, but it doesn't seem to pop the stack.

There is a simple solution to that. Delete the value, then use array_merge() on your array:

<?php $folders= array_merge($folders) ; ?>
Maresa 16-Oct-2007 02:41
Note that since unset() returns void, you can't do this:

isset($some_var) && unset($some_var);

You'll get: Parse error: syntax error, unexpected T_UNSET in ...

The reason is because it would parse as
<bool> && <void>;
which is not a valid PHP statement.

Thus, the only alternative is to actually use if statement.
gerry+phpnet at buzka dot com 20-Sep-2007 06:07
Quote from http://fr.php.net/session_unset

"Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal."

So basically don't do:
unset($_SESSION)

Instead do:
$_SESSION = array();
Hayley Watson 24-Aug-2007 06:02
In regard to some confusion earlier in these notes about what causes unset() to trigger notices when unsetting variables that don't exist....

Unsetting variables that don't exist, as in
<?php
unset($undefinedVariable);
?>
does not trigger an "Undefined variable" notice. But
<?php
unset($undefinedArray[$undefinedKey]);
?>
triggers two notices, because this code is for unsetting an element of an array; neither $undefinedArray nor $undefinedKey are themselves being unset, they're merely being used to locate what should be unset. After all, if they did exist, you'd still expect them to both be around afterwards. You would NOT want your entire array to disappear just because you unset() one of its elements!
Sinured 17-Jul-2007 06:08
The main difference between <?php unset($var);?> and <?php $var = null;?> is, that unset() will reset the state to $var to something like "not set at all".

<?php
// register_globals = Off
error_reporting(E_ALL);
echo $var; // Notice

$var = null;
echo $var; // Nothing

unset($var);
echo $var; // Notice
?>
chad 0x40 herballure 0x2e com 31-May-2007 04:11
It is observed on PHP 5.1.6 that <?php unset($this); ?> inside of a method will remove the reference to $this in that method. $this isn't considered "special" as far as unset() is concerned.
RQuadling at GMail dot com 28-Mar-2007 02:28
If you want to remove a value from an array, then there is no direct mechanism.

The following function uses the array_keys() function to find the key(s) of the value that you want to remove and then removes the elements for that key.

I've also given some examples and the output.

<?php
/**
  * array array_remove ( array input, mixed search_value [, bool strict] )
  **/
function array_remove(array &$a_Input, $m_SearchValue, $b_Strict = False) {
    $a_Keys = array_keys($a_Input, $m_SearchValue, $b_Strict);
    foreach($a_Keys as $s_Key) {
        unset($a_Input[$s_Key]);
    }
    return $a_Input;
}
?>

Beside scalar variables (integers, floats, strings, boolean), you can also use arrays as the values you want to remove.

<?php
// Results in array(8, 8.0, '8', '8.0')
array_remove(array(8, 8.0, '8', '8.0', array(8), array('8')), array(8));

// Results in array(8, 8.0, '8', '8.0', array('8'))
array_remove(array(8, 8.0, '8', '8.0', array(8), array('8')), array(8), True);
?>
09-Feb-2007 05:10
Just to confirm, USING UNSET CAN DESTROY AN ENTIRE ARRAY. I couldn't find reference to this anywhere so I decided to write this.

The difference between using unset and using $myarray=array(); to unset is that obviously the array will just be overwritten and will still exist.

<?php

$myarray=array("Hello","World");

echo $myarray[0].$myarray[1];

unset($myarray);
//$myarray=array();

echo $myarray[0].$myarray[1];

echo $myarray;
?>

Output with unset is:
<?
HelloWorld

Notice: Undefined offset: 0 in C:\webpages\dainsider\myarray.php on line 10

Notice: Undefined offset: 1 in C:\webpages\dainsider\myarray.php on line 10

Output with $myarray=array(); is:
?>

<?
HelloWorld

Notice: Undefined offset: 0 in C:\webpages\dainsider\myarray.php on line 10

Notice: Undefined offset: 1 in C:\webpages\dainsider\myarray.php on line 10

Array
?>
hessodreamy at gmail dot com 17-Jan-2007 12:51
To clarify what hugo dot dworak at gmail dot com said about unsetting things that aren't already set:

unsetting a non-existent key within an array does NOT throw an error.
<?
$array = array();

unset($array[2]);
//this does not throw an error

unset($array[$undefinedVar]);
//Throws an error because of the undefined variable, not because of a non-existent key.
?>
dibakar dot datta at gmail dot com 01-Apr-2006 08:31
Instead of using the unset function  for unregistering your session or other array values you can also do this samll feature and get this task done with just 1 line code.

Suppose, if you like to unregister your session store values.
You can use:

      $_SESSION = array();

Well this syntax saves lot's of time instead of unsetting each values.
hugo dot dworak at gmail dot com 26-Dec-2005 03:23
If one tries to unset a typical variable that does not exist, no errors, warning or noticies will occur. However, if one tries to unset a non-existent array or an array with non-existent key, this will result in a notice. For instance:

<?php
  $true = true;
  $array = array ();
  unset ($true, $undefinedVariable, $array [$undefinedKey], $undefinedArray [$undefinedKey]);
?>

The output is (PHP 5.0.5):

Notice: Undefined variable: undefinedKey
Notice: Undefined variable: undefinedKey
Notice: Undefined variable: undefinedArray
clark at everettsconsulting dot com 11-Sep-2005 09:50
In PHP 5.0.4, at least, one CAN unset array elements inside functions from arrays passed by reference to the function.
As implied by the manual, however, one can't unset the entire array by passing it by reference.

<?php
function remove_variable (&$variable)  // pass variable by reference
{
    unset($variable);
}

function remove_element (&$array, $key) // pass array by reference
{
    unset($array[$key]);
}

$scalar = 'Hello, there';
echo 'Value of $scalar is: ';
print_r ($scalar); echo '<br />';
// Value of $scalar is: Hello, there

remove_variable($scalar); // try to unset the variable
echo 'Value of $scalar is: ';
print_r ($scalar); echo '<br />';
// Value of $scalar is: Hello, there

$array = array('one' => 1, 'two' => 2, 'three' => 3);
echo 'Value of $array is: ';
print_r ($array); echo '<br />';
// Value of $array is: Array ( [one] => 1 [two] => 2 [three] => 3 )

remove_variable($array); // try to unset the array
echo 'Value of $array is: ';
print_r ($array); echo '<br />';
// Value of $array is: Array ( [one] => 1 [two] => 2 [three] => 3 )

remove_element($array, 'two'); // successfully remove an element from the array
echo 'Value of $array is: ';
print_r ($array); echo '<br />';
// Value of $array is: Array ( [one] => 1 [three] => 3 )

?>
no at spam dot com 31-Aug-2005 12:22
In addition to what timo dot hummel at 4fb dot de said;

>For the curious: unset also frees memory of the variable used.
>
>It might be possible that the in-memory size of the PHP Interpreter isn't reduced, but your scripts won't touch the memory_limit boundary. Memory is reused if you declare new variables.

It might be worth adding that functions apparently don't free up memory on exit the same way unset does..
Maybe this is common knowledge, but although functions destroys variables on exit, it (apparently) doesn't help the memory.

So if you use huge variables inside functions, be sure to unset them if you can before returning from the function.

In my case, if I did not unset before return, then the script would use 20 MB more of memory than if I did unset.
This was tested with php 5.0.4 on apache 2 on windows xp, with no memory limit.

Before I did the test, I was under the impression that when you exit from functions, the memory used inside it would be cleared and reused. Maybe this should be made clear in the manual, for either unset() or in the chapter for functions.
muhamad_zakaria at yahoo dot com 05-Jul-2005 04:08
We have experienced when we applied 'unset' to the overloaded properties (PHP5), consider the code below:
<?php
    class TheObj {
        public $RealVar1, $RealVar2, $RealVar3, $RealVar4;
        public $Var = array();

        function __set($var, $val) {
            $this->Var[$var] = $val;
        }
        function __get($var) {
            if(isset($this->Var[$var])) return $this->Var[$var];
            else return -1;
        }
    }

    $SomeObj = new TheObj;

    // here we set for real variables
    $SomeObj->RealVar1 = 'somevalue';
    $SomeObj->{'RealVar2'} = 'othervalue';
    $SomeObj->{'RealVar'.(3)} = 'othervaluetoo';
    $SomeObj->{'RealVar'.'4'} = 'anothervalue';

    // and here we set for virtual variables
    $SomeObj->Virtual1 = 'somevalue';
    $SomeObj->{'Virtual2'} = 'othervalue';
    $SomeObj->{'Virtual'.(3)} = 'othervaluetoo';
    $SomeObj->{'Virtual'.'4'} = 'anothervalue';

    // now we will try to unset these variables
    unset($SomeObj->RealVar1);
    unset($SomeObj->{'RealVar'.(3)});

    //the lines below will catch by '__get' magic method since these variables are unavailable anymore
    print $SomeObj->RealVar1."\n";
    print $SomeObj->{'RealVar'.(3)}."\n";

    // now we will try to unset these variables
    unset($SomeObj->Virtual1);
    unset($SomeObj->{'Virtual'.(3)});

    //but, these variables are still available??? eventhough they're "unset"-ed
    print $SomeObj->Virtual1."\n";
    print $SomeObj->{'Virtual'.(3)}."\n";
?>

Please note that PHP doesn't have magic callback to unset overloaded properties. This is the reason why unset($SomeObj->Virtual1) doesn't work.

But it does work when we set 'null' value such as the following code:
<?php
    // now we will set 'null' value instead of using unset statement
    $SomeObj->Virtual1 = null;
    $SomeObj->{'Virtual'.(3)} = null;

    // and now these variables are no longer available
    print $SomeObj->Virtual1."\n";
    print $SomeObj->{'Virtual'.(3)}."\n";
?>
Sound ugly, yeah?

This applied to the "virtual" array variable too, see more at http://bugs.php.net/bug.php?id=33513 (at feedback) about it.
PS: we used PHP version 5.1.0-dev from the CVS snapshot when we wrote the above codes.
franckraynal at free dot fr 26-Feb-2005 02:02
Here is another way to make 'unset' work with session variables from within a function :

<?php
function unsetSessionVariable ($sessionVariableName) {
   unset($GLOBALS[_SESSION][$sessionVariableName]);
}
?>

May it work with others than me...
F.
bedbin at gmail dot com 02-Feb-2005 03:33
usefull tip:
if you have session variables like these.
<?php
echo "<pre>";
$_SESSION["num"] = array(1,2,3,4);
var_dump($_SESSION);

echo "-<br>";
unset($_SESSION);
var_dump($_SESSION);
?>
gives out:

array(1) {
  ["num"]=>
  array(4) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
    [3]=>
    int(4)
  }
}
-
NULL

if you use empty instead unset you get same output as first var_dump($_SESSION) gives.
I hope help sb.
harycary at netscape dot net 14-Dec-2004 06:56
If you ever have to unset all the variables of a class from within a funciton of that class use the following code:

<?php

class User
{

     function User_login ( ... )
     {...}

     function User_logout ( $greeting )
     {

          ...
          foreach ( array_keys ( get_object_vars ( &$this ) ) as $val)
          {    unset( $this->$val );    }
          $this->greeting = $greeting;
          ...

     }

}

?>
If anyone knows of a more effective way please post a reply.
dan AT --nospam-- cubeland DOT co DOT uk 04-Nov-2004 08:38
dh at argosign dot de -
it is possible to unset globals from within functions thanks to the $GLOBALS array:

<?php
$x = 10;

function test() {
    // don't need to do ' global $x; '
    unset ($GLOBALS['x']);
    echo 'x: ' . $GLOBALS['x'] . '<br />';
}

test();
echo "x: $x<br />";

// will result in
/*
x:
x:
*/
?>
timo dot hummel at 4fb dot de 07-Sep-2004 03:24
For the curious: unset also frees memory of the variable used.

It might be possible that the in-memory size of the PHP Interpreter isn't reduced, but your scripts won't touch the memory_limit boundary. Memory is reused if you declare new variables.
thorry at thorry dot net 05-Aug-2004 11:15
The documentation is not entirely clear when it comes to static variables. It says:

If a static variable is unset() inside of a function, unset() destroys the variable and all its references.

<?php
function foo()
{
   static $a;
   $a++;
   echo "$a\n";
   unset($a);
}

foo();
foo();
foo();
?>

The above example would output:

1
2
3

And it does! But the variable is NOT deleted, that's why the value keeps on increasing, otherwise the output would be:

1
1
1

The references are destroyed within the function, this handeling is the same as with global variables, the difference is a static variable is a local variable.

Be carefull using unset and static values as the output may not be what you expect it to be. It appears to be impossible to destroy a static variable. You can only destroy the references within the current executing function, a successive static statement will restore the references.

The documentation would be better if it would say:
"If a static variable is unset() inside of a function, unset() destroys all references to the variable. "

Example: (tested PHP 4.3.7)
<?php
function foo()
{
   static $a;
   $a++;
   echo "$a\n";
   unset($a);
   echo "$a\n";
   static $a;
   echo "$a\n";
}

foo();
foo();
foo();
?>

Would output:

1

1
2

2
3

3
anon at no spam dot no address dot com 17-Jul-2004 06:19
Adding on to what bond at noellebond dot com said, if you want to remove an index from the end of the array, if you use unset, the next index value will still be what it would have been.

Eg you have
<?php
 $x = array(1, 2);

 for ($i = 0; $i < 5; $i++)
 {
    unset($x[(count($x)-1)]); //remove last set key in the array

    $x[] = $i;
 }
?>

You would expect:
Array([0] => 1, [1] => 4)
as you want it to remove the last set key....

but you actually get
Array ( [0] => 1 [4] => 2 [5] => 3 [6] => 4 )

This is since even though the last key is removed, the auto indexing still keeps its previous value.

The only time where this would not seem right is when you remove a value off the end. I guess different people would want it different ways.

The way around this is to use array_pop() instead of unset() as array_pop() refreshes the autoindexing thing for the array.
<?php
 $x = array(1, 2);

 for ($i = 0; $i < 5; $i++)
 {
    array_pop($x); // removes the last item in the array

    $x[] = $i;
 }
?>

 This returns the expected value of x = Array([0] => 1, [1] => 4);

Hope this helps someone who may need this for some odd reason, I did.
andre at twg dot com dot au 07-Mar-2004 06:16
Only This works with register_globals being 'ON'.

unset( $_SESSION['variable'] );

The above will not work with register_globals turned on (will only work outside of a function).

$variable = $_SESSION['variable'];
unset( $_SESSION['variable'], $variable );

The above will work with register_globals on & inside a function
warhog at warhog dot net 27-Jan-2004 05:52
you may wan't to unset all variables which are defined, here's one way:

<?php

function unset_all_vars($a)
{ foreach($a as $key => $val)
  { unset($GLOBALS[$key]); }
  return serialize($a); }

unset_all_vars(get_defined_vars());

?>

you can also save than a serialized var of the "memory" and perhaps store this in a temporary file.. very usefull if you work with text files and/or file uploads when you've got very large variables.

greetz
kdechant at midwestarts dot com 23-Nov-2003 12:47
As of PHP version 4.3.3, unset() results in a parse error if it is used with the @ error suppression operator.

For example:

@unset($var); // parse error
unset(@$var); // parse error
unset($var); // okay
frank at agentbrand dot com 09-Nov-2003 06:59
Use array_values() after unset() to reindex your array.
 Note that unset() removes the index as a key, you will need to reindex your array again to get expected behavior


변수에 대한 정보를 덤프

  • ≪ unset
  • var_export ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수에 대한 정보를 덤프

var_dump

(PHP 4, PHP 5, PHP 7)

var_dump — 변수에 대한 정보를 덤프

설명

void var_dump ( mixed $expression [, mixed $expression [, $... ]] )

이 함수는 형과 값을 포함하는 하나 이상의 표현을 구조화된 정보로 보여줍니다. 배열과 객체는 구조를 보여주기 위해서 재귀적으로 확장합니다.

PHP 5에서 객체의 모든 public, private, protected 프로퍼티를 출력에 반환합니다.

Tip

어떠한 결과가 브라우저로 직접 출력될 때, 출력 제어 함수를 이용해서 이 함수의 출력을 캡쳐하여, (예를 들면) string으로 저장할 수 있습니다.

인수

expression

내보낼 변수.

반환값

값을 반환하지 않습니다.

예제

Example #1 var_dump() 예제

<?php
$a = array (1, 2, array ("a", "b", "c"));
var_dump($a);
?>

위 예제의 출력:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  array(3) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
  }
}
<?php
$b = 3.1;
$c = true;
var_dump($b, $c);
?>

위 예제의 출력:

float(3.1)
bool(true)

참고

  • var_export() - 변수를 처리가능한 문자열 표현으로 출력하거나 반환합니다
  • print_r() - 변수에 관한 정보를 사람이 읽기 편하게 출력

User Contributed Notes

Anonymous 19-Jul-2016 03:27
Be careful this outputs to stdout stream (1) instead of the proper stderr stream (2).
bob 23-Mar-2016 07:37
I personally prefer a single standalone function called debug from github.com/hazardland/debug.php

It outputs html formatted dump for a complex objects/nested arrays with an expand/collapse buttons and gives ability to simply observe deep level data structures without depleting your brain resources or in a form of 4 space tab indented plain text which looks like this (but html mode is just a fully satisfying thing):

id : 13
name : "deposit"
class : "\paypal\deposit"
title : "Deposit"
system (cashier\system)
    name : "paypal"
    class : "\paypal\system"
    title : "PayPal"
    image : "cashier_system_image_paypal.png"
    methods : null
balance : ""
affect : 1
min (cashier\amount)
    currency : "USD"
    value : "1"
    origin : null
max (cashier\amount)
    currency : "USD"
    value : "100"
    origin : null
currencys (array)
    EUR : "EUR"
    USD : "USD"
    RUB : "RUB"
cashiers (array)
    2 : "2"
tax (cashier\tax)
    items (array)
        EUR (array)
            0 (array)
                from : 0
                to : 20
                value : 0.3
                percent : 0
        USD (array)
            0 (array)
                from : 0
                to : 20
                value : 0.3
                percent : 0
        RUB (array)
            0 (array)
                from : 0
                to : 1500
                value : 25
                percent : 0
active : true
order : 13
nikhazy dot akos at gmail dot com 21-Jul-2015 07:29
I use this function for smaller projects.

<?php
/*
Function by Akos Nikhazy
This function shows a formatted var_dump and backtrace of the line where it happens. Perfect for smaller projects for fast debugging.

@param mixed $v  -- variable to dump
@param bool $die -- it kills the script if true
*/
function varDump($v,$die = false)
{

                /*the var_dump formatted with pre*/
        echo '<pre>';
        var_dump($v);
        echo '</pre><hr>';

               /*backtrace if important as it gives even more info*/
                $backtrace = debug_backtrace();
        echo '<pre>';
        print_r($backtrace);
        echo '</pre>';

                /*stop here if needed*/
        if($die) die();

}
?>
Cyrus 05-Jun-2015 05:36
Here's a function to output var_dump to PHP's error log

<?php
function var_error_log( $object=null ){
    ob_start();                    // start buffer capture
    var_dump( $object );           // dump the values
    $contents = ob_get_contents(); // put the buffer into a variable
    ob_end_clean();                // end capture
    error_log( $contents );        // log contents of the result of var_dump( $object )
}
?>
b dot bergloev at gmail dot com 01-Nov-2014 08:45
I post a new var_dump function with colors and collapse features. It can also adapt to terminal output if you execute it from there. No need to wrap it in a pre tag to get it to work in browsers.

<?php
function dump_debug($input, $collapse=false) {
    $recursive = function($data, $level=0) use (&$recursive, $collapse) {
        global $argv;

        $isTerminal = isset($argv);

        if (!$isTerminal && $level == 0 && !defined("DUMP_DEBUG_SCRIPT")) {
            define("DUMP_DEBUG_SCRIPT", true);

            echo '<script language="Javascript">function toggleDisplay(id) {';
            echo 'var state = document.getElementById("container"+id).style.display;';
            echo 'document.getElementById("container"+id).style.display = state == "inline" ? "none" : "inline";';
            echo 'document.getElementById("plus"+id).style.display = state == "inline" ? "inline" : "none";';
            echo '}</script>'."\n";
        }

        $type = !is_string($data) && is_callable($data) ? "Callable" : ucfirst(gettype($data));
        $type_data = null;
        $type_color = null;
        $type_length = null;

        switch ($type) {
            case "String":
                $type_color = "green";
                $type_length = strlen($data);
                $type_data = "\"" . htmlentities($data) . "\""; break;

            case "Double":
            case "Float":
                $type = "Float";
                $type_color = "#0099c5";
                $type_length = strlen($data);
                $type_data = htmlentities($data); break;

            case "Integer":
                $type_color = "red";
                $type_length = strlen($data);
                $type_data = htmlentities($data); break;

            case "Boolean":
                $type_color = "#92008d";
                $type_length = strlen($data);
                $type_data = $data ? "TRUE" : "FALSE"; break;

            case "NULL":
                $type_length = 0; break;

            case "Array":
                $type_length = count($data);
        }

        if (in_array($type, array("Object", "Array"))) {
            $notEmpty = false;

            foreach($data as $key => $value) {
                if (!$notEmpty) {
                    $notEmpty = true;

                    if ($isTerminal) {
                        echo $type . ($type_length !== null ? "(" . $type_length . ")" : "")."\n";

                    } else {
                        $id = substr(md5(rand().":".$key.":".$level), 0, 8);

                        echo "<a href=\"javascript:toggleDisplay('". $id ."');\" style=\"text-decoration:none\">";
                        echo "<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span>";
                        echo "</a>";
                        echo "<span id=\"plus". $id ."\" style=\"display: " . ($collapse ? "inline" : "none") . ";\">&nbsp;&#10549;</span>";
                        echo "<div id=\"container". $id ."\" style=\"display: " . ($collapse ? "" : "inline") . ";\">";
                        echo "<br />";
                    }

                    for ($i=0; $i <= $level; $i++) {
                        echo $isTerminal ? "|    " : "<span style='color:black'>|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    }

                    echo $isTerminal ? "\n" : "<br />";
                }

                for ($i=0; $i <= $level; $i++) {
                    echo $isTerminal ? "|    " : "<span style='color:black'>|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                }

                echo $isTerminal ? "[" . $key . "] => " : "<span style='color:black'>[" . $key . "]&nbsp;=>&nbsp;</span>";

                call_user_func($recursive, $value, $level+1);
            }

            if ($notEmpty) {
                for ($i=0; $i <= $level; $i++) {
                    echo $isTerminal ? "|    " : "<span style='color:black'>|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                }

                if (!$isTerminal) {
                    echo "</div>";
                }

            } else {
                echo $isTerminal ?
                        $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "  " :
                        "<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span>&nbsp;&nbsp;";
            }

        } else {
            echo $isTerminal ?
                    $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "  " :
                    "<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span>&nbsp;&nbsp;";

            if ($type_data != null) {
                echo $isTerminal ? $type_data : "<span style='color:" . $type_color . "'>" . $type_data . "</span>";
            }
        }

        echo $isTerminal ? "\n" : "<br />";
    };

    call_user_func($recursive, $input);
}
?>
stlawson *AT* joyfulearthtech *DOT* com 10-Aug-2011 06:31
<?php
/**
 * Better GI than print_r or var_dump -- but, unlike var_dump, you can only dump one variable.
 * Added htmlentities on the var content before echo, so you see what is really there, and not the mark-up.
 *
 * Also, now the output is encased within a div block that sets the background color, font style, and left-justifies it
 * so it is not at the mercy of ambient styles.
 *
 * Inspired from:     PHP.net Contributions
 * Stolen from:       [highstrike at gmail dot com]
 * Modified by:       stlawson *AT* JoyfulEarthTech *DOT* com
 *
 * @param mixed $var  -- variable to dump
 * @param string $var_name  -- name of variable (optional) -- displayed in printout making it easier to sort out what variable is what in a complex output
 * @param string $indent -- used by internal recursive call (no known external value)
 * @param unknown_type $reference -- used by internal recursive call (no known external value)
 */
function do_dump(&$var, $var_name = NULL, $indent = NULL, $reference = NULL)
{
    $do_dump_indent = "<span style='color:#666666;'>|</span> &nbsp;&nbsp; ";
    $reference = $reference.$var_name;
    $keyvar = 'the_do_dump_recursion_protection_scheme'; $keyname = 'referenced_object_name';

    // So this is always visible and always left justified and readable
    echo "<div style='text-align:left; background-color:white; font: 100% monospace; color:black;'>";

    if (is_array($var) && isset($var[$keyvar]))
    {
        $real_var = &$var[$keyvar];
        $real_name = &$var[$keyname];
        $type = ucfirst(gettype($real_var));
        echo "$indent$var_name <span style='color:#666666'>$type</span> = <span style='color:#e87800;'>&amp;$real_name</span><br>";
    }
    else
    {
        $var = array($keyvar => $var, $keyname => $reference);
        $avar = &$var[$keyvar];

        $type = ucfirst(gettype($avar));
        if($type == "String") $type_color = "<span style='color:green'>";
        elseif($type == "Integer") $type_color = "<span style='color:red'>";
        elseif($type == "Double"){ $type_color = "<span style='color:#0099c5'>"; $type = "Float"; }
        elseif($type == "Boolean") $type_color = "<span style='color:#92008d'>";
        elseif($type == "NULL") $type_color = "<span style='color:black'>";

        if(is_array($avar))
        {
            $count = count($avar);
            echo "$indent" . ($var_name ? "$var_name => ":"") . "<span style='color:#666666'>$type ($count)</span><br>$indent(<br>";
            $keys = array_keys($avar);
            foreach($keys as $name)
            {
                $value = &$avar[$name];
                do_dump($value, "['$name']", $indent.$do_dump_indent, $reference);
            }
            echo "$indent)<br>";
        }
        elseif(is_object($avar))
        {
            echo "$indent$var_name <span style='color:#666666'>$type</span><br>$indent(<br>";
            foreach($avar as $name=>$value) do_dump($value, "$name", $indent.$do_dump_indent, $reference);
            echo "$indent)<br>";
        }
        elseif(is_int($avar)) echo "$indent$var_name = <span style='color:#666666'>$type(".strlen($avar).")</span> $type_color".htmlentities($avar)."</span><br>";
        elseif(is_string($avar)) echo "$indent$var_name = <span style='color:#666666'>$type(".strlen($avar).")</span> $type_color\"".htmlentities($avar)."\"</span><br>";
        elseif(is_float($avar)) echo "$indent$var_name = <span style='color:#666666'>$type(".strlen($avar).")</span> $type_color".htmlentities($avar)."</span><br>";
        elseif(is_bool($avar)) echo "$indent$var_name = <span style='color:#666666'>$type(".strlen($avar).")</span> $type_color".($avar == 1 ? "TRUE":"FALSE")."</span><br>";
        elseif(is_null($avar)) echo "$indent$var_name = <span style='color:#666666'>$type(".strlen($avar).")</span> {$type_color}NULL</span><br>";
        else echo "$indent$var_name = <span style='color:#666666'>$type(".strlen($avar).")</span> ".htmlentities($avar)."<br>";

        $var = $var[$keyvar];
    }

    echo "</div>";
}
?>
kobrasrealm at gmail dot com 23-Jun-2011 10:28
I wrote this dandy little function for using var_dump() on HTML documents so I don't have to view the source.

<?php
function htmldump($variable, $height="9em") {
echo "<pre style=\"border: 1px solid #000; height: {$height}; overflow: auto; margin: 0.5em;\">";
var_dump($variable);
echo "</pre>\n";
}
?>

You can pass arguments like this:
<?php
$sql = mysql_query("SELECT id, name, value FROM table WHERE cond = 'value'");
$s = mysql_fetch_assoc($sql);

// Dump variable containing the result of the MySQL query
var_dump($s);
?>

The second parameter lets you specify the height of the box. Default is 9em, but if you're expecting a huge output you'll probably want a higher value.

<?php
var_dump($s, "17em");
?>

Happy var_dumping.
flyfy1 at gmail dot com 22-Jun-2011 09:22
for output the var as string, include this function:
<?php
function var_dump_to_string($var){
    $output = "<pre>";
    _var_dump_to_string($var,$output);
    $output .= "</pre>";
    return $output;
}

function _var_dump_to_string($var,&$output,$prefix=""){
    foreach($var as $key=>$value){
        if(is_array($value)){
            $output.= $prefix.$key.": \n";
            _var_dump_to_string($value,$output,"  ".$prefix);
        } else{
            $output.= $prefix.$key.": ".$value."\n";
        }
    }
}
?>

 (say if you want to output $var to string $output)
<?
php $output = var_dump_to_string($var);
?>
divinity76 at gmail dot com 23-Apr-2011 03:53
a html-encoded var_dump

<?php
function htmlvardump(){ob_start(); $var = func_get_args(); call_user_func_array('var_dump', $var); echo htmentities(ob_get_clean());}
?>

(creds: ekneuss / mOo @ irc.freenode.net/#php )
geoff at spacevs dot com 08-Apr-2010 05:10
var_export does not return the same output as var_dump, if you want the var_dump output in a return you can wrap the call in ob_* functions.

<?PHP
  ob_start();
  var_dump($_SERVER);
  $dump = ob_get_clean();
?>
david at exposito dot mobi 07-Feb-2010 08:36
If you want to save exactly the content of an array into a variable to save ir later for example, use this:

<?php $xml = var_export($xml, true); ?>

You'll have in your same variable the content readable, instead of written down or anything else
ivolucien at gmail dot calm 06-Dec-2009 11:12
For the special case where you know that you are only dealing with arrays,
<?php string implode(string $glue, array $pieces) ?>
may be all that's necessary, and the $glue parameter can be used to insert placeholders if you need to do more complex formatting. Nested arrays and such are discussed on the implode() page.
egorinsk at gmail com 02-Feb-2009 01:08
Note that var_dump reset()'s array internal pointer!
rich dot schu at gmail dot com 10-Apr-2008 02:56
Keep in mind if you have xdebug installed it will limit the var_dump() output of array elements and object properties to 3 levels deep.

To change the default, edit your xdebug.ini file and add the folllowing line:
xdebug.var_display_max_depth=n

More information here:
http://www.xdebug.org/docs/display
highstrike at gmail dot com 08-Jan-2008 02:33
made 2 nifty functions based of what some people contributed here. Hope you find them usefull

usage ... call for the dump function. EG: dump($array, "Array dump");

<?php
////////////////////////////////////////////////////////
// Function:         dump
// Inspired from:     PHP.net Contributions
// Description: Helps with php debugging

function dump(&$var, $info = FALSE)
{
    $scope = false;
    $prefix = 'unique';
    $suffix = 'value';

    if($scope) $vals = $scope;
    else $vals = $GLOBALS;

    $old = $var;
    $var = $new = $prefix.rand().$suffix; $vname = FALSE;
    foreach($vals as $key => $val) if($val === $new) $vname = $key;
    $var = $old;

    echo "<pre style='margin: 0px 0px 10px 0px; display: block; background: white; color: black; font-family: Verdana; border: 1px solid #cccccc; padding: 5px; font-size: 10px; line-height: 13px;'>";
    if($info != FALSE) echo "<b style='color: red;'>$info:</b><br>";
    do_dump($var, '$'.$vname);
    echo "</pre>";
}

////////////////////////////////////////////////////////
// Function:         do_dump
// Inspired from:     PHP.net Contributions
// Description: Better GI than print_r or var_dump

function do_dump(&$var, $var_name = NULL, $indent = NULL, $reference = NULL)
{
    $do_dump_indent = "<span style='color:#eeeeee;'>|</span> &nbsp;&nbsp; ";
    $reference = $reference.$var_name;
    $keyvar = 'the_do_dump_recursion_protection_scheme'; $keyname = 'referenced_object_name';

    if (is_array($var) && isset($var[$keyvar]))
    {
        $real_var = &$var[$keyvar];
        $real_name = &$var[$keyname];
        $type = ucfirst(gettype($real_var));
        echo "$indent$var_name <span style='color:#a2a2a2'>$type</span> = <span style='color:#e87800;'>&amp;$real_name</span><br>";
    }
    else
    {
        $var = array($keyvar => $var, $keyname => $reference);
        $avar = &$var[$keyvar];

        $type = ucfirst(gettype($avar));
        if($type == "String") $type_color = "<span style='color:green'>";
        elseif($type == "Integer") $type_color = "<span style='color:red'>";
        elseif($type == "Double"){ $type_color = "<span style='color:#0099c5'>"; $type = "Float"; }
        elseif($type == "Boolean") $type_color = "<span style='color:#92008d'>";
        elseif($type == "NULL") $type_color = "<span style='color:black'>";

        if(is_array($avar))
        {
            $count = count($avar);
            echo "$indent" . ($var_name ? "$var_name => ":"") . "<span style='color:#a2a2a2'>$type ($count)</span><br>$indent(<br>";
            $keys = array_keys($avar);
            foreach($keys as $name)
            {
                $value = &$avar[$name];
                do_dump($value, "['$name']", $indent.$do_dump_indent, $reference);
            }
            echo "$indent)<br>";
        }
        elseif(is_object($avar))
        {
            echo "$indent$var_name <span style='color:#a2a2a2'>$type</span><br>$indent(<br>";
            foreach($avar as $name=>$value) do_dump($value, "$name", $indent.$do_dump_indent, $reference);
            echo "$indent)<br>";
        }
        elseif(is_int($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color$avar</span><br>";
        elseif(is_string($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color\"$avar\"</span><br>";
        elseif(is_float($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color$avar</span><br>";
        elseif(is_bool($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color".($avar == 1 ? "TRUE":"FALSE")."</span><br>";
        elseif(is_null($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> {$type_color}NULL</span><br>";
        else echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $avar<br>";

        $var = $var[$keyvar];
    }
}
?>
vladimir at pixeltomorrow dot com 10-Sep-2007 09:45
You can also use the PEAR package available at http://pear.php.net/package/Var_Dump

which parses the variable content in a very pleasant manner, a lot more easier to "follow" than the built-in var_dump() function.

Of course there are many others, but I prefer this one, because it's simply to use.

Just add at the begining of your file:

<?php

require('Var_Dump.php'); // make sure the pear package path is set in php.ini
Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'), array('mode' => 'normal','offset' => 4));
?>

then, instead of simply using var_dump($foo), use:

<?php

Var_Dump::display($foo);

?>

Read the documentation if you're looking for different output layouts.

Cheers!
Vladimir Ghetau
thriller dot ze at gmail dot com 21-Aug-2007 10:49
As Bryan said, it is possible to capture var_dump() output to a string. But it's not quite exact if the dumped variable contains HTML code.

You can use this instead:

<?php
echo '<pre>'; // This is for correct handling of newlines
ob_start();
var_dump($var);
$a=ob_get_contents();
ob_end_clean();
echo htmlspecialchars($a,ENT_QUOTES); // Escape every HTML special chars (especially > and < )
echo '</pre>';
?>
php at mikeboers dot com 28-Jun-2007 10:08
Howdy!

I am working on a pretty large project where I needed to dump a human readable form of whatever into the log files... and I thought var_export was too difficult to read. BigueNique at yahoo dot ca has a nice solution, although I needed to NOT modify whatever was being passed to dump.

So borrowing heavily from BigueNique's (just reworked his function) and someone's idea over in the object cloning page, I came up with the following function.

It makes a complete copy of whatever object you initially pass it, including all recursive definitions and outside objects references, then does the same thing as BigueNique's function. I also heavily reworked what it output, to suit my needs.

<?php

function var_log(&$varInput, $var_name='', $reference='', $method = '=', $sub = false) {

    static $output ;
    static $depth ;

    if ( $sub == false ) {
        $output = '' ;
        $depth = 0 ;
        $reference = $var_name ;
        $var = serialize( $varInput ) ;
        $var = unserialize( $var ) ;
    } else {
        ++$depth ;
        $var =& $varInput ;

    }

    // constants
    $nl = "\n" ;
    $block = 'a_big_recursion_protection_block';

    $c = $depth ;
    $indent = '' ;
    while( $c -- > 0 ) {
        $indent .= '|  ' ;
    }

    // if this has been parsed before
    if ( is_array($var) && isset($var[$block])) {

        $real =& $var[ $block ] ;
        $name =& $var[ 'name' ] ;
        $type = gettype( $real ) ;
        $output .= $indent.$var_name.' '.$method.'& '.($type=='array'?'Array':get_class($real)).' '.$name.$nl;

    // havent parsed this before
    } else {

        // insert recursion blocker
        $var = Array( $block => $var, 'name' => $reference );
        $theVar =& $var[ $block ] ;

        // print it out
        $type = gettype( $theVar ) ;
        switch( $type ) {

            case 'array' :
                $output .= $indent . $var_name . ' '.$method.' Array ('.$nl;
                $keys=array_keys($theVar);
                foreach($keys as $name) {
                    $value=&$theVar[$name];
                    var_log($value, $name, $reference.'["'.$name.'"]', '=', true);
                }
                $output .= $indent.')'.$nl;
                break ;

            case 'object' :
                $output .= $indent.$var_name.' = '.get_class($theVar).' {'.$nl;
                foreach($theVar as $name=>$value) {
                    var_log($value, $name, $reference.'->'.$name, '->', true);
                }
                $output .= $indent.'}'.$nl;
                break ;

            case 'string' :
                $output .= $indent . $var_name . ' '.$method.' "'.$theVar.'"'.$nl;
                break ;

            default :
                $output .= $indent . $var_name . ' '.$method.' ('.$type.') '.$theVar.$nl;
                break ;

        }

        // $var=$var[$block];

    }

    -- $depth ;

    if( $sub == false )
        return $output ;

}

// var_log( $var, '$name' ) ;

?>

Hope it works well for you!
Bryan 04-Jun-2007 06:38
Here's how you use output buffering to save var_dump to a string, as described in the Tip box.

<?php
ob_start();
var_dump($my_variable);
$my_string = ob_get_contents();
ob_end_clean();
?>
Zak Scott 24-May-2007 07:45
One of the most used approaches to this I have is:
<?php
echo "<pre>";
print_r($array); // or var_dump()
echo "</pre><br>";
?>

This is great for debugging purposes, no need for a long-winded debug class in most cases. Unless maybe you have a gigantic project that you didn't build yourself.
kaloyan at gmail dot com 26-Apr-2007 08:32
We can all agree that var_dump(); output is very spartan looking. The debug data needs to be organized better, and presented in a graceful way. In the era of Web 2.0 it is somewhat strange to use plain text to dump information. A DHTML powered informatiion dumping tool will be quite better - like the the open-source alternative of var_dump(); -- Krumo (http://krumo.sourceforge.net).

It renders the output using DHTML and collapsible nodes, it's layout is "skinable" and you can change it to fit your aesthetic taste. Krumo makes the output "human-readable" for real :) Plus it is compliant with both PHP4 and PHP5. Plus it detects "reference recursion". Plus you can use it to dump all various sort of data like debug back-traces, the superglobals ($_SERVER, $_ENV, $_REQUEST, $_COOKIE, $_GET, $_POST, $_SESSION), all the included files, all the declared classes, all the declared constants, all your PHP settings, all your php.ini values (if it is readable), all the loaded extensions, all the HTTP request headers, all the declared interfaces (for PHP5), all the file paths from INCLUDE_PATH, all the values of any particular INI file. Additionally it is designed to be easy to use - for example you can disable all the Krumo dumps instead of cleaning your code out of all print_r()'s and var_dump()'s. Anyway, if you check the site (http://krumo.sourceforge.net), you can found a lot of examples, demonstrations, documentation and all sort of helpful information.
thc at forestfactory dot de 06-Mar-2007 09:01
Well, like many others I was looking for a way to get nice dumps of variables. I think I'm about to solve this once and for all; or at least for PHP 5.1. Have a look at my project HLI. http://hli.forestfactory.de

It dumps and highlights not only Arrays and Object but also DOMDocument (as XML), DB results (as table), GD-Images (as PNG), DirectoryIterators (as table), Serialized Strings and many more.

It prints backtrace, object reflection (including PHPDoc) and the position of the dump. Dumps are movable, resizeable and foldable.

It is plugin-based, so its easy to write new dump-modes for other objects/resources. It generates XML output that is parsed by XSLT to HTML and will soon be able to be handled over AJAX (I will do this the next time I program some AJAX stuff).

It runs out of the box (tested with PHP 5.1 and PHP 5.2), but this is only a 0.1 release, so please tell me if something in wrong and I'll be happy to fix it.

Oh, and it's LGPL, so just use and enjoy it. I couldn't work without it any more.

Thomas
eric at ITCbrandt dot com swap ITC/brandt 31-Dec-2006 07:57
Very new to this and really appreciate the contributions of everyone.  I really rely on this site .  BigueNique inspired me with the last post.

I am writing an application that is WAY above my head and am buried in pages of variables and objects.  I really wanted an easier way to read them.

I had two problems - 1. The lists were too long; 2. It was hard to tell where one ended and the other began; 3.  By the time I saw my rendered forms, I forgot where my variable listing was. (Yeah, I can't count, get over it - now you know why this is so hard for me)

I had a variable dumping routine that worked better than the print_r, but elegant_dump was MUCH nicer.

I added a little bit of CSS and javascript to mine, and I have a really quick and easy way to display a colored title with a toggle control to expand or contract the variable structure and contents.

It might be too rudimentary for most of you guys, and I'm not answering anyone's question specifically, but perhaps this creative implementation will make someone's life a little easier.  At a minimum, It might be a good example of mixing in the different languages which was initially a big challenge for me.

simply call: dump_e($var,[$TracerMessage],[$ColorBackground],[$ColorContentBox]);

I have explained my functions as well as posted the required code to make it work here: http://tech.brandtitc.com/dump_e.php

It is a little too long to post in this venue.

Best Regards.
jonbarnett at gmail dot com 02-Oct-2006 09:25
dumping objects that reference each other could lead to infinite recursion
<?php
$brother = new Sibling();
$sister = new Sibling();
$brother->sister = $sister;
$sister->brother = $brother;

var_dump($brother);
/* dumps all of $brother's properties, including "sister", which dumps all of $sister's properties, including "brother", etc. */
?>
Storm 19-Aug-2006 07:12
ul_var_dump - dump $var to <ul><li></li></ul>

<?php

function ul_var_dump(&$var,$type=0)
{
    if(!$type)
        echo "<ul type='circle' style='border:1px solid #a0a0a0;padding-bottom:4px;padding-right:4px'>\n<li>";
    if(is_array($var))
    {
        echo "[array][".count($var)."]";
        echo "<ul type='circle' style='border:1px solid #a0a0a0;padding-bottom:4px;padding-right:4px'>\n";
        foreach($var as $k=>$v)
        {
            echo "<li>\"{$k}\"=>";
            ul_var_dump(&$v,1);
        }
        echo "</ul>\n";
    }
    else
        echo "[".gettype($var)."][{$var}]</li>\n";
    if(!$type)
        echo "</ul>\n";
}

?>
ospinto at hotmail dot com 07-Aug-2005 06:43
Just created this neat class that dumps a variable in a colored tabular structure similar to the cfdump tag in Coldfusion. Very easy to use and makes it so much easier to see the contents of variable. For examples and download, visit http://dbug.ospinto.com
edwardzyang at thewritingpot dot com 20-Mar-2005 11:06
If you're like me and uses var_dump whenever you're debugging, you might find these two "wrapper" functions helpful.

This one automatically adds the PRE tags around the var_dump output so you get nice formatted arrays.

<?php

function var_dump_pre($mixed = null) {
  echo '<pre>';
  var_dump($mixed);
  echo '</pre>';
  return null;
}

?>

This one returns the value of var_dump instead of outputting it.

<?php

function var_dump_ret($mixed = null) {
  ob_start();
  var_dump($mixed);
  $content = ob_get_contents();
  ob_end_clean();
  return $content;
}

?>

Fairly simple functions, but they're infinitely helpful (I use var_dump_pre() almost exclusively now).
anon 28-Jan-2005 03:31
var_dump(get_defined_vars());
will dump all defined variables to the browser.


변수를 처리가능한 문자열 표현으로 출력하거나 반환합니다

  • ≪ var_dump
  • 웹 서비스 ≫
  • PHP Manual
  • Variable handling 함수 목록
  • 변수를 처리가능한 문자열 표현으로 출력하거나 반환합니다

var_export

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

var_export — 변수를 처리가능한 문자열 표현으로 출력하거나 반환합니다

설명

mixed var_export ( mixed $expression [, bool $return ] )

var_export()는 주어진 변수에 대한 구조화된 정보를 얻습니다. 한 가지 차이를 제외하고, var_dump()와 동일합니다: 반환된 표현은 유효한 PHP 코드입니다.

인수

expression

내보내길 원하는 변수

return

TRUE로 설정하여 사용하면, var_export()는 변수 표현을 출력하는 대신에 반환합니다.

Note:

return 인수와 함께 사용할 때, 내부적으로 출력 버퍼링을 사용합니다. 그러므로, ob_start() 콜백 함수 안에서는 사용할 수 없습니다.

반환값

return 인수를 TRUE로 사용할 때 변수 표현을 반환합니다. 그 외에는 NULL을 반환합니다.

변경점

버전 | 설명
5.1.0 | __set_state 마법 메쏘드를 사용하여 클래스와 클래스를 가진 배열을 내보낼 수 있게 되었습니다.

예제

Example #1 var_export() 예제

<?php
$a = array (1, 2, array ("a", "b", "c"));
var_export($a);
?>

위 예제의 출력:

array (
  0 => 1,
  1 => 2,
  2 =>
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)
<?php
$b = 3.1;
$v = var_export($b, true);
echo $v;
?>

위 예제의 출력:

3.1

Example #2 PHP 5.1.0부터 클래스 내보내기

<?php
class A { public $var; }
$a = new A;
$a->var = 5;
var_export($a);
?>

위 예제의 출력:

A::__set_state(array(
    'var' => 5,
))

Example #3 __set_state 사용하기 (PHP 5.1.0부터)

<?php
class A
{
    public $var1;
    public $var2;

    public static function __set_state($an_array)
    {
        $obj = new A;
        $obj->var1 = $an_array['var1'];
        $obj->var2 = $an_array['var2'];
        return $obj;
    }
}

$a = new A;
$a->var1 = 5;
$a->var2 = 'foo';

eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array(
                                            //    'var1' => 5,
                                            //    'var2' => 'foo',
                                            // ));
var_dump($b);
?>

위 예제의 출력:

object (A)#2 (2) {
  ["var1"]=>
  int(5)
  ["var2"]=>
  string(3) "foo"
}

주의

Note:

이 함수로 resource형 변수는 내보낼 수 없습니다.

Note:

var_export()는 순환 참조를 다루지 못합니다. 처리 가능한 PHP 코드를 생성하는 것이 불가능에 가깝기 때문입니다. 배열이나 객체로 그러한 일을 하려면, serialize()를 사용하십시오.

참고

  • print_r() - 변수에 관한 정보를 사람이 읽기 편하게 출력
  • serialize() - 값의 저장 표현을 생성
  • var_dump() - 변수에 대한 정보를 덤프

User Contributed Notes

Mark P 30-Jan-2016 06:09
It doesn't appear to be documented, but the behaviour of `var_export()` changed in PHP 7.

Previously, `var_export(3.)` returned "3", now it returns "3.0".
cpmcgrat()uci ! edu 28-Jul-2015 07:45
When trying to use __set_state() to rebuild a huge, tricky class use the following:

class Foo
{
    public $a;
    public $b;
    public $c;
    public $d;
    public $e;
    public $f;
    public $g;
    public $h;
    public $i;

    public function __set_state($array)
    {
        $obj = new ArrayConfig;
        foreach($array as $k => $v) {
            eval('$obj->'.$k.' = '.$v.';');
        }
        return $obj;
    }
}

This will return a reconstructed version of the class without having to manually type each individual object in the class manually (as shown in the __set_state() example)
dan at coders dot co dot nz 10-Jan-2014 11:11
I found that my complex type was exporting with
  stdClass::__set_state()
in places. Not only was that strange and messy, it cannot be eval()-ed back in at all. Fatal error. Doh!

However a quick string-replace tidy-up of the result rendered it valid again.

    $macro = var_export($data, TRUE);
    $macro = str_replace("stdClass::__set_state", "(object)", $macro);
    $macro = '$data = ' . $macro . ';';

And now the string I output *can* be evaluated back in again.
wyattstorch42 at outlook dot com 23-Dec-2013 07:11
If you call var_export() on an instance of stdClass, it attempts to export it using ::__set_state(), which, for some reason, is not implemented in stdClass.

However, casting an associative array to an object usually produces the same effect (at least, it does in my case). So I wrote an improved_var_export() function to convert instances of stdClass to (object) array () calls. If you choose to export objects of any other class, I'd advise you to implement ::__set_state().

<?php
/**
 * An implementation of var_export() that is compatible with instances
 * of stdClass.
 * @param mixed $variable The variable you want to export
 * @param bool $return If used and set to true, improved_var_export()
 *     will return the variable representation instead of outputting it.
 * @return mixed|null Returns the variable representation when the
 *     return parameter is used and evaluates to TRUE. Otherwise, this
 *     function will return NULL.
 */
function improved_var_export ($variable, $return = false) {
    if ($variable instanceof stdClass) {
        $result = '(object) '.improved_var_export(get_object_vars($variable), true);
    } else if (is_array($variable)) {
        $array = array ();
        foreach ($variable as $key => $value) {
            $array[] = var_export($key, true).' => '.improved_var_export($value, true);
        }
        $result = 'array ('.implode(', ', $array).')';
    } else {
        $result = var_export($variable, true);
    }

    if (!$return) {
        print $result;
        return null;
    } else {
        return $result;
    }
}

// Example usage:
$obj = new stdClass;
$obj->test = 'abc';
$obj->other = 6.2;
$obj->arr = array (1, 2, 3);

improved_var_export((object) array (
    'prop1' => true,
    'prop2' => $obj,
    'assocArray' => array (
        'apple' => 'good',
        'orange' => 'great'
    )
));

/* Output:
(object) array ('prop1' => true, 'prop2' => (object) array ('test' => 'abc', 'other' => 6.2, 'arr' => array (0 => 1, 1 => 2, 2 => 3)), 'assocArray' => array ('apple' => 'good', 'orange' => 'great'))
*/
?>

Note: This function spits out a single line of code, which is useful to save in a cache file to include/eval. It isn't formatted for readability. If you want to print a readable version for debugging purposes, then I would suggest print_r() or var_dump().
chudinov at yahoo dot com 29-Nov-2013 06:55
Looks like since version 5.4.22 var_export uses the serialize_precision ini setting, rather than the precision one used for normal output of floating-point numbers.
As a consequence since version 5.4.22 for example var_export(1.1) will output 1.1000000000000001 (17 is default precision value) and not 1.1 as before.

<?php
//ouput 1.1000000000000001
var_export(1.1)
 ?>
NitPicker 11-Jul-2013 01:33
When it comes to HTML output (as discussed below), it's all fun and games until someone pokes their eye out with a "<".

Surround it with "<pre>", but do remember to wrap it in htmlspecialchars() as well.
sergei dot solomonov at gmail dot com 05-Oct-2012 01:00
<?php
$closure = function(){};

var_export($closure);

// output: Closure::__set_state(array())
?>
kexianbin at diyism dot com 16-Sep-2012 11:33
to use my_var_export(), it is as beautiful as var_export() and as could deal with recursive reference as print_r():

<?php
function my_var_export($var, $is_str=false)
         {$rtn=preg_replace(array('/Array\s+\(/', '/\[(\d+)\] => (.*)\n/', '/\[([^\d].*)\] => (.*)\n/'), array('array (', '\1 => \'\2\''."\n", '\'\1\' => \'\2\''."\n"), substr(print_r($var, true), 0, -1));
          $rtn=strtr($rtn, array("=> 'array ('"=>'=> array ('));
          $rtn=strtr($rtn, array(")\n\n"=>")\n"));
          $rtn=strtr($rtn, array("'\n"=>"',\n", ")\n"=>"),\n"));
          $rtn=preg_replace(array('/\n +/e'), array('strtr(\'\0\', array(\'    \'=>\'  \'))'), $rtn);
          $rtn=strtr($rtn, array(" Object',"=>" Object'<-"));
          if ($is_str)
             {return $rtn;
             }
          else
              {echo $rtn;
              }
         }

?>
jodybrabec at gmail dot com 28-Mar-2012 02:52
WORKAROUND for error "Nesting level too deep - recursive dependency":
ob_start();
var_dump($GLOBALS);
$dataDump = ob_get_clean();
echo $dataDump;
Anonymous 11-Nov-2011 06:13
There is an even simpler way to have clean output from var_export and print_r in html pages:

<?php
function pretty_var($myArray)
{
    echo "<pre>";
    var_export($myArray);
    echo "</pre>";
}
?>
laszlo dot heredy at gmail dot com 06-Oct-2010 10:57
Try this function instead of var_export($GLOBALS) or var_dump($GLOBALS) when all you want to know is the values of the variables you set on the current page.

<?php
function globalvars(){
    $result=array();
    $skip=array('GLOBALS','_ENV','HTTP_ENV_VARS',
                        '_POST','HTTP_POST_VARS','_GET',
                        'HTTP_GET_VARS',
                        '_COOKIE',
                        'HTTP_COOKIE_VARS','_SERVER',
                        'HTTP_SERVER_VARS',
                        '_FILES','HTTP_POST_FILES',
                        '_REQUEST','HTTP_SESSION_VARS',
                        '_SESSION');
    foreach($GLOBALS as $k=>$v)
        if(!in_array($k,$skip))
            $result[$k]=$v;
    return $result;
}//functionglobalvars

var_export(globalvars());
?>
4n4jmza02 at sneakemail dot com 18-Jun-2010 01:50
I learned the hard way that if var_export encounters a resource handle it exports it as "NULL", even if it is a valid handle. The documentation states that a handle cannot be exported, but it does not describe what happens if you try to do so anyway.

I had been using var_export in some debugging code while tracing a problem with a resource handle not being generated and ended up thinking that null handles were still being generated long after the problem had been fixed.
john dot risken at gmail dot com 17-Jan-2010 12:53
I didn't see this simple little item anywhere in the user notes. Maybe I'm blind!

Anyway, var_export and print_r both use spaces and carriage returns for formatting.  Sent to an html page, most of the formatting is lost. This simple function prints a nicely formatted array to an html screen:

<?php
function pretty_var($myArray){
    print str_replace(array("\n"," "),array("<br>","&nbsp;"), var_export($myArray,true))."<br>";
}
?>
rarioj at gmail dot com 29-Oct-2009 07:43
NOTE: If an object Foo has __set_state() method, but if that object contains another object Bar with no __set_state() method implemented, the resulting PHP expression will not be eval()-able.

This is an example (object Test that contains an instance of Exception).

<?php

class Test
{
  public $one;
  public $two;
  public function __construct($one, $two)
  {
    $this->one = $one;
    $this->two = $two;
  }
  public static function __set_state(array $array)
  {
    return new self($array['one'], $array['two']);
  }
}

$test = new Test('one', new Exception('test'));

$string = var_export($test, true);

/* $string =
Test::__set_state(array(
   'one' => 'one',
   'two' =>
  Exception::__set_state(array(
     'message' => 'test',
     'string' => '',
     'code' => 0,
     'file' => 'E:\\xampp\\htdocs\\test.Q.php',
     'line' => 35,
     'trace' =>
    array (
    ),
     'previous' => NULL,
  )),
))
*/

eval('$test2 = '.$string.';'); // Fatal error: Call to undefined method Exception::__set_state

?>

So avoid using var_export() on a complex array/object that contains other objects. Instead, use serialize() and unserialize() functions.

<?php

$string = 'unserialize('.var_export(serialize($test), true).')';

eval('$test2 = '.$string.';');

var_dump($test == $test2); // bool(true)

?>
ravenswd at gmail dot com 07-Jul-2009 07:32
(This replaces my note of 3-July-2009. The original version produced no output if a variable contained an empty array, or an array consisting only of empty arrays. For example, $bigarray['x'] = array(); Also, I have added a second version of the function.)

The output can be difficult to decipher when looking at an array with many levels and many elements on each level. For example:

<?php
print ('$bigarray = ' . var_export($bigarray, true) . "\n");
?>

will return:

$bigarray = array(
... (500 lines skipped) ...
          'mod' => 'charlie',

Whereas the routine below can be called with:

<?php
recursive_print ('$bigarray', $bigarray);
?>

and it will return:

$bigarray = array()
... (500 lines skipped) ...
$bigarray['foo']['bar']['0']['somethingelse']['mod'] = 'charlie'

Here's the function:

<?php
function recursive_print ($varname, $varval) {
  if (! is_array($varval)):
    print $varname . ' = ' . $varval . "<br>\n";
  else:
    print $varname . " = array()<br>\n";
    foreach ($varval as $key => $val):
      recursive_print ($varname . "['" . $key . "']", $val);
    endforeach;
  endif;
}
?>

For those who want a version that produces valid PHP code, use this version:

<?php
function recursive_print ($varname, $varval) {
  if (! is_array($varval)):
    print $varname . ' = ' . var_export($varval, true) . ";<br>\n";
  else:
    print $varname . " = array();<br>\n";
    foreach ($varval as $key => $val):
      recursive_print ($varname . "[" . var_export($key, true) . "]", $val);
    endforeach;
  endif;
}
?>

If your output is to a text file and not an HTML page, remove the <br>s.
ravenswd at gmail dot com 03-Jul-2009 02:21
The output can be difficult to decipher when looking at an array with many levels and many elements on each level. For example:

<?php
print ('$bigarray = ' . var_export($bigarray, true) . "\n");
?>

will return:

$bigarray = array(
... (500 lines skipped) ...
          'mod' => 'charlie',

Whereas the routine below can be called with:

<?php
recursive_print ('$bigarray', $bigarray);
?>

and it will return:

$bigarray['firstelement'] = 'something'
... (500 lines skipped) ...
$bigarray['foo']['bar']['0']['somethingelse']['mod'] = 'charlie'

Here's the function:

<?php
function recursive_print ($varname, $varval) {
  if (! is_array($varval)):
    print $varname . ' = ' . $varval . "<br>\n";
  else:
    foreach ($varval as $key => $val):
      recursive_print ($varname . "['" . $key . "']", $val);
    endforeach;
  endif;
}
?>
cmusicfan (at) gmail (daught) com 12-Jun-2009 06:19
Caution! var_export will add a backslash to single quotes (').

You may want to use stripslashes() to remove the mysteriously added backslashes.
stangelanda at arrowquick dot com 30-Jun-2007 01:20
I have been looking for the best method to store data in cache files.

First, I've identified two limitations of var_export verus serialize.  It can't store internal references inside of an array and it can't store a nested object or an array containing objects before PHP 5.1.0.

However, I could deal with both of those so I created a benchmark.  I used a single array containing from 10 to 150 indexes.  I've generate the elements' values randomly using booleans, nulls, integers, floats, and some nested arrays (the nested arrays are smaller averaging 5 elements but created similarly).  The largest percentage of elements are short strings around 10-15 characters.  While there is a small number of long strings (around 500 characters).

Benchmarking returned these results for 1000 * [total time] / [iterations (4000 in this case)]

serialize 3.656, 3.575, 3.68, 3.933, mean of 3.71
include 7.099, 5.42, 5.185, 6.076, mean of 5.95
eval 5.514, 5.204, 5.011, 5.788, mean of 5.38

Meaning serialize is around 1 and a half times faster than var_export for a single large array.  include and eval were consistently very close but eval was usually a few tenths faster (eval did better this particular set of trials than usual). An opcode cache like APC might make include faster, but otherwise serialize is the best choice.
Glen 24-May-2007 02:47
Like previously reported, i find var_export() frustrating when dealing with recursive structures.  Doing a :

<?php
var_export($GLOBALS);
?>

fails.  Interestingly, var_dump() has some logic to avoid recursive references.  So :

<?php
var_dump($GLOBALS);
?>

works (while being more ugly).  Unlike var_export(), var_dump() has no option to return the string, so output buffering logic is required if you want to direct the output.
Zorro 05-Sep-2005 08:24
This function can't export EVERYTHING. Moreover, you can have an error on an simple recursive array:

$test = array();
$test["oops"] = & $test;

echo var_export($test);

=>

Fatal error:  Nesting level too deep - recursive dependency? in ??.php on line 59
linus at flowingcreativity dot net 04-Jul-2005 06:50
<roman at DIESPAM dot feather dot org dot ru>, your function has inefficiencies and problems. I probably speak for everyone when I ask you to test code before you add to the manual.

Since the issue of whitespace only comes up when exporting arrays, you can use the original var_export() for all other variable types. This function does the job, and, from the outside, works the same as var_export().

<?php

function var_export_min($var, $return = false) {
    if (is_array($var)) {
        $toImplode = array();
        foreach ($var as $key => $value) {
            $toImplode[] = var_export($key, true).'=>'.var_export_min($value, true);
        }
        $code = 'array('.implode(',', $toImplode).')';
        if ($return) return $code;
        else echo $code;
    } else {
        return var_export($var, $return);
    }
}

?>
paul at worldwithoutwalls dot co dot uk 24-Nov-2004 08:22
var_export() differs from print_r() for variables that are resources, with print_r() being more useful if you are using the function for debugging purposes.
e.g.
<?php
$res = mysql_connect($dbhost, $dbuser, $dbpass);
print_r($res); //output: Resource id #14
var_export($res); //output: NULL
?>
php_manual_note at bigredspark dot com 16-Oct-2003 09:43
[john holmes]
True, but that method would require you to open and read the file into a variable and then unserialize it into another variable.

Using a file created with var_export() could simply be include()'d, which will be less code and faster.

[kaja]
If you are trying to find a way to temporarily save variables into some other file, check out serialize() and unserialize() instead - this one is more useful for its readable property, very handy while debugging.

[original post]
If you're like me, you're wondering why a function that outputs "correct PHP syntax" is useful. This function can be useful in implementing a cache system. You can var_export() the array into a variable and write it into a file. Writing a string such as

<?php
$string = '<?php $array = ' . $data . '; ?>';
?>

where $data is the output of var_export() can create a file that can be easily include()d back into the script to recreate $array.

The raw output of var_export() could also be eval()d to recreate the array.

---John Holmes...


웹 서비스

  • OAuth
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • OAuth 함수 목록
    • OAuth — The OAuth class
    • OAuthProvider — The OAuthProvider class
    • OAuthException — OAuthException class
  • SCA
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • SCA 함수 목록
  • SOAP
    • 소개
    • 설치/설정
    • 예약 상수
    • SOAP 함수 목록
    • SoapClient — The SoapClient class
    • SoapServer — The SoapServer class
    • SoapFault — The SoapFault class
    • SoapHeader — The SoapHeader class
    • SoapParam — The SoapParam class
    • SoapVar — The SoapVar class
  • Yar — Yet Another RPC Framework
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • Yar_Server — The Yar_Server class
    • Yar_Client — The Yar_Client class
    • Yar_Concurrent_Client — The Yar_Concurrent_Client class
    • Yar_Server_Exception — The Yar_Server_Exception class
    • Yar_Client_Exception — The Yar_Client_Exception class
  • XML-RPC
    • 소개
    • 설치/설정
    • 예약 상수
    • XML-RPC 함수 목록

User Contributed Notes

There are no user contributed notes for this page.


OAuth

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
    • FireEagle
  • OAuth 함수 목록
    • oauth_get_sbs — Generate a Signature Base String
    • oauth_urlencode — Encode a URI to RFC 3986
  • OAuth — The OAuth class
    • OAuth::__construct — Create a new OAuth object
    • OAuth::__destruct — The destructor
    • OAuth::disableDebug — Turn off verbose debugging
    • OAuth::disableRedirects — Turn off redirects
    • OAuth::disableSSLChecks — Turn off SSL checks
    • OAuth::enableDebug — Turn on verbose debugging
    • OAuth::enableRedirects — Turn on redirects
    • OAuth::enableSSLChecks — Turn on SSL checks
    • OAuth::fetch — Fetch an OAuth protected resource
    • OAuth::generateSignature — Generate a signature
    • OAuth::getAccessToken — Fetch an access token
    • OAuth::getCAPath — Gets CA information
    • OAuth::getLastResponse — Get the last response
    • OAuth::getLastResponseHeaders — Get headers for last response
    • OAuth::getLastResponseInfo — Get HTTP information about the last response
    • OAuth::getRequestHeader — Generate OAuth header string signature
    • OAuth::getRequestToken — Fetch a request token
    • OAuth::setAuthType — Set authorization type
    • OAuth::setCAPath — Set CA path and info
    • OAuth::setNonce — Set the nonce for subsequent requests
    • OAuth::setRequestEngine — The setRequestEngine purpose
    • OAuth::setRSACertificate — Set the RSA certificate
    • OAuth::setSSLChecks — Tweak specific SSL checks for requests.
    • OAuth::setTimestamp — Set the timestamp
    • OAuth::setToken — Sets the token and secret
    • OAuth::setVersion — Set the OAuth version
  • OAuthProvider — The OAuthProvider class
    • OAuthProvider::addRequiredParameter — Add required parameters
    • OAuthProvider::callconsumerHandler — Calls the consumerNonceHandler callback
    • OAuthProvider::callTimestampNonceHandler — Calls the timestampNonceHandler callback
    • OAuthProvider::calltokenHandler — Calls the tokenNonceHandler callback
    • OAuthProvider::checkOAuthRequest — Check an oauth request
    • OAuthProvider::__construct — Constructs a new OAuthProvider object
    • OAuthProvider::consumerHandler — Set the consumerHandler handler callback
    • OAuthProvider::generateToken — Generate a random token
    • OAuthProvider::is2LeggedEndpoint — is2LeggedEndpoint
    • OAuthProvider::isRequestTokenEndpoint — Sets isRequestTokenEndpoint
    • OAuthProvider::removeRequiredParameter — Remove a required parameter
    • OAuthProvider::reportProblem — Report a problem
    • OAuthProvider::setParam — Set a parameter
    • OAuthProvider::setRequestTokenPath — Set request token path
    • OAuthProvider::timestampNonceHandler — Set the timestampNonceHandler handler callback
    • OAuthProvider::tokenHandler — Set the tokenHandler handler callback
  • OAuthException — OAuthException class

User Contributed Notes

There are no user contributed notes for this page.


소개

이 확장은 OAuth 1.0a 사용자와 제공자의 연결을 제공합니다. OAuth는 애플리케이션이 사용자 이름과 비밀번호를 저장할 필요 없이 안전하게 데이터를 액세스할 수 있도록 하는 인증 프로토콜입니다.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

PECL/oauth requires PHP 5.1 or newer, ext/hash and ext/pcre.

PECL/oauth may optionally require libcurl if selected at build time. If configured with libcurl, it must be built with HTTPS support.

User Contributed Notes

There are no user contributed notes for this page.


설치

PECL 확장 설치에 관한 정보는 매뉴얼의 PECL 확장 설치에 있습니다. 새 릴리즈, 내려받기, 소스파일, 개발자 정보, CHANGELOG 등의 추가 정보는 여기에 있습니다: » http://pear.php.net/package/oauth

User Contributed Notes

There are no user contributed notes for this page.


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

Most of these constants involve problems, which are also described within the official OAuth » Problem Reporting documentation. Note however, that these constant names are specific to PHP, although the naming scheme is similar.

OAUTH_SIG_METHOD_RSASHA1 (string)
OAuth RSA-SHA1 signature method.
OAUTH_SIG_METHOD_HMACSHA1 (string)

OAuth HMAC-SHA1 signature method.

OAUTH_SIG_METHOD_HMACSHA256 (string)
OAuth HMAC-SHA256 signature method.
OAUTH_AUTH_TYPE_AUTHORIZATION (string)

This constant represents putting OAuth parameters in the Authorization header.

OAUTH_AUTH_TYPE_NONE (string)

This constant indicates a NoAuth OAuth request.

OAUTH_AUTH_TYPE_URI (string)

This constant represents putting OAuth parameters in the request URI.

OAUTH_AUTH_TYPE_FORM (string)

This constant represents putting OAuth parameters as part of the HTTP POST body.

OAUTH_HTTP_METHOD_GET (string)

Use the GET method for the OAuth request.

OAUTH_HTTP_METHOD_POST (string)

Use the POST method for the OAuth request.

OAUTH_HTTP_METHOD_PUT (string)

Use the PUT method for the OAuth request.

OAUTH_HTTP_METHOD_HEAD (string)

Use the HEAD method for the OAuth request.

OAUTH_HTTP_METHOD_DELETE (string)
Use the DELETE method for the OAuth request.
OAUTH_REQENGINE_STREAMS (integer)
Used by OAuth::setRequestEngine() to set the engine to PHP streams, as opposed to OAUTH_REQENGINE_CURL for Curl.
OAUTH_REQENGINE_CURL (integer)
Used by OAuth::setRequestEngine() to set the engine to Curl, as opposed to OAUTH_REQENGINE_STREAMS for PHP streams.
OAUTH_OK (integer)
Life is good.
OAUTH_BAD_NONCE (integer)
The oauth_nonce value was used in a previous request, therefore it cannot be used now.
OAUTH_BAD_TIMESTAMP (integer)
The oauth_timestamp value was not accepted by the service provider. In this case, the response should also contain the oauth_acceptable_timestamps parameter.
OAUTH_CONSUMER_KEY_UNKNOWN (integer)
The oauth_consumer_key is temporarily unacceptable to the service provider. For example, the service provider may be throttling the consumer.
OAUTH_CONSUMER_KEY_REFUSED (integer)
The consumer key was refused.
OAUTH_INVALID_SIGNATURE (integer)
The oauth_signature is invalid, as it does not match the signature computed by the service provider.
OAUTH_TOKEN_USED (integer)
The oauth_token has been consumed. It can no longer be used because it has already been used in the previous request(s).
OAUTH_TOKEN_EXPIRED (integer)
The oauth_token has expired.
OAUTH_TOKEN_REVOKED (integer)
The oauth_token has been revoked, and will never be accepted.
OAUTH_TOKEN_REJECTED (integer)
The oauth_token was not accepted by the service provider. The reason is not known, but it might be because the token was never issued, already consumed, expired, and/or forgotten by the service provider.
OAUTH_VERIFIER_INVALID (integer)
The oauth_verifier is incorrect.
OAUTH_PARAMETER_ABSENT (integer)
A required parameter was not received. In this case, the response should also contain the oauth_parameters_absent parameter.
OAUTH_SIGNATURE_METHOD_REJECTED (integer)
The oauth_signature_method was not accepted by service provider.

User Contributed Notes

There are no user contributed notes for this page.


예제

Table of Contents

  • FireEagle

User Contributed Notes

There are no user contributed notes for this page.


FireEagle

<?php
$req_url = 'https://fireeagle.yahooapis.com/oauth/request_token';
$authurl = 'https://fireeagle.yahoo.net/oauth/authorize';
$acc_url = 'https://fireeagle.yahooapis.com/oauth/access_token';
$api_url = 'https://fireeagle.yahooapis.com/api/0.1';
$conskey = 'your_consumer_key';
$conssec = 'your_consumer_secret';

session_start();

// In state=1 the next request should include an oauth_token.
// If it doesn't go back to 0
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0;
try {
  $oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
  $oauth->enableDebug();
  if(!isset($_GET['oauth_token']) && !$_SESSION['state']) {
    $request_token_info = $oauth->getRequestToken($req_url);
    $_SESSION['secret'] = $request_token_info['oauth_token_secret'];
    $_SESSION['state'] = 1;
    header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token']);
    exit;
  } else if($_SESSION['state']==1) {
    $oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
    $access_token_info = $oauth->getAccessToken($acc_url);
    $_SESSION['state'] = 2;
    $_SESSION['token'] = $access_token_info['oauth_token'];
    $_SESSION['secret'] = $access_token_info['oauth_token_secret'];
  }
  $oauth->setToken($_SESSION['token'],$_SESSION['secret']);
  $oauth->fetch("$api_url/user.json");
  $json = json_decode($oauth->getLastResponse());
  print_r($json);
} catch(OAuthException $E) {
  print_r($E);
}
?>

User Contributed Notes

pierre dot mihawk at gmail dot com 25-Jun-2014 04:09
In the case you're asking an API which gives you a reusable application key, you'll have to use the "verifier" in the getAccessToken() method, but also the "callback" parameter in the getRequestToken().

<?php
     $request_token_info = $oauth->getRequestToken($req_url, "<YOU_CALLBACK_URI>");
?>

In my case, once I accepted the form in the Authorize URI, I was redirected to this callback URI with a "oauth_verifier" GET parameter. I injected it into the access token request :

<?php
    $access_token_info = $oauth->getAccessToken($acc_url, '', $_GET['oauth_verifier']);
?>

I finally obtained an access token that I hardcoded ; it doesn't need any refresh.
kayalshri at gmail dot com 18-Apr-2013 02:28
Please refer the oAuth End Points for Top level service providers http://www.cheatography.com/kayalshri/cheat-sheets/oauth-end-points/
brianmoreau at uk2 dot net 09-Mar-2013 09:26
I have just managed to modify this script to work with the Twitter API...

The changes are...

$req_url = 'https://api.twitter.com/oauth/request_token';
$authurl = 'https://api.twitter.com/oauth/authorize';
$acc_url = 'https://api.twitter.com/oauth/access_token';
$api_url = 'https://api.twitter.com/1.1/account';
$conskey = 'YOURAPPconskey';
$conssec = 'YOURAPPconssec';

And change
#$oauth->fetch("$api_url/user.json");
# verify_credentials
# TO
$oauth->fetch("$api_url/verify_credentials.json");


OAuth 함수 목록

Table of Contents

  • oauth_get_sbs — Generate a Signature Base String
  • oauth_urlencode — Encode a URI to RFC 3986

User Contributed Notes

There are no user contributed notes for this page.


Generate a Signature Base String

  • ≪ OAuth 함수 목록
  • oauth_urlencode ≫
  • PHP Manual
  • OAuth 함수 목록
  • Generate a Signature Base String

oauth_get_sbs

(PECL OAuth >=0.99.7)

oauth_get_sbs — Generate a Signature Base String

설명

string oauth_get_sbs ( string $http_method , string $uri [, array $request_parameters ] )

Generates a Signature Base String according to pecl/oauth.

인수

http_method

The HTTP method.

uri

URI to encode.

request_parameters

Array of request parameters.

반환값

Returns a Signature Base String.

User Contributed Notes

There are no user contributed notes for this page.


Encode a URI to RFC 3986

  • ≪ oauth_get_sbs
  • OAuth ≫
  • PHP Manual
  • OAuth 함수 목록
  • Encode a URI to RFC 3986

oauth_urlencode

(PECL OAuth >=0.99.2)

oauth_urlencode — Encode a URI to RFC 3986

설명

string oauth_urlencode ( string $uri )

Encodes a URI to » RFC 3986.

인수

uri

URI to encode.

반환값

Returns an » RFC 3986 encoded string.

User Contributed Notes

bohwaz 30-Mar-2010 04:20
Note that php5.3 rawurlencode will do exactly the same thing.

For PHP 5.2, easy replacement to this function :

<?php

function rfc3986_encode($str)
{
  $str = rawurlencode($str);
  $str = str_replace('%E7', '~', $str);
  return $str;
}

?>


The OAuth class

(PECL OAuth >= 0.99.1)

소개

The OAuth extension provides a simple interface to interact with data providers using the OAuth HTTP specification to protect private resources.

클래스 개요

OAuth {
/* 프로퍼티 */
public $debug ;
public $sslChecks ;
public $debugInfo ;
/* 메소드 */
public __construct ( string $consumer_key , string $consumer_secret [, string $signature_method = OAUTH_SIG_METHOD_HMACSHA1 [, int $auth_type = 0 ]] )
public void __destruct ( void )
public bool disableDebug ( void )
public bool disableRedirects ( void )
public bool disableSSLChecks ( void )
public bool enableDebug ( void )
public bool enableRedirects ( void )
public bool enableSSLChecks ( void )
public mixed fetch ( string $protected_resource_url [, array $extra_parameters [, string $http_method [, array $http_headers ]]] )
public string generateSignature ( string $http_method , string $url [, mixed $extra_parameters ] )
public array getAccessToken ( string $access_token_url [, string $auth_session_handle [, string $verifier_token [, string $http_method ]]] )
public array getCAPath ( void )
public string getLastResponse ( void )
public string getLastResponseHeaders ( void )
public array getLastResponseInfo ( void )
public string getRequestHeader ( string $http_method , string $url [, mixed $extra_parameters ] )
public array getRequestToken ( string $request_token_url [, string $callback_url [, string $http_method ]] )
public mixed setAuthType ( int $auth_type )
public mixed setCAPath ([ string $ca_path [, string $ca_info ]] )
public mixed setNonce ( string $nonce )
public void setRequestEngine ( int $reqengine )
public mixed setRSACertificate ( string $cert )
public bool setSSLChecks ( int $sslcheck )
public mixed setTimestamp ( string $timestamp )
public bool setToken ( string $token , string $token_secret )
public bool setVersion ( string $version )
}

프로퍼티

debug

sslChecks

debugInfo

Table of Contents

  • OAuth::__construct — Create a new OAuth object
  • OAuth::__destruct — The destructor
  • OAuth::disableDebug — Turn off verbose debugging
  • OAuth::disableRedirects — Turn off redirects
  • OAuth::disableSSLChecks — Turn off SSL checks
  • OAuth::enableDebug — Turn on verbose debugging
  • OAuth::enableRedirects — Turn on redirects
  • OAuth::enableSSLChecks — Turn on SSL checks
  • OAuth::fetch — Fetch an OAuth protected resource
  • OAuth::generateSignature — Generate a signature
  • OAuth::getAccessToken — Fetch an access token
  • OAuth::getCAPath — Gets CA information
  • OAuth::getLastResponse — Get the last response
  • OAuth::getLastResponseHeaders — Get headers for last response
  • OAuth::getLastResponseInfo — Get HTTP information about the last response
  • OAuth::getRequestHeader — Generate OAuth header string signature
  • OAuth::getRequestToken — Fetch a request token
  • OAuth::setAuthType — Set authorization type
  • OAuth::setCAPath — Set CA path and info
  • OAuth::setNonce — Set the nonce for subsequent requests
  • OAuth::setRequestEngine — The setRequestEngine purpose
  • OAuth::setRSACertificate — Set the RSA certificate
  • OAuth::setSSLChecks — Tweak specific SSL checks for requests.
  • OAuth::setTimestamp — Set the timestamp
  • OAuth::setToken — Sets the token and secret
  • OAuth::setVersion — Set the OAuth version

User Contributed Notes

There are no user contributed notes for this page.


Create a new OAuth object

  • ≪ OAuth
  • OAuth::__destruct ≫
  • PHP Manual
  • OAuth
  • Create a new OAuth object

OAuth::__construct

(PECL OAuth >= 0.99.1)

OAuth::__construct — Create a new OAuth object

설명

public OAuth::__construct ( string $consumer_key , string $consumer_secret [, string $signature_method = OAUTH_SIG_METHOD_HMACSHA1 [, int $auth_type = 0 ]] )

Creates a new OAuth object

인수

consumer_key

The consumer key provided by the service provider.

consumer_secret

The consumer secret provided by the service provider.

signature_method

This optional parameter defines which signature method to use, by default it is OAUTH_SIG_METHOD_HMACSHA1 (HMAC-SHA1).

auth_type

This optional parameter defines how to pass the OAuth parameters to a consumer, by default it is OAUTH_AUTH_TYPE_AUTHORIZATION (in the Authorization header).

User Contributed Notes

There are no user contributed notes for this page.


The destructor

  • ≪ OAuth::__construct
  • OAuth::disableDebug ≫
  • PHP Manual
  • OAuth
  • The destructor

OAuth::__destruct

(PECL OAuth >= 0.99.9)

OAuth::__destruct — The destructor

설명

public void OAuth::__destruct ( void )

The destructor.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

참고

  • OAuth::__construct() - Create a new OAuth object

User Contributed Notes

There are no user contributed notes for this page.


Turn off verbose debugging

  • ≪ OAuth::__destruct
  • OAuth::disableRedirects ≫
  • PHP Manual
  • OAuth
  • Turn off verbose debugging

OAuth::disableDebug

(PECL OAuth >= 0.99.3)

OAuth::disableDebug — Turn off verbose debugging

설명

public bool OAuth::disableDebug ( void )

Turns off verbose request information (off by default). Alternatively, the debug property can be set to a FALSE value to turn debug off.

인수

이 함수는 인수가 없습니다.

반환값

TRUE

변경점

버전 | 설명
0.99.8 | The related debug property was added.

참고

  • OAuth::enableDebug() - Turn on verbose debugging

User Contributed Notes

There are no user contributed notes for this page.


Turn off redirects

  • ≪ OAuth::disableDebug
  • OAuth::disableSSLChecks ≫
  • PHP Manual
  • OAuth
  • Turn off redirects

OAuth::disableRedirects

(PECL OAuth >= 0.99.9)

OAuth::disableRedirects — Turn off redirects

설명

public bool OAuth::disableRedirects ( void )

Disable redirects from being followed automatically, thus allowing the request to be manually redirected.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

TRUE

참고

  • OAuth::enableRedirects() - Turn on redirects

User Contributed Notes

There are no user contributed notes for this page.


Turn off SSL checks

  • ≪ OAuth::disableRedirects
  • OAuth::enableDebug ≫
  • PHP Manual
  • OAuth
  • Turn off SSL checks

OAuth::disableSSLChecks

(PECL OAuth >= 0.99.5)

OAuth::disableSSLChecks — Turn off SSL checks

설명

public bool OAuth::disableSSLChecks ( void )

Turns off the usual SSL peer certificate and host checks, this is not for production environments. Alternatively, the sslChecks member can be set to FALSE to turn SSL checks off.

인수

이 함수는 인수가 없습니다.

반환값

TRUE

변경점

버전 | 설명
0.99.8 | The sslChecks member was added

참고

  • OAuth::enableSSLChecks() - Turn on SSL checks

User Contributed Notes

There are no user contributed notes for this page.


Turn on verbose debugging

  • ≪ OAuth::disableSSLChecks
  • OAuth::enableRedirects ≫
  • PHP Manual
  • OAuth
  • Turn on verbose debugging

OAuth::enableDebug

(PECL OAuth >= 0.99.3)

OAuth::enableDebug — Turn on verbose debugging

설명

public bool OAuth::enableDebug ( void )

Turns on verbose request information useful for debugging, the debug information is stored in the debugInfo member. Alternatively, the debug member can be set to a non-FALSE value to turn debug on.

인수

이 함수는 인수가 없습니다.

반환값

TRUE

변경점

버전 | 설명
0.99.8 | The debug and debugInfo members were added

참고

  • OAuth::disableDebug() - Turn off verbose debugging

User Contributed Notes

There are no user contributed notes for this page.


Turn on redirects

  • ≪ OAuth::enableDebug
  • OAuth::enableSSLChecks ≫
  • PHP Manual
  • OAuth
  • Turn on redirects

OAuth::enableRedirects

(PECL OAuth >= 0.99.9)

OAuth::enableRedirects — Turn on redirects

설명

public bool OAuth::enableRedirects ( void )

Follow and sign redirects automatically, which is enabled by default.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

TRUE

참고

  • OAuth::disableRedirects() - Turn off redirects

User Contributed Notes

There are no user contributed notes for this page.


Turn on SSL checks

  • ≪ OAuth::enableRedirects
  • OAuth::fetch ≫
  • PHP Manual
  • OAuth
  • Turn on SSL checks

OAuth::enableSSLChecks

(PECL OAuth >= 0.99.5)

OAuth::enableSSLChecks — Turn on SSL checks

설명

public bool OAuth::enableSSLChecks ( void )

Turns on the usual SSL peer certificate and host checks (enabled by default). Alternatively, the sslChecks member can be set to a non-FALSE value to turn SSL checks off.

인수

이 함수는 인수가 없습니다.

반환값

TRUE

변경점

버전 | 설명
0.99.8 | The sslChecks member was added

참고

  • OAuth::disableSSLChecks() - Turn off SSL checks

User Contributed Notes

There are no user contributed notes for this page.


Fetch an OAuth protected resource

  • ≪ OAuth::enableSSLChecks
  • OAuth::generateSignature ≫
  • PHP Manual
  • OAuth
  • Fetch an OAuth protected resource

OAuth::fetch

(PECL OAuth >= 0.99.1)

OAuth::fetch — Fetch an OAuth protected resource

설명

public mixed OAuth::fetch ( string $protected_resource_url [, array $extra_parameters [, string $http_method [, array $http_headers ]]] )

Fetch a resource.

인수

protected_resource_url

URL to the OAuth protected resource.

extra_parameters

Extra parameters to send with the request for the resource.

http_method

One of the OAUTH_HTTP_METHOD_* OAUTH constants, which includes GET, POST, PUT, HEAD, or DELETE.

HEAD (OAUTH_HTTP_METHOD_HEAD) can be useful for discovering information prior to the request (if OAuth credentials are in the Authorization header).

http_headers

HTTP client headers (such as User-Agent, Accept, etc.)

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
1.0.0 | 이전에는 실패시 FALSE가 아닌 NULL을 반환했습니다.
0.99.5 | The http_method parameter was added
0.99.8 | The http_headers parameter was added

예제

Example #1 OAuth::fetch() example

<?php
try {
    $oauth = new OAuth("consumer_key","consumer_secret",OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
    $oauth->setToken("access_token","access_token_secret");

    $oauth->fetch("http://photos.example.net/photo?file=vacation.jpg");

    $response_info = $oauth->getLastResponseInfo();
    header("Content-Type: {$response_info["content_type"]}");
    echo $oauth->getLastResponse();
} catch(OAuthException $E) {
    echo "Exception caught!\n";
    echo "Response: ". $E->lastResponse . "\n";
}
?>

참고

  • OAuth::getLastResponse() - Get the last response
  • OAuth::getLastResponseInfo() - Get HTTP information about the last response
  • OAuth::setToken() - Sets the token and secret

User Contributed Notes

Lars Stttrup Nielsen 17-Mar-2016 04:37
So I'm using this to talk to the Woocommerce REST API, and was having a lot of trouble figuring out what exactly $extra_parameters was supposed to look like (which WC REST API expects, besides being of the type OAUTH_AUTH_TYPE_URI).

The multidimensional array I fed it crashed PHP, so don't do that if you're in my shoes.

What ended up solving it was me looking through the OAuth source and noticing that $extra_parameters can also be a string, which, encoded as json (json_encode), solved all my troubles as WC accepted it.
zverik at textual dot ru 01-Apr-2014 05:46
If $extra_parameters is not an array, you have to specify Content-Type header, or else you'll get HTTP 401 error. Example:

<?php
$oauth->fetch(ENDPOINT, '{"action": "get_user_info"}', OAUTH_HTTP_METHOD_PUT, array('Content-Type' => 'application/json'));
?>
chris dot barr at ntlworld dot com 06-Mar-2013 02:59
The fetch() method will throw an OAuthException if the returned http status code is in the 4xx or 5xx range:

<?php
// Querying Twitter with bad login details
try {
  $oauth->fetch('https://api.twitter.com/1.1/favorites/list.json');
}
catch(Exception $e) {
  echo $e->getCode(); // 401
  // Message generated by OAuth class
  echo $e->getMessage(); // Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)
  // Message returned from Twitter
  echo $e->lastResponse; // {"errors":[{"message":"Could not authenticate you","code":32}]}
}
sun at drupal dot org 10-Aug-2011 03:40
Make sure that your $extra_parameters is an array.

If it's not, then OAuth will silently skip the malformed data type and produce a signature base string that is invalid (doesn't contain POST parameters, as defined in the RFC).

You should file a critical bug report against any REST API you find in the wild that accepts such a bogus signature to pass authentication.
contact info at mech dot cx 24-Mar-2011 01:00
I was having troubles getting fetch() to post, the remote server (Twitter, in this case) complained at me that their "resource only supports POST". Turned out to be a known bug in OAuth 1.1, downgrading to 1.0 fixed it.

Don't lose as much time over this as I did :-)
Lyuben Penkovski (l_penkovski at yahoo dot com) 27-Aug-2010 08:51
If the provider's web server is configured to use Keep-Alive extension to HTTP protocol (HTTP 1.1), there can be a big delay in the response time from the provider. By default Apache is configured to use Keep-Alive for 5 seconds. This is the delay after which the response will come back to the consumer. If you have this issue of delayed result, you can pass in HTTP headers when calling $consumer->fetch():

<?php
$consumer = new OAuth("consumer_key", "consumer_secret", OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_FORM);
$consumer->fetch('http://example.com/api/', null, OAUTH_HTTP_METHOD_POST, array('Connection'=>'close'));
?>

Then the provider will send the result immediately after it's ready with the processing and the connection will be closed. Unfortunately, when calling $consumer->getRequestToken() and $consumer->getAccessToken() there's no way provided to pass in HTTP headers and this delay (if present) cannot be avoided, or at least we could not find a way to avoid it.

The solution that worked for us is to send this header from the provider when returning result to the consumer:

<?php
$result = 'oauth_callback_accepted=true&oauth_token=' . $this->urlencode($token->oauth_token) .
          '&oauth_token_secret='.$this->urlencode($token->oauth_token_secret);

header('HTTP/1.1 200 OK');
header('Content-Length: '.strlen($result));
header('Content-Type: application/x-www-form-urlencoded');
header('Connection:close');
echo $result;
?>

This can work if you have the possibility to modify the code of the provider, e.g. if you are the provider yourself or if you can talk with the people that develop it and ask them to send this header for your request.


Generate a signature

  • ≪ OAuth::fetch
  • OAuth::getAccessToken ≫
  • PHP Manual
  • OAuth
  • Generate a signature

OAuth::generateSignature

(No version information available, might only be in Git)

OAuth::generateSignature — Generate a signature

설명

public string OAuth::generateSignature ( string $http_method , string $url [, mixed $extra_parameters ] )

Generate a signature based on the final HTTP method, URL and a string/array of parameters.

인수

http_method

HTTP method for request

url

URL for request

extra_parameters

String or array of additional parameters.

반환값

A string containing the generated signature실패 시 FALSE를 반환합니다

User Contributed Notes

There are no user contributed notes for this page.


Fetch an access token

  • ≪ OAuth::generateSignature
  • OAuth::getCAPath ≫
  • PHP Manual
  • OAuth
  • Fetch an access token

OAuth::getAccessToken

(PECL OAuth >= 0.99.1)

OAuth::getAccessToken — Fetch an access token

설명

public array OAuth::getAccessToken ( string $access_token_url [, string $auth_session_handle [, string $verifier_token [, string $http_method ]]] )

Fetch an access token, secret and any additional response parameters from the service provider.

인수

access_token_url

URL to the access token API.

auth_session_handle

Authorization session handle, this parameter does not have any citation in the core OAuth 1.0 specification but may be implemented by large providers. » See ScalableOAuth for more information.

verifier_token

For service providers which support 1.0a, a verifier_token must be passed while exchanging the request token for the access token. If the verifier_token is present in $_GET or $_POST it is passed automatically and the caller does not need to specify a verifier_token (usually if the access token is exchanged at the oauth_callback URL). » See ScalableOAuth for more information.

http_method

HTTP method to use, e.g. GET or POST.

반환값

Returns an array containing the parsed OAuth response on success or FALSE on failure.

변경점

버전 | 설명
1.0.0 | 이전에는 실패시 FALSE가 아닌 NULL을 반환했습니다.
0.99.9 | The verifier_token parameter was added

예제

Example #1 OAuth::getAccessToken() example

<?php
try {
    $oauth = new OAuth(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET);
    $oauth->setToken($request_token,$request_token_secret);
    $access_token_info = $oauth->getAccessToken("https://example.com/oauth/access_token");
    if(!empty($access_token_info)) {
        print_r($access_token_info);
    } else {
        print "Failed fetching access token, response was: " . $oauth->getLastResponse();
    }
} catch(OAuthException $E) {
    echo "Response: ". $E->lastResponse . "\n";
}
?>

위 예제의 출력 예시:

Array
(
    [oauth_token] => some_token
    [oauth_token_secret] => some_token_secret
)

참고

  • OAuth::getLastResponse() - Get the last response
  • OAuth::getLastResponseInfo() - Get HTTP information about the last response
  • OAuth::setToken() - Sets the token and secret

User Contributed Notes

There are no user contributed notes for this page.


Gets CA information

  • ≪ OAuth::getAccessToken
  • OAuth::getLastResponse ≫
  • PHP Manual
  • OAuth
  • Gets CA information

OAuth::getCAPath

(PECL OAuth >= 0.99.8)

OAuth::getCAPath — Gets CA information

설명

public array OAuth::getCAPath ( void )

Gets the Certificate Authority information, which includes the ca_path and ca_info set by OAuth::setCaPath().

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

An array of Certificate Authority information, specifically as ca_path and ca_info keys within the returned associative array.

참고

  • OAuth::setCAPath() - Set CA path and info
  • OAuth::getLastResponseInfo() - Get HTTP information about the last response

User Contributed Notes

There are no user contributed notes for this page.


Get the last response

  • ≪ OAuth::getCAPath
  • OAuth::getLastResponseHeaders ≫
  • PHP Manual
  • OAuth
  • Get the last response

OAuth::getLastResponse

(PECL OAuth >= 0.99.1)

OAuth::getLastResponse — Get the last response

설명

public string OAuth::getLastResponse ( void )

Get the raw response of the most recent request.

인수

이 함수는 인수가 없습니다.

반환값

Returns a string containing the last response.

참고

  • OAuth::getLastResponseInfo() - Get HTTP information about the last response
  • OAuth::fetch() - Fetch an OAuth protected resource

User Contributed Notes

There are no user contributed notes for this page.


Get headers for last response

  • ≪ OAuth::getLastResponse
  • OAuth::getLastResponseInfo ≫
  • PHP Manual
  • OAuth
  • Get headers for last response

OAuth::getLastResponseHeaders

(No version information available, might only be in Git)

OAuth::getLastResponseHeaders — Get headers for last response

설명

public string OAuth::getLastResponseHeaders ( void )

Get headers for last response.

인수

이 함수는 인수가 없습니다.

반환값

A string containing the last response's headers실패 시 FALSE를 반환합니다

User Contributed Notes

There are no user contributed notes for this page.


Get HTTP information about the last response

  • ≪ OAuth::getLastResponseHeaders
  • OAuth::getRequestHeader ≫
  • PHP Manual
  • OAuth
  • Get HTTP information about the last response

OAuth::getLastResponseInfo

(PECL OAuth >= 0.99.1)

OAuth::getLastResponseInfo — Get HTTP information about the last response

설명

public array OAuth::getLastResponseInfo ( void )

Get HTTP information about the last response.

인수

이 함수는 인수가 없습니다.

반환값

Returns an array containing the response information for the last request. Constants from curl_getinfo() may be used.

참고

  • OAuth::fetch() - Fetch an OAuth protected resource
  • OAuth::getLastResponse() - Get the last response

User Contributed Notes

There are no user contributed notes for this page.


Generate OAuth header string signature

  • ≪ OAuth::getLastResponseInfo
  • OAuth::getRequestToken ≫
  • PHP Manual
  • OAuth
  • Generate OAuth header string signature

OAuth::getRequestHeader

(No version information available, might only be in Git)

OAuth::getRequestHeader — Generate OAuth header string signature

설명

public string OAuth::getRequestHeader ( string $http_method , string $url [, mixed $extra_parameters ] )

Generate OAuth header string signature based on the final HTTP method, URL and a string/array of parameters

인수

http_method

HTTP method for request.

url

URL for request.

extra_parameters

String or array of additional parameters.

반환값

A string containing the generated request header실패 시 FALSE를 반환합니다

User Contributed Notes

There are no user contributed notes for this page.


Fetch a request token

  • ≪ OAuth::getRequestHeader
  • OAuth::setAuthType ≫
  • PHP Manual
  • OAuth
  • Fetch a request token

OAuth::getRequestToken

(PECL OAuth >= 0.99.1)

OAuth::getRequestToken — Fetch a request token

설명

public array OAuth::getRequestToken ( string $request_token_url [, string $callback_url [, string $http_method ]] )

Fetch a request token, secret and any additional response parameters from the service provider.

인수

request_token_url

URL to the request token API.

callback_url

OAuth callback URL. If callback_url is passed and is an empty value, it is set to "oob" to address the OAuth 2009.1 advisory.

http_method

HTTP method to use, e.g. GET or POST.

반환값

Returns an array containing the parsed OAuth response on success or FALSE on failure.

변경점

버전 | 설명
1.0.0 | 이전에는 실패시 FALSE가 아닌 NULL을 반환했습니다.
0.99.9 | The callback_url parameter was added

예제

Example #1 OAuth::getRequestToken() example

<?php
try {
    $oauth = new OAuth(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET);
    $request_token_info = $oauth->getRequestToken("https://example.com/oauth/request_token");
    if(!empty($request_token_info)) {
        print_r($request_token_info);
    } else {
        print "Failed fetching request token, response was: " . $oauth->getLastResponse();
    }
} catch(OAuthException $E) {
    echo "Response: ". $E->lastResponse . "\n";
}
?>

위 예제의 출력 예시:

Array
(
    [oauth_token] => some_token
    [oauth_token_secret] => some_token_secret
)

참고

  • OAuth::getLastResponse() - Get the last response
  • OAuth::getLastResponseInfo() - Get HTTP information about the last response

User Contributed Notes

bohwaz 29-Mar-2010 04:29
Please note that if you don't supply callback_url, the oauth parameter oauth_callback will not be sent to the server and will result in an error from the server, as this parameter is REQUIRED in the OAuth spec.


Set authorization type

  • ≪ OAuth::getRequestToken
  • OAuth::setCAPath ≫
  • PHP Manual
  • OAuth
  • Set authorization type

OAuth::setAuthType

(PECL OAuth >= 0.99.1)

OAuth::setAuthType — Set authorization type

설명

public mixed OAuth::setAuthType ( int $auth_type )

Set where the OAuth parameters should be passed.

인수

auth_type

auth_type can be one of the following flags (in order of decreasing preference as per OAuth 1.0 section 5.2):

OAUTH_AUTH_TYPE_AUTHORIZATION
Pass the OAuth parameters in the HTTP Authorization header.
OAUTH_AUTH_TYPE_FORM
Append the OAuth parameters to the HTTP POST request body.
OAUTH_AUTH_TYPE_URI
Append the OAuth parameters to the request URI.
OAUTH_AUTH_TYPE_NONE
None.

반환값

Returns TRUE if a parameter is correctly set, otherwise FALSE (e.g., if an invalid auth_type is passed in.)

변경점

버전 | 설명
1.0.0 | 이전에는 실패시 FALSE가 아닌 NULL을 반환했습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set CA path and info

  • ≪ OAuth::setAuthType
  • OAuth::setNonce ≫
  • PHP Manual
  • OAuth
  • Set CA path and info

OAuth::setCAPath

(PECL OAuth >= 0.99.8)

OAuth::setCAPath — Set CA path and info

설명

public mixed OAuth::setCAPath ([ string $ca_path [, string $ca_info ]] )

Sets the Certificate Authority (CA), both for path and info.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

ca_path

The CA Path being set.

ca_info

The CA Info being set.

반환값

Returns TRUE on success, or FALSE if either ca_path or ca_info are considered invalid.

변경점

버전 | 설명
1.0.0 | 이전에는 실패시 FALSE가 아닌 NULL을 반환했습니다.

참고

  • OAuth::getCaPath() - Gets CA information

User Contributed Notes

There are no user contributed notes for this page.


Set the nonce for subsequent requests

  • ≪ OAuth::setCAPath
  • OAuth::setRequestEngine ≫
  • PHP Manual
  • OAuth
  • Set the nonce for subsequent requests

OAuth::setNonce

(PECL OAuth >= 0.99.1)

OAuth::setNonce — Set the nonce for subsequent requests

설명

public mixed OAuth::setNonce ( string $nonce )

Sets the nonce for all subsequent requests.

인수

nonce

The value for oauth_nonce.

반환값

Returns TRUE on success, or FALSE if the nonce is considered invalid.

변경점

버전 | 설명
1.0.0 | 이전에는 실패시 FALSE가 아닌 NULL을 반환했습니다.

참고

  • OAuth::setToken() - Sets the token and secret
  • OAuth::setAuthType() - Set authorization type
  • OAuth::setVersion() - Set the OAuth version

User Contributed Notes

There are no user contributed notes for this page.


The setRequestEngine purpose

  • ≪ OAuth::setNonce
  • OAuth::setRSACertificate ≫
  • PHP Manual
  • OAuth
  • The setRequestEngine purpose

OAuth::setRequestEngine

(PECL OAuth >= 1.0.0)

OAuth::setRequestEngine — The setRequestEngine purpose

설명

public void OAuth::setRequestEngine ( int $reqengine )

Sets the Request Engine, that will be sending the HTTP requests.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

reqengine

The desired request engine. Set to OAUTH_REQENGINE_STREAMS to use PHP Streams, or OAUTH_REQENGINE_CURL to use Curl.

반환값

값을 반환하지 않습니다.

오류/예외

Emits an OAuthException exception if an invalid request engine is chosen.

예제

Example #1 OAuth::setRequestEngine() example

<?php
$consumer = new OAuth();

$consumer->setRequestEngine(OAUTH_REQENGINE_STREAMS);
?>

참고

  • Curl
  • PHP streams
  • OAuthException

User Contributed Notes

There are no user contributed notes for this page.


Set the RSA certificate

  • ≪ OAuth::setRequestEngine
  • OAuth::setSSLChecks ≫
  • PHP Manual
  • OAuth
  • Set the RSA certificate

OAuth::setRSACertificate

(PECL OAuth >= 1.0.0)

OAuth::setRSACertificate — Set the RSA certificate

설명

public mixed OAuth::setRSACertificate ( string $cert )

Sets the RSA certificate.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

cert

The RSA certificate.

반환값

Returns TRUE on success, or FALSE on failure (e.g., the RSA certificate cannot be parsed.)

변경점

버전 | 설명
1.0.0 | 이전에는 실패시 FALSE가 아닌 NULL을 반환했습니다.

예제

Example #1 An OAuth::setRsaCertificate() example

<?php
$consume = new OAuth('1234', '', OAUTH_SIG_METHOD_RSASHA1);

$consume->setRSACertificate(file_get_contents('test.pem'));
?>

참고

  • OAuth::setCaPath() - Set CA path and info

User Contributed Notes

There are no user contributed notes for this page.


Tweak specific SSL checks for requests.

  • ≪ OAuth::setRSACertificate
  • OAuth::setTimestamp ≫
  • PHP Manual
  • OAuth
  • Tweak specific SSL checks for requests.

OAuth::setSSLChecks

(No version information available, might only be in Git)

OAuth::setSSLChecks — Tweak specific SSL checks for requests.

설명

public bool OAuth::setSSLChecks ( int $sslcheck )

Tweak specific SSL checks for requests.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

sslcheck

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

There are no user contributed notes for this page.


Set the timestamp

  • ≪ OAuth::setSSLChecks
  • OAuth::setToken ≫
  • PHP Manual
  • OAuth
  • Set the timestamp

OAuth::setTimestamp

(PECL OAuth >= 1.0.0)

OAuth::setTimestamp — Set the timestamp

설명

public mixed OAuth::setTimestamp ( string $timestamp )

Sets the OAuth timestamp for subsequent requests.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

timestamp

The timestamp.

반환값

Returns TRUE, unless the timestamp is invalid, in which case FALSE is returned.

변경점

버전 | 설명
1.0.0 | 이전에는 실패시 FALSE가 아닌 NULL을 반환했습니다.

참고

  • OAuth::setNonce() - Set the nonce for subsequent requests

User Contributed Notes

There are no user contributed notes for this page.


Sets the token and secret

  • ≪ OAuth::setTimestamp
  • OAuth::setVersion ≫
  • PHP Manual
  • OAuth
  • Sets the token and secret

OAuth::setToken

(PECL OAuth >= 0.99.1)

OAuth::setToken — Sets the token and secret

설명

public bool OAuth::setToken ( string $token , string $token_secret )

Set the token and secret for subsequent requests.

인수

token

The OAuth token.

token_secret

The OAuth token secret.

반환값

TRUE

예제

Example #1 OAuth::setToken() example

<?php
$oauth = new OAuth(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET);
$oauth->setToken("token","token-secret");
?>

User Contributed Notes

There are no user contributed notes for this page.


Set the OAuth version

  • ≪ OAuth::setToken
  • OAuthProvider ≫
  • PHP Manual
  • OAuth
  • Set the OAuth version

OAuth::setVersion

(PECL OAuth >= 0.99.1)

OAuth::setVersion — Set the OAuth version

설명

public bool OAuth::setVersion ( string $version )

Sets the OAuth version for subsequent requests

인수

version

OAuth version, default value is always "1.0"

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

There are no user contributed notes for this page.


The OAuthProvider class

(PECL OAuth >= 1.0.0)

소개

Manages an OAuth provider class.

See also an external in-depth tutorial titled » Writing an OAuth Provider Service, which takes a hands-on approach to providing this service. There are also » OAuth provider examples within the OAuth extensions sources.

클래스 개요

OAuthProvider {
/* Methods */
final public bool addRequiredParameter ( string $req_params )
public void callconsumerHandler ( void )
public void callTimestampNonceHandler ( void )
public void calltokenHandler ( void )
public void checkOAuthRequest ([ string $uri [, string $method ]] )
public __construct ([ array $params_array ] )
public void consumerHandler ( callable $callback_function )
final public static string generateToken ( int $size [, bool $strong = false ] )
public void is2LeggedEndpoint ( mixed $params_array )
public void isRequestTokenEndpoint ( bool $will_issue_request_token )
final public bool removeRequiredParameter ( string $req_params )
final public static string reportProblem ( string $oauthexception [, bool $send_headers = true ] )
final public bool setParam ( string $param_key [, mixed $param_val ] )
final public bool setRequestTokenPath ( string $path )
public void timestampNonceHandler ( callable $callback_function )
public void tokenHandler ( callable $callback_function )
}

Table of Contents

  • OAuthProvider::addRequiredParameter — Add required parameters
  • OAuthProvider::callconsumerHandler — Calls the consumerNonceHandler callback
  • OAuthProvider::callTimestampNonceHandler — Calls the timestampNonceHandler callback
  • OAuthProvider::calltokenHandler — Calls the tokenNonceHandler callback
  • OAuthProvider::checkOAuthRequest — Check an oauth request
  • OAuthProvider::__construct — Constructs a new OAuthProvider object
  • OAuthProvider::consumerHandler — Set the consumerHandler handler callback
  • OAuthProvider::generateToken — Generate a random token
  • OAuthProvider::is2LeggedEndpoint — is2LeggedEndpoint
  • OAuthProvider::isRequestTokenEndpoint — Sets isRequestTokenEndpoint
  • OAuthProvider::removeRequiredParameter — Remove a required parameter
  • OAuthProvider::reportProblem — Report a problem
  • OAuthProvider::setParam — Set a parameter
  • OAuthProvider::setRequestTokenPath — Set request token path
  • OAuthProvider::timestampNonceHandler — Set the timestampNonceHandler handler callback
  • OAuthProvider::tokenHandler — Set the tokenHandler handler callback

User Contributed Notes

There are no user contributed notes for this page.


Add required parameters

  • ≪ OAuthProvider
  • OAuthProvider::callconsumerHandler ≫
  • PHP Manual
  • OAuthProvider
  • Add required parameters

OAuthProvider::addRequiredParameter

(PECL OAuth >= 1.0.0)

OAuthProvider::addRequiredParameter — Add required parameters

설명

final public bool OAuthProvider::addRequiredParameter ( string $req_params )

Add required oauth provider parameters.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

req_params

The required parameters.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • OAuthProvider::removeRequiredParameter() - Remove a required parameter

User Contributed Notes

me at thomashunter dot name 07-Jun-2013 12:03
This method can be used for adding non-standard parameters required by your application, as well as parameters within the spec.

<?php
$Provider->addRequiredParameter("permissions");
$Provider->addRequiredParameter("oauth_callback");


Calls the consumerNonceHandler callback

  • ≪ OAuthProvider::addRequiredParameter
  • OAuthProvider::callTimestampNonceHandler ≫
  • PHP Manual
  • OAuthProvider
  • Calls the consumerNonceHandler callback

OAuthProvider::callconsumerHandler

(No version information available, might only be in Git)

OAuthProvider::callconsumerHandler — Calls the consumerNonceHandler callback

설명

public void OAuthProvider::callconsumerHandler ( void )

Calls the registered consumer handler callback function, which is set with OAuthProvider::consumerHandler().

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

오류/예외

콜백 함수가 호출되지 않거나, 지정되지 않으면 E_ERROR 등급의 오류가 발생합니다.

참고

  • OAuthProvider::consumerHandler() - Set the consumerHandler handler callback

User Contributed Notes

There are no user contributed notes for this page.


Calls the timestampNonceHandler callback

  • ≪ OAuthProvider::callconsumerHandler
  • OAuthProvider::calltokenHandler ≫
  • PHP Manual
  • OAuthProvider
  • Calls the timestampNonceHandler callback

OAuthProvider::callTimestampNonceHandler

(PECL OAuth >= 1.0.0)

OAuthProvider::callTimestampNonceHandler — Calls the timestampNonceHandler callback

설명

public void OAuthProvider::callTimestampNonceHandler ( void )

Calls the registered timestamp handler callback function, which is set with OAuthProvider::timestampNonceHandler().

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

오류/예외

콜백 함수가 호출되지 않거나, 지정되지 않으면 E_ERROR 등급의 오류가 발생합니다.

참고

  • OAuthProvider::timestampNonceHandler() - Set the timestampNonceHandler handler callback

User Contributed Notes

There are no user contributed notes for this page.


Calls the tokenNonceHandler callback

  • ≪ OAuthProvider::callTimestampNonceHandler
  • OAuthProvider::checkOAuthRequest ≫
  • PHP Manual
  • OAuthProvider
  • Calls the tokenNonceHandler callback

OAuthProvider::calltokenHandler

(PECL OAuth >= 1.0.0)

OAuthProvider::calltokenHandler — Calls the tokenNonceHandler callback

설명

public void OAuthProvider::calltokenHandler ( void )

Calls the registered token handler callback function, which is set with OAuthProvider::tokenHandler().

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

오류/예외

콜백 함수가 호출되지 않거나, 지정되지 않으면 E_ERROR 등급의 오류가 발생합니다.

참고

  • OAuthProvider::tokenHandler() - Set the tokenHandler handler callback

User Contributed Notes

There are no user contributed notes for this page.


Check an oauth request

  • ≪ OAuthProvider::calltokenHandler
  • OAuthProvider::__construct ≫
  • PHP Manual
  • OAuthProvider
  • Check an oauth request

OAuthProvider::checkOAuthRequest

(PECL OAuth >= 1.0.0)

OAuthProvider::checkOAuthRequest — Check an oauth request

설명

public void OAuthProvider::checkOAuthRequest ([ string $uri [, string $method ]] )

Checks an OAuth request.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

uri

The optional URI, or endpoint.

method

The HTTP method. Optionally pass in one of the OAUTH_HTTP_METHOD_* OAuth constants.

반환값

값을 반환하지 않습니다.

오류/예외

Emits an E_ERROR level error if the HTTP method cannot be detected.

참고

  • OAuthProvider::reportProblem() - Report a problem

User Contributed Notes

tim at timlytle dot net 17-Oct-2013 10:30
Seems this method can only be called once per instance (or gives the same result as the first call on repeat calls).

My use case is API endpoints that can be accessed by a user or with 2 legged auth. If normal oAuth failed, I was calling is2LeggedEndpoint(true) and running again.

For that to work the OAuth Provider needs to be created again, simply changing one of the flags seems to have no effect on the next call.
Lyuben Penkovski (l_penkovski at yahoo dot com) 23-Aug-2010 12:59
This function checks if OAuth request is valid and signed correctly.

$provider->checkOAuthRequest();

It does this by first calling timestampNonceHandler and expects result OAUHT_OK from it. If the result is different, an exception is thrown. It's up to you to write the code that checks timestamp/nonce combinations.

Secondly, it calls consumerHandler and expects your code in the consumerHandler function to set $provider->consumer_secret to the correct value (you should take it from your consumer storage location where it's saved with consumer key). If $provider->consumer_secret is not set, or is not set with the proper value an exception is thrown. Proper value means that it should be the same consumer key that was used to sign the request by the consumer before sending it to here (the provider). Again expected result from this function is OAUTH_OK or some OAUTH error code if you want to throw exception.

Third, it calls tokenHandler, but only WHEN you are requesting ACCESS token or requesting protected data with authorized ACCESS TOKEN. In order for the provider to call tokenHandler, before a call to the checkOAuthRequest function is made, the provider should call the method that says that this is not a request token endpoint (this is access token endpoint):

$provider->isRequestTokenEndpoint (false);
$provider->checkOAuthRequest();

Again here OAuthProvider is expecting your code in the tokenHandler to set $provider->token_secret to the correct value (you should take it from your token storage place) because during the signing process it uses CONSUMER SECRET (for request token) and CONSUMER SECRET AND TOKEN SECRET (for access token and fetch of protected data) to sign the request.

After these 3 handler functions are called and return good results (OAUTH_OK) and set the values of the required fields $provider->consumer_secret and $provider->token_secret, then the checkOAuthRequest function signs the request. If something goes wrong, it throws exception, otherwise there comes the place for your code to proceed and handle the request:

- you can create request token (if it's a first request for request token)
- you can create access token (if it's a request for access token)
- you can return protected data to the consumer (if it's a request to fetch protected data)

This is how the functions in my code look like, however please have in mind that I've just implemented it and it's possible that I have something missed or forgotten, but generally I think the idea should be clear:

$this->dbModel is the object for working with database and save/retrieve token and consumer data

<?php

public function timestampNonceHandler ( $provider )
{
    return $this->dbModel->checkTimestampNonce ( $provider->consumer_key,
                                                 $provider->token,
                                                 $provider->timestamp,
                                                 $provider->nonce );
}

public function consumerHandler ( $provider )
{
    $consumer = $this->dbModel->getConsumerSecrets ($provider->consumer_key);

    if($consumer['consumer_key'] != $provider->consumer_key)
    {
        return OAUTH_CONSUMER_KEY_UNKNOWN;
    }

    if( (int)$consumer['disabled'] != 0 )
    {
        return OAUTH_CONSUMER_KEY_REFUSED;
    }

    $provider->consumer_id = $consumer['consumer_id']; # this is not required by OAuthProvider but I use it later in tokenHandler
    $provider->consumer_secret = $consumer['consumer_secret']; # this is REQUIRED

    return OAUTH_OK;
}

public function tokenHandler ( $provider )
{
    $token = $this->dbModel->getToken( $provider->token );

    if( time() > $token['expire'] )
    {
        return OAUTH_TOKEN_EXPIRED;
    }

    if($token['consumer_id'] != $provider->consumer_id)
    {
        return OAUTH_TOKEN_REJECTED;
    }

    if( (int)$token['authorized'] == 0 )
    {
        return OAUTH_TOKEN_REJECTED;
    }

    if($token['token_type'] != 'access')
    {
        if($token['verifier'] != $provider->verifier)
            return OAUTH_VERIFIER_INVALID;
    }

    $provider->token_id = $token['token_id']; # not required to be set by OAuthProvider
    $provider->token_secret = $token['token_secret']; # this is REQUIRED

    return OAUTH_OK;
}

?>


Constructs a new OAuthProvider object

  • ≪ OAuthProvider::checkOAuthRequest
  • OAuthProvider::consumerHandler ≫
  • PHP Manual
  • OAuthProvider
  • Constructs a new OAuthProvider object

OAuthProvider::__construct

(PECL OAuth >= 1.0.0)

OAuthProvider::__construct — Constructs a new OAuthProvider object

설명

public OAuthProvider::__construct ([ array $params_array ] )

Initiates a new OAuthProvider object.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

params_array

Setting these optional parameters is limited to the CLI SAPI.

반환값

An OAuthProvider object.

예제

Example #1 OAuthProvider::__construct() example

<?php
try {

    $op = new OAuthProvider();

    // Uses user-defined callback functions
    $op->consumerHandler(array($this, 'lookupConsumer'));
    $op->timestampNonceHandler(array($this, 'timestampNonceChecker'));
    $op->tokenHandler(array($this, 'myTokenHandler'));

    // Ignore the foo_uri parameter
    $op->setParam('foo_uri', NULL);

    // No token needed for this end point
    $op->setRequestTokenPath('/v1/oauth/request_token');

    $op->checkOAuthRequest();

} catch (OAuthException $e) {

    echo OAuthProvider::reportProblem($e);
}
?>

참고

  • OAuthProvider::setParam() - Set a parameter

User Contributed Notes

There are no user contributed notes for this page.


Set the consumerHandler handler callback

  • ≪ OAuthProvider::__construct
  • OAuthProvider::generateToken ≫
  • PHP Manual
  • OAuthProvider
  • Set the consumerHandler handler callback

OAuthProvider::consumerHandler

(PECL OAuth >= 1.0.0)

OAuthProvider::consumerHandler — Set the consumerHandler handler callback

설명

public void OAuthProvider::consumerHandler ( callable $callback_function )

Sets the consumer handler callback, which will later be called with OAuthProvider::callConsumerHandler().

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

callback_function

The callable functions name.

반환값

값을 반환하지 않습니다.

예제

Example #1 Example OAuthProvider::consumerHandler() callback

<?php
function lookupConsumer($provider) {

    if ($provider->consumer_key === 'unknown') {
        return OAUTH_CONSUMER_KEY_UNKNOWN;
    } else if($provider->consumer_key == 'blacklisted' || $provider->consumer_key === 'throttled') {
        return OAUTH_CONSUMER_KEY_REFUSED;
    }

    $provider->consumer_secret = "the_consumers_secret";

    return OAUTH_OK;
}
?>

참고

  • OAuthProvider::callConsumerHandler() - Calls the consumerNonceHandler callback

User Contributed Notes

There are no user contributed notes for this page.


Generate a random token

  • ≪ OAuthProvider::consumerHandler
  • OAuthProvider::is2LeggedEndpoint ≫
  • PHP Manual
  • OAuthProvider
  • Generate a random token

OAuthProvider::generateToken

(PECL OAuth >= 1.0.0)

OAuthProvider::generateToken — Generate a random token

설명

final public static string OAuthProvider::generateToken ( int $size [, bool $strong = false ] )

Generates a string of pseudo-random bytes.

인수

size

The desired token length, in terms of bytes.

strong

Setting to TRUE means /dev/random will be used for entropy, as otherwise the non-blocking /dev/urandom is used. This parameter is ignored on Windows.

반환값

The generated token, as a string of bytes.

오류/예외

If the strong parameter is TRUE, then an E_WARNING level error will be emitted when the fallback rand() implementation is used to fill the remaining random bytes (e.g., when not enough random data was found, initially).

예제

Example #1 OAuthProvider::generateToken() example

<?php
$p = new OAuthProvider();

$t = $p->generateToken(4);

echo strlen($t),  PHP_EOL;
echo bin2hex($t), PHP_EOL;

?>

위 예제의 출력 예시:

4
b6a82c27

주의

Note:

When not enough random data is available to the system, this function will fill the remaining random bytes using the internal PHP rand() implementation.

참고

  • openssl_random_pseudo_bytes() - Generate a pseudo-random string of bytes
  • mcrypt_create_iv() - Creates an initialization vector (IV) from a random source

User Contributed Notes

carlosouza at me dot com 28-Mar-2012 11:32
Be careful when setting the 'strong' parameter to true.

If you system doesn't have enough entropy your script will block which can cause timeouts in other parts of your code.

In my case, the most serious symptom was my script blocking when trying to read from /dev/random and causing a 'MySQL has gone away' error.

Hopefully this saves someone the trouble when deciding to use /dev/random entropy


is2LeggedEndpoint

  • ≪ OAuthProvider::generateToken
  • OAuthProvider::isRequestTokenEndpoint ≫
  • PHP Manual
  • OAuthProvider
  • is2LeggedEndpoint

OAuthProvider::is2LeggedEndpoint

(PECL OAuth >= 1.0.0)

OAuthProvider::is2LeggedEndpoint — is2LeggedEndpoint

설명

public void OAuthProvider::is2LeggedEndpoint ( mixed $params_array )

The 2-legged flow, or request signing. It does not require a token.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

params_array

반환값

An OAuthProvider object.

예제

Example #1 OAuthProvider::is2LeggedEndpoint() example

<?php

$provider = new OAuthProvider();

$provider->is2LeggedEndpoint(true);

?>

참고

  • OAuthProvider::__construct() - Constructs a new OAuthProvider object

User Contributed Notes

There are no user contributed notes for this page.


Sets isRequestTokenEndpoint

  • ≪ OAuthProvider::is2LeggedEndpoint
  • OAuthProvider::removeRequiredParameter ≫
  • PHP Manual
  • OAuthProvider
  • Sets isRequestTokenEndpoint

OAuthProvider::isRequestTokenEndpoint

(PECL OAuth >= 1.0.0)

OAuthProvider::isRequestTokenEndpoint — Sets isRequestTokenEndpoint

설명

public void OAuthProvider::isRequestTokenEndpoint ( bool $will_issue_request_token )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

will_issue_request_token

Sets whether or not it will issue a request token, thus determining if OAuthProvider::tokenHandler() needs to be called.

반환값

값을 반환하지 않습니다.

참고

  • OAuthProvider::setRequestTokenPath() - Set request token path
  • OAuthProvider::reportProblem() - Report a problem

User Contributed Notes

There are no user contributed notes for this page.


Remove a required parameter

  • ≪ OAuthProvider::isRequestTokenEndpoint
  • OAuthProvider::reportProblem ≫
  • PHP Manual
  • OAuthProvider
  • Remove a required parameter

OAuthProvider::removeRequiredParameter

(PECL OAuth >= 1.0.0)

OAuthProvider::removeRequiredParameter — Remove a required parameter

설명

final public bool OAuthProvider::removeRequiredParameter ( string $req_params )

Removes a required parameter.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

req_params

The required parameter to be removed.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • OAuthProvider::setParam() - Set a parameter
  • OAuthProvider::addRequiredParameter() - Add required parameters

User Contributed Notes

There are no user contributed notes for this page.


Report a problem

  • ≪ OAuthProvider::removeRequiredParameter
  • OAuthProvider::setParam ≫
  • PHP Manual
  • OAuthProvider
  • Report a problem

OAuthProvider::reportProblem

(PECL OAuth >= 1.0.0)

OAuthProvider::reportProblem — Report a problem

설명

final public static string OAuthProvider::reportProblem ( string $oauthexception [, bool $send_headers = true ] )

Pass in a problem as an OAuthException, with possible problems listed in the OAuth constants section.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

oauthexception

The OAuthException.

반환값

값을 반환하지 않습니다.

참고

  • OAuthProvider::checkOAuthRequest() - Check an oauth request
  • OAuthProvider::isRequestTokenEndpoint() - Sets isRequestTokenEndpoint

User Contributed Notes

There are no user contributed notes for this page.


Set a parameter

  • ≪ OAuthProvider::reportProblem
  • OAuthProvider::setRequestTokenPath ≫
  • PHP Manual
  • OAuthProvider
  • Set a parameter

OAuthProvider::setParam

(PECL OAuth >= 1.0.0)

OAuthProvider::setParam — Set a parameter

설명

final public bool OAuthProvider::setParam ( string $param_key [, mixed $param_val ] )

Sets a parameter.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

param_key

The parameter key.

param_val

The optional parameter value.

To exclude a parameter from signature verification, set its value to NULL.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • OAuthProvider::addRequiredParameter() - Add required parameters

User Contributed Notes

There are no user contributed notes for this page.


Set request token path

  • ≪ OAuthProvider::setParam
  • OAuthProvider::timestampNonceHandler ≫
  • PHP Manual
  • OAuthProvider
  • Set request token path

OAuthProvider::setRequestTokenPath

(PECL OAuth >= 1.0.0)

OAuthProvider::setRequestTokenPath — Set request token path

설명

final public bool OAuthProvider::setRequestTokenPath ( string $path )

Sets the request tokens path.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

path

The path.

반환값

TRUE

참고

  • OAuthProvider::tokenHandler() - Set the tokenHandler handler callback

User Contributed Notes

There are no user contributed notes for this page.


Set the timestampNonceHandler handler callback

  • ≪ OAuthProvider::setRequestTokenPath
  • OAuthProvider::tokenHandler ≫
  • PHP Manual
  • OAuthProvider
  • Set the timestampNonceHandler handler callback

OAuthProvider::timestampNonceHandler

(PECL OAuth >= 1.0.0)

OAuthProvider::timestampNonceHandler — Set the timestampNonceHandler handler callback

설명

public void OAuthProvider::timestampNonceHandler ( callable $callback_function )

Sets the timestamp nonce handler callback, which will later be called with OAuthProvider::callTimestampNonceHandler(). Errors related to timestamp/nonce are thrown to this callback.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

callback_function

The callable functions name.

반환값

값을 반환하지 않습니다.

예제

Example #1 Example OAuthProvider::timestampNonceHandler() callback

<?php
function timestampNonceChecker($provider) {

    if ($provider->nonce === 'bad') {
        return OAUTH_BAD_NONCE;
    } elseif ($provider->timestamp == '0') {
        return OAUTH_BAD_TIMESTAMP;
    }

    return OAUTH_OK;
}
?>

참고

  • OAuthProvider::callTimestampNonceHandler() - Calls the timestampNonceHandler callback

User Contributed Notes

There are no user contributed notes for this page.


Set the tokenHandler handler callback

  • ≪ OAuthProvider::timestampNonceHandler
  • OAuthException ≫
  • PHP Manual
  • OAuthProvider
  • Set the tokenHandler handler callback

OAuthProvider::tokenHandler

(PECL OAuth >= 1.0.0)

OAuthProvider::tokenHandler — Set the tokenHandler handler callback

설명

public void OAuthProvider::tokenHandler ( callable $callback_function )

Sets the token handler callback, which will later be called with OAuthProvider::callTokenHandler().

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

callback_function

The callable functions name.

반환값

값을 반환하지 않습니다.

예제

Example #1 Example OAuthProvider::tokenHandler() callback

<?php
function tokenHandler($provider) {

    if ($provider->token === 'rejected') {
        return OAUTH_TOKEN_REJECTED;
    } elseif ($provider->token === 'revoked') {
        return OAUTH_TOKEN_REVOKED;
    }

    $provider->token_secret = "the_tokens_secret";
    return OAUTH_OK;
}
?>

참고

  • OAuthProvider::callTokenHandler() - Calls the tokenNonceHandler callback

User Contributed Notes

There are no user contributed notes for this page.


OAuthException class

(PECL OAuth >= 0.99.1)

소개

This exception is thrown when exceptional errors occur while using the OAuth extension and contains useful debugging information.

클래스 개요

OAuthException extends Exception {
/* 프로퍼티 */
public $lastResponse ;
public $debugInfo ;
/* 상속된 프로퍼티 */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* 상속된 메소드 */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}

프로퍼티

lastResponse

The response of the exception which occurred, if any

debugInfo

User Contributed Notes

There are no user contributed notes for this page.


SCA

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
    • The structure of a Service Component
    • Obtaining a proxy for another Service Component
    • Calling another Service Component
    • Locating and calling services from a script which is not an SCA Component
    • Exposing a Service Component as a Web service
    • Deploying an SCA component
    • Obtaining the WSDL for an SCA component offering a Service as a Web service
    • Understanding how the WSDL is generated
    • Working with Data Structures
    • Error handling
  • SCA 함수 목록
    • SCA::createDataObject — create an SDO
    • SCA::getService — Obtain a proxy for a service
    • SCA_LocalProxy::createDataObject — create an SDO
    • SCA_SoapProxy::createDataObject — create an SDO

User Contributed Notes

There are no user contributed notes for this page.


소개

Warning

이 확장 모듈은 실험적입니다. 이 확장 모듈의 작동(함수의 이름과 이 확장 모듈에 관련된 모든 문서 자료)은 이후의 PHP 릴리즈에서 예고 없이 변경될 수 있습니다. 이 확장 모듈의 사용은 사용자의 책임하에 이루어집니다.

SCA for PHP makes it possible for a PHP programmer to write reusable components, which can be called in a variety of ways, with an identical interface and with a minimum of fuss. At present components can call each other either locally or via Web services, but in the future it is expected that other ways will be possible. It provides the programmer with a way of doing this which will look as natural as possible in PHP.

SCA components use phpDocumentor-style (see http://www.phpdoc.org/) annotations to declare dependencies on other SCA components or Web services. The SCA for PHP runtime resolves these dependencies at runtime on behalf of the components, and thus allows the PHP programmer to focus on the business logic rather than on locating and obtaining references to dependencies.

The SCA for PHP programming model can be extended to support a number of service types, such as REST and Atompub. However, Web services (more accurately, WSDL defined, SOAP/HTTP services), are the only type currently specified.

Components also use annotations to define the interface which they expose as a service. The SCA for PHP runtime will automatically generate WSDL from these annotations, so that an SCA component is easily exposed as a web service. These annotations are a natural extension to those provided by phpDocumentor. Deploying a Web service can be as simple as placing a PHP component under the document root of a web server.

Components also use annotations to specify data structures (expressed using XML schema complex types) which are then handled using Service Data Objects (SDOs).

A PHP script which is not an SCA component and which contains no annotations can use the services of an SCA component. A PHP script or component can make calls to a web service that is not an SCA component, but using the same system of calls or annotations to obtain a reference.

First we show a single SCA component, ConvertedStockQuote which illustrates many of the features of SCA for PHP. It has one method, getQuote(), which given a stock "ticker" obtains a price quote for that stock, converted to a given currency. We shall be using this example as a basis for explaining the SCA for PHP throughout the rest of this document.

Example #1 A sample SCA component

<?php

include "SCA/SCA.php";

/**
 * Calculate a stock price for a given ticker symbol in a given currency.
 *
 * @service
 * @binding.soap
 */
class ConvertedStockQuote {

    /**
     * The currency exchange rate service to use.
     *
     * @reference
     * @binding.php ../ExchangeRate/ExchangeRate.php
     */
    public $exchange_rate;

    /**
     * The stock quote service to use.
     *
     * @reference
     * @binding.soap ../StockQuote/StockQuote.wsdl
     */
    public $stock_quote;

    /**
     * Get a stock quote for a given ticker symbol in a given currency.
     *
     * @param string $ticker The ticker symbol.
     * @param string $currency What currency to convert the value to.
     * @return float The stock value is the target currency.
     */
    function getQuote($ticker, $currency)
    {
        $quote  = $this->stock_quote->getQuote($ticker);
        $rate   = $this->exchange_rate->getRate($currency);
        return  $rate * $quote;
    }
}
?>

In this example, we see that an SCA component is implemented by a script containing a PHP class and includes SCA.php. The class contains a mixture of business logic and references to other components or services. In the illustrated getQuote() method there is only business logic, but it relies on the instance variables $stock_quote and $exchange_rate having been initialized. These refer to two other components and will be initialized by the SCA runtime with proxies for these two services, whenever this component executes. The annotations for these two services show one to be a local component, which will be called within the same PHP runtime, and one to be a remote component which will be called via a SOAP request. This component also exposes the getQuote() method both locally and as a web service, so it in turn can be called either locally or remotely.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

If you want to use SCA to consume or produce Web services then you need PHP 5.2.0 or later, built with the soap extension enabled. If you just want to use local components, and do not wish to use the Web service bindings, then this version of SCA for PHP will also run with PHP 5.1.6.

SCA is packaged along with SDO in one combined package on PECL. See http://www.php.net/sdo#sdo.installation for installing the SCA_SDO package from PECL. The SCA code must be on the include path of your PHP installation, for example if it is installed as /usr/local/lib/php/SCA, the include_path directive must include /usr/local/lib/php

User Contributed Notes

There are no user contributed notes for this page.


설치

PECL 확장 설치에 관한 정보는 매뉴얼의 PECL 확장 설치에 있습니다. 새 릴리즈, 내려받기, 소스파일, 개발자 정보, CHANGELOG 등의 추가 정보는 여기에 있습니다: » http://pecl.php.net/package/sca.

이 PECL 확장에 대한 DLL은 현재 사용할 수 없습니다. 윈도우에서 빌드하기 섹션을 참고하십시오.

User Contributed Notes

There are no user contributed notes for this page.


예제

Table of Contents

  • The structure of a Service Component
  • Obtaining a proxy for another Service Component
  • Calling another Service Component
  • Locating and calling services from a script which is not an SCA Component
  • Exposing a Service Component as a Web service
  • Deploying an SCA component
  • Obtaining the WSDL for an SCA component offering a Service as a Web service
  • Understanding how the WSDL is generated
  • Working with Data Structures
  • Error handling

The examples in the subsequent sections illustrate the following aspects of PHP for SCA:

  • How PHP annotations are used to define PHP classes as SCA components, and how annotations are used to define the services.

  • How an SCA component can be exposed as a Web service

  • How an SCA component can consume a Web service, whether provided by another SCA component or by some other service which knows nothing of SCA

  • How an SCA component can call another SCA component locally (within the same process and on the same call stack)

  • How a client script which is not an SCA component can use the getService call to obtain a proxy for an SCA component.

  • How data structures such as Addresses, or Puchase Orders, are represented as Service Data Objects, and handled.

  • How SCA components are deployed, and in particular how and when WSDL is generated for a service.

  • How parameters are always passed by value (and not by reference) between components, even when the calls are local. This ensures that the semantics of a call do not change depending on the location of a component.

  • How positional parameters to a service are supported, even when the underlying WSDL is document literal wrapped, and naturally supports only named parameters.

  • How business and runtime exceptions are handled.

User Contributed Notes

There are no user contributed notes for this page.


The structure of a Service Component

A service component is implemented by a class. To identify it as a service component, it contains an @service annotation. The SCA runtime will use the file name of the script to determine the component name, by convention. The class and script file must therefore share the same name.

PHP SCA components always expose a service, and there is no way for a component to be invoked other than to be called as a result of a Web service request, or called directly from another component or from a script. For this reason a valid PHP SCA component will always contain an @service annotation and at least one public method.

Each SCA Component requires that the SCA.php script is included. As well as containing the definition of the SCA class, this script contains executable PHP code that will run whenever the script is called, and which will be responsible for making the component behave as needed.

Caution

It is very important that if your file contains other includes, they come before the include for SCA.php. If there are includes after the include for SCA.php, they will not have been processed when the SCA runtime runs your class.

The example below illustrates this overall structure

Example #1 The structure of an SCA for PHP component

<?php

// any includes

include "SCA/SCA.php";

/**
 * @service
 */

class ConvertedStockQuote {

       // instance variables, business logic, including at least one public method

}
?>

User Contributed Notes

There are no user contributed notes for this page.


Obtaining a proxy for another Service Component

One SCA component can call the service provided by another SCA component. The service a component provides is made up of all of its public methods. SCA for PHP currently provides two ways for one component to call another: either locally (i.e. within the same PHP run-time, and on the same call stack) or remotely if the called component exposes a Web service binding.

In order for one component to call another, the calling component needs a proxy for the called component. This proxy is usually provided as an instance variable in the calling component, though proxies can also be obtained with the SCA::getService() call, as we shall see later. When a component is constructed, proxies are constructed for any instance variable which refer to another component, and these proxies are "injected" into the instance variables. Proxies are always used, whether the component is local or remote, in order to provide identical calling behavior between remote and local calls (for example, local calls are made to always pass data by-value). The proxies know how to locate the required component and to pass the calls made on to them.

Instance variables which are intended to hold proxies for services are indicated by the two PHPDocumentor-style annotations, @reference and @binding. Both annotations are placed in the documentation section for a class instance variable, as shown by the code below.

The @reference annotation before an instance variable indicates that the instance variable is to be initialized with a proxy to a component.

The @binding annotation has two forms @binding.php and @binding.soap, and indicates that the proxy is either for a local component or for a Web service respectively. For both @binding.php and @binding.soap, the annotation gives a target URI.

At the moment, with the annotation-based method of specifying dependencies, the only way to alter the intended target of a reference is to alter the annotation within the component.

In our ConvertedStockQuote example, the $exchange_rate instance variable will be initialized with a proxy to the local ExchangeRate component whenever an instance of the ConvertedStockQuote is constructed.

Example #1 Obtaining a proxy for a local PHP class

<?php
   /**
     * The currency exchange rate service to use.
     *
     * @reference
     * @binding.php ../ExchangeRate/ExchangeRate.php
     */
    public $exchange_rate;
?>

For @binding.php, the URI identifies the location of the script containing the implementation of the component. The component will be called locally. The service provided is the set of public methods of the component. The URI must be a simple pathname, either absolute or relative. The component will be loaded with the PHP include directive, after testing to see if it is already loaded with class_exists(). If the URI is a relative path, it is resolved relative to the component containing the annotation. Note that this is different from the normal PHP behaviour where scripts would be looked for along the PHP include_path, This is intended to provide some location-independence for cross-component references.

If this ExchangeRate service were remote and to be called as a Web service, only the @binding line changes. Instead of giving the location of a PHP class, it gives the location of the WSDL describing the web service. In our example component, this is illustrated by the second reference:

Example #2 Obtaining a proxy for a web service

<?php
/**
     * The stock quote service to use.
     *
     * @reference
     * @binding.soap ../StockQuote/StockQuote.wsdl
     */
    public $stock_quote;
?>

The StockQuote component will be called via a Web service request. In this case the URI for the WSDL can be a simple pathname, or may contain a PHP wrapper and begin, for example, with file:// or http://. In the event that it is a simple pathname, it can be absolute or relative, and if relative will be resolved relative to the component containing the annotation. Note that this is like the behaviour for @binding.php, and different from the normal PHP behaviour where the file would be looked for relative to the PHP current working directory, which would usually be the location of the first script to be called. This behaviour is intended to give consistency across the different bindings and to provide some location-independence for references between components.

User Contributed Notes

There are no user contributed notes for this page.


Calling another Service Component

The ConvertedStockQuote example also calls the proxies for the two components to which it refers.

Example #1 Calling services

<?php
$quote  = $this->stock_quote->getQuote($ticker);
$rate   = $this->exchange_rate->getRate($currency);
?>

The call to the StockQuote service is a call to a local service; the call to the ExchangeRate service is a call to a remote service. Note that the way the call is made looks the same regardless of whether the call is to a local service or a remote one.

The proxies which have been injected ensure that the way calls to components look and behave are the same way regardless of whether they are to a local or remote service, so that components are not sensitive to whether a call is to a local or a remote service. For example, the proxy for a local service takes copies of the arguments and passes only those copies, to ensure that calls are made to be pass-by-value, as they would be for a remote call. Also, the proxy for a remote service takes the arguments from a positional parameter list and ensures they are packaged properly in a SOAP request and converted back to a positional parameter list at the far end.

In the example above, the $ticker and $currency are clearly PHP scalar types. Components can pass the PHP scalar types string, integer, float and boolean, but data structures on service calls are always passed as Service Data Objects (SDOs). A later section describes how a component can create an SDO to pass on a local or Web service call, or how a component can create an SDO to return. The PHP SDO project documentation describes how to work with the SDO APIs (see the SDO pages).

User Contributed Notes

There are no user contributed notes for this page.


Locating and calling services from a script which is not an SCA Component

SCA components obtain proxies for other components or services as instance variables annotated with @reference, but this is not possible for a script that is not itself also a component. A client script which is not a component must use the SCA::getService() static method to obtain a proxy for a service, whether local or remote. The getService() method takes a URI as the argument. Typically this is the location of a local PHP script containing a component, or of a wsdl file, and is used in exactly the same way as the targets of the @binding annotations described in the previous section: that is, relative URIs are resolved against the location of the client script and not against the PHP include_path or current working directory.

For example, a script that needed to obtain proxies for the ExchangeRate and StockQuote services but was not a component would use the getService() method as follows:

Example #1 Obtaining a proxy using getService

<?php
$exchange_rate = SCA::getService('../ExchangeRate/ExchangeRate.php');
$stock_quote   = SCA::getService('../StockQuote/StockQuote.wsdl');
?>

Methods on services can then be called on the returned proxy, just as they can in a component.

Example #2 Making calls on the proxy

<?php
$quote  = $stock_quote->getQuote($ticker);
$rate   = $exchange_rate->getRate($currency);
?>

User Contributed Notes

There are no user contributed notes for this page.


Exposing a Service Component as a Web service

SCA for PHP can generate WSDL from the annotations within a service component, so that it can be easily deployed and exposed as a Web service. To provide SCA with the information it needs to generate the WSDL, it is necessary to add the annotation @binding.soap under the @service annotation and to specify the parameters and return values of the methods using the @param and @return annotations. These annotations will be read when WSDL is generated, and the order and types of the parameters determine the contents of the <schema> section of the WSDL.

SCA for PHP always generates document/literal wrapped WSDL for components that are exposing a Web service. Note that this does not stop components from consuming Web services which are not SCA components and which are documented with WSDL written in a different style.

The scalar types which can be used in the @param annotation are the four common PHP scalar types: boolean, integer, float and string. These are simply mapped to the XML schema types of the same name in the WSDL. The example below, which is a trivial implementation of the StockQuote service that the ConvertedStockQuote component calls, illustrates string and float types.

Example #1 StockQuote Service

<?php

include "SCA/SCA.php";

/**
 * Scaffold implementation for a remote StockQuote Web service.
 *
 * @service
 * @binding.soap
 *
 */
class StockQuote {

    /**
     * Get a stock quote for a given ticker symbol.
     *
     * @param string $ticker The ticker symbol.
     * @return float The stock quote.
     */
    function getQuote($ticker) {
        return 80.9;
  }
}
?>

WSDL much like the following (though with a service location other than 'localhost', probably) would be generated from this service:

Example #2 Generated WSDL

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xsi:type="tDefinitions"
    xmlns:tns2="http://StockQuote" xmlns:tns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:tns3="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" targetNamespace="http://StockQuote">
  <types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://StockQuote">
      <xs:element name="getQuote">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="ticker" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="getQuoteResponse">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="getQuoteReturn" type="xs:float"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
  </types>

  <message name="getQuoteRequest">
    <part name="getQuoteRequest" element="tns2:getQuote"/>
  </message>
  <message name="getQuoteResponse">
    <part name="return" element="tns2:getQuoteResponse"/>
  </message>
  <portType name="StockQuotePortType">
    <operation name="getQuote">
      <input message="tns2:getQuoteRequest"/>
      <output message="tns2:getQuoteResponse"/>
    </operation>
  </portType>
  <binding name="StockQuoteBinding" type="tns2:StockQuotePortType">
    <operation name="getQuote">
      <input>
        <tns3:body xsi:type="tBody" use="literal"/>
      </input>
      <output>
        <tns3:body xsi:type="tBody" use="literal"/>
      </output>
      <tns3:operation xsi:type="tOperation" soapAction=""/>
    </operation>
    <tns3:binding xsi:type="tBinding" transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
  </binding>
  <service name="StockQuoteService">
    <port name="StockQuotePort" binding="tns2:StockQuoteBinding">
      <tns3:address xsi:type="tAddress" location="http://localhost/StockQuote/StockQuote.php"/>
    </port>
  </service>
</definitions>

<!-- this line identifies this file as WSDL generated by SCA for PHP. Do not remove -->

User Contributed Notes

deadguysfrom at gmail dot com 31-Jul-2009 08:44
In order for SCA to find the service class, the script filename must match the service class name.  In this case, StockQuote.php


Deploying an SCA component

There are no special steps needed to deploy a PHP SCA component. It is sufficient to place the component PHP script in its proper place under the web server document root, just like any other PHP script. It is the SCA::initComponent() executable line within each component that will be executed whenever the script is called, and which will be responsible for making the component respond appropriately to Web service calls, local calls, or requests for WSDL.

User Contributed Notes

There are no user contributed notes for this page.


Obtaining the WSDL for an SCA component offering a Service as a Web service

SCA components that expose a Web service interface (i.e. have an @binding.soap annotation) will return their WSDL definition in response to an HTTP request with a get parameter of "wsdl". The usual way to obtain this is with "?wsdl" on the end of a URL. The example below uses file_get_contents() to obtain WSDL from a service and writes it to a temporary file before then obtaining a proxy for the service in the usual way. You could of course also obtain the WSDL in a browser, or by some other means, and save the file yourself.

Example #1 Generated WSDL

<?php
$wsdl = file_get_contents('http://www.example.com/Services/Example.php?wsdl');
file_put_contents("service.wsdl",$wsdl); //write the wsdl to a file
$service = SCA::getService('service.wsdl');
?>

NOTE: If the wsdl requires imported xsds, these will need to be fetched separately.

User Contributed Notes

There are no user contributed notes for this page.


Understanding how the WSDL is generated

SCA for PHP generates WSDL for components which contain an @binding.soap annotation after the @service annotation. To generate WSDL, the SCA runtime reflects on the component and examines the @param and @return annotations for each public method, as well as any @types annotations within the component. The information from the @param and @return annotations is used to build the <types> section of the WSDL. Any @types annotations which specify a separate schema file will result in an <import> element for that schema within the WSDL.

Location attribute of the <service> element

At the bottom of the WSDL is the <service> element which uses the location attribute to identify the URL of the service. For example this might look as follows:

Example #1 location attribute

<service name="ConvertedStockQuote"
...
location="http://localhost/ConvertedStockQuote/ConvertedStockQuote.php"/>

Note that this location is relative to the document root of the web server, and cannot be worked out in advance. It can only be worked out once the component is in its proper place under a running web server, when the hostname and port can be known and placed in the WSDL. Detail from the URL that requests the WSDL is used, so for example if the WSDL is generated in response to a request to http://www.example.com:1111/ConvertedStockQuote/ConvertedStockQuote.php?wsdl, a location of http://www.example.com:1111/ConvertedStockQuote/ConvertedStockQuote.php is what will be inserted into the location attribute in the WSDL.

Document/literal wrapped WSDL and positional parameters

SCA for PHP generates WSDL in the document/literal wrapped style. This style encloses the parameters and return types of a method in 'wrappers' which are named after the corresponding method. The <types> element at the top of the WSDL defines each of these wrappers. If we consider the getQuote() method of the ConvertedStockQuote example:

Example #2 method with two arguments

<?php
   /**
     * Get a stock quote for a given ticker symbol in a given currency.
     *
     * @param string $ticker The ticker symbol.
     * @param string $currency What currency to convert the value to.
     * @return float The stock value is the target currency.
     */
    function getQuote($ticker, $currency)
    {
        $quote  = $this->stock_quote->getQuote($ticker);
        $rate   = $this->exchange_rate->getRate($currency);
        return  $rate * $quote;
    }
?>

The WSDL generated to define this method will name both the method and the parameters, and give an XML schema type for the parameters. The types section of the WSDL looks like this:

Example #3 types section illustrating named parameters

<types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://ConvertedStockQuote">
      <xs:element name="getQuote">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="ticker" type="xs:string"/>
            <xs:element name="currency" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="getQuoteResponse">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="getQuoteReturn" type="xs:float"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
  </types>

The SCA run-time has special processing to handle how positional parameter lists in the interface are converted to XML containing named parameters in the soap request, and then back to positional parameter lists again. To see why this matters, consider how a PHP script which used a different interface to make a SOAP call would need to construct the parameter list. A PHP script using the PHP SoapClient, for example, would need to pass the SoapClient a single parameter giving the values for "ticker" and "currency", perhaps as an associative array. To insist that SCA components construct parameter lists to make Web service calls in this way would be to make local and remote calls look different, so a different approach is needed.

When SCA generates WSDL for an SCA component it includes a comment in the WSDL which marks that WSDL as being the interface for an SCA component. In this case, when one SCA component calls another through a Web service, the SCA runtime on the calling end takes the positional parameter list from the call and assigns the values one by one to the named elements in the soap message. For example a call to the getQuote() method defined above that passes the values 'IBM' and 'USD' and looks like this:

<?php
  $quote = $remote_service->getQuote('IBM','USD');
?>

will result in a soap message containing the following:

<getQuote>
  <ticker>IBM</ticker>
  <currency>USD</currency>
</getQuote>

On the service-providing end, the SCA run-time takes the parameters one by one from the soap message and forms a positional parameter list from them, re-forming the argument list ('IBM','USD').

Caution

At both ends the SCA runtime relies on the order in which the parameters appear in the soap message being the same as that in the target method's parameter list. This is ultimately determined by the order of the @param annotations: this determines the order in which the parameters appear in the WSDL and thereby the order in which they appear in the soap message. Therefore it is essential that the order of the @param annotations matches that of the parameters in the method's parameter list.

User Contributed Notes

There are no user contributed notes for this page.


Working with Data Structures

SCA components can pass and return the four PHP scalar types boolean, integer, float and string, but to pass or return data structures, SCA components use Service Data Objects (SDOs). SDOs are described in much more detail in the SDO pages of this manual. Readers familiar with SDOs will know that they are suitable for representing the sort of structured and semi-structured data that is frequently modeled in XML, and that they serialize very naturally for passing between remote components, or in Web services. SDOs are presently the only supported way to pass and return data structures. It is not possible to pass or return PHP objects, or PHP arrays.

The SCA runtime always assures data is passed by-value, even for local calls. To do this, the SCA runtime copies any SDOs in the parameter list before passing them on, just as it does for scalar types.

How data structures are defined to SCA components

Currently the only mechanism for specifying the location of a data structure definition is by specifying the types in an XML schema file. However, in the future it may be possible to define types in other ways, such as based on PHP classes or interfaces, or based on definitions expressed as associative arrays.

To illustrate the use of SDOs we introduce a new component. The PortfolioMangement service below returns an SDO representing a stock portfolio for a given customer.

Example #1 A Component that uses Data Structures

<?php

include "SCA/SCA.php";

/**
 * Manage the portfolio for a customer.
 *
 * @service
 * @binding.soap
 *
 * @types http://www.example.org/Portfolio PortfolioTypes.xsd
 *
 */
class PortfolioManagement {

    /**
     * Get the stock portfolio for a given customer.
     *
     * @param integer $customer_id The id for the customer
     * @return Portfolio http://www.example.org/Portfolio The stock portfolio (symbols and quantities)
     */
    function getPortfolio($customer_id) {
        // Pretend we just got this from a database
        $portfolio = SCA::createDataObject('http://www.example.org/Portfolio', 'Portfolio');
        $holding = $portfolio->createDataObject('holding');
        $holding->ticker = 'AAPL';
        $holding->number = 100.5;
        $holding = $portfolio->createDataObject('holding');
        $holding->ticker = 'INTL';
        $holding->number = 100.5;
        $holding = $portfolio->createDataObject('holding');
        $holding->ticker = 'IBM';
        $holding->number = 100.5;
        return $portfolio;
    }

}
?>

The @types annotation:

<?php
@types http://www.example.org/Portfolio PortfolioTypes.xsd
?>

indicates that types in the namespace http://www.example.org/Portfolio will be found in the schema file located by the URI PortfolioTypes.xsd. The generated WSDL would reproduce this information with an import statement as follows:

<xs:import schemaLocation="PortfolioTypes.xsd"
                      namespace="http://www.example.org/Portfolio"/>

so the URI, absolute or relative, must be one that can be resolved when included in the schemaLocation attribute.

Creating SDOs

Readers familiar with SDOs will know that they are always created according to a description of the permitted structure (sometimes referred to as the 'schema' or 'model') and that, rather than creating them directly using 'new', some form of data factory is needed. Often, an existing data object can be used as the data factory, but sometimes, and especially in order to get the first data object, something else must act as the data factory.

In SCA, either the SCA runtime class or the proxies for services, whether local or remote, can act as the data factories for SDOs. The choice of which to use, and when, is described in the next two sections.

We switch to a new example in order to illustrate the creation of SDOs, both to pass to a service, and to be returned from a service.

Creating an SDO to pass to a service

A caller of a service which requires a data structure to be passed in to it uses the proxy to the service as the data factory for the corresponding SDOs. For example, suppose a component makes use of a proxy for a service provided by a local AddressBook component.

<?php
/**
 * @reference
 * @binding.local AddressBook.php
 */
$address_book;
?>

The AddressBook component that it wishes to call is defined as follows:

<?php
/**
* @service
* @binding.soap
* @types http://addressbook ../AddressBook/AddressBook.xsd
*/
class AddressBook {

    /**
     * @param personType $person http://addressbook (a person object)
     * @return addressType http://addressbook (the address object for the person object)
     */
    function lookupAddress($person)  {
        ...
    }
}
?>

The AddressBook component provides a service method called lookupAddress() which uses types from the http://addressbook namespace. The lookupAddress method takes a personType data structure and returns an addressType. Both types are defined in the schema file addressbook.xsd.

Once the component that wishes to use the AddressBook component has been constructed, so that the $address_book instance variable contains a proxy for the service, the calling component can use the proxy in $address_book to create the person SDO, as shown below:

<?php
$william_shakespeare        = $address_book->createDataObject('http://addressbook','personType');
$william_shakespeare ->name = "William Shakespeare";
$address                    = $address_book->lookupAddress($william_shakespeare);
?>

Note, the use of the proxy as the means to create the SDO is not limited to SCA components. If a service is being called from a general PHP script, and the proxy was obtained with getService() then the same approach is used.

<?php
$address_book = SCA::getService('AddressBook.php');
$william_shakespeare = $address_book->createDataObject('http://addressbook','personType');
?>

Creating an SDO to return from a component

A component that needs to create a data object for return to a caller will not have a proxy to use as a data object, In this case it uses the createDataObject() static method on SCA.php. Hence if the AddressBook component described above needed to create an object of type addressType within the namespace http://addressbook, it might do so as follows:

<?php
$address = SCA::createDataObject('http://addressbook','addressType');
?>

User Contributed Notes

There are no user contributed notes for this page.


Error handling

This section describes how errors are handled. There are two types of errors:

  • SCA runtime exceptions are those that signal problems in the management of the execution of components, and in the interaction with remote services. These might occur due to network or configuration problems.

  • Business exceptions are those that are defined by the programmer. They extend the PHP Exception class, and are thrown and caught deliberately as part of the business logic.

Handling of Runtime exceptions

There are two types of SCA runtime exception:

  • SCA_RuntimeException - signals a problem found by or perhaps occurring within the SCA runtime. This can be thrown for a variety of reasons, many of which can occur regardless of whether a connection is being made to a local or a remote service: an error in one of the annotations within a component, a missing WSDL or php file, and so on. In the case of Web services, an SCA_RuntimeException can also be thrown if a SoapFault is received from a remote Web service and the fault code in the SoapFault indicates that a retry is unlikely to be successful.

  • SCA_ServiceUnavailableException - this is a subclass of SCA_RuntimeException and signals a problem in connecting to or using a remote service, but one which might succeed if retried. In the case of Web services, this exception is thrown if a SoapFault is received with a fault code that indicates that a retry might be successful.

Handling of Business exceptions

Business exceptions may be defined and thrown by a component in the normal way, regardless of whether the component has been called locally or remotely. The SCA runtime does not catch business exceptions that have been thrown by a component called locally, so they will be returned to a caller in the normal way. If a component has been called via a Web service, on the other hand, the SCA runtime on the service providing end does catch business exceptions, and will ensure these are passed back to the calling end and re-thrown. Assuming that the calling end has a definition of the exception (that is, is able to include a file containing the PHP class defining the exception) the re-thrown exception will contain the same details as the original, so that the getLine() and getFile() methods for example will contain the location where the exception was thrown within the business logic. The exception will be passed in the detail field of a soap fault with a fault code of "Client".

User Contributed Notes

There are no user contributed notes for this page.


SCA 함수 목록

예약 클래스

Most of the interface to SCA is through the annotations within SCA components so there are few public classes and methods. The only SCA classes that scripts or components can call are the SCA class itself, and the proxy classes SCA_LocalProxy and SCA_SoapProxy.

SCA

Much of the work of the SCA class is performed when the file SCA.php is included within an SCA component. However, a PHP script may include SCA.php and call the getService() method on the SCA class in order to obtain a proxy for a service. A component will not need to do this as proxies are obtained instead by defining an instance variable with the @reference annotation.

Components that need to create an SDO to return to a caller will need a data factory to call. For this purpose the SCA class supports the createDataObject() method, which will create an SDO according to the model defined by the component's @types annotations. The arguments to createDataObject() are the same as those to SDO's XML Data Access Service.

메쏘드

  • getService - obtain a proxy for a service

  • createDataObject - create an SDO

SCA_LocalProxy

When getService() is called with the target of a local PHP component, a local proxy is returned. A local proxy is also injected into the instance variables of a component that are defined with an @reference and an @binding.php anotation. When the script or component makes calls on the local proxy, they are passed on to the target component itself.

Components that need to create an SDO to pass to a component will need a data factory to call. For this purpose the SCA_LocalProxy class supports the createDataObject() method, which will create an SDO according to the model defined by the components' @types annotations. The arguments to the createDataObject() are the same as those to SDO's XML Data Access Service.

메쏘드

  • createDataObject - create an SDO

SCA_SoapProxy

When getService() is called with the target of a WSDL file, a SOAP proxy is returned. A SOAP proxy is also injected into the instance variables of a component that are defined with an @reference and an @binding.soap anotations. When the script or component makes calls on the SOAP proxy, they are formed into Web service SOAP requests and passed on to the target component, with the help of the PHP Soap extension.

Components that need to create an SDO to pass to a component will need a data factory to call. For this purpose the SCA_SoapProxy class supports the createDataObject() method, which will create an SDO according to the model defined within the target WSDL. The arguments to the createDataObject() are the same as those to SDO's XML Data Access Service.

메쏘드

  • createDataObject - create an SDO

Table of Contents

  • SCA::createDataObject — create an SDO
  • SCA::getService — Obtain a proxy for a service
  • SCA_LocalProxy::createDataObject — create an SDO
  • SCA_SoapProxy::createDataObject — create an SDO

User Contributed Notes

There are no user contributed notes for this page.


create an SDO

  • ≪ SCA 함수 목록
  • SCA::getService ≫
  • PHP Manual
  • SCA 함수 목록
  • create an SDO

SCA::createDataObject

(PECL SDO >= 0.5.0)

SCA::createDataObject — create an SDO

설명

SDO_DataObject SCA::createDataObject ( string $type_namespace_uri , string $type_name )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

This method is used inside an SCA component that needs to create an SDO to return. The parameters are the desired SDO's namespace URI and type name. The namespace and type must be defined in one of the schema files which are specified on the @types annotation within the component.

인수

type_namespace_uri

The namespace of the type.

type_name

The name of the type.

반환값

Returns the newly created SDO_DataObject.

오류/예외

SDO_TypeNotFoundException

Thrown if the namespaceURI and typeName do not correspond to a type in any of the schema files specified in the @types annotations.

User Contributed Notes

There are no user contributed notes for this page.


Obtain a proxy for a service

  • ≪ SCA::createDataObject
  • SCA_LocalProxy::createDataObject ≫
  • PHP Manual
  • SCA 함수 목록
  • Obtain a proxy for a service

SCA::getService

(PECL SDO >= 0.5.0)

SCA::getService — Obtain a proxy for a service

설명

mixed SCA::getService ( string $target [, string $binding [, array $config ]] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Examine the target and initialize and return a proxy of the appropriate sort. If the target is for a local PHP component the returned proxy will be an SCA_LocalProxy. If the target is for a WSDL file, the returned proxy will be a SCA_SoapProxy.

인수

target

An absolute or relative path to the target service or service description (e.g. a URL to a json-rpc service description, a PHP component, a WSDL file, and so on.). A relative path, if specified, is resolved relative to the location of the script issuing the getService() call, and not against the include_path or current working directory.

binding

The binding (i.e. protocol) to use to communicate with the service (e.g binding.jsonrpc for a json-rpc service). Note, some service types can be deduced from the target parameter (e.g. if the target parameter ends in .wsdl then SCA will assume binding.soap). Any binding which can be specified in an annotation can be specified here. For example 'binding.soap' is equivalent to the '@binding.soap' annotation.

config

Any additional configuration properties for the binding (e.g. array('location' => 'http://example.org')). Any binding configuration which can be specified in an annotation can be specified here. For example, 'location' is equivalent to the '@location' annotation to configure the location of a target soap service.

반환값

The SCA_LocalProxy or SCA_SoapProxy.

예제

Example #1 An SCA::getService() example

This example shows how to get a proxy to an email soap service described by EmailService.wsdl and located at http://example.org.

<?php
include 'SCA/SCA.php';
$service = SCA::getService('EmailService.wsdl', 'binding.soap', array('location' => 'http://example.org'));
$service->send(...);
?>

위 예제의 출력:

User Contributed Notes

There are no user contributed notes for this page.


create an SDO

  • ≪ SCA::getService
  • SCA_SoapProxy::createDataObject ≫
  • PHP Manual
  • SCA 함수 목록
  • create an SDO

SCA_LocalProxy::createDataObject

(PECL SDO >= 0.5.0)

SCA_LocalProxy::createDataObject — create an SDO

설명

SDO_DataObject SCA_LocalProxy::createDataObject ( string $type_namespace_uri , string $type_name )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

This method is used inside either an ordinary PHP script or an SCA component that needs to create an SDO to pass to a local service. The parameters are the desired SDO's namespace URI and type name. The namespace and type must be defined in the interface of the component that is to be called, so the namespace and type must be defined in one of the schema files which are specified on the @types annotation within the component for which the SCA_LocalProxy object is a proxy.

인수

type_namespace_uri

The namespace of the type.

type_name

The name of the type.

반환값

Returns the newly created SDO_DataObject.

오류/예외

SDO_TypeNotFoundException

Thrown if the namespaceURI and typeName do not correspond to a type in any of the schema files specified in the @types annotations within the component for which the SCA_LocalProxy object is a proxy..

User Contributed Notes

There are no user contributed notes for this page.


create an SDO

  • ≪ SCA_LocalProxy::createDataObject
  • SOAP ≫
  • PHP Manual
  • SCA 함수 목록
  • create an SDO

SCA_SoapProxy::createDataObject

(PECL SDO >= 0.5.0)

SCA_SoapProxy::createDataObject — create an SDO

설명

SDO_DataObject SCA_SoapProxy::createDataObject ( string $type_namespace_uri , string $type_name )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

This method is used inside either an ordinary PHP script or an SCA component that needs to create an SDO to pass to a web service. The parameters are the desired SDO's namespace URI and type name. The namespace and type must be defined in the interface of the component that is to be called, so the namespace and type must be defined within the WSDL for the web service. If the web service is also an SCA component then the types will have been defined within one of the schema files which are specified on the @types annotation within the component for which the SCA_SoapProxy object is a proxy.

인수

type_namespace_uri

The namespace of the type.

type_name

The name of the type.

반환값

Returns the newly created SDO_DataObject.

오류/예외

SDO_TypeNotFoundException

Thrown if the namespaceURI and typeName do not correspond to a type found in the WSDL that was used to initialise this SCA_SoapProxy.

User Contributed Notes

There are no user contributed notes for this page.


SOAP

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • SOAP 함수 목록
    • is_soap_fault — Checks if a SOAP call has failed
    • use_soap_error_handler — Set whether to use the SOAP error handler
  • SoapClient — The SoapClient class
    • SoapClient::__call — Calls a SOAP function (deprecated)
    • SoapClient::__construct — SoapClient constructor
    • SoapClient::__doRequest — Performs a SOAP request
    • SoapClient::__getFunctions — Returns list of available SOAP functions
    • SoapClient::__getLastRequest — Returns last SOAP request
    • SoapClient::__getLastRequestHeaders — Returns the SOAP headers from the last request
    • SoapClient::__getLastResponse — Returns last SOAP response
    • SoapClient::__getLastResponseHeaders — Returns the SOAP headers from the last response
    • SoapClient::__getTypes — Returns a list of SOAP types
    • SoapClient::__setCookie — The __setCookie purpose
    • SoapClient::__setLocation — Sets the location of the Web service to use
    • SoapClient::__setSoapHeaders — Sets SOAP headers for subsequent calls
    • SoapClient::__soapCall — Calls a SOAP function
    • SoapClient::SoapClient — SoapClient constructor
  • SoapServer — The SoapServer class
    • SoapServer::addFunction — Adds one or more functions to handle SOAP requests
    • SoapServer::addSoapHeader — Add a SOAP header to the response
    • SoapServer::__construct — SoapServer constructor
    • SoapServer::fault — Issue SoapServer fault indicating an error
    • SoapServer::getFunctions — Returns list of defined functions
    • SoapServer::handle — Handles a SOAP request
    • SoapServer::setClass — Sets the class which handles SOAP requests
    • SoapServer::setObject — Sets the object which will be used to handle SOAP requests
    • SoapServer::setPersistence — Sets SoapServer persistence mode
    • SoapServer::SoapServer — SoapServer constructor
  • SoapFault — The SoapFault class
    • SoapFault::__construct — SoapFault constructor
    • SoapFault::SoapFault — SoapFault constructor
    • SoapFault::__toString — Obtain a string representation of a SoapFault
  • SoapHeader — The SoapHeader class
    • SoapHeader::__construct — SoapHeader constructor
    • SoapHeader::SoapHeader — SoapHeader constructor
  • SoapParam — The SoapParam class
    • SoapParam::__construct — SoapParam constructor
    • SoapParam::SoapParam — SoapParam constructor
  • SoapVar — The SoapVar class
    • SoapVar::__construct — SoapVar constructor
    • SoapVar::SoapVar — SoapVar constructor

User Contributed Notes

Luke 08-Sep-2015 04:21
Was calling an asmx method like $success=$x->AuthenticateUser($userName,$password) and this was returning me an error.

However i changed it and added the userName and password in an array and its now KAWA...
spectrumcat at gmail dot com 23-Feb-2015 01:19
In case you'll get a .p12 certificate for your SOAP client to use (or any other actually) make sure to convert it to PEM and merge with the private key.

Convert p12 to PEM with merged private key:
openssl pkcs12 -in supplied_cert.p12 -out php_soap_cert.pem -clcerts

Example:
$wsdl = "./mywsdl.wsdl"; // Or "http://provider.com/api/api.wsdl"

$options = [
        'location'      => "http://provider.com/api/",
        'local_cert'    => "./php_soap_cert.pem",
        'passphrase'    => "certificate_password",
];

try {
    $soapClient = new SoapClient($wsdl, $options);
} catch(Exception $e) {
    var_dump($e);
}
rafinskipg at gmail dot com 14-May-2012 11:54
Support for MTOM addign this code to your project:

<?php
class MySoapClient extends SoapClient
{
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        $response = parent::__doRequest($request, $location, $action, $version, $one_way);
        // parse $response, extract the multipart messages and so on

        //this part removes stuff
        $start=strpos($response,'<?xml');
        $end=strrpos($response,'>');
        $response_string=substr($response,$start,$end-$start+1);
        return($response_string);
    }
}

?>

Then you can do this
<?php
  new MySoapClient($wsdl_url);
?>
dennis at pleasedontspam dot clansman dot nl 02-Dec-2011 12:53
When you receive the soapfault message: "looks like we got no XML document" it might be that you're talking to a server that supports MTOM (http://www.w3.org/TR/soap12-mtom/).

MTOM is, as of yet (5.3.6), not supported by the SoapClient.
mariamr at gmail dot com 17-Oct-2010 03:38
"SOAP-ERROR: Parsing WSDL: Couldn't find in 'PATH/TO/YOUR/WSDL'"

This error occurs because some versions of PHP SOAP client  do not send the user name and password that you pass as the options input parameter of the SoapClient constructor  . Yu need to pass the credentials in the web service URL like this:
$soapClient =new SoapClient("http://user:password@example.com/blahblah.wsdl");
vishal dot thakkar at exateam dot com 22-Feb-2010 03:19
I was trying to figure out for days and days to make SOAP server return an associative array and NOT an object ....

To get an array (not object) in the SOAP response, just serialize the array before returning it to the clent..

hope it helps...
Thanks.....
Vishal Thakkar.
stephenlansell at gmail dot com 04-Feb-2010 04:19
Here is an example of a php client talking to a asmx server:

<?php

        $soapClient = new SoapClient("https://soapserver.example.com/blahblah.asmx?wsdl");

        // Prepare SoapHeader parameters
        $sh_param = array(
                    'Username'    =>    'username',
                    'Password'    =>    'password');
        $headers = new SoapHeader('http://soapserver.example.com/webservices', 'UserCredentials', $sh_param);

        // Prepare Soap Client
        $soapClient->__setSoapHeaders(array($headers));

        // Setup the RemoteFunction parameters
        $ap_param = array(
                    'amount'     =>    $irow['total_price']);

        // Call RemoteFunction ()
        $error = 0;
        try {
            $info = $soapClient->__call("RemoteFunction", array($ap_param));
        } catch (SoapFault $fault) {
            $error = 1;
            print("
            alert('Sorry, blah returned the following ERROR: ".$fault->faultcode."-".$fault->faultstring.". We will now take you back to our home page.');
            window.location = 'main.php';
            ");
        }

        if ($error == 0) {
            $auth_num = $info->RemoteFunctionResult;

            if ($auth_num < 0) {
                ....

                // Setup the OtherRemoteFunction() parameters
                $at_param = array(
                            'amount'        => $irow['total_price'],
                            'description'    => $description);

                // Call OtherRemoteFunction()
                $trans = $soapClient->__call("OtherRemoteFunction", array($at_param));
                $trans_result = $trans->OtherRemoteFunctionResult;
            ....
                } else {
                    // Record the transaction error in the database

                // Kill the link to Soap
                unset($soapClient);
            }
        }
    }
}

?>
moazzam at moazzam-khan dot com 05-Oct-2009 06:37
If anyone is trying to use this for accessing Sabre's web services, it won't work. Sabre checks the request header "Content-Type" to see if it is "text/xml" . If it is not text/xml then it sends an error back.

You will need to create a socket connection and use that to send the request over.
Raphal Gertz 19-Apr-2009 06:50
Juste a note to avoid wasting time on php-soap protocol and format support.

Until php 5.2.9 (at least) the soap extension is only capable of understanding wsdl 1.0 and 1.1 format.

The wsdl 2.0, a W3C recommendation since june 2007, ISN'T supported in php soap extension.
(the soap/php_sdl.c source code don't handle wsdl2.0 format)

The wsdl 2.0 is juste the 1.2 version renamed because it has substantial differences from WSDL 1.1.

The differences between the two format may not be invisible if you don't care a lot.

The wsdl 1.0 format structure (see http://www.w3.org/TR/wsdl) :
<definitions ...>
    <types ...>
    </types>
    <message ...>
        <part ...>
    </message>
    <portType ...>
        <operation ...>
            <input ... />
            <output ... />
            <fault ... />
        </operation>
    </portType>
    <binding ...>
        <operation ...>
            <input ... />
            <output ... />
            <fault ... />
        </operation>
    </binding>
    <service ...>
        <port ...>
    </service>
</definitions>

And the wsdl 2.0 format structure (see http://www.w3.org/TR/wsdl20/) :
<description ...>
    <types ...>
    </types>
    <interface ...>
        <fault ... />
        <operation ...>
            <input ... />
            <output ... />
            <fault ... />
        </operation>
    </interface>
    <binding ...>
        <fault ... />
        <operation ...>
            <input ... />
            <output ... />
            <fault ... />
        </operation>
    </binding>
    <service ...>
        <endpoint ...>
    </service>
</description>

The typical error message if you provide a wsdl 2.0 format file :
PHP Fatal error:  SOAP-ERROR: Parsing WSDL: Couldn't find <definitions> in 'wsdl/example.wsdl' in /path/client.php on line 9
jeff at jeffrodriguez dot com 03-Feb-2009 10:37
Named parameters are actually super easy once you know how to do it. No need to bother with SoapParam or SoapVar for the most part.

The code:
<?php
$ws->getAsset(array(
  'assetId' => 5967
));
?>

Generates the following SOAP body: (formatted for readability)
<SOAP-ENV:Body>
  <ns1:getAsset>
    <assetId>5967</assetId>
  </ns1:getAsset>
</SOAP-ENV:Body>
beau dot scott at gmail dot com 23-Oct-2008 12:34
If you've been trying to send null to a service that considers empty property tags as empty strings rather than null, here's an extending class that fixes this using xsi:nil:

<?php
class XSoapClient extends SoapClient
{
    const XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
    const _NULL_ = "xxx_replacedduetobrokephpsoapclient_xxx";

    protected $mustParseNulls = false;

    public function __doRequest($request, $location, $action, $version, $one_way = null)
    {
        if($this->mustParseNulls)
        {
            $this->mustParseNulls = false;
            $request = preg_replace('/<ns1:(\w+)>'.self::_NULL_.'<\/ns1:\\1>/',
                        '<ns1:$1 xsi:nil="true"/>',
                        $request, -1, &$count);

            if ($count > 0)
            {
                $request = preg_replace('/(<SOAP-ENV:Envelope )/',
                    '\\1 xmlns:xsi="'.self::XSI_NS.'" ',
                    $request);
            }
        }
        return parent::__doRequest($request, $location, $action, $version, $one_way);
    }

    public function __call($method, $params)
    {
        foreach($params as $k => $v)
        {
            if($v === null)
            {
                $this->mustParseNulls = true;
                $params[$k] = self::_NULL_;
            }
        }
        return parent::__call($method, $params);
    }
}
?>
Ryan 22-Oct-2008 03:21
If you are having an issue where SOAP cannot find the functions that are actually there if you view the wsdl file, it's because PHP is caching the wsdl file (for a day at a time). To turn this off, have this line on every script that uses SOAP: ini_set("soap.wsdl_cache_enabled", "0"); to disable the caching feature.
ivan dot cubillas at xeridia dot com 08-Aug-2008 10:02
The problem with the PHP WebService Client

We've a JAVA WebService Server which could have attached documents. When the PHP Client send the dates, the Server take it correctly, but when
the server return it to the PHP Client, the client reply always that is not a XML valid document.

The problem could be than the JAVA Server adds (to this type of XML binary files)  the documentType head: xml+xop and the clean doesn't understand
this head as a XML document.

There is some possibility to solve this problem in the PHP Client without doing changes in the JAVA Server?
aeg at mallxs dot nl 28-May-2008 02:19
SoapFault exception: [SOAP-ENV:Client] looks like we got no XML document

This error can have to reasons:
1: Your server script has some hidden output like spaces before or afher <?php ... ?> or echoing some text
2: You have a bug in your server script;
    resulting in a error message output

Test your server script by calling it direct  from the browser.
The result should be a clean output like :

?<SOAP-ENV:Envelope>
?<SOAP-ENV:Body>
?<SOAP-ENV:Fault>
  <faultcode>SOAP-ENV:Server</faultcode>
  <faultstring>Bad Request. Can't find HTTP_RAW_POST_DATA</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This tell's you there was a request but without data; This is OK
Now if you get the error when calling with your soap client;
remember that your server now will call other functions in the server.php script because it now has some data to process
nodkz at mail dot ru 24-May-2008 11:25
PROBLEM (with SOAP extension under PHP5) of transferring object, that contains objects or array of objects. Nested object would not transfer.

SOLUTION:
This class was developed by trial and error by me. So this 23 lines of code for most developers writing under PHP5 solves fate of using SOAP extension.

<?php
/*
According to specific of organization process of SOAP class in PHP5, we must wrap up complex objects in SoapVar class. Otherwise objects would not be encoded properly and could not be loaded on remote SOAP handler.

Function "getAsSoap" call for encoding object for transmission. After encoding it can be properly transmitted.
*/
abstract class SOAPable {
    public function getAsSOAP() {
        foreach($this as $key=>&$value) {
            $this->prepareSOAPrecursive($this->$key);
        }
        return $this;
    }

    private function prepareSOAPrecursive(&$element) {
        if(is_array($element)) {
            foreach($element as $key=>&$val) {
                $this->prepareSOAPrecursive($val);
            }
            $element=new SoapVar($element,SOAP_ENC_ARRAY);
        }elseif(is_object($element)) {
            if($element instanceof SOAPable) {
                $element->getAsSOAP();
            }
            $element=new SoapVar($element,SOAP_ENC_OBJECT);
        }
    }
}

// ------------------------------------------
//  ABSTRACT EXAMPLE
// ------------------------------------------

class PersonList extends SOAPable {
  protected $ArrayOfPerson; // variable MUST be protected or public!
}

class Person extends SOAPable {
   //any data
}

$client=new SoapClient("test.wsdl", array( 'soap_version'=>SOAP_1_2, 'trace'=>1, 'classmap' => array('Person' => "Person", 'PersonList' => "PersonList")  ));

$PersonList=new PersonList;

// some actions

$PersonList->getAsSOAP();

$client->someMethod($PersonList);

?>

So every class, which will transfer via SOAP, must be extends from class SOAPable.
As you can see, in code above, function prepareSOAPrecursive search another nested objects in parent object or in arrays, and if does it, tries call function getAsSOAP() for preparation of nested objects, after that simply wrap up via SoapVar class.

So in code before transmitting simply call $obj->getAsSOAP()


소개

The SOAP extension can be used to write SOAP Servers and Clients. It supports subsets of » SOAP 1.1, » SOAP 1.2 and » WSDL 1.1 specifications.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

This extension requires the libxml PHP extension. This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default.

User Contributed Notes

There are no user contributed notes for this page.


설치

To enable SOAP support, configure PHP with --enable-soap .

User Contributed Notes

dbagnara 21-Aug-2016 07:02
for Ubuntu 16.04 LTS
sudo apt-get install php7.0-soap
sudo systemctl restart apache2.service
qiancy 27-Jun-2013 11:27
yum install php53-soap
--centos Linux 2.6.18-308.el5
Anonymous 03-Nov-2010 05:03
If you are using 3rd party all-in-one servers on windows (like Uniform Server etc), "php_soap.dll" may not be included in the default distribution. If this is the case, the do phpinfo(); first, check what compiler is used - you need to find out if it's VC6 or VC9, then got to official php download page, get windows binary distribution (.zip archive recommended), and copy php_soap.dll from that archive to your server (on Uniform it's <path>usr\local\php\extensions\ and then restart the apache service. Please note that despite of compiler difference, PHP comes in two flavours - thread safe and non thread safe. If one does not work for you (check logs - "unable to load dynamic library '...' usually means file is not there (not the case) or cannot be loaded (due to compiler ABI mismatch or ts/nts mismatch). Try thread safe binaries first.
ifzen at yahoo dot com 04-Oct-2010 09:36
And CentOS or Red-Hat : yum install php-soap
walter 11-Jun-2010 09:06
Gentoo: add the 'soap' use flag to /etc/make.conf and emerge -av php


실행시 설정

이 함수의 작동은 php.ini 설정에 영향을 받습니다.

SOAP 설정 옵션
이름 | 기본값 | 가변성 | 변경점
soap.wsdl_cache_enabled | 1 | PHP_INI_ALL |
soap.wsdl_cache_dir | /tmp | PHP_INI_ALL |
soap.wsdl_cache_ttl | 86400 | PHP_INI_ALL |
soap.wsdl_cache | 1 | PHP_INI_ALL |
soap.wsdl_cache_limit | 5 | PHP_INI_ALL |

위 설정 지시어에 대한 간단한 설명입니다.

soap.wsdl_cache_enabled integer

Enables or disables the WSDL caching feature.

soap.wsdl_cache_dir string

Sets the directory name where the SOAP extension will put cache files.

soap.wsdl_cache_ttl integer

Sets the number of seconds (time to live) that cached files will be used instead of the originals.

soap.wsdl_cache integer

If soap.wsdl_cache_enabled is on, this setting determines the type of caching. It can be any of: WSDL_CACHE_NONE (0), WSDL_CACHE_DISK (1), WSDL_CACHE_MEMORY (2) or WSDL_CACHE_BOTH (3). This can also be set via the options array in the SoapClient or SoapServer constructor.

soap.wsdl_cache_limit integer

Maximum number of in-memory cached WSDL files. Adding further files into a full memory cache will delete the oldest files from it.

User Contributed Notes

no dot tehwan dot spam at com dot nospam dot gmail 21-Aug-2014 05:05
Keep in mind if you have memory cache enabled that even if you use an updated WSDL file, it will still be using the one in memory. If you download the WSDL files yourself, avoid using memory cache as you will have no control over it, or set the TTL to a small value (default is 1 day).
martijn at netexpo dot nl 19-Oct-2012 10:37
Please note that these three ini settings will also affect the behaviour of your soap server (and clients as well) :

1. default_socket_timeout default 60 secs
Large or slow requests to your soap server or long processes at your soap server will return soap fault after 60 secs like : Error Fetching http headers.

2. max_execution_time default 30 secs
This can be the next bottleneck (but only when your default_socket_timeout is larger then this setting). Your soap server will not return anything, no faults no output, just an empty string.

3. memory_limit default 128M
Will throw fatal errors when the soap server script itself has low memory or will let your services return empty strings when the data it processes puts memory usage over this limit.

Other max POST settings luckily (but a bit suprisingly to me) have _no_ effect for your soap server. Those are :

max_input_time
max_input_nesting_level
max_input_vars
post_max_size
suhosin.post.max_array_depth
suhosin.post.max_array_index_length
suhosin.post.max_name_length
suhosin.post.max_totalname_length
suhosin.post.max_vars
suhosin.post.max_value_length
hpralow at users dot sf dot net 24-Jul-2008 04:40
If you want to dissable WSDL-caching, you can do so with
<?php
ini_set('soap.wsdl_cache_enabled', '0');
ini_set('soap.wsdl_cache_ttl', '0');
?>
However you must do this before creating a SOAP object (soap_client or soap_server).
Changing the settings like above does not have any effect on already existing soap-objects.

[EDIT BY danbrown AT php DOT net: Typofix provided by (james AT voodoo DOT co DOT uk) on 4-FEB-09.]


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

SOAP constants
Constant | Value | Description
SOAP_1_1 (integer) | 1 |
SOAP_1_2 (integer) | 2 |
SOAP_PERSISTENCE_SESSION (integer) | 1 |
SOAP_PERSISTENCE_REQUEST (integer) | 2 |
SOAP_FUNCTIONS_ALL (integer) | 999 |
SOAP_ENCODED (integer) | 1 |
SOAP_LITERAL (integer) | 2 |
SOAP_RPC (integer) | 1 |
SOAP_DOCUMENT (integer) | 2 |
SOAP_ACTOR_NEXT (integer) | 1 |
SOAP_ACTOR_NONE (integer) | 2 |
SOAP_ACTOR_UNLIMATERECEIVER (integer) | 3 |
SOAP_COMPRESSION_ACCEPT (integer) | 32 |
SOAP_COMPRESSION_GZIP (integer) | 0 |
SOAP_COMPRESSION_DEFLATE (integer) | 16 |
SOAP_AUTHENTICATION_BASIC (integer) | 0 |
SOAP_AUTHENTICATION_DIGEST (integer) | 1 |
SOAP_SSL_METHOD_TLS (integer) | 0 | Since PHP 5.5.0.
SOAP_SSL_METHOD_SSLv2 (integer) | 1 | Since PHP 5.5.0.
SOAP_SSL_METHOD_SSLv3 (integer) | 2 | Since PHP 5.5.0.
SOAP_SSL_METHOD_SSLv23 (integer) | 3 | Since PHP 5.5.0.
UNKNOWN_TYPE (integer) | 999998 |
XSD_STRING (integer) | 101 |
XSD_BOOLEAN (integer) | 102 |
XSD_DECIMAL (integer) | 103 |
XSD_FLOAT (integer) | 104 |
XSD_DOUBLE (integer) | 105 |
XSD_DURATION (integer) | 106 |
XSD_DATETIME (integer) | 107 |
XSD_TIME (integer) | 108 |
XSD_DATE (integer) | 109 |
XSD_GYEARMONTH (integer) | 110 |
XSD_GYEAR (integer) | 111 |
XSD_GMONTHDAY (integer) | 112 |
XSD_GDAY (integer) | 113 |
XSD_GMONTH (integer) | 114 |
XSD_HEXBINARY (integer) | 115 |
XSD_BASE64BINARY (integer) | 116 |
XSD_ANYURI (integer) | 117 |
XSD_QNAME (integer) | 118 |
XSD_NOTATION (integer) | 119 |
XSD_NORMALIZEDSTRING (integer) | 120 |
XSD_TOKEN (integer) | 121 |
XSD_LANGUAGE (integer) | 122 |
XSD_NMTOKEN (integer) | 123 |
XSD_NAME (integer) | 124 |
XSD_NCNAME (integer) | 125 |
XSD_ID (integer) | 126 |
XSD_IDREF (integer) | 127 |
XSD_IDREFS (integer) | 128 |
XSD_ENTITY (integer) | 129 |
XSD_ENTITIES (integer) | 130 |
XSD_INTEGER (integer) | 131 |
XSD_NONPOSITIVEINTEGER (integer) | 132 |
XSD_NEGATIVEINTEGER (integer) | 133 |
XSD_LONG (integer) | 134 |
XSD_INT (integer) | 135 |
XSD_SHORT (integer) | 136 |
XSD_BYTE (integer) | 137 |
XSD_NONNEGATIVEINTEGER (integer) | 138 |
XSD_UNSIGNEDLONG (integer) | 139 |
XSD_UNSIGNEDINT (integer) | 140 |
XSD_UNSIGNEDSHORT (integer) | 141 |
XSD_UNSIGNEDBYTE (integer) | 142 |
XSD_POSITIVEINTEGER (integer) | 143 |
XSD_NMTOKENS (integer) | 144 |
XSD_ANYTYPE (integer) | 145 |
XSD_ANYXML (integer) | 147 |
APACHE_MAP (integer) | 200 |
SOAP_ENC_OBJECT (integer) | 301 |
SOAP_ENC_ARRAY (integer) | 300 |
XSD_1999_TIMEINSTANT (integer) | 401 |
XSD_NAMESPACE (integer) | http://www.w3.org/2001/XMLSchema |
XSD_1999_NAMESPACE (integer) | http://www.w3.org/1999/XMLSchema |
SOAP_SINGLE_ELEMENT_ARRAYS (integer) | 1 |
SOAP_WAIT_ONE_WAY_CALLS (integer) | 2 |
SOAP_USE_XSI_ARRAY_TYPE (integer) | 4 |
WSDL_CACHE_NONE (integer) | 0 |
WSDL_CACHE_DISK (integer) | 1 |
WSDL_CACHE_MEMORY (integer) | 2 |
WSDL_CACHE_BOTH (integer) | 3 |

User Contributed Notes

User 01-Oct-2010 05:56
We are casting the values (float) to simulate the 64-bits long. seem to work fine, of course, the values we are using have no decimal point.


SOAP 함수 목록

Table of Contents

  • is_soap_fault — Checks if a SOAP call has failed
  • use_soap_error_handler — Set whether to use the SOAP error handler

User Contributed Notes

admin at localfm dot info 13-Oct-2008 09:01
As you may find, SOAP functions return result of type stdClass, not array. It means that you'll need to use $row->id instead of $row['id'] which may be inconvenient.

You can cast stdClass to array using this construction:

<?php
$data = $soap->getMyData($someParams);
$data = (array)$data;

print $data['field']; // now it works
?>
vIceBerg 24-Sep-2008 04:18
I have PHP 5.2.5 on my computer. I get this result:

Array
(
    [0] => stdClass Object
        (
            [Client] => stdClass Object
                (
                    [Nom] => LeNom:00000
                    [Prenom] => LePrenom:00000
                )

        )

My host have 5.1.6, the same call returns this result:
Array
(
    [0] => stdClass Object
        (
            [Client] => Array
                (
                    [0] => stdClass Object
                        (
                            [Nom] => LeNom:00000
                            [Prenom] => LePrenom:00000

                        )

5.2.5 is the good structure.

Take care of the version you're using. It can be a lot of work to change all the code
daniel dot harding at chaptereight dot com 28-Jul-2008 05:13
Setting Attributes for Complex Type Elements PHP5/SOAP
=========================================

// Setting attributes for simple elements:
<parentEl id="ref1">12345</foo>
<?php
array("parentEl" => array("_" => 12345, "id" => "ref1"));
?>

// Setting attributes for complex elements:
<parentEl id="ref1">
    <childEl>blah</childEl>
</parentEl>

'parentEl' => array(
        array('childEl' => 'blah')
    ),
    'id' => 'ref1'
)
nodkz at mail dot ru 24-May-2008 11:28
PROBLEM (with SOAP extension under PHP5) of transferring object, that contains objects or array of objects. Nested object would not transfer.

SOLUTION:
This class was developed by trial and error by me. So this 23 lines of code for most developers writing under PHP5 solves fate of using SOAP extension.

<?php
/*
According to specific of organization process of SOAP class in PHP5, we must wrap up complex objects in SoapVar class. Otherwise objects would not be encoded properly and could not be loaded on remote SOAP handler.

Function "getAsSoap" call for encoding object for transmission. After encoding it can be properly transmitted.
*/
abstract class SOAPable {
    public function getAsSOAP() {
        foreach($this as $key=>&$value) {
            $this->prepareSOAPrecursive($this->$key);
        }
        return $this;
    }

    private function prepareSOAPrecursive(&$element) {
        if(is_array($element)) {
            foreach($element as $key=>&$val) {
                $this->prepareSOAPrecursive($val);
            }
            $element=new SoapVar($element,SOAP_ENC_ARRAY);
        }elseif(is_object($element)) {
            if($element instanceof SOAPable) {
                $element->getAsSOAP();
            }
            $element=new SoapVar($element,SOAP_ENC_OBJECT);
        }
    }
}

// ------------------------------------------
//  ABSTRACT EXAMPLE
// ------------------------------------------

class PersonList extends SOAPable {
  protected $ArrayOfPerson; // variable MUST be protected or public!
}

class Person extends SOAPable {
   //any data
}

$client=new SoapClient("test.wsdl", array( 'soap_version'=>SOAP_1_2, 'trace'=>1, 'classmap' => array('Person' => "Person", 'PersonList' => "PersonList")  ));

$PersonList=new PersonList;

// some actions

$PersonList->getAsSOAP();

$client->someMethod($PersonList);

?>

So every class, which will transfer via SOAP, must be extends from class SOAPable.
As you can see, in code above, function prepareSOAPrecursive search another nested objects in parent object or in arrays, and if does it, tries call function getAsSOAP() for preparation of nested objects, after that simply wrap up via SoapVar class.

So in code before transmitting simply call $obj->getAsSOAP()
aeolianmeson at dontfindme dot blitzeclipse dot com 07-May-2008 05:42
A quick example to do a login using a WSDL, do a second request using the session ID from the login response, and then dump the XML from that request.

    // Setting "trace" will allow us to view the request that we are making, after we have made it.
    $objClient = new SoapClient("http://www.somewhere.com/wsdls/some.wsdl", array('trace' => true));

    // These parameters satisfy this specific remote call.
    $arrParameters_Login = array('username' => 'username', 'password' => 'password', 'company' => 'Company');

    // Invoke the remote call "login()".
    $objLogin =  $objClient->login($arrParameters_Login);

    // Grab session ID that this remote call will provide.
    $strSessionID = $objLogin->loginReturn->sessionId;

    // Set headers-- The remote call "query()" will require a header pointing to our session.

    $strHeaderComponent_Session = "<SessionHeader><sessionId>$strSessionID</sessionId></SessionHeader>";

    $objVar_Session_Inside = new SoapVar($strHeaderComponent_Session, XSD_ANYXML, null, null, null);
    $objHeader_Session_Outside = new SoapHeader('namespace.com', 'SessionHeader', $objVar_Session_Inside);

    // More than one header can be provided in this array.
    $objClient->__setSoapHeaders(array($objHeader_Session_Outside));

    // Set the query parameters.

    $strQuery = 'select empID from Time where empID = 92389278';
    $arrParameters_Query = array('queryString' => $strQuery);

    // Make call.

    $objResponse = $objClient->query($arrParameters_Query);

    header('Content-Type: text/xml; ');
    print($objClient->__getLastRequest());

The request that goes out:

<SOAP-ENV:Envelope>
    <SOAP-ENV:Header>
        <SessionHeader>
            <sessionId>8789364113604871127</sessionId>
        </SessionHeader>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:query>
            <ns1:queryString>select empID from Time where empID = 92389278</ns1:queryString>
        </ns1:query>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
rlee at melbournechapter dot net 02-May-2008 02:38
Just a note on "DTD not recognised..." faults. Check to make sure your wsdl file contains:

<?xml version ='1.0' encoding ='UTF-8' ?>

Also make sure you use full paths, to your service (in the wsdl, client and server)
...
<wsdlsoap:address location='http://www.mysite.com.au/web_services/myserver.php' />
...
<?php
// SOAP Server
$server = new SoapServer('http://www.mysite.com.au/web_services/hello.wsdl');
...
...
?>

<?php
// SOAP Client
 $client = new SoapClient('http://www.mysite.com.au/web_services/hello.wsdl');
...
...
?>

FYI im no SOAP expert but I hope this helps someone out ;)
Aurlien Christman 31-Mar-2008 10:34
From PHP 5, you can use the undocumented method SoapClient::__setLocation() to redefine the target EPR :

<?php

$client = new SoapClient("some_wsdl");
$client->__setLocation("http://new/target/epr");

$client->someFunction("param1", "param2");

?>
Andr Hnsel 24-Mar-2008 08:42
Note that HTTP auth is not done when fetching the WSDL file, only when carrying out the actual SOAP request.

Bug #27777 states that it is possible to supply credentials for the WSDL file using URL notation:

<?php
$client = new SoapClient("http://yourLogin:yourPassword@foo.com/bar.wsdl");
?>
thomas dot rabaix at gmail dot com 13-Mar-2008 05:41
I have set up a small article which explains how to use SOAP with NTLM authentication.

http://tinyurl.com/328rgv (Ghrrr ! the url is too long)

Hope it can help you !

Thomas
Drydenmaker 14-Jan-2008 04:59
Working with a third party service I got: "SOAP-ERROR: Parsing Schema: unexpected <text> in restriction"

Thought it would be worth it to share.  What it is saying is that there is some text in an invalid place.  C# seems to ignore it, and if you are using nusoap it doesn't notice it either.  But what was causing my problem was something like:
<types>
    <schema ...

            <simpleType name="some type">
                <restriction base="xsd:string">
                    <enumeration value="foo"/>;
                </restriction>
            </simpleType>

Note the semicolon (;).  Filter that out and your good.
Dominik 07-Dec-2007 10:31
If you are working
- on localhost
- in WSDL mode
and encounter the following error when you try to connect the soap client with the soap server

Fatal error: Uncaught SoapFault exception: [HTTP] Could not connect to host...

then change "localhost" to "127.0.0.1" in your WSDL file:
<soap:address location='http://127.0.0.1/soap-server.php'/>
Emanuele Colonnelli 06-Dec-2007 02:24
Hi,

I'd like to add this trick to get SoapClient correctly running with a WSDD-generated Axis 1 webservice.

By default, Apache Axis 1 generates a SOAP RPC/Encoded WSDL with "urn:" prefixes before all <SERVICE_NAME> occurrences.

Axis 1 WSDL also uses an "impl:" namespace prefix.

Suppose your service's name is "myWS", your WSDL will contains a "<wsdl:binding>" element with the name attribute valued "urn:myWSSoapBinding":

<wsdl:binding name="myWSSoapBinding">

PHP SoapClient has a problem (I hope it will be fixed soon ) parsing this WSDL and generates this error:

SOAP-ERROR: Parsing WSDL: No  element with name 'myWSSoapBinding'

This error arise because PHP SoapClient could not resolve the "urn:impl:myWSSoapBinding" QName in generated WSDL:

<wsdl:service name="myWSService">
<wsdl:port binding="impl:urn:myWSSoapBinding" name="urn:myWS">
<wsdlsoap:address location="http://URL/services/urn:myWS"/>
</wsdl:port>
</wsdl:service>

To solve this problem I parse WSDL applying a preg_replace to delete all "urn:" occurrences.

Then I save this generated WSDL somewhere in my site and point my SoapClient to this location.

I also need to specify additional parameters to SoapClient constructor:

<?php
$soap = new SoapClient("wsdl/myWS.wsdl",
array(
  "style" => SOAP_RPC,
  "use" => SOAP_ENCODED,
  "soap_version" => SOAP_1_1,
  "uri"=>"urn:myWS"
));
?>

I suggest you this resources to a full understanding of this kind of problems:

http://www.ibm.com/developerworks/
webservices/library/ws-whichwsdl/

http://wso2.org/wiki/display/wsfphp/
PHP+SOAP+Extension+API+-+Backward+Compatibility

I hope this save you hours of imprecations! :)

Emanuele Colonnelli
AL13N 28-Nov-2007 01:05
this might be helpful, as it took quite some time for me to find this out:

if you're using some .wsdl and there's a sequence that can be there more than once (ie: maxOccurs > 1), you can specify an non-associative array for it if you have more than 1 items OR you could just specify the one item if there is only one:

<?php
'items' => array(
    array(
        'itemId' => 5,
        'name' => 'some name',
    ),
    array(
        'itemId' => 6,
        'name' => 'some other name',
    ),
),
?>

this also works:

<?php
'items' => array(
    'itemId' => 5,
    'name' => 'some name',
),
?>
Anonymous 26-Nov-2007 08:54
when dealing with wsdl and flash soap with php
(not exactly php, but proper wsdl - took me hours to figure it to work properly in flash webservice)

when naming definitions,
don't use "tns:" but use "typens:"

makesure your "definitions/targetNamespace"
is same as your "soap:body" namespace
example: "urn:mynamespace"

makesure your "binding/type" is declared with "typens"

makesure your service/port/binding is set to 'typens:...'

if you did not do it properly, you will end up with
WSDL.UnrecognizedNamespace - in flash
but seems okay in php soapclient...

happy coding :)
hope this help you to save your time on the wsdl :)
james at mercstudio dot com 24-Nov-2007 10:45
If you use wsdl,

makesure you define the input correctly.
if your method does not contain any input parameter,
you got to makesure that you:

 - do not create the message tag for the input..
 - do not put input within porttype / operation
 - do not put input within binding / operation

if not, you will get the error:
[Client] looks like we got no XML

d***, took me several hours figuring that out...
Dennis Du Krger 23-Nov-2007 04:15
A word of warning:

Do NOT use associative arrays or arrays not starting with element number 0...

Various versions of the soap extension will not send it through properly.
t dot kloppenburg at billiton dot de 22-Nov-2007 12:33
I had the "DTD are not supported by SOAP" message, too. It helped me to var_dump the complete "SoapFault" exception to find helpful information in a field "["faultstring"]=>....".

It was an error my soap handler (adding a non existant function).
dirk at waferthin dot com 21-Jun-2007 01:31
I hope this will save someone time. When developing and testing your SOAP server, remember to disable WSDL caching in BOTH client and server:

$ini = ini_set("soap.wsdl_cache_enabled", 0);
jochen dot munz at municons dot com 11-Jun-2007 05:56
When encountering an error message like this

[faultstring] => Function ("yourMethod") is not a valid method for this service

although it is present in the WSDL etc., be aware that PHP caches the wsdl locally for better performance. You can disable the caching via php.ini/.htaccess completely or remove the cache file (/tmp/wsdl-.. if you are on Linux) to force regeneration of it.
Marcio Annunciato 17-May-2007 11:22
In case your soap response containts an ampersand, in certain cases you might get an "unterminated entity reference" warning and data might appear truncated (related to bug #36795?). Maybe there is a more formal fix or setting, but since I couldn't find anything I came with the following:

class MySoapClient extends SoapClient {
    function __doRequest($request, $location, $action, $version) {
        $res = parent::__doRequest($request, $location, $action, $version);
        $resx = str_replace('&amp;', '&amp;amp;', $res);
        print_r($resx);
        return ($resx);
    }
}
albert at jool dot nl 27-Mar-2007 11:59
A note for ppl who use certificates with SOAP under a Windows config: it seems to be required to give the full path to the certificate file - and don't prepend 'file://' :

<?php

$wsdl = "test.wsdl";
$local_cert = "c:\htdocs\mycert.pem";
$passphrase = "xyz";

$client = new SoapClient($wsdl, array('local_cert' => $local_cert, 'passphrase' => $passphrase);

?>
kroesjnov at hotmail dot com 09-Feb-2007 04:38
If you dont want to manually maintain the classmap, make sure you use the same names for your PHP object classes and your WSDL complexTypes, and use the following code:

$classmap = array();
$tmpClient = new SoapClient("soapserver.wsdl");

foreach($tmpClient->__getTypes() as $type)
{
    $array = split(" ", $type);
    if($array[0] == "struct" && class_exists($array[1]))
    {
        $classmap[$array[1]] = $array[1];
    }
}
unset($tmpClient);

$server = new SoapServer("soapserver.wsdl", array("classmap" => $classmap));
unset($classmap);
$server->setClass("someclass");
$server->handle();
brephophagist 18-Jan-2007 06:45
For those working from a wsdl full of complex types that just want a class structure to hang your code on and not worry about typing in long parameter lists (or creating a script to do so): wsdl2php is a wonderful time-saver. Gets a structure going so you can go in and add what validation and special data munging you need: http://www.urdalen.no/wsdl2php/

Big up yourself, Knut.
Olaf Krische 21-Nov-2006 10:48
If you use SSL with a cert and password authentication:

$wsdl = "https://ws.ecopatz.de/ProductInfo?wsdl";
$pass = 'a password';
$certFile = "./mycert.pem";

  $client = new SoapClient($wsdl,
    array(
     'local_cert' => $certFile,
     'passphrase' => $pass
    )
  );

If you have problems with the certfile like this:

  Warning: SoapClient::__construct(): Unable to set local cert chain file `./mycert.pem'; Check that your cafile/capath settings include details of your certificate and its issuer in productinfo.php on line 27

then the certFile is probably in the "wrong format" (the wrong format for php maybe). It worked for me, when i appended the content of the private key file and the certificate file to a single file "mycert.pem":

  cat mycert.key >mycert.pem # mycert.key was the private key
  cat mycert.crt >>mycert.pem # mycert.crt was the signed certificate

Thanks to an author somewhere, who pointed to "curl --cert", where this little "so unimportant" dependency has been mentioned.
nico 25-Aug-2006 03:20
If you want to build a Soap Server for Microsoft Office's client (like Microsoft Office Research Service) you need to rewrite SOAP's namespaces :

<?php
// (...)

$server = new SoapServer($wsdl, array('uri' => $uri, 'classmap' => $classmap));
$server->setClass($class);
function callback($buffer)
{
    $s = array('<ns1:RegistrationResponse>', 'ns1:', 'xmlns:ns1="urn:Microsoft.Search"');
    $r = array('<RegistrationResponse xmlns="urn:Microsoft.Search">', '', '');
   return (str_replace($s, $r, $buffer));
}
ob_start('callback');
$server->handle();
ob_end_flush();

// (...)
?>

There are a complete example at this URL : http://touv.ouvaton.org/article.php3?id_article=104
info at webability dot info 22-Aug-2006 09:30
If you want to use soap over https, you *must* compile php with the configure option with-openssl or you will have the error "[HTTP] SSL support not available in this build" while trying to use the web services over SSL
mini at freebsd dot org 09-Jul-2006 01:54
wokan at cox dot net is incorrect about the security of URI-passed values to HTTPS URIs.  HTTPS connections are HTTP inside SSL -- all of the HTTP traffic, including the request, are encrypted.
lists at 5etdemi dot com 14-Mar-2006 03:26
To enable SoapClient on Windows, simply add a line:

extension=php_soap.dll

in php.ini in the extensions section.
adm 01-Mar-2006 08:27
Wondering why the function you just added to your WSDL file is not available to your SOAP client? Turn off WSDL caching, which (as the documentation says) is on by default.

At the top of your script, use:

$ini = ini_set("soap.wsdl_cache_enabled","0");
Matthew Glubb 20-Jan-2006 05:05
For anyone wondering why they are getting a "DTD are not supported by SOAP" fault string. It is because you are probably being returned an HTML page due to an internal server error, 404, etc.

Refer to the documentation for SoapClient->__getLastResponse in order to see what returned by the server.
ckl at ecw dot de 17-Jan-2006 08:54
To debug a SOAP service using SoapServer(), a WSDL file and Zend Studio Client/Server, you have to append ?start_debug=1&amp;debug_port=10000 to the service location:
--- snip ---
... method / service definition ....

<service name="SOAPService">
  <port
    name="SOAPServicePort"
    binding="typens:SOAPServiceBinding">
   <soap:address
   location="$URL?start_debug=1&amp;debug_port=10000"/>
  </port>
</service>
--- snap ---
Rui Martins 18-Dec-2005 05:33
Here's an example on how to pass ArrayOfAnyType arguments
containing complex types.

Suppose your WSDL file defines "http://any.url.com/" as the default namespace and a complex type "SomeComplexType".

If you want to call a WebServices which takes an ArrayOfAnyType argument of "SomeComplexType"s you need to perform the following:

<?php
// complexTypes being an array containing several instances of SomeComplexType

myWSParameter = array();
foreach (complexTypes as ct)
{
   // Don't misspell the type or the namespace. Also note that php won't assume the default namespace defined in the WSDL file.
   myWSParameter []= new SoapVar(ct, 0, "SomeComplexType", "http://any.url.com/");
}
?>

On the other hand, when a WebService returns an ArrayOfAnyType you have to do the following to access each of its elements.

<?php
// Here, we will be echoing each return item
$res = $someWS->myFunction($myArgs)

// If only one element is returned, an array won't be built
if (is_array(myFunctionResult->anyType))
{
   foreach (myFunctionResult->anyType as $soapVar)
   {
      echo $soapVar->enc_value;
   }
}
else
{
   echo myFunctionResult->anyType->enc_value;
}
?>

This has all been tested using a .NET WebService.
ChrisB 24-Oct-2005 08:03
Heads up for anyone using PHP Soap + Sessions + PEAR DB classes.

Every time you make a call, via the soap client to your web service, your PEAR DB session is put to sleep and it doesnt by default wake upon the next request.

To fix this I simply called my particular database close call ifx_close() below my $soap->handle();
Bob 11-Oct-2005 10:17
If you are scratching your head why NuSOAP not working on PHP 5.x , the reason is this built-in SOAP Extenstion uses same soapclient() class name as Nusoap.

replace 'soapclient' with 'soapclient_xxx'  in nusoap.php and you are good to go...
johnjawed at gmail dot com 22-Sep-2005 06:29
For those wondering on how to set attributes on nodes with PHP5 SOAP, it would be done as such:

<... soap env/header>
<foo bar="blah">12345</foo>

array("foo" => array("_" => 12345, "bar" => "blah"));
Darryl 20-Jul-2005 08:46
Having trouble passing complex types over SOAP using a PHP SoapServer in WSDL mode?  Not getting decoded properly?  This may be the solution you're looking for!

When using ComplexType in the schema portion of the WSDL file, You need use an additional step to tell PHP SOAP how to encode the objects.  The first method would be to explicitely encapsulate the object in a SoapVar object - telling PHP to use generalized SOAP encoding rules (which encodes all ComplexTypes as Structs).  This won't work, though, if the client is expecting the objects to be encoded according to the WSDL's schema.  So, The actual way to do this is:

* First, define a specific PHP class which is actually just a data structure holding the various properties, and the appropriate  ComplexType in the WSDL.

<?php
 class MyComplexDataType {
     public $myProperty1;
     public $myProperty2;
 }
?>
 <complexType name="MyWSDLStructure">
 <sequence>
    <element name="MyProperty1" type="xsd:integer"/>
    <element name="MyProperty2" type="xsd:string"/>
 </sequence>
 </complexType>

* Next, Tell the SoapServer when you initialize it to map these two structures together.

<?php
 $classmap = array('MyWSDLStructure' => 'MyComplexDataType');
 $server = new SoapServer("http://MyServer/MyService.wsdl", array('classmap' => $classmap))
?>

* Finally, have your method return an instance of your class directly, and let the SoapServer take care of encoding!

<?php
 public function MySoapCall() {
     $o = new MyComplexDataType();

     $o->myProperty1 = 1;
     $o->myProperty2 = "MyString";

     return $o
 }
?>
Jim Plush 13-Jul-2005 01:45
If you're trying to use the SOAP Extension over SSL with a custom PEM file you need to do this:

$client->_local_cert = "C:\\path\myCert.pem";
pash_ka 18-May-2005 09:52
One good tutorial on using this extension is on IBM web site:
"Using the PHP 5 SOAP extension to consume a WebSphere Web service"
http://www.ibm.com /developerworks/library/os-phpws/?ca=dgr-lnxw06PHP5soap
flobee at gmail dot com 11-May-2005 10:25
wow, actually a cool program and soap is new for me.
I found out some interessting things i can not debug because the scripts exit without any error messages or notes. :-(

you may have problems with the memory and/or especially on "shared servers" when server load is high.
sometimes the script does the job, sometimes it just stopping at any unknown point.
these are the steps my script does:
* get data from remote server ( ~ 4.5 MB)
* parsing requested object and store the data in a database.

the return with debug messages was intressting:
-> check Mem limit: 30M
-> $client = new new SoapClient($url_wsdl, $options);
-> Memory usage: 185888
-> $client->[requested_method_to_get_data]();
-> check: __getLastResponseHeaders() - after:
-> HTTP/1.1 200 OK // remote server is fine with me :-)
-> Content-Length: 4586742  // I got the data
-> check: Memory usage now: 23098872 // ups !!! this can't be true!!

so, and if now someone on the server takes the rest of RAM  the walk thought the data breaks :-(

so, i need to store the xml tree ($client->client->__last_response) and parsing it by the classical way. (if you would request more RAM, you may get in trouble with the admin if you run a script like this more often! (on shared servers)
OrionI 18-Apr-2005 05:12
If you're having problems calling a .NET web service, see the comments over on http://php.net/soap_soapclient_soapcall (the __soapCall method).
kucerar at hhmi dot org 25-Mar-2005 09:04
Here are 73 test cases that detail the kinds of schemas that PHP5 currently supports and the structures you are expected to plug into them for the return value from your services.   Much better than trying to guess!

http://cvs.php.net/co.php/pecl/soap/tests/schema

You can cycle through the listings by changing the main index in the URL below,  rather than go in and out of the pages

http://cvs.php.net/co.php/pecl/soap/tests/schema/schema052.phpt?r=1.2

I downloaded the whole thing with this,  CVS might work too.
http://www.httrack.com/

With them all downloaded I just browse them with Textpad.
Norman Clarke <norman at dontblink dot com> 22-Feb-2005 01:29
Note that if you should need to set the timeout for your soap request,  you can use ini_set to change the value for the default_socket_timeout. I previously used NuSOAP, whose soap client class has a timeout option, and it took me a while to figure out that PHP's soap uses the same socket options as everything else.
wokan at cox dot net 06-Feb-2005 02:06
It's not good security practice to pass the username and password in the URI when the point of SSL is to prevent that information from being intercepted.  Putting that information in the URI makes it interceptable.  HTTPS-Posted values are safe because values passed in the headers are sent after the SSL handshake has been completed.
mikx at mikx dot de 29-Mar-2004 05:12
It took me a while to properly establish a password protected client connection via https on windows/apache1.3. Here my little guide:

1. The SOAP extension isn't activated by default (PHP5 RC1). Just add "extension=php_soap.dll" to the php.ini and don't forget to set the extension_dir properly (in most cases "c:\php\ext").

2. Add "extension=php_openssl.dll" to the php.ini. This module depends on libeay32.dll and ssleay32.dll - copy them from your php folder to your system32 folder.

3. Restart apache

4. The sourcecode

$client = new SoapClient("https://yourLogin:yourPassword@foo.com/bar.wsdl", array(
    "login"      => "yourLogin",
    "password"   => "yourPassword",
    "trace"      => 1,
    "exceptions" => 0));

$client->yourFunction();

print "<pre>\n";
print "Request: \n".htmlspecialchars($client->__getLastRequest()) ."\n";
print "Response: \n".htmlspecialchars($client->__getLastResponse())."\n";
print "</pre>";

Currently it seems to be necessary to add your login and password both in the uri and in the options array. Not sure if this is the expected behavior.


Checks if a SOAP call has failed

  • ≪ SOAP 함수 목록
  • use_soap_error_handler ≫
  • PHP Manual
  • SOAP 함수 목록
  • Checks if a SOAP call has failed

is_soap_fault

(Unknown)

is_soap_fault — Checks if a SOAP call has failed

설명

bool is_soap_fault ( mixed $object )

This function is useful to check if the SOAP call failed, but without using exceptions. To use it, create a SoapClient object with the exceptions option set to zero or FALSE. In this case, the SOAP method will return a special SoapFault object which encapsulates the fault details (faultcode, faultstring, faultactor and faultdetails).

If exceptions is not set then SOAP call will throw an exception on error. is_soap_fault() checks if the given parameter is a SoapFault object.

인수

object

The object to test.

반환값

This will return TRUE on error, and FALSE otherwise.

예제

Example #1 is_soap_fault() example

<?php
$client = new SoapClient("some.wsdl", array('exceptions' => 0));
$result = $client->SomeFunction();
if (is_soap_fault($result)) {
    trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
?>

Example #2 SOAP's standard method for error reporting is exceptions

<?php
try {
    $client = new SoapClient("some.wsdl");
    $result = $client->SomeFunction(/* ... */);
} catch (SoapFault $fault) {
    trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
?>

참고

  • SoapClient::SoapClient() - SoapClient constructor
  • SoapFault::SoapFault() - SoapFault constructor

User Contributed Notes

There are no user contributed notes for this page.


Set whether to use the SOAP error handler

  • ≪ is_soap_fault
  • SoapClient ≫
  • PHP Manual
  • SOAP 함수 목록
  • Set whether to use the SOAP error handler

use_soap_error_handler

(Unknown)

use_soap_error_handler — Set whether to use the SOAP error handler

설명

bool use_soap_error_handler ([ bool $handler = true ] )

This function sets whether or not to use the SOAP error handler in the SOAP server. It will return the previous value. If set to TRUE, details of errors in a SoapServer application will be sent to the client as a SOAP fault message. If FALSE, the standard PHP error handler is used. The default is to send error to the client as SOAP fault message.

인수

handler

Set to TRUE to send error details to clients.

반환값

Returns the original value.

참고

  • set_error_handler() - Sets a user-defined error handler function
  • set_exception_handler() - Sets a user-defined exception handler function

User Contributed Notes

There are no user contributed notes for this page.


The SoapClient class

(PHP 5 >= 5.0.1, PHP 7)

소개

The SoapClient class provides a client for » SOAP 1.1, » SOAP 1.2 servers. It can be used in WSDL or non-WSDL mode.

클래스 개요

SoapClient {
/* 메소드 */
public mixed __call ( string $function_name , string $arguments )
public SoapClient ( mixed $wsdl [, array $options ] )
public string __doRequest ( string $request , string $location , string $action , int $version [, int $one_way = 0 ] )
public array __getFunctions ( void )
public string __getLastRequest ( void )
public string __getLastRequestHeaders ( void )
public string __getLastResponse ( void )
public string __getLastResponseHeaders ( void )
public array __getTypes ( void )
public void __setCookie ( string $name [, string $value ] )
public string __setLocation ([ string $new_location ] )
public bool __setSoapHeaders ([ mixed $soapheaders ] )
public mixed __soapCall ( string $function_name , array $arguments [, array $options [, mixed $input_headers [, array &$output_headers ]]] )
public SoapClient ( mixed $wsdl [, array $options ] )
}

Table of Contents

  • SoapClient::__call — Calls a SOAP function (deprecated)
  • SoapClient::__construct — SoapClient constructor
  • SoapClient::__doRequest — Performs a SOAP request
  • SoapClient::__getFunctions — Returns list of available SOAP functions
  • SoapClient::__getLastRequest — Returns last SOAP request
  • SoapClient::__getLastRequestHeaders — Returns the SOAP headers from the last request
  • SoapClient::__getLastResponse — Returns last SOAP response
  • SoapClient::__getLastResponseHeaders — Returns the SOAP headers from the last response
  • SoapClient::__getTypes — Returns a list of SOAP types
  • SoapClient::__setCookie — The __setCookie purpose
  • SoapClient::__setLocation — Sets the location of the Web service to use
  • SoapClient::__setSoapHeaders — Sets SOAP headers for subsequent calls
  • SoapClient::__soapCall — Calls a SOAP function
  • SoapClient::SoapClient — SoapClient constructor

User Contributed Notes

Ricardo Pedrassani 03-Jun-2016 10:53
If the XML have identities with same name in different levels there is a solution. You don´t have to ever submit a raw XML (this PHP SOAP object don´t allows send a RAW XML), so you have to always translate your XML to a array, like the example below:

$originalXML = "
<xml>
  <firstClient>
      <name>someone</name>
      <adress>R. 1001</adress>
  </firstClient>
  <secondClient>
      <name>another one</name>
      <adress></adress>
  </secondClient>
</xml>"

//Translate the XML above in a array, like PHP SOAP function requires
$myParams = array('firstClient' => array('name' => 'someone',
                                  'adress' => 'R. 1001'),
            'secondClient' => array('name' => 'another one',
                                  'adress' => ''));

$webService = new SoapClient($someURL);
$result = $webService->someWebServiceFunction($myParams);
mcinantyspam at fejm dot pl 13-May-2016 08:32
Please note, that if you provide values that contain illegal xml characters (ASCII codes 0-8,  11-12, 14-15 - or x0-x8, xB-xC, xE-xF in hex), php's SoapClient will do send them in request, although such request is improper because it does not meet XML 1.0 requirements.
You always have to replace or remove these characters in your data before supplying them to SoapClient
info at nospam x valiton x com 13-Apr-2015 05:06
CAUTION:
I had quite a bit of trouble trying to make a request with fopen through a proxy to a secure url.  I kept getting a 400 Bad Request back from the remote host.  It was receiving the proxy url as the SNI host.  In order to get around this I had to explicity set the SNI host to the domain I was trying to reach.  It's apparently the issue outlined in this bug:

https://bugs.php.net/bug.php?id=63519

<?php
$domain = parse_url($file, PHP_URL_HOST);
$proxy_string = "tcp://" . WP_PROXY_HOST  . ":" . WP_PROXY_PORT;
$opts = array(
    'http' => array( 'proxy' => $proxy_string ),
    'ssl' => array( 'SNI_enabled' => true, 'SNI_server_name' => $domain));
$context = stream_context_create($opts);
$handle = fopen( $file, 'r', false, $context );
?>

src:
http://php.net/manual/en/context.http.php#114314
acopantepuy at gmail dot com 11-Apr-2015 11:45
when they want to pass variables into the http header that is how it is done:

<?php

$aHTTP['http']['header'] =  "User-Agent: PHP-SOAP/5.5.11\r\n";

$aHTTP['http']['header'].= "username: XXXXXXXXXXX\r\n"."password: XXXXX\r\n";

$context = stream_context_create($aHTTP);

$client=new SoapClient("https://ocppws-cert.extra.bcv.org.ve:443/AltoValor/BancoUniversal?WSDL",array('trace' => 1,"stream_context" => $context));

$result = $client->jornadaActiva();
var_dump($result);
?>
acopantepuy at gmail dot com 11-Apr-2015 11:42
when they want to pass variables into the http header that is how it is done:

<?php

$aHTTP['http']['header'] =  "User-Agent: PHP-SOAP/5.5.11\r\n";

$aHTTP['http']['header'].= "username: C1040001760102\r\n"."password: bcv2020\r\n";

$context = stream_context_create($aHTTP);

$client=new SoapClient("https://ocppws-cert.extra.bcv.org.ve:443/AltoValor/BancoUniversal?WSDL",array('trace' => 1,"stream_context" => $context));

$result = $client->jornadaActiva();
var_dump($result);
?>
romuloum at hotmail dot com 20-Feb-2015 01:22
After migrating to PHP 5.6.5, the soap 1.2 did not work anymore. So I solved the problem by adding optional parameters SSL.
My error: failed to load external entity
How to solve:
        // options for ssl in php 5.6.5
        $opts = array(
            'ssl' => array('ciphers'=>'RC4-SHA', 'verify_peer'=>false, 'verify_peer_name'=>false)
        );
        // SOAP 1.2 client
        $params = array ('encoding' => 'UTF-8', 'verifypeer' => false, 'verifyhost' => false, 'soap_version' => SOAP_1_2, 'trace' => 1, 'exceptions' => 1, "connection_timeout" => 180, 'stream_context' => stream_context_create($opts) );
        $oSoapClient = new SoapClient ( $url . "?WSDL", $params );
wira dot sanjaya dot saputro at gmail dot com 05-Jan-2015 08:59
Hi,

can you more specified how to solve this?
i also stuck in here.

Thanks
softontherocks at gmail dot com 22-Nov-2014 05:32
I posted in this URL http://softontherocks.blogspot.com/2014/02/web-service-soap-con-php.html a full example of a nusoap web service.

There is defined the server and the cliente who calls the web service.

I hope it would be useful for you.
martin dot schenck at plugsurfing dot com 16-Sep-2014 12:34
If you want to connect to a server that only supports SSLv2/3 and/or TLS 1.0 (no TLS 2 or 3), tell the SOAP client if you get a connection error by setting the appropriate stream context:
<?php
  $opts = array(
    'ssl' => array('ciphers'=>'RC4-SHA')
  );

  $objSoapClient = new SoapClient(
    'https://example.com/?wsdl',
    array ( "encoding"=>"ISO-8859-1",
    'stream_context' => stream_context_create($opts)
    // your options
    );
?>

You might need to disable the SOAP caching of WSDLs in order for the SOAP client to use the new context.
Stefan 03-Jun-2014 08:04
There is a known bug with some versions of Xdebug which can cause SoapClient to not throw an exception but instead cause a fatal error.

Surround the SoapClient call with xdebug_disable(); and xdebug_enable();  to work around this problem.

For reference:

http://bugs.xdebug.org/view.php?id=249
https://bugs.php.net/bug.php?id=47584
Anonymous 13-Sep-2013 06:25
I got the following error when trying to load the WSDL file:

Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: <message> 'MessageName' already defined

In my case there was no double definitions but it was including other files - I think one of them contained a duplicate message with a different namespace (which SoapClient doesn't like).

I resolved it by loading the WSDL in SoapUI, right-clicking and selecting "Export definition". The WSDL that then got created worked fine. Hope this helps somebody.
Guillermo Prandi 20-Feb-2012 11:10
To make an HTTPS call with a client certificate, you can do it this way:

1) Create a file containing both the key and the signed certificate. I did this by concatenating my (.pem) key and the certificate I've got signed from my CA (also .pem format). The file looked like this:

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKagQC1N27Ilb9pWil2NaX2qM8FquXBXK5T1AydOv7sCotsc8MAwbi7
......(snip)......
wAiOCD4K9TyMS76pIS8UyfJl/oIrn7EF24BUpaUfsh8=
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
MIIC/zCCAmigAwIBAgIEAIl1JzANBgkqhkiG9w0BAQQuADBQMQswCQYxVQQGEaJV
......(snip)......
eSPds1hLKYSg0bd3uI7LhaDLOC1PPgb77sYe/uYkUWuHBzllts5x/pfue0zaIBKG
Omjy
-----END CERTIFICATE-----

2) Reference your certificate in the creation of the SOAP client, like this:

$protected_url = "https://my-server/?wsdl";
$my_cert_file = "/my/path/to/mycert.pem";

$client = new SoapClient($protected_url, array('local_cert', $my_cert_file) );
egiardina at badatmath dot com 04-Aug-2011 03:05
This one drove me nuts... if you are connecting to a web service run on Cassini, (Visual Studio's web server) from php, you may not be able to call web service functions or load a WSDL out-of-the-box.

My set up is that I am using xampp on my windows development machine, and also using Visual Studio's built in web-server. I built a web service in visual studio. I wrote a simple PHP script, running under xampp, to access this service, so the two projects can 'talk' to each other. What I found was the WSDL was never loaded.

So I moved the WSDL local to the PHP file and accessed it directly. Still no calls. Turns out, PHP's SoapClient seems to have a problem with 'localhost' as a web service endpoint. So I hand-edited the web service endpoint URL to read 127.0.0.1 instead of localhost, and Voila! Web Service calls work:

Here is my PHP, now working:
<?php
$client = new SoapClient("http://localhost/code/soap.wsdl");
$something =  $client->HelloWorld(array());
echo $something->HelloWorldResult;
die();

?>

Here is my modified WSDL:
  <wsdl:service name="MyService">
    <wsdl:port name="MyServiceSoap" binding="tns:MyServiceSoap">
      <soap:address location="http://127.0.0.1:55274/MyService/MyService.asmx" />
....

    </wsdl:port>
stepan dot zarubin at gmail dot com 30-Jun-2011 08:51
Well, this example works fine:

<?php
try {
    $x = @new SoapClient("non-existent.wsdl");
} catch (Exception $e) {
    echo $e->getMessage();
}
?>

Just make sure use NEW with @.
jjlopez 09-Mar-2011 08:36
If you are making soap calls in WSDL mode , and the address of your web service includes a port different from 80 (like http://my_ip_address:8080//service.asmx?wsdl), the WSDL file is fetched correctly, but all subsequent requests are made without any port in the host field. This causes a SoapFault exception when trying to call any of the service's methods.

You need to redefine the soapClient class and force the port in each call.

See this example:

http://www.victorstanciu.ro/php-soapclient-port-bug-workaround/
hugues at zonereseau dot com 18-Feb-2011 06:17
When you need to connect to services requiring to send extra header use this method.

Here how we can to it with PHP and SoapClient

<?php
class exampleChannelAdvisorAuth
{
    public $DeveloperKey;
    public $Password;

    public function __construct($key, $pass)
    {
        $this->DeveloperKey = $key;
        $this->Password = $pass;
    }
}

$devKey        = "";
$password    = "";
$accountId    = "";

// Create the SoapClient instance
$url         = "";
$client     = new SoapClient($url, array("trace" => 1, "exception" => 0));

// Create the header
$auth         = new ChannelAdvisorAuth($devKey, $password);
$header     = new SoapHeader("http://www.example.com/webservices/", "APICredentials", $auth, false);

// Call wsdl function
$result = $client->__soapCall("DeleteMarketplaceAd", array(
    "DeleteMarketplaceAd" => array(
        "accountID"        => $accountId,
        "marketplaceAdID"    => "9938745"        // The ads ID
    )
), NULL, $header);

// Echo the result
echo "<pre>".print_r($result, true)."</pre>";
if($result->DeleteMarketplaceAdResult->Status == "Success")
{
    echo "Item deleted!";
}
?>
fourat dot zouari at tritux dot com 20-Jun-2009 05:08
In addition to the KeepAlive trick which is a "server-side" modification, on the "client side" default_socket_timeout should be increased from its default value (60) when you deal with ~slow SOAP servers.
As for the KeepAlive, if creating a new separate vhost for the soap api is not possible, you can add this to your existing vhost: BrowserMatch "^PHP-SOAP" nokeepalive
where PHP-SOAP is the agent name of your soap client, if you dont know what agent name your client use, just checkout the access.log of your apache.
peter dot hansen at fastit dot net 01-May-2009 03:57
When you get errors like:
"Fatal error: Uncaught SoapFault exception: [HTTP] Error Fetching http headers in"
after a few (time intensive) SOAP-Calls, check your webserver-config.

Sometimes the webservers "KeepAlive"-Setting tends to result in this error. For SOAP-Environments I recommend you to disable KeepAlive.

Hint: It might be tricky to create a dedicated vhost for your SOAP-Gateways and disable keepalive just for this vhost because for normal webpages Keepalive is a nice speed-boost.


Calls a SOAP function (deprecated)

  • ≪ SoapClient
  • SoapClient::__construct ≫
  • PHP Manual
  • SoapClient
  • Calls a SOAP function (deprecated)

SoapClient::__call

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__call — Calls a SOAP function (deprecated)

설명

public mixed SoapClient::__call ( string $function_name , string $arguments )

Calling this method directly is deprecated. Usually, SOAP functions can be called as methods of the SoapClient object; in situations where this is not possible or additional options are needed, use SoapClient::__soapCall().

User Contributed Notes

philipp dot gruber at catchoftheday dot com dot au 07-Apr-2014 09:27
If you are using a WSDL, the library will strip out anything from your parameters that is not defined in WSDL, without giving you any notice about this.

So if your parameters are not fully matching the WSDL, it will simply send no parameters at all.
This can be a bit hard to debug if you don't have access to the target server.

__soapCall() expects parameters in an array called 'parameters' as opposed to calling the function via it's WSDL name, where it accepts the parameters as a plain array.

I.e. if a function called sendOrder expects a parameter (array) called orderDetails, you can call it like this:

$orderDetails = array(/* your data */);
$soap->sendOrder(array('orderDetails' => $orderDetails));

Which is equivalent to:

$client->__soapCall('sendOrder', array('parameters' => array('orderDetails' => $orderDetails)));

Note the additional 'parameters' key used in __soapCall().
KRavEN 17-Jun-2009 03:56
extend of __call thats adds a retry to handle the occasional 'Could not connect to host' exceptions

<?php
class LocalSoapClient extends SoapClient
{

  public function __call($function_name, $arguments)
  {
    $result = false;
    $max_retries = 5;
    $retry_count = 0;

    while(! $result && $retry_count < $max_retries)
    {
      try
      {
        $result = parent::__call($function_name, $arguments);
      }
      catch(SoapFault $fault)
      {
        if($fault->faultstring != 'Could not connect to host')
        {
          throw $fault;
        }
      }
      sleep(1);
      $retry_count ++;
    }
    if($retry_count == $max_retries)
    {
      throw new SoapFault('Could not connect to host after 5 attempts');
    }
    return $result;
  }
}
?>
bananos at dev dot co dot ua 11-Apr-2008 02:13
I'm writing PHP SOAP client (based on php_soap extension) which uses Google AdService,
today, I tried to create sample Ad using   AdService::addAds()  and discovered that my SOAP library does not pass
"headline", "description1", "description2"  parameters in SOAP request.

First, I thought that this was something wrong with my data, coz I was getting validation errors like
"One or more input elements failed validation."   Then I decided to take a look at WSDL description
https://adwords.google.com/api/adwords/v11/AdService?wsdl

And voila !  I've found this:

<complexType name="Ad" abstract="true">
<sequence>
  <element name="adGroupId" type="xsd:int"/>
  <element name="adType" nillable="true" minOccurs="0" type="impl:AdType"/>
  <element name="destinationUrl" nillable="true" minOccurs="0" type="xsd:string"/>
  <element name="disapproved" type="xsd:boolean"/>
  <element name="displayUrl" nillable="true" minOccurs="0" type="xsd:string"/>
  <element name="exemptionRequest" nillable="true" minOccurs="0" type="xsd:string"/>
  <element name="id" type="xsd:long"/>
  <element name="status" nillable="true" minOccurs="0" type="impl:AdStatus"/>
</sequence>

</complexType>

There is no any ''description", "headline1", "headline2", however there is another complexType

<complexType name="TextAd">
    <complexContent>
    <extension base="impl:Ad">
    <sequence>

<element name="description1" nillable="true" type="xsd:string"/>
<element name="description2" nillable="true" type="xsd:string"/>
<element name="headline" nillable="true" type="xsd:string"/>
</sequence>

</extension>
</complexContent>
</complexType>

So, the thing is when you are working with php_soap be aware of complexType extensions which does not work in php_soap when you try to do things like:

<?php

$client = new
    SoapClient(
       "https://adwords.google.com/api/adwords/v11/AdService?wsdl",
        array(  'trace'      => true,
                'exceptions' => true,
             )

    );

$entropy     = substr(md5(rand(0, time())), 0, 10);
//create Ad test structure
$sample_ad =  array(
               "id" => 0,
               "adGroupId" => 0,
               "adType" => 'TextAd',
               "disapproved" => false,
               "destinationUrl" => 'http://test.com',
               "displayUrl" => 'www.Test.com',
               "status" => "Paused", //  Enabled / Disabled
               "descr_iption1" => 'D1_'.$entropy,
               "des_cription2" => 'D2_'.$entropy,
               "head_line" => 'H_'.$entropy
        );

$client->addAds(array('ads' => array($sample_ad) );
?>
Samil Abud 19-Dec-2007 09:00
Hi,
This is a good example, for the SOAP function "__call".
However it is deprecated.

<?php
$wsdl = "http://webservices.tekever.eu/ctt/?wsdl";
$int_zona = 5;
$int_peso = 1001;
$cliente = new SoapClient($wsdl);
print "<p>Envio Internacional: ";
$vem = $cliente->__call('CustoEMSInternacional',array($int_zona, $int_peso));
print $vem;
print "</p>";
?>


SoapClient constructor

  • ≪ SoapClient::__call
  • SoapClient::__doRequest ≫
  • PHP Manual
  • SoapClient
  • SoapClient constructor

SoapClient::__construct

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__construct — SoapClient constructor

설명

public SoapClient::SoapClient ( mixed $wsdl [, array $options ] )

이 함수는 다음 함수의 별칭입니다: SoapClient::SoapClient()

User Contributed Notes

There are no user contributed notes for this page.


Performs a SOAP request

  • ≪ SoapClient::__construct
  • SoapClient::__getFunctions ≫
  • PHP Manual
  • SoapClient
  • Performs a SOAP request

SoapClient::__doRequest

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__doRequest — Performs a SOAP request

설명

public string SoapClient::__doRequest ( string $request , string $location , string $action , int $version [, int $one_way = 0 ] )

Performs SOAP request over HTTP.

This method can be overridden in subclasses to implement different transport layers, perform additional XML processing or other purpose.

인수

request

The XML SOAP request.

location

The URL to request.

action

The SOAP action.

version

The SOAP version.

one_way

If one_way is set to 1, this method returns nothing. Use this where a response is not expected.

반환값

The XML SOAP response.

변경점

버전 | 설명
5.1.3 | The one_way parameter was added.

예제

Example #1 SoapClient::__doRequest() example

<?php
function Add($x,$y) {
  return $x+$y;
}

class LocalSoapClient extends SoapClient {

  function __construct($wsdl, $options) {
    parent::__construct($wsdl, $options);
    $this->server = new SoapServer($wsdl, $options);
    $this->server->addFunction('Add');
  }

  function __doRequest($request, $location, $action, $version, $one_way = 0) {
    ob_start();
    $this->server->handle($request);
    $response = ob_get_contents();
    ob_end_clean();
    return $response;
  }

}

$x = new LocalSoapClient(NULL,array('location'=>'test://',
                                   'uri'=>'http://testuri.org'));
var_dump($x->Add(3,4));
?>

User Contributed Notes

tschallacka 22-Mar-2016 11:01
I just wanted to note in plain english how to build up this request, because I made some assumptions by just misterpreting the parameters.

This is for future reference(for myself) and others that might end up a bit mistified by just not understanding the finesses of soap and are trying to learn.

    $this->__doRequest(string $request , string $location , string $action , int $version [, int $one_way = 0 ] );

$request = The XML Soap enveloppe
$location = The url to the WSDL file. No matter that you defined this before when setting up the object, you need to reuse it here.
$action = the soap action to be performed. This is defined in the wsdl file and can be in the form of a single form or an url. it's just a parameter and might not be an actual valid url
$version = SOAP_1_1 =  content headers (Content-Type: text/xml; CHARSET=ks_c_5601-1987?)
                  SOAP_1_2 = content headers (Content-Type: application/soap+xml; CHARSET=ks_c_5601-1987; action="somesoapaction defined in $action")

If you send a SOAP_1_2 request to a SOAP_1_1 server you might get a reply in the following form

    HTTP/1.1 415 Cannot process the message because the content type 'application/soap+xml; CHARSET=ks_c_5601-1987; action="somesoapaction defined in $action"' was not the expected type 'text/xml; CHARSET=ks_c_5601-1987'.

You need to switch down to SOAP_1_1 in that case to get the proper formatting the server can understand
tbernard at qcsupply dot com 03-Feb-2015 02:08
If you're having trouble connecting to an authenticated SOAP service there's something important to note here.

__doRequest() is only used when a SOAPClient function is called, NOT when the WSDL if fetched and parsed.  That means if your WSDL file is not publicly accessible but also resides behind your authentication it won't be accessible by default.  Instead you'll have to create an overloaded stream wrapper and register it for whatever protocol (probably HTTP) you'll be using.
darren dot yee at emc dot com 14-Mar-2013 07:45
Note when extending __doRequest, calling __getLastRequest will probably report incorrect information unless you make sure to update the internal __last_request variable. Save yourself some headaches.

function __doRequest($request, $location, $action, $version) {
      $request = preg_replace('/abc/', 'def', $request);
      $ret = parent::__doRequest($request, $location, $action, $version);
      $this->__last_request = $request;
      return $ret;
}
alireza dot meskin at gmail 02-Apr-2012 09:29
Change Blocking mode for Socket stream and set Timeout for Soap requests

<?php

class TimeoutSoapClient extends SoapClient
{
    const TIMEOUT = 20;
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        $url_parts = parse_url($location);
        $host = $url_parts['host'];
        $http_req = 'POST '.$location.' HTTP/1.0'."\r\n";
        $http_req .= 'Host: '.$host."\r\n";
        $http_req .= 'SoapAction: '.$action."\r\n";
        $http_req .= "\r\n";
        $http_req .= $request;
        $port = 80;
        if ($url_parts['scheme'] == 'https')
        {
            $port = 443;
            $host = 'ssl://'.$host;
        }
        $socket = fsockopen($host, $port);
        fwrite($socket, $request);
        stream_set_blocking($socket, false);
        $response = '';
        $stop = microtime(true) + self::TIMEOUT;
        while (!feof($socket))
        {
            $response .= fread($socket, 2000);
            if (microtime(true) > $stop)
            {
                throw new SoapFault('Client', 'HTTP timeout');
            }
        }
        return $response;
    }
}
bwhitehead at tableausoftware dot no dot com dot spam 12-Aug-2011 11:49
Note that the SoapClient.__doRequest() method circumvents the throwing of SoapFault exceptions.

Specifically, if you call the __doRequest() method and it fails, it would normally throw a SoapFault exception.  However, the __doRequest() method doesn't actually throw the exception. Instead, the exception is saved in a class attribute called SoapFault.__soap_fault, and is actually thrown AFTER the __doRequest method completes (but the call stack will show that the exception was created inside the __doRequest method.

I successfully used the following code to query the locally cached exception object that was not thrown:

<?php
$exception = null;
try {
    $result = parent::__doRequest($request, $location, $action, $version, $one_way);
}
catch (SoapFault $sf) {
    //this code was not reached
    $exception = $sf;
}
catch (Exception $e) {
    //nor was this code reached either
    $exception = $e;
}
if((isset($this->__soap_fault)) && ($this->__soap_fault != null)) {
    //this is where the exception from __doRequest is stored
    $exception = $this->__soap_fault;
}

//decide what to do about the exception here
// [enter code here]
//or throw the exception
if($exception != null) {
    throw $exception;
}
//note: you may want to unset the __soap_fault value if you don't want it thrown again up the call stack
?>
psfere at hotmail dot com 21-Sep-2010 06:52
I was needing to add a blank soap header (<SOAP-ENV:Header /> and found no other place that has done this.  The only way I was able to support this was to extend SoapClient and re-define __doRequest.  Hope this helps someone or if there is support for this in the library, please point me in the right direction:

<?php
class MySoapCli extends SoapClient {
  function __doRequest($request, $location, $action, $version) {
    $dom = new DomDocument('1.0', 'UTF-8');
    $dom->preserveWhiteSpace = false;
    $dom->loadXML($request);
    $hdr = $dom->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'SOAP-ENV:Header');
    $dom->documentElement->insertBefore($hdr, $dom->documentElement->firstChild);
    $request = $dom->saveXML();
    return parent::__doRequest($request, $location, $action, $version);
  }
}
?>
lepidosteus 19-Nov-2009 11:59
If you happen to get an error during your request which says "SOAP-ERROR: Encoding: Can't decode apache map, only Strings or Longs are allowd as keys", the reason seems to be the response xml using integer for keys and php failling to understand them

Here is something that worked for me (converts integer keys to strings):

<?php
class mySoap extends SoapClient
{
    public function __doRequest($request, $location, $action, $version)
    {
        $result = parent::__doRequest($request, $location, $action, $version);
        $result = str_replace('<key xsi:type="xsd:int">', '<key xsi:type="xsd:string">', $result);
        return $result;
    }
}

// $soap = new mySoap(...
?>
Artur Graniszewski 08-Nov-2009 01:35
Beware of PHP incosistent behaviour in __doRequest() method. It seems that some arguments passed to this method are passed by reference!

If you try to create your own __doRequest() method and store it's arguments as SoapClient properties you will find that after __soapCall all of them will be null or unknown.

<?php
    protected $__soapAction = '';

    public function __doRequest($request, $location, $action, $version, $oneWay = 0) {
        ob_start();
        $this->server->handle($request);
        $response = ob_get_contents();
        ob_end_clean();
        $this->__soapAction = $action;
        return $response;
    }
?>

In above example $this->__soapAction will be null after $obj->__soapCall()..

To store $action value, you must cast it to a string (so PHP will be forced to create a new variable with different memory pointer):

<?php
    public function __doRequest($request, $location, $action, $version, $oneWay = 0) {
        ob_start();
        $this->server->handle($request);
        $response = ob_get_contents();
        ob_end_clean();
        $this->__soapAction = (string)$action;
        return $response;
    }
?>
james dot ellis at gmail dot com 04-Feb-2008 01:12
If your application interacts with SOAP services and you wish to cache the responses for consumption later, then overriding SoapClient::__doRequest is the way to go.

For instance, if you know that the information presented doesn't change that often and you don't want to do a superfluous HTTP request, you can grab a response from a local cache and let SoapClient do the transformation to PHP data types.

<?php
class YourNamespace_SoapClient_Local extends SoapClient {
    protected $cacheDocument = "";
    public function __construct($wsdl, $options) {
        parent::__construct($wsdl, $options);
    }

    /**
     * SetCacheDocument() sets the previously cached document contents
     */
    public function SetCacheDocument($document) {
        $this->cacheDocument = $document;
    }

    /**
     * __doRequest() overrides the standard SoapClient to handle a local request
     */
    public function __doRequest() {
        return $this->cacheDocument;
    }
}

//---- code snippet showing usage within a class
//$document is a cached SOAP response document from a previous request, saved with SoapClient::__getLastResponse() to some cache somewhere
//for the purpose of this example, it is assumed that $this->wsdl, $this->options, $this->method and $this->params are set.

public function SoapRequest($document) {
    $method = $this->method;
    if($document == "") {
        //uncached
        try {
            //default options
            $client = new SoapClient($this->wsdl, $this->options);
            $result = $client->$method($this->params);
            //send the response to the cache
            $this->CacheResponse($client->__getLastResponse());
        } catch(SoapFault $fault) {
            //log something
            return FALSE;
        }
    } else {
        //cached document
        try {
            /**
             * the WSDL needs to be set to allow the method to be called on the client object
             * and to trigger SoapClient to decode the response to native data types
             */
            $client = new YourNamespace_SoapClient_Local($this->wsdl, $this->options);
            $client->SetCacheDocument($document);
            $result = $client->$method($this->params);
        } catch (SoapFault $fault) {
            //log something
            return FALSE;
        }
    }
    return $result;
}
?>

I'll leave you to work out the caching, plenty of options there.. ;)
albert at jool dot nl 26-Mar-2007 03:28
If you want to communicate with a default configured ASP.NET server with SOAP 1.1 support, override your __doRequest with the following code. Adjust the namespace parameter, and all is good to go.

<?php
class MSSoapClient extends SoapClient {

    function __doRequest($request, $location, $action, $version) {
        $namespace = "http://tempuri.com";

        $request = preg_replace('/<ns1:(\w+)/', '<$1 xmlns="'.$namespace.'"', $request, 1);
        $request = preg_replace('/<ns1:(\w+)/', '<$1', $request);
        $request = str_replace(array('/ns1:', 'xmlns:ns1="'.$namespace.'"'), array('/', ''), $request);

        // parent call
        return parent::__doRequest($request, $location, $action, $version);
    }
}

$client = new MSSoapClient(...);
?>

Hope this will save people endless hours of fiddling...
jfitz at spacelink dot com 05-Dec-2006 06:56
Note that __getLastRequest() data are buffered _before_ the call to __doRequest().  Thus any modifications you make to the XML while in __doRequest() will not be visible in the output of __getLastRequest().   This is so in at least v5.2.0
06-Jul-2006 10:17
Do you have problems with the PHP5 SoapClient when you need to send a request to a service with a ComplexType parameter?

Maybe because my service is build in Delphi with REMObjects SDK 3.0 I had the problems, maybe not. Anyway, this was my remedy:
<?php
$versie = new stdClass();//define a basic class object
$versie->versieID = $aVersie->versieID();//fill it with the exact attributes as your complextype Object in the wsdl is
$versie->versieNummer = $aVersie->versieNummer();
$versie->isActief = $aVersie->isActief();

$soapVersieType = new SoapVar($versie , SOAP_ENC_OBJECT, "Versie", "http://127.0.0.1:8999/SOAP?wsdl"); //create the complex soap type, Versie is the name of my complex type in the wsdl, the latter url beeing the location of my wsdl.

try{
  $result =  $soapClient->BewaarVersie($this->sessieId,$soapVersieType); //BewaarVersie is a function derived from my WSDL with two params.
}
catch(SoapFault $e){
  trigger_error('Something soapy went wrong: '.$e->faultstring,E_USER_WARNING);            }
?>
After some more testing i found out that the conversion to the StdClass() object was not required. My 'Versie' local object has the attributes for the 'Versie' wsdl complex type defined as private vars and give no pain when i create the SoapVar with an instance of the local 'Versie' Object.
metator at netcabo dot pt 21-Oct-2005 01:32
You can use this method to correct the SOAP request before sending it, if necessary. You can use the DOM API to accomplish that.

<?php

public ExtendedClient extends SoapClient {

   function __construct($wsdl, $options = null) {
      parent::__construct($wsdl, $options);
   }

   function __doRequest($request, $location, $action, $version) {
      $dom = new DOMDocument('1.0');

      try {

         //loads the SOAP request to the Document
         $dom->loadXML($request);

      } catch (DOMException $e) {
         die('Parse error with code ' . $e->code);
      }

      //create a XPath object to query the request
      $path = new DOMXPath($dom);

      //search for a node
      $nodesToFix = $path->query('//SOAP-ENV:Envelope/SOAP-ENV:Body/path/to/node');

      //check if nodes are ok
      $this->checkNodes($path, $nodesToFix);

      //save the modified SOAP request
      $request = $dom->saveXML();

      //doRequest
      return parent::__doRequest($request, $location, $action, $version);
   }

   function checkNodes(DOMXPath $path, DOMNodeList $nodes) {
      //iterate through the node list
      for ($i = 0; $ < $nodes->length; $i++) {
         $aNode = $nodes->item($i);

         //just an example
         if ($node->nodeValue == null) {
            //do something. For instance, let's remove it.
            $node->parentNode->removeChild($node);
         }
      }
   }
}
?>

This gives the developer the chance to solve interoperability problems with a web service.


Returns list of available SOAP functions

  • ≪ SoapClient::__doRequest
  • SoapClient::__getLastRequest ≫
  • PHP Manual
  • SoapClient
  • Returns list of available SOAP functions

SoapClient::__getFunctions

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__getFunctions — Returns list of available SOAP functions

설명

public array SoapClient::__getFunctions ( void )

Returns an array of functions described in the WSDL for the Web service.

Note:

이 함수는 WSDL 모드에서만 작동합니다.

인수

이 함수는 인수가 없습니다.

반환값

The array of SOAP function prototypes, detailing the return type, the function name and type-hinted paramaters.

예제

Example #1 SoapClient::__getFunctions() example

<?php
$client = new SoapClient('http://soap.amazon.com/schemas3/AmazonWebServices.wsdl');
var_dump($client->__getFunctions());
?>

위 예제의 출력:

array(26) {
  [0]=>
  string(70) "ProductInfo KeywordSearchRequest(KeywordRequest $KeywordSearchRequest)"
  [1]=>
  string(79) "ProductInfo TextStreamSearchRequest(TextStreamRequest $TextStreamSearchRequest)"
  [2]=>
  string(64) "ProductInfo PowerSearchRequest(PowerRequest $PowerSearchRequest)"
...
  [23]=>
  string(107) "ShoppingCart RemoveShoppingCartItemsRequest(RemoveShoppingCartItemsRequest $RemoveShoppingCartItemsRequest)"
  [24]=>
  string(107) "ShoppingCart ModifyShoppingCartItemsRequest(ModifyShoppingCartItemsRequest $ModifyShoppingCartItemsRequest)"
  [25]=>
  string(118) "GetTransactionDetailsResponse GetTransactionDetailsRequest(GetTransactionDetailsRequest $GetTransactionDetailsRequest)"
}

참고

  • SoapClient::SoapClient() - SoapClient constructor

User Contributed Notes

jvanoort at simplexis dot nl 06-Jan-2016 12:32
_getFunctions can return null, not only array. This happens if the WSDL has not been parsed yet. This may be helpful to someone debugging, just like I was a few minutes ago when writing an extension to SoapClient.
Beebs 13-Nov-2013 11:26
The following code works for me while it appears soap.amazon.com has been deprecated and taken out of service. http://webservices.amazon.com has replaced Amazon's SOAP.

<?php
$client = new SoapClient('http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl');
var_dump($client->__getFunctions());
?>
hasegeli at arebt dot com 02-Jun-2009 09:19
This function did not work if schema is not in wsdl file.
Dougal Matthews 17-Jul-2008 10:24
example for amazon webservice

<?php

$wsdl_url =
  "http://soap.amazon.com/schemas3/AmazonWebServices.wsdl";
$client     = new SoapClient($wsdl_url);
var_dump($client->__getFunctions());

?>


Returns last SOAP request

  • ≪ SoapClient::__getFunctions
  • SoapClient::__getLastRequestHeaders ≫
  • PHP Manual
  • SoapClient
  • Returns last SOAP request

SoapClient::__getLastRequest

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__getLastRequest — Returns last SOAP request

설명

public string SoapClient::__getLastRequest ( void )

Returns the XML sent in the last SOAP request.

Note:

This method works only if the SoapClient object was created with the trace option set to TRUE.

인수

이 함수는 인수가 없습니다.

반환값

The last SOAP request, as an XML string.

예제

Example #1 SoapClient::__getLastRequest() example

<?php
$client = new SoapClient("some.wsdl", array('trace' => 1));
$result = $client->SomeFunction();
echo "REQUEST:\n" . $client->__getLastRequest() . "\n";
?>

참고

  • SoapClient::__getLastRequestHeaders() - Returns the SOAP headers from the last request
  • SoapClient::__getLastResponse() - Returns last SOAP response
  • SoapClient::__getLastResponseHeaders() - Returns the SOAP headers from the last response

User Contributed Notes

hejdavladislav at gmail dot com 15-Sep-2016 07:20
Note that when you create SoapClient with option "trace" set to FALSE or omit it, than "__getLastRequest()" always returns NULL.
LZhang 11-Jul-2014 05:22
Creating an xml file and link to the file is my way.

$outputxml = '/output.xml';

file_put_contents($_SERVER['DOCUMENT_ROOT'] . $outputxml, $client->__getLastRequest());

echo '<a href="' . $outputxml . '">here</a>';
mayurvirkar at gmail dot com 09-Sep-2013 05:37
Just to make it a bit more readable

echo "REQUEST:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastRequest())) . "\n";
echo "Response:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastResponse())) . "\n";

PS: If you are using \n then you need to enclose above statements in <pre>. You can also use <br />, but it gets a bit messy.
Anonymous 26-Jan-2010 10:32
Adding htmlentities() can be helpful since it makes the XML visible in your browser without needing to view the source.

<?php

echo "REQUEST:\n" . htmlentities($client->__getLastRequest()) . "\n";

?>
Ersin DOGAN 12-Apr-2009 07:40
I guess many peoples calls getLastRequest and it returns nothing. "Heey where is the my last request". Now we will see our request,  when you created a SoapClient instance, you should give a option parameter as below :

<?php
// below $option=array('trace',1);
// correct one is below
$option=array('trace'=>1);

$client=new SoapClient('some.wsdl',$option);

try{
  $client->aMethodAtRemote();
}catch(SoapFault $fault){
  // <xmp> tag displays xml output in html
  echo 'Request : <br/><xmp>',
  $client->__getLastRequest(),
  '</xmp><br/><br/> Error Message : <br/>',
  $fault->getMessage();
}
?>

"trace" parameter enables output of request. Now, you should see SOAP request.
Omar Nabil 12-Nov-2008 03:14
the getLastResult function doesn't require that the server answers.
as my browser handled and hided the xml code I thought the function doesn't return the last request


Returns the SOAP headers from the last request

  • ≪ SoapClient::__getLastRequest
  • SoapClient::__getLastResponse ≫
  • PHP Manual
  • SoapClient
  • Returns the SOAP headers from the last request

SoapClient::__getLastRequestHeaders

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__getLastRequestHeaders — Returns the SOAP headers from the last request

설명

public string SoapClient::__getLastRequestHeaders ( void )

Returns the SOAP headers from the last request.

Note:

This function only works if the SoapClient object was created with the trace option set to TRUE.

인수

이 함수는 인수가 없습니다.

반환값

The last SOAP request headers.

예제

Example #1 SoapClient::__getLastRequestHeaders() example

<?php
$client = SoapClient("some.wsdl", array('trace' => 1));
$result = $client->SomeFunction();
echo "REQUEST HEADERS:\n" . $client->__getLastRequestHeaders() . "\n";
?>

참고

  • SoapClient::__getLastResponseHeaders() - Returns the SOAP headers from the last response
  • SoapClient::__getLastRequest() - Returns last SOAP request
  • SoapClient::__getLastResponse() - Returns last SOAP response

User Contributed Notes

mischa molhoek 02-Jun-2015 05:48
it seems to me that is does not return the SOAP headers as it states in the explanation, but the request headers....


Returns last SOAP response

  • ≪ SoapClient::__getLastRequestHeaders
  • SoapClient::__getLastResponseHeaders ≫
  • PHP Manual
  • SoapClient
  • Returns last SOAP response

SoapClient::__getLastResponse

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__getLastResponse — Returns last SOAP response

설명

public string SoapClient::__getLastResponse ( void )

Returns the XML received in the last SOAP response.

Note:

This method works only if the SoapClient object was created with the trace option set to TRUE.

인수

이 함수는 인수가 없습니다.

반환값

The last SOAP response, as an XML string.

예제

Example #1 SoapClient::__getLastResponse() example

<?php
$client = SoapClient("some.wsdl", array('trace' => 1));
$result = $client->SomeFunction();
echo "Response:\n" . $client->__getLastResponse() . "\n";
?>

참고

  • SoapClient::__getLastResponseHeaders() - Returns the SOAP headers from the last response
  • SoapClient::__getLastRequest() - Returns last SOAP request
  • SoapClient::__getLastRequestHeaders() - Returns the SOAP headers from the last request

User Contributed Notes

mayurvirkar at gmail dot com 09-Sep-2013 05:38
Just to make it a bit more readable

echo "REQUEST:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastRequest())) . "\n";
echo "RESPONSE:\n" . htmlentities(str_ireplace('><', ">\n<", $client->__getLastResponse())) . "\n";

PS: If you are using \n then you need to enclose above statements in <pre>. You can also use <br />, but it gets a bit messy.
bshafs at gmail dot com 22-Jun-2011 09:45
You don't need to surround SoapClient in a try-catch block.  Just set the "exceptions" option to false, and the SoapFault instance will be returned instead of thrown.
ceo at l-i-e dot com 04-Jan-2006 10:31
D'oh!
That example needs:
$soapClient = new SoapClient($url, array('trace'=>1));
to turn ON tracing in the first place.
ceo at l-i-e dot com 04-Jan-2006 10:30
You almost for sure will need to wrap a try/catch block around your SOAP call in order to use these to debug something that's not working.

Otherwise, PHP throws a fatal error before you can execute this function.

For example:
<?php
    $soapClient = new SoapClient($url);
    echo htmlentities($soapClient->__getFunctions());
    //Assume that has output 'someFunction' (among others)
    try {
        $results = $soapClient->someFunction(...);
    }
    catch (SoapFault $soapFault) {
        var_dump($soapFault);
        echo "Request :<br>", htmlentities($soapClient->__getLastRequest()), "<br>";
        echo "Response :<br>", htmlentities($soapClient->__getLastResponse()), "<br>";
    }
?>

Without try/catch, your just get the Fatal Error and PHP commits suicide before you can call __getLastRequest/__getLastResponse


Returns the SOAP headers from the last response

  • ≪ SoapClient::__getLastResponse
  • SoapClient::__getTypes ≫
  • PHP Manual
  • SoapClient
  • Returns the SOAP headers from the last response

SoapClient::__getLastResponseHeaders

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__getLastResponseHeaders — Returns the SOAP headers from the last response

설명

public string SoapClient::__getLastResponseHeaders ( void )

Returns the SOAP headers from the last response.

Note:

This function only works if the SoapClient object was created with the trace option set to TRUE.

인수

이 함수는 인수가 없습니다.

반환값

The last SOAP response headers.

예제

Example #1 SoapClient::__getLastResponse() example

<?php
$client = SoapClient("some.wsdl", array('trace' => 1));
$result = $client->SomeFunction();
echo "RESPONSE HEADERS:\n" . $client->__getLastResponseHeaders() . "\n";
?>

참고

  • SoapClient::__getLastRequestHeaders() - Returns the SOAP headers from the last request
  • SoapClient::__getLastRequest() - Returns last SOAP request
  • SoapClient::__getLastResponse() - Returns last SOAP response

User Contributed Notes

noncey 30-Sep-2015 09:33
For long running scripts which may be set to a longer timeout in the PHP script, be aware that the IIS connection timeout settings do not restart when you initiate a SOAP call even though you are using a new web address than your current running script.

Because of this, we noted a SOAP call that seemed to not return headers from a Web Service. It was found to be related to IIS timing out. This was in IIS 7.5. The resolution was to increase the connection timeout for IIS at the website/domain level.

There were no faults or exceptions captured when this occurred so your only indicator is that you have no header information.
jeroen{at}asystance{dot}nl 17-Aug-2009 12:20
Note SoapClient does not wait for a response for one-way operations (i.e. <wsdl:operation>s that do not have a <wsdl:response>)! To see if a request failed, use
$sc = new SoapClient( 'some.wsdl', array('features'=>SOAP_WAIT_ONE_WAY_CALLS));

See http://bugs.php.net/bug.php?id=36083 and http://bugs.php.net/bug.php?id=49278


Returns a list of SOAP types

  • ≪ SoapClient::__getLastResponseHeaders
  • SoapClient::__setCookie ≫
  • PHP Manual
  • SoapClient
  • Returns a list of SOAP types

SoapClient::__getTypes

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__getTypes — Returns a list of SOAP types

설명

public array SoapClient::__getTypes ( void )

Returns an array of types described in the WSDL for the Web service.

Note:

이 함수는 WSDL 모드에서만 작동합니다.

인수

이 함수는 인수가 없습니다.

반환값

The array of SOAP types, detailing all structures and types.

예제

Example #1 SoapClient::__getTypes() example

<?php
$client = new SoapClient('http://soap.amazon.com/schemas3/AmazonWebServices.wsdl');
var_dump($client->__getTypes());
?>

위 예제의 출력:

array(88) {
  [0]=>
  string(30) "ProductLine ProductLineArray[]"
  [1]=>
  string(85) "struct ProductLine {
 string Mode;
 string RelevanceRank;
 ProductInfo ProductInfo;
}"
  [2]=>
  string(105) "struct ProductInfo {
 string TotalResults;
 string TotalPages;
 string ListName;
 DetailsArray Details;
}"
...
  [85]=>
  string(32) "ShortSummary ShortSummaryArray[]"
  [86]=>
  string(121) "struct GetTransactionDetailsRequest {
 string tag;
 string devtag;
 string key;
 OrderIdArray OrderIds;
 string locale;
}"
  [87]=>
  string(75) "struct GetTransactionDetailsResponse {
 ShortSummaryArray ShortSummaries;
}"
}

참고

  • SoapClient::SoapClient() - SoapClient constructor

User Contributed Notes

sanok 24-Sep-2014 04:19
Great snippet Felipe!

Thank you very much, very useful!
felipe dot cwb at hotmail dot com 22-Jul-2014 03:50
<?php
// to see formated types

$soap = new SoapClient('http://domain.com/ws.php?WSDL');

echo '<pre>';
echo '<h2>Types:</h2>';
$types = $soap->__getTypes();
foreach ($types as $type) {
    $type = preg_replace(
        array('/(\w+) ([a-zA-Z0-9]+)/', '/\n /'),
        array('<font color="green">${1}</font> <font color="blue">${2}</font>', "\n\t"),
        $type
    );
    echo $type;
    echo "\n\n";
}
echo '</pre>';


The __setCookie purpose

  • ≪ SoapClient::__getTypes
  • SoapClient::__setLocation ≫
  • PHP Manual
  • SoapClient
  • The __setCookie purpose

SoapClient::__setCookie

(PHP 5 >= 5.0.4, PHP 7)

SoapClient::__setCookie — The __setCookie purpose

설명

public void SoapClient::__setCookie ( string $name [, string $value ] )

Defines a cookie to be sent along with the SOAP requests.

Note:

Calling this method will affect all following calls to SoapClient methods.

인수

name

The name of the cookie.

value

The value of the cookie. If not specified, the cookie will be deleted.

반환값

값을 반환하지 않습니다.

User Contributed Notes

Jaygiri Goswami 17-Dec-2011 10:22
You can access the cookies sent by SOAP server using $client->_cookies

Most you need to send session cookies back to server.
lukasz at szostak dot biz 14-Aug-2006 07:53
On the server side you can access the cookie via $_COOKIE['cookie_name'].

Also remember that since HTTP is a stateless protocol, you have to send the cookie with every single SOAP call in order for it to be visible at server site.

It seems obvious but might save one a lot of time.


Sets the location of the Web service to use

  • ≪ SoapClient::__setCookie
  • SoapClient::__setSoapHeaders ≫
  • PHP Manual
  • SoapClient
  • Sets the location of the Web service to use

SoapClient::__setLocation

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__setLocation — Sets the location of the Web service to use

설명

public string SoapClient::__setLocation ([ string $new_location ] )

Sets the endpoint URL that will be touched by following SOAP requests. This is equivalent to specifying the location option when constructing the SoapClient.

Note:

Calling this method is optional. The SoapClient uses the endpoint from the WSDL file by default.

인수

new_location

The new endpoint URL.

반환값

The old endpoint URL.

예제

Example #1 SoapClient::__setLocation() example

<?php
$client = new SoapClient('http://example.com/webservice.php?wsdl');

$client->__setLocation('http://www.somethirdparty.com');

$old_location = $client->__setLocation(); // unsets the location option

echo $old_location;

?>

위 예제의 출력 예시:

http://www.somethirdparty.com

참고

  • SoapClient::SoapClient() - SoapClient constructor

User Contributed Notes

There are no user contributed notes for this page.


Sets SOAP headers for subsequent calls

  • ≪ SoapClient::__setLocation
  • SoapClient::__soapCall ≫
  • PHP Manual
  • SoapClient
  • Sets SOAP headers for subsequent calls

SoapClient::__setSoapHeaders

(PHP 5 >= 5.0.5, PHP 7)

SoapClient::__setSoapHeaders — Sets SOAP headers for subsequent calls

설명

public bool SoapClient::__setSoapHeaders ([ mixed $soapheaders ] )

Defines headers to be sent along with the SOAP requests.

Note:

Calling this method will replace any previous values.

인수

soapheaders

The headers to be set. It could be SoapHeader object or array of SoapHeader objects. If not specified or set to NULL, the headers will be deleted.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 SoapClient::__setSoapHeaders() example

<?php

$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));
$header = new SoapHeader('http://soapinterop.org/echoheader/',
                            'echoMeStringRequest',
                            'hello world');

$client->__setSoapHeaders($header);

$client->__soapCall("echoVoid", null);
?>

Example #2 Set Multiple Headers

<?php

$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));
$headers = array();

$headers[] = new SoapHeader('http://soapinterop.org/echoheader/',
                            'echoMeStringRequest',
                            'hello world');

$headers[] = new SoapHeader('http://soapinterop.org/echoheader/',
                            'echoMeStringRequest',
                            'hello world again');

$client->__setSoapHeaders($headers);

$client->__soapCall("echoVoid", null);
?>

User Contributed Notes

mlconnor at yahoo dot com 11-Nov-2010 10:28
Does anyone know how to get the response headers?  The getLastResponseHeader returns a string response, not the complex object I was expecting...
peamik1953 at NOSPAM dot btinternet dot com 12-Jul-2010 08:06
You cannot add an additional header. If you want two headers, and one already exists, first delete it with $client->__setSoapHeaders(NULL). Then issue $client->__setSoapHeaders($headers) where $headers is an array of soapHeader() objects.
kedar dot purohit @ mavs dot uta dot edu 10-Sep-2009 09:58
To create complex SOAP Headers, you can do something like this:

Required SOAP Header:

<soap:Header>
    <RequestorCredentials xmlns="http://namespace.example.com/">
      <Token>string</Token>
      <Version>string</Version>
      <MerchantID>string</MerchantID>
      <UserCredentials>
        <UserID>string</UserID>
        <Password>string</Password>
      </UserCredentials>
    </RequestorCredentials>
</soap:Header>

Corresponding PHP code:

<?php

$ns = 'http://namespace.example.com/'; //Namespace of the WS.

//Body of the Soap Header.
$headerbody = array('Token' => $someToken,
                    'Version' => $someVersion,
                    'MerchantID'=>$someMerchantId,
                      'UserCredentials'=>array('UserID'=>$UserID,
                                             'Password'=>$Pwd));

//Create Soap Header.
$header = new SOAPHeader($ns, 'RequestorCredentials', $headerbody);

//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);

?>
jayrajput at gmail dot com 30-Mar-2009 10:07
With multiple SOAP headers, when using SoapVar for creation of SoapHeader the PHP code just terminates (command terminated). I am not sure if that is a bug.

Without the SOAPVar the code worked fine for me

There are different way to creart SoapHeader I was using SoapVar and the code was not working. I am still a novice with this SOAP stuff.

Tried using normal strings and it worked fine. SoapHeader can take SoapVar or string as the third argument.

my code:

<?php
// first soap header.
$var = new SoapVar($header, XSD_ANYXML);
$soapHeader = new SoapHeader(NAME_SPACE, "Security", $var);
// second soap header.
$var2 = new SoapVar($header2, XSD_ANYXML);
$soapHeader2 = new SoapHeader(DIFF_NAME_SPACE, "ID", $var2);

$client = new SoapClient($wsdl, array("location" => $location));

$headers = array();
$headers[] = $soapHeader;
$headers[] = $soapHeader2;

// Here my code was just terminating.
$client->__setSoapHeaders($headers);
?>


Calls a SOAP function

  • ≪ SoapClient::__setSoapHeaders
  • SoapClient::SoapClient ≫
  • PHP Manual
  • SoapClient
  • Calls a SOAP function

SoapClient::__soapCall

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::__soapCall — Calls a SOAP function

설명

public mixed SoapClient::__soapCall ( string $function_name , array $arguments [, array $options [, mixed $input_headers [, array &$output_headers ]]] )

This is a low level API function that is used to make a SOAP call. Usually, in WSDL mode, SOAP functions can be called as methods of the SoapClient object. This method is useful in non-WSDL mode when soapaction is unknown, uri differs from the default or when sending and/or receiving SOAP Headers.

On error, a call to a SOAP function can cause PHP to throw exceptions or return a SoapFault object if exceptions are disabled. To check if the function call failed to catch the SoapFault exceptions, check the result with is_soap_fault().

인수

function_name

The name of the SOAP function to call.

arguments

An array of the arguments to pass to the function. This can be either an ordered or an associative array. Note that most SOAP servers require parameter names to be provided, in which case this must be an associative array.

options

An associative array of options to pass to the client.

The location option is the URL of the remote Web service.

The uri option is the target namespace of the SOAP service.

The soapaction option is the action to call.

input_headers

An array of headers to be sent along with the SOAP request.

output_headers

If supplied, this array will be filled with the headers from the SOAP response.

반환값

SOAP functions may return one, or multiple values. If only one value is returned by the SOAP function, the return value of __soapCall will be a simple value (e.g. an integer, a string, etc). If multiple values are returned, __soapCall will return an associative array of named output parameters.

On error, if the SoapClient object was constructed with the exceptions option set to FALSE, a SoapFault object will be returned.

예제

Example #1 SoapClient::__soapCall() example

<?php

$client = new SoapClient("some.wsdl");
$client->SomeFunction($a, $b, $c);

$client->__soapCall("SomeFunction", array($a, $b, $c));
$client->__soapCall("SomeFunction", array($a, $b, $c), NULL,
                    new SoapHeader(), $output_headers);

$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));
$client->SomeFunction($a, $b, $c);
$client->__soapCall("SomeFunction", array($a, $b, $c));
$client->__soapCall("SomeFunction", array($a, $b, $c),
                    array('soapaction' => 'some_action',
                          'uri'        => 'some_uri'));
?>

참고

  • SoapClient::SoapClient() - SoapClient constructor
  • SoapParam::SoapParam() - SoapParam constructor
  • SoapVar::SoapVar() - SoapVar constructor
  • SoapHeader::SoapHeader() - SoapHeader constructor
  • SoapFault::SoapFault() - SoapFault constructor
  • is_soap_fault() - Checks if a SOAP call has failed

User Contributed Notes

arturklesun at gmail dot com 11-Aug-2016 03:16
Sharing my experience cuz i believe it is most important for you if you decide to use this Soap Client implementation.

At php 7.0.8 the stdClass generated by SoapClient from the response does not use "minOccurs" and "maxOccurs" WSDL modifiers to distinct properties in stdClass-es (aka keys in "associative arrays") and elements in arrays ("aka indexed arrays").

Instead, the implementation decides whether tag is _a key in associative array_ or _one of elements with same tag in indexed array_ by simply the fact whether there is just one element with such tag in the sequence or many.

Consider alive example from my case:
<?php
response xml:
...
<ValidatingCarrier>
    <Alternate>AA</Alternate>
</ValidatingCarrier>
...
<ValidatingCarrier>
    <Alternate>AA</Alternate>
    <Alternate>AY</Alternate>
</ValidatingCarrier>
...

response structure generated by SoapClient:
...
[ValidatingCarrier] => stdClass Object(
    [Alternate] => AA // here it is a string
)
...
[ValidatingCarrier] => stdClass Object (
    [Alternate] => Array ( // and here this is an array
        [0] => AA
        [1] => AY
    )
)
...

field XSD definition:
<xs:element name="Alternate" type="CarrierCode" minOccurs="0" maxOccurs="24">
?>

You see, the definition in XSD tells us this tag can be repeated up to 24 times, what means it would be an indexed array, but SoapClient does not take that into account and treats the first <Alternate> in example as a value instead of array containing this value.

Undoubtedly, a value should be a property in the stdClass (a key in associative array) _only_ when maxOccurs is 1 or less or not specified (default is 1, see https://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints). Hope this info will be useful for you when you will be implementing your own, correctly working, SoapClient.
ub at sturmundbraem dot ch 11-Jun-2016 09:51
To avoid the SOAP client returning sometimes objects and sometimes arrays of objects, there's a setting:

   $this->soapClient = new \SoapClient($wsdlUrl, array(
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
    'trace' => DEBUG_SOAP
   ));
pmeth at softersoftware dot com 13-May-2014 01:44
Note, when trying to overload the __soapCall function, you should not use type hints or STRICT_STANDARDS will complain.
The correct signature is as follows:

<?php
class MySoapClient extends \SoapClient
{
    ...
    public function __soapCall($function_name, $arguments, $options = null, $input_headers = null, &$output_headers = null)
     {
        ...
    }
    ....
}
?>
vb 18-Oct-2012 01:23
Note that calling __soapCall and calling the generated method from WSDL requires specifying parameters in two different ways.

For example, if you have a web service with method login that takes username and password, you can call it the following way:
<?php
$params = array('username'=>'name', 'password'=>'secret');
$client->login($params);
?>

If you want to call __soapCall, you must wrap the arguments in another array as follows:
<?php
$client->__soapCall('login', array($params));
?>
snuufix+nospam at gmail dot com 11-Feb-2011 12:23
I am using SOAP call response headers to sign request results.

After alot of hours, I finally got the best way to get SOAP response headers (other than parsing __getLastResponse() witch requires trace option enabled) is using __soapCall() wrapper.

You can extend SoapClient class and wrap some functions to make sure you get the headers.

<?php

class API extends SoapClient
{

    // ... Constructor, etc.

    // Get SOAP Server response headers
    public function __soapCall($function, $arguments, $options = array(), $input_headers = null, &$output_headers = null)
    {
        parent::__soapCall($function, $arguments, $options, $input_headers, $output_headers);

        print_r($output_headers); // Array of response headers
    }

    // If you are using WSDL you need this, so you still can call functions directly without calling __soapCall manualy
    public function __call($func, $args)
    {
        return $this->__soapCall($func, $args);
    }

?>
niko dot hujanen at gmail dot com 10-Nov-2010 08:06
I was integrating a local warehouse system with a SOAP to our system and noticed some things.

1) Service uses "dot-namespacing" (fe. Item.Price) I classmapped this to Item_Price and used magical __get() and __set() to strip off 'Item.' from variable names. Works like a charm.

Example:
<Item.Price>1.52</Item.Price>

<?php
class Item_Price {
    public $price;
    public function __get($field) { $field = str_replace('Item.', '', $field); return $this->$field; }
    public function __set($field, $value) { $field = str_replace('Item.', '', $field); $this->$field = $value; }
?>

2) I noticed that classmapping a segment with attributes and value is possible. Class uses variables as attributes and $_ is the value.

Example:
<?php
class Foo {
    public $item = 'Bar';
    public $_ = 'Value here';
}
?>
Turns to <Foo item="Bar">Value here</Foo>

Example of using classes with soapcalls:
<?php
    $client = ExtendSoapClient::getInstance();
    $foo = new Foo();
    $response = $client->sendFoo($foo);
?>

Posted these since I think the documentation is lacking and very obsolete. Hopefully someone's burden eases.
james dot ellis at gmail dot com 10-Jun-2010 04:12
If you are using this method, remember that the array of arguments need to be passed in with the ordering being the same order that the SOAP endpoint expects.

e.g
<?php
//server expects: Foo(string name, int age)

//won't work
$args = array(32, 'john');
$out = $client->__soapCall('Foo', $args);

//will work
$args = array('john', 32);
$out = $client->__soapCall('Foo', $args);
?>
lesley at casbah dot com dot au 22-Apr-2009 03:55
If you are calling a weblogic service with a certificate and you are getting this error on the soap method call statement:

[EJB:010160]Security Violation: User: '<anonymous>' has insufficient permission to access EJB: type=<ejb>, application=app2_0_52_3, module=appsejb.jar, ejb=AppMethod, method=AppMethod, methodInterface=Remote, signature={java.lang.String}.

then check the wsdl in the soap open call - it must be https.  Supplied with an http wsdl this took some time to figure out.

<?php
    $client = new SoapClient("https://example.com/AppWs/Service?WSDL",
                array('trace'=>true,
                      'exceptions'=>true,
                      'local_cert' => "/var/www/html/app/newcert.pem",
                      'passphrase' => "thepassphrase"));
    $response = $client->__soapCall("MyMethod", array("param1" => $value1));
?>

Thanks to the authors for their comments on enabling openssl and curl by uncommenting extension=php_opensll.dll and extension=php_curl.dll in php.ini.  Also for Olaf's instructions "append the content of the private key file and the certificate file to a single file". I did this using copy command on the DOS prompt:

copy mycert.pem+mykey.pem newcert.pem
Tim Williams 08-Apr-2009 03:55
One little gotcha when passing the parameters where you need to have attributes and a simpletype value:

To get the xml

<foo bar="moo">cheese</foo>

You'd pass in:

<?php
array("foo" => array("_" => "cheese", "bar"=>"moo"));
?>

See that "_" bit? It really wasn't obvious from the documentation.
deWarlock 02-Apr-2009 01:10
The most annoying thing is that you'll get no warning trying to pass object not matching wsdl e.g. if server expects smth like $Object->expName->... and you would pass $Object->otherName the client will send empty request without notifying you.
Also pay attention that names are key sensitive.

In my case I spent hours trying to pass $Post->Lead->... object instead of $Post->lead-> ...
Shto 03-Mar-2009 12:17
One thing to note.

This happened to me and it took a while until I discovered what the problem was.

I was trying to get .NET objects from a provided web service, however it always seemed to return empty objects. It did return the backbone, but nothing within the objects that made up the structure.

Anyhow, it seems that you have to be very precise with the arrays when calling these functions. Par example, do this:

<?php
$obj = $client->__soapCall($SOAPCall, array('parameters'=>$SoapCallParameters));
?>

meaning that you must put an array as the second argument with 'parameters' as the key and the soap call parameters as the value.

Also make sure that the parameter variable, in my case $SoapCallParameters is in the form of what is requested by the webservice.

So, don't just make an array of the form:
<?php

(
   [0] => 'Mary',
   [1] => 1983
)

?>

but if the webservice requests a 'muid' variable as 'Mary' and a 'birthyear' as 1983, then make your array like this:

<?php

(
   [muid] => 'Mary',
   [birthyear] => 1983
)

?>

The above arrays refer to the $SoapCallParameters variable.

Hope this helps somebody, not having to spend too much time figuring out the problems.
Alejandro Cavallo 19-Mar-2008 04:38
I get the same problem reported above by ryan:
"ryan at grunt dot tv
22-Sep-2005 01:38
If you want to pass an xml document node as a function parameter, your need to create a SoapVar object with a text represention of the xml node and the XSD_ANYXML encoding constant. However, this constant is not exported by the extension and is not documented for some unknown reason."

When defined variable soapVar I assigned the type XSD_STRING rather than as XSD_ANYXML.
And when calls to the function you must pass the soapVar in an associative array.

For example if the function (called myFunc) expect a parameter called xmlInput you should do something like this:
$soapvar = new SoapVar($query, XSD_STRING);
$result = $client->myFunc(array('xmlInput'=>$soapvar));

This XML fragment are wrapped around xmlImput tags.-
stefan at datax dot biz 24-Oct-2007 04:28
The call to __soapCall returned an object to me either. This is the function which makes my life easier:

function obj2array($obj) {
  $out = array();
  foreach ($obj as $key => $val) {
    switch(true) {
        case is_object($val):
         $out[$key] = obj2array($val);
         break;
      case is_array($val):
         $out[$key] = obj2array($val);
         break;
      default:
        $out[$key] = $val;
    }
  }
  return $out;
}

Usage:
...
$response = $client ->__soapCall("track", array('parameters' => $request));
$response = obj2array($response);

Hope it helps.
temp at mailinator dot com 30-Jan-2007 01:43
The options in the third argument is documented in http://php.net/manual/en/function.soap-soapclient-construct.php
paulsjv at gmail dot com 11-Jan-2006 04:44
I was working with SOAP for the first time and I had to create a client that sent a date range to a WSDL (Web Services Description Language) to return some information I needed.  I didn't know how to pass the params and there really was no documentation about it.  The main thing you have to make sure to do is when you pass params to a method that is definied by the WSDL that you are calling is that you use the same param name for the key of the array or the object variable as shown below.  Also, if you don't know what the methods/functions that a WSDL has or the params that you need to pass it you can use the __getFunctions() and __getTypes() methods after you declare your new SoapClient.

<?php
// From and to are the two params that the execute function needs
// when called from the WSDL so make you to have them as the
// key to an array like below
$params["From"] = "06/01/2005"; // also can use $params->From = "date";
$params["to"] = "12/31/2005"; // also can use $params->to = "date";

$client = new SoapClient("some.wsdl");

try {
        print($client->execute($params));
} catch (SoapFault $exception) {
        echo $exception;
}
?>
ryan at grunt dot tv 22-Sep-2005 06:38
If you want to pass an xml document node as a function parameter, your need to create a SoapVar object with a text represention of the xml node and the XSD_ANYXML encoding constant. However, this constant is not exported by the extension and is not documented for some unknown reason.

Therefore, to get this to work you must either register the XSD_ANYXML #define as a PHP constant, or use the integer value of the constant when creating the SoapVar, which is 147.

$soapvar = new SoapVar($xml_text, 147);

$params = array("ItemXml" => $soapvar, "PropertyView" => "blah");
$result = $this->soapclient->__soapCall("SaveItem", array("parameters"=>$params), null, $this->soapheaders);

However, this still doesnt give the correct result. For some reason, the ItemXml parameter node is not wrapped around the associated xml parameter in the soap request, and the following soap is produced (assumming '<item>blah</item>' is used as the $xml_text):

<SOAP-ENV:Envelope xmlns:SOAP-ENV="..." xmlns:ns1="...">
<SOAP-ENV:Header>...</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:SaveItem>
<item>blah</item>
<ns1:PropertyView>blah</ns1:PropertyView>
</ns1:SaveItem>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
DesmondJ 05-May-2005 12:36
Following OrionI's example:

<?php
 $client = new SoapClient("http://server/sumservice.asmx?WSDL");
 $params->a = 2;
 $params->b = 3;
 $objectresult = $client->Sum($params);
 $simpleresult = $objectresult->SumResult;
 print($simpleresult); //produces "-1"
?>

Please note that the lines:

"$client->Sum($params);"

and

"$simpleresult = $objectresult->SumResult;"

are based off of each other. If your web service function is called "Sum", then add "Result" to the end of it to get the results of the call.

EG:

<?php
 $client = new SoapClient("http://server/mathservice.asmx?WSDL");
 $params->a = 2;
 $params->b = 3;
 $objectresult = $client->Minus($params); // note the name of the function is "Minus"
 $simpleresult = $objectresult->MinusResult; // note the name of the result is referenced as "MinusResult"
 print($simpleresult); //produces "5"
?>
OrionI 18-Apr-2005 04:27
Correction on the previously submitted code snippet...the incoming parameter for .NET also has to be in object or array form for it to be correctly converted to the XML form that .NET expects (as already mentioned by Llu?s P?mies). The full example (when using WSDL) should be like this:
<?php
 $client = new SoapClient("http://server/myservice.asmx?WSDL");
 $params->param1 = $value1;
 $params->param2 = $value2;
 $objectresult = $client->MyMethod($params);
 $simpleresult = $objectresult->MyMethodResult;
?>
So if you have a C# function like this:
//sumservice.asmx
...
[WebMethod]
public int Sum(int a, int b)
{
  return a + b;
}
...
The PHP client would be this:
<?php
 $client = new SoapClient("http://server/sumservice.asmx?WSDL");
 $params->a = 2;
 $params->b = 3;
 $objectresult = $client->Sum($params);
 $simpleresult = $objectresult->SumResult;
 print($simpleresult); //produces "5"
?>
OrionI 15-Apr-2005 08:46
When calling over SOAP to a .NET application, you may end up with an object as a result instead of a simple type, even if you're just grabbing a simple type (like a boolean result). Use a property accessor to get the actual result, like this:
<?php
 $client = new SoapClient("http://server/myservice.asmx?WSDL");
 $objectresult = $client->MyMethod($param1, $param2);
 $simpleresult = $objectresult->MyMethodResult;
?>
Note that .NET seems to name the result MethodNameResult for method MethodName.
18-Feb-2005 12:19
If your service is a .NET doc/lit, which means the input message has a single part named 'parameters' that is a structure that wraps the parameters.  Your call should look like this:

<?php

$params = array('param_name_1'=>$val_1,'param_name_2'=>$val_2);
$client->call('MethodName', array('parameters' => $params));

?>


SoapClient constructor

  • ≪ SoapClient::__soapCall
  • SoapServer ≫
  • PHP Manual
  • SoapClient
  • SoapClient constructor

SoapClient::SoapClient

(PHP 5 >= 5.0.1, PHP 7)

SoapClient::SoapClient — SoapClient constructor

설명

public SoapClient::SoapClient ( mixed $wsdl [, array $options ] )

This constructor creates SoapClient objects in WSDL or non-WSDL mode.

인수

wsdl

URI of the WSDL file or NULL if working in non-WSDL mode.

Note:

During development, WSDL caching may be disabled by the use of the soap.wsdl_cache_ttl php.ini setting otherwise changes made to the WSDL file will have no effect until soap.wsdl_cache_ttl is expired.

options

An array of options. If working in WSDL mode, this parameter is optional. If working in non-WSDL mode, the location and uri options must be set, where location is the URL of the SOAP server to send the request to, and uri is the target namespace of the SOAP service.

The style and use options only work in non-WSDL mode. In WSDL mode, they come from the WSDL file.

The soap_version option should be one of either SOAP_1_1 or SOAP_1_2 to select SOAP 1.1 or 1.2, respectively. If omitted, 1.1 is used.

For HTTP authentication, the login and password options can be used to supply credentials. For making an HTTP connection through a proxy server, the options proxy_host, proxy_port, proxy_login and proxy_password are also available. For HTTPS client certificate authentication use local_cert and passphrase options. An authentication may be supplied in the authentication option. The authentication method may be either SOAP_AUTHENTICATION_BASIC (default) or SOAP_AUTHENTICATION_DIGEST.

The compression option allows to use compression of HTTP SOAP requests and responses.

The encoding option defines internal character encoding. This option does not change the encoding of SOAP requests (it is always utf-8), but converts strings into it.

The trace option enables tracing of request so faults can be backtraced. This defaults to FALSE

The classmap option can be used to map some WSDL types to PHP classes. This option must be an array with WSDL types as keys and names of PHP classes as values.

Setting the boolean trace option enables use of the
methods
SoapClient->__getLastRequest,

SoapClient->__getLastRequestHeaders,

SoapClient->__getLastResponse and

SoapClient->__getLastResponseHeaders.

The exceptions option is a boolean value defining whether soap errors throw exceptions of type SoapFault.

The connection_timeout option defines a timeout in seconds for the connection to the SOAP service. This option does not define a timeout for services with slow responses. To limit the time to wait for calls to finish the default_socket_timeout setting is available.

The typemap option is an array of type mappings. Type mapping is an array with keys type_name, type_ns (namespace URI), from_xml (callback accepting one string parameter) and to_xml (callback accepting one object parameter).

The cache_wsdl option is one of WSDL_CACHE_NONE, WSDL_CACHE_DISK, WSDL_CACHE_MEMORY or WSDL_CACHE_BOTH.

The user_agent option specifies string to use in User-Agent header.

The stream_context option is a resource for context.

The features option is a bitmask of SOAP_SINGLE_ELEMENT_ARRAYS, SOAP_USE_XSI_ARRAY_TYPE, SOAP_WAIT_ONE_WAY_CALLS.

The keep_alive option is a boolean value defining whether to send the Connection: Keep-Alive header or Connection: close.

The ssl_method option is one of SOAP_SSL_METHOD_TLS, SOAP_SSL_METHOD_SSLv2, SOAP_SSL_METHOD_SSLv3 or SOAP_SSL_METHOD_SSLv23.

오류/예외

SoapClient::SoapClient() will generate an E_ERROR error if the location and uri options aren't provided in non-WSDL mode.

A SoapFault exception will be thrown if the wsdl URI cannot be loaded.

변경점

버전 | 설명
5.5.0 | New ssl_method option.
5.4.0 | New keep_alive option.

예제

Example #1 SoapClient::SoapClient() example

<?php

$client = new SoapClient("some.wsdl");

$client = new SoapClient("some.wsdl", array('soap_version'   => SOAP_1_2));

$client = new SoapClient("some.wsdl", array('login'          => "some_name",
                                            'password'       => "some_password"));

$client = new SoapClient("some.wsdl", array('proxy_host'     => "localhost",
                                            'proxy_port'     => 8080));

$client = new SoapClient("some.wsdl", array('proxy_host'     => "localhost",
                                            'proxy_port'     => 8080,
                                            'proxy_login'    => "some_name",
                                            'proxy_password' => "some_password"));

$client = new SoapClient("some.wsdl", array('local_cert'     => "cert_key.pem"));

$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));

$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/",
                                     'style'    => SOAP_DOCUMENT,
                                     'use'      => SOAP_LITERAL));

$client = new SoapClient("some.wsdl",
  array('compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP));

$client = new SoapClient("some.wsdl", array('encoding'=>'ISO-8859-1'));

class MyBook {
    public $title;
    public $author;
}

$client = new SoapClient("books.wsdl", array('classmap' => array('book' => "MyBook")));

?>

User Contributed Notes

cdesully at webslicer dot com 31-Mar-2016 03:39
I found a good use for typemap.  When using classmap any xml tags that had attributes, attribute values were not captured, so a typemap allows you to override how a class gets implemented and being able to get any attributes not previously captured.

<ItemList>
    <Item productId="1234">
        <feature>red</feature>
    </Item>
    <Item productId="5678">
        <feature>blue</feature>
    </Item>
</ItemList>

<?php //semi-pseudo code
namespace ExampleNS;

class ItemList {
    public $item = [];
}
class Item {
    public $productId;
    public $feature;
}

class Service {
    public function __construct(){
        $options = [
            'classmap' => [
                'ItemList' => 'ExampleNS\ItemList',
                'Item' => 'ExampleNS\Item'
            ],
            'typemap'=>[
                'type_ns' => 'http://some.wsdl.ns/sdo',
                'type_name' => 'Item',
                'from_xml' => [__CLASS__,'newConversion']
            ]
        ];
    }

    public function newConversion($xml){
        return simplexml_load_string($xml);
        //or more appropriately dig into the simplexml object and
        //instantiate the correct classes intended to be defined and
       //return those
    }
}
?>
Anonymous 11-Dec-2015 05:24
After installing PHP 7 on my test server I was receiving a 500 - Internal Server error when instantiating a new SoapClient. If there's an existing wsdl cache file, it needs to be deleted.
omidkosari at yahoo dot com 26-Jun-2015 01:05
To solve the problem with a lot of FIN_WAIT2 or CLOSE_WAIT , you should use the option keep_alive=false
info at nospam x valiton x com 20-Apr-2015 10:00
CAUTION:
I had quite a bit of trouble trying to make a request with fopen through a proxy to a secure url.  I kept getting a 400 Bad Request back from the remote host.  It was receiving the proxy url as the SNI host.  In order to get around this I had to explicity set the SNI host to the domain I was trying to reach.  It's apparently the issue outlined in this bug:

https://bugs.php.net/bug.php?id=63519

<?php
$domain = parse_url($file, PHP_URL_HOST);
$proxy_string = "tcp://" . WP_PROXY_HOST  . ":" . WP_PROXY_PORT;
$opts = array(
    'http' => array( 'proxy' => $proxy_string ),
    'ssl' => array( 'SNI_enabled' => true, 'SNI_server_name' => $domain));
$context = stream_context_create($opts);
$handle = fopen( $file, 'r', false, $context );
?>

src:
http://php.net/manual/en/context.http.php#114314
champetier dot etienne at gmail dot com 23-Mar-2015 11:30
When using classmap, never put a fully qualified classname (starting with \)
there is a bug (https://bugs.php.net/bug.php?id=69280)
softontherocks at gmail dot com 02-Dec-2014 04:38
I posted in this URL http://softontherocks.blogspot.com/2014/02/web-service-soap-con-php.html a full example of a nusoap web service.

There is defined the server and the client who calls the web service.

I hope it would be useful for you.
joshua at wiredrive dot com 23-Oct-2014 08:42
As of 5.5, ssl_method defaults to SOAP_SSL_METHOD_SSLv23. Just wanted to add this because it was not explicitly specified above.
Peter 08-May-2014 11:18
if you need to use ws-security with a nonce and a timestamp, you can use this :

<?php

class WsseAuthHeader extends SoapHeader {

private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
private $wsu_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';

function __construct($user, $pass) {

    $created = gmdate('Y-m-d\TH:i:s\Z');
    $nonce = mt_rand();
    $passdigest = base64_encode( pack('H*', sha1( pack('H*', $nonce) . pack('a*',$created).  pack('a*',$pass))));

    $auth = new stdClass();
    $auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
    $auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
    $auth->Nonce = new SoapVar($passdigest, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
    $auth->Created = new SoapVar($created, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wsu_ns);

    $username_token = new stdClass();
    $username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);

    $security_sv = new SoapVar(
        new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns),
        SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);
    parent::__construct($this->wss_ns, 'Security', $security_sv, true);
}
}

?>

and with your SoapClient do :

<?php
$client = new SoapClient("http://host/path");
$client->__setSoapHeaders(Array(new WsseAuthHeader("user", "pass")));
?>

works for me. based on a stackoverlfow post which only did the username and password, not the nonce and the timestamp
michael dot arnauts at gmail dot com 30-Jan-2014 12:53
This doesn't seem to be documented, but when you want to use compression for your outgoing requests, you have to OR with the compression level:

<?php
 $client = new SoapClient("some.wsdl",
  array('compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 9));
?>

This can be really usefull if you want to send large amounts of data.

Source: https://bugs.php.net/bug.php?id=36283
tolean dot dj at gmail dot com 13-Aug-2013 11:21
Here's a function for easier debugging:

<?php
function soapDebug($client){

    $requestHeaders = $client->__getLastRequestHeaders();
    $request = prettyXml($client->__getLastRequest());
    $responseHeaders = $client->__getLastResponseHeaders();
    $response = prettyXml($client->__getLastResponse());

    echo '<code>' . nl2br(htmlspecialchars($requestHeaders, true)) . '</code>';
    echo highlight_string($request, true) . "<br/>\n";

    echo '<code>' . nl2br(htmlspecialchars($responseHeaders, true)) . '</code>' . "<br/>\n";
    echo highlight_string($response, true) . "<br/>\n";
}

$client = new SoapClient(null, array(
    'location' => 'http://sita.local/_mpay/server.php',
    'uri' => 'http://sita.local/_mpay/',
    'trace' => 1,
));

$result = $client->__soapCall('getDate', array('format' => 'Y-m-d H:i:s'));
soapDebug($client);
?>
miguel dot angel dot fuentes dot casanova at gmail dot com 28-Jun-2013 04:39
Somebody using SoapClient has sometimes a lot of CLOSE_WAIT states?

It's in a local connection in a openSuse 11 (x86_64, version 11, patchlevel 2) with Apache 2.2.22 and PHP 5.3.15.
pgl at yoyo dot org 09-Apr-2013 09:21
Note that if libxml_disable_entity_loader() has been called, you cannot specify a WSDL file - you'll get an error that PHP "failed to load external entity".

To resolve this, add

<?php
libxml_disable_entity_loader(false);
?>

Above any calls to instantiate SoapClient.
francesco [at] paladinux [dot] net 16-Mar-2013 05:43
Where you have the problem of
"looks like we got no XML document SoapClient", really the real problem is the xml code from remote server is not clear (after last tag is possible that you have space or other caracter that the your supplier of service have lost :  no one is perfect!)
I've read some solution, like here, but you must to know that is possible, that is possible that the remote server don't wrong, and the "surplus" (latin word for added values) is needed.
In particolar with axis Java server, is possible that the configuration of server is needed and the "surplus" is one or more attachments...
The solution is 3:
1) contact your supplier if is possible to remove caracter if is an error (good luck! ;) )
2) use another class like nusoap resolve the problem (but has other problem!)
3) this technical solution:
when call the wsdl be sure that the option trace and exceptions are setted (the first true and the second false)

<?php
$SoapClient = new SoapClient(NULL,
          array(
....
            'trace'          => 1,
        'exceptions'      => 0
          )
?>
(if you don't do that you can't use the __getLastResponse)

Now You can do the request and use __getLastResponse() to obtain all the part..
<?php
$result  = $SoapClient -> problematicCall($par);
?>

After all  you obtain a variable like that:

-----=_Part_9325_126764118.1363377774664
Content-Type: text/xml; CHARSET=ks_c_5601-1987
Content-Transfer-Encoding: binary
Content-Id: <1C06F7FBA057796C5A6185605F9F93E4>

<?xml version="1.0" encoding="utf-8"?><...etc >&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;MESSAGGIO&gt;
  &lt;DA&gt;MI&lt;/DA&gt;
  &lt;A&gt;PALADINUX&lt;/A&gt;
  &lt;ID&gt;12345&lt;/ID&gt;
  &lt;TIPODOC&gt;RC&lt;/TIPODOC&gt;
  &lt;RISPOSTA&gt;
&lt;/MESSAGGIO&gt;
</rxproblematicCallReturn></ns1:rxCatalogoMaterialiResponse></soapenv:Body></soapenv:Envelope>
------=_Part_9325_126764118.1234567890123
Content-Type: application/zip
Content-Transfer-Encoding: binary
Content-Id: <43E91F7E7PALADINUXE07BF041A7BAC9>
-------------------------

Is easy understand if the added part is needed or not!
Now you can explode all the part and parse!

Hope this can help you.See ya in the cyberspace!
ale dot comp_06 at xox dot ch 12-Jun-2012 04:56
If your service is protected through basic http authentication and you're not using wsdl, you will have to put the login in the SoapClient's option:

<?php
$client = new SoapClient(
    null,
    array(
        'soap_version' => SOAP_1_2,
        'location' => ...
        ...,
        'login' => 'username',
        'password' => '*******',
    )
);
?>
chris at phblock dot atNoSpam 04-Nov-2011 02:57
this is the syntax of typemap (found at http://svn.php.net/viewvc/php/php-src/trunk/ext/soap/tests/typemap012.phpt?view=markup)
<?php
$options=Array(
'typemap' => array(
array("type_ns"  => "http://schemas.nothing.com",
         "type_name" => "soap",
         "to_xml"  => "some_funktion_name")
));
?>
Chris Gunawardena 15-Aug-2011 09:56
To monitor SOAP calls in and out of a unix server:

sudo tcpdump -nn -vv -A -s 0 -i eth0 dst or src host xxx.xxx.xxx.xxx and port 80

And always use 'cache_wsdl' => WSDL_CACHE_NONE
svenr at selfhtml dot org 27-Jul-2011 12:51
The "classmap" option actually is a mapping from the "ComplexType" used within the SOAP to your PHP Classes.

Do not confuse the XML tag names returned for your request with these ComplexTypes. SOAP allows them to be different.

I had something like this:
...
<soapenv:body>
<ns1:TagName>
  <ns1:Content>FooBar</ns1:Content>
</ns1:TagName>
</soapenv:body>

TagName is not the key you want to put in your classmap, you have to know the name of the ComplexType this TagName refers to. This info is contained inside the WSDL resource.
willem dot stuursma at hyves dot nl 30-May-2011 11:00
If you want to use classes in a different namespace in your classmap, just use the backslash in the the target class name.

Example:
<?php
$classmap = array('result' => 'MyNamespace\\Result');
?>

You need to specify the backslash twice because it is the escape character in strings.
simonlang at gmx dot ch 06-Apr-2011 03:35
Example for a soap client with HTTP authentication over a proxy:

<?php
new SoapClient(
    'service.wsdl',
    array(
        // Stuff for development.
        'trace' => 1,
        'exceptions' => true,
        'cache_wsdl' => WSDL_CACHE_NONE,
        'features' => SOAP_SINGLE_ELEMENT_ARRAYS,

        // Auth credentials for the SOAP request.
        'login' => 'username',
        'password' => 'password',

        // Proxy url.
        'proxy_host' => 'example.com', // Do not add the schema here (http or https). It won't work.
        'proxy_port' => 44300,

        // Auth credentials for the proxy.
        'proxy_login' => NULL,
        'proxy_password' => NULL,
    )
);
?>

Providing an URL to a WSDL file on the remote server (which as well is protected with HTTP authentication) didn't work. I downloaded the WSDL and stored it on the local server.
Asaf Meller 13-Jan-2011 02:57
a full working php .net soap configuration :
notes
1. web.config on .net server must work with basichttp binding.
2. paramaters to soap functions must be passed as :
array ('parm1_name'=>'parm1_value',
         'parm2_name'=>'parm2_value'...)

<?php
header('Content-Type: text/plain');

try {
            $options = array(
                'soap_version'=>SOAP_1_1,
                'exceptions'=>true,
                'trace'=>1,
                'cache_wsdl'=>WSDL_CACHE_NONE
            );
            $client = new SoapClient('http://www.example.com/end_point.wsdl', $options);
    // Note where 'Get' and 'request' tags are in the XML

        } catch (Exception $e) {
            echo "<h2>Exception Error!</h2>";
            echo $e->getMessage();
        }

echo 'running HelloWorld :';

 try {
   $response=$client->HelloWorld();

}
catch (Exception $e)
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

print_r($response);
?>

good luck !
Asaf.
faebu at faebu dot ch 21-Dec-2010 03:58
I'm experiencing the same problems when trying to load a WDSL fiel which is protected by basic http authentication, since the parameters login and password are just used for the request but not when reading the wdsl file. I just use the following workaround by downloading the xml file to a non-protected location on my server. Please notice that this doesn't support any kind of caching.

Hope it helps

<?php
class SoapAuthClient extends SoapClient {
    /**
     * Since the PHP SOAP package does not support basic authentication
     * this class downloads the WDSL file using the cURL package and
     * creates a local copy of the wdsl on your server.
     * Make sure you provide the following additional parameter in the
     * $options Array:
     * wdsl_local_copy => true
     */

    private $cache_dir = '/home/example/htdocs/cache/';
    private $cache_url = 'http://www.example.com/cache/';

    function SoapAuthClient($wdsl, $options) {
        if (isset($options['wdsl_local_copy']) &&
            $options['wdsl_local_copy'] == true &&
            isset($options['login']) &&
            isset($options['password'])) {

            $file = md5(uniqid()).'.xml';

            if (($fp = fopen($this->cache_dir.$file, "w")) == false) {
                throw new Exception('Could not create local WDSL file ('.$this->cache_dir.$file.')');
            }

            $ch = curl_init();
            $credit = ($options['login'].':'.$options['password']);
            curl_setopt($ch, CURLOPT_URL, $wdsl);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, $credit);
            curl_setopt($ch, CURLOPT_TIMEOUT, 15);
            curl_setopt($ch, CURLOPT_FILE, $fp);
            if (($xml = curl_exec($ch)) === false) {
                //curl_close($ch);
                fclose($fp);
                unlink($this->cache_dir.$file);

                throw new Exception(curl_error($ch));
            }

            curl_close($ch);
            fclose($fp);
            $wdsl = $this->cache_url.$file;
        }

        unset($options['wdsl_local_copy']);
        unset($options['wdsl_force_local_copy']);

        echo $wdsl;
        parent::__construct($wdsl, $options);

        unlink($this->cache_dir.$file);
    }
}
?>
tatupheba at gmail dot com 20-Dec-2010 11:30
Hello folks!

A hint for developers:

When programming some soap server set the "soap.wsdl_cache_enabled" directive in php.ini file to 0:

soap.wsdl_cache_enabled=0

Otherwise it will give a bunch of strange errors saying that your wsdl is incorrect or is missing.

Doing that will save you from a lot of useless pain.
titan at phpdevshell dot org 06-Oct-2010 03:11
It should be noted that if you receive a return error : 'Object reference not set to an instance of an object.'. This could be due to something as simple as passing the incorrect parameters. When you look at this XML:

    <Get xmlns="http://example.com">
      <request>
        <CustomerId>string</CustomerId>
        <From>dateTime</From>
        <To>dateTime</To>
      </request>
    </Get>

Your code should look something like this:

<?php
        try {
            $options = array(
                'soap_version'=>SOAP_1_2,
                'exceptions'=>true,
                'trace'=>1,
                'cache_wsdl'=>WSDL_CACHE_NONE
            );
            $client = new SoapClient('http://example.com/doc.asmx?WSDL', $options);
// Note where 'Get' and 'request' tags are in the XML
            $results = $client->Get(array('request'=>array('CustomerId'=>'1234')));
        } catch (Exception $e) {
            echo "<h2>Exception Error!</h2>";
            echo $e->getMessage();
        }

$results = $client->Get(array('request'=>array('CustomerId'=>'842115')));
?>
marcovtwout at hotmail dot com 29-Jun-2010 05:30
If your WSDL file containts a parameter with a base64Binary type, you should not use base64_encode() when passing along your soap vars. When doing the request, the SOAP library automatically base64 encodes your data, so otherwise you'll be encoding it twice.

WSDL snipplet:
<element name="content" type="base64Binary" xmime:expectedContentTypes="*/*"/>

<?php
$string = 'data_you_want_to_send___like_xml_in_soap';
$soap_data = array(
        'foo' => 'bar',
      //'content' => base64_encode($string ) // don't do this
        'content' => $string //do this
);
$response = $client->Send($soap_data);
?>
marcovtwout at hotmail dot com 26-Jun-2010 03:20
Being new to SOAP, I was searching for a while to find out why my message was getting a response in soapUI, but not with my php code. The specific service I was addressing gives a HTTP 202 Accepted on success (no response), but returns a SOAP message on errors.

Situation:
Using an (authenticated) client connection, and a WDSL file, SOAP calls with type "One-Way" don't give a response header, even though a response is expected.

Solution:
When calling the client constructor, set SOAP_WAIT_ONE_WAY_CALLS in the $options['features'].
tim at tdinternet dot com 10-Jun-2010 05:05
PHP 5.2.11 seems to not be very picky about the correctness of a WSDL.

Other SOAP clients were complaining about schema and namespace issues, while PHP's SoapClient worked completely fine.  Fixing those issues for the other clients however broke PHP's SoapClient to the point where objects being passed to the SOAP method were becoming empty arrays on the server side.

Lesson learned:  some elements were being prefixed with xsd: and others were not -- make absolutely sure your WSDL is correct and consistent (I'm using a tweaked WSDL_Gen.php).
james dot ellis at gmail dot com 10-Jun-2010 04:06
I was having troubles getting responses from a Coldfusion SOAP server, with no obvious issues in the SoapClient used.

Eventually I found that the server was only accepting SOAP 1.1 requests and not 1.2. Not sure if this is  a system wide Coldfusion setting but if you hit the same wall, try setting the SoapClient option "soap_version" to the constant SOAP_1_1 (which is the default but mine was defaulting to 1.2 as the client was being reused for another service)
ajcartmell at fonant dot com 09-Jun-2010 12:57
There seems to be a problem with specifying empty strings for proxy_host and proxy_port options in recent versions of PHP (from a version later than 5.2.9, and equal to or earlier than 5.2.11).

Supplying empty string values for proxy_host and proxy_port causes "host not found" type errors: supplying NULL or FALSE works fine.
reinaldo at mandic dot com dot br 25-May-2010 04:43
You need both certificates, privateKey and publicKey on the same file, on local_cert path to work.

# cat publicKey.pem privateKey.pem > key.pem
bhargav dot khatana at gmail dot com 11-Apr-2010 08:10
It took me longer than a week to figure out how to implement WSSE (Web Service Security) headers in native PHP SOAP. There are no much resource available on this, so thought to add this here for community benefit.

Step1: Create two classes to create a structure for WSSE headers

<?php
class clsWSSEAuth {
          private $Username;
          private $Password;
        function __construct($username, $password) {
                 $this->Username=$username;
                 $this->Password=$password;
              }
}

class clsWSSEToken {
        private $UsernameToken;
        function __construct ($innerVal){
            $this->UsernameToken = $innerVal;
        }
}
?>

Step2: Create Soap Variables for UserName and Password

<?php
$username = 1111;
$password = 1111;

//Check with your provider which security name-space they are using.
$strWSSENS = "http://schemas.xmlsoap.org/ws/2002/07/secext";

$objSoapVarUser = new SoapVar($username, XSD_STRING, NULL, $strWSSENS, NULL, $strWSSENS);
$objSoapVarPass = new SoapVar($password, XSD_STRING, NULL, $strWSSENS, NULL, $strWSSENS);
?>

Step3: Create Object for Auth Class and pass in soap var

<?php
$objWSSEAuth = new clsWSSEAuth($objSoapVarUser, $objSoapVarPass);
?>

Step4: Create SoapVar out of object of Auth class

<?php
$objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
?>

Step5: Create object for Token Class

<?php
$objWSSEToken = new clsWSSEToken($objSoapVarWSSEAuth);
?>

Step6: Create SoapVar out of object of Token class

<?php
$objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
?>

Step7: Create SoapVar for 'Security' node

<?php
$objSoapVarHeaderVal=new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);
?>

Step8: Create header object out of security soapvar

<?php
$objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal,true, 'http://abce.com');

//Third parameter here makes 'mustUnderstand=1
//Forth parameter generates 'actor="http://abce.com"'
?>

Step9: Create object of Soap Client

<?php
$objClient = new SoapClient($WSDL, $arrOptions);
?>

Step10: Set headers for soapclient object

<?php
$objClient->__setSoapHeaders(array($objSoapVarWSSEHeader));
?>

Step 11: Final call to method

<?php
$objResponse = $objClient->__soapCall($strMethod, $requestPayloadString);
?>
meltir at meltir dot com 29-Mar-2010 02:59
To those fighting with NTLM authenticated proxy servers, here's a solution I'm using atm:

<?php
/**
 * A child of SoapClient with support for ntlm proxy authentication
 *
 * @author Meltir <meltir@meltir.com>
 *
 */
class NTLM_SoapClient extends SoapClient {

    public function __construct($wsdl, $options = array()) {
        if (empty($options['proxy_login']) || empty($options['proxy_password'])) throw new Exception('Login and password required for NTLM authentication!');
        $this->proxy_login = $options['proxy_login'];
        $this->proxy_password = $options['proxy_password'];
        $this->proxy_host = (empty($options['proxy_host']) ? 'localhost' : $options['proxy_host']);
        $this->proxy_port = (empty($options['proxy_port']) ? 8080 : $options['proxy_port']);
        parent::__construct($wsdl, $options);
    }

    /**
     * Call a url using curl with ntlm auth
     *
     * @param string $url
     * @param string $data
     * @return string
     * @throws SoapFault on curl connection error
     */
    protected function callCurl($url, $data) {
    $handle   = curl_init();
    curl_setopt($handle, CURLOPT_HEADER, false);
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_FAILONERROR, true);
    curl_setopt($handle, CURLOPT_HTTPHEADER, Array("PHP SOAP-NTLM Client") );
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    curl_setopt($handle, CURLOPT_PROXYUSERPWD,$this->proxy_login.':'.$this->proxy_password);
    curl_setopt($handle, CURLOPT_PROXY, $this->proxy_host.':'.$this->proxy_port);
    curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
    $response = curl_exec($handle);
    if (empty($response)) {
      throw new SoapFault('CURL error: '.curl_error($handle),curl_errno($handle));
    }
    curl_close($handle);
    return $response;
    }

    public function __doRequest($request,$location,$action,$version,$one_way = 0) {
        return $this->callCurl($location,$request);
    }

}
?>

Requires curl and could be extended, but it works for my simple needs.
eric dot caron at gmail dot com 31-Jan-2010 10:26
Though pointed out by jan at bestbytes, and discussed in bug #27777, one common source of the "Parsing WSDL: Couldn't find <definitions>" error is from trying to access a WSDL that is protected by HTTP authentication. Passing the login/password in the 2nd parameter doesn't always work; so if you encounter this error message and are trying to access a protected WSDL file, try passing the username and password in with the first parameter.
Anonymous 10-Jan-2010 03:53
I had to struggle with a rather strange behavior when trying to consume standard Parlay X web services with no success. However, I found a remedy to my problem.

The problem which I faced was about an erroneous invalid HTTP basic authentication request sent to the web service. Although I was sending the right credentials, I was getting an authentication error. It turns out that PHP was sending HTTP requests to another endpoint which is not exposed directly through the web service and that end point does not require authentication.

My remedy for that issue was by using this simple lines in the example of using sendSms Paraly-X method.

First, creating a soap client without any HTTP authentication options:

<?php
$client = new SoapClient($wsdl_url);
?>

The above request will cache the wsdl in the /tmp directory. Immediately after this construction we create another soap client, this time with HTTP authentication options:

<?php
try {
        $client = new SoapClient($wsdl_url, array('login' => "griffin",
                                            'password'       => "password"));
} catch (Exception $e) {
        printf("Error:sendSms: %s\n",$e->__toString());
        return false;
}
?>

Now it should work without any problem. Without the second call, PHP will call sendSms without credentials resulting in a failed attempt due to missing authentication information. I found this procedure to be the simplest.

This process should be done every time the cached wsdl expires, or simply a one can increase the time-to-live for the cached wsdl from php.ini
jon dot gilbert at net-entwicklung dot de 12-Dec-2009 06:55
Note that creating a soap client for an invalid URL (you do test what happens, when a service is not available, right?) usually throws an exception which can be caught with try..catch. However, if xdebug is active you will get a fatal error, which obviously cannot be caught.
sloloem at gmail dot com 19-Oct-2009 10:14
I had an issue figuring out the use of classmap that took me quite a while to figure out.  I was assuming the WSDL type the docs were referring to was the name of the element being returned in the SOAP, so like,
<ns1:node id="8675309" />

and I was wondering why mapping
'classmap'=>array('node'=>'MyNode')

did nothing.
That's because in my WSDL I defined node as:

<xsd:element name="node" type="tns:nodeType" />

The classmap I needed was:
'classmap'=>array('nodeType'=>'MyNode')

I was able to find the type names using SoapClient->__getTypes()
Later, I realized where I could look inside the WSDL for the typename I needed.

I dunno if I missed something painfully obvious but maybe this can clear up some of the docs.
Jon 06-May-2009 12:13
We've had some problems using SoapClient connecting to an external server via Microsoft ISA (presently v.2006 but this may apply to other versions too). We supply the proxy_host, proxy_port, proxy_login and proxy_password but the ISA server reports the login in its logs as "anonymous".

Our sysadmin believes this is because PHP is not supplying NTLN information (Windows security protocol) in the correct format (and whether it should work with proprietary proxies is of course another debate). We'd tried "username", "DOMAIN\username" to no effect. The solution is to add an exception in the ISA server for the target hostname/IP; null can then be supplied for proxy_login and proxy_password and the connection should then work as expected.

On a slightly related note, if you are having problems make sure the port number is supplied as an integer. Some versions of PHP will not use the proxy with SoapClient if the port number is supplied as a string.
jan at bestbytes dot de 12-Feb-2009 04:32
You CAN get a wsdl, if basic authentication is required:

<?php

$login = 'bert';
$password = 'berts password';

$client = new SoapClient(
    'http://' . urlencode($login) . ':' . urlencode($password) . '@www.server.com/path/to/wsdl',
    array(
        'login' => $login,
        'password' => $password
    )
);

?>
info at nicksilvestro dot net 10-Nov-2008 07:40
For anyone having trouble with ArrayOf_xsd_string and getting an error similar to 'No deserializer defined for array type {http://www.w3.org/2001/XMLSchema}string'
Try using the 'features' param, set to SOAP_USE_XSI_ARRAY_TYPE - this makes sure the correct deserializer is used.

eg,
<?php
$client = new SoapClient("some.wsdl", array('features' => SOAP_USE_XSI_ARRAY_TYPE));
?>
sniper 28-Sep-2008 08:46
i was looking for a good example and couldnt find one,
finally found it somewhere(forgot where) i think this is
the best example to make a soap request with multiple params

$params->AWSAccessKeyId = AMAZON_API_KEY;
$params->Request->SearchIndex = 'Books';
$params->Request->Keywords = 'php5 oop';

$amazon = new SoapClient('http://webservices.amazon.com
/AWSECommerceService/AWSECommerceService.wsdl');
$result = $amazon->itemSearch($params);
taras dot dot dot di at gmail dot com 16-Sep-2008 06:23
This took me a while to figure out.

As described at the bottom of here: http://bugs.php.net/bug.php?id=38703

having xdebug may interfere with the constructor throwing exceptions
alex on reutone comma com 10-Sep-2008 03:53
To connect PHP SOAP to MS SOAP (CRM/EXCHANGE/...) I have created some classes using the explanation below and in other places.
www.reutone.com/heb/articles.php?instance_id=62&actions=show&id=521
naugtur at gmail dot com 19-Jun-2008 03:48
SoapFault exception: [Client] looks like we got no XML document in <document> has been already mentioned to occur when your server outputs something before <?xml ... > tag.

For all those having problems with that, and no access to the server code:
This is how to make a proxy that would clean responses for You

<?php
/**
 * Simple class taken from a note by James Ellis [in __doRequest() page of manual]
 */
class Proxy_Client extends SoapClient {
    protected $cacheDocument = "";
    public function __construct($wsdl, $options) {
        parent::__construct($wsdl, $options);
    }

    /**
     * SetCacheDocument() sets the previously cached document contents
     */
    public function SetCacheDocument($document) {
        $this->cacheDocument = $document;
    }

    /**
     * __doRequest() overrides the standard SoapClient to handle a local request
     */
    public function __doRequest() {
        return $this->cacheDocument;
    }
}

//put this code in your function or wherever You have all required variables set

$client = new SoapClient($wsdl_url,$settings_array);
$void=$client->$method($params);  //call this to get response from server

$response_string=$client->__getLastResponse();

//this part removes stuff
$start=strpos($response_string,'<?xml');
$end=strrpos($response_string,'>');
$response_string=substr($response_string,$start,$end-$start+1);

//get your proxy prepared
$proxy = new Proxy_Client($wsdl_url,$settings_array);
//and fill it with the server's response
$proxy->SetCacheDocument($response_string);

$and_finally_the_result_is=$proxy->$method($params);

print_r($and_finally_the_result_is); //this allows You to see what's there

?>

$method is the method's name eg. $method='getVersion';
$params - typical params for a soap method
david at globulebleu dot com 09-Jun-2008 09:44
When using classmap option to map the SOAP results to a class, the constructor
of the object you've mapped to is not called.

$client = new SoapClient("url_to_wsdl",
   array('classmap' => array('contact' => "Contact"));

$params = array("1");

$contact = $client->__soapCall("get_contact", $params);

Expected result:

A contact object that has properties initialized (i.e. db connections,
....).

Actual result:

A contact object without the properties.

Thanks for your help.

David Georges.
Anno v. Heimburg 27-May-2008 07:23
> When using HTTP basic authentication, PHP will only send
> the credentials when invoking the service, not when
> fetching the WSDL.

The same goes for using an SSL client certficate, the SoapClient will only present the certificate on the actual remote call, not when getting the WSDL. The workaround is the same as above. HttpRequest works as expected.
cw at netresearch dot de 30-Apr-2008 10:24
The "cache_wsdl" option takes constants like WSDL_CACHE_NONE or WSDL_CACHE_DISK that are listed on the "SOAP constants" page -> /manual/en/soap.constants.php
james dot ellis at gmail dot com 30-Jan-2008 02:10
A note regarding boolean values that may seem obvious on reflection but could be a gotcha for some:

Seeing a SOAP request example with <SomeBooleanParam>true</SomeBooleanParam> may lead you to pass in string "true" or "false" as the parameter, which is incorrect - the correct method is to use boolean data types.

<?php
$client = new SoapClient($wsdl,$options);
$method = "DoSomething";
$params = new stdClass;

$params->SomeBooleanParam = TRUE;
$client->$method($params);
/**
 simplified request snippet would be
 <SomeBooleanParam>true</SomeBooleanParam>
**/

//this will also be correct, but not for the right reasons:
$params->SomeBooleanParam = "true";
$client->$method($params);
/**
 simplified request snippet would be
 <SomeBooleanParam>true</SomeBooleanParam>
**/

//this is where you may be wondering what is going on
$params->SomeBooleanParam = "false";
$client->$method($params);
/**
 simplified request snippet would be
 <SomeBooleanParam>true</SomeBooleanParam>
**/

//you need to do this instead
$params->SomeBooleanParam = FALSE;
$client->$method($params);
/**
 simplified request snippet would be
 <SomeBooleanParam>false</SomeBooleanParam>
**/
?>

Hope that helps!
giosh94mhz 08-Jul-2007 08:46
Oops!

I have written the wrong exception message in my last post.

The correct exception is:

SoapFault exception: [Client] looks like we got no XML document in <document>
lsmith at php dot net 08-Feb-2007 04:20
As noted in the bug report http://bugs.php.net/bug.php?id=36226, it is considered a feature that sequences with a single element do not come out as arrays. To override this "feature" you can do the following:

$x = new SoapClient($wsdl, array('features' =>
SOAP_SINGLE_ELEMENT_ARRAYS));
matt_schiros at academicsuperstore dot com 30-Nov-2006 09:26
When using classmap to map the SOAP results to a class, the constructor of the object you've mapped to is _not_ called.  This applies to the PHP5 __construct() and the PHP4 ClassName() constructors.
Marius Mathiesen 01-Mar-2006 01:57
I kept having a problem using an HTTP proxy with SOAP. The proxy_port parameter has to be an integer, ie. "proxy_port"=>"80" won't work, you'll have to use "proxy_port"=>80.

HTH,
Marius
php at sowen dot de 22-Dec-2005 03:40
If you're using CLI and there are multiple IP addresses available for outgoing SOAP-requests, try this "secret" to set outgoing IP:

e.g. for local IP 10.1.4.71:

$opts = array('socket' => array('bindto' => '10.1.4.71:0'));
$context = stream_context_create($opts);
$client  = new SoapClient(null, array('location'=>'http://...','uri' => '...','stream_context' => $context));

You can also set other options for the stream context, please refer to this page:

Appendix M: http://www.php.net/manual/en/wrappers.php

Bye,
   Nils Sowen
jared at ws-db dot com 28-Aug-2005 11:31
If you connect to a SoapServer, that has been created with session persistence, you can access the server's session id via SoapClient->_cookies[<session_id_name>][0]. This property becomes available after your first client method call.

I have only tested this with session.use_cookies=1.
Arjan van Bentem 12-Aug-2005 08:31
When using HTTP basic authentication, PHP will only send the credentials when invoking the service, not when fetching the WSDL.

To solve this, one needs to fetch the WSDL manually (or using PHP, of course!) and store it on the file system. Obviously, the first parameter for SoapClient(..) then needs to refer to that local copy.

Alternatively, the user annotations at http://nl3.php.net/manual/en/ref.soap.php state that one could encode the login and password into the URL as well.

See http://bugs.php.net/bug.php?id=27777
Jim Plush 11-Jul-2005 09:56
As of version 5.0.4 you can now dynamically change your location even if you're using a wsdl

<?php

$client = new SoapClient("http://some.host.net/wsdl/somefile.wsdl",array(
    "location" => 'myurl.com'));

?>

notice you can now use the "location" key.
This means you can have the same wsdl file not define a location until runtime which is great if you have a development test site or if you distribute your files to other companies.
Prior to this change you would have to ship a custom wsdl file to every client you had with their location hardcoded.
Brian Grayless 04-Mar-2005 11:30
Using a WSDL file is the way to go, however, for my particular application, the LOCATION:PORT needed to be dynamic so that my SOAP clients would be able to call a different service based on the client domain.

If you are using a WSDL, SoapClient() requires a URL direct to an actual URL and does not let you use a PHP file that outputs the dynamic WSDL XML in its stead. So, I ended up making a separate WSDL for each possible service needed and had to maintain them all if the service description changed.

Finally, after some fiddling, I was able to create a PHP page with the proper Mime type headers so that I could then trick SoapClient() to think that it was being passed a file with a ".wsdl" extension.

Example:

<?php

// filename: wsdl.php

header('Content-Type: application/xml; CHARSET=ks_c_5601-1987');
header('Content-Disposition: attachment; filename="filename.wsdl"');

$wsdl = 'path/wsdl_name.wsdl';

// read in file
$handle = fopen($wsdl, "r");
$wsdl_xml = fread($handle, filesize($wsdl));
fclose($handle);

// put code here to replace url and port in xml

echo $wsdl_xml;

?>

Now, in order to make this work, you can't just call a relative path to the file. I believe it has to go through Apache to properly set the mime type headers, etc... So you would use a full "http://....." address as the path to the wsdl.php file.

<?php

//... somewhere in your soap client code

$wsdl_loc = "http://yourdomain.com/wsdl.php";

$soap_client = new SoapClient($wsdl_loc, $client_param_array);

?>

Another, perhaps not so clean, way of achieving this would be to modify your .htaccess file in the directory where your WSDL file exists to force ".wsdl" files to run through the PHP engine. Add the following to your .htaccess:

AddType application/x-httpd-php .php .wsdl

You can then put dynamic PHP code snippets in your *.wsdl files to change whatever values you need to.

There are pros and cons to each solutions. The Mime solution probably taxes the system more as it has to read the file in every time a soap request is made. The htaccess solution makes it so you have to depend on either a modified .htaccess or Apache conf file.

Perhaps if you set the "soap.wsdl_cache_enabled", using ini_set(), to 1 (default), the caching will make it so it doesn't read the file every time for the Mime solution.


The SoapServer class

(PHP 5 >= 5.0.1, PHP 7)

소개

The SoapServer class provides a server for the » SOAP 1.1 and » SOAP 1.2 protocols. It can be used with or without a WSDL service description.

클래스 개요

SoapServer {
/* 메소드 */
public void addFunction ( mixed $functions )
public void addSoapHeader ( SoapHeader $object )
public __construct ( mixed $wsdl [, array $options ] )
public void fault ( string $code , string $string [, string $actor [, string $details [, string $name ]]] )
public array getFunctions ( void )
public void handle ([ string $soap_request ] )
public void setClass ( string $class_name [, mixed $args [, mixed $... ]] )
public void setObject ( object $object )
public void setPersistence ( int $mode )
public SoapServer ( mixed $wsdl [, array $options ] )
}

Table of Contents

  • SoapServer::addFunction — Adds one or more functions to handle SOAP requests
  • SoapServer::addSoapHeader — Add a SOAP header to the response
  • SoapServer::__construct — SoapServer constructor
  • SoapServer::fault — Issue SoapServer fault indicating an error
  • SoapServer::getFunctions — Returns list of defined functions
  • SoapServer::handle — Handles a SOAP request
  • SoapServer::setClass — Sets the class which handles SOAP requests
  • SoapServer::setObject — Sets the object which will be used to handle SOAP requests
  • SoapServer::setPersistence — Sets SoapServer persistence mode
  • SoapServer::SoapServer — SoapServer constructor

User Contributed Notes

softontherocks at gmail dot com 30-Oct-2014 03:20
I posted in this URL http://softontherocks.blogspot.com/2014/02/web-service-soap-con-php.html a full example of a nusoap web service.

There is defined the server and the cliente who calls the web service.

I hope it would be useful for you.
s at dumdeedum dot com 13-Sep-2013 02:32
I was running PHP 5.3.2 and couldn't for the life of me get SOAP headers to work, no matter how carefully I built my class/wsdl/client.  What finally fixed it was updating to the latest PHP.  No idea if there was a bug somewhere or what, but it's never a bad idea to stay current and it might save you weeks of frustration!
phpman dot fulter47 at recursor dot net 05-Jul-2012 02:36
For those (like me) coming from other SOAP frameworks and getting confused:

- A PHP SOAP server application created using the SoapServer class can not be used as a standalone SOAP server.
It needs an HTTP server to receive SOAP requests, which must run a PHP engine to execute the PHP SOAP server script as a CGI.

- For this reason, there are no settings in PHP that control the Interface/Port under which the SOAP server will wait for requests - it is the HTTP server who is responsible for that.

- The "uri" parameter is an "id value", used as the namespace for the SOAP response. It is only required if no WSDL file is used (that contains this setting), i.e. "wsdl" is set to NULL. It does _not_ control the Hostname/Port under which the HTTP server will be reachable for requests.
php1221 at klox dot net 18-Aug-2011 10:39
While there are plenty of mentions online that SoapServer doesn't support SOAP Headers, this isn't true.

In your class, if you declare a function with the name of the header, the function will be called when that header is received.

<?php
class MySoapService {
  private $user_is_valid;

  function MyHeader($header) {
    if ((isset($header->Username)) && (isset($header->Password))) {
      if (ValidateUser($header->Username, $header->Password)) {
        $user_is_valid = true;
      }
    }
  }

  function MySoapRequest($request) {
    if ($user_is_valid) {
      // process request
    }
    else {
      throw new MyFault("MySoapRequest", "User not valid.");
    }
  }
}
?>
hawky83 at googlemail dot com 21-Jan-2011 11:46
Another simple example for SOAP_SERVER with errorhandling an params and wsdl:

SERVER (soap_all_srv.php):

<?php
// PEAR::SOAP einbinden
require_once "SOAP/Server.php";
$skiptrace =& PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
$skiptrace = true;

// Service-Class
class mytimeserv {

  // __dispatch_map
  public $__dispatch_map = array ();

  // In/Out param -> __dispatch_map
  public function __construct() {
    $this->__dispatch_map["now"] =
      array ("in" => array("format" => "string"),
             "out" => array("time" => "string"));
  }

  // get back __dispatch_map in __dispatch
  public function __dispatch($methodname) {

    if (isset($this->__dispatch_map[$methodname])) {
      return $this->__dispatch_map[$methodname];
    }

    return NULL;
  }

  // servicemthod with parameters
  function now ($format) {

    // formaterror?
    if (($format == null) || (trim($format) == "")) {

      // send errormessage
      return new SOAP_Fault("Kein Parameter angegeben","0815", "Client");
    }

    date_default_timezone_set('Europe/Berlin');

    $time = date ($format);

    // return SOAP-Obj.
    return (new SOAP_Value('time','string', $time));
  }
}

// service-class
$service = new mytimeserv();

// server
$ss = new SOAP_Server();

// add service with name
$ss->addObjectMap (&$service,"urn:mytimeserv");

// service or wsdl
if (isset($_SERVER["REQUEST_METHOD"])&& $_SERVER["REQUEST_METHOD"] == "POST") {

    // postdata -> service
    $ss->service ($HTTP_RAW_POST_DATA);

} else {

  // wsdl-param in url
  if (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'],'wsdl') == 0) {

    // DISCO_Server for WSDL
    require_once "SOAP/Disco.php";
    $disco = new SOAP_DISCO_Server ($ss,"mytimeserv","My Time Service");

    // set HTML-Header
    header("Content-type: text/xml");

    // return wsdl
    print $disco->getWSDL ();
  }
}

?>

CLIENT (soap_all_client.php) (for wsdl: http://example.com/soap_all_srv.php?wsdl):
<?php

require_once "SOAP/Client.php";

// SOAP/WSDL
$sw = new SOAP_WSDL ("http://example.com/soap_all_srv.php?wsdl");

// Proxy-Obj.
$proxy = $sw->getProxy ();

// servicemthod
$erg = $proxy->now ("H:i:s");

// return
print $erg."\n";

?>
mikkel at nsales dot dk 03-Jun-2010 12:15
If you want to return a custom object array from a nusoap webservice, you have to cast the objects to arrays like so:

<?php
$users = array();
while($res = $db_obj->fetch_row())
{
  $user = new user();
  $user->Id = $res['id'];
  $user->Username = $res['username'];
  $user->Email = $res['email'];

  $users[] = (array) $user;
}

return ($users);
?>
junk2cm at gmx dot de 25-May-2010 09:54
As the SoapServer class lacks the possibility of dealing with SOAP-Headers, my following workaround is suitable to my needs when I had to do authentication via the SOAP-Headers:
1. Use a class which handles SOAP requests and let the constructor of this class take the sent headers.
2. Before invoking the SoapServer extract the SOAP-Header from the incoming raw data.
3. When using setClass pass the extracted header to the handling class.

In Your Server-Skript do things like this
<?php
$hdr = file_get_contents("php://input");
if (strpos($hdr,'<s:Header>')===false) {
    $hdr = null;
} else {
    $hdr = explode('<s:Header>',$hdr);
    $hdr = explode('</s:Header>',$hdr[1]);
    $hdr = $hdr[0];
}
$srv = new SoapServer('Your_wsdl');
$srv->setClass("ServiceClass",$hdr);
$srv->handle();
?>

And the service class is like this:
<?php
class ServiceClass {
    var $IsAuthenticated;

    function __construct($hdr) {
        $this->IsAuthenticated = false;
        if ($hdr!=null) {
            $this->header = simplexml_load_string($hdr);
            //Your authentication stuff goes here...
        }
    }

    function SomeFunctionOfTheService($a) {
        if ($this->IsAuthenticated) {
            //here Your function...
        }
    }

}
?>
carlos dot vini at gmail dot com 20-May-2010 10:55
SoapServer does not support WSDL with literal/document. I have a class:

<?php
class My_Soap {
    /**
     * Returns Hello World.
     *
     * @param string $world
     * @return string
     */
    public function getInterAdmins($world) {
        return 'hello' . $world;
    }
}
?>

To fix this I had to create proxy class:
<?php
class My_Soap_LiteralDocumentProxy {
   public function __call($methodName, $args) {
       $soapClass = new My_Soap();
       $result = call_user_func_array(array($soapClass, $methodName),  $args[0]);
       return array($methodName . 'Result' => $result);
   }
}
?>

Now make sure that the WSDL is created using My_Soap. And that the Server is created using My_Soap_LiteralDocumentProxy:

<?php

if (isset($_GET['wsdl'])) {
    $wsdl = new Zend_Soap_AutoDiscover(); // It generates the WSDL
    $wsdl->setOperationBodyStyle(array(
        'use' => 'literal'
    ));
    $wsdl->setBindingStyle(array(
        'style' => 'document'
    ));
    $wsdl->setClass('My_Soap');
    $wsdl->handle();
} else {
    $server = new Zend_Soap_Server('http://localhost/something/webservice.php?wsdl');
    $server->setClass('My_Soap_LiteralDocumentProxy');
    $server->handle();
}

?>
dsubar at interna dot com 26-Dec-2009 06:49
Do not put a SoapServer and a SoapClient in the same PHP file. This seems to cause arbitrary behavior. On the PHP interpreter in Eclipse, everything worked fine. Under MAMP, I got an undocumented error. In moving the client from the same file as the server, everything worked fine.


Adds one or more functions to handle SOAP requests

  • ≪ SoapServer
  • SoapServer::addSoapHeader ≫
  • PHP Manual
  • SoapServer
  • Adds one or more functions to handle SOAP requests

SoapServer::addFunction

(PHP 5 >= 5.0.1, PHP 7)

SoapServer::addFunction — Adds one or more functions to handle SOAP requests

설명

public void SoapServer::addFunction ( mixed $functions )

Exports one or more functions for remote clients

인수

functions

To export one function, pass the function name into this parameter as a string.

To export several functions, pass an array of function names.

To export all the functions, pass a special constant SOAP_FUNCTIONS_ALL.

Note:

functions must receive all input arguments in the same order as defined in the WSDL file (They should not receive any output parameters as arguments) and return one or more values. To return several values they must return an array with named output parameters.

반환값

값을 반환하지 않습니다.

예제

Example #1 SoapServer::addFunction() example

<?php

function echoString($inputString)
{
    return $inputString;
}

$server->addFunction("echoString");

function echoTwoStrings($inputString1, $inputString2)
{
    return array("outputString1" => $inputString1,
                 "outputString2" => $inputString2);
}
$server->addFunction(array("echoString", "echoTwoStrings"));

$server->addFunction(SOAP_FUNCTIONS_ALL);

?>

참고

  • SoapServer::SoapServer() - SoapServer constructor
  • SoapServer::setClass() - Sets the class which handles SOAP requests

User Contributed Notes

dotpointer at gmail dot com 27-Oct-2007 03:52
Be careful with SOAP_FUNCTIONS_ALL, as it adds ALL availiable PHP functions to your server.

This can be a potential security threat, imagine clients doing this:

echo $client->file_get_contents("c:\\my files\\my_passwords.doc");

And voila, they have the contents of your file my_passwords.doc.
Evan Borgstrom 24-Aug-2006 12:42
In response to comment by Nathan O'Sullivan about returning (or passing) a complex type, you can also use the stdClass() object.

Assume you define a complex type like so:

<xsd:complexType name="TestType">
        <xsd:all>
                <xsd:element name="A" type="xsd:string" />
                <xsd:element name="B" type="xsd:int" />
                <xsd:element name="C" type="xsd:boolean" />
        </xsd:all>
</xsd:complexType>

To use an object in place of an array you can do:

$test = new stdClass();
$test->A = "test string";
$test->B = 45;
$test->C = false;

$result = $client->Test($test);
Nathan O'Sullivan 29-Apr-2005 08:36
You may be left wondering, as I was, how to return a complex type - consider the following WSDL snippets, for a method called Login:

 <xs:element name="Login">
        <xs:complexType>
          <xs:sequence>
            <xs:element minOccurs="0" maxOccurs="1" name="username" type="xs:string" />
            <xs:element minOccurs="0" maxOccurs="1" name="password" type="xs:string" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>

   <xs:complexType name="UserInfo">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="1" name="Id" type="xs:string" />
          <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
          <xs:element minOccurs="0" maxOccurs="1" name="Nickname" type="xs:string" />
          <xs:element minOccurs="0" maxOccurs="1" name="Email" type="xs:string" />
        </xs:sequence>
      </xs:complexType>

   <xs:element name="LoginResponse">
        <xs:complexType>
          <xs:sequence>
            <xs:element minOccurs="0" maxOccurs="1" name="LoginResult" type="s0:UserInfo" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>

Here's a working Login function  that I've added with add SoapServer::addFunction

function Login($username, $password)
{
  return array("LoginResult", array("Id"=>1, "Name"=>"Nathan", "Nickname"=>"Nathan", "Email"=>"email address") );
}

The UserInfo complextype is represented by the inner array.  The outer array has just one element, "LoginResult".  The LogineResponse element  seems to be treated as a one-member array by PHP.


Add a SOAP header to the response

  • ≪ SoapServer::addFunction
  • SoapServer::__construct ≫
  • PHP Manual
  • SoapServer
  • Add a SOAP header to the response

SoapServer::addSoapHeader

(PHP 5 >= 5.0.1, PHP 7)

SoapServer::addSoapHeader — Add a SOAP header to the response

설명

public void SoapServer::addSoapHeader ( SoapHeader $object )

Adds a SOAP header to be returned with the response to the current request.

인수

object

The header to be returned.

반환값

값을 반환하지 않습니다.

User Contributed Notes

There are no user contributed notes for this page.


SoapServer constructor

  • ≪ SoapServer::addSoapHeader
  • SoapServer::fault ≫
  • PHP Manual
  • SoapServer
  • SoapServer constructor

SoapServer::__construct

(PHP 5 >= 5.0.1, PHP 7)

SoapServer::__construct — SoapServer constructor

설명

public SoapServer::__construct ( mixed $wsdl [, array $options ] )

이 함수는 다음 함수의 별칭입니다: SoapServer::SoapServer()

User Contributed Notes

Anonymous 10-Jun-2012 11:35
// Workin Server with Client for localhost

// server.php

<?php
class MyClass {
  public function helloWorld() {

    return 'Hallo Welt '. print_r(func_get_args(), true);
  }
}

try {
  $server = new SOAPServer(
    NULL,
    array(
     'uri' => 'http://localhost/soap/server.php'
    )
  );

  $server->setClass('MyClass');
  $server->handle();
}

catch (SOAPFault $f) {
  print $f->faultstring;
}

?>

// client.php:

<?php
$client = new SoapClient(null, array(
      'location' => "http://localhost/soap/server.php",
      'uri'      => "http://localhost/soap/server.php",
      'trace'    => 1 ));

echo $return = $client->__soapCall("helloWorld",array("world"));
?>

// Hope you like it


Issue SoapServer fault indicating an error

  • ≪ SoapServer::__construct
  • SoapServer::getFunctions ≫
  • PHP Manual
  • SoapServer
  • Issue SoapServer fault indicating an error

SoapServer::fault

(PHP 5 >= 5.0.1, PHP 7)

SoapServer::fault — Issue SoapServer fault indicating an error

설명

public void SoapServer::fault ( string $code , string $string [, string $actor [, string $details [, string $name ]]] )

Sends a response to the client of the current request indicating an error.

Note:

This can only be called when handling a request.

인수

code

The error code to return

string

A brief description of the error

actor

A string identifying the actor that caused the fault.

details

More details of the fault

name

The name of the fault. This can be used to select a name from a WSDL file.

반환값

값을 반환하지 않습니다.

참고

  • SoapFault::SoapFault() - SoapFault constructor

User Contributed Notes

christian at cmjdesign dot dk 07-Jun-2013 07:41
hi,
to control the fault output one may do the following

/**
 * mySoapServer class
 */
class mySoapServer extends SoapServer {
    public function __construct($wsdl, array $options = null) {
        parent::SoapServer($wsdl, $options);
    }
    public function fault ($code, $string, $actor = null, $details = null, $name = null) {
        throw new SoapFault($code, $string, $actor, $details, $name);
    }
}

Use:
try {
    $server = new mySoapServer(null, array('uri' => $_SERVER['REQUEST_URI']));
    $server->setClass('mySoapAPI');
    $server->handle();
} catch (SoapFault $exc) {
    echo $exc->getTraceAsString();
}

that how i did this,
hope iot can help some one.
brian dot reynolds at risaris dot com 21-Feb-2012 06:44
Example Usage:

return new SoapFault( "Client", "foo or bar must be supplied");

SOAP Fault Codes

VersionMismatch: Found an invalid namespace for the SOAP Envelope element
MustUnderstand    : An immediate child element of the Header element, with the mustUnderstand attribute set to "1", was not understood
Client: The message was incorrectly formed or contained incorrect information
Server: There was a problem with the server so the message could not proceed
dub357 at gmail dot com 09-Feb-2012 05:01
This function also sends a 500 response code back to the client with the request.
This was causing issues with an Apache Axis 1.2 client I had so I instead implemented by own fault handling:

<?php
header("Content-Type: text/xml");
header("Status: 200");
die("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\\"http://schemas.xmlsoap.org/soap/envelope/\\">
  <SOAP-ENV:Body>
    <SOAP-ENV:Fault>
      <faultcode>500</faultcode>
      <faultstring>".$ex->getMessage())."</faultstring>
    </SOAP-ENV:Fault>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>");
?>
Amr Mostafa 09-Sep-2009 03:16
This function currently terminates execution as well, which may be undesirable. See: http://bugs.php.net/bug.php?id=49513
Anonymous 16-Jul-2008 09:12
If you use Adobe Flex, Flash or AIR as SOAP client and are unable to get the error message in case of a soap fault, upgrade to PHP 5.2.6.

Details in:
http://bugs.php.net/bug.php?id=43507


Returns list of defined functions

  • ≪ SoapServer::fault
  • SoapServer::handle ≫
  • PHP Manual
  • SoapServer
  • Returns list of defined functions

SoapServer::getFunctions

(PHP 5 >= 5.0.1, PHP 7)

SoapServer::getFunctions — Returns list of defined functions

설명

public array SoapServer::getFunctions ( void )

Returns a list of the defined functions in the SoapServer object. This method returns the list of all functions added by SoapServer::addFunction() or SoapServer::setClass().

인수

이 함수는 인수가 없습니다.

반환값

An array of the defined functions.

예제

Example #1 SoapServer::getFunctions() example

<?php
$server = new SoapServer(NULL, array("uri" => "http://test-uri"));
$server->addFunction(SOAP_FUNCTIONS_ALL);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $server->handle();
} else {
  echo "This SOAP server can handle following functions: ";
  $functions = $server->getFunctions();
  foreach($functions as $func) {
    echo $func . "\n";
  }
}
?>

참고

  • SoapServer::SoapServer() - SoapServer constructor
  • SoapServer::addFunction() - Adds one or more functions to handle SOAP requests
  • SoapServer::setClass() - Sets the class which handles SOAP requests

User Contributed Notes

There are no user contributed notes for this page.


Handles a SOAP request

  • ≪ SoapServer::getFunctions
  • SoapServer::setClass ≫
  • PHP Manual
  • SoapServer
  • Handles a SOAP request

SoapServer::handle

(PHP 5 >= 5.0.1, PHP 7)

SoapServer::handle — Handles a SOAP request

설명

public void SoapServer::handle ([ string $soap_request ] )

Processes a SOAP request, calls necessary functions, and sends a response back.

인수

soap_request

The SOAP request. If this argument is omitted, the request is assumed to be in the raw POST data of the HTTP request.

반환값

값을 반환하지 않습니다.

예제

Example #1 SoapServer::handle() example

<?php
function test($x)
{
    return $x;
}

$server = new SoapServer(null, array('uri' => "http://test-uri/"));
$server->addFunction("test");
$server->handle();
?>

참고

  • SoapServer::SoapServer() - SoapServer constructor

User Contributed Notes

Bas van Dorst 19-Feb-2014 11:58
Additional information to the comment of "Joeri Thissen" (http://www.php.net/manual/en/soapserver.handle.php#113866)

In some cases the replacements generates a timeout (looks like it is in combination with Nginx). The problem is that PHP has already sent a content-length, and the webserver is still waiting for new content.

To fix this you have to reset the HTTP Content-Length with the right value:

<?php
ob_start();
$soapServer->handle();
$result = ob_get_contents();
ob_end_clean();

$result = str_replace("abcdef", "abc", $result);
$length = strlen($result);

header("Content-Length: ".$length);
echo $result;
Joeri Thissen 11-Dec-2013 07:59
Sometimes returned data can contain characters which are not valid in xml 1.0. This causes the xml being output by SoapServer::handle to be invalid. Although it's probably better to sanitize the data ealier, a combination of output buffering and a simple regular expression can be used as a quick fix to make sure the output is indeed valid xml.

For example:

<?php
    ob_start();
    $soapServer->handle();
    $soapXml = ob_get_contents();
    ob_end_clean();
    $soapXml = preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $soapXml);
    echo $soapXml;
?>
dub357 at gmail dot com 09-Feb-2012 07:54
After much headache and looking through PHP source code, I finally found out why the handle() function would immediately send back a fault with the string "Bad Request".
Turns out that my client was sending valid XML, but the first line of the XML was the actual XML declaration:

<?xml version="1.0" encoding="UTF-8"?>

When the "handle" function in the SoapServer class is called, it first tries to parse the XML.  When the XML document can't be parsed, a "Bad Request" fault is returned and execution of the script immediately stops.  I assume that the XML parser built into PHP (libxml2) already assumes the document to be XML and when it finds the declaration, it thinks it isn't valid.

I added some XML parsing calls to my service before the handle() function is called to check for valid XML and avoid the "Bad Request" fault.  This also allows me to send back a more suitable error message:

<?php
$parser = xml_parser_create("UTF-8");
if (!xml_parse($parser,$HTTP_RAW_POST_DATA,true)){
   $webService->fault("500", "Cannot parse XML: ".
      xml_error_string(xml_get_error_code($parser)).
       " at line: ".xml_get_current_line_number($parser).
       ", column: ".xml_get_current_column_number($parser));
}
?>
prenaud at profideo dot com 09-Jan-2012 11:23
Please note that when defining several services in one wsdl file, and calling one of those services, you may always get the response for your first service.

This is a known bug. You will find its description and some workarounds here  : https://bugs.php.net/bug.php?id=49169
Artur Graniszewski 15-Nov-2009 07:19
Be aware that SoapServer::handle(); method sends additional HTTP headers to the browser. One of them is "Content-Type: application/soap+xml". If you want to execute SOAP methods locally as a part of SoapClient::__doRequest() (see example at http://pl2.php.net/manual/en/soapclient.dorequest.php ) you may need to reset (override) this header back to "Content-Type: text/html" like so:

<?php
function Add($x,$y) {
  return $x+$y;
}

class LocalSoapClient extends SoapClient {

  function __construct($wsdl, $options) {
    parent::__construct($wsdl, $options);
    $this->server = new SoapServer($wsdl, $options);
    $this->server->addFunction('Add');
  }

  function __doRequest($request, $location, $action, $version) {
    ob_start();
    $this->server->handle($request);
    $response = ob_get_contents();
    ob_end_clean();
    return $response;
  }

}

$x = new LocalSoapClient(NULL,array('location'=>'test://',
                                   'uri'=>'http://testuri.org'));

header("Content-Type: text/html");

var_dump($x->Add(3,4));

?>
tom at backslashinteractive dot com 03-Oct-2008 07:54
In response to Blizzke:

Sometimes this problem can be hidden by an Apache segmentation fault along with an HTTP headers error SoapFault thrown to the client.

If you get either of those 2, try checking to make sure that style="rpc" in your WSDL file's soap:operation's.

-T
Blizzke at gmail dot com 12-Mar-2008 01:39
Seems pretty logical once you find the solution, but it took me quite a while to figure this one out:
If you are using WSDL based SOAP requests and you have more than one operation in your binding (with the same parameters), make sure the <soap:operation> style is set to rpc, NOT body!

When you specify 'body' here, all that will be transmitted in the request is the parameters for the function call, and SoapServer->handle() will use the first function it finds with the same parameter-makeup to handle the call.

ie If you have 2 functions:
<?php
function One ( string $blah );
function Two ( string $blah );
?>
Making a client call with SoapClient -> Two ( 'test' ); will result in One ( ) being called when your 'type' is set to 'body'

The actual method to call will only be included in the request when your type is set to 'rpc', resulting in the expected behavior
king dot maxemilian at noos dot fr 21-Aug-2007 06:23
Sometime, it happens that PHP does not detect anything in $HTTP_RAW_POST_DATA.

To solve this problem and make it work in any case:

function soaputils_autoFindSoapRequest()    {
    global $HTTP_RAW_POST_DATA;

    if($HTTP_RAW_POST_DATA)
        return $HTTP_RAW_POST_DATA;

    $f = file("php://input");
    return implode(" ", $f);
}

$server = new SoapServer($wsdl);
$server->setClass($MyClass);

$server->handle(soaputils_autoFindSoapRequest());


Sets the class which handles SOAP requests

  • ≪ SoapServer::handle
  • SoapServer::setObject ≫
  • PHP Manual
  • SoapServer
  • Sets the class which handles SOAP requests

SoapServer::setClass

(PHP 5 >= 5.0.1, PHP 7)

SoapServer::setClass — Sets the class which handles SOAP requests

설명

public void SoapServer::setClass ( string $class_name [, mixed $args [, mixed $... ]] )

Exports all methods from specified class.

The object can be made persistent across request for a given PHP session with the SoapServer::setPersistence() method.

인수

class_name

The name of the exported class.

args

These optional parameters will be passed to the default class constructor during object creation.

반환값

값을 반환하지 않습니다.

참고

  • SoapServer::SoapServer() - SoapServer constructor
  • SoapServer::addFunction() - Adds one or more functions to handle SOAP requests
  • SoapServer::setPersistence() - Sets SoapServer persistence mode

User Contributed Notes

funky2step at gmail dot com 25-Nov-2011 10:54
When passing arguments to the default class constructor, make sure that you don't try and use the magic __construct method because that won't work.

<?php

class MyClass
{
  function MyClass($arg1, $arg2)
  {

  }
}

// Not:

class MyClass
{
  function __construct($arg1, $arg2)
  {

  }
}

?>
christiaan at oakfox dot net 04-May-2009 01:21
You can also retrieve object properties the following way while using __autoload($class_name).

<?php

# Start Session
session_start();

# Auto Load Class as Required
function __autoload($class_name)
{
    require_once "/var/www/example/class/". $class_name. ".php";
}

//service

$_SESSION[_bogus_session_name] = unserialize($_SESSION[_bogus_session_name]);
$server = new SoapServer('service.wsdl');
$server->setClass("MyClass");
$server->setPersistence(SOAP_PERSISTENCE_SESSION);
$server->handle();
$_SESSION[_bogus_session_name] = serialize($_SESSION[_bogus_session_name])

?>
Ariz Jacinto 05-Dec-2008 01:38
If you want your SOAP client to be able to save and then retrieve the object properties, you need to set the SOAP server to be persistent by setting session.auto_start=0, invoking session_start(), and SoapServer->setPersistence(SOAP_PERSISTENCE_SESSION) in the following manner:

<?php
  //set ini
  ini_set("soap.wsdl_cache_enabled", 0);
  ini_set("session.auto_start", 0);

  //class file
  require_once('MyClass.php');

  //for persistent session
  session_start();

  //service
  $server = new SoapServer('service.wsdl');
  $server->setClass("MyClass");
  $server->setPersistence(SOAP_PERSISTENCE_SESSION);
  $server->handle();
?>


Sets the object which will be used to handle SOAP requests

  • ≪ SoapServer::setClass
  • SoapServer::setPersistence ≫
  • PHP Manual
  • SoapServer
  • Sets the object which will be used to handle SOAP requests

SoapServer::setObject

(PHP 5 >= 5.2.0, PHP 7)

SoapServer::setObject — Sets the object which will be used to handle SOAP requests

설명

public void SoapServer::setObject ( object $object )

This sets a specific object as the handler for SOAP requests, rather than just a class as in SoapServer::setClass().

인수

object

The object to handle the requests.

반환값

값을 반환하지 않습니다.

참고

  • SoapServer::setClass() - Sets the class which handles SOAP requests

User Contributed Notes

There are no user contributed notes for this page.


Sets SoapServer persistence mode

  • ≪ SoapServer::setObject
  • SoapServer::SoapServer ≫
  • PHP Manual
  • SoapServer
  • Sets SoapServer persistence mode

SoapServer::setPersistence

(PHP 5 >= 5.1.2, PHP 7)

SoapServer::setPersistence — Sets SoapServer persistence mode

설명

public void SoapServer::setPersistence ( int $mode )

This function allows changing the persistence state of a SoapServer object between requests. This function allows saving data between requests utilizing PHP sessions. This method only has an affect on a SoapServer after it has exported functions utilizing SoapServer::setClass().

Note:

The persistence of SOAP_PERSISTENCE_SESSION makes only objects of the given class persistent, but not the class static data. In this case, use $this->bar instead of self::$bar.

Note:

SOAP_PERSISTENCE_SESSION serializes data on the class object between requests. In order to properly utilize resources (e.g. PDO), __wakeup() and __sleep() magic methods should be utilized.

인수

mode

One of the SOAP_PERSISTENCE_XXX constants.

SOAP_PERSISTENCE_REQUEST - SoapServer data does not persist between requests. This is the default behavior of any SoapServer object after setClass is called.

SOAP_PERSISTENCE_SESSION - SoapServer data persists between requests. This is accomplished by serializing the SoapServer class data into $_SESSION['_bogus_session_name'], because of this session_start() must be called before this persistence mode is set.

반환값

값을 반환하지 않습니다.

예제

Example #1 SoapServer::setPersistence() example

<?php
 class MyFirstPersistentSoapServer {
     private $resource; // (Such as PDO, mysqli, etc..)
     public $myvar1;
     public $myvar2;

     public function __construct() {
         $this->__wakeup(); // We're calling our wakeup to handle starting our resource
     }

     public function __wakeup() {
         $this->resource = CodeToStartOurResourceUp();
     }

     public function __sleep() {
         // We make sure to leave out $resource here, so our session data remains persistent
         // Failure to do so will result in the failure during the unserialization of data
         // on the next request; thus, our SoapObject would not be persistent across requests.
         return array('myvar1','myvar2');
     }
 }

 try {
     session_start();
     $server = new SoapServer(null, array('uri' => $_SERVER['REQUEST_URI']));
     $server->setClass('MyFirstPersistentSoapServer');
     // setPersistence MUST be called after setClass, because setClass's
     // behavior sets SESSION_PERSISTENCE_REQUEST upon enacting the method.
     $server->setPersistence(SOAP_PERSISTENCE_SESSION);
     $server->handle();
 } catch(SoapFault $e) {
     error_log("SOAP ERROR: ". $e->getMessage());
 }
?>

참고

  • SoapServer::setClass() - Sets the class which handles SOAP requests

User Contributed Notes

csnaitsirch at web dot de 01-Apr-2010 02:25
I want to give one example for the order of commands if you want to use a class in persistence mode.

<?php
// 1. class definition or include
class UserService
{
    public function __construct() { }
}

// 2. start the session after defining or including the class!!
session_start();

// 3. instanciate the server
$server = new SoapServer(null, array("something"));

// 4. set the class to use
$server->setClass('UserService');

// 5. set persistance mode
$server->setPersistence(SOAP_PERSISTENCE_SESSION);

// 6. handle the request
$server->handle();
?>
doug dot manley at gmail dot com 30-May-2008 09:54
When using "SoapServer::setPersistence( SOAP_PERSISTENCE_SESSION )", you apparently MUST include the class that was used in "SoapServer::setClass()" BEFORE any "session_*" commands.

I found this out using "__autoload()" and a whole lot of "syslog()"; it kept failing to include the class that I was using for my soap server, but that class is ONLY ever referenced by the page itself, and even then only for the purposes of setting the class for the soap server; none of my code would ever cause it to autoload.  The problem was that I was including my session-handling code first.

If the session gets started BEFORE the page defines the class definition, then persistence CANNOT happen.

The order should be:
1. Include the class for use with the soap server.
2. Start up your session.
3. Set up your soap server.
4. Handle your soap request.
boogiebug at gmail dot com 26-Mar-2008 04:40
setPersistence works only for a single instance of service class.

To use multiple instance of services objects, you need to instantiate the classes into objects and use an undocumented SoapServer's method - setObject() to add the service object into the SoapServer object, and handle the service object persistence with $_SESSION instead.

For example:

$ServiceObjects = array()
$ServiceObjects[0] = new ServiceClass1();
$ServiceObjects[1] = new ServiceClass2();
$ServiceObjects[2] = new ServiceClass3();

$_SESSION['ServiceClass1'] = $ServiceObjects[0];
$_SESSION['ServiceClass2'] = $ServiceObjects[1];
$_SESSION['ServiceClass3'] = $ServiceObjects[2];

...

$Servers = array()
for ( $i = 0; $i < count($ServiceObjects); i++)
{
  $s = new SoapServer($wsdl);
  $s->setObject($ServiceObject[$i]);
  $Servers[] = $s;
}

...

$Server[$i]->handle()

...
jan at pinna dot nl 26-Jan-2008 12:28
I found that using both modes (SOAP_PERSISTENCE_SESSION and SOAP_PERSISTENCE_REQUEST) cannot be used simultaniously. Because it didn't work at once, I started experimenting by using different settings and as stated below in the comments, "...also use SOAP_PERSISTENCE_REQUEST to save objects between requests" led me to think it was nessecary to use both modes. Well, it might for others, be but for me it turned out a day of freaking out ;) (trying all kinds of session stuff, etc etc).
Also, if persistence doesn't work, please check if session_start() is called somewhere in the script and try not to call it twice or whatsoever: it won't work...
jared at ws-db dot com 28-Aug-2005 09:07
I had some issues getting session persistence (SOAP_PERSISTENCE_SESSION) to work. I finally got it working after setting session.auto_start=0, and then only calling session_start() in the script containing the SoapServer. Maybe this is obvious, but took me a bit to figure it out.

I only tried it with session.use_cookies=1, so if the settings above don't work for you, make sure cookies are enabled, though it may work without the need for cookies.
cperez1000 at hotmail dot com 29-May-2005 05:54
Always remember to place the "setPersistence" method before the handle method, otherwise it won't work.  It sounds obvious, but it's still a very common mistake, since no errors are shown.


SoapServer constructor

  • ≪ SoapServer::setPersistence
  • SoapFault ≫
  • PHP Manual
  • SoapServer
  • SoapServer constructor

SoapServer::SoapServer

(PHP 5 >= 5.0.1, PHP 7)

SoapServer::SoapServer — SoapServer constructor

설명

public SoapServer::SoapServer ( mixed $wsdl [, array $options ] )

This constructor allows the creation of SoapServer objects in WSDL or non-WSDL mode.

인수

wsdl

To use the SoapServer in WSDL mode, pass the URI of a WSDL file. Otherwise, pass NULL and set the uri option to the target namespace for the server.

options

Allow setting a default SOAP version (soap_version), internal character encoding (encoding), and actor URI (actor).

The classmap option can be used to map some WSDL types to PHP classes. This option must be an array with WSDL types as keys and names of PHP classes as values.

The typemap option is an array of type mappings. Type mapping is an array with keys type_name, type_ns (namespace URI), from_xml (callback accepting one string parameter) and to_xml (callback accepting one object parameter).

The cache_wsdl option is one of WSDL_CACHE_NONE, WSDL_CACHE_DISK, WSDL_CACHE_MEMORY or WSDL_CACHE_BOTH.

There is also a features option which can be set to SOAP_WAIT_ONE_WAY_CALLS, SOAP_SINGLE_ELEMENT_ARRAYS, SOAP_USE_XSI_ARRAY_TYPE.

The send_errors option can be set to FALSE to sent a generic error message ("Internal error") instead of the specific error message sent otherwise.

예제

Example #1 SoapServer::SoapServer() example

<?php

$server = new SoapServer("some.wsdl");

$server = new SoapServer("some.wsdl", array('soap_version' => SOAP_1_2));

$server = new SoapServer("some.wsdl", array('actor' => "http://example.org/ts-tests/C"));

$server = new SoapServer("some.wsdl", array('encoding'=>'ISO-8859-1'));

$server = new SoapServer(null, array('uri' => "http://test-uri/"));

class MyBook {
    public $title;
    public $author;
}

$server = new SoapServer("books.wsdl", array('classmap' => array('book' => "MyBook")));

?>

변경점

버전 | 설명
5.2.0 | Added the typemap option.

참고

  • SoapClient::SoapClient() - SoapClient constructor

User Contributed Notes

driezasson at me dot com 03-Jul-2015 09:31
There is a option key 'send_errors' that can be use not to expose PHP (fatal) error messages to the SoapFault message text.

If server is created with a 'send_errors' => false option provied, the faultstring of the SoapFault thrown upon running into a PHP error would be a generic "Internal Error" instead of e.g. "Class 'MySecretClass' not found.".

Example:
$server = new SoapServer('http://path.to/my.wsdl', array('send_errors' => false));

The only place this is mentioned is in a discussion under the bug #42214. However, it is not mentioned in the manual page, at least not at the time of writing this note.
the dot a77icu5 at gmail dot com 07-Jan-2014 06:14
only complementing Juamei answer, the header is passed to UsernameToken method as an object with 2 properties Username and Password.
andyidol at gmail dot com 03-Apr-2012 08:21
Here's my solution to make SOAP-headers based authentication.

1). First of all we define the decorator class for our service class:

<?php

class SOAP_Service_Secure
{
    protected $class_name    = '';
    protected $authenticated = false;

    // -----

    public function __construct($class_name)
    {
        $this->class_name = $class_name;

    }

    public function AuthHeader($Header)
    {
        if($Header->username == 'foo' && $Header->password == 'bar')
            $this->authenticated = true;

    }

    public function __call($method_name, $arguments)
    {
        if(!method_exists($this->class_name, $method_name))
            throw new Exception('method not found');

        $this->checkAuth();

        return call_user_func_array(array($this->class_name, $method_name), $arguments);

    }

    // -----

    protected function checkAuth()
    {
        if(!$this->authenticated)
            HTML_Output::error(403);

    }

}

?>

2). Then we pass an instance of it to the SoapServer object.

<?php

    $Service = new SOAP_Service_Secure('My_Service');

    $Server = new SoapServer('my-service.wsdl');

    $Server->setObject($Service);

    $Server->handle();

?>

3). Implementing a client:

<?php

class AuthHeader
{
    public $username;
    public $password;

}

// -----

$Client = new SoapClient('http://example.com/my-service.wsdl', array(
    'exceptions' => true
));

// -----

$AuthHeader = new AuthHeader();

$AuthHeader->username = 'foo';
$AuthHeader->password = 'bar';

$Headers[] = new SoapHeader('http://example.com', 'AuthHeader', $AuthHeader);

$Client->__setSoapHeaders($Headers);

// -----

$Result = $Client->someMethod();

?>

As you can see SoapServer uses our decorator class to answer client requests. If request contains "AuthHeader" SOAP-header then our wrapper checks the credentials and sets $authenticated parameter to TRUE (on success). Then SoapServer calls requested method on our wrapper, we are intercepting it with __call(), checking authentication and if everything is allright we are calling real method on the "My_Service" class.
jille at quis dot cx 07-May-2011 05:04
The SoapServer can not generate WSDL's yet. (PHP 5.3.5)
It'll exit with "SOAP-ENV:ServerWSDL generation is not supported yet"
ricohn hotmail com 29-Jan-2011 04:18
If you're running $soapServer->handle() and getting an empty server response and no errors (even in the logs), check if you're instantiating the SOAP server like this:

<?php
$soapServer = new SoapServer('http://www.example.com/soap/endpoint?wsdl');
//...
$soapServer->handle(); // Result: Empty response
?>

Instantiating the SOAP server with one the following should fix it:

<?php
// With slash before ?wsdl:
$soapServer = new SoapServer('http://www.example.com/soap/endpoint/?wsdl');
// Or instantiate with the "uri" option, which works with or without the trailing slash:
$soapServer = new SoapServer(null, array('uri' => 'http://www.example.com/soap/endpoint?wsdl'));
$soapServer = new SoapServer(null, array('uri' => 'http://www.example.com/soap/endpoint/?wsdl'));
?>

Hope that helps somebody! Zend_Soap_Server wasn't working for me, and I eventually found that it had to do with the native PHP SOAP class (which ZF uses).
nilzor at gmail dot com 12-May-2010 12:04
Calling a server from .NET:
If you intend to call a PHP SOAP server from a .NET Client, there are a couple of things to be aware of. First of all, I've only got it working in WSDL mode. If anyone have made it work in non-WSDL-mode, I'd be interested in hearing that. In WSDL-mode it works, but both input and output-parameters are wrapped from .NET.

Basically, a function with this signature:

<?php
function SomeFunction($param1, $param2)
{
   return $param1+$param2;
}
?>

Must be rewritten to this, without changing the WSDL:

<?php
function SomeFunction($data)
{
 $valueArray = get_object_vars($data);
 $param1 = $valueArray["param1"];
 $param2 = $valueArray["param2"];
 return Array("SomeFunctionResult" => $param1+$param2);
}
?>
Sloloem 07-Oct-2009 05:31
typemap isn't very well documented at all, but I thought I might be able to use it to clean some things up and map between object fields and XML attributes that have different names but are conceptually the same thing.

Unfortunately type map works at the object level, not the field level like I was hoping, requiring you to provide functions for building the entire object from an XML string (using the XML parser of your choice) and vice versa.  I found the wording of the docs very confusing, both parameters are strings which are function names.

the to_xml function will take the object as a parameter and return a string,
the from_xml function will take an xml string as a parameter and should return an object of the correct type.

Both methods can also throw SOAPFaults or other exceptions which can be caught from the actual client method call ($client->someRemoteMethod())

There are decent usage examples in the PHP build tests:  http://cvs.php.net/viewvc.cgi/php-src/ext/soap/tests/?diff_format=u
The typemap tests are at the bottom.
spiro79 at gmail dot com 21-Sep-2009 10:12
If you are getting an error about always_populate_raw_post_data or Can't find HTTP_RAW_POST_DATA then it means the server isn't getting info from the (you guessed!) raw post data.
Try doing something like this when coding your server:

<?php
$server = new SoapServer(null,array('uri' => "http://localhost/namespace"));
$server->setClass('myClass');
$data = file_get_contents('php://input');
$server->handle($data);
?>

Worked for me!
kostya[.]chumickin [at] 9mail[.]c0m 28-Mar-2009 12:06
Beware: in wsdl mode SoapServer does not properly validate if
request matches the given WSDL. It just skips invalid elements. http://bugs.php.net/bug.php?id=45966
beckman at angryox.com 04-Nov-2008 11:01
While others may not do what I did, I thought if my WSDL was not local and needed to be grabbed via HTTP, I should set the $wsdl var in __construct() to NULL, and set the 'uri' value in the additional parameters.

This does enable non-WSDL mode, which only causes problems when using complex types.  Since your complex types are not respected/imported, you get unexpected problems out the wazoo.

So if you are exposing your class via "setClass" to the SOAP server and for some reason your "types" aren't being returned, you can do this:

<?php
$server = new SoapServer('http://soap.example.com/genwsdl.php?api');
$server->setClass('myClass'); // previously included or defined
$server->handle();
?>

This is NOT correct:
<?php
$server = new SoapServer(NULL, array('uri' => 'http://soap.example.com/genwsdl.php?api'));
?>

I sure wish someone really smart would go into a bit more detail in this documentation of the SoapServer options, why they exist, when to use them, etc.  I find the SOAP docs lacking polish.
lordi at msdi dot ca 29-Jul-2008 06:45
Things that I've searched a lot for that might be useful to somebody:

If you are using Zend Studio, you can easily generate a wsdl file using the WSDL Generator if you document your code using PHPDOC.

One thing a lot of people are searching everywhere, is the way to pass/return an array of variables... In your PHPDOC block use the [] after the variable name like this

<?php
class SoapServer {
  /**
   * This is My Soap Server
   * @param integer[] $a
   * @param string[] $b
   * @param bar[] $c
  */
  public function foo ($a, $b, $c){
  }
}
?>
This way, the function will require an array of integers for $a, an array of strings for $b and an array of objets of Class "bar" for $c

In the WSDL, you'll find something like this

<xsd:complexType name="barArray">
  <xsd:complexContent>
    <xsd:restriction base="soapenc:Array">
      <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="typens:bar[]"/>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="bar">
  <xsd:all>
    <xsd:element name="var1" type="xsd:boolean"/>
    <xsd:element name="var2" type="xsd:integer"/>
    <xsd:element name="var3" type="xsd:string"/>
  </xsd:all>
</xsd:complexType>

I didn't define anywhere the barArray type, Zend's WSDL Generator generated it automatically...

If you are not using Zend you can generate the wsdl file manually.

Hope it helps someone
php dot net at juamei dot com 31-Oct-2007 12:42
In response to Timo, it is possible to access Soap Headers from the SoapServer class and call methods to handle them. Rather than treat them seperately, they are treated as part of the Soap request.

To pass approximately wss standard headers (note that I couldn't get attributes from the header tags without text preprocessing, so standards compliance failed me), first create the following class on the client:
<?php
/*
 * Login credentials to be supplied as a SOAP header
 * Class used to ensure Soap Head tags in correct format.
 */
class SoapHeaderUsernameToken
{
    /** @var int Password */
    public $Password;
    /** @var int Username */
    public $Username;

    public function __construct($l, $p)
    {
        $this->Password = $p;
        $this->Username = $l;
    }
}
?>

Now, set the Soap Headers:

<?php
    // Set the login headers
    $wsu = 'http://schemas.xmlsoap.org/ws/2002/07/utility';
    $usernameToken = new SoapHeaderUsernameToken($this->username, $this->password);
    $soapHeaders[] = new SoapHeader($wsu, 'UsernameToken', $usernameToken);
?>

Next instantiate the client object, add the headers and make the call:

<?php
    $client = new SoapClient( $wsdl);
    $client->__setSoapHeaders( $soapHeaders );
    $client->__soapCall( $method, $params );
?>

Moving to the Server, you'll handle the Soap call somewhat like:
<?php
// Instantiate server with relevant wsdl & class.
$server = new SoapServer( 'mysoapwsdl.wsdl' );
$server->setClass(  'mysoapclass' );
$server->handle();
?>

The key here is that the headers will be handled first by the server which will call the method mysoapclass::UsernameToken. The way we work this is the UsernameToken method authenticates the username / password combo and sets a protected class var. Then when the Soap Body is handled and the appropriate class method called, a check is made at the start of callable method to ensure authentication has been passed.

If the Soap auth header is missing, the soap body method will be called anyway so the check is essential in case a malevolent client deliberately leaves the headers off.

An example server class is below (missing various methods which I leave as an exercise to the reader to provide):
<?php
class mysoapclass {
    /*
     * Authentication function
     * This is called by the Soap header of the same name. The function name is a Ws-security standard auth tag
     * and corresponds to the header tag.
     *
     * @param string username
     * @param string password
     */
    public function UsernameToken( $username, $password ){
        // Store username for logging
        $this->Username = $username;

        $auth = new $this->AuthClass( $username, $password, get_class($this) );
        if( $auth->IsValid() ){
            $this->Authenticated = true;
        } else {
            $this->ThrowSoapFault( 'auth' );
        }
    }

    /*
     * Test method
     */
    public function felineResponse( $action ){

        // Place this at the start of every exposed method
        // This is because the SoapServer will attempt to call
        // if no authentication headers are passed.
        if(!$this->Authenticated){
            $this->ThrowSoapFault( 'auth' );
        }

        if( $action == 'stroke' ){
            return 'purr';
        } elseif( $action == 'tease' ){
            return 'hiss';
        }
    }

}
?>

This is a adaptation of stuff I found online somewhere...
joel dot pearson at gmail dot com 11-Jul-2007 07:12
In response to jas [at] dansupport (dot) dk:

All you need to do is disable wsdl caching like so:

ini_set("soap.wsdl_cache_enabled", "0");

Then you don't need to delete any cache files.
jas [at] dansupport (dot) dk 30-May-2007 08:24
A general thing i've experienced with SOAP and which, for some reason, isn't mentioned in ANY tutorials I've read, is this: The server tends to cache the interface. This means that if you add a function you'll usually get errors that the function doesn't exist.
If your SOAP server is written in PHP just delete the cache files, usually located in /tmp, whenever you add a function, or modify the parameters. These are named wsdl-******something******
I hope this will spare someone the grief I've experienced with this.
Timo 03-Aug-2006 02:46
It is currently not possible to process soap headers from within a SoapServer instance. If soap headers are specified within a WSDL file, you have to extract the headers manually from the request.

For more information please see http://bugs.php.net/bug.php?id=38309


The SoapFault class

(PHP 5 >= 5.0.1, PHP 7)

소개

Represents a SOAP fault.

클래스 개요

SoapFault extends Exception {
/* 상속된 프로퍼티 */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* 메소드 */
__construct ( string $faultcode , string $faultstring [, string $faultactor [, string $detail [, string $faultname [, string $headerfault ]]]] )
SoapFault ( string $faultcode , string $faultstring [, string $faultactor [, string $detail [, string $faultname [, string $headerfault ]]]] )
public string __toString ( void )
/* 상속된 메소드 */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}

Table of Contents

  • SoapFault::__construct — SoapFault constructor
  • SoapFault::SoapFault — SoapFault constructor
  • SoapFault::__toString — Obtain a string representation of a SoapFault

User Contributed Notes

fbernoldi at gmail dot com 04-Oct-2012 02:46
Hi all,

I've decided to post this since it may be helpful, I've spend a couple of days trying to do this.

In order to use wsdl's specified faults with complex types, i.e:

WSDL definitions:

(xsd:schema namespace, ns1 = target namespace)

<xsd:element name="doubleFault">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="detail1" type="xsd:string"/>
      <xsd:element name="detail2" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

WSDL messages:

<message name="fault_specified">
    <part name="relevant_name" element="ns1:doubleFault"/>
</message>

WSDL port type:

<portType name="test">
  <operation name="operationTest">
  <input message="ns1:not_relevant_request"/>
  <output message="ns1:not_relevant_response"/>
  <fault name="FaultSpecified" message="ns1:fault_specified"/>
  ....
</portType>

You have to specify the response in the detail parameter as an array corresponding the tag names.

PHP Code:

<?php

function operationTest($request_param ...) {

    // ...
    $array_details = array("detail1" => "Explanation 1", "detail2" => "Explanation 2");

    return new SoapFault("Server", "example fault string", null, $array_details, "FaultSpecified");

}

$server = new SOAPServer("handmade.wsdl");
$server->addFunction("operationTest");
$server->handle();

?>

that should respond something like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://mynamespace">
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Server</faultcode>
         <faultstring>example fault string</faultstring>
         <detail>
            <ns1:doubleFault>
               <detail1>Explanation 1</detail1>
               <detail2>Explanation 2</detail2>
            </ns1:doubleFault>
         </detail>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I Hope it helps,
Federico.
chris AT cmbuckley DOT co DOT uk 13-May-2010 12:53
A bit more digging in ext/soap/soap.c and the set_soap_fault function reveals the other undocumented properties from the constructor:

<?php
try {
    throw new SoapFault('code', 'string', 'actor', 'detail', 'name', 'header');
} catch (Exception $ex) {
    var_dump($ex->faultcode, $ex->faultstring, $ex->faultactor, $ex->detail, $ex->_name, $ex->headerfault);
}
?>
dmitry dot koterov at gmail dot com 29-Oct-2009 11:21
You may use undocumented and invisible property $e->faultcode to access string version of $code. Because standard $e->getCode() does not work:

<?php
$e = new SoapFault("test", "msg");
var_dump($e->getCode()); // prints "0"
var_dump($e->faultcode); // prints "test"
?>

Also you may use namespaced fault codes:

<?php
$e = new SoapFault(array("namespace", "test"), "msg");
?>

- see ext/soap/soap.php, PHP_METHOD(SoapFault, SoapFault). To access the namespace, use $e->faultcodens


SoapFault constructor

  • ≪ SoapFault
  • SoapFault::SoapFault ≫
  • PHP Manual
  • SoapFault
  • SoapFault constructor

SoapFault::__construct

(PHP 5 >= 5.0.1, PHP 7)

SoapFault::__construct — SoapFault constructor

설명

SoapFault::__construct ( string $faultcode , string $faultstring [, string $faultactor [, string $detail [, string $faultname [, string $headerfault ]]]] )

이 함수는 다음 함수의 별칭입니다: SoapFault::SoapFault()

User Contributed Notes

csnaitsirch at web dot de 01-Apr-2010 02:04
The first Parameter of the constructor, the faultcode, of SoapFault must be a string. Otherwise it will lead to an error.

<?php
throw new SoapFault(1, "Error message!"); // wrong
throw new SoapFault("1", "Error message!"); // right
?>


SoapFault constructor

  • ≪ SoapFault::__construct
  • SoapFault::__toString ≫
  • PHP Manual
  • SoapFault
  • SoapFault constructor

SoapFault::SoapFault

(PHP 5 >= 5.0.1, PHP 7)

SoapFault::SoapFault — SoapFault constructor

설명

SoapFault::SoapFault ( string $faultcode , string $faultstring [, string $faultactor [, string $detail [, string $faultname [, string $headerfault ]]]] )

This class is used to send SOAP fault responses from the PHP handler. faultcode, faultstring, faultactor and detail are standard elements of a SOAP Fault.

인수

faultcode

The error code of the SoapFault.

faultstring

The error message of the SoapFault.

faultactor

A string identifying the actor that caused the error.

detail

More details about the cause of the error.

faultname

Can be used to select the proper fault encoding from WSDL.

headerfault

Can be used during SOAP header handling to report an error in the response header.

예제

Example #1 Some examples

<?php
function test($x)
{
    return new SoapFault("Server", "Some error message");
}

$server = new SoapServer(null, array('uri' => "http://test-uri/"));
$server->addFunction("test");
$server->handle();
?>

It is possible to use PHP exception mechanism to throw SOAP Fault.

Example #2 Some examples

<?php
function test($x)
{
    throw new SoapFault("Server", "Some error message");
}

$server = new SoapServer(null, array('uri' => "http://test-uri/"));
$server->addFunction("test");
$server->handle();
?>

참고

  • SoapServer::fault() - Issue SoapServer fault indicating an error
  • is_soap_fault() - Checks if a SOAP call has failed

User Contributed Notes

Istratov Vadim 15-Dec-2010 03:46
In case you need to return SOAP Fault for SOAP Client based on Adobe Flash/Flex/AIR and stuck with Flash's inability to process SOAP messages with HTTP code 500 (that's what SOAP Fault returns; read more on this error here: http://bugs.php.net/bug.php?id=43507) then try this dirty hack:

<?php
$server = new SoapServer ( "SomeWSDL.wsdl" );
$server->setClass ( "SOAP_Class" );

/**
 * Catching SOAP Server response and overriding HTTP Status code.
 */
ob_start();
$server->handle ();
$soapResponse = ob_get_contents();
ob_end_clean();

header('HTTP/1.0 200 OK');
echo $soapResponse;
?>

Keep in mind that from PHP 5.2.6 SOAP Fault has an HTTP Status Code = 200 if User-agent is "Shockwave Flash", but when Flash object is integrated into HTML page SOAP Server receives User-agent = Broswer-agent and NOT "Shockwave Flash".

Also it'll be a good idea to return HTTP 200 Code only if 500 Error is caused by known service faults.
klemens dot r at gmail dot com 20-May-2010 11:17
To get custom Soap Error Codes use in the catch $e->faultcode instead of $e->getCode.

<?php
try {
    // ...
} catch (SoapFault $e) {
    echo $e->faultcode;
}
?>
del at babel dot com dot au 14-Jul-2008 09:10
There are only a few legitimate "codes" that a SOAP fault can have and be understood by most clients in accordance with the protocol.  You can't just pass in, for example, a database error code or you'll get a fatal error like "SoapFault::__construct(): Invalid parameters. Invalid fault code."

The ones you generally want to use are "Sender" where you've gotten bad parameters, or "Receiver" where the server has gotten an error for some reason.

See here:

http://www.w3.org/TR/soap12-part1/#faultcodes


Obtain a string representation of a SoapFault

  • ≪ SoapFault::SoapFault
  • SoapHeader ≫
  • PHP Manual
  • SoapFault
  • Obtain a string representation of a SoapFault

SoapFault::__toString

(PHP 5 >= 5.0.1, PHP 7)

SoapFault::__toString — Obtain a string representation of a SoapFault

설명

public string SoapFault::__toString ( void )

Returns a string representation of the SoapFault.

인수

이 함수는 인수가 없습니다.

반환값

A string describing the SoapFault.

User Contributed Notes

There are no user contributed notes for this page.


The SoapHeader class

(PHP 5 >= 5.0.1, PHP 7)

소개

Represents a SOAP header.

클래스 개요

SoapHeader {
/* 메소드 */
__construct ( string $namespace , string $name [, mixed $data [, bool $mustunderstand [, string $actor ]]] )
SoapHeader ( string $namespace , string $name [, mixed $data [, bool $mustunderstand = false [, string $actor ]]] )
}

Table of Contents

  • SoapHeader::__construct — SoapHeader constructor
  • SoapHeader::SoapHeader — SoapHeader constructor

User Contributed Notes

abdul dot rashid at paytabs dot co 13-Mar-2016 07:06
Just to add some note regarding his john at jtresponse dot co dot uk

In PHP you can try following code to avoid the <item><key/>

$Auth = new stdClass();
$Auth->SystemId = "DATA";
$Auth->UserName = "USERNAME";
$Auth->Password = "PASSWORD";

$header = new SoapHeader('NAMESPACE','Auth',$Auth,false);
$soapClient->__setSoapHeaders($header);

  <SOAP-ENV:Header>
    <ns1:Auth>
      <ns1:SystemId>DATA</ns1:SystemId>
      <ns1:UserName>USERNAME</ns1:UserName>
      <ns1:Password>PASSWORD</ns1:Password>
    </ns1:Auth>
  </SOAP-ENV:Header>
voroks at logics dot net dot au 03-Jul-2015 09:43
Example by john at jtresponse dot co dot uk does miss one important point: to be able to add attributes they must be mentioned in WSDL. If they not exist in WSDL they WILL NOT appear as attributes but rather <item><key/><value/></item> elements.
john at jtresponse dot co dot uk 13-Mar-2012 10:30
None of the examples really do it for me.
Note: you should NOT need to hard-code any XML.

Here is an example of creating a nested header and including a parameter.

$client = new SoapClient(WSDL,array());

$auth = array(
        'UserName'=>'USERNAME',
        'Password'=>'PASSWORD',
        'SystemId'=> array('_'=>'DATA','Param'=>'PARAM'),
        );
  $header = new SoapHeader('NAMESPACE','Auth',$auth,false);
  $client->__setSoapHeaders($header);

Gives the following header XML:

  <SOAP-ENV:Header>
    <ns1:Auth>
      <ns1:SystemId Param="PARAM">DATA</ns1:SystemId>
      <ns1:UserName>USERNAME</ns1:UserName>
      <ns1:Password>PASSWORD</ns1:Password>
    </ns1:Auth>
  </SOAP-ENV:Header>


SoapHeader constructor

  • ≪ SoapHeader
  • SoapHeader::SoapHeader ≫
  • PHP Manual
  • SoapHeader
  • SoapHeader constructor

SoapHeader::__construct

(PHP 5 >= 5.0.1, PHP 7)

SoapHeader::__construct — SoapHeader constructor

설명

SoapHeader::__construct ( string $namespace , string $name [, mixed $data [, bool $mustunderstand [, string $actor ]]] )

이 함수는 다음 함수의 별칭입니다: SoapHeader::SoapHeader()

User Contributed Notes

There are no user contributed notes for this page.


SoapHeader constructor

  • ≪ SoapHeader::__construct
  • SoapParam ≫
  • PHP Manual
  • SoapHeader
  • SoapHeader constructor

SoapHeader::SoapHeader

(PHP 5 >= 5.0.1, PHP 7)

SoapHeader::SoapHeader — SoapHeader constructor

설명

SoapHeader::SoapHeader ( string $namespace , string $name [, mixed $data [, bool $mustunderstand = false [, string $actor ]]] )

Constructs a new SoapHeader object.

인수

namespace

The namespace of the SOAP header element.

name

The name of the SoapHeader object.

data

A SOAP header's content. It can be a PHP value or a SoapVar object.

mustUnderstand

Value of the mustUnderstand attribute of the SOAP header element.

actor

Value of the actor attribute of the SOAP header element.

예제

Example #1 SoapHeader::SoapHeader() example

<?php
$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));
$client->__soapCall("echoVoid", null, null,
                new SoapHeader('http://soapinterop.org/echoheader/',
                               'echoMeStringRequest',
                               'hello world'));
?>

참고

  • SoapClient::__soapCall() - Calls a SOAP function
  • SoapVar::SoapVar() - SoapVar constructor
  • SoapParam::SoapParam() - SoapParam constructor
  • SoapServer::addSoapHeader() - Add a SOAP header to the response

User Contributed Notes

yacahuma 28-Jan-2009 03:29
I was unable to connect to .net using any of the previous notes. I found out that the easiest way is the one that works. Everyone is trying to use soapvar . but you dont need to.

$ns = 'http://blabla.com/Services/Services';
$AuthHeader = new AuthHeader($LoginResponse);
$header =  new SoapHeader($ns,"AuthHeader",  $AuthHeader,false);
$this->soapClient->__setSoapHeaders(array($header));

my header was suppose to look like this
<soapenv:Header>
<ns1:AuthHeader>
<ns1:SessionKey>xxxx</ns1:SessionKey>
<ns1:SessionRole>xxxxx</ns1:SessionRole>
<ns1:UserType>xx</ns1:UserType>
<ns1:UserName>xxx</ns1:UserName>
</ns1:AuthHeader>
</soapenv:Header>

so my AuthHeader is just a simple class
class AuthHeader
{
  var $SessionKey;//string
  var $SessionRole;//string
  var $UserType;//string
  var $UserName;//string

  function __construct($LoginResponse)
  {
     $this->SessionKey  = $LoginResponse->LoginResult->SessionKey;
     $this->SessionRole = $LoginResponse->LoginResult->SessionRole;
     $this->UserType    = $LoginResponse->LoginResult->UserType;
     $this->UserName    = $LoginResponse->LoginResult->UserName;

  }
}

The important lesson here is the when you build your header you can use a normal class. It does not need to be a soapvar
aeolianmeson at dontfindme dot blitzeclipse dot com 07-May-2008 05:31
To do headers (the raw way):

    // Setting "trace" will allow us to view the request that we are making, after we have made it.
    $objClient = new SoapClient("http://www.somewhere.com/wsdls/some.wsdl", array('trace' => true));

    // These parameters satisfy this specific remote call.
    $arrParameters_Login = array('username' => 'username', 'password' => 'password', 'company' => 'Company');

    // Invoke the remote call "login()".
    $objLogin =  $objClient->login($arrParameters_Login);

    // Grab session ID that this remote call will provide.
    $strSessionID = $objLogin->loginReturn->sessionId;

    // Set headers-- The remote call "query()" will require a header pointing to our session.

    $strHeaderComponent_Session = "<SessionHeader><sessionId>$strSessionID</sessionId></SessionHeader>";

    $objVar_Session_Inside = new SoapVar($strHeaderComponent_Session, XSD_ANYXML, null, null, null);
    $objHeader_Session_Outside = new SoapHeader('namespace.com', 'SessionHeader', $objVar_Session_Inside);

    // More than one header can be provided in this array.
    $objClient->__setSoapHeaders(array($objHeader_Session_Outside));

    // Set the query parameters.

    $strQuery = 'select empID from Time where empID = 92389278';
    $arrParameters_Query = array('queryString' => $strQuery);

    // Make call.

    $objResponse = $objClient->query($arrParameters_Query);

    header('Content-Type: text/xml; ');
    print($objClient->__getLastRequest());

The request that goes out:

<SOAP-ENV:Envelope>
    <SOAP-ENV:Header>
        <SessionHeader>
            <sessionId>8789364113604871127</sessionId>
        </SessionHeader>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:query>
            <ns1:queryString>select empID from Time where empID = 92389278</ns1:queryString>
        </ns1:query>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
harlan at plauditdesign dot com 15-Mar-2006 08:04
Here is a way to get headers on the server side. It will fill up an assoc with all headers in a namespace. In its current state, it only handles text content; but each header is expanded to a DOMNode, so you can do whatever you wish to parse custom types.

In my case this method is useful because I want to pass PHPSESSID in a SOAP header, and I need to set it before I even call SoapServer::handle().

//
// READ SOAP HEADERS, STOP READING AT SOAPENV:BODY ELEMENT
//
$xml = new XmlReader();
$xml->XML( $HTTP_RAW_POST_DATA );

$shoppingCartHeaders = array();
while( $xml->read() ) {
    if( $xml->namespaceURI == "urn:com.plauditdesign.shoppingcart.client.headers#"
        && $xml->nodeType == XMLReader::ELEMENT ) {

        $headerNode = $xml->expand();
        $shoppingCartHeaders[ $xml->localName ] = $headerNode->textContent;
    } elseif( $xml->namespaceURI == "http://schemas.xmlsoap.org/soap/envelope/"
        && $xml->nodeType == XMLReader::ELEMENT
        && $xml->localName == "Body" ) {

        $xml->close();
    }
}

...

if( isset( $shoppingCartHeaders["sessionId"] ) ) {
    session_id( $shoppingCartHeaders["sessionId"] );
}

...

$server->setPersistence( SOAP_PERSISTENCE_SESSION );
$server->handle();
jared DOT kuolt at gmail dot com 28-Oct-2005 06:57
To build the authentication headers like below FOR WSDL:

**NOTE** I cannot find documentation on the __setSoapHeaders() method, though it does work in 5.0.4

<?php

class MySoapClass
{

    function __construct(){
        // Blah blah blah
        $this->soap = new SoapClient($this->foo, $this->bar);

    }

    // Build that header!
    private function build_auth_header(){
        // Build an object with parameters
        $auth->username = $this->username;
        $auth->password = $this->password;

        $authvalues = new SoapVar($auth, SOAP_ENC_OBJECT);
        $header =  new SoapHeader($this->name_space, "Authentication", // Rename this to the tag you need
                                                          $authvalues, false);

        $this->soap->__setSoapHeaders(array($header));

    }

    // Wrapper so we can build auth header first
    public function MySoapFunction($params){

        $this->build_auth_header();
        $this->soap->MySoapFunction($params);

    }

}
?>
mobi at delfnet dot pl 31-May-2005 09:24
In botoom code is bug.

is:

// create authentication header values
$authvalues=new SoapVar($auth,SOAP_ENC_OBJECT,'authenticate');

should be:

// create authentication header values
$authvalues=new SoapVar($auth,SOAP_ENC_OBJECT);
clewis at myfonts dot com 09-Feb-2005 05:52
If you are using WSDL to define your SOAP Headers, note that PHP's SoapServer class will not process incoming headers unless the <message> and <part> names are identical for the header methods.

Define a SOAP header function like this in WSDL:

<!-- replace "tns:" with your own namespace abbreviation -->

<!-- define method arguments using a complexType -->
<xsd:complexType name="HeaderMethodArgs">
    <xsd:all>
        <xsd:element ... name="arg1"/>
        <xsd:element ... name="arg2"/>
    </xsd:all>
</xsd:complexType>

<!-- define method message with single part -->
<message name='headerMethodName'>
    <part name='headerMethodName' type='tns:HeaderMethodArgs'/>
</message>

<!-- add header tag to operations that use this header -->
<operation name='someBodyMethod'>
    ...
    <input>
        <soap:body .../>
        <soap:header
            ...
            message='tns:headerMethodName'
            part='headerMethodName'
        />
    </input>
    <output>...</output>
</operation>


The SoapParam class

(PHP 5 >= 5.0.1, PHP 7)

소개

Represents parameter to a SOAP call.

클래스 개요

SoapParam {
/* 메소드 */
__construct ( mixed $data , string $name )
SoapParam ( mixed $data , string $name )
}

Table of Contents

  • SoapParam::__construct — SoapParam constructor
  • SoapParam::SoapParam — SoapParam constructor

User Contributed Notes

There are no user contributed notes for this page.


SoapParam constructor

  • ≪ SoapParam
  • SoapParam::SoapParam ≫
  • PHP Manual
  • SoapParam
  • SoapParam constructor

SoapParam::__construct

(PHP 5 >= 5.0.1, PHP 7)

SoapParam::__construct — SoapParam constructor

설명

SoapParam::__construct ( mixed $data , string $name )

이 함수는 다음 함수의 별칭입니다: SoapParam::SoapParam()

User Contributed Notes

There are no user contributed notes for this page.


SoapParam constructor

  • ≪ SoapParam::__construct
  • SoapVar ≫
  • PHP Manual
  • SoapParam
  • SoapParam constructor

SoapParam::SoapParam

(PHP 5 >= 5.0.1, PHP 7)

SoapParam::SoapParam — SoapParam constructor

설명

SoapParam::SoapParam ( mixed $data , string $name )

Constructs a new SoapParam object.

인수

data

The data to pass or return. This parameter can be passed directly as PHP value, but in this case it will be named as paramN and the SOAP service may not understand it.

name

The parameter name.

예제

Example #1 SoapParam::SoapParam() example

<?php
$client = new SoapClient(null,array('location' => "http://localhost/soap.php",
                                    'uri'      => "http://test-uri/"));
$client->SomeFunction(new SoapParam($a, "a"),
                      new SoapParam($b, "b"),
                      new SoapParam($c, "c"));
?>

참고

  • SoapClient::__soapCall() - Calls a SOAP function
  • SoapVar::SoapVar() - SoapVar constructor

User Contributed Notes

lyroe_chan at hotmail dot com 16-Jan-2014 09:33
If you want  to create a SOAP parameter like:

<a n="something">DATA</a>

You can try like this:

array('a' => array('_' => 'DATA', 'n'=>'something'));

This will generated xml like this:
<a n="something">DATA</a>
mjordan at sfu dot ca 15-Nov-2013 09:52
You can also pass parameters (at least simple ones) to SOAP functions like this:

<?php
  $return = $client->someFunction(array('paramName' => 'paramValue'));
?>
Alex 25-Feb-2010 02:20
You probably want to try SoapVar instead of SoapParam if you want to specify attributes/namespace.
Alex 25-Feb-2010 02:18
Make sure to always cast your parameters prior to creating a SoapParam.  Otherwise you will wind up with an incorrect xsi:type and possibly no value.

$value = 0;
$param0 = new SoapParam(
                $value, $param0_name);

will give you:

<$param0_name xsi:type="xsd:null"></$param0_name>

while:

$value = 0;
$param0 = new SoapParam(
                (int)$value, $param0_name);

<$param0_name xsi:type="xsd:int">0</$param0_name>

which is probably what you want.
barryking93 at gmail dot com 05-Dec-2007 11:42
You have to use SoapVar instead of SoapParam if you want it to do something fancy like using different opening and closing tags.  I ran into this using the SOAP API for Zimbra.
Jeremy 12-Jul-2007 05:31
Is there anyway to create a SOAP parameter like:

<a n="something">DATA</a>

If I try to form a param using the following code the resulting request is:

Code: SoapParam("DATA", "a n=\"something\"");
Result: <a n="something">DATA</a n="something">

This is giving me an error from the SOAP server because its expecting a properly formed closing tag without the encapsulated attribute.


The SoapVar class

(PHP 5 >= 5.0.1, PHP 7)

소개

A class representing a variable or object for use with SOAP services.

클래스 개요

SoapVar {
/* 메소드 */
__construct ( mixed $data , string $encoding [, string $type_name [, string $type_namespace [, string $node_name [, string $node_namespace ]]]] )
SoapVar ( mixed $data , string $encoding [, string $type_name [, string $type_namespace [, string $node_name [, string $node_namespace ]]]] )
}

Table of Contents

  • SoapVar::__construct — SoapVar constructor
  • SoapVar::SoapVar — SoapVar constructor

User Contributed Notes

seth dot johnson at gmail dot com 25-Feb-2015 09:09
It is not documented and thus may be subject to change but if you need to inspect the constructed SoapVar it sets everything you pass it on public variables:

<?php
$foo = new \stdClass();
$foosoap = new \SoapVar($foo, SOAP_ENC_OBJECT, 'Foo');
var_dump($foosoap);
echo $foosoap->enc_stype;
echo get_class($foosoap->enc_value);
?>

Will output (tested in PHP 5.3.3 cli):

object(SoapVar)#2 (3) {
  ["enc_type"]=>
  int(301)
  ["enc_value"]=>
  object(stdClass)#1 (0) {
  }
  ["enc_stype"]=>
  string(3) "Foo"
}

Foo
stdClass


SoapVar constructor

  • ≪ SoapVar
  • SoapVar::SoapVar ≫
  • PHP Manual
  • SoapVar
  • SoapVar constructor

SoapVar::__construct

(PHP 5 >= 5.0.1, PHP 7)

SoapVar::__construct — SoapVar constructor

설명

SoapVar::__construct ( mixed $data , string $encoding [, string $type_name [, string $type_namespace [, string $node_name [, string $node_namespace ]]]] )

이 함수는 다음 함수의 별칭입니다: SoapVar::SoapVar()

User Contributed Notes

There are no user contributed notes for this page.


SoapVar constructor

  • ≪ SoapVar::__construct
  • Yar ≫
  • PHP Manual
  • SoapVar
  • SoapVar constructor

SoapVar::SoapVar

(PHP 5 >= 5.0.1, PHP 7)

SoapVar::SoapVar — SoapVar constructor

설명

SoapVar::SoapVar ( mixed $data , string $encoding [, string $type_name [, string $type_namespace [, string $node_name [, string $node_namespace ]]]] )

Constructs a new SoapVar object.

인수

data

The data to pass or return.

encoding

The encoding ID, one of the XSD_... constants.

type_name

The type name.

type_namespace

The type namespace.

node_name

The XML node name.

node_namespace

The XML node namespace.

예제

Example #1 SoapVar::SoapVar() example

<?php
class SOAPStruct {
    function SOAPStruct($s, $i, $f)
    {
        $this->varString = $s;
        $this->varInt = $i;
        $this->varFloat = $f;
    }
}
$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));
$struct = new SOAPStruct('arg', 34, 325.325);
$soapstruct = new SoapVar($struct, SOAP_ENC_OBJECT, "SOAPStruct", "http://soapinterop.org/xsd");
$client->echoStruct(new SoapParam($soapstruct, "inputStruct"));
?>

참고

  • SoapClient::__soapCall() - Calls a SOAP function
  • SoapParam::SoapParam() - SoapParam constructor

User Contributed Notes

Francesco Nardone 26-Mar-2014 10:50
I spent hours trying to find how to send a request where an element is repeated. Here is how I managed to do it:
<?php
$parm = array();
$parm[] = new SoapVar('123', XSD_STRING, null, null, 'customerNo' );
$parm[] = new SoapVar('THIS', XSD_STRING, null, null, 'selection' );
$parm[] = new SoapVar('THAT', XSD_STRING, null, null, 'selection' );
$resp = $client->getStuff( new SoapVar($parm, SOAP_ENC_OBJECT) );
?>

This will send something like:
<getStuff>
  <customerNo>123</customerNo>
  <selection>THIS</selection>
  <selection>THAT</selection>
</getStuff>

Hope this will save someone else's time.
it at teamamericany dot com 20-Jul-2013 06:13
To send requests containing ComplexTypes we needed to create stdClass objects for each ComplexType. If the ComplexType has an attribute we created a class with all the fields and attributes as members and added this class to the classmap with the correct type name from the wsdl. For example:

 //to create soap client
            $soapClient = new SoapClient("<your wsdl url>",
                 array('classmap' => array('RoomType' => 'RoomType'), 'trace' => 1));

             //creating request object using custom class since there is an attribute needed

    class RoomType {
        //attribute
        var $code;
        var $name;
    }
            $RoomType = new RoomType();
            $RoomType->code = $code;

                        //plain stdClass is ok since no attributes defined
            $LRoomCode = new stdClass();
            $LRoomCode->RoomType = new SoapVar($RoomType, SOAP_ENC_OBJECT, "RoomType", "<your namespace url from wsdl>");

            //define a basic class object since attributes are not needed for this complex type
             $RateAvailabilityReq = new stdClass();
             $RateAvailabilityReq->LRoomCode = new SoapVar($LRoomCode, SOAP_ENC_OBJECT, "ArrayOfRoomType", "<your namespace url>");
            $RateAvailabilityReq->idB2BHotel = $idB2BHotel;
             $RateAvailabilityReq->idProduct = $idProduct;
             $RateAvailabilityReq->lenght = $lenght;
            $RateAvailabilityReq->nameDistributor = $nameDistributor;
            $RateAvailabilityReq->password = $password;
            $RateAvailabilityReq->propertyCode = $propertyCode;
             $RateAvailabilityReq->startDate = $startDate;
             $RateAvailabilityReq->userName = $userName;
             $RateUpdateReqType = new SoapVar($RateAvailabilityReq , SOAP_ENC_OBJECT, "RateAvailabilityReq", "<your namespace url>");

             $soapRequestType = new stdClass();
             $soapRequestType->in0 = $RateUpdateReqType;
             $getRateAvailabilityReq = new SoapVar($soapRequestType , SOAP_ENC_OBJECT, "getRateAvailability", "<your namespace url>");

             $result = $soapClient->getRateAvailability($getRateAvailabilityReq);

Reading attributes from ComplexType response objects also required a custom class added to the classmap array with the attribute as a member of the class.
Joni Lindqvist 25-Oct-2012 05:49
I worked for hours to figure out how to create following structure:

<ns:Foo attr='bar'>
   <ns:Baz attr='foobar'>barbaz</ns:Baz>
</ns:Foo>

It can be done with following array structure:

$arr = array(
  'Foo' => array(
     'attr'=>'bar',
     'Baz'=>array( '_' => 'barbaz', 'attr'=>'foobar')
  )
);
tcell 01-Mar-2012 03:53
One potential gotcha with this is that if the XML you are passing in has the <?xml... declaration, it may cause problems on the server side. It's best to strip this out with str_replace.
Krzych 03-Dec-2010 03:54
It might be obvious to some but not to everyone
if you ever wondered how to encode attribute in soap object, use proper coding and not hacking by passing xml and flag XSD_ANYXML

try using this
<?php
 $amount['_'] = 25;
 $amount['currencyId'] = 'GBP';
 $encodded = new SoapVar($amount, SOAP_ENC_OBJECT);

?>
and end result wound be
<amount currencyId="GBP">25</amount>

hope that helps someone
cb at wasteland dot org 02-Jul-2009 11:01
If you need to add attributes XML entities in your SOAP query like this:

"<FilterBy Column="Id" FilterOperator="=" FilterValue="NUMBER" Table="Case">"

Use SoapVar() with the type XSD_ANYXML as shown below.  Example uses __soapCall and other SoapParams.

<?php
$response = $client->__soapCall('GetFilteredRecordList',
    array(new SoapParam($DSToken, 'Token'),
          new SoapParam('Slide', 'TableName'),
          new SoapVar('<FilterBy Column="Id" FilterOperator="=" FilterValue="NUMBER" Table="Case"/>', XSD_ANYXML)),
    array('soapaction' => 'http://www.example.com/webservices/GetFilteredRecordList'));
?>

Use this so you don't waste HOURS looking around:
OrionI 11-Jul-2005 04:30
This class is useful when dealing with the "anyType" type (generic object reference): it lets you specify the xsd type to provide "late binding" type information.

Here's a really simple example: I have a .NET service that can take a string, a date, and integer, or other types, so I use the .NET "object" type. Here's an example of such a service--this one just tells me what type I passed in. (It's nice to use when checking to see if PHP passed in the type information the way .NET expects it.)

//inside a service.asmx.cs file...
[WebMethod]
public string WhatTypeIsThis(object ObjectParameter)
{
   return "You passed in a " + ObjectParameter.GetType().Name
     + ": " + ObjectParamter.ToString();
}

To call this service with a string from PHP, I used this code:
<?php
//set up the service client using WSDL
$client = new SoapClient("http://localhost/folder/service.asmx?WSDL");

//This is the variable that will be typed as an XSD string
$typedVar = new SoapVar("mystring", XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema");
//This is the wrapper object for incoming parameters
$wrapper->ObjectParameter = $typedVar;
//This is the named parameter object that will be passed in to the service
$params = new SoapParam($wrapper, "WhatTypeIsThis");

//call the service with the string
$result = $client->WhatTypeIsThis($params);
//show the result
echo $result->WhatTypeIsThisResult;
?>

The output from this should be:
"You passed in a String: mystring"

The SOAP message that is passed in looks like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://tempuri.org/">
    <SOAP-ENV:Body>
        <ns1:WhatTypeIsThis>
            <ns1:ObjectParameter xsi:type="xsd:string">mystring</ns1:ObjectParameter>
        </ns1:WhatTypeIsThis>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

It's that xsi:type="xsd:string" that gives .NET the heads up that though the ObjectParameter is being passed in as an object, it is also a string.

When trying other types, it's helpful to see exactly what is being sent to the service--to see the SOAP messages like the one above, use the trace option when making your SOAP client and then call the $client->__getLastRequest() function. (See http://www.php.net/soap_soapclient_getlastrequest)  You may need to use try/catch constructs if you're generating errors.

Another helpful function is var_dump($client->__getTypes())--it shows how PHP parsed the WSDL file to create types to pass back and forth.


Yet Another RPC Framework

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
  • Yar_Server — The Yar_Server class
    • Yar_Server::__construct — Register a server
    • Yar_Server::handle — Start RPC Server
  • Yar_Client — The Yar_Client class
    • Yar_Client::__call — Call service
    • Yar_Client::__construct — Create a client
    • Yar_Client::setOpt — Set calling contexts
  • Yar_Concurrent_Client — The Yar_Concurrent_Client class
    • Yar_Concurrent_Client::call — Register a concurrent call
    • Yar_Concurrent_Client::loop — Send all calls
    • Yar_Concurrent_Client::reset — Clean all registered calls
  • Yar_Server_Exception — The Yar_Server_Exception class
    • Yar_Server_Exception::getType — The getType purpose
  • Yar_Client_Exception — The Yar_Client_Exception class
    • Yar_Client_Exception::getType — The getType purpose

User Contributed Notes

There are no user contributed notes for this page.


소개

Yar is a RPC framework which aims to provide a simple and easy way to do communication between PHP applications It has the ability to concurrently call multiple remote services.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

if you want to use msgpack as packager, you need compile Yar by your self with ./configure --enable-msgpack

User Contributed Notes

There are no user contributed notes for this page.


설치

이 » PECL 확장은 PHP 배포판에서 제공하지 않습니다.

PECL 확장 설치에 관한 정보는 매뉴얼의 PECL 확장 설치에 있습니다. 새 릴리즈, 내려받기, 소스파일, 개발자 정보, CHANGELOG 등의 추가 정보는 여기에 있습니다: » http://pecl.php.net/package/yar.

이 PECL 확장에 대한 DLL은 현재 사용할 수 없습니다. 윈도우에서 빌드하기 섹션을 참고하십시오.

User Contributed Notes

liubinn_208391 at qq dot com 08-Dec-2013 02:35
Yar Concurrent Client doesn't work on Mac OS.  It is because the cURL shipped with Mac has some difference.

you can resolve that by install a new cURL, like (brew install curl).

then use :
"export DYLD_LIBRARY_PATH=/path-to-new-curl/lib:$DYLD_LIBRARY_PATH"

make the PHP use the new cURL.

of course you can also re-compile your PHP with --with-curl=/path-to-new-curl/


실행시 설정

이 함수의 작동은 php.ini 설정에 영향을 받습니다.

Yar 설정 옵션
이름 | 기본값 | 가변성 | 변경점
yar.packager | php | PHP_INI_SYSTEM |
yar.debug | Off | PHP_INI_ALL |
yar.connect_timeout | 1000 | PHP_INI_ALL |
yar.timeout | 5000 | PHP_INI_ALL |
yar.expose_info | On | PHP_INI_SYS |

위 설정 지시어에 대한 간단한 설명입니다.

yar.packager string

it could be php, json, and msgpack(require built with msgpack support)

yar.debug string

yar.connect_timeout integer

timeout in ms

yar.timeout integer

timeout in ms

yar.expose_info bool

whether expose the service info(when access the server via GET)

User Contributed Notes

There are no user contributed notes for this page.


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

YAR_VERSION (string)
YAR_CLIENT_PROTOCOL_HTTP (integer)
YAR_CLIENT_OPT_PACKAGER (integer)
YAR_CLIENT_OPT_TIMEOUT (integer)
YAR_CLIENT_OPT_CONNECT_TIMEOUT (integer)
YAR_PACKAGER_PHP (string)
YAR_PACKAGER_JSON (string)
YAR_ERR_OKEY (integer)
YAR_ERR_OUTPUT (integer)
YAR_ERR_TRANSPORT (integer)
YAR_ERR_REQUEST (integer)
YAR_ERR_PROTOCOL (integer)
YAR_ERR_PACKAGER (integer)
YAR_ERR_EXCEPTION (integer)

User Contributed Notes

There are no user contributed notes for this page.


예제

Example #1 Yar Server Example

<?php

/* assume this page can be accessed by http://example.com/operator.php */

class Operator {

    /**
     * Add two operands
     * @param interge
     * @return interge
     */
    public function add($a, $b) {
        return $this->_add($a, $b);
    }

    /**
     * Sub
     */
    public function sub($a, $b) {
        return $a - $b;
    }

    /**
     * Mul
     */
    public function mul($a, $b) {
        return $a * $b;
    }

    /**
     * Protected methods will not be exposed
     * @param interge
     * @return interge
     */
    protected function _add($a, $b) {
        return $a + $b;
    }
}

$server = new Yar_Server(new Operator());
$server->handle();
?>

Example #2 Access the server in borwser(GET request)

위 예제의 출력 예시:

Yar Server Info

Example #3 Yar Client Example

<?php
$client = new yar_client("http://example.com/operator.php");

/* call directly */
var_dump($client->add(1, 2));

/* call via call */
var_dump($client->call("add", array(3, 2)));

/* __add can not be called */
var_dump($client->_add(1, 2));
?>

위 예제의 출력 예시:

int(3)
int(5)
PHP Fatal error:  Uncaught exception 'Yar_Server_Exception' with message 'call to api Operator::_add() failed' in *

Example #4 Yar Concurrent Client Example

<?php
function callback($ret, $callinfo) {
    echo $callinfo['method'] , " result: ", $ret , "\n";
}

/* register async call to remote services */
Yar_Concurrent_Client::call("http://example.com/operator.php", "add", array(1, 2), "callback");
Yar_Concurrent_Client::call("http://example.com/operator.php", "sub", array(2, 1), "callback");
Yar_Concurrent_Client::call("http://example.com/operator.php", "mul", array(2, 2), "callback");

/* sent all request and wait for response */
Yar_Concurrent_Client::loop();
?>

위 예제의 출력 예시:

mul result: 4
sub result: 1
add result: 3

User Contributed Notes

124960772 at qq dot com 10-Nov-2015 08:03
<?php
function callback($ret, $callinfo) {
    echo $callinfo['method'] , " result: ", $ret , "\n";
}

/* 注?一??步?用 */
Yar_Concurrent_Client::call("http://example.com/operator.php", "add", array(1, 2), "callback");
/* ?送所有注?的?用, 等待返回, 返回后Yar??用callback回掉函? */
Yar_Concurrent_Client::loop();
/* 重置call ,否?上面的call??用*/
Yar_Concurrent_Client::reset();
Yar_Concurrent_Client::call("http://example.com/operator.php", "sub", array(2, 1), "callback");
Yar_Concurrent_Client::loop();

?>


The Yar_Server class

(No version information available, might only be in Git)

소개

클래스 개요

Yar_Server {
/* 프로퍼티 */
protected $_executor ;
/* 메소드 */
final public __construct ( Object $obj )
public boolean handle ( void )
}

프로퍼티

_executor

Table of Contents

  • Yar_Server::__construct — Register a server
  • Yar_Server::handle — Start RPC Server

User Contributed Notes

There are no user contributed notes for this page.


Register a server

  • ≪ Yar_Server
  • Yar_Server::handle ≫
  • PHP Manual
  • Yar_Server
  • Register a server

Yar_Server::__construct

(PECL yar >= 1.0.0)

Yar_Server::__construct — Register a server

설명

final public Yar_Server::__construct ( Object $obj )

Set up a Yar HTTP RPC Server, All the public methods of $obj will be register as a RPC service.

인수

obj

An Object, all public methods of its will be registered as RPC services.

반환값

An instance of Yar_Server.

예제

Example #1 Yar_Server::__construct() example

<?php
class API {
    /**
     * the doc info will be generated automatically into service info page.
     * @params
     * @return
     */
    public function some_method($parameter, $option = "foo") {
         return "some_method";
    }

    protected function client_can_not_see() {
    }
}

$service = new Yar_Server(new API());
$service->handle();
?>

위 예제의 출력 예시:

참고

  • Yar_Server::handle() - Start RPC Server
    •

User Contributed Notes

There are no user contributed notes for this page.


Start RPC Server

  • ≪ Yar_Server::__construct
  • Yar_Client ≫
  • PHP Manual
  • Yar_Server
  • Start RPC Server

Yar_Server::handle

(PECL yar >= 1.0.0)

Yar_Server::handle — Start RPC Server

설명

public boolean Yar_Server::handle ( void )

Start a RPC HTTP server, and ready for accpet RPC requests.

Note:

Usual RPC calls will be issued as HTTP POST requests. If a HTTP GET request is issued to the uri, the service information (commented section above) will be printed on the page

인수

이 함수는 인수가 없습니다.

반환값

boolean

예제

Example #1 Yar_Server::handle() example

<?php
class API {
    /**
     * the doc info will be generated automatically into service info page.
     * @params
     * @return
     */
    public function some_method($parameter, $option = "foo") {
    }

    protected function client_can_not_see() {
    }
}

$service = new Yar_Server(new API());
$service->handle();
?>

위 예제의 출력 예시:

참고

  • Yar_Server::__construct() - Register a server
    •

User Contributed Notes

There are no user contributed notes for this page.


The Yar_Client class

(No version information available, might only be in Git)

소개

클래스 개요

Yar_Client {
/* 프로퍼티 */
protected $_protocol ;
protected $_uri ;
protected $_options ;
protected $_running ;
/* 메소드 */
public void __call ( string $method , array $parameters )
final public __construct ( string $url )
public boolean setOpt ( number $name , mixed $value )
}

프로퍼티

_protocol

_uri

_options

_running

Table of Contents

  • Yar_Client::__call — Call service
  • Yar_Client::__construct — Create a client
  • Yar_Client::setOpt — Set calling contexts

User Contributed Notes

porschegt23 at foxmail dot com 05-Jul-2016 08:07
A simple example on here:

server.php
<?php
class API {

    public function api($parameter = "", $option = "foo") {
          return $this->client_can_not_see($parameter);
    }

    public function doAdd($a = 0, $b = 0) {
          return $a+$b;
    }

    protected function client_can_not_see( $name ) {
      return "?好$name~";
    }
}

$service = new Yar_Server ( new API () );
$service->handle ();
?>
client.php
<?php
  $client = new Yar_Client("http://host/server.php");
  $result = $client->api("parameter");
  echo $result.'<hr>';
  echo $client->doAdd(10, 20);
?>


Call service

  • ≪ Yar_Client
  • Yar_Client::__construct ≫
  • PHP Manual
  • Yar_Client
  • Call service

Yar_Client::__call

(PECL yar >= 1.0.0)

Yar_Client::__call — Call service

설명

public void Yar_Client::__call ( string $method , array $parameters )

Issue a call to remote RPC method.

인수

method

Remote RPC method name.

parameters

Parameters.

반환값

예제

Example #1 Yar_Client::__call() example

<?php

$client = new Yar_Client("http://host/api/");

/* call remote service */
$result = $client->some_method("parameter");
?>

위 예제의 출력 예시:

참고

  • Yar_Client::setOpt() - Set calling contexts
    •

User Contributed Notes

There are no user contributed notes for this page.


Create a client

  • ≪ Yar_Client::__call
  • Yar_Client::setOpt ≫
  • PHP Manual
  • Yar_Client
  • Create a client

Yar_Client::__construct

(PECL yar >= 1.0.0)

Yar_Client::__construct — Create a client

설명

final public Yar_Client::__construct ( string $url )

Create a Yar_Client to a Yar_Server.

인수

url

Yar Server URL.

반환값

Yar_Client instance.

예제

Example #1 Yar_Client::__construct() example

<?php
$client = new Yar_Client("http://host/api/");
?>

위 예제의 출력 예시:

참고

  • Yar_Client::__call() - Call service
    •

  • Yar_Client::setOpt() - Set calling contexts
    •

User Contributed Notes

There are no user contributed notes for this page.


Set calling contexts

  • ≪ Yar_Client::__construct
  • Yar_Concurrent_Client ≫
  • PHP Manual
  • Yar_Client
  • Set calling contexts

Yar_Client::setOpt

(PECL yar >= 1.0.0)

Yar_Client::setOpt — Set calling contexts

설명

public boolean Yar_Client::setOpt ( number $name , mixed $value )

인수

name

it can be: YAR_OPT_PACKAGER, YAR_OPT_PERSISTENT (Need server support), YAR_OPT_TIMEOUT, YAR_OPT_CONNECT_TIMEOUT

value

반환값

예제

Example #1 Yar_Client::setOpt() example

<?php

$cient = new Yar_Client("http://host/api/");

//Set timeout to 1s
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, 1000);

//Set packager to JSON
$client->SetOpt(YAR_OPT_PACKAGER, "json");

/* call remote service */
$result = $client->some_method("parameter");
?>

위 예제의 출력 예시:

참고

  • Yar_Client::__call() - Call service
    •

User Contributed Notes

There are no user contributed notes for this page.


The Yar_Concurrent_Client class

(No version information available, might only be in Git)

소개

클래스 개요

Yar_Concurrent_Client {
/* 프로퍼티 */
static $_callstack ;
static $_callback ;
static $_error_callback ;
/* 메소드 */
public static int call ( string $uri , string $method , array $parameters [, callable $callback ] )
public static boolean loop ([ callable $callback [, callable $error_callback ]] )
public static boolean reset ( void )
}

프로퍼티

_callstack

_callback

_error_callback

Table of Contents

  • Yar_Concurrent_Client::call — Register a concurrent call
  • Yar_Concurrent_Client::loop — Send all calls
  • Yar_Concurrent_Client::reset — Clean all registered calls

User Contributed Notes

There are no user contributed notes for this page.


Register a concurrent call

  • ≪ Yar_Concurrent_Client
  • Yar_Concurrent_Client::loop ≫
  • PHP Manual
  • Yar_Concurrent_Client
  • Register a concurrent call

Yar_Concurrent_Client::call

(PECL yar >= 1.0.0)

Yar_Concurrent_Client::call — Register a concurrent call

설명

public static int Yar_Concurrent_Client::call ( string $uri , string $method , array $parameters [, callable $callback ] )

Register a RPC call, but won't sent it immediately, it will be send while further call to Yar_Concurrent_Client::loop()

인수

uri

The RPC server URI(http, tcp)

method

Service name(aka the method name)

parameters

Parameters

callback

A function callback, which will be called while the response return.

반환값

An unique id, can be used to identified which call it is.

예제

Example #1 Yar_Concurrent_Client::call() example

<?php
function callback($retval, $callinfo) {
     var_dump($retval);
}

function error_callback($type, $error, $callinfo) {
    error_log($error);
}

Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback");
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"));   // if the callback is not specificed,
                                                                               // callback in loop will be used
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", NULL, array(YAR_OPT_PACKAGER => "json"));
                                                                               //this server accept json packager
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", NULL, array(YAR_OPT_TIMEOUT=>1));
                                                                               //custom timeout

//The requests are not sent yet
?>

위 예제의 출력 예시:

참고

  • Yar_Concurrent_Client::loop() - Send all calls
    •

  • Yar_Concurrent_Client::reset() - Clean all registered calls
    •

  • Yar_Server::__construct() - Register a server
    •

  • Yar_Server::handle() - Start RPC Server
    •

User Contributed Notes

There are no user contributed notes for this page.


Send all calls

  • ≪ Yar_Concurrent_Client::call
  • Yar_Concurrent_Client::reset ≫
  • PHP Manual
  • Yar_Concurrent_Client
  • Send all calls

Yar_Concurrent_Client::loop

(PECL yar >= 1.0.0)

Yar_Concurrent_Client::loop — Send all calls

설명

public static boolean Yar_Concurrent_Client::loop ([ callable $callback [, callable $error_callback ]] )

Send all registed remote RPC calls.

인수

callback

If this callback is set, then Yar will call this callback after all calls are sent and before any response return, with a $callinfo NULL.

Then, if user didn't specify callback when registering concurrent call, this callback will be used to handle response, otherwise, the callback specified while registering will be used.

error_callback

If this callback is set, then Yar will call this callback while error occurred.

반환값

예제

Example #1 Yar_Concurrent_Client::loop() example

<?php
function callback($retval, $callinfo) {
     if ($callinfo == NULL) {
        echo "Now, all requests are sent, and no any response available\n";
     } else {
        echo "This is a remote call response, the method name is", $callinfo["method"],
             ". calling sequence is " , $callinfo["sequence"] , "\n";
        var_dump($retval);
     }
}

function error_callback($type, $error, $callinfo) {
    error_log($error);
}

Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback");
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"));   // if the callback is not specificed,
                                                                               // callback in loop will be used
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", NULL, array(YAR_OPT_PACKAGER => "json"));
                                                                               //this server accept json packager
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", NULL, array(YAR_OPT_TIMEOUT=>1));
                                                                               //custom timeout

Yar_Concurrent_Client::loop("callback", "error_callback"); //send the requests,
                                                           //the error_callback is optional
?>

위 예제의 출력 예시:

Now, all requests are sent, and no any response available
This is a remote call response, the method name issome_method. calling sequence is 4
string(11) "some_method"
This is a remote call response, the method name issome_method. calling sequence is 1
string(11) "some_method"
This is a remote call response, the method name issome_method. calling sequence is 2
string(11) "some_method"
This is a remote call response, the method name issome_method. calling sequence is 3
string(11) "some_method"

참고

  • Yar_Concurrent_Client::call() - Register a concurrent call
  • Yar_Concurrent_Client::reset() - Clean all registered calls
  • Yar_Server::__construct() - Register a server
  • Yar_Server::handle() - Start RPC Server

User Contributed Notes

There are no user contributed notes for this page.


Clean all registered calls

  • ≪ Yar_Concurrent_Client::loop
  • Yar_Server_Exception ≫
  • PHP Manual
  • Yar_Concurrent_Client
  • Clean all registered calls

Yar_Concurrent_Client::reset

(No version information available, might only be in Git)

Yar_Concurrent_Client::reset — Clean all registered calls

설명

public static boolean Yar_Concurrent_Client::reset ( void )

Clean all registered calls

인수

반환값

예제

Example #1 Yar_Concurrent_Client::reset() example

위 예제의 출력 예시:

참고

  • Yar_Concurrent_Client::call() - Register a concurrent call
    •

  • Yar_Concurrent_Client::loop() - Send all calls
    •

  • Yar_Server::__construct() - Register a server
    •

  • Yar_Server::handle() - Start RPC Server
    •

User Contributed Notes

There are no user contributed notes for this page.


The Yar_Server_Exception class

(No version information available, might only be in Git)

소개

If service threw exceptions, A Yar_Server_Exception will be threw in client side.

클래스 개요

Yar_Server_Exception extends Exception {
/* 프로퍼티 */
protected $_type ;
/* 메소드 */
public string getType ( void )
/* 상속된 메소드 */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}

프로퍼티

message

code

file

line

_type

Table of Contents

  • Yar_Server_Exception::getType — The getType purpose

User Contributed Notes

There are no user contributed notes for this page.


The getType purpose

  • ≪ Yar_Server_Exception
  • Yar_Client_Exception ≫
  • PHP Manual
  • Yar_Server_Exception
  • The getType purpose

Yar_Server_Exception::getType

(PECL yar >= 1.0.0)

Yar_Server_Exception::getType — The getType purpose

설명

public string Yar_Server_Exception::getType ( void )

Get the exception original type threw by server

인수

이 함수는 인수가 없습니다.

반환값

string

예제

Example #1 Yar_Server_Exception::getType() example

//Server.php
<?php
class Custom_Exception extends Exception {};

class API {
    public function throw_exception($name) {
        throw new Custom_Exception($name);
    }
}

$service = new Yar_Server(new API());
$service->handle();
?>

//Client.php
<?php
$client = new Yar_Client("http://host/api.php");

try {
    $client->throw_exception("client");
} catch (Yar_Server_Exception $e) {
    var_dump($e->getType());
    var_dump($e->getMessage());
}

위 예제의 출력 예시:

string(16) "Custom_Exception"
string(6) "client"

참고

User Contributed Notes

There are no user contributed notes for this page.


The Yar_Client_Exception class

(No version information available, might only be in Git)

소개

클래스 개요

Yar_Client_Exception extends Exception {
/* 프로퍼티 */
/* 메소드 */
public void getType ( void )
/* 상속된 메소드 */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}

프로퍼티

message

code

file

line

Table of Contents

  • Yar_Client_Exception::getType — The getType purpose

User Contributed Notes

There are no user contributed notes for this page.


The getType purpose

  • ≪ Yar_Client_Exception
  • XML-RPC ≫
  • PHP Manual
  • Yar_Client_Exception
  • The getType purpose

Yar_Client_Exception::getType

(PECL yar >= 1.0.0)

Yar_Client_Exception::getType — The getType purpose

설명

public void Yar_Client_Exception::getType ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

예제

Example #1 Yar_Client_Exception::getType() example

<?php

/* ... */

?>

위 예제의 출력 예시:

...

참고

User Contributed Notes

There are no user contributed notes for this page.


XML-RPC

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • XML-RPC 함수 목록
    • xmlrpc_decode_request — Decodes XML into native PHP types
    • xmlrpc_decode — Decodes XML into native PHP types
    • xmlrpc_encode_request — Generates XML for a method request
    • xmlrpc_encode — Generates XML for a PHP value
    • xmlrpc_get_type — Gets xmlrpc type for a PHP value
    • xmlrpc_is_fault — Determines if an array value represents an XMLRPC fault
    • xmlrpc_parse_method_descriptions — Decodes XML into a list of method descriptions
    • xmlrpc_server_add_introspection_data — Adds introspection documentation
    • xmlrpc_server_call_method — Parses XML requests and call methods
    • xmlrpc_server_create — Creates an xmlrpc server
    • xmlrpc_server_destroy — Destroys server resources
    • xmlrpc_server_register_introspection_callback — Register a PHP function to generate documentation
    • xmlrpc_server_register_method — Register a PHP function to resource method matching method_name
    • xmlrpc_set_type — Sets xmlrpc type, base64 or datetime, for a PHP string value

User Contributed Notes

There are no user contributed notes for this page.


소개

These functions can be used to write XML-RPC servers and clients. You can find more information about XML-RPC at » http://www.xmlrpc.com/, and more documentation on this extension and its functions at » http://xmlrpc-epi.sourceforge.net/.

Warning

이 확장 모듈은 실험적입니다. 이 확장 모듈의 작동(함수의 이름과 이 확장 모듈에 관련된 모든 문서 자료)은 이후의 PHP 릴리즈에서 예고 없이 변경될 수 있습니다. 이 확장 모듈의 사용은 사용자의 책임하에 이루어집니다.

User Contributed Notes

There are no user contributed notes for this page.


설치

XML-RPC support in PHP is not enabled by default. You will need to use the --with-xmlrpc[=DIR] configuration option when compiling PHP to enable XML-RPC support. This extension is bundled into PHP as of 4.1.0.

User Contributed Notes

evsninja 17-Oct-2014 05:59
The easiest way to install XML-RPC in CentOS (migth not work on CentOS 7):
yum install php-xmlrpc
service httpd restart

Ready to go!
renzvinci at gmail dot com 08-Nov-2013 01:18
Note: On OSX uncomment the following line in php.ini :

;extension=php_xmlrpc.dll
David Simon 09-Jan-2013 04:17
On Ubuntu 12.10, try sudo apt-get install php5-xmlrpc
SalmanAbbas007 13-Mar-2011 02:53
Note: On Windows you just have to uncomment the following line in php.ini :-

;extension=php_xmlrpc.dll


자원형

This extension defines a XML-RPC server resource returned by xmlrpc_server_create().

User Contributed Notes

There are no user contributed notes for this page.


XML-RPC 함수 목록

Table of Contents

  • xmlrpc_decode_request — Decodes XML into native PHP types
  • xmlrpc_decode — Decodes XML into native PHP types
  • xmlrpc_encode_request — Generates XML for a method request
  • xmlrpc_encode — Generates XML for a PHP value
  • xmlrpc_get_type — Gets xmlrpc type for a PHP value
  • xmlrpc_is_fault — Determines if an array value represents an XMLRPC fault
  • xmlrpc_parse_method_descriptions — Decodes XML into a list of method descriptions
  • xmlrpc_server_add_introspection_data — Adds introspection documentation
  • xmlrpc_server_call_method — Parses XML requests and call methods
  • xmlrpc_server_create — Creates an xmlrpc server
  • xmlrpc_server_destroy — Destroys server resources
  • xmlrpc_server_register_introspection_callback — Register a PHP function to generate documentation
  • xmlrpc_server_register_method — Register a PHP function to resource method matching method_name
  • xmlrpc_set_type — Sets xmlrpc type, base64 or datetime, for a PHP string value

User Contributed Notes

digibrisk at gmail dot NOSPAM dot SPAMNO dot com 05-Oct-2007 12:24
Adding to what giunta dot gaetano at sea-aeroportimilano dot it and astrolox at lawyersonline dot co dot uk said about the Sourceforge PHP XML-RPC project: You can probably use function_exists() to determine whether the extension is installed so you don't have to incur performance costs. If it's not installed, then the function won't exist, and function_exists() returns false. You can then fall back on the alternative library if that's the case. For example:

<?php

if(!function_exists("xmlrpc_server_create")) {
    // include necessary files.
}

?>
handco at gmail dot com 20-Feb-2007 05:19
Hi,

A little snippet to implement OO XML RPC Server.

File RPCPlugin.php :

class RPCPlugins {

    private $plugins;

    function __construct ($pathname, $rpcServer) {
        $d = dir($pathname);
        while (($file = $d->read()) !== false) {
            if (ereg('(.*)\.php$', $file, $regs)) {
                include_once ($pathname . '/' . $file);
                $class=$regs[1];
                $this->plugins = new $class($rpcServer);
            }
        }
    }

}

class RPCPlugin {

    private $_rpcServer;

    function __construct($rpcServer) {
        $this->_rpcServer = $rpcServer;

        $methods = get_class_methods($this);

        foreach ($methods as $method) {
            if (substr($method, 0,1) != '_') {
                xmlrpc_server_register_method($rpcServer,get_class($this) . "." . $method,array(&$this,$method));
            }
        }

    }

}

An example of plugin in plugins/Test.php  :

 class Test extends RPCPlugin {

     function HelloWorld ($method, $params) {
         return "Hello World --->>" . $params[0];
     }

 }

Now the real server :

require_once 'RPCPlugin.php';

 $rpcServer = xmlrpc_server_create();

$plugins = new RPCPlugins(realpath('plugins/'), $rpcServer);

 $resp = xmlrpc_server_call_method($rpcServer,$HTTP_RAW_POST_DATA,null);
 if ($resp) {
     header ('Content-Type: text/xml');
     echo $resp;
 }
 xmlrpc_server_destroy($rpcServer)

Then you can call ethod by classname.method.
For this sample Test.HelloWorld

Hn'Co
giunta dot gaetano at sea-aeroportimilano dot it 13-Sep-2006 03:40
If you need to use this extension but are stuck on a server where it can not be installed, the php-xmlrpc library found at http://phpxmlrpc.sourceforge.net includes an emulation layer that aims to be 100% compatible with the API of the native extension (as part of the "extras" package since version 0.2).
This means your code should be able to run unmodified on top of the php-xmlrpc library. Of course performance will be at least an order of magnitude worse...
keithNO dot SPAMthornhill at gmail dot com 17-May-2006 11:16
for others attempting the same thing, here is what a function would look like if you wanted to send a base64 encoded file from a client and then save it onto the server. the other code necessary to call this function via an RPC is available in other comments so i won't repeat it.

parameters:
1 - name of file
2 - base64 encoded data of file

note the use of $file_data->scalar

<?
    function sendFile($method_name, $params, $user_data) {
        $file = "/somedir/" . $params[0];
        $file_data = $params[1];

        $fh = @fopen($file, "wb");
        if ($fh) {
            if (@fwrite($fh, $file_data->scalar)) {
                $msg = "success msg";
            } else {
                $msg = "couldn't write to file";
            }
            fclose($fh);
            return $msg;
        } else {
            return "couldn't open file";
        }
    }
?>
09-Mar-2006 09:47
This XML-RPC Service makes the use XML-RPC very esay.

<?php

/**
* function myfun() returns
*@return array
*/
function myfunc(){
    return $some_array;
}

$ws = new XML_RPC_Server();
$ws->registerFunction('myfunc');
$ws->run();

?>

http://www.pure-php.de/node/31

It creates also a simple docu.
http://www.pure-php.com/php/service.php?doc
john # curioussymbols com 19-Oct-2005 01:37
I couldn't make the 'xmlrpc_errors' php.ini setting do anything
noticeable (PHP 4.3.11), so I used the following code to report errors
from my XMLRPC server. Hope it's helpful for someone.

<?php
function return_xmlrpc_error($errno,$errstr,$errfile=NULL,$errline=NULL
       ,$errcontext=NULL){
    global $xmlrpc_server;
    if(!$xmlrpc_server)die("Error: $errstr in '$errfile', line '$errline'");

    header("Content-type: text/xml; CHARSET=ks_c_5601-1987");
    print(xmlrpc_encode(array(
        'faultCode'=>$errno
        ,'faultString'=>"Remote XMLRPC Error from
          ".$_SERVER['HTTP_HOST'].": $errstr in at $errfile:$errline"
    )));
    die();
}
set_error_handler('return_xmlrpc_error');
?>

In my server function, I just trigger_error("message",E_USER_ERROR)]
 if something can't be completed.

Then on the client side,
<?php
        $data =& xmlrpc_decode($response['body']);

        if(xmlrpc_is_fault($data)){
            trigger_error($data['faultString'],E_USER_ERROR);
        }
?>
martin dot rode at programmfabrik dot de 04-Aug-2005 02:28
To connect to a python xmlrpc server I use:

function do_call($host, $port, $request) {

    $url = "http://$host:$port/";
    $header[] = "Content-type: text/xml";
    $header[] = "Content-length: ".strlen($request);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        print curl_error($ch);
    } else {
        curl_close($ch);
        return $data;
    }
}

$request = xmlrpc_encode_request('add', array(3, 4));
$response = do_call($host, $port, $request);
astrolox at lawyersonline dot co dot uk 22-Mar-2005 01:20
The PHP XML-RPC project at SourceForge makes life a hell of a lot easier. However, the project uses some function names which are identical to thoses provided by the XML-RPC extention.

If you are on a server with XML-RPC extension compiled in but wish to use the PHP based version then you will have to rename some of the functions.

I notice that sourceforce says there is activity on the project in 2005 but the last release was January 12, 2003.

I recommend that you use this not so friendly PHP extention if available. However this sourceforce project is still a good idea if you don't control which extenions are be available on the server.

http://phpxmlrpc.sourceforge.net/
Jerome Delamarche 14-Oct-2004 11:58
The documentation lacks an example that shows how to send a fault in a response. Here is how to do it:

$args = array("faultCode" => $errcode, "faultString" => $errmsg);
$resp = xmlrpc_encode_request(NULL,$args);
//echo $resp;
php at hendrik-krauss dot de 13-Aug-2004 09:38
On "datetime" values:

If you implement an XML-RPC server with these functions and a client calls a method on your server, sending a datetime as parameter (in ISO 8601 format, as specified at http://www.xmlrpc.com/spec), the PHP XML-RPC will pass your registered server method an object as parameter. That object, for example, looks like:
obj->type="datetime"
obj->scalar="20040420T13:32:40"
obj->timestamp=1082460760

If you do xmlrpc_get_type(obj), it will return "datetime", so presumably that function just returns the value of 'type'. 'scalar' seems to be the on-the-wire representation of the datetime (ISO 8601, exactly as received). 'timestamp' appears to be the ISO value in 'scalar' converted into a normal PHP timestamp (i.e. Unix time_t).

Note on 'scalar': Using a MySQL DB, we did something like  "select blah where start_time >= $obj->scalar ;". That actually worked and returned expected results, so MySQL appears to handle that ISO 8601 format correctly.
swunderlin at REMOVE-telemedia dot ch 09-Jun-2004 12:20
pear hs an XML_RPC package, if you can't recompile your php:
http://pear.php.net/package/XML_RPC
steph at zend dot com 08-Jun-2004 01:04
It took me a while to get a client together without external libraries.  This very basic client/server pair works on my home set-up - hopefully it will save the next xml-rpc virgin some grief.

/* clienttest.php */
<?php
function do_call($host, $port, $request) {

    $fp = fsockopen($host, $port, $errno, $errstr);
    $query = "POST /home/servertest.php HTTP/1.0\nUser_Agent: My Egg Client\nHost: ".$host."\nContent-Type: text/xml\nContent-Length: ".strlen($request)."\n\n".$request."\n";

    if (!fputs($fp, $query, strlen($query))) {
        $errstr = "Write error";
        return 0;
    }

    $contents = '';
    while (!feof($fp)) {
        $contents .= fgets($fp);
    }

    fclose($fp);
    return $contents;
}

$host = 'localhost';
$port = 80;
$request = xmlrpc_encode_request('cycle', 'egg');
$response = do_call($host, $port, $request);
/* do something with $response, e.g. print it */
?>

/* servertest.php */
<?php
function lifecycle($method, $params) {
/* $method = 'cycle', $params = (array of) request parameter(s); $data is also passed from xmlrpc_server_call_method, if we had any data to pass */
    switch($params[0]) {
        case 'egg':
            $reply = 'All eggs will be birds one day.';
        break;
        default:
            $reply = 'That must have been an otheregg';
    }
    return $reply;
}

$server = xmlrpc_server_create();

/* register the 'external' name and then the 'internal' name */
xmlrpc_server_register_method($server, "cycle", "lifecycle");

$request = $HTTP_RAW_POST_DATA; // no you don't need 'always on', and no $_POST doesn't work.

/* the parameters here are 'server, xml-string and user data'.  There's supposed to be an optional 'output options' array too, but I can't get it working :( hence header() call */
$response = xmlrpc_server_call_method($server, $request, null);
header('Content-Type: text/xml');
print $response;

xmlrpc_server_destroy($server);
?>
mboeren at php dot net 25-Feb-2004 01:34
Just a quick addition to my previous xmlrpc_client class: since you cannot use remote methods containing capital letters or methods from subhandlers (like 'system.listMethods()'), I added a 'call(...)' method to the class.

<?php
    // this method should be copy/pasted in the
    // xmlrpc_client class

    function call($function)
    {
        $return = NULL;
        $argv = func_get_args();
        array_shift($argv); // remove function argument
        $this->__call($function, $argv, &$return);
        return $return;
    }

    // now, you can also do
    $result = $client->call('system.listMethods');
    $sum = client->call('add', '1', '2');
?>
mboeren at php dot net 24-Feb-2004 04:01
I use the following code (requires the overload extension) to make developing clients easier:

<?php
include("utils/utils.php"); // from xmlrpc-epi utils

/*
Usage:
    $client = new xmlrpc_client("http://localhost:7080");
    print $client->echo('x')."\n";
    print $client->add(1, 3)."\n";

*/
class xmlrpc_client
{
    var $url;
    var $urlparts;

    function xmlrpc_client($url)
    {
        $this->url = $url;
        $this->urlparts = parse_url($this->url);
        foreach(array('scheme', 'host', 'user', 'pass', 'path',
                      'query', 'fragment')
                as $part) {
            if (!isset($this->urlparts[$part])) {
                $this->urlparts[$part] = NULL;
                }
            }
    }

    function __call($function, $arguments, &$return)
    {
        $requestprms['host'] = $this->urlparts['host'];
        $requestprms['port'] = $this->urlparts['port'];
        $requestprms['uri'] = $this->urlparts['path'];
        $requestprms['method'] = $function;
        $requestprms['args'] = $arguments;
        $requestprms['debug'] = 0;
        $requestprms['timeout'] = 0;
        $requestprms['user'] = NULL;
        $requestprms['pass'] = NULL;
        $requestprms['secure'] = 0;

        $result = xu_rpc_http_concise($requestprms);
        if (is_array($result) && isset($result['faultCode'])) {
            print('Error in xmlrpc call \''.$function.'\''."\n");
            print('  code   : '.$result['faultCode']."\n");
            print('  message: '.$result['faultString']."\n");
            return false;
            }
        $return = $result;
        return true;
    }

}
overload('xmlrpc_client');

?>
bmichael at goldparrot dot com 09-Feb-2003 06:52
If anyone is interested in making XMLRPC requests directly from the client, I have been able to get xmlrpc to
work with vcXMLRPC javascript backend.

After about 1 week of scanning the market, I found this solution to be the best on Javascript back end.  It uses the Microsoft.HTTP activeX control for IE, or HTTPRequest Object for Mozilla.

You include vc(Virtual Cowboys) vcXMLRPC.js file into your pages and make the rpc calls from with javascript to create the requests.

It works both ways.

Two Notes:

I have tested it on IE 6.02 and you need to change lines in ProcessRequest :
function to read:

  dom = this.getObject("XMLDOM",http.responseText);

and change the getObject function to use the latest ActiveX Control:

 MSXML2.XMLHTTP.3.0  (or 4.0)
 MSXML2.DOMDocument.3.0  (or 4.0)

The controls are found on MSDN in the Web Services -> XML area.

As another note, you DO NOT NEED the rpcproxy.cgi script to use this.  That is a proxy script to get around JS Security.  You can use PHP to build the proxy.  But, I was able to get the CGI working with GCC compiler on Solaris (change the -KPCI, depend and -x03 optimizer settings in the Makefile )
steve at orangeNOSPAMimagineering dot com 24-Aug-2002 10:32
There's a handy library by Keith Devens (version 2.2.1) at
http://www.keithdevens.com/software/xmlrpc/

Here is a sample client. It remotely calls sample.sumAndDifference
with two parameters (3 and 5).
It returns:

sum => 8
difference => -2

<?php
include ("kd_xmlrpc.php");
// define("XMLRPC_DEBUG", 0);    // Set to 1 for handy debugging

$method = "sample.sumAndDifference";
$params = XMLRPC_prepare(array(3,5));

$site = "xmlrpc-c.sourceforge.net";
$location = "/api/sample.php";

list($success, $result) = XMLRPC_request( $site, $location, $method, $params );

// XMLRPC_debug_print();    // uncomment for debugging

foreach ( $result as $key => $value ) {
          echo(" $key => $value \n");
}

?>
hfuecks at pinkgoblin dot com 15-Aug-2002 05:32
This extension does not handle the process of making making XML-RPC client requests via HTTP; it only prepares the XML-RPC request payload.

This differs from many other XML-RPC implementations but offers greater flexibility, allowing SSL connections, authentication headers and XML-RPC via other transports like SMTP.
ivanr at webkreator dot com 21-Jun-2002 08:50
For a really easy way to use this XML-RPC extension take a look at

XML-RPC Class Server (http://www.webkreator.com/php/xcs/)

It automatically creates servers out of PHP classes. Creating clients is almost as easy, especially with the recent addition of the overload extension to PHP (see http://www.php.net/manual/en/ref.overload.php).
nic at uklinux dot NOSPAM dot net 24-Apr-2002 06:05
An alternative XML-RPC implementation is available at http://xmlrpc.usefulinc.com - it's written in PHP so you can use it on servers for which you don't have the luxury of rebuilding PHP on.

nic


Decodes XML into native PHP types

  • ≪ XML-RPC 함수 목록
  • xmlrpc_decode ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Decodes XML into native PHP types

xmlrpc_decode_request

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_decode_request — Decodes XML into native PHP types

설명

mixed xmlrpc_decode_request ( string $xml , string &$method [, string $encoding ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

User Contributed Notes

e dot sand at elisand dot com 22-Jun-2009 05:20
This function works exactly the same as xmlrpc_decode() except it gives you access to the method being called as well (xmlrpc_decode() only outputs the params from the XML-RPC call).

To use this function, simply create an empty variable to store the method string in, and then pass by reference like so:

<?php
$method = null;
$params = xmlrpc_decode_request('<xml rpc request>', &$method);
?>

What you will get back are the params stored in $params, and $method will have the method being requested.


Decodes XML into native PHP types

  • ≪ xmlrpc_decode_request
  • xmlrpc_encode_request ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Decodes XML into native PHP types

xmlrpc_decode

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_decode — Decodes XML into native PHP types

설명

mixed xmlrpc_decode ( string $xml [, string $encoding = "iso-8859-1" ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

인수

xml

XML response returned by XMLRPC method.

encoding

Input encoding supported by iconv.

반환값

Returns either an array, or an integer, or a string, or a boolean according to the response returned by the XMLRPC method.

예제

See example by xmlrpc_encode_request().

참고

  • xmlrpc_encode_request() - Generates XML for a method request
  • xmlrpc_is_fault() - Determines if an array value represents an XMLRPC fault

User Contributed Notes

Alwin 18-May-2016 11:24
Note that from libxml 2.7.9+ there is a limit of 10MB for the XML-RPC response.

If the response is larger, xmlrpc_decode will simply return NULL.

There is currently no way to override this limit like we can with the other xml functions (LIBXML_PARSEHUGE)
carmageddon at gmail dot com 03-Jun-2013 04:42
Apparently there is a slight problem with xmlrpc_decode (or php) which re-formats this input: <value><double>0.000000</double></value>

As the double number 0.

To get around it, use: number_format($val, 2);
Output would be 0.00
phil dot berry at elise-international dot net 06-Sep-2011 05:09
Make sure the server isn't returning a string with a space for the first character, this fails in version 5.3.3 and the function returns null (though seems to be ok in 5.2).

Easily sorted by  trimming the response data:

<?php xmlrpc_decode( trim($response) ); ?>
ryon dot sherman at gmail dot com 22-Aug-2009 01:18
64 bit (i8) integers are not parsed by xmlrpc_decode().
Use a string replacement to work around this:

<?php

$xml = str_replace('i8>', 'i4>', $xml);

$decoded_xml = xmlrpc_decode($xml);

?>
david dot bachelart at polytechnique dot org 18-Jul-2004 05:18
Be careful with encodings, the xmlrpc-decode function is rather strict. For example, the following response parse returns NULL :

<?xml version="1.0"?>
<methodResponse>
   <params>
      <param>
         <value><string>a & b</string></value>
         </param>
      </params>
   </methodResponse>

You should use entities :
<?xml version="1.0"?>
<methodResponse>
   <params>
      <param>
         <value><string>a &amp; b</string></value>
         </param>
      </params>
   </methodResponse>

If your server does not encode responses properly, you may have to process responses before parse.
hfuecks at pinkgoblin dot com 16-Aug-2002 12:57
Use this with an XML-RPC client to decode a server response into native PHP variables. It will automatically translate the response XML-RPC data types into their PHP equivalents.

This function will return only false is there is any problem with format of the XML it receives.

The HTTP response header will need to be stripped off with something like;

<?php
$xml=(substr($response, strpos($response, "\r\n\r\n")+4));

$phpvars = xmlrpc_decode ($xml);
?>


Generates XML for a method request

  • ≪ xmlrpc_decode
  • xmlrpc_encode ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Generates XML for a method request

xmlrpc_encode_request

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_encode_request — Generates XML for a method request

설명

string xmlrpc_encode_request ( string $method , mixed $params [, array $output_options ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

인수

method

Name of the method to call.

params

Method parameters compatible with method signature.

output_options

Array specifying output options may contain (default values are emphasised):

  • output_type: php, xml

  • verbosity: no_white_space, newlines_only, pretty

  • escaping: cdata, non-ascii, non-print, markup (may be a string with one value or an array with multiple values)

  • version: simple, xmlrpc, soap 1.1, auto

  • encoding: iso-8859-1, other character set supported by iconv

반환값

Returns a string containing the XML representation of the request.

예제

Example #1 XMLRPC client functions example

<?php
$request = xmlrpc_encode_request("method", array(1, 2, 3));
$context = stream_context_create(array('http' => array(
    'method' => "POST",
    'header' => "Content-Type: text/xml",
    'content' => $request
)));
$file = file_get_contents("http://www.example.com/xmlrpc", false, $context);
$response = xmlrpc_decode($file);
if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
    print_r($response);
}
?>

참고

  • stream_context_create() - Creates a stream context
  • file_get_contents() - Reads entire file into a string
  • xmlrpc_decode() - Decodes XML into native PHP types

User Contributed Notes

Anonymous 27-Aug-2009 09:33
ever tried transmitting an array like the following with xmlrpc?
$var1=array(7=>14,9=>18);

The output array looks quite different! It will look like that:
$var2=array(14,18);

The only solution i found is to prepend a space to the index:
$var3=array(' 7'=>14,' 9'=>18);

Using that method you'll get the right result. ($var1)
cometfish at hotmail dot com 01-Sep-2008 09:47
The example above is incorrect - the header needs to be an array, see post by "chris dot vigelius at gmx dot net": http://au.php.net/manual/en/function.stream-context-create.php#74431
His post also shows how to do browser authentication, as below:
<?php
   $request = xmlrpc_encode_request("methodName", array("methodParam"));
   $auth = base64_encode($username.":".$password);
   $header = (version_compare(phpversion(), '5.2.8'))
     ? array("Content-Type: text/xml","Authorization: Basic $auth")
     : "Content-Type: text/xml\r\nAuthorization: Basic $auth" ; //[1]
   $context = stream_context_create(array('http' => array(
      'method' => "POST",
      'header' => $header,
      'content' => $request
   )));
   $webservice="http://www.example.com/rpc";
   $file = file_get_contents($webservice, false, $context);
   $response = xmlrpc_decode($file);
   if (xmlrpc_is_fault($response)) {
      return "xmlrpc: $response[faultString] ($response[faultCode])";
   } else {
      return $response;
   }
?>

1 - EDITOR NOTE: THIS IS A FIX FROM "SandersWang dt php at gmail dot com"
fredrik at it dot cdon dot com 06-Feb-2008 02:05
It should be noted that encoding does not seem to encode anything, just specify what goes into the XML header.

We had problems with double-encoded UTF strings being saved to database when using this function, sending it of to a apache xml-rpc servlet and storing it in mysql database. It was solved by setting 'escaping' to just 'markup' and 'encoding' to 'UTF-8' (don't forget to set 'utf-8' in xmlrpc_decode too).

It seems that UTF-8 encoded strings gets escaped with their bytes as entities instead of their characters as entites.
handco at gmail dot com 12-Mar-2007 05:39
Simple OO client with function Overload :

the php metho test_helloworld is translated to xmlrpc method test.helloworld.

class RpcClient {

    private $_methods;
    private $_context;
    private $_url;

    function __construct ($url, $user, $passwd) {
        $auth = base64_encode(sprintf('%s:%s', $user,$passwd));
        $this->_context = stream_context_create(array(
            'http' => array(
                'method' => 'POST',
                'header' => "Content-Type: text/xml\r\n".
                            "Authorization: Basic $auth" ,

            )
        ));
        $this->_url = $url;

        $this->registerMethod ("Test_HelloWorld");

    }

    function __call($methodName, $params) {
        if (array_key_exists($methodName,$this->_methods)) {
            // on appelle la fonction RPC
            $m = str_replace('_', '.', $methodName);
            $r = xmlrpc_encode_request($m, $params,array('verbosity'=>'newlines_only'));
            $c = $this->_context;
            stream_context_set_option($c,'http','content',$r);
            $f = file_get_contents($this->_url,false,$c);
            $resp = xmlrpc_decode($f);
            return $resp;
        } else {
            // on appelle la fonction de l'objet
            call_user_method_array($methodName, $this,$params);
        }
    }

    private function registerMethod ($method) {
        $this->_methods[$method] = true;
    }

}
giunta dot gaetano at sea-aeroportimilano dot it 21-Aug-2006 12:54
Take care that this function will generate invalid xmlrpc content when invoked with certain parameters (said content will be happily parsed by the lib itself, but not by other implementations).

xmlrpc_encode_request(null, null)
will generate a response without a value

xmlrpc_encode_request('myfunc', array('faultCode' => 666, 'faultString' => 'hello world')
will generated a request containing a <fault> member instead of <params>
php at hendrik dash krauss dot de 28-May-2005 11:29
For examples / documentation of the array output_options, see http://xmlrpc-epi.sourceforge.net/main.php?t=php_api#output_options

In short, output_options lets you send compact xmlrpc (without all the "pretty whitespace" xmlrpc_encode adds normally), apply an own escaping table prior to sending, set the encoding, and a couple of other things (the page even says something about soap 1.1 ... I don't know details).
<URL:http://www.dlitz.net/go/contact/> 04-May-2004 08:28
Note that as far as I can tell, the &#10; characters generated by PHP in the base64 fields don't appear to violate the XML-RPC standard at all.  XML-RPC messages *are* in XML format, and as such, the XML entities should be getting decoded before being passed to a base64 decoder.  So, the previously-mentioned Jakarta-based XML-RPC server appears to violate the XML spec.  i.e. There's nothing here that needs to be "fixed" in PHP.
kelly at seankelly dot biz 29-Dec-2002 01:02
Binary strings (set with xmlrpc_set_type) go into a <base64>...</base64> block like you'd expect. But after every 80th character, this function inserts the XML entity "&#10;", which is a Unicode newline, as if to cause a line-wrap, which is admittedly silly.

Silly though it may be, it causes real problems for some XML-RPC servers, such as http://jakarta.apache.org/xmlrpc/ (nee Helma). Stripping out those entities with something like

$req = preg_replace('/&#10;/', '', xmlrpc_encode_request("my.method", $args));

works around the problem.
hfuecks at pinkgoblin dot com 15-Aug-2002 06:06
This function should be used by an XML-RPC client to create an XML payload for an XML-RPC request;

<?php
$params = "system.methodSignature";
$method = "system.methodHelp";
$request = xmlrpc_encode_request($method,$params);
echo ( $request );
?>

Produces;

<?xml version='1.0' encoding="iso-8859-1" ?>
<methodCall>
<methodName>system.methodHelp</methodName>
<params>
 <param>
  <value>
   <string>system.methodSignature</string>
  </value>
 </param>
</params>
</methodCall>

The second argument recognises the type of variable and generates the correct XML-RPC structure. See xmlrpc_encode() for more details.


Generates XML for a PHP value

  • ≪ xmlrpc_encode_request
  • xmlrpc_get_type ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Generates XML for a PHP value

xmlrpc_encode

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_encode — Generates XML for a PHP value

설명

string xmlrpc_encode ( mixed $value )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

User Contributed Notes

remi 13-May-2009 10:56
This xmlrpc_encode function doesn't generate the "methodResponse" tag required by some clients.

Using xmlrpc_encode_request seems better:

<?php
$xmlresponse = xmlrpc_encode_request(NULL, $phpresponse);

xmlrpc_encode(1) generate
<?xml version="1.0" encoding="utf-8"?>
<params>
<param>
 <value>
  <int>1</int>
 </value>
</param>
</params>

xmlrpc_encode_request(NULL,1) generate
<?xml version="1.0" encoding="iso-8859-1"?>
<methodResponse>
<params>
 <param>
  <value>
   <int>1</int>
  </value>
 </param>
</params>
</methodResponse>
?>
dave dot wilcock at gmail dot com 21-Sep-2007 04:05
Worth noting, you can use this function to generate fault response xml.

If you pass a pre-determined array into this function, as follows

<?
$myArray = array ("faultCode"=>1,"faultString"=>"Too many params");
$xml = xmlrpc_encode($myArray);
?>

... you will get output much like the following:

<?xml version="1.0" encoding="utf-8"?>
<fault>
<value>
 <struct>
  <member>
   <name>faultCode</name>
   <value>
    <int>4</int>
   </value>
  </member>
  <member>
   <name>faultString</name>
   <value>
    <string>Too many params</string>
   </value>
  </member>
 </struct>
</value>
</fault>

All that is needed to do here to make it valid XML-RPC is inject some of your own methodResponse tags.

Check the XML-RPC specification at http://www.xmlrpc.com/spec
08-Jun-2006 04:32
Another quirk (more of a bug i think) is the automatic encoding of hashes where all keys that begin with a digit between 1 and 9 are lost:
http://bugs.php.net/bug.php?id=37746

Luckily I found a quick workaround. Just append chr(0x00) to your keys and the xmlrpc response will be correct. Maybe this will apply to encoding of hashes for requests too.
Amir.LaherATcomplinetDOTcom 26-Aug-2005 10:54
Beware this quirk: when you xmlrpc_encode a hash into xmlrpc, numeric keys will not be kept. Even strings containing numbers-only will be lost.

i.e.: xmlrpc_decode(xmlrpc_encode(array('123'=>456)));
returns: array(0=>456);

Apparently this is intentional functionality (see http://bugs.php.net/bug.php?id=21949)

2 workarounds:
1. prepend/append a string (even whitespace) to all keys, and strip them out at the other end. eg array('key_123'=>456);
2. create an array of hashes. array(array('key'=>123,'value'=>456));

I favour the second option because it's more deliberate.

... this also applies to xmlrpc_encode_request()
hfuecks at pinkgoblin dot com 15-Aug-2002 05:16
This function would be used by an XML-RPC server to convert PHP variables into an XML-RPC response.

It would be used within the PHP functions defined using xmlrpc_server_register_method()

Uses PHP variable reflection to assign the correct data types for the XML-RPC response. For example;

<?php
$params = array ( "one"=>"red","two"=>"blue","three"=>"green" );

$response = xmlrpc_encode ( $params );

echo ( $response );
?>

Produces;

<?xml version='1.0' encoding="utf-8" ?>
<params>
<param>
 <value>
  <struct>
   <member>
    <name>one</name>
    <value>
     <string>red</string>
    </value>
   </member>
   <member>
    <name>two</name>
    <value>
     <string>blue</string>
    </value>
   </member>
   <member>
    <name>three</name>
    <value>
     <string>green</string>
    </value>
   </member>
  </struct>
 </value>
</param>
</params>

While
<?php
$params = array ( "red", "blue", "green" );

$response = xmlrpc_encode ( $params );

echo ( $response );
?>

produces;

<?xml version='1.0' encoding="utf-8" ?>
<params>
<param>
 <value>
  <array>
   <data>
    <value>
     <string>red</string>
    </value>
    <value>
     <string>blue</string>
    </value>
    <value>
     <string>green</string>
    </value>
   </data>
  </array>
 </value>
</param>
</params>

And

<?php
$params = 1;

$response = xmlrpc_encode ( $params );

echo ( $response );
?>

produces

<?xml version='1.0' encoding="utf-8" ?>
<params>
<param>
 <value>
  <int>1</int>
 </value>
</param>
</params>

It's useful to be aware of the settype() function (http://www.php.net/settype).

For binary base64 data and XML-RPC iso8601 date times the xmlrpc_set_type() function should be used on a PHP variable before using xmlrpc_encode()


Gets xmlrpc type for a PHP value

  • ≪ xmlrpc_encode
  • xmlrpc_is_fault ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Gets xmlrpc type for a PHP value

xmlrpc_get_type

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_get_type — Gets xmlrpc type for a PHP value

설명

string xmlrpc_get_type ( mixed $value )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

This function is especially useful for base64 and datetime strings.

인수

value

PHP value

반환값

Returns the XML-RPC type.

예제

Example #1 XML-RPC type example

<?php
echo xmlrpc_get_type(null) . "\n"; // base64
echo xmlrpc_get_type(false) . "\n"; // boolean
echo xmlrpc_get_type(1) . "\n"; // int
echo xmlrpc_get_type(1.0) . "\n"; // double
echo xmlrpc_get_type("") . "\n"; // string
echo xmlrpc_get_type(array()) . "\n"; // array
echo xmlrpc_get_type(new stdClass) . "\n"; // array
echo xmlrpc_get_type(STDIN) . "\n"; // int
?>

참고

  • xmlrpc_set_type() - Sets xmlrpc type, base64 or datetime, for a PHP string value

User Contributed Notes

hfuecks at pinkgoblin dot com 16-Aug-2002 07:41
This function returns the type of a PHP variable in XML-RPC terms. It won't "spot" base64 or iso8601 types unless they have been defined using xmlrpc_set_type().

For iso8601 type it returns "datetime".

Otherwise it returns strings corresponding directly the XML-RPC data types e.g. "struct","int","string","base64" etc.


Determines if an array value represents an XMLRPC fault

  • ≪ xmlrpc_get_type
  • xmlrpc_parse_method_descriptions ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Determines if an array value represents an XMLRPC fault

xmlrpc_is_fault

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

xmlrpc_is_fault — Determines if an array value represents an XMLRPC fault

설명

bool xmlrpc_is_fault ( array $arg )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

인수

arg

Array returned by xmlrpc_decode().

반환값

Returns TRUE if the argument means fault, FALSE otherwise. Fault description is available in $arg["faultString"], fault code is in $arg["faultCode"].

예제

See example by xmlrpc_encode_request().

참고

  • xmlrpc_decode() - Decodes XML into native PHP types

User Contributed Notes

angelo at at dot com 25-Jan-2011 09:31
A note, response from xmlrpc_decode is not always an array. Whenever the XMLRPC server returns a string, xmlrpc_is_fault will complain about not being an array.

Best way to detect errors is

<?php

    $response = xmlrpc_decode($file);

    if (is_array($response) && xmlrpc_is_fault($response)) {
        throw new Exception($response['faultString'], $response['faultCode']);
    }

?>


Decodes XML into a list of method descriptions

  • ≪ xmlrpc_is_fault
  • xmlrpc_server_add_introspection_data ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Decodes XML into a list of method descriptions

xmlrpc_parse_method_descriptions

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_parse_method_descriptions — Decodes XML into a list of method descriptions

설명

array xmlrpc_parse_method_descriptions ( string $xml )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

User Contributed Notes

There are no user contributed notes for this page.


Adds introspection documentation

  • ≪ xmlrpc_parse_method_descriptions
  • xmlrpc_server_call_method ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Adds introspection documentation

xmlrpc_server_add_introspection_data

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_server_add_introspection_data — Adds introspection documentation

설명

int xmlrpc_server_add_introspection_data ( resource $server , array $desc )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

User Contributed Notes

There are no user contributed notes for this page.


Parses XML requests and call methods

  • ≪ xmlrpc_server_add_introspection_data
  • xmlrpc_server_create ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Parses XML requests and call methods

xmlrpc_server_call_method

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_server_call_method — Parses XML requests and call methods

설명

string xmlrpc_server_call_method ( resource $server , string $xml , mixed $user_data [, array $output_options ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

User Contributed Notes

marco.buratto at tiscali punto it 28-Dec-2006 05:15
xmlrpc_server_call_method() with class methods

<?php
require_once ('Connections/adodb_mysql_connection.php');

// Instantiating my own class
$my_report = new external_report($db_connection);

// Setting up the XML-RPC "server"
$xmlrpc_server_handler = xmlrpc_server_create();
xmlrpc_server_register_method($xmlrpc_server_handler, "external_method", array(&$my_report, "export"));

// Creating XML return data
if ($response = xmlrpc_server_call_method($xmlrpc_server_handler, $HTTP_RAW_POST_DATA, null))
    {
    header('Content-Type: text/xml');
    echo $response;
    }

// **************** class definition ****************

class external_report
    {
    protected $db_connection;

    public function __construct($db_connection_pointer)
        {
        if (method_exists($db_connection_pointer, "Execute")) $this->db_connection = $db_connection_pointer;
        else die("...");
        }

    public function export($method_name, $params_array)
        {
        $id_dir = (int)$params_array[0];
        $id_usr = (int)$params_array[1]; // not used, just an example
        // We have to add arguments' validating code here and NOT inside the constructor (as usual)
        // because arguments are passed directly by xmlrpc_server_call_method (?!!)

        $myexport = array();

        $dirs_query = "SELECT documento_id FROM tabella_cartelle WHERE cartella_id = ".$id_dir;
        $dirs_result = $this->db_connection->Execute($dirs_query) or die("...");

        $index = 0;
        while(!$dirs_result->EOF)
            {
            $docs_query = "SELECT codice, titolo FROM tabella_documenti WHERE id_documento = ".$dirs_result->Fields('documento_id');
            $docs_result = $this->db_connection->Execute($docs_query) or die("...");

            $myexport[$index]['codice'] = $docs_result->Fields('codice');
            $myexport[$index]['titolo'] = $docs_result->Fields('titolo');

            $index++;
            $dirs_result->MoveNext();
            }

        return $myexport;
        }
    }
?>
nyvsld at gmail dot com 27-Nov-2005 02:08
<?php
/* method implementation */
function impl($method_name,$params,$user_data){
  var_dump(func_get_args('impl'));
  return array_sum($params);
}

/* create server */
$s=xmlrpc_server_create();
xmlrpc_server_register_method($s,'add','impl');

/* calling server method */
$req=xmlrpc_encode_request('add',array(1,2,3));
$resp=xmlrpc_server_call_method($s,$req,array(3,4));

/* process result */
$decoded=xmlrpc_decode($resp);
if(xmlrpc_is_fault($decoded)){
    echo 'fault!';
}

var_dump($decoded);
?>


Creates an xmlrpc server

  • ≪ xmlrpc_server_call_method
  • xmlrpc_server_destroy ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Creates an xmlrpc server

xmlrpc_server_create

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_server_create — Creates an xmlrpc server

설명

resource xmlrpc_server_create ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

User Contributed Notes

There are no user contributed notes for this page.


Destroys server resources

  • ≪ xmlrpc_server_create
  • xmlrpc_server_register_introspection_callback ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Destroys server resources

xmlrpc_server_destroy

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_server_destroy — Destroys server resources

설명

int xmlrpc_server_destroy ( resource $server )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

User Contributed Notes

nospam at bigbadweb dot co dot uk 22-Nov-2005 11:26
This function returns true (1) if successful and false (0) if resource is invalid or could not be destroyed.


Register a PHP function to generate documentation

  • ≪ xmlrpc_server_destroy
  • xmlrpc_server_register_method ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Register a PHP function to generate documentation

xmlrpc_server_register_introspection_callback

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_server_register_introspection_callback — Register a PHP function to generate documentation

설명

bool xmlrpc_server_register_introspection_callback ( resource $server , string $function )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

User Contributed Notes

There are no user contributed notes for this page.


Register a PHP function to resource method matching method_name

  • ≪ xmlrpc_server_register_introspection_callback
  • xmlrpc_set_type ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Register a PHP function to resource method matching method_name

xmlrpc_server_register_method

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_server_register_method — Register a PHP function to resource method matching method_name

설명

bool xmlrpc_server_register_method ( resource $server , string $method_name , string $function )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

User Contributed Notes

giunta dot gaetano at sea-aeroportimilano dot it 11-Aug-2006 03:55
To have an xmlrpc fault response programatically generated by the server, the php function registered as method handler must return an array containing a FaultCode and a FaultString members.

function $myfunc($methodname, $vals, $extra_data)
{
...
return array('faultCode' => 666, 'faultString' => 'DOH!');
}
nyvsld at gmail dot com 27-Nov-2005 02:03
prototype of registered function:

function method_impl(string $method_name, array $params, array $user_data);

$method_name
    the public method name, known by calling client
$params
    parameters specified by calling client
$user_data
    any local data, passed by `xmlrpc_server_call_method'
dante at lorenso dot com 11-Aug-2005 09:46
To register a callback to a 'static' function within the same class, consider a syntax like the following:
<code>
$callback = array (__CLASS__, "my_function_name");
xmlrpc_server_register_method($xmlrpc_server, "my_function", $callback);
</code>
Doing it this way makes it easier to rename your class later.
eiriks at hollowmatrix dot com 28-Mar-2005 12:08
Remember that you can't do like Chrigu and Nate said if you want to add methods from a static class (Hence you can't create any instances of it).
A workaround is to create lambda functions calling the
methods:

// Our static handler class
static class MyHandler
{
    public function getPrice($item)
    {
        $prices = array("apple" => 4, "orange" => 5);
        return $prices[$item];
    }
    public function buy($item, $number)
    {
        $price = self::getPrice($item) * $number;
        do_thing_to_sell_the_item();
        return $price;
    }
}

// Use reflection to get method names and parameters
$mirror = new ReflectionClass("MyHandler");
foreach ($mirror->getMethods() as $method)
{
    // Create new "lambda" function for each method

    // Generate argument list
    $args = array();
    foreach ($method->getParameters() as $param)
    {
        $args[] = '$'.$param->getName();
    }
    $args = implode(',', $args);

    // Generate code
    $methodname = $method->getName();
    $code = "return {$real_class}::{$methodname}({$args});";

    // Create function, retrieve function name
    $function_name = create_function($args, $code);

    // Register the function
    xmlrpc_server_register_method($myserver, $methodname, $function_name);
}
Nate Parsons 30-Apr-2003 12:12
In case its not completely obvious what Chrigu meant,

You can register a method inside your class by doing the following:

xml_rpc_server_register_methode($xmlrpc_server, "myClientCall", array(&$this, "handleClientCallFunc"));

where $this == the magic class $this. =)
18-Nov-2002 07:40
Here is an example how to register a class methode:

xml_rpc_server_register_methode($xmlrpc_server, "foo", array(&$bar, "foo_func"));

where $bar is the instance of your class and foo_func a methode of this class. Don't forget the '&'!

hope this may be useful...

Chrigu


Sets xmlrpc type, base64 or datetime, for a PHP string value

  • ≪ xmlrpc_server_register_method
  • Windows Only Extensions ≫
  • PHP Manual
  • XML-RPC 함수 목록
  • Sets xmlrpc type, base64 or datetime, for a PHP string value

xmlrpc_set_type

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

xmlrpc_set_type — Sets xmlrpc type, base64 or datetime, for a PHP string value

설명

bool xmlrpc_set_type ( string &$value , string $type )

Sets xmlrpc type, base64 or datetime, for a PHP string value.

Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

인수

value

Value to set the type

type

'base64' or 'datetime'

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다. If successful, value is converted to an object.

예제

Example #1 A xmlrpc_set_type() example

<?php

$params = date("Ymd\TH:i:s", time());
xmlrpc_set_type($params, 'datetime');
echo xmlrpc_encode($params);

?>

위 예제의 출력 예시:

<?xml version="1.0" encoding="utf-8"?>
<params>
<param>
 <value>
  <dateTime.iso8601>20090322T23:43:03</dateTime.iso8601>
 </value>
</param>
</params>

오류/예외

Issues E_WARNING with type unsupported by XMLRPC.

User Contributed Notes

bmatheny at mobocracy dot net 24-Mar-2006 12:08
The following code segfaults some older (pre 5.1.2) versions of PHP:

$foo = date('c', time());
xmlrpc_set_type($foo, 'datetime');

Please upgrade before reporting as a bug.
shem((at))etkDOTca [aka.Przemyslaw Szot] 30-Oct-2004 11:23
Once you use the xmlrpc_set_type function, the data is encoded into a PHP object.  In your XMLRPC server, in order to access the data you must be able to access the necessary part of the object.

So.. to expend on the example above:

<---------- CLIENT ---------->
$string = "My logging event.";
$date = "20030115T12:22:37"; // Must be this format
$binary = fread($fp, 128);
xmlrpc_set_type(&$date, "datetime");
xmlrpc_set_type(&$binary, "base64");
$xmlrpcReq = xmlrpc_encode_request("log.data", array($string, $date, $binary));

In order to retrieve the binary file data you would need to get the scalar portion of the object:

<---------- SERVER ------------>
$string=$params[0];
$date_obj=$params[1];
$binary_obj=$params[2];

$date=$date_obj->scalar;
$binary_data=$binary_obj->scalar;

// Then you can proceed to write the binary
fwrite($handle,$binary_data);
kelly at seankelly dot biz 28-Dec-2002 04:53
The problem is that PHP has a string type which is also used to hold binary data and dates.  But XML-RPC defines three separate types for strings, binary data, and dates.  How do you tell how you want strings encoded?  That's where this function comes in.

Suppose the XML-RPC method "log.data" took a string, a date, and a binary object.  To tell XML-RPC that the date (which is a PHP string) is a really a date and that the binary data (which is also a PHP string) is really binary data, try:

$string = "My logging event.";
$date = "20030115T12:22:37"; // Must be this format
$binary = fread($fp, 128);
xmlrpc_set_type(&$date, "datetime");
xmlrpc_set_type(&$binary, "base64");
$xmlrpcReq = xmlrpc_encode_request("log.data", array($string, $date, $binary));

Note the reference passing in the calls to xmlrpc_set_type; that enables the function to change the values from strings into what xmlrpc_encode/_request expects (which are objects that include a bonus field that tells the desired XML-RPC type).


Windows Only Extensions

  • COM — COM and .Net (Windows)
    • 소개
    • 설치/설정
    • 예약 상수
    • Errors and error handling
    • 예제
    • COM — The COM class
    • DOTNET — The DOTNET class
    • VARIANT — VARIANT class
    • COM 함수 목록
  • win32ps
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • win32ps 함수 목록
  • win32service
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • win32service 함수 목록

User Contributed Notes

There are no user contributed notes for this page.


COM and .Net (Windows)

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • Errors and error handling
  • 예제
    • For Each
    • Arrays and Array-style COM properties
  • COM — The COM class
  • DOTNET — The DOTNET class
  • VARIANT — VARIANT class
  • COM 함수 목록
    • com_create_guid — Generate a globally unique identifier (GUID)
    • com_event_sink — Connect events from a COM object to a PHP object
    • com_get_active_object — Returns a handle to an already running instance of a COM object
    • com_load_typelib — Loads a Typelib
    • com_message_pump — Process COM messages, sleeping for up to timeoutms milliseconds
    • com_print_typeinfo — Print out a PHP class definition for a dispatchable interface
    • variant_abs — Returns the absolute value of a variant
    • variant_add — "Adds" two variant values together and returns the result
    • variant_and — Performs a bitwise AND operation between two variants
    • variant_cast — Convert a variant into a new variant object of another type
    • variant_cat — concatenates two variant values together and returns the result
    • variant_cmp — Compares two variants
    • variant_date_from_timestamp — Returns a variant date representation of a Unix timestamp
    • variant_date_to_timestamp — Converts a variant date/time value to Unix timestamp
    • variant_div — Returns the result from dividing two variants
    • variant_eqv — Performs a bitwise equivalence on two variants
    • variant_fix — Returns the integer portion of a variant
    • variant_get_type — Returns the type of a variant object
    • variant_idiv — Converts variants to integers and then returns the result from dividing them
    • variant_imp — Performs a bitwise implication on two variants
    • variant_int — Returns the integer portion of a variant
    • variant_mod — Divides two variants and returns only the remainder
    • variant_mul — Multiplies the values of the two variants
    • variant_neg — Performs logical negation on a variant
    • variant_not — Performs bitwise not negation on a variant
    • variant_or — Performs a logical disjunction on two variants
    • variant_pow — Returns the result of performing the power function with two variants
    • variant_round — Rounds a variant to the specified number of decimal places
    • variant_set_type — Convert a variant into another type "in-place"
    • variant_set — Assigns a new value for a variant object
    • variant_sub — Subtracts the value of the right variant from the left variant value
    • variant_xor — Performs a logical exclusion on two variants

User Contributed Notes

jfcollado at orangemail dot es 15-Sep-2014 11:05
Hello people!
I'm trying to use COM class for LDAP, something like :
 $ADSI = new COM("LDAP:");
    $user = $ADSI->OpenDSObject("LDAP://".$server."/".$newuser_dn, $adminuser, $adminpassword, 1);
    $user->SetPassword($newuser_password);
    $user->SetInfo();
But  it not conect.....don't change the password and I'm need that this works....Help me!!!
robert dot johnson at icap dot com 24-Jul-2012 06:30
(From?) PHP 5.4.5 for Windows:

If you are seeing this in your error log:

Fatal error:  Class 'COM' not found

You require this in php.ini:

[PHP_COM_DOTNET]
extension=php_com_dotnet.dll

Previously it was compiled as built-in on the Windows build.  I assume this has happened because the com extension can now be built as a shared component.
Anonymous 23-Sep-2009 02:00
Add hyperlink at a Word Document's bookmark

<?php
// Create COM instance to word
       function clsMSWord($Visible = false)
       {
           $this->handle = new COM("word.application") or die("Unable to instanciate Word");
           $this->handle->Visible = $Visible;
       }

function WriteHyperlink($Bookmark,$Path,$Text)
       {
               $objBookmark = $this->handle->ActiveDocument->Bookmarks($Bookmark);
               $range = $objBookmark->Range;
               $objHyperlink = $this->handle->ActiveDocument->Hyperlinks;
               $objHyperlink->add($range,$Path,"","",$Text);

       }
?>
Dave Bachtel 27-May-2009 11:20
Hello everybody!

Here is some helpful advice for people attempting to use COM with Microsoft MapPoint 2006 or 2009 with PHP.

If you are using apache, it MUST be running under the same credentials as a desktop user that has already installed/run mappoint or modifiy the service and select the "Allow Service to Interact with Desktop" option.  Otherwise, it won't work due to the EULA popup having to be accepted.  Mappoint 2004 works just fine, this only applies to 2006 and 2009.

For troubleshooting, the error that appears in the System event viewer is:
The server {31851F82-AFE6-11D2-A3C9-00C04F72F340} did not register with DCOM within the required timeout.

The error generated by PHP, if you happen to get lucky and let the COM() function timeout by cranking up set_time_limit() timeout is:
<?php

set_time_limit(1000) ;

$mapoint =  new COM("MapPoint.Application") or die("Unable to instantiate Mappoint COM object");

?>
Generates:
Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `MapPoint.Application': Server execution failed ' in [somefile.php]:9 Stack trace: #0 [somefile.php](9): com->com('MapPoint.Applic...') #1 {main} thrown in [somefile.php] on line 9

Also, if you have multiple versions of MapPoint installed, you will need to run:
c:\path\to\mappoint\MapPoint.exe /registerserver

(using the correct folder path) to select the right version of the com object to use.  Gooood luck!
acsandeep at gmail dot com 05-Apr-2009 06:40
If you are trying to get the properties of a Word document opened via COM object, you may need to define some constants in your script like so.

<?php
define('wdPropertyTitle', 1);
define('wdPropertySubject', 2);
define('wdPropertyAuthor', 3);
define('wdPropertyKeywords', 4);
define('wdPropertyComments', 5);
define('wdPropertyTemplate', 6);
define('wdPropertyLastAuthor', 7);

$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("Sample.doc"));
$Author = $word->ActiveDocument->BuiltInDocumentProperties(wdPropertyAuthor);

echo $Author;
?>
long2hu3 ATT yahoo DOTT com 18-Mar-2009 01:38
When you work with MS Excel, Word, ... and other applications, never forget that COM doesn't know their predefined constants, thus if you want to do sth. that would look like this in VB:

With myRange.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With

you should either use numbers or define constants above, like this:

<?php

define('xlEdgeBottom', 9);
define('xlContinuous', 1);
define('xlThin', 2);
define('xlAutomatic', -4105);

$ex = new COM("Excel.Application", NULL, CP_UTF8) or Die ("Did not instantiate Excel");
$wb = $ex->Application->Workbooks->Add();
$ws = $wb->Worksheets(1);

$xra = $ws->Range("A1:A6");

$bs = $xra->Borders(xlEdgeBottom);
$bs->LineStyle = xlContinuous;
$bs->Weight = xlThin;
$bs->ColorIndex = xlAutomatic;

?>

It is pointless to try to use text strings, i.e.

<?php

$bs = $xra->Borders('xlEdgeBottom');
$bs->Weight = 'xlThin';

?>

this will cause an error: Unable to set property Weight of class Border ...
ilayansmano at gmail dot com 26-Aug-2008 10:39
Extracting text from Word Documents via PHP and COM

<?php
$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("Sample.doc"));

// Extract content.
$content = (string) $word->ActiveDocument->Content;

echo $content;

$word->ActiveDocument->Close(false);

$word->Quit();
$word = null;
unset($word);
?>


소개

COM is an acronym for Component Object Model; it is an object orientated layer (and associated services) on top of DCE RPC (an open standard) and defines a common calling convention that enables code written in any language to call and interoperate with code written in any other language (provided those languages are COM aware). Not only can the code be written in any language, but it need not even be part of the same executable; the code can be loaded from a DLL, be found in another process running on the same machine, or, with DCOM (Distributed COM), be found in another process on a remote machine, all without your code even needing to know where a component resides.

There is a subset of COM known as OLE Automation which comprises a set of COM interfaces that allow loose binding to COM objects, so that they can be introspected and called at run-time without compile-time knowledge of how the object works. The PHP COM extension utilizes the OLE Automation interfaces to allow you to create and call compatible objects from your scripts. Technically speaking, this should really be called the "OLE Automation Extension for PHP", since not all COM objects are OLE compatible.

Now, why would or should you use COM? COM is one of the main ways to glue applications and components together on the Windows platform; using COM you can launch Microsoft Word, fill in a document template and save the result as a Word document and send it to a visitor of your web site. You can also use COM to perform administrative tasks for your network and to configure your IIS; these are just the most common uses; you can do much more with COM.

Starting with PHP 5, this extension (and this documentation) was rewritten from scratch and much of the old confusing and bogus cruft has been removed. Additionally, we support the instantiation and creation of .Net assemblies using the COM interoperability layer provided by Microsoft.

Please read » this article for an overview of the changes in this extension in PHP 5.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

COM functions are only available for the Windows version of PHP.

.Net support requires PHP 5 and the .Net runtime.

User Contributed Notes

There are no user contributed notes for this page.


설치

As of PHP 5.3.15 / 5.4.5, this extension requires php_com_dotnet.dll to be enabled inside of php.ini in order to use these functions. Previous versions of PHP enabled these extensions by default.

You are responsible for installing support for the various COM objects that you intend to use (such as MS Word); we don't and can't bundle all of those with PHP.

User Contributed Notes

Andriy Bazanov 20-Aug-2012 08:23
As of 5.3.15 (if you are still on 5.3 branch) you have to add extension=php_com_dotnet.dll line into your php.ini to have COM, DOTNET, VARIANT and similar classes available and working.

From PHP Change log:
    COM
        Fixed bug #62146 com_dotnet cannot be built shared

Because of this, the official PHP builds for Windows are now built with "--enable-com-dotnet=shared" option, which means no COM/DOTNET support by default.

Some of my projects that I currently maintain use adodb library which uses VARIANT class and fails to work without it.
robert dot johnson at icap dot com 28-Jul-2012 01:15
From PHP 5.4.5, COM and DOTNET is no longer built into the php core.you have to add COM support in php.ini:

[COM_DOT_NET]
extension=php_com_dotnet.dll

Otherwise you will see this in your error log:
Fatal error:  Class \'COM\' not found

The extension is included with php 5.4.5 for Windows.


실행시 설정

이 함수의 작동은 php.ini 설정에 영향을 받습니다.

Com configuration options
이름 | 기본값 | 가변성 | 변경점
com.allow_dcom | "0" | PHP_INI_SYSTEM | Available since PHP 4.0.5.
com.autoregister_typelib | "0" | PHP_INI_ALL | PHP_INI_SYSTEM in PHP 4. Available since PHP 4.1.0.
com.autoregister_verbose | "0" | PHP_INI_ALL | PHP_INI_SYSTEM in PHP 4. Available since PHP 4.1.0.
com.autoregister_casesensitive | "1" | PHP_INI_ALL | PHP_INI_SYSTEM in PHP 4. Available since PHP 4.1.0.
com.code_page | "" | PHP_INI_ALL | Available since PHP 5.0.0.
com.typelib_file | "" | PHP_INI_SYSTEM | Available since PHP 4.0.5.
PHP_INI_* 모드에 대한 상세와 정의는 Where a configuration setting may be set를 참고하십시오.

위 설정 지시어에 대한 간단한 설명입니다.

com.allow_dcom

When this is turned on, PHP will be allowed to operate as a D-COM (Distributed COM) client and will allow the PHP script to instantiate COM objects on a remote server.

com.autoregister_typelib

When this is turned on, PHP will attempt to register constants from the typelibrary of objects that it instantiates, if those objects implement the interfaces required to obtain that information. The case sensitivity of the constants it registers is controlled by the configuration directive.

com.autoregister_verbose

When this is turned on, any problems with loading a typelibrary during object instantiation will be reported using the PHP error mechanism. The default is off, which does not emit any indication if there was an error finding or loading the type library.

com.autoregister_casesensitive

When this is turned on (the default), constants found in auto-loaded type libraries will be registered case sensitively. See com_load_typelib() for more details.

com.code_page

It controls the default character set code-page to use when passing strings to and from COM objects. If set to an empty string, PHP will assume that you want CP_ACP, which is the default system ANSI code page.

If the text in your scripts is encoded using a different encoding/character set by default, setting this directive will save you from having to pass the code page as a parameter to the COM class constructor. Please note that by using this directive (as with any PHP configuration directive), your PHP script becomes less portable; you should use the COM constructor parameter whenever possible.

Note: This configuration directive was introduced with PHP 5.

com.typelib_file

When set, this should hold the path to a file that contains a list of typelibraries that should be loaded on startup. Each line of the file will be treated as the type library name and loaded as though you had called com_load_typelib(). The constants will be registered persistently, so that the library only needs to be loaded once. If a type library name ends with the string #cis or #case_insensitive, then the constants from that library will be registered case insensitively.

User Contributed Notes

There are no user contributed notes for this page.


자원형

This extension defines a reference to a COM component returned by deprecated com_load() (this function does not exist in PHP 5; use the COM class instead).

User Contributed Notes

There are no user contributed notes for this page.


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

COM constants
Constant | Value | Description | Notes
CLSCTX_INPROC_SERVER (integer) | 1 | The code that creates and manages objects of this class is a DLL that runs in the same process as the caller of the function specifying the class context. |
CLSCTX_INPROC_HANDLER (integer) | 2 | The code that manages objects of this class is an in-process handler. This is a DLL that runs in the client process and implements client-side structures of this class when instances of the class are accessed remotely. |
CLSCTX_LOCAL_SERVER (integer) | 4 | The EXE code that creates and manages objects of this class runs on same machine but is loaded in a separate process space. |
CLSCTX_REMOTE_SERVER (integer) | 16 | A remote context. The code that creates and manages objects of this class is run on a different computer. |
CLSCTX_SERVER (integer) | 21 | Indicates server code, whether in-process, local, or remote. This definition ORs CLSCTX_INPROC_SERVER, CLSCTX_LOCAL_SERVER, and CLSCTX_REMOTE_SERVER. |
CLSCTX_ALL (integer) | 23 | Indicates all class contexts. This definition ORs CLSCTX_INPROC_HANDLER and CLSCTX_SERVER. |
VT_NULL (integer) | 1 | NULL pointer reference. |
VT_EMPTY (integer) | 0 | A property with a type indicator of VT_EMPTY has no data associated with it; that is, the size of the value is zero. |
VT_UI1 (integer) | 17 | 1-byte unsigned integer. |
VT_I2 (integer) | 2 | Two bytes representing a 2-byte signed integer value. |
VT_I4 (integer) | 3 | 4-byte signed integer value. |
VT_R4 (integer) | 4 | 32-bit IEEE floating point value. |
VT_R8 (integer) | 5 | 64-bit IEEE floating point value. |
VT_BOOL (integer) | 11 | Boolean value. |
VT_ERROR (integer) | 10 | Error code; containing the status code associated with the error. |
VT_CY (integer) | 6 | 8-byte two's complement integer (scaled by 10,000). |
VT_DATE (integer) | 7 | A 64-bit floating point number representing the number of days (not seconds) since December 31, 1899. For example, January 1, 1900, is 2.0, January 2, 1900, is 3.0, and so on). This is stored in the same representation as VT_R8. |
VT_BSTR (integer) | 8 | Pointer to a null-terminated Unicode string. |
VT_DECIMAL (integer) | 14 | A decimal structure. |
VT_UNKNOWN (integer) | 13 | A pointer to an object that implements the IUnknown interface. |
VT_DISPATCH (integer) | 9 | A pointer to a pointer to an object was specified. |
VT_VARIANT (integer) | 12 | A type indicator followed by the corresponding value. VT_VARIANT can be used only with VT_BYREF. |
VT_I1 (integer) | 16 | 1-byte signed integer. |
VT_UI2 (integer) | 18 | 2-byte unsigned integer. |
VT_UI4 (integer) | 19 | 4-byte unsigned integer. |
VT_INT (integer) | 22 | 4-byte signed integer value (equivalent to VT_I4). |
VT_UINT (integer) | 23 | 4-byte unsigned integer (equivalent to VT_UI4). |
VT_ARRAY (integer) | 8192 | If the type indicator is combined with VT_ARRAY by an OR operator, the value is a pointer to a SAFEARRAY. VT_ARRAY can use the OR with the following data types: VT_I1, VT_UI1, VT_I2, VT_UI2, VT_I4, VT_UI4, VT_INT, VT_UINT, VT_R4, VT_R8, VT_BOOL, VT_DECIMAL, VT_ERROR, VT_CY, VT_DATE, VT_BSTR, VT_DISPATCH, VT_UNKNOWN and VT_VARIANT. |
VT_BYREF (integer) | 16384 | If the type indicator is combined with VT_BYREF by an OR operator, the value is a reference. Reference types are interpreted as a reference to data, similar to the reference type in C++. |
CP_ACP (integer) | 0 | Default to ANSI code page. |
CP_MACCP (integer) | 2 | Macintosh code page. |
CP_OEMCP (integer) | 1 | Default to OEM code page. |
CP_UTF7 (integer) | 65000 | Unicode (UTF-7). |
CP_UTF8 (integer) | 65001 | Unicode (UTF-8). |
CP_SYMBOL (integer) | 42 | SYMBOL translations. |
CP_THREAD_ACP (integer) | 3 | Current thread's ANSI code page |
VARCMP_LT (integer) | 0 | The left bstr is less than right bstr. |
VARCMP_EQ (integer) | 1 | The two parameters are equal. |
VARCMP_GT (integer) | 2 | The left bstr is greater than right bstr. |
VARCMP_NULL (integer) | 3 | Either expression is NULL. |
NORM_IGNORECASE (integer) | 1 | Ignore case sensitivity. |
NORM_IGNORENONSPACE (integer) | 2 | Ignore nonspacing characters. |
NORM_IGNORESYMBOLS (integer) | 4 | Ignore symbols. |
NORM_IGNOREWIDTH (integer) | 131072 | Ignore string width. |
NORM_IGNOREKANATYPE (integer) | 65536 | Ignore Kana type. |
NORM_IGNOREKASHIDA (integer) | 262144 | Ignore Arabic kashida characters. | Availability is dependent upon under lying library.
DISP_E_DIVBYZERO (integer) | -2147352558 | A return error that indicates a divide by zero error. |
DISP_E_OVERFLOW (integer) | -2147352566 | An error that indicates that a value could not be coerced to its expected representation. |
MK_E_UNAVAILABLE (integer) | -2147221021 | iMoniker COM status code, return on errors where the function call failed due to unavailability. |

User Contributed Notes

There are no user contributed notes for this page.


Errors and error handling

Exceptions (PHP 5)

This extension will throw instances of the class com_exception whenever there is a potentially fatal error reported by COM. All COM exceptions have a well-defined code property that corresponds to the HRESULT return value from the various COM operations. You may use this code to make programmatic decisions on how to handle the exception.

User Contributed Notes

There are no user contributed notes for this page.


예제

Table of Contents

  • For Each
  • Arrays and Array-style COM properties

User Contributed Notes

There are no user contributed notes for this page.


For Each

Starting with PHP 5, you may use PHP's own foreach statement to iterate over the contents of a standard COM/OLE IEnumVariant. In layman's terms, this means that you can use foreach in places where you would have used For Each in VB/ASP code.

Example #1 For Each in ASP

<%
Set domainObject = GetObject("WinNT://Domain")
For Each obj in domainObject
  Response.Write obj.Name & "<br />"
Next
%>

Example #2 while() ... Next() in PHP 4

<?php
$domainObject = new COM("WinNT://Domain");
while ($obj = $domainObject->Next()) {
   echo $obj->Name . "<br />";
}
?>

Example #3 foreach in PHP 5

<?php
$domainObject = new COM("WinNT://Domain");
foreach ($domainObject as $obj) {
   echo $obj->Name . "<br />";
}
?>

User Contributed Notes

szir at sch dot bme dot hu 05-Jul-2011 11:54
As described in the PHP 5 COM changes article: http://devzone.zend.com/node/view/id/762
You not just "may use foreach statement", but you have to, because $domainObject->Next() is not available in PHP 5 (syntax has been dropped, not backwards compatible)


Arrays and Array-style COM properties

Many COM objects expose their properties as arrays, or using array-style access. In PHP 4, you may use PHP array syntax to read/write such a property, but only a single dimension is allowed. If you want to read a multi-dimensional property, you could instead make the property access into a function call, with each parameter representing each dimension of the array access, but there is no way to write to such a property.

PHP 5 introduces the following new features to make your life easier:

  • Access multi-dimensional arrays, or COM properties that require multiple parameters using PHP array syntax. You can also write or set properties using this technique.

  • Iterate SafeArrays ("true" arrays) using the foreach control structure. This works because SafeArrays include information about their size. If an array-style property implements IEnumVariant then you can also use foreach for that property too; take a look at For Each for more information on this topic.

User Contributed Notes

There are no user contributed notes for this page.


The COM class

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

Description

The COM class allows you to instantiate an OLE compatible COM object and call its methods and access its properties.

$obj = new COM("Application.ID")

Methods

COM::__construct ( string $module_name [, mixed $server_name [, int $codepage [, string $typelib ]]] )

COM class constructor. The parameters have the following meanings:

module_name
Can be a ProgID, Class ID or Moniker that names the component to load. A ProgID is typically the application or DLL name, followed by a period, followed by the object name. e.g: Word.Application. A Class ID is the UUID that uniquely identifies a given class. A Moniker is a special form of naming, similar in concept to a URL scheme, that identifies a resource and specifies how it should be loaded. As an example, you could load up Word and get an object representing a word document by specifying the full path to the word document as the module name, or you can use LDAP: as a moniker to use the ADSI interface to LDAP.
server_name
The name of the DCOM server on which the component should be loaded and run. If NULL, the object is run using the default for the application. The default is typically to run it on the local machine, although the administrator might have configured the application to launch on a different machine. If you specify a non-NULL value for server, PHP will refuse to load the object unless the configuration option is set to TRUE.

If server_name is an array, it should contain the following elements (case sensitive!). Note that they are all optional (although you need to specify both Username and Password together); if you omit the Server setting, the default server will be used (as mentioned above), and the instantiation of the object will not be affected by the directive.

DCOM server name
server_name key | type | description
Server | string | The name of the server.
Username | string | The username to connect as.
Password | string | The password for Username.
Flags | integer | One or more of the following constants, logically OR'd together: CLSCTX_INPROC_SERVER, CLSCTX_INPROC_HANDLER, CLSCTX_LOCAL_SERVER, CLSCTX_REMOTE_SERVER, CLSCTX_SERVER and CLSCTX_ALL. The default value if not specified here is CLSCTX_SERVER if you also omit Server, or CLSCTX_REMOTE_SERVER if you do specify a server. You should consult the Microsoft documentation for CoCreateInstance for more information on the meaning of these constants; you will typically never have to use them.

codepage
Specifies the codepage that is used to convert strings to unicode-strings and vice versa. The conversion is applied whenever a PHP string is passed as a parameter or returned from a method of this COM object. The code page is sticky in PHP 5, which means that it will propagate to objects and variants returned from the object. Possible values are CP_ACP (use system default ANSI code page - the default if this parameter is omitted), CP_MACCP, CP_OEMCP, CP_SYMBOL, CP_THREAD_ACP (use codepage/locale set for the current executing thread), CP_UTF7 and CP_UTF8. You may also use the number for a given codepage; consult the Microsoft documentation for more details on codepages and their numeric values.

Overloaded Methods

The returned object is an overloaded object, which means that PHP does not see any fixed methods as it does with regular classes; instead, any property or method accesses are passed through to COM.

Starting with PHP 5, PHP will automatically detect methods that accept parameters by reference, and will automatically convert regular PHP variables to a form that can be passed by reference. This means that you can call the method very naturally; you needn't go to any extra effort in your code.

In PHP 4, to pass parameters by reference you need to create an instance of the VARIANT class to wrap the byref parameters.

Pseudo Methods

In PHP versions prior to 5, a number of not very pleasant hacks meant that the following method names were not passed through to COM and were handled directly by PHP. PHP 5 eliminates these things; read the details below to determine how to fix your scripts. These magic method names are case insensitive.

void COM::AddRef ( void )

Artificially adds a reference count to the COM object.

Warning

You should never need to use this method. It exists as a logical complement to the Release() method below.

void COM::Release ( void )

Artificially removes a reference count from the COM object.

Warning

You should never need to use this method. Its existence in PHP is a bug designed to work around a bug that keeps COM objects running longer than they should.

Pseudo Methods for Iterating

These pseudo methods are only available if com_isenum() returns TRUE, in which case, they hide any methods with the same names that might otherwise be provided by the COM object. These methods have all been eliminated in PHP 5, and you should use For Each instead.

variant COM::All ( void )

Returns a variant representing a SafeArray that has 10 elements; each element will be an empty/null variant. This function was supposed to return an array containing all the elements from the iterator, but was never completed. Do not use.

variant COM::Next ( void )

Returns a variant representing the next element available from the iterator, or FALSE when there are no more elements.

variant COM::Prev ( void )

Returns a variant representing the previous element available from the iterator, or FALSE when there are no more elements.

void COM::Reset ( void )

Rewinds the iterator back to the start.

COM examples

Example #1 COM example (1)

<?php
// starting word
$word = new COM("word.application") or die("Unable to instantiate Word");
echo "Loaded Word, version {$word->Version}\n";

//bring it to front
$word->Visible = 1;

//open an empty document
$word->Documents->Add();

//do some weird stuff
$word->Selection->TypeText("This is a test...");
$word->Documents[1]->SaveAs("Useless test.doc");

//closing word
$word->Quit();

//free the object
$word = null;
?>

Example #2 COM example (2)

<?php

$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$conn->Open("Provider=SQLOLEDB; Data Source=localhost;
Initial Catalog=database; User ID=user; Password=password");

$rs = $conn->Execute("SELECT * FROM sometable");    // Recordset

$num_columns = $rs->Fields->Count();
echo $num_columns . "\n";

for ($i=0; $i < $num_columns; $i++) {
    $fld[$i] = $rs->Fields($i);
}

$rowcount = 0;
while (!$rs->EOF) {
    for ($i=0; $i < $num_columns; $i++) {
        echo $fld[$i]->value . "\t";
    }
    echo "\n";
    $rowcount++;            // increments rowcount
    $rs->MoveNext();
}

$rs->Close();
$conn->Close();

$rs = null;
$conn = null;

?>

User Contributed Notes

juan156_elias at gmail dot com 09-Nov-2015 12:22
This is an example of using .Net using the NetPhp library that is based on the COM/DOTNET classes.

You can use any .dll file,  wether it is or not COM decorated and any of the .Net framework types out of the box. You can also target any version of the .Net framework.

<?php

$runtime = new \NetPhp\Core\NetPhpRuntime('COM', 'netutilities.NetPhpRuntime');

// Add both SpreadsheetLight and the OpenXML it depends on.
$runtime->RegisterAssemblyFromFile('/binaries/SpreadsheetLight.dll', 'SpreadsheetLight');
$runtime->RegisterAssemblyFromFile('/binaries/DocumentFormat.OpenXml.dll', 'DocumentFormat.OpenXml');
$runtime->RegisterAssemblyFromFile('/binaries/AjaxMin.dll', 'AjaxMin');

// Using the FullName of a type that belongs to an assembly that has already been registered.
$datetime = $runtime->TypeFromName("System.DateTime");

// Using the FullName of a type that has not been registered yet (from a file)
$minifier = $runtime->TypeFromFile("Microsoft.Ajax.Utilities.Minifier", APPLICATION_ROOT . '/binaries/AjaxMin.dll');

// Using the FullName of a type that has not been registered yet (autodiscoverable)
$datetime2 = $runtime->TypeFromAssembly("System.DateTime", "mscorlib, ....");

$datetime->Instantiate();
echo $datetime->ToShortDateString()->Val(); // Outputs 01/01/0001

// We can only use Int32 from native PHP, so parse
// an Int64 that is equivalent to (long) in the DateTime constructor.
$ticks = $runtime->TypeFromName("System.Int64")->Parse('98566569856565656');

$datetime->Instantiate($ticks);
echo $datetime->ToShortDateString()->Val(); // Outputs 07/05/0313

// We can only use Int32 from native PHP, so parse
// an Int64 that is equivalent to (long) in the DateTime constructor.
$ticks = $runtime->TypeFromName("System.Int64")->Parse('98566569856565656');

// Dump a .Net System.Timers.Timer object!
$data = $timer->GetPhpFromJson();

var_dump($data);

    // Outputs:
    //  object(stdClass)[38]
    //     public 'AutoReset' => boolean true
    //     public 'Enabled' => boolean false
    //     public 'Interval' => int 100
    //     public 'Site' => null
    //     public 'SynchronizingObject' => null
    //     public 'Container' => null

// Check if today is monday
$IsMonday = $runtime->TypeFromName("System.DateTime")->Now->DayOfWeek->
Equals($runtime->TypeFromName("System.DayOfWeek")->Enum('Monday'));

?>
Eion Robb 13-Feb-2014 06:59
All the examples here show
<?php $var = new COM('Whatever.Something') or die(); ?>
however the 'or die()' bit is redundant as a fatal 'com_exception' exception will be thrown, killing your script.  If you want to handle the 'or' condition you need to catch the exception, eg
<?php
try {
$var = new COM('Whatever.Something');
} catch (com_exception $e) {
 //couldn't load, do something else like register
exec('regsvr32 /s whatever.dll');
}
?>
rogier 28-Oct-2011 07:09
Took me a while to figure this out by trial and error:

If your frustrated when getting a com-exception like "Error [0x80004002] ..." (with a message nagging you about an unsupported interface), and you're calling a class method through COM that expects a char as an argument, you should NOT call the method with a single-character string.

The correct way of calling a method like this is to use a VARIANT with a type of VT_UI1.

COM enabled method definition:

public bool DoSomething(char arg);

<?php

$com = new COM('Some.Class.Name');

// This will fail
$var = 'a';
$com->DoSomething($var);

// This works correctly
$var = new VARIANT(ord('a'), VT_UI1);
$com->DoSomething($var);

?>

I hope this helps some people
sparrowstail at googlemail dot com 12-Nov-2010 08:35
This script reports all Windows drives, drive type, status (if not available), free space and total drive size:

<?php

    $fso = new COM('Scripting.FileSystemObject');
    $D = $fso->Drives;
    $type = array("Unknown","Removable","Fixed","Network","CD-ROM","RAM Disk");
    foreach($D as $d ){
       $dO = $fso->GetDrive($d);
       $s = "";
       if($dO->DriveType == 3){
           $n = $dO->Sharename;
       }else if($dO->IsReady){
           $n = $dO->VolumeName;
           $s = file_size($dO->FreeSpace) . " free of: " . file_size($dO->TotalSize);
       }else{
           $n = "[Drive not ready]";
       }
   echo "Drive " . $dO->DriveLetter . ": - " . $type[$dO->DriveType] . " - " . $n . " - " . $s . "<br>";

    }

      function file_size($size)
      {
      $filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
      return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Bytes';
      }

?>

There are a number of other properties of the drive object which can be called this way - see:

http://msdn.microsoft.com/en-us/library/ts2t8ybh%28VS.85%29.aspx
z3n at overflow dot biz 25-Sep-2009 05:35
On a query reply using ADODB.Connection object, all my $rs->Fields['field_name']->Value returned `variable Object` when inserted into a array, like this:

<?php
for ($rl[$v]=array(),_qm($_query);!$res->EOF;$res->MoveNext()) {
    $rl[$v][]=$res->Fields['x']->Value;
}
?>

I figured out that converting the value into a INT fixed the issue:

<?php $rl[$v][]=intval($res->Fields['x']->Value); ?>
rupams2002 at yahoo dot com 24-Jul-2009 07:38
If you make a DLL in VB6 and want to access from PHP, here is the example. Only connection method is defined. You need to create rest....

How to use in PHP
DLL refers ProjectName . ClassName
$Obj = new COM ('ProjectName.ClassName');
$dw->MsSql_Connect('MachineName', 'DB_NAme');

If you want to connect Database through DLL then

Open project in VB6 Class Module :
And add this code and compile and make DLL.

Public Function MsSql_Connect(ByVal DBMachineID As String, ByVal DBName As String) As Boolean
    Dim LocalCn As New ADODB.Connection
    Dim conStr As String, DB_UserID As String, DB_Password As String

    On Error GoTo ErrorHandler

    DB_UserID = "DBUserName"
    DB_Password = "DBPassword"

    Set LocalCn = New ADODB.Connection
    LocalCn.CursorLocation = adUseClient

    conStr = "Provider=SQLOLEDB.1;Password=" & DB_Password & ";Persist Security Info=True;User ID=" & DB_UserID & ";Initial Catalog=" & DBName & ";Data Source=" & DBMachineID & ";"
    LocalCn.Open conStr

    DBConnection = conStr

    MsSql_Connect = True
    Set LocalCn = Nothing

ErrorHandler:
    If Err.Number <> 0 Then
       MsSql_Connect = False
       Set LocalCn = Nothing
       Err.Raise vbObjectError + 513, App.EXEName, Err.Description
    End If
End Function

"DBConnection" is a variable you need to Set + Get property in VB6 to keep the connection string alive in DLL.

Remember :
Err.Raise vbObjectError + 513, App.EXEName, Err.Description

will raise error in PHP, user can see error in browser and stop execution php code.

If you need any help email me.

Mohammed Ziaul Hasan [Rupam]
rupams2002@yahoo.com
Senior Developer
Canada
pavanphp Gudipati 17-Jun-2009 03:49
Here I Am describing a Program from which you can connect any exe(software) using COM component.
pdf2swf is a software for converting PDf to SWF files.

This is best example for  WScript.Shell

<?php
#code for pdf to swf using pdf2swf software
#this codeworks in windows environment.

## Important Parameters
$software_path  ="C:\\SWFTools\\pdf2swf" ;
$pdf_path       ="C:\\SWFTools\\abcd.pdf" ;
$argument       = "-o";
$swf_output     ="C:\\SWFTools\\abcd.swf" ;

#actual code
$cmd        =" $software_path $pdf_path  $argument $swf_output";

$WshShell   = new COM("WScript.Shell");
$oExec      = $WshShell->Run("cmd /C $cmd ", 0, true);

# 0 for command prompt invisible mode
# 3 for visible

?>
tsintra at humansoft dot pt 11-May-2009 05:18
After one week of trying to understand what was wrong with my PHP communication with MS Word, i finally got it working...
It seems that if you're running IIS, the COM object are invoked with restricted privileges.
If you're having permission problems like not being able to opening or saving a document and you're getting errors like:

 - This command is not available because no document is open
or
 - Command failed

try this (if you're running IIS):

- Execute "dcomcnfg"
- Open Component Services > Computers > My Computer > DCOM Config
- Search for Microsoft Office Word 97-2003 Document (it will be something like this translated to your language, so take a while and search for it)
- Right-Click on it and open the properties
- Choose "Identity" tab
- Normally this is set to "the launching user". You have to change this to "the interactive user" or a admin user of your choice.
- Apply these new settings and test your COM application. It should work fine now.

I hope I save lots and lots of hours of headaches to some of you :)
fernando at digitins dot com dot br 08-Apr-2009 04:30
When you save a MS Word document with this:
<?php
$word->Documents[1]->SaveAs('myfile.doc');
?>

Your file maybe go to this location:
C:\WINDOWS\system32\config\systemprofile\My documents (and not to your script directory)
dpchiesa at hotmail dot com 28-Mar-2009 06:35
Here's a way to create an AES-Encrypted ZIP file in PHP, using DotNetZip via the COM class.

<?php
try
{
  $fname = "zip-generated-from-php-" . date('Y-m-d-His') . ".zip";
  $zipOutput = "c:\\temp\\" . $fname;
  $zip = new COM("Ionic.Zip.ZipFile");
  $zip->Name = $zipOutput;
  $dirToZip= "c:\\temp\\psh";
  #$dirToZip= "c:\\dinoch\\webs\\php";
  $zip->Encryption = 3;
  $zip->Password = "AES-Encryption-Is-Secure";
  $zip->AddDirectory($dirToZip);
  $zip->Save();
  $zip->Dispose();

  if (file_exists($zipOutput))
  {
    header('Cache-Control: no-cache, must-revalidate');
    header('Content-Type: application/x-zip');
    header('Content-Disposition: attachment; filename=' . $fname);
    header('Content-Length: ' . filesize($zipOutput));
    readfile($zipOutput);
    unlink($zipOutput);
  }
  else
  {
    echo '<html>';
    echo '  <head>';
    echo '  <title>Calling DotNetZip from PHP through COM</title>';
    echo '  <link rel="stylesheet" href="basic.css"/>';
    echo '  </head>';
    echo '<body>';
    echo '<h2>Whoops!</h2>' . "<br/>\n";
    echo '<p>The file was not successfully generated.</p>';
    echo '</body>';
    echo '</html>';
  }
}
catch (Exception $e)
{
    echo '<html>';
    echo '  <head>';
    echo '  <title>Calling DotNetZip from PHP through COM</title>';
    echo '  <link rel="stylesheet" href="basic.css"/>';
    echo '  </head>';
    echo '<body>';
    echo '<h2>Whoops!</h2>' . "<br/>\n";
    echo '<p>The file was not successfully generated.</p>';
    echo '<p>Caught exception: ',  $e->getMessage(), '</p>', "\n";
    echo '<pre>';
    echo $e->getTraceAsString(), "\n";
    echo '</pre>';
    echo '</body>';
    echo '</html>';
}

?>
pavanphp at sparsh dot net 14-Feb-2009 02:42
This is the code for entering Values in MS word through Php Program.

First : You need to make word file with including Boolmark
(Ms word 2003 , Insert -> Bookmark ->Bookmark Name -> add).
Example
Name : add here bookmark
 This is something like input as a text box.  Here I given 5 fields.
Name , TODAYDATE , Company ,project , Features

Here I opened a word file, and put data into Bookmark fields using PHP.

Just note : it works in c:/ only. (not sure why), C:/program files may take longer time. (waiting for ...)

<?php

com_load_typelib('Word.Application');

$word = new COM("word.application") or die("Unable to instantiate Word");

// use like this. Place your's word file in C drive
$word->Documents->Open('C:/reminder_new.doc');
echo $current_date = date("m/d/Y");

$tim= date("Y-m-d");

$info_array=array(  "Name"      => "pavan",
                    "TODAYDATE" => "$tim" ,
                    "Company"   => "sparsh" ,
                    "project"   => "Myrepc",
                    "Features"   => "Auto fill form editor") ;

foreach($info_array as $bookmarkname => $re)
    {

        $objBookmark = $word->ActiveDocument->Bookmarks($bookmarkname);
        $range = $objBookmark->Range;
        echo $range->Text = $re ;

    }

$new_file = "c:/reminder_filled.doc";

$word->Documents[1]->SaveAs($new_file);

$word->ActiveDocument->Close(false);

$word->Quit();
$word = null;

?>
james dot m dot love at gmail dot com 07-Jan-2009 12:27
If you're using COM to create a DSN Less connection to an MS Access 2007 database, you'll find through Googling that you need to use an updated version of Jet, so the connection string I've found looks like this:

<?php
$databaselocation = "C:\Path\to\db.accdb";
$conn = new COM('ADODB.Connection') or exit('Cannot start ADO.');
$conn->Open("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=$databaselocation") or die('Cannot start with Jet');
?>

However, on my setup (WinVista, Access 2007, PHP526) I found that it always 'dies' with "Cannot start with Jet", even if the connection was successful ($conn->State reads "1").

I figured to just drop the die() command on $conn->Open(). If something catastrophic happens with the ADO connection then it's own error handling engine passes stack traces, etc back to the console/standard output.

Therefore, my Open() command looks like this:

<?php
$conn->Open("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=$databaselocation");
?>
casanoteva at yahoo dot com 26-Dec-2008 10:19
Hi, just wanna share to everyone after tried and tested many many times to create report in PDF format from Crystal Report.
Finally I can do it without any error.
 Here is my system; PHP 5.1.6, MSSQL2005 and Crystal Report Server XI RL2

<?php

//- Variables - for your RPT and PDF
echo "Print Report Test";
$my_report = "D:\\Folder1\\SubFolder1\\Report.rpt"; //
rpt source file
$my_pdf = "D:\\Folder1\\SubFolder1\\Report.pdf"; // RPT export to pdf file
//-Create new COM object-depends on your Crystal Report version
$ObjectFactory= new COM("CrystalReports115.ObjectFactory.1") or die ("Error on load"); // call COM port
$crapp = $ObjectFactory-> CreateObject("CrystalDesignRunTime.Application"); // create an instance for Crystal
$creport = $crapp->OpenReport($my_report, 1); // call rpt report

// to refresh data before

//- Set database logon info - must have
$creport->Database->Tables(1)->SetLogOnInfo("servername", "DBname", "user", "password");

//- field prompt or else report will hang - to get through
$creport->EnableParameterPrompting = 0;

//- DiscardSavedData - to refresh then read records
$creport->DiscardSavedData;
$creport->ReadRecords();

//export to PDF process
$creport->ExportOptions->DiskFileName=$my_pdf; //export to pdf
$creport->ExportOptions->PDFExportAllPages=true;
$creport->ExportOptions->DestinationType=1; // export to file
$creport->ExportOptions->FormatType=31; // PDF type
$creport->Export(false);

//------ Release the variables ------
$creport = null;
$crapp = null;
$ObjectFactory = null;

//------ Embed the report in the webpage ------
print "<embed src=\"D:\\Folder1\\SubFolder1\\Report.pdf\" width=\"100%\" height=\"100%\">"

?>

 That's all for me, anyway in Crystal Report I have to check DisCardSavedData, Save data with report and AutoSave (1 min) in Option Tab and also in Report Options I have to check save dat with report, too.

Hope this should be help to anybody

Casanoteva
johnno1985 at gmail dot com 22-Aug-2008 08:34
I couldn't find an example anywhere on what I was trying to do.  After taking bits from everyone I ended up with something that worked.

Im using Crystal Reports 10, MS SQL Server 2005, Apache2, and PHP5.

My report uses an SQL Store Procedure and passes some parameter to get the report data, so our PHP must set the database logon info and pass the parameters.

Below is the solution I came up with:

<?php

//------  Variables ------
$my_report = "C:\\Apache2\htdocs\\test\\MyReport.rpt";  //This must be the full path to the file
$my_pdf = "C:\\Apache2\htdocs\\test\\MyReport.pdf";

//------ Create a new COM Object of Crytal Reports 10 ------
$ObjectFactory= new COM("CrystalReports10.ObjectFactory.1");

//------ Create a instance of library Application -------
$crapp = $ObjectFactory->CreateObject("CrystalDesignRunTime.Application.10");

//------ Open your rpt file ------
$creport = $crapp->OpenReport($my_report, 1);

//------ Set database logon info ------
$creport->Database->Tables(1)->SetLogOnInfo("MYSERVER", "Database", "user", "password");

//------ Suppress the Parameter field prompt or else report will hang ------
$creport->EnableParameterPrompting = 0;

//------ DiscardSavedData make a Refresh in your data -------
$creport->DiscardSavedData;
$creport->ReadRecords();

//------ Pass formula fields --------
$creport->FormulaFields->Item(1)->Text = ("'My Report Title'");
$creport->ParameterFields(1)->AddCurrentValue ("FirstParameter");
$creport->ParameterFields(2)->AddCurrentValue (2000);

//------ Export to PDF -------
$creport->ExportOptions->DiskFileName=$my_pdf;
$creport->ExportOptions->FormatType=31;
$creport->ExportOptions->DestinationType=1;
$creport->Export(false);

//------ Release the variables ------
$creport = null;
$crapp = null;
$ObjectFactory = null;

//------ Embed the report in the webpage ------
print "<embed src=\"MyReport.pdf\" width=\"100%\" height=\"100%\">"

?>

Obviously there's plenty of room for refinement in there, but it works and should get you going on the right track.  I also found that during testing, the script was hanging before supressing the parameter prompt. Once this had happened, some exports that had been working earlier stopped completely and wouldn't work again untill I restarted Apache.  This might be something worth doing if you keep having issues after a script has hung.
verticka at hotmail dot com 02-Aug-2008 01:38
Voila une deuxime fonction pour excel 2007 et le plugin pdf

<?php
function xls2pdf($doc,$pdf)
{

// Pas de parametres requis
$empty = new VARIANT();

/* Supression du pdf si il existe */
if(file_exists ($pdf))
@unlink($pdf);

/* Demarrage de Word */
$w = new COM("excel.application") or die("Impossible d'instancier l'application Word");

/* Test de Word 2007 */
if($w->Version > 11)
{
    /* Amener Word devant */
    $w->Visible = 1;

    /* Test du fichier */
    if(file_exists ($doc))
        $w->Workbooks->Open($doc);
    else
        return false;

    /* Quelques commandes */
    $w->Workbooks[1]->ExportAsFixedFormat(0,$pdf);

    /* Fermeture de word */
    $w->Workbooks[1]->Close(false);
}
$w->Quit();

/* Liberation des ressources */
$w = null;
unset($w);

/* Test du fichier */
if(file_exists ($pdf))
    return true;
else
    return false;

}

if(xls2pdf("c:\\test.xls","c:\\test.pdf"))
echo "ok";
else
echo "nok";
?>

Pour les options d'exportation :
http://msdn.microsoft.com/en-us/library/bb238907.aspx

http://verticka.blogspot.com/
mastrboy.servebeer.com 13-Jun-2008 02:03
quick wmi query example:

<?php
    $obj = new COM ( 'winmgmts://localhost/root/CIMV2' );
    $wmi_computersystem =     $obj->ExecQuery("Select * from Win32_ComputerSystem");
    $wmi_bios             =    $obj->ExecQuery("Select * from Win32_BIOS");
    foreach ( $wmi_computersystem as $wmi_call )
    {
        $model = $wmi_call->Model;
    }

    foreach ( $wmi_bios as $wmi_call )
    {
        $serial = $wmi_call->SerialNumber;
        $bios_version = $wmi_call->SMBIOSBIOSVersion;
    }
    echo "Bios version   : $bios_version\n".
         "Serial number  : $serial\n".
         "Hardware Model : $model\n";
?>
mbirth at webwriters dot de 08-Jun-2008 12:19
Using the DynamicWrapper from http://freenet-homepage.de/gborn/WSHBazaar/WSHDynaCall.htm and after registering it via "regsvr32 dynwrap.dll", you can easily set output colors to make colorful CLI scripts even on Windows.

<?php

$dw = new COM('DynamicWrapper');  // needs dynwrap.dll (regsvr32 dynwrap.dll)

// register needed features
$dw->Register('kernel32.dll', 'GetStdHandle', 'i=h', 'f=s', 'r=l');
$dw->Register('kernel32.dll', 'SetConsoleTextAttribute', 'i=hl', 'f=s', 'r=t');
$dw->Register('kernel32.dll', 'SetConsoleTitle', 'i=s', 'f=s', 'r=l');

// get console handle
$ch = self::$dw->GetStdHandle(-11);  // -11 = STD_OUTPUT_HANDLE

?>

After these initialization steps, you can set the console output color using:

<?php
$dw->SetConsoleTextAttribute($ch, 14);
echo 'This is yellow text!';
$dw->SetConsoleTextAttribute($ch, 7);
echo 'Back to normal gray!';
?>

Using SetConsoleTitle you can even set the title displayed in the console window.

Color values are 0..7 for dark colors and 8..15 for light colors. Sequence is (black/silver, blue, green, cyan, red, magenta, yellow/brown, white/gray). I also found that if you add 16384 to the value, it should be inverse. 32768 added should get underlined text. But these latter two didn't work for me.
amenthes at NOSPAM dot gmail dot com 03-Mar-2008 07:13
If you ever want to save an Excel(.xls) file into the Office Excel 2003 XML format, just use

$excel->Workbooks[1]->SaveAs('new_file_name.xml', 46);

That's the number you need, 46.
halfer 21-Jun-2007 02:03
Thanks to paul at completewebservices who added a note earlier; I have used his code to solve a particularly difficult issue with the Crystal 9 runtime component. For some reason, VBA/VBS can pass a native Date type as a parameter to a Crystal Report requiring a date, but in PHP I've tried all manner of strings, unix timestamps, COM variant dates etc and all of these resulted in a com_exception.

My solution was to employ VBScript to borrow the CDate function, which works correctly. Not the most elegant of solutions, but this parameterised approach is to be preferred to search-n-replacing the RecordSelectionFormula string. Of course if anyone here has an even better approach, do post it here - I am sure it would be of use.

<?php
// $rptParam is a report parameter object having a type of date
$oScript = new COM("MSScriptControl.ScriptControl");
$oScript->Language = "VBScript";
$oScript->AllowUI = false;
$oScript->AddObject('rptParam', $rptParam, true);
$oScript->AddCode('Function SetDateParameter(strDate)
rptParam.AddCurrentValue(CDate(strDate))
End Function');
$oScript->Run("SetDateParameter", "25 April 2006");
?>
hwcshirley at gmail dot com 22-May-2006 07:59
Reply to: djogopatrao at gmail dot com
I have change little bit your code. Its work.

$COM_Object = "CrystalDesignRunTime.Application";
$my_report = "C:\\appserv\\www\\mc\\test.rpt";
$my_pdf = "C:\\appserv\www\\mc\\test.pdf";

$crapp= New COM($COM_Object) or die("Unable to Create Object");
$creport = $crapp->OpenReport($my_report, 1);
$creport->ReadRecords(); // attention!

$creport->ExportOptions->DiskFileName=$my_pdf;
$creport->ExportOptions->PDFExportAllPages=true;
$creport->ExportOptions->DestinationType=1; // Export to File
$creport->ExportOptions->FormatType=31; // Type: PDF
$creport->Export(false);
djogopatrao at gmail dot com 05-Apr-2006 09:25
My previous notes about Crystal Reports have vanished away, but anyway I was up to correct them.

The code above connects to COM (beware of the version number! search on the Registry Editor for the correct name to put in $COM_Object), opens a report from a file ($my_report), reload records from database and then exports it to a PDF file ($my_pdf).

* IMPORTANT * this code, when invoked from the browser (I'm using Apache) AND with a report that connects to a ODBC database, does not work (the problem is specificly the line ReadRecords(), but may arise without it too when data is not saved into the report file).

But when you run this code by PHP-CLI (that is, by the command line), it works ok! I bug-reported this, but have no answer till now ( give it a vote, if you thing it's important: http://bugs.php.net/bug.php?id=36959 ).

Also, exceptions may arise, so put it on a try_catch block.

--------------------------

// by dfcp '06 (djogopatrao@gmail.com)

$COM_Object = "CrystalReports11.ObjectFactory.1";
$my_report = "C:\\report.rpt";
$my_pdf = "C:\\report.pdf";

$ObjectFactory= New COM($COM_Object);
$crapp = $ObjectFactory->CreateObject("CrystalDesignRunTime.Application");
$creport = $crapp->OpenReport($my_report, 1);
$creport->ReadRecords(); // attention!

$creport->ExportOptions->DiskFileName=$my_pdf;
$creport->ExportOptions->PDFExportAllPages=true;
$creport->ExportOptions->DestinationType=1; // Export to File
$creport->ExportOptions->FormatType=31; // Type: PDF
$creport->Export(false);
djogopatrao at gmail dot com 04-Mar-2006 04:27
I found somewhere (http://www.recrystallize.com/merchant/supportfaq/supportfaq0003.htm) a list relating each version of Crystal Reports to its respective progID - it may help some fellows out there.

7          Crystal.CRPE.Application

8.0        CrystalRuntime.Application or CrystalRuntime.Application.8

8.5        CrystalRuntime.Application or CrystalRuntime.Application.8.5

9 (RDC)    CrystalRuntime.Application.9
9 (RAS)    CrystalReports.ObjectFactory.2
10 (RDC)   CrystalRuntime.Application.11
10 (CEE)   CrystalReports10.ObjectFactory.1

XI (RDC)   CrystalRuntime.Application.11
XI (RAS)   CrystalReports11.ObjectFactory.1
paul at completewebservices dot com dot au 29-Jan-2006 12:49
It seems that PHP does not support setting of properties with arguments.  This is how I got around the problem:

        // Object with method I want to set
        $auth = new COM("AUTHXOCX.AuthXOCXCtrl.1");

        // ScriptControl
        $oScript = new COM("MSScriptControl.ScriptControl");
        $oScript->Language = "VBScript";
        $oScript->AllowUI = TRUE;

        // Add out object to the control
        $oScript->AddObject('auth', $auth, true);

        // Create a VBScript function that allow me to set them
       $oScript->AddCode(
'Function fixAccess(accessname)
    auth.AuthDataReferrerEnabled(accessname) = 1
    auth.AuthDataAuthentiXDBEnabled(accessname) = 0
End Function');

        // Execute function
        $oScript->Run("fixAccess", $dir);
volker at kybs dot de 07-Oct-2005 09:51
To use the windows speech system install the "Speech SDK 5.1 for Windows? applications" from the Microsoft homepage (free download).
Then you can do Text2Speech like this:

$voice = new COM("SAPI.SpVoice");
$voice->Speak("Hello, lets have a conversation");
sadi at unicornsoftbd dot com 06-Oct-2005 10:56
If your PHP is configured as ISAPI module then the COM object you create in ur php page will use the same memory space as the web server. If your COM object takes too much memory then this may force some COM error as PHP does not handle the memory usage of COM itself. And also Web server doesnt know what do to free COM memory. But if you use PHP as CGI then PHP itself handle the memory usage of COM. Once in a script where i used COM and PHP as ISAPI module i got unknown COM error.But when i used PHP as CGI there was no error. So those who want to use COM object in your application please be careful about it.
deletethis@bjoern(at)syltonline(doot)de 15-Sep-2005 04:36
If you can get the old "TaskScheduler.dll" and register it, it is quite simple to handle and start Windows tasks from PHP.
This is extremely useful when you got things to do which must be run in a different user context. Prepare a task with the user and use PHP & Com to build a webinterface for it.

Here is an example how to start a Windows task:

  function start_task ($taskname) {
    $SchedObj = new COM("Scheduler.SchedulingAgent.1");
    foreach ($SchedObj->Tasks as $task) {
      if (strtolower( (string) $task) == strtolower( $taskname.".job" )) {
         $task->Run();
      }
    }
  }

I'm also working on a class to handle infos, adding and removing of tasks via PHP. If you are interested drop me a line.
jbr at ya-right dot com 19-Aug-2005 10:07
Getting * directory * permissions ( attributes ) with COM

PHP (5) example...

<?
    $dir = 'e:/www/docs/www/docs/junk/';

    define ( 'READ_ONLY', 1 );
    define ( 'IS_HIDDEN', 2 );
    define ( 'IS_SYSTEM', 4 );

    $hold = array ();

    $obj = new COM ( 'scripting.filesystemobject' );

    if ( is_object ( $obj ) )
    {
        $all = $obj->getfolder ( $dir );

        foreach ( $all->subfolders AS $directory )
        {
            if ( substr ( $directory->name, - 3 ) != 'cnf' )
            {
                $hold[$dir . $directory->name . '/'] =  array ( 'read_only' => ( ( $directory->Attributes & READ_ONLY ) <> 0 ? 1 : 0 ), 'is_hidden' => ( ( $directory->Attributes & IS_HIDDEN ) <> 0 ? 1 : 0 ), 'is_system' => ( ( $directory->Attributes & IS_SYSTEM ) <> 0 ? 1 : 0 ) );
            }
        }

        $obj = null;
    }
    else
    {
        echo 'sorry can not crate object: file_system_object';
    }

    print_r ( $hold );
?>

To do the same thing for * files * with in the directory named in $dir, change above ( foreach() ) to what is below...

        foreach ( $all->files AS $file )
        {
            $hold[$file->name] =  array ( 'read_only' => ( ( $file->Attributes & READ_ONLY ) <> 0 ? 1 : 0 ), 'is_hidden' => ( ( $file->Attributes & IS_HIDDEN ) <> 0 ? 1 : 0 ), 'is_system' => ( ( $file->Attributes & IS_SYSTEM ) <> 0 ? 1 : 0 ) );
        }

pretty much anything you could think of doing in windows can be done using PHP and COM...

John
csaba at alum dot mit dot edu 04-Mar-2005 02:21
Getting IE to the foreground
If you are using a command line (CLI) version of PHP on a Win32 platform (e.g. XP Pro, SP2), you might want to have output directed to IE (perhaps you'll want to work with the output there) when Popup does not suffice (see my earlier post, below).

It's easy enough to get an instance of IE using $ie = new COM("InternetExplorer.Application");.  The problem is, you don't necessarily see it in the foreground (especially if you already have one open) and who wants to waste keystrokes getting to it?  The code below has been working for me (If you want to do other adjustments (e.g. $ie->Document->ParentWindow->resizeTo(800,500); or $ie->Document->Body->bgColor = "yellow";), doing them before the $ie->Visible = true; line will avoid screen distractions):

<?php
function newIEtoForeground($title, $evtPrefix="") {
    // brings new instance of IE to foreground with title $title
    if (!$extPrefix) $ie = new COM("InternetExplorer.Application");
    else $ie = new COM("InternetExplorer.Application", $evtPrefix);
    $ie->Navigate2("about:blank");
    $oWSH = new COM("WScript.Shell");
    while ($ie->ReadyState!=4) usleep(10000);

    $ie->Document->Title = ($tmpTitle = mt_rand());  //unique title
    $ie->Visible = true;
    while (!$oWSH->AppActivate("$tmpTitle - M")) usleep(10000);

    $ie->Document->Title = $title;
    $ie->Document->ParentWindow->opener="me";  // allows self.close()
    return $ie;
}
?>

Csaba Gabor from Vienna
csaba at alum dot mit dot edu 14-Feb-2005 09:22
Basic Windows IO
If you are using a command line (CLI) version of PHP on a Win32 platform, you might like to have a simple graphical interface to get input and output.  The following gives an illustration for both.

<?php
    // first we get some input
    $oScript = new COM("MSScriptControl.ScriptControl");
    $oScript->Language = "VBScript";
    $title = "I/O Demo: Input half";
    $initial = "Change this value";
    $prompt = "Please enter a value";

    $code = <<<EOF
Function getInput()
    inVal = InputBox("$prompt", "$title", "$initial")
    getInput = inVal         'how return values are assigned in VB
End Function
EOF;

    $oScript->AddCode($code);
    $input = $oScript->Eval("getInput()");
    if (gettype($input)=="NULL") $input = "Input box was cancelled";

    // now we show some output
    $oWSH = new COM("WScript.Shell");
    $title = "I/O Demo: Output half";
    $timeout = 2;        // 0 for no timeout
    $style = 0 + 48;    // buttons to show + alert symbol
    $oWSH->Popup($input, $timeout, $title, $style);
?>

This is example is overblown for illustrative purposes.  The whole input part collapses if the amount of code used is a single statement.  So, this would have sufficed:
$code = "InputBox(\"$prompt\", \"$title\", \"$initial\")";
$input = $oScript->Eval($code);

This technique exposes quite a bit of scripting power tied into the Windows operating system and VBScript.  However, you should have a really specific reason for using it since VBScript tends to be dog slow when compared to PHP in my tests.  Simple IO like this is good, however, and the popup size can be quite large.  Furthermore, this could be a viable route to go for accessing WinAPI's.

http://www.ss64.com/wsh/popup.html shows some documentation for $oWSH->Popup

Csaba Gabor from Vienna
phpguy _from_ toshpro _dot_ com 12-Nov-2004 02:55
if you ever need to connect to an access database (on the server, using the IIS or APACHE user account), here is code below that can help to ease the process.  It is written like this because we have a MySQL as well as an Access database and need to be able to switch back and forth.  The runQuery function will return a 2 dimensional array and can use column names or numbers for the index of the internal arrays.  External arrays are always integer-indexed.  We also have found issues with Access staying open and growing from 7MB to 2.5GB within two hours, so we open and close it each time we run a query.  If it is not a select query it will return TRUE.  if it fails with errors, it will return false.

There is a showErr flag that we mostly only use for MySQL, since you can output MySQL errors without PHP errors.  Access doesn't appear to be that friendly.

<?php

function runQuery(&$conn,
                    $strSQL,
                    $associative=true,
                    $debug=false,
                    $showSQL=false,
                    $showErr=false) {
    return runMSQuery($conn,$strSQL,$associative,$debug,$showSQL,$showErr);
    //return runMyQuery($conn,$strSQL,$associative,$debug,$showSQL,$showErr);
}

function openMSDB($dbfile="c:/path/and/filename.mdb") {
    if (!@$conn = new COM("ADODB.Connection"))
        exit("Unable to create an ADODB connection<br>");
    $strConn = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".$dbfile;
    @$conn->open($strConn);
    if($conn->State == 0) return false;
    return $conn;
}

function closeMSDB(&$conn) {
    @$conn->Close();
    $conn = null;
    return true;
}

function runMSQuery(&$conn,
                    $strSQL,
                    $associative=true,
                    $debug=false,
                    $showSQL=false,
                    $showErr=false) {
    if(!is_object($conn)) if(!$conn=openMSDB()) return false;
    if( $showSQL || $debug ) echo $strSQL."\n<br>\n";
    $rtn = false;
    if($debug) $rs = $conn->execute($strSQL);
    else $rs = @$conn->execute($strSQL);

    if(!$rs) {
        if($showErr) echo "Error running SQL.<br>\n";
        closeMSDB($conn);
        return false;
    }
    if ($rs->State == 0) {
        closeMSDB($conn);
        return true;
    } else {
        $rows=0;
        while(!$rs->EOF) {
            for($i=0;$i<$rs->Fields->count;$i++) {
                $rsf = $rs->Fields($i);
                if($associative) $rtn[$rows][$rsf->Name] = $rsf->value;
                else $rtn[$rows][$i] = $rsf->value;
            }
            $rows++;
            $rs->MoveNext();
        }
        if($rs->State != 0) $rs->Close();
        $rs = null;
    }
    if($debug) var_dump($rtn);
    closeMSDB($conn);
    return $rtn;
}
?>
sodeh at dana dot ir 30-Oct-2004 12:11
for connection to sql server and know the utf-8 code
use this method to connect the database

$db = new COM("ADODB.Connection",NULL, 65001 );
flintjt at hotmail dot com 11-Mar-2004 08:45
I have searched for ways to open and read an excel document and I found some information which I thought that I would document.  Information on the object model to help with programming COM objects can be found in your MS Office help files, search for VBA*.chm.
Also, here is a script that I used to pull a cell from each worksheet, complete w/ variable for pathnames & worksheets.  This script also closes excel completely.  The coding might be dirty, but it gets the job done.
--------copy from here-----------
<pre>
<?PHP
$filename = "c:/spreadhseet/test.xls";
$sheet1 = 1;
$sheet2 = "sheet2";
$excel_app = new COM("Excel.application") or Die ("Did not connect");
print "Application name: {$excel_app->Application->value}\n" ;
print "Loaded version: {$excel_app->Application->version}\n";
$Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");
$Worksheet = $Workbook->Worksheets($sheet1);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("C4");
$excel_cell->activate;
$excel_result = $excel_cell->value;
print "$excel_result\n";
$Worksheet = $Workbook->Worksheets($sheet2);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("C4");
$excel_cell->activate;
$excel_result = $excel_cell->value;
print "$excel_result\n";
#To close all instances of excel:
$Workbook->Close;
unset($Worksheet);
unset($Workbook);
$excel_app->Workbooks->Close();
$excel_app->Quit();
unset($excel_app);
?>
</pre>
----------------------------------
jon at jonroig dot com 03-Dec-2003 09:28
Here's a simple example of printing formatted templates with MS Word.

To begin with, I've already built my invoice in Word and saved it as an RTF file.
In this case, I'm using "sampleInvoice.php"

Fields that will be replaced have been marked as ||CUSTOMER ID|| and so on.

<?php
$dataText = "";

// open the file
if(!($fp= fopen ("C:\wordTest\sampleInvoice.rtf", "r"))) die ("Can't open");
$dataText = fread($fp, 2000000);
fclose ($fp);

// replace the template fields
$dataText = str_replace ("||CUSTOMER ID||",$customerIDValue, $dataText);

// save the file as an rtf
$timeStamp = time();
$saveFile = "C:/wordTemp/".$timeStamp."-invoice-".$customerIDValue.".rtf";
if(!($fq= fopen ($saveFile, "w+"))) die ("Can't open");
fwrite ($fq, $dataText);
fclose ($fq);

// initialize the word components
$word = new COM("word.application") or die("Unable to instantiate Word");

print "Loaded word version ($word->Version)\n";
$word->visible = true;
$word->Documents->Add();

// open the file
$word->Documents->Open("$saveFile");

// output the file to the default printer
$word->ActiveDocument->PrintOut(1);

// shutdown word
$word->Quit();
?>
drich at nea-online dot net 18-Nov-2003 11:47
Changing Fields (Not FORMFIELDS) In MS Word....

Searched most of the afternoon for this, as it is something needed here for our projects.  Maybe this can help someone else as well.

<?php
$input = "x:\\path\\to\\test.doc";
$word = @new COM("word.application") or die("Unable to instantiate Word");
print "Loaded Word, version {$word->Version}\n<br>";
$word->Visible = 1;
print "Set Word To Visible<br>";
$word->Documents->Open($input);
print "Opened $input<br>";
$word->Activate;
print "Activated Word<br>";

print "Editing Fields<br>";
$Field = $word->ActiveDocument->Fields(1);
$fieldData = $Field->Result;
$fieldData->Text = "Testing";

// Print the document.
$word->Printout();

// closing word
$word->Quit();

// free the object
$word->Release();
$word = null;

?>
Mihael Peternelj; Slovenia 18-Oct-2003 02:22
If you wnat to connect to Access MDB Library you can use this small "progy". It works so, that if someone other accesses the library, it waits for so long to access it again because (as i know) MDB librarys can be accessed only once at the same time.

<?php
function mdb($sql, $array = 0, $base = "Base.mdb", $timelimit = 5)
    {
    $r_items = array();
    $c = new COM("ADODB.Connection");
    $r = false;
    $time = time();
    $process = $i = 0;
    while($r == false && $process <= $timelimit)
        {
        $deb = $c->open('DRIVER={Microsoft Access Driver (*.mdb)}; Dbq='. realpath($base) .';');
        $r = $c->execute($sql);
        $process = time() - $time;
        }
    if($process >= $timelimit) exit("mdbSQL Query Timeout!");
    if(strtolower(substr($sql, 0, 6)) == "insert" or strtolower(substr($sql, 0, 6)) == "update" or strtolower(substr($sql, 0, 6)) == "delete") return true;
    $num_fields = $r->fields->count();
    while (!$r->EOF)
        {
        for($j = 0; $j < $num_fields; $j++)
            {
            $key = $r->Fields($j);
            if($array == 0) $r_items[$i][$key->name] = $key->value;
            else $r_items[$i][$j] = $key->value;
            }
        $i++;
        $r->MoveNext();
        }
    $r->close();
    $c->close();
    $r = null;
    $c = null;
    return $r_items;
    }
?>

I got the source from the Manual, what i did is that i made it more compatible and user frendly. :)

I hope it could some day help someone so as it did me...

Greetings from Slovenia!

Mihael Peternelj
admin at CdnDomainRegistry dot ca 21-Sep-2003 12:57
Here's an example/comparison of VBScript vs PHP's COM component to connect to an IIS Webserver.
The examples backup the IIS MetaBase (your server's website(s) configuration) in IIS.

VBScript:
  Dim obj, iFlags
  Set obj = GetObject("IIS://LocalHost")
  ' Backup to next available version number.
  ' Set flags to save the metabase first and
  ' force the backup even if save fails.
  iFlags = (MD_BACKUP_SAVE_FIRST or MD_BACKUP_FORCE_BACKUP)
  obj.Backup "MyBackups", MD_BACKUP_NEXT_VERSION, iFlags
  ' Note: The above was copied from Microbucks.
  ' This is how you would have to do it.
  ' i.e. use the actual constant's value.
  iFlags = (2 or 4)
  obj.Backup "MyBackups", &HFFFFFFFF, iFlags

PHP:
  <?php
  $obj = new COM("IIS://LocalHost")or die("Unable to instantiate IIS");
  $err = com_invoke ($obj, "Backup", "MyBackups", "&HFFFFFFFF", 2|4 );
  $obj = null;
  print "err=".$err; // returns 0 on success
  ?>

Glen
20-Sep-2003 07:49
Example on how to read a registry value:

<?php
  $shell= &new COM('WScript.Shell');
  var_dump($shell->regRead('HKEY_CURRENT_USER\Environment\TEMP'));
?>

(outputs string(39) "%USERPROFILE%\Lokale Einstellungen\Temp" on my system)
rickardsjoquist at hotmail dot com 09-Jul-2003 07:33
If you want to use a DSN-less connection to a database (access for example) and return the results of a query as a multidimensional array with the fieldnames as identifier try this. Hope it helps someone :-)
--------------------------------------------------------

<?php
function db($sql) {
    $c = new COM("ADODB.Connection");
    $c->open('DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=' . realpath("relative_path/db.mdb"));
    $r = $c->execute($sql);
    $i = 0;
    $num_fields = $r->fields->count();
    while (!$r->EOF)
        {
            for($j = 0;$j<$num_fields;$j++) {
            $key = $r->Fields($j);
            $r_items[$i][$key->name] = $key->value;
        }
        $i++;
        $r->MoveNext();
    }
    $r->close();
    $c->close();
    $r = null;
    $c = null;
    return $r_items;
}
?>

--------------------------------------------------------
use it like this:
--------------------------------------------------------
<?php
$results = db("SELECT field_a, field_b FROM table");
foreach($result as $i => $item) {
    echo $item['field_a'];
    echo $item['field_b'];
}
?>
--------------------------------------------------------
You can also use: print_r($result); if you want the structure of the array to be printed.
info at sharedprojects dot nl 16-Jun-2003 06:41
<?php
// An other way to connect
// this connect to a ODBC-datasourced added in the
// ODBC Data Source Administrator under System DSN

$odbcname = "northwind";
$conn->Open("DSN=$odbcname");
?>
eguan1ao_at_yahoo_dot_com 20-Nov-2002 10:35
An example of connecting to a Microsoft Access database, executing an SQL query, and displaying the results in HTML.

<?php

$db = 'C:\\Program Files\\Microsoft Office\\Office\\Samples\\Northwind.mdb';

$conn = new COM('ADODB.Connection');

// Two ways to connect. Choose one.
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db");
//$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db");

$sql = 'SELECT   ProductName, QuantityPerUnit, UnitPrice
         FROM     Products
          ORDER BY ProductName';
$rs = $conn->Execute($sql);

?>

<table>
<tr>
          <th>Product Name</th>
         <th>Quantity Per Unit</th>
         <th>Unit Price</th>
</tr>
<?php while (!$rs->EOF): ?>
         <tr>
                  <td><?= $rs->Fields['ProductName']->Value ?></td>
                   <td><?= $rs->Fields['QuantityPerUnit']->Value ?></td>
                  <td><?= $rs->Fields['UnitPrice']->Value ?></td>
          </tr>
         <?php $rs->MoveNext() ?>
<?php endwhile ?>
</table>

<?php

$rs->Close();
$conn->Close();

?>
kmason at avaya dot com 30-Jul-2002 06:56
It should be noted that to run anything that needs to interact with the UI (the word example), one needs to ensure that the process that the Web server is running under can access the UI.  For Apache, this is an option in the Services control panel.
yinon at xacct dot com 06-Apr-2002 09:33
In order to get the Word exaple running, do the following on the server side.
Worked for me...
1. Click START-->RUN and enter "dcomcnfg"
2. In the "Applications" tab, go down to "Microsoft Word Document"
3. Click PROPERTIES button
4. Go to the "Security" Tab
5. Click "Use custom access permissions", and then click EDIT
6. Click ADD and then click SHOW USERS
7. Highlight the IIS anonymous user account (usually IUSR_<machinename>), click ADD
8. Go back to the "Security" tab by hitting OK
9. Click "Use custom launch permissions", and the click EDIT
10. Click ADD and then click SHOW USERS
11. Highlight the IIS anonymous user account (usually IUSR_<machinename>), click ADD
12. Hit OK, and then hit APPLY.

Also, you should look at the "Identity" tab in the Microsoft Word Document PROPERTIES and see that it is set to "Interactive User"

ALSO, log into the machine AS the IUSR_<machinename> account, start word, and make sure to click through the dialog boxes that Word shows the first time it is run for a certain user.  In other words, make sure Word opens cleanly for the IUSR_ user.

More useful information could be found here:
http://www.email-screen.com/support-doc2txt.html
Franky at boucheros dot com 19-Mar-2002 03:38
For Access to Ms Access
use another Provider like this:

<?php
$conn = new COM("ADODB.Connection")
or die("Cannot start ADO");
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=./thefile.mdb;");
$rs = $conn->Execute("SELECT * FROM thetable");
?>
mstaiger at informatik dot uni-siegen dot de 24-Jan-2002 03:17
The example above (MS Word) is quiet unsatisfying, as I find. Especially since I couldn't find anybody who actually got it running, including myself.

You might therefore give this Powerpoint-example a chance:
----------------------------------
#Instantiate a PowerPoint object.
$ppoint = new COM("PowerPoint.application") or die("Unable to instantiate PowerPoint");

<?php
#Create a new presentation
$ppoint->Presentations->Add() or die ("Could not create presentation");

//Add a slide
$slide=$ppoint->Presentations[1]->Slides->Add(1,1);

//Get the name of this slide
$slidename = $slide->Name();
echo"<br>slidename : $slidename";

//Change the name of this new slide
$slide->Name = "New Slidename";
$slidename = $slide->Name();
echo"<br>NEW slidename : $slidename";

//Save the presentation
$ppoint->Presentations[1]->SaveAs("c:/InetPub/www/test.ppt");
$ppoint->Presentations[1]->Close();

//closing powerpoint
$ppoint->Quit();

//free the object
$ppoint->Release();
$ppoint = null;
-----------------------
?>

the config is :
w2k-server/office2k/iis5/php 4.1.1 isapi

If you happen to run into a working word2k-example, please let me know.

Marc


The DOTNET class

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

Description

The DOTNET class allows you to instantiate a class from a .Net assembly and call its methods and access its properties.

$obj = new DOTNET("assembly", "classname")

Methods

DOTNET::__construct ( string $assembly_name , string $class_name [, int $codepage ] )

DOTNET class constructor. assembly_name specifies which assembly should be loaded, and class_name specifices which class in that assembly to instantiate. You may optionally specify a codepage to use for unicode string transformations; see the COM class for more details on code pages.

The returned object is an overloaded object, which means that PHP does not see any fixed methods as it does with regular classes; instead, any property or method accesses are passed through to COM and from there to DOTNET. In other words, the .Net object is mapped through the COM interoperability layer provided by the .Net runtime.

Once you have created a DOTNET object, PHP treats it identically to any other COM object; all the same rules apply.

Example #1 DOTNET example

<?php
 $stack = new DOTNET("mscorlib", "System.Collections.Stack");
 $stack->Push(".Net");
 $stack->Push("Hello ");
 echo $stack->Pop() . $stack->Pop();
?>

Note:

You need to install the .Net runtime on your web server to take advantage of this feature.

User Contributed Notes

juan156_elias at gmail dot com 05-Jun-2015 05:00
Using COM and DOTNET directly is quite a nightmare. DOTNET only allows you to target .Net 3.5 and below, and all the binaries need to be COM  Visible. This basically means that you will need to write your own .Net binaries for everything, at least wrappers.

There is a library out there (NetPhp) built on top of the COM class that will solve all these issues, so you can get code like this:

$manager = new \NetPhp\Core\NetManager();
$manager->RegisterAssembly('mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', 'mscorlib');
$manager->RegisterClass('mscorlib', 'System.IO.File', 'File');
$manager->RegisterClass('mscorlib', 'System.IO.FileOptions', 'FileOptions');
$file = $manager->Create('mscorlib', 'File');
$fileoptions = $manager->Create('mscorlib', 'System.IO.FileOptions')->Enum('Encrypted');

$file->Create("C:\\www\\prueba.tres", 2048, $fileoptions);
m dot atifmajeed at gmail dot com 11-Feb-2015 01:29
How to use donet class for creating object of parameterized constructor

http://php.net/manual/en/class.dotnet.php

private SGF ingerPrintManager m_FPM; //member variable
SGFPMDeviceName device_name = SGFPMDeviceName.DEV_ FDU02 ;
m_FPM = new SGF ingerPrintManager (device_name);
ricardopsj at gmail dot com 14-Apr-2014 10:30
Create an Excel Workbook using DOTNET.

<?php

$full_assembly_string = 'Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c';
$full_class_name = 'Microsoft.Office.Interop.Excel.ApplicationClass';

$e = new DOTNET($full_assembly_string, $full_class_name);
$wb = $e->workbooks->add();
$Precios = $wb->Worksheets(1);
$Precios->Name = 'Precios';
$Venta = $wb->Worksheets(2);
$Venta->Name = 'Venta';
$Tons = $wb->Worksheets(3);
$Tons->Name = 'Tons';

$Meses = Array('2014-01', '2014-02', '2014-03', '2014-04', '2014-05', '2014-06', '2014-07', '2014-08', '2014-09', '2014-10', '2014-11', '2014-12');
foreach ($Meses as $Numero => $Mes) {
   $Precios->Range("A" . ($Numero+1))->Value = $Mes;
}

$wb->SaveAs('c:\temp\Meta.2014.05.xlsx');
$wb->Close();

?>

Go to c:\windows\assembly to know what value to put in $full_assembly_string.

If you don't know the assembly, use http://www.red-gate.com/products/dotnet-development/reflector/ to browse it, use what you learn there to fill $full_class_name.

Enjoy,

Ricardo.
jan dot zahradnik at data3s dot com 06-Jan-2014 06:17
PHP searches only GAC for .NET 2.0 runtime, so your DLL has to be targeted 2.0 - 3.5 framework.

Also it recognize only classes, not stucts, so you can't instantiate DateTime for example.
sandips (yardi) 29-Jun-2011 11:57
exercise to try different type of method signature and calling them in php.

VB.net Code
Public Class Class1
    Public Function SayHello(ByVal input As String) As String
        Return "Php input: " & input & "</BR>Dot net library said- 'Hello'"
    End Function
    Public Function ConcatArray(ByVal Values As Object()) As String
        Dim ret As String = String.Empty
        For Each sval As Object In Values
            ret = ret & sval.ToString & " "
        Next
        Return ret.Substring(1, ret.Length - 1)
    End Function
    Public Function ReturnObject(ByVal FirstName As String, ByVal LastName As String) As Name
        Dim obj As New Name
        obj.FirstName = FirstName
        obj.LastName = LastName
        Return obj
    End Function
'To explain Byref doesn't work with php
    Public Function CallByRef(ByRef value As String) As String
        value = "Value to ByRef parameter set in dot net."
        Return "ByRef CallByRef() method called."
    End Function
End Class

Public Class Name
    Public FirstName As String = String.Empty
    Public LastName As String = String.Empty
End Class

Php code to test .net library
<?php
  $class1 = new DOTNET("DotNetTest,"
              ."Version=1.0.0.0,"
                          ."Culture=neutral,"
                      ."PublicKeyToken=6675daefc27eafc4"
              ,"DotNetTest.Class1");
 echo $class1->SayHello("Hi PHP!!!!");
 echo "</P>";

 $parameter[0] = 12345;
 $parameter[1] = "MyString";
 $parameter[2] = false;
 $ret = $class1 -> ConcatArray($parameter);
 echo $ret;
 echo "</P>";

 $Obj = $class1 -> ReturnObject("Sandip","Shimpi");
 echo $Obj->FirstName." ".$Obj->LastName;

 echo "</P>";
 $myByRef = "";
 echo "Value of ByRef Variable before calling .net CallByRef() method: ".$myByRef;
 echo "</BR>";
 echo $class1 -> CallByRef($myByRef);
 echo "</BR>";
 echo "Now value of ByRef Variable: ".$myByRef;
 echo $myByRef;
?>
Anonymous 05-Jul-2010 02:17
As suggested before, you'll likely have to use a full assembly string including the assembly name, the assembly version (not the dll file version), the culture, and the public key token.  Otherwise, you'll get a 'file not found' catchable error.  To quickly find this on your own system to get the ball rolling, open C:\Windows\assembly in explorer (not in cmd as that shows a different view), scroll down the list until you find the assembly you want to work with, hover the mouse over it, and the tooltip shows the exact string to use (though processcorArchitecture if shown is optional).  You'll have to use .net utilities or other methods to automate collecting this info on other machines.

You'll also have to use the fully-qualified class name.  In other words, if you want to use the 'Form' class within 'System.Windows.Forms', you have to use the full class hierarchy 'System.Windows.Forms.Form'.  Otherwise you'll get an unhelpful 'Failed to instantiate .Net object' catchable error.

The following example shows the correct syntax to use the 'Form' class to pop up a blank form from php-cli, which can be used in leu of many Windows GUI PHP extensions out there.  Replace Version with your local version, and PublicKeyToken with your own local key token.

<?php

$full_assembly_string = 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=a8425bc35256e463';
$full_class_name = 'System.Windows.Forms.Form';
$form = new DOTNET($full_assembly_string, $full_class_name);

// code to add buttons, menus, text, etc

$form->Show();

$form_event = '';
while($form_event !== 'close') {

  // handle form functions and events

  }
?>
murat at muratyaman dot co dot uk 12-Feb-2010 02:46
First, I hit the same mine:

"... Failed to instantiate .Net object [Unwrapped, QI for IDispatch] [0x80004002] No such interface supported ..."

In order to load the assembly and class in it successfully, I had to change my AssemblyInfo.cs file regarding the visibility of the types in it.

// some code above

// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible(true)]

// some code below
m holmes at windowlogic dot com dot au 22-Sep-2009 06:31
For strongly-named NET assemblies that are registered in the GAC, you can just use the assembly name
e.g: $x = new DOTNET ("myAssembly", "myClass");

For strongly-named NET assemblies that aren't registered in the GAC, you need to use the full assembly string
e.g. $x = new DOTNET('myAssembly, Version=X.X.X.X, Culture=neutral, PublicKeyToken=ZZZZZZZZZZZZ', 'myClass');

You can't instantiate assemblies that haven't been strongly named.

"Strongly named" means that the assembly has a public key. To strongly name your own classes in Visual Studio, go to the Signing tab in the project properties and click the 'sign the assembly' box and choose a key file name.

To register an assembly in the GAC, there are various tools around to do that, but the easiest method is to drag-and-drop the compiled assembly into c:\windows\assembly using windows explorer (a shell extension is installed by default that handles registering dragged files).
kschroeder at mirageworks dot com 08-Apr-2009 08:55
In order to find out what the PublicKeyToken value is, open up Explorer and go to c:\windows\assembly.  In that directory you will see all of the registered assemblies along with their PublicKeyToken value.
sam at bitopia dot co dot uk 02-May-2008 05:54
If you want to load any other assembly apart from mscorlib, you'll need to include it like the following

$x = new DOTNET('The.Assembly.Name, Version=X.X.X.X, Culture=neutral, PublicKeyToken=ZZZZZZZZZZZZ', 'The.Class.Name');

Replace all relevant details.


VARIANT class

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

Description

The VARIANT is COM's equivalent of the PHP zval; it is a structure that can contain a value with a range of different possible types. The VARIANT class provided by the COM extension allows you to have more control over the way that PHP passes values to and from COM.

$vVar = new VARIANT($var)

Methods

VARIANT::__construct ([ mixed $value [, int $type [, int $codepage ]]] )

VARIANT class constructor. Parameters:

value
initial value. if omitted, or set to NULL an VT_EMPTY object is created.
type
specifies the content type of the VARIANT object. Possible values are one of the VT_XXX 예약 상수. In PHP versions prior to PHP 5, you could force PHP to pass a variant object by reference by OR'ing VT_BYREF with the type. In PHP 5, this hack is not supported; instead, PHP 5 can detect parameters passed by reference automatically; they do not even need to be passed as VARIANT objects. Consult the MSDN library for additional information on the VARIANT type.
codepage
specifies the codepage that is used to convert strings to unicode. See the parameter of the same name in the COM class for more information.

PHP versions prior to PHP 5 define a number of (undocumented) virtual properties for instances of the VARIANT class; these properties have all been removed in PHP 5 in favour of its more natural syntax; these differences are best highlighted by example:

Example #1 Variant example, PHP 4.x style

<?php
$v = new VARIANT(42);
print "The type is " . $v->type . "<br/>";
print "The value is " . $v->value . "<br/>";
?>

Example #2 Variant example, PHP 5 style

<?php
$v = new VARIANT(42);
print "The type is " . variant_get_type($v) . "<br/>";
print "The value is " . $v . "<br/>";
?>

The reason for the change is that, internally, the COM extension sees VARIANT, COM and DOTNET classes as the same thing, and the design philosophy for these classes is that all property and member accesses are passed through to COM with no interference. The new syntax is more natural and less effort, and most of the removed virtual properties didn't make any sense in a PHP context in any case.

Note:

PHP 5 takes a much simpler approach to handling VARIANTs; when returning a value or fetching a variant property, the variant is converted to a PHP value only when there is a direct mapping between the types that would not result in a loss of information. In all other cases, the result is returned as an instance of the VARIANT class. You can force PHP to convert or evaluate the variant as a PHP native type by using a casting operator explicitly, or implicitly casting to a string by printing it. You may use the wide range of variant functions to perform arithmetic operations on variants without forcing a conversion or risking a loss of data.

See also variant_get_type().

User Contributed Notes

darren at dcook dot org 17-Jul-2007 02:59
If you are frustrated that print_r($obj) (where $obj is something returned from a call to a function on a COM object) does not return anything helpful, and that variant_get_type($obj) just returns a number, the function you are actually after is:
  com_print_typeinfo($obj);

It lists all functions, variables, their types in a human-readable (well, programmer-readable) format. Lovely!
mark dot pearson at capita dot co dot uk 29-Oct-2003 08:51
Running PHP 4.3.2 on Windows 2000 I had to use the following expression to create an empty Variant:

<?php
 $empty = new Variant(null);
 print $empty->type //     ==>   1
?>

NOT

<?php
 $empty = new Variant();
 print $empty->type //     ==>   0
?>

The two expressions return different Variant type values!
richard dot quadling at carval dot co dot uk 26-Feb-2003 02:50
With thanks to Harald Radi and Wez Furlong.

Some VBA functions have optional parameters. Sometimes the parameters you want to pass are not consecutive.

e.g.

GoTo What:=wdGoToBookmark, Name="BookMarkName"
GoTo(wdGoToBookmark,,,"BookMarkName)

In PHP, the "blank" parameters need to be empty.

Which is ...

<?php
// Some servers may have an auto timeout, so take as long as you want.
set_time_limit(0);

// Show all errors, warnings and notices whilst developing.
error_reporting(E_ALL);

// Used as a placeholder in certain COM functions where no parameter is required.
$empty = new VARIANT();

// Load the appropriate type library.
com_load_typelib('Word.Application');

// Create an object to use.
$word = new COM('word.application') or die('Unable to load Word');
print "Loaded Word, version {$word->Version}\n";

// Open a new document with bookmarks of YourName and YourAge.
$word->Documents->Open('C:/Unfilled.DOC');

// Fill in the information from the form.
$word->Selection->GoTo(wdGoToBookmark,$empty,$empty,'YourName'); // Note use of wdGoToBookmark, from the typelibrary and the use of $empty.
$word->Selection->TypeText($_GET['YourName']);

$word->Selection->GoTo(wdGoToBookmark,$empty,$empty,'YourAge');
$word->Selection->TypeText($_GET['YourAge']);

// Save it, close word and finish.
$word->Documents[1]->SaveAs("C:/{$_GET['YourName']}.doc");
$word->Quit();
$word->Release();
$word = null;
print "Word closed.\n";
?>

The example document is ...

Hello [Bookmark of YourName], you are [Bookmark of YourAge] years old.

and it would be called ...

word.php?YourName=Richard%20Quadling&YourAge=35

Regards,

Richard.
alain at samoun dot com 02-Sep-2001 10:37
<?php
# I think that we need some examples of this thing:
# Lets define a real variant:
$varREAL=  new Variant("9.34 is a real number",VT_R8);
print "Value:". $varREAL->value; # Will print 9.34
# Now an integer
  $varINT=  new Variant("9.34 Printed as an integer",VT_INT);
print "Value:". $varINT->value; # Will print 9
# Now a string
  $varSTR=  new Variant("9.34 Printed as a string",VT_BSTR);
print "Value:". $varSTR->value; # Will 9.34 Printed as a string

?>


COM 함수 목록

참고

For further information on COM read the » COM specification or perhaps take a look at Don Box's » Yet Another COM Library (YACL). You might find some additional useful information in our FAQ for PHP와 COM. If you're thinking of using MS Office applications on the server side, you should read the information here: » Considerations for Server-Side Automation of Office.

Table of Contents

  • com_create_guid — Generate a globally unique identifier (GUID)
  • com_event_sink — Connect events from a COM object to a PHP object
  • com_get_active_object — Returns a handle to an already running instance of a COM object
  • com_load_typelib — Loads a Typelib
  • com_message_pump — Process COM messages, sleeping for up to timeoutms milliseconds
  • com_print_typeinfo — Print out a PHP class definition for a dispatchable interface
  • variant_abs — Returns the absolute value of a variant
  • variant_add — "Adds" two variant values together and returns the result
  • variant_and — Performs a bitwise AND operation between two variants
  • variant_cast — Convert a variant into a new variant object of another type
  • variant_cat — concatenates two variant values together and returns the result
  • variant_cmp — Compares two variants
  • variant_date_from_timestamp — Returns a variant date representation of a Unix timestamp
  • variant_date_to_timestamp — Converts a variant date/time value to Unix timestamp
  • variant_div — Returns the result from dividing two variants
  • variant_eqv — Performs a bitwise equivalence on two variants
  • variant_fix — Returns the integer portion of a variant
  • variant_get_type — Returns the type of a variant object
  • variant_idiv — Converts variants to integers and then returns the result from dividing them
  • variant_imp — Performs a bitwise implication on two variants
  • variant_int — Returns the integer portion of a variant
  • variant_mod — Divides two variants and returns only the remainder
  • variant_mul — Multiplies the values of the two variants
  • variant_neg — Performs logical negation on a variant
  • variant_not — Performs bitwise not negation on a variant
  • variant_or — Performs a logical disjunction on two variants
  • variant_pow — Returns the result of performing the power function with two variants
  • variant_round — Rounds a variant to the specified number of decimal places
  • variant_set_type — Convert a variant into another type "in-place"
  • variant_set — Assigns a new value for a variant object
  • variant_sub — Subtracts the value of the right variant from the left variant value
  • variant_xor — Performs a logical exclusion on two variants

User Contributed Notes

Sorin Negulescu 05-Nov-2016 07:49
I have spent a lot of hours trying to make word.application work on a Windows Server using Apache as a service.
The application was working if Apache was started from command line (not as a service).

In the hope that this will help others not to lose hours of trying to solve this problem here is what I found out:

- In php.ini make sure you have the following key under the [COM_DOT_NET]: extension=php_com_dotnet.dll
- Apache shall run under a normal user account (not SYSTEM account). Run services.msc and fill-in the info of the user under the Apache service's Properties tab.
- Make sure you are able to start MS Word normally under that user account and dismiss all the dialogs that may appear (license, your info with initials, etc.). Start Word once again and make sure no dialogs appear.
- Launch dcomcnfg.exe->Console Root->Component Services->Computers->My Computer->DCOM Config->Microsoft Office Word 97 - 2003->Right Click(Properties)->Identity Tab and fill in the same user account information as for Apache service.
- If you cannot find the Microsoft Office Word 97 - 2003, make sure you are launching the correct DCOM Config (64 bit vs 32 bit) depending on what version of Office you have installed. To launch the 32 bit version run: mmc comexp.msc /32

Before following the steps described above, the winword.exe app appears in task manager but, if you look at the memory consumption you can notice two things:
- it increases slowly
- it stops usually at about 7-8 MB.

Keywords: COM, word.application, Apache, service, winword, timeout, no error
codeslinger at compsalot dot com 18-Dec-2009 11:59
it took me awhile to track this down, the website given below is long gone, and it was hard to find the new site, so thought I would post it here.

Here is the current website for DynamicWrapper
http://freenet-homepage.de/gborn/WSHBazaar/WSHDynaCall.htm

DynamicWrapper and WinBinder are dlls which enable php to call windows api's directly.

see notes below for an example of using DynamicWrapper.

It is an interesting alternative to http://www.winbinder.org  which does similar things.
larrywalker at at chelseagroton dot com 02-Sep-2008 05:53
To run a php script in a background process and pass it you var use

<?php

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("c:\\xampp\\php\\php.exe

c:\\xampp\\Not_on_Web\\ftptestback.php --user=$username", 7, false);

?>

Works for me.
monica at digitaldesignservices dot com 27-Dec-2007 11:41
Working with Word 2003 COM object.

Have you been trying to work with the Word.Application COM and the Word Document would crash and the PHP script would hang?

Turns out that there is an issue within the Office COM object that doesn't let the web application (IIS, Apache) scripting engine release the object after instancing the COM.  The Service Pack 3 office update claims to remedy this.  Will test and post findings.
tomfmason at nospam-gmail dot com 08-Oct-2007 04:26
To get the cpu load percentage you can do something like this.

<?php
$wmi = new COM('winmgmts://');
$processor = $wmi->ExecQuery("SELECT * FROM Win32_Processor");
foreach($processor as $obj){
    $cpu_load_time = $obj->LoadPercentage;
}
echo $cpu_load_time;
?>

reference http://msdn2.microsoft.com/en-us/library/aa394373.aspx

To list current apache instances

<?php
$wmi = new COM('winmgmts://');
$processes = $wmi->ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'httpd.exe'");
foreach($processes as $process){
    echo $process->CommandLine . "<br />";
    echo $process->ProcessId . "<br />";
}
?>

reference http://msdn2.microsoft.com/en-us/library/aa394372.aspx

To run a php script in a background process

<?php
$dir = "C:\\path\\to\\dir";
$php_path = "C:\\path\\to\\php.exe";
$file = "somescript.php";
//send time current timestamp
$cmd_options = "-t " . time();
$wscript = new COM('WScript.Shell');
$wscript->Run("cmd /K CD $php_path $dir\\$file  &  ", 0, false);
?>

Enjoy

Tom
sk89q 28-Sep-2007 08:13
The only way I've been able to call Win32 API functions with PHP5 has been through COM and DynamicWrapper.

Here's an example to play a beep sound with your computer speaker:
<?php
$com = new COM("DynamicWrapper");
$com->Register("KERNEL32.DLL", "Beep", "i=ll", "f=s", "r=l");
$com->Beep(5000, 100);
?>
naveed at php dot net 10-Mar-2007 02:24
Here is a spell check implementation that displays the suggestion list for each misspelled word.

<?php
function SpellCheck($input)
{
    $word=new COM("word.application") or die("Cannot create Word object");
    $word->Visible=false;
    $word->WindowState=2;
    $word->DisplayAlerts=false;
    $doc=$word->Documents->Add();
    $doc->Content=$input;
    $doc->CheckSpelling();
    $result= $doc->SpellingErrors->Count;
    if($result!=0)
    {
        echo "Input text contains misspelled words.\n\n";
        for($i=1; $i <= $result; $i++)
        {
            echo "Original Word: " .$doc->SpellingErrors[$i]->Text."\n";
            $list=$doc->SpellingErrors[$i]->GetSpellingSuggestions();
            echo "Suggestions: ";
            for($j=1; $j <= $list->Count; $j++)
            {
                $correct=$list->Item($j);
                echo $correct->Name.",";
            }
            echo "\n\n";
        }
    }
    else
    {
        echo "No spelling mistakes found.";
    }
    $word->ActiveDocument->Close(false);
    $word->Quit();
    $word->Release();
    $word=null;
}

$str="Hellu world. There is a spellling error in this sentence.";
SpellCheck($str);
?>
Francois-R dot Boyer at PolyMtl dot ca 14-Feb-2007 05:32
I had the problem when trying to call Access VBA scripts from PHP; the call was working but Access would never quit.  The problem was that the Apache server is running as SYSTEM, and not a user we normally use to run Office.  And as the first time a user runs Office, it asked the SYSTEM user to enter its name and initials! ;-)

To correct this problem: Stop Apache, go to Services, Apache, Properties, "Log On" tab, and check "Allow service to interact with desktop.  Restart Apache, then open your office application, in a page loaded by the Apache server, with a small PHP code like this, :

<?php
$app = new COM("Access.Application"); // or Word.Application or Excel.Application ...
$app->Visible = true; // so that we see the window on screen
?>

Put the name you want and quit the application.  Now your Office application should terminate correctly the next time you load it with a COM object in PHP.  You can stop Apache uncheck the "Allow service to interact with desktop", and restart Apache if you like.  I don't even require a Quit or anything to be sent to Access, it quits automatically when PHP terminates.

And for those who wish to know how I'm calling Access VBA scripts, instead of using ODBC or any other way to send SQL requests, which does not seem to work to call VBA scripts, I open the database as a COM object and it works fine:

<?php
$db_com = new COM("pathname of the file.mdb");
$result = $db_com->Application->Run("function_name", 'param1', 'param2', '...');
?>
wiliamfeijo at gmail dot com 05-Feb-2007 09:10
This code use a Crystal Reports file with a connection on
 Oracle 9i Database, suports manipulate and refresh data
 and export to PDF file:

------ REMEMBER ------
These functions are only available if you had instaled the
Crystal Reports Developer Edition. Catch the trial on the
http://www.businessobjects.com web site.

<?php
//------  Variables ------
$my_report = "C:\\rel_apontamento.rpt";
$my_pdf = "C:\\teste.pdf";

//------ Create a new COM Object of Crytal Reports XI ------
$ObjectFactory= new
 COM("CrystalReports11.ObjectFactory.1");

//------ Create a instance of library Application -------
$crapp
=$ObjectFactory->
CreateObject("CrystalDesignRunTime.Application.11");

//------ Open your rpt file ------
$creport = $crapp->OpenReport($my_report, 1);

//------ Connect to Oracle 9i DataBase ------
$crapp->LogOnServer('crdb_oracle.dll','YOUR_TNS',
'YOUR_TABLE','YOUR_LOGIN','YOUR_PASSWORD');

//------ Put the values that you want --------
$creport->RecordSelectionFormula=
"{YOUR_TABLE.FIELD}='ANY_VALUE'";

//------ This is very important. DiscardSavedData make a
 Refresh in your data -------
$creport->DiscardSavedData;

//------ Read the records :-P -------
$creport->ReadRecords();

//------ Export to PDF -------
$creport->ExportOptions->DiskFileName=$my_pdf;
$creport->ExportOptions->FormatType=31;
$creport->ExportOptions->DestinationType=1;
$creport->Export(false);

//------ Release the variables
$creport = null;
$crapp = null;
$ObjectFactory = null;
?>

It's functional and perfectly !!!!!!!

Thanks.....
03-Oct-2006 01:11
If someone want to get a COM object out of a DCOM object can do something like that:

<?php
  $dcom_obj = new COM('dacom.object','remotehost') or die("Unable to get DCOM object!");
  $com_obj = new Variant(NULL);
  $dcom_obj->Get_Module($com_obj); //user function which returns a custom IDispatch (casted as variant*)
?>

Hopefully this will help someone, because it took me quite long to figure this out.
kiotech at hotmail dot com 18-Jul-2006 03:35
For use parameters with PHP this is a Code Example

<?php
$ObjectFactory= New COM("CrystalReports11.ObjectFactory.1");
  $crapp = $ObjectFactory->CreateObject("CrystalDesignRunTime.Application");
  $creport = $crapp->OpenReport("//report1.rpt", 1);

      $z= $creport->ParameterFields(1)->SetCurrentValue("Mango");
      $z= $creport->ParameterFields(2)->SetCurrentValue(5000);

  $creport->ExportOptions->DiskFileName="reportin.pdf";
  $creport->ExportOptions->PDFExportAllPages=true;
  $creport->ExportOptions->DestinationType=1;   $creport->ExportOptions->FormatType=31;
  $creport->Export(false);

$len = filesize("reportin.pdf");
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=reportin.pdf");
readfile("reportin.pdf");
?>
a dot kulikov at pool4tool dot com 13-Mar-2006 06:36
In case you are wondering how to group rows or columns in the freshly created EXCEL files, then this may help you

<?php
   /***
    * Grouping Rows optically in Excel Using a COM Object
    *
    * That was not easy, I have spent several hours of trial and error to get
    * this thing to work!!!
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @since  13.03.2006
    *
    * @see    Open Excel, Hit Alt+F11, thne Hit F2 -- this is your COM bible
    ***/

   //starting excel
   $excel = new COM("excel.application") or die("Unable to instanciate excel");
   print "Loaded excel, version {$excel->Version}\n";

   //bring it to front
   #$excel->Visible = 1;//NOT

   //dont want alerts ... run silent
   $excel->DisplayAlerts = 0;

   //create a new workbook
   $wkb = $excel->Workbooks->Add();

   //select the default sheet
   $sheet=$wkb->Worksheets(1);

   //make it the active sheet
   $sheet->activate;

   //fill it with some bogus data
   for($row=1;$row<=7;$row++){
       for ($col=1;$col<=5;$col++){

          $sheet->activate;
          $cell=$sheet->Cells($row,$col);
          $cell->Activate;
          $cell->value = 'pool4tool 4eva ' . $row . ' ' . $col . ' ak';
       }//end of colcount for loop
   }

   ///////////
   // Select Rows 2 to 5
   $r = $sheet->Range("2:5")->Rows;

   // group them baby, yeah
   $r->Cells->Group;

   // save the new file
   $strPath = 'tfile.xls';
   if (file_exists($strPath)) {unlink($strPath);}
   $wkb->SaveAs($strPath);

   //close the book
   $wkb->Close(false);
   $excel->Workbooks->Close();

   //free up the RAM
   unset($sheet);

   //closing excel
   $excel->Quit();

   //free the object
   $excel = null;
?>
Pedro Heliodoro 05-Mar-2006 11:38
Using the Windows Media Player OCX and the latest snap of PHP 5.1 i was able to eject a CD-ROM drive

<?php
//create an instance of Windows Media Player
$mp = new COM("WMPlayer.OCX");
//ejects the first cd-rom on the drive list
$mp->cdromcollection->item(0)->eject();

?>
vedanta dot barooah at gmail dot com 29-Jan-2006 04:59
Microsoft provides two binaries to create windows services: INSTSRV.EXE and SRVANY.EXE, using these php scripts can be run as windows services.

More information is available at: http://support.microsoft.com/?kbid=137890
sadi at unicornsoftbd dot com 17-Nov-2005 01:18
There's no way to use static dlls in php. only way to use a dll in php is to make it a com object first and then using it in php like this

<?php
$NewCom=new COM("uni.example");
?>

once we created the COM object this can be used like any other php classes.
                       sadi
                       www.unicornsoftbd.com
allan666 at gmail dot com 29-Sep-2005 12:37
If you are using a COM object that opens windows or dialogs (like Office applications), those windows or dialogs will not appear and will stay invisible even if you use codes like

$word->visible = true;

To solve this problem, go to the web service properties (Administrative Tools) and click (in the Logon TAB) "Allow the Service to Iterate with desktop".

This explain lots of problems to close applications. When you try to close, the dialos asking to save the file must appear, but only in the option to iterate with desktop is on.

Hope it helps.
dazb@localhost 26-May-2005 10:53
For permissions problems, etc. including "Could Not Open Macro Storage" with Word;
How To Configure Office Applications to Run Under the Interactive User Account

http://theether.net/download/Microsoft/kb/288366.html

[EDITOR NOTE: As reported by "damir@example", the link originally provided in this note is dead. The note was updated with a new link sent from "damir". - Editor thiago AT php DOT net]
mpg123 at thinstall dot com 09-May-2005 10:21
I found the lack of good online docs about how to access a windows environment very frustrating.  I finally found something here: http://www.sitepoint.com/forums/showthread.php?t=92358 but it's a php4 way.  I cleaned up the code, added introspection for methods and voila.  Thanks and credit to the original posters.

<?php
// php translation of asp script by Bill Wooten from
// http://www.4guysfromrolla.com/webtech/082802-1.shtml
function showServices($vComputerName, $vClass)  {
    $objLocator = new COM("WbemScripting.SWbemLocator");

    if($vComputerName == "") $objService = $objLocator->ConnectServer();
    else                     $objService = $objLocator->ConnectServer($vComputerName);

    $objWEBM = $objService->Get($vClass);
    $objProp = $objWEBM->Properties_;
    $objMeth = $objWEBM->Methods_;

    foreach($objMeth as $methItem) echo "Method: " . $methItem->Name ."\r\n";

    echo "\r\n";

    $objWEBMCol = $objWEBM->Instances_();

    foreach($objWEBMCol as $objItem) {

        echo "[" . $objItem->Name . "]\r\n";

        foreach($objProp as $propItem) {
           $tmp = $propItem->Name;
           echo "$tmp: " . $objItem->$tmp . "\r\n";
        }

         echo "\r\n";
    }
}

// Test the function:
showServices("", "Win32_Processor");
showServices("", "Win32_LogicalDisk");
angelo [at] mandato <dot> com 25-Apr-2005 06:11
ADO + PHP5 + SQL DATE FIELDS

Using a COM("ADODB.Connection") object in PHP4 and PHP5 to return date fields in databases will have different results.

In PHP4, the date is returned as a string as represented in the database.  In my case, '2005-04-11 11:35:44'.

In PHP5, the date is returned as a VARIANT type, when printed or implicitly casted would result as '4/11/2005 11:35:44 AM'.

For this example, the solution is to use the variant conversion function variant_date_to_timestamp() to convert the value to a Unix timestamp.

Please refer to this as an example.  Other date types may be converted using one of the other variant_<convert> functions.
bruce at yourweb dot com dot au 24-Apr-2005 02:47
Simple convert xls to csv
<?php
// starting excel
$excel = new COM("excel.application") or die("Unable to instanciate excel");
print "Loaded excel, version {$excel->Version}\n";

//bring it to front
#$excel->Visible = 1;//NOT
//dont want alerts ... run silent
$excel->DisplayAlerts = 0;

//open  document
$excel->Workbooks->Open("C:\\mydir\\myfile.xls");
//XlFileFormat.xlcsv file format is 6
//saveas command (file,format ......)
$excel->Workbooks[1]->SaveAs("c:\\mydir\\myfile.csv",6);

//closing excel
$excel->Quit();

//free the object
$excel->Release();
$excel = null;
?>
Jack dot CHLiang at gmail dot com 11-Apr-2005 07:02
If you want to search an Excel file and don't connect with ODBC, you can try the function I provide. It will search a keyword in the Excel find and return its sheet name, text, field and the row which found the keyword.

<?php

// The example of print out the result
$result = array();
searchEXL("C:/test.xls", "test", $result);

foreach($result as $sheet => $rs){
    echo "Found at $sheet";

    echo "<table width=\"100%\" border=\"1\"><tr>";

    for($i = 0; $i < count($rs["FIELD"]); $i++)
        echo "<th>" . $rs["FIELD"][$i] . "</th>";

    echo "</tr>";

    for($i = 0; $i < count($rs["TEXT"]); $i++) {
        echo "<tr>";

        for($j = 0; $j < count($rs["FIELD"]); $j++)
            echo "<td>" . $rs["ROW"][$i][$j] . "</td>";

        echo "</tr>";
    }
    echo "</table>";

}

/**
 * @param $file string The excel file path
 * @param $keyword string The keyword
 * @param $result array The search result
 */

function searchEXL($file, $keyword, &$result) {
       $exlObj = new COM("Excel.Application") or Die ("Did not connect");
       $exlObj->Workbooks->Open($file);
       $exlBook = $exlObj->ActiveWorkBook;
       $exlSheets = $exlBook->Sheets;

       for($i = 1; $i <= $exlSheets->Count; $i++) {
           $exlSheet = $exlBook->WorkSheets($i);

           $sheetName = $exlSheet->Name;

           if($exlRange = $exlSheet->Cells->Find($keyword)) {
            $col = 1;
            while($fields = $exlSheet->Cells(1, $col)) {
                        if($fields->Text == "")
                    break;

                $result[$sheetName]["FIELD"][] = $fields->Text;
                $col++;
            }

            $firstAddress = $exlRange->Address;
            $finding = 1;
            $result[$sheetName]["TEXT"][] = $exlRange->Text;

            for($j = 1; $j <= count($result[$sheetName]["FIELD"]); $j++) {
                $cell = $exlSheet->Cells($exlRange->Row ,$j);
                $result[$sheetName]["ROW"][$finding - 1][$j - 1] = $cell->Text;
            }

            while($exlRange = $exlRange->Cells->Find($keyword)) {
                   if($exlRange->Address == $firstAddress)
                    break;

                $finding++;
                $result[$sheetName]["TEXT"][] = $exlRange->Text;

                for($j = 1; $j <= count($result[$sheetName]["FIELD"]); $j++) {
                    $cell = $exlSheet->Cells($exlRange->Row ,$j);
                    $result[$sheetName]["ROW"][$finding - 1][$j - 1] = $cell->Text;
                }

            }

           }

    }

   $exlBook->Close(false);
   unset($exlSheets);
   $exlObj->Workbooks->Close();
   unset($exlBook);
   $exlObj->Quit;
   unset($exlObj);
}
?>

For more information, please visit my blog site (written in Chinese)
http://www.microsmile.idv.tw/blog/index.php?p=77
michael at example dot com 23-Feb-2005 09:42
After alot of trouble with IIS 6/Windows 2003 and PHP 5.0.2 I figured out how to use Imagemagick COM+/OLE interface. Seems you have to recreate the COM object after each convert() otherwise it will sometimes fail with very strange errors, since the PHP COM interface is not as stable as the old ASP one apparently is.
pelegk2 at walla DOT co DOT il 13-Feb-2005 05:49
Printing to a network printer :
read this note for :

NOTE: Using COM with windows NT/2000/XP with apache as a service
- Run dcomcnfg.exe
- Find word application and click properties
- Click the Security tab
- Use Custom Access Permissions
- Add the user who runs the web server service
- Use Custom Launch permissions
- Add the user who runs the web server service

THEN

<?php
// starting word
$word = new COM("word.application") or die("Unable to instanciate Word");
print "Loaded Word, version {$word->Version}\n";

// bring it to front
$word->Visible = 1;
//this is for example a printer on my network
$word->ActivePrinter = "\\\\192.168.4.201\\printer1";

// Open a word document, or anything else that word
// can read
$input ="c:\\test.html";
$word->Documents->Open($input);

$word->ActiveDocument->PrintOut();

// closing word
$word->Documents[1]->Close(false);
$word->Quit();

// free the object
$word->Release();
$word = null;
unset($word);
?>
06-Jan-2005 06:11
if you want to add a new hyperlink with excel :

<?php
$cell=$sheet->Range('O'.$j);
$cell->Hyperlinks->Add($cell ,'here_your_file');
?>

I write this note because is very difficult to find
the good method to do that .

Merci beaucoup
sorin dot andrei at mymail dot ro 25-Sep-2004 11:24
[Editor's Note:
Serializing the object, storing it and then (on the next page) unserializing it WILL work.
End Note]

the big problem for PHP is it can't save in session an COM object

an samples code is here. This works just first time, then give the error:
Call to undefined method com::Refresh() on line  $mMap->Refresh();
the reson is he can't save the object $mMap in session

<?php
    session_start();
      if (isset($_SESSION['map'])) {
            $mMap = $_SESSION['map'];
        }
        else
        {
        $ArcIMSConnector = new COM ("aims.ArcIMSConnector")  or die("can not create AIMS message object");
            echo "Loaded ArcIMSConnector, <br>";
            $ArcIMSConnector->ServerName = "map.sdsu.edu";
            $ArcIMSConnector->ServerPort = 5300;

            $mMap = new COM ("aims.Map")  or die("can not create aims.Map message object");
            echo "Loaded aims.Map, <br>";
            print "AIMS-ServerName= $ArcIMSConnector->ServerName <br>";

            $resultInit= $mMap->InitMap($ArcIMSConnector,"Worldgeography");

            $mMap->Width    = 400;            //                                'Width of the map in pixels
            $mMap->Height   = 300;            //                                'Height of the map in pixels
            $mMap->BackColor = 15130848;
            $_SESSION['map'] = $mMap;
        }

    $resultRefresh = $mMap->Refresh();                                //Refresh the map
    $urlImage      = $mMap->GetImageAsUrl();
    print "<br>urlImage=$urlImage<br>";

    print "<br><IMG SRC='$urlImage'>";

/*
    $ArcIMSConnector->Release();
    $ArcIMSConnector = null;

    $mMap->Release();
    $mMap = null;
*/

?>
ferozzahid [at] usa [dot] com 24-Aug-2004 09:06
To pass a parameter by reference to a COM function, you need to pass VARIANT to it. Common data types like integers and strings will not work for it.

As an example, calling a function that retrieves the name of a person will look like:

<?php
$Name = new VARIANT;

$comobj = new COM("MyCOMOBj.Component") or die("Couldn't create the COM Component");

if(!$comobj->GetName($Name)) {
    echo("Could not retrieve name");
}
else {
    echo("The name retrieved is : " . $Name->value);
}
?>

$Name->type will contain the type of the value stored in the VARIANT e.g. VT_BSTR.

Note For PHP 5:

Insted of '$Name->value', we can use only '$Name' for getting the value stored in the VARIANT. To get the type of the value stored in the VARIANT, use 'variant_get_type($Name)'.

Feroz Zahid
Shawn Coppock 11-Aug-2004 10:15
For those of us that are not experts with object models, if you have Microsoft Excel or Microsoft Word available, go into the Visual Basic Editor (Alt+F11). Now you can open the object browser window (F2) and see what you find.

If you select "Word" in the object browser, then the classes and members of that class are displayed below. For example, if you click on "Selection" in the classes column, and then "Font" in the members column, you will then see "Property Font As Font Member of Word.Selection" displayed at the bottom. Click on the "Font" link there.

Click on Word "Selection" again in the classes column, and then "PageSetup" in the members column, you will then see "Property PageSetup As PageSetup Member of word.Selection" displayed at the bottom. Click on the "PageSetup" link there.

Using the above examples, you can now formulate your Word Document from php. Here is a simple example that creates a 1 & 1/2" space for text based on the margin settings & with dark red text and an 8pt Helvetica font face:

<?php
$content = "Insert Sample Text Here\n\nThis starts a new paragraph line.";
$word= new COM("word.application") or die("Unable to create Word document");
print "Loaded Word, version {$word->Version}\n";
$word->Visible = 0;
$word->Documents->Add();
$word->Selection->PageSetup->LeftMargin = '3"';
$word->Selection->PageSetup->RightMargin = '4"';
$word->Selection->Font->Name = 'Helvetica';
$word->Selection->Font->Size = 8;
$word->Selection->Font->ColorIndex= 13; //wdDarkRed = 13
$word->Selection->TypeText("$content");
$word->Documents[1]->SaveAs("some_tst.doc");
$word->quit();
echo "done";
?>

This example (and others) worked for me with Microsoft Word 10.0 Object Library (MS Word 2002), Apache & PHP on Windows XP. Experimenting using the Visual Basic editor will yield some amazing documents from PHP as it will help you see the relationships of the objects used and give you an idea as to how to construct your code. But be prepared for an occaisional PHP.exe crash if your code is way off base.

Hope this helps!
Jason Neal 21-Jul-2004 09:48
If you are having issues with the outlook.application locking up when you try to access it, ensure that your apache server is running in the context of a user on the machine instead of the system account.
angelo [at] mandato <dot> com 03-Jun-2004 08:37
Useful PHP MSWord class I created to do some simple file conversions.  This class could have a lot more to it but I am not familiar with all the COM MS Word function calls.
<?php
    // msword.inc.php

    // NOTE: Using COM with windows NT/2000/XP with apache as a service
    // - Run dcomcnfg.exe
    // - Find word application and click properties
    // - Click the Security tab
    // - Use Custom Access Permissions
    //      - Add the user who runs the web server service
    // - Use Custom Launch permissions
    //      - Add the user who runs the web server service

    $wdFormatDocument = 0;
    $wdFormatTemplate = 1;
    $wdFormatText = 2;
    $wdFormatTextLineBreaks = 3;
    $wdFormatDOSText = 4;
    $wdFormatDOSTextLineBreaks = 5;
    $wdFormatRTF = 6;
    $wdFormatUnicodeText = 7;
    $wdFormatHTML=8;

    class MSWord
    {
        // Vars:
        var $handle;

        // Create COM instance to word
        function MSWord($Visible = false)
        {
            $this->handle = new COM("word.application") or die("Unable to instanciate Word");
            $this->handle->Visible = $Visible;
        }

        // Open existing document
        function Open($File)
        {
            $this->handle->Documents->Open($File);
        }

        // Create new document
        function NewDocument()
        {
            $this->handle->Documents->Add();
        }

        // Write text to active document
        function WriteText( $Text )
        {
            $this->handle->Selection->Typetext( $Text );
        }

        // Number of documents open
        function DocumentCount()
        {
            return $this->handle->Documents->Count;
        }

        // Save document as another file and/or format
        function SaveAs($File, $Format = 0 )
        {
            $this->handle->ActiveDocument->SaveAs($File, $Format);
        }

        // Save active document
        function Save()
        {
            $this->handle->ActiveDocument->Save();
        }

        // close active document.
        function Close()
        {
            $this->handle->ActiveDocument->Close();
        }

        // Get word version
        function GetVersion()
        {
            return $this->handle->Version;
        }

        // get handle to word
        function GetHandle()
        {
            return $this->handle;
        }

        // Clean up instance with word
        function Quit()
        {
            if( $this->handle )
            {
                // close word
                $this->handle->Quit();

                // free the object
                $this->handle->Release();
                $this->handle = null;
            }
        }
    };

?>

Example 1, opens an html file, writes text to it, then saves it as a document:
<?php
    $input = "C:\\test.htm";
    $output = "C:\\test.doc";

    $Word = new MSWord;
    $Word->Open($input);
    $Word->WriteText("This is a test ");
    $Word->SaveAs($output);
    $Word->Quit();
?>

Example 2, opens an html file, then saves it as a rtf file:
<?php
    $input = "C:\\test.htm";
    $output = "C:\\test.rtf";

    $Word = new MSWord;
    $Word->Open($input);
    $Word->SaveAs($output, $wdFormatRTF);
    $Word->Quit();
?>
angelo [at] mandato <dot> com 19-May-2004 01:30
Useful PHP functions I created (similar to other standard PHP database functions) for working with an ADO datasource:
<?php
    // ado.inc.php

    $ADO_NUM = 1;
    $ADO_ASSOC = 2;
    $ADO_BOTH = 3;

    function ado_connect( $dsn )
    {
        $link = new COM("ADODB.Connection");
        $link->Open($dsn);
        return $link;
    }

    function ado_close( $link )
    {
        $link->Close();
    }

    function ado_num_fields( $rs )
    {
        return $rs->Fields->Count;
    }

    function ado_error($link)
    {
        return $link->Errors[$link->Errors->Count-1]->Number;
    }

    function ado_errormsg($link)
    {
        return $link->Errors[$link->Errors->Count-1]->Description;
    }

    function ado_fetch_array( $rs, $result_type,  $row_number = -1 )
    {
        global $ADO_NUM, $ADO_ASSOC, $ADO_BOTH;
        if( $row_number > -1 ) // Defined in adoint.h - adBookmarkFirst    = 1
            $rs->Move( $row_number, 1 );

        if( $rs->EOF )
            return false;

        $ToReturn = array();
        for( $x = 0; $x < ado_num_fields($rs); $x++ )
        {
            if( $result_type == $ADO_NUM || $result_type == $ADO_BOTH )
                $ToReturn[ $x ] = $rs->Fields[$x]->Value;
            if( $result_type == $ADO_ASSOC || $result_type == $ADO_BOTH )
                $ToReturn[ $rs->Fields[$x]->Name ] = $rs->Fields[$x]->Value;
        }
        $rs->MoveNext();
        return $ToReturn;
    }

    function ado_num_rows( $rs )
    {
        return $rs->RecordCount;
    }

    function ado_free_result( $rs )
    {
        $rs->Close();
    }

    function ado_query( $link, $query )
    {
        return $link->Execute($query);
    }

    function ado_fetch_assoc( $rs,  $row_number = -1 )
    {
        global $ADO_ASSOC;
        return  ado_fetch_array( $rs, $ADO_ASSOC, $row_number);
    }

    function ado_fetch_row( $rs,  $row_number = -1 )
    {
        global $ADO_NUM;
        return ado_fetch_array( $rs, $ADO_NUM, $row_number);
    }

    // Extra functions:
    function ado_field_len( $rs, $field_number )
    {
        return $rs->Fields[$field_number]->Precision;
    }

    function ado_field_name( $rs, $field_number )
    {
        return $rs->Fields[$field_number]->Name;
    }

    function ado_field_scale( $rs, $field_number )
    {
        return $rs->Fields[$field_number]->NumericScale;
    }

    function ado_field_type( $rs, $field_number )
    {
        return $rs->Fields[$field_number]->Type;
    }

?>

The aod version was a typo.
mark dickens at mindspring dot com 29-Feb-2004 05:03
Just a note to those wanting to use COM to interface to printers under Windows 2000/XP.  If it doesn't work or seems to hang, try changing the logon identity under which the Apache (assuming you're using Apache) service is running from the System account to another account.  By default most services will install to run under the System account and although the System account has total authority on the computer it is running on, it has no authority outside of that realm as I understand it.  Apparently accessing printers is considered "outside the realm".  I used this technique to interface a Dymo thermal label printer to my server and it works now.
platiumstar at canada dot com 13-Feb-2004 12:08
I didn't found any working examples of PHP using the MSMQ COM object, so I made up one, thought I share it with everybody..it works ok.
You have to create a local Public message queue named "TestQueue" first.

<?php
define(MQ_SEND_ACCESS , 2);
define(MQ_DENY_NONE , 0);

$msgQueueInfo =  new COM("MSMQ.MSMQQueueInfo") or die("can not create MSMQ Info object");
$msgQueueInfo->PathName = ".\TestQueue";

$msgQueue =  new COM("MSMQ.MSMQQueue") or die("can not create MSMQ object");
$msgQueue=$msgQueueInfo->Open(MQ_SEND_ACCESS, MQ_DENY_NONE );

$msgOut = new COM("MSMQ.MSMQMessage") or die("can not create MSMQ message object");
$msgOut->Body = "Hello world!";
$msgOut->Send($msgQueue);

$msgQueue->Close();
unset($msgOut);
unset($msgQueue);
unset($msgQueueInfo);
?>

enjoy
PS
kleontiv -at- combellga -dot- ru 15-Dec-2003 08:47
And here is an example of using ADSI via ADODB. This sample is unique because only ADODB can provide recursive Active directory searching and listing. But ADODB can only READ Active directory. So you can find something by ADODB and then modify that directly by ADSI.

With previos example this help build you PHP coded interface to Menaging and browsing Microsoft Active Directory.

Also ADODB on ADSI helps you sort recordset and search on it.

<?php

$objConnection = new COM ("ADODB.Connection") ;
$objConnection->Open("Provider=ADsDSOObject;user id=DOMAIN\\login;password=your_passowrd;") ;

$objCommand = new COM("ADODB.Command") ;
$objCommand->ActiveConnection = $objConnection ;

$Cmd  = "<LDAP://dc=mydomain,dc=com>;" ;
$Cmd .= "(&(objectClass=user)(mail=*)(!cn=SystemMailbox*));" ;
$Cmd .= "name,mail,telephoneNumber;subtree" ;

$objCommand->Properties['Sort On']->Value = "name" ;

$objCommand->CommandText = $Cmd ;
$objRecordSet = $objCommand->Execute() ;

$OrderNumber = 0 ;
while(!$objRecordSet->EOF())
 {
  $OrderNumber ++ ;
  $nn = $objRecordSet->Fields['name']->Value ;
  $mm = $objRecordSet->Fields['mail']->Value ;
  $ph = $objRecordSet->Fields['telephoneNumber']->Value ;
  echo "$OrderNumber: $nn mail: $mm phone: $ph<br>" ;
  $objRecordSet->MoveNext() ;
 }

Echo "===========================<br>" ;
Echo "All MailBoxes: " ;
Echo $objRecordSet->RecordCount() ;
$objRecordSet->Close() ;
$objCommand->Close() ;
$objConnection->Close() ;

unset($objRecordSet) ;
unset($objCommand) ;
unset($objConnection) ;

?>
kleontiv -at- combellga -dot- ru 15-Dec-2003 06:27
I think some one wants to access to Microsoft Active Directory using ADSI with different credential by PHP. You can also do it via pure LDAP but using ADSI it is more simpler.

On VB, VBS or ASP you can do like folowing (so not trivial):

<%
Set dso = GetObject("LDAP:")

Set DsObj = dso.OpenDSObject( _
    "LDAP://DC=mydomain,DC=com", _
    "DOMAIN\login", _
    "password", _
    ADS_SECURE_AUTHENTICATION + ADS_SERVER_BIND + ADS_USE_ENCRYPTION)

for each Obj in DsObj
    Response.Write Obj.name & " with class of: " & Obj.class & "<br>"
next
%>

On PHP you can do like that:

<?php
$ADSI = new COM("LDAP:") ;

$DsObj = $ADSI->OpenDsObject("LDAP://DC=mydomain,DC=com",
                             "DOMAIN\login",
                             "password",
                             515) ;

while($Obj = $DsObj->Next())
 {
  echo $Obj->name.' with class of: '.$Obj->class.'<br>' ;
 }
?>

The access type enumeration you can use as ADS_AUTHENTICATION_ENUM.
david at wu-man dot com 06-Dec-2003 11:42
After many hours of trial and error, I have finally discovered how to kill the notorious Word process.  I hope my solution works for everyone else.

#1) $word->Quit() is perfectly fine.  Don't bother feeding in as parameter those empty variants.

#2) Do NOT have com_load_typelib('Word.Application') anywhere in your script.

#3) Make sure you have the following line before $word->Quit() :

$word->Documents[1]->Close(false);

#4) Add the following lines after $word->Quit() :

<?php
$word->Release();
$word = null;
unset($word);
?>

If my solution indeed helped you with your problem, please email me and let me know.  I would like to see if the problem was indeed solved.
alanraycom at el hogar dot net com 04-Dec-2003 12:50
XSLT transformations using MSXML can be done with this code

<?php
function xsltransform1($xslpath,$xmlstring)
 {
  $xml= new COM("Microsoft.XMLDOM");
  $xml->async=false;
  // $xmlstring is an xml string
  $xml->load($xmlstring);
  $xsl = new COM("Microsoft.XMLDOM");
  $xsl->async=false;
  // $xslpath is path to an xsl file
  $xsl->load($xslpath);
  $response=$xml->transformNode($xsl);
  unset($xml);
  unset($xsl);
  return $response;
 }
?>

enjoy
Alan Young
chris at hitcatcher dot com 02-Dec-2003 01:19
Many COM objects don't do well when passing long (win32) filenames as function parameters.  You can get the 8.3 name of the folder or file on *most* (some win platforms don't support it, and it can be toggled off in the registry for performance reasons) servers using the following:

<?php
//create FSO instance
$exFSO = new COM("Scripting.FileSystemObject") or die ("Could not create Scripting.FileSystemObject");

//set vars
$myDir = "C:\\Program Files\\";
$myFile = "a long file name.txt";

//get folder & file objects
$exDir = $exFSO->GetFolder($myDir);
$exFile = $exFSO->GetFile($myDir . $myFile);
echo $exDir->ShortPath;
echo $exFile->ShortPath;
?>
23-Nov-2003 06:57
Here is an example of reading the custom properties like Author, Keywords, etc. which are stored in MS Office and other Windows files. You must install and register dsofile as described at http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q224351. The .frm file in that distribution lists most of the available properties.

<?php
// the file you wish to access
$fn = '/docs/MFA.xls';

$oFilePropReader = new COM('DSOleFile.PropertyReader');
$props = $oFilePropReader->GetDocumentProperties($fn);

// one syntax
$au = com_get($props, 'Author');
print "au: $au \n";

//another syntax
$str = 'LastEditedBy';
$lsb = $props->$str;
var_dump($lsb);

// set a property if you wish
if (!$props->IsReadOnly()) {
  $props->Subject = 'tlc';
}
?>
brana 17-Nov-2003 02:17
How to send email via MAPI (works with Windows NT4, apache launched as a service)

<?php
#New MAPI session...
$session = new COM("MAPI.Session");

#Login
$strProfileInfo = "exchange_server" . chr(10) . "exchange_user";
$session->Logon("", "", 0, 1, 0, 1, $strProfileInfo);

# New message...
$msg = $session->Outbox->Messages->Add();
$msg->Subject = "Subject";
$msg->Text = "body";

#set recipients...
$rcpts = $msg->Recipients;
$rcpts->AddMultiple("toto@toto.com", 1);   #To...
$rcpts->AddMultiple("titi@titi.com", 2);   #Cc...
$rcpts->AddMultiple("tutu@tutu.com", 3);   #Bcc...

# Resolve senders
$rcpts->Resolve();

#Send message
$msg->Send();

#Logoff
$session->Logoff();

#Cleanup
if($msg!=null) {
    $msg->Release();
    $msg = null;
}
if($session!=null) {
    $session->Release();
    $session = null;
}
?>

ps : NT account launching the service and exchange user must match !
03-Oct-2003 09:55
Convert Microsoft word document .DOC to adobe acrobat .PS or .PDF files:

Drawback: if any dialog boxes pop up, the conversion will become non-automated.

<?php
// You must have word and adobe acrobat distiller on
// your system, although theoretically any printer driver
// that makes .PS files would work.

// starting word
$word = new COM("word.application") or die("Unable to instanciate Word");
print "Loaded Word, version {$word->Version}\n";

// bring it to front
$word->Visible = 1;

// Select the acrobat distiller as the active printer.
// I presume that you could also use the Acrobat PDF writer // driver that comes with Acrobat full version.
$word->ActivePrinter = "Acrobat Distiller";

// Open a word document, or anything else that word
// can read
$word->Documents->Open($input);

// This will create a .PS file called $output
$word->ActiveDocument->PrintOut(0, 0, 0, $output);

// If you need a .pdf instead, use the acrobat
// distiller's watched folder option.

// closing word
$word->Quit();

// free the object
$word->Release();
$word = null;

?>
court shrock 26-Sep-2003 02:24
took me a while until I found out how to loop through COM objects in PHP.... in ASP, you use something like:

<%
set domainObject = GetObject("WinNT://Domain")

for each obj in domainObject
  Response.write obj.Name & "<br>"
next

%>

I finally found a note in a PHP bogus bug report that said you needed to do something like this (this is using ADSI):

<?php
$adsi = new COM("WinNT://Domain");

while($obj = $adsi->Next()) {
  echo $obj->Name.'<br>';
}// while

// will list all the names of all objects in the domain 'Domain'
?>

Also, you can access the IIS metabase by doing something like the following:

<?php
$iis = new COM("IIS://localhost/w3svc");

while($obj = $iis->Next()) {
  if ($obj->Class == 'IISWebServer') {
    $site = new COM("IIS://Localhost/w3svc/".$obj->Name);
    echo 'serviceID ('.$obj->Name.'): '.$site->ServerComment.'<br>';
    $bindings = $site->ServerBindings;

    echo '<ul>';
    foreach($bindings as $binding) {
      list($ip, $port, $hostHeader) = explode(':', $binding);
      echo "<li>$ip:$port -- $hostHeader</li>";
    }// foreach
    echo '</ul>';

    // hint: ssl bindings are in $site->SecureBindings

    unset($site);

  }// if

}// while
unset($iis);

// will list all the sites and their bindings on 'localhost'
?>
php at dictim dot com 06-Sep-2003 10:17
This took me days to work out how to do so I gotta share it:

How to get the word count from a MS Word Document:

<?php
#Instantiate the Word component.

$word = new COM("word.application") or die("Unable to instantiate Word");

$word->Visible = 1;

$word->Documents->Open("c:/anydocument.doc");
$temp = $word->Dialogs->Item(228);
$temp->Execute();
$numwords = $temp->Words();

echo $numwords;

$word->Quit();
?>

Note that this is the only way to get the word count accurately:
$word->ActiveDocument->Words->Count;
Will see all carriage returns and punctuation as words so is wildy innaccurate.
seidenberg at cf-mail dot net 12-Jul-2003 12:36
open PDF Documents with Internet Explorer

<?php
$browser = new COM("InternetExplorer.Application");
$browser->Visible = true;
$browser->Navigate("path_to_your_pdf_document");
?>

this works for me with php-gtk
tomas dot pacl at atlas dot cz 16-Jun-2003 03:07
Passing parameters by reference in PHP 4.3.2 (and in a future releases of PHP )

In PHP 4.3.2 allow_call_time_pass_reference option is set to "Off" by default and future versions may not support this option any longer.
Some COM functions has by-reference parameters. VARIANT object must be used to call this functions, but it would be passed without an '&' before variable name.

Example:

<?php
$myStringVariant = new VARIANT("over-write me", VT_BSTR);

/* works */
$result = $comobj->DoSomethingTo($myStringVariant );

/* works only with allow_call_time_pass_reference option set to "On" and may not work in a future release of PHP */
$result = $comobj->DoSomethingTo(&$myStringVariant );

/* The value in the variant can be retrieved by: */
$theActualValue = $myStringVariant->value;
?>
php at racinghippo dot co dot uk 23-Mar-2003 02:47
PASSING/RETURNING PARAMETERS BY REFERENCE
=========================================

Some COM functions not only return values through the return value, but also act upon a variable passed to it as a parameter *By Reference*:

RetVal = DoSomethingTo (myVariable)

Simply sticking an '&' in front of myVariable doesn't work - you need to pass it a variant of the correct type, as follows:

  $myStringVariant = new VARIANT("over-write me", VT_BSTR);
  $result = $comobj->DoSomethingTo( &$myStringVariant );

The value in the variant can then be retrieved by:

  $theActualValue = $myStringVariant->value;

Other types of variant can be created as your needs demand - see the preceding section on the VARIANT type.
alex at domini dot net 05-Mar-2003 11:55
To manage the Windows Register at HKEY_LOCAL_MACHINE\SOFTWARE\ use these functions:

<?php

/**
 * @return string
 * @param aplicacio string
 * @param nom string
 * @desc Retorna el valor del registre HKEY_LOCAL_MACHINE\SOFTWARE\aplicacio\nom
 */
function rtv_registre($aplicacio, $nom)
{
    Global $WshShell;
    $registre =  "HKEY_LOCAL_MACHINE\SOFTWARE\\" . $aplicacio . "\\"  . $nom;
    $valor = $WshShell->RegRead($registre);
    return($valor);

}

/**
 * @return string
 * @param aplicacio string
 * @param nom string
 * @param valor string
 * @param tipus string
 * @desc Actualitza el valor del registre HKEY_LOCAL_MACHINE\SOFTWARE\aplicacio\nom
 */
function put_registre($aplicacio, $nom, $valor, $tipus="REG_SZ")
{
    Global $WshShell;
    $registre =  "HKEY_LOCAL_MACHINE\SOFTWARE\\" . $aplicacio . "\\"  . $nom;
    $retorn = $WshShell->RegWrite($registre, $valor, $tipus);
    return($retorn);

}

//
// Creem una inst?ncia del COM que ens serveix per
// accedir al registre de Windows, i la mantenim.
// D'aquesta manera millorem el rendiment.
$WshShell = new COM("WScript.Shell");
?>
richard dot quadling at carval dot co dot uk 26-Feb-2003 02:51
With thanks to Harald Radi and Wez Furlong.

Some VBA functions have optional parameters. Sometimes the parameters you want to pass are not consecutive.

e.g.

GoTo What:=wdGoToBookmark, Name="BookMarkName"
GoTo(wdGoToBookmark,,,"BookMarkName)

In PHP, the "blank" parameters need to be empty.

Which is ...

<?php
// Some servers may have an auto timeout, so take as long as you want.
set_time_limit(0);

// Show all errors, warnings and notices whilst developing.
error_reporting(E_ALL);

// Used as a placeholder in certain COM functions where no parameter is required.
$empty = new VARIANT();

// Load the appropriate type library.
com_load_typelib('Word.Application');

// Create an object to use.
$word = new COM('word.application') or die('Unable to load Word');
print "Loaded Word, version {$word->Version}\n";

// Open a new document with bookmarks of YourName and YourAge.
$word->Documents->Open('C:/Unfilled.DOC');

// Fill in the information from the form.
$word->Selection->GoTo(wdGoToBookmark,$empty,$empty,'YourName'); // Note use of wdGoToBookmark, from the typelibrary and the use of $empty.
$word->Selection->TypeText($_GET['YourName']);

$word->Selection->GoTo(wdGoToBookmark,$empty,$empty,'YourAge');
$word->Selection->TypeText($_GET['YourAge']);

// Save it, close word and finish.
$word->Documents[1]->SaveAs("C:/{$_GET['YourName']}.doc");
$word->Quit();
$word->Release();
$word = null;
print "Word closed.\n";
?>

The example document is ...

Hello [Bookmark of YourName], you are [Bookmark of YourAge] years old.

and it would be called ...

word.php?YourName=Richard%20Quadling&YourAge=35

Regards,

Richard.
pchason at juno dot com 11-Dec-2002 08:23
Searched for days on how to set up a multi-column html output grid that didn't use an array when creating an ADODB connection. Couldn't find one so I figured it out myself. Pretty simple. Wish I would have tried it myself sooner. Commented code with database connection is below...

<html>
<body>
<table>
<?php
//create the database connection
$conn = new COM("ADODB.Connection");
$dsn = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("mydb.mdb");
$conn->Open($dsn);
//pull the data through SQL string
$rs = $conn->Execute("select clients from web");
$clients = $rs->Fields(1);
//start the multicolumn data output
$i = 0;
$columns = 5;
while (!$rs->EOF){
//if $i equals 0, start a row
if($i == 0){
echo "<tr>";
}
//then start writing the cells in the row, increase $i by one, and move to the next record
echo "<td>" . $clients->value . "</td>";
$i++;
$rs->movenext();
//once $i increments to equal $columns, end the row,
//and start the process over by setting $i to 0
if($i == $columns || $rs->EOF) {
echo "</tr>";
$i = 0;
    }
}
//end multicolumn data output

//close the ADO connections and release resources
$rs->Close();
$conn->Close();
$rs = null;
$conn = null; ?>
</table>
</body>
</html>
tedknightusa at hotmail dot com 26-Sep-2002 06:29
A Notes example:

<?php
$session = new COM "Lotus.NotesSession" );
$session->Initialize($password);

$someDB = "C:\Lotus\Notes\Data\somedb.nsf";
$db = $session->GetDatabase("", $someDB) or die("Cannot get someDB.nsf");
$view = $db->GetView("some_view") or die("Some View not found");

$doc = $view->GetFirstDocument() or die("Unable to get Initial Some Document");

// loop through
while($doc){
  $col_vals = $doc->ColumnValues();
  echo $col_vals[0];
  $doc = $view->GetNextDocument($doc);
}
?>
dwt at myrealbox dot com 27-Aug-2002 12:31
When first writing applications instancing COM objects I always faced the problem of the instanced program not terminating correctly although everything else worked fine. In fact the process had to be killed manually by using the task manager. When experimenting a bit I was able to isolate the cause for this peculiar behaviour to be illustrated by the sample code below:

<?php

//Accessing arrays by retrieving elements via function ArrayName(Index) works, but certainly is neither efficient nor good style
$excel=new COM("Excel.Application");
$excel->sheetsinnewworkbook=1;
$excel->Workbooks->Add();

$book=$excel->Workbooks(1);
$sheet=$book->Worksheets(1);

$sheet->Name="Debug-Test";
$book->saveas("C:\\debug.xls");

$book->Close(false);
unset($sheet);
unset($book);
$excel->Workbooks->Close();
$excel->Quit();
unset($excel);

//Accessing arrays as usual (using the [] operator) works, buts leads to the application not being able to terminate by itself
$excel=new COM("Excel.Application");
$excel->sheetsinnewworkbook=1;
$excel->Workbooks->Add();

$excel->Workbooks[1]->Worksheets[1]->Name="Debug-Test";
$excel->Workbooks[1]->saveas("C:\\debug.xls");

$excel->Workbooks[1]->Close(false);
$excel->Workbooks->Close();
$excel->Quit();
unset($excel);

?>

The sample code performs the same action twice and each time the file is properly created. Yet for some mysterious reason the instanced excel process won't terminate once you've accessed an array the way most programmers (especially those who do a lot of oop in c++) do! Therefore until the PHP developers become aware of this problem we'll have to use the inefficient coding style illustrated in the first example.
nospam at bol dot com dot br 21-Aug-2002 12:30
This is a sample to make a parametrized query using ADO via COM, this sample was used with Foxpro but will work's with most ADO complient databases. This was tested on IIS with WIN XP pro.

<?php
// Create the main connection
$dbc = new COM("ADODB.Connection")  or die ("connection create fail");
$dbc->Provider = "MSDASQL";
$dbc->Open("FoxDatabase");
// Creates a temporary record set
$RSet = new COM("ADODB.Recordset");
// Create one ADO command
$cm   = new COM("ADODB.Command");
$cm->Activeconnection = $dbc;
// the ? inside values will be the parameters $par01 and $par02
$cm->CommandText =  "Insert Into testtable ( mycharfield,mymemofield) VALUES (?,?)" ;
$cm->Prepared = TRUE;
// Creates and append 2 parameters
$par01 = $cm->CreateParameter('ppar01',129,1,50,'ABCDEFGHIKL');
$cm->Parameters->Append($par01);
$par02 = $cm->CreateParameter('ppar01',129,1,50,'123456789012346789');
$cm->Parameters->Append($par02);
// Call 10 times the exec comand to show 10 different queries
for ($i = 1; $i <= 10; $i++) {
  $par01->Value = 'Count '.$i;
  $par02->Value = 'Passing here'.$i.' times';
  $RSet = $cm->Execute;
}
$RSet = $dbc->Execute("select * from testtable");
while (!$RSet->EOF){
  echo $RSet->fields["mycharfield"]->value.' ' ;
  echo $RSet->fields["mymemofield"]->value ;
  echo '<BR>';
  $RSet->movenext();
}
$RSet->close;
$dbc->close;
$cm->close;
$RSet = null;
$dbc = null;
$cm = null;
?>
admin at purplerain dot org 19-Apr-2002 01:34
an easy way to convert your file from .doc to .html

<?php
// starting word
$word = new COM("word.application") or die("Unable to instanciate Word");

// if you want see thw World interface the value must be '1' else '0'
$word->Visible = 1;

//doc file location
$word->Documents->Open("E:\\first.doc");

//html file location  '8' mean HTML format
$word->Documents[1]->SaveAs("E:\\test_doc.html",8);

//closing word
$word->Quit();

//free the object from the memory
$word->Release();
$word = null;
?>
admin at purplerain dot org 02-Apr-2002 12:01
An easy way to send e-mail using your default Outlook account:

<?php
$objApp = new COM("Outlook.Application");
$myItem = $objApp->CreateItem(olMailItem);
$a=$myItem->Recipients->Add("admin@purplerain.org");
$myItem->Subject="Subject";
$myItem->Body="This is a Body Section now.....!";
$myItem->Display();
$myItem->Send();
?>
madon at cma-it dot com 07-Mar-2002 09:59
I thought this excel chart example could be useful.

Note the use of Excel.application vs Excel.sheet.

<pre>
<?php
    print "Hi";
#Instantiate the spreadsheet component.
#    $ex = new COM("Excel.sheet") or Die ("Did not connect");
$exapp = new COM("Excel.application") or Die ("Did not connect");

#Get the application name and version
print "Application name:{$ex->Application->value}<BR>" ;
print "Loaded version: {$ex->Application->version}<BR>";

$wkb=$exapp->Workbooks->add();
#$wkb = $ex->Application->ActiveWorkbook or Die ("Did not open workbook");
print "we opened workbook<BR>";

$ex->Application->Visible = 1; #Make Excel visible.
print "we made excell visible<BR>";

$sheets = $wkb->Worksheets(1); #Select the sheet
print "selected a sheet<BR>";
$sheets->activate; #Activate it
print "activated sheet<BR>";

#This is a new sheet
$sheets2 = $wkb->Worksheets->add(); #Add a sheet
print "added a new sheet<BR>";
$sheets2->activate; #Activate it
print "activated sheet<BR>";

$sheets2->name="Report Second page";

$sheets->name="Report First page";
print "We set a name to the sheet: $sheets->name<BR>";

# fills a columns
$maxi=20;
for ($i=1;$i<$maxi;$i++) {
    $cell = $sheets->Cells($i,5) ; #Select the cell (Row Column number)
    $cell->activate; #Activate the cell
    $cell->value = $i*$i; #Change it to 15000
}

$ch = $sheets->chartobjects->add(50, 40, 400, 100); # make a chartobject

$chartje = $ch->chart; # place a chart in the chart object
$chartje->activate; #activate chartobject
$chartje->ChartType=63;
$selected = $sheets->range("E1:E$maxi"); # set the data the chart uses
$chartje->setsourcedata($selected); # set the data the chart uses
print "set the data the chart uses <BR>";

$file_name="D:/apache/Apache/htdocs/alm/tmp/final14.xls";
if (file_exists($file_name)) {unlink($file_name);}
#$ex->Application->ActiveWorkbook->SaveAs($file_name); # saves sheet as final.xls
$wkb->SaveAs($file_name); # saves sheet as final.xls
print "saved<BR>";

#$ex->Application->ActiveWorkbook->Close("False");
$exapp->Quit();
unset($exapp);
?>

</pre>

Alex Madon


Generate a globally unique identifier (GUID)

  • ≪ COM 함수 목록
  • com_event_sink ≫
  • PHP Manual
  • COM 함수 목록
  • Generate a globally unique identifier (GUID)

com_create_guid

(PHP 5, PHP 7)

com_create_guid — Generate a globally unique identifier (GUID)

설명

string com_create_guid ( void )

Generates a Globally Unique Identifier (GUID).

A GUID is generated in the same way as DCE UUID's, except that the Microsoft convention is to enclose a GUID in curly braces.

반환값

Returns the GUID as a string.

참고

  • uuid_create() in the PECL uuid extension

User Contributed Notes

Dave Pearson (dave at pds-uk dot com) 14-Apr-2016 07:07
Here's my final version of a GUIDv4 function (based on others work here) that should work on all platforms and gracefully fallback to less cryptographically secure version if others are not supported...

<?php
/**
 * Returns a GUIDv4 string
 *
 * Uses the best cryptographically secure method
 * for all supported pltforms with fallback to an older,
 * less secure version.
 *
 * @param bool $trim
 * @return string
 */
function GUIDv4 ($trim = true)
{
    // Windows
    if (function_exists('com_create_guid') === true) {
        if ($trim === true)
            return trim(com_create_guid(), '{}');
        else
            return com_create_guid();
    }

    // OSX/Linux
    if (function_exists('openssl_random_pseudo_bytes') === true) {
        $data = openssl_random_pseudo_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }

    // Fallback (PHP 4.2+)
    mt_srand((double)microtime() * 10000);
    $charid = strtolower(md5(uniqid(rand(), true)));
    $hyphen = chr(45);                  // "-"
    $lbrace = $trim ? "" : chr(123);    // "{"
    $rbrace = $trim ? "" : chr(125);    // "}"
    $guidv4 = $lbrace.
              substr($charid,  0,  8).$hyphen.
              substr($charid,  8,  4).$hyphen.
              substr($charid, 12,  4).$hyphen.
              substr($charid, 16,  4).$hyphen.
              substr($charid, 20, 12).
              $rbrace;
    return $guidv4;
}
?>
indrora 08-Nov-2015 04:09
If you're going to generate random UUIDs, at least make them conform:

* The uppermost byte of the third stanza must be 4
* the uppermost byte of the fourth stanza may be any of (8 9 a b)

see also: The wikipedia page for UUIDs: https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
pavel.volyntsev(at)gmail 27-Aug-2015 09:37
Use more cryptographically strong algorithm to generate pseudo-random bytes and format it as GUID v4 string

function guidv4()
{
    if (function_exists('com_create_guid') === true)
        return trim(com_create_guid(), '{}');

    $data = openssl_random_pseudo_bytes(16);
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
mark at briley dot com 31-Mar-2015 12:34
I made a mistake on my previous note.  I used rtrim instead of just trim to take off the curly brackets.  Here is my note again - or please change the RTRIM to just TRIM in my previous note.  Thanks!  :-)

Kristof_Polleunis at yahoo dot com has an excellent function for obtaining a GUID but I believe it can be improved by checking for whether or not to remove the curly braces so the programmer who uses it doesn't have to do this.  Here is the improved function:

<?php
#
#   Taken from the PHP documentation website.
#
#   Kristof_Polleunis at yahoo dot com
#
#   A guid function that works in all php versions:
#   MEM 3/30/2015 : Modified the function to allow someone
#       to specify whether or not they want the curly
#       braces on the GUID.
#
function guid( $opt = true ){       //  Set to true/false as your default way to do this.

    if( function_exists('com_create_guid') ){
        if( $opt ){ return com_create_guid(); }
            else { return trim( com_create_guid(), '{}' ); }
        }
        else {
            mt_srand( (double)microtime() * 10000 );    // optional for php 4.2.0 and up.
            $charid = strtoupper( md5(uniqid(rand(), true)) );
            $hyphen = chr( 45 );    // "-"
            $left_curly = $opt ? chr(123) : "";     //  "{"
            $right_curly = $opt ? chr(125) : "";    //  "}"
            $uuid = $left_curly
                . substr( $charid, 0, 8 ) . $hyphen
                . substr( $charid, 8, 4 ) . $hyphen
                . substr( $charid, 12, 4 ) . $hyphen
                . substr( $charid, 16, 4 ) . $hyphen
                . substr( $charid, 20, 12 )
                . $right_curly;
            return $uuid;
            }
}

?>

As the comments say - set the incoming option ($opt) to either TRUE or FALSE depending upon whether you want the curly braces to always show up or not.  If you need to obtain a GUID in the other way - all you have to do is to send the opposite value to the function.
Alix Axel 17-Aug-2010 12:59
The phunction PHP framework (http://sourceforge.net/projects/phunction/) uses the following function to generate valid version 4 UUIDs:

<?php

function GUID()
{
    if (function_exists('com_create_guid') === true)
    {
        return trim(com_create_guid(), '{}');
    }

    return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}

?>

The output generated by the sprintf() and mt_rand() calls is identical to com_create_guid() results.
Kristof_Polleunis at yahoo dot com 28-Apr-2005 05:16
A guid function that works in all php versions:

<?php
function guid(){
    if (function_exists('com_create_guid')){
        return com_create_guid();
    }else{
        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);// "}"
        return $uuid;
    }
}
echo guid();
?>


Connect events from a COM object to a PHP object

  • ≪ com_create_guid
  • com_get_active_object ≫
  • PHP Manual
  • COM 함수 목록
  • Connect events from a COM object to a PHP object

com_event_sink

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

com_event_sink — Connect events from a COM object to a PHP object

설명

bool com_event_sink ( variant $comobject , object $sinkobject [, mixed $sinkinterface ] )

Instructs COM to sink events generated by comobject into the PHP object sinkobject.

Be careful how you use this feature; if you are doing something similar to the example below, then it doesn't really make sense to run it in a web server context.

인수

comobject

sinkobject

sinkobject should be an instance of a class with methods named after those of the desired dispinterface; you may use com_print_typeinfo() to help generate a template class for this purpose.

sinkinterface

PHP will attempt to use the default dispinterface type specified by the typelibrary associated with comobject, but you may override this choice by setting sinkinterface to the name of the dispinterface that you want to use.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 COM event sink example

<?php
class IEEventSinker {
    var $terminated = false;

   function ProgressChange($progress, $progressmax) {
      echo "Download progress: $progress / $progressmax\n";
    }

    function DocumentComplete(&$dom, $url) {
      echo "Document $url complete\n";
    }

    function OnQuit() {
      echo "Quit!\n";
      $this->terminated = true;
    }
}
$ie = new COM("InternetExplorer.Application");
// note that you don't need the & for PHP 5!
$sink = new IEEventSinker();
com_event_sink($ie, $sink, "DWebBrowserEvents2");
$ie->Visible = true;
$ie->Navigate("http://www.example.org");
while(!$sink->terminated) {
  com_message_pump(4000);
}
$ie = null;
?>

참고

  • com_print_typeinfo() - Print out a PHP class definition for a dispatchable interface
  • com_message_pump() - Process COM messages, sleeping for up to timeoutms milliseconds

User Contributed Notes

fjortiz 14-Jul-2005 09:33
In case someone needs a skeleton sink for ADODB.Connection events:

class ADOConnectionEventSink    {

    function BeginTransComplete( $translevel, $objerror, $status, $objconn )    {
        return 0;
    }

    function CommitTransComplete( $objerror, $status, $objconn )    {
        return 0;
    }

    function RolbackTransComplete( $objerror, $status, $objconn )    {
        return 0;
    }

    function WillConnect ( $ConnectionString, $userid, $psword, $options, $status, $objconn )    {
        return 0;
    }

    function ConnectComplete ( $objerror, $status, $objconn)    {
        return 0;
    }

    function Disconnect( $status, $objConn )    {
        return 0;
    }

    function WillExecute ( $src, $cursortyp, $locktyp, $options, $status, $objcomm, $objrs, $objconn )    {
        return 0;
    }

    function ExecuteComplete ( $recaffected, $objerror, $status, $objcomm, $objrs, $objconn )    {
        return 0;
    }

    function InfoMessage ( $objerror, $status, $objconn)    {
        return 0;
    }
}

// later on...
$db = new COM("ADODB.Connection", NULL, $charPage);
$sink = new ADOConnectionEventSink();
com_event_sink($db, $sink, "ConnectionEvents");
//...


Returns a handle to an already running instance of a COM object

  • ≪ com_event_sink
  • com_load_typelib ≫
  • PHP Manual
  • COM 함수 목록
  • Returns a handle to an already running instance of a COM object

com_get_active_object

(PHP 5, PHP 7)

com_get_active_object — Returns a handle to an already running instance of a COM object

설명

variant com_get_active_object ( string $progid [, int $code_page ] )

com_get_active_object() is similar to creating a new instance of a COM object, except that it will only return an object to your script if the object is already running. OLE applications use something known as the "Running Object Table" to allow well-known applications to be launched only once; this function exposes the COM library function GetActiveObject() to get a handle on a running instance.

인수

progid

progid must be either the ProgID or CLSID for the object that you want to access (for example Word.Application).

code_page

Acts in precisely the same way that it does for the COM class.

반환값

If the requested object is running, it will be returned to your script just like any other COM object.

오류/예외

There are a variety of reasons why this function might fail, the most common being that the object is not already running. In that situation, the exception error code will be MK_E_UNAVAILABLE; you can use the getCode method of the exception object to check the exception code.

주의

Warning

Using com_get_active_object() in a web server context is not always a smart idea. Most COM/OLE applications are not designed to handle more than one client concurrently, even (or especially!) Microsoft Office. You should read » Considerations for Server-Side Automation of Office for more information on the general issues involved.

User Contributed Notes

gerrit at timingteam dot nl 04-Jan-2009 06:25
Please note the next behavior (Apache 2.2.8, W2K, WXP)

If Apache runs as a service, com_get_active_object() always returns MK_E_UNAVAILABLE, even when it is definitely sure that the Running Object Table contains a reference to the required object.

If Apache runs in a console, com_get_active_object()  returns the object handle as required.

This apparently is a Windows issue.


Loads a Typelib

  • ≪ com_get_active_object
  • com_message_pump ≫
  • PHP Manual
  • COM 함수 목록
  • Loads a Typelib

com_load_typelib

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

com_load_typelib — Loads a Typelib

설명

bool com_load_typelib ( string $typelib_name [, bool $case_insensitive = true ] )

Loads a type-library and registers its constants in the engine, as though they were defined using define().

Note that it is much more efficient to use the configuration setting to pre-load and register the constants, although not so flexible.

If you have turned on , then PHP will attempt to automatically register the constants associated with a COM object when you instantiate it. This depends on the interfaces provided by the COM object itself, and may not always be possible.

인수

typelib_name

typelib_name can be one of the following:

  • The filename of a .tlb file or the executable module that contains the type library.

  • The type library GUID, followed by its version number, for example {00000200-0000-0010-8000-00AA006D2EA4},2,0.

  • The type library name, e.g. Microsoft OLE DB ActiveX Data Objects 1.0 Library.

PHP will attempt to resolve the type library in this order, as the process gets more and more expensive as you progress down the list; searching for the type library by name is handled by physically enumerating the registry until we find a match.

case_insensitive

The case_insensitive behaves in the same way as the parameter with the same name in the define() function.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

tomas dot burba at softneta dot lt 13-Mar-2012 08:38
I had some problems with a third-party Word-controlling code that is somewhat similar to Richard's example. (Word 2007 SP1, XP SP2.) Rewrote some parts according to that example, no luck. Error messages from Word were crazy:

"Bad parameter" -- it didn't like wdGoToBookmark but was ok with its numeric equivalent -1;

"Call to undefined method variant::SaveAs()" -- the same with Save() that was suitable in my case, so no options were remaining.

But then I ran the example via php.exe in the command line, and it worked! Eventually it turned out that Apache service must be run as some real user instead of SYSTEM.
RQuadling at GMail dot com 17-Jun-2008 12:40
Some libraries load other libraries as part of their functionality. For example Crystal Reports Object Factory uses the CreateObject() method to create an instance of a Crystal Design RunTime Application. If you want to use constants defined in the newly loaded library, you will need to manually register the library - even with com.autoregister_typelib set to true.

<?php
echo 'com.autoregister_typelib = ', ini_get('com.autoregister_typelib'), PHP_EOL;

$o_CrObjectFactory = New COM('CrystalReports11.ObjectFactory.1');

$o_CrApplication = $o_CrObjectFactory->CreateObject("CrystalDesignRunTime.Application");

echo 'Will not be defined : ', crOpenReportByDefault, PHP_EOL;

com_load_typelib('CrystalDesignRunTime.Application');

echo 'Will now be defined : ', crOpenReportByDefault, PHP_EOL;
?>

outputs ...

com.autoregister_typelib = 1
Will not be defined :
Notice: Use of undefined constant crOpenReportByDefault - assumed 'crOpenReportByDefault' in C:\refl.php on line 8
crOpenReportByDefault
Will now be defined : 0
04-May-2004 04:18
In case any of you were wondering what com_load_typelib actually DOES, I have found that it loads constants\enums that the COM obeject has defined.  The example below is using the Nero COM object (from Nero Burning rom, version 6+).  The Nero object defines a bunch of constants, but they can not be used as PHP constants unless you use the mentioned function.  So the example below will work just fine:

<?php
$aNero = new COM("Nero.Nero") or die ("Nero didn't load, too bad");
com_load_typelib("Nero.Nero");
$aNero->GetDrives(NERO_MEDIA_CD);
?>

But this one will not, since PHP will return a message saying that the constant is undefined:

<?php
$aNero = new COM("Nero.Nero") or die ("Nero didn't load, too bad");
$aNero->GetDrives(NERO_MEDIA_CD);
?>

This one took me a while to figure out, hope it helps.

Also, check out http://www.php.net/manual/en/faq.com.php to find out how to get COM events working in PHP.
ojpp at webdevfm dot com 31-Jan-2004 03:33
Hello PHP community, I want to help in a problem I found in this COM built-in function: com_load_typelib(), when I executed it in order to open a COM Server process (for example: Word, Excel, etc.) I was in trouble to release the object from memory, I figured it out, this is the solution:

Change a configuration in the php.ini:

; autoregister constants of a components typlib on com_load()
com.autoregister_typelib = true

When com.autoregister_typelib directive is true, PHP parser manage the COM server type library, I hope it helps.
richard dot quadling at carval dot co dot uk 26-Feb-2003 02:55
<?php
// Some servers may have an auto timeout, so take as long as you want.
set_time_limit(0);

// Show all errors, warnings and notices whilst developing.
error_reporting(E_ALL);

// Used as a placeholder in certain COM functions where no parameter is required.
$empty = new VARIANT();

// Load the appropriate type library.
com_load_typelib('Word.Application');

// Create an object to use.
$word = new COM('word.application') or die('Unable to load Word');
print "Loaded Word, version {$word->Version}\n";

// Open a new document with bookmarks of YourName and YourAge.
$word->Documents->Open('C:/Unfilled.DOC');

// Fill in the information from the form.
$word->Selection->GoTo(wdGoToBookmark,$empty,$empty,'YourName'); // Note use of wdGoToBookmark, from the typelibrary and the use of $empty.
$word->Selection->TypeText($_GET['YourName']);

$word->Selection->GoTo(wdGoToBookmark,$empty,$empty,'YourAge');
$word->Selection->TypeText($_GET['YourAge']);

// Save it, close word and finish.
$word->Documents[1]->SaveAs("C:/{$_GET['YourName']}.doc");
$word->Quit();
$word->Release();
$word = null;
print "Word closed.\n";
?>

The example document is ...

Hello [Bookmark of YourName], you are [Bookmark of YourAge] years old.

and it would be called ...

word.php?YourName=Richard%20Quadling&YourAge=35

Regards,

Richard.


Process COM messages, sleeping for up to timeoutms milliseconds

  • ≪ com_load_typelib
  • com_print_typeinfo ≫
  • PHP Manual
  • COM 함수 목록
  • Process COM messages, sleeping for up to timeoutms milliseconds

com_message_pump

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

com_message_pump — Process COM messages, sleeping for up to timeoutms milliseconds

설명

bool com_message_pump ([ int $timeoutms = 0 ] )

This function will sleep for up to timeoutms milliseconds, or until a message arrives in the queue.

The purpose of this function is to route COM calls between apartments and handle various synchronization issues. This allows your script to wait efficiently for events to be triggered, while still handling other events or running other code in the background. You should use it in a loop, as demonstrated by the example in the com_event_sink() function, until you are finished using event bound COM objects.

인수

timeoutms

The timeout, in milliseconds.

If you do not specify a value for timeoutms, then 0 will be assumed. A 0 value means that no waiting will be performed; if there are messages pending they will be dispatched as before; if there are no messages pending, the function will return FALSE immediately without sleeping.

반환값

If a message or messages arrives before the timeout, they will be dispatched, and the function will return TRUE. If the timeout occurs and no messages were processed, the return value will be FALSE.

User Contributed Notes

There are no user contributed notes for this page.


Print out a PHP class definition for a dispatchable interface

  • ≪ com_message_pump
  • variant_abs ≫
  • PHP Manual
  • COM 함수 목록
  • Print out a PHP class definition for a dispatchable interface

com_print_typeinfo

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

com_print_typeinfo — Print out a PHP class definition for a dispatchable interface

설명

bool com_print_typeinfo ( object $comobject [, string $dispinterface [, bool $wantsink = false ]] )

The purpose of this function is to help generate a skeleton class for use as an event sink. You may also use it to generate a dump of any COM object, provided that it supports enough of the introspection interfaces, and that you know the name of the interface you want to display.

인수

comobject

comobject should be either an instance of a COM object, or be the name of a typelibrary (which will be resolved according to the rules set out in com_load_typelib()).

dispinterface

The name of an IDispatch descendant interface that you want to display.

wantsink

If set to TRUE, the corresponding sink interface will be displayed instead.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • com_event_sink() - Connect events from a COM object to a PHP object
  • com_load_typelib() - Loads a Typelib

User Contributed Notes

Richard Lynch 04-Oct-2006 01:51
In my particular version of PHP, the second and third arguments are not, in fact, optional.

Passing in '' for both, however, yielded a bucketful of information.

YMMV
csaba at alum dot mit dot edu 14-Feb-2005 09:49
com_print_typeinfo is really useful if you're trying to figure out what properties and methods you have access to.  For example, I might do:

<?php
    $oExplorer = new COM("Shell.Application");
    com_print_typeinfo($oExplorer);
?>

The first line shows me the class of the object (what VBScript calls 'typename'), in my case IShellDispatch4.  It's frequently the case that if you plunk that in as the second argument to com_print_typeinfo, you get way more methods/properties coming back.  Thus:

<?php
    $oExplorer = new COM("Shell.Application");
    com_print_typeinfo($oExplorer, "IShellDispatch4");
?>

Furthermore, you might get additional functions listed if you try lower number suffixes (or not).  At any rate, it would be useful for PHP to have a typename function like VBScript does.  For example, if you iterate through the Windows of $oExplorer you get both IE and Explorer windows and typename is the easy way to differentiate between them.  Here's what I'm using:

<?php
    function typeName($objCOM) {
    if (empty($objCOM)) return "no COM object";
    if (gettype($objCOM)!="object") return "not a COM object";
    ob_start();
    com_print_typeinfo($objCOM);
    $typeInfo = ob_get_contents();
    ob_end_clean();
    $pattern = "/^\\s*class (.*) \\{/";
    if (!($matchCnt = preg_match($pattern, $typeInfo, $aMatch))) return "Not found";
    return $aMatch[1];
}
?>

Csaba Gabor from Vienna


Returns the absolute value of a variant

  • ≪ com_print_typeinfo
  • variant_add ≫
  • PHP Manual
  • COM 함수 목록
  • Returns the absolute value of a variant

variant_abs

(PHP 5, PHP 7)

variant_abs — Returns the absolute value of a variant

설명

mixed variant_abs ( mixed $val )

Returns the absolute value of a variant.

인수

val

The variant.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Returns the absolute value of val.

참고

  • abs() - 절대값

User Contributed Notes

There are no user contributed notes for this page.


"Adds" two variant values together and returns the result

  • ≪ variant_abs
  • variant_and ≫
  • PHP Manual
  • COM 함수 목록
  • "Adds" two variant values together and returns the result

variant_add

(PHP 5, PHP 7)

variant_add — "Adds" two variant values together and returns the result

설명

mixed variant_add ( mixed $left , mixed $right )

Adds left to right using the following rules (taken from the MSDN library), which correspond to those of Visual Basic:

Variant Addition Rules
If | Then
Both expressions are of the string type | Concatenation
One expression is a string type and the other a character | Addition
One expression is numeric and the other is a string | Addition
Both expressions are numeric | Addition
Either expression is NULL | NULL is returned
Both expressions are empty | Integer subtype is returned

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Returns the result.

참고

  • variant_sub() - Subtracts the value of the right variant from the left variant value

User Contributed Notes

There are no user contributed notes for this page.


Performs a bitwise AND operation between two variants

  • ≪ variant_add
  • variant_cast ≫
  • PHP Manual
  • COM 함수 목록
  • Performs a bitwise AND operation between two variants

variant_and

(PHP 5, PHP 7)

variant_and — Performs a bitwise AND operation between two variants

설명

mixed variant_and ( mixed $left , mixed $right )

Performs a bitwise AND operation. Note that this is slightly different from a regular AND operation.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Variant AND Rules
If left is | If right is | then the result is
TRUE | TRUE | TRUE
TRUE | FALSE | FALSE
TRUE | NULL | NULL
FALSE | TRUE | FALSE
FALSE | FALSE | FALSE
FALSE | NULL | FALSE
NULL | TRUE | NULL
NULL | FALSE | FALSE
NULL | NULL | NULL

참고

  • variant_or() - Performs a logical disjunction on two variants

User Contributed Notes

There are no user contributed notes for this page.


Convert a variant into a new variant object of another type

  • ≪ variant_and
  • variant_cat ≫
  • PHP Manual
  • COM 함수 목록
  • Convert a variant into a new variant object of another type

variant_cast

(PHP 5, PHP 7)

variant_cast — Convert a variant into a new variant object of another type

설명

variant variant_cast ( variant $variant , int $type )

This function makes a copy of variant and then performs a variant cast operation to force the copy to have the type given by type.

This function wraps VariantChangeType() in the COM library; consult MSDN for more information.

인수

variant

The variant.

type

type should be one of the VT_XXX constants.

반환값

Returns a VT_DATE variant.

참고

  • variant_set_type() - Convert a variant into another type "in-place"

User Contributed Notes

There are no user contributed notes for this page.


concatenates two variant values together and returns the result

  • ≪ variant_cast
  • variant_cmp ≫
  • PHP Manual
  • COM 함수 목록
  • concatenates two variant values together and returns the result

variant_cat

(PHP 5, PHP 7)

variant_cat — concatenates two variant values together and returns the result

설명

mixed variant_cat ( mixed $left , mixed $right )

Concatenates left with right and returns the result.

This function is notionally equivalent to $left . $right.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Returns the result of the concatenation.

참고

  • 문자열 연산자 for the string concatenation operator

User Contributed Notes

There are no user contributed notes for this page.


Compares two variants

  • ≪ variant_cat
  • variant_date_from_timestamp ≫
  • PHP Manual
  • COM 함수 목록
  • Compares two variants

variant_cmp

(PHP 5, PHP 7)

variant_cmp — Compares two variants

설명

int variant_cmp ( mixed $left , mixed $right [, int $lcid [, int $flags ]] )

Compares left with right.

This function will only compare scalar values, not arrays or variant records.

인수

left

The left operand.

right

The right operand.

lcid

A valid Locale Identifier to use when comparing strings (this affects string collation).

flags

flags can be one or more of the following values OR'd together, and affects string comparisons:

Variant Comparision Flags
value | meaning
NORM_IGNORECASE | Compare case insensitively
NORM_IGNORENONSPACE | Ignore nonspacing characters
NORM_IGNORESYMBOLS | Ignore symbols
NORM_IGNOREWIDTH | Ignore string width
NORM_IGNOREKANATYPE | Ignore Kana type
NORM_IGNOREKASHIDA | Ignore Arabic kashida characters

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Returns one of the following:

Variant Comparision Results
value | meaning
VARCMP_LT | left is less than right
VARCMP_EQ | left is equal to right
VARCMP_GT | left is greater than right
VARCMP_NULL | Either left, right or both are NULL

User Contributed Notes

There are no user contributed notes for this page.


Returns a variant date representation of a Unix timestamp

  • ≪ variant_cmp
  • variant_date_to_timestamp ≫
  • PHP Manual
  • COM 함수 목록
  • Returns a variant date representation of a Unix timestamp

variant_date_from_timestamp

(PHP 5, PHP 7)

variant_date_from_timestamp — Returns a variant date representation of a Unix timestamp

설명

variant variant_date_from_timestamp ( int $timestamp )

Converts timestamp from a unix timestamp value into a variant of type VT_DATE. This allows easier interopability between the unix-ish parts of PHP and COM.

인수

timestamp

A unix timestamp.

반환값

Returns a VT_DATE variant.

참고

  • variant_date_to_timestamp() - Converts a variant date/time value to Unix timestamp
  • mktime() - Get Unix timestamp for a date
  • time() - Return current Unix timestamp

User Contributed Notes

There are no user contributed notes for this page.


Converts a variant date/time value to Unix timestamp

  • ≪ variant_date_from_timestamp
  • variant_div ≫
  • PHP Manual
  • COM 함수 목록
  • Converts a variant date/time value to Unix timestamp

variant_date_to_timestamp

(PHP 5, PHP 7)

variant_date_to_timestamp — Converts a variant date/time value to Unix timestamp

설명

int variant_date_to_timestamp ( variant $variant )

Converts variant from a VT_DATE (or similar) value into a Unix timestamp. This allows easier interopability between the Unix-ish parts of PHP and COM.

인수

variant

The variant.

반환값

Returns a unix timestamp.

참고

  • variant_date_from_timestamp() - Returns a variant date representation of a Unix timestamp
  • date() - 로컬 날짜/시간을 형식화합니다
  • strftime() - Format a local time/date according to locale settings

User Contributed Notes

radek at tetik dot cz 26-Dec-2015 12:21
variant_date_to_timestamp() assumes the COM date in the current Windows time zone and converts it to the current PHP time zone


Returns the result from dividing two variants

  • ≪ variant_date_to_timestamp
  • variant_eqv ≫
  • PHP Manual
  • COM 함수 목록
  • Returns the result from dividing two variants

variant_div

(PHP 5, PHP 7)

variant_div — Returns the result from dividing two variants

설명

mixed variant_div ( mixed $left , mixed $right )

Divides left by right and returns the result.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Variant Division Rules
If | Then
Both expressions are of the string, date, character, boolean type | Double is returned
One expression is a string type and the other a character | Division and a double is returned
One expression is numeric and the other is a string | Division and a double is returned.
Both expressions are numeric | Division and a double is returned
Either expression is NULL | NULL is returned
right is empty and left is anything but empty | A com_exception with code DISP_E_DIVBYZERO is thrown
left is empty and right is anything but empty. | 0 as type double is returned
Both expressions are empty | A com_exception with code DISP_E_OVERFLOW is thrown

참고

  • variant_idiv() - Converts variants to integers and then returns the result from dividing them

User Contributed Notes

There are no user contributed notes for this page.


Performs a bitwise equivalence on two variants

  • ≪ variant_div
  • variant_fix ≫
  • PHP Manual
  • COM 함수 목록
  • Performs a bitwise equivalence on two variants

variant_eqv

(PHP 5, PHP 7)

variant_eqv — Performs a bitwise equivalence on two variants

설명

mixed variant_eqv ( mixed $left , mixed $right )

Performs a bitwise equivalence on two variants.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

If each bit in left is equal to the corresponding bit in right then TRUE is returned, otherwise FALSE is returned.

User Contributed Notes

There are no user contributed notes for this page.


Returns the integer portion of a variant

  • ≪ variant_eqv
  • variant_get_type ≫
  • PHP Manual
  • COM 함수 목록
  • Returns the integer portion of a variant

variant_fix

(PHP 5, PHP 7)

variant_fix — Returns the integer portion of a variant

설명

mixed variant_fix ( mixed $variant )

Gets the integer portion of a variant.

인수

variant

The variant.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

If variant is negative, then the first negative integer greater than or equal to the variant is returned, otherwise returns the integer portion of the value of variant.

주의

Warning

This documentation is based on the MSDN documentation; it appears that this function is either the same as variant_int(), or that there is an error in the MSDN documentation.

참고

  • variant_int() - Returns the integer portion of a variant
  • variant_round() - Rounds a variant to the specified number of decimal places
  • floor() - 소수점 아래 내림
  • ceil() - 소수점 아래 올림
  • round() - 소수 반올림

User Contributed Notes

There are no user contributed notes for this page.


Returns the type of a variant object

  • ≪ variant_fix
  • variant_idiv ≫
  • PHP Manual
  • COM 함수 목록
  • Returns the type of a variant object

variant_get_type

(PHP 5, PHP 7)

variant_get_type — Returns the type of a variant object

설명

int variant_get_type ( variant $variant )

Returns the type of a variant object.

인수

variant

The variant object.

반환값

This function returns an integer value that indicates the type of variant, which can be an instance of COM, DOTNET or VARIANT classes. The return value can be compared to one of the VT_XXX constants.

The return value for COM and DOTNET objects will usually be VT_DISPATCH; the only reason this function works for those classes is because COM and DOTNET are descendants of VARIANT.

In PHP versions prior to 5, you could obtain this information from instances of the VARIANT class ONLY, by reading a fake type property. See the VARIANT class for more information on this.

User Contributed Notes

There are no user contributed notes for this page.


Converts variants to integers and then returns the result from dividing them

  • ≪ variant_get_type
  • variant_imp ≫
  • PHP Manual
  • COM 함수 목록
  • Converts variants to integers and then returns the result from dividing them

variant_idiv

(PHP 5, PHP 7)

variant_idiv — Converts variants to integers and then returns the result from dividing them

설명

mixed variant_idiv ( mixed $left , mixed $right )

Converts left and right to integer values, and then performs integer division.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Variant Integer Division Rules
If | Then
Both expressions are of the string, date, character, boolean type | Division and integer is returned
One expression is a string type and the other a character | Division
One expression is numeric and the other is a string | Division
Both expressions are numeric | Division
Either expression is NULL | NULL is returned
Both expressions are empty | A com_exception with code DISP_E_DIVBYZERO is thrown

참고

  • variant_div() - Returns the result from dividing two variants

User Contributed Notes

There are no user contributed notes for this page.


Performs a bitwise implication on two variants

  • ≪ variant_idiv
  • variant_int ≫
  • PHP Manual
  • COM 함수 목록
  • Performs a bitwise implication on two variants

variant_imp

(PHP 5, PHP 7)

variant_imp — Performs a bitwise implication on two variants

설명

mixed variant_imp ( mixed $left , mixed $right )

Performs a bitwise implication operation.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Variant Implication Table
If left is | If right is | then the result is
TRUE | TRUE | TRUE
TRUE | FALSE | TRUE
TRUE | NULL | TRUE
FALSE | TRUE | TRUE
FALSE | FALSE | TRUE
FALSE | NULL | TRUE
NULL | TRUE | TRUE
NULL | FALSE | NULL
NULL | NULL | NULL

User Contributed Notes

There are no user contributed notes for this page.


Returns the integer portion of a variant

  • ≪ variant_imp
  • variant_mod ≫
  • PHP Manual
  • COM 함수 목록
  • Returns the integer portion of a variant

variant_int

(PHP 5, PHP 7)

variant_int — Returns the integer portion of a variant

설명

mixed variant_int ( mixed $variant )

Gets the integer portion of a variant.

인수

variant

The variant.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

If variant is negative, then the first negative integer greater than or equal to the variant is returned, otherwise returns the integer portion of the value of variant.

참고

  • variant_fix() - Returns the integer portion of a variant
  • variant_round() - Rounds a variant to the specified number of decimal places
  • floor() - 소수점 아래 내림
  • ceil() - 소수점 아래 올림
  • round() - 소수 반올림

User Contributed Notes

There are no user contributed notes for this page.


Divides two variants and returns only the remainder

  • ≪ variant_int
  • variant_mul ≫
  • PHP Manual
  • COM 함수 목록
  • Divides two variants and returns only the remainder

variant_mod

(PHP 5, PHP 7)

variant_mod — Divides two variants and returns only the remainder

설명

mixed variant_mod ( mixed $left , mixed $right )

Divides left by right and returns the remainder.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Returns the remainder of the division.

참고

  • variant_div() - Returns the result from dividing two variants
  • variant_idiv() - Converts variants to integers and then returns the result from dividing them

User Contributed Notes

There are no user contributed notes for this page.


Multiplies the values of the two variants

  • ≪ variant_mod
  • variant_neg ≫
  • PHP Manual
  • COM 함수 목록
  • Multiplies the values of the two variants

variant_mul

(PHP 5, PHP 7)

variant_mul — Multiplies the values of the two variants

설명

mixed variant_mul ( mixed $left , mixed $right )

Multiplies left by right.

인수

left

The left operand.

right

The right operand.

Boolean values are converted to -1 for FALSE and 0 for TRUE.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Variant Multiplication Rules
If | Then
Both expressions are of the string, date, character, boolean type | Multiplication
One expression is a string type and the other a character | Multiplication
One expression is numeric and the other is a string | Multiplication
Both expressions are numeric | Multiplication
Either expression is NULL | NULL is returned
Both expressions are empty | Empty string is returned

참고

  • variant_div() - Returns the result from dividing two variants
  • variant_idiv() - Converts variants to integers and then returns the result from dividing them

User Contributed Notes

There are no user contributed notes for this page.


Performs logical negation on a variant

  • ≪ variant_mul
  • variant_not ≫
  • PHP Manual
  • COM 함수 목록
  • Performs logical negation on a variant

variant_neg

(PHP 5, PHP 7)

variant_neg — Performs logical negation on a variant

설명

mixed variant_neg ( mixed $variant )

Performs logical negation of variant.

인수

variant

The variant.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Returns the result of the logical negation.

User Contributed Notes

There are no user contributed notes for this page.


Performs bitwise not negation on a variant

  • ≪ variant_neg
  • variant_or ≫
  • PHP Manual
  • COM 함수 목록
  • Performs bitwise not negation on a variant

variant_not

(PHP 5, PHP 7)

variant_not — Performs bitwise not negation on a variant

설명

mixed variant_not ( mixed $variant )

Performs bitwise not negation on variant and returns the result.

인수

variant

The variant.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Returns the bitwise not negation. If variant is NULL, the result will also be NULL.

User Contributed Notes

There are no user contributed notes for this page.


Performs a logical disjunction on two variants

  • ≪ variant_not
  • variant_pow ≫
  • PHP Manual
  • COM 함수 목록
  • Performs a logical disjunction on two variants

variant_or

(PHP 5, PHP 7)

variant_or — Performs a logical disjunction on two variants

설명

mixed variant_or ( mixed $left , mixed $right )

Performs a bitwise OR operation. Note that this is slightly different from a regular OR operation.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Variant OR Rules
If left is | If right is | then the result is
TRUE | TRUE | TRUE
TRUE | FALSE | TRUE
TRUE | NULL | TRUE
FALSE | TRUE | TRUE
FALSE | FALSE | FALSE
FALSE | NULL | NULL
NULL | TRUE | TRUE
NULL | FALSE | NULL
NULL | NULL | NULL

참고

  • variant_and() - Performs a bitwise AND operation between two variants
  • variant_xor() - Performs a logical exclusion on two variants

User Contributed Notes

There are no user contributed notes for this page.


Returns the result of performing the power function with two variants

  • ≪ variant_or
  • variant_round ≫
  • PHP Manual
  • COM 함수 목록
  • Returns the result of performing the power function with two variants

variant_pow

(PHP 5, PHP 7)

variant_pow — Returns the result of performing the power function with two variants

설명

mixed variant_pow ( mixed $left , mixed $right )

Returns the result of left to the power of right.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Returns the result of left to the power of right.

참고

  • pow() - 거듭제곱 표현

User Contributed Notes

There are no user contributed notes for this page.


Rounds a variant to the specified number of decimal places

  • ≪ variant_pow
  • variant_set_type ≫
  • PHP Manual
  • COM 함수 목록
  • Rounds a variant to the specified number of decimal places

variant_round

(PHP 5, PHP 7)

variant_round — Rounds a variant to the specified number of decimal places

설명

mixed variant_round ( mixed $variant , int $decimals )

Returns the value of variant rounded to decimals decimal places.

인수

variant

The variant.

decimals

Number of decimal places.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Returns the rounded value.

참고

  • round() - 소수 반올림

User Contributed Notes

There are no user contributed notes for this page.


Convert a variant into another type "in-place"

  • ≪ variant_round
  • variant_set ≫
  • PHP Manual
  • COM 함수 목록
  • Convert a variant into another type "in-place"

variant_set_type

(PHP 5, PHP 7)

variant_set_type — Convert a variant into another type "in-place"

설명

void variant_set_type ( variant $variant , int $type )

This function is similar to variant_cast() except that the variant is modified "in-place"; no new variant is created. The parameters for this function have identical meaning to those of variant_cast().

인수

variant

The variant.

type

반환값

값을 반환하지 않습니다.

참고

  • variant_cast() - Convert a variant into a new variant object of another type

User Contributed Notes

igor at dnestr dot com 13-Dec-2013 06:41
I need to pass VT_ARRAY | VT_BSTR | VT_BYREF in COM method but i haven't any idea how can do this.
martin at itmission dot com 22-May-2009 07:55
In my application I was using ADODB to retrieve data from Microsoft Access.  During queries, fields defined as "Date/Time" in Access were being returned to PHP as instances of the Variant class.

For some reason, the variant_date_to_timestamp function returned incorrect timestamps.  As a workaround, I used variant_set_type($variant, VT_BSTR) to get a string representation of the date.  This can be used for most variant types PHP doesn't handle.


Assigns a new value for a variant object

  • ≪ variant_set_type
  • variant_sub ≫
  • PHP Manual
  • COM 함수 목록
  • Assigns a new value for a variant object

variant_set

(PHP 5, PHP 7)

variant_set — Assigns a new value for a variant object

설명

void variant_set ( variant $variant , mixed $value )

Converts value to a variant and assigns it to the variant object; no new variant object is created, and the old value of variant is freed/released.

인수

variant

The variant.

value

반환값

값을 반환하지 않습니다.

User Contributed Notes

There are no user contributed notes for this page.


Subtracts the value of the right variant from the left variant value

  • ≪ variant_set
  • variant_xor ≫
  • PHP Manual
  • COM 함수 목록
  • Subtracts the value of the right variant from the left variant value

variant_sub

(PHP 5, PHP 7)

variant_sub — Subtracts the value of the right variant from the left variant value

설명

mixed variant_sub ( mixed $left , mixed $right )

Subtracts right from left.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Variant Subtraction Rules
If | Then
Both expressions are of the string type | Subtraction
One expression is a string type and the other a character | Subtraction
One expression is numeric and the other is a string | Subtraction.
Both expressions are numeric | Subtraction
Either expression is NULL | NULL is returned
Both expressions are empty | Empty string is returned

참고

  • variant_add() - "Adds" two variant values together and returns the result

User Contributed Notes

There are no user contributed notes for this page.


Performs a logical exclusion on two variants

  • ≪ variant_sub
  • win32ps ≫
  • PHP Manual
  • COM 함수 목록
  • Performs a logical exclusion on two variants

variant_xor

(PHP 5, PHP 7)

variant_xor — Performs a logical exclusion on two variants

설명

mixed variant_xor ( mixed $left , mixed $right )

Performs a logical exclusion.

인수

left

The left operand.

right

The right operand.

Note:

variant 연산 함수와 마찬가지로, 이 함수의 인수로는 PHP 자료형(정수, 문자열, 소수, 논리, NULL)이나 COM, VARIANT, DOTNET 클래스의 인스턴스를 사용할 수 있습니다. PHP 자료형은 VARIANT 클래스 생성자와 같은 방식으로 vriant로 변환됩니다. COM과 DOTNET 객체는 기본 프로퍼티 값을 variant 값으로 사용합니다.

variant 연산 함수는 비슷한 이름의 COM 라이브러리 함수의 래퍼입니다; 이 함수들에 관한 자세한 정보는 MSDN 라이브러리를 참조하십시오. PHP 함수 이름은 차이가 있습니다; 예를 들어, PHP에서 variant_add()는 MSDN 문서의 VarAdd()에 해당합니다.

반환값

Variant XOR Rules
If left is | If right is | then the result is
TRUE | TRUE | FALSE
TRUE | FALSE | TRUE
FALSE | TRUE | TRUE
FALSE | FALSE | FALSE
NULL | NULL | NULL

참고

  • variant_or() - Performs a logical disjunction on two variants
  • variant_and() - Performs a bitwise AND operation between two variants

User Contributed Notes

tinelbarb at yahoo dot com 30-Aug-2010 09:21
I've been using a lot the VARIANT_XOR function (all the VARIANT functions ar cool) and I was faceing the problem that some custom copilation of PHP, especially LAMP packs, doesn't have the VARANT functions included, so the scripts halts.
I had to find an alternative for those who doesn't have an implamentation of VARIANT_XOR.
I'd love if somebody improve my "A_XOR_B" function by changing the name in "VARIANT_XOR" and to run the original VARIANT_XOR function (being optimized) if it is already in the PHP compilation, else run the custom XOR code.

<?php
function a_xor_b($a=0, $b=0) {
    return ( ($a!=$b) && ($a||$b) ) ? TRUE : FALSE ;
}
?>

If using this function, make sure you use the same type for arguments ;-)

Here is a sample code using it:

<?php
if ( a_xor_b(strlen($column1)>0,strlen($column2)>0) ) {
  $add_and='';
}
if ( !a_xor_b(strlen($column1)>0,strlen($column2)>0 ) && strlen($column1)>0 ) {
  $add_and=' and ';
}
$some_sql_filter=$query_str.' having '.$column1.$add_and.$column2;
?>

The use of VARIANT_XOR was identical.

@PHP TEAM: the VARIANT set is so great :-)
tinelbarb at yahoo dot com 30-Aug-2010 09:20
I've been using a lot the VARIANT_XOR function (all the VARIANT functions ar cool) and I was faceing the problem that some custom copilation of PHP, especially LAMP packs, doesn't have the VARANT functions included, so the scripts halts.
I had to find an alternative for those who doesn't have an implamentation of VARIANT_XOR.
I'd love if somebody improve my "A_XOR_B" function by changing the name in "VARIANT_XOR" and to run the original VARIANT_XOR function (being optimized) if it is already in the PHP compilation, else run the custom XOR code.

<?php
function a_xor_b($a=0, $b=0) {
    return ( ($a!=$b) && ($a||$b) ) ? TRUE : FALSE ;
}
?>

If using this function, make sure you use the same type for arguments ;-)

Here is a sample code using it:

<?php
if ( a_xor_b(strlen($column1)>0,strlen($column2)>0) ) {
  $add_and='';
}
if ( !a_xor_b(strlen($column1)>0,strlen($column2)>0 ) && strlen($column1)>0 ) {
  $add_and=' and ';
}
$some_sql_filter=$query_str.' having '.$column1.$add_and.$column2;
?>

The use of VARIANT_XOR was identical.

@PHP TEAM: the VARIANT set is so great :-)
tinelbarb at yahoo dot com dot RE-MO-VE dot ME 30-Aug-2010 09:20
I've been using a lot the VARIANT_XOR function (all the VARIANT functions ar cool) and I was faceing the problem that some custom copilation of PHP, especially LAMP packs, doesn't have the VARANT functions included, so the scripts halts.
I had to find an alternative for those who doesn't have an implamentation of VARIANT_XOR.
I'd love if somebody improve my "A_XOR_B" function by changing the name in "VARIANT_XOR" and to run the original VARIANT_XOR function (being optimized) if it is already in the PHP compilation, else run the custom XOR code.

<?php
function a_xor_b($a=0, $b=0) {
    return ( ($a!=$b) && ($a||$b) ) ? TRUE : FALSE ;
}
?>

If using this function, make sure you use the same type for arguments ;-)

Here is a sample code using it:

<?php
if ( a_xor_b(strlen($column1)>0,strlen($column2)>0) ) {
  $add_and='';
}
if ( !a_xor_b(strlen($column1)>0,strlen($column2)>0 ) && strlen($column1)>0 ) {
  $add_and=' and ';
}
$some_sql_filter=$query_str.' having '.$column1.$add_and.$column2;
?>

The use of VARIANT_XOR was identical.

@PHP TEAM: the VARIANT set is so great :-)


win32ps

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
    • Win32ps examples
  • win32ps 함수 목록
    • win32_ps_list_procs — List running processes
    • win32_ps_stat_mem — Stat memory utilization
    • win32_ps_stat_proc — Stat process

User Contributed Notes

There are no user contributed notes for this page.


소개

The win32ps extension is a Windows specific extension that allows PHP retrieve statistics about process and memory utilization.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

Windows NT, Windows 2000, Windows XP or Windows Server 2003. Any version of Windows derived from Windows NT should be compatible.

User Contributed Notes

There are no user contributed notes for this page.


설치

PECL 확장 설치에 관한 정보는 매뉴얼의 PECL 확장 설치에 있습니다. 새 릴리즈, 내려받기, 소스파일, 개발자 정보, CHANGELOG 등의 추가 정보는 여기에 있습니다: » http://pecl.php.net/package/win32ps.

User Contributed Notes

There are no user contributed notes for this page.


예제

Table of Contents

  • Win32ps examples

User Contributed Notes

There are no user contributed notes for this page.


Win32ps examples

Example #1 Statistics about the current PHP process

<?php
print_r(win32_ps_stat_proc());
/*
    Array
    (
        [pid] => 936
        [exe] => D:\Daten\Source\php-5.1\Debug_TS\php.exe
        [mem] => Array
            (
                [page_fault_count] => 2062
                [peak_working_set_size] => 8396800
                [working_set_size] => 8396800
                [quota_peak_paged_pool_usage] => 32080
                [quota_paged_pool_usage] => 31876
                [quota_peak_non_paged_pool_usage] => 4240
                [quota_non_paged_pool_usage] => 3888
                [pagefile_usage] => 5865472
                [peak_pagefile_usage] => 5865472
            )

        [tms] => Array
            (
                [created] => 0.093
                [kernel] => 0.015
                [user] => 0.062
            )

    )
*/
?>

Example #2 Statistics about global memory utilization

<?php
print_r(win32_ps_stat_mem());
/*
    Array
    (
        [load] => 37
        [unit] => 1024
        [total_phys] => 1048096
        [avail_phys] => 649960
        [total_pagefile] => 2521368
        [avail_pagefile] => 2237940
        [total_virtual] => 2097024
        [avail_virtual] => 2057848
    )
*/
?>

User Contributed Notes

There are no user contributed notes for this page.


win32ps 함수 목록

Table of Contents

  • win32_ps_list_procs — List running processes
  • win32_ps_stat_mem — Stat memory utilization
  • win32_ps_stat_proc — Stat process

User Contributed Notes

There are no user contributed notes for this page.


List running processes

  • ≪ win32ps 함수 목록
  • win32_ps_stat_mem ≫
  • PHP Manual
  • win32ps 함수 목록
  • List running processes

win32_ps_list_procs

(PECL win32ps >= 1.0.1)

win32_ps_list_procs — List running processes

설명

array win32_ps_list_procs ( void )

Retrieves statistics about all running processes.

반환값

Returns FALSE on failure, or an array consisting of process statistics like win32_ps_stat_proc() returns for all running processes on success.

참고

  • win32_ps_stat_proc() - Stat process

User Contributed Notes

cf at chronofish dot com 10-Sep-2007 08:27
For those of you in the WAMP environment, here is a little function that you may find handy:

<?
function kill_proc($processName)
{
    @$processList = win32_ps_list_procs();
    foreach ($processList as $processArray)
    {
        $pid = $processArray['pid'];
        if (basename($processArray['exe']) == $processName)
        {
            `taskkill /f /PID $pid 2>&1`;
        }
    }
}
?>

-CF


Stat memory utilization

  • ≪ win32_ps_list_procs
  • win32_ps_stat_proc ≫
  • PHP Manual
  • win32ps 함수 목록
  • Stat memory utilization

win32_ps_stat_mem

(PECL win32ps >= 1.0.1)

win32_ps_stat_mem — Stat memory utilization

설명

array win32_ps_stat_mem ( void )

Retrieves statistics about the global memory utilization.

반환값

Returns FALSE on failure, or an array consisting of the following information on success:

load

The current memory load in percent of physical memory.

unit

This is always 1024, and indicates that the following values are the count of 1024 bytes.

total_phys

The amount of total physical memory.

avail_phys

The amount of still available physical memory.

total_pagefile

The amount of total pageable memory (physical memory + paging file).

avail_pagefile

The amount of still available pageable memory (physical memory + paging file).

total_virtual

The amount of total virtual memory for a process.

avail_virtual

The amount of still available virtual memory for a process.

User Contributed Notes

There are no user contributed notes for this page.


Stat process

  • ≪ win32_ps_stat_mem
  • win32service ≫
  • PHP Manual
  • win32ps 함수 목록
  • Stat process

win32_ps_stat_proc

(PECL win32ps >= 1.0.1)

win32_ps_stat_proc — Stat process

설명

array win32_ps_stat_proc ([ int $pid = 0 ] )

Retrieves statistics about the process with the process id pid.

인수

pid

The process id of the process to stat. If omitted, the id of the current process.

반환값

Returns FALSE on failure, or an array consisting of the following information on success:

pid

The process id.

exe

The path to the executable image.

mem

An array containing information about the following memory utilization indicators: page_fault_count, peak_working_set_size, working_set_size, quota_peak_paged_pool_usage, quota_paged_pool_usage, quota_peak_non_paged_pool_usage, quota_non_paged_pool_usage, pagefile_usage and peak_pagefile_usage.

tms

An array containing information about the following CPU time utilization indicators: created, kernel and user.

참고

  • win32_ps_list_procs() - List running processes

User Contributed Notes

There are no user contributed notes for this page.


win32service

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
    • Win32Service Service Type Bitmasks
    • Win32Service Service Status Constants
    • Win32Service Service Control Message Constants
    • Win32Service Service Control Message Accepted Bitmasks
    • Win32Service Service Start Type Constants
    • Win32Service Service Error Control Constants
    • Win32Service Service Flag Constants
    • Win32 Error Codes
    • Win32 Base Priority Classes
  • 예제
  • win32service 함수 목록
    • win32_continue_service — Resumes a paused service
    • win32_create_service — Creates a new service entry in the SCM database
    • win32_delete_service — Deletes a service entry from the SCM database
    • win32_get_last_control_message — Returns the last control message that was sent to this service
    • win32_pause_service — Pauses a service
    • win32_query_service_status — Queries the status of a service
    • win32_set_service_status — Update the service status
    • win32_start_service_ctrl_dispatcher — Registers the script with the SCM, so that it can act as the service with the given name
    • win32_start_service — Starts a service
    • win32_stop_service — Stops a service

User Contributed Notes

gospodin dot p dot zh at gmail dot com 04-Oct-2012 03:49
win32service extension does not realize very important (as for me) method of ServiceBase::OnCustomCommand
----
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.oncustomcommand.aspx
"OnCustomCommand lets you specify additional functionality beyond starting, stopping, pausing and continuing services."


소개

The win32service extension is a Windows specific extension that allows PHP to communicate with the Service Control Manager to start, stop, register and unregister services, and even allows your PHP scripts to run as a service.

User Contributed Notes

There are no user contributed notes for this page.


설치

PECL 확장 설치에 관한 정보는 매뉴얼의 PECL 확장 설치에 있습니다. 새 릴리즈, 내려받기, 소스파일, 개발자 정보, CHANGELOG 등의 추가 정보는 여기에 있습니다: » http://pecl.php.net/package/win32service

User Contributed Notes

There are no user contributed notes for this page.


예약 상수

Table of Contents

  • Win32Service Service Type Bitmasks
  • Win32Service Service Status Constants
  • Win32Service Service Control Message Constants
  • Win32Service Service Control Message Accepted Bitmasks
  • Win32Service Service Start Type Constants
  • Win32Service Service Error Control Constants
  • Win32Service Service Flag Constants
  • Win32 Error Codes
  • Win32 Base Priority Classes

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

User Contributed Notes

There are no user contributed notes for this page.


Win32Service Service Type Bitmasks

  • ≪ 예약 상수
  • Win32Service Service Status Constants ≫
  • PHP Manual
  • 예약 상수
  • Win32Service Service Type Bitmasks
Win32Service Service Type Bitmasks
Constant | Value | Description
WIN32_SERVICE_WIN32_OWN_PROCESS | 0x00000010 | The service runs in its own process.
WIN32_SERVICE_INTERACTIVE_PROCESS | 0x00000100 | The service can interact with the desktop. This option is not available on Windows Vista or later.
WIN32_SERVICE_WIN32_OWN_PROCESS_INTERACTIVE | 0x00000110 | The service runs in its own process and can interact with the desktop. This option is not available on Windows Vista or later.

User Contributed Notes

There are no user contributed notes for this page.


Win32Service Service Status Constants

  • ≪ Win32Service Service Type Bitmasks
  • Win32Service Service Control Message Constants ≫
  • PHP Manual
  • 예약 상수
  • Win32Service Service Status Constants
Win32Service Service Status Constants
Constant | Value | Description
WIN32_SERVICE_CONTINUE_PENDING | 0x00000005 | The service continue is pending.
WIN32_SERVICE_PAUSE_PENDING | 0x00000006 | The service pause is pending.
WIN32_SERVICE_PAUSED | 0x00000007 | The service is paused.
WIN32_SERVICE_RUNNING | 0x00000004 | The service is running.
WIN32_SERVICE_START_PENDING | 0x00000002 | The service is starting.
WIN32_SERVICE_STOP_PENDING | 0x00000003 | The service is stopping.
WIN32_SERVICE_STOPPED | 0x00000001 | The service is not running.

User Contributed Notes

There are no user contributed notes for this page.


Win32Service Service Control Message Constants

  • ≪ Win32Service Service Status Constants
  • Win32Service Service Control Message Accepted Bitmasks ≫
  • PHP Manual
  • 예약 상수
  • Win32Service Service Control Message Constants
Win32Service Service Control Message Constants
Constant | Value | Description
WIN32_SERVICE_CONTROL_CONTINUE | 0x00000003 | Notifies a paused service that it should resume.
WIN32_SERVICE_CONTROL_INTERROGATE | 0x00000004 | Notifies a service that it should report its current status information to the service control manager.
WIN32_SERVICE_CONTROL_PAUSE | 0x00000002 | Notifies a service that it should pause.
WIN32_SERVICE_CONTROL_PRESHUTDOWN | 0x0000000F | Notifies a service that the system will be shutting down. A service that handles this notification blocks system shutdown until the service stops or the preshutdown time-out interval expires. This value is not supported by Windows Server 2003 and Windows XP/2000.
WIN32_SERVICE_CONTROL_SHUTDOWN | 0x00000005 | Notifies a service that the system is shutting down so the service can perform cleanup tasks. If a service accepts this control code, it must stop after it performs its cleanup tasks. After the SCM sends this control code, it will not send other control codes to the service.
WIN32_SERVICE_CONTROL_STOP | 0x00000001 | Notifies a service that it should stop.

User Contributed Notes

There are no user contributed notes for this page.


Win32Service Service Control Message Accepted Bitmasks

  • ≪ Win32Service Service Control Message Constants
  • Win32Service Service Start Type Constants ≫
  • PHP Manual
  • 예약 상수
  • Win32Service Service Control Message Accepted Bitmasks
Win32Service Service Control Message Accepted Bitmasks
Constant | Value | Description
WIN32_SERVICE_ACCEPT_PAUSE_CONTINUE | 0x00000002 | The service can be paused and continued. This control code allows the service to receive WIN32_SERVICE_CONTROL_PAUSE and WIN32_SERVICE_CONTROL_CONTINUE notifications.
WIN32_SERVICE_ACCEPT_PRESHUTDOWN | 0x00000100 | The service can perform preshutdown tasks. This control code enables the service to receive WIN32_SERVICE_CONTROL_PRESHUTDOWN notifications. This value is not supported by Windows Server 2003 and Windows XP/2000.
WIN32_SERVICE_ACCEPT_SHUTDOWN | 0x00000004 | The service is notified when system shutdown occurs. This control code allows the service to receive WIN32_SERVICE_CONTROL_SHUTDOWN notifications.
WIN32_SERVICE_ACCEPT_STOP | 0x00000001 | The service can be stopped. This control code allows the service to receive WIN32_SERVICE_CONTROL_STOP notifications.

User Contributed Notes

There are no user contributed notes for this page.


Win32Service Service Start Type Constants

  • ≪ Win32Service Service Control Message Accepted Bitmasks
  • Win32Service Service Error Control Constants ≫
  • PHP Manual
  • 예약 상수
  • Win32Service Service Start Type Constants
Win32Service Service Start Type Constants
Constant | Value | Description
WIN32_SERVICE_AUTO_START | 0x00000002 | A service started automatically by the service control manager during system startup.
WIN32_SERVICE_DEMAND_START | 0x00000003 | A service started by the service control manager when a process calls the StartService function.
WIN32_SERVICE_DISABLED | 0x00000004 | A service that cannot be started. Attempts to start the service result in the error code WIN32_ERROR_SERVICE_DISABLED.

User Contributed Notes

There are no user contributed notes for this page.


Win32Service Service Error Control Constants

  • ≪ Win32Service Service Start Type Constants
  • Win32Service Service Flag Constants ≫
  • PHP Manual
  • 예약 상수
  • Win32Service Service Error Control Constants
Win32Service Service Error Control Constants
Constant | Value | Description
WIN32_SERVICE_ERROR_IGNORE | 0x00000000 | The startup program ignores the error and continues the startup operation.
WIN32_SERVICE_ERROR_NORMAL | 0x00000001 | The startup program logs the error in the event log but continues the startup operation.

User Contributed Notes

There are no user contributed notes for this page.


Win32Service Service Flag Constants

  • ≪ Win32Service Service Error Control Constants
  • Win32 Error Codes ≫
  • PHP Manual
  • 예약 상수
  • Win32Service Service Flag Constants
Win32Service Service Flag Constants
Constant | Value | Description
WIN32_SERVICE_RUNS_IN_SYSTEM_PROCESS | 0x00000001 | The service runs in a system process that must always be running.

User Contributed Notes

There are no user contributed notes for this page.


Win32 Error Codes

  • ≪ Win32Service Service Flag Constants
  • Win32 Base Priority Classes ≫
  • PHP Manual
  • 예약 상수
  • Win32 Error Codes
Win32 Error Codes
Constant | Value | Description
WIN32_ERROR_ACCESS_DENIED | 0x00000005 | The handle to the SCM database does not have the appropriate access rights.
WIN32_ERROR_CIRCULAR_DEPENDENCY | 0x00000423 | A circular service dependency was specified.
WIN32_ERROR_DATABASE_DOES_NOT_EXIST | 0x00000429 | The specified database does not exist.
WIN32_ERROR_DEPENDENT_SERVICES_RUNNING | 0x0000041B | The service cannot be stopped because other running services are dependent on it.
WIN32_ERROR_DUPLICATE_SERVICE_NAME | 0x00000436 | The display name already exists in the service control manager database either as a service name or as another display name.
WIN32_ERROR_FAILED_SERVICE_CONTROLLER_CONNECT | 0x00000427 | This error is returned if the program is being run as a console application rather than as a service. If the program will be run as a console application for debugging purposes, structure it such that service-specific code is not called.
WIN32_ERROR_INSUFFICIENT_BUFFER | 0x0000007A | The buffer is too small for the service status structure. Nothing was written to the structure.
WIN32_ERROR_INVALID_DATA | 0x0000000D | The specified service status structure is invalid.
WIN32_ERROR_INVALID_HANDLE | 0x00000006 | The handle to the specified service control manager database is invalid.
WIN32_ERROR_INVALID_LEVEL | 0x0000007C | The InfoLevel parameter contains an unsupported value.
WIN32_ERROR_INVALID_NAME | 0x0000007B | The specified service name is invalid.
WIN32_ERROR_INVALID_PARAMETER | 0x00000057 | A parameter that was specified is invalid.
WIN32_ERROR_INVALID_SERVICE_ACCOUNT | 0x00000421 | The user account name specified in the user parameter does not exist. See win32_create_service().
WIN32_ERROR_INVALID_SERVICE_CONTROL | 0x0000041C | The requested control code is not valid, or it is unacceptable to the service.
WIN32_ERROR_PATH_NOT_FOUND | 0x00000003 | The service binary file could not be found.
WIN32_ERROR_SERVICE_ALREADY_RUNNING | 0x00000420 | An instance of the service is already running.
WIN32_ERROR_SERVICE_CANNOT_ACCEPT_CTRL | 0x00000425 | The requested control code cannot be sent to the service because the state of the service is WIN32_SERVICE_STOPPED, WIN32_SERVICE_START_PENDING, or WIN32_SERVICE_STOP_PENDING.
WIN32_ERROR_SERVICE_DATABASE_LOCKED | 0x0000041F | The database is locked.
WIN32_ERROR_SERVICE_DEPENDENCY_DELETED | 0x00000433 | The service depends on a service that does not exist or has been marked for deletion.
WIN32_ERROR_SERVICE_DEPENDENCY_FAIL | 0x0000042C | The service depends on another service that has failed to start.
WIN32_ERROR_SERVICE_DISABLED | 0x00000422 | The service has been disabled.
WIN32_ERROR_SERVICE_DOES_NOT_EXIST | 0x00000424 | The specified service does not exist as an installed service.
WIN32_ERROR_SERVICE_EXISTS | 0x00000431 | The specified service already exists in this database.
WIN32_ERROR_SERVICE_LOGON_FAILED | 0x0000042D | The service did not start due to a logon failure. This error occurs if the service is configured to run under an account that does not have the "Log on as a service" right.
WIN32_ERROR_SERVICE_MARKED_FOR_DELETE | 0x00000430 | The specified service has already been marked for deletion.
WIN32_ERROR_SERVICE_NO_THREAD | 0x0000041E | A thread could not be created for the service.
WIN32_ERROR_SERVICE_NOT_ACTIVE | 0x00000426 | The service has not been started.
WIN32_ERROR_SERVICE_REQUEST_TIMEOUT | 0x0000041D | The process for the service was started, but it did not call StartServiceCtrlDispatcher, or the thread that called StartServiceCtrlDispatcher may be blocked in a control handler function.
WIN32_ERROR_SHUTDOWN_IN_PROGRESS | 0x0000045B | The system is shutting down; this function cannot be called.
WIN32_NO_ERROR | 0x00000000 | No error.

User Contributed Notes

There are no user contributed notes for this page.


Win32 Base Priority Classes

  • ≪ Win32 Error Codes
  • 예제 ≫
  • PHP Manual
  • 예약 상수
  • Win32 Base Priority Classes
Win32 Base Priority Classes
Constant | Value | Description
WIN32_ABOVE_NORMAL_PRIORITY_CLASS | 0x00008000 | Process that has priority above WIN32_NORMAL_PRIORITY_CLASS but below WIN32_HIGH_PRIORITY_CLASS.
WIN32_BELOW_NORMAL_PRIORITY_CLASS | 0x00004000 | Process that has priority above WIN32_IDLE_PRIORITY_CLASS but below WIN32_NORMAL_PRIORITY_CLASS.
WIN32_HIGH_PRIORITY_CLASS | 0x00000080 | Process that performs time-critical tasks that must be executed immediately. The threads of the process preempt the threads of normal or idle priority class processes. An example is the Task List, which must respond quickly when called by the user, regardless of the load on the operating system. Use extreme care when using the high-priority class, because a high-priority class application can use nearly all available CPU time.
WIN32_IDLE_PRIORITY_CLASS | 0x00000040 | Process whose threads run only when the system is idle. The threads of the process are preempted by the threads of any process running in a higher priority class. An example is a screen saver. The idle-priority class is inherited by child processes.
WIN32_NORMAL_PRIORITY_CLASS | 0x00000020 | Process with no special scheduling needs.
WIN32_REALTIME_PRIORITY_CLASS | 0x00000100 | Process that has the highest possible priority. The threads of the process preempt the threads of all other processes, including operating system processes performing important tasks. For example, a real-time process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive.

User Contributed Notes

There are no user contributed notes for this page.


예제

Example #1 Registering a PHP script to run as a service

<?php
win32_create_service(array(
    'service'     => 'dummyphp',                                           # the name of your service
    'display'     => 'sample dummy PHP service',                           # short description
    'description' => 'This is a dummy Windows service created using PHP.', # long description
    'params'      => '"' . __FILE__ . '"  run',                            # path to the script and parameters
));
?>

Example #2 Unregistering a service

<?php
win32_delete_service('dummyphp');
?>

Example #3 Running as a service

<?php
if ($argv[1] == 'run') {
  win32_start_service_ctrl_dispatcher('dummyphp');

  while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
    # do your work here.
    # try not to take up more than 30 seconds before going around the loop
    # again
  }
}
?>

User Contributed Notes

There are no user contributed notes for this page.


win32service 함수 목록

Table of Contents

  • win32_continue_service — Resumes a paused service
  • win32_create_service — Creates a new service entry in the SCM database
  • win32_delete_service — Deletes a service entry from the SCM database
  • win32_get_last_control_message — Returns the last control message that was sent to this service
  • win32_pause_service — Pauses a service
  • win32_query_service_status — Queries the status of a service
  • win32_set_service_status — Update the service status
  • win32_start_service_ctrl_dispatcher — Registers the script with the SCM, so that it can act as the service with the given name
  • win32_start_service — Starts a service
  • win32_stop_service — Stops a service

User Contributed Notes

brian dot ngure at gmail dot com 30-Apr-2010 12:03
An example of how it should be done:

<?php

class TestClass {
    private $args;

    function __construct($arg) {
        $this->args = $arg;
        $this->run();
    }

    private function run() {
        if ($this->args == 'run') {
            win32_start_service_ctrl_dispatcher('test_service');

            while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
                # do your work here.
                # try not to take up more than 30 seconds before going around the loop
                # again
            }
        }
    }
}

$object = new TestClass($argv[1]);

?>
me at sylvain tiret besse point fr 20-Nov-2007 09:45
Just to help a little, the service control command must be in the first script called. If you try to type this portion of code in a separate file and include it, the SCM will not get the controls.
example :

- file1.inc :
<?PHP
class MyClass{

 function __construct() {

    $x = win32_start_service_ctrl_dispatcher('service');

    while (WIN32_SERVICE_CONTROL_STOP!=win32_get_last_control_message()){
        //your code
  }
}
?>

- service.php :
<?PHP
include("file1.inc");
$object = new MyClass();

?>

WON'T WORK !


Resumes a paused service

  • ≪ win32service 함수 목록
  • win32_create_service ≫
  • PHP Manual
  • win32service 함수 목록
  • Resumes a paused service

win32_continue_service

(PECL win32service SVN)

win32_continue_service — Resumes a paused service

설명

int win32_continue_service ( string $servicename [, string $machine ] )

Resumes a paused, named service. Requires administrative privileges.

인수

servicename

The short name of the service.

machine

Optional machine name. If omitted, the local machine is used.

반환값

Returns WIN32_NO_ERROR on success, FALSE if there is a problem with the parameters or a Win32 Error Code on failure.

참고

  • win32_start_service() - Starts a service
  • Win32 Error Codes

User Contributed Notes

There are no user contributed notes for this page.


Creates a new service entry in the SCM database

  • ≪ win32_continue_service
  • win32_delete_service ≫
  • PHP Manual
  • win32service 함수 목록
  • Creates a new service entry in the SCM database

win32_create_service

(PECL win32service SVN)

win32_create_service — Creates a new service entry in the SCM database

설명

mixed win32_create_service ( array $details [, string $machine ] )

인수

details

An array of service details:

service

The short name of the service. This is the name that you will use to control the service using the net command. The service must be unique (no two services can share the same name), and, ideally, should avoid having spaces in the name.

display

The display name of the service. This is the name that you will see in the Services Applet.

description

The long description of the service. This is the description that you will see in the Services Applet.

user

The name of the user account under which you want the service to run. If omitted, the service will run as the LocalSystem account. If the username is specified, you must also provide a password.

password

The password that corresponds to the user.

path

The full path to the executable module that will be launched when the service is started. If omitted, the path to the current PHP process will be used.

params

Command line parameters to pass to the service when it starts. If you want to run a PHP script as the service, then the first parameter should be the full path to the PHP script that you intend to run. If the script name or path contains spaces, then wrap the full path to the PHP script with ".

load_order

Controls the load_order. This is not yet fully supported.

svc_type

Sets the service type. If omitted, the default value is WIN32_SERVICE_WIN32_OWN_PROCESS. Don't change this unless you know what you're doing.

start_type

Specifies how the service should be started. The default is WIN32_SERVICE_AUTO_START which means the service will be launched when the machine starts up.

error_control

Informs the SCM what it should do when it detects a problem with the service. The default is WIN32_SERVER_ERROR_IGNORE. Changing this value is not yet fully supported.

delayed_start

If delayed_start is set to TRUE, then this will inform the SCM that this service should be started after other auto-start services are started plus a short delay.

Any service can be marked as a delayed auto-start service; however, this setting has no effect unless the service's start_type is WIN32_SERVICE_AUTO_START.

This setting is only applicable on Windows Vista and Windows Server 2008 or greater.

base_priority

To reduce the impact on processor utilisation, it may be necessary to set a base priority lower than normal.

The base_priority can be set to one of the constants define in Win32 Base Priority Classes.

machine

The optional machine name on which you want to create a service. If omitted, it will use the local machine.

반환값

Returns WIN32_NO_ERROR on success, FALSE if there is a problem with the parameters or a Win32 Error Code on failure.

예제

Example #1 A win32_create_service() example

Create a service with the short name 'dummyphp'.

<?php
$x = win32_create_service(array(
    'service'     => 'dummyphp',                                           # the name of your service
    'display'     => 'sample dummy PHP service',                           # short description
    'description' => 'This is a dummy Windows service created using PHP.', # long description
    'params'      => '"' . __FILE__ . '"  run',                            # path to the script and parameters
));
debug_zval_dump($x);
?>

참고

  • win32_delete_service() - Deletes a service entry from the SCM database
  • Win32 Base Priority Classes
  • Win32 Error Codes

User Contributed Notes

Anonymous 08-Oct-2010 07:05
[An example of how to create a Windows service.  Evaluate code first and use at your own risk!]

<?php

//No timeouts, Flush Content immediatly
    set_time_limit(0);
    ob_implicit_flush();

//Service Settings
    $phpPath = "D:\\php\\php5.2.9";
    $ServiceName = 'phpServiceName';
    $ServiceDisplay = 'phpDisplayName';

//Windows Service Control
    $ServiceAction = "status";
    //$ServiceAction = "debug";
    if ( isset($_GET['ServiceAction']) and strlen($_GET['ServiceAction']) ) {
        $ServiceAction = addslashes($_GET['ServiceAction']);
    } else if ( isset($argv) and isset($argv[1]) and strlen($argv[1]) ) {
        $ServiceAction = $argv[1];
    }
    if( $ServiceAction == "status" ) {
        $ServiceStatus = win32_query_service_status($ServiceName);
        if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_STOPPED ) {
            echo "Service Stopped\n\n";
        } else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_START_PENDING ) {
            echo "Service Start Pending\n\n";
        } else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_STOP_PENDING ) {
            echo "Service Stop Pending\n\n";
        } else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_RUNNING ) {
            echo "Service Running\n\n";
        } else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_CONTINUE_PENDING ) {
            echo "Service Continue Pending\n\n";
        } else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_PAUSE_PENDING ) {
            echo "Service Pause Pending\n\n";
        } else if ( $ServiceStatus['CurrentState'] == WIN32_SERVICE_PAUSED ) {
            echo "Service Paused\n\n";
        } else{
            echo "Service Unknown\n\n";
        }
      exit;
    } else if ( $ServiceAction == "install" ) {
    //Install Windows Service
        win32_create_service( Array(
            'service' => $ServiceName,
            'display' => $ServiceDisplay,
            'params' => __FILE__ . " run",
            'path' => $phpPath."\\php.exe",
        ));
        echo "Service Installed\n\n";
        exit;
    } else if ( $ServiceAction == "uninstall" ) {
    //Remove Windows Service
        win32_delete_service($ServiceName);
        echo "Service Removed\n\n";
        exit;
    } else if( $ServiceAction == "start") {
    //Start Windows Service
      win32_start_service($ServiceName);
      echo "Service Started\n\n";
      exit;
    } else if( $ServiceAction == "stop" ) {
    //Stop Windows Service
      win32_stop_service($ServiceName);
      echo "Service Stopped\n\n";
      exit;
    } else if ( $ServiceAction == "run" ) {
    //Run Windows Service
        win32_start_service_ctrl_dispatcher($ServiceName);
        win32_set_service_status(WIN32_SERVICE_RUNNING);
    } else if ( $ServiceAction == "debug" ) {
    //Debug Windows Service
        set_time_limit(10);
    } else {
        exit();
    }

//Server Loop
    while (1) {
    //Handle Windows Service Request
        usleep(100*1000);
        if ( $ServiceAction == "run" ) {
            switch ( win32_get_last_control_message() ) {
                case WIN32_SERVICE_CONTROL_CONTINUE:
                    break;
                case WIN32_SERVICE_CONTROL_INTERROGATE:
                    win32_set_service_status(WIN32_NO_ERROR);
                break;
                case WIN32_SERVICE_CONTROL_STOP:
                    win32_set_service_status(WIN32_SERVICE_STOPPED);
                    exit;
                default:
                    win32_set_service_status(WIN32_ERROR_CALL_NOT_IMPLEMENTED);
            }
        }
    //User Loop
        sleep(1);
        echo "\n<BR>YOUR CODE HERE";
    }

//Exit
    if ( $ServiceAction == "run" ) {
        win32_set_service_status(WIN32_SERVICE_STOPPED);
    }
    exit();
?>
pauljamesthomson at gmail dot com 22-Jan-2007 01:54
Other start_type values:

0x00000002: A service started automatically by the service control manager during system startup. For more information, see Automatically Starting Services.

0x00000000: A device driver started by the system loader. This value is valid only for driver services.

0x00000003: A service started by the service control manager when a process calls the win32_start_service() function.

0x00000004: A service that cannot be started. Attempts to start the service result in the error code

0x00000001: A device driver started by the IoInitSystem function. This value is valid only for driver services.

I can confirm that 0x00000003 works as expected (service is created, but must be started manually).

More here:

http://msdn2.microsoft.com/en-us/library/ms682450.aspx


Deletes a service entry from the SCM database

  • ≪ win32_create_service
  • win32_get_last_control_message ≫
  • PHP Manual
  • win32service 함수 목록
  • Deletes a service entry from the SCM database

win32_delete_service

(PECL win32service SVN)

win32_delete_service — Deletes a service entry from the SCM database

설명

mixed win32_delete_service ( string $servicename [, string $machine ] )

Attempts to delete a service from the SCM database. Administrative privileges are required for this to succeed.

This function really just marks the service for deletion. If other processes (such as the Services Applet) are open, then the deletion will be deferred until those applications are closed. If a service is marked for deletion, further attempts to delete it will fail, and attempts to create a new service with that name will also fail.

인수

servicename

The short name of the service.

machine

The optional machine name. If omitted, the local machine will be used.

반환값

Returns WIN32_NO_ERROR on success, FALSE if there is a problem with the parameters or a Win32 Error Code on failure.

예제

Example #1 A win32_delete_service() example

Deletes the dummyphp service.

<?php
win32_delete_service('dummyphp');
?>

참고

  • win32_create_service() - Creates a new service entry in the SCM database
  • Win32 Error Codes

User Contributed Notes

There are no user contributed notes for this page.


Returns the last control message that was sent to this service

  • ≪ win32_delete_service
  • win32_pause_service ≫
  • PHP Manual
  • win32service 함수 목록
  • Returns the last control message that was sent to this service

win32_get_last_control_message

(PECL win32service SVN)

win32_get_last_control_message — Returns the last control message that was sent to this service

설명

int win32_get_last_control_message ( void )

Returns the control code that was last sent to this service process. When running as a service you should periodically check this to determine if your service needs to stop running.

반환값

Returns a control constant which will be one of the Win32Service Service Control Message Constants: WIN32_SERVICE_CONTROL_CONTINUE, WIN32_SERVICE_CONTROL_INTERROGATE, WIN32_SERVICE_CONTROL_PAUSE, WIN32_SERVICE_CONTROL_PRESHUTDOWN, WIN32_SERVICE_CONTROL_SHUTDOWN, WIN32_SERVICE_CONTROL_STOP.

참고

  • win32_start_service_ctrl_dispatcher() - Registers the script with the SCM, so that it can act as the service with the given name
  • Win32Service Service Control Message Constants

User Contributed Notes

kermodebea at kermodebear dot Oh Are Gee 02-May-2006 08:35
The definitions of some of these control codes can be found here (URL broken due to comment system, sorry about that!): http://msdn.microsoft.com/library
/default.asp?url=/library/en-us/dllproc/base/handler.asp

However, for your convenience (and mine):
SERVICE_CONTROL_STOP, 0x00000001
Notifies a service that it should stop.

If a service accepts this control code, it must stop upon receipt. After the SCM sends this control code, it does not send other control codes.

Windows XP/2000:  If the service returns NO_ERROR and continues to run, it continues to receive control codes. This behavior changed starting with Windows Server 2003 and Windows XP SP2.

SERVICE_CONTROL_PAUSE, 0x00000002
Notifies a service that it should pause.
(Although I guess that there is no requirement that you must.)

SERVICE_CONTROL_CONTINUE, 0x00000003
Notifies a paused service that it should resume.

SERVICE_CONTROL_INTERROGATE, 0x00000004
Notifies a service that it should report its current status information to the service control manager.
(I'm guessing that a call to win32_set_service_status() would satisfy interrogation?)

SERVICE_CONTROL_SHUTDOWN, 0x00000005
Notifies a service that the system is shutting down so the service can perform cleanup tasks.


Pauses a service

  • ≪ win32_get_last_control_message
  • win32_query_service_status ≫
  • PHP Manual
  • win32service 함수 목록
  • Pauses a service

win32_pause_service

(PECL win32service SVN)

win32_pause_service — Pauses a service

설명

int win32_pause_service ( string $servicename [, string $machine ] )

Pauses a named service. Requires administrative privileges.

인수

servicename

The short name of the service.

machine

Optional machine name. If omitted, the local machine is used.

반환값

Returns WIN32_NO_ERROR on success, FALSE if there is a problem with the parameters or a Win32 Error Code on failure.

참고

  • win32_start_service() - Starts a service
  • Win32 Error Codes

User Contributed Notes

There are no user contributed notes for this page.


Queries the status of a service

  • ≪ win32_pause_service
  • win32_set_service_status ≫
  • PHP Manual
  • win32service 함수 목록
  • Queries the status of a service

win32_query_service_status

(PECL win32service SVN)

win32_query_service_status — Queries the status of a service

설명

mixed win32_query_service_status ( string $servicename [, string $machine ] )

Queries the current status for a service, returning an array of information.

인수

servicename

The short name of the service.

machine

The optional machine name. If omitted, the local machine will be used.

반환값

Returns an array consisting of the following information on success, FALSE if there is a problem with the parameters or a Win32 Error Code on failure.

ServiceType

The dwServiceType. See Win32Service Service Type Bitmasks.

CurrentState

The dwCurrentState. See Win32Service Service Status Constants.

ControlsAccepted

Which service controls are accepted by the service. See Win32Service Service Control Message Accepted Bitmasks.

Win32ExitCode

If the service exited, the return code from the process.

ServiceSpecificExitCode

If the service exited with an error condition, the service specific code that is logged in the event log is visible here.

CheckPoint

If the service is shutting down, holds the current check point number. This is used by the SCM as a kind of heart-beat to detect a wedged service process. The value of the check point is best interpreted in conjunction with the WaitHint value.

WaitHint

If the service is shutting down it will set WaitHint to a checkpoint value that will indicate 100% completion. This can be used to implement a progress indicator.

ProcessId

The Windows process identifier. If 0, the process is not running.

ServiceFlags

The dwServiceFlags. See Win32Service Service Service Flag Constants.

참고

  • Win32Service Predefined Constants

User Contributed Notes

demers dot alex at gmail dot com 21-Jul-2009 05:39
This function will return an array containing the above information as a return value, but if this fails it will return an integer which is a System Error Code. All the System Error Codes can be found here:

http://msdn.microsoft.com/en-us/library/ms681381%28VS.85%29.aspx

In my case, it returned 5, in which I immediately knew why and fixed the issue right away.

In the case of mnemotronic at netscape dot net, here in the documentation notes, it returned 1060 which is:

ERROR_SERVICE_DOES_NOT_EXIST
1060 (0x424)
The specified service does not exist as an installed service.

Again, it should not return FALSE, instead an System Error Code for Windows.


Update the service status

  • ≪ win32_query_service_status
  • win32_start_service_ctrl_dispatcher ≫
  • PHP Manual
  • win32service 함수 목록
  • Update the service status

win32_set_service_status

(PECL win32service SVN)

win32_set_service_status — Update the service status

설명

bool win32_set_service_status ( int $status [, int $checkpoint = 0 ] )

Informs the SCM of the current status of a running service. This call is only valid for a running service process.

인수

status

The service status code, one of WIN32_SERVICE_RUNNING, WIN32_SERVICE_STOPPED, WIN32_SERVICE_STOP_PENDING, WIN32_SERVICE_START_PENDING, WIN32_SERVICE_CONTINUE_PENDING, WIN32_SERVICE_PAUSE_PENDING, WIN32_SERVICE_PAUSED.

checkpoint

The checkpoint value the service increments periodically to report its progress during a lengthy start, stop, pause, or continue operation. For example, the service should increment this value as it completes each step of its initialization when it is starting up.

The checkpoint is only valid when the status is one of WIN32_SERVICE_STOP_PENDING, WIN32_SERVICE_START_PENDING, WIN32_SERVICE_CONTINUE_PENDING or WIN32_SERVICE_PAUSE_PENDING.

반환값

Returns TRUE on success, FALSE if there is a problem with the parameters or a Win32 Error Code on failure.

참고

  • win32_start_service_ctrl_dispatcher() - Registers the script with the SCM, so that it can act as the service with the given name
  • Win32Service Service Status Constants

User Contributed Notes

paul_nelson57071 at yahoo dot com 13-May-2010 08:04
So after much searching I finally found all the peices to make a working windows service & server including:
- Installing & Uninstalling the service
- Starting & Stopping the Service
- Actually running a basic Server
- Responding to windows Service to Stop/Start/Restart

I hope you find this useful! I did this using PHP 5.2 and the PECL library for 5.2 (PHP 5.3 at the time does not have a PECL Library compiled for it)

<?php
    //Service Settings
    $Service = 'MyServiceFinal';
    $Display = 'My PHP Service';

    //Server Settings
    $host = '127.0.0.1';        //Bind to IP #
    $port = 23;                            //On port #
    $max = 20;                            //Maximum Clients

    //Exit if no start-up argument is passed
    if (!isset($argv[1])){ShowHelp(); exit;}

    //Create Windows Service
    if ($argv[1] == 'install') {
    $x = win32_create_service(array(
        'service' => $Service,
        'display' => $Service,
        'params' => __file__ . ' run',
        'path' => 'c:\\php\\php.exe',
    ));
    debug_zval_dump($x);
    echo "Service Installed\n\n";
    exit;

  //Remove Windows Service
    } else if ($argv[1] == 'uninstall') {
        $x = win32_delete_service($Service);
        debug_zval_dump($x);
        echo "Service Removed\n\n";
        exit;

  //Start Windows Service
    } elseif($argv[1] == "start") {
      $x = win32_start_service($Service);
      debug_zval_dump($x);
      echo "Service Started\n\n";
      exit;

    //Stop Windows Service
    } elseif($argv[1] == "stop") {
      $x = win32_stop_service($Service);
      debug_zval_dump($x);
      echo "Service Stopped\n\n";
      exit;

    //Unkown Command
    } else if ($argv[1] != 'run') {
        ShowHelp(); exit();
    }

//Tell windows the service has started...
if (!win32_start_service_ctrl_dispatcher($Service)) die("Could not connect to service : $Service");
win32_set_service_status(WIN32_SERVICE_RUNNING);

// No timeouts, flush content immediatly
set_time_limit(0);
ob_implicit_flush();

//Server Loop
while (1) {
    usleep(500);

    //Handle Windows Service Request
    switch (win32_get_last_control_message()) {
        case WIN32_SERVICE_CONTROL_CONTINUE: break; // Continue server routine
        case WIN32_SERVICE_CONTROL_INTERROGATE: win32_set_service_status(WIN32_NO_ERROR); break; // Respond with status
        case WIN32_SERVICE_CONTROL_STOP: win32_set_service_status(WIN32_SERVICE_STOPPED); exit; // Terminate script
        default: win32_set_service_status(WIN32_ERROR_CALL_NOT_IMPLEMENTED); // Add more cases to handle other service calls
    }
    //Acutal Server Code

}

//Clean up and Exit
win32_set_service_status(WIN32_SERVICE_STOPPED);
exit;

//*************** FUNCTIONS ***************
function ShowHelp(){
    echo "Usage:
    install:\t installs servce
    uninstall:\t deletes service
    start:\t\t Start the windows service
    stop:\t\t Stops the windows service
    run:\t\t called by CMS to run the service

For information on code numbers type 'net helpmsg xxxx'
EXAMPEL: 'net helpmsg 1072'

    ";
}
?>
m dot hald at surfto dot ch 12-May-2008 01:16
On my Machine (Win XP SP2) the function "win32_get_last_control_message" never returns 4 so i will have to reply to 0 also.

finally after mixing several examples i now have a version working woithout filling my event log with errors:

<?php

/*
 * PHP SAMPLE TEMPLATE
 */

/*
 * APP MAIN FUNCTION
 * Called each second after the function last time finished
 * Should not take longer than max 20 seconds to execute
 * ELSE you should call NTServiceResponder(); at least each 20 secs
 * WARNING: NTServiceResponder will exit; if there was a STOP request
 */
function Service_Main()
{
    // YOU APPLICATION CODE HERE !!!
    sleep(1); // dummy something
}

/********************************************************************
 *
 * SERVICE CONTROLLING
 *
 ********************************************************************/

$SERVICE_NAME = "phptestservice";
$SERVICE_DISPLAY = "Test Service with PHP";

// so u can get: $SERVICE_PATH_PARTS["dirname"] $SERVICE_PATH_PARTS["basename"]  $SERVICE_PATH_PARTS["extension"]
$SERVICE_PATH_PARTS = pathinfo(__FILE__);

$SERVICE_PARAMS = " run";

    if (!isset($argv[1]))
    {
        die("this application need to be installed as a service.\n run with param install");
    }

    if ($argv[1] == 'install')
    {
        $x = win32_create_service(array(
                                        'service' => $SERVICE_NAME,
                                        'display' => $SERVICE_DISPLAY,
                                        'params' =>  __FILE__ . $SERVICE_PARAMS,
                                        //'path' =>  $SERVICE_PATH_PARTS["dirname"] . '\php.exe'
                                        ));
        debug_zval_dump($x);
        exit;
    }
    else if ($argv[1] == 'uninstall')
    {
        $x = win32_delete_service('dummyphp');
        debug_zval_dump($x);
        exit;
    }
    else if ($argv[1] != 'run')
    {
        die("bogus args, needs to run as service");
    }

    // Connect to service dispatcher and notify that startup was successful
    if (!win32_start_service_ctrl_dispatcher($SERVICE_NAME)) die('Could not connect to service :'.$SERVICE_NAME);
    win32_set_service_status(WIN32_SERVICE_RUNNING);

    // Main Server Loop
    while (1)
    {
        NTServiceResponder();

        // Main script goes here
        Service_Main();

        sleep(1); // at least 1 sec delay per loop
    }
    win32_set_service_status(WIN32_SERVICE_STOPPED);

/*
 * Response to NTServiceRequests
 */
function NTServiceResponder()
{

    switch (win32_get_last_control_message())
    {
    case 0: // PATCH for: seems never to go to 4 (WIN32_SERVICE_CONTROL_INTERROGATE)
        win32_set_service_status(WIN32_SERVICE_RUNNING);
        return TRUE;
        break;
    case WIN32_SERVICE_CONTROL_CONTINUE:
        return TRUE; // "Continue"
    case WIN32_SERVICE_CONTROL_INTERROGATE:
        win32_set_service_status(WIN32_SERVICE_RUNNING);
        return TRUE; // Respond with status
    case WIN32_SERVICE_CONTROL_STOP:
        win32_set_service_status(WIN32_SERVICE_STOPPED);
        exit; // Terminate script
    default:
        win32_set_service_status(WIN32_ERROR_CALL_NOT_IMPLEMENTED); // Add more cases to handle other service calls
    }

    return FALSE;
}

?>


Registers the script with the SCM, so that it can act as the service with the given name

  • ≪ win32_set_service_status
  • win32_start_service ≫
  • PHP Manual
  • win32service 함수 목록
  • Registers the script with the SCM, so that it can act as the service with the given name

win32_start_service_ctrl_dispatcher

(PECL win32service SVN)

win32_start_service_ctrl_dispatcher — Registers the script with the SCM, so that it can act as the service with the given name

설명

mixed win32_start_service_ctrl_dispatcher ( string $name )

When launched via the Service Control Manager, a service process is required to "check-in" with it to establish service monitoring and communication facilities. This function performs the check-in by spawning a thread to handle the lower-level communication with the service control manager.

Once started, the service process should do 2 things. The first is to tell the Service Control Manager that the service is running. This is achieved by calling win32_set_service_status() with the WIN32_SERVICE_RUNNING constant. If you need to perform some lengthy process before the service is actually running, then you can use the WIN32_SERVICE_START_PENDING constant. The second is to continue to check-in with the service control manager so that it can determine if it should terminate. This is achieved by periodically calling win32_get_last_control_message() and handling the return code appropriately.

인수

name

The short-name of the service, as registered by win32_create_service().

반환값

Returns TRUE on success, FALSE if there is a problem with the parameters or a Win32 Error Code on failure.

예제

Example #1 A win32_start_service_ctrl_dispatcher() example

Check if the service is runnig under the SCM.

<?php
if (!win32_start_service_ctrl_dispatcher('dummyphp')) {
  die("I'm probably not running under the service control manager");
}

win32_set_service_status(WIN32_SERVICE_START_PENDING);

// Some lengthy process to get this service up and running.

win32_set_service_status(WIN32_SERVICE_RUNNING);

while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
  # do some work here, trying not to take more than around 30 seconds
  # before coming back into the loop again
}
?>

참고

  • win32_set_service_status() - Update the service status
  • win32_get_last_control_message() - Returns the last control message that was sent to this service
  • Win32 Error Codes

User Contributed Notes

andrea 09-Oct-2009 12:07
Insert value of params between " if the string have a space:

<?php
// First you need to create a service, you only need to do this once
/*
win32_create_service(array(
    'service' => 'myphpservice',
    'display' => 'My PHP Service',
    'params' => '"c:\\my folder\myphpservice.php"',
    'path' => 'c:\\PHP\\php.exe'));
*/
?>
dylan at nopower dot org 14-Jul-2007 04:59
<?php
// First you need to create a service, you only need to do this once
/*
win32_create_service(array(
    'service' => 'myphpservice',
    'display' => 'My PHP Service',
    'params' => 'c:\\myphpservice.php',
    'path' => 'c:\\PHP\\php.exe'));
*/

$myservicename = 'myphpservice';

// Connect to service dispatcher and notify that startup was successful
if (!win32_start_service_ctrl_dispatcher($myservicename)) die('Could not connect to service :'.$myservicename);
win32_set_service_status(WIN32_SERVICE_RUNNING);

// Main Server Loop
while (1) {
    switch (win32_get_last_control_message()) {
        case WIN32_SERVICE_CONTROL_CONTINUE: break; // Continue server routine
        case WIN32_SERVICE_CONTROL_INTERROGATE: win32_set_service_status(WIN32_NO_ERROR); break; // Respond with status
        case WIN32_SERVICE_CONTROL_STOP: win32_set_service_status(WIN32_SERVICE_STOPPED); exit; // Terminate script
        default: win32_set_service_status(WIN32_ERROR_CALL_NOT_IMPLEMENTED); // Add more cases to handle other service calls
    }

    // Main script goes here

    sleep(10); // Run every 10 seconds
}
win32_set_service_status(WIN32_SERVICE_STOPPED);
?>
Guibod 10-Jul-2007 02:41
Don't try to call "win32_start_service_ctrl_dispatcher" too late in your code. You'd trigger a #2186 error : "The service is not responding to the control function." (from commande line) or a #1053 error : "The service did not respond to the start or control request in a timely fashion." (from services GUI).

Try not to load a bunch of PEAR classes before to register php script as Service like I did.

Another good hint, to get a verbose version of the return code, you can call "NET HELPMSG ###" from command line where ### is your error code.


Starts a service

  • ≪ win32_start_service_ctrl_dispatcher
  • win32_stop_service ≫
  • PHP Manual
  • win32service 함수 목록
  • Starts a service

win32_start_service

(PECL win32service SVN)

win32_start_service — Starts a service

설명

int win32_start_service ( string $servicename [, string $machine ] )

Attempts to start the named service. Usually requires administrative privileges.

인수

servicename

The short name of the service.

machine

Optional machine name. If omitted, the local machine is used.

반환값

Returns WIN32_NO_ERROR on success, FALSE if there is a problem with the parameters or a Win32 Error Code on failure.

참고

  • win32_stop_service() - Stops a service
  • Win32 Error Codes

User Contributed Notes

edo888 at gmail dot com 18-Mar-2010 02:23
[This] can solve 1053 issue.
You just need to add " if you have spaces in your path...

<?php
win32_create_service(array(
        'service' => 'COMPLAINTS',                 # the name of your service
        'display' => 'Receive email complaints and direct them to the website', # description
        'params' => '"' . __FILE__ . '"' . ' run', # path to the script and parameters
    ));
?>
gunday at poczta dot onet dot pl 16-Oct-2007 03:40
If you get a 1053 error when trying to launch your service, check your service script for errors!

E.g. if you have some syntax errors, the service will attempt to run the php interpreter, but the script won't answer to the service manager with
win32_start_service_ctrl_dispatcher('servicename').

Don't think it is because the call comes to late. It just never came, because there were errors. Test your services with CLI php and then try to run the service.


Stops a service

  • ≪ win32_start_service
  • XML 조작 ≫
  • PHP Manual
  • win32service 함수 목록
  • Stops a service

win32_stop_service

(PECL win32service SVN)

win32_stop_service — Stops a service

설명

int win32_stop_service ( string $servicename [, string $machine ] )

Stops a named service. Requires administrative privileges.

인수

servicename

The short name of the service.

machine

Optional machine name. If omitted, the local machine is used.

반환값

Returns WIN32_NO_ERROR on success, FALSE if there is a problem with the parameters or a Win32 Error Code on failure.

참고

  • win32_start_service() - Starts a service
  • Win32 Error Codes

User Contributed Notes

There are no user contributed notes for this page.


XML 조작

  • DOM — Document Object Model
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • DOMAttr — The DOMAttr class
    • DOMCdataSection — The DOMCdataSection class
    • DOMCharacterData — The DOMCharacterData class
    • DOMComment — The DOMComment class
    • DOMDocument — The DOMDocument class
    • DOMDocumentFragment — The DOMDocumentFragment class
    • DOMDocumentType — The DOMDocumentType class
    • DOMElement — The DOMElement class
    • DOMEntity — The DOMEntity class
    • DOMEntityReference — The DOMEntityReference class
    • DOMException — The DOMException class
    • DOMImplementation — The DOMImplementation class
    • DOMNamedNodeMap — The DOMNamedNodeMap class
    • DOMNode — The DOMNode class
    • DOMNodeList — The DOMNodeList class
    • DOMNotation — The DOMNotation class
    • DOMProcessingInstruction — The DOMProcessingInstruction class
    • DOMText — The DOMText class
    • DOMXPath — The DOMXPath class
    • DOM 함수 목록
  • libxml
    • 소개
    • 설치/설정
    • 예약 상수
    • libXMLError — The libXMLError class
    • libxml 함수 목록
  • SDO — Service Data Objects
    • 소개
    • 설치/설정
    • 예약 상수
    • Limitations
    • 예제
    • SDO 함수 목록
  • SDO-DAS-Relational — SDO Relational Data Access Service
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • Limitations
    • SDO-DAS-Relational 함수 목록
  • SDO DAS XML — SDO XML Data Access Service
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • SDO DAS XML 함수 목록
  • SimpleXML
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • SimpleXMLElement — The SimpleXMLElement class
    • SimpleXML 함수 목록
  • WDDX
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • WDDX 함수 목록
  • XMLDiff — XML diff and merge
    • 소개
    • 설치/설정
    • XMLDiff\Base — The XMLDiff\Base class
    • XMLDiff\DOM — The XMLDiff\DOM class
    • XMLDiff\Memory — The XMLDiff\Memory class
    • XMLDiff\File — The XMLDiff\File class
  • XML Parser
    • 소개
    • 설치/설정
    • 예약 상수
    • Event Handlers
    • Case Folding
    • Error Codes
    • Character Encoding
    • 예제
    • XML Parser 함수 목록
  • XMLReader
    • 소개
    • 설치/설정
    • XMLReader — The XMLReader class
  • XMLWriter
    • 소개
    • 설치/설정
    • 예약 상수
    • XMLWriter 함수 목록
  • XSL
    • 소개
    • 설치/설정
    • 예약 상수
    • 예제
    • XSLTProcessor — The XSLTProcessor class

User Contributed Notes

There are no user contributed notes for this page.


Document Object Model

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
  • DOMAttr — The DOMAttr class
    • DOMAttr::__construct — Creates a new DOMAttr object
    • DOMAttr::isId — Checks if attribute is a defined ID
  • DOMCdataSection — The DOMCdataSection class
    • DOMCdataSection::__construct — Constructs a new DOMCdataSection object
  • DOMCharacterData — The DOMCharacterData class
    • DOMCharacterData::appendData — Append the string to the end of the character data of the node
    • DOMCharacterData::deleteData — Remove a range of characters from the node
    • DOMCharacterData::insertData — Insert a string at the specified 16-bit unit offset
    • DOMCharacterData::replaceData — Replace a substring within the DOMCharacterData node
    • DOMCharacterData::substringData — Extracts a range of data from the node
  • DOMComment — The DOMComment class
    • DOMComment::__construct — Creates a new DOMComment object
  • DOMDocument — The DOMDocument class
    • DOMDocument::__construct — Creates a new DOMDocument object
    • DOMDocument::createAttribute — Create new attribute
    • DOMDocument::createAttributeNS — Create new attribute node with an associated namespace
    • DOMDocument::createCDATASection — Create new cdata node
    • DOMDocument::createComment — Create new comment node
    • DOMDocument::createDocumentFragment — Create new document fragment
    • DOMDocument::createElement — Create new element node
    • DOMDocument::createElementNS — Create new element node with an associated namespace
    • DOMDocument::createEntityReference — Create new entity reference node
    • DOMDocument::createProcessingInstruction — Creates new PI node
    • DOMDocument::createTextNode — Create new text node
    • DOMDocument::getElementById — Searches for an element with a certain id
    • DOMDocument::getElementsByTagName — Searches for all elements with given local tag name
    • DOMDocument::getElementsByTagNameNS — Searches for all elements with given tag name in specified namespace
    • DOMDocument::importNode — Import node into current document
    • DOMDocument::load — Load XML from a file
    • DOMDocument::loadHTML — Load HTML from a string
    • DOMDocument::loadHTMLFile — Load HTML from a file
    • DOMDocument::loadXML — Load XML from a string
    • DOMDocument::normalizeDocument — Normalizes the document
    • DOMDocument::registerNodeClass — Register extended class used to create base node type
    • DOMDocument::relaxNGValidate — Performs relaxNG validation on the document
    • DOMDocument::relaxNGValidateSource — Performs relaxNG validation on the document
    • DOMDocument::save — Dumps the internal XML tree back into a file
    • DOMDocument::saveHTML — Dumps the internal document into a string using HTML formatting
    • DOMDocument::saveHTMLFile — Dumps the internal document into a file using HTML formatting
    • DOMDocument::saveXML — Dumps the internal XML tree back into a string
    • DOMDocument::schemaValidate — Validates a document based on a schema
    • DOMDocument::schemaValidateSource — Validates a document based on a schema
    • DOMDocument::validate — Validates the document based on its DTD
    • DOMDocument::xinclude — Substitutes XIncludes in a DOMDocument Object
  • DOMDocumentFragment — The DOMDocumentFragment class
    • DOMDocumentFragment::appendXML — Append raw XML data
  • DOMDocumentType — The DOMDocumentType class
  • DOMElement — The DOMElement class
    • DOMElement::__construct — Creates a new DOMElement object
    • DOMElement::getAttribute — Returns value of attribute
    • DOMElement::getAttributeNode — Returns attribute node
    • DOMElement::getAttributeNodeNS — Returns attribute node
    • DOMElement::getAttributeNS — Returns value of attribute
    • DOMElement::getElementsByTagName — Gets elements by tagname
    • DOMElement::getElementsByTagNameNS — Get elements by namespaceURI and localName
    • DOMElement::hasAttribute — Checks to see if attribute exists
    • DOMElement::hasAttributeNS — Checks to see if attribute exists
    • DOMElement::removeAttribute — Removes attribute
    • DOMElement::removeAttributeNode — Removes attribute
    • DOMElement::removeAttributeNS — Removes attribute
    • DOMElement::setAttribute — Adds new attribute
    • DOMElement::setAttributeNode — Adds new attribute node to element
    • DOMElement::setAttributeNodeNS — Adds new attribute node to element
    • DOMElement::setAttributeNS — Adds new attribute
    • DOMElement::setIdAttribute — Declares the attribute specified by name to be of type ID
    • DOMElement::setIdAttributeNode — Declares the attribute specified by node to be of type ID
    • DOMElement::setIdAttributeNS — Declares the attribute specified by local name and namespace URI to be of type ID
  • DOMEntity — The DOMEntity class
  • DOMEntityReference — The DOMEntityReference class
    • DOMEntityReference::__construct — Creates a new DOMEntityReference object
  • DOMException — The DOMException class
  • DOMImplementation — The DOMImplementation class
    • DOMImplementation::__construct — Creates a new DOMImplementation object
    • DOMImplementation::createDocument — Creates a DOMDocument object of the specified type with its document element
    • DOMImplementation::createDocumentType — Creates an empty DOMDocumentType object
    • DOMImplementation::hasFeature — Test if the DOM implementation implements a specific feature
  • DOMNamedNodeMap — The DOMNamedNodeMap class
    • DOMNamedNodeMap::getNamedItem — Retrieves a node specified by name
    • DOMNamedNodeMap::getNamedItemNS — Retrieves a node specified by local name and namespace URI
    • DOMNamedNodeMap::item — Retrieves a node specified by index
  • DOMNode — The DOMNode class
    • DOMNode::appendChild — Adds new child at the end of the children
    • DOMNode::C14N — Canonicalize nodes to a string
    • DOMNode::C14NFile — Canonicalize nodes to a file
    • DOMNode::cloneNode — Clones a node
    • DOMNode::getLineNo — Get line number for a node
    • DOMNode::getNodePath — Get an XPath for a node
    • DOMNode::hasAttributes — Checks if node has attributes
    • DOMNode::hasChildNodes — Checks if node has children
    • DOMNode::insertBefore — Adds a new child before a reference node
    • DOMNode::isDefaultNamespace — Checks if the specified namespaceURI is the default namespace or not
    • DOMNode::isSameNode — Indicates if two nodes are the same node
    • DOMNode::isSupported — Checks if feature is supported for specified version
    • DOMNode::lookupNamespaceURI — Gets the namespace URI of the node based on the prefix
    • DOMNode::lookupPrefix — Gets the namespace prefix of the node based on the namespace URI
    • DOMNode::normalize — Normalizes the node
    • DOMNode::removeChild — Removes child from list of children
    • DOMNode::replaceChild — Replaces a child
  • DOMNodeList — The DOMNodeList class
    • DOMNodelist::item — Retrieves a node specified by index
  • DOMNotation — The DOMNotation class
  • DOMProcessingInstruction — The DOMProcessingInstruction class
    • DOMProcessingInstruction::__construct — Creates a new DOMProcessingInstruction object
  • DOMText — The DOMText class
    • DOMText::__construct — Creates a new DOMText object
    • DOMText::isWhitespaceInElementContent — Indicates whether this text node contains whitespace
    • DOMText::splitText — Breaks this node into two nodes at the specified offset
  • DOMXPath — The DOMXPath class
    • DOMXPath::__construct — Creates a new DOMXPath object
    • DOMXPath::evaluate — Evaluates the given XPath expression and returns a typed result if possible
    • DOMXPath::query — Evaluates the given XPath expression
    • DOMXPath::registerNamespace — Registers the namespace with the DOMXPath object
    • DOMXPath::registerPhpFunctions — Register PHP functions as XPath functions
  • DOM 함수 목록
    • dom_import_simplexml — Gets a DOMElement object from a SimpleXMLElement object

User Contributed Notes

ohcc at 163 dot com 30-Oct-2015 02:31
<?php
    // this note is about how to get a DOMNode's outerHTML and innerHTML.
    $dom = new DOMDocument('1.0','UTF-8');
    $dom->loadHTML('<html><body><div><p>p1</p><p>p2</p></div></body></html>');
    $node = $dom->getElementsByTagName('div')->item(0);
    $outerHTML = $node->ownerDocument->saveHTML($node);
    $innerHTML = '';
    foreach ($node->childNodes as $childNode){
        $innerHTML .= $childNode->ownerDocument->saveHTML($childNode);
    }
    echo '<h2>outerHTML: </h2>';
    echo htmlspecialchars($outerHTML);
    echo '<h2>innerHTML: </h2>';
    echo htmlspecialchars($innerHTML);
?>
super dot puma at gmail dot com 13-May-2014 01:57
If  you want to print the DOM XML file content, you can use the next code:

$doc = new DOMDocument();
$doc->load($xmlFileName);
echo "<br>" . $doc->documentURI;
$x = $doc->documentElement;
getNodeContent($x->childNodes, 0);

function getNodeContent($nodes, $level){
    foreach ($nodes AS $item) {
    //  print "<br><br>TIPO: " . $item->nodeType ;
      printValues($item, $level);
      if ($item->nodeType == 1) { //DOMElement
          foreach ($item->attributes AS $itemAtt) {
              printValues($itemAtt, $level+3);
          }
          if($item->childNodes || $item->childNodes->lenth > 0) {
              getNodeContent($item->childNodes, $level+5);
          }
      }
    }
}

function printValues($item, $level){
    if ($item->nodeType == 1) { //DOMElement
        printLevel($level);
        print $item->nodeName . " = " . $item->nodeValue;
    }
    if ($item->nodeType == 2) { //DOMAttr
        printLevel($level);
        print $item->name . " = " . $item->value ;
    }
    if ($item->nodeType == 3) { //DOMText
      if ($item->isWhitespaceInElementContent() == false){
        printLevel($level);
        print $item->wholeText ;
      }
    }
}

function printLevel($level)
{
    print "<br>";
    if ($level == 0) {
        print "<br>";
    }
    for($i=0; $i < $level; $i++) {
        print "-";
    }
}
Drupella 17-Sep-2011 06:18
Here is a fast innerHTML function that returns the result without iterating over child nodes.

<?php
function innerHTML($el) {
  $doc = new DOMDocument();
  $doc->appendChild($doc->importNode($el, TRUE));
  $html = trim($doc->saveHTML());
  $tag = $el->nodeName;
  return preg_replace('@^<' . $tag . '[^>]*>|</' . $tag . '>$@', '', $html);
}
?>

Example
<?php
$doc = new DOMDocument();
// A corrupt HTML string
$doc->loadHTML('<HTML><A HREF="ss">asd</A>');
$body = $doc->getElementsByTagName('body')->item(0);
print htmlspecialchars(innerHTML($body));
// Prints <a href="ss">asd</a>
?>
ben_demott at hotmail dot com 23-Apr-2010 10:36
A function among several others to parse a google results page, I wrote this some time ago - google has probably changed their site since then, but I thought this might be helpful to someone.

I'm moving servers, but I will probably throw this up on my blog when I get it back up.

<?php

function googleResult($listItem) {
    // given a LIST ITEM element, this will validate, and return an array for that LI entry as an inline result from google.
    /*
     * <li class='g w0'>
     *   <h3 class='r'>
     *     <a href='the URL' class='l'>
     *       Description <em>description</em>
     *     </a>
     *   </h3>
     * </li>
     *
       UPDATE:
       This function will now look for any subcontainer that has an href, it doesn't have to be an H3
       this will make it work with a few more formatted search results.
     */

    $listItem = $listItem->childNodes;
        // Yes I don't use instanceof - I guess you'll have to deal.
    foreach($listItem as $element) {
        if(is_object($element) && get_class($element) == 'DOMElement' && $element->hasChildNodes()) {
            $hrefContainer = $element->childNodes;
            foreach($hrefContainer as $element2) {
                if(is_object($element2) && get_class($element2) == 'DOMElement' && $element2->nodeName == 'a' && $element2->hasAttribute('href')) {
                    $anchor = $element2;
                    unset($h3);
                    unset($element2);
                    break;
                } else {
                  //print __LINE__ ." :: Breaking out of loop (normal result) element is not an annchor Element='".$element2->nodeName."'\n";
                }
            }
            unset($element);
            unset($listItem);
            break;
        }
    }
    if(empty($anchor) || !is_object($anchor) || get_class($anchor) != 'DOMElement') {
        //print __LINE__ ." :: Returning false, did not locate anchor through iteration...";
        return false;
    }
    $href = $anchor->getAttribute('href');
    if(empty($href)) {
        //print __LINE__ ." :: Found anchor object, could not read href attribute / href is empty? href='$href'\n";
        return false;
    }
    $description = $anchor->childNodes;
    $urlDescription = '';
    foreach($description as $words) {
        $name =  trim($words->nodeName);
        if($name == 'em' || $name == '#text' || $name == 'b') {
            if(!empty($words->nodeValue)) {
                $text = trim($words->nodeValue);
                $urlDescription = $urlDescription . $text . ' ';
            }
        }
    }
    $urlDescription = htmlspecialchars_decode($urlDescription, ENT_QUOTES);
    $urlDescription = trim($urlDescription);
    return array('description' => $urlDescription, 'href' => $href);
}
sweisman at pobox dot com 24-Sep-2009 10:19
I had problems with the dom2array_full function by "nospam at ya dot ru". Here's my function, which works correctly for my project, and might work for yours:

<?php
function dom_to_array($root)
{
    $result = array();

    if ($root->hasAttributes())
    {
        $attrs = $root->attributes;

        foreach ($attrs as $i => $attr)
            $result[$attr->name] = $attr->value;
    }

    $children = $root->childNodes;

    if ($children->length == 1)
    {
        $child = $children->item(0);

        if ($child->nodeType == XML_TEXT_NODE)
        {
            $result['_value'] = $child->nodeValue;

            if (count($result) == 1)
                return $result['_value'];
            else
                return $result;
        }
    }

    $group = array();

    for($i = 0; $i < $children->length; $i++)
    {
        $child = $children->item($i);

        if (!isset($result[$child->nodeName]))
            $result[$child->nodeName] = dom_to_array($child);
        else
        {
            if (!isset($group[$child->nodeName]))
            {
                $tmp = $result[$child->nodeName];
                $result[$child->nodeName] = array($tmp);
                $group[$child->nodeName] = 1;
            }

            $result[$child->nodeName][] = dom_to_array($child);
        }
    }

    return $result;
}
?>
odessa131 at aol dot nospam dot com 20-Sep-2009 03:49
I had the hardest time updating a complex XML document. Here's a quick example on how to do it.

<?php

// Load the XML from a file.
$xml = "a2062.xml"; // This is an XFDL form previously unencoded and ungzipped.
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->Load($xml);

// Create an XPath query.
// Note: you must define the namespace if the XML document has defined namespaces.
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('xfdl', "http://www.PureEdge.com/XFDL/6.5");

// Locate the value for the first Item Description field.
$query = "//xfdl:page/xfdl:field[@sid='ITEMDESA']/xfdl:value";
$nodeList = $xpath->query($query);

$nodeList->item(0)->nodeValue = "This is the text in the value node of the first Item Description field inside the DA 2062 PureEdge form.";

$dom->save($xml);

?>

I hope this helps someone.
philipwaynerollins at gmail dot com 29-Jul-2009 11:06
You can get the "innerHTML" by nodeValue so

<?php
    $doc = new DOMDocument( );
    $ele = $doc->createElement( 'p', 'Sensei Ninja' );
    print $ele->nodeValue;
?>

You can even set it if you want

<?php
    $doc = new DOMDocument( );
    $ele = $doc->createElement( 'p' );
    $ele->nodeValue = 'Sensei Ninja';
    $doc->appendChild( $ele );
    print $doc->saveHTML( );
?>
Junior 20-Mar-2009 10:26
innerHTML in PHP DOM

<?php
function DOMinnerHTML($element)
{
    $innerHTML = "";
    $children = $element->childNodes;
    foreach ($children as $child)
    {
        $tmp_dom = new DOMDocument();
        $tmp_dom->appendChild($tmp_dom->importNode($child, true));
        $innerHTML.=trim($tmp_dom->saveHTML());
    }
    return $innerHTML;
}
?>

Example:

<?php
$dom= new DOMDocument();
$dom->load($html_string);
$dom->preserveWhiteSpace = false;

$domTable = $dom->getElementsByTagName("table");

foreach ($domTable as $tables)
{
    echo DOMinnerHTML($tables);
}
?>
nospam at ya dot ru 06-Jan-2009 07:18
<?PHP
function dom2array_full($node){
    $result = array();
    if($node->nodeType == XML_TEXT_NODE) {
        $result = $node->nodeValue;
    }
    else {
        if($node->hasAttributes()) {
            $attributes = $node->attributes;
            if(!is_null($attributes))
                foreach ($attributes as $index=>$attr)
                    $result[$attr->name] = $attr->value;
        }
        if($node->hasChildNodes()){
            $children = $node->childNodes;
            for($i=0;$i<$children->length;$i++) {
                $child = $children->item($i);
                if($child->nodeName != '#text')
                if(!isset($result[$child->nodeName]))
                    $result[$child->nodeName] = dom2array($child);
                else {
                    $aux = $result[$child->nodeName];
                    $result[$child->nodeName] = array( $aux );
                    $result[$child->nodeName][] = dom2array($child);
                }
            }
        }
    }
    return $result;
}
?>
fantasyman3000 at gmail dot com 06-Dec-2008 02:37
In response to "simlee at indiana dot edu",
- First of all thanks for sharing your funciton.
- It didn't work for me so i rewrite it from scratch using different method.

Here is the new version, hope it helps someone :

<?php
/**
* result sample : /html[1]/body[1]/span[1]/fieldset[1]/div[1]
* @return string
*/
function getNodeXPath( $node ) {
    $result='';
    while ($parentNode = $node->parentNode) {
        $nodeIndex=-1;
        $nodeTagIndex=0;
        do {
            $nodeIndex++;
            $testNode = $parentNode->childNodes->item( $nodeIndex );

            if ($testNode->nodeName==$node->nodeName and $testNode->parentNode->isSameNode($node->parentNode) and $testNode->childNodes->length>0) {
                //echo "{$testNode->parentNode->nodeName}-{$testNode->nodeName}-{}<br/>";
                $nodeTagIndex++;
            }

        } while (!$node->isSameNode($testNode));

        $result="/{$node->nodeName}[{$nodeTagIndex}]".$result;
        $node=$parentNode;
    };
    return $result;
}
?>

By Sina.Salek.ws
cgorbit spamless_at ya dot ru 18-Oct-2008 07:39
Show expandable DOM like in Firebug.

http://cgorbit.itkm.ru/temp/phpdotnet/common.js needed for script, sorry, but it's my framework.

<?php
    function gaie( $node, &$arr, $attr )
    {
        if ( $value = $node->getAttribute( $attr ) )
            $arr[ $attr ] = u2w( $value );
    }

    function innerXML( $l, $collapse_all = true, $top_run = true )
    {
        $atts4elements = array(
            'a' => 'href',
            'table' => 'border,cellpadding,cellspacing',
            'td' => 'colspan,rowspan'
        );
        $s = '';

        if ( $top_run ) :
            ob_start();
            ?><html>
            <head>
                <meta http-uquiv="Content-type" content="text/html; charset=windows-1251" />
                <script type="text/javascript" src="/common.js"></script>
            </head>
            <body>
            <style type="text/css">
                body {
                }

                .root-inner-xml-list {
                    list-style-type: none;

                    font-size: 1em;
                    font-family: monospace;
                }

                .root-inner-xml-list ul {
                    list-style-type: none;
                }

                .root-inner-xml-list li.text-node span.value {
                    color: black;
                    font-weight: bold;
                    background-color: lightgrey;

                    margin-right: 1em;
                    padding: 0em 0.2em;
                }

                .root-inner-xml-list li.text-node span.hex {
                    color: gray;
                    font-style: italic;
                }

                .root-inner-xml-list li.element-node {
                    color: blue;
                }

                .root-inner-xml-list .attribute-name {
                    color: green;
                }

                .root-inner-xml-list .attribute-value {
                    color: red;
                    color: rgb( 205, 0, 0 );
                }

                .root-inner-xml-list .node-with-childs {
                    cursor: pointer;
                    //cursor: hand;
                }

                .root-inner-xml-list .collapsed-node {
                    background-color: yellow;
                }

                .root-inner-xml-list .collapsed-node + ul {
                    display: none;
                }
            </style>

            <ul class="root-inner-xml-list">
            <?
            $s .= ob_get_clean();
        endif;

        //if ( $top_run ) $l = $l->childNodes;

        foreach ( $l as $i => $node )
        {
            if ( $node->nodeType == XML_ELEMENT_NODE )
            {
                $atts = array();
                $atts2get = array( 'id', 'class', 'style' );

                if ( @$atts4elements[ $node->tagName ] )
                    $atts2get = array_merge( $atts2get, explode( ',', $atts4elements[ $node->tagName ] ) );

                foreach ( $atts2get as $att2get )
                    gaie( $node, $atts, $att2get );

                $attr_arr = array();
                foreach ( $atts as $name => $value )
                    $attr_arr[] = sprintf( '<span class="attribute-name">%s</span>="<span class="attribute-value">%s</span>"', $name, $value );

                $atts_s = '';
                if ( $attr_arr )
                    $atts_s = join( ' ', $attr_arr );

                $span_class_name = array( 'node-name' );
                if ( $node->childNodes->length )
                    $span_class_name[] = 'node-with-childs';
                if ( $collapse_all )
                    $span_class_name[] = 'collapsed-node';
                $span_class_name = join( ' ', $span_class_name );

                $inner = '<span class="' . $span_class_name . '">&lt;'. $node->tagName . ' ' . $atts_s . '&gt;</span>';

                if ( $node->childNodes->length )
                    $inner .= '<ul>' . innerXML( $node->childNodes, $collapse_all, false ) . '</ul>';

                $className = 'element-node';
            }
            elseif ( $node->nodeType == XML_TEXT_NODE )
            {
                $value = htmlspecialchars( u2w( $node->nodeValue ) );
                $inner = '<span class="value">' . $value . '</span><span class="hex">' . string2hex( $value ) . '</span>';

                $className = 'text-node';
            }
            else
                continue;

            $s .= '<li class="' . $className . '">' . $inner . '</li>';

            if ( $top_run ) echo '<hr />';
        }

        if ( $top_run ):
        ob_start();
        ?></ul>
            <script type="text/javascript">
                onLoad( function(){
                    addEvent( $( '.root-inner-xml-list .node-with-childs', true ), 'click', function( e ){
                        var el = e.target;

                        ul = getNextSibling( el );

                        if ( !ul || ul.tagName != 'UL' ) return;

                        invertClass( el, 'collapsed-node' );
                    });
                });

                globalInit();
            </script>
        </body>
        </html>
        <?
        $s .= ob_get_clean();
        endif;

        return $s;
    }
?>
miguelangelhdz at NOSPAM dot com 22-Sep-2008 03:19
After searching how to extend the DOMDocument and DOMElement I found a way in the bug: http://bugs.php.net/bug.php?id=35104. The following code shows how:

<?php
class extDOMDocument extends DOMDocument {
 public function createElement($name, $value=null) {
  $orphan = new extDOMElement($name, $value); // new  sub-class object
  $docFragment = $this->createDocumentFragment(); // lightweight container maintains "ownerDocument"
  $docFragment->appendChild($orphan); // attach
  $ret = $docFragment->removeChild($orphan); // remove
  return $ret; // ownerDocument set; won't be destroyed on  method exit
 }
 // .. more class definition
}

class extDOMElement extends DOMElement {
 function __construct($name, $value='', $namespaceURI=null) {
  parent::__construct($name, $value, $namespaceURI);
 }
  //  ... more class definition here
}

$doc = new extDOMDocument('test');
$el = $doc->createElement('tagname');
$el->setAttribute("attr", "val");
$doc->appendChild($el);

// append discards the DOMDocumentFragment and just adds its child nodes, but ownerDocument is maintained.
echo get_class($el)."<br/>";
echo get_class($doc->documentElement)."<br/>";
echo "<xmp>".$doc->saveXML()."</xmp>";
?>
tobiasz.cudnik[at]gmail.com 29-Jul-2008 03:47
If you need simple interface to DOM check out phpQuery - jQuery port to PHP:
http://code.google.com/p/phpquery/

It uses CSS selectors to fetch nodes.
Here's example how it works:
<?php
// just one file to include
require('phpQuery/phpQuery.php');

$html = '
<div>
    mydiv
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</div>';

// intialize new DOM from markup
phpQuery::newDocument($markup)
    ->find('ul > li')
        ->addClass('my-new-class')
        ->filter(':last')
            ->addClass('last-li');

// query all unordered lists in last used DOM
pq('ul')->insertAfter('div');

// iterate all LIs from last used DOM
foreach(pq('li') as $li) {
    // iteration returns plain DOM nodes, not phpQuery objects
    pq($li)->addClass('my-second-new-class');
}

// same as pq('anything')->htmlOuter()
// but on document root (returns doctype etc)
print phpQuery::getDocument();
?>

It uses DOM extension and XPath so it works only in PHP5.
Sven Arduwie 17-Mar-2008 03:28
I needed a function that quickly converts an xml with key/value pairs to an array.

for example:
<?xml version="1.0" encoding="UTF-8"?>
<test>
    <key id="Array key #1" value="Value #1"/>
    <key id="Array key #2" value="Value #2"/>
    <key id="Dupe" value="Duplicate keys"/>
    <key id="Dupe" value="create numeric arrays"/>
    <key id="And another key">
        <![CDATA[
            Multiline
            data
            works
            just
            as
            well.
        ]]>
    </key>
    <nested>
        <key id="Nested key" value="Nested data works as well, but it still results in a 1 dimensional array."/>
    </nested>
</test>

Results in the following array:
array(5) {
  ["Array key #1"]=>
  string(8) "Value #1"
  ["Array key #2"]=>
  string(8) "Value #2"
  ["Dupe"]=>
  array(2) {
    [0]=>
    string(14) "Duplicate keys"
    [1]=>
    string(21) "create numeric arrays"
  }
  ["And another key"]=>
  string(49) "Multiline
            data
            works
            just
            as
            well."
  ["Nested key"]=>
  string(73) "Nested data works as well, but it still results in a 1 dimensional array."
}

Here's the code:
<?php
    function xml2array($xml) {
        $domDocument = new DOMDocument;
        $domDocument->loadXML($xml);
        $domXPath = new DOMXPath($domDocument);
        $array = array();
        foreach ($domXPath->query('//key') as $keyDOM) {
            $id = $keyDOM->getAttribute('id');
            $value = $keyDOM->hasAttribute('value') ? $keyDOM->getAttribute('value') : trim($keyDOM->textContent);
            if (array_key_exists($id, $array)) {
                if (is_array($array[$id])) {
                    $array[$id][] = $value;
                } else {
                    $array[$id] = array($array[$id]);
                    $array[$id][] = $value;
                }
            } else {
                $array[$id] = $value;
            }
        }
        return $array;
    }
?>
danf dot 1979 at []gmail[] dot com 24-Feb-2008 04:47
This is a couple of classes to deal with yahoo yui menu.

/*
  $menubar = new MenuBar();

  $file = new Menu("File");
  $file->setAttribute("href", "http://file.com");

  $quit = new Menu("Quit");
  $quit->setAttribute("href", "http://quit.com");

  $file->appendChild($quit);
  $menubar->appendChild($file);

  echo $menubar->grab();
*/

//
// Author: Daniel Queirolo.
// LGPL
//

/** ---------------------------------
/**  Class MenuBar()
/**  Creates a the menubar and appends
/**  yuimenubaritems to it.
/** ---------------------------------*/

class MenuBar extends DOMDocument
{

  public $menuID = "nav_menu";  // holds the css id that javascript yui menu code should have to recognize
  private $UL;                  // This node holds every menu, This is THE node.

  /** ---------------------------------
  /**  Constructor
  /**  Generates a menubar skeleton and the UL node
  /** ---------------------------------*/

  public function __construct() {

    parent::__construct();

    $rootdiv = parent::createElement("div");
    $rootdiv->setAttribute("class", "yui-skin-sam");

    parent::appendChild($rootdiv);

    $yui_menubar = parent::createElement("div");
    $yui_menubar->setAttribute("id", $this->menuID);
    $yui_menubar->setAttribute("class", "yuimenubar");

    $rootdiv->appendChild($yui_menubar);

    $bd = parent::createElement("div");
    $bd->setAttribute("class", "bd");

    $yui_menubar->appendChild($bd);

    $ul = parent::createElement("ul");
    $ul->setAttribute("class", "first-of-type");

    // ALL Menu() instances ocurr inside an <ul> tag.

    $this->UL = $bd->appendChild($ul);

  }

  /** ---------------------------------
  /**  appendChild()
  /**  Appends a new yuimenubaritem to the menubar UL node.
  /**  This function changes <li> and <a> classes to yuiMENUBARsomething
  /** ---------------------------------*/

  public function appendChild($child) {

    $li = parent::importNode($child->LI, true);

    $li->setAttribute("class", "yuimenubaritem");

    $li->getElementsByTagName("a")->item(0)->setAttribute("class", "yuimenubaritemlabel");

    $this->UL->appendChild($li);

  }

  public function grab() {

    return parent::saveHTML();

  }

}

/** ---------------------------------
/**  Class Menu()
/**  Creates a yuimenuitem li node
/** ---------------------------------*/

class Menu extends DOMDocument {

  public $LI; // stores the <li> node (THE link) that will be exported to MenuBar() or used on appendChild()

  /** ---------------------------------
  /**  Constructor
  /**  Generates a yuimenuitem li node
  /**  No yuimenubar items are created here. MenuBar handles that.
  /** ---------------------------------*/

  public function __construct($link_name) {

    parent::__construct();

    $li = parent::createElement("li");
    $li->setAttribute("class", "yuimenuitem");

    // LI node stores THE link.
    // if appendChild is used, the new (sub) Menu() would be LI node child.

    $this->LI = parent::appendChild($li);

    $a = parent::createElement("a", $link_name);
    $a->setAttribute("class", "yuimenuitemlabel");

    $li->appendChild($a);

    $this->li = $li;
    $this->a = $a;

  }

  /** ---------------------------------
  /**  appendChild
  /**  Appends a (sub) Menu() to current Menu() in LI
  /** ---------------------------------*/

  public function appendChild($child) {

    $yuimenu = parent::createElement("div");
    $yuimenu->setAttribute("class", "yuimenu");

    $this->LI->appendChild($yuimenu);

    $bd = parent::createElement("div");
    $bd->setAttribute("class", "bd");

    $yuimenu->appendChild($bd);

    $ul = parent::createElement("ul");

    $bd->appendChild($ul);

    // child->NODE holds THE link from the new child (from child's __construct())

    $ul->appendChild(parent::importNode($child->LI, true));

  }

  public function setAttribute($name, $value, $node="a") {

      if ($node == "a") {
        $this->a->setAttribute($name, $value);
      }

      else {
        $this->li->setAttribute($name, $value);
      }
  }

}
ptichy at pobox dot sk 09-Feb-2008 11:40
How to get contents of node with all tags by PHP Dom:
<?php
// XML data
$xml_string = "<?xml version='1.0' ?>
<sentence>
How to get contents of node with all tags by PHP Dom: <br/>
<b>cabbages</b>, <i>tomatoes</i>,
  <i>apples</i>, <font  color='purple'>aubergines</font>
<ol>
   <li> row 1</li>
   <li> row 2 </li>
   <li> row 3 </li>
 </ol>
 <table border = '1'>
  <tbody>
    <tr>
      <td>I am best</td>
    </tr>
    <tr>
      <td>Programmer</td>
    </tr>
  </tbody>
</table>
You see my projects at:
<strong><a href='http://www.itdeveloping.eu' >www.itdeveloping.eu</a></strong>
</sentence>";

// parse it
$doc= new DOMDocument();
if (!$doc->loadXML($xml_string))
{
   echo ("Error in XML document");
}

$nody = $doc->getElementsByTagName('sentence'); // gets NodeList

$nod=$nody->item(0);//Node
getContent($Content,$nod);
echo $Content;

function getContent(&$NodeContent="",$nod)
{    $NodList=$nod->childNodes;
    for( $j=0 ;  $j < $NodList->length; $j++ )
    {       $nod2=$NodList->item($j);//Node j
        $nodemane=$nod2->nodeName;
        $nodevalue=$nod2->nodeValue;
        if($nod2->nodeType == XML_TEXT_NODE)
            $NodeContent .=  $nodevalue;
        else
        {     $NodeContent .= "<$nodemane ";
           $attAre=$nod2->attributes;
           foreach ($attAre as $value)
              $NodeContent .="{$value->nodeName}='{$value->nodeValue}'" ;
            $NodeContent .=">";
            getContent($NodeContent,$nod2);
            $NodeContent .= "</$nodemane>";
        }
    }

}
?>
Anonymous 30-Jan-2008 08:37
In response to...
"If you create your own custom element extending DOMElement and append him in place of the document element, you cannot access to any new members newly defined in your custom class via DOMDocument::$documentElement."

... it is not a bug, it is a feature. The DOMDocument::$documentElement property name may be misleading but according to the DOM Level 2 Core specification it is a convenience attribute meant to access the root element of your DOMDocument. See here: http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document
ryoroxdahouse at hotmail dot com 25-Jan-2008 09:14
I use DOM to generate dynamically XHTML document.

When trying to extend the DOMDocument and DOMElement classes, I found a very annoying bug concerning DOMDocument::$documentElement.

If you create your own custom element extending DOMElement and append him in place of the document element, you cannot access to any new members newly defined in your custom class via DOMDocument::$documentElement.

In my situation, I cannot use DOMDocument::registerNodeClass() because the document element is not necessarily the base class for all the elements in my document.

*******
problem
*******

See bellow for the repro step:

<?php
    class MyElement extends DOMElement{
        public $myProp="myProp";
        public function myMethod(){
            return 'myMethod()';
        }
    }

    $myDocument=new DOMDocument();
    $myDocument->appendChild(new MyElement('myElement','myElement'));
    echo ('$myElement->myProp :'.$myDocument->documentElement->myProp.'<br />');
    echo ('$myElement->myMethod :'.$myDocument->documentElement->myMethod().'<br />');
?>

will output:

Notice: Undefined property: DOMElement::$myProp in C:\Program Files\EasyPHP 2.0b1\www\testDOMBug\test2.php on line 11
$myElement->myProp :

Fatal error: Call to undefined method DOMElement::myMethod() in C:\Program Files\EasyPHP 2.0b1\www\testDOMBug\test2.php on line 12

*******
solution
*******

After searching around, I found a pretty odd way to fix this problem. It seems that you have to stock a reference to your appended document element in an user-defined (and persistent) variable (in other words, not only in DOMDocument::$documentElement). See below:

<?php
    class MyElement extends DOMElement{
        public $myProp="myProp";
        public function myMethod(){
            return 'myMethod()';
        }
    }

    $myDocument=new DOMDocument();
    $mydocumentElement=$myDocument->appendChild(new MyElement('myElement','myElement')); //here is the hack
    echo ('$myElement->myProp :'.$myDocument->documentElement->myProp.'<br />');
    echo ('$myElement->myMethod :'.$myDocument->documentElement->myMethod().'<br />');
?>

will output:

$myElement->myProp :myProp
$myElement->myMethod :myMethod()

Hope it will help.
moshedolev at gmail dot com 08-Jan-2008 07:15
I developed a group of functions that make it very easy to extract any information you want from any page you load from the internet. All based on the DOMDocument object.
You can read the entire documentation and download the source code here:
http://www.tintetoner-shop.de/DomUtilities/

Enjoy !!
naudyj at aus3d.com 04-Jan-2008 02:06
The following can take a XML_TEXT_NODE node and return the contents in an array. Yanick's contribution rocks - but
it overwrote with duplicates only keeping the last line
in the returned array. All the other functions i tested from various sources failed to handle text nodes correctly. Hope this helps someone. It is adapted from code on this site.

function myTextNode($n, &$a)
{
 static $depth = 0;
 static $sz = '';

 if ($cn = $n->firstChild)
 {
  while ($cn)
  {
   if ($cn->nodeType == XML_TEXT_NODE)
   {
    $sz .= $cn->nodeValue;
   }
   elseif ($cn->nodeType == XML_ELEMENT_NODE)
   {
    $b = 1;
    if ($cn->hasChildNodes())
    {
     $depth++;
     if ($this->myHeadings($cn, $a))
     {
      if ($sz){
       array_push($a, $sz);
       $sz = '';
      }
     }
     $depth--;
    }
   }
   $cn = $cn->nextSibling;
  }
  return $b;
 }
}

so you could use:

$nodes = $dom->getElementsByTagName("td");
if($nodes){
 foreach ($nodes as $node){
  $a = Array();
  myTextNode($node, $a);
 }
}
trollboy at shoggoth dot net 13-Nov-2007 12:12
I wrote a simple class to turn an associative array into xml, as this seems like something fairly simple, but nothing out there does it that I know of.  That said,  enjoy.

<?php

    /**
     * basic class for converting an array to xml.
     * @author Matt Wiseman (trollboy at shoggoth.net)
     *
     */
    class array2xml {

        public $data;
        public $dom_tree;

        /**
         * basic constructor
         *
         * @param array $array
         */
        public  function __construct($array){
            if(!is_array($array)){
                throw new Exception('array2xml requires an array', 1);
                unset($this);
            }
            if(!count($array)){
                throw new Exception('array is empty', 2);
                unset($this);
            }

            $this->data = new DOMDocument('1.0');

            $this->dom_tree = $this->data->createElement('result');
            $this->data->appendChild($this->dom_tree);
            $this->recurse_node($array, $this->dom_tree);
        }

        /**
         * recurse a nested array and return dom back
         *
         * @param array $data
         * @param dom element $obj
         */
        private function recurse_node($data, $obj){
            $i = 0;
            foreach($data as $key=>$value){
                if(is_array($value)){
                    //recurse if neccisary
                    $sub_obj[$i] = $this->data->createElement($key);
                    $obj->appendChild($sub_obj[$i]);
                    $this->recurse_node($value, $sub_obj[$i]);
                } elseif(is_object($value)) {
                    //no object support so just say what it is
                    $sub_obj[$i] = $this->data->createElement($key, 'Object: "' . $key . '" type: "'  . get_class($value) . '"');
                    $obj->appendChild($sub_obj[$i]);
                } else {
                    //straight up data, no weirdness
                    $sub_obj[$i] = $this->data->createElement($key, $value);
                    $obj->appendChild($sub_obj[$i]);
                }
                $i++;
            }
        }

        /**
         * get the finished xml
         *
         * @return string
         */
        public function saveXML(){
            return $this->data->saveXML();
        }
    }

    $test = array(
        'cat'=>'animal',
        'container'=> array(
            'thing'=>'stuff',
            'thing2'=>'stuff2',
            'thing3'=>3,
            'thing4'=>4,
            'wtf'=> new stdClass(),
        ),
        'foo'=>'bar',
    );

    var_dump($test);
    try {
    $o_test = new array2xml($test);
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    echo $o_test->saveXML();
    ?>
jim.filter (at gmail.com) 03-Nov-2007 04:42
Array to DOM

Here is a recursive function to turn a multidimensional array into an XML document.  It will handle multiple elements of the same tag, but only one per parent element. IE:

Can't generate:         Can generate:
<root>                  <root>
  <sub1>data1</sub1>      <subs1>
  <sub1>data2</sub1>         <value>data1</value>
  <sub2>data1</sub2>         <value>data2</value>
  <sub2>data2</sub2>      </subs1>
</root>                   <subs2>
                            <value>data1</value>
                            <value>data2</value>
                          <subs2>
                        </root>

Also, the function performs no type of error checking on your array and will throw a DOMException if a key value you used in your array contains invalid characters for a proper DOM tag. This function is untested for "deep" multidimensional arrays.

Complete code ready to run with example:

<?PHP
  function AtoX($array, $DOM=null, $root=null){

    if($DOM  == null){$DOM  = new DOMDocument('1.0', 'iso-8859-1');}
    if($root == null){$root = $DOM->appendChild($DOM->createElement('root'));}

    $name = $array['#MULTIPLE_ELEMENT_NAME'];

      foreach($array as $key => $value){
        if(is_int($key) && $name != null){
          if(is_array($value)){
            $subroot = $root->appendChild($DOM->createElement($name));
            AtoX($value, $DOM, $subroot);
          }
          else if(is_scalar($value)){
            $root->appendChild($DOM->createElement($name, $value));
          }
        }
        else if(is_string($key) && $key != '#MULTIPLE_ELEMENT_NAME'){
          if(is_array($value)){
            $subroot = $root->appendChild($DOM->createElement($key));
            AtoX($value, $DOM, $subroot);
          }
          else if(is_scalar($value)){
            $root->appendChild($DOM->createElement($key, $value));
          }
        }
      }
    return $DOM;
  }

  $array = array(
                    '#MULTIPLE_ELEMENT_NAME' => 'GenericDatas',
                    'Date'      => 'November 03, 2007',
                    'Company'   => 'Facility One',
                    'Field'     => 'Facility Management Software',
                    'Employees' => array(
                                      '#MULTIPLE_ELEMENT_NAME' => 'Employee',
                                      'Cindy',
                                      'Sean',
                                      'Joe',
                                      'Owen',
                                      'Jim',
                                      'Dale',
                                      'Kelly',
                                      'Ryan',
                                      'Johnathan',
                                      'Robin',
                                      'William Marcus',
                                      'NewCoops' => array(
                                                          '#MULTIPLE_ELEMENT_NAME' => 'Coop',
                                                          'John',
                                                          'Tyler',
                                                          'Ray',
                                                          'Dawn'
                                                         )
                                    ),
                    'Datas',
                    'DATAS',
                    'OtherDatas'
                );

  $DOM  = new DOMDocument('1.0', 'iso-8859-1');
  $root = $DOM->appendChild($DOM->createElement('CompanyData'));
  $DOM  = AtoX($array, $DOM, $root);
  $DOM->save('C:\test.xml');
?>
freyjkell at gmail dot com 02-Nov-2007 07:02
In order to REALLY well handle XHTML entities with DOM, you can do following things:

1. Add this DOCTYPE to your documents
<!DOCTYPE xhtmlentities PUBLIC "-//W3C//ENTITIES XHTML Character Entities 1.0//EN" "/xhtml11.ent">

2. Copy http://freyjkell.ovh.org/xhtml11.ent into your document root.

3. In your PHP:
<?php
$dom=new DOMDocument();
$dom->load('file.xhtml',LIBXML_DTDLOAD); // NOT resolveExternals - it needs true doctype, and includes crap code
// some DOM operations
$doctype=DOMImplementation::createDocumentType("html","-//W3C//DTD XHTML 1.1//EN","http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"); // creating real doctype
$output=DOMImplementation::createDocument('','',$doctype);
$output->appendChild($output->importNode($dom->documentElement,true));
$output->encoding='utf-8';
$output=$output->saveXML();
$xhtml=preg_match(
    '/application\/xhtml\+xml(?![+a-z])'.
    '(;q=(0\.\d{1,3}|[01]))?/i',
    $_SERVER['HTTP_ACCEPT'],$xhtml) &&
    (isset($xhtml[2])?$xhtml[2]:1) > 0 ||
    strpos($_SERVER["HTTP_USER_AGENT"],
    "W3C_Validator")!==false ||
    strpos($_SERVER["HTTP_USER_AGENT"],
    "WebKit")!==false; // XHTML Content-Negotiation
header('Content-Type: '.($xhtml?'application/xhtml+xml':'text/html').'; CHARSET=ks_c_5601-1987');
print $output;
?>
PHPdeveloper 06-Sep-2007 11:46
The Yanik's dom2array() function (added on 14-Mar-2007 08:40) does not handle multiple nodes with the same name, i.e.:

<foo>
  <name>aa</name>
  <name>bb</name>
</foo>

It will overwrite former and your array will contain just the last one ("bb")
Francois Hill 01-Aug-2007 08:21
In response to lutfi at smartconsultant dot us :

(see my post on
http://fr2.php.net/manual/en/
function.dom-domdocument-getelementsbytagname.php
)

Use this class I wrote:

class XPathableNode extends DOMNode
{
    protected $Node;
    protected $DOMDocument_from_node;
    protected $DOMXpath_for_node;

    public function __construct(/* DOMNode */ $node)
    {
        $this->Node=$node;
        $this->DOMDocument_from_node=new
                                                          DomDocument();
        $domNode=$this->DOMDocument_from_node
                                  ->importNode($this->Node, true);
        $this->DOMDocument_from_node
                                 ->appendChild($domNode);
        $this->DomXpath_for_node =
                    new Domxpath($this->
                                         DOMDocument_from_node);
    }

    public function convertHTML()
    {    return $this->DOMDocument_from_node
                                                             ->saveHTML();
    }

    public /*DomNodeList*/ function applyXpath($xpath)
    {    return $this->DomXpath_for_node
                                                       ->query($xpath);
    }
}

(sorry for the display... What a terrible hinderance on the
part of php.net !)

Then :
Make a new XPathableNode out of your parent node.
You may then retrieve a DOMNodeList from it by applying a
xpath (thus being able to specify the depth  and name of
elements you want).

Has got me around some (of the many) DOM awkwardnesses a few times.

;o)
lutfi at smartconsultant dot us 20-Jul-2007 06:06
i have some problem parsing recurred xml tree here:

<menu name='parent1' >
  <submenu name='file' >
     <submenu name='open' >Open file</submenu>
     <submenu name='close' >Close file</submenu>
  </submenu>
  <submenu name='edit' >
     <submenu name='cut' >Cut Clipboards</submenu>
     <submenu name='copy' >Copy Clipboards</submenu>
     <submenu name='paste' >Paste Clipboards</submenu>
  </submenu>
</menu>

with getElementsByTagName all submenu is on the same level.
i want it to be structured like tree list, but not change the 'submenu' tag
Sanados at failure dot at 05-Jun-2007 11:45
appended to
brian dot reynolds at risaris dot com
20-Feb-2007 10:09

when you got variable nodes at start you array fails and looses nodes  beneath.
solution that counts occurance though eats up performance:

function xmlToArray($n)
{
    $xml_array = array();
    $occurance = array();

     foreach($n->childNodes as $nc)
     {
         $occurance[$nc->nodeName]++;
     }

    foreach($n->childNodes as $nc){
        if( $nc->hasChildNodes() )
        {
            if($occurance[$nc->nodeName] > 1)
            {
                $xml_array[$nc->nodeName][] = xmlToArray($nc);
            }
            else
            {
                $xml_array[$nc->nodeName] = xmlToArray($nc);
            }
        }
        else
        {
            return utf8_decode($nc->nodeValue);
        }
    }
    return $xml_array;
}
Yanik <clonyara(at)ahoo(dot)com> 14-Mar-2007 08:40
I hate DOM model !
so I wrote dom2array simple function (simple for use):

function dom2array($node) {
  $res = array();
  print $node->nodeType.'<br/>';
  if($node->nodeType == XML_TEXT_NODE){
      $res = $node->nodeValue;
  }
  else{
      if($node->hasAttributes()){
          $attributes = $node->attributes;
          if(!is_null($attributes)){
              $res['@attributes'] = array();
              foreach ($attributes as $index=>$attr) {
                  $res['@attributes'][$attr->name] = $attr->value;
              }
          }
      }
      if($node->hasChildNodes()){
          $children = $node->childNodes;
          for($i=0;$i<$children->length;$i++){
              $child = $children->item($i);
              $res[$child->nodeName] = dom2array($child);
          }
      }
  }
  return $res;
}
Nevyn at N dot O dot S dot P dot A dot M dot emai dot it 10-Mar-2007 04:35
I wrote a couple of functions to:
 - create a DOMDocument from a file
 - parse the namespaces in it
 - create a XPath object with all the namespaces registered
 - load the schemalocations
 - validate the file on the main schema (the one without prefix)

It is useful for me, see if it is also for someone else!!

Giulio

function decodeNode($node)
{
    $out = $node->ownerDocument->saveXML($node);
    $re = "{^<((?:\\w*:)?\\w*)". //the tag name
    "[\\s\n\r]*((?:[\\s\n\r]*".
    "(?:\\w*:)?\\w+[\\s\n\r]*=[\\s\n\r]*". //possible attribute name
    "(?:\"[^\"]*\"|\'[^\']*\'))*)". //attribute value
    "[\\s\n\r]*>[\r\n]*".
    "((?:.*[\r\n]*)*)". //content
    "[\r\n]*</\\1>$}"; //closing tag
    preg_match($re, $out, $mat);
    return $mat;
}

function innerXml($node)
{
    $mat = decodeNode($node);
    return $mat[3];
}

function getnodeAttributes($node)
{
    $mat = decodeNode($node);
    $txt = $mat[2];
    $re = "{((?:\\w*:)?\\w+)[\\s\n\r]*=[\\s\n\r]*(\"[^\"]*\"|\'[^\']*\')}";
    preg_match_all($re, $txt, $mat);
    $att = array();
    for ($i=0; $i<count($mat[0]); $i++)
    {
        $value = $mat[2][$i];
        if ($value[0] == "\'" || $value[0] == "\"")
        {
            $len = strlen($value);
            $value = substr($value, 1, strlen($value)-2);
        }
        $att[ $mat[1][$i] ] = $value;
    }
    return $att;
}

function loadXml($file)
{
    $doc = new DOMDocument();
    $doc->load($file);
    //cerca l'attributo xmlns
    $xsi = false;
    $doc->namespaces = array();
    $doc->xpath = new DOMXPath($doc);

    $attr = getnodeAttributes($doc->documentElement);
    foreach ($attr as $name => $value)
    {
        if (substr($name,0,5) == "xmlns")
        {
            $uri = $value;
            $pre = $doc->documentElement->lookupPrefix($uri);
            if ($uri == "http://www.w3.org/2001/XMLSchema-instance")
                $xsi = $pre;
            $doc->namespaces[$pre] = $uri;
            if ($pre == "")
                $pre = "noname";
            $doc->xpath->registerNamespace($pre, $uri);
        }
    }

    if ($xsi)
    {
        $doc->schemaLocations = array();
        $lst = $doc->xpath->query("//@$xsi:schemaLocation");
        foreach($lst as $el)
        {
            $re = "{[\\s\n\r]*([^\\s\n\r]+)[\\s\n\r]*([^\\s\n\r]+)}";
            preg_match_all($re, $el->nodeValue, $mat);
            for ($i=0; $i<count($mat[0]); $i++)
            {
                $value = $mat[2][$i];
                $doc->schemaLocations[ $mat[1][$i] ] = $value;
            }
        }

        $olddir = getcwd();
        chdir(dirname($file));
        $schema = $doc->schemaLocations[$doc->namespaces[""]];
        if (substr($schema,0,7) == "file://")
        {
            $schema = substr($value,7);
        }
        if (!$doc->schemaValidate($schema))
            dbg()->err("Invalid file");
        chdir($olddir);
    }

    return $doc;
}
brian dot reynolds at risaris dot com 20-Feb-2007 10:09
I found the xml2array function below very useful, but there seems to be a bug in it. The $item variable was never getting set. I've expanded this out to be a bit more readable, and the corrected code is :

function xmlToArray($n)
{
    $return=array();

    foreach($n->childNodes as $nc){
        if( $nc->hasChildNodes() ){
            if( $n->firstChild->nodeName== $n->lastChild->nodeName&&$n->childNodes->length>1){
                $item = $n->firstChild;
                $return[$nc->nodeName][]=$this->xmlToArray($item);
            }
            else{
                 $return[$nc->nodeName]=$this->xmlToArray($nc);
            }
       }
       else{
           $return=$nc->nodeValue;
       }
    }
    return $return;
}
cooper at asu dot ntu-kpi dot kiev dot ua 22-Nov-2006 11:32
If you are using not object-oriented functions and it takes too much time to change them all (or you'll be replacing them later) then as a temporary decision can be used this modules:

For DOM XML:
http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/

For XSLT:
http://alexandre.alapetite.net/doc-alex/xslt-php4-php5/
cormac at idreamproducts dot com 17-Oct-2006 12:48
Most email clients ignore stylesheets in HTML formatted emails.  The best way to ensure your HTML is formatted correctly by a broad spectrum of email clients, including webmail implementations as Gmail, is to use inline style attributes.  The following function uses DOM to parse an inline stylesheet, and will replace element class and id attributes with inline style attributes, and add inline style attributes for generic tag stylesheet rules.  It will remove the stylesheet and any used class and id attributes as these are defunct for most email clients.  It is a fairly lightweight function and does not support CSS inheritance, but will work for simple stylesheets e.g.:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>HTML EMAIL</title>
<style type="text/css">
body {
  margin: 10px 10px;
  font: 8pt arial;
  background: #fff;
  color: #000;
}
p {
  margin: 0 0 10px;
  line-height: 1.2em;
  text-align: justify;
}
p.centered {
  text-align: centre;
}
p#right {
  text-align: right;
}
</style>
</head>
<body>
<p>Sample text justified</p>
<p class="centered">Centered text here</p>
<p id="right">Right-aligned text</p>
</body>
</html>

Here's the function:

<?php
function parseStyleSheetfor Email($html)
{
  $doc = new DOMDocument;
  $doc->loadHTML($html);
  // grab inline stylesheet as DOM object
  $oStyle = $doc->getElementsByTagName('style')->item(0);
  // grab rule identifiers and rules
  preg_match_all('/^([-#._a-z0-9]+) ?\{(.*?)\}/ims', $oStyle->nodeValue, $aMatches, PREG_SET_ORDER);
  foreach ($aMatches as $aRule) {
    $rule_id = $aRule[1];
    // clean up rules
    $rule = str_replace(array("\r", "\n", '  ', '; '), array('', '', ' ', ';'), $aRule[2]);
    $rule = preg_replace(array('/^ /', '/;$/'), '', $rule);
    // generic rules
    if (!strstr($rule_id, '.') && !strstr($rule_id, '#')) {
      $items = $doc->getElementsByTagName($rule_id);
      // set style attribute equal to rule from stylesheet
      foreach ($items as $item) {
        // if there is already inline style append it to end of stylesheet rule
        $current_style = $item->getAttribute('style');
        if (!empty($current_style)) {
          $item->setAttribute('style', $rule . ';' . $current_style);
        } else {
          $item->setAttribute('style', $rule);
        }
      }
    // classes
    } elseif (strstr($rule_id, '.')) {
      list($rule_tag, $rule_class) = explode('.', $rule_id);
      $items = $doc->getElementsByTagName($rule_tag);
      foreach ($items as $item) {
        $class = $item->getAttribute('class');
        if ($class == $rule_class) {
          // if there is already inline style append it to end of stylesheet rule
          $current_style = $item->getAttribute('style');
          if (!empty($current_style)) {
            $item->setAttribute('style', $current_style . ';' . $rule);
          } else {
            $item->setAttribute('style', $rule);
          }
          // remove class as it won't be used now
          $item->removeAttribute('class');
        }
      }
    // ids
    } elseif (strstr($rule_id, '#')) {
      list($rule_tag, $id) = explode('#', $rule_id);
      $item = $doc->getElementById($id);
      $current_style = $item->getAttribute('style');
      if (!empty($current_style)) {
        $item->setAttribute('style', $current_style . ';' . $rule);
      } else {
        $item->setAttribute('style', $rule);
      }
      // remove class as it won't be used now
      $item->removeAttribute('id');
    }
  }
  // remove inline stylesheet
  $oStyle->parentNode->removeChild($oStyle);
  return $doc->saveHTML();
}
?>
sean at lookin3d dot com 17-Aug-2006 07:21
$xmlDoc=<<<XML
<?xml version="1.0"?>
<methodCall>
   <methodName>examples.getStateName</methodName>
   <params>
      <param>
         <value><i4>41</i4></value>
         </param>
      </params>
   </methodCall>
XML;

$xml= new DOMDocument();
$xml->preserveWhiteSpace=false;
$xml->loadXML($xmlDoc);
print_r(xml2array($xml));

function xml2array($n)
{
    $return=array();
    foreach($n->childNodes as $nc)
    ($nc->hasChildNodes())
    ?($n->firstChild->nodeName== $n->lastChild->nodeName&&$n->childNodes->length>1)
    ?$return[$nc->nodeName][]=xml2array($item)
    :$return[$nc->nodeName]=xml2array($nc)
    :$return=$nc->nodeValue;
    return $return;
}
simlee at indiana dot edu 29-Dec-2005 09:16
The project I'm currently working on uses XPaths to dynamically navigate through chunks of an XML file.  I couldn't find any PHP code on the net that would build the XPath to a node for me, so I wrote my own function.  Turns out it wasn't as hard as I thought it might be (yay recursion), though it does entail using some PHP shenanigans...

Hopefully it'll save someone else the trouble of reinventing this wheel.

<?php
    function getNodeXPath( $node ) {
        // REMEMBER THAT XPATHS USE BASE-1 INSTEAD OF BASE-0!!!

        // Get the index for the current node by looping through the siblings.
        $parentNode = $node->parentNode;
        if( $parentNode != null ) {
            $nodeIndex = 0;
            do {
                $testNode = $parentNode->childNodes->item( $nodeIndex );
                $nodeName = $testNode->nodeName;
                $nodeIndex++;

                // PHP trickery!  Here we create a counter based on the node
                //  name of the test node to use in the XPath.
                if( !isset( $$nodeName ) ) $$nodeName = 1;
                else $$nodeName++;

                // Failsafe return value.
                if( $nodeIndex > $parentNode->childNodes->length ) return( "/" );
            } while( !$node->isSameNode( $testNode ) );

            // Recursively get the XPath for the parent.
            return( getNodeXPath( $parentNode ) . "/{$node->nodeName}[{$$nodeName}]" );
        } else {
            // Hit the root node!  Note that the slash is added when
            //  building the XPath, so we return just an empty string.
            return( "" );
        }
    }
?>
johanwthijs-at-hotmail-dot-com 14-Dec-2005 03:25
Being an experienced ASP developer I was wondering how to replace textual content of a node (with msxml this is simply acheived by setting the 'text' property of a node). Out of frustration I started to play around with SimpleXml but I could not get it to work in combination with xPath.

I took me a lot of time to find out so I hope this helps others:

function replaceNodeText($objXml, $objNode, $strNewContent){
    /*
    This function replaces a node's string content with strNewContent
    */
    $objNodeListNested = &$objNode->childNodes;
    foreach ( $objNodeListNested as $objNodeNested ){
         if ($objNodeNested->nodeType == XML_TEXT_NODE)$objNode->removeChild ($objNodeNested);
    }

    $objNode->appendChild($objXml->createTextNode($strNewContent));
}

$objXml= new DOMDocument();
$objXml->loadXML('<root><node id="1">bla</note></root>');
$objXpath = new domxpath($objXml);

$strXpath="/root/node[@id='1']";
$objNodeList = $objXpath ->query($strXpath);
foreach ($objNodeList as $objNode){
//pass the node by reference
replaceNodeText($objXml, &$objNode, $strImportedValue);
}
mark at vectrex dot org dot uk 17-Nov-2005 11:10
Note that these DOM functions expect (and presumably return) all their data in UTF-8 character encoding, regardless of what PHP's current encoding is. This means that text nodes, attribute values etc, should be in utf8.

This applies even if you're generating an XML document which is not ultimately in utf8.

Mark
toby at tobiasly dot com 22-Oct-2005 08:08
This module is not included by default either in the CentOS 4 "centosplus" repository. For those using PHP5 on CentOS 4, a simple "yum --enablerepo=centosplus install php-xml" will do the trick (this will install both the XML and DOM modules).
pes_cz 29-Sep-2005 09:32
When I tried to parse my XHTML Strict files with DOM extension, it couldn't understand xhtml entities (like &copy;). I found post about it here (14-Jul-2005 09:05) which adviced to add resolveExternals = true, but it was very slow. There was some small note about xml catalogs but without any glue. Here it is:

XML catalogs is something like cache. Download all needed dtd's to /etc/xml, edit file /etc/xml/catalog and add this line: <public publicId="-//W3C//DTD XHTML 1.0 Strict//EN" uri="file:///etc/xml/xhtml1-strict.dtd" />

Thats all. Thanks to http://www.whump.com/moreLikeThis/link/03815
amir.laherATcomplinet.com 22-Aug-2005 02:09
This particular W3C page provides invaluable documentation for the DOM classes implemented in php5 (via libxml2). It fills in plenty of php.net's gaps:

http://www.w3.org/TR/DOM-Level-2-Core/core.html

Some key examples:
* concise summary of the class heirachy (1.1.1)
* clarification that DOM level 2 doesn't allow for population of internal DTDs
* explanation of DOMNode->normalize()
* explanation of the DOMImplementation class

The interfaces are described in OMG's Interface Definition Language
aidan at php dot net 19-Jul-2005 03:04
As of PHP 5.1, libxml options may be set using constants rather than the use of proprietary DomDocument properties.

DomDocument->resolveExternals is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDATTR

DomDocument->validateOnParse is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDVALID

PHP 5.1 users are encouraged to use the new constants.

Example:

DomDocument->load($file, LIBXML_DTDLOAD|LIBXML_DTDATTR);

DomDocument->load($file, LIBXML_DTDLOAD|LIBXML_DTDVALID);
aidan at php dot net 19-Jul-2005 05:27
When dealing with validation or loading, the output errors can be quite annoying.

PHP 5.1 introduces libxml_get_errors().

http://php.net/libxml_get_errors
php at webdevelopers dot cz 14-Jul-2005 09:05
[Editor's Note: If you're using entities, then you have no choice. XML Catalogs can speed DTD resolution.]

Never use

$dom->resolveExternals=true;

when parsing XHTML document that has the DOCTYPE declaration with DTD URL specified in it.

Otherwise parsing the XHTML with DOCTYPE like this one:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

will result in PHP/DOM downloading the DTD file from W3C site when parsing your document. It will add extra delay to your script - I experienced that $dom->load()'s total time was from 1 to 16 seconds.

elixon
spammable69 at hotmail dot com 12-Apr-2005 07:18
I wrote a framework to implement the StyleSheet interfaces as specified on the W3C website.  The code is written in PHP, and is NOT a complete implementation.  Use it how ya like.  I was planning on adding the CSSStyleSheet interfaces as well.  Feel free to ask.

<?
    class StyleSheetList {
        public length;
        private self;

        function __construct ( ) {
            $this->self = array();
        }

        function __get($property, $&ret) {
            if($property == 'length')
                $ret = count($this->self);
            return true;
        }

        function __set($property, $val) {
            if($property == 'length')
                return true;
        }

        function item( $index ) {
            return $this->self[$index];
        }
    }

    class MediaList extends StyleSheetList {

        function appendMedium ( $newMedium ) {
            array_push($this->self, $newMedium);
        }

        function deleteMedium ( $oldMedium ) {
            foreach($this->self as $item) {
                if( $item == $oldMedium ) {
                    $item = $this->self[ $this->length-1 ];
                    array_pop($this->self);
                    break;
                }
            }
        }
    }

    class DocumentStyle {
        public styleSheets;

        function __construct ( ) {
            $this->styleSheets = new StyleSheetList();
        }

        function __set($property, $val) {
            if($property == 'styleSheets')
                return true;
        }
    }

    class LinkStyle {
        public sheet;

        function __construct () {
            $this->sheet = new StyleSheet();
        }

        function __set($property, $val) {
            if($property == 'sheet')
                return true;
        }
    }

    class StyleSheet {
        public type;
        public disabled;
        public ownerNode;
        public parentStyleSheet;
        public href;
        public title;
        public media;

        function __construct( $type, $disabled, $ownerNode, $parentStyleSheet, $href, $title){
            $this->type = $type;
            $this->disabled = $disabled;
            $this->media = new MediaList();
            $this->ownerNode = $ownerNode;
            $this->parentStyleSheet = $parentStyleSheet;
            $this->href = $href;
            $this->title = $title;
        }
    }
?>

Only contactable via http://murpsoft.com/contact.html


소개

The DOM extension allows you to operate on XML documents through the DOM API with PHP 5.

Note: PHP 4 offers an older domxml extension.

Note:

The DOM extension uses UTF-8 encoding. Use utf8_encode() and utf8_decode() to work with texts in ISO-8859-1 encoding or Iconv for other encodings.

User Contributed Notes

There are no user contributed notes for this page.


설치/설정

Table of Contents

  • 요구 조건
  • 설치
  • 실행시 설정
  • 자원형

User Contributed Notes

krared at wordpress dot com 16-Apr-2013 06:47
for noobs:

wget http://www6.atomicorp.com/channels/atomic/centos/6/i386/RPMS/php-xml-5.2.17-1.el6.art.i686.rpm

rpm -Uvh php-xml-5.2.17-1.el6.art.i686.rpm

php -m

service httpd restart
d at duane dot com 11-May-2012 02:02
On Fedora 16, you can use
yum -y install php-xml
and then be sure to add
extension=dom.so
in the php.ini file for this to work.
Anonymous 05-Oct-2011 04:40
In Redhat 5.5 using php53 you need to not only install php53-xml but php53-devel as well to install DOMDocument
eugenekay at kashpureff dot org 25-Jan-2011 07:08
Certain Linux distributions do not have this extension included in the minimum PHP package. It can usually be found in one of the "optional" php-* packages.

For CentOS, you will need to run "yum install php-xml", which provides this extension.
Sergey A. 05-Apr-2010 02:38
don't forget
extension=dom.so
in extensions.ini
on freebsd
z1mok at yahoo dot com 22-Apr-2009 02:45
You need the DOM MODULE installed if you want to use Youtube Data Api with the Zend GData Library.

Otherwise you cannot obtain a valid Upload Form Token.


요구 조건

This extension requires the libxml PHP extension. This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default.

User Contributed Notes

david at brayworth dot com dot au 13-Jun-2011 02:55
Under Fedora packaging ssystems you will need php-xml
 => yum install php-xml
James 18-Apr-2010 07:58
You also need to check libxml is installed. Check phpinfo() > Dom to see if it is. Bug report filed to get this added here.


설치

이 확장은 기본값으로 활성화됩니다. 컴파일 시에 다음 옵션으로 비활성화 할 수 있습니다: --disable-dom

User Contributed Notes

There are no user contributed notes for this page.


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

XML constants
Constant | Value | Description
XML_ELEMENT_NODE (integer) | 1 | Node is a DOMElement
XML_ATTRIBUTE_NODE (integer) | 2 | Node is a DOMAttr
XML_TEXT_NODE (integer) | 3 | Node is a DOMText
XML_CDATA_SECTION_NODE (integer) | 4 | Node is a DOMCharacterData
XML_ENTITY_REF_NODE (integer) | 5 | Node is a DOMEntityReference
XML_ENTITY_NODE (integer) | 6 | Node is a DOMEntity
XML_PI_NODE (integer) | 7 | Node is a DOMProcessingInstruction
XML_COMMENT_NODE (integer) | 8 | Node is a DOMComment
XML_DOCUMENT_NODE (integer) | 9 | Node is a DOMDocument
XML_DOCUMENT_TYPE_NODE (integer) | 10 | Node is a DOMDocumentType
XML_DOCUMENT_FRAG_NODE (integer) | 11 | Node is a DOMDocumentFragment
XML_NOTATION_NODE (integer) | 12 | Node is a DOMNotation
XML_HTML_DOCUMENT_NODE (integer) | 13 |
XML_DTD_NODE (integer) | 14 |
XML_ELEMENT_DECL_NODE (integer) | 15 |
XML_ATTRIBUTE_DECL_NODE (integer) | 16 |
XML_ENTITY_DECL_NODE (integer) | 17 |
XML_NAMESPACE_DECL_NODE (integer) | 18 |
XML_ATTRIBUTE_CDATA (integer) | 1 |
XML_ATTRIBUTE_ID (integer) | 2 |
XML_ATTRIBUTE_IDREF (integer) | 3 |
XML_ATTRIBUTE_IDREFS (integer) | 4 |
XML_ATTRIBUTE_ENTITY (integer) | 5 |
XML_ATTRIBUTE_NMTOKEN (integer) | 7 |
XML_ATTRIBUTE_NMTOKENS (integer) | 8 |
XML_ATTRIBUTE_ENUMERATION (integer) | 9 |
XML_ATTRIBUTE_NOTATION (integer) | 10 |
DOMException constants
Constant | Value | Description
DOM_PHP_ERR (integer) | 0 | Error code not part of the DOM specification. Meant for PHP errors.
DOM_INDEX_SIZE_ERR (integer) | 1 | If index or size is negative, or greater than the allowed value.
DOMSTRING_SIZE_ERR (integer) | 2 | If the specified range of text does not fit into a DOMString.
DOM_HIERARCHY_REQUEST_ERR (integer) | 3 | If any node is inserted somewhere it doesn't belong
DOM_WRONG_DOCUMENT_ERR (integer) | 4 | If a node is used in a different document than the one that created it.
DOM_INVALID_CHARACTER_ERR (integer) | 5 | If an invalid or illegal character is specified, such as in a name.
DOM_NO_DATA_ALLOWED_ERR (integer) | 6 | If data is specified for a node which does not support data.
DOM_NO_MODIFICATION_ALLOWED_ERR (integer) | 7 | If an attempt is made to modify an object where modifications are not allowed.
DOM_NOT_FOUND_ERR (integer) | 8 | If an attempt is made to reference a node in a context where it does not exist.
DOM_NOT_SUPPORTED_ERR (integer) | 9 | If the implementation does not support the requested type of object or operation.
DOM_INUSE_ATTRIBUTE_ERR (integer) | 10 | If an attempt is made to add an attribute that is already in use elsewhere.
DOM_INVALID_STATE_ERR (integer) | 11 | If an attempt is made to use an object that is not, or is no longer, usable.
DOM_SYNTAX_ERR (integer) | 12 | If an invalid or illegal string is specified.
DOM_INVALID_MODIFICATION_ERR (integer) | 13 | If an attempt is made to modify the type of the underlying object.
DOM_NAMESPACE_ERR (integer) | 14 | If an attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
DOM_INVALID_ACCESS_ERR (integer) | 15 | If a parameter or an operation is not supported by the underlying object.
DOM_VALIDATION_ERR (integer) | 16 | If a call to a method such as insertBefore or removeChild would make the Node invalid with respect to "partial validity", this exception would be raised and the operation would not be done.

User Contributed Notes

There are no user contributed notes for this page.


예제

Many examples in this reference require an XML file. We will use book.xml that contains the following:

Example #1 book.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
]>
<book id="listing">
 <title>My lists</title>
 <chapter id="books">
  <title>My books</title>
  <para>
   <informaltable>
    <tgroup cols="4">
     <thead>
      <row>
       <entry>Title</entry>
       <entry>Author</entry>
       <entry>Language</entry>
       <entry>ISBN</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>The Grapes of Wrath</entry>
       <entry>John Steinbeck</entry>
       <entry>en</entry>
       <entry>0140186409</entry>
      </row>
      <row>
       <entry>The Pearl</entry>
       <entry>John Steinbeck</entry>
       <entry>en</entry>
       <entry>014017737X</entry>
      </row>
      <row>
       <entry>Samarcande</entry>
       <entry>Amine Maalouf</entry>
       <entry>fr</entry>
       <entry>2253051209</entry>
      </row>
      <!-- TODO: I have a lot of remaining books to add.. -->
     </tbody>
    </tgroup>
   </informaltable>
  </para>
 </chapter>
</book>

User Contributed Notes

There are no user contributed notes for this page.


The DOMAttr class

(PHP 5, PHP 7)

소개

DOMAttr represents an attribute in the DOMElement object.

클래스 개요

DOMAttr extends DOMNode {
/* 프로퍼티 */
public readonly string $name ;
public readonly DOMElement $ownerElement ;
public readonly bool $schemaTypeInfo ;
public readonly bool $specified ;
public string $value ;
/* 메소드 */
public __construct ( string $name [, string $value ] )
public bool isId ( void )
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

프로퍼티

name

The name of the attribute

ownerElement

The element which contains the attribute

schemaTypeInfo

Not implemented yet, always is NULL

specified

Not implemented yet, always is NULL

value

The value of the attribute

참고

  • » W3C specification of Attr

Table of Contents

  • DOMAttr::__construct — Creates a new DOMAttr object
  • DOMAttr::isId — Checks if attribute is a defined ID

User Contributed Notes

greubel at nkey dot de 07-Dec-2014 12:04
Please take care on debugging DOMAttr related code. This will cause an E_WARNING that retrieving schemaTypeInfo attribute is not implemented yet. An IDE like Eclipse will inspect the object and its member elements. In conjunction with registered error handlers, which will translate the E_WARNING into Exception your debugging session will end unexpectedly. This has caused me some trouble while debugging my PHPUnit test case in eclipse using "Makegood" plugin.

A possible workarround is to deactivate the error handling during debugging using error_reporting(0). Remove the statement after finishing your debugging session.

Notable ticket in Bugtracker: https://bugs.php.net/bug.php?id=61858
And a xdebug tracker ticket: http://bugs.xdebug.org/view.php?id=910
chregu at php dot net 23-Mar-2009 08:20
See also the properties from the inherited class DOMNode ( http://php.net/manual/en/class.domnode.php ), eg. namespaceURI, prefix, localName, etc, especially if you have to deal with namespaced attributes


Creates a new DOMAttr object

  • ≪ DOMAttr
  • DOMAttr::isId ≫
  • PHP Manual
  • DOMAttr
  • Creates a new DOMAttr object

DOMAttr::__construct

(PHP 5, PHP 7)

DOMAttr::__construct — Creates a new DOMAttr object

설명

public DOMAttr::__construct ( string $name [, string $value ] )

Creates a new DOMAttr object. This object is read only. It may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. To create a writable node, use DOMDocument::createAttribute.

인수

name

The tag name of the attribute.

value

The value of the attribute.

예제

Example #1 Creating a new DOMAttr object

<?php

$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$attr = $element->setAttributeNode(new DOMAttr('attr', 'attrvalue'));
echo $dom->saveXML();

?>

위 예제의 출력:

<?xml version="1.0" encoding="utf-8"?>
<root attr="attrvalue" />

참고

  • DOMDocument::createAttribute() - Create new attribute

User Contributed Notes

Anonymous 19-Sep-2014 07:48
The output should be:

<?xml version="1.0" encoding="iso-8859-1"?>
<root attr="attrvalue"/>


Checks if attribute is a defined ID

  • ≪ DOMAttr::__construct
  • DOMCdataSection ≫
  • PHP Manual
  • DOMAttr
  • Checks if attribute is a defined ID

DOMAttr::isId

(PHP 5, PHP 7)

DOMAttr::isId — Checks if attribute is a defined ID

설명

public bool DOMAttr::isId ( void )

This function checks if the attribute is a defined ID.

According to the DOM standard this requires a DTD which defines the attribute ID to be of type ID. You need to validate your document with DOMDocument::validate or DOMDocument::validateOnParse before using this function.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 DOMAttr::isId() Example

<?php

$doc = new DomDocument;

// We need to validate our document before refering to the id
$doc->validateOnParse = true;
$doc->Load('book.xml');

// We retrieve the attribute named id of the chapter element
$attr = $doc->getElementsByTagName('chapter')->item(0)->getAttributeNode('id');

var_dump($attr->isId()); // bool(true)

?>

User Contributed Notes

There are no user contributed notes for this page.


The DOMCdataSection class

(PHP 5, PHP 7)

소개

The DOMCdataSection inherits from DOMText for textural representation of CData constructs.

클래스 개요

DOMCdataSection extends DOMText {
/* Methods */
public __construct ( string $value )
/* Inherited methods */
public bool DOMText::isWhitespaceInElementContent ( void )
public DOMText DOMText::splitText ( int $offset )
}

Table of Contents

  • DOMCdataSection::__construct — Constructs a new DOMCdataSection object

User Contributed Notes

There are no user contributed notes for this page.


Constructs a new DOMCdataSection object

  • ≪ DOMCdataSection
  • DOMCharacterData ≫
  • PHP Manual
  • DOMCdataSection
  • Constructs a new DOMCdataSection object

DOMCdataSection::__construct

(PHP 5, PHP 7)

DOMCdataSection::__construct — Constructs a new DOMCdataSection object

설명

public DOMCdataSection::__construct ( string $value )

Constructs a new CDATA node. This works like the DOMText class.

인수

value

The value of the CDATA node. If not supplied, an empty CDATA node is created.

예제

Example #1 Creating a new DOMCdataSection object

<?php

$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->appendChild(new DOMElement('root'));
$text = $element->appendChild(new DOMCdataSection('root value'));
echo $dom->saveXML();

?>

위 예제의 출력:

<?xml version="1.0" encoding="utf-8"?>
<root><![CDATA[root value]]></root>

참고

  • DOMText::__construct() - Creates a new DOMText object
  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

There are no user contributed notes for this page.


The DOMCharacterData class

(PHP 5, PHP 7)

소개

Represents nodes with character data. No nodes directly correspond to this class, but other nodes do inherit from it.

클래스 개요

DOMCharacterData extends DOMNode {
/* 프로퍼티 */
public string $data ;
readonly public int $length ;
/* 메소드 */
void appendData ( string $data )
void deleteData ( int $offset , int $count )
void insertData ( int $offset , string $data )
void replaceData ( int $offset , int $count , string $data )
string substringData ( int $offset , int $count )
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

프로퍼티

data

The contents of the node.

length

The length of the contents.

참고

  • » W3C specification of CharacterData

Table of Contents

  • DOMCharacterData::appendData — Append the string to the end of the character data of the node
  • DOMCharacterData::deleteData — Remove a range of characters from the node
  • DOMCharacterData::insertData — Insert a string at the specified 16-bit unit offset
  • DOMCharacterData::replaceData — Replace a substring within the DOMCharacterData node
  • DOMCharacterData::substringData — Extracts a range of data from the node

User Contributed Notes

jesdisciple dot FOO at gmail dot BAR dot com 08-Jul-2010 01:35
In case you're looking for the CDATA class to which nodes /do/ correspond, it's called DOMCdataSection (per my PHP installation) and isn't documented on php.net.  Presumably, it doesn't add anything to this API.

See also: http://www.php.net/manual/en/domdocument.createcdatasection.php


Append the string to the end of the character data of the node

  • ≪ DOMCharacterData
  • DOMCharacterData::deleteData ≫
  • PHP Manual
  • DOMCharacterData
  • Append the string to the end of the character data of the node

DOMCharacterData::appendData

(PHP 5, PHP 7)

DOMCharacterData::appendData — Append the string to the end of the character data of the node

설명

void DOMCharacterData::appendData ( string $data )

Append the string data to the end of the character data of the node.

인수

data

The string to append.

반환값

값을 반환하지 않습니다.

참고

  • DOMCharacterData::deleteData() - Remove a range of characters from the node
  • DOMCharacterData::insertData() - Insert a string at the specified 16-bit unit offset
  • DOMCharacterData::replaceData() - Replace a substring within the DOMCharacterData node
  • DOMCharacterData::substringData() - Extracts a range of data from the node

User Contributed Notes

There are no user contributed notes for this page.


Remove a range of characters from the node

  • ≪ DOMCharacterData::appendData
  • DOMCharacterData::insertData ≫
  • PHP Manual
  • DOMCharacterData
  • Remove a range of characters from the node

DOMCharacterData::deleteData

(PHP 5, PHP 7)

DOMCharacterData::deleteData — Remove a range of characters from the node

설명

void DOMCharacterData::deleteData ( int $offset , int $count )

Deletes count characters starting from position offset.

인수

offset

The offset from which to start removing.

count

The number of characters to delete. If the sum of offset and count exceeds the length, then all characters to the end of the data are deleted.

반환값

값을 반환하지 않습니다.

오류/예외

DOM_INDEX_SIZE_ERR

Raised if offset is negative or greater than the number of 16-bit units in data, or if count is negative.

참고

  • DOMCharacterData::appendData() - Append the string to the end of the character data of the node
  • DOMCharacterData::insertData() - Insert a string at the specified 16-bit unit offset
  • DOMCharacterData::replaceData() - Replace a substring within the DOMCharacterData node
  • DOMCharacterData::substringData() - Extracts a range of data from the node

User Contributed Notes

There are no user contributed notes for this page.


Insert a string at the specified 16-bit unit offset

  • ≪ DOMCharacterData::deleteData
  • DOMCharacterData::replaceData ≫
  • PHP Manual
  • DOMCharacterData
  • Insert a string at the specified 16-bit unit offset

DOMCharacterData::insertData

(PHP 5, PHP 7)

DOMCharacterData::insertData — Insert a string at the specified 16-bit unit offset

설명

void DOMCharacterData::insertData ( int $offset , string $data )

Inserts string data at position offset.

인수

offset

The character offset at which to insert.

data

The string to insert.

반환값

값을 반환하지 않습니다.

오류/예외

DOM_INDEX_SIZE_ERR

Raised if offset is negative or greater than the number of 16-bit units in data.

참고

  • DOMCharacterData::appendData() - Append the string to the end of the character data of the node
  • DOMCharacterData::deleteData() - Remove a range of characters from the node
  • DOMCharacterData::replaceData() - Replace a substring within the DOMCharacterData node
  • DOMCharacterData::substringData() - Extracts a range of data from the node

User Contributed Notes

There are no user contributed notes for this page.


Replace a substring within the DOMCharacterData node

  • ≪ DOMCharacterData::insertData
  • DOMCharacterData::substringData ≫
  • PHP Manual
  • DOMCharacterData
  • Replace a substring within the DOMCharacterData node

DOMCharacterData::replaceData

(PHP 5, PHP 7)

DOMCharacterData::replaceData — Replace a substring within the DOMCharacterData node

설명

void DOMCharacterData::replaceData ( int $offset , int $count , string $data )

Replace count characters starting from position offset with data.

인수

offset

The offset from which to start replacing.

count

The number of characters to replace. If the sum of offset and count exceeds the length, then all characters to the end of the data are replaced.

data

The string with which the range must be replaced.

반환값

값을 반환하지 않습니다.

오류/예외

DOM_INDEX_SIZE_ERR

Raised if offset is negative or greater than the number of 16-bit units in data, or if count is negative.

참고

  • DOMCharacterData::appendData() - Append the string to the end of the character data of the node
  • DOMCharacterData::deleteData() - Remove a range of characters from the node
  • DOMCharacterData::insertData() - Insert a string at the specified 16-bit unit offset
  • DOMCharacterData::substringData() - Extracts a range of data from the node

User Contributed Notes

There are no user contributed notes for this page.


Extracts a range of data from the node

  • ≪ DOMCharacterData::replaceData
  • DOMComment ≫
  • PHP Manual
  • DOMCharacterData
  • Extracts a range of data from the node

DOMCharacterData::substringData

(PHP 5, PHP 7)

DOMCharacterData::substringData — Extracts a range of data from the node

설명

string DOMCharacterData::substringData ( int $offset , int $count )

Returns the specified substring.

인수

offset

Start offset of substring to extract.

count

The number of characters to extract.

반환값

The specified substring. If the sum of offset and count exceeds the length, then all 16-bit units to the end of the data are returned.

오류/예외

DOM_INDEX_SIZE_ERR

Raised if offset is negative or greater than the number of 16-bit units in data, or if count is negative.

참고

  • DOMCharacterData::appendData() - Append the string to the end of the character data of the node
  • DOMCharacterData::deleteData() - Remove a range of characters from the node
  • DOMCharacterData::insertData() - Insert a string at the specified 16-bit unit offset
  • DOMCharacterData::replaceData() - Replace a substring within the DOMCharacterData node

User Contributed Notes

There are no user contributed notes for this page.


The DOMComment class

(PHP 5, PHP 7)

소개

Represents comment nodes, characters delimited by <!-- and -->.

클래스 개요

DOMComment extends DOMCharacterData {
/* 메소드 */
public __construct ([ string $value ] )
/* 상속된 메소드 */
void DOMCharacterData::appendData ( string $data )
void DOMCharacterData::deleteData ( int $offset , int $count )
void DOMCharacterData::insertData ( int $offset , string $data )
void DOMCharacterData::replaceData ( int $offset , int $count , string $data )
string DOMCharacterData::substringData ( int $offset , int $count )
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

참고

  • » W3C specification of Comment

Table of Contents

  • DOMComment::__construct — Creates a new DOMComment object

User Contributed Notes

There are no user contributed notes for this page.


Creates a new DOMComment object

  • ≪ DOMComment
  • DOMDocument ≫
  • PHP Manual
  • DOMComment
  • Creates a new DOMComment object

DOMComment::__construct

(PHP 5, PHP 7)

DOMComment::__construct — Creates a new DOMComment object

설명

public DOMComment::__construct ([ string $value ] )

Creates a new DOMComment object. This object is read only. It may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. To create a writeable node, use DOMDocument::createComment.

인수

value

The value of the comment.

예제

Example #1 Creating a new DOMComment

<?php

$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$comment = $element->appendChild(new DOMComment('root comment'));
echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?><root><!--root comment--></root> */

?>

참고

  • DOMDocument::createComment() - Create new comment node

User Contributed Notes

There are no user contributed notes for this page.


The DOMDocument class

(PHP 5, PHP 7)

소개

Represents an entire HTML or XML document; serves as the root of the document tree.

클래스 개요

DOMDocument extends DOMNode {
/* 프로퍼티 */
readonly public string $actualEncoding ;
readonly public DOMConfiguration $config ;
readonly public DOMDocumentType $doctype ;
readonly public DOMElement $documentElement ;
public string $documentURI ;
public string $encoding ;
public bool $formatOutput ;
readonly public DOMImplementation $implementation ;
public bool $preserveWhiteSpace = true ;
public bool $recover ;
public bool $resolveExternals ;
public bool $standalone ;
public bool $strictErrorChecking = true ;
public bool $substituteEntities ;
public bool $validateOnParse = false ;
public string $version ;
readonly public string $xmlEncoding ;
public bool $xmlStandalone ;
public string $xmlVersion ;
/* 메소드 */
public __construct ([ string $version [, string $encoding ]] )
public DOMAttr createAttribute ( string $name )
public DOMAttr createAttributeNS ( string $namespaceURI , string $qualifiedName )
public DOMCDATASection createCDATASection ( string $data )
public DOMComment createComment ( string $data )
public DOMDocumentFragment createDocumentFragment ( void )
public DOMElement createElement ( string $name [, string $value ] )
public DOMElement createElementNS ( string $namespaceURI , string $qualifiedName [, string $value ] )
public DOMEntityReference createEntityReference ( string $name )
public DOMProcessingInstruction createProcessingInstruction ( string $target [, string $data ] )
public DOMText createTextNode ( string $content )
public DOMElement getElementById ( string $elementId )
public DOMNodeList getElementsByTagName ( string $name )
public DOMNodeList getElementsByTagNameNS ( string $namespaceURI , string $localName )
public DOMNode importNode ( DOMNode $importedNode [, bool $deep ] )
public mixed load ( string $filename [, int $options = 0 ] )
public bool loadHTML ( string $source [, int $options = 0 ] )
public bool loadHTMLFile ( string $filename [, int $options = 0 ] )
public mixed loadXML ( string $source [, int $options = 0 ] )
public void normalizeDocument ( void )
public bool registerNodeClass ( string $baseclass , string $extendedclass )
public bool relaxNGValidate ( string $filename )
public bool relaxNGValidateSource ( string $source )
public int save ( string $filename [, int $options ] )
public string saveHTML ([ DOMNode $node = NULL ] )
public int saveHTMLFile ( string $filename )
public string saveXML ([ DOMNode $node [, int $options ]] )
public bool schemaValidate ( string $filename [, int $flags ] )
public bool schemaValidateSource ( string $source [, int $flags ] )
public bool validate ( void )
public int xinclude ([ int $options ] )
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

프로퍼티

actualEncoding

Deprecated. Actual encoding of the document, is a readonly equivalent to encoding.

config

Deprecated. Configuration used when DOMDocument::normalizeDocument() is invoked.

doctype

The Document Type Declaration associated with this document.

documentElement

This is a convenience attribute that allows direct access to the child node that is the document element of the document.

documentURI

The location of the document or NULL if undefined.

encoding

Encoding of the document, as specified by the XML declaration. This attribute is not present in the final DOM Level 3 specification, but is the only way of manipulating XML document encoding in this implementation.

formatOutput

Nicely formats output with indentation and extra space.

implementation

The DOMImplementation object that handles this document.

preserveWhiteSpace

Do not remove redundant white space. Default to TRUE.

recover

Proprietary. Enables recovery mode, i.e. trying to parse non-well formed documents. This attribute is not part of the DOM specification and is specific to libxml.

resolveExternals

Set it to TRUE to load external entities from a doctype declaration. This is useful for including character entities in your XML document.

standalone

Deprecated. Whether or not the document is standalone, as specified by the XML declaration, corresponds to xmlStandalone.

strictErrorChecking

Throws DOMException on errors. Default to TRUE.

substituteEntities

Proprietary. Whether or not to substitute entities. This attribute is not part of the DOM specification and is specific to libxml.

validateOnParse

Loads and validates against the DTD. Default to FALSE.

version

Deprecated. Version of XML, corresponds to xmlVersion.

xmlEncoding

An attribute specifying, as part of the XML declaration, the encoding of this document. This is NULL when unspecified or when it is not known, such as when the Document was created in memory.

xmlStandalone

An attribute specifying, as part of the XML declaration, whether this document is standalone. This is FALSE when unspecified.

xmlVersion

An attribute specifying, as part of the XML declaration, the version number of this document. If there is no declaration and if this document supports the "XML" feature, the value is "1.0".

주의

Note:

The DOM extension uses UTF-8 encoding. Use utf8_encode() and utf8_decode() to work with texts in ISO-8859-1 encoding or Iconv for other encodings.

참고

  • » W3C specification for Document

Table of Contents

  • DOMDocument::__construct — Creates a new DOMDocument object
  • DOMDocument::createAttribute — Create new attribute
  • DOMDocument::createAttributeNS — Create new attribute node with an associated namespace
  • DOMDocument::createCDATASection — Create new cdata node
  • DOMDocument::createComment — Create new comment node
  • DOMDocument::createDocumentFragment — Create new document fragment
  • DOMDocument::createElement — Create new element node
  • DOMDocument::createElementNS — Create new element node with an associated namespace
  • DOMDocument::createEntityReference — Create new entity reference node
  • DOMDocument::createProcessingInstruction — Creates new PI node
  • DOMDocument::createTextNode — Create new text node
  • DOMDocument::getElementById — Searches for an element with a certain id
  • DOMDocument::getElementsByTagName — Searches for all elements with given local tag name
  • DOMDocument::getElementsByTagNameNS — Searches for all elements with given tag name in specified namespace
  • DOMDocument::importNode — Import node into current document
  • DOMDocument::load — Load XML from a file
  • DOMDocument::loadHTML — Load HTML from a string
  • DOMDocument::loadHTMLFile — Load HTML from a file
  • DOMDocument::loadXML — Load XML from a string
  • DOMDocument::normalizeDocument — Normalizes the document
  • DOMDocument::registerNodeClass — Register extended class used to create base node type
  • DOMDocument::relaxNGValidate — Performs relaxNG validation on the document
  • DOMDocument::relaxNGValidateSource — Performs relaxNG validation on the document
  • DOMDocument::save — Dumps the internal XML tree back into a file
  • DOMDocument::saveHTML — Dumps the internal document into a string using HTML formatting
  • DOMDocument::saveHTMLFile — Dumps the internal document into a file using HTML formatting
  • DOMDocument::saveXML — Dumps the internal XML tree back into a string
  • DOMDocument::schemaValidate — Validates a document based on a schema
  • DOMDocument::schemaValidateSource — Validates a document based on a schema
  • DOMDocument::validate — Validates the document based on its DTD
  • DOMDocument::xinclude — Substitutes XIncludes in a DOMDocument Object

User Contributed Notes

biker dot mike at gmx dot com 22-Aug-2016 10:36
Look out for the following gotcha when loading XML from a string:

<?php
$doc = new \DOMDocument;
$doc->documentURI = $myXmlFilename;
$doc->loadXML($myXmlString);
?>

documentURI is now set to the value of $myXmlFilename, right?

Wrong!

It's set to the current working directory.  If you want to manually set documentURI to something other than the CWD, do so AFTER the call to loadXML().

E.g.:
<?php
$doc = new \DOMDocument;
$doc->loadXML($myXmlString);
$doc->documentURI = $myXmlFilename;
?>

documentURI really is now set to the value of $myXmlFilename.
developer at nabtron dot com 17-Dec-2015 04:34
For those landing here and checking for encoding issue with utf-8 characteres, it's pretty easy to correct it, without adding any additional output tag to your html.

We'll be utilizing: mb_convert_encoding

Thanks to the user who shared: SmartDOMDocument in previous comments, I got the idea of solving it. However I truly wish that he shared the method instead of giving a link.

Anyway coming back to the solution, you can simply use:

<?php

            // checks if the content we're receiving isn't empty, to avoid the warning
            if ( empty( $content ) ) {
                return false;
            }

            // converts all special characters to utf-8
            $content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');

            // creating new document
            $doc = new DOMDocument('1.0', 'utf-8');

            //turning off some errors
            libxml_use_internal_errors(true);

            // it loads the content without adding enclosing html/body tags and also the doctype declaration
            $doc->LoadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

            // do whatever you want to do with this code now

?>

I hope it solves the issue for someone! If you need my help or service to fix your code, you can reach me on nabtron.com or contact me at the email mentioned with this comment.
ingjetel at gmail dot com 14-May-2015 08:54
Easy function for basic output of XML file via DOM parsing

<?php
$dom = new DomDocument();
$dom->load("./file.xml") or die("error");
$start = $dom->documentElement;
fc($start);

function fc($node) {
  $child = $node->childNodes;
  foreach($child as $item) {
    if ($item->nodeType == XML_TEXT_NODE) {
      if (strlen(trim($item->nodeValue))) echo trim($item->nodeValue)."<br/>";
    }
    else if ($item->nodeType == XML_ELEMENT_NODE) fc($item);
  }
}
?>
qrworld.net 12-Nov-2014 01:35
In this post http://softontherocks.blogspot.com/2014/11/descargar-el-contenido-de-una-url_11.html I found a simple way to get the content of a URL with DOMDocument, loadHTMLFile and saveHTML().

function getURLContent($url){
    $doc = new DOMDocument;
    $doc->preserveWhiteSpace = FALSE;
    @$doc->loadHTMLFile($url);
    return $doc->saveHTML();
}
danny dot nunez15 at gmail dot com 28-Oct-2013 11:54
A simple function to grab all links in a page.

    function get_links($url) {

        // Create a new DOM Document to hold our webpage structure
        $xml = new DOMDocument();

        // Load the url's contents into the DOM

        $xml->loadHTMLFile($url);

        // Empty array to hold all links to return
        $links = array();

        //Loop through each <a> tag in the dom and add it to the link array
        foreach ($xml->getElementsByTagName('a') as $link) {
            $url = $link->getAttribute('href');
            if (!empty($url)) {
                $links[] = $link->getAttribute('href');
            }
        }

        //Return the links
        return $links;
    }
sites.sitesbr.net 22-Jan-2013 09:37
How to objetify a DomDocument with hierarchy like:
<root>
    <item>
          <prop1>info1</prop1>
          <prop2>info2</prop2>
          <prop3>info3</prop3>
     </item>
    <item>
          <prop1>info1</prop1>
          <prop2>info2</prop2>
          <prop3>info3</prop3>
     </item>
</root>

It's possible to use in object style to retrieve information, as:

<?php
     $theNodeValue = $aitem->prop1;
?>

Here is the code: one Class and 2 functions.

<?php
 class ArrayNode{
       public $nodeName, $nodeValue;
 }

 function getChildNodeElements( $domNode ){
     $nodes = array();
     for( $i=0; $i < $domNode->childNodes->length; $i++){
       $cn = $domNode->childNodes->item($i);
       if( $cn->nodeType == 1){
           $nodes[] = $cn;
           }
     }
    return $nodes;
 }

 function getArrayNodes( $domDoc ){
     $res = array();

       for( $i=0; $i < $domDoc->childNodes->length; $i++){
       $cn = $domDoc->childNodes->item($i);
       # The first is the root tag...
          if( $cn->nodeType == 1){
               # But we want it's childNodes.
                $sub_cn = getChildNodeElements( $cn);
                # Found the tagName:
                $baseItemTagName = $sub_cn[0]->nodeName;
                break;
            }
        }

       $dnl = $domDoc->getElementsByTagName( $baseItemTagName);

       for( $i=0; $i< $dnl->length; $i++){
          $arrayNode = new ArrayNode();

      # Summary
      $arrayNode->nodeName = $dnl->item($i)->nodeName;
      $arrayNode->nodeValue = $dnl->item($i)->nodeValue;

      # Child Nodes
      $cn = $dnl->item($i)->childNodes;
      for( $k=0; $k<$cn->length; $k++){
           if( $cn->item($k)->nodeName == "#text" && trim($cn->item($k)->nodeValue) == "") continue;
           $arrayNode->{$cn->item($k)->nodeName} = $cn->item($k)->nodeValue;
      }

      # Attributes
      $attr = $dnl->item($i)->attributes;
      for( $k=0; $k < $attr->length; $k++){
           if(! is_null($attr)){
            if( $attr->item($k)->nodeName == "#text" && trim($attr->item($k)->nodeValue) == "") continue;
            $arrayNode->{$attr->item($k)->nodeName} = $attr->item($k)->nodeValue;
           }
      }

      $res[] = $arrayNode;

       }

     return $res;
 }
?>

To use it:

<?php

  # First you load a XML in a DomDocument variable.

   $url = "/path/to/yourxmlfile.xml";
   $domSrc = file_get_contents($url);
   $dom = new DomDocument();
   $dom->loadXML( $domSrc );

  # Then, you get the ArrayNodes from the DomDocument.

    $ans = getArrayNodes( $dom );

    for( $i=0; $i < count( $ans ) ; $i++){

    $cn =  $ans[ $i];

    $info1 =  $cn->prop1;
    $info2 =  $cn->prop2;
    $info3 =  $cn->prop3;

         // ...

   }

?>
Nick M 01-Jun-2011 07:16
You may need to save all or part of a DOMDocument as an XHTML-friendly string, something compliant with both XML and HTML 4. Here's the DOMDocument class extended with a saveXHTML method:

<?php

/**
 * XHTML Document
 *
 * Represents an entire XHTML DOM document; serves as the root of the document tree.
 */
class XHTMLDocument extends DOMDocument {

  /**
   * These tags must always self-terminate. Anything else must never self-terminate.
   *
   * @var array
   */
  public $selfTerminate = array(
      'area','base','basefont','br','col','frame','hr','img','input','link','meta','param'
  );

  /**
   * saveXHTML
   *
   * Dumps the internal XML tree back into an XHTML-friendly string.
   *
   * @param DOMNode $node
   *         Use this parameter to output only a specific node rather than the entire document.
   */
  public function saveXHTML(DOMNode $node=null) {

    if (!$node) $node = $this->firstChild;

    $doc = new DOMDocument('1.0');
    $clone = $doc->importNode($node->cloneNode(false), true);
    $term = in_array(strtolower($clone->nodeName), $this->selfTerminate);
    $inner='';

    if (!$term) {
      $clone->appendChild(new DOMText(''));
      if ($node->childNodes) foreach ($node->childNodes as $child) {
        $inner .= $this->saveXHTML($child);
      }
    }

    $doc->appendChild($clone);
    $out = $doc->saveXML($clone);

    return $term ? substr($out, 0, -2) . ' />' : str_replace('><', ">$inner<", $out);

  }

}

?>

This hasn't been benchmarked, but is probably significantly slower than saveXML or saveHTML and should be used sparingly.
evert at er dot nl 20-Nov-2010 11:17
A nice and simple node 2 array I wrote, worth a try ;)

<?php
function getArray($node)
{
    $array = false;

    if ($node->hasAttributes())
    {
        foreach ($node->attributes as $attr)
        {
            $array[$attr->nodeName] = $attr->nodeValue;
        }
    }

    if ($node->hasChildNodes())
    {
        if ($node->childNodes->length == 1)
        {
            $array[$node->firstChild->nodeName] = $node->firstChild->nodeValue;
        }
        else
        {
            foreach ($node->childNodes as $childNode)
            {
                if ($childNode->nodeType != XML_TEXT_NODE)
                {
                    $array[$childNode->nodeName][] = $this->getArray($childNode);
                }
            }
        }
    }

    return $array;
}
?>
admin at beerpla dot net 12-Mar-2010 11:12
After seeing many complaints about certain DOMDocument shortcomings, such as bad handling of encodings and always saving HTML fragments with <html>, <head>, and DOCTYPE, I decided that a better solution is needed.

So here it is: SmartDOMDocument. You can find it at http://beerpla.net/projects/smartdomdocument/

Currently, the main highlights are:

- SmartDOMDocument inherits from DOMDocument, so it's very easy to use - just declare an object of type SmartDOMDocument instead of DOMDocument and enjoy the new behavior on top of all existing functionality (see example below).

- saveHTMLExact() - DOMDocument has an extremely badly designed "feature" where if the HTML code you are loading does not contain <html> and <body> tags, it adds them automatically (yup, there are no flags to turn this behavior off).
Thus, when you call $doc->saveHTML(), your newly saved content now has <html><body> and DOCTYPE in it. Not very handy when trying to work with code fragments (XML has a similar problem).
SmartDOMDocument contains a new function called saveHTMLExact() which does exactly what you would want - it saves HTML without adding that extra garbage that DOMDocument does.

- encoding fix - DOMDocument notoriously doesn't handle encoding (at least UTF-8) correctly and garbles the output.
SmartDOMDocument tries to work around this problem by enhancing loadHTML() to deal with encoding correctly. This behavior is transparent to you - just use loadHTML() as you would normally.

- SmartDOMDocument Object As String - you can use a SmartDOMDocument object as a string which will print out its contents.
For example:
<?php
echo "Here is the HTML: $smart_dom_doc";
?>

I'm going to maintain this code and try to fix bugs as they come in.

Enjoy.
tloach at gmail dot com 05-Feb-2010 07:01
For anyone else who has been having issues with formatOuput not working, here is a work-around:

rather than just doing something like:

<?php
$outXML = $xml->saveXML();
?>

force it to reload the XML from scratch, then it will format correctly:

<?php
$outXML = $xml->saveXML();
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->loadXML($outXML);
$outXML = $xml->saveXML();
?>
jay at jaygilford dot com 27-Jan-2010 05:46
Here's a small function I wrote to get all page links using the DOMDocument which will hopefully be of use to others

<?php
/**
 * @author Jay Gilford
 */

/**
 * get_links()
 *
 * @param string $url
 * @return array
 */
function get_links($url) {

    // Create a new DOM Document to hold our webpage structure
    $xml = new DOMDocument();

    // Load the url's contents into the DOM
    $xml->loadHTMLFile($url);

    // Empty array to hold all links to return
    $links = array();

    //Loop through each <a> tag in the dom and add it to the link array
    foreach($xml->getElementsByTagName('a') as $link) {
        $links[] = array('url' => $link->getAttribute('href'), 'text' => $link->nodeValue);
    }

    //Return the links
    return $links;
}
?>
fcartegnie 31-Oct-2009 10:30
Be careful with formatOutput().

Creating an empty node like this:
createElement('foo','')
instead of
createElement('foo')
will break formatOutput.
PhilipWayneRollins at gmail dot com 15-Aug-2009 10:32
If you want to use the DOMDocument to create xHTML documents here is a simple class

Note this is designed for creating xHTML documents from scratch but could be easily extended to work with xHTML documents. Also this is for xHTML not XML.

<?php
    class Document
    {
        public $doctype;
        public $head;
        public $title = 'Sensei Ninja';
        public $body;
        private $styles;
        private $metas;
        private $scripts;
        private $document;

        function __construct (  )
        {
            $this->document = new DOMDocument( );
            $this->head = $this->document->createElement( 'head', ' ' );
            $this->body = $this->document->createElement( 'body', ' ' );
        }

        public function addStyleSheet ( $url, $media='all' )
        {
            $element = $this->document->createElement( 'link' );
            $element->setAttribute( 'type', 'text/css' );
            $element->setAttribute( 'href', $url );
            $element->setAttribute( 'media', $media );
            $this->styles[] = $element;
        }

        public function addScript ( $url )
        {
            $element = $this->document->createElement( 'script', ' ' );
            $element->setAttribute( 'type', 'text/javascript' );
            $element->setAttribute( 'src', $url );
            $this->scripts[] = $element;
        }

        public function addMetaTag ( $name, $content )
        {
            $element = $this->document->createElement( 'meta' );
            $element->setAttribute( 'name', $name );
            $element->setAttribute( 'content', $content );
            $this->metas[] = $element;
        }

        public function setDescription ( $dec )
        {
            $this->addMetaTag( 'description', $dec );
        }

        public function setKeywords ( $keywords )
        {
            $this->addMetaTag( 'keywords', $keywords );
        }

        public function createElement ( $nodeName, $nodeValue=null )
        {
          return $this->document->createElement( $nodeName, $nodeValue );
        }

        public function assemble ( )
        {
            // Doctype creation
            $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML TRANSITIONAL 1.0//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';

            // Create the head element
            $title = $this->document->createElement( 'title', $this->title );
            // Add stylesheets if needed
            if ( is_array( $this->styles ))
                foreach ( $this->styles as $element )
                    $this->head->appendChild( $element );
            // Add scripts if needed
            if(  is_array( $this->scripts ))
                foreach ( $this->scripts as $element )
                    $this->head->appendChild( $element );
            // Add meta tags if needed
            if ( is_array( $this->metas ))
                foreach ( $this->metas as $element )
                    $this->head->appendChild( $element );
            $this->head->appendChild( $title );

            // Create the document
            $html = $this->document->createElement( 'html' );
            $html->setAttribute( 'xmlns', 'http://www.w3.org/1999/xhtml' );
            $html->setAttribute( 'xml:lang', 'en' );
            $html->setAttribute( 'lang', 'en' );
            $html->appendChild( $this->head );
            $html->appendChild( $this->body );

            $this->document->appendChild( $html );
            return $doctype . $this->document->saveXML( );
        }

    }

?>

Small example

<?php
        $document = new Document( );
    $document->title = 'Hello';
    $document->addStyleSheet( 'StyleSheets/main.css' );
    $div = $document->createElement( 'div' );
    $div->nodeValue = 'Hello, world!';
    $div->setAttribute( 'style', 'color: red;' );
    $document->body->appendChild( $div );
    printf( '%s', $document->assemble( ) );
?>
cmyk777 at gmail dot com 23-May-2009 09:31
This function may help to debug current dom element:

<?php
function dom_dump($obj) {
    if ($classname = get_class($obj)) {
        $retval = "Instance of $classname, node list: \n";
        switch (true) {
            case ($obj instanceof DOMDocument):
                $retval .= "XPath: {$obj->getNodePath()}\n".$obj->saveXML($obj);
                break;
            case ($obj instanceof DOMElement):
                $retval .= "XPath: {$obj->getNodePath()}\n".$obj->ownerDocument->saveXML($obj);
                break;
            case ($obj instanceof DOMAttr):
                $retval .= "XPath: {$obj->getNodePath()}\n".$obj->ownerDocument->saveXML($obj);
                //$retval .= $obj->ownerDocument->saveXML($obj);
                break;
            case ($obj instanceof DOMNodeList):
                for ($i = 0; $i < $obj->length; $i++) {
                    $retval .= "Item #$i, XPath: {$obj->item($i)->getNodePath()}\n".
"{$obj->item($i)->ownerDocument->saveXML($obj->item($i))}\n";
                }
                break;
            default:
                return "Instance of unknown class";
        }
    } else {
        return 'no elements...';
    }
    return htmlspecialchars($retval);
}
?>

Example usage:

<?php
$dom = new DomDocument();
$dom->load('test.xml');
$body = $dom->documentElement->getElementsByTagName('book');
echo '<pre>'.dom_dump($body).'<pre>';
?>

Output:

Instance of DOMNodeList, node list:
Item #0, XPath: /library/book[1]
<book isbn="0345342968">
<title>Fahrenheit 451</title>
<author>R. Bradbury</author>
<publisher>Del Rey</publisher>
</book>
Item #1, XPath: /library/book[2]
<book isbn="0048231398">
<title>The Silmarillion</title>
<author>J.R.R. Tolkien</author>
<publisher>G. Allen &amp; Unwin</publisher>
</book>
Item #2, XPath: /library/book[3]
<book isbn="0451524934">
<title>1984</title>
<author>G. Orwell</author>
<publisher>Signet</publisher>
</book>
Item #3, XPath: /library/book[4]
<book isbn="031219126X">
<title>Frankenstein</title>
<author>M. Shelley</author>
<publisher>Bedford</publisher>
</book>
Item #4, XPath: /library/book[5]
<book isbn="0312863551">
<title>The Moon Is a Harsh Mistress</title>
<author>R. A. Heinlein</author>
<publisher>Orb</publisher>
</book>
Fernando H 11-Apr-2008 09:48
Showing a quick example of how to use this class, just so that new users can get a quick start without having to figure it all out by themself. ( At the day of posting, this documentation just got added and is lacking examples. )

<?php

// Set the content type to be XML, so that the browser will   recognise it as XML.
header( "content-type: application/xml; charset=ISO-8859-15" );

// "Create" the document.
$xml = new DOMDocument( "1.0", "ISO-8859-15" );

// Create some elements.
$xml_album = $xml->createElement( "Album" );
$xml_track = $xml->createElement( "Track", "The ninth symphony" );

// Set the attributes.
$xml_track->setAttribute( "length", "0:01:15" );
$xml_track->setAttribute( "bitrate", "64kb/s" );
$xml_track->setAttribute( "channels", "2" );

// Create another element, just to show you can add any (realistic to computer) number of sublevels.
$xml_note = $xml->createElement( "Note", "The last symphony composed by Ludwig van Beethoven." );

// Append the whole bunch.
$xml_track->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );

// Repeat the above with some different values..
$xml_track = $xml->createElement( "Track", "Highway Blues" );

$xml_track->setAttribute( "length", "0:01:33" );
$xml_track->setAttribute( "bitrate", "64kb/s" );
$xml_track->setAttribute( "channels", "2" );
$xml_album->appendChild( $xml_track );

$xml->appendChild( $xml_album );

// Parse the XML.
print $xml->saveXML();

?>

Output:
<Album>
  <Track length="0:01:15" bitrate="64kb/s" channels="2">
    The ninth symphony
    <Note>
      The last symphony composed by Ludwig van Beethoven.
    </Note>
  </Track>
  <Track length="0:01:33" bitrate="64kb/s" channels="2">Highway Blues</Track>
</Album>

If you want your PHP->DOM code to run under the .xml extension, you should set your webserver up to run the .xml extension with PHP ( Refer to the installation/configuration configuration for PHP on how to do this ).

Note that this:
<?php
$xml = new DOMDocument( "1.0", "ISO-8859-15" );
$xml_album = $xml->createElement( "Album" );
$xml_track = $xml->createElement( "Track" );
$xml_album->appendChild( $xml_track );
$xml->appendChild( $xml_album );
?>

is NOT the same as this:
<?php
// Will NOT work.
$xml = new DOMDocument( "1.0", "ISO-8859-15" );
$xml_album = new DOMElement( "Album" );
$xml_track = new DOMElement( "Track" );
$xml_album->appendChild( $xml_track );
$xml->appendChild( $xml_album );
?>

although this will work:
<?php
$xml = new DOMDocument( "1.0", "ISO-8859-15" );
$xml_album = new DOMElement( "Album" );
$xml->appendChild( $xml_album );
?>


Creates a new DOMDocument object

  • ≪ DOMDocument
  • DOMDocument::createAttribute ≫
  • PHP Manual
  • DOMDocument
  • Creates a new DOMDocument object

DOMDocument::__construct

(PHP 5, PHP 7)

DOMDocument::__construct — Creates a new DOMDocument object

설명

public DOMDocument::__construct ([ string $version [, string $encoding ]] )

Creates a new DOMDocument object.

인수

version

The version number of the document as part of the XML declaration.

encoding

The encoding of the document as part of the XML declaration.

예제

Example #1 Creating a new DOMDocument

<?php

$dom = new DOMDocument('1.0', 'iso-8859-1');

echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?> */

?>

참고

  • DOMImplementation::createDocument() - Creates a DOMDocument object of the specified type with its document element

User Contributed Notes

qrworld.net 12-Nov-2014 01:36
In this post http://softontherocks.blogspot.com/2014/11/descargar-el-contenido-de-una-url_11.html I found a simple way to get the content of a URL with DOMDocument, loadHTMLFile and saveHTML().

function getURLContent($url){
    $doc = new DOMDocument;
    $doc->preserveWhiteSpace = FALSE;
    @$doc->loadHTMLFile($url);
    return $doc->saveHTML();
}
_ michael 01-Jun-2010 04:23
The constuctor arguments are useful if you want to build a new document using createElement, appendChild etc.

By contrast, these arguments are overriden as soon as you load a document from source by calling load() or loadXML().

* If the source contains an XML declaration specifying an encoding, that encoding is used.
* If the XML declaration does not specify an encoding, or if the source does not contain a declaration at all, UTF-8 is assumed.

This behaviour applies no matter what you declared when you called new DOMDocument().
blackhailang at gmail dot com 10-Oct-2008 04:54
domdocument::domdocument() expects at least
At the least, I found that It due to ZEND optimizer, uninstall it,working well, but the speeds will be slowlly :-(.

Comment :
item 1 : 2008-10-03 17:10:58, gkrong said:
"Warning: domdocument::domdocument() expects at least 1
 parameter"
If you use PHP 5 in windows, you don't need to declare
php_domxml.dll in your php.ini file.
so u can give comment in the line php_domxml.dll in your
php.ini file.
you only need to comment it out, but do not delete the
php_domxml.dll file in the ext directory.
steve at mrclay dot org 08-Oct-2008 06:45
To expand on bholbrook's comment, if you receive this: "Warning: domdocument::domdocument() expects at least 1 parameter", it is due to the old domxml extension, which you need to disable.

domxml overwrites DOMDocument::_construct with an alias to domxml_open_mem, so this code:
<?php
$doc = new DOMDocument();
?>
...essentially does this:
<?php
$dom = domxml_open_mem();
?>
...which is why PHP complains about expecting at least 1 parameter (it expects a string of XML).
christian dot reinecke at web dot de 16-Jul-2008 04:09
If you get the error message "domdocument::domdocument() expects parameter 2 to be long, string given"  for a code sample like this:

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->xmlStandalone = false;
echo $dom->saveXML();

which is obviously correct if you compare the constructor signature:

__construct  ([ string $version  [, string $encoding  ]] )

make sure you're not overwritting this dom library by another (f.e. extension=php_domxml.dll in php.ini). XAMPP f.e. delivers its standard version with php_domxml.dll which ends up in this error message
dave at revlob dot com 14-Feb-2008 11:07
@Jarl

Not sure if this is what you meant when you said "The constructor example clearly shows that version and encoding only end up in the XML header", but you can also affect other parameters in the generated XML header, by accessing the DOMDocument's properties, for example:

<?php

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->xmlStandalone = false;
echo $dom->saveXML();

// <?xml version="1.0" encoding="UTF-8" standalone="no"?>
bholbrook at servillian dot com 05-Dec-2007 11:25
Make sure that php_domxml.dll on windows is removed before using the domdocument class as they cannot coexist.
jarl kringelding skim punt com 24-Sep-2007 11:22
Be aware using the encoding parameter in the constructor.
It does not mean that all data is automatically encoded for you in the supplied encoding. You need to do that yourself once you choose an encoding other than the default UTF-8. See the note on DOM Functions on how to properly work with other encodings...

The constructor example clearly shows that version and encoding only end up in the XML header.


Create new attribute

  • ≪ DOMDocument::__construct
  • DOMDocument::createAttributeNS ≫
  • PHP Manual
  • DOMDocument
  • Create new attribute

DOMDocument::createAttribute

(PHP 5, PHP 7)

DOMDocument::createAttribute — Create new attribute

설명

public DOMAttr DOMDocument::createAttribute ( string $name )

This function creates a new instance of class DOMAttr. 이 노드는 DOMNode::appendChild() 등을 통하여 삽입하지 않으면 보여지지 않습니다.

인수

name

The name of the attribute.

반환값

The new DOMAttr or FALSE if an error occurred.

오류/예외

DOM_INVALID_CHARACTER_ERR

Raised if name contains an invalid character.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMDocument::createAttributeNS() - Create new attribute node with an associated namespace
  • DOMDocument::createCDATASection() - Create new cdata node
  • DOMDocument::createComment() - Create new comment node
  • DOMDocument::createDocumentFragment() - Create new document fragment
  • DOMDocument::createElement() - Create new element node
  • DOMDocument::createElementNS() - Create new element node with an associated namespace
  • DOMDocument::createEntityReference() - Create new entity reference node
  • DOMDocument::createProcessingInstruction() - Creates new PI node
  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

Skateside 06-Apr-2013 01:16
This may be obvious to most people, but it gave me a few minutes of head-scratching before I figured it out, but ampersands need to be properly escaped as HTML entities.

<?php
$doc= new DOMDocument('1.0', "UTF-8");

$invalid_elem = $doc->createElement('field', 'incorrect attribute');
$invalid_attr = $doc->createAttribute('name');
$invalid_attr->value = 'foo&bar';
$invalid_elem->appendChild($invalid_attr);
$doc->appendChild($invalid_elem);

$valid_elem = $doc->createElement('field', 'correct attribute');
$valid_attr = $doc->createAttribute('name');
$valid_attr->value = 'foo&amp;bar';
$valid_elem->appendChild($valid_attr);
$doc->appendChild($valid_elem);

?>

Will output:
<?xml version="1.0" encoding="UTF-8"?>
<field name="foo">incorrect attribute</field>
<field name="foo&bar">correct attribute</field>

As near as I can figure out, no exception is thrown for an invalid attribute. This will affect DOMDocuments created as HTML as well as XML.
Beerkeeper 09-Nov-2011 04:19
Just in case it isn't clear (like I had), an example:

<?php

$domDocument = new DOMDocument('1.0', "UTF-8");
$domElement = $domDocument->createElement('field','some random data');
$domAttribute = $domDocument->createAttribute('name');

// Value for the created attribute
$domAttribute->value = 'attributevalue';

// Don't forget to append it to the element
$domElement->appendChild($domAttribute);

// Append it to the document itself
$domDocument->appendChild($domElement);

?>

Will output:
<?xml version="1.0" encoding="UTF-8"?>
<field name="attributevalue">some random data</field>


Create new attribute node with an associated namespace

  • ≪ DOMDocument::createAttribute
  • DOMDocument::createCDATASection ≫
  • PHP Manual
  • DOMDocument
  • Create new attribute node with an associated namespace

DOMDocument::createAttributeNS

(PHP 5, PHP 7)

DOMDocument::createAttributeNS — Create new attribute node with an associated namespace

설명

public DOMAttr DOMDocument::createAttributeNS ( string $namespaceURI , string $qualifiedName )

This function creates a new instance of class DOMAttr. 이 노드는 DOMNode::appendChild() 등을 통하여 삽입하지 않으면 보여지지 않습니다.

인수

namespaceURI

The URI of the namespace.

qualifiedName

The tag name and prefix of the attribute, as prefix:tagname.

반환값

The new DOMAttr or FALSE if an error occurred.

오류/예외

DOM_INVALID_CHARACTER_ERR

Raised if qualifiedName contains an invalid character.

DOM_NAMESPACE_ERR

Raised if qualifiedName is a malformed qualified name, or if qualifiedName has a prefix and namespaceURI is NULL.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMDocument::createAttribute() - Create new attribute
  • DOMDocument::createCDATASection() - Create new cdata node
  • DOMDocument::createComment() - Create new comment node
  • DOMDocument::createDocumentFragment() - Create new document fragment
  • DOMDocument::createElement() - Create new element node
  • DOMDocument::createElementNS() - Create new element node with an associated namespace
  • DOMDocument::createEntityReference() - Create new entity reference node
  • DOMDocument::createProcessingInstruction() - Creates new PI node
  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

_ michael 01-Jun-2010 04:35
If a new namespace is introduced while creating and inserting an attribute, createAttributeNS() does not behave in the same way as createElementNS().

(1) Location: With createAttributeNS(), the new namespace is declared at the level of the document element. By contrast, createElementNS() declares the new namespace at the level of the affected element itself.

(2) Timing: With createAttributeNS(), the new namespace is declared in the document as soon as the attribute is created - the attribute does not actually have to be inserted. createElementNS() doesn't affect the document as long as the element is not inserted.

An example:

<?php

    $source = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<root><tag></tag></root>
XML;

    /*

     I. createAttributeNS:
     * a new namespace shows up immediately, even without insertion of the attribute
     * the new namespace is declared at the level of the document element

    */

    $doc = new DOMDocument( '1.0' );
    $doc->loadXML( $source );

    // (1) We just create a "namespace'd" attribute without appending it to any element.
    $attr_ns = $doc->createAttributeNS( '{namespace_uri_here}', 'example:attr' );

    print $doc->saveXML() . "\n";

    /*
      Result: The namespace declaration appears, having been added to the document element. Output:

      <?xml version="1.0" encoding="UTF-8"?>
      <root xmlns:example="{namespace_uri_here}"><tag/></root>

    */

    // (2) Next, we give the attribute a value and insert it.
    $attr_ns->value = 'value';
    $doc->getElementsByTagName( 'tag' )->item(0)->appendChild( $attr_ns );

    print $doc->saveXML() . "\n";

    /*
      Result: The "namespace'd" attribute shows up as well. Output:

      <?xml version="1.0" encoding="UTF-8"?>
      <root xmlns:example="{namespace_uri_here}"><tag example:attr="value"/></root>

    */

    /*

     II. createElementNS:
     * a new namespace shows up only when the element is inserted
     * the new namespace is declared at the level of the inserted element

    */

    $doc = new DOMDocument( '1.0' );
    $doc->loadXML( $source );

    // (1) We create a "namespace'd" element without inserting it into the document.
    $elem_ns = $doc->createElementNS( '{namespace_uri_here}', 'example:newtag' );

    print $doc->saveXML() . "\n";

    /*
      Result: The document remains unchanged. Output:

      <?xml version="1.0" encoding="UTF-8"?>
      <root><tag/></root>

    */

    // (2) Next, we insert the new element.
    $doc->getElementsByTagName( 'tag' )->item(0)->appendChild( $elem_ns );

    print $doc->saveXML() . "\n";

    /*
      Result: The namespace declaration appears, and it is embedded in the element using it. Output:

      <?xml version="1.0" encoding="UTF-8"?>
      <root><tag><example:newtag xmlns:example="{namespace_uri_here}"/></tag></root>

    */

?>


Create new cdata node

  • ≪ DOMDocument::createAttributeNS
  • DOMDocument::createComment ≫
  • PHP Manual
  • DOMDocument
  • Create new cdata node

DOMDocument::createCDATASection

(PHP 5, PHP 7)

DOMDocument::createCDATASection — Create new cdata node

설명

public DOMCDATASection DOMDocument::createCDATASection ( string $data )

This function creates a new instance of class DOMCDATASection. 이 노드는 DOMNode::appendChild() 등을 통하여 삽입하지 않으면 보여지지 않습니다.

인수

data

The content of the cdata.

반환값

The new DOMCDATASection or FALSE if an error occurred.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMDocument::createAttribute() - Create new attribute
  • DOMDocument::createAttributeNS() - Create new attribute node with an associated namespace
  • DOMDocument::createComment() - Create new comment node
  • DOMDocument::createDocumentFragment() - Create new document fragment
  • DOMDocument::createElement() - Create new element node
  • DOMDocument::createElementNS() - Create new element node with an associated namespace
  • DOMDocument::createEntityReference() - Create new entity reference node
  • DOMDocument::createProcessingInstruction() - Creates new PI node
  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

Marc info[at]braincast.nl 08-Mar-2011 10:55
Here's some code that takes an associative array and prints it asXML() but creates CDATA sections for each string

<?php
class SimpleXMLExtended extends SimpleXMLElement{
    public function addCData($string){
        $dom = dom_import_simplexml($this);
        $cdata = $dom->ownerDocument->createCDATASection($string);
        $dom->appendChild($cdata);
    }
}
function assocArrayToXML($root_element_name,$ar){
    $xml = new SimpleXMLExtended("<?xml version=\"1.0\"?><{$root_element_name}></{$root_element_name}>");
    $f = create_function('$f,$c,$a','
            foreach($a as $k=>$v) {
                if(is_array($v)) {
                    if (!is_numeric($k))$ch=$c->addChild($k);
                    else $ch = $c->addChild(substr($c->getName(),0,-1));
                    $f($f,$ch,$v);
                } else {
                    if (is_numeric($v)){ $c->addChild($k, $v);
                    }else{$n = $c->addChild($k); $n->addCData($v);}
                }
            }');
    $f($f,$xml,$ar);
    return $xml->asXML();
}

/* sample */
$result = array("title"=>"CDATA Sample");
$result['items'] = array();
$result['items'][] = array('title'=>'Some string', 'number' => 1);
$result['items'][] = array('title'=>'Some string', 'number' => 2);
$result['items'][] = array('title'=>'Some string', 'number' => 3);

echo assocArrayToXML('result',$result);
?>

The is_numeric check could be changed by a more elaborate regular expression to check if the string is actually xml unsafe but this worked for me.
jesdisciple dot FOO at gmail dot BAR dot com 08-Jul-2010 01:44
If you would like to refer to the documentation for the class of the returned object, see http://www.php.net/manual/en/class.domcharacterdata.php
info at troptoek dot com 25-Mar-2008 06:40
A common issue seems to be adding javascript to CDATA and the browser throwing a javascript error. To ensure the javascript works use the following code when adding CDATA:

<?php
/**
 * Append Caracter Data to a node and check for a javascript node
 *
 * @param DOMElement $appendToNode
 * @param string $text
 */
function appendCdata($appendToNode, $text)
{
    if (strtolower($appendToNode->nodeName) == 'script') {  // Javascript hack
        $cm = $appendToNode->ownerDocument->createTextNode("\n//");
        $ct = $appendToNode->ownerDocument->createCDATASection("\n" . $text . "\n//");
        $appendToNode->appendChild($cm);
        $appendToNode->appendChild($ct);
    } else {  // Normal CDATA node
        $ct = $appendToNode->ownerDocument->createCDATASection($text);
        $appendToNode->appendChild($ct);
    }
}
?>
The result should be:

<script type="text/javascript">
//<![CDATA[
function someJsText() {
   document.write('Some js with <a href="#">HTML</a> content');
}
//]]></script>
loathsome 04-Aug-2007 11:28
Here's a function that will create a CDATA-section around a string coming from SimpleXML.

<?php
function sxml_cdata($path, $string){
  $dom = dom_import_simplexml($path);
  $cdata = $dom->ownerDocument->createCDATASection($string);
  $dom->appendChild($cdata);
}
?>


Create new comment node

  • ≪ DOMDocument::createCDATASection
  • DOMDocument::createDocumentFragment ≫
  • PHP Manual
  • DOMDocument
  • Create new comment node

DOMDocument::createComment

(PHP 5, PHP 7)

DOMDocument::createComment — Create new comment node

설명

public DOMComment DOMDocument::createComment ( string $data )

This function creates a new instance of class DOMComment. 이 노드는 DOMNode::appendChild() 등을 통하여 삽입하지 않으면 보여지지 않습니다.

인수

data

The content of the comment.

반환값

The new DOMComment or FALSE if an error occurred.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMDocument::createAttribute() - Create new attribute
  • DOMDocument::createAttributeNS() - Create new attribute node with an associated namespace
  • DOMDocument::createCDATASection() - Create new cdata node
  • DOMDocument::createDocumentFragment() - Create new document fragment
  • DOMDocument::createElement() - Create new element node
  • DOMDocument::createElementNS() - Create new element node with an associated namespace
  • DOMDocument::createEntityReference() - Create new entity reference node
  • DOMDocument::createProcessingInstruction() - Creates new PI node
  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

php dot sirlancelot at spamgourmet dot com 25-Nov-2009 09:10
To prevent a parser error when the comment string would contain the character sequence "--", do this:

<?php

$CommentString = 'This contains -- some weird -- characters.';

$CommentNode = $DomDocument->createComment(
        str_replace('--', '-'.chr(194).chr(173).'-', $CommentString)
);
?>

This will insert a Soft Hyphen in between the two hyphens which will not cause the parser to error out.
capps at solareclipse dot net 30-Mar-2007 12:55
Use caution: this function does NOT escape, encode, or otherwise change the contents of the string.

This means that if your string contains two hyphens in a row (--), that will *end* the comment, which can easily create invalid XML when the document is serialized.


Create new document fragment

  • ≪ DOMDocument::createComment
  • DOMDocument::createElement ≫
  • PHP Manual
  • DOMDocument
  • Create new document fragment

DOMDocument::createDocumentFragment

(PHP 5, PHP 7)

DOMDocument::createDocumentFragment — Create new document fragment

설명

public DOMDocumentFragment DOMDocument::createDocumentFragment ( void )

This function creates a new instance of class DOMDocumentFragment. 이 노드는 DOMNode::appendChild() 등을 통하여 삽입하지 않으면 보여지지 않습니다.

반환값

The new DOMDocumentFragment or FALSE if an error occurred.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMDocument::createAttribute() - Create new attribute
  • DOMDocument::createAttributeNS() - Create new attribute node with an associated namespace
  • DOMDocument::createCDATASection() - Create new cdata node
  • DOMDocument::createComment() - Create new comment node
  • DOMDocument::createElement() - Create new element node
  • DOMDocument::createElementNS() - Create new element node with an associated namespace
  • DOMDocument::createEntityReference() - Create new entity reference node
  • DOMDocument::createProcessingInstruction() - Creates new PI node
  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

There are no user contributed notes for this page.


Create new element node

  • ≪ DOMDocument::createDocumentFragment
  • DOMDocument::createElementNS ≫
  • PHP Manual
  • DOMDocument
  • Create new element node

DOMDocument::createElement

(PHP 5, PHP 7)

DOMDocument::createElement — Create new element node

설명

public DOMElement DOMDocument::createElement ( string $name [, string $value ] )

This function creates a new instance of class DOMElement. 이 노드는 DOMNode::appendChild() 등을 통하여 삽입하지 않으면 보여지지 않습니다.

인수

name

The tag name of the element.

value

The value of the element. By default, an empty element will be created. The value can also be set later with DOMElement::$nodeValue.

The value is used verbatim except that the < and > entity references will escaped. Note that & has to be manually escaped; otherwise it is regarded as starting an entity reference. Also " won't be escaped.

반환값

Returns a new instance of class DOMElement or FALSE if an error occurred.

오류/예외

DOM_INVALID_CHARACTER_ERR

Raised if name contains an invalid character.

예제

Example #1 Creating a new element and inserting it as root

<?php

$dom = new DOMDocument('1.0', 'utf-8');

$element = $dom->createElement('test', 'This is the root element!');

// We insert the new element as root (child of the document)
$dom->appendChild($element);

echo $dom->saveXML();
?>

위 예제의 출력:

<?xml version="1.0" encoding="utf-8"?>
<test>This is the root element!</test>

Example #2 Passing text containing an unescaped & as value

<?php
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->createElement('foo', 'me & you');
$dom->appendChild($element);
echo $dom->saveXML();
?>

위 예제의 출력 예시:

Warning: DOMDocument::createElement(): unterminated entity reference             you in /in/BjTCg on line 4
<?xml version="1.0" encoding="utf-8"?>
<foo/>

주의

Note:

The value will not be escaped. Use DOMDocument::createTextNode() to create a text node with escaping support.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMDocument::createAttribute() - Create new attribute
  • DOMDocument::createAttributeNS() - Create new attribute node with an associated namespace
  • DOMDocument::createCDATASection() - Create new cdata node
  • DOMDocument::createComment() - Create new comment node
  • DOMDocument::createDocumentFragment() - Create new document fragment
  • DOMDocument::createElementNS() - Create new element node with an associated namespace
  • DOMDocument::createEntityReference() - Create new entity reference node
  • DOMDocument::createProcessingInstruction() - Creates new PI node
  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

tschmieder at bitworks dot de 25-Mar-2015 02:42
Remember:

If you want to perform multiple actions with a new node, you may need to create a copy of it before

means:
## Create an address to an unique memory block !
$td = $dom->createElement('td');
## Change some things in this original unique pattern
$td->setAttribute('class', 'saldo');

## clone the unique pattern to two own one's
$td1 = clone $td;
$td2 = clone $td;

## alter properties in each one
$td1->nodeValue = 'Ich bin die erste neue Node';
$td2->nodeValue = 'Ich bin die zweite neue Node';

## find the parent element
$tr = $dom->getElementById('t001-tr001');
## find the first and the last child (here only for clearity)
$first = $tr->firstChild;
$last = $tr->lastChild;

## produce the new nodes
$newtd1 = $tr->insertBefore($td1, $first);
$newtd2 = $tr->appendChild($td2);

conclusion:
YOU NEED AN ORIGINAL NEW NODE FOR EACH ACTION!
funkathustra 04-Mar-2012 05:44
Although the built-in DOM functions are great, since they're designed to support generic XML, generating HTML DOMs becomes particularly verbose. I ended up writing this function to drastically speed things up.
Instead of calling something like
<?php
  $div = $dom->createElement("div");
  $div->setAttribute("class","MyClass");
  $div->setAttribute("id","MyID");
  $someOtherDiv->appendChild($div);
?>
you can accomplish the same thing with:
<?php
  $div = newElement("div", $someOtherDiv, "class=MyClass;id=MyID");
?>
The "key1=value;key2=value" syntax is really fast to use, but obviously doesn't hold up if your content has those characters in it. So, you can also pass it an array:
<?php
  $div = newElement("div", $someOtherDiv, array("class","MyClass"));
?>
Or an array of arrays, representing different attributes:
<?php
  $div = newElement("form", $someOtherDiv, array(array("method","get"), array("action","/refer/?id=5");
?>

Here's the function:

<?php
function newElement($type, $insertInto = NULL, $params=NULL, $content="")
    {
        $tempEl =  $this->dom->createElement($type, $content);
        if(gettype($params) == "string" && strlen($params) > 0)
        {
            $attributesCollection =split(";", $params);
            foreach($attributesCollection  as $attribute)
            {
                $keyvalue = split("=", $attribute);
                $tempEl->setAttribute($keyvalue[0], $keyvalue[1]);
            }
        }
        if(gettype($params) == "array")
        {
            if(gettype($params[0]) == "array")
            {
                foreach($params as $attribute)
                {
                    $tempEl->setAttribute($attribute[0], $attribute[1]);
                }
            } else {
                $tempEl->setAttribute($params[0], $params[1]);
            }
        }
?>
lars dot c dot magnusson at gmail dot com 26-Nov-2010 10:56
You may think insertBefore and insertAfter is a direct alternative for appendChild, this is not the case.

<?php
$dom = new DOMDocument();
$dom->load($file);

$dom->appendChild($newNode); //Works fine
$dom->insertBefore($newNode, $refNode); //Will fail

$refNode->parentNode->insertBefore($newNode, $refNode); // thanx to yasindagli (first post)
?>
yasindagli at gmail dot com 17-Jun-2009 11:01
To create elements with attributes,

<?php

function createElement($domObj, $tag_name, $value = NULL, $attributes = NULL)
{
    $element = ($value != NULL ) ? $domObj->createElement($tag_name, $value) : $domObj->createElement($tag_name);

    if( $attributes != NULL )
    {
        foreach ($attributes as $attr=>$val)
        {
            $element->setAttribute($attr, $val);
        }
    }

    return $element;
}

$dom = new DOMDocument('1.0', 'utf-8');

$elm = createElement($dom, 'foo', 'bar', array('attr_name'=>'attr_value'));

$dom->appendChild($elm);

echo $dom->saveXML();

?>

outputs :
<?xml version="1.0" encoding="utf-8"?>
<foo attr_name="attr_value">bar</foo>
chris AT cmbuckley DOT co DOT uk 26-May-2009 04:45
Note that the NUL character "\0" is not in the list of invalid characters for $name, so no error is triggered, but the tag name will be truncated at the null byte:

<?php

$dom = new DOMDocument('1.0', 'utf-8');
$el = $dom->createElement('foo' . "\0" . 'bar', 'Hello World');
echo $el->tagName; // outputs "foo"

?>
estill at gvtc dot com 12-Jun-2007 11:38
Note that the second parameter (value), although convenient, is non-standard. You should create elements like this instead:

<?php
$doc = new DOMDocument('1.0', 'iso-8859-1');

$root = $doc->createElement('test');
$doc->appendChild($root);

$root_text = $doc->createTextNode('This is the root element!');
$root->appendChild($root_text);

print $doc->saveXML();
?>

Or, alternatively, extend the DOMDocument class and add your own custom, convenience method to avoid intruding on the standard:

<?php
class CustomDOMDocument extends DOMDocument {
  function createElementWithText($name, $child_text) {
    // Creates an element with a child text node

    // @param  string  $name        element tag name
    // @param  string  $child_text  child node text

    // @return  object  new element

    $element = $this->createElement($name);

    $element_text = $this->createTextNode($child_text);
    $element->appendChild($element_text);

    return $element;
  }
}

$doc = new CustomDOMDocument('1.0', 'iso-8859-1');

$root = $doc->createElementWithText('test', 'This is the root element!');
$doc->appendChild($root);

print $doc->saveXML();
?>

Also use caution with (or avoid) the 'DOMElement->nodeValue' property. It can return some unexpected values and changing its value will replace (remove) all descendants of the element with a single text node. It's also non-standard; according to the DOM spec it should return NULL.
mikek dot nospam at nospam dot muonics dot com 04-Mar-2007 02:24
With regard to the note below about needing htmlentities to avoid warnings about unterminated entity references, I thought it worthwhile to mention that that you don't need to with createTextNode and DOMText::__construct.  If you mix both methods of setting text nodes and do (or don't) apply htmlentities consistently to all data to be displayed, you'll get &amp;s (or warnings and badly-formed xml).

It's probably in one's best interest to extend DOMElement and DOMDocument so that it creates a DOMText node and appends it, rather than passing it up to the DOMElement constructor.  Otherwise, good luck using (or not using) htmlentities in all the right places in your code, especially as code changes get made.

<?php

class XDOMElement extends DOMElement {
    function __construct($name, $value = null, $namespaceURI = null) {
        parent::__construct($name, null, $namespaceURI);
    }
}

class XDOMDocument extends DOMDocument {
    function __construct($version = null, $encoding = null) {
        parent::__construct($version, $encoding);
        $this->registerNodeClass('DOMElement', 'XDOMElement');
    }

    function createElement($name, $value = null, $namespaceURI = null) {
        $element = new XDOMElement($name, $value, $namespaceURI);
        $element = $this->importNode($element);
        if (!empty($value)) {
            $element->appendChild(new DOMText($value));
        }
        return $element;
    }
}

$doc1 = new XDOMDocument();
$doc1_e1 = $doc1->createElement('foo', 'bar & baz');
$doc1->appendChild($doc1_e1);
echo $doc1->saveXML();

$doc2 = new XDOMDocument();
$doc2_e1 = $doc2->createElement('foo');
$doc2->appendChild($doc2_e1);
$doc2_e1->appendChild($doc2->createTextNode('bar & baz'));
echo $doc2->saveXML();

?>

Text specified in createElement:
<?xml version=""?>
<foo>bar &amp; baz</foo>

Text added via createTextNode:
<?xml version=""?>
<foo>bar &amp; baz</foo>
sergsokolenko at gmail dot com 30-Jan-2007 01:11
To avoid warning message "unterminated entity reference" you may use htmlentities() for escaping supplied value:
<?php
//...
$dom->createElement('name', htmlentities($text))
//...
?>


Create new element node with an associated namespace

  • ≪ DOMDocument::createElement
  • DOMDocument::createEntityReference ≫
  • PHP Manual
  • DOMDocument
  • Create new element node with an associated namespace

DOMDocument::createElementNS

(PHP 5, PHP 7)

DOMDocument::createElementNS — Create new element node with an associated namespace

설명

public DOMElement DOMDocument::createElementNS ( string $namespaceURI , string $qualifiedName [, string $value ] )

This function creates a new element node with an associated namespace. 이 노드는 DOMNode::appendChild() 등을 통하여 삽입하지 않으면 보여지지 않습니다.

인수

namespaceURI

The URI of the namespace.

qualifiedName

The qualified name of the element, as prefix:tagname.

value

The value of the element. By default, an empty element will be created. You can also set the value later with DOMElement::$nodeValue.

반환값

The new DOMElement or FALSE if an error occurred.

오류/예외

DOM_INVALID_CHARACTER_ERR

Raised if qualifiedName contains an invalid character.

DOM_NAMESPACE_ERR

Raised if qualifiedName is a maformed qualified name.

예제

Example #1 Creating a new element and inserting it as root

<?php

$dom = new DOMDocument('1.0', 'utf-8');

$element = $dom->createElementNS('http://www.example.com/XFoo', 'xfoo:test', 'This is the root element!');

// We insert the new element as root (child of the document)
$dom->appendChild($element);

echo $dom->saveXML();
?>

위 예제의 출력:

<?xml version="1.0" encoding="utf-8"?>
<xfoo:test xmlns:xfoo="http://www.example.com/XFoo">This is the root element!</xfoo:test>

Example #2 A namespace prefix example

<?php
$doc  = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$root = $doc->createElementNS('http://www.w3.org/2005/Atom', 'element');
$doc->appendChild($root);
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:g', 'http://base.google.com/ns/1.0');
$item = $doc->createElementNS('http://base.google.com/ns/1.0', 'g:item_type', 'house');
$root->appendChild($item);

echo $doc->saveXML(), "\n";

echo $item->namespaceURI, "\n"; // Outputs: http://base.google.com/ns/1.0
echo $item->prefix, "\n";       // Outputs: g
echo $item->localName, "\n";    // Outputs: item_type
?>

위 예제의 출력:

<?xml version="1.0" encoding="utf-8"?>
<element xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
  <g:item_type>house</g:item_type>
</element>

http://base.google.com/ns/1.0
g
item_type

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMDocument::createAttribute() - Create new attribute
  • DOMDocument::createAttributeNS() - Create new attribute node with an associated namespace
  • DOMDocument::createCDATASection() - Create new cdata node
  • DOMDocument::createComment() - Create new comment node
  • DOMDocument::createDocumentFragment() - Create new document fragment
  • DOMDocument::createElement() - Create new element node
  • DOMDocument::createEntityReference() - Create new entity reference node
  • DOMDocument::createProcessingInstruction() - Creates new PI node
  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

Martin 28-Dec-2011 06:39
To avoid multiple xmlns re-declaration, make sure you appending ElementNS into actual DOMDocument tree (not into some currently-assembed derelict element).


Create new entity reference node

  • ≪ DOMDocument::createElementNS
  • DOMDocument::createProcessingInstruction ≫
  • PHP Manual
  • DOMDocument
  • Create new entity reference node

DOMDocument::createEntityReference

(PHP 5, PHP 7)

DOMDocument::createEntityReference — Create new entity reference node

설명

public DOMEntityReference DOMDocument::createEntityReference ( string $name )

This function creates a new instance of class DOMEntityReference. 이 노드는 DOMNode::appendChild() 등을 통하여 삽입하지 않으면 보여지지 않습니다.

인수

name

The content of the entity reference, e.g. the entity reference minus the leading & and the trailing ; characters.

반환값

The new DOMEntityReference or FALSE if an error occurred.

오류/예외

DOM_INVALID_CHARACTER_ERR

Raised if name contains an invalid character.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMDocument::createAttribute() - Create new attribute
  • DOMDocument::createAttributeNS() - Create new attribute node with an associated namespace
  • DOMDocument::createCDATASection() - Create new cdata node
  • DOMDocument::createComment() - Create new comment node
  • DOMDocument::createDocumentFragment() - Create new document fragment
  • DOMDocument::createElement() - Create new element node
  • DOMDocument::createElementNS() - Create new element node with an associated namespace
  • DOMDocument::createProcessingInstruction() - Creates new PI node
  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

alicewonder at shastaherps dot org 25-Sep-2015 04:48
It appears that this does not work with numbered entities, only named entities.

$nbspace = $dom->createEntityReference('nbsp');

works

$nbspace = $dom->createEntityReference('#160');

does not. This makes this function rather useless when generating an XSL unless you modify the XSL doctype to include the named entity for the character you want.
Tuhin Bepari 04-Nov-2013 03:00
<?php
/*Entity is a group of words which print a special symbol.
Like if we want to show copy right symbol in html page then we use &copy; code and browser convert this to actual copyright symbol.
There have lots of entity, you can find them all form http://dev.w3.org/html5/html-author/charref
if you want to use < or > or both <> into a node value than xml will give and warning or make this value as a node.
So tell the xml parser that < or > is not tag symbol it is a entity.To do that you have to right &lt(<) and &gt;(>) instead of < and > symbol.

Entity references always begin with an ampersand (&) and end with a semicolon (;).
DO not need to use & and ; symbol begin and end of entity.Remove it when you want to use it to DOMDocument::createEntityReference
Then append to to a tag where you want to show this symbol.Like below
*/
$dom=new DOMDocument("1.0","UTF-8");
$example=$dom->createElement("example","This is copyright ");
$entity=$dom->createEntityReference("copy");
$example->appendChild($entity);
$dom->appendChild($example);
echo $dom->saveXML();

output is
This is copyright ©


Creates new PI node

  • ≪ DOMDocument::createEntityReference
  • DOMDocument::createTextNode ≫
  • PHP Manual
  • DOMDocument
  • Creates new PI node

DOMDocument::createProcessingInstruction

(PHP 5, PHP 7)

DOMDocument::createProcessingInstruction — Creates new PI node

설명

public DOMProcessingInstruction DOMDocument::createProcessingInstruction ( string $target [, string $data ] )

This function creates a new instance of class DOMProcessingInstruction. 이 노드는 DOMNode::appendChild() 등을 통하여 삽입하지 않으면 보여지지 않습니다.

인수

target

The target of the processing instruction.

data

The content of the processing instruction.

반환값

The new DOMProcessingInstruction or FALSE if an error occurred.

오류/예외

DOM_INVALID_CHARACTER_ERR

Raised if target contains an invalid character.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMDocument::createAttribute() - Create new attribute
  • DOMDocument::createAttributeNS() - Create new attribute node with an associated namespace
  • DOMDocument::createCDATASection() - Create new cdata node
  • DOMDocument::createComment() - Create new comment node
  • DOMDocument::createDocumentFragment() - Create new document fragment
  • DOMDocument::createElement() - Create new element node
  • DOMDocument::createElementNS() - Create new element node with an associated namespace
  • DOMDocument::createEntityReference() - Create new entity reference node
  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

romain at supinfo dot com 03-Feb-2009 08:57
A use exemple of this method :

Usefull for generating an XML linked with a XSLT !

<?php

// "Create" the document.
$xml = new DOMDocument( "1.0", "ISO-8859-15" );

//to have indented output, not just a line
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;

// ------------- Interresting part here ------------

//creating an xslt adding processing line
$xslt = $xml->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="base.xsl"');

//adding it to the xml
$xml->appendChild($xslt);

// ----------- / Interresting part here -------------

//adding some elements
$root = $xml->createElement("list");
$node = $xml->createElement("contact", "John Doe");
$root-> appendChild($node);
$xml-> appendChild($root);

//creating the file
$xml-> save("output.xml");

?>

output.xml :

<?xml version="1.0" encoding="ISO-8859-15"?>
<?xml-stylesheet type="text/xsl" href="base.xsl"?> //the line has been created successfully
<list>
  <contact>John Doe</contact>
</list>


Create new text node

  • ≪ DOMDocument::createProcessingInstruction
  • DOMDocument::getElementById ≫
  • PHP Manual
  • DOMDocument
  • Create new text node

DOMDocument::createTextNode

(PHP 5, PHP 7)

DOMDocument::createTextNode — Create new text node

설명

public DOMText DOMDocument::createTextNode ( string $content )

This function creates a new instance of class DOMText. 이 노드는 DOMNode::appendChild() 등을 통하여 삽입하지 않으면 보여지지 않습니다.

인수

content

The content of the text.

반환값

The new DOMText or FALSE if an error occurred.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMDocument::createAttribute() - Create new attribute
  • DOMDocument::createAttributeNS() - Create new attribute node with an associated namespace
  • DOMDocument::createCDATASection() - Create new cdata node
  • DOMDocument::createComment() - Create new comment node
  • DOMDocument::createDocumentFragment() - Create new document fragment
  • DOMDocument::createElement() - Create new element node
  • DOMDocument::createElementNS() - Create new element node with an associated namespace
  • DOMDocument::createEntityReference() - Create new entity reference node
  • DOMDocument::createProcessingInstruction() - Creates new PI node

User Contributed Notes

drewish at katherinehouse dot com 17-Dec-2014 07:03
The docs don't mention it but this method performs escaping on the value. If you don't want that use createElement() instead.
Alex Kaye 25-Jul-2009 04:04
A quick note to anyone who is using character entities (e.g. &copy;) in this, and finding them automatically escaped. The correct thing to do here is to use the createEntityReference method (e.g. createEntityReference("copy");), and then appendChild this entity between text nodes.

Example:

<?php
$copyright_text_1 = $this->document->createTextNode("Copyright ");
$copyright_text_2 = $this->document->createTextNode(" Alex Kaye 2009");
$copyright_symbol = $this->document->createEntityReference("copy");
$copyright->appendChild($copyright_text_1);
$copyright->appendChild($copyright_symbol);
$copyright->appendChild($copyright_text_2);
?>


Searches for an element with a certain id

  • ≪ DOMDocument::createTextNode
  • DOMDocument::getElementsByTagName ≫
  • PHP Manual
  • DOMDocument
  • Searches for an element with a certain id

DOMDocument::getElementById

(PHP 5, PHP 7)

DOMDocument::getElementById — Searches for an element with a certain id

설명

public DOMElement DOMDocument::getElementById ( string $elementId )

This function is similar to DOMDocument::getElementsByTagName but searches for an element with a given id.

For this function to work, you will need either to set some ID attributes with DOMElement::setIdAttribute or a DTD which defines an attribute to be of type ID. In the later case, you will need to validate your document with DOMDocument::validate or DOMDocument::$validateOnParse before using this function.

인수

elementId

The unique id value for an element.

반환값

Returns the DOMElement or NULL if the element is not found.

예제

Example #1 DOMDocument::getElementById() Example

다음 예제는 다음 내용의 book.xml을 사용합니다:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books [
  <!ELEMENT books   (book+)>
  <!ELEMENT book    (title, author+, xhtml:blurb?)>
  <!ELEMENT title   (#PCDATA)>
  <!ELEMENT blurb   (#PCDATA)>
  <!ELEMENT author  (#PCDATA)>
  <!ATTLIST books   xmlns        CDATA  #IMPLIED>
  <!ATTLIST books   xmlns:xhtml  CDATA  #IMPLIED>
  <!ATTLIST book    id           ID     #IMPLIED>
  <!ATTLIST author  email        CDATA  #IMPLIED>
]>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<books xmlns="http://books.php/" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <book id="php-basics">
    <title>PHP Basics</title>
    <author email="jim.smith@basics.php">Jim Smith</author>
    <author email="jane.smith@basics.php">Jane Smith</author>
    <xhtml:blurb><![CDATA[
<p><em>PHP Basics</em> provides an introduction to PHP.</p>
]]></xhtml:blurb>
  </book>
  <book id="php-advanced">
    <title>PHP Advanced Programming</title>
    <author email="jon.doe@advanced.php">Jon Doe</author>
  </book>
</books>
<?php

$doc = new DomDocument;

// We need to validate our document before refering to the id
$doc->validateOnParse = true;
$doc->Load('book.xml');

echo "The element whose id is 'php-basics' is: " . $doc->getElementById('php-basics')->tagName . "\n";

?>

위 예제의 출력:

The element whose id is 'php-basics' is: book

참고

  • DOMDocument::getElementsByTagName() - Searches for all elements with given local tag name

User Contributed Notes

ed at edgiardina dot com 13-Oct-2010 08:41
Please note that if your HTML does not contain a doctype declaration, then getElementById will always return null.
paradox_haze at live dot de 02-Mar-2010 07:53
Had some issues with getElementById() while searching for a specific element in a XHTML document.
I wrote a small function what was solving my problem:

<?php
function getElementById($id)
{
    $xpath = new DOMXPath($this->domDocument);
    return $xpath->query("//*[@id='$id']")->item(0);
}
?>
carl2088 at gmail dot com 11-Apr-2009 09:41
From my experience, getElementById seem to work fine without any setups if you have loaded a HTML document. But in order for getElementById to work with a simple XML document that you've "constructed", you have to set up the id with "xml:" prefix and use setIdAttribute on the element you created or it won't work. See following example, hope this will save someone's frustration. If you have loaded the xml file, then all you have to make sure is the ID has a xml: prefix for the attribute.  But if you start to append the XML document, don't forget to setIdAttribute on the id name or those elements or getElementById will return null when you try to find them.

<?php

    /* test.xml
    <?xml version="1.0" encoding="utf-8"?>
    <root>
        <child xml:id="id_xxxxxx" status="partial">
            <sub_child>Some Data</sub_child>
        </child>
    </root>
    */

    $xmlDom = new DOMDocument('1.0', 'utf-8');
    $xmlDom->formatOutput = true;                        // we want a nice output

    // create a root
    $eltRoot = $xmlDom->createElement("root");
    $xmlDom->appendChild($eltRoot);

    $eltChild = $xmlDom->createElement("child");
    $eltRoot->appendChild($eltChild);

    // add a id attribute
    $attr = $xmlDom->createAttribute("xml:id");    // needs xml prefix or getElementById won't work
    $eltChild->appendChild($attr);

    /// create the text node and append to the created element
    $tNode = $xmlDom->createTextNode("id_8120528");
    $attr->appendChild($tNode);
    $eltChild->setIdAttribute("xml:id", true);    // VERY IMPORT or getElementById won't work

    // add a id attribute
    $attr = $xmlDom->createAttribute("status");
    $eltChild->appendChild($attr);

    /// create the text node and append to the created element
    $tNode = $xmlDom->createTextNode("partial");
    $attr->appendChild($tNode);

    // add a subchild
    $eltSub = $xmlDom->createElement("sub_child");
    $eltChild->appendChild($eltSub);

    $tNode = $xmlDom->createTextNode("Some Data");
    $eltSub->appendChild($tNode);

    $id = null;
    $id = $xmlDom->getElementById("id_8120528");

    assert ($id != null);

    $strId = $id->getAttribute("xml:id"); // bug? empty
    $strStatus = $id->getAttribute("status"); // this works!

    assert ($id !=null);

    $xmlDom->save("./_data/test.xml");

    $xmlDom->load("./_data/test.xml"); // reloading fixes the problem

    $nodeRoot = $xmlDom->getElementsByTagName("root");
    if ($nodeRoot->length > 0) {
        $eltRoot = $nodeRoot->item(0);
    }

    assert($eltRoot != null);

    $id = null;
    $id = $xmlDom->getElementById("id_8120528");

    assert ($id != null);

    $strId = $id->getAttribute("xml:id"); // this works now!
    $strStatus = $id->getAttribute("status"); // this works!

?>
guillaume dot crico at gmail dot com 01-Oct-2008 04:55
You don't want to use "xml:id" ?
Here is the relaxNG trick (with a generic schema):
(tested with libxml 2.6.26)

<?php
$doc = new DOMDocument();
$doc->load(...);

$rng = '
<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
        <element>
            <anyName/>
            <ref name="anythingID"/>
        </element>
    </start>
    <define name="anythingID">
        <zeroOrMore>
            <choice>
                <element>
                    <anyName/>
                    <ref name="anythingID"/>
                </element>
                <attribute name="id">
                    <data type="ID"/>
                </attribute>
                <zeroOrMore>
                    <attribute><anyName/></attribute>
                </zeroOrMore>
                <text/>
            </choice>
        </zeroOrMore>
    </define>
</grammar>
';

$doc->relaxNGValidateSource($rng);
var_dump($doc->getElementById('id1'));
?>

Note that ID values must be valid ones :
  - integers do no work!
  - @see http://www.w3.org/TR/REC-xml/#id
  - => (Letter | '_' | ':') ( Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender  )*
simon at somewhere dot com 30-Jan-2007 05:07
SAVE YOURSELF A MAJOR HEADACHE AND A LOT OF SEARCHING THROUGH DOCUMENTATION -

Instead of using $object->setAttribute('id', 'id_name_here')
USE THIS: $object->setAttribute('xml:id', 'id_name_here')

Then, to get the node value: $domDocumentObject->getElementById('id_name_here');

The xml:id attribute should AUTOMATICALLY be defined!!

Woohoo!  That was easy......
jonbarnett at gmail dot com 01-Nov-2006 05:36
If your XML document does not have a DTD that defines the "id" attribute as an ID, then the easiest thing to do is to use XPath->query() to find an element that matches "//[@id='x']"
Tangui dot Le-Pense at laposte dot net 08-Apr-2006 03:00
Validating a document from a DTD so as to use getElementById is sometimes impossible (for example when the head and body elements are not included yet in a XHtml document : the validation failed).
Fortunately, xml:id is supported by this function :)
That may be useful.
http://www.w3.org/TR/xml-id/
20-Dec-2005 04:04
If you're trying to use getElementById with a xml file validated on a xsd file you must first use the schemaValidate function or getElementById will return null
Example:

  $dom = new DomDocument();
  $dom->load("users.xml");
  $dom->schemaValidate("users.xsd");

  $curruser = $dom->getElementById($user->name);
bart at mediawave dot nl 11-Sep-2005 01:46
It seems getElementById works fine without setting validateOnParse to true. Which is nice since setting this to true caused some performance problems with my script.


Searches for all elements with given local tag name

  • ≪ DOMDocument::getElementById
  • DOMDocument::getElementsByTagNameNS ≫
  • PHP Manual
  • DOMDocument
  • Searches for all elements with given local tag name

DOMDocument::getElementsByTagName

(PHP 5, PHP 7)

DOMDocument::getElementsByTagName — Searches for all elements with given local tag name

설명

public DOMNodeList DOMDocument::getElementsByTagName ( string $name )

This function returns a new instance of class DOMNodeList containing all the elements with a given local tag name.

인수

name

The local name (without namespace) of the tag to match on. The special value * matches all tags.

반환값

A new DOMNodeList object containing all the matched elements.

예제

Example #1 Basic Usage Example

<?php
$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<books>
 <book>Patterns of Enterprise Application Architecture</book>
 <book>Design Patterns: Elements of Reusable Software Design</book>
 <book>Clean Code</book>
</books>
XML;

$dom = new DOMDocument;
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('book');
foreach ($books as $book) {
    echo $book->nodeValue, PHP_EOL;
}
?>

위 예제의 출력:

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code

참고

  • DOMDocument::getElementsByTagNameNS() - Searches for all elements with given tag name in specified namespace

User Contributed Notes

rsvvuuren at hotmail dot com 10-Jan-2014 07:00
I was in need of $dom->getElementsByTagName that would hist magic within a contextNode.

Why i needed getElementsByTagName instead of just simple using an xPath->query is because while looping through the returned nodelist, more nodes with the tagName i was looking for where created.

When using getElementsByTagName, the new nodes are "added" to the nodelist i already am looping through.

When using an xpath query, you wil only loop through the original nodelist, the newly created elements wil not appear in that nodelist.

I was already using an extended class on domDocument, so it was simple to create an kind of getElementsByTagName that would accept an contextNode.

<?php
class SmartDocument extends DOMDocument {
    private $localDom;
    public $xpath;
    private $serialize = array('localDom');

    private $elemName;
    private $elemCounter;

    /**
     * Constructor
     */
    function __construct() {
        parent::__construct ( '1.0', 'UTF-8' );
        $this->preserveWhiteSpace = false;
        $this->recover = TRUE;
        $this->xpath = new DOMXpath ( $this );
    }

    /**
     * GetElementsByTagname within an contextNode
     *
     * @param string $name
     * @param DomNode $contextNode
     * @return DOMNode|NULL
     */
    public function getElementsByTagNameContext($name, $contextNode) {

        if($this->elemName!=$name) {
            $this->elemCounter     = 0;
            $this->elemName        =$name;
        }

        $this->elemLength      = $this->xpath->evaluate('count(.//*[name()="'.$this->elemName.'"])', $contextNode);
        while($this->elemCounter < $this->elemLength) {
            $this->elemCounter++;
            $nl = $this->xpath->query('.//*[name()="'.$this->elemName.'"]['.$this->elemCounter.']', $contextNode);
            if($nl->length == 1) {
                return $nl->item(0);
            }
        }

        $this->elemLength      = null;
        $this->elemCounter     = null;
        $this->elemName        = null;
        return null;
    }
}

?>

Usage:

<?php

 $doc = new SmartDocument();
 $doc->load('book.xml');

 $nl = $doc->query('//books');
 foreach($nl as $node) {
  while($book = $doc->getElementsByTagNameContext('book', $node)) {
   //When you now create new nodes within this loop as child or following-sibling of this node
  // They show up within this loop
  }

 }

?>
simon at onepointltd dot com 10-Sep-2013 04:54
Here is a function to convert the populated tables in and HTML document into an array.

<?php
//Create an array containing all the populated tables in an HTML page
//Returned array: tables_to_array[table number][row number][column number]
function tables_to_array ($url) {
  $htmlDocDom = new DOMDocument();

  @$htmlDocDom->loadHTMLFile($url);
  $htmlDocDom->preserveWhiteSpace = false;
  $tableCounter = 0;
  $htmlDocTableArray = array();
  $htmlDocTables = $htmlDocDom->getElementsByTagName('table');
  foreach ($htmlDocTables as $htmlDocTable) {
    $htmlDocTableArray[$tableCounter] = array();
    $htmlDocRows= $htmlDocTable->getElementsByTagName('tr');
    $htmlDocRowCount = 0;
    $htmlDocTableArray[$tableCounter] = array();
    foreach ($htmlDocRows as $htmlDocRow) {
      if (strlen($htmlDocRow->nodeValue) > 1)
      {
        $htmlDocColCount = 0;
        $htmlDocTableArray[$tableCounter][$htmlDocRowCount] = array();
        $htmlDocCols = $htmlDocRow->getElementsByTagName('td');
        foreach ($htmlDocCols as $htmlDocCol) {
          $htmlDocTableArray[$tableCounter][$htmlDocRowCount][] = $htmlDocCol->nodeValue;
          $htmlDocColCount++;
        }
        $htmlDocRowCount++;
      }
    }
    if ($htmlDocRowCount > 1) $tableCounter++;
  }
  return($htmlDocTableArray);
}
?>
yaakov dot moddel at gmail dot com 29-Aug-2012 07:47
This is a very simplistic way to traverse xml nodes and childnodes using the DOMDocument class

<?php
$xml ='<?xml version="1.0" encoding="utf-8"?>
<root>
    <Parent>
        <child>child 1</child>
        <child>child 2</child>
        <child>child 3</child>
        <subParent>
            <Grandchild>Grandchild 1</Grandchild>
            <Grandchild>Grandchild 2</Grandchild>
            <Grandchild>Grandchild 3</Grandchild>
        </subParent>
    </Parent>
    <Parent>
        <child>child 4</child>
        <child>child 5</child>
        <child>child 6</child>
        <subParent>
            <Grandchild>Grandchild 4</Grandchild>
            <Grandchild>Grandchild 5</Grandchild>
            <Grandchild>Grandchild 6</Grandchild>
        </subParent>
    </Parent>
</root>';
      $doc = new DOMDocument();
      $doc->preserveWhiteSpace = false;
      $doc->loadXML($xml);
      $i=0;
            while(is_object($finance = $doc->getElementsByTagName("Parent")->item($i)))
            {
                     foreach($finance->childNodes as $nodename)
                     {
                             if($nodename->nodeName=='subParent')
                                {
                                     foreach($nodename->childNodes as $subNodes)
                                     {
                                     echo $subNodes->nodeName." - ".$subNodes->nodeValue."<br>";
                                     }
                                }
                             else
                                {
                                echo $nodename->nodeName." - ".$nodename->nodeValue."<br>";
                                }
                     }
      $i++;
            }
?>
metron at underhive-planet dot com 08-Oct-2010 02:28
My first try to get a stable solution with this function, failed with this Exception:

"Fatal error: Call to undefined method DOMNodeList::getElementsByTagName()"
This is the xml snipplet:
<?xml version="1.0" encoding="UTF-8"?>
<root>
 <component>
  <properties>
    ....<any element>
  </properties>
 </component>
</root>

So the php code to climb along the elements is:
<?php

$src = new DOMDocument('1.0', 'utf-8');
$src->formatOutput = true;
$src->preserveWhiteSpace = false;

//Loading extern file
$src->load('../xml/Item_component.xml');

//Check each child of first indexed branch node of: <component>
//First get element after root element: <component>

//1st level
$component = $src->getElementsByTagName('component')->item(0);

//2nd level, get next element after component, here it fails!!
$properties = $component->getElementsByTagName('properties')->item(0);
...
?>

I realised, that there is a different when using different libxml2 versions on Apache2. This code will fail with libxml2 version 2.6.23 and PHP version 5.2.6
--
->It works fine with libxml2 version 2.6.32 and PHP version 5.2.6-3ubuntu4.6
->...and finally it also works with libxml2 2.7.7 and PHP >= 5.3

So if you bored to search for solutions with DOM like I did, please ensure that your www environment has the correct libxml2 / PHP Version installed on your apache2 server.
calvin at g mail 03-Oct-2010 01:41
My first post!
And this is how I get elements by attribute and its value.
For an example, if I want to grab all DIV tags with class name 'className', then...

<?php
$some_link = 'some website';
$tagName = 'div';
$attrName = 'class';
$attrValue = 'className';

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
@$dom->loadHTMLFile($some_link);

$html = getTags( $dom, $tagName, $attrName, $attrValue );
echo $html;

function getTags( $dom, $tagName, $attrName, $attrValue ){
    $html = '';
    $domxpath = new DOMXPath($dom);
    $newDom = new DOMDocument;
    $newDom->formatOutput = true;

    $filtered = $domxpath->query("//$tagName" . '[@' . $attrName . "='$attrValue']");
    // $filtered =  $domxpath->query('//div[@class="className"]');
    // '//' when you don't know 'absolute' path

    // since above returns DomNodeList Object
    // I use following routine to convert it to string(html); copied it from someone's post in this site. Thank you.
    $i = 0;
    while( $myItem = $filtered->item($i++) ){
        $node = $newDom->importNode( $myItem, true );    // import node
        $newDom->appendChild($node);                    // append node
    }
    $html = $newDom->saveHTML();
    return $html;
}

?>

Please, improve it, and share it.
Philip N 01-Sep-2010 12:17
Note that when using getElementsByTagName that it is a dynamic list. Thus if you have code which adjusts the DOM structure it will change the results of the getElementsByTagName results list.

The following code iterates through a complete set of results and changes them all to a new tag:

<?php
 $nodes = $xml->getElementsByTagName ("oldtag");

 $nodeListLength = $nodes->length; // this value will also change
 for ($i = 0; $i < $nodeListLength; $i ++)
 {
    $node = $nodes->item(0);

    // some code to change the tag name from "oldtag" to something else
    // e.g. encrypting a tag element
 }
?>

Since the list is dynamically updating, $nodes->item(0) is the next "unchanged" tag.
Marco Maranao 28-Apr-2010 07:22
The following takes a list of news items from an XML file (or an RSS feed), assigning it to an array first for a name value pair and then generating an HTML list.

<?php

$xml  =<<<EOT
<?xml version="1.0" encoding="ISO-8859-1"?>
<news>
    <item>
        <title>News 1</title>
        <created>04/2/2010 08:00 EST</created>
        <url>http://news.example.com/news.pdf</url>
    </item>
    <item>
        <title>News 2</title>
        <created>04/25/2010 08:00 EST</created>
        <url>http://news.example.com/news.pdf</url>
    </item>
    <item>
        <title>News 3</title>
        <created>04/27/2010 08:00 EST</created>
        <url>http://news.example.com/news.pdf</url>
    </item>
</news>
EOT;

$doc = new DOMDocument();

if ($doc->loadXML($xml)) {
    $items = $doc->getElementsByTagName('item');
    $headlines = array();

    foreach($items as $item) {
        $headline = array();

        if($item->childNodes->length) {
            foreach($item->childNodes as $i) {
                $headline[$i->nodeName] = $i->nodeValue;
            }
        }

        $headlines[] = $headline;
    }

    if(!empty($headlines)) {
        $hc = 0;

        echo '<ul>';

        foreach($headlines as $headline) {
            if(++$hc <= 3) {
                echo '<li>'
                    .'<a href="'.$headline['url'].'" target="_blank">'
                        .'<span>'.date('F j, Y', strtotime($headline['created'])).'</span><br />'
                        .$headline['title']
                    .'</a>'
                .'</li>';
            }
        }

        echo '</ul>';
    }
}

?>
james 02-Feb-2010 04:03
Problem:
You have an XML document that contains filename references to, say, images. Each filename reference is defined by <file>filename.ext</file> tag. You'd like to perform perform additional validation, say, after running the XML document through XSD validation. The additional validation can be any of your choice, in this example, it would be ideal to convert the PHP code to a function. The function would then determine if the images exist and return either an integer value or a boolean.

<?xml version="1.0"?>
<root>
<box>
<file>example.png</file>
</box>
<content>
<item>
<image><file>example2.png</file></image>
<caption>The above image is an example</caption>
</item>
</content>
</root>

Solution:
<?php

$dom = new DomDocument();
$dom->prevservWhiteSpace = false;

if (!@$dom->load("example.xml")) {
    echo "example.xml doesn't exist!\n";
    return;
}

$imageList = $dom->getElementsByTagName('file');
$imageCnt  = $imageList->length;

for ($idx = 0; $idx < $imageCnt; $idx++) {
    print $imageList->item($idx)->nodeValue . "\n";
}

?>

The above PHP code could easily be turned into a function that returns an array of image filenames, an integer value relative to the number of images found, etc.

Hope this is helpful.
gurmukh24 at gmail dot com 03-Mar-2009 01:06
Following Example is of multiple attributes and multiple child nodes. this is being used to make joomla plugin for bulk upload of articles. Gurmukh Singh Bhatti

<?php
$xml =<<<EOT
<?xml version="1.0"?>
<root>
<section name="Section1">
  <category id="Category1" name="google">
   <arti name="article1">
   <p>any html code here</p>
   <b>my name is so so</b>
    </arti>
   <arti name="article2">value2</arti>
   <arti name="article3">value3</arti>
   <arti name="article4">value4</arti>
  </category>
    <category id="Category2" name="yahoo">
   <arti name="articleSection2">Test value</arti>
  </category>
</section>
<section name="Section2">
  <category id="category1_of_section2" name="msn">
   <arti name="article2">value1</arti>
   <arti name="article3">value2</arti>
  </category>
    <category id="Category2_of_section2" name="webcare">
    <arti name="param3">value4</arti>
   </category>
</section>
</root>
EOT;

$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
$params = $dom->getElementsByTagName('section'); // Find Sections
$k=0;
foreach ($params as $param) //go to each section 1 by 1
{
         echo "Section Attribute :-> ".$params->item($k)->getAttribute('name')."<br>";   //get section attribute
         $params2 = $params->item($k)->getElementsByTagName('category'); //digg categories with in Section
      $i=0; // values is used to iterate categories
        foreach ($params2 as $p) {
           echo "&nbsp;&nbsp;- Category Attribute Name :-> ".$params2->item($i)->getAttribute('name')."<br>"; //get Category attributes
            $params3 = $params2->item($i)->getElementsByTagName('arti'); //dig Arti into Categories
                 $j=0;//values used to interate Arti
                     foreach ($params3 as $p2)
                   {
                    echo "&nbsp;&nbsp;&nbsp;- Article Attribute Name : ".$params3->item($j)->getAttribute('name').""; //get arti atributes
echo "&nbsp;&nbsp; Value : ".$params3->item($j)->nodeValue."<br>"; //get Node value ;
                              $j++;
                   }
         $i++;
      }
$k++;

}
?>

output :
 Section Attribute :-> Section1
  - Category Attribute Name :-> google
            - Article Attribute Name : article1   Value : any html code heremy name is so so
            - Article Attribute Name : article2   Value : value2
            - Article Attribute Name : article3   Value : value3
            - Article Attribute Name : article4   Value : value4
  - Category Attribute Name :-> yahoo
            - Article Attribute Name : articleSection2   Value : Test value
Section Attribute :-> Section2
  - Category Attribute Name :-> msn
            - Article Attribute Name : article2   Value : value1
            - Article Attribute Name : article3   Value : value2
  - Category Attribute Name :-> webcare
            - Article Attribute Name : param3   Value : value4
James L 19-Aug-2008 01:04
Return if there are no matches is an empty DOMNodeList. Check using length property, e.g.:

<?php
$nodes=$domDocument->getElementsByTagName('book') ;
if ($nodes->length==0) {
   // no results
}
?>
jason at shaped dot ca 11-Feb-2008 01:59
in response to tildy at pr dot hu

my preferred way is (in example to gather country data from an iso 3166 xml flie)

$countries = new DOMDocument();
$countries->load("./lib/iso_3166.xml");

$countriesList = $countries->getElementsByTagName("ISO_3166-1_Entry");

foreach($countriesList as $country) {
    $values = $country->getElementsByTagName("*");
    foreach($values as $node) {
      echo $node->nodeName."=".$node->nodeValue;
    }
}
Finding values of a node 14-Mar-2007 11:01
I don't know if this is that obvious but it was not for me, so in addition to gem at rellim dot com's posting:
adding

<?php
echo $param -> nodeValue.'<br>';
?>

to the loop will output
value1
value2
value3
gem at rellim dot com 30-Sep-2004 01:20
Here is an example of getElementsByTagName():

<?php
$xml =<<<EOT
<?xml version="1.0"?>
<config>
  <section id="section1">
   <param name="param1">value1</param>
   <param name="param2">value2</param>
  </section>
  <section id="section2">
   <param name="param3">value3</param>
  </section>
</config>
EOT;

$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
$params = $dom->getElementsByTagName('param');

foreach ($params as $param) {
       echo $param -> getAttribute('name').'<br>';
}
?>

Expected result:
--------------
param1
param2
param3


Searches for all elements with given tag name in specified namespace

  • ≪ DOMDocument::getElementsByTagName
  • DOMDocument::importNode ≫
  • PHP Manual
  • DOMDocument
  • Searches for all elements with given tag name in specified namespace

DOMDocument::getElementsByTagNameNS

(PHP 5, PHP 7)

DOMDocument::getElementsByTagNameNS — Searches for all elements with given tag name in specified namespace

설명

public DOMNodeList DOMDocument::getElementsByTagNameNS ( string $namespaceURI , string $localName )

Returns a DOMNodeList of all elements with a given local name and a namespace URI.

인수

namespaceURI

The namespace URI of the elements to match on. The special value * matches all namespaces.

localName

The local name of the elements to match on. The special value * matches all local names.

반환값

A new DOMNodeList object containing all the matched elements.

예제

Example #1 Get all the XInclude elements

<?php

$xml = <<<EOD
<?xml version="1.0" ?>
<chapter xmlns:xi="http://www.w3.org/2001/XInclude">
<title>Books of the other guy..</title>
<para>
 <xi:include href="book.xml">
  <xi:fallback>
   <error>xinclude: book.xml not found</error>
  </xi:fallback>
 </xi:include>
 <include>
  This is another namespace
 </include>
</para>
</chapter>
EOD;
$dom = new DOMDocument;

// load the XML string defined above
$dom->loadXML($xml);

foreach ($dom->getElementsByTagNameNS('http://www.w3.org/2001/XInclude', '*') as $element) {
    echo 'local name: ', $element->localName, ', prefix: ', $element->prefix, "\n";
}
?>

위 예제의 출력:

local name: include, prefix: xi
local name: fallback, prefix: xi

참고

  • DOMDocument::getElementsByTagName() - Searches for all elements with given local tag name

User Contributed Notes

RiKdnUA at mail dot ru 20-Mar-2015 04:47
<?php
ini_set("display_errors","on"); error_reporting(-1);
$doc="<?xml version='1.0' encoding='WINDOWS-1251'?>
<htm:body xmlns:htm='w3.org'>
<span>Text</span>
<htm:img htm:src='imgSrc'/>
</htm:body>";
$dom = new DOMDocument('1.0','WINDOWS-1251');
$dom->preserveWhiteSpace=false;
$dom->loadXML($doc);
$domList=$dom->getElementsByTagNameNS('*','*');//all namespaces, all local names
echo "Количество элементов. The number of elements: ".$domList->length."<br />";// 2 (not 3)
for ($i=0; $i<$domList->length; $i++)
{echo "Элемент $i. The element $i: ".$domList->item($i)->tagName."<br />";
}
/*
Tested in PHP 5.2.9-2 and 5.2.17
The above example will output:
Количество элементов. The number of elements: 2
Элемент 0. The element 0: htm:body
Элемент 1. The element 1: htm:img

Вывод. Не найден элемент span
Conclusion. Not found span element
*/
?>


Import node into current document

  • ≪ DOMDocument::getElementsByTagNameNS
  • DOMDocument::load ≫
  • PHP Manual
  • DOMDocument
  • Import node into current document

DOMDocument::importNode

(PHP 5, PHP 7)

DOMDocument::importNode — Import node into current document

설명

public DOMNode DOMDocument::importNode ( DOMNode $importedNode [, bool $deep ] )

This function returns a copy of the node to import and associates it with the current document.

인수

importedNode

The node to import.

deep

If set to TRUE, this method will recursively import the subtree under the importedNode.

Note:

To copy the nodes attributes deep needs to be set to TRUE

반환값

The copied node or FALSE, if it cannot be copied.

오류/예외

DOMException is thrown if node cannot be imported.

예제

Example #1 DOMDocument::importNode() example

Copying nodes between documents.

<?php

$orgdoc = new DOMDocument;
$orgdoc->loadXML("<root><element><child>text in child</child></element></root>");

// The node we want to import to a new document
$node = $orgdoc->getElementsByTagName("element")->item(0);

// Create a new document
$newdoc = new DOMDocument;
$newdoc->formatOutput = true;

// Add some markup
$newdoc->loadXML("<root><someelement>text in some element</someelement></root>");

echo "The 'new document' before copying nodes into it:\n";
echo $newdoc->saveXML();

// Import the node, and all its children, to the document
$node = $newdoc->importNode($node, true);
// And then append it to the "<root>" node
$newdoc->documentElement->appendChild($node);

echo "\nThe 'new document' after copying the nodes into it:\n";
echo $newdoc->saveXML();
?>

위 예제의 출력:

The 'new document' before copying nodes into it:
<?xml version="1.0"?>
<root>
  <someelement>text in some element</someelement>
</root>

The 'new document' after copying the nodes into it:
<?xml version="1.0"?>
<root>
  <someelement>text in some element</someelement>
  <element>
    <child>text in child</child>
  </element>
</root>

User Contributed Notes

rikdnua at mail dot ru 11-Sep-2013 10:54
The behavior of importNode($node, false) is different in PHP 5.2.9-2 and PHP 5.2.17.
The second option is explicitly set to false. In PHP 5.2.9-2 importNode() imports the elements WITHOUT attributes. In PHP 5.2.17 the elements imported ALONG with their attributes.
<?php
$xml="
<html>
<a href='yandex.com'>Yandex.com</a>
<a href='rik.dn.ua'>RiK.dn.ua</a>
</html>
";
$doc=new domDocument('1.0');
$doc->loadXML($xml);
$list=$doc->getElementsByTagName('a');
$doc1=new domDocument('1.0');
$doc1->formatOutput=true;
for($i=0; $i<$list->length; $i++) $doc1->appendChild($doc1->importNode($list->item($i), false));
$doc1->save('file.xml');
?>

file.xml PHP 5.2.9-2
<?xml version="1.0"?>
<a/>
<a/>

file.xml PHP 5.2.17
<?xml version="1.0"?>
<a href="yandex.com"/>
<a href="rik.dn.ua"/>
c dot 1 at smithies dot org 20-Oct-2009 03:40
Assume that $source and $dest are instances of DOMDocument. Assume that $sourcedoc contains an element with ID 'sourceID' and that $destdoc contains an element with ID 'destID'. Assume that we have already set up source and destination element variables thus:

<?php
 $src = $sourcedoc->getElementById('sourceID');
 $dst = $destdoc->getElementById('destID');
?>

Finally, assume that $sel has more than one child node.

In order to import the child elements of $src as children of $dst, you might do something like this:

<?php
$src = $dest->importNode($src, TRUE);

foreach ($src->childNodes as $el) $dst->appendChild($el);
?>

But this does not work. foreach gets confused, because (as noted below) appending an imported element to another existing element in the same document causes it to be removed from its current parent element.

Therefore, the following technique should be used:

<?php
foreach ($src->childNodes as $el) $dst->appendChild($destdoc->importNode($el, TRUE));
?>
p dot reisinger at gmail dot com 29-Dec-2008 12:43
When you use function/method below, php automaticaly inserts namespaces (if you have some). If you want to join xml files exactly, then you can just use placeholders and treat xml as a string.
<?php
function joinXMLStrings($file1, $file2)
{
    //remove xml declaration
    $file2 = trim(preg_replace('/<\?xml.*\?>/', '', $file2, 1));

    //insert file2 in the place of placeholder in the first file
    $file1 = trim(preg_replace('/<\?file2\s\?>/', $file2, $file1, 1));
}
?>

so function looks like this and you insert two xml files. First file has to have placeholder (where you insert file2). Placeholder in this case will be <?file2 ?>

!!!this is only for ilustration - test before you use it
p dot reisinger at gmail dot com 23-Dec-2008 04:32
method (can be used as a function as well) that joins two xml files. first argument is parent xml (the one to be inserted into), second child (the one to be inserted) and third is optional argument that specifies the parent's tag where to insert children xml. If not specified then children is inserted as the last element, just before end of the root
<?php
    protected function joinXML($parent, $child, $tag = null)
    {
        $DOMChild = new DOMDocument;
        $DOMChild->loadXML($child);
        $node = $DOMChild->documentElement;

        $DOMParent = new DOMDocument;
        $DOMParent->formatOutput = true;
        $DOMParent->loadXML($parent);

        $node = $DOMParent->importNode($node, true);

        if ($tag !== null) {
            $tag = $DOMParent->getElementsByTagName($tag)->item(0);
            $tag->appendChild($node);
        } else {
            $DOMParent->documentElement->appendChild($node);
        }

        return $DOMParent->saveXML();
    }
?>
mailme at sove dot nl 11-Jun-2008 02:29
DOMDocument->importNode with seconds argument false will leave attributes behind. To fix this:

$__DOM->importNode
   (
      $__INPUT->cloneNode(false), true
   );

$__DOM (DOMDocument) will import the $__INPUT node (DOMElement) INCLUDING attributes.
mark at 4inloop dot de 06-Apr-2007 02:51
When you left out the second argument or enter false, not only the child nodes are ommited. The attributes of the node are also cut off.
adjwilli 05-Oct-2006 10:08
Editing XML with PHP can be a pain in the Secretary of State Powell, so here's a script to replace an XML node with a user-provided one through the POST. It's usually a good idea to run the $_POST['xml'] through a validation check and clean it for other thing before running this.

Pretty much this script expects a user-provided node called $_POST['xml'] and the XPath of the node in the original document that you want to replace called $_POST['XPath']. It also loads the original XML document from $xml. The function nodeRunner begins with the root node of the document you're editting and the XPath for the root element (these are more to make recursion easy than anything else).

$doc = new DOMDocument();
$doc->loadXML($xml); // $xml expects an XML string

function nodeRunner ($node,$xpath) {
    global $doc;
    if ($xpath == $_POST['XPath']) {

        $xmlPost = new DOMDocument();
        $xmlPost->loadXML($_POST['xml']);

        $newNode = $doc->importNode($xmlPost->firstChild,true);

        $node->parentNode->replaceChild($newNode,$node);
    } else {

        $page = 1;
        $section = 1;

        if ($node->hasChildNodes()) {
            foreach ($node->childNodes as $nodling) {
                $nodeName = $nodling->nodeName;
                if ($nodeName == 'page' || $nodeName == 'section') {
                    nodeRunner ($nodling,$xpath."/".$nodeName."[".$$nodeName."]");
                    $$nodeName++;
                }
            }
        }
    }
}

nodeRunner ($doc->documentElement,"/root[1]"); // /root should be explicit name of the root  element of the XPath

$doc->saveXML();
stomas 19-Sep-2006 03:14
I think this should do the same:

<?
 // Import $b into $a's document
 function myimport($a, $b)
 {
   if ($a->ownerDocument->isSameNode($b->ownerDocument))
   {
    return $b->cloneNode(TRUE);
   }
   else
   {
    return $a->ownerDocument->importNode($b, TRUE);
   }
 }
 ?>
Colin 12-Sep-2006 06:11
As of PHP 5.1.6 with libxml 2.6.26 and DOM/XML API version 20031129, importNode() does nothing if attempting to import from the same document.  Meaning, if you do an $ret = importNode and then appendChild($ret) or insertBefore($ret, ...) then you will end up *moving* the node instead of ending up with a copy.

If you expect importNode to give you a copy of the source (in this case, a deep copy) then you must account for them being from the same document.  This function addresses this:

<?
// Import $b into $a's document
function myimport($a, $b)
{
  if ($a->ownerDocument->isSameNode($b->ownerDocument))
  {
    $temp = new DOMDocument();
    $ret = $temp->importNode($b, TRUE);
    return $a->ownerDocument->importNode($ret, TRUE);
  }
  else
  {
    return $a->ownerDocument->importNode($b, TRUE);
  }
}
?>

(Function was freshly coded for this note but I based it off another, working function of mine.)

This function checks if the documents are the same and uses a new document (guaranteed to be different this way) to force a copy into $temp and then force a copy back into $a->ownerDocument.

Obviously, no error checking has been done.
andy dot clark at dial dot pipex dot com 15-Aug-2006 05:20
One useful use of importNode is to copy one node onto another.

function CopyXMLNode($SourceNode,$DestNode)
 {
  if ($SourceNode->nodeName != '#text')
   {
     //Copy this node
    $node = $DestNode->ownerDocument->importNode($SourceNode, true);
    $node = $DestNode->appendChild($node);
    //Now copy the child nodes
    foreach ($SourceNode->childNodes AS $item)
     {
     $this->CopyXMLNode($item,$node);
     }
    }
  }
Fitopaldi 04-Aug-2005 08:29
importNode returns a copy of the node to import and associates it with the current document, but not import the node to the current DOMDocument. Use appendChild for import the copy of the node to current DOMDocument.

<?
 $domNode = $dom->importNode($aDomNode, true);
 $currentDomDocument->appendChild($domNode);
?>


Load XML from a file

  • ≪ DOMDocument::importNode
  • DOMDocument::loadHTML ≫
  • PHP Manual
  • DOMDocument
  • Load XML from a file

DOMDocument::load

(PHP 5, PHP 7)

DOMDocument::load — Load XML from a file

설명

public mixed DOMDocument::load ( string $filename [, int $options = 0 ] )

Loads an XML document from a file.

Warning

Unix style paths with forward slashes can cause significant performance degradation on Windows systems; be sure to call realpath() in such a case.

인수

filename

The path to the XML document.

options

Bitwise OR of the libxml option constants.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다. If called statically, returns a DOMDocument실패 시 FALSE를 반환합니다.

오류/예외

If an empty string is passed as the filename or an empty file is named, a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml's error handling functions.

이 메소드는 정적으로 호출할 수있지만, E_STRICT 오류가 발생합니다.

예제

Example #1 Creating a Document

<?php
$doc = new DOMDocument();
$doc->load('book.xml');
echo $doc->saveXML();
?>

참고

  • DOMDocument::loadXML() - Load XML from a string
  • DOMDocument::save() - Dumps the internal XML tree back into a file
  • DOMDocument::saveXML() - Dumps the internal XML tree back into a string

User Contributed Notes

sainthyoga2003 at gmail dot com 10-Feb-2012 12:37
in the default example:

<?php
$doc = new DOMDocument();
$doc->load('book.xml');
echo $doc->saveXML();
?>

you must enter the absolute path for book.xml due for get a false result in load function.
sainthyoga2003 at gmail dot com 25-Feb-2011 02:38
i've found the partial solution for xml:id warning,is explained at this address: https://fosswiki.liip.ch/display/BLOG/GetElementById+Pitfalls

there explains that:
The ID does have to be a valid NCName, which for example means, that the first letter can't be a number.

and in my xml:id i had a number. :D
sainthyoga2003 at gmail dot com 16-Feb-2011 06:31
adding an id for a child, when i use abstract mode i get the following warning:

[quote]Warning: DOMDocument::load() [domdocument.load]: xml:id : attribute value xx is not an NCName in /fake/path/to/xmlfile[/quote]
_ michael 01-Jun-2010 05:34
load() will handle non-ASCII characters depending on the details of the XML declaration, but in a somewhat surprising way. One would assume that the declarations '<?xml version="1.0" encoding="UTF-8"?>' and '<?xml version="1.0"?>' are treated in the same way because UTF-8 is the default encoding anyway. But not so.

* If there is an XML declaration defining the encoding *explicitly*, the non-ASCII characters remain unchanged.
* If the XML declaration does not define the encoding explicitly, or if the XML declaration is missing, non-ASCII characters are converted into numeric entities.

So the document

    <?xml version="1.0"?>
    <root><nonascii>a</nonascii></root>

will be converted to

    <?xml version="1.0"?>
    <root><nonascii>&#xE4;</nonascii></root>

The same happens if there is no XML declaration at all. On the other hand, the document

    <?xml version="1.0" encoding="UTF-8"?>
    <root><nonascii>a</nonascii></root>

will remain as it is.

This behaviour applies to loadXML() as well.
_ michael 19-Apr-2010 04:50
XHTML and entities: The solution proposed below by zachatwork at gmail dot com didn't work for me. I checked on a number of servers (both LAMPP and WAMPP) - on each of them, calling loadXML() with the LIBXML_DTDLOAD option triggered an external request for the DTD. And that's bad news.

If allow_url_fopen is turned off, the request for the DTD fails with a warning. If it is turned on, the request fails because these w3c URLs return a 503 Service Unavailable.

HTML entities still generate a warning in either case.

The best solution, as far as I can tell, is simply to ignore the warnings and suppress them using '@'. I can't recommend parsing XHTML with loadHTML() instead of loadXML() - yes, you get rid of the entity problem, but loadHTML() changes the source while parsing it (tries to 'fix' it even though there is nothing to fix).
zachatwork at gmail dot com 01-Jul-2009 08:28
You can easily avoid the warning about &nbsp; references by using the LIBXML_DTDLOAD option.

<?php

$html = <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  </head>
  <body>
    <p>&nbsp;</p>
  </body>
</html>
EOF;

// This one works perfectly.
$dom = new DOMDocument();
$dom->loadXML($html, LIBXML_DTDLOAD);
print $dom->saveXML();

// This one produces a warning.
$dom = new DOMDocument();
$dom->loadXML($html);
print $dom->saveXML();

?>

See also: http://www.php.net/manual/en/libxml.constants.php

Note that libxml will detect that your DTD is locally available via /etc/xml/catalog. So there is no worry about this causing your DOM loads to make external network requests.
Jonas Due Vesterheden 09-Jun-2009 05:18
I had a problem with loading documents over HTTP. I would get errors looking like this:

Warning: DOMDocument::load(http://external/document.xml): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error

The document would load fine in browsers and using wget. The problem is that DOMDocument::load() on my systems (both OS X and Linux) didn't send any User-Agent header which for some weird reason made Microsoft-IIS/6.0 respond with the 500 error.

The solution is found on http://php.net/manual/en/function.libxml-set-streams-context.php :

<?php
$opts = array(
    'http' => array(
        'user_agent' => 'PHP libxml agent',
    )
);

$context = stream_context_create($opts);
libxml_set_streams_context($context);

// request a file through HTTP
$doc = DOMDocument::load('http://www.example.com/file.xml');
?>
syntaxiko 05-Apr-2009 07:16
Function will not work if using XML DOM PECL module
darren at viamedia dot co dot za 05-Aug-2008 04:17
If you are loading xml with the intention of validating it against an internal dtd and you have experienced issues with the validation it could be related to missing LIBXML constants.

I found this post by "aidan at php dot net" in root level dom docs and thought it might be more useful here:
As of PHP 5.1, libxml options may be set using constants rather than the use of proprietary DomDocument properties.

DomDocument->resolveExternals is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDATTR

DomDocument->validateOnParse is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDVALID

PHP 5.1 users are encouraged to use the new constants.

Example:
<?php
$dom = new DOMDocument;
// Resolve externals
$dom->load($file, LIBXML_DTDLOAD|LIBXML_DTDATTR);
// OR
// Validate against DTD
$dom->load($file, LIBXML_DTDLOAD|LIBXML_DTDVALID);
$dom->validate();
?>
the_N_Channel 16-Jul-2008 01:53
NOTE, will not load successfully if there is a comment at the beginning of the file before the <?xml version="1.0" ?> declaration!
hh dot lohmann at yahoo dot de 30-Aug-2007 12:48
BadGuy´s note may be confusing since what he depicts is no special property of the relevant method. PHP works always in and on a local file system which means that if you want to use resources from other systems or - what is, indeed, BadGuy´s problem - need resources that have been dealt with by other programs or processes, you have to state and manage that explicitly in your code. PHP is just a quite normal program in that.

BadGuy´s solution is using the "http wrapper" to get output from another process (see "wrappers" in the PHP manual). Doing this, the appropriate syntax for http calls has to be respected.
admin at tijnema dot tijnema dot info 02-Jun-2007 01:39
In reply to BadGuy [at] BadGuy [dot] nl

When the news.php file is located on the same server, like you said in the first example then http://my.beautiful-website.com/xmlsource/news.php wouldn't work, but you should use http://localhost/xmlsource/news.php or http://127.0.0.1/xmlsource/news.php
BadGuy [at] BadGuy [dot] nl 18-Jan-2007 05:50
Note that this method uses the local file system before doing anything remote. The 'disadvantage' would be that if you would do the following:
<?php
$xml = new DOMDocument;
$xml->load("xmlsource/news.php");
?>

This would not make the method read the actual output of the news.php file --presumably valid xml data--, but the file contents --obviously this would be php code. So this will return an error saying news.php is missing the xml declaration and maybe the xml start-tag

What would work is the following:

<?php
$xml = new DOMDocument;
$xml->load("http://my.beautiful-website.com/xmlsource/news.php");
?>

This will force a http request to be used to get this file instead of just locally reading it and the file just returning code
daevid at daevid dot com 19-Oct-2005 02:08
Suppose you wanted to dynamically load an array from an .XSD file. This method is your guy. just remember to use the actual xs: portion in xpaths and such.

All the other "load" methods will error out.

<?php
$attributes = array();
$xsdstring = "/htdocs/api/xsd/common.xsd";
$XSDDOC = new DOMDocument();
$XSDDOC->preserveWhiteSpace = false;
if ($XSDDOC->load($xsdstring))
{
    $xsdpath = new DOMXPath($XSDDOC);
    $attributeNodes =
              $xsdpath->
              query('//xs:simpleType[@name="attributeType"]')
              ->item(0);
    foreach ($attributeNodes->childNodes as $attr)
    {
        $attributes[ $attr->getAttribute('value') ] = $attr->getAttribute('name');
    }
    unset($xsdpath);
}
print_r($attributes);
?>


Load HTML from a string

  • ≪ DOMDocument::load
  • DOMDocument::loadHTMLFile ≫
  • PHP Manual
  • DOMDocument
  • Load HTML from a string

DOMDocument::loadHTML

(PHP 5, PHP 7)

DOMDocument::loadHTML — Load HTML from a string

설명

public bool DOMDocument::loadHTML ( string $source [, int $options = 0 ] )

The function parses the HTML contained in the string source. Unlike loading XML, HTML does not have to be well-formed to load. This function may also be called statically to load and create a DOMDocument object. The static invocation may be used when no DOMDocument properties need to be set prior to loading.

인수

source

The HTML string.

options

Since PHP 5.4.0 and Libxml 2.6.0, you may also use the options parameter to specify additional Libxml parameters.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다. If called statically, returns a DOMDocument실패 시 FALSE를 반환합니다.

오류/예외

If an empty string is passed as the source, a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml's error handling functions.

이 메소드는 정적으로 호출할 수있지만, E_STRICT 오류가 발생합니다.

오류가 있는 HTML은 성공적으로 읽을 수 있지만, 잘못된 마크업을 만날 때 마다 E_WARNING 오류가 발생합니다. libxml의 오류 처리 함수functions로 이러한 오류를 다룰 수 있습니다.

예제

Example #1 Creating a Document

<?php
$doc = new DOMDocument();
$doc->loadHTML("<html><body>Test<br></body></html>");
echo $doc->saveHTML();
?>

변경점

버전 | 설명
5.4.0 | Added options parameter.

참고

  • DOMDocument::loadHTMLFile() - Load HTML from a file
  • DOMDocument::saveHTML() - Dumps the internal document into a string using HTML formatting
  • DOMDocument::saveHTMLFile() - Dumps the internal document into a file using HTML formatting

User Contributed Notes

fr at felix-riesterer dot de 13-Feb-2016 10:43
Remember: If you use an HTML5 doctype and a meta element like so

<meta CHARSET=ks_c_5601-1987">

your HTML code will get interpreted as ISO-8859-something and non-ASCII chars will get converted into HTML entities. However the HTML4-like version will work (as has been pointed out 10 years ago by "bigtree at 29a"):

<meta http-equiv="Content-Type" content="text/html; CHARSET=ks_c_5601-1987">
finkenb2 at mail dot lib dot msu dot edu 06-Oct-2015 01:03
Warning:  This does not function well with HTML5 elements such as SVG.  Most of the advice on the Web is to turn off errors in order to have it work with HTML5.
cake at brothercake dot com 17-Dec-2012 02:57
Be aware that this function doesn't actually understand HTML -- it fixes tag-soup input using the general rules of SGML, so it creates well-formed markup, but has no idea which element contexts are allowed.

For example, with input like this where the first element isn't closed:

    <span>hello <div>world</div>

loadHTML will change it to this, which is well-formed but invalid:

    <span>hello <div>world</div></span>
Alex 10-Apr-2010 05:45
Beware of the "gotcha" (works as designed but not as expected): if you use loadHTML, you cannot validate the document. Validation is only for XML. Details here: http://bugs.php.net/bug.php?id=43771&edit=1
Shane Harter 04-Jan-2010 05:42
DOMDocument is very good at dealing with imperfect markup, but it throws warnings all over the place when it does.

This isn't well documented here. The solution to this is to implement a separate aparatus for dealing with just these errors.

Set libxml_use_internal_errors(true) before calling loadHTML. This will prevent errors from bubbling up to your default error handler. And you can then get at them (if you desire) using other libxml error functions.

You can find more info here http://www.php.net/manual/en/ref.libxml.php
mdmitry at gmail dot com 21-Dec-2009 06:02
You can also load HTML as UTF-8 using this simple hack:

<?php

$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="UTF-8">' . $html);

// dirty fix
foreach ($doc->childNodes as $item)
    if ($item->nodeType == XML_PI_NODE)
        $doc->removeChild($item); // remove hack
$doc->encoding = 'UTF-8'; // insert proper

?>
piopier 14-Jun-2009 05:29
Here is a function I wrote to capitalize the previous remarks about charset problems (UTF-8...) when using loadHTML and then DOM functions.
It adds the charset meta tag just after <head> to improve automatic encoding detection, converts any specific character to an html entity, thus PHP DOM functions/attributes will return correct values.

<?php
mb_detect_order("ASCII,UTF-8,ISO-8859-1,windows-1252,iso-8859-15");
function loadNprepare($url,$encod='') {
        $content        = file_get_contents($url);
        if (!empty($content)) {
                if (empty($encod))
                        $encod  = mb_detect_encoding($content);
                $headpos        = mb_strpos($content,'<head>');
                if (FALSE=== $headpos)
                        $headpos= mb_strpos($content,'<HEAD>');
                if (FALSE!== $headpos) {
                        $headpos+=6;
                        $content = mb_substr($content,0,$headpos) . '<meta http-equiv="Content-Type" content="text/html; charset='.$encod.'">' .mb_substr($content,$headpos);
                }
                $content=mb_convert_encoding($content, 'HTML-ENTITIES', $encod);
        }
        $dom = new DomDocument;
        $res = $dom->loadHTML($content);
        if (!$res) return FALSE;
        return $dom;
}
?>

NB: it uses mb_strpos/mb_substr instead of mb_ereg_replace because that seemed more efficient with huge html pages.
Errol 11-Feb-2009 05:05
It should be noted that when any text is provided within the body tag
outside of a containing element, the DOMDocument will encapsulate that
text into a paragraph tag (<p>).

For example:
<?php
$doc = new DOMDocument();
$doc->loadHTML("<html><body>Test<br><div>Text</div></body></html>");
echo $doc->saveHTML();
?>

will yield:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<p>Test<br></p>
<div>Text</div>
</body></html>

while:
<?php
$doc = new DOMDocument();
$doc->loadHTML(
    "<html><body><i>Test</i><br><div>Text</div></body></html>");
echo $doc->saveHTML();
?>

will yield:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<i>Test</i><br><div>Text</div>
</body></html>
jamesedwardcooke+php at gmail dot com 20-Oct-2008 08:37
Using loadHTML() automagically sets the doctype property of your DOMDocument instance(to the doctype in the html, or defaults to 4.0 Transitional). If you set the doctype with DOMImplementation it will be overridden.

I assumed it was possible to set it and then load html with the doctype I defined(in order to decide the doctype at runtime), and ran into a huge headache trying to find out where my doctype was going. Hopefully this helps someone else.
xuanbn at yahoo dot com 04-Oct-2007 10:38
If you use loadHTML() to process utf HTML string (eg in Vietnamese), you may experience result in garbage text, while some files were OK. Even your HTML already have meta charset  like

  <meta http-equiv="content-type" content="text/html; CHARSET=ks_c_5601-1987">

I have discovered that, to help loadHTML() process utf file correctly, the meta tag should come first, before any utf string appear. For example, this HTML file

<html>
 <head>
    <meta http-equiv="content-type" content="text/html; CHARSET=ks_c_5601-1987">
    <title> Vietnamese - Ti?ng Vi?t</title>
  </head>
<body></body>
</html>

will be OK with loadHTML() when <meta> tag appear <title> tag.

But the file below will not regcornize by loadHTML() because <title> tag contains utf string appear before <meta> tag.

<html>
 <head>
    <title> Vietnamese - Ti?ng Vi?t</title>
    <meta http-equiv="content-type" content="text/html; CHARSET=ks_c_5601-1987">
  </head>
<body></body>
</html>
hanhvansu at yahoo dot com 27-Apr-2007 05:50
When using loadHTML() to process UTF-8 pages, you may meet the problem that the output of dom functions are not like the input. For example, if you want to get "C?nh tranh", you will receive "Caº¡nh tranh".  I suggest we use mb_convert_encoding before load UTF-8 page :
<?php
    $pageDom = new DomDocument();
    $searchPage = mb_convert_encoding($htmlUTF8Page, 'HTML-ENTITIES', "UTF-8");
    @$pageDom->loadHTML($searchPage);

?>
romain dot lalaut at laposte dot net 15-Feb-2007 05:31
Note that the elements of such document will have no namespace even with <html xmlns="http://www.w3.org/1999/xhtml">
bigtree at DONTSPAM dot 29a dot nl 26-Apr-2005 11:15
Pay attention when loading html that has a different charset than iso-8859-1. Since this method does not actively try to figure out what the html you are trying to load is encoded in (like most browsers do), you have to specify it in the html head. If, for instance, your html is in utf-8, make sure you have a meta tag in the html's head section:

<head>
<meta http-equiv="Content-Type" content="text/html; CHARSET=ks_c_5601-1987"/>
</head>

If you do not specify the charset like this, all high-ascii bytes will be html-encoded. It is not enough to set the dom document you are loading the html in to UTF-8.


Load HTML from a file

  • ≪ DOMDocument::loadHTML
  • DOMDocument::loadXML ≫
  • PHP Manual
  • DOMDocument
  • Load HTML from a file

DOMDocument::loadHTMLFile

(PHP 5, PHP 7)

DOMDocument::loadHTMLFile — Load HTML from a file

설명

public bool DOMDocument::loadHTMLFile ( string $filename [, int $options = 0 ] )

The function parses the HTML document in the file named filename. Unlike loading XML, HTML does not have to be well-formed to load.

인수

filename

The path to the HTML file.

options

Since PHP 5.4.0 and Libxml 2.6.0, you may also use the options parameter to specify additional Libxml parameters.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다. If called statically, returns a DOMDocument실패 시 FALSE를 반환합니다.

오류/예외

If an empty string is passed as the filename or an empty file is named, a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml's error handling functions.

이 메소드는 정적으로 호출할 수있지만, E_STRICT 오류가 발생합니다.

오류가 있는 HTML은 성공적으로 읽을 수 있지만, 잘못된 마크업을 만날 때 마다 E_WARNING 오류가 발생합니다. libxml의 오류 처리 함수functions로 이러한 오류를 다룰 수 있습니다.

예제

Example #1 Creating a Document

<?php
$doc = new DOMDocument();
$doc->loadHTMLFile("filename.html");
echo $doc->saveHTML();
?>

변경점

버전 | 설명
5.4.0 | Added options parameter.

참고

  • DOMDocument::loadHTML() - Load HTML from a string
  • DOMDocument::saveHTML() - Dumps the internal document into a string using HTML formatting
  • DOMDocument::saveHTMLFile() - Dumps the internal document into a file using HTML formatting

User Contributed Notes

qrworld.net 12-Nov-2014 01:37
In this post http://softontherocks.blogspot.com/2014/11/descargar-el-contenido-de-una-url_11.html I found a simple way to get the content of a URL with DOMDocument, loadHTMLFile and saveHTML().

function getURLContent($url){
    $doc = new DOMDocument;
    $doc->preserveWhiteSpace = FALSE;
    @$doc->loadHTMLFile($url);
    return $doc->saveHTML();
}
onemanbanddan at gmail dot com 04-Mar-2014 09:28
The options for surpressing errors and warnings will not work with this as they do for loadXML()
e.g.
<?php
$doc->loadHTMLFile($file, LIBXML_NOWARNING | LIBXML_NOERROR);
?>
will not work.
you must use:
<?php
libxml_use_internal_errors(true);
$doc->loadHTMLFile($file);
?>
and handle the exceptions as neccesarry.
Mark Omohundro, ajamyajax dot com 28-Nov-2008 03:32
<?php
// try this html listing example for all nodes / includes a few getElementsByTagName options:

$file = $DOCUMENT_ROOT. "test.html";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);

// example 1:
$elements = $doc->getElementsByTagName('*');
// example 2:
$elements = $doc->getElementsByTagName('html');
// example 3:
//$elements = $doc->getElementsByTagName('body');
// example 4:
//$elements = $doc->getElementsByTagName('table');
// example 5:
//$elements = $doc->getElementsByTagName('div');

if (!is_null($elements)) {
  foreach ($elements as $element) {
    echo "<br/>". $element->nodeName. ": ";

    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo $node->nodeValue. "\n";
    }
  }
}
?>
gzech at SPAMFILTER dot eso dot org 21-Feb-2007 11:09
If you want to suppress output warnings from loadHTMLFile($url), put an @ sign in front. This even works in:
<?php
$load = @$dom->loadHTMLFile($url);
?>
andy at carobert dot com 25-May-2005 10:24
This puts the HTML into a DOM object which can be parsed by individual tags, attributes, etc..  Here is an example of getting all the 'href' attributes and corresponding node values out of the 'a' tag. Very cool....

<?php
$myhtml = <<<EOF
<html>
<head>
<title>My Page</title>
</head>
<body>
<p><a href="/mypage1">Hello World!</a></p>
<p><a href="/mypage2">Another Hello World!</a></p>
</body>
</html>
EOF;

$doc = new DOMDocument();
$doc->loadHTML($myhtml);

$tags = $doc->getElementsByTagName('a');

foreach ($tags as $tag) {
       echo $tag->getAttribute('href').' | '.$tag->nodeValue."\n";
}
?>

This should output:

/mypage1 | Hello World!
/mypage2 | Another Hello World!
bens at effortlessis dot com 09-Apr-2005 02:11
Note that this function doesn't parse the individual tags WITHIN the html file - it's all loaded as a "black box", and you end up with an XML widget that comprises nothing but the complete chunk of HTML.

I was hoping it would function as a sort of HTML-validator/parser, but that doesn't seem to be the case.


Load XML from a string

  • ≪ DOMDocument::loadHTMLFile
  • DOMDocument::normalizeDocument ≫
  • PHP Manual
  • DOMDocument
  • Load XML from a string

DOMDocument::loadXML

(PHP 5, PHP 7)

DOMDocument::loadXML — Load XML from a string

설명

public mixed DOMDocument::loadXML ( string $source [, int $options = 0 ] )

Loads an XML document from a string.

인수

source

The string containing the XML.

options

Bitwise OR of the libxml option constants.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다. If called statically, returns a DOMDocument실패 시 FALSE를 반환합니다.

오류/예외

If an empty string is passed as the source, a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml's error handling functions.

이 메소드는 정적으로 호출할 수있지만, E_STRICT 오류가 발생합니다.

예제

Example #1 Creating a Document

<?php
$doc = new DOMDocument();
$doc->loadXML('<root><node/></root>');
echo $doc->saveXML();
?>

Example #2 Static invocation of loadXML

<?php
// Issues an E_STRICT error
$doc = DOMDocument::loadXML('<root><node/></root>');
echo $doc->saveXML();
?>

참고

  • DOMDocument::load() - Load XML from a file
  • DOMDocument::save() - Dumps the internal XML tree back into a file
  • DOMDocument::saveXML() - Dumps the internal XML tree back into a string

User Contributed Notes

Gustavo L. Fabro 15-Nov-2013 01:41
Always remember that with the default parameters this function doesn't handle well large files, i.e. if a text node is longer than 10Mb it can raise an exception stating:

DOMDocument::loadXML(): internal error Extra content at the end of the document in Entity

even though the XML is fine.

The cause is a definition in parserInternals.h of lixml:
#define XML_MAX_TEXT_LENGTH 10000000

To allow the function to process larger files, pass the LIBXML_PARSEHUGE as an option and it will work just fine:

$domDocument->loadXML($xml, LIBXML_PARSEHUGE);
szalma dot laszlo at zengo dot eu 27-Oct-2009 05:14
Loading from a string works fine without the <?xml version="1.0" encoding="utf-8"?> header, but in this case the xmlEncoding won't be set, and this makes the utf-8 characters (international, and special characters) to be encoded as hexa entities when saved with saveXML()!
remacg 26-Feb-2009 04:33
Instead of doing this:

<?php
$str = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE root [
<!ENTITY nbsp "&#160;">
]>
<div>This&nbsp;is a non-breaking space.</div>
XML;

$dd2 = new DOMDocument();
$dd2->loadXML($str);

echo $dd2->saveXML();
?>

simply use:

loadHTML() rather than loadXML().
olalonde at NOSPAM dot gmail dot com 22-Mar-2008 02:00
For some reason, when you set DOMDocument's property 'recover' to true, using '@' to mask errors thrown by loadXml() won't work.

Here's my workaround:

function maskErrors() {}
set_error_handler('maskErrors');
$dom->loadXml($xml);
restore_error_handler();

You could also simply do this: error_reporting(0); and then set back error_reporting to its original state.
Stuart Grimshaw 26-Jun-2007 10:57
Possible values for the options parameter can be found here:

http://us3.php.net/manual/en/ref.libxml.php#libxml.constants
Marc Liyanage 18-Jun-2007 04:08
The documentation states that loadXML can be called statically, but this is misleading. This feature seems to be a special case hack and its use seems to be discouraged according to http://bugs.php.net/bug.php?id=41398.

Calling the method statically will fail with an error if the code runs with E_STRICT error reporting enabled.

The documentation should be changed to make it clear that a static call is against recommended practice and won't work with E_STRICT.
jazzslider at hotmail dot com 30-Apr-2007 10:14
When using loadXML() to parse a string that contains entity references (e.g., &nbsp;), be sure that those entity references are properly declared through the use of a DOCTYPE declaration; otherwise, loadXML() will not be able to interpret the string.

Example:
<?php
$str = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<div>This&nbsp;is a non-breaking space.</div>
XML;

$dd1 = new DOMDocument();
$dd1->loadXML($str);

echo $dd1->saveXML();
?>

Given the above code, PHP will issue a Warning about the entity 'nbsp' not being properly declared.  Also, the call to saveXML() will return nothing but a trimmed-down version of the original processing instruction...everything else is gone, and all because of the undeclared entity.

Instead, explicitly declare the entity first:
<?php
$str = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE root [
<!ENTITY nbsp "&#160;">
]>
<div>This&nbsp;is a non-breaking space.</div>
XML;

$dd2 = new DOMDocument();
$dd2->loadXML($str);

echo $dd2->saveXML();
?>

Since the 'nbsp' entity is defined in the DOCTYPE, PHP no longer issues that Warning; the string is now well-formed, and loadXML() understands it perfectly.

You can also use references to external DTDs in the same way (e.g., <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">), which is particularly important if you need to do this for many different documents with many different possible entities.

Also, as a sidenote...entity references created by createEntityReference() do not need this kind of explicit declaration.
shaoyu73 at gmail dot com 16-Mar-2007 05:53
earth at anonymous dot com,

preserveWhiteSpace property needs to be set to false for formatOutput to work properly, for some reason.

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->loadXML($xmlStr);
...
$element->appendChild(...);
...
$dom->formatOutput = true;
$xmlStr = $dom->saveXML();
echo $xmlStr;

This would format the output nicely.
Gavin Sinai gsinai at gmx dot net 30-Aug-2006 04:34
loadXml reports an error instead of throwing an exception when the xml is not well formed. This is annoying if you are trying to to loadXml() in a try...catch statement. Apparently its a feature, not a bug, because this conforms to a spefication.

If you want to catch an exception instead of generating a report, you could do something like

<?php
function HandleXmlError($errno, $errstr, $errfile, $errline)
{
    if ($errno==E_WARNING && (substr_count($errstr,"DOMDocument::loadXML()")>0))
    {
        throw new DOMException($errstr);
    }
    else
        return false;
}

function XmlLoader($strXml)
{
    set_error_handler('HandleXmlError');
    $dom = new DOMDocument();
    $dom->loadXml($strXml);
    restore_error_handler();
    return $dom;
 }

?>

Returning false in function HandleXmlError() causes a fallback to the default error handler.
mp at webfactory dot de 08-Aug-2006 05:26
While loadXML() expects its input to have a leading XML processing instruction to deduce the encoding used, there's no such concept in (non-XML) HTML documents. Thus, the libxml library underlying the DOM functions peeks at the <META> tags to figure out the encoding used.

See http://xmlsoft.org/encoding.html.
earth at anonymous dot com 12-Apr-2006 11:28
Note that loadXML crops off beginning and trailing whitespace and linebreaks.

When using loadXML and appendChild to add a chunk of XML to an existing document, you may want to force a linebreak between the end of the XML chunk and the next line (usually a close tag) in the output file:

$childDocument = new DOMDocument;
$childDocument>preserveWhiteSpace = true;
$childDocument->loadXML(..XML-Chunk..);
$mNewNode = $mainDOcument->importNode($childDocument->documentElement, true);
$ParentNode->appendChild($mNewNode);
$ParentNode->appendChild($mainDocument->createTextNode("\\n  ");

Although it is said that DOM should not be used to make 'pretty' XML output, it is something I struggled with to get something that was readable for testing.  Another solution is to use the createDocumentFragment()->appendXML(..XML-Chunk..) instead, which seems not to trim off linebreaks like DOMDocument->loadXML() does.


Normalizes the document

  • ≪ DOMDocument::loadXML
  • DOMDocument::registerNodeClass ≫
  • PHP Manual
  • DOMDocument
  • Normalizes the document

DOMDocument::normalizeDocument

(PHP 5, PHP 7)

DOMDocument::normalizeDocument — Normalizes the document

설명

public void DOMDocument::normalizeDocument ( void )

This method acts as if you saved and then loaded the document, putting the document in a "normal" form.

반환값

값을 반환하지 않습니다.

참고

  • » The DOM Specification
  • DOMNode::normalize() - Normalizes the node

User Contributed Notes

lingasamy dot sakthivel at gmail dot com 16-Apr-2015 02:44
You can use this in this way.

$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadHTML($content); // html content
$xpath = new DOMXPath($doc);
$doc->normalizeDocument();

Note that, you can use $doc->normalizeDocument(); at any point of time.


Register extended class used to create base node type

  • ≪ DOMDocument::normalizeDocument
  • DOMDocument::relaxNGValidate ≫
  • PHP Manual
  • DOMDocument
  • Register extended class used to create base node type

DOMDocument::registerNodeClass

(PHP 5 >= 5.2.0, PHP 7)

DOMDocument::registerNodeClass — Register extended class used to create base node type

설명

public bool DOMDocument::registerNodeClass ( string $baseclass , string $extendedclass )

This method allows you to register your own extended DOM class to be used afterward by the PHP DOM extension.

This method is not part of the DOM standard.

인수

baseclass

The DOM class that you want to extend. You can find a list of these classes in the chapter introduction.

extendedclass

Your extended class name. If NULL is provided, any previously registered class extending baseclass will be removed.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
5.2.2 | Prior to 5.2.2, a previously registered extendedclass had to be unregistered before being able to register a new class extending the same baseclass.

예제

Example #1 Adding a new method to DOMElement to ease our code

<?php

class myElement extends DOMElement {
   function appendElement($name) {
      return $this->appendChild(new myElement($name));
   }
}

class myDocument extends DOMDocument {
   function setRoot($name) {
      return $this->appendChild(new myElement($name));
   }
}

$doc = new myDocument();
$doc->registerNodeClass('DOMElement', 'myElement');

// From now on, adding an element to another costs only one method call !
$root = $doc->setRoot('root');
$child = $root->appendElement('child');
$child->setAttribute('foo', 'bar');

echo $doc->saveXML();

?>

위 예제의 출력:

<?xml version="1.0"?>
<root><child foo="bar"/></root>

Example #2 Retrieving elements as custom class

<?php
class myElement extends DOMElement {
    public function __toString() {
        return $this->nodeValue;
    }
}

$doc = new DOMDocument;
$doc->loadXML("<root><element><child>text in child</child></element></root>");
$doc->registerNodeClass("DOMElement", "myElement");

$element = $doc->getElementsByTagName("child")->item(0);
var_dump(get_class($element));

// And take advantage of the __toString method..
echo $element;
?>

위 예제의 출력:

string(9) "myElement"
text in child

Example #3 Retrieving owner document

When instantiating a custom DOMDocument the ownerDocument property will refer to the instantiated class, meaning there is no need (and in fact not possible) to use DOMDocument::registerNodeClass() with DOMDocument

<?php
class myDOMDocument extends DOMDocument {
}

class myOtherDOMDocument extends DOMDocument {
}

// Create myDOMDocument with some XML
$doc = new myDOMDocument;
$doc->loadXML("<root><element><child>text in child</child></element></root>");

$child = $doc->getElementsByTagName("child")->item(0);

// The current owner of the node is myDOMDocument
var_dump(get_class($child->ownerDocument));

// Import a node from myDOMDocument
$newdoc = new myOtherDOMDocument;
$child = $newdoc->importNode($child);

// The new owner of the node has changed to myOtherDOMDocument
var_dump(get_class($child->ownerDocument));
?>

위 예제의 출력:

string(13) "myDOMDocument"
string(18) "myOtherDOMDocument"

User Contributed Notes

crh3675 at gmail dot com 26-Sep-2009 05:23
Creating innerHTML and outerHTML

<?php

class DOMHTMLElement extends DOMElement
{
    function __construct() { parent::__construct();}

    public function innerHTML()
    {
        $doc = new DOMDocument();
      foreach ($this->childNodes as $child){
          $doc->appendChild($doc->importNode($child, true));
        }
        $content = $doc->saveHTML();
        return $content;
    }

    public function outerHTML()
    {
        $doc = new DOMDocument();
        $doc->appendChild($doc->importNode($this, true));
        $content = $doc->saveHTML();
        return $content;
    }
}

$dom = DOMDocument::loadHTMLFile($file);
$dom->registerNodeClass('DOMElement','DOMHTMLElement');

if($dom)
{
    $xpath = new DOMXpath($dom);
    $regions = $xpath->query("//*[contains(@class, 'editable')]");
    $content = '';

    foreach($regions as $region){
        $content .= $region->outerHTML();
    }

    return $content;

}else{
    throw new Exception('Cannot parse HTML.  Please verify the syntax is correct.');
}
?>
arnold at adaniels dot nl 23-Jul-2009 12:10
Note than save and saveXML are not affected by __toString().


Performs relaxNG validation on the document

  • ≪ DOMDocument::registerNodeClass
  • DOMDocument::relaxNGValidateSource ≫
  • PHP Manual
  • DOMDocument
  • Performs relaxNG validation on the document

DOMDocument::relaxNGValidate

(PHP 5, PHP 7)

DOMDocument::relaxNGValidate — Performs relaxNG validation on the document

설명

public bool DOMDocument::relaxNGValidate ( string $filename )

Performs » relaxNG validation on the document based on the given RNG schema.

인수

filename

The RNG file.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • DOMDocument::relaxNGValidateSource() - Performs relaxNG validation on the document
  • DOMDocument::schemaValidate() - Validates a document based on a schema
  • DOMDocument::schemaValidateSource() - Validates a document based on a schema
  • DOMDocument::validate() - Validates the document based on its DTD

User Contributed Notes

scarywound at gmail dot com 17-Oct-2012 08:39
Keep in mind that this method can't handle schemas in compact syntax!
mitchell dot beaumont at mitchellbeaumont dot com 27-Aug-2007 04:28
It's worth noting that when validation fails this function also triggers a warning.

You can suppress the warning using the set_error_handler and restore_error_handler functions.


Performs relaxNG validation on the document

  • ≪ DOMDocument::relaxNGValidate
  • DOMDocument::save ≫
  • PHP Manual
  • DOMDocument
  • Performs relaxNG validation on the document

DOMDocument::relaxNGValidateSource

(PHP 5, PHP 7)

DOMDocument::relaxNGValidateSource — Performs relaxNG validation on the document

설명

public bool DOMDocument::relaxNGValidateSource ( string $source )

Performs » relaxNG validation on the document based on the given RNG source.

인수

source

A string containing the RNG schema.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • DOMDocument::relaxNGValidate() - Performs relaxNG validation on the document
  • DOMDocument::schemaValidate() - Validates a document based on a schema
  • DOMDocument::schemaValidateSource() - Validates a document based on a schema
  • DOMDocument::validate() - Validates the document based on its DTD

User Contributed Notes

There are no user contributed notes for this page.


Dumps the internal XML tree back into a file

  • ≪ DOMDocument::relaxNGValidateSource
  • DOMDocument::saveHTML ≫
  • PHP Manual
  • DOMDocument
  • Dumps the internal XML tree back into a file

DOMDocument::save

(PHP 5, PHP 7)

DOMDocument::save — Dumps the internal XML tree back into a file

설명

public int DOMDocument::save ( string $filename [, int $options ] )

Creates an XML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.

인수

filename

The path to the saved XML document.

options

Additional Options. Currently only LIBXML_NOEMPTYTAG is supported.

반환값

Returns the number of bytes written or FALSE if an error occurred.

변경점

버전 | 설명
5.1.0 | Added the options parameter

예제

Example #1 Saving a DOM tree into a file

<?php

$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;

$root = $doc->createElement('book');
$root = $doc->appendChild($root);

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);

echo 'Wrote: ' . $doc->save("/tmp/test.xml") . ' bytes'; // Wrote: 72 bytes

?>

참고

  • DOMDocument::saveXML() - Dumps the internal XML tree back into a string
  • DOMDocument::load() - Load XML from a file
  • DOMDocument::loadXML() - Load XML from a string

User Contributed Notes

dubois-holvoet dot patrick at neuf dot fr 26-Feb-2016 05:13
I just intend to bring my little contribution to the use of DOMDocument::save() function along with PHP serialize() function.

Sometimes you could have to serialize a PHP object before you put it into a XML structure (or other..). And then I suppose you could have to save this XML structure in a file. Of course, you will further need to read this file and unserialize it before to use its content.

The problem appears when there are some properties described as Protected in the object to be serialized.

As it's written in the PHP documentation, serialize function add for those protected properties an asterisk before the name of the property along with a NULL character on each side of the asterisk.
In this case, the DOMDocument::save function will stop the save operation just before the first NULL character met in the string to be saved, because PHP consider it as a potential risk.
So, after that, the unserialize operation of the file become impossible.

To solve that, here is a first solution to apply :

For the serialize , before the DOMDocument::save() ::
$data_serial = explode("\x00\x2A\x00", serialize($object));  //take off the 'NULL * NULL' string
$data_serial = implode("\x5C\x30\x2A\x5C\x30", $data_serial);    //and replace with '\0*\0' string

Before the unserialize :
$data_serial = explode("\x5C\x30\x2A\x5C\x30", $serialized_object);    // take off the '\0*\0' string
$serial_object = implode("\x00\x2A\x00", $data_serial);      // and replace with 'NULL * NULL' string as included by the previous serialize

And a second solution :

For the serialize, before the DOMDocument::save() :
$serialized_object = addslashes(serialize($object)) ;

Before re-using the object in your application :
$object = unserialize(stripslashes($serialized_object)) ;

Hope this will help some.
ludvig at ergopedia dot no 31-Oct-2014 07:21
When creating a DOMDocument from scratch and saving it, the encoding will be utf-8, although it's declared as iso-8859-1.

When loading an XML-file declared and saved as iso-8859-1, php will keep the correct encoding when it's saved after changes are made.

Php converts files declared as iso-8859-1 to utf-8 internally. To add text containing special characters, the text must be encoded as utf-8. When the document is saved, special characters are converted to iso-8859-1.

To save an xml created from scratch, use fopen/fwrite and utf8_decode:

   $doc = new DOMDocument('1.0', 'iso-8859-1');

//Add som nodes and text with apropriate methods

   $f = fopen('xmlfile.xml', 'w+');
   fwrite($f, utf8_decode($doc->saveXML()));
   fclose($f);
john at johnreid dot it 06-Aug-2013 06:42
I had been hitting my head against the wall when I couldn't get an edited XML file to save due to file permissions, even though I knew that it was OK.

Eventually I realised that for some odd reason, when I went to save the XML file, it would try to write the file to the root directory.

As such, use realpath() to keep the complete system path to the XML file when you load it:

<?php
$myfile = 'myxml.xml';
$myfile = realpath($myfile);
$doc = new DOMDocument('1.0');
$doc->load($myfile);

// Let's just add a couple of elements for good measure
$root = $doc->documentElement;

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$text = $doc->createTextNode('This is a title');
$text = $title->appendChild($text);

$doc->save($myfile);
?>
ss at littlerain dot com 01-Feb-2009 09:05
In addition to:
DOMDocument->formatOutput = true
DOMDocument->preserveWhiteSpace = false

...you also have to ensure you don't add text nodes as siblings of element nodes, or formatOutput won't work.
chenel324 at gmail dot com 19-Oct-2008 03:38
It took me forever to discover that
DOMDocument->formatOutput = true
will only have an effect on documents that are loaded from disk if one also sets
DOMDocument->preserveWhiteSpace = false ....

Hope this saves somebody a headache.
siegparr at NOSPAM dot web dot de 07-Jul-2006 05:00
The XML parser converts the text of an XML document into UTF-8, even if you have set the character encoding of the XML, for example as a second parameter of the DOMDocument constructor. After parsing the XML with the load() command all its texts have been converted to UTF-8.

In case you append text nodes with special characters (e. g. Umlaut) to your XML document you should therefore use utf8_encode() with your text to convert it into UTF-8 before you append the text to the document. Otherwise you will get an error message like "output conversion failed due to conv error" at the save() command. See example below:

<?php
// Text to insert into XML below
$txt = "a text with special characters like 'a', 'ß', 'U' etc.";

// Create Instance of DOMDocument
$dom =  new DOMDocument;
// Load XML file
// Was created before with DOMDocument('1.0', 'iso-8859-1')
$dom = $dom->load("file.xml");
// Find the parent node
$parent = $dom->documentElement;
// Create Instance of DomXPath
$xpath = new DomXPath($dom);
// new node will be inserted before this node
$next = $xpath->query("//parentnode/childnode");
// Create the new element
$new_elem = $dom->createElement('new_elem');
// Insert the new element
$parent->insertBefore($new_elem, $next->item(0));
// DOMXML = utf-8! (will be converted to iso-8859-1 only at 'save()')
// prevents error message "output conversion failed due to conv error" at 'save()'
$txt = utf8_encode($txt);
// Create new text node with utf-8 encoded string
$nodetext = $dom->createTextNode("$txt");
// Append text node to new element
$nodetext = $new_elem->appendChild($nodetext);
// save
$dom->save("file.xml");
?>

Hope this helps someone.

siegparr


Dumps the internal document into a string using HTML formatting

  • ≪ DOMDocument::save
  • DOMDocument::saveHTMLFile ≫
  • PHP Manual
  • DOMDocument
  • Dumps the internal document into a string using HTML formatting

DOMDocument::saveHTML

(PHP 5, PHP 7)

DOMDocument::saveHTML — Dumps the internal document into a string using HTML formatting

설명

public string DOMDocument::saveHTML ([ DOMNode $node = NULL ] )

Creates an HTML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.

인수

node

Optional parameter to output a subset of the document.

반환값

Returns the HTML, or FALSE if an error occurred.

변경점

버전 | 설명
5.3.6 | The node parameter was added.

예제

Example #1 Saving a HTML tree into a string

<?php

$doc = new DOMDocument('1.0');

$root = $doc->createElement('html');
$root = $doc->appendChild($root);

$head = $doc->createElement('head');
$head = $root->appendChild($head);

$title = $doc->createElement('title');
$title = $head->appendChild($title);

$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);

echo $doc->saveHTML();

?>

참고

  • DOMDocument::saveHTMLFile() - Dumps the internal document into a file using HTML formatting
  • DOMDocument::loadHTML() - Load HTML from a string
  • DOMDocument::loadHTMLFile() - Load HTML from a file

User Contributed Notes

contact at cathexis dot de 31-Aug-2016 07:41
If you load HTML from a string ensure the charset is set.

<?php
...
$html_src = '<html><head><meta content="text/html; CHARSET=ks_c_5601-1987" http-equiv="Content-Type"></head><body>';
$html_src .= '...';
...
?>

Otherwise the charset will be ISO-8859-1!
tomas dot strejcek at ghn dot cz 20-Aug-2016 09:34
As of PHP 5.4 and Libxml 2.6, there is currently simpler approach:

when you load html as this

$html->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

in the output, there will be no doctype, html or body tags
Anonymous 12-Dec-2015 12:38
To solve the script tag problem just add an empty text node to the script node and DOMDocument will render <script src="your.js"></script> nicely.
RiKdnUA at mail dot ru 30-Apr-2015 12:49
Tested in PHP 5.2.9-2 and PHP 5.2.17.
saveHTML() игнорирует свойство DOMDocument->encoding. Метод saveHTML() сохраняет html-документ в кодировке, которая указана в теге <meta> исходного (загруженного) html-документа.
saveHTML() ignores property DOMDocument->encoding. Method saveHTML() saves the html-document encoding, which is specified in the tag <meta> source (downloaded) html-document.
Example:
file.html. Кодировка файла должна совпадать с указанной в теге <meta>. The encoding of the file must match the specified tag <meta>.
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title>Russian language</title></head>
<body>Русский язык</body></html>

<?php
error_reporting(-1);
$document=new domDocument('1.0', 'UTF-8');
$document->preserveWhiteSpace=false;
$document->loadHTMLFile('file.html');
$document->formatOutput=true;
$document->encoding='UTF-8';
$htm=$document->saveHTML();
echo"Записано байт. Recorded bytes: ".file_put_contents('file_new.html', $htm);
?>
file_new.html будет в кодировке Windows-1251 (НЕ в UTF-8).
file_new.html will be encoded in Windows-1251 (not in UTF-8).

saveHTML() и file_put_contents() позволяют преодолеть недостаток метода saveHTMLFile().
Смотрите мой комментарий к методу saveHTMLFile().
saveHTML() and file_put_contents() allows you to overcome the lack of a method saveHTMLFile().
See my comment on the method saveHTMLFile().
http://php.net/manual/ru/domdocument.savehtmlfile.php
qrworld.net 12-Nov-2014 01:34
In this post http://softontherocks.blogspot.com/2014/11/descargar-el-contenido-de-una-url_11.html I found a simple way to get the content of a URL with DOMDocument, loadHTMLFile and saveHTML().

function getURLContent($url){
    $doc = new DOMDocument;
    $doc->preserveWhiteSpace = FALSE;
    @$doc->loadHTMLFile($url);
    return $doc->saveHTML();
}
alvaro at demogracia dot com 29-Mar-2011 08:04
Since PHP/5.3.6, DOMDocument->saveHTML() accepts an optional DOMNode parameter similarly to DOMDocument->saveXML():

http://bugs.php.net/bug.php?id=39771
Yajo 24-Nov-2010 04:21
Another way to workaround the <script/> problem is putting a semicolon (;) inside the script element.
Anonymous 09-Feb-2010 04:52
If you want a simpler way to get around the <script> tag problem try:

<?php

  $script = $doc->createElement ('script');\
  // Creating an empty text node forces <script></script>
  $script->appendChild ($doc->createTextNode (''));
  $head->appendChild ($script);

?>
Anonymous 13-May-2009 04:35
To avoid script tags from being output as <script />, you can use the DOMDocumentFragment class:

<?php

$doc = new DOMDocument();
$doc -> loadXML($xmlstring);
$fragment = $doc->createDocumentFragment();
/* Append the script element to the fragment using raw XML strings (will be preserved in their raw form) and if succesful proceed to insert it in the DOM tree */
if($fragment->appendXML("<script type='text/javascript' src='$source'></script>") {
  $xpath = new DOMXpath($doc);
  $resultlist = $xpath->query("//*[local-name() = 'html']/*[local-name() = 'head']"); /* namespace-safe method to find all head elements which are childs of the html element, should only return 1 match */
  foreach($resultlist as $headnode)  // insert the script tag
     $headnode->appendChild($fragment);
}
$doc->saveXML(); /* and our script tags will still be <script></script> */

?>
Bart Feenstra 18-Jan-2009 07:17
I am using this solution to prevent tags and the doctype from being added to the HTML string automatically:

<?php
$html = '<h1>Hello world!</h1>';
$html = '<div>' . $html . '</div>';
$doc = new DOMDocument;
$doc->loadHTML($html);
echo substr($doc->saveXML($doc->getElementsByTagName('div')->item(0)), 5, -6)

// Outputs: "<h1>Hello world!</h1>"
?>
m at hbblogs daught calm 18-Aug-2008 05:41
This method, as of 5.2.6, will automatically add <html><body> and <!DOCTYPE> tags to the document if they are missing, without asking whether you want them. In my application, I needed to use the DOM methods to manipulate just a fragment of html, so these tags were rather unhelpful.

Here's a simple hack to remove them in case, like me, all you wanted to do was perform a few operations on an HTML fragment.

$html_fragment = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
Anonymous 26-Apr-2008 05:15
<?php
function getDOMString($retNode) {
  if (!$retNode) return null;
  $retval = strtr($retNode-->ownerDocument->saveXML($retNode),
  array(
    '></area>' => ' />',
    '></base>' => ' />',
    '></basefont>' => ' />',
    '></br>' => ' />',
    '></col>' => ' />',
    '></frame>' => ' />',
    '></hr>' => ' />',
    '></img>' => ' />',
    '></input>' => ' />',
    '></isindex>' => ' />',
    '></link>' => ' />',
    '></meta>' => ' />',
    '></param>' => ' />',
    'default:' => '',
    // sometimes, you have to decode entities too...
    '&quot;' => '&#34;',
    '&amp;' =>  '&#38;',
    '&apos;' => '&#39;',
    '&lt;' =>   '&#60;',
    '&gt;' =>   '&#62;',
    '&nbsp;' => '&#160;',
    '&copy;' => '&#169;',
    '&laquo;' => '&#171;',
    '&reg;' =>   '&#174;',
    '&raquo;' => '&#187;',
    '&trade;' => '&#8482;'
  ));
  return $retval;
}
?>
mjaque at ilkebenson dot com 19-Feb-2008 08:34
DOMDocument->saveXML() doesn't generate a proper XHTML format either.

There is a problem with "script" empty elements. For example:

This will be the code generated by saveXML, with an empty script tag.

<html>
  <head>
    <script type="text/JavaScript" src="myScript.js"/>
  </head>
  <body>
    <p>I will not appear</p>
    <script type="text/JavaScript">
    alert("Not working");
    </script>
  </body>
</html>

I don't know if this is valid XHTML (W3C Validator doesn't complain), but both FF 2.0 and IE 6 will not render it properly. Both will use </script> as the closing tag for the first script causing js errors and ignoring in between elements.

You can post-process saveXML string in order to close empty tags with the following function:

<?php
    function cerrarTag($tag, $xml){
        $indice = 0;
        while ($indice< strlen($xml)){
            $pos = strpos($xml, "<$tag ", $indice);
            if ($pos){
                $posCierre = strpos($xml, ">", $pos);
                if ($xml[$posCierre-1] == "/"){
                    $xml = substr_replace($xml, "></$tag>", $posCierre-1, 2);
                }
                $indice = $posCierre;
            }
            else break;
        }
        return $xml;
    }
?>

At least script and select empty elements should be closed. This example shows how it can be used:

<?php
    define("CABECERA_XHTML", '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');

  $xhtml = $docXML->saveXML($docXML->documentElement);
  $xhtml = cerrarTag("script", $xhtml);
  $xhtml = cerrarTag("select", $xhtml);
  $xhtml = CABECERA_XHTML."\n".$xhtml;
  echo $xhtml;
?>
archanglmr at yahoo dot com 28-Nov-2007 12:28
If created your DOMDocument object using loadHTML() (where the source is from another site) and want to pass your changes back to the browser you should make sure the HTTP Content-Type header matches your meta content-type tags value because modern browsers seem to ignore the meta tag and trust just the HTTP header. For example if you're reading an ISO-8859-1 document and your web server is claiming UTF-8 you need to correct it using the header() function.

<?php
header('Content-Type: text/html; charset=iso-8859-1');
?>
xoplqox 20-Nov-2007 08:07
XHTML:

If the output is XHTML use the function saveXML().

Output example for saveHTML:

<select name="pet" size="3" multiple>
    <option selected>mouse</option>
    <option>bird</option>
    <option>cat</option>
</select>

XHTML conform output using saveXML:

<select name="pet" size="3" multiple="multiple">
    <option selected="selected">mouse</option>
    <option>bird</option>
    <option>cat</option>
</select>
tyson at clugg dot net 22-Apr-2005 02:44
<?php
// Using DOM to fix sloppy HTML.
// An example by Tyson Clugg <tyson@clugg.net>
//
// vim: syntax=php expandtab tabstop=2

function tidyHTML($buffer)
{
  // load our document into a DOM object
  $dom = @DOMDocument::loadHTML($buffer);
  // we want nice output
  $dom->formatOutput = true;
  return($dom->saveHTML());
}

// start output buffering, using our nice
// callback funtion to format the output.
ob_start("tidyHTML");

?>
<html>
<p>It's like comparing apples to oranges.
</html>
<?php

// this will be called implicitly, but we'll
// call it manually to illustrate the point.
ob_end_flush();

?>

The above code takes out sloppy HTML:
 <html>
 <p>It's like comparing apples to oranges.
 </html>

And cleans it up to the following:
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
 <html><body><p>It's like comparing apples to oranges.
 </p></body></html>


Dumps the internal document into a file using HTML formatting

  • ≪ DOMDocument::saveHTML
  • DOMDocument::saveXML ≫
  • PHP Manual
  • DOMDocument
  • Dumps the internal document into a file using HTML formatting

DOMDocument::saveHTMLFile

(PHP 5, PHP 7)

DOMDocument::saveHTMLFile — Dumps the internal document into a file using HTML formatting

설명

public int DOMDocument::saveHTMLFile ( string $filename )

Creates an HTML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.

인수

filename

The path to the saved HTML document.

반환값

Returns the number of bytes written or FALSE if an error occurred.

예제

Example #1 Saving a HTML tree into a file

<?php

$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;

$root = $doc->createElement('html');
$root = $doc->appendChild($root);

$head = $doc->createElement('head');
$head = $root->appendChild($head);

$title = $doc->createElement('title');
$title = $head->appendChild($title);

$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);

echo 'Wrote: ' . $doc->saveHTMLFile("/tmp/test.html") . ' bytes'; // Wrote: 129 bytes

?>

참고

  • DOMDocument::saveHTML() - Dumps the internal document into a string using HTML formatting
  • DOMDocument::loadHTML() - Load HTML from a string
  • DOMDocument::loadHTMLFile() - Load HTML from a file

User Contributed Notes

RiKdnUA at mail dot ru 28-Jul-2013 01:15
saveHTMLFile() always saves the file in UTF-8. Even if the DOMDocument->encoding explicitly prescribe different from UTF-8 encoding. All "non-Latin" characters will be converted to HTML-entities. Tested in PHP 5.2.9-2 and PHP 5.2.17. Example:

<?php
$document=new domDocument('1.0', 'WINDOWS-1251');
$document->loadHTML('<html><head><title>Russian language</title></head><body>Русский язык</body></html>');
$document->formatOutput=true;
$document->encoding='WINDOWS-1251';
echo "Записано байт. Recorded bytes: ".$document->saveHTMLFile('html.html');
?>

Method recorded file in UTF-8 encoding. The contents of the file html.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; CHARSET=ks_c_5601-1987">
<title>Russian language</title>
</head>
<body>Ðonneee ycue</body>
</html>
naebeth at hotmail dot NOSPAM dot com 24-Sep-2012 07:03
Not mentioned in the documentation is the fact that using DOMDocument::saveHTMLFile() will automatically overwrite the contents if an existing file is used - with no notice, warning or error thrown.

Make sure you check the filename before using this function so that you don't accidentally overwrite important files.

Example:
<?php

$file = fopen('test.html', 'w');
fwrite($file, 'this is some text');
fclose($file);

$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->loadHTML('<html><head><title>Test</title></head><body></body></html>');
$doc->saveHTMLFile('test.html');

// test.html
/*
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; CHARSET=ks_c_5601-1987">
<title>Test</title>
</head>
<body></body>
</html>
*/

?>

If you're dynamically generating a series of pages using DOMDocument objects, make sure you are also dynamically generating the file or directory names using something that can't easily be confused for an existing file/folder, or check if the desired path already exists before saving so that you don't accidentally delete previous files.
deep42thouSPAMght42 at y_a_h_o_o dot com 14-Jan-2011 12:46
I foolishly assumed that this function was equivalent to
<?php
file_put_contents($filename, $document->saveHTML());
?>
but there are differences in the generated HTML:
<?php
$doc = new DOMDocument();
$doc->loadHTML(
    '<html><head><title>Test</title></head><body></body></html>'
);
$doc->encoding = 'iso-8859-1';

echo $doc->saveHTML();
#<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
#<html>
#<head><title>Test</title></head>
#<body></body>
#</html>

$doc->saveHTMLFile('output.html');
#<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
#<html><head><meta http-equiv="Content-Type" content="text/html; CHARSET=ks_c_5601-1987"><title>Test</title></head><body></body></html>

?>
Note that saveHTMLFile() adds a UTF-8 meta tag despite the ISO-8859-1 document encoding.


Dumps the internal XML tree back into a string

  • ≪ DOMDocument::saveHTMLFile
  • DOMDocument::schemaValidate ≫
  • PHP Manual
  • DOMDocument
  • Dumps the internal XML tree back into a string

DOMDocument::saveXML

(PHP 5, PHP 7)

DOMDocument::saveXML — Dumps the internal XML tree back into a string

설명

public string DOMDocument::saveXML ([ DOMNode $node [, int $options ]] )

Creates an XML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.

인수

node

Use this parameter to output only a specific node without XML declaration rather than the entire document.

options

Additional Options. Currently only LIBXML_NOEMPTYTAG is supported.

반환값

Returns the XML, or FALSE if an error occurred.

오류/예외

DOM_WRONG_DOCUMENT_ERR

Raised if node is from another document.

변경점

버전 | 설명
5.1.0 | Added the options parameter

예제

Example #1 Saving a DOM tree into a string

<?php

$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;

$root = $doc->createElement('book');
$root = $doc->appendChild($root);

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);

echo "Saving all the document:\n";
echo $doc->saveXML() . "\n";

echo "Saving only the title part:\n";
echo $doc->saveXML($title);

?>

위 예제의 출력:

Saving all the document:
<?xml version="1.0"?>
<book>
  <title>This is the title</title>
</book>

Saving only the title part:
<title>This is the title</title>

참고

  • DOMDocument::save() - Dumps the internal XML tree back into a file
  • DOMDocument::load() - Load XML from a file
  • DOMDocument::loadXML() - Load XML from a string

User Contributed Notes

Alexander Fedulin 05-Mar-2014 05:29
As @fbernodi said earlier, there is a problem with saveXML of DOMNode when you have a number of defined namespaces. The simple solution for this:

<code>
   // $node is some node of some other document
   $temp_document = new DOMDocument('1.0', 'utf-8');
   $temp_document->appendChild($temp_document->importNode($node, true));
   echo $temp_document->saveXML();
</code>
fbernoldi at gmail dot com 02-Aug-2013 05:24
Hello,

I had an unexpected behavior in some parsing code, because of this saveXML method that is saving the tags without namespace specification. If you try to load into a DOMDocument a string saved from a DOMNode that has a referenced namespace but it the original document it was not defined in that element, you get a not well formed xml. In the example you can see the issue and a possible solution, the other solution may be add to the ChildElement node the namespace reference (<testNS:ChildElement testNS="http://example.org">), but in my scenario it was not needed.

<?php

//Test XML
$test_xml =
"<?xml version=\"1.0\"?>
<testNS:RootNode xmlns:testNS=\"http://example.org\">
    <testNS:ChildElement>
        <testNS:AnotherChildElement>I'm a Another Child node.</testNS:AnotherChildElement>
    </testNS:ChildElement>
</testNS:RootNode>";

//We define our Test DOMDocument
$domDoc = new DOMDocument("1.0");
$domDoc->loadXML($test_xml);

//We use xpath to search ChildElement:
$domXPath = new DOMXPath($domDoc);
$domXPath->registerNamespace("testNS", "http://example.org");
$DOMNodeList_ChildElement = $domXPath->query("//testNS:RootNode/testNS:ChildElement");
$ChildElement = $DOMNodeList_ChildElement->item(0);

echo "Not usefull xml to load in another document:\n\n";
echo $domDoc->saveXML($ChildElement);

/* Output here:
 *
 * Not usefull xml to load in another document:
 *
 *  <testNS:ChildElement>
 *        <testNS:AnotherChildElement>I'm a Another Child node.</testNS:AnotherChildElement>
 *    </testNS:ChildElement>
 *
 */

/**
 * Function to help us clone the element to another document.
 * @param DOMElement $node The node to clone.
 * @param DOMDocument $doc The document where we are going to reference the elements.
 * @return DOMElement The new cloned element without namespace.
 */
function cloneNode($node, $doc) {
    //Create new element with the original localName (w/o namespace)
    $nd = $doc->createElement($node->localName);

    //Clone attributes
    foreach($node->attributes as $value)
        $nd->setAttribute($value->localName, $value->value);

    //No more childs then we finish.
    if (!$node->childNodes)
        return $nd;
    //We have childs, add them
    foreach($node->childNodes as $child) {
        if ($child->nodeName=="#text") //Only needed to clone text nodes, i.e. text comments, spaces, tabs. etc.
            $nd->appendChild($doc->createTextNode($child->nodeValue));
        else
            $nd->appendChild(cloneNode($child, $doc)); //recursion to clone all children.
    }
    return $nd;
}

//New Document to reference the new node without namespaces
$domDoc2 = new DOMDocument("1.0");

//We clone this node taking out the namespace
$new_node = cloneNode($ChildElement, $domDoc2);

echo "\n\nWe can load this into a DOMDocument without problems:\n\n";
echo $domDoc2->saveXML($new_node);

/*
 * We can load this into a DOMDocument without problems:
 *
 *    <ChildElement>
 *        <AnotherChildElement>I'm a Another Child node.</AnotherChildElement>
 *    </ChildElement>
 */

?>
xianrenb at gmail dot com 16-Jul-2012 07:54
If you want to save xhtml (in a string), you may try the following method:
<?php
    $doc = new DOMDocument('1.0');

    // ...

    $xhtml = (string) $doc->saveXML($doc->doctype);
    $xhtml .= "\n";
    $xhtml .= (string) $doc->saveXML($doc->documentElement);
?>
samstah at gmail dot com 19-Mar-2010 12:56
We discovered using DOMDocument::saveHTML() that it converts to HTML 4.01 compatible markup; rather than XHTML. The simple answer is to use saveXML() instead, although this adds the XML declaration to the top.

To qjerry.com at gmail.com, thanks for the pointer below - but I think that the simplest way seems to be using:
<?php $domDocument->saveXML($domDocument->documentElement); ?>

Of course, if you're dealing with XHTML, this will strip any <!DOCTYPE> declarations in the document too.
qjerry.com at gmail.com 04-May-2009 01:24
The simpliest (and probably the fastest) way to strip an XML declaration (<?xml version="1.0" ... ?>) out of output document, is to output child nodes of DOMDocument separately:

<?php

$document = new DOMDocument();
$document->load('/some/file.xml');

// this will also output doctype and comments at top level
foreach($document->childNodes as $node)
    $result .= $document->saveXML($node)."\n";

echo $result;

?>

This might be userful when dealing with browser compatibility issues, for example, well known problem with valid XHTML in IE6.
vikramvmalhotra at hotmail dot com 28-Jan-2009 12:49
if you are storing multi-byte characters in XML, then saving the XML using saveXML() will create problems. It will spit out the characters converted in encoded format.

<?php
$str = domdoc->saveXML(); // gives "&x#1245;" some encoded data
?>

Instead do the following

<?php
$str = domdoc->saveXML(domdoc->documentElement); // gives "保存しました" correct multi-byte data
?>
shinsh at shinmugen dot net 23-May-2008 04:57
Be careful, this function has been changed in 5.2.6 version. Adding a required parameter which is not that required wasn't the most intelligent idea ever, especially for a function which is often used.

To fix your programs if you have an error, fill the first parameter like this:

$dom->saveXML($dom->documentElement);

Why didn't the devs simply implement this as optional parameter, fixing the default parameter as documentElement?
Kriogen 26-Dec-2007 09:57
Create before test.xml with:
<?xml version="1.0" encoding="utf-8"?>
<Photos>
</Photos>

and past after in your php-file:

<?php
$simp = simplexml_load_file('test.xml');
$node = $simp->addChild('home');
$node->addChild('mychild', 'insert text');
$s = simplexml_import_dom($simp);
$s->saveXML('test.xml');
?>

This code creating a child-nod in the root.
Owner http://www.mensfashion.ru
JITR 03-Aug-2007 02:10
Comment to `devin at SPAMISBAD dot tritarget dot com''s post:

Thanks for pointing out the pitfalls of `formatOutput' vs. `load*()'. This has certainly saved me from some possible surprises.

I think the seemingly strange behaviour can be explained. Warning: The following stuff is mostly based on deductions and experiments. Much less on studying the sources and specs (I'm not sure some of these would provide answer anyway, at least not easily).

As you point out, `preserveWhiteSpace' must be set before loading the DOM from the source string (I'm working with `loadXML()' but I believe the situation should be the same with `load()' you used). This looks logical, as this property seems to control the parsing and DOM creation process during which text nodes containing the whitespace are either included or dropped. This can be proven by dumping the DOM structure and comparing the results based on the value of `preserveWhiteSpace'. With `preserveWhiteSpace' set to `FALSE', no text nodes containing whitespace will be present in the returned DOM. When this property is `TRUE', these nodes will be present.

Note: When speaking about the whitespace in the previous paragraph, we're most certainly speaking about so called `whitespace in element content' or `element content whitespace', if I'm not mistaken. See also my comment in the notes of `DOMText->isWhitespaceInElementContent()' method.

As for the mysterious effect on the output of `saveXLM()', I think the explanation lies in the presence or absence of the above mentioned whitespace text nodes. This was also proven by experiments: After adding such a node into a DOM which contained none (the DOM was created using `loadXML()' with `preserveWhiteSpace' set to `FALSE'), the output formatting got affected in a such a way, the formatting got lost for the rest of the document after the added node. I think the presence of whitespace text nodes forces such rendering, that the content of these nodes is used to separate adjoining nodes thus disabling default formatting. Only when there are no such text nodes present, the ouput formatting takes effect (provided the `formatOutput' is set to `TRUE', of course).

Well, the thing I don't really understand is how you did get an output of a signle line with `formatOutput' set to `TRUE'. This has happened to me when no whitespace text nodes were present (ie. when loading the XML with `preserveWhiteSpace' set to `FALSE') *and* with `formatOutput' set to *`FALSE'* (with the opposite value of `formatOutput', the formatting should do it's work and you should not end up with just one line). But I haven't seen your source. Perhaps you had whitespace nodes containing no new-lines in your DOM?

As for the CAUTION about root element, I didn't see any problems with empty root element neither in shortened nor full form. What did you have in mind, when you said it `WORKS' or `DOES NOT WORK'?
Sander 25-Apr-2007 02:35
Note that for large DOM trees (tens of thousands of elements nested at least a few levels deep), setting formatOutput to true drives up memory usage to rather insane levels when you call saveXML(). (Tested with PHP 5.2.1) Pretty output is not worth that cost.
nevyn at NOSPAM dot email dot PLEASE dot it 27-Dec-2006 10:41
A little function to get the full xml contents of a Xml node.

function innerXml($node)
{
    $out = $node->ownerDocument->saveXML($node);
    $re = "{^<(\\w*)(?:\\s*\\w+=(?:\"[^\"]*\"|\'[^\']*\'))*\\s*>(.*)</\\1>$}";
    preg_match($re, $out, $mat);
    return $mat[2];
}
devin at SPAMISBAD dot tritarget dot com 15-Aug-2006 07:52
It took some searching to figure this one out. I didn't see much in the way of explaining this glitch in the manual thus far. (For PHP5 I believe)

formatOutput = true; appears to fail when the origin of the DOM came from a file via load(). EX:

<?php
    $dom = new DOMDocument();
    $dom->load ("test.xml");
    $dom->formatOutput = true;

    $new_tag = $dom->createElement ('testNode');
    $new_tag->appendChild (
        $dom->createElement ('test', 'this is a test'));
    $dom->documentElement->appendChild ($new_tag);

    printf ("<pre>%s</pre>", htmlentities ($dom->saveXML()));
?>

Will not indent the output and will display the modified nodes all in one long line. Makes for editing a config.xml a bit difficult when saving to a file.

By adding the preserveWhiteSpace = false; BEFORE the load() the formatOutput works as expected. EX:

<?php
    $dom = new DOMDocument();
    $dom->preserveWhiteSpace = false;
    $dom->load ("test.xml");
    $dom->formatOutput = true;

    $new_tag = $dom->createElement ('testNode');
    $new_tag->appendChild (
        $dom->createElement ('test', 'this is a test'));
    $dom->documentElement->appendChild ($new_tag);

    printf ("<pre>%s</pre>", htmlentities ($dom->saveXML()));
?>

CAUTION: If your loaded xml file (test.xml) has an empty root node that is not shortened or has no children this will NOT work.

Example:

DOES NOT WORK:
<?xml version="1.0"?>
<root>
</root>

WORKS:
<?xml version="1.0"?>
<root/>

WORKS:
<?xml version="1.0"?>
<root>
  <!-- comment -->
</root>

WORKS:
<?xml version="1.0"?>
<root>
  <child/>
</root>
27-Mar-2006 01:20
I used the function posted by "joe" but the following works to me for get the innerXML
<?php
$itemLeido = $XMLRespuesta->getElementsByTagName("articulos");
foreach($itemLeido as $node) {
    echo($node->ownerDocument->saveXML($node));
}
?>
mswiercz at mwerk dot com 10-Sep-2004 12:43
Quick tip to minimize memory when generating documents with DOM.

Rather than using
   $xmlStr = DOMDocument->saveXML();
   echo $xmlStr;

to dump a large DOM to the output buffer, use a PHP output stream, as in

   DOMDocument->save('php://output');

A lot of memory will be saved when generating large DOMs.
padys at tlen dot pl 05-Aug-2004 04:36
When you save whole document:
DOMDocument->saveXML() produces string in encoding defined in property DOMDocument->encoding.

When you save only one node:
DOMDocument->saveXML(DOMNode) produces always string in UTF-8.


Validates a document based on a schema

  • ≪ DOMDocument::saveXML
  • DOMDocument::schemaValidateSource ≫
  • PHP Manual
  • DOMDocument
  • Validates a document based on a schema

DOMDocument::schemaValidate

(PHP 5, PHP 7)

DOMDocument::schemaValidate — Validates a document based on a schema

설명

public bool DOMDocument::schemaValidate ( string $filename [, int $flags ] )

Validates a document based on the given schema file.

인수

filename

The path to the schema.

flags

A bitmask of Libxml schema validation flags. Currently the only supported value is LIBXML_SCHEMA_CREATE. Available since PHP 5.5.2 and Libxml 2.6.14.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
5.5.2 | Added the flags parameter

참고

  • DOMDocument::schemaValidateSource() - Validates a document based on a schema
  • DOMDocument::relaxNGValidate() - Performs relaxNG validation on the document
  • DOMDocument::relaxNGValidateSource() - Performs relaxNG validation on the document
  • DOMDocument::validate() - Validates the document based on its DTD

User Contributed Notes

mmamsch at googlemail dot com 16-Sep-2015 07:01
A note for people trying to validate complex schemas with PHP.  It seems that libxml will not automatically try to import referenced schemas, but simply skip the validation if the schemas were not explicitly imported.

In our example we tried to validate an XML file against a schema with the namespace "xttp://automotive-his.de/200706/rif" that contains a reference to the namespace "xttp://automotive-his.de/200706/rif-xhtml":

  <xsd:complexType name="XHTML-CONTENT">
    <xsd:sequence>
      <xsd:any namespace="xttp://automotive-his.de/200706/rif-xhtml"/>
    </xsd:sequence>
  </xsd:complexType>

which basically says that an xhtml-content element can contain any element from the rif-xhtml namespace.

However since libxml does not know where to find the schema file, it will stop the validation if the elements from the referenced namespace, passing also document with invalid xhtml content as valid.

The solution was to create a combined schema, which contains import statements for all files matching the referenced schemas:

<xsd:schema xmlns:xs="xttp://www.w3.org/2001/XMLSchema">
<xs:import namespace="xttp://automotive-his.de/200706/rif-xhtml" schemaLocation="rif-xhtml.xsd"/>
<xs:import namespace="xttp://automotive-his.de/200706/rif" schemaLocation="rif.xsd"/>
<xs:import namespace="xttp://www.w3.org/XML/1998/namespace" schemaLocation="../xml.xsd"/>
</xsd:schema>

Note that the schemaLocation tells the validator, where to find the files for the corresponding namespaces. When validating the XML document against this combined schema, libxml validated correctly the content inside the XHTML-Content.

Hope this helps someone out there.
mati 12-Aug-2013 06:15
If write any error detector from xml file using schema and libxml function watch out on some unreseloved bug

https://bugs.php.net/bug.php?id=54138
wkoehler at ce-gmbh dot com 11-Jan-2011 11:37
In older versions of PHP5 this function might cause error messages when dealing with namespaces. I had problems with PHP 5.2.14 which comes with libXML V2.6.16. After switching to PHP 5.3.5 with libXML V2.7.7 I no longer had problems. Took me only about 30 hrs to figure that one out.
NetPanther 27-Mar-2009 10:42
Initial situation:
- Debian Lenny
- Apache 2 with PHP 5.2.6
- libxml 2.6.32

Problem: While trying to validate a manually created (!) DOMDocument against an existing XML Schema, I ran into a warning like the one below. Validation fails, even though the document IS valid and the command line tool xmllint confirms this (even with libxml 2.6.32, so this must be a problem with DOM). The validation works fine with libxml 2.7.3.

Warning: DOMDocument::schemaValidate() [domdocument.schemavalidate]: Element 'YourRootElementName': No matching global declaration available for the validation root. in /var/www/YourFile.php on line X

Solution: As libxml 2.7.3 is not provided for Debian Lenny yet and this problem seems to be caused by DOM (s.o.), I currently use the following workaround on my machines. DOM obviously has some namespace problems with documents, that have been created manually (i.e. they were not loaded from a file).

So my workaround is saving the DOMDocument temporarily, re-loading it and then validating the temporary DOMDocument. Strangely enough the validation of the same document (= same content) now works. Sure, creating a temporary file is not a nice solution, but unless this bug is fixed, this workaround should do just fine.

<?php

// Works with libxml 2.7.3 and higher.
public function isValid()
{
  return $this->dom->schemaValidate('schema.xsd');
}

// Workaround for prior libxml versions, e.g. 2.6.32.
public function isValid()
{
  // Create temporary file and save manually created DOMDocument.
  $tempFile = time() . '-' . rand() . '-document.tmp';
  $this->dom->save($tempFile);

  // Create temporary DOMDocument and re-load content from file.
  $tempDom = new DOMDocument();
  $tempDom->load($tempFile);

  // Delete temporary file.
  if (is_file($tempFile))
  {
    unlink($tempFile);
  }

  // Validate temporary DOMDocument.
  return $tempDom->schemaValidate('schema.xsd');
}

?>
poletto at jeuxvideo dot com 13-Feb-2008 09:32
I had a tricky issue using this method, i thought it was bug but i realized i misundestood something about namespaces.
When you want to use a schema to describe an xml document, you basically put the default namespace to a personnal namespace (and you refer to this namespace in the targetNamespace attribute of your schema).

<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://my.uri.net">
  <elt>
    <x>blabla</x>
    <y>blabla</y>
  </elt>
</root>

That xmlns attribute specifies a "default namespace" and means that the root element and its children are in this namespace.
What i misunderstood is that, there's no way with the DOM api to specify a "default namespace" for each children of the root element.
Thus you may need to use createElementNS() and createAttributeNS() methods for each created element or attribute in the document specifying the URI of your namespace each time ("http://my.uri.net").

This only applies if you want to validate a document built with the API, not with a document loaded from an xml file or stream.
nevyn at NOSPAM dot email dot it 19-Dec-2006 02:41
I've seen that trying to validate an XML with a schema containing keys, they were ignored, and the XML was considered still valid altough it contained duplicate keys.
justin at redwiredesign dot com 08-Nov-2006 12:32
In his previous comment, Mike A wrote about validating documents using an XSD. However, you can validate without one. In my case, I needed to ensure that the content entered was just valid XML or not, and I couldn't find an XSD to support that. So I wrote this:

public static function validate($xml)
{
    libxml_use_internal_errors(true);

    $doc = new DOMDocument('1.0', 'utf-8');
    $doc->loadXML($xml);

    $errors = libxml_get_errors();
    if (empty($errors))
    {
        return true;
    }

    $error = $errors[0];
    if ($error->level < 3)
    {
        return true;
    }

    $lines = explode("\r", $xml);
    $line = $lines[($error->line)-1];

    $message = $error->message.' at line '.$error->line.':<br />'.htmlentities($line);

    return $message;
}

The catch here is that the function only checks for the first error is LIBXML_ERR_FATAL, which would break XSL/XML compilation.

In my experience, the errors are returned by libxml_get_errors in descending severity, so this may be an OK thing to do.
Mike A. 17-Feb-2006 09:03
For more detailed feedback from DOMDocument::schemaValidate, disable libxml errors and fetch error information yourself.  See http://php.net/manual/en/ref.libxml.php for more info.

example.xml
<?xml version="1.0"?>
<example>
    <child_string>This is an example.</child_string>
    <child_integer>Error condition.</child_integer>
</example>

example.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
    <xs:element name="example">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="child_string" type="xs:string"/>
                <xs:element name="child_integer" type="xs:integer"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

<?php

function libxml_display_error($error)
{
    $return = "<br/>\n";
    switch ($error->level) {
        case LIBXML_ERR_WARNING:
            $return .= "<b>Warning $error->code</b>: ";
            break;
        case LIBXML_ERR_ERROR:
            $return .= "<b>Error $error->code</b>: ";
            break;
        case LIBXML_ERR_FATAL:
            $return .= "<b>Fatal Error $error->code</b>: ";
            break;
    }
    $return .= trim($error->message);
    if ($error->file) {
        $return .=    " in <b>$error->file</b>";
    }
    $return .= " on line <b>$error->line</b>\n";

    return $return;
}

function libxml_display_errors() {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
        print libxml_display_error($error);
    }
    libxml_clear_errors();
}

// Enable user error handling
libxml_use_internal_errors(true);

$xml = new DOMDocument();
$xml->load('example.xml');

if (!$xml->schemaValidate('example.xsd')) {
    print '<b>DOMDocument::schemaValidate() Generated Errors!</b>';
    libxml_display_errors();
}

?>

Old error message:
Warning: DOMDocument::schemaValidate() [function.schemaValidate]: Element 'child_integer': 'Error condition.' is not a valid value of the atomic type 'xs:integer'. in example.php on line 40

New error message:
DOMDocument::schemaValidate() Generated Errors!
Error 1824: Element 'child_integer': 'Error condition.' is not a valid value of the atomic type 'xs:integer'. in example.xml on line 4


Validates a document based on a schema

  • ≪ DOMDocument::schemaValidate
  • DOMDocument::validate ≫
  • PHP Manual
  • DOMDocument
  • Validates a document based on a schema

DOMDocument::schemaValidateSource

(PHP 5, PHP 7)

DOMDocument::schemaValidateSource — Validates a document based on a schema

설명

public bool DOMDocument::schemaValidateSource ( string $source [, int $flags ] )

Validates a document based on a schema defined in the given string.

인수

source

A string containing the schema.

flags

A bitmask of Libxml schema validation flags. Currently the only supported value is LIBXML_SCHEMA_CREATE. Available since PHP 5.5.2 and Libxml 2.6.14.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
5.5.2 | Added the flags parameter

참고

  • DOMDocument::schemaValidate() - Validates a document based on a schema
  • DOMDocument::relaxNGValidate() - Performs relaxNG validation on the document
  • DOMDocument::relaxNGValidateSource() - Performs relaxNG validation on the document
  • DOMDocument::validate() - Validates the document based on its DTD

User Contributed Notes

There are no user contributed notes for this page.


Validates the document based on its DTD

  • ≪ DOMDocument::schemaValidateSource
  • DOMDocument::xinclude ≫
  • PHP Manual
  • DOMDocument
  • Validates the document based on its DTD

DOMDocument::validate

(PHP 5, PHP 7)

DOMDocument::validate — Validates the document based on its DTD

설명

public bool DOMDocument::validate ( void )

Validates the document based on its DTD.

You can also use the validateOnParse property of DOMDocument to make a DTD validation.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다. If the document have no DTD attached, this method will return FALSE.

예제

Example #1 Example of DTD validation

<?php
$dom = new DOMDocument;
$dom->Load('book.xml');
if ($dom->validate()) {
    echo "This document is valid!\n";
}
?>

You can also validate your XML file while loading it:

<?php
$dom = new DOMDocument;
$dom->validateOnParse = true;
$dom->Load('book.xml');
?>

참고

  • DOMDocument::schemaValidate() - Validates a document based on a schema
  • DOMDocument::schemaValidateSource() - Validates a document based on a schema
  • DOMDocument::relaxNGValidate() - Performs relaxNG validation on the document
  • DOMDocument::relaxNGValidateSource() - Performs relaxNG validation on the document

User Contributed Notes

cabral dot brc at gmail dot com 08-Sep-2010 09:46
I was trying to validate my SVG file just to get the IDs with getElementById(),

<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<text id="text_titulo" x="150" y="20" font-family="Arial" font-size="15px" style="font-weight:bold;">Title</text>
<text id="text_subtitulo" x="200" y="35" font-family="Arial" font-size="10px">Sub Title</text>
<g transform="translate(0,50) scale(0.1)">
  <a id="a_AC" ...... >
    <polygon id="polygon_AC" ....... />
  </a>
</g>
</svg>

but when calling $dom->load(), it was getting the errors "failed to open stream" and "Validation failed: no DTD found !I/O warning : failed to load external entity"
so I created my own validation wrapper (I just want to find the IDs with getElementById() )

<?php
  $docSVG = new DOMDocument();
  $docSVG->load(realpath($filepath));
  setAllId($docSVG);

function setAllId($DOMNode){
  if($DOMNode->hasChildNodes()){
    foreach ($DOMNode->childNodes as $DOMElement) {
      if($DOMElement->hasAttributes()){
        $id=$DOMElement->getAttribute("id");
        if($id){
          $DOMElement->setIdAttribute("id",$id);
        }
      }
      setAllId($DOMElement);
    }
  }
}
?>
a dot schaffhirt at sedna-soft dot de 17-Sep-2008 11:22
When validating documents with this method there are two issues I don't like about it. First, it creates a bunch of warnings, which one would not expect, as the plot of calling this method is preventing any warnings that could occur when erroneously relying on the document's validity. Second, it only returns a boolean with no chance of getting additional details about the reasons for rendering invalid.

That's the reason for me to use a little wrapper, which I post here in case anyone finds it useful.

Please note that it only works with PHP5 or later.

<?php
    class MyDOMDocument {
        private $_delegate;
        private $_validationErrors;

        public function __construct (DOMDocument $pDocument) {
            $this->_delegate = $pDocument;
            $this->_validationErrors = array();
        }

        public function __call ($pMethodName, $pArgs) {
            if ($pMethodName == "validate") {
                $eh = set_error_handler(array($this, "onValidateError"));
                $rv = $this->_delegate->validate();
                if ($eh) {
                    set_error_handler($eh);
                }
                return $rv;
            }
            else {
                return call_user_func_array(array($this->_delegate, $pMethodName), $pArgs);
            }
        }
        public function __get ($pMemberName) {
            if ($pMemberName == "errors") {
                return $this->_validationErrors;
            }
            else {
                return $this->_delegate->$pMemberName;
            }
        }
        public function __set ($pMemberName, $pValue) {
            $this->_delegate->$pMemberName = $pValue;
        }
        public function onValidateError ($pNo, $pString, $pFile = null, $pLine = null, $pContext = null) {
            $this->_validationErrors[] = preg_replace("/^.+: */", "", $pString);
        }
    }
?>

<?php
    // $doc is a DOMDocument object
    $myDoc = new MyDOMDocument($doc); // copy constructor

    // do anything with $myDoc that you would with $doc

    $isValid = $myDoc->validate(); // won't create warnings
    if (!$isValid) {
        print_r($myDoc->errors); // the array all warnings are collected in
    }
?>

Maybe you need to change the the part
preg_replace("/^.+: */", "", $pString)
to something different depending on your system's error reporting settings (HTML or plain text), whatsoever

Best Regards,

Anja
darren at viamedia dot co dot za 05-Aug-2008 04:19
If you are loading xml with the intention of validating it against an internal dtd and you have experienced issues with the validation it could be related to missing LIBXML constants.

I found this post by "aidan at php dot net" in root level dom docs and thought it might be more useful here:
As of PHP 5.1, libxml options may be set using constants rather than the use of proprietary DomDocument properties.

DomDocument->resolveExternals is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDATTR

DomDocument->validateOnParse is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDVALID

PHP 5.1 users are encouraged to use the new constants.

Example:
<?php
$dom = new DOMDocument;
// Resolve externals
$dom->load($file, LIBXML_DTDLOAD|LIBXML_DTDATTR);
// OR
// Validate against DTD
$dom->load($file, LIBXML_DTDLOAD|LIBXML_DTDVALID);
$dom->validate();
?>


Substitutes XIncludes in a DOMDocument Object

  • ≪ DOMDocument::validate
  • DOMDocumentFragment ≫
  • PHP Manual
  • DOMDocument
  • Substitutes XIncludes in a DOMDocument Object

DOMDocument::xinclude

(PHP 5, PHP 7)

DOMDocument::xinclude — Substitutes XIncludes in a DOMDocument Object

설명

public int DOMDocument::xinclude ([ int $options ] )

This method substitutes » XIncludes in a DOMDocument object.

Note:

Due to libxml2 automatically resolving entities, this method will produce unexpected results if the included XML file have an attached DTD.

인수

options

libxml parameters. Available since PHP 5.1.0 and Libxml 2.6.7.

반환값

Returns the number of XIncludes in the document, -1 if some processing failed, or FALSE if there were no substitutions.

예제

Example #1 DOMDocument::xinclude() example

<?php

$xml = <<<EOD
<?xml version="1.0" ?>
<chapter xmlns:xi="http://www.w3.org/2001/XInclude">
 <title>Books of the other guy..</title>
 <para>
  <xi:include href="book.xml">
   <xi:fallback>
    <error>xinclude: book.xml not found</error>
   </xi:fallback>
  </xi:include>
 </para>
</chapter>
EOD;

$dom = new DOMDocument;

// let's have a nice output
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;

// load the XML string defined above
$dom->loadXML($xml);

// substitute xincludes
$dom->xinclude();

echo $dom->saveXML();

?>

위 예제의 출력 예시:

<?xml version="1.0"?>
<chapter xmlns:xi="http://www.w3.org/2001/XInclude">
  <title>Books of the other guy..</title>
  <para>
    <row xml:base="/home/didou/book.xml">
       <entry>The Grapes of Wrath</entry>
       <entry>John Steinbeck</entry>
       <entry>en</entry>
       <entry>0140186409</entry>
      </row>
    <row xml:base="/home/didou/book.xml">
       <entry>The Pearl</entry>
       <entry>John Steinbeck</entry>
       <entry>en</entry>
       <entry>014017737X</entry>
      </row>
    <row xml:base="/home/didou/book.xml">
       <entry>Samarcande</entry>
       <entry>Amine Maalouf</entry>
       <entry>fr</entry>
       <entry>2253051209</entry>
      </row>
  </para>
</chapter>

User Contributed Notes

nicolas_rainardNOSPAM at yahoo dot fr 12-Jul-2007 01:56
If you use the loadXML() method instead of the load() one (let's say, to process the XML string before loading and parsing it), you will have problems with xinclude(), because the parser will not know where to find the files to include.
Using chdir() before xinclude() will not help.

The solution is to set the documentURI property of the DOMDocument object accordingly to it's original filename, and everything will work fine !

<?php

$xml = file_get_contents($file);
$xml = do_something_with($xml);

$doc = new DOMDocument;
$doc->documentURI = $file;
$doc->loadXML($xml);
$doc->xinclude();

?>


The DOMDocumentFragment class

(PHP 5, PHP 7)

클래스 개요

DOMDocumentFragment extends DOMNode {
/* 프로퍼티 */
/* 메소드 */
public bool appendXML ( string $data )
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

Table of Contents

  • DOMDocumentFragment::appendXML — Append raw XML data

User Contributed Notes

Ricki 29-Mar-2011 03:00
DOMDocumentFragment only appears useful if created from a parent DOMDocument eg.

1. $dom = new DOMDocument("1.0","UTF-8");
2. $docFrag = $dom->createDocumentFragment();
3. Now append items to $docFrag
4. Graft $docFrag contents back onto $dom at the desired location

Conversely taking this approach:
1. $dom = new DOMDocument("1.0","UTF-8");
2. $docFrag = new DOMDocumentFragment();
3. Now append items to $docFrag

...will fail on step 3 with a "read only" error as $docFrag is not created as a child of  DOMDocument.

I'm not sure of the reason for this: on the web people have cited security, and others have cited poor design however whatever the reason, it is really limiting when wanting to encapsulating generic independent DocumentFragments into classes for easy grafting to the desired tree. The only workarounds i have seen look expensive from a performance perspective and cumbersome from a coding perspective ie. create a  dummy $dom for temporary use.

(This is valid as of PHP 5.3) I've put this here as i've wasted a lot of time finding it out - I hope this saves others some heartache.

Using new DOMDocumentFramt
matthijs at stdin dot nl 29-Mar-2011 12:06
Note that DOMDocumentFragment is a bit special when it's added to another node. When that happens, not the fragment itself is added as a child, but all of the children of the fragment are moved over to the new parent node.

For example, consider this script:

<?php

/* Create a document and a fragment containing a single node */
$doc = new DOMDocument();
$fragment = $doc->createDocumentFragment();
$fragment->appendChild($doc->createElement('foo'));

/* Now, the foo node is a child of the fragment */
var_dump($fragment->firstChild);

/* After appending the fragment to another node, the children of the
 * fragment will have been transfered to that node (and the fragment is
 * not present in the children list!) */
$doc->appendChild($fragment);
/* So the fragment has no children anymore */
var_dump($fragment->firstChild);
/* But $doc has a single child, which is the foo element, not the
 * fragment */
var_dump($doc->childNodes->length);
var_dump($doc->firstChild);

?>

This produces the following output:

object(DOMElement)#3 (0) {
}
NULL
int(1)
object(DOMElement)#3 (0) {
}


Append raw XML data

  • ≪ DOMDocumentFragment
  • DOMDocumentType ≫
  • PHP Manual
  • DOMDocumentFragment
  • Append raw XML data

DOMDocumentFragment::appendXML

(PHP 5 >= 5.1.0, PHP 7)

DOMDocumentFragment::appendXML — Append raw XML data

설명

public bool DOMDocumentFragment::appendXML ( string $data )

Appends raw XML data to a DOMDocumentFragment.

This method is not part of the DOM standard. It was created as a simpler approach for appending an XML DocumentFragment in a DOMDocument.

If you want to stick to the standards, you will have to create a temporary DOMDocument with a dummy root and then loop through the child nodes of the root of your XML data to append them.

인수

data

XML to append.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 Appending XML data to your document

<?php
$doc = new DOMDocument();
$doc->loadXML("<root/>");
$f = $doc->createDocumentFragment();
$f->appendXML("<foo>text</foo><bar>text2</bar>");
$doc->documentElement->appendChild($f);
echo $doc->saveXML();
?>

위 예제의 출력:

<?xml version="1.0"?>
<root><foo>text</foo><bar>text2</bar></root>

User Contributed Notes

lpetrov(AT)axisvista.com 21-Jul-2007 02:45
Here is (maybe) a better example:
/**
* Helper function for replacing $node (DOMNode)
* with an XML code (string)
*
* @var DOMNode $node
* @var string $xml
*/
public function replaceNodeXML(&$node,$xml) {
 $f = $this->dom->createDocumentFragment();
 $f->appendXML($xml);
 $node->parentNode->replaceChild($f,$node);
}

Copied from the "PHP5 Dom Based Template" article at:
http://blog.axisvista.com/?p=35


The DOMDocumentType class

(PHP 5, PHP 7)

소개

Each DOMDocument has a doctype attribute whose value is either NULL or a DOMDocumentType object.

클래스 개요

DOMDocumentType extends DOMNode {
/* 프로퍼티 */
readonly public string $publicId ;
readonly public string $systemId ;
readonly public string $name ;
readonly public DOMNamedNodeMap $entities ;
readonly public DOMNamedNodeMap $notations ;
readonly public string $internalSubset ;
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

프로퍼티

publicId

The public identifier of the external subset.

systemId

The system identifier of the external subset. This may be an absolute URI or not.

name

The name of DTD; i.e., the name immediately following the DOCTYPE keyword.

entities

A DOMNamedNodeMap containing the general entities, both external and internal, declared in the DTD.

notations

A DOMNamedNodeMap containing the notations declared in the DTD.

internalSubset

The internal subset as a string, or null if there is none. This does not contain the delimiting square brackets.

User Contributed Notes

There are no user contributed notes for this page.


The DOMElement class

(PHP 5, PHP 7)

클래스 개요

DOMElement extends DOMNode {
/* 프로퍼티 */
readonly public bool $schemaTypeInfo ;
readonly public string $tagName ;
/* 메소드 */
public __construct ( string $name [, string $value [, string $namespaceURI ]] )
public string getAttribute ( string $name )
public DOMAttr getAttributeNode ( string $name )
public DOMAttr getAttributeNodeNS ( string $namespaceURI , string $localName )
public string getAttributeNS ( string $namespaceURI , string $localName )
public DOMNodeList getElementsByTagName ( string $name )
public DOMNodeList getElementsByTagNameNS ( string $namespaceURI , string $localName )
public bool hasAttribute ( string $name )
public bool hasAttributeNS ( string $namespaceURI , string $localName )
public bool removeAttribute ( string $name )
public bool removeAttributeNode ( DOMAttr $oldnode )
public bool removeAttributeNS ( string $namespaceURI , string $localName )
public DOMAttr setAttribute ( string $name , string $value )
public DOMAttr setAttributeNode ( DOMAttr $attr )
public DOMAttr setAttributeNodeNS ( DOMAttr $attr )
public void setAttributeNS ( string $namespaceURI , string $qualifiedName , string $value )
public void setIdAttribute ( string $name , bool $isId )
public void setIdAttributeNode ( DOMAttr $attr , bool $isId )
public void setIdAttributeNS ( string $namespaceURI , string $localName , bool $isId )
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

프로퍼티

schemaTypeInfo

Not implemented yet, always return NULL

tagName

The element name

주의

Note:

The DOM extension uses UTF-8 encoding. Use utf8_encode() and utf8_decode() to work with texts in ISO-8859-1 encoding or Iconv for other encodings.

Table of Contents

  • DOMElement::__construct — Creates a new DOMElement object
  • DOMElement::getAttribute — Returns value of attribute
  • DOMElement::getAttributeNode — Returns attribute node
  • DOMElement::getAttributeNodeNS — Returns attribute node
  • DOMElement::getAttributeNS — Returns value of attribute
  • DOMElement::getElementsByTagName — Gets elements by tagname
  • DOMElement::getElementsByTagNameNS — Get elements by namespaceURI and localName
  • DOMElement::hasAttribute — Checks to see if attribute exists
  • DOMElement::hasAttributeNS — Checks to see if attribute exists
  • DOMElement::removeAttribute — Removes attribute
  • DOMElement::removeAttributeNode — Removes attribute
  • DOMElement::removeAttributeNS — Removes attribute
  • DOMElement::setAttribute — Adds new attribute
  • DOMElement::setAttributeNode — Adds new attribute node to element
  • DOMElement::setAttributeNodeNS — Adds new attribute node to element
  • DOMElement::setAttributeNS — Adds new attribute
  • DOMElement::setIdAttribute — Declares the attribute specified by name to be of type ID
  • DOMElement::setIdAttributeNode — Declares the attribute specified by node to be of type ID
  • DOMElement::setIdAttributeNS — Declares the attribute specified by local name and namespace URI to be of type ID

User Contributed Notes

johnny 04-Mar-2014 12:48
Get html of a node
 $html .= $dom->saveHTML($node);
Janne Enberg 07-Nov-2013 08:02
This page doesn't list the inherited properties from DOMNode, e.g. the quite important textContent property. It would be immensely helpful if it would list those as well.
felix dot klee at inka dot de 27-Feb-2013 12:23
How to rename an element and preserve attributes:

<?php

// Changes the name of element $element to $newName.
function renameElement($element, $newName) {
  $newElement = $element->ownerDocument->createElement($newName);
  $parentElement = $element->parentNode;
  $parentElement->insertBefore($newElement, $element);

  $childNodes = $element->childNodes;
  while ($childNodes->length > 0) {
    $newElement->appendChild($childNodes->item(0));
  }

  $attributes = $element->attributes;
  while ($attributes->length > 0) {
    $attribute = $attributes->item(0);
    if (!is_null($attribute->namespaceURI)) {
      $newElement->setAttributeNS('http://www.w3.org/2000/xmlns/',
                                  'xmlns:'.$attribute->prefix,
                                  $attribute->namespaceURI);
    }
    $newElement->setAttributeNode($attribute);
  }

  $parentElement->removeChild($element);
}

function prettyPrint($d) {
  $d->formatOutput = true;
  echo '<pre>'.htmlspecialchars($d->saveXML()).'</pre>';
}

$d = new DOMDocument( '1.0' );
$d->loadXML('<?xml version="1.0"?>
<library>
  <data a:foo="1" x="bar" xmlns:a="http://example.com/a">
    <invite>
      <username>jmansa</username>
      <userid>1</userid>
    </invite>
    <update>1</update>
  </data>
</library>');

$xpath = new DOMXPath($d);
$elements = $xpath->query('/library/data');
if ($elements->length == 1) {
  $element = $elements->item(0);
  renameElement($element, 'invites');
}

prettyPrint($d);

?>
loopduplicate at burningtoken dot com 22-Apr-2011 11:34
This works perfect for me as well:

<?php $xml = $domElement->ownerDocument->saveXML($domElement); ?>
Anonymous 24-Dec-2010 05:22
you can use DOMNode::nodeValue
DOMElement inherits this public property.

$elem->nodeValue
dpetroff ( at ) gmail.com 05-Dec-2010 08:24
Hi!

Combining all th comments, the easiest way to get inner HTML of the node is to use this function:

<?php
function get_inner_html( $node ) {
    $innerHTML= '';
    $children = $node->childNodes;
    foreach ($children as $child) {
        $innerHTML .= $child->ownerDocument->saveXML( $child );
    }

    return $innerHTML;
}
?>
nawaman at gmail dot com 04-Sep-2009 05:08
The following code shows can text-only content be extracted from a document.

<?php
function getTextFromNode($Node, $Text = "") {
    if ($Node->tagName == null)
        return $Text.$Node->textContent;

    $Node = $Node->firstChild;
    if ($Node != null)
        $Text = getTextFromNode($Node, $Text);

    while($Node->nextSibling != null) {
        $Text = getTextFromNode($Node->nextSibling, $Text);
        $Node = $Node->nextSibling;
    }
    return $Text;
}

function getTextFromDocument($DOMDoc) {
    return getTextFromNode($DOMDoc->documentElement);
}

$Doc = new DOMDocument();
$Doc->loadHTMLFile("Test.html");
echo getTextFromDocument($Doc)."\n";
?>
Daniel Morlock 07-May-2009 06:22
It would be nice to have a function which converts a document/node/element into a string.

Anyways, I use the following code snippet to get the innerHTML value of a DOMNode:

<?php
function getInnerHTML($Node)
{
     $Body = $Node->ownerDocument->documentElement->firstChild->firstChild;
     $Document = new DOMDocument();
     $Document->appendChild($Document->importNode($Body,true));
     return $Document->saveHTML();
}
?>
patrick smith 04-Nov-2008 05:14
Although it may be preferable to use the dom to manipulate elements, sometimes it's useful to actually get the innerHTML from a document element (e.g. to load into a client-side editor).

To get the innerHTML of a specific element ($elem_id) in a specific html file ($filepath):

<?php
$innerHTML = '';
$doc = new DOMDocument();
$doc->loadHTMLFile($filepath);
$elem = $doc->getElementById($elem_id);

// loop through all childNodes, getting html
$children = $elem->childNodes;
foreach ($children as $child) {
    $tmp_doc = new DOMDocument();
    $tmp_doc->appendChild($tmp_doc->importNode($child,true));
    $innerHTML .= $tmp_doc->saveHTML();
}
?>
Pinochet 25-Oct-2008 02:33
Hi to get the value of DOMElement just get the nodeValue public parameter (it is inherited from DOMNode):
<?php
echo $domElement->nodeValue;
?>
Everything is obvious if you now about this thing ;-)
j DOT wagner ( AT ) medieninnovation.com 08-Oct-2008 06:11
Caveat!
It took me almost an hour to figure this out, so I hope it saves at least one of you some time.

If you want to debug your DOM tree and try var_dump() or similar you will be fooled into thinking the DOMElement that you are looking at is empty, because var_dump() says: object(DOMElement)#1 (0) { }

After much debugging I found out that all DOM objects are invisible to var_dump() and print_r(), my guess is because they are C objects and not PHP objects. So I tried saveXML(), which works fine on DOMDocument, but is not implemented on DOMElement.

The solution is simple (if you know it):
$xml = $domElement->ownerDocument->saveXML($domElement);

This will give you an XML representation of $domElement.
Severin 14-Sep-2008 03:18
I wanted to find similar Elements - thats why I built an Xpath-String like this - maybe somebody needs it... its not very pretty - but neither is domdocument :)

<?php

$dom->load($xmlFile))

$xpathQuery = '//*';
$xmlNodes = $xpath->query($xpathQuery);

$pathlist = array();
$attrlist = array();
foreach ($xmlNodes as $node) {

  $depth = $this->_getDomDepth($node);   //get Path-Depth (for array key)
  $pathlist[$depth] = $node->tagName;     // tagname

  $attrs = $node->attributes;
  $attr='';
  $a=0;
  foreach ($attrs as $attrName => $attrNode)  // attributes
            {
              if ($attrName !='reg')
              {
                if ($a++!=0) $attr .= ' and ';
                $attr .= '@'.$attrName.'='."'".$attrNode->value."'";
              }
            }

          $attrlist[$depth] = $attr?'['.$attr.']':'';

          $path = ''; for ($i=0;$i<=$depth;$i++) $path .= '/'.$pathlist[$i].$attrlist[$i];  // the xpath of the actual Element

    // ... now you can go on and user $path to find similar elements
    }
  }
}

 private function _getDomDepth(DomNode $node)
   {
     $r = -2;
     while ($node) {
       $r++;
       $node = $node->parentNode;
     }
     return  $r;
   }
?>
ae.fxx 18-Jul-2008 10:49
Hi there.

Remember to append a DOMNode (or any of its descendants) to a DOMDocument __BEFORE__ you try to append a child to it.

I don't know why it has to be this way but it can't be done without it.

bye


Creates a new DOMElement object

  • ≪ DOMElement
  • DOMElement::getAttribute ≫
  • PHP Manual
  • DOMElement
  • Creates a new DOMElement object

DOMElement::__construct

(PHP 5, PHP 7)

DOMElement::__construct — Creates a new DOMElement object

설명

public DOMElement::__construct ( string $name [, string $value [, string $namespaceURI ]] )

Creates a new DOMElement object. This object is read only. It may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. To create a writeable node, use DOMDocument::createElement or DOMDocument::createElementNS.

인수

name

The tag name of the element. When also passing in namespaceURI, the element name may take a prefix to be associated with the URI.

value

The value of the element.

namespaceURI

A namespace URI to create the element within a specific namespace.

예제

Example #1 Creating a new DOMElement

<?php

$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$element_ns = new DOMElement('pr:node1', 'thisvalue', 'http://xyz');
$element->appendChild($element_ns);
echo $dom->saveXML(); /* <?xml version="1.0" encoding="utf-8"?>
<root><pr:node1 xmlns:pr="http://xyz">thisvalue</pr:node1></root> */

?>

참고

  • DOMDocument::createElement() - Create new element node
  • DOMDocument::createElementNS() - Create new element node with an associated namespace

User Contributed Notes

Fabian dot Blech at gmx dot de 22-Jul-2010 05:30
Remember, Dom-Nodes mustn't start with a number:

allowed:
<t12345t4>Value</t12345t4>

Not allowed:
<12345t4>VALUE</12345t4>
troelskn at gmail dot com 03-Jun-2008 04:46
Note that this function is buggy. You have to manually escape the $value argument with htmlspecialchars.
See: http://bugs.php.net/bug.php?id=31191
adar at darkpoetry dot de 07-May-2007 10:27
If you like to view an element simply do:

<?php
echo htmlentities($element->C14N());
?>

Undocumented but found ;)


Returns value of attribute

  • ≪ DOMElement::__construct
  • DOMElement::getAttributeNode ≫
  • PHP Manual
  • DOMElement
  • Returns value of attribute

DOMElement::getAttribute

(PHP 5, PHP 7)

DOMElement::getAttribute — Returns value of attribute

설명

public string DOMElement::getAttribute ( string $name )

Gets the value of the attribute with name name for the current node.

인수

name

The name of the attribute.

반환값

The value of the attribute, or an empty string if no attribute with the given name is found.

참고

  • DOMElement::hasAttribute() - Checks to see if attribute exists
  • DOMElement::setAttribute() - Adds new attribute
  • DOMElement::removeAttribute() - Removes attribute

User Contributed Notes

mpalmer at cybersource dot com 09-Nov-2007 12:32
- - - - - - - - - - - - - -

XML Data:
<data>
<Report ID="1">
    <Date>REVIEW</Date>
    <AuthorID>1</AuthorID>
</Report>
<Report ID="2">
    <Date>REVIEW</Date>
    <AuthorID>2</AuthorID>
</Report>
</data>

- - - - - - - - - - - - - -

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load( 'data.xml' );

$searchNode = $xmlDoc->getElementsByTagName( "Report" );

foreach( $searchNode as $searchNode )
{
    $valueID = $searchNode->getAttribute('ID');

    $xmlDate = $searchNode->getElementsByTagName( "Date" );
    $valueDate = $xmlDate->item(0)->nodeValue;

    $xmlAuthorID = $searchNode->getElementsByTagName( "AuthorID" );
    $valueAuthorID = $xmlAuthorID->item(0)->nodeValue;

    echo "$valueID - $valueDate - $valueAuthorID\n";
}
?>

- - - - - - - - - - - - - -

Output:

1 - REVIEW - 1
2 - REVIEW - 2

- - - - - - - - - - - - - -


Returns attribute node

  • ≪ DOMElement::getAttribute
  • DOMElement::getAttributeNodeNS ≫
  • PHP Manual
  • DOMElement
  • Returns attribute node

DOMElement::getAttributeNode

(PHP 5, PHP 7)

DOMElement::getAttributeNode — Returns attribute node

설명

public DOMAttr DOMElement::getAttributeNode ( string $name )

Returns the attribute node with name name for the current element.

인수

name

The name of the attribute.

반환값

The attribute node.

참고

  • DOMElement::hasAttribute() - Checks to see if attribute exists
  • DOMElement::setAttributeNode() - Adds new attribute node to element
  • DOMElement::removeAttributeNode() - Removes attribute

User Contributed Notes

lowezar at gmail dot com 28-Feb-2011 05:22
It may also return DOMNameSpaceNode if you`re requesting 'xmlns:_prefix_'. So you`re unable to remove it with removeAttributeNode() which requires DOMAttr as parameter.
Alex 18-Feb-2009 05:33
Just trying to add some clarification: this function will return boolean "false" when there is not a $name attribute :)

Cheers, Alex


Returns attribute node

  • ≪ DOMElement::getAttributeNode
  • DOMElement::getAttributeNS ≫
  • PHP Manual
  • DOMElement
  • Returns attribute node

DOMElement::getAttributeNodeNS

(PHP 5, PHP 7)

DOMElement::getAttributeNodeNS — Returns attribute node

설명

public DOMAttr DOMElement::getAttributeNodeNS ( string $namespaceURI , string $localName )

Returns the attribute node in namespace namespaceURI with local name localName for the current node.

인수

namespaceURI

The namespace URI.

localName

The local name.

반환값

The attribute node.

참고

  • DOMElement::hasAttributeNS() - Checks to see if attribute exists
  • DOMElement::setAttributeNodeNS() - Adds new attribute node to element
  • DOMElement::removeAttributeNode() - Removes attribute

User Contributed Notes

There are no user contributed notes for this page.


Returns value of attribute

  • ≪ DOMElement::getAttributeNodeNS
  • DOMElement::getElementsByTagName ≫
  • PHP Manual
  • DOMElement
  • Returns value of attribute

DOMElement::getAttributeNS

(PHP 5, PHP 7)

DOMElement::getAttributeNS — Returns value of attribute

설명

public string DOMElement::getAttributeNS ( string $namespaceURI , string $localName )

Gets the value of the attribute in namespace namespaceURI with local name localName for the current node.

인수

namespaceURI

The namespace URI.

localName

The local name.

반환값

The value of the attribute, or an empty string if no attribute with the given localName and namespaceURI is found.

참고

  • DOMElement::hasAttributeNS() - Checks to see if attribute exists
  • DOMElement::setAttributeNS() - Adds new attribute
  • DOMElement::removeAttributeNS() - Removes attribute

User Contributed Notes

There are no user contributed notes for this page.


Gets elements by tagname

  • ≪ DOMElement::getAttributeNS
  • DOMElement::getElementsByTagNameNS ≫
  • PHP Manual
  • DOMElement
  • Gets elements by tagname

DOMElement::getElementsByTagName

(PHP 5, PHP 7)

DOMElement::getElementsByTagName — Gets elements by tagname

설명

public DOMNodeList DOMElement::getElementsByTagName ( string $name )

This function returns a new instance of the class DOMNodeList of all descendant elements with a given tag name, in the order in which they are encountered in a preorder traversal of this element tree.

인수

name

The tag name. Use * to return all elements within the element tree.

반환값

This function returns a new instance of the class DOMNodeList of all matched elements.

참고

  • DOMElement::getElementsByTagNameNS() - Get elements by namespaceURI and localName

User Contributed Notes

ddkkdd twenty twenty at gmail dot com 09-Sep-2010 12:15
A simple function that uses this method to print the nodes it finds in an xml document. Improvements welcomed.

<?php
function printTags($tag){

  $node = $this->doc->getElementsByTagName($tag);
   for($c = 0; $c<$node->length; $c++){
     $text[$c] =$this->doc->saveXML($node->item($c));
     echo $text[$c];

   }

}
?>


Get elements by namespaceURI and localName

  • ≪ DOMElement::getElementsByTagName
  • DOMElement::hasAttribute ≫
  • PHP Manual
  • DOMElement
  • Get elements by namespaceURI and localName

DOMElement::getElementsByTagNameNS

(PHP 5, PHP 7)

DOMElement::getElementsByTagNameNS — Get elements by namespaceURI and localName

설명

public DOMNodeList DOMElement::getElementsByTagNameNS ( string $namespaceURI , string $localName )

This function fetch all the descendant elements with a given localName and namespaceURI.

인수

namespaceURI

The namespace URI.

localName

The local name. Use * to return all elements within the element tree.

반환값

This function returns a new instance of the class DOMNodeList of all matched elements in the order in which they are encountered in a preorder traversal of this element tree.

참고

  • DOMElement::getElementsByTagName() - Gets elements by tagname

User Contributed Notes

spam at chovy dot com 04-Jun-2009 02:36
I had some difficulty stripping all default NS attributes for an ns-uri in one shot, the following will work though...first strip the documentElement namespace, then getElementsByTagNameNS() -- the documentation should reflect that the 2nd argument is actually the name of the tag, not the local namespace prefix as I first expected:

<?php

function strip_default_ns( $xml = null, $ns_uri = 'http://example.com/XML-Foo' ) {
    $ns_local = '';
    $ns_tag = '*';

    if ( empty($xml) ) return false;

    //remove document namespace
    $dom = new DOMDocument();
    $dom->loadXML($xml);
    $dom->documentElement->removeAttributeNS($ns_uri, $ns_local);

    //strip element namespaces
    foreach ( $dom->getElementsByTagNameNS($ns_uri, $ns_tag) as $elem ) {
        $elem->removeAttributeNS($ns_uri, $ns_local);
    }

    return $dom->saveXML();
}

$stripped_xml = strip_default_ns($the_xml);

?>

$stripped_xml can now take advantage of running XPath queries on it for the NULL namespace.


Checks to see if attribute exists

  • ≪ DOMElement::getElementsByTagNameNS
  • DOMElement::hasAttributeNS ≫
  • PHP Manual
  • DOMElement
  • Checks to see if attribute exists

DOMElement::hasAttribute

(PHP 5, PHP 7)

DOMElement::hasAttribute — Checks to see if attribute exists

설명

public bool DOMElement::hasAttribute ( string $name )

Indicates whether attribute named name exists as a member of the element.

인수

name

The attribute name.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • DOMElement::hasAttributeNS() - Checks to see if attribute exists
  • DOMElement::getAttribute() - Returns value of attribute
  • DOMElement::setAttribute() - Adds new attribute
  • DOMElement::removeAttribute() - Removes attribute

User Contributed Notes

There are no user contributed notes for this page.


Checks to see if attribute exists

  • ≪ DOMElement::hasAttribute
  • DOMElement::removeAttribute ≫
  • PHP Manual
  • DOMElement
  • Checks to see if attribute exists

DOMElement::hasAttributeNS

(PHP 5, PHP 7)

DOMElement::hasAttributeNS — Checks to see if attribute exists

설명

public bool DOMElement::hasAttributeNS ( string $namespaceURI , string $localName )

Indicates whether attribute in namespace namespaceURI named localName exists as a member of the element.

인수

namespaceURI

The namespace URI.

localName

The local name.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • DOMElement::hasAttribute() - Checks to see if attribute exists
  • DOMElement::getAttributeNS() - Returns value of attribute
  • DOMElement::setAttributeNS() - Adds new attribute
  • DOMElement::removeAttributeNS() - Removes attribute

User Contributed Notes

There are no user contributed notes for this page.


Removes attribute

  • ≪ DOMElement::hasAttributeNS
  • DOMElement::removeAttributeNode ≫
  • PHP Manual
  • DOMElement
  • Removes attribute

DOMElement::removeAttribute

(PHP 5, PHP 7)

DOMElement::removeAttribute — Removes attribute

설명

public bool DOMElement::removeAttribute ( string $name )

Removes attribute named name from the element.

인수

name

The name of the attribute.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

참고

  • DOMElement::hasAttribute() - Checks to see if attribute exists
  • DOMElement::getAttribute() - Returns value of attribute
  • DOMElement::setAttribute() - Adds new attribute

User Contributed Notes

suwayan at mail dot ru 30-Mar-2012 09:19
<?php
/*When I try to get a some attribute from not validated HTML or XML document, PHP dies with no errors in logs or output:
*/
        function is_attribute_value($obj,$type,$value)
        {
            $_ret=false;
            if($obj)
            {
                if($val=$obj->getAttribute($type))
                {
                    if($val==$value)
                    {
                        $_ret=true;
                    }
                }
            }
            return $_ret;
        }
//And this check helped to me:
        function is_attribute_value($obj,$type,$value)
        {
            $_ret=false;
            if($obj->attributes)
            {
                if($val=$obj->getAttribute($type))
                {
                    if($val==$value)
                    {
                        $_ret=true;
                    }
                }
            }
            return $_ret;
        }
?>
Rakesh Verma - rakeshnsony at gmail dot com 11-Nov-2010 08:38
<?php

//Store your html into $html variable.

$html="<html>
<head>
<title>Rakesh Verma</title>
</head>

<body>
    <a href='http://example.com'>Example</a>
    <a href='http://google.com'>Google</a>
    <a href='http://www.yahoo.com'>Yahoo</a>
</body>

</html>";

$dom = new DOMDocument();
$dom->loadHTML($html);

//Evaluate Anchor tag in HTML
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
        $href = $hrefs->item($i);
        $url = $href->getAttribute('href');

        //remove and set target attribute
        $href->removeAttribute('target');
        $href->setAttribute("target", "_blank");

        $newURL=$url.".au";

        //remove and set href attribute
        $href->removeAttribute('href');
        $href->setAttribute("href", $newURL);
}

// save html
$html=$dom->saveHTML();

echo $html;

?>


Removes attribute

  • ≪ DOMElement::removeAttribute
  • DOMElement::removeAttributeNS ≫
  • PHP Manual
  • DOMElement
  • Removes attribute

DOMElement::removeAttributeNode

(PHP 5, PHP 7)

DOMElement::removeAttributeNode — Removes attribute

설명

public bool DOMElement::removeAttributeNode ( DOMAttr $oldnode )

Removes attribute oldnode from the element.

인수

oldnode

The attribute node.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

DOM_NOT_FOUND_ERROR

Raised if oldnode is not an attribute of the element.

참고

  • DOMElement::hasAttribute() - Checks to see if attribute exists
  • DOMElement::getAttributeNode() - Returns attribute node
  • DOMElement::setAttributeNode() - Adds new attribute node to element

User Contributed Notes

xr07354 at gmx dot de 18-Jun-2013 10:16
Basic: I use PHP5.4.9 from Ubuntu 13.04 repository. The aim of my code is to iterate HTML source (as a DomDocument) recursively and cleanup everything that is not valid to be used inside Epub files (i.e. and attribute align is not valid for paragraphs in Epubs).

FIRST: Today I tried removing attributes from a DOMElement using this simple code:
<?php
for ( $k=0; $k < $element->attributes->length; $k++) {
    if( /* some rule */ ){
        var_dump( $element->attributes->item($k)->nodeName);
        $element->removeAttributeNode( $element->attributes->item($k));
    }
}
?>
Unfortunately all attributes still existed when this loop was finished, even if these var_dumps told me that deleting them was tried.

I solved this problem iterating the attributes list backward:
<?php
for ( $k = $element->attributes->length - 1; $k >= 0; --$k) {
    if( /* same rule */ ){
        var_dump( $element->attributes->item($k)->nodeName);
        $element->removeAttributeNode( $element->attributes->item($k));
    }
}
?>
SECOND: DOMElement::removeAttributeNode does NOT return a bool but a DOMAttr object.


Removes attribute

  • ≪ DOMElement::removeAttributeNode
  • DOMElement::setAttribute ≫
  • PHP Manual
  • DOMElement
  • Removes attribute

DOMElement::removeAttributeNS

(PHP 5, PHP 7)

DOMElement::removeAttributeNS — Removes attribute

설명

public bool DOMElement::removeAttributeNS ( string $namespaceURI , string $localName )

Removes attribute is namespace namespaceURI named localName from the element.

인수

namespaceURI

The namespace URI.

localName

The local name.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

참고

  • DOMElement::hasAttributeNS() - Checks to see if attribute exists
  • DOMElement::getAttributeNS() - Returns value of attribute
  • DOMElement::setAttributeNS() - Adds new attribute

User Contributed Notes

primaryspace at hotmail dot com 10-Aug-2005 09:56
This method can be used to remove a namespace declaration from an element, even though the declaration is not considered an attribute by the DOM.  The prefix will be removed from any element or attribute using it.

<?php

$dom = DOMDocument::loadXML('<foo:root xmlns:foo="http:bar"/>');
$element = $dom->childNodes->item(0);

// prints '0':
echo (int) $element->hasAttributeNS('http:bar', 'foo');

$element->removeAttributeNS('http:bar', 'foo');

/* prints '<?xml version="1.0"?> <root/>': */
print htmlentities($dom->saveXML());

?>


Adds new attribute

  • ≪ DOMElement::removeAttributeNS
  • DOMElement::setAttributeNode ≫
  • PHP Manual
  • DOMElement
  • Adds new attribute

DOMElement::setAttribute

(PHP 5, PHP 7)

DOMElement::setAttribute — Adds new attribute

설명

public DOMAttr DOMElement::setAttribute ( string $name , string $value )

Sets an attribute with name name to the given value. If the attribute does not exist, it will be created.

인수

name

The name of the attribute.

value

The value of the attribute.

반환값

The new DOMAttr or FALSE if an error occurred.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

예제

Example #1 Setting an attribute

<?php
$doc = new DOMDocument("1.0");
$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);
$newnode->setAttribute("align", "left");
?>

참고

  • DOMElement::hasAttribute() - Checks to see if attribute exists
  • DOMElement::getAttribute() - Returns value of attribute
  • DOMElement::removeAttribute() - Removes attribute

User Contributed Notes

lehal2@hotmail 06-Jun-2013 06:44
The use of Dom to first remove and then add the width and height to the first img tag from the text.I hope it help you to save your time
<?php
$html = '

            <img src="http://www.example.com/images/header.jpg" width="898" height="223" style="border-bottom:5px solid #cccccc;"/>
        <img src="http://www.example.com/images/header2.jpg" width="898" height="223" style="border-bottom:5px solid #cccccc;"/>
        ';

$doc = DOMDocument::loadHTML($html);
$c =0;
foreach($doc->getElementsByTagName('img') as $image){
    if ($c>0) continue;
    foreach(array('width', 'height') as $attribute_to_remove){
        echo $attribute_to_remove;
        if($image->hasAttribute($attribute_to_remove)){
            $image->removeAttribute($attribute_to_remove);

        }
        if($attribute_to_remove=='height'){
            if(!$image->hasAttribute($attribute_to_remove)){
                $image->setAttribute($attribute_to_remove,'220');

            }}
        if($attribute_to_remove=='width'){
      if(!$image->hasAttribute($attribute_to_remove)){
            $image->setAttribute($attribute_to_remove,'700');

        }}
        $c = $c+1;
    }
}
echo $doc->saveHTML();
Rakesh Verma - rakeshnsony at gmail dot com 11-Nov-2010 08:27
<?php
//Store your html into $html variable.
$html="
<html>
<head>
<title>Untitled Document</title>
</head>

<body>
    <a href='http://example.com'>Example</a><br>
    <a href='http://google.com'>Google</a><br>

    <a href='http://www.yahoo.com'>Yahoo</a><br>
</body>

</html>";

$dom = new DOMDocument();
$dom->loadHTML($html);

//Evaluate Anchor tag in HTML
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

for ($i = 0; $i < $hrefs->length; $i++) {
        $href = $hrefs->item($i);
        $url = $href->getAttribute('href');

        //remove and set target attribute
        $href->removeAttribute('target');
        $href->setAttribute("target", "_blank");

        $newURL=$url."/newurl";

        //remove and set href attribute
        $href->removeAttribute('href');
        $href->setAttribute("href", $newURL);
}

// save html
$html=$dom->saveHTML();

echo $html;

?>
address at gmail dot com 03-Jul-2008 04:25
If wanting to set an attribute of an element with unique id of "1"

<?php
$dom = new DomDocument();
$dom->load('test.xml');
$xp = new DomXPath($dom);
$res = $xp->query("//*[@id = '1']");
$res->item(0)->setAttribute('title','2');
$dom->save('test.xml');
?>
Vasil Rangelov 01-Aug-2007 01:44
@karvjorm,
Using this method is not exactly a good practice. Infact, I think it may be a bug.
Names beginning with "xml" are reserved (for namespaces in this case).
setAttribute() should return false in this case I think, but I suppose it doesn't.

Still, the right way to do it is with createElementNS().
It lets you specify the namespace when creating the element node.

So the equivalent to yours ($html added to allow word wrapping) is:

$dom = new DomDocument('1.0','iso-8859-15');
$html = $dom->createElementNS('http://www.w3.org/1999/xhtml', 'html');
$ht_ml = $dom->appendChild($html);
$ht_ml->setAttribute('xml:lang','fi');
$ht_ml->setAttribute('lang','fi');
karvjorm at users.sourceforge.net 03-Mar-2007 12:05
$dom = new DomDocument('1.0','iso-8859-15');

$ht_ml = $dom->appendChild($dom->createElement('html'));
$ht_ml->setAttribute('xmlns','http://www.w3.org/1999/xhtml');
$ht_ml->setAttribute('xml:lang','fi');
$ht_ml->setAttribute('lang','fi');

Result:

<?xml version="1.0" encoding="iso-8859-15"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi" lang="fi">


Adds new attribute node to element

  • ≪ DOMElement::setAttribute
  • DOMElement::setAttributeNodeNS ≫
  • PHP Manual
  • DOMElement
  • Adds new attribute node to element

DOMElement::setAttributeNode

(PHP 5, PHP 7)

DOMElement::setAttributeNode — Adds new attribute node to element

설명

public DOMAttr DOMElement::setAttributeNode ( DOMAttr $attr )

Adds new attribute node attr to element.

인수

attr

The attribute node.

반환값

Returns old node if the attribute has been replaced or NULL.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

참고

  • DOMElement::hasAttribute() - Checks to see if attribute exists
  • DOMElement::getAttributeNode() - Returns attribute node
  • DOMElement::removeAttributeNode() - Removes attribute

User Contributed Notes

karvjorm at users.sourceforge.net 03-Mar-2007 01:01
$dom = new DomDocument('1.0','iso-8859-15');

$ht_ml = $dom->appendChild($dom->createElement('html'));

$he_ad = $ht_ml->appendChild($dom->createElement('head'));

$tit_le= $he_ad->appendChild($dom->createElement('title'));
$tit_le->appendChild($dom->createTextNode('DOMAttr test'));

$me_ta = $he_ad->appendChild(new DOMElement('meta'));
$me_ta->setAttributeNode(new DOMAttr('name', 'Description'));
$me_ta->setAttributeNode(new DOMAttr('content', 'example'));

$me_ta = $he_ad->appendChild(new DOMElement('meta'));
$me_ta->setAttributeNode(new DOMAttr('name', 'Author'));
$me_ta->setAttributeNode(new DOMAttr('content', 'karvjorm'));

Result:

<?xml version="1.0" encoding="iso-8859-15"?>
<html>
  <head>
    <title>DOMAttr test</title>
    <meta name="Description" content="example"/>
    <meta name="Author" content="karvjorm"/>
  </head>


Adds new attribute node to element

  • ≪ DOMElement::setAttributeNode
  • DOMElement::setAttributeNS ≫
  • PHP Manual
  • DOMElement
  • Adds new attribute node to element

DOMElement::setAttributeNodeNS

(PHP 5, PHP 7)

DOMElement::setAttributeNodeNS — Adds new attribute node to element

설명

public DOMAttr DOMElement::setAttributeNodeNS ( DOMAttr $attr )

Adds new attribute node attr to element.

인수

attr

The attribute node.

반환값

Returns the old node if the attribute has been replaced.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

참고

  • DOMElement::hasAttributeNS() - Checks to see if attribute exists
  • DOMElement::getAttributeNodeNS() - Returns attribute node
  • DOMElement::removeAttributeNode() - Removes attribute

User Contributed Notes

There are no user contributed notes for this page.


Adds new attribute

  • ≪ DOMElement::setAttributeNodeNS
  • DOMElement::setIdAttribute ≫
  • PHP Manual
  • DOMElement
  • Adds new attribute

DOMElement::setAttributeNS

(PHP 5, PHP 7)

DOMElement::setAttributeNS — Adds new attribute

설명

public void DOMElement::setAttributeNS ( string $namespaceURI , string $qualifiedName , string $value )

Sets an attribute with namespace namespaceURI and name name to the given value. If the attribute does not exist, it will be created.

인수

namespaceURI

The namespace URI.

qualifiedName

The qualified name of the attribute, as prefix:tagname.

value

The value of the attribute.

반환값

값을 반환하지 않습니다.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

DOM_NAMESPACE_ERR

Raised if qualifiedName is a malformed qualified name, or if qualifiedName has a prefix and namespaceURI is NULL.

참고

  • DOMElement::hasAttributeNS() - Checks to see if attribute exists
  • DOMElement::getAttributeNS() - Returns value of attribute
  • DOMElement::removeAttributeNS() - Removes attribute

User Contributed Notes

catalinenache78 at gmail dot com 18-May-2011 04:39
To add new brand xml namespace use:

<?php
    $element->setAttributeNS(
        'http://www.w3.org/2000/xmlns/', // xmlns namespace URI
        'xmlns:mynamespace',
        'example.com/mynamespace'
    );
?>

'http://www.w3.org/2000/xmlns/' URI is important
to be able to add  new namespaces !!!

Later you can use your namespace like:

<?php
    $element->setAttributeNS(
        'example.com/mynamespace',
        'mynamespace:something',
        'value'
    );
?>


Declares the attribute specified by name to be of type ID

  • ≪ DOMElement::setAttributeNS
  • DOMElement::setIdAttributeNode ≫
  • PHP Manual
  • DOMElement
  • Declares the attribute specified by name to be of type ID

DOMElement::setIdAttribute

(PHP 5, PHP 7)

DOMElement::setIdAttribute — Declares the attribute specified by name to be of type ID

설명

public void DOMElement::setIdAttribute ( string $name , bool $isId )

Declares the attribute name to be of type ID.

인수

name

The name of the attribute.

isId

Set it to TRUE if you want name to be of type ID, FALSE otherwise.

반환값

값을 반환하지 않습니다.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

DOM_NOT_FOUND

Raised if name is not an attribute of this element.

참고

  • DOMDocument::getElementById() - Searches for an element with a certain id
  • DOMElement::setIdAttributeNode() - Declares the attribute specified by node to be of type ID
  • DOMElement::setIdAttributeNS() - Declares the attribute specified by local name and namespace URI to be of type ID

User Contributed Notes

There are no user contributed notes for this page.


Declares the attribute specified by node to be of type ID

  • ≪ DOMElement::setIdAttribute
  • DOMElement::setIdAttributeNS ≫
  • PHP Manual
  • DOMElement
  • Declares the attribute specified by node to be of type ID

DOMElement::setIdAttributeNode

(PHP 5, PHP 7)

DOMElement::setIdAttributeNode — Declares the attribute specified by node to be of type ID

설명

public void DOMElement::setIdAttributeNode ( DOMAttr $attr , bool $isId )

Declares the attribute specified by attr to be of type ID.

인수

attr

The attribute node.

isId

Set it to TRUE if you want name to be of type ID, FALSE otherwise.

반환값

값을 반환하지 않습니다.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

DOM_NOT_FOUND

Raised if name is not an attribute of this element.

참고

  • DOMDocument::getElementById() - Searches for an element with a certain id
  • DOMElement::setIdAttribute() - Declares the attribute specified by name to be of type ID
  • DOMElement::setIdAttributeNS() - Declares the attribute specified by local name and namespace URI to be of type ID

User Contributed Notes

There are no user contributed notes for this page.


Declares the attribute specified by local name and namespace URI to be of type ID

  • ≪ DOMElement::setIdAttributeNode
  • DOMEntity ≫
  • PHP Manual
  • DOMElement
  • Declares the attribute specified by local name and namespace URI to be of type ID

DOMElement::setIdAttributeNS

(PHP 5, PHP 7)

DOMElement::setIdAttributeNS — Declares the attribute specified by local name and namespace URI to be of type ID

설명

public void DOMElement::setIdAttributeNS ( string $namespaceURI , string $localName , bool $isId )

Declares the attribute specified by localName and namespaceURI to be of type ID.

인수

namespaceURI

The namespace URI of the attribute.

localName

The local name of the attribute, as prefix:tagname.

isId

Set it to TRUE if you want name to be of type ID, FALSE otherwise.

반환값

값을 반환하지 않습니다.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if the node is readonly.

DOM_NOT_FOUND

Raised if name is not an attribute of this element.

참고

  • DOMDocument::getElementById() - Searches for an element with a certain id
  • DOMElement::setIdAttribute() - Declares the attribute specified by name to be of type ID
  • DOMElement::setIdAttributeNode() - Declares the attribute specified by node to be of type ID

User Contributed Notes

There are no user contributed notes for this page.


The DOMEntity class

(PHP 5, PHP 7)

소개

This interface represents a known entity, either parsed or unparsed, in an XML document.

클래스 개요

DOMEntity extends DOMNode {
/* 프로퍼티 */
readonly public string $publicId ;
readonly public string $systemId ;
readonly public string $notationName ;
public string $actualEncoding ;
readonly public string $encoding ;
readonly public string $version ;
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

프로퍼티

publicId

The public identifier associated with the entity if specified, and NULL otherwise.

systemId

The system identifier associated with the entity if specified, and NULL otherwise. This may be an absolute URI or not.

notationName

For unparsed entities, the name of the notation for the entity. For parsed entities, this is NULL.

actualEncoding

An attribute specifying the encoding used for this entity at the time of parsing, when it is an external parsed entity. This is NULL if it an entity from the internal subset or if it is not known.

encoding

An attribute specifying, as part of the text declaration, the encoding of this entity, when it is an external parsed entity. This is NULL otherwise.

version

An attribute specifying, as part of the text declaration, the version number of this entity, when it is an external parsed entity. This is NULL otherwise.

User Contributed Notes

There are no user contributed notes for this page.


The DOMEntityReference class

(PHP 5, PHP 7)

클래스 개요

DOMEntityReference extends DOMNode {
/* 프로퍼티 */
/* 메소드 */
public __construct ( string $name )
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

Table of Contents

  • DOMEntityReference::__construct — Creates a new DOMEntityReference object

User Contributed Notes

There are no user contributed notes for this page.


Creates a new DOMEntityReference object

  • ≪ DOMEntityReference
  • DOMException ≫
  • PHP Manual
  • DOMEntityReference
  • Creates a new DOMEntityReference object

DOMEntityReference::__construct

(PHP 5, PHP 7)

DOMEntityReference::__construct — Creates a new DOMEntityReference object

설명

public DOMEntityReference::__construct ( string $name )

Creates a new DOMEntityReference object.

인수

name

The name of the entity reference.

예제

Example #1 Creating a new DOMEntityReference

<?php

$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$entity = $element->appendChild(new DOMEntityReference('nbsp'));
echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?><root>&nbsp;</root> */

?>

참고

  • DOMDocument::createEntityReference() - Create new entity reference node

User Contributed Notes

There are no user contributed notes for this page.


The DOMException class

(PHP 5, PHP 7)

소개

DOM operations raise exceptions under particular circumstances, i.e., when an operation is impossible to perform for logical reasons.

See also 예외.

클래스 개요

DOMException extends Exception {
/* 프로퍼티 */
readonly public int $code ;
/* 상속된 메소드 */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}

프로퍼티

code

An integer indicating the type of error generated

User Contributed Notes

There are no user contributed notes for this page.


The DOMImplementation class

(PHP 5, PHP 7)

소개

The DOMImplementation interface provides a number of methods for performing operations that are independent of any particular instance of the document object model.

클래스 개요

DOMImplementation {
/* 프로퍼티 */
/* 메소드 */
__construct ( void )
public DOMDocument createDocument ([ string $namespaceURI = NULL [, string $qualifiedName = NULL [, DOMDocumentType $doctype = NULL ]]] )
public DOMDocumentType createDocumentType ([ string $qualifiedName = NULL [, string $publicId = NULL [, string $systemId = NULL ]]] )
public bool hasFeature ( string $feature , string $version )
}

Table of Contents

  • DOMImplementation::__construct — Creates a new DOMImplementation object
  • DOMImplementation::createDocument — Creates a DOMDocument object of the specified type with its document element
  • DOMImplementation::createDocumentType — Creates an empty DOMDocumentType object
  • DOMImplementation::hasFeature — Test if the DOM implementation implements a specific feature

User Contributed Notes

LANGE.LUDO 08-Nov-2014 03:05
Ok got it working like a charm using "proxy pattern" with traits. The idea being declaring the common methods inside a "trait" in order for extended and registered Node Classes to have access even if not derived / child of the extended DOMNode...

Here a small snippet :
<?php
    namespace my;

    trait tNode
    {    // We need the magic method __get in order to add properties such as DOMNode->parentElement
        public function __get($name)
        {    if(property_exists($this, $name)){return $this->$name;}
            if(method_exists($this, $name)){return $this->$name();}
            throw new \ErrorException('my\\Node property \''.(string) $name.'\' not found...', 42, E_USER_WARNING);
        }

        // parentElement property definition
        private function parentElement()
        {    if($this->parentNode === null){return null;}
            if($this->parentNode->nodeType === XML_ELEMENT_NODE){return $this->parentNode;}
            return $this->parentNode->parentElement();
        }

        // JavaScript equivalent
        public function isEqualNode(\DOMNode $node){return $this->isSameNode($node);}
        public function compareDocumentPosition(\DOMNode $otherNode)
        {    if($this->ownerDocument !== $otherNode->ownerDocument){return DOCUMENT_POSITION_DISCONNECTED;}
            $c = strcmp($this->getNodePath(), $otherNode->getNodePath());
            if($c === 0){return 0;}
            else if($c < 0){return DOCUMENT_POSITION_FOLLOWING | ($c < -1 ? DOCUMENT_POSITION_CONTAINED_BY : 0);}
            return DOCUMENT_POSITION_PRECEDING | ($c > 1 ? DOCUMENT_POSITION_CONTAINS : 0);
        }
        public function contains(\DOMNode $otherNode){return ($this->compareDocumentPosition($otherNode) >= DOCUMENT_POSITION_CONTAINED_BY);}
    }

    class Document extends \DomDocument
    {    public function __construct($version=null, $encoding=null)
        {    parent::__construct($version, $encoding);
            $this->registerNodeClass('DOMNode', 'my\Node');
            $this->registerNodeClass('DOMElement', 'my\Element');
            $this->registerNodeClass('DOMDocument', 'my\Document');
            /* [...] */
        }
    }

    class Element extends \DOMElement
    {    use tNode;
        /* [...] */
    }

    class Node extends \DOMNode
    {    use tNode;
        /* [...] */
    }

?>
LANGE.LUDO 07-Nov-2014 05:14
From what I've seen you must "recode" the "createDocument" method in order to call your own object extension...

<?php
[...]
    public function createDocument($namespaceURI=null, $qualifiedName=null, DOMDocumentType $docType=null)
    {    $doc=new Document();
        $doc->appendChild(parent::createDocumentType('html'));
        $this->doc->appendChild($namespaceURI ? $this->doc->createElementNS($namespaceURI,$qualifiedName) : $this->doc->createElement($qualifiedName));
    return $doc;
    }
[...]
?>

Where I am struggling is extending the whole tree so that calling the customized Implementation method returns a whole extended tree...
Expected result : Implementation > Document > DOMDocument > Node > DOMNode
Actual result : Implementation > Document > DOMDocument > DOMNode :(
LANGE.LUDO 06-Nov-2014 07:59
Why not use the DOMImplementation to directly register your DOM Class extensions ?

<?php
namespace My;
class Implementation extends \DOMImplementation
{    private $doc;
    public function __construct(){return $this->createDocument();}
    public function __get($name){return $this->doc->{$name};}
    public function __set($name,$value){$this->doc->{$name}=$value;}
    public function __isset($name){return isset($this->doc->{$name});}
    public function __unset($name){return $this->doc->__unset($name);}
    public function __call($name,$args){return call_user_func_array(array($this->doc,$name),$args);}

    public function createDocument($namespaceURI=null,$qualifiedName=null,DOMDocumentType $docType=null)
    {    $this->doc=parent::createDocument($namespaceURI,$qualifiedName,$docType);
        $this->doc->xmlVersion='1.0';
        $this->doc->xmlEncoding='UTF-8';
        $this->doc->registerNodeClass('DOMDocument','My\Document');
        $this->doc->registerNodeClass('DOMDocumentFragment','My\DocumentFragment');
        $this->doc->registerNodeClass('DOMElement','My\Element');
        $this->doc->registerNodeClass('DOMComment','My\Comment');
        $this->doc->registerNodeClass('DOMNode','My\Node');
        /* ..., ..., ... */
        return $this->doc;
    }
}
?>
giorgio dot liscio at email dot it 21-Jul-2010 03:26
officially, the w3 specifies that the way to access dom interfaces is through this class

so if you use

$doc = new DOMDocument("1.0", "UTF-8");

use instead:

$x = new DOMImplementation();
$doc = $x->createDocument(NULL,"rootElementName");
$doc->xmlVersion="1.0";
$doc->xmlEncoding="UTF-8";

it is not required by php's implementation, but, probably, it is a good practice

see:

http://w3.org/TR/DOM-Level-3-Core/core.html#DOMImplementation

http://w3.org/TR/DOM-Level-3-Core/core.html#DOMImplementationList

http://w3.org/TR/DOM-Level-3-Core/core.html#DOMImplementationSource


Creates a new DOMImplementation object

  • ≪ DOMImplementation
  • DOMImplementation::createDocument ≫
  • PHP Manual
  • DOMImplementation
  • Creates a new DOMImplementation object

DOMImplementation::__construct

(PHP 5, PHP 7)

DOMImplementation::__construct — Creates a new DOMImplementation object

설명

DOMImplementation::__construct ( void )

Creates a new DOMImplementation object.

User Contributed Notes

There are no user contributed notes for this page.


Creates a DOMDocument object of the specified type with its document element

  • ≪ DOMImplementation::__construct
  • DOMImplementation::createDocumentType ≫
  • PHP Manual
  • DOMImplementation
  • Creates a DOMDocument object of the specified type with its document element

DOMImplementation::createDocument

(PHP 5, PHP 7)

DOMImplementation::createDocument — Creates a DOMDocument object of the specified type with its document element

설명

public DOMDocument DOMImplementation::createDocument ([ string $namespaceURI = NULL [, string $qualifiedName = NULL [, DOMDocumentType $doctype = NULL ]]] )

Creates a DOMDocument object of the specified type with its document element.

인수

namespaceURI

The namespace URI of the document element to create.

qualifiedName

The qualified name of the document element to create.

doctype

The type of document to create or NULL.

반환값

A new DOMDocument object. If namespaceURI, qualifiedName, and doctype are null, the returned DOMDocument is empty with no document element

오류/예외

DOM_WRONG_DOCUMENT_ERR

Raised if doctype has already been used with a different document or was created from a different implementation.

DOM_NAMESPACE_ERR

Raised if there is an error with the namespace, as determined by namespaceURI and qualifiedName.

이 메소드는 정적으로 호출할 수있지만, E_STRICT 오류가 발생합니다.

참고

  • DOMDocument::__construct() - Creates a new DOMDocument object
  • DOMImplementation::createDocumentType() - Creates an empty DOMDocumentType object

User Contributed Notes

eboyjr 22-Jul-2010 09:48
To add on to the other example, here's how to create an XHTML 1.0 transitional document with head, title, and body elements.

<?php

$document = DOMImplementation::createDocument(null, 'html',
    DOMImplementation::createDocumentType("html",
        "-//W3C//DTD XHTML 1.0 Transitional//EN",
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"));
$document->formatOutput = true;

$html = $document->documentElement;
$head = $document->createElement('head');
$title = $document->createElement('title');
$text = $document->createTextNode('Title of Page');
$body = $document->createElement('body');

$title->appendChild($text);
$head->appendChild($title);
$html->appendChild($head);
$html->appendChild($body);

echo $document->saveXML();
?>

This outputs: (http links removed due to spam)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "doctype.dtd">
<html xmlns="w3org1999xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; CHARSET=ks_c_5601-1987" />
    <title>Title of Page</title>
  </head>
  <body></body>
</html>

Note the saveXML function. If saveHTML was used instead, you get the output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "doctype.dtd">
<html>
<head><title>Title of Page</title></head>
<body></body>
</html>
arturm at union dot com dot pl 06-May-2006 07:23
To create HTML document with doctype:

<?php
$doctype = DOMImplementation::createDocumentType("html",
                "-//W3C//DTD HTML 4.01//EN",
                "http://www.w3.org/TR/html4/strict.dtd");
$doc = DOMImplementation::createDocument(null, 'html', $doctype);
?>


Creates an empty DOMDocumentType object

  • ≪ DOMImplementation::createDocument
  • DOMImplementation::hasFeature ≫
  • PHP Manual
  • DOMImplementation
  • Creates an empty DOMDocumentType object

DOMImplementation::createDocumentType

(PHP 5, PHP 7)

DOMImplementation::createDocumentType — Creates an empty DOMDocumentType object

설명

public DOMDocumentType DOMImplementation::createDocumentType ([ string $qualifiedName = NULL [, string $publicId = NULL [, string $systemId = NULL ]]] )

Creates an empty DOMDocumentType object. Entity declarations and notations are not made available. Entity reference expansions and default attribute additions do not occur.

인수

qualifiedName

The qualified name of the document type to create.

publicId

The external subset public identifier.

systemId

The external subset system identifier.

반환값

A new DOMDocumentType node with its ownerDocument set to NULL.

오류/예외

DOM_NAMESPACE_ERR

Raised if there is an error with the namespace, as determined by qualifiedName.

이 메소드는 정적으로 호출할 수있지만, E_STRICT 오류가 발생합니다.

예제

Example #1 Creating a document with an attached DTD

<?php

// Creates an instance of the DOMImplementation class
$imp = new DOMImplementation;

// Creates a DOMDocumentType instance
$dtd = $imp->createDocumentType('graph', '', 'graph.dtd');

// Creates a DOMDocument instance
$dom = $imp->createDocument("", "", $dtd);

// Set other properties
$dom->encoding = 'UTF-8';
$dom->standalone = false;

// Create an empty element
$element = $dom->createElement('graph');

// Append the element
$dom->appendChild($element);

// Retrieve and print the document
echo $dom->saveXML();

?>

위 예제의 출력:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE graph SYSTEM "graph.dtd">
<graph/>

참고

  • DOMImplementation::createDocument() - Creates a DOMDocument object of the specified type with its document element

User Contributed Notes

until-all-bytes-are-free at example dot org 09-Apr-2011 01:28
I had problems to use a DTD from a file. It needed to be resolved relatively and it contained characters that made DomDocument failed to resolve the file.

Encoding and an absolute filename did not help much. So I used the data:// streamwrapper ( http://php.net/manual/en/wrappers.data.php ) as a work-around:

<?php

// relative or absolute filename
$path = '...';

// convert file contents into a filename
$data = file_get_contents($path);
$systemId = 'data://text/plain;base64,'.base64_encode($data);

// ...

// Creates a DOMDocumentType instance
$dtd = $aImp->createDocumentType('qualified name', '', $systemId);

?>

Works like a charm.


Test if the DOM implementation implements a specific feature

  • ≪ DOMImplementation::createDocumentType
  • DOMNamedNodeMap ≫
  • PHP Manual
  • DOMImplementation
  • Test if the DOM implementation implements a specific feature

DOMImplementation::hasFeature

(PHP 5, PHP 7)

DOMImplementation::hasFeature — Test if the DOM implementation implements a specific feature

설명

public bool DOMImplementation::hasFeature ( string $feature , string $version )

Test if the DOM implementation implements a specific feature.

You can find a list of all features in the » Conformance section of the DOM specification.

인수

feature

The feature to test.

version

The version number of the feature to test. In level 2, this can be either 2.0 or 1.0.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

오류/예외

이 메소드는 정적으로 호출할 수있지만, E_STRICT 오류가 발생합니다.

예제

Example #1 Testing your DOM Implementation

<?php

$features = array(
  'Core'           => 'Core module',
  'XML'            => 'XML module',
  'HTML'           => 'HTML module',
  'Views'          => 'Views module',
  'Stylesheets'    => 'Style Sheets module',
  'CSS'            => 'CSS module',
  'CSS2'           => 'CSS2 module',
  'Events'         => 'Events module',
  'UIEvents'       => 'User interface Events module',
  'MouseEvents'    => 'Mouse Events module',
  'MutationEvents' => 'Mutation Events module',
  'HTMLEvents'     => 'HTML Events module',
  'Range'          => 'Range module',
  'Traversal'      => 'Traversal module'
);

foreach ($features as $key => $name) {
  if (DOMImplementation::hasFeature($key, '2.0')) {
    echo "Has feature $name\n";
  } else {
    echo "Missing feature $name\n";
  }
}

?>

참고

  • DOMNode::isSupported() - Checks if feature is supported for specified version

User Contributed Notes

There are no user contributed notes for this page.


The DOMNamedNodeMap class

(PHP 5, PHP 7)

클래스 개요

DOMNamedNodeMap implements Traversable {
/* 프로퍼티 */
readonly public int $length ;
/* 메소드 */
DOMNode getNamedItem ( string $name )
DOMNode getNamedItemNS ( string $namespaceURI , string $localName )
DOMNode item ( int $index )
}

프로퍼티

length

The number of nodes in the map. The range of valid child node indices is 0 to length - 1 inclusive.

Table of Contents

  • DOMNamedNodeMap::getNamedItem — Retrieves a node specified by name
  • DOMNamedNodeMap::getNamedItemNS — Retrieves a node specified by local name and namespace URI
  • DOMNamedNodeMap::item — Retrieves a node specified by index

User Contributed Notes

kendsnyder at gmail dot com 29-Oct-2009 04:55
To add to xafford's comment. When iterating a named node map collection using ->item() or using foreach, removing a attribute with DOMNode->removeAttribute() or DOMNode->removeAttributeNode() alters the collection as if it were a stack. To illustrate, the code below tries to remove all attributes from each element but only removes the first. One work around is to copy the named node map into an array before removing attributes. Using PHP 5.2.9 on Windows XP.

<?php
error_reporting(E_ALL);
$html  = '<h1 id="h1test" class="h1test">Heading</h1>';
$html .= '<p align="left" class="ptest">Hello World</p>';

$doc = new DOMDocument();
$doc->loadHTML($html);

// remove attributes from  the h1 element
$h1 = $doc->getElementsByTagName('h1')->item(0);
$length = $h1->attributes->length;
for ($i = 0; $i < $length; ++$i) {
    $name = $h1->attributes->item($i)->name;
    $h1->removeAttribute($name);
    echo "h1: removed attribute `$name`<br>";
}
// remove attributes from the p element
$p = $doc->getElementsByTagName('p')->item(0);
foreach ($p->attributes as $name => $attrNode) {
    $p->removeAttribute($name);
    echo "p: removed attribute `$name`<br>";
}

?>

OUTPUT:
-------
h1: removed attribute `id`

Notice: Trying to get property of non-object in nodemap.php on line 13
h1: removed attribute ``
p: removed attribute `align`
xafford 15-Oct-2009 12:16
I stumbled upon a problem with DOMNamedNodeMap. If you iterate over a DOMNamedNodeMap, representing the attributes of a DOMElement with foreach and you use DOMElement::removeAttributeNode only the first attribute will be handled.

Example (not complete):

<?php

/*
 * Imagine you got a node like this:
 * <a onclick="alert('evil')" href="http://example.com">evil</a>
 * and onclick should be removed, href would not be tested.
 */

foreach ( $node->attributes as $attribute )
{

    echo 'checking attribute ', $attribute->name, '<br />';

    if ( ! in_array ( $attribute->name, $allowed_attributes ) )
    {
        $node->removeAttributeNode ( $attribute );
    }

}

?>

The output would be:

checking attribute onclick


Retrieves a node specified by name

  • ≪ DOMNamedNodeMap
  • DOMNamedNodeMap::getNamedItemNS ≫
  • PHP Manual
  • DOMNamedNodeMap
  • Retrieves a node specified by name

DOMNamedNodeMap::getNamedItem

(PHP 5, PHP 7)

DOMNamedNodeMap::getNamedItem — Retrieves a node specified by name

설명

DOMNode DOMNamedNodeMap::getNamedItem ( string $name )

Retrieves a node specified by its nodeName.

인수

name

The nodeName of the node to retrieve.

반환값

A node (of any type) with the specified nodeName, or NULL if no node is found.

참고

  • DOMNamedNodeMap::getNamedItemNS() - Retrieves a node specified by local name and namespace URI

User Contributed Notes

franp at free dot fr 03-Mar-2006 04:28
Basic example of use :

<?xml version="1.0" encoding="UTF-8"?>
<racine version="2.0a">
  <article/>
</racine>

<?php
(...)
 echo $doc->documentElement->attributes->getNamedItem("version")->nodeValue;

// returns "2.0a"
?>


Retrieves a node specified by local name and namespace URI

  • ≪ DOMNamedNodeMap::getNamedItem
  • DOMNamedNodeMap::item ≫
  • PHP Manual
  • DOMNamedNodeMap
  • Retrieves a node specified by local name and namespace URI

DOMNamedNodeMap::getNamedItemNS

(PHP 5, PHP 7)

DOMNamedNodeMap::getNamedItemNS — Retrieves a node specified by local name and namespace URI

설명

DOMNode DOMNamedNodeMap::getNamedItemNS ( string $namespaceURI , string $localName )

Retrieves a node specified by localName and namespaceURI.

인수

namespaceURI

The namespace URI of the node to retrieve.

localName

The local name of the node to retrieve.

반환값

A node (of any type) with the specified local name and namespace URI, or NULL if no node is found.

참고

  • DOMNamedNodeMap::getNamedItem() - Retrieves a node specified by name

User Contributed Notes

There are no user contributed notes for this page.


Retrieves a node specified by index

  • ≪ DOMNamedNodeMap::getNamedItemNS
  • DOMNode ≫
  • PHP Manual
  • DOMNamedNodeMap
  • Retrieves a node specified by index

DOMNamedNodeMap::item

(PHP 5, PHP 7)

DOMNamedNodeMap::item — Retrieves a node specified by index

설명

DOMNode DOMNamedNodeMap::item ( int $index )

Retrieves a node specified by index within the DOMNamedNodeMap object.

인수

index

Index into this map.

반환값

The node at the indexth position in the map, or NULL if that is not a valid index (greater than or equal to the number of nodes in this map).

User Contributed Notes

reyjacobs at yahoo dot com 07-Mar-2008 08:24
Nevermind: I got it, and here it is:

<?php
if($element->hasAttributes())
{
    $attributes = $element->attributes;
    if(!is_null($attributes))
    {
        foreach ($attributes as $index=>$attr)
        {
            echo $attr->name."=\"".$attr->value."\"";
        }
    }
}
?>


The DOMNode class

(PHP 5, PHP 7)

클래스 개요

DOMNode {
/* 프로퍼티 */
public readonly string $nodeName ;
public string $nodeValue ;
public readonly int $nodeType ;
public readonly DOMNode $parentNode ;
public readonly DOMNodeList $childNodes ;
public readonly DOMNode $firstChild ;
public readonly DOMNode $lastChild ;
public readonly DOMNode $previousSibling ;
public readonly DOMNode $nextSibling ;
public readonly DOMNamedNodeMap $attributes ;
public readonly DOMDocument $ownerDocument ;
public readonly string $namespaceURI ;
public string $prefix ;
public readonly string $localName ;
public readonly string $baseURI ;
public string $textContent ;
/* 메소드 */
public DOMNode appendChild ( DOMNode $newnode )
public string C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode cloneNode ([ bool $deep ] )
public int getLineNo ( void )
public string getNodePath ( void )
public bool hasAttributes ( void )
public bool hasChildNodes ( void )
public DOMNode insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool isDefaultNamespace ( string $namespaceURI )
public bool isSameNode ( DOMNode $node )
public bool isSupported ( string $feature , string $version )
public string lookupNamespaceURI ( string $prefix )
public string lookupPrefix ( string $namespaceURI )
public void normalize ( void )
public DOMNode removeChild ( DOMNode $oldnode )
public DOMNode replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

프로퍼티

nodeName

Returns the most accurate name for the current node type

nodeValue

The value of this node, depending on its type. Contrary to the W3C specification, the node value of DOMElement nodes is equal to DOMNode::textContent instead of NULL.

nodeType

Gets the type of the node. One of the predefined XML_xxx_NODE constants

parentNode

The parent of this node. If there is no such node, this returns NULL.

childNodes

A DOMNodeList that contains all children of this node. If there are no children, this is an empty DOMNodeList.

firstChild

The first child of this node. If there is no such node, this returns NULL.

lastChild

The last child of this node. If there is no such node, this returns NULL.

previousSibling

The node immediately preceding this node. If there is no such node, this returns NULL.

nextSibling

The node immediately following this node. If there is no such node, this returns NULL.

attributes

A DOMNamedNodeMap containing the attributes of this node (if it is a DOMElement) or NULL otherwise.

ownerDocument

The DOMDocument object associated with this node.

namespaceURI

The namespace URI of this node, or NULL if it is unspecified.

prefix

The namespace prefix of this node, or NULL if it is unspecified.

localName

Returns the local part of the qualified name of this node.

baseURI

The absolute base URI of this node or NULL if the implementation wasn't able to obtain an absolute URI.

textContent

The text content of this node and its descendants.

주의

Note:

The DOM extension uses UTF-8 encoding. Use utf8_encode() and utf8_decode() to work with texts in ISO-8859-1 encoding or Iconv for other encodings.

변경점

버전 | 설명
5.6.1 | The textContent property has been made writable (formerly it has been readonly).

참고

  • » W3C specification of Node

Table of Contents

  • DOMNode::appendChild — Adds new child at the end of the children
  • DOMNode::C14N — Canonicalize nodes to a string
  • DOMNode::C14NFile — Canonicalize nodes to a file
  • DOMNode::cloneNode — Clones a node
  • DOMNode::getLineNo — Get line number for a node
  • DOMNode::getNodePath — Get an XPath for a node
  • DOMNode::hasAttributes — Checks if node has attributes
  • DOMNode::hasChildNodes — Checks if node has children
  • DOMNode::insertBefore — Adds a new child before a reference node
  • DOMNode::isDefaultNamespace — Checks if the specified namespaceURI is the default namespace or not
  • DOMNode::isSameNode — Indicates if two nodes are the same node
  • DOMNode::isSupported — Checks if feature is supported for specified version
  • DOMNode::lookupNamespaceURI — Gets the namespace URI of the node based on the prefix
  • DOMNode::lookupPrefix — Gets the namespace prefix of the node based on the namespace URI
  • DOMNode::normalize — Normalizes the node
  • DOMNode::removeChild — Removes child from list of children
  • DOMNode::replaceChild — Replaces a child

User Contributed Notes

zlk1214 at gmail dot com 15-Jan-2016 10:07
A function that can set the inner HTML without encoding error. $html can be broken content such as "<a ID=id20>ssss"
function setInnerHTML($node, $html) {
    removeChildren($node);
    if (empty($html)) {
        return;
    }

    $doc = $node->ownerDocument;
    $htmlclip = new DOMDocument();
    $htmlclip->loadHTML('<meta http-equiv="Content-Type" content="text/html;CHARSET=ks_c_5601-1987"><div>' . $html . '</div>');
    $clipNode = $doc->importNode($htmlclip->documentElement->lastChild->firstChild, true);
    while ($item = $clipNode->firstChild) {
        $node->appendChild($item);
    }
}
metanull 25-Jul-2014 12:11
Yet another DOMNode to php array conversion function.
Other ones on this page are generating too "complex" arrays; this one should keep the array as tidy as possible.
Note: make sure to set LIBXML_NOBLANKS when calling DOMDocument::load, loadXML or loadHTML
See: http://be2.php.net/manual/en/libxml.constants.php
See: http://be2.php.net/manual/en/domdocument.loadxml.php

<?php
         /**
         * Returns an array representation of a DOMNode
         * Note, make sure to use the LIBXML_NOBLANKS flag when loading XML into the DOMDocument
         * @param DOMDocument $dom
         * @param DOMNode $node
         * @return array
         */
        function nodeToArray( $dom, $node) {
            if(!is_a( $dom, 'DOMDocument' ) || !is_a( $node, 'DOMNode' )) {
                return false;
            }
            $array = false;
            if( empty( trim( $node->localName ))) {// Discard empty nodes
                return false;
            }
            if( XML_TEXT_NODE == $node->nodeType ) {
                return $node->nodeValue;
            }
            foreach ($node->attributes as $attr) {
                $array['@'.$attr->localName] = $attr->nodeValue;
            }
            foreach ($node->childNodes as $childNode) {
                if ( 1 == $childNode->childNodes->length && XML_TEXT_NODE == $childNode->firstChild->nodeType ) {
                    $array[$childNode->localName] = $childNode->nodeValue;
                }  else {
                    if( false !== ($a = self::nodeToArray( $dom, $childNode))) {
                        $array[$childNode->localName] =     $a;
                    }
                }
            }
            return $array;
        }
?>
pizarropablo at gmail dot com 16-Apr-2014 11:36
In response to: alastair dot dallas at gmail dot com about "#text" nodes.
"#text" nodes appear when there are spaces or new lines between end tag and next initial tag.

Eg "<data><age>10</age>[SPACES]<other>20</other>[SPACES]</data>"

"data" childNodes has 4 childs:
- age = 10
- #text = spaces
- other = 20
- #text =  spaces
matej dot golian at gmail dot com 29-Aug-2013 10:15
Here is a little function that truncates a DomNode to a specified number of text characters. I use it to generate HTML excerpts for my blog entries.

<?php

function makehtmlexcerpt(DomNode $html, $excerptlength)
{
$remove = 0;
$htmllength = strlen(html_entity_decode($html->textContent, ENT_QUOTES, 'UTF-8'));
$truncate = $htmllength - $excerptlength;
if($htmllength > $excerptlength)
{
if($html->hasChildNodes())
{
$children = $html->childNodes;
for($counter = 0; $counter < $children->length; $counter ++)
{
$child = $children->item($children->length - ($counter + 1));
$childlength = strlen(html_entity_decode($child->textContent, ENT_QUOTES, 'UTF-8'));
if($childlength <= $truncate)
{
$remove ++;
$truncate = $truncate - $childlength;
}
else
{
$child = makehtmlexcerpt($child, $childlength - $truncate);
break;
}
}
if($remove != 0)
{
for($counter = 0; $counter < $remove; $counter ++)
{
$html->removeChild($html->lastChild);
}
}
}
else
{
if($html->nodeName == '#text')
{
$html->nodeValue = substr(html_entity_decode($html->nodeValue, ENT_QUOTES, 'UTF-8'), 0, $htmllength - $truncate);
}
}
}
return $html;
}

?>
alastair dot dallas at gmail dot com 25-Sep-2011 05:44
The issues around mixed content took me some experimentation to remember, so I thought I'd add this note to save others time.

When your markup is something like: <div><p>First text.</p><ul><li><p>First bullet</p></li></ul></div>, you'll get XML_ELEMENT_NODEs that are quite regular. The <div> has children <p> and <ul> and the nodeValue for both <p>s yields the text you expect.

But when your markup is more like <p>This is <b>bold</b> and this is <i>italic</i>.</p>, you realize that the nodeValue for XML_ELEMENT_NODEs is not reliable. In this case, you need to look at the <p>'s child nodes. For this example, the <p> has children: #text, <b>, #text, <i>, #text.

In this example, the nodeValue of <b> and <i> is the same as their #text children. But you could have markup like: <p>This <b>is bold and <i>bold italic</i></b>, you see?</p>. In this case, you need to look at the children of <b>, which will be #text, <i>, because the nodeValue of <b> will not be sufficient.

XML_TEXT_NODEs have no children and are always named '#text'. Depending on how whitespace is handled, your tree may have "empty" #text nodes as children of <body> and elsewhere.

Attributes are nodes, but I had forgotten that they are not in the tree expressed by childNodes. Walking the full tree using childNodes will not visit any attribute nodes.
imranomar at gmail dot com 20-Mar-2011 03:10
Just discovered that node->nodeValue strips out all the tags
I. Cook 19-Apr-2010 11:43
For a reference with more information about the XML DOM node types, see http://www.w3schools.com/dom/dom_nodetype.asp

(When using PHP DOMNode, these constants need to be prefaced with "XML_")
R. Studer 13-Jan-2010 06:03
For clarification:
The assumingly 'discoverd' by previous posters and seemingly undocumented methods (.getElementsByTagName and .getAttribute) on this class (DOMNode) are in fact methods of the class DOMElement, which inherits from DOMNode.

See: http://www.php.net/manual/en/class.domelement.php
David Rekowski 08-Jan-2010 10:54
You cannot simply overwrite $textContent, to replace the text content of a DOMNode, as the missing readonly flag suggests. Instead you have to do something like this:

<?php

$node->removeChild($node->firstChild);
$node->appendChild(new DOMText('new text content'));

?>

This example shows what happens:

<?php

$doc = DOMDocument::loadXML('<node>old content</node>');
$node = $doc->getElementsByTagName('node')->item(0);
echo "Content 1: ".$node->textContent."\n";

$node->textContent = 'new content';
echo "Content 2: ".$node->textContent."\n";

$newText = new DOMText('new content');

$node->appendChild($newText);
echo "Content 3: ".$node->textContent."\n";

$node->removeChild($node->firstChild);
$node->appendChild($newText);
echo "Content 4: ".$node->textContent."\n";

?>

The output is:

Content 1: old content // starting content
Content 2: old content // trying to replace overwriting $node->textContent
Content 3: old contentnew content // simply appending the new text node
Content 4: new content // removing firstchild before appending the new text node

If you want to have a CDATA section, use this:

<?php
$doc = DOMDocument::loadXML('<node>old content</node>');
$node = $doc->getElementsByTagName('node')->item(0);
$node->removeChild($node->firstChild);
$newText = $doc->createCDATASection('new cdata content');
$node->appendChild($newText);
echo "Content withCDATA: ".$doc->saveXML($node)."\n";
?>
Steve K 03-Nov-2009 08:47
This class apparently also has a getElementsByTagName method.

I was able to confirm this by evaluating the output from DOMNodeList->item() against various tests with the is_a() function.
marc at ermshaus dot org 05-May-2009 05:36
It took me forever to find a mapping for the XML_*_NODE constants. So I thought, it'd be handy to paste it here:

 1 XML_ELEMENT_NODE
 2 XML_ATTRIBUTE_NODE
 3 XML_TEXT_NODE
 4 XML_CDATA_SECTION_NODE
 5 XML_ENTITY_REFERENCE_NODE
 6 XML_ENTITY_NODE
 7 XML_PROCESSING_INSTRUCTION_NODE
 8 XML_COMMENT_NODE
 9 XML_DOCUMENT_NODE
10 XML_DOCUMENT_TYPE_NODE
11 XML_DOCUMENT_FRAGMENT_NODE
12 XML_NOTATION_NODE
matt at lamplightdb dot co dot uk 06-Apr-2009 02:39
And apparently also a setAttribute method too:

$node->setAttribute( 'attrName' , 'value' );
brian wildwoodassociates.info 08-Dec-2008 08:27
This class has a getAttribute method.

Assume that a DOMNode object $ref contained an anchor taken out of a DOMNode List.  Then

    $url = $ref->getAttribute('href');

would isolate the url associated with the href part of the anchor.


Adds new child at the end of the children

  • ≪ DOMNode
  • DOMNode::C14N ≫
  • PHP Manual
  • DOMNode
  • Adds new child at the end of the children

DOMNode::appendChild

(PHP 5, PHP 7)

DOMNode::appendChild — Adds new child at the end of the children

설명

public DOMNode DOMNode::appendChild ( DOMNode $newnode )

This function appends a child to an existing list of children or creates a new list of children. The child can be created with e.g. DOMDocument::createElement(), DOMDocument::createTextNode() etc. or simply by using any other node.

인수

newnode

The appended child.

반환값

The node added.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if this node is readonly or if the previous parent of the node being inserted is readonly.

DOM_HIERARCHY_REQUEST_ERR

Raised if this node is of a type that does not allow children of the type of the newnode node, or if the node to append is one of this node's ancestors or this node itself.

DOM_WRONG_DOCUMENT_ERR

Raised if newnode was created from a different document than the one that created this node.

예제

The following example will add a new element node to a fresh document.

Example #1 Adding a child

<?php

$doc = new DOMDocument;

$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);

echo $doc->saveXML();
?>

참고

  • DOMNode::insertBefore() - Adds a new child before a reference node
  • DOMNode::removeChild() - Removes child from list of children
  • DOMNode::replaceChild() - Replaces a child

User Contributed Notes

frame at dynamiccreated dot de 01-Dec-2010 01:14
Aware dealing with DOMNodeList and appendChild() on the same Node.

If you want to replace only the children not the DOMElement itself you probably foreach childNodes-property or get the DOMElements with a for-loop and item()-method of the DOMNodeList.

You will fail if you not clone the received single DOMElement. Actually the count of the DOMNodelist will be decreased on appendChild count but appendChild seems to refer to the old Nodelist and nothing visible will happen. Cloning will help.
krisdover at hotmail dot com 01-Jun-2008 05:49
What's not mentioned here is that DOMNode::appendChild() can also be used to move an existing node to another part of the DOMDocument, e.g.

<?php
$doc = new DOMDocument();
$doc->loadXML("<foobar><bar/><foo/></foobar>");
$bar = $doc->documentElement->firstChild;
$foo = $doc->documentElement->lastChild;
$foo->appendChild($bar);
print $doc->saveXML();
?>

This produces:

<?xml version="1.0"?>
<foobar><foo><bar/></foo></foobar>

Note that the nodes "<foo/>" and "<bar/>" were siblings, i.e. the first and last child of "<foobar>" but using appendChild() we were able to move "<bar/>" so that it is a child of "<foo/>".

This saves you the trouble of doing a DOMNode::removeChild($bar) to remove "<bar/>" before appending it as a child of "<foo/>".

Kris Dover
jrtayloriv at gmail dot com 30-Mar-2008 01:56
If you want to create nested DOM elements:

<?php
    $doc = new DOMDocument();

    $foo = $doc->createElement("foo");
    $doc->appendChild($foo);

    $bar = $doc->createElement("bar");
    $foo->appendChild($bar);

    $bazz = $doc->createElement("bazz");
    $foo->appendChild($bazz);

    echo $doc->saveXML();
?>

Is equivalent to:

<foo>
  <bar></bar>
  <bazz></bazz>
</foo>


Canonicalize nodes to a string

  • ≪ DOMNode::appendChild
  • DOMNode::C14NFile ≫
  • PHP Manual
  • DOMNode
  • Canonicalize nodes to a string

DOMNode::C14N

(PHP 5 >= 5.2.0, PHP 7)

DOMNode::C14N — Canonicalize nodes to a string

설명

public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

Canonicalize nodes to a string

인수

exclusive

Enable exclusive parsing of only the nodes matched by the provided xpath or namespace prefixes.

with_comments

Retain comments in output.

xpath

An array of xpaths to filter the nodes by.

ns_prefixes

An array of namespace prefixes to filter the nodes by.

반환값

Returns canonicalized nodes as a string실패 시 FALSE를 반환합니다

참고

  • DOMNode::C14NFile() - Canonicalize nodes to a file

User Contributed Notes

lordbaco 28-Jan-2016 06:57
Good to know:

<< Due to a known issue in XML canonicalization in PHP, processing large metadata files in SimpleSAMLphp takes a big amount of resources, with that amount growing approximately by the square of the number of entities in the metadata set >>
https://simplesamlphp.org/metaprocessing

<< The C14N() function appears to have a runtime that is O(N^2) (or possibly worse?) depending on input size, which means that it becomes very slow as the input grows. For example, an input with around 196000 nodes takes about 290 seconds, while an input with 486000 nodes takes 2200 seconds. >>
https://bugs.php.net/bug.php?id=53655

We had the same issue with a 4.1 MB XML (105k lines). The sample code of ticket  #53655 takes 1h36 minute to canonicalize it!
jorda at edpsciences dot org 13-Mar-2015 06:11
C14N() returns an empty string if the node is not included in the document tree:
<?php
$d = new DOMDocument('1.0');
$d->loadXML('<foo></foo>');
$n = $d->createElement('bar');
var_dump($n->C14N());
$d->documentElement->appendChild($n);
var_dump($n->C14N());
?>
output:
string(0) ""
string(11) "<bar></bar>"
Rijk 27-Mar-2013 06:26
When working with (malformed) HTML, you're probably better off using DOMDocument's saveHTML() method instead. C14N() will attempt to make your HTML valid XML, for example by converting <br> to <br></br>.

So instead of:
$html = $Node->C14N();

Use:
$html = $Node->ownerDocument->saveHTML( $Node );


Canonicalize nodes to a file

  • ≪ DOMNode::C14N
  • DOMNode::cloneNode ≫
  • PHP Manual
  • DOMNode
  • Canonicalize nodes to a file

DOMNode::C14NFile

(PHP 5 >= 5.2.0, PHP 7)

DOMNode::C14NFile — Canonicalize nodes to a file

설명

public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )

Canonicalize nodes to a file.

인수

uri

Path to write the output to.

exclusive

Enable exclusive parsing of only the nodes matched by the provided xpath or namespace prefixes.

with_comments

Retain comments in output.

xpath

An array of xpaths to filter the nodes by.

ns_prefixes

An array of namespace prefixes to filter the nodes by.

반환값

Number of bytes written실패 시 FALSE를 반환합니다

참고

  • DOMNode::C14N() - Canonicalize nodes to a string

User Contributed Notes

There are no user contributed notes for this page.


Clones a node

  • ≪ DOMNode::C14NFile
  • DOMNode::getLineNo ≫
  • PHP Manual
  • DOMNode
  • Clones a node

DOMNode::cloneNode

(PHP 5, PHP 7)

DOMNode::cloneNode — Clones a node

설명

public DOMNode DOMNode::cloneNode ([ bool $deep ] )

Creates a copy of the node.

인수

deep

Indicates whether to copy all descendant nodes. This parameter is defaulted to FALSE.

반환값

The cloned node.

User Contributed Notes

h-fate at gmx dot net 02-Oct-2014 04:48
If you have an object that holds a DOMNode, cloning the object won't clone the DOMNode with it. If you simply copy the object or add its DOMNode several times, you will in fact only move the DOMNode in the tree, not multiply it. That might seem obvious, but took me half a day to find out.

The object needs to use __clone and clone the node manually:

<?php
class containsNode {
    public $node; //set from somewhere

    public function __clone() {
        $this->node = $this->node->cloneNode(TRUE);
    }
}
?>
frame at dynamiccreated dot de 02-Dec-2012 11:19
Remeber that DOMNode always needs a reference to a parent node or DOMDocument.

For example, if you try to clone a node - copy all children - and overwrite or delete the variable which holds the cloned node - all children will loose any reference and getting invalid.

This will cause a nice message like "Couldn't... node no longer exists" if you have luck. In most cases PHP only gives you the poor information "Couldn't fetch DOM[...]" which makes hard to find out whats going on, depending on the current operation.
cemkalyoncu at gmail dot com 28-Apr-2009 12:04
If you need some function to clone a node without touching namespaces you can use the following.

<?php
private function cloneNode($node,$doc){
    $nd=$doc->createElement($node->nodeName);

    foreach($node->attributes as $value)
        $nd->setAttribute($value->nodeName,$value->value);

    if(!$node->childNodes)
        return $nd;

    foreach($node->childNodes as $child) {
        if($child->nodeName=="#text")
            $nd->appendChild($doc->createTextNode($child->nodeValue));
        else
            $nd->appendChild(cloneNode($child,$doc));
    }

    return $nd;
}
?>
[montana] at [percepticon] dot [com] 16-Feb-2009 11:23
<?php

//@oliver thanks for example source...

/*
 cloneNode(false) does not omit
 Attributes of cloned node,
 to achieve this an iteration is required.
 this is probably less efficient
 than merely creating a new
 node from the desired nodeName
 but in some cases could be useful.

use case:

omit subnodes and attributes of
secured portions of an xml document
without altering expected general structure;
*/
//xml to use

$file="<?xml version='1.0'?>
<book type='paperback'>
    <title name='MAP'>Red Nails</title>
    <price>$12.99</price>
    <author>
        <name first='Robert' middle='E' last='Howard'/>
        <birthdate disco='false' nirvana='definitely'>
            9/21/1977
            <month title='september' />
        </birthdate>
    </author>
    <author>
        <name first='Arthur' middle='Mc' last='Kayn'/>
    </author>
</book>";

$doc = new domDocument;

$doc->loadXML($file);

$xpath = new domXPath($doc);

$query = "//author/birthdate";
$xpathQuery = $xpath->query($query);

//would be a loop in production code...
$child = $xpathQuery->item(0);

$parent = $child->parentNode;

$doppel = $child->cloneNode(false);

$limit = $doppel->attributes->length;

for ($a=0;$a<$limit;$a++) {
    $doppel->removeAttributeNode($doppel->attributes->item(0));
}
//swap for now empty node
$parent->replaceChild( $doppel, $child);

print $doc->saveXML();

?>
oliver dot christen at camptocamp dot com 19-Nov-2004 05:48
simple exemple of node cloning

<?xml version="1.0"?>

<book type="paperback">
    <title name='MAP'>Red Nails</title>
    <price>$12.99</price>
    <author>
        <name first="Robert" middle="E" last="Howard"/>
        <birthdate>9/21/1977</birthdate>
    </author>
    <author>
        <name first="Arthur" middle="Mc" last="Kayn"/>
    </author>
</book>

<?php

//filename xml file to use
$file = 'book.xml';

$doc = new domDocument;

if (file_exists($file)) {
    $doc->load($file);
} else {
    exit('Erreur !.');
}

$xpath = new domXPath($doc);

$query = "//author/*";
$xpathQuery = $xpath->query($query);

$size = $xpathQuery->length;
for ($i=0; $i<$size; $i++){
    $node = $xpathQuery->item($i);
    if ($node->nodeName == 'birthdate' && $node->hasChildNodes() && $node->firstChild->textContent != ''){
        $clonenode = $node->cloneNode(true);
        $refnode = $node;
    }
}
for ($i=0; $i<$size; $i++){
    $node = $xpathQuery->item($i);
    if (!$node->isSameNode($refnode)){
        $newnode = $node->appendChild($clonenode);
    }
}

print $doc->saveXML();

?>


Get line number for a node

  • ≪ DOMNode::cloneNode
  • DOMNode::getNodePath ≫
  • PHP Manual
  • DOMNode
  • Get line number for a node

DOMNode::getLineNo

(PHP 5 >= 5.3.0, PHP 7)

DOMNode::getLineNo — Get line number for a node

설명

public int DOMNode::getLineNo ( void )

Gets line number for where the node is defined.

인수

이 함수는 인수가 없습니다.

반환값

Always returns the line number where the node was defined in.

예제

Example #1 DOMNode::getLineNo() example

<?php
// XML dump for below example
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<root>
    <node />
</root>
XML;

// Create a new DOMDocument instance
$dom = new DOMDocument;

// Load the XML
$dom->loadXML($xml);

// Print where the line where the 'node' element was defined in
printf('The <node> tag is defined on line %d', $dom->getElementsByTagName('node')->item(0)->getLineNo());
?>

위 예제의 출력:

The <node> tag is defined in line 3

User Contributed Notes

ruud at vanmelick dot com 26-Oct-2013 06:15
The 65535 line number limit is no longer a problem when you use libxml 2.9 or higher, but you have to explicitly enable support for big line numbers:

<?php
define('XML_PARSE_BIG_LINES', 4194304);
$dom = new DOMDocument;
$dom->loadXML($xml, XML_PARSE_BIG_LINES);
?>
Anonymous 19-May-2012 11:21
The DOMNode::getLineNo() method doesn't work properly due to a libxml2 bug.

https://bugzilla.gnome.org/show_bug.cgi?id=676026
luke dot NOREPLY at webconnex dot com 05-Mar-2011 03:28
This function is buggy. It doesn't always return the correct line number, especially for text elements. As an alternative you can do something like this:

<?php
$text = $node->ownerDocument->saveXML($node);
$line += substr_count($text, "\n");
?>

You'll want to keep a reference to $line (starting at 0) and add to it as you parse over the document recursively.

In order for this to work you have to tell DOMDocument to preserve white space before loading the document.


Get an XPath for a node

  • ≪ DOMNode::getLineNo
  • DOMNode::hasAttributes ≫
  • PHP Manual
  • DOMNode
  • Get an XPath for a node

DOMNode::getNodePath

(PHP 5 >= 5.2.0, PHP 7)

DOMNode::getNodePath — Get an XPath for a node

설명

public string DOMNode::getNodePath ( void )

Gets an XPath location path for the node.

인수

이 함수는 인수가 없습니다.

반환값

Returns a string containing the XPath, or NULL in case of an error.

예제

Example #1 DOMNode::getNodePath() example

<?php
// Create a new DOMDocument instance
$dom = new DOMDocument;

// Load the XML
$dom->loadXML('
<fruits>
 <apples>
  <apple>braeburn</apple>
  <apple>granny smith</apple>
 </apples>
 <pears>
  <pear>conference</pear>
 </pears>
</fruits>
');

// Print XPath for each element
foreach ($dom->getElementsByTagName('*') as $node) {
    echo $node->getNodePath() . "\n";
}
?>

위 예제의 출력:

/fruits
/fruits/apples
/fruits/apples/apple[1]
/fruits/apples/apple[2]
/fruits/pears
/fruits/pears/pear

참고

  • DOMXPath

User Contributed Notes

There are no user contributed notes for this page.


Checks if node has attributes

  • ≪ DOMNode::getNodePath
  • DOMNode::hasChildNodes ≫
  • PHP Manual
  • DOMNode
  • Checks if node has attributes

DOMNode::hasAttributes

(PHP 5, PHP 7)

DOMNode::hasAttributes — Checks if node has attributes

설명

public bool DOMNode::hasAttributes ( void )

This method checks if the node has attributes. The tested node has to be an XML_ELEMENT_NODE.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • DOMNode::hasChildNodes() - Checks if node has children

User Contributed Notes

There are no user contributed notes for this page.


Checks if node has children

  • ≪ DOMNode::hasAttributes
  • DOMNode::insertBefore ≫
  • PHP Manual
  • DOMNode
  • Checks if node has children

DOMNode::hasChildNodes

(PHP 5, PHP 7)

DOMNode::hasChildNodes — Checks if node has children

설명

public bool DOMNode::hasChildNodes ( void )

This function checks if the node has children.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • DOMNode::hasAttributes() - Checks if node has attributes

User Contributed Notes

Anonymous 22-Mar-2015 01:33
Hi what if its a dynamic file and we cannot use get elements by tag name then how do we print the contents of all level tags?
richard dot gildx at gmail dot com 29-Jul-2012 10:20
This "hasChildNodes()" exercise is simple enough to make it clear and understandable. Or, you could take it as a tag empty check. By Richard Holm, Sweden.

<?php
$xmldoc=
'<?xml version="1.0" ?>
<root>
<text>Text</text>
<none/>
<empty></empty>
<space> </space>
</root>';

$domdoc=new DOMDocument();
$domdoc->loadXML($xmldoc);

$tag=$domdoc->getElementsByTagName('root')->item(0);
$v=$tag->hasChildNodes()?" hasChildNodes":" hasNoChildNodes";
echo $tag->tagName.$v."<br/>";

$tag=$domdoc->getElementsByTagName('text')->item(0);
$v=$tag->hasChildNodes()?" hasChildNodes":" hasNoChildNodes";
echo $tag->tagName.$v."<br/>";

$tag=$domdoc->getElementsByTagName('none')->item(0);
$v=$tag->hasChildNodes()?" hasChildNodes":" hasNoChildNodes";
echo $tag->tagName.$v."<br/>";

$tag=$domdoc->getElementsByTagName('empty')->item(0);
$v=$tag->hasChildNodes()?" hasChildNodes":" hasNoChildNodes";
echo $tag->tagName.$v."<br/>";

$tag=$domdoc->getElementsByTagName('space')->item(0);
$v=$tag->hasChildNodes()?" hasChildNodes":" hasNoChildNodes";
echo $tag->tagName.$v."<br/>";
?>

Output:
root hasChildNodes
text hasChildNodes
none hasNoChildNodes
empty hasNoChildNodes
space hasChildNodes
sansana 15-Oct-2010 04:04
Personally I think using a simple:[code]if($DOMNode->childNodes <>0){}[/code] works better.
syngcw at syncgw.com 04-Jan-2010 02:53
This function is a bit tricky. If you want to find XML childnodes it is useless. You need to create a work-around:

<?php

$x = new DOMDocument();
$x->loadXML('
<A>
 <B>b-text</B>
 <C>
  <D>d-text</D>
 </C>
 <E/>
</A>');

shownode($x->getElementsByTagName('A')->item(0));
function shownode($x) {
 foreach ($x->childNodes as $p)
  if (hasChild($p)) {
      echo $p->nodeName.' -> CHILDNODES<br>';
      shownode($p);
  } elseif ($p->nodeType == XML_ELEMENT_NODE)
   echo $p->nodeName.' '.$p->nodeValue.'<br>';
}
function hasChild($p) {
 if ($p->hasChildNodes()) {
  foreach ($p->childNodes as $c) {
   if ($c->nodeType == XML_ELEMENT_NODE)
    return true;
  }
 }
 return false;
}

?>

shows:
B b-text
C -> CHILDNODES
D d-text
E
jintoppy at gmail dot com 28-Apr-2009 01:13
<?php

$doc = new DOMDocument;
$node = $doc->createElement("FirstMain", "First Main Node. This have child");
$doc->appendChild($node);
$childnode = $doc->createElement("childnode", "child node");
$node->appendChild($childnode);
$secondnode = $doc->createElement("SecondMain", "First Main Node. This don't have child");
$doc->appendChild($secondnode);
$doc->saveXML();
$nodeElmt = $doc->getElementsByTagName("FirstMain");
/*
if given as $nodeElmt = $doc->getElementsByTagName("childnode");
the output would be "This node has childnodes"

if given as $nodeElmt = $doc->getElementsByTagName("SecondMain");
the output would be "This node has no childnodes"

*/

foreach($nodeElmt as $nodeElmt)
{
if($nodeElmt->hasChildNodes())
{
echo "This node has childnodes";
}
else
{
echo "This node has no childnodes";
}
}

  ?>

- - - - - - - - - - - - - -

Output:

This node has childnodes

- - - - - - - - - - - - - -


Adds a new child before a reference node

  • ≪ DOMNode::hasChildNodes
  • DOMNode::isDefaultNamespace ≫
  • PHP Manual
  • DOMNode
  • Adds a new child before a reference node

DOMNode::insertBefore

(PHP 5, PHP 7)

DOMNode::insertBefore — Adds a new child before a reference node

설명

public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )

This function inserts a new node right before the reference node. If you plan to do further modifications on the appended child you must use the returned node.

인수

newnode

The new node.

refnode

The reference node. If not supplied, newnode is appended to the children.

반환값

The inserted node.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if this node is readonly or if the previous parent of the node being inserted is readonly.

DOM_HIERARCHY_REQUEST_ERR

Raised if this node is of a type that does not allow children of the type of the newnode node, or if the node to append is one of this node's ancestors or this node itself.

DOM_WRONG_DOCUMENT_ERR

Raised if newnode was created from a different document than the one that created this node.

DOM_NOT_FOUND

Raised if refnode is not a child of this node.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children

User Contributed Notes

jarry1250 at gmail dot com 30-May-2015 10:59
Note that supplying the same node for $newnode and $refnode leads to an E_WARNING ("Couldn't add newnode as the previous sibling of refnode"). For example imagine one wanted to make $newnode the first child of its parent by doing:

<?php
$firstSibling = $newnode->parentNode->firstChild;
// Bad:
$newnode->parentNode->insertBefore( $newnode, $firstSibling );
?>

This would generate a warning if it already was the first child of its parent, since $newnode and $firstSibling are identical. Easy to work around though:

<?php
$firstSibling = $newnode->parentNode->firstChild;
// Better:
if( $newnode !== $firstSibling ) {
    $newnode->parentNode->insertBefore( $newnode, $firstSibling );
}
?>
DrTebi at Yahoo 03-Oct-2007 02:54
Sorry, my previous posting worked only for the top node. Here the corrected version, which will work for any node:

XML
----
<?xml version="1.0"?>
<contacts>
  <person>Adam</person>
  <person>Eva</person>
  <person>Thomas</person>
</contacts>

PHP
---
<?php
// load XML, create XPath object
$xml = new DomDocument();
$xml->preserveWhitespace = false;
$xml->load('contacts.xml');
$xpath = new DOMXPath($xml);

// get node eva, which we will append to
$eva = $xpath->query('/contacts/person[.="Eva"]')->item(0);

// create node john
$john = $xml->createElement('person', 'John');

// insert john after eva
//   "in eva's parent node (=contacts) insert
//   john before eva's next node"
// this also works if eva would be the last node
$eva->parentNode->insertBefore($john, $eva->nextSibling);

// show result
header('Content-Type: text/plain');
print $xml->saveXML();
?>

Result
------
<?xml version="1.0"?>
<contacts>
  <person>Adam</person>
  <person>Eva</person><person>John</person>
  <person>Thomas</person>
</contacts>
justin at redwiredesign dot com 07-Feb-2007 05:20
The previous example is incorrect, and causes a DOM_NOT_FOUND error, as the child nodes are not direct descendants of the root node.

Therefore, the line:

$parent_path = "/root";

needs to change to:

$parent_path = "/root/parent";

or

$parent_path = "//parent";

for this example to work
jg at handcode dot de 18-Aug-2005 09:18
example to insert <newnode/> between <chid1/> and <child2/>

<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
  <parent>
    <child nr="1"/>
    <child nr="2"/>
  </parent>
</root>

<?php

$xml_src = 'test.xml';

// XPath-Querys
$parent_path = "//parent";
$next_path = "//parent/child[@nr='2']";

// Create a new DOM document
$dom = new DomDocument();
$dom->load($xml_src);

// Find the parent node
$xpath = new DomXPath($dom);

// Find parent node
$parent = $xpath->query($parent_path);

// new node will be inserted before this node
$next = $xpath->query($next_path);

// Create the new element
$element = $dom->createElement('newnode');

// Insert the new element
$parent->item(0)->insertBefore($element, $next->item(0));

echo $dom->saveXML();

?>
Jerry Ellis 03-Jun-2005 09:45
1st argument) a node to insert
2nd argument) a reference node - this is the node that the new node will be inserted before

The trick to using this method is that the OBJECT on which you actually CALL the insertBefore() method is actually the PARENT node of the reference node!

INCORRECT:
$DOMNode_refNode->insertBefore($DOMNode_newNode, $DOMNode_refNode);

CORRECT:
$DOMNode_refNode->parentNode->insertBefore($DOMNode_newNode, $DOMNode_refNode);


Checks if the specified namespaceURI is the default namespace or not

  • ≪ DOMNode::insertBefore
  • DOMNode::isSameNode ≫
  • PHP Manual
  • DOMNode
  • Checks if the specified namespaceURI is the default namespace or not

DOMNode::isDefaultNamespace

(PHP 5, PHP 7)

DOMNode::isDefaultNamespace — Checks if the specified namespaceURI is the default namespace or not

설명

public bool DOMNode::isDefaultNamespace ( string $namespaceURI )

Tells whether namespaceURI is the default namespace.

인수

namespaceURI

The namespace URI to look for.

반환값

Return TRUE if namespaceURI is the default namespace, FALSE otherwise.

User Contributed Notes

There are no user contributed notes for this page.


Indicates if two nodes are the same node

  • ≪ DOMNode::isDefaultNamespace
  • DOMNode::isSupported ≫
  • PHP Manual
  • DOMNode
  • Indicates if two nodes are the same node

DOMNode::isSameNode

(PHP 5, PHP 7)

DOMNode::isSameNode — Indicates if two nodes are the same node

설명

public bool DOMNode::isSameNode ( DOMNode $node )

This function indicates if two nodes are the same node. The comparison is not based on content

인수

node

The compared node.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

There are no user contributed notes for this page.


Checks if feature is supported for specified version

  • ≪ DOMNode::isSameNode
  • DOMNode::lookupNamespaceURI ≫
  • PHP Manual
  • DOMNode
  • Checks if feature is supported for specified version

DOMNode::isSupported

(PHP 5, PHP 7)

DOMNode::isSupported — Checks if feature is supported for specified version

설명

public bool DOMNode::isSupported ( string $feature , string $version )

Checks if the asked feature is supported for the specified version.

인수

feature

The feature to test. See the example of DOMImplementation::hasFeature() for a list of features.

version

The version number of the feature to test.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • DOMImplementation::hasFeature() - Test if the DOM implementation implements a specific feature

User Contributed Notes

There are no user contributed notes for this page.


Gets the namespace URI of the node based on the prefix

  • ≪ DOMNode::isSupported
  • DOMNode::lookupPrefix ≫
  • PHP Manual
  • DOMNode
  • Gets the namespace URI of the node based on the prefix

DOMNode::lookupNamespaceURI

(PHP 5, PHP 7)

DOMNode::lookupNamespaceURI — Gets the namespace URI of the node based on the prefix

설명

public string DOMNode::lookupNamespaceURI ( string $prefix )

Gets the namespace URI of the node based on the prefix.

인수

prefix

The prefix of the namespace.

반환값

The namespace URI of the node.

참고

  • DOMNode::lookupPrefix() - Gets the namespace prefix of the node based on the namespace URI

User Contributed Notes

_ michael 30-Apr-2010 11:22
You have to pass *null* to lookupNamespaceURI if you want to read the default namespace. Passing an empty string doesn't work - even though the documentation says that lookupNamespaceURI takes a string as argument (and null would just be converted to an empty string).
lewar 05-Jun-2007 02:20
Simple way to get the URI of the default namespace:

<?php
    $document = new DOMDocument();
    $document->load($xml_file);
    $uri = $document->documentElement->lookupnamespaceURI(NULL);
?>


Gets the namespace prefix of the node based on the namespace URI

  • ≪ DOMNode::lookupNamespaceURI
  • DOMNode::normalize ≫
  • PHP Manual
  • DOMNode
  • Gets the namespace prefix of the node based on the namespace URI

DOMNode::lookupPrefix

(PHP 5, PHP 7)

DOMNode::lookupPrefix — Gets the namespace prefix of the node based on the namespace URI

설명

public string DOMNode::lookupPrefix ( string $namespaceURI )

Gets the namespace prefix of the node based on the namespace URI.

인수

namespaceURI

The namespace URI.

반환값

The prefix of the namespace.

참고

  • DOMNode::lookupNamespaceURI() - Gets the namespace URI of the node based on the prefix

User Contributed Notes

There are no user contributed notes for this page.


Normalizes the node

  • ≪ DOMNode::lookupPrefix
  • DOMNode::removeChild ≫
  • PHP Manual
  • DOMNode
  • Normalizes the node

DOMNode::normalize

(PHP 5, PHP 7)

DOMNode::normalize — Normalizes the node

설명

public void DOMNode::normalize ( void )

Remove empty text nodes and merge adjacent text nodes in this node and all its children.

반환값

값을 반환하지 않습니다.

참고

  • » The DOM Specification
  • DOMDocument::normalizeDocument() - Normalizes the document

User Contributed Notes

Amir Laher 22-Aug-2005 01:45
This is a quote from the DOM level 2 spec, explaining the normalize() function.
----
normalize
Puts all Text nodes in the full depth of the sub-tree underneath this Node, including attribute nodes, into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes. This can be used to ensure that the DOM view of a document is the same as if it were saved and re-loaded, and is useful when operations (such as XPointer [XPointer] lookups) that depend on a particular document tree structure are to be used.
----
(source: http://www.w3.org/TR/DOM-Level-2-Core/core.html)

i.e. the idea of normalize is to make text nodes the same as they would be if the xml document was re-loaded into DOM. It's handy if you want to compare DOM nodes.


Removes child from list of children

  • ≪ DOMNode::normalize
  • DOMNode::replaceChild ≫
  • PHP Manual
  • DOMNode
  • Removes child from list of children

DOMNode::removeChild

(PHP 5, PHP 7)

DOMNode::removeChild — Removes child from list of children

설명

public DOMNode DOMNode::removeChild ( DOMNode $oldnode )

This functions removes a child from a list of children.

인수

oldnode

The removed child.

반환값

If the child could be removed the function returns the old child.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if this node is readonly.

DOM_NOT_FOUND

Raised if oldnode is not a child of this node.

예제

The following example will delete the chapter element of our XML document.

Example #1 Removing a child

<?php

$doc = new DOMDocument;
$doc->load('book.xml');

$book = $doc->documentElement;

// we retrieve the chapter and remove it from the book
$chapter = $book->getElementsByTagName('chapter')->item(0);
$oldchapter = $book->removeChild($chapter);

echo $doc->saveXML();
?>

위 예제의 출력:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
          "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
<book id="listing">
 <title>My lists</title>

</book>

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMNode::replaceChild() - Replaces a child

User Contributed Notes

Kenney 28-Mar-2013 07:19
This method will remove all child nodes but leave the attributes (and namespace declarations) intact.

<?php
function removeChildren( &$node )
{
  $node->parentNode->replaceChild(
    $n = $node->cloneNode( false ),
    $node );

  $node = $n;
}
?>
drimsun at gmail dot com 12-Feb-2010 12:37
If you try to remove children of a nested node (e.g. a <ul> list that contains another <ul>) and only remove the first level of child nodes, you'll get a messed-up node object.

Upon re-use of the messed-up node you'll get an error message that says: "Fatal error: Call to a member function blahblah() on a non-object".

To remove ALL child nodes, use this recursive function instead:
<?php
/**
 * Helper function that removes all child nodes of $node recursively
 * (i.e. including child nodes of the child nodes and so forth).
 */
function remove_children(&$node) {
  while ($node->firstChild) {
    while ($node->firstChild->firstChild) {
      remove_children($node->firstChild);
    }
    $node->removeChild($node->firstChild);
  }
}
?>
Justin Carlson 05-Nov-2009 08:46
Just a note, if you remove a node the whitespace it occupied will remain if you save the file.

To avoid this, use $document->preserveWhiteSpace = false;

For Example:

<?php
$document = new DomDocument();
$document->preserveWhiteSpace = false;
?>
crazy888s at hotmail dot com 15-Apr-2009 10:50
You can't remove DOMNodes from a DOMNodeList as you're iterating over them in a foreach loop. For example...

<?php
$domNodeList = $domDocument->getElementsByTagname('p');
foreach ( $domNodeList as $domElement ) {
  //  ...do stuff with $domElement...
  $domElement->parentNode->removeChild($domElement);
}
?>

... will seemingly leave the internal iterator on the foreach out of wack and results will be quite strange. Though, making a queue of items to remove seems to work. For example...

<?php
$domNodeList = $domDocument->getElementsByTagname('p');
$domElemsToRemove = array();
foreach ( $domNodeList as $domElement ) {
  // ...do stuff with $domElement...
  $domElemsToRemove[] = $domElement;
}
foreach( $domElemsToRemove as $domElement ){
  $domElement->parentNode->removeChild($domElement);
}
?>
Justin Sheckler 30-Jan-2009 05:44
These two functions might be helpful to anyone trying to delete a node and all of its children:

<?php
function deleteNode($node) {
    deleteChildren($node);
    $parent = $node->parentNode;
    $oldnode = $parent->removeChild($node);
}

function deleteChildren($node) {
    while (isset($node->firstChild)) {
        deleteChildren($node->firstChild);
        $node->removeChild($node->firstChild);
    }
}
?>
tim cameron ryan 19-Jul-2008 10:00
Or something like:

<?php

while ($node->childNodes->length)
     $node->removeChild($node->firstChild);

?>
Michael D (DigitalGemstones.com) 06-Jul-2008 01:15
Note that iterating through the childNodes array and removing those children will stop the iteration.

For example:
<?php
foreach($node->childNodes as $child)
{
  if(iWantToDeleteThisNode($child))
     $child->parentNode->removeChild($child);
}
?>

Will not work.  Note that in removing the node, the childNodes array gets rebuilt (it seems) and so only the first item will be removed.  In order to properly remove all the children you want to remove, you will need to do something like the following:

<?php
$nodesToDelete = array();
foreach($node->childNodes as $child)
{
  if(iWantToDeleteThisNode($child))
    $nodesToDelete[] = $child;
}
foreach($nodesToDelete as $node)
{
  $node->parentNode->removeChild($node);
}
?>

I believe this is actually more efficient than the first snippet would be (if it worked) but obviously I cannot benchmark it.
Wala 04-Apr-2008 07:57
I used the following to delete an entire node using the child element:

<?php
$childElement->parentNode->parentNode-> removeChild($childElement->parentNode);
?>

I could not have thought of it without the help of everyone who contributed to this thread.

Thanks!

(I had to introduce the extra space between parentNode->removeChild to get rid of the wordwrap() warning while posting this note)
Vasil Rangelov 06-Oct-2007 07:14
At the time of writing, I suppose rightfully, removeChild() removes only the selected node, but when you remove an element, it's child elements are not removed. If you want to achieve that, replaceChild() is the solution.

The following should remove all descendants of the $node DOMNode, regardless of it's name:
<?php
$node->replaceChild(new DOMElement($node->nodeName), $node);
?>
If you're replacing the root element, you must explicitly state that with $node->documentElement as the second argument.
mytto at openxtrem dot com 07-Sep-2005 11:20
Back again on removing childs and iterators robustness.

Things get a bit more complicated when you only want to remove 'some' nodes according to a certain condition. Then you can't just remove the first one repeatedly.

The trick is to copy the content of the node list into a more robust collection than DOMNodeList, I name array!

The following piece of code will, for instance, remove all empty child nodes:

<?php
// Copy childNodes array
$childNodes = array();
foreach($node->childNodes as $childNode) {
  $childNodes[] = $childNode;
}

// Browse with the copy
foreach ($childNodes as $childNode) {
  if (!$childNode->hasChildNodes()); {
    $childNode->parentNode->removeChild($childNode);
  }
}
?>
blaine (at) blainegarrett (dot com) 23-Aug-2005 07:10
This might be obvious, but might help someone as well...

If you simply have a node that you need to remove (perhaps from an xpath) and don't know the parent node offhand (the examples seem to assume you have the parent in a variable), then you can do something easy like this...

$node->parentNode->removeChild($node);

parentNode is a scalar property of the Element.

Hope that helps.

--
Blaine Garrett
pedro at leb dot usp dot br 30-May-2005 01:24
You may also use the hasChild function:
<?php

while($node->hasChildNodes()) {
  $node->removeChild($node->childNodes->item(0));
}

?>

When you remove a childNode, the next node becomes the first one!
vsematika at centrum dot cz 13-Feb-2005 12:35
For those who don't understand >sbeam at onsetcorps dot net on 02-Feb-2005 12:07< 'hack', here's a little discussion:

First but *wrong* try would be:
<?php
foreach ($parent->childNodes as $child) {
   $parent->removeChild($child);
?>
This doesn't work because DOM tree id modified on-the-fly and this confuses foreach loop.

The idea behind sbeam's trick is that after removing the first item in the first iteration, the second item in childNodes nodelist immediately becomes the first item. That's why we must _always_ remove the first child. Here's another implementation:

<?php
$count = $parent->childNodes->length;
for ($i = 0; $i < $count; $i++) {
   $oldNode = $parent->removeChild($parent->childNodes->item(0)); // !!! not item($i) !!!
}
?>


Replaces a child

  • ≪ DOMNode::removeChild
  • DOMNodeList ≫
  • PHP Manual
  • DOMNode
  • Replaces a child

DOMNode::replaceChild

(PHP 5, PHP 7)

DOMNode::replaceChild — Replaces a child

설명

public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )

This function replaces the child oldnode with the passed new node. If the newnode is already a child it will not be added a second time. If the replacement succeeds the old node is returned.

인수

newnode

The new node. It must be a member of the target document, i.e. created by one of the DOMDocument->createXXX() methods or imported in the document by DOMDocument::importNode.

oldnode

The old node.

반환값

The old node or FALSE if an error occur.

오류/예외

DOM_NO_MODIFICATION_ALLOWED_ERR

Raised if this node is readonly or if the previous parent of the node being inserted is readonly.

DOM_HIERARCHY_REQUEST_ERR

Raised if this node is of a type that does not allow children of the type of the newnode node, or if the node to put in is one of this node's ancestors or this node itself.

DOM_WRONG_DOCUMENT_ERR

Raised if newnode was created from a different document than the one that created this node.

DOM_NOT_FOUND

Raised if oldnode is not a child of this node.

참고

  • DOMNode::appendChild() - Adds new child at the end of the children
  • DOMNode::removeChild() - Removes child from list of children

User Contributed Notes

jb at jbpros dot com 02-Mar-2005 05:08
If you are trying to replace more than one node at once, you have to be careful about iterating over the DOMNodeList.  If the old node has a different name from the new node, it will be removed from the list once it has been replaced.  Use a regressive loop:

<?php

$xml = new DOMDocument;
$xml->load('docfile.xml');

$elements = $xml->getElementsByTagNameNS('http://www.example.com/NS/', '*');
$i = $elements->length - 1;
while ($i > -1) {
    $element = $elements->item($i);
    $ignore = false;

    $newelement = $xml>createTextNode('Some new node!');
    $element->parentNode->replaceChild($newelement, $element);
    $i--;
}

?>

The loop counter ($i) will always be in the list's interval as removed elements indexes are above the counter.
aidan at php dot net 26-Dec-2004 11:17
Here is a simple example for replacing a node:

Let's define our XML like so:

<?php
$xml = <<<XML
<?xml version="1.0"?>
<root>
  <parent>
     <child>bar</child>
     <child>foo</child>
  </parent>
</root>
XML;
?>

If we wanted to replace the entire <parent> node, we could do something like this:

<?php
// Create a new document fragment to hold the new <parent> node
$parent = new DomDocument;
$parent_node = $parent ->createElement('parent');

// Add some children
$parent_node->appendChild($parent->createElement('child', 'somevalue'));
$parent_node->appendChild($parent->createElement('child', 'anothervalue'));

// Add the keywordset into the new document
// The $parent variable now holds the new node as a document fragment
$parent->appendChild($parent_node);
?>

Next, we need to locate the old node:

<?php
// Load the XML
$dom = new DomDocument;
$dom->loadXML($xml);

// Locate the old parent node
$xpath = new DOMXpath($dom);
$nodelist = $xpath->query('/root/parent');
$oldnode = $nodelist->item(0);
?>

We then import and replace the new node:

<?php
// Load the $parent document fragment into the current document
$newnode = $dom->importNode($parent->documentElement, true);

// Replace
$oldnode->parentNode->replaceChild($newnode, $oldnode);

// Display
echo $dom->saveXML();
?>

Our new node is successfully imported:

<?xml version="1.0"?>
<root>
<parent><child>somevalue</child><child>anothervalue</child></parent>
</root>
franp at free dot fr 23-Sep-2004 05:39
1) If your XPath query returns a NodeList including a unique item, or if you know for sure the order of the items returned, you can use the "item(n)" syntax instead of the "foreach" syntax.
This can greatly improve you code lisibility.
s the method name implies, replaceChild cannot replace a node itself but a child of a node.
The trick is to use replaceChild on the parent node of your Xpath query result.

<?xml version="1.0" ?>
<action>
 <actionGlobal>
  <actionGlobalFR>sometext</actionGlobalFR>
 <actionGlobal>
</action>

$frag = $doc->createElement("actionGlobalFR");
$fragA = $doc->createTextNode("anothertext");
$frag->appendChild($fragA);

$xpResult = $xp->query("/action/actionGlobal/actionGlobalFR");
$blipblip = $xpResult->item(0)->parentNode->replaceChild($ajout, $xpResult->item(0));

2) Also, as the method name implies, replaceChild cannot replace a node itself but a child of a node.
Still it is possible to replace a node pointed by XPath istead of its child.
The trick is to use replaceChild on the parent node of your Xpath query result.

<?xml version="1.0" ?>
<action>
  <FR>French Text</FR>
</action>

<?php
$frag = $doc->createElement("EN");
$fragA = $doc->createTextNode("English Text");
$frag->appendChild($fragA);

$xpResult = $xp->query("/action/FR");
$blipblip = $xpResult->item(0)->parentNode->replaceChild($fragA, $xpResult->item(0));
?>

Et voil? !

This produces :
<?xml version="1.0" ?>
<action>
  <EN>English Text</EN>
</action>

....................................................

3) Also, be carefull, you CANNOT replace a node that doesn't exist.
While this may seems obvious, it is easy to forget.

Consider this :
<?xml version="1.0" ?>
<action>
  <EN></EN>
</action>

You cannot use replaceChild() to turn this into :
<?xml version="1.0" ?>
<action>
  <EN>Some text</EN>
</action>

The reason is that since the <EN></EN> element is empty, it has no child (this is clearer to understand if you consider that <EN></EN> can be written <EN />).
The fact that you intend to put some text inbetween <EN> and </EN> does not change the fact that it has no text yet, thus no child yet.
When dealing with DOM, do not take your dream for the reality. The DOM parser doen't care about your dreams. If an element is currently empty, it has no child, whatever you intend to fill in.

Thus, the solution to teh problem is to use appendChild intead of replaceChild :

<?php
$fragA = $doc->createTextNode("Some Text");
$xpResult = $xp->query("/action/EN");
$blipblip = $xpResult->item(0)->appendChild($fragA);
?>

This produces the awaited :
<?xml version="1.0" ?>
<action>
  <EN>Some Text</EN>
</action>

....................................................

4) Note that the description of replaceChild in the doc is wrong. Arguments have been inverted.
The correct description is :
object DOMNode->replaceChild (object newnode, object oldnode)


The DOMNodeList class

(PHP 5, PHP 7)

클래스 개요

DOMNodeList implements Traversable {
/* 프로퍼티 */
readonly public int $length ;
/* 메소드 */
DOMElement DOMNodelist::item ( int $index )
}

프로퍼티

length

The number of nodes in the list. The range of valid child node indices is 0 to length - 1 inclusive.

참고

  • » W3C specification of NodeList

Table of Contents

  • DOMNodelist::item — Retrieves a node specified by index

User Contributed Notes

ignitedfirestarter at gmail dot com 04-Jul-2012 07:39
If you want to recurse over a DOM then this might help:
<?php

/**
 * PHP's DOM classes are recursive but don't provide an implementation of
 * RecursiveIterator. This class provides a RecursiveIterator for looping over DOMNodeList
 */
class DOMNodeRecursiveIterator extends ArrayIterator implements RecursiveIterator {

  public function __construct (DOMNodeList $node_list) {

    $nodes = array();
    foreach($node_list as $node) {
      $nodes[] = $node;
    }

    parent::__construct($nodes);

  }

  public function getRecursiveIterator(){
    return new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST);
  }

  public function hasChildren () {
    return $this->current()->hasChildNodes();
  }

  public function getChildren () {
    return new self($this->current()->childNodes);
  }

}
?>
geompse at gmail dot com 19-May-2010 05:39
Note that $length is calculated (php5.3.2).
Iterating over a large NodeList may be time expensive.

Prefer :
$nb = $nodelist->length;
for($pos=0; $pos<$nb; $pos++)

Than:
for($pos=0; $pos<$nodelist->length; $pos++)

I had a hard time figuring that out...
c dot 1 at smithies dot org 28-Aug-2008 12:26
I doubt the accuracy of what saad105050 wrote below. In particular, in his example, he seems to assume that $element->getElementsByTagName() will return NULL if there are no matching nodes. This is not what happens; as per the documentation, a DOMNodeList is returned with the length property zero.
bobvandell at hotmail dot com 26-Aug-2008 06:12
That's actually incorrect. You can use function results as objects. It makes building an API for your database very clean and neat. For example:

Our code:

$articles = Node::screate('tags', 123456)->assets('like:title:test')->articles;

We use the above code to get articles that are linked to assets that are linked to a specific tag in our database.
c dot 1 at smithies dot org 23-May-2008 02:43
You can modify, and even delete, nodes from a DOMNodeList if you iterate backwards:

$els = $document->getElementsByTagName('input');
for ($i = $els->length; --$i >= 0; ) {
  $el = $els->item($i);
  switch ($el->getAttribute('name')) {
    case 'MAX_FILE_SIZE' :
      $el->parentNode->removeChild($el);
    break;
    case 'inputfile' :
      $el->setAttribute('type', 'text');
    //break;
  }
}
drichter at muvicom dot de 14-May-2008 03:11
Addition to my first note:

An traditional for-loop does not allow you to change the DOM-tree while looping - the effects are the nearly the same as with foreach. So you have to collect the nodes in an array and do the tree-altering stuff within a second loop (looping the array this time ...)
drichter at muvicom dot de 14-May-2008 01:56
I have done some testing and have found 2 results:
(My System: Win XP with PHP 5.2.1)

1) Iteration with foreach does function correctly as "james dot j dot hackett at gmail dot com" writes, _if_ you only do readonly stuff with foreach or minor writings of some attributes.

2) foreach does not function, if you are doing some DOM-Operations while iterating. In my situation it was adding the iterated $node as an child to an new node:

$newNode = $dom->createElement('newNode') ;
foreach ($nodeList as $node) {
  echo $node->nodeValue ;
  $newNode->appendChild($node) ;
}

This only gives you the first element ...

I'm interpreting it as an confusing but correct behavior because of the changes within the $dom-object while appending the node at an additional place ...

So, if you want to do something like 2) use for, length and item() :)
james dot j dot hackett at gmail dot com 08-May-2008 06:47
In Response to 'kassah at gmail'

You don't need to convert a DOMNodeList to an array in order iterate through it using 'foreach'.  You can use foreach directly with the DOMNodeList.

$nodeList = $someDomDocument->getElementsbytagname('user');

foreach ($nodeList as $node) {
    echo $node->nodeValue;
}
kassah at gmail dot com 05-May-2008 02:06
// Converts a DOMNodeList to an Array that can be easily foreached
function dnl2array($domnodelist) {
    $return = array();
    for ($i = 0; $i < $domnodelist->length; ++$i) {
        $return[] = $domnodelist->item($i);
    }
    return $return;
}
brack at wjp dot de 21-Apr-2008 11:35
In PHP 5.2.5 (Windows) it is not possible to iterate correctly over the DOMNodeList object returned by DOMNode->childNodes using foreach. Instead I had to use the for loop in conjunction with the item() method of DOMNodeList for iterating over all child nodes correctly.

I don't know whether this is really a bug, but apparently it is.


Retrieves a node specified by index

  • ≪ DOMNodeList
  • DOMNotation ≫
  • PHP Manual
  • DOMNodeList
  • Retrieves a node specified by index

DOMNodelist::item

(PHP 5, PHP 7)

DOMNodelist::item — Retrieves a node specified by index

설명

DOMElement DOMNodelist::item ( int $index )

Retrieves a node specified by index within the DOMNodeList object.

Tip

If you need to know the number of nodes in the collection, use the length property of the DOMNodeList object.

인수

index

Index of the node into the collection.

반환값

The node at the indexth position in the DOMNodeList, or NULL if that is not a valid index.

예제

Example #1 Traversing all the entries of the table

<?php

$doc = new DOMDocument;
$doc->load('book.xml');

$items = $doc->getElementsByTagName('entry');

for ($i = 0; $i < $items->length; $i++) {
    echo $items->item($i)->nodeValue . "\n";
}

?>

Alternatively, you can use foreach, which is a much more convenient way:

<?php

foreach ($items as $item) {
    echo $item->nodeValue . "\n";
}

?>

위 예제의 출력:

Title
Author
Language
ISBN
The Grapes of Wrath
John Steinbeck
en
0140186409
The Pearl
John Steinbeck
en
014017737X
Samarcande
Amine Maalouf
fr
2253051209

User Contributed Notes

jeffpuckett2 at gmail dot com 15-Jun-2016 11:55
DOMNodelist::item can return a DOMElement object, which extends DOMNode class. But it can also return a DOMText object.

<?php
$xml = '
<root>
  <node/>
  <node>
    <sub>more</sub>
  </node>
  <node>
    <sub>another</sub>
  </node>
  <node>value</node>
</root>
';

$doc = new DOMDocument();
$doc->loadXML($xml);

$items = $doc->documentElement->childNodes;
for ($i = 0; $i < $items->length; $i++)
  echo get_class($items->item($i)).PHP_EOL;
?>

Results in this output:

DOMText
DOMElement
DOMText
DOMElement
DOMText
DOMElement
DOMText
DOMElement
DOMText
strangerli at onlinehome dot de 13-Sep-2013 05:11
Actually "item" returns DOMElement and not DOMNode. DOMElement extends only DOMNode.

http://3v4l.org/KAj7c
Anonymous 26-Jun-2012 08:36
calling domnodelist->item($i) with an index $i greater than domnodelist->length - 1 , will return the entire document, will not produce an error, and will not circle back to the start of the list.
Nagy Attila 06-Jan-2011 06:02
DOMNodeList::item does not return in constant time!

If you need to iterate over large nodelist then you'd better use standard navigation.

Instead of:

<?php
foreach ($nodelist as $node) {
    // do something
}
?>

you could do:

<?php
$node = $parentnode->firstChild;

do {
    // do something
} while ($node = $node->nextSibling);
?>
olivier dot berger at it-sudparis dot eu 21-Aug-2008 11:56
It seems that with zend.ze1_compatibility_mode on, the only way to iterate over the items list is with :
for ($i = 0; $i < $nodeList->length; ++$i) {
    $nodeName = $nodeList->item($i)->nodeName;
    $nodeValue = $nodeList->item($i)->nodeValue;
}

As other attemps failed :

for ($i = 0; $i < $nodeList->length; ++$i) {
    $node = &$nodeList->item($i);
    $nodeName = $node->nodeName;
    $nodeValue = $node->nodeValue;
}

or :
foreach ($nodeList as $node) {
    echo $node->nodeName;
    echo $node->nodeValue;
}
vinyanov at poczta dot onet dot pl 06-Jan-2008 07:12
SimpleXML has its own SPL iterator. See http://www.php.net/~helly/php/ext/spl/classSimpleXMLIterator.html . But I guess that there are none for DOM nodes. By the way, two out of three implementations I found over the Net were not recursive, so I wrote my own. Here is the snippet:

<?php

class DOMNodeListIterator implements RecursiveIterator
{
    private
        $nodes,
        $offset;

    function __construct(DOMNodeList $nodes)
    {
        return $this -> nodes = $nodes;
    }

    function rewind()
    {
        return $this -> offset = 0;
    }

    function current()
    {
        return $this -> nodes -> item($this -> offset);
    }

    function key()
    {
        return $this -> current() -> nodeName;
    }

    function next()
    {
        return $this -> offset++;
    }

    function valid()
    {
        return $this -> offset < $this -> nodes -> length;
    }

    function hasChildren()
    {
        return isset($this -> current() -> childNodes -> length) && $this -> current() -> childNodes -> length > 0;
    }

    function getChildren()
    {
        return new self($this -> current() -> childNodes);
    }
}

?>

Remember to use RecursiveIteratorIterator::SELF_FIRST flag when you create your iterator iterator.

<?php

$iterator = new DOMNodeListIterator($document -> childNodes);
$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);

?>

Should work, has just few minutes though. :)
Hayley Watson 29-Oct-2007 11:14
Keep in mind that DOMNodelists are "live" - changes to the document or node that the DOMNodelist was derived from will be reflected in the DOMNodelist. In other words, a list of a parent node's children will change if you change the parent's children!
james dot dunmore at gmai dot com 27-Jul-2007 05:57
tfg_allardyce at gmail dot com

I have had exactly this problem.

To rectify I've had to do this:
<?php
$old_element = $doc->getElementsByTagName('Element1')->item(0);
$new_element = $doc->createElement('NewElement1');

$old_element_childNodes = $old_element->childNodes;
$length = $old_element_childNodes->length;

for($i = 0; $i < $length; $i++)
{
    $oldChildren_array[] = $old_element_childNodes->item($i);
}

foreach($oldChildren_array as $old_c)
{
    $new_element->appendChild($old_c);
}
?>

Rather than this:
(which I will bug report)
<?php
$old_element = $doc->getElementsByTagName('Element1')->item(0):
$new_element = $doc->createElement('NewElement1');

foreach($old_element->childNode as $node)
{
     $new_element->appendChild($node);
}

?>

Using the latter, randomally removes the children!
Geoffrey Thubron 26-May-2007 11:47
@ tfg_allardyce at gmail dot com

You could loop through the list backwards, that way, you are only ever taking off the last item from the list, and hence wont have disrupted the order.
oliver dot christen at camptocamp dot com 13-Feb-2007 04:27
NodeList are something annoying because you can't output the content with a simple print_r, so I did a little function that add all the node to a new empty DOMDocument and output it as a string.
Have fun.

<?php

public function domNodeList_to_string($DomNodeList) {
    $output = '';
    $doc = new DOMDocument;
    while ( $node = $DomNodeList->item($i) ) {
        // import node
        $domNode = $doc->importNode($node, true);
        // append node
        $doc->appendChild($domNode);
        $i++;
    }
    $output = $doc->saveXML();
    $output = print_r($output, 1);
    // I added this because xml output and ajax do not like each others
    $output = htmlspecialchars($output);
    return $output;
}

?>


The DOMNotation class

(PHP 5, PHP 7)

클래스 개요

DOMNotation extends DOMNode {
/* 프로퍼티 */
readonly public string $publicId ;
readonly public string $systemId ;
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

프로퍼티

publicId

systemId

User Contributed Notes

There are no user contributed notes for this page.


The DOMProcessingInstruction class

(PHP 5, PHP 7)

클래스 개요

DOMProcessingInstruction extends DOMNode {
/* 프로퍼티 */
readonly public string $target ;
public string $data ;
/* 메소드 */
public __construct ( string $name [, string $value ] )
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

프로퍼티

target

data

Table of Contents

  • DOMProcessingInstruction::__construct — Creates a new DOMProcessingInstruction object

User Contributed Notes

There are no user contributed notes for this page.


Creates a new DOMProcessingInstruction object

  • ≪ DOMProcessingInstruction
  • DOMText ≫
  • PHP Manual
  • DOMProcessingInstruction
  • Creates a new DOMProcessingInstruction object

DOMProcessingInstruction::__construct

(PHP 5, PHP 7)

DOMProcessingInstruction::__construct — Creates a new DOMProcessingInstruction object

설명

public DOMProcessingInstruction::__construct ( string $name [, string $value ] )

Creates a new DOMProcessingInstruction object. This object is read only. It may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. To create a writeable node, use DOMDocument::createProcessingInstruction.

인수

name

The tag name of the processing instruction.

value

The value of the processing instruction.

예제

Example #1 Creating a new DOMProcessingInstruction object

<?php

$dom = new DOMDocument('1.0', 'UTF-8');
$html = $dom->appendChild(new DOMElement('html'));
$body = $html->appendChild(new DOMElement('body'));
$pinode = new DOMProcessingInstruction('php', 'echo "Hello World"; ');
$body->appendChild($pinode);
echo $dom->saveXML();

?>

위 예제의 출력:

<?xml version="1.0" encoding="UTF-8"?>
<html><body><?php echo "Hello World"; ?></body></html>

참고

  • DOMDocument::createProcessingInstruction() - Creates new PI node

User Contributed Notes

There are no user contributed notes for this page.


The DOMText class

(PHP 5, PHP 7)

소개

The DOMText class inherits from DOMCharacterData and represents the textual content of a DOMElement or DOMAttr.

클래스 개요

DOMText extends DOMCharacterData {
/* 프로퍼티 */
readonly public string $wholeText ;
/* 메소드 */
public __construct ([ string $value ] )
public bool isWhitespaceInElementContent ( void )
public DOMText splitText ( int $offset )
/* 상속된 메소드 */
public DOMNode DOMNode::appendChild ( DOMNode $newnode )
public string DOMNode::C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int DOMNode::C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
public string DOMNode::getNodePath ( void )
public bool DOMNode::hasAttributes ( void )
public bool DOMNode::hasChildNodes ( void )
public DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool DOMNode::isDefaultNamespace ( string $namespaceURI )
public bool DOMNode::isSameNode ( DOMNode $node )
public bool DOMNode::isSupported ( string $feature , string $version )
public string DOMNode::lookupNamespaceURI ( string $prefix )
public string DOMNode::lookupPrefix ( string $namespaceURI )
public void DOMNode::normalize ( void )
public DOMNode DOMNode::removeChild ( DOMNode $oldnode )
public DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

프로퍼티

wholeText

Holds all the text of logically-adjacent (not separated by Element, Comment or Processing Instruction) Text nodes.

Table of Contents

  • DOMText::__construct — Creates a new DOMText object
  • DOMText::isWhitespaceInElementContent — Indicates whether this text node contains whitespace
  • DOMText::splitText — Breaks this node into two nodes at the specified offset

User Contributed Notes

Trititaty 20-Oct-2016 08:47
Text replacement function for DOM.

<?php
function domTextReplace( $search, $replace, DOMNode &$domNode, $isRegEx = false ) {
  if ( $domNode->hasChildNodes() ) {
    $children = array();
    // since looping through a DOM being modified is a bad idea we prepare an array:
    foreach ( $domNode->childNodes as $child ) {
      $children[] = $child;
    }
    foreach ( $children as $child ) {
      if ( $child->nodeType === XML_TEXT_NODE ) {
        $oldText = $child->wholeText;
        if ( $isRegEx ) {
          $newText = preg_replace( $search, $replace, $oldText );
        } else {
          $newText = str_replace( $search, $replace, $oldText );
        }
        $newTextNode = $domNode->ownerDocument->createTextNode( $newText );
        $domNode->replaceChild( $newTextNode, $child );
      } else {
        domTextReplace( $search, $replace, $child, $isRegEx );
      }
    }
  }
}


Creates a new DOMText object

  • ≪ DOMText
  • DOMText::isWhitespaceInElementContent ≫
  • PHP Manual
  • DOMText
  • Creates a new DOMText object

DOMText::__construct

(PHP 5, PHP 7)

DOMText::__construct — Creates a new DOMText object

설명

public DOMText::__construct ([ string $value ] )

Creates a new DOMText object.

인수

value

The value of the text node. If not supplied an empty text node is created.

예제

Example #1 Creating a new DOMText

<?php

$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->appendChild(new DOMElement('root'));
$text = $element->appendChild(new DOMText('root value'));
echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?><root>root value</root> */

?>

참고

  • DOMDocument::createTextNode() - Create new text node

User Contributed Notes

Thomas 27-Apr-2012 10:54
I experinced that DOMText, given a integer as only value to the constructor failed to return valid data. I had to typecast the input to a string first:

$intValue = 3252; // Post/Zip-code
$some_element->appendChild( new DOMText( (string) $intValue) );

When not typecasting using "(string)" the XML output from $root->saveXML() would not validate.


Indicates whether this text node contains whitespace

  • ≪ DOMText::__construct
  • DOMText::splitText ≫
  • PHP Manual
  • DOMText
  • Indicates whether this text node contains whitespace

DOMText::isWhitespaceInElementContent

(PHP 5, PHP 7)

DOMText::isWhitespaceInElementContent — Indicates whether this text node contains whitespace

설명

public bool DOMText::isWhitespaceInElementContent ( void )

Indicates whether this text node contains only whitespace or it is empty. The text node is determined to contain whitespace in element content during the load of the document.

반환값

Returns TRUE if node contains zero or more whitespace characters and nothing else. Returns FALSE otherwise.

User Contributed Notes

JITR 08-Jul-2007 12:36
To add to `permidion at yahoo dot com''s comment:

Quite true. According to DOM Level 3 Core, the `isWhitespaceInElementContent()' method indicates, whether the node contains `whitespace in element content' (or `element content whitespace' according to the newer spec), see [1].

Btw, it seems the `isWhitespaceInElementContent()' method was replaced with the `isElementContentWhitespace' attribute in the current W3C recommendation, see [2].

I have the feeling, though, it would be more precise with the word `only' added, like: `... indicates, whether the node contains *only* element content whitespace.' This would better correspond with the fact the result is false, when there are any non-whitespace characters present (in addition to the whitespace ones), as already noted.

[1] (please concatenate the following two lines)
http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html
#core-Text3-isWhitespaceInElementContent
[2] (please concatenate the following two lines)
http://www.w3.org/TR/DOM-Level-3-Core/core.html
#Text3-isElementContentWhitespace
permidion at yahoo dot com 28-Oct-2004 05:30
if you have an xml element such as <name>Foo Bar</name> (note the space), isWhitespaceInElementContent will return false.
However it will return true for <name><name> or <name/>
so, that function can be used to determine if the element has a text content or not


Breaks this node into two nodes at the specified offset

  • ≪ DOMText::isWhitespaceInElementContent
  • DOMXPath ≫
  • PHP Manual
  • DOMText
  • Breaks this node into two nodes at the specified offset

DOMText::splitText

(PHP 5, PHP 7)

DOMText::splitText — Breaks this node into two nodes at the specified offset

설명

public DOMText DOMText::splitText ( int $offset )

Breaks this node into two nodes at the specified offset, keeping both in the tree as siblings.

After being split, this node will contain all the content up to the offset. If the original node had a parent node, the new node is inserted as the next sibling of the original node. When the offset is equal to the length of this node, the new node has no data.

인수

offset

The offset at which to split, starting from 0.

반환값

The new node of the same type, which contains all the content at and after the offset.

User Contributed Notes

Flix Cloutier 28-Oct-2013 02:31
It should be noted that $offset is a **character offset**, not a **byte offset**. This means that most other PHP string functions that deal with lengths and offsets (strlen, strpos, preg_match with PREG_OFFSET_CAPTURE, etc.) use and return values unsuitable for this method if used with multibyte strings (like UTF-8 strings).

Byte offsets can be converted to character offsets with mb_strlen:

<?php
function char_offset($string, $byte_offset, $encoding = null)
{
    $substr = substr($string, 0, $byte_offset);
    return mb_strlen($substr, $encoding ?: mb_internal_encoding());
}
?>


The DOMXPath class

(PHP 5, PHP 7)

소개

Supports XPath 1.0

클래스 개요

DOMXPath {
/* 프로퍼티 */
public DOMDocument $document ;
/* 메소드 */
public __construct ( DOMDocument $doc )
public mixed evaluate ( string $expression [, DOMNode $contextnode [, bool $registerNodeNS = true ]] )
public DOMNodeList query ( string $expression [, DOMNode $contextnode [, bool $registerNodeNS = true ]] )
public bool registerNamespace ( string $prefix , string $namespaceURI )
public void registerPhpFunctions ([ mixed $restrict ] )
}

프로퍼티

document

Table of Contents

  • DOMXPath::__construct — Creates a new DOMXPath object
  • DOMXPath::evaluate — Evaluates the given XPath expression and returns a typed result if possible
  • DOMXPath::query — Evaluates the given XPath expression
  • DOMXPath::registerNamespace — Registers the namespace with the DOMXPath object
  • DOMXPath::registerPhpFunctions — Register PHP functions as XPath functions

User Contributed Notes

archimedix32783262 at mailinator dot com 03-Sep-2014 09:03
Note that evaluate() will use the same encoding as the XML document.

So if you have a UTF-16 XML, you will have to query using UTF-16 strings.

You can use iconv() to convert from your code's encoding to the target encoding for better legibility.
dhz 05-Jan-2011 07:47
I just spent far too much time chasing this one....

When running an xpath query on a table be careful about table internal nodes (ie: <tr></tr>, and <td></td>).  If the master <table> tag is missing, then query() (and likely evaluate() also) will return unexpected results.

I had a DOMNode with a structure like this:

<td>
    <table></table>
    <table>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
        </tr>
    </table>
</td>

Upon which I was trying to do a relative query (ie: <?php $xpath_obj->query('my/x/path', $relative_node); ?>).

But because of the lone outer <td></td> tags, the inner tags were being invalidated, while the nodes were still recognized.  Meaning that the following query would work:

<?php $xpath_obj->query('*[2]/*[*[2]]', $relative_node); ?>

But when replacing any of the "*" tokens with the corresponding (and valid) "table", "tr", or "td" tokens the query would inexplicably break.
david at lionhead dot nl 11-Aug-2009 03:33
When using DOMXPath and having a default namespace. Consider using an intermediate function to add the default namespace to all queries:

<?php
// The default namespace: x:xmlns="http://..."
$path="/Book/Title";
$path=preg_replace("\/([a-zA-Z])","/x:$1",$path);

// Result: /x:Book/x:Title
?>
Mark Omohundro, ajamyajax dot com 14-Dec-2008 06:15
<?php
// to retrieve selected html data, try these DomXPath examples:

$file = $DOCUMENT_ROOT. "test.html";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);

$xpath = new DOMXpath($doc);

// example 1: for everything with an id
//$elements = $xpath->query("//*[@id]");

// example 2: for node data in a selected id
//$elements = $xpath->query("/html/body/div[@id='yourTagIdHere']");

// example 3: same as above with wildcard
$elements = $xpath->query("*/div[@id='yourTagIdHere']");

if (!is_null($elements)) {
  foreach ($elements as $element) {
    echo "<br/>[". $element->nodeName. "]";

    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo $node->nodeValue. "\n";
    }
  }
}
?>


Creates a new DOMXPath object

  • ≪ DOMXPath
  • DOMXPath::evaluate ≫
  • PHP Manual
  • DOMXPath
  • Creates a new DOMXPath object

DOMXPath::__construct

(PHP 5, PHP 7)

DOMXPath::__construct — Creates a new DOMXPath object

설명

public DOMXPath::__construct ( DOMDocument $doc )

Creates a new DOMXPath object.

인수

doc

The DOMDocument associated with the DOMXPath.

User Contributed Notes

thsoft at vipmail dot hu 04-Jul-2007 01:57
As of PHP 5.2.0, DOMXPath object implements XPath 1.0.
Saulo Vallory 27-Jun-2007 12:23
The DOMXPath object doesn't create a reference to the document. If you load a new xml using either load, loadXML or loadHTML functions the DOMXPath object will still query and evaluate over the source in the DOMDocument when it was created.


Evaluates the given XPath expression and returns a typed result if possible

  • ≪ DOMXPath::__construct
  • DOMXPath::query ≫
  • PHP Manual
  • DOMXPath
  • Evaluates the given XPath expression and returns a typed result if possible

DOMXPath::evaluate

(PHP 5 >= 5.1.0, PHP 7)

DOMXPath::evaluate — Evaluates the given XPath expression and returns a typed result if possible

설명

public mixed DOMXPath::evaluate ( string $expression [, DOMNode $contextnode [, bool $registerNodeNS = true ]] )

Executes the given XPath expression and returns a typed result if possible.

인수

expression

The XPath expression to execute.

contextnode

The optional contextnode can be specified for doing relative XPath queries. By default, the queries are relative to the root element.

registerNodeNS

The optional registerNodeNS can be specified to disable automatic registration of the context node.

반환값

Returns a typed result if possible or a DOMNodeList containing all nodes matching the given XPath expression.

If the expression is malformed or the contextnode is invalid, DOMXPath::evaluate() returns FALSE.

변경점

버전 | 설명
5.3.3 | The registerNodeNS parameter was added.

예제

Example #1 Getting the count of all the english books

<?php

$doc = new DOMDocument;

$doc->load('book.xml');

$xpath = new DOMXPath($doc);

$tbody = $doc->getElementsByTagName('tbody')->item(0);

// our query is relative to the tbody node
$query = 'count(row/entry[. = "en"])';

$entries = $xpath->evaluate($query, $tbody);
echo "There are $entries english books\n";

?>

위 예제의 출력:

There are 2 english books

참고

  • DOMXPath::query() - Evaluates the given XPath expression

User Contributed Notes

aazaharov81 at gmail dot com 14-Jan-2015 09:48
To query DOMNodes by their HTML classes, use such snippet
<?php

// CssClassXPathSelector
function ccxs($class) {
    return '[contains(concat(" ", normalize-space(@class), " "), " ' . $class . ' ")]';
}

 // then just
 $domitems = $this->xpath("//*[@id='searchResultsRows']//a" . ccxs('listing_row'));
?>
daniel at danielnorton dot com 26-Jun-2011 06:56
Note that this method does not provide any means to distinguish between a successful result that returns FALSE and an error.

For example, this will succeed and return FALSE:

<?php $xpath->evaluate("1 = 0"); ?>

One workaround when you know you are expecting a Boolean is to wrap the result with string(). e.g.

<?php $xpath->evaluate("string(1 = 0)"); ?>

This will return a string "false" on success, or the Boolean FALSE on error.
Damien Bezborodov 29-Mar-2011 03:13
If your expression returns a node set, you will get a DOMNodeList instead of a typed result. Instead, try modifying your expression from "//node[1]" to "string(//node[1])".
daniel ad demus dk 16-Jun-2008 12:02
It seems this method is only included from PHP 5.1.
yuriucsal at NOSPAM dot yahoo dot com dot br 06-Mar-2005 09:55
this class can substitute the method evaluate while it is not validated. Made for Yuri Bastos and Jo?o Gilberto Magalh?es.

<?php

    class XPtahQuery
    {
        // function returns a DOMNodeList from a relative xPath
        public static function selectNodes($pNode, $xPath)
        {

            $pos = strpos(self::getFullXpath($pNode),"/",1);
            $xPathQuery = substr(self::getFullXpath($pNode),$pos);//to paste  /#document[1]/
            $xPathQueryFull = $xPathQuery. $xPath;
            $domXPath = new DOMXPath($pNode->ownerDocument);
            $rNodeList = $domXPath->query($xPathQueryFull);

                return $rNodeList;
        }
        // function returns a DOMNode from a xPath from other DOMNode
        public static function selectSingleNode($pNode, $xPath)
        {

            $pos = strpos(self::getFullXpath($pNode),"/",1);
            $xPathQuery = substr(self::getFullXpath($pNode),$pos);//to paste  /#document[1]/
            $xPathQueryFull = $xPathQuery. $xPath;
            $domXPath = new DOMXPath($pNode->ownerDocument);
            $rNode = $domXPath->query($xPathQueryFull)->item(0);

                return $rNode;
        }
        //utilitaries functions off selectSingleNode
        private function getNodePos($pNode, $nodeName)
        {
            if($pNode == null)
                {
                        return 0;
            }
            else
            {
                $var = 0;
                    if ($pNode->previousSibling != null)
                    {
                    if ($pNode->previousSibling->nodeName == $nodeName)
                    {
                        $var = 1;
                    }
                    }
                    return self::getNodePos($pNode->previousSibling, $nodeName) + $var;
            }
        }
        //utilitaries functions off selectSingleNode
        private function getFullXpath($pNode)
        {
            if($pNode == null)
                {
                        return "";
            }
            else
            {

                return self::getFullXpath($pNode->parentNode) . "/" . $pNode->nodeName . "[" .strval(self::getNodePos($pNode, $pNode->nodeName)+1) . "]";//+1 to get the real xPath index

            }
        }
    }
?>


Evaluates the given XPath expression

  • ≪ DOMXPath::evaluate
  • DOMXPath::registerNamespace ≫
  • PHP Manual
  • DOMXPath
  • Evaluates the given XPath expression

DOMXPath::query

(PHP 5, PHP 7)

DOMXPath::query — Evaluates the given XPath expression

설명

public DOMNodeList DOMXPath::query ( string $expression [, DOMNode $contextnode [, bool $registerNodeNS = true ]] )

Executes the given XPath expression.

인수

expression

The XPath expression to execute.

contextnode

The optional contextnode can be specified for doing relative XPath queries. By default, the queries are relative to the root element.

registerNodeNS

The optional registerNodeNS can be specified to disable automatic registration of the context node.

반환값

Returns a DOMNodeList containing all nodes matching the given XPath expression. Any expression which does not return nodes will return an empty DOMNodeList.

If the expression is malformed or the contextnode is invalid, DOMXPath::query() returns FALSE.

변경점

버전 | 설명
5.3.3 | The registerNodeNS parameter was added.

예제

Example #1 Getting all the english books

<?php

$doc = new DOMDocument;

// We don't want to bother with white spaces
$doc->preserveWhiteSpace = false;

$doc->Load('book.xml');

$xpath = new DOMXPath($doc);

// We starts from the root element
$query = '//book/chapter/para/informaltable/tgroup/tbody/row/entry[. = "en"]';

$entries = $xpath->query($query);

foreach ($entries as $entry) {
    echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
         " by {$entry->previousSibling->nodeValue}\n";
}
?>

위 예제의 출력:

Found The Grapes of Wrath, by John Steinbeck
Found The Pearl, by John Steinbeck

We can also use the contextnode parameter to shorten our expression:

<?php

$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;

$doc->load('book.xml');

$xpath = new DOMXPath($doc);

$tbody = $doc->getElementsByTagName('tbody')->item(0);

// our query is relative to the tbody node
$query = 'row/entry[. = "en"]';

$entries = $xpath->query($query, $tbody);

foreach ($entries as $entry) {
    echo "Found {$entry->previousSibling->previousSibling->nodeValue}," .
         " by {$entry->previousSibling->nodeValue}\n";
}
?>

참고

  • DOMXPath::evaluate() - Evaluates the given XPath expression and returns a typed result if possible

User Contributed Notes

chris dot russo99 at gmail dot com 17-Aug-2016 12:58
If you can't find a solution for PHP XPATH case sensitivity, you can always try this approach:

http://fsockopen.com/php-programming/your-final-stop-for-php-xpath-case-insensitive

Instead of inserting PHP functions into a XPATH object, convert the XPATH object into an Array(), and then use any PHP function, the regular way.
ikmahesh at cdac dot in 08-Jul-2015 06:29
The parameters for $xPath->query() method is case sensitive.
It matches IDs exact words.
RiKdnUa at mail dot ru 18-Sep-2013 01:26
Пример XPath запроса к XML документу. XML документ содержить элементы с именами из НЕлатинских символов (кириллица). При использовании в XPath запросе предиката, функция DOMXPath::query() выдает предупреждение и запрос не работает. Чтобы запрос работал, надо явно указывать ось. Файл этого примера должен быть в кодировке WINDOWS-1251. Тестировал в PHP 5.2.9-2 и PHP 5.2.17

Example XPath-query to the XML-document. XML-document contains an elements with the names of non-Latin characters (cyrillic). When used predicate in XPath-query, function DOMXPath::query() gives a warning and query does not work. In order to earned the query, it is necessary to explicitly specify the axis. The file of this example is to be in the encoding WINDOWS-1251. Tested in PHP 5.2.9-2 and PHP 5.2.17
<?php
ini_set("display_errors","on");
error_reporting(-1);
function utf8encode($str){return iconv('WINDOWS-1251', 'UTF-8', $str);}
$xml="<?xml version='1.0' encoding='WINDOWS-1251'?>
<часть>
<ссылка href='yandex.com'>Яндекс</ссылка>
<ссылка href='rik.dn.ua/fotopan.php'>г.Донецк</ссылка>
</часть>
";
$document=new domDocument();
$document->preserveWhiteSpace=false;
$document->loadXML($xml);
$domxpath=new domXpath($document);
$list=$domxpath->query(utf8encode('/child::часть/child::ссылка'));//Ok
echo '$list->length='.$list->length."\n<br/>\n";
$list=$domxpath->query(utf8encode('/часть/ссылка'));//Ok
echo '$list->length='.$list->length."\n<br/>\n";
$list=$domxpath->query(utf8encode('/child::часть/child::ссылка[position()=1]'));//Ok
echo '$list->length='.$list->length."\n<br/>\n";
$list=$domxpath->query(utf8encode('/часть/ссылка[1]'));//Warning: DOMXPath::query() [domxpath.query]: Invalid expression in ...
echo '$list->length='.$list->length."\n<br/>\n";
$list=$domxpath->query(utf8encode('/часть/ссылка[position()=1]'));//Warning: DOMXPath::query() [domxpath.query]: Invalid expression in ...
echo '$list->length='.$list->length."\n<br/>\n";
?>
info at syncgw dot com 22-Sep-2011 06:05
A warning to all PHP programmer using this function PHP 5.0.0.0: We needed an equivalent to upper-case() function (is not available in XPath 1.0).

XML-Document:

<Rec>
<SourceRef>./c:calendar2</SourceRef>
<SourceRef>./c:calendar</SourceRef>
</Rec>

using

query('//DataStore[translate(SourceRef,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")="./C:CALENDAR"]/.')

returns ZERO matches.

If you change XML-Source to

<Rec>
<SourceRef>./c:calendar</SourceRef>
<SourceRef>./c:calendar2</SourceRef>
</Rec>

everything works fine
yuriscom at gmail dot com 07-Sep-2011 07:29
I hope it will be helpful for someone:

I spent some time to solve the problem when you query a string with quotes inside.

Suppose you have:
$parameter = "aaa \"bbb\"";
$domxpath->query("//path[text()=\"".$parameter."\""];

In versions > 5.3.0 there is registerPhpFunctions where you can put an addslashes. But in older version you cannot do it in simple way.

So the solution is to use a concat function. So when you have a substring with " inside, wrap it with '. And when you have a substring with ', then wrap with in ".

The code is:

<?php
$dom = new DOMDocument;
$dom->loadXML("<name>'bla' \"bla\" bla</name>");
$xpath = new DOMXPath($dom);
$nodeList = $xpath->query("//name[text()=concat(\"'bla' \" ,'\"bla\"' ,\" bla\")]");
?>

Below is the function that receives a string and returns a concat pattern for the xpath query.

<?php
    function getPattern_MQ($pattern) {
        // initiating an array of substrings
        $ar = array();
        // points to the current position in a string
        $offset = 0;
        $strlen = strlen($pattern);
        while (true) {
            // find a position of quotes
            $qPos = strpos($pattern, "\"", $offset);

            if (!$qPos) {
                // no more quotes
                $leftOver = $offset - $strlen;
                if ($leftOver < 0) {
                    $string = substr($pattern, $leftOver);
                    $ar[] = "\"" . $string . "\"";
                }
                break;
            }
            // add the whole substring before the quotes into the array
            $ar[] = "\"" . substr($pattern, $offset, ($qPos - $offset)) . "\"";
            // add the quotes wrapped with single quot
            $ar[] = "'" . substr($pattern, $qPos, 1) . "'";
            $offset = $qPos + 1;
        }
        // join the array to get: concat("aaa",'"',"bbb",'"');
        $pattern = "concat(''," . join(",", $dynamicPatternsAr) . ")";
        return $pattern;
    }
?>
chris AT cmbuckley DOT co DOT uk 14-Apr-2011 04:31
To help with the problem where the default namespace is not registered with the DOMXPath object, you can use the following replacement to update your paths accordingly:

<?php

$xml = <<<EOS
<root xmlns="urn:test">
    <foo>bar</foo>
</root>
EOS;

$expression = '//foo';
$prefix = 'fakeprefix';

$doc = new DOMDocument();
$doc->loadXML($xml);

$context = $doc->documentElement; // or whichever element you choose
$xpath = new DOMXPath($doc);

// register namespace as below, and apply a regex to the expression
if (null !== $context->namespaceURI) {
    $xpath->registerNamespace($prefix, $context->namespaceURI);
    $expression = preg_replace('#(::|/\s*|\A)(?![/@].+?|[a-z\-]+::)#', '$1' . $prefix . ':$2', $expression);
    var_dump($expression); // string(16) "//fakeprefix:foo"
}

$foo = $xpath->query($expression, $context)->item(0);
var_dump($doc->saveXML($foo)); // string(14) "<foo>bar</foo>"

?>
kkez at example dot com 03-Sep-2010 04:51
If the query() function seems to ignore your $contextnode, and instead returns all the tags in the document, try to use a relative path (use a . in front of the query):

<?php
    $xml = "<?xml version='1.0' encoding='UTF-8'?>
        <test>
            <tag1>
                <uselesstag>
                    <tag2>test</tag2>
                </uselesstag>
            </tag1>
            <tag2>test2</tag2>
        </test>";

    $dom = new DomDocument();
    $dom->loadXML($xml);
    $xpath = new DomXPath($dom);

    $tag1 = $dom->getElementsByTagName("tag1")->item(0);

    echo $xpath->query("//tag2")->length; //output 2 -> correct
    echo $xpath->query("//tag2", $tag1)->length; //output 2 -> wrong, the query is not relative
    echo $xpath->query(".//tag2", $tag1)->length; //output 1 -> correct (note the dot in front of //)
?>

See that i couldn't use $xpath->query("tag2", $tag1) as per the documentation, since "tag2" is not a direct child of "tag1".
I don't know why this note was deleted, i just tested it and it's correct.
It's not a bug, it's simply not written in the documentation.
Anonymous 27-Nov-2009 09:26
I found this useful for building page templates

<?php
$xsl = new DOMDocument;
$xsl->load('layout.xsl');

// Set the <xsl:include> href attribute, the inner stylesheet to include in this layout
$xpath = new DomXPath($xsl);
$res = $xpath->query('//xsl:include');
$res->item(0)->setAttribute('href','page.xsl');
$xsl->save('media/xsl/layout.xsl');
?>
Niklas 27-Aug-2008 11:36
For XPath escaping use the following method (of course it could be more efficient).
<?php
public function xpathescape($string)
{
$result = 'concat(';

for($i=0, $j=strlen($string); $i<$j; ++$i)
{
if($i > 0)
$result .= ",";

if($string[$i] == '\'')
$result .= "\"".$string[$i]."\"";
else
$result .= '\''.$string[$i].'\'';
}

$result .= ')';

return $result;
}
?>

Use it this way:
<php
$xpath->query('//example[sub='.xpathescape($acomplexstring).']');

?>
adam dot prall at thinkingman dot com 20-Jul-2008 06:01
If you're wondering, like I was, why your XPath queries are not returning any of the new DOMElements you create in your (X)HTML documents, and only the ones originally loaded in with (for example) loadXML(), this is why; if you're doing things right, you have registered the nameSpace 'html' after creating your DOMXPath object thus:

<?php

class XPathQueryLength {
     private $nameSpace = '';
     function __construct(DOMDocument $doc) {
          $this->xpath = new DOMXPath($this->doc);
          $this->xpath->registerNamespace(
                 'html','http://www.w3.org/1999/xhtml' );
     }
     function queryLength($query) {
          return $this->xpath->query($query)->length;
     }
}

?>

...but don't forget that when adding new elements to the above DOMDocument $doc, to use createElementNS() instead of createElement(), otherwise you'll have this problem:

<?php

//$doc is a previously loaded XHTML document containing a normal html, head and body structure
//$body is the first selected tag using $doc->getElementsByTagName('body');

$pTag = $doc->createElement('p','This is a new paragraph!');
$body->appendChild($pTag);

$xPath = new XPathQueryLength($doc);
print $xPath->queryLength('//html:p');

output: 0

print $xPath->queryLength('//p');

output: 1

?>

So do this instead:

<?php

//$doc is a previously loaded XHTML document containing a normal html, head and body structure
//$body is the first selected tag using $doc->getElementsByTagName('body');

$pTag = $doc->createElementNS('http://www.w3.org/1999/xhtml','p','This is a new paragraph!');
$body->appendChild($pTag);

$xPath = new XPathQueryLength($doc);
print $xPath->queryLength('//html:p');

output: 2

print $xPath->queryLength('//p');

output: 0

?>

The resulting XHTML file from both example scripts looks much like this:

<html>
  <head></head>
  <body>
     <p>This is a hardcoded paragraph.</p>
     <p>This is a new paragraph!</p>
  </body>
</html>

...so you would think that a paragraph is a paragraph is a paragraph, since you never see the prefix, as in "<html:p>This is a new paragraph!</html:p>".

This may seem glaringly obvious, but I was writing a class that converts CSS queries into XPath queries, and the fact that a namespace had been registered was rather buried in the code.

We love the DOM, the DOM is good to us.
Nibinaear 03-Mar-2008 03:15
I've searched the entire web looking for a way to update / modify/ change/ alter the elements of an xml file and found NOTHING!

So here it is, the defninitive way to "Change XML elements with PHP" rather than adding / appending new ones. This uses XPATH:

<?php

// Create a DOMDocument instance
$xml = new DOMDocument;

// Ignore whitespace between nodes (default: true)
$xml->preserveWhiteSpace = false;

$file='about.xml';

// Load the XML data source
$xml->Load($file);

$xpath = new DOMXPath($xml);

$query='/regions/branch';

$entries = $xpath->query($query);

foreach ($entries as $entry)
{
  $entry->firstChild->nodeValue="like this!";
  echo $entry->firstChild->nodeValue;
}

$xml->save($file);

?>
ondrej dot fischer at 4internet dot cz 25-Oct-2007 03:46
Unfortunately PHP's DOM extension doesn't support use of:
<?xml-stylesheet type="text/xsl" ... ?>
processing instruction.
Here is an example, how to implement it using XPath query and extending DOMDocument by a method output().

<?php

// This simple function adds missing direct usage of anonymous instances
// in PHP5's reference model
function a($var) {
   return $var;
}

// Extended DOMDocument class
class MyDOMDocument extends DOMDocument
{

   public function output()
   {
      $stylesheets = array();
      $PIs = a(new DOMXPath($this))
                  ->query('/processing-instruction("xml-stylesheet")');

      foreach($PIs as $PI)
      {
         // This might be implemented cleaner by regular parsing
         // of DOMProcessingInstruction::data property
         if(ereg('type *= *"text/xsl" +href *= *"([^"]+)"', $PI->data, $mem))
         {
            // Here should be verified, that XSL file exists.
            a($stylesheets[] = new DOMDocument())->load($mem[1]);
         }
      }

      if($stylesheets)
      {
         $processor = new XSLTProcessor();
         foreach($stylesheets as $stylesheet)
            $processor->importStylesheet($stylesheet);
         return $processor->transformToDoc($this);
      }
      // If no stylesheet instructions present, return self directly
      else return $this;

   }
}

?>

Usage:

<?php

$document = new MyDOMDocument();
$document->load('my.xml');
echo $document->output()->saveXML();

?>

With following file my.xml:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="my.xsl" ?>
<my-root />

and existing file my.xsl that code will transform the xml file using my.xsl and output the result.
jbarnett at flowershopnetwork dot com 11-Sep-2007 10:46
The order of nodes in the return value is not guaranteed.

When my code was on an old server, the returned DOMNodeList was in document order.  On the new server, the returned DOMNodeList is in a consistent order, but it is not in document order.

PHP passes this function call off to the xmlXPathEvalExpression() function in libxml.  That function in libxml only accepts two arguments - the same two this PHP function accepts.  There must have been a change in the libxml version from the old server to the new server, and that libxml behaves differently.

This would be okay if PHP had a way to compare nodes so I can resort the nodes manually, but there is not.

So, there is no guaranteed way to get an ordered list of nodes like DOM 3 XPath provides.
Hayley Watson 13-Aug-2007 02:43
Note that if your DOMDocument was loaded from HTML, where element and attribute names are case-insensitive, the DOM parser converts them all to lower-case, so your XPath queries will have to as well; '//A/@HREF' won't find anything even if the original HTML contained "<A HREF='example.com'>".
nicolas_rainardNOSPAM at yahoo dot fr 10-Jul-2007 11:04
Please note that what clochix says is valid for *any* document which has a default namespace (as it is the case for XHTML).

This document :

<?xml version="1.0" encoding="UTF-8" ?>

<root xmlns="http://www.exemple.org/namespace">

    <element id="1">
    ...
    </element>

    <element id="2">
    ...
    </element>

</element>

must be accessed this way :

$document = new DOMDocument();
$document->load('document.xml');

$xpath = new DOMXPath($document);
$xpath->registerNameSpace('fakeprefix', 'http://www.exemple.org/namespace');

$elements = $xpath->query('//fakeprefix:element');

Of course, there is no prefix in the original document, but the DOMXPath class *needs* one, whatever it is, if you use a default namespace. It *doesn't work* if you specify an empty prefix like this :

$xpath->registerNameSpace('', 'http://www.exemple.org/namespace');

Hope this help to spare some time...
clochix at clochix dot net 01-Mar-2007 01:05
If you want to perform queries on XHTML documents, you must fix a default namespace:

<?php
$doc = new DOMDocument;
$doc->preserveWhiteSpace = true;
$doc->resolveExternals = true; // for character entities
$doc->load("http://www.w3.org/");
$xpath = new DOMXPath($doc);
// won't work
$entries = $xpath->query("//div");
// you should use :
$xpath->registerNamespace("html", "http://www.w3.org/1999/xhtml");
$entries = $xpath->query("//html:div");
?>
jakob dot voss at nichtich dot de 14-Nov-2005 09:33
You can transform the result nodes into new DOMDocument objects this way:

<?php
$result = $xpath->query($query);
$resultNode = $result->item(0);
$newDom = new DOMDocument;
$newDom->appendChild($newDom->importNode($resultNode,1));

print "<pre>" . htmlspecialchars($newDom->saveXML()) . "</pre>";
?>
Eric Hanson 13-Jul-2005 02:40
Two great XPath references follow.

XPath in Five Paragraphs (finally!):
http://www.rpbourret.com/xml/XPathIn5.htm

The w3c spec actually has a bunch of helpful examples:
http://www.w3.org/TR/xpath#location-paths


Registers the namespace with the DOMXPath object

  • ≪ DOMXPath::query
  • DOMXPath::registerPhpFunctions ≫
  • PHP Manual
  • DOMXPath
  • Registers the namespace with the DOMXPath object

DOMXPath::registerNamespace

(PHP 5, PHP 7)

DOMXPath::registerNamespace — Registers the namespace with the DOMXPath object

설명

public bool DOMXPath::registerNamespace ( string $prefix , string $namespaceURI )

Registers the namespaceURI and prefix with the DOMXPath object.

인수

prefix

The prefix.

namespaceURI

The URI of the namespace.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

igor dot pellegrini at diespam-berlinonline dot de 11-Mar-2015 04:32
The documentation is not really detailed.
Probably someone is also interested in the possibility of unregistering the namespace.

Worths to note that the original libxml function "xmlXPathRegisterNs()"

* Does NOT accept NULL or empty string for the argument "$prefix".

   and

* Unregisters the namespace if the "$namespaceURI" argument is NULL.

Here the documentation:
http://xmlsoft.org/html/libxml-xpathInternals.html#xmlXPathRegisterNs
xmedeko at gmail dot com 20-Sep-2011 01:58
A general way to register the root default namespace:

<?php
$xml = new DomDocument();
$xml->load('data.xml');
$xpath = new DOMXPath($xml);
$rootNamespace = $xml->lookupNamespaceUri($xml->namespaceURI);
$xpath->registerNamespace('x', $rootNamespace);
?>

And then just query:

<?php $elementList = $xpath->query('//x:items/x:name'); ?>
dulao5 at gmail dot com 16-Jun-2009 09:01
The following code can work for XML default namespace.
<?php
$xml = <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:other="http://other.w3.org/other" >
        <id>uYG7-sPwjFg</id>
        <published>2009-05-17T18:29:31.000Z</published>
</entry>
EOT;

$doc = new DOMDocument;
$doc->loadXML($xml);
$xpath =  DOMXPath($doc);

$xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");

$xpath_str = '//atom:entry/atom:published/text()';

$entries = $xpath->evaluate($xpath_str);

print $entries->item(0)->nodeValue ."\n";

?>
agent009 31-Mar-2009 01:55
Ok, here's a function that extends XPath syntax with the following special characters:

~ inserts default namespace prefix if defined

# shorthand for text()

% shorthand for comment()

$ shorthand for node()

?* shorthand for processing-instruction()

?foo shorthand for processing-instruction("foo")

? shorthand for processing-instruction("")

^ escapes following character (with literal or SGML entity as needed)

All of the above except ^ are ignored within quoted strings

<?php
function extendXPath($str, $defns = NULL) {
   $quote = false;
   $map = array(
      '~' => isset($defns) ? "$defns:" : '',
      '#' => 'text()',
      '%' => 'comment()',
      '$' => 'node()'
   );
   $out = '';

   for ($i = 0, $len = strlen($str); $i < $len; $i++) {
      $c = $str[$i];
      if (!$quote && array_key_exists($c, $map)) {
         $out .= $map[$c];
      } else switch ($c) {
         case '^':
            $out .= htmlspecialchars($str[++$i], ENT_QUOTES);
            break;
         case '?':
            if ($quote) {
               $out .= $c;
            } elseif ($str[$i + 1] == '*') {
               $out .= 'processing-instruction()';
               $i++;
            } else {
               preg_match('/^\w+/', substr($str, $i + 1), $matches);
               $out .= 'processing-instruction("'.$matches[0].'")';
               $i += strlen($matches[0]);
            };
            break;
         case '"':
            $quote = !$quote;
         default:
            $out .= $c;
      };
   };

   return $out;
}
?>
cameron kellough 15-Apr-2007 07:33
This is called prefix mapping and it is necessary to use xpath to handle documents which have default namespaces. //root/item will search for items with no namespace, not items with the namespace described as the default in the xmlns declaration.  This problem is maddening as it just looks on the surface like xpath isn't working.
spam at spam dot spam 31-Mar-2005 09:46
It is mentioned in a few places on the web, but it wasn't mentioned here. You need to use this function to set up a prefix for the default namespace of a document.

For instance, if you are trying to parse a Microsoft Spreadsheet XML file, which has the default namespace of "urn:schemas-microsoft-com:office:spreadsheet":

$doc = DOMDocument::load("my_spreadsheet.xml);
$xpath = new DOMXPath($doc);
$xpath->registerNamespace("m",
        "urn:schemas-microsoft-com:office:spreadsheet");
$query = '/m:Workbook/m:Worksheet[1]/m:Table';
$result = $xpath->query($query, $doc);

You can use anything in place of the 'm', but you have to specify something! Just asking for "/Workbook/Worksheet/Table" doesn't work.


Register PHP functions as XPath functions

  • ≪ DOMXPath::registerNamespace
  • DOM 함수 목록 ≫
  • PHP Manual
  • DOMXPath
  • Register PHP functions as XPath functions

DOMXPath::registerPhpFunctions

(PHP 5 >= 5.3.0, PHP 7)

DOMXPath::registerPhpFunctions — Register PHP functions as XPath functions

설명

public void DOMXPath::registerPhpFunctions ([ mixed $restrict ] )

This method enables the ability to use PHP functions within XPath expressions.

인수

restrict

Use this parameter to only allow certain functions to be called from XPath.

This parameter can be either a string (a function name) or an array of function names.

반환값

값을 반환하지 않습니다.

예제

The following examples use book.xml which contains the following:

Example #1 book.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
 <book>
  <title>PHP Basics</title>
  <author>Jim Smith</author>
  <author>Jane Smith</author>
 </book>
 <book>
  <title>PHP Secrets</title>
  <author>Jenny Smythe</author>
 </book>
 <book>
  <title>XML basics</title>
  <author>Joe Black</author>
 </book>
</books>

Example #2 DOMXPath::registerPHPFunctions() with php:functionString

<?php
$doc = new DOMDocument;
$doc->load('book.xml');

$xpath = new DOMXPath($doc);

// Register the php: namespace (required)
$xpath->registerNamespace("php", "http://php.net/xpath");

// Register PHP functions (no restrictions)
$xpath->registerPHPFunctions();

// Call substr function on the book title
$nodes = $xpath->query('//book[php:functionString("substr", title, 0, 3) = "PHP"]');

echo "Found {$nodes->length} books starting with 'PHP':\n";
foreach ($nodes as $node) {
    $title  = $node->getElementsByTagName("title")->item(0)->nodeValue;
    $author = $node->getElementsByTagName("author")->item(0)->nodeValue;
    echo "$title by $author\n";
}

?>

위 예제의 출력 예시:

Found 2 books starting with 'PHP':
PHP Basics by Jim Smith
PHP Secrets by Jenny Smythe

Example #3 DOMXPath::registerPHPFunctions() with php:function

<?php
$doc = new DOMDocument;
$doc->load('book.xml');

$xpath = new DOMXPath($doc);

// Register the php: namespace (required)
$xpath->registerNamespace("php", "http://php.net/xpath");

// Register PHP functions (has_multiple only)
$xpath->registerPHPFunctions("has_multiple");

function has_multiple($nodes) {
    // Return true if more than one author
    return count($nodes) > 1;
}
// Filter books with multiple authors
$books = $xpath->query('//book[php:function("has_multiple", author)]');

echo "Books with multiple authors:\n";
foreach ($books as $book) {
    echo $book->getElementsByTagName("title")->item(0)->nodeValue . "\n";
}

?>

위 예제의 출력 예시:

Books with multiple authors:
PHP Basics

참고

  • DOMXPath::registerNamespace() - Registers the namespace with the DOMXPath object
  • DOMXPath::query() - Evaluates the given XPath expression
  • DOMXPath::evaluate() - Evaluates the given XPath expression and returns a typed result if possible

User Contributed Notes

There are no user contributed notes for this page.


DOM 함수 목록

Table of Contents

  • dom_import_simplexml — Gets a DOMElement object from a SimpleXMLElement object

User Contributed Notes

There are no user contributed notes for this page.


Gets a DOMElement object from a SimpleXMLElement object

  • ≪ DOM 함수 목록
  • libxml ≫
  • PHP Manual
  • DOM 함수 목록
  • Gets a DOMElement object from a SimpleXMLElement object

dom_import_simplexml

(PHP 5, PHP 7)

dom_import_simplexml — Gets a DOMElement object from a SimpleXMLElement object

설명

DOMElement dom_import_simplexml ( SimpleXMLElement $node )

This function takes the node node of class SimpleXML and makes it into a DOMElement node. This new object can then be used as a native DOMElement node.

인수

node

The SimpleXMLElement node.

반환값

The DOMElement node added or FALSE if any errors occur.

예제

Example #1 Import SimpleXML into DOM with dom_import_simplexml()

<?php

$sxe = simplexml_load_string('<books><book><title>blah</title></book></books>');

if ($sxe === false) {
    echo 'Error while parsing the document';
    exit;
}

$dom_sxe = dom_import_simplexml($sxe);
if (!$dom_sxe) {
    echo 'Error while converting XML';
    exit;
}

$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);

echo $dom->saveXML();

?>

참고

  • simplexml_import_dom() - Get a SimpleXMLElement object from a DOM node.

User Contributed Notes

biniou at yopmail dot com 20-May-2016 06:58
Very useful to add a CDATA node with SimpleXMLElement (use it like addChild) :

<?php
class My_SimpleXMLElement extends SimpleXMLElement {

    public function addChildWithCData($name, $value = NULL) {
        $new_child = $this->addChild($name);

        $node = dom_import_simplexml($new_child);
        $no = $node->ownerDocument;
        $node->appendChild($no->createCDATASection($value));

        return $new_child;
    }
}
Jeff M 06-Mar-2009 10:09
SimpleXML is an 'Object Mapping XML API'. It is not DOM, per se. SimpleXML converts the XML elements into PHP's native data types.

The dom_import_simplexml and simplexml_import_dom functions do *not* create separate copies of the original object. You are free to use the methods of either or both interchangeably, since the underlying instance is the same.

<?php
    // initialize a simplexml object
    $sxe = simplexml_load_string('<root/>');

    // get a dom interface on the simplexml object
    $dom = dom_import_simplexml($sxe);

    // dom adds a new element under the root
    $element = $dom->appendChild(new DOMElement('dom_element'));

    // dom adds an attribute on the new element
    $element->setAttribute('creator', 'dom');

    // simplexml adds an attribute on the dom element
    $sxe->dom_element['sxe_attribute'] = 'added by simplexml';

    // simplexml adds a new element under the root
    $element = $sxe->addChild('sxe_element');

    // simplexml adds an attribute on the new element
    $element['creator'] = 'simplexml';

    // dom finds the simplexml element (via DOMNodeList->index)
    $element = $dom->getElementsByTagName('sxe_element')->item(0);

    // dom adds an attribute on the simplexml element
    $element->setAttribute('dom_attribute', 'added by dom');

    echo ('<pre>');
    print_r($sxe);
    echo ('</pre>');
?>

Outputs:

SimpleXMLElement Object
(
    [dom_element] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [creator] => dom
                    [sxe_attribute] => added by simplexml
                )

        )

    [sxe_element] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [creator] => simplexml
                    [dom_attribute] => added by dom
                )

        )

)

What this illustrates is that both interfaces are operating on the same underlying object instance. Also, when you dom_import_simplexml, you can create and add new elements without reference to an ownerDocument (or documentElement).

So passing a SimpleXMLElement to another method does not mean the recipient is limited to using SimpleXML methods.

Hey Presto! Your telescope has become a pair of binoculars!
crescentfreshpot at yahoo dot com 12-Dec-2007 09:22
justinpatrin at php dot net:
> To get a proper DOM document (which you need to do most things) you need...

No you don't. Just do:
<?php
$dom = dom_import_simplexml($xml)->ownerDocument;
?>
justinpatrin at php dot net 02-Jun-2006 06:44
I've found that newer versions of PHP5 require some special syntax in order to properly convert between SimpleXML and DOM. It's not as easy as calling dom_import_simplexml() with a SimpleXML node. To get a proper DOM document (which you need to do most things) you need:
<?php
//$xml is a SimpleXML instance
$domnode = dom_import_simplexml($xml);
$dom = new DOMDocument();
$domnode = $dom->importNode($domnode, true);
$dom->appendChild($domnode);
?>

Switching back, though, is, well...simple.

<?php
//$dom is a DOMDocument instance
$xml = simplexml_import_dom($dom);
?>


libxml

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • libXMLError — The libXMLError class
  • libxml 함수 목록
    • libxml_clear_errors — Clear libxml error buffer
    • libxml_disable_entity_loader — Disable the ability to load external entities
    • libxml_get_errors — Retrieve array of errors
    • libxml_get_last_error — Retrieve last error from libxml
    • libxml_set_external_entity_loader — Changes the default external entity loader
    • libxml_set_streams_context — Set the streams context for the next libxml document load or write
    • libxml_use_internal_errors — Disable libxml errors and allow user to fetch error information as needed

User Contributed Notes

There are no user contributed notes for this page.


소개

These functions/constants are available as of PHP 5.1.0, and the following core extensions rely on this libxml extension: DOM, libxml, SimpleXML, SOAP, WDDX, XSL, XML, XMLReader, XMLRPC 그리고 XMLWriter.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

This extension requires » libxml >= 2.6.0.

User Contributed Notes

There are no user contributed notes for this page.


설치

The libxml extension is enabled by default, although it may be disabled with --disable-libxml .

The optional --with-libxml-dir directive is used to specify the location of libxml on the system that PHP is being compiled on, otherwise only the default locations are scanned. The configure process checks for libxml (specifically, xml2-config) in the following order:

  1. The location ([DIR]) specified with --with-libxml-dir ([DIR]=/bin/xml2-config)

  2. /usr/local/bin/xml2-config

  3. /usr/bin/xml2-config

If configure cannot find xml2-config in the directory specified by --with-libxml-dir , then it'll continue on and check the default locations.

User Contributed Notes

fred5 at no dot spam dot co dot za 01-Aug-2014 07:29
(I tried to enter a bug but the PHP version selection drop down isn't working so I have entered here)

This documentation is very confusing as it refers to xml2-config as a directory and file interchangeably (it is a file) ...so I'm not clear whether to use the containing directory or the actual file in the switch


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

LIBXML_BIGLINES (integer)
Allows line numbers greater than 65535 to be reported correctly.

Note:

Only available as of PHP 7.0.0 with Libxml >= 2.9.0

LIBXML_COMPACT (integer)
Activate small nodes allocation optimization. This may speed up your application without needing to change the code.

Note:

Only available in Libxml >= 2.6.21

LIBXML_DTDATTR (integer)
Default DTD attributes
LIBXML_DTDLOAD (integer)
Load the external subset
LIBXML_DTDVALID (integer)
Validate with the DTD
LIBXML_HTML_NOIMPLIED (integer)
Sets HTML_PARSE_NOIMPLIED flag, which turns off the automatic adding of implied html/body... elements.

Note:

Only available in Libxml >= 2.7.7 (as of PHP >= 5.4.0)

LIBXML_HTML_NODEFDTD (integer)
Sets HTML_PARSE_NODEFDTD flag, which prevents a default doctype being added when one is not found.

Note:

Only available in Libxml >= 2.7.8 (as of PHP >= 5.4.0)

LIBXML_NOBLANKS (integer)
Remove blank nodes
LIBXML_NOCDATA (integer)
Merge CDATA as text nodes
LIBXML_NOEMPTYTAG (integer)
Expand empty tags (e.g. <br/> to <br></br>)

Note:

This option is currently just available in the DOMDocument::save and DOMDocument::saveXML functions.

LIBXML_NOENT (integer)
Substitute entities
LIBXML_NOERROR (integer)
Suppress error reports
LIBXML_NONET (integer)
Disable network access when loading documents
LIBXML_NOWARNING (integer)
Suppress warning reports
LIBXML_NOXMLDECL (integer)
Drop the XML declaration when saving a document

Note:

Only available in Libxml >= 2.6.21

LIBXML_NSCLEAN (integer)
Remove redundant namespace declarations
LIBXML_PARSEHUGE (integer)
Sets XML_PARSE_HUGE flag, which relaxes any hardcoded limit from the parser. This affects limits like maximum depth of a document or the entity recursion, as well as limits of the size of text nodes.

Note:

Only available in Libxml >= 2.7.0 (as of PHP >= 5.3.2 and PHP >= 5.2.12)

LIBXML_PEDANTIC (integer)
Sets XML_PARSE_PEDANTIC flag, which enables pedantic error reporting.

Note:

Available as of PHP >= 5.4.0

LIBXML_XINCLUDE (integer)
Implement XInclude substitution
LIBXML_ERR_ERROR (integer)
A recoverable error
LIBXML_ERR_FATAL (integer)
A fatal error
LIBXML_ERR_NONE (integer)
No errors
LIBXML_ERR_WARNING (integer)
A simple warning
LIBXML_VERSION (integer)
libxml version like 20605 or 20617
LIBXML_DOTTED_VERSION (string)
libxml version like 2.6.5 or 2.6.17
LIBXML_SCHEMA_CREATE (integer)
Create default/fixed value nodes during XSD schema validation

Note:

Only available in Libxml >= 2.6.14 (as of PHP >= 5.5.2)

User Contributed Notes

@oneseventeen 06-Feb-2011 08:51
When inserting XML DOM Elements inside existing XML DOM Elements that I loaded from an XML file using the following code, none of my new elements were formatted correctly, they just showed up on one line:

<?php
$dom = DOMDocument::load('file.xml');
$dom->formatOutput = true;
//$dom->add some new elements with child nodes somewhere inside the loaded XML using insertBefore();
$dom->saveXML();
//output: everything looks normal but the new nodes are all on one line.
?>

I found I could pass LIBXML_NOBLANKS to the load method and it would reformat the whole document, including my added stuff:
<?php
$dom = DOMDocument::load('file.xml', LIBXML_NOBLANKS);
$dom->formatOutput = true;
//$dom->add some new elements with child nodes somewhere inside the loaded XML using insertBefore();
$dom->saveXML();
//output: everything looks newly formatted, including new nodes
?>

Hope this helps, took me hours of trial and error to figure this out!
zachatwork at gmail dot com 10-Feb-2010 08:30
Note: The LIBXML_NOXMLDECL constant is defined in this library but is not supported by DOMDocument (yet).

See also: http://bugs.php.net/bug.php?id=47137

<?php

print "PHP_VERSION:      ".PHP_VERSION."\n";
print "LIBXML_VERSION:   ".LIBXML_VERSION."\n";
print "LIBXML_NOXMLDECL: ".LIBXML_NOXMLDECL."\n";

$dom = new DomDocument();
$dom->loadXML("<foo />");

# This should work but doesn't.

print "DOMDocument doesn't honor LIBXML_NOXMLDECL:\n";
print $dom->saveXML(null,LIBXML_NOXMLDECL);

# This works, and will still work after the above is fixed.

print "Forwards compatible workaround:\n";
$lines = explode("\n", $dom->saveXML(null, LIBXML_NOXMLDECL), 2);
if(!preg_match('/^\<\?xml/', $lines[0]))
    print $lines[0];
print $lines[1];

?>

PHP_VERSION:      5.3.1-0.dotdeb.1
LIBXML_VERSION:   20632
LIBXML_NOXMLDECL: 2
DOMDocument doesn't honor LIBXML_NOXMLDECL:
<?xml version="1.0"?>
<foo/>
Forwards compatible workaround:
<foo/>


The libXMLError class

(PHP 5 >= 5.1.0, PHP 7)

소개

Contains various information about errors thrown by libxml. The error codes are described within the official » xmlError API documentation.

클래스 개요

libXMLError {
/* 프로퍼티 */
public int $level ;
public int $code ;
public int $column ;
public string $message ;
public string $file ;
public int $line ;
}

프로퍼티

level

the severity of the error (one of the following constants: LIBXML_ERR_WARNING, LIBXML_ERR_ERROR or LIBXML_ERR_FATAL)

code

The error's code.

column

The column where the error occurred.

Note:

This property isn't entirely implemented in libxml and therefore 0 is often returned.

message

The error message, if any.

file

The filename, or empty if the XML was loaded from a string.

line

The line where the error occurred.

User Contributed Notes

There are no user contributed notes for this page.


libxml 함수 목록

Table of Contents

  • libxml_clear_errors — Clear libxml error buffer
  • libxml_disable_entity_loader — Disable the ability to load external entities
  • libxml_get_errors — Retrieve array of errors
  • libxml_get_last_error — Retrieve last error from libxml
  • libxml_set_external_entity_loader — Changes the default external entity loader
  • libxml_set_streams_context — Set the streams context for the next libxml document load or write
  • libxml_use_internal_errors — Disable libxml errors and allow user to fetch error information as needed

User Contributed Notes

There are no user contributed notes for this page.


Clear libxml error buffer

  • ≪ libxml 함수 목록
  • libxml_disable_entity_loader ≫
  • PHP Manual
  • libxml 함수 목록
  • Clear libxml error buffer

libxml_clear_errors

(PHP 5 >= 5.1.0, PHP 7)

libxml_clear_errors — Clear libxml error buffer

설명

void libxml_clear_errors ( void )

libxml_clear_errors() clears the libxml error buffer.

반환값

값을 반환하지 않습니다.

참고

  • libxml_get_errors() - Retrieve array of errors
  • libxml_get_last_error() - Retrieve last error from libxml

User Contributed Notes

There are no user contributed notes for this page.


Disable the ability to load external entities

  • ≪ libxml_clear_errors
  • libxml_get_errors ≫
  • PHP Manual
  • libxml 함수 목록
  • Disable the ability to load external entities

libxml_disable_entity_loader

(PHP 5 >= 5.2.11, PHP 7)

libxml_disable_entity_loader — Disable the ability to load external entities

설명

bool libxml_disable_entity_loader ([ bool $disable = true ] )

Disable/enable the ability to load external entities.

인수

disable

Disable (TRUE) or enable (FALSE) libxml extensions (such as DOM, XMLWriter and XMLReader) to load external entities.

반환값

Returns the previous value.

참고

  • libxml_use_internal_errors() - Disable libxml errors and allow user to fetch error information as needed
  • The LIBXML_NONET constant

User Contributed Notes

brendan at bloodbone dot ws 25-Mar-2014 07:23
This also seems to have an impact on <xsl:import /> statements if this is applied when loading XSLT for the XSLTProcessor class.
phofstetter at sensational dot ch 28-Jan-2014 01:36
Be mindful that this also disables url loading in simplexml_load_file() and likely other libxml based functions that deal with URLs
daschtour at me dot com 25-Dec-2013 08:56
This function was reported to be not thread safe. So this might affect php-scripts on the same server.
simonsimcity 01-Mar-2012 03:23
Using this function you can prevent a vulnerable to Local and Remote File Inclusion attacks.

You'll see it in an example where I load and validate the following string:

<!DOCTYPE scan [<!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource=/etc/passwd">]>
<scan>&test;</scan>

One way to prevent that the file in given back is to set this value to 0.
Please take a closer look at the release of symfony 2.0.11


Retrieve array of errors

  • ≪ libxml_disable_entity_loader
  • libxml_get_last_error ≫
  • PHP Manual
  • libxml 함수 목록
  • Retrieve array of errors

libxml_get_errors

(PHP 5 >= 5.1.0, PHP 7)

libxml_get_errors — Retrieve array of errors

설명

array libxml_get_errors ( void )

Retrieve array of errors.

반환값

Returns an array with LibXMLError objects if there are any errors in the buffer, or an empty array otherwise.

예제

Example #1 A libxml_get_errors() example

This example demonstrates how to build a simple libxml error handler.

<?php

libxml_use_internal_errors(true);

$xmlstr = <<< XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <titles>PHP: Behind the Parser</title>
 </movie>
</movies>
XML;

$doc = simplexml_load_string($xmlstr);
$xml = explode("\n", $xmlstr);

if ($doc === false) {
    $errors = libxml_get_errors();

    foreach ($errors as $error) {
        echo display_xml_error($error, $xml);
    }

    libxml_clear_errors();
}

function display_xml_error($error, $xml)
{
    $return  = $xml[$error->line - 1] . "\n";
    $return .= str_repeat('-', $error->column) . "^\n";

    switch ($error->level) {
        case LIBXML_ERR_WARNING:
            $return .= "Warning $error->code: ";
            break;
         case LIBXML_ERR_ERROR:
            $return .= "Error $error->code: ";
            break;
        case LIBXML_ERR_FATAL:
            $return .= "Fatal Error $error->code: ";
            break;
    }

    $return .= trim($error->message) .
               "\n  Line: $error->line" .
               "\n  Column: $error->column";

    if ($error->file) {
        $return .= "\n  File: $error->file";
    }

    return "$return\n\n--------------------------------------------\n\n";
}

?>

위 예제의 출력:

  <titles>PHP: Behind the Parser</title>
----------------------------------------------^
Fatal Error 76: Opening and ending tag mismatch: titles line 4 and title
  Line: 4
  Column: 46

--------------------------------------------

참고

  • libxml_get_last_error() - Retrieve last error from libxml
  • libxml_clear_errors() - Clear libxml error buffer

User Contributed Notes

There are no user contributed notes for this page.


Retrieve last error from libxml

  • ≪ libxml_get_errors
  • libxml_set_external_entity_loader ≫
  • PHP Manual
  • libxml 함수 목록
  • Retrieve last error from libxml

libxml_get_last_error

(PHP 5 >= 5.1.0, PHP 7)

libxml_get_last_error — Retrieve last error from libxml

설명

LibXMLError libxml_get_last_error ( void )

Retrieve last error from libxml.

반환값

Returns a LibXMLError object if there is any error in the buffer, FALSE otherwise.

참고

  • libxml_get_errors() - Retrieve array of errors
  • libxml_clear_errors() - Clear libxml error buffer

User Contributed Notes

There are no user contributed notes for this page.


Changes the default external entity loader

  • ≪ libxml_get_last_error
  • libxml_set_streams_context ≫
  • PHP Manual
  • libxml 함수 목록
  • Changes the default external entity loader

libxml_set_external_entity_loader

(PHP 5 >= 5.4.0, PHP 7)

libxml_set_external_entity_loader — Changes the default external entity loader

설명

void libxml_set_external_entity_loader ( callable $resolver_function )

Changes the default external entity loader.

인수

resolver_function

A callable that takes three arguments. Two strings, a public id and system id, and a context (an array with four keys) as the third argument. This callback should return a resource, a string from which a resource can be opened, or NULL.

반환값

값을 반환하지 않습니다.

예제

Example #1 libxml_set_external_entity_loader() example

<?php
$xml = <<<XML
<!DOCTYPE foo PUBLIC "-//FOO/BAR" "http://example.com/foobar">
<foo>bar</foo>
XML;

$dtd = <<<DTD
<!ELEMENT foo (#PCDATA)>
DTD;

libxml_set_external_entity_loader(
    function ($public, $system, $context) use($dtd) {
        var_dump($public);
        var_dump($system);
        var_dump($context);
        $f = fopen("php://temp", "r+");
        fwrite($f, $dtd);
        rewind($f);
        return $f;
    }
);

$dd = new DOMDocument;
$r  = $dd->loadXML($xml);

var_dump($dd->validate());
?>

위 예제의 출력:

string(10) "-//FOO/BAR"
string(25) "http://example.com/foobar"
array(4) {
    ["directory"]    => NULL
    ["intSubName"]   => NULL
    ["extSubURI"]    => NULL
    ["extSubSystem"] => NULL
}
bool(true)

참고

  • libxml_disable_entity_loader() - Disable the ability to load external entities

User Contributed Notes

There are no user contributed notes for this page.


Set the streams context for the next libxml document load or write

  • ≪ libxml_set_external_entity_loader
  • libxml_use_internal_errors ≫
  • PHP Manual
  • libxml 함수 목록
  • Set the streams context for the next libxml document load or write

libxml_set_streams_context

(PHP 5, PHP 7)

libxml_set_streams_context — Set the streams context for the next libxml document load or write

설명

void libxml_set_streams_context ( resource $streams_context )

Sets the streams context for the next libxml document load or write.

인수

streams_context

The stream context resource (created with stream_context_create())

반환값

값을 반환하지 않습니다.

예제

Example #1 A libxml_set_streams_context() example

<?php

$opts = array(
    'http' => array(
        'user_agent' => 'PHP libxml agent',
    )
);

$context = stream_context_create($opts);
libxml_set_streams_context($context);

// request a file through HTTP
$doc = DOMDocument::load('http://www.example.com/file.xml');

?>

참고

  • stream_context_create() - Creates a stream context

User Contributed Notes

There are no user contributed notes for this page.


Disable libxml errors and allow user to fetch error information as needed

  • ≪ libxml_set_streams_context
  • SDO ≫
  • PHP Manual
  • libxml 함수 목록
  • Disable libxml errors and allow user to fetch error information as needed

libxml_use_internal_errors

(PHP 5 >= 5.1.0, PHP 7)

libxml_use_internal_errors — Disable libxml errors and allow user to fetch error information as needed

설명

bool libxml_use_internal_errors ([ bool $use_errors = false ] )

libxml_use_internal_errors() allows you to disable standard libxml errors and enable user error handling.

인수

use_errors

Enable (TRUE) user error handling or disable (FALSE) user error handling. Disabling will also clear any existing libxml errors.

반환값

This function returns the previous value of use_errors.

예제

Example #1 A libxml_use_internal_errors() example

This example demonstrates the basic usage of libxml errors and the value returned by this function.

<?php

// enable user error handling
var_dump(libxml_use_internal_errors(true));

// load the document
$doc = new DOMDocument;

if (!$doc->load('file.xml')) {
    foreach (libxml_get_errors() as $error) {
        // handle errors here
    }

    libxml_clear_errors();
}

?>

위 예제의 출력:

bool(false)

참고

  • libxml_clear_errors() - Clear libxml error buffer
  • libxml_get_errors() - Retrieve array of errors

User Contributed Notes

marcovtwout 07-May-2013 10:05
When using libxml_use_internal_errors(true), and perform a possibly memory intensive operation like simplexml_load_string() next, you might end up with a blank screen and no php error logged because the error message been suppressed.
james at dunmore dot me dot uk 21-Aug-2008 12:03
We use a custom error handler using  set_error_handler()  I found that after I call

libxml_use_internal_errors(false);

It resets my error_handler - not sure if this is a bug or not, I will report it as a bug - however, I needed to repeat my call to set_error_handler

(did confuse me for a while)
B Crawford 04-Oct-2007 03:22
When using this funtion, be sure to clear your internal error buffer. If you dn't and you are using this in a long running process, you may find that all your memory is used up.


Service Data Objects

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • Limitations
  • 예제
    • Basic usage
    • Setting and Getting Property Values
    • Working with Sequenced Data Objects
    • Reflecting on Service Data Objects
  • SDO 함수 목록
    • SDO_DAS_ChangeSummary::beginLogging — Begin change logging
    • SDO_DAS_ChangeSummary::endLogging — End change logging
    • SDO_DAS_ChangeSummary::getChangeType — Get the type of change made to an SDO_DataObject
    • SDO_DAS_ChangeSummary::getChangedDataObjects — Get the changed data objects from a change summary
    • SDO_DAS_ChangeSummary::getOldContainer — Get the old container for a deleted SDO_DataObject
    • SDO_DAS_ChangeSummary::getOldValues — Get the old values for a given changed SDO_DataObject
    • SDO_DAS_ChangeSummary::isLogging — Test to see whether change logging is switched on
    • SDO_DAS_DataFactory::addPropertyToType — Adds a property to a type
    • SDO_DAS_DataFactory::addType — Add a new type to a model
    • SDO_DAS_DataFactory::getDataFactory — Get a data factory instance
    • SDO_DAS_DataObject::getChangeSummary — Get a data object's change summary
    • SDO_DAS_Setting::getListIndex — Get the list index for a changed many-valued property
    • SDO_DAS_Setting::getPropertyIndex — Get the property index for a changed property
    • SDO_DAS_Setting::getPropertyName — Get the property name for a changed property
    • SDO_DAS_Setting::getValue — Get the old value for the changed property
    • SDO_DAS_Setting::isSet — Test whether a property was set prior to being modified
    • SDO_DataFactory::create — Create an SDO_DataObject
    • SDO_DataObject::clear — Clear an SDO_DataObject's properties
    • SDO_DataObject::createDataObject — Create a child SDO_DataObject
    • SDO_DataObject::getContainer — Get a data object's container
    • SDO_DataObject::getSequence — Get the sequence for a data object
    • SDO_DataObject::getTypeName — Return the name of the type for a data object.
    • SDO_DataObject::getTypeNamespaceURI — Return the namespace URI of the type for a data object.
    • SDO_Exception::getCause — Get the cause of the exception.
    • SDO_List::insert — Insert into a list
    • SDO_Model_Property::getContainingType — Get the SDO_Model_Type which contains this property
    • SDO_Model_Property::getDefault — Get the default value for the property
    • SDO_Model_Property::getName — Get the name of the SDO_Model_Property
    • SDO_Model_Property::getType — Get the SDO_Model_Type of the property
    • SDO_Model_Property::isContainment — Test to see if the property defines a containment relationship
    • SDO_Model_Property::isMany — Test to see if the property is many-valued
    • SDO_Model_ReflectionDataObject::__construct — Construct an SDO_Model_ReflectionDataObject
    • SDO_Model_ReflectionDataObject::export — Get a string describing the SDO_DataObject.
    • SDO_Model_ReflectionDataObject::getContainmentProperty — Get the property which defines the containment relationship to the data object
    • SDO_Model_ReflectionDataObject::getInstanceProperties — Get the instance properties of the SDO_DataObject
    • SDO_Model_ReflectionDataObject::getType — Get the SDO_Model_Type for the SDO_DataObject
    • SDO_Model_Type::getBaseType — Get the base type for this type
    • SDO_Model_Type::getName — Get the name of the type
    • SDO_Model_Type::getNamespaceURI — Get the namespace URI of the type
    • SDO_Model_Type::getProperties — Get the SDO_Model_Property objects defined for the type
    • SDO_Model_Type::getProperty — Get an SDO_Model_Property of the type
    • SDO_Model_Type::isAbstractType — Test to see if this SDO_Model_Type is an abstract data type
    • SDO_Model_Type::isDataType — Test to see if this SDO_Model_Type is a primitive data type
    • SDO_Model_Type::isInstance — Test for an SDO_DataObject being an instance of this SDO_Model_Type
    • SDO_Model_Type::isOpenType — Test to see if this type is an open type
    • SDO_Model_Type::isSequencedType — Test to see if this is a sequenced type
    • SDO_Sequence::getProperty — Return the property for the specified sequence index.
    • SDO_Sequence::insert — Insert into a sequence
    • SDO_Sequence::move — Move an item to another sequence position

User Contributed Notes

There are no user contributed notes for this page.


소개

Service Data Objects (SDOs) enable PHP applications to work with data from different sources (like a database query, an XML file, and a spreadsheet) using a single interface.

Each different kind of data source requires a Data Access Service (DAS) to provide access to the data in the data source. In your PHP application, you use a DAS to create an SDO instance that represents some data in the data source. You can then set and get values in the SDO instance using the standard SDO interface. Finally, you use a DAS to write the modified data back to a data source, typically the same one.

See the list of Data Access Services for details on those currently available. In addition to the provided DASs, SDO also provides interfaces to enable others to be implemented (see the section on SDO Data Access Services Interface for more details).

This extension is derived from concepts taken from the » Service Data Objects specification. It includes a version of the » Apache Tuscany SDO for C++ project.

The Structure of a Service Data Object

A Service Data Object instance is made up of a tree of data objects. The tree is defined by containment relationships between the data objects. For example, a Company data object might consist of a number of Department data objects and therefore the Company would have a containment relationship to the Departments.

An SDO may also have non-containment references between data objects in the tree. For example, one Employee data object might reference another Employee to identify a career mentor.

As well as data objects referencing each other, they can also have primitive properties. For example, the Company data object might have a property called "name" of type string, for holding the name of the company (for example, "Acme").

Each of these properties of a data object - containment relationships, non-containment references, or primitive properties - may be many-valued or single-valued. In the above examples, Departments is many-valued and the Company name is single-valued.

In PHP, each SDO data object is represented as a PHP object. The properties of the data object can be accessed using either object syntax or associative array syntax. We'll see some examples of this later.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

The SDO extension requires PHP 5.1.0 or higher. It also requires the libxml2 library. Normally libxml2 will already be installed, but if not, it can be downloaded from » http://www.xmlsoft.org/.

User Contributed Notes

pallsopp at gmail dot com 27-Aug-2012 02:47
That is not strictly true!

Make these changes and it will compile just fine.

If using with PHP >= 5.3.0, you need to make the following source code
changes:

remove all "static" except "static char rcs_id[] ..." from sdo.cpp.

Add #include <stdio.h> to commonj/sdo/SDODataConverter.cpp

/usr/local/lib/php/SCA/SCA_CommentReader.php
  Line 381: $targetLine = preg_replace("{[ \t]+}", " ", $targetLine);
  Line 385: if (strpos($word,$bindingAnnotation) === 0) {
  Line 592: $targetLine = preg_replace("{[ \t]+}", " ", $targetLine);

/usr/local/lib/php/SCA/SCA_AnnotationRules.php
  Line 222:  $line         = preg_replace("{[ \t]+}", " ", $line);

I did exactly that on a 64bit CentOS box and it works fine.
jerrywilborn at gmail dot com 21-Apr-2011 06:57
The extension does not compile against PHP 5.3.  Additionally, it uses SoapServer::setObject() which has only been available since PHP 5.2.


설치

PECL 확장 설치에 관한 정보는 매뉴얼의 PECL 확장 설치에 있습니다. 새 릴리즈, 내려받기, 소스파일, 개발자 정보, CHANGELOG 등의 추가 정보는 여기에 있습니다: » http://pecl.php.net/package/sca_sdo.

Note:

Earlier versions of the SDO extension required a separate shared library for the XML DAS. This is now obsolete and any references to php_sdo_das_xml.dll or sdo_das_xml.so should be removed from your php.ini.

Unix systems

  1. The three SDO components - the SDO core, the XML DAS and the Relational DAS - are packaged together with Service Component Architecture (SCA) into one PECL project, SCA_SDO, so you can download SCA and all three parts of SDO with the command:

    pecl install SCA_SDO

    This command will build the SDO shared library as well as installing the PHP files that make up SCA and the SDO Relational DAS.

    If you want to use the latest beta version, then instead run:

    pecl install SCA_SDO-beta

  2. The pecl command automatically installs the SDO module into your PHP extensions directory. To enable the SDO extension you must add the following line to php.ini:

    extension=sdo.so

    For more information about building PECL packages, consult the PECL installation section of the manual.

Building SDO on Linux

This section describes how to build the SDO core and XML DAS on Linux. You would only need to know how to do this if you wish to build a recent version that you have checked out of SVN.

  1. Change to the main extension directory: cd < wherever your sdo code is >

  2. Run phpize, which will set up the environment to compile SDO.

  3. Next, run ./configure; make; make install. Please note, you may need to login as root to install the extension.

  4. Make sure that the module is loaded by PHP, by adding extension=sdo.so to your php.ini file.

User Contributed Notes

Trouble with sdo 19-Sep-2012 07:46
It is a shame they haven't upgraded this package. However, there is a update on github so that it will compile on php 5.3. Unfortunately it still doesn't compile on a Mac, because is uses a preprocessor macro (__WORDSIZE) which is not defined. I just assumed it to be 32b, and edited the code accordingly (using CloCkWeRX's branch).
arvenil at klecza dot pl 04-Sep-2012 04:34
This extension doesn't compile anymore https://bugs.php.net/bug.php?id=58951


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

SDO_DAS_ChangeSummary::NONE=0 (integer)
Represents a change type of 'none'.
SDO_DAS_ChangeSummary::MODIFICATION=1 (integer)
Represents a change type of 'modification'.
SDO_DAS_ChangeSummary::ADDITION=2 (integer)
Represents a change type of 'addition'.
SDO_DAS_ChangeSummary::DELETION=3 (integer)
Represents a change type of 'deletion'.

User Contributed Notes

There are no user contributed notes for this page.


Limitations

Implementation Limitations

The following are limitations in the current SDO implementation:

  1. There is no support for multi-byte character sets. This will be considered, depending on community requirements, in a Unicode-enabled version of PHP.

SDO Limitations

The following SDO 2.0 concepts are not supported in the current PHP implementation. It is not necessarily the case that these will all be added over time. Their inclusion will depend on community requirements.

  1. Bi-directional relationships.

  2. Type and property alias names.

  3. Read-only properties.

  4. The Helper classes defined in SDO 2.0 are not directly implemented. However equivalent function is provided in a more natural way for PHP. For example the function of CopyHelper::copy() is provided by applying the PHP clone keyword to a data object.

User Contributed Notes

There are no user contributed notes for this page.


예제

Table of Contents

  • Basic usage
  • Setting and Getting Property Values
  • Working with Sequenced Data Objects
  • Reflecting on Service Data Objects

User Contributed Notes

There are no user contributed notes for this page.


Basic usage

The examples below assume an SDO created with the schema and instance information shown below, using the XML Data Access Service.

The instance document below describes a single company, called 'MegaCorp', which contains a single department, called 'Advanced Technologies'. The Advanced Technologies department contains three employees. The company employeeOfTheMonth is referencing the second employee, 'Jane Doe'.

<?xml version="1.0" encoding="UTF-8" ?>
<company xmlns="companyNS" name="MegaCorp"
  employeeOfTheMonth="E0003">
  <departments name="Advanced Technologies" location="NY" number="123">
    <employees name="John Jones" SN="E0001"/>
    <employees name="Jane Doe" SN="E0003"/>
    <employees name="Al Smith" SN="E0004" manager="true"/>
  </departments>
</company>

The root element of the schema is a company. The company contains departments, and each department contains employees. Each element has a number of attributes to store things like name, serial number, and so on. Finally, the company also has an IDREF attribute which identifies one of the employees as the 'employeeOfTheMonth'.

<xsd:schema
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:sdo="commonj.sdo"
  xmlns:sdoxml="commonj.sdo/xml"
  xmlns:company="companyNS"
  targetNamespace="companyNS">
  <xsd:element name="company" type="company:CompanyType"/>
  <xsd:complexType name="CompanyType">
    <xsd:sequence>
      <xsd:element name="departments" type="company:DepartmentType"
        maxOccurs="unbounded"/>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:string"/>
    <xsd:attribute name="employeeOfTheMonth" type="xsd:IDREF"
      sdoxml:propertyType="company:EmployeeType"/>
  </xsd:complexType>
  <xsd:complexType name="DepartmentType">
    <xsd:sequence>
      <xsd:element name="employees" type="company:EmployeeType"
        maxOccurs="unbounded"/>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:string"/>
    <xsd:attribute name="location" type="xsd:string"/>
    <xsd:attribute name="number" type="xsd:int"/>
  </xsd:complexType>
  <xsd:complexType name="EmployeeType">
    <xsd:attribute name="name" type="xsd:string"/>
    <xsd:attribute name="SN" type="xsd:ID"/>
    <xsd:attribute name="manager" type="xsd:boolean"/>
  </xsd:complexType>
</xsd:schema>

The XML Data Access Service maps the schema to an SDO. Attributes such as "name" become primitive properties, the sequence of employees becomes a many-valued containment relationship, and so on. Note that the containment relationships are expressed as one complex type within another, whereas non-containment references are expressed in terms of ID and IDREF, with a special sdoxml:propertyType attribute specifying the type of the non-containment reference.

User Contributed Notes

There are no user contributed notes for this page.


Setting and Getting Property Values

The following examples assume $company is the root of a tree of data objects created from the schema and instance document shown above.

Example #1 Access via property name

Data object properties can be accessed using the object property access syntax. The following sets the company name to 'Acme'.

<?php
$company->name = 'Acme';
?>

Example #2 Access via property name as array index

We can also access properties using associative array syntax. The simplest form of this uses the property name as the array index. For example, the following sets the company name and gets the employeeOfTheMonth.

<?php
$company['name'] = 'UltraCorp';
$eotm = $company['employeeOfTheMonth'];
?>

Example #3 Data Object iteration

We can iterate over the properties of a data object using foreach. The following iterates over the properties of the employee of the month.

<?php
  $eotm = $company->employeeOfTheMonth;
  foreach ($eotm as $name => $value) {
      echo "$name: $value\n";
  }
?>

which will output:

name: Jane Doe
SN: E0003

The 'manager' property is not output, because it has not been set.

Example #4 Access many-valued property by name

Many-valued data object properties can also be accessed using the object property name syntax. The following gets the list of departments.

<?php
$departments = $company->departments;
?>

Example #5 Many-valued element access

We can access individual elements of many-valued properties using array syntax. The following accesses the first department in the company.

<?php
$ad_tech_dept = $company->departments[0];
?>

Example #6 Many-valued property iteration

Many-valued properties can also be iterated over using foreach. The following iterates over the company's departments.

<?php
  foreach ($company->departments as $department) {
    // ...
  }
?>

Each iteration will assign the next department in the list to the variable $department.

Example #7 Chained property access

We can chain property references on a single line. The following sets and gets the name of the first department.

<?php
  $company->departments[0]->name = 'Emerging Technologies';
  $dept_name = $company->departments[0]->name;
?>

Using the associative array syntax, this is equivalent to

<?php
  $company['departments'][0]['name'] = 'Emerging Technologies';
  $dept_name = $company['departments'][0]['name'];
?>

In either case, the dept_name variable is set to 'Emerging Technologies'.

Example #8 XPath navigation

The associative array index can be an XPath-like expression. Valid expressions are defined by an augmented sub-set of XPath.

Two forms of indexing into many-valued properties are supported. The first is the standard XPath array syntax with the indexing starting at one, the second is an SDO extension to XPath with an index starting at zero. The standard syntax is:

<?php
  $jane_doe = $company["departments[1]/employees[2]"];
?>

and the SDO XPath extension syntax is:

<?php
  $jane_doe = $company["departments.0/employees.1"];
?>

Both these examples get the second employee from the first department.

Example #9 XPath querying

We can use XPath to query and identify parts of a data object based on instance data. The following retrieves the manager from the 'Advanced Technologies' department.

<?php
 $ad_tech_mgr =
  $company["departments[name='Advanced Technologies']/employees[manager=true]"];
?>

Example #10 Creating child data objects

A data object can be a factory for its child data objects. A child data object is automatically part of the data graph. The following add a new employee to the 'Advanced Technologies' department.

<?php
  $ad_tech_dept = $company["departments[name='Advanced Technologies']"];
  $new_hire = $ad_tech_dept->createDataObject('employees');
  $new_hire->name = 'John Johnson';
  $new_hire->SN = 'E0005';
  $new_hire->manager = false;
?>

Example #11 Unset a primitive property

We can use the isset() and unset() functions to test and remove items from the data object.

The following clears the name of the first department.

<?php
  unset($company->departments[0]->name);
?>

Example #12 Unset a data object

unset can also be used to remove a data object from the tree. The following example shows John Jones leaving the company.

<?php
  unset($company->departments[0]->employees[0]);
?>

Example #13 Unset a referenced data object

The following removes the 'employeeOfTheMonth' from the company. If this were a containment relationship then the employee would be removed from the company (probably not a good idea to sack your best employee each month!), but since this is a non-containment reference, the employee being referenced will remain in the department in the company, but will no longer be accessible via the employeeOfTheMonth property.

<?php
  if (isset($company->employeeOfTheMonth)) {
    unset($company->employeeOfTheMonth);
  }
?>

Example #14 Access via property index

Data object properties can be accessed via their property index using array syntax. The property index is the position at which the property's definition appears in the model (in this case the xml schema). We can see from the schema listing above that the company name attribute is the second company property (the SDO interface makes no distinction between XML attributes and elements). The following sets the company name to 'Acme', with the same result as Access via property name

<?php
  $company[1] = 'Acme';
?>

Using the index directly in this way is likely to be fragile. Normally the property name syntax should be preferred, but the property index may be required in special cases.

User Contributed Notes

Malcolm Murphy 27-Aug-2016 02:05
You can also use curly brackets to evaluate property names (e.g., to access a property name containing special [non-standard] characters without first needing to assign the name to a variable).

<?php

$object = new StdClass();

// standard access using variable
$nonStandardPropertyName = '/a/path?for=example';
$object->$nonStandardPropertyName = 'access using a variable';

// alternative access using curly braces
$object->{'/a/path?for=example'} = 'set using curly braces';
lists at lnx-bsp dot net 16-Apr-2015 09:20
See also: $this pseudo-variable, http://php.net/manual/en/language.oop5.basic.php

In addition, when inside a class method function, indirect access to object properties also works:

<?php
    $name = 'departments';
    $departments = $this->$name;
    $this->$name = $different;
?>


Working with Sequenced Data Objects

Sequenced data objects are SDOs which can track property ordering across the properties of a data object. They can also contain unstructured text elements (text element which do not belong to any of the SDO's properties). Sequenced data objects are useful for working with XML documents which allow unstructured text (i.e. mixed=true) or if the elements can be interleaved (

<A/><B/><A/>
). This can occur for example when the schema defines maxOccurs>1 on a element which is a complexType with a choice order indicator.

The examples below assume an SDO created with the following schema and instance information, using the XML Data Access Service.

The schema below describes the format of a letter. The letter can optionally contain three properties; date, firstName, and lastName. The schema states mixed="true" which means that unstructured text can be interspersed between the three properties.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:letter="http://letterSchema"
  targetNamespace="http://letterSchema">
  <xsd:element name="letters" type="letter:FormLetter"/>
  <xsd:complexType name="FormLetter" mixed="true">
    <xsd:sequence>
      <xsd:element name="date" minOccurs="0" type="xsd:string"/>
      <xsd:element name="firstName" minOccurs="0" type="xsd:string"/>
      <xsd:element name="lastName" minOccurs="0" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

The following is an instance letter document. It contains the three letter properties; date, firstName and lastName, and has unstructured text elements for the address and letter body.

<letter:letters xmlns:letter="http://letterSchema">
  <date>March 1, 2005</date>
  Mutual of Omaha
  Wild Kingdom, USA
  Dear
  <firstName>Casy</firstName>
  <lastName>Crocodile</lastName>
  Please buy more shark repellent.
  Your premium is past due.
</letter:letters>

When loaded, the letter data object will have the sequence and property indices shown in the table below:

Sequence Index | Property Index:Name | Value
0 | 0:date | March 1, 2005
1 | - | Mutual of Omaha
2 | - | Wild Kingdom, USA
3 | - | Dear
4 | 1:firstName | Casy
5 | 2:lastName | Crocodile
6 | - | Please buy more shark repellent.
7 | - | Your premium is past due.

To ensure sequence indices are maintained, sequenced data objects should be manipulated through the SDO_Sequence interface. This allows the data object's instance data to be manipulated in terms of the sequence index as opposed to the property index (shown in the table above). The following examples assume the letter instance has been loaded into a data object referenced by the variable $letter.

Example #1 Getting the SDO_Sequence interface

We obtain a data object's sequence using the getSequence() method. The follow gets the sequence for the letter data object.

<?php
  $letter_seq = $letter->getSequence();
?>

All subsequent examples assume that the $letter_seq variable has been assigned the sequence for the letter data object.

Example #2 Get/set sequence values

We can get and set individual values (including unstructured text) using the sequence index. The following sets the firstName to 'Snappy' and gets the last sequence values (the unstructured text, 'Your premium is past due.').

<?php
  $letter_seq[4] = 'Snappy';
  $text = $letter_seq[count($letter_seq) - 1];
?>

Example #3 Sequence iteration

We can iterate through the individual sequence values using foreach. The following runs through the individual values in sequence order.

<?php
foreach ($letter->getSequence() as $value) {
    // ...
}
?>

Example #4 Sequence versus Data Object

Setting values through the data object interface may result in the value not being part of the sequence. A value set through the data object will only be accessible through the sequence if the property was already part of the sequence. The following example sets the lastName through the data object and gets it through the sequence. This is fine because lastName already exists in the sequence. If it had not previously been set, then lastName would be set to 'Smith', but would not be part of the sequence.

<?php
  $letter[2] = 'Smith';
  $last_name = $letter_seq[5];
?>

Example #5 Adding to a sequence

We can add new values to a sequence using the SDO_Sequence::insert() method. The following examples assume that the 'firstName' and 'lastName' properties are initially unset.

<?php
  // Append a firstName value to the sequence
  // value: 'Smith'
  // sequence index: NULL (append)
  // propertyIdentifier: 1 (firtName property index)
  $letter_seq->insert('Smith', NULL, 1);

  // Append a lastName value to the sequence
  // value: 'Jones'
  // sequence index: NULL (append)
  // propertyIdentifier: 'lastName' (lastName property name)
  $letter_seq->insert('Jones', NULL, 'lastName');

  // Append unstructured text
  // value: 'Cancel Subscription.'
  // sequence index: absent (append)
  // propertyIdentifier: absent (unstructured text)
  $letter_seq->insert('Cancel Subscription.');

  // Insert new unstructured text.  Subsequent sequence values
  // are shifted up.
  // value: 'Care of:'
  // sequence index: 1 (insert as second element)
  // propertyIdentifier: absent (unstructured text)
  $letter_seq->insert('Care of:', 1);
?>

Example #6 Removing from a sequence

We can use the isset() and unset() functions to test and remove items from the sequence (Note: unset() currently leaves the values in the data object, but this behaviour is likely to change to also remove the data from the data object). A sequence behaves like a contiguous list; therefore, removing items from the middle will shift entries at higher indices down. The following example tests to see if the first sequence element is set and unsets it if is.

<?php
  if (isset($letter_seq[0])) {
    unset($letter_seq[0]);
  }
?>

User Contributed Notes

There are no user contributed notes for this page.


Reflecting on Service Data Objects

SDOs have a knowledge of the structure they have been created to represent (the model). For example, a Company SDO created using the Company XML schema above would only be permitted to contain DepartmentType data objects which in turn could only contain EmployeeType data objects.

Sometimes it is useful to be able to access this model information at runtime. For example, this could be used to automatically generate a user interface for populating a data object. The model information is accessed using reflection.

Example #1 Reflecting on a Data Object

The following example shows how we can reflect on an empty Employee data object.

<?php
  // Create the employee data object (e.g. from an XML Data Access Service)
  $employee = ...;
  $reflection = new SDO_Model_ReflectionDataObject($employee);
  print($reflection);
?>

위 예제의 출력:

object(SDO_Model_ReflectionDataObject)#4 { - ROOT OBJECT - Type {
companyNS:EmployeeType[3] { commonj.sdo:String $name;
commonj.sdo:String $SN; commonj.sdo:Boolean $manager; } }

Using print on the SDO_Model_ReflectionDataObject writes out the data object's model. We can see from the output how the type companyNS:EmployeeType has three properties and we can see the names of the properties along with their types. Note, the primitive types are listed as SDO types (e.g. commonj.sdo namespace, String type). It is worth noting that this is the SDO model and when these are surfaced to an application they can be treated as the PHP equivalent types (e.g. string and boolean).

Example #2 Accessing the type information

We can query the type information of a data object using reflection. The following example checks the type corresponds to a data object rather than a primitive and then iterates through the properties of the type, writing out the name of each property ($type and $property are SDO_Model_Type and SDO_Model_Property objects, respectively).

<?php
    // Create the employee data object (e.g. from an XML Data Access Service)
    $employee = ...;
    $reflection = new SDO_Model_ReflectionDataObject($employee);
    $type = $reflection->getType();
    if (! $type->isDataType()) {
        foreach ($type->getProperties() as $property) {
            print $property->getName() . "\n";
        }
    }
?>

위 예제의 출력:

name
SN
manager

User Contributed Notes

There are no user contributed notes for this page.


SDO 함수 목록

Data Access Services

The table below lists the currently provided SDO Data Access Services:

DAS Name | Description
SDO_DAS_XML | An XML Data Access Service supporting reading/writing SDOs as XML documents.
SDO_DAS_Relational | A PDO-based Data Access Service supporting reading/writing SDO to relational databases. Implements an optimistic concurrency policy for updates.

예약 클래스

SDO consists of three sets of interfaces. The first set covers those interfaces for use by typical SDO applications. These are identified by the package prefix 'SDO_'. The second set is those used to reflect on, and work with, the model of a data object. These are identified by the package prefix 'SDO_Model_'. Finally, the third set are those use by Data Access Service implementations and are identified by the package prefix 'SDO_DAS_'. The majority of SDO users will not need to use or understand the 'SDO_Model_' and 'SDO_DAS_' interfaces.

SDO Application Programmer Interface

SDO_DataObject

The main interface through which data objects are manipulated. In addition to the methods below, SDO_DataObject extends the ArrayAccess, SDO_PropertyAccess (defines __get() / __set() methods for property access overloading), Iterator, and Countable interfaces.

메쏘드

  • getSequence - get the sequence for the data object

  • createDataObject - create a child data object

  • clear - unset the properties of a data object

  • getContainer - get the container (also known as 'parent') of this data object

  • getTypeName - get the name of the type for this data object

  • getTypeNamespaceURI - get the namespace URI of the type for this data object

SDO_Sequence

The interface through which sequenced data objects can be accessed to preserve ordering across a data object's properties and to allow unstructured text. SDO_Sequence preserves contiguous indices and therefore inserting or removing elements may shift other elements up or down. In addition to the methods below, SDO_Sequence extends the ArrayAccess, Iterator and Countable interface.

메쏘드

  • getProperty - get the property for a given sequence index

  • move - move an element from one property index to another

  • insert - insert a new value into the sequence

SDO_List

The interface through which many-valued properties are manipulated. In addition to the method defined below, SDO_List extends ArrayAccess, Iterator and Countable. SDO_List preserves contiguous indices and therefore inserting or removing elements may shift other elements up or down.

메쏘드

  • insert - insert a new value into the list

SDO_DataFactory

The interface through which data objects can be created. A Data Access Service is responsible for populating the model (i.e. configuring the data factory with the type and structure information for the data objects it can create.) for the factory and can then optionally return an instance of, or implement, the SDO_DataFactory interface.

메쏘드

  • create - create a new data object

SDO_Exception

An SDO_Exception is thrown when the caller's request cannot be completed. The subclasses of SDO_Exception are:

  • SDO_PropertyNotSetException - the property specified exists but has not been set or does not have a default value

  • SDO_PropertyNotFoundException - the property specified is not part of the data object's type

  • SDO_TypeNotFoundException - the specified namespace URI or type name is unknown

  • SDO_InvalidConversionException - conversion between the types of the assignment is not possible

  • SDO_IndexOutOfBoundsException - the numeric index into a data object, sequence or list is not in the valid range

  • SDO_UnsupportedOperationException - the request cannot be completed because it is not allowed, for example an attempt to set a read-only property.

메쏘드

One method is added to those inherited from the built in Exception class:

  • getCause - get the cause of this SDO_Exception

SDO Reflection Application Programmer Interfaces

SDO_Model_ReflectionDataObject

The main interface used to reflect on a data object instance to obtain its model type and property information. It is designed to follow the reflection pattern introduced in PHP 5.

생성자

  • __construct - construct a new SDO_Model_ReflectionDataObject.

메쏘드

  • export - get a string describing the data object.

  • getType - get the SDO_Model_Type for the data object.

  • getInstanceProperties - get the instance properties of the data object.

  • getContainmentProperty - get the property which defines the containment relationship to the data object.

SDO_Model_Type

The interface through which a data object's type information can be retrieved. This interface can be used to find out the type name and namespace URI of the type, whether the type allow open content, and so on.

메쏘드

  • getName - get the name of the type.

  • getNamespaceURI - get the namespace URI of the type.

  • isInstance - test for a data object being an instance of the type.

  • getProperties - get the properties of the type.

  • getProperty - get a property of the type.

  • isDataType - test to see if this type is a primitive scalar type.

  • isSequencedType - test to see if this is a sequenced type.

  • isOpenType - test to see if this is an open type.

  • isAbstractType - test to see if this is an abstract type.

  • getBaseType - get the base type of this type (if one exists).

SDO_Model_Property

The interface through which a data object's property information can be retrieved. This interface can be used to find out the type of a property, whether a property has a default value, whether the property is contained or reference by its parent, its cardinality, and so on.

메쏘드

  • getName - get the name of the property.

  • getType - get the type of the property.

  • isMany - test to see if the property is many-valued.

  • isContainment - test to see if the property describes a containment relationship.

  • getContainingType - get the type which contains this property.

  • getDefault - get the default value for a property.

SDO Data Access Service Developer Interfaces

SDO_DAS_DataObject

The interface through which a Data Access Service can access a data object's SDO_DAS_ChangeSummary. The change summary is used by the Data Access Service to check for conflicts when applying changes back to a data source.

메쏘드

  • getChangeSummary - get the change summary for a data object

SDO_DAS_ChangeSummary

The interface through which the change history of a data object is accessed. The change summary holds information for any modifications on a data object which occurred since logging was activated. In the case of deletions and modifications, the old values are also held in the change summary.

If logging is no longer active then the change summary only holds changes made up to the point when logging was deactivated. Reactivating logging clears the change summary. This is useful when a set of changes have been written out by a DAS and the data object is to be reused.

메쏘드

  • beginLogging - begin logging changes made to a data object

  • endLogging - end logging changes made to a data object

  • isLogging - test to see if change logging is on

  • getChangedDataObjects - get a list of the data objects which have been changed

  • getChangeType - get the type of change which has been made to a data object

  • getOldValues - get a list of old values for a data object

  • getOldContainer - get the old container data object for a deleted data object

SDO_DAS_Setting

The interface through which the old value for a property is accessed. A list of settings is returned by the change summary method getOldValues.

메쏘드

  • getPropertyIndex - get the property index for the changed property

  • getPropertyName - get the property name for the changed property

  • getValue - get the old value for the changed property

  • getListIndex - get the list index for the old value if it was part of a many-valued property

  • isSet - test to see if the property was set prior to being modified

SDO_DAS_DataFactory

The interface for constructing the model for an SDO_DataObject. The SDO_DAS_DataFactory is an abstract class providing a static method which returns a concrete data factory implementation. The implementation is used by Data Access Services to create an SDO model from their model. For example, a Relational Data Access Service might create and populate an SDO_DAS_DataFactory model based on a schema for a relational database.

메쏘드

  • getDataFactory - static methods for getting a concrete data factory instance

  • addType - add a new type to the SDO model

  • addPropertyToType - add a new property to a type definition in the SDO model

Table of Contents

  • SDO_DAS_ChangeSummary::beginLogging — Begin change logging
  • SDO_DAS_ChangeSummary::endLogging — End change logging
  • SDO_DAS_ChangeSummary::getChangeType — Get the type of change made to an SDO_DataObject
  • SDO_DAS_ChangeSummary::getChangedDataObjects — Get the changed data objects from a change summary
  • SDO_DAS_ChangeSummary::getOldContainer — Get the old container for a deleted SDO_DataObject
  • SDO_DAS_ChangeSummary::getOldValues — Get the old values for a given changed SDO_DataObject
  • SDO_DAS_ChangeSummary::isLogging — Test to see whether change logging is switched on
  • SDO_DAS_DataFactory::addPropertyToType — Adds a property to a type
  • SDO_DAS_DataFactory::addType — Add a new type to a model
  • SDO_DAS_DataFactory::getDataFactory — Get a data factory instance
  • SDO_DAS_DataObject::getChangeSummary — Get a data object's change summary
  • SDO_DAS_Setting::getListIndex — Get the list index for a changed many-valued property
  • SDO_DAS_Setting::getPropertyIndex — Get the property index for a changed property
  • SDO_DAS_Setting::getPropertyName — Get the property name for a changed property
  • SDO_DAS_Setting::getValue — Get the old value for the changed property
  • SDO_DAS_Setting::isSet — Test whether a property was set prior to being modified
  • SDO_DataFactory::create — Create an SDO_DataObject
  • SDO_DataObject::clear — Clear an SDO_DataObject's properties
  • SDO_DataObject::createDataObject — Create a child SDO_DataObject
  • SDO_DataObject::getContainer — Get a data object's container
  • SDO_DataObject::getSequence — Get the sequence for a data object
  • SDO_DataObject::getTypeName — Return the name of the type for a data object.
  • SDO_DataObject::getTypeNamespaceURI — Return the namespace URI of the type for a data object.
  • SDO_Exception::getCause — Get the cause of the exception.
  • SDO_List::insert — Insert into a list
  • SDO_Model_Property::getContainingType — Get the SDO_Model_Type which contains this property
  • SDO_Model_Property::getDefault — Get the default value for the property
  • SDO_Model_Property::getName — Get the name of the SDO_Model_Property
  • SDO_Model_Property::getType — Get the SDO_Model_Type of the property
  • SDO_Model_Property::isContainment — Test to see if the property defines a containment relationship
  • SDO_Model_Property::isMany — Test to see if the property is many-valued
  • SDO_Model_ReflectionDataObject::__construct — Construct an SDO_Model_ReflectionDataObject
  • SDO_Model_ReflectionDataObject::export — Get a string describing the SDO_DataObject.
  • SDO_Model_ReflectionDataObject::getContainmentProperty — Get the property which defines the containment relationship to the data object
  • SDO_Model_ReflectionDataObject::getInstanceProperties — Get the instance properties of the SDO_DataObject
  • SDO_Model_ReflectionDataObject::getType — Get the SDO_Model_Type for the SDO_DataObject
  • SDO_Model_Type::getBaseType — Get the base type for this type
  • SDO_Model_Type::getName — Get the name of the type
  • SDO_Model_Type::getNamespaceURI — Get the namespace URI of the type
  • SDO_Model_Type::getProperties — Get the SDO_Model_Property objects defined for the type
  • SDO_Model_Type::getProperty — Get an SDO_Model_Property of the type
  • SDO_Model_Type::isAbstractType — Test to see if this SDO_Model_Type is an abstract data type
  • SDO_Model_Type::isDataType — Test to see if this SDO_Model_Type is a primitive data type
  • SDO_Model_Type::isInstance — Test for an SDO_DataObject being an instance of this SDO_Model_Type
  • SDO_Model_Type::isOpenType — Test to see if this type is an open type
  • SDO_Model_Type::isSequencedType — Test to see if this is a sequenced type
  • SDO_Sequence::getProperty — Return the property for the specified sequence index.
  • SDO_Sequence::insert — Insert into a sequence
  • SDO_Sequence::move — Move an item to another sequence position

User Contributed Notes

ng4rrjanbiah at rediffmail dot com 18-Mar-2006 07:07
Some useful links on SDO:
1. Quick intro ( http://www.obalweb.net/wppro/?p=19 )
2. SDO for Zend Conf 2005 ( http://www.ibm.com/developerworks/forums/weblogs/data/SDOforZendConf2005.pdf ), Presentation, [481 KB], Graham Charters, 2005-10-18
3. Introducing Service Data Objects for PHP ( http://www.zend.com/pecl/tutorials/sdo.php ), 2005-08-05
4. Service Data Objects specification ( http://www.ibm.com/developerworks/library/specification/j-commonj-sdowmt/ ), 2003-2005

HTH,
R. Rajesh Jeba Anbiah


Begin change logging

  • ≪ SDO 함수 목록
  • SDO_DAS_ChangeSummary::endLogging ≫
  • PHP Manual
  • SDO 함수 목록
  • Begin change logging

SDO_DAS_ChangeSummary::beginLogging

(^)

SDO_DAS_ChangeSummary::beginLogging — Begin change logging

설명

void SDO_DAS_ChangeSummary::beginLogging ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Begin logging changes made to the SDO_DataObject.

인수

None.

반환값

None.

User Contributed Notes

There are no user contributed notes for this page.


End change logging

  • ≪ SDO_DAS_ChangeSummary::beginLogging
  • SDO_DAS_ChangeSummary::getChangeType ≫
  • PHP Manual
  • SDO 함수 목록
  • End change logging

SDO_DAS_ChangeSummary::endLogging

(^)

SDO_DAS_ChangeSummary::endLogging — End change logging

설명

void SDO_DAS_ChangeSummary::endLogging ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

End logging changes made to an SDO_DataObject.

인수

None.

반환값

None.

User Contributed Notes

There are no user contributed notes for this page.


Get the type of change made to an SDO_DataObject

  • ≪ SDO_DAS_ChangeSummary::endLogging
  • SDO_DAS_ChangeSummary::getChangedDataObjects ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the type of change made to an SDO_DataObject

SDO_DAS_ChangeSummary::getChangeType

(^)

SDO_DAS_ChangeSummary::getChangeType — Get the type of change made to an SDO_DataObject

설명

int SDO_DAS_ChangeSummary::getChangeType ( SDO_DataObject $dataObject )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get the type of change which has been made to the supplied SDO_DataObject.

인수

dataObject

The SDO_DataObject which has been changed.

반환값

The type of change which has been made. The change type is expressed as an enumeration and will be one of the following four values:

  • SDO_DAS_ChangeSummary::NONE

  • SDO_DAS_ChangeSummary::MODIFICATION

  • SDO_DAS_ChangeSummary::ADDITION

  • SDO_DAS_ChangeSummary::DELETION

User Contributed Notes

There are no user contributed notes for this page.


Get the changed data objects from a change summary

  • ≪ SDO_DAS_ChangeSummary::getChangeType
  • SDO_DAS_ChangeSummary::getOldContainer ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the changed data objects from a change summary

SDO_DAS_ChangeSummary::getChangedDataObjects

(^)

SDO_DAS_ChangeSummary::getChangedDataObjects — Get the changed data objects from a change summary

설명

SDO_List SDO_DAS_ChangeSummary::getChangedDataObjects ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get an SDO_List of the SDO_DataObjects which have been changed. These data objects can then be used to identify the types of change made to each, along with the old values.

인수

None.

반환값

Returns an SDO_List of SDO_DataObjects.

User Contributed Notes

There are no user contributed notes for this page.


Get the old container for a deleted SDO_DataObject

  • ≪ SDO_DAS_ChangeSummary::getChangedDataObjects
  • SDO_DAS_ChangeSummary::getOldValues ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the old container for a deleted SDO_DataObject

SDO_DAS_ChangeSummary::getOldContainer

(^)

SDO_DAS_ChangeSummary::getOldContainer — Get the old container for a deleted SDO_DataObject

설명

SDO_DataObject SDO_DAS_ChangeSummary::getOldContainer ( SDO_DataObject $data_object )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get the old container (SDO_DataObject) for a deleted SDO_DataObject.

인수

data_object

The SDO_DataObject which has been deleted and whose container we wish to identify.

반환값

The old containing data object of the deleted SDO_DataObject.

User Contributed Notes

There are no user contributed notes for this page.


Get the old values for a given changed SDO_DataObject

  • ≪ SDO_DAS_ChangeSummary::getOldContainer
  • SDO_DAS_ChangeSummary::isLogging ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the old values for a given changed SDO_DataObject

SDO_DAS_ChangeSummary::getOldValues

(^)

SDO_DAS_ChangeSummary::getOldValues — Get the old values for a given changed SDO_DataObject

설명

SDO_List SDO_DAS_ChangeSummary::getOldValues ( SDO_DataObject $data_object )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get a list of the old values for a given changed SDO_DataObject. Returns a list of SDO_DAS_Settings describing the old values for the changed properties of the SDO_DataObject.

인수

data_object

The data object which has been changed.

반환값

A list of SDO_DAS_Settings describing the old values for the changed properties of the SDO_DataObject. If the change type is SDO_DAS_ChangeSummary::ADDITION, this list is empty.

User Contributed Notes

There are no user contributed notes for this page.


Test to see whether change logging is switched on

  • ≪ SDO_DAS_ChangeSummary::getOldValues
  • SDO_DAS_DataFactory::addPropertyToType ≫
  • PHP Manual
  • SDO 함수 목록
  • Test to see whether change logging is switched on

SDO_DAS_ChangeSummary::isLogging

(^)

SDO_DAS_ChangeSummary::isLogging — Test to see whether change logging is switched on

설명

bool SDO_DAS_ChangeSummary::isLogging ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Test to see whether change logging is switched on.

인수

None.

반환값

Returns TRUE if change logging is on, otherwise returns FALSE.

User Contributed Notes

There are no user contributed notes for this page.


Adds a property to a type

  • ≪ SDO_DAS_ChangeSummary::isLogging
  • SDO_DAS_DataFactory::addType ≫
  • PHP Manual
  • SDO 함수 목록
  • Adds a property to a type

SDO_DAS_DataFactory::addPropertyToType

(^)

SDO_DAS_DataFactory::addPropertyToType — Adds a property to a type

설명

void SDO_DAS_DataFactory::addPropertyToType ( string $parent_type_namespace_uri , string $parent_type_name , string $property_name , string $type_namespace_uri , string $type_name [, array $options ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Adds a property to a type. The type must already be known to the SDO_DAS_DataFactory (i.e. have been added using addType()). The property becomes a property of the type. This is how the graph model for the structure of an SDO_DataObject is built.

인수

parent_type_namespace_uri

The namespace URI for the parent type.

parent_type_name

The type name for the parent type.

property_name

The name by which the property will be known in the parent type.

type_namespace_uri

The namespace URI for the type of the property.

type_name

The type name for the type of the property

options

This array holds one or more key=>value pairs to set attribute values for the property. The optional keywords are:

many

A flag to say whether the property is many-valued. A value of 'true' adds the property as a many-valued property (default is 'false').

readOnly

A flag to say whether the property is read-only. A value of 'true' means the property value cannot be modified through the SDO application APIs (default is 'false').

containment

A flag to say whether the property is contained by the parent. A value of 'true' means the property is contained by the parent. A value of 'false' results in a non-containment reference (default is 'true'). This flag is only interpreted when adding properties which are data object types, otherwise it is ignored.

default

A default value for the property. Omitting this key means that the property does not have a default value. A property can only have a default value if it is a single-valued data type (primitive).

반환값

None.

변경점

버전 | 설명
0.5.2 | Optional parameters many, readOnly, and containment deprecated in favour of the options array.

예제

Example #1 A SDO_DAS_DataFactory::addPropertyToType() example

The following adds an 'addressline' property to a Person type. The person type is identified by its namespace, 'PersonNS', and type name, 'PersonType'. The type of the 'addressline' property is a many-valued SDO data type (primitive) with namespace 'commonj.sdo' and type name 'String'.

<?php
  $df->addPropertyToType('PersonNS', 'PersonType',
    'addressline', 'commonj.sdo', 'String', array('many'=>true));
?>

User Contributed Notes

There are no user contributed notes for this page.


Add a new type to a model

  • ≪ SDO_DAS_DataFactory::addPropertyToType
  • SDO_DAS_DataFactory::getDataFactory ≫
  • PHP Manual
  • SDO 함수 목록
  • Add a new type to a model

SDO_DAS_DataFactory::addType

(^)

SDO_DAS_DataFactory::addType — Add a new type to a model

설명

void SDO_DAS_DataFactory::addType ( string $type_namespace_uri , string $type_name [, array $options ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Add a new type to the SDO_DAS_DataFactory, defined by its namespace and type name. The type becomes part of the model of data objects that the data factory can create.

인수

type_namespace_uri

The namespace of the type.

type_name

The name of the type.

options

This array holds one or more key=>value pairs to set attribute values for the type. The optional keywords are:

open

A flag to say whether the type is open. An SDO_DataObject whose type is open can have properties added to them which are not described by the type. This capability is used to support working with XML documents whose schema support open content such as that described by an <xsd:any> element. The default value is 'false'.

sequenced

A flag to say whether the type is sequenced. Sequenced types can have the ordering across properties preserved and can contain unstructured text. The default value is 'false'. For more information on sequenced types see the section on Working with Sequenced Data Objects.

basetype

If specified, an array of namespace URI and type name strings for the type from which this type is derived. An example of the use of base types is when a type derived in an XML schema inherits from another type by using <extension base="...">.

반환값

None.

예제

Example #1 A SDO_DAS_DataFactory::addType() example

The following adds a new data object type of 'CompanyType' where that type belongs to the namespace 'CompanyNS'.

<?php
  $df->addType('CompanyNS', 'CompanyType');
?>

User Contributed Notes

There are no user contributed notes for this page.


Get a data factory instance

  • ≪ SDO_DAS_DataFactory::addType
  • SDO_DAS_DataObject::getChangeSummary ≫
  • PHP Manual
  • SDO 함수 목록
  • Get a data factory instance

SDO_DAS_DataFactory::getDataFactory

(^)

SDO_DAS_DataFactory::getDataFactory — Get a data factory instance

설명

SDO_DAS_DataFactory SDO_DAS_DataFactory::getDataFactory ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Static method to get an instance of an SDO_DAS_DataFactory. This instance is initially only configured with the basic SDO types. A Data Access Service is responsible for populating the data factory model and then allowing PHP applications to create SDOs based on the model through the SDO_DataFactory interface. PHP applications should always obtain a data factory from a configured Data Access Service, not through this interface.

인수

None.

반환값

Returns an SDO_DAS_DataFactory.

User Contributed Notes

There are no user contributed notes for this page.


Get a data object's change summary

  • ≪ SDO_DAS_DataFactory::getDataFactory
  • SDO_DAS_Setting::getListIndex ≫
  • PHP Manual
  • SDO 함수 목록
  • Get a data object's change summary

SDO_DAS_DataObject::getChangeSummary

(^)

SDO_DAS_DataObject::getChangeSummary — Get a data object's change summary

설명

SDO_DAS_ChangeSummary SDO_DAS_DataObject::getChangeSummary ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get the SDO_DAS_ChangeSummary for an SDO_DAS_DataObject, or NULL if it does not have one.

인수

None.

반환값

Returns the SDO_DAS_ChangeSummary for an SDO_DAS_DataObject, or NULL if it does not have one.

User Contributed Notes

There are no user contributed notes for this page.


Get the list index for a changed many-valued property

  • ≪ SDO_DAS_DataObject::getChangeSummary
  • SDO_DAS_Setting::getPropertyIndex ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the list index for a changed many-valued property

SDO_DAS_Setting::getListIndex

(^)

SDO_DAS_Setting::getListIndex — Get the list index for a changed many-valued property

설명

int SDO_DAS_Setting::getListIndex ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get the list index for a modification made to an element of a many-valued property. For example, if we modified the third element of a many-valued property we could obtain an SDO_DAS_Setting from the change summary corresponding to that modification. A call to getListIndex() on that setting would return the value 2 (lists are indexed from zero).

인수

None.

반환값

The list index for the element of the many-valued property which has been changed.

User Contributed Notes

There are no user contributed notes for this page.


Get the property index for a changed property

  • ≪ SDO_DAS_Setting::getListIndex
  • SDO_DAS_Setting::getPropertyName ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the property index for a changed property

SDO_DAS_Setting::getPropertyIndex

(^)

SDO_DAS_Setting::getPropertyIndex — Get the property index for a changed property

설명

int SDO_DAS_Setting::getPropertyIndex ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the property index for the changed property. This index identifies the property which was modified in data object.

인수

None.

반환값

The property index for a changed property.

User Contributed Notes

There are no user contributed notes for this page.


Get the property name for a changed property

  • ≪ SDO_DAS_Setting::getPropertyIndex
  • SDO_DAS_Setting::getValue ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the property name for a changed property

SDO_DAS_Setting::getPropertyName

(^)

SDO_DAS_Setting::getPropertyName — Get the property name for a changed property

설명

string SDO_DAS_Setting::getPropertyName ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the property name for the changed property. This name identifies the property which was modified in data object.

인수

None.

반환값

The property name for a changed property.

User Contributed Notes

There are no user contributed notes for this page.


Get the old value for the changed property

  • ≪ SDO_DAS_Setting::getPropertyName
  • SDO_DAS_Setting::isSet ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the old value for the changed property

SDO_DAS_Setting::getValue

(^)

SDO_DAS_Setting::getValue — Get the old value for the changed property

설명

mixed SDO_DAS_Setting::getValue ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the old value for the changed property. This can be used by a Data Access Service when writing updates to a data source. The DAS uses the old value to detect conflicts by comparing it with the current value in the data source. If they do not match, then the data source has been updated since the data object was originally populated, and therefore writing any new updates risks compromising the integrity of the data.

인수

None.

반환값

Returns the old value of the changed property.

User Contributed Notes

There are no user contributed notes for this page.


Test whether a property was set prior to being modified

  • ≪ SDO_DAS_Setting::getValue
  • SDO_DataFactory::create ≫
  • PHP Manual
  • SDO 함수 목록
  • Test whether a property was set prior to being modified

SDO_DAS_Setting::isSet

(^)

SDO_DAS_Setting::isSet — Test whether a property was set prior to being modified

설명

bool SDO_DAS_Setting::isSet ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Test whether a property was set prior to being modified. If it was set prior to being modified then the SDO_DAS_Setting will also contain the old value.

인수

None.

반환값

Returns TRUE if the property was set prior to being modified, otherwise returns FALSE.

User Contributed Notes

There are no user contributed notes for this page.


Create an SDO_DataObject

  • ≪ SDO_DAS_Setting::isSet
  • SDO_DataObject::clear ≫
  • PHP Manual
  • SDO 함수 목록
  • Create an SDO_DataObject

SDO_DataFactory::create

(^)

SDO_DataFactory::create — Create an SDO_DataObject

설명

void SDO_DataFactory::create ( string $type_namespace_uri , string $type_name )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Create a new SDO_DataObject given the data object's namespace URI and type name.

인수

type_namespace_uri

The namespace of the type.

type_name

The name of the type.

반환값

Returns the newly created SDO_DataObject.

오류/예외

SDO_TypeNotFoundException

Thrown if the namespaceURI and typeName do not correspond to a type known to this data factory.

User Contributed Notes

There are no user contributed notes for this page.


Clear an SDO_DataObject's properties

  • ≪ SDO_DataFactory::create
  • SDO_DataObject::createDataObject ≫
  • PHP Manual
  • SDO 함수 목록
  • Clear an SDO_DataObject's properties

SDO_DataObject::clear

(^)

SDO_DataObject::clear — Clear an SDO_DataObject's properties

설명

void SDO_DataObject::clear ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Clear an SDO_DataObject's properties. Read-only properties are unaffected. Subsequent calls to isset() for the data object will return FALSE.

인수

None.

반환값

No return values.

User Contributed Notes

There are no user contributed notes for this page.


Create a child SDO_DataObject

  • ≪ SDO_DataObject::clear
  • SDO_DataObject::getContainer ≫
  • PHP Manual
  • SDO 함수 목록
  • Create a child SDO_DataObject

SDO_DataObject::createDataObject

(^)

SDO_DataObject::createDataObject — Create a child SDO_DataObject

설명

SDO_DataObject SDO_DataObject::createDataObject ( mixed $identifier )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Create a child SDO_DataObject of the default type for the property identified. The data object is automatically inserted into the tree and a reference to it is returned.

인수

identifier

Identifies the property for the data object type to be created. Can be either a property name (string), a property index (int), or an SDO_Model_Property.

반환값

Returns the newly created SDO_DataObject.

오류/예외

SDO_PropertyNotFoundException

Thrown if the identifier does not correspond to a property of the data object.

User Contributed Notes

There are no user contributed notes for this page.


Get a data object's container

  • ≪ SDO_DataObject::createDataObject
  • SDO_DataObject::getSequence ≫
  • PHP Manual
  • SDO 함수 목록
  • Get a data object's container

SDO_DataObject::getContainer

(^)

SDO_DataObject::getContainer — Get a data object's container

설명

SDO_DataObject SDO_DataObject::getContainer ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get the data object which contains this data object.

인수

None.

반환값

Returns the SDO_DataObject which contains this SDO_DataObject, or returns NULL if this is a root SDO_DataObject (i.e. it has no container).

User Contributed Notes

There are no user contributed notes for this page.


Get the sequence for a data object

  • ≪ SDO_DataObject::getContainer
  • SDO_DataObject::getTypeName ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the sequence for a data object

SDO_DataObject::getSequence

(^)

SDO_DataObject::getSequence — Get the sequence for a data object

설명

SDO_Sequence SDO_DataObject::getSequence ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Return the SDO_Sequence for this SDO_DataObject. Accessing the SDO_DataObject through the SDO_Sequence interface acts on the same SDO_DataObject instance data, but preserves ordering across properties.

인수

None.

반환값

The SDO_Sequence for this SDO_DataObject, or returns NULL if the SDO_DataObject is not of a type which can have a sequence.

User Contributed Notes

There are no user contributed notes for this page.


Return the name of the type for a data object.

  • ≪ SDO_DataObject::getSequence
  • SDO_DataObject::getTypeNamespaceURI ≫
  • PHP Manual
  • SDO 함수 목록
  • Return the name of the type for a data object.

SDO_DataObject::getTypeName

(^)

SDO_DataObject::getTypeName — Return the name of the type for a data object.

설명

string SDO_DataObject::getTypeName ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Return the name of the type for a data object. A convenience method corresponding to SDO_Model_ReflectionDataObject::getType().getName().

인수

None.

반환값

The name of the type for the data object.

User Contributed Notes

There are no user contributed notes for this page.


Return the namespace URI of the type for a data object.

  • ≪ SDO_DataObject::getTypeName
  • SDO_Exception::getCause ≫
  • PHP Manual
  • SDO 함수 목록
  • Return the namespace URI of the type for a data object.

SDO_DataObject::getTypeNamespaceURI

(^)

SDO_DataObject::getTypeNamespaceURI — Return the namespace URI of the type for a data object.

설명

string SDO_DataObject::getTypeNamespaceURI ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Return the namespace URI of the type for a data object. A convenience method corresponding to SDO_Model_ReflectionDataObject::getType().getNamespaceURI().

인수

None.

반환값

The namespace URI of the type for the data object.

User Contributed Notes

There are no user contributed notes for this page.


Get the cause of the exception.

  • ≪ SDO_DataObject::getTypeNamespaceURI
  • SDO_List::insert ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the cause of the exception.

SDO_Exception::getCause

(^)

SDO_Exception::getCause — Get the cause of the exception.

설명

mixed SDO_Exception::getCause ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the cause of this exception or NULL if the cause is nonexistent or unknown. Typically the cause will be an SDO_CPPException object, which may be used to obtain additional diagnostic information.

인수

None.

반환값

Returns the cause of this exception or NULL if the cause is nonexistent or unknown.

User Contributed Notes

There are no user contributed notes for this page.


Insert into a list

  • ≪ SDO_Exception::getCause
  • SDO_Model_Property::getContainingType ≫
  • PHP Manual
  • SDO 함수 목록
  • Insert into a list

SDO_List::insert

(^)

SDO_List::insert — Insert into a list

설명

void SDO_List::insert ( mixed $value [, int $index ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Insert a new element at a specified position in the list. All subsequent list items are moved up.

인수

value

The new value to be inserted. This can be either a primitive or an SDO_DataObject.

index

The position at which to insert the new element. If this argument is not specified then the new value will be appended.

반환값

None.

오류/예외

SDO_IndexOutOfBoundsException

Thrown if the list index is less than zero or greater than the size of the list.

SDO_InvalidConversionException

Thrown if the type of the new value does not match the type for the list (e.g. the type of the many-valued property that the list represents).

User Contributed Notes

There are no user contributed notes for this page.


Get the SDO_Model_Type which contains this property

  • ≪ SDO_List::insert
  • SDO_Model_Property::getDefault ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the SDO_Model_Type which contains this property

SDO_Model_Property::getContainingType

(^)

SDO_Model_Property::getContainingType — Get the SDO_Model_Type which contains this property

설명

SDO_Model_Type SDO_Model_Property::getContainingType ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the SDO_Model_Type which contains this property.

인수

None.

반환값

Returns the SDO_Model_Type which contains this property.

User Contributed Notes

There are no user contributed notes for this page.


Get the default value for the property

  • ≪ SDO_Model_Property::getContainingType
  • SDO_Model_Property::getName ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the default value for the property

SDO_Model_Property::getDefault

(^)

SDO_Model_Property::getDefault — Get the default value for the property

설명

mixed SDO_Model_Property::getDefault ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the default value for the property. Only primitive data type properties can have default values.

인수

None.

반환값

Returns the default value for the property.

User Contributed Notes

There are no user contributed notes for this page.


Get the name of the SDO_Model_Property

  • ≪ SDO_Model_Property::getDefault
  • SDO_Model_Property::getType ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the name of the SDO_Model_Property

SDO_Model_Property::getName

(^)

SDO_Model_Property::getName — Get the name of the SDO_Model_Property

설명

string SDO_Model_Property::getName ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the name of the SDO_Model_Property.

인수

None.

반환값

Returns the name of the SDO_Model_Property.

User Contributed Notes

There are no user contributed notes for this page.


Get the SDO_Model_Type of the property

  • ≪ SDO_Model_Property::getName
  • SDO_Model_Property::isContainment ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the SDO_Model_Type of the property

SDO_Model_Property::getType

(^)

SDO_Model_Property::getType — Get the SDO_Model_Type of the property

설명

SDO_Model_Type SDO_Model_Property::getType ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get the SDO_Model_Type of the property. The SDO_Model_Type describes the type information for the property, such as its type name, namespace URI, whether it is a primitive data type, and so on.

인수

None.

반환값

Returns the SDO_Model_Type describing the property's type information.

User Contributed Notes

There are no user contributed notes for this page.


Test to see if the property defines a containment relationship

  • ≪ SDO_Model_Property::getType
  • SDO_Model_Property::isMany ≫
  • PHP Manual
  • SDO 함수 목록
  • Test to see if the property defines a containment relationship

SDO_Model_Property::isContainment

(^)

SDO_Model_Property::isContainment — Test to see if the property defines a containment relationship

설명

bool SDO_Model_Property::isContainment ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Test to see if the property corresponds to a containment relationship. Returns TRUE if the property defines a containment relationship, or FALSE if it is reference.

인수

None.

반환값

Returns TRUE if the property defines a containment relationship, or FALSE if it is reference.

User Contributed Notes

There are no user contributed notes for this page.


Test to see if the property is many-valued

  • ≪ SDO_Model_Property::isContainment
  • SDO_Model_ReflectionDataObject::__construct ≫
  • PHP Manual
  • SDO 함수 목록
  • Test to see if the property is many-valued

SDO_Model_Property::isMany

(^)

SDO_Model_Property::isMany — Test to see if the property is many-valued

설명

bool SDO_Model_Property::isMany ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Test to see if the property is many-valued. Returns TRUE if this is a many-valued property, otherwise returns FALSE.

인수

None.

반환값

Returns TRUE if this is a many-valued property, otherwise returns FALSE.

User Contributed Notes

There are no user contributed notes for this page.


Construct an SDO_Model_ReflectionDataObject

  • ≪ SDO_Model_Property::isMany
  • SDO_Model_ReflectionDataObject::export ≫
  • PHP Manual
  • SDO 함수 목록
  • Construct an SDO_Model_ReflectionDataObject

SDO_Model_ReflectionDataObject::__construct

(^)

SDO_Model_ReflectionDataObject::__construct — Construct an SDO_Model_ReflectionDataObject

설명

SDO_Model_ReflectionDataObject::__construct ( SDO_DataObject $data_object )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Construct an SDO_Model_ReflectionDataObject to reflect on an SDO_DataObject. Reflecting on an SDO_DataObject gives access to information about its model. The model contains information such as the data object's type, and whether that type is sequenced (preserves ordering across properties) or open (each instance can have its model extended). The model also holds information about the data object's properties, any default values they may have, and so on.

인수

data_object

The SDO_DataObject being reflected upon.

반환값

None.

User Contributed Notes

There are no user contributed notes for this page.


Get a string describing the SDO_DataObject.

  • ≪ SDO_Model_ReflectionDataObject::__construct
  • SDO_Model_ReflectionDataObject::getContainmentProperty ≫
  • PHP Manual
  • SDO 함수 목록
  • Get a string describing the SDO_DataObject.

SDO_Model_ReflectionDataObject::export

(^)

SDO_Model_ReflectionDataObject::export — Get a string describing the SDO_DataObject.

설명

mixed SDO_Model_ReflectionDataObject::export ( SDO_Model_ReflectionDataObject $rdo [, bool $return ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get a string describing the SDO_DataObject. The default behaviour is to print the output, but if TRUE is specified for return, it is returned as a string.

인수

rdo

An SDO_Model_ReflectionDataObject.

return

If TRUE, return the output as a string, otherwise print it.

반환값

Returns the output if TRUE is specified for return, otherwise NULL.

User Contributed Notes

There are no user contributed notes for this page.


Get the property which defines the containment relationship to the data object

  • ≪ SDO_Model_ReflectionDataObject::export
  • SDO_Model_ReflectionDataObject::getInstanceProperties ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the property which defines the containment relationship to the data object

SDO_Model_ReflectionDataObject::getContainmentProperty

(^)

SDO_Model_ReflectionDataObject::getContainmentProperty — Get the property which defines the containment relationship to the data object

설명

SDO_Model_Property SDO_Model_ReflectionDataObject::getContainmentProperty ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get the SDO_Model_Property that contains the SDO_DataObject. This method is used to navigate up to the parent's property which contains the data object which has been reflected upon.

인수

None.

반환값

Returns the container's SDO_Model_Property which references the SDO_DataObject, or NULL if it is a root SDO_DataObject.

User Contributed Notes

There are no user contributed notes for this page.


Get the instance properties of the SDO_DataObject

  • ≪ SDO_Model_ReflectionDataObject::getContainmentProperty
  • SDO_Model_ReflectionDataObject::getType ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the instance properties of the SDO_DataObject

SDO_Model_ReflectionDataObject::getInstanceProperties

(^)

SDO_Model_ReflectionDataObject::getInstanceProperties — Get the instance properties of the SDO_DataObject

설명

array SDO_Model_ReflectionDataObject::getInstanceProperties ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get the instance properties for the SDO_DataObject. The instance properties consist of all the properties defined on the data object's type, plus any instance properties from open content (if the data object is an open type).

인수

None.

반환값

An array of SDO_Model_Property objects.

User Contributed Notes

There are no user contributed notes for this page.


Get the SDO_Model_Type for the SDO_DataObject

  • ≪ SDO_Model_ReflectionDataObject::getInstanceProperties
  • SDO_Model_Type::getBaseType ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the SDO_Model_Type for the SDO_DataObject

SDO_Model_ReflectionDataObject::getType

(^)

SDO_Model_ReflectionDataObject::getType — Get the SDO_Model_Type for the SDO_DataObject

설명

SDO_Model_Type SDO_Model_ReflectionDataObject::getType ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the SDO_Model_Type for the SDO_DataObject. The SDO_Model_Type holds all the information about the data object's type, such as namespace URI, type name, whether it is a primitive data type, and so on.

인수

None.

반환값

Returns the SDO_Model_Type for the SDO_DataObject.

User Contributed Notes

There are no user contributed notes for this page.


Get the base type for this type

  • ≪ SDO_Model_ReflectionDataObject::getType
  • SDO_Model_Type::getName ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the base type for this type

SDO_Model_Type::getBaseType

(^)

SDO_Model_Type::getBaseType — Get the base type for this type

설명

SDO_Model_Type SDO_Model_Type::getBaseType ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get the base type for this type. Returns the SDO_Model_Type for the base type if this type inherits from another, otherwise returns NULL. An example of when base types occur is when a type defined in XML schema inherits from another type by using

<extension base="...">
.

인수

None.

반환값

Returns the SDO_Model_Type for the base type if this type inherits from another, otherwise returns NULL.

User Contributed Notes

There are no user contributed notes for this page.


Get the name of the type

  • ≪ SDO_Model_Type::getBaseType
  • SDO_Model_Type::getNamespaceURI ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the name of the type

SDO_Model_Type::getName

(^)

SDO_Model_Type::getName — Get the name of the type

설명

string SDO_Model_Type::getName ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the name of the type. The combination of type name and namespace URI is used to uniquely identify the type.

인수

None.

반환값

Returns the name of the type.

User Contributed Notes

There are no user contributed notes for this page.


Get the namespace URI of the type

  • ≪ SDO_Model_Type::getName
  • SDO_Model_Type::getProperties ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the namespace URI of the type

SDO_Model_Type::getNamespaceURI

(^)

SDO_Model_Type::getNamespaceURI — Get the namespace URI of the type

설명

string SDO_Model_Type::getNamespaceURI ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the namespace URI of the type. The combination of namespace URI and type name is used to uniquely identify the type.

인수

None.

반환값

Returns the namespace URI of the type.

User Contributed Notes

There are no user contributed notes for this page.


Get the SDO_Model_Property objects defined for the type

  • ≪ SDO_Model_Type::getNamespaceURI
  • SDO_Model_Type::getProperty ≫
  • PHP Manual
  • SDO 함수 목록
  • Get the SDO_Model_Property objects defined for the type

SDO_Model_Type::getProperties

(^)

SDO_Model_Type::getProperties — Get the SDO_Model_Property objects defined for the type

설명

array SDO_Model_Type::getProperties ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get an array of SDO_Model_Property objects describing the properties defined for the SDO_Model_Type. Each SDO_Model_Property holds information such as the property name, default value, and so on.

인수

None.

반환값

Returns an array of SDO_Model_Property objects.

User Contributed Notes

There are no user contributed notes for this page.


Get an SDO_Model_Property of the type

  • ≪ SDO_Model_Type::getProperties
  • SDO_Model_Type::isAbstractType ≫
  • PHP Manual
  • SDO 함수 목록
  • Get an SDO_Model_Property of the type

SDO_Model_Type::getProperty

(^)

SDO_Model_Type::getProperty — Get an SDO_Model_Property of the type

설명

SDO_Model_Property SDO_Model_Type::getProperty ( mixed $identifier )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Get an SDO_Model_Property of the type, identified by its property index or property name.

인수

identifier

The property index or property name.

반환값

Returns the SDO_Model_Property.

User Contributed Notes

There are no user contributed notes for this page.


Test to see if this SDO_Model_Type is an abstract data type

  • ≪ SDO_Model_Type::getProperty
  • SDO_Model_Type::isDataType ≫
  • PHP Manual
  • SDO 함수 목록
  • Test to see if this SDO_Model_Type is an abstract data type

SDO_Model_Type::isAbstractType

(^)

SDO_Model_Type::isAbstractType — Test to see if this SDO_Model_Type is an abstract data type

설명

bool SDO_Model_Type::isAbstractType ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Test to see if this SDO_Model_Type is an abstract data type. Returns TRUE if this type is abstract, that is, no SDO_DataObject of this type can be instantiated, though other types may inherit from it.

인수

None.

반환값

Returns TRUE if this type is an abstract data type, otherwise returns FALSE.

User Contributed Notes

There are no user contributed notes for this page.


Test to see if this SDO_Model_Type is a primitive data type

  • ≪ SDO_Model_Type::isAbstractType
  • SDO_Model_Type::isInstance ≫
  • PHP Manual
  • SDO 함수 목록
  • Test to see if this SDO_Model_Type is a primitive data type

SDO_Model_Type::isDataType

(^)

SDO_Model_Type::isDataType — Test to see if this SDO_Model_Type is a primitive data type

설명

bool SDO_Model_Type::isDataType ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Test to see if this SDO_Model_Type is a primitive data type. Returns TRUE if this type is a primitive data type, otherwise returns FALSE.

인수

None.

반환값

Returns TRUE if this type is a primitive data type, otherwise returns FALSE.

User Contributed Notes

There are no user contributed notes for this page.


Test for an SDO_DataObject being an instance of this SDO_Model_Type

  • ≪ SDO_Model_Type::isDataType
  • SDO_Model_Type::isOpenType ≫
  • PHP Manual
  • SDO 함수 목록
  • Test for an SDO_DataObject being an instance of this SDO_Model_Type

SDO_Model_Type::isInstance

(^)

SDO_Model_Type::isInstance — Test for an SDO_DataObject being an instance of this SDO_Model_Type

설명

bool SDO_Model_Type::isInstance ( SDO_DataObject $data_object )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Test for an SDO_DataObject being an instance of this SDO_Model_Type. Returns TRUE if the SDO_DataObject provided is an instance of this SDO_Model_Type, or a derived type, otherwise returns FALSE.

인수

data_object

The SDO_DataObject to be tested.

반환값

Returns TRUE if the SDO_DataObject provided is an instance of this SDO_Model_Type, or a derived type, otherwise returns FALSE.

User Contributed Notes

There are no user contributed notes for this page.


Test to see if this type is an open type

  • ≪ SDO_Model_Type::isInstance
  • SDO_Model_Type::isSequencedType ≫
  • PHP Manual
  • SDO 함수 목록
  • Test to see if this type is an open type

SDO_Model_Type::isOpenType

(^)

SDO_Model_Type::isOpenType — Test to see if this type is an open type

설명

bool SDO_Model_Type::isOpenType ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Test to see if this type is open. Returns TRUE if this type is open, otherwise returns FALSE. An SDO_DataObject whose type is open can have properties added to them which are not described by the type. This capability is used to support working with XML documents whose schema support open content, such as that defined by an

<xsd:any>
element.

인수

None.

반환값

Returns TRUE if this type is open, otherwise returns FALSE.

User Contributed Notes

There are no user contributed notes for this page.


Test to see if this is a sequenced type

  • ≪ SDO_Model_Type::isOpenType
  • SDO_Sequence::getProperty ≫
  • PHP Manual
  • SDO 함수 목록
  • Test to see if this is a sequenced type

SDO_Model_Type::isSequencedType

(^)

SDO_Model_Type::isSequencedType — Test to see if this is a sequenced type

설명

bool SDO_Model_Type::isSequencedType ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Test to see if this is a sequenced type. Returns TRUE if this type is sequence, otherwise returns FALSE. Sequenced types can have the ordering across properties preserved and can contain unstructured text. For more information on sequenced types see the section on Working with Sequenced Data Objects.

인수

None.

반환값

Returns TRUE if this type is sequence, otherwise return FALSE.

User Contributed Notes

There are no user contributed notes for this page.


Return the property for the specified sequence index.

  • ≪ SDO_Model_Type::isSequencedType
  • SDO_Sequence::insert ≫
  • PHP Manual
  • SDO 함수 목록
  • Return the property for the specified sequence index.

SDO_Sequence::getProperty

(^)

SDO_Sequence::getProperty — Return the property for the specified sequence index.

설명

SDO_Model_Property SDO_Sequence::getProperty ( int $sequence_index )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Return the property for the specified sequence index.

인수

sequence_index

The position of the element in the sequence.

반환값

An SDO_Model_Property. A return value of NULL means the sequence element does not belong to a property and must therefore be unstructured text.

오류/예외

SDO_IndexOutOfBoundsException

Thrown if the sequence index is less than zero or greater than the size of the sequence.

User Contributed Notes

There are no user contributed notes for this page.


Insert into a sequence

  • ≪ SDO_Sequence::getProperty
  • SDO_Sequence::move ≫
  • PHP Manual
  • SDO 함수 목록
  • Insert into a sequence

SDO_Sequence::insert

(^)

SDO_Sequence::insert — Insert into a sequence

설명

void SDO_Sequence::insert ( mixed $value [, int $sequenceIndex [, mixed $propertyIdentifier ]] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Insert a new element at a specified position in the sequence. All subsequent sequence items are moved up.

인수

value

The new value to be inserted. This can be either a primitive or an SDO_DataObject.

sequenceIndex

The position at which to insert the new element. Default is NULL, which results in the new value being appended to the sequence.

propertyIdentifier

Either a property index, a property name, or an SDO_Model_Property, used to identify a property in the sequence's corresponding SDO_DataObject. A value of NULL signifies unstructured text.

반환값

None.

오류/예외

SDO_IndexOutOfBoundsException

Thrown if the sequence index is less than zero or greater than the size of the sequence.

SDO_InvalidConversionException

Thrown if the type of the new value cannot be juggled to match the type for the specified data object property.

User Contributed Notes

There are no user contributed notes for this page.


Move an item to another sequence position

  • ≪ SDO_Sequence::insert
  • SDO-DAS-Relational ≫
  • PHP Manual
  • SDO 함수 목록
  • Move an item to another sequence position

SDO_Sequence::move

(^)

SDO_Sequence::move — Move an item to another sequence position

설명

void SDO_Sequence::move ( int $toIndex, int $fromIndex )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Modify the position of the item in the sequence, without altering the value of the property in the SDO_DataObject.

인수

toIndex

The destination sequence index. If this index is less than zero or greater than the size of the sequence then the value is appended.

fromIndex

The source sequence index.

반환값

None.

오류/예외

SDO_IndexOutOfBoundsException

Thrown if the fromIndex sequence index is less than zero or greater than the size of the sequence.

User Contributed Notes

There are no user contributed notes for this page.


SDO Relational Data Access Service

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
    • Creating, retrieving, updating and deleting data
    • Specifying the metadata
    • One-table examples
    • Two-table examples
    • Three-table example
  • Limitations
  • SDO-DAS-Relational 함수 목록
    • SDO_DAS_Relational::applyChanges — Applies the changes made to a data graph back to the database.
    • SDO_DAS_Relational::__construct — Creates an instance of a Relational Data Access Service
    • SDO_DAS_Relational::createRootDataObject — Returns the special root object in an otherwise empty data graph. Used when creating a data graph from scratch.
    • SDO_DAS_Relational::executePreparedQuery — Executes an SQL query passed as a prepared statement, with a list of values to substitute for placeholders, and return the results as a normalised data graph.
    • SDO_DAS_Relational::executeQuery — Executes a given SQL query against a relational database and returns the results as a normalised data graph.

User Contributed Notes

There are no user contributed notes for this page.


소개

Warning

이 확장 모듈은 실험적입니다. 이 확장 모듈의 작동(함수의 이름과 이 확장 모듈에 관련된 모든 문서 자료)은 이후의 PHP 릴리즈에서 예고 없이 변경될 수 있습니다. 이 확장 모듈의 사용은 사용자의 책임하에 이루어집니다.

In order to use the Relational Data Access Service for Service Data Objects, you will need to understand some of the concepts behind SDO: the data graph, the data object, the disconnected way of working, the change summary, XPath and property expressions, and so on. If you are not familiar with these ideas, you might want to look first at the section on SDO. In addition, the Relational DAS makes use of the PDO extension to isolate itself from specifics of different back-end relational databases. In order to use the Relational DAS you will need to be able to create and pass a PDO database connection; for this reason you might also want to take a look at the section on PDO.

The job of the Relational DAS is to move data between the application and a relational database. In order to do this it needs to be told the mapping between the database entities - tables, columns, primary keys and foreign keys - and the elements of the SDO model - types, properties, containment relationships and so on. You specify this information as metadata when you construct the Relational DAS.

Overview of Operation

  1. The first step is to call the Relational DAS's constructor, passing the metadata that defines the mapping between database and SDO model. There are examples of this below.

  2. The next step might be to call the executeQuery() or executePreparedQuery() methods on the Relational DAS, passing either a literal SQL statement for the DAS to prepare and execute, or a prepared statement with placeholders and a list of values to be inserted. You may also need to specify a small amount of metadata about the query itself, so that the Relational DAS knows exactly what columns will be returned from the database and in what order. You will also need to pass a PDO database connection.

    The return value from executeQuery() or executePreparedQuery() is a normalised data graph containing all the data from the result set. For a query that returns data obtained from a number of tables, this graph will contain a number of data objects, linked by SDO containment relationships. There may also be SDO non-containment references within the data.

    Once the query has been executed and the data graph constructed, there is no need for either that instance of the Relational DAS or the database connection. There are no locks held on the database. Both the Relational DAS and the PDO database connection can be garbage collected.

  3. Quite possibly the data in the data graph will go through a number of modifications. The data graph can be serialised into the PHP session and so may have a lifetime beyond just one client-server interaction. Data objects can be created and added to the graph, the data objects already in the graph can be deleted, and data objects in the graph can be modified.

  4. Finally, the changes made to the data graph can be applied back to the database using the applyChanges() method of the Relational DAS. For this, another instance of the Relational DAS must be constructed, using the same metadata, and another connection to the database obtained. The connection and the data graph are passed to applyChanges(). At this point the Relational DAS examines the change summary and generates the necessary INSERT, UPDATE and DELETE SQL statements to apply the changes. Any UPDATE and DELETE statements are qualified with the original values of the data so that should the data have changed in the database in the meantime this will be detected. Assuming no such collisions have occurred the changes will be committed to the database. The application can then continue to work with the data graph, make more changes and apply them, or can discard it.

There are other ways of working with the data in the database: it is possible to just create data objects and write them to the database without a preliminary call to executeQuery(), for example. This scenario and others are explored in the Examples section below.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

The Relational DAS requires that the SDO extension be installed. The SDO extension requires a version of PHP 5.1, and the Relational DAS requires a recent version that contains an important fix for PDO. The most up-to-date information about required levels of PHP should be found in the changelog for the package on PECL. At the time of writing, though, the Relational DAS requires the most recent beta level of PHP 5.1, that is PHP 5.1.0.

The Relational DAS uses PDO to access the relational database, and so should run with a variety of different relational databases. At the time of writing it has been tested in the following configurations

  • MySQL 4.1.14, on Windows. The Relational DAS operates correctly with the php_pdo_mysql driver that comes with the pre-built binaries for PHP 5.1.0.

  • MySQL 4.1.13, on Linux. It is necessary to have the most up-to-date PDO driver for MySQL, which comes built in to PHP 5.1.0. It may be necessary to uninstall the usual driver that would have come from PECL using pear uninstall pdo_mysql. You will need to configure PHP with the --with-pdo-mysql option.

  • DB2 8.2 Personal Edition, on Windows. The Relational DAS operates correctly with the php_pdo_odbc driver that comes with the pre-built binaries for PHP 5.1.0.

  • DB2 8.2 Personal Developer's Edition, on Linux. The Developer's Edition is needed because it contains the include files needed when PHP is configured and built. You will need to configure PHP with the --with-pdo-odbc=ibm-db2 option.

The Relational DAS applies changes to the database within a user-delimited transaction: that is, it issues a call to PDO::beginTransaction() before beginning to apply changes, and PDO::commit() or PDO::rollback() on completion. Whichever database is chosen, the database and the PDO driver for the database must support these calls.

User Contributed Notes

There are no user contributed notes for this page.


설치

The installation instructions for all the SDO components are in the SDO install section of the SDO documentation.

In any case, the essential facts are that the Relational DAS is written in PHP and it should be placed somewhere on the PHP include_path.

Your application will of course need to include the Relational DAS with a statement like this:

<?php
require_once 'SDO/DAS/Relational.php';
?>

User Contributed Notes

There are no user contributed notes for this page.


실행시 설정

이 확장은 php.ini 설정이 존재하지 않습니다.

Tracing

You may be interested in seeing the SQL statements that are generated in order to apply changes back to the database. At the top of the SDO/DAS/Relational.php you will find a number of constants which control whether the process of constructing and executing the SQL statements is to be traced. Try setting DEBUG_EXECUTE_PLAN to TRUE to see the generated SQL statements.

User Contributed Notes

There are no user contributed notes for this page.


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

The Relational DAS introduces no predefined constants.

User Contributed Notes

There are no user contributed notes for this page.


예제

Table of Contents

  • Creating, retrieving, updating and deleting data
  • Specifying the metadata
  • One-table examples
  • Two-table examples
  • Three-table example

User Contributed Notes

There are no user contributed notes for this page.


Creating, retrieving, updating and deleting data

This section illustrates how the Relational DAS can be used to create, retrieve, update and delete data in a relational database. Many of the examples are illustrated with a three-table database that contains companies, departments within those companies, and employees that work in those departments. This example is used in a number of places within the SDO literature. See the examples section of the » Service Data Objects specification or the Examples section of the documentation for the SDO extension.

The Relational DAS is constructed with metadata that defines the relational database and how it should be mapped to SDO. The long section that follows describes this metadata and how to construct the Relational DAS. The examples that follow it all assume that this metadata is in an included php file.

The examples below and others can all be found in the Scenarios directory in the Relational DAS package.

The Relational DAS throws exceptions in the event that it finds errors in the metadata or errors when executing SQL statements against the database. For brevity the examples below all omit the use of try/catch blocks around the calls to the Relational DAS.

These examples all differ from the expected use of SDO in two important respects.

First, they show all interactions with the database completed within one script. In this respect these scenarios are not realistic but are chosen to illustrate just the use of the Relational DAS. It is expected that interactions with the database will be separated in time and the data graph serialized and deserialized into the PHP session one or more times as the application interacts with an end user.

Second, all queries executed against the database use hard-coded queries with no variables substituted. In this case it is safe to use the simple executeQuery() call, and this is what the examples illustrate. In practice, though, it is unlikely that the SQL statement is known entirely ahead of time. In order to allow variables to be safely substituted into the SQL queries, without running the risk of injecting SQL with unknown effects, it is safer to use the executePreparedQuery() which takes a prepared SQL statement containing placeholders and a list of values to be substituted.

User Contributed Notes

There are no user contributed notes for this page.


Specifying the metadata

This first long section describes in detail how the metadata describing the database and the required SDO model is supplied to the Relational DAS.

When the constructor for the Relational DAS is invoked, it needs to be passed several pieces of information. The bulk of the information, passed as an associative array in the first argument to the constructor, tells the Relational DAS what it needs to know about the relational database. It describes the names of the tables, columns, primary keys and foreign keys. It should be fairly easy to understand what is required, and once written it can be placed in a php file and included when needed. The remainder of the information, passed in the second and third arguments to the constructor, tells the Relational DAS what it needs to know about the relationships between objects and the shape of the data graph; it ultimately determines how the data from the database is to be normalized into a graph.

Database metadata

The first argument to the constructor describes the target relational database.

Each table is described by an associative array with up to four keys.

Key | Value
name | The name of the table.
columns | An array listing the names of the columns, in any order.
PK | The name of the column containing the primary key.
FK | An array with two entries, 'from' and 'to', which define a column containing a foreign key, and a table to which the foreign key points. If there are no foreign keys in the table then the 'FK' entry does not need to be specified. Only one foreign key can be specified. Only a foreign key pointing to the primary key of a table can be specified.

<?php
/*****************************************************************
* METADATA DEFINING THE DATABASE
******************************************************************/
$company_table = array (
  'name' => 'company',
  'columns' => array('id', 'name',  'employee_of_the_month'),
  'PK' => 'id',
  'FK' => array (
      'from' => 'employee_of_the_month',
      'to' => 'employee',
      ),
  );
$department_table = array (
  'name' => 'department',
  'columns' => array('id', 'name', 'location', 'number', 'co_id'),
  'PK' => 'id',
  'FK' => array (
      'from' => 'co_id',
      'to' => 'company',
      )
  );
$employee_table = array (
  'name' => 'employee',
  'columns' => array('id', 'name', 'SN', 'manager', 'dept_id'),
  'PK' => 'id',
  'FK' => array (
      'from' => 'dept_id',
      'to' => 'department',
      )
  );
$database_metadata = array($company_table, $department_table, $employee_table);
?>

This metadata corresponds to a relational database that might have been defined to MySQL as:

create table company (
 id integer auto_increment,
 name char(20),
 employee_of_the_month integer,
 primary key(id)
);
create table department (
 id integer auto_increment,
 name char(20),
 location char(10),
 number integer(3),
 co_id integer,
 primary key(id)
);
create table employee (
 id integer auto_increment,
 name char(20),
 SN char(4),
 manager tinyint(1),
 dept_id integer,
 primary key(id)
);

or to DB2 as:

create table company ( \
  id integer not null generated by default as identity,  \
  name varchar(20), \
  employee_of_the_month integer, \
  primary key(id) )
create table department ( \
  id integer not null generated by default as identity, \
  name varchar(20), \
  location varchar(10), \
  number integer, \
  co_id integer, \
  primary key(id) )
create table employee ( \
  id integer not null generated by default as identity, \
  name varchar(20), \
  SN char(4), \
  manager smallint, \
  dept_id integer, \
  primary key(id) )

Note that although in this example there are no foreign keys specified to the database and so the database is not expected to enforce referential integrity, the intention behind the co_id column on the department table and the dept_id column on the employee table is they should contain the primary key of their containing company or department record, respectively. So these two columns are acting as foreign keys.

There is a third foreign key in this example, that from the employee_of_the_month column of the company record to a single row of the employee table. Note the difference in intent between this foreign key and the other two. The employee_of_the_month column represents a single-valued relationship: there can be only one employee of the month for a given company. The co_id and dept_id columns represent multi-valued relationships: a company can contain many departments and a department can contain many employees. This distinction will become evident when the remainder of the metadata picks out the company-department and department-employee relationships as containment relationships.

There are a few simple rules to be followed when constructing the database metadata:

  • All tables must have primary keys, and the primary keys must be specified in the metadata. Without primary keys it is not possible to keep track of object identities. As you can see from the SQL statements that create the tables, primary keys can be auto-generated, that is, generated and assigned by the database when a record is inserted. In this case the auto-generated primary key is obtained from the database and inserted into the data object immediately after the row is inserted into the database.

  • It is not necessary to specify in the metadata all the columns that exist in the database, only those that will be used. For example, if the company table had another column that the application did not want to access with SDO, this need not be specified in the metadata. On the other hand it would have done no harm to specify it: if specified in the metadata but never retrieved, or assigned to by the application, then the unused column will not affect anything.

  • In the database metadata note that the foreign key definitions identify not the destination column in the table which is pointed to, but the table name itself. Strictly, the relational model permits the destination of a foreign key to be a non-primary key. Only foreign keys that point to a primary key are useful for constructing the SDO model, so the metadata specifies the table name. It is understood that the foreign key points to the primary key of the given table.

Given these rules, and given the SQL statements that define the database, the database metadata should be easy to construct.

What the Relational DAS does with the metadata

The Relational DAS uses the database metadata to form most of the SDO model. For each table in the database metadata, an SDO type is defined. Each column which can represent a primitive value (columns which are not defined as foreign keys) are added as properties to the SDO type.

All primitive properties are given a type of string in the SDO model, regardless of their SQL type. When writing values back to the database the Relational DAS will create SQL statements that treat the values as strings, and the database will convert them to the appropriate type.

Foreign keys are interpreted in one of two ways, depending on the metadata in the third argument to the constructor that defines the SDO containment relationships. A discussion of this is therefore deferred until the section on SDO containment relationships below.

Specifying the application root type

The second argument to the constructor is the application root type. The true root of each data graph is an object of a special root type and all application data objects come somewhere below that. Of the various application types in the SDO model, one has to be the application type immediately below the root of the data graph. If there is only one table in the database metadata, the application root type can be inferred, and this argument can be omitted.

Specifying the SDO containment relationships

The third argument to the constructor defines how the types in the model are to be linked together to form a graph. It identifies the parent-child relationships between the types which collectively form a graph. The relationships need to be supported by foreign keys to be found in the data, in a way shortly to be described.

The metadata is an array containing one or more associative arrays, each of which identifies a parent and a child. The example below shows a parent-child relationship from company to department, and another from department to employee. Each of these will become an SDO property defining a multi-valued containment relationship in the SDO model.

<?php
$department_containment = array( 'parent' => 'company', 'child' => 'department');
$employee_containment = array( 'parent' => 'department', 'child' => 'employee');

$SDO_containment_metadata = array($department_containment, $employee_containment);
?>

Foreign keys in the database metadata are interpreted as properties with either multi-valued containment relationships or single-valued non-containment references, depending on whether they have a corresponding SDO containment relationship specified in the metadata. In the example here, the foreign keys from department to company (the co_id column in the department table) and from employee to department (the dept_id column in the employee table) are interpreted as supporting the SDO containment relationships. Each containment relationship mentioned in the SDO containment relationships metadata must have a corresponding foreign key present in the database and defined in the database metadata. The values of the foreign key columns for containment relationships do not appear in the data objects, instead each is represented by a containment relationship from the parent to the child. So the co_id column in the department row in the database, for example, does not appear as a property on the department type, but instead as a containment relationship called department on the company type. Note that the foreign key and the parent-child relationship appear to have opposite senses: the foreign key points from the department to the company, but the parent-child relationship points from company to department.

The third foreign key in this example, the employee_of_the_month , is handled differently. This is not mentioned in the SDO containment relationships metadata. As a consequence this is interpreted in the second way: it becomes a single-valued non-containment reference on the company object, to which can be assigned references to SDO data objects of the employee type. It does appear as a property on the company type. The way to assign a value to it in the SDO data graph is to have a graph that contains an employee object through the containment relationships, and to assign the object to it. This is illustrated in the later examples below.

User Contributed Notes

There are no user contributed notes for this page.


One-table examples

The following set of examples all use the Relational DAS to work with a data graph containing just one application data object, a single company and the data just to be found the company table. These examples do not exercise the power of SDO or the Relational DAS and of course the same result could be achieved more economically with direct SQL statements but they are intended to illustrate how to work with the Relational DAS.

For this very simple scenario it would be possible to simplify the database metadata to include just the company table - if that were done the second and third arguments to the constructor and the column specifier used in the query example would become optional.

Example #1 Creating a data object

The simplest example is that of creating a single data object and writing it to the database. In this example a single company object is created, its name is set to 'Acme', and the Relational DAS is called to write the changes to the database. The company name is set here using the property name method. See the Examples section on the SDO extension for other ways of accessing the properties of an object.

Data objects can only be created when you have a data object to start with, however. It is for that reason that the first call to the Relational DAS here is to obtain a root object. This is in effect how to ask for an empty data graph - the special root object is the true root of the tree. The company data object is then created with a call to createDataObject() on the root object. This creates the company data object and inserts it in the graph by inserting into a multi-valued containment property on the root object called 'company'.

When the Relational DAS is called to apply the changes a simple insert statement 'INSERT INTO company (name) VALUES ("Acme");' will be constructed and executed. The auto-generated primary key will be set into the data object and the change summary will be reset, so that it would be possible to continue working with the same data object, modify it, and apply the newer changes a second time.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/**************************************************************
* Construct the DAS with the metadata
***************************************************************/
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_containment_metadata);

/**************************************************************
* Obtain a root object and create a company object underneath.
* Make a simple change to the data object.
***************************************************************/
$root = $das  -> createRootDataObject();
$acme = $root -> createDataObject('company');

$acme->name = "Acme";

/**************************************************************
* Get a database connection and write the object to the database
***************************************************************/
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);
$das -> applyChanges($dbh, $root);
?>

Example #2 Retrieving a data object

In this example a single data object is retrieved from the database - or possibly more than one if there is more than one company called 'Acme'. For each company returned, the name and id properties are echoed.

In this example the third argument to executeQuery(), the column specifier is needed as there are other tables in the metadata with column names of name and id. If there were no possible ambiguity it could be omitted.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/**************************************************************
* Construct the DAS with the metadata
***************************************************************/
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_containment_metadata);

/**************************************************************
* Get a database connection
***************************************************************/
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);

/**************************************************************
* Issue a query to obtain a company object - possibly more if they exist
***************************************************************/
$root = $das->executeQuery($dbh,
         'select name, id from company where name="Acme"',
          array('company.name', 'company.id') );

/**************************************************************
* Echo name and id
***************************************************************/
foreach ($root['company'] as $company) {
  echo "Company obtained from the database has name = " .
  $company['name'] . " and id " . $company['id'] . "\n";
}
?>

Example #3 Updating a data object

This example combines the previous two, in the sense that in order to be updated the object must first be retrieved. The application code reverses the company name (so 'Acme' becomes 'emcA') and then the changes are written back to the database in the same way that they were when the object was created. Because the query searches for the name both ways round the program can be run repeatedly to find the company and reverse its name each time.

In this example the same instance of the Relational DAS is reused for the applyChanges(), as is the PDO database handle. This is quite alright; it also alright to allow the previous instances to be garbage collected and to obtain new instances. No state data regarding the graph is held the Relational DAS once it has returned a data graph to the application. All necessary data is either within the graph itself, or can be reconstructed from the metadata.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/**************************************************************
* Construct the DAS with the metadata
***************************************************************/
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_containment_metadata);

/**************************************************************
* Get a database connection
***************************************************************/
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);

/**************************************************************
* Issue a query to obtain a company object - possibly more if they exist
***************************************************************/
$root = $das->executeQuery($dbh,
  'select name, id from company where name="Acme" or name="emcA"',
  array('company.name', 'company.id') );

/**************************************************************
* Alter the name of just the first company
***************************************************************/
$company = $root['company'][0];
echo "obtained a company with name of " . $company->name . "\n";
$company->name = strrev($company->name);

/**************************************************************
* Write the change back
***************************************************************/
$das->applyChanges($dbh,$root);
?>

Example #4 Deleting a data object

Any companies called 'Acme' or its reverse 'emcA' are retrieved. They are then all deleted from the graph with unset.

In this example they are all deleted in one go by unsetting the containing property (the property defining the containment relationship). It is also possible to delete them individually.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/**************************************************************
* Construct the DAS with the metadata
***************************************************************/
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_containment_metadata);

/**************************************************************
* Get a database connection
***************************************************************/
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);

/**************************************************************
* Issue a query to obtain a company object - possibly more if they exist
***************************************************************/
$root = $das->executeQuery($dbh,
  'select name, id from company where name="Acme" or name="emcA"',
  array('company.name', 'company.id') );

/**************************************************************
* Delete any companies found from the data graph
***************************************************************/
unset($root['company']);

/**************************************************************
* Write the change(s) back
***************************************************************/
$das->applyChanges($dbh,$root);
?>

User Contributed Notes

There are no user contributed notes for this page.


Two-table examples

The following set of examples all use two tables from the company database: the company and department tables. These examples exercise more of the function of the Relational DAS.

In this series of examples a company and department are created, retrieved, updated, and finally deleted. This illustrates the lifecycle for a data graph containing more than one object. Note that this example clears out the company and department tables at the start so that the exact results of the queries can be known.

You can find these examples combined into one script called 1cd-CRUD in the Scenarios directory in the Relational DAS package.

Example #1 One company, one department - Create

As in the earlier example of creating just one company data object, the first action after constructing the Relational DAS is to call createRootDataObject() to obtain the special root object of the otherwise empty data graph. The company object is then created as a child of the root object, and the department object as a child of the company object.

When it comes to applying the changes, the Relational DAS has to perform special processing to maintain the foreign keys that support the containment relationships, especially if auto-generated primary keys are involved. In this example, the relationship between the auto-generated primary key id in the company table and the co_id column in the department table must be maintained. When inserting a company and department for the first time the Relational DAS has to first insert the company row, then call PDO's getLastInsertId() method to obtain the auto-generated primary key, then add that as the value of the co_id column when inserting the department row.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/*************************************************************************************
* Empty out the two tables
*************************************************************************************/
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);
$pdo_stmt = $dbh->prepare('DELETE FROM COMPANY;');
$rows_affected = $pdo_stmt->execute();
$pdo_stmt = $dbh->prepare('DELETE FROM DEPARTMENT;');
$rows_affected = $pdo_stmt->execute();

/**************************************************************
* Create a company with name Acme and one department, the Shoe department
***************************************************************/
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_containment_metadata);

$root = $das -> createRootDataObject();

$acme = $root -> createDataObject('company');
$acme -> name = "Acme";

$shoe = $acme->createDataObject('department');
$shoe->name = 'Shoe';

$das -> applyChanges($dbh, $root);

?>

Example #2 One company, one department - Retrieve and Update

In this case the SQL query passed to executeQuery() performs an inner join to join the data from the company and department tables. Primary keys for both the company and department tables must be included in the query. The result set is re-normalised to form a normalised data graph. Note that a column specifier is passed as the third argument to the executeQuery() call enabling the Relational DAS to know which column is which in the result set.

Note that the co_id column although used in the query is not needed in the result set. In order to understand what the Relational DAS is doing when it builds the data graph it may be helpful to visualise what the result set looks like. Although the data in the database is normalised, so that multiple department rows can point through their foreign key to one company row, the data in the result set is non-normalised: that is, if there is one company and multiple departments, the values for the company are repeated in each row. The Relational DAS has to reverse this process and turn the result set back into a normalised data graph, with just one company object.

In this example the Relational DAS will examine the result set and column specifier, find data for both the company and department tables, find primary keys for both, and interpret each row as containing data for a department and its parent company. If it has not seen data for that company before (it uses the primary key to check) it creates a company object and then a department object underneath it. If it has seen data for that company before and has already created the company object it just creates the department object underneath.

In this way the Relational DAS can retrieve and renormalise data for multiple companies and multiple departments underneath them.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/**************************************************************
* Retrieve the company and Shoe department, then delete Shoe and add IT
***************************************************************/
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_containment_metadata);

$root = $das->executeQuery($dbh,
'select c.id, c.name, d.id, d.name from company c, department d where d.co_id = c.id',
array('company.id','company.name','department.id','department.name'));

$acme = $root['company'][0];            // get the first company - will be 'Acme'
$shoe = $acme['department'][0];         // get the first department underneath - will be 'Shoe'

unset($acme['department'][0]);

$it = $acme->createDataObject('department');
$it->name = 'IT';

$das -> applyChanges($dbh, $root);
?>

Example #3 One company, two departments - Retrieve and Delete

In this example the company and department are retrieved and then deleted. It is not necessary to delete them individually (although that would be possible) - deleting the company object from the data graph also deletes any departments underneath it.

Note the way that the company object is actually deleted using the PHP unset call. The unset has to be performed on the containing property which in this case is the company property on the special root object. You must use:

<?php
unset($root['company'][0]);
?>
and not:
<?php
unset($acme); //WRONG
?>
Simply unsetting $acme would destroy the variable but leave the data in the data graph untouched.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/**************************************************************
* Retrieve the company and IT department, then delete the whole company
***************************************************************/
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_containment_metadata);

$root = $das->executeQuery($dbh,
'select c.id, c.name, d.id, d.name from company c, department d where d.co_id = c.id',
array('company.id','company.name','department.id','department.name'));

$acme = $root['company'][0];
$it = $acme['department'][0];

unset($root['company'][0]);

$das -> applyChanges($dbh, $root);

?>

User Contributed Notes

There are no user contributed notes for this page.


Three-table example

The following examples use all three tables from the company database: the company, department, and employee tables. These introduce the final piece of function not exercised by the examples above: the non-containment reference employee_of_the_month.

Like the examples above for company and department, this set of examples is intended to illustrate the full lifecycle of such a data graph.

Example #1 One company, one department, one employee - Create

In this example a company is created containing one department and just one employee. Note that this example clears out all three tables at the start so that the exact results of the queries can be known.

Note how once the company, department and employee have been created, the employee_of_the_month property of the company can be made to point at the new employee. As this is a non-containment reference, this cannot be done until the employee object has been created within the graph. Non-containment references need to be managed carefully. For example if the employee were now deleted from under the department, it would not be correct to try to save the graph without first clearing or re-assigning the employee_of_the_month property. The closure rule for SDO data graphs requires that any object pointed at by a non-containment reference must also be reachable by containment relationships.

When it comes to inserting the graph into the database, the procedure is similar to the example of inserting the company and department, but employee_of_the_month introduces an extra complexity. The Relational DAS needs to insert the objects working down the tree formed by containment relationships, so company, then department, then employee. This is necessary so that it always has the auto-generated primary key of a parent on hand to include in a child row. But when the company row is inserted the employee who is employee of the month has not yet been inserted and the primary key is not known. The procedure is that after the employee record is inserted and its primary key known, a final step is performed in which the company record is updated with the employee's primary key.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/*************************************************************************************
* Empty out the three tables
*************************************************************************************/
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);
$pdo_stmt = $dbh->prepare('DELETE FROM COMPANY;');
$rows_affected = $pdo_stmt->execute();
$pdo_stmt = $dbh->prepare('DELETE FROM DEPARTMENT;');
$rows_affected = $pdo_stmt->execute();
$pdo_stmt = $dbh->prepare('DELETE FROM EMPLOYEE;');
$rows_affected = $pdo_stmt->execute();

/*************************************************************************************
* Create a tiny but complete company.
* The company name is Acme.
* There is one department, Shoe.
* There is one employee, Sue.
* The employee of the month is Sue.
*************************************************************************************/
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_containment_metadata);
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);

$root             = $das  -> createRootDataObject();
$acme             = $root -> createDataObject('company');
$acme -> name     = "Acme";
$shoe             = $acme -> createDataObject('department');
$shoe -> name     = 'Shoe';
$shoe -> location = 'A-block';
$sue              = $shoe -> createDataObject('employee');
$sue -> name      = 'Sue';
$acme -> employee_of_the_month = $sue;

$das -> applyChanges($dbh, $root);

echo "Wrote back Acme with one department and one employee\n";
?>

Example #2 One company, one department, one employee - Retrieve and update

The SQL statement passed to the Relational DAS is this time an inner join that retrieves data from all three tables. Otherwise this example introduces nothing that has not appeared in a previous example.

The graph is updated by the addition of a new department and employee and some alterations to the name properties of the existing objects in the graph. The combined changes are then written back. The Relational DAS will process and apply an arbitrary mixture of additions, modifications and deletions to and from the data graph.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/*************************************************************************************
* Find the company again and change various aspects.
* Change the name of the company, department and employee.
* Add a second department and a new employee.
* Change the employee of the month.
*************************************************************************************/
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_containment_metadata);
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);

$root = $das->executeQuery($dbh,
  "select c.id, c.name, c.employee_of_the_month, d.id, d.name, e.id, e.name " .
  "from company c, department d, employee e " .
  "where e.dept_id = d.id and d.co_id = c.id and c.name='Acme'",
   array('company.id','company.name','company.employee_of_the_month',
   'department.id','department.name','employee.id','employee.name'));
$acme         = $root['company'][0];

$shoe         = $acme->department[0];
$sue          = $shoe -> employee[0];

$it           = $acme->createDataObject('department');
$it->name     = 'IT';
$it->location = 'G-block';
$billy        = $it->createDataObject('employee');
$billy->name  = 'Billy';

$acme->name   = 'MegaCorp';
$shoe->name   = 'Footwear';
$sue->name    = 'Susan';

$acme->employee_of_the_month = $billy;
$das -> applyChanges($dbh, $root);
echo "Wrote back company with extra department and employee and all the names changed (Megacorp/Footwear/Susan)\n";

?>

Example #3 One company, two departments, two employees - Retrieve and delete

The company is retrieved as a complete data graph containing five data objects - the company, two departments and two employees. They are all deleted by deleting the company object. Deleting an object from the graph deletes all the object beneath it in the graph. Five SQL DELETE statements will be generated and executed. As always they will be qualified with a WHERE clause that contains all of the fields that were retrieved, so that any updates to the data in the database in the meantime by another process will be detected.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/*************************************************************************************
* Now read it one more time and delete it.
* You can delete part, apply the changes, then carry on working with the same graph but
* care is needed to keep closure - you cannot delete the employee who is eotm without
* reassigning. For safety here we delete the company all in one go.
*************************************************************************************/
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_containment_metadata);
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);

$root = $das->executeQuery($dbh,
  "select c.id, c.name, c.employee_of_the_month, d.id, d.name, e.id, e.name " .
  "from company c, department d, employee e " .
  "where e.dept_id = d.id and d.co_id = c.id and c.name='MegaCorp';",
   array('company.id','company.name','company.employee_of_the_month',
   'department.id','department.name','employee.id','employee.name'));
$megacorp = $root['company'][0];

unset($root['company']);
$das -> applyChanges($dbh, $root);

echo "Deleted the company, departments and employees all in one go.\n";

?>

User Contributed Notes

There are no user contributed notes for this page.


Limitations

There are the following limitations in the current release of the Relational DAS:

  • No support for nulls. There is no support for SQL NULL type. It is not legal to assign PHP NULL to a data object property and the Relational DAS will not write that back as a NULL to the database. If nulls are found in the database on a query, the property will remain unset.

  • Only two types of SDO relationship. The metadata described below allows the Relational DAS to model just two types of SDO relationship: multi-valued containment relationships and single-valued non-containment references. In SDO, whether a property describes a single- or multi-valued relationship, and whether it is containment or non-containment, are independent. The full range of possibilities that SDO allows cannot all be defined. There may be relationships that it would be useful to model but which the current implementation cannot manage. One example is a single-valued containment relationship.

  • No support for the full range of SDO data types. The Relational DAS defines all primitive properties in the SDO model as being of type string. SDO defines a richer set of types containing various integer, float, boolean and data and time types. String is adequate for the purposes of the Relational DAS since the combination of PHP, PDO and the database will ensure that values passed as strings will be converted to the proper type before being put in the database. This does affect some scenarios in which the Relational DAS has to work with a data graph that has come from or will go to a different DAS.

  • Only one foreign key per table. The metadata only provides the means to specify one foreign key per table. This foreign key may be mapped to one of the two types of SDO relationship supported. Obviously there are some scenarios that cannot be described under this limitation - it is not possible to have two non-containment references from one table to another for example.

User Contributed Notes

There are no user contributed notes for this page.


SDO-DAS-Relational 함수 목록

예약 클래스

The Relational DAS provides two classes: the Relational DAS itself and the subclass of Exception that can be thrown. The Relational DAS has four publicly useful calls: the constructor, the createRootDataObject() call to obtain the root object of an empty data graph, the executeQuery() call to obtain a data graph containing data from a relational database, and the applyChanges() call to write changes made to a data graph back to the relational database.

SDO_DAS_Relational

The only object other than an SDO_DAS_Relational_Exception with which the application is expected to interact.

메쏘드

  • __construct - construct the Relational DAS with a model derived from the passed metadata

  • createRootDataObject - obtain an otherwise empty data graph containing just the special root object

  • executeQuery - execute an SQL query passed as a literal string and return the results as a normalised data graph

  • executePreparedQuery - execute an SQL query passed as a prepared statement, with a list of values to substitute for placeholders, and return the results as a normalised data graph

  • applyChanges - examine the change summary in the data graph and apply those changes back to the database, subject to an assumption of optimistic concurrency

SDO_DAS_Relational_Exception

Is a subclass of PHP's Exception. It adds no behaviour to Exception. Thrown, with useful descriptive text, to signal errors in the metadata or unexpected failures to perform SQL operations.

Table of Contents

  • SDO_DAS_Relational::applyChanges — Applies the changes made to a data graph back to the database.
  • SDO_DAS_Relational::__construct — Creates an instance of a Relational Data Access Service
  • SDO_DAS_Relational::createRootDataObject — Returns the special root object in an otherwise empty data graph. Used when creating a data graph from scratch.
  • SDO_DAS_Relational::executePreparedQuery — Executes an SQL query passed as a prepared statement, with a list of values to substitute for placeholders, and return the results as a normalised data graph.
  • SDO_DAS_Relational::executeQuery — Executes a given SQL query against a relational database and returns the results as a normalised data graph.

User Contributed Notes

There are no user contributed notes for this page.


Applies the changes made to a data graph back to the database.

  • ≪ SDO-DAS-Relational 함수 목록
  • SDO_DAS_Relational::__construct ≫
  • PHP Manual
  • SDO-DAS-Relational 함수 목록
  • Applies the changes made to a data graph back to the database.

SDO_DAS_Relational::applyChanges

(^)

SDO_DAS_Relational::applyChanges — Applies the changes made to a data graph back to the database.

설명

void SDO_DAS_Relational::applyChanges ( PDO $database_handle , SDODataObject $root_data_object )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Given a PDO database handle and the special root object of a data graph, examine the change summary in the datagraph and applies the changes to the database. The changes that it can apply can be creations of data objects, deletes of data objects, and modifications to properties of data objects.

인수

PDO_database_handle

Constructed using the PDO extension. A typical line to construct a PDO database handle might look like this:

$dbh = new PDO("mysql:dbname=COMPANYDB;host=localhost",DATABASE_USER,DATABASE_PASSWORD);

root_data_object

The special root object which is at the top of every SDO data graph.

반환값

None. Note however that the datagraph that was passed is still intact and usable. Furthermore, if data objects were created and written back to a table with autogenerated primary keys, then those primary keys will now be set in the data objects. If the changes were successfully written, then the change summary associated with the datagraph will have been cleared, so that it is possible to now make further changes to the data graph and apply those changes in turn. In this way it is possible to work with the same data graph and apply changes repeatedly.

오류/예외

SDO_DAS_Relational::applyChanges() can throw an SDO_DAS_Relational_Exception if it is unable to apply all the changes correctly.

The Relational DAS starts a database transaction before beginning to apply the changes and will commit the transaction only if they are all successful. The Relational DAS generates qualified update and delete statements which contain a where clause that specifies that the row to be updated or deleted must contain the same values that it did when the data was first retrieved. This is how the optimistic concurrency is implemented. If any of the qualified update or delete statements fails to update or delete their target row, it may be because the data has been altered in the database in the meantime. In any event, if any update fails for any reason, the transaction is rolled back and an exception thrown. The exception will contain the generated SQL statement that failed.

The Relational DAS also catches any PDO exceptions and obtains PDO diagnostic information which it includes in an SDO_DAS_Relational_Exception which it then throws.

예제

Please see the Examples section in the general information about the Relational DAS for many examples of calling this method. Please see also the section on Tracing to see how you can see what SQL statements are generated by the Relational DAS.

User Contributed Notes

There are no user contributed notes for this page.


Creates an instance of a Relational Data Access Service

  • ≪ SDO_DAS_Relational::applyChanges
  • SDO_DAS_Relational::createRootDataObject ≫
  • PHP Manual
  • SDO-DAS-Relational 함수 목록
  • Creates an instance of a Relational Data Access Service

SDO_DAS_Relational::__construct

(^)

SDO_DAS_Relational::__construct — Creates an instance of a Relational Data Access Service

설명

SDO_DAS_Relational::__construct ( array $database_metadata [, string $application_root_type [, array $SDO_containment_references_metadata ]] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Constructs an instance of a Relational Data Access Service from the passed metadata.

인수

database_metadata

An array containing one or more table definitions, each of which is an associative array containing the keys name, columns, PK, and optionally, FK. For a full discussion of the metadata, see the metadata section in the general information about the Relational DAS.

application_root_type

The root of each data graph is an object of a special root type and the application data objects come below that. Of the various application types in the SDO model, one has to be the application type immediately below the root of the data graph. If there is only one table in the database metadata, so the application root type can be inferred, this argument can be omitted.

SDO_containment_references_metadata

An array containing one or more definitions of a containment relation, each of which is an associative array containing the keys parent and child. The containment relations describe how the types in the model are connected to form a tree. The type specified as the application root type must be present as one of the parent types in the containment references. If the application only needs to work with one table at a time, and there are no containment relations in the model, this argument can be omitted. For a full discussion of the metadata, see the metadata section in the general information about the Relational DAS.

반환값

Returns an SDO_DAS_Relational object on success.

오류/예외

SDO_DAS_Relational::__construct() throws a SDO_DAS_Relational_Exception if any problems are found in the metadata.

예제

For a full discussion of the metadata, see the metadata section in the general information about the Relational DAS.

User Contributed Notes

There are no user contributed notes for this page.


Returns the special root object in an otherwise empty data graph. Used when creating a data graph from scratch.

  • ≪ SDO_DAS_Relational::__construct
  • SDO_DAS_Relational::executePreparedQuery ≫
  • PHP Manual
  • SDO-DAS-Relational 함수 목록
  • Returns the special root object in an otherwise empty data graph. Used when creating a data graph from scratch.

SDO_DAS_Relational::createRootDataObject

(^)

SDO_DAS_Relational::createRootDataObject — Returns the special root object in an otherwise empty data graph. Used when creating a data graph from scratch.

설명

SDODataObject SDO_DAS_Relational::createRootDataObject ( void )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Returns the special root object at the top of an otherwise empty data graph. This call is used when the application wants to create a data graph from scratch, without having called executeQuery() to create a data graph.

The special root object has one multi-valued containment property, with a name of the application root type that was passed when the Relational DAS was constructed. The property can take values of only that type. The only thing that the application can usefully do with the root type is to call createDataObject() on it, passing the name of the application root type, in order to create a data object of their own application type.

인수

None.

반환값

The root object.

오류/예외

None.

예제

Please see the Examples section in the general information about the Relational DAS for many examples of calling this method.

User Contributed Notes

There are no user contributed notes for this page.


Executes an SQL query passed as a prepared statement, with a list of values to substitute for placeholders, and return the results as a normalised data graph.

  • ≪ SDO_DAS_Relational::createRootDataObject
  • SDO_DAS_Relational::executeQuery ≫
  • PHP Manual
  • SDO-DAS-Relational 함수 목록
  • Executes an SQL query passed as a prepared statement, with a list of values to substitute for placeholders, and return the results as a normalised data graph.

SDO_DAS_Relational::executePreparedQuery

(^)

SDO_DAS_Relational::executePreparedQuery — Executes an SQL query passed as a prepared statement, with a list of values to substitute for placeholders, and return the results as a normalised data graph.

설명

SDODataObject SDO_DAS_Relational::executePreparedQuery ( PDO $database_handle , PDOStatement $prepared_statement , array $value_list [, array $column_specifier ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Executes a given query against the relational database, using the supplied PDO database handle. Differs from the simpler executeQuery() in that it takes a prepared statement and a list of values. This is the appropriate call to use either when the statement is to executed a number of times with different arguments, and there is therefore a performance benefit to be had from preparing the statement only once, or when the SQL statement is to contain varying values taken from a source that cannot be completely trusted. In this latter case it may be unsafe to construct the SQL statement by simply concatenating the parts of the statement together, since the values may contain pieces of SQL. To guard against this, a so-called SQL injection attack, it is safer to prepare the SQL statement with placeholders (also known as parameter markers, denoted by '?') and supply a list of the values to be substituted as a separate argument. Otherwise this function is the same as executeQuery() in that it uses the model that it built from the metadata to interpret the result set and returns a data graph.

인수

PDO_database_handle

Constructed using the PDO extension. A typical line to construct a PDO database handle might look like this:

$dbh = new PDO("mysql:dbname=COMPANYDB;host=localhost",DATABASE_USER,DATABASE_PASSWORD);

prepared_statement

A prepared SQL statement to be executed against the database. This will have been prepared by PDO's prepare() method.

value_list

An array of the values to be substituted into the SQL statement in place of the placeholders. In the event that there are no placeholders or parameter markers in the SQL statement then this argument can be specified as NULL or as an empty array;

column_specifier

The Relational DAS needs to examine the result set and for every column, know which table and which column of that table it came from. In some circumstances it can find this information for itself, but sometimes it cannot. In these cases a column specifier is needed, which is an array that identifies the columns. Each entry in the array is simply a string in the form table-name.column_name.

The column specifier is needed when there are duplicate column names in the database metadata, For example, in the database used within the examples, all the tables have both a id and a name column. When the Relational DAS fetches the result set from PDO it can do so with the PDO_FETCH_ASSOC attribute, which will cause the columns in the results set to be labelled with the column name, but will not distinguish duplicates. So this will only work when there are no duplicates possible in the results set.

To summarise, specify a column specifier array whenever there is any uncertainty about which column could be from which table and only omit it when every column name in the database metadata is unique.

All of the examples in the Examples use a column specifier. There is one example in the Scenarios directory of the installation that does not: that which works with just the employee table, and because it works with just one table, there can not exist duplicate column names.

반환값

Returns a data graph. Specifically, it returns a root object of a special type. Under this root object will be the data from the result set. The root object will have a multi-valued containment property with the same name as the application root type specified on the constructor, and that property will contain one or more data objects of the application root type.

In the event that the query returns no data, the special root object will still be returned but the containment property for the application root type will be empty.

오류/예외

SDO_DAS_Relational::executePreparedQuery() can throw an SDO_DAS_Relational_Exception if it is unable to construct the data graph correctly. This can occur for a number of reasons: for example if it finds that it does not have primary keys in the result set for all the objects. It also catches any PDO exceptions and obtains PDO diagnostic information which it includes in an SDO_DAS_Relational_Exception which it then throws.

예제

Example #1 Retrieving a data object using executePreparedQuery()

In this example a single data object is retrieved from the database - or possibly more than one if there is more than one company called 'Acme'. For each company returned, the name and id properties are echoed.

Other examples of the use of executePreparedQuery() can be found in the example code supplied in sdo/DAS/Relational/Scenarios.

<?php
require_once 'SDO/DAS/Relational.php';
require_once 'company_metadata.inc.php';

/**************************************************************
 * Construct the DAS with the metadata
 ***************************************************************/
$das = new SDO_DAS_Relational ($database_metadata,'company',$SDO_reference_metadata);

/**************************************************************
 * Get a database connection
 ***************************************************************/
$dbh = new PDO(PDO_DSN,DATABASE_USER,DATABASE_PASSWORD);

/**************************************************************
 * Issue a query to obtain a company object - possibly more if they exist
 * Use a prepared query with a placeholder.
 ***************************************************************/
$name = 'Acme';
$pdo_stmt = $dbh->prepare('select name, id from company where name=?');
$root = $das->executePreparedQuery(
    $dbh,
    $pdo_stmt,
    array($name),
    array('company.name', 'company.id'));

/**************************************************************
 * Echo name and id
 ***************************************************************/
foreach ($root['company'] as $company) {
    echo "Company obtained from the database has name = " .
    $company['name'] . " and id " . $company['id'] . "\n";
}
?>

User Contributed Notes

There are no user contributed notes for this page.


Executes a given SQL query against a relational database and returns the results as a normalised data graph.

  • ≪ SDO_DAS_Relational::executePreparedQuery
  • SDO DAS XML ≫
  • PHP Manual
  • SDO-DAS-Relational 함수 목록
  • Executes a given SQL query against a relational database and returns the results as a normalised data graph.

SDO_DAS_Relational::executeQuery

(^)

SDO_DAS_Relational::executeQuery — Executes a given SQL query against a relational database and returns the results as a normalised data graph.

설명

SDODataObject SDO_DAS_Relational::executeQuery ( PDO $database_handle , string $SQL_statement [, array $column_specifier ] )
Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

Executes a given query against the relational database, using the supplied PDO database handle. Uses the model that it built from the metadata to interpret the result set. Returns a data graph.

인수

PDO_database_handle

Constructed using the PDO extension. A typical line to construct a PDO database handle might look like this:

$dbh = new PDO("mysql:dbname=COMPANYDB;host=localhost",DATABASE_USER,DATABASE_PASSWORD);

SQL_statement

The SQL statement to be executed against the database.

column_specifier

The Relational DAS needs to examine the result set and for every column, know which table and which column of that table it came from. In some circumstances it can find this information for itself, but sometimes it cannot. In these cases a column specifier is needed, which is an array that identifies the columns. Each entry in the array is simply a string in the form table-name.column_name.

The column specifier is needed when there are duplicate column names in the database metadata. For example, in the database used within the examples, all the tables have both a id and a name column. When the Relational DAS fetches the result set from PDO it can do so with the PDO_FETCH_ASSOC attribute, which will cause the columns in the results set to be labelled with the column name, but will not distinguish duplicates. So this will only work when there are no duplicates possible in the results set.

To summarise, specify a column specifier array whenever there is any uncertainty about which column could be from which table and only omit it when every column name in the database metadata is unique.

All of the examples in the Examples use a column specifier. There is one example in the Scenarios directory of the installation that does not: that which works with just the employee table, and because it works with just one table, there can not exist duplicate column names.

반환값

Returns a data graph. Specifically, it returns a root object of a special type. Under this root object will be the data from the result set. The root object will have a multi-valued containment property with the same name as the application root type specified on the constructor, and that property will contain one or more data objects of the application root type.

In the event that the query returns no data, the special root object will still be returned but the containment property for the application root type will be empty.

오류/예외

SDO_DAS_Relational::executeQuery() can throw an SDO_DAS_Relational_Exception if it is unable to construct the data graph correctly. This can occur for a number of reasons: for example if it finds that it does not have primary keys in the result set for all the objects. It also catches any PDO exceptions and obtains PDO diagnostic information which it includes in an SDO_DAS_Relational_Exception which it then throws.

예제

Please see the Examples section in the general information about the Relational DAS for many examples of calling this method.

User Contributed Notes

There are no user contributed notes for this page.


SDO XML Data Access Service

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
  • SDO DAS XML 함수 목록
    • SDO_DAS_XML::addTypes — To load a second or subsequent schema file to a SDO_DAS_XML object
    • SDO_DAS_XML::create — To create SDO_DAS_XML object for a given schema file
    • SDO_DAS_XML::createDataObject — Creates SDO_DataObject for a given namespace URI and type name
    • SDO_DAS_XML::createDocument — Creates an XML Document object from scratch, without the need to load a document from a file or string.
    • SDO_DAS_XML::loadFile — Returns SDO_DAS_XML_Document object for a given path to xml instance document
    • SDO_DAS_XML::loadString — Returns SDO_DAS_XML_Document for a given xml instance string
    • SDO_DAS_XML::saveFile — Saves the SDO_DAS_XML_Document object to a file
    • SDO_DAS_XML::saveString — Saves the SDO_DAS_XML_Document object to a string
    • SDO_DAS_XML_Document::getRootDataObject — Returns the root SDO_DataObject
    • SDO_DAS_XML_Document::getRootElementName — Returns root element's name
    • SDO_DAS_XML_Document::getRootElementURI — Returns root element's URI string
    • SDO_DAS_XML_Document::setEncoding — Sets the given string as encoding
    • SDO_DAS_XML_Document::setXMLDeclaration — Sets the xml declaration
    • SDO_DAS_XML_Document::setXMLVersion — Sets the given string as xml version

User Contributed Notes

There are no user contributed notes for this page.


소개

In order to use the XML Data Access Service for Service Data Objects, you will need to understand some of the concepts behind SDO: the data graph, the data object, XPath and property expressions, and so on. If you are not familiar with these ideas, you might want to look first at the section on SDO.

The job of the XML DAS is to move data between the application and an XML data source, which can be either a file or a URL. SDOs are always created and maintained according to a model which defines type names and what property names each type may have. For data which is from XML, this SDO model is built from a schema file written in XML schema language (an xsd file). This schema file is usually passed to the create method when the XMLDAS is initialised. The » SDO 2.0 specification defines the mapping between XML types and SDO types. There are a number of small limitations in the PHP support - not everything which is in the specification can be done - and these limitations are summarised in a later section.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

The SDO XML Data Access Service requires PHP 5.1.0 or higher. It is packaged with the SDO extension and requires SDO to have been installed. See the SDO installation instructions for the details of how to do this.

User Contributed Notes

There are no user contributed notes for this page.


설치

The XML Data Access Service is packaged and installed as part of the SDO extension. Please Refer SDO installation instructions.

User Contributed Notes

There are no user contributed notes for this page.


예제

Several of the following examples are based on the letter example described in the SDO documentation. The examples assume the XML Schema for the letter is contained in a file letter.xsd and the letter instance is in the file letter.xml. These two files are reproduced here:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:letter="http://letterSchema"
  targetNamespace="http://letterSchema">
  <xsd:element name="letters" type="letter:FormLetter"/>
  <xsd:complexType name="FormLetter" mixed="true">
    <xsd:sequence>
      <xsd:element name="date" minOccurs="0" type="xsd:string"/>
      <xsd:element name="firstName" minOccurs="0" type="xsd:string"/>
      <xsd:element name="lastName" minOccurs="0" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

<letter:letters xmlns:letter="http://letterSchema">
  <date>March 1, 2005</date>
  Mutual of Omaha
  Wild Kingdom, USA
  Dear
  <firstName>Casy</firstName>
  <lastName>Crocodile</lastName>
  Please buy more shark repellent.
  Your premium is past due.
</letter:letters>

Example #1 Loading, altering, and saving an XML document

The following example shows how an XML document can be loaded from a file, altered, and written back.

<?php
/**
 * Load, update, and save an XML document
 */
try {
   $xmldas = SDO_DAS_XML::create("letter.xsd");
   $document = $xmldas->loadFile("letter.xml");
   $root_data_object = $document->getRootDataObject();
   $root_data_object->date = "September 03, 2004";
   $root_data_object->firstName = "Anantoju";
   $root_data_object->lastName = "Madhu";
   $xmldas->saveFile($document, "letter-out.xml");
   echo "New file has been written:\n";
   print file_get_contents("letter-out.xml");
} catch (SDO_Exception $e) {
   print($e->getMessage());
}
?>

An instance of the XML DAS is first obtained from the SDO_DAS_XML::create() method, which is a static method of the SDO_DAS_XML class. The location of the xsd is passed as a parameter. Once we have an instance of the XML DAS initialised with a given schema, we can use it to load the instance document using the loadFile() method. There is also a loadString() method if you want to load an XML instance document from a string. If the instance document loads successfully, you will be returned an object of type SDO_DAS_XML_Document, on which you can call the getRootDataObject() method to get the SDO data object which is the root of the SDO data graph. You can then use SDO operations to change the graph. In this example we alter the date, firstName, and lastName properties. Then we use the saveFile() method to write the changed document back to the file system. The saveFile method has an optional extra integer argument which if specified will cause the XML DAS to format the XML, using the integer as the amount to indent by at each change in level on the document.

This will write the following to letter-out.xml.

<?xml version="1.0" encoding="UTF-8"?>
<FormLetter xmlns="http://letterSchema" xsi:type="FormLetter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <date>September 03, 2004</date>
  Mutual of Omaha
  Wild Kingdom, USA
  Dear
  <firstName>Anantoju</firstName>
  <lastName>Madhu</lastName>
  Please buy more shark repellent.
  Your premium is past due.
</FormLetter>

Example #2 Creating a new XML document

The previous example loaded the document from a file. This example shows how to create an SDO data graph in memory. In this example it is then saved to an XML string. Furthermore, because the letter contains both structured and unstructured content, it uses the Sequence API as well assignments to properties to construct the data graph.

<?php
/**
 * Create an XML document from scratch
 */
try {
   $xmldas = SDO_DAS_XML::create("letter.xsd");
   try {
       $doc = $xmldas->createDocument();
       $rdo = $doc->getRootDataObject();
       $seq = $rdo->getSequence();
       $seq->insert("April 09, 2005", NULL, 'date');
       $seq->insert("Acme Inc. ", NULL, NULL);
       $seq->insert("United Kingdom. ");
       $seq->insert("Dear", NULL, NULL);
       $seq->insert("Tarun", NULL, "firstName");
       $seq->insert("Nayaraaa", NULL, "lastName");
       $rdo->lastName = "Nayar";
       $seq->insert("Please note that your order number ");
       $seq->insert(12345);
       $seq->insert(" has been dispatched today. Thanks for your business with us.");
       print($xmldas->saveString($doc));
   } catch (SDO_Exception $e) {
       print($e);
   }
} catch (SDO_Exception $e) {
   print("Problem creating an XML document: " . $e->getMessage());
}
?>

The createDocument() method on the XML DAS returns a document object with a single root data object corresponding to an empty document element. The element name of the document element is known from the schema file. If there is any ambiguity about what the document element is, as there can be when more than one schema has been loaded into the same XML DAS, the element name and the namespace URI can be passed to the createDocument() method.

This will emit the following output (line breaks have been inserted for readability):

<?xml version="1.0" encoding="UTF-8"?>
<FormLetter xmlns="http://letterSchema" xsi:type="FormLetter"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<date>April 09, 2005</date>
Acme Inc. United Kingdom.
Dear
<firstName>Tarun</firstName>
<lastName>Nayar</lastName>
Please note that your order number 12345 has been
dispatched today. Thanks for your business with us.
</FormLetter>

Example #3 Setting XML document properties

This third example shows you how to set the XML version and encoding on the document object. These will be used when the XML is written out. If no XML declaration is wanted at all (perhaps you want to generate the XML as a string to embed in something) then you can use the setXMLDeclaration() method to suppress it.

<?php
/**
 * Illustrate the calls that control the XML declaration
 */
   $xmldas = SDO_DAS_XML::create("letter.xsd");
   $document = $xmldas->loadFile("letter.xml");
   $document->setXMLVersion("1.1");
   $document->setEncoding("ISO-8859-1");
   print($xmldas->saveString($document));
?>

The XML version and encoding are set in the XML declaration at the top of the XML document.

<?xml version="1.1" encoding="utf-8"?>
.../...

Example #4 Using an open type

This fourth example illustrates the use of an SDO open type and the use of the createDataObject() method. For this example we use the following two schema:

<schema
  xmlns="http://www.w3.org/2001/XMLSchema">

  <element name="jungle">
    <complexType>
      <sequence>
        <any minOccurs="0" maxOccurs="unbounded"/>
      </sequence>
    </complexType>
  </element>

</schema>

Note the presence of the any element in the definition. This first schema defines the jungle complex type as containing a sequence of any other type. The other types that the example will use are defined in a second schema file:

<schema xmlns= "http://www.w3.org/2001/XMLSchema">

   <complexType name="snakeType">
     <sequence>
       <element name= "name" type="string"/>
       <element name= "length" type="positiveInteger" />
     </sequence>
   </complexType>

   <complexType name="bearType">
     <sequence>
       <element name= "name" type="string"/>
       <element name= "weight" type="positiveInteger" />
     </sequence>
   </complexType>

   <complexType name="pantherType">
     <sequence>
       <element name= "name" type="string"/>
       <element name= "colour" type="string" />
     </sequence>
   </complexType>

</schema>

Here is the example PHP code that uses these two schema files:

<?php

/**
 * Illustrate open types and the use of the addTypes() method
 */

$xmldas = SDO_DAS_XML::create();
$xmldas->addTypes("jungle.xsd"); // this is an open type i.e. the xsd specifies it can contain "any" type
$xmldas->addTypes('animalTypes.xsd');

$baloo            = $xmldas->createDataObject('','bearType');
$baloo->name      = "Baloo";
$baloo->weight    = 800;

$bagheera         = $xmldas->createDataObject('','pantherType');
$bagheera->name   = "Bagheera";
$bagheera->colour = 'inky black';

$kaa              = $xmldas->createDataObject('','snakeType');
$kaa->name        = "Kaa";
$kaa->length      = 25;

$document         = $xmldas->createDocument();
$do               = $document->getRootDataObject();
$do->bear         = $baloo;
$do->panther      = $bagheera;
$do->snake        = $kaa;

print($xmldas->saveString($document,2));

?>

These two schema files are loaded into the XML DAS with first the create() and addTypes() methods. The createDataObject() method is used to create three separate data objects. In each case the namespaceURI and typename of the type are passed to the createDataObject() method: in this example the namespace URI is blank because no namespace is used in the schema. Once the three data objects - representing a bear, a panther and a snake - have been created, a document object is created with the createDocument() method. In this case there is no ambiguity about what the document element of the document should be - as the second schema file only defines complex types, the document element can only be the global jungle element defined in the first schema. This document will have a single root data object corresponding to an empty document element jungle. As this is an open type, properties can be added at will. When the first assignment is made to $do->bear, a property bear is added to the root data object: likewise for the next two assignments. When the document is written out by the saveString() method, the resulting document is:

<?xml version="1.0" encoding="UTF-8"?>
<jungle xsi:type="jungle" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <bear xsi:type="bearType">
    <name>Baloo</name>
    <weight>800</weight>
  </bear>
  <panther xsi:type="pantherType">
    <name>Bagheera</name>
    <colour>inky black</colour>
  </panther>
  <snake xsi:type="snakeType">
    <name>Kaa</name>
    <length>25</length>
  </snake>
</jungle>

Example #5 Finding out what you can from the document

This example is intended to illustrate how you can find the element name and namespace of the document element from the XML Document object, and the SDO type and namespace from the root data object of the XML data object, and how they relate to one another. This can be difficult to understand because there are four method calls: two can be made against the Document object, and two that can be made against any data object including the root data object. Because of the rules that define how the SDO model is derived from the XML model, when the data object concerned is the root object that represents the document object for the document, only three possible values can come back from these four method calls.

The two method calls that can be made against the document are getRootElementName() and getRootEelementURI(). These return the element name and namespace of the document element, respectively.

The two method calls that can be made against any data object are getTypeName() and getTypeNamespaceURI(). These return the SDO type name and type namespace of the data object, respectively.

Always, calling getRootElementURI() on the document object will return the same value as calling getNamespaceURI() on the root data object. Essentially, the information is all derived from the first few lines of the schema file, where there are three distinct pieces of information. For illustration, here again are the first few lines of the letter.xsd that we used above.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:letter="http://letterSchema"
            targetNamespace="http://letterSchema">

  <xsd:element name="letters" type="letter:FormLetter"/>

  <xsd:complexType name="FormLetter" mixed="true">
    .../...

The three important values are:

  • letters, the name of the document element

  • FormLetter, the name of the complex type of the document element. This is also the name of the SDO type of the root data object.

  • http://letterSchema, the namespace to which the document element belongs. This is also the namespaceURI of the SDO type of the root data object.

It is part of the XML-SDO mapping rules that when the SDO model is built from the schema file, the typename and namespaceURI of the SDO types for the root element are taken from those of the complex type of the document element, where it exists. Hence in this example the typename of the root data object is FormLetter. In the event that there is no separate complex type definition for the document element, when the type is defined inline and is anonymous, the SDO type name will be the same as the element name.

The following program loads the letter document and checks the return values from each of the four calls.

<?php
/**
 * Finding out what you can about the document and document element
 * This can be quite hard to understand because there are four calls
 * Two calls are made against the document
 * Two calls are made against the root data object and its model
 * Because of the SDO-XML mapping rules and how the SDO model is derived
 * from the XML model, only three possible values can come back from these four calls.
 * Always, $document->getRootElementURI() == (type of root data object)->namespaceURI
 * Essentially, it all comes form the first few lines of the xsd:
 * <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 *   xmlns:letter="http://letterSchema"
 *   targetNamespace="http://letterSchema">
 *   <xsd:element name="letters" type="letter:FormLetter"/>
 */

$xmldas = SDO_DAS_XML::create("letter.xsd");
$document = $xmldas->loadFile("letter.xml");
$root_do = $document->getRootDataObject();

/**
 * The "root element name" is the element name of the document element
 * in this case 'letters'
 * This matches the 'name' attribute of the document element in the xsd and matches
 * the element name from the xml
 */
echo "The document element name is " . $document->getRootElementName() . "\n";
assert($document->getRootElementName() == 'letters'); // a property of the document

/**
 * The "root element URI" is the namespace part of the element name of the document element
 * in this case 'http://letterSchema' since 'letters' is in that namespace
 * This is taken from the xsd and matches the namespace picked up from the xml
 */
echo "The document element is in the namespace " . $document->getRootElementURI() . "\n";
assert($document->getRootElementURI() == 'http://letterSchema'); // a property of the document

/**
 * The type name is taken from the SDO model
 * The XML-SDO mapping rules make this either:
 *   The name of the complexType if there is one (in this case there is)
 *   The document element name if there no complexType
 * This is taken from the xsd
 */
echo "The type name of the root data object is " . $root_do->getTypeName() . "\n";
assert($root_do->getTypeName() == 'FormLetter');

/**
 * The type's namespaceURI is taken from the SDO model
 * The XML-SDO mapping rules ensure that this will always be the same as
 * the namepace URI of the document element
 */
echo "The namespaceURI of the root data object is " . $root_do->getTypeNamespaceURI() . "\n";
assert($root_do->getTypeNamespaceURI() == 'http://letterSchema');

?>

The output from this program is as follows:

The document element name is letters
The document element is in the namespace http://letterSchema
The type name of the root data object is FormLetter
The namespaceURI of the root data object is http://letterSchema

Example #6 Printing the SDO model

The XML DAS provides a simple means to see what types and properties have been loaded. The php "print" or "echo" instruction will print out the types and properties.

<?php
/**
 * Illustrate printing out the model
 */

$xmldas = SDO_DAS_XML::create("letter.xsd");
print $xmldas;

?>

The output from this program is as follows:

object(SDO_XML_DAS)#1 {
18 types have been defined. The types and their properties are::
1. commonj.sdo:BigDecimal
2. commonj.sdo:BigInteger
3. commonj.sdo:Boolean
4. commonj.sdo:Byte
5. commonj.sdo:Bytes
6. commonj.sdo:ChangeSummary
7. commonj.sdo:Character
8. commonj.sdo:DataObject
9. commonj.sdo:Date
10. commonj.sdo:Double
11. commonj.sdo:Float
12. commonj.sdo:Integer
13. commonj.sdo:Long
14. commonj.sdo:Short
15. commonj.sdo:String
16. commonj.sdo:URI
17. http://letterSchema:FormLetter
    - date (commonj.sdo:String)
    - firstName (commonj.sdo:String)
    - lastName (commonj.sdo:String)
18. http://letterSchema:RootType
    - letters (http://letterSchema:FormLetter)

User Contributed Notes

cc at yarmschool dot org 14-Oct-2014 07:48
Hello. I need to know if I could this with PHP under Windows IIS servers. Thank you


SDO DAS XML 함수 목록

예약 클래스

The XML DAS provides two main classes. The first is SDO_DAS_XML which is the main class used to fetch the data from the XML source and used to write the data back. The second is the SDO_DAS_XML_Document class, which represents the data in the XML document.

There are also some exception classes which can be thrown if errors are found when looking for or parsing the xsd or xml files.

SDO_DAS_XML

This is the main class of the XML DAS, which is used fetch the data from the xml source and also used to write the data back. Other than the methods to load and save xml files,

메쏘드

  • create This is a static method available in the SDO_DAS_XML class. Used to construct an SDO_DAS_XML object.

  • addTypes Works in much the same way as create() but used to add the contents of a second or subsequent schema file to an XML DAS that has already been created.

  • createDataObject Can be used to construct an SDO data object of a given type.

  • createDocument Can be used to construct an XML Document object from scratch.

  • loadFile Loads the xml instance document from a file. This file can be at local file system or it can be on a remote host.

  • loadString same as the above method. Loads the xml instance which is available as string.

  • saveFile save SDO_DAS_XML_Document object as a xml file.

  • saveString save SDO_DAS_XML_Document object as a xml string.

SDO_DAS_XML_Document

This class can be used to get to the name and namespace of the document element, and to get to the root data object of the document. Lastly, it can also be used to set the XML version and encoding of a document on output.

메쏘드

  • getRootDataObject gets the root DataObject.

  • getRootElementName gets the root DataObject's name.

  • getRootElementURI gets the root DataObject's URI.

  • setEncoding sets the encoding string with the given value.

  • setXMLDeclaraion to set/unset the xml declaration.

  • setXMLVersion sets the xml version with the given value.

SDO_DAS_XML_ParserException

Is a subclass of SDO_Exception. Thrown for any parser errors while loading the xsd/xml file.

SDO_DAS_XML_FileException

Is a subclass of SDO_Exception. Thrown by any of the methods that load data from a file, when the file cannot be found.

Limitations compared with SDO 2.0 specification

The » SDO 2.0 specification defines the mapping between XML types and SDO types. With Java SDO, this mapping is implemented by the XMLHelper. With SDO for PHP, this mapping is implemented by the XML Data Access Service. The XML DAS implements the mapping described in the SDO 2.0 specification with some restrictions. A detailed list is of the limitations is:

XML Simple Types

  1. Simple Type with sdoJava:instanceClass - no PHP equivalent provided.

  2. Simple Type with sdoJava:extendedInstanceClass - no PHP equivalent provided.

  3. Simple Type with list of itemType.

  4. Simple Type with union.

XML Complex Types

  1. Complex Type with sdo:aliasName - no PHP support for SDO Type aliases.

XSD Attribute

  1. Attribute with sdo:aliasName - no PHP support for SDO property aliases.

  2. Attribute with default value - no PHP support for SDO property defaults.

  3. Attribute with fixed value - no PHP support for SDO read-only properties or default values.

  4. Attribute referencing a DataObject with sdo:propertyType - no support for sdo:propertyType="...".

  5. Attribute with bi-directional property to a DataObject with sdo:oppositeProperty and sdo:propertyType - no PHP support for SDO opposite.

XSD Elements

  1. Element with sdo:aliasName - no PHP support for SDO property aliases.

  2. Element with substitution group.

XSD Elements with Simple Type

  1. Element of SimpleType with default - no PHP support for SDO defaults

  2. Element of SimpleType with fixed value - no PHP support for SDO read-only properties or default values.

  3. Element of SimpleType with sdo:string - no support for sdo:string="true".

  4. Element referencing a DataObject with sdo:propertyType - no support for sdo:propertyType="..."

  5. Element with bi-directional reference to a DataObject with sdo:oppositeProperty and sdo:propertyType - no PHP support for SDO opposite.

Table of Contents

  • SDO_DAS_XML::addTypes — To load a second or subsequent schema file to a SDO_DAS_XML object
  • SDO_DAS_XML::create — To create SDO_DAS_XML object for a given schema file
  • SDO_DAS_XML::createDataObject — Creates SDO_DataObject for a given namespace URI and type name
  • SDO_DAS_XML::createDocument — Creates an XML Document object from scratch, without the need to load a document from a file or string.
  • SDO_DAS_XML::loadFile — Returns SDO_DAS_XML_Document object for a given path to xml instance document
  • SDO_DAS_XML::loadString — Returns SDO_DAS_XML_Document for a given xml instance string
  • SDO_DAS_XML::saveFile — Saves the SDO_DAS_XML_Document object to a file
  • SDO_DAS_XML::saveString — Saves the SDO_DAS_XML_Document object to a string
  • SDO_DAS_XML_Document::getRootDataObject — Returns the root SDO_DataObject
  • SDO_DAS_XML_Document::getRootElementName — Returns root element's name
  • SDO_DAS_XML_Document::getRootElementURI — Returns root element's URI string
  • SDO_DAS_XML_Document::setEncoding — Sets the given string as encoding
  • SDO_DAS_XML_Document::setXMLDeclaration — Sets the xml declaration
  • SDO_DAS_XML_Document::setXMLVersion — Sets the given string as xml version

User Contributed Notes

There are no user contributed notes for this page.


To load a second or subsequent schema file to a SDO_DAS_XML object

  • ≪ SDO DAS XML 함수 목록
  • SDO_DAS_XML::create ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • To load a second or subsequent schema file to a SDO_DAS_XML object

SDO_DAS_XML::addTypes

(^)

SDO_DAS_XML::addTypes — To load a second or subsequent schema file to a SDO_DAS_XML object

설명

void SDO_DAS_XML::addTypes ( string $xsd_file )

Load a second or subsequent schema file to an XML DAS that has already been created with the static method create(). Although the file may be any valid schema file, a likely reason for using this method is to add a schema file containing definitions of extra complex types, hence the name. See Example 4 of the parent document for an example.

인수

xsd_file

Path to XSD Schema file.

반환값

None if successful, otherwise throws an exception as described below.

오류/예외

SDO_TypeNotFoundException

Thrown if a type is not defined in the underlying model.

SDO_DAS_XML_ParserException

Thrown for any problems while parsing the given XSD File.

SDO_DAS_XML_FileException

Thrown if the specified file cannot be found.

User Contributed Notes

There are no user contributed notes for this page.


To create SDO_DAS_XML object for a given schema file

  • ≪ SDO_DAS_XML::addTypes
  • SDO_DAS_XML::createDataObject ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • To create SDO_DAS_XML object for a given schema file

SDO_DAS_XML::create

(^)

SDO_DAS_XML::create — To create SDO_DAS_XML object for a given schema file

설명

SDO_DAS_XML SDO_DAS_XML::create ([ mixed $xsd_file [, string $key ]] )

This is the only static method of SDO_DAS_XML class. Used to instantiate SDO_DAS_XML object.

인수

xsd_file

Path to XSD Schema file. This is optional. If omitted a DAS will be created that only has the SDO base types defined. Schema files can then be loaded with the addTypes() method. Can be string or array of values.

key

반환값

Returns SDO_DAS_XML object on success otherwise throws an exception as described below.

오류/예외

SDO_TypeNotFoundException

Thrown if a type is not defined in the underlying model.

SDO_DAS_XML_ParserException

Thrown for any problems while parsing the given XSD File.

SDO_DAS_XML_FileException

Thrown if the specified file cannot be found.

User Contributed Notes

There are no user contributed notes for this page.


Creates SDO_DataObject for a given namespace URI and type name

  • ≪ SDO_DAS_XML::create
  • SDO_DAS_XML::createDocument ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Creates SDO_DataObject for a given namespace URI and type name

SDO_DAS_XML::createDataObject

(^)

SDO_DAS_XML::createDataObject — Creates SDO_DataObject for a given namespace URI and type name

설명

SDO_DataObject SDO_DAS_XML::createDataObject ( string $namespace_uri , string $type_name )

Creates SDO_DataObject for a given namespace URI and type name. The type should be defined in the underlying model otherwise SDO_TypeNotFoundException will be thrown.

인수

namespace_uri

Namespace URI of the type name.

type_name

Type Name.

반환값

Returns SDO_DataObject on success.

오류/예외

SDO_TypeNotFoundException

Thrown if a type is not defined in the underlying model.

User Contributed Notes

There are no user contributed notes for this page.


Creates an XML Document object from scratch, without the need to load a document from a file or string.

  • ≪ SDO_DAS_XML::createDataObject
  • SDO_DAS_XML::loadFile ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Creates an XML Document object from scratch, without the need to load a document from a file or string.

SDO_DAS_XML::createDocument

(^)

SDO_DAS_XML::createDocument — Creates an XML Document object from scratch, without the need to load a document from a file or string.

설명

SDO_DAS_XML_Document SDO_DAS_XML::createDocument ([ string $document_element_name ] )
SDO_DAS_XML_Document SDO_DAS_XML::createDocument ( string $document_element_namespace_URI , string $document_element_name [, SDO_DataObject $dataobject ] )

Creates an XML Document object. This will contain just one empty root element on which none of the properties will have been set. The purpose of this call is to allow an application to create an XML document from scratch without the need to load a document from a file or string. The document that is created will be as if a document had been loaded that contained just a single empty document element with no attributes set or elements within it.

createDocument() may need to be told what the document element is. This will not be necessary in simple cases. When there is no ambiguity then no parameter need be passed to the method. However it is possible to load more than one schema file into the same XML DAS and in this case there may be more than one possible document element defined: furthermore it is even possible that there are two possible document elements that differ only in the namespace. To cope with these cases it is possible to specify either the document element name, or both the document element name and namespace to the method.

인수

document_element_name

The name of the document element. Only needed if there is more than one possibility.

document_element_namespace_URI

The namespace part of the document element name. Only needed if there is more than one possible document element with the same name.

dataobject

반환값

Returns an SDO_XML_DAS_Document object on success.

오류/예외

SDO_UnsupportedOperationException

Thrown if an element name or element name and namespace URI is passed, but not found in the underlying model.

User Contributed Notes

There are no user contributed notes for this page.


Returns SDO_DAS_XML_Document object for a given path to xml instance document

  • ≪ SDO_DAS_XML::createDocument
  • SDO_DAS_XML::loadString ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Returns SDO_DAS_XML_Document object for a given path to xml instance document

SDO_DAS_XML::loadFile

(^)

SDO_DAS_XML::loadFile — Returns SDO_DAS_XML_Document object for a given path to xml instance document

설명

SDO_XMLDocument SDO_DAS_XML::loadFile ( string $xml_file )

Constructs the tree of SDO_DataObjects from the given address to xml instance document. Returns SDO_DAS_XML_Document Object. Use SDO_DAS_XML_Document::getRootDataObject method to get root data object.

인수

xml_file

Path to Instance document. This can be a path to a local file or it can be a URL.

반환값

Returns SDO_DAS_XML_Document object on Success or throws exception as described.

오류/예외

SDO_TypeNotFoundException

Thrown if a type is not defined by the underlying model.

SDO_PropertyNotFoundException

Thrown if a property within a type is not defined in the underlying model.

SDO_DAS_XML_ParserException

Thrown for any problems while parsing the given XSD File.

SDO_DAS_XML_FileException

Thrown if the specified file cannot be found.

User Contributed Notes

There are no user contributed notes for this page.


Returns SDO_DAS_XML_Document for a given xml instance string

  • ≪ SDO_DAS_XML::loadFile
  • SDO_DAS_XML::saveFile ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Returns SDO_DAS_XML_Document for a given xml instance string

SDO_DAS_XML::loadString

(^)

SDO_DAS_XML::loadString — Returns SDO_DAS_XML_Document for a given xml instance string

설명

SDO_DAS_XML_Document SDO_DAS_XML::loadString ( string $xml_string )

Constructs the tree of SDO_DataObjects from the given xml instance string. Returns SDO_DAS_XML_Document Object. Use SDO_DAS_XML_Document::getRootDataObject method to get root data object.

인수

xml_string

xml string.

반환값

Returns SDO_DAS_XML_Document object on Success or throws exception as described.

오류/예외

SDO_TypeNotFoundException

Thrown if a type is not defined by the underlying model.

SDO_PropertyNotFoundException

Thrown if the a property within a type is not defined in the underlying model.

SDO_DAS_XML_ParserException

Thrown for any problems while parsing the given XSD File.

User Contributed Notes

There are no user contributed notes for this page.


Saves the SDO_DAS_XML_Document object to a file

  • ≪ SDO_DAS_XML::loadString
  • SDO_DAS_XML::saveString ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Saves the SDO_DAS_XML_Document object to a file

SDO_DAS_XML::saveFile

(^)

SDO_DAS_XML::saveFile — Saves the SDO_DAS_XML_Document object to a file

설명

void SDO_DAS_XML::saveFile ( SDO_XMLDocument $xdoc , string $xml_file [, int $indent ] )

Saves the SDO_DAS_XML_Document object to a file.

인수

xdoc

SDO_DAS_XML_Document object.

xml_file

xml file.

indent

Optional argument to specify that the xml should be formatted. A non-negative integer is the amount to indent each level of the xml. So, the integer 2, for example, will indent the xml so that each contained element is two spaces further to the right than its containing element. The integer 0 will cause the xml to be completely left-aligned. The integer -1 means no formatting - the xml will come out on one long line.

반환값

None.

오류/예외

SDO_DAS_XML_FileException

Thrown if the specified file cannot be found.

User Contributed Notes

There are no user contributed notes for this page.


Saves the SDO_DAS_XML_Document object to a string

  • ≪ SDO_DAS_XML::saveFile
  • SDO_DAS_XML_Document::getRootDataObject ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Saves the SDO_DAS_XML_Document object to a string

SDO_DAS_XML::saveString

(^)

SDO_DAS_XML::saveString — Saves the SDO_DAS_XML_Document object to a string

설명

string SDO_DAS_XML::saveString ( SDO_XMLDocument $xdoc [, int $indent ] )

Saves the SDO_DAS_XML_Document object to string.

인수

xdoc

SDO_DAS_XML_Document object.

indent

Optional argument to specify that the xml should be formatted. A non-negative integer is the amount to indent each level of the xml. So, the integer 2, for example, will indent the xml so that each contained element is two spaces further to the right than its containing element. The integer 0 will cause the xml to be completely left-aligned. The integer -1 means no formatting - the xml will come out on one long line.

반환값

xml string.

User Contributed Notes

There are no user contributed notes for this page.


Returns the root SDO_DataObject

  • ≪ SDO_DAS_XML::saveString
  • SDO_DAS_XML_Document::getRootElementName ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Returns the root SDO_DataObject

SDO_DAS_XML_Document::getRootDataObject

(^)

SDO_DAS_XML_Document::getRootDataObject — Returns the root SDO_DataObject

설명

SDO_DataObject SDO_DAS_XML_Document::getRootDataObject ( void )

Returns the root SDO_DataObject.

인수

반환값

Returns the root SDO_DataObject.

User Contributed Notes

There are no user contributed notes for this page.


Returns root element's name

  • ≪ SDO_DAS_XML_Document::getRootDataObject
  • SDO_DAS_XML_Document::getRootElementURI ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Returns root element's name

SDO_DAS_XML_Document::getRootElementName

(^)

SDO_DAS_XML_Document::getRootElementName — Returns root element's name

설명

string SDO_DAS_XML_Document::getRootElementName ( void )

Returns root element's name.

인수

반환값

Returns root element's name.

User Contributed Notes

There are no user contributed notes for this page.


Returns root element's URI string

  • ≪ SDO_DAS_XML_Document::getRootElementName
  • SDO_DAS_XML_Document::setEncoding ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Returns root element's URI string

SDO_DAS_XML_Document::getRootElementURI

(^)

SDO_DAS_XML_Document::getRootElementURI — Returns root element's URI string

설명

string SDO_DAS_XML_Document::getRootElementURI ( void )

Returns root element's URI string.

인수

반환값

Returns root element's URI string.

User Contributed Notes

There are no user contributed notes for this page.


Sets the given string as encoding

  • ≪ SDO_DAS_XML_Document::getRootElementURI
  • SDO_DAS_XML_Document::setXMLDeclaration ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Sets the given string as encoding

SDO_DAS_XML_Document::setEncoding

(^)

SDO_DAS_XML_Document::setEncoding — Sets the given string as encoding

설명

void SDO_DAS_XML_Document::setEncoding ( string $encoding )

Sets the given string as encoding.

인수

encoding

Encoding string.

반환값

None.

User Contributed Notes

There are no user contributed notes for this page.


Sets the xml declaration

  • ≪ SDO_DAS_XML_Document::setEncoding
  • SDO_DAS_XML_Document::setXMLVersion ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Sets the xml declaration

SDO_DAS_XML_Document::setXMLDeclaration

(^)

SDO_DAS_XML_Document::setXMLDeclaration — Sets the xml declaration

설명

void SDO_DAS_XML_Document::setXMLDeclaration ( bool $xmlDeclatation )

Controls whether an XML declaration will be generated at the start of the XML document. Set to TRUE to generate the XML declaration, or FALSE to suppress it.

인수

xmlDeclatation

Boolean value to set the XML declaration.

반환값

None.

User Contributed Notes

There are no user contributed notes for this page.


Sets the given string as xml version

  • ≪ SDO_DAS_XML_Document::setXMLDeclaration
  • SimpleXML ≫
  • PHP Manual
  • SDO DAS XML 함수 목록
  • Sets the given string as xml version

SDO_DAS_XML_Document::setXMLVersion

(^)

SDO_DAS_XML_Document::setXMLVersion — Sets the given string as xml version

설명

void SDO_DAS_XML_Document::setXMLVersion ( string $xmlVersion )

Sets the given string as xml version.

인수

xmlVersion

xml version string.

반환값

None.

User Contributed Notes

There are no user contributed notes for this page.


SimpleXML

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
    • Dealing with XML errors
  • SimpleXMLElement — The SimpleXMLElement class
    • SimpleXMLElement::addAttribute — Adds an attribute to the SimpleXML element
    • SimpleXMLElement::addChild — Adds a child element to the XML node
    • SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element
    • SimpleXMLElement::attributes — Identifies an element's attributes
    • SimpleXMLElement::children — Finds children of given node
    • SimpleXMLElement::__construct — Creates a new SimpleXMLElement object
    • SimpleXMLElement::count — Counts the children of an element
    • SimpleXMLElement::getDocNamespaces — Returns namespaces declared in document
    • SimpleXMLElement::getName — Gets the name of the XML element
    • SimpleXMLElement::getNamespaces — Returns namespaces used in document
    • SimpleXMLElement::registerXPathNamespace — Creates a prefix/ns context for the next XPath query
    • SimpleXMLElement::saveXML — 별칭: SimpleXMLElement::asXML
    • SimpleXMLElement::__toString — Returns the string content
    • SimpleXMLElement::xpath — Runs XPath query on XML data
  • SimpleXML 함수 목록
    • simplexml_import_dom — Get a SimpleXMLElement object from a DOM node.
    • simplexml_load_file — Interprets an XML file into an object
    • simplexml_load_string — Interprets a string of XML into an object

User Contributed Notes

oleg at mastak dot fi 30-Mar-2016 06:57
Two lines xml2array:

<?php

$xml = simplexml_load_string($xmlstring);
$array = (array) $xml;

?>
Serge 27-Feb-2015 07:58
XML data values should not contain "&" and that need to be replaced by html-entity "&amp;"

You can use this code to replace lonely "&" to "&amp;":

<?php
$sText = preg_replace('#&(?![a-z]{1,6};)#i', '&amp;', $sText);
?>

This will replace just "&" into "&amp;" but dont touches other html-entities like "&nbsp;", "&lt;" etc and, of course, "&amp;".

P.S. In regexp max length is 6 becouse I found that is the maximum length of possible html entity using this code:
<?php
max(array_map('strlen', array_values(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES | ENT_HTML5)))) - 2
?>
minus two is for first "&" and last ";" in an any html entity.
Tim Adam Perez 24-Jan-2014 05:22
XML to Array in a single line:
$array = (array) simplexml_load_string($xml);

Enjoy!
maikel at yuluma dot com 17-Oct-2013 06:02
In reply to  soloman at textgrid dot com,

2 line XML2Array:

$xml = simplexml_load_string($file);
$array = (array)$xml;
dkrnl at yandex dot ru 30-Sep-2013 11:57
Wrapper XMLReader class, for simple SAX-reading huge xml:
https://github.com/dkrnl/SimpleXMLReader

Usage example: http://github.com/dkrnl/SimpleXMLReader/blob/master/examples/example1.php

<?php

/**
 * Simple XML Reader
 *
 * @license Public Domain
 * @author Dmitry Pyatkov(aka dkrnl) <dkrnl@yandex.ru>
 * @url http://github.com/dkrnl/SimpleXMLReader
 */
class SimpleXMLReader extends XMLReader
{

    /**
     * Callbacks
     *
     * @var array
     */
    protected $callback = array();

    /**
     * Add node callback
     *
     * @param  string   $name
     * @param  callback $callback
     * @param  integer  $nodeType
     * @return SimpleXMLReader
     */
    public function registerCallback($name, $callback, $nodeType = XMLREADER::ELEMENT)
    {
        if (isset($this->callback[$nodeType][$name])) {
            throw new Exception("Already exists callback $name($nodeType).");
        }
        if (!is_callable($callback)) {
            throw new Exception("Already exists parser callback $name($nodeType).");
        }
        $this->callback[$nodeType][$name] = $callback;
        return $this;
    }

    /**
     * Remove node callback
     *
     * @param  string  $name
     * @param  integer $nodeType
     * @return SimpleXMLReader
     */
    public function unRegisterCallback($name, $nodeType = XMLREADER::ELEMENT)
    {
        if (!isset($this->callback[$nodeType][$name])) {
            throw new Exception("Unknow parser callback $name($nodeType).");
        }
        unset($this->callback[$nodeType][$name]);
        return $this;
    }

    /**
     * Run parser
     *
     * @return void
     */
    public function parse()
    {
        if (empty($this->callback)) {
            throw new Exception("Empty parser callback.");
        }
        $continue = true;
        while ($continue && $this->read()) {
            if (isset($this->callback[$this->nodeType][$this->name])) {
                $continue = call_user_func($this->callback[$this->nodeType][$this->name], $this);
            }
        }
    }

    /**
     * Run XPath query on current node
     *
     * @param  string $path
     * @param  string $version
     * @param  string $encoding
     * @return array(SimpleXMLElement)
     */
    public function expandXpath($path, $version = "1.0", $encoding = "UTF-8")
    {
        return $this->expandSimpleXml($version, $encoding)->xpath($path);
    }

    /**
     * Expand current node to string
     *
     * @param  string $version
     * @param  string $encoding
     * @return SimpleXMLElement
     */
    public function expandString($version = "1.0", $encoding = "UTF-8")
    {
        return $this->expandSimpleXml($version, $encoding)->asXML();
    }

    /**
     * Expand current node to SimpleXMLElement
     *
     * @param  string $version
     * @param  string $encoding
     * @param  string $className
     * @return SimpleXMLElement
     */
    public function expandSimpleXml($version = "1.0", $encoding = "UTF-8", $className = null)
    {
        $element = $this->expand();
        $document = new DomDocument($version, $encoding);
        $node = $document->importNode($element, true);
        $document->appendChild($node);
        return simplexml_import_dom($node, $className);
    }

    /**
     * Expand current node to DomDocument
     *
     * @param  string $version
     * @param  string $encoding
     * @return DomDocument
     */
    public function expandDomDocument($version = "1.0", $encoding = "UTF-8")
    {
        $element = $this->expand();
        $document = new DomDocument($version, $encoding);
        $node = $document->importNode($element, true);
        $document->appendChild($node);
        return $document;
    }

}
?>
whyme 29-Dec-2012 06:55
Simple means simple.  If you know the structure and just want the value of a tag:

<?php
$xml = simplexml_load_file($xmlfile);
print $xml->City->Street->Address->HouseColor;
?>

Warning, numbers can come out as strings, empty elements like <HouseColor></HouseColor> come out as array(0)
Laurent Vervisch 24-Jun-2012 07:57
Here is an example of an easy mapping between xml and classes defined by user.

<?php
class XmlClass extends SimpleXMLElement
{
    /**
     * Returns this object as an instance of the given class.
     */
    public function asInstanceOf($class_name)
    {
        // should check that class_name is valid

        return simplexml_import_dom(dom_import_simplexml($this), $class_name);
    }

    public function __call($name, array $arguments)
    {
        echo "magic __call called for method $name on instance of ".get_class()."\n";

        // class could be mapped according $this->getName()
        $class_name = 'Test';

        $instance = $this->asInstanceOf($class_name);
        return call_user_func_array(array($instance, $name), $arguments);
    }
}

class Test extends XmlClass
{
    public function setValue($string)
    {
        $this->{0} = $string;
    }
}

$xml = new XmlClass('<example><test/></example>');
$test = $xml->test->asInstanceOf('Test');
echo get_class($xml->test), "\n";
echo get_class($test), "\n";

$test->setValue('value set directly by instance of Test');
echo (string)$xml->test, "\n";
echo (string)$test, "\n";

$xml->test->setValue('value set by instance of XmlClass and magic __call');
echo (string)$xml->test, "\n";
echo (string)$test, "\n";
?>

XmlClass
Test
value set directly by instance of Test
value set directly by instance of Test
magic __call called for method setValue on instance of XmlClass
value set by instance of XmlClass and magic __call
value set by instance of XmlClass and magic __call
xaviered at gmail dot com 18-May-2012 04:00
Here is a recursive function that will convert a given SimpleXMLElement object into an array, preserving namespaces and attributes.

<?php
function xmlObjToArr($obj) {
        $namespace = $obj->getDocNamespaces(true);
        $namespace[NULL] = NULL;

        $children = array();
        $attributes = array();
        $name = strtolower((string)$obj->getName());

        $text = trim((string)$obj);
        if( strlen($text) <= 0 ) {
            $text = NULL;
        }

        // get info for all namespaces
        if(is_object($obj)) {
            foreach( $namespace as $ns=>$nsUrl ) {
                // atributes
                $objAttributes = $obj->attributes($ns, true);
                foreach( $objAttributes as $attributeName => $attributeValue ) {
                    $attribName = strtolower(trim((string)$attributeName));
                    $attribVal = trim((string)$attributeValue);
                    if (!empty($ns)) {
                        $attribName = $ns . ':' . $attribName;
                    }
                    $attributes[$attribName] = $attribVal;
                }

                // children
                $objChildren = $obj->children($ns, true);
                foreach( $objChildren as $childName=>$child ) {
                    $childName = strtolower((string)$childName);
                    if( !empty($ns) ) {
                        $childName = $ns.':'.$childName;
                    }
                    $children[$childName][] = xmlObjToArr($child);
                }
            }
        }

        return array(
            'name'=>$name,
            'text'=>$text,
            'attributes'=>$attributes,
            'children'=>$children
        );
    }
?>
thedoc8786 at gmail dot com 24-Mar-2012 04:42
FAIL! This function works better than the one I posted below:

<?php
    function toArray($xml) {
        $array = json_decode(json_encode($xml), TRUE);

        foreach ( array_slice($array, 0) as $key => $value ) {
            if ( empty($value) ) $array[$key] = NULL;
            elseif ( is_array($value) ) $array[$key] = toArray($value);
        }

        return $array;
    }
thedoc8786 at gmail dot com 24-Mar-2012 01:19
I know it is over-done, but the following is a super-short example of a XML to Array conversion function (recursive):

<?php
    function toArray(SimpleXMLElement $xml) {
        $array = (array)$xml;

        foreach ( array_slice($array, 0) as $key => $value ) {
            if ( $value instanceof SimpleXMLElement ) {
                $array[$key] = empty($value) ? NULL : toArray($value);
            }
        }
        return $array;
    }
xananax at yelostudio dot com 08-Sep-2011 03:25
None of the XML2Array functions that I found satisfied me completely; Their results did not always fit the project I was working on, and I found none that would account for repeating XML elements (such as
<fields><field/><field/><field/></fields>)
So I rolled out my own; hope it helps someone.
<?php
/**
 * Converts a simpleXML element into an array. Preserves attributes.<br/>
 * You can choose to get your elements either flattened, or stored in a custom
 * index that you define.<br/>
 * For example, for a given element
 * <code>
 * <field name="someName" type="someType"/>
 * </code>
 * <br>
 * if you choose to flatten attributes, you would get:
 * <code>
 * $array['field']['name'] = 'someName';
 * $array['field']['type'] = 'someType';
 * </code>
 * If you choose not to flatten, you get:
 * <code>
 * $array['field']['@attributes']['name'] = 'someName';
 * </code>
 * <br>__________________________________________________________<br>
 * Repeating fields are stored in indexed arrays. so for a markup such as:
 * <code>
 * <parent>
 *     <child>a</child>
 *     <child>b</child>
 *     <child>c</child>
 * ...
 * </code>
 * you array would be:
 * <code>
 * $array['parent']['child'][0] = 'a';
 * $array['parent']['child'][1] = 'b';
 * ...And so on.
 * </code>
 * @param simpleXMLElement    $xml            the XML to convert
 * @param boolean|string    $attributesKey    if you pass TRUE, all values will be
 *                                            stored under an '@attributes' index.
 *                                            Note that you can also pass a string
 *                                            to change the default index.<br/>
 *                                            defaults to null.
 * @param boolean|string    $childrenKey    if you pass TRUE, all values will be
 *                                            stored under an '@children' index.
 *                                            Note that you can also pass a string
 *                                            to change the default index.<br/>
 *                                            defaults to null.
 * @param boolean|string    $valueKey        if you pass TRUE, all values will be
 *                                            stored under an '@values' index. Note
 *                                            that you can also pass a string to
 *                                            change the default index.<br/>
 *                                            defaults to null.
 * @return array the resulting array.
 */
function simpleXMLToArray(SimpleXMLElement $xml,$attributesKey=null,$childrenKey=null,$valueKey=null){

    if($childrenKey && !is_string($childrenKey)){$childrenKey = '@children';}
    if($attributesKey && !is_string($attributesKey)){$attributesKey = '@attributes';}
    if($valueKey && !is_string($valueKey)){$valueKey = '@values';}

    $return = array();
    $name = $xml->getName();
    $_value = trim((string)$xml);
    if(!strlen($_value)){$_value = null;};

    if($_value!==null){
        if($valueKey){$return[$valueKey] = $_value;}
        else{$return = $_value;}
    }

    $children = array();
    $first = true;
    foreach($xml->children() as $elementName => $child){
        $value = simpleXMLToArray($child,$attributesKey, $childrenKey,$valueKey);
        if(isset($children[$elementName])){
            if(is_array($children[$elementName])){
                if($first){
                    $temp = $children[$elementName];
                    unset($children[$elementName]);
                    $children[$elementName][] = $temp;
                    $first=false;
                }
                $children[$elementName][] = $value;
            }else{
                $children[$elementName] = array($children[$elementName],$value);
            }
        }
        else{
            $children[$elementName] = $value;
        }
    }
    if($children){
        if($childrenKey){$return[$childrenKey] = $children;}
        else{$return = array_merge($return,$children);}
    }

    $attributes = array();
    foreach($xml->attributes() as $name=>$value){
        $attributes[$name] = trim($value);
    }
    if($attributes){
        if($attributesKey){$return[$attributesKey] = $attributes;}
        else{$return = array_merge($return, $attributes);}
    }

    return $return;
}
?>
soloman at textgrid dot com 10-Aug-2011 02:06
Three line xml2array:

<?php

$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

?>

Ta da!
streaver91 at gmail dot com 05-May-2011 05:49
The BIGGEST differece between an XML and a PHP array is that in an XML file, the name of elements can be the same even if they are siblings, eg. "<pa><ch /><ch /><ch /></pa>", while in an PHP array, the key of which must be different.

I think the array structure developed by svdmeer can fit for XML, and fits well.

here is an example array converted from an xml file:
array(
"@tag"=>"name",
"@attr"=>array(
    "id"=>"1","class"=>"2")
"@text"=>"some text",
)

or if it has childrens, that can be:

array(
"@tag"=>"name",
"@attr"=>array(
    "id"=>"1","class"=>"2")
"@items"=>array(
    0=>array(
        "@tag"=>"name","@text"=>"some text"
    )
)

Also, I wrote a function that can change that array back to XML.

<?php
function array2XML($arr,$root) {
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><{$root}></{$root}>");
$f = create_function('$f,$c,$a','
        foreach($a as $v) {
            if(isset($v["@text"])) {
                $ch = $c->addChild($v["@tag"],$v["@text"]);
            } else {
                $ch = $c->addChild($v["@tag"]);
                if(isset($v["@items"])) {
                    $f($f,$ch,$v["@items"]);
                }
            }
            if(isset($v["@attr"])) {
                foreach($v["@attr"] as $attr => $val) {
                    $ch->addAttribute($attr,$val);
                }
            }
        }');
$f($f,$xml,$arr);
return $xml->asXML();
}
?>
phil at dier dot us 19-Jan-2011 08:37
Here's a function I came up with to convert an associative array to XML.  Works for multidimensional arrays as well.

<?php
function assocArrayToXML($root_element_name,$ar)
{
    $xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$root_element_name}></{$root_element_name}>");
    $f = create_function('$f,$c,$a','
            foreach($a as $k=>$v) {
                if(is_array($v)) {
                    $ch=$c->addChild($k);
                    $f($f,$ch,$v);
                } else {
                    $c->addChild($k,$v);
                }
            }');
    $f($f,$xml,$ar);
    return $xml->asXML();
}
?>
philipp at strazny dot com 24-Nov-2010 02:33
Here's a quick way to dump the nodeValues from SimpleXML into an array using the path to each nodeValue as key. The paths are compatible with e.g. DOMXPath. I use this when I need to update values externally (i.e. in code that doesn't know about the underlying xml). Then I use DOMXPath to find the node containing the original value and update it.

<?php
function XMLToArrayFlat($xml, &$return, $path='', $root=false)
{
    $children = array();
    if ($xml instanceof SimpleXMLElement) {
        $children = $xml->children();
        if ($root){ // we're at root
            $path .= '/'.$xml->getName();
        }
    }
    if ( count($children) == 0 ){
        $return[$path] = (string)$xml;
        return;
    }
    $seen=array();
    foreach ($children as $child => $value) {
        $childname = ($child instanceof SimpleXMLElement)?$child->getName():$child;
        if ( !isset($seen[$childname])){
            $seen[$childname]=0;
        }
        $seen[$childname]++;
        XMLToArrayFlat($value, $return, $path.'/'.$child.'['.$seen[$childname].']');
    }
}
?>

Use like this:

<?php
$xml = simplexml_load_string(...some xml string...);
$xmlarray = array(); // this will hold the flattened data
XMLToArrayFlat($xml, $xmlarray, '', true);
?>

You can also pull multiple files in one array:

<?php
foreach($files as $file){
    $xml = simplexml_load_file($file);
    XMLToArrayFlat($xml, $xmlarray, $file.':', true);
}
?>
The respective filename/path is thus prefixed to each key.
kristof at viewranger dot com 18-Nov-2010 02:14
If you tried to load an XML file with this, but the CDATA parts were not loaded for some reason, is because you should do it this way:

$xml = simplexml_load_file($this->filename, 'SimpleXMLElement', LIBXML_NOCDATA);

This converts CDATA to String in the returning object.
sherwinterunez at yahoo dot com 01-Sep-2010 08:15
<?php

// Sherwin R. Terunez
//
// This is my own version of XML Object to Array
//

function amstore_xmlobj2array($obj, $level=0) {

    $items = array();

    if(!is_object($obj)) return $items;

    $child = (array)$obj;

    if(sizeof($child)>1) {
        foreach($child as $aa=>$bb) {
            if(is_array($bb)) {
                foreach($bb as $ee=>$ff) {
                    if(!is_object($ff)) {
                        $items[$aa][$ee] = $ff;
                    } else
                    if(get_class($ff)=='SimpleXMLElement') {
                        $items[$aa][$ee] = amstore_xmlobj2array($ff,$level+1);
                    }
                }
            } else
            if(!is_object($bb)) {
                $items[$aa] = $bb;
            } else
            if(get_class($bb)=='SimpleXMLElement') {
                $items[$aa] = amstore_xmlobj2array($bb,$level+1);
            }
        }
    } else
    if(sizeof($child)>0) {
        foreach($child as $aa=>$bb) {
            if(!is_array($bb)&&!is_object($bb)) {
                $items[$aa] = $bb;
            } else
            if(is_object($bb)) {
                $items[$aa] = amstore_xmlobj2array($bb,$level+1);
            } else {
                foreach($bb as $cc=>$dd) {
                    if(!is_object($dd)) {
                        $items[$obj->getName()][$cc] = $dd;
                    } else
                    if(get_class($dd)=='SimpleXMLElement') {
                        $items[$obj->getName()][$cc] = amstore_xmlobj2array($dd,$level+1);
                    }
                }
            }
        }
    }

    return $items;
}

?>
michael dot allen at emp dot shentel dot com 15-Apr-2010 08:06
Here is a very robust SimpleXML parser.  Can be used to load files, strings, or DOM into SimpleXML, or can be used to perform the reverse when handed SimpleXML.

<?php
/**
 * XMLParser Class File
 *
 * This class loads an XML document into a SimpleXMLElement that can
 * be processed by the calling application.  This accepts xml strings,
 * files, and DOM objects.  It can also perform the reverse, converting
 * an SimpleXMLElement back into a string, file, or DOM object.
 */
class XMLParser {
    /**
     * While parsing, parse the supplied XML document.
     *
     * Sets up a SimpleXMLElement object based on success of parsing
     * the XML document file.
     *
     * @param string $doc the xml document location path
     * @return object
     */
    public static function loadFile($doc) {
        if (file_exists($doc)) {
            return simplexml_load_file($doc);
        } else {
            throw new Exception ("Unable to load the xml file " .
                                 "using: \"$doc\"", E_USER_ERROR);
        }
    }
    /**
     * While parsing, parse the supplied XML string.
     *
     * Sets up a SimpleXMLElement object based on success of parsing
     * the XML string.
     *
     * @param string $string the xml document string
     * @return object
     */
    public static function loadString($string) {
        if (isset($string)) {
            return simplexml_load_string($string);
        } else {
            throw new Exception ("Unable to load the xml string " .
                                 "using: \"$string\"", E_USER_ERROR);
        }
    }
    /**
     * While parsing, parse the supplied XML DOM node.
     *
     * Sets up a SimpleXMLElement object based on success of parsing
     * the XML DOM node.
     *
     * @param object $dom the xml DOM node
     * @return object
     */
    public static function loadDOM($dom) {
        if (isset($dom)) {
            return simplexml_import_dom($dom);
        } else {
            throw new Exception ("Unable to load the xml DOM node " .
                                 "using: \"$dom\"", E_USER_ERROR);
        }
    }
    /**
     * While parsing, parse the SimpleXMLElement.
     *
     * Sets up a XML file, string, or DOM object based on success of
     * parsing the XML DOM node.
     *
     * @param object $path the xml document location path
     * @param string $type the return type (string, file, dom)
     * @param object $simplexml the simple xml element
     * @return mixed
     */
    public static function loadSXML($simplexml, $type, $path) {
        if (isset($simplexml) && isset($type)) {
        switch ($type) {
            case 'string':
                return $simplexml->asXML();
            break;
            case 'file':
                if (isset($path)) {
                    return $simplexml->asXML($path);
                } else {
                    throw new Exception ("Unable to create the XML file. Path is missing or" .
                                         "is invalid: \"$path\"", E_USER_ERROR);
                }
            break;
            case 'dom':
                return dom_import_simplexml($simplexml);
            break;
        }
        } else {
            throw new Exception ("Unable to load the simple XML element " .
                                 "using: \"$simplexml\"", E_USER_ERROR);
        }
    }
}
?>
emmanuel 17-Feb-2010 07:21
dynamic sql in php using xml:

test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<sql>
    <statement>
        SELECT * FROM USERS
        <call criteria="byId">WHERE id = %d</call>
        <call criteria="byUsername">WHERE username = "%s"</call>;
    </statement>
</sql>

index.php:
<?php
function callMe($param) {
    $search = array('byUsername' => 'dynsql');

    if (isset($search[$param[1]])) {
        return sprintf($param[2], $search[$param[1]]);
    }

    return "";
}

$xml = simplexml_load_file("test.xml");
$string = $xml->statement->asXML();
$string = preg_replace_callback('/<call criteria="(\w+)">(.*?)<\/call>/', 'callMe', $string);
$node = simplexml_load_string($string);
echo $node;
?>

obviously, this example can be improved [in your own code.]
mahmutta at gmail dot com 20-Jan-2010 09:58
while using simple xml and get double or float int value from xml object for using math operations (+ * - / ) some errors happens on the operation, this is because of simple xml returns everythings to objects.
exmple;

<?php

$name = "somestring";
$size = 11.45;
$xml = '
<xml>
 <name>somestring</name>
 <size>11.45</size>
</xml>';

$xmlget = simplexml_load_string($xml)

echo $xml->size*2;    // 20  its false
// ($xml->size is an object (int)11 and  (45) )

// this is true
echo $size*2;            // 22.90
echo (float)$size*2;   // 22.90
?>
antoine dot rabanes at gmail dot com 16-Oct-2009 04:22
if for some reasons you need the string value instead of the simpleXML Object you can cast the return value as a string.

exemple:

<?php
$all_api_call = simplexml_load_file($url);
$all_api = array();
$all_api = $all_api_call->result;
$list_all_api_name = array();
$i = 0;
foreach ($all_api->children() as $funcky_function)
{
    $string_tmp = (string )$funcky_function->function;
    $list_all_api_name[$i++] = $putain;
}
?>

...
walter 12-Jun-2009 04:58
Working fix for the infamous SimpleXML + memcache bug.

Improves code at http://tinyurl.com/bmoon-simplexml to actually work for arbitrary-depth structures.  (Because neither the function nor the json_decode(json_encode($obj)) hack listed there worked for me.)

There's probably some superfluous code in here that could be improved on -- but it works!

<?php
 # convert a structure that may include objects to a pure
 # array-based structure (that can be stored in memcache)
 #  ... includes support for simplexml!
 # (nb: may have problems with infinite recursive structs)
 function enforce_array($obj) {
  $array = (array)$obj;
  if(empty($array)) {
   $array = '';
  }
  else {
   foreach($array as $key=>$value) {
    if(!is_scalar($value)) {
     if(is_a($value,'SimpleXMLElement')) {
      $tmp = memcache_objects_to_array($value);
      if(!is_array($tmp)) {
       $tmp = ''.$value;
      }
      $array[$key] = $tmp;
     }
     else {
      $array[$key] = enforce_array($value);
     }
    }
    else {
     $array[$key] = $value;
    }
   }
  }
  return $array;
 }
?>
mail at kleineedv dot de 28-Apr-2009 09:37
I had a problem with simplexml reading nodes from an xml file. It always return an SimpleXML-Object but not the text inside the node.

Example:
<?xml version="1.0" encoding="UTF-8"?>
<Test>
    <Id>123</Id>
</Test>

Reading this xml into a variable called $xml and then doing the following
<?php
$myId = $xml->Id;
?>
Did not return 123 in $myId, but instead I got a SimpleXMLElement Object.

The solution is simple, when you know it. Use explicit string conversion.
<?php
$myId = (string)$xml->Id;
?>
oscargodson at gmail dot com 25-Jan-2009 12:05
To add to what others have said, you can't directly put a $_GET or $_POST value into a variable then into an attribute using SimpleXML. You must first convert it to an integer.

This will NOT work

<?php
      $page_id = $_GET['id'];
      echo $xml->page[$page_id]
?>

You will get something like:
Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/mysite/index.php on line 10

However, this WILL work and is much simpler then using (string) or other methods.
<?php
   $page_id = intval($_GET['id']);
   echo $xml->page[$page_id]
?>
bxt at die-optimisten dot net 26-Nov-2008 04:47
Addition to QLeap's post:
SimpleXML will return a reference to an object containing the node value and you can't use references in session variables as there is no feasible way to restore a reference to another variable.

This won't work too:
$val=$this->xml->node->attributes()->name;
echo $array[$val]; // will cause a warning because of the wrong index type.

You have to convert/cast to a String first:
echo $array[(string)$val];

This will work as expected, because converting will call the __toString() method. Therefor echo works too:
echo $val; // will display the name
QLeap 18-Nov-2008 11:27
Storing SimpleXMLElement values in $_SESSION does not work. Saving the results as an object or individual elements of the object will result in the dreaded "Warning: session_start() [function.session-start]: Node no longer exists" error.

For example, this does not work:

    $xml  = new SimpleXMLElement($page);
    $country  = $xml->Response->Placemark->AddressDetails->Country->CountryNameCode;
    $_SESSION['country'] = $country;

This will work:

    $_SESSION['country'] = (string) $country;
gwhitescarver at yahoo dot com 11-Sep-2008 10:02
Moving some code from a PHP 5.2.6 / Windows environment to a 5.2.0 / Linux environment, I somehow lost access to a plain text node within a SimpleXML Object.  On a var_dump of $xml_node, a [0] element was shown as the string '12'.  However, $xml_node[0] was evaluating NULL in 5.2.0.  You can see below the code change I made, pulling my data out of the raw XML with a regular expression.  Hope this is useful to someone.

//In some versions of PHP it seems we cannot access the [0] element of a SimpleXML Object.  Doesn't work in 5.2.0:
//$count = $xml_node[0];
//grab the raw XML:
$count = ($xml_node->asXML());
//pull out the number between the closing and opening brace of the xml:
$count = preg_replace('/.*>(\d*)<.*/', '$1', $count);
charlie at blankcanvasstudios dot com 14-Aug-2008 08:21
Optimizing aalaap at gmail dot com's php

<?php
function is_rss($feedxml) {
    @$feed = simplexml_load_string($feedxml);

    return ($feed->channel->item)?true:false;
}

function is_atom($feedxml) {
    @$feed = new SimpleXMLElement($feedxml);
    ($feed->entry):true:false;
}
?>
aalaap at gmail dot com 30-Jun-2008 03:58
Here are two quick and dirty functions that use SimpleXML to detect if a feed xml is RSS or ATOM:

<?php
function is_rss($feedxml) {
    @$feed = new SimpleXMLElement($feedxml);

    if ($feed->channel->item) {
        return true;
    } else {
        return false;
    }
}

function is_atom($feedxml) {
    @$feed = new SimpleXMLElement($feedxml);

    if ($feed->entry) {
        return true;
    } else {
        return false;
    }
}
?>

The functions take in the full text feed (retrieved via cURL, for example) and return a true or a false based on the result.


소개

SimpleXML 확장은 XML을 객체로 변환하는 매우 간단하고 쉬운 방식을 제공합니다. XML을 일반적인 프로퍼티 선택자와 배열 이터레이터를 사용하여 처리할 수 있습니다.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

SimpleXML 확장은 PHP 5가 필요합니다.

User Contributed Notes

There are no user contributed notes for this page.


설치

SimpleXML 확장은 기본으로 설치됩니다. 설치하지 않으려면, --disable-simplexml 옵션을 사용하십시오.

User Contributed Notes

alvaro at digdib dot com 02-Jan-2009 11:40
Gentoo users: emerge does not compile dev-lang/php with simplexml by default. To enable this option, add the flag simplexml to your USE variable and recompile php.


예제

Table of Contents

  • Dealing with XML errors

User Contributed Notes

Juan Herrera 19-Dec-2009 09:49
When using simplexml to access a element the returned object may be a SimpleXMLElement instead of a string.

Example:

<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
    <cmd>login</cmd>
    <login>Richard</login>
</document>
XML;

$xml = simplexml_load_string($string);
print_r($xml);
$login = $xml->login;
print_r($login);
$login = (string) $xml->login;
print_r($login);
?>

Expected result:
----------------
SimpleXMLElement Object
(
    [cmd] => login
    [login] => Richard
)
Richard
Richard

Actual result:
--------------
SimpleXMLElement Object
(
    [cmd] => login
    [login] => Richard
)
SimpleXMLElement Object
(
    [0] => Richard
)
Richard

But this is an intended behavior. See http://bugs.php.net/bug.php?id=29500
info at accountr dot eu 05-May-2009 05:35
Instead of using  <?php eval($code) ?>

you can use this method,

<?php

$node = "node";

$XML = simplexml_load_file($file);

$text = (string)$XML->nodes->{$node}; // works
// or
$text2 = (string)$XML->{$node}->child; // also works fine

?>

I think it is much more easier instead of eval().
Kari P. 15-Aug-2008 11:17
To take care of proper error checking, one must use try-catch blocks:

<?php
try {
  $sxe = new SimpleXMLElement($xmlstring);
} catch (Exception $e) {
  echo "bad xml";
}
?>

To get rid of warnings, use @ in front of new:

<?php
try {
  $sxe = @new SimpleXMLElement($xmlstring);
} catch (Exception $e) {
  echo "bad xml";
}
?>
Honeymonster 30-Apr-2008 03:52
@david b
@Fabi W

Why can't you use the ->{} operators?

e.g

$xml->{$node}

I've not tested this, but see no reason why it would not work.
mike at mike-griffiths dot co dot uk 18-Apr-2008 06:11
It is important that you select the correct method of loading the XML in depending on the format of your XML file.

If you use the wrong function to load it in you will get some strange errors.


  • ≪ 예제
  • Dealing with XML errors ≫
  • PHP Manual
  • 예제

여기 있는 많은 예제는 XML 문자열이 필요합니다. 모든 예제에서 문자열을 반복하는 대신에, 각 예제에서 포함하는 파일을 넣어두겠습니다. 이 파일은 다음 예제 섹션에 있습니다. 이 방법 대신, XML 문서를 만들고 simplexml_load_file()으로 읽어올 수도 있습니다.

Example #1 XML 문자열을 가진 포함 파일 example.php

<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#211;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>
XML;
?>

SimpleXML의 간단함은 기본 XML 문서에서 하나의 문자열이나 숫자를 가져오는 데에서 보여집니다.

Example #2 <plot> 가져오기.

<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

echo $xml->movie[0]->plot; // "So this language. It's like..."
?>

XML 문서 안에 PHP 이름 규칙에 어긋나는 요소(예: 하이픈)에 접근할 때는 요소 이름을 {}과 '로 감쌉니다.

Example #3 <line> 가져오기.

<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

echo $xml->movie->{'great-lines'}->line; // "PHP solves all my web problems"
?>

Example #4 SimpleXML에서 다중 요소에 접근하기.

하나의 부모 요소에 포함되는 여러 개의 자식 요소에 접근할 때는, 일반적인 반복 기술이 적용됩니다.

<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

/* 각 <movie> 노드에서, 각각의 <plot>을 출력합니다. */
foreach ($xml->movie as $movie) {
   echo $movie->plot, '<br />';
}

?>

Example #5 속성 사용하기

여기까지, 요소 이름과 값만을 다뤘습니다. SimpleXML은 요소의 속성에도 접근할 수 있습니다. 요소 속성에 접근할때는 요소를 단순히 array로 취급하면 됩니다.

<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

/* 첫번째 movie의 <rating> 노드에 접근합니다.
 * Output the rating scale, too. */
foreach ($xml->movie[0]->rating as $rating) {
    switch((string) $rating['type']) { // Get attributes as element indices
    case 'thumbs':
        echo $rating, ' thumbs up';
        break;
    case 'stars':
        echo $rating, ' stars';
        break;
    }
}
?>

Example #6 요소와 속성을 텍스트와 비교하기.

요소와 속성을 문자열과 비교하거나, 문자열을 요구하는 함수에 통과시킬 때는 (string)을 사용하여 문자열로 변환해야 합니다. 그렇지 않으면, PHP는 요소를 객체로 취급합니다.

<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

if ((string) $xml->movie->title == 'PHP: Behind the Parser') {
    print 'My favorite movie.';
}

htmlentities((string) $xml->movie->title);
?>

Example #7 두 요소 비교하기.

PHP 5.2.0부터 두 개의 SimpleXMLElement는 같은 곳을 가르키고 있어도 다른 것으로 간주합니다.

<?php
$el1 = new SimpleXMLElement($xmlstr);
$el2 = new SimpleXMLElement($xmlstr);
var_dump($el1 == $el2); // PHP 5.2.0 부터 false
?>

Example #8 XPath 사용하기.

SimpleXML은 XPath 지원을 내장하고 있습니다. 모든 <character> 요소를 찾으려면:

<?php
include 'example.php';
$xml = new SimpleXMLElement($xmlstr);

foreach ($xml->xpath('//character') as $character) {
    echo $character->name, 'played by ', $character->actor, '<br />';
}
?>

'//'은 와일드카드로 사용됩니다. 절대 경로로 지정하려면, 슬래시 하나를 생략하십시오.

Example #9 값 설정하기.

SimpleXML의 데이터는 바뀔 수 있습니다. SimpleXML은 모든 객체에 대한 변경을 허용합니다.

<?php
include 'example.php';
$xml = new SimpleXMLElement($xmlstr);

$xml->movie[0]->characters->character[0]->name = 'Miss Coder';

echo $xml->asXML();
?>

위 코드는 원본에서 Ms. Coder를 Miss Coder로 변경한 새 XML 문서를 출력합니다.

Example #10 요소와 속성 추가하기.

PHP 5.1.3부터, SimpleXML은 자식과 속성을 쉽게 추가할 수 있게 되었습니다.

<?php
include 'example.php';
$xml = new SimpleXMLElement($xmlstr);

$character = $xml->movie[0]->characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');

$rating = $xml->movie[0]->addChild('rating', 'PG');
$rating->addAttribute('type', 'mpaa');

echo $xml->asXML();
?>

위 코드는 원본 XML에서 새로운 character와 rating을 추가한 XML 문서를 출력합니다.

Example #11 DOM 상호 작용.

PHP는 XML 노드를 SimpleXML과 DOM 형식으로 변환할 수 있습니다. 이 예제는 DOM 요소를 SimpleXML로 바꾸는 방법을 보여줍니다.

<?php
$dom = new domDocument;
$dom->loadXML('<books><book><title>blah</title></book></books>');
if (!$dom) {
     echo 'Error while parsing the document';
     exit;
}

$s = simplexml_import_dom($dom);

echo $s->book[0]->title;
?>

위 예제의 출력:

blah

User Contributed Notes

rowan dot collins at gmail dot com 05-Jun-2015 03:58
There is a common "trick" often proposed to convert a SimpleXML object to an array, by running it through json_encode() and then json_decode(). I'd like to explain why this is a bad idea.

Most simply, because the whole point of SimpleXML is to be easier to use and more powerful than a plain array. For instance, you can write <?php $foo->bar->baz['bing'] ?> and it means the same thing as <?php $foo->bar[0]->baz[0]['bing'] ?>, regardless of how many bar or baz elements there are in the XML; and if you write <?php (string)$foo->bar[0]->baz[0] ?> you get all the string content of that node - including CDATA sections - regardless of whether it also has child elements or attributes. You also have access to namespace information, the ability to make simple edits to the XML, and even the ability to "import" into a DOM object, for much more powerful manipulation. All of this is lost by turning the object into an array rather than reading understanding the examples on this page.

Additionally, because it is not designed for this purpose, the conversion to JSON and back will actually lose information in some situations. For instance, any elements or attributes in a namespace will simply be discarded, and any text content will be discarded if an element also has children or attributes. Sometimes, this won't matter, but if you get in the habit of converting everything to arrays, it's going to sting you eventually.

Of course, you could write a smarter conversion, which didn't have these limitations, but at that point, you are getting no value out of SimpleXML at all, and should just use the lower level XML Parser functions, or the XMLReader class, to create your structure. You still won't have the extra convenience functionality of SimpleXML, but that's your loss.
mymymymy at mymymymy dot com 02-Jan-2015 06:41
$children = $xml->children();
var_dump($children[0]["@attributes"]);//"NULL"
var_dump($children[0]);//(2) ["@attributes"]=>array(1) {['x']=> string(1) '2'}

no faithfully (not true)
------
example:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [name] => USA
        )
)

print_r($xml->country->attributes()->name);
show:
 SimpleXMLElement Object
(
    [0] => USA
)

print_r((string)$xml->country->attributes()->name);
show:
USA
RiKdnUA at mail dot ru 03-Dec-2013 06:40
Если кодировка XML-документа отличается от UTF-8, объявление кодировки должно следовать сразу после version='...' и перед standalone='...'. Это требование стандарта XML.

If encoding XML-document differs from UTF-8. Encoding declaration should follow immediately after the version = '...' and before standalone = '...'. This requirement is standard XML.

<?xml version='1.0' encoding='windows-1251' standalone='yes'?>
Ok
<?xml version='1.0' standalone='yes' encoding='windows-1251'?>
<body>Русский язык. Russian language</body>
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in...
eng dot emad_2010 at yahoo dot com 09-Oct-2013 08:46
Example #  DOM Interoperability ??????? ??????
PHP has a mechanism to convert XML nodes between SimpleXML and DOM formats. This example shows how one might change a DOM element to SimpleXML  .
The simplexml_import_dom() function returns a SimpleXMLElement object from a DOM node.
DOM         object

<?php
//create DOM  document $dom that contain XML
$dom = new DOMDocument;
$dom->loadXML('<books><book><title>blah</title></book></books>');
if (!$dom) {
    echo 'Error while parsing the document';
    exit;
}
//convert DOM document $dom to object $books
$books = simplexml_import_dom($dom);

//access object $books
echo $books->book[0]->title;
?>
jishcem at gmail dot com 12-Mar-2013 04:31
For me it was easier to use arrays than objects,

So, I used this code,

$xml = simplexml_load_file('xml_file.xml');

$json_string = json_encode($xml);

$result_array = json_decode($json_string, TRUE);

Hope it would help someone
gkokmdam at zonnet dot nl 23-Feb-2011 10:54
A quick tip on xpath queries and default namespaces. It looks like the XML-system behind SimpleXML has the same workings as I believe the XML-system .NET uses: when one needs to address something in the default namespace, one will have to declare the namespace using registerXPathNamespace and then use its prefix to address the otherwise in the default namespace living element.

<?php
$string = <<<XML
<?xml version='1.0'?>
<document xmlns="http://www.w3.org/2005/Atom">
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;

$xml = simplexml_load_string($string);
$xml->registerXPathNamespace("def", "http://www.w3.org/2005/Atom");

$nodes = $xml->xpath("//def:document/def:title");

?>
kdos 31-Jan-2011 02:32
Using stuff like: is_object($xml->module->admin) to check if there actually is a node called "admin", doesn't seem to work as expected, since simplexml always returns an object- in that case an empty one  - even if a particular node does not exist.
For me good old empty() function seems to work just fine in such cases.

Cheers
Max K. 20-Jun-2010 11:38
From the README file:

SimpleXML is meant to be an easy way to access XML data.

SimpleXML objects follow four basic rules:

1) properties denote element iterators
2) numeric indices denote elements
3) non numeric indices denote attributes
4) string conversion allows to access TEXT data

When iterating properties then the extension always iterates over
all nodes with that element name. Thus method children() must be
called to iterate over subnodes. But also doing the following:
foreach ($obj->node_name as $elem) {
  // do something with $elem
}
always results in iteration of 'node_name' elements. So no further
check is needed to distinguish the number of nodes of that type.

When an elements TEXT data is being accessed through a property
then the result does not include the TEXT data of subelements.

Known issues
============

Due to engine problems it is currently not possible to access
a subelement by index 0: $object->property[0].
ie dot raymond at gmail dot com 02-Apr-2010 12:07
If you need to output valid xml in your response, don't forget to set your header content type to xml in addition to echoing out the result of asXML():

<?php

$xml=simplexml_load_file('...');
...
...xml stuff
...

//output xml in your response:
header('Content-Type: text/xml');
echo $xml->asXML();
?>
php at keith tyler dot com 23-Dec-2009 09:57
[Editor's Note: The SimpleXMLIterator class, however, does implement these methods.]

While SimpleXMLElement claims to be iterable, it does not seem to implement the standard Iterator interface functions like ::next and ::reset properly. Therefore while foreach() works, functions like next(), current(), or each() don't seem to work as you would expect -- the pointer never seems to move or keeps getting reset.
bjorn at xQmail dot eu 31-Aug-2009 04:48
If you're not sure the XML will be valid you'd better use:

<?php
$xmlObject = simplexml_load_string($xml);
// or
$xmlObject = simplexml_load_file(xml);
?>

Both of these return a SimpleXMLElement Object or a libXMLError Object.
radams at circlepix com 24-Apr-2009 07:52
To test whether an element exists:

<?php

    $xml = <<<EOT
<?xml version='1.0' standalone='yes'?>
<root>
    <test1></test1>
    <test2 />
    <test4> </test4>
</root>
EOT;

    $xmlDoc = new SimpleXMLElement($xml);

echo "Test1: \n";
var_dump($xmlDoc->test1);
echo "\n(" . (bool)$xmlDoc->test1 . ")";
echo "\n\n";

echo "Test2: \n";
var_dump($xmlDoc->test2);
echo "\n(" . (bool)$xmlDoc->test2 . ")";
echo "\n\n";

echo "Test3: \n";
var_dump($xmlDoc->test3);
echo "\n(" . (bool)$xmlDoc->test3 . ")";
echo "\n\n";

echo "Test4: \n";
var_dump($xmlDoc->test4);
echo "\n(" . (bool)$xmlDoc->test4 . ")";
echo "\n\n";

?>

The var_dumps for test1, test2, and test3 are identical, but the (bool) test gives a '1' for test1 and test2, and a '' for test3.


Dealing with XML errors

Dealing with XML errors when loading documents is a very simple task. Using the libxml functionality it is possible to suppress all XML errors when loading the document and then iterate over the errors.

The libXMLError object, returned by libxml_get_errors(), contains several properties including the message, line and column (position) of the error.

Example #1 Loading broken XML string

<?php
libxml_use_internal_errors(true);
$sxe = simplexml_load_string("<?xml version='1.0'><broken><xml></broken>");
if (!$sxe) {
    echo "Failed loading XML\n";
    foreach(libxml_get_errors() as $error) {
        echo "\t", $error->message;
    }
}
?>

위 예제의 출력:

Failed loading XML
    Blank needed here
    parsing XML declaration: '?>' expected
    Opening and ending tag mismatch: xml line 1 and broken
    Premature end of data in tag broken line 1

참고

  • libxml_use_internal_errors()
  • libxml_get_errors()
  • libXMLError

User Contributed Notes

1337 at netapp dot com 04-Sep-2015 11:21
If you need to process the content of your broken XML-doc you might find this interesting. It has blown past a few simple corruptions for me.
http://php.net/manual/en/class.domdocument.php#domdocument.props.recover
tuxedobob 12-Sep-2014 06:29
Now that the /e modifier is considered deprecated in preg_replace, you can use a negative lookahead to replace unescaped ampersands with &amp; without throwing warnings:

$str = preg_replace('/&(?!;{6})/', '&amp;', $str);

You probably should have been doing this before /e was deprecated, actually.
openbip at gmail dot com 26-Feb-2010 05:20
Note that "if (! $sxe) {" may give you a false-negative if the XML document was empty (e.g. "<root />").  In that case, $sxe will be:

object(SimpleXMLElement)#1 (0) {
}

which will evaluate to false, even though nothing technically went wrong.

Consider instead: "if ($sxe === false) {"
Jacob Tabak 15-Feb-2010 10:17
If you are trying to load an XML string with some escaped and some unescaped ampersands, you can pre-parse the string to ecsape the unescaped ampersands without modifying the already escaped ones:
<?php
$s = preg_replace('/&[^; ]{0,6}.?/e', "((substr('\\0',-1) == ';') ? '\\0' : '&amp;'.substr('\\0',1))", $s);
?>


The SimpleXMLElement class

(PHP 5 >= 5.0.1, PHP 7)

소개

Represents an element in an XML document.

클래스 개요

SimpleXMLElement implements Traversable {
/* 메소드 */
final public __construct ( string $data [, int $options = 0 [, bool $data_is_url = false [, string $ns = "" [, bool $is_prefix = false ]]]] )
public void addAttribute ( string $name [, string $value [, string $namespace ]] )
public SimpleXMLElement addChild ( string $name [, string $value [, string $namespace ]] )
public mixed asXML ([ string $filename ] )
public SimpleXMLElement attributes ([ string $ns = NULL [, bool $is_prefix = false ]] )
public SimpleXMLElement children ([ string $ns [, bool $is_prefix = false ]] )
public int count ( void )
public array getDocNamespaces ([ bool $recursive = false [, bool $from_root = true ]] )
public string getName ( void )
public array getNamespaces ([ bool $recursive = false ] )
public bool registerXPathNamespace ( string $prefix , string $ns )
public string __toString ( void )
public array xpath ( string $path )
}

Table of Contents

  • SimpleXMLElement::addAttribute — Adds an attribute to the SimpleXML element
  • SimpleXMLElement::addChild — Adds a child element to the XML node
  • SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element
  • SimpleXMLElement::attributes — Identifies an element's attributes
  • SimpleXMLElement::children — Finds children of given node
  • SimpleXMLElement::__construct — Creates a new SimpleXMLElement object
  • SimpleXMLElement::count — Counts the children of an element
  • SimpleXMLElement::getDocNamespaces — Returns namespaces declared in document
  • SimpleXMLElement::getName — Gets the name of the XML element
  • SimpleXMLElement::getNamespaces — Returns namespaces used in document
  • SimpleXMLElement::registerXPathNamespace — Creates a prefix/ns context for the next XPath query
  • SimpleXMLElement::saveXML — 별칭: SimpleXMLElement::asXML
  • SimpleXMLElement::__toString — Returns the string content
  • SimpleXMLElement::xpath — Runs XPath query on XML data

User Contributed Notes

frame at dynamiccreated dot de 14-Jun-2013 10:36
Important note I miss here in the documentation:

SimpleXML supports Array/Iteration-Methods. Therefore it is possible to

add attributes
edit attributes
remove attributes

add nodes
edit nodes
remove nodes

This is the reason why SimpleXML provides only add-methods not deleting- or editing-methods. We also need this methods because SimpleXML acts as a normal class and
new member will not converted to a new node.

Deleting a node seems not to be possible within a foreach-loop. The reason is simple. To do that we need a valid key, but the Iterator only gives us a "understandable feedback" on which node we are working on: the tag name.

So this will not work:

<?php
foreach($doc->seg as $key => $seg)
{
    if((string)$seg['id'] === 'whatever')
    {
        unset($seg); // only clears local copy
        unset($seg[$key]); // wrong index "seg"
    }
}
?>

But this, eg. will work:
<?php
unset($doc->seg[2]);
?>

In case of doubt always do a print_r/var_dump(). It nicely shows the real linked indexes.
Yukull 15-Feb-2013 10:59
xml to object conversion function :
<?php
/**
    @param:
        $xml: SimpleXMLElement
        $force: set to true to always create 'text', 'attribute', and 'children' even if empty
    @return
        object with attributs:
            (string) name: XML tag name
            (string) text: text content of the attribut name
            (array) attributes: array witch keys are attribute key and values are attribute value
            (array) children: array of objects made with xml2obj() on each child
**/
function xml2obj($xml,$force = false){

    $obj = new StdClass();

    $obj->name = $xml->getName();

    $text = trim((string)$xml);
    $attributes = array();
    $children = array();

    foreach($xml->attributes() as $k => $v){
        $attributes[$k]  = (string)$v;
    }

    foreach($xml->children() as $k => $v){
        $children[] = xml2obj($v,$force);
    }

    if($force or $text !== '')
        $obj->text = $text;

    if($force or count($attributes) > 0)
        $obj->attributes = $attributes;

    if($force or count($children) > 0)
        $obj->children = $children;

    return $obj;
}
?>
heaver 31-May-2012 04:33
XML to JSON conversion without '@attributes'
<?php
function XML2JSON($xml) {

        function normalizeSimpleXML($obj, &$result) {
            $data = $obj;
            if (is_object($data)) {
                $data = get_object_vars($data);
            }
            if (is_array($data)) {
                foreach ($data as $key => $value) {
                    $res = null;
                    normalizeSimpleXML($value, $res);
                    if (($key == '@attributes') && ($key)) {
                        $result = $res;
                    } else {
                        $result[$key] = $res;
                    }
                }
            } else {
                $result = $data;
            }
        }
        normalizeSimpleXML(simplexml_load_string($xml), $result);
        return json_encode($result);
    }
?>
demian dot katz at villanova dot edu 30-Mar-2012 02:50
Here is a more namespace-aware version of the earlier function for attaching one SimpleXMLElement to another.  I'm sure it could still be further improved (right now it generates some redundant xmlns definitions), but it seems to be working well enough for my purposes so far.

function SimpleXMLElement_append($parent, $child)
{
    // get all namespaces for document
    $namespaces = $child->getNamespaces(true);

    // check if there is a default namespace for the current node
    $currentNs = $child->getNamespaces();
    $defaultNs = count($currentNs) > 0 ? current($currentNs) : null;
    $prefix = (count($currentNs) > 0) ? current(array_keys($currentNs)) : '';
    $childName = strlen($prefix) > 1
        ? $prefix . ':' . $child->getName() : $child->getName();

    // check if the value is string value / data
    if (trim((string) $child) == '') {
        $element = $parent->addChild($childName, null, $defaultNs);
    } else {
        $element = $parent->addChild(
            $childName, htmlspecialchars((string)$child), $defaultNs
        );
    }

    foreach ($child->attributes() as $attKey => $attValue) {
        $element->addAttribute($attKey, $attValue);
    }
    foreach ($namespaces as $nskey => $nsurl) {
        foreach ($child->attributes($nsurl) as $attKey => $attValue) {
            $element->addAttribute($nskey . ':' . $attKey, $attValue, $nsurl);
        }
    }

    // add children -- try with namespaces first, but default to all children
    // if no namespaced children are found.
    $children = 0;
    foreach ($namespaces as $nskey => $nsurl) {
        foreach ($child->children($nsurl) as $currChild) {
            SimpleXMLElement_append($element, $currChild);
            $children++;
        }
    }
    if ($children == 0) {
        foreach ($child->children() as $currChild) {
            SimpleXMLElement_append($element, $currChild);
        }
    }
}
saganmarketing.com 12-Mar-2012 09:27
Parsing an invalid XML string through SimpleXML causes the script to crash completely (usually) therefore it is best to make sure the XML is valid before parsing with something like this:

// Must be tested with ===, as in if(isXML($xml) === true){}
// Returns the error message on improper XML
function isXML($xml){
    libxml_use_internal_errors(true);

    $doc = new DOMDocument('1.0', 'utf-8');
    $doc->loadXML($xml);

    $errors = libxml_get_errors();

    if(empty($errors)){
        return true;
    }

    $error = $errors[0];
    if($error->level < 3){
        return true;
    }

    $explodedxml = explode("r", $xml);
    $badxml = $explodedxml[($error->line)-1];

    $message = $error->message . ' at line ' . $error->line . '. Bad XML: ' . htmlentities($badxml);
    return $message;
}
Anonymous 14-Nov-2011 09:34
Warning to anyone trying to parse XML with a key name that includes a hyphen ie.)
<subscribe>
    <callback-url>example url</callback-url>
</subscribe>

In order to access the callback-url you will need to do something like the following:
<?php
$xml = simplexml_load_string($input);
$callback = $xml->{"callback-url"};
?>
If you attempt to do it without the curly braces and quotes you will find out that you are returned a 0 instead of what you want.
juanfhj at gmail dot spam dot me dot not dot com 14-Aug-2011 08:01
To access the underlying element as a string, it's necessary to make the cast $x = (string)$my_xml_element.
dans at dansheps dot com 22-Apr-2011 09:06
I created a little function to parse the simple XML into a structure which is easier to iterate(I believe anyways) than the simpleXML node structure.

<?php
function parseSimpleXML($xmldata)
    {
        $childNames = array();
        $children = array();

        if( $xmldata->count() !== 0 )
        {
            foreach( $xmldata->children() AS $child )
            {
                $name = $child->getName();

                if( !isset($childNames[$name]) )
                {
                    $childNames[$name] = 0;
                }

                $childNames[$name]++;
                $children[$name][] = $this->parseSimpleXML($child);
            }
        }

        $returndata = new XMLNode();
        if( $xmldata->attributes()->count() > 0 )
        {
            $returndata->{'@attributes'} = new XMLAttribute();
            foreach( $xmldata->attributes() AS $name => $attrib )
            {
                $returndata->{'@attributes'}->{$name} = (string)$attrib;
            }
        }

        if( count($childNames) > 0 )
        {
            foreach( $childNames AS $name => $count )
            {
                if( $count === 1 )
                {
                    $returndata->{$name} = $children[$name][0];
                }
                else
                {
                    $returndata->{$name} = new XMLMultiNode();
                    $counter = 0;
                    foreach( $children[$name] AS $data )
                    {
                        $returndata->{$name}->{$counter} = $data;
                        $counter++;
                    }
                }
            }
        }
        else
        {
            if( (string)$xmldata !== '' )
            {
                $returndata->{'@innerXML'} = (string)$xmldata;
            }
        }
        return $returndata;
    }
?>
ms dot n at 163 dot com 12-Feb-2011 07:50
Adds a new function for SimpleXMLElement class, in order to output HTML code.

<?php
class CeiXML extends SimpleXMLElement{
public function asHTML(){
$ele=dom_import_simplexml($this);
$dom = new DOMDocument('1.0', 'utf-8');
$element=$dom->importNode($ele,true);
$dom->appendChild($element);
return $dom->saveHTML();
}
}
?>
Pavel Musil pavel dot musil at gmail dot com 10-Feb-2011 12:18
Simple recursive function to append XML object into SimpleXMLElement node:

<?php
// root: parent element - SimpleXMLElement instance
// append: XML object from simplexml_load_string

function xml_join($root, $append) {
    if ($append) {
        if (strlen(trim((string) $append))==0) {
            $xml = $root->addChild($append->getName());
            foreach($append->children() as $child) {
                xml_join($xml, $child);
            }
        } else {
            $xml = $root->addChild($append->getName(), (string) $append);
        }
        foreach($append->attributes() as $n => $v) {
            $xml->addAttribute($n, $v);
        }
    }
}

$xml_append = simplexml_load_string('<data><items><item id="1">value</item></items></data>');
$xml_root = new SimpleXMLElement('<result></result>');

$cxml = $xml_root->addChild('clone');
xml_join($cxml, $xml_append->items->item[0]);

print $xml_root->asXML();
?>

Result:
<?xml version="1.0"?>
<result>
    <clone>
        <item id="1">
            value
        </item>
    </clone>
</result>
rmirabelle 08-Nov-2010 06:42
To further previous comments and drive the point home:

What makes SimpleXMLElement tricky to work with is that it feels and behaves like an object, but is actually a system RESOURCE,  (specifically a libxml resource).

That's why you can't store a SimpleXMLElement to $_SESSION or perform straight comparison operations on node values without first casting them to some type of object.  $_SESSION expects to store 'an object' and comparison operators expect to compare 2 'objects' and SimpleXMLElements are not objects.

When you echo or print a node's value, PHP converts the value (a resource) into a string object for you.  It's a time saver for sure, but can fool you into thinking that your SimpleXMLElement is an object.

Hope this helps clarify
joshduck at gmail dot com 29-Sep-2010 02:00
You should cast elements to a float (or even string) if you plan on using them in addition, multiplication, etc. If you don't then PHP will incorrectly treat the node values as an integer.

<?php
$obj = new SimpleXMLElement('<root>
    <a>1.9</a>
    <b>1.9</b>
</root>');

var_dump($obj->a + $obj->b);
var_dump((float)$obj->a + (float)$obj->b);
var_dump((string)$obj->a + (string)$obj->b);
?>

The first line gives: int(2)
The second and third give the expected result: float(3.8)
francs at seznam dot cz 23-Sep-2010 02:28
Be aware when you trying to cast some attribute to boolean.

(boolean)$xml->attributes()->someAtt;

returns TRUE if attribute is array([0] => 0);

use (boolean)(int) instead.
kweij at lsg dot nl 26-Jul-2010 01:47
I'm using SimpleXML for, ofcourse, it's simplicity, however I did wanted to manipulate the xml and combining one SimpleXMLElement with any other, so I wrote this function to add a SimpleXMLElement-child.

<?php
function SimpleXMLElement_append($key, $value) {
    // check class
    if ((get_class($key) == 'SimpleXMLElement') && (get_class($value) == 'SimpleXMLElement')) {
        // check if the value is string value / data
        if (trim((string) $value) == '') {
            // add element and attributes
            $element = $key->addChild($value->getName());
            foreach ($value->attributes() as $attKey => $attValue) {
                $element->addAttribute($attKey, $attValue);
            }
            // add children
            foreach ($value->children() as $child) {
                SimpleXMLElement_append($element, $child);
            }
        } else {
            // set the value of this item
            $element = $key->addChild($value->getName(), trim((string) $value));
        }
    } else {
        // throw an error
        throw new Exception('Wrong type of input parameters, expected SimpleXMLElement');
    }
}
?>

I'd recommend SimpleXMLElement to extend it's addChild() function with the functionalitity above.
cherubangel at gmail dot com 02-Jul-2010 01:28
Note that changing attributes from within a foreach loop, especially namespaced attributes, can be very tricky.

For example, when trying to change the value of an existing xlink:href attribute:
<?php
foreach($xmlelement -> attributes('xlink', true) as $attribute => $attribvalue){
    $attribvalue[0] = 'value'; // Throws an error
    $attribvalue = 'value'; // Does not change your XML
    $xmlelement -> addAttribute($attribute, 'value', 'http://www.w3.org/1999/xlink'); // Adds an attribute, does not change existing one.
    $xmlelement[$attribute] = 'value'; // Adds an attribute, does not change existing one.
}
?>

Instead, you should access the array returned by the attributes() function directly, like this:
<?php
    $xmlelement -> attributes('xlink', true) -> href = 'value'; // Works!
?>
php at keith tyler dot com 20-Feb-2010 03:23
The root node element of your input XML string is not retrievable as a property.

<?php
$xml="<foo>bar</foo>";
$sxe=new SimpleXMLElement($xml);
print $sxe->foo;
?>

prints nothing. You can only get to the root element via the array index method ($sxe[0]).

Also, you may not have two (or more) root elements -- that is apparently not well-formed XML.

<?php
$xml="<foo/><bar/>";
$sxe=new SimpleXMLElement($xml);
?>

throws an exception. A Q&D is to append an arbitraty root node structure to both ends of the input:

<?php
$xml="<foo/><bar/>";
$sxe=new SimpleXMLElement("<z>".$xml."</z>");
?>

Doing this also solves the above problem of root node property accessibility. (It may not work if your XML string includes a declaration.)
triplepoint at gmail dot com 20-Dec-2009 01:19
It's occasionally useful to add an XML processing instruction to a SimpleXMLElement (treating it as if it were a full document).
<?php
class SimpleXMLElement_Plus extends SimpleXMLElement {

    public function addProcessingInstruction( $name, $value )
    {
        // Create a DomElement from this simpleXML object
        $dom_sxe = dom_import_simplexml($this);

        // Create a handle to the owner doc of this xml
        $dom_parent = $dom_sxe->ownerDocument;

        // Find the topmost element of the domDocument
        $xpath = new DOMXPath($dom_parent);
        $first_element = $xpath->evaluate('/*[1]')->item(0);

        // Add the processing instruction before the topmost element
        $pi = $dom_parent->createProcessingInstruction($name, $value);
        $dom_parent->insertBefore($pi, $first_element);
    }
}
?>

For example, if you had a simpleXMLElement_Plus object made out of the xml fragment:
<xml><content /></xml>

And you needed the output to be:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="xsl/xsl.xsl"?>
<xml><content/></xml>

you could do (using the class above):
<?php
$xml = new SimpleXMLElement_Plus('<xml><content /></xml>');
$xml->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="xsl/xsl.xsl"');
echo $xml->asXML();
?>
ivandosreisandrade at gmail dot com 20-Nov-2009 03:55
Hello,

here goes my contribution for those whom are struggling to understand how SimpleXMLElement works.

After some time trying to figure out how this works, I've came up to this small example:

<?php
    $xmlstr = "<?xml version='1.0' ?>\n".
              // optionally you can specify a xml-stylesheet for presenting the results. just uncoment the following line and change the stylesheet name.
              /* "<?xml-stylesheet type='text/xsl' href='xml_style.xsl' ?>\n". */
              "<book></book>";

    // create the SimpleXMLElement object with an empty <book> element
    $xml = new SimpleXMLElement($xmlstr);

    // add some child nodes
    $xml->addChild("title", "Title of my book");
    $xml->addChild("abstract", "My book is about learning to work with SimpleXMLElement");

    // add some more child nodes
    $chapter1 = $xml->addChild("chapter_1");
    // add an attribute to child chapter_1
    $chapter1->addAttribute("chapter_title", "Introduction to my book");

    $chapter2 = $xml->addChild("chapter_2");
    $chapter2->addAttribute("chapter_title", "Development of my book");

    $chapter3 = $xml->addChild("chapter_3");
    $chapter3->addAttribute("chapter_title", "Another chapter of my book");

    $conclusion = $xml->addChild("conclusion", "The ending of my book");

    // insert the header to tell the browser how to read the document
    header("Content-type: text/xml");
    // print the SimpleXMLElement as a XML well-formed string
    echo $xml->asXML();
?>

With this script you can just copy-paste and try to understand how it works.
I hope it can help somebody :)
brett at brettbrewer dot com 28-Oct-2009 01:53
Figuring out how to access the properties of a SimpleXmlElement object was a little tricky for me. In particular, it took a while to discover that I needed to cast my SimpleXmlElement properties to be of type "string" to print them or do comparisons on them. For instance, assuming you already have a string of xml in $xmlstr...

<?php
$sxml= new SimpleXmlElement($xmlstr);

if ((string) $sxml->property== "somevalue") {
    echo (string) $sxml->property;
}
?>
The properties of a SimpleXmlElement object are objects themselves, so you need to put "(string)" before them, which casts their values to a string instead of an object. I assume if you were doing a numeric comparison you'd want to cast to an (int) or something numeric instead.


Adds an attribute to the SimpleXML element

  • ≪ SimpleXMLElement
  • SimpleXMLElement::addChild ≫
  • PHP Manual
  • SimpleXMLElement
  • Adds an attribute to the SimpleXML element

SimpleXMLElement::addAttribute

(PHP 5 >= 5.1.3, PHP 7)

SimpleXMLElement::addAttribute — Adds an attribute to the SimpleXML element

설명

public void SimpleXMLElement::addAttribute ( string $name [, string $value [, string $namespace ]] )

Adds an attribute to the SimpleXML element.

인수

name

The name of the attribute to add.

value

The value of the attribute.

namespace

If specified, the namespace to which the attribute belongs.

반환값

값을 반환하지 않습니다.

예제

Note:

Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide.

Example #1 Add attributes and children to a SimpleXML element

<?php

include 'example.php';

$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');

$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');

$characters = $movie->addChild('characters');
$character  = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');

$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');

echo $sxe->asXML();

?>

위 예제의 출력 예시:

<?xml version="1.0" standalone="yes"?>
<movies type="documentary">
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#xD3;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
 <movie>
  <title>PHP2: More Parser Stories</title>
  <plot>This is all about the people who make it work.</plot>
  <characters>
   <character>
    <name>Mr. Parser</name>
    <actor>John Doe</actor>
   </character>
  </characters>
  <rating type="stars">5</rating>
 </movie>
</movies>

참고

  • SimpleXMLElement::addChild() - Adds a child element to the XML node

User Contributed Notes

Patanjali 10-Apr-2016 07:37
You cannot use this to update an existing attribute's value.

Instead, get a DOM version of the SimpleXMLElement and use setAttribute, as per:

$XML = '<element a="aa">Text</element>';
$snode = new simple_xml_element($XML);

$dnode = dom_import_simplexml($snode);
$dnode->setAttribute('a', 'bb');

Resulting XML for the node is:
<element a="bb">Text</element>
p.servus 19-Nov-2011 02:42
If you need an Namespace, you must write a Prefix of the Namespace + the Name of the Attribute ("prefix:name") as the first parameter AND the uri as the third one. (I don't know why?!)

Example code:
<?php
$xml = new SimpleXMLElement("<packagedElement></packagedElement>");
$xml->addAttribute("xmi:type", "uml:Class", "http://schema.omg.org/spec/XMI/2.1");
echo $xml->asXml();
?>

Output:
<packagedElement xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmi:type="uml:Class"></packagedElement>
sarlak 07-May-2011 04:49
You can access a node child with his name this way :

<?php
$root = new SimpleXMLElement($filePath);
echo $root->nodeName->attributes;

// It works recursivly so this will work too
echo $root->nodeName->subNodeName->attributes();
?>

But if you want to ADD an attribute to a children you MUST use the children() method to access and modfiy it or it will modify the parent's attributes.

<?php
// Add a subnode
$element->nodeName->addChild('subNodeName', "whatever you want");

// Get the pos in the childrens of the parent node
$lastNodePos = $element->nodeName->count()-1;

// Get the parents node childrens
$nodeChildrens = $element->nodeName->children();

// Add an attribute to the last created
$nodeChildrens[$lastNodePos]->addAttribute('attributeName', "The attribute value);
?>
booleer at yahoo dot it 16-Dec-2010 06:44
If the attribute already exists, addAttribute does nothing.

example:
<?php
$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <item id="foo">
<root>
XML;

$xml = simplexml_load_string($xml_string);
$xml->item->addAttribute('id', 'bar');
echo $xml->asXML();
?>

The above examples will output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <item id="foo">
<root>


Adds a child element to the XML node

  • ≪ SimpleXMLElement::addAttribute
  • SimpleXMLElement::asXML ≫
  • PHP Manual
  • SimpleXMLElement
  • Adds a child element to the XML node

SimpleXMLElement::addChild

(PHP 5 >= 5.1.3, PHP 7)

SimpleXMLElement::addChild — Adds a child element to the XML node

설명

public SimpleXMLElement SimpleXMLElement::addChild ( string $name [, string $value [, string $namespace ]] )

Adds a child element to the node and returns a SimpleXMLElement of the child.

인수

name

The name of the child element to add.

value

If specified, the value of the child element.

namespace

If specified, the namespace to which the child element belongs.

반환값

The addChild method returns a SimpleXMLElement object representing the child added to the XML node.

예제

Note:

Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide.

Example #1 Add attributes and children to a SimpleXML element

<?php

include 'example.php';

$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');

$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');

$characters = $movie->addChild('characters');
$character  = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');

$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');

echo $sxe->asXML();

?>

위 예제의 출력 예시:

<?xml version="1.0" standalone="yes"?>
<movies type="documentary">
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#xD3;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
 <movie>
  <title>PHP2: More Parser Stories</title>
  <plot>This is all about the people who make it work.</plot>
  <characters>
   <character>
    <name>Mr. Parser</name>
    <actor>John Doe</actor>
   </character>
  </characters>
  <rating type="stars">5</rating>
 </movie>
</movies>

참고

  • SimpleXMLElement::addAttribute() - Adds an attribute to the SimpleXML element

User Contributed Notes

johninen at gmail dot com 14-Sep-2015 10:27
In the docs for google sitemaps it is required an element for mobile sitemaps that looks like this: <mobile:mobile/>

I used some time to figure out how to make it, but it is quite simple when understood.

$mobile_schema = 'http://www.google.com/schemas/sitemap-mobile/1.0';

//Create root element
$xml_mobile = new SimpleXMLElement('
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="'.$mobile_schema.'"></urlset>
');

//Add required children
$url_mobile = $xml_b_list_mobile->addChild('url');
$url_mobile->addChild('loc', 'your-mobile-site-url');
$url_mobile->addChild('mobile:mobile', null, $mobile_schema);

For this to work properly the attribute xmlns:mobile must be set in the root node, and then used as namespace(third argument) when creating the mobile:mobile child with null as value.
thallisphp at gmail dot com 08-May-2014 04:35
<?php
class MySimpleXMLElement extends SimpleXMLElement
{
    /**
     * Add SimpleXMLElement code into a SimpleXMLElement
     *
     * @param SimpleXMLElement $append
     */
    public function appendXML($append)
    {
        if ($append) {
            if (strlen(trim((string)$append)) == 0) {
                $xml = $this->addChild($append->getName());
            } else {
                $xml = $this->addChild($append->getName(), (string)$append);
            }

            foreach ($append->children() as $child) {
                $xml->appendXML($child);
            }

            foreach ($append->attributes() as $n => $v) {
                $xml->addAttribute($n, $v);
            }
        }
    }
}
?>
frosty dot z at freesbee dot fr 16-May-2013 11:31
To complete Volker Grabsch's comment, stating :
"Note that although addChild() escapes "<" and ">", it does not escape the ampersand "&"."

To work around that problem, you can use direct property assignment such as :

<?php
$xmlelement->value = 'my value < > &';
// results in <value>my value &lt; &gt; &amp;</value>
?>

instead of doing :

<?php
$xmlelement->addChild('value', 'my value < > &');
// results in <value>my value &lt; &gt; &</value> (invalid XML)
?>

See also: http://stackoverflow.com/questions/552957 (Rationale behind SimpleXMLElement's handling of text values in addChild and addAttribute)

HTH
passerbyxp at gmail dot com 27-Mar-2013 12:59
Be aware that simply because you can <?php $dom->addChild();?> doesn't mean your XML is valid under simple XML itself:

<?php
$dom=simplexml_load_string("<test></test>");
$dom->addChild("3D","1,2,3");
$xml=$dom->asXML();
echo $xml;
/* echos:
<?xml version="1.0"?>
<test><3D>1,2,3</3D></test>
*/

$dom=simplexml_load_string($xml);
/*
generates a whole lots of warnings,
and refuses to create an object.
*/
?>

Same in PHP 5.3 and 5.4
ivan dot kakurov at gmail dot com 13-Jan-2013 01:09
Here's my solution for creating XML from Multidimensional Array.
<?php
//DATA
        $xmlDAta = array(
            array(
                "name"  => "nameVal",
                "value" => "valVal",
                "css"   => "cssVal"
            ),
            array(
                "name"  => "name1Val",
                "value" => "val1Val",
                "css"   => "css1Val"
            ),
            "tname" => array(
                array(
                    "iTname"   => "iTname",
                    "iTname2"  => "iTname1",
                    "iTname2"  => "iTname2",
                    "iTbname3" => array(
                        "iiTbname"  => "tbName",
                        "iiTbname1" => "tbName1",
                    ),
                ),
            ),
            "tdata" => "otheerDAta"
        );

   /**
     * Create XML using string or array
     *
     * @param mixed $data input data
     * @param SimpleXMLElement $xml
     * @param string $child name of first level child
     *
     * @return adding Xml formated data into SimpleXmlElement
     */

function data2XML(array $data, SimpleXMLElement $xml, $child = "items")
    {

        foreach($data as $key => $val) {
            if(is_array($val)) {

                if(is_numeric($key)) {
                    $node  = $xml->addChild($child);
                    $nodes = $node->getName($child);
                } else {

                    $node  = $xml->addChild($key);
                    $nodes = $node->getName($key);
                }

                $node->addChild($nodes, self::data2Xml($val, $node));
            } else {
                $xml->addChild($key, $val);
            }
        }

    }

//Use

        $xml    = new SimpleXMLElement("<root/>");
        Util::data2XML($xmlDAta, $xml, "Items");

?>
jerikojerk 31-Jul-2011 04:32
If you're looking for a way to append children you may be interested in this:

<?php
$x = new SimpleXMLElement('<root name="toplevel"></root>');
$f1 = new SimpleXMLElement('<child pos="1">alpha</child>');
$f2 = new SimpleXMLElement('<child pos="2">beta</child>');
$f3 = new SimpleXMLElement('<child pos="3">gamma</child>');

$x->{$f1->getName()} = $f1;
$x->{$f2->getName()}[] = $f2;
$x->{$f3->getName()}[] = $f3;

echo 'count child=',$x->count(),"\n";
echo $x->asXML();

foreach ( $x->children() as $foo )
{
    var_dump($foo);
}

?>
alex dot feraud at gmail dot com 16-Jun-2011 11:52
Here is a class with more functions for SimpleXMLElement :

<?php
/**
 *
 * Extension for SimpleXMLElement
 * @author Alexandre FERAUD
 *
 */
class ExSimpleXMLElement extends SimpleXMLElement
{
    /**
     * Add CDATA text in a node
     * @param string $cdata_text The CDATA value  to add
     */
  private function addCData($cdata_text)
  {
   $node= dom_import_simplexml($this);
   $no = $node->ownerDocument;
   $node->appendChild($no->createCDATASection($cdata_text));
  }

  /**
   * Create a child with CDATA value
   * @param string $name The name of the child element to add.
   * @param string $cdata_text The CDATA value of the child element.
   */
    public function addChildCData($name,$cdata_text)
    {
        $child = $this->addChild($name);
        $child->addCData($cdata_text);
    }

    /**
     * Add SimpleXMLElement code into a SimpleXMLElement
     * @param SimpleXMLElement $append
     */
    public function appendXML($append)
    {
        if ($append) {
            if (strlen(trim((string) $append))==0) {
                $xml = $this->addChild($append->getName());
                foreach($append->children() as $child) {
                    $xml->appendXML($child);
                }
            } else {
                $xml = $this->addChild($append->getName(), (string) $append);
            }
            foreach($append->attributes() as $n => $v) {
                $xml->addAttribute($n, $v);
            }
        }
    }
}
?>
Volker Grabsch 21-Apr-2011 03:17
Note that although addChild() escapes "<" and ">", it does not escape the ampersand "&".

So addChild() is unsuited to handle user-defined input!

Instead, you will have to replace all "&" with "&amp;" before calling addChild().

Or, use htmlspecialchars() which also replaces other characters, but won't do any harm as addChild() won't replace those again.
felipenmoura at gmail dot com 12-Apr-2011 03:06
This method returns a reference to the specific SimpleXMLElement.
If you use:
<?php
    $xml= new SimpleXMLElement('<root></root>');
    $xml->father['name']= 'Fathers name'; // creates automatically a father tag with attribute name
    $son= $xml->father->addChild('son'); // uses the first father tag
    $son['name']= 'first son';
    $otherSon= $xml->father->addChild('son'); // uses the first father tag but now, in a second son tag
    $otherSon['name']= 'second son';

    echo htmlentities($xml->asXML());
?>

The result will be
<root>
    <father>
        <son name='first son' />
        <son name='second son' />
    </father>
</root>

So, once you change something to the just added child, you are actually accessing the element inside the SimpleXMLElement as a reference.


Return a well-formed XML string based on SimpleXML element

  • ≪ SimpleXMLElement::addChild
  • SimpleXMLElement::attributes ≫
  • PHP Manual
  • SimpleXMLElement
  • Return a well-formed XML string based on SimpleXML element

SimpleXMLElement::asXML

(PHP 5 >= 5.0.1, PHP 7)

SimpleXMLElement::asXML — Return a well-formed XML string based on SimpleXML element

설명

public mixed SimpleXMLElement::asXML ([ string $filename ] )

The asXML method formats the parent object's data in XML version 1.0.

인수

filename

If specified, the function writes the data to the file rather than returning it.

반환값

If the filename isn't specified, this function returns a string on success and FALSE on error. If the parameter is specified, it returns TRUE if the file was written successfully and FALSE otherwise.

예제

Example #1 Get XML

<?php
$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml = new SimpleXMLElement($string);

echo $xml->asXML();

?>

위 예제의 출력:

<?xml version="1.0"?>
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>

asXML also works on Xpath results:

Example #2 Using asXML() on SimpleXMLElement::xpath() results

<?php
// Continued from example XML above.

/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');

while(list( , $node) = each($result)) {
    echo $node->asXML();
}
?>

위 예제의 출력:

<c>text</c><c>stuff</c>

참고

  • SimpleXMLElement::__toString() - Returns the string content

User Contributed Notes

vergisses at mailnesia dot com 04-Dec-2014 05:22
This is dumb. It only shows you how to display pretty XML, not how to save it and have a pretty formatted XML file.
andreas dot theissen at t-online dot de 27-Oct-2012 09:37
To prevent asXML from encoding vowels unwantedly, simply use an approriate XML header with encoding in advance.

If you do so, asXML will happily leave your vowels (and the header) entirely untouched.

<?php

$xmlstr =
'<?xml version="1.0" encoding="UTF-8"?>
<keys>
  <key lang="en">&lt;Insert&gt;</key>
  <key lang="de">&lt;Einfugen&gt;</key>
</keys>';

$sxe = new SimpleXMLElement($xmlstr);

$output = $sxe->asXML();

?>

$xmlstr and $output are identical now.

The subsequent use of html_entity_decode() (as proposed in the very beginning in another post) has several drawbacks:

1. It is slow
2. It is expensive
3. If there are already encoded arrow brackets or double quotes in your source for instance (as shown in the above example), markup will be broken.
oleg dot pavlin at gmail dot com 10-Jan-2012 01:17
Function asXML decodes special chars like ø, æ and others to &#xE6;, &#xF8;

To get normal output use without quoting:

$xml = html_entity_decode($xml, ENT_NOQUOTES, 'UTF-8');
jcr at di dot uminho dot pt 25-Jan-2011 11:32
Code snippet to load a XML document, increment an attribute and store it again.
My XML looks like:
<?xml version="1.0" encoding="ISO-8859-1"?>
<doctypes counter="16">
    <doctype id="d1">
        <name>Carta</name>
        <acro>CA</acro>
    </doctype>
...
</doctypes>

I want to retrieve the counter attribute, increment it, and store the new document at the end:

<?php
  $document = simplexml_load_file("mydoc.xml");
  $cont = (integer) $document['counter'];
  $document['counter'] = $cont+1;
  $document->asXML("mydoc.xml");
?>


Identifies an element's attributes

  • ≪ SimpleXMLElement::asXML
  • SimpleXMLElement::children ≫
  • PHP Manual
  • SimpleXMLElement
  • Identifies an element's attributes

SimpleXMLElement::attributes

(PHP 5 >= 5.0.1, PHP 7)

SimpleXMLElement::attributes — Identifies an element's attributes

설명

public SimpleXMLElement SimpleXMLElement::attributes ([ string $ns = NULL [, bool $is_prefix = false ]] )

This function provides the attributes and values defined within an xml tag.

Note: SimpleXML은 대부분의 메쏘드에 추가 반복자 속성 규칙을 가지고 있습니다. 이들은 var_dump()나 객체를 조사하는 어떠한 것으로도 볼 수 없습니다.

인수

ns

An optional namespace for the retrieved attributes

is_prefix

Default to FALSE

반환값

Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.

Returns NULL if called on a SimpleXMLElement object that already represents an attribute and not a tag.

예제

Example #1 Interpret an XML string

<?php
$string = <<<XML
<a>
 <foo name="one" game="lonely">1</foo>
</a>
XML;

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}
?>

위 예제의 출력:

name="one"
game="lonely"

참고

User Contributed Notes

andy dot locologic+101 at gmail dot com 14-Aug-2015 07:00
Easiest and safest way to get attributes as an array is to use the iterator_to_array function (see http://php.net/manual/en/function.iterator-to-array.php):

<?php
      $x = new SimpleXMLElement('<div class="myclass" id="myid"/>');
      $attributes = iterator_to_array($x->attributes());
?>
zevnikrok 19-Dec-2014 08:01
If you wish to get attributes as an associative array of key value pairs, you don't need to iterate through them, you can get it with this one liner:

  reset($element->attributes())
webfelipemaia at gmail dot com 07-Sep-2013 02:11
Use attributes to display when it meets certain condition defined attribute / value in xml tags.

Use atributos para exibir quando atende determinada condicao definida atributo / valor em tags XML.

Consider the following example:
Considere o seguinte exemplo:

(file.xml)

<?xml version="1.0" encoding="UTF-8"?>

<list>
    <item type="Language">
        <name>PHP</name>
        <link>www.php.net</link>
    </item>
    <item type="Database">
        <name>Java</name>
        <link>www.oracle.com/br/technologies/java/?</link>
    </item>
</list>

Checks if the attribute value equals "Language", if equal prints everything that is related to "Language".

Verifica se o valor do atributo e igual a "Language", se for, imprime tudo o que for relativo ao mesmo.

<?php

$xml = simplexml_load_file("file.xml");

foreach($xml->children() as $child) {

        $role = $child->attributes();

        foreach($child as $key => $value) {

            if($role == "Language")
            echo("[".$key ."] ".$value . "<br />");

        }
}
?>

output:
saida:

[name] PHP
[link] www.php.net
adrian at foeder dot de 14-Jun-2013 07:15
in order to get a possibly present xml:space attribute, use the following construct:

<?php
$simpleXmlElement->element->attributes('http://www.w3.org/XML/1998/namespace')->space;
?>

This will, for example, return 'preserve' if set.

Just passing by 'xml' as namespace argument of the attribute() method didn't work out for me. Passing by the complete namespace URI works also if it is not explicitly defined in the underlying XML document.

Also, $simpleXmlElement->getNamespaces() does not return anything of use.
totalwipeout at gmail dot com 09-Nov-2012 02:21
Tip to get a real array of all attributes of a node (not SimpleXML's object acting like an array)

<?php
//- $node is a SimpleXMLElement object

$atts_object = $node->attributes(); //- get all attributes, this is not a real array
$atts_array = (array) $atts_object; //- typecast to an array

//- grab the value of '@attributes' key, which contains the array your after
$atts_array = $atts_array['@attributes'];

var_dump($atts_object); //- outputs object(SimpleXMLElement)[19]
                        //-             public '@attributes' => ...

var_dump($atts_array); //- outputs array (size=11) ...

?>
Hope this helps!
chris at chlab dot ch 23-Mar-2012 07:16
Note that you must provide the namespace if you want to access an attribute of a non-default namespace:

Consider the following example:

<?php
$xml = <<<XML
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
 <Table Foo="Bar" ss:ExpandedColumnCount="7">
 </Table>
</Workbook>
XML;

$sxml = new SimpleXMLElement($xml);

/**
 * Access attribute of default namespace
 */
var_dump((string) $sxml->Table[0]['Foo']);
// outputs: 'Bar'

/**
 * Access attribute of non-default namespace
 */
var_dump((int) $sxml->Table[0]['ExpandedColumnCount']);
// outputs: 0

var_dump((int) $sxml->Table[0]->attributes('ss', TRUE)->ExpandedColumnCount);
// outputs: '7'
?>
Andrei 17-May-2011 01:46
Reading the attributes of the root element with name space prefixes as in an Atom feed

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/&quot;Ck4EQXYzeCp7ImA9WhZWFE4.&quot;">

<?php
$xml =  @simplexml_load_file($feed);
$att_gd = $xml->attributes("gd",1);
$Etag = $att_gd["etag"];
?>
sarlak 27-Apr-2011 10:12
<?php
$att = 'attribueName';

// You can access an element's attribute just like this :
$attribute = $element->attributes()->$att;

// This will save the value of the attribute, and not the objet
$attribute = (string)$element->attributes()->$att;

// You also can edit it this way :
$element->attributes()->$att = 'New value of the attribute';
?>
leap 02-Apr-2011 06:07
If you want to save the value of an attribute into an array, typecast it to a string.

<?php
// stores an xml-element-object
$dataStore['value'] = $attributes->myValue

// stores the value of the attribute
$dataStore['value'] = (string)$attributes->myValue
?>
alan at performantsystems dot com 24-May-2010 07:26
So lets say you have database type data in an XML string called $xmlstring with the key or item ID as an XML Attribute and all content data as regular XML Elements, as above. SimpleXML processes the Attributes as an array, so we can play along and push the Attributes into an array. Then we can get the value of any specific Attribute we want by addressing it by name, such as "ID".

Considering this data:

<?xml version="1.0" encoding="utf-8"?>
<data>
   <item ID="30001">
      <Company>Navarro Corp.</Company>
   </item>
   <item ID="30002">
      <Company>Performant Systems</Company>
   </item>
   <item ID="30003">
      <Company>Digital Showcase</Company>
   </item>
</data>

Example of listing both the ID Attribute and Company Element values:

<?php
$xmlObject = new SimpleXMLElement($xmlstring);
foreach ($xmlObject->children() as $node) {
        $arr = $node->attributes();   // returns an array
        print ("ID=".$arr["ID"]);     // get the value of this attribute
        print ("  Company=".$node->Company);
        print ("<p><hr>");
}
?>
Xeoncross 11-Apr-2010 04:46
It is really simple to access attributes using array form. However, you must convert them to strings or ints if you plan on passing the values to functions.

<?php
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [id] => 55555
        )

    [text] => "hello world"
)
?>

Then using a function

<?php
function xml_attribute($object, $attribute)
{
    if(isset($object[$attribute]))
        return (string) $object[$attribute];
}
?>

I can get the "id" like this

<?php
print xml_attribute($xml, 'id'); //prints "55555"
?>
gillllberg at gmail dot com 04-Nov-2009 11:21
To get an attribute in the node, use node->attributes()->attributeName
skerr at mojavi dot org 10-Dec-2004 07:55
You can also access the node as an array to get attributes:

<?php

$xml = simplexml_load_file('file.xml');

echo 'Attribute: ' . $xml['attribute'];

?>
inge at elektronaut dot no 26-May-2004 07:53
here's a simple function to get an attribute by name, based on the example

<?php
function findAttribute($object, $attribute) {
  foreach($object->attributes() as $a => $b) {
    if ($a == $attribute) {
      $return = $b;
    }
  }
  if($return) {
    return $return;
  }
}
?>


Finds children of given node

  • ≪ SimpleXMLElement::attributes
  • SimpleXMLElement::__construct ≫
  • PHP Manual
  • SimpleXMLElement
  • Finds children of given node

SimpleXMLElement::children

(PHP 5 >= 5.0.1, PHP 7)

SimpleXMLElement::children — Finds children of given node

설명

public SimpleXMLElement SimpleXMLElement::children ([ string $ns [, bool $is_prefix = false ]] )

This method finds the children of an element. The result follows normal iteration rules.

Note: SimpleXML은 대부분의 메쏘드에 추가 반복자 속성 규칙을 가지고 있습니다. 이들은 var_dump()나 객체를 조사하는 어떠한 것으로도 볼 수 없습니다.

인수

ns

An XML namespace.

is_prefix

If is_prefix is TRUE, ns will be regarded as a prefix. If FALSE, ns will be regarded as a namespace URL.

반환값

Returns a SimpleXMLElement element, whether the node has children or not.

변경점

버전 | 설명
5.2.0 | The optional parameter is_prefix was added.

예제

Example #1 Traversing a children() pseudo-array

<?php
$xml = new SimpleXMLElement(
'<person>
 <child role="son">
  <child role="daughter"/>
 </child>
 <child role="daughter">
  <child role="son">
   <child role="son"/>
  </child>
 </child>
</person>');

foreach ($xml->children() as $second_gen) {
    echo ' The person begot a ' . $second_gen['role'];

    foreach ($second_gen->children() as $third_gen) {
        echo ' who begot a ' . $third_gen['role'] . ';';

        foreach ($third_gen->children() as $fourth_gen) {
            echo ' and that ' . $third_gen['role'] .
                ' begot a ' . $fourth_gen['role'];
        }
    }
}
?>

위 예제의 출력:

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son

Example #2 Using namespaces

<?php
$xml = '<example xmlns:foo="my.foo.urn">
  <foo:a>Apple</foo:a>
  <foo:b>Banana</foo:b>
  <c>Cherry</c>
</example>';

$sxe = new SimpleXMLElement($xml);

$kids = $sxe->children('foo');
var_dump(count($kids));

$kids = $sxe->children('foo', TRUE);
var_dump(count($kids));

$kids = $sxe->children('my.foo.urn');
var_dump(count($kids));

$kids = $sxe->children('my.foo.urn', TRUE);
var_dump(count($kids));

$kids = $sxe->children();
var_dump(count($kids));
?>
int(0)
int(2)
int(2)
int(0)
int(1)

주의

SimpleXMLElement::children() returns a node object no matter if the current node has children or not. Use count() on the return value to see if there are any children. As of PHP 5.3.0, SimpleXMLElement::count() may be used instead.

참고

  • SimpleXMLElement::count() - Counts the children of an element
  • count() - 배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다

User Contributed Notes

thetazzbot at gmail dot com 09-Sep-2011 03:56
Ok so say you have this xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE nitf
PUBLIC "-//IPTC-NAA//DTD NITF 3.1//EN"
       "http://www.nitf.org/site/nitf-documentation/nitf-3-1.dtd">
<nitf>
  <head>
    <docdata management-status="embargoed">
      <!--data/2011/08/23/parties/doc4e53aa5783cca930694817.txt-->
      <doc-id id-string="doc4e53aa5783cca930694817"/>
      <date.release norm="20110823T000000"/>
      <key-list/>
    </docdata>
    <pubdata type="web" position.section="parties" position.sequence="0"/>
  </head>

-- snip --

If you need to access the nodes named "like.this" such as the date.release above, here is how I did it:

$xml = new SimpleXMLElement($xhtml);
 echo (string)$xml->head->docdata->{"date.release"}["norm"];

Took me a while to figure that one out ;)
coldshine1 at rambler dot ru 10-Dec-2010 11:37
This is my recursive simple function to parse XML with attributes.

<?php
function ParseXML($node, &$parent=array(), $only_child=true) {

        //Current node name
        $node_name = $node->getName();

        //Let's count children
        $only_child = true;
        if($node->count() > 1 ) $only_child = false;

        //If there is no child, then there may be text data
        if($only_child){
            $content="$node";
            if (strlen($content)>0) $parent['content']=$content;
        }

        //Get attributes of current node
        foreach ($node->attributes() as $k=>$v) {
            $parent['@attributes'][$k]="$v";
        }

        //Get children
        $count = 0;
        foreach ($node->children() as $child_name=>$child_node) {
            if(!$only_child) //If there are siblings then we'll add node to the end of the array
                LGServerPVR::ParseXML($child_node, $parent[$node_name][$child_name][$count], $only_child);
            else
                LGServerPVR::ParseXML($child_node, $parent[$node_name][$child_name], $only_child);
            $count++;
        }

        return $parent;
        //(c) c01d[s]h!nE 10.12.2010
    }

?>
Anonymous 26-Oct-2010 02:19
Here's my xml-to-array routine. Unlike many of the other comments here, this one takes namespaces into account.

<?php

class XmlArray {

  public function load_dom ($xml) {
    $node=simplexml_import_dom($xml);
    return $this->add_node($node);
  }

  public function load_string ($s) {
    $node=simplexml_load_string($s);
    return $this->add_node($node);
  }

  private function add_node ($node, &$parent=null, $namespace='', $recursive=false) {

    $namespaces = $node->getNameSpaces(true);
    $content="$node";

    $r['name']=$node->getName();
    if (!$recursive) {
      $tmp=array_keys($node->getNameSpaces(false));
      $r['namespace']=$tmp[0];
      $r['namespaces']=$namespaces;
    }
    if ($namespace) $r['namespace']=$namespace;
    if ($content) $r['content']=$content;

    foreach ($namespaces as $pre=>$ns) {
      foreach ($node->children($ns) as $k=>$v) {
        $this->add_node($v, $r['children'], $pre, true);
      }
      foreach ($node->attributes($ns) as $k=>$v) {
        $r['attributes'][$k]="$pre:$v";
      }
    }
    foreach ($node->children() as $k=>$v) {
      $this->add_node($v, $r['children'], '', true);
    }
    foreach ($node->attributes() as $k=>$v) {
      $r['attributes'][$k]="$v";
    }

    $parent[]=&$r;
    return $parent[0];

  }

}

?>
mrdaniellee at live dot com 10-Jan-2010 06:11
use this If you want to view the HTML as well as data.

normal dumps display the parsed versions of HTML.
This code displays the HTML as Text in a Text Area, and also display all other a data along side as an array

perfect for debugging (xml with html).

Try This Code:

<?php
$xml  = simplexml_load_file($url);

function xml2array_parse($xml){
     foreach ($xml->children() as $parent => $child){
         $return["$parent"] = xml2array_parse($child)?xml2array_parse($child):"$child";
     }
     return $return;
 }

 print "<pre><textarea style=\"width:200%;height:100%;\">";
 print_r(xml2array_parse($xml));
 print "</textarea></pre>";
?>
rodmen at gmail dot com 21-May-2009 05:32
Transform xml to array php

<?php
function xml2phpArray($xml,$arr){
    $iter = 0;
        foreach($xml->children() as $b){
                $a = $b->getName();
                if(!$b->children()){
                        $arr[$a] = trim($b[0]);
                }
                else{
                        $arr[$a][$iter] = array();
                        $arr[$a][$iter] = xml2phpArray($b,$arr[$a][$iter]);
                }
        $iter++;
        }
        return $arr;
}

$Array = simplexml_load_string(file_get_contents('myfile.xml'));
print_r(xml2phpArray($Array,array()));
?>
Anonymous 06-Sep-2008 02:39
for XML namespaces such as <dc:creator> in RSS feeds use

<?php
    $xml = new SimpleXMLElement($string);
    $item = $xml->channel[0]->item[0];
    $dc = $item->children("http://purl.org/dc/elements/1.1/");
    echo $dc->creator;
?>
transglobe at gmx dot de 19-Mar-2008 11:37
I made a slightly differnt approch towards the RecurseXML function. Beeing hungry I had problems with the code, as it did just overwrite two <maincourse>s. So here is what I did:

<?php

$xml = new SimpleXMLElement(
'<meal>
   <type>Lunch</type>
   <time>12:30</time>
   <menu>
     <entree>salad</entree>
     <maincourse>
        <part>ships</part>
        <part>steak</part>
     </maincourse>
     <maincourse>
        <part>fisch</part>
        <part>rice</part>
     </maincourse>
     <maincourse>
        <part>wine</part>
        <part>cheese</part>
     </maincourse>
   </menu>
</meal>');

$vals = array();
RecurseXML($xml,$vals);

foreach($vals as $key=>$value)
  print("{$key} = {$value}<BR>\n");

function RecurseXML($xml,&$vals,$parent="") {

  $childs=0;
  $child_count=-1; # Not realy needed.
  $arr=array();
        foreach ($xml->children() as $key=>$value) {
                if (in_array($key,$arr)) {
                        $child_count++;
                } else {
                        $child_count=0;
                }
                $arr[]=$key;
                $k=($parent == "") ? "$key.$child_count" : "$parent.$key.$child_count";
                $childs=RecurseXML($value,$vals,$k);
                if ($childs==0) {
                        $vals[$k]= (string)$value;
                }
        }

  return $childs;
}

?>
Output is like this:
type.0 = Lunch
time.0 = 12:30
menu.0.entree.0 = salad
menu.0.maincourse.0.part.0 = ships
menu.0.maincourse.0.part.1 = steak
menu.0.maincourse.0 =
menu.0.maincourse.1.part.0 = fisch
menu.0.maincourse.1.part.1 = rice
menu.0.maincourse.1 =
menu.0.maincourse.2.part.0 = wine
menu.0.maincourse.2.part.1 = cheese
menu.0.maincourse.2 =
menu.0 =

(Not beautiful, but it solved my case...)
crescentfreshpot at yahoo dot com 10-Dec-2007 05:16
Just a warning that the iterable returned from children() contains the '@attributes' key, which is "invisible" during a foreach but can be seen if using a different construct, such as list()=each() or casting to an array before iterating w/ foreach.
aero 23-May-2007 05:07
Here's a simple, recursive, function to transform XML data into pseudo E4X syntax ie. root.child.value = foobar

<?php
error_reporting(E_ALL);

$xml = new SimpleXMLElement(
'<Patriarch>
   <name>Bill</name>
   <wife>
     <name>Vi</name>
   </wife>
   <son>
     <name>Bill</name>
   </son>
   <daughter>
     <name>Jeri</name>
     <husband>
       <name>Mark</name>
     </husband>
     <son>
       <name>Greg</name>
     </son>
     <son>
       <name>Tim</name>
     </son>
     <son>
       <name>Mark</name>
     </son>
     <son>
       <name>Josh</name>
         <wife>
           <name>Kristine</name>
         </wife>
         <son>
           <name>Blake</name>
         </son>
         <daughter>
           <name>Liah</name>
         </daughter>
     </son>
   </daughter>
</Patriarch>');

RecurseXML($xml);

function RecurseXML($xml,$parent="")
{
   $child_count = 0;
   foreach($xml as $key=>$value)
   {
      $child_count++;
      if(RecurseXML($value,$parent.".".$key) == 0)  // no childern, aka "leaf node"
      {
         print($parent . "." . (string)$key . " = " . (string)$value . "<BR>\n");
      }
   }
   return $child_count;
}

?>

The output....

.name = Bill
.wife.name = Vi
.son.name = Bill
.daughter.name = Jeri
.daughter.husband.name = Mark
.daughter.son.name = Greg
.daughter.son.name = Tim
.daughter.son.name = Mark
.daughter.son.name = Josh
.daughter.son.wife.name = Kristine
.daughter.son.son.name = Blake
.daughter.son.daughter.name = Liah
taylorbarstow at that google mail thingy 21-Apr-2006 04:38
Sometimes you actually want an array, not a pseudo array.   This is especially true when you aren't dealing with attributes (i.e., you just want the array of child nodes).

Do like this:

<?php
$children = $sxml->xpath('child::node()');
?>

The reason you might want this is to be able to use array functions like array_shift, array_pop, etc.  This is especially true when you are writing recursive functions.  Simplexml works really well in iterative programming, but if you try to implement recursion it gets ugly.
Sebastian 27-Oct-2005 12:45
Just a quick addition:

If you need to access a child node which contains a dash, you need to encapsulate it with {""}.

For example:
<?php
foreach ($domain->domain-listing as $product) {
}
?>

The example above doesn't work because of the dash. But instead you need to use:
<?php
foreach ($domain->{"domain-listing"} as $product) {
}
?>

At least for me the second example works perfectly fine.
no-one 01-Jun-2005 03:05
For anyone who hasn't read Sterling Hughe's article (http://www.zend.com/php5/articles/php5-simplexml.php):

<?php
$xml_document =<<<EOT
<?xml version="1.0"?>
<root xmlns:foo="http://example.com">
  <foo:bar>baz</foo:bar>
</root>
EOT;

$xml_document = simplexml_load_xml($xml_document);

$foo_ns_bar = $xml_document->children('http://example.com');

echo $foo_ns_bar->bar[0]; // prints 'baz'
?>
Andrew Rose (rose dot andrew at gmail dot com) 16-Mar-2005 01:48
The example below shows the basic use of depth-first recursion to span the xml tree.

This is coded for the command line, and it prints out the original sentance above and then the copy cat sentence it creates itself for comparison, which as you will see; this example is slightly off from, I'll leave it upto you to resolve this issue.

All in all I personaly think xml and recursion go hand in hand, so if you don't understand recursion but know xml and want to use php to manipulate xml you will need to learn about recursion at some point.

<?php
$xml = simplexml_load_string(
'<person>
 <child role="son">
  <child role="daughter"/>
 </child>
 <child role="daughter">
  <child role="son">
   <child role="son"/>
  </child>
 </child>
</person>');

function recurse($child)
 {
   foreach($child->children() as $children) {
     echo ' who begot a '.$children['role'];
     recurse($children);
   }
    return;
 }

foreach($xml->children() as $children) {
 echo 'The person begot a '.$children['role'];
  recurse($children, 0);
 echo '; ';
}

echo "\n";
echo 'The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son'."\n";

?>
zyxwvu at users dot sourceforge dot net 15-Feb-2004 07:32
File:

<category>
  <item>text</item>
  <bold>text</bold>
  <item>text</item>
  <item>text</item>
  <mark>text</mark>
  <bold>text</bold>
</category>

If you want to get also names of the tags, you can use this loop layout:

<?php
foreach($category -> children() as $name => $node){
  echo $name.'<br/>';
}
?>


Creates a new SimpleXMLElement object

  • ≪ SimpleXMLElement::children
  • SimpleXMLElement::count ≫
  • PHP Manual
  • SimpleXMLElement
  • Creates a new SimpleXMLElement object

SimpleXMLElement::__construct

(PHP 5 >= 5.0.1, PHP 7)

SimpleXMLElement::__construct — Creates a new SimpleXMLElement object

설명

final public SimpleXMLElement::__construct ( string $data [, int $options = 0 [, bool $data_is_url = false [, string $ns = "" [, bool $is_prefix = false ]]]] )

Creates a new SimpleXMLElement object.

인수

data

A well-formed XML string or the path or URL to an XML document if data_is_url is TRUE.

options

Optionally used to specify additional Libxml parameters.

Note:

It may be necessary to pass LIBXML_PARSEHUGE to be able to process deeply nested XML or very large text nodes.

data_is_url

By default, data_is_url is FALSE. Use TRUE to specify that data is a path or URL to an XML document instead of string data.

ns

Namespace prefix or URI.

is_prefix

TRUE if ns is a prefix, FALSE if it's a URI; defaults to FALSE.

반환값

Returns a SimpleXMLElement object representing data.

오류/예외

Produces an E_WARNING error message for each error found in the XML data and additionally throws an Exception if the XML data could not be parsed.

Tip

Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.

예제

Note:

Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide.

Example #1 Create a SimpleXMLElement object

<?php

include 'example.php';

$sxe = new SimpleXMLElement($xmlstr);
echo $sxe->movie[0]->title;

?>

위 예제의 출력:

PHP: Behind the Parser

Example #2 Create a SimpleXMLElement object from a URL

<?php

$sxe = new SimpleXMLElement('http://example.org/document.xml', NULL, TRUE);
echo $sxe->asXML();

?>

변경점

버전 | 설명
5.2.0 | Added the ns and is_prefix parameters.
5.1.2 | Added the options and data_is_url parameters.

참고

  • simplexml_load_string() - Interprets a string of XML into an object
  • simplexml_load_file() - Interprets an XML file into an object
  • Dealing with XML errors
  • libxml_use_internal_errors() - Disable libxml errors and allow user to fetch error information as needed

User Contributed Notes

ahmad dot mayahi at gmail dot com 08-Jun-2016 06:36
You won't be able to load an XML file without root element:

//This will throw an exception
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>', null, false);

//Here is the solution
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>', null, false);
Phil Cross 01-Sep-2014 08:39
It's worth noting, that you need to typecast dynamic property names to string in order to retrieve there value, for example:

<?php
    $xml = new SimpleXMLElement('records.xml', 0, true);

    // This will work as expected because its a hard coded property value
    foreach($xml as $record){
        echo $record->id;    // Will output the ID fine
    }

    // Dynamic properties require typecasting to string
    $xml_field = 'id';
    foreach($xml as $record){

        // This will dump a SimpleXMLElement object
        var_dump($record->$xml_field);

        // This will output the value as expected
        echo (string)$record->$xml_field;
    }
?>
bertolini dot cedric at me dot com 16-Jun-2014 08:07
It's worth noting that the behavior of SimpleXmlElement::__construct is not exactly the same as simplexml_load_string, regarding libxml_use_internal_errors().

In my case,

<?php
libxml_use_internal_errors(true);
new \SimpleXmlElement($data);
?>

was still throwing error. But as soon as I switched to

<?php
libxml_use_internal_errors(true);
simplexml_load_string($data);
?>

everything worked fine and I stopped getting an error.
kumarldh at gmail dot com 23-Mar-2011 07:50
Using libxml_use_internal_errors() may suppress errors but Exception still requires decent handling. I used following code snippet.

<?php
libxml_use_internal_errors(true);
try{
    $xmlToObject = new SimpleXMLElement($notSoWellFormedXML);
} catch (Exception $e){
    echo 'Please try again later...';
    exit();
}
?>
uramihsayibok, gmail, com 17-Aug-2009 03:52
As I was filling out a bug report, I realized why (speculation here) the constructor is final: so that functions like simplexml_load_file and simplexml_load_string can work. I imagine the PHP-ized code looks something like

<?php

function simplexml_load_file($filename, $class_name = "SimpleXMLElement", $options = 0, $ns = "", $is_prefix = false) {
    return new $class_name($filename, $options, true, $ns, $is_prefix);
}

?>

If we were to use a different $class_name and change the constructor's definition these functions wouldn't work.

There's no easy, sensible solution that keeps simplexml_load_file and simplexml_load_string.
tudor at culise dot net 20-Nov-2007 12:35
This class is extendable, but it's too bad that its constructor cannot be overriden (PHP says it's a final method). Thus the class should be wrapped using the delegation principle rather that extended.


Counts the children of an element

  • ≪ SimpleXMLElement::__construct
  • SimpleXMLElement::getDocNamespaces ≫
  • PHP Manual
  • SimpleXMLElement
  • Counts the children of an element

SimpleXMLElement::count

(PHP 5 >= 5.3.0, PHP 7)

SimpleXMLElement::count — Counts the children of an element

설명

public int SimpleXMLElement::count ( void )

This method counts the number of children of an element.

반환값

Returns the number of elements of an element.

예제

Example #1 Counting the number of children

<?php
$xml = <<<EOF
<people>
 <person name="Person 1">
  <child/>
  <child/>
  <child/>
 </person>
 <person name="Person 2">
  <child/>
  <child/>
  <child/>
  <child/>
  <child/>
 </person>
</people>
EOF;

$elem = new SimpleXMLElement($xml);

foreach ($elem as $person) {
    printf("%s has got %d children.\n", $person['name'], $person->count());
}
?>

위 예제의 출력:

Person 1 has got 3 children.
Person 2 has got 5 children.

참고

  • SimpleXMLElement::children() - Finds children of given node

User Contributed Notes

daniel dot erni at focusedpublishing dot com 22-Mar-2011 05:37
Alternative code for PHP < 5.3:
<?php
$count = $node->count(); // PHP > 5.3
$count = count($node->children()); // PHP < 5.3
?>


Returns namespaces declared in document

  • ≪ SimpleXMLElement::count
  • SimpleXMLElement::getName ≫
  • PHP Manual
  • SimpleXMLElement
  • Returns namespaces declared in document

SimpleXMLElement::getDocNamespaces

(PHP 5 >= 5.1.2, PHP 7)

SimpleXMLElement::getDocNamespaces — Returns namespaces declared in document

설명

public array SimpleXMLElement::getDocNamespaces ([ bool $recursive = false [, bool $from_root = true ]] )

Returns namespaces declared in document

인수

recursive

If specified, returns all namespaces declared in parent and child nodes. Otherwise, returns only namespaces declared in root node.

from_root

Allows you to recursively check namespaces under a child node instead of from the root of the XML doc.

반환값

The getDocNamespaces method returns an array of namespace names with their associated URIs.

예제

Example #1 Get document namespaces

<?php

$xml = <<<XML
<?xml version="1.0" standalone="yes"?>
<people xmlns:p="http://example.org/ns">
    <p:person id="1">John Doe</p:person>
    <p:person id="2">Susie Q. Public</p:person>
</people>
XML;

$sxe = new SimpleXMLElement($xml);

$namespaces = $sxe->getDocNamespaces();
var_dump($namespaces);

?>

위 예제의 출력:

array(1) {
   ["p"]=>
   string(21) "http://example.org/ns"
}

Example #2 Working with multiple namespaces

<?php

$xml = <<<XML
<?xml version="1.0" standalone="yes"?>
<people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
    <p:person t:id="1">John Doe</p:person>
    <p:person t:id="2" a:addr="123 Street" xmlns:a="http://example.org/addr">
        Susie Q. Public
    </p:person>
</people>
XML;

$sxe = new SimpleXMLElement($xml);

$namespaces = $sxe->getDocNamespaces(TRUE);
var_dump($namespaces);

?>

위 예제의 출력:

array(3) {
  ["p"]=>
  string(21) "http://example.org/ns"
  ["t"]=>
  string(23) "http://example.org/test"
  ["a"]=>
  string(23) "http://example.org/addr"
}

변경점

버전 | 설명
5.4.0 | Added from_root parameter.

참고

  • SimpleXMLElement::getNamespaces() - Returns namespaces used in document
  • SimpleXMLElement::registerXPathNamespace() - Creates a prefix/ns context for the next XPath query

User Contributed Notes

There are no user contributed notes for this page.


Gets the name of the XML element

  • ≪ SimpleXMLElement::getDocNamespaces
  • SimpleXMLElement::getNamespaces ≫
  • PHP Manual
  • SimpleXMLElement
  • Gets the name of the XML element

SimpleXMLElement::getName

(PHP 5 >= 5.1.3, PHP 7)

SimpleXMLElement::getName — Gets the name of the XML element

설명

public string SimpleXMLElement::getName ( void )

Gets the name of the XML element.

반환값

The getName method returns as a string the name of the XML tag referenced by the SimpleXMLElement object.

예제

Note:

Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide.

Example #1 Get XML element names

<?php
include 'example.php';
$sxe = new SimpleXMLElement($xmlstr);

echo $sxe->getName() . "\n";

foreach ($sxe->children() as $child)
{
    echo $child->getName() . "\n";
}

?>

위 예제의 출력:

movies
movie

User Contributed Notes

There are no user contributed notes for this page.


Returns namespaces used in document

  • ≪ SimpleXMLElement::getName
  • SimpleXMLElement::registerXPathNamespace ≫
  • PHP Manual
  • SimpleXMLElement
  • Returns namespaces used in document

SimpleXMLElement::getNamespaces

(PHP 5 >= 5.1.2, PHP 7)

SimpleXMLElement::getNamespaces — Returns namespaces used in document

설명

public array SimpleXMLElement::getNamespaces ([ bool $recursive = false ] )

Returns namespaces used in document

인수

recursive

If specified, returns all namespaces used in parent and child nodes. Otherwise, returns only namespaces used in root node.

반환값

The getNamespaces method returns an array of namespace names with their associated URIs.

예제

Example #1 Get document namespaces in use

<?php

$xml = <<<XML
<?xml version="1.0" standalone="yes"?>
<people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
    <p:person id="1">John Doe</p:person>
    <p:person id="2">Susie Q. Public</p:person>
</people>
XML;

$sxe = new SimpleXMLElement($xml);

$namespaces = $sxe->getNamespaces(true);
var_dump($namespaces);

?>

위 예제의 출력:

array(1) {
  ["p"]=>
  string(21) "http://example.org/ns"
}

참고

  • SimpleXMLElement::getDocNamespaces() - Returns namespaces declared in document
  • SimpleXMLElement::registerXPathNamespace() - Creates a prefix/ns context for the next XPath query

User Contributed Notes

thiago at osfeio dot com 30-Oct-2013 01:23
this article has more info : http://www.sitepoint.com/parsing-xml-with-simplexml/
harry at nospam dot thestorm dot plus dot com 17-Feb-2012 11:21
If the namespace is nested in the xml, then you will have to loop over the nodes.

<?php

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
  <people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
      <items>
            <title>This is a test of namespaces and my patience</title>
            <p:person id="1">John Doe</p:person>
            <p:person id="2">Susie Q. Public</p:person>
            <p:person id="1">Fish Man</p:person>
      </items>
  </people>
XML;

$sxe = new SimpleXMLElement($xml);

foreach ($sxe as $out_ns)
{
    $ns = $out_ns->getNamespaces(true);

    $child = $out_ns->children($ns['p']);

    foreach ($child as $out)
    {
        echo $out . "<br />";
    }
}
?>
harry at nospam dot thestorm dot plus dot com 16-Feb-2012 09:50
To read a namespace node you have to use the children method.

<?php

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
    <p:person id="1">John Doe</p:person>
    <p:person id="2">Susie Q. Public</p:person>
</people>
XML;

$sxe = new SimpleXMLElement($xml);

$ns = $sxe->getNamespaces(true);

$child = $sxe->children($ns['p']);

foreach ($child->person as $out_ns)
{
    echo $out_ns;
}

?>


Creates a prefix/ns context for the next XPath query

  • ≪ SimpleXMLElement::getNamespaces
  • SimpleXMLElement::saveXML ≫
  • PHP Manual
  • SimpleXMLElement
  • Creates a prefix/ns context for the next XPath query

SimpleXMLElement::registerXPathNamespace

(PHP 5 >= 5.2.0, PHP 7)

SimpleXMLElement::registerXPathNamespace — Creates a prefix/ns context for the next XPath query

설명

public bool SimpleXMLElement::registerXPathNamespace ( string $prefix , string $ns )

Creates a prefix/ns context for the next XPath query. In particular, this is helpful if the provider of the given XML document alters the namespace prefixes. registerXPathNamespace will create a prefix for the associated namespace, allowing one to access nodes in that namespace without the need to change code to allow for the new prefixes dictated by the provider.

인수

prefix

The namespace prefix to use in the XPath query for the namespace given in ns.

ns

The namespace to use for the XPath query. This must match a namespace in use by the XML document or the XPath query using prefix will not return any results.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 Setting a namespace prefix to use in an XPath query

<?php

$xml = <<<EOD
<book xmlns:chap="http://example.org/chapter-title">
    <title>My Book</title>
    <chapter id="1">
        <chap:title>Chapter 1</chap:title>
        <para>Donec velit. Nullam eget tellus vitae tortor gravida scelerisque.
            In orci lorem, cursus imperdiet, ultricies non, hendrerit et, orci.
            Nulla facilisi. Nullam velit nisl, laoreet id, condimentum ut,
            ultricies id, mauris.</para>
    </chapter>
    <chapter id="2">
        <chap:title>Chapter 2</chap:title>
        <para>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin
            gravida. Phasellus tincidunt massa vel urna. Proin adipiscing quam
            vitae odio. Sed dictum. Ut tincidunt lorem ac lorem. Duis eros
            tellus, pharetra id, faucibus eu, dapibus dictum, odio.</para>
    </chapter>
</book>
EOD;

$sxe = new SimpleXMLElement($xml);

$sxe->registerXPathNamespace('c', 'http://example.org/chapter-title');
$result = $sxe->xpath('//c:title');

foreach ($result as $title) {
  echo $title . "\n";
}

?>

위 예제의 출력:

Chapter 1
Chapter 2

Notice how the XML document shown in the example sets a namespace with a prefix of chap. Imagine that this document (or another one like it) may have used a prefix of c in the past for the same namespace. Since it has changed, the XPath query will no longer return the proper results and the query will require modification. Using registerXPathNamespace avoids future modification of the query even if the provider changes the namespace prefix.

참고

  • SimpleXMLElement::xpath() - Runs XPath query on XML data
  • SimpleXMLElement::getDocNamespaces() - Returns namespaces declared in document
  • SimpleXMLElement::getNamespaces() - Returns namespaces used in document

User Contributed Notes

Lea Hayes 12-Jan-2011 06:56
Looks like you have to use registerXPathNamespace for each node when using XPath:

<?php
$xml = simplexml_load_file($filename);

$xml->registerXPathNamespace('test', 'http://example.com');

$shopping_element = $xml->xpath('test:shopping-list');

// Breaks with out the following line:

$shopping_element->registerXPathNamespace('test', 'http://example.com');

$fruit = $shopping_element->xpath('test:fruit');
?>


별칭: SimpleXMLElement::asXML

  • ≪ SimpleXMLElement::registerXPathNamespace
  • SimpleXMLElement::__toString ≫
  • PHP Manual
  • SimpleXMLElement
  • 별칭: SimpleXMLElement::asXML

SimpleXMLElement::saveXML

(PHP 5 >= 5.2.0, PHP 7)

SimpleXMLElement::saveXML — 별칭: SimpleXMLElement::asXML()

설명

다음 메소드의 별칭입니다: SimpleXMLElement::asXML()

User Contributed Notes

There are no user contributed notes for this page.


Returns the string content

  • ≪ SimpleXMLElement::saveXML
  • SimpleXMLElement::xpath ≫
  • PHP Manual
  • SimpleXMLElement
  • Returns the string content

SimpleXMLElement::__toString

(No version information available, might only be in Git)

SimpleXMLElement::__toString — Returns the string content

설명

public string SimpleXMLElement::__toString ( void )

Returns text content that is directly in this element. Does not return text content that is inside this element's children.

인수

이 함수는 인수가 없습니다.

반환값

Returns the string content on success or an empty string on failure.

예제

Example #1 Get string content

<?php
$xml = new SimpleXMLElement('<a>1 <b>2 </b>3</a>');
echo $xml;
?>

위 예제의 출력:

1 3

참고

  • SimpleXMLElement::asXML() - Return a well-formed XML string based on SimpleXML element

User Contributed Notes

Patanjali 27-Mar-2016 07:55
[Someone remove that other Patanjali's note, because it has errors! :-(]

For those for whom it may not be immediately obvious from the example, the echo is what is forcing __toString() to be used.

However, to assign the text of a node (but not its children) to a variable:

$XML = new SimpleXMLElement('<p>Hello<span> world</span>.<span> Good day!</span></p>');

$Text = $XML->__toString();

is effectively:
$Text = 'Hello.'; // The <span>s are ignored.

Either of:
$Text = $XML->span->__toString();
$Text = $XML->span[0]->__toString();

is effectively:
$Text = ' world'; // Only the first <span> is used.

$Text = $XML->span[1]->__toString();

is effectively:
$Text = ' Good day!'; // Only the second <span> is used.
Patanjali 27-Mar-2016 07:36
For those for whom it may not be immediately obvious from the example, to assign the text of a node (but not its children) to a variable:

$XML = new SimpleXMLElement('<p>Hello<span> world</span>.<span> Good day!</span></p>');

Either:
$Text = $XML;
$Text = $XML->__String();

is effectively:
$Text = 'Hello.'; // The <span>s are ignored.

Either of:
$Text = $XML->span;
$Text = $XML->span[0];

is effectively:
$Text = ' world'; // Only the first <span> is used.

$Text = $XML->span[1];

is effectively:
$Text = ' Good day!'; // Only the second <span> is used.


Runs XPath query on XML data

  • ≪ SimpleXMLElement::__toString
  • SimpleXML 함수 목록 ≫
  • PHP Manual
  • SimpleXMLElement
  • Runs XPath query on XML data

SimpleXMLElement::xpath

(PHP 5 >= 5.2.0, PHP 7)

SimpleXMLElement::xpath — Runs XPath query on XML data

설명

public array SimpleXMLElement::xpath ( string $path )

The xpath method searches the SimpleXML node for children matching the XPath path.

인수

path

An XPath path

반환값

Returns an array of SimpleXMLElement objects or FALSE in case of an error.

예제

Example #1 Xpath

<?php
$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml = new SimpleXMLElement($string);

/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');

while(list( , $node) = each($result)) {
    echo '/a/b/c: ',$node,"\n";
}

/* Relative paths also work... */
$result = $xml->xpath('b/c');

while(list( , $node) = each($result)) {
    echo 'b/c: ',$node,"\n";
}
?>

위 예제의 출력:

/a/b/c: text
/a/b/c: stuff
b/c: text
b/c: stuff

Notice that the two results are equal.

참고

  • SimpleXMLElement::registerXPathNamespace() - Creates a prefix/ns context for the next XPath query
  • SimpleXMLElement::getDocNamespaces() - Returns namespaces declared in document
  • SimpleXMLElement::getNamespaces() - Returns namespaces used in document

User Contributed Notes

Alexis Peters 11-Oct-2016 06:11
If you want this behavior for an multidimensional array you can use this:

<?php
function apath(&$a, $path, $value = NULL) {
    $pathes = explode('/',$path);

    if(count($pathes) > 1) {
        $firstPart = array_shift($pathes);
        if(!isset($a[$firstPart])) {
            $a[$firstPart] = array();
        }
        return apath($a[$firstPart], implode('/', $pathes), $value);
    } else {
        if($value){
            $a[$path] = $value;
        } else {
            return $a[$path];
        }
    }
}

apath($a, 'user/name', array('prename' => 'foo', 'lastname' => 'bar'));

$a = array();
var_dump($a);
var_dump(apath($a, 'user/name/lastname'));

?>
Gives you this:

array(1) {
  ["user"]=>
  array(1) {
    ["name"]=>
    array(2) {
      ["prename"]=>
      string(5) "foo"
      ["lastname"]=>
      string(8) "bar"
    }
  }
}
string(8) "bar"
Anonymous 30-Jan-2015 02:30
As mentioned already xpath will fail is the default namespace 'xmlns' is used like in:

<?php

$xmlstring = $string = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <fileVersion appName="xl" />
</workbook>
XML;
?>

xpath cannot search through the xml without explicitly specifying a namespace.
There are 2 options :
1. rename the 'xmlns' into something else to trick xpath into believing that no default namespace is defined.
2. register a string as the default namespace and use that string in all your queries. Unfortunatly, an empty space will not work.
No other option currently exist until XPath2.0 becomes the default library.
leonjanzen at gmail dot com 20-Oct-2014 04:48
To run an xpath query on an XML document that has a namespace, the namespace must be registered with SimpleXMLElement::registerXPathNamespace() before running the query. If the XML document namespace does not include a prefix, you must make up an arbitrary one, and then use it in your query.

<?php
$strXml= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<mydoc xmlns="http://www.url.com/myns">
    <message>Test message</message>
</mydoc>
XML;

$xmlDoc=new \SimpleXMLElement($strXml);

foreach($xmlDoc->getDocNamespaces() as $strPrefix => $strNamespace) {
    if(strlen($strPrefix)==0) {
        $strPrefix="a"; //Assign an arbitrary namespace prefix.
    }
    $xmlDoc->registerXPathNamespace($strPrefix,$strNamespace);
}

print($xmlDoc->xpath("//a:message")[0]); //Use the arbitrary namespace prefix in the query.
?>

This will output:

Test message
BenjaminBeck at gmx dot de 13-Sep-2014 10:36
If you want to search multiple values from xml on the behalf or one value then this code can be helpfull to you.

if there is:
<Record>
  <country>Pakistan</country>
  <code>+92</code>
  <Value>100<Value>
</Record>

then try this one:
<?php
$sxe =  simplexml_load_file("countries.XML");
foreach($sxe->xpath('//RECORD') as $item) {

    $v = $row->xpath('./country[. ="Pakistan"]');
    if($v[0]){
        print $item->country;
        print $item->code;
        print $item->value;
    }

}
?>
yetihehe at yetihehe dot com 12-Jan-2011 11:21
Xpath actually knows which element is root regardless of element on which you execute it. Example:

<?php

$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <b>
  <c>code</c>
 </b>
</a>
XML;

header('content-type: text/plain');

$xml = new SimpleXMLElement($string);

//relative to root
$b0=$xml->b[0]->xpath('//c');
while(list( , $node) = each($b0)) {
    echo 'b[0]: //c: ',$node,"\n";
}

$b1=$xml->b[1]->xpath('//c');
while(list( , $node) = each($b1)) {
    echo 'b[1]: //c: ',$node,"\n";
}

echo "\n";

//relative to current element
$b0=$xml->b[0]->xpath('.//c');
while(list( , $node) = each($b0)) {
    echo 'b[0]: .//c: ',$node,"\n";
}

$b1=$xml->b[1]->xpath('.//c');
while(list( , $node) = each($b1)) {
    echo 'b[1]: .//c: ',$node,"\n";
}

?>

Will return:
b[0]: //c: text
b[0]: //c: stuff
b[0]: //c: code
b[1]: //c: text
b[1]: //c: stuff
b[1]: //c: code

b[0]: .//c: text
b[0]: .//c: stuff
b[1]: .//c: code
stefan at efectos dot nl 09-Nov-2010 11:33
Having problems selecting information through xpath when there are namespaces in the XML?

Try this quick and dirty solution, but it works:

<?php
$objImageXml = new SimpleXMLElement($objImageXml->asXML());
?>
grummfy at gmail dot com 11-Feb-2010 01:08
On a xml that have namespace you need to do this before your xpath request (or empty array will be return) :

<?php
$string = str_replace('xmlns=', 'ns=', $string); //$string is a string that contains xml...
?>
DrupalFocus at earthlink dot net 02-Dec-2009 09:27
Looks like the relative path is relative to the SimpleXMLElement object that you call the Xpath() method on.  Basically chopping off the root element.

<?php
$string = <<<XML
<foo>
  <bar>
    <a>
     <b>
      <c>text</c>
      <c>stuff</c>
     </b>
     <d>
      <c>code</c>
     </d>
    </a>
  </bar>
</foo>
XML;

$xml = new SimpleXMLElement($string);
/* Search for children using 2nds level relative <bar><a><b><c> */
$result = $xml->xpath('a/b/c');
if ($result)
foreach($result as $node) {
    drupal_set_message(t('a/b/c: @node.', array('@node' => $node)));
}
else
  drupal_set_message(t('a/b/c: failed.'));

/* it relative to the SimpleXMLElement Search for 2nds level relative <bar><a><b><c> but where is foo */
$bar = $xml->bar;
$result = $bar->xpath('a/b/c');
foreach($result as $node) {
    drupal_set_message(t('bar->a/b/c: @node.', array('@node' => $node)));
}
?>

results in :
#
# a/b/c: failed.
# bar->a/b/c: text.
# bar->a/b/c: stuff.
behnam@zwnj 24-Sep-2009 10:34
Seems like this function cannot return the result of XPath string() [1] function.  For example if you query xpath("string(a/@b)") you will get bool(false) although the node "a" may have the attribute "b" set to a non-empty string.

[1] http://www.w3.org/TR/xpath#section-String-Functions
Anti Veeranna 17-Jun-2009 12:50
Whether it returns a false or empty array for a non-existing path seems to depend on libxml version.

I tried the following code:

<?php
$data = '<foo><bar></bar></foo>';
$xml = simplexml_load_string($data);
print gettype($xml->xpath('/first/second'));
?>

On a machine with libxml 2.6.16 and PHP 5.2.8 I got back boolean. On a different machine with libxml 2.7.3 and PHP 5.2.8/5.2.9 I got back empty array.

You can check the libxml version from command line by executing: php --ri libxml

or simply look at phpinfo() output
arealzone-czernobyl at yahoo dot de 12-Jun-2009 12:23
The simplest way to count nodes in a XML doc:

<?php
$xml = new SimpleXMLElement($xmlstr);

$xmlNode = $xml->xpath('root/yourNodeName');
$nodeCount = count($xmlNode);

echo $nodeCount;
?>
anemik 03-Jul-2008 12:46
If you want to find easly all records satisfying some condition in XML data like

....
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
   </book>
...

try example below

<?php

$xmlStr = file_get_contents('data/books.xml');
$xml = new SimpleXMLElement($xmlStr);
// seach records by tag value:
// find all book records with price higher than 40$
$res = $xml->xpath("book/price[.>'40']/parent::*");
print_r($res);

?>

You will see response like:
Array (
[0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => bk101
                )

            [author] => Gambardella, Matthew
            [title] => XML Developer's Guide
            [genre] => Computer
            [price] => 44.95
            [publish_date] => 2000-10-01
            [description] => An in-depth look at creating applications
      with XML.
        )
...
paul at pmtlogic dot com 18-Jun-2008 09:17
xpath doesn't seem to be able to handle single quotes embedded in the query itself. For instance, I want to find geo coordinates in an xml file based on country name.

xml snippet:

<zones>
<zone country="Cote d'Ivoire" fullName="Yamoussoukro" geo="6.82,-5.28" id="1050"><url prefix="1001" value="fiji.html" /><url prefix="1002" value="C" /></zone>
</zones>

The following code does not work:

<?php
$xml = simplexml_load_file("my.xml");
$result = $xml->xpath("//zone[@country='Cote d\'Ivoire']");

foreach ( $result[0]->attributes() as $key => $val ) {
    print "<div class='coords'>$key: $val</div>\n";
}
?>

I have tried many variations on the embedded single quote (i.e. escape codes) but with no result. W3C offers no explanation either.

In addition, there doesn't seem to be any way of embedding wildcards in the attribute value (you can embed wildcards in the attribute name). Otherwise the following might be a reasonable substitute in this context:

<?php $result = $xml->xpath("//zone[@country='Cote d*Ivoire']"); ?>
canuemail at gmail dot com 05-May-2008 02:05
If you want to search multiple values from xml on the behalf or one value then this code can be helpfull to you.

if there is:
<Record>
  <country>Pakistan</country>
  <code>+92</code>
  <Value>100<Value>
</Record>

then try this one:
<?php
$sxe =  simplexml_load_file("countries.XML");
foreach($sxe->xpath('//RECORD') as $item) {

    $row = simplexml_load_string($item->asXML());
    $v = $row->xpath('//country[. ="Pakistan"]');
    if($v[0]){
        print $item->country;
        print $item->code;
        print $item->value;
    }

}
?>
Hugh 19-Dec-2005 12:23
Note that this function does not ALWAYS return an array. It seems that if the specified path is not found in the document, it will return false. You need to check for the false value before using foreach, if you wish to avoid a warning (and often to handle errors or control program flow in this situation).
drewish at katherinehouse dot com 11-Jul-2005 03:16
xpath() can also be used to select elements by their attributes. For a good XPath reference check out: http://www.w3schools.com/xpath/xpath_syntax.asp

<?php
$string = <<<XML
<sizes>
    <size label="Square" width="75" height="75" />
    <size label="Thumbnail" width="100" height="62" />
    <size label="Small" width="112" height="69" />
    <size label="Large" width="112" height="69" />
</sizes>
XML;

$xml = simplexml_load_string($string);
$result = $xml->xpath("//size[@label='Large']");

// print the first (and only) member of the array
echo $result[0]->asXml();
?>

The script would print:
<size label="Large" width="112" height="69"/>


SimpleXML 함수 목록

Table of Contents

  • simplexml_import_dom — Get a SimpleXMLElement object from a DOM node.
  • simplexml_load_file — Interprets an XML file into an object
  • simplexml_load_string — Interprets a string of XML into an object

User Contributed Notes

brandonkirsch at gmail dot com 10-Sep-2013 04:41
Contrary to other notes posted here, SimpleXML *DOES NOT* properly handle non-UTF8 encoded XML documents.

A proper "iso-8859-1" declared and encoded XML document will not parse with SimpleXML for PHP 5.2.17 or 5.5.1 when using extended characters (that is, byte values greater than 127)

DOMDocument, on the other hand, will correctly parse incoming iso-8859-1 XML documents and can also be used to perform character translation to UTF8:

$dom = new DOMDocument();

if($dom->loadXML($fileContents)){ // $fileContents is an XML document with iso-8859-1 encoding specified in the declaration
            $dom->encoding = 'utf-8'; // convert document encoding to UTF8
            return $dom->saveXML(); // return valid, utf8-encoded XML
William Fahria 16-Jul-2013 04:34
A simple implementation:
You can inform a direct XML in text format to the function.

function eoXML2Array($xmlContent, $out = array()){
    $xmlObject = is_object($xmlContent) ? $xmlContent : simplexml_load_string($xmlContent);
    foreach((array) $xmlObject as $index => $node)
        $out[$index] = ( is_object($node) || is_array($node) ) ? eoXML2Array( $node ) : $node;
    return $out;
}

Usage: $your_array = eoXML2Array(your xml text);
r dot m dot 0100101 at gmail dot com 26-Apr-2013 12:00
This is a slight adjustment to http://www.php.net/manual/en/ref.simplexml.php#103617 's function.
The function given by them would convert a simpleXML to an array but only at the highest level. Once you add the is_array check with the is_object it should convert each level to an array (well it did for me through 3 levels).

function simpleXmlObjectToArray ( $xmlObject, $out = array () )
{
    foreach ( (array) $xmlObject as $index => $node )
        $out[$index] = ( is_object ( $node ) || is_array($node) )
        ? simpleXmlObjectToArray ( $node )
        : $node;

    return $out;
}
Happy Coding!
Eric 29-Jan-2013 12:00
If the SimpleXMLObject contains an array, adding is_array() allows the function to keep digging for Objects.

<?php
function xml2array ( $xmlObject, $out = array () )
{
        foreach ( (array) $xmlObject as $index => $node )
            $out[$index] = ( is_object ( $node ) ||  is_array ( $node ) ) ? xml2array ( $node ) : $node;

        return $out;
}
?>
thomas at guenneguez dot com 14-Dec-2011 11:36
Merge two xml only with SimpleXML :
Function corrected if note containe only value.

<?php
function mergeXML(&$base, $add)
{
    if ( $add->count() != 0 )
        $new = $base->addChild($add->getName());
    else
        $new = $base->addChild($add->getName(), $add);
    foreach ($add->attributes() as $a => $b)
    {
        $new->addAttribute($a, $b);
    }
    if ( $add->count() != 0 )
    {
        foreach ($add->children() as $child)
        {
            mergeXML($new, $child);
        }
    }
}
?>

Use : <?php mergeXML($xmlElement, simplexml_load_string($toMerge)) ?>
julien dot breiner at laposte dot net 09-Dec-2011 07:31
Merge two xml only with SimpleXML :

<?php
private function mergeXML(&$base, $add)
{
    $new = $base->addChild($add->getName());
    foreach ($add->attributes() as $a => $b) {
        $new[$a] = $b;
    }
    foreach ($add->children() as $child) {
        $this->mergeXML($new, $child);
    }
}?>

Use : <?php mergeXML($xmlElement, simplexml_load_string($toMerge)) ?>
k dot antczak at livedata dot pl 22-Apr-2011 10:08
A quick snippet which converts XML Object into array:

<?php
function xml2array ( $xmlObject, $out = array () )
{
        foreach ( (array) $xmlObject as $index => $node )
            $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;

        return $out;
}
?>

Have fun! ;-)
help :-) @ theapotek :-) com 08-Apr-2011 09:54
If you are having trouble accessing CDATA in your simplexml document, you don't need to str_replace/preg_replace the CDATA out before loading it with simplexml.

You can do this instead, and all your CDATA contents will be merged into the element contents as strings.

$xml = simplexml_load_file($xmlfile,
'SimpleXMLElement', LIBXML_NOCDATA);
Andrez zenarter at hotmail dot com 17-Jul-2009 07:56
I just wanna add an additional to
"Append/combine/merge one simplexml to another"
The parent should has attributes too.

<?php

function append_simplexml(&$simplexml_to, &$simplexml_from)
{

static $firstLoop=true;

//Here adding attributes to parent
if( $firstLoop )
            {
                foreach( $simplexml_from->attributes() as $attr_key => $attr_value )
                {
                    $simplexml_to->addAttribute($attr_key, $attr_value);
                }
            }

foreach ($simplexml_from->children() as $simplexml_child)
    {
        $simplexml_temp = $simplexml_to->addChild($simplexml_child->getName(), (string) $simplexml_child);
        foreach ($simplexml_child->attributes() as $attr_key => $attr_value)
        {
            $simplexml_temp->addAttribute($attr_key, $attr_value);
        }

       $firstLoop=false;

        append_simplexml($simplexml_temp, $simplexml_child);
    }

unset( $firstLoop=false );
}

?>

I have aproblem to assign an object into a variable used as an object later, Then i found the code above, it solve half of my problem. I have it should have the parent name assign too but it didnt. Any better code?
Douglas Marsh 06-Jul-2009 05:53
SimpleXML is really nice for loading/converting XML data into native PHP data structures. I was considering crude searching of SVG for the width/height and since it really is an XML file... I found a SUPER easy method for parsing out the information I wanted:

<?php
// putting incomplete SVG data inline for readability
$RawXML = <<< XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:dc="http://www.example.com/dc"
   xmlns:cc="http://www.example.com/cc"
   xmlns:rdf="http://www.example.com/rdf"
   xmlns:svg="http://www.example.com/svg"
   xmlns="http://www.example.com/"
   xmlns:sodipodi="http://www.example.com/sodipodi"
   xmlns:inkscape="http://www.example.com/inkscape"
   id="svg2"
   sodipodi:version="0.32"
   inkscape:version="0.46"
   width="586.25"
   height="743.75"
   xml:space="preserve"
   sodipodi:docname="some_svg_file.svg"
   inkscape:output_extension="org.inkscape.output.svg.inkscape">
 <!-- portions removed to keep this short -->
</svg>
XML;

$svg = new SimpleXMLElement($RawXML);

$height = $svg['height'];
$width = $svg['width'];

echo "The width x height is: ${width} x ${height} \n";

?>
info at ajaxforum dot ru 17-Jun-2009 09:32
Append/combine/merge one simplexml to another:

<?php
include 'example.php';
$el1 = new SimpleXMLElement($xmlstr);
$el2 = new SimpleXMLElement($xmlstr);

// wrong way!
// php convert $el2->movie[0] to string silently
$el1->addChild('movie', $el2->movie[0]);

// wrong way!
// php 5.1 convert $el2->movie[0] to string and generate Warning: It is not yet possible to assign complex types to properties. It is not possible to assign complex types to nodes
// php 5.2 convert $el2->movie[0] to string silently
$el1->addChild('movie');
$el1->movie[2] = $el2->movie[0];

// right way!
$el1_movie = $el1->addChild('movie');
append_simplexml($el1_movie, $el2->movie[0]);

echo "<pre>"; print_r($el1); echo "</pre>";

/**
 * Add one simplexml to another
 *
 * @param object $simplexml_to
 * @param object $simplexml_from
 * @author Boris Korobkov
 * @link http://www.ajaxforum.ru/
 */
function append_simplexml(&$simplexml_to, &$simplexml_from)
{
    foreach ($simplexml_from->children() as $simplexml_child)
    {
        $simplexml_temp = $simplexml_to->addChild($simplexml_child->getName(), (string) $simplexml_child);
        foreach ($simplexml_child->attributes() as $attr_key => $attr_value)
        {
            $simplexml_temp->addAttribute($attr_key, $attr_value);
        }

        append_simplexml($simplexml_temp, $simplexml_child);
    }
}
?>
codejunky32 at NOSPAM dot com 22-May-2009 05:45
Just wanted to add a post as to how you can extract the value from a SimpleXMLElement. Its not as straightforward as you think. Because its a complex object you can't just access the element directly. Here is a sample of data that represents a var_dump of a SimpleXmlElement

array(1) {
  [0]=>
  object(SimpleXMLElement)#13 (2) {
    ["@attributes"]=>
    array(1) {
      ["name"]=>
      string(5) "title"
    }
    [0]=>
    string(19) "PHP Tech Book"
  }
}

If you want to extract the title of the book you have to cast the specified element to a string like so.
$newTitle = (string) $title[0];

The $title variable is the SimpleXMLElement that you have extracted from the xml document using simplexml_load_string for instance.  To initially access the title element from the xml document you can do like so, using xpath.
$title = $doc->xpath('str[@name="title"]');

Hope this helps someone out there.
james dot ellis at gmail dot com 29-Aug-2008 03:55
While SimpleXML is a powerful tool, developers using it to process and handle large XML documents & strings should take into careful consideration its memory usage requirements.
SimpleXML requires the entire XML tree to be available prior to any processing actions on that tree -- this requires the entire tree to be in memory. Ok for a 40kb XML file but when dealing with > 100MB files you will see performance degradation, especially if you have a busy server.

An alternative to processing large XML files is the XMLReader class, which operates in streaming mode, of which an excellent tutorial is presented here:
http://www.ibm.com/developerworks/library/x-pullparsingphp.html

XML is an incredibly verbose format. If you are dealing with large data structures, especially in web services, do you you actually need to represent the data as an XML tree? - other serialisation formats such as JSON, Serialised PHP, Google Protocol Buffers (http://code.google.com/p/protobuf/) and even CSV can remarkably reduce processing time, bandwidth and load  when dealing with large files. A 110MB XML file can become a 65MB CSV file, with the same data in it, simply because the data identifiers (tags) are only represented once in the entire document.
m0sh3 at hotmail dot com 23-May-2008 12:24
Be aware of quirks like this one:

<?php

$a = new SimpleXMLElement('<a><b/></a>');

$a->b = 'test &amp; test';
$a->c = 'test &amp; test';

print_r($a);
/*
OUTPUT:

SimpleXMLElement Object
(
    [b] => test & test
    [c] => test &amp; test
)
*/
nathan at gimpstraw dot com 05-Apr-2008 06:43
I'm using SimpleXML to process data sent back from an API request, and I ran into the CDATA problem and the error you get from html entities, and here is a solution i came up with, don't know if it's the most practical, but it's working.

<?php
$str = <<< XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<menu>
    <item>
        <title><![CDATA[Caf&eacute;]]></title>
        <description><![CDATA[Some description.]]></description>
    </item>
</menu>
XML;

$str = preg_replace("/\<\!\[CDATA\[(.*?)\]\]\>/ies", "'[CDATA]'.base64_encode('$1').'[/CDATA]'", $str);
$xml = new SimpleXMLElement($str);

// Return item title's
foreach ($xml->item as $item) {
    $tmp = (string) $item->title;
    $tmp = preg_replace("/\[CDATA\](.*?)\[\/CDATA\]/ies", "base64_decode('$1')", $tmp);
    echo $tmp;
}
?>
ngkongs at gmail dot com 19-Mar-2008 07:44
fix the function i posted before, have a wrong quote placement

function implode2($glue="", $var){
    if ($var){
        foreach ($var as $value){
            $array[]=strval($value);
        }
        return implode($glue, $array);
    }
    else return false;
}
philippe_marc_--a---t--_novartis_com 12-Feb-2008 06:46
TIP: if you run into memory issue using simpleXML.
You may want to use PHP>5.2.5 in order to avoid that: http://bugs.php.net/bug.php?id=38604
If not possible, avoid "foreach ( $xmldata->node as $node )" like calls.
andrvm 03-Jan-2008 12:01
<?php
/**
    * Remove node/nodes xml with xpath
    *
    * @param SimpleXMLElement                 $xml
    * @param string XPath                     $path
    * @param string ('one'|'child'|'all')     $multi
    *
    * Use:
    *
    * Example xml file - http://ru2.php.net/manual/ru/ref.simplexml.php
    *
    * $xml = simplexml_load_file($xmlfile);
    *
    * //1. remove only 1 node (without child nodes)
    * //   $path must return only 1 (unique) node without child nodes
    * removeNode($xml, '//movie/rating[@type="thumbs"]');
    *
    * //2. remove 1 node (with 1 child nodes)
    * //    $path can return any nodes - will be removed only first node
    * //   with all child nodes
    * removeNode($xml, '//characters', 'child')
    *
    * //3. remove all nodes (with child nodes)
    * //   $path can return any nodes - will be removed all
    * //   with child nodes
    * removeNode($xml, '//rating', 'all')
    *
    * $xml->asXML($xmlfile);
    *
    */
   function removeNode($xml, $path, $multi='one')
   {
       $result = $xml->xpath($path);

          # for wrong $path
          if (!isset($result[0])) return false;

          switch ($multi) {
              case 'all':
                    $errlevel = error_reporting(E_ALL & ~E_WARNING);
                    foreach ($result as $r) unset ($r[0]);
                    error_reporting($errlevel);
                    return true;

                case 'child':
                   unset($result[0][0]);
                   return true;

                case 'one':
                   if (count($result[0]->children())==0 && count($result)==1) {
                       unset($result[0][0]);
                       return true;
                   }

                default:
                      return false;
          }
 }
?>

p.s. after use (if deleted all nodes):
<?php

   function trimXml($xmlfile)
   {
       $dom = new DOMDocument();

          $dom->preserveWhiteSpace = false;

         if (!$dom->load($xmlfile)) return false;

         $dom->formatOutput = true;

         if (!$dom->save($xmlfile)) return false;

         return true;
   }
?>
onPHP5.com 18-Nov-2007 01:37
It does not say in the docs, but SimpleXML will convert all text into UTF-8, if the source XML declaration has another encoding. Eg, if the source has the following XML decl:

<?xml version="1.0" encoding="windows-1251" ?>

all the text in the resulting SimpleXMLElement will be in UTF-8 automatically.
lordi at msdi dot ca 23-Oct-2007 02:35
If you need to do math calculations on values extracted from simplexml document, you might need to cast the value as float to prevent precision loss. Here is an example:

<?php
    $objXML = new SimpleXMLElement('<test x="-123.45"></test>');

    //Shows correctly
    echo $objXML['x']."\n";

    //We loose the decimals
    echo $objXML['x'] + $objXML['x']."\n";

    $x = $objXML['x'];

    //This works if we cast the amounts
    echo (float)$objXML['x'] + (float)$objXML['x']."\n";

    //Calculated on a string, no problem
    echo "-123.45" + "-123.45";

?>

This is due to the fact that $objXML['x'] is not a string (php would cast it automatically) neither a float, but a SimpleXMLElement object.

"echo var_dump($x);" will output this
~~
object(SimpleXMLElement)#3 (1) {
  [0]=>
  string(7) "-123.45"
}
~~

I opened a bug request on php but here is the answer they gave me:

~~
Status: Won't fix

The behavior is defined by the engine not the extension. When performing mathematical operations on objects, they are treated as integers. It is up to the user to cast the object to the appropriate type to maintain proper precision.
~~
cmsa at gmx dot de 27-Aug-2007 02:07
I had a problem with entities.

My first solution:
I saved Data that way:
$ENTRY_->
addchild('Nachricht',htmlentities($_POST["blog"]));

Had Entities in the XML-File like:
<!ENTITY auml "&amp;auml">

And I loaded the Data that way:
html_entity_decode($ENTRY->Nachname);

But after saving and
loading the xml-file the entity-entry
<!ENTITY auml "&amp;auml">
disappeared. strange...

My second solution:
With saving the Data this way:
$ENTRY_->
addchild('Nachricht',htmlentities(htmlentities($_POST["blog"])));

I can now load it with html_entity_decode without the
 entity-entry in the XML-file!
I tested it with aou.

Hope it helpes.
karsten dot bruch at bms-verlage dot de 16-Aug-2007 05:28
When creating a new XML document and adding text with umlauts and such

<?php $SimpleXMLElement->asXML(); ?>

will silently NOT output any content with umlauts.

Use htmlentities () while adding Umlauts & co to solve the "problem"
anthony dot parsons at manx dot net 04-Aug-2007 04:40
If you're handling lots of HTML or mixed-content XML you'll probably want to use the DOM functions instead of SimpleXML. Take this for example:
<?php
$html = new SimpleXMLElement('<div><p>Some text, <a href="#">a link,</a> more text</p></div>');

echo $html->p->a,"<br>\n"; // "a link,"
echo $html->p;             // "Some text, more text" (!)
?>
In the above example reconstructing the original markup is impossible because of the way SimpleXML represents the data.
php at werner dash ott dot de 28-Mar-2007 03:03
Concerning SimpleXML and sessions:

When creating a SimpleXML object (to be precise: a SimpleXMLElement object) in the context of a session and storing it in $_SESSION['XMLobject'], this object does not "survive" the session!

By the time re-entering the session, print_r($_SESSION['XMLobject']) says:

['XMLobject'] => SimpleXMLElement Object
Warning:  Node no longer exists in /your_php_file.php on line xyz
   (
   )

(Message simplified for the sake of better readability.)
codje2 at lboro dot ac dot uk 11-Mar-2007 10:46
Sometimes it's nice to mix up data storage types. This is a very simple SQL to XML converter. Feed it a SQL query and it outputs the result in XML.

The first paramater should be a mysql_query result
(optional)The second is the xml name for each row (i.e the second depth of XML)
(optional)The third is the name of the XML document, the root name

<?php
$result=mysql_query("SELECT * FROM users");
sql_to_xml($result,"users","members");

function sql_to_xml($mysql_result,$row_name="row",$doc_name="root")
{

    $xml= new SimpleXMLElement("<$doc_name></$doc_name>");
    while($line=mysql_fetch_assoc($mysql_result))
    {
        $row=$xml->addChild($row_name);
        foreach($line as $column => $value)
        {
            $row->$column="$value";
        }
    }
    return $xml->asXML();
}

?>
php at NOSPAM dot coutch dot com 21-Feb-2007 11:04
Memory leak when setting attributes as in example (#Example 2134. Setting values)

This probably goes unnoticed in web scripts (unless you do a LOT of xml manipulations), but I ran into this in my standalone script that processes a large number of XML files.

The following code will eat up memory quite fast:

<?php
include 'example.php';

while (true) {
  $xml = new SimpleXMLElement($xmlstr);
  $xml->movie[0]->characters->character[0]->name = 'Miss Coder';
}
?>

while this seems to behave correctly:

<?php
include 'example.php';

while (true) {
  $xml = new SimpleXMLElement($xmlstr);
  $c = $xml->xpath("//character");
  $c[0]->name = 'Miss Coder';
}
?>

This looks like bug #38604, and I just confirmed that in 6.0.0-dev (on Windows at least) it is fixed.  It is NOT fixed in 5.2.1 or  5.2.2-dev (2/21 build), so for 5.2 users, use the second form to avoid leaks.
joel [from the website] purerave.com 20-Feb-2007 02:57
It doesn't mention this anywhere, but creationg a new SimpleXMLElement object from a non-valid string throws an exception. It looks ugly in the php log as it dumps the stack in multiple lines.

The correct way to create a new SimpleXMLElement object is like so:
<?php
$xmlstr = ''; // empty to throw an exception
try {
   $xml = new SimpleXMLElement($xmlstr);
} catch (Exception $e) {
   // handle the error
   echo '$xmlstr is not a valid xml string';
}
?>
matt at mattbostock dot com 06-Nov-2006 08:40
Be careful when using var_export to debug element attributes - it won't work! Always use print() or similar for checking the contents of element attributes.
Arnaud Bienvenu 19-Oct-2006 04:35
As of PHP 5.1.4, trying to iterate on a non-existent node will actually create that node.

<?
$xml = simplexml_load_string('<root></root>');
foreach($xml->b as $dummy);
echo $xml->asXML();
?>

Gives :

<?xml version="1.0"?>
<root><b/></root>

You might think it is a bug, but PHP developers seam to consider it as a feature : http://bugs.php.net/bug.php?id=39164
Jason Sheets <jsheets at shadonet dot com> 31-May-2006 07:31
Here is my Simple XML to array function, it is recursive and has the benefit of maintaining key value relationships and has worked well for me.

<?php
function XMLToArray($xml)
{
  if ($xml instanceof SimpleXMLElement) {
    $children = $xml->children();
    $return = null;
  }

  foreach ($children as $element => $value) {
    if ($value instanceof SimpleXMLElement) {
      $values = (array)$value->children();

      if (count($values) > 0) {
        $return[$element] = XMLToArray($value);
      } else {
        if (!isset($return[$element])) {
          $return[$element] = (string)$value;
        } else {
          if (!is_array($return[$element])) {
            $return[$element] = array($return[$element], (string)$value);
          } else {
            $return[$element][] = (string)$value;
          }
        }
      }
    }
  }

  if (is_array($return)) {
    return $return;
  } else {
    return $false;
  }
}
?>
daniel dot oconnor at gmail dot com 18-May-2006 03:21
SimpleXML handles namespaces, but it's not documented very well here at all.

If you wanted to parse, say, an open office manifest file or a piece of RDF/XML, you have to get elements and attributes by the namespace url.

Example:
<?php
function display($in) {
    if (file_exists($in)) {
        $xml = simplexml_load_file($in);
    } else {
        throw new Exception($in . " does not exist");
    }

    $manifest = $xml->children('http://openoffice.org/2001/manifest');

    foreach ($manifest->xpath('//manifest:file-entry') as $file) {
        foreach ($file->attributes('http://openoffice.org/2001/manifest') as $key => $value) {
            print "Key:" . $key . "\n";
            print "Value:" . $value . "\n";
        }
    }

}
?>

File: manifest.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">
<manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">
 <manifest:file-entry manifest:media-type="application/vnd.sun.xml.writer" manifest:full-path="/"/>
 <manifest:file-entry manifest:media-type="application/vnd.sun.xml.ui.configuration" manifest:full-path="Configurations2/"/>
 <manifest:file-entry manifest:media-type="application/binary" manifest:full-path="layout-cache"/>
 <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
 <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="styles.xml"/>
 <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="meta.xml"/>
 <manifest:file-entry manifest:media-type="" manifest:full-path="Thumbnails/thumbnail.png"/>
 <manifest:file-entry manifest:media-type="" manifest:full-path="Thumbnails/"/>
 <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="settings.xml"/>
</manifest:manifest>
paul at preinheimer dot com 06-May-2005 02:11
Hi,

If you want to access an element that has a dash in its name, (as is common with the XML documents provided by the Library of Congress, as well as the NWS) you will need to handle it a little bit differently.

You can either use XPATH, which works fine, but will return an array of results every time, even if there is a single result.
eg.

<?php
$xml->xpath('/data/time-layout/start-valid-time');
?>

You can also choose just to encapsulate the element names containing a dash:
<?php $xml->data->{'time-layout'}->{'start-valid-time'} ?>

--

On a only partially related note, dealing with SimpleXML is one of the only times I have employed casting with PHP. While iterating (foreach) through the valid times, echo'ing the element worked great (it merely echo'ed the apropriate time), assigning it to another variable resulted in a SimpleXML object containing the time to be assigned, rather than just the time itself. This was resolved by casting the time to a string:

<?php
foreach($xml->data->{'time-layout'}->{'start-valid-time'} AS $time)
{
 $weatherDates[] = (string) $time;
}
?>
T CHASSAGNETTE t_chassagnette at yahoo dot fr 04-May-2005 10:57
Another method to parse an XML Document into a PHP array with SIMPLEXML inspired from Daniel FAIVRE !

<?php
function xml2php($xml)
{
    $fils = 0;
    $tab = false;
    $array = array();
    foreach($xml->children() as $key => $value)
    {
        $child = xml2php($value);

        //To deal with the attributes
        foreach($node->attributes() as $ak=>$av)
        {
            $child[$ak] = (string)$av;

        }

        //Let see if the new child is not in the array
        if($tab==false && in_array($key,array_keys($array)))
        {
            //If this element is already in the array we will create an indexed array
            $tmp = $array[$key];
            $array[$key] = NULL;
            $array[$key][] = $tmp;
            $array[$key][] = $child;
            $tab = true;
        }
        elseif($tab == true)
        {
            //Add an element in an existing array
            $array[$key][] = $child;
        }
        else
        {
            //Add a simple element
            $array[$key] = $child;
        }

        $fils++;
      }

    if($fils==0)
    {
        return (string)$xml;
    }

    return $array;

}
?>
nelson_menezes at yahoo dot co dot uk 01-Mar-2005 02:05
Note that SimpleXML expects to both read and output XML in UTF-8 encoding. You'll need to add a line such as this at the top of your input XML file if it isn't saved in UTF-8 (adjust to whatever encoding used):

<?xml version="1.0" encoding="ISO-8859-1" ?>

On the output side of things, if you're not serving/handling UTF-8, you'll need to use utf8_decode() [red. but that will only work for ISO-8859-1, not other encodings]. Common mistake: http://bugs.php.net/bug.php?id=28154
brcavanagh AT NO SPAM hotmail.com 03-Jan-2005 06:18
If you are looking to use SimpleXML for anything but reading XML documents, you should really reconsider, and use the XML DOM library.  By the time you get enough utilities implemented in DOM to handle all the set backs in SimpleXML, you will have defeated the purpose of using SimpleXML.  There are a few reasons for this, and there are already many workrounds, but the primairy issues are this

1) No complex node assignment.  You cannot switch nodes or replace them.

2) No appending new child nodes

3) Whenever you do something like $new_node = $xml_doc->node you will always get a reference even if you use a clone method, which will crash the script.

Other than that, its a great tool for reading docs.


Get a SimpleXMLElement object from a DOM node.

  • ≪ SimpleXML 함수 목록
  • simplexml_load_file ≫
  • PHP Manual
  • SimpleXML 함수 목록
  • Get a SimpleXMLElement object from a DOM node.

simplexml_import_dom

(PHP 5, PHP 7)

simplexml_import_dom — Get a SimpleXMLElement object from a DOM node.

설명

SimpleXMLElement simplexml_import_dom ( DOMNode $node [, string $class_name = "SimpleXMLElement" ] )

This function takes a node of a DOM document and makes it into a SimpleXML node. This new object can then be used as a native SimpleXML element.

인수

node

A DOM Element node

class_name

You may use this optional parameter so that simplexml_import_dom() will return an object of the specified class. That class should extend the SimpleXMLElement class.

반환값

Returns a SimpleXMLElement실패 시 FALSE를 반환합니다.

Warning

이 함수는 논리 FALSE를 반환하지만, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

예제

Example #1 Importing DOM

<?php
$dom = new DOMDocument;
$dom->loadXML('<books><book><title>blah</title></book></books>');
if (!$dom) {
    echo 'Error while parsing the document';
    exit;
}

$s = simplexml_import_dom($dom);

echo $s->book[0]->title;
?>

위 예제의 출력:

blah

참고

  • dom_import_simplexml() - Gets a DOMElement object from a SimpleXMLElement object

User Contributed Notes

brandonkirsch at perceptionilluminates dot com 15-Aug-2013 12:38
simplexml_import_dom() will accept DOMNodes or other SimpleXMLElements.  This has a useful purpose if you are extending the SimpleXMLElement class and would prefer the xpath() method to return your class extension instead of basic SimpleXMLElements.  For example:

class MySimpleXML extends SimpleXMLElement{
    public function xpath($xpath){
        $return = array();
        $simpleXmls = parent::xpath($xpath); // run SimpleXMLElement xpath, returning an array of SimpleXMLElements
        foreach($simpleXmls as $xml){
            $return[] = simplexml_import_dom($xml,'MySimpleXML'); // copy SimpleXMLElement, returning as MySimpleXML instance
        }
        return $return;
    }
}

Now I can create an instance of MySimpleXML, run an xpath, and get my results back as an array of MySimpleXML elements instead of the native SimpleXMLElement:

$mySimpleXml = new mySimpleXml('<root><node/></root>');

$array = $mySimpleXml->xpath('/root/node'); // returns array with a single MySimpleXml object


Interprets an XML file into an object

  • ≪ simplexml_import_dom
  • simplexml_load_string ≫
  • PHP Manual
  • SimpleXML 함수 목록
  • Interprets an XML file into an object

simplexml_load_file

(PHP 5, PHP 7)

simplexml_load_file — Interprets an XML file into an object

설명

SimpleXMLElement simplexml_load_file ( string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] )

Convert the well-formed XML document in the given file to an object.

인수

filename

Path to the XML file

Note:

Libxml 2 unescapes the URI, so if you want to pass e.g. b&c as the URI parameter a, you have to call simplexml_load_file(rawurlencode('http://example.com/?a=' . urlencode('b&c'))). Since PHP 5.1.0 you don't need to do this because PHP will do it for you.

class_name

You may use this optional parameter so that simplexml_load_file() will return an object of the specified class. That class should extend the SimpleXMLElement class.

options

Since PHP 5.1.0 and Libxml 2.6.0, you may also use the options parameter to specify additional Libxml parameters.

ns

Namespace prefix or URI.

is_prefix

TRUE if ns is a prefix, FALSE if it's a URI; defaults to FALSE.

반환값

Returns an object of class SimpleXMLElement with properties containing the data held within the XML document,실패 시 FALSE를 반환합니다.

Warning

이 함수는 논리 FALSE를 반환하지만, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

오류/예외

Produces an E_WARNING error message for each error found in the XML data.

Tip

Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.

예제

Example #1 Interpret an XML document

<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.

if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');

    print_r($xml);
} else {
    exit('Failed to open test.xml.');
}
?>

This script will display, on success:

SimpleXMLElement Object
(
  [title] => Example Title
  ...
)

At this point, you can go about using $xml->title and any other elements.

참고

  • simplexml_load_string() - Interprets a string of XML into an object
  • SimpleXMLElement::__construct() - Creates a new SimpleXMLElement object
  • Dealing with XML errors
  • libxml_use_internal_errors() - Disable libxml errors and allow user to fetch error information as needed

User Contributed Notes

sirgrayjn at gmail dot com 23-Sep-2016 06:51
// Be carefull if you migrate or use local machine
// for test/development.

// Windows directory separators: "\" and "/"
// You may mix separators "C:\somedir\www/img/bg.jpg".

// Mixed separators path work fine in other functions
// But simplexml_load_file() failed with mixed separators.

// Examples:
include("C:\dir\my.php");    // work (windows)
include("C:\dir/my.php");    // work (windows) with mixed
include("C:/dir/my.php");    // work (windows, linux)
simplexml_load_file("C:\dir\my.php");    // work
simplexml_load_file("C:\dir/my.php");    // failed with mixed
simplexml_load_file("C:/dir/my.php");    // work
rex111 at bigmir dot net 09-Sep-2016 12:02
Get all tags and their values. (recursive)

<?php
$xml = simplexml_load_file('settings.xml');

function all_tag($xml){
    $i=0; $name = "";
    foreach ($xml as $k){
        $tag = $k->getName();
        $tag_value = $xml->$tag;
        if ($name == $tag){ $i++;    }
                $name = $tag;
            echo $tag .' '.$tag_value[$i].'<br />';
        // recursive
           all_tag($xml->$tag->children());
    }
}

all_tag($xml);
?>
Rich 24-Nov-2015 11:51
I stumbled on this: a single element with a simple string in it becomes a string, but a single element with a *space* in it becomes an Array, with one element, the string space.

I'm sure to XML mystics this is wise and wonderful but it really confused me,  and I thought it might confuse others.

<?php
$parsed = simplexml_load_string('<container><space> </space><blank></blank><string>hello</string></container>');
$content = json_decode(json_encode($parsed),TRUE);
var_dump($content);
/* Output is:
array(3) {
  'space' => array(1) {           ← did NOT expect this!
    [0] => string(1) " "
  }
  'blank' => array(0) { }
  'string' => string(5) "hello"
}
 */
tuxedobob 30-Jun-2015 10:25
Occasionally you may try to load a file and have it complain about an entity and throw a parser error.

If this is the case, check to make sure that the file in question does not contain an ampersand (&) without a corresponding entity reference.

If it does, or if you want to err on the side of caution, then instead of using simplexml_load_file, try this:

$file = file_get_contents('stuff.xml');
$temp = preg_replace('/&(?!(quot|amp|pos|lt|gt);)/', '&amp;', $file);
$xml = simplexml_load_string($temp) or die("xml not loading");

Read the file into a string, add 'amp;' after any '&' that is not part of a character entity, then parse the string as xml.
kannan at 99deals dot in 04-Sep-2012 03:27
If you have some nodes which are having special characters, it would not load properly

for an instance see the nodes below
<node:number>1538-7445</node:number>
<node:coverDisplayDate>Sep  1 2012 12:00:00:000AM</node:coverDisplayDate>

either you have to change the : to other special characters like '-' in order to convert it properly

Correct Node
<node-number>1538-7445</node-number>
<node-coverDisplayDate>Sep  1 2012 12:00:00:000AM</node-coverDisplayDate>

I have wasted my precious time while debugging this. Please aware about this. ?
ricardo at ricardomartins dot info 22-Aug-2012 02:49
Sometimes we have xml's with hyphens nodes, like

<my_xml>
 <some-node>value</some-node>
</my_xml>

You'll need to use
<?php
$simpleXmlObj->{'some-node'}
?>

instead of
<?php
$simpleXmlObj->some-node;
?>
fusionstream at gmail dot com 05-Apr-2012 01:15
If you are loading many files, this may slow down your page load time.

To set a timeout, use file_get_context and then simplexml_load_string

<?php

$fp = fopen('http://www.example.com/rss', false, stream_create_context(array('http' => array('timeout', '1.5'))));

if ($fp) {
    print_r( simplexml_load_string($fp) );
} else {
    echo "The request timed out";
}
?>
sean at aliencreations dot com 17-Mar-2011 07:59
If you find that you are receiving 500 errors with simplexml_load_file() but you can access the xml/rss feed manually through a browser, your script is probably being blocked by a user agent sniffer.

Add this code before your xml call to remedy this issue

<?php

ini_set("user_agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
ini_set("max_execution_time", 0);
ini_set("memory_limit", "10000M");

$rss = simplexml_load_file($feed_url);

?>
Nanuit at ossi dot at 08-Feb-2011 10:51
A little function very helpfull in using simplexml_load_file behind a proxy

<?php
function getXMLfromURL($url) {
      $Proxy = getenv("HTTP_PROXY");

      if (strlen($Proxy) > 1) {
        $r_default_context = stream_context_get_default ( array
                    ('http' => array(
                        'proxy' => $Proxy,
                        'request_fulluri' => True,
                    ),
                )
            );
        libxml_set_streams_context($r_default_context);
      }
      $daten = simplexml_load_file($url);
      return ($daten);
    }
?>

where HTTP_PROXY is set to e.g.: tcp://proxy:8080
guego dot ol at ig dot com dot br 13-Nov-2010 12:26
Analyze fully XML.

<?php
$xml = simplexml_load_file('file.xml');

foreach($xml as $key0 => $value){
echo "..1..[$key0] => $value";
foreach($value->attributes() as $attributeskey0 => $attributesvalue1){
echo "________[$attributeskey0] = $attributesvalue1";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value as $key => $value2){
echo "....2.....[$key] => $value2";
foreach($value2->attributes() as $attributeskey => $attributesvalue2){
echo "________[$attributeskey] = $attributesvalue2";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value2 as $key2 => $value3){
echo ".........3..........[$key2] => $value3";
foreach($value3->attributes() as $attributeskey2 => $attributesvalue3){
echo "________[$attributeskey2] = $attributesvalue3";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value3 as $key3 => $value4){
echo "...................4....................[$key3] => $value4";
foreach($value4->attributes() as $attributeskey3 => $attributesvalue4){
echo "________[$attributeskey3] = $attributesvalue4";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value4 as $key4 => $value5){
echo ".....................5......................[$key4] => $value5";
foreach($value5->attributes() as $attributeskey4 => $attributesvalue5){
echo "________[$attributeskey4] = $attributesvalue5";
}
echo '<br />';
////////////////////////////////////////////////
foreach($value5 as $key5 => $value6){
echo "......................6.......................[$key5] => $value6";
foreach($value6->attributes() as $attributeskey5 => $attributesvalue6){
echo "________[$attributeskey5] = $attributesvalue6";
}
echo '<br />';
}}}}}
echo '<br />';
}
?>
raduispas at gmail dot com 21-Oct-2010 03:12
if you want to check when this function fails,make sure to compare the return value with ===  instead of == :

<?php
$url = 'http://www.example.com';
$xml = simpleXML_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA);
if($xml ===  FALSE)
{
   //deal with error
}
else { //do stuff }
?>

Otherwise you may end up with FALSE all the time even if the document is ok. Hope this helps someone ;)
jamie at splooshmedia dot co dot uk 31-Mar-2010 03:17
A wrapper around simplexml_load_file to circumvent nasty error messages when the xml server times out or gives a 500 error etc.

<?php
function loadXML2($domain, $path, $timeout = 30) {

    /*
        Usage:

        $xml = loadXML2("127.0.0.1", "/path/to/xml/server.php?code=do_something");
        if($xml) {
            // xml doc loaded
        } else {
            // failed. show friendly error message.
        }
    */

    $fp = fsockopen($domain, 80, $errno, $errstr, $timeout);
    if($fp) {
        // make request
        $out = "GET $path HTTP/1.1\r\n";
        $out .= "Host: $domain\r\n";
        $out .= "Connection: Close\r\n\r\n";
        fwrite($fp, $out);

        // get response
        $resp = "";
        while (!feof($fp)) {
            $resp .= fgets($fp, 128);
        }
        fclose($fp);
        // check status is 200
        $status_regex = "/HTTP\/1\.\d\s(\d+)/";
        if(preg_match($status_regex, $resp, $matches) && $matches[1] == 200) {
            // load xml as object
            $parts = explode("\r\n\r\n", $resp);
            return simplexml_load_string($parts[1]);
        }
    }
    return false;

}
?>
Smokey 15-Mar-2010 07:16
for nested and same name values i'v made up this little bit for getting and displaying multiable values from google's geocode when a exact match is not found it returns all close matches in the following format(this is an abriged version of there output)

<Response>
  <Placemark id="1">
    <address> New York 24, NY, USA</address>
    <AddressDetails>
      ..................
    </AddressDetails>
    <Point>
      <coordinates>-73.5850086,40.7207442,0</coordinates>
    </Point>
  </Placemark>
  <Placemark id="2">
    <address>New York 27, NY, USA</address>
    <AddressDetails>
      ...................
    </AddressDetails>
    <Point>
      <coordinates>-72.8987835,40.8003588,0</coordinates>
    </Point>
  </Placemark>
  <Placemark id="3">
    <address>Cedar Place School, 20 Cedar Pl, Yonkers, NY 10705, USA</address>
    <AddressDetails>
      ..................
    </AddressDetails>
    <Point>
      <coordinates>-73.8966320,40.9256520,0</coordinates>
    </Point>
  </Placemark>
</Response>

<?php
// get and breakdown the results then store them in $var's
$Address = "99999 parkplace, new york, NY";
$urladdress = urlencode($Address);
$Base_url = "http://maps.google.com/maps/geo?q=";
$urlParts = "&output=xml";
$urlrequest = $Base_url . $urladdress . $urlParts;
$xml = simplexml_load_file($urlrequest);
$num = "0";
foreach ($xml->Response->Placemark as $value){
    $num++;
    $GeoFindAdd{$num} = $value->address;
    $GeoFindCords{$num} = $value->Point->coordinates;
}

// a simple display for the results
echo "Found ",$num," Possable Geo Data Sets <br>";
$CountNumResults = "0";
for ( ; $num > 0; $num--){
    $CountNumResults++;
    echo $countnum,"<br> Address = ",$GeoFindAdd{$num},"<br> Coordinates = ",$GeoFindCords{$num},"<br>";
}
echo "END";
?>
knl at bitflop dot com 11-Sep-2009 09:02
If you need to parse the data from SimpleXML into a session variable remember to define the data as a string first.

If you don't you will get warnings of "Node no longer exists" pointing to your session_start() function.

This will work:

<?php

    $new_version = simplexml_load_file('http://example.com/version.xml');
    $_SESSION['current_version'] = (string)$new_version->version;

?>
neil art neilanddeb dort com 17-Aug-2009 03:00
Because the encoding of my XML file is UTF-8 and the
encoding of my web page is iso-8859-1 I was getting strange characters such as a€™ instead of a right single quote.

The solution to this turned out to be hard to find, but really easy to implement.

http://uk3.php.net/manual/en/function.iconv.php

Using the iconv() function you can convert from one encodign to another, the TRANSLIT option seems to work best for what I needed.  Here's my example:

<?php
// convert string from utf-8 to iso8859-1
$horoscope = iconv( "UTF-8", "ISO-8859-1//TRANSLIT", $horoscope );
?>

I found the solution on this page...
http://tinyurl.com/lm39xc
Hope this helps
cryonyx at cerebrate dot ru 21-Oct-2008 12:34
In case you have a XML file with a series of equally named elements on one level simplexml incorrectly processes them and doesn't allow to walk through the array using foreach(). As far as I'm concerned, it is the problem caused by PHP xml_parser (see: http://ru2.php.net/manual/ru/function.xml-parser-create.php#53188).

To avoid this, just use count() and walk through the array using for().

Example:

<params>
  <param>
    <name>version.shell</name>
    <value>1.0</value>
  </param>
  <param>
      <name>version.core</name>
      <value>1.0</value>
  </param>
  <param>
      <name>file.lang</name>
      <value>vc.lang</value>
  </param>
  ...
</params>

<?php
$filename = '...';
$xml = simplexml_load_file($filename);
$p_cnt = count($xml->param);
for($i = 0; $i < $p_cnt; $i++) {
  $param = $xml->param[$i];
  ...;
}
?>
mario 02-Sep-2008 11:08
If you want CDATA in your object you should use LIBXML_NOCDATA

<?php
$xml = simplexml_load_file($file_xml, 'SimpleXMLElement',LIBXML_NOCDATA);

    print_r($xml);
?>
l [DOT] anzinger [AT] gmail [DOT] com 26-Mar-2008 06:03
If you don't want that the CDATA values get escaped, just load the XML with LIBXML_NOCDATA as an 3rd argument.

Note: A PHP version >= 5.1.0 is required for this to work.

Example:

<?php simplexml_load_file('xmldatei.xml', null, LIBXML_NOCDATA); ?>
php at werner dash ott dot de 29-Mar-2007 10:13
Making SimpleXMLElement objects session save.

Besides the effect of not surviving sessions, the SimpleXMLElement object may even crash the session_start() function when trying to re-enter the session!

To come up with a solution for this, I used a pattern as follows. The core idea is to transform the SimpleXMLElement between session calls to and from a string representation which of course is session save.

<?php
  //
  // session save handling of SimpleXMLElement objects
  // (applies to/ tested with PHP 5.1.5 and PHP 5.2.1)
  // The myClass pattern allows for conveniently accessing
  // XML structures while being session save
  //
  class myClass
  {
    private $o_XMLconfig = null;
    private $s_XMLconfig = '';

    public function __construct($args_configfile)
    {
      $this->o_XMLconfig = simplexml_load_file($args_configfile);
      $this->s_XMLconfig = $this->o_XMLconfig->asXML();
    } // __construct()

    public function __destruct()
    {
      $this->s_XMLconfig = $this->o_XMLconfig->asXML();
      unset($this->o_XMLconfig); // this object would otherwise crash
                                 // the subsequent call of
                                 // session_start()!
    } // __destruct()

    public function __wakeup()
    {
      $this->o_XMLconfig = simplexml_load_string($this->s_XMLconfig);
    } // __wakeup()

  } // class myClass
?>
wouter at code-b dot nl 20-Feb-2007 11:08
To correctly extract a value from a CDATA just make sure you cast the SimpleXML Element to a string value by using the cast operator:

<?php
$xml = '<?xml version="1.0" encoding="UTF-8" ?>
<rss>
    <channel>
        <item>
            <title><![CDATA[Tom & Jerry]]></title>
        </item>
    </channel>
</rss>';

$xml = simplexml_load_string($xml);

// echo does the casting for you
echo $xml->channel->item->title;

// but vardump (or print_r) not!
var_dump($xml->channel->item->title);

// so cast the SimpleXML Element to 'string' solve this issue
var_dump((string) $xml->channel->item->title);
?>

Above will output:

Tom & Jerry

object(SimpleXMLElement)#4 (0) {}

string(11) "Tom & Jerry"
Kyle 10-Dec-2006 11:35
In regards to Anonymous on 7th April 2006

There is a way to get back HTML tags. For example:

<?xml version="1.0"?>
<intro>
    Welcome to <b>Example.com</b>!
</intro>

<?php
// I use @ so that it doesn't spit out content of my XML in an error message if the load fails. The content could be passwords so this is just to be safe.
$xml = @simplexml_load_file('content_intro.xml');
if ($xml) {
    // asXML() will keep the HTML tags but it will also keep the parent tag <intro> so I strip them out with a str_replace. You could obviously also use a preg_replace if you have lots of tags.
    $intro = str_replace(array('<intro>', '</intro>'), '', $xml->asXML());
} else {
    $error = "Could not load intro XML file.";
}
?>

With this method someone can change the intro in content_intro.xml and ensure that the HTML is well formed and not ruin the whole site design.
Anonymous 07-Apr-2006 06:21
What has been found when using the script is that simplexml_load_file() will remove any HTML formating inside the XML file, and will also only load so many layers deep. If your XML file is to deap, it will return a boolean false.
fdouteaud at gmail dot com 09-Mar-2006 02:21
Be careful if you are using simplexml data directly to feed your MySQL database using MYSQLi and bind parameters.

The data coming from simplexml are Objects and the bind parameters functions of MySQLi do NOT like that! (it causes some memory leak and can crash Apache/PHP)

In order to do this properly you MUST cast your values to the right type (string, integer...) before passing them to the binding methods of MySQLi.
I did not find that in the documentation and it caused me a lot of headache.
info at evasion dot cc 06-Feb-2006 05:26
Sorry there's a mistake in the previous function :
<?php
   function &getXMLnode($object, $param) {
       foreach($object as $key => $value) {
           if(isset($object->$key->$param)) {
               return $object->$key->$param;
           }
           if(is_object($object->$key)&&!empty($object->$key)) {
               $new_obj = $object->$key;
               // Must use getXMLnode function there (recursive)
               $ret = getXMLnode($new_obj, $param);

           }
       }
       if($ret) return (string) $ret;
       return false;
   }
?>
skutter at imprecision dot net 03-Feb-2006 06:11
So it seems SimpleXML doesn't support CDATA... I bashed together this little regex function to sort out the CDATA before trying to parse XML with the likes of simplexml_load_file / simplexml_load_string. Hope it might help somebody and would be very interested to hear of better solutions. (Other than *not* using SimpleXML of course! ;)

It looks for any <![CDATA [Text and HTML etc in here]]> elements, htmlspecialchar()'s the encapsulated data and then strips the "<![CDATA [" and "]]>" tags out.

<?php
function simplexml_unCDATAise($xml) {
    $new_xml = NULL;
    preg_match_all("/\<\!\[CDATA \[(.*)\]\]\>/U", $xml, $args);

    if (is_array($args)) {
        if (isset($args[0]) && isset($args[1])) {
            $new_xml = $xml;
            for ($i=0; $i<count($args[0]); $i++) {
                $old_text = $args[0][$i];
                $new_text = htmlspecialchars($args[1][$i]);
                $new_xml = str_replace($old_text, $new_text, $new_xml);
            }
        }
    }

    return $new_xml;
}

//Usage:
$xml = 'Your XML with CDATA...';
$xml = simplexml_unCDATAise($xml);
$xml_object = simplexml_load_string($xml);
?>
info at evasion dot cc 03-Feb-2006 12:37
Suppose you have loaded a XML file into $simpleXML_obj.
The structure is like below :

SimpleXMLElement Object
(

    [node1] => SimpleXMLElement Object
        (
            [subnode1] => value1
            [subnode2] => value2
            [subnode3] => value3
        )

    [node2] => SimpleXMLElement Object
        (
            [subnode4] => value4
            [subnode5] => value5
            [subnode6] => value6
        )

)

When searching a specific node in the object, you may use this function :

<?php

    function &getXMLnode($object, $param) {
        foreach($object as $key => $value) {
            if(isset($object->$key->$param)) {
                return $object->$key->$param;
            }
            if(is_object($object->$key)&&!empty($object->$key)) {
                $new_obj = $object->$key;
                $ret = getCfgParam($new_obj, $param);
            }
        }
        if($ret) return (string) $ret;
        return false;
    }
?>

So if you want to get subnode4 value you may use this function like this :

<?php
$result = getXMLnode($simpleXML_obj, 'subnode4');
echo $result;
?>

It display "value4"
genialbrainmachine at NOSPAM dot tiscali dot it 30-Sep-2005 05:52
Micro$oft Word uses non-standard characters and they create problems in using simplexml_load_file.
Many systems include non-standard Word character in their implementation of ISO-8859-1. So an XML document containing that characters can appear well-formed (i.e.) to many browsers. But if you try to load this kind of documents with simplexml_load_file you'll have a little bunch of troubles..
I believe that this is exactly the same question discussed in htmlentites. Following notes to htmlentitles are interesting here too (given in the reverse order, to grant the history):
http://it.php.net/manual/en/function.htmlentities.php#26379
http://it.php.net/manual/en/function.htmlentities.php#41152
http://it.php.net/manual/en/function.htmlentities.php#42126
http://it.php.net/manual/en/function.htmlentities.php#42511
mark 12-Sep-2005 08:06
If the property of an object is empty the array is not created. Here is a version object2array that transfers properly.

<?php
function object2array($object)
{
    $return = NULL;

    if(is_array($object))
    {
        foreach($object as $key => $value)
            $return[$key] = object2array($value);
    }
    else
    {
        $var = get_object_vars($object);

        if($var)
        {
            foreach($var as $key => $value)
                $return[$key] = ($key && !$value) ? NULL : object2array($value);
        }
        else return $object;
    }

    return $return;
}
?>


Interprets a string of XML into an object

  • ≪ simplexml_load_file
  • WDDX ≫
  • PHP Manual
  • SimpleXML 함수 목록
  • Interprets a string of XML into an object

simplexml_load_string

(PHP 5, PHP 7)

simplexml_load_string — Interprets a string of XML into an object

설명

SimpleXMLElement simplexml_load_string ( string $data [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] )

Takes a well-formed XML string and returns it as an object.

인수

data

A well-formed XML string

class_name

You may use this optional parameter so that simplexml_load_string() will return an object of the specified class. That class should extend the SimpleXMLElement class.

options

Since PHP 5.1.0 and Libxml 2.6.0, you may also use the options parameter to specify additional Libxml parameters.

ns

Namespace prefix or URI.

is_prefix

TRUE if ns is a prefix, FALSE if it's a URI; defaults to FALSE.

반환값

Returns an object of class SimpleXMLElement with properties containing the data held within the xml document,실패 시 FALSE를 반환합니다.

Warning

이 함수는 논리 FALSE를 반환하지만, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

오류/예외

Produces an E_WARNING error message for each error found in the XML data.

Tip

Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.

예제

Example #1 Interpret an XML string

<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;

$xml = simplexml_load_string($string);

print_r($xml);
?>

위 예제의 출력:

SimpleXMLElement Object
(
  [title] => Forty What?
  [from] => Joe
  [to] => Jane
  [body] =>
   I know that's the answer -- but what's the question?
)

At this point, you can go about using $xml->body and such.

참고

  • simplexml_load_file() - Interprets an XML file into an object
  • SimpleXMLElement::__construct() - Creates a new SimpleXMLElement object
  • Dealing with XML errors
  • libxml_use_internal_errors() - Disable libxml errors and allow user to fetch error information as needed

User Contributed Notes

artistan at gmail dot com 11-Jun-2015 09:32
Here is my update to Bob's simple SimpleXML wrapper function.
I noticed his version would turn an empty SimpleXMLElement into an empty array.

<?php
    /**
     * http://php.net/manual/en/function.simplexml-load-string.php#91564
     *
     * bool/array unserialize_xml ( string $input [ , callback $callback ] )
     * Unserializes an XML string, returning a multi-dimensional associative array, optionally runs a callback on all non-array data
     * Returns false on all failure
     * Notes:
     * Root XML tags are stripped
     * Due to its recursive nature, unserialize_xml() will also support SimpleXMLElement objects and arrays as input
     * Uses simplexml_load_string() for XML parsing, see SimpleXML documentation for more info
     *
     * @param $input
     * @param null $callback
     * @param bool $recurse
     * @return array|mixed
     *
     */
    function unserialize_xml($input, $callback = null, $recurse = false)
    {
        // Get input, loading an xml string with simplexml if its the top level of recursion
        $data = ((!$recurse) && is_string($input))? simplexml_load_string($input): $input;
        // Convert SimpleXMLElements to array
        if ($data instanceof SimpleXMLElement){
            if(!empty($data)){
                $data = (array) $data;
            } else {
                $data = '';
            }
        }
        // Recurse into arrays
        if (is_array($data)) foreach ($data as &$item) $item = unserialize_xml($item, $callback, true);
        // Run callback and return
        return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data): $data;
    }
?>
meustrus 08-May-2015 12:38
Be careful checking for parse errors. An empty SimpleXMLElement may resolve to FALSE, and if your XML contains no text or only contains namespaced elements your error check may be wrong. Always use `=== FALSE` when checking for parse errors.

<?php

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Root xmlns:ns1="http://example.com/custom">
<ns1:Node>There's stuff here</ns1:Node>
</ns1:Root>
XML;

$simplexml = simplexml_load_string($xml);

// This prints "Parse Error".
echo ($simplexml ? 'Valid XML' : 'Parse Error'), PHP_EOL;

// But this prints "There's stuff here", proving that
// the SimpleXML object was created successfully.
echo $simplexml->children('http://example.com/custom')->Node, PHP_EOL;

// Use this instead:
echo ($simplexml !== FALSE ? 'Valid XML' : 'Parse Error'), PHP_EOL;

?>

See:

https://bugs.php.net/bug.php?id=31045
https://bugs.php.net/bug.php?id=30972
https://bugs.php.net/bug.php?id=69596
dkrnl at yandex dot ru 30-Sep-2013 11:58
Wrapper XMLReader class, for simple SAX-reading huge xml:
https://github.com/dkrnl/SimpleXMLReader

Usage example: http://github.com/dkrnl/SimpleXMLReader/blob/master/examples/example1.php
AllenJB 12-Oct-2012 05:09
<?php
$xml = json_decode(json_encode((array) simplexml_load_string($string)), 1);
?>

A reminder that json_encode attempts to convert data to UTF-8 without specific knowledge of the source encoding. This method can cause encoding issues if you're not working in UTF-8.
Anonymous 01-Mar-2012 05:54
Use libxml_disable_entity_loader() to restrict loading of external files.  See http://www.idontplaydarts.com/2011/02/scanning-the-internal-network-using-simplexml/
lb at bostontech dot net 08-Nov-2011 07:40
please note that:

<?
$data_array = (array) simplexml_load_string($xml_string);
?>

will only convert the root element to an array, where as the child elements remain XML objects.

To prove this, try returning a value N objects deep by association.  Then try the same exercise using something like the object2array example below. (Which works great).
Diego Araos, diego at klapmedia dot com 05-Feb-2011 09:57
A simpler way to transform the result into an array (requires json module).

<?php
function object2array($object) { return @json_decode(@json_encode($object),1); }
?>

Example:
<?php
$xml_object=simplexml_load_string('<SOME XML DATA');
$xml_array=object2array($xml_object);
?>
ascammon at hotmail dot com 28-Dec-2010 10:04
I had a hard time finding this documented, so posting it here in case it helps someone:

If you want to use multiple libxml options, separate them with a pipe, like so:

<?php
$xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS);
?>
thejhereg at gmail dot com 30-Aug-2010 08:24
"simplexml_load_string() : Entity: line #: parser error : Comment not terminated"

On the off chance you see this error and you're pulling your hair out over it, simplexml can't seem to correctly parse XML comment tags if the comment contains "--".

Is silly and likely won't happen very often -- but sometimes it does. ;-)
jeff at creabilis dot com 17-Nov-2009 11:28
If you want to set the charset of the outputed xml, simply set the encoding attribute like this :

<?php simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><xml/>'); ?>

The generated xml outputed by $xml->asXML will containt accentuated characters like 'e' instead of &#xE9;.

Hope this help
Bob 17-Jun-2009 11:16
Here is my simple SimpleXML wrapper function.
As far as I can tell, it does the same as Julio Cesar Oliveira's (above).
It parses an XML string into a multi-dimensional associative array.
The second argument is a callback that is run on all data (so for example, if you want all data trimmed, like Julio does in his function, just pass 'trim' as the second arg).
<?php
function unserialize_xml($input, $callback = null, $recurse = false)
/* bool/array unserialize_xml ( string $input [ , callback $callback ] )
 * Unserializes an XML string, returning a multi-dimensional associative array, optionally runs a callback on all non-array data
 * Returns false on all failure
 * Notes:
    * Root XML tags are stripped
    * Due to its recursive nature, unserialize_xml() will also support SimpleXMLElement objects and arrays as input
    * Uses simplexml_load_string() for XML parsing, see SimpleXML documentation for more info
 */
{
    // Get input, loading an xml string with simplexml if its the top level of recursion
    $data = ((!$recurse) && is_string($input))? simplexml_load_string($input): $input;
    // Convert SimpleXMLElements to array
    if ($data instanceof SimpleXMLElement) $data = (array) $data;
    // Recurse into arrays
    if (is_array($data)) foreach ($data as &$item) $item = unserialize_xml($item, $callback, true);
    // Run callback and return
    return (!is_array($data) && is_callable($callback))? call_user_func($callback, $data): $data;
}
?>
tiznull 07-Apr-2009 07:16
SimpleXMLElement - Warning: unserialize() [function.unserialize]: Node no longer exists in ... .php

If you get this error from storing serialized SimpleXMLElement data then this is your fix...

<?php

function sxml_unserialze($str) {
return unserialize(str_replace(array('O:16:"SimpleXMLElement":0:{}', 'O:16:"SimpleXMLElement":'), array('s:0:"";', 'O:8:"stdClass":'), $str));
}

?>
Julio Cesar Oliveira 30-Mar-2009 04:14
The XML2Array func now Recursive!

<?php
function XML2Array ( $xml , $recursive = false )
{
    if ( ! $recursive )
    {
        $array = simplexml_load_string ( $xml ) ;
    }
    else
    {
        $array = $xml ;
    }

    $newArray = array () ;
    $array = ( array ) $array ;
    foreach ( $array as $key => $value )
    {
        $value = ( array ) $value ;
        if ( isset ( $value [ 0 ] ) )
        {
            $newArray [ $key ] = trim ( $value [ 0 ] ) ;
        }
        else
        {
            $newArray [ $key ] = XML2Array ( $value , true ) ;
        }
    }
    return $newArray ;
}
?>
Julio Cesar Oliveira 26-Mar-2009 05:57
<?php
function XML2Array ( $xml )
{
    $array = simplexml_load_string ( $xml );
    $newArray = array ( ) ;
    $array = ( array ) $array ;
    foreach ( $array as $key => $value )
    {
        $value = ( array ) $value ;
        $newArray [ $key] = $value [ 0 ] ;
    }
    $newArray = array_map("trim", $newArray);
  return $newArray ;
}
?>
supzero at phparts dot net 07-Mar-2009 07:04
if we don't know children number. How many loop. How many key.

<?php
class XML {
    protected $pointer;
    public    $degerler=array();

    function loadString($string){
        $this->pointer = simplexml_load_string($string);
                return $this->pointer;
    }

    function loadFile($file){
        $this->pointer = simplexml_load_file($file);
        return $this->pointer;
    }

    function getname(){
        return $this->pointer->getName();
    }
    function child(){
        return $this->pointer->children();
    }
    function att(){
        return $this->pointer->attributes();
    }
    function toArray(){
        foreach ($this->child() as $sq){
            $this->degerler[$this->getname()][$sq->getname()][][] = $sq; // How many key
        }
        return;
    }

}
?>
Mark Omohundro, ajamyajax.com 04-Nov-2008 07:33
How about a recursive function to reduce the xml hard-coding in your apps?  Here is my simple listing routine as an example:

<?php
function list_xml($str) {
  $root = simplexml_load_string($str);
  list_node($root);
}

function list_node($node) {
  foreach ($node as $element) {
    echo $element. "\n";
    if ($element->children()) {
      echo "<br/>";
      list_node($element);
    }
  }
}
?>
javalc6 at gmail dot com 18-Oct-2008 03:25
I wanted to convert an array containing strings and other arrays of the same type into a simplexml object.

Here is the code of the function array2xml that I've developed to perform this conversion. Please note that this code is simple without any checks.

<?php
function array2xml($array, $tag) {

    function ia2xml($array) {
        $xml="";
        foreach ($array as $key=>$value) {
            if (is_array($value)) {
                $xml.="<$key>".ia2xml($value)."</$key>";
            } else {
                $xml.="<$key>".$value."</$key>";
            }
        }
        return $xml;
    }

    return simplexml_load_string("<$tag>".ia2xml($array)."</$tag>");
}

$test['type']='lunch';
$test['time']='12:30';
$test['menu']=array('entree'=>'salad', 'maincourse'=>'steak');

echo array2xml($test,"meal")->asXML();
?>
rowan dot collins at gmail dot com 10-Jul-2008 04:07
There seems to be a lot of talk about SimpleXML having a "problem" with CDATA, and writing functions to rip it out, etc. I thought so too, at first, but it's actually behaving just fine under PHP 5.2.6

The key is noted above example #6 here:
http://uk2.php.net/manual/en/simplexml.examples.php

"To compare an element or attribute with a string or pass it into a function that requires a string, you must cast it to a string using (string). Otherwise, PHP treats the element as an object."

If a tag contains CDATA, SimpleXML remembers that fact, by representing it separately from the string content of the element. So some functions, including print_r(), might not show what you expect. But if you explicitly cast to a string, you get the whole content.

<?php
$xml = simplexml_load_string('<foo>Text1 &amp; XML entities</foo>');
print_r($xml);
/*
SimpleXMLElement Object
(
    [0] => Text1 & XML entities
)
*/

$xml2 = simplexml_load_string('<foo><![CDATA[Text2 & raw data]]></foo>');
print_r($xml2);
/*
SimpleXMLElement Object
(
)
*/
// Where's my CDATA?

// Let's try explicit casts
print_r( (string)$xml );
print_r( (string)$xml2 );
/*
Text1 & XML entities
Text2 & raw data
*/
// Much better
?>
SmartD 11-Jun-2008 05:40
A small 'n nice function to extract an XML and return it as an array. If there is a bug, please let me know here. I testet it for my purposes and it works.

<?php

public function extractXML($xml) {

if (!($xml->children())) {
    return (string) $xml;
}

foreach ($xml->children() as $child) {
    $name=$child->getName();
    if (count($xml->$name)==1) {
        $element[$name] = $this->extractXML($child);
    } else {
        $element[][$name] = $this->extractXML($child);
    }
}

return $element;
}

// you can call it this way

$xml = false;

$xml = @simplexml_load_string($xmlstring);

// 1)

if ($xml) {
  $array = extractXML($xml);
}  else {
  $array = false;
}

// 2)

if ($xml) {
  $array[$xml->getName()] = extractXML($xml);
}  else {
  $array = false;
}

?>
nospam at qool dot com 05-Jun-2008 08:45
simplexml doesn't appear to like long attributes. I have tried passing it a valid xhtml document but the url in the anchor tag was causing simplexml to generate an error.
kyle at codeincarnate dot com 22-Apr-2008 02:28
A looked through a lot of the sample code for reading XML files with CDATA, but they didn't work out that well for me.  However, I found that the following piece of code worked perfectly for reading through a file using lots of CDATA.

<?php
$article_string = file_get_contents($path);
$article_string = preg_replace_callback('/<!\[CDATA\[(.*)\]\]>/', 'filter_xml', $article_string);
$article_xml = simplexml_load_string($article_string);

function filter_xml($matches) {
    return trim(htmlspecialchars($matches[1]));
}

?>
paulyg76 at NOSPAM dot gmail dot com 19-Feb-2008 03:39
Be careful using nested SimpleXML objects in double quoted strings.

<?php
$xmlstring = '<root><node>123</node><foo><bar>456</bar></foo></root>';

$root = simplexml_load_string($xmlstring);

echo "Node is: $root->node"; // Works: Node is 123
echo "Bar is: $root->foo->bar"; // Doesn't work, outputs: Bar is: ->bar
// use curly brackets to fix
echo "Bar is: {$root->foo->bar}"; // Works: Bar is 456

?>
amir_abiri at ipcmedia dot com 08-Feb-2008 05:47
It doesn't seem to be documented anywhere, but you can refer to an element "value" for the purpose of changing it like so:

<?php
$xml = simplexml_load_string('<root><number>1</number></root>');
echo $xml->asXml(). "\n\n";

$xml->number->{0} = $xml->number->{0} + 1;

echo $xml->asXml();
?>

echos:
<?xml version="1.0"?>
<root><number>1</number></root>

<?xml version="1.0"?>
<root><number>2</number></root>

However, this only works with a direct assignment, not with any of the other operators:

<?php
$xml = simplexml_load_string('<root><number>1</number></root>');
echo $xml->asXml(). "\n\n";

$xml->number->{0} += 1;
// Or:
$xml->number->{0}++;

echo $xml->asXml();
?>

Both of the above cases would result in:

<?xml version="1.0"?>
<root><number>1</number></root>

<?xml version="1.0"?>
<root><number>1<0/></number></root>
bojan 04-Feb-2008 01:38
As was said before don't use var_dump() or print_r() to see SimpleXML object structure as they do not returns always what you expect.
Consider the following:

<?php

// data in xml
$xml_txt = '
<root>
  <folder ID="65" active="1" permission="1"><![CDATA[aaaa]]></folder>
  <folder ID="65" active="1" permission="1"><![CDATA[bbbb]]></folder>
</root>';

// load xml into SimpleXML object
$xml = simplexml_load_string($xml_txt, 'SimpleXMLElement', LIBXML_NOCDATA);//LIBXML_NOCDATA LIBXML_NOWARNING

// see object structure
print_r($xml);

/* this prints
SimpleXMLElement Object
(
    [folder] => Array
        (
            [0] => aaaa
            [1] => bbbb
        )

)
*/

// but...
foreach ($xml->folder as $value){
    print_r($value);
}
/* prints complete structure of each folder element:
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [ID] => 65
            [active] => 1
            [permission] => 1
        )

    [0] => aaaa
)

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [ID] => 65
            [active] => 1
            [permission] => 1
        )

    [0] => bbbb
)

*/
?>
hattori at hanso dot com 16-Oct-2007 12:19
Theres a problem with the below workaround when serializing fields containing html CDATA. For any other content type then HTML try to modfiy function parseCDATA.
Just add these lines before serializing.
This is also a workaround for this bug http://bugs.php.net/bug.php?id=42001

<?PHP
if(strpos($content, '<![CDATA[')) {
   function parseCDATA($data) {
      return htmlentities($data[1]);
   }
   $content = preg_replace_callback(
      '#<!\[CDATA\[(.*)\]\]>#',
      'parseCDATA',
      str_replace("\n", " ", $content)
   );
}
?>
hattori at hanso dot com 17-Sep-2007 11:15
If you want to serialize and unserialize SimpleXMLElement objects for caching, you need to transform the SimpleXMLElement object into a standard class object before unserializing.
This is only if you want to cache converted data, the functionallity of the SimpleXMLElement will not be held.

$content = '<SomeXML....'

$serialized = str_replace(
  array('O:16:"SimpleXMLElement":0:{}', 'O:16:"SimpleXMLElement":'),
  array('s:0:"";', 'O:8:"stdClass":'),
  serialize(simplexml_load_string($content))
);

$unserialized = unserialize($serialized);
nbijnens at servs dot eu 10-Sep-2007 02:17
Please note that not all LIBXML options are supported with the options argument.

For instance LIBXML_XINCLUDE does not work. But there is however a work around:

<?php
$xml = new DOMDocument();
$xml->loadXML ($XMLString);

$xml->xinclude();
$xml = simplexml_import_dom($xml);

?>
Pedro 06-Jul-2007 10:28
Attention:

simplexml_load_string has a problem with entities other than (&lt;, &gt;, &amp;, &quot; and &apos;).

Use numeric character references instead!
youx_free_fr 23-May-2007 06:52
While needing to add an xml subtree to an existing simplexml object, I noticed that simplexml_load_string fails with strings like
<emptynode></emptynode>

I needed to use dom instead of simplexml to bypass this problem and work with any kind of xml strings.
php at teamhirsch dot com 24-Apr-2007 11:46
It's worth noting that in the example above, $xml->body will actually return an object of type SimpleXMLElement, not a string, e.g.

 SimpleXMLElement Object (
       [0] => this is the text in the body tag
  )

If you want to get a string out of it you must explicitly cast it using (string) or double quotes, or pass $xml->body (or whatever attribute you want to access) to any function that returns a string, such as urldecode() or trim().
m dot ament at mailcity dot com 06-Mar-2007 03:51
Warning:

The parsing of XML-data will stop when reaching character 0.
Please avoid this character in your XML-data.
mindpower 30-Jan-2007 12:03
A simple extension that adds a method for retrieving a specific attribute:

<?php
class simple_xml_extended extends SimpleXMLElement
{
    public    function    Attribute($name)
    {
        foreach($this->Attributes() as $key=>$val)
        {
            if($key == $name)
                return (string)$val;
        }
    }

}

$xml = simplexml_load_string('
<xml>
  <dog type="poodle" owner="Mrs Smith">Rover</dog>
</xml>', 'simple_xml_extended');

echo $xml->dog->Attribute('type');

?>

outputs 'poodle'

I prefer to use this technique rather than typecasting attributes.
h 15-Nov-2006 07:44
seems like simplexml has a line-length restriction - fails if a largeish XML doc with no linebreaks is passed as a string or file.

h
roy dot walter at nospam dot brookhouse dot co dot uk 16-Oct-2006 06:15
simplexml provides a neat way to do 'ini' files. Preferences for any number of users can be held in a single XML file having elements for each user name with user specific preferences as attributes of child elements. The separate <pref/>'s could of course be combined as multiple attributes of a single <pref/> element but this could get unwieldy.

In the sample code below the makeXML() function uses the simplexml_load_string function to generate some XML to play with and the readPrefs() function parses the requested users preferences into an array.

<?php
function makeXML() {

$xmlString = <<<XML
<preferences>
    <johndoe>
        <pref color="#FFFFFF"/>
        <pref size="14"/>
        <pref font="Verdana"/>
    </johndoe>
    <janedoe>
        <pref color="#000000"/>
        <pref size="16"/>
        <pref font="Georgia"/>
    </janedoe>
</preferences>
XML;

return simplexml_load_string($xmlString);

}
function readPrefs($user, $xml) {

    foreach($xml->$user as $arr);
        $n = count($arr);

    for($i=0;$i<$n;$i++) {
        foreach($xml->$user->pref[$i]->attributes() as $a=>$b) {
            $prefs[$a] = (string)$b;
        }
    }

    print_r($prefs);
}

readPrefs('johndoe', makeXML());

?>
paul at santasoft dot com 25-Jul-2006 05:22
If you have PHP > 5.1 and LibXML > 2.6, use this function call to have simplexml convert CDATA into plain text.

simplexml_load_string($xmlstring, 'SimpleXMLElement', LIBXML_NOCDATA);

Too bad, so sad with PHP < 5.1.
lists at cyberlot dot net 07-Sep-2005 09:51
While you can't add new elements to a SimpleXML object you can however add new attributes
<?php
$string = '<doc channel="chat"><test1>Hello</test1></doc>';
$xml = simplexml_load_string($string);
$xml->test1['sub'] = 'No';
echo $xml->asXML();
?>

Will return output
<doc channel="chat"><test1 sub="No">Hello</test1></doc>
Maciek Ruckgaber <maciekrb at gmai dot com> 08-Jun-2005 02:07
after wondering around some time, i just realized something (maybe obvious, not very much for me). Hope helps someone to not waste time as i did :-P

when you have something like:

<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://foosite.foo/">2328</double>
XML;
?>

you will have the simpleXML object "transformed" to the text() content:

<?php
$xml = simplexml_load_string($xmlstr);
echo $xml; // this will echo 2328  (string)
?>
igor kraus 07-Jan-2005 07:37
A simple way to merge two SimpleXML objects.

<?php
/**
* Pumps all child elements of second SimpleXML object into first one.
*
* @param    object      $xml1   SimpleXML object
* @param    object      $xml2   SimpleXML object
* @return   void
*/
function simplexml_merge (SimpleXMLElement &$xml1, SimpleXMLElement $xml2)
{
    // convert SimpleXML objects into DOM ones
    $dom1 = new DomDocument();
    $dom2 = new DomDocument();
    $dom1->loadXML($xml1->asXML());
    $dom2->loadXML($xml2->asXML());

    // pull all child elements of second XML
    $xpath = new domXPath($dom2);
    $xpathQuery = $xpath->query('/*/*');
    for ($i = 0; $i < $xpathQuery->length; $i++)
    {
        // and pump them into first one
        $dom1->documentElement->appendChild(
            $dom1->importNode($xpathQuery->item($i), true));
    }
    $xml1 = simplexml_import_dom($dom1);
}

$xml1 = simplexml_load_string('<root><child>child 1</child></root>');
$xml2 = simplexml_load_string('<root><child>child 2</child></root>');
simplexml_merge($xml1, $xml2);
echo($xml1->asXml());
?>

Will output:
<?xml version="1.0"?>
<root>
    <child>child 1</child>
    <child>child 2</child>
</root>
cellog at php dot net 01-Sep-2004 01:11
simplexml does not simply handle CDATA sections in a foreach loop.

<?php
$sx = simplexml_load_string('
<test>
 <one>hi</one>
 <two><![CDATA[stuff]]></two>
 <t>
  <for>two</for>
 </t>
 <multi>one</multi>
 <multi>two</multi>
</test>');
foreach((array) $sx as $tagname => $val) {
    if (is_string($val)) {
       // <one> will go here
    } elseif (is_array($val)) {
       // <multi> will go here because it happens multiple times
    } elseif (is_object($val)) {
      // <t> will go here because it contains tags
      // <two> will go here because it contains CDATA!
    }
}
?>

To test in the loop, do this

<?php
if (count((array) $val) == 0) {
    // this is not a tag that contains other tags
    $val = '' . $val;
    // now the CDATA is revealed magically.
}
?>
greg dot steffensen at spamless dot richmond dot edu 20-Feb-2004 12:04
Simplexml's simplicity can be deceptive.  Simplexml elements behave either as objects or strings, depending on the context in which they're used (through overloading of the __toString() method, I assume).  Statements implying conversion to string treat them as strings, while assignment operations treat them as objects.  This can lead to unexpected behavior if, for example, you are trying to compare the values of two Simplexml elements.  The expected syntax will not work.  To force conversion to strings, just "typecast' whatever Simplexml element you're using.  For example:

<?php
$s = simplexml_load_string('<foo>43</foo> <bar>43</bar>');

// Evaluates to false by comparing object IDs instead of strings
($s->foo == $s->bar);

// Evaluates to true
((string)$s->foo == (string)$s->bar);
?>

[Ed. Note: Changed from quotes to casts because casts provide a quicker and more explicit conversion than do double quotes.]


WDDX

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
    • wddx examples
  • WDDX 함수 목록
    • wddx_add_vars — Add variables to a WDDX packet with the specified ID
    • wddx_deserialize — Unserializes a WDDX packet
    • wddx_packet_end — Ends a WDDX packet with the specified ID
    • wddx_packet_start — Starts a new WDDX packet with structure inside it
    • wddx_serialize_value — Serialize a single value into a WDDX packet
    • wddx_serialize_vars — Serialize variables into a WDDX packet

User Contributed Notes

There are no user contributed notes for this page.


소개

These functions are intended for work with » WDDX.

User Contributed Notes

alvaro at demogracia dot com 29-Sep-2008 05:49
From OpenWDDX.Org:

"The Web Distributed Data Exchange, or WDDX, is a free, open XML-based technology that allows Web applications created with any platform to easily exchange data with one another over the Web."

"WDDX is an XML-based technology that enables the exchange of complex data between Web programming languages, creating what some refer to as 'Web syndicate networks'. WDDX consists of a language-independent representation of data based on an XML 1.0 DTD, and a set of modules for a wide variety of languages that use WDDX. WDDX can be used with HTTP, SMTP, POP, FTP and other Internet protocols that support transferring textual data."


요구 조건

This extension requires the libxml PHP extension. This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default.

In order to use WDDX, you will need to install the expat library (which comes with Apache 1.3.7 or higher).

User Contributed Notes

There are no user contributed notes for this page.


설치

After installing the required expat library, compile PHP with --enable-wddx , and use --with-libexpat-dir for expat.

PHP 윈도우 버전에서는 이 확장에 대한 지원이 포함되어 있습니다. 이 함수들을 이용하기 위해서 추가로 확장을 읽어들일 필요가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


자원형

This extension defines a WDDX packet identifier returned by wddx_packet_start().

User Contributed Notes

There are no user contributed notes for this page.


예제

Table of Contents

  • wddx examples

User Contributed Notes

dormilich at netscape dot net 27-Nov-2008 03:37
If you want to use the WDDX 1.0 DTD for validation (http://www.openwddx.org/downloads/dtd/wddx_dtd_10.txt) check that you define the comment as element, not as attribute.

<?xml version="1.0" encoding="iso-8859-1" ?>
<wddxPacket version='1.0'>
    <header>
        <comment>PHP</comment>
    </header>
    <data><!-- WDDX serialized data --></data>
</wddxPacket>


wddx examples

All the functions that serialize variables use the first element of an array to determine whether the array is to be serialized into an array or structure. If the first element has string key, then it is serialized into a structure, otherwise, into an array.

Example #1 Serializing a single value with WDDX

<?php
echo wddx_serialize_value("PHP to WDDX packet example", "PHP packet");
?>

This example will produce:

<wddxPacket version='1.0'><header comment='PHP packet'/><data>
<string>PHP to WDDX packet example</string></data></wddxPacket>

Example #2 Using incremental packets with WDDX

<?php
$pi = 3.1415926;
$packet_id = wddx_packet_start("PHP");
wddx_add_vars($packet_id, "pi");

/* Suppose $cities came from database */
$cities = array("Austin", "Novato", "Seattle");
wddx_add_vars($packet_id, "cities");

$packet = wddx_packet_end($packet_id);
echo $packet;
?>

This example will produce:

<wddxPacket version='1.0'><header comment='PHP'/><data><struct>
<var name='pi'><number>3.1415926</number></var><var name='cities'>
<array length='3'><string>Austin</string><string>Novato</string>
<string>Seattle</string></array></var></struct></data></wddxPacket>

Note:

If you want to serialize non-ASCII characters you have to convert your data to UTF-8 first (see utf8_encode() and iconv()).

User Contributed Notes

There are no user contributed notes for this page.


WDDX 함수 목록

Table of Contents

  • wddx_add_vars — Add variables to a WDDX packet with the specified ID
  • wddx_deserialize — Unserializes a WDDX packet
  • wddx_packet_end — Ends a WDDX packet with the specified ID
  • wddx_packet_start — Starts a new WDDX packet with structure inside it
  • wddx_serialize_value — Serialize a single value into a WDDX packet
  • wddx_serialize_vars — Serialize variables into a WDDX packet

User Contributed Notes

iGEL 14-Mar-2008 11:03
As of version 5.2.5, this is wrong:

> Note: If you want to serialize non-ASCII characters you
> have to convert your data to UTF-8 first (see utf8_encode()
> and iconv()).

Serializing a string with WDDX will always convert it from ISO-8859-1 to UTF-8, no matter what the original encoding is. So if your data already are UTF-8, you get just gibberish.

You can restore the original encoding by using utf8_decode on the serialized wddx data, even though your original data may use chars not included in ISO-8859-1:

<?php
header("Content-Type: text/xml;encoding=utf-8");
echo utf8_decode(wddx_serialize_value("안녕 하세요"));
?>

(This is why I dislike PHP, sorry)
egbert teeselink 23-Feb-2008 07:49
If your PHP doesn't have wddx support installed, and you need it, and you don't want to install PEAR just for wddx, try this short piece of code, based on SimpleXML:

//can read any wddx file generated from php arrays. assumes var elements are only inside struct elements and that only one packet exists.
function wddx_read_node($node, $type)
{
    switch($type)
    {
    case "boolean":
        return (string)$node;
    case "number":
    case "binary":
    case "string":
        return (string)$node;
    case "null":
        return null;
    case "array":
        $a = array();
        foreach($node as $subtype => $subnode)
        {
            $a[] = wddx_read_node($subnode, $subtype);
        }
        return $a;
    case "struct":
        $a = array();
        foreach($node as $subtype => $subnode)
        {
            list($key, $value) = wddx_read_node($subnode, $subtype); //must be a var element
            $a[$key] = $value;
        }
        return $a;
    case "var":
        list($subnode, $subtype) = getchild($node);
        return array((string)$node["name"], wddx_read_node($subnode, $subtype));
    case "data":
        list($subnode, $subtype) = getchild($node);
        return wddx_read_node($subnode, $subtype);
    }
}

function wddx_read($string)
{
    $xml = simplexml_load_string($string);
    $d = wddx_read_node($xml->data, "data");
    return $d;
}

important notice: this one may not work with all wddx input. multiple packets or variables outside a "struct" are not supported. basically, you always get a PHP (associative) array, never something else. feel free to make additions.
no at spam dot thx 12-Aug-2007 11:09
To bradburn at kiwi dot de:

PHP is only capable of serializing properly variables which match one of its native (scalar) types (See http://php.net/types). Which means that only variables of type booleans, integers, floating point numbers, string and NULL will be serialized properly.

I think there are two exceptions though:
- arrays are serialized by processing them recursively, so if its only composed of the above mentioned types, you should be fine.
- floating point numbers and integers may use the same representation while serialized in WDDX (I don't know much about WDDX, so I'm not 100% sure about this statement).

An interesting case would be whether objects can be serialized or not...
Jimmy Wimenta 16-Jul-2004 02:53
PHP's WDDX is useful only for exchanging data between PHP applications, but definetly not for exchanging data between different languages (which actually defeats the purpose of WDDX).

For example:

$hash1 = array ("2" => "Two", "4" => "Four", "5" => "Five");
$hash2 = array ("0" => "Zero", "1" => "One", "2" => "Two");

$hash1 will be serialized as hash, but
$hash2 will be serialized as array/list, because the key happen to be a sequence starting from 0.

Unless the library provide a way for users to specify the type, it can never be used for cross-platform data exchange.
Q1tum at hotmail dot com 21-Nov-2003 02:08
To insert arrays into a wddx variable here is a fine way to do it:

<?php

$sql = 'SELECT * FROM example';
$query = mysql_query($sql, $db) or die(mysql_error());

while($result = mysql_fetch_array($query)) {
    $id[] = $result[ 'id'];
    $name[] = $result['name'];
    $description[] = $result[$prefix . 'description'];
}

mysql_free_result($query);

wddx_add_vars($packet_id, "id");
wddx_add_vars($packet_id, "name");
wddx_add_vars($packet_id, "description");

$wddxSerializeValue = wddx_packet_end($packet_id);

?>
bradburn at kiwi dot de 30-Jul-2002 04:02
With ref to the above comment about typing, I have found that -- oddly enough -- PHP's WDDX supports the following WDDX types: null, boolean (true/false), number and string, *but* not date-time.

as an example, use the following values in an array that you then serialize:

$number = 5,
$null = NULL,
$bool = true,
$string = 'this is a string'.

they will all serialize correctly, e.g. the third entry comes out as:

<var name='bool'><boolean value='true'/></var>

i have tried with the 'official' format for WDDX 'datetime', e.g. '1998-9-15T09:05:32+4:0' (from the DTD @ http://www.openwddx.org/downloads/dtd/wddx_dtd_10.txt)  but have only succeeded in getting this encoded as a 'string' type.

if anyone else has any more information on this, it would be welcome. i would like to store the variables in 'appropriate' fields in a database, and the fact that only datetime is not supported is slightly irritating -- otherwise it would be a very useful function.
djm at web dot us dot uu dot net 19-Oct-1999 05:45
Since there aren't any examples of reversing the process, here's one. If you had the packet produced by the above example (without the htmlentities() call), you could retrieve the values like this:

<?php
$value = wddx_deserialize($packet);
print "pi is:<br>" . $value["pi"] . "<p>\n";
print "cities is:<br>\n";
while (list($key, $val) = each($value["cities"])) {
    print "$key => $val<br>\n";
}
?>

which outputs:

<pre>
pi is:
3.1415926

cities is:
0 => Austin
1 => Novato
2 => Seattle
</pre>


Add variables to a WDDX packet with the specified ID

  • ≪ WDDX 함수 목록
  • wddx_deserialize ≫
  • PHP Manual
  • WDDX 함수 목록
  • Add variables to a WDDX packet with the specified ID

wddx_add_vars

(PHP 4, PHP 5, PHP 7)

wddx_add_vars — Add variables to a WDDX packet with the specified ID

설명

bool wddx_add_vars ( resource $packet_id , mixed $var_name [, mixed $... ] )

Serializes the passed variables and add the result to the given packet.

인수

This function takes a variable number of parameters.

packet_id

A WDDX packet, returned by wddx_packet_start().

var_name

Can be either a string naming a variable or an array containing strings naming the variables or another array, etc.

...

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

maciek at webenlinea dot com 05-Sep-2004 08:20
Here is a simple way of serializing dynamic values from an array:

<?PHP
//a unix timestamp
$date    = "1094095513";

//some data to be included
$books   = array('programming'   => array('php','perl','java'),
                'markup'        => array('UML','XML','HTML')
                );

//stick data to an array to iterate over
$data_to_serialize = array($date,$books);

//create the packet
$packet = wddx_packet_start("SOME DATA ARRAY");

//loop through the data
foreach($data_to_serialize as $key => $data)
{
  //create a var whith the name of the content of $key
  $$key = $data;
  wddx_add_vars($packet,$key);
}

echo wddx_packet_end($packet);

?>

Have fun !


Unserializes a WDDX packet

  • ≪ wddx_add_vars
  • wddx_packet_end ≫
  • PHP Manual
  • WDDX 함수 목록
  • Unserializes a WDDX packet

wddx_deserialize

(PHP 4, PHP 5, PHP 7)

wddx_deserialize — Unserializes a WDDX packet

설명

mixed wddx_deserialize ( string $packet )

Unserializes a WDDX packet.

인수

packet

A WDDX packet, as a string or stream.

반환값

Returns the deserialized value which can be a string, a number or an array. Note that structures are deserialized into associative arrays.

주의

Warning

Do not pass untrusted user input to wddx_deserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this.

User Contributed Notes

valer underscore crisan at yahoo 01-Apr-2009 11:03
In PHP 5.2.9, we noticed that wddx serialize and deserialize seems to use UTF-8 encoding for XML input and the output data. There are some posts below that talk about fixing the input XML data so deserialize would work fine on special chars in ISO-8859-1 encoding, but none of the posts talks about the output encoding.

Consider this test:
$text = "Jur";
$packet = wddx_serialize_value($text);
$header = '<?xml version="1.0" encoding="iso-8859-1"?>';
$newText = wddx_deserialize($header . $packet);
echo "WDDX Packet: $packet\n";
echo "Deserialized Object: $newText\n";

The output is:

WDDX Packet: <wddxPacket version='1.0'><header/><data><string>Jur</string></data></wddxPacket>
Deserialized Object: JA¼r

The deserialized string is not in ISO-8859-1 encoding. It is in UTF-8 encoding. So we need to run utf8_decode() on the deserialized data to convert it back to ISO-8859-1 encoding which is what our application expects.

Hope this helps.
Magnus Deininger, dma05 at web dot de 12-Feb-2009 02:57
When writing your wddx file manually with an UTF-8 aware editor and saving it in utf-8, if your data gets its special characters mysteriously scrambled, try to add an xml header that marks the output as iso-8859-1, like this one:

<?xml version="1.1" encoding="iso-8859-1" ?>

This makes the wddx decode function treat the input as iso-8859-1, so it will not try to treat it as utf-8 and do an implicit decode to iso-8859-1. You will then have read all string data in the wddx packet in their original utf-8 encoding, so that 'echo' and other output functions will produce the intended result if you have set the output encoding to utf-8.

(Bugs reports on this behaviour seem to be treated as bogus, so it would seem in order to point out this incorrect and highly confusing side-effect.)
dormilich at netscape dot net 28-Nov-2008 02:13
When deserializing objects make sure you have the class definition loaded. wddx_deserialize() doesn't call the class itself, so you will receive a fatal error.
Nevertheless you can look for the class manually and delegate it to __autoload().

<?php
// $wddx_string needs to be valid XML to be loaded by SimpleXML.
// class_exists() will call the __autoload() function. if you don't
// want to use __autoloload(), use require_once()

function loadClassesFromWDDX($wddx_string)
{
    $xml = new SimpleXMLElement($wddx_string);

    foreach ($xml->xpath('//var') as $var)
    {
        if ($var['name'] == 'php_class_name')
        {
            if (!class_exists($var->string))
            {
                throw new Exception('Class '" . $var->string . "'not available.');
                // trigger_error('Class '" . $var->string . "'not available.', E_USER_ERROR);
            }
        }
    }
}
?>
dave at codexweb dot co dot za 21-Jan-2006 01:40
Here's a handy wddx_deserialize clone I wrote a while back. It uses PHP5 SimpleXML and recursion to do pretty much the same task as wddx_deserialize. Hope it comes in handy for someone.

<?php

if (!function_exists('wddx_deserialize'))
{
    /**
     * Clone implementation of wddx_deserialize
     */
    function wddx_deserialize($xmlpacket)
    {
        if ($xmlpacket instanceof SimpleXMLElement)
        {
            if (!empty($xmlpacket->struct))
            {
                $struct = array();
                foreach ($xmlpacket->xpath("struct/var") as $var)
                {
                    if (!empty($var["name"]))
                    {
                        $key = (string) $var["name"];
                        $struct[$key] = wddx_deserialize($var);
                    }
                }
                return $struct;
            }
            else if (!empty($xmlpacket->array))
            {
                $array = array();
                foreach ($xmlpacket->xpath("array/*") as $var)
                {
                    array_push($array, wddx_deserialize($var));
                }
                return $array;
            }
            else if (!empty($xmlpacket->string))
            {
                return (string) $xmlpacket->string;
            }
            else if (!empty($xmlpacket->number))
            {
                return (int) $xmlpacket->number;
            }
            else
            {
                if (is_numeric((string) $xmlpacket))
                {
                    return (int) $xmlpacket;
                }
                else
                {
                    return (string) $xmlpacket;
                }
            }
        }
        else
        {
            $sxe = simplexml_load_string($xmlpacket);
            $datanode = $sxe->xpath("/wddxPacket[@version='1.0']/data");
            return wddx_deserialize($datanode[0]);
        }
    }
}

?>
php dot net at werner-ott dot de 24-Nov-2005 09:20
On migrating wddx_deserialize() from PHP 4.x to PHP 5.1 (5.1.0RC6):

While

  $buffer = wddx_serialize_vars($some_array);
  $some_array = wddx_deserialize($buffer);

worked fine with PHP 4.x, the deserialization failed with PHP 5.1. In the above example $some_array will just be an empty string under 5.1

While wddx_serialize_vars() seems to behave identical in 4.x and 5.1, wddx_deserialize() does NOT.

Prepending XML encoding information to the buffer turned out to be at least a workaround. So, the following works with PHP 5.1:

  $buffer = wddx_serialize_vars($some_array);
  $buffer = '<?xml version="1.0"
          encoding="ISO-8859-1"?>'.
          $buffer;
  $some_array = wddx_deserialize($buffer);

NB: It may well be, that the behavioural difference between 4.x and 5.1 described above can only be observed if the array contains certain characters, i.e. german Umlaute (??????). So this workaround may not be necessary in each and every case.
mail_fidel at yahoo dot com 19-Nov-2004 09:11
To properly deserialize a wddx file it was to be utf8_encoded.

The following code will NOT work (PHP5.0.2 FreeBSD):

<?php
   $data = wddx_serialize_value(array("xyz???","abcd"));
   print $data;
   print_r(wddx_deserialize($data));

?>

Even thought the serialization is done right, the deserialization will not work unless you encode the text using utf8.

This code WILL work:

<?php
   $data = wddx_serialize_value(array(utf8_encode("xyz???"),"abcd"));
   print $data;
   print_r(wddx_deserialize($data));

?>
xavier at c-est-simple dot com 19-May-2004 01:09
JavaScript/PHP interroperability:

If you want to exchange wddx packets between a JavaScript application using the JS wddx library from openwddx (http://www.openwddx.org/downloads/), and a PHP script using wddx functions, be aware that the encoding of the classes of objects serialized in the wddx packet is not made the same way in both implementations.

The JS lib uses a 'type' attribute in the 'struct' tab and gives it the name of the class of the object serialized in the 'struct' element, while PHP uses a 'php_class_name' tag within the 'struct'.
NB : to get the type attribute with JS serialisation, you need to set the 'wddxSerializationType' member in your classes

Example with an object of class 'exemple' containing 2 members _attr1 and _attr2.
(NB : in french, 'example' is spelled 'exemple', okay ? :-)

JS serialization :
<wddxPacket version='1.0'>
 <header/>
 <data>
  <struct type="exemple">
   <var name='_attr1'>
    <string>foo</string>
   </var>
   <var name='_attr2'>
    <string>bar</string>
   </var>
  </struct>
 </data>
</wddxPacket>

php serialization:
<wddxPacket version='1.0'>
 <header/>
 <data>
  <struct>
   <var name='php_class_name'>
    <string>exemple</string>
   </var>
   <var name='_attr1'>
    <string>foo</string>
   </var>
   <var name='_attr2'>
    <string>bar</string>
   </var>
  </struct>
 </data>
</wddxPacket>

I like the JS approach better.
So if you need the compatibility, the workaround is quite simple:
- add a 'php_class_name' member in your JS objects, giving it the name of the class for value, and the JS generated packet will be correctly deserialized with PHP in classed objects.

- use a regexp on a PHP generated packet to add the 'type' attribute so that the JS deserialization instanciates objects of the right class:
      $pat = "/<struct><var name='php_class_name'><string>(\w+)<\/string>/" ;
      $rep = "<struct type='\$1'><var name='php_class_name'><string>\$1</string>" ;
      $serialised = preg_replace($pat, $rep, $serialised );
NB : this only works because the 'php_class_name' tag is just next to the 'struct' tag.
Who knows if this is going to be true in future releases... ?

This way, you can exchange JS and PHP objects (provided you define your JS and PHP classes the same way) smoothly...

It's worth saying that both implementations, as well as my interroperability workaround, handle correctly objects with members that are also objects.

NB : NB means Nota Bene.
xavier at c-est-simple.com 18-May-2004 07:19
Beware, although the deserialized objects are instanciated class objects (if the php_class_name tag is amongst the children of the struct tag and its text content is the name of a loaded class), the constructor of the class has not been called at all.
You can use the members and call the methods of the object all right, but any initialisation action that the constructor might be in charge of is missing.
php at mike2k dot com 11-Sep-2001 03:06
if you have WDDX data coming from a third party source and it isnt being parsed correctly in your php, try translating their WDDX. we have a third party sending us WDDX from cold fusion, and it uses a <recordset> element, which PHP seems to ignore (looking around the web we found some other people having trouble) so the (hopefully temporary) solution we found was to translate the WDDX midstream and then we were able to parse it:

$url = "http://somedomain.com/blah.wddx";
$fp = fopen($url,"r");
$page = fread($fp,10000);
fclose($fp);
$page = str_replace("<recordset","<struct",$page);
$page = str_replace("</recordset","</struct",$page);
$page = str_replace("<field","<var",$page);
$page = str_replace("</field","</var",$page);
$values = wddx_deserialize($page);
while(list($key,$val) = each($values)) {
        print "$key => $val<BR>";
}

this works for us. it's a hack, and hopefully php will support this in the future or something.
medvitz at medvitz dot net 15-Oct-2000 02:29
You can also serialize instantiated class objects with the wddx functions.
ie.

<?php
$packet_id = wddx_packet_start("PHP");
wddx_add_vars($packet_id, "objvar");
$packet = wddx_packet_end($packet_id);

$a =  wddx_deserialize($packet);
$obj = $a[objvar];
?>

now $obj is a useable object, that looks like the original.

Note that with this method, you need to know what the variable name of the original object was.  You can use one of the array functions to get the object in the event that the original object's variable name was unknown.

*** only verified on linux with 4.0.2
djm at web dot us dot uu dot net 19-Oct-1999 06:58
The packet string you pass to this function doesn't have to be sanitized first; it searches for a WDDX packet within the string.  So the string can contain HTML markup and random text outside of the packet, and that is all ignored.
This means you can get a packet from another web page like this:
<?php
$fhandle = fopen("http://myserver.com/packet.php", "r");
if ($fhandle) {
        $page = fread($fhandle,10000);
        fclose($fhandle);
        $value = wddx_deserialize($page);
        print "pi is:<br>" . $value["pi"] . "<p>\n";
} else {
        print "could not get packet.php<p>";
}
?>


Ends a WDDX packet with the specified ID

  • ≪ wddx_deserialize
  • wddx_packet_start ≫
  • PHP Manual
  • WDDX 함수 목록
  • Ends a WDDX packet with the specified ID

wddx_packet_end

(PHP 4, PHP 5, PHP 7)

wddx_packet_end — Ends a WDDX packet with the specified ID

설명

string wddx_packet_end ( resource $packet_id )

Ends and returns the given WDDX packet.

인수

packet_id

A WDDX packet, returned by wddx_packet_start().

반환값

Returns the string containing the WDDX packet.

User Contributed Notes

There are no user contributed notes for this page.


Starts a new WDDX packet with structure inside it

  • ≪ wddx_packet_end
  • wddx_serialize_value ≫
  • PHP Manual
  • WDDX 함수 목록
  • Starts a new WDDX packet with structure inside it

wddx_packet_start

(PHP 4, PHP 5, PHP 7)

wddx_packet_start — Starts a new WDDX packet with structure inside it

설명

resource wddx_packet_start ([ string $comment ] )

Start a new WDDX packet for incremental addition of variables. It automatically creates a structure definition inside the packet to contain the variables.

인수

comment

An optional comment string.

반환값

Returns a packet ID for use in later functions, or FALSE on error.

User Contributed Notes

There are no user contributed notes for this page.


Serialize a single value into a WDDX packet

  • ≪ wddx_packet_start
  • wddx_serialize_vars ≫
  • PHP Manual
  • WDDX 함수 목록
  • Serialize a single value into a WDDX packet

wddx_serialize_value

(PHP 4, PHP 5, PHP 7)

wddx_serialize_value — Serialize a single value into a WDDX packet

설명

string wddx_serialize_value ( mixed $var [, string $comment ] )

Creates a WDDX packet from a single given value.

인수

var

The value to be serialized

comment

An optional comment string that appears in the packet header.

반환값

Returns the WDDX packet, or FALSE on error.

User Contributed Notes

chris at digitalbase dot com 05-Jan-2002 01:36
When serializing JavaScript objects to interact with PHP objects, you should make the JavaScript object's first entry a php_class_name.

PHP:
class myclass {
    var $name;
    var $age;
    var $ssn;
    ...

    function myclass() {
        $this->name = "";
        $this->age = "";
        $this->ssn = "";
        ...
    }
}

JavaScript:
// serialization example code is from the
// WDDX SDK http://www.openwddx.org
function SerializeMsg(name, age, ssn, ...) {
    var MyObj = new Object;

    // the following line is necessary in order
    // to deserialize back to a PHP object
    MyObj.php_class_name = "myclass";

    MyObj.name = name;
    MyObj.age = age;
    MyObj.ssn = ssn;
    ...

    // Create a new serializer "object" to do our work for us
    MySer = new WddxSerializer;

    // Serialize the Message variable into a WDDX Packet
    MyWDDXPacket = MySer.serialize(MyObj);

    // Output WDDX Packet so we can see what it looks like
    return MyWDDXPacket;
}

Now you can use this in a page which is its own form action:
<?php
// don't forget to include the class definition from above!
if (!isSet($wddx_packet))
    $MyObj = new myclass();
else
    $MyObj = wddx_deserialize($wddx_packet);
?>

<html>
<head>
<script language="JavaScript">
<!--
function Init() {
    // sets the form's text-elements
    tmp = document.MyForm;

    tmp.name.value = "<? echo $MyObj->name ?>";
    tmp.age.value = "<? echo $MyObj->age ?>";
    tmp.ssn.value = "<? echo $MyObj->ssn ?>";
    ...
}

function SerializeMsg(name, age, ssn, ...) {
// see above!
}

function SubmitForm() {
    // sumbits the form
    tmp = document.MyForm;

    tmp.wddx_packet.value =
    SerializeMsg(tmp.name.value, tmp.age.value, tmp.ssn.value,...);
    document.submit();
}
// -->
</script>
</head>
<body>
...
<form name="MyForm"
action="<? echo $PHP_SELF ?>" method="post">
<input name="wddx_packet" type="hidden">
<input name="name" type="text">
<input name="age" type="text">
<input name="ssn" type="text">
...
<input name="SubmitBtn" type="button" onclick="SumbitForm()">
</form>
</body>
</html>

---------------------------------
If you don't provide that first 'php_class_name' in the JavaScript function, then when PHP deserializes it will deserialize into an associative array and not an object instance.

Note that this provides a way to maintain state without
using sessions or cookies - just good old HTML!

Hope this helps!
--
Chris Hansen


Serialize variables into a WDDX packet

  • ≪ wddx_serialize_value
  • XMLDiff ≫
  • PHP Manual
  • WDDX 함수 목록
  • Serialize variables into a WDDX packet

wddx_serialize_vars

(PHP 4, PHP 5, PHP 7)

wddx_serialize_vars — Serialize variables into a WDDX packet

설명

string wddx_serialize_vars ( mixed $var_name [, mixed $... ] )

Creates a WDDX packet with a structure that contains the serialized representation of the passed variables.

인수

This function takes a variable number of parameters.

var_name

Can be either a string naming a variable or an array containing strings naming the variables or another array, etc.

...

반환값

Returns the WDDX packet, or FALSE on error.

예제

Example #1 wddx_serialize_vars() example

<?php
$a = 1;
$b = 5.5;
$c = array("blue", "orange", "violet");
$d = "colors";

$clvars = array("c", "d");
echo wddx_serialize_vars("a", "b", $clvars);
?>

위 예제의 출력:

<wddxPacket version='1.0'><header/><data><struct><var name='a'><number>1</number></var>
<var name='b'><number>5.5</number></var><var name='c'><array length='3'>
<string>blue</string><string>orange</string><string>violet</string></array></var>
<var name='d'><string>colors</string></var></struct></data></wddxPacket>

User Contributed Notes

kangus at jaga dot us 13-Oct-2003 12:57
A function to use for dates with PHP & WDDX
getdate()

$ta="<FORM><TEXTAREA rows=15 cols=80>" ;
$te="</TEXTAREA></FORM>";
$packet_id=wddx_packet_start("Date Example--");
$myDate=getdate();
wddx_add_vars($packet_id,'myDate');
$package=wddx_packet_end($packet_id);
print($ta.$package.$te);

Produces:

<wddxPacket version='1.0'><header><comment>Date Example--</comment></header>
<data><struct><var name='myDate'>
<struct><var name='seconds'><number>12</number></var>
<var name='minutes'><number>55</number></var>
<var name='hours'><number>1</number></var>
<var name='mday'><number>13</number></var>
<var name='wday'><number>1</number></var>
<var name='mon'><number>10</number></var>
<var name='year'><number>2003</number></var>
<var name='yday'><number>285</number></var>
<var name='weekday'><string>Monday</string></var>
<var name='month'><string>October</string></var>
<var name='0'><number>1066031712</number>
</var></struct></var></struct></data></wddxPacket>
tychay at alumni dot caltech dot edu 31-Mar-2002 09:15
The above feature will cause a slight trouble since the name of the variable will always be "wddxtemp" but you can use preg_replace to fix that.

Also, a better approach is to serialize the entire class. WDDX will that write a reserved "php_class_name" variable which can be used to reconstruct the class during a deserialize.
tychay at alumni dot caltech dot edu 31-Mar-2002 07:49
You can use globals to get around the above in classes:

<?php
class Test
{
  var $str;
  var $wddx;

  function Test()
  {
    $this->str = "test";
    $this->wddx_serialize($this->str);
    header('Content-Type: text/xml');
    echo ($this->wddx);
  }

  function wddx_serialize($var) {
    global $wddxtemp;
    unset($wddxtemp);
    $wddxtemp = $var;
    $this->wddx = wddx_serialize_vars("wddxtemp");
  }
}

new Test();
?>
-terry
tychay at alumni dot caltech dot edu 31-Mar-2002 07:45
You can use globals to get around the above in classes:

<?php
class Test
{
  var $str;
  var $wddx;

  function Test()
  {
    $this->str = "test";
    $this->wddx_serialize($this->str);
    header('Content-Type: text/xml');
    echo ($this->wddx);
  }

  function wddx_serialize($var) {
    global $wddxtemp;
    unset($wddxtemp);
    $wddxtemp = $var;
    $this->wddx = wddx_serialize_vars("wddxtemp");
  }
}

new Test();
?>
-terry
christian dot knoflach at gmx dot at 22-May-2001 12:56
Be careful using WDDX - functions on object variables. As I just had to find out the following won't work:

<?php
class Test
{
  var $str;
  var $wddx;

  function Test()
  {
    $this->str = "test";
    $this->wddx = wddx_serialize_vars("this->str");
    print(htmlentities($this->wddx));
  }
}

new Test();
?>

That's what you'll get:
<wddxPacket version='1.0'><header/><data><struct></struct></data></wddxPacket>


XML diff and merge

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
  • XMLDiff\Base — The XMLDiff\Base class
    • XMLDiff\Base::__construct — Constructor
    • XMLDiff\Base::diff — Produce diff of two XML documents
    • XMLDiff\Base::merge — Produce new XML document based on diff
  • XMLDiff\DOM — The XMLDiff\DOM class
    • XMLDiff\DOM::diff — Diff two DOMDocument objects
    • XMLDiff\DOM::merge — Produce merged DOMDocument
  • XMLDiff\Memory — The XMLDiff\Memory class
    • XMLDiff\Memory::diff — Diff two XML documents
    • XMLDiff\Memory::merge — Produce merged XML document
  • XMLDiff\File — The XMLDiff\File class
    • XMLDiff\File::diff — Diff two XML files
    • XMLDiff\File::merge — Produce merged XML document

User Contributed Notes

There are no user contributed notes for this page.


소개

The extension is able to produce diffs of two XML documents and then to apply the difference to the source document. The diff is a XML document containing copy/insert/delete instruction nodes in human readable format. DOMDocument objects, local files and strings in memory can be processed.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

libdiffmark - version 0.10 is bundled with the extension. If diffmark is available from your system packages, its usage is strongly encouraged.

libxml extension

DOM extension

User Contributed Notes

There are no user contributed notes for this page.


설치

이 » PECL 확장은 PHP 배포판에서 제공하지 않습니다.

PECL 확장 설치에 관한 정보는 매뉴얼의 PECL 확장 설치에 있습니다. 새 릴리즈, 내려받기, 소스파일, 개발자 정보, CHANGELOG 등의 추가 정보는 여기에 있습니다: » http://pecl.php.net/package/xmldiff.

A DLL for this PECL extension is available under » http://windows.php.net/downloads/pecl/releases/xmldiff/.

User Contributed Notes

There are no user contributed notes for this page.


The XMLDiff\Base class

(PECL xmldiff >= 0.8.0)

소개

Base abstract class for all the comparsion classes in the extension.

클래스 개요

XMLDiff\Base {
/* 메소드 */
public __construct ( string $nsname )
abstract public mixed diff ( mixed $from , mixed $to )
abstract public mixed merge ( mixed $src , mixed $diff )
}

Table of Contents

  • XMLDiff\Base::__construct — Constructor
  • XMLDiff\Base::diff — Produce diff of two XML documents
  • XMLDiff\Base::merge — Produce new XML document based on diff

User Contributed Notes

There are no user contributed notes for this page.


Constructor

  • ≪ XMLDiff\Base
  • XMLDiff\Base::diff ≫
  • PHP Manual
  • XMLDiff\Base
  • Constructor

XMLDiff\Base::__construct

(PECL xmldiff >= 0.8.0)

XMLDiff\Base::__construct — Constructor

설명

public XMLDiff\Base::__construct ( string $nsname )

Base constructor for all the worker classes in the xmldiff extension.

인수

nsname

Custom namespace name for the diff document. The default namespace is http://www.locus.cz/diffmark and that's enough to avoid namespace conflicts. Use this parameter if you want to change it for some reason.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Produce diff of two XML documents

  • ≪ XMLDiff\Base::__construct
  • XMLDiff\Base::merge ≫
  • PHP Manual
  • XMLDiff\Base
  • Produce diff of two XML documents

XMLDiff\Base::diff

(PECL xmldiff >= 0.8.0)

XMLDiff\Base::diff — Produce diff of two XML documents

설명

abstract public mixed XMLDiff\Base::diff ( mixed $from , mixed $to )

Abstract diff method to be implemented by inheriting classes.

The basic purpose of this method is to produce diff of the two documents. The param order matters and will produce different output.

인수

from

Source XML document.

to

Target XML document.

반환값

Implementation dependent.

User Contributed Notes

There are no user contributed notes for this page.


Produce new XML document based on diff

  • ≪ XMLDiff\Base::diff
  • XMLDiff\DOM ≫
  • PHP Manual
  • XMLDiff\Base
  • Produce new XML document based on diff

XMLDiff\Base::merge

(PECL xmldiff >= 0.8.0)

XMLDiff\Base::merge — Produce new XML document based on diff

설명

abstract public mixed XMLDiff\Base::merge ( mixed $src , mixed $diff )

Abstract merge method to be implemented by inheriting classes.

Basically the method purpose is to produce a new XML document based on the diff information.

인수

src

Source XML document.

diff

Document produced by the diff method.

반환값

Implementation dependent.

User Contributed Notes

There are no user contributed notes for this page.


The XMLDiff\DOM class

(PECL xmldiff >= 0.8.0)

소개

클래스 개요

XMLDiff\DOM extends XMLDiff\Base {
/* 메소드 */
public DOMDocument diff ( DOMDocument $from , DOMDocument $to )
public DOMDocument merge ( DOMDocument $src , DOMDocument $diff )
/* 상속된 메소드 */
public XMLDiff\Base::__construct ( string $nsname )
abstract public mixed XMLDiff\Base::diff ( mixed $from , mixed $to )
abstract public mixed XMLDiff\Base::merge ( mixed $src , mixed $diff )
}

Table of Contents

  • XMLDiff\DOM::diff — Diff two DOMDocument objects
  • XMLDiff\DOM::merge — Produce merged DOMDocument

User Contributed Notes

There are no user contributed notes for this page.


Diff two DOMDocument objects

  • ≪ XMLDiff\DOM
  • XMLDiff\DOM::merge ≫
  • PHP Manual
  • XMLDiff\DOM
  • Diff two DOMDocument objects

XMLDiff\DOM::diff

(PECL xmldiff >= 0.8.0)

XMLDiff\DOM::diff — Diff two DOMDocument objects

설명

public DOMDocument XMLDiff\DOM::diff ( DOMDocument $from , DOMDocument $to )

Diff two DOMDocument instances and produce the new one containing the diff information.

인수

from

Source DOMDocument object.

to

Target DOMDocument object.

반환값

DOMDocument with the diff information or NULL.

User Contributed Notes

There are no user contributed notes for this page.


Produce merged DOMDocument

  • ≪ XMLDiff\DOM::diff
  • XMLDiff\Memory ≫
  • PHP Manual
  • XMLDiff\DOM
  • Produce merged DOMDocument

XMLDiff\DOM::merge

(PECL xmldiff >= 0.8.0)

XMLDiff\DOM::merge — Produce merged DOMDocument

설명

public DOMDocument XMLDiff\DOM::merge ( DOMDocument $src , DOMDocument $diff )

Create new DOMDocument based on the diff.

인수

src

Source DOMDocument object.

diff

DOMDocument object containing the diff information.

반환값

Merged DOMDocument or NULL.

User Contributed Notes

There are no user contributed notes for this page.


The XMLDiff\Memory class

(PECL xmldiff >= 0.8.0)

소개

클래스 개요

XMLDiff\Memory extends XMLDiff\Base {
/* 메소드 */
public string diff ( string $from , string $to )
public string merge ( string $src , string $diff )
/* 상속된 메소드 */
public XMLDiff\Base::__construct ( string $nsname )
abstract public mixed XMLDiff\Base::diff ( mixed $from , mixed $to )
abstract public mixed XMLDiff\Base::merge ( mixed $src , mixed $diff )
}

Table of Contents

  • XMLDiff\Memory::diff — Diff two XML documents
  • XMLDiff\Memory::merge — Produce merged XML document

User Contributed Notes

There are no user contributed notes for this page.


Diff two XML documents

  • ≪ XMLDiff\Memory
  • XMLDiff\Memory::merge ≫
  • PHP Manual
  • XMLDiff\Memory
  • Diff two XML documents

XMLDiff\Memory::diff

(PECL xmldiff >= 0.8.0)

XMLDiff\Memory::diff — Diff two XML documents

설명

public string XMLDiff\Memory::diff ( string $from , string $to )

Diff two strings containing XML documents and produce the diff information.

인수

from

Source XML document.

to

Target XML document.

반환값

String with the XML document containing the diff information or NULL.

User Contributed Notes

There are no user contributed notes for this page.


Produce merged XML document

  • ≪ XMLDiff\Memory::diff
  • XMLDiff\File ≫
  • PHP Manual
  • XMLDiff\Memory
  • Produce merged XML document

XMLDiff\Memory::merge

(PECL xmldiff >= 0.8.0)

XMLDiff\Memory::merge — Produce merged XML document

설명

public string XMLDiff\Memory::merge ( string $src , string $diff )

Create new XML document based on diffs and source document.

인수

src

Source XML document.

diff

XML document containing diff information.

반환값

String with the new XML document or NULL.

User Contributed Notes

There are no user contributed notes for this page.


The XMLDiff\File class

(PECL xmldiff >= 0.8.0)

소개

클래스 개요

XMLDiff\File extends XMLDiff\Base {
/* 메소드 */
public string diff ( string $from , string $to )
public string merge ( string $src , string $diff )
/* 상속된 메소드 */
public XMLDiff\Base::__construct ( string $nsname )
abstract public mixed XMLDiff\Base::diff ( mixed $from , mixed $to )
abstract public mixed XMLDiff\Base::merge ( mixed $src , mixed $diff )
}

Table of Contents

  • XMLDiff\File::diff — Diff two XML files
  • XMLDiff\File::merge — Produce merged XML document

User Contributed Notes

ngbabu at gmail dot com 21-Nov-2015 05:45
The following snippet will show the use case of XMLDiff\File.

<?php
$dxml=new XMLDiff\File;
$diff = $dxml->diff("file1.xml", "file2.xml");
$difffile = 'difference.xml';
file_put_contents($difffile, $diff);
?>


Diff two XML files

  • ≪ XMLDiff\File
  • XMLDiff\File::merge ≫
  • PHP Manual
  • XMLDiff\File
  • Diff two XML files

XMLDiff\File::diff

(PECL xmldiff >= 0.8.0)

XMLDiff\File::diff — Diff two XML files

설명

public string XMLDiff\File::diff ( string $from , string $to )

Diff two local XML files and produce string with the diff information.

인수

from

Path to the source document.

to

Path to the target document.

반환값

String with the XML document containing the diff information or NULL.

User Contributed Notes

There are no user contributed notes for this page.


Produce merged XML document

  • ≪ XMLDiff\File::diff
  • XML Parser ≫
  • PHP Manual
  • XMLDiff\File
  • Produce merged XML document

XMLDiff\File::merge

(PECL xmldiff >= 0.8.0)

XMLDiff\File::merge — Produce merged XML document

설명

public string XMLDiff\File::merge ( string $src , string $diff )

Create new XML document based on diffs and source document.

인수

src

Path to the source XML document.

diff

Path to the XML document with the diff information.

반환값

String with the new XML document or NULL.

User Contributed Notes

There are no user contributed notes for this page.


XML Parser

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • Event Handlers
  • Case Folding
  • Error Codes
  • Character Encoding
  • 예제
    • XML Element Structure Example
    • XML Tag Mapping Example
    • XML External Entity Example
  • XML Parser 함수 목록
    • utf8_decode — Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1
    • utf8_encode — Encodes an ISO-8859-1 string to UTF-8
    • xml_error_string — Get XML parser error string
    • xml_get_current_byte_index — Get current byte index for an XML parser
    • xml_get_current_column_number — Get current column number for an XML parser
    • xml_get_current_line_number — Get current line number for an XML parser
    • xml_get_error_code — Get XML parser error code
    • xml_parse_into_struct — Parse XML data into an array structure
    • xml_parse — Start parsing an XML document
    • xml_parser_create_ns — Create an XML parser with namespace support
    • xml_parser_create — Create an XML parser
    • xml_parser_free — Free an XML parser
    • xml_parser_get_option — Get options from an XML parser
    • xml_parser_set_option — Set options in an XML parser
    • xml_set_character_data_handler — Set up character data handler
    • xml_set_default_handler — Set up default handler
    • xml_set_element_handler — Set up start and end element handlers
    • xml_set_end_namespace_decl_handler — Set up end namespace declaration handler
    • xml_set_external_entity_ref_handler — Set up external entity reference handler
    • xml_set_notation_decl_handler — Set up notation declaration handler
    • xml_set_object — Use XML Parser within an object
    • xml_set_processing_instruction_handler — Set up processing instruction (PI) handler
    • xml_set_start_namespace_decl_handler — Set up start namespace declaration handler
    • xml_set_unparsed_entity_decl_handler — Set up unparsed entity declaration handler

User Contributed Notes

marcgear at gmail dot com 04-Aug-2010 07:12
While this SAX event based parser is better for memory management than the tree based parsers of SimpleXML and DOM, the pull-based parser XMLReader is much easier to use than the xml_parser_* functions, and still doesn't require loading the file into memory.
nat 04-Sep-2009 09:19
An event-based parser such XML Parser is preferable for large files, because tree-based parsers must fully load the file into memory in order to parse the XML. Event-based parsers do not need to load the entire file into memory to begin parsing.
pavel dot lishin at gmail dot com 28-Jul-2009 06:33
If you're wanting to actually work with XML data as it was intended, treating it as a tree, try http://us2.php.net/manual/en/book.simplexml.php .


소개

XML (eXtensible Markup Language) is a data format for structured document interchange on the Web. It is a standard defined by The World Wide Web consortium (W3C). Information about XML and related technologies can be found at » http://www.w3.org/XML/.

This PHP extension implements support for James Clark's expat in PHP. This toolkit lets you parse, but not validate, XML documents. It supports three source character encodings also provided by PHP: US-ASCII, ISO-8859-1 and UTF-8. UTF-16 is not supported.

This extension lets you create XML parsers and then define handlers for different XML events. Each XML parser also has a few parameters you can adjust.

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

This extension requires the libxml PHP extension. This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default.

This extension uses an expat compat layer by default. It can use also expat, which can be found at » http://www.jclark.com/xml/expat.html. The Makefile that comes with expat does not build a library by default, you can use this make rule for that:

libexpat.a: $(OBJS)
    ar -rc $@ $(OBJS)
    ranlib $@
A source RPM package of expat can be found at » http://sourceforge.net/projects/expat/.

User Contributed Notes

There are no user contributed notes for this page.


설치

이 함수들은 번들된 expat 라이브러리를 이용하여 기본값으로 활성화되어 있습니다. XML 지원을 비활성화 하려면 --disable-xml 을 사용하십시오. PHP를 아파치 1.3.9 이상의 모듈로 컴파일한다면, PHP는 자동적으로 아파치에 번들된 expat를 사용합니다. 번들된 expat 라이브러리를 사용하지 않으려면, PHP 설정에 --with-expat-dir=DIR 을 지정하십시오. DIR은 expat를 설치한 베이스 디렉토리를 지정해야 합니다.

PHP 윈도우 버전에서는 이 확장에 대한 지원이 포함되어 있습니다. 이 함수들을 이용하기 위해서 추가로 확장을 읽어들일 필요가 없습니다.

User Contributed Notes

Anonymous 17-Mar-2016 06:56
Please mention also for NON-windows users, how this is configured/installed in other environments.


자원형

The xml resource as returned by xml_parser_create() and xml_parser_create_ns() references an xml parser instance to be used with the functions provided by this extension.

User Contributed Notes

There are no user contributed notes for this page.


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

XML_ERROR_NONE (integer)
XML_ERROR_NO_MEMORY (integer)
XML_ERROR_SYNTAX (integer)
XML_ERROR_NO_ELEMENTS (integer)
XML_ERROR_INVALID_TOKEN (integer)
XML_ERROR_UNCLOSED_TOKEN (integer)
XML_ERROR_PARTIAL_CHAR (integer)
XML_ERROR_TAG_MISMATCH (integer)
XML_ERROR_DUPLICATE_ATTRIBUTE (integer)
XML_ERROR_JUNK_AFTER_DOC_ELEMENT (integer)
XML_ERROR_PARAM_ENTITY_REF (integer)
XML_ERROR_UNDEFINED_ENTITY (integer)
XML_ERROR_RECURSIVE_ENTITY_REF (integer)
XML_ERROR_ASYNC_ENTITY (integer)
XML_ERROR_BAD_CHAR_REF (integer)
XML_ERROR_BINARY_ENTITY_REF (integer)
XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF (integer)
XML_ERROR_MISPLACED_XML_PI (integer)
XML_ERROR_UNKNOWN_ENCODING (integer)
XML_ERROR_INCORRECT_ENCODING (integer)
XML_ERROR_UNCLOSED_CDATA_SECTION (integer)
XML_ERROR_EXTERNAL_ENTITY_HANDLING (integer)
XML_OPTION_CASE_FOLDING (integer)
XML_OPTION_TARGET_ENCODING (integer)
XML_OPTION_SKIP_TAGSTART (integer)
XML_OPTION_SKIP_WHITE (integer)
XML_SAX_IMPL (string)
Holds the SAX implementation method. Can be libxml or expat.

User Contributed Notes

There are no user contributed notes for this page.


Event Handlers

The XML event handlers defined are:

Supported XML handlers
PHP function to set handler | Event description
xml_set_element_handler() | Element events are issued whenever the XML parser encounters start or end tags. There are separate handlers for start tags and end tags.
xml_set_character_data_handler() | Character data is roughly all the non-markup contents of XML documents, including whitespace between tags. Note that the XML parser does not add or remove any whitespace, it is up to the application (you) to decide whether whitespace is significant.
xml_set_processing_instruction_handler() | PHP programmers should be familiar with processing instructions (PIs) already. <?php ?> is a processing instruction, where php is called the "PI target". The handling of these are application-specific, except that all PI targets starting with "XML" are reserved.
xml_set_default_handler() | What goes not to another handler goes to the default handler. You will get things like the XML and document type declarations in the default handler.
xml_set_unparsed_entity_decl_handler() | This handler will be called for declaration of an unparsed (NDATA) entity.
xml_set_notation_decl_handler() | This handler is called for declaration of a notation.
xml_set_external_entity_ref_handler() | This handler is called when the XML parser finds a reference to an external parsed general entity. This can be a reference to a file or URL, for example. See the external entity example for a demonstration.
xml_set_start_namespace_decl_handler() | This handler is called for the start of a namespace declaration.
xml_set_end_namespace_decl_handler() | This handler is called for the end of a namespace declaration. Note that this event is NOT under LibXML.

User Contributed Notes

There are no user contributed notes for this page.


Case Folding

The element handler functions may get their element names case-folded. Case-folding is defined as "a process applied to a sequence of characters, in which those identified as non-uppercase are replaced by their uppercase equivalents". In other words, when it comes to XML, case-folding simply means uppercasing.

By default, all the element names that are passed to the handler functions are case-folded. This behaviour can be queried and controlled per XML parser with the xml_parser_get_option() and xml_parser_set_option() functions, respectively.

User Contributed Notes

There are no user contributed notes for this page.


Error Codes

The following constants are defined for XML error codes (as returned by xml_parse()):

  • XML_ERROR_NONE
  • XML_ERROR_NO_MEMORY
  • XML_ERROR_SYNTAX
  • XML_ERROR_NO_ELEMENTS
  • XML_ERROR_INVALID_TOKEN
  • XML_ERROR_UNCLOSED_TOKEN
  • XML_ERROR_PARTIAL_CHAR
  • XML_ERROR_TAG_MISMATCH
  • XML_ERROR_DUPLICATE_ATTRIBUTE
  • XML_ERROR_JUNK_AFTER_DOC_ELEMENT
  • XML_ERROR_PARAM_ENTITY_REF
  • XML_ERROR_UNDEFINED_ENTITY
  • XML_ERROR_RECURSIVE_ENTITY_REF
  • XML_ERROR_ASYNC_ENTITY
  • XML_ERROR_BAD_CHAR_REF
  • XML_ERROR_BINARY_ENTITY_REF
  • XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF
  • XML_ERROR_MISPLACED_XML_PI
  • XML_ERROR_UNKNOWN_ENCODING
  • XML_ERROR_INCORRECT_ENCODING
  • XML_ERROR_UNCLOSED_CDATA_SECTION
  • XML_ERROR_EXTERNAL_ENTITY_HANDLING

User Contributed Notes

djmaze 14-Aug-2010 05:29
Here's the full list of all 733 error codes
http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors
Michael 20-Aug-2009 12:33
XML_ERR_NAME_REQUIRED - Means you have an entity that isn't valid in XML.

For example: & < and > all have to be escaped as &amp; &lt; and &gt; respectively.


Character Encoding

PHP's XML extension supports the » Unicode character set through different character encodings. There are two types of character encodings, source encoding and target encoding. PHP's internal representation of the document is always encoded with UTF-8.

Source encoding is done when an XML document is parsed. Upon creating an XML parser, a source encoding can be specified (this encoding can not be changed later in the XML parser's lifetime). The supported source encodings are ISO-8859-1, US-ASCII and UTF-8. The former two are single-byte encodings, which means that each character is represented by a single byte. UTF-8 can encode characters composed by a variable number of bits (up to 21) in one to four bytes. The default source encoding used by PHP is ISO-8859-1.

Target encoding is done when PHP passes data to XML handler functions. When an XML parser is created, the target encoding is set to the same as the source encoding, but this may be changed at any point. The target encoding will affect character data as well as tag names and processing instruction targets.

If the XML parser encounters characters outside the range that its source encoding is capable of representing, it will return an error.

If PHP encounters characters in the parsed XML document that can not be represented in the chosen target encoding, the problem characters will be "demoted". Currently, this means that such characters are replaced by a question mark.

User Contributed Notes

There are no user contributed notes for this page.


예제

Table of Contents

  • XML Element Structure Example
  • XML Tag Mapping Example
  • XML External Entity Example

User Contributed Notes

There are no user contributed notes for this page.


XML Element Structure Example

This first example displays the structure of the start elements in a document with indentation.

Example #1 Show XML Element Structure

<?php
$file = "data.xml";
$depth = array();

function startElement($parser, $name, $attrs)
{
    global $depth;

    if (!isset($depth[$parser])) {
        $depth[$parser] = 0;
    }

    for ($i = 0; $i < $depth[$parser]; $i++) {
        echo "  ";
    }
    echo "$name\n";
    $depth[$parser]++;
}

function endElement($parser, $name)
{
    global $depth;
    $depth[$parser]--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);
?>

User Contributed Notes

Sample at hotmail dot com 04-Jul-2013 07:32
<?php

public static function xml2array($element, $arr = array())
    {
        if(is_string($element))
        {
            $element = (strlen($element) > 5 && substr($element, -4) === '.xml')
                            ? simplexml_load_file(DATAPATH.$element)
                            : simplexml_load_string($element);
        }
         $iter = 0;
        foreach($element->children() as $b)
        {
            $a = $b->getName();
            if(!$b->children()){
                    $arr[$a] = trim($b[0]);
            }
            else{
                    $arr[$a][$iter] = array();
                    $arr[$a][$iter] = self::xml2array($b,$arr[$a][$iter]);
            }
                $iter++;
        }
        return $arr;
    }
}

?>
roan at horning dot us 31-Oct-2009 06:07
The following example will echo back an xml file using the basic event based parser.  Currently, (at least in the version of PHP5 I'm using) it will skip The XML declaration and the Doctype declaration--they don't seem to be captured by the default handler.

<?php
echo "<pre>";

$file = "test.xml";
echo $file."\n";
global $inTag;

$inTag = "";
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
xml_set_processing_instruction_handler($xml_parser, "pi_handler");
xml_set_default_handler($xml_parser, "parseDEFAULT");
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "contents");

if (!($fp = fopen($file, "r"))) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
       die( sprintf("XML error: %s at line %d",
                            xml_error_string(xml_get_error_code($xml_parser)),
                            xml_get_current_line_number($xml_parser)));
    }
}
while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
       die( sprintf("XML error: %s at line %d",
                            xml_error_string(xml_get_error_code($xml_parser)),
                            xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);

function startElement($parser, $name, $attrs) {

    global $inTag;
    global $depth;

    $padTag = str_repeat(str_pad(" ", 3), $depth);

    if (!($inTag == "")) {
        echo "&gt;";
    }
    echo "\n$padTag&lt;$name";
    foreach ($attrs as $key => $value) {
        echo "\n$padTag".str_pad(" ", 3);
        echo " $key=\"$value\"";
    }
    $inTag = $name;
    $depth++;
}

function endElement($parser, $name) {

    global $depth;
   global $inTag;
    global $closeTag;

    $depth--;

   if ($closeTag == TRUE) {
       echo "&lt/$name&gt;";
       $inTag = "";
   } elseif ($inTag == $name) {
       echo " /&gt;";
       $inTag = "";
   } else {
         $padTag = str_repeat(str_pad(" ", 3), $depth);
       echo "\n$padTag&lt/$name&gt;";
    }
}

function contents($parser, $data) {

    global $closeTag;

    $data = preg_replace("/^\s+/", "", $data);
    $data = preg_replace("/\s+$/", "", $data);

    if (!($data == ""))  {
        echo "&gt;$data";
        $closeTag = TRUE;
    } else {
        $closeTag = FALSE;
     }
}

function parseDEFAULT($parser, $data) {

    $data = preg_replace("/</", "&lt;", $data);
    $data = preg_replace("/>/", "&gt;", $data);
    echo $data;
}

function pi_handler($parser, $target, $data) {

    echo "&lt;?$target $data?&gt;\n";
}
echo "</pre>";
?>


XML Tag Mapping Example

Example #1 Map XML to HTML

This example maps tags in an XML document directly to HTML tags. Elements not found in the "map array" are ignored. Of course, this example will only work with a specific XML document type.

<?php
$file = "data.xml";
$map_array = array(
    "BOLD"     => "B",
    "EMPHASIS" => "I",
    "LITERAL"  => "TT"
);

function startElement($parser, $name, $attrs)
{
    global $map_array;
    if (isset($map_array[$name])) {
        echo "<$map_array[$name]>";
    }
}

function endElement($parser, $name)
{
    global $map_array;
    if (isset($map_array[$name])) {
        echo "</$map_array[$name]>";
    }
}

function characterData($parser, $data)
{
    echo $data;
}

$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);
?>

User Contributed Notes

There are no user contributed notes for this page.


XML External Entity Example

This example highlights XML code. It illustrates how to use an external entity reference handler to include and parse other documents, as well as how PIs can be processed, and a way of determining "trust" for PIs containing code.

XML documents that can be used for this example are found below the example (xmltest.xml and xmltest2.xml.)

Example #1 External Entity Example

<?php
$file = "xmltest.xml";

function trustedFile($file)
{
    // only trust local files owned by ourselves
    if (!preg_match("@^([a-z][a-z0-9+.-]*)\:\/\/@i", $file)
        && fileowner($file) == getmyuid()) {
            return true;
    }
    return false;
}

function startElement($parser, $name, $attribs)
{
    echo "&lt;<font color=\"#0000cc\">$name</font>";
    if (count($attribs)) {
        foreach ($attribs as $k => $v) {
            echo " <font color=\"#009900\">$k</font>=\"<font
                   color=\"#990000\">$v</font>\"";
        }
    }
    echo "&gt;";
}

function endElement($parser, $name)
{
    echo "&lt;/<font color=\"#0000cc\">$name</font>&gt;";
}

function characterData($parser, $data)
{
    echo "<b>$data</b>";
}

function PIHandler($parser, $target, $data)
{
    switch (strtolower($target)) {
        case "php":
            global $parser_file;
            // If the parsed document is "trusted", we say it is safe
            // to execute PHP code inside it.  If not, display the code
            // instead.
            if (trustedFile($parser_file[$parser])) {
                eval($data);
            } else {
                printf("Untrusted PHP code: <i>%s</i>",
                        htmlspecialchars($data));
            }
            break;
    }
}

function defaultHandler($parser, $data)
{
    if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
        printf('<font color="#aa00aa">%s</font>',
                htmlspecialchars($data));
    } else {
        printf('<font size="-1">%s</font>',
                htmlspecialchars($data));
    }
}

function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,
                                  $publicId) {
    if ($systemId) {
        if (!list($parser, $fp) = new_xml_parser($systemId)) {
            printf("Could not open entity %s at %s\n", $openEntityNames,
                   $systemId);
            return false;
        }
        while ($data = fread($fp, 4096)) {
            if (!xml_parse($parser, $data, feof($fp))) {
                printf("XML error: %s at line %d while parsing entity %s\n",
                       xml_error_string(xml_get_error_code($parser)),
                       xml_get_current_line_number($parser), $openEntityNames);
                xml_parser_free($parser);
                return false;
            }
        }
        xml_parser_free($parser);
        return true;
    }
    return false;
}

function new_xml_parser($file)
{
    global $parser_file;

    $xml_parser = xml_parser_create();
    xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    xml_set_processing_instruction_handler($xml_parser, "PIHandler");
    xml_set_default_handler($xml_parser, "defaultHandler");
    xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");

    if (!($fp = @fopen($file, "r"))) {
        return false;
    }
    if (!is_array($parser_file)) {
        settype($parser_file, "array");
    }
    $parser_file[$xml_parser] = $file;
    return array($xml_parser, $fp);
}

if (!(list($xml_parser, $fp) = new_xml_parser($file))) {
    die("could not open XML input");
}

echo "<pre>";
while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d\n",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
echo "</pre>";
echo "parse complete\n";
xml_parser_free($xml_parser);

?>

Example #2 xmltest.xml

<?xml version='1.0'?>
<!DOCTYPE chapter SYSTEM "/just/a/test.dtd" [
<!ENTITY plainEntity "FOO entity">
<!ENTITY systemEntity SYSTEM "xmltest2.xml">
]>
<chapter>
 <TITLE>Title &plainEntity;</TITLE>
 <para>
  <informaltable>
   <tgroup cols="3">
    <tbody>
     <row><entry>a1</entry><entry morerows="1">b1</entry><entry>c1</entry></row>
     <row><entry>a2</entry><entry>c2</entry></row>
     <row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
    </tbody>
   </tgroup>
  </informaltable>
 </para>
 &systemEntity;
 <section id="about">
  <title>About this Document</title>
  <para>
   <!-- this is a comment -->
   <?php echo 'Hi!  This is PHP version ' . phpversion(); ?>
  </para>
 </section>
</chapter>

This file is included from xmltest.xml:

Example #3 xmltest2.xml

<?xml version="1.0"?>
<!DOCTYPE foo [
<!ENTITY testEnt "test entity">
]>
<foo>
   <element attrib="value"/>
   &testEnt;
   <?php echo "This is some more PHP code being executed."; ?>
</foo>

User Contributed Notes

There are no user contributed notes for this page.


XML Parser 함수 목록

Table of Contents

  • utf8_decode — Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1
  • utf8_encode — Encodes an ISO-8859-1 string to UTF-8
  • xml_error_string — Get XML parser error string
  • xml_get_current_byte_index — Get current byte index for an XML parser
  • xml_get_current_column_number — Get current column number for an XML parser
  • xml_get_current_line_number — Get current line number for an XML parser
  • xml_get_error_code — Get XML parser error code
  • xml_parse_into_struct — Parse XML data into an array structure
  • xml_parse — Start parsing an XML document
  • xml_parser_create_ns — Create an XML parser with namespace support
  • xml_parser_create — Create an XML parser
  • xml_parser_free — Free an XML parser
  • xml_parser_get_option — Get options from an XML parser
  • xml_parser_set_option — Set options in an XML parser
  • xml_set_character_data_handler — Set up character data handler
  • xml_set_default_handler — Set up default handler
  • xml_set_element_handler — Set up start and end element handlers
  • xml_set_end_namespace_decl_handler — Set up end namespace declaration handler
  • xml_set_external_entity_ref_handler — Set up external entity reference handler
  • xml_set_notation_decl_handler — Set up notation declaration handler
  • xml_set_object — Use XML Parser within an object
  • xml_set_processing_instruction_handler — Set up processing instruction (PI) handler
  • xml_set_start_namespace_decl_handler — Set up start namespace declaration handler
  • xml_set_unparsed_entity_decl_handler — Set up unparsed entity declaration handler

User Contributed Notes

anrdaemon at freemail dot ru 10-Feb-2016 03:06
You must not, never ever use utf8_encode() and utf8_decode().
If you need multibyte conversion utility, use iconv() instead. At the very least, you will get a notification, if your string can not be converted.
utf8_* crap, like other crap of this nature, is ONLY SUPPORTING ONE CONVERSION. ISO-8859-1 <=> UTF-8.
Glen at ITIStudios dot ca 16-Aug-2013 03:42
Update: concatenate element contents in function cdata()
<?php
/**
 *  XML to Associative Array Class
 *
 *  Usage:
 *     $domObj = new xmlToArrayParser($xml);
 *     $domArr = $domObj->array;
 *
 *     if($domObj->parse_error) echo $domObj->get_xml_error();
 *     else print_r($domArr);
 *
 *     On Success:
 *     eg. $domArr['top']['element2']['attrib']['var2'] => val2
 *
 *     On Error:
 *     eg. Error Code [76] "Mismatched tag", at char 58 on line 3
 */

/**
 * Convert an xml file or string to an associative array (including the tag attributes):
 * $domObj = new xmlToArrayParser($xml);
 * $elemVal = $domObj->array['element']
 * Or:  $domArr=$domObj->array;  $elemVal = $domArr['element'].
 *
 * @version  3.0
 * @param Str $xml file/string.
 */
class xmlToArrayParser {
  /** The array created by the parser can be assigned to any variable: $anyVarArr = $domObj->array.*/
  public  $array = array();
  public  $parse_error = false;
  private $parser;
  private $pointer;

  /** Constructor: $domObj = new xmlToArrayParser($xml); */
  public function __construct($xml) {
    $this->pointer =& $this->array;
    $this->parser = xml_parser_create("UTF-8");
    xml_set_object($this->parser, $this);
    xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
    xml_set_element_handler($this->parser, "tag_open", "tag_close");
    xml_set_character_data_handler($this->parser, "cdata");
    $this->parse_error = xml_parse($this->parser, ltrim($xml))? false : true;
  }

  /** Free the parser. */
  public function __destruct() { xml_parser_free($this->parser);}

  /** Get the xml error if an an error in the xml file occured during parsing. */
  public function get_xml_error() {
    if($this->parse_error) {
      $errCode = xml_get_error_code ($this->parser);
      $thisError =  "Error Code [". $errCode ."] \"<strong style='color:red;'>" . xml_error_string($errCode)."</strong>\",
                            at char ".xml_get_current_column_number($this->parser) . "
                            on line ".xml_get_current_line_number($this->parser)."";
    }else $thisError = $this->parse_error;
    return $thisError;
  }

  private function tag_open($parser, $tag, $attributes) {
    $this->convert_to_array($tag, 'attrib');
    $idx=$this->convert_to_array($tag, 'cdata');
    if(isset($idx)) {
      $this->pointer[$tag][$idx] = Array('@idx' => $idx,'@parent' => &$this->pointer);
      $this->pointer =& $this->pointer[$tag][$idx];
    }else {
      $this->pointer[$tag] = Array('@parent' => &$this->pointer);
      $this->pointer =& $this->pointer[$tag];
    }
    if (!empty($attributes)) { $this->pointer['attrib'] = $attributes; }
  }

  /** Adds the current elements content to the current pointer[cdata] array. */
  private function cdata($parser, $cdata){
    if (isset($this->pointer['cdata'])) { $this->pointer['cdata'] .= $cdata;}
    else { $this->pointer['cdata'] = $cdata; }
  }

  private function tag_close($parser, $tag) {
    $current = & $this->pointer;
    if(isset($this->pointer['@idx'])) {unset($current['@idx']);}
    $this->pointer = & $this->pointer['@parent'];
    unset($current['@parent']);
    if(isset($current['cdata']) && count($current) == 1) { $current = $current['cdata'];}
    else if(empty($current['cdata'])) {unset($current['cdata']);}
  }

  /** Converts a single element item into array(element[0]) if a second element of the same name is encountered. */
  private function convert_to_array($tag, $item) {
    if(isset($this->pointer[$tag]) && !is_array($this->pointer[$tag])){
      $content = $this->pointer[$tag];
      $this->pointer[$tag] = array((0) => $content);
      $idx = 1;
    }else if (isset($this->pointer[$tag])) {
      $idx = count($this->pointer[$tag]);
      if(!isset($this->pointer[$tag][0])) {
        foreach ($this->pointer[$tag] as $key => $value) {
            unset($this->pointer[$tag][$key]);
            $this->pointer[$tag][0][$key] = $value;
    }}}else $idx = null;
    return $idx;
  }
}
?>
Glen at ITIStudios dot ca 06-Apr-2013 04:38
Update: corrected isset() in convert_to_array() function
<?php
/**
 *  XML to Associative Array Class
 *
 *  Usage:
 *     $domObj = new xmlToArrayParser($xml);
 *     $domArr = $domObj->array;
 *
 *     if($domObj->parse_error) echo $domObj->get_xml_error();
 *     else print_r($domArr);
 *
 *     On Success:
 *     eg. $domArr['top']['element2']['attrib']['var2'] => val2
 *
 *     On Error:
 *     eg. Error Code [76] "Mismatched tag", at char 58 on line 3
 */

/**
 * Convert an xml file or string to an associative array (including the tag attributes):
 * $domObj = new xmlToArrayParser($xml);
 * $elemVal = $domObj->array['element']
 * Or:  $domArr=$domObj->array;  $elemVal = $domArr['element'].
 *
 * @version  2.0
 * @param Str $xml file/string.
 */
class xmlToArrayParser {
  /** The array created by the parser can be assigned to any variable: $anyVarArr = $domObj->array.*/
  public  $array = array();
  public  $parse_error = false;
  private $parser;
  private $pointer;

  /** Constructor: $domObj = new xmlToArrayParser($xml); */
  public function __construct($xml) {
    $this->pointer =& $this->array;
    $this->parser = xml_parser_create("UTF-8");
    xml_set_object($this->parser, $this);
    xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
    xml_set_element_handler($this->parser, "tag_open", "tag_close");
    xml_set_character_data_handler($this->parser, "cdata");
    $this->parse_error = xml_parse($this->parser, ltrim($xml))? false : true;
  }

  /** Free the parser. */
  public function __destruct() { xml_parser_free($this->parser);}

  /** Get the xml error if an an error in the xml file occured during parsing. */
  public function get_xml_error() {
    if($this->parse_error) {
      $errCode = xml_get_error_code ($this->parser);
      $thisError =  "Error Code [". $errCode ."] \"<strong style='color:red;'>" . xml_error_string($errCode)."</strong>\",
                            at char ".xml_get_current_column_number($this->parser) . "
                            on line ".xml_get_current_line_number($this->parser)."";
    }else $thisError = $this->parse_error;
    return $thisError;
  }

  private function tag_open($parser, $tag, $attributes) {
    $this->convert_to_array($tag, 'attrib');
    $idx=$this->convert_to_array($tag, 'cdata');
    if(isset($idx)) {
      $this->pointer[$tag][$idx] = Array('@idx' => $idx,'@parent' => &$this->pointer);
      $this->pointer =& $this->pointer[$tag][$idx];
    }else {
      $this->pointer[$tag] = Array('@parent' => &$this->pointer);
      $this->pointer =& $this->pointer[$tag];
    }
    if (!empty($attributes)) { $this->pointer['attrib'] = $attributes; }
  }

  /** Adds the current elements content to the current pointer[cdata] array. */
  private function cdata($parser, $cdata) { $this->pointer['cdata'] = trim($cdata); }

  private function tag_close($parser, $tag) {
    $current = & $this->pointer;
    if(isset($this->pointer['@idx'])) {unset($current['@idx']);}

    $this->pointer = & $this->pointer['@parent'];
    unset($current['@parent']);

    if(isset($current['cdata']) && count($current) == 1) { $current = $current['cdata'];}
    else if(empty($current['cdata'])) {unset($current['cdata']);}
  }

  /** Converts a single element item into array(element[0]) if a second element of the same name is encountered. */
  private function convert_to_array($tag, $item) {
    if(isset($this->pointer[$tag]) && !is_array($this->pointer[$tag])){
      $content = $this->pointer[$tag];
      $this->pointer[$tag] = array((0) => $content);
      $idx = 1;
    }else if (isset($this->pointer[$tag])) {
      $idx = count($this->pointer[$tag]);
      if(!isset($this->pointer[$tag][0])) {
        foreach ($this->pointer[$tag] as $key => $value) {
            unset($this->pointer[$tag][$key]);
            $this->pointer[$tag][0][$key] = $value;
    }}}else $idx = null;
    return $idx;
  }
}
?>
Glen at ITIStudios dot ca 03-Dec-2011 12:37
<?php
/**
 *  XML to Associative Array Class
 *
 *  Usage:
 *     $domObj = new xmlToArrayParser($xml);
 *     $domArr = $domObj->array;
 *
 *     if($domObj->parse_error) echo $domObj->get_xml_error();
 *     else print_r($domArr);
 *
 *     On Success:
 *     eg. $domArr['top']['element2']['attrib']['var2'] => val2
 *
 *     On Error:
 *     eg. Error Code [76] "Mismatched tag", at char 58 on line 3
 */

/**
 * Convert an xml file or string to an associative array (including the tag attributes):
 * $domObj = new xmlToArrayParser($xml);
 * $elemVal = $domObj->array['element']
 * Or:  $domArr=$domObj->array;  $elemVal = $domArr['element'].
 *
 * @version  2.0
 * @param Str $xml file/string.
 */
class xmlToArrayParser {
  /** The array created by the parser can be assigned to any variable: $anyVarArr = $domObj->array.*/
  public  $array = array();
  public  $parse_error = false;
  private $parser;
  private $pointer;

  /** Constructor: $domObj = new xmlToArrayParser($xml); */
  public function __construct($xml) {
    $this->pointer =& $this->array;
    $this->parser = xml_parser_create("UTF-8");
    xml_set_object($this->parser, $this);
    xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
    xml_set_element_handler($this->parser, "tag_open", "tag_close");
    xml_set_character_data_handler($this->parser, "cdata");
    $this->parse_error = xml_parse($this->parser, ltrim($xml))? false : true;
  }

  /** Free the parser. */
  public function __destruct() { xml_parser_free($this->parser);}

  /** Get the xml error if an an error in the xml file occured during parsing. */
  public function get_xml_error() {
    if($this->parse_error) {
      $errCode = xml_get_error_code ($this->parser);
      $thisError =  "Error Code [". $errCode ."] \"<strong style='color:red;'>" . xml_error_string($errCode)."</strong>\",
                            at char ".xml_get_current_column_number($this->parser) . "
                            on line ".xml_get_current_line_number($this->parser)."";
    }else $thisError = $this->parse_error;
    return $thisError;
  }

  private function tag_open($parser, $tag, $attributes) {
    $this->convert_to_array($tag, 'attrib');
    $idx=$this->convert_to_array($tag, 'cdata');
    if(isset($idx)) {
      $this->pointer[$tag][$idx] = Array('@idx' => $idx,'@parent' => &$this->pointer);
      $this->pointer =& $this->pointer[$tag][$idx];
    }else {
      $this->pointer[$tag] = Array('@parent' => &$this->pointer);
      $this->pointer =& $this->pointer[$tag];
    }
    if (!empty($attributes)) { $this->pointer['attrib'] = $attributes; }
  }

  /** Adds the current elements content to the current pointer[cdata] array. */
  private function cdata($parser, $cdata) { $this->pointer['cdata'] = trim($cdata); }

  private function tag_close($parser, $tag) {
    $current = & $this->pointer;
    if(isset($this->pointer['@idx'])) {unset($current['@idx']);}

    $this->pointer = & $this->pointer['@parent'];
    unset($current['@parent']);

    if(isset($current['cdata']) && count($current) == 1) { $current = $current['cdata'];}
    else if(empty($current['cdata'])) {unset($current['cdata']);}
  }

  /** Converts a single element item into array(element[0]) if a second element of the same name is encountered. */
  private function convert_to_array($tag, $item) {
    if(isset($this->pointer[$tag][$item])) {
      $content = $this->pointer[$tag];
      $this->pointer[$tag] = array((0) => $content);
      $idx = 1;
    }else if (isset($this->pointer[$tag])) {
      $idx = count($this->pointer[$tag]);
      if(!isset($this->pointer[$tag][0])) {
        foreach ($this->pointer[$tag] as $key => $value) {
            unset($this->pointer[$tag][$key]);
            $this->pointer[$tag][0][$key] = $value;
    }}}else $idx = null;
    return $idx;
  }
}
?>

This is supplimental information for the "class xmlToArrayParser".
This is a fully functional error free, extensively tested php class unlike the posts that follow it.

Key phrase: Fully functional, fully tested, error free XML To Array parser.

<?php
/**
 * class xmlToArrayParser
 *
  Notes:
  1. 'attrib' and 'cdata' are keys added to the array when the element contains both attributes and content.
  2. Ignores content that is not in between it's own set of tags.
  3. Don't know if it recognizes processing instructions nor do I know about processing instructions.
     <\?some_pi some_attr="some_value"?>  This is the same as a document declaration.
  4. Empty elements are not included unless they have attributes.
  5. Version 2.0, Dec. 2, 2011, added xml error reporting.

  Usage:
    $domObj = new xmlToArrayParser($xml);
    $elemVal = $domObj->array['element']
    Or assign the entire array to its own variable:
    $domArr = $domObj->array;
    $elemVal = $domArr['element']

  Example:
    $xml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <top>
      <element1>element content 1</element1>
      <element2 var2="val2" />
      <element3 var3="val3" var4="val4">element content 3</element3>
      <element3 var5="val5">element content 4</element3>
      <element3 var6="val6" />
      <element3>element content 7</element3>
    </top>';

    $domObj = new xmlToArrayParser($xml);
    $domArr = $domObj->array;

    if($domObj->parse_error) echo $domObj->get_xml_error();
    else print_r($domArr);

    On Success:
    $domArr['top']['element1'] => element content 1
    $domArr['top']['element2']['attrib']['var2'] => val2
    $domArr['top']['element3']['0']['attrib']['var3'] => val3
    $domArr['top']['element3']['0']['attrib']['var4'] => val4
    $domArr['top']['element3']['0']['cdata'] => element content 3
    $domArr['top']['element3']['1']['attrib']['var5'] => val5
    $domArr['top']['element3']['1']['cdata'] => element content 4
    $domArr['top']['element3']['2']['attrib']['var6'] => val6
    $domArr['top']['element3']['3'] => element content 7

    On Error:
    Error Code [76] "Mismatched tag", at char 58 on line 3
 *
 */
?>
wolfon.AT-DoG.inbox.ru 29-Jul-2008 12:09
Finally a simple xml => array class.
Functioning like SimpleXML library.

<?php
class xml  {
    private $parser;
    private $pointer;
    public $dom;

    public function __construct($data) {
        $this->pointer =& $this->dom;
        $this->parser = xml_parser_create();
        xml_set_object($this->parser, $this);
        xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
        xml_set_element_handler($this->parser, "tag_open", "tag_close");
        xml_set_character_data_handler($this->parser, "cdata");
        xml_parse($this->parser, $data);
    }

    private function tag_open($parser, $tag, $attributes) {
        if (isset($this->pointer[$tag]['@attributes'])) {
            $content = $this->pointer[$tag];
            $this->pointer[$tag] = array(0 => $content);
            $idx = 1;
        } else if (isset($this->pointer[$tag]))
            $idx = count($this->pointer[$tag]);

        if (isset($idx)) {
            $this->pointer[$tag][$idx] = Array(
                '@idx' => $idx,
                '@parent' => &$this->pointer);
               $this->pointer =& $this->pointer[$tag][$idx];
        } else {
            $this->pointer[$tag] = Array(
                '@parent' => &$this->pointer);
            $this->pointer =& $this->pointer[$tag];
        }
        if (!empty($attributes))
            $this->pointer['@attributes'] = $attributes;
    }

    private function cdata($parser, $cdata) {
           $this->pointer['@data'] = $cdata;
    }

    private function tag_close($parser, $tag) {
        $current = & $this->pointer;
        if (isset($this->pointer['@idx']))
            unset($current['@idx']);

        $this->pointer = & $this->pointer['@parent'];

          unset($current['@parent']);
           if (isset($current['@data']) && count($current) == 1)
               $current = $current['@data'];
           else if (empty($current['@data'])||$current['@data']==0)
               unset($current['@data']);
    }
}
?>

maybe I'll do some explanations on habr
geoff at spacevs dot com 08-Nov-2007 06:13
Reading xml into a class:

<?PHP
        class XmlData {}
        $elements = array();
        $elements[] =& new XmlData();
        function startElement($parser, $name, $attrs) {
                global $elements;
                $element =& new XMLData();
                $elements[count($elements)-1]->$name =& $element;
                $elements[] =& $element;
        }
        function endElement($parser, $name) {
                global $elements;
                array_pop($elements);
        }
        function characterData($parser, $data) {
                global $elements;
                $elements[count($elements)-1]->data = $data;
        }
        $xml_parser     = xml_parser_create();
        xml_set_element_handler($xml_parser, "startElement", "endElement");
        xml_set_character_data_handler($xml_parser, "characterData");
        xml_parse($xml_parser, $xml, true);
        xml_parser_free($xml_parser);
        $request =& array_pop($elements);

        echo $request->LOGIN->USER->data;
?>
demonpants at gmail dot com 23-Oct-2007 05:59
I wanted to access the ISBN database, and was previously parsing the HTML string generated from their main page, that is until I discovered they have an API that returns XML.

So, if anyone wants to get some information from the ISBN database, all you need to do is the following.

<?php
//Search the ISBN database for the book.
            $url = "http://www.isbndb.com/api/books.xml? access_key=KEY&index1=isbn&value1=$_GET[ISBN]";
            $p = xml_parser_create();
            xml_parse_into_struct($p,file_get_contents($url),$results,$index);
            xml_parser_free($p);

            $title = $results[$index[TITLELONG][0]][value];
            $author = $results[$index[AUTHORSTEXT][0]][value];
            $publisher = $results[$index[PUBLISHERTEXT][0]][value];
?>

You will need to get an access key from isbndb.com, but it takes two seconds and is free. When you get it, replace KEY in the URL with your own key. Also, my code above will search for the book that fits the ISBN number stored in the GET variable ISBN - you can search by other parameters and return more than one result, but my example is  for a simple ISBN search.
Zvjezdan Patz 09-Sep-2007 05:22
The problem I had was I needed to generate xml on the screen for users to actually see and copy to a file.

I'm generating the xml manually from a php file and the browser kept interpreting the xml...not very helpful.

This is how you get around it:

<?php

$file  = file_get_contents("http://example.com/xml.php?whatever=$whatever");
print nl2br(htmlentities($file));

?>

Prints all my xml quite nicely.
v9 at fakehalo dot us 14-Jul-2007 05:04
I needed this for work/personal use.  Sometimes you'll have a XML string generated as one long string and no line breaks...nusoap in the case of today/work, but there are any other number of possible things that will generate these.  Anyways, this simply takes a long XML string and returns an indented/line-breaked version of the string for display/readability.

<?php
function xmlIndent($str){
    $ret = "";
    $indent = 0;
    $indentInc = 3;
    $noIndent = false;
    while(($l = strpos($str,"<",$i))!==false){
        if($l!=$r && $indent>0){ $ret .= "\n" . str_repeat(" ",$indent) . substr($str,$r,($l-$r)); }
        $i = $l+1;
        $r = strpos($str,">",$i)+1;
        $t = substr($str,$l,($r-$l));
        if(strpos($t,"/")==1){
            $indent -= $indentInc;
            $noIndent = true;
        }
        else if(($r-$l-strpos($t,"/"))==2 || substr($t,0,2)=="<?"){ $noIndent = true; }
        if($indent<0){ $indent = 0; }
        if($ret){ $ret .= "\n"; }
        $ret .= str_repeat(" ",$indent);
        $ret .= $t;
        if(!$noIndent){ $indent += $indentInc; }
        $noIndent = false;
    }
    $ret .= "\n";
    return($ret);
}
?>

(...this was only tested for what i needed at work, could POSSIBLY need additions)
geoffers [at] gmail [dot] com 31-Dec-2006 12:27
Time to add my attempt at a very simple script that parses XML into a structure:

<?php

class Simple_Parser
{
    var $parser;
    var $error_code;
    var $error_string;
    var $current_line;
    var $current_column;
    var $data = array();
    var $datas = array();

    function parse($data)
    {
        $this->parser = xml_parser_create('UTF-8');
        xml_set_object($this->parser, $this);
        xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
        xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
        xml_set_character_data_handler($this->parser, 'cdata');
        if (!xml_parse($this->parser, $data))
        {
            $this->data = array();
            $this->error_code = xml_get_error_code($this->parser);
            $this->error_string = xml_error_string($this->error_code);
            $this->current_line = xml_get_current_line_number($this->parser);
            $this->current_column = xml_get_current_column_number($this->parser);
        }
        else
        {
            $this->data = $this->data['child'];
        }
        xml_parser_free($this->parser);
    }

    function tag_open($parser, $tag, $attribs)
    {
        $this->data['child'][$tag][] = array('data' => '', 'attribs' => $attribs, 'child' => array());
        $this->datas[] =& $this->data;
        $this->data =& $this->data['child'][$tag][count($this->data['child'][$tag])-1];
    }

    function cdata($parser, $cdata)
    {
        $this->data['data'] .= $cdata;
    }

    function tag_close($parser, $tag)
    {
        $this->data =& $this->datas[count($this->datas)-1];
        array_pop($this->datas);
    }
}

$xml_parser = new Simple_Parser;
$xml_parser->parse('<foo><bar>test</bar></foo>');

?>
Didier: dlvb ** free * fr 24-Dec-2006 06:53
Hi !

After parsing the XML and modifying it, I just add a method to rebuild the XML form the internal structure (xmlp->document).
The method xmlp->toXML writes into xmlp->XML attributes. Then, you just have to output it.
I hope it helps.

<?php
class XMLParser {

var $parser;
var $filePath;
var $document;
var $currTag;
var $tagStack;
var $XML;
var $_tag_to_close = false;
var $TAG_ATTRIBUT = 'attr';
var $TAG_DATA = 'data';

function XMLParser($path) {
    $this->parser = xml_parser_create();
    $this->filePath = $path;
    $this->document = array();
    $this->currTag =& $this->document;
    $this->tagStack = array();
    $this->XML = "";
}

function parse() {
    xml_set_object($this->parser, $this);
    xml_set_character_data_handler($this->parser, 'dataHandler');
    xml_set_element_handler($this->parser, 'startHandler', 'endHandler');

   if(!($fp = fopen($this->filePath, "r"))) {
       die("Cannot open XML data file: $this->filePath");
       return false;
     }

    while($data = fread($fp, 4096)) {
        if(!xml_parse($this->parser, $data, feof($fp))) {
            die(sprintf("XML error: %s at line %d",
                xml_error_string(xml_get_error_code($this->parser)),
             xml_get_current_line_number($this->parser)));
      }
    }

    fclose($fp);
    xml_parser_free($this->parser);

    return true;
}

function startHandler($parser, $name, $attribs) {
     if(!isset($this->currTag[$name]))
          $this->currTag[$name] = array();

     $newTag = array();
     if(!empty($attribs))
          $newTag[$this->TAG_ATTRIBUT] = $attribs;
     array_push($this->currTag[$name], $newTag);

     $t =& $this->currTag[$name];
     $this->currTag =& $t[count($t)-1];
     array_push($this->tagStack, $name);
}

function dataHandler($parser, $data) {
    $data = trim($data);

    if(!empty($data)) {
      if(isset($this->currTag[$this->TAG_DATA]))
            $this->currTag[$this->TAG_DATA] .= $data;
      else
            $this->currTag[$this->TAG_DATA] = $data;
    }
}

function endHandler($parser, $name) {
     $this->currTag =& $this->document;
     array_pop($this->tagStack);

     for($i = 0; $i < count($this->tagStack); $i++) {
          $t =& $this->currTag[$this->tagStack[$i]];
          $this->currTag =& $t[count($t)-1];
     }
}

function clearOutput () {
    $this->XML = "";
}

function openTag ($tag) {
    $this->XML.="<".strtolower ($tag);
    $this->_tag_to_close = true;
}

function closeTag () {
    if ($this->_tag_to_close) {
        $this->XML.=">";
        $this->_tag_to_close = false;
    }
}

function closingTag ($tag) {
    $this->XML.="</".strtolower ($tag).">";
}

function output_attributes ($contenu_fils) {
    foreach ($contenu_fils[$this->TAG_ATTRIBUT] as $nomAttribut => $valeur) {
        $this->XML.= " ".strtolower($nomAttribut)."=\"".$valeur."\"";
    }
}

function addData ($texte) {
// to be completed
    $ca  = array ("e", "e", "e", "a");
    $par = array ("&eacute;", "&egrave;", "&ecirc;", "agrave;");
    return htmlspecialchars(str_replace ($ca, $par, $texte), ENT_NOQUOTES);
}

function toXML ($tags="") {
    if ($tags=="") {
        $tags = $this->document;
      $this->clearOutput ();
    }

    foreach ($tags as $tag => $contenu) {
        $this->process ($tag, $contenu);
    }
}

function process ($tag, $contenu) {
     // Pour tous les TAGs
    foreach ($contenu as $indice => $contenu_fils) {
         $this->openTag ($tag);

         // Pour tous les fils (non attribut et non data)
         foreach ($contenu_fils as $tagFils => $fils) {
             switch ($tagFils) {
                 case $this->TAG_ATTRIBUT:
                         $this->output_attributes ($contenu_fils);
                        $this->closeTag ();
                         break;
                 case $this->TAG_DATA:
                        $this->closeTag ();
                         $this->XML.= $this->addData ($contenu_fils [$this->TAG_DATA]);
                         break;
                 default:
                         $this->closeTag ();
                         $this->process ($tagFils, $fils);
                         break;
             }
         }

        $this->closingTag ($tag);
    }
}

}
?>
forquan 29-Jan-2006 12:45
Here's code that will create an associative array from an xml file.  Keys are the tag data and subarrays are formed from attributes and child tags

<?php
$p =& new xmlParser();
$p->parse('/*xml file*/');
print_r($p->output);
?>

<?php
class xmlParser{
   var $xml_obj = null;
   var $output = array();
   var $attrs;

   function xmlParser(){
       $this->xml_obj = xml_parser_create();
       xml_set_object($this->xml_obj,$this);
       xml_set_character_data_handler($this->xml_obj, 'dataHandler');
       xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
   }

   function parse($path){
       if (!($fp = fopen($path, "r"))) {
           die("Cannot open XML data file: $path");
           return false;
       }

       while ($data = fread($fp, 4096)) {
           if (!xml_parse($this->xml_obj, $data, feof($fp))) {
               die(sprintf("XML error: %s at line %d",
               xml_error_string(xml_get_error_code($this->xml_obj)),
               xml_get_current_line_number($this->xml_obj)));
               xml_parser_free($this->xml_obj);
           }
       }

       return true;
   }

   function startHandler($parser, $name, $attribs){
       $_content = array();
       if(!empty($attribs))
         $_content['attrs'] = $attribs;
       array_push($this->output, $_content);
   }

   function dataHandler($parser, $data){
       if(!empty($data) && $data!="\n") {
           $_output_idx = count($this->output) - 1;
           $this->output[$_output_idx]['content'] .= $data;
       }
   }

   function endHandler($parser, $name){
       if(count($this->output) > 1) {
           $_data = array_pop($this->output);
           $_output_idx = count($this->output) - 1;
           $add = array();
           if ($_data['attrs'])
                $add['attrs'] = $_data['attrs'];
           if ($_data['child'])
                $add['child'] = $_data['child'];
           $this->output[$_output_idx]['child'][$_data['content']] = $add;
       }
   }
}
?>
Greg S 17-Nov-2005 05:56
If you need utf8_encode support and configure PHP with --disable-all you will have some trouble. Unfortunately the configure options aren't completely documented. If you need utf8 functions and have everything disabled just recompile PHP with --enable-xml and you should be good to go.
compu_global_hyper_mega_net_2 at yahoo dot com 19-Sep-2004 10:35
The documentation regarding white space was never complete I think.

The XML_OPTION_SKIP_WHITE doesn't appear to do anything.  I want to preserve the newlines in a cdata section.  Setting XML_OPTION_SKIP_WHITE to 0 or false doesn't appear to help.  My character_data_handler is getting called once for each line.  This obviously should be reflected in the documentation as well.  When/how often does the handler get called exactly?  Having to build separate test cases is very time consuming.

Inserting newlines myself in my cdata handler is no good either.  For non actual CDATA sections that cause my handler to get called, long lines are split up in multiple calls.  My handler would not be able to tell the difference whether or not the subsequent calls would be due to the fact that the data is coming from the next line or the fact that some internal buffer is long enough for it to 'flush' out and call the handler.
This behaviour also needs to be properly documented.
odders 19-Mar-2004 07:36
I wrote a simple xml parser mainly to deal with rss version 2. I found lots of examples on the net, but they were all masive and bloated and hard to manipulate.

Output is sent to an array, which holds arrays containg data for each item.

Obviously, you will have to make modifications to the code to suit your needs, but there isnt a lot of code there, so that shouldnt be a problem.

<?php

   $currentElements = array();
   $newsArray = array();

   readXml("./news.xml");

   echo("<pre>");
   print_r($newsArray);
   echo("</pre>");

   // Reads XML file into formatted html
   function readXML($xmlFile)
   {

      $xmlParser = xml_parser_create();

      xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, false);
      xml_set_element_handler($xmlParser, startElement, endElement);
      xml_set_character_data_handler($xmlParser, characterData);

      $fp = fopen($xmlFile, "r");

      while($data = fread($fp, filesize($xmlFile))){
         xml_parse($xmlParser, $data, feof($fp));}

      xml_parser_free($xmlParser);

   }

   // Sets the current XML element, and pushes itself onto the element hierarchy
   function startElement($parser, $name, $attrs)
   {

      global $currentElements, $itemCount;

      array_push($currentElements, $name);

      if($name == "item"){$itemCount += 1;}

   }

   // Prints XML data; finds highlights and links
   function characterData($parser, $data)
   {

      global $currentElements, $newsArray, $itemCount;

      $currentCount = count($currentElements);
      $parentElement = $currentElements[$currentCount-2];
      $thisElement = $currentElements[$currentCount-1];

      if($parentElement == "item"){
         $newsArray[$itemCount-1][$thisElement] = $data;}
      else{
         switch($name){
            case "title":
               break;
            case "link":
               break;
            case "description":
               break;
            case "language":
               break;
            case "item":
               break;}}

   }

   // If the XML element has ended, it is poped off the hierarchy
   function endElement($parser, $name)
   {

      global $currentElements;

      $currentCount = count($currentElements);
      if($currentElements[$currentCount-1] == $name){
         array_pop($currentElements);}

   }

?>
software at serv-a-com dot com 22-Jan-2003 11:08
use:

<?php
while ($data = str_replace("\n","",fread($fp, 4096))){
 // ...
}
?>

instead of:

<?php
while ($data = fread($fp, 4096)) {
  // ...
}
?>

[UPDATE 17-FEB-2003: This post] resulted in some of the visitors e-mailg me on the carriage return stripping issue with questions. I'll try to make the following mumble as brief and easy to understand as possible.

1. Overview of the 4096 fragmentation issue
As you know the following freads the file 4096 bytes at a time (that is 4KB) this is perhaps ok for testing expat and figuring out how things work, but it it rather dangerous in the production environment. Data may not be fully understandable due to fread fragmentation and improperly formatted due to numerous sources(formats) of data contained within (i.e. end of line delimited CDATA).

<?php
while ($data = fread($fp, 4096)) {
  if (!xml_parse($xml_parser, $data, feof($fp))) {
    // ...
  }
}
?>

Sometimes to save time one may want to load it all up into a one big variable and leave all the worries to expat. I think anything under 500 KB is ok (as long as nobody knows about it). Some may argue that larger variables are acceptable or even necessary because of the magic that take place while parsing using xml_parse. Our XML parser(expat) works and can be successfully implemented only when we know what type of XML data we are dealing with, it's average size and structure of general layout and data contained within tags. For example if the tags are followed by a line delimiter like a new line we can read it with fgets in and with minimal effort make sure that no data will be sent to the function that does not end with a end tag. But this require a fair knowledge of the file's preference for storing XML data and tags (and a bit of code between reading data and xml_parse'ing it).
It will save you a headache.

2. Pre Parser Strings and New Line Delimited Data
One important thing to note at this point is that the xml_parse function requires a string variable. You can manipulate the content of any string variable easily as we all know.

A better approach to removing newlines than:

<?php
while ($data = fread($fp, 4096)) {
$data = preg_replace("/\n|\r/","",$data); //flarp
if (!xml_parse($xml_parser, $data, feof($fp))) {...
?>

Above works across all 3 line-delimited text files  (\n, \r, \r\n). But this could potentially (or will most likely) damage or scramble data contained in for example CDATA areas. As far as I am concerned end of line characters should not be used _within_ XML tags. What seems to be the ultimate solution is to pre-parse the loaded data this would require checking the position within the XML document and adding or subtracting (using a in-between fread temporary variable) data based on conditions like: "Is within tag", "Is within CDATA" etc. before fedding it to the parser. This of course opens up a new can of worms (as in parse data for the parser...). (above procedure would take place between fread and xml_parser calls this method would be compatible with the general usage examples on top of the page)

3. The Answer to parsing arbitrary XML and Preprocessor Revisited
You can't just feed any XML document to the parser you constructed and assuming that it will work! You have to know what kind of methods for storing data are used, for example is there a end of line delimited data in the  file ?, Are there any carriage returns in the tags etc... XML files come formatted in different ways some are just a one long string of characters with out any end of line markers others have newlines, carriage returns or both (Microsloth Windows). May or may not contain space and other whitespace between tags. For this reason it is important to what I call Normalize the data before feeding it to the parser. You can perform this with regular expressions or plain old str_replace and concatenation. In many cases this can be done to the file it self sometimes to string data on the fly( as shown in the example above). But I feel it is important to normalize the data before even calling the function to call xml_parse. If you have the ability to access all data before that call you can convert it to what you fell the data should have been in the first place and omit many surprises and expensive regular expression substitution (in a tight spot) while fread'ing the data.
guy at bhaktiandvedanta dot com 27-Sep-2002 09:01
For a simple XML parser you can use this function. It doesn't require any extensions to run.

<?php
// Extracts content from XML tag

function GetElementByName ($xml, $start, $end) {

    global $pos;
    $startpos = strpos($xml, $start);
    if ($startpos === false) {
        return false;
    }
    $endpos = strpos($xml, $end);
    $endpos = $endpos+strlen($end);
    $pos = $endpos;
    $endpos = $endpos-$startpos;
    $endpos = $endpos - strlen($end);
    $tag = substr ($xml, $startpos, $endpos);
    $tag = substr ($tag, strlen($start));

    return $tag;

}

// Open and read xml file. You can replace this with your xml data.

$file = "data.xml";
$pos = 0;
$Nodes = array();

if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}
while ($getline = fread($fp, 4096)) {
    $data = $data . $getline;
}

$count = 0;
$pos = 0;

// Goes throw XML file and creates an array of all <XML_TAG> tags.
while ($node = GetElementByName($data, "<XML_TAG>", "</XML_TAG>")) {
    $Nodes[$count] = $node;
    $count++;
    $data = substr($data, $pos);
}

// Gets infomation from tag siblings.
for ($i=0; $i<$count; $i++) {
$code = GetElementByName($Nodes[$i], "<Code>", "</Code>");
$desc = GetElementByName($Nodes[$i], "<Description>", "</Description>");
$price = GetElementByName($Nodes[$i], "<BasePrice>", "</BasePrice>");
}
?>

Hope this helps! :)
Guy Laor
dmarsh dot NO dot SPAM dot PLEASE at spscc dot ctc dot edu 18-Sep-2002 09:27
Some reference code I am working on as "XML Library" of which I am folding it info an object. Notice the use of the DEFINE:

Mainly Example 1 and parts of 2 & 3 re-written as an object:
--- MyXMLWalk.lib.php ---
<?php

if (!defined("PHPXMLWalk")) {
define("PHPXMLWalk",TRUE);

class XMLWalk {
 var $p; //short for xml parser;
 var $e; //short for element stack/array

  function prl($x,$i=0) {
    ob_start();
    print_r($x);
    $buf=ob_get_contents();
    ob_end_clean();
    return join("\n".str_repeat(" ",$i),split("\n",$buf));
  }

 function XMLWalk() {
  $this->p = xml_parser_create();
  $this->e = array();
  xml_parser_set_option($this->p, XML_OPTION_CASE_FOLDING, true);
  xml_set_element_handler($this->p, array(&$this, "startElement"), array(&$this, "endElement"));
  xml_set_character_data_handler($this->p, array(&$this, "dataElement"));
  register_shutdown_function(array(&$this, "free")); // make a destructor
  }

  function startElement($parser, $name, $attrs) {
    if (count($attrs)>=1) {
      $x = $this->prl($attrs, $this->e[$parser]+6);
    } else {
      $x = "";
    }

    print str_repeat(" ",$this->e[$parser]+0). "$name $x\n";
    $this->e[$parser]++;
    $this->e[$parser]++;
  }

  function dataElement($parser, $data) {
    print str_repeat(" ",$this->e[$parser]+0). htmlspecialchars($data, ENT_QUOTES) ."\n";
  }

  function endElement($parser, $name) {
    $this->e[$parser]--;
    $this->e[$parser]--;
  }
  function parse($data, $fp) {
    if (!xml_parse($this->p, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($this->p)),
                    xml_get_current_line_number($this->p)));
    }
  }

  function free() {
    xml_parser_free($this->p);
  }

} // end of class

} // end of define

?>

--- end of file ---

Calling code:
<?php

...

require("MyXMLWalk.lib.php");

$file = "x.xml";

$xme = new XMLWalk;

if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
  $xme->parse($data, $fp);
}

...
?>
jon at gettys dot org 14-Aug-2002 10:59
[Editor's note: see also xml_parse_into_struct().]

Very simple routine to convert an XML file into a PHP structure. $obj->xml contains the resulting PHP structure. I would be interested if someone could suggest a cleaner method than the evals I am using.

<?php
$filename = 'sample.xml';
$obj->tree = '$obj->xml';
$obj->xml = '';

function startElement($parser, $name, $attrs) {
    global $obj;

    // If var already defined, make array
    eval('$test=isset('.$obj->tree.'->'.$name.');');
    if ($test) {
      eval('$tmp='.$obj->tree.'->'.$name.';');
      eval('$arr=is_array('.$obj->tree.'->'.$name.');');
      if (!$arr) {
        eval('unset('.$obj->tree.'->'.$name.');');
        eval($obj->tree.'->'.$name.'[0]=$tmp;');
        $cnt = 1;
      }
      else {
        eval('$cnt=count('.$obj->tree.'->'.$name.');');
      }

      $obj->tree .= '->'.$name."[$cnt]";
    }
    else {
      $obj->tree .= '->'.$name;
    }
    if (count($attrs)) {
        eval($obj->tree.'->attr=$attrs;');
    }
}

function endElement($parser, $name) {
    global $obj;
    // Strip off last ->
    for($a=strlen($obj->tree);$a>0;$a--) {
        if (substr($obj->tree, $a, 2) == '->') {
            $obj->tree = substr($obj->tree, 0, $a);
            break;
        }
    }
}

function characterData($parser, $data) {
    global $obj;

    eval($obj->tree.'->data=\''.$data.'\';');
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($filename, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);
print_r($obj->xml);
return 0;

?>
hans dot schneider at bbdo-interone dot de 24-Jan-2002 05:43
I had to TRIM the data when I passed one large String containig a wellformed XML-File to xml_parse. The String was read by CURL, which aparently put a BLANK at the end of the String. This BLANK produced a "XML not wellformed"-Error in xml_parse!
sam at cwa dot co dot nz 28-Sep-2000 04:39
I've discovered some unusual behaviour in this API when ampersand entities are parsed in cdata; for some reason the parser breaks up the section around the entities, and calls the handler repeated times for each of the sections. If you don't allow for this oddity and you are trying to put the cdata into a variable, only the last part will be stored.

You can get around this with a line like:

$foo .= $cdata;

If the handler is called several times from the same tag, it will append them, rather than rewriting the variable each time. If the entire cdata section is returned, it doesn't matter.

May happen for other entities, but I haven't investigated.

Took me a while to figure out what was happening; hope this saves someone else the trouble.
Daniel dot Rendall at btinternet dot com 07-Jul-1999 07:21
When using the XML parser, make sure you're not using the magic quotes option (e.g. use set_magic_quotes_runtime(0) if it's not the compiled default), otherwise you'll get 'not well-formed' errors when dealing with tags with attributes set in them.


Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1

  • ≪ XML Parser 함수 목록
  • utf8_encode ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1

utf8_decode

(PHP 4, PHP 5, PHP 7)

utf8_decode — Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1

설명

string utf8_decode ( string $data )

This function converts the string data from the UTF-8 encoding to ISO-8859-1. Bytes in the string which are not valid UTF-8, and UTF-8 characters which do not exist in ISO-8859-1 (that is, characters above U+00FF) are replaced with ?.

Note:

Many web pages marked as using the ISO-8859-1 character encoding actually use the similar Windows-1252 encoding, and web browsers will interpret ISO-8859-1 web pages as Windows-1252. Windows-1252 features additional printable characters, such as the Euro sign (€) and curly quotes (" "), instead of certain ISO-8859-1 control characters. This function will not convert such Windows-1252 characters correctly. Use a different function if Windows-1252 conversion is required.

인수

data

A UTF-8 encoded string.

반환값

Returns the ISO-8859-1 translation of data.

참고

  • utf8_encode() - Encodes an ISO-8859-1 string to UTF-8 - Performs the reverse conversion
  • mb_convert_encoding() - Convert character encoding - Converts between various character encodings, including UTF-8, ISO-8859-1 and Windows-1252
  • iconv() - Convert string to requested character encoding - Converts between various character encodings
  • recode_string() - Recode a string according to a recode request - Converts between various character encodings

User Contributed Notes

kode68 03-May-2016 03:09
Update Answer from okx dot oliver dot koenig at gmail dot com for PHP 5.6 since e/ modifier is depreciated

// This finally helped me to do the job, thanks to Blackbit, had to modify deprecated ereg:
// original comment: "Squirrelmail contains a nice function in the sources to convert unicode to entities:"

function charset_decode_utf_8($string)
    {
        /* Only do the slow convert if there are 8-bit characters */
        if ( !preg_match("/[\200-\237]/", $string) && !preg_match("/[\241-\377]/", $string) )
               return $string;

        // decode three byte unicode characters
          $string = preg_replace_callback("/([\340-\357])([\200-\277])([\200-\277])/",
                    create_function ('$matches', 'return \'&#\'.((ord($matches[1])-224)*4096+(ord($matches[2])-128)*64+(ord($matches[3])-128)).\';\';'),
                    $string);

        // decode two byte unicode characters
          $string = preg_replace_callback("/([\300-\337])([\200-\277])/",
                    create_function ('$matches', 'return \'&#\'.((ord($matches[1])-192)*64+(ord($matches[2])-128)).\';\';'),
                    $string);

        return $string;
    }

Enjoy
solomon_closson at yahoo dot com 22-Oct-2015 04:11
Getting rid of all unicode characters in a string seems fairly simple to me using the following:

$str = str_replace('?', '', utf8_decode($str));

Not sure why the long, dragged-out functions for this are needed.  The above seems to work just fine.
okx dot oliver dot koenig at gmail dot com 08-Feb-2015 08:48
// This finally helped me to do the job, thanks to Blackbit, had to modify deprecated ereg:
// original comment: "Squirrelmail contains a nice function in the sources to convert unicode to entities:"

function charset_decode_utf_8 ($string) {
    /* Only do the slow convert if there are 8-bit characters */
    /* avoid using 0xA0 (\240) in ereg ranges. RH73 does not like that */
    if (!preg_match("/[\200-\237]/", $string)
     && !preg_match("/[\241-\377]/", $string)
    ) {
        return $string;
    }

    // decode three byte unicode characters
    $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",
        "'&#'.((ord('\\1')-224)*4096 + (ord('\\2')-128)*64 + (ord('\\3')-128)).';'",
        $string
    );

    // decode two byte unicode characters
    $string = preg_replace("/([\300-\337])([\200-\277])/e",
        "'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'",
        $string
    );

    return $string;
}
sashott at gmail dot com 04-Dec-2014 08:57
Use of utf8_decode was not enough for me by get page content from another site. Problem appear by different alphabet from standard latin. As example some chars (corresponding to HTML codes &bdquo; , &nbsp; and others) are converted to "?" or "xA0" (hex value). You need to make some conversion before execute utf8_decode. And you can not replace simple, that they can be part of 2 bytes code for a char (UTF-8 use 2 bytes). Next is for cyrillic alphabet, but for other must be very close.

function convertMethod($text){
    //Problem is that utf8_decode convert HTML chars for &bdquo; and other to ? or &nbsp; to \xA0. And you can not replace, that they are in some char bytes and you broke cyrillic (or other alphabet) chars.
    $problem_enc=array(
        'euro',
        'sbquo',
        'bdquo',
        'hellip',
        'dagger',
        'Dagger',
        'permil',
        'lsaquo',
        'lsquo',
        'rsquo',
        'ldquo',
        'rdquo',
        'bull',
        'ndash',
        'mdash',
        'trade',
        'rsquo',
        'brvbar',
        'copy',
        'laquo',
        'reg',
        'plusmn',
        'micro',
        'para',
        'middot',
        'raquo',
        'nbsp'
    );
    $text=mb_convert_encoding($text,'HTML-ENTITIES','UTF-8');
    $text=preg_replace('#(?<!\&ETH;)\&('.implode('|',$problem_enc).');#s','--amp{$1}',$text);
    $text=mb_convert_encoding($text,'UTF-8','HTML-ENTITIES');
    $text=utf8_decode($text);
    $text=mb_convert_encoding($text,'HTML-ENTITIES','UTF-8');
    $text=preg_replace('#\-\-amp\{([^\}]+)\}#su','&$1;',$text);
    $text=mb_convert_encoding($text,'UTF-8','HTML-ENTITIES');
    return $text;
}

If this don't work, try to set "die($text);" on some places to look, what is happen to this row. Is better to test with long text. It is very possible to broke other alphabet character. In this case, it is very possible, that for you alphabet set "&ETH;" is not the right one. You need to set "die($text);" after this preg_replace and look HTML code for character before set "--amp".
Nitrogen 01-Apr-2013 09:37
Hi all. I have written this comprehensive UTF-8 decoder as I needed a way to split any UTF-8 string
into nice and useable pieces of information, such as an array of code points that make up the input
string regardless if it's UTF-8 or not.

Here's what's good about this function:
1) It splits a UTF-8 encoded string into an array of code points.
2) It supports from 1 byte (7 bit) to 6 byte (31 bit) UTF-8 characters.
3) It checks the validity of the UTF-8 input string and provides an array of erroneous UTF-8 offsets.
4) Provides you with the character length of the UTF-8 string.

<?php
function get_utf_8($input, $include_raw = FALSE) {
  $result = array(
    'length'=>0,
    'errorbytes'=>array(),
    'codepoints'=>array()
  );
  $codepoints = array();
  $len = strlen($input);

  for($i=0;$i<$len;$i++) {
    $c = ord($input{$i});
    $msb = ($c>>7)&1;
    $bytes = 1;

    if(!$msb)
      $codepoints[]= $include_raw?array($c, chr($c)):$c;
    else {
      for($j=0;$j<8&&(($c>>(7-$j))&1);$j++); // how many bytes represent this Unicode character? $j
      if(($j>=2 && $j<=6) && $len>$i+$j-1) { // valid range? is there $j-1 extra bytes?
        $code = $c&pow(2, 7-$j)-1;
        for($k=1;$k<$j;$k++) {
          if(((ord($input{$i+$k})>>6)&3)==2)
            $code = ($code<<6)|(ord($input{$i+$k})&63);
          else {
            $result['errorbytes'][]= $i;
            continue 2;
          }
        }
        $codepoints[]= $include_raw?array($code, substr($input, $i, $j)):$code;
        $i+=$j-1;
      }
      else
        $result['errorbytes'][]= $i;
    }
    $result['length']++;
  }
  $result['codepoints'] = $codepoints;
  return($result);
}
?>

Some examples of the result:

<?php
$r = get_utf_8('迎接。');
/*
Array(
  [length] => 3
  [errorbytes] => Array(
  )
  [codepoints] => Array(
    [0] => 36814
    [1] => 25509
    [2] => 12290
  )
)
*/

// Using the $include_raw parameter includes the raw UTF-8 bytes...
$r = get_utf_8('迎接。', TRUE);
/*
Array(
  [length] => 3
  [errorbytes] => Array(
  )
  [codepoints] => Array(
    [0] => Array(
      [0] => 36814
      [1] => 迎
    )
    [1] => Array(
      [0] => 25509
      [1] => 接
    )
    [2] => Array(
      [0] => 12290
      [1] => 。
    )
  )
)
*/
?>
christoffer 29-Oct-2012 07:35
The preferred way to use this on an array would be with the built in PHP function "array_map()", as for example:
$array = array_map("utf8_decode", $array);
Michael Pfister 12-Sep-2012 10:10
I personally prefer this method for decoding an array:

<?php
private function utf8DecodeArray(&$item, $key) {
    $item = utf8_decode($item);
}
?>

Then just call it like this within the same class:
array_walk_recursive($YOURARRAY, array(&$this, 'utf8DecodeArray'));

This will replace your array with the utf8 decoded one.
chris at deliens dot be 18-Apr-2012 09:37
I didn't see any post showing a way to recursively utf8_decode an array.
So here's one I've just built for this purpose:

function utf8DecodeArray($array) {
    $utf8DecodedArray = array();

    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $utf8DecodedArray[$key] = utf8DecodeArray($value);
            continue;
        }

        $utf8DecodedArray[$key] = utf8_decode($value);
    }

    return $utf8DecodedArray;
}
deceze at gmail dot com 15-Jul-2011 04:01
Please note that utf8_decode simply converts a string encoded in UTF-8 to ISO-8859-1. A more appropriate name for it would be utf8_to_iso88591. If your text is already encoded in ISO-8859-1, you do not need this function. If you don't want to use ISO-8859-1, you do not need this function.

Note that UTF-8 can represent many more characters than ISO-8859-1. Trying to convert a UTF-8 string that contains characters that can't be represented in ISO-8859-1 to ISO-8859-1 will garble your text and/or cause characters to go missing. Trying to convert text that is not encoded in UTF-8 using this function will most likely garble the text.

If you need to convert any text from any encoding to any other encoding, look at iconv() instead.
gabriel arobase gabsoftware dot com 12-Jun-2011 05:41
If you want to retrieve some UTF-8 data from your database, you don't need utf8_decode().

Simply do the following query before any SELECT :

$result = mysql_query("SET NAMES utf8");
mkluczka at gmail dot com 16-Sep-2010 01:36
In jQuery AJAX requests, latin2 specific chars have 2B length, and common chars have 1B length.

PHP functions for UTF8->latin2 conversion didn't work for me, so I wrote this simple function
to convert from jQuery AJAX encoding to ISO-8859-2 (latin2).

<?php
function jquery2iso($in)
{
  $CONV = array();
  $CONV['c4']['85'] = '?';
  $CONV['c4']['84'] = '?';
  $CONV['c4']['87'] = '?';
  $CONV['c4']['86'] = '?';
  $CONV['c4']['99'] = '?';
  $CONV['c4']['98'] = '?';
  $CONV['c5']['82'] = 'ł';
  $CONV['c5']['81'] = 'Ł';
  $CONV['c4']['84'] = '?';
  $CONV['c4']['83'] = '?';
  $CONV['c3']['b3'] = 'o';
  $CONV['c3']['93'] = 'O';
  $CONV['c5']['9b'] = '?';
  $CONV['c5']['9a'] = '?';
  $CONV['c5']['ba'] = '?';
  $CONV['c5']['b9'] = '?';
  $CONV['c5']['bc'] = '?';
  $CONV['c5']['bb'] = '?';

  $i=0;
  $out = '';
  while($i<strlen($in))
  {
    if(array_key_exists(bin2hex($in[$i]), $CONV))
    {
      $out .= $CONV[bin2hex($in[$i])][bin2hex($in[$i+1])];
      $i += 2;
    }
    else
    {
      $out .= $in[$i];
      $i += 1;
    }
  }

  return $out;
}

?>
andrew 25-Nov-2009 05:13
a simple function to convert encoding that appears mixed.

<?php
$trans = new Latin1UTF8();

$mixed = "MIXED TEXT INPUT";

print "Original: ".$mixed;
print "Latin1:   ".$trans->mixed_to_latin1($mixed);
print "UTF-8:    ".$trans->mixed_to_utf8($mixed);

class Latin1UTF8 {

    private $latin1_to_utf8;
    private $utf8_to_latin1;
    public function __construct() {
        for($i=32; $i<=255; $i++) {
            $this->latin1_to_utf8[chr($i)] = utf8_encode(chr($i));
            $this->utf8_to_latin1[utf8_encode(chr($i))] = chr($i);
        }
    }

    public function mixed_to_latin1($text) {
        foreach( $this->utf8_to_latin1 as $key => $val ) {
            $text = str_replace($key, $val, $text);
        }
        return $text;
    }

    public function mixed_to_utf8($text) {
        return utf8_encode($this->mixed_to_latin1($text));
    }
}
?>
Anonymous 08-Aug-2009 11:25
In addition to Blackbit's note below, here's my shot at converting UTF-8 strings to HTML entities.

<?php $encoded = preg_replace('/([\200-\277])/e', "'&#'.(ord('\\1')).';'", $utf8str); ?>

This code converts one byte UTF-8 to HTML entities while below you'll find conversions for two byte and three byte UTF-8.
pedro at pontonet dot com 06-May-2009 12:25
I've modified above utf8_array_decode to utf8_array_encode. The main difference is the later encodes a multi-dimension array.

Here goes the code:

<?php
function utf8_array_encode($input)
{
    $return = array();

    foreach ($input as $key => $val)
    {
        if( is_array($val) )
        {
            $return[$key] = utf8_array_encode($val);
        }
        else
        {
            $return[$key] = utf8_encode($val);
        }
    }
    return $return;
}
?>

Enjoy it!

Pedro Bastos
ytak at gmx dot net 30-Jan-2009 06:35
If you running Gentoo Linux and encounter problems with some PHP5 applications saying:
Call to undefined function: utf8_decode()
Try reemerge PHP4 with 'xml' flag enabled.
info at vanylla dot it 26-Jan-2009 09:21
IMPORTANT: when converting UTF8 data that contains the EURO sign DON'T USE utf_decode function.

utf_decode converts the data into ISO-8859-1 charset. But ISO-8859-1 charset does not contain the EURO sign, therefor the EURO sign will be converted into a question mark character '?'

In order to convert properly UTF8 data with EURO sign you must use:

iconv("UTF-8", "CP1252", $data)
Blackbit 12-Aug-2008 01:40
Squirrelmail contains a nice function in the sources to convert unicode to entities:

<?php
function charset_decode_utf_8 ($string) {
      /* Only do the slow convert if there are 8-bit characters */
    /* avoid using 0xA0 (\240) in ereg ranges. RH73 does not like that */
    if (! ereg("[\200-\237]", $string) and ! ereg("[\241-\377]", $string))
        return $string;

    // decode three byte unicode characters
    $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",        \
    "'&#'.((ord('\\1')-224)*4096 + (ord('\\2')-128)*64 + (ord('\\3')-128)).';'",    \
    $string);

    // decode two byte unicode characters
    $string = preg_replace("/([\300-\337])([\200-\277])/e", \
    "'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'", \
    $string);

    return $string;
}
?>
punchivan at gmail dot com 24-Jun-2008 11:13
EY! the bug is not in the function 'utf8_decode'. The bug is in the function 'mb_detect_encoding'. If you put a word with a special char at the end like this 'accentue', that will lead to a wrong result (UTF-8) but if you put another char at the end like this 'accentuee' you will get it right. So you should always add a ISO-8859-1 character to your string for this check. My advise is to use a blank space.
I´ve tried it and it works!

function ISO_convert($array)
{
    $array_temp = array();

    foreach($array as $name => $value)
    {
        if(is_array($value))
          $array_temp[(mb_detect_encoding($name." ",'UTF-8,ISO-8859-1') == 'UTF-8' ? utf8_decode($name) : $name )] = ISO_convert($value);
        else
          $array_temp[(mb_detect_encoding($name." ",'UTF-8,ISO-8859-1') == 'UTF-8' ? utf8_decode($name) : $name )] = (mb_detect_encoding($value." ",'UTF-8,ISO-8859-1') == 'UTF-8' ? utf8_decode($value) : $value );
    }

    return $array_temp;
}
webmaster at lapstore dot de 19-Jun-2008 05:11
[EDITED BY cataphract AT php DOT net - NOTE: This has been fixed
in PHP 5.3.4 and 5.2.15.]

Warning!
This function contains a possible security risk when you try to convert escaped strings (see addslashes() and related functions).
It reacts nasty on broken multibyte sequences. In UTF-8, follow-up bytes ALWAYS have the binary pattern 10xxxxxx, but this fact is not handled by utf8_decode in the way you would expect: If you pass a start byte (110xxxxx, 1110xxxx, 11110xxx - or even invalid sequences like 11111100), followed by one or more non-multibyte chars (0xxxxxxx), the start sequence "char" will be replaced by '?' (0x3F) and up to three following chars will disappear even if they are single-byte-chars (0xxxxxxx). So if you escape a string with a typical escape char like backslash, you would expect that your escaping would always survive a call to utf8decode because the escape char is in the assumed safe ascii range 0-127, but that is NOT the case!
Try things like utf8_decode("test: u\\\"123456") to check it out.
To avoid problems take care that string-escaping always is the last step of data manipulation when you depend on leak-proof escaping.
juantxito at example dot com 28-May-2008 10:26
To decode the values and the keys, i think this would work

 function utf8_array_decode($input){

    $return = array();

        foreach ($input as $key => $val) {
        $k = utf8_decode($key);
                $return[$k] = utf8_decode($val);
            }
            return $return;
        }
phpnet at freshsite dot de 09-May-2008 10:08
I didn't find an utf8_array_decode. This one only decodes the values, not the keys.

        function utf8_array_decode($input){
            foreach ($input as $key => $val) {
                $input[$key] = utf8_decode($val);
            }
            return $input;
        }
haugas at gmail dot com 08-May-2008 11:11
If you don't know exactly, how many times your string is encoded, you can use this function:

<?php

function _utf8_decode($string)
{
  $tmp = $string;
  $count = 0;
  while (mb_detect_encoding($tmp)=="UTF-8")
  {
    $tmp = utf8_decode($tmp);
    $count++;
  }

  for ($i = 0; $i < $count-1 ; $i++)
  {
    $string = utf8_decode($string);

  }
  return $string;

}

?>
lukasz dot mlodzik at gmail dot com 05-Mar-2008 01:46
Update to MARC13 function utf2iso()
I'm using it to handle AJAX POST calls.
Despite using
http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'; charset='utf-8');
it still code Polish letters using UTF-16

This is only for Polish letters:

<?php
function utf16_2_utf8 ($nowytekst) {
        $nowytekst = str_replace('%u0104','?',$nowytekst);    //?
        $nowytekst = str_replace('%u0106','?',$nowytekst);    //?
        $nowytekst = str_replace('%u0118','?',$nowytekst);    //?
        $nowytekst = str_replace('%u0141','Ł',$nowytekst);    //Ł
        $nowytekst = str_replace('%u0143','?',$nowytekst);    //?
        $nowytekst = str_replace('%u00D3','O',$nowytekst);    //O
        $nowytekst = str_replace('%u015A','?',$nowytekst);    //?
        $nowytekst = str_replace('%u0179','?',$nowytekst);    //?
        $nowytekst = str_replace('%u017B','?',$nowytekst);    //?

        $nowytekst = str_replace('%u0105','?',$nowytekst);    //?
        $nowytekst = str_replace('%u0107','?',$nowytekst);    //?
        $nowytekst = str_replace('%u0119','?',$nowytekst);    //?
        $nowytekst = str_replace('%u0142','ł',$nowytekst);    //ł
        $nowytekst = str_replace('%u0144','?',$nowytekst);    //?
        $nowytekst = str_replace('%u00F3','o',$nowytekst);    //o
        $nowytekst = str_replace('%u015B','?',$nowytekst);    //?
        $nowytekst = str_replace('%u017A','?',$nowytekst);    //?
        $nowytekst = str_replace('%u017C','?',$nowytekst);    //?
   return ($nowytekst);
   }
?>

Everything goes smooth, but it doesn't change '%u00D3','O' and '%u00F3','o'. I dont have idea what to do with that.

Remember! File must be saved in UTF-8 coding.
tacchete at gmail dot com 13-Dec-2007 01:36
Known problem with Byte Order Mark (BOM) and header() in pages of a site.

For example at sending headings or to a dynamic conclusion in other coding distinct from UTF-8 by means of XSLT (<xsl:output encoding="windows-1251"/>).

To clean all symbols BOM from the text of page:

1. exclude BOM from the main file;
2. write down function of a return call for the buffer

<?php
header('content-type: text/html; charset: utf-8');
ob_start('ob');
function ob($buffer)
{
    return str_replace("\xef\xbb\xbf", '', $buffer);
}
?>

it will exclude BOM from a code of the connected files;
3. do not experience for BOM in connected files;
4. be pleased.
ludvig dot ericson at gmail dot com 15-Jul-2007 07:52
A better way to convert would be to use iconv, see http://www.php.net/iconv -- example:

<?php
$myUnicodeString = "Aao";
echo iconv("UTF-8", "ISO-8859-1", $myUnicodeString);
?>

Above would echo out the given variable in ISO-8859-1 encoding, you may replace it with whatever you prefer.

Another solution to the issue of misdisplayed glyphs is to simply send the document as UTF-8, and of course send UTF-8 data:

<?php
# Replace text/html with whatever MIME-type you prefer.
header("Content-Type: text/html; CHARSET=ks_c_5601-1987");
?>
MARC13 07-Jul-2007 04:50
I did this function to convert data from AJAX call to insert to my database.
It converts UTF-8 from XMLHttpRequest() to ISO-8859-2 that I use in LATIN2 MySQL database.

<?php
function utf2iso($tekst)
{
        $nowytekst = str_replace("%u0104","\xA1",$tekst);    //?
        $nowytekst = str_replace("%u0106","\xC6",$nowytekst);    //?
        $nowytekst = str_replace("%u0118","\xCA",$nowytekst);    //?
        $nowytekst = str_replace("%u0141","\xA3",$nowytekst);    //Ł
        $nowytekst = str_replace("%u0143","\xD1",$nowytekst);    //?
        $nowytekst = str_replace("%u00D3","\xD3",$nowytekst);    //O
        $nowytekst = str_replace("%u015A","\xA6",$nowytekst);    //?
        $nowytekst = str_replace("%u0179","\xAC",$nowytekst);    //?
        $nowytekst = str_replace("%u017B","\xAF",$nowytekst);    //?

        $nowytekst = str_replace("%u0105","\xB1",$nowytekst);    //?
        $nowytekst = str_replace("%u0107","\xE6",$nowytekst);    //?
        $nowytekst = str_replace("%u0119","\xEA",$nowytekst);    //?
        $nowytekst = str_replace("%u0142","\xB3",$nowytekst);    //ł
        $nowytekst = str_replace("%u0144","\xF1",$nowytekst);    //?
        $nowytekst = str_replace("%u00D4","\xF3",$nowytekst);    //o
        $nowytekst = str_replace("%u015B","\xB6",$nowytekst);    //?
        $nowytekst = str_replace("%u017A","\xBC",$nowytekst);    //?
        $nowytekst = str_replace("%u017C","\xBF",$nowytekst);    //?

    return ($nowytekst);
}
?>

In my case also the code file that deals with AJAX calls must be in UTF-8 coding.
visus at portsonline dot net 23-Jun-2007 06:08
Following code helped me with mixed (UTF8+ISO-8859-1(x)) encodings. In this case, I have template files made and maintained by designers who do not care about encoding and MySQL data in utf8_binary_ci encoded tables.

<?php

class Helper
{
    function strSplit($text, $split = 1)
    {
        if (!is_string($text)) return false;
        if (!is_numeric($split) && $split < 1) return false;

        $len = strlen($text);

        $array = array();

        $i = 0;

        while ($i < $len)
        {
            $key = NULL;

            for ($j = 0; $j < $split; $j += 1)
            {
                $key .= $text{$i};

                $i += 1;
            }

            $array[] = $key;
        }

        return $array;
    }

    function UTF8ToHTML($str)
    {
        $search = array();
        $search[] = "/([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)/e";
        $search[] = "/&#228;/";
        $search[] = "/&#246;/";
        $search[] = "/&#252;/";
        $search[] = "/&#196;/";
        $search[] = "/&#214;/";
        $search[] = "/&#220;/";
        $search[] = "/&#223;/";

        $replace = array();
        $replace[] = 'Helper::_UTF8ToHTML("\\1")';
        $replace[] = "a";
        $replace[] = "o";
        $replace[] = "u";
        $replace[] = "A";
        $replace[] = "O";
        $replace[] = "u";
        $replace[] = "ß";

        $str = preg_replace($search, $replace, $str);

        return $str;
    }

    function _UTF8ToHTML($str)
    {
        $ret = 0;

        foreach((Helper::strSplit(strrev(chr((ord($str{0}) % 252 % 248 % 240 % 224 % 192) + 128).substr($str, 1)))) as $k => $v)
            $ret += (ord($v) % 128) * pow(64, $k);
        return "&#".$ret.";";
    }
}

// Usage example:

$tpl = file_get_contents("template.tpl");
/* ... */
$row = mysql_fetch_assoc($result);

print(Helper::UTF8ToHTML(str_replace("{VAR}", $row['var'], $tpl)));

?>
luka8088 at gmail dot com 22-Jun-2007 04:03
simple UTF-8 to HTML conversion:

function utf8_to_html ($data)
    {
    return preg_replace("/([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)/e", '_utf8_to_html("\\1")', $data);
    }

function _utf8_to_html ($data)
    {
    $ret = 0;
    foreach((str_split(strrev(chr((ord($data{0}) % 252 % 248 % 240 % 224 % 192) + 128) . substr($data, 1)))) as $k => $v)
        $ret += (ord($v) % 128) * pow(64, $k);
    return "&#$ret;";
    }

Example:
echo utf8_to_html("a b ? ? ? こ に ち わ ()[]{}!#$?*");

Output:
a b &#269; &#263; &#382; &#12371; &#12395; &#12385; &#12431; ()[]{}!#$?*
Sadi 13-Jun-2007 12:38
Once again about polish letters. If you use fananf's solution, make sure that PHP file is coded with cp1250 or else it won't work. It's quite obvious, however I spent some time before I finally figured that out, so I thought I post it here.
alexlevin at kvadro dot net 21-May-2007 05:20
If you running Gentoo Linux and encounter problems with some PHP4 applications saying:
Call to undefined function: utf8_decode()
Try reemerge PHP4 with 'expat' flag enabled.
ahmed dot adaileh at gmail dot com 08-Mar-2007 12:32
I searched a lot everywhere to find a suitable function which converts my UTF8 characters to the windows-1250 charset for Polish language, but couldn't find anything :(

Following is a function which does that:

function show_polish ($text) {
 $text = str_replace("A?", '&#260;', $text); //?
 $text = str_replace("A†", '&#262;', $text); //?
 $text = str_replace("A?", '&#280;', $text); //?
 $text = str_replace("A?", '&#321;', $text); //Ł
 $text = str_replace("A?", '&#323;', $text); //?
 $text = str_replace("A"", '&#211;', $text); //O
 $text = str_replace("A?", '&#346;', $text); //?
 $text = str_replace("A¹", '&#377;', $text); //?
 $text = str_replace("A≫", '&#379;', $text); //?
 $text = str_replace("A...", '&#261;', $text); //?
 $text = str_replace("A‡", '&#263;', $text); //?
 $text = str_replace("A™", '&#281;', $text); //?
 $text = str_replace("A?", '&#322;', $text); //ł
 $text = str_replace("A?", '&#324;', $text); //?
 $text = str_replace("A³", '&#243;', $text); //o
 $text = str_replace("A?", '&#347;', $text); //?
 $text = str_replace("Aº", '&#378;', $text); //?
 $text = str_replace("A¼", '&#380;', $text); //?

return $text;
}

You can refer to http://hermes.umcs.lublin.pl/~awmarcz/awm/info/pl-codes.htm
if you want to use HTML hex. code rather than HTML dec. code which I used in my function.
fananf at nerdshack dot com 05-Mar-2007 04:22
Comment to AJGORS reply from 28-Dec-2006 02:38:

You have used twice "?" instead of "?".

Correct code should be:

ISO version:

function utf82iso88592($text) {
 $text = str_replace("\xC4\x85", '±', $text);
 $text = str_replace("\xC4\x84", 'ˇ', $text);
 $text = str_replace("\xC4\x87", '?', $text);
 $text = str_replace("\xC4\x86", '?', $text);
 $text = str_replace("\xC4\x99", '?', $text);
 $text = str_replace("\xC4\x98", '?', $text);
 $text = str_replace("\xC5\x82", 'ł', $text);
 $text = str_replace("\xC5\x81", 'Ł', $text);
 $text = str_replace("\xC3\xB3", 'o', $text);
 $text = str_replace("\xC3\x93", 'O', $text);
 $text = str_replace("\xC5\x9B", '¶', $text);
 $text = str_replace("\xC5\x9A", '|', $text);
 $text = str_replace("\xC5\xBC", '?', $text);
 $text = str_replace("\xC5\xBB", '?', $text);
 $text = str_replace("\xC5\xBA", '?', $text);
 $text = str_replace("\xC5\xB9", '￢', $text);
 $text = str_replace("\xc5\x84", '?', $text);
 $text = str_replace("\xc5\x83", '?', $text);

return $text;
}

CP version:

function utf82iso88592($text) {
 $text = str_replace("\xC4\x85", '?', $text);
 $text = str_replace("\xC4\x84", '?', $text);
 $text = str_replace("\xC4\x87", '?', $text);
 $text = str_replace("\xC4\x86", '?', $text);
 $text = str_replace("\xC4\x99", '?', $text);
 $text = str_replace("\xC4\x98", '?', $text);
 $text = str_replace("\xC5\x82", 'ł', $text);
 $text = str_replace("\xC5\x81", 'Ł', $text);
 $text = str_replace("\xC3\xB3", 'o', $text);
 $text = str_replace("\xC3\x93", 'O', $text);
 $text = str_replace("\xC5\x9B", '?', $text);
 $text = str_replace("\xC5\x9A", '?', $text);
 $text = str_replace("\xC5\xBC", '?', $text);
 $text = str_replace("\xC5\xBB", '?', $text);
 $text = str_replace("\xC5\xBA", '?', $text);
 $text = str_replace("\xC5\xB9", '?', $text);
 $text = str_replace("\xc5\x84", '?', $text);
 $text = str_replace("\xc5\x83", '?', $text);

return $text;
}
sam 06-Feb-2007 05:20
In addition to yannikh's note, to convert a hex utf8 string

<?php

echo utf8_decode("\x61\xc3\xb6\x61");
// works as expected

$abc="61c3b661";
$newstr = "";
$l = strlen($abc);
for ($i=0;$i<$l;$i+=2){
    $newstr .= "\x".$abc[$i].$abc[$i+1];
}
echo utf8_decode($newstr);
// or varieties  of "\x": "\\x" etc does NOT output what you want

echo utf8_decode(pack('H*',$abc));
// this outputs the correct string, like the first line.

?>
Ajgor 28-Dec-2006 02:38
small upgrade for polish decoding:

function utf82iso88592($text) {
 $text = str_replace("\xC4\x85", '?', $text);
 $text = str_replace("\xC4\x84", '?', $text);
 $text = str_replace("\xC4\x87", '?', $text);
 $text = str_replace("\xC4\x86", '?', $text);
 $text = str_replace("\xC4\x99", '?', $text);
 $text = str_replace("\xC4\x98", '?', $text);
 $text = str_replace("\xC5\x82", 'ł', $text);
 $text = str_replace("\xC5\x81", 'Ł', $text);
 $text = str_replace("\xC3\xB3", 'o', $text);
 $text = str_replace("\xC3\x93", 'O', $text);
 $text = str_replace("\xC5\x9B", '?', $text);
 $text = str_replace("\xC5\x9A", '?', $text);
 $text = str_replace("\xC5\xBC", '?', $text);
 $text = str_replace("\xC5\xBB", '?', $text);
 $text = str_replace("\xC5\xBA", '?', $text);
 $text = str_replace("\xC5\xB9", '?', $text);
 $text = str_replace("\xc5\x84", '?', $text);
 $text = str_replace("\xc5\x83", '?', $text);

return $text;
} // utf82iso88592
paul.hayes at entropedia.co.uk 27-Oct-2006 04:10
I noticed that the utf-8 to html functions below are only for 2 byte long codes. Well I wanted 3 byte support (sorry haven't done 4, 5 or 6). Also I noticed the concatination of the character codes did have the hex prefix 0x and so failed with the large 2 byte codes)

<?
  public function utf2html (&$str) {

    $ret = "";
    $max = strlen($str);
    $last = 0;  // keeps the index of the last regular character
    for ($i=0; $i<$max; $i++) {
        $c = $str{$i};
        $c1 = ord($c);
        if ($c1>>5 == 6) {  // 110x xxxx, 110 prefix for 2 bytes unicode
            $ret .= substr($str, $last, $i-$last); // append all the regular characters we've passed
            $c1 &= 31; // remove the 3 bit two bytes prefix
            $c2 = ord($str{++$i}); // the next byte
            $c2 &= 63;  // remove the 2 bit trailing byte prefix
            $c2 |= (($c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
            $c1 >>= 2; // c1 shifts 2 to the right
            $ret .= "&#" . ($c1 * 0x100 + $c2) . ";"; // this is the fastest string concatenation
            $last = $i+1;
        }
        elseif ($c1>>4 == 14) {  // 1110 xxxx, 110 prefix for 3 bytes unicode
            $ret .= substr($str, $last, $i-$last); // append all the regular characters we've passed
            $c2 = ord($str{++$i}); // the next byte
            $c3 = ord($str{++$i}); // the third byte
            $c1 &= 15; // remove the 4 bit three bytes prefix
            $c2 &= 63;  // remove the 2 bit trailing byte prefix
            $c3 &= 63;  // remove the 2 bit trailing byte prefix
            $c3 |= (($c2 & 3) << 6); // last 2 bits of c2 become first 2 of c3
            $c2 >>=2; //c2 shifts 2 to the right
            $c2 |= (($c1 & 15) << 4); // last 4 bits of c1 become first 4 of c2
            $c1 >>= 4; // c1 shifts 4 to the right
            $ret .= '&#' . (($c1 * 0x10000) + ($c2 * 0x100) + $c3) . ';'; // this is the fastest string concatenation
            $last = $i+1;
        }
    }
    $str=$ret . substr($str, $last, $i); // append the last batch of regular characters
}
?>
tobias at code-x dot de 20-Oct-2006 02:13
converting uft8-html sign &#301; to uft8

<?
function uft8html2utf8( $s ) {
        if ( !function_exists('uft8html2utf8_callback') ) {
             function uft8html2utf8_callback($t) {
                     $dec = $t[1];
            if ($dec < 128) {
              $utf = chr($dec);
            } else if ($dec < 2048) {
              $utf = chr(192 + (($dec - ($dec % 64)) / 64));
              $utf .= chr(128 + ($dec % 64));
            } else {
              $utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
              $utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
              $utf .= chr(128 + ($dec % 64));
            }
            return $utf;
             }
        }
        return preg_replace_callback('|&#([0-9]{1,});|', 'uft8html2utf8_callback', $s );
}
echo uft8html2utf8('test: &#301;');
?>
e dot panzyk at panzyk dot net 04-Oct-2006 11:38
enhanced UTF8-Decoder

After recognising that UTF8-Decode converts some French Characters to "?" i end with that Function.

The space will be need when a String ends with a converted Char ( a buggy Php Function will fit a /hex00 char at the End )

function utf8dec ( $s_String )
    {
    $s_String = html_entity_decode(htmlentities($s_String." ", ENT_COMPAT, 'UTF-8'));
    return substr($s_String, 0, strlen($s_String)-1);
    }

Hope it helps ... cost me a lot of time ...
ethaizone [AT] hotmail [DOT] com 02-Aug-2006 07:11
This function I use convert UTF-8 to Thai font (iso-8859-11).
It from iso8859_11toUTF8 function [Suttichai Mesaard-www.ceforce.com] at utf8_encode page.

It useful for translate string from mod_rewrite to real url.
I makes SEO Url In Thai language.

<?php
function UTF8toiso8859_11($string) {

     if ( ! ereg("[\241-\377]", $string) )
         return $string;

     $UTF8 = array(
"\xe0\xb8\x81" => "\xa1",
"\xe0\xb8\x82" => "\xa2",
"\xe0\xb8\x83" => "\xa3",
"\xe0\xb8\x84" => "\xa4",
"\xe0\xb8\x85" => "\xa5",
"\xe0\xb8\x86" => "\xa6",
"\xe0\xb8\x87" => "\xa7",
"\xe0\xb8\x88" => "\xa8",
"\xe0\xb8\x89" => "\xa9",
"\xe0\xb8\x8a" => "\xaa",
"\xe0\xb8\x8b" => "\xab",
"\xe0\xb8\x8c" => "\xac",
"\xe0\xb8\x8d" => "\xad",
"\xe0\xb8\x8e" => "\xae",
"\xe0\xb8\x8f" => "\xaf",
"\xe0\xb8\x90" => "\xb0",
"\xe0\xb8\x91" => "\xb1",
"\xe0\xb8\x92" => "\xb2",
"\xe0\xb8\x93" => "\xb3",
"\xe0\xb8\x94" => "\xb4",
"\xe0\xb8\x95" => "\xb5",
"\xe0\xb8\x96" => "\xb6",
"\xe0\xb8\x97" => "\xb7",
"\xe0\xb8\x98" => "\xb8",
"\xe0\xb8\x99" => "\xb9",
"\xe0\xb8\x9a" => "\xba",
"\xe0\xb8\x9b" => "\xbb",
"\xe0\xb8\x9c" => "\xbc",
"\xe0\xb8\x9d" => "\xbd",
"\xe0\xb8\x9e" => "\xbe",
"\xe0\xb8\x9f" => "\xbf",
"\xe0\xb8\xa0" => "\xc0",
"\xe0\xb8\xa1" => "\xc1",
"\xe0\xb8\xa2" => "\xc2",
"\xe0\xb8\xa3" => "\xc3",
"\xe0\xb8\xa4" => "\xc4",
"\xe0\xb8\xa5" => "\xc5",
"\xe0\xb8\xa6" => "\xc6",
"\xe0\xb8\xa7" => "\xc7",
"\xe0\xb8\xa8" => "\xc8",
"\xe0\xb8\xa9" => "\xc9",
"\xe0\xb8\xaa" => "\xca",
"\xe0\xb8\xab" => "\xcb",
"\xe0\xb8\xac" => "\xcc",
"\xe0\xb8\xad" => "\xcd",
"\xe0\xb8\xae" => "\xce",
"\xe0\xb8\xaf" => "\xcf",
"\xe0\xb8\xb0" => "\xd0",
"\xe0\xb8\xb1" => "\xd1",
"\xe0\xb8\xb2" => "\xd2",
"\xe0\xb8\xb3" => "\xd3",
"\xe0\xb8\xb4" => "\xd4",
"\xe0\xb8\xb5" => "\xd5",
"\xe0\xb8\xb6" => "\xd6",
"\xe0\xb8\xb7" => "\xd7",
"\xe0\xb8\xb8" => "\xd8",
"\xe0\xb8\xb9" => "\xd9",
"\xe0\xb8\xba" => "\xda",
"\xe0\xb8\xbf" => "\xdf",
"\xe0\xb9\x80" => "\xe0",
"\xe0\xb9\x81" => "\xe1",
"\xe0\xb9\x82" => "\xe2",
"\xe0\xb9\x83" => "\xe3",
"\xe0\xb9\x84" => "\xe4",
"\xe0\xb9\x85" => "\xe5",
"\xe0\xb9\x86" => "\xe6",
"\xe0\xb9\x87" => "\xe7",
"\xe0\xb9\x88" => "\xe8",
"\xe0\xb9\x89" => "\xe9",
"\xe0\xb9\x8a" => "\xea",
"\xe0\xb9\x8b" => "\xeb",
"\xe0\xb9\x8c" => "\xec",
"\xe0\xb9\x8d" => "\xed",
"\xe0\xb9\x8e" => "\xee",
"\xe0\xb9\x8f" => "\xef",
"\xe0\xb9\x90" => "\xf0",
"\xe0\xb9\x91" => "\xf1",
"\xe0\xb9\x92" => "\xf2",
"\xe0\xb9\x93" => "\xf3",
"\xe0\xb9\x94" => "\xf4",
"\xe0\xb9\x95" => "\xf5",
"\xe0\xb9\x96" => "\xf6",
"\xe0\xb9\x97" => "\xf7",
"\xe0\xb9\x98" => "\xf8",
"\xe0\xb9\x99" => "\xf9",
"\xe0\xb9\x9a" => "\xfa",
"\xe0\xb9\x9b" => "\xfb",
 );

     $string=strtr($string,$UTF8);
     return $string;
 }
?>

Jo, EThaiZone.Com
2ge at NO2geSPAM dot us 26-Jan-2006 10:00
Hello all,

I like to use COOL (nice) URIs, example: http://example.com/try-something
I'm using UTF8 as input, so I have to write a function UTF8toASCII to have nice URI. Here is what I come with:

<?php
function urlize($url) {
 $search = array('/[^a-z0-9]/', '/--+/', '/^-+/', '/-+$/' );
 $replace = array( '-', '-', '', '');
 return preg_replace($search, $replace, utf2ascii($url));
}

function utf2ascii($string) {
 $iso88591  = "\\xE0\\xE1\\xE2\\xE3\\xE4\\xE5\\xE6\\xE7";
 $iso88591 .= "\\xE8\\xE9\\xEA\\xEB\\xEC\\xED\\xEE\\xEF";
 $iso88591 .= "\\xF0\\xF1\\xF2\\xF3\\xF4\\xF5\\xF6\\xF7";
 $iso88591 .= "\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF";
 $ascii = "aaaaaaaceeeeiiiidnooooooouuuuyyy";
 return strtr(mb_strtolower(utf8_decode($string), 'ISO-8859-1'),$iso88591,$ascii);
}

echo urlize("Fucking ?m?l");

?>

I hope this helps someone.
php-net at ---NOSPAM---lc dot yi dot org 08-Dec-2005 09:04
I've just created this code snippet to improve the user-customizable emails sent by one of my websites.

The goal was to use UTF-8 (Unicode) so that non-english users have all the Unicode benefits, BUT also make life seamless for English (or specifically, English MS-Outlook users).  The niggle: Outlook prior to 2003 (?)  does not properly detect unicode emails.  When "smart quotes" from MS Word were pasted into a rich text area and saved in Unicode, then sent by email to an Outlook user, more often than not, these characters were wrongly rendered as "greek".

So, the following code snippet replaces a few strategic characters into html entities which Outlook XP (and possibly earlier) will render as expected.  [Code based on bits of code from previous posts on this and the htmlenties page]
<?php
    $badwordchars=array(
        "\xe2\x80\x98", // left single quote
        "\xe2\x80\x99", // right single quote
        "\xe2\x80\x9c", // left double quote
        "\xe2\x80\x9d", // right double quote
        "\xe2\x80\x94", // em dash
        "\xe2\x80\xa6" // elipses
    );
    $fixedwordchars=array(
        "&#8216;",
        "&#8217;",
        '&#8220;',
        '&#8221;',
        '&mdash;',
        '&#8230;'
    );
    $html=str_replace($badwordchars,$fixedwordchars,$html);
?>
yannikh at gmeil dot com 08-Dec-2005 08:34
I had to tackle a very interesting problem:

I wanted to replace all \xXX in a text by it's letters. Unfortunatelly XX were ASCII and not utf8. I solved my problem that way:
<?php preg_replace ('/\\\\x([0-9a-fA-F]{2})/e', "pack('H*',utf8_decode('\\1'))",$v); ?>
thierry.bo # netcourrier point com 30-Sep-2005 09:38
In response to fhoech (22-Sep-2005 11:55), I just tried a simultaneous test with the file UTF-8-test.txt using your regexp, 'j dot dittmer' (20-Sep-2005 06:30) regexp (message #56962), `php-note-2005` (17-Feb-2005 08:57) regexp in his message on `mb-detect-encoding` page (http://us3.php.net/manual/en/function.mb-detect-encoding.php#50087) who is using a regexp from the W3C (http://w3.org/International/questions/qa-forms-utf-8.html), and PHP mb_detect_encoding function.

Here are a summarize of the results :

201 lines are valid UTF8 strings using phpnote regexp
203 lines are valid UTF8 strings using j.dittmer regexp
200 lines are valid UTF8 strings using fhoech regexp
239 lines are valid  UTF8 strings using using mb_detect_encoding

Here are the lines with differences (left to right, phpnote, j.dittmer and fhoech) :

Line #70 : NOT UTF8|IS UTF8!|IS UTF8! :2.1.1 1 byte (U-00000000): ""
Line #79 : NOT UTF8|IS UTF8!|IS UTF8! :2.2.1 1 byte (U-0000007F): ""
Line #81 : IS UTF8!|IS UTF8!|NOT UTF8 :2.2.3 3 bytes (U-0000FFFF): "&#65535;" |
Line #267 : IS UTF8!|IS UTF8!|NOT UTF8 :5.3.1 U+FFFE = ef bf be = "&#65534;" |
Line #268 : IS UTF8!|IS UTF8!|NOT UTF8 :5.3.2 U+FFFF = ef bf bf = "&#65535;" |

Interesting is that you said that your regexp corrected j.dittmer regexp that failed on 5.3 section, but it my test I have the opposite result ?!

I ran this test on windows XP with PHP 4.3.11dev. Maybe these differences come from operating system, or PHP version.

For mb_detect_encoding I used the command :

mb_detect_encoding($line, 'UTF-8, ISO-8859-1, ASCII');
fhoech 22-Sep-2005 07:55
Sorry, I had a typo in my last comment. Corrected regexp:

^([\\x00-\\x7f]|
[\\xc2-\\xdf][\\x80-\\xbf]|
\\xe0[\\xa0-\\xbf][\\x80-\\xbf]|
[\\xe1-\\xec][\\x80-\\xbf]{2}|
\\xed[\\x80-\\x9f][\\x80-\\xbf]|
\\xef[\\x80-\\xbf][\\x80-\\xbd]|
\\xee[\\x80-\\xbf]{2}|
\xf0[\\x90-\\xbf][\\x80-\\xbf]{2}|
[\\xf1-\\xf3][\\x80-\\xbf]{3}|
\\xf4[\\x80-\\x8f][\\x80-\\xbf]{2})*$
fhoech 22-Sep-2005 07:12
JF Sebastian's regex is almost perfect as far as I'm concerned. I found one error (it failed section 5.3 "Other illegal code positions" from http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt) which I corrected as follows:

^([\\x00-\\x7f]|
[\\xc2-\\xdf][\\x80-\\xbf]|
\\xe0[\\xa0-\\xbf][\\x80-\\xbf]|
[\\xe1-\\xec][\\x80-\\xbf]{2}|
\\xed[\\x80-\\x9f][\\x80-\\xbf]|
\\xef[\\x80-\\xbf][\\x80-\\xbc]|
\\xee[\\x80-\\xbf]{2}|
\\xf0[\\x90-\\xbf][\\x80-\\xbf]{2}|
[\\xf1-\\xf3][\\x80-\\xbf]{3}|
\\xf4[\\x80-\\x8f][\\x80-\\xbf]{2})*$

(Again, concatenate to one single line to make it work)
j dot dittmer at portrix dot net 20-Sep-2005 02:30
The regex in the last comment has some typos. This is a
syntactically valid one, don't know if it's correct though.
You've to concat the expression in one long line.

^(
[\x00-\x7f]|
[\xc2-\xdf][\x80-\xbf]|
[\xe0][\xa0-\xbf][\x80-\xbf]|
[\xe1-\xec][\x80-\xbf]{2}|
[\xed][\x80-\x9f][\x80-\xbf]|
[\xee-\xef][\x80-\xbf]{2}|
[\xf0][\x90-\xbf][\x80-\xbf]{2}|
[\xf1-\xf3][\x80-\xbf]{3}|
[\xf4][\x80-\x8f][\x80-\xbf]{2}
)*$
chris at mrwsp dot com 09-Aug-2005 12:54
A small improvement.

JF Sebastian's regex for UTF-8 is not quite correct.  Because code points could otherwise be coded in more than one way using UTF-8, the Standard stipulates that the shortest possible representation for a character should be used.  So some 'duplicate' combinations his regex accepts are not valid UTF-8.  Additionally, his regex accepts characters beyond the valid Unicode code space.

The regex should be:

^([\x00-\x7f]|
[\xc2-\xdf][\x80-\xbf]|
[\xe0][\xa0-\xbf][\x80-xbf]|
[\xe1-\xec][\x80-\xbf]{2}|
[\xed][\x80-\x9f][\x80-\xbf]|
[\xee-\xef][\x80-\xbf]{2}|
[\xf0][\x90-\xbf][\x80-\xbf]{2}|
[\xf1-\xf3][\x80-\xbf]{3}|
[\xf4][\x80-\x8f][\x80-\xbf]{2}*$)
miracle at balkansys dot com 14-Jun-2005 11:24
The best multilanguage library I have found is a part of a CMS system - typo3

http://www.typo3.org

It can convert from and to any charset + it does it by three methods - mbstring, iconv, or the raw way by scripts. It uses only one of these techniques - the fastest if available.

That was the only way I could make mysql 3.23 contain letters in almost any language, while maintaining my website in utf-8 only.
Vladimir Stwora, vlad4321 at fastmail dot fm 09-May-2005 12:41
If you want to convert utf-8 to ascii, you can use the following procedure:

<?php
function utf2ascii($string) {
   $string=iconv('utf-8','windows-1250',$string);
   $win  ='??????...';  // I was unable to paste here all set of characters, but you get the point
   $ascii='zyaieuu...';
   $string = StrTr($string,$win,$ascii);
   return $string;
  }
?>
This works based on the assumption that you know what language text (and thus what charset) you want to convert from. In the above example I am converting from an eastern European language, so I know I can safely use windows-1250 charset as an intermediem charset. You will have to adjust the charset based on your language.

Please remember that you have to save this file separately and it must be coded in the charset, which you will call within function iconv. Otherwise it will not work.
JF Sebastian 30-Mar-2005 04:09
The following Perl regular expression tests if a string is well-formed Unicode UTF-8 (Broken up after each | since long lines are not permitted here. Please join as a single line, no spaces, before use.):

^([\x00-\x7f]|
[\xc2-\xdf][\x80-\xbf]|
\xe0[\xa0-\xbf][\x80-\xbf]|
[\xe1-\xec][\x80-\xbf]{2}|
\xed[\x80-\x9f][\x80-\xbf]|
[\xee-\xef][\x80-\xbf]{2}|
f0[\x90-\xbf][\x80-\xbf]{2}|
[\xf1-\xf3][\x80-\xbf]{3}|
\xf4[\x80-\x8f][\x80-\xbf]{2})*$

NOTE: This strictly follows the Unicode standard 4.0, as described in chapter 3.9, table 3-6, "Well-formed UTF-8 byte sequences" ( http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G31703 ).

ISO-10646, a super-set of Unicode, uses UTF-8 (there called "UCS", see http://www.unicode.org/faq/utf_bom.html#1 ) in a relaxed variant that supports a 31-bit space encoded into up to six bytes instead of Unicode's 21 bits in up to four bytes. To check for ISO-10646 UTF-8, use the following Perl regular expression (again, broken up, see above):

^([\x00-\x7f]|
[\xc0-\xdf][\x80-\xbf]|
[\xe0-\xef][\x80-\xbf]{2}|
[\xf0-\xf7][\x80-\xbf]{3}|
[\xf8-\xfb][\x80-\xbf]{4}|
[\xfc-\xfd][\x80-\xbf]{5})*$

The following function may be used with above expressions for a quick UTF-8 test, e.g. to distinguish ISO-8859-1-data from UTF-8-data if submitted from a <form accept-charset="utf-8,iso-8859-1" method=..>.

function is_utf8($string) {
    return (preg_match('/[insert regular expression here]/', $string) === 1);
}
ivanmaz(remove) at mech dot math dot msu dot su 16-Mar-2005 12:50
Here is my variant of UTF8 to Cyrillic Win-1251 encoding convertor that replaces all characters but latin and Russian ones with &#...; entities:

function utf2win1251 ($s)
{
 $out = "";

 for ($i=0; $i<strlen($s); $i++)
 {
  $c1 = substr ($s, $i, 1);
  $byte1 = ord ($c1);
  if ($byte1>>5 == 6) // 110x xxxx, 110 prefix for 2 bytes unicode
  {
   $i++;
   $c2 = substr ($s, $i, 1);
   $byte2 = ord ($c2);
   $byte1 &= 31; // remove the 3 bit two bytes prefix
   $byte2 &= 63; // remove the 2 bit trailing byte prefix
   $byte2 |= (($byte1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
   $byte1 >>= 2; // c1 shifts 2 to the right

   $word = ($byte1<<8) + $byte2;
   if ($word==1025) $out .= chr(168);                    // ?
   elseif ($word==1105) $out .= chr(184);                // ?
   elseif ($word>=0x0410 && $word<=0x044F) $out .= chr($word-848); // ?-? ?-?
   else
   {
     $a = dechex($byte1);
     $a = str_pad($a, 2, "0", STR_PAD_LEFT);
     $b = dechex($byte2);
     $b = str_pad($b, 2, "0", STR_PAD_LEFT);
     $out .= "&#x".$a.$b.";";
   }
  }
  else
  {
   $out .= $c1;
  }
 }

 return $out;
}

The function is based on 2 other functions posted below.
I hope it will help those who convert UTF8-encoded text to Win-1251 to use it safely on Russian web pages (works fine in all browsers).
marcelo at maccoy dot com dot br 14-Feb-2005 07:24
function decode_utf8($str){
       # erase null signs in string
          $str=eregi_replace("^.{10,13}q\?","",$str);
       # paterns
           $pat = "/=([0-9A-F]{2})/";
           $cha="'.chr(hexdec(";
       # to decode with eval and replace
          eval("\$str='".
                  preg_replace($pat,$cha."'$1')).'",$str).
                  "';");
        # return
           return $str;
        }

Note:
It's possible put it in 3 lines, but I don't got in this first code submition.
husamb at maksimum dot net 27-Jan-2005 10:16
Hi, I collected the some scripts in this page and I written a new customizable script. You can switch easily iso type to convert. There are definitions in unicode.org page at http://www.unicode.org/Public/MAPPINGS/ISO8859/.

<?php
# GLOBAL VARIABLES
$url = "http://www.unicode.org/Public/MAPPINGS/ISO8859/8859-9.TXT";
//$url = "8859-9.txt";
$iso2utf = array();
$utf2iso = array();

# UNICODE MAPPING TABLE PARSING
function create_map($url){
    global $iso2utf, $utf2iso;
    $fl = @(file($url)) OR (die("cannot open file : $url\n"));
    for ($i=0; $i<count($fl); $i++){
        if($fl[$i][0] != '#' && trim($fl[$i])){
            list($iso, $uni, $s, $desc) = split("\t",$fl[$i]);
            $iso2utf[$iso] = $uni;
            $utf2iso[$uni] = $iso;
        }
    }
}

# FINDING UNICODE LETTER'S DECIMAL ASCII VALUE
function uniord($c){
    $ud = 0;
    if (ord($c{0})>=0 && ord($c{0})<=127)   $ud = $c{0};
    if (ord($c{0})>=192 && ord($c{0})<=223) $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
    if (ord($c{0})>=224 && ord($c{0})<=239) $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
    if (ord($c{0})>=240 && ord($c{0})<=247) $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
    if (ord($c{0})>=248 && ord($c{0})<=251) $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
    if (ord($c{0})>=252 && ord($c{0})<=253) $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
    if (ord($c{0})>=254 && ord($c{0})<=255) $ud = false; //error
    return $ud;
}

# PARSING UNICODE STRING
function utf2iso($source) {
    global $utf2iso;
    $pos = 0;
    $len = strlen ($source);
    $encodedString = '';

    while ($pos < $len) {
        $is_ascii = false;
        $asciiPos = ord (substr ($source, $pos, 1));
        if(($asciiPos >= 240) && ($asciiPos <= 255)) {
            // 4 chars representing one unicode character
            $thisLetter = substr ($source, $pos, 4);
            $thisLetterOrd = uniord($thisLetter);
            $pos += 4;
        }
        else if(($asciiPos >= 224) && ($asciiPos <= 239)) {
            // 3 chars representing one unicode character
            $thisLetter = substr ($source, $pos, 3);
            $thisLetterOrd = uniord($thisLetter);
            $pos += 3;
        }
        else if(($asciiPos >= 192) && ($asciiPos <= 223)) {
            // 2 chars representing one unicode character
            $thisLetter = substr ($source, $pos, 2);
            $thisLetterOrd = uniord($thisLetter);
            $pos += 2;
        }
        else{
            // 1 char (lower ascii)
            $thisLetter = substr ($source, $pos, 1);
            $thisLetterOrd = uniord($thisLetter);
            $pos += 1;
            $encodedString .= $thisLetterOrd;
            $is_ascii = true;
        }
        if(!$is_ascii){
            $hex = sprintf("%X", $thisLetterOrd);
            if(strlen($hex)<4) for($t=strlen($hex);$t<4;$t++)$hex = "0".$hex;
            $hex = "0x".$hex;
            $hex = $utf2iso[$hex];
            $hex = str_replace('0x','',$hex);
            $dec = hexdec($hex);
            $encodedString .= sprintf("%c", $dec);
        }
    }
    return $encodedString;
}

# CREATING ISO2UTF & UTF2ISO MAPS
create_map($url);

# TESTING
$unicode_string = "Ekonomi_ve_\xc4\xb0\xc5\x9f_D\xc3\xbcnyas\xc4\xb1";

echo "unicode string : <b>" . $unicode_string . "</b>";
echo "<br><br>";
echo "ISO8859 (latin5 / turkish) converted string : <b>" . utf2iso($unicode_string) . "</b>";
?>

The unicode string is turkish. ITs mean in english is 'Economy and Business World' :)

Husam
rasmus at flajm dot se 19-Jan-2005 11:57
If you don't have the multibyte extension installed, here's a function to decode UTF-16 encoded strings. It support both BOM-less and BOM'ed strings, (big- and little-endian byte order.)

<?php
/**
 * Decode UTF-16 encoded strings.
 *
 * Can handle both BOM'ed data and un-BOM'ed data.
 * Assumes Big-Endian byte order if no BOM is available.
 *
 * @param   string  $str  UTF-16 encoded data to decode.
 * @return  string  UTF-8 / ISO encoded data.
 * @access  public
 * @version 0.1 / 2005-01-19
 * @author  Rasmus Andersson {@link http://rasmusandersson.se/}
 * @package Groupies
 */
function utf16_decode( $str ) {
    if( strlen($str) < 2 ) return $str;
    $bom_be = true;
    $c0 = ord($str{0});
    $c1 = ord($str{1});
    if( $c0 == 0xfe && $c1 == 0xff ) { $str = substr($str,2); }
    elseif( $c0 == 0xff && $c1 == 0xfe ) { $str = substr($str,2); $bom_be = false; }
    $len = strlen($str);
    $newstr = '';
    for($i=0;$i<$len;$i+=2) {
        if( $bom_be ) { $val = ord($str{$i})   << 4; $val += ord($str{$i+1}); }
        else {        $val = ord($str{$i+1}) << 4; $val += ord($str{$i}); }
        $newstr .= ($val == 0x228) ? "\n" : chr($val);
    }
    return $newstr;
}
?>
dobersch at gmx dot net 04-Nov-2004 03:11
Sorry, there's an error in my previous comment, my error_reporting was not set to E_ALL ... so the notices disappeared...

It seems that those Google URLs are not only UTF-8 encoded, but after encoding, several values also get replaced. (cp1252 ?)

Just the way as described in following comments on the utf8_encode() page:
http://de3.php.net/manual/de/function.utf8-encode.php#44843 and
http://de3.php.net/manual/de/function.utf8-encode.php#45226

Below I post a solution for getting back the ISO-8859-1 encoded string from the encoded data, by turning the function from Aidan the other way around.

hope this time, everything is alright...

<?PHP
ini_set('error_reporting', E_ALL);
// map taken from http://de3.php.net/manual/de/function.utf8-encode.php#45226
$cp1252_map = array(
   "\xc2\x80" => "\xe2\x82\xac", /* EURO SIGN */
   "\xc2\x82" => "\xe2\x80\x9a", /* SINGLE LOW-9 QUOTATION MARK */
   "\xc2\x83" => "\xc6\x92",    /* LATIN SMALL LETTER F WITH HOOK */
   "\xc2\x84" => "\xe2\x80\x9e", /* DOUBLE LOW-9 QUOTATION MARK */
   "\xc2\x85" => "\xe2\x80\xa6", /* HORIZONTAL ELLIPSIS */
   "\xc2\x86" => "\xe2\x80\xa0", /* DAGGER */
   "\xc2\x87" => "\xe2\x80\xa1", /* DOUBLE DAGGER */
   "\xc2\x88" => "\xcb\x86",    /* MODIFIER LETTER CIRCUMFLEX ACCENT */
   "\xc2\x89" => "\xe2\x80\xb0", /* PER MILLE SIGN */
   "\xc2\x8a" => "\xc5\xa0",    /* LATIN CAPITAL LETTER S WITH CARON */
   "\xc2\x8b" => "\xe2\x80\xb9", /* SINGLE LEFT-POINTING ANGLE QUOTATION */
   "\xc2\x8c" => "\xc5\x92",    /* LATIN CAPITAL LIGATURE OE */
   "\xc2\x8e" => "\xc5\xbd",    /* LATIN CAPITAL LETTER Z WITH CARON */
   "\xc2\x91" => "\xe2\x80\x98", /* LEFT SINGLE QUOTATION MARK */
   "\xc2\x92" => "\xe2\x80\x99", /* RIGHT SINGLE QUOTATION MARK */
   "\xc2\x93" => "\xe2\x80\x9c", /* LEFT DOUBLE QUOTATION MARK */
   "\xc2\x94" => "\xe2\x80\x9d", /* RIGHT DOUBLE QUOTATION MARK */
   "\xc2\x95" => "\xe2\x80\xa2", /* BULLET */
   "\xc2\x96" => "\xe2\x80\x93", /* EN DASH */
   "\xc2\x97" => "\xe2\x80\x94", /* EM DASH */

   "\xc2\x98" => "\xcb\x9c",    /* SMALL TILDE */
   "\xc2\x99" => "\xe2\x84\xa2", /* TRADE MARK SIGN */
   "\xc2\x9a" => "\xc5\xa1",    /* LATIN SMALL LETTER S WITH CARON */
   "\xc2\x9b" => "\xe2\x80\xba", /* SINGLE RIGHT-POINTING ANGLE QUOTATION*/
   "\xc2\x9c" => "\xc5\x93",    /* LATIN SMALL LIGATURE OE */
   "\xc2\x9e" => "\xc5\xbe",    /* LATIN SMALL LETTER Z WITH CARON */
   "\xc2\x9f" => "\xc5\xb8"      /* LATIN CAPITAL LETTER Y WITH DIAERESIS*/
);

// I find this name a little misleading because the result won't be valid UTF8 data
function cp1252_to_utf8($str) {
   global $cp1252_map;
   return  strtr(utf8_encode($str), $cp1252_map);
}

function cp1252_utf8_to_iso($str) { // the other way around...
  global $cp1252_map;
  return  utf8_decode( strtr($str, array_flip($cp1252_map)) );
}

$euro = "\xe2\x82\xac";  // "google encoded" euro sign
$str = cp1252_utf8_to_iso($euro);

for($i=0; $i<strlen($str); $i++) {
  print '&quot;' . $str[$i] . '&quot; - ' . ord($str[$i]) . ' (decimal)<br />';
}
?>
vpribish at shopping dot com 10-Sep-2004 08:55
big thanks to wielspm and mittag for the function to convert wide utf to html.  I have altered your function to output decimal html entities which i hear are more widely supported.  Also I've optimized it, it is now twice as fast.

<?php
function utf2html ($str) {
$ret = "";
$max = strlen($str);
$last = 0;  // keeps the index of the last regular character
for ($i=0; $i<$max; $i++) {
 $c = $str{$i};
 $c1 = ord($c);
 if ($c1>>5 == 6) {   // 110x xxxx, 110 prefix for 2 bytes unicode
    $ret .= substr($str, $last, $i-$last); // append all the regular characters we've passed
    $c1 &= 31; // remove the 3 bit two bytes prefix
    $c2 = ord($str{++$i}); // the next byte
    $c2 &= 63;  // remove the 2 bit trailing byte prefix
    $c2 |= (($c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
    $c1 >>= 2; // c1 shifts 2 to the right
    $ret .= "&#" . ($c1 * 100 + $c2) . ";"; // this is the fastest string concatenation
    $last = $i+1;
 }
}
return $ret . substr($str, $last, $i); // append the last batch of regular characters
}
?>
Aidan Kehoe <php-manual at parhasard dot net> 31-Jan-2004 02:35
The fastest way I've found to check if something is valid UTF-8 is
<?php
if (iconv('UTF-8', 'UTF-8', $input) != $input) {
        /* It's not UTF-8--for me, it's probably CP1252, the Windows
           version of Latin 1, with directed quotation marks and
           the Euro sign.  */
}
 ?>.
The iconv() C library fails if it's told a string is UTF-8 and it isn't; the PHP one doesn't, it just returns the conversion up to the point of failure, so you have to compare the result to the input to find out if the conversion succeeded.
lg83news at free dot fr 29-Dec-2003 06:40
Here are some functions for converting UTF-8 to and from Latin 9 (aka ISO-8859-15), if your PHP file is encoded as UTF-8.

<?php
function latin9_to_utf8($latin9str) { // replaces utf8_encode()
    $trans = array("?"=>"?", "?"=>"?", "?"=>"?", "?"=>"?", "?"=>"?", "?"=>"?", "?"=>"?", "?"=>"?");
    $wrong_utf8str = utf8_encode($latin9str);
    $utf8str = strtr($wrong_utf8str, $trans);
    return $utf8str;
}
function utf8_to_latin9($utf8str) { // replaces utf8_decode()
    $trans = array("?"=>"?", "?"=>"?", "?"=>"?", "?"=>"?", "?"=>"?", "?"=>"?", "?"=>"?", "?"=>"?");
    $wrong_utf8str = strtr($utf8str, $trans);
    $latin9str = utf8_decode($wrong_utf8str);
    return $latin9str;
}
?>

Note: the above functions will not work if your PHP file is not encoded in UTF-8.

You can copy this binary data to your PHP file instead, it'll work with any encoding (including UTF-8) as long as you copy it as bytes, not characters:

<?php
function latin9_to_utf8($latin9str) { // replaces utf8_encode()
    $trans = array("¤"=>"€", "|"=>"? ", "¨"=>"?", "´"=>"?", "¸"=>"?", "¼"=>"Œ", "½"=>"œ", "¾"=>"?");
    $wrong_utf8str = utf8_encode($latin9str);
    $utf8str = strtr($wrong_utf8str, $trans);
    return $utf8str;
}
function utf8_to_latin9($utf8str) { // replaces utf8_decode()
    $trans = array("€"=>"¤", "? "=>"|", "?"=>"¨", "?"=>"´", "?"=>"¸", "Œ"=>"¼", "œ"=>"½", "?"=>"¾");
    $wrong_utf8str = strtr($utf8str, $trans);
    $latin9str = utf8_decode($wrong_utf8str);
    return $latin9str;
}
?>

(Note: the PHP manual is served as ISO-8859-1, but many of the bytes used here have no meaning in ISO-8859-1, so they'll probably be displayed as Windows-1252 if your using Windows, or garbage otherwise. Just copy the bytes!)
gto at interia dot pl 25-Nov-2003 04:12
Correction to function converting utf82iso88592 and iso88592tutf8.
Janusz forgot about "&#324;", and "&#380;" exchanged from "&#378;" here and there.

GTo

function utf82iso88592($tekscik) {
     $tekscik = str_replace("\xC4\x85", "&#261;", $tekscik);
     $tekscik = str_replace("\xC4\x84", '&#260;', $tekscik);
     $tekscik = str_replace("\xC4\x87", '&#263;', $tekscik);
     $tekscik = str_replace("\xC4\x86", '&#262;', $tekscik);
     $tekscik = str_replace("\xC4\x99", '&#281;', $tekscik);
     $tekscik = str_replace("\xC4\x98", '&#280;', $tekscik);
     $tekscik = str_replace("\xC5\x82", '&#322;', $tekscik);
     $tekscik = str_replace("\xC5\x81", '&#321;', $tekscik);
     $tekscik = str_replace("\xC5\x84", '&#324;', $tekscik);
     $tekscik = str_replace("\xC5\x83", '&#323;', $tekscik);
     $tekscik = str_replace("\xC3\xB3", '?', $tekscik);
     $tekscik = str_replace("\xC3\x93", '?', $tekscik);
     $tekscik = str_replace("\xC5\x9B", '&#347;', $tekscik);
     $tekscik = str_replace("\xC5\x9A", '&#346;', $tekscik);
     $tekscik = str_replace("\xC5\xBC", '&#380;', $tekscik);
     $tekscik = str_replace("\xC5\xBB", '&#379;', $tekscik);
     $tekscik = str_replace("\xC5\xBA", '&#378;', $tekscik);
     $tekscik = str_replace("\xC5\xB9", '&#377;', $tekscik);
     return $tekscik;
} // utf82iso88592

function iso885922utf8($tekscik) {
     $tekscik = str_replace("&#261;", "\xC4\x85", $tekscik);
     $tekscik = str_replace('&#260;', "\xC4\x84", $tekscik);
     $tekscik = str_replace('&#263;', "\xC4\x87", $tekscik);
     $tekscik = str_replace('&#262;', "\xC4\x86", $tekscik);
     $tekscik = str_replace('&#281;', "\xC4\x99", $tekscik);
     $tekscik = str_replace('&#280;', "\xC4\x98", $tekscik);
     $tekscik = str_replace('&#322;', "\xC5\x82", $tekscik);
     $tekscik = str_replace('&#321;', "\xC5\x81", $tekscik);
     $tekscik = str_replace('&#324;', "\xC5\x84", $tekscik);
     $tekscik = str_replace('&#323;',"\xC5\x83", $tekscik);
     $tekscik = str_replace('?', "\xC3\xB3", $tekscik);
     $tekscik = str_replace('?', "\xC3\x93", $tekscik);
     $tekscik = str_replace('&#347;', "\xC5\x9B", $tekscik);
     $tekscik = str_replace('&#346;', "\xC5\x9A", $tekscik);
     $tekscik = str_replace('&#380;', "\xC5\xBC", $tekscik);
     $tekscik = str_replace('&#379;', "\xC5\xBB", $tekscik);
     $tekscik = str_replace('&#378;', "\xC5\xBA", $tekscik);
     $tekscik = str_replace('&#377;', "\xC5\xB9", $tekscik);
     return $tekscik;
} // iso885922utf8
nospam at jra dot nu 23-Nov-2003 12:48
There is an error in the 'smart_utf8_decode' function posted by ' goran_johansson' below. It should look like this:

function smart_utf8_decode($in_str)
{
    // Replace ? with a unique string
    $new_str = str_replace("?", "q0u0e0s0t0i0o0n", $in_str);

    // Try the utf8_decode
    $new_str=utf8_decode($new_str);

    // if it contains ? marks
    if (strpos($new_str,"?") !== false)
    {
        // Something went wrong, set new_str to the original string.
        $new_str=$in_str;
    }
    else
    {
        // If not then all is well, put the ?-marks back where is belongs
        $new_str = str_replace("q0u0e0s0t0i0o0n", "?", $new_str);
    }

    return $new_str;
}
janusz dot s at remove_this dot and_this dot poczta dot fm 25-Oct-2003 05:54
Here are functions to PROPERLY de/encode Unicode (UTF-8) string to/from ISO-8859-2 (Polish character set).

Regards,
Janusz

function utf82iso88592($tekscik) {
 $tekscik = str_replace("\xC4\x85", "?", $tekscik);
 $tekscik = str_replace("\xC4\x84", '?', $tekscik);
 $tekscik = str_replace("\xC4\x87", '?', $tekscik);
 $tekscik = str_replace("\xC4\x86", '?', $tekscik);
 $tekscik = str_replace("\xC4\x99", '?', $tekscik);
 $tekscik = str_replace("\xC4\x98", '?', $tekscik);
 $tekscik = str_replace("\xC5\x82", '?', $tekscik);
 $tekscik = str_replace("\xC5\x81", '?', $tekscik);
 $tekscik = str_replace("\xC3\xB3", '?', $tekscik);
 $tekscik = str_replace("\xC3\x93", '?', $tekscik);
 $tekscik = str_replace("\xC5\x9B", '?', $tekscik);
 $tekscik = str_replace("\xC5\x9A", '?', $tekscik);
 $tekscik = str_replace("\xC5\xBC", '?', $tekscik);
 $tekscik = str_replace("\xC5\xBB", '?', $tekscik);
 $tekscik = str_replace("\xC5\xBA", '?', $tekscik);
 $tekscik = str_replace("\xC5\xB9", '?', $tekscik);
 return $tekscik;
} // utf82iso88592

function iso885922utf8($tekscik) {
 $tekscik = str_replace("?", "\xC4\x85", $tekscik);
 $tekscik = str_replace('?', "\xC4\x84", $tekscik);
 $tekscik = str_replace('?', "\xC4\x87", $tekscik);
 $tekscik = str_replace('?', "\xC4\x86", $tekscik);
 $tekscik = str_replace('?', "\xC4\x99", $tekscik);
 $tekscik = str_replace('?', "\xC4\x98", $tekscik);
 $tekscik = str_replace('?', "\xC5\x82", $tekscik);
 $tekscik = str_replace('?', "\xC5\x81", $tekscik);
 $tekscik = str_replace('?', "\xC3\xB3", $tekscik);
 $tekscik = str_replace('?', "\xC3\x93", $tekscik);
 $tekscik = str_replace('?', "\xC5\x9B", $tekscik);
 $tekscik = str_replace('?', "\xC5\x9A", $tekscik);
 $tekscik = str_replace('?', "\xC5\xBC", $tekscik);
 $tekscik = str_replace('?', "\xC5\xBB", $tekscik);
 $tekscik = str_replace('?', "\xC5\xBA", $tekscik);
 $tekscik = str_replace('?', "\xC5\xB9", $tekscik);
 return $tekscik;
} // iso885922utf8
bn2 (bn2 at ukr dot net) 01-Sep-2003 05:56
Function to decode uft8 to win-1251 (russian) charset. Don't support ukrainian letters. Support only 2-byte coding.

function utf8win1251($s){
$out="";$c1="";$byte2=false;
for ($c=0;$c<strlen($s);$c++){
$i=ord($s[$c]);
if ($i<=127) $out.=$s[$c];
if ($byte2){
$new_c2=($c1&3)*64+($i&63);
$new_c1=($c1>>2)&5;
$new_i=$new_c1*256+$new_c2;
if ($new_i==1025) $out_i=168; else
if ($new_i==1105) $out_i=184; else $out_i=$new_i-848;
$out.=chr($out_i);
$byte2=false;}
if (($i>>5)==6) {$c1=$i;$byte2=true;}
}
return $out;}
morris_hirsch at hotmail dot com 18-Aug-2003 06:43
Be aware that utf8_decode can not properly store ''wide'' code entities which have a numeric value too big for a byte.

This function may help you.  It converts utf8 to strict ASCII with no high-bit codes, they are all written as numerics.

// UTF-8 encoding
// bytes bits representation
// 1   7  0bbbbbbb
// 2  11  110bbbbb 10bbbbbb
// 3  16  1110bbbb 10bbbbbb 10bbbbbb
// 4  21  11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
// Each b represents a bit that can be used to store character data.

// input CANNOT have single byte upper half extended ascii codes

function numeric_entify_utf8 ($utf8_string) {

  $out = "";
  $ns = strlen ($utf8_string);
  for ($nn = 0; $nn < $ns; $nn++) {
    $ch = $utf8_string [$nn];
    $ii = ord ($ch);

//1 7 0bbbbbbb (127)

    if ($ii < 128) $out .= $ch;

//2 11 110bbbbb 10bbbbbb (2047)

   else if ($ii >>5 == 6) {
  $b1 = ($ii & 31);

  $nn++;
   $ch = $utf8_string [$nn];
    $ii = ord ($ch);
  $b2 = ($ii & 63);

  $ii = ($b1 * 64) + $b2;

      $ent = sprintf ("&#%d;", $ii);
      $out .= $ent;
    }

//3 16 1110bbbb 10bbbbbb 10bbbbbb

   else if ($ii >>4 == 14) {
  $b1 = ($ii & 31);

  $nn++;
   $ch = $utf8_string [$nn];
    $ii = ord ($ch);
  $b2 = ($ii & 63);

  $nn++;
   $ch = $utf8_string [$nn];
    $ii = ord ($ch);
  $b3 = ($ii & 63);

  $ii = ((($b1 * 64) + $b2) * 64) + $b3;

      $ent = sprintf ("&#%d;", $ii);
      $out .= $ent;
    }

//4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb

   else if ($ii >>3 == 30) {
  $b1 = ($ii & 31);

  $nn++;
   $ch = $utf8_string [$nn];
    $ii = ord ($ch);
  $b2 = ($ii & 63);

  $nn++;
   $ch = $utf8_string [$nn];
    $ii = ord ($ch);
  $b3 = ($ii & 63);

  $nn++;
   $ch = $utf8_string [$nn];
    $ii = ord ($ch);
  $b4 = ($ii & 63);

  $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4;

      $ent = sprintf ("&#%d;", $ii);
      $out .= $ent;
    }

  }
  return $out;
}
johan dot andersson at strateg dot se 05-May-2003 10:25
A simple way to convert utf-8 encoded strings...
(PHP 4 >= 4.3.0)

<?php
$newstring = html_entity_decode(htmlentities($utf8_string, ENT_COMPAT, 'UTF-8'));
?>

(For 4.1.0 >= PHP < 4.3.0 use this function instead of html_entity_decode)

<?php
function unhtmlentities ($string)
{
    $trans_tbl = get_html_translation_table (HTML_ENTITIES);
    $trans_tbl = array_flip ($trans_tbl);
    return strtr ($string, $trans_tbl);
}

$newstring = unhtmlentities(htmlentities($utf8_string, ENT_COMPAT, 'UTF-8'));
?>

//Johan
24-Feb-2003 03:10
Oups, this is the non bugged version :)

Well sometimes you need to store a utf-8 string in a database table column with a fixed size.
Here is a function that fix a string which has been broken (by a substr for example) in a middle of a utf-8 char sequence.

function FixUtf8BrokenString($Desc)
{
 // UTF-8 encoding
 // bytes : representation
 // 1     : 0bbbbbbb
 // 2    : 110bbbbb 10bbbbbb
 // 3     : 1110bbbb 10bbbbbb 10bbbbbb
 // 4     : 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
 // to see if a string is broken in middle of a utf8 char
 // we search for last byte encoding size of utf-8 char
 // if number of last bytes in string is lower than encoding size
 // we remove those last bytes

 // if last byte is ord < 128, ok. we return !
 if (ord($Desc[strlen($Desc) - 1]) < (0x80))
   return $Desc

 // loop for finding byte encoding size
 $nbbytes = 1;
 while (ord($Desc[strlen($Desc) - $nbbytes]) > 0x7F)
 {
   if (ord($Desc[strlen($Desc) - $nbbytes]) > 0xBF)
     break;
   $nbbytes++;
 }

  // check if byte encoding size is encoding a size of 4 bytes
  if ((ord($Desc[strlen($Desc) - $nbbytes]) > 0xF0) && ($nbbytes == 4))
    return $Desc;
  // check if byte encoding size is encoding a size of 3 bytes
  if ((ord($Desc[strlen($Desc) - $nbbytes]) > 0xE0) && ($nbbytes == 3))
    return $Desc;
  // check if byte encoding size is encoding a size of 2 bytes
  if ((ord($Desc[strlen($Desc) - $nbbytes]) > 0xC0) && ($nbbytes == 2))
    return $Desc;
  // then this is the case where string is badly broken, we remove last bytes
  return substr($Desc, 0, -$nbbytes);
}

$str = "Ekonomi_ve_\xc4\xb0\xc5\x9f_D\xc3\xbcnyas\xc4\xb1";
$broken = substr($str, 0, 12);
$fixed = FixUtf8BrokenString($broken);

echo "$str<hr>$fixed<hr>$broken<hr>";

Sebastien Meudec.
18-Dec-2002 06:08
addition to above entrie:

it does not mean "when it comes to a '='" but when it comes to a "0"
mittag - -add- -marcmittag- -dot- -de 18-Dec-2002 05:56
The above function does not work entirely correct. It comes to problems, if there is a leading "=" in one of the two Strings it produces and glues out of the two bytes of the unicode letter.

The following works:

<?php
//Convert Unicode to ASCII + Entities
$fp = fopen($DOCUMENT_ROOT."/your_unicode_text.txt", "r");
while ( !feof($fp) )
{ $string = fgets($fp, 1000);

 $utf2html_string .= $string;
}
$string2 = $utf2html_string;
fclose ( $fp);

function utf2html ()
{
global $utf2html_string;

$utf2html_retstr = "";
 for ($utf2html_p=0; $utf2html_p<strlen($utf2html_string); $utf2html_p++) {
 $utf2html_c = substr ($utf2html_string, $utf2html_p, 1);
 $utf2html_c1 = ord ($utf2html_c);
  if ($utf2html_c1>>5 == 6) {// 110x xxxx, 110 prefix for 2 bytes unicode
    $utf2html_p++;
   $utf2html_t = substr ($utf2html_string, $utf2html_p, 1);
  $utf2html_c2 = ord ($utf2html_t);
   $utf2html_c1 &= 31; // remove the 3 bit two bytes prefix
   $utf2html_c2 &= 63; // remove the 2 bit trailing byte prefix
   $utf2html_c2 |= (($utf2html_c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
   $utf2html_c1 >>= 2; // c1 shifts 2 to the right
   $a = dechex($utf2html_c1);
   $a = str_pad($a, 2, "0", STR_PAD_LEFT);
   $b = dechex($utf2html_c2);
   $b = str_pad($b, 2, "0", STR_PAD_LEFT);

   $utf2html_n_neu = $a.$b;
   $utf2html_n_neu_speicher = $utf2html_n_neu;
   $utf2html_n_neu = "&#x".$utf2html_n_neu.";";

 $utf2html_retstr .= $utf2html_n_neu;

  }
  else {
   $utf2html_retstr .= $utf2html_c;
  }
 }
 echo $utf2html_retstr;
 //return $utf2html_retstr;

}

utf2html();

?>
wielspm at xs4all dot nl 05-Dec-2002 06:44
Here is a function I made to convert all 2 byte utf in a &#xxx; form:

function utf2html ($utf2html_string)
{
  $utf2html_retstr = "";
  for ($utf2html_p=0; $utf2html_p<strlen($utf2html_string); $utf2html_p++):
    $utf2html_c = substr ($utf2html_string, $utf2html_p, 1);
    $utf2html_c1 = ord ($utf2html_c);
    if ($utf2html_c1>>5 == 6): // 110x xxxx, 110 prefix for 2 bytes unicode
      $utf2html_p++;
       $utf2html_t = substr ($utf2html_string, $utf2html_p, 1);
      $utf2html_c2 = ord ($utf2html_t);
      $utf2html_c1 &= 31; // remove the 3 bit two bytes prefix
      $utf2html_c2 &= 63; // remove the 2 bit trailing byte prefix
      $utf2html_c2 |= (($utf2html_c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2
      $utf2html_c1 >>= 2; // c1 shifts 2 to the right
       $utf2html_n = dechex($utf2html_c1).dechex($utf2html_c2);
      $utf2html_retstr .= sprintf ("&#%03d;", hexdec($utf2html_n));
    else:
      $utf2html_retstr .= $utf2html_c;
    endif;
  endfor;
  return $utf2html_retstr;
}
ronen at greyzone dot com 02-Mar-2002 02:37
The following function will take a utf-8 encoded string and convert it to Unicode entities (the format is &#nnn; or &#nnnnn; with n={0..9} ).  Most browsers will display Unicode entities regardless of the encoding of the page.  Otherwise try CHARSET=ks_c_5601-1987 to make sure the entities display correctly.  This works well with IE and Mozilla (tested with Mozilla 0.9.8 for X-Windos).

<?php
/**
* takes a string of utf-8 encoded characters and converts it to a string of unicode entities
* each unicode entitiy has the form &#nnnnn; n={0..9} and can be displayed by utf-8 supporting
* browsers
* @param $source string encoded using utf-8 [STRING]
* @return string of unicode entities [STRING]
* @access public
*/
function utf8ToUnicodeEntities ($source) {
    // array used to figure what number to decrement from character order value
    // according to number of characters used to map unicode to ascii by utf-8
    $decrement[4] = 240;
    $decrement[3] = 224;
    $decrement[2] = 192;
    $decrement[1] = 0;

    // the number of bits to shift each charNum by
    $shift[1][0] = 0;
    $shift[2][0] = 6;
    $shift[2][1] = 0;
    $shift[3][0] = 12;
    $shift[3][1] = 6;
    $shift[3][2] = 0;
    $shift[4][0] = 18;
    $shift[4][1] = 12;
    $shift[4][2] = 6;
    $shift[4][3] = 0;

    $pos = 0;
    $len = strlen ($source);
    $encodedString = '';
    while ($pos < $len) {
        $asciiPos = ord (substr ($source, $pos, 1));
        if (($asciiPos >= 240) && ($asciiPos <= 255)) {
            // 4 chars representing one unicode character
            $thisLetter = substr ($source, $pos, 4);
            $pos += 4;
        }
        else if (($asciiPos >= 224) && ($asciiPos <= 239)) {
            // 3 chars representing one unicode character
            $thisLetter = substr ($source, $pos, 3);
            $pos += 3;
        }
        else if (($asciiPos >= 192) && ($asciiPos <= 223)) {
            // 2 chars representing one unicode character
            $thisLetter = substr ($source, $pos, 2);
            $pos += 2;
        }
        else {
            // 1 char (lower ascii)
            $thisLetter = substr ($source, $pos, 1);
            $pos += 1;
        }

        // process the string representing the letter to a unicode entity
        $thisLen = strlen ($thisLetter);
        $thisPos = 0;
        $decimalCode = 0;
        while ($thisPos < $thisLen) {
            $thisCharOrd = ord (substr ($thisLetter, $thisPos, 1));
            if ($thisPos == 0) {
                $charNum = intval ($thisCharOrd - $decrement[$thisLen]);
                $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
            }
            else {
                $charNum = intval ($thisCharOrd - 128);
                $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
            }

            $thisPos++;
        }

        if ($thisLen == 1)
            $encodedLetter = "&#". str_pad($decimalCode, 3, "0", STR_PAD_LEFT) . ';';
        else
            $encodedLetter = "&#". str_pad($decimalCode, 5, "0", STR_PAD_LEFT) . ';';

        $encodedString .= $encodedLetter;
    }

    return $encodedString;
}
?>

Ronen.


Encodes an ISO-8859-1 string to UTF-8

  • ≪ utf8_decode
  • xml_error_string ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Encodes an ISO-8859-1 string to UTF-8

utf8_encode

(PHP 4, PHP 5, PHP 7)

utf8_encode — Encodes an ISO-8859-1 string to UTF-8

설명

string utf8_encode ( string $data )

This function converts the string data from the ISO-8859-1 encoding to UTF-8.

Note:

Many web pages marked as using the ISO-8859-1 character encoding actually use the similar Windows-1252 encoding, and web browsers will interpret ISO-8859-1 web pages as Windows-1252. Windows-1252 features additional printable characters, such as the Euro sign (€) and curly quotes (" "), instead of certain ISO-8859-1 control characters. This function will not convert such Windows-1252 characters correctly. Use a different function if Windows-1252 conversion is required.

인수

data

An ISO-8859-1 string.

반환값

Returns the UTF-8 translation of data.

참고

  • utf8_decode() - Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1 - Performs the reverse conversion
  • mb_convert_encoding() - Convert character encoding - Converts between various character encodings, including UTF-8, ISO-8859-1 and Windows-1252
  • iconv() - Convert string to requested character encoding - Converts between various character encodings
  • recode_string() - Recode a string according to a recode request - Converts between various character encodings

User Contributed Notes

Pini 17-Nov-2015 12:14
My version of utf8_encode_deep,
In case you need one that returns a value without changing the original.

        /**
        * Convert Anything To UTF-8
        * @param mixed $var The variable you want to convert.
        * @param boolean $deep Deep convertion? (*Default: TRUE).
        * @return mixed
        */
        function anything_to_utf8($var,$deep=TRUE){
            if(is_array($var)){
                foreach($var as $key => $value){
                    if($deep){
                        $var[$key] = anything_to_utf8($value,$deep);
                    }elseif(!is_array($value) && !is_object($value) && !mb_detect_encoding($value,'utf-8',true)){
                         $var[$key] = utf8_encode($var);
                    }
                }
                return $var;
            }elseif(is_object($var)){
                foreach($var as $key => $value){
                    if($deep){
                        $var->$key = anything_to_utf8($value,$deep);
                    }elseif(!is_array($value) && !is_object($value) && !mb_detect_encoding($value,'utf-8',true)){
                         $var->$key = utf8_encode($var);
                    }
                }
                return $var;
            }else{
                return (!mb_detect_encoding($var,'utf-8',true))?utf8_encode($var):$var;
            }
        }
a dot rueedlinger at gmail dot com 22-Jun-2013 10:06
If you need a function which converts a string array into a utf8 encoded string array then this function might be useful for you:

<?php
function utf8_string_array_encode(&$array){
    $func = function(&$value,&$key){
        if(is_string($value)){
            $value = utf8_encode($value);
        }
        if(is_string($key)){
            $key = utf8_encode($key);
        }
        if(is_array($value)){
            utf8_string_array_encode($value);
        }
    };
    array_walk($array,$func);
    return $array;
}
?>
Oscar Broman 06-Sep-2012 08:15
Walk through nested arrays/objects and utf8 encode all strings.

<?php
// Usage
class Foo {
    public $somevar = 'whoop whoop';
}

$structure = array(
    'object' => (object) array(
        'entry' => 'hello world',
        'another_array' => array(
            'string',
            1234,
            'another string'
        )
    ),
    'string' => 'foo',
    'foo_object' => new Foo
);

utf8_encode_deep($structure);

// $structure is now utf8 encoded
print_r($structure);

// The function
function utf8_encode_deep(&$input) {
    if (is_string($input)) {
        $input = utf8_encode($input);
    } else if (is_array($input)) {
        foreach ($input as &$value) {
            utf8_encode_deep($value);
        }

        unset($value);
    } else if (is_object($input)) {
        $vars = array_keys(get_object_vars($input));

        foreach ($vars as $var) {
            utf8_encode_deep($input->$var);
        }
    }
}
?>
deceze at gmail dot com 15-Jul-2011 03:51
Please note that utf8_encode only converts a string encoded in ISO-8859-1 to UTF-8. A more appropriate name for it would be "iso88591_to_utf8". If your text is not encoded in  ISO-8859-1, you do not need this function. If your text is already in UTF-8, you do not need this function. In fact, applying this function to text that is not encoded in ISO-8859-1 will most likely simply garble that text.

If you need to convert text from any encoding to any other encoding, look at iconv() instead.
bastianschwarz t live punkt de 13-May-2011 05:25
My version for converting ISO array keys to utf8:

<?php
function convertArrayKeysToUtf8(array $array) {
    $convertedArray = array();
    foreach($array as $key => $value) {
      if(!mb_check_encoding($key, 'UTF-8')) $key = utf8_encode($key);
      if(is_array($value)) $value = $this->convertArrayKeysToUtf8($value);

      $convertedArray[$key] = $value;
    }
    return $convertedArray;
  }
?>
powtac 4t gmx d0t de 11-Feb-2011 10:11
I tried a lot of things, but this seems to be the final fail save method to convert any string to proper UTF-8.

<?php
function _convert($content) {
    if(!mb_check_encoding($content, 'UTF-8')
        OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {

        $content = mb_convert_encoding($content, 'UTF-8');

        if (mb_check_encoding($content, 'UTF-8')) {
            // log('Converted to UTF-8');
        } else {
            // log('Could not converted to UTF-8');
        }
    }
    return $content;
}
?>
Yumok 15-Sep-2010 01:26
Avoiding use of preg_match to detect if utf8_encode is needed:

<?php
                $string = $string_input; // avoid being destructive

                $string = preg_replace("#[\x09\x0A\x0D\x20-\x7E]#"        ,"",$string);         // ASCII
                $string = preg_replace("#[\xC2-\xDF][\x80-\xBF]#"            ,"",$string);             // non-overlong 2-byte
                $string = preg_replace("#\xE0[\xA0-\xBF][\x80-\xBF]#"    ,"",$string);     // excluding overlongs
                $string = preg_replace("#[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}#","",$string);     // straight 3-byte
                $string = preg_replace("#\xED[\x80-\x9F][\x80-\xBF]#"    ,"",$string);     // excluding surrogates
                $string = preg_replace("#\xF0[\x90-\xBF][\x80-\xBF]{2}#","",$string);     // planes 1-3
                $string = preg_replace("#[\xF1-\xF3][\x80-\xBF]{3}#"    ,"",$string);     //  planes 4-15
                $string = preg_replace("#\xF4[\x80-\x8F][\x80-\xBF]{2}#","",$string);     // plane 16

                $rc = ($string == ""?true:false);
?>
darkenergy at hispeed dot ch 10-Aug-2010 03:15
to encode a string only if it is not yet UTF-8, the most elegant solution i found is:

<?php
//$s is a string from whatever source
mb_detect_encoding($s, "UTF-8") == "UTF-8" ? : $s = utf8_encode($s);
?>

hope it helps
squeegee 26-Aug-2009 03:20
I think this is a reasonable port of Perl's Encoding::FixLatin by Grant McLean, which converts a string with mixed encodings (ASCII, ISO-8859-1, CP1252, and UTF-8) to UTF-8.

<?php

function init_byte_map(){
  global $byte_map;
  for($x=128;$x<256;++$x){
    $byte_map[chr($x)]=utf8_encode(chr($x));
  }
  $cp1252_map=array(
    "\x80"=>"\xE2\x82\xAC",    // EURO SIGN
    "\x82" => "\xE2\x80\x9A",  // SINGLE LOW-9 QUOTATION MARK
    "\x83" => "\xC6\x92",      // LATIN SMALL LETTER F WITH HOOK
    "\x84" => "\xE2\x80\x9E",  // DOUBLE LOW-9 QUOTATION MARK
    "\x85" => "\xE2\x80\xA6",  // HORIZONTAL ELLIPSIS
    "\x86" => "\xE2\x80\xA0",  // DAGGER
    "\x87" => "\xE2\x80\xA1",  // DOUBLE DAGGER
    "\x88" => "\xCB\x86",      // MODIFIER LETTER CIRCUMFLEX ACCENT
    "\x89" => "\xE2\x80\xB0",  // PER MILLE SIGN
    "\x8A" => "\xC5\xA0",      // LATIN CAPITAL LETTER S WITH CARON
    "\x8B" => "\xE2\x80\xB9",  // SINGLE LEFT-POINTING ANGLE QUOTATION MARK
    "\x8C" => "\xC5\x92",      // LATIN CAPITAL LIGATURE OE
    "\x8E" => "\xC5\xBD",      // LATIN CAPITAL LETTER Z WITH CARON
    "\x91" => "\xE2\x80\x98",  // LEFT SINGLE QUOTATION MARK
    "\x92" => "\xE2\x80\x99",  // RIGHT SINGLE QUOTATION MARK
    "\x93" => "\xE2\x80\x9C",  // LEFT DOUBLE QUOTATION MARK
    "\x94" => "\xE2\x80\x9D",  // RIGHT DOUBLE QUOTATION MARK
    "\x95" => "\xE2\x80\xA2",  // BULLET
    "\x96" => "\xE2\x80\x93",  // EN DASH
    "\x97" => "\xE2\x80\x94",  // EM DASH
    "\x98" => "\xCB\x9C",      // SMALL TILDE
    "\x99" => "\xE2\x84\xA2",  // TRADE MARK SIGN
    "\x9A" => "\xC5\xA1",      // LATIN SMALL LETTER S WITH CARON
    "\x9B" => "\xE2\x80\xBA",  // SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
    "\x9C" => "\xC5\x93",      // LATIN SMALL LIGATURE OE
    "\x9E" => "\xC5\xBE",      // LATIN SMALL LETTER Z WITH CARON
    "\x9F" => "\xC5\xB8"       // LATIN CAPITAL LETTER Y WITH DIAERESIS
  );
  foreach($cp1252_map as $k=>$v){
    $byte_map[$k]=$v;
  }
}

function fix_latin($instr){
  if(mb_check_encoding($instr,'UTF-8'))return $instr; // no need for the rest if it's all valid UTF-8 already
  global $nibble_good_chars,$byte_map;
  $outstr='';
  $char='';
  $rest='';
  while((strlen($instr))>0){
    if(1==preg_match($nibble_good_chars,$input,$match)){
      $char=$match[1];
      $rest=$match[2];
      $outstr.=$char;
    }elseif(1==preg_match('@^(.)(.*)$@s',$input,$match)){
      $char=$match[1];
      $rest=$match[2];
      $outstr.=$byte_map[$char];
    }
    $instr=$rest;
  }
  return $outstr;
}

$byte_map=array();
init_byte_map();
$ascii_char='[\x00-\x7F]';
$cont_byte='[\x80-\xBF]';
$utf8_2='[\xC0-\xDF]'.$cont_byte;
$utf8_3='[\xE0-\xEF]'.$cont_byte.'{2}';
$utf8_4='[\xF0-\xF7]'.$cont_byte.'{3}';
$utf8_5='[\xF8-\xFB]'.$cont_byte.'{4}';
$nibble_good_chars = "@^($ascii_char+|$utf8_2|$utf8_3|$utf8_4|$utf8_5)(.*)$@s";

?>

Then just call fix_latin wherever you need it.
mrezair at azarbod dot com 23-Mar-2009 07:19
I found this little function very useful in fixing strings that are not in utf-8 but need be converted

<?php
// Fixes the encoding to uf8
function fixEncoding($in_str)
{
  $cur_encoding = mb_detect_encoding($in_str) ;
  if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8"))
    return $in_str;
  else
    return utf8_encode($in_str);
} // fixEncoding
?>
rogeriogirodo at gmail dot com 19-Mar-2009 01:24
This function may be useful do encode array keys and values [and checks first to see if it's already in UTF format]:

<?php
public static function to_utf8($in)
{
        if (is_array($in)) {
            foreach ($in as $key => $value) {
                $out[to_utf8($key)] = to_utf8($value);
            }
        } elseif(is_string($in)) {
            if(mb_detect_encoding($in) != "UTF-8")
                return utf8_encode($in);
            else
                return $in;
        } else {
            return $in;
        }
        return $out;
}
?>

Hope this may help.

[NOTE BY danbrown AT php DOT net: Original function written by (cmyk777 AT gmail DOT com) on 28-JAN-09.]
Julio Cesar 20-Jan-2009 02:08
With This Script you can convert a lot of files in
subfolders and convert to UTF8 without problems!

I thought about that when I was converting an eclipse
Project to UTF-8 and I loose all the Accentuation O.o

But with this script YOU WILL NOT! ;-)

I Make this based on Aidan Kehoe's Script and webmaster at
asylum-et dot com of http://www.php.net/scandir:

<?php
ini_set("implicit_flush", "on");
ini_set("max_execution_time", 0);
ini_set("register_argc_argv", "on");
ini_set("html_errors", "Off");

function cp1252_to_utf8($str) {
    $cp1252_map = array ("\xc2\x80" => "\xe2\x82\xac",
    "\xc2\x82" => "\xe2\x80\x9a",
    "\xc2\x83" => "\xc6\x92",
    "\xc2\x84" => "\xe2\x80\x9e",
    "\xc2\x85" => "\xe2\x80\xa6",
    "\xc2\x86" => "\xe2\x80\xa0",
    "\xc2\x87" => "\xe2\x80\xa1",
    "\xc2\x88" => "\xcb\x86",
    "\xc2\x89" => "\xe2\x80\xb0",
    "\xc2\x8a" => "\xc5\xa0",
    "\xc2\x8b" => "\xe2\x80\xb9",
    "\xc2\x8c" => "\xc5\x92",
    "\xc2\x8e" => "\xc5\xbd",
    "\xc2\x91" => "\xe2\x80\x98",
    "\xc2\x92" => "\xe2\x80\x99",
    "\xc2\x93" => "\xe2\x80\x9c",
    "\xc2\x94" => "\xe2\x80\x9d",
    "\xc2\x95" => "\xe2\x80\xa2",
    "\xc2\x96" => "\xe2\x80\x93",
    "\xc2\x97" => "\xe2\x80\x94",

    "\xc2\x98" => "\xcb\x9c",
    "\xc2\x99" => "\xe2\x84\xa2",
    "\xc2\x9a" => "\xc5\xa1",
    "\xc2\x9b" => "\xe2\x80\xba",
    "\xc2\x9c" => "\xc5\x93",
    "\xc2\x9e" => "\xc5\xbe",
    "\xc2\x9f" => "\xc5\xb8"
);
    return strtr ( utf8_encode ( $str ), $cp1252_map );
}
function rscandir($base="", &$data=array()) {

  $array = array_diff(scandir($base), array(".", ".."));

  foreach($array as $value) :

    if (is_dir($base.$value)) :
      //$data[] = $base.$value."/";
      $data = rscandir($base.$value."/", $data);

    elseif (is_file($base.$value) &&
!eregi(".jpg|.gif|.png|.ttf|.dataModel|.wsdlDataModel
|.project|.jsdtscope|.prefs|.name|.container|
.exe|.bat|.cmd|.src|.dll|.ini|.swf|.fla|.bmp\$",
$value)) : /* where you put the unwanted extensions  */
    echo "Converting to UTF8 " . $base.$value . "\r\n";

file_put_contents(
        $base.$value,
            cp1252_to_utf8(
            file_get_contents($base.$value)));

    endif;

  endforeach;

  return $data;

}
echo "Type a Folder (With a Slash in end): ";
$folder = trim(fgets(STDIN));

rscandir($folder);

?>

You can put this on windows Dir and put a Batch like this:

@echo off
php -n C:\windows\ConvertUTF8.php
pause

So you can convert your files from any where, just type on
Execute Command Like: ConvertFilesToUTF8

I think this will help everyone! Enjoy ;-)

P.s: I remove the comments becouse the wordwrap
bitseeker 22-Sep-2008 11:07
...or just use this simple piece of code to check valid utf-8 string:

<?php
    /**
     * Returns true if $string is valid UTF-8 and false otherwise.
     *
     * @since        1.14
     * @param [mixed] $string     string to be tested
     * @subpackage
     */
    function is_utf8($string) {

        // From http://w3.org/International/questions/qa-forms-utf-8.html
        return preg_match('%^(?:
              [\x09\x0A\x0D\x20-\x7E]            # ASCII
            | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
            |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
            | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
            |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
            |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
            | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
            |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
        )*$%xs', $string);

    }
?>
hmdker at gmail dot com 24-Aug-2008 06:49
Here's my is_utf8 function, to detect valid UTF-8 text.

<?php
function is_utf8($str) {
    $c=0; $b=0;
    $bits=0;
    $len=strlen($str);
    for($i=0; $i<$len; $i++){
        $c=ord($str[$i]);
        if ($c >= 128) {
            if(($c >= 254)) return false;
            elseif($c >= 252) $bits=6;
            elseif($c >= 248) $bits=5;
            elseif($c >= 240) $bits=4;
            elseif($c >= 224) $bits=3;
            elseif($c >= 192) $bits=2;
            else return false;
            if(($i+$bits) > $len) return false;
            while($bits > 1){
                $i++;
                $b=ord($str[$i]);
                if($b < 128 || $b > 191) return false;
                $bits--;
            }
        }
    }
    return true;
}

?>

[NOTE BY danbrown AT php DOT net: Contains a bugfix supplied by "svenmika" on 09-JUL-09 with the following note: "A value of exactly 128 (binary 10000000) in the first byte of a character is invalid."]
akam 30-Jun-2008 04:14
<?php
// Author akam at akameng dot com
// Support 6 bit
function UTF_to_Unicode($input, $array=False) {

 $bit1  = pow(64, 0);
 $bit2  = pow(64, 1);
 $bit3  = pow(64, 2);
 $bit4  = pow(64, 3);
 $bit5  = pow(64, 4);
 $bit6  = pow(64, 5);

 $value = '';
 $val   = array();

 for($i=0; $i< strlen( $input ); $i++){

     $ints = ord ( $input[$i] );

     $z     = ord ( $input[$i] );
     $y     = ord ( $input[$i+1] ) - 128;
     $x     = ord ( $input[$i+2] ) - 128;
     $w     = ord ( $input[$i+3] ) - 128;
     $v     = ord ( $input[$i+4] ) - 128;
     $u     = ord ( $input[$i+5] ) - 128;

     if( $ints >= 0 && $ints <= 127 ){
        // 1 bit
        $value .= '&#'.($z * $bit1).';';
        $val[]  = $value;
     }
     if( $ints >= 192 && $ints <= 223 ){
        // 2 bit
        $value .= '&#'.(($z-192) * $bit2 + $y * $bit1).';';
        $val[]  = $value;
     }
     if( $ints >= 224 && $ints <= 239 ){
        // 3 bit
        $value .= '&#'.(($z-224) * $bit3 + $y * $bit2 + $x * $bit1).';';
        $val[]  = $value;
     }
     if( $ints >= 240 && $ints <= 247 ){
        // 4 bit
        $value .= '&#'.(($z-240) * $bit4 + $y * $bit3 +
$x * $bit2 + $w * $bit1).';';
        $val[]  = $value;
     }
     if( $ints >= 248 && $ints <= 251 ){
        // 5 bit
        $value .= '&#'.(($z-248) * $bit5 + $y * $bit4
+ $x * $bit3 + $w * $bit2 + $v * $bit1).';';
        $val[]  = $value;
     }
     if( $ints == 252 && $ints == 253 ){
        // 6 bit
        $value .= '&#'.(($z-252) * $bit6 + $y * $bit5
+ $x * $bit4 + $w * $bit3 + $v * $bit2 + $u * $bit1).';';
        $val[]  = $value;
     }
     if( $ints == 254 || $ints == 255 ){
       echo 'Wrong Result!<br>';
     }

 }

 if( $array === False ){
    return $unicode = $value;
 }
 if($array === True ){
     $val     = str_replace('&#', '', $value);
     $val     = explode(';', $val);
     $len = count($val);
     unset($val[$len-1]);

     return $unicode = $val;
 }

}

function Unicode_to_UTF( $input, $array=TRUE){

     $utf = '';
    if(!is_array($input)){
       $input     = str_replace('&#', '', $input);
       $input     = explode(';', $input);
       $len = count($input);
       unset($input[$len-1]);
    }
    for($i=0; $i < count($input); $i++){

    if ( $input[$i] <128 ){
       $byte1 = $input[$i];
       $utf  .= chr($byte1);
    }
    if ( $input[$i] >=128 && $input[$i] <=2047 ){

       $byte1 = 192 + (int)($input[$i] / 64);
       $byte2 = 128 + ($input[$i] % 64);
       $utf  .= chr($byte1).chr($byte2);
    }
    if ( $input[$i] >=2048 && $input[$i] <=65535){

       $byte1 = 224 + (int)($input[$i] / 4096);
       $byte2 = 128 + ((int)($input[$i] / 64) % 64);
       $byte3 = 128 + ($input[$i] % 64);

       $utf  .= chr($byte1).chr($byte2).chr($byte3);
    }
    if ( $input[$i] >=65536 && $input[$i] <=2097151){

       $byte1 = 240 + (int)($input[$i] / 262144);
       $byte2 = 128 + ((int)($input[$i] / 4096) % 64);
       $byte3 = 128 + ((int)($input[$i] / 64) % 64);
       $byte4 = 128 + ($input[$i] % 64);
       $utf  .= chr($byte1).chr($byte2).chr($byte3).
chr($byte4);
    }
    if ( $input[$i] >=2097152 && $input[$i] <=67108863){

       $byte1 = 248 + (int)($input[$i] / 16777216);
       $byte2 = 128 + ((int)($input[$i] / 262144) % 64);
       $byte3 = 128 + ((int)($input[$i] / 4096) % 64);
       $byte4 = 128 + ((int)($input[$i] / 64) % 64);
       $byte5 = 128 + ($input[$i] % 64);
       $utf  .= chr($byte1).chr($byte2).chr($byte3).
chr($byte4).chr($byte5);
    }
    if ( $input[$i] >=67108864 && $input[$i] <=2147483647){

       $byte1 = 252 + ($input[$i] / 1073741824);
       $byte2 = 128 + (($input[$i] / 16777216) % 64);
       $byte3 = 128 + (($input[$i] / 262144) % 64);
       $byte4 = 128 + (($input[$i] / 4096) % 64);
       $byte5 = 128 + (($input[$i] / 64) % 64);
       $byte6 = 128 + ($input[$i] % 64);
       $utf  .= chr($byte1).chr($byte2).chr($byte3).
chr($byte4).chr($byte5).chr($byte6);
    }
   }
   return $utf;
}
?>
www.tricinty.com 11-Jun-2008 12:43
<?php
    /**
    * Encodes an ISO-8859-1 mixed variable to UTF-8 (PHP 4, PHP 5 compat)
    * @param    mixed    $input An array, associative or simple
    * @param    boolean  $encode_keys optional
    * @return    mixed     ( utf-8 encoded $input)
    */

    function utf8_encode_mix($input, $encode_keys=false)
    {
        if(is_array($input))
        {
            $result = array();
            foreach($input as $k => $v)
            {
                $key = ($encode_keys)? utf8_encode($k) : $k;
                $result[$key] = utf8_encode_mix( $v, $encode_keys);
            }
        }
        else
        {
            $result = utf8_encode($input);
        }

        return $result;
    }
?>
klein at buchung-24 dot de 04-Jun-2008 01:22
IF you don´t use the function from ethan dot nelson at ltd dot org in a class, you´ll get an error, so please try

function utf_prepare(&$array)
{
    foreach($array AS $key => &$value)
    {
        if (is_array($value))
        {
            utf_prepare($value);
        } else
        {
            $value = utf8_encode($value);
        }
    }
}
www.qaiser.net 17-Apr-2008 04:56
that isUTF8 function is a killer...

wouldn't something like

if ( preg_match( "~(\x00[\x80-\xff]|[\x00-\x07][\x00-\xff]~", $string ) ) { /* is utf */ };

be a lot more efficient? it doesn't take into account all the ranges, but it has to be a better method and a simple start since it'll quit on the first successful match. think of encoding and decoding a 1mb string--not good. i'm having to work with +20 meg xml files.
renardo13 at free dot fr 01-Apr-2008 01:56
another nice way to implement an isUTF8 function ...

<?php

function isUTF8($string)
{
    return (utf8_encode(utf8_decode($string)) == $string);
}

?>
ethan dot nelson at ltd dot org 07-Nov-2007 02:41
This does the same thing as some of the posts below (minus the keys), but I thought I'd share anyway cause it is slightly more elegant.  Also, its a good example using references such that this could be used as a callback function.

  function utf_prepare(&$array) {

    foreach($array AS $key => &$value) {

      if (is_array($value)) {
        $this->utf_prepare($value);
      } else {
        $value = utf8_encode($value);
      }

    }

  }
luka8088 at gmail dot com 22-Jun-2007 04:19
simple HTML to UTF-8 conversion:

function html_to_utf8 ($data)
    {
    return preg_replace("/\\&\\#([0-9]{3,10})\\;/e", '_html_to_utf8("\\1")', $data);
    }

function _html_to_utf8 ($data)
    {
    if ($data > 127)
        {
        $i = 5;
        while (($i--) > 0)
            {
            if ($data != ($a = $data % ($p = pow(64, $i))))
                {
                $ret = chr(base_convert(str_pad(str_repeat(1, $i + 1), 8, "0"), 2, 10) + (($data - $a) / $p));
                for ($i; $i > 0; $i--)
                    $ret .= chr(128 + ((($data % pow(64, $i)) - ($data % ($p = pow(64, $i - 1)))) / $p));
                break;
                }
            }
        }
        else
        $ret = "&#$data;";
    return $ret;
    }

Example:
echo html_to_utf8("a b &#269; &#263; &#382; &#12371; &#12395; &#12385; &#12431; ()[]{}!#$?* &lt; &#62;");

Output:
a b ? ? ? こ に ち わ ()[]{}!#$?* &lt; &#62;
hillar dot petersen at gmail dot com 30-May-2007 07:59
In addition to my previous post. If your values are already in utf-8 maybe you want to utf8_encode array keys only. This will do it:

<?php
/**
 * (Recursively) utf8_encode all array keys.
 *
 * @param array $array
 * @return array with utf8_encoded keys
 */

function utf8_encode_array_keys($array)
{
  $array_type = array_type($array);

  if ($array_type == "map")
  {
    $result_array = array();

    foreach($array as $key => $value)
    {
      if (is_array($value))
      {
        // recursion
        $result_array[utf8_encode($key)] = utf8_encode_array_keys($value);
      }
      else
      {
        // value is not an array, no recursion
        $result_array[utf8_encode($key)] = $value;
      }
    }

    return $result_array;
  }

  else if ($array_type == "vector")
  {
    // do not encode anything, just follow the value if it is an array
    $result_array = array();

    foreach ($array as $key => $value)
    {
      if (is_array($value))
      {
        // recursion
        $result_array[$key] = utf8_encode_array_keys($value);
      }
      else
      {
        // value is not an array, no recursion
        $result_array[$key] = $value;
      }
    }

    return $result_array;
  }

  return false;     // argument is not an array, return false
}
?>

Also note that both this operation (with keys only) and the operation with both keys and values can be reversed by replacing "encode" by "decode".
hillar dot petersen at gmail dot com 29-May-2007 04:06
If you are interested in recursively converting ISO-8859-1-encoded arrays into UTF-8, then this is one way to do it. Could use a small refactor though. (I used it to prepare some ISO-8859-1 arrays for json_encode. Note that for this to work your values and for associative arrays also your keys must be ISO-8859-1-encoded.)

<?php
/**
 * (Recursively) utf8_encode each value in an array.
 *
 * @param array $array
 * @return array utf8_encoded
 */

function utf8_encode_array($array)
{
  if (is_array($array))
  {
    $result_array = array();

    foreach($array as $key => $value)
    {

      if (array_type($array) == "map")
      {
        // encode both key and value

        if (is_array($value))
        {
          // recursion
          $result_array[utf8_encode($key)] = utf8_encode_array($value);
        }
        else
        {
          // no recursion
          if (is_string($value))
          {
            $result_array[utf8_encode($key)] = utf8_encode($value);
          }
          else
          {
            // do not re-encode non-strings, just copy data
            $result_array[utf8_encode($key)] = $value;
          }

        }

      }

      else if (array_type($array) == "vector")
      {
        // encode value only

        if (is_array($value))
        {
          // recursion
          $result_array[$key] = utf8_encode_array($value);
        }
        else
        {
          // no recursion

          if (is_string($value))
          {
            $result_array[$key] = utf8_encode($value);
          }
          else
          {
            // do not re-encode non-strings, just copy data
            $result_array[$key] = $value;
          }

        }

      }

    }

    return $result_array;
  }

  return false;     // argument is not an array, return false
}

/**
 * Determines array type ("vector" or "map"). Returns false if not an array at all.
 * (I hope a native function will be introduced in some future release of PHP, because
 * this check is inefficient and quite costly in worst case scenario.)
 *
 * @param array $array The array to analyze
 * @return string array type ("vector" or "map") or false if not an array
 */

function array_type($array)
{
  if (is_array($array))
  {
    $next = 0;

    $return_value = "vector";  // we have a vector until proved otherwise

    foreach ($array as $key => $value)
    {

      if ($key != $next)
      {
        $return_value = "map";  // we have a map
        break;
      }

      $next++;
    }

    return $return_value;
  }

  return false;    // not array
}
?>
nikooo adog bk adot ru - Nickolaz 03-May-2007 04:02
You can use this simple code to convert win-1251 into Unicode.

    function rus2uni($str,$isTo = true)
    {
        $arr = array('ё'=>'&#x451;','Ё'=>'&#x401;');
        for($i=192;$i<256;$i++)
            $arr[chr($i)] = '&#x4'.dechex($i-176).';';
        $str =preg_replace(array('@([а-я]) @i','@ ([а-я])@i'),array('$1&#x0a0;','&#x0a0;$1'),$str);
        return strtr($str,$isTo?$arr:array_flip($arr));
    }

That is useful for xml_parser (to parse windows-1251 files like utf-8).
18-Apr-2007 06:06
I just read what I wrote, sorry for the typos it was a long day:

here's the rewritten code:

xml_tpl.php
<?php
    header("Content-Type: text/html;charset=ISO-8859-1");
    print "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
    $names=array('jack','bob','vanessa','catherine','valerie');
?>
<parent>
<?php foreach($names as $name) {?>
    <child name="<?php print $name?>" />
<?php } ?>
</parent>

<?php
function create_xml(){
    ob_start();
    include "xml_tpl.php";
    $trapped_content=ob_get_contents();
    ob_end_clean();
    $file_path= "./somefile.xml";
    $file_handle=fopen($file_path,'w');
    fwrite($file_handle,utf8_encode($trapped_content));
}

?>
penda ekoka 17-Apr-2007 08:15
creating utf-8 xml files:
this is something that has wasted a lot of my time, I hope this will spare you the headaches:

my method consists of creating an xml template that will look like this (this is probably optional, I'm sure you can use good ol' print or echo statements):

xml_tpl.php
<?php
header("Content-Type: text/html;charset=ISO-8859-1");
print "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
$names=array('jack','bob','vanessa','catherine','valerie');
?>
<parent>
<?php foreach($names as $name) {?>
    <child name="<?php print $name?>" />
<?php } ?>
</parent>
?>

from a function or a method I include the previous template and trap the outputted content in an output buffer. The buffured content is then inserted into a file:

<?php
function create_xml(){
    ob_start();
    include "xml_php.php";
    $trapped_content=ob_get_contents();
    ob_end_clean();
    $file_path= "./somefile.xml";
    $file_handle=fopen($somefile,'w');
    fwrite($file_handle,utf8_encode($trapped_content));
}

?>

Some side notes:
- note that the utf8_encode function goes inside the fwrite() function.
- when troubleshooting, make sure to transfer text file (xml included) and scripts in ascii mode when using ftp. For some unknown reason my ftp client did not have xml set as an ascii transfer candidate and was automatically tranfering them in binary. That little "feature" ended up costing me hours of frustration, as the encoding information would just "vanish" between transfer and I kept scratching my head as to why manually created utf8 files were not behaving as they should.
emze at donazga dot net 17-Dec-2006 06:42
/*
Every function seen so far is incomplete or resource consumpting. Here are two -- integer 2 utf sequence (i3u) and utf sequence to integer (u3i). Below is a code snippet that checks well behavior at the range boundaries.

Someday they might be hardcoded into PHP...
*/

function i3u($i) { // returns UCS-16 or UCS-32 to UTF-8 from an integer
  $i=(int)$i; // integer?
  if ($i<0) return false; // positive?
  if ($i<=0x7f) return chr($i); // range 0
  if (($i & 0x7fffffff) <> $i) return '?'; // 31 bit?
  if ($i<=0x7ff) return chr(0xc0 | ($i >> 6)) . chr(0x80 | ($i & 0x3f));
  if ($i<=0xffff) return chr(0xe0 | ($i >> 12)) . chr(0x80 | ($i >> 6) & 0x3f)
      . chr(0x80  | $i & 0x3f);
  if ($i<=0x1fffff) return chr(0xf0 | ($i >> 18)) . chr(0x80 | ($i >> 12) & 0x3f)
      . chr(0x80 | ($i >> 6) & 0x3f) . chr(0x80  | $i & 0x3f);
  if ($i<=0x3ffffff) return chr(0xf8 | ($i >> 24)) . chr(0x80 | ($i >> 18) & 0x3f)
      . chr(0x80 | ($i >> 12) & 0x3f) . chr(0x80 | ($i >> 6) & 0x3f) . chr(0x80  | $i & 0x3f);
  return chr(0xfc | ($i >> 30)) . chr(0x80 | ($i >> 24) & 0x3f) . chr(0x80 | ($i >> 18) & 0x3f)
      . chr(0x80 | ($i >> 12) & 0x3f) . chr(0x80 | ($i >> 6) & 0x3f) . chr(0x80  | $i & 0x3f);
}

function u3i($s,$strict=1) { // returns integer on valid UTF-8 seq, NULL on empty, else FALSE
  // NOT strict: takes only DATA bits, present or not; strict: length and bits checking
  if ($s=='') return NULL;
  $l=strlen($s); $o=ord($s{0});
  if ($o <= 0x7f && $l==1) return $o;
  if ($l>6 && $strict) return false;
  if ($strict) for ($i=1;$i<$l;$i++) if (ord($s{$i}) > 0xbf || ord($s{$i})< 0x80) return false;
  if ($o < 0xc2) return false; // no-go even if strict=0
  if ($o <= 0xdf && ($l=2 && $strict)) return (($o & 0x1f) << 6 | (ord($s{1}) & 0x3f));
  if ($o <= 0xef && ($l=3 && $strict)) return (($o & 0x0f) << 12 | (ord($s{1}) & 0x3f) << 6
     |  (ord($s{2}) & 0x3f));
  if ($o <= 0xf7 && ($l=4 && $strict)) return (($o & 0x07) << 18 | (ord($s{1}) & 0x3f) << 12
     | (ord($s{2}) & 0x3f) << 6 |  (ord($s{3}) & 0x3f));
  if ($o <= 0xfb && ($l=5 && $strict)) return (($o & 0x03) << 24 | (ord($s{1}) & 0x3f) << 18
     | (ord($s{2}) & 0x3f) << 12 | (ord($s{3}) & 0x3f) << 6 |  (ord($s{4}) & 0x3f));
  if ($o <= 0xfd && ($l=6 && $strict)) return (($o & 0x01) << 30 | (ord($s{1}) & 0x3f) << 24
     | (ord($s{2}) & 0x3f) << 18 | (ord($s{3}) & 0x3f) << 12
     | (ord($s{4}) & 0x3f) << 6 |  (ord($s{5}) & 0x3f));
  return false;
}

// boundary behavior checking
$do=array(0x7f,0x7ff,0xffff,0x1fffff,0x3ffffff,0x7fffffff);
foreach ($do as $ii) for ($i=$ii;$i<=$ii+1; $i++) {
  $o=i3u($i);
  for ($j=0;$j<strlen($o);$j++) print "O[$j]=" . sprintf('%08b',ord($o{$j})) . ", ";
  print "c=$i, o=[$o].\n";
  print "Back: [$o] => [" . u3i($o) . "]\n";
}
sadikkeskin at hotmail dot com 21-Nov-2006 11:49
i wrote a function to convert encoding utf8 to iso-8859-9. This function is very useful if you want to use this for ajax.
you can apply same way for other languages.
<?
function str_encode ($string,$to="iso-8859-9",$from="utf8") {
    if($to=="iso-8859-9" && $from=="utf8"){
        $str_array = array(
       chr(196).chr(177) => chr(253),
       chr(196).chr(176) => chr(221),
       chr(195).chr(182) => chr(246),
       chr(195).chr(150) => chr(214),
       chr(195).chr(167) => chr(231),
       chr(195).chr(135) => chr(199),
       chr(197).chr(159) => chr(254),
       chr(197).chr(158) => chr(222),
       chr(196).chr(159) => chr(240),
       chr(196).chr(158) => chr(208),
       chr(195).chr(188) => chr(252),
       chr(195).chr(156) => chr(220)
       );
       return str_replace(array_keys($str_array), array_values($str_array), $string);

    }
    return $string;
}
?>
genert at adsuk dot com 01-Oct-2006 07:23
If you encoded data with utf8_encode function and you would like to decode it in javascript use library found here: http://www.webtoolkit.info/. There is encoder too.
27-Sep-2006 10:30
In reply to Cundle:

Note: The BOM is completely unnecessary in UTF-8. UTF-8 is interpreted the same way regardless of endianness, e.g. Λ (U+039B, GREEK CAPITAL LETTER LAMDA) is represented as the octets 0xCE, 0x9B, always in that order.

Extra note: UTF-16 and UCS-2 are different. The same letter would be encoded as 0x03 0x9B on big-endian (e.g. Motorola) architecture, but 0x9B 0x03 on little-endian (e.g Intel) architecture.

But in any case, there's nothing wrong with putting a BOM at the beginning of a UTF-8 encoded file. It is just treated as U+FEFF Zero Width No-Break Space.
James Cundle 18-Jul-2006 04:33
I had some difficulty finding a way to easily write UTF-8 files with the byte order mark included. This is the simple solution I have come up with:

<?php
function writeUTF8File($filename,$content) {
        $dhandle=fopen($filename,"w");
        # Now UTF-8 - Add byte order mark
        fwrite($dhandle, pack("CCC",0xef,0xbb,0xbf));
        fwrite($dhandle,$content);
        fclose($dhandle);
}
?>

When you read the file back in using fopen, the BOM will also be there. To remove it, I also wrote the following function:

<?php
function removeBOM($str=""){
        if(substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
                $str=substr($str, 3);
        }
        return $str;
}
?>
rocketman 16-Mar-2006 01:46
If you are looking for a function to replace special characters with the hex-utf-8 value (e.g. fur Webservice-Security/WSS4J compliancy) you might use this:

$textstart = "Große";
$utf8 ='';
$max = strlen($txt);

for ($i = 0; $i < $max; $i++) {

if ($txt{i} == "&"){
$neu = "&x26;";
}
elseif ((ord($txt{$i}) < 32) or (ord($txt{$i}) > 127)){
$neu = urlencode(utf8_encode($txt{$i}));
$neu = preg_replace('#\%(..)\%(..)\%(..)#','&#x\1;&#x\2;&#x\3;',$neu);
$neu = preg_replace('#\%(..)\%(..)#','&#x\1;&#x\2;',$neu);
$neu = preg_replace('#\%(..)#','&#x\1;',$neu);
}
else {
$neu = $txt{$i};
}

$utf8 .= $neu;
} // for $i

$textnew = $utf8;

In this example $textnew will be "Gr&#xC3;&#xB6;&#xC3;&#x9F;e"
mailing at jcn50 dot com 21-Jan-2006 07:40
I recommend using this alternative for every language:

$new=mb_convert_encoding($s,"UTF-8","auto");

Don't forget to set all your pages to "utf-8" encoding, otherwise just use HTML entities.

jcn50.
migueldiaz at gennio dot com 14-Dec-2005 06:23
Here's my function to know if one string is encoded in UTF8.

If we encode in UTF8 a string or text file that is already encoded in UTF8, it's expected to find the character '?' ( ALT+159)  in the final string.

<?php

function isUTF8($string)
{
    $string_utf8 = utf8_encode($string);
    if( strpos($string_utf8,"?",0) !== false ) // "?" is ALT+159
         return true;  // the original string was utf8
    else
         return false; // otherwise
}

?>

regards
Miguel D?az
04-Nov-2005 11:34
// Reads a file story.txt ascii (as typed on keyboard)
// converts it to Georgian character using utf8 encoding
// if I am correct(?) just as it should be when typed on Georgian computer
// it outputs it as an html file
//
// http://www.comweb.nl/keys_to_georgian.html
// http://www.comweb.nl/keys_to_georgian.php
// http://www.comweb.nl/story.txt

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<HTML>
<HEAD>
<TITLE>keys to unicode code</TITLE>

// this meta tag is needed
<meta http-equiv="Content-Type" content="text/html;CHARSET=ks_c_5601-1987" >

// note the sylfean font seems to be standard installed on Windows XP
// It supports Georgian

<style TYPE="text/css">
<!--
body {font-family:sylfaen; }
-->
</style>
</HEAD>

<BODY>

<?
$eng=array(97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
112,113,114,115,116,117,118,119,120,121,122,87,82,84,83,
67,74,90);
$geo=array(4304,4305,4330,4307,4308,4324,4306,4336,4312,4335,4313,
4314,4315,4316,4317,4318,4325,4320,4321,4322,4323,4309,
4332,4334,4327,4310,4333,4326,4311,4328,4329,4319,4331,
91,93,59,39,44,46,96);

$fc=file("story.txt");
foreach($fc as $line)
{
   $spacestart=1;
   for ($i=0; $i<strlen($line); $i+=1)
   {
      $character=ord(substr($line,$i,1));
      $found=0;
      for ($k=0; $k<count($eng); $k+=1)
      {
         if ($eng[$k]==$character)
         {
             print code2utf( $geo[$k] );
             $found=1;
         }
      }
      if ($found==0)
      {
         if ($character==126 || $character==32 || $character==10 || $character==9)
         {
            if ($character==9)  { print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'; }
            if ($character==10) { print "<BR>\n"; }
            if ($character==32)
            {
               if ($spacestart==1) {print '&nbsp;'; } else { print " "; }
            }
            if ($character==126){ print "~";      }
         } else
         {
            print substr($line,$i,1);
         }
      }
      if ($character!=32) { $spacestart=0; }
   }
}

/**
 * Function coverts number of utf char into that character.
 * Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336
 *
 * @param int $num
 * @return utf8char
*/
function code2utf($num)
{
   if($num<128)return chr($num);
   if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128);
   if($num<65536)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
   if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
   return '';
}
?>

</BODY>
</HTML>
Janci 04-Nov-2005 01:00
I was searching for a function similar to Javascript's unescape(). In most cases it is OK to use url_decode() function but not if you've got UTF characters in the strings. They are converted into %uXXXX entities that url_decode() cannot handle.
I googled the net and found a function which actualy converts these entities into HTML entities (&#xxx;) that your browser can show correctly. If you're OK with that, the function can be found here: http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps

But it was not OK with me because I needed a string in my charset to make some comparations and other stuff. So I have modified the above function and in conjuction with code2utf() function mentioned in some other note here, I have managed to achieve my goal:

<?php
/**
 * Function converts an Javascript escaped string back into a string with specified charset (default is UTF-8).
 * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps
 *
 * @param string $source escaped with Javascript's escape() function
 * @param string $iconv_to destination character set will be used as second paramether in the iconv function. Default is UTF-8.
 * @return string
 */
function unescape($source, $iconv_to = 'UTF-8') {
    $decodedStr = '';
    $pos = 0;
    $len = strlen ($source);
    while ($pos < $len) {
        $charAt = substr ($source, $pos, 1);
        if ($charAt == '%') {
            $pos++;
            $charAt = substr ($source, $pos, 1);
            if ($charAt == 'u') {
                // we got a unicode character
                $pos++;
                $unicodeHexVal = substr ($source, $pos, 4);
                $unicode = hexdec ($unicodeHexVal);
                $decodedStr .= code2utf($unicode);
                $pos += 4;
            }
            else {
                // we have an escaped ascii character
                $hexVal = substr ($source, $pos, 2);
                $decodedStr .= chr (hexdec ($hexVal));
                $pos += 2;
            }
        }
        else {
            $decodedStr .= $charAt;
            $pos++;
        }
    }

    if ($iconv_to != "UTF-8") {
        $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);
    }

    return $decodedStr;
}

/**
 * Function coverts number of utf char into that character.
 * Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336
 *
 * @param int $num
 * @return utf8char
 */
function code2utf($num){
    if($num<128)return chr($num);
    if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128);
    if($num<65536)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
    if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
    return '';
}
?>
aktionimskript at gmx dot net 01-Sep-2005 05:52
if you want to put variables as parameter in a flashfile, i prefer using to convert the string with utf8_encode() [or preg_replace, or iconv] and after this i encode it with urlencode();

<?php
     $yourstring="yourstring";
     $str_utf8=utf8_encode($yourstring);
     $str_encoded=urlencode($str_utf8);
     echo "<script language='javascript'>";
     echo "parameterForFlash='".$str_encoded."';";
     echo "</script>";
?>

now you can use the variable (parameterForFlash) in your javascript (plugindetection), that writes the flash object/embed.
suttichai at ceforce dot com 28-May-2005 09:26
This function I use convert Thai font (iso-8859-11) to UTF-8. For my case, It work properly. Please try to use this function if you have a problem to convert charset iso-8859-11 to UTF-8.

function iso8859_11toUTF8($string) {

     if ( ! ereg("[\241-\377]", $string) )
         return $string;

     $iso8859_11 = array(
"\xa1" => "\xe0\xb8\x81",
"\xa2" => "\xe0\xb8\x82",
"\xa3" => "\xe0\xb8\x83",
"\xa4" => "\xe0\xb8\x84",
"\xa5" => "\xe0\xb8\x85",
"\xa6" => "\xe0\xb8\x86",
"\xa7" => "\xe0\xb8\x87",
"\xa8" => "\xe0\xb8\x88",
"\xa9" => "\xe0\xb8\x89",
"\xaa" => "\xe0\xb8\x8a",
"\xab" => "\xe0\xb8\x8b",
"\xac" => "\xe0\xb8\x8c",
"\xad" => "\xe0\xb8\x8d",
"\xae" => "\xe0\xb8\x8e",
"\xaf" => "\xe0\xb8\x8f",
"\xb0" => "\xe0\xb8\x90",
"\xb1" => "\xe0\xb8\x91",
"\xb2" => "\xe0\xb8\x92",
"\xb3" => "\xe0\xb8\x93",
"\xb4" => "\xe0\xb8\x94",
"\xb5" => "\xe0\xb8\x95",
"\xb6" => "\xe0\xb8\x96",
"\xb7" => "\xe0\xb8\x97",
"\xb8" => "\xe0\xb8\x98",
"\xb9" => "\xe0\xb8\x99",
"\xba" => "\xe0\xb8\x9a",
"\xbb" => "\xe0\xb8\x9b",
"\xbc" => "\xe0\xb8\x9c",
"\xbd" => "\xe0\xb8\x9d",
"\xbe" => "\xe0\xb8\x9e",
"\xbf" => "\xe0\xb8\x9f",
"\xc0" => "\xe0\xb8\xa0",
"\xc1" => "\xe0\xb8\xa1",
"\xc2" => "\xe0\xb8\xa2",
"\xc3" => "\xe0\xb8\xa3",
"\xc4" => "\xe0\xb8\xa4",
"\xc5" => "\xe0\xb8\xa5",
"\xc6" => "\xe0\xb8\xa6",
"\xc7" => "\xe0\xb8\xa7",
"\xc8" => "\xe0\xb8\xa8",
"\xc9" => "\xe0\xb8\xa9",
"\xca" => "\xe0\xb8\xaa",
"\xcb" => "\xe0\xb8\xab",
"\xcc" => "\xe0\xb8\xac",
"\xcd" => "\xe0\xb8\xad",
"\xce" => "\xe0\xb8\xae",
"\xcf" => "\xe0\xb8\xaf",
"\xd0" => "\xe0\xb8\xb0",
"\xd1" => "\xe0\xb8\xb1",
"\xd2" => "\xe0\xb8\xb2",
"\xd3" => "\xe0\xb8\xb3",
"\xd4" => "\xe0\xb8\xb4",
"\xd5" => "\xe0\xb8\xb5",
"\xd6" => "\xe0\xb8\xb6",
"\xd7" => "\xe0\xb8\xb7",
"\xd8" => "\xe0\xb8\xb8",
"\xd9" => "\xe0\xb8\xb9",
"\xda" => "\xe0\xb8\xba",
"\xdf" => "\xe0\xb8\xbf",
"\xe0" => "\xe0\xb9\x80",
"\xe1" => "\xe0\xb9\x81",
"\xe2" => "\xe0\xb9\x82",
"\xe3" => "\xe0\xb9\x83",
"\xe4" => "\xe0\xb9\x84",
"\xe5" => "\xe0\xb9\x85",
"\xe6" => "\xe0\xb9\x86",
"\xe7" => "\xe0\xb9\x87",
"\xe8" => "\xe0\xb9\x88",
"\xe9" => "\xe0\xb9\x89",
"\xea" => "\xe0\xb9\x8a",
"\xeb" => "\xe0\xb9\x8b",
"\xec" => "\xe0\xb9\x8c",
"\xed" => "\xe0\xb9\x8d",
"\xee" => "\xe0\xb9\x8e",
"\xef" => "\xe0\xb9\x8f",
"\xf0" => "\xe0\xb9\x90",
"\xf1" => "\xe0\xb9\x91",
"\xf2" => "\xe0\xb9\x92",
"\xf3" => "\xe0\xb9\x93",
"\xf4" => "\xe0\xb9\x94",
"\xf5" => "\xe0\xb9\x95",
"\xf6" => "\xe0\xb9\x96",
"\xf7" => "\xe0\xb9\x97",
"\xf8" => "\xe0\xb9\x98",
"\xf9" => "\xe0\xb9\x99",
"\xfa" => "\xe0\xb9\x9a",
"\xfb" => "\xe0\xb9\x9b"
 );

     $string=strtr($string,$iso8859_11);
     return $string;
 }

Suttichai Mesaard-www.ceforce.com
bisqwit at iki dot fi 20-May-2005 10:15
For reference, it may be insightful to point out that:
  utf8_encode($s)
is actually identical to:
  recode_string('latin1..utf8', $s)
and:
  iconv('iso-8859-1', 'utf-8', $s)
That is, utf8_encode is a specialized case of character set conversions.

If your string to be converted to utf-8 is something other than iso-8859-1 (such as iso-8859-2 (Polish/Croatian)), you should use recode_string() or iconv() instead rather than trying to devise complex str_replace statements.
JF Sebastian 09-Apr-2005 12:54
The following Perl regular expression tests if a string is well-formed Unicode UTF-8 (Broken up after each | since long lines are not permitted here. Please join as a single line, no spaces, before use.):

^([\x00-\x7f]|
[\xc2-\xdf][\x80-\xbf]|
\xe0[\xa0-\xbf][\x80-\xbf]|
[\xe1-\xec][\x80-\xbf]{2}|
\xed[\x80-\x9f][\x80-\xbf]|
[\xee-\xef][\x80-\xbf]{2}|
f0[\x90-\xbf][\x80-\xbf]{2}|
[\xf1-\xf3][\x80-\xbf]{3}|
\xf4[\x80-\x8f][\x80-\xbf]{2})*$

NOTE: This strictly follows the Unicode standard 4.0, as described in chapter 3.9, table 3-6, "Well-formed UTF-8 byte sequences" ( http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G31703 ).

ISO-10646, a super-set of Unicode, uses UTF-8 (there called "UCS", see http://www.unicode.org/faq/utf_bom.html#1 ) in a relaxed variant that supports a 31-bit space encoded into up to six bytes instead of Unicode's 21 bits in up to four bytes. To check for ISO-10646 UTF-8, use the following Perl regular expression (again, broken up, see above):

^([\x00-\x7f]|
[\xc0-\xdf][\x80-\xbf]|
[\xe0-\xef][\x80-\xbf]{2}|
[\xf0-\xf7][\x80-\xbf]{3}|
[\xf8-\xfb][\x80-\xbf]{4}|
[\xfc-\xfd][\x80-\xbf]{5})*$

The following function may be used with above expressions for a quick UTF-8 test, e.g. to distinguish ISO-8859-1-data from UTF-8-data if submitted from a <form accept-charset="utf-8,iso-8859-1" method=..>.

function is_utf8($string) {
   return (preg_match('/[insert regular expression here]/', $string) === 1);
}
http://iubito.free.fr 10-Mar-2005 08:57
Here's a function I made to know if one string or textfile is already encoded in UTF8 :

<?php
/**
 * Returns <kbd>true</kbd> if the string or array of string is encoded in UTF8.
 *
 * Example of use. If you want to know if a file is saved in UTF8 format :
 * <code> $array = file('one file.txt');
 * $isUTF8 = isUTF8($array);
 * if (!$isUTF8) --> we need to apply utf8_encode() to be in UTF8
 * else --> we are in UTF8 :)
 * </code>
 * @param mixed A string, or an array from a file() function.
 * @return boolean
 */
function isUTF8($string)
{
    if (is_array($string))
    {
        $enc = implode('', $string);
        return @!((ord($enc[0]) != 239) && (ord($enc[1]) != 187) && (ord($enc[2]) != 191));
    }
    else
    {
        return (utf8_encode(utf8_decode($string)) == $string);
    }
}
?>
Denis G. 24-Feb-2005 02:32
Sniplet to convert ASCII coded text to UTF-8:

$text= preg_replace ('/([\x80-\xff])/se', "pack (\"C*\", (ord ($1) >> 6) | 0xc0, (ord ($1) & 0x3f) | 0x80)", $text);
anonymous at anonymous dot com 24-Jan-2005 11:49
A few bugs in your example code:

 function code2utf($num){
  if($num<128)return chr($num);
  if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128);
  if($num<65536)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
  if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
  return '';
 }
schofei at yahoo dot de 11-Jan-2005 12:23
regarding the above code2utf function...

> romans at void dot lv
> 02-Oct-2002 09:59
> Here is optimized function which converts
> binary UTF symbol code into unicoded string....

Thanks for providing your nice conversion code, however due to some missing parenthesis 4-byte utf-8 chars are not converted properly.

Here is a corrected version of the code2utf function:

 function code2utf($num){
  if($num<128)return chr($num);
  if($num<1024)return chr(($num>>6)+192).chr(($num&63)+128);
  if($num<32768)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
  if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
  return '';
 }

regards
Scho Fei
hrpeters (at) gmx (dot) net 14-Dec-2004 07:46
// Validate Unicode UTF-8 Version 4
// This function takes as reference the table 3.6 found at http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf
// It also flags overlong bytes as error

function is_validUTF8($str)
{
    // values of -1 represent disalloweded values for the first bytes in current UTF-8
    static $trailing_bytes = array (
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
        -1,-1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
        2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
    );

    $ups = unpack('C*', $str);
    if (!($aCnt = count($ups))) return true; // Empty string *is* valid UTF-8
    for ($i = 1; $i <= $aCnt;)
    {
        if (!($tbytes = $trailing_bytes[($b1 = $ups[$i++])])) continue;
        if ($tbytes == -1) return false;

        $first = true;
        while ($tbytes > 0 && $i <= $aCnt)
        {
            $cbyte = $ups[$i++];
            if (($cbyte & 0xC0) != 0x80) return false;

            if ($first)
            {
                switch ($b1)
                {
                    case 0xE0:
                        if ($cbyte < 0xA0) return false;
                        break;
                    case 0xED:
                        if ($cbyte > 0x9F) return false;
                        break;
                    case 0xF0:
                        if ($cbyte < 0x90) return false;
                        break;
                    case 0xF4:
                        if ($cbyte > 0x8F) return false;
                        break;
                    default:
                        break;
                }
                $first = false;
            }
            $tbytes--;
        }
        if ($tbytes) return false; // incomplete sequence at EOS
    }
    return true;
}
Mark AT modernbill DOT com 09-Nov-2004 08:56
If you haven't guessed already: If the UTF-8 character has no representation in the ISO-8859-1 codepage, a ? will be returned. You might want to wrap a function around this to make sure you aren't saving a bunch of ???? into your database.
Aidan Kehoe <php-manual at parhasard dot net> 30-Aug-2004 04:05
Here's some code that addresses the issue that Steven describes in the previous comment;

<?php

/* This structure encodes the difference between ISO-8859-1 and Windows-1252,
   as a map from the UTF-8 encoding of some ISO-8859-1 control characters to
   the UTF-8 encoding of the non-control characters that Windows-1252 places
   at the equivalent code points. */

$cp1252_map = array(
    "\xc2\x80" => "\xe2\x82\xac", /* EURO SIGN */
    "\xc2\x82" => "\xe2\x80\x9a", /* SINGLE LOW-9 QUOTATION MARK */
    "\xc2\x83" => "\xc6\x92",     /* LATIN SMALL LETTER F WITH HOOK */
    "\xc2\x84" => "\xe2\x80\x9e", /* DOUBLE LOW-9 QUOTATION MARK */
    "\xc2\x85" => "\xe2\x80\xa6", /* HORIZONTAL ELLIPSIS */
    "\xc2\x86" => "\xe2\x80\xa0", /* DAGGER */
    "\xc2\x87" => "\xe2\x80\xa1", /* DOUBLE DAGGER */
    "\xc2\x88" => "\xcb\x86",     /* MODIFIER LETTER CIRCUMFLEX ACCENT */
    "\xc2\x89" => "\xe2\x80\xb0", /* PER MILLE SIGN */
    "\xc2\x8a" => "\xc5\xa0",     /* LATIN CAPITAL LETTER S WITH CARON */
    "\xc2\x8b" => "\xe2\x80\xb9", /* SINGLE LEFT-POINTING ANGLE QUOTATION */
    "\xc2\x8c" => "\xc5\x92",     /* LATIN CAPITAL LIGATURE OE */
    "\xc2\x8e" => "\xc5\xbd",     /* LATIN CAPITAL LETTER Z WITH CARON */
    "\xc2\x91" => "\xe2\x80\x98", /* LEFT SINGLE QUOTATION MARK */
    "\xc2\x92" => "\xe2\x80\x99", /* RIGHT SINGLE QUOTATION MARK */
    "\xc2\x93" => "\xe2\x80\x9c", /* LEFT DOUBLE QUOTATION MARK */
    "\xc2\x94" => "\xe2\x80\x9d", /* RIGHT DOUBLE QUOTATION MARK */
    "\xc2\x95" => "\xe2\x80\xa2", /* BULLET */
    "\xc2\x96" => "\xe2\x80\x93", /* EN DASH */
    "\xc2\x97" => "\xe2\x80\x94", /* EM DASH */

    "\xc2\x98" => "\xcb\x9c",     /* SMALL TILDE */
    "\xc2\x99" => "\xe2\x84\xa2", /* TRADE MARK SIGN */
    "\xc2\x9a" => "\xc5\xa1",     /* LATIN SMALL LETTER S WITH CARON */
    "\xc2\x9b" => "\xe2\x80\xba", /* SINGLE RIGHT-POINTING ANGLE QUOTATION*/
    "\xc2\x9c" => "\xc5\x93",     /* LATIN SMALL LIGATURE OE */
    "\xc2\x9e" => "\xc5\xbe",     /* LATIN SMALL LETTER Z WITH CARON */
    "\xc2\x9f" => "\xc5\xb8"      /* LATIN CAPITAL LETTER Y WITH DIAERESIS*/
);

function cp1252_to_utf8($str) {
        global $cp1252_map;
        return  strtr(utf8_encode($str), $cp1252_map);
}

?>
steven -at- acko -dot- net 17-Aug-2004 11:45
Note that you should only use utf8_encode() on ISO-8859-1 data, and not on data using the Windows-1252 codepage. Microsoft's Windows-1252 codepage contains ISO-8859-1, but it includes several characters in the range 0x80-0x9F whose codepoints in Unicode do not match the byte's value (in Unicode, codepoints U+80 - U+9F are unassigned).

utf8_encode() simply assumes the bytes integer value is the codepoint number in Unicode.

E.g. in 1252, byte 0x80 is the euro sign, which is U+20AC. The same goes for curly quotes, em dashes, etc.

utf8_encode() will convert 0x80 into U+0080 (an unassigned codepoint) rather than U+20AC.

To convert 1252 to UTF-8, use iconv, recode or mbstring.
Net Raven 24-Jun-2004 09:58
I often need to convert multi language text sent to me for use in websites and other apps into UTF8 encoded so I can insert it into source code and databases.

I knocked up a small web page with its charset set to UTF8 then set it up so I can paste from the original doc (eg word or excel) and have the page return the UTF8 encoded version.

Of course the browser will convert the unicode to UTF8 for you as part of the submit (I use IE5 or better for this) then all you have to do in the PHP is encode the UTF8 so the browser will show it in its raw form.

Its a bit bulky but I just convert ALL character to html numbered entities (brute force and ignorance does it again.)

I've used this to encode everything from Hebrew to Japanese without problems

<?
header("Content-Type: text/plain; CHARSET=ks_c_5601-1987");
$code = (get_magic_quotes_gpc())?stripslashes($GLOBALS[code]):$GLOBALS[code];
?>
<html>
<head>
    <title>UTF8 ENCODER PAGE</title>
    <meta http-equiv="content-type" content="text/html; CHARSET=ks_c_5601-1987">
</head>
<body>
<form method=post action="?seed=<?=time()?>">
    Original Unicode<br />
    <textarea name="code" cols="80" rows="10"><?=$code?></textarea><br />
    Encoded UTF8<br />
    <textarea name="encd" cols="80" rows="10"><?
        for ($i = 0; $i < strlen($code); $i++) {
            echo '&#'.ord(substr($code,$i,1));
        }
    ?></textarea><br />
    <input type="submit" value="encode">
</form>
</body>
</html>
lorro at lorro dot wigner dot bme dot hu 06-Apr-2004 04:12
Good news is that utf8_encode (like UTF-8) passes '<', '>', '/', '\'', '"', etc., so you are free to utf8_encode complete blocks of html text that includes tags.
Bad news is that UTF-8 is stupid enough so that utf8_encode(utf8_encode($str)) != utf8_encode($str) in most of the cases. What you can do is write utf8_ensure like:

function utf8_ensure($str) {
    return seems_utf8($str)? $str: utf8_encode($str);
}

Comes handy when your view library tries to encode the same text multiple times.
bmorel at ssi dot fr 16-Feb-2004 09:28
Here is a simple function that can help, if you want to know if a string could be UTF-8 or not :

<?php
function seems_utf8($Str) {
 for ($i=0; $i<strlen($Str); $i++) {
  if (ord($Str[$i]) < 0x80) $n=0; # 0bbbbbbb
  elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
  elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
  elseif ((ord($Str[$i]) & 0xF0) == 0xF0) $n=3; # 1111bbbb
  else return false; # Does not match any model
  for ($j=0; $j<$n; $j++) { # n octets that match 10bbbbbb follow ?
   if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80)) return false;
  }
 }
 return true;
}
?>
Karen 01-Oct-2003 09:33
Re the previous post about converting GB2312 code to Unicode code which displayed the following function:

<?
// Program by sadly (www.phpx.com)

function gb2unicode($gb)
{
   if(!trim($gb))
    return $gb;
   $filename="gb2312.txt";
   $tmp=file($filename);
   $codetable=array();
   while(list($key,$value)=each($tmp))
    $codetable[hexdec(substr($value,0,6))]=substr($value,9,4);
   $utf="";
   while($gb)
    {
      if (ord(substr($gb,0,1))>127)
     {
        $this=substr($gb,0,2);
        $gb=substr($gb,2,strlen($gb));
        $utf.="&#x".$codetable[hexdec(bin2hex($this))-0x8080].";";
      }
     else
     {
      $gb=substr($gb,1,strlen($gb));
      $utf.=substr($gb,0,1);
     }
     }
  return $utf;
}
?>

I found that a small change was needed in the code to properly handle latin characters embedded in the middle of gb2312 text, as when the text includes a URL or email address. Just reverse the two lines in the part of the statement above that handles ord vals !>127.

Change:

$gb=substr($gb,1,strlen($gb));
$utf.=substr($gb,0,1);

to:

$utf.=substr($gb,0,1);
$gb=substr($gb,1,strlen($gb));

In the original function, the first latin chacter was dropped and it was not converting the first non-latin character after the latin text (everything was shifted one character too far to the right). Reversing those two lines makes it work correctly in every example I have tried.

Also, the source of the gb2312.txt file needed for this to work has changed. You can find it a couple places:

http://tcl.apache.org/sources/tcl/tools/encoding/gb2312.txt
ftp://ftp.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/GB/GB2312.TXT
artem at w510 dot tm dot odessa dot ua 03-Jun-2003 04:10
Loading variables in flash

you can lost a lot of hours if your charset is not iso-88951 and you cant' see your characters in flash

you must use iconv instead with your codepage
(for example windows-1251 for ukrainian, russian)

$fw = fopen("flash_input.txt", "w");
if( $fw )
{
    $utf = iconv("windows-1251","UTF-8",$variable_value);
    $out = 'variable_name='.$utf;
    fputs($fw, $out);
    fclose($fw);
}

and no urlecode is needed if you save data in file!
mualem_i at hotmail dot com 22-May-2003 03:12
Hebrew!! What a language. I had some trouble placing the Hebrew in a javascript based drop down menu, the text appeared as UTF8 so I made this function to overcome the problem (Not talking about efficiency)

function rtf_heb($string)
    {
    $array = split (" ",$string) ;
    foreach ($array as $VAL)
        {
        $VAL = str_replace("&#1488","?",$VAL);
        $VAL = str_replace("&#1489","?",$VAL);
        $VAL = str_replace("&#1490","?",$VAL);
        $VAL = str_replace("&#1491","?",$VAL);
        $VAL = str_replace("&#1492","?",$VAL);
        $VAL = str_replace("&#1493","?",$VAL);
        $VAL = str_replace("&#1494","?",$VAL);
        $VAL = str_replace("&#1495","?",$VAL);
        $VAL = str_replace("&#1496","?",$VAL);
        $VAL = str_replace("&#1497","?",$VAL);
        $VAL = str_replace("&#1499","?",$VAL);
        $VAL = str_replace("&#1500","?",$VAL);
        $VAL = str_replace("&#1502","?",$VAL);
        $VAL = str_replace("&#1504","?",$VAL);
        $VAL = str_replace("&#1505","?",$VAL);
        $VAL = str_replace("&#1506","?",$VAL);
        $VAL = str_replace("&#1508","?",$VAL);
        $VAL = str_replace("&#1510","?",$VAL);
        $VAL = str_replace("&#1511","?",$VAL);
        $VAL = str_replace("&#1512","?",$VAL);
        $VAL = str_replace("&#1513","?",$VAL);
        $VAL = str_replace("&#1514","?",$VAL);
        $VAL = str_replace("&#1498","?",$VAL);
        $VAL = str_replace("&#1507","?",$VAL);
        $VAL = str_replace("&#1503","?",$VAL);
        $VAL = str_replace("&#1501","?",$VAL);
        $VAL = str_replace("&#1509","?",$VAL);
        $VAL = str_replace(";","",$VAL);
        $send_VAR .= $VAL." ";

        }
        return $send_VAR;
    }
RoyLaw at 263 dot Net 19-May-2003 01:16
There is a function for converting GB2312 code to Unicode code.It maybe useful for programming on XML/WML in non-English lanaguages.

<?
// Program by sadly (www.phpx.com)

function gb2unicode($gb)
{
   if(!trim($gb))
    return $gb;
   $filename="gb2312.txt";
   $tmp=file($filename);
   $codetable=array();
   while(list($key,$value)=each($tmp))
    $codetable[hexdec(substr($value,0,6))]=substr($value,9,4);
   $utf="";
   while($gb)
    {
      if (ord(substr($gb,0,1))>127)
     {
        $this=substr($gb,0,2);
        $gb=substr($gb,2,strlen($gb));
        $utf.="&#x".$codetable[hexdec(bin2hex($this))-0x8080].";";
      }
     else
     {
      $gb=substr($gb,1,strlen($gb));
      $utf.=substr($gb,0,1);
     }
     }
  return $utf;
}
?>

This function requires a code list of gb2312,you can download it at
ftp://ftp.unicode.org/Public/MAPPINGS/EASTASIA/GB/GB2312.TXT
sunish_mv at rediffmail dot com 04-Apr-2003 07:50
/*Here I have a class that will convert ISCII (Indian Standard Code for Information Interchange) devnagiri (Hindi) string to unicode string. /*

<?php

  class iscii2utf8 {

      var $map;

      function iscii2utf8() {

          $this->map = array (
                   "a0" =>  '63'  ,
                 "a1" => '2305' ,
                 "a2" => '2306' ,
                 "a3" => '2307' ,
                 "a4" => '2309' ,
                 "a5" => '2310' ,
                 "a6" => '2311' ,
                 "a7" => '2312' ,
                 "a8" => '2313' ,
                 "a9" => '2314' ,
                 "aa" => '2315' ,
                 "ab" => '2318' ,
                 "ac" => '2319' ,
                 "ad" => '2320' ,
                 "ae" => '2317' ,
                 "af" => '2322' ,
                 "b0" => '2323' ,
                 "b1" => '2324' ,
                 "b2" => '2321' ,
                 "b3" => '2325' ,
                 "b4" => '2326' ,
                 "b5" => '2327' ,
                 "b6" => '2328' ,
                 "b7" => '2329' ,
                 "b8" => '2330' ,
                 "b9" => '2331' ,
                 "ba" => '2332' ,
                 "bb" => '2333' ,
                 "bc" => '2334' ,
                 "bd" => '2335' ,
                 "be" => '2336' ,
                 "bf" => '2337' ,
                 "c0" => '2338' ,
                 "c1" => '2339' ,
                 "c2" => '2340' ,
                 "c3" => '2341' ,
                 "c4" => '2342' ,
                 "c5" => '2343' ,
                 "c6" => '2344' ,
                 "c7" => '2345' ,
                 "c8" => '2346' ,
                 "c9" => '2347' ,
                 "ca" => '2348' ,
                 "cb" => '2349' ,
                 "cc" => '2350' ,
                 "cd" => '2351' ,
                 "ce" => '2399' ,
                 "cf" => '2352' ,
                 "d0" => '2353' ,
                 "d1" => '2354' ,
                 "d2" => '2355' ,
                 "d3" => '2356' ,
                 "d4" => '2357' ,
                 "d5" => '2358' ,
                 "d6" => '2359' ,
                 "d7" => '2360' ,
                 "d8" => '2361' ,
                 "d9" =>  '63'  ,
                 "da" => '2366' ,
                 "db" => '2367' ,
                 "dc" => '2368' ,
                 "dd" => '2369' ,
                 "de" => '2370' ,
                 "df" => '2371' ,
                 "e0" => '2374' ,
                 "e1" => '2375' ,
                 "e2" => '2376' ,
                 "e3" => '2373' ,
                 "e4" => '2378' ,
                 "e5" => '2379' ,
                 "e6" => '2380' ,
                 "e7" => '2377' ,
                 "e8" => '2381' ,
                 "e9" =>  '63'  ,
                 "ea" => '2404' ,
                 "eb" =>  '63'  ,
                 "ec" =>  '63'  ,
                 "ed" =>  '63'  ,
                 "ee" =>  '63'  ,
                 "ef" =>  '63'  ,
                 "f0" =>  '63'  ,
                 "f1" => '2406' ,
                 "f2" => '2407' ,
                 "f3" => '2408' ,
                 "f4" => '2409' ,
                 "f5" => '2410' ,
                 "f6" => '2411' ,
                 "f7" => '2412' ,
                 "f8" => '2413' ,
                 "f9" => '2414' ,
                 "fa" => '2415' ,
                 "fb" =>  '63'  ,
                 "fc" =>  '63'  ,
                 "fd" =>  '63'  ,
                 "fe" =>  '63'  ,
                 "ff" =>  '63'  ,);
        }

        function code2utf($num){

             //Returns the utf string corresponding to the unicode value
             //courtesy - romans@void.lv

             if($num<128)return chr($num);
             if($num<1024)return chr(($num>>6)+192).chr(($num&63)+128);
             if($num<32768)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
             if($num<2097152)return chr($num>>18+240).chr((($num>>12)&63)+128).chr(($num>>6)&63+128). chr($num&63+128);
             return '';

        }

        function convertstring($iscii) {
            //Returs utf8 string equibalent of given iscii string

            $str = "";
            for($i = 0; $i<strlen($iscii); $i++) {

                $c = dechex(ord(substr($iscii,$i,1)));
                if (isset($this->map[$c] )) {
                    $s = $this->code2utf($this->map[$c]);
                    $str .= ($s == "?")?"":$s;
                    }
                else {
                   $str .= substr($iscii,$i,1);
                   }

            }

            return $str;
        }

    }

?>
rbotzer at yahoo dot com 01-Apr-2003 10:25
BTW, the 21-bit range is pretty old news.  Unicode 3.x uses a 31bit encoding scheme that allows for 2 billion characters.

I'll post an enhanced encoder soon.  In the meanwhile here's the current encoding scheme: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8

Ronen
webmaster at swisswebgroup dot com 31-Mar-2003 02:54
if you try to pass data to a flash movie with the
actionscripts functions loadVars or sendAndLoad give this a try,
if you have problems with special chars like &auml; &ouml; ....

echo "&data1=".urlencode(utf8_encode("???"))
    ."&data2=".urlencode(utf8_encode("???"));

greets

js
romans at void dot lv 03-Oct-2002 03:59
Here is optimized function which converts binary UTF symbol code into unicoded string.

 function code2utf($num){
  if($num<128)return chr($num);
  if($num<1024)return chr(($num>>6)+192).chr(($num&63)+128);
  if($num<32768)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
  if($num<2097152)return chr($num>>18+240).chr((($num>>12)&63)+128).chr(($num>>6)&63+128). chr($num&63+128);
  return '';
 }
dimitrisATccfDOTauthDOTgr 28-Aug-2002 08:04
To make utf8_encode and utf8_decode support other than iso-8859-1 encodings, you can easily define your encoding in the PHP source.
In the file PHP_SOURCE/ext/xml/xml.c add the following code, for e.g. greek iso-8859-7:

DEFINE TWO NEW FUNCTIONS UP TOP:
inline static unsigned short xml_encode_iso_8859_7(unsigned char);
inline static char xml_decode_iso_8859_7(unsigned short);

AND THEN IMPLEMENT THEM BELOW:
/* {{{ xml_encode_iso_8859_7() - Dimitris Daskopoulos 28/8/02 */
/* map iso-8859-7 chars to Unicode chars */
inline static unsigned short xml_encode_iso_8859_7(unsigned char c)
{
        if (c < 0x80) { /* low-ASCII, leave as is */
                return (unsigned short)c;
        } else { /* Greek character in high-ASCII */
                /* map to UCS greek range (U+0310..03ff) */
                /* assume that c < 0xff */
                return (unsigned short)(c + 720);
        }
}
/* }}} */

/* {{{ xml_decode_iso_8859_7() - Dimitris Daskopoulos 28/8/02 */
/* map Unicode chars to iso-8859-7 chars */
inline static char xml_decode_iso_8859_7(unsigned short c)
{
        if (c < 0x100) { /* char in latin chart, leave as is */
                return (char)c;
        } else if (c > 0x030f && c < 0x0400) { /* char in greek chart */
                /* map back to ISO-8859-7 greek (high-ASCII) */
                return (char)(c - 720);
        } else { /* char not in latin or greek Unicode charts */
                /* return question mark character */
                return (char)('?');
        }
}
/* }}} */

These two work fine for greek iso-8859-7, but studying http://www.unicode.org/charts you
can implement mappings between unicode and other iso-8859-x quite easily.

In both functions (utf8_encode and utf8_decode), change the requested encoding to the one you prefer, e.g.

encoded = xml_utf8_encode(Z_STRVAL_PP(arg), Z_STRLEN_PP(arg), &len, "ISO-8859-7");

decoded = xml_utf8_decode(Z_STRVAL_PP(arg), Z_STRLEN_PP(arg), &len, "ISO-8859-7");

Make sure you add the new encoding
in the structure, by entering a new
row with the official name (ISO-8859-7), and the names of the
two functions you have just defined:
xml_encoding xml_encodings[] = {
        { "ISO-8859-1", xml_decode_iso_8859_1, xml_encode_iso_8859_1 },
        { "US-ASCII",   xml_decode_us_ascii,   xml_encode_us_ascii   },
        { "UTF-8",      NULL,                  NULL                  },
        { "ISO-8859-7", xml_decode_iso_8859_7, xml_encode_iso_8859_7 },
        { NULL,         NULL,                  NULL                  }
};

Finally, the following is probably not necessary, but I changed the default encoding (found in 2 spots in this file) to whatever encoding you prefer in your
pages, e.g.:
XML(default_encoding) = "ISO-8859-7";

This solution is a little messy,
since the utf8_encode function does not accept an argument for choosing the encoding method to use but hardwires the encoding method in the source code. Maybe PHP developers will provide this option in future releases. Until then, this is a quick and dirty solution that will work for
localized PHP pages.

Dimitris Daskopoulos
27-Aug-2002 08:30
For XML generation, if you want non-ASCII ISO-8859-1 characters within text and attributes, you don't absolutely need UTF-8 encoding:

The optional XML declaration can change the default encoding for characters from UTF-8 to ISO-8859-1:

<?xml version="1.0" encoding="iso-8859-1" ?>

This can save a lot of PHP code if you just want to generate ISO-8859-1 text and attribute values...

XML specification requires that all parsers support both the UTF-8 encoding (by default), and the ISO-8859-1 character set. Other character sets may be supported also by specifying them in the encoding attribute of the leading XML declaration (but the target parser must support this character set to allow automatic conversion of the source text into Unicode character entities.
dutoit at NOSPAM dot abonder dot com 01-Aug-2002 08:50
To write an XML element $title containing "exotic" (eg. non ASCII ? & ? ?...) 2 solutions I found :
Fastest :
$xml .= "<title><![CDATA[" . $title ."]]></title>\n"

or cleanest :
$xml .= "<title>".utf8_encode(htmlspecialchars($title))."</title>\n"

After that, your xml can be parsed without errors.
lars(at)ioflux(dot)net 13-Mar-2002 05:29
This will also do the job for those who're interested:

<?

function utf8toiso8859($string)
{
  $returns = "";
  $UTF8len = array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6);
  $pos = 0;
  $antal = strlen($string);

  do
  {
    $c = ord($string[$pos]);
    $len = $UTF8len[($c >> 2) & 0x3F];
    switch ($len)
    {
      case 6:
        $u = $c & 0x01;
        break;
      case 5:
        $u = $c & 0x03;
        break;
      case 4:
        $u = $c & 0x07;
        break;
      case 3:
        $u = $c & 0x0F;
        break;
      case 2:
        $u = $c & 0x1F;
        break;
      case 1:
        $u = $c & 0x7F;
        break;
      case 0:  /* unexpected start of a new character */
        $u = $c & 0x3F;
        $len = 5;
        break;
    }
    while (--$len && (++$pos < $antal && $c =
ord($string[$pos])))
    {
      if (($c & 0xC0) == 0x80)
        $u = ($u << 6) | ($c & 0x3F);
      else
      { /* unexpected start of a new character */
        $pos--;
        break;
      }
    }
    if ($u <= 0xFF)
      $returns .= chr($u);
    else
      $returns .= '?';
  } while (++$pos < $antal);
  return $returns;
}

?>
ronen at greyzone dot com 07-Mar-2002 09:01
The following function will utf-8 encode unicode entities &#nnn(nn); with n={0..9}

/**
* takes a string of unicode entities and converts it to a utf-8 encoded string
* each unicode entitiy has the form &#nnn(nn); n={0..9} and can be displayed by utf-8 supporting
* browsers.  Ascii will not be modified.
* @param $source string of unicode entities [STRING]
* @return a utf-8 encoded string [STRING]
* @access public
*/
function utf8Encode ($source) {
    $utf8Str = '';
    $entityArray = explode ("&#", $source);
    $size = count ($entityArray);
    for ($i = 0; $i < $size; $i++) {
        $subStr = $entityArray[$i];
        $nonEntity = strstr ($subStr, ';');
        if ($nonEntity !== false) {
            $unicode = intval (substr ($subStr, 0, (strpos ($subStr, ';') + 1)));
            // determine how many chars are needed to reprsent this unicode char
            if ($unicode < 128) {
                $utf8Substring = chr ($unicode);
            }
            else if ($unicode >= 128 && $unicode < 2048) {
                $binVal = str_pad (decbin ($unicode), 11, "0", STR_PAD_LEFT);
                $binPart1 = substr ($binVal, 0, 5);
                $binPart2 = substr ($binVal, 5);

                $char1 = chr (192 + bindec ($binPart1));
                $char2 = chr (128 + bindec ($binPart2));
                $utf8Substring = $char1 . $char2;
            }
            else if ($unicode >= 2048 && $unicode < 65536) {
                $binVal = str_pad (decbin ($unicode), 16, "0", STR_PAD_LEFT);
                $binPart1 = substr ($binVal, 0, 4);
                $binPart2 = substr ($binVal, 4, 6);
                $binPart3 = substr ($binVal, 10);

                $char1 = chr (224 + bindec ($binPart1));
                $char2 = chr (128 + bindec ($binPart2));
                $char3 = chr (128 + bindec ($binPart3));
                $utf8Substring = $char1 . $char2 . $char3;
            }
            else {
                $binVal = str_pad (decbin ($unicode), 21, "0", STR_PAD_LEFT);
                $binPart1 = substr ($binVal, 0, 3);
                $binPart2 = substr ($binVal, 3, 6);
                $binPart3 = substr ($binVal, 9, 6);
                $binPart4 = substr ($binVal, 15);

                $char1 = chr (240 + bindec ($binPart1));
                $char2 = chr (128 + bindec ($binPart2));
                $char3 = chr (128 + bindec ($binPart3));
                $char4 = chr (128 + bindec ($binPart4));
                $utf8Substring = $char1 . $char2 . $char3 . $char4;
            }

            if (strlen ($nonEntity) > 1)
                $nonEntity = substr ($nonEntity, 1); // chop the first char (';')
            else
                $nonEntity = '';

            $utf8Str .= $utf8Substring . $nonEntity;
        }
        else {
            $utf8Str .= $subStr;
        }
    }

    return $utf8Str;
}

Ronen.


Get XML parser error string

  • ≪ utf8_encode
  • xml_get_current_byte_index ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Get XML parser error string

xml_error_string

(PHP 4, PHP 5, PHP 7)

xml_error_string — Get XML parser error string

설명

string xml_error_string ( int $code )

Gets the XML parser error string associated with the given code.

인수

code

An error code from xml_get_error_code().

반환값

Returns a string with a textual description of the error code, or FALSE if no description was found.

참고

  • xml_get_error_code() - Get XML parser error code

User Contributed Notes

greg at getnetez dot com 24-Aug-2012 04:27
Two notes in one.

Here is the combination of xml_get_error_code() and xml_error_string() to get the error printout as an echo response

Also, I tested before and after xml_parser_free() -- as expected, the error is indeed cleared with the free() so check your error before calling xml_parser_free()

Pushing a deadline or I would add in sample faulted xml for full p.o.c.

somebody else want to step up for that??

G

<?php
$p = xml_parser_create();
xml_parse_into_struct($p, $data, $vals, $index);
//This call prints error
echo 'ERROR if any '.xml_error_string(xml_get_error_code($p));
xml_parser_free($p);
echo "Index array\\n";
print_r($index);
echo "\\nVals array\\n";
print_r($vals);
//No Error left here to be printed
echo 'ERROR if any '.xml_error_string(xml_get_error_code($p));
exit;
?>


Get current byte index for an XML parser

  • ≪ xml_error_string
  • xml_get_current_column_number ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Get current byte index for an XML parser

xml_get_current_byte_index

(PHP 4, PHP 5, PHP 7)

xml_get_current_byte_index — Get current byte index for an XML parser

설명

int xml_get_current_byte_index ( resource $parser )

Gets the current byte index of the given XML parser.

인수

parser

A reference to the XML parser to get byte index from.

반환값

This function returns FALSE if parser does not refer to a valid parser, or else it returns which byte index the parser is currently at in its data buffer (starting at 0).

주의

Warning

This function returns byte index according to UTF-8 encoded text disregarding if input is in another encoding.

참고

  • xml_get_current_column_number() - Get current column number for an XML parser
  • xml_get_current_line_number() - Get current line number for an XML parser

User Contributed Notes

turan dot yuksel at tcmb dot gov dot tr 20-Sep-2005 12:38
The outcome of this function is heavily dependent on the parser implementation used. For example, at the point where the start_element_ callback is called, libxml2 parser consumes the entire element name and attributes, however expat does not.


Get current column number for an XML parser

  • ≪ xml_get_current_byte_index
  • xml_get_current_line_number ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Get current column number for an XML parser

xml_get_current_column_number

(PHP 4, PHP 5, PHP 7)

xml_get_current_column_number — Get current column number for an XML parser

설명

int xml_get_current_column_number ( resource $parser )

Gets the current column number of the given XML parser.

인수

parser

A reference to the XML parser to get column number from.

반환값

This function returns FALSE if parser does not refer to a valid parser, or else it returns which column on the current line (as given by xml_get_current_line_number()) the parser is currently at.

참고

  • xml_get_current_byte_index() - Get current byte index for an XML parser
  • xml_get_current_line_number() - Get current line number for an XML parser

User Contributed Notes

shaun at 5810 dot com 05-Jul-2003 06:04
Column numbers apparently start at 0. So you may want to add 1.
javajohn at gulfbridge.net 23-Mar-2003 07:03
"Column number" refers to the current character position within the current line of the XML document.


Get current line number for an XML parser

  • ≪ xml_get_current_column_number
  • xml_get_error_code ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Get current line number for an XML parser

xml_get_current_line_number

(PHP 4, PHP 5, PHP 7)

xml_get_current_line_number — Get current line number for an XML parser

설명

int xml_get_current_line_number ( resource $parser )

Gets the current line number for the given XML parser.

인수

parser

A reference to the XML parser to get line number from.

반환값

This function returns FALSE if parser does not refer to a valid parser, or else it returns which line the parser is currently at in its data buffer.

참고

  • xml_get_current_column_number() - Get current column number for an XML parser
  • xml_get_current_byte_index() - Get current byte index for an XML parser

User Contributed Notes

There are no user contributed notes for this page.


Get XML parser error code

  • ≪ xml_get_current_line_number
  • xml_parse_into_struct ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Get XML parser error code

xml_get_error_code

(PHP 4, PHP 5, PHP 7)

xml_get_error_code — Get XML parser error code

설명

int xml_get_error_code ( resource $parser )

Gets the XML parser error code.

인수

parser

A reference to the XML parser to get error code from.

반환값

This function returns FALSE if parser does not refer to a valid parser, or else it returns one of the error codes listed in the error codes section.

참고

  • xml_error_string() - Get XML parser error string

User Contributed Notes

There are no user contributed notes for this page.


Parse XML data into an array structure

  • ≪ xml_get_error_code
  • xml_parse ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Parse XML data into an array structure

xml_parse_into_struct

(PHP 4, PHP 5, PHP 7)

xml_parse_into_struct — Parse XML data into an array structure

설명

int xml_parse_into_struct ( resource $parser , string $data , array &$values [, array &$index ] )

This function parses an XML string into 2 parallel array structures, one (index) containing pointers to the location of the appropriate values in the values array. These last two parameters must be passed by reference.

인수

parser

A reference to the XML parser.

data

A string containing the XML data.

values

An array containing the values of the XML data

index

An array containing pointers to the location of the appropriate values in the $values.

반환값

xml_parse_into_struct() returns 0 for failure and 1 for success. This is not the same as FALSE and TRUE, be careful with operators such as ===.

예제

Below is an example that illustrates the internal structure of the arrays being generated by the function. We use a simple note tag embedded inside a para tag, and then we parse this and print out the structures generated:

Example #1 xml_parse_into_struct() example

<?php
$simple = "<para><note>simple note</note></para>";
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
?>

When we run that code, the output will be:

Index array
Array
(
    [PARA] => Array
        (
            [0] => 0
            [1] => 2
        )

    [NOTE] => Array
        (
            [0] => 1
        )

)

Vals array
Array
(
    [0] => Array
        (
            [tag] => PARA
            [type] => open
            [level] => 1
        )

    [1] => Array
        (
            [tag] => NOTE
            [type] => complete
            [level] => 2
            [value] => simple note
        )

    [2] => Array
        (
            [tag] => PARA
            [type] => close
            [level] => 1
        )

)

Event-driven parsing (based on the expat library) can get complicated when you have an XML document that is complex. This function does not produce a DOM style object, but it generates structures amenable of being transversed in a tree fashion. Thus, we can create objects representing the data in the XML file easily. Let's consider the following XML file representing a small database of aminoacids information:

Example #2 moldb.xml - small database of molecular information

<?xml version="1.0"?>
<moldb>

  <molecule>
      <name>Alanine</name>
      <symbol>ala</symbol>
      <code>A</code>
      <type>hydrophobic</type>
  </molecule>

  <molecule>
      <name>Lysine</name>
      <symbol>lys</symbol>
      <code>K</code>
      <type>charged</type>
  </molecule>

</moldb>
And some code to parse the document and generate the appropriate objects:

Example #3 parsemoldb.php - parses moldb.xml into an array of molecular objects

<?php

class AminoAcid {
    var $name;  // aa name
    var $symbol;    // three letter symbol
    var $code;  // one letter code
    var $type;  // hydrophobic, charged or neutral

    function AminoAcid ($aa)
    {
        foreach ($aa as $k=>$v)
            $this->$k = $aa[$k];
    }
}

function readDatabase($filename)
{
    // read the XML database of aminoacids
    $data = implode("", file($filename));
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $data, $values, $tags);
    xml_parser_free($parser);

    // loop through the structures
    foreach ($tags as $key=>$val) {
        if ($key == "molecule") {
            $molranges = $val;
            // each contiguous pair of array entries are the
            // lower and upper range for each molecule definition
            for ($i=0; $i < count($molranges); $i+=2) {
                $offset = $molranges[$i] + 1;
                $len = $molranges[$i + 1] - $offset;
                $tdb[] = parseMol(array_slice($values, $offset, $len));
            }
        } else {
            continue;
        }
    }
    return $tdb;
}

function parseMol($mvalues)
{
    for ($i=0; $i < count($mvalues); $i++) {
        $mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
    }
    return new AminoAcid($mol);
}

$db = readDatabase("moldb.xml");
echo "** Database of AminoAcid objects:\n";
print_r($db);

?>
After executing parsemoldb.php, the variable $db contains an array of AminoAcid objects, and the output of the script confirms that:
** Database of AminoAcid objects:
Array
(
    [0] => aminoacid Object
        (
            [name] => Alanine
            [symbol] => ala
            [code] => A
            [type] => hydrophobic
        )

    [1] => aminoacid Object
        (
            [name] => Lysine
            [symbol] => lys
            [code] => K
            [type] => charged
        )

)

User Contributed Notes

gramo dot gnu at gmail dot com 24-Apr-2014 01:51
I really need to make the "xml_parse_into_array" function, and I figured out this "recursive function" solution. (I just test that with ISO-8859-1 charset, but I think it works with ASCII and UTF-8)

/*------------------------------------------------------------------*/
function xml_parse_into_array($xml)
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DESCRIPCION:
Convierte un texto en xml codificado en ISO-8859-1 a un arreglo de php.
--------------------------------------------------------------------*/
{
    $retorno = array();

    $p = xml_parser_create();
    xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
    xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($p, $xml, $elementos, $llaves);
    xml_parser_free($p);

    $Qprocesados = 0;
    $retorno = xml_sub_parse_into_array($elementos, $Qprocesados);

    return array($retorno, $Qprocesados);
}

/*------------------------------------------------------------------*/
function xml_sub_parse_into_array($xml, &$i, $abierto = null)
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DESCRIPCION:
--------------------------------------------------------------------*/
{
    $retorno = array();

    //----------------------------------------
    // Caso trivial: TAG COMPLETO
    //----------------------------------------
    if ($xml[$i]['type'] == 'complete' && isset($xml[$i]['value'])) {
        $retorno[$xml[$i]['tag']] = trim($xml[$i]['value']);
        $i ++;
        return $retorno;
    //----------------------------------------
    // Caso trivial: TAG COMPLETO
    //----------------------------------------
    } else if ($xml[$i]['type'] == 'cdata' && isset($xml[$i]['value'])) {
        $retorno = trim($xml[$i]['value']);
        $i ++;
        return $retorno;
    //----------------------------------------
    // Caso trivial: TAG VACIO
    //----------------------------------------
    } else if ($xml[$i]['type'] == 'complete') {
        // $retorno = "<".$xml[$i]['tag']." />";
        $retorno[$xml[$i]['tag']] = null;
        $i ++;
        return $retorno;
    //----------------------------------------
    // Caso recursivo: TAG CON TAGS ADENTRO
    //----------------------------------------
    } else if ($xml[$i]['type'] == 'open') {
        $abierto = $xml[$i]['tag'];
        $nivel   = $xml[$i]['level'];

        if (!isset($retorno[$abierto]))
            $retorno[$abierto] = array();

        if (isset($xml[$i]['value'])) {
            $retorno[$abierto][] = trim($xml[$i]['value']);
        }

        $i ++;
        $termino = false;
        while (!$termino && $i < count($xml)) {
            if ($xml[$i]['tag'] == $abierto &&
                $xml[$i]['type'] == 'close' &&
                $xml[$i]['level'] == $nivel ) {
                $termino = true;
            } else {
                $retorno[$abierto][] = xml_sub_parse_into_array($xml, $i, $abierto);
                if ($i < count($xml))
                    if ($xml[$i]['type'] == 'close') {
                        $i ++;
                        return $retorno;
                    }
            }
        }
        return $retorno;
    }
}
rejeesh_designdirect at yahoo dot com 21-Dec-2013 06:25
Simple method for format XML to Array

function xml2array($xml){
    $opened = array();
    $xml_parser = xml_parser_create();
    xml_parse_into_struct($xml_parser, $xml, $xmlarray);
    $array = array();
    $arrsize = sizeof($xmlarray);
    for($j=0;$j<$arrsize;$j++){
        $val = $xmlarray[$j];
        switch($val["type"]){
            case "open":
                $opened[$val["tag"]] = $array;
                unset($array);
                break;
            case "complete":
                $array[$val["tag"]][] = $val["value"];
            break;
            case "close":
                $opened[$val["tag"]] = $array;
                $array = $opened;
            break;
        }
    }
    return $array;
}
andy at andynformatics dot de 14-Jun-2012 09:35
To get out of an XML-Structure like this:
<root><element><subelement attribute="value">some text</subelement></element><anotherelement>Hello World</anotherelement></root>
something like this:

Array
(
    [tag] => ROOT
    [0] => Array
        (
            [tag] => ELEMENT
            [0] => Array
                (
                    [tag] => SUBELEMENT
                    [attributes] => Array
                        (
                            [ATTRIBUTE] => value
                        )

                    [value] => some text
                )

        )

    [1] => Array
        (
            [tag] => ANOTHERELEMENT
            [value] => Hello World
        )

)

I have written a short and pretty simple function:

<?php
function xml2array($xml){
    $opened = array();
    $opened[1] = 0;
    $xml_parser = xml_parser_create();
    xml_parse_into_struct($xml_parser, $xml, $xmlarray);
    $array = array_shift($xmlarray);
    unset($array["level"]);
    unset($array["type"]);
    $arrsize = sizeof($xmlarray);
    for($j=0;$j<$arrsize;$j++){
        $val = $xmlarray[$j];
        switch($val["type"]){
            case "open":
                $opened[$val["level"]]=0;
            case "complete":
                $index = "";
                for($i = 1; $i < ($val["level"]); $i++)
                    $index .= "[" . $opened[$i] . "]";
                $path = explode('][', substr($index, 1, -1));
                $value = &$array;
                foreach($path as $segment)
                    $value = &$value[$segment];
                $value = $val;
                unset($value["level"]);
                unset($value["type"]);
                if($val["type"] == "complete")
                    $opened[$val["level"]-1]++;
            break;
            case "close":
                $opened[$val["level"]-1]++;
                unset($opened[$val["level"]]);
            break;
        }
    }
    return $array;
}
?>
sujithfem at gmail dot com 26-Oct-2009 10:46
following function use to get the value for given XML tag

<?php
function getXmlValueByTag($inXmlset,$needle){
        $resource    =    xml_parser_create();//Create an XML parser
        xml_parse_into_struct($resource, $inXmlset, $outArray);// Parse XML data into an array structure
        xml_parser_free($resource);//Free an XML parser

        for($i=0;$i<count($outArray);$i++){
            if($outArray[$i]['tag']==strtoupper($needle)){
                $tagValue    =    $outArray[$i]['value'];
            }
        }
        return $tagValue;
    }

    echo getXmlValueByTag($inXmlset,$needle);
?>
Adam Tylmad 30-Apr-2009 09:59
Thanx to jukea i've upgraded the characterData-function to allow the value 0. Here is the new complete code:

<?php

class XMLParser {
    var $path;
    var $result;

    function cleanString($string) {
        return trim(str_replace("'", "&#39;", $string));
    }

    function XMLParser($encoding, $data) {
        $this->path = "\$this->result";
        $this->index = 0;

        $xml_parser = xml_parser_create($encoding);
        xml_set_object($xml_parser, &$this);
        xml_set_element_handler($xml_parser, 'startElement', 'endElement');
        xml_set_character_data_handler($xml_parser, 'characterData');

        xml_parse($xml_parser, $data, true);
        xml_parser_free($xml_parser);
    }

    function startElement($parser, $tag, $attributeList) {
        $this->path .= "->".$tag;
        eval("\$data = ".$this->path.";");
        if (is_array($data)) {
            $index = sizeof($data);
            $this->path .= "[".$index."]";
        } else if (is_object($data)) {
            eval($this->path." = array(".$this->path.");");
            $this->path .= "[1]";
        }

        foreach($attributeList as $name => $value)
            eval($this->path."->".$name. " = '".XMLParser::cleanString($value)."';");
    }

    function endElement($parser, $tag) {
        $this->path = substr($this->path, 0, strrpos($this->path, "->"));
    }

    function characterData($parser, $data) {
        if (strlen($data = XMLParser::cleanString($data)))
            eval($this->path." = '$data';");
    }
}

?>
Adam Tylmad 30-Apr-2009 09:58
Thanx to jukea i've upgraded the characterData-function to allow the value 0. Here is the new complete code:

<?php

class XMLParser {
    var $path;
    var $result;

    function cleanString($string) {
        return trim(str_replace("'", "&#39;", $string));
    }

    function XMLParser($encoding, $data) {
        $this->path = "\$this->result";
        $this->index = 0;

        $xml_parser = xml_parser_create($encoding);
        xml_set_object($xml_parser, &$this);
        xml_set_element_handler($xml_parser, 'startElement', 'endElement');
        xml_set_character_data_handler($xml_parser, 'characterData');

        xml_parse($xml_parser, $data, true);
        xml_parser_free($xml_parser);
    }

    function startElement($parser, $tag, $attributeList) {
        $this->path .= "->".$tag;
        eval("\$data = ".$this->path.";");
        if (is_array($data)) {
            $index = sizeof($data);
            $this->path .= "[".$index."]";
        } else if (is_object($data)) {
            eval($this->path." = array(".$this->path.");");
            $this->path .= "[1]";
        }

        foreach($attributeList as $name => $value)
            eval($this->path."->".$name. " = '".XMLParser::cleanString($value)."';");
    }

    function endElement($parser, $tag) {
        $this->path = substr($this->path, 0, strrpos($this->path, "->"));
    }

    function characterData($parser, $data) {
        if (strlen($data = XMLParser::cleanString($data)))
            eval($this->path." = '$data';");
    }
}

?>
aerik at thesylvans dot com 29-Nov-2008 12:55
Here's another attempt at dealing with rebuilding the xml from the array.  I'm basically preventing certain tags from being self closing by just checking for them explicitly (sure wish expat didn't interpret <textarea /> and <textarea></textarea> the same way (as "complete" tags).

<?php
function array2xml($xmlary){
  $o='';
  foreach($xmlary as $tag ){
    if($tag['tag'] == 'textarea' && !isset($tag['value'])){
      //fake a value so it won't self close
      $tag['value']='';
    }
    //tab space:
    $t = '';
    for($i=1; $i < $tag['level'];$i++){
      $t.="\t";
    }
    switch($tag['type']){
      case 'complete':
      case 'open':
        $o.=$t.'<'.$tag['tag'];
        if(isset($tag['attributes'])){
          foreach($tag['attributes'] as $attr=>$aval){
            $o.=' '.$attr.'="'.$aval.'"';
          }//foreach
        }//attributes
        if($tag['type'] == 'complete'){
          if(!isset($tag['value'])){
            $o .= ' />'."\n";
          } else {
            $o .= '>'."\n".$t.$tag['value']."\n".$t.'</'.$tag['tag'].'>'."\n";
          }
        }else{
          $o .= '>'."\n";
        }
        break;
      case 'close':
        $o .= $t.'</'.$tag['tag'].'>'."\n";
        break;
      case 'cdata':
        $o .= $t.$tag['value']."\n";
        break;
    }//switch
  }//foreach
}
?>
lpatrick 03-Nov-2008 01:36
@ dale at ucsc dot edu
I modified your code for entries like <caption>Value</caption>
<?php
$o='';
foreach( $vals as $tag ){
    //tab space:
    for($i=1; $i < $tag['level'];$i++)
        $o.="\t";
    if($tag['type']!='close'){
        if($tag['type']!='cdata')
            $o.='<'.$tag['tag'];
        if(isset($tag['attributes'])){
            foreach($tag['attributes'] as $attr=>$aval){
                $o.=' '.$attr.'="'.$aval.'"';
            }
        }
        if($tag['type']!='cdata'){
            $o.=($tag['type']=='complete' && (!isset($tag['value']) || $tag['value']==''))?'/>':'>';
        }
        $o.=(isset($tag['value']))?$tag['value']:'';
        if($tag['type']!='cdata'){
            $o.=($tag['type']=='complete' && (isset($tag['value']) && $tag['value']<>''))?'</'.$tag['tag'].'>':'';
        }
        $o.="\n";
    }else{
        $o.='</'.$tag['tag'].'>'."\n";
    }
}
?>
dale at ucsc dot edu 26-Sep-2008 01:16
here you can [re]assemble xml from a xml_parse_into_struct generated array: ie array2xml

<?php
$xml_parser = xml_parser_create( 'UTF-8' ); // UTF-8 or ISO-8859-1
xml_parser_set_option( $xml_parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option( $xml_parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct( $xml_parser, $my_text_xml, $aryXML );
xml_parser_free($xml_parser);

//now use aryXML array to xml string:
        $o='';
        foreach( $aryXML as $tag ){
            //tab space:
            for($i=1; $i < $tag['level'];$i++)
                $o.="\t";
            if($tag['type']!='close'){
                if($tag['type']!='cdata')
                    $o.='<'.$tag['tag'];
                if(isset($tag['attributes'])){
                    foreach($tag['attributes'] as $attr=>$aval){
                        $o.=' '.$attr.'="'.$aval.'"';
                    }
                }
                if($tag['type']!='cdata'){
                    $o.=($tag['type']=='complete')?'/>'."\n":'>'."\n";
                }
                $o.=(isset($tag['value']))?$tag['value']:'';
            }else{
                $o.='</'.$tag['tag'].'>';
            }
        }
?>
Llorenc 10-Sep-2008 12:20
Sorry, forgot my last post below, I confused CDATA sections (<![CDATA[]]>) with the value type "cdata", which despite its name it's very different! The first allows adding markup (&,<,>) as regular text and the second is to indicate a not complete node between the open and close nodes. What is true is that the CDATA delimiters are lost after xml_parse_into_struct so it's not possible to revert the process.

Please, feel free to remove my comments or leave them if you think they can help anyone.
Llorenc 09-Sep-2008 03:08
Many examples below using this function check if a given tag returned is of type "cdata". It seems to me nowadays CDATA sections are not detected by all implementations, if any. In the servers I could test (FreeBSD 6.2 with PHP 5.2.6 and libxml 2.6.26 or PHP 4.4.8 and expat 2.0.0 - MacOSX 10.5.4 with PHP 5.2.6 and libxml 2.6.16) my CDATA sections were reported as being of type "complete".

This problem was reported earlier as a bug (#26188), but was discarded because "the behaviour is quite dependent on the backend XML parsing implementation" (sic).

So if you want to detect CDATA sections and not depend on a given implementation, you'll have to forget this function and use xml_parse and xml_set_character_data_handler instead.
POST at PSPAETH dot DE 14-Aug-2008 05:45
I wrote a simple function which totally flattens any xml structure. You can then directly get the data or use regular expression functions to fetch data from the output. E.g., if the input is
   <gallery xmlns='http://www.pspaeth.de/joomlaPlugins/psg'>
        <g imageDir='einsatzGallerie'></g>
          <caption for="f1.jpg"}This is picture number 1</caption>
          <caption for="f2.jpg"}This is picture number 2</caption>
        </g>
   </gallery>
the output will be an array:
   ['.gallery0@xmlns']=>
      ".gallery0@xmlns=http://www.pspaeth.de/joomlaPlugins/psg"
   ['.gallery0.g0@imageDir']=>
      ".gallery0.g0@imageDir=einsatzGallerie"
   ['.gallery0.g0.caption0@for']=>
      ".gallery0.g0.caption0@for=f1.jpg"
   ['.gallery0.g0.caption0']=>
      ".gallery0.g0.caption0=This is picture number 1"
   ['.gallery0.g0.caption1@for']=>
      ".gallery0.g0.caption1@for=f2.jpg"
   ['.gallery0.g0.caption1']=>
      ".gallery0.g0.caption1=This is picture number 2"

Here the code:
<?php
function _xmlToArray($xml) {
   $parser = xml_parser_create('UTF-8'); // UTF-8 or ISO-8859-1
   xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
   xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
   xml_parse_into_struct($parser, $xml, $values);
   xml_parser_free($parser);

   $return = array();
   $stack = '';
   $attrs = array();
   $levelCounters = array(0);
   foreach($values as $val) {
     if($val['type'] == "open") {
       $ind = array_pop($levelCounters);
       $stack = $stack . "." . $val['tag'] . $ind;
       if ($val['attributes']) {
           foreach($val['attributes'] as $attrKey => $attrVal)
             $return[$stack . '@' . $attrKey] = $stack . '@' . $attrKey . "=" .
                $attrVal;
       }
       array_push($levelCounters,$ind+1);
       array_push($levelCounters,0);
     } elseif($val['type'] == "close") {
       $stack = substr($stack,0,strrpos($stack,'.'));
     } elseif($val['type'] == "complete") {
       $ind = array_pop($levelCounters);
       $stack = $stack . "." . $val['tag'] . $ind;
       if ($val['attributes']) {
           foreach($val['attributes'] as $attrKey => $attrVal)
             $return[$stack . '@' . $attrKey] = $stack . '@' . $attrKey . "=" .
                $attrVal;
       }
       $return[$stack] = $stack . "=" . $val['value'];
       $stack = substr($stack,0,strrpos($stack,'.'));
       array_push($levelCounters,$ind+1);
     }
   }
   return $return;
}
?>
wickedfather at hotmail dot com 06-Jul-2008 09:33
Had a comment up that was removed saying I'd found drawbacks, but here's a class what I wrote which works fine for me.  Comments removed due to post restrictions.  Pass the xml into the parse method or in the constructor.  Php4 and 5 safe.

class XMLThing
{
    var $rawXML;
    var $valueArray = array();
    var $keyArray = array();
    var $parsed = array();
    var $index = 0;
    var $attribKey = 'attributes';
    var $valueKey = 'value';
    var $cdataKey = 'cdata';
    var $isError = false;
    var $error = '';

    function XMLThing($xml = NULL)
    {
        $this->rawXML = $xml;
    }

    function parse($xml = NULL)
    {
        if (!is_null($xml))
        {
            $this->rawXML = $xml;
        }

        $this->isError = false;

        if (!$this->parse_init())
        {
            return false;
        }

        $this->index = 0;
        $this->parsed = $this->parse_recurse();
        $this->status = 'parsing complete';

        return $this->parsed;
    }

    function parse_recurse()
    {
        $found = array();
        $tagCount = array();

        while (isset($this->valueArray[$this->index]))
        {
            $tag = $this->valueArray[$this->index];
            $this->index++;

            if ($tag['type'] == 'close')
            {
                return $found;
            }

            if ($tag['type'] == 'cdata')
            {
                $tag['tag'] = $this->cdataKey;
                $tag['type'] = 'complete';
            }

            $tagName = $tag['tag'];

            if (isset($tagCount[$tagName]))
            {
                if ($tagCount[$tagName] == 1)
                {
                    $found[$tagName] = array($found[$tagName]);
                }

                $tagRef =& $found[$tagName][$tagCount[$tagName]];
                $tagCount[$tagName]++;
            }
            else
            {
                $tagCount[$tagName] = 1;
                $tagRef =& $found[$tagName];
            }

            switch ($tag['type'])
            {
                case 'open':
                    $tagRef = $this->parse_recurse();

                    if (isset($tag['attributes']))
                    {
                        $tagRef[$this->attribKey] = $tag['attributes'];
                    }

                    if (isset($tag['value']))
                    {
                        if (isset($tagRef[$this->cdataKey]))
                        {
                            $tagRef[$this->cdataKey] = (array)$tagRef[$this->cdataKey];
                            array_unshift($tagRef[$this->cdataKey], $tag['value']);
                        }
                        else
                        {
                            $tagRef[$this->cdataKey] = $tag['value'];
                        }
                    }
                    break;

                case 'complete':
                    if (isset($tag['attributes']))
                    {
                        $tagRef[$this->attribKey] = $tag['attributes'];
                        $tagRef =& $tagRef[$this->valueKey];
                    }

                    if (isset($tag['value']))
                    {
                        $tagRef = $tag['value'];
                    }
                    break;
            }
        }

        return $found;
    }

    function parse_init()
    {
        $this->parser = xml_parser_create();

        $parser = $this->parser;
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
        if (!$res = (bool)xml_parse_into_struct($parser, $this->rawXML, $this->valueArray, $this->keyArray))
        {
            $this->isError = true;
            $this->error = 'error: '.xml_error_string(xml_get_error_code($parser)).' at line '.xml_get_current_line_number($parser);
        }
        xml_parser_free($parser);

        return $res;
    }
}
Anonymous 02-Jul-2008 05:35
@jemptymethod:

I found only one downfall to your function. If you have duplicates, the first one cannot be accessed iteratively.

I added the following to fix the problem:

<?php
...
        else if (is_array($parent['children'][$tag])) {
            if(!isset($parent['children'][$tag][0])) {
                $oldSingle = $parent['children'][$tag];
                $parent['children'][$tag] = null;
                $parent['children'][$tag][] = $oldSingle;

            }
              $parent['children'][$tag][] = $pop['children'];
        }
...
?>
jemptymethod at gmail dot com 16-Jun-2008 10:39
@wickedfather, re: beaudurrant....I just cannot concur that the latter's class is "great"....First of all, it doesn't handle attributes.  Second of all, as my code below demonstrates, there is no need waste processing by determining duplicates within their own loop.  Thirdly, when there are duplicates, it collapses the layer in the array above the duplicates.

Here is some much more consise code, though not (yet) object oriented, and also not yet handling attributes; it maintains a stack for each level:

<?php
function xml_parse_into_assoc($file) {
  $data = implode("", file($file));
  $p = xml_parser_create();

  xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
  xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);

  xml_parse_into_struct($p, $data, $vals, $index);
  xml_parser_free($p);

  $levels = array(null);

  foreach ($vals as $val) {
    if ($val['type'] == 'open' || $val['type'] == 'complete') {
      if (!array_key_exists($val['level'], $levels)) {
        $levels[$val['level']] = array();
      }
    }

    $prevLevel =& $levels[$val['level'] - 1];
    $parent = $prevLevel[sizeof($prevLevel)-1];

    if ($val['type'] == 'open') {
      $val['children'] = array();
      array_push(&$levels[$val['level']], $val);
      continue;
    }

    else if ($val['type'] == 'complete') {
      $parent['children'][$val['tag']] = $val['value'];
    }

    else if ($val['type'] == 'close') {
      $pop = array_pop($levels[$val['level']]);
      $tag = $pop['tag'];

      if ($parent) {
        if (!array_key_exists($tag, $parent['children'])) {
          $parent['children'][$tag] = $pop['children'];
        }
        else if (is_array($parent['children'][$tag])) {
          $parent['children'][$tag][] = $pop['children'];
        }
      }
      else {
        return(array($pop['tag'] => $pop['children']));
      }
    }

    $prevLevel[sizeof($prevLevel)-1] = $parent;
  }
}
?>
jukea 18-Apr-2008 11:09
concerning Adam Tylmad's code, note that the line

 if ($data = xml::cleanString($data))

prevents 0 values to be considered, as this will evaluate to false. I just tracked down this bug in our system .. ouch
wickedfather at hotmail dot com 12-Apr-2008 05:36
To beaudurrant - that class is great and structures things in a very sensible way.  Only problem is that it raises an error if a tag is empty, so would suggest a simple mod to the parse method just to add an isset test.

if (isset($val['value']))
{
    $this->setArrayValue($this->output, $stack, $val['value']);
}
mathiasrav at gmail dot com 10-Mar-2008 05:42
In response to Anonymous' post at 26-Feb-2008 11:50:

Saying that you "don't understand everything" isn't going to get you very popular - you should understand the code you use.
foreach isn't *slow* in PHP, it is actually faster than the equivalent for-construct (which, in many cases, isn't available).

The reason your script is slow is simply your use of xml_parse_into_struct - it reads the whole XML-string and doesn't return until it has parsed and validated it all. If you're looking for efficiency, you'll have to use the more low-level xml_parser_create, xml_set_*_handler functions. Then make sure you don't keep everything in a huge array before outputting it (at least don't if you're going for efficiency).
Anonymous 26-Feb-2008 11:50
Hi, I actually use this parser without understanding everything. I read somewhere that using "foreach" is very slow, and I indeed noticed that this parser was slow, when getting a lot of data, so how should I edit it to make it faster ? (with the exact same output) thanks in advance

    $xml_parser = xml_parser_create();
    $data = $outputone;

    xml_parse_into_struct($xml_parser, $data, $vals, $index);
    xml_parser_free($xml_parser);

    $params = array();
    $level = array();
    $i="1";

    foreach ($vals as $xml_elem) {
        if ($xml_elem['type'] == 'open' && $xml_elem['level'] == '1') {
                $level[$xml_elem['level']] = $xml_elem['tag'];
        }
        if ($xml_elem['type'] == 'open' && $xml_elem['level'] == '2') {
                $level[$xml_elem['level']] = $xml_elem['tag']."".$i;
                $i++;
        }
        if ($xml_elem['type'] == 'complete') {
            $start_level = 1;
            $php_stmt = '$params';
            while($start_level < $xml_elem['level']) {
                $php_stmt .= '[$level['.$start_level.']]';
                $start_level++;
            }
            $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
                eval($php_stmt);
        }
    }
cesaralcaide at gmail dot com 10-Jan-2008 11:35
I didn't fount an appropiate xml2array translaction for my purpose, so I wrote this:

(convert an xml string to an associative array allowing multiple elements with the same name)

/////////////////////////////////// Inicio XML
//
//
//
// Convierte un XML en un array asociativo cuyos elementos son arrays
// (para permitir varios elementos del mismo nombre)
//
// Limitacion: el elemento "attributos" no puede aparecer en el XML, pues corresponde al
// de attribs de un tag (un Tag empieza con "T",no "t")
//
////////////////////////////////////

function xml_analiza($xml) {
 global $xml_resul,$xml_n,$xml_cont,$xml_attr;
 $xml_n = 0;
 $xml_resul = array();
 $xml_cont = array();
 $xml_attr = array();
 $p = xml_parser_create();
 //Si quisieramos distinguir Mayusculas-Minusculas en los nombres de etiquetas:
 xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
 xml_set_element_handler($p, "xml_inicio", "xml_fin");
 xml_set_character_data_handler($p, "xml_char");

 // Recorta hasta el primer tag del XML:
 $i = instr($xml,"<" . "?xml");
 if (!$i) return array();
 $j = instr($i,$xml,"?" . ">");
 if (!$j) return array();
 $xml = substr($xml,$j+2);

 if (!xml_parse($p, $xml))
  alarma("URG","xml_analiza","XML error: " . xml_error_string(xml_get_error_code($p))
   . " en la linea " . xml_get_current_line_number($p) . " ($xml)");
 xml_parser_free($p);
 if (!sizeof($xml_resul)) return array();
 return $xml_resul[0];
}

function xml_inicio($p, $nombre, $atributos) {
 global $xml_resul,$xml_n,$xml_cont,$xml_attr;
 $xml_n++;
 $xml_resul[] = array();
 $xml_cont[] = "";
 $xml_attr[] = $atributos;
}

function xml_fin($p, $nombre) {
 global $xml_resul,$xml_n,$xml_cont,$xml_attr;
 $xml_n--;
 $nuevo = array_pop($xml_resul);
 $nombre = $nombre;
 if ($nombre == "attributos") alarma("URG","xml_analiza","Tag con nombre no permitido (attributos)");
 $conte = array_pop($xml_cont);
 $attrib = array_pop($xml_attr);
 if ($conte) $xml_resul[$xml_n][$nombre][] = $conte;
 else {
  if ($nuevo) {
   if ($attrib) $nuevo["attributos"][] = $attrib;
   $xml_resul[$xml_n][$nombre][] = $nuevo;
  }
  else
   $xml_resul[$xml_n][$nombre][] = "";
 }
}

function xml_char($p, $data) {
 global $xml_cont;
 $xml_cont[sizeof($xml_cont)-1] .= trim(str_replace("\n","",$data));
}

function xml_a($a) {
 // Devuelve un elemento del array XML, p.e.: xml_a($v,"FichaCircuito","Red",0)
 $n = func_num_args();
 for ($i=1;$i<$n;$i++) {
  $b = func_get_arg($i);
  if (isset($a[$b]))
   $a = $a[$b];
  else {
   if (!isset($a[0][$b][0])) return "";
   $a = $a[0][$b];
  }
 }
 return $a;
}
beaudurrant at gmail dot com 19-Dec-2007 08:23
This is extending what Alf Marius Foss Olsen had posted above.

It takes into account array keys with the same name and uses an increment for them instead of overwriting the keys.

I am using it for SOAP requests (20K - 150K) and it parses very fast compared to PEAR.

<?

class XMLParser  {

    // raw xml
    private $rawXML;
    // xml parser
    private $parser = null;
    // array returned by the xml parser
    private $valueArray = array();
    private $keyArray = array();

    // arrays for dealing with duplicate keys
    private $duplicateKeys = array();

    // return data
    private $output = array();
    private $status;

    public function XMLParser($xml){
        $this->rawXML = $xml;
        $this->parser = xml_parser_create();
        return $this->parse();
    }

    private function parse(){

        $parser = $this->parser;

        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); // Dont mess with my cAsE sEtTings
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);     // Dont bother with empty info
        if(!xml_parse_into_struct($parser, $this->rawXML, $this->valueArray, $this->keyArray)){
            $this->status = 'error: '.xml_error_string(xml_get_error_code($parser)).' at line '.xml_get_current_line_number($parser);
            return false;
        }
        xml_parser_free($parser);

        $this->findDuplicateKeys();

        // tmp array used for stacking
        $stack = array();
        $increment = 0;

        foreach($this->valueArray as $val) {
            if($val['type'] == "open") {
                //if array key is duplicate then send in increment
                if(array_key_exists($val['tag'], $this->duplicateKeys)){
                    array_push($stack, $this->duplicateKeys[$val['tag']]);
                    $this->duplicateKeys[$val['tag']]++;
                }
                else{
                    // else send in tag
                    array_push($stack, $val['tag']);
                }
            } elseif($val['type'] == "close") {
                array_pop($stack);
                // reset the increment if they tag does not exists in the stack
                if(array_key_exists($val['tag'], $stack)){
                    $this->duplicateKeys[$val['tag']] = 0;
                }
            } elseif($val['type'] == "complete") {
                //if array key is duplicate then send in increment
                if(array_key_exists($val['tag'], $this->duplicateKeys)){
                    array_push($stack, $this->duplicateKeys[$val['tag']]);
                    $this->duplicateKeys[$val['tag']]++;
                }
                else{
                    // else send in tag
                    array_push($stack,  $val['tag']);
                }
                $this->setArrayValue($this->output, $stack, $val['value']);
                array_pop($stack);
            }
            $increment++;
        }

        $this->status = 'success: xml was parsed';
        return true;

    }

    private function findDuplicateKeys(){

        for($i=0;$i < count($this->valueArray); $i++) {
            // duplicate keys are when two complete tags are side by side
            if($this->valueArray[$i]['type'] == "complete"){
                if( $i+1 < count($this->valueArray) ){
                    if($this->valueArray[$i+1]['tag'] == $this->valueArray[$i]['tag'] && $this->valueArray[$i+1]['type'] == "complete"){
                        $this->duplicateKeys[$this->valueArray[$i]['tag']] = 0;
                    }
                }
            }
            // also when a close tag is before an open tag and the tags are the same
            if($this->valueArray[$i]['type'] == "close"){
                if( $i+1 < count($this->valueArray) ){
                    if(    $this->valueArray[$i+1]['type'] == "open" && $this->valueArray[$i+1]['tag'] == $this->valueArray[$i]['tag'])
                        $this->duplicateKeys[$this->valueArray[$i]['tag']] = 0;
                }
            }

        }

    }

    private function setArrayValue(&$array, $stack, $value){
        if ($stack) {
            $key = array_shift($stack);
            $this->setArrayValue($array[$key], $stack, $value);
            return $array;
        } else {
            $array = $value;
        }
    }

    public function getOutput(){
        return $this->output;
    }

    public function getStatus(){
        return $this->status;
    }

}

?>

Usage:

$p = new XMLParser($xml);
$p->getOutput();
php dot net at crazedsanity dot com 24-Oct-2007 04:32
There's an updated version of cs-phpxml (http://sf.net/projects/cs-phpxml, or https://cs-phpxml.svn.sourceforge.net/svnroot/cs-phpxml/releases for the latest out of subversion) which easily converts an XML string into a PHP array.  Using my previous example:::

<?php
/**
 *
 * *********** EXAMPLE ***********
 *
 * Original file contents:
 * <test xmlns="stuff">
 *         <indexOne>hello</indexOne>
 *         <my_single_index testAttribute="hello" />
 *         <multiple_items>
 *             <item>1</item>
 *             <item>2</item>
 *         </multiple_items>
 * </test>
 *
 * Would return:::
 *
 * array(
 *     TEST => array(
 *         indexOne => hello,
 *         my_single_index => NULL,
 *         multiple_items => array(
 *               items => array(
 *                   0 => 1,
 *                   1 => 2
 *               )
 *         ),
 *     ),
 * );
*/
?>

I've been using this in many production environments, and it's been very stable.  The syntax is pretty simple, too:::

<?php
require_once(dirname(__FILE__) ."/cs-phpxml/xmlParserClass.php");
$xmlParser = new xmlParser(file_get_contents("test.xml"));
$myArray = $xmlParser->get_tree(TRUE);

?>
Alf Marius Foss Olsen 12-Sep-2007 03:46
<?php
/*
  An easy lightweight (Array ->) XML -> Array algorithm..

  Typical case: You have an array you want to export to an external server,
                so you make XML out of it, exports it, and "on the other side"
                make it into an array again. These two functions will take care
                of that last part, ie XML -> Array

  NOTE! The function XMLToArray assumes that the XML _dont_ have nodes on the
        same level with the same name, then it just wont work. This is not a
        problem, as this case deals with Array -> XML -> Array, and an array
        cant have to identical indexes/keys.
*/

function XMLToArray($xml) {
  $parser = xml_parser_create('ISO-8859-1'); // For Latin-1 charset
  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); // Dont mess with my cAsE sEtTings
  xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); // Dont bother with empty info
  xml_parse_into_struct($parser, $xml, $values);
  xml_parser_free($parser);

  $return = array(); // The returned array
  $stack = array(); // tmp array used for stacking
  foreach($values as $val) {
    if($val['type'] == "open") {
      array_push($stack, $val['tag']);
    } elseif($val['type'] == "close") {
      array_pop($stack);
    } elseif($val['type'] == "complete") {
      array_push($stack, $val['tag']);
      setArrayValue($return, $stack, $val['value']);
      array_pop($stack);
    }//if-elseif
  }//foreach
  return $return;
}//function XMLToArray

function setArrayValue(&$array, $stack, $value) {
  if ($stack) {
    $key = array_shift($stack);
    setArrayValue($array[$key], $stack, $value);
    return $array;
  } else {
    $array = $value;
  }//if-else
}//function setArrayValue

// USAGE:
$xml = <<<QQQ
<?xml version="1.0"?>
<root>
  <node1>Some text</node1>
  <node2a>
    <node2b>
      <node2c>Some text</node2c>
    </node2b>
  </node2a>
</root>\n
QQQ;
$array = XMLToArray($xml);
print "<pre>";
print_r($array);
print "</pre>";
//  Output:
//
//  Array
//  (
//      [root] => Array
//          (
//              [node1] => Some text
//              [node2a] => Array
//                  (
//                      [node2b] => Array
//                          (
//                              [node2c] => Some text
//                          )
//                  )
//          )
//  )
?>
vinod at citadel-soft dot com 01-Sep-2007 12:43
My previous code was having some bugs in, that is fixed now

<?php
class CSLXmlReader {
    private $tagstack;
    private $xmlvals;
    private  $xmlvarArrPos;
    private $xmlfile;
    function __construct($filename)  // constructor to intialize the stack and val array
    {

        $this->tagstack = array();   // contain the open tags till now
        $this->xmlvals = array();
        $this->xmlvarArrPos = $this->xmlvals;  // temporary variable to hold the current tag position
        $this->xmlfile = $filename;
    }
    function readDatabase()
    {
        // read the XML database
        $data = implode("", file($this->xmlfile));
        $parser = xml_parser_create();
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
        xml_parse_into_struct($parser, $data, $values, $tags);
        xml_parser_free($parser);

        foreach($values as $key => $val)  //
        {
            if($val['type'] == "open")
            {
                array_push($this->tagstack, $val['tag']);
                $this->getArrayPath();
                if(count($this->xmlvarArrPos) > 0 && (!array_key_exists(0,$this->xmlvarArrPos)))
                {
                    $temp1 = $this->xmlvarArrPos;
                    $this->xmlvarArrPos =  array();
                    $this->xmlvarArrPos[0] = $temp1;
                    array_push($this->tagstack, 1);
                }
                else if( array_key_exists(0,$this->xmlvarArrPos)){
                    $opncount = count($this->xmlvarArrPos);
                    array_push($this->tagstack, $opncount);
                }
                $tagStackPointer += 1;
            }else if($val['type'] == "close")
            {
                while( $val['tag'] != ($lastOpened = array_pop($this->tagstack))){}
            }else if($val['type'] ==  "complete")
            {
                $this->getArrayPath();
                if( array_key_exists($val['tag'],$this->xmlvarArrPos))
                {
                    if(array_key_exists(0,$this->xmlvarArrPos[$val['tag']]))
                    {
                        $elementCount = count($this->xmlvarArrPos[$val['tag']]);
                        $this->xmlvarArrPos[$val['tag']][$elementCount] = $val['value'];
                    }else
                    {
                        $temp1 = $this->xmlvarArrPos[$val['tag']];
                        $this->xmlvarArrPos[$val['tag']] =  array();
                        $this->xmlvarArrPos[$val['tag']][0] = $temp1;
                        $this->xmlvarArrPos[$val['tag']][1] = $val['value'];
                    }
                } else
                {
                    $this->xmlvarArrPos[$val['tag']] = $val['value'];
                }
            }
        }
        reset($this->xmlvals);
        return $this->xmlvals;
    }
    function getArrayPath()
    {

        reset($this->xmlvals);
        $this->xmlvarArrPos = &$this->xmlvals;
        foreach($this->tagstack as $key)
        {
            $this->xmlvarArrPos = &$this->xmlvarArrPos[$key];

        }
    }

}

$readerObj  = new CSLXmlReader("test.xml");
$xmlvals = $readerObj->readDatabase();

echo "########## XML Values In array as the multidimentional array  #############\n";
echo "<pre>";
print_r($xmlvals);
echo "</pre>";

?>
php dot net at crazedsanity dot com 13-Jul-2007 07:43
If you're interested in something that creates arrays in PHP, handles attributes well, and is easily transferrable back into XML, you may want to take a look at the cs-phpxml project at SourceForge.net (http://sf.net/projects/cs-phpxml).  It's not necessarily documented very well, but it will do something like this:

<?php
/**
 *
 * *********** EXAMPLE ***********
 *
 * Original file contents:
 * <test xmlns="stuff">
 *         <indexOne>hello</indexOne>
 *         <my_single_index testAttribute="hello" />
 *         <multiple_items>
 *             <item>1</item>
 *             <item>2</item>
 *         </multiple_items>
 * </test>
 *
 * Would return:
 *
 * array(
 *     TEST => array(
 *         type => 'open',
 *         attributes => array(
 *             xmlns => 'stuff'
 *         )
 *         INDEXONE => 'hello',
 *         MY_SINGLE_INDEX = array(
 *             type => 'complete',
 *
 *         )
 *     )
 * );
*/
?>

It's presently under development, but I'm using it in several production environments.  The XMLCreator is kinda clunky (builds XML within PHP code).  NOTE: it has a dependency on "cs-arraytopath", also available at sourceforge via http://sf.net/projects/cs-arraytopath .  The setup is a bit irritating, and it's fragile when handling quoting & formatting the data, but I think it's worth the hassle for most projects.
siteres at gmail dot com 07-Feb-2007 10:38
PHP: XML to Array and backwards:

Here the XML with PHP solution: XML->Array and Array->XML.
Work with it as with usual array.

Sources are here:
http://mysrc.blogspot.com/2007/02/php-xml-to-array-and-backwards.html
(leave me comments:)

Example #1 (1.xml):

<ddd>
 <onemore dd="55">
  <tt>333</tt>
  <tt ss="s1">555</tt>
  <tt>777</tt>
 </onemore>
 <two>sdf rr</two>
</ddd>

The code:

$xml=xml2ary(file_get_contents('1.xml'));
print_r($xml);

Here is the Array result:

Array
(
   [ddd] => Array (
           [_c] => Array (
                   [_p] => Array *RECURSION*
                   [onemore] => Array (
                           [_a] => Array (
                                   [dd] => 55
                               )
                           [_c] => Array (
                                   [_p] => Array  *RECURSION*
                                   [tt] => Array (
                                           [0] => Array (
                                                   [_v] => 333
                                               )
                                           [1] => Array (
                                                   [_a] => Array (
                                                           [ss] => s1
                                                       )
                                                   [_v] => 555
                                               )
                                           [2] => Array (
                                                   [_v] => 777
                                               )
                                       )
                               )
                       )
                   [two] => Array (
                           [_v] => sdf rr
                       )
               )
       )
)
amish 08-Jan-2007 06:21
Previous parser worked great for me, except a few issues. It did not work well if the element has attributes. I had a huge xml with so many elements, and attributes. Somehow, it mixed up my array, and messed up the keys. Hope following code help fix those issues....

$xml_response = "
<test>
   <item>
     <name att=\"this should show up\">Item1</name>
     <id>item_1</id>
     <description> This is Item 1</description>
     <quantity>10</quantity>
     <navigation website='site1'>test1</navigation>
     <navigation website='site2'>test2</navigation>
   </item>
</test>
"

$parser = xml_parser_create();
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($parser,$xml_response,$values,$tags);
xml_parser_free($parser);

$params = array();
$level = array();

foreach ($values as $xml_elem) {
    $start_level = 1;
           if ($xml_elem['type'] == 'open') {
            if (array_key_exists('attributes',$xml_elem)) {
                 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
             } else {
                 $level[$xml_elem['level']] = $xml_elem['tag'];
             }
               $name_array    =    array();
         $i=0;
           }

           if ($xml_elem['type'] == 'complete') {
           if(!in_array($xml_elem['tag'], $name_array)){
                 array_push($name_array, $xml_elem['tag']);
                   $i=0;
              }else{
            $i++;
             }
             $php_stmt = '$params';
             while($start_level < $xml_elem['level']) {
                      $php_stmt .= '[$level['.$start_level.']]';
                    $test    =    $php_stmt;
                    $start_level++;
             }
             $php_stmt .= '[$xml_elem[\'tag\']][$i] = $xml_elem[\'value\'];';
             if(isset($xml_elem['attributes'])){
                         foreach ($xml_elem['attributes'] as $key=>$va){
                     $attribute    =    '';
                     $new_stmt    =    '';
                     $attribute    =    "".$i."_attribute_".$key."";
                       $new_stmt .= $test.'[$xml_elem[\'tag\']][$attribute] = $va;';
                     eval($new_stmt);
                    }
              }
              eval($php_stmt);
              $start_level--;
           }
           if ($xml_elem['type'] == 'close') {
           array_pop($level);
           }
}
webmaster at after5webdesign dot com 30-Nov-2006 01:48
That parser also has a problem in which it will not parse more items than the current level it is on. That is, parsing this:  <1><2>A</2><2>B</2><2>C</2></1>
Will only result in this: A B
C is never processed.

It might be better with something like this:
          $file = get_url('http://news.search.yahoo.com/news/rss?p=current+events', URL_CONTENT);
          $data = $file['content'];
          $xml_parser = xml_parser_create();
          xml_parse_into_struct($xml_parser, $data, $vals, $index);
          xml_parser_free($xml_parser);
          //Uncomment the lines below to see the entire structure of your XML document
          //echo "<pre>INDEX: \n";
          //print_r ($index);
          //echo "\n \n \n VALUES:";
          //print_r ($vals);
          //echo "</pre>";
          $params = array();
          $level = array();
          $start_level = 1;
          foreach ($vals as $xml_elem) {
            if ($xml_elem['type'] == 'open') {
              if (array_key_exists('attributes',$xml_elem)) {
                list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
              } else {
                $level[$xml_elem['level']] = $xml_elem['tag'];
              }
            }
            if ($xml_elem['type'] == 'complete') {
              $php_stmt = '$params';
              while($start_level < $xml_elem['level']) {
                $php_stmt .= '[$level['.$start_level.']]';
                $start_level++;
              }
              $php_stmt .= '[$xml_elem[\'tag\']][] = $xml_elem[\'value\'];';
              eval($php_stmt);
              $start_level--;
            }
          }
          echo "<pre>";
          print_r ($params);
          echo "</pre>";

~Tim_Myth
tsivert 17-Nov-2006 12:06
To John.

The reason that you only get the last item is that you declare a array of one element that is constantly overwritten by the last element...

I don't know if you want to put the items as two different childarrays of the parent or if you want to put the items in one childarray with two elements.

To put the items in two different childarrays, change the line
$php_stmt .= '[$level['.$start_level.']]';
to
$php_stmt .= '[$level['.$start_level.']][]';

To put in same childarray change line
$php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
to
$php_stmt .= '[$xml_elem[\'tag\']][] = $xml_elem[\'value\'];';

Hope this helps you!

tsivert
john 10-Nov-2006 08:15
I'm currently using this parser and it's working the way I want it to, but it has a little glitch and I was hoping maybe someone can let me know why.

Here's the parser and use for example purposes the following input:

$xml_response = '<?xml version="1.0" encoding="UTF-8"?>
<test>
    <item>First Item</item>
    <item>Second Item</item>
</test>'

$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $xml_response, $vals, $index);
xml_parser_free($xml_parser);

$params = array();
$level = array();
foreach ($vals as $xml_elem) {
  if ($xml_elem['type'] == 'open') {
   if (array_key_exists('attributes',$xml_elem)) {
     list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
   } else {
     $level[$xml_elem['level']] = $xml_elem['tag'];
   }
  }
  if ($xml_elem['type'] == 'complete') {
   $start_level = 1;
   $php_stmt = '$params';
   while($start_level < $xml_elem['level']) {
     $php_stmt .= '[$level['.$start_level.']]';
     $start_level++;
   }
   $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
   eval($php_stmt);
  }
}

echo "<pre>";
print_r ($params);
echo "</pre>";

At the output, only the last <item> shows (i.e.Second Item). The first one is lost.

What should I change so it keeps ALL <item> tags.
A3 04-Nov-2006 02:28
XML -> Array
<?
    $data = '<root><a><b x="s" a="2">asdf</b><c></c></a></root>';

    $p = xml_parser_create();
    xml_parse_into_struct($p, $data, $vals);
    xml_parser_free($p);

    $key = $output = array();
   foreach ($vals as $id=>$item) {
      if ($item["type"]=="open" || $item["level"]>count($key)) {// && count($key)<=$item["level"])
          array_push($key, $id);
          $temp = array("tag"=>$item["tag"], "value"=>"", "attributes"=>array());
          eval("\$output[".implode("][", $key)."] = \$temp;");
      }
      if ($item["type"]=="close" || $item["level"]<count($key))// && $item["level"]>=count($key))
          array_pop($key);
      if (isset($item["attributes"]))
          eval("\$output[".implode("][", $key)."]['attributes'] = array_merge(\$output[".implode("][", $key)."]['attributes'], \$item['attributes']);");
      if (isset($item["value"]))
          eval("\$output[".implode("][", $key)."]['value'] .= \$item['value'];");
   }
?>
Elad Elrom 12-Sep-2006 11:14
This is a quick fix for parsing XML from remote URL, some of the example above will work when trying to parse on your local server without "http://" but not when trying to parse from remote server using "http://www.URL"...

<?
$file="http://www.URL.com/file.XML";

$xml_parser = xml_parser_create();

$handle = fopen($file, "rb");
$contents = '';
while (!feof($handle)) {
  $data .= fread($handle, 8192);
}
fclose($handle);

xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);

$params = array();
$level = array();
foreach ($vals as $xml_elem) {
  if ($xml_elem['type'] == 'open') {
   if (array_key_exists('attributes',$xml_elem)) {
     list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
   } else {
     $level[$xml_elem['level']] = $xml_elem['tag'];
   }
  }
  if ($xml_elem['type'] == 'complete') {
   $start_level = 1;
   $php_stmt = '$params';
   while($start_level < $xml_elem['level']) {
     $php_stmt .= '[$level['.$start_level.']]';
     $start_level++;
   }
   $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
   eval($php_stmt);
  }
}

echo "<pre>";
print_r ($params);
echo "</pre>";
?>
mad dot cat at mcmadcat dot com 06-Sep-2006 01:55
this my love function:
<?php
function mc_parse_xml($filename)
{
    $xml = file_get_contents($filename);
    $p = xml_parser_create();
    xml_parse_into_struct($p, $xml, $values, $index);
    xml_parser_free($p);
    for ($i=0;$i<count($values);$i++) {
        if (isset($values[$i]['attributes'])) {
            $parent = $values[$i]['tag'];
            $keys = array_keys($values[$i]['attributes']);
            for ($z=0;$z<count($keys);$z++)
            {
                $content[$parent][$i][$keys[$z]] = $values[$i]['attributes'][$keys[$z]];
                if (isset($content[$parent][$i]['VALUE'])) $content[$parent][$i]['VALUE'] = $values[$i]['value'];
            }
        }
    }
    foreach ($content as $key => $values) {
        $content[$key] = array_values($content[$key]);
    }
    if (is_array($content)) return $content;
    else return false;
}
?>
webmaster at unitedscripters dot com 17-Jul-2006 02:29
Ps keep in mind that some Rss feeds include spurious tags as... html entities (see Google news Rss feeds: they include tables as &lt;table blah blah!).

If so, in my rssSnapper below add this:

<?php
$input=preg_replace("/(<!\\[CDATA\\[)|(\\]\\]>)/", '', $input);
$input=html_entity_decode($input); //<-- added line
?>

You may play around with the code and make it perfect, testing it on various feeds. Not _all_ XML is worth of an XML parser and the sleepless nights it entails.
webmaster at unitedscripters dot com 16-Jul-2006 11:43
It may be not entirely immaterial to stress that when you are dealing with incoming XML files such as RSS feeds, and you are about to include several of them in some page of yours, resorting to the PHP XML oriented functions is neither _necessarily_ the best idea, nor it is _strictly_ indispensable.

I have in mind, here, also a note that time ago was on this documentation by some info at gramba dot tv:

QUOTE
I was working with the xml2array functions below and had big performance problems. I fired them on a 20MB XML file and had to quit since all approaches of parsing where just too slow (more than 20 Minute parsing etc..). The solution was parsing it manually with preg_match, which increased performance by more than 20 times (processing time about 1 minute).
UNQUOTE

Calling in a specific XML structure function, and arranging a whole class, when all you want from an incoming files may be the contents of a few tags, is not the only option you are left with, when you are at PHP.

Here is a simple function that parses a XML RSS feed using no XML oriented function: keeping this in mind may spare you the need to create extremely complex classes as the ones we see here when _all_ you may want is a few titles and descriptions from an RSS (if that's your goal, you don't need XML parsers):

<?php
function rssSnapper($input='', $limit=0, $feedChannel='Yahoo!News'){
$input=file_get_contents($input);
    if(!$input){return '';};
$input=preg_replace("/[\\n\\r\\t]+/", '', $input);
$input=preg_replace("/(<!\\[CDATA\\[)|(\\]\\]>)/", '', $input);
preg_match_all("/<item>(.*?)<\\/item>/", $input, $items, PREG_SET_ORDER);
$limit=(int)$limit;
$limit=($limit && is_numeric($limit) && abs($limit)<sizeof($items))? sizeof($items)-abs($limit): 0;
while(sizeof($items)>$limit){
    $item=array_shift($items);
    $item=$item[1];
    preg_match_all("/<link>(.*?)<\\/link>/", $item, $link, PREG_SET_ORDER);
    preg_match_all("/<title>(.*?)<\\/title>/", $item, $title, PREG_SET_ORDER);
    preg_match_all("/<author>(.*?)<\\/author>/", $item, $author, PREG_SET_ORDER);
    preg_match_all("/<pubDate>(.*?)<\\/pubDate>/", $item, $pubDate, PREG_SET_ORDER);
    preg_match_all("/<description>(.*?)<\\/description>/", $item, $description, PREG_SET_ORDER);
        if(sizeof($link)){ $link = strip_tags($link[0][1]); };
        if(sizeof($title)){ $title = strtoupper( strip_tags($title[0][1]) ); };
        if(sizeof($author)){ $author = strip_tags($author[0][1]); };
        if(sizeof($pubDate)){ $pubDate = strip_tags($pubDate[0][1]); };
        if(sizeof($description)){ $description = strip_tags($description[0][1]); };
    print <<<USAVIT

    <!-- ITEM STARTS -->
    <div class="news_bg_trick">
    <a href="$link" class="item" target="_blank">
    <span class="title">$title<span class="channel">$feedChannel</span></span>
    <span class="title_footer">
    by <span class="author">$author</span> -
    <span class="date">$pubDate</span>
    </span>
    <span class="description">$description</span>
    </a>
    </div>
    <!-- ITEM ENDS -->

USAVIT;
}//out of loop
/*unitedscripters.com*/}
?>

The printing phase assigns Css class names: the output is thus fully customizable by a mere style sheet.

The use of strip_tags is a reminder from Chris Shiflett: distrust incoming data, always, anyway.
I hope no typos slipped in in transcription. Arguably not perfect, but I hope a good alternative idea to spending three days on a full fledged XML parser just to grab... three tags from a RSS feed!
bye, ALberto
efredricksen at gmail dot com 24-May-2006 09:55
Perhaps the one true parser:? I modified xademax's fine code to tidy it up, codewise and style wise, rationalize some minor crazyness, and make names fit nomenclature from the XML spec. (There are no uses of eval, and shame on you people who do.)

<?php
class XmlElement {
  var $name;
  var $attributes;
  var $content;
  var $children;
};

function xml_to_object($xml) {
  $parser = xml_parser_create();
  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  xml_parse_into_struct($parser, $xml, $tags);
  xml_parser_free($parser);

  $elements = array();  // the currently filling [child] XmlElement array
  $stack = array();
  foreach ($tags as $tag) {
    $index = count($elements);
    if ($tag['type'] == "complete" || $tag['type'] == "open") {
      $elements[$index] = new XmlElement;
      $elements[$index]->name = $tag['tag'];
      $elements[$index]->attributes = $tag['attributes'];
      $elements[$index]->content = $tag['value'];
      if ($tag['type'] == "open") {  // push
        $elements[$index]->children = array();
        $stack[count($stack)] = &$elements;
        $elements = &$elements[$index]->children;
      }
    }
    if ($tag['type'] == "close") {  // pop
      $elements = &$stack[count($stack) - 1];
      unset($stack[count($stack) - 1]);
    }
  }
  return $elements[0];  // the single top-level element
}

// For example:
$xml = '
<parser>
   <name language="en-us">Fred Parser</name>
   <category>
       <name>Nomenclature</name>
       <note>Noteworthy</note>
   </category>
</parser>
';
print_r(xml_to_object($xml));
?>

will give:

xmlelement Object
(
    [name] => parser
    [attributes] =>
    [content] =>
    [children] => Array
        (
            [0] => xmlelement Object
                (
                    [name] => name
                    [attributes] => Array
                        (
                            [language] => en-us
                        )

                    [content] => Fred Parser
                    [children] =>
                )

            [1] => xmlelement Object
                (
                    [name] => category
                    [attributes] =>
                    [content] =>
                    [children] => Array
                        (
                            [0] => xmlelement Object
                                (
                                    [name] => name
                                    [attributes] =>
                                    [content] => Nomenclature
                                    [children] =>
                                )

                            [1] => xmlelement Object
                                (
                                    [name] => note
                                    [attributes] =>
                                    [content] => Noteworthy
                                    [children] =>
                                )

                        )

                )

        )

)
Stuart 24-May-2006 12:26
This is a great little function for a lot of XML work, but note that this function does not handle XML entities properly.

The basic XML entities &lt; &gt; &amp; &quot; are fine, just anything else will not work:

If the entity is defined in the XML header, the parser will drop it completely from the struct it creates.

If the entity is not defined in the XML then the parser will crash out with an undefined entity error.

You should be able to work around this limitation by using a preg_replace on your XML string before passing it to the parser.

(Further details in Bug #35271; this is just a warning to those thinking of using this function for parsing real XML documents not just trivial XML examples)
donna at coloma dot com 28-Apr-2006 09:15
I needed a very simple parser for a set of name-value pairs to be stored in a single database field. I started with the mold example, paired it down, picked up the "id" attributes. Perhaps it will be useful for someone else.

<?php
/* simple conversion for name-value fields */

$xmlInput = "<?xml version=\"1.0\"?>
<mcw_settings>
  <field id=\"imageAlign\">left</field>
  <field id=\"caption\">What a nice picture.</field>
</mcw_settings>";

$desiredResult = array (
    'imageAlign' => "left",
    'caption' => "What a nice picture."
);

function parseFields ($data)
{
    // read the XML database of fields
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $data, $values, $tags);
    xml_parser_free($parser);
    $fields = null;

    // loop through the structures
    $fieldIndices = $tags['field'];
    for ( $i=0; $i < count($fieldIndices); $i++ ) {
        $fieldInfo = $values[$fieldIndices[$i]];
        $fields[$fieldInfo['attributes']['id']] = $fieldInfo['value'];
    }
    return $fields;
}

$test = parseFields($xmlInput);
echo "** Result:\n";
print_r($test);
?>
matt at australiangamer dot com 03-Apr-2006 04:58
I liked VampBoy's code as it gave me structure pretty much as I wanted it. Just two notes, though:

There is a bug in this code. subdivide() checks for pre-existing values, but not in the case of a "complete" element.

In the following (appallingly bad) XML

<Names>
<Name>Matt</Name>
<Name>Stacy</Name>
<Names>

Only Stacy is added to Names in the array, wiping Matt in the process.

To fix this replace the following code
<?php
elseif ($dat[level] === $level && $dat[type] === "complete"){
   $newarray[$dat[tag]]=$dat[value];
}
?>
with
<?php
elseif ($dat[level] === $level && $dat[type] === "complete"){
     if (isset($newarray[$dat['tag']]) && is_array($newarray[$dat['tag']])){
          $newarray[$dat['tag']][] = $dat['value'];
     } elseif (isset($newarray[$dat['tag']]) && !is_array($newarray[$dat['tag']])){
          $newarray[$dat['tag']] = array($newarray[$dat['tag']], $dat['value']);
     } else {
          $newarray[$dat['tag']]=$dat['value'];
     }
}?>

Oh, also, note that the $dat[level], etc, WILL generate warnings, and should be more correctly written as $dat['level'].

The other thing I thought I should point out is that the array keys when created using xml_parse_into_struct will be an UPPERCASE version of your existing element names. If case is important, especially if you, like me, need mixedCase, do the following:

<?php
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
?>
30-Mar-2006 08:57
$simple = '<?xml version="1.0"?>
<moldb>
    <molecule>
        <name>Alanine</name>
        <symbol>ala</symbol>
        <code>A</code>
        <type>hydrophobic</type>
    </molecule>
    <molecule>
        <name>Lysine</name>
        <symbol>lys</symbol>
        <code>K</code>
        <type>charged</type>
    </molecule>
</moldb>';

$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "<pre>";
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);

foreach ($vals as $k => $v) {
    $i = 0;
    if (trim($v['value']) != '') {
        if ($arr[$i][$v['tag']] != '') {
            $i ++;
        }
        $arr[$i][$v['tag']] = $v['value'];
    }
}
xademax at gmail dot com 08-Jan-2006 09:15
This is just another simple xml parser :)

<?php

class Xml
{
    var $tag;
    var $value;
    var $attributes;
    var $next;
}

function xml2array($xml_string)
{
    $Parser = xml_parser_create();
    xml_parser_set_option($Parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($Parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($Parser, $xml_string, $Xml_Values);
    xml_parser_free($Parser);
    $XmlClass = array();
    $LastObj = array();
    $NowObj = &$XmlClass;

    foreach($Xml_Values as $Xml_Key => $Xml_Value)
    {
        $Index = count($NowObj);
        if($Xml_Value["type"] == "complete")
        {
            $NowObj[$Index] = new Xml;
            $NowObj[$Index]->tag = $Xml_Value["tag"];
            $NowObj[$Index]->value = $Xml_Value["value"];
            $NowObj[$Index]->attributes = $Xml_Value["attributes"];
        }
        elseif($Xml_Value["type"] == "open")
        {
            $NowObj[$Index] = new Xml;
            $NowObj[$Index]->tag = $Xml_Value["tag"];
            $NowObj[$Index]->value = $Xml_Value["value"];
            $NowObj[$Index]->attributes = $Xml_Value["attributes"];
            $NowObj[$Index]->next = array();
            $LastObj[count($LastObj)] = &$NowObj;
            $NowObj = &$NowObj[$Index]->next;
        }
        elseif($Xml_Value["type"] == "close")
        {
            $NowObj = &$LastObj[count($LastObj) - 1];
            unset($LastObj[count($LastObj) - 1]);
        }

    }

    return $XmlClass;
}

$String = "
<parser>
    <parseur_name>MyParser</parseur_name>
    <category>
        <name>Name 1</name>
        <note>A note 1</note>
    </category>
</parser>
";
$Xml = xml2array($String);

print_r($Xml);
?>

This exemple will show :
Array
(
    [0] => Xml Object
        (
            [tag] => parser
            [value] =>
            [attributes] =>
            [next] => Array
                (
                    [0] => Xml Object
                        (
                            [tag] => parseur_name
                            [value] => MyParser
                            [attributes] =>
                            [next] =>
                        )

                    [1] => Xml Object
                        (
                            [tag] => category
                            [value] =>
                            [attributes] =>
                            [next] => Array
                                (
                                    [0] => Xml Object
                                        (
                                            [tag] => name
                                            [value] => Name 1
                                            [attributes] =>
                                            [next] =>
                                        )

                                    [1] => Xml Object
                                        (
                                            [tag] => note
                                            [value] => A note 1
                                            [attributes] =>
                                            [next] =>
                                        )

                                )

                        )

                )

        )

)
VampBoy 14-Dec-2005 11:45
WHUPS! that was a broken test version.here is the real one:

class xml2array{
/* This class parses XML tags into a recursive, associative array with the tags as the associative array elements names.

if it encounters multiples of the same tag within a stream, it enumerates them as a sub array under the tag thus:

Array (
    [Lvl1tag] => Array (
        [0] => Array(
            [Lvl2tag] = "foo")
        [1]=> Array(
            [Lvl2tag] = "bar")
    )
)

It tries to detect when there is only one copy of a tag under another, and concatinate properly.
*/

    function readxmlfile($xmlfile){ // reads XML file in and returns it
      $xmlstream =fopen($xmlfile,r);
      $xmlraw=fread($xmlstream,1000000);
      fclose($xmlstream);
      return $xmlraw;
    }

    function parseXMLintoarray ($xmldata){ // starts the process and returns the final array
      $xmlparser = xml_parser_create();
      xml_parse_into_struct($xmlparser, $xmldata, $arraydat);
      xml_parser_free($xmlparser);
      $semicomplete = $this->subdivide($arraydat);
      $complete = $this->correctentries($semicomplete);
      return $complete;
    }

    function subdivide ($dataarray, $level = 1){
      foreach ($dataarray as $key => $dat){
        if ($dat[level] === $level && $dat[type] === "open"){
          $toplvltag = $dat[tag];
        } elseif ($dat[level] === $level && $dat[type] === "close" && $dat[tag]=== $toplvltag){
          $newarray[$toplvltag][] = $this->subdivide($temparray,($level +1));

          unset($temparray,$nextlvl);
        } elseif ($dat[level] === $level && $dat[type] === "complete"){
          $newarray[$dat[tag]]=$dat[value];
        } elseif ($dat[type] === "complete"||$dat[type] === "close"||$dat[type] === "open"){
          $temparray[]=$dat;
        }
      }
      return $newarray;
    }
function correctentries($dataarray){

if (is_array($dataarray)){
  $keys =  array_keys($dataarray);
  if (count($keys)== 1 && is_int($keys[0])){
    $tmp = $dataarray[0];
    unset($dataarray[0]);
       $dataarray = $tmp;
  }
  $keys2 = array_keys($dataarray);
  foreach($keys2 as $key){
    $tmp2 = $dataarray[$key];
    unset($dataarray[$key]);
    $dataarray[$key] = $this->correctentries($tmp2);
    unset($tmp2);
  }
  }
return $dataarray;
}
}
p dot gasiorowski at axent dot pl 13-Dec-2005 03:31
Something similar to kieran's _xml2array, however much more faster.
------
class ObjectFromXML
{
    var $parser;
    var $iter = 0;
    var $path = array();
    var $xml = array();

    function ObjectFromXML($XML)
    {
        $this->parser = xml_parser_create();

        xml_set_object($this->parser, &$this);

        xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
        xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);

        xml_set_element_handler($this->parser, "hanleTagStart", "hanleTagEnd");
        xml_set_character_data_handler($this->parser, "hanleTagCData");

        xml_parse($this->parser, $XML);
        xml_parser_free($this->parser);

        $this->xml = $this->xml['_children'][0];
    }

    function getEvalPath()
    {
        return '$this->xml[' . "'" . implode("']['", $this->path) . "'" . ']';
    }

    function hanleTagStart($parser, $tag, $attributes)
    {
        array_push($this->path, '_children');
        array_push($this->path, ($this->iter++));

        $e = $this->getEvalPath();
        eval ($e . "['_name'] = \$tag;");
        if ($attributes !== array())
        {
            eval ($e . "['_attributes'] = \$attributes;");
        }
    }

    function hanleTagCData($parser, $cdata)
    {
        $e = $this->getEvalPath();
        eval ($e . "['_value'] = \$cdata;");
    }

    function hanleTagEnd($parser, $tag)
    {
        array_pop($this->path);
        array_pop($this->path);
    }
}
mbirth at webwriters dot de 08-Nov-2005 12:48
Searching for a nice and working way to get a RSS feed into an array-structure, I found the solution posted by kieran but disliked those several eval()s. So I wrote my own using references/pointers.

<?php

class RSSParser {

  var $struct = array();  // holds final structure
  var $curptr;  // current branch on $struct
  var $parents = array();  // parent branches of current branch

  function RSSParser($url) {
    $this->curptr =& $this->struct;  // set ref to base
    $xmlparser = xml_parser_create();
    xml_set_object($xmlparser, $this);
    xml_set_element_handler($xmlparser, 'tag_open', 'tag_close');
    xml_set_character_data_handler($xmlparser, 'cdata');
    $fp = fopen($url, 'r');

    while ($data = fread($fp, 4096))
      xml_parse($xmlparser, $data, feof($fp))
      || die(sprintf("XML error: %s at line %d",
         xml_error_string(xml_get_error_code($xmlparser)),
         xml_get_current_line_number($xmlparser)));

    fclose($fp);
    xml_parser_free($xmlparser);
  }

  function tag_open($parser, $tag, $attr) {
    $i = count($this->curptr['children']);
    $j = count($this->parents);
    $this->curptr['children'][$i]=array();  // add new child element
    $this->parents[$j] =& $this->curptr;  // store current position as parent
    $this->curptr =& $this->curptr['children'][$i];  // submerge to newly created child element
    $this->curptr['name'] = $tag;
    if (count($attr)>0) $this->curptr['attr'] = $attr;
  }

  function tag_close($parser, $tag) {
    $i = count($this->parents);
    if ($i>0) $this->curptr =& $this->parents[$i-1];  // return to parent element
    unset($this->parents[$i-1]);  // clear from list of parents
  }

  function cdata($parser, $data) {
    $data = trim($data);
    if (!empty($data)) {
      $this->curptr['value'] .= $data;
    }
  }

}

$myparser = new RSSParser('getitems.xml');
$anotherparser = new RSSParser('http://johndoe:secret@myfeeds.com/getfeed.xml');

print_r($myparser->struct);
print_r($anotherparser->struct);

?>
kieran at kieran dot ca 20-Oct-2005 06:20
<?
/*
|
| _xml2array - another abstraction layer on xml_parse_into_struct
|              that returns a nice nested array.
|
|      @param: $xml is a string containing a full xml document
|
|     returns: a nested php array that looks like this:
|
|              array
|              (
|                  [_name] => the name of the tag
|                  [_attributes] => an array of 'attribute'=>'value' combos
|                  [_value] => the text contents of the node
|                  [_children] => an array of these arrays, one for each node.
|              )
|
|       notes: thanks to 'jeffg at activestate dot com' who inspired
|              me to essentially re-write his example code from php.net
|
|           me: Kieran Huggins < kieran[at]kieran[dot]ca >
|
*/
function _xml2array($xml){
    global $keys;
    global $level;
    if(!is_array($xml)){ // init on first run
        $raw_xml = $xml;
        $p = xml_parser_create();
        xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
        xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
        xml_parse_into_struct($p, $raw_xml, $xml, $idx);
        xml_parser_free($p);
    }
    for($i=0;$i<count($xml,1);$i++){
        // set the current level
        $level = $xml[$i]['level'];

        if($level<1)break;

        // mark this level's tag in the array
        $keys[$level] = '['.$i.']';

        // if we've come down a level, sort output and destroy the upper level
        if(count($keys)>$level) unset($keys[count($keys)]);

        // ignore close tags, they're useless
        if($xml[$i]['type']=="open" || $xml[$i]['type']=="complete"){

            // build the evalstring
            $e = '$output'.implode('[\'_children\']',$keys);

            // set the tag name
            eval($e.'[\'_name\'] = $xml[$i][\'tag\'];');

            // set the attributes
            if($xml[$i]['attributes']){
                eval($e.'[\'_attributes\'] = $xml[$i][\'attributes\'];');
            }

            // set the value
            if($xml[$i]['value']){
                eval($e.'[\'_value\'] = trim($xml[$i][\'value\']);');
            }

        }

    }

    return $output;
}
?>
info at gramba dot tv 23-Aug-2005 05:54
I was working with the xml2array functions below and had big performance problems. I fired them on a 20MB XML file and had to quit since all approaches of parsing where just too slow (more than 20 Minute parsing etc..). The solution was parsing it manually with preg_match, which increased performance by more than 20 times (processing time about 1 minute).

Rough example function with high performance:

<?php

function customXMLtoARRAY($xmlstring) {

    // get all nodes
    preg_match_all("#<node>(.*?)</node>#s",$xmlstring,$nodes);
    $xmlstring = NULL;

    $allnodes = array();

    // put subnodes into node
    while($nodes = array_pop($nodes[1])) {

        $nodecontent = array();

        // Content1
        preg_match("#<content1>(.*?)</content1>#",$eventreihe,$val);
        $nodecontent['content1'] = $val[1];

        $allnodes[] = $nodecontent;

    }
    return $allnodes;
}

?>
Chris Hester 28-Jul-2005 02:06
The array generated from XML stores not only the elements but also any spaces and linebreaks between the tags. This results in a much longer array. (I had 24 array fields instead of 10!) To cure this use the following code when creating the parser:

<?php
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
?>
grusin at gmail dot com 20-Jul-2005 08:54
Here is a simple patch to peter's xml2array function.

Replace:

<?
case 'open':
$tag_or_id = (array_key_exists ('attributes', xml_elem)) ? $xml_elem['attributes']['ID'] : $xml_elem['tag'];
$ptrs[$level][$tag_or_id] = array ();
$ptrs[$level+1] = & $ptrs[$level][$tag_or_id];
break;
?>

with:

<?
case 'open':
$tag_or_id = (array_key_exists ('attributes', $xml_elem)) ? $xml_elem['attributes']['ID'] : $xml_elem['tag'];
$ptrs[$level][$tag_or_id][] = array ();
$ptrs[$level+1] = & $ptrs[$level][$tag_or_id][count($ptrs[$level][$tag_or_id])-1];
break;
?>

and now code should handle multiple element case :)
Dustin 13-Jul-2005 05:39
If you happen to have problems with weird charaters, I added this code to the first line of dUDA's function:

$XML = utf8_decode($XML);
PhF at madmac dot fr 23-Jun-2005 10:11
The code previously posted by noob at noobsrule dot com doesn't work when the same tag name is used at different levels.
(but perhaps "$php_used_prefix" was intended for that ?)
For example:
<RatedShipment>
  <TransportationCharges>
    ...
  </TransportationCharges>
  <RatedPackage>
    <TransportationCharges>
      ...
    </TransportationCharges>
  </RatedPackage>
</RatedShipment>
<?php
    $p = xml_parser_create();
    xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($p, $UPSxml, $values, $idx);
    xml_parser_free($p);

    // tracking used keys
    $usedKeys = array();
    $deepLevel = -1;

    // start a php array string (evaluated later)
    $forEvalPrefix = '$xml_array';

    // loop throught the value array
    foreach ($values as $key => $val) {
        $tagName = $val['tag']; // pass the key tag into a more friendly looking variable
        $level = $val['level']; // idem
        if($val['type'] == 'open') {
            $deepLevel++; // increase deep level
            $forEvalPrefix .= '[\''. $tagName .'\']';

            // begin used keys checks to allow multidimensionatity under the same tag
            (isset($usedKeys[$level][$tagName])) ? $usedKeys[$level][$tagName]++ : $usedKeys[$level][$tagName] = 0;
            $forEvalPrefix .= '['. $usedKeys[$level][$tagName] .']';
        }
        if($val['type'] == 'complete') {
            ($level > $deepLevel) ? $deepLevel++ : ''; // increase $deepLevel only if current level is bigger
            $tagValue = addslashes($val['value'] ); // format the value for evaluation as a string
            $forEvalSuffix = '[\''. $tagName .'\'] = \''. $tagValue .'\';'; // create a string to append to the current prefix
            $forEval = $forEvalPrefix . $forEvalSuffix; // (without "$php_used_prefix"...)
            eval($forEval); // write the string to the array structure
        }
        if($val['type'] == 'close') {
            unset($usedKeys[$deepLevel]); // Suppress tagname's keys useless
            $deepLevel--;
            $forEvalPrefix = substr($forEvalPrefix, 0, strrpos($forEvalPrefix, '[')); // cut off the used keys node
            $forEvalPrefix = substr($forEvalPrefix, 0, strrpos($forEvalPrefix, '[')); // cut off the end level of the array string prefix
        }
    }
echo '<pre>';
print_r ($xml_array);
echo '</pre>';
?>

MADmac ;-)
peter at elemental dot org 15-Jun-2005 05:46
Quick addendum to my post below... The code does *not* handle cases where multiple elements are used without attribute IDs, as is the case with the moldb example.  (It is probably worth noting that some of the other code below doesn't do this either, and is still more complex.)

It is not difficult to add that functionality, but I have yet to find an elegant way to integrate that feature, and since I don't need that functionality at the moment, I'm going leave that as an exercise to the reader.  ;-)

-p.
peter at elemental dot org 15-Jun-2005 01:09
Some of the code posted previously seemed absurdly complex, so I thought I'd see if I could simplify things a bit. Here's what I came up with.  (I'm still a PHP newbie, so perhaps I simplified too much.  ;-)  Untested for speed, but it *should* be pretty quick since it uses references instead of lookups or (gack) evals.

Enjoy!

/*
 *  Convert a string containing XML into a nested array
 *
 *  Takes a raw XML string as input
 *  Returns a nested array
 */

function xml2array ($xml_data)
{
    // parse the XML datastring
    $xml_parser = xml_parser_create ();
    xml_parse_into_struct ($xml_parser, $xml_data, $vals, $index);
    xml_parser_free ($xml_parser);

    // convert the parsed data into a PHP datatype
    $params = array();
    $ptrs[0] = & $params;
    foreach ($vals as $xml_elem) {
        $level = $xml_elem['level'] - 1;
        switch ($xml_elem['type']) {
        case 'open':
            $tag_or_id = (array_key_exists ('attributes', $xml_elem)) ? $xml_elem['attributes']['ID'] : $xml_elem['tag'];
            $ptrs[$level][$tag_or_id] = array ();
            $ptrs[$level+1] = & $ptrs[$level][$tag_or_id];
            break;
        case 'complete':
            $ptrs[$level][$xml_elem['tag']] = (isset ($xml_elem['value'])) ? $xml_elem['value'] : '';
            break;
        }
    }

    return ($params);

}
gleber at mapnet dot pl 27-Mar-2005 12:03
XMLtoArray created by dudus at onet dot pl was not good enough for me, that's why i decided to create my own version, and here's the result:

<?php

function &last(&$array) {
if (!count($array)) return null;
end($array);
return $array[key($array)];
}

function myParseXML(&$vals, &$dom, &$lev) {
    do {
        $curr = current($vals);
        $lev = $curr['level'];
        switch ($curr['type']) {
            case 'open':
                if (isset($dom[$curr['tag']])) {
                    $tmp = $dom[$curr['tag']];
                    if (!$tmp['__multi'])
                        $dom[$curr['tag']] = array('__multi' => true, $tmp);
                    array_push($dom[$curr['tag']], array());
                    $new =& last($dom[$curr['tag']]);
                } else {
                    $dom[$curr['tag']] = array();
                    $new =& $dom[$curr['tag']];
                }
                next($vals);
                myParseXML(&$vals, $new, $lev);
                break;
            case 'cdata':
                break;
            case 'complete':
                if (!isset($dom[$curr['tag']]))
                    $dom[$curr['tag']] = $curr['value'];
                else {
                    if (is_array($dom[$curr['tag']]))
                        array_push($dom[$curr['tag']] , $curr['value']);
                    else
                        array_push($dom[$curr['tag']] = array($dom[$curr['tag']]) , $curr['value']);
                }
                break;
            case 'close':
                return;
        }
    }
    while (next($vals)!==FALSE);
}

function MyXMLtoArray($XML) {
       $xml_parser = xml_parser_create();
       xml_parse_into_struct($xml_parser, $XML, $vals);
       xml_parser_free($xml_parser);
       reset($vals);
       $dom = array(); $lev = 0;
       myParseXML($vals, $dom, $lev);
       return $dom;
}

?>

MyXMLtoArray() is that function you should call, other are for "internal" use.

P.S. It's 4-5 time faster then dudus at onet dot pl 's code
noob at noobsrule dot com 24-Jan-2005 07:59
This is my first time contributing but I found all the examples either didn't work or were beyond my comprehension.  I also wanted to maintain the XML multi-dimensionality so I could do loops based on keys I was expecting rather than numbers or some other whatnot.  This script is probably way inefficient, but should give you a multidimensional array with a depth that will match the XML source.  Great for situations when you aren't sure what you'll be getting back.  Its also only based on the values array of xml_parse_into_str() using the 3 tag types (open, complete, and close) -- hopefully it will make more sense to other noobs.

Basically it just constructs a string ($php_prefix) that is tweaked if it hits an "open" or "close" tag and evaluated if it hits a "complete" tag.

Thanks.

<?php

# initiate curl session...
$ch = curl_init( "http://yourtargethost" ) or die ( "could not initialize curl" );

# set curl options...
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $request_str );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 0 );
curl_setopt( $ch, CURLOPT_REFERER, https://foobar.com" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );

# execute curl commands...
$curl_result = curl_exec( $ch ) or die ( "could not execute the post" );
curl_close( $ch ); // close curl session

# pass the XML result into 2 arrays ( index and values )...
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $curl_result, $vals, $index);
xml_parser_free($p);

# create an empty used key tracker...
$used_keys = array();

# start a php array string (evaluated later)...
$php_prefix = "$"."my_array";

# loop through the values array...
foreach ($vals as $key => $val) {
    if( $val['type'] == "open" ) {
        $my_tag = $val['tag']; // pass the key tag into a more friendly looking variable...
        $php_prefix .= "['".$my_tag."']"; # add a new level to the array string...

        # begin used key checks to allow multidimensionality under the same tag, adds a number to the prefix if necessary...
        if( ! array_key_exists( $my_tag, $used_keys ) ) {
            $used_keys[$my_tag] = 0;
            $php_prefix .= "[".$used_keys[$my_tag]."]";
        }else {
            $used_keys[$my_tag]++; // increment the key tracker to avoid overwriting existing key...
            $php_prefix .= "[".$used_keys[$my_tag]."]";
        }
    } elseif( $val['type'] == "complete" ) {
        $my_value = addslashes( $val['value'] ); # format the value for evaluation as a string...
        $php_suffix = "['".$val['tag']."'] = \"".$my_value."\";"; # create a string with the tag and value to append to the current prefix (just used here)...
        $php_str = $php_prefix.$php_used_prefix.$php_suffix;
        eval( $php_str ); # write the string to the array structure...
    }
    if( $val['type'] == "close" ) {
        $php_prefix = substr( $php_prefix, 0, strrpos( $php_prefix, "[" ) ); # cut off the used keys node...
        $php_prefix = substr( $php_prefix, 0, strrpos( $php_prefix, "[" ) ); # cut off the end level of the array string prefix...
    }
}

echo "<pre>";
//print_r ($index);
//echo "<br><br><br>";
//print_r ($vals);
//echo "<br><br><br>";
print_r ($my_array);
echo "</pre>";

?>
dudus at onet dot pl 18-Jan-2005 03:33
This function return array from XML. Included attributes and tags in same level.
I wrote this function based on mmustafa code.

enjoy!
dUDA

-------------------------------

<?
function XMLtoArray($XML)
{
    $xml_parser = xml_parser_create();
    xml_parse_into_struct($xml_parser, $XML, $vals);
    xml_parser_free($xml_parser);
    // wyznaczamy tablice z powtarzajacymi sie tagami na tym samym poziomie
    $_tmp='';
    foreach ($vals as $xml_elem)
    {
        $x_tag=$xml_elem['tag'];
        $x_level=$xml_elem['level'];
        $x_type=$xml_elem['type'];
        if ($x_level!=1 && $x_type == 'close')
        {
            if (isset($multi_key[$x_tag][$x_level]))
                $multi_key[$x_tag][$x_level]=1;
            else
                $multi_key[$x_tag][$x_level]=0;
        }
        if ($x_level!=1 && $x_type == 'complete')
        {
            if ($_tmp==$x_tag)
                $multi_key[$x_tag][$x_level]=1;
            $_tmp=$x_tag;
        }
    }
    // jedziemy po tablicy
    foreach ($vals as $xml_elem)
    {
        $x_tag=$xml_elem['tag'];
        $x_level=$xml_elem['level'];
        $x_type=$xml_elem['type'];
        if ($x_type == 'open')
            $level[$x_level] = $x_tag;
        $start_level = 1;
        $php_stmt = '$xml_array';
        if ($x_type=='close' && $x_level!=1)
            $multi_key[$x_tag][$x_level]++;
        while($start_level < $x_level)
        {
              $php_stmt .= '[$level['.$start_level.']]';
              if (isset($multi_key[$level[$start_level]][$start_level]) && $multi_key[$level[$start_level]][$start_level])
                  $php_stmt .= '['.($multi_key[$level[$start_level]][$start_level]-1).']';
              $start_level++;
        }
        $add='';
        if (isset($multi_key[$x_tag][$x_level]) && $multi_key[$x_tag][$x_level] && ($x_type=='open' || $x_type=='complete'))
        {
            if (!isset($multi_key2[$x_tag][$x_level]))
                $multi_key2[$x_tag][$x_level]=0;
            else
                $multi_key2[$x_tag][$x_level]++;
             $add='['.$multi_key2[$x_tag][$x_level].']';
        }
        if (isset($xml_elem['value']) && trim($xml_elem['value'])!='' && !array_key_exists('attributes',$xml_elem))
        {
            if ($x_type == 'open')
                $php_stmt_main=$php_stmt.'[$x_type]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
            else
                $php_stmt_main=$php_stmt.'[$x_tag]'.$add.' = $xml_elem[\'value\'];';
            eval($php_stmt_main);
        }
        if (array_key_exists('attributes',$xml_elem))
        {
            if (isset($xml_elem['value']))
            {
                $php_stmt_main=$php_stmt.'[$x_tag]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
                eval($php_stmt_main);
            }
            foreach ($xml_elem['attributes'] as $key=>$value)
            {
                $php_stmt_att=$php_stmt.'[$x_tag]'.$add.'[$key] = $value;';
                eval($php_stmt_att);
            }
        }
    }
     return $xml_array;
}    // END XMLtoArray
?>
memandeemail at gmail dot com 09-Dec-2004 04:09
a BREAK on right place, will be faster and bugprevent

    /**
    * @return bool
    * @param xml_array $values
    * @param xml_array[reference] $tags
    * @desc Reconstruct xml_parse_into_struct to open and close concept
    */
    function reconstruct_tag($values, &$tags) {
        unset($return);
        foreach ($tags as $tkey => $tvalue) {
            foreach ($tvalue as $vvalue) {
                if ($values[$vvalue]['type'] == 'open' or $values[$vvalue]['type'] == 'complete') {
                    $return[$tkey][] = $vvalue;
                }
                if ($values[$vvalue]['type'] == 'open') {
                    foreach ($tvalue as $tv_value) {
                        if ($values[$tv_value]['type'] == 'close'
                        and $values[$tv_value]['level'] == $values[$vvalue]['level']
                        and $tv_value > $vvalue) {
                            $return[$tkey][] = $tv_value;
                            break;
                        }
                    }
                }
            }
        }
        $tags = $return;
        return (bool) $tags;
    }
memandeemail at gmail dot com 03-Dec-2004 01:32
If you have de XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<teste>
    <teste>
        <teste>
        </teste>
    </teste>
</teste>

this function will return this tags:
Array
(
    [teste] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
        )

)

With this function (right below) will convert the struc to OPEN and CLOSE concept.

The New Struct:
Array
(
    [teste] => Array
        (
            [0] => 0
            [1] => 4
            [2] => 1
            [3] => 3
            [4] => 2
        )

)
The Function:
/**
 * @return bool
 * @param xml_array $values
 * @param xml_array[reference] $tags
 * @desc Reconstruct xml_parse_into_struct
*/
function reconstruct_tag($values, &$tags) {
    unset($return);
    foreach ($tags as $tkey => $tvalue) {
        foreach ($tvalue as $vvalue) {
            if ($values[$vvalue]['type'] == 'open' or $values[$vvalue]['type'] == 'complete') {
                $return[$tkey][] = $vvalue;
            }
            if ($values[$vvalue]['type'] == 'open') {
                foreach ($tvalue as $tv_value) {
                    if ($values[$tv_value]['type'] == 'close'
                    and $values[$tv_value]['level'] == $values[$vvalue]['level']) {
                        $return[$tkey][] = $tv_value;
                    }
                }
            }
        }
    }
    $tags = $return;
    return (bool) $tags;
}

Ahh, DO NOT forget this comment:

jukkaho at mail dot student dot oulu dot fi
29-Jun-2004 12:17
Everyone seems to forget the return value of this function (including people making examples!), but it is there for a reason:
0 if failed (invalid XML), 1 if succeeded.

Return value really is an integer, be careful with those === operators.
rob at digital hyphen crocus dot com 03-Sep-2004 06:21
I wrote this function in a bad mood, to get some data you can loop through. sprint_r or var_dump its output to see what it does. It's not pretty, but at least you can loop through the data without a LOT of hassle.

This can also be done with handlers, but I couldn't be bothered with global variables.

<?php

$data = xml_parse_into_struct($somexml);

function xml_getmeaningfuldata($data) {
foreach($data as $element) {
        if ($element['type'] == "open") {
                if (!$elementnos[$parents.$element['tag']]) {
                        $elementnos[$parents.$element['tag']] = 1;
                }
                $elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['attributes'] = $element['attributes'];
                $elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['value'] = trim($element['value']);
                $parents .= $element['tag'].":".$elementnos[$parents.$element['tag']]."^";
        }
        if ($element['type'] == "cdata") {
                if (trim($element['value'])) {
                        $elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['values'] = $element['value'];
                }
        }
        if ($element['type'] == "complete") {
                if (!$elementnos[$parents.$element['tag']]) {
                        $elementnos[$parents.$element['tag']] = 1;
                }
                $elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['attributes'] = $element['attributes'];
                $elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['value'] = trim($element['value']);
                $elementnos[$parents.$element['tag']]++;
        }
        if ($element['type'] == "close") {
                $parents = substr($parents, 0, strrpos(substr($parents, 0, -1), "^")+1);
                $elementnos[$parents.$element['tag']]++;
//              if (sizeof($parents) == 1) unset($parents);
        }
}

return $elements;

}
happyseb at free dot fr 24-Aug-2004 05:51
I would just add something about the solution proposed by "gordon at kanazawa dot ac dot jp".

Something went wrong about the fact that "cdata" are treated the same way as "complete" in the xml_get_children first and in the xml_get_child then.

When ?
with this method, something like this :
<tagA>
  <tagB>valueB</tagB>
  valueA
  <tagC>valueC</tagC>
</tagA>
is considered as the same thing as
<tagA>
  <tagB>valueB</tagB>
  <tagA>valueA</tagA>
  <tagC>valueC</tagC>
</tagA>

The algorithm is nomore bijective.

Even if it might not be perfect :-) i propose a little modification of the xml_get_child function :

function xml_get_child(&$v, $children=NULL) {
      $c = array(); // the "child"
      if (isset($v['tag']) && $v['type'] != 'cdata') $c['tag'] = $v['tag'];
      if (isset($v['value'])) $c['value'] = $v['value'];
      if (isset($v['attributes'])) $c['attributes'] = $v['attributes'];
      if (is_array($children)) $c['children'] = $children;
      return $c;
}

moreover, the following function will take the tree (made from the xml_get_tree function) as a parameter and build the xml content back

    /*
    // setTree
    // set xml Tree from a well-formed array
    // manage indentation
    // Parameters are :
    // $1 (needed) : Tree to transform
    // $2 (optional) : Indentation type (default is one tabulation)
    // $3 (optional) : First indent (default is empty string)
    */

    function setTree($Atree, $indentType = '    ', $Sindent = '')
    {
      if (! isset ($Atree['tag']) && isset ($Atree['value'])) return ($Sindent.trim($Atree['value'])."\r\n") ;
      else {
        $Stemp = $Sindent.'<'.$Atree['tag'].' ' ;
        if (isset ($Atree['attributes']) && is_array ($Atree['attributes'])) {
          foreach ($Atree['attributes'] as $Aattribute => $Svalue) $Stemp .= $Aattribute.'="'.$Svalue.'" ' ;
        }
        if (! isset ($Atree['children']) && ! isset ($Atree['value'])) $Stemp .= '/>'."\r\n" ;
        else {
          $Stemp .= '>'."\r\n" ;
          if (isset ($Atree['value'])) $Stemp .= $Sindent.$indentType.trim($Atree['value'])."\r\n" ;
          if (isset ($Atree['children']) && is_array ($Atree['children']))
            foreach ($Atree['children'] as $Achild) $Stemp .= $this->setTree($Achild, $indentType, $Sindent.$indentType) ;
          $Stemp .= $Sindent.'</'.$Atree['tag'].'>'."\r\n" ;
        }
        return ($Stemp) ;
      }
    }
sdh33 at cornell dot edu 14-Jun-2004 07:12
(The previous post I wrote was incorrect.  I fix that herein)
I used the algorithm from stephane for writing back to a file, but ran into the following problem.  My original XML looks like

<ENTRY id="1"> ... </ENTRY><ENTRY id="2"> ... </ENTRY>

This got saved back as

<1> ... </1><2> ... </2>,

which expat doesn't like.  So I propose the following modification:

function writeXmlToFile($data, $f, $niv = 0) {
  foreach($data as $name => $elem) {
    if (preg_match("/\A[0-9]/",$name)) {
      $open = "ENTRY id=\"$name\""; $name = "ENTRY";
    } else {
      $open = $name;
    }
    if(is_array($elem)) {
      fwrite($f, str_repeat("  ", $niv)."<".$open.">"."\n");
      writeXmlToFile($elem, $f, $niv + 1);
      fwrite($f, str_repeat("  ", $niv)."</".$name.">"."\n");
    } else {
      fwrite($f, str_repeat("  ", $niv)."<".$open.">".$elem."</".$name.">"."\n");
    }
  }
}
stephane dot dekeyzer at noemail dot com 08-Apr-2004 08:54
for writing the array of mmustafa back to a xml file, use this code:

function writeXmlFile($fileName, $struct){
  $f = fopen($fileName, "w");
  writeXmlToFile($struct, $f);
  fclose($f);
}

function writeXmlToFile($data, $f, $niv = 0){
  foreach($data as $name => $elem){
    if(is_array($elem)){
      fwrite($f, str_repeat("  ", $niv)."<".$name.">"."\n");
      writeXmlToFile($elem, $f, $niv + 1);
      fwrite($f, str_repeat("  ", $niv)."</".$name.">"."\n");
    }
    else{
      fwrite($f, str_repeat("  ", $niv)."<".$name.">".$elem."</".$name.">"."\n");
    }
  }
}
gordon at kanazawa dot ac dot jp 26-Jan-2004 05:36
Many thanks to "dave at ovumdesign dot com" for his excellent routines based on the fine work of "gdemartini at bol dot com". I offer a further refinement below, which removes the duplication of the code to create a child node. The resulting tree is the same as in Dave's earlier version and so can be compacted using his "xml_condense_tree"

function xml_get_child(&$v, $children=NULL) {
   $c = array(); // the "child"
   if (isset($v['tag'])) $c['tag'] = $v['tag'];
   if (isset($v['value'])) $c['value'] = $v['value'];
   if (isset($v['attributes'])) $c['attributes'] = $v['attributes'];
   if (is_array($children)) $c['children'] = $children;
   return $c;
}
function xml_get_children(&$values, &$i) {
   $children = array();
   while ($i < count($values)) {
      $v = &$values[$i++];
      switch ($v['type']) {
         case 'cdata':
         case 'complete':
            $children[] = xml_get_child($v);
            break;
         case 'open':
            $children[] = xml_get_child($v, xml_get_children($values, $i));
            break;
         case 'close':
            break 2; // leave "while" loop
      }
   }
   return $children;
}
function xml_get_tree($file) {
   $data = join('', file($file));

   $p = xml_parser_create();
   xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
   xml_parser_set_option($p, XML_OPTION_SKIP_WHITE,  1);
   xml_parse_into_struct($p, $data, $values);
   xml_parser_free($p);

   return xml_get_children($values, $i=0);
}
mmustafa at vsnl dot com 06-Jan-2004 09:19
A small and simple php code to convert the raw array returned by xml_parse_into_struct into a proper data structure ..

<?php
$file = "data.xml";
$xml_parser = xml_parser_create();

if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

$data = fread($fp, filesize($file));
fclose($fp);
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);

$params = array();
$level = array();
foreach ($vals as $xml_elem) {
  if ($xml_elem['type'] == 'open') {
    if (array_key_exists('attributes',$xml_elem)) {
      list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
    } else {
      $level[$xml_elem['level']] = $xml_elem['tag'];
    }
  }
  if ($xml_elem['type'] == 'complete') {
    $start_level = 1;
    $php_stmt = '$params';
    while($start_level < $xml_elem['level']) {
      $php_stmt .= '[$level['.$start_level.']]';
      $start_level++;
    }
    $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
    eval($php_stmt);
  }
}

echo "<pre>";
print_r ($params);
echo "</pre>";
?>

Converts an XML document like ..

<country id="ZZ">
<name>My Land</name>
<location>15E</location>
<area>40000</area>
<state1>
<name>Hi State</name>
<area>1000</area>
<population>2000</population>
<city1>
<location>13E</location>
<population>500</population>
<area>500</area>
</city1>
<city2>
<location>13E</location>
<population>500</population>
<area>5000</area>
</city2>
</state1>
<state2>
<name>Low State</name>
<area>3000</area>
<population>20000</population>
<city1>
<location>15E</location>
<population>5000</population>
<area>1500</area>
</city1>
</state2>
</country>

To a php datastructure like ..

Array
(
    [ZZ] => Array
        (
            [NAME] => My Land
            [LOCATION] => 15E
            [AREA] => 40000
            [STATE1] => Array
                (
                    [NAME] => Hi State
                    [AREA] => 1000
                    [POPULATION] => 2000
                    [CITY1] => Array
                        (
                            [LOCATION] => 13E
                            [POPULATION] => 500
                            [AREA] => 500
                        )

                    [CITY2] => Array
                        (
                            [LOCATION] => 13E
                            [POPULATION] => 500
                            [AREA] => 5000
                        )

                )

            [STATE2] => Array
                (
                    [NAME] => Low State
                    [AREA] => 3000
                    [POPULATION] => 20000
                    [CITY1] => Array
                        (
                            [LOCATION] => 15E
                            [POPULATION] => 5000
                            [AREA] => 1500
                        )

                )

        )

)
26-Sep-2003 12:43
actually, using the notation

$object->property is the right one

and you can use it always, preventing errors with arrays
Eric 22-Sep-2003 05:50
Yet another XML -> nested array converter.

This is based on http://www.devdump.com/phpxml.php, but adds features while reducing code complexity.

It can take several forms of input:
 - string (buffer)
 - file handle (pipe)
 - file name -or-
 - URL

It can optionally collapse arrays of same-named tags to a single element if there is only one tag with a given name.

It can optionally preserve tag order for XML applications where position is significant, such as CallXML.

XMLParser.obj at:
http://eric.pollmann.net/work/public_domain/

Corrections and suggestions enthusiastically welcomed.  :)
Adam Tylmad 12-Sep-2003 02:01
I've just debugged and optimized my xmlparser.

gdemartini's xml-example generates this structure:

stdClass Object
(
    [FORM] => stdClass Object
        (
            [SECTION] => stdClass Object
                (
                    [NAME] => Data
                    [EDITFIELD] => stdClass Object
                        (
                            [LABEL] => Text:
                            [NAME] => poll_text
                            [LENGTH] => 255
                            [SIZE] => 56
                            [REQUIRED] => T
                        )

                    [MEMOFIELD] => stdClass Object
                        (
                            [LABEL] => Options
                            [NAME] => options
                            [COLS] => 56
                            [ROWS] => 5
                            [REQUIRED] => T
                        )
                )
        )
)

the moldb example generates this structure:

stdClass Object
(
    [MOLDB] => stdClass Object
        (
            [MOLECULE] => Array
                (
                    [0] => stdClass Object
                        (
                            [NAME] => Alanine
                            [SYMBOL] => ala
                            [CODE] => A
                            [TYPE] => hydrophobic
                        )

                    [1] => stdClass Object
                        (
                            [NAME] => Lysine
                            [SYMBOL] => lys
                            [CODE] => K
                            [TYPE] => charged
                        )
                )
        )
)

here is the code. enjoy!

<?php

class XMLParser {
    var $path;
    var $result;

    function cleanString($string) {
        return trim(str_replace("'", "&#39;", $string));
    }

    function XMLParser($encoding, $data) {
        $this->path = "\$this->result";
        $this->index = 0;

        $xml_parser = xml_parser_create($encoding);
        xml_set_object($xml_parser, &$this);
        xml_set_element_handler($xml_parser, 'startElement', 'endElement');
        xml_set_character_data_handler($xml_parser, 'characterData');

        xml_parse($xml_parser, $data, true);
        xml_parser_free($xml_parser);
    }

    function startElement($parser, $tag, $attributeList) {
        $this->path .= "->".$tag;
        eval("\$data = ".$this->path.";");
        if (is_array($data)) {
            $index = sizeof($data);
            $this->path .= "[".$index."]";
        } else if (is_object($data)) {
            eval($this->path." = array(".$this->path.");");
            $this->path .= "[1]";
        }

        foreach($attributeList as $name => $value)
            eval($this->path."->".$name. " = '".XMLParser::cleanString($value)."';");
    }

    function endElement($parser, $tag) {
        $this->path = substr($this->path, 0, strrpos($this->path, "->"));
    }

    function characterData($parser, $data) {
        if ($data = XMLParser::cleanString($data))
            eval($this->path." = '$data';");
    }
}

?>
plusplus7 at hotmail dot com 18-Jun-2003 03:40
Quick and dirty function to get original file from $vals array in manual example. Build on this for custom parsing of individual elements (something like this should be built-in IMO).

<pre>
function parseVals($vals){
    foreach($vals as $valobj){
        if($valobj['type']=='open' || $valobj['type']=='complete'){
            $retstr .= '<'.$valobj['tag'];
            if(is_array($valobj['attributes'])){
                foreach($valobj['attributes'] as $key=>$val){
                    $retstr .= " $key=\"$val\"";
                }
            }
        }
        if($valobj['type']=='open'){
            $retstr .= '>'.$valobj['value'];
        }elseif($valobj['type']=='complete' && $valobj['value']){
            $retstr .= '>'.$valobj['value'].'</'.$valobj['tag'].'>';
        }elseif($valobj['type']=='complete'){
            $retstr .= ' />';
        }elseif($valobj['type']=='cdata'){
            $retstr .= $valobj['value'];
        }elseif($valobj['type']=='close'){
            $retstr .= '</'.$valobj['tag'].'>';
        }

    }
    return $retstr;
}

</pre>
masuod_a at hotmail dot com 24-Nov-2002 01:32
wanna load object from xml file? this function take a tree returned by xml_get_tree() [dave@ovumdesign.com's version] and return an object.
it can't work with attributes, i hate attributes (except id), but may be useful for you.

- how to use :
------------------------- xml file -------------
<config>
   <images>
      <image id="1">
        <name>my pet</name>
        <file>pet001.jpg</file>
      </image>
      <image id="2">
        <name>harry's pet</name>
        <file>pet003.jpg</file>
      </image>
   </images>
   <library>lib_functions.php</library>
</config>
----------------------------------------------
$config=xml_to_object($xml_tree);
$config->library;    #return "lib_functions.php"
$config->images->image__1->name; #return "my pet"
----------------------------------------------
function xml_to_object($xml_tree,$root=false){
             $object_name=$xml_tree['tag'];
             foreach ($xml_tree['children'] as $children){
                          $child_id=$children['attributes']['id'];
                          $child_name=$children['tag'];
                          $child_name.=($child_id) ? "__".$child_id:'';

                          if (is_array($children['children'])) {
                                      $child_object=xml_to_object($children);
                                      $temp_object->$child_name=$child_object;
                          } else {
                                    $temp_object->$child_name=$children['value'];
                         }
             }

             if (!$root)
                          $xml_object=$temp_object;
             else       $xml_object->$object_name=$temp_object;

             return $xml_object;
}

also you can use this function for loading array from xml , but i suggest you to use kris@h3x.com's functions[ http://www.devdump.com/phpxml.php ].

function xml_to_array($xml_tree,$root=false){
   $array_name=$xml_tree['tag'];
   foreach ($xml_tree['children'] as $children){
       $child_id=$children['attributes']['id'];
       $child_name=$children['tag'];
       $child_name.=($child_id) ? "__".$child_id:'';
       if (is_array($children['children'])) {
            $child_array=xml_to_array($children);
            $temp_array[$child_name]=$child_array;
       } else {
            $temp_array[$child_name]=$children['value'];
       }
   }

   if (!$root)
      $xml_array=$temp_array;
   else
      $xml_array[$array_name]=$temp_array;

    return $xml_array;
}
bisqwit at iki dot fi dot not dot for dot ads dot invalid 20-Nov-2002 05:33
I made my own xml2tree()-function.
The difference to the previously suggested ones on this manual page is that this creates a tree where the tags are keys.

Example input:

<tree>
 <leaf name="gruu"> pl?? </leaf>
 <animal type="cat" />
</tree>

Example output:

Array
( 'tree' => Array
     ( 0 => Array
          ( 'children' => Array
               ( 'leaf' => Array
                    ( 0 => Array
                         ( 'attrs' => Array('name' => 'gruu'),
                           'values' => Array(0 => ' pl?? ')
                    )    ),
                 'animal' => Array
                    ( 0 => Array
                         ( 'attrs' => Array('type' => 'cat')
)    )   )    )    )   )

function xml2tree2($string)
{
  $parser = xml_parser_create();
  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  xml_parse_into_struct($parser, $string, $vals, $index);
  xml_parser_free($parser);

  $stack = array( array() );
  $stacktop = 0;
  $parent = array();
  foreach($vals as $val)
  {
    $type = $val['type'];
    if($type=='open' || $type=='complete')
    {
      // open tag
      $stack[$stacktop++] = $tagi;
      $tagi = array('tag' => $val['tag']);

      if(isset($val['attributes']))  $tagi['attrs'] = $val['attributes'];
      if(isset($val['value']))
        $tagi['values'][] = $val['value'];
    }
    if($type=='complete' || $type=='close')
    {
      // finish tag
      $tags[] = $oldtagi = $tagi;
      $tagi = $stack[--$stacktop];
      $oldtag = $oldtagi['tag'];
      unset($oldtagi['tag']);
      $tagi['children'][$oldtag][] = $oldtagi;
      $parent = $tagi;
    }
    if($type=='cdata')
    {
      $tagi['values'][] = $val['value'];
    }
  }
  return $parent['children'];
}

Ps: Note that the xml parsing for some reason chokes if the values contain <> (even inside ""). You need to escape them as &lt; and &gt;.
Same for & (for obvious reasons). Escape it as &amp;.
For example "&nbsp;" should be written as "&amp;nbsp;".
joeldegan at yahoo dot com 20-Nov-2002 01:40
Using one of the nice function above, I was able to get these two great structures below on nmap info for a machine and then the PID lists for each open port.. Very handy for my current project.

$test = `nmap -sU -sS localhost -oX -`;
$ret = xml2array($test);
$z=0;
while (list($key,$val)=each($ret["host"][0][3]["children"])){
    if ($val["tag"] == "PORT"){
        $new[$z]["PROTOCOL"] = $val["attributes"]["PROTOCOL"];
        $new[$z]["PORT"] = $val["attributes"]["PORTID"];

        //echo $val["attributes"]["PROTOCOL"]."\n";
        //echo $val["attributes"]["PORTID"]."\n";
        $z++;
    }//fi
}//wend

print_r($new);

function port_pids($portnum){
    $ret = `lsof -i :$portnum -F pc`;
    $ret = explode("\n", $ret);
    $z=0;
    for($a=0;$a<count($ret);$a++){
        $port = $ret[$a];
        $a++;
        $com = $ret[$a];
        $new[$z]["PID"]=substr($port, 1);
        $new[$z]["COMMAND"]=substr($com, 1);
        $z++;
    }//wend
    return $new;
}//end function

print_r(port_pids("3306"));
no at chance dot com 08-Nov-2002 07:38
Thanks to gdemartini's tree algorithm , I was able to create a variation that display's the information in a way that allows you to then easily parse the information with less code and process time. Here's the variation and I've also posted a quick function at the bottom that allows you to visually display complex arrays in human readable format. Hope this helps someone as much as gdemartini's functions helped me.

I decided to post it on my server as opposed to the forums as it was too large and needed some good html formatting.  It can found at http://www.devdump.com/phpxml.php .
ambi at gmx dot de 06-Oct-2002 12:59
Sorry.. unreachable and unneeded code in my posting above :-)
Here is the fixed version:
function xml_condense_tree($tree) {
    if ($tree["children"]) {
            unset($nodecounters);
            foreach ($tree['children'] as $index => $node) {
                if (!isset($nodecounters[$node['tag']]))
                $nodecounters[$node['tag']]=1;
                else $nodecounters[$node['tag']]++;
            }
        foreach ($tree['children'] as $index => $node) {
            if (isset($node['children'])) {
                $tree['children'][$index] = xml_condense_tree($node);
            } elseif (isset($node['value']) and !$node['attributes'] and $nodecounters[$node['tag']]==1) {
                $tree['values'][$node['tag']] = $node['value'];
                unset($tree['children'][$index]);
            }
        }
    }
    if (!$tree['children']) unset($tree['children']);

    return $tree;
}
09-Aug-2002 10:20
This is based on a comment above by mh@mainlab.de.  This version makes more sense to me and I included a few comments that might make it more usefull for specific purposes.

// Look through $vals and $index,
// generated by parse_xml_data(),
// for $location.  $location resembles a unix path.
// E.g. "my/xml/path/to/my/data".
// Note: This is NOT a full-feature xml utility.
// Your path must be unique in the xml data
// and the tags must not contain slashes.
//
// returns: cdata from last element of $location
//          or NULL.
function get_xml_values($vals, $index, $location) {
  $location = explode('/', $location);
  $location_level = count($location);
  for ($n=0; $n<$location_level; ++$n) {
    $val_indexes=$index[$location[$n]];
    $good=0;
    for ($i=0; $i<count($val_indexes); ++$i) {
      $v = $vals[$val_indexes[$i]];
      // level is 1 based, n is 0 based:
      if ($v['level']-1 == $n) {
        if ($v['type'] == 'open') {
          // We found an open tag at the correct level.
          $good=1;
          break; // Remove break to avoid 'open' hiding
                 // 'complete' at last $n. (Speed Vs. Robustness:
                 // For now, I'm going with speed).
        }
        if ($v['type'] == 'complete') {
          // We found it, after finding all the prior elements:
          // You may want to change this to
          // return $v
          // so you can call $v['value'] and $v['attributes']
          return $v['value'];
        }
      }
    }
    if (!$good) {
      return NULL;
    }
  }
}
farinspace-at-hotmail-dot-com 21-Jul-2002 03:41
Wow, took a while to consume all of this. I am a noobie at XML, in fact i just really started working with it today. I went ahead and added to the above code by gdemartini@bol.com.br. I also added the update by nyk@cowham.net.

Added 'value' => $vals[$i]['value'] to the open case, as some nodes have both cdata as well as a children node. In previous versions of the functions the cdata of a node(a) that also had children would get stored in the children's array of the parent node(a). Now only children values are in the children array and the cdata is in the value key of the parent node(a).

Added 'value' => $vals[$i]['value'] to tree array as the vals array produced by PHP includes a value to the top most node if no children exist (for completeness).

Also, using xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE,1) will strip all white space from both around the XML tags as well as inside the cdata. I added a piece of code by waldo@wh-e.com to take care of this. All SPACE characters are now only stripped from around the XML tags and cdata spacing is retained.

CODE:
***************************************

function GetChildren($vals, &$i) {

    $children = array();

    while (++$i < sizeof($vals)) {

        // compair type
        switch ($vals[$i]['type']) {

            case 'cdata':
                $children[] = $vals[$i]['value'];
                break;
            case 'complete':
                $children[] = array(
                    'tag' => $vals[$i]['tag'],
                    'attributes' => $vals[$i]['attributes'],
                    'value' => $vals[$i]['value']
                );
                break;
            case 'open':
                $children[] = array(
                    'tag' => $vals[$i]['tag'],
                    'attributes' => $vals[$i]['attributes'],
                    'value' => $vals[$i]['value'],
                    'children' => GetChildren($vals, $i)
                );
                break;
            case 'close':
                return $children;
        }
    }
}

function GetXMLTree($file) {

    $data = implode('', file($file));

    // by: waldo@wh-e.com - trim space around tags not within
    $data = eregi_replace(">"."[[:space:]]+"."<","><",$data);

    // XML functions
    $p = xml_parser_create();

    // by: anony@mous.com - meets XML 1.0 specification
    xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
    xml_parse_into_struct($p, $data, &$vals, &$index);
    xml_parser_free($p);

    $i = 0;
    $tree = array();
    $tree[] = array(
        'tag' => $vals[$i]['tag'],
        'attributes' => $vals[$i]['attributes'],
        'value' => $vals[$i]['value'],
        'children' => GetChildren($vals, $i)
    );

    return $tree;
}

***************************************
dave at ovumdesign dot com 03-Apr-2002 12:32
Here is another variation on gdemartini@bol.com.br's nice tree algorithm. I have included nyk@cowham.net's "cdata" suggestion. I made a few changes to suppress unitialized variable warnings with error_reporting(E_ALL), and made a few stylistic changes.

There are two functional modifications as well. The first is the added option to expat to turn off "case folding", since XML is technically a case-sensitive language. The second is the returned tree - instead of returning a single-element list (an array with index 0 pointing to the top node) like the original algorithm, it returns the top node itself. This saves you an unnecessary level of indirection.

function xml_get_children($vals, &$i) {
    $children = array();
    if (isset($vals[$i]['value'])) $children[] = $vals[$i]['value'];

    while (++$i < count($vals)) {
        switch ($vals[$i]['type']) {
        case 'cdata':
            $children[] = $vals[$i]['value'];
            break;

        case 'complete':
            $children[] = array(
                'tag'        => $vals[$i]['tag'],
                'attributes' => isset($vals[$i]['attributes'])?
                                $vals[$i]['attributes'] : null,
                'value'      => $vals[$i]['value'],
            );
            break;

        case 'open':
            $children[] = array(
                'tag'        => $vals[$i]['tag'],
                'attributes' => isset($vals[$i]['attributes'])?
                                $vals[$i]['attributes'] : null,
                'children'   => xml_get_children($vals, $i),
            );
            break;

        case 'close':
            return $children;
        }
    }
}

function xml_get_tree($file) {
    $data = join('', file($file));

    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE,   1);
    xml_parse_into_struct($parser, $data, $vals, $index);
    xml_parser_free($parser);

    return array(
        'tag'        => $vals[0]['tag'],
        'attributes' => isset($vals[0]['attributes'])?
                        $vals[0]['attributes'] : null,
        'children'   => xml_get_children($vals, $i = 0),
    );
}

This function takes a tree returned by xml_get_tree() and simplifies it similarly to voncken@mailandnews.com and oknapp@soly.de's suggestions. It finds any nodes that expat considered "complete" (childless) and contain no attributes, and it moves the tag name (as the key) and value into an element in the parent node's "values" array.

For example:
$tree = xml_get_tree('moldb.xml');
$tree = xml_condense_tree($tree);
echo $tree['children'][1]['values']['name'];

Outputs:
Lysine

function xml_condense_tree($tree) {
    foreach ($tree['children'] as $index => $node) {
        if (isset($node['children'])) {
            $tree['children'][$index] = xml_condense_tree($node);

        } elseif (isset($node['value']) and !$node['attributes']) {
            $tree['values'][$node['tag']] = $node['value'];
            unset($tree['children'][$index]);
        }
    }

    if (!$tree['children']) unset($tree['children']);
    return $tree;
}

Enjoy,
Dave
voncken at mailandnews dot com 06-Jan-2002 02:24
I needed something a bit different:
this is the result of my xml2array function (from the moldb example)

Array
(
    [molecule] => Array
        (
            [0] => Array
                (
                    [name] => Alanine
                    [symbol] => ala
                    [code] => A
                    [type] => hydrophobic
                )

            [1] => Array
                (
                    [name] => Lysine
                    [symbol] => lys
                    [code] => K
                    [type] => charged
                )

        )

)

function xml2array($data)
  {
    //mvo voncken@mailandnews.com
    //original ripped from  on the php-manual:gdemartini@bol.com.br
    //to be used for data retrieval(result-structure is Data oriented)
    $p = xml_parser_create();
    xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($p, $data, &$vals, &$index);
    xml_parser_free($p);

    $tree = array();
    $i = 0;
    //array_push($tree, array('tag' => $vals[$i]['tag'], 'attributes'=> $vals[$i]['attributes'],                            'children' => ));
    $tree = GetChildren($vals, $i);
    return $tree;
  }

 function GetChildren($vals, &$i)
  {
    $children = array();
    if ($vals[$i]['value']) array_push($children, $vals[$i]['value']);

    $prevtag = "";
    while (++$i < count($vals)) // so pra nao botar while true ;-)
    {
      switch ($vals[$i]['type'])
      {
         case 'cdata':
           array_push($children, $vals[$i]['value']);
           break;
         case 'complete':
           $children[ strtolower($vals[$i]['tag']) ] = $vals[$i]['value'];
           break;

         case 'open':

           //restartindex on unique tag-name
           $j++;
           if ($prevtag <> $vals[$i]['tag']) {
                   $j = 0;
                   $prevtag = $vals[$i]['tag'];
            }
           $children[ strtolower($vals[$i]['tag']) ][$j] = GetChildren($vals,$i);
           break;

         case 'close':
           return $children;
      }
    }
  }


Start parsing an XML document

  • ≪ xml_parse_into_struct
  • xml_parser_create_ns ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Start parsing an XML document

xml_parse

(PHP 4, PHP 5, PHP 7)

xml_parse — Start parsing an XML document

설명

int xml_parse ( resource $parser , string $data [, bool $is_final = false ] )

xml_parse() parses an XML document. The handlers for the configured events are called as many times as necessary.

인수

parser

A reference to the XML parser to use.

data

Chunk of data to parse. A document may be parsed piece-wise by calling xml_parse() several times with new data, as long as the is_final parameter is set and TRUE when the last data is parsed.

is_final

If set and TRUE, data is the last piece of data sent in this parse.

반환값

Returns 1 on success or 0 on failure.

For unsuccessful parses, error information can be retrieved with
xml_get_error_code(),
xml_error_string(),
xml_get_current_line_number(),
xml_get_current_column_number() and
xml_get_current_byte_index().

Note:

Entity errors are reported at the end of the data thus only if is_final is set and TRUE.

User Contributed Notes

Programmerdude 06-Aug-2014 04:26
Due to restrictions in libxml2 the maximum length of $data is 9.5MB. If you need to process more than 9.5MB you can do so by calling the function multiple times and setting $is_final on the last call.
alejandro dot anv at gmail dot com 13-Mar-2014 10:00
Here is my array2xml function. I made it because I coudn't find a generic recursive function that generates a prety formatted XML from any array.

Usage:

<?php echo xml2array('rootname',$myarray); ?>

or if you want to specify a spacing character(s) other than TAB:

<?php echo xml2array('rootname',$myarray,'   '); ?>

or if you want avoid spacing at all

<?php echo xml2array('rootname',$myarray,''); ?>

<?php
function array2xml($nombre,$valor,$tab="\t",$nivel=0){

if ($nivel==0) $r='<?xml version="1.0" encoding="ISO-8859-1"?>';

$arr=!is_numeric($nombre);
$spacing=str_repeat($tab,$nivel);
if ($tab!='') $newline="\n";
else $newline='';

if (is_array($valor)){

   if (count($valor)>0){
      $r.=$newline;
      //$r.=str_repeat($tab,$nivel);
      foreach($valor as $k=>$v){
         if ($arr) $r.="$spacing<$nombre>";

         if (is_array($v))  $r.=array2xml($k,$v,$tab,$nivel+$arr);
         else $r.="$spacing<$k>$v</$k>$newline";

         if ($arr) $r.="$spacing</$nombre>$newline";
         }//foreach
      }//count
   else $r.="$spacing<$nombre></$nombre>"; // it's an empty array
   }
else {
     if ($arr) $r.="$spacing<$nombre>";
     $r.=$valor;
     if ($arr) $r.="</$nombre>$newline";
     }

return $r;
}
?>
Ashok dot 893 at gmail dot com 26-Apr-2010 07:52
This is very simple way to convert all applicable objects into associative array.  This works with not only SimpleXML but any kind of object. The input can be either array or object. This function also takes an options parameter as array of indices to be excluded in the return array. And keep in mind, this returns only the array of non-static and accessible variables of the object since using the function get_object_vars().

<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();

    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }

    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}
?>

Usage:

<?php
$xmlUrl = "feed.xml"; // XML feed file/URL
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
print_r($arrXml);
?>
lz_speedy at web dot de 30-Dec-2008 07:20
Best seen xml2array function ever
<?php
function xml2array($url, $get_attributes = 1, $priority = 'tag')
{
    $contents = "";
    if (!function_exists('xml_parser_create'))
    {
        return array ();
    }
    $parser = xml_parser_create('');
    if (!($fp = @ fopen($url, 'rb')))
    {
        return array ();
    }
    while (!feof($fp))
    {
        $contents .= fread($fp, 8192);
    }
    fclose($fp);
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, trim($contents), $xml_values);
    xml_parser_free($parser);
    if (!$xml_values)
        return; //Hmm...
    $xml_array = array ();
    $parents = array ();
    $opened_tags = array ();
    $arr = array ();
    $current = & $xml_array;
    $repeated_tag_index = array ();
    foreach ($xml_values as $data)
    {
        unset ($attributes, $value);
        extract($data);
        $result = array ();
        $attributes_data = array ();
        if (isset ($value))
        {
            if ($priority == 'tag')
                $result = $value;
            else
                $result['value'] = $value;
        }
        if (isset ($attributes) and $get_attributes)
        {
            foreach ($attributes as $attr => $val)
            {
                if ($priority == 'tag')
                    $attributes_data[$attr] = $val;
                else
                    $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
            }
        }
        if ($type == "open")
        {
            $parent[$level -1] = & $current;
            if (!is_array($current) or (!in_array($tag, array_keys($current))))
            {
                $current[$tag] = $result;
                if ($attributes_data)
                    $current[$tag . '_attr'] = $attributes_data;
                $repeated_tag_index[$tag . '_' . $level] = 1;
                $current = & $current[$tag];
            }
            else
            {
                if (isset ($current[$tag][0]))
                {
                    $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
                    $repeated_tag_index[$tag . '_' . $level]++;
                }
                else
                {
                    $current[$tag] = array (
                        $current[$tag],
                        $result
                    );
                    $repeated_tag_index[$tag . '_' . $level] = 2;
                    if (isset ($current[$tag . '_attr']))
                    {
                        $current[$tag]['0_attr'] = $current[$tag . '_attr'];
                        unset ($current[$tag . '_attr']);
                    }
                }
                $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
                $current = & $current[$tag][$last_item_index];
            }
        }
        elseif ($type == "complete")
        {
            if (!isset ($current[$tag]))
            {
                $current[$tag] = $result;
                $repeated_tag_index[$tag . '_' . $level] = 1;
                if ($priority == 'tag' and $attributes_data)
                    $current[$tag . '_attr'] = $attributes_data;
            }
            else
            {
                if (isset ($current[$tag][0]) and is_array($current[$tag]))
                {
                    $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
                    if ($priority == 'tag' and $get_attributes and $attributes_data)
                    {
                        $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
                    }
                    $repeated_tag_index[$tag . '_' . $level]++;
                }
                else
                {
                    $current[$tag] = array (
                        $current[$tag],
                        $result
                    );
                    $repeated_tag_index[$tag . '_' . $level] = 1;
                    if ($priority == 'tag' and $get_attributes)
                    {
                        if (isset ($current[$tag . '_attr']))
                        {
                            $current[$tag]['0_attr'] = $current[$tag . '_attr'];
                            unset ($current[$tag . '_attr']);
                        }
                        if ($attributes_data)
                        {
                            $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
                        }
                    }
                    $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
                }
            }
        }
        elseif ($type == 'close')
        {
            $current = & $parent[$level -1];
        }
    }
    return ($xml_array);
}
?>

Returns a well formed array like the structure of the xml-document

<root>
 <child1>
  <child1child1/>
 </child1>
</root>

create an array like
array[root][child1][child1child1]

lg
neoyahuu at yahoo dot com 13-Dec-2007 05:18
Instead of passing a URL, we can pass the XML content to this class (either you
 want to use CURL, Socks or fopen to retrieve it first) and instead of using
 array, I'm using separator '|' to identify which data to get (in order to make
 it short to retrieve a complex XML data). Here is my class with built-in fopen
 which you can pass URL or you can pass the content instead :

p/s : thanks to this great help page.

<?php

class xx_xml {

    // XML parser variables
    var $parser;
    var $name;
    var $attr;
    var $data  = array();
    var $stack = array();
    var $keys;
    var $path;

    // either you pass url atau contents.
    // Use 'url' or 'contents' for the parameter
    var $type;

    // function with the default parameter value
    function xx_xml($url='http://www.example.com', $type='url') {
        $this->type = $type;
        $this->url  = $url;
        $this->parse();
    }

    // parse XML data
    function parse()
    {
        $data = '';
        $this->parser = xml_parser_create();
        xml_set_object($this->parser, $this);
        xml_set_element_handler($this->parser, 'startXML', 'endXML');
        xml_set_character_data_handler($this->parser, 'charXML');

        xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);

        if ($this->type == 'url') {
            // if use type = 'url' now we open the XML with fopen

            if (!($fp = @fopen($this->url, 'rb'))) {
                $this->error("Cannot open {$this->url}");
            }

            while (($data = fread($fp, 8192))) {
                if (!xml_parse($this->parser, $data, feof($fp))) {
                    $this->error(sprintf('XML error at line %d column %d',
                    xml_get_current_line_number($this->parser),
                    xml_get_current_column_number($this->parser)));
                }
            }
        } else if ($this->type == 'contents') {
            // Now we can pass the contents, maybe if you want
            // to use CURL, SOCK or other method.
            $lines = explode("\n",$this->url);
            foreach ($lines as $val) {
                if (trim($val) == '')
                    continue;
                $data = $val . "\n";
                if (!xml_parse($this->parser, $data)) {
                    $this->error(sprintf('XML error at line %d column %d',
                    xml_get_current_line_number($this->parser),
                    xml_get_current_column_number($this->parser)));
                }
            }
        }
    }

    function startXML($parser, $name, $attr)    {
        $this->stack[$name] = array();
        $keys = '';
        $total = count($this->stack)-1;
        $i=0;
        foreach ($this->stack as $key => $val)    {
            if (count($this->stack) > 1) {
                if ($total == $i)
                    $keys .= $key;
                else
                    $keys .= $key . '|'; // The saparator
            }
            else
                $keys .= $key;
            $i++;
        }
        if (array_key_exists($keys, $this->data))    {
            $this->data[$keys][] = $attr;
        }    else
            $this->data[$keys] = $attr;
        $this->keys = $keys;
    }

    function endXML($parser, $name)    {
        end($this->stack);
        if (key($this->stack) == $name)
            array_pop($this->stack);
    }

    function charXML($parser, $data)    {
        if (trim($data) != '')
            $this->data[$this->keys]['data'][] = trim(str_replace("\n", '', $data));
    }

    function error($msg)    {
        echo "<div align=\"center\">
            <font color=\"red\"><b>Error: $msg</b></font>
            </div>";
        exit();
    }
}

?>

And example of retrieving XML data:
p/s: example use to retrieve weather

<?php
include_once "xx_xml.class.php";

// Im using simple curl (the original is in class) to get the contents

$pageurl = "http://xml.weather.yahoo.com/forecastrss?p=MYXX0008&u=c";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $pageurl );
$thecontents = curl_exec ( $ch );
curl_close($ch);

// We want to pass only a ready XML content instead of URL
// But if you want to use URL , skip the curl functions above and use this
// $xx4 = new xx_xml("url here",'url');

$xx4 = new xx_xml($thecontents,'contents');
// As you can see, we use saparator '|' instead of long array
$Code = $xx4->data ['rss|channel|item|yweather:condition']['code'] ;
$Celcius = $xx4->data ['rss|channel|item|yweather:condition']['temp'] ;
$Text = $xx4->data ['rss|channel|item|yweather:condition']['text'] ;
$Cityname = $xx4->data ['rss|channel|yweather:location']['city'] ;

?>

Hope this helps.
joris dot landman at chello dot nl 29-Jul-2007 04:42
This page has been a great help! I've adapted the examples below to make a class to parse an X(HT)ML file to a multidimensional array.

Spaces, tabs, breaks, etc. are included in the array as TEXT_NODE, since in XHTML they may be functional. A function to trim them can easily be added if so desired.

I've written my class to store multiple tags with the same name. In order to do this I've used nested arrays with the key NODES to store the order in which tags and data were parsed. These NODES arrays can be used as a blueprint to reconstruct the X(HT)ML part of the document in it's entirity, including formatting. (Doctype will have to be added for validity).

<?php

class my_xml_object {

    var $xml_data; # raw xml data from file
    function parse_xml_file($my_uri) {
        $this->xml_data = null; # clear previously parsed file and related variables
        if (is_file($my_uri) && is_readable($my_uri)) { # existing and readable uri?
            $my_file = fopen($my_uri, "r");
            while($my_xml_input = fread($my_file, filesize($my_uri))) {
                $this->xml_data .= $my_xml_input; # add data to xml_data
            }
            fclose($my_file);
            $this->parse_xml_data($this->xml_data); # parse data
        } else {
            trigger_error("supplied argument is not a URI to a (readable) file", E_USER_ERROR);
        }
    }

    var $xml_array = Array(); # xml array from parsed data
    function parse_xml_data($my_data) { # adapted from class by randlem@gmail.com, tgrabietz@bupnet.de, bbellwfu@gmail.com, Kyle Bresin - see http://nl2.php.net/xml_parse
        $this->xml_array = Array(0 => Array()); # clear previously parsed file and related variables; populate first element
        $my_parser = xml_parser_create(); # set up parser
        xml_set_object($my_parser, $this); # enable parser within object
        xml_set_element_handler($my_parser, "xml_tag_open", "xml_tag_close");
        xml_set_character_data_handler($my_parser, "xml_tag_data");
        if (!xml_parse($my_parser, $my_data)) {
            trigger_error("data can not be parsed", E_USER_ERROR); # inspect problems #die(sprintf("<br />\n<b>Error</b>:  %s on line <b>%d</b><br />\n", xml_error_string(xml_get_error_code($my_parser)), xml_get_current_line_number($my_parser)));
        }
        xml_parser_free($my_parser); # free parser
        return $this->xml_array; # return xml array
    }

    var $my_branch = Array();
    function xml_tag_open($my_parser, $my_name, $my_attributes) {
        array_push($this->my_branch, $my_name); # add tag name to branch
        $this->xml_array[] = Array(); # nest array in xml array for data
        if (count($my_attributes)) {
            $this->xml_array[count($this->xml_array) - 1]["ATTRIBUTES"] = $my_attributes; # nest attributes array
        }
    }

    function xml_tag_data($my_parser, $my_data) {
        $this->xml_array[count($this->my_branch)]["TEXT_NODE"][] = $my_data; # add data to nested array
        $this->xml_array[count($this->my_branch)]["NODES"][] = "TEXT_NODE"; # add text node to nested NODES array
    }

    function xml_tag_close($my_parser, $my_name) {
        $this->xml_array[count($this->my_branch) - 1][$this->my_branch[count($this->my_branch) - 1]][] = $this->xml_array[count($this->xml_array) - 1]; # nest arrays to follow document structure
        array_pop($this->xml_array); # pop off element that was nested
        $this->xml_array[count($this->my_branch) - 1]["NODES"][] = $my_name; # add tag node to nested NODES array
        array_pop($this->my_branch); # update branch
    }

?>
dgrimes at scvl dot com 29-Jun-2007 03:14
One note about magic quotes: magic_quotes_runtime needs to be disabled before parsing XML. It can cause strange errors during parsing. Just add the following at the top of your program:

set_magic_quotes_runtime(0);

If you need magic quotes you can use stripslashes or save the current magic quotes setting with get_magic_quotes_runtime() then disable and parse your XML and then restore the previous magic quotes setting.
php at b635 dot com 23-Jun-2007 07:17
The suggestions below have been a great help, but there was one thing I really needed...

I'm parsing Amazon XML data, and I wanted to be able to index into the array using something like:

<?php
  print "<p>" . $strAXML->arrOutput[ITEMLOOKUPRESPONSE][ITEMS][ITEM][SMALLIMAGE][URL];
?>

To solve this, I had to push all of the open tags onto a separate stack, add any tag data to the tail end of an attributed multi-dimensional array, and then pop the tag name off of the stack once it was closed...

<?php

class xml2array {

  var $arrOutput = array();
  var $arrName = array();
  var $objParser;
  var $strXmlData;

  function parse($strInputXML) {

    // standard XML parse object setup

    $this->objParser = xml_parser_create ();
    xml_set_object($this->objParser,$this);
    xml_set_element_handler($this->objParser, "tagOpen", "tagClosed");

    xml_set_character_data_handler($this->objParser, "tagData");

    $this->strXmlData = xml_parse($this->objParser,$strInputXML );
    if(!$this->strXmlData) {
      die(sprintf("XML error: %s at line %d",
        xml_error_string(xml_get_error_code($this->objParser)),
        xml_get_current_line_number($this->objParser)));
    }

    xml_parser_free($this->objParser);

    return $this->arrOutput;
  }

  function tagOpen($parser, $name, $attrs) {
    // push the current tag name to an array of still-open tag names
    array_push ($this->arrName, $name);

    // merge the array of current attributes to the open tag
    // NOTE: this does not currently handle multiple attributes with the same name
    // (i.e. it will overwrite them with the last values)

    $strEval = "\$this->arrOutput";
    foreach ($this->arrName as $value) {
      $strEval .= "[" . $value . "]";
    }
    $strEval = $strEval . " = array_merge (" . $strEval . ",\$attrs);";

    eval ($strEval);
  }

  function tagData($parser, $tagData) {

    // set the latest open tag equal to the tag data

    $strEval = "\$this->arrOutput";
    foreach ($this->arrName as $value) {
      $strEval .= "[" . $value . "]";
    }

    $strEval = $strEval . " = \$tagData;";

    eval ($strEval);
  }

  function tagClosed($parser, $name) {

    // pop this tag (and any subsequent tags) off the stack of open tag names

    for ($i = count ($this->arrName) - 1; $i > 0; $i--) {
      $currName = $this->arrName[$i];
      array_pop ($this->arrName);
      if ($currName == $name) {
        break;
      }
    }
  }

}

?>
james @at@ mercstudio dot Com dot nospam 02-Apr-2006 09:10
hi,

i've modified bbellwfu at gmail dot com to as below:

features added:
  - toXML (convert back array to xml string)
  - changed name, according to macromedia flash xml concept : children -> childrens, tagdata -> nodevalue, name -> nodename,
  - added pointer firstchild to childrens[0] (if exists)

some findings that i would like to share:
 - <![cdata[my value here]]> (does not work on property value
 - xml file must be htmlentity based (if not using cdata)
 - xml line feed on node data seems to be double line feed on windows (still figuring why)
 - xml line feed on attribute value seems to be ignored...

here's my code below :)

class u007xml
{
   var $arrOutput = array();
   var $resParser;
   var $strXmlData;

    function u007xml($tfile = "")
    {
        if(trim($tfile) != "") { $this->loadFile($tfile);}
    }

    function loadFile($tfile)
    {
        $this->thefile = $tfile;

        $th = file($tfile);
        $tdata = implode("\n", $th);

        return $this->parse($tdata);
    }

   function parse($strInputXML)
   {
        $this->resParser = xml_parser_create ();
        xml_set_object($this->resParser,$this);
        xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");

        xml_set_character_data_handler($this->resParser, "tagData");

        $this->strXmlData = xml_parse($this->resParser,$strInputXML );

        if(!$this->strXmlData) {
           die(sprintf("XML error: %s at line %d",
        xml_error_string(xml_get_error_code($this->resParser)),
        xml_get_current_line_number($this->resParser)));
        }

        xml_parser_free($this->resParser);

        return $this->arrOutput;
   }

   //called on each xml tree
   function tagOpen($parser, $name, $attrs) {
       $tag=array("nodename"=>$name,"attributes"=>$attrs);
       array_push($this->arrOutput,$tag);
   }

  //called on data for xml
   function tagData($parser, $tagData) {
       if(trim($tagData)) {
           if(isset($this->arrOutput[count($this->arrOutput)-1]['nodevalue'])) {
               $this->arrOutput[count($this->arrOutput)-1]['nodevalue'] .= $this->parseXMLValue($tagData);
           }
           else {
               $this->arrOutput[count($this->arrOutput)-1]['nodevalue'] = $this->parseXMLValue($tagData);
           }
       }
   }

  //called when finished parsing
   function tagClosed($parser, $name) {
       $this->arrOutput[count($this->arrOutput)-2]['childrens'][] = $this->arrOutput[count($this->arrOutput)-1];

       if(count ($this->arrOutput[count($this->arrOutput)-2]['childrens'] ) == 1)
       {
            $this->arrOutput[count($this->arrOutput)-2]['firstchild'] =& $this->arrOutput[count($this->arrOutput)-2]['childrens'][0];
       }
       array_pop($this->arrOutput);
   }

    function toArray()
    {
        //not used, we can call loadString or loadFile instead...
    }

    function parseXMLValue($tvalue)
    {
        $tvalue = htmlentities($tvalue);
        return $tvalue;
    }

    function toXML($tob = null)
    {
        //return back xml
        $result = "";

        if( $tob == null)
        {
            $tob = $this->arrOutput;
        }

        if(!isset($tob))
        {
            echo "XML Array empty...";
            return null;
        }

        for($c = 0; $c < count($tob); $c++)
        {
            $result .="<" . $tob[$c]["nodename"];

            while (list($key, $value) = each($tob[$c]["attributes"]))
            {
                $result .=" " . $key."=\"" . $this->parseXMLValue($value) . "\"";
            }

            $result .= ">";

            //assign node value
            if( isset($tob[$c]["nodevalue"]) )
            {
                $result .= $tob[$c]["nodevalue"];
            }

            if( count($tob[$c]["childrens"]) > 0 )
            {
                $result .= "\r\n" . $this->toXML(&$tob[$c]["childrens"]) . "";
            }

            $result .= "</" . $tob[$c]["nodename"] . ">\r\n";

        }//end of each array...

        return $result;
    }

    function displayXML()
    {
        print_r($this->arrOutput);
    }

    function getXML($tob = null)
    {
        return "<?xml version='1.0'?>\r\n" . $this->toXML($tob);
    }

}//end of u007xml class

//examples below:

$xx = new u007xml();
$xx->loadFile("xml3.xml");

//$xx->displayXML();

print $xx->getXML();
ben at autonomic dot net 16-Mar-2006 02:12
bbellwfu's code does not handle 'text nodes' properly.
Consider the innards of a tag like <root>xxx<tag2/>yyy</root>
The 'tagData' for root will be "xxxyyy" and you have lost all information about where "tag2" was in that sequence.

Quick and dirty hack.

Replace tagData with this code :
   function tagData($parser, $tagData) {
          $last_element=count($this->arrOutput)-1;
        $this->arrOutput[$last_element]['children'][] = array("textnode",$tagData);
   }

What this does is adds 'textnodes' as children of its containing parent, *in the right sequence* (rather like the internet browsers do it). This then lets you do some more sensible secondary work like recursively looking up internal references within the document...
Kyle Bresin 20-Jan-2006 12:15
Just wanted to note a small bug in bbellwfu's class (which is really great btw).

It fails to capture any datums which are equal to numerical zero.

The problem lies in the function tagData, the first if statement should be:

if(trim($tagData) != '') {
tim at alloutinteraction dot com 01-Oct-2005 04:29
I wanted to create a really simple XML parser, but I found the array management in xml_parse a bit daunting. So I flattened my XML and parsed it using string matching. It wouldn't be difficult to add xml depth (of 2 plus levels) by modifying the parsedXML array.

<?
// here's the raw html
$xmlRaw="<order>Order data</order><label>Label data</label><control>123</control>";
// here are the xml field names
$xmlFieldNames=array("order", "label", "control");
// for each xml field...
foreach ($xmlFieldNames as $xmlField) {
    if(strpos($xmlRaw,$xmlField)!==false){
        // I've broken 1 single line into 4 for display purposes
        $parsedXML[$xmlField]=substr($xmlRaw,
        strpos($xmlRaw,"<$xmlField>")+strlen("<$xmlField>"),
        strpos($xmlRaw,"</$xmlField>")-strlen("<$xmlField>")
        -strpos($xmlRaw,"<$xmlField>"));
    }
}
print_r($parsedXML);
// prints: Array ( [order] => Order data [label] => Label data [control] => 123 )

?>

Hope you find this useful (coded it while ill in bed with streaming cold, but felt much better afterwards!)

Tim (a lazy coder)
bbellwfu at gmail dot com 05-May-2005 11:51
Just improving a little bit on the code examples from tgrabietz and randlem below... everything in one pretty class, plus some checks in place so that the element data doesnt get split up (thanks to flobee on the xml_set_character_data_handler page)

<?php

/* Usage
 Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;

 $objXML = new xml2Array();
 $arrOutput = $objXML->parse($strYourXML);
 print_r($arrOutput); //print it out, or do whatever!

*/
class xml2Array {

    var $arrOutput = array();
    var $resParser;
    var $strXmlData;

    function parse($strInputXML) {

            $this->resParser = xml_parser_create ();
            xml_set_object($this->resParser,$this);
            xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");

            xml_set_character_data_handler($this->resParser, "tagData");

            $this->strXmlData = xml_parse($this->resParser,$strInputXML );
            if(!$this->strXmlData) {
               die(sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($this->resParser)),
            xml_get_current_line_number($this->resParser)));
            }

            xml_parser_free($this->resParser);

            return $this->arrOutput;
    }
    function tagOpen($parser, $name, $attrs) {
       $tag=array("name"=>$name,"attrs"=>$attrs);
       array_push($this->arrOutput,$tag);
    }

    function tagData($parser, $tagData) {
       if(trim($tagData)) {
            if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
                $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
            }
            else {
                $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
            }
       }
    }

    function tagClosed($parser, $name) {
       $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
       array_pop($this->arrOutput);
    }
}
?>

Will output something like...

<snippet>
Array
(
    [0] => Array
        (
            [name] => GETMESSAGESRESPONSE
            [attrs] => Array
                (
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => STATUS
                            [attrs] => Array
                                (
                                )

                        )

</snippet>
alex dot garcia at noos dot fr 14-Mar-2005 10:47
Here is the inverse function which takes parsed xml array in entry and outputs xml string

enjoy !

function getXmlFromArray($root){

        if(count($root) > 0){

            $curr_name = $root['name'];
            $attribs = $root['attrs'];
            $curr_childs = $root['children'];
            $curr_data = $root['cdata'];

            $xml .= '<'.$curr_name;

            if(count($attribs) > 0){
                $i = 1;
                foreach($attribs as $key => $value){
                    $curr_attribs .= $key.'="'.$value.'"';
                    $i++;
                    if($i <= count($attribs)){
                        $curr_attribs .= ' ';
                    }
                }
                $xml .= ' '.$curr_attribs;
            }

            if($curr_data != ''){
                $xml .= '><![CDATA['.$curr_data.']]></'.$curr_name.'>';
            } else {
                if(count($curr_childs) > 0){
                    $xml .= '>';
                    foreach($curr_childs as $child){
                        $xml .= getXmlFromArray($child);
                    }
                    $xml .= '</'.$curr_name.'>';
                } else {
                    $xml .= '/>';
                }
            }

        }
        return $xml;
    }
tgrabietz at bupnet dot de 22-Sep-2004 05:05
it's like randlem at gmail dot com's great code, without using a "class container" but parsing cdata. The script returns the tree-structure in a single array.

<?php
$file = 'simple.xml';
$stack = array();

function startTag($parser, $name, $attrs)
{
   global $stack;
   $tag=array("name"=>$name,"attrs"=>$attrs);
   array_push($stack,$tag);

}

function cdata($parser, $cdata)
{
    global $stack,$i;

    if(trim($cdata))
    {
        $stack[count($stack)-1]['cdata']=$cdata;
    }
}

function endTag($parser, $name)
{
   global $stack;
   $stack[count($stack)-2]['children'][] = $stack[count($stack)-1];
   array_pop($stack);
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "cdata");

$data = xml_parse($xml_parser,file_get_contents($file));
if(!$data) {
   die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}

xml_parser_free($xml_parser);

print("<pre>\n");
print_r($stack);
print("</pre>\n");
?>
ByK 16-Sep-2004 05:12
modified from yours code. I think it's work!!.
class CXml
{
    var $xml_data;
    var $obj_data;
    var $pointer;

    function CXml() { }

    function Set_xml_data( &$xml_data )
    {
        $this->index = 0;
        $this->pointer[] = &$this->obj_data;

        //strip white space between tags
        $this->xml_data = eregi_replace(">"."[[:space:]]+"."<","><",$xml_data);
        $this->xml_parser = xml_parser_create( "UTF-8" );

        xml_parser_set_option( $this->xml_parser, XML_OPTION_CASE_FOLDING, false );
        xml_set_object( $this->xml_parser, &$this );
        xml_set_element_handler( $this->xml_parser, "_startElement", "_endElement");
        xml_set_character_data_handler( $this->xml_parser, "_cData" );

        xml_parse( $this->xml_parser, $this->xml_data, true );
        xml_parser_free( $this->xml_parser );
    }

    function _startElement( $parser, $tag, $attributeList )
    {
        foreach( $attributeList as $name => $value )
        {
            $value = $this->_cleanString( $value );
            $object->$name = $value;
        }
        //replaces the special characters with the underscore (_) in tag name
        $tag = preg_replace("/[:\-\. ]/", "_", $tag);
        eval( "\$this->pointer[\$this->index]->" . $tag . "[] = \$object;" );
        eval( "\$size = sizeof( \$this->pointer[\$this->index]->" . $tag . " );" );
        eval( "\$this->pointer[] = &\$this->pointer[\$this->index]->" . $tag . "[\$size-1];" );

        $this->index++;
    }

    function _endElement( $parser, $tag )
    {
        array_pop( $this->pointer );
        $this->index--;
    }

    function _cData( $parser, $data )
    {
        if (empty($this->pointer[$this->index])) {
            if (rtrim($data, "\n"))
                $this->pointer[$this->index] = $data;
        } else {
            $this->pointer[$this->index] .= $data;
        }
    }

    function _cleanString( $string )
    {
        return utf8_decode( trim( $string ) );
    }
}

$m_xml = new CXml();
$xml_data = file_get_contents( $filename );

$m_xml->Set_XML_data( $xml_data );

$newsid = $m_xml->obj_data->root[0]->NewsID[0];
randlem at gmail dot com 15-Sep-2004 05:43
Here's a handy way to generate a tree that can be can be decended easily.

<?php
$file = 'xmltest.xml';

$tag_tree = array();
$stack = array();

class tag {
    var $name;
    var $attrs;
    var $children;
    function tag($name, $attrs, $children) {
        $this->name = $name;
        $this->attrs = $attrs;
        $this->children = $children;
    }
}

function startTag($parser, $name, $attrs) {
    global $tag_tree, $stack;
    $tag = new tag($name,$attrs,'');
    array_push($stack,$tag);
}

function endTag($parser, $name) {
    global $stack;
    $stack[count($stack)-2]->children[] = $stack[count($stack)-1];
    array_pop($stack);
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");

$data = xml_parse($xml_parser,file_get_contents($file));
if(!$data) {
    die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}

xml_parser_free($xml_parser);

print("\n");
print_r($stack);
print("\n");
?>
michelek 26-Oct-2003 07:42
its maybe not better, but me thinks its more stright-forward

--INPUT:

<?xml version="1.0" encoding="UTF-8"?>
<world>
    <country name="sweden">
        <city name="stockholm">
            <user>Adam</user>
            <user>Eva</user>
        </city>
        <city name="g?teborg">
            <user>God</user>
        </city>
    </country>
    <country name="usa">
        <city name="new york">
            <user>Clinton</user>
            <user>Bush</user>
        </city>
    </country>
</world>

--CODE:
<?
/**
 * m_i_h_k_e_l_AT_w_w_DOT_e_e
 * 26.10.2003
**/
$filename = "m.m.xml";
$xmlC = new XmlC();
$xml_data = file_get_contents( $filename );

$xmlC->Set_XML_data( $xml_data );

echo( "<pre>\n" );
print_r( $xmlC->obj_data );
echo( "</pre>\n" );

class XmlC
{
  var $xml_data;
  var $obj_data;
  var $pointer;

  function XmlC()
  {
  }

  function Set_xml_data( &$xml_data )
  {
    $this->index = 0;
    $this->pointer[] = &$this->obj_data;

    $this->xml_data = $xml_data;
    $this->xml_parser = xml_parser_create( "UTF-8" );

    xml_parser_set_option( $this->xml_parser, XML_OPTION_CASE_FOLDING, false );
    xml_set_object( $this->xml_parser, &$this );
    xml_set_element_handler( $this->xml_parser, "_startElement", "_endElement");
    xml_set_character_data_handler( $this->xml_parser, "_cData" );

    xml_parse( $this->xml_parser, $this->xml_data, true );
    xml_parser_free( $this->xml_parser );
  }

  function _startElement( $parser, $tag, $attributeList )
  {
    foreach( $attributeList as $name => $value )
    {
      $value = $this->_cleanString( $value );
      $object->$name = $value;
    }
    eval( "\$this->pointer[\$this->index]->" . $tag . "[] = \$object;" );
    eval( "\$size = sizeof( \$this->pointer[\$this->index]->" . $tag . " );" );
    eval( "\$this->pointer[] = &\$this->pointer[\$this->index]->" . $tag . "[\$size-1];" );

    $this->index++;
  }

  function _endElement( $parser, $tag )
  {
    array_pop( $this->pointer );
    $this->index--;
  }

  function _cData( $parser, $data )
  {
    if( trim( $data ) )
    {
      $this->pointer[$this->index] = trim( $data );
    }
  }

  function _cleanString( $string )
  {
    return utf8_decode( trim( $string ) );
  }

}
?>
Adam Tylmad 11-Sep-2003 10:11
I've created a parser that returns an
object based on a xml document.

example:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<country name="sweden">
    <city name="stockholm">
        <user>Adam</user>
        <user>Eve</user>
    </city>
    <city name="g?teborg">
        <user>God</user>
    </city>
</country>
<country name="usa">
    <city name="new york">
        <user>Clinton</user>
        <user>Bush</user>
    </city>
</country>

generates the following object structure:
[country] => Array
(
    [0] => stdClass Object
        (
            [name] => sweden
            [city] => Array
                (
                    [0] => stdClass Object
                        (
                            [name] => stockholm
                            [user] => Array
                                (
                                    [0] => Adam
                                    [1] => Eve
                                )
                        )
                    [1] => stdClass Object
                        (
                            [name] => g?teborg
                            [user] => God
                        )
                )
        )
    [1] => stdClass Object
        (
            [name] => usa
            [city] => stdClass
                (
                    [name] => new york
                    [user] => Array
                        (
                            [0] => Clinton
                            [1] => Bush
                        )
                )
        )
)

Here is the code:

class XMLParser {
    var $path;
    var $result;

    function XMLParser($encoding, $data) {
        $this->path = "\$this->result";
        $this->index = 0;

        $xml_parser = xml_parser_create($encoding);
        xml_set_object($xml_parser, &$this);
        xml_set_element_handler($xml_parser, 'startElement', 'endElement');
        xml_set_character_data_handler($xml_parser, 'characterData');

        xml_parse($xml_parser, $data, true);
        xml_parser_free($xml_parser);
    }

        function startElement($parser, $tag, $attributeList) {
            eval("\$vars = get_object_vars(".$this->path.");");
            $this->path .= "->".$tag;
            if ($vars and array_key_exists($tag, $vars)) {
                 eval("\$data = ".$this->path.";");
                     if (is_array($data)) {
                           $index = sizeof($data);
                           $this->path .= "[".$index."]";
                     } else if (is_object($data)) {
                           eval($this->path." = array(".$this->path.");");
                           $this->path .= "[1]";
                     }
            }
            eval($this->path." = null;");

            foreach($attributeList as $name => $value)
                eval($this->path."->".$name. " = '".XMLParser::cleanString($value)."';");
        }

    function endElement($parser, $tag) {
        $this->path = substr($this->path, 0, strrpos($this->path, "->"));
    }

    function characterData($parser, $data) {
        eval($this->path." = '".trim($data)."';");
    }
}

enjoy! And please make it better if you can ;-)


Create an XML parser with namespace support

  • ≪ xml_parse
  • xml_parser_create ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Create an XML parser with namespace support

xml_parser_create_ns

(PHP 4 >= 4.0.5, PHP 5, PHP 7)

xml_parser_create_ns — Create an XML parser with namespace support

설명

resource xml_parser_create_ns ([ string $encoding [, string $separator = ":" ]] )

xml_parser_create_ns() creates a new XML parser with XML namespace support and returns a resource handle referencing it to be used by the other XML functions.

인수

encoding

The optional encoding specifies the character encoding for the input/output in PHP 4. Starting from PHP 5, the input encoding is automatically detected, so that the encoding parameter specifies only the output encoding. In PHP 4, the default output encoding is the same as the input charset. In PHP 5.0.0 and 5.0.1, the default output charset is ISO-8859-1, while in PHP 5.0.2 and upper is UTF-8. The supported encodings are ISO-8859-1, UTF-8 and US-ASCII.

separator

With a namespace aware parser tag parameters passed to the various handler functions will consist of namespace and tag name separated by the string specified in separator.

반환값

Returns a resource handle for the new XML parser.

참고

  • xml_parser_create() - Create an XML parser
  • xml_parser_free() - Free an XML parser

User Contributed Notes

jonnyNO at SPAM dot sanriowasteland dot net 11-Jun-2002 02:49
This is from the Expat Function Reference by Clark Cooper, which is a reference to the C api.

"XML_Parser XML_ParserCreateNS(const XML_Char*encoding, XML_Char sep)
Constructs a new parser that has namespace processing in effect. Namespace expanded element names and attribute names are returned as a concatenation of the namespace URI, sep, and the local part of the name. This means that you should pick a character for sep that can't be part of a legal URI."

(from http://www.xml.com/pub/a/1999/09/expat/reference.html)

So thats what this function is for.  Now you know.


Create an XML parser

  • ≪ xml_parser_create_ns
  • xml_parser_free ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Create an XML parser

xml_parser_create

(PHP 4, PHP 5, PHP 7)

xml_parser_create — Create an XML parser

설명

resource xml_parser_create ([ string $encoding ] )

xml_parser_create() creates a new XML parser and returns a resource handle referencing it to be used by the other XML functions.

인수

encoding

The optional encoding specifies the character encoding for the input/output in PHP 4. Starting from PHP 5, the input encoding is automatically detected, so that the encoding parameter specifies only the output encoding. In PHP 4, the default output encoding is the same as the input charset. If empty string is passed, the parser attempts to identify which encoding the document is encoded in by looking at the heading 3 or 4 bytes. In PHP 5.0.0 and 5.0.1, the default output charset is ISO-8859-1, while in PHP 5.0.2 and upper is UTF-8. The supported encodings are ISO-8859-1, UTF-8 and US-ASCII.

반환값

Returns a resource handle for the new XML parser.

참고

  • xml_parser_create_ns() - Create an XML parser with namespace support
  • xml_parser_free() - Free an XML parser

User Contributed Notes

marek995 at seznam dot cz 24-Sep-2010 08:46
I created a function, which combines xml_paresr_create and all functions around.

<?php
function html_parse($file)
     {
      $array = str_split($file, 1);
      $count = false;
      $text = "";
      $end = false;
      foreach($array as $temp)
       {
        switch($temp)
         {
          case "<":
           between($text);
           $text = "";
           $count = true;
           $end = false;
           break;
          case ">":
           if($end == true) {end_tag($text);}
           else {start_tag($text);}
           $text = "";
           break;
          case "/":
           if($count == true) {$end = true;}
           else {$text = $text . "/";}
           break;
          default:
           $count = false;
           $text = $text . $temp;
         }
       }
     }
?>
The input value is a string.
It calls functions start_tag() , between() and end_tag() just like the original xml parser.

But it has a few differences:
  - It does NOT check the code. Just resends values to that three functions, no matter, if they are right
  - It works with parameters. For example: from tag <sth b="42"> sends sth b="42"
  - It works wit diacritics. The original parser sometimes wrapped the text before the first diacritics appearance.
  - Works with all encoding. If the input is UTF-8, the output will be UTF-8 too
  - It works with strings. Not with file pointers.
  - No "Reserved XML name" error
  - No doctype needed
  - It does not work with commentaries, notes, programming instructions etc. Just the tags

definition of the handling functions is:

<?php
function between($stuff) {}
?>

No other attributes
juanhdv at NOSPAM dot divvol dot org 05-Dec-2007 09:08
In PHP 5, when including in your xml file the definition '<?xml version="1.0" encoding="ISO-8859-1" ?>',   I'd also recommend adding the option below:

xml_parser_set_option($xml_parser,XML_OPTION_TARGET_ENCODING, "ISO-8859-1").

It works fine!

If your enconding is 'UTF-8', just replace 'ISO-8859-1'.
19-Apr-2006 04:42
I'd also recommend adding the option below
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
Tobbe 25-May-2005 03:01
The above "XML to array" code does not work properly if you have several tags on the same level and with the same name, example:

<currenterrors>
<error>
<description>This is a real error...</description>
</error>
<error>
<description>This is a second error...</description>
</error>
<error>
<description>Lots of errors today...</description>
</error>
<error>
<description>This is the last error...</description>
</error>
</currenterrors>

It will then only display the first <error>-tag.
In this case you will need to number the tags automatically or maybe have several arrays for each new element.
php at stock-consulting dot com 21-Feb-2005 11:47
Even though I passed "UTF-8" as encoding type PHP (Version 4.3.3) did *not* treat the input file as UTF-8. The input file was missing the BOM header bytes (which may indeed be omitted, according to RFC3629...but things are a bit unclear there. The RFC seems to make mere recommendations concering the BOM header). If you want to sure that PHP treats an UTF-8 encoded file correctly, make sure that it begins with the corresponding 3 byte BOM header (0xEF 0xBB 0xBF)
jcalvert at gmx dot net 03-Apr-2004 07:39
To maintain compatibility between PHP4 and PHP5 you should always pass a string argument to this function. PHP4 autodetects the format of the input if you leave it out whereas PHP5 will assume the format to be ISO-8859-1 (and choke on the byte order marker of UTF-8 files).

Calling the function as <?php $res = xml_parser_create('') ?> will cause both versions of PHP to autodetect the format.


Free an XML parser

  • ≪ xml_parser_create
  • xml_parser_get_option ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Free an XML parser

xml_parser_free

(PHP 4, PHP 5, PHP 7)

xml_parser_free — Free an XML parser

설명

bool xml_parser_free ( resource $parser )

Frees the given XML parser.

인수

parser
A reference to the XML parser to free.

반환값

This function returns FALSE if parser does not refer to a valid parser, or else it frees the parser and returns TRUE.

User Contributed Notes

nidzho 28-Aug-2016 05:12
Unset the parser after calling xml_parser_free() to prevent memory leaks:

<?php
xml_parser_free($parser);
unset($parser);
?>
dio at edu dot uni-klu dot ac dot at 01-Oct-2005 06:56
If you try to free an XML parser while it is still parsing, strange things will happen - your script will "hang" and no output will be sent to the browser. Consider this pseudo-code example:

-------

...
if (!xml_parse($parser)) echo 'XML error';

xml_parser_free($parser);

...

function SomeCallbackWhichWasSetBefore(...)
{
global $parser;

...

if (some_error_happened) xml_parser_free($parser);  //problem!

...

}

------

It would be logical that xml_parse would return false if the parser was freed while parsing, right? Wrong! Instead, everything hangs and no output will be sent out (no matter whether output buffering is on or not). It took me more than an hour to figure out why: you cannot free a parser handle that is currently parsing. A simple solution:

-------

$xml_error = false;
if (!xml_parse($parser))
echo 'XML error (directly from parser)';
else if ($xml_error)
echo 'XML error (from some callback function);

xml_parser_free($parser);

...

function SomeCallbackWhichWasSetBefore(...)
{
global $parser;
global $xml_error;

if ($xml_error)
return;

...

if (some_error_occured)
{
$xml_error = false;
return;
}

...

}

-------

If you use this solution you will have to check for $xml_error in every callback function. Essentially what you're doing is that, in case you want to stop parsing because of an error, you continue until xml_parse() is finished (without actually doing anything) and then free the parser and act on the error.

Of course the underlying problem is that you cannot stop a parser while it is parsing. There should be some function like xml_throw_error() or xml_parser_stop() or whatever, but unfortunately there isn't.
php dot net at gwprogramming dot com (George Webb) 31-Oct-2003 03:56
I found that with PHP 4.3.4RC1, if you don't call xml_parser_free() before your script ends, some sort of ugliness occurs with the webserver; i.e. the HTTP connection is closed.  (The apache error_log says "exit signal Segmentation fault (11)".)

Usually PHP tends to clean up connections you don't explicitly close (e.g. database connections), but in this case apparently it doesn't.

Some web browsers (MSIE for one) do not actually show this problem, so you may not actually notice it.  Opera 7.11 does show it, which is how I discovered it.

So don't forget to call xml_parser_free() ... always!


Get options from an XML parser

  • ≪ xml_parser_free
  • xml_parser_set_option ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Get options from an XML parser

xml_parser_get_option

(PHP 4, PHP 5, PHP 7)

xml_parser_get_option — Get options from an XML parser

설명

mixed xml_parser_get_option ( resource $parser , int $option )

Gets an option value from an XML parser.

인수

parser
A reference to the XML parser to get an option from.
option
Which option to fetch. XML_OPTION_CASE_FOLDING and XML_OPTION_TARGET_ENCODING are available. See xml_parser_set_option() for their description.

반환값

This function returns FALSE if parser does not refer to a valid parser or if option isn't valid (generates also a E_WARNING). Else the option's value is returned.

User Contributed Notes

There are no user contributed notes for this page.


Set options in an XML parser

  • ≪ xml_parser_get_option
  • xml_set_character_data_handler ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Set options in an XML parser

xml_parser_set_option

(PHP 4, PHP 5, PHP 7)

xml_parser_set_option — Set options in an XML parser

설명

bool xml_parser_set_option ( resource $parser , int $option , mixed $value )

Sets an option in an XML parser.

인수

parser

A reference to the XML parser to set an option in.

option

Which option to set. See below.

The following options are available:

XML parser options
Option constant | Data type | Description
XML_OPTION_CASE_FOLDING | integer | Controls whether case-folding is enabled for this XML parser. Enabled by default.
XML_OPTION_SKIP_TAGSTART | integer | Specify how many characters should be skipped in the beginning of a tag name.
XML_OPTION_SKIP_WHITE | integer | Whether to skip values consisting of whitespace characters.
XML_OPTION_TARGET_ENCODING | string | Sets which target encoding to use in this XML parser.By default, it is set to the same as the source encoding used by xml_parser_create(). Supported target encodings are ISO-8859-1, US-ASCII and UTF-8.

value

The option's new value.

반환값

This function returns FALSE if parser does not refer to a valid parser, or if the option could not be set. Else the option is set and TRUE is returned.

User Contributed Notes

www.thomaskoch.it 31-Jul-2008 11:22
The option XML_OPTION_SKIP_WHITE has no effect in my PHP 5.2.6 (with expat-1.95.8-5). To skip cdata composed of white space only, simply check for that at the beginning of your cdata callback function:

<?php
function callback_cdata($parser, $cdata)
{
 if(!trim($cdata))
  return;

 // ... continue processing ...
}
?>
maschoen at pobox dot com 11-Oct-2007 09:19
I'm a little confused about using xml_parser_set_option and xml_parser_get_option with XML_OPTION_SKIP_WHITE and XML_OPTION_SKIP_TAGSTART.

Looking at the source code, ...set_option() accepts both ...SKIP_WHITE and ...SKIP_TAGSTART, and sets a program variables.  On the other hand, ...get_option() returns an error for both.   I'm not sure of the code actually uses the variables properly, although it does reference the set variables.  This does look like an oversite.
j[no_spam_please] at [thx]jessepearson dot net 09-Mar-2007 05:08
In the function below, you need to update two lines if you don't want php to throw warnings.

change these two:
           $elements[$index]['attributes'] = $tag['attributes'];
           $elements[$index]['content'] = $tag['value'];

to this:
      $elements[$index]['attributes'] = empty($tag['attributes']) ? "" : $tag['attributes'];
      $elements[$index]['content']    = empty($tag['value']) ? "" : $tag['value'];
06-Sep-2006 04:56
A little mod to a function posted here...

function xml_to_array( $file )
{
    $parser = xml_parser_create();
    xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
    xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
    xml_parse_into_struct( $parser, file_get_contents($file), $tags );
    xml_parser_free( $parser );

    $elements = array();
    $stack = array();
    foreach ( $tags as $tag )
    {
        $index = count( $elements );
        if ( $tag['type'] == "complete" || $tag['type'] == "open" )
        {
            $elements[$index] = array();
            $elements[$index]['name'] = $tag['tag'];
            $elements[$index]['attributes'] = $tag['attributes'];
            $elements[$index]['content'] = $tag['value'];

            if ( $tag['type'] == "open" )
            {    # push
                $elements[$index]['children'] = array();
                $stack[count($stack)] = &$elements;
                $elements = &$elements[$index]['children'];
            }
        }

        if ( $tag['type'] == "close" )
        {    # pop
            $elements = &$stack[count($stack) - 1];
            unset($stack[count($stack) - 1]);
        }
    }
    return $elements[0];
}
ksavage at achaean dot com 28-Mar-2005 10:25
Had one heck of a time getting curl to send my XML request.  Tried a lot of different things, FINALLY ended up with this.

Im making a curl request to paymentech / Orbital 's payment gateway.   Essentially, I took the same header i made for the fsockopen() request, and used it for this curl request. (Hostinc company didnt allow fsockopen().

You'll note that this specific gateway requires a custom content type header.  Thats what gave me the most trouble.  When using the CURLOPT_HTTPHEADER, and CURLOPT_POSTFIELDS together, it doesnt matter, it sends the default post header because of CURLOPT_POSTFIELDS.

<?php

$url = "https://example.com";
$page = "/proccessing_page.cgi";

$post_string = "<YourXML>All XML stuff Here</YourXML>";

       $header  = "POST ".$page." HTTP/1.0 \r\n";
        $header .= "MIME-Version: 1.0 \r\n";
        $header .= "Content-type: application/PTI26 \r\n";
        $header .= "Content-length: ".strlen($post_string)." \r\n";
        $header .= "Content-transfer-encoding: text \r\n";
        $header .= "Request-number: 1 \r\n";
        $header .= "Document-type: Request \r\n";
        $header .= "Interface-Version: Test 1.4 \r\n";
        $header .= "Connection: close \r\n\r\n";
        $header .= $post_string;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 4);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);

        $data = curl_exec($ch);         if (curl_errno($ch)) {
           print curl_error($ch);
        } else {
           curl_close($ch);
        }

// use XML Parser on $data, and your set!

        $xml_parser = xml_parser_create();
        xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
        xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
        xml_parse_into_struct($xml_parser, $data, $vals, $index);
        xml_parser_free($xml_parser);

// $vals = array of XML tags.  Go get em!

?>

Worked like a dream for me.  Enjoy!
matt at mcbridematt dot dhs dot org 16-Dec-2002 02:56
Re: anony at mous dot com
case folding is a violation of the XHTML specification. xhtml is all in lowercase to be inline with the DOM
pupeno at pupeno dot com 16-Nov-2002 10:14
XML is case sensitive, then, from my point of view, disabling case folding doesn't goes against xml 1.0 specifications but the contrary, disabling case folding allow us to distiguish between diferent cases of the same letter ('a' and 'A') which of XML are two diferent things.
From my point of view, disabling case folding is a good practice and I think it should be disabled by default.
More information on:
http://www.isacat.net/2001/xml/case.htm
and
http://www.w3.org/TR/REC-xml
Thank you.
ta at NOSPAM dot magicsquare dot info 09-Jul-2002 01:52
XML_OPTION_SKIP_WHITE works for me (linux, php 4.2.1)

phpinfo says :

XML Support active
XML Namespace Support active
EXPAT Version expat_1.95.2

i guess it depends on expat version/presence
chris at wildcharacters dot com 22-Oct-2001 04:32
The two 'skip' tags are not available to windows users using PHP 4.0.4.  The following:

$skipWhite = xml_parser_get_option ($xmlParser,XML_OPTION_SKIP_WHITE);
$skipTagStart = xml_parser_get_option ($xmlParser,XML_OPTION_SKIP_TAGSTART);

generates the following errors:
Warning: xml_parser_get_option: unknown option in c:/program files/abria merlin/apache/htdocs/xml/sax_test.php on line 230
anony at mous dot com 20-Apr-2000 02:26
Disable case folding or your code will be violating the XML 1.0 specification.


Set up character data handler

  • ≪ xml_parser_set_option
  • xml_set_default_handler ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Set up character data handler

xml_set_character_data_handler

(PHP 4, PHP 5, PHP 7)

xml_set_character_data_handler — Set up character data handler

설명

bool xml_set_character_data_handler ( resource $parser , callable $handler )

Sets the character data handler function for the XML parser parser.

인수

parser

A reference to the XML parser to set up character data handler function.

handler

handler is a string containing the name of a function that must exist when xml_parse() is called for parser.

The function named by handler must accept two parameters:

handler ( resource $parser , string $data )
parser
The first parameter, parser, is a reference to the XML parser calling the handler.
data
The second parameter, data, contains the character data as a string.

Character data handler is called for every piece of a text in the XML document. It can be called multiple times inside each fragment (e.g. for non-ASCII strings).

If a handler function is set to an empty string, or FALSE, the handler in question is disabled.

Note: 함수 이름 대신에, 객체의 참조와 메쏘드 이름을 가지는 배열을 사용할 수 있습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

svanegmond at tinyplanet dot ca 08-Oct-2010 09:06
For those who are having their character entities eaten by this function (e.g. &amp; &lt; and so forth):

You will notice that your callback gets called several times.

input: Give me some beans &amp; rice. &#032;

call 1: Give me some beans
call 2: amp; and rice
call 3: #032;

There is an obvious workaround. This is not perfect:

<?php
        $text = preg_replace('/^([a-z]+;)/','&\1', $text);
        $text = preg_replace('/^(#[0-9]+;)/', '&\1', $text);
?>
jhill at live dot com 29-Aug-2008 04:11
To detect that concatenation of data is taking place, you can keep track of whether the last function call was to the data processing function.
e.g. using $this->inside_data variable below:

<?php
xml_set_element_handler($this->parser, "start_tag", "end_tag");
xml_set_character_data_handler($this->parser, "contents");

protected function contents($parser, $data)
{
    switch ($this->current_tag) {
            case "name":
                if ($this->inside_data)
                    $this->name .= $data; // need to concatenate data
                else
                    $this->name = $data;
                break;
         ...
    }
    $this->inside_data = true;
}

protected function start_tag($parser, $name)
{
    $this->current_tag = $name;
    $this->inside_data = false;
}

protected function end_tag() {
    $this->current_tag = '';
    $this->inside_data = false;
}
?>
unspammable-iain at iaindooley dot com 03-Jan-2006 10:41
re: jason at omegavortex dot com below, another way to deal with whitespace issues is:

        function charData($parser,$data)
        {
            $char_data = trim($data);

            if($char_data)
                $char_data = preg_replace('/  */',' ',$data);

            $this->cdata .= $char_data;
        }

This means that:

    <p>here is my text <a href="something">my text</a>
    and here is some more after some spaces at the
    beginning of the line</p>

comes out properly. You could do further replacements if you want to deal with tabs in your files. i only ever use spaces. if you only use trim() then you would lose the space before the <a> tag above, but trim() is a good way to check for completely empty char data, then just replace more than one space with a single space. this will preserve a single space at the beginning and end of the cdata.
jason at omegavortex dot com 07-Dec-2005 08:00
I don't believe the problem has been addressed, but if you're parsing an XML file and run into the line break (or tab) problem I believe this function may help:

if (!preg_match("/((\r|)\n)/i", $data) || preg_match("/\\t+/i", $data)) {
(Code Here)
}
wako057 at gmx dot fr 22-Aug-2005 06:39
That give an example about the note.
When you are creating a class and you need to use a method of your class to the function xml_set_character_data_handler that is the way to point give the method:

class example
{
   function example()
   {
   }

  function data($parseur, $texte)
  {
      switch ($this->derniereBaliseRencontree)
      {
         case "TITLE":
                     -----
          break;
         case "LINK"
                    -----
         break;
        case "DESCRIPTION":
                    -----
        break;
     }
  }

  function fetchOpen($parseur, $nomBalise, $tableauAttributs)
  {
     ----------------
  }

  function fetchClose($parseur, $nomBalise)
  {
     ----------------
  }

  function ZZZ()
  {
      $parseurXML = xml_parser_create();
       xml_set_element_handler($parseurXML, Array(&$this, 'fetchOpen'),   Array(&$this, 'fetchClose'));
         ---------------- <IMPORTANT> --------------------------
       xml_set_character_data_handler($parseurXML, Array(&$this, 'data'));
        ---------------- </IMPORTANT> --------------------------
}
ben at removethis emediastudios dotcom 06-Aug-2005 12:53
I too love the undocumented "splitting" functionality :-p.

Rather than concatinating the data based on whether or not the current tag name has changed from the previous tag name I suggest always concatinating like the following with the $catData variable being unset in the endElement function:

<?php

function endElement ($parser, $data) {
  global $catData;

  // Because we are at an element end we know any splitting is finished
  unset($GLOBALS['catData']);
}

function characterData ($parser, $data) {
  global $catData;

  // Concatinate data in case splitting is taking place
  $catData.=$data;

}

?>

This got me around a problem with data like the following where, because characterData is not called for empty tags, the previous and current tag names were the same even though splitting was not taking place.

<companydept>
<companydeptID></companydeptID>
<companyID>1</companyID>
<companydeptName></companydeptName>
</companydept>
<companydept>
<companydeptID></companydeptID>
<companyID>2</companyID>
<companydeptName></companydeptName>
</companydept>
<companydept>
<companydeptID></companydeptID>
<companyID>3</companyID>
<companydeptName></companydeptName>
</companydept>
yaroukh at email dot cz 25-Apr-2005 05:51
It would be nice if someone could complete documentation of this function. I think that the "splitting" behaviour should (at least) be mentioned within the documentation, if not explained (please!). I'm not quite sure whether the cut comes after each 1024bytes/chars of data.

My experience looks as follows:
[xmlFile]
...
    <label>slo|?ka</label>
    <comment>koment|?&#345; slo?ky</comment>
...
[/xmlFile]
(Places where the character-data got splitted are marked with pipes. Plus there was latin small letter 'r' with caron instead of &#345;.)

Since the splitting is not mentioned in documentation one could assume that it is a bug; especially when you work with UTF-8 and the cuts come right before some special characters.
(Should the concatenating of $cData be considered to be the proper & 'final' way of processing character-data?)

Also I'd suggest to add another line in "Description" when fc has an alternate usage (instead of hiding it within the "Note" :o); in this particular case I'd prefer this:

Description:
bool xml_set_character_data_handler ( resource parser, callback handler )
bool xml_set_character_data_handler ( resource parser, object reference, method name )

... there are dozens of functions ofcourse where documentation works this way (I mean not mentioning the alternate usage in the "Description" part).

Have a nice day
  Yaroukh
flobee 13-Feb-2005 06:53
re. to Philippe Marc , and  karuna_gadde examples

i found out that the xml_set_character_data_handler call back  function can be called more often for the same element in particular the content is just a few chars long (happen on windows)

so a check up can give you the answer an may be for long strings too.
eg:
<?php
xml_set_character_data_handler($this->parser, "cdata");
//...
function cdata($parser, $cdata) {
// ...
if(isset($this->data[$this->currentItem][$this->currentField])) {
    $this->data[$this->currentItem][$this->currentField] .= $cdata;
} else {
    $this->data[$this->currentItem][$this->currentField] = $cdata;
}
?>
Philippe Marc 28-Oct-2004 04:21
How to overide the 1024 characters limitation of xml_set_character_data_handler.
Took me some time to find out how to deal with that!

When calling a basic XML parser:
$parseurXML = xml_parser_create();
xml_set_element_handler($parseurXML, "opentagfunction", "closetagfunction");
xml_set_character_data_handler($parseurXML, "textfunction");

The textfunction only receive 1024 characters at once, even if the text is 4000 characters long. In facts, the parser seems to split the data in pieces of 1024 characters. The way to handle that is to concatenate them.

example:
If you have an XML tag called UNIPROT_ABSTRACT containing a 4000 characters protein description:
function textfunction($parser, $text)
    {
     if ($last_tag_read=='UNIPROT_ABSTRACT') $uniprot.=$text;
    }
The function is called 4 times and receives 1024+1024+1024+928 characters that will be concatenated in the $uniprot variable using the ".=" concatenation fonction.

Easy to do, but not documented!
Brad dot Harrison at griffith dot edu dot au 12-Jul-2004 07:23
If you need to trim the white space for HTML code and don't rely on spaces for formatting text (if you are then it is time to use Style Sheets) then this code will come in very useful.

 $data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
 $data=eregi_replace(">"."[[:space:]]+",">",$data);
 $data=eregi_replace("[[:space:]]+"."<","<",$data);
karuna_gadde at yahoo dot com 27-May-2004 04:01
<?
class Parser {

var $att;
var $id;
var $title;
var $content;
var $index=-1;
var $xml_parser;
var $tagname;

function parser()
{
$file = "data.xml";
$this->xml_parser = xml_parser_create();
xml_set_object($this->xml_parser,$this);
xml_set_element_handler($this->xml_parser, "startElement", "endElement");
xml_set_character_data_handler($this->xml_parser, 'elementContent');
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    $data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
if (!xml_parse($this->xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser)));
}
}
xml_parser_free($this->xml_parser);
}

function startElement($parser, $name, $attrs) {
if (($name=="TREE") or ($name=="NODE") or ($name=="LEAFNODE"))
    {
         $this->index++;
         $this->att[$this->index]=$name;
    }
    $this->tagname=$name;
}

function elementContent($parser, $data) {
switch ($this->tagname)
{
     case 'ID':
                 $this->id[$this->index]=trim($data);
                 break;
     case 'TITLE' :
                 $this->title[$this->index]=trim($data);
                 //echo $data;
                 break;
     case 'CONTENT'    :
                 $this->content[$this->index]=trim($data);
                 break;

}
}
function endElement($parser, $name){
$this->tagname=="";
}
}
?>

I thought this class is more help full to know about xml_parser with no white spaces
michael dot stilson at wmich dot edu 04-Mar-2004 09:31
Hello,

This is an addition to the note posted by:
wiart at yahoo dot com
22-Aug-2003 05:31
Which is located below.

I had similar problems manually creating XML docs and adding new-lines within my node data, e.g.

<root>
    ...
    <node attribute="something">
        Here is some data. There is a lot of data, and I want to
        be able to read the data from a terminal window, so I add
        newlines to fit everything within 80 columns.
    </node>
    ...
</root>

So, given the above example, my data handler gets called 3 times and the result left in my variable is:

"newlines to fit everything within 80 columns."

Instead of all of the data within "node", which I was expecting.

By using the concatenation operator; however, as suggested by the mentioned note, I was able to get what I needed. Which is of course:

"Here is some data. There is a lot of data, and I want to be able to read the data from a terminal window, so I add newlines to fit everything within 80 columns."
dan30odd08 at hotmail dot com 02-Dec-2003 01:38
I just want to mention that i ran into a problem when parsing an xml file using the character data handler. If you happen to have a string which is also an internal php function stored in your xml data file and you want to output it as a string the parser dosent seem to recognize it.
   I found a way around this problem. In my case i was storing a string with the value read. This would not allow me to output the data so to work around this problem i added a backslash for every character in the data element.

   e.g.      <xml>
    from    <element>read</element>
    to       <element>////read</element>

i dont know if anyone has ran into this problem or not but i thought i would just put it here just so in case someone is getting stuck with this.
wiart at yahoo dot com 22-Aug-2003 02:31
WARNING !!!
Always use concatenation for getting the content of a XML tag when you write the function that will deal with the Character Data handling

Example:

$mycontent = '';
$xml_parser = xml_parser_create ();
xml_set_character_data_handler($xml_parser, "_characterdata");

function _characterdata($parser, $data){
  global $mycontent;
  //HERE: use .= and not =
  $mycontent .= $data;
}

...

while ($data = fread($fp, 4096)) {
    xml_parse($xml_parser, $data, feof($fp));
}

I had the following problem with the use of '=$data' :

In one of my XML documents, the parsing stopped in the middle of a character data :

In original document:
<prop name="nbres">100</prop>

What happened:
<prop name="nbres">10[parsing stopped here in chunck of 4096bytes]0</prop>

As I did not use the concatenation, when I displayed the value of the 'nbres' property, the value was 0 instead of 100 because the first time the function characterdata was called :
$mycontent = 10;

and the second time:
$mycontent = 0;      //!!!!!!!!!!!!! HO HO !! some problem occured ...

Instead of $mycontent = 100;

CONCLUSION:
IN YOUR CHARACTER DATA HANDLER FUNCTIONS, NEVER FORGET THE . (concatenation operator) !!!!! IF YOU DON'T WANT TO HAVE WEIRD BUGS SOME TIMES
morris_hirsch at hotmail dot com 18-Aug-2003 06:15
Thanks to Christian Stocker for clearing up my entity issues,  where some entities are parsed correctly and others not.

The problem is the ''wide'' entities that have a large numeric code simply can not fit in a single byte, which is the default encoding for both source input to the parser and data output from the parser.  So the parser puts out a ''?'' to say it could not store the code value. One could argue that if the input has a &1234; the output should simply copy it as &1234; instead of the ''?'' but that would still mean the  parser behaves two different ways according to the code values, and anyway they don't do it.

So, we need utf8 encoding for the output, and the slightly not obvious way to say so is

  $xml_parser = xml_parser_create ("UTF-8");

which means BOTH source input and data output are utf8.
Remember that utf8 is a superset of basic ASCII but not of extended ASCII, so your input can contain e.g. &eacute;
spelled out, but a native eacute character is wrong here.
Just utf8_encode your input to be sure.

That should do it, and thanks again for the help.
morris_hirsch at hotmail dot com 06-Aug-2003 09:23
Here are two ways to deal with named entities in the XML.

1. Put a list of named entities at the front so the parser knows what they all mean

  $decl = '<!DOCTYPE rootname [

<!ENTITY frac12  "&#x00BD;" >
<!ENTITY frac14  "&#x00BC;" >
<!ENTITY frac34  "&#x00BE;" >
<!ENTITY frac18  "&#x215B;" >
<!ENTITY frac38  "&#x215C;" >
<!ENTITY frac58  "&#x215D;" >
<!ENTITY frac78  "&#x215E;" >
<!ENTITY frac13  "&#x2153;" >
<!ENTITY frac23  "&#x2154;" >
<!ENTITY frac15  "&#x2155;" >
<!ENTITY frac25  "&#x2156;" >
<!ENTITY frac35  "&#x2157;" >
<!ENTITY frac45  "&#x2158;" >
<!ENTITY frac16  "&#x2159;" >
<!ENTITY frac56  "&#x215A;" >

<!ENTITY mdash  "&#x2014;" >

 ..... lots of others

<!ENTITY uuml    "&#252;" >
<!ENTITY yacute  "&#253;" >
<!ENTITY thorn   "&#254;" >
<!ENTITY yuml    "&#255;" >

]>';

$parseThis = $decl . "<rootname> ..... </rootname>";

This works fine for all the single-byte (European) codes,
but not the wide codes like emdash or frac18.
They seem to be trashed when the character data handler gets them.  They all echo as "?"

There may be a way to make them work, but until I find it,
or a newer release takes care of it, here is a work-around that does work.

2. Rewrite every ampersand as &amp; in the input stream

 $ampXML = str_replace ("&", "&amp;", $sourceXML);

Now the parser will see &amp;foo; instead of &foo;

It does not try to decode it, so wide (two byte) values are not a problem, and no list of names is needed.

When you write that to your output you have &foo;

which is usually fine for the next stage of your process.
www.PhoC.de 20-May-2003 04:39
In some cases it's better to avoid storing data which is not needed. In these cases the function

chop()  ( => Alias of function rtrim() )

could be usefull to prevent data like

"\t"
"\r"
"\o"
"\n"
"\x0B"
" "

to be stored in a array or a string or something like this.

Example: (storing data in a string)
________________
$filename="xyz.xml";

if(!($fp=fopen($filename,"r"))){
   die("Cannot open $filename ");
}

while (!feof($fp))
{
   $data .= chop(fgets($fp, 4096));
}

fclose($fp);
________________

Have a look at the documentation of rtrim() http://www.php.net/manual/en/function.rtrim.php.

Greetz
PhoC
waldo at wh-e dot com 07-Mar-2002 06:52
Here's a way to strip all the spaces between tags in an xml document.

//strip white space between tags
$data=eregi_replace(">"."[[:space:]]+"."<","><",$data);

So this:

<house>
  <garage>
    <car>Waldo</car>
  </garage>
</house>

would be changed to:

<house><garage><car>Waldo</car></garage></house>

It was useful to me. Maybe you too?
ken at positive-edge dot com 29-Jan-2002 05:20
the function handler is called several times when it parses the character data.  It doesn't return the entire string as it suggests.  There are special exceptions that will always force the parser to stop scanning and call the character data handler.  This is when:

- The parser runs into an Entity Declaration, such as &amp; (&) or &apos; (?)
- The parser finishes parsing an entity
- The parser runs into the new-line character (\n)
- The parser runs into a series of tab characters (\t)

And perhaps others.

For instance, if we have this xml content:

<mytag name=?Ken Egervari? title=?Chief Technology Officer?>
    Ken has been Positive Edge&apos;s Chief Technology Officer for 2 years.
</mytag>

The parser will call the character data handler 6 times.  This is what will happen:

1    \n
2    \t
3    Ken has been Positive Edge
4    ?
5    s Chief Technology Officer for 2 years.
6    \n

I hope that helps people out.
mogmios at mlug dot missouri dot edu 24-Oct-2001 01:24
If you want to strip unwanted whitespace in an HTML-like manner then there are two steps.

The first you need to strip consecutive whitespaces from all you input data like this:  $data = eregi_replace ( "[[:space:]]+", " ", $data );

Then in your cdata handler make a check to see if it's blank space. An easy way is like this: if ( trim ( $cdata ) ) { work on cdata }

That should take care of any whitespace issues you might have.
annies at cs dot tu-berlin dot de 03-Aug-2001 07:43
The maximum size of the second parmeter $data seems to be 1024. Is there more character data  in the XML text the handler is called again immediately with the next portion.


Set up default handler

  • ≪ xml_set_character_data_handler
  • xml_set_element_handler ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Set up default handler

xml_set_default_handler

(PHP 4, PHP 5, PHP 7)

xml_set_default_handler — Set up default handler

설명

bool xml_set_default_handler ( resource $parser , callable $handler )

Sets the default handler function for the XML parser parser.

인수

parser

A reference to the XML parser to set up default handler function.

handler

handler is a string containing the name of a function that must exist when xml_parse() is called for parser.

The function named by handler must accept two parameters:

handler ( resource $parser , string $data )
parser
The first parameter, parser, is a reference to the XML parser calling the handler.
data
The second parameter, data, contains the character data.This may be the XML declaration, document type declaration, entities or other data for which no other handler exists.

If a handler function is set to an empty string, or FALSE, the handler in question is disabled.

Note: 함수 이름 대신에, 객체의 참조와 메쏘드 이름을 가지는 배열을 사용할 수 있습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

anoril at anoril dot com 13-Jul-2006 04:54
I have the same issue using two installation of PHP5: on accepts to use the default handler while the other only uses the character_data one.

Maybe a configuration problem...

;) Nonor.
phillip 26-Apr-2005 11:13
it seems to me that in PHP5 the function defined as default-handler (using xml_set_default_handler()) doesen't get passed the cdata anymore:

i.e.:
xml_set_element_handler($this->parser, 'parseSTART', 'parseEND');
xml_set_default_handler($this->parser, 'parseDEFAULT');
function parseSTART() { ... }
function parseEND() { ... }
function parseDEFAULT() { ... }

under PHP5, parseDEFAULT will NOT get passed any cdata, but unter PHP4 it will. at least that's my take on the strange stuff (not) happening after migrating to PHP5.

my solution was to add a xml_set_character_data_handler($parser, 'parseDEFAULT'). it worked for me.


Set up start and end element handlers

  • ≪ xml_set_default_handler
  • xml_set_end_namespace_decl_handler ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Set up start and end element handlers

xml_set_element_handler

(PHP 4, PHP 5, PHP 7)

xml_set_element_handler — Set up start and end element handlers

설명

bool xml_set_element_handler ( resource $parser , callable $start_element_handler , callable $end_element_handler )

Sets the element handler functions for the XML parser. start_element_handler and end_element_handler are strings containing the names of functions that must exist when xml_parse() is called for parser.

인수

parser

A reference to the XML parser to set up start and end element handler functions.

start_element_handler

The function named by start_element_handler must accept three parameters:

start_element_handler ( resource $parser , string $name , array $attribs )
parser
The first parameter, parser, is a reference to the XML parser calling the handler.
name
The second parameter, name, contains the name of the element for which this handler is called.If case-folding is in effect for this parser, the element name will be in uppercase letters.
attribs
The third parameter, attribs, contains an associative array with the element's attributes (if any).The keys of this array are the attribute names, the values are the attribute values.Attribute names are case-folded on the same criteria as element names.Attribute values are not case-folded. The original order of the attributes can be retrieved by walking through attribs the normal way, using each().The first key in the array was the first attribute, and so on.

Note: 함수 이름 대신에, 객체의 참조와 메쏘드 이름을 가지는 배열을 사용할 수 있습니다.

end_element_handler

The function named by end_element_handler must accept two parameters:

end_element_handler ( resource $parser , string $name )
parser
The first parameter, parser, is a reference to the XML parser calling the handler.
name
The second parameter, name, contains the name of the element for which this handler is called.If case-folding is in effect for this parser, the element name will be in uppercase letters.

If a handler function is set to an empty string, or FALSE, the handler in question is disabled.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

kok at nachon dot nl 26-Sep-2008 03:44
Here is another example of detecting empty elements. I works with libxml2. Note that it handles buffer boundaries.

<?php

$depth = 0; //current depth, used for pretty printing
$empty = false; //whether the tag is empty
$offset = 0; //the index of the start of the current buffer within the stream

function tagStart($parser, $name, array $attribs) {
    global $depth, $empty, $data, $offset, $lastchar;
    $idx = xml_get_current_byte_index($parser);
    /* xml_get_current_byte_index returns index within the streams and not
       within the buffer.*/

    /* Check if the index is within the buffer. */
    if (isset($data[$idx - $offset])) {
        $c = $data[$idx - $offset];
    } else {
        /* If it isn't simple use the last character of the buffer. */
        $c = $lastchar;
    }
    $empty = $c == '/';
    echo str_repeat("\t", $depth), "<$name", ($empty ? '/>' : '>'), "\n";
    if (!$empty) ++$depth;
}

function tagEnd($parser, $name) {
    global $depth, $empty;
    if (!$empty) {
        --$depth;
        echo str_repeat("\t", $depth), "</$name>\n";
    } else {
        $empty = false;
    }
}

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($parser, 'tagStart', 'tagEnd');

$data1 = '
<test>
    <empty att="3" />
    <nocontent></nocontent>
    <content>
        <empty/>
        <empty/>
    </content>
    <empty/';

$data2 = '>
    <empty att="5" />
</test>
';

$data = &$data1;
$length = strlen($data1);
$lastchar = $data[$length-1];
xml_parse($parser, $data1);
$offset .= $length;
$data = &$data2;
xml_parse($parser, $data2);
darien at etelos dot com 12-Feb-2007 07:48
This documentation is somewhat awry. I know it's been said many times before, but it bears repeating...

If using PHP4, you may be required to use xml_set_object() instead of calling any of the xml_set_*_handler() functions with a two-item array. It will work fine on PHP5, but move the same code to PHP4 and it will create one copie of $this (even if you use &$this) for each handler you set!

<?php
// This code will fail mysteriously on PHP4.
$this->parser = xml_parser_create();
xml_set_element_handler(
            $this->parser,
            array(&$this,"start_tag"),
            array(&$this,"end_tag")
        );
        xml_set_character_data_handler(
            $this->parser,
            array(&$this,"tag_data")
        );
?>

<?php
// This code will work on PHP4.
$this->parser = xml_parser_create();
xml_set_object($this->parser,&$this);
xml_set_element_handler(
            $this->parser,
            "start_tag",
            "end_tag"
        );
        xml_set_character_data_handler(
            $this->parser,
            "tag_data"
        );
?>
lloeki at gmail dot com 31-Oct-2006 12:07
I modified the previous script, so that it is associative. I find it more useful that way. BTW I prefer strtolower() things, but that's not mandatory at all.

<?php

$file = "data.xml";
$depth = 0;
$tree = array();
$tree['name'] = "root";
$stack[] = &$tree;

function startElement($parser, $name, $attrs) {
   global $depth;
   global $stack;
   global $tree;

   $element = array();
   foreach ($attrs as $key => $value) {
       $element[strtolower($key)]=$value;
   }

   end($stack);
   $stack[key($stack)][strtolower($name)] = &$element;
   $stack[strtolower($name)] = &$element;

   $depth++;
}

function endElement($parser, $name) {
   global $depth;
   global $stack;

   array_pop($stack);
   $depth--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
   die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
   if (!xml_parse($xml_parser, $data, feof($fp))) {
       die(sprintf("XML error: %s at line %d",
                   xml_error_string(xml_get_error_code($xml_parser)),
                   xml_get_current_line_number($xml_parser)));
   }
}
xml_parser_free($xml_parser);
$tree = end(end($stack));
echo "<pre>";
print_r($tree);
echo "</pre>";

?>
rubentrancoso at gmail dot com 29-Aug-2006 10:44
My 25 cents. This example show how to parse a XML in a associative array tree.

<?php

$file = "flow/flow.xml";
$depth = 0;
$tree = array();
$tree['name'] = "root";
$stack[count($stack)] = &$tree;

function startElement($parser, $name, $attrs) {
   global $depth;
   global $stack;
   global $tree;

   $element = array();
   $element['name'] = $name;
   foreach ($attrs as $key => $value) {
        //echo $key."=".$value;
        $element[$key]=$value;
    }

   $last = &$stack[count($stack)-1];
   $last[count($last)-1] = &$element;
   $stack[count($stack)] = &$element;

   $depth++;
}

function endElement($parser, $name) {
   global $depth;
   global $stack;

   array_pop($stack);
   $depth--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
   die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
   if (!xml_parse($xml_parser, $data, feof($fp))) {
       die(sprintf("XML error: %s at line %d",
                   xml_error_string(xml_get_error_code($xml_parser)),
                   xml_get_current_line_number($xml_parser)));
   }
}
xml_parser_free($xml_parser);
$tree = $stack[0][0];
echo "<pre>";
print_r($tree);
echo "</pre>";
redb 15-May-2006 02:48
Example below (BadParser) works fine with some changes.

xml_set_element_handler ( $parser, array ( &$this, 'tagStart' ), array ( &$this, 'tagEnd' ) );
xml_set_character_data_handler ( $parser, array ( &$this, 'tagContent' ) );
vladimir-leontiev at uiowa dot edu 13-Dec-2005 06:44
It seems that characterData() gets characters in chuncks of 1024; therefore if you have string of characters between you tags that is longer than 1024 then characterData() will be called more that once for single pair of tags. I don't know if this feature(bug?) is documented anywhere, I just wanted to warn everyone about this; it had tripped me. I use php 4.3.10 on Linux.
hendra_g at hotmail dot com 19-Nov-2005 05:10
I ran into the same problem with 'ibjoel at hotmail dot com' in regards to self-closing tags, and found that the script that he/she wrote did not work as I expected.
I played around with some of php's functions and examples and compiled something, which may not be the neatest solution, but it works for the data that 'ibjoel at hotmail dot com' provided.
The data needs to be read from a file though, so the fp can be utilised. It still uses the xml_get_current_byte_index(resource parser) trick, but this time, I check for the last 2 character before the index and test if it's "/>".

<?php
/* myxmltest.xml:
<normal_tag>
  <self_close_tag />
     data
  <normal_tag>data
     <self_close_tag attr="value" />
  </normal_tag>
     data
  <normal_tag></normal_tag>
</normal_tag>
*/

//## Global Variables ##//
$file = "myxmltest.xml";
$character_data_on = false;
$tag_complete = true;

function startElement($parser, $name, $attrs)
{
    global $character_data_on;
    global $tag_complete;

    echo "&lt;<font color=\"#0000cc\">$name</font>";
    //## Print the attributes ##//
    if (sizeof($attrs)) {
        while (list($k, $v) = each($attrs)) {
            echo " <font color=\"#009900\">$k</font>=\"<font
                   color=\"#990000\">$v</font>\"";
        }
    }
    //## Tag is still still incomplete,
    //## will be completed at either endElement or characterData ##//
    $tag_complete = false;
    $character_data_on = false;
}

function endElement($parser, $name)
{
    global $fp;
    global $character_data_on;
    global $tag_complete;

    //#### Test for self-closing tag ####//
    //## xml_get_current_byte_index(resource parser) when run in this
    //## function, gives the index at (indicated by *):
    //##   for self closing tag: <br />*
    //##   for individual closing tag: <div>character data*</div>
    //## So to test for self-closing tag, we can just test for the last 2
    //## characters from the index
    //###################################//

    if (!$character_data_on) {
        //## Record current fp position ##//
        $temp_fp = ftell($fp);

        //## Point fp to 2 bytes before the end element byte index ##//
        $end_element_byte_index = xml_get_current_byte_index($parser);
        fseek($fp,$end_element_byte_index-2);

        //## Gets the last 2 characters before the end element byte index ##//
        $validator = fgets($fp, 3);

        //## Restore fp position ##//
        fseek($fp,$temp_fp);

        //## If the last 2 character is "/>" ##//
        if ($validator=="/>") {
            //// Complete the self-closing tag ////
            echo " /&gt";
            //// Otherwise it is an individual closing tag ////
        } else echo "&gt&lt/<font color=\"#0000cc\">$name</font>&gt";
        $tag_complete = true;
    } else echo "&lt/<font color=\"#0000cc\">$name</font>&gt";

    $character_data_on = false;
}

function characterData($parser, $data)
{
    global $character_data_on;
    global $tag_complete;

    if ((!$character_data_on)&&(!$tag_complete)) {
        echo "&gt";
        $tag_complete = true;
    }
    echo "<b>$data</b>";
    $character_data_on = true;
}

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

echo "<pre>";
while ($file_content = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $file_content, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
echo "</pre>";
xml_parser_free($xml_parser);
?>
turan dot yuksel at tcmb dot gov dot tr 20-Sep-2005 12:41
The method that 'ibjoel at hotmail dot com' have described requires libxml2 as the xml parser, it does not work with expat. For a brief explanation, see xml_get_current_byte_index.
ibjoel at hotmail dot com 01-Aug-2005 12:49
I noticed that in the example below, and all the examples I've seen on this site for viewing xml in html, the look of self closing tags such as <br /> are not preserved. The parser cannot distinguish between <tag /> and <tag></tag>, and if your start and end element functions are like these examples, both instances will be output with both an indvidual start and end tag.  I needed to preserve self-closing tags and it took me a while to figure out this work around. Hope this helps someone...

The start tag is left open, and then completed by it's first child, the next start tag or its end tag.  The end tag will complete with " />", or </tag> depending on the number of bytes between the start and end tags in the parsed data.
<?php
//$data=filepath or string
$data=<<<DATA
<normal_tag>
  <self_close_tag />
      data
  <normal_tag>data
     <self_close_tag attr="value" />
  </normal_tag>
      data
  <normal_tag></normal_tag>
</normal_tag>
DATA;

function startElement($parser, $name, $attrs)
{
        xml_set_character_data_handler($parser, "characterData");
        global $first_child, $start_byte;
        if($first_child)          //close start tag if neccessary
                echo "><br />";
        $first_child=true;
        $start_byte=xml_get_current_byte_index ($parser);
        if(count($attrs)>=1){
                foreach($attrs as $x=>$y){
                        $attr_string .= " $x=\"$y\"";
                }
        }
        echo htmlentities("<{$name}{$attr_string}"); //unclosed starttag
}

function endElement($parser, $name)
{
        global $first_child, $start_byte;
        $byte=xml_get_current_byte_index ($parser);
        if($byte-$start_byte>2){           //if end tag is more than 2 bytes from start tag
                if($first_child)          //close start tag if neccessary
                        echo "><br />";
                echo htmlentities("</{$name}>")."<br />";  //individual end tag
        }else
                echo " /><br />";  // self closing tag
        $first_child=false;

}

function characterData($parser, $data)
{
        global $first_child;
        if($first_child)  //if $data is first child, close start tag
                echo "><br />";
        if($data=trim($data))
                echo "<font color='blue'>$data</font><br />";
        $first_child=false;
}

function ParseData($data)
{
        $xml_parser = xml_parser_create();
        xml_set_element_handler($xml_parser, "startElement", "endElement");
        xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
        if(is_file($data))
        {
                if (!($fp = fopen($file, "r"))) {
                        die("could not open XML input");
                }

                while ($data = fread($fp, 4096)) {
                        if (!xml_parse($xml_parser, $data, feof($fp))) {
                                $error=xml_error_string(xml_get_error_code($xml_parser));
                               $line=xml_get_current_line_number($xml_parser);
                                die(sprintf("XML error: %s at line %d",$error,$line));
                        }
                }
        }else{
                if (!xml_parse($xml_parser, $data, 1)) {
                                $error=xml_error_string(xml_get_error_code($xml_parser));
                                $line=xml_get_current_line_number($xml_parser);
                                die(sprintf("XML error: %s at line %d",$error,$line));
                }
        }

        xml_parser_free($xml_parser);
}

ParseData($data);
?>
youniforever at naver dot com 30-Apr-2005 12:41
<html>
  <head>
    <title>SAX Demonstration</title>
   <META HTTP-EQUIV='Content-type' CONTENT='text/html; charset=euc-kr'>
  </head>
  <body>
    <h1>RSS ??????</h1>

      <?php

     $file = "data.xml";

      $currentTag = "";
      $currentAttribs = "";

      function startElement($parser, $name, $attribs)
      {
          global $currentTag, $currentAttribs;
          $currentTag = $name;

          $currentAttribs = $attribs;
          switch ($name) {

          default:
              echo("<b>&lt$name&gt</b><br>");
              break;
          }
      }

      function endElement($parser, $name)
      {
          global $currentTag;
          switch ($name) {
          default:
              echo("<br><b>&lt/$name&gt</b><br><br>");
              break;
          }
          $currentTag = "";
          $currentAttribs = "";
      }

      function characterData($parser, $data)
      {
          global $currentTag;
          switch ($currentTag) {
          case "link":
              echo("<a href=\"$data\">$data</a>\n");
              break;
          case "title":
              echo("title : $data");
              break;
          default:
              echo($data);
              break;
          }
      }

     $xmlParser = xml_parser_create();

      $caseFold = xml_parser_get_option($xmlParser,
                                        XML_OPTION_CASE_FOLDING);

      $targetEncoding = xml_parser_get_option($xmlParser,
                                              XML_OPTION_TARGET_ENCODING);

      if ($caseFold == 1) {
          xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, false);
      }

      xml_set_element_handler($xmlParser, "startElement", "endElement");
      xml_set_character_data_handler($xmlParser, "characterData");

      if (!($fp = fopen($file, "r"))) {
          die("Cannot open XML data file: $file");
      }

     while ($data = fread($fp, 4096)) {
          if (!xml_parse($xmlParser, $data, feof($fp))) {
              die(sprintf("XML error: %s at line %d",
                          xml_error_string(xml_get_error_code($xmlParser)),
                          xml_get_current_line_number($xmlParser)));
              xml_parser_free($xmlParser);
          }
      }
      xml_parser_free($xmlParser);
      ?>
    </table>
  </body>
</html>
13-Mar-2005 10:34
In response to aw at avatartechnology dot com...
In response to landb at mail dot net...

When your functions are in an object:
Careful ! Don't forget to add: & (reference) to your parameters.

xml_set_element_handler($parser, array(&$this,"_startElement"), array(&$this,"_endElement"));
--> xmlparse will work on your object (good).

instead of:
xml_set_element_handler($parser, array($this,"_startElement"), array($this,"_endElement"));
---> xmlparse will work on a COPY of your object (often bad)

Vin-s
(sorry for my english)
aw at avatartechnology dot com 21-Oct-2004 05:40
In response to landb at mail dot net...

As the notes mention, you can pass an array that contains the reference to an object and a method name when you need... so you can call methods in your own class as handlers like this:

xml_set_element_handler($parser, array($this,"_startElement"), array($this,"_endElement"));

Hope it helps...
tj at tobyjoe dot com 31-Jan-2003 08:22
It seems that the tag handlers don't block on one another (the end handler is called whether or not the begin handler has finished). this can put you in a tight spot if you don't realize it while planning your app.
11-Oct-2001 01:09
You CAN use classes to parse XML. Just take a look at the following function:

xml_set_object
jg at jmkg dot net 07-May-2001 08:08
If you are using a class for xml parsing, and want to check the return value of xml_set_element_handler in case it  fails, you must do this outside of the class's constructor. Inside the constructor, PHP-4.0.5 will die.

Basically, put all your xml initialisation code in another function of the class, and keep it out of the constructor.


Set up end namespace declaration handler

  • ≪ xml_set_element_handler
  • xml_set_external_entity_ref_handler ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Set up end namespace declaration handler

xml_set_end_namespace_decl_handler

(PHP 4 >= 4.0.5, PHP 5, PHP 7)

xml_set_end_namespace_decl_handler — Set up end namespace declaration handler

설명

bool xml_set_end_namespace_decl_handler ( resource $parser , callable $handler )

Set a handler to be called when leaving the scope of a namespace declaration. This will be called, for each namespace declaration, after the handler for the end tag of the element in which the namespace was declared.

인수

parser

A reference to the XML parser.

handler

handler is a string containing the name of a function that must exist when xml_parse() is called for parser.

The function named by handler must accept two parameters, and should return an integer value. If the value returned from the handler is FALSE (which it will be if no value is returned), the XML parser will stop parsing and xml_get_error_code() will return XML_ERROR_EXTERNAL_ENTITY_HANDLING.

handler ( resource $parser , string $prefix )
parser
The first parameter, parser, is a reference to the XML parser calling the handler.
prefix
The prefix is a string used to reference the namespace within an XML object.

If a handler function is set to an empty string, or FALSE, the handler in question is disabled.

Note: 함수 이름 대신에, 객체의 참조와 메쏘드 이름을 가지는 배열을 사용할 수 있습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

주의

Note:

This event is not supported under LibXML.

참고

  • xml_set_start_namespace_decl_handler() - Set up start namespace declaration handler

User Contributed Notes

Aeden Jameson 20-Dec-2004 06:19
NOTE: The namespace end event is not supported under libxml.  This issue is formally addressed in the bug report at http://bugs.php.net/bug.php?id=30834


Set up external entity reference handler

  • ≪ xml_set_end_namespace_decl_handler
  • xml_set_notation_decl_handler ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Set up external entity reference handler

xml_set_external_entity_ref_handler

(PHP 4, PHP 5, PHP 7)

xml_set_external_entity_ref_handler — Set up external entity reference handler

설명

bool xml_set_external_entity_ref_handler ( resource $parser , callable $handler )

Sets the external entity reference handler function for the XML parser parser.

인수

parser

A reference to the XML parser to set up external entity reference handler function.

handler

handler is a string containing the name of a function that must exist when xml_parse() is called for parser.

The function named by handler must accept five parameters, and should return an integer value.If the value returned from the handler is FALSE (which it will be if no value is returned), the XML parser will stop parsing and xml_get_error_code() will return XML_ERROR_EXTERNAL_ENTITY_HANDLING.

handler ( resource $parser , string $open_entity_names , string $base , string $system_id , string $public_id )
parser
The first parameter, parser, is a reference to the XML parser calling the handler.
open_entity_names
The second parameter, open_entity_names, is a space-separated list of the names of the entities that are open for the parse of this entity (including the name of the referenced entity).
base
This is the base for resolving the system identifier (system_id) of the external entity.Currently this parameter will always be set to an empty string.
system_id
The fourth parameter, system_id, is the system identifier as specified in the entity declaration.
public_id
The fifth parameter, public_id, is the public identifier as specified in the entity declaration, or an empty string if none was specified; the whitespace in the public identifier will have been normalized as required by the XML spec.

If a handler function is set to an empty string, or FALSE, the handler in question is disabled.

Note: 함수 이름 대신에, 객체의 참조와 메쏘드 이름을 가지는 배열을 사용할 수 있습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

There are no user contributed notes for this page.


Set up notation declaration handler

  • ≪ xml_set_external_entity_ref_handler
  • xml_set_object ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Set up notation declaration handler

xml_set_notation_decl_handler

(PHP 4, PHP 5, PHP 7)

xml_set_notation_decl_handler — Set up notation declaration handler

설명

bool xml_set_notation_decl_handler ( resource $parser , callable $handler )

Sets the notation declaration handler function for the XML parser parser.

A notation declaration is part of the document's DTD and has the following format:

<!NOTATION <parameter>name</parameter>
{ <parameter>systemId</parameter> | <parameter>publicId</parameter>?>
See » section 4.7 of the XML 1.0 spec for the definition of notation declarations.

인수

parser

A reference to the XML parser to set up notation declaration handler function.

handler

handler is a string containing the name of a function that must exist when xml_parse() is called for parser.

The function named by handler must accept five parameters:

handler ( resource $parser , string $notation_name , string $base , string $system_id , string $public_id )
parser
The first parameter, parser, is a reference to the XML parser calling the handler.
notation_name
This is the notation's name, as per the notation format described above.
base
This is the base for resolving the system identifier (system_id) of the notation declaration. Currently this parameter will always be set to an empty string.
system_id
System identifier of the external notation declaration.
public_id
Public identifier of the external notation declaration.

If a handler function is set to an empty string, or FALSE, the handler in question is disabled.

Note: 함수 이름 대신에, 객체의 참조와 메쏘드 이름을 가지는 배열을 사용할 수 있습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

There are no user contributed notes for this page.


Use XML Parser within an object

  • ≪ xml_set_notation_decl_handler
  • xml_set_processing_instruction_handler ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Use XML Parser within an object

xml_set_object

(PHP 4, PHP 5, PHP 7)

xml_set_object — Use XML Parser within an object

설명

bool xml_set_object ( resource $parser , object &$object )

This function allows to use parser inside object. All callback functions could be set with xml_set_element_handler() etc and assumed to be methods of object.

Caution

In addition to calling xml_parser_free() when the parsing is finished, as of PHP 7.0.0 it is necessary to also explicitly unset the reference to parser to avoid memory leaks.

인수

parser

A reference to the XML parser to use inside the object.

object

The object where to use the XML parser.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 xml_set_object() example

<?php
class XMLParser
{
    private $parser;

    function __construct()
    {
        $this->parser = xml_parser_create();

        xml_set_object($this->parser, $this);
        xml_set_element_handler($this->parser, "tag_open", "tag_close");
        xml_set_character_data_handler($this->parser, "cdata");
    }

    function __destruct()
    {
        xml_parser_free($this->parser);
        unset($this->parser);
    }

    function parse($data)
    {
        xml_parse($this->parser, $data);
    }

    function tag_open($parser, $tag, $attributes)
    {
        var_dump($tag, $attributes);
    }

    function cdata($parser, $cdata)
    {
        var_dump($cdata);
    }

    function tag_close($parser, $tag)
    {
        var_dump($tag);
    }
}

$xml_parser = new XMLParser();
$xml_parser->parse("<A ID='hallo'>PHP</A>");
?>

위 예제의 출력:

string(1) "A"
array(1) {
  ["ID"]=>
  string(5) "hallo"
}
string(3) "PHP"
string(1) "A"

User Contributed Notes

joey at gimo dot co dot uk 11-Aug-2016 07:32
This function seems to be of very limited use when [$object_instance, "method_name"] is a callable.
wake dot shinigami at gmail dot com 23-Nov-2005 05:28
I finally managed to get xml_set_object() to work, after much documentation searching.  As the solution I found has been hinted at, the focus has been wrong.

There have been many problems getting class contained XML parsers to make changes to the members of an instance.  This is because, from what I'm guessing, the functions are using a either a new annonymous instance of the class or an uninstanced version of the class.

What we want to make sure it does it that the parser is accessing its handlers as member methods of a particular instance of a class.  This can be done using the array method for passing a callback, setting the object as a reference to this instance.  This way, you know that the parser is going to call the function properly.

Ex:
<?php
class Parser {

 private $parser;
 private $data;

 public function __construct() {
  $this->parser = NULL;
  $this->data = '';
 }

 private function ParseElementStart($parser, $name, $attrs) {
  // Code
 }
 private function ParseElementEnd($parser, $name) {
  // Code
 }

 public function Parse($XMLdata) {
  $this->parser = xml_parser_create();
  xml_set_object($this->parser, $this);
  xml_set_element_handler($this->parser,
                          array(&$this, 'ParseElementStart'),
                          array(&$this, 'ParseElementEnd'));
  xml_parse($this->parser, $XMLdata);
  xml_parser_free($this->parser);
 }
}
?>

With this, when you call the Parse method, the data in that instance can be modified.  I'm not sure if xml_set_object becomes unneccissary when using the array callbacks, but I leave it in just to make sure that the xml_parse function knows it's in the object.

As above, I'd recommend for memory sake, that the XML parser be created, used, and freed all in the same function, to ensure that everything is cleaned up correctly.
bettmenn at gmx dot de 16-Oct-2004 11:03
Adding to 'lmfe at mega dot ist dot utl dot pt' contribution below, I have to say that this behaviour is very usual for PHP if MemberVars of an Object arent initialized from the constructor.
What worked best for me so far was something like this:
<?php
class foo {
  var $bar
  // the constructor
  function foo() {
    unset($this->bar);  // with every instanciation, the variable is cleared for the object
  }
}
?>
lmfe at mega dot ist dot utl dot pt 29-Sep-2004 07:51
Using PHP 4.3.0, I got this strange behaviour:

<?php
class xml  {

     /* (as specified in the documentation above) */

} // end of class xml

/* This code works */
$xml_parser = new xml();
$xml_parser->parse("<A ID='hallo'>PHP</A>");

/* This code works */
$xml_parser2 = new xml();
$xml_parser2->parse("<A ID='hallo2'>PHP2</A>");

/* This code DON'T work */
$xml_parser = new xml();
$xml_parser->parse("<A ID='hallo3'>PHP3</A>");
?>

In the third block of code, php complains it can't find the handlers it needs.
It seems this problem only happens if the variable is used more than once.
Calling xml_parser_free doesn't help
hwheinzen at t-online dot de 15-Aug-2004 12:38
(Adding to zitan's note)
It seems easy enough to provide output functions for callback handler classes too.

Example:

<?php
class CallBack {
    var $name = 'Callback';
    var $info = 'Information!';
    function toString() {
        return $this->name.': '.$this->info;
    }
}
class Main {
    var $name = 'Main';
    var $callBackObject;
    function setCallBack(&$cBIn) { $this->callBackObject = $cBIn; }
    function toString() {
        return $this->name.': '.$this->callBackObject->toString();
    }
}

$cb = & new CallBack;
$m = & new Main;
$m->setCallBack(&$cb);
echo $cb->toString();
echo $m->toString();
?>

So, after collecting information during the parse operation within the callback handler class, e.g. in tag_close(), retrieving them is possible.
zitan at mediasculpt dot net 02-Sep-2003 06:05
Just a note about creating abstract "call back handlers" as mentioned in some of the other notes.  In this case I recommend extending the base XML class and overwritting the handler methods.  The reason I wanted to do this, is that if you have a separate callback method class it causes problems, for example if you want to collect information out of the XML file and store it in an array.  You can get around it with global variables, but I prefer to use them only when required ;)

Example:

<?php
class xml_output extends xml{

  var $output = array();

   function xml_output(){
    $this->xml();
   }

    //overwrite base methods
    function tag_open($parser, $tag, $attributes)
    {
        array_push($this->output, "<$tag, attributes>");
    }

    function cdata($parser, $cdata)
    {
         array_push($this->output, "$cdata");
    }

    function tag_close($parser, $tag)
    {
         array_push($this->output, "</$tag>");
    }

}

$xml_parser = & new xml_output();
$xml_parser->parse("<A ID='hallo'>PHP</A>");
echo("$xml_parser->output");
?>
sbeam at syxyz dot net 12-Nov-2002 06:34
passing the object as a call-time ref (&$this) as in the example will create a warning in php 4.1+. Pass xml_set_object($xp,$this); instead. This does not seem to break anything - but I dont know for sure.
chiefgeek at ecodedesign.com 05-Aug-2002 07:37
Just adding a little note about my above written example.  It needs some improvements.

Because PHP passes by VALUE by default, when you pass an array like this:
array($callback_handler, 'handler_method')
PHP makes a copy of the callback_handler object and uses the handler_method in the copy.

This is not an ideal situation for many reasons...which I will not get into here...but you should have an idea by now.

The best way to fix this is to change a few things. In the function declaration change the parameter from $callback_handler to &$callback_handler. So now your declaration should look like this

function set_callback_handler(&$callback_handler)
{
  ...
}

Now each time you reference $callback_handler change it to &$callback_handler.  For example:

xml_set_element_handler(
          $this->xml_parser,
          array(&$callback_handler, 'start_element'),
          array(&$callback_handler, 'close_Element' ));

This ensures that PHP will always use the same object.
chiefgeek at ecodedesign.com 04-Aug-2002 05:25
xml_set_object is great, if you want to hard code your start_element, end_element, etc functions within your xml class.

But what if you want to increase the modularity of your application?

Solution, create one generic XMLParser class that handles everything *except* the callback funtions.  Then create an abstract XMLCallbackHandler class which you can extend to provide any customization you want.

So how do you tell the php's xml_parser that you want to use this other class to handle the callback functions.

xml_set_object?  This only works if the functions are *within* the object you called this method from.

The solution lies in the xml_set_element_handler() function.
Take a look at this sample source...

class XMLParser
{

  ...

  function set_callback_handler($callback_handler)
  {
    // Assign the startElement endElement functions
    xml_set_element_handler(
          $this->xml_parser,
          array($callback_handler, 'start_element'),
          array($callback_handler, 'close_Element' ));

    /* By passing an array with this ($object, 'function_name') structure
       to the 2nd and 3rd parameters.  We are able to tell the parser to
       look in another object for these callback functions.
    */

    // Assign the character data function
    xml_set_character_data_handler(
          $this->xml_parser,
          array($callback_handler, 'character_data'));
    }
}

class myCallBackHandler
{
  function start_element(...)
  {
    //code goes here
  }

  function end_element(...)
  {
    //code goes here
  }

  function character_data(...)
  {
    //code goes here
  }
}

Now all thats left to do is use these classes..

$parser = new XMLParser();
$parser->set_callback_handler(new myCallbackHandler());
dfoesch at cs dot nmsu dot edu 12-Apr-2002 02:38
English explaination of why you do: $xml = & new xml();

Ok, when PHP executes "new xml()" it creates an anonymous variable (one that you can't reference with any name) which it then runs the constructor function on.  Ok, now once that's done, it then assigns by VALUE in the example above.  This means that that lovely pointer to your parser is pointing to the anonymous instance of your class, and not the used instance of your class... thus making a "shadow" of all your variables, where assignments inside the parser access different variables than outside the parser.  What PHP _SHOULD_ be doing (similar to C++) is have this statement execute such that the assignment is done by REFERENCE, so that you assign the new name to the class that actually got constructed, and not just a copy of the class that you constructed.

If they did it right, this code wouldn't be broken.
14-Feb-2002 08:10
Call-time pass-by-reference has been deprecated, so the example is buggy as was already mentionned. However since 4.04 (I think) "new" can return objects by reference. So the clean way to have both the parser initialised in the constructor, and to save the result in your object is to do

$xml_parser = & new xml();

see http://www.php.net/manual/en/language.oop.newref.php

Ivan
pete dot nelson at serverSXPYAZMsolved dot com 27-Aug-2001 07:15
<p>in reply to jon9mm's question, I have found the same problem (function 'startElement' does not exist).  I finally found the way around this, by re-reading this doc for xml_set_object.  You HAVE to use the xml_set_object($this->parser, &$this) within your parser function.</p> <p>When that function is called, your object temporarily becomes the parser object and shares scope (so it can see 'startElement', etc).   Once that function is done, the xml_set_object(...) call goes out of scope, and your object is no longer bound to the parser.  So the simplist solution is to call xml_set_object(...), do any parsing, and then call xml_parser_free(...) all within the same function.</p>
<p>And remember, don't call xml_set_object(...) in your object's constructor - your object will be forever bound to the parser object and you'll loose all access to your object's member functions.</p>
<p>This is all explained in the examples above, but it took me reading it a couple of times to grok how it all works.</p>
nick at category4 dot com 14-May-2001 10:45
If you're using the XML parser within an object, be careful not to accidentally call any methods that don't exist within the object. Instead of complaining that the missing method doesn't exist, PHP will say it's not able to find the handler routines, even if you've indicated them correctly. (PHP version 4.0.5 on FreeBSD).
williams at cse dot unl dot edu 06-Dec-2000 09:42
If xml_set_object is not called in the constructor of an object, the values that the parser modifies will be saved. Else, the values do not seem to be saved as stated by "i_sofer" above.
--- Start Example Code ---
class foo {
var xmlparser;
function foo() { \\the constructor
$this->xmlparser=xmlparser_create();
}
function parse() {
xml_set_object($this->xmlparser,&$this);
\\also include data handler
\\and element handlers here
}
}
--- End Example Code ---
i_sofer at yahoo dot com 19-Nov-2000 01:15
Using xml parser in objects seem to have a problem that values modified(even of the object itself) get lost as soon as parsing ends...
malcontent at msgto dot com 20-Aug-2000 09:11
The example will generate a warning unless allow_call_time_pass_reference is set to true in your INI file. It also will not accept a call by value.
robert at webmotion dot com 18-Apr-2000 07:58
While true that there is no automatic destructor for PHP class objects, it is quite simple to create a method called destroy and perform the destruction manually. In the example above it may not be necessary to free the xml parser immediately... it would appear from the example that the object is re-useable.


Set up processing instruction (PI) handler

  • ≪ xml_set_object
  • xml_set_start_namespace_decl_handler ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Set up processing instruction (PI) handler

xml_set_processing_instruction_handler

(PHP 4, PHP 5, PHP 7)

xml_set_processing_instruction_handler — Set up processing instruction (PI) handler

설명

bool xml_set_processing_instruction_handler ( resource $parser , callable $handler )

Sets the processing instruction (PI) handler function for the XML parser parser.

A processing instruction has the following format:

<?
target data
?>
You can put PHP code into such a tag, but be aware of one limitation: in an XML PI, the PI end tag (?>) can not be quoted, so this character sequence should not appear in the PHP code you embed with PIs in XML documents.If it does, the rest of the PHP code, as well as the "real" PI end tag, will be treated as character data.

인수

parser

A reference to the XML parser to set up processing instruction (PI) handler function.

handler

handler is a string containing the name of a function that must exist when xml_parse() is called for parser.

The function named by handler must accept three parameters:

handler ( resource $parser , string $target , string $data )
parser
The first parameter, parser, is a reference to the XML parser calling the handler.
target
The second parameter, target, contains the PI target.
data
The third parameter, data, contains the PI data.

If a handler function is set to an empty string, or FALSE, the handler in question is disabled.

Note: 함수 이름 대신에, 객체의 참조와 메쏘드 이름을 가지는 배열을 사용할 수 있습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

There are no user contributed notes for this page.


Set up start namespace declaration handler

  • ≪ xml_set_processing_instruction_handler
  • xml_set_unparsed_entity_decl_handler ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Set up start namespace declaration handler

xml_set_start_namespace_decl_handler

(PHP 4 >= 4.0.5, PHP 5, PHP 7)

xml_set_start_namespace_decl_handler — Set up start namespace declaration handler

설명

bool xml_set_start_namespace_decl_handler ( resource $parser , callable $handler )

Set a handler to be called when a namespace is declared. Namespace declarations occur inside start tags. But the namespace declaration start handler is called before the start tag handler for each namespace declared in that start tag.

인수

parser

A reference to the XML parser.

handler

handler is a string containing the name of a function that must exist when xml_parse() is called for parser.

The function named by handler must accept three parameters, and should return an integer value. If the value returned from the handler is FALSE (which it will be if no value is returned), the XML parser will stop parsing and xml_get_error_code() will return XML_ERROR_EXTERNAL_ENTITY_HANDLING.

handler ( resource $parser , string $prefix , string $uri )
parser
The first parameter, parser, is a reference to the XML parser calling the handler.
prefix
The prefix is a string used to reference the namespace within an XML object.
uri
Uniform Resource Identifier (URI) of namespace.

If a handler function is set to an empty string, or FALSE, the handler in question is disabled.

Note: 함수 이름 대신에, 객체의 참조와 메쏘드 이름을 가지는 배열을 사용할 수 있습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • xml_set_end_namespace_decl_handler() - Set up end namespace declaration handler

User Contributed Notes

There are no user contributed notes for this page.


Set up unparsed entity declaration handler

  • ≪ xml_set_start_namespace_decl_handler
  • XMLReader ≫
  • PHP Manual
  • XML Parser 함수 목록
  • Set up unparsed entity declaration handler

xml_set_unparsed_entity_decl_handler

(PHP 4, PHP 5, PHP 7)

xml_set_unparsed_entity_decl_handler — Set up unparsed entity declaration handler

설명

bool xml_set_unparsed_entity_decl_handler ( resource $parser , callable $handler )

Sets the unparsed entity declaration handler function for the XML parser parser.

The handler will be called if the XML parser encounters an external entity declaration with an NDATA declaration, like the following:

<!ENTITY <parameter>name</parameter> {<parameter>publicId</parameter> | <parameter>systemId</parameter>}
        NDATA <parameter>notationName</parameter>

See » section 4.2.2 of the XML 1.0 spec for the definition of notation declared external entities.

인수

parser

A reference to the XML parser to set up unparsed entity declaration handler function.

handler

handler is a string containing the name of a function that must exist when xml_parse() is called for parser.

The function named by handler must accept six parameters:

handler ( resource $parser , string $entity_name , string $base , string $system_id , string $public_id , string $notation_name )
parser
The first parameter, parser, is a reference to the XML parser calling the handler.
entity_name
The name of the entity that is about to be defined.
base
This is the base for resolving the system identifier (systemId) of the external entity.Currently this parameter will always be set to an empty string.
system_id
System identifier for the external entity.
public_id
Public identifier for the external entity.
notation_name
Name of the notation of this entity (see xml_set_notation_decl_handler()).

If a handler function is set to an empty string, or FALSE, the handler in question is disabled.

Note: 함수 이름 대신에, 객체의 참조와 메쏘드 이름을 가지는 배열을 사용할 수 있습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

There are no user contributed notes for this page.


XMLReader

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • XMLReader — The XMLReader class
    • XMLReader::close — Close the XMLReader input
    • XMLReader::expand — Returns a copy of the current node as a DOM object
    • XMLReader::getAttribute — Get the value of a named attribute
    • XMLReader::getAttributeNo — Get the value of an attribute by index
    • XMLReader::getAttributeNs — Get the value of an attribute by localname and URI
    • XMLReader::getParserProperty — Indicates if specified property has been set
    • XMLReader::isValid — Indicates if the parsed document is valid
    • XMLReader::lookupNamespace — Lookup namespace for a prefix
    • XMLReader::moveToAttribute — Move cursor to a named attribute
    • XMLReader::moveToAttributeNo — Move cursor to an attribute by index
    • XMLReader::moveToAttributeNs — Move cursor to a named attribute
    • XMLReader::moveToElement — Position cursor on the parent Element of current Attribute
    • XMLReader::moveToFirstAttribute — Position cursor on the first Attribute
    • XMLReader::moveToNextAttribute — Position cursor on the next Attribute
    • XMLReader::next — Move cursor to next node skipping all subtrees
    • XMLReader::open — Set the URI containing the XML to parse
    • XMLReader::read — Move to next node in document
    • XMLReader::readInnerXML — Retrieve XML from current node
    • XMLReader::readOuterXML — Retrieve XML from current node, including itself
    • XMLReader::readString — Reads the contents of the current node as a string
    • XMLReader::setParserProperty — Set parser options
    • XMLReader::setRelaxNGSchema — Set the filename or URI for a RelaxNG Schema
    • XMLReader::setRelaxNGSchemaSource — Set the data containing a RelaxNG Schema
    • XMLReader::setSchema — Validate document against XSD
    • XMLReader::XML — Set the data containing the XML to parse

User Contributed Notes

kraussInode at yahoo dot com 27-Mar-2010 09:54
See also XMLWriter: XMLReader and XMLWriter complement each other.


소개

The XMLReader extension is an XML Pull parser. The reader acts as a cursor going forward on the document stream and stopping at each node on the way.

인코딩

It is important to note that internally, libxml uses the UTF-8 encoding and as such, the encoding of the retrieved contents will always be in UTF-8 encoding.

User Contributed Notes

There are no user contributed notes for this page.


설치

The XMLReader extension was initially a PECL extension for PHP 5. It was later moved to the PHP source (bundled) as of PHP 5.1.0, and later enabled by default as of PHP 5.1.2.

이 확장은 기본값으로 활성화됩니다. 컴파일 시에 다음 옵션으로 비활성화 할 수 있습니다: --disable-xmlreader

User Contributed Notes

Miguel Cruz 11-Jun-2012 10:06
If you are having trouble loading this extension (perhaps with a log message like 'Undefined symbol "dom_node_class_entry"'), make sure that you have xmlreader.so listed AFTER dom.so in php.ini.
alvaro at digdib dot com 02-Jan-2009 11:33
Gentoo users: emerge does not compile dev-lang/php with --enable-xmlreader by default. To enable this option, add the flag xmlreader to your USE variable and recompile php.


The XMLReader class

(PHP 5 >= 5.1.0, PHP 7)

소개

The XMLReader extension is an XML Pull parser. The reader acts as a cursor going forward on the document stream and stopping at each node on the way.

클래스 개요

XMLReader {
/* 상수 */
const int NONE = 0 ;
const int ELEMENT = 1 ;
const int ATTRIBUTE = 2 ;
const int TEXT = 3 ;
const int CDATA = 4 ;
const int ENTITY_REF = 5 ;
const int ENTITY = 6 ;
const int PI = 7 ;
const int COMMENT = 8 ;
const int DOC = 9 ;
const int DOC_TYPE = 10 ;
const int DOC_FRAGMENT = 11 ;
const int NOTATION = 12 ;
const int WHITESPACE = 13 ;
const int SIGNIFICANT_WHITESPACE = 14 ;
const int END_ELEMENT = 15 ;
const int END_ENTITY = 16 ;
const int XML_DECLARATION = 17 ;
const int LOADDTD = 1 ;
const int DEFAULTATTRS = 2 ;
const int VALIDATE = 3 ;
const int SUBST_ENTITIES = 4 ;
/* 프로퍼티 */
public readonly int $attributeCount ;
public readonly string $baseURI ;
public readonly int $depth ;
public readonly bool $hasAttributes ;
public readonly bool $hasValue ;
public readonly bool $isDefault ;
public readonly bool $isEmptyElement ;
public readonly string $localName ;
public readonly string $name ;
public readonly string $namespaceURI ;
public readonly int $nodeType ;
public readonly string $prefix ;
public readonly string $value ;
public readonly string $xmlLang ;
/* 메소드 */
public bool close ( void )
public DOMNode expand ([ DOMNode $basenode ] )
public string getAttribute ( string $name )
public string getAttributeNo ( int $index )
public string getAttributeNs ( string $localName , string $namespaceURI )
public bool getParserProperty ( int $property )
public bool isValid ( void )
public string lookupNamespace ( string $prefix )
public bool moveToAttribute ( string $name )
public bool moveToAttributeNo ( int $index )
public bool moveToAttributeNs ( string $localName , string $namespaceURI )
public bool moveToElement ( void )
public bool moveToFirstAttribute ( void )
public bool moveToNextAttribute ( void )
public bool next ([ string $localname ] )
public bool open ( string $URI [, string $encoding [, int $options = 0 ]] )
public bool read ( void )
public string readInnerXML ( void )
public string readOuterXML ( void )
public string readString ( void )
public bool setParserProperty ( int $property , bool $value )
public bool setRelaxNGSchema ( string $filename )
public bool setRelaxNGSchemaSource ( string $source )
public bool setSchema ( string $filename )
public bool xml ( string $source [, string $encoding [, int $options = 0 ]] )
}

프로퍼티

attributeCount

The number of attributes on the node

baseURI

The base URI of the node

depth

Depth of the node in the tree, starting at 0

hasAttributes

Indicates if node has attributes

hasValue

Indicates if node has a text value

isDefault

Indicates if attribute is defaulted from DTD

isEmptyElement

Indicates if node is an empty element tag

localName

The local name of the node

name

The qualified name of the node

namespaceURI

The URI of the namespace associated with the node

nodeType

The node type for the node

prefix

The prefix of the namespace associated with the node

value

The text value of the node

xmlLang

The xml:lang scope which the node resides

예약 상수

XMLReader Node Types

XMLReader::NONE

No node type

XMLReader::ELEMENT

Start element

XMLReader::ATTRIBUTE

Attribute node

XMLReader::TEXT

Text node

XMLReader::CDATA

CDATA node

XMLReader::ENTITY_REF

Entity Reference node

XMLReader::ENTITY

Entity Declaration node

XMLReader::PI

Processing Instruction node

XMLReader::COMMENT

Comment node

XMLReader::DOC

Document node

XMLReader::DOC_TYPE

Document Type node

XMLReader::DOC_FRAGMENT

Document Fragment node

XMLReader::NOTATION

Notation node

XMLReader::WHITESPACE

Whitespace node

XMLReader::SIGNIFICANT_WHITESPACE

Significant Whitespace node

XMLReader::END_ELEMENT

End Element

XMLReader::END_ENTITY

End Entity

XMLReader::XML_DECLARATION

XML Declaration node

XMLReader Parser Options

XMLReader::LOADDTD

Load DTD but do not validate

XMLReader::DEFAULTATTRS

Load DTD and default attributes but do not validate

XMLReader::VALIDATE

Load DTD and validate while parsing

XMLReader::SUBST_ENTITIES

Substitute entities and expand references

Table of Contents

  • XMLReader::close — Close the XMLReader input
  • XMLReader::expand — Returns a copy of the current node as a DOM object
  • XMLReader::getAttribute — Get the value of a named attribute
  • XMLReader::getAttributeNo — Get the value of an attribute by index
  • XMLReader::getAttributeNs — Get the value of an attribute by localname and URI
  • XMLReader::getParserProperty — Indicates if specified property has been set
  • XMLReader::isValid — Indicates if the parsed document is valid
  • XMLReader::lookupNamespace — Lookup namespace for a prefix
  • XMLReader::moveToAttribute — Move cursor to a named attribute
  • XMLReader::moveToAttributeNo — Move cursor to an attribute by index
  • XMLReader::moveToAttributeNs — Move cursor to a named attribute
  • XMLReader::moveToElement — Position cursor on the parent Element of current Attribute
  • XMLReader::moveToFirstAttribute — Position cursor on the first Attribute
  • XMLReader::moveToNextAttribute — Position cursor on the next Attribute
  • XMLReader::next — Move cursor to next node skipping all subtrees
  • XMLReader::open — Set the URI containing the XML to parse
  • XMLReader::read — Move to next node in document
  • XMLReader::readInnerXML — Retrieve XML from current node
  • XMLReader::readOuterXML — Retrieve XML from current node, including itself
  • XMLReader::readString — Reads the contents of the current node as a string
  • XMLReader::setParserProperty — Set parser options
  • XMLReader::setRelaxNGSchema — Set the filename or URI for a RelaxNG Schema
  • XMLReader::setRelaxNGSchemaSource — Set the data containing a RelaxNG Schema
  • XMLReader::setSchema — Validate document against XSD
  • XMLReader::XML — Set the data containing the XML to parse

User Contributed Notes

dkrnl at yandex dot ru 04-Oct-2013 01:10
Wrapper XMLReader class, for simple SAX-reading huge xml:
https://github.com/dkrnl/SimpleXMLReader

Usage example: http://github.com/dkrnl/SimpleXMLReader/blob/master/examples/example1.php

<?php

/**
 * Simple XML Reader
 *
 * @license Public Domain
 * @author Dmitry Pyatkov(aka dkrnl) <dkrnl@yandex.ru>
 * @url http://github.com/dkrnl/SimpleXMLReader
 */
class SimpleXMLReader extends XMLReader
{

    /**
     * Callbacks
     *
     * @var array
     */
    protected $callback = array();

    /**
     * Add node callback
     *
     * @param  string   $name
     * @param  callback $callback
     * @param  integer  $nodeType
     * @return SimpleXMLReader
     */
    public function registerCallback($name, $callback, $nodeType = XMLREADER::ELEMENT)
    {
        if (isset($this->callback[$nodeType][$name])) {
            throw new Exception("Already exists callback $name($nodeType).");
        }
        if (!is_callable($callback)) {
            throw new Exception("Already exists parser callback $name($nodeType).");
        }
        $this->callback[$nodeType][$name] = $callback;
        return $this;
    }

    /**
     * Remove node callback
     *
     * @param  string  $name
     * @param  integer $nodeType
     * @return SimpleXMLReader
     */
    public function unRegisterCallback($name, $nodeType = XMLREADER::ELEMENT)
    {
        if (!isset($this->callback[$nodeType][$name])) {
            throw new Exception("Unknow parser callback $name($nodeType).");
        }
        unset($this->callback[$nodeType][$name]);
        return $this;
    }

    /**
     * Run parser
     *
     * @return void
     */
    public function parse()
    {
        if (empty($this->callback)) {
            throw new Exception("Empty parser callback.");
        }
        $continue = true;
        while ($continue && $this->read()) {
            if (isset($this->callback[$this->nodeType][$this->name])) {
                $continue = call_user_func($this->callback[$this->nodeType][$this->name], $this);
            }
        }
    }

    /**
     * Run XPath query on current node
     *
     * @param  string $path
     * @param  string $version
     * @param  string $encoding
     * @return array(SimpleXMLElement)
     */
    public function expandXpath($path, $version = "1.0", $encoding = "UTF-8")
    {
        return $this->expandSimpleXml($version, $encoding)->xpath($path);
    }

    /**
     * Expand current node to string
     *
     * @param  string $version
     * @param  string $encoding
     * @return SimpleXMLElement
     */
    public function expandString($version = "1.0", $encoding = "UTF-8")
    {
        return $this->expandSimpleXml($version, $encoding)->asXML();
    }

    /**
     * Expand current node to SimpleXMLElement
     *
     * @param  string $version
     * @param  string $encoding
     * @param  string $className
     * @return SimpleXMLElement
     */
    public function expandSimpleXml($version = "1.0", $encoding = "UTF-8", $className = null)
    {
        $element = $this->expand();
        $document = new DomDocument($version, $encoding);
        $node = $document->importNode($element, true);
        $document->appendChild($node);
        return simplexml_import_dom($node, $className);
    }

    /**
     * Expand current node to DomDocument
     *
     * @param  string $version
     * @param  string $encoding
     * @return DomDocument
     */
    public function expandDomDocument($version = "1.0", $encoding = "UTF-8")
    {
        $element = $this->expand();
        $document = new DomDocument($version, $encoding);
        $node = $document->importNode($element, true);
        $document->appendChild($node);
        return $document;
    }

}
?>
kula_shakerz 03-Apr-2013 11:27
Found this in the IXmlReader docs at msdn but it's also valid for XMLReader in PHP.

You should save the value of $isEmptyElement before processing attributes, or call moveToElement to make $isEmptyElement valid after processing attributes.

$isEmptyElement returns FALSE when XMLReader is positioned on an attribute node, even if attribute's parent element is empty.
Mike De Smet 26-Sep-2012 01:35
For those of you getting xml files that do not contain duplicate elements (in the same element), the following converter converts to arrays with key/value mapping (thus overwriting duplicate elements!):

Note this is untested with attributes although I built in support.

<?php
    function xml2assoc($xml, array &$target = array()) {
        while ($xml->read()) {
            switch ($xml->nodeType) {
                case XMLReader::END_ELEMENT:
                    return $target;
                case XMLReader::ELEMENT:
                    $name = $xml->name;
                    $target[$name] = $xml->hasAttributes ? array() : '';
                    if (!$xml->isEmptyElement) {
                        $target[$name] = array();
                        xml2assoc($xml, $target[$name]);
                    }

                    if ($xml->hasAttributes)
                        while($xml->moveToNextAttribute())
                            $target[$name]['@'.$xml->name] = $xml->value;
                    break;
                case XMLReader::TEXT:
                case XMLReader::CDATA:
                    $target = $xml->value;
            }
        }
        return $target;
    }
?>
casella dot email at google dot mail dot com 23-Sep-2012 12:59
To verify that all nodes are read without error/warning you can use this code:
<?php
$endofxml = false;
$xml_url = "example.xml";
$reader = new XMLReader();
if(!$reader->open($xml_url)){
    print "Error to open XML: $xml_url\n";
} else {
    while ($reader->read()) {
        $firstnode = (!isset($firstnode)) ? $reader->name : $firstnode;
/*
DO SOMETHING
*/
        if ($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == $firstnode) {
            $endofxml = true;
        }
    }
}
if($endofxml) {
    print "no error found";
} else {
    print "error found";
}
?>

This code is useful to trap $reader->read() error/warning.
lee8oi at gmail dot com 21-Mar-2012 10:54
Sometimes you have an unusual URL that doesn't actually point to an xml file but still returns xml as output (Like the Battlefield Heroes generated syndication urls). Using get_file_contents(url) you can retrieve the xml data from these urls and pass it as a variable for processing as an XML String.

Unfortunately simpleXML or xml DOM cannot process all xml strings. Some have error boxes added to the end of them (such as Battlefield Heroes syndicated news). These boxes cause an end of file sort of error and closes out the script. XMLReader grabs data from these strings without error.
japos dot trash at googlemail dot com 09-Oct-2010 11:17
Take care about how to use XMLReader::$isElementEmpty. I don't know if it is a bug or not, but $isElementEmpty is set for the current context and NOT just for the element. If you move your cursor to an attribute, $isElementEmpty will ALWAYS be false.

<?php
 $xml = new XMLReader();
 $xml->XML('<tag attr="value" />');
 $xml->read();
 var_dump($xml->isEmptyElement);
 $xml->moveToNextAttribute();
 var_dump($xml->isEmptyElement);
?>

will output

(bool) true
(bool) false

So be sure to store $isEmptyElement before moving the cursor.
jart (at) mail.ru 23-Sep-2010 03:59
Guys, I hope this example will help
you can erase prints showing the process-
and it will be a piece of nice code.

<?php
function xml2assoc($xml, $name)
{
    print "<ul>";

    $tree = null;
    print("I'm inside " . $name . "<br>");

    while($xml->read())
    {
        if($xml->nodeType == XMLReader::END_ELEMENT)
        {
            print "</ul>";
            return $tree;
        }

        else if($xml->nodeType == XMLReader::ELEMENT)
        {
            $node = array();

            print("Adding " . $xml->name ."<br>");
            $node['tag'] = $xml->name;

            if($xml->hasAttributes)
            {
                $attributes = array();
                while($xml->moveToNextAttribute())
                {
                    print("Adding attr " . $xml->name ." = " . $xml->value . "<br>");
                    $attributes[$xml->name] = $xml->value;
                }
                $node['attr'] = $attributes;
            }

            if(!$xml->isEmptyElement)
            {
                $childs = xml2assoc($xml, $node['tag']);
                $node['childs'] = $childs;
            }

            print($node['tag'] . " added <br>");
            $tree[] = $node;
        }

        else if($xml->nodeType == XMLReader::TEXT)
        {
            $node = array();
            $node['text'] = $xml->value;
            $tree[] = $node;
            print "text added = " . $node['text'] . "<br>";
        }
    }

    print "returning " . count($tree) . " childs<br>";
    print "</ul>";

    return $tree;
}

echo "<PRE>";

$xml = new XMLReader();
$xml->open('test.xml');
$assoc = xml2assoc($xml, "root");
$xml->close();

print_r($assoc);
echo "</PRE>";

?>

It reads this xml:

<test>
    <hallo volume="loud"> me <br/> lala </hallo>
    <hallo> me </hallo>
</test>
jnettles at inccrra dot org 02-Oct-2009 06:51
Just in case someone is confused, if you're wanting to simply pass a string of XML instead of an entire file, you would do this.

<?php
$foo = new XMLReader();
$foo->xml($STRING);
?>

.... where $STRING holds your XML. You cannot pass it like $foo = $STRING or $foo->xml = $STRING.
james dot ellis at example dot com 26-Mar-2009 12:16
The "XML2Assoc" functions noted here should be used with caution... basically they are duplicating the functionality already present in SimpleXML. They may work but they won't scale.

Their are two main uses cases for parsing XML, each suited to either XMLReader or SimpleXML.

1. SimpleXML is an excellent tool for easy access to an XML document tree using native PHP data types. It starts to flounder with massive (> 50M or so) XML documents, as it reads the entire document into memory before it can be processed. SimpleXML will just laugh at you then die when your server runs out of memory (or it will cause a load spike).

2. Aside from the reasoning behind massive XML documents, if you have to deal with massive XML documents, use XMLReader to process them. Don't try and gather an entire XML document into a PHP data structure using XMLReader and a PHP xml2assoc() function, you are reinventing the SimpleXML wheel.
When parsing massive XML documents using XMLReader, gather the data you need to perform an operation then perform it before skipping to the next node. Do not build massive data structures from a massive XML document, your server (and it's admins) will not like you.
PxL 23-Jan-2009 12:36
A basic parser

<?php
function xml2assoc($xml) {
    $arr = array();
    if (!preg_match_all('|\<\s*?(\w+).*?\>(.*)\<\/\s*\\1.*?\>|s', $xml, $m)) return $xml;
    if (is_array($m[1]))
        for ($i = 0;$i < sizeof($m[1]); $i++) $arr[$m[1][$i]] = xml2assoc($m[2][$i]);
    else $arr[$m[1]] = xml2assoc($m[2]);

    return $arr;
}
?>
boukeversteegh at gmail dot com 18-Jan-2009 11:06
XML to ASSOCIATIVE ARRAY

Improved algorithm based on Sergey Aikinkulov's. The problem was that it would overwrite nodes if they had the same tag name. Because of that <a><b/><b/><a> would be read as if <a><b/><a/>. This algorithm handles it better and outputs an easy to understand array:

<?php
function xml2assoc($xml) {
    $tree = null;
    while($xml->read())
        switch ($xml->nodeType) {
            case XMLReader::END_ELEMENT: return $tree;
            case XMLReader::ELEMENT:
                $node = array('tag' => $xml->name, 'value' => $xml->isEmptyElement ? '' : xml2assoc($xml));
                if($xml->hasAttributes)
                    while($xml->moveToNextAttribute())
                        $node['attributes'][$xml->name] = $xml->value;
                $tree[] = $node;
            break;
            case XMLReader::TEXT:
            case XMLReader::CDATA:
                $tree .= $xml->value;
        }
    return $tree;
}

?>

Usage:

myxml.xml:
------
<PERSON>
    <NAME>John</NAME>
    <PHONE type="home">555-555-555</PHONE>
</PERSON>
----

<?
    $xml = new XMLReader();
    $xml->open('myxml.xml');
    $assoc = xml2assoc($xml);
    $xml->close();
    print_r($assoc);
?>

Outputs:
Array
(
    [0] => Array
        (
            [tag] => PERSON
            [value] => Array
                (
                    [0] => Array
                        (
                            [tag] => NAME
                            [value] => John
                        )

                    [1] => Array
                        (
                            [tag] => PHONE
                            [value] => 555-555-555
                            [attributes] => Array
                                (
                                    [type] => home
                                )

                        )

                )

        )

)

For reasons that have to do with recursion, it returns an array with the ROOT xml node as the first childNode, rather than to return only the ROOT node.
andrei_antal at yahoo dot com 08-Jan-2009 05:25
<?php
//Pull certain elements
 $reader = new XMLReader();
  $reader->open($xmlfile);
while ($reader->read()) {
 switch ($reader->nodeType) {
   case (XMLREADER::ELEMENT):

if ($reader->name == "Code")
     {
       $reader->read();
       $code = trim($reader->value);
       echo "$code\n";
       break;
     }

 if ($reader->name == "Name")
     {
       $reader->read();
       $customername = trim( $reader->value );
       echo "$name\n";
       break;
     }

 if ($reader->name == "Camp")
    {
      $camp = trim($reader->getAttribute("ID"));
       echo "$camp\n";
      break;
    }
  }
}
?>
godseth at o2 dot pl 28-Nov-2008 11:34
Thanks rein_baarsma33 AT hotmail DOT com for bugfixes.

This is my new child of XML parsing method  based on my and yours modification.

XML2ASSOC Is a complete solution for parsing ordinary XML

<?php
/**
 * XML2Assoc Class to creating
 * PHP Assoc Array from XML File
 *
 * @author godseth (AT) o2.pl & rein_baarsma33 (AT) hotmail.com (Bugfixes in parseXml Method)
 * @uses XMLReader
 *
 */

class Xml2Assoc {

    /**
     * Optimization Enabled / Disabled
     *
     * @var bool
     */
    protected $bOptimize = false;

    /**
     * Method for loading XML Data from String
     *
     * @param string $sXml
     * @param bool $bOptimize
     */

    public function parseString( $sXml , $bOptimize = false) {
        $oXml = new XMLReader();
        $this -> bOptimize = (bool) $bOptimize;
        try {

            // Set String Containing XML data
            $oXml->XML($sXml);

            // Parse Xml and return result
            return $this->parseXml($oXml);

        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }

    /**
     * Method for loading Xml Data from file
     *
     * @param string $sXmlFilePath
     * @param bool $bOptimize
     */
    public function parseFile( $sXmlFilePath , $bOptimize = false ) {
        $oXml = new XMLReader();
        $this -> bOptimize = (bool) $bOptimize;
        try {
            // Open XML file
            $oXml->open($sXmlFilePath);

            // // Parse Xml and return result
            return $this->parseXml($oXml);

        } catch (Exception $e) {
            echo $e->getMessage(). ' | Try open file: '.$sXmlFilePath;
        }
    }

    /**
     * XML Parser
     *
     * @param XMLReader $oXml
     * @return array
     */
    protected function parseXml( XMLReader $oXml ) {

        $aAssocXML = null;
        $iDc = -1;

        while($oXml->read()){
            switch ($oXml->nodeType) {

                case XMLReader::END_ELEMENT:

                    if ($this->bOptimize) {
                        $this->optXml($aAssocXML);
                    }
                    return $aAssocXML;

                case XMLReader::ELEMENT:

                    if(!isset($aAssocXML[$oXml->name])) {
                        if($oXml->hasAttributes) {
                            $aAssocXML[$oXml->name][] = $oXml->isEmptyElement ? '' : $this->parseXML($oXml);
                        } else {
                            if($oXml->isEmptyElement) {
                                $aAssocXML[$oXml->name] = '';
                            } else {
                                $aAssocXML[$oXml->name] = $this->parseXML($oXml);
                            }
                        }
                    } elseif (is_array($aAssocXML[$oXml->name])) {
                        if (!isset($aAssocXML[$oXml->name][0]))
                        {
                            $temp = $aAssocXML[$oXml->name];
                            foreach ($temp as $sKey=>$sValue)
                            unset($aAssocXML[$oXml->name][$sKey]);
                            $aAssocXML[$oXml->name][] = $temp;
                        }

                        if($oXml->hasAttributes) {
                            $aAssocXML[$oXml->name][] = $oXml->isEmptyElement ? '' : $this->parseXML($oXml);
                        } else {
                            if($oXml->isEmptyElement) {
                                $aAssocXML[$oXml->name][] = '';
                            } else {
                                $aAssocXML[$oXml->name][] = $this->parseXML($oXml);
                            }
                        }
                    } else {
                        $mOldVar = $aAssocXML[$oXml->name];
                        $aAssocXML[$oXml->name] = array($mOldVar);
                        if($oXml->hasAttributes) {
                            $aAssocXML[$oXml->name][] = $oXml->isEmptyElement ? '' : $this->parseXML($oXml);
                        } else {
                            if($oXml->isEmptyElement) {
                                $aAssocXML[$oXml->name][] = '';
                            } else {
                                $aAssocXML[$oXml->name][] = $this->parseXML($oXml);
                            }
                        }
                    }

                    if($oXml->hasAttributes) {
                        $mElement =& $aAssocXML[$oXml->name][count($aAssocXML[$oXml->name]) - 1];
                        while($oXml->moveToNextAttribute()) {
                            $mElement[$oXml->name] = $oXml->value;
                        }
                    }
                    break;
                case XMLReader::TEXT:
                case XMLReader::CDATA:

                    $aAssocXML[++$iDc] = $oXml->value;

            }
        }

        return $aAssocXML;
    }

    /**
     * Method to optimize assoc tree.
     * ( Deleting 0 index when element
     *  have one attribute / value )
     *
     * @param array $mData
     */
    public function optXml(&$mData) {
        if (is_array($mData)) {
            if (isset($mData[0]) && count($mData) == 1 ) {
                $mData = $mData[0];
                if (is_array($mData)) {
                    foreach ($mData as &$aSub) {
                        $this->optXml($aSub);
                    }
                }
            } else {
                foreach ($mData as &$aSub) {
                    $this->optXml($aSub);
                }
            }
        }
    }

}

?>

[EDIT BY danbrown AT php DOT net:  Fixes were also provided by "Alex" and (qdog AT qview DOT org) in user notes on this page (since removed).]
Sergey Aikinkulov 19-Jun-2008 12:51
Next version xml2assoc with some improve fixes:
 - no doubled data
 - no buffer arrays

<?php
/*
    Read XML structure to associative array
    --
    Using:
    $xml = new XMLReader();
    $xml->open([XML file]);
    $assoc = xml2assoc($xml);
    $xml->close();
*/
    function xml2assoc($xml) {
      $assoc = null;
      while($xml->read()){
        switch ($xml->nodeType) {
          case XMLReader::END_ELEMENT: return $assoc;
          case XMLReader::ELEMENT:
            $assoc[$xml->name][] = array('value' => $xml->isEmptyElement ? '' : xml2assoc($xml));
            if($xml->hasAttributes){
              $el =& $assoc[$xml->name][count($assoc[$xml->name]) - 1];
              while($xml->moveToNextAttribute()) $el['attributes'][$xml->name] = $xml->value;
            }
            break;
          case XMLReader::TEXT:
          case XMLReader::CDATA: $assoc .= $xml->value;
        }
      }
      return $assoc;
    }
?>
desk_ocean at msn dot com 16-Mar-2008 07:03
make some modify from Sergey Aikinkulov's note

<?php
function xml2assoc(&$xml){
    $assoc = NULL;
    $n = 0;
    while($xml->read()){
        if($xml->nodeType == XMLReader::END_ELEMENT) break;
        if($xml->nodeType == XMLReader::ELEMENT and !$xml->isEmptyElement){
            $assoc[$n]['name'] = $xml->name;
            if($xml->hasAttributes) while($xml->moveToNextAttribute()) $assoc[$n]['atr'][$xml->name] = $xml->value;
            $assoc[$n]['val'] = xml2assoc($xml);
            $n++;
        }
        else if($xml->isEmptyElement){
            $assoc[$n]['name'] = $xml->name;
            if($xml->hasAttributes) while($xml->moveToNextAttribute()) $assoc[$n]['atr'][$xml->name] = $xml->value;
            $assoc[$n]['val'] = "";
            $n++;
        }
        else if($xml->nodeType == XMLReader::TEXT) $assoc = $xml->value;
    }
    return $assoc;
}
?>

add else if($xml->isEmptyElement)
may be some xml has emptyelement
itari 15-Feb-2008 05:30
<?php
function parseXML($node,$seq,$path) {
global $oldpath;
    if (!$node->read())
      return;
    if ($node->nodeType != 15) {
      print '<br/>'.$node->depth;
      print '-'.$seq++;
      print '  '.$path.'/'.($node->nodeType==3?'text() = ':$node->name);
      print $node->value;
      if ($node->hasAttributes) {
        print ' [hasAttributes: ';
        while ($node->moveToNextAttribute()) print '@'.$node->name.' = '.$node->value.' ';
        print ']';
        }
      if ($node->nodeType == 1) {
        $oldpath=$path;
        $path.='/'.$node->name;
        }
      parseXML($node,$seq,$path);
      }
    else parseXML($node,$seq,$oldpath);
}

$source = "<tag1>this<tag2 id='4' name='foo'>is</tag2>a<tag2 id='5'>common</tag2>record</tag1>";
$xml = new XMLReader();
$xml->XML($source);
print htmlspecialchars($source).'<br/>';
parseXML($xml,0,'');
?>

Output:

<tag1>this<tag2 id='4' name='foo'>is</tag2>a<tag2 id='5'>common</tag2>record</tag1>

0-0 /tag1
1-1 /tag1/text() = this
1-2 /tag1/tag2 [hasAttributes: @id = 4 @name = foo ]
2-3 /tag1/text() = is
1-4 /text() = a
1-5 /tag2 [hasAttributes: @id = 5 ]
2-6 /text() = common
1-7 /text() = record
orion at ftf-hq dot dk 15-Feb-2006 01:50
Some more documentation (i.e. examples) would be nice :-)

This is how I read some mysql parameters in an xml file:

<?php
    $xml = new XMLReader();
    $xml->open("config.xml");
    $xml->setParserProperty(2,true); // This seems a little unclear to me - but it worked :)

    while ($xml->read()) {
        switch ($xml->name) {
        case "mysql_host":
            $xml->read();
            $conf["mysql_host"] = $xml->value;
            $xml->read();
            break;
        case "mysql_username":
            $xml->read();
            $conf["mysql_user"] = $xml->value;
            $xml->read();
            break;
        case "mysql_password":
            $xml->read();
            $conf["mysql_pass"] = $xml->value;
            $xml->read();
            break;
        case "mysql_database":
            $xml->read();
            $conf["mysql_db"] = $xml->value;
            $xml->read();
            break;
        }
    }

    $xml->close();
?>

The XML file used:
<?xml version='1.0'?>
<MySQL_INIT>
   <mysql_host>localhost</mysql_host>
   <mysql_database>db_database</mysql_database>
   <mysql_username>root</mysql_username>
   <mysql_password>password</mysql_password>
</MySQL_INIT>


Close the XMLReader input

  • ≪ XMLReader
  • XMLReader::expand ≫
  • PHP Manual
  • XMLReader
  • Close the XMLReader input

XMLReader::close

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::close — Close the XMLReader input

설명

public bool XMLReader::close ( void )

Closes the input the XMLReader object is currently parsing.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::open() - Set the URI containing the XML to parse
  • XMLReader::xml() - Set the data containing the XML to parse

User Contributed Notes

There are no user contributed notes for this page.


Returns a copy of the current node as a DOM object

  • ≪ XMLReader::close
  • XMLReader::getAttribute ≫
  • PHP Manual
  • XMLReader
  • Returns a copy of the current node as a DOM object

XMLReader::expand

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::expand — Returns a copy of the current node as a DOM object

설명

public DOMNode XMLReader::expand ([ DOMNode $basenode ] )

This method copies the current node and returns the appropriate DOM object.

인수

basenode

A DOMNode defining the target DOMDocument for the created DOM object.

반환값

The resulting DOMNode or FALSE on error.

변경점

버전 | 설명
5.3.0 | The parameter basenode was added.

User Contributed Notes

There are no user contributed notes for this page.


Get the value of a named attribute

  • ≪ XMLReader::expand
  • XMLReader::getAttributeNo ≫
  • PHP Manual
  • XMLReader
  • Get the value of a named attribute

XMLReader::getAttribute

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::getAttribute — Get the value of a named attribute

설명

public string XMLReader::getAttribute ( string $name )

Returns the value of a named attribute or NULL if the attribute does not exist or not positioned on an element node.

인수

name

The name of the attribute.

반환값

The value of the attribute, or NULL if no attribute with the given name is found or not positioned on an element node.

변경점

버전 | 설명
5.1.3 | Return NULL if no attribute found. Previously, returned an empty string.

참고

  • XMLReader::getAttributeNo() - Get the value of an attribute by index
  • XMLReader::getAttributeNs() - Get the value of an attribute by localname and URI

User Contributed Notes

Anonymous 16-Feb-2011 06:02
Or use XMLReader::ELEMENT and XMLReader::END_ELEMENT rather than the numbers 1 and 15.
Gabi 02-Jul-2007 05:15
might be obvious, but not to everyone ;-) ... when reading attributes from a node that has sub-nodes (and creating an output from this node), the output will be issued twice, once on the <ELEMENT> tag and once on the end tag </ELEMENT>. To avoid this, you can test on which part of the node you are using the property nodeType.
It'll be 1 for the element, 15 for the end element.


Get the value of an attribute by index

  • ≪ XMLReader::getAttribute
  • XMLReader::getAttributeNs ≫
  • PHP Manual
  • XMLReader
  • Get the value of an attribute by index

XMLReader::getAttributeNo

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::getAttributeNo — Get the value of an attribute by index

설명

public string XMLReader::getAttributeNo ( int $index )

Returns the value of an attribute based on its position or an empty string if attribute does not exist or not positioned on an element node.

인수

index

The position of the attribute.

반환값

The value of the attribute, or an empty string (before PHP 5.6) or NULL (from PHP 5.6 onwards) if no attribute exists at index or is not positioned on the element.

변경점

버전 | 설명
5.6.0 | XMLReader::getAttributeNo() now returns NULL if the attribute doesn't exist.

참고

  • XMLReader::getAttribute() - Get the value of a named attribute
  • XMLReader::getAttributeNs() - Get the value of an attribute by localname and URI

User Contributed Notes

There are no user contributed notes for this page.


Get the value of an attribute by localname and URI

  • ≪ XMLReader::getAttributeNo
  • XMLReader::getParserProperty ≫
  • PHP Manual
  • XMLReader
  • Get the value of an attribute by localname and URI

XMLReader::getAttributeNs

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::getAttributeNs — Get the value of an attribute by localname and URI

설명

public string XMLReader::getAttributeNs ( string $localName , string $namespaceURI )

Returns the value of an attribute by name and namespace URI or an empty string if attribute does not exist or not positioned on an element node.

인수

localName

The local name.

namespaceURI

The namespace URI.

반환값

The value of the attribute, or an empty string (before PHP 5.6) or NULL (from PHP 5.6 onwards) if no attribute with the given localName and namespaceURI is found or not positioned of element.

변경점

버전 | 설명
5.6.0 | XMLReader::getAttributeNS() now returns NULL if the attribute doesn't exist.

참고

  • XMLReader::getAttribute() - Get the value of a named attribute
  • XMLReader::getAttributeNo() - Get the value of an attribute by index

User Contributed Notes

There are no user contributed notes for this page.


Indicates if specified property has been set

  • ≪ XMLReader::getAttributeNs
  • XMLReader::isValid ≫
  • PHP Manual
  • XMLReader
  • Indicates if specified property has been set

XMLReader::getParserProperty

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::getParserProperty — Indicates if specified property has been set

설명

public bool XMLReader::getParserProperty ( int $property )

Indicates if specified property has been set.

인수

property

One of the parser option constants.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::setParserProperty() - Set parser options

User Contributed Notes

There are no user contributed notes for this page.


Indicates if the parsed document is valid

  • ≪ XMLReader::getParserProperty
  • XMLReader::lookupNamespace ≫
  • PHP Manual
  • XMLReader
  • Indicates if the parsed document is valid

XMLReader::isValid

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::isValid — Indicates if the parsed document is valid

설명

public bool XMLReader::isValid ( void )

Returns a boolean indicating if the document being parsed is currently valid.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 Validating XML

<?php
$xml = XMLReader::open('test.xml');

// The validate parser option must be enabled for
// this method to work properly
$xml->setParserProperty(XMLReader::VALIDATE, true);

var_dump($xml->isValid());
?>

주의

Note: This checks the current node, not the entire document.

참고

  • XMLReader::setParserProperty() - Set parser options
  • XMLReader::setRelaxNGSchema() - Set the filename or URI for a RelaxNG Schema
  • XMLReader::setRelaxNGSchemaSource() - Set the data containing a RelaxNG Schema
  • XMLReader::setSchema() - Validate document against XSD

User Contributed Notes

zubin at trattonuovo dot com 20-Jan-2010 12:00
I encountered some problems to use isValid method with xml2assoc function.
I use this way to validate the entire xml file and put it into an associative array.

$xml = new XMLReader();
if (!$xml->xml($xml_string, NULL, LIBXML_DTDVALID)) {
  echo "XML not valid: load error";
  exit();
}

libxml_use_internal_errors(TRUE);

$xml_array = xml2assoc($xml);

$arErrors = libxml_get_errors();
$xml_errors = "";
foreach ($arErrors AS $xmlError) $xml_errors .= $xmlError->message;
if ($xml_errors != "") {
  echo "XML not valid: ".$xml_errors;
  exit();
}

//all ok
remy dot damour at laposte dot net 31-Dec-2008 12:27
1. If you validate against relax-ng, no need to call $xml->setParserProperty(XMLReader::VALIDATE, true);

2. Be aware that $xml->isValid() will return validity for currently active node (ie. node currently positioned using $xml->read()). It won't check validity of your entire tree at once, but rather on a step by step basis
anzenews at volja dot net 22-Jan-2008 07:50
This comment is only partially correct:
"isValid() always returns false unless you enable checking for validity by $reader->setParserProperty(XMLReader::VALIDATE, true);"
This enables DTD checking, but you can also check by using RelaxNG (see setRelaxNGSchema() and setRelaxNGSchemaSource()).

And also, this is NOT correct:
"If you just need to check if XML file is well formed, successful loading into XMLReader object is usually enough."
It is not enough. Pull parsers operate on stream and if you have a large enough file they will not know it is well formed until it is read to the end. If you need to know if it is well formed or/and valid, read it till the end or validation error (you can use next() for fast reading if you don't care about contents).


Lookup namespace for a prefix

  • ≪ XMLReader::isValid
  • XMLReader::moveToAttribute ≫
  • PHP Manual
  • XMLReader
  • Lookup namespace for a prefix

XMLReader::lookupNamespace

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::lookupNamespace — Lookup namespace for a prefix

설명

public string XMLReader::lookupNamespace ( string $prefix )

Lookup in scope namespace for a given prefix.

인수

prefix

String containing the prefix.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

There are no user contributed notes for this page.


Move cursor to a named attribute

  • ≪ XMLReader::lookupNamespace
  • XMLReader::moveToAttributeNo ≫
  • PHP Manual
  • XMLReader
  • Move cursor to a named attribute

XMLReader::moveToAttribute

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::moveToAttribute — Move cursor to a named attribute

설명

public bool XMLReader::moveToAttribute ( string $name )

Positions cursor on the named attribute.

인수

name

The name of the attribute.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::moveToElement() - Position cursor on the parent Element of current Attribute
  • XMLReader::moveToAttributeNo() - Move cursor to an attribute by index
  • XMLReader::moveToAttributeNs() - Move cursor to a named attribute
  • XMLReader::moveToFirstAttribute() - Position cursor on the first Attribute

User Contributed Notes

There are no user contributed notes for this page.


Move cursor to an attribute by index

  • ≪ XMLReader::moveToAttribute
  • XMLReader::moveToAttributeNs ≫
  • PHP Manual
  • XMLReader
  • Move cursor to an attribute by index

XMLReader::moveToAttributeNo

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::moveToAttributeNo — Move cursor to an attribute by index

설명

public bool XMLReader::moveToAttributeNo ( int $index )

Positions cursor on attribute based on its position.

인수

index

The position of the attribute.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::moveToElement() - Position cursor on the parent Element of current Attribute
  • XMLReader::moveToAttribute() - Move cursor to a named attribute
  • XMLReader::moveToAttributeNs() - Move cursor to a named attribute
  • XMLReader::moveToFirstAttribute() - Position cursor on the first Attribute

User Contributed Notes

There are no user contributed notes for this page.


Move cursor to a named attribute

  • ≪ XMLReader::moveToAttributeNo
  • XMLReader::moveToElement ≫
  • PHP Manual
  • XMLReader
  • Move cursor to a named attribute

XMLReader::moveToAttributeNs

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::moveToAttributeNs — Move cursor to a named attribute

설명

public bool XMLReader::moveToAttributeNs ( string $localName , string $namespaceURI )

Positions cursor on the named attribute in specified namespace.

인수

localName

The local name.

namespaceURI

The namespace URI.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::moveToElement() - Position cursor on the parent Element of current Attribute
  • XMLReader::moveToAttribute() - Move cursor to a named attribute
  • XMLReader::moveToAttributeNo() - Move cursor to an attribute by index
  • XMLReader::moveToFirstAttribute() - Position cursor on the first Attribute

User Contributed Notes

There are no user contributed notes for this page.


Position cursor on the parent Element of current Attribute

  • ≪ XMLReader::moveToAttributeNs
  • XMLReader::moveToFirstAttribute ≫
  • PHP Manual
  • XMLReader
  • Position cursor on the parent Element of current Attribute

XMLReader::moveToElement

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::moveToElement — Position cursor on the parent Element of current Attribute

설명

public bool XMLReader::moveToElement ( void )

Moves cursor to the parent Element of current Attribute.

반환값

Returns TRUE if successful and FALSE if it fails or not positioned on Attribute when this method is called.

참고

  • XMLReader::moveToAttribute() - Move cursor to a named attribute
  • XMLReader::moveToAttributeNo() - Move cursor to an attribute by index
  • XMLReader::moveToAttributeNs() - Move cursor to a named attribute
  • XMLReader::moveToFirstAttribute() - Position cursor on the first Attribute

User Contributed Notes

There are no user contributed notes for this page.


Position cursor on the first Attribute

  • ≪ XMLReader::moveToElement
  • XMLReader::moveToNextAttribute ≫
  • PHP Manual
  • XMLReader
  • Position cursor on the first Attribute

XMLReader::moveToFirstAttribute

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::moveToFirstAttribute — Position cursor on the first Attribute

설명

public bool XMLReader::moveToFirstAttribute ( void )

Moves cursor to the first Attribute.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::moveToElement() - Position cursor on the parent Element of current Attribute
  • XMLReader::moveToAttribute() - Move cursor to a named attribute
  • XMLReader::moveToAttributeNo() - Move cursor to an attribute by index
  • XMLReader::moveToAttributeNs() - Move cursor to a named attribute
  • XMLReader::moveToNextAttribute() - Position cursor on the next Attribute

User Contributed Notes

There are no user contributed notes for this page.


Position cursor on the next Attribute

  • ≪ XMLReader::moveToFirstAttribute
  • XMLReader::next ≫
  • PHP Manual
  • XMLReader
  • Position cursor on the next Attribute

XMLReader::moveToNextAttribute

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::moveToNextAttribute — Position cursor on the next Attribute

설명

public bool XMLReader::moveToNextAttribute ( void )

Moves cursor to the next Attribute if positioned on an Attribute or moves to first attribute if positioned on an Element.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::moveToElement() - Position cursor on the parent Element of current Attribute
  • XMLReader::moveToAttribute() - Move cursor to a named attribute
  • XMLReader::moveToAttributeNo() - Move cursor to an attribute by index
  • XMLReader::moveToAttributeNs() - Move cursor to a named attribute
  • XMLReader::moveToFirstAttribute() - Position cursor on the first Attribute

User Contributed Notes

There are no user contributed notes for this page.


Move cursor to next node skipping all subtrees

  • ≪ XMLReader::moveToNextAttribute
  • XMLReader::open ≫
  • PHP Manual
  • XMLReader
  • Move cursor to next node skipping all subtrees

XMLReader::next

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::next — Move cursor to next node skipping all subtrees

설명

public bool XMLReader::next ([ string $localname ] )

Positions cursor on the next node skipping all subtrees.

인수

localname

The name of the next node to move to.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::moveToNextAttribute() - Position cursor on the next Attribute
  • XMLReader::moveToElement() - Position cursor on the parent Element of current Attribute
  • XMLReader::moveToAttribute() - Move cursor to a named attribute

User Contributed Notes

ppp dot BOTSNEEDNOTAPPLY at salesfloor dot net 09-Sep-2015 06:48
This method appears to follow these rules:

 - if $localName names a sibling node, the cursor is moved to that node;
 - if $localName names an ancestor node, the cursor is moved to the end of that node;
 - if $localName names a node that is a sibling of any of the current node's ancestors, the cursor is moved to that node;
 - otherwise the cursor is moved outside the document.

Note especially that this method never moves the cursor to child nodes.

For example, given this XML document

<?xml version="1.0" encoding="UTF-8"?>
<root id="root" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <aaa id="1">
    <bbb id="1.1"></bbb>
    <bbb id="1.2"></bbb>
  </aaa>
  <ccc id="2" />
  <aaa id="3">
    <ddd id="3.1">
      <aaa id="3.1.1"></aaa>
    </ddd>
    <aaa id="3.2"></aaa>
  </aaa>
  <aaa id="4">
    <eee id="4.1"></eee>
  </aaa>
</root>

going from <root id="root"> to "bbb" places the cursor outside the document;
going from <aaa id="1"> to "bbb" places the cursor outside the document;
going from <aaa id="1"> to "aaa" places the cursor on <aaa id="3">;
going from <bbb id="1.1"> to "bbb" places the cursor on <bbb id="1.2">;
going from <bbb id="1.2"> to "bbb" places the cursor outside the document;
going from <bbb id="1.1"> to "ddd" places the cursor outside the document;
going from <bbb id="1.1"> to "aaa" places the cursor on </aaa>;
going from <bbb id="1.1"> to "ccc" places the cursor on <ccc id="2">;
going from <bbb id="1.1"> to "nonsuch" places the cursor outside the document;
going from <bbb id="1.1"> to "root" places the cursor on </root>;
going from <ddd id="3.1"> to "aaa" places the cursor on <aaa id="3.2">;
going from <ddd id="3.1"> to "eee" places the cursor outside the document;

Try it yourself:

<?php

$document = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<root id="root" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <aaa id="1">
    <bbb id="1.1"></bbb>
    <bbb id="1.2"></bbb>
  </aaa>
  <ccc id="2" />
  <aaa id="3">
    <ddd id="3.1">
      <aaa id="3.1.1"></aaa>
    </ddd>
    <aaa id="3.2"></aaa>
  </aaa>
  <aaa id="4">
    <eee id="4.1"></eee>
  </aaa>
</root>
XML;

$filename = "/tmp/xmlreader.php.xml";
file_put_contents($filename, $document);

echo "given this XML document\n\n$document\n\n";

showNext("root", "bbb");
showNext("1", "bbb");
showNext("1", "aaa");
showNext("1.1", "bbb");
showNext("1.2", "bbb");
showNext("1.1", "ddd");
showNext("1.1", "aaa");
showNext("1.1", "ccc");
showNext("1.1", "nonsuch");
showNext("1.1", "root");
showNext("3.1", "aaa");
showNext("3.1", "eee");

function showNext($from, $to) {
  global $filename;

  $xml = new \XmlReader();
  $xml->open("file://$filename");
  while ($xml->read()) {
    if ($xml->nodeType === \XmlReader::ELEMENT) {
      if ($xml->getAttribute("id") == $from) {
        echo "going from <$xml->name id=\"$from\">";
        break;
      }
    }
  }

  $xml->next($to);
  $destination = "";
  if($xml->nodeType === \XmlReader::NONE) {
    if(!$xml->read()) {
      $destination = "outside the document";
    }
  }

  if(!$destination) {
    if ($xml->nodeType === \XmlReader::END_ELEMENT) {
      $destination = "on </$xml->name>";
    } else if ($xml->nodeType === \XmlReader::ELEMENT) {
      $destination = "on <$xml->name id=\"" . $xml->getAttribute("id") . "\">";
    }
  }

  echo " to \"$to\" places the cursor $destination;\n";

  $xml->close();
}
?>
gholson19 at gmail dot com 14-May-2015 06:21
To skip over deletion nodes in the xml extracted from a word document, do something like this:

if ($paragraph->nodeType == XMLREADER::ELEMENT && $paragraph->name === 'w:del'){$paragraph->next();}


Set the URI containing the XML to parse

  • ≪ XMLReader::next
  • XMLReader::read ≫
  • PHP Manual
  • XMLReader
  • Set the URI containing the XML to parse

XMLReader::open

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::open — Set the URI containing the XML to parse

설명

public bool XMLReader::open ( string $URI [, string $encoding [, int $options = 0 ]] )

Set the URI containing the XML document to be parsed.

인수

URI

URI pointing to the document.

encoding

The document encoding or NULL.

options

A bitmask of the LIBXML_* constants.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다. If called statically, returns an XMLReader실패 시 FALSE를 반환합니다.

오류/예외

이 메소드는 정적으로 호출할 수있지만, E_STRICT 오류가 발생합니다.

변경점

버전 | 설명
5.2.0 | encoding and options were added.

참고

  • XMLReader::xml() - Set the data containing the XML to parse
  • XMLReader::close() - Close the XMLReader input

User Contributed Notes

mood(_a_)twolate.com 14-Oct-2016 08:03
For some reasons, the open() method keep throwing me this error :

PHP Warning:  XMLReader::open(): Unable to open source data in /var/www/nota/ethamap/fat_xml.php

It doesn't make sense as the xml file target hosted on my server is perfectly reachable. Adding this line before invoking open() fixed it :

libxml_disable_entity_loader(false);

Please view https://bugs.php.net/bug.php?id=62577
It is somehow related.
alvaro at demogracia dot com 14-Nov-2014 09:35
XML can optionally declare its own encoding:

    <?xml version="1.0" encoding="UTF-8"?>

You can use the $encoding parameter to provide this information (if missing) or override it (if wrong).

Output is always UTF-8 (that's how libxml works).
crungmungus at gmail dot com 16-Sep-2008 02:11
Windows users remember to enable php_openssl.dll in your php.ini if you want to be able to use this function (and others) with a HTTPS URL.


Move to next node in document

  • ≪ XMLReader::open
  • XMLReader::readInnerXML ≫
  • PHP Manual
  • XMLReader
  • Move to next node in document

XMLReader::read

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::read — Move to next node in document

설명

public bool XMLReader::read ( void )

Moves cursor to the next node in the document.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::moveToElement() - Position cursor on the parent Element of current Attribute
  • XMLReader::moveToAttribute() - Move cursor to a named attribute
  • XMLReader::next() - Move cursor to next node skipping all subtrees

User Contributed Notes

peter at 3xe dot co dot uk 13-Oct-2016 09:08
Another approach to the 'also reads closing tags' gotcha:

<?php
$reader = new XMLReader();
$reader->open('users.xml');

while ($reader->read()) {
  if ($reader->nodeType == XMLReader::END_ELEMENT) {
    continue; //skips the rest of the code in this iteration
  }
  //do something with desired node type
  if($reader->name == 'user') {
    //...
  }
}
?>
Anonymous 20-Mar-2015 06:49
> I would have liked to use the next() function instead, but as I needed to parse 2 different subtrees, I couldn't figure out how to find all the columns, reset the pointer, and then find all the rows.

I just use:

$reader->close();
$reader->open($url);

to reset the pointer.
ecziegler at gmail 29-Jan-2014 09:27
If like myself you have been turning the interwebz upside down looking for a solution for this issue:
PHP Warning:  XMLReader::read(): /tmp/xml_feed.xml:4183934: parser error : Input is not proper UTF-8, indicate encoding !

For some reason, this warning breaks the execution - is it a fatal error in disguise?

After days of frustration I found it!!!!
tidy -xml -o output.xml -utf8 -f error.log input.xml

You can invoque tidy using exec, It takes several seconds to convert a 250Mb feed, but it worthy the time.

In my case the issue was with latin1 charset, and for some reason I had to pass the xml through tidy 2 times - first time around creates new errors, second time it fixes everything.

I know invalid xml should be fixed by xml creators, but it works differently in the real world.
Nate 27-Jun-2011 07:49
It is interesting to note that this function will stop on closing tags as well.  I have an XML document similar to the following:

<root>
  <columns>
    <column>columnX</column>
    <column>columnY</column>
  </columns>
  <table>
    <row>
      <columnX>38</columnX>
      <columnY>50</columnY>
    </row>
    <row>
      <columnX>82</columnY>
      <columnY>28</columnY>
    </row>
    ...
  </table>
</root>

I need to parse the <columns> object to know what attributes to check for from each <row> node.  Therefore I was doing the following:

<?php
while ($xml->read()) {
  if ($xml->name === 'column') {
   //parse column node to into $columns array
  }
  elseif ($xml->name === 'row') {
    //parse row node, using constructed $columns array
  }
}
?>

This kind of worked in that I ended up with an array of all the data I wanted, but the array I constructed was twice as large as I expected and every other entry was empty.  Took me a while to debug, but finally figured out that checking <?php $xml->name === 'row' ?> matches both <row> and </row>, so the check should really be something more like:

<?php

if ($xml->name === 'row' && $xml->nodeType == XMLReader::ELEMENT) {
  // parse row node
}

?>

I would have liked to use the next() function instead, but as I needed to parse 2 different subtrees, I couldn't figure out how to find all the columns, reset the pointer, and then find all the rows.
andy at siliconrockstar dot com 16-May-2011 10:01
A very simple way to avoid typing $reader->read() multiple times when you want to skip some nodes:

<?php
class smartXMLReader extends XMLReader {
    public function readTimes($count) {
        $i = 0;
        while($i < $count){
            $this->read();
            $i++;
        }
    }
}
?>

Using this smartXMLReader,

<?php
$reader->readTimes(3);
?>

is equivalent to

<?php
$reader->read();
$reader->read();
$reader->read();
?>

Makes getting around in your XML document a bit easier :)
Yannik 10-Nov-2009 12:38
If you have trouble parsing xml documents with text tags bigger than 10MB, this might help you:

<?php
$xmlreader = new XMLReader();
$xmlreader->open($uri, null, 1<<19);
?>

Since in libxml there is a constant
XML_PARSE_HUGE = 1<<19
which enables parsing xml documents with huge texts.

Please note that this option is not accessible using the setParserProperty method and that there is currently no constant to do the work, which will hopefully change soon:

http://bugs.php.net/bug.php?id=49660
jirka at kosek dot cz 08-Feb-2006 10:01
libxml2 contains much more useful method readString() that will read and return whole text content of element. You can call it after receiving start tag (XMLReader::ELEMENT). You can use this PHP code to emulate this method until PHP will directly call underlying libxml2 implementation.

<?php
class XMLReader2 extends XMLReader
{
  function readString()
  {
        $depth = 1;
        $text = "";

        while ($this->read() && $depth != 0)
        {
            if (in_array($this->nodeType, array(XMLReader::TEXT, XMLReader::CDATA, XMLReader::WHITESPACE, XMLReader::SIGNIFICANT_WHITESPACE)))
                $text .= $this->value;
            if ($this->nodeType == XMLReader::ELEMENT) $depth++;
            if ($this->nodeType == XMLReader::END_ELEMENT) $depth--;
        }
        return $text;
    }
}
?>

Just use XMLReader2 instead of XMLReader.


Retrieve XML from current node

  • ≪ XMLReader::read
  • XMLReader::readOuterXML ≫
  • PHP Manual
  • XMLReader
  • Retrieve XML from current node

XMLReader::readInnerXML

(PHP 5 >= 5.2.0, PHP 7)

XMLReader::readInnerXML — Retrieve XML from current node

설명

public string XMLReader::readInnerXML ( void )

Reads the contents of the current node, including child nodes and markup.

인수

이 함수는 인수가 없습니다.

반환값

Returns the contents of the current node as a string. Empty string on failure.

주의

Caution

이 함수는 PHP가 libxml 20620 이후와 함께 컴파일 되었을 때만 사용할 수 있습니다.

참고

  • XMLReader::readString() - Reads the contents of the current node as a string
  • XMLReader::readOuterXML() - Retrieve XML from current node, including itself
  • XMLReader::expand() - Returns a copy of the current node as a DOM object

User Contributed Notes

Evert Pot 21-Mar-2015 05:26
Disregard my other comment.

readInnerXml does not advance the cursor, so you can call it more than once. I made a mistake testing this.
Evert Pot 21-Mar-2015 05:21
Note that readInnerXml() also advances the cursor.


Retrieve XML from current node, including itself

  • ≪ XMLReader::readInnerXML
  • XMLReader::readString ≫
  • PHP Manual
  • XMLReader
  • Retrieve XML from current node, including itself

XMLReader::readOuterXML

(PHP 5 >= 5.2.0, PHP 7)

XMLReader::readOuterXML — Retrieve XML from current node, including itself

설명

public string XMLReader::readOuterXML ( void )

Reads the contents of the current node, including the node itself.

인수

이 함수는 인수가 없습니다.

반환값

Returns the contents of current node, including itself, as a string. Empty string on failure.

주의

Caution

이 함수는 PHP가 libxml 20620 이후와 함께 컴파일 되었을 때만 사용할 수 있습니다.

참고

  • XMLReader::readString() - Reads the contents of the current node as a string
  • XMLReader::readInnerXML() - Retrieve XML from current node
  • XMLReader::expand() - Returns a copy of the current node as a DOM object

User Contributed Notes

ralf dot horlebein at web dot de 21-Jul-2014 07:57
Please note that empty tags like <span></span> will be automatically converted to the short notation <span />.  This could cause trouble in some cases.
simon at avalon dot net dot nz 17-Mar-2011 01:35
Be aware that when you extract a piece of XML from a larger XML document using the readOuterXML() function, it tries to determine the namespaces (as defined in the original XML document) that apply to the new piece, and inserts them into the appropriate tags as xmlns attributes. This can be disconcerting, but it should be a benign alteration in most cases, and I understand that it causes the new chunk of XML to be correctly formatted so it is valid in it's own right.

As far as I'm aware it's not possible to change this behaviour.


Reads the contents of the current node as a string

  • ≪ XMLReader::readOuterXML
  • XMLReader::setParserProperty ≫
  • PHP Manual
  • XMLReader
  • Reads the contents of the current node as a string

XMLReader::readString

(PHP 5 >= 5.2.0, PHP 7)

XMLReader::readString — Reads the contents of the current node as a string

설명

public string XMLReader::readString ( void )

Reads the contents of the current node as a string.

인수

이 함수는 인수가 없습니다.

반환값

Returns the content of the current node as a string. Empty string on failure.

주의

Caution

이 함수는 PHP가 libxml 20620 이후와 함께 컴파일 되었을 때만 사용할 수 있습니다.

참고

  • XMLReader::readOuterXML() - Retrieve XML from current node, including itself
  • XMLReader::readInnerXML() - Retrieve XML from current node
  • XMLReader::expand() - Returns a copy of the current node as a DOM object

User Contributed Notes

Lea Hayes 07-Jun-2011 10:51
I would like to reiterate that this does not work when compiled with versions of libxml lower than 20620. I found that it just presents a blank page.

<?php $string = $reader->readString(); ?>

I worked around this issue by adding the following helper function:

<?php
function read_string($reader) {
    $node = $reader->expand();
    return $node->textContent;
}
?>

And then using:

<?php $string = read_string($reader); ?>
Michael Rusch 18-May-2009 07:27
For those who, like me, are having trouble figuring out the difference between getString() an getInnerXML(), the difference is that getString() does NOT include markup.  So, for:

<foo>bar<baz>Hello, world!</baz></foo>

getString() on the <foo> node would return

bar Hello, world!

whereas getInnerXML() would return

bar<baz>Hello, world!</baz>


Set parser options

  • ≪ XMLReader::readString
  • XMLReader::setRelaxNGSchema ≫
  • PHP Manual
  • XMLReader
  • Set parser options

XMLReader::setParserProperty

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::setParserProperty — Set parser options

설명

public bool XMLReader::setParserProperty ( int $property , bool $value )

Set parser options. The options must be set after
XMLReader::open() or
XMLReader::xml() are called and before
the first XMLReader::read() call.

인수

property

One of the parser option constants.

value

If set to TRUE the option will be enabled otherwise will be disabled.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

There are no user contributed notes for this page.


Set the filename or URI for a RelaxNG Schema

  • ≪ XMLReader::setParserProperty
  • XMLReader::setRelaxNGSchemaSource ≫
  • PHP Manual
  • XMLReader
  • Set the filename or URI for a RelaxNG Schema

XMLReader::setRelaxNGSchema

(PHP 5 >= 5.2.0, PHP 7)

XMLReader::setRelaxNGSchema — Set the filename or URI for a RelaxNG Schema

설명

public bool XMLReader::setRelaxNGSchema ( string $filename )

Set the filename or URI for the RelaxNG Schema to use for validation.

인수

filename

filename or URI pointing to a RelaxNG Schema.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::setRelaxNGSchemaSource() - Set the data containing a RelaxNG Schema
  • XMLReader::setSchema() - Validate document against XSD
  • XMLReader::isValid() - Indicates if the parsed document is valid

User Contributed Notes

remy dot damour at laposte dot net 31-Dec-2008 01:40
Make sure to load your data prior to calling ->setRelaxNGSchema() otherwise you will get an error.

<?php
$xml_reader = new XMLReader();
$xml_reader->setRelaxNGSchema($rng_schema);
$xml_reader->xml($xml_data);
?>
Above code generates the following warning: "Warning: XMLReader::setRelaxNGSchemaSource()
[xmlreader.setrelaxngschemasource]: Unable to set schema. This must be
set prior to reading or schema contains errors."

whereas the following does not generate a warning:
<?php
$xml_reader = new XMLReader();
$xml_reader->xml($xml_data); // or $xml_reader->xml($xml_file);
$xml_reader->setRelaxNGSchema($rng_schema);
?>

cf. http://bugs.php.net/bug.php?id=46978


Set the data containing a RelaxNG Schema

  • ≪ XMLReader::setRelaxNGSchema
  • XMLReader::setSchema ≫
  • PHP Manual
  • XMLReader
  • Set the data containing a RelaxNG Schema

XMLReader::setRelaxNGSchemaSource

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::setRelaxNGSchemaSource — Set the data containing a RelaxNG Schema

설명

public bool XMLReader::setRelaxNGSchemaSource ( string $source )

Set the data containing a RelaxNG Schema to use for validation.

인수

source

String containing the RelaxNG Schema.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLReader::setRelaxNGSchema() - Set the filename or URI for a RelaxNG Schema
  • XMLReader::setSchema() - Validate document against XSD
  • XMLReader::isValid() - Indicates if the parsed document is valid

User Contributed Notes

remy dot damour at laposte dot net 31-Dec-2008 01:43
If you get the following warning message when calling ->setRelaxNGSchemaSource(): "Warning: XMLReader::setRelaxNGSchemaSource()
[xmlreader.setrelaxngschemasource]: Unable to set schema. This must be
set prior to reading or schema contains errors."

Make sure to load data using XMLReader::open() or XMLReader::xml() prior to calling XMLReader::setRelaxNGSchemaSource().

Cf. comment on XMLReader::setRelaxNGSchema for more details.
anzenews at volja dot net 21-Jan-2008 09:18
This function and setRelaxNGSchema() seem picky about when they are called - right after the call to open(). For example:

<?php
  $schema="/path/to/schema.rng";
  $xmlfile="/path/to/xml.xml";

  $xml = new XMLReader();
  $xml->open($xmlfile);
  $xml->setRelaxNGSchemaSource(file_get_contents($schema));

  while ($xml->read()) {
   // ...
  }

  $xml->close();
?>


Validate document against XSD

  • ≪ XMLReader::setRelaxNGSchemaSource
  • XMLReader::XML ≫
  • PHP Manual
  • XMLReader
  • Validate document against XSD

XMLReader::setSchema

(PHP 5 >= 5.2.0, PHP 7)

XMLReader::setSchema — Validate document against XSD

설명

public bool XMLReader::setSchema ( string $filename )

Use W3C XSD schema to validate the document as it is processed. Activation is only possible before the first Read().

인수

filename

The filename of the XSD schema.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

오류/예외

Issues E_WARNING if libxml was built without schema support, the schema contains errors or if XMLReader::read() has already been called.

주의

Caution

이 함수는 PHP가 libxml 20620 이후와 함께 컴파일 되었을 때만 사용할 수 있습니다.

참고

  • XMLReader::setRelaxNGSchema() - Set the filename or URI for a RelaxNG Schema
  • XMLReader::setRelaxNGSchemaSource() - Set the data containing a RelaxNG Schema
  • XMLReader::isValid() - Indicates if the parsed document is valid

User Contributed Notes

There are no user contributed notes for this page.


Set the data containing the XML to parse

  • ≪ XMLReader::setSchema
  • XMLWriter ≫
  • PHP Manual
  • XMLReader
  • Set the data containing the XML to parse

XMLReader::XML

(PHP 5 >= 5.1.0, PHP 7)

XMLReader::XML — Set the data containing the XML to parse

설명

public bool XMLReader::xml ( string $source [, string $encoding [, int $options = 0 ]] )

Set the data containing the XML to parse.

인수

source

String containing the XML to be parsed.

encoding

The document encoding or NULL.

options

A bitmask of the LIBXML_* constants.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다. If called statically, returns an XMLReader실패 시 FALSE를 반환합니다.

오류/예외

이 메소드는 정적으로 호출할 수있지만, E_STRICT 오류가 발생합니다.

변경점

버전 | 설명
5.2.0 | encoding and options were added.

참고

  • XMLReader::open() - Set the URI containing the XML to parse
  • XMLReader::close() - Close the XMLReader input

User Contributed Notes

There are no user contributed notes for this page.


XMLWriter

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • XMLWriter 함수 목록
    • XMLWriter::endAttribute — End attribute
    • XMLWriter::endCData — End current CDATA
    • XMLWriter::endComment — Create end comment
    • XMLWriter::endDocument — End current document
    • XMLWriter::endDTDAttlist — End current DTD AttList
    • XMLWriter::endDTDElement — End current DTD element
    • XMLWriter::endDTDEntity — End current DTD Entity
    • XMLWriter::endDTD — End current DTD
    • XMLWriter::endElement — End current element
    • XMLWriter::endPI — End current PI
    • XMLWriter::flush — Flush current buffer
    • XMLWriter::fullEndElement — End current element
    • XMLWriter::openMemory — Create new xmlwriter using memory for string output
    • XMLWriter::openURI — Create new xmlwriter using source uri for output
    • XMLWriter::outputMemory — Returns current buffer
    • XMLWriter::setIndentString — Set string used for indenting
    • XMLWriter::setIndent — Toggle indentation on/off
    • XMLWriter::startAttributeNS — Create start namespaced attribute
    • XMLWriter::startAttribute — Create start attribute
    • XMLWriter::startCData — Create start CDATA tag
    • XMLWriter::startComment — Create start comment
    • XMLWriter::startDocument — Create document tag
    • XMLWriter::startDTDAttlist — Create start DTD AttList
    • XMLWriter::startDTDElement — Create start DTD element
    • XMLWriter::startDTDEntity — Create start DTD Entity
    • XMLWriter::startDTD — Create start DTD tag
    • XMLWriter::startElementNS — Create start namespaced element tag
    • XMLWriter::startElement — Create start element tag
    • XMLWriter::startPI — Create start PI tag
    • XMLWriter::text — Write text
    • XMLWriter::writeAttributeNS — Write full namespaced attribute
    • XMLWriter::writeAttribute — Write full attribute
    • XMLWriter::writeCData — Write full CDATA tag
    • XMLWriter::writeComment — Write full comment tag
    • XMLWriter::writeDTDAttlist — Write full DTD AttList tag
    • XMLWriter::writeDTDElement — Write full DTD element tag
    • XMLWriter::writeDTDEntity — Write full DTD Entity tag
    • XMLWriter::writeDTD — Write full DTD tag
    • XMLWriter::writeElementNS — Write full namespaced element tag
    • XMLWriter::writeElement — Write full element tag
    • XMLWriter::writePI — Writes a PI
    • XMLWriter::writeRaw — Write a raw XML text

User Contributed Notes

lingtalfi 09-Oct-2015 05:35
The single quote predefined entity is not escaped.
The other predefined entities are but not the single quote, so you will have to do it yourself.

That's what you would expect:

Ampersand     &     &amp;
Single Quote     '     &apos;
Double Quote     "     &quot;
Greater Than     >     &gt;
Less Than     <     &lt;

That's what you get with \XmlWriter:

Ampersand     &     &amp;
Single Quote     '     '
Double Quote     "     &quot;
Greater Than     >     &gt;
Less Than     <     &lt;
Alexey Zakhlestin 09-Jun-2008 12:47
important: XMLWriter expects UTF-8 data from you. It can output xml in various encodings, but input should be strictly utf-8!


소개

This is the XMLWriter extension. It wraps the libxml xmlWriter API.

This extension represents a writer that provides a non-cached, forward-only means of generating streams or files containing XML data.

This extension can be used in an object oriented style or a procedural one. Every method documented describes the alternative procedural call.

User Contributed Notes

There are no user contributed notes for this page.


설치

The XMLWriter extension was initially a PECL extension for PHP 5. It was later added to the PHP source (bundled) as of PHP 5.1.2. This extension is enabled by default.

이 확장은 기본값으로 활성화됩니다. 컴파일 시에 다음 옵션으로 비활성화 할 수 있습니다: --disable-xmlwriter

User Contributed Notes

There are no user contributed notes for this page.


자원형

There is one resource used by the procedural version of the XMLWriter extension: returned by xmlwriter_open_memory() or xmlwriter_open_uri().

User Contributed Notes

There are no user contributed notes for this page.


XMLWriter 함수 목록

Table of Contents

  • XMLWriter::endAttribute — End attribute
  • XMLWriter::endCData — End current CDATA
  • XMLWriter::endComment — Create end comment
  • XMLWriter::endDocument — End current document
  • XMLWriter::endDTDAttlist — End current DTD AttList
  • XMLWriter::endDTDElement — End current DTD element
  • XMLWriter::endDTDEntity — End current DTD Entity
  • XMLWriter::endDTD — End current DTD
  • XMLWriter::endElement — End current element
  • XMLWriter::endPI — End current PI
  • XMLWriter::flush — Flush current buffer
  • XMLWriter::fullEndElement — End current element
  • XMLWriter::openMemory — Create new xmlwriter using memory for string output
  • XMLWriter::openURI — Create new xmlwriter using source uri for output
  • XMLWriter::outputMemory — Returns current buffer
  • XMLWriter::setIndentString — Set string used for indenting
  • XMLWriter::setIndent — Toggle indentation on/off
  • XMLWriter::startAttributeNS — Create start namespaced attribute
  • XMLWriter::startAttribute — Create start attribute
  • XMLWriter::startCData — Create start CDATA tag
  • XMLWriter::startComment — Create start comment
  • XMLWriter::startDocument — Create document tag
  • XMLWriter::startDTDAttlist — Create start DTD AttList
  • XMLWriter::startDTDElement — Create start DTD element
  • XMLWriter::startDTDEntity — Create start DTD Entity
  • XMLWriter::startDTD — Create start DTD tag
  • XMLWriter::startElementNS — Create start namespaced element tag
  • XMLWriter::startElement — Create start element tag
  • XMLWriter::startPI — Create start PI tag
  • XMLWriter::text — Write text
  • XMLWriter::writeAttributeNS — Write full namespaced attribute
  • XMLWriter::writeAttribute — Write full attribute
  • XMLWriter::writeCData — Write full CDATA tag
  • XMLWriter::writeComment — Write full comment tag
  • XMLWriter::writeDTDAttlist — Write full DTD AttList tag
  • XMLWriter::writeDTDElement — Write full DTD element tag
  • XMLWriter::writeDTDEntity — Write full DTD Entity tag
  • XMLWriter::writeDTD — Write full DTD tag
  • XMLWriter::writeElementNS — Write full namespaced element tag
  • XMLWriter::writeElement — Write full element tag
  • XMLWriter::writePI — Writes a PI
  • XMLWriter::writeRaw — Write a raw XML text

User Contributed Notes

benstendahl 05-Nov-2014 02:09
XML allows for repeated nodes that an array does not support in its associative index.  To account for this we can use numerically indexed arrays.

$array = array(
    'element' => array(
        array('node' => 'some value'),
        array('node' => 'some other value')
    ),
    'key' => 'value'
);

This requires that we change the fromArray() method in the class below to check for numeric indexes and throw them away.

public function fromArray(array $array)
 {
    foreach ($array as $key=>$val) {
        if (is_array($val)) {
            if (is_numeric($key)) {
                // numeric keys aren't allowed so we'll skip the key
                $this->fromArray($val);
            } else {
                $this->startElement($key);
                $this->fromArray($val);
                $this->endElement();
            }
        } else {
            $this->writeElement($key, $val);
        }
    }
}
neftali dot yagua at gmail dot com 03-Jun-2011 03:43
Inspired in the XmlConstruct.

<?php
class XLIFFConstruct extends XMLWriter
{

    /**
     * Constructor.
     * @param string $prm_rootElementName A root element's name of a current xml document
     * @param string $prm_xsltFilePath Path of a XSLT file.
     * @access public
     * @param null
     */
      var $_phrase_id=1;
    public function __construct(){
        $this->openMemory();
        $this->setIndent(true);
        $this->setIndentString(' ');
        $this->startDocument('1.0', 'UTF-8');

        if($prm_xsltFilePath){
            $this->writePi('xml-stylesheet', 'type="text/xsl" href="'.$prm_xsltFilePath.'"');
        }
        $this->startElement('xliff');
        $this->writeAttribute('version', '1.0');
        $this->startElement('file');
        $this->writeAttribute('original', 'global');
        $this->writeAttribute('source-language', 'es');
        $this->writeAttribute('datatype', 'plaintext');
        $this->writeAttribute('date', date('c'));
        $this->startElement('body');
    }
    public function addPhrase($source, $target){
        $this->startElement('trans-unit');
      $this->writeAttribute('id', $this->_phrase_id++);
        $this->startElement('source');
        $this->text($source);
      $this->endElement();
        $this->startElement('target');
      $this->text($target);
      $this->endElement();
      $this->endElement();
    }
    public function getDocument(){
        $this->endElement();
        $this->endElement();
        $this->endElement();
        $this->endDocument();
        return $this->outputMemory();
    }
    public function output(){
        header('Content-type: text/xml');
        echo $this->getDocument();
    }
}
?>

Example:

<?php
$xliff = new XLIFFConstruct();
$xliff->addPhrase('source','target');
$xliff->addPhrase('add','anadir');
$xliff->addPhrase('open','abrir');
$xliff->addPhrase('change','cambiar');
$xliff->addPhrase('new','nuevo');
$xliff->addPhrase('save','guardar');

echo $xliff->getDocument();

?>
darko at uvcms dot com 22-Feb-2010 04:30
setElement in the previous example does exactly the same thing as already existing writeElement.
massimo71 19-Feb-2009 11:24
I had a feature to the XmlConstruct class by Alexandre Aprica. Now you can use nested array to generate nested xml elements.

<?php
class XmlConstruct extends XMLWriter
{

    /**
     * Constructor.
     * @param string $prm_rootElementName A root element's name of a current xml document
     * @param string $prm_xsltFilePath Path of a XSLT file.
     * @access public
     * @param null
     */
    public function __construct($prm_rootElementName, $prm_xsltFilePath=''){
        $this->openMemory();
        $this->setIndent(true);
        $this->setIndentString(' ');
        $this->startDocument('1.0', 'UTF-8');

        if($prm_xsltFilePath){
            $this->writePi('xml-stylesheet', 'type="text/xsl" href="'.$prm_xsltFilePath.'"');
        }

        $this->startElement($prm_rootElementName);
    }

    /**
     * Set an element with a text to a current xml document.
     * @access public
     * @param string $prm_elementName An element's name
     * @param string $prm_ElementText An element's text
     * @return null
     */
    public function setElement($prm_elementName, $prm_ElementText){
        $this->startElement($prm_elementName);
        $this->text($prm_ElementText);
        $this->endElement();
    }

    /**
     * Construct elements and texts from an array.
     * The array should contain an attribute's name in index part
     * and a attribute's text in value part.
     * @access public
     * @param array $prm_array Contains attributes and texts
     * @return null
     */
    public function fromArray($prm_array){
      if(is_array($prm_array)){
        foreach ($prm_array as $index => $element){
          if(is_array($element)){
            $this->startElement($index);
            $this->fromArray($element);
            $this->endElement();
          }
          else
            $this->setElement($index, $element);

        }
      }
    }

    /**
     * Return the content of a current xml document.
     * @access public
     * @param null
     * @return string Xml document
     */
    public function getDocument(){
        $this->endElement();
        $this->endDocument();
        return $this->outputMemory();
    }

    /**
     * Output the content of a current xml document.
     * @access public
     * @param null
     */
    public function output(){
        header('Content-type: text/xml');
        echo $this->getDocument();
    }

}

Example:

$contents = array(
  'page_title' => 'Generate a XHTML page from XML+XSLT files',
  'welcome_msg' => 'Simple XHTML document from XML+XSLT files!',
  'prova' => array(
    "gino" => array(
      "innergino" => "gino inner value"
    ),
    "filo" => "filodata"
  ),
);

$XmlConstruct = new XmlConstruct('root');
$XmlConstruct->fromArray($contents);
$XmlConstruct->output();
?>
Yves Sucaet 25-Jul-2007 04:10
If you want your XML-output to be seen as XML by the browser, you need to modify your header. The XmlWriter does not do this for you! Therefore, the first line of your script should be:

<?php header("Content-type: text/xml"); ?>
Alexandre Arica 16-Apr-2006 02:25
How to generate a simple XML document for a XSL-Transformation purpose.

We have 3 files:
- 'index.php' : output a XML document
- 'XmlConstruct.class.php' : allow to construct a XML document with 'xmlwriter'
- 'index.xsl' : contains a XSLT document

Contents of the file 'index.php' :

<?php
$contents = array('page_title' => 'Generate a XHTML page from XML+XSLT files',
                    'welcome_msg' => 'Simple XHTML document from XML+XSLT files!');

require('XmlConstruct.class.php');
$XmlConstruct = new XmlConstruct('rootElement', 'index.xsl');
$XmlConstruct->fromArray($contents);
$XmlConstruct->output();
?>

Contents of the file  'XmlConstruct.class.php' :

<?php

/**
 * Construct a simple XML document.
 * This class inherits from the (PHP) class 'xmlwriter'.
 * You will need at least PHP 5.1.2
 *
 * @author Alexandre Arica
 * @since  16 April 2006
 * @version 1.0 modified the 16 April 2006
 */
class XmlConstruct extends XMLWriter
{

    /**
     * Constructor.
     * @param string $prm_rootElementName A root element's name of a current xml document
     * @param string $prm_xsltFilePath Path of a XSLT file.
     * @access public
     * @param null
     */
    public function __construct($prm_rootElementName, $prm_xsltFilePath=''){
        $this->openMemory();
        $this->setIndent(true);
        $this->setIndentString(' ');
        $this->startDocument('1.0', 'UTF-8');

        if($prm_xsltFilePath){
            $this->writePi('xml-stylesheet', 'type="text/xsl" href="'.$prm_xsltFilePath.'"');
        }

        $this->startElement($prm_rootElementName);
    }

    /**
     * Set an element with a text to a current xml document.
     * @access public
     * @param string $prm_elementName An element's name
     * @param string $prm_ElementText An element's text
     * @return null
     */
    public function setElement($prm_elementName, $prm_ElementText){
        $this->startElement($prm_elementName);
        $this->text($prm_ElementText);
        $this->endElement();
    }

    /**
     * Construct elements and texts from an array.
     * The array should contain an attribute's name in index part
     * and a attribute's text in value part.
     * @access public
     * @param array $prm_array Contains attributes and texts
     * @return null
     */
    public function fromArray($prm_array){
        if(is_array($prm_array)){
            foreach ($prm_array as $index => $text){
                $this->setElement($index, $text);
            }
        }
    }

    /**
     * Return the content of a current xml document.
     * @access public
     * @param null
     * @return string Xml document
     */
    public function getDocument(){
        $this->endElement();
        $this->endDocument();
        return $this->outputMemory();
    }

    /**
     * Output the content of a current xml document.
     * @access public
     * @param null
     */
    public function output(){
        header('Content-type: text/xml');
        echo $this->getDocument();
    }

}

?>

Contents of the file 'index.xsl' :

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet
 version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns="http://www.w3.org/TR/xhtml1/strict">

<xsl:output
    method="html"
    encoding="utf-8"
/>

<xsl:template match="rootElement">

<html xmlns="http://www.w3.org/1999/xhtml">

        <head>

            <title><xsl:value-of select="page_title" /></title>

        </head>

        <body>

            <xsl:value-of select="welcome_msg" />

        </body>

</html>

</xsl:template>

</xsl:stylesheet>
Carlos Averett 15-Apr-2006 07:49
Using XMLWriter to create a WAP page:

<?php
$memory = xmlwriter_open_memory();
xmlwriter_start_document($memory,'1.0','UTF-8');
xmlwriter_start_dtd($memory,'html','-//WAPFORUM//DTD XHTML Mobile 1.0//EN', 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd');
xmlwriter_end_dtd($memory);
xmlwriter_start_element ($memory,'html'); // <html>
xmlwriter_write_attribute( $memory, 'xmlns', 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd');
xmlwriter_write_attribute( $memory, 'xm:lang', 'en');

xmlwriter_start_element($memory,'head'); // <head>

xmlwriter_write_element ($memory,'title', 'Test WAP Document');

xmlwriter_end_element($memory); // </head>
xmlwriter_start_element($memory,'body'); // <body>
xmlwriter_start_element($memory,'ol'); // <ol>

xmlwriter_write_element ($memory,'li', 'One Item');
xmlwriter_write_element ($memory,'li', 'Another Item');
xmlwriter_write_element ($memory,'li', 'Another Item');

xmlwriter_end_element($memory); // </ol>
xmlwriter_end_element($memory); // </body>
xmlwriter_end_element($memory); // </html>

xmlwriter_end_dtd($memory);
$xml = xmlwriter_output_memory($memory,true);
?>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.wapforum.org/DTD/xhtml-mobile10.dtd" xm:lang="en">
<head>
<title>Test WAP Document</title>
</head>
<body>
<ol>
<li>One Item</li>
<li>Another Item</li>
<li>Another Item</li>
</ol>
</body>
</html>


End attribute

  • ≪ XMLWriter 함수 목록
  • XMLWriter::endCData ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • End attribute

XMLWriter::endAttribute

xmlwriter_end_attribute

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::endAttribute -- xmlwriter_end_attribute — End attribute

설명

객체 기반 형식

bool XMLWriter::endAttribute ( void )

절차식 형식

bool xmlwriter_end_attribute ( resource $xmlwriter )

Ends the current attribute.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startAttribute() - Create start attribute
  • XMLWriter::startAttributeNS() - Create start namespaced attribute
  • XMLWriter::writeAttribute() - Write full attribute
  • XMLWriter::writeAttributeNS() - Write full namespaced attribute

User Contributed Notes

There are no user contributed notes for this page.


End current CDATA

  • ≪ XMLWriter::endAttribute
  • XMLWriter::endComment ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • End current CDATA

XMLWriter::endCData

xmlwriter_end_cdata

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::endCData -- xmlwriter_end_cdata — End current CDATA

설명

객체 기반 형식

bool XMLWriter::endCData ( void )

절차식 형식

bool xmlwriter_end_cdata ( resource $xmlwriter )

Ends the current CDATA section.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startCData() - Create start CDATA tag
  • XMLWriter::writeCData() - Write full CDATA tag

User Contributed Notes

There are no user contributed notes for this page.


Create end comment

  • ≪ XMLWriter::endCData
  • XMLWriter::endDocument ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create end comment

XMLWriter::endComment

xmlwriter_end_comment

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 1.0.0)

XMLWriter::endComment -- xmlwriter_end_comment — Create end comment

설명

객체 기반 형식

bool XMLWriter::endComment ( void )

절차식 형식

bool xmlwriter_end_comment ( resource $xmlwriter )

Ends the current comment.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startComment() - Create start comment
  • XMLWriter::writeComment() - Write full comment tag

User Contributed Notes

There are no user contributed notes for this page.


End current document

  • ≪ XMLWriter::endComment
  • XMLWriter::endDTDAttlist ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • End current document

XMLWriter::endDocument

xmlwriter_end_document

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::endDocument -- xmlwriter_end_document — End current document

설명

객체 기반 형식

bool XMLWriter::endDocument ( void )

절차식 형식

bool xmlwriter_end_document ( resource $xmlwriter )

Ends the current document.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startDocument() - Create document tag

User Contributed Notes

There are no user contributed notes for this page.


End current DTD AttList

  • ≪ XMLWriter::endDocument
  • XMLWriter::endDTDElement ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • End current DTD AttList

XMLWriter::endDTDAttlist

xmlwriter_end_dtd_attlist

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::endDTDAttlist -- xmlwriter_end_dtd_attlist — End current DTD AttList

설명

객체 기반 형식

bool XMLWriter::endDTDAttlist ( void )

절차식 형식

bool xmlwriter_end_dtd_attlist ( resource $xmlwriter )

Ends the current DTD attribute list.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startDTDAttlist() - Create start DTD AttList
  • XMLWriter::writeDTDAttlist() - Write full DTD AttList tag

User Contributed Notes

There are no user contributed notes for this page.


End current DTD element

  • ≪ XMLWriter::endDTDAttlist
  • XMLWriter::endDTDEntity ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • End current DTD element

XMLWriter::endDTDElement

xmlwriter_end_dtd_element

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::endDTDElement -- xmlwriter_end_dtd_element — End current DTD element

설명

객체 기반 형식

bool XMLWriter::endDTDElement ( void )

절차식 형식

bool xmlwriter_end_dtd_element ( resource $xmlwriter )

Ends the current DTD element.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startDTDElement() - Create start DTD element
  • XMLWriter::writeDTDElement() - Write full DTD element tag

User Contributed Notes

There are no user contributed notes for this page.


End current DTD Entity

  • ≪ XMLWriter::endDTDElement
  • XMLWriter::endDTD ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • End current DTD Entity

XMLWriter::endDTDEntity

xmlwriter_end_dtd_entity

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::endDTDEntity -- xmlwriter_end_dtd_entity — End current DTD Entity

설명

객체 기반 형식

bool XMLWriter::endDTDEntity ( void )

절차식 형식

bool xmlwriter_end_dtd_entity ( resource $xmlwriter )

Ends the current DTD entity.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startDTDEntity() - Create start DTD Entity
  • XMLWriter::writeDTDEntity() - Write full DTD Entity tag

User Contributed Notes

There are no user contributed notes for this page.


End current DTD

  • ≪ XMLWriter::endDTDEntity
  • XMLWriter::endElement ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • End current DTD

XMLWriter::endDTD

xmlwriter_end_dtd

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::endDTD -- xmlwriter_end_dtd — End current DTD

설명

객체 기반 형식

bool XMLWriter::endDTD ( void )

절차식 형식

bool xmlwriter_end_dtd ( resource $xmlwriter )

Ends the DTD of the document.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startDTD() - Create start DTD tag
  • XMLWriter::writeDTD() - Write full DTD tag

User Contributed Notes

There are no user contributed notes for this page.


End current element

  • ≪ XMLWriter::endDTD
  • XMLWriter::endPI ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • End current element

XMLWriter::endElement

xmlwriter_end_element

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::endElement -- xmlwriter_end_element — End current element

설명

객체 기반 형식

bool XMLWriter::endElement ( void )

절차식 형식

bool xmlwriter_end_element ( resource $xmlwriter )

Ends the current element.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startElement() - Create start element tag
  • XMLWriter::writeElement() - Write full element tag

User Contributed Notes

contact at paulchaplin dot com 26-May-2012 09:32
Note that elements opened this way will *remain open* (i.e. the XML will be badly formed and thus invalid) unless *each element* is explicitly closed with XMLWriter::endElement.

If you have multiple elements open, and wish to simply close them all -- including the root element -- this snippet is what you want:

<?php

// Automatically close all remaining open elements.
while ($xml->endElement() !== false) { continue; }

?>

In some cases, XMLWriter::writeElement might serve you better: it writes complete elements, but isn't suitable when you need to loop to generate its contents, unless this is done beforehand.


End current PI

  • ≪ XMLWriter::endElement
  • XMLWriter::flush ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • End current PI

XMLWriter::endPI

xmlwriter_end_pi

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::endPI -- xmlwriter_end_pi — End current PI

설명

객체 기반 형식

bool XMLWriter::endPI ( void )

절차식 형식

bool xmlwriter_end_pi ( resource $xmlwriter )

Ends the current processing instruction.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startPI() - Create start PI tag
  • XMLWriter::writePI() - Writes a PI

User Contributed Notes

There are no user contributed notes for this page.


Flush current buffer

  • ≪ XMLWriter::endPI
  • XMLWriter::fullEndElement ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Flush current buffer

XMLWriter::flush

xmlwriter_flush

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 1.0.0)

XMLWriter::flush -- xmlwriter_flush — Flush current buffer

설명

객체 기반 형식

mixed XMLWriter::flush ([ bool $empty = true ] )

절차식 형식

mixed xmlwriter_flush ( resource $xmlwriter [, bool $empty = true ] )

Flushes the current buffer.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

empty

Whether to empty the buffer or not. Default is TRUE.

반환값

If you opened the writer in memory, this function returns the generated XML buffer, Else, if using URI, this function will write the buffer and return the number of written bytes.

User Contributed Notes

There are no user contributed notes for this page.


End current element

  • ≪ XMLWriter::flush
  • XMLWriter::openMemory ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • End current element

XMLWriter::fullEndElement

xmlwriter_full_end_element

(PHP 5 >= 5.2.0, PHP 7, PECL xmlwriter >= 2.0.4)

XMLWriter::fullEndElement -- xmlwriter_full_end_element — End current element

설명

객체 기반 형식

bool XMLWriter::fullEndElement ( void )

절차식 형식

bool xmlwriter_full_end_element ( resource $xmlwriter )

End the current xml element. Writes an end tag even if the element is empty.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endElement() - End current element

User Contributed Notes

There are no user contributed notes for this page.


Create new xmlwriter using memory for string output

  • ≪ XMLWriter::fullEndElement
  • XMLWriter::openURI ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create new xmlwriter using memory for string output

XMLWriter::openMemory

xmlwriter_open_memory

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::openMemory -- xmlwriter_open_memory — Create new xmlwriter using memory for string output

설명

객체 기반 형식

bool XMLWriter::openMemory ( void )

절차식 형식

resource xmlwriter_open_memory ( void )

Creates a new XMLWriter using memory for string output.

인수

반환값

객체 기반 형식: 성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

절차식 형식: Returns a new xmlwriter 자원 for later use with the xmlwriter functions on success, FALSE on error.

참고

  • XMLWriter::openURI() - Create new xmlwriter using source uri for output

User Contributed Notes

alvaro at demogracia dot com 16-Jun-2011 10:05
A simple usage example:

<?php
header('Content-type: text/xml; CHARSET=ks_c_5601-1987');

$oXMLWriter = new XMLWriter;
$oXMLWriter->openMemory();
$oXMLWriter->startDocument('1.0', 'UTF-8');

$oXMLWriter->startElement('test');
$oXMLWriter->text('Hello, World!');
$oXMLWriter->endElement();

$oXMLWriter->endDocument();
echo $oXMLWriter->outputMemory(TRUE);
?>
prashant at prashantpatil dot net 27-Apr-2010 03:40
Here is an simple example of using openMemory. This is particularly useful when you are handling a large amount of data and you want to split the xml creation in batches.

<?php
$batchSize = 25;
$xmlWriter = new XMLWriter();
$xmlWriter->openUri('php://output');
$xmlWriter->setIndent(true);
if($xmlWriter)
{
  $xmlWriter->startDocument('1.0','UTF-8');
  $xmlWriter->startElement('Books');

  $memXmlWriter = new XMLWriter();

  $memXmlWriter->openMemory();
  $memXmlWriter->setIndent(true);

  for($i=1;$i<=1000;$i++)
  {
    $memXmlWriter->startElement('book');
      $memXmlWriter->text('book_'.$i);
    $memXmlWriter->endElement();

    if($i%5 == 0)
    {
      $batchXmlString = $memXmlWriter->outputMemory(true);
      $xmlWriter->writeRaw($batchXmlString);
    }
  }
  $memXmlWriter->flush();
  unset($memXmlWriter);
  $xmlWriter->endElement();
  $xmlWriter->endDocument();
}
?>


Create new xmlwriter using source uri for output

  • ≪ XMLWriter::openMemory
  • XMLWriter::outputMemory ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create new xmlwriter using source uri for output

XMLWriter::openURI

xmlwriter_open_uri

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::openURI -- xmlwriter_open_uri — Create new xmlwriter using source uri for output

설명

객체 기반 형식

bool XMLWriter::openURI ( string $uri )

절차식 형식

resource xmlwriter_open_uri ( string $uri )

Creates a new XMLWriter using uri for the output.

인수

uri

The URI of the resource for the output.

반환값

객체 기반 형식: 성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

절차식 형식: Returns a new xmlwriter 자원 for later use with the xmlwriter functions on success, FALSE on error.

참고

  • XMLWriter::openMemory() - Create new xmlwriter using memory for string output

User Contributed Notes

tloeffler 10-Sep-2015 03:08
Reproduced on Windows:

If you get an exception when you try to handle the created file (rename, move etc.) you should unset the XMLWriter instance.

In other case you'll get an exception because the XMLWriter process holds access to this file.

<?php
$xml = new \XMLWriter();
$xml->openUri(...)
...
$xml->flush();

unset($xml); //important!
icepick dot pt at hotmail dot com 16-Mar-2009 10:13
I wanted to stream a XML file directly to the user and having him prompted to save it without displaying the content, but when using

<?php
$out =new XMLWriter();
$out->openURI('php://output');
?>

the user's browser was displaying the file contents instead of prompting the user with a filesave window.

To have the XML file stream saved directly to the user without being displayed by the browser, you shoud add:

<?php
header("Content-Type: text/html/force-download");
header("Content-Disposition: attachment; filename='file_name.xml'");
$out =new XMLWriter();
$out->openURI('php://output');
?>

This will prompt the user with a dialog box to save 'file_name.xml' on his disk, and will not display the content of the streamed XML.
yankee at ignorethis dot cipher DASH code dot de 19-Nov-2007 07:46
Want to write xml-data directly to the user instead of memory or file?
This will do:
<?php
$out =new XMLWriter();
$out->openURI('php://output');
?>


Returns current buffer

  • ≪ XMLWriter::openURI
  • XMLWriter::setIndentString ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Returns current buffer

XMLWriter::outputMemory

xmlwriter_output_memory

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::outputMemory -- xmlwriter_output_memory — Returns current buffer

설명

객체 기반 형식

string XMLWriter::outputMemory ([ bool $flush = true ] )

절차식 형식

string xmlwriter_output_memory ( resource $xmlwriter [, bool $flush = true ] )

Returns the current buffer.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

flush

Whether to flush the output buffer or not. Default is TRUE.

반환값

Returns the current buffer as a string.

참고

  • XMLWriter::flush() - Flush current buffer

User Contributed Notes

dave at dtracorp dot com 23-Feb-2007 08:08
when writing xml with xmlwriter, there is no need to use htmlspecialchars or htmlentities for the text() method (this will only cause double encoding)
the xmlwriter takes care of entities for you after calling outputMemory


Set string used for indenting

  • ≪ XMLWriter::outputMemory
  • XMLWriter::setIndent ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Set string used for indenting

XMLWriter::setIndentString

xmlwriter_set_indent_string

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::setIndentString -- xmlwriter_set_indent_string — Set string used for indenting

설명

객체 기반 형식

bool XMLWriter::setIndentString ( string $indentString )

절차식 형식

bool xmlwriter_set_indent_string ( resource $xmlwriter , string $indentString )

Sets the string which will be used to indent each element/attribute of the resulting xml.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

indentString

The indentation string.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::setIndent() - Toggle indentation on/off

User Contributed Notes

There are no user contributed notes for this page.


Toggle indentation on/off

  • ≪ XMLWriter::setIndentString
  • XMLWriter::startAttributeNS ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Toggle indentation on/off

XMLWriter::setIndent

xmlwriter_set_indent

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::setIndent -- xmlwriter_set_indent — Toggle indentation on/off

설명

객체 기반 형식

bool XMLWriter::setIndent ( bool $indent )

절차식 형식

bool xmlwriter_set_indent ( resource $xmlwriter , bool $indent )

Toggles indentation on or off.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

indent

Whether indentation is enabled.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::setIndentString() - Set string used for indenting

User Contributed Notes

nemiga at gmail dot com 28-Jul-2011 09:05
This param should be used only after openMemory() call:

<?php
$version = '1.0';
$encoding = 'UTF-8';
$rootName = 'root';

$xml = new XMLWriter();

$xml->openMemory();
$xml->setIndent(true);
$xml->startDocument($version, $encoding);
$xml->startElement($this->rootName);
// .. add more elements here
$xml->endElement();
echo $xml->outputMemory();
?>


Create start namespaced attribute

  • ≪ XMLWriter::setIndent
  • XMLWriter::startAttribute ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start namespaced attribute

XMLWriter::startAttributeNS

xmlwriter_start_attribute_ns

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startAttributeNS -- xmlwriter_start_attribute_ns — Create start namespaced attribute

설명

객체 기반 형식

bool XMLWriter::startAttributeNS ( string $prefix , string $name , string $uri )

절차식 형식

bool xmlwriter_start_attribute_ns ( resource $xmlwriter , string $prefix , string $name , string $uri )

Starts a namespaced attribute.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

prefix

The namespace prefix.

name

The attribute name.

uri

The namespace URI.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startAttribute() - Create start attribute
  • XMLWriter::endAttribute() - End attribute
  • XMLWriter::writeAttribute() - Write full attribute
  • XMLWriter::writeAttributeNS() - Write full namespaced attribute

User Contributed Notes

There are no user contributed notes for this page.


Create start attribute

  • ≪ XMLWriter::startAttributeNS
  • XMLWriter::startCData ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start attribute

XMLWriter::startAttribute

xmlwriter_start_attribute

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startAttribute -- xmlwriter_start_attribute — Create start attribute

설명

객체 기반 형식

bool XMLWriter::startAttribute ( string $name )

절차식 형식

bool xmlwriter_start_attribute ( resource $xmlwriter , string $name )

Starts an attribute.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

name

The attribute name.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startAttributeNS() - Create start namespaced attribute
  • XMLWriter::endAttribute() - End attribute
  • XMLWriter::writeAttribute() - Write full attribute
  • XMLWriter::writeAttributeNS() - Write full namespaced attribute

User Contributed Notes

alvaro at demogracia dot com 11-Apr-2011 12:08
The attribute value can be set with XMLWriter::text(), e.g.:

<?php
$writer = new XMLWriter;
$writer->openURI('php://output');
$writer->startDocument('1.0', 'UTF-8');

$writer->startElement('response');
    $writer->startElement('status');
        $writer->startAttribute('code');
            $writer->text('500');
        $writer->endAttribute();
    $writer->endElement();
$writer->endElement();

$writer->endDocument();
?>

... generates:

<?xml version="1.0" encoding="UTF-8"?>
<response><status code="500"/></response>


Create start CDATA tag

  • ≪ XMLWriter::startAttribute
  • XMLWriter::startComment ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start CDATA tag

XMLWriter::startCData

xmlwriter_start_cdata

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startCData -- xmlwriter_start_cdata — Create start CDATA tag

설명

객체 기반 형식

bool XMLWriter::startCData ( void )

절차식 형식

bool xmlwriter_start_cdata ( resource $xmlwriter )

Starts a CDATA.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endCData() - End current CDATA
  • XMLWriter::writeCData() - Write full CDATA tag

User Contributed Notes

There are no user contributed notes for this page.


Create start comment

  • ≪ XMLWriter::startCData
  • XMLWriter::startDocument ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start comment

XMLWriter::startComment

xmlwriter_start_comment

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 1.0.0)

XMLWriter::startComment -- xmlwriter_start_comment — Create start comment

설명

객체 기반 형식

bool XMLWriter::startComment ( void )

절차식 형식

bool xmlwriter_start_comment ( resource $xmlwriter )

Starts a comment.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endComment() - Create end comment
  • XMLWriter::writeComment() - Write full comment tag

User Contributed Notes

There are no user contributed notes for this page.


Create document tag

  • ≪ XMLWriter::startComment
  • XMLWriter::startDTDAttlist ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create document tag

XMLWriter::startDocument

xmlwriter_start_document

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startDocument -- xmlwriter_start_document — Create document tag

설명

객체 기반 형식

bool XMLWriter::startDocument ([ string $version = 1.0 [, string $encoding = NULL [, string $standalone ]]] )

절차식 형식

bool xmlwriter_start_document ( resource $xmlwriter [, string $version = 1.0 [, string $encoding = NULL [, string $standalone ]]] )

Starts a document.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

version

The version number of the document as part of the XML declaration.

encoding

The encoding of the document as part of the XML declaration.

standalone

yes or no.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endDocument() - End current document

User Contributed Notes

msbmail at gmail dot com 29-Oct-2012 10:08
Some errors in generating the XML document are related to the format used to write the PHP file from which the XML is generated.

In Windows environments, be sure to save your documents in ANSI / ASCII and you will avoid many of them.
icepick dot pt at hotmail dot com 30-Mar-2009 10:25
I was having a very hard time triyng to use:

$writer->startDocument( '1.0' , 'Windows-1252' );

Somehow this caused the generation of an empty xml file, and I could not figure out why.
When all other efforts failed I asked for help to the team that developed the XMLwriter extension, and got a very quick reply as follows:

Quote:
==============================
Windows-1252 support is only available if libxml2 is link with iconv
support (it usually is, but there are exceptions). I know on my Mac, the
built in libxml2 doesnt provide iconv support - they go with some
Unicode hack which doesnt provide it, so I had to build my own. It is
built by default (and your code works) on my linux machines.

It is possible to create the XML without a declaration, which means you
could add it manually, but if you use any characters that are differnt
between UTF-8 and Windows-1252, the encoding would be wrong in the
document, so do not recommend.
==============================

Then light was made on my head: I am using a linux server distribution that by default is still running PHP 4.3. A few months ago I decided to force the upgrade of PHP to the newest version 5.1.6 at that time, and that has brougth me the XMLwriter extension that I am now using. But clearly there are compatibility issues betwen the rest of the server's defaults and PHP 5 wich is probably why it's development team has not upgraded yet.

Let's hope all the remaining environments catch up with this extension. It is in fact most handy.

And in the meantime, I have figured out a (not recomended, obviously) workaroud that works for me:

Instead of using two parameters, I used only the first one:

$writer->startDocument( '1.0" encoding="Windows-1252' );

this will output:

<?xml version="1.0" encoding="Windows-1252"?>


Create start DTD AttList

  • ≪ XMLWriter::startDocument
  • XMLWriter::startDTDElement ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start DTD AttList

XMLWriter::startDTDAttlist

xmlwriter_start_dtd_attlist

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startDTDAttlist -- xmlwriter_start_dtd_attlist — Create start DTD AttList

설명

객체 기반 형식

bool XMLWriter::startDTDAttlist ( string $name )

절차식 형식

bool xmlwriter_start_dtd_attlist ( resource $xmlwriter , string $name )

Starts a DTD attribute list.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

name

The attribute list name.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endDTDAttlist() - End current DTD AttList
  • XMLWriter::writeDTDAttlist() - Write full DTD AttList tag

User Contributed Notes

There are no user contributed notes for this page.


Create start DTD element

  • ≪ XMLWriter::startDTDAttlist
  • XMLWriter::startDTDEntity ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start DTD element

XMLWriter::startDTDElement

xmlwriter_start_dtd_element

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startDTDElement -- xmlwriter_start_dtd_element — Create start DTD element

설명

객체 기반 형식

bool XMLWriter::startDTDElement ( string $qualifiedName )

절차식 형식

bool xmlwriter_start_dtd_element ( resource $xmlwriter , string $qualifiedName )

Starts a DTD element.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

qualifiedName

The qualified name of the document type to create.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endDTDElement() - End current DTD element
  • XMLWriter::writeDTDElement() - Write full DTD element tag

User Contributed Notes

There are no user contributed notes for this page.


Create start DTD Entity

  • ≪ XMLWriter::startDTDElement
  • XMLWriter::startDTD ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start DTD Entity

XMLWriter::startDTDEntity

xmlwriter_start_dtd_entity

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startDTDEntity -- xmlwriter_start_dtd_entity — Create start DTD Entity

설명

객체 기반 형식

bool XMLWriter::startDTDEntity ( string $name , bool $isparam )

절차식 형식

bool xmlwriter_start_dtd_entity ( resource $xmlwriter , string $name , bool $isparam )

Starts a DTD entity.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

name

The name of the entity.

isparam

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endDTDEntity() - End current DTD Entity
  • XMLWriter::writeDTDEntity() - Write full DTD Entity tag

User Contributed Notes

There are no user contributed notes for this page.


Create start DTD tag

  • ≪ XMLWriter::startDTDEntity
  • XMLWriter::startElementNS ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start DTD tag

XMLWriter::startDTD

xmlwriter_start_dtd

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startDTD -- xmlwriter_start_dtd — Create start DTD tag

설명

객체 기반 형식

bool XMLWriter::startDTD ( string $qualifiedName [, string $publicId [, string $systemId ]] )

절차식 형식

bool xmlwriter_start_dtd ( resource $xmlwriter , string $qualifiedName [, string $publicId [, string $systemId ]] )

Starts a DTD.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

qualifiedName

The qualified name of the document type to create.

publicId

The external subset public identifier.

systemId

The external subset system identifier.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endDTD() - End current DTD
  • XMLWriter::writeDTD() - Write full DTD tag

User Contributed Notes

joaner1206 at gmail dot com 04-Oct-2011 11:52
If you will write a HTML document :

 // $xml = new XMLWriter();  you know it and openURI()

<?php
 // for HTML 5
$xml->startDTD('html');
 // for XHTML 1.0
$xml->startDTD('html', '-//W3C//DTD XHTML 1.0 Strict//EN','http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'); // standards compliant

$xml->endDTD();
?>
it will be :
<!DOCTYPE html>
or    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">

don't need to startDocument() .


Create start namespaced element tag

  • ≪ XMLWriter::startDTD
  • XMLWriter::startElement ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start namespaced element tag

XMLWriter::startElementNS

xmlwriter_start_element_ns

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startElementNS -- xmlwriter_start_element_ns — Create start namespaced element tag

설명

객체 기반 형식

bool XMLWriter::startElementNS ( string $prefix , string $name , string $uri )

절차식 형식

bool xmlwriter_start_element_ns ( resource $xmlwriter , string $prefix , string $name , string $uri )

Starts a namespaced element.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

prefix

The namespace prefix.

name

The element name.

uri

The namespace URI.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endElement() - End current element
  • XMLWriter::writeElementNS() - Write full namespaced element tag

User Contributed Notes

pike-php at kw dot nl 07-Mar-2011 01:02
by default, the xmlns: definition is repeated on every element.

if you do want the prefix, but dont want the xmlns: declaration repeated, set the namespace to null. dont forget to declare the namespace prefix somewhere higher in your document, though:

<?php
$w->startElementNS('foo', 'bar', 'http://example.com/foo');
$w->startElementNS('foo', 'baz', null);
$w->endElement();
$w->endElement();
?>
anthony dot parsons at manx dot net 05-Aug-2007 07:27
If you don't want any namespace prefix at all but still want the xmlns attribute, set $prefix to null.


Create start element tag

  • ≪ XMLWriter::startElementNS
  • XMLWriter::startPI ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start element tag

XMLWriter::startElement

xmlwriter_start_element

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startElement -- xmlwriter_start_element — Create start element tag

설명

객체 기반 형식

bool XMLWriter::startElement ( string $name )

절차식 형식

bool xmlwriter_start_element ( resource $xmlwriter , string $name )

Starts an element.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

name

The element name.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endElement() - End current element
  • XMLWriter::writeElement() - Write full element tag

User Contributed Notes

reedsilverstein at gmail dot com 24-Jul-2014 05:59
I needed to make a self closing tag with one attribute so I did:

<?php

$writer->startElement('CookTime');
  $writer->writeAttribute('minMinute', $cook_time);
$writer->endElement();

?>

Output: <CookTime minMinute="10"/>
toby at php dot net 21-Oct-2010 08:01
Note that startElement() and startElementNS() naturally do not write the closing ">" of the tag, since you may add an arbitrary number of attributes after starting a tag.

However, in some cases you may want to have the starting element tag closed in the output buffer before writing any further elements or content (e.g. if you communicate via an XML stream). To achieve this, you can simply use the text() method with an empty string.
marcuslists at brightonart dot co dot uk 16-Oct-2009 02:10
Call trim on the tag as a trailing space breaks the end element.


Create start PI tag

  • ≪ XMLWriter::startElement
  • XMLWriter::text ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Create start PI tag

XMLWriter::startPI

xmlwriter_start_pi

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::startPI -- xmlwriter_start_pi — Create start PI tag

설명

객체 기반 형식

bool XMLWriter::startPI ( string $target )

절차식 형식

bool xmlwriter_start_pi ( resource $xmlwriter , string $target )

Starts a processing instruction tag.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

target

The target of the processing instruction.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::endPI() - End current PI
  • XMLWriter::writePI() - Writes a PI

User Contributed Notes

There are no user contributed notes for this page.


Write text

  • ≪ XMLWriter::startPI
  • XMLWriter::writeAttributeNS ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write text

XMLWriter::text

xmlwriter_text

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::text -- xmlwriter_text — Write text

설명

객체 기반 형식

bool XMLWriter::text ( string $content )

절차식 형식

bool xmlwriter_text ( resource $xmlwriter , string $content )

Writes a text.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

content

The contents of the text.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

User Contributed Notes

c dot 1 at smithies dot org 27-Nov-2011 07:55
There is no need to use htmlspecialchars() on your data: it is done automatically.
eikonomega at gmail dot com 17-Apr-2010 12:04
This method creates a text node (W3C type 3).


Write full namespaced attribute

  • ≪ XMLWriter::text
  • XMLWriter::writeAttribute ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write full namespaced attribute

XMLWriter::writeAttributeNS

xmlwriter_write_attribute_ns

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writeAttributeNS -- xmlwriter_write_attribute_ns — Write full namespaced attribute

설명

객체 기반 형식

bool XMLWriter::writeAttributeNS ( string $prefix , string $name , string $uri , string $content )

절차식 형식

bool xmlwriter_write_attribute_ns ( resource $xmlwriter , string $prefix , string $name , string $uri , string $content )

Writes a full namespaced attribute.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

prefix

The namespace prefix.

name

The attribute name.

uri

The namespace URI.

content

The attribute value.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::writeAttribute() - Write full attribute
  • XMLWriter::startAttribute() - Create start attribute
  • XMLWriter::startAttributeNS() - Create start namespaced attribute
  • XMLWriter::endAttribute() - End attribute

User Contributed Notes

There are no user contributed notes for this page.


Write full attribute

  • ≪ XMLWriter::writeAttributeNS
  • XMLWriter::writeCData ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write full attribute

XMLWriter::writeAttribute

xmlwriter_write_attribute

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writeAttribute -- xmlwriter_write_attribute — Write full attribute

설명

객체 기반 형식

bool XMLWriter::writeAttribute ( string $name , string $value )

절차식 형식

bool xmlwriter_write_attribute ( resource $xmlwriter , string $name , string $value )

Writes a full attribute.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

name

The name of the attribute.

value

The value of the attribute.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::writeAttributeNS() - Write full namespaced attribute
  • XMLWriter::startAttribute() - Create start attribute
  • XMLWriter::startAttributeNS() - Create start namespaced attribute
  • XMLWriter::endAttribute() - End attribute

User Contributed Notes

Paul 24-Sep-2016 05:43
If you intermix writing sub-elements and attributes, any attempt to write attributes after the first sub-element will fail and return false:

<?php
$xml = new XMLWriter();
$xml->openMemory();

$xml->startElement('element');
$xml->writeAttribute('attr1', '0');
$xml->writeElement('subelem', '0');
var_dump($xml->writeAttribute('attr2', '0'));
$xml->endElement();

echo $xml->flush();
?>

Outputs:

bool(false)
<element attr1="0"><subelem>0</subelem></element>

This is because this is a forward-only writer and the start tags are already finished. There is no going back to add more attributes later, there is no place to put attr2. This is exactly how it is supposed to work.
Jason Hughes 15-Apr-2011 10:37
If you intermix writing sub-elements and attributes, any attributes that are written after the first sub-element are ignored/discarded:

<?php
$xml->startElement('element');
  $xml->writeAttribute('attr1', 0);
  $xml->writeElement('subelem', 0);
  $xml->writeAttribute('attr2', 0);
$xml->endElement();
?>

Outputs:

<element attr1=0>
  <subelem>0</subelem>
</element>

This is stupid, but the way it works as of PHP 5.2.4.


Write full CDATA tag

  • ≪ XMLWriter::writeAttribute
  • XMLWriter::writeComment ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write full CDATA tag

XMLWriter::writeCData

xmlwriter_write_cdata

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writeCData -- xmlwriter_write_cdata — Write full CDATA tag

설명

객체 기반 형식

bool XMLWriter::writeCData ( string $content )

절차식 형식

bool xmlwriter_write_cdata ( resource $xmlwriter , string $content )

Writes a full CDATA.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

content

The contents of the CDATA.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startCData() - Create start CDATA tag
  • XMLWriter::endCData() - End current CDATA

User Contributed Notes

t dot w dot natt at UNIVERSITY dot bath dot ac dot uk 09-Sep-2008 05:18
Seen a couple of bug reports and gotchas with this one but not a simple code example. Just in case someone finds it useful:

<?php

// serve xml doc as xml
header('Content-type: application/xml');

// set up the document
$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('mydoc');
$xml->startElement('myele');

// CData output
$xml->startElement('mycdataelement');
$xml->writeCData("text for inclusion within CData tags");
$xml->endElement();

// end the document and output
$xml->endElement();
$xml->endElement();
echo $xml->outputMemory(true);
?>

Outputs:
<?xml version="1.0" encoding="UTF-8"?>
<mydoc>
    <myele>
        <mycdataelement><![CDATA[text for inclusion within CData tags]]>
        </mycdataelement>
    </myele>
</mydoc>
thesoupdragon at hotmail dot com 21-Jun-2007 11:14
A rather strange effect with this.

UTF8 Mysql database

                            $xml->startElement('tablecolor2');
    $xml->writeCData( $tablecolor2 );
    $xml->endElement();

does not work !

but
                            $xml->startElement('tablecolor2');
    $tablecolor2 = utf8_decode ( $val['pcolor2']);
    $xml->writeCData( utf8_encode ($tablecolor2) );
    $xml->endElement();

cannot explain this - but it may help someone
dave at dtracorp dot com 06-Mar-2007 10:30
i don't know if this is a bug with the underlying c code or not, or this is by design (i'm not a big xml guy), but for me, running on fedora core 5, php 5.2.1
to get this to work, the cdata functions need to wrap the description (or whatever element the cdata should appear in)
for example.
// initiate xmlwriter object as $xw
// add header, titles, etc.

// start cdata
$xw->startCData();
// start description
$xw->startElement('description');
// write cdata
$xw->writeCData('<img src="http://php.net/images/php.gif" />');
// write the description contents
$xw->text('php logo');
// end the description element
$xw->endElement();
// end the cdata
$xw->endCData();

// end xml, and output

otherwise, i just got warnings about writing cdata in the wrong context, and no cdata would be written


Write full comment tag

  • ≪ XMLWriter::writeCData
  • XMLWriter::writeDTDAttlist ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write full comment tag

XMLWriter::writeComment

xmlwriter_write_comment

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writeComment -- xmlwriter_write_comment — Write full comment tag

설명

객체 기반 형식

bool XMLWriter::writeComment ( string $content )

절차식 형식

bool xmlwriter_write_comment ( resource $xmlwriter , string $content )

Writes a full comment.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

content

The contents of the comment.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startComment() - Create start comment
  • XMLWriter::endComment() - Create end comment

User Contributed Notes

thewrs at gmail dot com 05-Dec-2007 11:37
This will write a XML <!----> style comment to your XMLWriter resource.

Strings up to 50 characters long are displayed on a single line with no spacing between the tags.

Anything above 50 characters will show on multiple formatted lines. Using the \n metacharater inserts a newline and maintains indenting.


Write full DTD AttList tag

  • ≪ XMLWriter::writeComment
  • XMLWriter::writeDTDElement ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write full DTD AttList tag

XMLWriter::writeDTDAttlist

xmlwriter_write_dtd_attlist

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writeDTDAttlist -- xmlwriter_write_dtd_attlist — Write full DTD AttList tag

설명

객체 기반 형식

bool XMLWriter::writeDTDAttlist ( string $name , string $content )

절차식 형식

bool xmlwriter_write_dtd_attlist ( resource $xmlwriter , string $name , string $content )

Writes a DTD attribute list.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

name

The name of the DTD attribute list.

content

The content of the DTD attribute list.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startDTDAttlist() - Create start DTD AttList
  • XMLWriter::endDTDAttlist() - End current DTD AttList

User Contributed Notes

There are no user contributed notes for this page.


Write full DTD element tag

  • ≪ XMLWriter::writeDTDAttlist
  • XMLWriter::writeDTDEntity ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write full DTD element tag

XMLWriter::writeDTDElement

xmlwriter_write_dtd_element

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writeDTDElement -- xmlwriter_write_dtd_element — Write full DTD element tag

설명

객체 기반 형식

bool XMLWriter::writeDTDElement ( string $name , string $content )

절차식 형식

bool xmlwriter_write_dtd_element ( resource $xmlwriter , string $name , string $content )

Writes a full DTD element.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

name

The name of the DTD element.

content

The content of the element.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startDTDElement() - Create start DTD element
  • XMLWriter::endDTDElement() - End current DTD element

User Contributed Notes

There are no user contributed notes for this page.


Write full DTD Entity tag

  • ≪ XMLWriter::writeDTDElement
  • XMLWriter::writeDTD ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write full DTD Entity tag

XMLWriter::writeDTDEntity

xmlwriter_write_dtd_entity

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writeDTDEntity -- xmlwriter_write_dtd_entity — Write full DTD Entity tag

설명

객체 기반 형식

bool XMLWriter::writeDTDEntity ( string $name , string $content , bool $pe , string $pubid , string $sysid , string $ndataid )

절차식 형식

bool xmlwriter_write_dtd_entity ( resource $xmlwriter , string $name , string $content , bool $pe , string $pubid , string $sysid , string $ndataid )

Writes a full DTD entity.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

name

The name of the entity.

content

The content of the entity.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startDTDEntity() - Create start DTD Entity
  • XMLWriter::endDTDEntity() - End current DTD Entity

User Contributed Notes

There are no user contributed notes for this page.


Write full DTD tag

  • ≪ XMLWriter::writeDTDEntity
  • XMLWriter::writeElementNS ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write full DTD tag

XMLWriter::writeDTD

xmlwriter_write_dtd

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writeDTD -- xmlwriter_write_dtd — Write full DTD tag

설명

객체 기반 형식

bool XMLWriter::writeDTD ( string $name [, string $publicId [, string $systemId [, string $subset ]]] )

절차식 형식

bool xmlwriter_write_dtd ( resource $xmlwriter , string $name [, string $publicId [, string $systemId [, string $subset ]]] )

Writes a full DTD.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

name

The DTD name.

publicId

The external subset public identifier.

systemId

The external subset system identifier.

subset

The content of the DTD.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startDTD() - Create start DTD tag
  • XMLWriter::endDTD() - End current DTD

User Contributed Notes

test2 at twinmail dot de 02-Jul-2009 01:14
Use NULL for $publicId to force 'SYSTEM' instead of 'PUBLIC' output.


Write full namespaced element tag

  • ≪ XMLWriter::writeDTD
  • XMLWriter::writeElement ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write full namespaced element tag

XMLWriter::writeElementNS

xmlwriter_write_element_ns

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writeElementNS -- xmlwriter_write_element_ns — Write full namespaced element tag

설명

객체 기반 형식

bool XMLWriter::writeElementNS ( string $prefix , string $name , string $uri [, string $content ] )

절차식 형식

bool xmlwriter_write_element_ns ( resource $xmlwriter , string $prefix , string $name , string $uri [, string $content ] )

Writes a full namespaced element tag.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

prefix

The namespace prefix.

name

The element name.

uri

The namespace URI.

content

The element contents.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
5.2.3 | The content parameter became optional.

참고

  • XMLWriter::startElementNS() - Create start namespaced element tag
  • XMLWriter::endElement() - End current element
  • XMLWriter::writeElement() - Write full element tag

User Contributed Notes

There are no user contributed notes for this page.


Write full element tag

  • ≪ XMLWriter::writeElementNS
  • XMLWriter::writePI ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write full element tag

XMLWriter::writeElement

xmlwriter_write_element

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writeElement -- xmlwriter_write_element — Write full element tag

설명

객체 기반 형식

bool XMLWriter::writeElement ( string $name [, string $content ] )

절차식 형식

bool xmlwriter_write_element ( resource $xmlwriter , string $name [, string $content ] )

Writes a full element tag.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

name

The element name.

content

The element contents.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
5.2.3 | The content parameter became optional.

참고

  • XMLWriter::startElement() - Create start element tag
  • XMLWriter::endElement() - End current element
  • XMLWriter::writeElementNS() - Write full namespaced element tag

User Contributed Notes

rjb at robertjbrown dot com 25-Jan-2010 03:04
Quick note - writeElement also seems to decode any HTML entities during the write. For example, if you pass it "Me & You", it will write the output as "Me &amp; You".
googolplex at gmx dot net 09-Dec-2008 03:08
The function XMLWriter::writeElement is used to write an element (i.e. a XML tag, an [optional] content, and a closing XML tag) in one line without sub-elements:

<?php

$oXMLout = new XMLWriter();
$oXMLout->openMemory();
$oXMLout->writeElement("quantity", 8);
$oXMLout->writeElement("price_per_quantity", 110);
print $oXMLout->outputMemory();

// produces:
// <quantity>8</quantity>
// <price_per_quantity>110</price_per_quantity>

?>

whereas if you want to include sub-elements you have to use the XMLWriter::startElement / XMLWriter::endElement pair:

<?php

$oXMLout = new XMLWriter();
$oXMLout->openMemory();
$oXMLout->startElement("item");
$oXMLout->writeElement("quantity", 8);
$oXMLout->writeElement("price_per_quantity", 110);
$oXMLout->endElement();
print $oXMLout->outputMemory();

// produces:
// <item>
// <quantity>8</quantity>
// <price_per_quantity>110</price_per_quantity>
// </item>

?>


Writes a PI

  • ≪ XMLWriter::writeElement
  • XMLWriter::writeRaw ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Writes a PI

XMLWriter::writePI

xmlwriter_write_pi

(PHP 5 >= 5.1.2, PHP 7, PECL xmlwriter >= 0.1.0)

XMLWriter::writePI -- xmlwriter_write_pi — Writes a PI

설명

객체 기반 형식

bool XMLWriter::writePI ( string $target , string $content )

절차식 형식

bool xmlwriter_write_pi ( resource $xmlwriter , string $target , string $content )

Writes a processing instruction.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

target

The target of the processing instruction.

content

The content of the processing instruction.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::startPI() - Create start PI tag
  • XMLWriter::endPI() - End current PI

User Contributed Notes

ghargoil at remove dot gmx dot this dot net 30-Jul-2006 07:13
Just wanted to point out that if you do the following code in php, you can generate a simple php page with a stylesheet.

<?php
        $res = xmlwriter_open_memory();
        xmlwriter_set_indent_string($res, "\t");
        xmlwriter_start_document($res);
        xmlwriter_write_pi($res, "xml-stylesheet", "type=\"text/xml\" href=\"yourstyle.xsl\"");
        xmlwriter_end_document($res);
        $string = xmlwriter_output_memory($res);
        echo $string;
?>

This results in the following:
X-Powered-By: PHP/5.1.4
Content-type: text/html

<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="yourstyle.xsl"?>


Write a raw XML text

  • ≪ XMLWriter::writePI
  • XSL ≫
  • PHP Manual
  • XMLWriter 함수 목록
  • Write a raw XML text

XMLWriter::writeRaw

xmlwriter_write_raw

(PHP 5 >= 5.2.0, PHP 7, PECL xmlwriter >= 2.0.4)

XMLWriter::writeRaw -- xmlwriter_write_raw — Write a raw XML text

설명

객체 기반 형식

bool XMLWriter::writeRaw ( string $content )

절차식 형식

bool xmlwriter_write_raw ( resource $xmlwriter , string $content )

Writes a raw xml text.

인수

xmlwriter

순차적 호출에서만 사용. XMLWriter resource가 수정됩니다. 이 자원은 xmlwriter_open_uri()나 xmlwriter_open_memory() 호출로 만들어집니다.

content

The text string to write.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XMLWriter::text() - Write text

User Contributed Notes

There are no user contributed notes for this page.


XSL

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
    • 실행시 설정
    • 자원형
  • 예약 상수
  • 예제
    • Example collection.xml and collection.xsl files
    • Error handling with libxml error handling functions
  • XSLTProcessor — The XSLTProcessor class
    • XSLTProcessor::__construct — Creates a new XSLTProcessor object
    • XSLTProcessor::getParameter — Get value of a parameter
    • XsltProcessor::getSecurityPrefs — Get security preferences
    • XSLTProcessor::hasExsltSupport — Determine if PHP has EXSLT support
    • XSLTProcessor::importStylesheet — Import stylesheet
    • XSLTProcessor::registerPHPFunctions — Enables the ability to use PHP functions as XSLT functions
    • XSLTProcessor::removeParameter — Remove parameter
    • XSLTProcessor::setParameter — Set value for a parameter
    • XSLTProcessor::setProfiling — Sets profiling output file
    • XsltProcessor::setSecurityPrefs — Set security preferences
    • XSLTProcessor::transformToDoc — Transform to a DOMDocument
    • XSLTProcessor::transformToUri — Transform to URI
    • XSLTProcessor::transformToXML — Transform to XML

User Contributed Notes

Jaroslaw Zabiello 23-Jun-2011 04:10
This is more usefull, no files needed, it just takes XML and XSL strings as input parameters and returns transformed XML

<?php
/**
 * @param  $xml
 * @param  $xsl
 * @return string xml
 */
function transform($xml, $xsl) {
   $xslt = new XSLTProcessor();
   $xslt->importStylesheet(new  SimpleXMLElement($xsl));
   return $xslt->transformToXml(new SimpleXMLElement($xml));
}
?>
sudhir dot vis at gmail dot com 25-Apr-2009 08:40
Here is a simplest way to transform the xml from php

<?php
$sXml  = "<xml>";
$sXml .= "<sudhir>hello sudhir</sudhir>";
$sXml .= "</xml>";

# LOAD XML FILE
$XML = new DOMDocument();
$XML->loadXML( $sXml );

# START XSLT
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load( 'xsl/index.xsl', LIBXML_NOCDATA);
$xslt->importStylesheet( $XSL );
#PRINT
print $xslt->transformToXML( $XML );
?>
allens at nospam dot com 20-Apr-2009 05:45
Problem:
Getting Netbeans error message:
Fatal error: Class 'xsltprocessor' not found

Solution:
place copy of php.ini in the c:\windows directory!

by default the php.ini file has xsl disabled......search for the line

;extension=php_xsl.dll

to enable XSL....remove ; so that the extension gets included

Then if Netbeans gives you the following message:
PHP Startup: Unable to load dynamic library './php_xsl.dll'

then you need to run the php-5.2.9-1-win32-installer.msi again
this time selecting the XSL extension!
visual77 at gmail dot com 21-Sep-2008 01:35
The XSLTProcessor is kind of a pain to use to generate an entire website, mostly due to the tedious process of building an XML tree through DOMDocument's createElement and appendChild functions.

To get an entire website out of nothing but XML and XSL (which really is a superior system to just echoing out the HTML), you could use an object that does all the PHP data structure to XML data tree conversion, and combines with the XSLTProcessor for rendering. A good object like this is DOMi, which you can find at http://domi.sourceforge.net.
emilise dot victor at gmail dot com 12-Mar-2008 05:10
Here is a way to get an output without using <xml:message> : use the var_dump function of php :
- First bind the php functions with
<? $xslt->registerPHPFunctions() ?>
and xmlns:php="http://php.net/xsl" in the stylesheet
- Then you can call the var_dump function like this : <xsl:variable name="a" select="php:function('var_dump', $return)" />

I couldn't find a way to call the function and have it really print the result without creating a variable, but well, it's just for debugging...
shangxiao at php dot net 11-Jan-2008 02:38
When using the DOM api to add xsl commands to your stylesheet dynamically, be sure to use createElementNS() rather than createElement() otherwise the XSLTProcessor will not recognise these DOM nodes.

Eg:
$xsl->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:element');
pb at online-magazin dot at 12-Oct-2007 12:13
Calling the Saxon XSLT Processor is a very esay task to do!

You just need to do some simple task

1. Install the JavaBridge for PHP

2. Download the freeware (B) Saxon distribution from http://saxon.sourceforge.net/

3. Put the jar files in a directory whre you have access

4. Use this sample of code

// Directory where the jar files are located
define("SAXON_DIR", $_SERVER['DOCUMENT_ROOT']."/saxonb8.9.0/");

// include the jars
java_require(SAXON_DIR."saxon8.jar;".SAXON_DIR."saxon8-dom.jar");

$sXslFile = $_SERVER['DOCUMENT_ROOT']."/myfirst.xsl"; // The xsl file
$sXmlFile = $_SERVER['DOCUMENT_ROOT']."/myfirst.xml"; // The xml file

try
{
  $oXslSource = new java("javax.xml.transform.stream.StreamSource", "file://".$sXslFile);
  $oXmlSource = new java("javax.xml.transform.stream.StreamSource", "file://".$sXmlFile);

  $oFeatureKeys = new JavaClass("net.sf.saxon.FeatureKeys");

  // Create the Factory
  $oTransformerFactory = new java("net.sf.saxon.TransformerFactoryImpl");

  //Disable source document validation
  $oTransformerFactory->setAttribute($oFeatureKeys->SCHEMA_VALIDATION, 4);

  // Create a new Transformer
  $oTransFormer = $oTransformerFactory->newTransformer($oXslSource);

  // Create a StreamResult to store the output
  $oResultStringWriter = new java("java.io.StringWriter");
  $oResultStream = new java("javax.xml.transform.stream.StreamResult", $oResultStringWriter);

  // Transform
  $oTransFormer->transform($oXmlSource, $oResultStream);

  // Echo the output from the transformation
  echo java_cast($oResultStringWriter->toString(), "string");

}
catch(JavaException $e){
   echo java_cast($e->getCause()->toString(), "string");
   exit;
}

5. Enjoy

This is working quite well.
Vasil Rangelov 21-Aug-2007 04:31
@basslines at gmail dot com
"
As far as I can tell, the most recent stable versions of LibXML/LibXSLT/LibEXSLT do NOT support xPath 2.0 / XSLT 2.0 transformations. The only support for XSLT 2.0 that I've found is in Java's SAXON processor (http://saxon.sourceforge.net/).
"

Using SAXON or AltovaXML by using their JAVA bindings could prove to be a difficult task to do, and .NET seems to be buggy (at least for me). However, I've made a class that uses SAXON or AltovaXML from the command line. AltovaXML could also be used with COM, provided it's registered as a COM component.

You can download my wrapper from:
http://xslt2processor.sourceforge.net/

I still need to implement a more graceful error handling (the way the XSL extension uses the Libxml functions), and once the bug
http://bugs.php.net/bug.php?id=41577
is fixed, I'll implement the .NET interfaces too.

I'll appreciate any feedback.
geoffreyj dot lee at gmail dot com 02-Jul-2007 08:02
Here are a couple useful tips for those who care about cross-browser compatibility.

Given:

<script type="text/javascript" src="test.js"></script>

XSLT will automatically condense it to:

<script type="text/javascript" src="test.js"/>

While this is good, Internet Explorer breaks on empty script tags. So, the simple solution is to add something to prevent an empty tag. For example:

<script type="text/javascript" src="test.js"><xsl:comment/></script>

Produces:

<script type="text/javascript" src="test.js">

</script>

------------------------

Also, here is a way to use IE's conditional comments:

<xsl:comment>[if gte IE 5]&#62;
&#60;link rel="stylesheet" type="text/css" href="ie.css" /&#62;
&#60;![endif]</xsl:comment>

Produces:

<!--[if gte IE 5]>
<link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->

This will let you isolate IE-related CSS without needing to use ugly CSS hacks.
gabriel dot birke at web dot de 10-May-2007 12:19
Each <xsl:message> tag will generate a PHP error (level Warning). If you want to collect and pretty-print them, you could use a custom error handling function that collects the warnings in a static variable.
mnot at mnot dot net 09-May-2007 05:59
You can set an HTTP proxy for the XSLT document() function to use (as well as DTD external references) by setting http_proxy in the environment.

E.g., in Apache configuration;

SetEnv http_proxy http://127.0.0.1:3128/
Ryan D. Hatch 02-May-2007 10:14
Looking for php_xsl.dll?

If you installed on Windows using the MSI Installer - you may not have it.

1.) Add/Remove Programs and Change PHP Installation.  Select XSL Extension.

OR

2.) Download complete .zip file of PHP and you can copy php_xsl.dll into your PHP/etc directory.

Just a note.

Ryan D. Hatch
adrian at foeder dot de 14-Sep-2006 11:45
If you want to use the document()-function inside your XSL stylesheet, please note that you have to use an absolute path there.
Important for windows users: the absolute path *has* to be with forward slashes, so subsitute windows-path-backslashes to forward slashes before you transform the document.

Examples:
This will NOT work:
<xsl:copy-of select="document('test.xml')" />

This will also NOT work:
<xsl:copy-of select="document('c:\temp\test.xml')" />

But this WILL work:
<xsl:copy-of select="document('c:/temp/test.xml')" />
28-Apr-2006 03:26
Enable libxslt library, PHP 5, under windows:

To Enable:
In your php.ini
1. Uncomment ;extension=php_xsl.dll
2. Change extension_dir to
     extension_dir = "./ext"

To Confirm:
1. In a test.php page:
<?php phpinfo() ?>
2. Run test.php
3. Search for "libxslt Version". It should return a version number in a XSL headed table.

Further info Google
"Configuring and Testing PHP Servers for XSL Support"
Fabrice Bonny 30-Mar-2006 10:34
In response to how to use entities from DTD (internal or external) in XSLT. It works if you do this way:

$xsl = new DOMDocument;
$xsl->resolveExternals = TRUE;
$xsl->substituteEntities = TRUE;
$xsl->load(...);

Hope this helps! ;-)
basslines at gmail dot com 29-Mar-2006 07:07
As far as I can tell, the most recent stable versions of LibXML/LibXSLT/LibEXSLT do NOT support xPath 2.0 / XSLT 2.0 transformations. The only support for XSLT 2.0 that I've found is in Java's SAXON processor (http://saxon.sourceforge.net/).
17-Mar-2006 06:14
In response to appletalk at gmail dot com
<snip>
As many of you may have noticed, DOM parser gives errors if the '&nbsp;' entity is present. The E_WARN message looks like:
Warning: DOMDocument::load() [function.load]: Entity 'nbsp' not defined in ...
There're many ways to solve this:
.....
b) Defining &nbsp;
At the top of the document, after the <?xml?> definition, add:
   <!DOCTYPE xsl:stylesheet [
   <!ENTITY nbsp "&#160;" >
   ]>
.......</snip>
Just wanted to let people know that option b does NOT work. I'm not sure why this isn't implemented correctly, but it isn't, so don't waste your time. It's unfortunate the DOMXSL transform is so much less capable than the old xslt function.
olivier dot sow at alias dot france 05-Aug-2005 03:27
if you have 'XML parser error 4: not well-formed (invalid token)' error without but your are sure to do no error at all, try this:
<?
function
XSL_transformation($XMLFile,$XSLFile,$ResultFile,$xsl_params){

        $Str = '';

        $xp = xslt_create() or trigger_error('Could not create XSLT process.', E_USER_ERROR);

        xslt_set_encoding($xp, 'ISO-8859-1');

        // read the files into memory
        $xsl_string = join('', file($XSLFile));
        $xml_string = join('', file($XMLFile));

        // set the argument buffer
        $arg_buffer = array('/xml' => $xml_string, '/xsl' => $xsl_string);

        // process the two files to get the desired output
        if (($Str = xslt_process($xp, 'arg:/xml', 'arg:/xsl', NULL, $arg_buffer, $xsl_params))){

            //Saves transformation result into '$ResultFile'
            $fp = fopen($ResultFile,"w+");
            fwrite($fp,$Str);
            fclose($fp);

            return true;
        }

        return false;

    }
?>

found here: http://bugs.php.net/bug.php?id=16193
venkatesh at lammersmedical dot com 01-Mar-2005 03:17
Here is function to read from the XSL sheet which is saved as a text file.

function ReadExcelSheet($filename){
             $test=file($filename);
             $ar1=str_replace("~[^\t]*\t","\t",$test);
             $ar2=str_replace("~","",$ar1);
             $ar=str_replace("?","",$ar2);
             $temp=array();
             for ($i=0; $i<count($ar); $i++) {
                       if((substr($ar[$i],0,1)!= "\t")){
                           if($ar[$i]!=="\r\n"){
                           array_push($temp,$ar[$i]);
            }
        }
    }
    $name=split("\t",$temp[0]);
    $ExcelList=array();
    for($i=1;$i<count($temp);$i++){
        $split_result=split("\t",$temp[$i]);
        array_push($ExcelList,$split_result);
    }
    $result=insert_into_array($ExcelList,0,$name);
    return($result);
}
appletalk at gmail dot com 05-Feb-2005 07:32
As many of you may have noticed, DOM parser gives errors if the '&nbsp;' entity is present. The E_WARN message looks like:

Warning: DOMDocument::load() [function.load]: Entity 'nbsp' not defined in ...

There're many ways to solve this:
a) The hard way
<xsl:text disable-output-escaping="yes"> &amp;nbsp;</xsl:text>

b) Defining &nbsp;
At the top of the document, after the <?xml?> definition, add:
   <!DOCTYPE xsl:stylesheet [
    <!ENTITY nbsp "&#160;" >
    ]>

c) External Doctype
Just in case you want need other HTML entities, you can call an external doctype with the proper definitions

<!DOCTYPE page SYSTEM "http://gv.ca/dtd/character-entities.dtd">

Of course, you can download the file and place it in your server.
kekoajs at yahoo dot com 22-Dec-2004 11:07
Wow, I spent the better part of a day looking for how one could pass an entire test expression to an XSL stylesheet.  It seems that the XSLT 1.0 specification doesn't support it but PHP 5 (and maybe 4s) inclusion of  EXSLT allows one to do exactly that...

simply add these lines...

xmlns:dyn="http://exslt.org/dynamic"
extension-element-prefixes="dyn"

to the <xsl:stylesheet> element and when using an expression stored in a <xsl:param> element write

<xsl:if test="dyn:evaluate($param-name)">

and viola!  you can now use expressions generated externally in your stylesheet!

EXSLT adds many useful functions that can be integrated into your XSL in a similar fashion.  You can go to http://exslt.org/ to learn more...
rd at jerntorget dot se 24-Sep-2004 12:58
If you get the following warning message:

xsltApplyOneTemplate: _if_ was not compiled in (if can for example be apply-template or some other method), i've found that the problem seems to be an &nbsp; directly before the <xsl:if> or what ever is causing the problem.

One way to get thru the problem is to use span tags around &nbsp;  like : <span>&nbsp;</span>
jw at jwscripts dot com 06-Sep-2004 11:23
The following code is a wrapper to support calls to some of the old xslt_* functions:

<?

if (PHP_VERSION >= 5) {
    // Emulate the old xslt library functions
    function xslt_create() {
        return new XsltProcessor();
    }

    function xslt_process($xsltproc,
                          $xml_arg,
                          $xsl_arg,
                          $xslcontainer = null,
                          $args = null,
                          $params = null) {
        // Start with preparing the arguments
        $xml_arg = str_replace('arg:', '', $xml_arg);
        $xsl_arg = str_replace('arg:', '', $xsl_arg);

        // Create instances of the DomDocument class
        $xml = new DomDocument;
        $xsl = new DomDocument;

        // Load the xml document and the xsl template
        $xml->loadXML($args[$xml_arg]);
        $xsl->loadXML($args[$xsl_arg]);

        // Load the xsl template
        $xsltproc->importStyleSheet($xsl);

        // Set parameters when defined
        if ($params) {
            foreach ($params as $param => $value) {
                $xsltproc->setParameter("", $param, $value);
            }
        }

        // Start the transformation
        $processed = $xsltproc->transformToXML($xml);

        // Put the result in a file when specified
        if ($xslcontainer) {
            return @file_put_contents($xslcontainer, $processed);
        } else {
            return $processed;
        }

    }

    function xslt_free($xsltproc) {
        unset($xsltproc);
    }
}

$arguments = array(
    '/_xml' => file_get_contents("newxslt.xml"),
    '/_xsl' => file_get_contents("newxslt.xslt")
);

$xsltproc = xslt_create();
$html = xslt_process(
    $xsltproc,
    'arg:/_xml',
    'arg:/_xsl',
    null,
    $arguments
);

xslt_free($xsltproc);
print $html;

?>
rojaro 24-Jun-2004 04:43
If you're want to use XML from a variable e.g. $xmldata but the XSLT-StyleSheet from a file, you can do it also the following way:

<?php
$xslt = new xsltProcessor;
$xslt->importStyleSheet(DomDocument::load('filename.xsl'));
print $xslt->transformToXML(DomDocument::loadXML($xmldata));
?>


소개

The XSL extension implements the XSL standard, performing » XSLT transformations using the » libxslt library

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

This extension requires the libxml PHP extension. This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default.

This extension uses libxslt which can be found at » http://xmlsoft.org/XSLT/. libxslt version 1.1.0 or greater is required.

User Contributed Notes

There are no user contributed notes for this page.


설치

PHP 5 includes the XSL extension by default and can be enabled by adding the argument --with-xsl[=DIR] to your configure line (DIR being the libxslt installation directory).

User Contributed Notes

Michael 11-Jun-2009 11:26
On some linux systems you might be able to simply do this:

sudo apt-get install php5-xsl

or similar.


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

XSL_CLONE_AUTO (integer)
XSL_CLONE_NEVER (integer)
XSL_CLONE_ALWAYS (integer)
LIBXSLT_VERSION (integer)
libxslt version like 10117. Available as of PHP 5.1.2.
LIBXSLT_DOTTED_VERSION (string)
libxslt version like 1.1.17. Available as of PHP 5.1.2.
LIBEXSLT_VERSION (integer)
libexslt version like 813. Available as of PHP 5.1.2.
LIBEXSLT_DOTTED_VERSION (string)
libexslt version like 1.1.17. Available as of PHP 5.1.2.
XSL_SECPREF_NONE (integer)
Deactivate all security restrictions.
XSL_SECPREF_READ_FILE (integer)
Disallows reading files.
XSL_SECPREF_WRITE_FILE (integer)
Disallows writing files.
XSL_SECPREF_CREATE_DIRECTORY (integer)
Disallows creating directories.
XSL_SECPREF_READ_NETWORK (integer)
Disallows reading network files.
XSL_SECPREF_WRITE_NETWORK (integer)
Disallows writing network files.
XSL_SECPREF_DEFAULT (integer)
Disallows all write access, i.e. a bitmask of XSL_SECPREF_WRITE_NETWORK | XSL_SECPREF_CREATE_DIRECTORY | XSL_SECPREF_WRITE_FILE.

User Contributed Notes

There are no user contributed notes for this page.


예제

Table of Contents

  • Example collection.xml and collection.xsl files
  • Error handling with libxml error handling functions

User Contributed Notes

hipertracker at gmail dot com 28-Mar-2011 02:49
This is more convenient, no files nor verbose variables needed:

<?php
  $xslt = new XSLTProcessor();
  $xslt->importStylesheet(new SimpleXMLElement($xslt_string));
  echo $xslt->transformToXml(new SimpleXMLElement($xml_string));
?>
contato at log dot blog dot br 16-Oct-2008 02:43
Here's a very simple example on how to use PHP5 to transform a XML file using a XSL file.

<?php

   $xslDoc = new DOMDocument();
   $xslDoc->load("collection.xsl");

   $xmlDoc = new DOMDocument();
   $xmlDoc->load("collection.xml");

   $proc = new XSLTProcessor();
   $proc->importStylesheet($xslDoc);
   echo $proc->transformToXML($xmlDoc);

?>

For the sake of simplicity there's no error handling on this code. I hope this helps.


Example collection.xml and collection.xsl files

Many examples in this reference require both an XML and an XSL file. We will use collection.xml and collection.xsl that contains the following:

Example #1 collection.xml

<collection>
 <cd>
  <title>Fight for your mind</title>
  <artist>Ben Harper</artist>
  <year>1995</year>
 </cd>
 <cd>
  <title>Electric Ladyland</title>
  <artist>Jimi Hendrix</artist>
  <year>1997</year>
 </cd>
</collection>

Example #2 collection.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:param name="owner" select="'Nicolas Eliaszewicz'"/>
 <xsl:output method="html" encoding="iso-8859-1" indent="no"/>
 <xsl:template match="collection">
  Hey! Welcome to <xsl:value-of select="$owner"/>'s sweet CD collection!
  <xsl:apply-templates/>
 </xsl:template>
 <xsl:template match="cd">
  <h1><xsl:value-of select="title"/></h1>
  <h2>by <xsl:value-of select="artist"/> - <xsl:value-of select="year"/></h2>
  <hr />
 </xsl:template>
</xsl:stylesheet>

User Contributed Notes

There are no user contributed notes for this page.


Error handling with libxml error handling functions

libxml offers a number of functions for handling errors, which can be employed to capture and deal with errors in XSLT processing.

Example #1 fruits.xml

A valid XML file.

<fruits>
 <fruit>Apple</fruit>
 <fruit>Banana</fruit>
 <fruit>Cherry</fruit>
</fruits>

Example #2 fruits.xsl

Contains an invalid select expression.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" encoding="utf-8" indent="no"/>
 <xsl:template match="fruits">
  <ul>
   <xsl:apply-templates/>
  </ul>
 </xsl:template>
 <xsl:template match="fruit">
  <li><xsl:value-of select="THIS IS A DELIBERATE ERROR!"/></li>
 </xsl:template>
</xsl:stylesheet>

Example #3 Collating and printing errors

The example below captures and displays libxml errors raised when calling XSLTProcessor::importStyleSheet() with a stylesheet containing an error.

<?php

$xmldoc = new DOMDocument();
$xsldoc = new DOMDocument();
$xsl = new XSLTProcessor();

$xmldoc->loadXML('fruits.xml');
$xsldoc->loadXML('fruits.xsl');

libxml_use_internal_errors(true);
$result = $xsl->importStyleSheet($xsldoc);
if (!$result) {
    foreach (libxml_get_errors() as $error) {
        echo "Libxml error: {$error->message}\n";
    }
}
libxml_use_internal_errors(false);

if ($result) {
    echo $xsl->transformToXML($xmldoc);
}

?>

위 예제의 출력 예시:

Libxml error: Invalid expression

Libxml error: compilation error: file fruits.xsl line 9 element value-of
Libxml error: xsl:value-of : could not compile select expression 'THIS IS A DELIBERATE ERROR!'

User Contributed Notes

There are no user contributed notes for this page.


The XSLTProcessor class

(PHP 5, PHP 7)

소개

클래스 개요

XSLTProcessor {
/* 메소드 */
string getParameter ( string $namespaceURI , string $localName )
public int XsltProcessor::getSecurityPrefs ( void )
bool hasExsltSupport ( void )
public bool importStylesheet ( object $stylesheet )
void registerPHPFunctions ([ mixed $restrict ] )
bool removeParameter ( string $namespaceURI , string $localName )
bool setParameter ( string $namespace , string $name , string $value )
bool setProfiling ( string $filename )
public int XsltProcessor::setSecurityPrefs ( int $securityPrefs )
DOMDocument transformToDoc ( DOMNode $doc )
int transformToURI ( DOMDocument $doc , string $uri )
string transformToXML ( object $doc )
}

Table of Contents

  • XSLTProcessor::__construct — Creates a new XSLTProcessor object
  • XSLTProcessor::getParameter — Get value of a parameter
  • XsltProcessor::getSecurityPrefs — Get security preferences
  • XSLTProcessor::hasExsltSupport — Determine if PHP has EXSLT support
  • XSLTProcessor::importStylesheet — Import stylesheet
  • XSLTProcessor::registerPHPFunctions — Enables the ability to use PHP functions as XSLT functions
  • XSLTProcessor::removeParameter — Remove parameter
  • XSLTProcessor::setParameter — Set value for a parameter
  • XSLTProcessor::setProfiling — Sets profiling output file
  • XsltProcessor::setSecurityPrefs — Set security preferences
  • XSLTProcessor::transformToDoc — Transform to a DOMDocument
  • XSLTProcessor::transformToUri — Transform to URI
  • XSLTProcessor::transformToXML — Transform to XML

User Contributed Notes

tschallacka 29-Feb-2016 06:58
uncomment extension=php_xsl.dll on windows to activate it in your php.ini. Then restart your webserver to refresh php.
flavius 14-Nov-2015 02:49
It requires PHP5 XSL extension. On linux:

sudo apt-get install php5-xsl


Creates a new XSLTProcessor object

  • ≪ XSLTProcessor
  • XSLTProcessor::getParameter ≫
  • PHP Manual
  • XSLTProcessor
  • Creates a new XSLTProcessor object

XSLTProcessor::__construct

(PHP 5, PHP 7)

XSLTProcessor::__construct — Creates a new XSLTProcessor object

설명

XSLTProcessor::__construct ( void )

Creates a new XSLTProcessor object.

인수

이 함수는 인수가 없습니다.

반환값

값을 반환하지 않습니다.

예제

Example #1 Creating an XSLTProcessor

<?php

$xsldoc = new DOMDocument();
$xsldoc->load($xsl_filename);

$xmldoc = new DOMDocument();
$xmldoc->load($xml_filename);

$xsl = new XSLTProcessor();
$xsl->importStyleSheet($xsldoc);
echo $xsl->transformToXML($xmldoc);

?>

User Contributed Notes

There are no user contributed notes for this page.


Get value of a parameter

  • ≪ XSLTProcessor::__construct
  • XsltProcessor::getSecurityPrefs ≫
  • PHP Manual
  • XSLTProcessor
  • Get value of a parameter

XSLTProcessor::getParameter

(PHP 5, PHP 7)

XSLTProcessor::getParameter — Get value of a parameter

설명

string XSLTProcessor::getParameter ( string $namespaceURI , string $localName )

Gets a parameter if previously set by XSLTProcessor::setParameter().

인수

namespaceURI

The namespace URI of the XSLT parameter.

localName

The local name of the XSLT parameter.

반환값

The value of the parameter (as a string), or FALSE if it's not set.

참고

  • XSLTProcessor::setParameter() - Set value for a parameter
  • XSLTProcessor::removeParameter() - Remove parameter

User Contributed Notes

There are no user contributed notes for this page.


Get security preferences

  • ≪ XSLTProcessor::getParameter
  • XSLTProcessor::hasExsltSupport ≫
  • PHP Manual
  • XSLTProcessor
  • Get security preferences

XsltProcessor::getSecurityPrefs

(PHP >= 5.4.0)

XsltProcessor::getSecurityPrefs — Get security preferences

설명

public int XsltProcessor::getSecurityPrefs ( void )

Gets the security preferences.

인수

이 함수는 인수가 없습니다.

반환값

A bitmask consisting of XSL_SECPREF_READ_FILE, XSL_SECPREF_WRITE_FILE, XSL_SECPREF_CREATE_DIRECTORY, XSL_SECPREF_READ_NETWORK, XSL_SECPREF_WRITE_NETWORK.

User Contributed Notes

There are no user contributed notes for this page.


Determine if PHP has EXSLT support

  • ≪ XsltProcessor::getSecurityPrefs
  • XSLTProcessor::importStylesheet ≫
  • PHP Manual
  • XSLTProcessor
  • Determine if PHP has EXSLT support

XSLTProcessor::hasExsltSupport

(PHP 5 >= 5.0.4, PHP 7)

XSLTProcessor::hasExsltSupport — Determine if PHP has EXSLT support

설명

bool XSLTProcessor::hasExsltSupport ( void )

This method determines if PHP was built with the » EXSLT library.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 Testing EXSLT support

<?php

$proc = new XSLTProcessor;
if (!$proc->hasExsltSupport()) {
    die('EXSLT support not available');
}

// do EXSLT stuff here ..

?>

User Contributed Notes

There are no user contributed notes for this page.


Import stylesheet

  • ≪ XSLTProcessor::hasExsltSupport
  • XSLTProcessor::registerPHPFunctions ≫
  • PHP Manual
  • XSLTProcessor
  • Import stylesheet

XSLTProcessor::importStylesheet

(PHP 5, PHP 7)

XSLTProcessor::importStylesheet — Import stylesheet

설명

public bool XSLTProcessor::importStylesheet ( object $stylesheet )

This method imports the stylesheet into the XSLTProcessor for transformations.

인수

stylesheet

The imported style sheet as a DOMDocument or SimpleXMLElement object.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

변경점

버전 | 설명
5.2.8 | Accepts SimpleXMLElement again which was broken since PHP 5.2.6.

User Contributed Notes

rbmeo at yahoo dot com 12-Jul-2012 06:52
To make your import dynamic, try this code:

<?php
$dom = new DOMDocument();
$dom->load('main.xsl');
$xpath = new DomXPath($dom);
$importnode= $questionsXsl->createElement('xsl:include');
$attr= $questionsXsl->createAttribute('href');
$attr->value = 'import.xsl';
$importnode->appendChild($attr);
$dom->documentElement->insertBefore($importnode,$ref);
$dom->loadXml($dom->saveXml());
?>

this code basically loads the main stylesheet, prepend the import xsl code then reload as xml string so the imported stylesheet will be loaded at dom.
mishak@mishak 16-Jun-2011 02:09
actually return value is bool, returns true on success and false on error.
kevin at metalaxe dot com 18-Nov-2007 12:26
Just for reference, as of this writing, this function does not support importing multiple stylesheets. The following will output only the stylesheet transformation of the second imported sheet:

<?php

# LOAD XML FILE
$XML = new DOMDocument();
$XML->load( 'data.xml' );

# START XSLT
$xslt = new XSLTProcessor();

# IMPORT STYLESHEET 1
$XSL = new DOMDocument();
$XSL->load( 'template1.xsl' );
$xslt->importStylesheet( $XSL );

#IMPORT STYLESHEET 2
$XSL = new DOMDocument();
$XSL->load( 'template2.xsl' );
$xslt->importStylesheet( $XSL );

#PRINT
print $xslt->transformToXML( $XML );
?>

This wasn't documented and quite dissapointing.
fcartegnie 21-Jul-2007 03:11
PHP5 xsl processor has a different behaviour than PHP4's one with CDATA sections. (see http://bugs.php.net/bug.php?id=29837)
Loaded XSL sheet CDATA sections does not allow, by default, output-escaping handling (everything in the CDATA is escaped by default).

So in this case you can't build your XSL Dom the usual way:
    $xsldom = DomDocument::loadXML(file_get_contents('sheet.xsl'));

and must go through this one (allowing LIBXML_NOCDATA parameter):
    $xsldom = new DomDocument;
    $xsldom->load('sheet.xsl', LIBXML_NOCDATA);

Then the CDATA output-escaping behaviour will be correct.
diesel at spbnet dot ru 19-Mar-2007 06:07
This is not a problem. You may set DOMDocument's documentURI property.
Something like this

<?php
$xsl = new DOMDocument('1.0','UTF-8');

$xsl->loadXML(file_get_contents('/foo/bar/somefile.xsl');
$xsl->documentURI = '/foo/bar/somefile.xsl';

$xslProc = new XSLTProcessor();
$xslProc->importStylesheet($xsl);
?>

and document('other.xsl') will work fine!
bbrosseau at gmail 02-Mar-2005 05:46
For those who wants to use external documents, it is important not to use the DomDocument::loadXML because the processor will not have the path to look for other files

So if you want to transform some xml with a pre-generated stylesheet $f:

<?php
$f = 'somestylesheet.xsl';
$xsl = DomDocument::loadXML(file_get_contents($f));
?>

document('other.xml') will not work with relative path and <?php $xsl = DomDocument::load($f); ?> will!


Enables the ability to use PHP functions as XSLT functions

  • ≪ XSLTProcessor::importStylesheet
  • XSLTProcessor::removeParameter ≫
  • PHP Manual
  • XSLTProcessor
  • Enables the ability to use PHP functions as XSLT functions

XSLTProcessor::registerPHPFunctions

(PHP 5 >= 5.0.4, PHP 7)

XSLTProcessor::registerPHPFunctions — Enables the ability to use PHP functions as XSLT functions

설명

void XSLTProcessor::registerPHPFunctions ([ mixed $restrict ] )

This method enables the ability to use PHP functions as XSLT functions within XSL stylesheets.

인수

restrict

Use this parameter to only allow certain functions to be called from XSLT.

This parameter can be either a string (a function name) or an array of functions.

반환값

값을 반환하지 않습니다.

예제

Example #1 Simple PHP Function call from a stylesheet

<?php
$xml = <<<EOB
<allusers>
 <user>
  <uid>bob</uid>
 </user>
 <user>
  <uid>joe</uid>
 </user>
</allusers>
EOB;
$xsl = <<<EOB
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:php="http://php.net/xsl">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
 <xsl:template match="allusers">
  <html><body>
    <h2>Users</h2>
    <table>
    <xsl:for-each select="user">
      <tr><td>
        <xsl:value-of
             select="php:function('ucfirst',string(uid))"/>
      </td></tr>
    </xsl:for-each>
    </table>
  </body></html>
 </xsl:template>
</xsl:stylesheet>
EOB;
$xmldoc = DOMDocument::loadXML($xml);
$xsldoc = DOMDocument::loadXML($xsl);

$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);
?>

변경점

버전 | 설명
5.1.0 | The restrict parameter was added.

User Contributed Notes

Matthew, info at mathias-schaefer dot de 25-Nov-2010 05:34
=> there is no instance of your class needed, you have to take care about the namespace (using 5.3).

<!-- xsl:
<xsl:value-of select="php:function ('My\Name\Space\MyClassName::methodName',1+2)"/>
--->

<?php
namespace My\Name\Space;

   class MyClassName
   {
      static public function methodName($someArgument)
      {
         return "resultis:".$someArgument;
      }
   }
?>

<!-- result:
resultis:3
-->

=> no to-string conversion of the argument needed
=> returning numbers is also possible (maybe DOM nodes too)

Keep on hackin'
Demerit 10-Aug-2010 04:58
Let's get the terminology correct here, folks.

If you are calling a function that is residing within a class, reference it by its INSTANCE name, NOT its class name.

class.my_class.php:

<?php
class class_name {
static function function_name($in){
return ($out);
}
}
?>

calling_document.php:

<?php
require_once('class.my_class.php');
$instance_name=new class_name();
?>

my_xslt.xsl:

<xsl:value-of select="php:function ('instance_name::function_name','garbage')" />

BTW, in 5.1.6 double-colon (::) is the only thing that works when calling a function that resides within a class. However, you must declare your function as static. Using -> as some have suggested (e.g., php:function ('instance_name->function_name','garbage')) doesn't work at all, no matter how you declare your function.
Nic Luciano 22-Jul-2009 08:09
Ritch said "If you wish to use a function from inside a class use the double colon (::) notation..."

Also note that the class function must be declared as static.
Ariz Jacinto 08-Jul-2008 11:21
If you want to use replace() function similar to XPath 2.0[1]  and you want to avoid a long XSLT function similar to this one[2], you can use PHP's str_replace() instead:

   <xsl:value-of select="php:functionString('str_replace','pattern', 'replacement' , . )">

Links:
  [1] http://www.w3.org/TR/2004/WD-xpath-functions-20041029/#func-replace
  [2] http://aspn.activestate.com/ASPN/Cookbook/XSLT/Recipe/65426

_
thsoft at vipmail dot hu 27-Dec-2007 11:22
Unfortunately, you can't pass arrays to PHP from XLST.
But you can pass an arbitrary number of arguments to a PHP function and then get them with func_get_args() as an array.
[There's no sense writing a function that is identical to the array() construct, because a function's return value is converted to a string when passed as an argument to another function. This means deep arrays cannot be handled this way.]
thsoft at vipmail dot hu 01-Feb-2007 12:20
The correct way to receive DOM nodes (let it be element, document, document fragment etc.) returned from PHP is to use <xsl:copy-of select="php:function...

I found the idea and an example here: http://bugs.php.net/bug.php?id=29409

For the technical details about the reason, see http://www.w3.org/TR/xslt#copy-of
phil at worldpoolfederation dot com 01-Dec-2006 12:22
Not true

Use :: to call static functions
Use -> to call non static functions

You must define a function as static in php 5.1 to be able to call it statically.
mark at thirst dot org 11-Oct-2006 04:25
Ritch said "If you wish to use a function from inside a class use the double colon (::) notation..."   This worked in 5.0.4 but no longer works in 5.1.6.
franp at free dot fr 02-Sep-2006 10:45
Note that if you want your output to validate against some xhtml dtd, you must add the following attribute to the xsl:stylesheet element of the xslt stylesheet :
exclude-result-prefixes="php".

Otherwise, you get an "invalid attribute xmlns:php" error.
taylorbarstow at that google mail thingy 26-May-2006 02:33
Add-on to my previous note (below) about returning nodesets to XSLT from PHP functions:

You don't have to return a DOMDocument, DOMElement works just as well.  Plus, retuning a DOMElement gets around the problem of discarding the root node which I discuss below and which is also touched on by "Ingram".
heinemann dot juergen at hjcms dot de 08-Mar-2006 11:23
You can find mor Examples at PHP Sources php-5.*/ext/xsl/tests
<?php

$xform = <<<EOT
<?xml version = '1.0' encoding = 'utf-8' ?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:php="http://php.net/xsl"
    xsl:extension-element-prefixes="php"
>
<xsl:output method="xml" indent="yes" encoding="utf-8" />
<xsl:namespace-alias stylesheet-prefix="php" result-prefix="xsl" />
<xsl:template match="root">
<html>
<head>
    <title>Dateformat</title>
</head>
<body>
<xsl:for-each select="datenode">
    <li>
        <xsl:value-of select="php:functionString('convertDate', . )" />
    </li>
    </xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
EOT;

function convertDate( $i )
{
    setlocale( LC_TIME, 'de_DE' );
    return utf8_encode( strftime( '%B %d %A %Y %H:%M:%S CET', $i ) );
}

$xsl = new XSLTProcessor;
$xsl->registerPHPFunctions();
$xsl->setParameter( 'DOCTYPE', 'PUBLIC', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' );
$xsl->setParameter( 'html', 'xmlns', 'http://www.w3.org/1999/xhtml' );

$xdom = new DomDocument( '1.0', 'utf-8' );
$xdom->loadXML( $xform );

$xsl->importStyleSheet( $xdom );
unset( $xdom );

$dom = new DomDocument( '1.0', 'utf-8' );
$r = $dom->appendChild( $dom->createElement( 'root' ) );
foreach ( range( 1, 12 ) AS $i ) {
    $r->appendChild( $dom->createElement( 'datenode', mktime( date('G'), date('i'), date('s'), $i, date('d'), date('Y') ) ) );
}

header( "Content-Type: text/html; CHARSET=ks_c_5601-1987;" );
header( "Content-Encoding: utf-8" );
echo $xsl->transformToXML( $dom );

?>
Ingram 28-Feb-2006 09:33
Upon testing returning of a nodeset contributed by

taylorbarstow at that google mail thingy

(which works excellently, TY!)

I found that with using:

===
"Presumably, it's worth creating a template to do the discard:

<xsl:template select="*" mode="discardRoot">
   <xsl:apply-templates select="./*" />
</xsl:template>

Which you can call like so:

<xsl:apply-templates select="php:function('getNodeSet')" mode="discardRoot" /> "
===

I could only output the text and not any of the tags after applying templates - i.e. it stripped all elements around text.

Instead using:

===
<xsl:template match="/">
   <xsl:for-each select="php:function('getNodeSet')" />
     <xsl:apply-templates />
   </xsl:for-each>
</xsl:template>

which effectively discards the root node.
===

Worked fine and allowed me to apply-templates without problem on the returned nodeset.
hopeseekr at gmail dot com 01-Feb-2006 09:17
One of the peskiest things I had problems with was encoding URL parameters.  I mean, pretend you want to populate a link with "search+terms" instead of just "search terms".  I was including two seperate URLs in the XML and that was ludicrous.

Below is a far more elegant PHP+XSLT solution.  You will also see it uses two *undocumented* features of registerPHPFunctions(), namely php:functionString() and the passing of parameters to the function.  I figured this out by trial and error; I really hope this note helps you as it *greatly* expands the power of php functions in XSLT!

<?php
/* --- XML input --- */
<search_results>
   <query>concert tickets</query>
</search_results>

/* --- XSL code --- */
<!-- Display query -->
<xsl:template match="search_results">
   <!-- Get URL-encoded string via PHP -->
   <xsl:variable name="safeurl" as="xs:string" select="php:functionString('urlencode', query)" />
   <p>Your search for <em><xsl:value-of select="query"/></em> can be continued at <a href="http://www.tixtix.com/search.php?q={$safeurl}">our search engine</a></p>
</xsl:template>

/* --- XHTML output --- */
<p>Your search for <em>concert tickets</em> can be continued at <a href="http://www.tixtix.com/search.php?q=space+cowboy">our search engine</a></p>
?>

Cool, huh?
taylorbarstow at that google mail thingy 01-Dec-2005 04:14
From a PHP function, you can pass a nodeset back to XSL using a DOMDocument.  For example:

<?php

function getNodeSet() {
   $xml =
       "<test>" .
       "<a-node>This is a node</a-node>" .
       "<a-node>This is another node</a-node>" .
       "</test>";
   $doc = new DOMDocument;
   $doc->loadXml($xml);
   return $doc;
}

?>

The only problem I've found is that the root level node in your returned DOM document acts like the root level node of your original.   SO, it's easy to introduce an infinite loop like so:

<xsl:template match="/">
   <xsl:apply-templates select="php:function('getNodeSet')" />
</xsl:template>

To avoid this, I've been using a construct like:

<xsl:template match="/">
   <xsl:for-each select="php:function('getNodeSet')" />
      <xsl:apply-templates />
   </xsl:for-each>
</xsl:template>

which effectively discards the root node.  Presumably, it's worth creating a template to do the discard:

<xsl:template select="*" mode="discardRoot">
   <xsl:apply-templates select="./*" />
</xsl:template>

Which you can call like so:

<xsl:apply-templates select="php:function('getNodeSet')" mode="discardRoot" />
Ritch at Bugsoftware dot co dot uk 22-Jun-2005 12:18
If you wish to use a function from inside a class use the double colon (::) notation, for example;

php:functionString('classname::function')

The funtion is fired off as a static and as such acts like a function in the global namespace.
benbarnett 04-Mar-2005 01:34
You can use the php:functionString() in the XSL, which will automatically convert output to a string!
zac at zacbowling dot com 27-Oct-2004 10:48
One thing I have told a lot of people to do if they are having
issues with this function is to check for any 'xmlns' attributes
that get generated and added to your xml pages by some
types of popular software.

<?php
$file = "http://data.map***.net/m***ck.asmx/GetMessages?IMEI=$id";

$docxml = file_get_contents($file);

//You may have to do something like this where
//I remove any instance of xmlns tags that get
//returned by my ASP.NET SOAP responses.

$docxml =
 str_replace("xmlns=\"http://data.map***.net/m***ck.asmx?WSDL\"",
  "",$docxml);

$xslt = new xsltProcessor;

//You don't remove them then this function will blow up.
$xslt->registerPHPFunctions();
$xslt->importStyleSheet(DomDocument::load('../xsl/message.xsl'));
print $xslt->transformToXML(DomDocument::loadXML($docxml));

?>

Also a few cool tricks with this function is that you can call
built in PHP functions. For example:

<xsl:value-of
  select="php:function('nl2br',string(MessageContent/Message))"
  disable-output-escaping="yes"/>

That XSL value will now return your normal string but replace
all your new line charactors in your xml with '<br />'s.

Also note the 'disable-output-escaping="yes"' statement. If
you don't call this, then the output of that bind will be ran
thru basicly a "htmlencode()" type function.

Last but not least, take a look at the 'string()' function I
called in XSL before passing it back. That is because without
calling that, when it runs it will try and pass the node object,
and not its value (which is what you most likely only want).

This function is very awsome and could lead to some very
interesting code development. Skins could be loaded
remotely. You could write an RSS viewer in PHP without much
code. You could parse XHTML pages into another view (either
localy or remotely). Then you can take that same XML
content and throw it against ASP.NET, Java, or even a
command line processing tool using that same exact XSL
style sheet and generate the front ends for you page without
much change. I'm very excited.

Happy codding.
junkmail at eighteyes dot com 08-Oct-2004 11:52
When writing a stylesheet that uses a callback function be sure to include a namespace declaration for php, as follows:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl" version='1.0'>
begemot at php dot com dot ua 28-Sep-2004 03:24
I think it help  your.
<?php

function dateLang () {
        return strftime("%A");
}

$xsl = new DomDocument();
$xsl->load("datetime.xsl");
$inputdom = new DomDocument();
$inputdom->load("today.xml");

$proc = new XsltProcessor();
$proc->registerPhpFunctions();

// Load the documents and process using $xslt
$xsl = $proc->importStylesheet($xsl);

/* transform and output the xml document */
$newdom = $proc->transformToDoc($inputdom);

print $newdom->saveXML();

?>

Here's the XSLT stylesheet, datetime.xsl, that will call that function:

<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:template match="/">
  <xsl:value-of select="php:function('dateLang')" />
</xsl:template>
</xsl:stylesheet>

And here's an absolute minimal XML file, today.xml, to pass through the stylesheet (although articles.xml would achieve the same result):

<?xml version="1.0" encoding="iso-8859-1" ?>
<today></today>
zac at zacbowling dot com 27-Aug-2004 06:48
One thing to note about this function. A lot of values need to be converted to a XSLT string using the "string()" function in XLS before you pass them to your functions, and when you return them make sure that if they are strings that you call the "strval()" in php before doing so. This saved me hours.

Hope that helps.

Zac Bowling


Remove parameter

  • ≪ XSLTProcessor::registerPHPFunctions
  • XSLTProcessor::setParameter ≫
  • PHP Manual
  • XSLTProcessor
  • Remove parameter

XSLTProcessor::removeParameter

(PHP 5, PHP 7)

XSLTProcessor::removeParameter — Remove parameter

설명

bool XSLTProcessor::removeParameter ( string $namespaceURI , string $localName )

Removes a parameter, if set. This will make the processor use the default value for the parameter as specified in the stylesheet.

인수

namespaceURI

The namespace URI of the XSLT parameter.

localName

The local name of the XSLT parameter.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

참고

  • XSLTProcessor::setParameter() - Set value for a parameter
  • XSLTProcessor::getParameter() - Get value of a parameter

User Contributed Notes

There are no user contributed notes for this page.


Set value for a parameter

  • ≪ XSLTProcessor::removeParameter
  • XSLTProcessor::setProfiling ≫
  • PHP Manual
  • XSLTProcessor
  • Set value for a parameter

XSLTProcessor::setParameter

(PHP 5, PHP 7)

XSLTProcessor::setParameter — Set value for a parameter

설명

bool XSLTProcessor::setParameter ( string $namespace , string $name , string $value )
bool XSLTProcessor::setParameter ( string $namespace , array $options )

Sets the value of one or more parameters to be used in subsequent transformations with XSLTProcessor. If the parameter doesn't exist in the stylesheet it will be ignored.

인수

namespace

The namespace URI of the XSLT parameter.

name

The local name of the XSLT parameter.

value

The new value of the XSLT parameter.

options

An array of name => value pairs. This syntax is available since PHP 5.1.0.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 Changing the owner before the transformation

<?php

$collections = array(
    'Marc Rutkowski' => 'marc',
    'Olivier Parmentier' => 'olivier'
);

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

foreach ($collections as $name => $file) {
    // Load the XML source
    $xml = new DOMDocument;
    $xml->load('collection_' . $file . '.xml');

    $proc->setParameter('', 'owner', $name);
    $proc->transformToURI($xml, 'file:///tmp/' . $file . '.html');
}

?>

참고

  • XSLTProcessor::getParameter() - Get value of a parameter
  • XSLTProcessor::removeParameter() - Remove parameter

User Contributed Notes

richard at aggmedia dot net 05-Mar-2009 04:47
Note that there is no way to remove a parameter from an XSLTProcessor unless you know its name, and there is no way (that I can find) to get a list of the current parameters.

This means that you cannot reuse an XSLTProcessor with different parameters unless you call XSLTProcessor->removeParameter() on every parameter, and to do that you need to know the names of all the currently set parameters.

I bumped into this because we were caching XSLTProcessors for reuse, and they were spitting out content based on phantom parameters (they were still there from previous uses).
Lennaert van der Linden 13-Feb-2008 03:28
The parameter will not be set if the value contains both single and double quotes. Instead a warning will be shown when transforming the document:

PHP Warning:  XSLTProcessor::transformToXml(): Cannot create XPath expression (string contains both quote and double-quotes)
brettz9 09-May-2007 05:27
It seems heinemann's usage is not correct and does not achieve the intended result.

This method's purpose is to change a global <xsl:param> value in the XSL stylesheet--not to change an attribute of any other element.  <xsl:param> basically lets you set up a stylesheet which can be customized (as from PHP) externally (without needing to tamper with the original XSL file).

Here's an example of usage (that will work):

Stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="print_something" select="defaultstring"/>
    <xsl:template match="/mydoc">
        <p style="color:red;">Printed parameter: <xsl:value-of select="$print_something"/></p>
    </xsl:template>
</xsl:stylesheet>

Script:

<?php
$dom = new DOMDocument();
$xsl = new XSLTProcessor;
$xsl->setParameter( '', 'print_something', "Now I've overridden the default!");
$style = realpath( "./my_stylesheet.xslt" );
$dom->load($style);
$xsl->importStyleSheet($dom);
$dom->loadXML('<mydoc></mydoc>');

$out = $xsl->transformToXML( $dom );

var_dump( '<pre>',
    htmlentities( $out, ENT_QUOTES, 'utf-8' ),
    $xsl->getParameter('', 'print_something'),
'</pre>' );
?>

gives:

string(5) "

"
string(143) "<?xml version="1.0"?>
<p style="color:red;">Printed parameter: Now I've overridden the default!</p>
"
string(32) "Now I've overridden the default!"
string(6) "

"

Notice that at present adding a namespace will not work. The only option at present is to set the first parameter for namespace to an empty string (though you can add the prefix with colon to the second argument for name in order to set the parameter for a namespace-prefixed parameter name).

See http://bugs.php.net/bug.php?id=30622
OrionI 01-May-2007 04:19
After looking at this a little further (see http://bugs.php.net/bug.php?id=41248), it appears that it's a shortcoming of libxslt, not PHP, that prevents passing in DOMDocuments or DOMNodes as parameters.
Orion I 01-May-2007 05:46
I've been trying to pass in a DOMDocument object as a parameter so I can stuff a bunch of data into XML nodes, but it appears that this function is not capable of it. I was hoping to get it to work like it does in the .NET 2.0 framework. (See http://msdn2.microsoft.com/en-us/library/
system.xml.xsl.xsltargumentlist.addparam.aspx)
But after looking at the PHP 5.2.1 source code, /php-5.2.1/ext/xsl/xsltprocessor.c line 604-650, it appears that it's not possible to do so in PHP even though it appears that libxslt supports it (see http://xmlsoft.org/XSLT/html/libxslt-variables.html
#xsltParseGlobalParam)

In fact, if the parameters aren't exactly what's expected, you'll always get a warning like this:

Wrong parameter count for XSLTProcessor::setParameter()
heinemann dot juergen at hjcms dot de 07-Mar-2006 12:39
Example for how it works.

<?xml version = '1.0' encoding = 'utf-8' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"
    indent="yes"
    encoding="ISO-8859-15"
    doctype-system = "-//W3C//DTD XHTML 1.0 Transitional//EN"
    doctype-public = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
/>
<xsl:template match="docs">
<html>
<head>
    <title>
        <xsl:text>Example</xsl:text>
    </title>
</head>
<body>
    <xsl:for-each select="block">
        <div>
            <xsl:value-of select="." />
        </div>
    </xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

------------------

<?php
$dom = new DomDocument( '1.0', 'utf-8' );
$xsl = new XSLTProcessor;
$xsl->setParameter( 'block', 'xmlns', 'http://www.w3.org/1999/xhtml' );

$style = realpath( "./my_stylesheet.xslt" );
$dom->load( $style );
$xsl->importStyleSheet( $dom );

$dom->loadXML( '<docs>
    <block>Howto set xhtml Transitional Namespaces width php</block>
    <block>see http://www.php.net</block>
</docs>' );

$out = $xsl->transformToXML( $dom );

var_dump( '<pre>',
    htmlentities( $out, ENT_QUOTES, 'utf-8' ),
    $xsl->getParameter( 'block', 'xmlns' ),
    $xsl->getParameter( 'docs', 'xmlns' ),
'</pre>' );

?>
Stoke A 12-Aug-2005 12:40
XSLTProcessor::removeParameter() won't work on parameters set via XSLTProcessor::setParameter(string namespace, array options) in 5.1+ -- they need to be set and removed one by one.


Sets profiling output file

  • ≪ XSLTProcessor::setParameter
  • XsltProcessor::setSecurityPrefs ≫
  • PHP Manual
  • XSLTProcessor
  • Sets profiling output file

XSLTProcessor::setProfiling

(PHP >= 5.3.0)

XSLTProcessor::setProfiling — Sets profiling output file

설명

bool XSLTProcessor::setProfiling ( string $filename )

Sets the file to output profiling information when processing a stylesheet.

인수

filename

Path to the file to dump profiling information.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 Example profiling output

<?php
// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->setProfiling('profiling.txt');
$proc->importStyleSheet($xsl); // attach the xsl rules

echo trim($proc->transformToDoc($xml)->firstChild->wholeText);
?>

The above code will produce the following information in the profiling file:

number               match                name      mode  Calls Tot 100us Avg

    0                   cd                                    2      3      1
    1           collection                                    1      1      1

                         Total                                3      4

User Contributed Notes

There are no user contributed notes for this page.


Set security preferences

  • ≪ XSLTProcessor::setProfiling
  • XSLTProcessor::transformToDoc ≫
  • PHP Manual
  • XSLTProcessor
  • Set security preferences

XsltProcessor::setSecurityPrefs

(PHP >= 5.4.0)

XsltProcessor::setSecurityPrefs — Set security preferences

설명

public int XsltProcessor::setSecurityPrefs ( int $securityPrefs )

Sets the security preferences.

인수

securityPrefs

The new security preferences. The following constants can be ORed: XSL_SECPREF_READ_FILE, XSL_SECPREF_WRITE_FILE, XSL_SECPREF_CREATE_DIRECTORY, XSL_SECPREF_READ_NETWORK, XSL_SECPREF_WRITE_NETWORK. Alternatively, XSL_SECPREF_NONE or XSL_SECPREF_DEFAULT can be passed.

반환값

Returns the old security preferences.

User Contributed Notes

There are no user contributed notes for this page.


Transform to a DOMDocument

  • ≪ XsltProcessor::setSecurityPrefs
  • XSLTProcessor::transformToUri ≫
  • PHP Manual
  • XSLTProcessor
  • Transform to a DOMDocument

XSLTProcessor::transformToDoc

(PHP 5, PHP 7)

XSLTProcessor::transformToDoc — Transform to a DOMDocument

설명

DOMDocument XSLTProcessor::transformToDoc ( DOMNode $doc )

Transforms the source node to a DOMDocument applying the stylesheet given by the XSLTProcessor::importStylesheet() method.

인수

doc

The node to be transformed.

반환값

The resulting DOMDocument or FALSE on error.

예제

Example #1 Transforming to a DOMDocument

<?php

// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

echo trim($proc->transformToDoc($xml)->firstChild->wholeText);

?>

위 예제의 출력:

Hey! Welcome to Nicolas Eliaszewicz's sweet CD collection!

참고

  • XSLTProcessor::transformToUri() - Transform to URI
  • XSLTProcessor::transformToXml() - Transform to XML

User Contributed Notes

franp at free dot fr 31-Aug-2006 02:16
In most cases if you expect XML (or XHTML) as output you better use transformToXML() directly. You gain better control over xsl:output attributes, notably omit-xml-declaration.

Instead of :
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$dom = $proc->transformToDoc($xml);
echo $dom->saveXML();

do use :
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$newXml = $proc->transformToXML($xml);
echo $newXml;

In the first case, <?xml version="1.0" encoding="utf-8"?> is added whatever you set the omit-xml-declaration while transformToXML() take the attribute into account.


Transform to URI

  • ≪ XSLTProcessor::transformToDoc
  • XSLTProcessor::transformToXML ≫
  • PHP Manual
  • XSLTProcessor
  • Transform to URI

XSLTProcessor::transformToUri

(PHP 5, PHP 7)

XSLTProcessor::transformToUri — Transform to URI

설명

int XSLTProcessor::transformToURI ( DOMDocument $doc , string $uri )

Transforms the source node to an URI applying the stylesheet given by the XSLTProcessor::importStylesheet() method.

인수

doc

The document to transform.

uri

The target URI for the transformation.

반환값

Returns the number of bytes written or FALSE if an error occurred.

예제

Example #1 Transforming to a HTML file

<?php

// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

$proc->transformToURI($xml, 'file:///tmp/out.html');

?>

참고

  • XSLTProcessor::transformToDoc() - Transform to a DOMDocument
  • XSLTProcessor::transformToXml() - Transform to XML

User Contributed Notes

jonbarnett at gmail dot com 13-Jul-2007 09:32
Sometimes you don't want to transform to a file, an XML string (because you're using text or html), or a DOMDocument.

To transform to standard output, you can use php://output

<?php
$proc->transformToURI($xml, 'php://output');
?>

To transform to a string (of HTML or text, instead of XML), you can use the above in conjunction with output buffering.
<?php
ob_start();
$proc->transformToURI($xml, 'php://output');
$outputString = ob_get_flush();
?>


Transform to XML

  • ≪ XSLTProcessor::transformToUri
  • GUI Extensions ≫
  • PHP Manual
  • XSLTProcessor
  • Transform to XML

XSLTProcessor::transformToXML

(PHP 5, PHP 7)

XSLTProcessor::transformToXML — Transform to XML

설명

string XSLTProcessor::transformToXML ( object $doc )

Transforms the source node to a string applying the stylesheet given by the xsltprocessor::importStylesheet() method.

인수

doc

The DOMDocument or SimpleXMLElement object to be transformed.

반환값

The result of the transformation as a string or FALSE on error.

예제

Example #1 Transforming to a string

<?php

// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

echo $proc->transformToXML($xml);

?>

위 예제의 출력:

Hey! Welcome to Nicolas Eliaszewicz's sweet CD collection!

<h1>Fight for your mind</h1><h2>by Ben Harper - 1995</h2><hr>
<h1>Electric Ladyland</h1><h2>by Jimi Hendrix - 1997</h2><hr>

참고

  • XSLTProcessor::transformToDoc() - Transform to a DOMDocument
  • XSLTProcessor::transformToUri() - Transform to URI

User Contributed Notes

Josef Hornych 05-Mar-2015 04:42
Note that the method's name is sort of deceiving, because it does not only output XML, but any string that is generated by the processor. It should rather be called transformToString. So if your output method is  "text/plain", for example, this is the way to receive the resulting string.
zweibieren at yahoo dot com 15-Sep-2010 08:15
Entities are omitted from the output with the code above.
The symptom was that &nbsp;
-- which should work with UTF-8 encoding --
did not even get to XSLTProcessor, let alone through it.
After too much hacking I discovered the simple fix:
set substituteEntities to true in the DOMDocument for the XSL file.
That is, replace the loading of the xsl document with

<?php
   $xsl = new DOMDocument;
   $xsl->substituteEntities = true;    // <===added line
   $xsl->load('collection.xsl');
?>

However, this fails when data entries have HTML entity references. (Some database entries may even contain user generated text.) libxml has the pedantic habit of throwing a FATAL error for any undefined entitiy. Solution: hide the entities so libxml doesn't see them.

<?php
function hideEntities($data) {
        return str_replace("&", "&amp;", $data);
}
?>

You could just add this to the example, but it is tidier to define a function to load data into a DOMDocument. This way you don't need entity declarations in catalog.xsl, either.

<?php
// Added function for  Example #1

/** Load an XML file and create a DOMDocument.
Handles arbitrary entities, even undefined ones.
*/
function fileToDOMDoc($filename) {
    $dom= new DOMDocument;
    $xmldata = file_get_contents($filename);
    $xmldata = str_replace("&", "&amp;", $xmldata);  // disguise &s going IN to loadXML()
    $dom->substituteEntities = true;  // collapse &s going OUT to transformToXML()
    $dom->loadXML($xmldata);
    return $dom;
}

// Compare with  Example #1 Transforming to a string

// Load the XML sources
$xml = fileToDOMDoc('collection.xml');
$xsl = fileToDOMDoc('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);

// transform $xml according to the stylesheet $xsl
echo $proc->transformToXML($xml); // transform the data
?>
michael dot weibel at tilllate dot com 03-Dec-2009 10:33
If you retrieve "false" from the transformToXML method, use libxml_get_last_error() or libxml_get_errors() to retrieve the errors.
Charlie Murder 29-Nov-2009 04:43
To prevent your xsl file from automatically prepending

<?xml version="1.0"?>

whilst keeping the output as xml, which is preferable for a validated strict xhtml file, rather specify output as

<xsl:output method="xml" omit-xml-declaration="yes" />
john 05-Feb-2008 08:09
If your xsl doesn't have <html> tags. The output will contain <?xml version="1.0"?>. To fix this you can add the following to your xsl stylesheet:

<xsl:output
method="html" />
smubldg4 at hotmail dot com 03-Oct-2007 05:58
How to fix:: *Fatal error: Call to undefined method domdocument::load()*

If you get this error, visit the php.ini file and try commenting out the following, like this:

;[PHP_DOMXML]
;extension=php_domxml.dll

Suddenly, the wonderfully simple example above works as advertised.
jeandenis dot boivin at gmail dot com 14-Mar-2007 01:59
$domTranObj = $xslProcessor->transformToDoc($domXmlObj);
$domHtmlText = $domTranObj->saveHTML();

Do fix the <meta> for valid XHTML but do not correctly end empty node like <br /> which ouput like this : <br></br>

Some browser note this as 2 different <br /> ...

To fix this use

$domTranObj = $xslProcessor->transformToDoc($domXmlObj);
$domHtmlText = $domTranObj->saveXML();
Thomas Praxl 22-Jan-2007 10:36
I noticed an incompatibility between libxslt (php4) and the transformation through XSLTProcessor.
Php5 and the XSLTProcessor seem to add implicit CDATA-Section-Elements.
If you have an xslt like

<script type="text/javascript">
foo('<xsl:value-of select="bar" />');
</script>

It will result in

<script type="text/javascript"><![CDATA[
foo('xpath-result-of-bar');
]]></script>

(at least for output method="xml" in order to produce strict xhtml with xslt1)

That brings up an error (at least) in Firefox 1.5 as it is no valid javascript.
It should look like that:

<script type="text/javascript">//<![CDATA[
foo('xpath-result-of-bar');
]]></script>

As the CDATA-Section is implicit, I was not able to disable the output or to put a '//' before it.

I tried everything about xsl:text disable-output-escaping="yes"

I also tried to disable implicit adding of CDATA with <output cdata-section-elements="" />
(I thought that would exclude script-tags. It didn't).

The solution:

<xsl:text disable-output-escaping="yes">&lt;script type="text/javascript"&gt;
  foo('</xsl:text><xsl:value-of select="bar" /><xsl:text disable-output-escaping="yes">');
            &lt;/script&gt;</xsl:text>

Simple, but it took me a while.
werner@mollentze 15-Aug-2006 10:53
The transformToXML function can produce valid XHTML output - it honours the <xsl:output> element's attributes, which defines the format of the output document.

For instance, if you want valid XHTML 1.0 Strict output, you can provide the following attribute values for the <xsl:output> element in your XSL stylesheet:

<xsl:output
method="xml"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" />
lsoethout at hotmail dot com 19-Feb-2006 11:35
The function transformToXML has a problem with the meta content type tag. It outputs it like this:

    <meta http-equiv="Content-Type" content="text/html; CHARSET=ks_c_5601-1987">

which is not correct X(HT)ML, because it closes with '>' instead of with '/>'.

A way to get the output correct is to use instead of transformToXML first transformToDoc anf then saveHTML:

    $domTranObj = $xslProcessor->transformToDoc($domXmlObj);
    $domHtmlText = $domTranObj->saveHTML();
jlipps at mac 06-Jan-2005 05:05
transformToXML, if you have registered PHP functions previously, does indeed attempt to execute these functions when it finds them in a php:function() pseudo-XSL function. It even finds static functions within classes, for instance:

<xsl:value-of select="php:function('MyClass::MyFunction', string(@attr), string(.))" disable-output-escaping="yes"/>

However, in this situation transformToXML does not try to execute "MyClass::MyFunction()". Instead, it executes "myclass:myfunction()". In PHP, since classes and functions are (I think) case-insensitive, this causes no problems.

A problem arises when you are combining these features with the __autoload() feature. So, say I have MyClass.php which contains the MyFunction definition. Generally, if I call MyClass::MyFunction, PHP will pass "MyClass" to __autoload(), and __autoload() will open up "MyClass.php".

What we have just seen, however, means that transformToXML will pass "myclass" to __autoload(), not "MyClass", with the consequence that PHP will try to open "myclass.php", which doesn't exist, instead of "MyClass.php", which does. On case-insensitive operating systems, this is not significant, but on my RedHat server, it is--PHP will give a file not found error.

The only solution I have found is to edit the __autoload() function to look for class names which are used in my XSL files, and manually change them to the correct casing.

Another solution, obviously, is to use all-lowercase class and file names.


GUI Extensions

  • UI
    • 소개
    • 설치/설정
    • UI\Point — Represents a position (x,y)
    • UI\Size — Represents dimenstions (width, height)
    • UI\Window — Window
    • UI\Control — Control
    • UI\Menu — Menu
    • UI\MenuItem — Menu Item
    • UI\Area — Area
    • UI\Executor — Execution Scheduler
    • UI\Controls\Tab — Tab Control
    • UI\Controls\Check — Check Control
    • UI\Controls\Button — Button Control
    • UI\Controls\ColorButton — ColorButton Control
    • UI\Controls\Label — Label Control
    • UI\Controls\Entry — Entry Control
    • UI\Controls\MultilineEntry — MultilineEntry Control
    • UI\Controls\Spin — Spin Control
    • UI\Controls\Slider — Slider Control
    • UI\Controls\Progress — Progress Control
    • UI\Controls\Separator — Control Separator
    • UI\Controls\Combo — Combo Control
    • UI\Controls\EditableCombo — EdiableCombo Control
    • UI\Controls\Radio — Radio Control
    • UI\Controls\Picker — Picker Control
    • UI\Controls\Form — Control Form (Arrangement)
    • UI\Controls\Grid — Control Grid (Arrangement)
    • UI\Controls\Group — Control Group (Arrangement)
    • UI\Controls\Box — Control Box (Arrangement)
    • UI\Draw\Pen — Draw Pen
    • UI\Draw\Path — Draw Path
    • UI\Draw\Matrix — Draw Matrix
    • UI\Draw\Color — Color Representation
    • UI\Draw\Stroke — Draw Stroke
    • UI\Draw\Brush — Brushes
    • UI\Draw\Brush\Gradient — Gradient Brushes
    • UI\Draw\Brush\LinearGradient — Linear Gradient
    • UI\Draw\Brush\RadialGradient — Radial Gradient
    • UI\Draw\Text\Layout — Represents Text Layout
    • UI\Draw\Text\Font — Represents a Font
    • UI\Draw\Text\Font\Descriptor — Font Descriptor
    • UI 함수 목록
    • UI\Draw\Text\Font\Weight — Font Weight Settings
    • UI\Draw\Text\Font\Italic — Italic Font Settings
    • UI\Draw\Text\Font\Stretch — Font Stretch Settings
    • UI\Draw\Line\Cap — Line Cap Settings
    • UI\Draw\Line\Join — Line Join Settings
    • UI\Key — Key Identifiers
    • UI\Exception\InvalidArgumentException — InvalidArgumentException
    • UI\Exception\RuntimeException — RuntimeException

User Contributed Notes

There are no user contributed notes for this page.


UI

  • 소개
  • 설치/설정
    • 요구 조건
    • 설치
  • UI\Point — Represents a position (x,y)
    • UI\Point::at — Size Coercion
    • UI\Point::__construct — Construct a new Point
    • UI\Point::getX — Retrieves X
    • UI\Point::getY — Retrieves Y
    • UI\Point::setX — Set X
    • UI\Point::setY — Set Y
  • UI\Size — Represents dimenstions (width, height)
    • UI\Size::__construct — Construct a new Size
    • UI\Size::getHeight — Retrieves Height
    • UI\Size::getWidth — Retrives Width
    • UI\Size::of — Point Coercion
    • UI\Size::setHeight — Set Height
    • UI\Size::setWidth — Set Width
  • UI\Window — Window
    • UI\Window::add — Add a Control
    • UI\Window::__construct — Construct a new Window
    • UI\Window::error — Show Error Box
    • UI\Window::getSize — Get Window Size
    • UI\Window::getTitle — Get Title
    • UI\Window::hasBorders — Border Detection
    • UI\Window::hasMargin — Margin Detection
    • UI\Window::isFullScreen — Full Screen Detection
    • UI\Window::msg — Show Message Box
    • UI\Window::onClosing — Closing Callback
    • UI\Window::open — Open Dialog
    • UI\Window::save — Save Dialog
    • UI\Window::setBorders — Border Use
    • UI\Window::setFullScreen — Full Screen Use
    • UI\Window::setMargin — Margin Use
    • UI\Window::setSize — Set Size
    • UI\Window::setTitle — Window Title
  • UI\Control — Control
    • UI\Control::destroy — Destroy Control
    • UI\Control::disable — Disable Control
    • UI\Control::enable — Enable Control
    • UI\Control::getParent — Get Parent Control
    • UI\Control::getTopLevel — Get Top Level
    • UI\Control::hide — Hide Control
    • UI\Control::isEnabled — Determine if Control is enabled
    • UI\Control::isVisible — Determine if Control is visible
    • UI\Control::setParent — Set Parent Control
    • UI\Control::show — Control Show
  • UI\Menu — Menu
    • UI\Menu::append — Append Menu Item
    • UI\Menu::appendAbout — Append About Menu Item
    • UI\Menu::appendCheck — Append Checkable Menu Item
    • UI\Menu::appendPreferences — Append Preferences Menu Item
    • UI\Menu::appendQuit — Append Quit Menu Item
    • UI\Menu::appendSeparator — Append Menu Item Separator
    • UI\Menu::__construct — Construct a new Menu
  • UI\MenuItem — Menu Item
    • UI\MenuItem::disable — Disable Menu Item
    • UI\MenuItem::enable — Enable Menu Item
    • UI\MenuItem::isChecked — Detect Checked
    • UI\MenuItem::onClick — On Click Callback
    • UI\MenuItem::setChecked — Set Checked
  • UI\Area — Area
    • UI\Area::onDraw — Draw Callback
    • UI\Area::onKey — Key Callback
    • UI\Area::onMouse — Mouse Callback
    • UI\Area::redraw — Redraw Area
    • UI\Area::scrollTo — Area Scroll
    • UI\Area::setSize — Set Size
  • UI\Executor — Execution Scheduler
    • UI\Executor::__construct — Construct a new Executor
    • UI\Executor::kill — Stop Executor
    • UI\Executor::onExecute — Execution Callback
    • UI\Executor::setInterval — Interval Manipulation
  • UI\Controls\Tab — Tab Control
    • UI\Controls\Tab::append — Append Page
    • UI\Controls\Tab::delete — Delete Page
    • UI\Controls\Tab::hasMargin — Margin Detection
    • UI\Controls\Tab::insertAt — Insert Page
    • UI\Controls\Tab::pages — Page Count
    • UI\Controls\Tab::setMargin — Set Margin
  • UI\Controls\Check — Check Control
    • UI\Controls\Check::__construct — Construct a new Check
    • UI\Controls\Check::getText — Get Text
    • UI\Controls\Check::isChecked — Checked Detection
    • UI\Controls\Check::onToggle — Toggle Callback
    • UI\Controls\Check::setChecked — Set Checked
    • UI\Controls\Check::setText — Set Text
  • UI\Controls\Button — Button Control
    • UI\Controls\Button::__construct — Construct a new Button
    • UI\Controls\Button::getText — Get Text
    • UI\Controls\Button::onClick — Click Handler
    • UI\Controls\Button::setText — Set Text
  • UI\Controls\ColorButton — ColorButton Control
    • UI\Controls\ColorButton::getColor — Get Color
    • UI\Controls\ColorButton::onChange — Change Handler
    • UI\Controls\ColorButton::setColor — Set Color
  • UI\Controls\Label — Label Control
    • UI\Controls\Label::__construct — Construct a new Label
    • UI\Controls\Label::getText — Get Text
    • UI\Controls\Label::setText — Set Text
  • UI\Controls\Entry — Entry Control
    • UI\Controls\Entry::__construct — Construct a new Entry
    • UI\Controls\Entry::getText — Get Text
    • UI\Controls\Entry::isReadOnly — Detect Read Only
    • UI\Controls\Entry::onChange — Change Handler
    • UI\Controls\Entry::setReadOnly — Set Read Only
    • UI\Controls\Entry::setText — Set Text
  • UI\Controls\MultilineEntry — MultilineEntry Control
    • UI\Controls\MultilineEntry::append — Append Text
    • UI\Controls\MultilineEntry::__construct — Construct a new Multiline Entry
    • UI\Controls\MultilineEntry::getText — Get Text
    • UI\Controls\MultilineEntry::isReadOnly — Read Only Detection
    • UI\Controls\MultilineEntry::onChange — Change Handler
    • UI\Controls\MultilineEntry::setReadOnly — Set Read Only
    • UI\Controls\MultilineEntry::setText — Set Text
  • UI\Controls\Spin — Spin Control
    • UI\Controls\Spin::__construct — Construct a new Spin
    • UI\Controls\Spin::getValue — Get Value
    • UI\Controls\Spin::onChange — Change Handler
    • UI\Controls\Spin::setValue — Set Value
  • UI\Controls\Slider — Slider Control
    • UI\Controls\Slider::__construct — Construct a new Slider
    • UI\Controls\Slider::getValue — Get Value
    • UI\Controls\Slider::onChange — Change Handler
    • UI\Controls\Slider::setValue — Set Value
  • UI\Controls\Progress — Progress Control
    • UI\Controls\Progress::getValue — Get Value
    • UI\Controls\Progress::setValue — Set Value
  • UI\Controls\Separator — Control Separator
    • UI\Controls\Separator::__construct — Construct a new Separator
  • UI\Controls\Combo — Combo Control
    • UI\Controls\Combo::append — Append Option
    • UI\Controls\Combo::getSelected — Get Selected Option
    • UI\Controls\Combo::onSelected — Selected Handler
    • UI\Controls\Combo::setSelected — Set Selected Option
  • UI\Controls\EditableCombo — EdiableCombo Control
    • UI\Controls\EditableCombo::append — Append Option
    • UI\Controls\EditableCombo::getText — Get Text
    • UI\Controls\EditableCombo::onChange — Change Handler
    • UI\Controls\EditableCombo::setText — Set Text
  • UI\Controls\Radio — Radio Control
    • UI\Controls\Radio::append — Append Option
    • UI\Controls\Radio::getSelected — Get Selected Option
    • UI\Controls\Radio::onSelected — Selected Handler
    • UI\Controls\Radio::setSelected — Set Selected Option
  • UI\Controls\Picker — Picker Control
    • UI\Controls\Picker::__construct — Construct a new Picker
  • UI\Controls\Form — Control Form (Arrangement)
    • UI\Controls\Form::append — Append Control
    • UI\Controls\Form::delete — Delete Control
    • UI\Controls\Form::isPadded — Padding Detection
    • UI\Controls\Form::setPadded — Set Padding
  • UI\Controls\Grid — Control Grid (Arrangement)
    • UI\Controls\Grid::append — Append Control
    • UI\Controls\Grid::isPadded — Padding Detection
    • UI\Controls\Grid::setPadded — Set Padding
  • UI\Controls\Group — Control Group (Arrangement)
    • UI\Controls\Group::append — Append Control
    • UI\Controls\Group::__construct — Construct a new Group
    • UI\Controls\Group::getTitle — Get Title
    • UI\Controls\Group::hasMargin — Margin Detection
    • UI\Controls\Group::setMargin — Set Margin
    • UI\Controls\Group::setTitle — Set Title
  • UI\Controls\Box — Control Box (Arrangement)
    • UI\Controls\Box::append — Append Control
    • UI\Controls\Box::__construct — Construct a new Box
    • UI\Controls\Box::delete — Delete Control
    • UI\Controls\Box::getOrientation — Get Orientation
    • UI\Controls\Box::isPadded — Padding Detection
    • UI\Controls\Box::setPadded — Set Padding
  • UI\Draw\Pen — Draw Pen
    • UI\Draw\Pen::clip — Clip a Path
    • UI\Draw\Pen::fill — Fill a Path
    • UI\Draw\Pen::restore — Restore
    • UI\Draw\Pen::save — Save
    • UI\Draw\Pen::stroke — Stroke a Path
    • UI\Draw\Pen::transform — Matrix Transform
    • UI\Draw\Pen::write — Draw Text at Point
  • UI\Draw\Path — Draw Path
    • UI\Draw\Path::addRectangle — Draw a Rectangle
    • UI\Draw\Path::arcTo — Draw an Arc
    • UI\Draw\Path::bezierTo — Draw Bezier Curve
    • UI\Draw\Path::closeFigure — Close Figure
    • UI\Draw\Path::__construct — Construct a new Path
    • UI\Draw\Path::end — Finalize Path
    • UI\Draw\Path::lineTo — Draw a Line
    • UI\Draw\Path::newFigure — Draw Figure
    • UI\Draw\Path::newFigureWithArc — Draw Figure with Arc
  • UI\Draw\Matrix — Draw Matrix
    • UI\Draw\Matrix::invert — Invert Matrix
    • UI\Draw\Matrix::isInvertible — Invertible Detection
    • UI\Draw\Matrix::multiply — Multiply Matrix
    • UI\Draw\Matrix::rotate — Rotate Matrix
    • UI\Draw\Matrix::scale — Scale Matrix
    • UI\Draw\Matrix::skew — Skew Matrix
    • UI\Draw\Matrix::translate — Translate Matrix
  • UI\Draw\Color — Color Representation
    • UI\Draw\Color::__construct — Construct new Color
    • UI\Draw\Color::getChannel — Color Manipulation
    • UI\Draw\Color::setChannel — Color Manipulation
  • UI\Draw\Stroke — Draw Stroke
    • UI\Draw\Stroke::__construct — Construct a new Stroke
    • UI\Draw\Stroke::getCap — Get Line Cap
    • UI\Draw\Stroke::getJoin — Get Line Join
    • UI\Draw\Stroke::getMiterLimit — Get Miter Limit
    • UI\Draw\Stroke::getThickness — Get Thickness
    • UI\Draw\Stroke::setCap — Set Line Cap
    • UI\Draw\Stroke::setJoin — Set Line Join
    • UI\Draw\Stroke::setMiterLimit — Set Miter Limit
    • UI\Draw\Stroke::setThickness — Set Thickness
  • UI\Draw\Brush — Brushes
    • UI\Draw\Brush::__construct — Construct a new Brush
    • UI\Draw\Brush::getColor — Get Color
    • UI\Draw\Brush::setColor — Set Color
  • UI\Draw\Brush\Gradient — Gradient Brushes
    • UI\Draw\Brush\Gradient::addStop — Stop Manipulation
    • UI\Draw\Brush\Gradient::delStop — Stop Manipulation
    • UI\Draw\Brush\Gradient::setStop — Stop Manipulation
  • UI\Draw\Brush\LinearGradient — Linear Gradient
    • UI\Draw\Brush\LinearGradient::__construct — Construct a Linear Gradient
  • UI\Draw\Brush\RadialGradient — Radial Gradient
    • UI\Draw\Brush\RadialGradient::__construct — Construct a new Radial Gradient
  • UI\Draw\Text\Layout — Represents Text Layout
    • UI\Draw\Text\Layout::__construct — Construct a new Text Layout
    • UI\Draw\Text\Layout::setColor — Set Color
    • UI\Draw\Text\Layout::setWidth — Set Width
  • UI\Draw\Text\Font — Represents a Font
    • UI\Draw\Text\Font::__construct — Construct a new Font
    • UI\Draw\Text\Font::getAscent — Font Metrics
    • UI\Draw\Text\Font::getDescent — Font Metrics
    • UI\Draw\Text\Font::getLeading — Font Metrics
    • UI\Draw\Text\Font::getUnderlinePosition — Font Metrics
    • UI\Draw\Text\Font::getUnderlineThickness — Font Metrics
  • UI\Draw\Text\Font\Descriptor — Font Descriptor
    • UI\Draw\Text\Font\Descriptor::__construct — Construct a new Font Descriptor
    • UI\Draw\Text\Font\Descriptor::getFamily — Get Font Family
    • UI\Draw\Text\Font\Descriptor::getItalic — Style Detection
    • UI\Draw\Text\Font\Descriptor::getSize — Size Detection
    • UI\Draw\Text\Font\Descriptor::getStretch — Style Detection
    • UI\Draw\Text\Font\Descriptor::getWeight — Weight Detection
  • UI 함수 목록
    • UI\Draw\Text\Font\fontFamilies — Retrieve Font Families
    • UI\quit — Quit UI Loop
    • UI\run — Enter UI Loop
  • UI\Draw\Text\Font\Weight — Font Weight Settings
  • UI\Draw\Text\Font\Italic — Italic Font Settings
  • UI\Draw\Text\Font\Stretch — Font Stretch Settings
  • UI\Draw\Line\Cap — Line Cap Settings
  • UI\Draw\Line\Join — Line Join Settings
  • UI\Key — Key Identifiers
  • UI\Exception\InvalidArgumentException — InvalidArgumentException
  • UI\Exception\RuntimeException — RuntimeException

User Contributed Notes

There are no user contributed notes for this page.


소개

This extension wraps libui to provide an OO API for the cross platform development of native look-and-feel user interfaces .

User Contributed Notes

There are no user contributed notes for this page.


요구 조건

Installation requires the libui library, and PHP 7 or newer.

This extension requires the » libui library version 0.1.0 or higher to be installed. See the libui documentation for a list of prerequisites.

User Contributed Notes

There are no user contributed notes for this page.


설치

이 » PECL 확장은 PHP 배포판에서 제공하지 않습니다.

PECL 확장 설치에 관한 정보는 매뉴얼의 PECL 확장 설치에 있습니다. 새 릴리즈, 내려받기, 소스파일, 개발자 정보, CHANGELOG 등의 추가 정보는 여기에 있습니다: » http://pecl.php.net/package/ui.

Windows binaries (DLL files) for this PECL extension are available from the PECL website.

User Contributed Notes

There are no user contributed notes for this page.


Represents a position (x,y)

(PHP 7, UI 0.9.9)

소개

Points are used throughout UI to represent co-ordinates on a screen, control, or area.

클래스 개요

final UI\Point {
/* 프로퍼티 */
public $x ;
public $y ;
/* Constructor */
public __construct ( float $x , float $y )
/* 메소드 */
public static UI\Point at ( float $point )
public static UI\Point at ( UI\Size $size )
public float getX ( void )
public float getY ( void )
public setX ( float $point )
public setY ( float $point )
}

프로퍼티

x

Holds the X co-ordinate, can be read/written directly

y

Holds the Y co-ordinate, can be read/written directly

Table of Contents

  • UI\Point::at — Size Coercion
  • UI\Point::__construct — Construct a new Point
  • UI\Point::getX — Retrieves X
  • UI\Point::getY — Retrieves Y
  • UI\Point::setX — Set X
  • UI\Point::setY — Set Y

User Contributed Notes

There are no user contributed notes for this page.


Size Coercion

  • ≪ UI\Point
  • UI\Point::__construct ≫
  • PHP Manual
  • UI\Point
  • Size Coercion

UI\Point::at

(PHP 7, UI 1.0.2)

UI\Point::at — Size Coercion

설명

public static UI\Point UI\Point::at ( float $point )
public static UI\Point UI\Point::at ( UI\Size $size )

Shall return a UI\Point object where x and y are equal to those supplied, either in float or UI\Size form

인수

point

The value for x and y

size

The Size to convert

반환값

The resulting Point

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Point

  • ≪ UI\Point::at
  • UI\Point::getX ≫
  • PHP Manual
  • UI\Point
  • Construct a new Point

UI\Point::__construct

(PHP 7, UI 0.9.9)

UI\Point::__construct — Construct a new Point

설명

public UI\Point::__construct ( float $x , float $y )

Construct a new Point using new co-ordinates

인수

x

The new X co-ordinate

y

The new Y co-ordinate

User Contributed Notes

There are no user contributed notes for this page.


Retrieves X

  • ≪ UI\Point::__construct
  • UI\Point::getY ≫
  • PHP Manual
  • UI\Point
  • Retrieves X

UI\Point::getX

(PHP 7, UI 0.9.9)

UI\Point::getX — Retrieves X

설명

public float UI\Point::getX ( void )

Retrieves the X co-ordinate

인수

이 함수는 인수가 없습니다.

반환값

The current X co-ordinate

User Contributed Notes

There are no user contributed notes for this page.


Retrieves Y

  • ≪ UI\Point::getX
  • UI\Point::setX ≫
  • PHP Manual
  • UI\Point
  • Retrieves Y

UI\Point::getY

(PHP 7, UI 0.9.9)

UI\Point::getY — Retrieves Y

설명

public float UI\Point::getY ( void )

Retrieves the Y co-ordinate

인수

이 함수는 인수가 없습니다.

반환값

The current Y co-ordinate

User Contributed Notes

There are no user contributed notes for this page.


Set X

  • ≪ UI\Point::getY
  • UI\Point::setY ≫
  • PHP Manual
  • UI\Point
  • Set X

UI\Point::setX

(PHP 7, UI 0.9.9)

UI\Point::setX — Set X

설명

public UI\Point::setX ( float $point )

Set the X co-ordinate

인수

point

The new X co-ordinate

반환값

User Contributed Notes

There are no user contributed notes for this page.


Set Y

  • ≪ UI\Point::setX
  • UI\Size ≫
  • PHP Manual
  • UI\Point
  • Set Y

UI\Point::setY

(PHP 7, UI 0.9.9)

UI\Point::setY — Set Y

설명

public UI\Point::setY ( float $point )

Set the Y co-ordinate

인수

point

The new Y co-ordinate

반환값

User Contributed Notes

There are no user contributed notes for this page.


Represents dimenstions (width, height)

(PHP 7, UI 0.9.9)

소개

Sizes are used throughout UI to represent the size of a screen, control, or area.

클래스 개요

final UI\Size {
/* 프로퍼티 */
public $width ;
public $height ;
/* Constructor */
public __construct ( float $width , float $height )
/* 메소드 */
public float getHeight ( void )
public float getWidth ( void )
public static UI\Size of ( float $size )
public static UI\Size of ( UI\Point $point )
public setHeight ( float $size )
public setWidth ( float $size )
}

프로퍼티

width

Holds the width, can be read/written directly

height

Holds the height, can be read/written directly

Table of Contents

  • UI\Size::__construct — Construct a new Size
  • UI\Size::getHeight — Retrieves Height
  • UI\Size::getWidth — Retrives Width
  • UI\Size::of — Point Coercion
  • UI\Size::setHeight — Set Height
  • UI\Size::setWidth — Set Width

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Size

  • ≪ UI\Size
  • UI\Size::getHeight ≫
  • PHP Manual
  • UI\Size
  • Construct a new Size

UI\Size::__construct

(PHP 7, UI 0.9.9)

UI\Size::__construct — Construct a new Size

설명

public UI\Size::__construct ( float $width , float $height )

Construct a new Size using new width and height

인수

width

The new width

height

The new height

User Contributed Notes

There are no user contributed notes for this page.


Retrieves Height

  • ≪ UI\Size::__construct
  • UI\Size::getWidth ≫
  • PHP Manual
  • UI\Size
  • Retrieves Height

UI\Size::getHeight

(PHP 7, UI 0.9.9)

UI\Size::getHeight — Retrieves Height

설명

public float UI\Size::getHeight ( void )

Retrieves the Height

인수

이 함수는 인수가 없습니다.

반환값

The current Height

User Contributed Notes

There are no user contributed notes for this page.


Retrives Width

  • ≪ UI\Size::getHeight
  • UI\Size::of ≫
  • PHP Manual
  • UI\Size
  • Retrives Width

UI\Size::getWidth

(PHP 7, UI 0.9.9)

UI\Size::getWidth — Retrives Width

설명

public float UI\Size::getWidth ( void )

Retrieves the Width

인수

이 함수는 인수가 없습니다.

반환값

The current Width

User Contributed Notes

There are no user contributed notes for this page.


Point Coercion

  • ≪ UI\Size::getWidth
  • UI\Size::setHeight ≫
  • PHP Manual
  • UI\Size
  • Point Coercion

UI\Size::of

(PHP 7, UI 1.0.2)

UI\Size::of — Point Coercion

설명

public static UI\Size UI\Size::of ( float $size )
public static UI\Size UI\Size::of ( UI\Point $point )

Shall return a UI\Size object where width and height are equal to those supplied, either in float or UI\Point form

인수

size

The value for width and height

point

The Point to convert

반환값

The resulting Size

User Contributed Notes

There are no user contributed notes for this page.


Set Height

  • ≪ UI\Size::of
  • UI\Size::setWidth ≫
  • PHP Manual
  • UI\Size
  • Set Height

UI\Size::setHeight

(PHP 7, UI 0.9.9)

UI\Size::setHeight — Set Height

설명

public UI\Size::setHeight ( float $size )

Set new Height

인수

size

The new Height

반환값

User Contributed Notes

There are no user contributed notes for this page.


Set Width

  • ≪ UI\Size::setHeight
  • UI\Window ≫
  • PHP Manual
  • UI\Size
  • Set Width

UI\Size::setWidth

(PHP 7, UI 0.9.9)

UI\Size::setWidth — Set Width

설명

public UI\Size::setWidth ( float $size )

Set new Width

인수

size

The new Width

반환값

User Contributed Notes

There are no user contributed notes for this page.


Window

(PHP 7, UI 0.9.9)

소개

Represents a UI Window

클래스 개요

UI\Window extends UI\Control {
/* 프로퍼티 */
protected $controls ;
/* Constructor */
public __construct ( string $title , Size $size [, boolean $menu = false ] )
/* 메소드 */
public add ( UI\Control $control )
public error ( string $title , string $msg )
public UI\Size getSize ( void )
public string getTitle ( void )
public bool hasBorders ( void )
public bool hasMargin ( void )
public bool isFullScreen ( void )
public msg ( string $title , string $msg )
protected int onClosing ( void )
public string open ( void )
public string save ( void )
public setBorders ( boolean $borders )
public setFullScreen ( boolean $full )
public setMargin ( boolean $margin )
public setSize ( UI\Size $size )
public setTitle ( string $title )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

프로퍼티

controls

Contains controls, should not be manipulated directly

Table of Contents

  • UI\Window::add — Add a Control
  • UI\Window::__construct — Construct a new Window
  • UI\Window::error — Show Error Box
  • UI\Window::getSize — Get Window Size
  • UI\Window::getTitle — Get Title
  • UI\Window::hasBorders — Border Detection
  • UI\Window::hasMargin — Margin Detection
  • UI\Window::isFullScreen — Full Screen Detection
  • UI\Window::msg — Show Message Box
  • UI\Window::onClosing — Closing Callback
  • UI\Window::open — Open Dialog
  • UI\Window::save — Save Dialog
  • UI\Window::setBorders — Border Use
  • UI\Window::setFullScreen — Full Screen Use
  • UI\Window::setMargin — Margin Use
  • UI\Window::setSize — Set Size
  • UI\Window::setTitle — Window Title

User Contributed Notes

There are no user contributed notes for this page.


Add a Control

  • ≪ UI\Window
  • UI\Window::__construct ≫
  • PHP Manual
  • UI\Window
  • Add a Control

UI\Window::add

(PHP 7, UI 0.9.9)

UI\Window::add — Add a Control

설명

public UI\Window::add ( UI\Control $control )

Shall add a Control to this Window

인수

control

The Control to add

반환값

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Window

  • ≪ UI\Window::add
  • UI\Window::error ≫
  • PHP Manual
  • UI\Window
  • Construct a new Window

UI\Window::__construct

(PHP 7, UI 0.9.9)

UI\Window::__construct — Construct a new Window

설명

public UI\Window::__construct ( string $title , Size $size [, boolean $menu = false ] )

Shall construct a new Window

인수

title

The title of the Window

size

The size of the Window

menu

Menu switch

User Contributed Notes

There are no user contributed notes for this page.


Show Error Box

  • ≪ UI\Window::__construct
  • UI\Window::getSize ≫
  • PHP Manual
  • UI\Window
  • Show Error Box

UI\Window::error

(PHP 7, UI 0.9.9)

UI\Window::error — Show Error Box

설명

public UI\Window::error ( string $title , string $msg )

Shall show an error box

인수

title

The title of the error box

msg

The message for the error box

User Contributed Notes

There are no user contributed notes for this page.


Get Window Size

  • ≪ UI\Window::error
  • UI\Window::getTitle ≫
  • PHP Manual
  • UI\Window
  • Get Window Size

UI\Window::getSize

(PHP 7, UI 0.9.9)

UI\Window::getSize — Get Window Size

설명

public UI\Size UI\Window::getSize ( void )

Shall return the size of this Window

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Get Title

  • ≪ UI\Window::getSize
  • UI\Window::hasBorders ≫
  • PHP Manual
  • UI\Window
  • Get Title

UI\Window::getTitle

(PHP 7, UI 0.9.9)

UI\Window::getTitle — Get Title

설명

public string UI\Window::getTitle ( void )

Shall retrieve the title of this Window

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Border Detection

  • ≪ UI\Window::getTitle
  • UI\Window::hasMargin ≫
  • PHP Manual
  • UI\Window
  • Border Detection

UI\Window::hasBorders

(PHP 7, UI 0.9.9)

UI\Window::hasBorders — Border Detection

설명

public bool UI\Window::hasBorders ( void )

Shall detect if borders are used on this Window

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Margin Detection

  • ≪ UI\Window::hasBorders
  • UI\Window::isFullScreen ≫
  • PHP Manual
  • UI\Window
  • Margin Detection

UI\Window::hasMargin

(PHP 7, UI 0.9.9)

UI\Window::hasMargin — Margin Detection

설명

public bool UI\Window::hasMargin ( void )

Shall detect if margins are used on this Window

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Full Screen Detection

  • ≪ UI\Window::hasMargin
  • UI\Window::msg ≫
  • PHP Manual
  • UI\Window
  • Full Screen Detection

UI\Window::isFullScreen

(PHP 7, UI 0.9.9)

UI\Window::isFullScreen — Full Screen Detection

설명

public bool UI\Window::isFullScreen ( void )

Shall detect if this Window us using the whole screen

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Show Message Box

  • ≪ UI\Window::isFullScreen
  • UI\Window::onClosing ≫
  • PHP Manual
  • UI\Window
  • Show Message Box

UI\Window::msg

(PHP 7, UI 0.9.9)

UI\Window::msg — Show Message Box

설명

public UI\Window::msg ( string $title , string $msg )

Shall show a message box

인수

title

The title of the message box

msg

The message

User Contributed Notes

There are no user contributed notes for this page.


Closing Callback

  • ≪ UI\Window::msg
  • UI\Window::open ≫
  • PHP Manual
  • UI\Window
  • Closing Callback

UI\Window::onClosing

(PHP 7, UI 0.9.9)

UI\Window::onClosing — Closing Callback

설명

protected int UI\Window::onClosing ( void )

Should gracefully destroy this Window

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Open Dialog

  • ≪ UI\Window::onClosing
  • UI\Window::save ≫
  • PHP Manual
  • UI\Window
  • Open Dialog

UI\Window::open

(PHP 7, UI 0.9.9)

UI\Window::open — Open Dialog

설명

public string UI\Window::open ( void )

Shall show an open file dialog

인수

이 함수는 인수가 없습니다.

반환값

Returns the name of the file selected for opening

User Contributed Notes

There are no user contributed notes for this page.


Save Dialog

  • ≪ UI\Window::open
  • UI\Window::setBorders ≫
  • PHP Manual
  • UI\Window
  • Save Dialog

UI\Window::save

(PHP 7, UI 0.9.9)

UI\Window::save — Save Dialog

설명

public string UI\Window::save ( void )

Shall show a save dialog

인수

이 함수는 인수가 없습니다.

반환값

Returns the file name selecting for saving

User Contributed Notes

There are no user contributed notes for this page.


Border Use

  • ≪ UI\Window::save
  • UI\Window::setFullScreen ≫
  • PHP Manual
  • UI\Window
  • Border Use

UI\Window::setBorders

(PHP 7, UI 0.9.9)

UI\Window::setBorders — Border Use

설명

public UI\Window::setBorders ( boolean $borders )

Shall enable or disable the use of borders on this Window

인수

borders

User Contributed Notes

There are no user contributed notes for this page.


Full Screen Use

  • ≪ UI\Window::setBorders
  • UI\Window::setMargin ≫
  • PHP Manual
  • UI\Window
  • Full Screen Use

UI\Window::setFullScreen

(PHP 7, UI 0.9.9)

UI\Window::setFullScreen — Full Screen Use

설명

public UI\Window::setFullScreen ( boolean $full )

Shall enable or disable the use of the full screen for this Window

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

full

User Contributed Notes

There are no user contributed notes for this page.


Margin Use

  • ≪ UI\Window::setFullScreen
  • UI\Window::setSize ≫
  • PHP Manual
  • UI\Window
  • Margin Use

UI\Window::setMargin

(PHP 7, UI 0.9.9)

UI\Window::setMargin — Margin Use

설명

public UI\Window::setMargin ( boolean $margin )

Shall enable or disable the use of margins for this Window

인수

margin

User Contributed Notes

There are no user contributed notes for this page.


Set Size

  • ≪ UI\Window::setMargin
  • UI\Window::setTitle ≫
  • PHP Manual
  • UI\Window
  • Set Size

UI\Window::setSize

(PHP 7, UI 0.9.9)

UI\Window::setSize — Set Size

설명

public UI\Window::setSize ( UI\Size $size )

Shall set the size of this Window

인수

size

반환값

User Contributed Notes

There are no user contributed notes for this page.


Window Title

  • ≪ UI\Window::setSize
  • UI\Control ≫
  • PHP Manual
  • UI\Window
  • Window Title

UI\Window::setTitle

(PHP 7, UI 0.9.9)

UI\Window::setTitle — Window Title

설명

public UI\Window::setTitle ( string $title )

Shall set the title for this Window

인수

title

The new title

User Contributed Notes

There are no user contributed notes for this page.


Control

(PHP 7, UI 0.9.9)

소개

This is the closed base class for all UI Controls.

클래스 개요

final UI\Control {
/* 메소드 */
public destroy ( void )
public disable ( void )
public enable ( void )
public UI\Control getParent ( void )
public int getTopLevel ( void )
public hide ( void )
public bool isEnabled ( void )
public bool isVisible ( void )
public setParent ( UI\Control $parent )
public show ( void )
}

Table of Contents

  • UI\Control::destroy — Destroy Control
  • UI\Control::disable — Disable Control
  • UI\Control::enable — Enable Control
  • UI\Control::getParent — Get Parent Control
  • UI\Control::getTopLevel — Get Top Level
  • UI\Control::hide — Hide Control
  • UI\Control::isEnabled — Determine if Control is enabled
  • UI\Control::isVisible — Determine if Control is visible
  • UI\Control::setParent — Set Parent Control
  • UI\Control::show — Control Show

User Contributed Notes

There are no user contributed notes for this page.


Destroy Control

  • ≪ UI\Control
  • UI\Control::disable ≫
  • PHP Manual
  • UI\Control
  • Destroy Control

UI\Control::destroy

(PHP 7, UI 0.9.9)

UI\Control::destroy — Destroy Control

설명

public UI\Control::destroy ( void )

Shall destroy this Control

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Disable Control

  • ≪ UI\Control::destroy
  • UI\Control::enable ≫
  • PHP Manual
  • UI\Control
  • Disable Control

UI\Control::disable

(PHP 7, UI 0.9.9)

UI\Control::disable — Disable Control

설명

public UI\Control::disable ( void )

Shall disable this Control

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Enable Control

  • ≪ UI\Control::disable
  • UI\Control::getParent ≫
  • PHP Manual
  • UI\Control
  • Enable Control

UI\Control::enable

(PHP 7, UI 0.9.9)

UI\Control::enable — Enable Control

설명

public UI\Control::enable ( void )

Shall enable this Control

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Get Parent Control

  • ≪ UI\Control::enable
  • UI\Control::getTopLevel ≫
  • PHP Manual
  • UI\Control
  • Get Parent Control

UI\Control::getParent

(PHP 7, UI 0.9.9)

UI\Control::getParent — Get Parent Control

설명

public UI\Control UI\Control::getParent ( void )

Shall return the parent Control

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Get Top Level

  • ≪ UI\Control::getParent
  • UI\Control::hide ≫
  • PHP Manual
  • UI\Control
  • Get Top Level

UI\Control::getTopLevel

(PHP 7, UI 0.9.9)

UI\Control::getTopLevel — Get Top Level

설명

public int UI\Control::getTopLevel ( void )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Hide Control

  • ≪ UI\Control::getTopLevel
  • UI\Control::isEnabled ≫
  • PHP Manual
  • UI\Control
  • Hide Control

UI\Control::hide

(PHP 7, UI 0.9.9)

UI\Control::hide — Hide Control

설명

public UI\Control::hide ( void )

Shall hide this Control

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Determine if Control is enabled

  • ≪ UI\Control::hide
  • UI\Control::isVisible ≫
  • PHP Manual
  • UI\Control
  • Determine if Control is enabled

UI\Control::isEnabled

(PHP 7, UI 0.9.9)

UI\Control::isEnabled — Determine if Control is enabled

설명

public bool UI\Control::isEnabled ( void )

Shall detect if this Control is enabled

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Determine if Control is visible

  • ≪ UI\Control::isEnabled
  • UI\Control::setParent ≫
  • PHP Manual
  • UI\Control
  • Determine if Control is visible

UI\Control::isVisible

(PHP 7, UI 0.9.9)

UI\Control::isVisible — Determine if Control is visible

설명

public bool UI\Control::isVisible ( void )

Shall detect if this Control is visible

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Set Parent Control

  • ≪ UI\Control::isVisible
  • UI\Control::show ≫
  • PHP Manual
  • UI\Control
  • Set Parent Control

UI\Control::setParent

(PHP 7, UI 0.9.9)

UI\Control::setParent — Set Parent Control

설명

public UI\Control::setParent ( UI\Control $parent )

Shall set the parent Control of this Control

인수

parent

The parent Control

반환값

User Contributed Notes

There are no user contributed notes for this page.


Control Show

  • ≪ UI\Control::setParent
  • UI\Menu ≫
  • PHP Manual
  • UI\Control
  • Control Show

UI\Control::show

(PHP 7, UI 0.9.9)

UI\Control::show — Control Show

설명

public UI\Control::show ( void )

Shall show this Control

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Menu

(PHP 7, UI 0.9.9)

소개

Menus must be constructed before the first Window, and can be shown on any Window

클래스 개요

UI\Menu {
/* Constructor */
public __construct ( string $name )
/* 메소드 */
public UI\MenuItem append ( string $name [, string $type = UI\MenuItem::class ] )
public UI\MenuItem appendAbout ([ string $type = UI\MenuItem::class ] )
public UI\MenuItem appendCheck ( string $name [, string $type = UI\MenuItem::class ] )
public UI\MenuItem appendPreferences ([ string $type = UI\MenuItem::class ] )
public UI\MenuItem appendQuit ([ string $type = UI\MenuItem::class ] )
public appendSeparator ( void )
}

Table of Contents

  • UI\Menu::append — Append Menu Item
  • UI\Menu::appendAbout — Append About Menu Item
  • UI\Menu::appendCheck — Append Checkable Menu Item
  • UI\Menu::appendPreferences — Append Preferences Menu Item
  • UI\Menu::appendQuit — Append Quit Menu Item
  • UI\Menu::appendSeparator — Append Menu Item Separator
  • UI\Menu::__construct — Construct a new Menu

User Contributed Notes

There are no user contributed notes for this page.


Append Menu Item

  • ≪ UI\Menu
  • UI\Menu::appendAbout ≫
  • PHP Manual
  • UI\Menu
  • Append Menu Item

UI\Menu::append

(PHP 7, UI 0.9.9)

UI\Menu::append — Append Menu Item

설명

public UI\MenuItem UI\Menu::append ( string $name [, string $type = UI\MenuItem::class ] )

Shall append a new Menu Item

인수

name

The name (text) for the new item

type

The type for the new item

반환값

A constructed object of the given type

User Contributed Notes

There are no user contributed notes for this page.


Append About Menu Item

  • ≪ UI\Menu::append
  • UI\Menu::appendCheck ≫
  • PHP Manual
  • UI\Menu
  • Append About Menu Item

UI\Menu::appendAbout

(PHP 7, UI 0.9.9)

UI\Menu::appendAbout — Append About Menu Item

설명

public UI\MenuItem UI\Menu::appendAbout ([ string $type = UI\MenuItem::class ] )

Shall append an About menu item

인수

type

The type for the new item

반환값

A constructed About menu item of the given type

User Contributed Notes

There are no user contributed notes for this page.


Append Checkable Menu Item

  • ≪ UI\Menu::appendAbout
  • UI\Menu::appendPreferences ≫
  • PHP Manual
  • UI\Menu
  • Append Checkable Menu Item

UI\Menu::appendCheck

(PHP 7, UI 0.9.9)

UI\Menu::appendCheck — Append Checkable Menu Item

설명

public UI\MenuItem UI\Menu::appendCheck ( string $name [, string $type = UI\MenuItem::class ] )

Shall append a checkable menu item

인수

name

The name (text) for the new item

type

The type for the new item

반환값

A constructed checkable menu item of the given type

User Contributed Notes

There are no user contributed notes for this page.


Append Preferences Menu Item

  • ≪ UI\Menu::appendCheck
  • UI\Menu::appendQuit ≫
  • PHP Manual
  • UI\Menu
  • Append Preferences Menu Item

UI\Menu::appendPreferences

(PHP 7, UI 0.9.9)

UI\Menu::appendPreferences — Append Preferences Menu Item

설명

public UI\MenuItem UI\Menu::appendPreferences ([ string $type = UI\MenuItem::class ] )

Shall append a Preferences menu item

인수

type

The type for the new item

반환값

A constructed Preferences menu item of the given type

User Contributed Notes

There are no user contributed notes for this page.


Append Quit Menu Item

  • ≪ UI\Menu::appendPreferences
  • UI\Menu::appendSeparator ≫
  • PHP Manual
  • UI\Menu
  • Append Quit Menu Item

UI\Menu::appendQuit

(PHP 7, UI 0.9.9)

UI\Menu::appendQuit — Append Quit Menu Item

설명

public UI\MenuItem UI\Menu::appendQuit ([ string $type = UI\MenuItem::class ] )

Shall append a Quit menu item

인수

type

The type for the new item

반환값

A constructed Quit menu item of the given type

User Contributed Notes

There are no user contributed notes for this page.


Append Menu Item Separator

  • ≪ UI\Menu::appendQuit
  • UI\Menu::__construct ≫
  • PHP Manual
  • UI\Menu
  • Append Menu Item Separator

UI\Menu::appendSeparator

(PHP 7, UI 0.9.9)

UI\Menu::appendSeparator — Append Menu Item Separator

설명

public UI\Menu::appendSeparator ( void )

Shall append a separator

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Menu

  • ≪ UI\Menu::appendSeparator
  • UI\MenuItem ≫
  • PHP Manual
  • UI\Menu
  • Construct a new Menu

UI\Menu::__construct

(PHP 7, UI 0.9.9)

UI\Menu::__construct — Construct a new Menu

설명

public UI\Menu::__construct ( string $name )

Shall construct a new Menu

인수

name

The name (text) for the menu

User Contributed Notes

There are no user contributed notes for this page.


Menu Item

(PHP 7, UI 0.9.9)

소개

Menu Items should only be created by the Menu

클래스 개요

UI\MenuItem {
/* 메소드 */
public disable ( void )
public enable ( void )
public bool isChecked ( void )
protected onClick ( void )
public setChecked ( boolean $checked )
}

Table of Contents

  • UI\MenuItem::disable — Disable Menu Item
  • UI\MenuItem::enable — Enable Menu Item
  • UI\MenuItem::isChecked — Detect Checked
  • UI\MenuItem::onClick — On Click Callback
  • UI\MenuItem::setChecked — Set Checked

User Contributed Notes

There are no user contributed notes for this page.


Disable Menu Item

  • ≪ UI\MenuItem
  • UI\MenuItem::enable ≫
  • PHP Manual
  • UI\MenuItem
  • Disable Menu Item

UI\MenuItem::disable

(PHP 7, UI 0.9.9)

UI\MenuItem::disable — Disable Menu Item

설명

public UI\MenuItem::disable ( void )

Shall disable this Menu Item

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Enable Menu Item

  • ≪ UI\MenuItem::disable
  • UI\MenuItem::isChecked ≫
  • PHP Manual
  • UI\MenuItem
  • Enable Menu Item

UI\MenuItem::enable

(PHP 7, UI 0.9.9)

UI\MenuItem::enable — Enable Menu Item

설명

public UI\MenuItem::enable ( void )

Shall enable this Menu Item

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Detect Checked

  • ≪ UI\MenuItem::enable
  • UI\MenuItem::onClick ≫
  • PHP Manual
  • UI\MenuItem
  • Detect Checked

UI\MenuItem::isChecked

(PHP 7, UI 0.9.9)

UI\MenuItem::isChecked — Detect Checked

설명

public bool UI\MenuItem::isChecked ( void )

Shall detect if this Menu Item is checked

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


On Click Callback

  • ≪ UI\MenuItem::isChecked
  • UI\MenuItem::setChecked ≫
  • PHP Manual
  • UI\MenuItem
  • On Click Callback

UI\MenuItem::onClick

(PHP 7, UI 0.9.9)

UI\MenuItem::onClick — On Click Callback

설명

protected UI\MenuItem::onClick ( void )

Shall be executed when this Menu Item is clicked

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Checked

  • ≪ UI\MenuItem::onClick
  • UI\Area ≫
  • PHP Manual
  • UI\MenuItem
  • Set Checked

UI\MenuItem::setChecked

(PHP 7, UI 0.9.9)

UI\MenuItem::setChecked — Set Checked

설명

public UI\MenuItem::setChecked ( boolean $checked )

Shall set the checked status of this Menu Item

인수

checked

The new status

User Contributed Notes

There are no user contributed notes for this page.


Area

(PHP 7, UI 0.9.9)

소개

An Area represents a canvas which can be used to draw, and respond to mouse and key events.

클래스 개요

UI\Area extends UI\Control {
/* Constants */
const integer Ctrl ;
const integer Alt ;
const integer Shift ;
const integer Super ;
const integer Down ;
const integer Up ;
/* 메소드 */
protected onDraw ( UI\Draw\Pen $pen , UI\Size $areaSize , UI\Point $clipPoint , UI\Size $clipSize )
protected onKey ( string $key , integer $ext , integer $flags )
protected onMouse ( UI\Point $areaPoint , UI\Size $areaSize , integer $flags )
public redraw ( void )
public scrollTo ( UI\Point $point , UI\Size $size )
public setSize ( UI\Size $size )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

예약 상수

UI\Area::Ctrl

Shall be set in the modifiers passed to key and mouse events when the CTRL key is active

UI\Area::Alt

Shall be set in the modifiers passed to key and mouse events when the ALT key is active

UI\Area::Shift

Shall be set in the modifiers passed to key and mouse events when the SHIFT key is active

UI\Area::Super

UI\Area::Down

Shall be set in the modifiers passed to key and mouse events

UI\Area::Up

Shall be set in the modifiers passed to key and mouse events

Table of Contents

  • UI\Area::onDraw — Draw Callback
  • UI\Area::onKey — Key Callback
  • UI\Area::onMouse — Mouse Callback
  • UI\Area::redraw — Redraw Area
  • UI\Area::scrollTo — Area Scroll
  • UI\Area::setSize — Set Size

User Contributed Notes

There are no user contributed notes for this page.


Draw Callback

  • ≪ UI\Area
  • UI\Area::onKey ≫
  • PHP Manual
  • UI\Area
  • Draw Callback

UI\Area::onDraw

(PHP 7, UI 0.9.9)

UI\Area::onDraw — Draw Callback

설명

protected UI\Area::onDraw ( UI\Draw\Pen $pen , UI\Size $areaSize , UI\Point $clipPoint , UI\Size $clipSize )

Shall be invoked when this Area requires redrawing

인수

pen

A Pen suitable for drawing in this Area

areaSize

The size of the Area

clipPoint

The clip point of the Area

clipSize

The clip size of the Area

User Contributed Notes

There are no user contributed notes for this page.


Key Callback

  • ≪ UI\Area::onDraw
  • UI\Area::onMouse ≫
  • PHP Manual
  • UI\Area
  • Key Callback

UI\Area::onKey

(PHP 7, UI 0.9.9)

UI\Area::onKey — Key Callback

설명

protected UI\Area::onKey ( string $key , integer $ext , integer $flags )

Shall be executed on key events

인수

key

The key pressed

ext

The extended key pressed

flags

Event modifiers

반환값

User Contributed Notes

There are no user contributed notes for this page.


Mouse Callback

  • ≪ UI\Area::onKey
  • UI\Area::redraw ≫
  • PHP Manual
  • UI\Area
  • Mouse Callback

UI\Area::onMouse

(PHP 7, UI 0.9.9)

UI\Area::onMouse — Mouse Callback

설명

protected UI\Area::onMouse ( UI\Point $areaPoint , UI\Size $areaSize , integer $flags )

Shall be executed on mouse events

인수

areaPoint

The co-ordinates of the event

areaSize

The size of the area of the event

flags

Event modifiers

User Contributed Notes

There are no user contributed notes for this page.


Redraw Area

  • ≪ UI\Area::onMouse
  • UI\Area::scrollTo ≫
  • PHP Manual
  • UI\Area
  • Redraw Area

UI\Area::redraw

(PHP 7, UI 0.9.9)

UI\Area::redraw — Redraw Area

설명

public UI\Area::redraw ( void )

Requests that this Area is redrawn

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Area Scroll

  • ≪ UI\Area::redraw
  • UI\Area::setSize ≫
  • PHP Manual
  • UI\Area
  • Area Scroll

UI\Area::scrollTo

(PHP 7, UI 0.9.9)

UI\Area::scrollTo — Area Scroll

설명

public UI\Area::scrollTo ( UI\Point $point , UI\Size $size )

Scroll this Area

인수

point

The point to scroll to

size

The size of the scroll pane

User Contributed Notes

There are no user contributed notes for this page.


Set Size

  • ≪ UI\Area::scrollTo
  • UI\Executor ≫
  • PHP Manual
  • UI\Area
  • Set Size

UI\Area::setSize

(PHP 7, UI 0.9.9)

UI\Area::setSize — Set Size

설명

public UI\Area::setSize ( UI\Size $size )

Sets the size of this Area

인수

size

The new size

User Contributed Notes

There are no user contributed notes for this page.


Execution Scheduler

(PHP 7, UI 2.0.0)

소개

This facility schedules repetitive execution of a callback, useful for animations and other such activities.

클래스 개요

abstract UI\Executor {
/* Constructor */
public __construct ( void )
public __construct ( integer $microseconds )
public __construct ( integer $seconds , integer $microseconds )
/* 메소드 */
public void kill ( void )
abstract protected void onExecute ( void )
public bool setInterval ( integer $microseconds )
public bool setInterval ( integer $seconds , integer $microseconds )
}

Table of Contents

  • UI\Executor::__construct — Construct a new Executor
  • UI\Executor::kill — Stop Executor
  • UI\Executor::onExecute — Execution Callback
  • UI\Executor::setInterval — Interval Manipulation

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Executor

  • ≪ UI\Executor
  • UI\Executor::kill ≫
  • PHP Manual
  • UI\Executor
  • Construct a new Executor

UI\Executor::__construct

(PHP 7, UI 2.0.0)

UI\Executor::__construct — Construct a new Executor

설명

public UI\Executor::__construct ( void )
public UI\Executor::__construct ( integer $microseconds )
public UI\Executor::__construct ( integer $seconds , integer $microseconds )

Shall construct an executor with the given interval, will not start executing until main loop is entered

인수

seconds

Seconds between executions

microseconds

Microseconds between executions

User Contributed Notes

There are no user contributed notes for this page.


Stop Executor

  • ≪ UI\Executor::__construct
  • UI\Executor::onExecute ≫
  • PHP Manual
  • UI\Executor
  • Stop Executor

UI\Executor::kill

(PHP 7, UI 2.0.0)

UI\Executor::kill — Stop Executor

설명

public void UI\Executor::kill ( void )

Shall stop an executor, the executor cannot be restarted

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Execution Callback

  • ≪ UI\Executor::kill
  • UI\Executor::setInterval ≫
  • PHP Manual
  • UI\Executor
  • Execution Callback

UI\Executor::onExecute

(PHP 7, UI 2.0.0)

UI\Executor::onExecute — Execution Callback

설명

abstract protected void UI\Executor::onExecute ( void )

Shall be repetitively queued for execution in the main thread

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Interval Manipulation

  • ≪ UI\Executor::onExecute
  • UI\Controls\Tab ≫
  • PHP Manual
  • UI\Executor
  • Interval Manipulation

UI\Executor::setInterval

(PHP 7, UI 2.0.0)

UI\Executor::setInterval — Interval Manipulation

설명

public bool UI\Executor::setInterval ( integer $microseconds )
public bool UI\Executor::setInterval ( integer $seconds , integer $microseconds )

Shall set the new interval. An interval of 0 will pause the executor until a new interval has been set

인수

seconds

microseconds

반환값

Indication of success

User Contributed Notes

There are no user contributed notes for this page.


Tab Control

(PHP 7, UI 0.9.9)

소개

A Tab can contain many pages of Controls, each with a title, each selectable by the user.

클래스 개요

UI\Controls\Tab extends UI\Control {
/* 프로퍼티 */
protected $controls ;
/* 메소드 */
public integer append ( string $name , UI\Control $control )
public boolean delete ( integer $index )
public bool hasMargin ( integer $page )
public insertAt ( string $name , integer $page , UI\Control $control )
public integer pages ( void )
public setMargin ( integer $page , boolean $margin )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

프로퍼티

controls

Contains controls, should not be manipulated directly

Table of Contents

  • UI\Controls\Tab::append — Append Page
  • UI\Controls\Tab::delete — Delete Page
  • UI\Controls\Tab::hasMargin — Margin Detection
  • UI\Controls\Tab::insertAt — Insert Page
  • UI\Controls\Tab::pages — Page Count
  • UI\Controls\Tab::setMargin — Set Margin

User Contributed Notes

There are no user contributed notes for this page.


Append Page

  • ≪ UI\Controls\Tab
  • UI\Controls\Tab::delete ≫
  • PHP Manual
  • UI\Controls\Tab
  • Append Page

UI\Controls\Tab::append

(PHP 7, UI 0.9.9)

UI\Controls\Tab::append — Append Page

설명

public integer UI\Controls\Tab::append ( string $name , UI\Control $control )

Append a new page to this Tab

인수

name

The name for the new page

control

The control for the new page

반환값

Shall return the index of the appended control, may be 0

User Contributed Notes

There are no user contributed notes for this page.


Delete Page

  • ≪ UI\Controls\Tab::append
  • UI\Controls\Tab::hasMargin ≫
  • PHP Manual
  • UI\Controls\Tab
  • Delete Page

UI\Controls\Tab::delete

(PHP 7, UI 0.9.9)

UI\Controls\Tab::delete — Delete Page

설명

public boolean UI\Controls\Tab::delete ( integer $index )

Shall remove the selected page from this Tab

인수

index

The index of the page to remove

반환값

Indication of success

User Contributed Notes

There are no user contributed notes for this page.


Margin Detection

  • ≪ UI\Controls\Tab::delete
  • UI\Controls\Tab::insertAt ≫
  • PHP Manual
  • UI\Controls\Tab
  • Margin Detection

UI\Controls\Tab::hasMargin

(PHP 7, UI 0.9.9)

UI\Controls\Tab::hasMargin — Margin Detection

설명

public bool UI\Controls\Tab::hasMargin ( integer $page )

Shall detect if the given page has a margin.

인수

page

The index of the page

반환값

User Contributed Notes

There are no user contributed notes for this page.


Insert Page

  • ≪ UI\Controls\Tab::hasMargin
  • UI\Controls\Tab::pages ≫
  • PHP Manual
  • UI\Controls\Tab
  • Insert Page

UI\Controls\Tab::insertAt

(PHP 7, UI 0.9.9)

UI\Controls\Tab::insertAt — Insert Page

설명

public UI\Controls\Tab::insertAt ( string $name , integer $page , UI\Control $control )

Shall insert a new page into this Tab

인수

name

The name for the new page

page

The index to perform the insertion before

control

The control for the new page

User Contributed Notes

There are no user contributed notes for this page.


Page Count

  • ≪ UI\Controls\Tab::insertAt
  • UI\Controls\Tab::setMargin ≫
  • PHP Manual
  • UI\Controls\Tab
  • Page Count

UI\Controls\Tab::pages

(PHP 7, UI 0.9.9)

UI\Controls\Tab::pages — Page Count

설명

public integer UI\Controls\Tab::pages ( void )

Shall return the number of pages in this Tab

인수

이 함수는 인수가 없습니다.

반환값

The number of pages in this Tab

User Contributed Notes

There are no user contributed notes for this page.


Set Margin

  • ≪ UI\Controls\Tab::pages
  • UI\Controls\Check ≫
  • PHP Manual
  • UI\Controls\Tab
  • Set Margin

UI\Controls\Tab::setMargin

(PHP 7, UI 0.9.9)

UI\Controls\Tab::setMargin — Set Margin

설명

public UI\Controls\Tab::setMargin ( integer $page , boolean $margin )

Shall enable or disable margins on the selected page

인수

page

The page to select

margin

Margin switch

User Contributed Notes

There are no user contributed notes for this page.


Check Control

(PHP 7, UI 0.9.9)

소개

A Check is a labelled checkable box

클래스 개요

UI\Controls\Check extends UI\Control {
/* Constructor */
public __construct ( string $text )
/* 메소드 */
public string getText ( void )
public bool isChecked ( void )
protected onToggle ( void )
public setChecked ( boolean $checked )
public setText ( string $text )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

Table of Contents

  • UI\Controls\Check::__construct — Construct a new Check
  • UI\Controls\Check::getText — Get Text
  • UI\Controls\Check::isChecked — Checked Detection
  • UI\Controls\Check::onToggle — Toggle Callback
  • UI\Controls\Check::setChecked — Set Checked
  • UI\Controls\Check::setText — Set Text

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Check

  • ≪ UI\Controls\Check
  • UI\Controls\Check::getText ≫
  • PHP Manual
  • UI\Controls\Check
  • Construct a new Check

UI\Controls\Check::__construct

(PHP 7, UI 0.9.9)

UI\Controls\Check::__construct — Construct a new Check

설명

public UI\Controls\Check::__construct ( string $text )

Shall construct a new Check

인수

text

The text (label) for the Check box

User Contributed Notes

There are no user contributed notes for this page.


Get Text

  • ≪ UI\Controls\Check::__construct
  • UI\Controls\Check::isChecked ≫
  • PHP Manual
  • UI\Controls\Check
  • Get Text

UI\Controls\Check::getText

(PHP 7, UI 0.9.9)

UI\Controls\Check::getText — Get Text

설명

public string UI\Controls\Check::getText ( void )

Shall return the text (label) for this Check

인수

이 함수는 인수가 없습니다.

반환값

The current text (label)

User Contributed Notes

There are no user contributed notes for this page.


Checked Detection

  • ≪ UI\Controls\Check::getText
  • UI\Controls\Check::onToggle ≫
  • PHP Manual
  • UI\Controls\Check
  • Checked Detection

UI\Controls\Check::isChecked

(PHP 7, UI 0.9.9)

UI\Controls\Check::isChecked — Checked Detection

설명

public bool UI\Controls\Check::isChecked ( void )

Shall detect the status of this Check

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Toggle Callback

  • ≪ UI\Controls\Check::isChecked
  • UI\Controls\Check::setChecked ≫
  • PHP Manual
  • UI\Controls\Check
  • Toggle Callback

UI\Controls\Check::onToggle

(PHP 7, UI 0.9.9)

UI\Controls\Check::onToggle — Toggle Callback

설명

protected UI\Controls\Check::onToggle ( void )

Shall be executed when the status of this Check is changed

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Checked

  • ≪ UI\Controls\Check::onToggle
  • UI\Controls\Check::setText ≫
  • PHP Manual
  • UI\Controls\Check
  • Set Checked

UI\Controls\Check::setChecked

(PHP 7, UI 0.9.9)

UI\Controls\Check::setChecked — Set Checked

설명

public UI\Controls\Check::setChecked ( boolean $checked )

Shall change the status of this Check

인수

checked

The new status

User Contributed Notes

There are no user contributed notes for this page.


Set Text

  • ≪ UI\Controls\Check::setChecked
  • UI\Controls\Button ≫
  • PHP Manual
  • UI\Controls\Check
  • Set Text

UI\Controls\Check::setText

(PHP 7, UI 0.9.9)

UI\Controls\Check::setText — Set Text

설명

public UI\Controls\Check::setText ( string $text )

Shall set the text (label) for this Check

인수

text

The new text (label)

User Contributed Notes

There are no user contributed notes for this page.


Button Control

(PHP 7, UI 0.9.9)

소개

Represents a labelled clickable button

클래스 개요

UI\Controls\Button extends UI\Control {
/* Constructor */
public __construct ( string $text )
/* 메소드 */
public string getText ( void )
protected onClick ( void )
public setText ( string $text )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

Table of Contents

  • UI\Controls\Button::__construct — Construct a new Button
  • UI\Controls\Button::getText — Get Text
  • UI\Controls\Button::onClick — Click Handler
  • UI\Controls\Button::setText — Set Text

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Button

  • ≪ UI\Controls\Button
  • UI\Controls\Button::getText ≫
  • PHP Manual
  • UI\Controls\Button
  • Construct a new Button

UI\Controls\Button::__construct

(PHP 7, UI 0.9.9)

UI\Controls\Button::__construct — Construct a new Button

설명

public UI\Controls\Button::__construct ( string $text )

Shall construct a new Button

인수

text

The text (label) for this Button

User Contributed Notes

There are no user contributed notes for this page.


Get Text

  • ≪ UI\Controls\Button::__construct
  • UI\Controls\Button::onClick ≫
  • PHP Manual
  • UI\Controls\Button
  • Get Text

UI\Controls\Button::getText

(PHP 7, UI 0.9.9)

UI\Controls\Button::getText — Get Text

설명

public string UI\Controls\Button::getText ( void )

Shall retrieve the text (label) for this Button

인수

이 함수는 인수가 없습니다.

반환값

The current text (label)

User Contributed Notes

There are no user contributed notes for this page.


Click Handler

  • ≪ UI\Controls\Button::getText
  • UI\Controls\Button::setText ≫
  • PHP Manual
  • UI\Controls\Button
  • Click Handler

UI\Controls\Button::onClick

(PHP 7, UI 0.9.9)

UI\Controls\Button::onClick — Click Handler

설명

protected UI\Controls\Button::onClick ( void )

Shall be executed when this Button is clicked

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Text

  • ≪ UI\Controls\Button::onClick
  • UI\Controls\ColorButton ≫
  • PHP Manual
  • UI\Controls\Button
  • Set Text

UI\Controls\Button::setText

(PHP 7, UI 0.9.9)

UI\Controls\Button::setText — Set Text

설명

public UI\Controls\Button::setText ( string $text )

Shall set the text (label) for this Button

인수

text

The new text (label)

User Contributed Notes

There are no user contributed notes for this page.


ColorButton Control

(PHP 7, UI 0.9.9)

소개

A Color Button is a button which displays a color picker when clicked

클래스 개요

UI\Controls\ColorButton extends UI\Control {
/* 메소드 */
public UI\Color getColor ( void )
protected onChange ( void )
public setColor ( UI\Draw\Color $color )
public setColor ( integer $color )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

Table of Contents

  • UI\Controls\ColorButton::getColor — Get Color
  • UI\Controls\ColorButton::onChange — Change Handler
  • UI\Controls\ColorButton::setColor — Set Color

User Contributed Notes

There are no user contributed notes for this page.


Get Color

  • ≪ UI\Controls\ColorButton
  • UI\Controls\ColorButton::onChange ≫
  • PHP Manual
  • UI\Controls\ColorButton
  • Get Color

UI\Controls\ColorButton::getColor

(PHP 7, UI 0.9.9)

UI\Controls\ColorButton::getColor — Get Color

설명

public UI\Color UI\Controls\ColorButton::getColor ( void )

Shall retrieve the currently selected Color

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Change Handler

  • ≪ UI\Controls\ColorButton::getColor
  • UI\Controls\ColorButton::setColor ≫
  • PHP Manual
  • UI\Controls\ColorButton
  • Change Handler

UI\Controls\ColorButton::onChange

(PHP 7, UI 0.9.9)

UI\Controls\ColorButton::onChange — Change Handler

설명

protected UI\Controls\ColorButton::onChange ( void )

Shall be executed when the selected Color is changed

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Color

  • ≪ UI\Controls\ColorButton::onChange
  • UI\Controls\Label ≫
  • PHP Manual
  • UI\Controls\ColorButton
  • Set Color

UI\Controls\ColorButton::setColor

(PHP 7, UI 0.9.9)

UI\Controls\ColorButton::setColor — Set Color

설명

public UI\Controls\ColorButton::setColor ( UI\Draw\Color $color )
public UI\Controls\ColorButton::setColor ( integer $color )

Shall set the currently selected Color

인수

color

The new color

User Contributed Notes

There are no user contributed notes for this page.


Label Control

(PHP 7, UI 0.9.9)

소개

A Label is a single line of text, meant to identify, for the user, some element of the interface.

클래스 개요

UI\Controls\Label extends UI\Control {
/* Constructor */
public __construct ( string $text )
/* 메소드 */
public string getText ( void )
public setText ( string $text )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

Table of Contents

  • UI\Controls\Label::__construct — Construct a new Label
  • UI\Controls\Label::getText — Get Text
  • UI\Controls\Label::setText — Set Text

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Label

  • ≪ UI\Controls\Label
  • UI\Controls\Label::getText ≫
  • PHP Manual
  • UI\Controls\Label
  • Construct a new Label

UI\Controls\Label::__construct

(PHP 7, UI 0.9.9)

UI\Controls\Label::__construct — Construct a new Label

설명

public UI\Controls\Label::__construct ( string $text )

Shall construct a new Label

인수

text

The text for this label

User Contributed Notes

There are no user contributed notes for this page.


Get Text

  • ≪ UI\Controls\Label::__construct
  • UI\Controls\Label::setText ≫
  • PHP Manual
  • UI\Controls\Label
  • Get Text

UI\Controls\Label::getText

(PHP 7, UI 0.9.9)

UI\Controls\Label::getText — Get Text

설명

public string UI\Controls\Label::getText ( void )

Shall return the current text for this Label

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Set Text

  • ≪ UI\Controls\Label::getText
  • UI\Controls\Entry ≫
  • PHP Manual
  • UI\Controls\Label
  • Set Text

UI\Controls\Label::setText

(PHP 7, UI 0.9.9)

UI\Controls\Label::setText — Set Text

설명

public UI\Controls\Label::setText ( string $text )

Shall set the text for this Label

인수

text

The new text

User Contributed Notes

There are no user contributed notes for this page.


Entry Control

(PHP 7, UI 0.9.9)

소개

An Entry is a text entry control, suitable for entering plain text, passwords, or search terms.

클래스 개요

UI\Controls\Entry extends UI\Control {
/* Constants */
const integer Normal ;
const integer Password ;
const integer Search ;
/* Constructor */
public __construct ([ integer $type = UI\Controls\Entry::Normal ] )
/* 메소드 */
public string getText ( void )
public bool isReadOnly ( void )
protected onChange ( void )
public setReadOnly ( boolean $readOnly )
public setText ( string $text )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

예약 상수

UI\Controls\Entry::Normal

A normal single line entry

UI\Controls\Entry::Password

A password entry

UI\Controls\Entry::Search

A search entry

Table of Contents

  • UI\Controls\Entry::__construct — Construct a new Entry
  • UI\Controls\Entry::getText — Get Text
  • UI\Controls\Entry::isReadOnly — Detect Read Only
  • UI\Controls\Entry::onChange — Change Handler
  • UI\Controls\Entry::setReadOnly — Set Read Only
  • UI\Controls\Entry::setText — Set Text

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Entry

  • ≪ UI\Controls\Entry
  • UI\Controls\Entry::getText ≫
  • PHP Manual
  • UI\Controls\Entry
  • Construct a new Entry

UI\Controls\Entry::__construct

(PHP 7, UI 0.9.9)

UI\Controls\Entry::__construct — Construct a new Entry

설명

public UI\Controls\Entry::__construct ([ integer $type = UI\Controls\Entry::Normal ] )

Construct a new entry of the given type

인수

type

Entry::Normal, Entry::Password, or Entry::Search

User Contributed Notes

There are no user contributed notes for this page.


Get Text

  • ≪ UI\Controls\Entry::__construct
  • UI\Controls\Entry::isReadOnly ≫
  • PHP Manual
  • UI\Controls\Entry
  • Get Text

UI\Controls\Entry::getText

(PHP 7, UI 0.9.9)

UI\Controls\Entry::getText — Get Text

설명

public string UI\Controls\Entry::getText ( void )

Shall return the current text from this Entry

인수

이 함수는 인수가 없습니다.

반환값

The current text

User Contributed Notes

There are no user contributed notes for this page.


Detect Read Only

  • ≪ UI\Controls\Entry::getText
  • UI\Controls\Entry::onChange ≫
  • PHP Manual
  • UI\Controls\Entry
  • Detect Read Only

UI\Controls\Entry::isReadOnly

(PHP 7, UI 0.9.9)

UI\Controls\Entry::isReadOnly — Detect Read Only

설명

public bool UI\Controls\Entry::isReadOnly ( void )

Shall detect if this Entry is read only

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Change Handler

  • ≪ UI\Controls\Entry::isReadOnly
  • UI\Controls\Entry::setReadOnly ≫
  • PHP Manual
  • UI\Controls\Entry
  • Change Handler

UI\Controls\Entry::onChange

(PHP 7, UI 0.9.9)

UI\Controls\Entry::onChange — Change Handler

설명

protected UI\Controls\Entry::onChange ( void )

Shall be executed when the text in this Entry changes

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Read Only

  • ≪ UI\Controls\Entry::onChange
  • UI\Controls\Entry::setText ≫
  • PHP Manual
  • UI\Controls\Entry
  • Set Read Only

UI\Controls\Entry::setReadOnly

(PHP 7, UI 0.9.9)

UI\Controls\Entry::setReadOnly — Set Read Only

설명

public UI\Controls\Entry::setReadOnly ( boolean $readOnly )

Shall enable or disable read only for this Entry

인수

readOnly

User Contributed Notes

There are no user contributed notes for this page.


Set Text

  • ≪ UI\Controls\Entry::setReadOnly
  • UI\Controls\MultilineEntry ≫
  • PHP Manual
  • UI\Controls\Entry
  • Set Text

UI\Controls\Entry::setText

(PHP 7, UI 0.9.9)

UI\Controls\Entry::setText — Set Text

설명

public UI\Controls\Entry::setText ( string $text )

Shall set the text for this Entry

인수

text

The new text

User Contributed Notes

There are no user contributed notes for this page.


MultilineEntry Control

(PHP 7, UI 0.9.9)

소개

A Multiline Entry is a text entry control able to hold multiple lines of text, with or without wrapping.

클래스 개요

UI\Controls\MultilineEntry extends UI\Control {
/* Constants */
const integer Wrap ;
const integer NoWrap ;
/* Constructor */
public __construct ([ integer $type ] )
/* 메소드 */
public append ( string $text )
public string getText ( void )
public bool isReadOnly ( void )
protected onChange ( void )
public setReadOnly ( boolean $readOnly )
public setText ( string $text )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

예약 상수

UI\Controls\MultilineEntry::Wrap

Allow lines to wrap

UI\Controls\MultilineEntry::NoWrap

Do not allow lines to wrap

Table of Contents

  • UI\Controls\MultilineEntry::append — Append Text
  • UI\Controls\MultilineEntry::__construct — Construct a new Multiline Entry
  • UI\Controls\MultilineEntry::getText — Get Text
  • UI\Controls\MultilineEntry::isReadOnly — Read Only Detection
  • UI\Controls\MultilineEntry::onChange — Change Handler
  • UI\Controls\MultilineEntry::setReadOnly — Set Read Only
  • UI\Controls\MultilineEntry::setText — Set Text

User Contributed Notes

There are no user contributed notes for this page.


Append Text

  • ≪ UI\Controls\MultilineEntry
  • UI\Controls\MultilineEntry::__construct ≫
  • PHP Manual
  • UI\Controls\MultilineEntry
  • Append Text

UI\Controls\MultilineEntry::append

(PHP 7, UI 0.9.9)

UI\Controls\MultilineEntry::append — Append Text

설명

public UI\Controls\MultilineEntry::append ( string $text )

Shall append the given text to the text in this Multiline Entry

인수

text

The text to append

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Multiline Entry

  • ≪ UI\Controls\MultilineEntry::append
  • UI\Controls\MultilineEntry::getText ≫
  • PHP Manual
  • UI\Controls\MultilineEntry
  • Construct a new Multiline Entry

UI\Controls\MultilineEntry::__construct

(PHP 7, UI 0.9.9)

UI\Controls\MultilineEntry::__construct — Construct a new Multiline Entry

설명

public UI\Controls\MultilineEntry::__construct ([ integer $type ] )

Shall construct a new Multiline Entry of the given type

인수

type

MultilineEntry::Wrap or MultilineEntry::NoWrap

User Contributed Notes

There are no user contributed notes for this page.


Get Text

  • ≪ UI\Controls\MultilineEntry::__construct
  • UI\Controls\MultilineEntry::isReadOnly ≫
  • PHP Manual
  • UI\Controls\MultilineEntry
  • Get Text

UI\Controls\MultilineEntry::getText

(PHP 7, UI 0.9.9)

UI\Controls\MultilineEntry::getText — Get Text

설명

public string UI\Controls\MultilineEntry::getText ( void )

Shall return the text in this Multiline Entry

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Read Only Detection

  • ≪ UI\Controls\MultilineEntry::getText
  • UI\Controls\MultilineEntry::onChange ≫
  • PHP Manual
  • UI\Controls\MultilineEntry
  • Read Only Detection

UI\Controls\MultilineEntry::isReadOnly

(PHP 7, UI 0.9.9)

UI\Controls\MultilineEntry::isReadOnly — Read Only Detection

설명

public bool UI\Controls\MultilineEntry::isReadOnly ( void )

Shall detect if this Multiline Entry is read only

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Change Handler

  • ≪ UI\Controls\MultilineEntry::isReadOnly
  • UI\Controls\MultilineEntry::setReadOnly ≫
  • PHP Manual
  • UI\Controls\MultilineEntry
  • Change Handler

UI\Controls\MultilineEntry::onChange

(PHP 7, UI 0.9.9)

UI\Controls\MultilineEntry::onChange — Change Handler

설명

protected UI\Controls\MultilineEntry::onChange ( void )

Shall be executed when the text in this Multiline Entry is changed

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Read Only

  • ≪ UI\Controls\MultilineEntry::onChange
  • UI\Controls\MultilineEntry::setText ≫
  • PHP Manual
  • UI\Controls\MultilineEntry
  • Set Read Only

UI\Controls\MultilineEntry::setReadOnly

(PHP 7, UI 0.9.9)

UI\Controls\MultilineEntry::setReadOnly — Set Read Only

설명

public UI\Controls\MultilineEntry::setReadOnly ( boolean $readOnly )

Shall enable or disable read only on this Multiline Entry

인수

readOnly

User Contributed Notes

There are no user contributed notes for this page.


Set Text

  • ≪ UI\Controls\MultilineEntry::setReadOnly
  • UI\Controls\Spin ≫
  • PHP Manual
  • UI\Controls\MultilineEntry
  • Set Text

UI\Controls\MultilineEntry::setText

(PHP 7, UI 0.9.9)

UI\Controls\MultilineEntry::setText — Set Text

설명

public UI\Controls\MultilineEntry::setText ( string $text )

Shall set the text in this Multiline Entry

인수

text

The new text

User Contributed Notes

There are no user contributed notes for this page.


Spin Control

(PHP 7, UI 0.9.9)

소개

A Spin box is a text box with an up-down control which changes the integer value in the box, within a defined range

클래스 개요

UI\Controls\Spin extends UI\Control {
/* Constructor */
public __construct ( integer $min , integer $max )
/* 메소드 */
public int getValue ( void )
protected onChange ( void )
public setValue ( integer $value )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

Table of Contents

  • UI\Controls\Spin::__construct — Construct a new Spin
  • UI\Controls\Spin::getValue — Get Value
  • UI\Controls\Spin::onChange — Change Handler
  • UI\Controls\Spin::setValue — Set Value

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Spin

  • ≪ UI\Controls\Spin
  • UI\Controls\Spin::getValue ≫
  • PHP Manual
  • UI\Controls\Spin
  • Construct a new Spin

UI\Controls\Spin::__construct

(PHP 7, UI 0.9.9)

UI\Controls\Spin::__construct — Construct a new Spin

설명

public UI\Controls\Spin::__construct ( integer $min , integer $max )

Constructs a new Spin with the given range

인수

min

The minimum value allowed

max

The maximum value allowed

User Contributed Notes

There are no user contributed notes for this page.


Get Value

  • ≪ UI\Controls\Spin::__construct
  • UI\Controls\Spin::onChange ≫
  • PHP Manual
  • UI\Controls\Spin
  • Get Value

UI\Controls\Spin::getValue

(PHP 7, UI 0.9.9)

UI\Controls\Spin::getValue — Get Value

설명

public int UI\Controls\Spin::getValue ( void )

Get the value in this Spin

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Change Handler

  • ≪ UI\Controls\Spin::getValue
  • UI\Controls\Spin::setValue ≫
  • PHP Manual
  • UI\Controls\Spin
  • Change Handler

UI\Controls\Spin::onChange

(PHP 7, UI 0.9.9)

UI\Controls\Spin::onChange — Change Handler

설명

protected UI\Controls\Spin::onChange ( void )

Shall be executed when the value in this Spin changes

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Value

  • ≪ UI\Controls\Spin::onChange
  • UI\Controls\Slider ≫
  • PHP Manual
  • UI\Controls\Spin
  • Set Value

UI\Controls\Spin::setValue

(PHP 7, UI 0.9.9)

UI\Controls\Spin::setValue — Set Value

설명

public UI\Controls\Spin::setValue ( integer $value )

Set the value in this Spin

인수

value

The new value

User Contributed Notes

There are no user contributed notes for this page.


Slider Control

(PHP 7, UI 0.9.9)

소개

A Slider is a control which represents a range, and a current value in the range. The sliding element of the control (sometimes called the "thumb") reflects the value, and can be adjusted within the range.

클래스 개요

UI\Controls\Slider extends UI\Control {
/* Constructor */
public __construct ( integer $min , integer $max )
/* 메소드 */
public integer getValue ( void )
protected onChange ( void )
public setValue ( integer $value )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

Table of Contents

  • UI\Controls\Slider::__construct — Construct a new Slider
  • UI\Controls\Slider::getValue — Get Value
  • UI\Controls\Slider::onChange — Change Handler
  • UI\Controls\Slider::setValue — Set Value

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Slider

  • ≪ UI\Controls\Slider
  • UI\Controls\Slider::getValue ≫
  • PHP Manual
  • UI\Controls\Slider
  • Construct a new Slider

UI\Controls\Slider::__construct

(PHP 7, UI 0.9.9)

UI\Controls\Slider::__construct — Construct a new Slider

설명

public UI\Controls\Slider::__construct ( integer $min , integer $max )

Construct a new Slider with the given range

인수

min

The minimum allowed value

max

The maximum allowed value

User Contributed Notes

There are no user contributed notes for this page.


Get Value

  • ≪ UI\Controls\Slider::__construct
  • UI\Controls\Slider::onChange ≫
  • PHP Manual
  • UI\Controls\Slider
  • Get Value

UI\Controls\Slider::getValue

(PHP 7, UI 0.9.9)

UI\Controls\Slider::getValue — Get Value

설명

public integer UI\Controls\Slider::getValue ( void )

Get the value from this Slider

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Change Handler

  • ≪ UI\Controls\Slider::getValue
  • UI\Controls\Slider::setValue ≫
  • PHP Manual
  • UI\Controls\Slider
  • Change Handler

UI\Controls\Slider::onChange

(PHP 7, UI 0.9.9)

UI\Controls\Slider::onChange — Change Handler

설명

protected UI\Controls\Slider::onChange ( void )

Shall be executed when the value of this Slider changes

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Value

  • ≪ UI\Controls\Slider::onChange
  • UI\Controls\Progress ≫
  • PHP Manual
  • UI\Controls\Slider
  • Set Value

UI\Controls\Slider::setValue

(PHP 7, UI 0.9.9)

UI\Controls\Slider::setValue — Set Value

설명

public UI\Controls\Slider::setValue ( integer $value )

Shall set the value for this Slider

인수

value

The new value

User Contributed Notes

There are no user contributed notes for this page.


Progress Control

(PHP 7, UI 0.9.9)

소개

A Progress control is a familiar Progress bar: It represents progress as a percentage, with a possible range of 0 to 100 (inclusive).

클래스 개요

UI\Controls\Progress extends UI\Control {
/* 메소드 */
public int getValue ( void )
public setValue ( integer $value )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

Table of Contents

  • UI\Controls\Progress::getValue — Get Value
  • UI\Controls\Progress::setValue — Set Value

User Contributed Notes

There are no user contributed notes for this page.


Get Value

  • ≪ UI\Controls\Progress
  • UI\Controls\Progress::setValue ≫
  • PHP Manual
  • UI\Controls\Progress
  • Get Value

UI\Controls\Progress::getValue

(PHP 7, UI 0.9.9)

UI\Controls\Progress::getValue — Get Value

설명

public int UI\Controls\Progress::getValue ( void )

Shall retrieve the current value of this Progress bar

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Value

  • ≪ UI\Controls\Progress::getValue
  • UI\Controls\Separator ≫
  • PHP Manual
  • UI\Controls\Progress
  • Set Value

UI\Controls\Progress::setValue

(PHP 7, UI 0.9.9)

UI\Controls\Progress::setValue — Set Value

설명

public UI\Controls\Progress::setValue ( integer $value )

Shall set the value for this Progress bar

인수

value

An integer between 0 and 100 (inclusive)

User Contributed Notes

There are no user contributed notes for this page.


Control Separator

(PHP 7, UI 0.9.9)

소개

A Separator represents a control separator, it has no other function.

클래스 개요

UI\Controls\Separator extends UI\Control {
/* Constants */
const integer Horizontal ;
const integer Vertical ;
/* Constructor */
public __construct ([ integer $type = UI\Controls\Separator::Horizontal ] )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

예약 상수

UI\Controls\Separator::Horizontal

A Horizontal Separator

UI\Controls\Separator::Vertical

A Vertical Separator

Table of Contents

  • UI\Controls\Separator::__construct — Construct a new Separator

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Separator

  • ≪ UI\Controls\Separator
  • UI\Controls\Combo ≫
  • PHP Manual
  • UI\Controls\Separator
  • Construct a new Separator

UI\Controls\Separator::__construct

(PHP 7, UI 0.9.9)

UI\Controls\Separator::__construct — Construct a new Separator

설명

public UI\Controls\Separator::__construct ([ integer $type = UI\Controls\Separator::Horizontal ] )

Shall construct a new Separator of the given type

인수

type

Separator::Horizonal or Separator::Vertical

User Contributed Notes

There are no user contributed notes for this page.


Combo Control

(PHP 7, UI 0.9.9)

소개

A Combo control represents a list of options, like the familiar select HTML element.

클래스 개요

UI\Controls\Combo extends UI\Control {
/* 메소드 */
public append ( string $text )
public integer getSelected ( void )
protected onSelected ( void )
public setSelected ( integer $index )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

Table of Contents

  • UI\Controls\Combo::append — Append Option
  • UI\Controls\Combo::getSelected — Get Selected Option
  • UI\Controls\Combo::onSelected — Selected Handler
  • UI\Controls\Combo::setSelected — Set Selected Option

User Contributed Notes

There are no user contributed notes for this page.


Append Option

  • ≪ UI\Controls\Combo
  • UI\Controls\Combo::getSelected ≫
  • PHP Manual
  • UI\Controls\Combo
  • Append Option

UI\Controls\Combo::append

(PHP 7, UI 0.9.9)

UI\Controls\Combo::append — Append Option

설명

public UI\Controls\Combo::append ( string $text )

Append an option to this Combo

인수

text

The text for the new option

User Contributed Notes

There are no user contributed notes for this page.


Get Selected Option

  • ≪ UI\Controls\Combo::append
  • UI\Controls\Combo::onSelected ≫
  • PHP Manual
  • UI\Controls\Combo
  • Get Selected Option

UI\Controls\Combo::getSelected

(PHP 7, UI 0.9.9)

UI\Controls\Combo::getSelected — Get Selected Option

설명

public integer UI\Controls\Combo::getSelected ( void )

Shall retrieve the index of the option selected in this Combo

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Selected Handler

  • ≪ UI\Controls\Combo::getSelected
  • UI\Controls\Combo::setSelected ≫
  • PHP Manual
  • UI\Controls\Combo
  • Selected Handler

UI\Controls\Combo::onSelected

(PHP 7, UI 0.9.9)

UI\Controls\Combo::onSelected — Selected Handler

설명

protected UI\Controls\Combo::onSelected ( void )

Shall be executed when an option is selected in this Combo

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Selected Option

  • ≪ UI\Controls\Combo::onSelected
  • UI\Controls\EditableCombo ≫
  • PHP Manual
  • UI\Controls\Combo
  • Set Selected Option

UI\Controls\Combo::setSelected

(PHP 7, UI 0.9.9)

UI\Controls\Combo::setSelected — Set Selected Option

설명

public UI\Controls\Combo::setSelected ( integer $index )

Shall set the currently selected option in this Combo

인수

index

The index of the option to select

User Contributed Notes

There are no user contributed notes for this page.


EdiableCombo Control

(PHP 7, UI 0.9.9)

소개

An Editable Combo is a Combo which allows the user to enter custom options

클래스 개요

UI\Controls\EditableCombo extends UI\Control {
/* 메소드 */
public append ( string $text )
public string getText ( void )
protected onChange ( void )
public setText ( string $text )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

Table of Contents

  • UI\Controls\EditableCombo::append — Append Option
  • UI\Controls\EditableCombo::getText — Get Text
  • UI\Controls\EditableCombo::onChange — Change Handler
  • UI\Controls\EditableCombo::setText — Set Text

User Contributed Notes

There are no user contributed notes for this page.


Append Option

  • ≪ UI\Controls\EditableCombo
  • UI\Controls\EditableCombo::getText ≫
  • PHP Manual
  • UI\Controls\EditableCombo
  • Append Option

UI\Controls\EditableCombo::append

(PHP 7, UI 0.9.9)

UI\Controls\EditableCombo::append — Append Option

설명

public UI\Controls\EditableCombo::append ( string $text )

Shall append a new option to this Editable Combo

인수

text

The text for the new option

User Contributed Notes

There are no user contributed notes for this page.


Get Text

  • ≪ UI\Controls\EditableCombo::append
  • UI\Controls\EditableCombo::onChange ≫
  • PHP Manual
  • UI\Controls\EditableCombo
  • Get Text

UI\Controls\EditableCombo::getText

(PHP 7, UI 0.9.9)

UI\Controls\EditableCombo::getText — Get Text

설명

public string UI\Controls\EditableCombo::getText ( void )

Get the value of the currently selected option in this Editable Combo

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Change Handler

  • ≪ UI\Controls\EditableCombo::getText
  • UI\Controls\EditableCombo::setText ≫
  • PHP Manual
  • UI\Controls\EditableCombo
  • Change Handler

UI\Controls\EditableCombo::onChange

(PHP 7, UI 0.9.9)

UI\Controls\EditableCombo::onChange — Change Handler

설명

protected UI\Controls\EditableCombo::onChange ( void )

Shall be executed when the value of this Editable Combobox changes

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Text

  • ≪ UI\Controls\EditableCombo::onChange
  • UI\Controls\Radio ≫
  • PHP Manual
  • UI\Controls\EditableCombo
  • Set Text

UI\Controls\EditableCombo::setText

(PHP 7, UI 0.9.9)

UI\Controls\EditableCombo::setText — Set Text

설명

public UI\Controls\EditableCombo::setText ( string $text )

Shall set the text of the currently selected option in this Editable Combo

인수

text

The new text

User Contributed Notes

There are no user contributed notes for this page.


Radio Control

(PHP 7, UI 0.9.9)

소개

A Radio is similar to the radio input type familiar from HTML

클래스 개요

UI\Controls\Radio extends UI\Control {
/* 메소드 */
public append ( string $text )
public integer getSelected ( void )
protected onSelected ( void )
public setSelected ( integer $index )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

Table of Contents

  • UI\Controls\Radio::append — Append Option
  • UI\Controls\Radio::getSelected — Get Selected Option
  • UI\Controls\Radio::onSelected — Selected Handler
  • UI\Controls\Radio::setSelected — Set Selected Option

User Contributed Notes

There are no user contributed notes for this page.


Append Option

  • ≪ UI\Controls\Radio
  • UI\Controls\Radio::getSelected ≫
  • PHP Manual
  • UI\Controls\Radio
  • Append Option

UI\Controls\Radio::append

(PHP 7, UI 0.9.9)

UI\Controls\Radio::append — Append Option

설명

public UI\Controls\Radio::append ( string $text )

Shall append a new option to this Radio

인수

text

The text (label) for the option

User Contributed Notes

There are no user contributed notes for this page.


Get Selected Option

  • ≪ UI\Controls\Radio::append
  • UI\Controls\Radio::onSelected ≫
  • PHP Manual
  • UI\Controls\Radio
  • Get Selected Option

UI\Controls\Radio::getSelected

(PHP 7, UI 0.9.9)

UI\Controls\Radio::getSelected — Get Selected Option

설명

public integer UI\Controls\Radio::getSelected ( void )

Shall retrieve the index of the currently selected option in this Radio

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Selected Handler

  • ≪ UI\Controls\Radio::getSelected
  • UI\Controls\Radio::setSelected ≫
  • PHP Manual
  • UI\Controls\Radio
  • Selected Handler

UI\Controls\Radio::onSelected

(PHP 7, UI 0.9.9)

UI\Controls\Radio::onSelected — Selected Handler

설명

protected UI\Controls\Radio::onSelected ( void )

Shall be executed when the option selected in this Radio changes

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Selected Option

  • ≪ UI\Controls\Radio::onSelected
  • UI\Controls\Picker ≫
  • PHP Manual
  • UI\Controls\Radio
  • Set Selected Option

UI\Controls\Radio::setSelected

(PHP 7, UI 0.9.9)

UI\Controls\Radio::setSelected — Set Selected Option

설명

public UI\Controls\Radio::setSelected ( integer $index )

Shall set the currently selected option in this Radio

인수

index

The index of the option to select

User Contributed Notes

There are no user contributed notes for this page.


Picker Control

(PHP 7, UI 0.9.9)

소개

A Picker represents a button which when clicked presents a native Date/Time/DateTime Picker interface to the user.

클래스 개요

UI\Controls\Picker extends UI\Control {
/* Constants */
const integer Date ;
const integer Time ;
const integer DateTime ;
/* Constructor */
public __construct ([ integer $type = UI\Controls\Picker::Date ] )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

예약 상수

UI\Controls\Picker::Date

A Date Picker

UI\Controls\Picker::Time

A Time Picker

UI\Controls\Picker::DateTime

A Date and Time Picker

Table of Contents

  • UI\Controls\Picker::__construct — Construct a new Picker

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Picker

  • ≪ UI\Controls\Picker
  • UI\Controls\Form ≫
  • PHP Manual
  • UI\Controls\Picker
  • Construct a new Picker

UI\Controls\Picker::__construct

(PHP 7, UI 0.9.9)

UI\Controls\Picker::__construct — Construct a new Picker

설명

public UI\Controls\Picker::__construct ([ integer $type = UI\Controls\Picker::Date ] )

Construct a new Picker of the given type

인수

type

Picker::Date, Picker::Time, or Picker::DateTime

User Contributed Notes

There are no user contributed notes for this page.


Control Form (Arrangement)

(PHP 7, UI 0.9.9)

소개

A Form is a control which allows the arrangement of other controls into a familiar layout (the form).

클래스 개요

UI\Controls\Form extends UI\Control {
/* 프로퍼티 */
protected $controls ;
/* 메소드 */
public integer append ( string $label , UI\Control $control [, boolean $stretchy = false ] )
public boolean delete ( integer $index )
public bool isPadded ( void )
public setPadded ( boolean $padded )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

프로퍼티

controls

Contains controls, should not be manipulated directly

Table of Contents

  • UI\Controls\Form::append — Append Control
  • UI\Controls\Form::delete — Delete Control
  • UI\Controls\Form::isPadded — Padding Detection
  • UI\Controls\Form::setPadded — Set Padding

User Contributed Notes

There are no user contributed notes for this page.


Append Control

  • ≪ UI\Controls\Form
  • UI\Controls\Form::delete ≫
  • PHP Manual
  • UI\Controls\Form
  • Append Control

UI\Controls\Form::append

(PHP 7, UI 0.9.9)

UI\Controls\Form::append — Append Control

설명

public integer UI\Controls\Form::append ( string $label , UI\Control $control [, boolean $stretchy = false ] )

Shall append the control to the form, and set the label

인수

label

The text for the label

control

A control

stretchy

Should be set true to stretch the control

반환값

Shall return the index of the appended control, may be 0

User Contributed Notes

There are no user contributed notes for this page.


Delete Control

  • ≪ UI\Controls\Form::append
  • UI\Controls\Form::isPadded ≫
  • PHP Manual
  • UI\Controls\Form
  • Delete Control

UI\Controls\Form::delete

(PHP 7, UI 0.9.9)

UI\Controls\Form::delete — Delete Control

설명

public boolean UI\Controls\Form::delete ( integer $index )

Shall delete the control at the given index in this Form

인수

index

The index of the control to remove

반환값

Indication of succcess

User Contributed Notes

There are no user contributed notes for this page.


Padding Detection

  • ≪ UI\Controls\Form::delete
  • UI\Controls\Form::setPadded ≫
  • PHP Manual
  • UI\Controls\Form
  • Padding Detection

UI\Controls\Form::isPadded

(PHP 7, UI 0.9.9)

UI\Controls\Form::isPadded — Padding Detection

설명

public bool UI\Controls\Form::isPadded ( void )

Shall detect if padding is enabled on this Form

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Padding

  • ≪ UI\Controls\Form::isPadded
  • UI\Controls\Grid ≫
  • PHP Manual
  • UI\Controls\Form
  • Set Padding

UI\Controls\Form::setPadded

(PHP 7, UI 0.9.9)

UI\Controls\Form::setPadded — Set Padding

설명

public UI\Controls\Form::setPadded ( boolean $padded )

Shall enable or disable padding on this Form

인수

padded

User Contributed Notes

There are no user contributed notes for this page.


Control Grid (Arrangement)

(PHP 7, UI 0.9.9)

소개

A Grid is a control which is allows the arrangement of children into a grid

클래스 개요

UI\Controls\Grid extends UI\Control {
/* Constants */
const integer Fill ;
const integer Start ;
const integer Center ;
const integer End ;
const integer Leading ;
const integer Top ;
const integer Trailing ;
const integer Bottom ;
/* 프로퍼티 */
protected $controls ;
/* 메소드 */
public append ( UI\Control $control , integer $left , integer $top , integer $xspan , integer $yspan , boolean $hexpand , integer $halign , boolean $vexpand , integer $valign )
public bool isPadded ( void )
public setPadded ( boolean $padding )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

예약 상수

UI\Controls\Grid::Fill

UI\Controls\Grid::Start

UI\Controls\Grid::Center

UI\Controls\Grid::End

UI\Controls\Grid::Leading

UI\Controls\Grid::Top

UI\Controls\Grid::Trailing

UI\Controls\Grid::Bottom

프로퍼티

controls

Contains controls, should not be manipulated directly

Table of Contents

  • UI\Controls\Grid::append — Append Control
  • UI\Controls\Grid::isPadded — Padding Detection
  • UI\Controls\Grid::setPadded — Set Padding

User Contributed Notes

There are no user contributed notes for this page.


Append Control

  • ≪ UI\Controls\Grid
  • UI\Controls\Grid::isPadded ≫
  • PHP Manual
  • UI\Controls\Grid
  • Append Control

UI\Controls\Grid::append

(PHP 7, UI 0.9.9)

UI\Controls\Grid::append — Append Control

설명

public UI\Controls\Grid::append ( UI\Control $control , integer $left , integer $top , integer $xspan , integer $yspan , boolean $hexpand , integer $halign , boolean $vexpand , integer $valign )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

control

The Control to append

left

top

xspan

yspan

hexpand

halign

vexpand

valign

반환값

User Contributed Notes

There are no user contributed notes for this page.


Padding Detection

  • ≪ UI\Controls\Grid::append
  • UI\Controls\Grid::setPadded ≫
  • PHP Manual
  • UI\Controls\Grid
  • Padding Detection

UI\Controls\Grid::isPadded

(PHP 7, UI 0.9.9)

UI\Controls\Grid::isPadded — Padding Detection

설명

public bool UI\Controls\Grid::isPadded ( void )

Shall detect if padding is enabled on this Grid

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Padding

  • ≪ UI\Controls\Grid::isPadded
  • UI\Controls\Group ≫
  • PHP Manual
  • UI\Controls\Grid
  • Set Padding

UI\Controls\Grid::setPadded

(PHP 7, UI 0.9.9)

UI\Controls\Grid::setPadded — Set Padding

설명

public UI\Controls\Grid::setPadded ( boolean $padding )

Shall enable or disable padding for this Grid

인수

padding

User Contributed Notes

There are no user contributed notes for this page.


Control Group (Arrangement)

(PHP 7, UI 0.9.9)

소개

A Group is a titled container for child controls

클래스 개요

UI\Controls\Group extends UI\Control {
/* 프로퍼티 */
protected $controls ;
/* Constructor */
public __construct ( string $title )
/* 메소드 */
public append ( UI\Control $control )
public string getTitle ( void )
public bool hasMargin ( void )
public setMargin ( boolean $margin )
public setTitle ( string $title )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

프로퍼티

controls

Contains controls, should not be manipulated directly

Table of Contents

  • UI\Controls\Group::append — Append Control
  • UI\Controls\Group::__construct — Construct a new Group
  • UI\Controls\Group::getTitle — Get Title
  • UI\Controls\Group::hasMargin — Margin Detection
  • UI\Controls\Group::setMargin — Set Margin
  • UI\Controls\Group::setTitle — Set Title

User Contributed Notes

There are no user contributed notes for this page.


Append Control

  • ≪ UI\Controls\Group
  • UI\Controls\Group::__construct ≫
  • PHP Manual
  • UI\Controls\Group
  • Append Control

UI\Controls\Group::append

(No version information available, might only be in Git)

UI\Controls\Group::append — Append Control

설명

public UI\Controls\Group::append ( UI\Control $control )

Shall append a control to this Group

인수

control

The control to append

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Group

  • ≪ UI\Controls\Group::append
  • UI\Controls\Group::getTitle ≫
  • PHP Manual
  • UI\Controls\Group
  • Construct a new Group

UI\Controls\Group::__construct

(PHP 7, UI 0.9.9)

UI\Controls\Group::__construct — Construct a new Group

설명

public UI\Controls\Group::__construct ( string $title )

Shall construct a new Group with the given title

인수

title

The text for the title label

User Contributed Notes

There are no user contributed notes for this page.


Get Title

  • ≪ UI\Controls\Group::__construct
  • UI\Controls\Group::hasMargin ≫
  • PHP Manual
  • UI\Controls\Group
  • Get Title

UI\Controls\Group::getTitle

(PHP 7, UI 0.9.9)

UI\Controls\Group::getTitle — Get Title

설명

public string UI\Controls\Group::getTitle ( void )

Shall return the current title for this Group

인수

이 함수는 인수가 없습니다.

반환값

The current title

User Contributed Notes

There are no user contributed notes for this page.


Margin Detection

  • ≪ UI\Controls\Group::getTitle
  • UI\Controls\Group::setMargin ≫
  • PHP Manual
  • UI\Controls\Group
  • Margin Detection

UI\Controls\Group::hasMargin

(PHP 7, UI 0.9.9)

UI\Controls\Group::hasMargin — Margin Detection

설명

public bool UI\Controls\Group::hasMargin ( void )

Shall detect if this Group has a margin

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Margin

  • ≪ UI\Controls\Group::hasMargin
  • UI\Controls\Group::setTitle ≫
  • PHP Manual
  • UI\Controls\Group
  • Set Margin

UI\Controls\Group::setMargin

(PHP 7, UI 0.9.9)

UI\Controls\Group::setMargin — Set Margin

설명

public UI\Controls\Group::setMargin ( boolean $margin )

Shall enable or disable margins for this Group

인수

margin

User Contributed Notes

There are no user contributed notes for this page.


Set Title

  • ≪ UI\Controls\Group::setMargin
  • UI\Controls\Box ≫
  • PHP Manual
  • UI\Controls\Group
  • Set Title

UI\Controls\Group::setTitle

(PHP 7, UI 0.9.9)

UI\Controls\Group::setTitle — Set Title

설명

public UI\Controls\Group::setTitle ( string $title )

Shall set the title for this Group

인수

title

The text for the new title

User Contributed Notes

There are no user contributed notes for this page.


Control Box (Arrangement)

(PHP 7, UI 0.9.9)

소개

A Box allows the arrangement of other controls

클래스 개요

UI\Controls\Box extends UI\Control {
/* Constants */
const integer Vertical ;
const integer Horizontal ;
/* 프로퍼티 */
protected $controls ;
/* Constructor */
public __construct ([ integer $orientation = UI\Controls\Box::Horizontal ] )
/* 메소드 */
public integer append ( Control $control [, boolean $stretchy = false ] )
public boolean delete ( integer $index )
public integer getOrientation ( void )
public bool isPadded ( void )
public setPadded ( boolean $padded )
/* 상속된 메소드 */
public UI\Control::destroy ( void )
public UI\Control::disable ( void )
public UI\Control::enable ( void )
public UI\Control UI\Control::getParent ( void )
public int UI\Control::getTopLevel ( void )
public UI\Control::hide ( void )
public bool UI\Control::isEnabled ( void )
public bool UI\Control::isVisible ( void )
public UI\Control::setParent ( UI\Control $parent )
public UI\Control::show ( void )
}

프로퍼티

controls

Contains controls, should not be manipulated directly

예약 상수

UI\Controls\Box::Vertical

UI\Controls\Box::Horizontal

Table of Contents

  • UI\Controls\Box::append — Append Control
  • UI\Controls\Box::__construct — Construct a new Box
  • UI\Controls\Box::delete — Delete Control
  • UI\Controls\Box::getOrientation — Get Orientation
  • UI\Controls\Box::isPadded — Padding Detection
  • UI\Controls\Box::setPadded — Set Padding

User Contributed Notes

There are no user contributed notes for this page.


Append Control

  • ≪ UI\Controls\Box
  • UI\Controls\Box::__construct ≫
  • PHP Manual
  • UI\Controls\Box
  • Append Control

UI\Controls\Box::append

(PHP 7, UI 0.9.9)

UI\Controls\Box::append — Append Control

설명

public integer UI\Controls\Box::append ( Control $control [, boolean $stretchy = false ] )

Shall append the given control to this Box

인수

control

The control to append

stretchy

Set true to stretch the control

반환값

Shall return the index of the appended control, may be 0

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Box

  • ≪ UI\Controls\Box::append
  • UI\Controls\Box::delete ≫
  • PHP Manual
  • UI\Controls\Box
  • Construct a new Box

UI\Controls\Box::__construct

(PHP 7, UI 0.9.9)

UI\Controls\Box::__construct — Construct a new Box

설명

public UI\Controls\Box::__construct ([ integer $orientation = UI\Controls\Box::Horizontal ] )

Shall construct a new box

인수

orientation

Box::Horizontal or Box::Vertical

User Contributed Notes

There are no user contributed notes for this page.


Delete Control

  • ≪ UI\Controls\Box::__construct
  • UI\Controls\Box::getOrientation ≫
  • PHP Manual
  • UI\Controls\Box
  • Delete Control

UI\Controls\Box::delete

(PHP 7, UI 0.9.9)

UI\Controls\Box::delete — Delete Control

설명

public boolean UI\Controls\Box::delete ( integer $index )

Shall delete the control at the given index from this Box

인수

index

The index of the control to delete

반환값

Indication of success

User Contributed Notes

There are no user contributed notes for this page.


Get Orientation

  • ≪ UI\Controls\Box::delete
  • UI\Controls\Box::isPadded ≫
  • PHP Manual
  • UI\Controls\Box
  • Get Orientation

UI\Controls\Box::getOrientation

(PHP 7, UI 0.9.9)

UI\Controls\Box::getOrientation — Get Orientation

설명

public integer UI\Controls\Box::getOrientation ( void )

Shall retrieve the orientation of this Box

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Padding Detection

  • ≪ UI\Controls\Box::getOrientation
  • UI\Controls\Box::setPadded ≫
  • PHP Manual
  • UI\Controls\Box
  • Padding Detection

UI\Controls\Box::isPadded

(PHP 7, UI 0.9.9)

UI\Controls\Box::isPadded — Padding Detection

설명

public bool UI\Controls\Box::isPadded ( void )

Shall detect if padding is enabled on this Box

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Set Padding

  • ≪ UI\Controls\Box::isPadded
  • UI\Draw\Pen ≫
  • PHP Manual
  • UI\Controls\Box
  • Set Padding

UI\Controls\Box::setPadded

(PHP 7, UI 0.9.9)

UI\Controls\Box::setPadded — Set Padding

설명

public UI\Controls\Box::setPadded ( boolean $padded )

Shall enable or disable padding on this Box

인수

padded

User Contributed Notes

There are no user contributed notes for this page.


Draw Pen

(PHP 7, UI 0.9.9)

소개

The Pen is passed to the Area Draw event handler, it is used for clipping, filling, stroking, and writing to Draw Paths.

클래스 개요

final UI\Draw\Pen {
/* 메소드 */
public clip ( UI\Draw\Path $path )
public fill ( UI\Draw\Path $path , UI\Draw\Brush $with )
public fill ( UI\Draw\Path $path , UI\Draw\Color $with )
public fill ( UI\Draw\Path $path , integer $with )
public restore ( void )
public save ( void )
public stroke ( UI\Draw\Path $path , UI\Draw\Brush $with , UI\Draw\Stroke $stroke )
public stroke ( UI\Draw\Path $path , UI\Draw\Color $with , UI\Draw\Stroke $stroke )
public stroke ( UI\Draw\Path $path , integer $with , UI\Draw\Stroke $stroke )
public transform ( UI\Draw\Matrix $matrix )
public write ( UI\Point $point , UI\Draw\Text\Layout $layout )
}

Table of Contents

  • UI\Draw\Pen::clip — Clip a Path
  • UI\Draw\Pen::fill — Fill a Path
  • UI\Draw\Pen::restore — Restore
  • UI\Draw\Pen::save — Save
  • UI\Draw\Pen::stroke — Stroke a Path
  • UI\Draw\Pen::transform — Matrix Transform
  • UI\Draw\Pen::write — Draw Text at Point

User Contributed Notes

There are no user contributed notes for this page.


Clip a Path

  • ≪ UI\Draw\Pen
  • UI\Draw\Pen::fill ≫
  • PHP Manual
  • UI\Draw\Pen
  • Clip a Path

UI\Draw\Pen::clip

(PHP 7, UI 0.9.9)

UI\Draw\Pen::clip — Clip a Path

설명

public UI\Draw\Pen::clip ( UI\Draw\Path $path )

Shall clip the given Path

인수

path

The path to clip

User Contributed Notes

There are no user contributed notes for this page.


Fill a Path

  • ≪ UI\Draw\Pen::clip
  • UI\Draw\Pen::restore ≫
  • PHP Manual
  • UI\Draw\Pen
  • Fill a Path

UI\Draw\Pen::fill

(PHP 7, UI 0.9.9)

UI\Draw\Pen::fill — Fill a Path

설명

public UI\Draw\Pen::fill ( UI\Draw\Path $path , UI\Draw\Brush $with )
public UI\Draw\Pen::fill ( UI\Draw\Path $path , UI\Draw\Color $with )
public UI\Draw\Pen::fill ( UI\Draw\Path $path , integer $with )

Shall fill the given path

인수

path

The path to fill

with

The color or brush to fill with

User Contributed Notes

There are no user contributed notes for this page.


Restore

  • ≪ UI\Draw\Pen::fill
  • UI\Draw\Pen::save ≫
  • PHP Manual
  • UI\Draw\Pen
  • Restore

UI\Draw\Pen::restore

(PHP 7, UI 0.9.9)

UI\Draw\Pen::restore — Restore

설명

public UI\Draw\Pen::restore ( void )

Shall restore a previously saved Pen

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Save

  • ≪ UI\Draw\Pen::restore
  • UI\Draw\Pen::stroke ≫
  • PHP Manual
  • UI\Draw\Pen
  • Save

UI\Draw\Pen::save

(PHP 7, UI 0.9.9)

UI\Draw\Pen::save — Save

설명

public UI\Draw\Pen::save ( void )

Shall save the Pen

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Stroke a Path

  • ≪ UI\Draw\Pen::save
  • UI\Draw\Pen::transform ≫
  • PHP Manual
  • UI\Draw\Pen
  • Stroke a Path

UI\Draw\Pen::stroke

(PHP 7, UI 0.9.9)

UI\Draw\Pen::stroke — Stroke a Path

설명

public UI\Draw\Pen::stroke ( UI\Draw\Path $path , UI\Draw\Brush $with , UI\Draw\Stroke $stroke )
public UI\Draw\Pen::stroke ( UI\Draw\Path $path , UI\Draw\Color $with , UI\Draw\Stroke $stroke )
public UI\Draw\Pen::stroke ( UI\Draw\Path $path , integer $with , UI\Draw\Stroke $stroke )

Shall stroke the given path

인수

path

The path to stroke

with

The color or brush to stroke with

stroke

The configuration of the stroke

User Contributed Notes

There are no user contributed notes for this page.


Matrix Transform

  • ≪ UI\Draw\Pen::stroke
  • UI\Draw\Pen::write ≫
  • PHP Manual
  • UI\Draw\Pen
  • Matrix Transform

UI\Draw\Pen::transform

(PHP 7, UI 0.9.9)

UI\Draw\Pen::transform — Matrix Transform

설명

public UI\Draw\Pen::transform ( UI\Draw\Matrix $matrix )

Shall perform matrix transformation

인수

matrix

The matrix to use

User Contributed Notes

There are no user contributed notes for this page.


Draw Text at Point

  • ≪ UI\Draw\Pen::transform
  • UI\Draw\Path ≫
  • PHP Manual
  • UI\Draw\Pen
  • Draw Text at Point

UI\Draw\Pen::write

(PHP 7, UI 0.9.9)

UI\Draw\Pen::write — Draw Text at Point

설명

public UI\Draw\Pen::write ( UI\Point $point , UI\Draw\Text\Layout $layout )

Shall draw the given text layout at the given point

인수

point

The point to perform the drawing

layout

The layout of the text to draw

User Contributed Notes

There are no user contributed notes for this page.


Draw Path

(PHP 7, UI 0.9.9)

소개

A Draw Path guides a Draw Pen, telling the Pen where to draw on an Area.

클래스 개요

UI\Draw\Path {
/* Constants */
const integer Winding ;
const integer Alternate ;
/* Constructor */
public __construct ([ integer $mode = UI\Draw\Path::Winding ] )
/* 메소드 */
public addRectangle ( UI\Point $point , UI\Size $size )
public arcTo ( UI\Point $point , float $radius , float $angle , float $sweep , float $negative )
public bezierTo ( UI\Point $point , float $radius , float $angle , float $sweep , float $negative )
public closeFigure ( void )
public end ( void )
public lineTo ( UI\Point $point , float $radius , float $angle , float $sweep , float $negative )
public newFigure ( UI\Point $point )
public newFigureWithArc ( UI\Point $point , float $radius , float $angle , float $sweep , float $negative )
}

예약 상수

UI\Draw\Path::Winding

This is the default draw path mode

UI\Draw\Path::Alternate

This is the alternate draw path mode

Table of Contents

  • UI\Draw\Path::addRectangle — Draw a Rectangle
  • UI\Draw\Path::arcTo — Draw an Arc
  • UI\Draw\Path::bezierTo — Draw Bezier Curve
  • UI\Draw\Path::closeFigure — Close Figure
  • UI\Draw\Path::__construct — Construct a new Path
  • UI\Draw\Path::end — Finalize Path
  • UI\Draw\Path::lineTo — Draw a Line
  • UI\Draw\Path::newFigure — Draw Figure
  • UI\Draw\Path::newFigureWithArc — Draw Figure with Arc

User Contributed Notes

There are no user contributed notes for this page.


Draw a Rectangle

  • ≪ UI\Draw\Path
  • UI\Draw\Path::arcTo ≫
  • PHP Manual
  • UI\Draw\Path
  • Draw a Rectangle

UI\Draw\Path::addRectangle

(PHP 7, UI 0.9.9)

UI\Draw\Path::addRectangle — Draw a Rectangle

설명

public UI\Draw\Path::addRectangle ( UI\Point $point , UI\Size $size )

Shall map the path of a rectangle of the given size, at the given point

인수

point

The point to begin the shape

size

The size of the rectangle

User Contributed Notes

There are no user contributed notes for this page.


Draw an Arc

  • ≪ UI\Draw\Path::addRectangle
  • UI\Draw\Path::bezierTo ≫
  • PHP Manual
  • UI\Draw\Path
  • Draw an Arc

UI\Draw\Path::arcTo

(PHP 7, UI 0.9.9)

UI\Draw\Path::arcTo — Draw an Arc

설명

public UI\Draw\Path::arcTo ( UI\Point $point , float $radius , float $angle , float $sweep , float $negative )

Shall map the path for an arc

인수

point

The point to begin mapping

radius

The radius of the arc

angle

The angle of the arc

sweep

The sweep of the arc

negative

반환값

User Contributed Notes

There are no user contributed notes for this page.


Draw Bezier Curve

  • ≪ UI\Draw\Path::arcTo
  • UI\Draw\Path::closeFigure ≫
  • PHP Manual
  • UI\Draw\Path
  • Draw Bezier Curve

UI\Draw\Path::bezierTo

(PHP 7, UI 0.9.9)

UI\Draw\Path::bezierTo — Draw Bezier Curve

설명

public UI\Draw\Path::bezierTo ( UI\Point $point , float $radius , float $angle , float $sweep , float $negative )

Shall draw a bezier curve

인수

point

The point at which to begin mapping

radius

The radius of the curve

angle

The angle of the curve

sweep

The sweep of the curve

negative

User Contributed Notes

There are no user contributed notes for this page.


Close Figure

  • ≪ UI\Draw\Path::bezierTo
  • UI\Draw\Path::__construct ≫
  • PHP Manual
  • UI\Draw\Path
  • Close Figure

UI\Draw\Path::closeFigure

(PHP 7, UI 0.9.9)

UI\Draw\Path::closeFigure — Close Figure

설명

public UI\Draw\Path::closeFigure ( void )

Shall close the current figure

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Path

  • ≪ UI\Draw\Path::closeFigure
  • UI\Draw\Path::end ≫
  • PHP Manual
  • UI\Draw\Path
  • Construct a new Path

UI\Draw\Path::__construct

(PHP 7, UI 0.9.9)

UI\Draw\Path::__construct — Construct a new Path

설명

public UI\Draw\Path::__construct ([ integer $mode = UI\Draw\Path::Winding ] )

Shall construct a new path in the given mode

인수

mode

Path::Winding or Path::Alternate

User Contributed Notes

There are no user contributed notes for this page.


Finalize Path

  • ≪ UI\Draw\Path::__construct
  • UI\Draw\Path::lineTo ≫
  • PHP Manual
  • UI\Draw\Path
  • Finalize Path

UI\Draw\Path::end

(PHP 7, UI 0.9.9)

UI\Draw\Path::end — Finalize Path

설명

public UI\Draw\Path::end ( void )

Shall finalize this Path

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Draw a Line

  • ≪ UI\Draw\Path::end
  • UI\Draw\Path::newFigure ≫
  • PHP Manual
  • UI\Draw\Path
  • Draw a Line

UI\Draw\Path::lineTo

(PHP 7, UI 0.9.9)

UI\Draw\Path::lineTo — Draw a Line

설명

public UI\Draw\Path::lineTo ( UI\Point $point , float $radius , float $angle , float $sweep , float $negative )

Shall map the path for a line

인수

point

The point to begin mapping

radius

angle

sweep

negative

반환값

User Contributed Notes

There are no user contributed notes for this page.


Draw Figure

  • ≪ UI\Draw\Path::lineTo
  • UI\Draw\Path::newFigureWithArc ≫
  • PHP Manual
  • UI\Draw\Path
  • Draw Figure

UI\Draw\Path::newFigure

(PHP 7, UI 0.9.9)

UI\Draw\Path::newFigure — Draw Figure

설명

public UI\Draw\Path::newFigure ( UI\Point $point )

Shall map a new figure at the given point

인수

point

The point to begin mapping

User Contributed Notes

There are no user contributed notes for this page.


Draw Figure with Arc

  • ≪ UI\Draw\Path::newFigure
  • UI\Draw\Matrix ≫
  • PHP Manual
  • UI\Draw\Path
  • Draw Figure with Arc

UI\Draw\Path::newFigureWithArc

(PHP 7, UI 0.9.9)

UI\Draw\Path::newFigureWithArc — Draw Figure with Arc

설명

public UI\Draw\Path::newFigureWithArc ( UI\Point $point , float $radius , float $angle , float $sweep , float $negative )

인수

point

radius

angle

sweep

negative

반환값

User Contributed Notes

There are no user contributed notes for this page.


Draw Matrix

(PHP 7, UI 0.9.9)

소개

클래스 개요

UI\Draw\Matrix {
/* 메소드 */
public invert ( void )
public bool isInvertible ( void )
public UI\Draw\Matrix multiply ( UI\Draw\Matrix $matrix )
public rotate ( UI\Point $point , float $amount )
public scale ( UI\Point $center , UI\Point $point )
public skew ( UI\Point $point , UI\Point $amount )
public translate ( UI\Point $point )
}

Table of Contents

  • UI\Draw\Matrix::invert — Invert Matrix
  • UI\Draw\Matrix::isInvertible — Invertible Detection
  • UI\Draw\Matrix::multiply — Multiply Matrix
  • UI\Draw\Matrix::rotate — Rotate Matrix
  • UI\Draw\Matrix::scale — Scale Matrix
  • UI\Draw\Matrix::skew — Skew Matrix
  • UI\Draw\Matrix::translate — Translate Matrix

User Contributed Notes

There are no user contributed notes for this page.


Invert Matrix

  • ≪ UI\Draw\Matrix
  • UI\Draw\Matrix::isInvertible ≫
  • PHP Manual
  • UI\Draw\Matrix
  • Invert Matrix

UI\Draw\Matrix::invert

(PHP 7, UI 0.9.9)

UI\Draw\Matrix::invert — Invert Matrix

설명

public UI\Draw\Matrix::invert ( void )

Shall invert this matrix

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Invertible Detection

  • ≪ UI\Draw\Matrix::invert
  • UI\Draw\Matrix::multiply ≫
  • PHP Manual
  • UI\Draw\Matrix
  • Invertible Detection

UI\Draw\Matrix::isInvertible

(PHP 7, UI 0.9.9)

UI\Draw\Matrix::isInvertible — Invertible Detection

설명

public bool UI\Draw\Matrix::isInvertible ( void )

Shall detect if this Matrix may be inverted

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Multiply Matrix

  • ≪ UI\Draw\Matrix::isInvertible
  • UI\Draw\Matrix::rotate ≫
  • PHP Manual
  • UI\Draw\Matrix
  • Multiply Matrix

UI\Draw\Matrix::multiply

(PHP 7, UI 0.9.9)

UI\Draw\Matrix::multiply — Multiply Matrix

설명

public UI\Draw\Matrix UI\Draw\Matrix::multiply ( UI\Draw\Matrix $matrix )

Shall multiply this matrix with the given matrix

인수

이 함수는 인수가 없습니다.

반환값

The new Matrix

User Contributed Notes

There are no user contributed notes for this page.


Rotate Matrix

  • ≪ UI\Draw\Matrix::multiply
  • UI\Draw\Matrix::scale ≫
  • PHP Manual
  • UI\Draw\Matrix
  • Rotate Matrix

UI\Draw\Matrix::rotate

(PHP 7, UI 0.9.9)

UI\Draw\Matrix::rotate — Rotate Matrix

설명

public UI\Draw\Matrix::rotate ( UI\Point $point , float $amount )

Shall rotate this Matrix

인수

point

amount

User Contributed Notes

There are no user contributed notes for this page.


Scale Matrix

  • ≪ UI\Draw\Matrix::rotate
  • UI\Draw\Matrix::skew ≫
  • PHP Manual
  • UI\Draw\Matrix
  • Scale Matrix

UI\Draw\Matrix::scale

(PHP 7, UI 0.9.9)

UI\Draw\Matrix::scale — Scale Matrix

설명

public UI\Draw\Matrix::scale ( UI\Point $center , UI\Point $point )

Shall scale this Matrix

인수

center

point

User Contributed Notes

There are no user contributed notes for this page.


Skew Matrix

  • ≪ UI\Draw\Matrix::scale
  • UI\Draw\Matrix::translate ≫
  • PHP Manual
  • UI\Draw\Matrix
  • Skew Matrix

UI\Draw\Matrix::skew

(PHP 7, UI 0.9.9)

UI\Draw\Matrix::skew — Skew Matrix

설명

public UI\Draw\Matrix::skew ( UI\Point $point , UI\Point $amount )

Shall skew this Matrix

인수

point

amount

User Contributed Notes

There are no user contributed notes for this page.


Translate Matrix

  • ≪ UI\Draw\Matrix::skew
  • UI\Draw\Color ≫
  • PHP Manual
  • UI\Draw\Matrix
  • Translate Matrix

UI\Draw\Matrix::translate

(PHP 7, UI 0.9.9)

UI\Draw\Matrix::translate — Translate Matrix

설명

public UI\Draw\Matrix::translate ( UI\Point $point )

Shall translate this Matrix

인수

point

User Contributed Notes

There are no user contributed notes for this page.


Color Representation

(PHP 7, UI 0.9.9)

소개

Represents RGBA colours, individual channels are accessible via public properties.

클래스 개요

UI\Draw\Color {
/* Constants */
const integer Red ;
const integer Green ;
const integer Blue ;
const integer Alpha ;
/* 프로퍼티 */
public $r ;
public $g ;
public $b ;
public $a ;
/* Constructor */
public __construct ([ UI\Draw\Color $color ] )
public __construct ([ integer $color ] )
/* 메소드 */
public float getChannel ( integer $channel )
public void setChannel ( integer $channel , float $value )
}

프로퍼티

r

Provides access to the red channel

g

Provides access to the green channel

b

Provides access to the blue channel

a

Provides access to the alpha channel

예약 상수

UI\Draw\Color::Red

Identifies the red channel

UI\Draw\Color::Green

Identifies the green channel

UI\Draw\Color::Blue

Identifies the blue channel

UI\Draw\Color::Alpha

Identifies the alpha channel

Table of Contents

  • UI\Draw\Color::__construct — Construct new Color
  • UI\Draw\Color::getChannel — Color Manipulation
  • UI\Draw\Color::setChannel — Color Manipulation

User Contributed Notes

There are no user contributed notes for this page.


Construct new Color

  • ≪ UI\Draw\Color
  • UI\Draw\Color::getChannel ≫
  • PHP Manual
  • UI\Draw\Color
  • Construct new Color

UI\Draw\Color::__construct

(PHP 7, UI 0.9.9)

UI\Draw\Color::__construct — Construct new Color

설명

public UI\Draw\Color::__construct ([ UI\Draw\Color $color ] )
public UI\Draw\Color::__construct ([ integer $color ] )

Shall construct a new color

인수

color

May be UI\Draw\Color or RRGGBBAA

User Contributed Notes

There are no user contributed notes for this page.


Color Manipulation

  • ≪ UI\Draw\Color::__construct
  • UI\Draw\Color::setChannel ≫
  • PHP Manual
  • UI\Draw\Color
  • Color Manipulation

UI\Draw\Color::getChannel

(PHP 7, UI 0.9.9)

UI\Draw\Color::getChannel — Color Manipulation

설명

public float UI\Draw\Color::getChannel ( integer $channel )

Shall retrieve the value for a channel

인수

channel

Constant channel identity

반환값

The current value of the requested channel

User Contributed Notes

There are no user contributed notes for this page.


Color Manipulation

  • ≪ UI\Draw\Color::getChannel
  • UI\Draw\Stroke ≫
  • PHP Manual
  • UI\Draw\Color
  • Color Manipulation

UI\Draw\Color::setChannel

(PHP 7, UI 0.9.9)

UI\Draw\Color::setChannel — Color Manipulation

설명

public void UI\Draw\Color::setChannel ( integer $channel , float $value )

Shall set the selected channel to the given value

인수

channel

Constant channel identity

value

The new value for the selected channel

반환값

User Contributed Notes

There are no user contributed notes for this page.


Draw Stroke

(PHP 7, UI 0.9.9)

소개

Holds the configuration for the Pen to perform a stroke

클래스 개요

UI\Draw\Stroke {
/* Constructor */
public __construct ([ integer $cap = UI\Draw\Line\Cap::Flat [, integer $join = UI\Draw\Line\Join::Miter [, float $thickness = 1 [, float $miterLimit = 10 ]]]] )
/* 메소드 */
public integer getCap ( void )
public integer getJoin ( void )
public float getMiterLimit ( void )
public float getThickness ( void )
public setCap ( integer $cap )
public setJoin ( integer $join )
public setMiterLimit ( float $limit )
public setThickness ( float $thickness )
}

Table of Contents

  • UI\Draw\Stroke::__construct — Construct a new Stroke
  • UI\Draw\Stroke::getCap — Get Line Cap
  • UI\Draw\Stroke::getJoin — Get Line Join
  • UI\Draw\Stroke::getMiterLimit — Get Miter Limit
  • UI\Draw\Stroke::getThickness — Get Thickness
  • UI\Draw\Stroke::setCap — Set Line Cap
  • UI\Draw\Stroke::setJoin — Set Line Join
  • UI\Draw\Stroke::setMiterLimit — Set Miter Limit
  • UI\Draw\Stroke::setThickness — Set Thickness

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Stroke

  • ≪ UI\Draw\Stroke
  • UI\Draw\Stroke::getCap ≫
  • PHP Manual
  • UI\Draw\Stroke
  • Construct a new Stroke

UI\Draw\Stroke::__construct

(PHP 7, UI 0.9.9)

UI\Draw\Stroke::__construct — Construct a new Stroke

설명

public UI\Draw\Stroke::__construct ([ integer $cap = UI\Draw\Line\Cap::Flat [, integer $join = UI\Draw\Line\Join::Miter [, float $thickness = 1 [, float $miterLimit = 10 ]]]] )

Shall construct a new Stroke

인수

cap

UI\Draw\Line\Cap::Flat, UI\Draw\Line\Cap::Round, or UI\Draw\Line\Cap::Square

join

UI\Draw\Line\Join::Miter, UI\Draw\Line\Join::Round, or UI\Draw\Line\Join::Bevel

thickness

The thickness of the stroke

miterLimit

Miter limit (default is sensible for all supported platforms)

User Contributed Notes

There are no user contributed notes for this page.


Get Line Cap

  • ≪ UI\Draw\Stroke::__construct
  • UI\Draw\Stroke::getJoin ≫
  • PHP Manual
  • UI\Draw\Stroke
  • Get Line Cap

UI\Draw\Stroke::getCap

(PHP 7, UI 0.9.9)

UI\Draw\Stroke::getCap — Get Line Cap

설명

public integer UI\Draw\Stroke::getCap ( void )

Shall retrieve the line cap setting of this Stroke

인수

이 함수는 인수가 없습니다.

반환값

UI\Draw\Line\Cap::Flat, UI\Draw\Line\Cap::Round, or UI\Draw\Line\Cap::Square

User Contributed Notes

There are no user contributed notes for this page.


Get Line Join

  • ≪ UI\Draw\Stroke::getCap
  • UI\Draw\Stroke::getMiterLimit ≫
  • PHP Manual
  • UI\Draw\Stroke
  • Get Line Join

UI\Draw\Stroke::getJoin

(PHP 7, UI 0.9.9)

UI\Draw\Stroke::getJoin — Get Line Join

설명

public integer UI\Draw\Stroke::getJoin ( void )

Shall retrieve the line join setting of this Stroke

인수

이 함수는 인수가 없습니다.

반환값

UI\Draw\Line\Join::Miter, UI\Draw\Line\Join::Round, or UI\Draw\Line\Join::Bevel

User Contributed Notes

There are no user contributed notes for this page.


Get Miter Limit

  • ≪ UI\Draw\Stroke::getJoin
  • UI\Draw\Stroke::getThickness ≫
  • PHP Manual
  • UI\Draw\Stroke
  • Get Miter Limit

UI\Draw\Stroke::getMiterLimit

(PHP 7, UI 0.9.9)

UI\Draw\Stroke::getMiterLimit — Get Miter Limit

설명

public float UI\Draw\Stroke::getMiterLimit ( void )

Shall retrieve the miter limit of this Stroke

인수

이 함수는 인수가 없습니다.

반환값

The current miter limit

User Contributed Notes

There are no user contributed notes for this page.


Get Thickness

  • ≪ UI\Draw\Stroke::getMiterLimit
  • UI\Draw\Stroke::setCap ≫
  • PHP Manual
  • UI\Draw\Stroke
  • Get Thickness

UI\Draw\Stroke::getThickness

(PHP 7, UI 0.9.9)

UI\Draw\Stroke::getThickness — Get Thickness

설명

public float UI\Draw\Stroke::getThickness ( void )

Shall retrieve the thickness of this Stroke

인수

이 함수는 인수가 없습니다.

반환값

The current thickness

User Contributed Notes

There are no user contributed notes for this page.


Set Line Cap

  • ≪ UI\Draw\Stroke::getThickness
  • UI\Draw\Stroke::setJoin ≫
  • PHP Manual
  • UI\Draw\Stroke
  • Set Line Cap

UI\Draw\Stroke::setCap

(PHP 7, UI 0.9.9)

UI\Draw\Stroke::setCap — Set Line Cap

설명

public UI\Draw\Stroke::setCap ( integer $cap )

Shall set the line cap setting for this Stroke

인수

cap

UI\Draw\Line\Cap::Flat, UI\Draw\Line\Cap::Round, or UI\Draw\Line\Cap::Square

User Contributed Notes

There are no user contributed notes for this page.


Set Line Join

  • ≪ UI\Draw\Stroke::setCap
  • UI\Draw\Stroke::setMiterLimit ≫
  • PHP Manual
  • UI\Draw\Stroke
  • Set Line Join

UI\Draw\Stroke::setJoin

(PHP 7, UI 0.9.9)

UI\Draw\Stroke::setJoin — Set Line Join

설명

public UI\Draw\Stroke::setJoin ( integer $join )

Shall set the line join setting for this Stroke

인수

join

UI\Draw\Line\Join::Miter, UI\Draw\Line\Join::Round, or UI\Draw\Line\Join::Bevel

반환값

User Contributed Notes

There are no user contributed notes for this page.


Set Miter Limit

  • ≪ UI\Draw\Stroke::setJoin
  • UI\Draw\Stroke::setThickness ≫
  • PHP Manual
  • UI\Draw\Stroke
  • Set Miter Limit

UI\Draw\Stroke::setMiterLimit

(PHP 7, UI 0.9.9)

UI\Draw\Stroke::setMiterLimit — Set Miter Limit

설명

public UI\Draw\Stroke::setMiterLimit ( float $limit )

Set the miter limit for this Stroke

인수

limit

The new limit

User Contributed Notes

There are no user contributed notes for this page.


Set Thickness

  • ≪ UI\Draw\Stroke::setMiterLimit
  • UI\Draw\Brush ≫
  • PHP Manual
  • UI\Draw\Stroke
  • Set Thickness

UI\Draw\Stroke::setThickness

(PHP 7, UI 0.9.9)

UI\Draw\Stroke::setThickness — Set Thickness

설명

public UI\Draw\Stroke::setThickness ( float $thickness )

Shall set the thickness for this Stroke

인수

thickness

The new thickness

User Contributed Notes

There are no user contributed notes for this page.


Brushes

(PHP 7, UI 0.9.9)

소개

Represents a solid color brush

클래스 개요

UI\Draw\Brush {
/* Constructor */
public __construct ( UI\Draw\Color $color )
public __construct ( integer $color )
/* 메소드 */
public UI\Draw\Color getColor ( void )
public void setColor ( UI\Draw\Color $color )
public void setColor ( integer $color )
}

Table of Contents

  • UI\Draw\Brush::__construct — Construct a new Brush
  • UI\Draw\Brush::getColor — Get Color
  • UI\Draw\Brush::setColor — Set Color

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Brush

  • ≪ UI\Draw\Brush
  • UI\Draw\Brush::getColor ≫
  • PHP Manual
  • UI\Draw\Brush
  • Construct a new Brush

UI\Draw\Brush::__construct

(PHP 7, UI 0.9.9)

UI\Draw\Brush::__construct — Construct a new Brush

설명

public UI\Draw\Brush::__construct ( UI\Draw\Color $color )
public UI\Draw\Brush::__construct ( integer $color )

Shall construct a solid brush using the given color

인수

color

Can be a UI\Draw\Color or RRGGBBAA

User Contributed Notes

There are no user contributed notes for this page.


Get Color

  • ≪ UI\Draw\Brush::__construct
  • UI\Draw\Brush::setColor ≫
  • PHP Manual
  • UI\Draw\Brush
  • Get Color

UI\Draw\Brush::getColor

(PHP 7, UI 0.9.9)

UI\Draw\Brush::getColor — Get Color

설명

public UI\Draw\Color UI\Draw\Brush::getColor ( void )

Shall return a UI\Draw\Color for this brush

인수

이 함수는 인수가 없습니다.

반환값

The current color of the brush

User Contributed Notes

There are no user contributed notes for this page.


Set Color

  • ≪ UI\Draw\Brush::getColor
  • UI\Draw\Brush\Gradient ≫
  • PHP Manual
  • UI\Draw\Brush
  • Set Color

UI\Draw\Brush::setColor

(PHP 7, UI 0.9.9)

UI\Draw\Brush::setColor — Set Color

설명

public void UI\Draw\Brush::setColor ( UI\Draw\Color $color )
public void UI\Draw\Brush::setColor ( integer $color )

Shall set the color of this brush to the color provided

인수

color

Can be a UI\Draw\Color or RRGGBBAA

반환값

User Contributed Notes

There are no user contributed notes for this page.


Gradient Brushes

(PHP 7, UI 2.0.0)

소개

Abstract for gradient brushes

클래스 개요

abstract UI\Draw\Brush\Gradient extends UI\Draw\Brush {
/* 메소드 */
public integer addStop ( float $position , UI\Draw\Color $color )
public integer addStop ( float $position , integer $color )
public integer delStop ( integer $index )
public boolean setStop ( integer $index , float $position , UI\Draw\Color $color )
public boolean setStop ( integer $index , float $position , integer $color )
/* 상속된 메소드 */
public UI\Draw\Color UI\Draw\Brush::getColor ( void )
public void UI\Draw\Brush::setColor ( UI\Draw\Color $color )
public void UI\Draw\Brush::setColor ( integer $color )
}

Table of Contents

  • UI\Draw\Brush\Gradient::addStop — Stop Manipulation
  • UI\Draw\Brush\Gradient::delStop — Stop Manipulation
  • UI\Draw\Brush\Gradient::setStop — Stop Manipulation

User Contributed Notes

There are no user contributed notes for this page.


Stop Manipulation

  • ≪ UI\Draw\Brush\Gradient
  • UI\Draw\Brush\Gradient::delStop ≫
  • PHP Manual
  • UI\Draw\Brush\Gradient
  • Stop Manipulation

UI\Draw\Brush\Gradient::addStop

(PHP 7, UI 2.0.0)

UI\Draw\Brush\Gradient::addStop — Stop Manipulation

설명

public integer UI\Draw\Brush\Gradient::addStop ( float $position , UI\Draw\Color $color )
public integer UI\Draw\Brush\Gradient::addStop ( float $position , integer $color )

Shall at a stop of the given color at the given position

인수

position

The position for the new stop

color

The color for the new stop, may be UI\Draw\Color or RRGGBBAA

반환값

Total number of stops

User Contributed Notes

There are no user contributed notes for this page.


Stop Manipulation

  • ≪ UI\Draw\Brush\Gradient::addStop
  • UI\Draw\Brush\Gradient::setStop ≫
  • PHP Manual
  • UI\Draw\Brush\Gradient
  • Stop Manipulation

UI\Draw\Brush\Gradient::delStop

(PHP 7, UI 2.0.0)

UI\Draw\Brush\Gradient::delStop — Stop Manipulation

설명

public integer UI\Draw\Brush\Gradient::delStop ( integer $index )

Warning

이 함수는 현재 문서화 되어있지 않습니다; 인수 목록만을 제공합니다.

인수

index

반환값

Total number of stops

User Contributed Notes

There are no user contributed notes for this page.


Stop Manipulation

  • ≪ UI\Draw\Brush\Gradient::delStop
  • UI\Draw\Brush\LinearGradient ≫
  • PHP Manual
  • UI\Draw\Brush\Gradient
  • Stop Manipulation

UI\Draw\Brush\Gradient::setStop

(PHP 7, UI 2.0.0)

UI\Draw\Brush\Gradient::setStop — Stop Manipulation

설명

public boolean UI\Draw\Brush\Gradient::setStop ( integer $index , float $position , UI\Draw\Color $color )
public boolean UI\Draw\Brush\Gradient::setStop ( integer $index , float $position , integer $color )

인수

index

The index of the stop to set

position

The position for the stop

color

The color for the stop, may be UI\Draw\Color or RRGGBBAA

반환값

Indication of success

User Contributed Notes

There are no user contributed notes for this page.


Linear Gradient

(PHP 7, UI 2.0.0)

소개

클래스 개요

UI\Draw\Brush\LinearGradient extends UI\Draw\Brush\Gradient {
/* Constructor */
public __construct ( UI\Point $start , UI\Point $end )
/* 상속된 메소드 */
public integer UI\Draw\Brush\Gradient::addStop ( float $position , UI\Draw\Color $color )
public integer UI\Draw\Brush\Gradient::addStop ( float $position , integer $color )
public integer UI\Draw\Brush\Gradient::delStop ( integer $index )
public boolean UI\Draw\Brush\Gradient::setStop ( integer $index , float $position , UI\Draw\Color $color )
public boolean UI\Draw\Brush\Gradient::setStop ( integer $index , float $position , integer $color )
}

Table of Contents

  • UI\Draw\Brush\LinearGradient::__construct — Construct a Linear Gradient

User Contributed Notes

There are no user contributed notes for this page.


Construct a Linear Gradient

  • ≪ UI\Draw\Brush\LinearGradient
  • UI\Draw\Brush\RadialGradient ≫
  • PHP Manual
  • UI\Draw\Brush\LinearGradient
  • Construct a Linear Gradient

UI\Draw\Brush\LinearGradient::__construct

(PHP 7, UI 2.0.0)

UI\Draw\Brush\LinearGradient::__construct — Construct a Linear Gradient

설명

public UI\Draw\Brush\LinearGradient::__construct ( UI\Point $start , UI\Point $end )

Shall construct a new linear gradient

인수

start

end

User Contributed Notes

There are no user contributed notes for this page.


Radial Gradient

(PHP 7, UI 2.0.0)

소개

클래스 개요

UI\Draw\Brush\RadialGradient extends UI\Draw\Brush\Gradient {
/* Constructor */
public __construct ( UI\Point $start , UI\Point $outer , float $radius )
/* 상속된 메소드 */
public integer UI\Draw\Brush\Gradient::addStop ( float $position , UI\Draw\Color $color )
public integer UI\Draw\Brush\Gradient::addStop ( float $position , integer $color )
public integer UI\Draw\Brush\Gradient::delStop ( integer $index )
public boolean UI\Draw\Brush\Gradient::setStop ( integer $index , float $position , UI\Draw\Color $color )
public boolean UI\Draw\Brush\Gradient::setStop ( integer $index , float $position , integer $color )
}

Table of Contents

  • UI\Draw\Brush\RadialGradient::__construct — Construct a new Radial Gradient

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Radial Gradient

  • ≪ UI\Draw\Brush\RadialGradient
  • UI\Draw\Text\Layout ≫
  • PHP Manual
  • UI\Draw\Brush\RadialGradient
  • Construct a new Radial Gradient

UI\Draw\Brush\RadialGradient::__construct

(PHP 7, UI 2.0.0)

UI\Draw\Brush\RadialGradient::__construct — Construct a new Radial Gradient

설명

public UI\Draw\Brush\RadialGradient::__construct ( UI\Point $start , UI\Point $outer , float $radius )

Shall construct a new radial gradient

인수

start

outer

radius

User Contributed Notes

There are no user contributed notes for this page.


Represents Text Layout

(PHP 7, UI 0.9.9)

소개

A Text Layout represents the layout of text which will be drawn by the Pen

클래스 개요

UI\Draw\Text\Layout {
/* Constructor */
public __construct ( string $text , UI\Draw\Text\Font $font , float $width )
/* 메소드 */
public setColor ( UI\Draw\Color $color [, integer $start = 0 [, integer $end ]] )
public setColor ( integer $color [, integer $start = 0 [, integer $end ]] )
public setWidth ( float $width )
}

Table of Contents

  • UI\Draw\Text\Layout::__construct — Construct a new Text Layout
  • UI\Draw\Text\Layout::setColor — Set Color
  • UI\Draw\Text\Layout::setWidth — Set Width

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Text Layout

  • ≪ UI\Draw\Text\Layout
  • UI\Draw\Text\Layout::setColor ≫
  • PHP Manual
  • UI\Draw\Text\Layout
  • Construct a new Text Layout

UI\Draw\Text\Layout::__construct

(PHP 7, UI 0.9.9)

UI\Draw\Text\Layout::__construct — Construct a new Text Layout

설명

public UI\Draw\Text\Layout::__construct ( string $text , UI\Draw\Text\Font $font , float $width )

Shall construct a new Text Layout

인수

text

The text for the layout

font

The font to use for writing

width

The width of the layout

User Contributed Notes

There are no user contributed notes for this page.


Set Color

  • ≪ UI\Draw\Text\Layout::__construct
  • UI\Draw\Text\Layout::setWidth ≫
  • PHP Manual
  • UI\Draw\Text\Layout
  • Set Color

UI\Draw\Text\Layout::setColor

(PHP 7, UI 0.9.9)

UI\Draw\Text\Layout::setColor — Set Color

설명

public UI\Draw\Text\Layout::setColor ( UI\Draw\Color $color [, integer $start = 0 [, integer $end ]] )
public UI\Draw\Text\Layout::setColor ( integer $color [, integer $start = 0 [, integer $end ]] )

Shall set the Color for all of, or a range of the text in the Layout

인수

color

The color to use

start

The starting character

end

The ending character, by default the end of the string

User Contributed Notes

There are no user contributed notes for this page.


Set Width

  • ≪ UI\Draw\Text\Layout::setColor
  • UI\Draw\Text\Font ≫
  • PHP Manual
  • UI\Draw\Text\Layout
  • Set Width

UI\Draw\Text\Layout::setWidth

(PHP 7, UI 0.9.9)

UI\Draw\Text\Layout::setWidth — Set Width

설명

public UI\Draw\Text\Layout::setWidth ( float $width )

Shall set the width of this Text Layout

인수

width

The new width

User Contributed Notes

There are no user contributed notes for this page.


Represents a Font

(PHP 7, UI 0.9.9)

소개

Loads a described font

클래스 개요

UI\Draw\Text\Font {
/* Constructor */
public __construct ( UI\Draw\Text\Font\Descriptor $descriptor )
/* 메소드 */
public float getAscent ( void )
public float getDescent ( void )
public float getLeading ( void )
public float getUnderlinePosition ( void )
public float getUnderlineThickness ( void )
}

Table of Contents

  • UI\Draw\Text\Font::__construct — Construct a new Font
  • UI\Draw\Text\Font::getAscent — Font Metrics
  • UI\Draw\Text\Font::getDescent — Font Metrics
  • UI\Draw\Text\Font::getLeading — Font Metrics
  • UI\Draw\Text\Font::getUnderlinePosition — Font Metrics
  • UI\Draw\Text\Font::getUnderlineThickness — Font Metrics

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Font

  • ≪ UI\Draw\Text\Font
  • UI\Draw\Text\Font::getAscent ≫
  • PHP Manual
  • UI\Draw\Text\Font
  • Construct a new Font

UI\Draw\Text\Font::__construct

(PHP 7, UI 0.9.9)

UI\Draw\Text\Font::__construct — Construct a new Font

설명

public UI\Draw\Text\Font::__construct ( UI\Draw\Text\Font\Descriptor $descriptor )

Shall construct a new Font using the given descriptor

인수

descriptor

The descriptor for the Font

User Contributed Notes

There are no user contributed notes for this page.


Font Metrics

  • ≪ UI\Draw\Text\Font::__construct
  • UI\Draw\Text\Font::getDescent ≫
  • PHP Manual
  • UI\Draw\Text\Font
  • Font Metrics

UI\Draw\Text\Font::getAscent

(PHP 7, UI 1.0.3)

UI\Draw\Text\Font::getAscent — Font Metrics

설명

public float UI\Draw\Text\Font::getAscent ( void )

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Font Metrics

  • ≪ UI\Draw\Text\Font::getAscent
  • UI\Draw\Text\Font::getLeading ≫
  • PHP Manual
  • UI\Draw\Text\Font
  • Font Metrics

UI\Draw\Text\Font::getDescent

(PHP 7, UI 1.0.3)

UI\Draw\Text\Font::getDescent — Font Metrics

설명

public float UI\Draw\Text\Font::getDescent ( void )

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Font Metrics

  • ≪ UI\Draw\Text\Font::getDescent
  • UI\Draw\Text\Font::getUnderlinePosition ≫
  • PHP Manual
  • UI\Draw\Text\Font
  • Font Metrics

UI\Draw\Text\Font::getLeading

(PHP 7, UI 1.0.3)

UI\Draw\Text\Font::getLeading — Font Metrics

설명

public float UI\Draw\Text\Font::getLeading ( void )

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Font Metrics

  • ≪ UI\Draw\Text\Font::getLeading
  • UI\Draw\Text\Font::getUnderlineThickness ≫
  • PHP Manual
  • UI\Draw\Text\Font
  • Font Metrics

UI\Draw\Text\Font::getUnderlinePosition

(PHP 7, UI 1.0.3)

UI\Draw\Text\Font::getUnderlinePosition — Font Metrics

설명

public float UI\Draw\Text\Font::getUnderlinePosition ( void )

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Font Metrics

  • ≪ UI\Draw\Text\Font::getUnderlinePosition
  • UI\Draw\Text\Font\Descriptor ≫
  • PHP Manual
  • UI\Draw\Text\Font
  • Font Metrics

UI\Draw\Text\Font::getUnderlineThickness

(PHP 7, UI 1.0.3)

UI\Draw\Text\Font::getUnderlineThickness — Font Metrics

설명

public float UI\Draw\Text\Font::getUnderlineThickness ( void )

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Font Descriptor

(PHP 7, UI 0.9.9)

소개

Describes a font

클래스 개요

UI\Draw\Text\Font\Descriptor {
/* Constructor */
public __construct ( string $family , float $size [, integer $weight = UI\Draw\Text\Font\Weight::Normal [, integer $italic = UI\Draw\Text\Font\Italic::Normal [, integer $stretch = UI\Draw\Text\Font\Stretch::Normal ]]] )
/* 메소드 */
public string getFamily ( void )
public int getItalic ( void )
public float getSize ( void )
public int getStretch ( void )
public int getWeight ( void )
}

Table of Contents

  • UI\Draw\Text\Font\Descriptor::__construct — Construct a new Font Descriptor
  • UI\Draw\Text\Font\Descriptor::getFamily — Get Font Family
  • UI\Draw\Text\Font\Descriptor::getItalic — Style Detection
  • UI\Draw\Text\Font\Descriptor::getSize — Size Detection
  • UI\Draw\Text\Font\Descriptor::getStretch — Style Detection
  • UI\Draw\Text\Font\Descriptor::getWeight — Weight Detection

User Contributed Notes

There are no user contributed notes for this page.


Construct a new Font Descriptor

  • ≪ UI\Draw\Text\Font\Descriptor
  • UI\Draw\Text\Font\Descriptor::getFamily ≫
  • PHP Manual
  • UI\Draw\Text\Font\Descriptor
  • Construct a new Font Descriptor

UI\Draw\Text\Font\Descriptor::__construct

(PHP 7, UI 0.9.9)

UI\Draw\Text\Font\Descriptor::__construct — Construct a new Font Descriptor

설명

public UI\Draw\Text\Font\Descriptor::__construct ( string $family , float $size [, integer $weight = UI\Draw\Text\Font\Weight::Normal [, integer $italic = UI\Draw\Text\Font\Italic::Normal [, integer $stretch = UI\Draw\Text\Font\Stretch::Normal ]]] )

Shall construct a new Font Descriptor

인수

family

The name of a valid family of fonts

size

The preferred size

weight

UI\Draw\Text\Font\Weight constant

italic

UI\Draw\Text\Font\Italic constant

stretch

UI\Draw\Text\Font\Stretch constant

User Contributed Notes

There are no user contributed notes for this page.


Get Font Family

  • ≪ UI\Draw\Text\Font\Descriptor::__construct
  • UI\Draw\Text\Font\Descriptor::getItalic ≫
  • PHP Manual
  • UI\Draw\Text\Font\Descriptor
  • Get Font Family

UI\Draw\Text\Font\Descriptor::getFamily

(PHP 7, UI 1.0.3)

UI\Draw\Text\Font\Descriptor::getFamily — Get Font Family

설명

public string UI\Draw\Text\Font\Descriptor::getFamily ( void )

Shall return the requested font family

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Style Detection

  • ≪ UI\Draw\Text\Font\Descriptor::getFamily
  • UI\Draw\Text\Font\Descriptor::getSize ≫
  • PHP Manual
  • UI\Draw\Text\Font\Descriptor
  • Style Detection

UI\Draw\Text\Font\Descriptor::getItalic

(PHP 7, UI 1.0.3)

UI\Draw\Text\Font\Descriptor::getItalic — Style Detection

설명

public int UI\Draw\Text\Font\Descriptor::getItalic ( void )

Shall return constant setting

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Size Detection

  • ≪ UI\Draw\Text\Font\Descriptor::getItalic
  • UI\Draw\Text\Font\Descriptor::getStretch ≫
  • PHP Manual
  • UI\Draw\Text\Font\Descriptor
  • Size Detection

UI\Draw\Text\Font\Descriptor::getSize

(PHP 7, UI 1.0.3)

UI\Draw\Text\Font\Descriptor::getSize — Size Detection

설명

public float UI\Draw\Text\Font\Descriptor::getSize ( void )

Shall return the requested size

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Style Detection

  • ≪ UI\Draw\Text\Font\Descriptor::getSize
  • UI\Draw\Text\Font\Descriptor::getWeight ≫
  • PHP Manual
  • UI\Draw\Text\Font\Descriptor
  • Style Detection

UI\Draw\Text\Font\Descriptor::getStretch

(PHP 7, UI 1.0.3)

UI\Draw\Text\Font\Descriptor::getStretch — Style Detection

설명

public int UI\Draw\Text\Font\Descriptor::getStretch ( void )

Shall return requested stretch

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Weight Detection

  • ≪ UI\Draw\Text\Font\Descriptor::getStretch
  • UI 함수 목록 ≫
  • PHP Manual
  • UI\Draw\Text\Font\Descriptor
  • Weight Detection

UI\Draw\Text\Font\Descriptor::getWeight

(PHP 7, UI 1.0.3)

UI\Draw\Text\Font\Descriptor::getWeight — Weight Detection

설명

public int UI\Draw\Text\Font\Descriptor::getWeight ( void )

Shall return requested weight

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


UI 함수 목록

Table of Contents

  • UI\Draw\Text\Font\fontFamilies — Retrieve Font Families
  • UI\quit — Quit UI Loop
  • UI\run — Enter UI Loop

User Contributed Notes

There are no user contributed notes for this page.


Retrieve Font Families

  • ≪ UI 함수 목록
  • UI\quit ≫
  • PHP Manual
  • UI 함수 목록
  • Retrieve Font Families

UI\Draw\Text\Font\fontFamilies

(PHP 7, UI 0.9.9)

UI\Draw\Text\Font\fontFamilies — Retrieve Font Families

설명

array UI\Draw\Text\Font\fontFamilies ( void )

Returns an array of valid font families for the current system

인수

이 함수는 인수가 없습니다.

User Contributed Notes

There are no user contributed notes for this page.


Quit UI Loop

  • ≪ UI\Draw\Text\Font\fontFamilies
  • UI\run ≫
  • PHP Manual
  • UI 함수 목록
  • Quit UI Loop

UI\quit

(PHP 7, UI 2.0.0)

UI\quit — Quit UI Loop

설명

void UI\quit ( void )

Shall cause the main loop to be exited

인수

이 함수는 인수가 없습니다.

반환값

User Contributed Notes

There are no user contributed notes for this page.


Enter UI Loop

  • ≪ UI\quit
  • UI\Draw\Text\Font\Weight ≫
  • PHP Manual
  • UI 함수 목록
  • Enter UI Loop

UI\run

(PHP 7, UI 2.0.0)

UI\run — Enter UI Loop

설명

void UI\run ([ integer $flags ] )

Shall cause PHP to enter into the main loop, by default control will not be returned to the caller

인수

flags

Set UI\Loop to return control, and UI\Wait to return control after waiting

반환값

User Contributed Notes

There are no user contributed notes for this page.


Font Weight Settings

(PHP 7, UI 0.9.9)

소개

클래스 개요

final UI\Draw\Text\Font\Weight {
/* Constants */
const integer Thin ;
const integer UltraLight ;
const integer Light ;
const integer Book ;
const integer Normal ;
const integer Medium ;
const integer SemiBold ;
const integer Bold ;
const integer UltraBold ;
const integer Heavy ;
const integer UltraHeavy ;
}

User Contributed Notes

There are no user contributed notes for this page.


Italic Font Settings

(PHP 7, UI 0.9.9)

소개

클래스 개요

final UI\Draw\Text\Font\Italic {
/* Constants */
const integer Normal = 0 ;
const integer Oblique = 1 ;
const integer Italic = 2 ;
}

User Contributed Notes

There are no user contributed notes for this page.


Font Stretch Settings

(PHP 7, UI 0.9.9)

소개

클래스 개요

final UI\Draw\Text\Font\Stretch {
/* Constants */
const integer UltraCondensed = 0 ;
const integer ExtraCondensed = 1 ;
const integer Condensed = 2 ;
const integer SemiCondensed = 3 ;
const integer Normal = 4 ;
const integer SemiExpanded = 5 ;
const integer Expanded = 6 ;
const integer ExtraExpanded = 7 ;
const integer UltraExpanded = 8 ;
}

User Contributed Notes

There are no user contributed notes for this page.


Line Cap Settings

(PHP 7, UI 0.9.9)

소개

클래스 개요

final UI\Draw\Line\Cap {
/* Constants */
const integer Flat ;
const integer Round ;
const integer Square ;
}

User Contributed Notes

There are no user contributed notes for this page.


Line Join Settings

(PHP 7, UI 0.9.9)

소개

클래스 개요

final UI\Draw\Line\Join {
/* Constants */
const integer Miter ;
const integer Round ;
const integer Bevel ;
}

User Contributed Notes

There are no user contributed notes for this page.


Key Identifiers

(PHP 7, UI 1.0.0)

소개

클래스 개요

final UI\Key {
/* Constants */
const integer Escape ;
const integer Insert ;
const integer Delete ;
const integer Home ;
const integer End ;
const integer PageUp ;
const integer PageDown ;
const integer Up ;
const integer Down ;
const integer Left ;
const integer Right ;
const integer F1 ;
const integer F2 ;
const integer F3 ;
const integer F4 ;
const integer F5 ;
const integer F6 ;
const integer F7 ;
const integer F8 ;
const integer F9 ;
const integer F10 ;
const integer F11 ;
const integer F12 ;
const integer N0 ;
const integer N1 ;
const integer N2 ;
const integer N3 ;
const integer N4 ;
const integer N5 ;
const integer N6 ;
const integer N7 ;
const integer N8 ;
const integer N9 ;
const integer NDot ;
const integer NEnter ;
const integer NAdd ;
const integer NSubtract ;
const integer NMultiply ;
const integer NDivide ;
}

User Contributed Notes

There are no user contributed notes for this page.


InvalidArgumentException

(PHP 7, UI 1.0.3)

소개

클래스 개요

UI\Exception\InvalidArgumentException extends InvalidArgumentException implements Throwable {
/* 상속된 프로퍼티 */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* 상속된 메소드 */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}

User Contributed Notes

There are no user contributed notes for this page.


RuntimeException

(PHP 7, UI 1.0.3)

소개

클래스 개요

UI\Exception\RuntimeException extends RuntimeException implements Throwable {
/* 상속된 프로퍼티 */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* 상속된 메소드 */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )
}

User Contributed Notes

There are no user contributed notes for this page.


PHP 코어: Zend 엔진 해커 가이드

  • Preface
  • Memory management
    • Basic memory management
    • Data persistence
    • Thread-Safe Resource Manager
  • Working with Variables
    • Introduction to Variables
    • Working with Arrays
    • Working with HashTable
    • Working with Objects
  • Writing Functions
  • Writing Classes
  • Working with Resources
  • Working with INI settings
  • Working with streams
  • The "counter" Extension - A Continuing Example
    • 설치/설정
    • 예약 상수
    • 예제
    • Counter — The Counter class
    • Basic — The basic interface
    • Extended — The extended interface
  • The PHP 5 build system
    • Building PHP for extension development
    • The ext_skel script
    • Talking to the UNIX build system: config.m4
    • Talking to the Windows build system: config.w32
  • Extension structure
    • Files which make up an extension
    • Basic constructs
    • The zend_module structure
    • Extension globals
    • Life cycle of an extension
    • Testing an extension
  • PDO Driver How-To
    • Prerequisites
    • Preparation and Housekeeping
    • Fleshing out your skeleton
    • Building
    • Testing
    • Packaging and distribution
    • pdo_dbh_t definition
    • pdo_stmt_t definition
    • Constants
    • Error handling
  • Extension FAQs
  • Zend Engine 2 API reference
  • Zend Engine 2 Opcodes
    • Opcode Descriptions and Examples
  • Zend Engine 1
    • Old introduction
    • Streams API for PHP Extension Authors
    • Zend API: PHP 코어 해킹
    • TSRM API

User Contributed Notes

There are no user contributed notes for this page.


Preface

This section of the manual is written for the Hacker: someone thinking about getting their hands dirty, someone who wants an understanding of internals in order to advance their PHP skills, or maybe someone looking to write the next best extension. Whatever the reason, this section will seek to provide a good understanding of the internals of PHP, how to write extensions, how to understand existing code containing mystical macros. All of the important internal functionality is documented here, enough anyway to convince you to read the source.

Out of necessity, this section assumes the reader has a working knowledge of the C programming language, and associated tools, like compilers and terminal emulators and the like. You don't need to be the next Alan Turing, but a working knowledge is none the less essential for this section to be of any use. With just that working knowledge, this section should be enough to get you well on the way to earning the title Hacker, and you do have to earn it.

The documentation in this section is current as of PHP 5.3.3, deviations in API functionality are duly noted in the appropriate sections.

Warning

This documentation is still under development. The original Zend documentation is preserved in its entirety in the Zend Engine 1 section for those who need it before this documentation is completed.

User Contributed Notes

There are no user contributed notes for this page.


Memory management

Table of Contents

  • Basic memory management
  • Data persistence
  • Thread-Safe Resource Manager

Memory management in the Zend Engine is deceptively simple. There are some APIs to learn, and some concepts behind them to understand, but that's about all.

User Contributed Notes

There are no user contributed notes for this page.


Basic memory management

The engine's memory management is implemented with features important to a system like PHP. The exact functionality of the engine's memory management and the optimizations performed are out of the scope of this document. However, a good understanding of its functionality provides a basis for a good understanding of the rest of the Hacker's Guide, and introduce you to terminology and functionality used throughout PHP.

The most important of its features for the Hacker, and the first thing to mention is tracking allocations. Tracking allocations allow the memory manager to avoid leaks, a thorn in the side of most Hackers. When PHP is built in debug mode (--enable-debug), detected leaks are reported, in a perfect world they would never get to deployment.

While tracking allocations is an important, and highly useful feature, the Hacker should not become lazy! Always attempt to resolve leaks before deploying your code, a memory leak in a SAPI environment can become a very big problem, very quickly.

Another, perhaps more incidental but still noteworthy, feature is that the memory manager is the part that allows a hard limit on memory usage for each instance of PHP. As we all know, there is no such thing as unlimited. If some code is running out of memory, it is likely to be written wrong, either by the Hacker, or the programmer of PHP. Limiting the memory therefore is not a restriction on the language that is supposed to be experienced in production, it is simply a way from stopping development environments from spiraling out of control when mistakes are made, and equally, when bugs are found in production.

From the Hacker's perspective, the memory management API looks very much like libc's (or whoever the Hacker prefers !) malloc implementation.

Main memory APIs
Prototype | Description
void *emalloc(size_t size) | Allocate size bytes of memory.
void *ecalloc(size_t nmemb, size_t size) | Allocate a buffer for nmemb elements of size bytes and makes sure it is initialized with zeros.
void *erealloc(void *ptr, size_t size) | Resize the buffer ptr, which was allocated using emalloc to hold size bytes of memory.
void efree(void *ptr) | Free the buffer pointed by ptr. The buffer had to be allocated by emalloc.
void *safe_emalloc(size_t nmemb, size_t size, size_t offset) | Allocate a buffer for holding nmemb blocks of each size bytes and an additional offset bytes. This is similar to emalloc(nmemb * size + offset) but adds a special protection against overflows.
char *estrdup(const char *s) | Allocate a buffer that can hold the NULL-terminated string s and copy the s into that buffer.
char *estrndup(const char *s, unsigned int length) | Similar to estrdup while the length of the NULL-terminated string is already known.

Note: The engines memory management functions do not return NULL upon failure, if memory cannot be allocated at runtime, the engine bails and raises an error.

Caution

Always use valgrind before deploying code and as a normal part of the Hacker's process. The engine can only report and detect leaks where it has allocated the memory. All of PHP is only a thin wrapper around third parties, those third parties do not use the engines memory management. Additionally, valgrind will catch errors that do not always halt or even have an apparent effect at execution time, it is just as important that there should be no errors, as it is important that avoidable leaks should be avoided.

Note: Some leaks are unavoidable, some libraries rely on the end of a process to free some of their structures, this is normal under some circumstances and acceptable where it is out of the Hacker's control.

While executing in a debug environment, configured with --enable-debug, the leak function used in the next example is actually implemented by the engine and is available to call in userland.

Example #1 Leak Detection in Action

ZEND_FUNCTION(leak)
{
    long leakbytes = 3;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &leakbytes) == FAILURE) {
        return;
    }

    emalloc(leakbytes);
}

위 예제의 출력 예시:

[Thu Oct 22 02:14:57 2009]  Script:  '-'
/home/johannes/src/PHP_5_3/Zend/zend_builtin_functions.c(1377) :  Freeing 0x088888D4 (3 bytes), script=-
=== Total 1 memory leaks detected ===

Note: USE_ZEND_ALLOC=0 in the environment will stop the memory manager from functioning, all allocations fall back on the default system allocators which can be useful for debugging leaks, and should only be used for this purpose.

User Contributed Notes

There are no user contributed notes for this page.


Data persistence

In this context, data persistence is taken to mean any data that is intended to survive the current request. The memory management within the engine is very focused on request bound allocations, but this is not always practical or appropriate. Persistent memory is sometimes required in order to satisfy requirements of external libraries, it can also be useful while Hacking.

A common use of persistent memory is to enable persistent SQL server connections, though this practice is frowned upon, it is none the less the most common use of this feature.

Note: All of the following functions take the additional persistent parameter, should this be false, the engine will use its regular allocators (emalloc) and the memory should not be considered persistent. Where memory is allocated as persistent, system allocators are invoked, under most circumstances they are still not able to return NULL pointers just as the Main memory APIs.

Persistent memory APIs
Prototype | Description
void *pemalloc(size_t size, zend_bool persistent) | Allocate size bytes of memory.
void *pecalloc(size_t nmemb, size_t size, zend_bool persistent) | Allocate a buffer for nmemb elements of size bytes and makes sure it is initialized with zeros.
void *perealloc(void *ptr, size_t size, zend_bool persistent) | Resize the buffer ptr, which was allocated using emalloc to hold size bytes of memory.
void pefree(void *ptr, zend_bool persistent) | Free the buffer pointed by ptr. The buffer had to be allocated by pemalloc.
void *safe_pemalloc(size_t nmemb, size_t size, size_t offset, zend_bool persistent) | Allocate a buffer for holding nmemb blocks of each size bytes and an additional offset bytes. This is similar to pemalloc(nmemb * size + offset) but adds a special protection against overflows.
char *pestrdup(const char *s, zend_bool persistent) | Allocate a buffer that can hold the NULL-terminated string s and copy the s into that buffer.
char *pestrndup(const char *s, unsigned int length, zend_bool persistent) | Similar to pestrdup while the length of the NULL-terminated string is already known.
Caution

It is important to remember that memory allocated to be persistent is not optimized or tracked by the engine; it is not subject to memory_limit, additionally, all variables that are created by the Hacker for the engine must not use persistent memory.

User Contributed Notes

There are no user contributed notes for this page.


Thread-Safe Resource Manager

When PHP is built with Thread Safety enabled, the engine requires a way to isolate contexts from one another, such that the threads of a process may service individual requests without interference. TSRM is omnipotent within PHP, extension authors have to do very little in order to make sure their extensions can function in both a Thread Safe and Non Thread Safe architecture.

Example #1 Accessor macros for per-module globals

#ifdef ZTS
#define COUNTER_G(v) TSRMG(counter_globals_id, zend_counter_globals *, v)
#else
#define COUNTER_G(v) (counter_globals.v)
#endif

The snippet above shows how an extension author is expected to define their global accessors. The TSRMG macro takes an identifier, type cast and element name. The identifier is assigned by TSRM during initialization of the module. Declaring global accessors in this way ensures that an extension can operate safely in a Thread Safe and Non Thread Safe architecture using the same logic.

TSRM manages the isolation and safety of all global structures within PHP, from executor globals to extension globals, a pointer to the isolated storage is also passed around with most, or many of the API functions. The macros TSRMLS_C and TSRMLS_CC translate to "thread safe local storage" and "thread safe local storage prefixed with a comma" respectively.

If a function requires a pointer to TSRM, it is declared with the macro TSRMLS_D or TSRMLS_DC in it's prototype, which translates to "thread safe local storage only" and "thread safe local storage prefixed with a comma" respectively. Many macros within the engine reference TSRM, so it is a good idea to declare most things to accept TSRM, such that if they need to call upon TSRM they do not have to fetch a pointer during execution.

Because TSRM is thread local, and some functions (for compatibility reasons) are not able to accept TSRM directly, the macro TSRMLS_FETCH exists, which requests that TSRM fetches the pointer to the thread local storage. This should be avoided wherever it can be, as it is not without cost in a Thread Safe setup.

Note: While developing extensions, build errors that contain "tsrm_ls is undefined" or errors to that effect stem from the fact that TSRMLS is undefined in the current scope, to fix this, declare the function to accept TSRMLS with the appropriate macro, if the prototype of the function in question cannot be changed, you must call TSRMLS_FETCH within the function body.

The functionality documented hereafter is aimed at advanced use of TSRM. It is not ordinary for extensions to have to interact with TSRM directly, the pecl programmer should use API's above TSRM such as the Module Globals API.

TSRM Internals
Prototype | Description
typedef int ts_rsrc_id | The type definition to represent a resource by numeric identifiers
int tsrm_startup(int expected_threads, int expected_resources, int debug_level, char *debug_filename) | expected_threads and expected_resources are not hard limits. debug_level and debug_filename only have an effect on windows in Debug mode or when TSRM_DEBUG is defined. Called once per process in ZTS mode during server startup.
void tsrm_shutdown(void) | Should be the last thing called in the process to destroy and free all storage TSRM storage in ZTS mode.
ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor) | Allocates and thread safe pointer of size bytes, calling ctor on the pointer, assigning (and returning) the id to rsrc_id, the dtor is called when the resource is free'd. Module globals are isolated in ZTS mode using this API.
void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id) | Fetches a resource by previously allocated id from the entries created by the specified thread identifed by th_id.
void *ts_resource(ts_rsrc_id id) | Fetches a resource by previously allocated id from the entries created by the calling thread.
void ts_free_thread(void) | Destroys and frees the entries for the calling thread, currently this API is only used by ISAPI module.
void ts_free_id(ts_rsrc_id id) | Destroys the resource identified by previously allocated id. Module globals are cleaned up in ZTS mode using this API.

Note: There are more TSRM API functions to enable the creation and destruction of interpreter contexts, the usage of such functionality is out of the scope of this documentation, please see TSRM/TSRM.h for more information.

TSRM Mutex API
Prototype | Description
MUTEX_T tsrm_mutex_alloc(void) | Allocates and returns a suitable MUTEX_T for the environment.
int tsrm_mutex_lock(MUTEX_T mutexp) | Locks mutexp for the calling thread.
int tsrm_mutex_unlock(MUTEX_T mutexp) | Unlocks mutexp for the calling thread.
void tsrm_mutex_free(MUTEX_T mutexp); | Destroys and frees the (unlocked) and previously allocated mutexp.

Note: The mutex API is exposed by TSRM. However, its internal usage is far too wide to document usefully here, none the less, basic functionality and prototypes are documented here.

User Contributed Notes

There are no user contributed notes for this page.


Working with Variables

Table of Contents

  • Introduction to Variables
  • Working with Arrays
  • Working with HashTable
  • Working with Objects

User Contributed Notes

There are no user contributed notes for this page.


Introduction to Variables

A good understanding of how variables are stored and manipulated is essential to becoming a Hacker. The engine attempts to cover up the complexity of the concept of a variable that can be any type by providing a uniform and intuitive set of macros for accessing the structures various fields. As the Hacker works through this chapter, they should become comfortable with the terminology and concepts involved with Variables in PHP.

Note:

PHP is a dynamic, loosely typed language, that uses copy-on-write and reference counting.

To clarify what exactly is meant by the statement above: PHP is a high level language, weak typing is implicit of the engines preference to convert, or coerce variables into the required type at execution time. Reference counting is the means by which the engine can deduce when a variable no longer has any references in the users code, and so is able to free the structures associated with the variable.

All variables in PHP are represented by one structure, the zval:

typedef struct _zval_struct {
    zvalue_value value;        /* variable value */
    zend_uint refcount__gc;    /* reference counter */
    zend_uchar type;           /* value type */
    zend_uchar is_ref__gc;     /* reference flag */
} zval;

The zval_value is a union which can represent all types a variable may hold:

typedef union _zvalue_value {
    long lval;                 /* long value */
    double dval;               /* double value */
    struct {
        char *val;
        int len;               /* this will always be set for strings */
    } str;                     /* string (always has length) */
    HashTable *ht;             /* an array */
    zend_object_value obj;     /* stores an object store handle, and handlers */
} zvalue_value;

It should be clear from the structures above that a variable can be of one type, the variable data is represented by the appropriate field in the zval_value union. The zval itself holds the type, reference count and a flag to indicate if a variable is a reference.

Native Type Constants
Constant | Mapping
IS_NULL | no value is set in this case
IS_LONG | lval
IS_DOUBLE | dval
IS_BOOL | lval
IS_RESOURCE | lval
IS_STRING | str
IS_ARRAY | ht
IS_OBJECT | obj

Note:

There are additional constants that help to identify internal types such as constant arrays and callable objects, their usage is outside the scope of this part of the documentation.

The following table defines the macros exposed by the engine for working with zval values:

Accessor Macros
Prototype | Accesses | Description
zend_uchar Z_TYPE(zval zv) | type | returns the type of the value
long Z_LVAL(zval zv) | value.lval |
zend_bool Z_BVAL(zval zv) | value.lval | cast long value to zend_bool
double Z_DVAL(zval zv) | value.dval |
long Z_RESVAL(zval zv) | value.lval | returns the resource list identifier for value
char* Z_STRVAL(zval zv) | value.str.val | return the string value
int Z_STRLEN(zval zv) | value.str.len | return the length of the string value
HashTable* Z_ARRVAL(zval zv) | value.ht | return the HashTable (array) value
zend_object_value Z_OBJVAL(zval zv) | value.obj | returns object value
uint Z_OBJ_HANDLE(zval zv) | value.obj.handle | returns the object handle for object value
zend_object_handlers* Z_OBJ_HT_P(zval zv) | value.obj.handlers | returns the handler table for object value
zend_class_entry* Z_OBJCE(zval zv) | value.obj | returns the class entry for object value
HashTable* Z_OBJPROP(zval zv) | value.obj | returns the properties of object value
HashTable* Z_OBJPROP(zval zv) | value.obj | returns the properties of object value
HashTable* Z_OBJDEBUG(zval zv) | value.obj | if an object has the get_debug_info handler set, it is called, else Z_OBJPROP is called

Please check the Reference Counting Basics chapter for details how reference counting and references work in detail.

Reference Count Manipulation
Prototype | Description
zend_uint Z_REFCOUNT(zval zv) | returns the reference count of the value
zend_uint Z_SET_REFCOUNT(zval zv) | sets the reference count of the value, returning it
zend_uint Z_ADDREF(zval zv) | pre-increments the reference count of value, returning it
zend_uint Z_DELREF(zval zv) | pre-decrements the reference count of value, returning it
zend_bool Z_ISREF(zval zv) | tells if the zval is a reference
void Z_UNSET_ISREF(zval zv) | set is_ref__gc to 0
void Z_SET_ISREF(zval zv) | set is_ref__gc to 1
void Z_SET_ISREF_TO(zval zv, zend_uchar to) | set is_ref__gc to to

Note:

The Z_* macros above all take a zval, they are all defined again suffixed with _P to take a pointer to a zval, for example zend_uchar Z_TYPE_P(zval* pzv), and again suffixed with _PP to take a pointer to a pointer to a zval, for example zend_uchar Z_TYPE_PP(zval** ppzv)

Creation, Destruction, Separation and Copying
Prototype | Description
ALLOC_ZVAL(zval* pzval) | emallocs pzval
ALLOC_INIT_ZVAL(zval* pzval) | emallocs pzval, and points pzval at a null typed zval for sanity
MAKE_STD_ZVAL(zval* pzval) | emallocs pzval, setting the refcount to 1
ZVAL_COPY_VALUE(zval* dst, zval* src) | sets the value and type of dst from the value and type of src
INIT_PZVAL_COPY(zval* dst, zval*dst) | performs ZVAL_COPY_VALUE, setting refcount of dst to 1, and setting is_ref__gc to 0
SEPARATE_ZVAL(zval** ppzval) | if the refcount of ppzval is >1, redirects *ppzval to a newly emalloc'd, copied, and constructed zval of the same type and value
SEPARATE_ZVAL_IF_NOT_REF(zval** ppzval) | if *ppzval is not a reference, will perform SEPARATE_ZVAL on ppzval
SEPARATE_ZVAL_TO_MAKE_IS_REF(zval** ppzval) | if *ppzval is not a reference, performs SEPARATE_ZVAL then Z_SET_ISREF_PP on ppzval
COPY_PZVAL_TO_ZVAL(zval dst, zval** src) | results in dst being a copy of src without affecting the refcount of src
MAKE_COPY_ZVAL(zval** src, zval* dst) | performs INIT_PZVAL_COPY, then zval_copy_ctor on the new zval
void zval_copy_ctor(zval** pzval) | performs maintenance of reference counts, used widely throughout the engine
void zval_ptr_dtor(zval* pzval) | decrements the refcount for the variable, if no refcounts remain the variable is destroyed
FREE_ZVAL(zval* pzval) | efrees pzval

Note:

Objects and Resources have a reference count as part of their respective structures, when zval_ptr_dtor is called on either, their appropriate del_ref method is executed. See Working with Objects and Working with Resources for more information.

If the Hacker only has room to remember two more functions, they should be zval_copy_ctor and zval_ptr_dtor, these are at the basis of the engines reference counting mechanisms and it is important to note that zval_copy_ctor does not actually result in any copying occuring under normal circumstances, it simply increases the reference count. By the same token, zval_ptr_dtor only really destroys a variable when there are no references remaining to it and the refcount is at 0.

PHP is weakly typed, as such the engine provides API functions for converting variables from one type to another.

Type Conversion
Prototype
void convert_to_long(zval* pzval)
void convert_to_double(zval* pzval)
void convert_to_long_base(zval* pzval, int base)
void convert_to_null(zval* pzval)
void convert_to_boolean(zval* pzval)
void convert_to_array(zval* pzval)
void convert_to_object(zval* pzval)
void convert_object_to_type(zval* pzval, convert_func_t converter)

Note:

convert_func_t functions should have the prototype (void) (zval* pzval)

By now you should have a good understanding of: the types that are native to the engine, how to detect types and read zval values, how to manipulate refcounts, and other zval flags

User Contributed Notes

There are no user contributed notes for this page.


Working with Arrays

Arrays are stored in HashTable strucures, and have the zval type IS_ARRAY. The API functions for creating, destroying and manipulating these structures as variables are documented here and can be found in Zend/zend_API.h

HashTable as Variable API
Prototype | Description
void array_init(zval* pzval) | initializes the variable as a HashTable, setting type and appropriate destructor function for the HashTable
void array_init_size(zval* pzval) | initializes the variable as array_init with a minimum of size buckets

Note:

Do not squint too hard looking for array_destroy: to destroy a variable array you should call zval_ptr_dtor on the variable, if there are no other references to the variable it will result in the array being destroyed.

Indexed Arrays API
Prototype
int add_index_long(zval* pzval, ulong index, long value)
int add_index_null(zval* pzval, ulong index)
int add_index_bool(zval* pzval, ulong index, zend_bool value)
int add_index_bool(zval* pzval, ulong index, zend_bool value)
int add_index_resource(zval* pzval, ulong index, uint value)
int add_index_double(zval* pzval, ulong index, double value)
int add_index_string(zval* pzval, ulong index, char* string, zend_bool duplicate)
int add_index_stringl(zval* pzval, ulong index, char* string, uint length, zend_bool duplicate)
int add_index_zval(zval* pzval, ulong index, zval* value)
int add_next_index_long(zval* pzval, long value)
int add_next_index_null(zval* pzval)
int add_next_index_bool(zval* pzval, zend_bool value)
int add_next_index_resource(zval* pzval, uint value)
int add_next_index_double(zval* pzval, double value)
int add_next_index_string(zval* pzval, const char* string, zend_bool dulpicate)
int add_next_index_stringl(zval* pzval, const char* string, uint length, zend_bool duplicate)
int add_next_index_zval(zval* pzval, zval* value)
Associative Arrays API
Prototype
int add_assoc_long(zval* pzval, const char* key, long value)
int add_assoc_long_ex(zval* pzval, const char* key, uint klen, long value)
int add_assoc_null(zval* pzval, const char* key)
int add_assoc_null_ex(zval* pzval, const char* key, uint klen)
int add_assoc_bool(zval* pzval, const char* key, zend_bool value)
int add_assoc_bool(zval* pzval, const char* key, zend_bool value)
int add_assoc_bool_ex(zval* pzval, const char* key, uint klen, zend_bool value)
int add_assoc_resource(zval* pzval, const char* key, uint value)
int add_assoc_resource_ex(zval* pzval, const char* key, uint klen, uint value)
int add_assoc_double(zval* pzval, const char* key, double value)
int add_assoc_double_ex(zval* pzval, const char* key, uint klen, double value)
int add_assoc_string(zval* pzval, const char* key, const char* value)
int add_assoc_string_ex(zval* pzval, const char* key, uint klen, const char* value)
int add_assoc_stringl(zval* pzval, const char* key, const char* value, uint vlen, zend_bool duplicate)
int add_assoc_stringl_ex(zval* pzval, const char* key, uint klen, const char* value, uint vlen, zend_bool duplicate)
int add_assoc_zval(zval* pzval, const char* key, zval* value)
int add_assoc_zval_ex(zval* pzval, const char* key, uint klen, zval* value)

Note:

add_*_string functions that accept a parameter named duplicate, will duplicate the string with estrndup when duplicate is true

Note:

add_*_zval functions do not adjust the refcount of the value parameter

To perform more advanced operations on array variables we must use the HashTable API directly.

User Contributed Notes

There are no user contributed notes for this page.


Working with HashTable

The HashTable structure serves many purposes in PHP and can be found everywhere, a good understanding of it's functionality is a preqrequisite of being a good Hacker.

The HashTable implemented by the engine is a standard HashTable, that is to say, a key => value based store, where the keys are always strings, whose hashes are calculated with a built in hashing algorithm zend_inline_hash_func(const char* key, uint length), which results in good distribution, and reasonable usage.

The internal structure and exact operation of HashTable is out of the scope of this document, this document serves to inform you of the API's available and how best to use them. For a more detailed picture of the HashTable see Zend/zend_hash.h.

Unless otherwise stated, these functions all return FAILURE if the requested operation fails for any reason, otherwise SUCCESS is returned.

Note:

It is important to remember that ordinarily HashTable API functions expect key lengths to be the length of the null terminated string, including the null termination, in other words they expect strlen(key) + 1

Below are some typedefs you should know when interacting with HashTable. They are mostly self explanitory and will allow the Hacker to fully understand the prototypes below properly.

typedef ulong (*hash_func_t)(const char *arKey, uint nKeyLength); /* mostly redundant */
typedef int  (*compare_func_t)(const void *, const void * TSRMLS_DC); /* comparison function */
typedef void (*sort_func_t)(void *, size_t, register size_t, compare_func_t TSRMLS_DC); /* sorting function */
typedef void (*dtor_func_t)(void *pDest); /* destructor function */
typedef void (*copy_ctor_func_t)(void *pElement); /* copy constructor */
typedef void (*copy_ctor_param_func_t)(void *pElement, void *pParam); /* copy with argument constructor */
typedef int (*apply_func_t)(void *pDest TSRMLS_DC); /* apply function */
typedef int (*apply_func_arg_t)(void *pDest, void *argument TSRMLS_DC); /* apply with argument function */
typedef int (*apply_func_args_t)(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key); /* apply with multiple arguments function */
HashTable API
int zend_hash_init(HashTable* ht, uint size, hash_func_t hash, dtor_func_t destructor, zend_bool persistent)
Initializes the hash table to hold at least size elements, hash exists for historical reasons and is always ignored, zend_inline_hash_func is always used as the hashing function. destructor may be NULL.
int zend_hash_add(HashTable* ht, const char* key, uint klen, void* data, uint dlen, void** dest)
adds data to the table using the specified key, where the key is length bytes long (and will be copied from key to the table). If the key is set FAILURE will be returned.
int zend_hash_update(HashTable* ht, const char* key, uint klen, void* data, uint dlen, void** dest)
adds data to the table using the specified key, where the key is length bytes long (and will be copied from key to the table). If the key has been set previously the old data has dtor_func_t called on it and the existing data is replaced with data
int zend_hash_find(HashTable* ht, const char* key, uint klen, void** data)
searches the table for key, setting *data and returning SUCCESS if it is found.
zend_bool zend_hash_exists(HashTable* ht, const char* key, uint klen)
returns positively if key is found in ht
int zend_hash_del(HashTable* ht, const char* key, uint klen)
removes the entry identified by key from the table if found
int zend_hash_index_update(HashTable* ht, ulong index, void* data, uint dsize, void** dest)
updates the entry at index in ht, with the data at data
int zend_hash_index_del(HashTable* ht, ulong index)
deletes the entry at index from ht
int zend_hash_index_find(HashTable* ht, ulong index, void** data)
redirects *data to the data identified by index in ht
int zend_hash_index_exists(HashTable* ht, ulong index)
returns positively if index is found in ht
ulong zend_hash_next_free_element(HashTable* ht)
returns the index of the next free element in ht
Caution

zend_hash_* functions that accept void* data should normally cast it to (void**) (ie, (void**) &data)

Traversing the HashTable is often necessary, to do this you provide a pointer to a HashTable, with an optional HashPosition - a structure internal to the HashTable API that allows traversal not to affect the internal position of HashTable. The following traversal functions are provided, and an example usage found below.

HashTable Traversal API
int zend_hash_internal_pointer_reset(HashTable* ht)
resets the internal pointer of ht to the start
int zend_hash_internal_pointer_reset_ex(HashTable* ht, HashPosition position)
sets position the the start of ht
int zend_hash_get_current_data(HashTable* ht, void* data)
gets the data at the current position in ht, data should be cast to void**, ie: (void**) &data
int zend_hash_get_current_data_ex(HashTable* ht, void* data, HashPosition position)
sets data to the data at position in ht
int zend_hash_get_current_key(HashTable* ht, void* data, char**key, uint klen, ulong index, zend_bool duplicate)
sets key, klen, and index from the key information at the current position. The possible return values HASH_KEY_IS_STRING and HASH_KEY_IS_LONG are indicative of the kind of key found at the current posision.
int zend_hash_get_current_key_ex(HashTable* ht, void* data, char**key, uint klen, ulong index, zend_bool duplicate, HashPosition position)
sets key, klen, and index from the key information at position. The possible return values HASH_KEY_IS_STRING and HASH_KEY_IS_LONG are indicative of the kind of key found at position.
int zend_hash_move_forward(HashTable* ht)
moves the internal pointer of ht to the next entry in ht
int zend_hash_move_forward_ex(HashTable* ht, HashPosition position)
moves position to the next entry in ht

The functions above allow traversal over a HashTable to be much like a normal loop, which will look something like:

HashPosition position;
zval **data = NULL;

for (zend_hash_internal_pointer_reset_ex(ht, &position);
     zend_hash_get_current_data_ex(ht, (void**) &data, &position) == SUCCESS;
     zend_hash_move_forward_ex(ht, &position)) {

     /* by now we have data set and can use Z_ macros for accessing type and variable data */

     char *key = NULL;
     uint  klen;
     ulong index;

     if (zend_hash_get_current_key_ex(ht, &key, &klen, &index, 0, &position) == HASH_KEY_IS_STRING) {
         /* the key is a string, key and klen will be set */
     } else {
         /* we assume the key to be long, index will be set */
     }
}

Note:

If a HashTable has been passed into the engine, it is a good idea to use the zend_hash_*_ex API to avoid unexpected behaviour in userland.

Note:

If a duplicate of the key is requested and HAS_KEY_IS_STRING is returned the caller must efree the duplicated key

In addition to traversal, the engine provides API functions for merging, copying, and comparing HashTables. These are all concepts that the Hacker should be familiar with. A concept possibly not in the repertoire of the Hacker is applying, which is lamens terms is functionality of the HashTable API allowing the Hacker to pass a callback function to be executed on every entry in a HashTable.

Copying, Merging and Sorting
void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size)
Copies the contents of source to target. tmp should be an unallocated temporary pointer of the appropriate type, used during copying. size is the size of each element.
void zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, void *tmp, uint size, zend_bool overwrite)
Merges the contents of source into destination, replacing existing entries only where overwrite is true.
void int zend_hash_sort(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber TSRMLS_DC)
renumber controls if indexes should be preserved, see the typedefs at the start of this section for more information

Note:

When a function accepts a copy_ctor_func_t pCopyConstructor, the function passed with be executed upon creation of every new entry in the HashTable. The most common copy constructor used within the engine is a wrapper around zval_copy_ctor, the macro ZVAL_COPY_CTOR.

User Contributed Notes

There are no user contributed notes for this page.


  • ≪ Working with HashTable
  • Writing Functions ≫
  • PHP Manual
  • Working with Variables
  • Working with Objects

Working with Objects

User Contributed Notes

There are no user contributed notes for this page.


Writing Functions

Functions and Methods in PHP take much the same form, a method is simply a function with a specific scope; the scope of their class entry. The Hacker will read about class entries in other sections of the Hacker's guide. The purpose of this section is to introduce the Hacker to the anatomy of a function or method; the Hacker will learn how to define functions, how to accept variables and how to return variables to the programmer of PHP.

The anatomy of a function could not be simpler:

PHP_FUNCTION(hackers_function) {
  /* your accepted arguments here */
  long number;

  /* accepting arguments */
  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &number) != SUCCESS) {
      return;
  }

  /* do some work on the input */
  number *= 2;

  /* set return value */
  RETURN_LONG(number);
}

The PHP_FUNCTION(hackers_function) preprocessor instruction will result in the following declaration:

void zif_hackers_function(INTERNAL_FUNCTION_PARAMETERS)

INTERNAL_FUNCTION_PARAMETERS is defined as a macro and explained in the following table:

INTERNAL_FUNCTION_PARAMETERS
Name and type | Description | Access macros
int ht | Number of actual parameters passed by user | ZEND_NUM_ARGS()
zval *return_value | Pointer to a PHP variable that can be filled with the return value passed to the user. The default type is IS_NULL. | RETVAL_*, RETURN_*
zval **return_value_ptr | When returning references to PHP set this to a pointer to your variable. It is not suggested to return references. |
zval *this_ptr | If this is a method call, points to the PHP variable holding the $this object. | getThis()
int return_value_used | Flag indicating whether the returned value will be used by the caller. |

For clarity, the fully expanded declaration for PHP_FUNCTION(hackers_function) looks like:

void zif_hackers_function(int ht, zval* return_value, zval** return_value_ptr, zval* this_ptr, int return_value_used)

The presence of this_ptr may be confusing, classes are covered in detail in later sections, suffice to say that PHP_METHOD(MyClass, hackersFunction) would result in the following declaration:

void zim_MyClass_hackersFunction(INTERNAL_FUNCTION_PARAMETERS)

hackers_function doesn't do anything useful, it accepts a number using the zend_parse_parameters API, doubles it, and returns it to the engine. It is obvious that a normal function would have to do something more complex than double the input, for the purposes of education we are keeping it simple. On entry to the function return_value is allocated and initialized to null, making null the default return value of any function in PHP.

If zend_parse_parameters does not recieve what is specified by the Hacker as the correct arguments, and the arguments recieved cannot be converted to conform with type_spec an error will be raised, and by convention, the Hacker should return immediately.

Note:

Arrays, Objects, and Resources cannot be converted.

Parsing Parameters Prototypes
int zend_parse_parameters(int num_args TSRMLS_DC, char *type_spec, ...)
int zend_parse_parameters_ex(int flags, int num_args TSRMLS_DC, char *type_spec, ...)
int zend_parse_parameter(int flags, int arg_num TSRMLS_DC, zval **arg, const char *spec, ...)

Note:

zend_parse_parameter is available from version 5.5, it behaves like zend_parse_parameters_ex except that instead of reading the arguments from the stack, it receives a single zval to convert, which may be changed in place.

Note:

flags is intended to be a mask, currently only ZEND_PARSE_PARAMS_QUIET will have any impact (supress warnings)

The variable arguments recieved by these API functions are expected to be the address of C variables, and should be considered the output of the zend_parse_parameters API functions.

Type Specifiers
Spec | Type | Locals
a | array | zval*
A | array or object | zval*
b | boolean | zend_bool
C | class | zend_class_entry*
d | double | double
f | function | zend_fcall_info*, zend_fcall_info_cache*
h | array | HashTable*
H | array or object | HashTable*
l | long | long
L | long (limits out-of-range LONG_MAX/LONG_MIN) | long
o | object | zval*
O | object (of specified zend_class_entry) | zval*, zend_class_entry*
p | string (a valid path) | char*, int
r | resource | zval*
s | string | char*, int
z | mixed | zval*
Z | mixed | zval**

Note:

Where the type specifier is O, the local zend_class_entry* is to be considered input (part of the type spec) to zend_parse_parameter

Advanced Type Specifiers
Spec | Description
* | a variable number of argument of the preceeding type, 0 or more
+ | a variable number of argument of the preceeding type, 1 or more
| | indicates that the remaining parameters are optional
/ | SEPARATE_ZVAL_IF_NOT_REF on the parameter it follows
! | the preceeding parameter can be of the specified type or null For 'b', 'l' and 'd', an extra argument of type zend_bool* must be passed after the corresponding bool*, long* or double* addresses which will be set true if null is recieved.

Note:

Consult README.PARAMETER_PARSING_API included in source distributions for more information on parsing parameters

Once the Hacker's function has executed whatever it was implemented to execute, it is time to set the return_value for the engine. The RETURN_ and RETVAL_ macros are just wrappers around Z_*_P macros that work with return_value.

Note:

RETURN_ macros cause execution to leave the function immediately (ie: return;), while RETVAL_ macros allow execution to continue after return_value has been set.

The Hacker should now have a reasonable understanding of the anatomy of a function, and to some degree, the anatomy of a method.

User Contributed Notes

There are no user contributed notes for this page.


  • ≪ Writing Functions
  • Working with Resources ≫
  • PHP Manual
  • PHP 코어: Zend 엔진 해커 가이드
  • Writing Classes

Writing Classes

User Contributed Notes

There are no user contributed notes for this page.


Working with Resources

This section will serve to introduce the Hacker to the concept and usage of Resources in PHP.

User Contributed Notes

There are no user contributed notes for this page.


  • ≪ Working with Resources
  • Working with streams ≫
  • PHP Manual
  • PHP 코어: Zend 엔진 해커 가이드
  • Working with INI settings

Working with INI settings

User Contributed Notes

There are no user contributed notes for this page.


Working with streams

Note:

Information on using streams within the PHP source code can be found in the Streams API for PHP Extension Authors reference.

User Contributed Notes

There are no user contributed notes for this page.


The "counter" Extension - A Continuing Example

Table of Contents

  • 설치/설정
  • 예약 상수
  • 예제
  • Counter
  • Basic
  • Extended

Preface

Throughout this Zend documentation, references are made to an example module in order to illustrate various concepts. The "counter" extension is this example, a fictional yet functional Zend module which strives to use as much of the Zend API as is reasonably possible. This short chapter describes the userland interface to the completed extension.

Note: "counter" serves no practical purpose whatsoever, as the functionality it provides is far more effectively implemented by appropriate userland code.

User Contributed Notes

There are no user contributed notes for this page.


  • ≪ The "counter" Extension - A Continuing Example
  • 소개 ≫
  • PHP Manual
  • The "counter" Extension - A Continuing Example
  • 설치/설정

설치/설정

Table of Contents

  • 소개
  • 실행시 설정
  • 자원형

User Contributed Notes

There are no user contributed notes for this page.


소개

The "counter" extension provides any number of counters to PHP code using it which reset at times determined by the caller.

There are three interfaces to "counter": basic, extended, and objective. The basic interface provides a single counter controlled by INI settings and function calls. The extended interface provides an arbitrary number of named counter resources which may optionally persist beyond the lifetime of a single PHP request. The objective interface combines both the basic and extended interfaces into a Counter class.

User Contributed Notes

There are no user contributed notes for this page.


실행시 설정

이 함수의 작동은 php.ini 설정에 영향을 받습니다.

Counter configuration options
Name | Default | Changeable | Changelog
counter.reset_time | COUNTER_RESET_PER_REQUEST | PHP_INI_ALL |
counter.save_path | "" | PHP_INI_ALL |
counter.initial_value | "0" | PHP_INI_ALL |

PHP_INI_* 모드에 대한 상세와 정의는 Where a configuration setting may be set를 참고하십시오.

counter.reset_time integer
counter.reset_time tells "counter" to reset the counter used by the basic interface. It may be any of the COUNTER_RESET_* constants (see below).
counter.save_path string
Tells "counter" where to save data that has to persist between invocations of PHP (i.e. any counter that has COUNTER_RESET_NEVER or COUNTER_FLAG_SAVE). A file will be created at this path, which must be readable and writable to whatever user PHP is running as.
counter.initial_value integer
Sets the initial value of the counter used by the basic interface whenever it is reset.

User Contributed Notes

There are no user contributed notes for this page.


자원형

The "counter" extension defines one resource type: a counter.

User Contributed Notes

There are no user contributed notes for this page.


예약 상수

이 확장은 다음의 상수들을 정의합니다. 이 확장을 PHP에 내장했거나, 실행시에 동적으로 읽어들일 경우에만 사용할 수 있습니다.

COUNTER_FLAG_PERSIST (integer)
A counter with this flag will be created as a persistent resource.
COUNTER_FLAG_SAVE (integer)
A counter with this flag will be saved between invocations of PHP.
COUNTER_FLAG_NO_OVERWRITE (integer)
This flag causes counter_create() to avoid overwriting an existing named counter with a new one.
COUNTER_META_NAME (string)
Pass this constant to get the name of a counter resource or object.
COUNTER_META_IS_PERISTENT (string)
Pass this constant to determine whether a counter resource or object is persistent (has the COUNTER_FLAG_PERSIST flag).
COUNTER_RESET_NEVER (integer)
The counter will never be reset.
COUNTER_RESET_PER_LOAD (integer)
The counter will be reset on each invocation of PHP.
COUNTER_RESET_PER_REQUEST (integer)
The counter will be reset on each request.

User Contributed Notes

There are no user contributed notes for this page.


예제

Table of Contents

  • Basic interface
  • Extended interface
  • Objective interface

User Contributed Notes

There are no user contributed notes for this page.


Basic interface

The basic interface provides three simple functions, illustrated here:

Example #1 "counter"'s basic interface

<?php
$starting_counter_value = counter_get();
counter_bump(1);
$second_counter_value = counter_get();
counter_reset();
$final_counter_value = counter_get();
printf("%3d %3d %3d", $starting_counter_value, $second_counter_value, $final_counter_value);
?>

위 예제의 출력:

  0   1   0

The basic interface also provides a number of INI settings.

User Contributed Notes

There are no user contributed notes for this page.


Extended interface

The extended interface provides a small suite of functions that allow the user to define an arbitrary number of named counters with unique settings. The basic interface can be used in parallel with the extended interface.

Example #1 "counter"'s extended interface

<?php
function print_counter_info($counter)
{
    if (is_resource($counter)) {
        printf("Counter's name is '%s' and is%s persistent. Its current value is %d.\n",
            counter_get_meta($counter, COUNTER_META_NAME),
            counter_get_meta($counter, COUNTER_META_IS_PERSISTENT) ? '' : ' not',
            counter_get_value($counter));
    } else {
      print "Not a valid counter!\n";
    }
}

if (($counter_one = counter_get_named("one")) === NULL) {
    $counter_one = counter_create("one", 0, COUNTER_FLAG_PERSIST);
}
counter_bump_value($counter_one, 2);
$counter_two = counter_create("two", 5);
$counter_three = counter_get_named("three");
$counter_four = counter_create("four", 2, COUNTER_FLAG_PERSIST | COUNTER_FLAG_SAVE | COUNTER_FLAG_NO_OVERWRITE);
counter_bump_value($counter_four, 1);

print_counter_info($counter_one);
print_counter_info($counter_two);
print_counter_info($counter_three);
print_counter_info($counter_four);
?>

When run once, the above example outputs:

Counter's name is 'one' and is persistent. Its current value is 2.
Counter's name is 'two' and is not persistent. Its current value is 5.
Not a valid counter!
Counter's name is 'four' and is persistent. Its current value is 3.

If run a second time within the same instance of PHP, it outputs:

Counter's name is 'one' and is persistent. Its current value is 4.
Counter's name is 'two' and is not persistent. Its current value is 5.
Not a valid counter!
Counter's name is 'four' and is persistent. Its current value is 4.

If then run a third time in a different instance of PHP, it outputs:

Counter's name is 'one' and is persistent. Its current value is 2.
Counter's name is 'two' and is not persistent. Its current value is 5.
Not a valid counter!
Counter's name is 'four' and is persistent. Its current value is 5.

User Contributed Notes

There are no user contributed notes for this page.


Objective interface

The objective interface provides an object-oriented way to access the extended interfaces. The following example shows how the above one would be implemented using the objective interface. The output of this example is exactly the same, except that instead of printing "Not a valid counter!", this will instead issue a PHP warning that the variable $counter_three is not an object. This example shows that it is possible to subclass the Counter class defined by the extension, as well as that the counter's value is maintained using an instance variable rather than method access.

Example #1 "counter"'s objective interface

<?php
class MyCounter extends Counter
{
    public function printCounterInfo() {
        printf("Counter's name is '%s' and is%s persistent. Its current value is %d.\n",
            $this->getMeta(COUNTER_META_NAME),
            $this->getMeta(COUNTER_META_IS_PERSISTENT) ? '' : ' not',
            $this->value);
    }
}

Counter::setCounterClass("MyCounter");
if (($counter_one = Counter::getNamed("one")) === NULL) {
    $counter_one = new Counter("one", 0, COUNTER_FLAG_PERSIST);
}
$counter_one->bumpValue(2); // we aren't allowed to "set" the value directly
$counter_two = new Counter("two", 5);
$counter_three = Counter::getNamed("three");
$counter_four = new Counter("four", 2, COUNTER_FLAG_PERSIST | COUNTER_FLAG_SAVE | COUNTER_FLAG_NO_OVERWRITE);
$counter_four->bumpValue(1);

$counter_one->printCounterInfo();
$counter_two->printCounterInfo();
$counter_three->printCounterInfo();
$counter_four->printCounterInfo();
?>

User Contributed Notes

There are no user contributed notes for this page.


The Counter class

소개

Represents a single counter object.

클래스 개요

Counter {
__construct ( string $name [, integer $initial_value [, integer $flags ]] )
integer getValue ( void )
bumpValue ( integer $offset )
voidresetValue ( void )
mixed getMeta ( integer $attribute )
static Counter getNamed ( string $name )
static voidsetCounterClass ( string $name )
}

Table of Contents

  • Counter::__construct — Creates an instance of a Counter which maintains a single numeric value.
  • Counter::getValue — Get the current value of a counter.
  • Counter::bumpValue — Change the current value of a counter.
  • Counter::resetValue — Reset the current value of a counter.
  • Counter::getMeta — Return a piece of metainformation about a counter.
  • Counter::getNamed — Retrieve an existing named counter.
  • Counter::setCounterClass — Set the class returned by Counter::getNamed.

User Contributed Notes

There are no user contributed notes for this page.


Creates an instance of a Counter which maintains a single numeric value.

  • ≪ Counter
  • Counter::getValue ≫
  • PHP Manual
  • Counter
  • Creates an instance of a Counter which maintains a single numeric value.

Counter::__construct

Counter::__construct — Creates an instance of a Counter which maintains a single numeric value.

설명

Counter::__construct ( string $name [, integer $initial_value [, integer $flags ]] )

Creates an instance of a Counter which maintains a single numeric value.

인수

name
The new counter's name.
initial_value
The initial value of the counter. Defaults to zero (0).
flags
Flags for the new counter, chosen from the COUNTER_FLAG_* constants.

반환값

Returns a Counter object on success.

오류/예외

Counter::__construct() throws an Exception if something goes wrong.

User Contributed Notes

There are no user contributed notes for this page.


Get the current value of a counter.

  • ≪ Counter::__construct
  • Counter::bumpValue ≫
  • PHP Manual
  • Counter
  • Get the current value of a counter.

Counter::getValue

Counter::getValue — Get the current value of a counter.

설명

integer Counter::getValue ( void )

Counter::getValue() returns the current value of a counter.

반환값

Counter::getValue() returns an integer.

참고

  • Counter::bumpValue() - Change the current value of a counter.
  • Counter::resetValue() - Reset the current value of a counter.

User Contributed Notes

There are no user contributed notes for this page.


Change the current value of a counter.

  • ≪ Counter::getValue
  • Counter::resetValue ≫
  • PHP Manual
  • Counter
  • Change the current value of a counter.

Counter::bumpValue

Counter::bumpValue — Change the current value of a counter.

설명

Counter::bumpValue ( integer $offset )

Counter::bumpValue() updates the current value of a counter.

인수

offset
The amount by which to change the counter's value. Can be negative.

참고

  • Counter::getValue() - Get the current value of a counter.
  • Counter::resetValue() - Reset the current value of a counter.

User Contributed Notes

There are no user contributed notes for this page.


Reset the current value of a counter.

  • ≪ Counter::bumpValue
  • Counter::getMeta ≫
  • PHP Manual
  • Counter
  • Reset the current value of a counter.

Counter::resetValue

Counter::resetValue — Reset the current value of a counter.

설명

voidCounter::resetValue ( void )

Counter::resetValue() resets the current value of a counter to its original initial value.

참고

  • Counter::getValue() - Get the current value of a counter.
  • Counter::bumpValue() - Change the current value of a counter.

User Contributed Notes

There are no user contributed notes for this page.


Return a piece of metainformation about a counter.

  • ≪ Counter::resetValue
  • Counter::getNamed ≫
  • PHP Manual
  • Counter
  • Return a piece of metainformation about a counter.

Counter::getMeta

Counter::getMeta — Return a piece of metainformation about a counter.

설명

mixed Counter::getMeta ( integer $attribute )

Counter::getMeta() returns metainformation about a counter.

인수

attribute
The metainformation to retrieve.

반환값

Counter::getMeta() returns values of varying types based on which metainformation was requested.

User Contributed Notes

There are no user contributed notes for this page.


Retrieve an existing named counter.

  • ≪ Counter::getMeta
  • Counter::setCounterClass ≫
  • PHP Manual
  • Counter
  • Retrieve an existing named counter.

Counter::getNamed

Counter::getNamed — Retrieve an existing named counter.

설명

static Counter Counter::getNamed ( string $name )

Counter::getNamed() returns an existing counter by name if that name exists, or NULL otherwise. This is a static function.

인수

name
The counter name to search for.

반환값

Counter::getNamed() returns an existing counter by name if that name exists, or NULL otherwise.

User Contributed Notes

There are no user contributed notes for this page.


Set the class returned by Counter::getNamed.

  • ≪ Counter::getNamed
  • Basic ≫
  • PHP Manual
  • Counter
  • Set the class returned by Counter::getNamed.

Counter::setCounterClass

Counter::setCounterClass — Set the class returned by Counter::getNamed().

설명

static voidCounter::setCounterClass ( string $name )

Counter::setCounterClass() changes the class of objects returned by Counter::getNamed(). The class being set must not have a public constructor and must be a subclass of Counter. If these conditions are not met, a fatal error is raised. This is a static function.

인수

name
The name of the class to use.

User Contributed Notes

There are no user contributed notes for this page.


The basic interface

Table of Contents

  • counter_get — Get the current value of the basic counter.
  • counter_bump — Update the current value of the basic counter.
  • counter_reset — Reset the current value of the basic counter.

User Contributed Notes

There are no user contributed notes for this page.


Get the current value of the basic counter.

  • ≪ Basic
  • counter_bump ≫
  • PHP Manual
  • Basic
  • Get the current value of the basic counter.

counter_get

counter_get — Get the current value of the basic counter.

설명

integer counter_get ( void )

counter_get() returns the current value of the basic interface's counter.

반환값

counter_get() returns an integer.

참고

  • counter_bump() - Update the current value of the basic counter.
  • counter_reset() - Reset the current value of the basic counter.

User Contributed Notes

There are no user contributed notes for this page.


Update the current value of the basic counter.

  • ≪ counter_get
  • counter_reset ≫
  • PHP Manual
  • Basic
  • Update the current value of the basic counter.

counter_bump

counter_bump — Update the current value of the basic counter.

설명

voidcounter_bump ( integer $offset )

counter_bump() updates the current value of the basic interface's counter.

인수

offset
The amount by which to change the counter's value. Can be negative.

참고

  • counter_get() - Get the current value of the basic counter.
  • counter_reset() - Reset the current value of the basic counter.

User Contributed Notes

There are no user contributed notes for this page.


Reset the current value of the basic counter.

  • ≪ counter_bump
  • Extended ≫
  • PHP Manual
  • Basic
  • Reset the current value of the basic counter.

counter_reset

counter_reset — Reset the current value of the basic counter.

설명

voidcounter_reset ( void )

counter_reset() resets the current value of the basic interface's counter to its original initial value.

참고

  • counter_get() - Get the current value of the basic counter.
  • counter_bump() - Update the current value of the basic counter.

User Contributed Notes

There are no user contributed notes for this page.


The extended interface

Table of Contents

  • counter_create — Creates a counter which maintains a single numeric value.
  • counter_get_value — Get the current value of a counter resource.
  • counter_bump_value — Change the current value of a counter resource.
  • counter_reset_value — Reset the current value of a counter resource.
  • counter_get_meta — Return a piece of metainformation about a counter resource.
  • counter_get_named — Retrieve an existing named counter as a resource.

User Contributed Notes

There are no user contributed notes for this page.


Creates a counter which maintains a single numeric value.

  • ≪ Extended
  • counter_get_value ≫
  • PHP Manual
  • Extended
  • Creates a counter which maintains a single numeric value.

counter_create

counter_create — Creates a counter which maintains a single numeric value.

설명

resource counter_create ( string $name [, integer $initial_value [, integer $flags ]] )

Creates a counter which maintains a single numeric value.

인수

name
The new counter's name.
initial_value
The initial value of the counter. Defaults to zero (0).
flags
Flags for the new counter, chosen from the COUNTER_FLAG_* constants.

반환값

Returns a counter resource.

User Contributed Notes

There are no user contributed notes for this page.


Get the current value of a counter resource.

  • ≪ counter_create
  • counter_bump_value ≫
  • PHP Manual
  • Extended
  • Get the current value of a counter resource.

counter_get_value

counter_get_value — Get the current value of a counter resource.

설명

integer counter_get_value ( resource $counter )

counter_get_value() returns the current value of a counter resource.

인수

counter
The counter resource to operate on.

반환값

counter_get_value() returns an integer.

참고

  • counter_bump_value() - Change the current value of a counter resource.
  • counter_reset_value() - Reset the current value of a counter resource.

User Contributed Notes

There are no user contributed notes for this page.


Change the current value of a counter resource.

  • ≪ counter_get_value
  • counter_reset_value ≫
  • PHP Manual
  • Extended
  • Change the current value of a counter resource.

counter_bump_value

counter_bump_value — Change the current value of a counter resource.

설명

voidcounter_bump_value ( resource $counter , integer $offset )

counter_bump_value() updates the current value of a counter resource.

인수

counter
The counter resource to operate on.
offset
The amount by which to change the counter's value. Can be negative.

참고

  • counter_get_value() - Get the current value of a counter resource.
  • counter_reset_value() - Reset the current value of a counter resource.

User Contributed Notes

There are no user contributed notes for this page.


Reset the current value of a counter resource.

  • ≪ counter_bump_value
  • counter_get_meta ≫
  • PHP Manual
  • Extended
  • Reset the current value of a counter resource.

counter_reset_value

counter_reset_value — Reset the current value of a counter resource.

설명

voidcounter_reset_value ( resource $counter )

counter_reset_value() resets the current value of a counter resource to its original initial value.

인수

counter
The counter resource to operate on.

참고

  • counter_get_value() - Get the current value of a counter resource.
  • counter_bump_value() - Change the current value of a counter resource.

User Contributed Notes

There are no user contributed notes for this page.


Return a piece of metainformation about a counter resource.

  • ≪ counter_reset_value
  • counter_get_named ≫
  • PHP Manual
  • Extended
  • Return a piece of metainformation about a counter resource.

counter_get_meta

counter_get_meta — Return a piece of metainformation about a counter resource.

설명

mixed counter_get_meta ( resource $counter , integer $attribute )

counter_get_meta() returns metainformation about a counter resource.

인수

counter
The counter resource to operate on.
attribute
The metainformation to retrieve.

반환값

counter_get_meta() returns values of varying types based on which metainformation was requested.

User Contributed Notes

There are no user contributed notes for this page.


Retrieve an existing named counter as a resource.

  • ≪ counter_get_meta
  • The PHP 5 build system ≫
  • PHP Manual
  • Extended
  • Retrieve an existing named counter as a resource.

counter_get_named

counter_get_named — Retrieve an existing named counter as a resource.

설명

resource Counter::getNamed ( string $name )

counter_get_named() returns an existing counter by name if that name exists, or NULL otherwise.

인수

name
The counter name to search for.

반환값

counter_get_name() returns an existing counter by name if that name exists, or NULL otherwise.

User Contributed Notes

There are no user contributed notes for this page.


The PHP 5 build system

Table of Contents

  • Building PHP for extension development
  • The ext_skel script
  • Talking to the UNIX build system: config.m4
  • Talking to the Windows build system: config.w32

With all the functionality and flexibility available in PHP 5, it is no surprise that it consists of several thousand files and over one million lines of source code. Equally unsurprising is the necessity of a build system to manage so much data. This section describes how to set PHP up for extension development, the layout of an extension within the PHP source tree, and how to interface your extension with the build system.

User Contributed Notes

There are no user contributed notes for this page.


Building PHP for extension development

In a typical PHP installation, the need for high performance almost always results in optimization at the cost of debugging facilities. This is a reasonable tradeoff for production use, but when developing an extension it falls short. What we need is a build of PHP which will give us some hints what has gone wrong when something does.

The Zend Engine provides a memory manager which is capable of tracking memory leaks in extensions and providing detailed debugging information. This tracking is disabled by default, as is thread-safety. To turn them on, pass the --enable-debug and --enable-maintainer-zts options to configure, along with whatever options you typically use. For instructions on building PHP from source, see the instructions at 일반적인 설치 고려사항. A typical configure line might look like this:

$ ./configure --prefix=/where/to/install/php --enable-debug --enable-maintainer-zts --enable-cgi --enable-cli --with-mysql=/path/to/mysql

User Contributed Notes

rich dot d dot rich at gmail dot com 03-Apr-2011 12:51
This may be obvious, but if you are installing PHP under a home directory, don't enter '--prefix=~/place'. Use the full form '--prefix=/home/fred/place'.

Otherwise, it will half work and cause problems (because the shell char gets expanded in some places and not others).


The ext_skel script

A PHP extension is composed of several files common to all extensions. As the details of many of those files are similar from extension to extension, it can be laborious to duplicate the content for each one. Fortunately, there is a script which can do all of the initial setup for you. It's called ext_skel, and it's been distributed with PHP since 4.0.

Running ext_skel with no parameters produces this output in PHP 5.2.2:

php-5.2.2/ext$ ./ext_skel
./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
           [--skel=dir] [--full-xml] [--no-help]

  --extname=module   module is the name of your extension
  --proto=file       file contains prototypes of functions to create
  --stubs=file       generate only function stubs in file
  --xml              generate xml documentation to be added to phpdoc-cvs
  --skel=dir         path to the skeleton directory
  --full-xml         generate xml documentation for a self-contained extension
                     (not yet implemented)
  --no-help          don't try to be nice and create comments in the code
                     and helper functions to test if the module compiled
Generally, when developing a new extension the only parameters you will be interested in are --extname and --no-help. Unless you are already experienced with the structure of an extension, you will not want to use --no-help; specifying it causes ext_skel to leave out many helpful comments in the files it generates.

This leaves you with --extname, which tells ext_skel what the name of your extension is. This "name" is an all-lowercase identifier containing only letters and underscores which is unique among everything in the ext/ folder of your PHP distribution.

The --proto option is intended to allow the developer to specify a header file from which a set of PHP functions will be created, ostensibly for the purpose of developing an extension based on a library, but it often functions poorly with most modern header files. A test run on the zlib.h header resulted in a very large number of empty and nonsense prototypes in the ext_skel output files. The --xml and --full-xml options are entirely nonfunctional thus far. The --skel option can be used to specify a modified set of skeleton files to work from, a topic which is beyond the scope of this section.

User Contributed Notes

There are no user contributed notes for this page.


Talking to the UNIX build system: config.m4

The config.m4 file for an extension tells the UNIX build system what configure options your extension supports, what external libraries and includes you require, and what source files are to be compiled as part of it. A reference to all the commonly used autoconf macros, both PHP-specific and those built into autoconf, is given in the Zend Engine 2 API reference section.

Tip

The version of autoconf you have installed makes a difference when developing an extension. For PHP 5.3 and earlier, you will have the best results with autoconf version 2.13 but versions up to and including 2.59 will work. For PHP 5.4 and later the oldest autoconf version you can use is 2.59 and you will have better results with later versions.

Example #1 An example config.m4 file

dnl $Id$
dnl config.m4 for extension example
PHP_ARG_WITH(example, for example support,
[  --with-example[=FILE]       Include example support. File is the optional path to example-config])
PHP_ARG_ENABLE(example-debug, whether to enable debugging support in example,
[  --enable-example-debug        example: Enable debugging support in example], no, no)
PHP_ARG_WITH(example-extra, for extra libraries for example,
[  --with-example-extra=DIR      example: Location of extra libraries for example], no, no)

dnl Check whether the extension is enabled at all
if test "$PHP_EXAMPLE" != "no"; then

  dnl Check for example-config. First try any path that was given to us, then look in $PATH
  AC_MSG_CHECKING([for example-config])
  EXAMPLE_CONFIG="example-config"
  if test "$PHP_EXAMPLE" != "yes"; then
    EXAMPLE_PATH=$PHP_EXAMPLE
  else
    EXAMPLE_PATH=`$php_shtool path $EXAMPLE_CONFIG`
  fi

  dnl If a usable example-config was found, use it
  if test -f "$EXAMPLE_PATH" && test -x "$EXAMPLE_PATH" && $EXAMPLE_PATH --version > /dev/null 2>&1; then
    AC_MSG_RESULT([$EXAMPLE_PATH])
    EXAMPLE_LIB_NAME=`$EXAMPLE_PATH --libname`
    EXAMPLE_INCDIRS=`$EXAMPLE_PATH --incdirs`
    EXAMPLE_LIBS=`$EXAMPLE_PATH --libs`

    dnl Check that the library works properly
    PHP_CHECK_LIBRARY($EXAMPLE_LIB_NAME, example_critical_function,
    [
      dnl Add the necessary include dirs
      PHP_EVAL_INCLINE($EXAMPLE_INCDIRS)
      dnl Add the necessary libraries and library dirs
      PHP_EVAL_LIBLINE($EXAMPLE_LIBS, EXAMPLE_SHARED_LIBADD)
    ],[
      dnl Bail out
      AC_MSG_ERROR([example library not found. Check config.log for more information.])
    ],[$EXAMPLE_LIBS]
    )
  else
    dnl No usable example-config, bail
    AC_MSG_RESULT([not found])
    AC_MSG_ERROR([Please check your example installation.])
  fi

  dnl Check whether to enable debugging
  if test "$PHP_EXAMPLE_DEBUG" != "no"; then
    dnl Yes, so set the C macro
    AC_DEFINE(USE_EXAMPLE_DEBUG,1,[Include debugging support in example])
  fi

  dnl Check for the extra support
  if test "$PHP_EXAMPLE_EXTRA" != "no"; then
    if test "$PHP_EXAMPLE_EXTRA" == "yes"; then
      AC_MSG_ERROR([You must specify a path when using --with-example-extra])
    fi

    PHP_CHECK_LIBRARY(example-extra, example_critical_extra_function,
    [
      dnl Add the neccessary paths
      PHP_ADD_INCLUDE($PHP_EXAMPLE_EXTRA/include)
      PHP_ADD_LIBRARY_WITH_PATH(example-extra, $PHP_EXAMPLE_EXTRA/lib, EXAMPLE_SHARED_LIBADD)
      AC_DEFINE(HAVE_EXAMPLEEXTRALIB,1,[Whether example-extra support is present and requested])
      EXAMPLE_SOURCES="$EXAMPLE_SOURCES example_extra.c"
    ],[
      AC_MSG_ERROR([example-extra lib not found. See config.log for more information.])
    ],[-L$PHP_EXAMPLE_EXTRA/lib]
    )
  fi

  dnl Finally, tell the build system about the extension and what files are needed
  PHP_NEW_EXTENSION(example, example.c $EXAMPLE_SOURCES, $ext_shared)
  PHP_SUBST(EXAMPLE_SHARED_LIBADD)
fi

A short introduction to autoconf syntax

config.m4 files are written using the GNU autoconf syntax. It can be described in a nutshell as shell scripting augmented by a powerful macro language. Comments are delimited by the string dnl, and strings are quoted using left and right brackets (e.g. [ and ]). Quoting of strings can be nested as many times as needed. A full reference to the syntax can be found in the autoconf manual at » http://www.gnu.org/software/autoconf/manual/.

PHP_ARG_*: Giving users the option

The very first thing seen in the example config.m4 above, aside from a couple of comments, are three lines using PHP_ARG_WITH() and PHP_ARG_ENABLE(). These provide configure with the options and help text seen when running ./configure --help. As the names suggest, the difference between the two is whether they create a --with-* option or an --enable-* option. Every extension should provide at least one or the other with the extension name, so that users can choose whether or not to build the extension into PHP. By convention, PHP_ARG_WITH() is used for an option which takes a parameter, such as the location of a library or program required by an extension, while PHP_ARG_ENABLE() is used for an option which represents a simple flag.

Example #2 Sample configure output

$ ./configure --help
...
  --with-example[=FILE]       Include example support. FILE is the optional path to example-config
  --enable-example-debug        example: Enable debugging support in example
  --with-example-extra=DIR      example: Location of extra libraries for example
...

$ ./configure --with-example=/some/library/path/example-config --disable-example-debug --with-example-extra=/another/library/path
...
checking for example support... yes
checking whether to enable debugging support in example... no
checking for extra libraries for example... /another/library/path
...

Note:

Regardless of the order in which options are specified on the command line when configure is called, the checks will be run in the order they are specified in config.m4.

Processing the user's choices

Now that config.m4 can provide the user with some choices of what to do, it's time to act upon those choices. In the example above, the obvious default for all three options, if any of them are unspecified, is "no". As a matter of convention, it is best to use this as the default for the option which enables the extension, as it will be overridden by phpize for extensions built separately, and should not clutter the extension space by default when being built into PHP. The code to process the three options is by far the most complicated.

Handling the --with-example[=FILE] option

The first check made of the --with-example[=FILE] option is whether it was set at all. As this option controls the inclusion of the entire extension, if it was unspecified, given in the negative form (--without-example ), or given the value "no", nothing else is done at all. In the example above, it is specified with the value /some/library/path/example-config, so the first test succeeds.

Next, the code calls AC_MSG_CHECKING(), an autoconf macro which outputs a standard "checking for something" line, and checks whether the user gave an explicit path to the fictional example-config. In this example, PHP_EXAMPLE got the value /some/library/path/example-config, which is now copied into the EXAMPLE_PATH variable. Had the user specified only --with-example , the code would have executed $php_shtool path $EXAMPLE_CONFIG, which would try to guess the location of example-config using the user's current PATH. Either way, the next step is to check whether the chosen EXAMPLE_PATH is a regular file, is executable, and can be run successfully. If so, AC_MSG_RESULT() is called, which completes the output line started by AC_MSG_CHECKING(). Otherwise, AC_MSG_ERROR() is called, which prints the given message and halts configure immediately.

The code now determines some site-specific configuration information by running example-config several times. The next call is to PHP_CHECK_LIBRARY(), a macro provided by the PHP buildsystem as a wrapper around autoconf's AC_CHECK_LIB(). PHP_CHECK_LIBRARY() attempts to compile, link, and run a program which calls the symbol specified by the second parameter in the library specified by the first, using the string given in the fifth as extra linker options. If the attempt succeeds, the script given in the third parameter is run. This script tells the PHP buildsystem to extract include paths, library paths, and library names from the raw option strings example-config provided. If the attempt fails, the script in the fourth parameter is run instead. In this case, AC_MSG_ERROR() is called to stop processing.

Handling the --enable-example-debug option

Processing the --enable-example-debug is much simpler. A simple check for its truth value is performed. If that check succeeds, AC_DEFINE() is called to make the C macro USE_EXAMPLE_DEBUG available to the source of the extension. The third parameter is a comment string for config.h; it is safe to leave this empty, and often is.

Handling the --with-example-extra=DIR option

For the sake of this example, the fictional "extra" functionality requested by the --with-example-extra=DIR option does not share the fictional example-config program, nor does it have any default paths to search. Therefore, the user is required to provide the installation prefix of the necessary library. This setup is somewhat unlikely in a real-world extension, but is considered illustrative.

The code begins in a now-familiar way by checking the truth value of PHP_EXAMPLE_EXTRA. If a negative form was provided, no further processing is done; the user did not request extra functionality. If a positive form was provided without a parameter, AC_MSG_ERROR() is called to halt processing. The next step is another invocation of PHP_CHECK_LIBRARY(). This time, since there is no set of predefined compiler options provided, PHP_ADD_INCLUDE() and PHP_ADD_LIBRARY_WITH_PATH() are used to construct the necessary include paths, library paths, and library flags for the extra functionality. AC_DEFINE() is also called to indicate to the code that the extra functionality was both requested and available, and a variable is set to tell later code that there are extra source files to build. If the check fails, the familiar AC_MSG_ERROR() is called. A different way to handle the failure would have been to call AC_MSG_WARNING() instead, e.g.:

AC_MSG_WARNING([example-extra lib not found. example will be built without extra functionality.])

In this case, configure would print a warning message rather than an error, and continue processing. Which way such failures are handled is a design decision left to the extension developer.

Telling the buildsystem what was decided

With all the necessary includes and libraries specified, with all the options processed and macros defined, one more thing remains to be done: The build system must be told to build the extension itself, and which files are to be used for that. To do this, the PHP_NEW_EXTENSION() macro is called. The first parameter is the name of the extension, which is the same as the name of the directory containing it. The second parameter is the list of all source files which are part of the extension. See PHP_ADD_BUILD_DIR() for information about adding source files in subdirectories to the build process. The third parameter should always be $ext_shared, a value which was determined by configure when PHP_ARG_WITH() was called for --with-example[=FILE] . The fourth parameter specifies a "SAPI class", and is only useful for extensions which require the CGI or CLI SAPIs specifically. It should be left empty in all other cases. The fifth parameter specifies a list of flags to be added to CFLAGS while building the extension; the sixth is a boolean value which, if "yes", will force the entire extension to be built using $CXX instead of $CC. All parameters after the third are optional. Finally, PHP_SUBST() is called to enable shared builds of the extension. See Extension FAQs for more information on disabling support for building an extension in shared mode.

The counter extension's config.m4 file

The counter extension previously documented has a much simpler config.m4 file than that described above, as it doesn't make use of many buildsystem features. This is a preferred method of operation for any extension that doesn't use an external or bundled library.

Example #3 counter's config.m4 file

dnl
$
Id$
dnl config.m4 for extension counter

PHP_ARG_ENABLE(counter, for counter support,
[  --enable-counter            Include counter support])

dnl Check whether the extension is enabled at all
if test "$PHP_COUNTER" != "no"; then
  dnl Finally, tell the build system about the extension and what files are needed
  PHP_NEW_EXTENSION(counter, counter.c counter_util.c, $ext_shared)
  PHP_SUBST(COUNTER_SHARED_LIBADD)
fi

User Contributed Notes

There are no user contributed notes for this page.


Talking to the Windows build system: config.w32

An extension's config.w32 file is similar in usage to the config.m4 file, with two critical differences: first, it is used for Windows builds, and second, it is written in JavaScript. This section makes no attempt to cover JavaScript syntax. For the moment, this section is incomplete in lieu of a Win32 testbed, and an experimental-only port of the example config.m4 is the only example provided.

Example #1 An example config.w32 file

// $Id$
// vim:ft=javascript
ARG_WITH("example", "for example support", "no");
ARG_ENABLE("example-debug", "for debugging support in example", "no")
ARG_WITH("example-extra", "for extra functionality in example", "no")
if (PHP_EXAMPLE != "no") {
    if (CHECK_LIB("libexample.lib", "example", PHP_EXAMPLE) &&
        CHECK_HEADER_ADD_INCLUDE("example.h", "CFLAGS_EXAMPLE", PHP_EXAMPLE + "\\include")) {

        if (PHP_EXAMPLE_DEBUG != "no") {
            AC_DEFINE('USE_EXAMPLE_DEBUG', 1, 'Debug support in example');
        }

        if (PHP_EXAMPLE_EXTRA != "no" &&
            CHECK_LIB("libexample-extra.lib", "example", PHP_EXAMPLE) &&
            CHECK_HEADER_ADD_INCLUDE("example-extra.h", "CFLAGS_EXAMPLE", PHP_EXAMPLE + ";" + PHP_PHP_BUILD + "\\include") {
            AC_DEFINE('HAVE_EXAMPLEEXTRA', 1, 'Extra functionality in example');
            HAVE_EXTRA = 1;
        } else {
            WARNING( "extra example functionality not enabled, lib not found" );
        }

        EXTENSION("example", "example.c");
        if (HAVE_EXTRA == 1) {
            ADD_SOURCES("example-extra.c");
        }
    } else {
        WARNING( "example not enabled; libraries not found" );
    }
}

The counter extension's config.w32 file

The counter extension previously documented has a much simpler config.w32 file than that described above, as it doesn't make use of many buildsystem features.

Example #2 counter's config.w32 file

// $Id$
// vim:ft=javascript
ARG_ENABLE("counter", "for counter support", "no");
if (PHP_COUNTER != "no") {
    EXTENSION("counter", "counter.c");
    ADD_SOURCE("counter-util.c");
}

User Contributed Notes

There are no user contributed notes for this page.


Extension structure

Table of Contents

  • Files which make up an extension
  • Basic constructs
  • The zend_module structure
  • Extension globals
  • Life cycle of an extension
  • Testing an extension

Many extension-writing guides focus on simple examples first and ignore the requirements of more complex implementations until later. Often such guides must repeat themselves over and over in order to describe these new features. This section describes extension structure from the perspective of a mature, practical implementation, in order to prepare users for needs and issues they will almost always encounter in the process of extension development.

User Contributed Notes

There are no user contributed notes for this page.


Files which make up an extension

Whether created by hand, using ext_skel, or by an alternate extension generator, such as » CodeGen, all extensions will have at least four files:

config.m4

UNIX build system configuration (see Talking to the UNIX build system: config.m4)

config.w32

Windows buildsystem configuration (see Talking to the Windows build system: config.w32)

php_counter.h

When building an extension as static module into the PHP binary the build system expects a header file with php_ prepended to the extension name which includes a declaration for a pointer to the extension's module structure. This file usually contains additional macros, prototypes, and globals, just like any header.

counter.c

Main extension source file. By convention, the name of this file is the extension name, but this is not a requirement. This file contains the module structure declaration, INI entries, management functions, userspace functions, and other requirements of an extension.

The buildsystem files are discussed elsewhere; this section concentrates on the rest. These four files make up the bare minimum for an extension, which may also contain any number of headers, source files, unit tests, and other support files. The list of files in the counter extension might look like this:

Example #1 Files in the counter extension, in no particular order

ext/
 counter/
  .svnignore
  config.m4
  config.w32
  counter_util.h
  counter_util.c
  php_counter.h
  counter.c
  package.xml
  CREDITS
  tests/
   critical_function_001.phpt
   critical_function_002.phpt
   optional_function_001.phpt
   optional_function_002.phpt

Non-source files

The .svnignore file is used for extensions which are checked into the PHP Subversion repository (usually » PECL); the one generated by ext_skel contains:

.deps
*.lo
*.la

These lines tell Subversion to ignore interim files generated by the PHP buildsystem. This is only a convenience, and can be omitted completely without ill effect.

The CREDITS file lists the contributors and/or maintainers of the extension in plain text format. The main purpose of this file is generating the credits information for bundled extensions as used by phpcredits(). By convention the first line of the file should hold the name of the extension, the second a comma separated list of contributors. The contributors are usually ordered by the chronological order of their contributions. In a » PECL package, this information is already maintained in package.xml, for example. This is another file which can be omitted without ill effect.

The package.xml file is specific to » PECL-based extensions; it is a metainformation file which gives details about an extension's dependencies, authors, installation requirements, and other tidbits. In an extension not being hosted in » PECL, this file is extraneous.

User Contributed Notes

There are no user contributed notes for this page.


Basic constructs

C is a very low-level language by modern definitions. This means that it has no built-in support for many features that PHP takes for granted, such as reflection, dynamic module loading, bounds checking, threadsafe data management and various useful data structures including linked lists and hash tables. At the same time, C is a common denominator of language support and functionality. Given enough work, none of these concepts are impossible; the Zend Engine uses them all.

A lot of effort has gone into making the Zend API both extensible and understandable, but C forces certain necessary declarations upon any extension that to an inexperienced eye seem redundant or plain unnecessary. All of those constructs, detailed in this section, are "write once and forget" in Zend Engine 2 and 3. Here are some excerpts from the pre-generated php_counter.h and counter.c files created by PHP 5.3's ext_skel, showing the pre-generated declarations:

Note: The astute reader will notice that there are several declarations in the real files that aren't shown here. Those declarations are specific to various Zend subsystems and are discussed elsewhere as appropriate.

extern zend_module_entry counter_module_entry;
#define phpext_counter_ptr &counter_module_entry

#ifdef PHP_WIN32
#    define PHP_COUNTER_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#    define PHP_COUNTER_API __attribute__ ((visibility("default")))
#else
#    define PHP_COUNTER_API
#endif

#ifdef ZTS
#include "TSRM.h"
#endif

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_counter.h"

/* ... */

#ifdef COMPILE_DL_COUNTER
ZEND_GET_MODULE(counter)
#endif
  • The lines concerning counter_module_entry declare a global variable, and a macroed pointer to it, which contains the zend_module_entry for the extension. Despite the later discussion regarding the drawbacks of "true" globals, this usage is intentional; Zend takes precautions to avoid misusing this variable.
  • PHP_COUNTER_API is declared for use by non-PHP functions the module intends to export for the use of other modules. The counter extension doesn't declare any of these, and in the final version of the header file, this macro has been removed. The PHPAPI macro is declared identically elsewhere and is used by the standard extension to make the phpinfo() utility functions available to other extensions.
  • The include of TSRM.h is skipped if PHP, or the extension, isn't being compiled with thread-safety, since in that case TSRM isn't used.
  • A standard list of includes, especially the extension's own php_counter.h, is given. config.h gives the extension access to determinations made by configure. php.h is the gateway to the entire PHP and Zend APIs. php_ini.h adds the APIs for runtime configuration (INI) entries. Not all extensions will use this. Finally, ext/standard/info.h imports the aforementioned phpinfo() utility API.
  • COMPILE_DL_COUNTER will only be defined by configure if the counter extension is both enabled and wants to be built as a dynamically loadable module instead of being statically linked into PHP. ZEND_GET_MODULE defines a tiny function which Zend can use to get the extension's zend_module_entry at runtime.

    Note: The astute reader who has peeked into main/php_config.h after trying to build with the counter module enabled statically may have noticed that there is also a HAVE_COUNTER constant defined that the source code doesn't check for. There's a simple reason this check isn't done: It's unnecessary. If the extension isn't enabled, the source file will never be compiled.

User Contributed Notes

Sam at SimpleSamples dot info 11-Sep-2016 02:12
Comparing C to PHP is unfair since C++ has so much more than C. You can say that you would have much more if C++ could be used but since it is not you must use what C offers. I am not criticizing PHP but it seems inappropriate to make PHP appear to be better than another language in a misleading way.

Go ahead and delete this if you want to but I hope the documentation is revised appropriately.


The zend_module structure

The main source file of a PHP extension contains several new constructs for a C programmer. The most important of these, the one touched first when starting a new extension, is the zend_module structure. This structure contains a wealth of information that tells the Zend Engine about the extension's dependencies, version, callbacks, and other critical data. The structure has mutated considerably over time; this section will focus on the structure as it has appeared since PHP 5.2, and will identify the very few parts which have changed in PHP 5.3.

The zend_module declaration from counter.c looks like this before any code has been written. The example file was generated by ext_skel --extname=counter, with some obsolete constructs removed:

Example #1 zend_module declaration in the counter extension

/* {{{ counter_module_entry
 */
zend_module_entry counter_module_entry = {
    STANDARD_MODULE_HEADER,
    "counter",
    counter_functions,
    PHP_MINIT(counter),
    PHP_MSHUTDOWN(counter),
    PHP_RINIT(counter),        /* Replace with NULL if there's nothing to do at request start */
    PHP_RSHUTDOWN(counter),    /* Replace with NULL if there's nothing to do at request end */
    PHP_MINFO(counter),
    "0.1", /* Replace with version number for your extension */
    STANDARD_MODULE_PROPERTIES
};
/* }}} */

This may look a bit daunting at first glance, but most of it is very simple to understand. Here's the declaration of zend_module from zend_modules.h in PHP 5.3:

Example #2 zend_module definition in PHP 5.3

struct _zend_module_entry {
    unsigned short size;
    unsigned int zend_api;
    unsigned char zend_debug;
    unsigned char zts;
    const struct _zend_ini_entry *ini_entry;
    const struct _zend_module_dep *deps;
    const char *name;
    const struct _zend_function_entry *functions;
    int (*module_startup_func)(INIT_FUNC_ARGS);
    int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);
    int (*request_startup_func)(INIT_FUNC_ARGS);
    int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);
    void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);
    const char *version;
    size_t globals_size;
#ifdef ZTS
    ts_rsrc_id* globals_id_ptr;
#else
    void* globals_ptr;
#endif
    void (*globals_ctor)(void *global TSRMLS_DC);
    void (*globals_dtor)(void *global TSRMLS_DC);
    int (*post_deactivate_func)(void);
    int module_started;
    unsigned char type;
    void *handle;
    int module_number;
};

Many of these fields will never be touched by an extension writer. There are a number of standard macros that set them to their proper values automatically. The macro STANDARD_MODULE_HEADER fills in everything up to the deps field. Alternatively, the STANDARD_MODULE_HEADER_EX will leave the deps field empty for the developer's use. The developer is always responsible for everything from name to version. After that, the STANDARD_MODULE_PROPERTIES macro will fill in the rest of the structure, or the STANDARD_MODULE_PROPERTIES_EX macro can be used to leave the extension globals and post-deactivation function fields unfilled. Most modern extensions will make use of module globals.

Note:

This table gives the values that each field would have if the developer were to fill in the structure entirely by hand, without recourse to any of the shortcut macros. This is not recommended. The "correct" values for many fields may change. Use the macros whenever possible.

Module structure field values
Field | Value | Description
size [1] [2] [3] | sizeof(zend_module_entry) | The size in bytes of the structure.
zend_api [1] [2] [3] | ZEND_MODULE_API_NO | The version of the Zend API this module was compiled against.
zend_debug [1] [2] [3] | ZEND_DEBUG | A flag indicating whether the module was compiled with debugging turned on.
zts [1] [2] [3] | USING_ZTS | A flag indicating whether the module was compiled with ZTS (TSRM) enabled (see Memory management).
ini_entry [1] [3] | NULL | This pointer is used internally by Zend to keep a non-local reference to any INI entries declared for the module.
deps [3] | NULL | A pointer to a list of dependencies for the module.
name | "mymodule" | The name of the module. This is the short name, such as "spl" or "standard".
functions | mymodule_functions | A pointer to the module's function table, which Zend uses to expose functions in the module to user space.
module_startup_func | PHP_MINIT(mymodule) | A callback function that Zend will call the first time a module is loaded into a particular instance of PHP.
module_shutdown_func | PHP_MSHUTDOWN(mymodule) | A callback function that Zend will call the when a module is unloaded from a particular instance of PHP, typically during final shutdown.
request_startup_func | PHP_RINIT(mymodule) | A callback function that Zend will call at the beginning of each request. This should be as short as possible or NULL as calling this has costs on every request.
request_shutdown_func | PHP_RSHUTDOWN(mymodule) | A callback function that Zend will call at the end of each request. This should be as short as possible or NULL as calling this has costs on every request.
info_func | PHP_MINFO(mymodule) | A callback function that Zend will call when the phpinfo() function is called.
version | NO_VERSION_YET | A string giving the version of the module, as specified by the module developer. It is recommended that the version number be either in the format expected by version_compare() (e.g. "1.0.5-dev"), or a CVS or SVN revision number (e.g. "$Rev: 322138 $").
globals_size [1] [4] [5] [6] | sizeof(zend_mymodule_globals) | The size of the data structure containing the module's globals, if any.
globals_id_ptr [1] [4] [5] [6] [7] | &mymodule_globals_id | Only one of these two fields will exist, depending upon whether the USING_ZTS constant is TRUE. The former is an index into TSRM's allocation table for the module's globals, and the latter is a pointer directly to the globals.
globals_ptr [1] [4] [5] [6] [8] | &mymodule_globals
globals_ctor [4] [5] [6] | PHP_GINIT(mymodule) | This funtion is called to initialize a module's globals before any module_startup_func.
globals_dtor [4] [5] [6] | PHP_GSHUTDOWN(mymodule) | This funtion is called to deallocate a module's globals after any module_shutdown_func.
post_deactivate_func [4] | ZEND_MODULE_POST_ZEND_DEACTIVATE_N(mymodule) | This function is called by Zend after request shutdown. It is rarely used.
module_started [1] [9] [4] | 0 | These fields are used for Zend's internal tracking information.
type [1] [9] [4] | 0
handle [1] [9] [4] | NULL
module_number [1] [9] [4] | 0
[1] This field is not intended for use by module developers. [2] This field is filled in by STANDARD_MODULE_HEADER_EX. [3] This field is filled in by STANDARD_MODULE_HEADER. [4] This field is filled in by STANDARD_MODULE_PROPERTIES. [5] This field is filled in by NO_MODULE_GLOBALS. [6] This field is filled in by PHP_MODULE_GLOBALS. [7] This field only exists when USING_ZTS is TRUE. [8] This field only exists when USING_ZTS is FALSE. [9] This field is filled in by STANDARD_MODULE_PROPERTIES_EX.

Filling in the structure in a practical situation

With all these fields to play with, it can be confusing to know which to use for what purpose. Here is the zend_module definition from the "counter" example extension after updating it to its final form.

Example #3 Counter extension module definition

/* {{{ counter_module_entry
 */
zend_module_entry counter_module_entry = {
    STANDARD_MODULE_HEADER,
    "counter",
    counter_functions,
    PHP_MINIT(counter),
    PHP_MSHUTDOWN(counter),
    PHP_RINIT(counter),
    PHP_RSHUTDOWN(counter),
    PHP_MINFO(counter),
    NO_VERSION_YET,
    PHP_MODULE_GLOBALS(counter),
    PHP_GINIT(counter),
    PHP_GSHUTDOWN(counter),
    NULL,
    STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
  • STANDARD_MODULE_HEADER is used since this module doesn't define any dependencies.
  • "counter" is the extension's name, and is used to define the various callback functions the module passes to Zend. "counter" uses module, globals, and request functions at startup and shutdown times, and provides information to phpinfo(), so all seven callbacks are defined.
  • It is assumed that there is a variable of type zend_function_entry * named counter_functions earlier in the file that contains the module definition, listing the functions the module exports to userspace.
  • NO_VERSION_YET is a particularly nice way of telling Zend the module doesn't have a version. It might have been more correct to place "1.0" here instead in a real module.
  • "counter" uses per-module globals, so PHP_MODULE_GLOBALS is used
  • This module has no post-deactivate function, so NULL is used.
  • Since this module does use globals, STANDARD_MODULE_PROPERTIES_EX is used to finish the structure.

What's changed between 5.2 and 5.3?

Nothing. The only differences in the zend_module structure between PHP 5.2 and PHP 5.3 are a few const keywords.

User Contributed Notes

There are no user contributed notes for this page.


Extension globals

Introduction to globals in a PHP extension

In a language such as C, a "global" variable is a variable that can be accessed from any function without any extra declaration. These traditional globals have a few drawbacks:

  • Barring any special options passed to the compiler, a global variable can be accessed and changed by any piece of code anywhere in the program, whether or not that code should be doing so.
  • A typical global variable is not thread safe.
  • The names of global variables are as global as the variables themselves.

A PHP extension's globals are more properly called the "extension state", since most modules must remember what they're doing between function calls. The "counter" extension is a perfect example of this need: The basic interface calls for a counter with a persistent value. A programmer new to Zend and PHP might do something like this in counter.c to store that value:

Example #1 The wrong way to store the basic counter interface's value

/* ... */
static long basic_counter_value;

/* ... */

PHP_FUNCTION(counter_get)
{
    RETURN_LONG(basic_counter_value);
}

On the surface this appears a viable solution, and indeed in a simple test it would function correctly. However, there are a number of situations in which more than one copy of PHP is running in the same thread, which means more than one instance of the counter module. Suddenly these multiple threads are sharing the same counter value, which is clearly undesirable. Another problem shows itself when considering that another extension might someday happen to have a global with the same name, and due to the rules of C scoping, this has the potential to cause a compile failure, or worse, a runtime error. Something more elaborate is needed, and so exists Zend's support for thread-safe per-module globals.

Declaring module globals

Whether a module uses only a single global or dozens, they must be defined in a structure, and that structure must be declared. There are some macros that assist with doing so in a way that avoids name conflicts between modules: ZEND_BEGIN_MODULE_GLOBALS(), ZEND_END_MODULE_GLOBALS(), and ZEND_DECLARE_MODULE_GLOBALS(). All three take as a parameter the short name of the module, which in the case of the counter module is simply "counter". Here is the global structure declaration from php_counter.h:

Example #2 The counter module's globals

ZEND_BEGIN_MODULE_GLOBALS(counter)
    long        basic_counter_value;
ZEND_END_MODULE_GLOBALS(counter)

And this is the declaration from counter.c:

Example #3 The counter module's global structure declaration

ZEND_DECLARE_MODULE_GLOBALS(counter)

Accessing module globals

As discussed above, per-module globals are declared inside a C structure whose name is obscured by Zend macros. As a result, the ideal way to access members of this structure is by the use of further macros. Accordingly, most if not all extensions which have globals have a declaration like this somewhere in their header file:

Example #4 Accessor macros for per-module globals

#ifdef ZTS
#define COUNTER_G(v) TSRMG(counter_globals_id, zend_counter_globals *, v)
#else
#define COUNTER_G(v) (counter_globals.v)
#endif

Note: This could have been generalized into a macro of its own by the Zend API, but as of PHP 5.3 (and PHP 6 at the time of this writing), that hasn't happened. The global accessor construct is written into the header by ext_skel and thus is generally left alone by extension writers, unless they wish to change the name of the accessor macro.

Note: COUNTER_G was the name given to the macro by ext_skel, but it's not necessary for it to have that name and could just as easily be called FOO instead.

Any code in the counter extension that accesses a global must thus wrap it in the macro COUNTER_G.

Warning

Any function which accesses globals must either be declared by Zend macros, have TSRMLS_DC as its last argument, or call the macro TSRMLS_FETCH before accessing the globals. See the TSRM documentation for more information.

So with all of that in mind, here is our new version of the counter_get():

Example #5 The right way to store the basic counter interface's value

/* php_counter.h */
ZEND_BEGIN_MODULE_GLOBALS(counter)
    long        basic_counter_value;
ZEND_END_MODULE_GLOBALS(counter)

#ifdef ZTS
#define COUNTER_G(v) TSRMG(counter_globals_id, zend_counter_globals *, v)
#else
#define COUNTER_G(v) (counter_globals.v)
#endif

/* counter.c */
ZEND_DECLARE_MODULE_GLOBALS(counter)

/* ... */

PHP_FUNCTION(counter_get)
{
    RETURN_LONG(COUNTER_G(basic_counter_value));
}

This is a correct implementation. It is not, however, a complete one. The section Life cycle of an extension explains why.

User Contributed Notes

There are no user contributed notes for this page.


Life cycle of an extension

A PHP extension goes through several phases during its lifetime. All of these phases are opportunities for the developer to perform various initialization, termination, or informational functions. The Zend API allows for hooks into five separate phases of an extension's existence, apart from calls by PHP functions.

Loading, unloading, and requests

As the Zend engine runs, it processes one or more "requests" from its client. In the traditional CGI implementation, this corresponds to one execution of a process. However, many other implementations, most notably the Apache module, can map many requests onto a single PHP process. A PHP extension may thus see many requests in its lifetime.

Overview

  • In the Zend API, a module is loaded into memory only once when the associated PHP process starts up. Each module receives a call to the "module initialization" function specified in its zend_module structure as it is loaded.
  • Whenever the associated PHP process starts to handle a request from its client - i.e. whenever the PHP interpreter is told to start working - each module receives a call to the "request initialization" function specified in its zend_module structure.
  • Whenever the associated PHP process is done handling a request, each module receives a call to the "request termination" function specified in its zend_module structure.
  • A given module is unloaded from memory when its associated PHP process is shut down in an orderly manner. The module receives a call to the "module termination" function specified in its zend_module structure at this time.

What to do, and when to do it

There are many tasks that might be performed at any of these four points. This table details where many common initialization and termination tasks belong.

What to do, and when to do it
Module initialization/termination | Request initialization/termination
Allocate/deallocate and initialize module global variables | Allocate/deallocate and initialize request-specific variables
Register/unregister class entries |
Register/unregister INI entries |
Register constants |

The phpinfo() callback

Aside from globals initialization and certain rarely-used callbacks, there is one more part of a module's lifecycle to examine: A call to phpinfo(). The output a user sees from this call, whether text or HTML or anything else, is generated by each individual extension that is loaded into the PHP interpreter at the time the call is made.

To provide for format-neutral output, the header "ext/standard/info.h" provides an array of functions to produce standardized display elements. Specifically, several functions which create the familiar tables exist:

php_info_print_table_start()
Open a table in phpinfo() output. Takes no parameters.
php_info_print_table_header()
Print a table header in phpinfo() output. Takes one parameter, the number of columns, plus the same number of char * parameters which are the texts for each column heading.
php_info_print_table_row()
Print a table row in phpinfo() output. Takes one parameter, the number of columns, plus the same number of char * parameters which are the texts for each column content.
php_info_print_table_end()
Close a table formerly opened by php_info_print_table_start(). Takes no parameters.

Using these four functions, it is possible to produce status information for nearly any combination of features in an extension. Here is the information callback from the counter extension:

Example #1 counter's PHP_MINFO function

/* {{{ PHP_MINFO(counter) */
PHP_MINFO_FUNCTION(counter)
{
    char        buf[10];

    php_info_print_table_start();
    php_info_print_table_row(2, "counter support", "enabled");
    snprintf(buf, sizeof(buf), "%ld", COUNTER_G(basic_counter_value));
    php_info_print_table_row(2, "Basic counter value", buf);
    php_info_print_table_end();
}
/* }}} */

User Contributed Notes

There are no user contributed notes for this page.


Testing an extension

One of the accepted requirements of good programming is testing. PHP provides a test harness for unit regression tests, in the form of a script called run-tests.php.

Tests for a particular module are stored in the tests/ subdirectory of the module's folder, and have a .phpt extension. For details of the PHPT format, see » http://qa.php.net/.

User Contributed Notes

There are no user contributed notes for this page.


PDO Driver How-To

Table of Contents

  • Prerequisites
  • Preparation and Housekeeping
  • Fleshing out your skeleton
  • Building
  • Testing
  • Packaging and distribution
  • pdo_dbh_t definition
  • pdo_stmt_t definition
  • Constants
  • Error handling

The purpose of this How-To is to provide a basic understanding of the steps required to write a database driver that interfaces with the PDO layer. Please note that this is still an evolving API and as such, subject to change. This document was prepared based on version 0.3 of PDO. The learning curve is steep; expect to spend a lot of time on the prerequisites.

User Contributed Notes

There are no user contributed notes for this page.


Prerequisites

The following is list of prerequisites and assumptions needed for writing a PDO database driver:

  1. A working target database, examples, demos, etc. working as per vendor specifications;

  2. A working development environment:

    1. Linux: standard development tools, gcc, ld, make, autoconf, automake, etc., versions dependent on distribution;

    2. Other Unix: standard development tools supplied by vendor plus the GNU development tool set;

    3. Win32: Visual Studio compiler suite;

  3. A working PHP environment version 5.0.3 or higher with a working PEAR extension version 1.3.5 or higher;

  4. A working PDO environment (can be installed using 'sudo pecl install PDO'), including the headers which will be needed to access the PDO type definitions and function declarations;

  5. A good working knowledge of the C programming language;

  6. A good working knowledge of the way to write a PHP extension; George Schlossnagle's Advanced PHP Programming (published by Developer's Library, chapters 21 and 22) is recommended;

  7. Finally, a familiarity with the Zend API that forms the heart of PHP, in particular paying attention to the memory management aspects.

User Contributed Notes

There are no user contributed notes for this page.


Preparation and Housekeeping

Source directory layout

The source directory for a typical PDO driver is laid out as follows, where SKEL represents a shortened form of the name of the database that the driver is going to connect to. Even though SKEL is presented here in uppercase (for clarity), the convention is to use lowercase characters.

pdo_SKEL/
  config.m4                  # unix build script
  config.w32                 # win32 build script
  CREDITS
  package.xml                # meta information about the package
  pdo_SKEL.c                 # standard PHP extension glue
  php_pdo_SKEL.h
  php_pdo_SKEL_int.h         # driver private header
  SKEL_dbh.c                 # contains the implementation of the PDO driver interface
  SKEL_stmt.c                # contains the implementation of the PDO statement interface
  tests/

The contents of these files are defined later in this document.

Creating a skeleton

The easiest way to get started is to use the ext_skel shell script found in the PHP build tree in the ext directory. This will build a skeleton directory containing a lot of the files listed above. It can be build by executing the following command from within the ext directory:

./ext_skel --extname=pdo_SKEL

This will generate a directory called pdo_SKEL containing the skeleton files that you can then modify. This directory should then be moved out of the php extension directory. PDO is a PECL extension and should not be included in the standard extension directory. As long as you have PHP and PDO installed, you should be able to build from any directory.

Standard Includes

Build Specific Headers

The header file config.h is generated by the configure process for the platform for the which the driver is being built. If this header is present, the HAVE_CONFIG_H compiler variable is set. This variable should be tested for and if set, the file config.h should be included in the compilation unit.

PHP Headers

The following standard public php headers should be included in each source module:

  1. php.h

  2. php_ini.h

  3. ext/standard/info.h

PDO Interface Headers

The following standard public PDO header files are also included in each source module:

pdo/php_pdo.h

This header file contains definitions of the initialization and shutdown functions in the main driver as well as definitions of global PDO variables.

pdo/php_pdo_driver.h

This header contains the types and API contracts that are used to write a PDO driver. It also contains method signature for calling back into the PDO layer and registering/unregistering your driver with PDO. Most importantly, this header file contains the type definitions for PDO database handles and statements. The two main structures a driver has to deal with, pdo_dbh_t and pdo_stmt_t, are described in more detail in Appendix A and B.

Driver Specific Headers

The typical PDO driver has two header files that are specific to the database implementation. This does not preclude the use of more depending on the implementation. The following two headers are, by convention, standard:

php_pdo_SKEL.h

This header file is virtually an exact duplicate in functionality and content of the previously defined pdo/php_pdo.h that has been specifically tailored for your database. If your driver requires the use of global variables they should be defined using the ZEND_BEGIN_MODULE_GLOBALS and ZEND_END_MODULE_GLOBALS macros. Macros are then used to access these variables. This macro is usually named PDO_SKEL_G(v) where v is global variable to be accessed. Consult the Zend programmer documentation for more information.

php_pdo_SKEL_int.h

This header file typically contains type definitions and function declarations specific to the driver implementation. It also should contain the db specific definitions of a pdo_SKEL_handle and pdo_SKEL_stmt structures. These are the names of the private data structures that are then referenced by the driver_data members of the handle and statement structures.

Optional Headers

Depending on the implementation details for a particular driver it may be necessary to include the following header:

#include <zend_exceptions.h>

User Contributed Notes

There are no user contributed notes for this page.


Fleshing out your skeleton

Major Structures and Attributes

The major structures, pdo_dbh_t and pdo_stmt_t are defined and explained in Appendix A and B respectively. Database and Statement attributes are defined in Appendix C. Error handling is explained in Appendix D.

pdo_SKEL.c: PHP extension glue

function entries

static function_entry pdo_SKEL_functions[] = {
  { NULL, NULL, NULL }
};

This structure is used to register functions into the global php function namespace. PDO drivers should try to avoid doing this, so it is recommended that you leave this structure initialized to NULL, as shown in the synopsis above.

Module entry

/* {{{ pdo_SKEL_module_entry */
#if ZEND_EXTENSION_API_NO >= 220050617
static zend_module_dep pdo_SKEL_deps[] = {
    ZEND_MOD_REQUIRED("pdo")
    {NULL, NULL, NULL}
};
#endif
/* }}} */

zend_module_entry pdo_SKEL_module_entry = {
#if ZEND_EXTENSION_API_NO >= 220050617
    STANDARD_MODULE_HEADER_EX, NULL,
    pdo_SKEL_deps,
#else
    STANDARD_MODULE_HEADER,
#endif
    "pdo_SKEL",
    pdo_SKEL_functions,
    PHP_MINIT(pdo_SKEL),
    PHP_MSHUTDOWN(pdo_SKEL),
    NULL,
    NULL,
    PHP_MINFO(pdo_SKEL),
    PHP_PDO_<DB>_MODULE_VERSION,
    STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_PDO_<DB>
ZEND_GET_MODULE(pdo_db)
#endif

A structure of type zend_module_entry called pdo_SKEL_module_entry must be declared and should include reference to the pdo_SKEL_functions table defined previously.

Standard PHP Module Extension Functions

PHP_MINIT_FUNCTION
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(pdo_SKEL)
{
    return php_pdo_register_driver(&pdo_SKEL_driver);
}
/* }}} */

This standard PHP extension function should be used to register your driver with the PDO layer. This is done by calling the php_pdo_register_driver() function passing a pointer to a structure of type pdo_driver_t typically named pdo_SKEL_driver. A pdo_driver_t contains a header that is generated using the PDO_DRIVER_HEADER(SKEL) macro and pdo_SKEL_handle_factory() function pointer. The actual function is described during the discussion of the SKEL_dbh.c unit.

PHP_MSHUTDOWN_FUNCTION
/* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(pdo_SKEL)
{
    php_pdo_unregister_driver(&pdo_SKEL_driver);
    return SUCCESS;
}
/* }}} */

This standard PHP extension function is used to unregister your driver from the PDO layer. This is done by calling the php_pdo_unregister_driver() function, passing the same pdo_SKEL_driver structure that was passed in the init function above.

PHP_MINFO_FUNCTION

This is again a standard PHP extension function. Its purpose is to display information regarding the module when the phpinfo() is called from a script. The convention is to display the version of the module and also what version of the db you are dependent on, along with any other configuration style information that might be relevant.

SKEL_driver.c: Driver implementation

This unit implements all of the database handling methods that support the PDO database handle object. It also contains the error fetching routines. All of these functions will typically need to access the global variable pool. Therefore, it is necessary to use the Zend macro TSRMLS_DC macro at the end of each of these statements. Consult the Zend programmer documentation for more information on this macro.

pdo_SKEL_error

static int pdo_SKEL_error(pdo_dbh_t *dbh,
  pdo_stmt_t *stmt, const char *file, int line TSRMLS_DC)

The purpose of this function is to be used as a generic error handling function within the driver. It is called by the driver when an error occurs within the driver. If an error occurs that is not related to SQLSTATE, the driver should set either dbh->error_code or stmt->error_code to an SQLSTATE that most closely matches the error or the generic SQLSTATE error "HY000". The file pdo_sqlstate.c in the PDO source contains a table of commonly used SQLSTATE codes that the PDO code explicitly recognizes. This setting of the error code should be done prior to calling this function.; This function should set the global pdo_err variable to the error found in either the dbh or the stmt (if the variable stmt is not NULL).

dbh

Pointer to the database handle initialized by the handle factory

stmt

Pointer to the current statement or NULL. If NULL, the error is derived by error code found in the dbh.

file

The source file where the error occurred or NULL if not available.

line

The line number within the source file if available.

If the dbh member methods is NULL (which implies that the error is being raised from within the PDO constructor), this function should call the zend_throw_exception_ex() function otherwise it should return the error code. This function is usually called using a helper macro that customizes the calling sequence for either database handling errors or statement handling errors.

Example #1 Example macros for invoking pdo_SKEL_error

#define pdo_SKEL_drv_error(what) \
    pdo_SKEL_error(dbh, NULL, what, __FILE__, __LINE__ TSRMLS_CC)
#define pdo_SKEL_drv_error(what) \
    pdo_SKEL_error(dbh, NULL, what, __FILE__, __LINE__ TSRMLS_CC)

For more info on error handling, see Error handling.

Note:

Despite being documented here, the PDO driver interface does not specify that this function be present; it is merely a convenient way to handle errors, and it just happens to be equally convenient for the majority of database client library APIs to structure your driver implementation in this way.

pdo_SKEL_fetch_error_func

static int pdo_SKEL_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt,
    zval *info TSRMLS_DC)

The purpose of this function is to obtain additional information about the last error that was triggered. This includes the driver specific error code and a human readable string. It may also include additional information if appropriate. This function is called as a result of the PHP script calling the PDO::errorInfo() method.

dbh

Pointer to the database handle initialized by the handle factory

stmt

Pointer to the most current statement or NULL. If NULL, the error translated is derived by error code found in the dbh.

info

A hash table containing error codes and messages.

The error_func should return two pieces of information as successive array elements. The first item is expected to be a numeric error code, the second item is a descriptive string. The best way to set this item is by using add_next_index. Note that the type of the first argument need not be long; use whichever type most closely matches the error code returned by the underlying database API.

/* now add the error information. */
/* These need to be added in a specific order */
add_next_index_long(info, error_code);   /* driver specific error code */
add_next_index_string(info, message, 0); /* readable error message */

This function should return 1 if information is available, 0 if the driver does not have additional info.

SKEL_handle_closer

static int SKEL_handle_closer(pdo_dbh_t *dbh TSRMLS_DC)

This function will be called by PDO to close an open database.

dbh

Pointer to the database handle initialized by the handle factory

This should do whatever database specific activity that needs to be accomplished to close the open database. PDO ignores the return value from this function.

SKEL_handle_preparer

static int SKEL_handle_preparer(pdo_dbh_t *dbh, const char *sql,
long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)

This function will be called by PDO in response to PDO::query() and PDO::prepare() calls from the PHP script. The purpose of the function is to prepare raw SQL for execution, storing whatever state is appropriate into the stmt that is passed in.

dbh

Pointer to the database handle initialized by the handle factory

sql

Pointer to a character string containing the SQL statement to be prepared.

sql_len

The length of the SQL statement.

Stmt

Pointer to the returned statement or NULL if an error occurs.

driver_options

Any driver specific/defined options.

This function is essentially the constructor for a stmt object. This function is responsible for processing statement options, and setting driver-specific option fields in the pdo_stmt_t structure.

PDO does not process any statement options on the driver's behalf before calling the preparer function. It is your responsibility to process them before you return, raising an error for any unknown options that are passed.

One very important responsibility of this function is the processing of SQL statement parameters. At the time of this call, PDO does not know if your driver supports binding parameters into prepared statements, nor does it know if it supports named or positional parameter naming conventions.

Your driver is responsible for setting stmt->supports_placeholders as appropriate for the underlying database. This may involve some run-time determination on the part of your driver, if this setting depends on the version of the database server to which it is connected. If your driver doesn't directly support both named and positional parameter conventions, you should use the pdo_parse_params() API to have PDO rewrite the query to take advantage of the support provided by your database.

Example #2 Using pdo_parse_params

int ret;
    char *nsql = NULL;
    int nsql_len = 0;

    /* before we prepare, we need to peek at the query; if it uses named parameters,
     * we want PDO to rewrite them for us */
    stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
    ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);

    if (ret == 1) {
        /* query was re-written */
        sql = nsql;
    } else if (ret == -1) {
        /* couldn't grok it */
        strcpy(dbh->error_code, stmt->error_code);
        return 0;
    }

    /* now proceed to prepare the query in "sql" */

Possible values for supports_placeholders are: PDO_PLACEHOLDER_NAMED, PDO_PLACEHOLDER_POSITIONAL and PDO_PLACEHOLDER_NONE. If the driver doesn't support prepare statements at all, then this function should simply allocate any state that it might need, and then return:

Example #3 Implementing preparer for drivers that don't support native prepared statements

static int SKEL_handle_preparer(pdo_dbh_t *dbh, const char *sql,
    long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)
{
    pdo_SKEL_db_handle *H = (pdo_SKEL_db_handle *)dbh->driver_data;
    pdo_SKEL_stmt *S = ecalloc(1, sizeof(pdo_SKEL_stmt));

    S->H = H;
    stmt->driver_data = S;
    stmt->methods = &SKEL_stmt_methods;
    stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;

    return 1;
}

This function returns 1 on success or 0 on failure.

SKEL_handle_doer

static long SKEL_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC)

This function will be called by PDO to execute a raw SQL statement. No pdo_stmt_t is created.

dbh

Pointer to the database handle initialized by the handle factory

sql

Pointer to a character string containing the SQL statement to be prepared.

sql_len

The length of the SQL statement.

This function returns 1 on success or 0 on failure.

SKEL_handle_quoter

static int SKEL_handle_quoter(pdo_dbh_t *dbh, const char *unquoted,
  int unquoted_len, char **quoted, int quoted_len, enum pdo_param_type param_type TSRMLS_DC)

This function will be called by PDO to turn an unquoted string into a quoted string for use in a query.

dbh

Pointer to the database handle initialized by the handle factory

unquoted

Pointer to a character string containing the string to be quoted.

unquoted_len

The length of the string to be quoted.

quoted

Pointer to the address where a pointer to the newly quoted string will be returned.

quoted_len

The length of the new string.

param_type

A driver specific hint for driver that have alternate quoting styles

This function is called in response to a call to PDO::quote() or when the driver has set supports_placeholder to PDO_PLACEHOLDER_NONE. The purpose is to quote a parameter when building SQL statements.

If your driver does not support native prepared statements, implementation of this function is required.

This function returns 1 if the quoting process reformatted the string, and 0 if it was not necessary to change the string. The original string will be used unchanged with a 0 return.

SKEL_handle_begin

static int SKEL_handle_begin(pdo_dbh_t *dbh TSRMLS_DC)

This function will be called by PDO to begin a database transaction.

dbh

Pointer to the database handle initialized by the handle factory

This should do whatever database specific activity that needs to be accomplished to begin a transaction. This function returns 1 for success or 0 if an error occurred.

SKEL_handle_commit

static int SKEL_handle_commit(pdo_dbh_t *dbh TSRMLS_DC)

This function will be called by PDO to end a database transaction.

dbh

Pointer to the database handle initialized by the handle factory

This should do whatever database specific activity that needs to be accomplished to commit a transaction. This function returns 1 for success or 0 if an error occurred.

SKEL_handle_rollback

static int SKEL_handle_rollback( pdo_dbh_t *dbh TSRMLS_DC)

This function will be called by PDO to rollback a database transaction.

dbh

Pointer to the database handle initialized by the handle factory

This should do whatever database specific activity that needs to be accomplished to rollback a transaction. This function returns 1 for success or 0 if an error occurred.

SKEL_handle_get_attribute

static int SKEL_handle_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC)

This function will be called by PDO to retrieve a database attribute.

dbh

Pointer to the database handle initialized by the handle factory

attr

long value of one of the PDO_ATTR_xxxx types. See Database and Statement Attributes Table for valid attributes.

return_value

The returned value for the attribute.

It is up to the driver to decide which attributes will be supported for a particular implementation. It is not necessary for a driver to supply this function. PDO driver handles the PDO_ATTR_PERSISTENT, PDO_ATTR_CASE, PDO_ATTR_ORACLE_NULLS, and PDO_ATTR_ERRMODE attributes directly.

This function returns 1 on success or 0 on failure.

SKEL_handle_set_attribute

static int SKEL_handle_set_attribute(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC)

This function will be called by PDO to set a database attribute, usually in response to a script calling PDO::setAttribute().

dbh

Pointer to the database handle initialized by the handle factory

attr

long value of one of the PDO_ATTR_xxxx types. See Database and Statement Attributes Table for valid attributes.

val

The new value for the attribute.

It is up to the driver to decide which attributes will be supported for a particular implementation. It is not necessary for a driver to provide this function if it does not need to support additional attributes. The PDO driver handles the PDO_ATTR_CASE, PDO_ATTR_ORACLE_NULLS, and PDO_ATTR_ERRMODE attributes directly.

This function returns 1 on success or 0 on failure.

SKEL_handle_last_id

static char * SKEL_handle_last_id(pdo_dbh_t *dbh, const char *name, unsigned int len TSRMLS_DC)

This function will be called by PDO to retrieve the ID of the last inserted row.

dbh

Pointer to the database handle initialized by the handle factory

name

string representing a table or sequence name.

len

the length of the name parameter.

This function returns a character string containing the id of the last inserted row on success or NULL on failure. This is an optional function.

SKEL_check_liveness

static int SKEL_check_liveness(pdo_dbh_t *dbh TSRMLS_DC)

This function will be called by PDO to test whether or not a persistent connection to a database is alive and ready for use.

dbh

Pointer to the database handle initialized by the handle factory

This function returns 1 if the database connection is alive and ready for use, otherwise it should return 0 to indicate failure or lack of support.

Note:

This is an optional function.

SKEL_get_driver_methods

static function_entry *SKEL_get_driver_methods(pdo_dbh_t *dbh, int kind TSRMLS_DC)

This function will be called by PDO in response to a call to any method that is not a part of either the PDO or PDOStatement classes. It's purpose is to allow the driver to provide additional driver specific methods to those classes.

dbh

Pointer to the database handle initialized by the handle factory

kind

One of the following:

PDO_DBH_DRIVER_METHOD_KIND_DBH

Set when the method call was attempted on an instance of the PDO class. The driver should return a pointer a function_entry table for any methods it wants to add to that class, or NULL if there are none.

PDO_DBH_DRIVER_METHOD_KIND_STMT

Set when the method call was attempted on an instance of the PDOStatement class. The driver should return a pointer to a function_entry table for any methods it wants to add to that class, or NULL if there are none.

This function returns a pointer to the function_entry table requested, or NULL there are no driver specific methods.

SKEL_handle_factory

static int SKEL_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC)

This function will be called by PDO to create a database handle. For most databases this involves establishing a connection to the database. In some cases, a persistent connection may be requested, in other cases connection pooling may be requested. All of these are database/driver dependent.

dbh

Pointer to the database handle initialized by the handle factory

driver_options

An array of driver options, keyed by integer option number. See Database and Statement Attributes Table for a list of possible attributes.

This function should fill in the passed database handle structure with its driver specific information on success and return 1, otherwise it should return 0 to indicate failure.

PDO processes the AUTOCOMMIT and PERSISTENT driver options before calling the handle_factory. It is the handle factory's responsibility to process other options.

Driver method table

A static structure of type pdo_dbh_methods named SKEL_methods must be declared and initialized to the function pointers for each defined function. If a function is not supported or not implemented the value for that function pointer should be set to NULL.

pdo_SKEL_driver

A structure of type pdo_driver_t named pdo_SKEL_driver should be declared. The PDO_DRIVER_HEADER(SKEL) macro should be used to declare the header and the function pointer to the handle factory function should set.

SKEL_statement.c: Statement implementation

This unit implements all of the database statement handling methods that support the PDO statement object.

SKEL_stmt_dtor

static int SKEL_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)

This function will be called by PDO to destroy a previously constructed statement object.

stmt

Pointer to the statement structure initialized by SKEL_handle_preparer.

This should do whatever is necessary to free up any driver specific storage allocated for the statement. The return value from this function is ignored.

SKEL_stmt_execute

static int SKEL_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)

This function will be called by PDO to execute the prepared SQL statement in the passed statement object.

stmt

Pointer to the statement structure initialized by SKEL_handle_preparer.

This function returns 1 for success or 0 in the event of failure.

SKEL_stmt_fetch

static int SKEL_stmt_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori,
   long offset TSRMLS_DC)

This function will be called by PDO to fetch a row from a previously executed statement object.

stmt

Pointer to the statement structure initialized by SKEL_handle_preparer.

ori

One of PDO_FETCH_ORI_xxx which will determine which row will be fetched.

offset

If ori is set to PDO_FETCH_ORI_ABS or PDO_FETCH_ORI_REL, offset represents the row desired or the row relative to the current position, respectively. Otherwise, this value is ignored.

The results of this fetch are driver dependent and the data is usually stored in the driver_data member of the pdo_stmt_t object. The ori and offset parameters are only meaningful if the statement represents a scrollable cursor. This function returns 1 for success or 0 in the event of failure.

SKEL_stmt_param_hook

static int SKEL_stmt_param_hook(pdo_stmt_t *stmt,
  struct pdo_bound_param_data *param, enum pdo_param_event event_type TSRMLS_DC)

This function will be called by PDO for handling of both bound parameters and bound columns.

stmt

Pointer to the statement structure initialized by SKEL_handle_preparer.

param

The structure describing either a statement parameter or a bound column.

event_type

The type of event to occur for this parameter, one of the following:

PDO_PARAM_EVT_ALLOC

Called when PDO allocates the binding. Occurs as part of PDOStatement::bindParam(), PDOStatement::bindValue() or as part of an implicit bind when calling PDOStatement::execute(). This is your opportunity to take some action at this point; drivers that implement native prepared statements will typically want to query the parameter information, reconcile the type with that requested by the script, allocate an appropriately sized buffer and then bind the parameter to that buffer. You should not rely on the type or value of the zval at param->parameter at this point in time.

PDO_PARAM_EVT_FREE

Called once per parameter as part of cleanup. You should release any resources associated with that parameter now.

PDO_PARAM_EXEC_PRE

Called once for each parameter immediately before calling SKEL_stmt_execute; take this opportunity to make any final adjustments ready for execution. In particular, you should note that variables bound via PDOStatement::bindParam() are only legal to touch now, and not any sooner.

PDO_PARAM_EXEC_POST

Called once for each parameter immediately after calling SKEL_stmt_execute; take this opportunity to make any post-execution actions that might be required by your driver.

PDO_PARAM_FETCH_PRE

Called once for each parameter immediately prior to calling SKEL_stmt_fetch.

PDO_PARAM_FETCH_POST

Called once for each parameter immediately after calling SKEL_stmt_fetch.

This hook will be called for each bound parameter and bound column in the statement. For ALLOC and FREE events, a single call will be made for each parameter or column. The param structure contains a driver_data field that the driver can use to store implementation specific information about each of the parameters.

For all other events, PDO may call you multiple times as the script issues PDOStatement::execute() and PDOStatement::fetch() calls.

If this is a bound parameter, the is_param flag in the param structure is set, otherwise the param structure refers to a bound column.

This function returns 1 for success or 0 in the event of failure.

SKEL_stmt_describe_col

static int SKEL_stmt_describe_col(pdo_stmt_t *stmt, int colno TSRMLS_DC)

This function will be called by PDO to query information about a particular column.

stmt

Pointer to the statement structure initialized by SKEL_handle_preparer.

colno

The column number to be queried.

The driver should populate the pdo_stmt_t member columns(colno) with the appropriate information. This function returns 1 for success or 0 in the event of failure.

SKEL_stmt_get_col_data

static int SKEL_stmt_get_col_data(pdo_stmt_t *stmt, int colno,
  char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)

This function will be called by PDO to retrieve data from the specified column.

stmt

Pointer to the statement structure initialized by SKEL_handle_preparer.

colno

The column number to be queried.

ptr

Pointer to the retrieved data.

len

The length of the data pointed to by ptr.

caller_frees

If set, ptr should point to emalloc'd memory and the main PDO driver will free it as soon as it is done with it. Otherwise, it will be the responsibility of the driver to free any allocated memory as a result of this call.

The driver should return the resultant data and length of that data in the ptr and len variables respectively. It should be noted that the main PDO driver expects the driver to manage the lifetime of the data. This function returns 1 for success or 0 in the event of failure.

SKEL_stmt_set_attr

static int SKEL_stmt_set_attr(pdo_stmt_t *stmt, long attr, zval *val TSRMLS_DC)

This function will be called by PDO to allow the setting of driver specific attributes for a statement object.

stmt

Pointer to the statement structure initialized by SKEL_handle_preparer.

attr

long value of one of the PDO_ATTR_xxxx types. See Database and Statement Attributes Table for valid attributes.

val

The new value for the attribute.

This function is driver dependent and allows the driver the capability to set database specific attributes for a statement. This function returns 1 for success or 0 in the event of failure. This is an optional function. If the driver does not support additional settable attributes, it can be NULLed in the method table. The PDO driver does not handle any settable attributes on the database driver's behalf.

SKEL_stmt_get_attr

static int SKEL_stmt_get_attr(pdo_stmt_t *stmt, long attr, zval
   *return_value TSRMLS_DC)

This function will be called by PDO to allow the retrieval of driver specific attributes for a statement object.

stmt

Pointer to the statement structure initialized by SKEL_handle_preparer.

attr

long value of one of the PDO_ATTR_xxxx types. See Database and Statement Attributes Table for valid attributes.

return_value

The returned value for the attribute.

This function is driver dependent and allows the driver the capability to retrieve a previously set database specific attribute for a statement. This function returns 1 for success or 0 in the event of failure. This is an optional function. If the driver does not support additional gettable attributes, it can be NULLed in the method table. The PDO driver does not handle any settable attributes on the database driver's behalf.

SKEL_stmt_get_col_meta

static int SKEL_stmt_get_col_meta(pdo_stmt_t *stmt, int colno,
   zval *return_value TSRMLS_DC)
Warning

This function is not well defined and is subject to change.

This function will be called by PDO to retrieve meta data from the specified column.

stmt

Pointer to the statement structure initialized by SKEL_handle_preparer.

colno

The column number for which data is to be retrieved.

return_value

Holds the returned meta data.

The driver author should consult the documentation for this function that can be found in the php_pdo_driver.h header as this will be the most current. This function returns 1 for success or 0 in the event of failure. The database driver does not need to provide this function.

Statement handling method table

A static structure of type pdo_stmt_methods named SKEL_stmt_methods should be declared and initialized to the function pointers for each defined function. If a function is not supported or not implemented the value for that function pointer should be set to NULL.

User Contributed Notes

There are no user contributed notes for this page.


Building

The build process is designed to work with PEAR (see » http://pear.php.net/ for more information about PEAR). There are two files that are used to assist in configuring your package for building. The first is config.m4 which is the autoconf configuration file for all platforms except Win32. The second is config.w32 which is a build configuration file for use on Win32. Skeleton files for these are built for you when you first set up your project. You then need to customize them to fit the needs of your project. Once you've customized your config files, you can build your driver using the following sequence of commands:

Before first build:

$ sudo pecl install PDO

For each build:

$ cd pdo_SKEL
$ phpize
$ ./configure
$ make
$ sudo make install

The process can then be repeated as necessary during the development process.

User Contributed Notes

There are no user contributed notes for this page.


Testing

PDO has a set of "core" tests that all drivers should pass before being released. They're designed to run from the PHP source distribution, so running the tests for your driver requires moving things around a bit. The suggested procedure is to obtain the latest PHP 5.1 snapshot and perform the following step:

$ cp -r pdo_SKEL /path/to/php-5.1/ext

This will allow the test harness to run your tests. The next thing you need to do is create a test that will redirect into the PDO common core tests. The convention is to name this file common.phpt; it should be placed in the tests subdirectory that was created by ext_skel when you created your extension skeleton. The content of this file should look something like the following:

--TEST--
SKEL
--SKIPIF--
<?php # vim:ft=php
if (!extension_loaded('pdo_SKEL')) print 'skip'; ?>
--REDIRECTTEST--
if (false !== getenv('PDO_SKEL_TEST_DSN')) {
# user set them from their shell
   $config['ENV']['PDOTEST_DSN'] = getenv('PDO_SKEL_TEST_DSN');
   $config['ENV']['PDOTEST_USER'] = getenv('PDO_SKEL_TEST_USER');
   $config['ENV']['PDOTEST_PASS'] = getenv('PDO_SKEL_TEST_PASS');
   if (false !== getenv('PDO_SKEL_TEST_ATTR')) {
      $config['ENV']['PDOTEST_ATTR'] = getenv('PDO_SKEL_TEST_ATTR');
   }
   return $config;
}
return array(
   'ENV' => array(
           'PDOTEST_DSN' => 'SKEL:dsn',
           'PDOTEST_USER' => 'username',
           'PDOTEST_PASS' => 'password'
       ),
   'TESTS' => 'ext/pdo/tests'
   );

This will cause the common core tests to be run, passing the values of PDOTEST_DSN, PDOTEST_USER and PDOTEST_PASS to the PDO constructor as the dsn, username and password parameters. It will first check the environment, so that appropriate values can be passed in when the test harness is run, rather than hard-coding the database credentials into the test file.

The test harness can be invoked as follows:

$ cd /path/to/php-5.1
$ make TESTS=ext/pdo_SKEL/tests PDO_SKEL_TEST_DSN="skel:dsn" \
 PDO_SKEL_TEST_USER=user PDO_SKEL_TEST_PASS=pass test

User Contributed Notes

There are no user contributed notes for this page.


Packaging and distribution

Creating a package

PDO drivers are released via PECL; all the usual rules for PECL extensions apply. Packaging is accomplished by creating a valid package.xml file and then running:

$ pecl package

This will create a tarball named PDO_SKEL-X.Y.Z.tgz.

Before releasing the package, you should test that it builds correctly; if you've made a mistake in your config.m4 or package.xml files, the package may not function correctly. You can test the build, without installing anything, using the following invocation:

$ pecl build package.xml

Once this is proven to work, you can test installation:

$ pecl package
$ sudo pecl install PDO_SKEL-X.Y.X.tgz

Full details about package.xml can be found in the PEAR Programmer's documentation (» http://pear.php.net/manual/).

Releasing the package

A PDO driver is released via the PHP Extension Community Library (PECL). Information about PECL can be found at » http://pecl.php.net/.

User Contributed Notes

There are no user contributed notes for this page.


pdo_dbh_t definition

All fields should be treated as read-only by the driver, unless explicitly stated otherwise.

pdo_dbh_t

/* represents a connection to a database */
struct _pdo_dbh_t {
    /* driver specific methods */
    struct pdo_dbh_methods *methods;
*
/* driver specific data */
    void *driver_data;
**
/* credentials */
    char *username, *password;
***
/* if true, then data stored and pointed at by this handle must all be
     * persistently allocated */
    unsigned is_persistent:1;
****
/* if true, driver should act as though a COMMIT were executed between
     * each executed statement; otherwise, COMMIT must be carried out manually
     * */
    unsigned auto_commit:1;
*****
/* if true, the driver requires that memory be allocated explicitly for
     * the columns that are returned */
    unsigned alloc_own_columns:1;
******
/* if true, commit or rollBack is allowed to be called */
    unsigned in_txn:1;

    /* max length a single character can become after correct quoting */
    unsigned max_escaped_char_length:3;
*******
/* data source string used to open this handle */
    const char *data_source;
********
unsigned long data_source_len;

    /* the global error code. */
    pdo_error_type error_code;
*********
enum pdo_case_conversion native_case
**********
, desired_case;
};
*

The driver must set this during SKEL_handle_factory().

**

This item is for use by the driver; the intended usage is to store a pointer (during SKEL_handle_factory()) to whatever instance data is required to maintain a connection to the database.

***

The username and password that were passed into the PDO constructor. The driver should use these values when it initiates a connection to the database.

****

If this is set to 1, then any data that is referenced by the dbh, including whatever structure your driver allocates, MUST be allocated persistently. This is easy to achieve; rather than using the usual emalloc() simply use pemalloc() and pass the value of this flag as the last parameter. Failure to use the appropriate kind of memory can lead to serious memory faults, resulting (in the best case) a hard crash, and in the worst case, an exploitable memory problem.

If, for whatever reason, your driver is not suitable to run persistently, you MUST check this flag in your SKEL_handle_factory() and raise an appropriate error.

*****

You should check this value in your SKEL_handle_doer() and SKEL_stmt_execute() functions; if it evaluates to true, you must attempt to commit the query now. Most database implementations offer an auto-commit mode that handles this automatically.

******

If your database client library API operates by fetching data into a caller-supplied buffer, you should set this flag to 1 during your SKEL_handle_factory(). When set, PDO will call your SKEL_stmt_describer() earlier than it would otherwise. This early call allows you to determine those buffer sizes and issue appropriate calls to the database client library.

If your database client library API simply returns pointers to its own internal buffers for you to copy after each fetch call, you should leave this value set to 0.

*******

If your driver doesn't support native prepared statements (supports_placeholders is set to PDO_PLACEHOLDER_NONE), you must set this value to the maximum length that can be taken up by a single character when it is quoted by your SKEL_handle_quoter() function. This value is used to calculate the amount of buffer space required when PDO executes the statement.

********

This holds the value of the DSN that was passed into the PDO constructor. If your driver implementation needed to modify the DSN for whatever reason, it should update this member during SKEL_handle_factory(). Modifying this member should be avoided. If you do change it, you must ensure that data_source_len is also correct.

*********

Whenever an error occurs during a call to one of your driver methods, you should set this member to the SQLSTATE code that best describes the error and return an error. In this HOW-TO, the suggested practice is to call SKEL_handle_error() when an error is detected, and have it set the error code.

**********

Your driver should set this during SKEL_handle_factory(); the value should reflect how the database returns the names of the columns in result sets. If the name matches the case that was used in the query, set it to PDO_CASE_NATURAL (this is actually the default). If the column names are always returned in upper case, set it to PDO_CASE_UPPER. If the column names are always returned in lower case, set it to PDO_CASE_LOWER. The value you set is used to determine if PDO should perform case folding when the user sets the PDO_ATTR_CASE attribute.

User Contributed Notes

There are no user contributed notes for this page.


pdo_stmt_t definition

All fields should be treated as read-only unless explicitly stated otherwise.

pdo_stmt_t

/* represents a prepared statement */
struct _pdo_stmt_t {
    /* driver specifics */
    struct pdo_stmt_methods *methods;
*
void *driver_data;
**
/* if true, we've already successfully executed this statement at least
     * once */
    unsigned executed:1;
***
/* if true, the statement supports placeholders and can implement
     * bindParam() for its prepared statements, if false, PDO should
     * emulate prepare and bind on its behalf */
    unsigned supports_placeholders:2;
****
/* the number of columns in the result set; not valid until after
     * the statement has been executed at least once.  In some cases, might
     * not be valid until fetch (at the driver level) has been called at least once.
     * */
    int column_count;
*****
struct pdo_column_data *columns;
******
/* points at the dbh that this statement was prepared on */
    pdo_dbh_t *dbh;

    /* keep track of bound input parameters.  Some drivers support
     * input/output parameters, but you can't rely on that working */
    HashTable *bound_params;
    /* When rewriting from named to positional, this maps positions to names */
    HashTable *bound_param_map;
    /* keep track of PHP variables bound to named (or positional) columns
     * in the result set */
    HashTable *bound_columns;

    /* not always meaningful */
    long row_count;

    /* used to hold the statement's current query */
    char *query_string;
    int query_stringlen;

    /* the copy of the query with expanded binds ONLY for emulated-prepare drivers */
    char *active_query_string;
    int active_query_stringlen;

    /* the cursor specific error code. */
    pdo_error_type error_code;

    /* used by the query parser for driver specific
     * parameter naming (see pgsql driver for example) */
    const char *named_rewrite_template;
};
* | The driver must set this during SKEL_handle_preparer().
** | This item is for use by the driver; the intended usage is to store a pointer (during SKEL_handle_factory()) to whatever instance data is required to maintain a connection to the database.
*** | This is set by PDO after the statement has been executed for the first time. Your driver can inspect this value to determine if it can skip one-time actions as an optimization.
**** | Discussed in more detail in SKEL_handle_preparer.
***** | Your driver is responsible for setting this field to the number of columns available in a result set. This is usually set during SKEL_stmt_execute() but with some database implementations, the column count may not be available until SKEL_stmt_fetch() has been called at least once. Drivers that implement SKEL_stmt_next_rowset() should update the column count when a new rowset is available.
****** | PDO will allocate this field based on the value that you set for the column count. You are responsible for populating each column during SKEL_stmt_describe(). You must set the precision, maxlen, name, namelen and param_type members for each column. The name is expected to be allocated using emalloc(); PDO will call efree() at the appropriate time.

User Contributed Notes

There are no user contributed notes for this page.


Constants

Database and Statement Attributes Table

Attribute

Valid value(s)

PDO_ATTR_AUTOCOMMIT

BOOL

TRUE if autocommit is set, FALSE otherwise.

dbh->auto_commit contains value. Processed by PDO directly.

PDO_ATTR_PREFETCH

LONG

Value of the prefetch size in drivers that support it.

PDO_ATTR_TIMEOUT

LONG

How long to wait for a db operation before timing out.

PDO_ATTR_ERRMODE

LONG

Processed and handled by PDO

PDO_ATTR_SERVER_VERSION

STRING

The "human-readable" string representing the Server/Version this driver is currently connected to.

PDO_ATTR_CLIENT_VERSION

STRING

The "human-readable" string representing the Client/Version this driver supports.

PDO_ATTR_SERVER_INFO

STRING

The "human-readable" description of the Server.

PDO_ATTR_CONNECTION_STATUS

LONG

Values not yet defined

PDO_ATTR_CASE

LONG

Processed and handled by PDO.

PDO_ATTR_CURSOR_NAME

STRING

String representing the name for a database cursor for use in "where current in <name>" SQL statements.

PDO_ATTR_CURSOR

LONG

PDO_CURSOR_FWDONLY

Forward only cursor

PDO_CURSOR_SCROLL

Scrollable cursor

The values for the attributes above are all defined in terms of the Zend API. The Zend API contains macros that can be used to convert a *zval to a value. These macros are defined in the Zend header file, zend_API.h in the Zend directory of your PHP build directory. Some of these attributes can be used with the statement attribute handlers such as the PDO_ATTR_CURSOR and PDO_ATTR_CURSOR_NAME. See the statement attribute handling functions for more information.

User Contributed Notes

There are no user contributed notes for this page.


Error handling

Error handling is implemented using a hand-shaking protocol between PDO and the database driver code. The database driver code signals PDO that an error has occurred via a failure (0) return from any of the interface functions. If a zero is returned, set the field error_code in the control block appropriate to the context (either the pdo_dbh_t or pdo_stmt_t block). In practice, it is probably a good idea to set the field in both blocks to the same value to ensure the correct one is getting used.

The error_mode field is a six-byte field containing a 5 character ASCIIZ SQLSTATE identifier code. This code drives the error message process. The SQLSTATE code is used to look up an error message in the internal PDO error message table (see pdo_sqlstate.c for a list of error codes and their messages). If the code is not known to PDO, a default "Unknown Message" value will be used.

In addition to the SQLSTATE code and error message, PDO will call the driver-specific fetch_err() routine to obtain supplemental data for the particular error condition. This routine is passed an array into which the driver may place additional information. This array has slot positions assigned to particular types of supplemental info:

  1. A native error code. This will frequently be an error code obtained from the database API.

  2. A descriptive string. This string can contain any additional information related to the failure. Database drivers typically include information such as an error message, code location of the failure, and any additional descriptive information the driver developer feels worthy of inclusion. It is generally a good idea to include all diagnostic information obtainable from the database interface at the time of the failure. For driver-detected errors (such as memory allocation problems), the driver developer can define whatever error information that seems appropriate.

User Contributed Notes

There are no user contributed notes for this page.


  • ≪ Error handling
  • Zend Engine 2 API reference ≫
  • PHP Manual
  • PHP 코어: Zend 엔진 해커 가이드
  • Extension FAQs

Extension FAQs

User Contributed Notes

There are no user contributed notes for this page.


  • ≪ Extension FAQs
  • Zend Engine 2 Opcodes ≫
  • PHP Manual
  • PHP 코어: Zend 엔진 해커 가이드
  • Zend Engine 2 API reference

Zend Engine 2 API reference

User Contributed Notes

There are no user contributed notes for this page.


Zend Engine 2 Opcodes

Table of Contents

  • Opcode Descriptions and Examples

When parsing PHP files, Zend Engine 2 generates a series of operation codes, commonly known as "opcodes", representing the function of the code. This part of the manual details those opcodes and their behaviour.

Opcodes may be dumped for a given PHP file using the vld extension (see » http://pecl.php.net/package/vld).

Opcode List
Number | Name | Has sample code?
0 | NOP | yes
1 | ADD | yes
2 | SUB | yes
3 | MUL | yes
4 | DIV | yes
5 | MOD | yes
6 | SL | yes
7 | SR | yes
8 | CONCAT | yes
9 | BW_OR | yes
10 | BW_AND | yes
11 | BW_XOR | yes
12 | BW_NOT | yes
13 | BOOL_NOT | yes
14 | BOOL_XOR | yes
15 | IS_IDENTICAL | yes
16 | IS_NOT_IDENTICAL | yes
17 | IS_EQUAL | yes
18 | IS_NOT_EQUAL | yes
19 | IS_SMALLER | yes
20 | IS_SMALLER_OR_EQUAL | yes
21 | CAST | yes
22 | QM_ASSIGN | yes
23 | ASSIGN_ADD | yes
24 | ASSIGN_SUB | yes
25 | ASSIGN_MUL | yes
26 | ASSIGN_DIV | yes
27 | ASSIGN_MOD | yes
28 | ASSIGN_SL | yes
29 | ASSIGN_SR | yes
30 | ASSIGN_CONCAT | yes
31 | ASSIGN_BW_OR | yes
32 | ASSIGN_BW_AND | yes
33 | ASSIGN_BW_XOR | yes
34 | PRE_INC | yes
35 | PRE_DEC | yes
36 | POST_INC | yes
37 | POST_DEC | yes
38 | ASSIGN | yes
39 | ASSIGN_REF | yes
40 | ECHO | yes
41 | PRINT | yes
42 | JMP | yes
43 | JMPZ | yes
44 | JMPNZ | yes
45 | JMPZNZ | yes
46 | JMPZ_EX | yes
47 | JMPNZ_EX | yes
48 | CASE | yes
49 | SWITCH_FREE | yes
50 | BRK | yes
51 | CONT | yes
52 | BOOL | yes
53 | INIT_STRING | yes
54 | ADD_CHAR | yes
55 | ADD_STRING | yes
56 | ADD_VAR | yes
57 | BEGIN_SILENCE | yes
58 | END_SILENCE | yes
59 | INIT_FCALL_BY_NAME | yes
60 | DO_FCALL | yes
61 | DO_FCALL_BY_NAME | yes
62 | RETURN | yes
63 | RECV | yes
64 | RECV_INIT | yes
65 | SEND_VAL | yes
66 | SEND_VAR | yes
67 | SEND_REF | yes
68 | NEW | yes
69 | INIT_NS_FCALL_BY_NAME | no
70 | FREE | yes
71 | INIT_ARRAY | yes
72 | ADD_ARRAY_ELEMENT | yes
73 | INCLUDE_OR_EVAL | yes
74 | UNSET_VAR | yes
75 | UNSET_DIM | yes
76 | UNSET_OBJ | yes
77 | FE_RESET | yes
78 | FE_FETCH | yes
79 | EXIT | yes
80 | FETCH_R | yes
81 | FETCH_DIM_R | yes
82 | FETCH_OBJ_R | yes
83 | FETCH_W | yes
84 | FETCH_DIM_W | yes
85 | FETCH_OBJ_W | yes
86 | FETCH_RW | yes
87 | FETCH_DIM_RW | yes
88 | FETCH_OBJ_RW | yes
89 | FETCH_IS | yes
90 | FETCH_DIM_IS | no
91 | FETCH_OBJ_IS | no
92 | FETCH_FUNC_ARG | yes
93 | FETCH_DIM_FUNC_ARG | yes
94 | FETCH_OBJ_FUNC_ARG | yes
95 | FETCH_UNSET | no
96 | FETCH_DIM_UNSET | no
97 | FETCH_OBJ_UNSET | no
98 | FETCH_DIM_TMP_VAR | yes
99 | FETCH_CONSTANT | yes
100 | GOTO | no
101 | EXT_STMT | yes
102 | EXT_FCALL_BEGIN | no
103 | EXT_FCALL_END | no
104 | EXT_NOP | no
105 | TICKS | yes
106 | SEND_VAR_NO_REF | no
107 | CATCH | yes
108 | THROW | yes
109 | FETCH_CLASS | yes
110 | CLONE | yes
111 | RETURN_BY_REF | no
112 | INIT_METHOD_CALL | yes
113 | INIT_STATIC_METHOD_CALL | yes
114 | ISSET_ISEMPTY_VAR | yes
115 | ISSET_ISEMPTY_DIM_OBJ | yes
116 | not documented | no
117 | not documented | no
118 | not documented | no
119 | not documented | no
120 | not documented | no
121 | not documented | no
122 | not documented | no
123 | not documented | no
124 | not documented | no
125 | not documented | no
126 | not documented | no
127 | not documented | no
128 | not documented | no
129 | not documented | no
130 | not documented | no
131 | not documented | no
132 | PRE_INC_OBJ | yes
133 | PRE_DEC_OBJ | yes
134 | POST_INC_OBJ | yes
135 | POST_DEC_OBJ | yes
136 | ASSIGN_OBJ | yes
137 | not documented | no
138 | INSTANCEOF | yes
139 | DECLARE_CLASS | yes
140 | DECLARE_INHERITED_CLASS | yes
141 | DECLARE_FUNCTION | yes
142 | RAISE_ABSTRACT_ERROR | yes
143 | DECLARE_CONST | no
144 | ADD_INTERFACE | no
145 | DECLARE_INHERITED_CLASS_DELAYED | no
146 | VERIFY_ABSTRACT_CLASS | no
147 | ASSIGN_DIM | yes
148 | ISSET_ISEMPTY_PROP_OBJ | yes
149 | HANDLE_EXCEPTION | yes
150 | USER_OPCODE | no
152 | ZEND_JMP_SET | no
153 | ZEND_DECLARE_LAMBDA_FUNCTION | no
154 | ZEND_ADD_TRAIT | no
155 | ZEND_BIND_TRAITS | no
156 | ZEND_SEPARATE | no
157 | ZEND_FETCH_CLASS_NAME | no
158 | ZEND_CALL_TRAMPOLINE | no
159 | ZEND_DISCARD_EXCEPTION | no
160 | ZEND_YIELD | no
161 | ZEND_GENERATOR_RETURN | no
162 | ZEND_FAST_CALL | no
163 | ZEND_FAST_RET | no
164 | ZEND_RECV_VARIADIC | no
165 | ZEND_SEND_UNPACK | no
166 | ZEND_POW | no
167 | ZEND_ASSIGN_POW | no
168 | ZEND_BIND_GLOBAL | no
169 | ZEND_COALESCE | no
170 | ZEND_SPACESHIP | no
171 | ZEND_DECLARE_ANON_CLASS | no
172 | ZEND_DECLARE_ANON_INHERITED_CLASS | no

User Contributed Notes

There are no user contributed notes for this page.


Opcode Descriptions and Examples

Table of Contents

  • ADD
  • ADD_ARRAY_ELEMENT
  • ADD_CHAR
  • ADD_INTERFACE
  • ADD_STRING
  • ADD_VAR
  • ASSIGN
  • ASSIGN_ADD
  • ASSIGN_BW_AND
  • ASSIGN_BW_OR
  • ASSIGN_BW_XOR
  • ASSIGN_CONCAT
  • ASSIGN_DIM
  • ASSIGN_DIV
  • ASSIGN_MOD
  • ASSIGN_MUL
  • ASSIGN_OBJ
  • ASSIGN_REF
  • ASSIGN_SL
  • ASSIGN_SR
  • ASSIGN_SUB
  • BEGIN_SILENCE
  • BOOL
  • BOOL_NOT
  • BOOL_XOR
  • BRK
  • BW_AND
  • BW_NOT
  • BW_OR
  • BW_XOR
  • CASE
  • CAST
  • CATCH
  • CLONE
  • CONCAT
  • CONT
  • DECLARE_CLASS
  • DECLARE_CONST
  • DECLARE_FUNCTION
  • DECLARE_INHERITED_CLASS
  • DECLARE_INHERITED_CLASS_DELAYED
  • DIV
  • DO_FCALL
  • DO_FCALL_BY_NAME
  • ECHO
  • END_SILENCE
  • EXIT
  • EXT_FCALL_BEGIN
  • EXT_FCALL_END
  • EXT_NOP
  • EXT_STMT
  • FE_FETCH
  • FE_RESET
  • FETCH_CLASS
  • FETCH_CONSTANT
  • FETCH_DIM_FUNC_ARG
  • FETCH_DIM_IS
  • FETCH_DIM_R
  • FETCH_DIM_RW
  • FETCH_DIM_TMP_VAR
  • FETCH_DIM_UNSET
  • FETCH_DIM_W
  • FETCH_FUNC_ARG
  • FETCH_IS
  • FETCH_OBJ_FUNC_ARG
  • FETCH_OBJ_IS
  • FETCH_OBJ_R
  • FETCH_OBJ_RW
  • FETCH_OBJ_UNSET
  • FETCH_OBJ_W
  • FETCH_R
  • FETCH_RW
  • FETCH_UNSET
  • FETCH_W
  • FREE
  • GOTO
  • HANDLE_EXCEPTION
  • INCLUDE_OR_EVAL
  • INIT_ARRAY
  • INIT_FCALL_BY_NAME
  • INIT_METHOD_CALL
  • INIT_NS_FCALL_BY_NAME
  • INIT_STATIC_METHOD_CALL
  • INIT_STRING
  • INSTANCEOF
  • IS_EQUAL
  • IS_IDENTICAL
  • IS_NOT_EQUAL
  • IS_NOT_IDENTICAL
  • IS_SMALLER
  • IS_SMALLER_OR_EQUAL
  • ISSET_ISEMPTY_DIM_OBJ
  • ISSET_ISEMPTY_PROP_OBJ
  • ISSET_ISEMPTY_VAR
  • JMP
  • JMPNZ
  • JMPNZ_EX
  • JMPZ
  • JMPZ_EX
  • JMPZNZ
  • MOD
  • MUL
  • NEW
  • NOP
  • POST_DEC
  • POST_DEC_OBJ
  • POST_INC
  • POST_INC_OBJ
  • PRE_DEC
  • PRE_DEC_OBJ
  • PRE_INC
  • PRE_INC_OBJ
  • PRINT
  • QM_ASSIGN
  • RAISE_ABSTRACT_ERROR
  • RECV
  • RECV_INIT
  • RETURN
  • RETURN_BY_REF
  • SEND_REF
  • SEND_VAL
  • SEND_VAR
  • SEND_VAR_NO_REF
  • SL
  • SR
  • SUB
  • SWITCH_FREE
  • THROW
  • TICKS
  • UNSET_DIM
  • UNSET_OBJ
  • UNSET_VAR
  • USER_OPCODE
  • VERIFY_ABSTRACT_CLASS
  • ZEND_DECLARE_LAMBDA_FUNCTION
  • ZEND_JMP_SET

User Contributed Notes

There are no user contributed notes for this page.


ADD

PHP code

<?php
/*
 * Adds "value1" to "value2" and stores the result into "result".
 * opcode number: 1
 */
echo 1 + 2;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | ADD |  |  | ~0 | 1,2
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

fengdingbo at gmail dot com 14-Aug-2013 01:56
<?php
echo 1+2;
?>
qiufeng@ubuntu:~$ php -dvld.active=1 test.php
Finding entry points
Branch analysis from position: 0
Return found
filename:       /home/qiufeng/test.php
function name:  (null)
number of ops:  3
compiled vars:  none
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   2     0  >   ADD                                              ~0      1, 2
         1      ECHO                                                     ~0
   3     2    > RETURN                                                   1

branch: #  0; line:     2-    3; sop:     0; eop:     2
path #1: 0,


ADD_ARRAY_ELEMENT

PHP code

<?php
/*
 * Add elem-value as an element to array-value
 * opcode number: 72
 */
$a = array(1,2,3);
print_r($a);
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | INIT_ARRAY |  |  | ~0 | 1
 | 1 | ADD_ARRAY_ELEMENT |  |  | ~0 | 2
 | 2 | ADD_ARRAY_ELEMENT |  |  | ~0 | 3
 | 3 | ASSIGN |  |  |  | !0,~0
7 | 4 | SEND_VAR |  |  |  | !0
 | 5 | DO_FCALL |  | 1 |  | 'print_r'
8 | 6 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ADD_CHAR

PHP code

<?php
/*
 * add regular characters in encapsed string
 * opcode number: 54
 */
echo "$a ";
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ADD_VAR |  |  |  | !0
 | 1 | ADD_CHAR |  |  |  | 32
 | 2 | ECHO |  |  |  |
 | 3 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ADD_INTERFACE

PHP code

<?php
/*
 * adds an implemented interface to a class declaration
 * opcode number: 144
 */
interface iA {
}

class A implements iA {
}
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
 | 1 | DECLARE_CLASS |  |  |  | 'a'
 | 2 | ADD_INTERFACE |  |  |  | 'iA'
 | 3 | VERIFY_ABSTRACT_CLASS |  |  |  |
 | 3 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ADD_STRING

PHP code

<?php
/*
 * add string value2 to string value2 and store in result
 * opcode number: 55
 */
echo "hello$a world";
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ADD_STRING |  |  | ~0 | 'hello'
 | 1 | ADD_VAR |  |  | ~0 | ~0,!0
 | 2 | ADD_STRING |  |  | ~0 | ~0,'+world'
 | 3 | ECHO |  |  |  | ~0
7 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ADD_VAR

PHP code

<?php
/*
 * add contents of variable in value2 to string value1 and store in result
 * opcode number: 56
 */
echo "hello$a world";
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ADD_STRING |  |  | ~0 | 'hello'
 | 1 | ADD_VAR |  |  | ~0 | ~0,!0
 | 2 | ADD_STRING |  |  | ~0 | ~0,'+world'
 | 3 | ECHO |  |  |  | ~0
7 | 4 | RETURN |  |  |  | 1

User Contributed Notes

fengdingbo at gmail dot com 22-Jul-2013 02:55
<?php
$abc = 'ok';
$php = 'php';
echo "$php";
?>
qiufeng@ubuntu:~/tmp/php$ php -dvld.active=1 test.php
Finding entry points
Branch analysis from position: 0
Return found
filename:       /home/qiufeng/tmp/php/test.php
function name:  (null)
number of ops:  4
compiled vars:  !0 = $abc, !1 = $php
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   2     0  >   ASSIGN                                                   !0, 'ok'
   3     1      ASSIGN                                                   !1, 'php'
   4     2      ECHO                                                     !1
   6     3    > RETURN                                                   1

branch: #  0; line:     2-    6; sop:     0; eop:     3
path #1: 0,


ASSIGN

PHP code

<?php
/*
 * assigns value1 to result
 * opcode number: 38
 */
$a=1;
$a='a';
$a=new A();
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,1
7 | 1 | ASSIGN |  |  |  | !0,'a'
8 | 2 | ZEND_FETCH_CLASS |  |  | :2 | 'A'
 | 3 | NEW |  |  | $3 | :2
 | 4 | DO_FCALL_BY_NAME |  | 0 |  |
 | 5 | ASSIGN |  |  |  | !0,$3
9 | 6 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_ADD

PHP code

<?php
/*
 * Add value1 to value2 and store in variable indicated by value1
 * opcode number: 23
 */
$a+=3;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_ADD |  |  |  | !0,3
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_BW_AND

PHP code

<?php
/*
 * Performs binary AND on result and value1 and stores in variable indicated by result.
 * opcode number: 32
 */
$a &= 64;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_BW_AND |  |  |  | !0,64
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_BW_OR

PHP code

<?php
/*
 * Performs binary OR on result and value1 and stores in variable indicated by result.
 * opcode number: 31
 */
$a |= 64;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_BW_OR |  |  |  | !0,64
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_BW_XOR

PHP code

<?php
/*
 * Performs binary XOR on result and value1and stores in variable indicated by result.
 * opcode number: 33
 */
$a ^= 64;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_BW_XOR |  |  |  | !0,64
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_CONCAT

PHP code

<?php
/*
 * Concats string values result and value1 and store in variable indicated by result
 * opcode number: 30
 */
$a .= 'z';
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_CONCAT |  |  |  | !0,'z'
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_DIM

PHP code

<?php
/*
 *
 * opcode number: 147
 */
$b = 1;
$a[1][2] = $b;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$b, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,1
7 | 1 | FETCH_DIM_W |  |  | $1 | !1,1
 | 2 | ZEND_ASSIGN_DIM |  |  |  | $1,2
 | 3 | ZEND_OP_DATA |  |  |  | !0,$3
8 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_DIV

PHP code

<?php
/*
 * Divide result by value1and store in variable indicated by result.
 * opcode number: 26
 */
$a/=3;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_DIV |  |  |  | !0,3
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_MOD

PHP code

<?php
/*
 * Perform result mod value1 and store in variable indicated by result
 * opcode number: 27
 */
$a %= 3;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_MOD |  |  |  | !0,3
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_MUL

PHP code

<?php
/*
 * Multiply result by value1 and store in variable indicated by result
 * opcode number: 25
 */
$a*=3;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_MUL |  |  |  | !0,3
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_OBJ

PHP code

<?php
/*
 *
 * opcode number: 136
 */
$obj->a = $otherobj;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj, !1=$otherobj

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_ASSIGN_OBJ |  |  |  | !0,'a'
 | 1 | ZEND_OP_DATA |  |  |  | !1
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_REF

PHP code

<?php
/*
 * indicates that variable indicated by value1 is to be treated in the global scope for the remainder of the current scope.
 * opcode number: 39
 */
global $a;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | FETCH_W | globallock |  | $0 | 'a'
 | 1 | ASSIGN_REF |  |  |  | !0,$0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_SL

PHP code

<?php
/*
 * Shift result by value1 bits to left and store in variable indicated by result
 * opcode number: 28
 */
$a <<= 3;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_SL |  |  |  | !0,3
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_SR

PHP code

<?php
/*
 * Shift result by value1 bits to right and store in variable indicated by result
 * opcode number: 29
 */
$a >>= 3;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_SR |  |  |  | !0,3
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ASSIGN_SUB

PHP code

<?php
/*
 * Subtract value1 from value2 and store in variable indicated by value1
 * opcode number: 24
 */
$a-=3;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN_SUB |  |  |  | !0,3
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


BEGIN_SILENCE

PHP code

<?php
/*
 * prepare to perform function call without displaying error messages
 * opcode number: 57
 */
/* Intentional file error */
$my_file = @file ('non_existent_file') or
   die ("error:'$php_errormsg'");
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$my_file, !1=$php_errormsg

line | # | op | fetch | ext | return | operands
7 | 0 | BEGIN_SILENCE |  |  | ~0 |
 | 1 | SEND_VAL |  |  |  | 'non_existent_file'
 | 2 | DO_FCALL |  | 1 |  | 'file'
 | 3 | END_SILENCE |  |  |  | ~0
 | 4 | ASSIGN |  |  | $2 | !0,$1
 | 5 | JMPNZ_EX |  |  | ~3 | $2,->11
8 | 6 | ADD_STRING |  |  | ~4 | 'error%3A%27'
 | 7 | ADD_VAR |  |  | ~4 | ~4,!1
 | 8 | ADD_CHAR |  |  | ~4 | ~4,39
 | 9 | EXIT |  |  |  | ~4
 | 10 | BOOL |  |  | ~3 | true
 | 11 | FREE |  |  |  | ~3
9 | 12 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


BOOL

PHP code

<?php
/*
 * convert value1 to boolean and store in result??????????
 * opcode number: 52
 */
if (1 || 2 || 1) echo "foo";
//$a = true;
//if($a) echo "foo";
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | JMPNZ_EX |  |  | ~0 | 1,->2
 | 1 | BOOL |  |  | ~0 | 2
 | 2 | JMPNZ_EX |  |  | ~0 | ~0,->4
 | 3 | BOOL |  |  | ~0 | 1
 | 4 | JMPZ |  |  |  | ~0,->7
 | 5 | ECHO |  |  |  | 'foo'
 | 6 | JMP |  |  |  | ->7
9 | 7 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


BOOL_NOT

PHP code

<?php
/*
 * Boolean (logical) not of "value"
 * opcode number: 13
 */
echo !1;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | BOOL_NOT |  |  | ~0 | 1
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


BOOL_XOR

PHP code

<?php
/*
 * Boolean (logical) xor of value1
 * opcode number: 14
 */
echo 1 xor 2;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | BOOL_XOR |  |  | ~0 | 1,2
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


BRK

PHP code

<?php
/*
 * ???  End of a case block exists the current switch block.  Followed by JMP?
 * opcode number: 50
 */
$x = 0;
while(1) {
    if($x == 0) break;
}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,0
7 | 1 | JMPZ |  |  |  | 1,->7
8 | 2 | IS_EQUAL |  |  | ~1 | !0,0
 | 3 | JMPZ |  |  |  | ~1,->6
 | 4 | BRK |  |  |  | 1
 | 5 | JMP |  |  |  | ->6
9 | 6 | JMP |  |  |  | ->1
10 | 7 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


BW_AND

PHP code

<?php
/*
 * Bit-wise and of value1 and value2
 * opcode number: 10
 */
echo 1&2;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | BW_AND |  |  | ~0 | 1,2
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


BW_NOT

PHP code

<?php
/*
 * Bit-wise not of "value"
 * opcode number: 12
 */
echo ~15;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | BW_NOT |  |  | ~0 | 15
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


BW_OR

PHP code

<?php
/*
 * Bit-wise or of value1 and value2
 * opcode number: 9
 */
echo 1|2;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | BW_OR |  |  | ~0 | 1,2
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


BW_XOR

PHP code

<?php
/*
 * Bit-wise xor of value1 and value2
 * opcode number: 11
 */
echo 1^2;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | BW_XOR |  |  | ~0 | 1,2
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


CASE

PHP code

<?php
/*
 * Set result to true if value1 equals value2.  The value2 must be a constant value?
 * opcode number: 48
 */
$i=0;
switch ($i) {
   case 0:
         echo "i=0";
         break;
   case 1:
         echo "i=1";
         break;
   case 2:
         echo "i=2";
         break;
}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$i

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,0
8 | 1 | CASE |  |  | ~1 | !0,0
 | 2 | JMPZ |  |  |  | ~1,->6
9 | 3 | ECHO |  |  |  | 'i%3D0'
10 | 4 | BRK |  |  |  | 1
11 | 5 | JMP |  |  |  | ->8
 | 6 | CASE |  |  | ~1 | !0,1
 | 7 | JMPZ |  |  |  | ~1,->11
12 | 8 | ECHO |  |  |  | 'i%3D1'
13 | 9 | BRK |  |  |  | 1
14 | 10 | JMP |  |  |  | ->13
 | 11 | CASE |  |  | ~1 | !0,2
 | 12 | JMPZ |  |  |  | ~1,->16
15 | 13 | ECHO |  |  |  | 'i%3D2'
16 | 14 | BRK |  |  |  | 1
17 | 15 | JMP |  |  |  | ->16
18 | 16 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


CAST

PHP code

<?php
/*
 * casts value1 as type value2 (type in extended_value)
 * opcode number: 21
 */
echo (int)1;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | CAST |  | 4 | ~0 | 1
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


CATCH

PHP code

<?php
/*
 *
 * opcode number: 107
 */
try {
    $error = 'Always throw this error';
    throw new Exception($error);

    // Code following an exception is not executed.
    echo 'Never executed';

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$error, !1=$e

line | # | op | fetch | ext | return | operands
7 | 0 | ASSIGN |  |  |  | !0,'Always+throw+this+error'
8 | 1 | ZEND_FETCH_CLASS |  |  | :1 | 'Exception'
 | 2 | NEW |  |  | $2 | :1
 | 3 | SEND_VAR |  |  |  | !0
 | 4 | DO_FCALL_BY_NAME |  | 1 |  |
 | 5 | ZEND_THROW |  | 0 |  | $2
11 | 6 | ECHO |  |  |  | 'Never+executed'
13 | 7 | JMP |  |  |  | ->15
 | 8 | ZEND_FETCH_CLASS |  |  | :4 | 'Exception'
 | 9 | ZEND_CATCH |  | 15 |  | $4,!1
14 | 10 | ECHO |  |  |  | 'Caught+exception%3A+'
 | 11 | ZEND_INIT_METHOD_CALL |  |  |  | !1,'getMessage'
 | 12 | DO_FCALL_BY_NAME |  | 0 |  |
 | 13 | ECHO |  |  |  | $6
 | 14 | ECHO |  |  |  | '%0A'
18 | 15 | ECHO |  |  |  | 'Hello+World'
19 | 16 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


CLONE

PHP code

<?php
/*
 *
 * opcode number: 110
 */
$obj = new A();
$copy = clone $obj;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj, !1=$copy

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | ZEND_CLONE |  |  | $4 | !0
 | 5 | ASSIGN |  |  |  | !1,$4
8 | 6 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


CONCAT

PHP code

<?php
/*
 * Concats string values string1 and string2
 * opcode number: 8
 */
echo "hello" . "world";
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | CONCAT |  |  | ~0 | 'hello','world'
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

shadiadiph at yahoo dot com 08-Jun-2012 12:37
Concat . is a very useful tool in php while creating long strings or creating a set of data for example;

<?php
// GIVE THE STRING A NAME
$mystring="";

// NOW USE CONCAT . TO BUILD THE STRING

$mystring.="hello,";
$mystring.="world";

echo $mystring;
?>

output will be
hello,world


CONT

PHP code

<?php
/*
 * Continue next iteration of current loop level
 * opcode number: 51
 */
$foo = false;
while(!$foo) {
    $foo = true;
    continue;
}
?>

PHP opcodes

Function name: (null)

compiled vars: !0 = $foo

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0, false
7 | 1 | BOOL_NOT |  |  | ~1 | !0
 | 2 | JMPZ |  |  |  | ~1, ->5
8 | 3 | ASSIGN |  |  |  | !0, true
9 | 4 | CONT |  |  |  | 1
10 | 5 | JMP |  |  |  | ->1
12 | 6 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


DECLARE_CLASS

PHP code

<?php
/*
 * Declare a class with the name of class-name as the implementation specified by ID.
 * opcode number: 139
 */
class A {
    function methodA(){
        echo "hello world";
    }
}
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
11 | 1 | RETURN |  |  |  | 1

Function name: methodA

Compiled variables: none

line | # | op | fetch | ext | return | operands
8 | 0 | ECHO |  |  |  | 'hello+world'
9 | 1 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


DECLARE_CONST

PHP code

<?php
/*
 *
 * opcode number: 143
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


DECLARE_FUNCTION

PHP code

<?php
/*
 *
 * opcode number: 141
 */
class A{
 function funcA(){
 }
}
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
10 | 1 | RETURN |  |  |  | 1

Function name: funcA

Compiled variables: none

line | # | op | fetch | ext | return | operands
8 | 0 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


DECLARE_INHERITED_CLASS

PHP code

<?php
/*
 *
 * opcode number: 140
 */
 if($b){
 class Foo {
  public static $my_static = 'foo';
  public function staticValue() {
    return self::$my_static;
  }
 }

 class Bar extends Foo {
  public function fooStatic() {
     echo parent::$my_static;
  }
 }
}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$b

line | # | op | fetch | ext | return | operands
6 | 0 | JMPZ |  |  |  | !0,->5
7 | 1 | ZEND_DECLARE_CLASS |  |  | $0 | '%00foo%2Fmnt%2Fworkspace%2Fws_phpscripts%2FPHPopcodes%2Fphpsamples%2FDECLARE_INHERITED_CLASS.php0xb7be503b','foo'
14 | 2 | ZEND_FETCH_CLASS |  |  | :1 | 'Foo'
 | 3 | ZEND_DECLARE_INHERITED_CLASS |  |  | $2 | '%00bar%2Fmnt%2Fworkspace%2Fws_phpscripts%2FPHPopcodes%2Fphpsamples%2FDECLARE_INHERITED_CLASS.php0xb7be50bc','bar'
19 | 4 | JMP |  |  |  | ->5
20 | 5 | RETURN |  |  |  | 1

Function name: staticValue

Compiled variables: !0=$my_static

line | # | op | fetch | ext | return | operands
10 | 0 | ZEND_FETCH_CLASS |  |  |  |
 | 1 | FETCH_R | staticmember |  | $1 | 'my_static'
 | 2 | RETURN |  |  |  | $1
11 | 3 | RETURN |  |  |  | null

Function name: fooStatic

Compiled variables: !0=$my_static

line | # | op | fetch | ext | return | operands
16 | 0 | ZEND_FETCH_CLASS |  |  | :0 |
 | 1 | FETCH_R | staticmember |  | $1 | 'my_static'
 | 2 | ECHO |  |  |  | $1
17 | 3 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


DECLARE_INHERITED_CLASS_DELAYED

PHP code

<?php
/*
 *
 * opcode number: 145
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


DIV

PHP code

<?php
/*
 * Divides "value1" by "value2" and stores the result into "result".
 * opcode number: 4
 */
echo 6/3;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | DIV |  |  | ~0 | 6,3
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


DO_FCALL

PHP code

<?php
/*
 * Call a function.  Following multiple SEND_VAL, SEND_VAR or SEND_REF.
 * opcode number: 60
 */
$a = phpinfo();
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | DO_FCALL |  | 0 |  | 'phpinfo'
 | 1 | ASSIGN |  |  |  | !0,$0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


DO_FCALL_BY_NAME

PHP code

<?php
/*
 * Call a function by name.  Following INIT_FCALL_BY_NAME and multiple SEND_VAL, SEND_VAR or SEND_REF.
 * opcode number: 61
 */
$x = 'phpinfo';
$a = $x();
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,'phpinfo'
7 | 1 | INIT_FCALL_BY_NAME |  |  |  | !0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !1,$1
8 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ECHO

PHP code

<?php
/*
 * Dump text
 * opcode number: 40
 */
echo "hello world";
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | ECHO |  |  |  | 'hello+world'
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


END_SILENCE

PHP code

<?php
/*
 * no longer surpress error messages
 * opcode number: 58
 */
$my_file = @file ('non_existent_file') or
   die ("error:'$php_errormsg'");
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$my_file, !1=$php_errormsg

line | # | op | fetch | ext | return | operands
6 | 0 | BEGIN_SILENCE |  |  | ~0 |
 | 1 | SEND_VAL |  |  |  | 'non_existent_file'
 | 2 | DO_FCALL |  | 1 |  | 'file'
 | 3 | END_SILENCE |  |  |  | ~0
 | 4 | ASSIGN |  |  | $2 | !0,$1
 | 5 | JMPNZ_EX |  |  | ~3 | $2,->11
7 | 6 | ADD_STRING |  |  | ~4 | 'error%3A%27'
 | 7 | ADD_VAR |  |  | ~4 | ~4,!1
 | 8 | ADD_CHAR |  |  | ~4 | ~4,39
 | 9 | EXIT |  |  |  | ~4
 | 10 | BOOL |  |  | ~3 | true
 | 11 | FREE |  |  |  | ~3
8 | 12 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


EXIT

PHP code

<?php
/*
 * Exit running after dumping "message".
 * opcode number: 79
 */
die("foobar");
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | EXIT |  |  |  | 'foobar'
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


EXT_FCALL_BEGIN

PHP code

<?php
/*
 *
 * opcode number: 102
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


EXT_FCALL_END

PHP code

<?php
/*
 *
 * opcode number: 103
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


EXT_NOP

PHP code

<?php
/*
 *
 * opcode number: 104
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


EXT_STMT

PHP code

<?php
/*
 *
 * opcode number: 101
 */
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
7 | 0 | EXT_STMT |  |  |  |
8 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FE_FETCH

PHP code

<?php
/*
 * Fetch an element from iterator.  If no element is available, jump to address.  Followed by OP_DATA?
 * opcode number: 78
 */
$a = array(1,2,3);
foreach($a as $num){
    print $num;
}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a, !1=$num

line | # | op | fetch | ext | return | operands
6 | 0 | INIT_ARRAY |  |  | ~0 | 1
 | 1 | ADD_ARRAY_ELEMENT |  |  | ~0 | 2
 | 2 | ADD_ARRAY_ELEMENT |  |  | ~0 | 3
 | 3 | ASSIGN |  |  |  | !0,~0
7 | 4 | FE_RESET |  |  | $2 | !0,->11
 | 5 | FE_FETCH |  |  | $3 | $2,->11
 | 6 | ZEND_OP_DATA |  |  |  |
 | 7 | ASSIGN |  |  |  | !1,$3
8 | 8 | PRINT |  |  | ~5 | !1
 | 9 | FREE |  |  |  | ~5
9 | 10 | JMP |  |  |  | ->5
 | 11 | SWITCH_FREE |  |  |  | $2
10 | 12 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FE_RESET

PHP code

<?php
/*
 * Initialize an iterator on array-value.  If the array is empty, jump to address.  Followed by FE_FETCH.
 * opcode number: 77
 */
$a = array(1,2,3);
foreach($a as $num){
    print $num;
}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a, !1=$num

line | # | op | fetch | ext | return | operands
6 | 0 | INIT_ARRAY |  |  | ~0 | 1
 | 1 | ADD_ARRAY_ELEMENT |  |  | ~0 | 2
 | 2 | ADD_ARRAY_ELEMENT |  |  | ~0 | 3
 | 3 | ASSIGN |  |  |  | !0,~0
7 | 4 | FE_RESET |  |  | $2 | !0,->11
 | 5 | FE_FETCH |  |  | $3 | $2,->11
 | 6 | ZEND_OP_DATA |  |  |  |
 | 7 | ASSIGN |  |  |  | !1,$3
8 | 8 | PRINT |  |  | ~5 | !1
 | 9 | FREE |  |  |  | ~5
9 | 10 | JMP |  |  |  | ->5
 | 11 | SWITCH_FREE |  |  |  | $2
10 | 12 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_CLASS

PHP code

<?php
/*
 *
 * opcode number: 109
 */
$obj = new A();
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_CONSTANT

PHP code

<?php
/*
 * Fetch the constant value bound to the specified name (name) and stores it into a variable (result).
 * opcode number: 99
 */
define("FOO", "something");
echo FOO;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | SEND_VAL |  |  |  | 'FOO'
 | 1 | SEND_VAL |  |  |  | 'something'
 | 2 | DO_FCALL |  | 2 |  | 'define'
7 | 3 | FETCH_CONSTANT |  |  | ~1 | 'FOO'
 | 4 | ECHO |  |  |  | ~1
8 | 5 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_DIM_FUNC_ARG

PHP code

<?php
/*
 *
 * opcode number: 93
 */

function foo(&$x)
{
  print($x);
}

$x = array(0, 1, 2, 3, 4, 5);
$z = "foo";

$z($x[0]);

?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$z

line | # | op | fetch | ext | return | operands
7 | 0 | NOP |  |  |  |
12 | 1 | INIT_ARRAY |  |  | ~0 | 0
 | 2 | ADD_ARRAY_ELEMENT |  |  | ~0 | 1
 | 3 | ADD_ARRAY_ELEMENT |  |  | ~0 | 2
 | 4 | ADD_ARRAY_ELEMENT |  |  | ~0 | 3
 | 5 | ADD_ARRAY_ELEMENT |  |  | ~0 | 4
 | 6 | ADD_ARRAY_ELEMENT |  |  | ~0 | 5
 | 7 | ASSIGN |  |  |  | !0,~0
13 | 8 | ASSIGN |  |  |  | !1,'foo'
15 | 9 | INIT_FCALL_BY_NAME |  |  |  | !1
 | 10 | FETCH_DIM_FUNC_ARG |  |  | $3 | !0,0
 | 11 | SEND_VAR |  |  |  | $3
 | 12 | DO_FCALL_BY_NAME |  | 1 |  |
17 | 13 | RETURN |  |  |  | 1

Function name: foo

Compiled variables: !0=$x

line | # | op | fetch | ext | return | operands
7 | 0 | RECV |  |  |  | 1
9 | 1 | PRINT |  |  | ~0 | !0
 | 2 | FREE |  |  |  | ~0
10 | 3 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


FETCH_DIM_IS

PHP code

<?php
/*
 *
 * opcode number: 90
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


FETCH_DIM_R

PHP code

<?php
/*
 * Fetch the value of the element at "index" in "array-value" to store it in "result".  Read-only?
 * opcode number: 81
 */
$x = array(1,2,3);
$a = 'x';
echo $$a[0];
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | INIT_ARRAY |  |  | ~0 | 1
 | 1 | ADD_ARRAY_ELEMENT |  |  | ~0 | 2
 | 2 | ADD_ARRAY_ELEMENT |  |  | ~0 | 3
 | 3 | ASSIGN |  |  |  | !0,~0
7 | 4 | ASSIGN |  |  |  | !1,'x'
8 | 5 | FETCH_DIM_R |  |  | $3 | !1,0
 | 6 | FETCH_R | local |  | $4 | $3
 | 7 | ECHO |  |  |  | $4
9 | 8 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_DIM_RW

PHP code

<?php
/*
 * Fetch the value of the element at "index" in "array-value" to store it in "result".
 * opcode number: 87
 */
$x = array(1,2,3);
$a = 'x';
$$a[0]++;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | INIT_ARRAY |  |  | ~0 | 1
 | 1 | ADD_ARRAY_ELEMENT |  |  | ~0 | 2
 | 2 | ADD_ARRAY_ELEMENT |  |  | ~0 | 3
 | 3 | ASSIGN |  |  |  | !0,~0
7 | 4 | ASSIGN |  |  |  | !1,'x'
8 | 5 | FETCH_DIM_R |  |  | $3 | !1,0
 | 6 | FETCH_RW | local |  | $4 | $3
 | 7 | POST_INC |  |  | ~5 | $4
 | 8 | FREE |  |  |  | ~5
9 | 9 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_DIM_TMP_VAR

PHP code

<?php
/*
 * Fetch an entry of an array from a temporal variable?
 * opcode number: 98
 */
list($x) = array("X");
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x

line | # | op | fetch | ext | return | operands
6 | 0 | INIT_ARRAY |  |  | ~0 | 'X'
 | 1 | FETCH_DIM_TMP_VAR |  |  | $1 | ~0,0
 | 2 | ASSIGN |  |  |  | !0,$1
 | 3 | FREE |  |  |  | ~0
7 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_DIM_UNSET

PHP code

<?php
/*
 * Fetch an entry of an array variable for the purpose of unset() operation.
 * opcode number: 96
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


FETCH_DIM_W

PHP code

<?php
/*
 * Fetch the value of the element at "index" in "array-value" to store it in "result".  Write-only?
 * opcode number: 84
 */
$a = 1;
while($a > 0){
    $a = 0;
}
/*$input =array(1,2,3);
while (list($var,) = @each($input)){
    unset($$var);
}*/
/*$a = array(1,2,3);
$x = 'a';
$$x[0] = 1;*/

/*while ($b = each($a)) {
  print $b;
}*/

?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,1
7 | 1 | IS_SMALLER |  |  | ~1 | 0,!0
 | 2 | JMPZ |  |  |  | ~1,->5
8 | 3 | ASSIGN |  |  |  | !0,0
9 | 4 | JMP |  |  |  | ->1
23 | 5 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_FUNC_ARG

PHP code

<?php
/*
 *
 * opcode number: 92
 */
function foo($x)
{
}

$x = 1;
$y = "x";
$z = "foo";

$z($$y);

?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$y, !2=$z

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
10 | 1 | ASSIGN |  |  |  | !0,1
11 | 2 | ASSIGN |  |  |  | !1,'x'
12 | 3 | ASSIGN |  |  |  | !2,'foo'
14 | 4 | INIT_FCALL_BY_NAME |  |  |  | !2
 | 5 | FETCH_FUNC_ARG | local |  | $3 | !1
 | 6 | SEND_VAR |  |  |  | $3
 | 7 | DO_FCALL_BY_NAME |  | 1 |  |
16 | 8 | RETURN |  |  |  | 1

Function name: foo

Compiled variables: !0=$x

line | # | op | fetch | ext | return | operands
6 | 0 | RECV |  |  |  | 1
8 | 1 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


FETCH_IS

PHP code

<?php
/*
 * Fetch the value from variable which is to be used to test if it is set or not, through isset()/isempty().
 * opcode number: 89
 */
echo isset($_SESSION['userid']);
echo isset($_SESSION['userid'][1]);
echo isset($_SESSION->prop->prop);
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | FETCH_IS |  |  | $0 | '_SESSION'
 | 1 | ZEND_ISSET_ISEMPTY_DIM_OBJ |  | 1 | ~1 | $0,'userid'
 | 2 | ECHO |  |  |  | ~1
7 | 3 | FETCH_IS |  |  | $2 | '_SESSION'
 | 4 | FETCH_DIM_IS |  |  | $3 | $2,'userid'
 | 5 | ZEND_ISSET_ISEMPTY_DIM_OBJ |  | 1 | ~4 | $3,1
 | 6 | ECHO |  |  |  | ~4
8 | 7 | FETCH_IS |  |  | $5 | '_SESSION'
 | 8 | FETCH_OBJ_IS |  |  | $6 | $5,'prop'
 | 9 | ZEND_ISSET_ISEMPTY_PROP_OBJ |  |  | ~7 | $6,'prop'
 | 10 | ECHO |  |  |  | ~7
9 | 11 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_OBJ_FUNC_ARG

PHP code

<?php
/*
 *
 * opcode number: 94
 */
include './classA.php';

function foo(&$x)
{
  print($x);
}

$z = "foo";

$obj = new A();
print $obj->num;
$z($obj->num);

?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$z, !1=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | INCLUDE_OR_EVAL |  |  |  | '.%2FclassA.php',INCLUDE
8 | 1 | NOP |  |  |  |
13 | 2 | ASSIGN |  |  |  | !0,'foo'
15 | 3 | ZEND_FETCH_CLASS |  |  | :2 | 'A'
 | 4 | NEW |  |  | $3 | :2
 | 5 | DO_FCALL_BY_NAME |  | 0 |  |
 | 6 | ASSIGN |  |  |  | !1,$3
16 | 7 | FETCH_OBJ_R |  |  | $6 | !1,'num'
 | 8 | PRINT |  |  | ~7 | $6
 | 9 | FREE |  |  |  | ~7
17 | 10 | INIT_FCALL_BY_NAME |  |  |  | !0
 | 11 | FETCH_OBJ_FUNC_ARG |  |  | $8 | !1,'num'
 | 12 | SEND_VAR |  |  |  | $8
 | 13 | DO_FCALL_BY_NAME |  | 1 |  |
19 | 14 | RETURN |  |  |  | 1

Function name: foo

Compiled variables: !0=$x

line | # | op | fetch | ext | return | operands
8 | 0 | RECV |  |  |  | 1
10 | 1 | PRINT |  |  | ~0 | !0
 | 2 | FREE |  |  |  | ~0
11 | 3 | RETURN |  |  |  | null

Function name: foo

Compiled variables: !0=$x

line | # | op | fetch | ext | return | operands
8 | 0 | RECV |  |  |  | 1
10 | 1 | PRINT |  |  | ~0 | !0
 | 2 | FREE |  |  |  | ~0
11 | 3 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


FETCH_OBJ_IS

PHP code

<?php
/*
 *
 * opcode number: 91
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


FETCH_OBJ_R

PHP code

<?php
/*
 * Fetch the "prop-name" property value of this object.  Read-only?
 * opcode number: 82
 */
$x = new A();
$a = 'x';
echo $$a->num;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | ASSIGN |  |  |  | !1,'x'
8 | 5 | FETCH_R | local |  | $5 | !1
 | 6 | FETCH_OBJ_R |  |  | $6 | $5,'num'
 | 7 | ECHO |  |  |  | $6
9 | 8 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_OBJ_RW

PHP code

<?php
/*
 * Fetch the "prop-name" property value of this object.
 * opcode number: 88
 */
$x = new A();
$a = 'x';
$$a->num++;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | ASSIGN |  |  |  | !1,'x'
8 | 5 | FETCH_RW | local |  | $5 | !1
 | 6 | ZEND_POST_INC_OBJ |  |  | ~7 | $5,'num'
 | 7 | FREE |  |  |  | ~7
9 | 8 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_OBJ_UNSET

PHP code

<?php
/*
 * Fetch a property of an object variable for the purpose of unset() operation.
 * opcode number: 97
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


FETCH_OBJ_W

PHP code

<?php
/*
 * Fetch an object from the property of this object and write to the property of the fectched object.
 * opcode number: 85
 */
$foo = new stdclass;
$foo->bar = new stdclass;
$foo->bar->baz = 'quix';
?>

PHP opcodes

Function name: (null)

Compiled variables: !0 = $foo

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  | 4 | :0 | 'stdclass'
 | 1 | NEW |  |  |  | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0
7 | 4 | ZEND_FETCH_CLASS |  | 4 | :5 | 'stdclass'
 | 5 | NEW |  |  |  | :5
 | 6 | DO_FCALL_BY_NAME |  | 0 |  |
 | 7 | ZEND_ASSIGN_OBJ |  |  |  | !0, 'bar'
 | 8 | ZEND_OP_DATA |  |  |  |
8 | 9 | FETCH_OBJ_W |  |  |  | !0, 'bar'
 | 10 | ZEND_ASSIGN_OBJ |  |  |  | 'baz'
 | 11 | ZEND_OP_DATA |  |  |  | 'quix'
9 | 12 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_R

PHP code

<?php
/*
 * Fetch the value of the varible of "name" to assign it to a variable?  Read-only?
 * opcode number: 80
 */
$x = 1;
$a = 'x';
echo $$a;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,1
7 | 1 | ASSIGN |  |  |  | !1,'x'
8 | 2 | FETCH_R | local |  | $2 | !1
 | 3 | ECHO |  |  |  | $2
9 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_RW

PHP code

<?php
/*
 * Fetch the value of the varible of "name" to assign it to a variable?
 * opcode number: 86
 */
$x = 1;
$a = 'x';
$$a++;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,1
7 | 1 | ASSIGN |  |  |  | !1,'x'
8 | 2 | FETCH_RW | local |  | $2 | !1
 | 3 | POST_INC |  |  | ~3 | $2
 | 4 | FREE |  |  |  | ~3
9 | 5 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FETCH_UNSET

PHP code

<?php
/*
 * Fetch a variable for the purpose of unset() operation.
 * opcode number: 95
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


FETCH_W

PHP code

<?php
/*
 * Fetch the value of the varible of "name" to assign it to a variable?  Write-only?
 * opcode number: 83
 */
$x = 1;
$a = 'x';
$$a = 2;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,1
7 | 1 | ASSIGN |  |  |  | !1,'x'
8 | 2 | FETCH_W | local |  | $2 | !1
 | 3 | ASSIGN |  |  |  | $2,2
9 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


FREE

PHP code

<?php
/*
 * Release the allocated space of the value.
 * opcode number: 70
 */
 print "Hello World";
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | PRINT |  |  | ~0 | 'Hello+World'
 | 1 | FREE |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


GOTO

PHP code

<?php
/*
 *
 * opcode number: 100
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


HANDLE_EXCEPTION

PHP code

<?php
/*
 *
 * opcode number: 149
 */
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


INCLUDE_OR_EVAL

PHP code

<?php
/*
 * Include the file specified by filename and eval it.
 * opcode number: 73
 */
include("test.php");
eval("test.php");
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | INCLUDE_OR_EVAL |  |  |  | 'test.php',INCLUDE
7 | 1 | INCLUDE_OR_EVAL |  |  |  | 'test.php',EVAL
8 | 2 | RETURN |  |  |  | 1

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
2 | 0 | DO_FCALL |  | 0 |  | 'phpinfo'
3 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


INIT_ARRAY

PHP code

<?php
/*
 * Allocate a new array with elem-value as the first element of the array.  Followed by ADD_ARRAY_ELEMENT.
 * opcode number: 71
 */
$a = array(1,2,3);
print_r($a);
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | INIT_ARRAY |  |  | ~0 | 1
 | 1 | ADD_ARRAY_ELEMENT |  |  | ~0 | 2
 | 2 | ADD_ARRAY_ELEMENT |  |  | ~0 | 3
 | 3 | ASSIGN |  |  |  | !0,~0
7 | 4 | SEND_VAR |  |  |  | !0
 | 5 | DO_FCALL |  | 1 |  | 'print_r'
8 | 6 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


INIT_FCALL_BY_NAME

PHP code

<?php
/*
 * prepare for a function call to function value1
 * opcode number: 59
 */
$x = 'phpinfo';
$a = $x();
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,'phpinfo'
7 | 1 | INIT_FCALL_BY_NAME |  |  |  | !0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !1,$1
8 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


INIT_METHOD_CALL

PHP code

<?php
/*
 * Prepare for a method call.  Followed by DO_FCALL.
 * opcode number: 112
 */
class A {
  var $num;
    function incrementNum(){
    $num++;
  }
}

$obj = new A();
$obj->incrementNum();

?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
13 | 1 | ZEND_FETCH_CLASS |  |  | :1 | 'A'
 | 2 | NEW |  |  | $2 | :1
 | 3 | DO_FCALL_BY_NAME |  | 0 |  |
 | 4 | ASSIGN |  |  |  | !0,$2
14 | 5 | ZEND_INIT_METHOD_CALL |  |  |  | !0,'incrementNum'
 | 6 | DO_FCALL_BY_NAME |  | 0 |  |
16 | 7 | RETURN |  |  |  | 1

Function name: incrementNum

Compiled variables: !0=$num

line | # | op | fetch | ext | return | operands
9 | 0 | POST_INC |  |  | ~0 | !0
 | 1 | FREE |  |  |  | ~0
10 | 2 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


INIT_NS_FCALL_BY_NAME

PHP code

<?php
/*
 *
 * opcode number: 69
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


INIT_STATIC_METHOD_CALL

PHP code

<?php
/*
 *
 * opcode number: 113
 */
class Foo {
    public static function aStaticMethod() {
            echo "hello world\n";
    }
}

Foo::aStaticMethod();
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
12 | 1 | ZEND_INIT_STATIC_METHOD_CALL |  |  |  | 'Foo','aStaticMethod'
 | 2 | ZEND_OP_DATA |  |  |  | 'foo%3A%3Aastaticmethod'
 | 3 | DO_FCALL_BY_NAME |  | 0 |  |
13 | 4 | RETURN |  |  |  | 1

Function name: aStaticMethod

Compiled variables: none

line | # | op | fetch | ext | return | operands
8 | 0 | ECHO |  |  |  | 'hello+world%0A'
9 | 1 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


INIT_STRING

PHP code

<?php
/*
 * appears to create a string variable in result
 * opcode number: 53
 */
echo "hello$a world";
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ADD_STRING |  |  | ~0 | 'hello'
 | 1 | ADD_VAR |  |  | ~0 | ~0,!0
 | 2 | ADD_STRING |  |  | ~0 | ~0,'+world'
 | 3 | ECHO |  |  |  | ~0
7 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


INSTANCEOF

PHP code

<?php
/*
 *
 * opcode number: 138
 */
$obj = new A();

if ($obj instanceof A) {
   echo 'A';
}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
8 | 4 | ZEND_FETCH_CLASS |  |  | :4 | 'A'
 | 5 | ZEND_INSTANCEOF |  |  | ~5 | !0,$4
 | 6 | JMPZ |  |  |  | ~5,->9
9 | 7 | ECHO |  |  |  | 'A'
10 | 8 | JMP |  |  |  | ->9
11 | 9 | RETURN |  |  |  | 1

User Contributed Notes

portugal {at} jawira {dot} com 18-Jun-2015 05:49
I'm commenting here because the note from "admin at torntech" is incomplete. You can perfectly replace "is_a()" function with "instanceof" operator. However you must use a variable to store the class name, otherwise you will get a Parse error:

<?php
$object = new \stdClass();
$class_name = '\stdClass';

var_dump($object instanceof $class_name);    // bool(true)
var_dump($object instanceof '\stdClass');    // Parse error: syntax error, unexpected ''\stdClass'' (T_CONSTANT_ENCAPSED_STRING)
?>

Please go to Type Operators page for more details about "instanceof": http://php.net/manual/en/language.operators.type.php
krall dot eugene at gmail dot com 11-Mar-2015 06:54
When checking instanceof against a class that implements the class in question, it will return true.

<?php

interface ExampleInterface
{
    public function interfaceMethod();

}

class ExampleClass implements ExampleInterface
{
    public function interfaceMethod()
    {
        return 'Hello World!';
    }
}

$exampleInstance = new ExampleClass();

if($exampleInstance instanceof ExampleInterface)
    echo 'Yes, it is';
else
    echo 'No, it is not';

?>

The result:

Yes, it is
admin at torntech dot com 11-Feb-2015 03:46
Despite this section being opcode examples, for php 5.3+ see also is_a()
*Note php 5.0 - 5.2 is_a() was depreciated in favor of instanceof but was undepreciated in 5.3+.

Allows for string comparison against the class name and functions in the same manner as cody mentions like so.
<?php
namespace Foo{
    class Bar {}
    class Baz extends Bar{}
}
namespace {
    $baz = new Foo\Baz;

    var_dump($baz instanceof Foo\Bar);
    /* returns bool(true) */

    var_dump(is_a($baz, 'Foo\\Bar'));
    /* returns bool(true) */

    var_dump($baz instanceof 'Foo\\Bar');
    /* returns Runtime parse error */
}
?>
cody at codysnider dot com 21-Jun-2012 03:54
When checking instanceof against a subclass of the class in question, it will return true.

<?php

class Foo {

    public $foobar = 'Foo';

    public function test() {
        echo $this->foobar . "\n";
    }

}

class Bar extends Foo {

    public $foobar = 'Bar';

}

$a = new Foo();
$b = new Bar();

echo "use of test() method\n";
$a->test();
$b->test();

echo "instanceof Foo\n";
var_dump($a instanceof Foo); // TRUE
var_dump($b instanceof Foo); // TRUE

echo "instanceof Bar\n";
var_dump($a instanceof Bar); // FALSE
var_dump($b instanceof Bar); // TRUE

echo "subclass of Foo\n";
var_dump(is_subclass_of($a, 'Foo')); // FALSE
var_dump(is_subclass_of($b, 'Foo')); // TRUE

echo "subclass of Bar\n";
var_dump(is_subclass_of($a, 'Bar')); // FALSE
var_dump(is_subclass_of($b, 'Bar')); // FALSE

?>

Result (CLI, 5.4.4):

use of test() method
Foo
Bar
instanceof Foo
bool(true)
bool(true)
instanceof Bar
bool(false)
bool(true)
subclass of Foo
bool(false)
bool(true)
subclass of Bar
bool(false)
bool(false)
asdf at asdf dot com 04-Apr-2012 07:13
Please note, that you get no warnings on non-existent classes:

<?php
class A() {
}

$a = new A();

$exists = ($a instanceof A); //TRUE
$exists = ($a instanceof NonExistentClass); //FALSE


IS_EQUAL

PHP code

<?php
/*
 * compares if value1 and value2 are equal
 * opcode number: 17
 */
echo (1 == 1);
echo (1 == 'c');
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | IS_EQUAL |  |  | ~0 | 1,1
 | 1 | ECHO |  |  |  | ~0
7 | 2 | IS_EQUAL |  |  | ~1 | 1,'c'
 | 3 | ECHO |  |  |  | ~1
8 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


IS_IDENTICAL

PHP code

<?php
/*
 * Compares value1 and value2 to see if they are equal AND have the same type
 * opcode number: 15
 */
echo (1===1);
echo (1==='a');
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | IS_IDENTICAL |  |  | ~0 | 1,1
 | 1 | ECHO |  |  |  | ~0
7 | 2 | IS_IDENTICAL |  |  | ~1 | 1,'a'
 | 3 | ECHO |  |  |  | ~1
8 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


IS_NOT_EQUAL

PHP code

<?php
/*
 * compares if value1 and value2 are not equal
 * opcode number: 18
 */
echo (1 != 1);
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | IS_NOT_EQUAL |  |  | ~0 | 1,1
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


IS_NOT_IDENTICAL

PHP code

<?php
/*
 * compares value1 and value2 to see if they are unequal or of different types
 * opcode number: 16
 */
echo (1 !== 1);
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | IS_NOT_IDENTICAL |  |  | ~0 | 1,1
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


IS_SMALLER

PHP code

<?php
/*
 * compares if value1 is less than value2
 * opcode number: 19
 */
echo (1 < 2);
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | IS_SMALLER |  |  | ~0 | 1,2
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


IS_SMALLER_OR_EQUAL

PHP code

<?php
/*
 * compares if value1 is less than or equal to value2
 * opcode number: 20
 */
echo (1 >= 2);
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | IS_SMALLER_OR_EQUAL |  |  | ~0 | 2,1
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ISSET_ISEMPTY_DIM_OBJ

PHP code

<?php
/*
 * Tests if the entry of an array is set or not.
 * opcode number: 115
 */
if(isset($a[0])) return 0;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_ISSET_ISEMPTY_DIM_OBJ |  | 1 | ~0 | !0,0
 | 1 | JMPZ |  |  |  | ~0,->4
 | 2 | RETURN |  |  |  | 0
 | 3 | JMP |  |  |  | ->4
7 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ISSET_ISEMPTY_PROP_OBJ

PHP code

<?php
/*
 *
 * opcode number: 148
 */
$obj = new A();
if(empty($obj->num)) return 0;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | ZEND_ISSET_ISEMPTY_PROP_OBJ |  |  | ~4 | !0,'num'
 | 5 | JMPZ |  |  |  | ~4,->8
 | 6 | RETURN |  |  |  | 0
 | 7 | JMP |  |  |  | ->8
8 | 8 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


ISSET_ISEMPTY_VAR

PHP code

<?php
/*
 * Tests if the value of a variable is set or not.
 * opcode number: 114
 */
if(isset($a)){ return 0;}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_ISSET_ISEMPTY_VAR |  | 5 | ~0 | !0
 | 1 | JMPZ |  |  |  | ~0,->4
 | 2 | RETURN |  |  |  | 0
 | 3 | JMP |  |  |  | ->4
7 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


JMP

PHP code

<?php
/*
 * Unconditonally jump to the address
 * opcode number: 42
 */
$foo = false;
while(!$foo) {
    $foo = true;
}
?>

PHP opcodes

Function name: (null)

compiled vars: !0 = $foo

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0, false
7 | 1 | BOOL_NOT |  |  | ~1 | !0
 | 2 | JMPZ |  |  |  | ~1, ->5
8 | 3 | ASSIGN |  |  |  | !0, true
9 | 4 | JMP |  |  |  | ->1
11 | 5 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


JMPNZ

PHP code

<?php
/*
 * Jump to the address if the value is not zero
 * opcode number: 44
 */
for($i=0; $i<3; $i++){
    echo "hi";
}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$i

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,0
 | 1 | IS_SMALLER |  |  | ~1 | !0,3
 | 2 | JMPZNZ |  | 6 |  | ~1,->8
 | 3 | POST_INC |  |  | ~2 | !0
 | 4 | FREE |  |  |  | ~2
 | 5 | JMP |  |  |  | ->1
7 | 6 | ECHO |  |  |  | 'hi'
8 | 7 | JMP |  |  |  | ->3
9 | 8 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


JMPNZ_EX

PHP code

<?php
/*
 * Jump to the address if the xor of value1 and value2 is zero .. ???
 * opcode number: 47
 */
if(1^2) return;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | BW_XOR |  |  | ~0 | 1,2
 | 1 | JMPZ |  |  |  | ~0,->4
 | 2 | RETURN |  |  |  | null
 | 3 | JMP |  |  |  | ->4
7 | 4 | RETURN |  |  |  | 1

User Contributed Notes

gennady at kovshenin dot com 16-Mar-2015 10:04
This one is emitted inside a do .. while structure where booleans are compared with logical OR.


JMPZ

PHP code

<?php
/*
 * Jump to the address if the value is zero
 * opcode number: 43
 */
if($a != 0) echo "foo";
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | IS_NOT_EQUAL |  |  | ~0 | !0,0
 | 1 | JMPZ |  |  |  | ~0,->4
 | 2 | ECHO |  |  |  | 'foo'
 | 3 | JMP |  |  |  | ->4
7 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


JMPZ_EX

PHP code

<?php
/*
 * Jump to the address if the xor of value1 and value2 is zero .. ???
 * opcode number: 46
 */

?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
7 | 0 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


JMPZNZ

PHP code

<?php
/*
 * Jump to the address given in the operands if the value is zero;
 * jump to the address given in extended data if nonzero.
 * opcode number: 45
 */
for($i=0; $i<3; $i++){
    echo "hi";
}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$i

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,0
 | 1 | IS_SMALLER |  |  | ~1 | !0,3
 | 2 | JMPZNZ |  | 6 |  | ~1,->8
 | 3 | POST_INC |  |  | ~2 | !0
 | 4 | FREE |  |  |  | ~2
 | 5 | JMP |  |  |  | ->1
7 | 6 | ECHO |  |  |  | 'hi'
8 | 7 | JMP |  |  |  | ->3
9 | 8 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


MOD

PHP code

<?php
/*
 * Makes the value of "result" congruent to "value1" modulo "value2".
 * opcode number: 5
 */
echo 6 % 3;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | MOD |  |  | ~0 | 6,3
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

martynas dot stropa at gmail dot com 29-Oct-2014 01:45
A function to handle integers of any length, including negatives. Returns remainder but also calculates division in the process (could be useful in some cases).

<?php
function remainder($dividend, $divisor) {
    if ($dividend == 0 || $divisor == 0) return 0;

    $dividend .= '';
    $remainder = 0;
    $division = '';

    // negative case
    while ($dividend < 0) {
        $dividend += $divisor;
        if ($dividend >= 0) return $dividend;
    }

    // positive case
    while (($remainder.$dividend)*1 > $divisor) {
        // get remainder big enough to divide
        while ($remainder*1 < $divisor) {
            $remainder .= $dividend[0];
            $remainder *= 1;
            $dividend = substr($dividend, 1);
        }

        // get highest multiplicator for remainder
        $mult = floor($remainder / $divisor);

        // add multiplicator to division
        $division .= $mult.'';

        // subtract from remainder
        $remainder -= $mult*$divisor;
    }

    // add remaining zeros if any, to division
    if (strlen($dividend) > 0 && $dividend*1 == 0) {
        $division .= $dividend;
    }

    return $remainder;
}
doug at unlikelysource dot com 06-Oct-2014 08:30
To determine odd or even it's faster and more efficient to use the bitwise & operator:
$a = 3;
if ($a & 1) {
    echo 'odd';
} else {
    echo 'even';
}
// returns 'odd'
napora dot adam at gmail dot com 26-May-2012 01:22
Might be helpful:

I had to create and "INSERT INTO" on every 50th row:

<?php
$lineBreak = 50;
$tableName = 'user';
$tableFields = 'id, name, password';
?>

in the loop:

<?php
if ($counter === 0 || $counter % $lineBreak === 0) {
    echo "INSERT INTO $tableName ($tableFields) VALUES (";
} else {
    echo ' (';
}

// add values comma-delimited

if ($counter === 0 || $counter % $lineBreak === 0) {
    $insertStmt .= ');';
} else {
    $insertStmt .= '),';
}
?>

(in my case I had to check if $no % $o === 0)
shaunny at hotmail dot com 08-Jan-2010 03:35
A simple check to see if a number is divisible by another number,
i.e. if a number is even.

<?php
if ($counter % 2) {
echo "This number is not even.";
} else {
echo "This number is even.";
}
?>


MUL

PHP code

<?php
/*
 * Multiplys "value1" by "value2" and stores the result into "result".
 * opcode number: 3
 */
echo 2*3;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | MUL |  |  | ~0 | 2,3
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


NEW

PHP code

<?php
/*
 * Construct an instance of "type" and store the reference to the object into "result".
 * opcode number: 68
 */
$obj = new A();
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


NOP

PHP code

<?php
/*
 * no operation
 * opcode number: 0
 */
function A(){};
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
7 | 1 | RETURN |  |  |  | 1

Function name: A

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


POST_DEC

PHP code

<?php
/*
 * decrements variable indicated by value1 by 1 (after performing other operations) and stores in result
 * opcode number: 37
 */
$a--;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | POST_DEC |  |  | ~0 | !0
 | 1 | FREE |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


POST_DEC_OBJ

PHP code

<?php
/*
 *
 * opcode number: 135
 */
$obj = new A();
$obj->num--;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | ZEND_POST_DEC_OBJ |  |  | ~5 | !0,'num'
 | 5 | FREE |  |  |  | ~5
8 | 6 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


POST_INC

PHP code

<?php
/*
 * increments variable indicated by value1 by 1 (after performing other operations) and stores in result
 * opcode number: 36
 */
$a++;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | POST_INC |  |  | ~0 | !0
 | 1 | FREE |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


POST_INC_OBJ

PHP code

<?php
/*
 * Increment the property value specified by prop-name and store the original value into result.
 * opcode number: 134
 */
$obj = new A();
$obj->num++;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | ZEND_POST_INC_OBJ |  |  | ~5 | !0,'num'
 | 5 | FREE |  |  |  | ~5
8 | 6 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


PRE_DEC

PHP code

<?php
/*
 * decrements variable indicated by value1 by 1 (before performing other operations) and stores in results
 * opcode number: 35
 */
--$a;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | PRE_DEC |  |  |  | !0
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


PRE_DEC_OBJ

PHP code

<?php
/*
 *
 * opcode number: 133
 */
$obj = new A();
--$obj->num;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | ZEND_PRE_DEC_OBJ |  |  |  | !0,'num'
8 | 5 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


PRE_INC

PHP code

<?php
/*
 * increments variable indicated by value1 by 1 (before performing other operations) and stores in result
 * opcode number: 34
 */
++$a;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | PRE_INC |  |  |  | !0
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


PRE_INC_OBJ

PHP code

<?php
/*
 *
 * opcode number: 132
 */
$obj = new A();
++$obj->num;
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | ZEND_PRE_INC_OBJ |  |  |  | !0,'num'
8 | 5 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


PRINT

PHP code

<?php
/*
 *
 * opcode number: 41
 */
print "Hello World";
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | PRINT |  |  | ~0 | 'Hello+World'
 | 1 | FREE |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


QM_ASSIGN

PHP code

<?php
/*
 * Question Mark Assign, used twice inside a question mark assign to temporarily assign result as value1  (this is followed up with an ASSIGN bytecode)
 * opcode number: 22
 */
function A(){
 echo 1?2:3;
}

function B(){
 $b = 0;
 $a = $b > 1 ? 10: 11;
}
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
10 | 1 | NOP |  |  |  |
14 | 2 | RETURN |  |  |  | 1

Function name: A

Compiled variables: none

line | # | op | fetch | ext | return | operands
7 | 0 | JMPZ |  |  |  | 1,->3
 | 1 | QM_ASSIGN |  |  | ~0 | 2
 | 2 | JMP |  |  |  | ->4
 | 3 | QM_ASSIGN |  |  | ~0 | 3
 | 4 | ECHO |  |  |  | ~0
8 | 5 | RETURN |  |  |  | null

Function name: B

Compiled variables: !0=$b, !1=$a

line | # | op | fetch | ext | return | operands
11 | 0 | ASSIGN |  |  |  | !0,0
12 | 1 | IS_SMALLER |  |  | ~1 | 1,!0
 | 2 | JMPZ |  |  |  | ~1,->5
 | 3 | QM_ASSIGN |  |  | ~2 | 10
 | 4 | JMP |  |  |  | ->6
 | 5 | QM_ASSIGN |  |  | ~2 | 11
 | 6 | ASSIGN |  |  |  | !1,~2
13 | 7 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


RAISE_ABSTRACT_ERROR

PHP code

<?php
/*
 *
 * opcode number: 142
 */

abstract class fail {
    abstract function show();
}

class pass extends fail {
    function show() {
        echo "Call to function show()\n";
    }
}

$t2 = new pass();
$t2->show();

$t = new fail();
$t->show();

echo "Done\n"; // shouldn't be displayed
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$t2, !1=$t

line | # | op | fetch | ext | return | operands
7 | 0 | NOP |  |  |  |
11 | 1 | NOP |  |  |  |
 | 2 | NOP |  |  |  |
17 | 3 | ZEND_FETCH_CLASS |  |  | :3 | 'pass'
 | 4 | NEW |  |  | $4 | :3
 | 5 | DO_FCALL_BY_NAME |  | 0 |  |
 | 6 | ASSIGN |  |  |  | !0,$4
18 | 7 | ZEND_INIT_METHOD_CALL |  |  |  | !0,'show'
 | 8 | DO_FCALL_BY_NAME |  | 0 |  |
20 | 9 | ZEND_FETCH_CLASS |  |  | :9 | 'fail'
 | 10 | NEW |  |  | $10 | :9
 | 11 | DO_FCALL_BY_NAME |  | 0 |  |
 | 12 | ASSIGN |  |  |  | !1,$10
21 | 13 | ZEND_INIT_METHOD_CALL |  |  |  | !1,'show'
 | 14 | DO_FCALL_BY_NAME |  | 0 |  |
23 | 15 | ECHO |  |  |  | 'Done%0A'
24 | 16 | RETURN |  |  |  | 1

Function name: show

Compiled variables: none

line | # | op | fetch | ext | return | operands
8 | 0 | ZEND_RAISE_ABSTRACT_ERROR |  |  |  |
 | 1 | RETURN |  |  |  | null

Function name: show

Compiled variables: none

line | # | op | fetch | ext | return | operands
13 | 0 | ECHO |  |  |  | 'Call+to+function+show%28%29%0A'
14 | 1 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


RECV

PHP code

<?php
/*
 * Receive a functoin argument of "argument-number" and store the argument value into a variable of "argument-number".  (e.g. $1 for 1)
 * opcode number: 63
 */
function hello($a){}
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
7 | 1 | RETURN |  |  |  | 1

Function name: hello

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | RECV |  |  |  | 1
 | 1 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


RECV_INIT

PHP code

<?php
/*
 * Initialize a function argument with "value" if not received from caller.  Otherwise same as RECV.
 * opcode number: 64
 */
function hello($a=5){}
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
7 | 1 | RETURN |  |  |  | 1

Function name: hello

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | RECV_INIT |  |  |  | 1,5
 | 1 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


RETURN

PHP code

<?php
/*
 * Return value from a funciton.
 * opcode number: 62
 */
return 1;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | RETURN |  |  |  | 1
7 | 1 | RETURN |  |  |  | 1

User Contributed Notes

fengdingbo at gmail dot com 23-Jul-2013 02:40
qiufeng@ubuntu:~$ php -dvld.active=1 -r 'return 1;'
Finding entry points
Branch analysis from position: 0
Return found
filename:       Command line code
function name:  (null)
number of ops:  2
compiled vars:  none
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   1     0  > > RETURN                                                   1
         1*   > RETURN                                                   null

branch: #  0; line:     1-    1; sop:     0; eop:     1
path #1: 0,


RETURN_BY_REF

PHP code

<?php
/*
 *
 * opcode number: 111
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


SEND_REF

PHP code

<?php
/*
 * Pass the reference value as an actual parameter to a function.  DO_FCALL follows.
 * opcode number: 67
 */
@each($input);
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | BEGIN_SILENCE |  |  | ~0 |
 | 1 | FETCH_W | local |  | $1 | 'input'
 | 2 | SEND_REF |  |  |  | $1
 | 3 | DO_FCALL |  | 1 |  | 'each'
 | 4 | END_SILENCE |  |  |  | ~0
7 | 5 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


SEND_VAL

PHP code

<?php
/*
 * Pass the constant value as an actual parameter to a function.  DO_FCALL follows.
 * opcode number: 65
 */
function funcA($msg){
    print $msg;
}

funcA('HELLO');

defined('IN_PHPBB');
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | NOP |  |  |  |
10 | 1 | SEND_VAL |  |  |  | 'HELLO'
 | 2 | DO_FCALL |  | 1 |  | 'funca'
12 | 3 | SEND_VAL |  |  |  | 'IN_PHPBB'
 | 4 | DO_FCALL |  | 1 |  | 'defined'
13 | 5 | RETURN |  |  |  | 1

Function name: funcA

Compiled variables: !0=$msg

line | # | op | fetch | ext | return | operands
6 | 0 | RECV |  |  |  | 1
7 | 1 | PRINT |  |  | ~0 | !0
 | 2 | FREE |  |  |  | ~0
8 | 3 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


SEND_VAR

PHP code

<?php
/*
 * Pass the variable value as an actual parameter to a function.  DO_FCALL follows.
 * opcode number: 66
 */
$a = array(1,2,3);
if(is_array($a)){ return 0; }
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | INIT_ARRAY |  |  | ~0 | 1
 | 1 | ADD_ARRAY_ELEMENT |  |  | ~0 | 2
 | 2 | ADD_ARRAY_ELEMENT |  |  | ~0 | 3
 | 3 | ASSIGN |  |  |  | !0,~0
7 | 4 | SEND_VAR |  |  |  | !0
 | 5 | DO_FCALL |  | 1 |  | 'is_array'
 | 6 | JMPZ |  |  |  | $2,->9
 | 7 | RETURN |  |  |  | 0
 | 8 | JMP |  |  |  | ->9
8 | 9 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


SEND_VAR_NO_REF

PHP code

<?php
/*
 *
 * opcode number: 106
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


SL

PHP code

<?php
/*
 * Shift bits of value1 to the left value2 steps (each step means "multiply by two")
 * opcode number: 6
 */
echo 8 << 2;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | SL |  |  | ~0 | 8,2
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


SR

PHP code

<?php
/*
 * Shift bits of value1 to the right value2 steps (each step means "divide by two")
 * opcode number: 7
 */
echo 8 >> 2;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | SR |  |  | ~0 | 8,2
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


SUB

PHP code

<?php
/*
 * Subtracts "value2" from "value1" and stores the result into "result".
 * opcode number: 2
 */
echo 1-2;
?>

PHP opcodes

Function name: (null)

Compiled variables: none

line | # | op | fetch | ext | return | operands
6 | 0 | SUB |  |  | ~0 | 1,2
 | 1 | ECHO |  |  |  | ~0
7 | 2 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


SWITCH_FREE

PHP code

<?php
/*
 * Release the allocated space of "value"?
 * opcode number: 49
 */
$a = array(1,2,3);
foreach($a as $num){
    print $num;
}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a, !1=$num

line | # | op | fetch | ext | return | operands
6 | 0 | INIT_ARRAY |  |  | ~0 | 1
 | 1 | ADD_ARRAY_ELEMENT |  |  | ~0 | 2
 | 2 | ADD_ARRAY_ELEMENT |  |  | ~0 | 3
 | 3 | ASSIGN |  |  |  | !0,~0
7 | 4 | FE_RESET |  |  | $2 | !0,->11
 | 5 | FE_FETCH |  |  | $3 | $2,->11
 | 6 | ZEND_OP_DATA |  |  |  |
 | 7 | ASSIGN |  |  |  | !1,$3
8 | 8 | PRINT |  |  | ~5 | !1
 | 9 | FREE |  |  |  | ~5
9 | 10 | JMP |  |  |  | ->5
 | 11 | SWITCH_FREE |  |  |  | $2
10 | 12 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


THROW

PHP code

<?php
/*
 *
 * opcode number: 108
 */
try {
    $error = 'Always throw this error';
    throw new Exception($error);

    // Code following an exception is not executed.
    echo 'Never executed';

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$error, !1=$e

line | # | op | fetch | ext | return | operands
7 | 0 | ASSIGN |  |  |  | !0,'Always+throw+this+error'
8 | 1 | ZEND_FETCH_CLASS |  |  | :1 | 'Exception'
 | 2 | NEW |  |  | $2 | :1
 | 3 | SEND_VAR |  |  |  | !0
 | 4 | DO_FCALL_BY_NAME |  | 1 |  |
 | 5 | ZEND_THROW |  | 0 |  | $2
11 | 6 | ECHO |  |  |  | 'Never+executed'
13 | 7 | JMP |  |  |  | ->15
 | 8 | ZEND_FETCH_CLASS |  |  | :4 | 'Exception'
 | 9 | ZEND_CATCH |  | 15 |  | $4,!1
14 | 10 | ECHO |  |  |  | 'Caught+exception%3A+'
 | 11 | ZEND_INIT_METHOD_CALL |  |  |  | !1,'getMessage'
 | 12 | DO_FCALL_BY_NAME |  | 0 |  |
 | 13 | ECHO |  |  |  | $6
 | 14 | ECHO |  |  |  | '%0A'
18 | 15 | ECHO |  |  |  | 'Hello+World'
19 | 16 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


TICKS

PHP code

<?php
/*
 *
 * opcode number: 105
 */
// A function that records the time when it is called
function profile()
{
   echo "profile function is called\n";
}

// Set up a tick handler
register_tick_function("profile");

// Initialize the function before the declare block
profile();

// Run a block of code, throw a tick every 2nd statement
declare(ticks=2) {
   for ($x = 0; $x < 10; ++$x) {
         echo "hello world\n";
   }
}
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x

line | # | op | fetch | ext | return | operands
7 | 0 | NOP |  |  |  |
13 | 1 | SEND_VAL |  |  |  | 'profile'
 | 2 | DO_FCALL |  | 1 |  | 'register_tick_function'
16 | 3 | DO_FCALL |  | 0 |  | 'profile'
20 | 4 | ASSIGN |  |  |  | !0,0
 | 5 | IS_SMALLER |  |  | ~3 | !0,10
 | 6 | JMPZNZ |  | 9 |  | ~3,->13
 | 7 | PRE_INC |  |  |  | !0
 | 8 | JMP |  |  |  | ->5
21 | 9 | ECHO |  |  |  | 'hello+world%0A'
 | 10 | TICKS |  |  |  | 2
22 | 11 | TICKS |  |  |  | 2
 | 12 | JMP |  |  |  | ->7
 | 13 | TICKS |  |  |  | 2
23 | 14 | TICKS |  |  |  | 2
24 | 15 | RETURN |  |  |  | 1

Function name: profile

Compiled variables: none

line | # | op | fetch | ext | return | operands
9 | 0 | ECHO |  |  |  | 'profile+function+is+called%0A'
10 | 1 | RETURN |  |  |  | null

User Contributed Notes

There are no user contributed notes for this page.


UNSET_DIM

PHP code

<?php
/*
 * Unset the entry of array-value, which is specified by index, and store the original element value into "result".
 * opcode number: 75
 */
$a = array(1,2,3);
unset($a[0]);
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$a

line | # | op | fetch | ext | return | operands
6 | 0 | INIT_ARRAY |  |  | ~0 | 1
 | 1 | ADD_ARRAY_ELEMENT |  |  | ~0 | 2
 | 2 | ADD_ARRAY_ELEMENT |  |  | ~0 | 3
 | 3 | ASSIGN |  |  |  | !0,~0
7 | 4 | UNSET_DIM |  |  | $2 | !0,0
8 | 5 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


UNSET_OBJ

PHP code

<?php
/*
 * Unset the property of the current object (this) and store the original value into "result".
 * opcode number: 76
 */
$obj = new A();
unset($obj->num);
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$obj

line | # | op | fetch | ext | return | operands
6 | 0 | ZEND_FETCH_CLASS |  |  | :0 | 'A'
 | 1 | NEW |  |  | $1 | :0
 | 2 | DO_FCALL_BY_NAME |  | 0 |  |
 | 3 | ASSIGN |  |  |  | !0,$1
7 | 4 | UNSET_OBJ |  |  | $4 | !0,'num'
8 | 5 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


UNSET_VAR

PHP code

<?php
/*
 * Unset the variable and store the original value into "result".
 * opcode number: 74
 */
$x = 1;
$a='x';
unset($$a);
?>

PHP opcodes

Function name: (null)

Compiled variables: !0=$x, !1=$a

line | # | op | fetch | ext | return | operands
6 | 0 | ASSIGN |  |  |  | !0,1
7 | 1 | ASSIGN |  |  |  | !1,'x'
8 | 2 | UNSET_VAR |  |  | $2 | !1
9 | 3 | RETURN |  |  |  | 1

User Contributed Notes

There are no user contributed notes for this page.


USER_OPCODE

PHP code

<?php
/*
 *
 * opcode number: 150
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


VERIFY_ABSTRACT_CLASS

PHP code

<?php
/*
 *
 * opcode number: 146
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


ZEND_DECLARE_LAMBDA_FUNCTION

PHP code

<?php
/*
 *
 * opcode number: 153
 * No sample PHP yet */
?>

User Contributed Notes

There are no user contributed notes for this page.


ZEND_JMP_SET

PHP code

<?php
/*
 *
 * opcode number: 152
 * No sample PHP yet */
?>

User Contributed Notes

moriarty at front dot ru 18-Mar-2013 12:19
<?php
$t = 1?:5;
?>

line     # *  op
--------------------------------------
  73     0  >   ZEND_JMP_SET
         1      QM_ASSIGN
         2      ASSIGN


Zend Engine 1

Table of Contents

  • Old introduction
  • Streams API for PHP Extension Authors
  • Zend API: PHP 코어 해킹
  • TSRM API

Zend Engine 1 is the internal engine used by PHP for the entire version 4 release line. It is no longer considered active, but the old ZE1 documentation is preserved here exactly as it was.

User Contributed Notes

There are no user contributed notes for this page.


Old introduction

If you are about to begin developing PHP or Zend extensions, you need to prepare yourself for the programming environment provided by the various APIs. This part of the documentation tries to introduce the APIs provided by the different PHP and Zend Engine versions available. Since most of the information available here is somewhat outdated, you'll want to read various files found in the PHP source, files such as README.SELF-CONTAINED-EXTENSIONS and README.EXT_SKEL in addition to the manual.

User Contributed Notes

There are no user contributed notes for this page.


Streams API for PHP Extension Authors

Note:

The functions in this chapter are for use in the PHP source code and are not PHP functions. Information on userland stream functions can be found in the Stream Reference.

Overview

The PHP Streams API introduces a unified approach to the handling of files and sockets in PHP extension. Using a single API with standard functions for common operations, the streams API allows your extension to access files, sockets, URLs, memory and script-defined objects. Streams is a run-time extensible API that allows dynamically loaded modules (and scripts!) to register new streams.

The aim of the Streams API is to make it comfortable for developers to open files, URLs and other streamable data sources with a unified API that is easy to understand. The API is more or less based on the ANSI C stdio family of functions (with identical semantics for most of the main functions), so C programmers will have a feeling of familiarity with streams.

The streams API operates on a couple of different levels: at the base level, the API defines php_stream objects to represent streamable data sources. On a slightly higher level, the API defines php_stream_wrapper objects which "wrap" around the lower level API to provide support for retrieving data and meta-data from URLs. An additional context parameter, accepted by most stream creation functions, is passed to the wrapper's stream_opener method to fine-tune the behavior of the wrapper.

Any stream, once opened, can also have any number of filters applied to it, which process data as it is read from/written to the stream.

Streams can be cast (converted) into other types of file-handles, so that they can be used with third-party libraries without a great deal of trouble. This allows those libraries to access data directly from URL sources. If your system has the fopencookie() or funopen() function, you can even pass any PHP stream to any library that uses ANSI stdio!

Streams Basics

Using streams is very much like using ANSI stdio functions. The main difference is in how you obtain the stream handle to begin with. In most cases, you will use php_stream_open_wrapper() to obtain the stream handle. This function works very much like fopen, as can be seen from the example below:

Example #1 simple stream example that displays the PHP home page

php_stream * stream = php_stream_open_wrapper("http://www.php.net", "rb", REPORT_ERRORS, NULL);
if (stream) {
    while(!php_stream_eof(stream)) {
        char buf[1024];

        if (php_stream_gets(stream, buf, sizeof(buf))) {
            printf(buf);
        } else {
            break;
        }
    }
    php_stream_close(stream);
}

The table below shows the Streams equivalents of the more common ANSI stdio functions. Unless noted otherwise, the semantics of the functions are identical.

ANSI stdio equivalent functions in the Streams API
ANSI Stdio Function | PHP Streams Function | Notes
fopen | php_stream_open_wrapper | Streams includes additional parameters
fclose | php_stream_close |
fgets | php_stream_gets |
fread | php_stream_read | The nmemb parameter is assumed to have a value of 1, so the prototype looks more like read(2)
fwrite | php_stream_write | The nmemb parameter is assumed to have a value of 1, so the prototype looks more like write(2)
fseek | php_stream_seek |
ftell | php_stream_tell |
rewind | php_stream_rewind |
feof | php_stream_eof |
fgetc | php_stream_getc |
fputc | php_stream_putc |
fflush | php_stream_flush |
puts | php_stream_puts | Same semantics as puts, NOT fputs
fstat | php_stream_stat | Streams has a richer stat structure

Streams as Resources

All streams are registered as resources when they are created. This ensures that they will be properly cleaned up even if there is some fatal error. All of the filesystem functions in PHP operate on streams resources - that means that your extensions can accept regular PHP file pointers as parameters to, and return streams from their functions. The streams API makes this process as painless as possible:

Example #2 How to accept a stream as a parameter

PHP_FUNCTION(example_write_hello)
{
    zval *zstream;
    php_stream *stream;

    if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream))
        return;

    php_stream_from_zval(stream, &zstream);

    /* you can now use the stream.  However, you do not "own" the
        stream, the script does.  That means you MUST NOT close the
        stream, because it will cause PHP to crash! */

    php_stream_write(stream, "hello\n");

    RETURN_TRUE();
}

Example #3 How to return a stream from a function

PHP_FUNCTION(example_open_php_home_page)
{
    php_stream *stream;

    stream = php_stream_open_wrapper("http://www.php.net", "rb", REPORT_ERRORS, NULL);

    php_stream_to_zval(stream, return_value);

    /* after this point, the stream is "owned" by the script.
        If you close it now, you will crash PHP! */
}

Since streams are automatically cleaned up, it's tempting to think that we can get away with being sloppy programmers and not bother to close the streams when we are done with them. Although such an approach might work, it is not a good idea for a number of reasons: streams hold locks on system resources while they are open, so leaving a file open after you have finished with it could prevent other processes from accessing it. If a script deals with a large number of files, the accumulation of the resources used, both in terms of memory and the sheer number of open files, can cause web server requests to fail. Sounds bad, doesn't it? The streams API includes some magic that helps you to keep your code clean - if a stream is not closed by your code when it should be, you will find some helpful debugging information in you web server error log.

Note: Always use a debug build of PHP when developing an extension (--enable-debug when running configure), as a lot of effort has been made to warn you about memory and stream leaks.

In some cases, it is useful to keep a stream open for the duration of a request, to act as a log or trace file for example. Writing the code to safely clean up such a stream is not difficult, but it's several lines of code that are not strictly needed. To save yourself the trouble of writing the code, you can mark a stream as being OK for auto cleanup. What this means is that the streams API will not emit a warning when it is time to auto-cleanup a stream. To do this, you can use php_stream_auto_cleanup().

Streams open options

These constants affect the operation of stream factory functions.

IGNORE_PATH
This is the default option for streams; it requests that the include_path is not to be searched for the requested file.
USE_PATH
Requests that the include_path is to be searched for the requested file.
IGNORE_URL
Requests that registered URL wrappers are to be ignored when opening the stream. Other non-URL wrappers will be taken into consideration when decoding the path. There is no opposite form for this flag; the streams API will use all registered wrappers by default.
IGNORE_URL_WIN
On Windows systems, this is equivalent to IGNORE_URL. On all other systems, this flag has no effect.
ENFORCE_SAFE_MODE
Requests that the underlying stream implementation perform safe_mode checks on the file before opening the file. Omitting this flag will skip safe_mode checks and allow opening of any file that the PHP process has rights to access.
REPORT_ERRORS
If this flag is set, and there was an error during the opening of the file or URL, the streams API will call the php_error function for you. This is useful because the path may contain username/password information that should not be displayed in the browser output (it would be a security risk to do so). When the streams API raises the error, it first strips username/password information from the path, making the error message safe to display in the browser.
STREAM_MUST_SEEK
This flag is useful when your extension really must be able to randomly seek around in a stream. Some streams may not be seekable in their native form, so this flag asks the streams API to check to see if the stream does support seeking. If it does not, it will copy the stream into temporary storage (which may be a temporary file or a memory stream) which does support seeking. Please note that this flag is not useful when you want to seek the stream and write to it, because the stream you are accessing might not be bound to the actual resource you requested.

Note: If the requested resource is network based, this flag will cause the opener to block until the whole contents have been downloaded.

STREAM_WILL_CAST
If your extension is using a third-party library that expects a FILE* or file descriptor, you can use this flag to request the streams API to open the resource but avoid buffering. You can then use php_stream_cast() to retrieve the FILE* or file descriptor that the library requires. The is particularly useful when accessing HTTP URLs where the start of the actual stream data is found after an indeterminate offset into the stream. Since this option disables buffering at the streams API level, you may experience lower performance when using streams functions on the stream; this is deemed acceptable because you have told streams that you will be using the functions to match the underlying stream implementation. Only use this option when you are sure you need it.

User Contributed Notes

armand at nl dot envida dot net 11-Sep-2003 11:05
To have your extension ignore the open_basedir restrictions that may be set, use
  STREAM_DISABLE_OPENBASEDIR

I'm not sure why this option is left out of the documentation,
there are valid and legit uses for this option.


Zend API: PHP 코어 해킹

Introduction

Those who know don't talk.

Those who talk don't know.

Sometimes, PHP "as is" simply isn't enough. Although these cases are rare for the average user, professional applications will soon lead PHP to the edge of its capabilities, in terms of either speed or functionality. New functionality cannot always be implemented natively due to language restrictions and inconveniences that arise when having to carry around a huge library of default code appended to every single script, so another method needs to be found for overcoming these eventual lacks in PHP.

As soon as this point is reached, it's time to touch the heart of PHP and take a look at its core, the C code that makes PHP go.

Warning

This information is currently rather outdated, parts of it only cover early stages of the ZendEngine 1.0 API as it was used in early versions of PHP 4.

More recent information may be found in the various README files that come with the PHP source and the » Internals section on the Zend website.

Overview

"Extending PHP" is easier said than done. PHP has evolved to a full-fledged tool consisting of a few megabytes of source code, and to hack a system like this quite a few things have to be learned and considered. When structuring this chapter, we finally decided on the "learn by doing" approach. This is not the most scientific and professional approach, but the method that's the most fun and gives the best end results. In the following sections, you'll learn quickly how to get the most basic extensions to work almost instantly. After that, you'll learn about Zend's advanced API functionality. The alternative would have been to try to impart the functionality, design, tips, tricks, etc. as a whole, all at once, thus giving a complete look at the big picture before doing anything practical. Although this is the "better" method, as no dirty hacks have to be made, it can be very frustrating as well as energy- and time-consuming, which is why we've decided on the direct approach.

Note that even though this chapter tries to impart as much knowledge as possible about the inner workings of PHP, it's impossible to really give a complete guide to extending PHP that works 100% of the time in all cases. PHP is such a huge and complex package that its inner workings can only be understood if you make yourself familiar with it by practicing, so we encourage you to work with the source.

What Is Zend? and What Is PHP?

The name Zend refers to the language engine, PHP's core. The term PHP refers to the complete system as it appears from the outside. This might sound a bit confusing at first, but it's not that complicated ( see below). To implement a Web script interpreter, you need three parts:

  1. The interpreter part analyzes the input code, translates it, and executes it.

  2. The functionality part implements the functionality of the language (its functions, etc.).

  3. The interface part talks to the Web server, etc.

Zend takes part 1 completely and a bit of part 2; PHP takes parts 2 and 3. Together they form the complete PHP package. Zend itself really forms only the language core, implementing PHP at its very basics with some predefined functions. PHP contains all the modules that actually create the language's outstanding capabilities.
The internal structure of PHP.

The following sections discuss where PHP can be extended and how it's done.

Extension Possibilities

As shown above, PHP can be extended primarily at three points: external modules, built-in modules, and the Zend engine. The following sections discuss these options.

External Modules

External modules can be loaded at script runtime using the function dl(). This function loads a shared object from disk and makes its functionality available to the script to which it's being bound. After the script is terminated, the external module is discarded from memory. This method has both advantages and disadvantages, as described in the following table:

Advantages | Disadvantages
External modules don't require recompiling of PHP. | The shared objects need to be loaded every time a script is being executed (every hit), which is very slow.
The size of PHP remains small by "outsourcing" certain functionality. | External additional files clutter up the disk.
 |  | Every script that wants to use an external module's functionality has to specifically include a call to dl(), or the extension tag in php.ini needs to be modified (which is not always a suitable solution).
To sum up, external modules are great for third-party products, small additions to PHP that are rarely used, or just for testing purposes. To develop additional functionality quickly, external modules provide the best results. For frequent usage, larger implementations, and complex code, the disadvantages outweigh the advantages.

Third parties might consider using the extension tag in php.ini to create additional external modules to PHP. These external modules are completely detached from the main package, which is a very handy feature in commercial environments. Commercial distributors can simply ship disks or archives containing only their additional modules, without the need to create fixed and solid PHP binaries that don't allow other modules to be bound to them.

Built-in Modules

Built-in modules are compiled directly into PHP and carried around with every PHP process; their functionality is instantly available to every script that's being run. Like external modules, built-in modules have advantages and disadvantages, as described in the following table:

Advantages | Disadvantages
No need to load the module specifically; the functionality is instantly available. | Changes to built-in modules require recompiling of PHP.
No external files clutter up the disk; everything resides in the PHP binary. | The PHP binary grows and consumes more memory.
Built-in modules are best when you have a solid library of functions that remains relatively unchanged, requires better than poor-to-average performance, or is used frequently by many scripts on your site. The need to recompile PHP is quickly compensated by the benefit in speed and ease of use. However, built-in modules are not ideal when rapid development of small additions is required.

The Zend Engine

Of course, extensions can also be implemented directly in the Zend engine. This strategy is good if you need a change in the language behavior or require special functions to be built directly into the language core. In general, however, modifications to the Zend engine should be avoided. Changes here result in incompatibilities with the rest of the world, and hardly anyone will ever adapt to specially patched Zend engines. Modifications can't be detached from the main PHP sources and are overridden with the next update using the "official" source repositories. Therefore, this method is generally considered bad practice and, due to its rarity, is not covered in this book.

Source Layout

Note:

Prior to working through the rest of this chapter, you should retrieve clean, unmodified source trees of your favorite Web server. We're working with Apache (available at » http://httpd.apache.org/) and, of course, with PHP (available at » http://www.php.net/ - does it need to be said?).

Make sure that you can compile a working PHP environment by yourself! We won't go into this issue here, however, as you should already have this most basic ability when studying this chapter.

Before we start discussing code issues, you should familiarize yourself with the source tree to be able to quickly navigate through PHP's files. This is a must-have ability to implement and debug extensions.

The following table describes the contents of the major directories.

Directory | Contents
php-src | Main PHP source files and main header files; here you'll find all of PHP's API definitions, macros, etc. (important). Everything else is below this directory.
php-src/ext | Repository for dynamic and built-in modules; by default, these are the "official" PHP modules that have been integrated into the main source tree. From PHP 4.0, it's possible to compile these standard extensions as dynamic loadable modules (at least, those that support it).
php-src/main | This directory contains the main php macros and definitions. (important)
php-src/pear | Directory for the PHP Extension and Application Repository. This directory contains core PEAR files.
php-src/sapi | Contains the code for the different server abstraction layers.
TSRM | Location of the "Thread Safe Resource Manager" (TSRM) for Zend and PHP.
ZendEngine2 | Location of the Zend Engine files; here you'll find all of Zend's API definitions, macros, etc. (important).

Discussing all the files included in the PHP package is beyond the scope of this chapter. However, you should take a close look at the following files:

  • php-src/main/php.h, located in the main PHP directory. This file contains most of PHP's macro and API definitions.

  • php-src/Zend/zend.h, located in the main Zend directory. This file contains most of Zend's macros and definitions.

  • php-src/Zend/zend_API.h, also located in the Zend directory, which defines Zend's API.

You should also follow some sub-inclusions from these files; for example, the ones relating to the Zend executor, the PHP initialization file support, and such. After reading these files, take the time to navigate around the package a little to see the interdependencies of all files and modules - how they relate to each other and especially how they make use of each other. This also helps you to adapt to the coding style in which PHP is authored. To extend PHP, you should quickly adapt to this style.

Extension Conventions

Zend is built using certain conventions; to avoid breaking its standards, you should follow the rules described in the following sections.

Macros

For almost every important task, Zend ships predefined macros that are extremely handy. The tables and figures in the following sections describe most of the basic functions, structures, and macros. The macro definitions can be found mainly in zend.h and zend_API.h. We suggest that you take a close look at these files after having studied this chapter. (Although you can go ahead and read them now, not everything will make sense to you yet.)

Memory Management

Resource management is a crucial issue, especially in server software. One of the most valuable resources is memory, and memory management should be handled with extreme care. Memory management has been partially abstracted in Zend, and you should stick to this abstraction for obvious reasons: Due to the abstraction, Zend gets full control over all memory allocations. Zend is able to determine whether a block is in use, automatically freeing unused blocks and blocks with lost references, and thus prevent memory leaks. The functions to be used are described in the following table:

Function | Description
emalloc() | Serves as replacement for malloc().
efree() | Serves as replacement for free().
estrdup() | Serves as replacement for strdup().
estrndup() | Serves as replacement for strndup(). Faster than estrdup() and binary-safe. This is the recommended function to use if you know the string length prior to duplicating it.
ecalloc() | Serves as replacement for calloc().
erealloc() | Serves as replacement for realloc().
emalloc(), estrdup(), estrndup(), ecalloc(), and erealloc() allocate internal memory; efree() frees these previously allocated blocks. Memory handled by the e*() functions is considered local to the current process and is discarded as soon as the script executed by this process is terminated.
Warning

To allocate resident memory that survives termination of the current script, you can use malloc() and free(). This should only be done with extreme care, however, and only in conjunction with demands of the Zend API; otherwise, you risk memory leaks.

Zend also features a thread-safe resource manager to provide better native support for multithreaded Web servers. This requires you to allocate local structures for all of your global variables to allow concurrent threads to be run. Because the thread-safe mode of Zend was not finished back when this was written, it is not yet extensively covered here.

Directory and File Functions

The following directory and file functions should be used in Zend modules. They behave exactly like their C counterparts, but provide virtual working directory support on the thread level.

Zend Function | Regular C Function
V_GETCWD() | getcwd()
V_FOPEN() | fopen()
V_OPEN() | open()
V_CHDIR() | chdir()
V_GETWD() | getwd()
V_CHDIR_FILE() | Takes a file path as an argument and changes the current working directory to that file's directory.
V_STAT() | stat()
V_LSTAT() | lstat()

String Handling

Strings are handled a bit differently by the Zend engine than other values such as integers, Booleans, etc., which don't require additional memory allocation for storing their values. If you want to return a string from a function, introduce a new string variable to the symbol table, or do something similar, you have to make sure that the memory the string will be occupying has previously been allocated, using the aforementioned e*() functions for allocation. (This might not make much sense to you yet; just keep it somewhere in your head for now - we'll get back to it shortly.)

Complex Types

Complex types such as arrays and objects require different treatment. Zend features a single API for these types - they're stored using hash tables.

Note:

To reduce complexity in the following source examples, we're only working with simple types such as integers at first. A discussion about creating more advanced types follows later in this chapter.

PHP's Automatic Build System

PHP 4 features an automatic build system that's very flexible. All modules reside in a subdirectory of the ext directory. In addition to its own sources, each module consists of a config.m4 file, for extension configuration. (for example, see » http://www.gnu.org/software/m4/)

All these stub files are generated automatically, along with .cvsignore, by a little shell script named ext_skel that resides in the ext directory. As argument it takes the name of the module that you want to create. The shell script then creates a directory of the same name, along with the appropriate stub files.

Step by step, the process looks like this:

:~/cvs/php4/ext:> ./ext_skel --extname=my_module
Creating directory my_module
Creating basic files: config.m4 .cvsignore my_module.c php_my_module.h CREDITS EXPERIMENTAL tests/001.phpt my_module.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/my_module/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-my_module
5.  $ make
6.  $ ./php -f ext/my_module/my_module.php
7.  $ vi ext/my_module/my_module.c
8.  $ make

Repeat steps 3-6 until you are satisfied with ext/my_module/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.
This instruction creates the aforementioned files. To include the new module in the automatic configuration and build process, you have to run buildconf, which regenerates the configure script by searching through the ext directory and including all found config.m4 files.

The default config.m4 shown in The default config.m4. is a bit more complex:

Example #1 The default config.m4.

dnl $Id: build.xml 297078 2010-03-29 16:25:51Z degeberg $
dnl config.m4 for extension my_module

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:

dnl PHP_ARG_WITH(my_module, for my_module support,
dnl Make sure that the comment is aligned:
dnl [  --with-my_module             Include my_module support])

dnl Otherwise use enable:

dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,
dnl Make sure that the comment is aligned:
dnl [  --enable-my_module           Enable my_module support])

if test "$PHP_MY_MODULE" != "no"; then
  dnl Write more examples of tests here...

  dnl # --with-my_module -> check with-path
  dnl SEARCH_PATH="/usr/local /usr"     # you might want to change this
  dnl SEARCH_FOR="/include/my_module.h"  # you most likely want to change this
  dnl if test -r $PHP_MY_MODULE/; then # path given as parameter
  dnl   MY_MODULE_DIR=$PHP_MY_MODULE
  dnl else # search default path list
  dnl   AC_MSG_CHECKING([for my_module files in default path])
  dnl   for i in $SEARCH_PATH ; do
  dnl     if test -r $i/$SEARCH_FOR; then
  dnl       MY_MODULE_DIR=$i
  dnl       AC_MSG_RESULT(found in $i)
  dnl     fi
  dnl   done
  dnl fi
  dnl
  dnl if test -z "$MY_MODULE_DIR"; then
  dnl   AC_MSG_RESULT([not found])
  dnl   AC_MSG_ERROR([Please reinstall the my_module distribution])
  dnl fi

  dnl # --with-my_module -> add include path
  dnl PHP_ADD_INCLUDE($MY_MODULE_DIR/include)

  dnl # --with-my_module -> chech for lib and symbol presence
  dnl LIBNAME=my_module # you may want to change this
  dnl LIBSYMBOL=my_module # you most likely want to change this

  dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
  dnl [
  dnl   PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $MY_MODULE_DIR/lib, MY_MODULE_SHARED_LIBADD)
  dnl   AC_DEFINE(HAVE_MY_MODULELIB,1,[ ])
  dnl ],[
  dnl   AC_MSG_ERROR([wrong my_module lib version or lib not found])
  dnl ],[
  dnl   -L$MY_MODULE_DIR/lib -lm -ldl
  dnl ])
  dnl
  dnl PHP_SUBST(MY_MODULE_SHARED_LIBADD)

  PHP_NEW_EXTENSION(my_module, my_module.c, $ext_shared)
fi

If you're unfamiliar with M4 files (now is certainly a good time to get familiar), this might be a bit confusing at first; but it's actually quite easy.

Note: Everything prefixed with dnl is treated as a comment and is not parsed.

The config.m4 file is responsible for parsing the command-line options passed to configure at configuration time. This means that it has to check for required external files and do similar configuration and setup tasks.

The default file creates two configuration directives in the configure script: --with-my_module and --enable-my_module. Use the first option when referring external files (such as the --with-apache directive that refers to the Apache directory). Use the second option when the user simply has to decide whether to enable your extension. Regardless of which option you use, you should uncomment the other, unnecessary one; that is, if you're using --enable-my_module, you should remove support for --with-my_module, and vice versa.

By default, the config.m4 file created by ext_skel accepts both directives and automatically enables your extension. Enabling the extension is done by using the PHP_EXTENSION macro. To change the default behavior to include your module into the PHP binary when desired by the user (by explicitly specifying --enable-my_module or --with-my_module), change the test for $PHP_MY_MODULE to == "yes":

if test "$PHP_MY_MODULE" == "yes"; then dnl
    Action.. PHP_EXTENSION(my_module, $ext_shared)
    fi
This would require you to use --enable-my_module each time when reconfiguring and recompiling PHP.

Note: Be sure to run buildconf every time you change config.m4!

We'll go into more details on the M4 macros available to your configuration scripts later in this chapter. For now, we'll simply use the default files.

Creating Extensions

We'll start with the creation of a very simple extension at first, which basically does nothing more than implement a function that returns the integer it receives as parameter. A simple extension. shows the source.

Example #2 A simple extension.

/* include standard header */
#include "php.h"

/* declaration of functions to be exported */
ZEND_FUNCTION(first_module);

/* compiled function list so Zend knows what's in this module */
zend_function_entry firstmod_functions[] =
{
    ZEND_FE(first_module, NULL)
    ZEND_FE_END
};

/* compiled module information */
zend_module_entry firstmod_module_entry =
{
    STANDARD_MODULE_HEADER,
    "First Module",
    firstmod_functions,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NO_VERSION_YET,
    STANDARD_MODULE_PROPERTIES
};

/* implement standard "stub" routine to introduce ourselves to Zend */
#if COMPILE_DL_FIRST_MODULE
ZEND_GET_MODULE(firstmod)
#endif

/* implement function that is meant to be made available to PHP */
ZEND_FUNCTION(first_module)
{
    long parameter;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &parameter) == FAILURE) {
        return;
    }

    RETURN_LONG(parameter);
}

This code contains a complete PHP module. We'll explain the source code in detail shortly, but first we'd like to discuss the build process. (This will allow the impatient to experiment before we dive into API discussions.)

Note:

The example source makes use of some features introduced with the Zend version used in PHP 4.1.0 and above, it won't compile with older PHP 4.0.x versions.

Compiling Modules

There are basically two ways to compile modules:

  • Use the provided "make" mechanism in the ext directory, which also allows building of dynamic loadable modules.

  • Compile the sources manually.

The first method should definitely be favored, since, as of PHP 4.0, this has been standardized into a sophisticated build process. The fact that it is so sophisticated is also its drawback, unfortunately - it's hard to understand at first. We'll provide a more detailed introduction to this later in the chapter, but first let's work with the default files.

The second method is good for those who (for some reason) don't have the full PHP source tree available, don't have access to all files, or just like to juggle with their keyboard. These cases should be extremely rare, but for the sake of completeness we'll also describe this method.

Compiling Using Make

To compile the sample sources using the standard mechanism, copy all their subdirectories to the ext directory of your PHP source tree. Then run buildconf, which will create an updated configure script containing appropriate options for the new extension. By default, all the sample sources are disabled, so you don't have to fear breaking your build process.

After you run buildconf, configure --help shows the following additional modules:

  --enable-array_experiments   BOOK: Enables array experiments
  --enable-call_userland       BOOK: Enables userland module
  --enable-cross_conversion    BOOK: Enables cross-conversion module
  --enable-first_module        BOOK: Enables first module
  --enable-infoprint           BOOK: Enables infoprint module
  --enable-reference_test      BOOK: Enables reference test module
  --enable-resource_test       BOOK: Enables resource test module
  --enable-variable_creation   BOOK: Enables variable-creation module

The module shown earlier in A simple extension. can be enabled with --enable-first_module or --enable-first_module=yes.

Compiling Manually

To compile your modules manually, you need the following commands:

Action | Command
Compiling | cc -fpic -DCOMPILE_DL_FIRST_MODULE=1 -I/usr/local/include -I. -I.. -I../Zend -c -o <your_object_file> <your_c_file>
Linking | cc -shared -L/usr/local/lib -rdynamic -o <your_module_file> <your_object_file(s)>
The command to compile the module simply instructs the compiler to generate position-independent code (-fpic shouldn't be omitted) and additionally defines the constant COMPILE_DL_FIRST_MODULE to tell the module code that it's compiled as a dynamically loadable module (the test module above checks for this; we'll discuss it shortly). After these options, it specifies a number of standard include paths that should be used as the minimal set to compile the source files.

Note: All include paths in the example are relative to the directory ext. If you're compiling from another directory, change the pathnames accordingly. Required items are the PHP directory, the Zend directory, and (if necessary), the directory in which your module resides.

The link command is also a plain vanilla command instructing linkage as a dynamic module.

You can include optimization options in the compilation command, although these have been omitted in this example (but some are included in the makefile template described in an earlier section).

Note: Compiling and linking manually as a static module into the PHP binary involves very long instructions and thus is not discussed here. (It's not very efficient to type all those commands.)

Using Extensions

Depending on the build process you selected, you should either end up with a new PHP binary to be linked into your Web server (or run as CGI), or with an .so (shared object) file. If you compiled the example file first_module.c as a shared object, your result file should be first_module.so. To use it, you first have to copy it to a place from which it's accessible to PHP. For a simple test procedure, you can copy it to your htdocs directory and try it with the source in A test file for first_module.so.. If you compiled it into the PHP binary, omit the call to dl(), as the module's functionality is instantly available to your scripts.

Warning

For security reasons, you should not put your dynamic modules into publicly accessible directories. Even though it can be done and it simplifies testing, you should put them into a separate directory in production environments.

Example #3 A test file for first_module.so.

<?php

// remove next comment if necessary
// dl("first_module.so");

$param = 2;
$return = first_module($param);

print("We sent '$param' and got '$return'");

?>

Calling this PHP file should output the following:

We sent '2' and got '2'

If required, the dynamic loadable module is loaded by calling the dl() function. This function looks for the specified shared object, loads it, and makes its functions available to PHP. The module exports the function first_module(), which accepts a single parameter, converts it to an integer, and returns the result of the conversion.

If you've gotten this far, congratulations! You just built your first extension to PHP.

Troubleshooting

Actually, not much troubleshooting can be done when compiling static or dynamic modules. The only problem that could arise is that the compiler will complain about missing definitions or something similar. In this case, make sure that all header files are available and that you specified their path correctly in the compilation command. To be sure that everything is located correctly, extract a clean PHP source tree and use the automatic build in the ext directory with the fresh files; this will guarantee a safe compilation environment. If this fails, try manual compilation.

PHP might also complain about missing functions in your module. (This shouldn't happen with the sample sources if you didn't modify them.) If the names of external functions you're trying to access from your module are misspelled, they'll remain as "unlinked symbols" in the symbol table. During dynamic loading and linkage by PHP, they won't resolve because of the typing errors - there are no corresponding symbols in the main binary. Look for incorrect declarations in your module file or incorrectly written external references. Note that this problem is specific to dynamic loadable modules; it doesn't occur with static modules. Errors in static modules show up at compile time.

Source Discussion

Now that you've got a safe build environment and you're able to include the modules into PHP files, it's time to discuss how everything works.

Module Structure

All PHP modules follow a common structure:

  • Header file inclusions (to include all required macros, API definitions, etc.)

  • C declaration of exported functions (required to declare the Zend function block)

  • Declaration of the Zend function block

  • Declaration of the Zend module block

  • Implementation of get_module()

  • Implementation of all exported functions

Header File Inclusions

The only header file you really have to include for your modules is php.h, located in the PHP directory. This file makes all macros and API definitions required to build new modules available to your code.

Tip: It's good practice to create a separate header file for your module that contains module-specific definitions. This header file should contain all the forward definitions for exported functions and include php.h. If you created your module using ext_skel you already have such a header file prepared.

Declaring Exported Functions

To declare functions that are to be exported (i.e., made available to PHP as new native functions), Zend provides a set of macros. A sample declaration looks like this:

ZEND_FUNCTION ( my_function );

ZEND_FUNCTION declares a new C function that complies with Zend's internal API. This means that the function is of type void and accepts INTERNAL_FUNCTION_PARAMETERS (another macro) as parameters. Additionally, it prefixes the function name with zif. The immediately expanded version of the above definitions would look like this:

void zif_my_function ( INTERNAL_FUNCTION_PARAMETERS );
Expanding INTERNAL_FUNCTION_PARAMETERS results in the following:
void zif_my_function( int ht
                    , zval * return_value
                    , zval * this_ptr
                    , int return_value_used
                    , zend_executor_globals * executor_globals
                    );

Since the interpreter and executor core have been separated from the main PHP package, a second API defining macros and function sets has evolved: the Zend API. As the Zend API now handles quite a few of the responsibilities that previously belonged to PHP, a lot of PHP functions have been reduced to macros aliasing to calls into the Zend API. The recommended practice is to use the Zend API wherever possible, as the old API is only preserved for compatibility reasons. For example, the types zval and pval are identical. zval is Zend's definition; pval is PHP's definition (actually, pval is an alias for zval now). As the macro INTERNAL_FUNCTION_PARAMETERS is a Zend macro, the above declaration contains zval. When writing code, you should always use zval to conform to the new Zend API.

The parameter list of this declaration is very important; you should keep these parameters in mind (see Zend's Parameters to Functions Called from PHP for descriptions).

Zend's Parameters to Functions Called from PHP
Parameter | Description
ht | The number of arguments passed to the Zend function. You should not touch this directly, but instead use ZEND_NUM_ARGS() to obtain the value.
return_value | This variable is used to pass any return values of your function back to PHP. Access to this variable is best done using the predefined macros. For a description of these see below.
this_ptr | Using this variable, you can gain access to the object in which your function is contained, if it's used within an object. Use the function getThis() to obtain this pointer.
return_value_used | This flag indicates whether an eventual return value from this function will actually be used by the calling script. 0 indicates that the return value is not used; 1 indicates that the caller expects a return value. Evaluation of this flag can be done to verify correct usage of the function as well as speed optimizations in case returning a value requires expensive operations (for an example, see how array.c makes use of this).
executor_globals | This variable points to global settings of the Zend engine. You'll find this useful when creating new variables, for example (more about this later). The executor globals can also be introduced to your function by using the macro TSRMLS_FETCH().

Declaration of the Zend Function Block

Now that you have declared the functions to be exported, you also have to introduce them to Zend. Introducing the list of functions is done by using an array of zend_function_entry. This array consecutively contains all functions that are to be made available externally, with the function's name as it should appear in PHP and its name as defined in the C source. Internally, zend_function_entry is defined as shown in Internal declaration of zend_function_entry..

Example #4 Internal declaration of zend_function_entry.

typedef struct _zend_function_entry {
    char *fname;
    void (*handler)(INTERNAL_FUNCTION_PARAMETERS);
    unsigned char *func_arg_types;
} zend_function_entry;
Entry | Description
fname | Denotes the function name as seen in PHP (for example, fopen, mysql_connect, or, in our example, first_module).
handler | Pointer to the C function responsible for handling calls to this function. For example, see the standard macro INTERNAL_FUNCTION_PARAMETERS discussed earlier.
func_arg_types | Allows you to mark certain parameters so that they're forced to be passed by reference. You usually should set this to NULL.
In the example above, the declaration looks like this:
zend_function_entry firstmod_functions[] =
{
    ZEND_FE(first_module, NULL)
    ZEND_FE_END
};
You can see that the last entry in the list always has to be ZEND_FE_END. This marker has to be set for Zend to know when the end of the list of exported functions is reached.

The macro ZEND_FE (short for 'Zend Function Entry') simply expands to a structure entry in zend_function_entry. Note that these macros introduce a special naming scheme to your functions - your C functions will be prefixed with zif_, meaning that ZEND_FE(first_module) will refer to a C function zif_first_module(). If you want to mix macro usage with hand-coded entries (not a good practice), keep this in mind.

Tip: Compilation errors that refer to functions named zif_*() relate to functions defined with ZEND_FE.

Macros for Defining Functions shows a list of all the macros that you can use to define functions.

Macros for Defining Functions
Macro Name | Description
ZEND_FE(name, arg_types) | Defines a function entry of the name name in zend_function_entry. Requires a corresponding C function. arg_types needs to be set to NULL. This function uses automatic C function name generation by prefixing the PHP function name with zif_. For example, ZEND_FE("first_module", NULL) introduces a function first_module() to PHP and links it to the C function zif_first_module(). Use in conjunction with ZEND_FUNCTION.
ZEND_NAMED_FE(php_name, name, arg_types) | Defines a function that will be available to PHP by the name php_name and links it to the corresponding C function name. arg_types needs to be set to NULL. Use this function if you don't want the automatic name prefixing introduced by ZEND_FE. Use in conjunction with ZEND_NAMED_FUNCTION.
ZEND_FALIAS(name, alias, arg_types) | Defines an alias named alias for name. arg_types needs to be set to NULL. Doesn't require a corresponding C function; refers to the alias target instead.
PHP_FE(name, arg_types) | Old PHP API equivalent of ZEND_FE.
PHP_NAMED_FE(runtime_name, name, arg_types) | Old PHP API equivalent of ZEND_NAMED_FE.

Note: You can't use ZEND_FE in conjunction with PHP_FUNCTION, or PHP_FE in conjunction with ZEND_FUNCTION. However, it's perfectly legal to mix ZEND_FE and ZEND_FUNCTION with PHP_FE and PHP_FUNCTION when staying with the same macro set for each function to be declared. But mixing is not recommended; instead, you're advised to use the ZEND_* macros only.

Declaration of the Zend Module Block

This block is stored in the structure zend_module_entry and contains all necessary information to describe the contents of this module to Zend. You can see the internal definition of this module in Internal declaration of zend_module_entry..

Example #5 Internal declaration of zend_module_entry.

typedef struct _zend_module_entry zend_module_entry;

    struct _zend_module_entry {
    unsigned short size;
    unsigned int zend_api;
    unsigned char zend_debug;
    unsigned char zts;
    char *name;
    zend_function_entry *functions;
    int (*module_startup_func)(INIT_FUNC_ARGS);
    int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);
    int (*request_startup_func)(INIT_FUNC_ARGS);
    int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);
    void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);
    char *version;

[ Rest of the structure is not interesting here ]

};
Entry | Description
size, zend_api, zend_debug and zts | Usually filled with the "STANDARD_MODULE_HEADER", which fills these four members with the size of the whole zend_module_entry, the ZEND_MODULE_API_NO, whether it is a debug build or normal build (ZEND_DEBUG) and if ZTS is enabled (USING_ZTS).
name | Contains the module name (for example, "File functions", "Socket functions", "Crypt", etc.). This name will show up in phpinfo(), in the section "Additional Modules."
functions | Points to the Zend function block, discussed in the preceding section.
module_startup_func | This function is called once upon module initialization and can be used to do one-time initialization steps (such as initial memory allocation, etc.). To indicate a failure during initialization, return FAILURE; otherwise, SUCCESS. To mark this field as unused, use NULL. To declare a function, use the macro ZEND_MINIT.
module_shutdown_func | This function is called once upon module shutdown and can be used to do one-time deinitialization steps (such as memory deallocation). This is the counterpart to module_startup_func(). To indicate a failure during deinitialization, return FAILURE; otherwise, SUCCESS. To mark this field as unused, use NULL. To declare a function, use the macro ZEND_MSHUTDOWN.
request_startup_func | This function is called once upon every page request and can be used to do one-time initialization steps that are required to process a request. To indicate a failure here, return FAILURE; otherwise, SUCCESS. Note: As dynamic loadable modules are loaded only on page requests, the request startup function is called right after the module startup function (both initialization events happen at the same time). To mark this field as unused, use NULL. To declare a function, use the macro ZEND_RINIT.
request_shutdown_func | This function is called once after every page request and works as counterpart to request_startup_func(). To indicate a failure here, return FAILURE; otherwise, SUCCESS. Note: As dynamic loadable modules are loaded only on page requests, the request shutdown function is immediately followed by a call to the module shutdown handler (both deinitialization events happen at the same time). To mark this field as unused, use NULL. To declare a function, use the macro ZEND_RSHUTDOWN.
info_func | When phpinfo() is called in a script, Zend cycles through all loaded modules and calls this function. Every module then has the chance to print its own "footprint" into the output page. Generally this is used to dump environmental or statistical information. To mark this field as unused, use NULL. To declare a function, use the macro ZEND_MINFO.
version | The version of the module. You can use NO_VERSION_YET if you don't want to give the module a version number yet, but we really recommend that you add a version string here. Such a version string can look like this (in chronological order): "2.5-dev", "2.5RC1", "2.5" or "2.5pl3".
Remaining structure elements | These are used internally and can be prefilled by using the macro STANDARD_MODULE_PROPERTIES_EX. You should not assign any values to them. Use STANDARD_MODULE_PROPERTIES_EX only if you use global startup and shutdown functions; otherwise, use STANDARD_MODULE_PROPERTIES directly.

In our example, this structure is implemented as follows:

zend_module_entry firstmod_module_entry =
{
    STANDARD_MODULE_HEADER,
    "First Module",
    firstmod_functions,
    NULL, NULL, NULL, NULL, NULL,
    NO_VERSION_YET,
    STANDARD_MODULE_PROPERTIES,
};
This is basically the easiest and most minimal set of values you could ever use. The module name is set to First Module, then the function list is referenced, after which all startup and shutdown functions are marked as being unused.

For reference purposes, you can find a list of the macros involved in declared startup and shutdown functions in Macros to Declare Startup and Shutdown Functions. These are not used in our basic example yet, but will be demonstrated later on. You should make use of these macros to declare your startup and shutdown functions, as these require special arguments to be passed (INIT_FUNC_ARGS and SHUTDOWN_FUNC_ARGS), which are automatically included into the function declaration when using the predefined macros. If you declare your functions manually and the PHP developers decide that a change in the argument list is necessary, you'll have to change your module sources to remain compatible.

Macros to Declare Startup and Shutdown Functions
Macro | Description
ZEND_MINIT(module) | Declares a function for module startup. The generated name will be zend_minit_<module> (for example, zend_minit_first_module). Use in conjunction with ZEND_MINIT_FUNCTION.
ZEND_MSHUTDOWN(module) | Declares a function for module shutdown. The generated name will be zend_mshutdown_<module> (for example, zend_mshutdown_first_module). Use in conjunction with ZEND_MSHUTDOWN_FUNCTION.
ZEND_RINIT(module) | Declares a function for request startup. The generated name will be zend_rinit_<module> (for example, zend_rinit_first_module). Use in conjunction with ZEND_RINIT_FUNCTION.
ZEND_RSHUTDOWN(module) | Declares a function for request shutdown. The generated name will be zend_rshutdown_<module> (for example, zend_rshutdown_first_module). Use in conjunction with ZEND_RSHUTDOWN_FUNCTION.
ZEND_MINFO(module) | Declares a function for printing module information, used when phpinfo() is called. The generated name will be zend_info_<module> (for example, zend_info_first_module). Use in conjunction with ZEND_MINFO_FUNCTION.

Creation of get_module()

This function is special to all dynamic loadable modules. Take a look at the creation via the ZEND_GET_MODULE macro first:

#if COMPILE_DL_FIRSTMOD
     ZEND_GET_MODULE(firstmod)
#endif

The function implementation is surrounded by a conditional compilation statement. This is needed since the function get_module() is only required if your module is built as a dynamic extension. By specifying a definition of COMPILE_DL_FIRSTMOD in the compiler command (see above for a discussion of the compilation instructions required to build a dynamic extension), you can instruct your module whether you intend to build it as a dynamic extension or as a built-in module. If you want a built-in module, the implementation of get_module() is simply left out.

get_module() is called by Zend at load time of the module. You can think of it as being invoked by the dl() call in your script. Its purpose is to pass the module information block back to Zend in order to inform the engine about the module contents.

If you don't implement a get_module() function in your dynamic loadable module, Zend will compliment you with an error message when trying to access it.

Implementation of All Exported Functions

Implementing the exported functions is the final step. The example function in first_module looks like this:

ZEND_FUNCTION(first_module)
{
    long parameter;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &parameter) == FAILURE) {
        return;
    }

    RETURN_LONG(parameter);
}
The function declaration is done using ZEND_FUNCTION, which corresponds to ZEND_FE in the function entry table (discussed earlier).

After the declaration, code for checking and retrieving the function's arguments, argument conversion, and return value generation follows (more on this later).

Summary

That's it, basically - there's nothing more to implementing PHP modules. Built-in modules are structured similarly to dynamic modules, so, equipped with the information presented in the previous sections, you'll be able to fight the odds when encountering PHP module source files.

Now, in the following sections, read on about how to make use of PHP's internals to build powerful extensions.

Accepting Arguments

One of the most important issues for language extensions is accepting and dealing with data passed via arguments. Most extensions are built to deal with specific input data (or require parameters to perform their specific actions), and function arguments are the only real way to exchange data between the PHP level and the C level. Of course, there's also the possibility of exchanging data using predefined global values (which is also discussed later), but this should be avoided by all means, as it's extremely bad practice.

PHP doesn't make use of any formal function declarations; this is why call syntax is always completely dynamic and never checked for errors. Checking for correct call syntax is left to the user code. For example, it's possible to call a function using only one argument at one time and four arguments the next time - both invocations are syntactically absolutely correct.

Determining the Number of Arguments

Since PHP doesn't have formal function definitions with support for call syntax checking, and since PHP features variable arguments, sometimes you need to find out with how many arguments your function has been called. You can use the ZEND_NUM_ARGS macro in this case. In previous versions of PHP, this macro retrieved the number of arguments with which the function has been called based on the function's hash table entry, ht, which is passed in the INTERNAL_FUNCTION_PARAMETERS list. As ht itself now contains the number of arguments that have been passed to the function, ZEND_NUM_ARGS has been stripped down to a dummy macro (see its definition in zend_API.h). But it's still good practice to use it, to remain compatible with future changes in the call interface. Note: The old PHP equivalent of this macro is ARG_COUNT.

The following code checks for the correct number of arguments:

if(ZEND_NUM_ARGS() != 2) WRONG_PARAM_COUNT;
If the function is not called with two arguments, it exits with an error message. The code snippet above makes use of the tool macro WRONG_PARAM_COUNT, which can be used to generate a standard error message like:
"Warning: Wrong parameter count for firstmodule() in /home/www/htdocs/firstmod.php on line 5"

This macro prints a default error message and then returns to the caller. Its definition can also be found in zend_API.h and looks like this:

ZEND_API void wrong_param_count(void);

#define WRONG_PARAM_COUNT { wrong_param_count(); return; }
As you can see, it calls an internal function named wrong_param_count() that's responsible for printing the warning. For details on generating customized error messages, see the later section "Printing Information."

Retrieving Arguments

Note: New parameter parsing API

This chapter documents the new Zend parameter parsing API introduced by Andrei Zmievski. It was introduced in the development stage between PHP 4.0.6 and 4.1.0.

Parsing parameters is a very common operation and it may get a bit tedious. It would also be nice to have standardized error checking and error messages. Since PHP 4.1.0, there is a way to do just that by using the new parameter parsing API. It greatly simplifies the process of receiving parameters, but it has a drawback in that it can't be used for functions that expect variable number of parameters. But since the vast majority of functions do not fall into those categories, this parsing API is recommended as the new standard way.

The prototype for parameter parsing function looks like this:

int zend_parse_parameters(int num_args TSRMLS_DC, char *type_spec, ...);
The first argument to this function is supposed to be the number of actual parameters passed to your function, so ZEND_NUM_ARGS() can be used for that. The second parameter should always be TSRMLS_CC macro. The third argument is a string that specifies the number and types of arguments your function is expecting, similar to how printf format string specifies the number and format of the output values it should operate on. And finally the rest of the arguments are pointers to variables which should receive the values from the parameters.

zend_parse_parameters() also performs type conversions whenever possible, so that you always receive the data in the format you asked for. Any type of scalar can be converted to another one, but conversions between complex types (arrays, objects, and resources) and scalar types are not allowed.

If the parameters could be obtained successfully and there were no errors during type conversion, the function will return SUCCESS, otherwise it will return FAILURE. The function will output informative error messages, if the number of received parameters does not match the requested number, or if type conversion could not be performed.

Here are some sample error messages:

Warning - ini_get_all() requires at most 1 parameter, 2 given
Warning - wddx_deserialize() expects parameter 1 to be string, array given
Of course each error message is accompanied by the filename and line number on which it occurs.

Here is the full list of type specifiers:

  • l - long

  • d - double

  • s - string (with possible null bytes) and its length

  • b - boolean

  • r - resource, stored in zval*

  • a - array, stored in zval*

  • o - object (of any class), stored in zval*

  • O - object (of class specified by class entry), stored in zval*

  • z - the actual zval*

The following characters also have a meaning in the specifier string:

  • | - indicates that the remaining parameters are optional. The storage variables corresponding to these parameters should be initialized to default values by the extension, since they will not be touched by the parsing function if the parameters are not passed.

  • / - the parsing function will call SEPARATE_ZVAL_IF_NOT_REF() on the parameter it follows, to provide a copy of the parameter, unless it's a reference.

  • ! - the parameter it follows can be of specified type or NULL (only applies to a, o, O, r, and z). If NULL value is passed by the user, the storage pointer will be set to NULL.

The best way to illustrate the usage of this function is through examples:

/* Gets a long, a string and its length, and a zval. */
long l;
char *s;
int s_len;
zval *param;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                          "lsz", &l, &s, &s_len, &param) == FAILURE) {
    return;
}

/* Gets an object of class specified by my_ce, and an optional double. */
zval *obj;
double d = 0.5;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                          "O|d", &obj, my_ce, &d) == FAILURE) {
    return;
}

/* Gets an object or null, and an array.
   If null is passed for object, obj will be set to NULL. */
zval *obj;
zval *arr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O!a", &obj, &arr) == FAILURE) {
    return;
}

/* Gets a separated array. */
zval *arr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &arr) == FAILURE) {
    return;
}

/* Get only the first three parameters (useful for varargs functions). */
zval *z;
zend_bool b;
zval *r;
if (zend_parse_parameters(3, "zbr!", &z, &b, &r) == FAILURE) {
    return;
}

Note that in the last example we pass 3 for the number of received parameters, instead of ZEND_NUM_ARGS(). What this lets us do is receive the least number of parameters if our function expects a variable number of them. Of course, if you want to operate on the rest of the parameters, you will have to use zend_get_parameters_array_ex() to obtain them.

The parsing function has an extended version that allows for an additional flags argument that controls its actions.

int zend_parse_parameters_ex(int flags, int num_args TSRMLS_DC, char *type_spec, ...);

The only flag you can pass currently is ZEND_PARSE_PARAMS_QUIET, which instructs the function to not output any error messages during its operation. This is useful for functions that expect several sets of completely different arguments, but you will have to output your own error messages.

For example, here is how you would get either a set of three longs or a string:

long l1, l2, l3;
char *s;
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
                             ZEND_NUM_ARGS() TSRMLS_CC,
                             "lll", &l1, &l2, &l3) == SUCCESS) {
    /* manipulate longs */
} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
                                    ZEND_NUM_ARGS(), "s", &s, &s_len) == SUCCESS) {
    /* manipulate string */
} else {
    php_error(E_WARNING, "%s() takes either three long values or a string as argument",
              get_active_function_name(TSRMLS_C));
    return;
}

With all the abovementioned ways of receiving function parameters you should have a good handle on this process. For even more example, look through the source code for extensions that are shipped with PHP - they illustrate every conceivable situation.

Old way of retrieving arguments (deprecated)

Note: Deprecated parameter parsing API

This API is deprecated and superseded by the new ZEND parameter parsing API.

After having checked the number of arguments, you need to get access to the arguments themselves. This is done with the help of zend_get_parameters_ex():

zval **parameter;

if(zend_get_parameters_ex(1, &parameter) != SUCCESS)
  WRONG_PARAM_COUNT;
All arguments are stored in a zval container, which needs to be pointed to twice. The snippet above tries to retrieve one argument and make it available to us via the parameter pointer.

zend_get_parameters_ex() accepts at least two arguments. The first argument is the number of arguments to retrieve (which should match the number of arguments with which the function has been called; this is why it's important to check for correct call syntax). The second argument (and all following arguments) are pointers to pointers to pointers to zvals. (Confusing, isn't it?) All these pointers are required because Zend works internally with **zval; to adjust a local **zval in our function,zend_get_parameters_ex() requires a pointer to it.

The return value of zend_get_parameters_ex() can either be SUCCESS or FAILURE, indicating (unsurprisingly) success or failure of the argument processing. A failure is most likely related to an incorrect number of arguments being specified, in which case you should exit with WRONG_PARAM_COUNT.

To retrieve more than one argument, you can use a similar snippet:

zval **param1, **param2, **param3, **param4;

if(zend_get_parameters_ex(4, &param1, &param2, &param3, &param4) != SUCCESS)
    WRONG_PARAM_COUNT;

zend_get_parameters_ex() only checks whether you're trying to retrieve too many parameters. If the function is called with five arguments, but you're only retrieving three of them with zend_get_parameters_ex(), you won't get an error but will get the first three parameters instead. Subsequent calls of zend_get_parameters_ex() won't retrieve the remaining arguments, but will get the same arguments again.

Dealing with a Variable Number of Arguments/Optional Parameters

If your function is meant to accept a variable number of arguments, the snippets just described are sometimes suboptimal solutions. You have to create a line calling zend_get_parameters_ex() for every possible number of arguments, which is often unsatisfying.

For this case, you can use the function zend_get_parameters_array_ex(), which accepts the number of parameters to retrieve and an array in which to store them:

zval **parameter_array[4];

/* get the number of arguments */
argument_count = ZEND_NUM_ARGS();

/* see if it satisfies our minimal request (2 arguments) */
/* and our maximal acceptance (4 arguments) */
if(argument_count < 2 || argument_count > 4)
    WRONG_PARAM_COUNT;

/* argument count is correct, now retrieve arguments */
if(zend_get_parameters_array_ex(argument_count, parameter_array) != SUCCESS)
    WRONG_PARAM_COUNT;
First, the number of arguments is checked to make sure that it's in the accepted range. After that, zend_get_parameters_array_ex() is used to fill parameter_array with valid pointers to the argument values.

A very clever implementation of this can be found in the code handling PHP's fsockopen() located in ext/standard/fsock.c, as shown in PHP's implementation of variable arguments in fsockopen().. Don't worry if you don't know all the functions used in this source yet; we'll get to them shortly.

Example #6 PHP's implementation of variable arguments in fsockopen().

pval **args[5];
int *sock=emalloc(sizeof(int));
int *sockp;
int arg_count=ARG_COUNT(ht);
int socketd = -1;
unsigned char udp = 0;
struct timeval timeout = { 60, 0 };
unsigned short portno;
unsigned long conv;
char *key = NULL;
FLS_FETCH();

if (arg_count > 5 || arg_count < 2 || zend_get_parameters_array_ex(arg_count,args)==FAILURE) {
    CLOSE_SOCK(1);
    WRONG_PARAM_COUNT;
}

switch(arg_count) {
    case 5:
        convert_to_double_ex(args[4]);
        conv = (unsigned long) (Z_DVAL_PP(args[4]) * 1000000.0);
        timeout.tv_sec = conv / 1000000;
        timeout.tv_usec = conv % 1000000;
        /* fall-through */
    case 4:
        if (!PZVAL_IS_REF(*args[3])) {
            php_error(E_WARNING,"error string argument to fsockopen not passed by reference");
        }
        pval_copy_constructor(*args[3]);
        ZVAL_EMPTY_STRING(*args[3]);
        /* fall-through */
    case 3:
        if (!PZVAL_IS_REF(*args[2])) {
            php_error(E_WARNING,"error argument to fsockopen not passed by reference");
            return;
        }
        ZVAL_LONG(*args[2], 0);
        break;
}

convert_to_string_ex(args[0]);
convert_to_long_ex(args[1]);
portno = (unsigned short) Z_LVAL_P(args[1]);

key = emalloc(Z_STRLEN_P(args[0]) + 10);

fsockopen() accepts two, three, four, or five parameters. After the obligatory variable declarations, the function checks for the correct range of arguments. Then it uses a fall-through mechanism in a switch() statement to deal with all arguments. The switch() statement starts with the maximum number of arguments being passed (five). After that, it automatically processes the case of four arguments being passed, then three, by omitting the otherwise obligatory break keyword in all stages. After having processed the last case, it exits the switch() statement and does the minimal argument processing needed if the function is invoked with only two arguments.

This multiple-stage type of processing, similar to a stairway, allows convenient processing of a variable number of arguments.

Accessing Arguments

To access arguments, it's necessary for each argument to have a clearly defined type. Again, PHP's extremely dynamic nature introduces some quirks. Because PHP never does any kind of type checking, it's possible for a caller to pass any kind of data to your functions, whether you want it or not. If you expect an integer, for example, the caller might pass an array, and vice versa - PHP simply won't notice.

To work around this, you have to use a set of API functions to force a type conversion on every argument that's being passed (see Argument Conversion Functions).

Note: All conversion functions expect a **zval as parameter.

Argument Conversion Functions
Function | Description
convert_to_boolean_ex() | Forces conversion to a Boolean type. Boolean values remain untouched. Longs, doubles, and strings containing 0 as well as NULL values will result in Boolean 0 (FALSE). Arrays and objects are converted based on the number of entries or properties, respectively, that they have. Empty arrays and objects are converted to FALSE; otherwise, to TRUE. All other values result in a Boolean 1 (TRUE).
convert_to_long_ex() | Forces conversion to a long, the default integer type. NULL values, Booleans, resources, and of course longs remain untouched. Doubles are truncated. Strings containing an integer are converted to their corresponding numeric representation, otherwise resulting in 0. Arrays and objects are converted to 0 if empty, 1 otherwise.
convert_to_double_ex() | Forces conversion to a double, the default floating-point type. NULL values, Booleans, resources, longs, and of course doubles remain untouched. Strings containing a number are converted to their corresponding numeric representation, otherwise resulting in 0.0. Arrays and objects are converted to 0.0 if empty, 1.0 otherwise.
convert_to_string_ex() | Forces conversion to a string. Strings remain untouched. NULL values are converted to an empty string. Booleans containing TRUE are converted to "1", otherwise resulting in an empty string. Longs and doubles are converted to their corresponding string representation. Arrays are converted to the string "Array" and objects to the string "Object".
convert_to_array_ex(value) | Forces conversion to an array. Arrays remain untouched. Objects are converted to an array by assigning all their properties to the array table. All property names are used as keys, property contents as values. NULL values are converted to an empty array. All other values are converted to an array that contains the specific source value in the element with the key 0.
convert_to_object_ex(value) | Forces conversion to an object. Objects remain untouched. NULL values are converted to an empty object. Arrays are converted to objects by introducing their keys as properties into the objects and their values as corresponding property contents in the object. All other types result in an object with the property scalar , having the corresponding source value as content.
convert_to_null_ex(value) | Forces the type to become a NULL value, meaning empty.

Note:

You can find a demonstration of the behavior in cross_conversion.php on the accompanying CD-ROM.

Cross-conversion behavior of PHP.

Using these functions on your arguments will ensure type safety for all data that's passed to you. If the supplied type doesn't match the required type, PHP forces dummy contents on the resulting value (empty strings, arrays, or objects, 0 for numeric values, FALSE for Booleans) to ensure a defined state.

Following is a quote from the sample module discussed previously, which makes use of the conversion functions:

zval **parameter;

if((ZEND_NUM_ARGS() != 1) || (zend_get_parameters_ex(1, &parameter) != SUCCESS))
{
    WRONG_PARAM_COUNT;
}

convert_to_long_ex(parameter);

RETURN_LONG(Z_LVAL_P(parameter));
After retrieving the parameter pointer, the parameter value is converted to a long (an integer), which also forms the return value of this function. Understanding access to the contents of the value requires a short discussion of the zval type, whose definition is shown in PHP/Zend zval type definition..

Example #7 PHP/Zend zval type definition.

typedef pval zval;

typedef struct _zval_struct zval;

typedef union _zvalue_value {
    long lval;                 /* long value */
    double dval;               /* double value */
    struct {
        char *val;
        int len;
    } str;
    HashTable *ht;             /* hash table value */
    struct {
        zend_class_entry *ce;
        HashTable *properties;
    } obj;
} zvalue_value;

struct _zval_struct {
    /* Variable information */
    zvalue_value value;        /* value */
    unsigned char type;        /* active type */
    unsigned char is_ref;
    short refcount;
};

Actually, pval (defined in php.h) is only an alias of zval (defined in zend.h), which in turn refers to _zval_struct. This is a most interesting structure. _zval_struct is the "master" structure, containing the value structure, type, and reference information. The substructure zvalue_value is a union that contains the variable's contents. Depending on the variable's type, you'll have to access different members of this union. For a description of both structures, see Zend zval Structure, Zend zvalue_value Structure and Zend Variable Type Constants.

Zend zval Structure
Entry | Description
value | Union containing this variable's contents. See Zend zvalue_value Structure for a description.
type | Contains this variable's type. For a list of available types, see Zend Variable Type Constants.
is_ref | 0 means that this variable is not a reference; 1 means that this variable is a reference to another variable.
refcount | The number of references that exist for this variable. For every new reference to the value stored in this variable, this counter is increased by 1. For every lost reference, this counter is decreased by 1. When the reference counter reaches 0, no references exist to this value anymore, which causes automatic freeing of the value.
Zend zvalue_value Structure
Entry | Description
lval | Use this property if the variable is of the type IS_LONG, IS_BOOLEAN, or IS_RESOURCE.
dval | Use this property if the variable is of the type IS_DOUBLE.
str | This structure can be used to access variables of the type IS_STRING. The member len contains the string length; the member val points to the string itself. Zend uses C strings; thus, the string length contains a trailing 0x00.
ht | This entry points to the variable's hash table entry if the variable is an array.
obj | Use this property if the variable is of the type IS_OBJECT.
Zend Variable Type Constants
Constant | Description
IS_NULL | Denotes a NULL (empty) value.
IS_LONG | A long (integer) value.
IS_DOUBLE | A double (floating point) value.
IS_STRING | A string.
IS_ARRAY | Denotes an array.
IS_OBJECT | An object.
IS_BOOL | A Boolean value.
IS_RESOURCE | A resource (for a discussion of resources, see the appropriate section below).
IS_CONSTANT | A constant (defined) value.

To access a long you access zval.value.lval, to access a double you use zval.value.dval, and so on. Because all values are stored in a union, trying to access data with incorrect union members results in meaningless output.

Accessing arrays and objects is a bit more complicated and is discussed later.

Dealing with Arguments Passed by Reference

If your function accepts arguments passed by reference that you intend to modify, you need to take some precautions.

What we didn't say yet is that under the circumstances presented so far, you don't have write access to any zval containers designating function parameters that have been passed to you. Of course, you can change any zval containers that you created within your function, but you mustn't change any zvals that refer to Zend-internal data!

We've only discussed the so-called *_ex() API so far. You may have noticed that the API functions we've used are called zend_get_parameters_ex() instead of zend_get_parameters(), convert_to_long_ex() instead of convert_to_long(), etc. The *_ex() functions form the so-called new "extended" Zend API. They give a minor speed increase over the old API, but as a tradeoff are only meant for providing read-only access.

Because Zend works internally with references, different variables may reference the same value. Write access to a zval container requires this container to contain an isolated value, meaning a value that's not referenced by any other containers. If a zval container were referenced by other containers and you changed the referenced zval, you would automatically change the contents of the other containers referencing this zval (because they'd simply point to the changed value and thus change their own value as well).

zend_get_parameters_ex() doesn't care about this situation, but simply returns a pointer to the desired zval containers, whether they consist of references or not. Its corresponding function in the traditional API, zend_get_parameters(), immediately checks for referenced values. If it finds a reference, it creates a new, isolated zval container; copies the referenced data into this newly allocated space; and then returns a pointer to the new, isolated value.

This action is called zval separation (or pval separation). Because the *_ex() API doesn't perform zval separation, it's considerably faster, while at the same time disabling write access.

To change parameters, however, write access is required. Zend deals with this situation in a special way: Whenever a parameter to a function is passed by reference, it performs automatic zval separation. This means that whenever you're calling a function like this in PHP, Zend will automatically ensure that $parameter is being passed as an isolated value, rendering it to a write-safe state:

my_function(&$parameter);

But this is not the case with regular parameters! All other parameters that are not passed by reference are in a read-only state.

This requires you to make sure that you're really working with a reference - otherwise you might produce unwanted results. To check for a parameter being passed by reference, you can use the macro PZVAL_IS_REF. This macro accepts a zval* to check if it is a reference or not. Examples are given in in Testing for referenced parameter passing..

Example #8 Testing for referenced parameter passing.

zval *parameter;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &parameter) == FAILURE)
    return;

/* check for parameter being passed by reference */
if (!PZVAL_IS_REF(parameter)) {
{
    zend_error(E_WARNING, "Parameter wasn't passed by reference");
    RETURN_NULL();
}

/* make changes to the parameter */
ZVAL_LONG(parameter, 10);
Testing for referenced parameter passing

Assuring Write Safety for Other Parameters

You might run into a situation in which you need write access to a parameter that's retrieved with zend_get_parameters_ex() but not passed by reference. For this case, you can use the macro SEPARATE_ZVAL, which does a zval separation on the provided container. The newly generated zval is detached from internal data and has only a local scope, meaning that it can be changed or destroyed without implying global changes in the script context:

zval **parameter;

/* retrieve parameter */
zend_get_parameters_ex(1, &parameter);

/* at this stage, <parameter> still is connected */
/* to Zend's internal data buffers */

/* make <parameter> write-safe */
SEPARATE_ZVAL(parameter);

/* now we can safely modify <parameter> */
/* without implying global changes */
SEPARATE_ZVAL uses emalloc() to allocate the new zval container, which means that even if you don't deallocate this memory yourself, it will be destroyed automatically upon script termination. However, doing a lot of calls to this macro without freeing the resulting containers will clutter up your RAM.

Note: As you can easily work around the lack of write access in the "traditional" API (with zend_get_parameters() and so on), this API seems to be obsolete, and is not discussed further in this chapter.

Creating Variables

When exchanging data from your own extensions with PHP scripts, one of the most important issues is the creation of variables. This section shows you how to deal with the variable types that PHP supports.

Overview

To create new variables that can be seen "from the outside" by the executing script, you need to allocate a new zval container, fill this container with meaningful values, and then introduce it to Zend's internal symbol table. This basic process is common to all variable creations:

zval *new_variable;

/* allocate and initialize new container */
MAKE_STD_ZVAL(new_variable);

/* set type and variable contents here, see the following sections */

/* introduce this variable by the name "new_variable_name" into the symbol table */
ZEND_SET_SYMBOL(EG(active_symbol_table), "new_variable_name", new_variable);

/* the variable is now accessible to the script by using $new_variable_name */

The macro MAKE_STD_ZVAL allocates a new zval container using ALLOC_ZVAL and initializes it using INIT_ZVAL. As implemented in Zend at the time of this writing, initializing means setting the reference count to 1 and clearing the is_ref flag, but this process could be extended later - this is why it's a good idea to keep using MAKE_STD_ZVAL instead of only using ALLOC_ZVAL. If you want to optimize for speed (and you don't have to explicitly initialize the zval container here), you can use ALLOC_ZVAL, but this isn't recommended because it doesn't ensure data integrity.

ZEND_SET_SYMBOL takes care of introducing the new variable to Zend's symbol table. This macro checks whether the value already exists in the symbol table and converts the new symbol to a reference if so (with automatic deallocation of the old zval container). This is the preferred method if speed is not a crucial issue and you'd like to keep memory usage low.

Note that ZEND_SET_SYMBOL makes use of the Zend executor globals via the macro EG. By specifying EG(active_symbol_table), you get access to the currently active symbol table, dealing with the active, local scope. The local scope may differ depending on whether the function was invoked from within a function.

If you need to optimize for speed and don't care about optimal memory usage, you can omit the check for an existing variable with the same value and instead force insertion into the symbol table by using zend_hash_update():

zval *new_variable;

/* allocate and initialize new container */
MAKE_STD_ZVAL(new_variable);

/* set type and variable contents here, see the following sections */

/* introduce this variable by the name "new_variable_name" into the symbol table */
zend_hash_update(
    EG(active_symbol_table),
    "new_variable_name",
    strlen("new_variable_name") + 1,
    &new_variable,
    sizeof(zval *),
    NULL
);
This is actually the standard method used in most modules.

The variables generated with the snippet above will always be of local scope, so they reside in the context in which the function has been called. To create new variables in the global scope, use the same method but refer to another symbol table:

zval *new_variable;

// allocate and initialize new container
MAKE_STD_ZVAL(new_variable);

//
// set type and variable contents here
//

// introduce this variable by the name "new_variable_name" into the global symbol table
ZEND_SET_SYMBOL(&EG(symbol_table), "new_variable_name", new_variable);
The macro ZEND_SET_SYMBOL is now being called with a reference to the main, global symbol table by referring EG(symbol_table).

Note: The active_symbol_table variable is a pointer, but symbol_table is not. This is why you have to use EG(active_symbol_table) and &EG(symbol_table) as parameters to ZEND_SET_SYMBOL - it requires a pointer.

Similarly, to get a more efficient version, you can hardcode the symbol table update:

zval *new_variable;

// allocate and initialize new container
MAKE_STD_ZVAL(new_variable);

//
// set type and variable contents here
//

// introduce this variable by the name "new_variable_name" into the global symbol table
zend_hash_update(
    &EG(symbol_table),
    "new_variable_name",
    strlen("new_variable_name") + 1,
    &new_variable,
    sizeof(zval *),
    NULL
);
Creating variables with different scopes. shows a sample source that creates two variables - local_variable with a local scope and global_variable with a global scope (see Figure 9.7). The full example can be found on the CD-ROM.

Note: You can see that the global variable is actually not accessible from within the function. This is because it's not imported into the local scope using global $global_variable; in the PHP source.

Example #9 Creating variables with different scopes.

ZEND_FUNCTION(variable_creation)
{
    zval *new_var1, *new_var2;

    MAKE_STD_ZVAL(new_var1);
    MAKE_STD_ZVAL(new_var2);

    ZVAL_LONG(new_var1, 10);
    ZVAL_LONG(new_var2, 5);

    ZEND_SET_SYMBOL(EG(active_symbol_table), "local_variable", new_var1);
    ZEND_SET_SYMBOL(&EG(symbol_table), "global_variable", new_var2);

    RETURN_NULL();

}
Creating variables with different scopes

Longs (Integers)

Now let's get to the assignment of data to variables, starting with longs. Longs are PHP's integers and are very simple to store. Looking at the zval.value container structure discussed earlier in this chapter, you can see that the long data type is directly contained in the union, namely in the lval field. The corresponding type value for longs is IS_LONG (see Creation of a long.).

Example #10 Creation of a long.

zval *new_long;

MAKE_STD_ZVAL(new_long);

new_long-&gt;type = IS_LONG;
new_long-&gt;value.lval = 10;
Alternatively, you can use the macro ZVAL_LONG:
zval *new_long;

MAKE_STD_ZVAL(new_long);
ZVAL_LONG(new_long, 10);

Doubles (Floats)

Doubles are PHP's floats and are as easy to assign as longs, because their value is also contained directly in the union. The member in the zval.value container is dval; the corresponding type is IS_DOUBLE.

zval *new_double;

MAKE_STD_ZVAL(new_double);

new_double-&gt;type = IS_DOUBLE;
new_double-&gt;value.dval = 3.45;
Alternatively, you can use the macro ZVAL_DOUBLE:
zval *new_double;

MAKE_STD_ZVAL(new_double);
ZVAL_DOUBLE(new_double, 3.45);

Strings

Strings need slightly more effort. As mentioned earlier, all strings that will be associated with Zend's internal data structures need to be allocated using Zend's own memory-management functions. Referencing of static strings or strings allocated with standard routines is not allowed. To assign strings, you have to access the structure str in the zval.value container. The corresponding type is IS_STRING:

zval *new_string;
char *string_contents = "This is a new string variable";

MAKE_STD_ZVAL(new_string);

new_string-&gt;type = IS_STRING;
new_string-&gt;value.str.len = strlen(string_contents);
new_string-&gt;value.str.val = estrdup(string_contents);
Note the usage of Zend's estrdup() here. Of course, you can also use the predefined macro ZVAL_STRING:
zval *new_string;
char *string_contents = "This is a new string variable";

MAKE_STD_ZVAL(new_string);
ZVAL_STRING(new_string, string_contents, 1);
ZVAL_STRING accepts a third parameter that indicates whether the supplied string contents should be duplicated (using estrdup()). Setting this parameter to 1 causes the string to be duplicated; 0 simply uses the supplied pointer for the variable contents. This is most useful if you want to create a new variable referring to a string that's already allocated in Zend internal memory.

If you want to truncate the string at a certain position or you already know its length, you can use ZVAL_STRINGL(zval, string, length, duplicate), which accepts an explicit string length to be set for the new string. This macro is faster than ZVAL_STRING and also binary-safe.

To create empty strings, set the string length to 0 and use empty_string as contents:

new_string-&gt;type = IS_STRING;
new_string-&gt;value.str.len = 0;
new_string-&gt;value.str.val = empty_string;
Of course, there's a macro for this as well (ZVAL_EMPTY_STRING):
MAKE_STD_ZVAL(new_string);
ZVAL_EMPTY_STRING(new_string);

Booleans

Booleans are created just like longs, but have the type IS_BOOL. Allowed values in lval are 0 and 1:

zval *new_bool;

MAKE_STD_ZVAL(new_bool);

new_bool-&gt;type = IS_BOOL;
new_bool-&gt;value.lval = 1;
The corresponding macros for this type are ZVAL_BOOL (allowing specification of the value) as well as ZVAL_TRUE and ZVAL_FALSE (which explicitly set the value to TRUE and FALSE, respectively).

Arrays

Arrays are stored using Zend's internal hash tables, which can be accessed using the zend_hash_*() API. For every array that you want to create, you need a new hash table handle, which will be stored in the ht member of the zval.value container.

There's a whole API solely for the creation of arrays, which is extremely handy. To start a new array, you call array_init().

zval *new_array;

MAKE_STD_ZVAL(new_array);

array_init(new_array);
array_init() always returns SUCCESS.

To add new elements to the array, you can use numerous functions, depending on what you want to do. Zend's API for Associative Arrays, Zend's API for Indexed Arrays, Part 1 and Zend's API for Indexed Arrays, Part 2 describe these functions. All functions return FAILURE on failure and SUCCESS on success.

Zend's API for Associative Arrays
Function | Description
add_assoc_long(zval *array, char *key, long n);() | Adds an element of type long.
add_assoc_unset(zval *array, char *key);() | Adds an unset element.
add_assoc_bool(zval *array, char *key, int b);() | Adds a Boolean element.
add_assoc_resource(zval *array, char *key, int r);() | Adds a resource to the array.
add_assoc_double(zval *array, char *key, double d);() | Adds a floating-point value.
add_assoc_string(zval *array, char *key, char *str, int duplicate);() | Adds a string to the array. The flag duplicate specifies whether the string contents have to be copied to Zend internal memory.
add_assoc_stringl(zval *array, char *key, char *str, uint length, int duplicate); () | Adds a string with the desired length length to the array. Otherwise, behaves like add_assoc_string().
add_assoc_zval(zval *array, char *key, zval *value);() | Adds a zval to the array. Useful for adding other arrays, objects, streams, etc...
Zend's API for Indexed Arrays, Part 1
Function | Description
add_index_long(zval *array, uint idx, long n);() | Adds an element of type long.
add_index_unset(zval *array, uint idx);() | Adds an unset element.
add_index_bool(zval *array, uint idx, int b);() | Adds a Boolean element.
add_index_resource(zval *array, uint idx, int r);() | Adds a resource to the array.
add_index_double(zval *array, uint idx, double d);() | Adds a floating-point value.
add_index_string(zval *array, uint idx, char *str, int duplicate);() | Adds a string to the array. The flag duplicate specifies whether the string contents have to be copied to Zend internal memory.
add_index_stringl(zval *array, uint idx, char *str, uint length, int duplicate);() | Adds a string with the desired length length to the array. This function is faster and binary-safe. Otherwise, behaves like add_index_string().
add_index_zval(zval *array, uint idx, zval *value);() | Adds a zval to the array. Useful for adding other arrays, objects, streams, etc...
Zend's API for Indexed Arrays, Part 2
Function | Description
add_next_index_long(zval *array, long n);() | Adds an element of type long.
add_next_index_unset(zval *array);() | Adds an unset element.
add_next_index_bool(zval *array, int b);() | Adds a Boolean element.
add_next_index_resource(zval *array, int r);() | Adds a resource to the array.
add_next_index_double(zval *array, double d);() | Adds a floating-point value.
add_next_index_string(zval *array, char *str, int duplicate);() | Adds a string to the array. The flag duplicate specifies whether the string contents have to be copied to Zend internal memory.
add_next_index_stringl(zval *array, char *str, uint length, int duplicate);() | Adds a string with the desired length length to the array. This function is faster and binary-safe. Otherwise, behaves like add_index_string().
add_next_index_zval(zval *array, zval *value);() | Adds a zval to the array. Useful for adding other arrays, objects, streams, etc...

All these functions provide a handy abstraction to Zend's internal hash API. Of course, you can also use the hash functions directly - for example, if you already have a zval container allocated that you want to insert into an array. This is done using zend_hash_update() for associative arrays (see Adding an element to an associative array.) and zend_hash_index_update() for indexed arrays (see Adding an element to an indexed array.):

Example #11 Adding an element to an associative array.

zval *new_array, *new_element;
char *key = "element_key";

MAKE_STD_ZVAL(new_array);
MAKE_STD_ZVAL(new_element);

array_init(new_array);

ZVAL_LONG(new_element, 10);

if(zend_hash_update(new_array-&gt;value.ht, key, strlen(key) + 1, (void *)&new_element, sizeof(zval *), NULL) == FAILURE)
{
    // do error handling here
}

Example #12 Adding an element to an indexed array.

zval *new_array, *new_element;
int key = 2;

MAKE_STD_ZVAL(new_array);
MAKE_STD_ZVAL(new_element);

array_init(new_array);

ZVAL_LONG(new_element, 10);

if(zend_hash_index_update(new_array-&gt;value.ht, key, (void *)&new_element, sizeof(zval *), NULL) == FAILURE)
{
    // do error handling here
}

To emulate the functionality of add_next_index_*(), you can use this:

zend_hash_next_index_insert(ht, zval **new_element, sizeof(zval *), NULL)

Note: To return arrays from a function, use array_init() and all following actions on the predefined variable return_value (given as argument to your exported function; see the earlier discussion of the call interface). You do not have to use MAKE_STD_ZVAL on this.

Tip: To avoid having to write new_array->value.ht every time, you can use HASH_OF(new_array), which is also recommended for compatibility and style reasons.

Objects

Since objects can be converted to arrays (and vice versa), you might have already guessed that they have a lot of similarities to arrays in PHP. Objects are maintained with the same hash functions, but there's a different API for creating them.

To initialize an object, you use the function object_init():

zval *new_object;

MAKE_STD_ZVAL(new_object);

if(object_init(new_object) != SUCCESS)
{
    // do error handling here
}
You can use the functions described in Zend's API for Object Creation to add members to your object.

Zend's API for Object Creation
Function | Description
add_property_long(zval *object, char *key, long l);() | Adds a long to the object.
add_property_unset(zval *object, char *key);() | Adds an unset property to the object.
add_property_bool(zval *object, char *key, int b);() | Adds a Boolean to the object.
add_property_resource(zval *object, char *key, long r);() | Adds a resource to the object.
add_property_double(zval *object, char *key, double d);() | Adds a double to the object.
add_property_string(zval *object, char *key, char *str, int duplicate);() | Adds a string to the object.
add_property_stringl(zval *object, char *key, char *str, uint length, int duplicate);() | Adds a string of the specified length to the object. This function is faster than add_property_string() and also binary-safe.
add_property_zval(zval *obect, char *key, zval *container):() | Adds a zval container to the object. This is useful if you have to add properties which aren't simple types like integers or strings but arrays or other objects.

Resources

Resources are a special kind of data type in PHP. The term resources doesn't really refer to any special kind of data, but to an abstraction method for maintaining any kind of information. Resources are kept in a special resource list within Zend. Each entry in the list has a correspondending type definition that denotes the kind of resource to which it refers. Zend then internally manages all references to this resource. Access to a resource is never possible directly - only via a provided API. As soon as all references to a specific resource are lost, a corresponding shutdown function is called.

For example, resources are used to store database links and file descriptors. The de facto standard implementation can be found in the MySQL module, but other modules such as the Oracle module also make use of resources.

Note:

In fact, a resource can be a pointer to anything you need to handle in your functions (e.g. pointer to a structure) and the user only has to pass a single resource variable to your function.

To create a new resource you need to register a resource destruction handler for it. Since you can store any kind of data as a resource, Zend needs to know how to free this resource if its not longer needed. This works by registering your own resource destruction handler to Zend which in turn gets called by Zend whenever your resource can be freed (whether manually or automatically). Registering your resource handler within Zend returns you the resource type handle for that resource. This handle is needed whenever you want to access a resource of this type later and is most of time stored in a global static variable within your extension. There is no need to worry about thread safety here because you only register your resource handler once during module initialization.

The Zend function to register your resource handler is defined as:

ZEND_API int zend_register_list_destructors_ex(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, char *type_name, int module_number);

There are two different kinds of resource destruction handlers you can pass to this function: a handler for normal resources and a handler for persistent resources. Persistent resources are for example used for database connection. When registering a resource, either of these handlers must be given. For the other handler just pass NULL.

zend_register_list_destructors_ex() accepts the following parameters:

ld | Normal resource destruction handler callback
pld | Pesistent resource destruction handler callback
type_name | A string specifying the name of your resource. It's always a good thing to specify a unique name within PHP for the resource type so when the user for example calls var_dump($resource); he also gets the name of the resource.
module_number | The module_number is automatically available in your PHP_MINIT_FUNCTION function and therefore you just pass it over.
The return value is a unique integer ID for your resource type.

The resource destruction handler (either normal or persistent resources) has the following prototype:

void resource_destruction_handler(zend_rsrc_list_entry *rsrc TSRMLS_DC);
The passed rsrc is a pointer to the following structure:
typedef struct _zend_rsrc_list_entry {

    void *ptr;
    int type;
    int refcount;

} zend_rsrc_list_entry;
The member void *ptr is the actual pointer to your resource.

Now we know how to start things, we define our own resource we want register within Zend. It is only a simple structure with two integer members:

typedef struct {

    int resource_link;
    int resource_type;

} my_resource;
Our resource destruction handler is probably going to look something like this:
void my_destruction_handler(zend_rsrc_list_entry *rsrc TSRMLS_DC) {

    // You most likely cast the void pointer to your structure type

    my_resource *my_rsrc = (my_resource *) rsrc->ptr;

    // Now do whatever needs to be done with you resource. Closing
    // Files, Sockets, freeing additional memory, etc.
    // Also, don't forget to actually free the memory for your resource too!

    do_whatever_needs_to_be_done_with_the_resource(my_rsrc);
}

Note:

One important thing to mention: If your resource is a rather complex structure which also contains pointers to memory you allocated during runtime you have to free them before freeing the resource itself!

Now that we have defined

  1. what our resource is and

  2. our resource destruction handler

we can go on and do the rest of the steps:

  1. create a global variable within the extension holding the resource ID so it can be accessed from every function which needs it

  2. define the resource name

  3. write the resource destruction handler

  4. and finally register the handler

// Somewhere in your extension, define the variable for your registered resources.
    // If you wondered what 'le' stands for: it simply means 'list entry'.
    static int le_myresource;

    // It's nice to define your resource name somewhere
    #define le_myresource_name  "My type of resource"

    [...]

    // Now actually define our resource destruction handler
    void my_destruction_handler(zend_rsrc_list_entry *rsrc TSRMLS_DC) {

        my_resource *my_rsrc = (my_resource *) rsrc->ptr;
        do_whatever_needs_to_be_done_with_the_resource(my_rsrc);
    }

    [...]

    PHP_MINIT_FUNCTION(my_extension) {

        // Note that 'module_number' is already provided through the
        // PHP_MINIT_FUNCTION() function definition.

        le_myresource = zend_register_list_destructors_ex(my_destruction_handler, NULL, le_myresource_name, module_number);

        // You can register additional resources, initialize
        // your global vars, constants, whatever.
    }

To actually register a new resource you use can either use the zend_register_resource() function or the ZEND_REGISTER_RESOURE() macro, both defined in zend_list.h. Although the arguments for both map 1:1 it's a good idea to always use macros to be upwards compatible:

int ZEND_REGISTER_RESOURCE(zval *rsrc_result, void *rsrc_pointer, int rsrc_type);
rsrc_result | This is an already initialized zval * container.
rsrc_pointer | Your resource pointer you want to store.
rsrc_type | The type which you received when you registered the resource destruction handler. If you followed the naming scheme this would be le_myresource.
The return value is a unique integer identifier for that resource.

What is really going on when you register a new resource is it gets inserted in an internal list in Zend and the result is just stored in the given zval * container:

rsrc_id = zend_list_insert(rsrc_pointer, rsrc_type);

    if (rsrc_result) {
        rsrc_result->value.lval = rsrc_id;
        rsrc_result->type = IS_RESOURCE;
    }

    return rsrc_id;
The returned rsrc_id uniquely identifies the newly registered resource. You can use the macro RETURN_RESOURE to return it to the user:
    RETURN_RESOURCE(rsrc_id)

Note:

It is common practice that if you want to return the resource immediately to the user you specify the return_value as the zval * container.

Zend now keeps track of all references to this resource. As soon as all references to the resource are lost, the destructor that you previously registered for this resource is called. The nice thing about this setup is that you don't have to worry about memory leakages introduced by allocations in your module - just register all memory allocations that your calling script will refer to as resources. As soon as the script decides it doesn't need them anymore, Zend will find out and tell you.

Now that the user got his resource, at some point he is passing it back to one of your functions. The value.lval inside the zval * container contains the key to your resource and thus can be used to fetch the resource with the following macro: ZEND_FETCH_RESOURCE:

ZEND_FETCH_RESOURCE(rsrc, rsrc_type, rsrc_id, default_rsrc_id, resource_type_name, resource_type)
rsrc | This is your pointer which will point to your previously registered resource.
rsrc_type | This is the typecast argument for your pointer, e.g. myresource *.
rsrc_id | This is the address of the zval *container the user passed to your function, e.g. &z_resource if zval *z_resource is given.
default_rsrc_id | This integer specifies the default resource ID if no resource could be fetched or -1.
resource_type_name | This is the name of the requested resource. It's a string and is used when the resource can't be found or is invalid to form a meaningful error message.
resource_type | The resource_type you got back when registering the resource destruction handler. In our example this was le_myresource.
This macro has no return value. It is for the developers convenience and takes care of TSRMLS arguments passing and also does check if the resource could be fetched. It throws a warning message and returns the current PHP function with NULL if there was a problem retrieving the resource.

To force removal of a resource from the list, use the function zend_list_delete(). You can also force the reference count to increase if you know that you're creating another reference for a previously allocated value (for example, if you're automatically reusing a default database link). For this case, use the function zend_list_addref(). To search for previously allocated resource entries, use zend_list_find(). The complete API can be found in zend_list.h.

Macros for Automatic Global Variable Creation

In addition to the macros discussed earlier, a few macros allow easy creation of simple global variables. These are nice to know in case you want to introduce global flags, for example. This is somewhat bad practice, but Table Macros for Global Variable Creation describes macros that do exactly this task. They don't need any zval allocation; you simply have to supply a variable name and value.

Macros for Global Variable Creation
Macro | Description
SET_VAR_STRING(name, value) | Creates a new string.
SET_VAR_STRINGL(name, value, length) | Creates a new string of the specified length. This macro is faster than SET_VAR_STRING and also binary-safe.
SET_VAR_LONG(name, value) | Creates a new long.
SET_VAR_DOUBLE(name, value) | Creates a new double.

Creating Constants

Zend supports the creation of true constants (as opposed to regular variables). Constants are accessed without the typical dollar sign ($) prefix and are available in all scopes. Examples include TRUE and FALSE, to name just two.

To create your own constants, you can use the macros in Macros for Creating Constants. All the macros create a constant with the specified name and value.

You can also specify flags for each constant:

  • CONST_CS - This constant's name is to be treated as case sensitive.

  • CONST_PERSISTENT - This constant is persistent and won't be "forgotten" when the current process carrying this constant shuts down.

To use the flags, combine them using a inary OR:
 // register a new constant of type "long"
     REGISTER_LONG_CONSTANT("NEW_MEANINGFUL_CONSTANT", 324, CONST_CS |
     CONST_PERSISTENT);
There are two types of macros - REGISTER_*_CONSTANT andREGISTER_MAIN_*_CONSTANT. The first type creates constants bound to the current module. These constants are dumped from the symbol table as soon as the module that registered the constant is unloaded from memory. The second type creates constants that remain in the symbol table independently of the module.

Macros for Creating Constants
Macro | Description
REGISTER_LONG_CONSTANT(name, value, flags) REGISTER_MAIN_LONG_CONSTANT(name, value, flags) | Registers a new constant of type long.
REGISTER_DOUBLE_CONSTANT(name, value, flags) REGISTER_MAIN_DOUBLE_CONSTANT(name, value, flags) | Registers a new constant of type double.
REGISTER_STRING_CONSTANT(name, value, flags) REGISTER_MAIN_STRING_CONSTANT(name, value, flags) | Registers a new constant of type string. The specified string must reside in Zend's internal memory.
REGISTER_STRINGL_CONSTANT(name, value, length, flags) REGISTER_MAIN_STRINGL_CONSTANT(name, value, length, flags) | Registers a new constant of type string. The string length is explicitly set to length. The specified string must reside in Zend's internal memory.

Duplicating Variable Contents: The Copy Constructor

Sooner or later, you may need to assign the contents of one zval container to another. This is easier said than done, since the zval container doesn't contain only type information, but also references to places in Zend's internal data. For example, depending on their size, arrays and objects may be nested with lots of hash table entries. By assigning one zval to another, you avoid duplicating the hash table entries, using only a reference to them (at most).

To copy this complex kind of data, use the copy constructor. Copy constructors are typically defined in languages that support operator overloading, with the express purpose of copying complex types. If you define an object in such a language, you have the possibility of overloading the "=" operator, which is usually responsible for assigning the contents of the rvalue (result of the evaluation of the right side of the operator) to the lvalue (same for the left side).

Overloading means assigning a different meaning to this operator, and is usually used to assign a function call to an operator. Whenever this operator would be used on such an object in a program, this function would be called with the lvalue and rvalue as parameters. Equipped with that information, it can perform the operation it intends the "=" operator to have (usually an extended form of copying).

This same form of "extended copying" is also necessary for PHP's zval containers. Again, in the case of an array, this extended copying would imply re-creation of all hash table entries relating to this array. For strings, proper memory allocation would have to be assured, and so on.

Zend ships with such a function, called zend_copy_ctor() (the previous PHP equivalent was pval_copy_constructor()).

A most useful demonstration is a function that accepts a complex type as argument, modifies it, and then returns the argument:

zval *parameter;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &parameter) == FAILURE)
   return;
}

// do modifications to the parameter here

// now we want to return the modified container:
*return_value = *parameter;
zval_copy_ctor(return_value);

The first part of the function is plain-vanilla argument retrieval. After the (left out) modifications, however, it gets interesting: The container of parameter is assigned to the (predefined) return_value container. Now, in order to effectively duplicate its contents, the copy constructor is called. The copy constructor works directly with the supplied argument, and the standard return values are FAILURE on failure and SUCCESS on success.

If you omit the call to the copy constructor in this example, both parameter and return_value would point to the same internal data, meaning that return_value would be an illegal additional reference to the same data structures. Whenever changes occurred in the data that parameter points to, return_value might be affected. Thus, in order to create separate copies, the copy constructor must be used.

The copy constructor's counterpart in the Zend API, the destructor zval_dtor(), does the opposite of the constructor.

Returning Values

Returning values from your functions to PHP was described briefly in an earlier section; this section gives the details. Return values are passed via the return_value variable, which is passed to your functions as argument. The return_value argument consists of a zval container (see the earlier discussion of the call interface) that you can freely modify. The container itself is already allocated, so you don't have to run MAKE_STD_ZVAL on it. Instead, you can access its members directly.

To make returning values from functions easier and to prevent hassles with accessing the internal structures of the zval container, a set of predefined macros is available (as usual). These macros automatically set the correspondent type and value, as described in Predefined Macros for Returning Values from a Function and Predefined Macros for Setting the Return Value of a Function.

Note:

The macros in Predefined Macros for Returning Values from a Function automatically return from your function, those in Predefined Macros for Setting the Return Value of a Function only set the return value; they don't return from your function.

Predefined Macros for Returning Values from a Function
Macro | Description
RETURN_RESOURCE(resource) | Returns a resource.
RETURN_BOOL(bool) | Returns a Boolean.
RETURN_NULL() | Returns nothing (a NULL value).
RETURN_LONG(long) | Returns a long.
RETURN_DOUBLE(double) | Returns a double.
RETURN_STRING(string, duplicate) | Returns a string. The duplicate flag indicates whether the string should be duplicated using estrdup().
RETURN_STRINGL(string, length, duplicate) | Returns a string of the specified length; otherwise, behaves like RETURN_STRING. This macro is faster and binary-safe, however.
RETURN_EMPTY_STRING() | Returns an empty string.
RETURN_FALSE | Returns Boolean false.
RETURN_TRUE | Returns Boolean true.
Predefined Macros for Setting the Return Value of a Function
Macro | Description
RETVAL_RESOURCE(resource) | Sets the return value to the specified resource.
RETVAL_BOOL(bool) | Sets the return value to the specified Boolean value.
RETVAL_NULL | Sets the return value to NULL.
RETVAL_LONG(long) | Sets the return value to the specified long.
RETVAL_DOUBLE(double) | Sets the return value to the specified double.
RETVAL_STRING(string, duplicate) | Sets the return value to the specified string and duplicates it to Zend internal memory if desired (see also RETURN_STRING).
RETVAL_STRINGL(string, length, duplicate) | Sets the return value to the specified string and forces the length to become length (see also RETVAL_STRING). This macro is faster and binary-safe, and should be used whenever the string length is known.
RETVAL_EMPTY_STRING | Sets the return value to an empty string.
RETVAL_FALSE | Sets the return value to Boolean false.
RETVAL_TRUE | Sets the return value to Boolean true.

Complex types such as arrays and objects can be returned by using array_init() and object_init(), as well as the corresponding hash functions on return_value. Since these types cannot be constructed of trivial information, there are no predefined macros for them.

Printing Information

Often it's necessary to print messages to the output stream from your module, just as print would be used within a script. PHP offers functions for most generic tasks, such as printing warning messages, generating output for phpinfo(), and so on. The following sections provide more details. Examples of these functions can be found on the CD-ROM.

zend_printf()

zend_printf() works like the standard printf(), except that it prints to Zend's output stream.

zend_error()

zend_error() can be used to generate error messages. This function accepts two arguments; the first is the error type (see zend_errors.h), and the second is the error message.

zend_error(E_WARNING, "This function has been called with empty arguments");
Zend's Predefined Error Messages. shows a list of possible values (see below). These values are also referred to in php.ini. Depending on which error type you choose, your messages will be logged.
Zend's Predefined Error Messages.
Error | Description
E_ERROR | Signals an error and terminates execution of the script immediately.
E_WARNING | Signals a generic warning. Execution continues.
E_PARSE | Signals a parser error. Execution continues.
E_NOTICE | Signals a notice. Execution continues. Note that by default the display of this type of error messages is turned off in php.ini.
E_CORE_ERROR | Internal error by the core; shouldn't be used by user-written modules.
E_COMPILE_ERROR | Internal error by the compiler; shouldn't be used by user-written modules.
E_COMPILE_WARNING | Internal warning by the compiler; shouldn't be used by user-written modules.
Display of warning messages in the browser.

Including Output in phpinfo()

After creating a real module, you'll want to show information about the module in phpinfo() (in addition to the module name, which appears in the module list by default). PHP allows you to create your own section in the phpinfo() output with the ZEND_MINFO() function. This function should be placed in the module descriptor block (discussed earlier) and is always called whenever a script calls phpinfo().

PHP automatically prints a section in phpinfo() for you if you specify the ZEND_MINFO function, including the module name in the heading. Everything else must be formatted and printed by you.

Typically, you can print an HTML table header using php_info_print_table_start() and then use the standard functions php_info_print_table_header() and php_info_print_table_row(). As arguments, both take the number of columns (as integers) and the column contents (as strings). Source code and screenshot for output in phpinfo. shows a source example and its output. To print the table footer, use php_info_print_table_end().

Example #13 Source code and screenshot for output in phpinfo().

php_info_print_table_start();
php_info_print_table_header(2, "First column", "Second column");
php_info_print_table_row(2, "Entry in first row", "Another entry");
php_info_print_table_row(2, "Just to fill", "another row here");
php_info_print_table_end();
Output of phpinfo()

Execution Information

You can also print execution information, such as the current file being executed. The name of the function currently being executed can be retrieved using the function get_active_function_name(). This function returns a pointer to the function name and doesn't accept any arguments. To retrieve the name of the file currently being executed, use zend_get_executed_filename(). This function accesses the executor globals, which are passed to it using the TSRMLS_C macro. The executor globals are automatically available to every function that's called directly by Zend (they're part of the INTERNAL_FUNCTION_PARAMETERS described earlier in this chapter). If you want to access the executor globals in another function that doesn't have them available automatically, call the macro TSRMLS_FETCH() once in that function; this will introduce them to your local scope.

Finally, the line number currently being executed can be retrieved using the function zend_get_executed_lineno(). This function also requires the executor globals as arguments. For examples of these functions, see Printing execution information..

Example #14 Printing execution information.

zend_printf("The name of the current function is %s&lt;br&gt;", get_active_function_name(TSRMLS_C));
zend_printf("The file currently executed is %s&lt;br&gt;", zend_get_executed_filename(TSRMLS_C));
zend_printf("The current line being executed is %i&lt;br&gt;", zend_get_executed_lineno(TSRMLS_C));
Printing execution information

Startup and Shutdown Functions

Startup and shutdown functions can be used for one-time initialization and deinitialization of your modules. As discussed earlier in this chapter (see the description of the Zend module descriptor block), there are module, and request startup and shutdown events.

The module startup and shutdown functions are called whenever a module is loaded and needs initialization; the request startup and shutdown functions are called every time a request is processed (meaning that a file is being executed).

For dynamic extensions, module and request startup/shutdown events happen at the same time.

Declaration and implementation of these functions can be done with macros; see the earlier section "Declaration of the Zend Module Block" for details.

Calling User Functions

You can call user functions from your own modules, which is very handy when implementing callbacks; for example, for array walking, searching, or simply for event-based programs.

User functions can be called with the function call_user_function_ex(). It requires a hash value for the function table you want to access, a pointer to an object (if you want to call a method), the function name, return value, number of arguments, argument array, and a flag indicating whether you want to perform zval separation.

ZEND_API int call_user_function_ex(HashTable *function_table, zval *object,
zval *function_name, zval **retval_ptr_ptr,
int param_count, zval **params[],
int no_separation);

Note that you don't have to specify both function_table and object; either will do. If you want to call a method, you have to supply the object that contains this method, in which case call_user_function()automatically sets the function table to this object's function table. Otherwise, you only need to specify function_table and can set object to NULL.

Usually, the default function table is the "root" function table containing all function entries. This function table is part of the compiler globals and can be accessed using the macro CG. To introduce the compiler globals to your function, call the macro TSRMLS_FETCH once.

The function name is specified in a zval container. This might be a bit surprising at first, but is quite a logical step, since most of the time you'll accept function names as parameters from calling functions within your script, which in turn are contained in zval containers again. Thus, you only have to pass your arguments through to this function. This zval must be of type IS_STRING.

The next argument consists of a pointer to the return value. You don't have to allocate memory for this container; the function will do so by itself. However, you have to destroy this container (using zval_dtor()) afterward!

Next is the parameter count as integer and an array containing all necessary parameters. The last argument specifies whether the function should perform zval separation - this should always be set to 0. If set to 1, the function consumes less memory but fails if any of the parameters need separation.

Calling user functions. shows a small demonstration of calling a user function. The code calls a function that's supplied to it as argument and directly passes this function's return value through as its own return value. Note the use of the constructor and destructor calls at the end - it might not be necessary to do it this way here (since they should be separate values, the assignment might be safe), but this is bulletproof.

Example #15 Calling user functions.

zval **function_name;
zval *retval;

if((ZEND_NUM_ARGS() != 1) || (zend_get_parameters_ex(1, &function_name) != SUCCESS))
{
    WRONG_PARAM_COUNT;
}

if((*function_name)->type != IS_STRING)
{
    zend_error(E_ERROR, "Function requires string argument");
}

TSRMSLS_FETCH();

if(call_user_function_ex(CG(function_table), NULL, *function_name, &retval, 0, NULL, 0) != SUCCESS)
{
    zend_error(E_ERROR, "Function call failed");
}

zend_printf("We have %i as type\n", retval->type);

*return_value = *retval;
zval_copy_ctor(return_value);
zval_ptr_dtor(&retval);

<?php

dl("call_userland.so");

function test_function()
{
    echo "We are in the test function!\n";
    return 'hello';
}

$return_value = call_userland("test_function");

echo "Return value: '$return_value'";
?>

위 예제의 출력:

We are in the test function!
We have 3 as type
Return value: 'hello'

Initialization File Support

PHP 4 features a redesigned initialization file support. It's now possible to specify default initialization entries directly in your code, read and change these values at runtime, and create message handlers for change notifications.

To create an .ini section in your own module, use the macros PHP_INI_BEGIN() to mark the beginning of such a section and PHP_INI_END() to mark its end. In between you can use PHP_INI_ENTRY() to create entries.

PHP_INI_BEGIN()
PHP_INI_ENTRY("first_ini_entry",  "has_string_value", PHP_INI_ALL, NULL)
PHP_INI_ENTRY("second_ini_entry", "2",                PHP_INI_SYSTEM, OnChangeSecond)
PHP_INI_ENTRY("third_ini_entry",  "xyz",              PHP_INI_USER, NULL)
PHP_INI_END()
The PHP_INI_ENTRY() macro accepts four parameters: the entry name, the entry value, its change permissions, and a pointer to a change-notification handler. Both entry name and value must be specified as strings, regardless of whether they really are strings or integers.

The permissions are grouped into three sections:PHP_INI_SYSTEM allows a change only directly in the php.ini file; PHP_INI_USER allows a change to be overridden by a user at runtime using additional configuration files, such as .htaccess; and PHP_INI_ALL allows changes to be made without restrictions. There's also a fourth level, PHP_INI_PERDIR, for which we couldn't verify its behavior yet.

The fourth parameter consists of a pointer to a change-notification handler. Whenever one of these initialization entries is changed, this handler is called. Such a handler can be declared using the PHP_INI_MH macro:

PHP_INI_MH(OnChangeSecond);             // handler for ini-entry "second_ini_entry"

// specify ini-entries here

PHP_INI_MH(OnChangeSecond)
{

    zend_printf("Message caught, our ini entry has been changed to %s&lt;br&gt;", new_value);

    return(SUCCESS);

}
The new value is given to the change handler as string in the variable new_value. When looking at the definition of PHP_INI_MH, you actually have a few parameters to use:
#define PHP_INI_MH(name) int name(php_ini_entry *entry, char *new_value,
                                  uint new_value_length, void *mh_arg1,
                                  void *mh_arg2, void *mh_arg3)
All these definitions can be found in php_ini.h. Your message handler will have access to a structure that contains the full entry, the new value, its length, and three optional arguments. These optional arguments can be specified with the additional macros PHP_INI_ENTRY1 (allowing one additional argument), PHP_INI_ENTRY2 (allowing two additional arguments), and PHP_INI_ENTRY3 (allowing three additional arguments).

The change-notification handlers should be used to cache initialization entries locally for faster access or to perform certain tasks that are required if a value changes. For example, if a constant connection to a certain host is required by a module and someone changes the hostname, automatically terminate the old connection and attempt a new one.

Access to initialization entries can also be handled with the macros shown in Macros to Access Initialization Entries in PHP.

Macros to Access Initialization Entries in PHP
Macro | Description
INI_INT(name) | Returns the current value of entry name as integer (long).
INI_FLT(name) | Returns the current value of entry name as float (double).
INI_STR(name) | Returns the current value of entry name as string. Note: This string is not duplicated, but instead points to internal data. Further access requires duplication to local memory.
INI_BOOL(name) | Returns the current value of entry name as Boolean (defined as zend_bool, which currently means unsigned char).
INI_ORIG_INT(name) | Returns the original value of entry name as integer (long).
INI_ORIG_FLT(name) | Returns the original value of entry name as float (double).
INI_ORIG_STR(name) | Returns the original value of entry name as string. Note: This string is not duplicated, but instead points to internal data. Further access requires duplication to local memory.
INI_ORIG_BOOL(name) | Returns the original value of entry name as Boolean (defined as zend_bool, which currently means unsigned char).

Finally, you have to introduce your initialization entries to PHP. This can be done in the module startup and shutdown functions, using the macros REGISTER_INI_ENTRIES() and UNREGISTER_INI_ENTRIES():

ZEND_MINIT_FUNCTION(mymodule)
{

    REGISTER_INI_ENTRIES();

}

ZEND_MSHUTDOWN_FUNCTION(mymodule)
{

    UNREGISTER_INI_ENTRIES();

}

Where to Go from Here

You've learned a lot about PHP. You now know how to create dynamic loadable modules and statically linked extensions. You've learned how PHP and Zend deal with internal storage of variables and how you can create and access these variables. You know quite a set of tool functions that do a lot of routine tasks such as printing informational texts, automatically introducing variables to the symbol table, and so on.

Even though this chapter often had a mostly "referential" character, we hope that it gave you insight on how to start writing your own extensions. For the sake of space, we had to leave out a lot; we suggest that you take the time to study the header files and some modules (especially the ones in the ext/standard directory and the MySQL module, as these implement commonly known functionality). This will give you an idea of how other people have used the API functions - particularly those that didn't make it into this chapter.

Reference: Some Configuration Macros

config.m4

The file config.m4 is processed by buildconf and must contain all the instructions to be executed during configuration. For example, these can include tests for required external files, such as header files, libraries, and so on. PHP defines a set of macros that can be used in this process, the most useful of which are described in M4 Macros for config.m4.

M4 Macros for config.m4
Macro | Description
AC_MSG_CHECKING(message) | Prints a "checking <message>" text during configure.
AC_MSG_RESULT(value) | Gives the result to AC_MSG_CHECKING; should specify either yes or no as value.
AC_MSG_ERROR(message) | Prints message as error message during configure and aborts the script.
AC_DEFINE(name,value,description) | Adds #define to php_config.h with the value of value and a comment that says description (this is useful for conditional compilation of your module).
AC_ADD_INCLUDE(path) | Adds a compiler include path; for example, used if the module needs to add search paths for header files.
AC_ADD_LIBRARY_WITH_PATH(libraryname,librarypath) | Specifies an additional library to link.
AC_ARG_WITH(modulename,description,unconditionaltest,conditionaltest) | Quite a powerful macro, adding the module with description to the configure --help output. PHP checks whether the option --with-<modulename> is given to the configure script. If so, it runs the script unconditionaltest (for example, --with-myext=yes), in which case the value of the option is contained in the variable $withval. Otherwise, it executes conditionaltest.
PHP_EXTENSION(modulename, [shared]) | This macro is a must to call for PHP to configure your extension. You can supply a second argument in addition to your module name, indicating whether you intend compilation as a shared module. This will result in a definition at compile time for your source as COMPILE_DL_<modulename>.

API Macros

A set of macros was introduced into Zend's API that simplify access to zval containers (see API Macros for Accessing zval Containers).

API Macros for Accessing zval Containers
Macro | Refers to
Z_LVAL(zval) | (zval).value.lval
Z_DVAL(zval) | (zval).value.dval
Z_STRVAL(zval) | (zval).value.str.val
Z_STRLEN(zval) | (zval).value.str.len
Z_ARRVAL(zval) | (zval).value.ht
Z_LVAL_P(zval) | (*zval).value.lval
Z_DVAL_P(zval) | (*zval).value.dval
Z_STRVAL_P(zval_p) | (*zval).value.str.val
Z_STRLEN_P(zval_p) | (*zval).value.str.len
Z_ARRVAL_P(zval_p) | (*zval).value.ht
Z_LVAL_PP(zval_pp) | (**zval).value.lval
Z_DVAL_PP(zval_pp) | (**zval).value.dval
Z_STRVAL_PP(zval_pp) | (**zval).value.str.val
Z_STRLEN_PP(zval_pp) | (**zval).value.str.len
Z_ARRVAL_PP(zval_pp) | (**zval).value.ht

User Contributed Notes

bhavacakra at gmail dot com 10-Mar-2007 10:41
A very nice resource http://devzone.zend.com/public/view/tag/Extension
vijay at planetbazaar dot com 17-Sep-2005 09:53
Notes for Windows PHP5 extension developers:

First, two great articles at http://www.zend.com/php/internals/index.php are in the must read category. Unfortunately an article on building extensions on Windows is yet to arrive at this time. So one gets stuck at section 'Building Your Extension' in the first article - because there is no 'phpize' for Windows to the best of my knowledge.

First point of curiosity is how the extensions that are available for Windows appear in the output of "cscript /nologo configure.js --help". Turns out the magic happens by executing "buildconf.bat". To cut a long story short, it turns out that for Windows, the build system looks for config.w32 in each extension directory instead of config.m4; config.w32 is actually a javascript (from what I gather reading the "vim:ft=javascript" comment at the top of the files).

To get going with the "hello world" sample in the article mentioned above here is the config.w32 to replace config.m4;

--- begin file: config.w32 ---
// hello world module configuration
// vim:ft=javascript
ARG_ENABLE("hello", "hello Enable Hello World support", "no");

if (PHP_HELLO == "yes") {
  EXTENSION("hello", "hello.c", null, "-Iext/hello");
  AC_DEFINE('HAVE_HELLO', 1, 'Whether you ave Hello World');
}

--- end file: config.w32 ---

And just to compare, here is the config.m4

--- begin file: config.m4 ---
dnl vim:ft=m4
dnl hello world module configuration
PHP_ARG_ENABLE(hello, whether to enable Hellow World support,
[ --enable-hello  Enable Hello World support])

if test "$PHP_HELLO" = "yes"; then
  AC_DEFINE(HAVE_HELLO, 1 [Whether you have Hello World])
  PHP_NEW_EXTENSION(hello, hello.c, $ext_shared)
fi

--- end file: config.m4 ---

And finally the key step is to execute 'buildconf.bat'; after this step:

  cscript /nologo configure.js | find "hello"

should show something like:
"--enable-hello     hello Enable Hello World support"
tony dot dodd at btinternet dot com 20-Aug-2005 05:15
These macros are defined in a separate header file called php_ini.h.
josephmdaly at gmail dot com 29-Jul-2005 10:58
It looks like PHP 5 uses a newer version of the windows socket library.

If your extension links to a library that uses the old version you might get a lot of errors about redefining things.

To fix this problem make sure the PHP header files are included before the library?s header files.
josephmdaly at gmail dot com 01-Jul-2005 12:26
Here is a quick example of how to read arrays passed as parameters:

int i;
/* data will be used to store one element on each iteration of a loop */
zval **data = NULL;
/* data_array is the array we are copying values into */
char **data_array = NULL;

/* Use zend_parse_parameters as you normally would with an 'a' in the type
   string.
   In this example it sets the variables data_arg and array_length
*/

data_array = emalloc(sizeof(char *) * array_length);

for (i = 0; i < array_length; i++) {
  /* Get an element out of the hash table as a zval */
  zend_hash_index_find(Z_ARRVAL_P(data_arg), i, (void **)&data);

  /* Make sure the zval is a string */
  convert_to_string(*data);

  /* Convert the zval into a string and store it */
  data_array[i] = Z_STRVAL_PP(data);
}
seymour_levine at hotmail dot com 14-Jun-2005 02:18
A small amount of administration is needed to make this work.

Mainly the dl command does not pick up the module created.

To remedy, use a full path for the extension_dir parameter in php.ini. Then create that subdirectory mod 755 vis:

        extension_dir=/usr/lib/php411

And create php411. The name is arbitrary.

In the php dl command, specify dl('firstmod.so'); do not use any slashes.
Sy Levine 13-Jun-2005 02:29
I installed and run 4.1.1 on RH 7.1  It runs on a separate Apache Server than the one furnished by Red Hat.

A few adminisrative changes were necessary to get  the function going.

The main one was finding and modifying php.ini.

The command: find . -print | grep php.ini should do that.
Mine was in /usr/local/lib

Look for extension_dir in php.ini then type the directive as

extension_dir=/usr/lib/php4

Use no trailing / in the directive. This is where the dl('firstmod.so') php statement accesses your module.

Tip: I do not use a leading / like dl('/firstmod.so') which will be misparsed.  {Note by Ed a PHP ...}

The source module is named firstmod.c.

I compiled and created the firstmod.so with a script based on the various notes about the compile parameters. See
markjolesen, ywarnier, 17-Apr, wish I .
davide dot chiodi at poste dot it 07-Apr-2005 07:34
to build with lcc-win32 compiler:

change #include "php.h" to #include "php-lcc.h"
this is php-lcc.h (copied from excellent windbinder project by Rubem Pechansky)

//ZEND constants
#if defined(_MSC_VER)        // Rick
#    if defined(ZEND_EXPORTS)
#        undef ZEND_EXPORTS
#    endif
#    undef ZEND_DEBUG
#else
#    define PHP_WIN32
#    define ZEND_WIN32
#endif

#define ZEND_DEBUG    FALSE    // Must be FALSE
#define ZTS                    // Must define ZTS (Zend Thread Safety)
#define PHP_REGEX_H            // Skip php_regex.h: '_off_t' not defined anywhere
#define COMPILE_DL_FIRST_MODULE 1
//----------------------------------------------------------------- DEPENDENCIES

#include <php.h>

//---------------------------------------------------------- AUXILIARY FUNCTIONS

int parse_array(zval *array, const char *fmt, ...);

//------------------------------------------------------------------ END OF FILE

you must also used tweaked headers file from winbinder author at this link: http://hypervisual.com/winbinder/files/archived/php4_tweaked_headers.zip
(php 4).
I used php 4.3.10 and all worked fine
ywarnier at beeznest dot org 25-Oct-2004 01:19
Quick note to save time to beginners

To make the example above work, I had to add -I../TSRM because the file tsrm_virtual_cwd.h couldn't be found otherwise (it's located in the TSRM directory which is not included above)

In my case (pure Debian Woody install), php libraries were located in /usr/include/php4 (and php.h was in the 'main' subdirectory) so I had to use this path instead of /usr/local/include
ppmm at wuxinan dot net 20-Jul-2004 02:40
In fact, empty string is not writeable, even when we add an slash after the type decl. Say the implementation of my_test() is like this:

char *s;
int s_len;
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s/", &s, &s_len)

and you pass an empty string my_test("") from the PHP script, then s[0] is not writeable. You will get a seg fault. The reason is that Zend doesn't emalloc a new copy of the empty string dynamically, instead it just assigns the empty_string (which is defined at compile time and located in unwriteable data seg) pointer to s.
someone at apple 02-May-2004 07:55
When following the directions for compiling via make, it may be necessary to delete the configure script before running buildconf.  Otherwise you may get a complaint about buggy cache code and the configure script won't actually be updated.
26-Apr-2004 07:13
If you want to free zval, you can do this:

{
zval *new_var;

/* allocate and initialize new container */
MAKE_STD_ZVAL(new_var);

/* set type and variable contents here, see the following sections */

ZVAL_STRING(new_var, "123456", 1);

/* introduce this variable by the name "new_variable_name" into the symbol table */
zend_hash_update(
    EG(active_symbol_table),
    "new_var",
    strlen("new_var") + 1,
    &new_var,
    sizeof(zval *),
    NULL
);

zend_hash_del(
    EG(active_symbol_table),
    "new_var",
    strlen("new_var") + 1);

FREE_ZVAL(new_var);
}
php-lists at nomeaning dot net 24-Apr-2004 09:36
phpmanual at oderwat dot de is incorrect:

TSRMLS_DC and TSRMLS_CC are both correct as listed in the original text.

TSRMLS_DC is used for Zend internal function declarations and TSRMLS_CC must be used when calling such functions.
Julien CROUZET jucrouzet_at_cpan.org 07-Apr-2004 11:51
Remember that even if RETURN_* macros sets the return_value to return variable to user, they also RETURN FROM YOU CODE by a "return ;", any code after these macro will be ignored.

If you want to place code after a return of variable (which is not a good idea for code readability), you'll have to use RETVAL_* macro instead.
Julien CROUZET jucrouzet_at_cpan.org 07-Apr-2004 11:44
Just an example of the way of the way of returning array :

/*
** Creates a array of on element (a long of value 42)
*/
ZEND_FUNCTION("make_42_array")
{
  zval *my_long;

  // Declaring and allocating return_value to be an array
  array_init(return_value);

  //Creates and assign the long element at (long)42
  MAKE_STD_ZVAL(my_long);
  ZVAL_LONG(new_element, 42);

  //Make this our $array[0] I do not use add_index_long to
  //to show a "by-hand" assignation
  zend_hash_index_update(HASH_OF(return_value), 0, (void *)&my_long, sizeof(zval *), NULL);

  //Returns nothing to satisfy the void prototype
  return ;

}
Julien CROUZET jucrouzet_at_cpan.org 06-Apr-2004 12:28
All associate array functions (add_assoc_long for example) are now just aliases of their extended version (add_assoc_long_ex).

The key lenght is calculated with strlen(key) + 1, but remember that this is NOT binary safe (strlen stops on the first null byte).

Users may want to use binary keys, so always prefer extended versions with a secure length !
jason dot lambert at uk dot clara dot net 31-Mar-2004 02:18
Just a note of caution, when using a string value within zend_parse_parameters() you MUST specify the length parameter also, or php may segfault on some machines.

Example possible segfault code:

char *s;
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &s)

Correct code:

char *s;
int s_len;
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &s, &s_len)
Julien CROUZET jucrouzet_at_cpan.org 24-Mar-2004 11:44
As a completion to k at ailis dot de :

According to ARG_SHOULD_BE_SENT_BY_REF macro in zend_compile.h, BYREF_FORCE_REST force the rest of the arguments to be passed as ref.

The first byte have to be the index of BYREF_FORCE_REST in the array.

Means, if you have, 5 args, the first two ones are NOT passed as ref, and the 3 others are, you can do :

static unsigned char rest_arguments_force_ref[] = { 3, BYREF_NONE, BYREF_NONE, BYREF_FORCE_REST };

We can see a few things in this example :

  * As k at ailis dot de said, the first byte is _NOT_ the number of args passed to the function.
  * The argument corresponding to BYREF_FORCE_REST MUST be passed as a reference
  * The others arguments MUST be passed as a reference
glamm at a-s-i dot com 07-Aug-2003 08:14
Regarding the note written by jo-shi at web dot de:

I do not think that the line
  ce = emalloc(sizeof(zend_class_entry));
is required, as the line
  ce = zend_register_internal_class(&class_entry TSRMLS_CC);
actually appears to go through the process of performing the memory allocation and reference counting of the object.
wilf at bigpond dot net dot au 15-Jun-2003 09:26
Another caching problem:

If you seen a message saying that 'may contain buggy cache code', ensure that you delete the 'autom4k...cache' directory before running buildconf.

A good test the see if your new module has been picked up by buildconf is:

$ grep -c my_module configure
(if that returns 0 then buildconf did not work).

Unfortunately this means that you have to completely rebuild your configure script each time you change an M4 file but it doesnt take too long.
wendywds at hotmail dot com 12-May-2003 12:39
Using php-4.3.1 on Redhat Linux 8, step 6 did not work as written.  There is no php executable in /usr/php-4.3.1.  Instead, with help from the pear-general mailing list, I discovered that the newly built php executable is in /usr/php-4.3.1/sapi/cli.  (The very end of the output of 'make' shows -o sapi/cli/php.  According to Johannes on pear-general, it might be in sapi/cgi instead.)

So I had to change step 6 to:
6.  $ sapi/cli/php -f ext/my_module/my_module.php
Ed a PHP enthusiast from NYC 28-Apr-2003 07:55
Thank you very much for the comments above!

Here is a distillation of how to compile your module as a dynamic library, placing it in your web server and loading it in your test .php file being served (PHP 4.3.1).

*** Compiling a module as a .so

$ cd ext/mystuff

$ cc -fpic -DCOMPILE_DL_MYSTUFF=1 -I/usr/local/include -I../../TSRM -I../.. -I../../main -I../../Zend -I../../ext/bcmath/libbcmath/src -c -o mystuff.o mystuff.c

$ cc -shared -L/usr/local/lib -rdynamic -o mystuff.so mystuff.o

*** Move it to the web server (doc root is /www)

$ cp mystuff.so /www/.

$ chmod 644 /www/mystuff.so

*** load the module in the .php file for testing purposes

<?
dl('/mystuff.so');

/*  your php code here  */
?>

The other thing that threw me off my tracks is that when dealing with strings you can allocate memory with emalloc and return the result with the RETURN_STRING macro (duplicate set to 0) and presumably PHP will take care of the efree -- or else I got it wrong and have plenty of leaks  ;-)
17-Apr-2003 08:08
Compiling Manually NOTE:
remenber add -DHAVE_CONFIG_H flag
otherwise you .so can't be ld. then you cp it to php modules directory.
i.e
Compiling cc -fpic -DHAVE_CONFIG_H  -I/usr/local/include -I. -I.. -I../Zend -c -o <your_object_file> <your_c_file>
19-Mar-2003 08:55
To built the modules shipped with the source just cd into the ext dir for example

cd ext/oci8

and type

export SHLIB_SUFFIX_NAME=so
phpize && aclocal && ./configure && make

now you have a subdir called "modules" with the .so file. Copy it to your extension dir and set everything up in php.ini.

This way you can add extensions to binary distributions like RPMS.
staffan dot palmroos at surfbuy dot se 07-Mar-2003 07:30
The specified_type_or_NULL operator ! in the type specifier string of zend_parse_parameters seems to work for strings too, in addition to (a, o, O, r, and z) as noted.
jo-shi at web dot de 03-Dec-2002 07:54
add_property_[*](*) overwrites existing values in your object:

ex:
 var_dump($myobj);
 object(objectname)(1){
   ["_myvalue"]=>int(23);
 }
 so in func1 i "add" a property again:
 PHP_FUNCTION(func1){
   add_property_long(getThis(),"_myvalue",32);
 }
 should (as expected) result
 object(objectname)(1){
   ["_myvalue"]=>int(32);
 }
/* annotation: getThis() is a function used by many extensions to find out the $this value in an object */
jo-shi at web dot de 03-Dec-2002 07:47
you can add functions to your object:
you may declare global
  static zend_function_entry
    php_my_class_functions[] = {
      ZEND_FE (func1, NULL)
      ZEND_FE (func2, NULL)
      {NULL, NULL, NULL}
    };

you may declare local
  zend_class_entry my_class_entry;
  zend_class_entry *ce;

you should do something like this
  INIT_CLASS_ENTRY(
    func_class_entry,
    "CLASSNAME",
    php_my_class_functions);
  ce = emalloc(sizeof(zend_class_entry));
  ce = zend_register_internal_class (
  &my_class_entry TSRMLS_CC);
  object_init_ex(return_value, ce);
this should produce a returned object in your php_code:
$myobj = yourfunc(/*yourparams*/);
$myobj->func1();
$myobj->func2();
k at ailis dot de 06-Apr-2002 12:56
If you are using one of the above macros you definitely want to use one of those convert-functions if you are not sure what data type is stored in the zval:

  convert_to_string(zval_p)
  convert_to_long(zval_p)
  convert_to_double(zval_p)
  convert_to_boolean(zval_p)
  convert_to_array(zval_p)
  convert_to_object(zval_p)

Otherwise you may get undefined results, because Z_STRVAL_P(zval_p) returns nonsense if the zval contains not a string. If you use convert_to_string() before, the value is converted to a string and you can safely access it via the Z_STRVAL* macros.

There are more useful convert-functions:

Resets the zval to NULL:
  convert_to_null(zval_p)

Converts to a long by using a special base:
  convert_to_long_base(zval_p, base)

There are some more functions which may be useful, but I don't know exactly what they are doing. Just take a look at Zend/zend_operaters.h.
k at ailis dot de 06-Apr-2002 12:41
To complete the macro list:

Access an object:
  Z_OBJ(zval)
  Z_OBJ_P(zval)
  Z_OBJ_PP(zval)

Access object property hash table:
  Z_OBJPROP(zval)
  Z_OBJPROP_P(zval)
  Z_OBJPROP_PP(zval)

Access object class entry:
  Z_OBJCE(zval)
  Z_OBJCE_P(zval)
  Z_OBJCE_PP(zval)

Access a ressource (which is fairly the same as Z_LVAL*):
  Z_RESVAL(zval)
  Z_RESVAL_P(zval)
  Z_RESVAL_PP(zval)
k at ailis dot de 06-Apr-2002 12:33
These macros can be used to access the boolean value of a zval. Yes, you can use Z_LVAL* for booleans, too, but these macros also do a type cast to zend_bool:

Z_BVAL(zval)
Z_BVAL_P(zval)
Z_BVAL_PP(zval)
k at ailis dot de 28-Mar-2002 02:57
Because it is not explained how the arg_types parameter works, I'll try to explain it myself: The arg_types parameter is an array of bytes. The first byte marks how many arg_type-specifiers follows. There are the following macros which can be used to regulate the reference forcing and I interprete them like this (No guarentee):

BYREF_NONE = Force passing by value
BYREF_FORCE = Force passing by reference
BYREF_ALLOW = Allow passing by reference
BYREF_FORCE_REST = ???

Commonly the arg_types parameter is used like this:

static unsigned char third_argument_force_ref[] = { 3, BYREF_NONE, BYREF_NONE, BYREF_FORCE };
ZEND_FE(foobar, third_argument_force_ref)

Note that the first element in the array does NOT specify how many arguments the function is expecting, only how many arg_type-Specifiers follows. Checking of the number of arguments has to be done in the function body itself (i.E. by using the zend_parse_parameters() function)
k at ailis dot de 28-Mar-2002 02:43
An information that is missing here: The function name parameter can be an array with two elements. The first element is an object reference or a class name and the second element is the method name. Zend recognizes if the parameter is a string (which results in a normal function call or a normal method call if the object parameter is used) or if it is an array.
john dot gallet at wanadoo dot fr 26-Mar-2002 08:58
Concerning all add_*_stringl() functions :
they do not behave like snprintf() for instance, i.e. the length of the string is not a *maximum length* but an *exact* length, and you end up with null '\000' or '\@'  caracters in your PHP string if the length parameter is greater than the actual length of the string to be associated with return_value.

Yes, it is clearly written but still a trick if you are used to strncpy() or snprintf().
HTH
k at ailis dot de 18-Mar-2002 05:32
object_init() and object_init_ex() are not calling the constructor of a class. You have to do this by yourself after creating the object.
k at ailis dot de 17-Mar-2002 03:48
There are also the following functions to add zvals to an array or hash:

ZEND_API int add_index_zval(zval *arg, uint index, zval *value);
ZEND_API int add_next_index_zval(zval *arg, zval *value);
ZEND_API int add_assoc_zval_ex(zval *arg, char *key, uint key_len, zval *value)
add_assoc_zval(__arg, __key, __value)
john dot gallet at wanadoo dot fr 05-Jan-2002 01:39
Distinction between --with and --enable : the "something external" means for exemple a shared object. If you are writing a C wrapper for an existing .so library, you should use --with

Do not forget the "cache" effect to modifyng M4 files. When you are testing and getting erros, be sure to delete config.cache before re-running buildconf and ./configure (I spent an hour looking for an error that was not there any more because of that).

HTH and happy developpment.


TSRM API

User Contributed Notes

There are no user contributed notes for this page.


FAQ: 자주 물어보는 질문들

  • 일반적인 정보
  • 메일링리스트
  • PHP 구하기
  • 데이터베이스와 관련된 문제
  • 설치
  • 빌드 시 발생할수 있는 문제 — 빌드 시 문제
  • PHP 사용하기
  • Password Hashing — Safe Password Hashing
  • PHP와 HTML
  • PHP와 COM
  • PHP 4에서 PHP 5로 이행하기
  • 기타 질문

User Contributed Notes

There are no user contributed notes for this page.


일반적인 정보

이 섹션은 PHP에 관한 일반적인 정보를 다룬다: PHP가 무엇이고 무엇을 하는지에 관해.

  1. PHP는 무엇입니까?
  2. PHP라는 약자는 무엇을 의미합니까?
  3. 각 버전 사이의 관계를 알고 싶습니다
  4. 동시에 여러 버전의 PHP를 사용할수 있습니까?
  5. PHP 3와 PHP 4는 무엇이 다릅니까?
  6. PHP 4와 PHP 5의 차이점은 무엇입니까?
  7. 버그를 발견한것 같아요! 누구에게 이 사실을 알려줘야 하나요?
PHP는 무엇입니까?

preface of the manual을 참고하면:

PHP는 HTML 내장 스크립트 언어입니다. 문법의 대부분이 C, Java, Perl 로부터 차용되었으며 PHP고유의 특성도 함께 내포되어있습니다. 이 언어의 목적은 웹개발자에게 동적으로 생성되는 페이지를 빨리 만들수 있도록 하는 데 있습니다.

PHP라는 약자는 무엇을 의미합니까?

PHP약어를 풀어쓰면 PHP: Hypertext Preprocessor입니다. 약어의 첫번째 글자가 약어이기 때문에 많은 사람에게 혼란을 줍니다. 이와 같은 약어를 재귀적 약어라고 부릅니다. 궁금하신 분은 » Free On-Line Dictionary of Computing이나 » Wikipedia 재귀 약어 엔트리를 확인해보세요.

각 버전 사이의 관계를 알고 싶습니다

PHP/FI 2.0는 PHP의 초기 버전이며 더이상 지원되지 않습니다. PHP 3는 PHP/FI 2.0의 다음 버전으로서 좀 더 나아진 단계입니다. PHP 5는 추가적인 OOP 특성과, 다른 많은 기능들을 제공하는 » 젠드 엔진 2를 사용합니다.

동시에 여러 버전의 PHP를 사용할수 있습니까?

네, PHP 4 소스 디렉토리 밑의 INSTALL문서를 참고하세요.

PHP 3와 PHP 4는 무엇이 다릅니까?

중요한 몇가지 새 기능의 목록입니다:

  • 확장된 API 모듈
  • UNIX 상의 통일된 빌드 과정
  • 멀티-쓰레드 웹서버를 지원하는 통일된 웹서버 인터페이스
  • 향상된 문법 하이라이터(highlighter)
  • 고유의 HTTP 세션 지원
  • 출력 버퍼링 지원
  • 좀 더 강화된 설정 시스템
  • 레퍼런스 카운팅(Reference counting)
이 특정은 물론 다른 특성에 대해 더 자세히 알고 싶다면 » What's new in PHP 4 overview문서를 참고하세요.

PHP 4와 PHP 5의 차이점은 무엇입니까?

PHP 5는 이전 버전과 가능한 호환되도록 설계되었습니다만, 몇몇 중요한 변경점이 있습니다. 이러한 차이점은 다음을 포함합니다:

  • 젠드 엔진 2.0 기반의 새 OOP 모델
  • 향상된 MySQL 지원을 위한 새 확장
  • SQLite의 내장 지원
  • 실행시 코드 권장을 위한 새 오류 보고 상수 E_STRICT
  • 간단한 코드 작성을 위한 많은 새 함수들 (많은 일반 처리에 대해서 코드 작성을 줄임)
보다 자세한 정보는 PHP 4에서 PHP 5로 이행하기 섹션과 하위 호환이 없는 변경점 섹션을 참고하십시오.

버그를 발견한것 같아요! 누구에게 이 사실을 알려줘야 하나요?

PHP 버그 데이터베이스로 가서 그 버그가 이미 보고가 되었는지 확인해야 합니다. 그 데이터베이스에서 그 버그가 보고되지 않았다면 버그 보고 제출폼을 사용하세요. 버그는 할당된 트래킹 번호(tracking number)를 부여해야 하고 나중에 보고자가 되돌릴수 있도록 해주며 버그의 상태를 점검할수 있도록 하기 때문에 메일링리스트에 보내는것 보다는 버그 데이터베이스를 사용하는것이 좋습니다. 버그 데이터베이스는 » http://bugs.php.net/에 가면 보실수 있습니다.

User Contributed Notes

There are no user contributed notes for this page.


메일링리스트

이 섹션은 어떻게 PHP 커뮤니티에 참가할수 있는지 알려줍니다. 최고의 방법은 메일링리스트입니다.

  1. 어떠한 PHP 메일링리스트가 있습니까?
  2. 다른 커뮤니티도 있습니까?
  3. 도와주세요! 메일링리스트에 구독신청/구독취소 할수 없는것 같습니다!
  4. 메일링리스트의 옛기록(아카이브:archive)은 어디에 있습니까?
  5. 메일링리스트에 무엇을 물어볼수 있나요?
  6. 메일링리스트에 글을 올릴때 포함해야 하는 정보는 어떤것이 있나요?
어떠한 PHP 메일링리스트가 있습니까?

여러가지 목적을 위한 메일링리스트가 있습니다. 메일링리스트의 전체 목록은 » 지원 페이지에서 볼 수 있습니다.

대부분의 일반적인 메일링리스트는 php-general입니다. 이것을 구독하려면 » php-general-subscribe@lists.php.net로 메일을 보내세요. 메일 메시지의 제목과 본문에 특별한것을 포함할 필요는 없습니다. 구독을 취소하려면 » php-general-unsubscribe@lists.php.net로 메일을 보냅니다.

» 지원페이지의 웹인터페이스를 통해서 구독요청(subscribe)이나 구독취소(unsubscribe)를 할수 있습니다.

다른 커뮤니티도 있습니까?

전세계에 셀수없이 존재합니다. 우리는 예를 들어 몇몇의 IRC 서버와 외국어 메일링리스트에 대한 목록을 » 지원 페이지에 제공하고 있습니다.

도와주세요! 메일링리스트에 구독신청/구독취소 할수 없는것 같습니다!

php-general 메일링리스트에 구독신청이나 구독취소를 하는데 문제가 있으면 메일링리스트 프로그램이 정확한 메일링 주소를 이해할수없기 때문일수도 있습니다. 당신의 이메일주소가 joeblow@example.com이었다면, php-general-subscribe-joeblow=example.com@lists.php.net로 구독신청폼을 전송할수 있습니다. 또는 php-general-unsubscribe-joeblow=example.com@lists.php.net로 구독취소폼을 전송할수 있습니다. 다른 메일링리스트도 비슷한 주소를 사용하십시오.

메일링리스트의 옛기록(아카이브:archive)은 어디에 있습니까?

» 지원페이지에서 archive sites 목록을 발견할수 있습니다. 메일링리스트 기사는 뉴스 메시지로도 저장이 됩니다. 뉴스리더로 » news://news.php.net/ 뉴스서버에 접속할수 있습니다. 뉴스서버를 위한 실험적인 웹 인터페이스도 또한 존재합니다. » http://news.php.net/입니다.

메일링리스트에 무엇을 물어볼수 있나요?

PHP는 나날이 성장해가고 있기 때문에 php-general 메일링리스트로 트래픽이 증가하고 있습니다. 매일 약 150에서 200개의 글이 올라가고 있습니다. 이 때문에 다른곳에서 찾을수 있을때에는 최후의 수단으로 메일링리스트를 사용하는것이 모든사람에게 좋습니다.

메일링리스트로 글을 올리기 전에 FAQ를 살펴보거나 매뉴얼을 보십시오. 거기에 없다면 메일링리스트 옛기록(archives)을 찾아보십시오 (위를 보세요) PHP를 설치하거나 설정할때 문제가 발생하는것이라면 포함된 모든 문서와 README 파일을 읽어주십시오. 아직도 도움이 될만한 어떤 정보도 얻지 못했다면 그때 메일링리스트를 사용해주십시오.

질문을 하기전에, » 좀더 나은 질문을 하기위한 방법이란 글을 읽는것이 모두에게 유익할것입니다.

메일링리스트에 글을 올릴때 포함해야 하는 정보는 어떤것이 있나요?

"PHP를 올리고 실행시킬수 없어요! 도와주세요! 뭐가 문제죠?"와 같은 질문은 절대적으로 아무에게도 도움이 안됩니다. PHP를 올리고 실행시키는데 문제가 있다면 무슨 운영체제이고 PHP의 버전이 무엇인지 어떤 형태의 배포본인지 (pre-compiled, CVS, RPMS 등등)에 대한 정보, 무엇을 했는지, 어디서 멈췄는지, 정확한 에러 메시지에 대해서도 포함시켜야 합니다.

물론 다른 문제에도 해당됩니다. 무엇을 했는지, 어디서 멈쳤는지, 무엇을 하려고 했는지, 가능하다면 정확한 에러메시지까지도 포함시켜야 합니다. 소스코드에 문제가 있다면 작동하지 않는 코드의 일부분을 포함시킬 필요가 있습니다. 필요 이상의 코드를 포함시키지는 마세요! 그것은 그 글을 읽기 어렵게 만들고 많은 사람들이 이 때문에 모두 그 글을 그냥 지나칠지도 모릅니다. 메일에 포함된 정보가 얼마나 필요한지에 대해 확신할수 없으면 적지않게 포함하는것이 낫습니다.

기억해야 또다른 중요한 사항은 제목에 문제점을 요약하라는것입니다. "도와주세요~~!!!"나 "여기서 문제가 뭔가요?"는 구독자의 대다수에 의해 무시당할것입니다.

그리고 마지막으로 당부하건데 » 좀더 나은 질문을 하기위한 방법이란 글을 읽어주시기 바랍니다. 이글은 모두에게 큰 도움이 될것이고 특별히 당신에게도 도움이 됩니다.

User Contributed Notes

There are no user contributed notes for this page.


PHP 구하기

이 섹션은 PHP를 다운로드할수 있는곳과 OS에 관련된 정보를 다룹니다.

  1. PHP는 어디서 구할수 있습니까?
  2. pre-compiled 바이너리 버전도 있습니까?
  3. 추가적인 PHP 확장을 컴파일하기 위해서 필요한 라이브러리는 어디서 구할수 있습니까?
  4. 이 라이브러리를 작동시키려면 어떻게 해야 합니까?
  5. 윈도우 머신에 CVS 저장소로부터 최종 PHP 소스 코드를 받아왔습니다. 그것을 컴파일하려면 어떻게 해야 합니까?
  6. Browser Capabilities File을 어디서 구할수 있나요?
PHP는 어디서 구할수 있습니까?

PHP는 PHP 관련 사이트들로부터 구할수 있습니다. » http://www.php.net/에 정리되어있습니다. 또한 익명 CVS를 사용하여 가장 최종 버전을 다운로드 받을수도 있습니다. » http://www.php.net/anoncvs.php에 자세한 정보가 있습니다.

pre-compiled 바이너리 버전도 있습니까?

모든 주요 리눅스/유닉스 플랫폼을 모든 확장 옵션을 가진 PHP로 컴파일 할수 없기 때문에, 윈도우 시스템에서만 precompiled 바이너리를 제공합니다. 알려진대로, 많은 리눅스 배포판은 요즘 PHP가 내장되어 나옵니다. 윈도우 바이너리는 » Downloads페이지 에서 내려받을수 있습니다. 리눅스는 현재 사용중인 배포판 웹사이트를 방문하세요.

추가적인 PHP 확장을 컴파일하기 위해서 필요한 라이브러리는 어디서 구할수 있습니까?

Note: *로 표시된것은 thread-safe 라이브러리가 아닙니다. 그래서 multi-thread 윈도우 웹서버(IIS, netscape)에서 서버 모듈로 PHP와 사용해서는 안됩니다. 아직까지는 유닉스 환경에서는 상관없습니다.

  • » LDAP (Unix).
  • » LDAP (Unix/Win) : Mozilla Directory (LDAP) SDK
  • » free LDAP server.
  • » Berkeley DB2 (Unix/Win) : http://www.sleepycat.com/.
  • » SNMP* (Unix): .
  • » GD* (Unix/Win).
  • » mSQL* (Unix).
  • » PostgreSQL (Unix).
  • » IMAP* (Win/Unix).
  • » Sybase-CT* (Linux, libc5) : Available locally.
  • » FreeType (libttf):.
  • » ZLib (Unix/Win32).
  • » expat XML parser (Unix/Win32).
  • » PDFLib.
  • » mcrypt.
  • » mhash.
  • » t1lib.
  • » dmalloc.
  • » aspell.
  • » readline.

이 라이브러리를 작동시키려면 어떻게 해야 합니까?

라이브러리를 제공하는 다음 지시가 필요합니다. 이 라이브러리 중 어떤것은 PHP의 'configure'스크립트 명령을 실행할때 자동적으로 인식이 됩니다. (GD 라이브러리 같은 경우). 다른것은 'configure'의 '--with-EXTENSION' 옵션을 사용하여 활성화시켜야 합니다. 이에 관련되 리스트를 보려면 'configure --help'명령을 실행하세요.

윈도우 머신에 CVS 저장소로부터 최종 PHP 소스 코드를 받아왔습니다. 그것을 컴파일하려면 어떻게 해야 합니까?

우선 Microsoft Visual C++ v6 (v5도 가능하지만 우리는 v6를 사용한다) 가 필요합니다. 그리고 다른 지원 파일이 필요합니다. 윈도우에서 PHP 소스로 빌드하기에 관한 섹션을 참고하시기 바랍니다.

Browser Capabilities File을 어디서 구할수 있나요?

» http://browscap.org/ 에서 browscap.ini파일을 찾을수 있습니다.

User Contributed Notes

Anonymous 17-Sep-2013 08:41
installation instructions are inside the downloaded zip of php


데이터베이스와 관련된 문제

이 섹션은 PHP와 데이터베이스의 연동에 관련되어 자주 묻는 질문들을 다룹니다. 네, PHP는 오늘날 거의 모든 데이터베이스와 연동이 가능합니다.

  1. PHP로 Microsoft SQL 서버로 접근이 가능하다고 들었습니다. 어떻게요?
  2. MS 액세스 데이터베이스(Microsoft Access database)에 접속할수있습니까?
  3. Why is the MySQL extension (ext/mysql) that I've been using for over 10 years discouraged from use? Is it deprecated? What do I use instead? How can I migrate?
  4. PHP 5 부터는 더이상 MySQL 클라이언트 라이브러리를 함께 제공하지 않는다고 하는데, 이말은 무슨 의미인가요? PHP로 계속 MySQL을 사용할수 있나요? MySQL을 사용하려고 하면 "function undefined"라는 에러 메시지를 보여줍니다. 무엇때문인지요?
  5. 공유 MySQL 지원(shared MySQL support)을 설치한후에, 아파치가 libphp4.so를 로드할때마다 코어 덤프가 일어납니다. 이것을 고칠수 있나요?
  6. 왜 다음과 같은 에러가 발생합니까: "Warning: 0 is not a MySQL result index in <file> on line <x>" 또는 "Warning: Supplied argument is not a valid MySQL result resource in <file> on line <x>?
PHP로 Microsoft SQL 서버로 접근이 가능하다고 들었습니다. 어떻게요?

윈도우 머신상에서, 인클루드 ODBC 지원(included ODBC support)과 ODBC 드라이버(correct ODBC driver)를 사용할수 있습니다.

유닉스 머신에서, 사이베이스-CT(Sybase-CT)드라이버를 사용할수있습니다. 왜냐하면 둘은 프로토콜이 호환되기 때문입니다. 사이베이스는 » 리눅스 시스템에 필요한 무료버전 을 만들었습니다. 다른 유닉스 운영체제에서는 적합 라이브러리(correct library) 로 사이베이스와 연동해야 합니다. 또한 다음 FAQ를 보세요.

MS 액세스 데이터베이스(Microsoft Access database)에 접속할수있습니까?

네, 윈도우 9x/Me나 NT/2000 상에서 운영하고 있다면 그리고 그 운영체제에서 ODBC를 사용할수 있고 MS 액세스 데이터베이스를 위한 ODBC 드라이버를 사용할수 있다면 필요한 모든 툴은 이미 보유한 것입니다.

유닉스에서 PHP를 사용하고 있고 윈도우 박스의 MS 액세스와 연동하기를 원한다면 유닉스 ODBC 드라이버가 필요합니다. » OpenLink Software에 이를 수행할수 있는 유닉스 기반 ODBC 드라이버가 있습니다.

다른 방법은 윈도우 ODBC 드라이버까지 포함되있고 데이터도 저장할수 있는 SQL 서버를 사용하는것입니다. 그 데이터는 MS 액세스에서 접근할수 있고(ODBC로), PHP로(내장 드라이버)로 접근할수 있습니다. 또는 액세스와 PHP 모두에게 가용한 파일 형식을 이용하는것입니다. 즉 일반 파일(flat files)이나 dBase 형식으로요. 이에 관련된 글을 OpenLink software의 Tim Hayes가 쓴글을 참고하세요:

PHP에서 직접 ODBC를 사용하여 데이터베이스에 접근 할 수 있을 경우 - 즉, OpenLink의 드라이버를 사용할 경우 중간 단계로 다른 데이터베이스를 사용하는 것은 좋은 생각이 아닙니다. 중간 파일 형식을 사용해야 한다면, OpenLink에서 Virtuoso(가상 데이터베이스 엔진)를 NT, 리눅스, 기타 유닉스 플랫폼에 릴리즈했습니다. » 웹 사이트에서 무료로 내려받을 수 있습니다.

다른 방법은 MySQL을 사용하고, 윈도우상에서 MyODBC드라이버를 사용하여 데이터베이스를 동기화하는 것입니다. Steve Lawrence의 글을 참고하세요:

  • MySQL의 지시에 따라 사용중인 플랫폼에 MySQL을 설치합니다. » www.mysql.com에서 받을수 있습니다. 데이터베이스를 설치할때 외에는 특별한 설정이 필요치않습니다. 계정을 설정하고, host 필드에는 %로 놓거나 MySQL을 접속하고자하는 윈도우 컴퓨터의 호스트명을 넣습니다. 서버명, 계정, 비밀번호를 넣습니다.
  • MySQL사이트에서 윈도우드라이버용 MyODBC를 다운받습니다. 윈도우 머신에 그것을 설치합니다. 이 프로그램에 포함되어있는 유틸로 동작을 테스트할수있습니다.
  • ODBC 관리자 유저나 시스템 dsn을 만듭니다. 제어판에 위치합니다. dsn 이름을 만들고 스텝1에서 설정한 MySQL데이터베이스에 대한 호스트명, 유저명, 패스워드, 포트등을 넣습니다.
  • 전체 설치로 액세스를 설치해서, 필요한 애드인을 얻습니다... 최소한 ODBC 지원과 연결 테이블 관리자가 필요합니다.
  • 이제부터 중요한 부분입니다! 새 액세스 데이터베이스를 만듭니다. 그 테이블에서 오른쪽 클릭하고 링크 테이블(Link Tables)를 선택하거나, 파일 메뉴 옵션에서 Get External Data를 선택한후에 링크 테이블(Link Tables)을 선택하세요. 파일 브라우저 박스가 나올때 files of type: ODBC 를 선택하세요. 시스템 dsn과 스텝 3에서 만든 dsn의 이름을 선택하세요. 연결된 테이블을 선택하고, OK를 클릭하세요 이젠 테이블을 열수 있고 MySQL서버에 데이터를 넣거나/지우거나/수정 할수 있습니다. 질의(query)를 수행할수도 있고 MySQL에 테이블을 임포트(import)/익스포트(export)할수 있습니다. 폼이나 레포트도 만들수있고 기타등등...

팁과 트릭:

  • 액세스에서 테이블을 만들수 있고 MySQL로 그 테이블을 익스포트(export)할수 있고, 거기에 링크를 걸수 있습니다. 이렇게 하면 테이블을 빨리 만들어냅니다.
  • 액세스에서 테이블을 만들때, 액세스에서 테이블에 쓸수있는 권한을 부여하기 위해 정의한 프라이머리 키(primary key)가 있어야 합니다. 액세스에 링크를 걸기전에 MySQL에 프라이머리 키를 만들었는지 확인합니다.
  • MySQL에서 테이블을 변경한다면, 액세스로 다시 링크를 걸어야 합니다. tools>add-ins>linked table manager로 가서 ODBC DSN을 보고, 거기에서 다시 링크할 테이블을 선택합니다. 거기서 dsn의 원본을 옮길수 있습니다. OK하기 전에 새 위치(new location) 항상 확인 체크박스를 클릭하면 됩니다.

Why is the MySQL extension (ext/mysql) that I've been using for over 10 years discouraged from use? Is it deprecated? What do I use instead? How can I migrate?

There are three MySQL extensions, as described under the Choosing a MySQL API section. The old API should not be used, and one day it will be deprecated and eventually removed from PHP. It is a popular extension so this will be a slow process, but you are strongly encouraged to write all new code with either mysqli or PDO_MySQL.

Migration scripts are not available at this time, although the mysqli API contains both a procedural and OOP API, with the procedural version being similar to ext/mysql.

It is not possible to mix the extensions. So, for example, passing a mysqli connection to PDO_MySQL or ext/mysql will not work.

PHP 5 부터는 더이상 MySQL 클라이언트 라이브러리를 함께 제공하지 않는다고 하는데, 이말은 무슨 의미인가요? PHP로 계속 MySQL을 사용할수 있나요? MySQL을 사용하려고 하면 "function undefined"라는 에러 메시지를 보여줍니다. 무엇때문인지요?

네, PHP에서는 앞으로도 계속 MySQL지원이 될것입니다. PHP 5의 단 하나의 변화가 클라이언트 라이브러리 자체를 함께 제공하지 않는다는 것입니다. 그 이유는 특별한 순서없이 아래글에서 설명하도록 하겠습니다:

  • 요근래의 대부분의 시스템은 이미 클라이언트 라이브러리가 설치되어있습니다.

  • 위에서 언급한대로, 라이브러리를 여러가지 버전을 갖게 되면 혼란스러워질수 있습니다. 예를 들면 어떤 버전은 mod_auth_mysql을 링크하고 다른 버전에는 PHP를 링크하고나서 아파치안에서 두개 버전을 모두 활성화시키면, 큰 충돌이 발생하게 됩니다. 또한, 함께 제공된 라이브러리가 설치된 서버버전과 항상 잘 작동하지도 않습니다. 이런 경우를 잘 보여주는 예가 mysql.socket 유닉스 도메인 소켓 파일이 있는 곳을 찾을수 없을때입니다.

  • 유지관리가 약간 더디게 되고 최신판 버전에 점점더 뒤떨어지게 될것입니다.

  • 앞으로의 라이브러리 버전은 GPL을 따르고 따라서 우리가 BSD/아파치 스타일의 라이센스 프로젝트인 GPL의 라이브러리를 함께 제공할수 없기 때문에 업그레이드 경로를 갖을수 없습니다. PHP 5에서 이런 깨끗한 중단이 최고의 선택으로 보입니다.

이와 같은 사실은 실제로 많은 사람들에게 영향이 있지는 않을것입니다. 유닉스 사용자, 최소한 그들이 무엇을 하는지 아는 사용자는 PHP를 빌드할 때 항상 --with-mysql=/usr 옵션을 추가하여 시스템의 libmysqlclient 라이브러리로 PHP를 빌드했습니다. 윈도우 사용자는 php.ini에서 php_mysql.dll 확장을 활성화할 수 있습니다. 자세한 설치 안내는 MySQL 레퍼런스를 참고하십시오. 또한, libmysql.dll이 시스템 PATH에 들어있는지 확인하십시오. 이를 하는 방법은 윈도우 시스템 PATH 설정하기 FAQ를 읽어보십시오. libmysql.dll(과 많은 PHP 관련 파일들)은 PHP 폴더에 존재하므로, PHP 폴더를 시스템 PATH에 추가할 수 있습니다.

공유 MySQL 지원(shared MySQL support)을 설치한후에, 아파치가 libphp4.so를 로드할때마다 코어 덤프가 일어납니다. 이것을 고칠수 있나요?

MySQL libs가 pthreads로 링크되어있다면 이런 현상이 발생합니다. ldd로 점검해보세요. 그렇다면 MySQL 타볼(tarball)를 가져와서 소스로 컴파일하거나 소스 RPM에서 재컴파일해서 spec 파일에서 thread 클라이언트 코드로 변경하는 곳을 제거하세요. 이 방법 모두 이런 현상을 고칠수 있습니다. 그리고나서 새로운 MySQL libs와 함께 PHP를 재컴파일하세요.

왜 다음과 같은 에러가 발생합니까: "Warning: 0 is not a MySQL result index in <file> on line <x>" 또는 "Warning: Supplied argument is not a valid MySQL result resource in <file> on line <x>?

0으로 되어있는 결과 식별자(result identifier)를 사용하려 하고 있습니다. 0이라는것은 어떤 이유로 질의가 실패했다는것을 말합니다. 질의를 보내고 돌아온 결과 식별자를 사용하기 전에 에러를 점검할 필요가 있습니다. 다음과 비슷한 방법이면 됩니다.

<?php

$result = mysql_query("SELECT * FROM tables_priv");
if (!$result) {
    echo mysql_error();
    exit;
}
?>
또는
<?php

$result = mysql_query("SELECT * FROM tables_priv")
    or die("Bad query: " . mysql_error());
?>

User Contributed Notes

ashay dot chaudhary at microsoft dot com 09-May-2010 10:20
Q: I heard it's possible to access Microsoft SQL Server from PHP. How?

"On Windows machines, you can simply use the included ODBC support and the correct ODBC driver."
There is a better solution now, download the SQL Server Driver for PHP from Microsoft's Download Center, it is officially supported by Microsoft.

Source code: sqlsrvphp.codeplex.com
plandis98 at yahoo dot com 18-Dec-2005 07:46
If one is using PHP on Windows to connection to MS SQL Server and does not want to pass credentials via the mssql_connect but instead use MS Connectionless NT Authentication, what is the proper syntax/usage?

I believe you want to turn on secure_connection in php.ini file:
; Use NT authentication when connecting to the server
mssql.secure_connection = On
djlopez at gmx dot de 08-Sep-2005 02:51
How to compile PHP5 when you have installed mysql, but
- you can't find the "mysql header files" or
- get this error: Cannot find MySQL header files under /usr/

If you want to use mysql 4.0 (*not* 4.1 or even higher!) just download & extract the latest mysql4.0 binary distribution to e.g.
/root/mysql40/

but don't start it (it even should not work, if you have a mysql server already running)

Then use --with-mysql=/root/mysql4 within your php5 configuration.

After compiling/installing (make, make install), you may delete the /root/mysql40/ directory, or keep it for later versions of php5.
20-Mar-2005 10:19
Just a quick note which may help Windows users setting up PHP/Apache/MySQL.  I was receiving these two error messages on Apache startup: 1) "The procedure entry point mysql_thread_end could not be located in the dynamic link library LIBMYSQL.dll" and 2) "PHP Startup: Unable to load dynamic library '<my php installation dir>/ext/php_mysql.dll' - The specified procedure could not be found".  After checking that libmysql.dll did exist in my root PHP install dir, and that this dir was in my path, I determined that the problem was an older version of libmysql.dll.  My solution was to search all harddrives for this file (I found 6 copies of 5 different versions), and rename all of them except my PHP install dir copy.  PHP, Apache and MySQL all function as expected after making this change.
knb at gfz-potsdam dot de 11-Oct-2004 11:53
This is a crucial piece of information for SYBASE users:

If you are using the free, but old, 11.x client libs from sybase,
 then compile with option  "--with-sybase-ct=$SYBASE"
substitute $SYBASE with the appropriate directory name.

option --with-sybase (without ct) can somehow be used to talk to old MS-SQL servers, but only with the 11.x client libs.

If you are using free, or have legally obtained, 12.x client libs from sybase, then compile with option  "--with-sybase-ct=$SYBASE/$SYBASE_OCS"
substitute $SYBASE/$SYBASE_OCS with the appropriate directory name.


설치

이 섹션은 PHP 설치에 관한 일반적인 질문을 다루고 있습니다. PHP는 대부분의 OS(OSX 이전의 MacOS는 예외)와 대부분의 웹 서버에서 사용할 수 있습니다.

PHP를 설치하려면, 설치와 설정에 있는 지시를 따르십시오.

  1. 왜 제품 환경에서 아파치2 쓰레드 MPM을 사용할 수 없습니까?
  2. 유닉스/윈도우: php.ini 파일을 어디에 놓아야 합니까?
  3. 유닉스: PHP를 설치하였지만, 문서를 읽을 때 마다 'Document Contains No Data'를 얻습니다! 대체 뭡니까?
  4. 유닉스: RPMS를 사용해서 PHP를 설치했지만, 아파치가 PHP 페이지를 처리하지 않습니다! 대체 뭡니까?
  5. 유닉스: 아파치에 FrontPage 확장 패치를 적용하였더니, 갑자기 PHP가 작동하지 않습니다. PHP가 아파치 FrontPage 확장과 호환되지 않습니까?
  6. 유닉스/윈도우: PHP를 설치하였지만, 브라우저에서 PHP 스크립트 파일에 접근하면 빈 화면이 나옵니다.
  7. 유닉스/윈도우: PHP를 설치하였지만, 브라우저에서 PHP 스크립트 파일에 접근하면 서버 500 오류가 나옵니다.
  8. 몇몇 OS: 오류 없이 PHP를 설치했지만, 아파치를 시작하려 할 때 정의되지 않은 심볼 오류가 나옵니다: [mybox:user /src/php4] root# apachectl configtest apachectl: /usr/local/apache/bin/httpd Undefined symbols: _compress _uncompress
  9. 윈도우: PHP를 설치하였지만, 브라우저에서 PHP 스크립트 파일에 접근할 때, 오류가 발생합니다: cgi error: The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:
  10. 윈도우: 모든 지시를 따랐는데, PHP와 IIS가 여전히 작동하지 않아요!
  11. IIS, PWS, OmniHTTPD, Xitami에서 PHP를 CGI로 실행할 때, 다음 오류가 나옵니다: Security Alert! PHP CGI cannot be accessed directly..
  12. 내 php.ini가 발견되고 읽어지는지 어떻게 압니까? 내 변경점이 적용되지 않는 것 같습니다.
  13. 윈도우에서 어떻게 PHP 디렉토리를 PATH에 추가합니까?
  14. 윈도우에서 어떻게 php.ini 파일을 사용하게 합니까?
  15. PHP와 아파치 내용 협상(MultiViews 옵션)을 사용할 수 있습니까?
  16. PHP는 GET과 POST 요청 메쏘드만 처리하도록 제한됩니까?
왜 제품 환경에서 아파치2 쓰레드 MPM을 사용할 수 없습니까?

PHP는 접착제입니다. 수많은 써드파티 라이브러리를 합쳐서 직관적으로 일관성 있게 하고, 배우기 쉬운 언어 인터페이스로 붙여서 좋은 웹 어플리케이션을 만드는 접착제입니다. PHP의 유연성과 힘은 놓여지는 플랫폼의 안정성과 강력함에 의존합니다. 작동하는 OS, 작동하는 웹 서버, 붙일 수 있는 작동하는 써드파티 라이브러리가 필요합니다. 이들 중 하나라도 작동을 멈췄을 때, PHP는 문제를 발견하고 빠르게 고칠 수 있는 방법이 필요합니다. 놓여지는 프레임워크에 완전히 분리된 실행 쓰레드, 완전히 분리된 메모리 세그먼트, 각 요청이 실행되는 강한 그릇을 갖추지 못한다면, PHP 시스템에 예상할 수 없는 약점을 가져옵니다.

쓰레드 MPM을 사용해야 한다면, PHP가 자신의 메모리 공간을 가지고 실행되는 FastCGI 설정을 살펴보십시오.

마지막으로, 쓰레드 MPM을 사용하는 것에 대한 이 경고는 윈도우 시스템에는 강요되지 않습니다. 플랫폼에 쓰이는 대부분의 라이브러리가 쓰레드 안전하게 짜여져 있기 때문입니다.

유닉스/윈도우: php.ini 파일을 어디에 놓아야 합니까?

기본값으로, 유닉스에서는 <install-path>/lib인 /usr/local/lib에 위치해야 합니다. 많은 사람들이 컴파일 시에 --with-config-file-path 플래그로 이를 변경합니다. 예를 들면, 다음과 같이 설정할 수 있습니다:

--with-config-file-path=/etc
이렇게 하면, 배포판의 php.ini-dist를 /etc/php.ini에 복사하여 수정함으로써 원하는 변경을 적용할 수 있습니다.

--with-config-file-scan-dir=PATH

윈도우에서 php.ini 파일의 기본 경로는 윈도우 디렉토리입니다. 아파치 웹서버를 사용한다면, 우선 아파치 설치 디렉토리에서 php.ini를 검색합니다. 즉, c:\program files\apache group\apache 입니다. 이 방법으로 같은 머신에서 작동하는 다른 버전의 아파치에서 각각의 php.ini 파일을 사용할 수 있습니다.

설정 파일 챕터를 참고하십시오.

유닉스: PHP를 설치하였지만, 문서를 읽을 때 마다 'Document Contains No Data'를 얻습니다! 대체 뭡니까?

아마도 PHP에 문제가 있어서 코어 덤프가 일어남을 의미합니다. 이런 경우엔 서버 오류 기록을 살펴보고, 작은 테스트 방식을 사용해서 문제가 재발하는지 확인해보십시오. 'gdb'를 사용할 줄 안다면, 오류 보고에 역추적을 제공해주십시오. 개발자가 문제를 파악하기 쉽습니다. PHP를 아파치 모듈로 사용한다면 다음과 같이 해보십시오:

  • httpd 프로세스 중지

  • gdb httpd

  • httpd 프로세스 중지

  • > run -X -f /path/to/httpd.conf

  • 브라우저에서 문제가 발생하는 URL을 시도합니다

  • > run -X -f /path/to/httpd.conf

  • 코어 덤프를 얻는다면, 여기에서 gdb가 이를 알려줍니다

  • type: bt

  • 오류 보고에 역추적을 포함시킵니다. » http://bugs.php.net/에서 제출할 수 있습니다.

스크립트가 정규 표현식 함수(preg_match() 계열)를 사용한다면, PHP와 아파치를 동일한 정규 표현식 패키지로 컴파일해야 합니다. PHP와 아파치 1.3.x에서는 자동으로 됩니다.

유닉스: RPMS를 사용해서 PHP를 설치했지만, 아파치가 PHP 페이지를 처리하지 않습니다! 대체 뭡니까?

아파치와 PHP를 모두 RPM 패키지로 설치했다고 가정하면, httpd.conf 파일에서 다음 줄 중 일부나 전체를 추가하거나 주석을 해제해야 합니다:

# Extra Modules
AddModule mod_php.c
AddModule mod_php.c
AddModule mod_perl.c

# Extra Modules
LoadModule php_module         modules/mod_php.so
LoadModule php5_module        modules/libphp5.so     # for PHP 5
LoadModule perl_module        modules/libperl.so
그리고 추가로:
AddType application/x-httpd-php .php
...를 전역 프로퍼티에 넣거나, PHP 지원을 추가할 VirtualDomain의 프로퍼티에 넣어야 합니다.

유닉스: 아파치에 FrontPage 확장 패치를 적용하였더니, 갑자기 PHP가 작동하지 않습니다. PHP가 아파치 FrontPage 확장과 호환되지 않습니까?

아니오, PHP는 FrontPage 확장과 함께 작동합니다. 문제는 FrontPage 패치가 PHP가 의존하는 아파치 구조를 변경했기 때문입니다. FP 패치를 한 후에 PHP를 재컴파일('make clean ; make'를 사용)하면 문제가 해결될 것입니다.

유닉스/윈도우: PHP를 설치하였지만, 브라우저에서 PHP 스크립트 파일에 접근하면 빈 화면이 나옵니다.

웹 브라우저에서 '소스 보기'를 하면 PHP 스크립트의 소스 코드가 나올겁니다. 이는 웹 서버가 스크립트를 PHP에 해석하도록 보내지 않았음을 의미합니다. 서버 설정이 무언가 잘못되었습니다 - PHP 설치 지시에 따라서 서버 설정을 다시 확인해보십시오.

유닉스/윈도우: PHP를 설치하였지만, 브라우저에서 PHP 스크립트 파일에 접근하면 서버 500 오류가 나옵니다.

서버가 PHP를 실행하려 할 때 무언가 잘못되었습니다. 확인할 수 있는 오류 메세지를 얻으려면, 명령줄에서 PHP 실행(윈도우에서는 php.exe이 있는 디렉토리로 변경하고 php -i를 실행하십시오. PHP 실행에 문제가 있으면, 적절한 오류 메세지가 나올 것이고, 이로써 해결을 위한 실마리를 얻을 수 있습니다. HTML 코드로 스크린이 채워진다면(phpinfo()의 출력) PHP는 작동하고, 문제는 서버 설정일 것입니다. 다시 확인해보십시오.

몇몇 OS: 오류 없이 PHP를 설치했지만, 아파치를 시작하려 할 때 정의되지 않은 심볼 오류가 나옵니다:
[mybox:user /src/php4] root# apachectl configtest
 apachectl: /usr/local/apache/bin/httpd Undefined symbols:
  _compress
  _uncompress

정확히 말하면, PHP는 아무 문제가 없지만 MySQL 클라이언트 라이브러리에 문제가 있습니다. 몇몇 경우 --with-zlib 이 필요합니다. MySQL FAQ에서도 다루고 있습니다.

윈도우: PHP를 설치하였지만, 브라우저에서 PHP 스크립트 파일에 접근할 때, 오류가 발생합니다:

cgi error:
The specified CGI application misbehaved by not
returning a complete set of HTTP headers.
The headers it did return are:

이 오류 메세지는 PHP가 무언가를 출력하는데 실패했음을 의미합니다. 확인할 수 있는 오류 메세지를 얻으려면, 명령줄에서 PHP 실행(윈도우에서는 php.exe)이 있는 디렉토리로 변경하여 php -i를 실행하십시오. PHP 실행에 문제가 있으면, 적합한 오류 메세지가 나오므로, 이를 해결의 실마리가 됩니다. HTML 코드로 화면이 가득 찬다면(phpinfo() 함수의 출력) PHP는 작동합니다.

PHP가 명령줄에서 작동한다면, 다시 브라우저에서 스크립트에 접근해보십시오. 여전히 실패한다면 다음 중 하나입니다:

  • PHP 스크립트, php.exe, php4ts.dll, php.ini, 적재하려는 PHP 확장의 파일 접근 권한 문제입니다. 익명 인터넷 사용자 ISUR_<machinename>이 이들에 접근할 수 없습니다.
  • 스크립트 파일이 존재하지 않습니다. (혹은, 웹 루트 디렉토리에서 상대 경로가 생각한 것과 다릅니다) IIS에서는 Internet Services Manager에서 'check file exists' 박스를 켬으로써 이 오류를 추적할 수 있습니다. 스크립트 파일이 존재하지 않는다면 서버가 404 오류를 내보냅니다. 이는 IIS가 스크립트 파일의 NTLanMan 권한에 기반한 인증을 수행하는 추가적인 장점이 있습니다.
윈도우: 모든 지시를 따랐는데, PHP와 IIS가 여전히 작동하지 않아요!

PHP 스크립트를 실행해야 하는 사람이 php.exe를 실행할 권한이 있는지 확인하십시오! IIS는 IIS가 설치되었을 때 추가된 익명 사용자를 사용합니다. 이 사용자가 php.exe에 권한이 있어야 합니다. 또한, 모든 인증된 사용자도 php.exe를 실행할 권한이 있어야 합니다. 그리고 IIS4에서는 PHP가 스크립트 엔진임을 알려야 합니다. 이 faq도 읽어보십시오.

IIS, PWS, OmniHTTPD, Xitami에서 PHP를 CGI로 실행할 때, 다음 오류가 나옵니다: Security Alert! PHP CGI cannot be accessed directly..

cgi.force_redirect 지시어를 0으로 설정해야 합니다. 기본값이 1이므로 지시어를 (;으로) 주석처리하면 안됩니다. 다른 지시어와 마찬가지로, php.ini에서 설정합니다.

기본값이 1이기 떄문에, 정확한 php.ini를 읽고 있는지 100% 확인하는게 중요합니다. 자세한 내용은 이 faq를 읽어보십시오.

내 php.ini가 발견되고 읽어지는지 어떻게 압니까? 내 변경점이 적용되지 않는 것 같습니다.

어떤 php.ini를 PHP에서 읽는지 확인하려면, phpinfo()를 호출하십시오. 윗 부분에, Configuration File (php.ini) 목록이 존재합니다. 이는 PHP가 어디에서 php.ini를 찾고 읽었는지 여부를 알려줍니다. 디렉토리 PATH만 존재한다면, 읽어지지 않은 것이므로, php.ini를 그 디렉토리에 넣으십시오. php.ini가 PATH에 포함되어 있다면, 읽혀지는 것입니다.

php.ini가 읽혀지고 PHP를 모듈로 사용한다면, php.ini를 변경한 후에 웹 서버를 재시작하십시오.

php_ini_loaded_file() 참고.

윈도우에서 어떻게 PHP 디렉토리를 PATH에 추가합니까?

윈도우 NT, 2000, XP, 2003에서:

  • 제어판에 가서 시스템 아이콘을 엽니다. (시작 -> 설정 -> 제어판 -> 시스템, 윈도우 XP/2003에서는 시작 -> 제어판 -> 시스템)

  • 고급 탭으로 갑니다

  • '환경 변수' 버튼을 클릭

  • '시스템 변수' 부분을 봅니다

  • Path 엔트리를 찾습니다 (스크롤해야 할 수 있습니다)

  • Path 엔트리를 더블 클릭

  • 끝 부분에 ';'를 앞에 덧붙여서 PHP 디렉토리를 넣습니다 (즉, ;C:\php)

  • 확인을 누르고 컴퓨터를 재시작합니다

윈도우 98/Me에서는 autoexec.bat 파일을 수정해야 합니다:

  • 메모장을 엽니다 (시작 -> 실행, notepad를 칩니다)

  • C:\autoexec.bat 파일을 엽니다.

  • PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;..... 줄에 위치하여 추가: 줄 마지막에 ;C:\php

  • 파일을 저장하고 컴퓨터를 재시작합니다

Note: 위 단계가 완료된 후에 PATH 변경이 적용되도록 재부팅하십시오.

PHP 매뉴얼이 파일을 윈도우 시스템 디렉토리에 넣도록 하는 경우가 있는데, 이 디렉토리(C:\Windows, C:\WINNT 등)가 기본적으로 시스템 PATH에 있기 때문입니다. 윈도우 시스템 디렉토리로 파일을 복사하는건 배제된지 오래되었고, 문제가 발생할 수 있습니다.

윈도우에서 어떻게 php.ini 파일을 사용하게 합니까?

여러 방법이 있습니다. 아파치를 사용한다면, 적합한 설치 지시를 읽어보십시오. (아파치 1, 아파치 2) 그 외의 경우에는 PHPRC 환경 변수를 설정해야 합니다:

윈도우 NT, 2000, XP, 2003에서:

  • 제어판에 가서 시스템 아이콘을 엽니다. (시작 -> 설정 -> 제어판 -> 시스템, 윈도우 XP/2003에서는 시작 -> 제어판 -> 시스템)

  • 고급 탭으로 갑니다

  • '환경 변수' 버튼을 클릭

  • '시스템 변수' 부분을 봅니다

  • '새로 만들기'를 클릭하고 변수 이름에 'PHPRC', 변수 값에 php.ini가 위치한 디렉토리를 넣습니다. (예. C:\php)

  • 확인을 누르고 컴퓨터를 재시작합니다

윈도우 98/Me에서는 autoexec.bat 파일을 수정해야 합니다:

  • 메모장을 엽니다 (시작 -> 실행, notepad를 칩니다)

  • C:\autoexec.bat 파일을 엽니다.

  • 파일 마지막에 새 줄을 추가합니다: set PHPRC=C:\php (C:\php를 php.ini가 위치한 디렉토리로 치환합니다) 경로에 공백을 포함할 수 없는 점에 주의하십시오. 예를 들어, PHP를 C:\Program Files\PHP에 설치하였다면, C:\PROGRA~1\PHP를 넣어야 합니다.

  • 파일을 저장하고 컴퓨터를 재시작합니다.

PHP와 아파치 내용 협상(MultiViews 옵션)을 사용할 수 있습니까?

PHP 파일로 연결이 확장자를 포함하면, 완벽하게 작동합니다. 이 FAQ는 PHP 파일이 확장자를 포함하지 않는 경우와, 확장자 없는 URL에서 내용 협상으로 PHP 파일을 고르는 방법만 다룹니다. 이 경우, AddType application/x-httpd-php .php 줄을 교체합니다:

# PHP 4
AddHandler php-script php
AddType text/html php

# PHP 5
AddHandler php5-script php
AddType text/html php
이 해결법은 아파치 1에서는 작동하지 않습니다. PHP 모듈이 php-script을 잡아내지 못하기 때문입니다.

PHP는 GET과 POST 요청 메쏘드만 처리하도록 제한됩니까?

아니오, CONNET 등, 어떠한 요청 메쏘드도 다룰 수 있습니다, 적합한 응답 상태는 header()로 보낼 수 있습니다. GET과 POST 메쏘드만 다뤄야 할 경우에는, 아파치 설정으로 가능합니다:

<LimitExcept GET POST>
Deny from all
</LimitExcept>

User Contributed Notes

ssaxena at nriol dot com 12-Mar-2010 11:28
Here are my struggle to get PHP 5.2.12 working on Windows XP SP3, IIS 5.1 (Please note Win XP SP3 has issue of FastCGI)

1. Dowloaded installer and installed in C:\Program Files\PHP as ISAPI module

---->Did not work

Added php5isapi.dll in ISAPI Filter and added the extension .php in Home Dir->Configuration
(For every setting change restarted IIS on cmd->iisreset)

---->Did not work

(And the Google was pointing to the Fast CGI issue; Don't know why microsoft has MS-FAST ! )
Uninstalled the PHP and cleaned up the folders, PATH environment variable & PHPRC, the IIS settings and registry entries and shut down(restart is different from shut down) the system to make it very clean

2. Reinstalled the PHP in C:\PHP as ISAPI and did the same as above in IIS
(For every setting change restarted IIS on cmd->iisreset)

---->Did not work

Uninstalled and cleaned up same as above and took a breath to make sure what I am doing wrong.
Shutdown the system.

3. Unzipped the php-5.2.12-Win32.zip and did the same as above (this time manually PATH variable and security permission for the PHP folder and following settings)

short_open_tag = On
error_reporting = E_ALL & ~E_NOTICE
display_errors = On
doc_root = "C:\Inetpub\wwwroot"
extension_dir = "C:\PHP\ext"
cgi.force_redirect = 0

---->Did not work

Uninstalled and cleaned up same as above
Shutdown the system.

4. Downloaded Microsoft Web Platform Installer 2.0 and select the PHP+Fast CGI+Windows Cache for PHP extension and Web Deployment Tools
restarted the system to take effect the settings

---->Did not work

Uninstalled and cleaned up same as above
Shutdown the system

5. Took the prev version of PHP php-5.2.4-Win32.zip unzip the folder and did the following changes:

short_open_tag = On
error_reporting = E_ALL & ~E_NOTICE
display_errors = On
doc_root = "C:\Inetpub\wwwroot"
extension_dir = "C:\PHP\ext"
cgi.force_redirect = 0

set the security permission as IIS_machinename with full access to the C:\PHP folder

------->IT GOT WOKRING

Hope people would be aware of php-5.2.12 and would love the php-5.2.4
john dot calder at manukau dot ac dot nz 04-May-2008 07:19
WINDOWS 2003 and IIS.  BIG LESSON FOR ME was "don't mess with the optional components".
I had lots of trouble until I tried installing without asking for extras and then it was very smooth.
I have found no need to set up paths.
I have found no need to configure IIS.
I have found no need to change security on the PHP folder.

What works for me with Windows/IIS:

1.  You need to be logged-on as "administrator"

2.  Run  "php-5.2.6-win32-installer.msi"
Your ONLY action during this wizard is to select "IIS ISAPI" as the webserver.  Do NOT CHANGE anything else!  I am guessing that the optional extras apply only to Apache because after enabling them I see Apache-related error messages.

3.  Setup a test virtual directory as you would for an ASP or ASP.NET web app.  eg "phptest"
Then use notepad to create a test page eg "test.php"
<?php
phpinfo();
?>

4.  In Internet Explorer, test with eg this address
http://localhost/phptest/test.php

IF this does not give you a beautiful page of details THEN start reading other advice on this page - especially helpful for me was the faq advice to test run "php.exe -i" from the command prompt.
charleslynch at xybersoftware dot com 18-Apr-2008 04:30
One final note to the message below if you are getting the message about not having rights to execute...You need to use IIS right click on the website and choose 'Properties/Home Directory' then on the 'Execute Permissions' combo box you need to choose 'Scripts only'...Click ok...another dialog may appear asking which directories to apply the action too...choose any directories that you intend on using php in and click ok...It should work now...
joerg at fenin dot de 02-Nov-2007 11:35
To limit HTTP Request Methods on the Apache server running PHP you should use mod_rewrite or mod_security instead of LimitExcept, to give you more control over the handling of such http errors.  Can go in .htaccess file or httpd.conf

# mod_rewrite
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_METHOD} !^(GET|HEAD|POST)$
  RewriteRule .* - [F,L]
</IfModule>

# mod_security
<IfModule mod_security.c>
  SecFilterSelective REQUEST_METHOD "!^(GET|HEAD|POST)$" "deny,log,status:403"
</IfModule>

You can use the php request method scanner at askapache to see exactly how your server deals with various request methods, and check that your server is configured properly..

http://www.askapache.com/online-tools/request-method-scanner/
scott at chaisson dot net 20-Sep-2007 06:18
Windows 2000 Server and IIS (SP4 and all patches):

After many frustrating hours of trying to figure out the install (again), It took a while, but here are a couple tips:

PHP working with IIS uses c:\winnt\php.ini

It doesn't matter what you set the path to, nor what you set the PHPRC environment variable to. After you get PHP running correctly in the installation folder (using php -i) copy the php.ini file into the c:\winnt folder.

Here's what worked for me:

- Add PHP path to System environment variable

- Add php5activescript.dll from http://snaps.php.net/ to the PHP folder, then Register it: "regsvr32 php5activescript.dll"

Set extension_dir and doc_root directives in PHP.INI:
  extension_dir = C:\WWW\PHP\ext
  doc_root = C:\WWW\Site
  cgi.force_redirect = 0

(the last cgi.force_redirect isn't required, but I like to set it anyway)

Copy the .INI file into WINNT folder

In IIS Manager, right-click "Default Web Site", Click "Home Directory",
  Click "Configuration", "Add" the ISAPI application:
    Executable: <your php install>\php5isapi.dll
    Extension: .php
    Limit to: GET,HEAD,POST
    Check: "Script Engine"
    Uncheck: "Check that file exists"

Finally: Set up an ISAPI filter:
  Click the "ISAPI Filters" tab,
  Filter Name: PHP
  Executable: <your php install>\php5isapi.dll

Notes: You don't have to set any of your folders as "Script source" folders. .PHP files aren't scripts, merely text files that require processing before being shipped off to the browser, which is what the ISAPI filter does.

Finally, working again.

Chase
christianfelix at laszlo dot com 17-Jul-2007 12:39
For Windows XP users

Just add php.ini to windows/system 32 and also save a copy in the setup folder e.g. c:/php/php.ini, c:/php5/php.ini,

You have to follow the suggestions, add to path, uncomment extensions and voila!!
Meiki67 skipthis at geemail 08-Jun-2007 01:41
Had a hard day to MSI install 5.2.3 on WinXP with MySQL. I use a drive substitution (subst drive: path - in my case subst d: c:\drive_d) and installed PHP in D:\PHP - which in reality is C:\DRIVE_D\PHP.  To make a long story short - only after (1) changing all substituted D:\ back to C:\DRIVE_D and (2) resolving all DOS short names to Win long names PHP started to work. The first one is clear - the substitution works only for the active console user - not for services or IUSR. For the second one I have no explanation, since DOS short names should work systemwide - maybe Win handles them correctly but PHP not ?

BTW <?php phpinfo(); ?> was of tremendeous help

Meiki
gui dot priaulx at gmail dot com 03-Jun-2007 09:05
When i first installed PHP 5.2 / 5.1 on Windows 2k3 server standard edition, i got error 404's on every page i tried to load, i then noticed that in the file extention configuration list, .php files where going to E:\PHP\\php5isapi.dll or something similar, the problem was the two back slashes, i'm not sure what their purpose is. After i changed it too one, PHP worked well for about 5 minutes then the Application poop started to crash and a whole new bag of problems fell open, but thats just observation if it helps anyone.
Curtis 19-May-2007 04:39
I have some additional troubleshooting tips for 53.7, which deals with getting a blank screen on your browser when running a PHP script.

In addition to server misconfiguration, it may also be that PHP isn't configured to output errors to the browser, which is the case if you or your host uses a more secure php.ini. If this is the case, you need to find out where errors are being logged to find out if any errors are preventing normal output from being delivered to the client.

If you have access to php.ini, check the 'display_errors' directive, which is, by default, located under the 'error_reporting' directive. It's helpful to have 'display_errors' on during debugging phases, but best to have it off in production environments.

Related functions: http://php.net/ini_set | http://php.net/error_reporting | http://php.net/error_log
knutw at sparhell dot no 07-Nov-2006 06:15
I recntly upgraded from PHP 5.1 to 5.2 on my Windows Server 2003 using IIS6. I selected CGI as before. My PHP folder is as always C:\Program Files\PHP. I also edited my brand new php.ini, enabling all my old extensions and recommended settings for IIS. I like not to keep my old config into a new version.

After upgrade, I just got blank pages in my browser, and the response status was 404 Not found.

I checked my installation twice, using this FAQ, but got no way.

Then I went to IIS Manager, Web Service Extensions. The php Service Extension was Allowed as it should. I deleted the Service Extension and added it. The old extension used short file names containing tilde (PROGRA~) character, but I don'æt know if that gas anything to do with the problem. Anyway, after allowing my" new" Service Extension, pointing to php-cgi.exe as before, mye web sites suddenly worked. So will revommend anyone with thos problem to delete and add the Servcie Extension. It didn't help re-allowing it, restarting IIS or the complete server.
14-Sep-2006 04:03
After setting the PHPRC environment variable, none of my PHP files would hit the preprocessor. The browser was just serving out PHP source code right there in the "view source."

The manual was telling me to set this var to "c:\php," but nothing was working. It's almost as if IIS/Win2000 wasn't reading my php.ini at all.

After I set that variable to "c:\php\php.ini," all of a sudden I was getting honest to goodness PHP pages working.
emzeperx at emzeper dot hu 01-Jan-2006 09:36
You do not have to restart the whole system make effect of changing the PATH env var. For me (sbs2003) worked automatically.
on older systems you can use

set $Path=%path%;c:\php

command to extend immediately the path var.
JUSTANOTHERUSER 14-Aug-2005 08:23
<LimitExcept GET POST>
Deny from all
</LimitExcept>

Does not work with Apache2+ & PHP5 . Apache Refuses to start
per dot fikse at ipj dot no 12-Jul-2005 10:22
Installing PHP 4 or 5 on Windows Server 2003 x64 results in http error 505. This is not a PHP bug. It is due to running a 32-bit ISAPI DLL under the 64-bit version of IIS 6, as documented here: http://support.microsoft.com/?id=895976

Solution: Flip the Metabase flag for Enable32bitAppOnWin64 as described in http://support.microsoft.com/?id=894435 ,
like this: CSCRIPT ADSUTIL.VBS SET W3SVC/AppPools/Enable32bitAppOnWin64 1

(Alternatively with full path: cscript %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32bitAppOnWin64 1)
patrick hat paddyworks dot com 23-May-2005 06:52
In setting up PHP 5.0.4 + IIS 6  I can concur with vlad at vkelman dot com that setting the environment variable PHPRC is ineffective at telling IIS where to look for php.ini.  The only place PHP5 seems to successfully find php.ini is in C:\Windows.  Using PHPIniDir with Apache2 works like a charm, however.
barry dot fagin at usafa dot af dot mil 04-Jan-2005 06:22
Fixing 404 (file not found) errors with PHP 5.0.3 and IIS 6.0

IIS 6.0 is installed with security settings that don't permit dynamic content.  This means that even if all the hints in the FAQ are paid attention to (.ini modified correctly, security and permissions correct for IUSR_xxx user, etc), you still get 404 errors for all php files.

To fix this:  IIS Manager -> expand local computer -> Web Services Extensions -> Add web service or extension -> .php, browse to c:\php\php-cgi.exe or wherever you had the php executable installed. That plus setting the doc_root in the .ini file solved the problem.

--BF
furrycod at hotmail dot com 07-Sep-2004 06:35
Great WAMP 1.4.4 (Apache 1.*, MySql 4.*, PHP 4.*) installation article at http://www.circaware.com/articles.php?id=2

This article details how to install these onto a USB stick--which means changing *nothing* on the host PC, such as registry entries, environment variables, files in the Windows path, NT services, ini files, etc.

The USB stick install seems awkward, but I installed WAMP to my harddrive using this article as a guide (essentially replacing E:\ with C:\), and it works fine.  No more trying to decide where the *real* php.ini or my.ini file is that needs to be changed.
masternuthin diespammer at yahoo dot com 24-Jun-2004 12:33
PHP 4.3.7.7 and IIS 6 (w2k3 standard)

Followed note from nigel dot salt at hotmail dot com
but had to modify "Script Map" to "ScriptMap"

Also, had to follow note from jimo at mail dot azhsv dot com

after all that - it worked.
jeff_graham at datacenterservices dot net 01-Feb-2004 10:51
As stated within the note "jimo at mail dot azhsv dot com
29-Nov-2003 04:33
PHP 4 and IIS 6.0 (Windows Server 2003)," I found the same steps were requiered for installation of PhP 5 beta 3 on my Windows 2003 server (Standard Edition)
nigel dot salt at hotmail dot com 14-Dec-2003 02:00
Finally got IIS 5 and PHP 5 to talk.

What worked for me was:

1) Set
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\
W3SVC\Parameters\Script Map]
".php"="your path to php\\php.exe"

2) ensure there is not a php.ini in the windows system folder and that there is one wherever you've put PHP

3) edit php.ini and set cgi force redirect to 0 and cgi.rfc2616_headers = 1

4) Put the PHP scripts in their own folder underneath the inetpub root

5) Open the IIS console, right click your new php folder
In the Directory tab
 set application name to the name of the folder
 set executable and script as permission
 set application protection to low
Click configuration and check that .php is mapped to wherever you put
PHP

Restart IIS

Try a very simple PHP page and it should work

Nigel
jimo at mail dot azhsv dot com 30-Nov-2003 04:33
PHP 4 and IIS 6.0 (Windows Server 2003)

Had difficulty getting PHP 4.3.3 to work with Win2003 Ent Server for use with SquirrelMail.  Problem found and believed solved.  Appears to be something New with IIS 6.0 (More security by Micro$oft)  Was getting 404 error and php was not being run as a cgi even though it was in extensions and IIS user had rights to execute it. (php from command line operated correctly)

If you are installing on Windows Server 2003 (only have Enterprise Edition ? Not sure if this applies to standard edition), and you have done the normal procedures for inclusion of PHP scripts (either ISAPI or CGI) in IIS, you will still get a 404 error when attempting to execute those scripts even though IIS has rights and the extension points to the correct file.  To alleviate this problem, perform the following:

1.    Open a command prompt window
2.    Change to C:\windows\system32
3.    Execute the command:  cscript  iisext.vbs /AddFile c:\php\php.exe 1 php 0 php
4.    Execute the command:  cscript iisext.vbs /EnFile c:\php\php.exe
5.    To check for correct execution of the above,
6.    Execute the command:  cscript IisExt.vbs /ListFile ? the php extension should be listed

If you try just step 4 without step 3 you get an error that the file is not in the list (and won?t be there till you do step 3 above)

After this, IIS seems to like the php extension and work normally.

For additional reference, see the below 3 Micro$oft KB Articles:
HOWTO: Enable and Disable ISAPI Extensions and CGI Applications in IIS 6.0 (MS KB Article 328360)

HOWTO List Web Server Extensions and Extension Files in IIS 6.0 (MS KB Article 328505)

HOWTO Add and Remove Web Service Extension Files in IIS 6.0 (MS KB Article 328419)
peter dot mosinskisNOSPAM at csuci dot edu 06-Mar-2003 09:36
Running Win2K AS + IIS5.0 + PHP 4.3.1, I ran into a nasty problem that I mistakenly thought had to do with something in the php.ini configuration. When I would try to submit a form using the HTTP method GET it would work, but I would get a "404 Page Not Found" error using the POST method.

All my permissions were set correctly, the php.ini was configured correctly. It had to do with URLScan being installed on IIS (see below)

Page 404 File Not Found Error When Using POST method in PHP (and Perl, and otherwise)
-------------------------------------------

This is related to the Microsoft URLScan utility. If you have this problem, the IIS administrator has probably run the IIS Lockdown tool as well as configured URLScan to reject this type of HTTP method when sent in a URL. These tools are meant to enhance web server security.

URLScan configuration files can be found in the \WINNT\system32\inetsrv\urlscan folder on the web server. Specifically, the file to modify is urlscan.ini.

The changes to make are as follows:
1. Make sure UseAllowVerbs=1 under [options]
2. Under the [AllowVerbs] section, list your allowed HTTP methods to include:
    GET
    POST

For more information on the IIS Lockdown tool and URLScan, visit http://technet.microsoft.com
monroe at peoplego dot com 18-May-2002 07:32
INSTALLATION ON OPENBSD 3.1 VIA PORTS COLLECTION
Would have succeed but no mysql support.
I commented out the FLAVORS+= dbase filepro mysql mysql_bundled postgresql iodbc in the /usr/ports/www/php4/Makefile and in the configure options added
--with-mysql=/usr/local   and it worked.

Good Luck


빌드 시 문제

이 절은 빌드 시에 가장 자주 발생하는 에러에 대해서 정리합니다.

  1. 익명 CVS 서버스에서 PHP의 가장 최종 버전을 가져왔습니다. 그런데 거기에는 configure 스크립트가 없네요!?
  2. 아파치와 연동하여 PHP 를 설정하려 하는데 문제가 발생합니다. httpd.h를 찾을 수 없다는 군요. 하지만 그 파일은 거기에 있거든요!
  3. PHP를 설정하려 하는데 (./configure), 다음과 같은 에러 를 본적이 있습니까? checking lex output file root... ./configure: lex: command not found configure: error: cannot find output from lex; giving up
  4. 제가 아파치를 시작하려하면, 다음과 같은 메시지가 뜹니다. fatal: relocation error: file /path/to/libphp4.so: symbol ap_block_alarms: referenced symbol not found
  5. configure를 실행하려하는데, GD, gdbm 이나 다른 패키지에 대한 라이브러리(library)와 인클루드(include) 파일을 읽지 못한다는 에러가 나네요!
  6. language-parser.tab.c파일을 컴파일할때, yytname undeclared라는 에러메시지를 보여주네요.
  7. make명령을 실행시킬때, 처음엔 잘나가다가 마지막에서 프로그램을 링크하려고 할때 특정 파일을 찾지 못한다고 하며 종료되어 버립니다.
  8. PHP를 링크할때, 엄청나게 많은 수의 undefined reference 에러메시지를 뿌려줍니다.
  9. 아파치 1.3 버전과 같이 설치하는 방법을 잘 모르겠습니다.
  10. 유닉스에서 아파치 모듈을 인스톨하기 위한 모든 단계를 따라했습니다. 그리고 브라우저에서 PHP 스크립트를 보려고하면 저장하겠냐고 묻습니다.
  11. --activate-module=src/modules/php4/libphp4.a옵션으로 실행하라고 하지만 그 파일은 존재하지않습니다. 그래서 --activate-module=src/modules/php4/libmodphp4.a으로 변경을 했습니다. 근데, 제대로 설치가 안되네요!? 무엇이 잘못되었나요?
  12. PHP를 정적인 모듈로 아파치와 연동하려고 --activate-module=src/modules/php4/libphp4.a 옵션으로 빌드하려 했습니다. 그런데 컴파일러가 ANSI 호환이 아니라고 나옵니다.
  13. --with-apxs옵션으로 PHP를 빌드하려고 하는데 이상한 메시지를 봤습니다.
  14. make명령 실행 중에 몇몇개의 에러메시지와 많은 RUSAGE_관련 에러 메시지가 발생합니다.
  15. MySQL과 연동하여 PHP를 컴파일하려고 할때 configure는 잘 진행되지만 make명령 실행중에는 다음과 같은 에러가 납니다. ext/mysql/libmysql/my_tempnam.o(.text+0x46): In function my_tempnam': /php4/ext/mysql/libmysql/my_tempnam.c:103: the use of tempnam' is dangerous, better use mkstemp', 무엇이 잘못된 건가요?
  16. PHP를 업그레이드 하고 싶습니다. 현재 PHP 설치를 위해 사용되었던 ./configure 실행 옵션을 어디서 발견할수 있습니까?
  17. GD라이브러리와 연동하여 PHP를 빌드하려 하면 이상한 컴파일 에러를 보이거나 실행중 세그먼테이션 폴트(segfault)를 발생시킵니다.
  18. PHP를 컴파일 할 때 여러 오류가 나면서 멈춥니다. 솔라리스를 사용하고 있습니다.
익명 CVS 서버스에서 PHP의 가장 최종 버전을 가져왔습니다. 그런데 거기에는 configure 스크립트가 없네요!?

configure.in파일이 configure 스크립트를 생성할수 있도록 GNU autoconf 패키지가 설치되어 있어야 합니다. 그리고 나서 CVS 서버에서 소스를 가져오신 후에 최상위 디렉토리에서 ./buildconf명령을 실행하세요. (또한, --enable-maintainer-mode옵션으로 configure를 실행하지 않으면, configure.in파일이 갱신되었을때, configure 스크립트는 자동적으로 재생성 되지 않을겁니다. 그래서 configure.in 파일이 갱신되었다는것을 알고나서 손수 재생성해야 할것입니다. 이것을 알아챌수 있는 방법은 configure 나 config.status이 실행된후 Makefile의 @VARIABLE@같은 것을 찾아 보는 것입니다.)

아파치와 연동하여 PHP 를 설정하려 하는데 문제가 발생합니다. httpd.h를 찾을 수 없다는 군요. 하지만 그 파일은 거기에 있거든요!

configure/setup 스크립트에 아파치 소스 트리의 최상위 디렉토리 위치를 알려줘야 합니다. 즉, --with-apache=/path/to/apache 을 적어주세요. --with-apache=/path/to/apache/src 이 아닙니다.

PHP를 설정하려 하는데 (./configure), 다음과 같은 에러 를 본적이 있습니까?

checking lex output file root... ./configure: lex: command not found
configure: error: cannot find output from lex; giving up

installation섹션을 주의깊게 읽어보세요. PHP를 컴파일하기 위해서는 flex와 bison 이 필요합니다. bison과 flex를 소스나 RPM패키지를 구해서 설치하세요.

제가 아파치를 시작하려하면, 다음과 같은 메시지가 뜹니다.

fatal: relocation error: file /path/to/libphp4.so:
symbol ap_block_alarms: referenced symbol not found

이 에러는 공유 사용 목적의 DSO 라이브러리로 아파치 프로그램을 컴파일 할때 주로 발생합니다. 아파치를 재설정(reconfigure)하세요 최소한 다음 옵션이 붙어야 합니다.

--enable-shared=max --enable-rule=SHARED_CORE

더 자세한 정보는 아파치 디렉토리의 최상위에 위치한 INSTALL 파일이나 아파치 » DSO 매뉴얼 페이지를 보세요.

configure를 실행하려하는데, GD, gdbm 이나 다른 패키지에 대한 라이브러리(library)와 인클루드(include) 파일을 읽지 못한다는 에러가 나네요!

비표준적인 위치에 존재하는 헤더파일이나 라이브러리를 찾을 수 있도록 configure 스크립트를 조정할수 있습니다. C 전처리기나 링커를 넘겨주기 위한 다음과같은 플래그를 설정함으로써 가능합니다.

    CPPFLAGS=-I/path/to/include LDFLAGS=-L/path/to/library ./configure
로그인 셸이 csh계열이라면(왜?) 다음처럼 처리합니다
    env CPPFLAGS=-I/path/to/include LDFLAGS=-L/path/to/library ./configure

language-parser.tab.c파일을 컴파일할때, yytname undeclared라는 에러메시지를 보여주네요.

Bison 을 업그레이드할필요가 있습니다. » http://www.gnu.org/software/bison/bison.html에서 최신버전을 받을수 있을겁니다.

make명령을 실행시킬때, 처음엔 잘나가다가 마지막에서 프로그램을 링크하려고 할때 특정 파일을 찾지 못한다고 하며 종료되어 버립니다.

make의 구버전 중 일부는 functions 디렉토리안에 functions 디렉토리에 들어있는 파일을 정확하게 옮겨놓지 못합니다. cp *.o functions 명령을 실행하고 나서 make명령을 재실행시켜보세요. 이래도 똑같은 증상을 보이면, GNU make를 최신버전으로 업그레이드하세요

PHP를 링크할때, 엄청나게 많은 수의 undefined reference 에러메시지를 뿌려줍니다.

링크 관련 줄을 보고 정확하게 라이브러리가 설정되었는지 확인하기 바랍니다. 보통 '-ldl'이라는 옵션을 빠뜨리거나, 데이터베이스 지원을 위한 라이브러리를 빠뜨린 경우가 대부분입니다.

어떤분들의 보고에 따르면 아파치와 같이 설치할때에는 libphp4.a파일 다음에 '-ldl'옵션을 추가해야한다고 합니다.

아파치 1.3 버전과 같이 설치하는 방법을 잘 모르겠습니다.

매우 쉽습니다. 다음 단계를 차근차근 따라해보세요.

  • » http://httpd.apache.org/download.cgi에서 아파치 1.3 최신 버전을 다운받는다.
  • 그 파일을 특정 디렉토리에 압축을 풉니다. 예를 들면, /usr/local/src/apache-1.3와 같은 디렉토리가 될수 있습니다.
  • 우선 다음명령을 실행시킵니다. ./configure --with-apache=/<path>/apache-1.3 (여기서 <path>는 아파치 1.3 디렉토리의 실제 경로로 대치하십시오.
  • 그리고 make명령과 make install명령을 차례대로 실행시킵니다. 이것이 완료되면 아파치의 하위 디렉토리에 필요한 파일들이 복사가 되는것입니다.
  • /<path>/apache-1.3/src디렉토리로 들어가서 Configuration파일을 변경하십시오. 설정 파일에 다음줄을 추가하세요. AddModule modules/php4/libphp4.a.
  • Type: ./configure명령실행후에 make명령을 실행하면 됩니다.
  • 이제 PHP가 연동되는 아파치 실행파일이 생겼을겁니다!

Note: 새로운 아파치의 ./configure스크립트 를 사용할수 있습니다. 아파치 디렉토리의 README.configure파일을 보거나, PHP 디렉토리의 INSTALL파일을 참고하세요.

유닉스에서 아파치 모듈을 인스톨하기 위한 모든 단계를 따라했습니다. 그리고 브라우저에서 PHP 스크립트를 보려고하면 저장하겠냐고 묻습니다.

이것은 어떤 이유로 PHP 모듈을 제대로 불려오지 못한 결과입니다. 우선은 다음의 세가지 경우를 체크하시기 바랍니다

  • 현재 실행되고있는 아파치의 실행파일이 새로 빌드된 실행파일인지 확인해야 합니다. 다음 명령을 실행하세요 /path/to/binary/httpd -l 출력된 리스트중에서 mod_php4.c파일이 보이지 않으면, 잘못된 파일을 실행하고 있는것입니다. 올바른 파일을 찾아서 실행하세요.
  • 아파치의 .conf파일에 다음과 같은 Mime 타입 설정을 추가했는지 확인하세요. AddType application/x-httpd-php .php 그리고 <Virtualhost> 이나 <Directory> 블록안에 이 AddType 줄이 숨겨져 있는지 확인하시기 바랍니다. 이렇게 되면 PHP 스크립트의 위치에 따라서 적용이 안될수 있기 때문입니다.
  • 마지막으로, 아파치 1.2와 아파치 1.3 버전간에 설정파일의 기본(default) 디렉토리가 바뀔수도 있습니다. 따라서 실질적으로 AddType 줄이 추가된 설정파일이 정확한 위치에 있는지 체크해야 합니다. httpd.conf파일에 일부러 문법에러(Syntax error)를 일으킬만한 설정을 해보거나 다른 방법으로 이 파일이 실제로 적용되는 파일인지 확인할수 있습니다.

--activate-module=src/modules/php4/libphp4.a옵션으로 실행하라고 하지만 그 파일은 존재하지않습니다. 그래서 --activate-module=src/modules/php4/libmodphp4.a으로 변경을 했습니다. 근데, 제대로 설치가 안되네요!? 무엇이 잘못되었나요?

처음에는 libphp4.a파일은 존재하지 않습니다. 그 파일은 아파치 설치 과정중에 생기는것입니다!

PHP를 정적인 모듈로 아파치와 연동하려고 --activate-module=src/modules/php4/libphp4.a 옵션으로 빌드하려 했습니다. 그런데 컴파일러가 ANSI 호환이 아니라고 나옵니다.

이 에러 메시지는 잘못된 것입니다. 요즘의 아파치버전은 이런 에러 메시지가 수정되었다고 합니다.

--with-apxs 옵션으로 PHP를 빌드하려고 하는데 이상한 메시지를 봤습니다.

이경우에는 다음 세가지 경우를 확인해봐야 합니다. 첫째, 아파치가 apxs 펄 스크립트를 빌드 할때, 어떤 이유로 종종 적절한 컴파일러와 플래그 변수(flag variables)들 없이 끝나버리는 경우가 있습니다. ( which apxs 명령을수행) 대부분 /usr/local/apache/bin/apxs나 /usr/sbin/apxs 인 경우가 많습니다. 그 파일을 열어서 다음과 비슷한지 점검하십시오.

my $CFG_CFLAGS_SHLIB  = ' ';          # substituted via Makefile.tmpl
my $CFG_LD_SHLIB      = ' ';          # substituted via Makefile.tmpl
my $CFG_LDFLAGS_SHLIB = ' ';          # substituted via Makefile.tmpl
이렇게 되어있다면 이것이 바로 문제입니다. 이외에 공백이나 잘못된 값이 채워져있을수 있습니다. 예를 들어서 'q()' 같은 경우입니다. 이 줄들은 다음으로 변경합니다
my $CFG_CFLAGS_SHLIB  = '-fpic -DSHARED_MODULE'; # substituted via Makefile.tmpl
my $CFG_LD_SHLIB      = 'gcc';                   # substituted via Makefile.tmpl
my $CFG_LDFLAGS_SHLIB = q(-shared);              # substituted via Makefile.tmpl
두번째는 레드햇 6.1과 레드햇 6.2에 관련된 경우입니다. 레드햇 배포판의 apxs가 깨지는 경우인데. 다음처럼 되어있습니다
my $CFG_LIBEXECDIR    = 'modules';         # substituted via APACI install
이렇다면 다음으로 변경합니다
my $CFG_LIBEXECDIR    = '/usr/lib/apache'; # substituted via APACI install
마지막으로, 아파치를 재설정/재설치했을경우에 make clean 명령을 ./configure 명령 후와 make 명령 전에 실행해보는 것입니다.

make명령 실행 중에 몇몇개의 에러메시지와 많은 RUSAGE_관련 에러 메시지가 발생합니다.

make명령 실행 중에 다음과 비슷한 상황이 벌어진다면:

microtime.c: In function `php_if_getrusage':
microtime.c:94: storage size of `usg' isn't known
microtime.c:97: `RUSAGE_SELF' undeclared (first use in this function)
microtime.c:97: (Each undeclared identifier is reported only once
microtime.c:97: for each function it appears in.)
microtime.c:103: `RUSAGE_CHILDREN' undeclared (first use in this function)
make[3]: *** [microtime.lo] Error 1
make[3]: Leaving directory `/home/master/php-4.0.1/ext/standard'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/home/master/php-4.0.1/ext/standard'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/master/php-4.0.1/ext'
make: *** [all-recursive] Error 1

당신의 시스템은 정상이 아닙니다. glibc-devel 패키지를 설치해서 /usr/include밑의 파일들을 고칠필요가 있습니다. 이것이 PHP를 위해 절대적인것은 아닙니다. 이런것을 해결하기 위해서는 다음과 같은 테스트를 해보세요.

$ cat >test.c <<X
#include <sys/resource.h>
X
$ gcc -E test.c >/dev/null
이 테스트 결과 에러가 발생한다면 그 include파일들은 확실히 정상이 아닙니다.

MySQL과 연동하여 PHP를 컴파일하려고 할때 configure는 잘 진행되지만 make명령 실행중에는 다음과 같은 에러가 납니다. ext/mysql/libmysql/my_tempnam.o(.text+0x46): In function my_tempnam': /php4/ext/mysql/libmysql/my_tempnam.c:103: the use of tempnam' is dangerous, better use mkstemp', 무엇이 잘못된 건가요?

우선, 이것이 경고(Warning)이고 치명적인 에러(Fatal Error)가 아니라는 것을 파악하는 것이 중요합니다. 이 메시지들이 make명령이 실행되는 동안 종종 마지막에 나오기 때문에 치명적인 에러(Fatal Error)처럼 보일지도 모르지만, 그렇지는 않습니다. 즉 컴파일러가 경고(Warning)메시지를 안보이도록 할수 있다면, 아무 메시지도 보여주지 않게 되는것입니다. 또 명심할것은 MySQL 은 기본적으로(default) 지원이 된다는것입니다.

Note:

PHP 4.3.2부터, 빌드(make)가 완료된 후 다음 텍스트를 보게 됩니다:

Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).

PHP를 업그레이드 하고 싶습니다. 현재 PHP 설치를 위해 사용되었던 ./configure 실행 옵션을 어디서 발견할수 있습니까?

현재 PHP설치 디렉토리의 config.nice 파일이나 다음스크립트를 실행시키면 확인할수 있습니다.

<?php phpinfo(); ?>
이 스크립트의 결과 페이지에서 상단에 ./configure실행 옵션이 보이므로 그 옵션을 쓰시면 됩니다.

GD라이브러리와 연동하여 PHP를 빌드하려 하면 이상한 컴파일 에러를 보이거나 실행중 세그먼테이션 폴트(segfault)를 발생시킵니다.

GD라이브러리와 PHP가 관련 라이브러리(예를 들면, libpng)와 제대로 링크가 되었는지 확인하십시오.

PHP를 컴파일 할 때 여러 오류가 나면서 멈춥니다. 솔라리스를 사용하고 있습니다.

PHP를 컴파일 할 때 GNU 유틸리티를 사용하지 않으면 문제가 발생할 수 있습니다. PHP를 컴파일할 때 적합한 GNU 툴을 사용하는지 확인해 보십시오. 예를 들어, 솔라리스에서 사용하는 SunOS BSD-호환이나 솔라리스 버전의 sed는 적합하지 않습니다. GNU나 Sub POSIX (xpg4) 버전의 sed을 사용해야 합니다. 연결: » GNU sed, » GNU flex, » GNU bison.

User Contributed Notes

Fabio 22-Feb-2012 10:47
When building PHP 5.3.x for Apache 2.4.x you may get an error in the apxs query for the MPM_NAME.
This is because the apxs included in Apache 2.4.x doesn't recognize that query anymore.

To resolve this issue one should modify the PHP configure file to use the right MPM module used by Apache.

To know the used MPM you can execute this command: apachectl -t -D DUMP_MODULES | grep mpm

Then, edit the PHP configure file, search for the APXS_MPM variable and force its value to prefork, event or worker according to the value returned by the previous command.

Hope it helps.
-
Fabio
terry at mackintoshweb dot com 20-Sep-2009 05:43
The configure script of PHP 5.3.0 has some test lines that use expr with the option --, my expr (version 2.0) does not except --.  This causes a run of error messages from the shell like:

./configure: line 2xxx: test: =: unary operator expected
expr: syntax error

I used buildconf --force to see if it would fix this.
While it resulted in a quit different file, it still had the lines of code with expr -- in it.
05-May-2007 03:48
@ anca-phpdoc at anca dot tv:

You can use ./configure --with-libxml-dir=/path_to_xml2-config
anca-phpdoc at anca dot tv 09-Dec-2005 04:48
For the configure newbies among us:

If you update or reinstall any of the libraries used to compile in a different directory than they started out, you will need to make sure that you update the config.cache file (or re-generate it) so that configure will not look in the wrong place for the information.

On Mac OS X, for example, I updated my libxml using Fink.  Fink placed the files in the /sw directory.  However, php was still looking for important libxml files (such as xml2-config) in the old directory (/usr/bin/xml2-config).   After updating config.cache with the new value of the xml2-config path, I was able to compile correctly.


PHP 사용하기

이 섹션은 PHP스크립트를 작성할때 직면할수 있는 매우 일반적인 에러를 정리했습니다.

  1. 폼으로부터 데이터를 제어할수 있는 PHP스크립트를 작성하려합니다. POST 메쏘드에서 사용가능한 변수를 알 수 있을까요?
  2. 작은따옴표(')에 역슬래시를 앞에(\') 붙여줄 필요가 있습니다. 이와 같은 일은 어떻게 정규표현식(regular expression)으로 처리할수 있을까요? 그리고 "를 \"로, \를 \\로 변환하고 싶습니다.
  3. All my " turn into \" and my ' turn into \', how do I get rid of all these unwanted backslashes? How and why did they get there? "은 \"으로, '는 \'으로 변경이 되어있습니다. 어떻게하면 이런 원치않는 역슬래시를 제거할수 있을까요? 어떻게 그리고 왜 이런 역슬래시가 생긴거죠?
  4. PHP 지시어 register_globals가 어떤 영향을 미칩니까?
  5.       다음과 같은 처리를 할때, 출력이 잘못된 순서로 됩니다:

<?php
function myfunc($argument)
{
    echo $argument + 10;
}
$variable = 10;
echo "myfunc($variable) = " . myfunc($variable);
?>

     무엇이 잘못된건가요?
  6. 헤이, 새줄(newline)이 왜 안생기나요? <pre> <?php echo "This should be the first line."; ?> <?php echo "This should show up after the new line above."; ?> </pre>
  7. 'Warning: Cannot send session cookie - headers already sent...' 나 'Cannot add header information - headers already sent...'같은 메시지를 봤습니다.
  8. 요청 헤더값을 직접 확인하고 싶습니다. 어떻게 하면 되죠?
  9. IIS로 인증을 하려합니다. 그런데 'No Input file specified'과 같은 메시지가 출력되는군요.
  10. 윈도우: IIS를 사용하여 다른 컴퓨터에 공유한 파일에 접근할 수 없습니다.
  11. PHP 스크립트는 IE와 Lynx에서는 동작을 합니다. 그러나 네스케이프에서는 출력의 일부가 사라져버립니다. "소스보기"를 하면 IE에서는 볼수 있지만 네스케이프에서는 볼수없습니다.
  12. 어떻게 XML과 PHP를 섞어쓸수 있다고 생각할수 있습니까? <?xml태그에 불만을 터트린단 말입니다!
  13. 이미 짜여진 코드를 옮기기 위해서 FrontPage나 다른 HTML 편집기를 어떻게 사용할수 있습니까?
  14. PHP에서 가용한 모든 변수 목록을 어디서 볼수 있습니까?
  15. 유료이고 상용 라이브러리인 PDFLib 등을 사용하지 않고 어떻게 하면 PDF 파일을 만들수 있습니까? 무료이면서 외부의 PDF 라이브러리가 필요없는 것을 사용하고 싶습니다.
  16. 유저 정의 함수에서 표준적인 CGI변수($DOCUMENT_ROOT나 $HTTP_REFERER)중 하나에 접근하여하였습니다. 그런데 그 변수를 찾을수가 없는것처럼 보입니다. 무엇이 잘못된건가요?
  17. 몇몇 PHP 지시어는 integer 바이트 값뿐이 아닌, 단축 바이트 값도 가질 수 있습니다. 어떠한 단축 바이트 옵션이 존재합니까? 그리고 php.ini 밖에서 사용할 수 있습니까?
폼으로부터 데이터를 제어할수 있는 PHP스크립트를 작성하려합니다. POST 메쏘드에서 사용가능한 변수를 알 수 있을까요?

PHP는 많은 미리 정의된 변수들을 가집니다. 슈퍼 글로벌(superglobal)변수인 $_POST처럼요. $_POST변수는 포스팅된 모든 변수의 연관배열이기 때문에 이 변수로 루프를 돌릴수도 있다. 예를 들어서 간단하게 foreach로 루프를 돌린다고 하면 empty()함수 값을 체크하고 그 값을 출력하면 된다.

<?php
$empty = $post = array();
foreach ($_POST as $varname => $varvalue) {
    if (empty($varvalue)) {
        $empty[$varname] = $varvalue;
    } else {
        $post[$varname] = $varvalue;
    }
}

print "<pre>";
if (empty($empty)) {
    print "None of the POSTed values are empty, posted:\n";
    var_dump($post);
} else {
    print "We have " . count($empty) . " empty values\n";
    print "Posted:\n"; var_dump($post);
    print "Empty:\n";  var_dump($empty);
    exit;
}
?>

Note: 자동 전역: 사용상 주의

$_GET, $_POST, $_SERVER 등의 자동 전역 배열은 PHP 4.1.0부터 사용할 수 있습니다. 자세한 내용은 매뉴얼의 superglobals 섹션을 읽어보십시오.

작은따옴표(')에 역슬래시를 앞에(\') 붙여줄 필요가 있습니다. 이와 같은 일은 어떻게 정규표현식(regular expression)으로 처리할수 있을까요? 그리고 "를 \"로, \를 \\로 변환하고 싶습니다.

addslashes()함수로 이와 같은 일을 처리할수있습니다.
mysql_escape_string()함수도 참고하십시오.
또한 stripslashes()함수로 역슬래시를 벗길 수 있습니다.

Note: 지시어 주의: magic_quotes_gpc

지시어 magic_quotes_gpc의 기본값은 on입니다. 이것은 모든 GET, POST, COOKIE 데이터에 대해 addslashes()를 실행합니다. 이를 제거하기 위해서 stripslashes()를 사용할 수 있습니다.

All my " turn into \" and my ' turn into \', how do I get rid of all these unwanted backslashes? How and why did they get there? "은 \"으로, '는 \'으로 변경이 되어있습니다. 어떻게하면 이런 원치않는 역슬래시를 제거할수 있을까요? 어떻게 그리고 왜 이런 역슬래시가 생긴거죠?

stripslashes()함수로 이 string에서 이런 역슬래시를 벗길수가 있습니다. 이런 마술적인 역슬래시가 존재하는 이유는 magic_quotes_gpc옵션이 활성화되어 있기 때문입니다.

Note: 지시어 주의: magic_quotes_gpc

지시어 magic_quotes_gpc의 기본값은 on입니다. 이것은 모든 GET, POST, COOKIE 데이터에 대해 addslashes()를 실행합니다. 이를 제거하기 위해서 stripslashes()를 사용할 수 있습니다.

PHP 지시어 register_globals가 어떤 영향을 미칩니까?

우선, 이 ini 설정이 무엇을 하는지 설명해야 합니다. URL http://example.com/foo.php?animal=cat이 있다고 하고, foo.php은 다음 PHP 코드를 가지고 있다고 합시다:

<?php
// $_GET 을 사용하는게 좋습니다.
echo $_GET['animal'];

// $animal이 존재하려면, register_globals가 on이여야 합니다.
// 이렇게 하지 마십시오!!
echo $animal;

// 이는 모든 변수에 적용되므로, $_SERVER도 동일합니다.
echo $_SERVER['PHP_SELF'];

// 다시 말하지만, $PHP_SELF가 존재하려며느 register_globals가 on이여야 합니다.
// 이렇게 하지 마십시오!!
echo $PHP_SELF;
?>

위 코드가 register_globals가 많은 변수를 만들어 내는 것을 보여줍니다. 몇 년간 이러한 코딩이 문제가 되어, 몇 년간 기본값으로 꺼버렸습니다. PHP 6에서는 이 배제된 기능을 없앨 것입니다. 주요 웹 호스트가 register_globals를 사용하지 않지만, 아직도 오래된 문서, 튜토리얼, 책이 on을 요구하고 있습니다. 적절히 대처하십시오.

추가 정보는 다음을 참고하십시오:

  • register_globals 지시어
  • regieter globals에 대한 보안 챕터
  • 외부 변수 다루기
  • 자동 전역 사용하기

Note:

위 예제에서, QUERY_STRING을 포함한 URL을 사용했습니다. 이러한 방식으로 정보를 넘기는 것은 GET HTTP 요청이기 때문에, 자동 전역 $_GET을 사용하였습니다.

다음과 같은 처리를 할때, 출력이 잘못된 순서로 됩니다:
<?php
function myfunc($argument)
{
    echo $argument + 10;
}
$variable = 10;
echo "myfunc($variable) = " . myfunc($variable);
?>
무엇이 잘못된건가요?

이 함수의 결과를 사용하기 위해서는 (위 예처럼 다른 스트링과 합치는) echo 함수대신에 return를 사용해야 합니다.

헤이, 새줄(newline)이 왜 안생기나요?
<pre>
<?php echo "This should be the first line."; ?>
<?php echo "This should show up after the new line above."; ?>
</pre>

PHP에서는 코드 블럭이 "?>" 나 "?>\n"로 끝날수 있습니다. 그래서 위 예에서 문장은 한줄로 출력이 될것입니다. 왜냐하면 블럭의 끝에 새줄표시(newline)를 빠뜨렸기 때문이요. 이와 같은 이유로 새줄쓰기를 하려면 각 블럭의 끝에 새줄표시(newline)를 해줘야합니다.

왜 이와같은 일을 할까요? 왜냐하면 일반적인 HTML을 만들때, 새줄을 쓰지 않고 매우 긴 줄을 만들거나 이런 효과를 이용해서 읽을수 없는 소스 페이지 를 만들어야 할때도 있기 때문이고,이는 당신의 삶을 좀더 안락하게 해줍니다.

'Warning: Cannot send session cookie - headers already sent...' 나 'Cannot add header information - headers already sent...'같은 메시지를 봤습니다.

header()함수, setcookie()함수, 세션 함수는 헤더에 출력을 합니다. 그러나 헤더는 모든 다른 컨텐트 전에 와야 합니다. 이 함수를 사용하기 전에 어떤 출력(HTML같은)도 있어서는 안됩니다. headers_sent()함수가 스크립트에서 이미 헤더를 보냈는지 체크해줍니다. Output Control Functions도 참고하세요.

요청 헤더값을 직접 확인하고 싶습니다. 어떻게 하면 되죠?

아파치 모듈로 PHP설치하셨다면 getallheaders()함수로 이와 같은 일을 할수 있습니다. 다음과 같은 코드로 요청 헤더의 모든값을 확인하실수 있습니다:

<?php
$headers = getallheaders();
foreach ($headers as $name => $content) {
    echo "headers[$name] = $content<br />\n";
}
?>

apache_lookup_uri(),
apache_response_headers(),
fsockopen()함수도 참고하세요.

IIS로 인증을 하려합니다. 그런데 'No Input file specified'과 같은 메시지가 출력되는군요.

IIS의 보안 모델은 여기서 결함이 있습니다. 이와같은 결함은 IIS하에서 실행되는 모든 CGI 프로그램에 있어서는 일반적인 현상입니다. 이와같은 에러를 피하는 방법으로는 일반 HTML 파일(PHP에 의해 파싱되지 않음)을 인증이 필요한 디렉토리 안에 엔트리 페이지로 만들어넣는것입니다. 그리고 나서 PHP 페이지로 리디렉트(redirect)하는 META 태그를 사용하거나, PHP 페이지로 링크를 거십시오. 이와같은 현상은 다른 NT 웹서버와는 상관이 없습니다. 더 많은 정보를 위해서 아래 링크로 가세요: » http://support.microsoft.com/kb/q160422/ 과 HTTP Authentication의 매뉴얼 섹션.

윈도우: IIS를 사용하여 다른 컴퓨터에 공유한 파일에 접근할 수 없습니다.

Go to Internet Information Services를 변경해야 합니다. PHP 파일 위치에 가서, 속성을 여십시오. File Security 탭에서, Edit -< Anonymous access and authentication control로 가십시오.

이 문제를 Anonymous Access을 끄고 Integrated Window Authentication을 켜두거나, Anonymous Access을 켜두고 접근 권한이 없는 사용자를 편집하여 해결할 수 있습니다.

PHP 스크립트는 IE와 Lynx에서는 동작을 합니다. 그러나 네스케이프에서는 출력의 일부가 사라져버립니다. "소스보기"를 하면 IE에서는 볼수 있지만 네스케이프에서는 볼수없습니다.

네스케이프는 HTML 태그(테이블같은)에 관한한 IE보다 엄격합니다. » validator.w3.org와 같은 HTML validator로 HTML 출력을 검증한다면 무엇이 문제인지 도움을 받을수 있습니다. 예를 들면, </table> 태그가 빠지면 이와같은 문제가 발생할수 있습니다.

또한 IE와 Lynx 모두 HTML 스트림(stream)에서 널문자(\0)를 무시 하지만, 네스케이프는 그렇지 않습니다. 이 널문자를 체크하기위한 최고의 방법은 PHP의 커맨드 라인(command line)버전 (또는 CGI버전으로도 알려져있슴)으로 컴파일해서 그 커맨드라인에서 스크립트를 실행해 보는것입니다. *nix에서 스크립트를 od -c로 파이프(pipe)해서 \0문자가 있는지 찾아보는것입니다. 윈도우상에서는 에디터나 바이너리 파일을 볼수 있는 다른 프로그램이 있어야 합니다. 네스케이프는 파일 안에서 널값을 봤을때 표준적으로 그 라인안의 어떤것도 출력하지 않습니다. 반면에 IE와 Lynx는 출력해줍니다.

어떻게 XML과 PHP를 섞어쓸수 있다고 생각할수 있습니까? <?xml태그에 불만을 터트린단 말입니다!

<?xml태그를 PHP코드안에 직접 넣기 위해서는 short_open_tags PHP 디렉티브 값을 0으로 설정함으로써 짧은 태그(short tags)를 비활성화해야 합니다. ini_set()함수로 이 디렉티브를 설정할수 없습니다. short_open_tags가 on이든 off든 관계없이 다음처럼 할수도 있습니다: <?php echo '<?xml'; ?> 이 디렉티브의 기본값(default)은 on입니다.

이미 짜여진 코드를 옮기기 위해서 FrontPage나 다른 HTML 편집기를 어떻게 사용할수 있습니까?

가장 쉬운 방법은 PHP코드안에서 ASP 태그 사용하기를 활성화시키는 것입니다. 이렇게 하면 <%와 %>의 ASP 스타일 코드 경계기호(delimiter)를 사용할 수 있게해줍니다. 몇몇 유명한 HTML 펴집기는 매우 지능적으로 이런 태그를 다루어줍니다(현재까지는). ASP 스타일 태그를 활성화시키려면 php.ini의 asp_tags를 활성화시키거나, 적절한 아파치 디렉티브(directive)를 사용해야합니다.

PHP에서 가용한 모든 변수 목록을 어디서 볼수 있습니까?

미리 정의된 변수에 관한 매뉴얼 페이지를 보십시오. 이 페이지에서는 일부의 미리 정의된 변수 목록이 포함되어있습니다. 모든 변수 목록은 phpinfo()함수를 호출함으로써 확인하실수 있습니다. PHP밖에서 온 변수들의 매뉴얼 섹션을 읽으십시오. 그페이지에서는 HTML폼, 쿠키, URL같은 외부변수에 관한 일반적인 시나리오를 설명해줍니다.

Note: register_globals: 중요 경고

PHP 4.2.0부터 PHP 지시어 register_globals의 기본값은 off입니다. PHP 커뮤니티에서는 이 지시어에 의존하지 않는 superglobals 방식의 사용을 권합니다.

유료이고 상용 라이브러리인 PDFLib 등을 사용하지 않고 어떻게 하면 PDF 파일을 만들수 있습니까? 무료이면서 외부의 PDF 라이브러리가 필요없는 것을 사용하고 싶습니다.

There are a few alternatives written in PHP such as » FPDF and » TCPDF.

There is also the Haru extension that uses the free libHaru external library.

유저 정의 함수에서 표준적인 CGI변수($DOCUMENT_ROOT나 $HTTP_REFERER)중 하나에 접근하여하였습니다. 그런데 그 변수를 찾을수가 없는것처럼 보입니다. 무엇이 잘못된건가요?

PHP 디렉티브 register_globals은 서버와 환경변수에 모두 영향을 미친다는것을 반드시 이해해야 합니다. register_globals = off (PHP 4.2.0버전 이래로 off가 디폴트)일때는 $DOCUMENT_ROOT변수는 존재하지 않을것입니다. 대신에 $_SERVER['DOCUMENT_ROOT']변수를 사용합니다. register_globals = on 이라면 $DOCUMENT_ROOT변수와 $GLOBALS['DOCUMENT_ROOT']변수 모두 존재할것입니다.

분명히 register_globals = on 인데, 함수 내에서 $DOCUMENT_ROOT 변수를 쓸수 없는 이유는 이 변수가 다른 변수와 같은 것이고 함수내에서는 global $DOCUMENT_ROOT와 같이 선언해야 합니다. 이와 더불어 변수 사용 범위 매뉴얼 페이지를 참고하세요. register_globals = off으로 놓는것이 바람직합니다.

Note: 자동 전역: 사용상 주의

$_GET, $_POST, $_SERVER 등의 자동 전역 배열은 PHP 4.1.0부터 사용할 수 있습니다. 자세한 내용은 매뉴얼의 superglobals 섹션을 읽어보십시오.

몇몇 PHP 지시어는 integer 바이트 값뿐이 아닌, 단축 바이트 값도 가질 수 있습니다. 어떠한 단축 바이트 옵션이 존재합니까? 그리고 php.ini 밖에서 사용할 수 있습니까?

사용할 수 있는 옵션은 K(킬로바이트), M(메가바이트), G(기가바이트; PHP 5.1.0부터 사용가능)입니다. 대소문자는 구별하지 않습니다. 1M은 1 메가바이트, 즉 1048576 바이트와 동일합니다. 1K은 1 킬로바이트, 즉 1024 바이트와 동일합니다. 이 단축어는 php.ini 밖에서는 사용할 수 없습니다. 대신 바이트의 integer 값을 사용하십시오. 이러한 값을 어떻게 변경하는 지에 대해서는 ini_get() 문서를 참고하십시오.

User Contributed Notes

robjbrain at gmail dot com 11-Oct-2016 01:37
There is no error message given if you accidentally use KB, MB or GB instead of K, M or G. From what I can tell the alphabetic characters will be ignored and it will be treated as bytes, so 1GB actually equals 1 Byte. phpinfo() and ini_get() will report what you have written e.g. 1GB, so it will not be clear that there is anything wrong with your setting even though there is.
forlfa at aol dot com 09-Jan-2016 03:14
/*Array function parameters are ordered as "needle, haystack" whereas String functions are the opposite, so "haystack, needle".*/

I'm putting that at the top of every .php file I own.
thierrybo 10-May-2015 10:42
Using php 5.4.27 memory_limit = -1 does not give unlimited memory but actually 10485760 bytes (or 10M)
info at zankmusic dot com 09-May-2009 06:10
Just spent a couple days on this one...
As previously posted, if you set
upload_max_filesize = 0.25M, the value reads correctly in phpinfo(), BUT it is effectively ZERO when implemented.

Note that the following also give ZERO bytes:

upload_max_filesize =  10M  (note the two white spaces before 10M), but the phpinfo() reads '10M' and everything looks ok.

I removed all white spaces in my php.ini file and viola...working fine again.
greenie2600 at yahoo dot com 10-Oct-2007 07:31
Here's a simple function that will convert the shorthand values described in point 64.16 to a number of bytes.

I use this to display the maximum size of file uploads to the user, so they don't waste time uploading a huge file only to find that it's too big. (I've been unable to find any browsers that actually support the MAX_FILE_SIZE technique described in chapter 38, and it's certainly not part of any W3C spec, so this is the next best thing.)

Here's how you'd use my function for that purpose (though you might want to abstract this to a function of its own):

<?php
echo 'Maximum file size: ' . convertBytes( ini_get( 'upload_max_filesize' ) ) / 1048576 . 'MB';
?>

And here's the function:

<?php
/**
 * Convert a shorthand byte value from a PHP configuration directive to an integer value
 * @param    string   $value
 * @return   int
 */
function convertBytes( $value ) {
    if ( is_numeric( $value ) ) {
        return $value;
    } else {
        $value_length = strlen( $value );
        $qty = substr( $value, 0, $value_length - 1 );
        $unit = strtolower( substr( $value, $value_length - 1 ) );
        switch ( $unit ) {
            case 'k':
                $qty *= 1024;
                break;
            case 'm':
                $qty *= 1048576;
                break;
            case 'g':
                $qty *= 1073741824;
                break;
        }
        return $qty;
    }
}
?>
kjn 02-Jul-2007 10:46
64.16 Byte Values

The number that precedes K, M, or G should be an integer.

E.g. this php.ini directive:
   post_max_size = 0.25M
is interpreted as 0, and thus prevents all POSTs.

It is particularly unhelpful that phpinfo() reports the string value assigned to post_max_size (0.25M), and not the numerical value that PHP extracts from that string (0M).


Safe Password Hashing

This section explains the reasons behind using hashing functions to secure passwords, as well as how to do so effectively.

  1. Why should I hash passwords supplied by users of my application?
  2. Why are common hashing functions such as md5 and sha1 unsuitable for passwords?
  3. How should I hash my passwords, if the common hash functions are not suitable?
  4. What is a salt?
  5. How do I store my salts?
Why should I hash passwords supplied by users of my application?

Password hashing is one of the most basic security considerations that must be made when designing any application that accepts passwords from users. Without hashing, any passwords that are stored in your application's database can be stolen if the database is compromised, and then immediately used to compromise not only your application, but also the accounts of your users on other services, if they do not use unique passwords.

By applying a hashing algorithm to your user's passwords before storing them in your database, you make it implausible for any attacker to determine the original password, while still being able to compare the resulting hash to the original password in the future.

It is important to note, however, that hashing passwords only protects them from being compromised in your data store, but does not necessarily protect them from being intercepted by malicious code injected into your application itself.

Why are common hashing functions such as md5() and sha1() unsuitable for passwords?

Hashing algorithms such as MD5, SHA1 and SHA256 are designed to be very fast and efficient. With modern techniques and computer equipment, it has become trivial to "brute force" the output of these algorithms, in order to determine the original input.

Because of how quickly a modern computer can "reverse" these hashing algorithms, many security professionals strongly suggest against their use for password hashing.

How should I hash my passwords, if the common hash functions are not suitable?

When hashing passwords, the two most important considerations are the computational expense, and the salt. The more computationally expensive the hashing algorithm, the longer it will take to brute force its output.

PHP 5.5 provides a native password hashing API that safely handles both hashing and verifying passwords in a secure manner. There is also » a pure PHP compatibility library available for PHP 5.3.7 and later.

Another option is the crypt() function, which supports several hashing algorithms in PHP 5.3 and later. When using this function, you are guaranteed that the algorithm you select is available, as PHP contains native implementations of each supported algorithm, in case one or more are not supported by your system.

The suggested algorithm to use when hashing passwords is Blowfish, which is also the default used by the password hashing API, as it is significantly more computationally expensive than MD5 or SHA1, while still being scalable.

Note that if you are using crypt() to verify a password, you will need to take care to prevent timing attacks by using a constant time string comparison. Neither PHP's == and === operators nor strcmp() perform constant time string comparisons. As password_verify() will do this for you, you are strongly encouraged to use the native password hashing API whenever possible.

What is a salt?

A cryptographic salt is data which is applied during the hashing process in order to eliminate the possibility of the output being looked up in a list of pre-calculated pairs of hashes and their input, known as a rainbow table.

In more simple terms, a salt is a bit of additional data which makes your hashes significantly more difficult to crack. There are a number of services online which provide extensive lists of pre-computed hashes, as well as the original input for those hashes. The use of a salt makes it implausible or impossible to find the resulting hash in one of these lists.

password_hash() will create a random salt if one isn't provided, and this is generally the easiest and most secure approach.

How do I store my salts?

When using password_hash() or crypt(), the return value includes the salt as part of the generated hash. This value should be stored verbatim in your database, as it includes information about the hash function that was used and can then be given directly to password_verify() or crypt() when verifying passwords.

The following diagram shows the format of a return value from crypt() or password_hash(). As you can see, they are self-contained, with all the information on the algorithm and salt required for future password verification.

The components of the value returned by password_hash and crypt: in order, the chosen algorithm, the algorithm's options, the salt used, and the hashed password.

User Contributed Notes

swardx at gmail dot com 30-Jun-2016 06:36
A great read..

https://nakedsecurity.sophos.com/2013/11/20/serious-security-how-to-store-your-users-passwords-safely/

Serious Security: How to store your users' passwords safely

In summary, here is our minimum recommendation for safe storage of your users' passwords:

    Use a strong random number generator to create a salt of 16 bytes or longer.
    Feed the salt and the password into the PBKDF2 algorithm.
    Use HMAC-SHA-256 as the core hash inside PBKDF2.
    Perform 20,000 iterations or more. (June 2016.)
    Take 32 bytes (256 bits) of output from PBKDF2 as the final password hash.
    Store the iteration count, the salt and the final hash in your password database.
    Increase your iteration count regularly to keep up with faster cracking tools.

Whatever you do, don't try to knit your own password storage algorithm.
alf dot henrik at ascdevel dot com 13-Mar-2014 04:58
I feel like I should comment some of the clams being posted as replies here.

For starters, speed IS an issue with MD5 in particular and also SHA1. I've written my own MD5 bruteforce application just for the fun of it, and using only my CPU I can easily check a hash against about 200mill. hash per second. The main reason for this speed is that you for most attempts can bypass 19 out of 64 steps in the algorithm. For longer input (> 16 characters) it won't apply, but I'm sure there's some ways around it.

If you search online you'll see people claiming to be able to check against billions of hashes per second using GPUs. I wouldn't be surprised if it's possible to reach 100 billion per second on a single computer alone these days, and it's only going to get worse. It would require a watt monster with 4 dual high-end GPUs or something, but still possible.

Here's why 100 billion per second is an issue:
Assume most passwords contain a selection of 96 characters. A password with 8 characters would then have 96^8 = 7,21389578984e+15 combinations.
With 100 billion per second it would then take 7,21389578984e+15 / 3600 = ~20 hours to figure out what it actually says. Keep in mind that you'll need to add the numbers for 1-7 characters as well. 20 hours is not a lot if you want to target a single user.

So on essence:
There's a reason why newer hash algorithms are specifically designed not to be easily implemented on GPUs.

Oh, and I can see there's someone mentioning MD5 and rainbow tables. If you read the numbers here, I hope you realize how incredibly stupid and useless rainbow tables have become in terms of MD5. Unless the input to MD5 is really huge, you're just not going to be able to compete with GPUs here. By the time a storage media is able to produce far beyond 3TB/s, the CPUs and GPUs will have reached much higher speeds.

As for SHA1, my belief is that it's about a third slower than MD5. I can't verify this myself, but it seems to be the case judging the numbers presented for MD5 and SHA1. The issue with speeds is basically very much the same here as well.

The moral here:
Please do as told. Don't every use MD5 and SHA1 for hasing passwords ever again. We all know passwords aren't going to be that long for most people, and that's a major disadvantage. Adding long salts will help for sure, but unless you want to add some hundred bytes of salt, there's going to be fast bruteforce applications out there ready to reverse engineer your passwords or your users' passwords.
loveun1x at yahooc dot om 09-Nov-2013 04:41
The section "Why are common hashing functions such as md5() and sha1() unsuitable for passwords?" is completely wrong. No one says MD5 and SHA-1 aren't suitable for password hashing due to their speed. (MD5 is broken for digital signatures but not yet for password hashing, but not because it is fast.) A single round of them is unsuitable, but that is not a problem with the underlying algorithms themselves.

Always use a salt, preferably a big (e.g. at least 16 characters) salt. Always run through multiple rounds of of the hashing. Most implementations today use several tens of thousands or hundreds of thousands of rounds, for example 7-zip's encrypted format uses 262,144 rounds of SHA-256.
sgbeal at googlemail dot com 19-Sep-2012 10:21
sha1 in conjunction with one or more salt values need not be as insecure as the above makes it out to be. e.g. the Fossil SCM creates an sha1 password hash based on a user's clear-text password combined with the user's name and a shared secret known only to the specific source repository for which the user is set up.
fluffy at beesbuzz dot biz 12-Jun-2012 08:33
The security issue with simple hashing (md5 et al) isn't really the speed, so much as the fact that it's idempotent; two different people with the same password will have the same hash, and so if one person's hash is brute-forced, the other one will as well.  This facilitates rainbow attacks.  Simply slowing the hash down isn't a very useful tactic for improving security.  It doesn't matter how slow and cumbersome your hash algorithm is - as soon as someone has a weak password that's in a dictionary, EVERYONE with that weak password is vulnerable.

Also, hash algorithms such as md5 are for the purpose of generating a digest and checking if two things are probably the same as each other; they are not intended to be impossible to generate a collision for.  Even if an underlying password itself requires a lot of brute forcing to determine, that doesn't mean it will be impossible to find some other bit pattern that generates the same hash in a trivial amount of time.

As such: please, please, PLEASE only use salted hashes for password storage.  There is no reason to implement your own salted hash mechanism, either, as crypt() already does an excellent job of this.


PHP와 HTML

PHP와 HTML은 많은 연관성을 가지고 있습니다: PHP는 HTML을 생성할 수있고, HTML은 PHP로 정보를 넘겨줄수 있습니다. 이 faq를 읽기전에 외부 변수를 가져오는 방법을 알아야 합니다. 이 주제에 관한 매뉴얼 페이지는 많은 예제도 포함되있습니다. register_globals가 무슨 의미인지도 알아야 합니다.

  1. 폼/URL을 통해서 변수를 넘겨줄때 인코딩(encoding)/디코딩(decoding)이 필요합니까?
  2. <input type="image">태그를 사용하려 합니다. 그런데 $foo.x과 $foo.y변수를 사용할수 없습니다. $_GET['foo.x']도 존재하지 않습니다. 그것들은 어디 있습니까?
  3. <form> 안에 어떻게 배열을 만들수 있습니까?
  4. 다중 선택 (select multiple) HTML 태그에서 어떻게 모든값을 가져올수 있습니까?
  5. 어떻게 하면 자바스크립트에서 PHP로 변수를 넘겨줄수있습니까?
폼/URL을 통해서 변수를 넘겨줄때 인코딩(encoding)/디코딩(decoding)이 필요합니까?

어떤 인코딩을 써야 하는지에 대해서 여러 경우가 존재합니다. string $data라는 변수를 사용한다고 가정하면 그리고 그 스트링의 값을 인코딩되는 않는 방식으로 넘겨주길 원하면, 다음과 같은 경우가 있습니다.

  • HTML 해석. 랜덤한 스트링을 지정하려면, 반드시 큰 따옴표로 막아줘야 합니다. 그리고 그 값을 htmlspecialchars()함수로 처리합니다.

  • URL: URL은 여러 부분으로 구성됩니다. 이 값전체를 하나의 항목으로 해석하길 원한다면, 반드시 urlencode()함수로 인코딩해야합니다.

Example #1 숨겨진 HTML 폼의 엘리먼트(element)

<?php
    echo "<input type='hidden' value='" . htmlspecialchars($data) . "' />\n";
?>

Note: $data를 urlencode()함수로 처리하는것은 잘못된것입니다. 왜냐하면 그 데이터를 urlencode() 처리할 책임은 브라우저에 있기 때문입니다. 많이 사용되는 브라우저는 정확하게 이와같은 일을 처리합니다. 이와같은 일은 메소드(method) (즉, GET이나 POST)에 따라 발생한다는것을 명심하세요. GET 요구의 경우에만 이 일을 처리합니다. POST 요구일때는 숨겨지기때문입니다.

Example #2 유저에 의해 조장되는 데이터

<?php
    echo "<textarea name='mydata'>\n";
    echo htmlspecialchars($data)."\n";
    echo "</textarea>";
?>

Note: 이 데이터는 브라우저에 의해 보여집니다. 왜냐하면 브라우저는 HTML 회피문자(escaped symbol)를 해석할것이기 때문입니다. 폼을 GET이나 POST로 보낼때, 데이터는 전송중에 브라우저에 의해 urlencode되고, PHP에 의해 직접적으로 urldecode될것입니다. 그래서 결국에는 urlencoding/urldecoding할 필요가 없는것입니다. 모든것은 자동적인 처리됩니다.

Example #3 URL 안에서

<?php
    echo "<a href='" . htmlspecialchars("/nextpage.php?stage=23&data=" .
        urlencode($data)) . "'>\n";
?>

Note: 사실 HTML GET 요구처럼 보이게 하는것이므로 이 데이터는 urlencode()를 할 필요가 있습니다.

Note: URL은 HTML속성 값으로 만들어지기 때문에 전체 URL을 htmlspecialchars()함수로 처리해야 합니다. 이 경우에 브라우저는 처음에 이 값을 un-htmlspecialchars()로 처리하고나서 URL을 넘겨주게 됩니다. PHP는 URL을 정확하게 이해할것입니다. 왜냐하면 그 데이터를 urlencoded()처리했기 때문입니다. URL에서 &는 &amp;로 변환이 된다는것을 명심해야 합니다. 이것을 잊었다고 하더라도 대부분의 브라우저는 제대로 처리하겠지만, 항상 그렇지는 않습니다. 그래서 URL이 동적이지 않더라도 URL을 반드시 htmlspecialchars()처리해야 할 것입니다.

<input type="image">태그를 사용하려 합니다. 그런데 $foo.x과 $foo.y변수를 사용할수 없습니다. $_GET['foo.x']도 존재하지 않습니다. 그것들은 어디 있습니까?

폼을 보낼때 표준 submit 버튼을 사용하는 대신에 image버튼을 사용할수 있습니다

<input type="image" src="image.gif" name="foo" />
유저가 image버튼을 클릭할때, 수행 폼은 추가적인 변수: foo.x와 foo.y을 보낼것입니다.

foo.x와 foo.y변수명은 PHP내에서 유효하지 않은 변수이기 때문에, 자동적으로 foo_x와 foo_y변수명으로 바뀝니다. 즉, 밑줄(underscore)로 변경이 됩니다. 그래서 외부 변수에서 설명된것처럼 다른 방법으로 이 변수를 이용할수 있습니다. 예를 들면 $_GET['foo_x']과 같습니다.

Note:

요청 변수 이름의 공백은 밑줄로 변환됩니다.

<form> 안에 어떻게 배열을 만들수 있습니까?

PHP스크립트에 배열을 보내는 <form>을 만들려면 <input>, <select> 또는 <textarea> 엘리먼트를 다음처럼 이름을 붙여봅니다:

<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyArray[]" />
변수명의 다음에 대괄호가 있습니다. 그것이 이 변수를 배열로 만들어줍니다. 다른 엘리먼트에 같은 이름을 부여함으로써 그 엘리먼트를 다른 배열로 그룹화할수 있습니다:
<input name="MyArray[]" />
<input name="MyArray[]" />
<input name="MyOtherArray[]" />
<input name="MyOtherArray[]" />
이것은 두 배열을 만들어냅니다. MyArray와 MyOtherArray배열은 PHP 스크립트로 보내집니다. 또한 배열에 특정 키값을 지정할수 있습니다:
<input name="AnotherArray[]" />
<input name="AnotherArray[]" />
<input name="AnotherArray[email]" />
<input name="AnotherArray[phone]" />
AnotherArray배열은 0, 1의 키와 email과 phone키를 내포하게 됩니다.

Note:

배열의 키는 HTML에서는 추가적인 특성입니다. 키로 지정하지 않으면 배열은 폼에 보여지는 엘리먼트 순서값으로 채워집니다. 첫번째 예제에서는 0, 1, 2, 3이 키값이 됩니다.

배열 함수 외부 변수를 참고하세요.

다중 선택 (select multiple) HTML 태그에서 어떻게 모든값을 가져올수 있습니까?

다중 선택 태그는 리스트 중에서 여러 아이템을 선택할 수 있게 합니다. 이 아이템들은 폼의 액션 핸들러로 보내집니다. 문제는 그들이 같은 위젯 이름으로 전달이 된다는 것입니다. 즉,

<select name="var" multiple="yes">
선택된 각 옵션 값은 액션 핸들러에 다음처럼 도착합니다:
var=option1
var=option2
var=option3
각 옵션은 $var변수의 이전 값을 덮어쓸것입니다. 해결방법은 PHP의 "폼 엘리먼트의 배열" 특성을 이용하는 것입니다. 다음처럼 사용합니다.
<select name="var[]" multiple="yes">
이것은 PHP가 $var변수를 배열로 취급하고 배열의 각 아이템이 var[]로 지정되도록 합니다. 첫번째 아이템은 $var[0] 다음은 $var[1], 등등이 됩니다. count()함수로 몇개의 옵션이 선택되었는지 알수 있습니다. 필요하다면 sort()함수로 옵션 배열을 정렬할수도 있을것입니다.

자바스크립트에서 각 엘리먼트에[]을 사용하는것은 문제가 발생한다는것을 명심해야 합니다. 대신에 숫자 폼의 ID를 사용하기 바랍니다. 또는 작은 따옴표로 변수명을 싸주십시오 그리고 엘리먼트 배열의 인덱스로 사용합니다. 예를 들면 다음과 같습니다.

variable = documents.forms[0].elements['var[]'];

어떻게 하면 자바스크립트에서 PHP로 변수를 넘겨줄수있습니까?

자바스크립트는 (주로) 클라이언트측 기술이고 PHP는 (주로) 서버측 기술이며, HTTP가 "stateless" 프로토콜이므로 두 언어는 직접적으로 변수를 공유할 수 없습니다.

그러나, 둘 사이에 변수를 넘길 수 있습니다. 이를 수행하는 한 방법은 PHP로 자바스크립트 코드를 생성하고, 브라우저가 새로고침 하도록 해서, 지정한 변수를 다시 PHP로 받아올 수 있습니다. 아래 예제가 이 방법을 보여줍니다 -- 이 방법으로 일반적으로 클라이언트 측에서만 가질 수 있는 화면 높이와 너비를 PHP 코드에서 다룰 수 있습니다.

Example #4 PHP로 자바스크립트 생성하기

<?php
if (isset($_GET['width']) AND isset($_GET['height'])) {
  // output the geometry variables
  echo "Screen width is: ". $_GET['width'] ."<br />\n";
  echo "Screen height is: ". $_GET['height'] ."<br />\n";
} else {
  // pass the geometry variables
  // (preserve the original query string
  //   -- post variables will need to handled differently)

  echo "<script language='javascript'>\n";
  echo "  location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
            . "&width=\" + screen.width + \"&height=\" + screen.height;\n";
  echo "</script>\n";
  exit();
}
?>

User Contributed Notes

Jay 01-Apr-2014 06:25
If you have a really large form, be sure to check your max_input_vars setting.  Your array[] will get truncated if it exceeds this.  http://www.php.net/manual/en/info.configuration.php#ini.max-input-vars
ciprian dot stingu at gmail dot com 17-Feb-2012 06:45
Using jquery is even easier to send (post) data between javascript and php

<?php
//make an asynchronous request
$.post("somefile.php", {"Param1" : true, "Param2" : 1, "Param3" : "some text"},
    function(received_data)
    {
        //do something with received data
        $("#some_element").html(received_data);
    });

//make a post request
$.ajax({
    type: "POST",
    url: "somefile.php",
    data: {"Param1" : true, "Param2" : 1, "Param3" : "some text"},
    success: function(received_data)
        {//do something with received data},
    async: false
});
?>
francesco 07-Nov-2006 03:41
Another way to pass variables from JavaScript to PHP.

<script type="text/javascript" language="JavaScript">
<!--
function getScreenResolution()
{
    return document.form.ScreenResolution.value = screen.width + "x" + screen.height;
}
//-->
</script>
<form name="form" action="screen.php?show=ok" method="post" >
<input name="ScreenResolution" type="text" size="20" maxlength="9" />
<input name="show" type="submit" value="Submit" onclick="getScreenResolution()" />
</form>
<?php
    echo $_POST['ScreenResolution'];
?>
tchibolecafe at freemail dot hu 12-Aug-2006 01:34
Notes on question "1. What encoding/decoding do I need when I pass a value through a
form/URL?"

Doing an htmlspecialchars() when echoing a string as an HTML attribute value is not enough to make the string safe if you have accented (non-ASCII) characters in it. See http://www.w3.org/TR/REC-html40/appendix/notes.html#non-ascii-chars

The referred document recommends the following method to be used:

<?php
  function fs_attr($path){
    $retval='';
    for($i=0;$i<strlen($path);$i++){
      $c=$path{$i};
      if(ord($c)<128){
        $retval.=$c;
      }else{
        $retval.=urlencode(utf8_encode($c));
      }
    }
    return htmlspecialchars($retval);
  }

  $img_path='eoua.jpg';
  echo '<img src="'.fs_attr($img_path).'">';
?>

However, using utf8 encoding for path names is among others supported by Windows NT, above method fails when running for example on an Apache server on Linux.

A more fail safe way:

<?php
        function fs_attr($path){
                $retval='';
                for($i=0;$i<strlen($path);$i++){
                        $c=$path{$i};
                        if(ord($c)<128){
                                $retval.=$c;
                        }else{
                                if(PHP_OS==='WINNT')
                                        $retval.=urlencode(utf8_encode($c));
                                else
                                        $retval.=urlencode($c);
                        }
                }

                return htmlspecialchars($retval);
        }
?>

There may be operating systems that want utf8 encoding, other than WINNT. Even this latter one won't work on those systems. I don't know about any possibility to determine immediately which encoding to be used on the file system of the server...
jetboy 30-Dec-2005 04:06
While previous notes stating that square brackets in the name attribute are valid in HTML 4 are correct, according to this:

http://www.w3.org/TR/xhtml1/#C_8

the type of the name attribute has been changed in XHTML 1.0, meaning that square brackets in XHTML's name attribute are not valid.

Regardless, at the time of writing, the W3C's validator doesn't pick this up on a XHTML document.
dmsuperman at comcast dot net 10-Sep-2005 06:05
Here's a great way to pass JavaScript to PHP without even leaving the page:

<script type="text/javascript">
<!--

function xmlreq(){
  if(window.XMLHttpRequest){
    req = new XMLHttpRequest();
  }else if(window.ActiveXObject){
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return(req);
}
function sendPhp(url){
  var req = xmlreq();
  req.onreadystatechange = stateHandler;
  req.open("GET", url, true);
  req.send(null);
}

sendPhp("updatedatabase.php?username=blah&displayname=whatever");
//-->
</script>
levinb at cs dot rpi dot edu 17-Jun-2005 06:17
Well, I was working on this one project, on the assumption that I could get the values of all elements with the same name from an appropriately named array.  Well, I was *very* disappointed when I couldn't, so I made it so I did anyway.

The following script should convert the raw post data to a $_POST variable, with form data from SELECT elements and their ilk being transformed into an array.  It's heavily unoptimized, and I probably missed something, but it's relatively easy to read.  I welcome corrections.

<?php

if ($_POST) {
        $postdata = file_get_contents('php://input');

        $uglybugger = '/(?<=&)([^&=]+)(?:=([^&]*))/';
        $matches = array();

        preg_match_all($uglybugger, $postdata, $matches);

        $_POST = array();

        $match_count = count($matches[0]);
        for ($i = 0; $i < $match_count; $i++) {
                if (!isset($_POST[$matches[1][$i]])) {
                        $_POST[$matches[1][$i]] = array();
                }
                $_POST[$matches[1][$i]][] = $matches[2][$i];
        }
        $match_count = count($_POST);
        for ($i = 0; $i < $match_count; $i++) {
                if (count($_POST[$i]) == 1) {
                        $_POST[$i] = $_POST[$i][0];
                }
        }
}

?>
dreptack at op dot pl 02-Jan-2005 09:37
I needed to post html form through image input element. But my problem was I had to use multiple image-buttons, each one for a single row of form table. Pressing the button was mention to tell script to delete this row from table and also (in the same request) save other data from the form table.
I wrote simple test-script to see what variable I should check for in a script:

I have a html document:

<form action="test.php" method="post">
<input type="image" name="varscalar" src="/images/no.gif" />
<input type="image" name="vararray[12]" src="/images/no.gif" />
</form>

And a php script:
<?php
  if ($_POST) {
    echo "post: <pre>"; print_r($_POST); echo '</pre>';
  }
?>

What I've discovered suprised me a lot!

After hitting on varscalar:

post:
Array
(
    [varscalar_x] => 6
    [varscalar_y] => 7
)

After hitting on upper right corner of vararray:

post:
Array
(
    [vararray] => Array
        (
            [12] => 2
        )

)

This mean when clicking on image-type input element, which name is an array, only y-part of a value is remembered.

The result is the same on: php 4.1.2 on Win98se, php 4.3.9-1 on linux
vlad at vkelman dot com 05-Jun-2004 04:04
"4.  How do I get all the results from a select multiple HTML tag?"

I think that behavior of PHP which forces to use [] after a name of 'select' control with multiple attribute specified is very unfortunate. I understand it comes from old times when registerglobals = on was commonly used. But it creates incompatibility between PHP and ASP or other server-side scripting languages. The same HTML page with 'select' control cannot post to PHP and ASP server pages, because ASP does not require [] and automatically recognize when arrays are posted.
Kenn White kennwhite dot nospam at hotmail dot com 13-Mar-2004 05:18
Concerning XHTML Strict and array notation in forms, hopefully the information below will be useful:

If I have a form, name="f", and, say, an input text box, name="user_data[Password]", then in Javascript, to reference it I would do something like:

var foo = f['user_data[Password]'].value;

Now, say that in making the switch to XHTML strict, I decide to fully embrace standards compliance, and change my form to id="f", and the input text box to id="user_data[Password]"

Because these have id instead of name, I discover, that all my javascript validation routines just broke.  It seems that I have to now change all my js code to something like:

document.getElementById( 'user_data[Password]' ).focus();

I test this on all the major modern browsers, and it works well.  I'm thinking, Great!  Until I try to validate said page.  It turns out that the bracket characters are invalid in id attributes.  Ack!  So I read this thread:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=
UTF-8&th=78dea36fd65d9bbe&seekm=
pqx99.19%24006.13377%40news.ca.inter.net#link11
(link needs to be spliced, sorry)

What does this mean, I start asking myself?  Do I have to abandon my goal to migrate to XHTML strict?  Transitional seems so unsatisfying.  And why bother with a technique that seems to work on most browsers, if it's broken.  Alas, there is hope.

But then I read http://www.w3.org/TR/xhtml1/#h-4.10 carefully.  It says "name" is deprecated as a form attribute, but *NOT* specifically as an attribute in form *elements*.  It seems my solution is to use "id" for the form itself, but I can legally use "name" for the individual form components, such as select and text input boxes.  I get the impression that "name" as an attribute is eventually going away completely, but in extensive testing using the W3C validator, it passes "name" on form components, as long as "id" (or, strangely, nothing) is used to denote the form itself.

So for XHTML strict, the bottom line:
 1. form, use id, not name
 2. input, use id if you can, but if you need to use bracketed notation (for example, passing PHP arrays), i.e., foo[], you *MUST* use name for XHTML strict validation.

-kenn

kennwhite.nospam@hotmail.com
davis at risingtiger dot net 09-Jan-2004 09:14
I thought this might be useful to fellow PHP heads like myself out there.

I recently came across a need to transfer full fledged mutli-dimensional arrays from PHP to JAVASCRIPT.

So here it is and hopefuly good things come from it.

<?php
function phparray_jscript($array, $jsarray)
{
    function loop_through($array,$dimen,$localarray)
    {
        foreach($array as $key => $value)
        {
            if(is_array($value))
            {
                echo ($localarray.$dimen."[\"$key\"] = new Array();\n");
                loop_through($value,($dimen."[\"".$key."\"]"),$localarray);
            }
            else
            {
                echo ($localarray.$dimen."[\"$key\"] = \"$value\";\n");
            }
        }
    }

    echo "<script language=\"Javascript1.1\">\n";
    echo "var $jsarray = new Array();\n";
    loop_through($array,"",$jsarray);
    echo "</script>";
}
?>
martellare at hotmail dot com 15-Mar-2003 09:28
I do not think you are right about not being able to specify something for the value attribute, but I can see where you would have thought it would fail:

A fair warning about testing to see if a variable exists...
when it comes to strings, the values '' and '0' are interpreted as false when tested this way...

<?php
if ($string) { ... }  //false for $string == '' || $string == '0'
?>

The best practice for testing to see if you received a variable from the form (which in the case of a checkbox, only happens when it is checked) is to test using this...

<?php
if ( isSet($string) ) { ... } //true if and only if the variable is set
?>

The function tests to see if the variable has been set, regardless of its contents.

By the way, if anyone's curious, when you do make a checkbox without specifying the value attribute, the value sent from the form for that checkbox becomes 'on'.  (That's for HTML in general, not PHP-specific).
martellare at hotmail dot com 26-Nov-2002 08:25
A JavaScript Note: Using element indexes to reference form elements can cause problems when you want to add new elements to your form; it can shift the indexes of the elements that are already there.

For example, You've got an array of checkboxes that exist at the beginning of a form:
===================

<FORM>
    <INPUT type="checkbox" name="fruits[]" value="apple">apple
    <INPUT type="checkbox" name="fruits[]" value="orange">orange
    <INPUT type="checkbox" name="fruits[]" value="banana">banana
</FORM>

===================
... These elements could be referenced in JavaScript like so:
===================

<SCRIPT language="JavaScript" type="text/javascript">
<!--
    var index = 0; //could be 1 or 2 as well
    alert(document.forms[0].elements[index]);
//-->
</SCRIPT>

===================
However, if you added a new textbox before these elements, the checkboxes indexes become 1 - 3 instead of 0 - 2;  That can mess up what ever code you create depending on those indexes.

Instead, try referencing your html arrays in JavaScript this way.  I know it works in Netscape 4 & IE 6, I hope it to some extent is universal...
===================

<SCRIPT language="JavaScript" type="text/javascript">
<!--
    var message = "";
    for (var i = 0; i < document.forms[0].elements['fruits[]'].length; i++)
    {
        message += "events[" + i + "]: " + document.forms[0].elements['fruits[]'][i].value + "\n";
    }
    alert(message);
//-->
</SCRIPT>

===================
bas at cipherware dot nospam dot com 17-Oct-2002 06:52
Ad 3. "How do I create arrays in a HTML <form>?":

You may have problems to access form elements, which have [] in their name, from JavaScript. The following syntax works in IE and Mozilla (Netscape).

index = 0;
theForm = document.forms[0];
theTextField = theForm['elementName[]'][index];


PHP와 COM

PHP는 Win32 플랫폼의 COM과 DCOM 객체에 접근할수 있습니다.

  1. 어떤것을 계산하는 DLL을 빌드했습니다. PHP상에서 이 DLL을 실행시킬수 있는 방법 이 있습니까?
  2. 'Unsupported variant type: xxxx (0xxxxx)'은 무엇을 의미합니까?
  3. PHP에서 비주얼한 객체(visual object)를 조작할수 있습니까?
  4. 세션안에 COM 객체를 저장할 수 있습니까?
  5. COM 에러를 어떻게 추적할 수 있습니까?
  6. 펄에서 쓰던 방법으로 PHP 스크립트에서 DLL 파일을 만들수 있습니까?
  7. 'Unable to obtain IDispatch interface for CLSID {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}'의 의미는 무엇입니까?
  8. 원격 서버에서 COM 객체를 어떻게 실행할수 있습니까?
  9. 'DCOM is disabled in C:\path...\scriptname.php on line 6'라는 메시지를 봤습니다. 어떻게 해야 하나요?
  10. PHP에서 ActiveX 객체를 부르거나/변경할수 있습니까?
  11. 특정 컴포넌트에서 실행중인 인스턴스에 접근 가능합니까?
  12. COM 객체로부터 받은 이벤트를 제어할 방법이 있습니까?
  13. 하나 이상의 인터페이스를 갖는 COM 객체의 메쏘드를 불러 들이려고 하는데 문제에 봉착했습니다. 어떻게 해야 합니까?
  14. 그래서 PHP와 COM이 작동하는군요. COM+는 어떤가요?
  15. PHP가 COM 객체를 다룰수있다면, PHP로 컴포넌트 자원을 관리하는 MTS를 쓸수있지 않을까요?
어떤것을 계산하는 DLL을 빌드했습니다. PHP상에서 이 DLL을 실행시킬수 있는 방법 이 있습니까?

간단한 DLL이라도 PHP에서 실행할 방법은 아직 없습니다. 접근가능한 COM서버를 포함하는 DLL이고, IDispatch 인터페이스로 구현되어있다면 가능할수도 있습니다.

'Unsupported variant type: xxxx (0xxxxx)'은 무엇을 의미합니까?

몇가지의 VARIANT 타입과 그 타입간의 조합이 가능합니다. 그들 대부분은 거의 지원이 되고있지만 일부가 아직 구현되어있지 않습니다. 배열은 완전히 지원되지 않습니다. 오직 1차원 배열만이 PHP와 COM사이에서 전달이 가능합니다. 지원되지 않는 다른 타입을 발견했다면 그 타입을 버그 레포트로 알려주기랍니다 (아직 보고되지 않는것들이라면) 그리고 더 자세한 정보도 함께 제공해주십시오.

PHP에서 비주얼한 객체(visual object)를 조작할수 있습니까?

가능합니다. 하지만 PHP는 대부분 웹서버의 환경에서 웹 스크립트 언어로서 이용되고 있습니다. 따라서 서버가 비주얼 객체를 보여주지는 않을것입니다. 응용프로그램 스크립트를 위한 PHP(PHP-GTK같은)를 사용한다면 COM을 통해서 비주얼 객체(visual object)에 접근하고 조작하는데 아무 제한이 없습니다.

세션안에 COM 객체를 저장할 수 있습니까?

그건 안됩니다. COM 인스턴스는 리소스로 취급되므로 오직 단일 스크립트의 문맥에서만 가용한것입니다.

COM 에러를 어떻게 추적할 수 있습니까?

PHP 5에서, COM 확장은 com_exception 예외를 발생합니다. 이를 잡아서 code 멤버를 조사함으로써 다음에 취할 행동을 결정할 수 있습니다.

PHP 4에서는 COM 오류를 PHP 자체에서 제공하는 방법(@, track_errors, ..)으로 추적할 수 없습니다.

펄에서 쓰던 방법으로 PHP 스크립트에서 DLL 파일을 만들수 있습니까?

만들수 없습니다. 재수없게도 PHP를 위해 그런 툴이 존재하지 않습니다.

'Unable to obtain IDispatch interface for CLSID {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}'의 의미는 무엇입니까?

이 에러는 다음과 같은 원인일수 있습니다.

  • CLSID가 잘못되었다
  • 필요한 DLL이 존재하지 않는다
  • 필요한 컴포넌트가 IDispatch 인터페이스로 구현되지 않았다

원격 서버에서 COM 객체를 어떻게 실행할수 있습니까?

로컬 객체를 실행시키는것과 완전히 똑같습니다. 단지 COM 생성자의 두번째 인수에 다른 서버의 IP를 넘겨주면 됩니다.

php.ini에서 com.allow_dcom =TRUE로 설정해야 합니다.

'DCOM is disabled in C:\path...\scriptname.php on line 6'라는 메시지를 봤습니다. 어떻게 해야 하나요?

php.ini를 수정하여 com.allow_dcom =TRUE로 설정합니다.

PHP에서 ActiveX 객체를 부르거나/변경할수 있습니까?

이것은 PHP에서 할일이 아닙니다. HTML 문서에서 ActiveX 객체가 포함되있으면 클라이언트 단에서 그 코드를 처리하는 것입니다. PHP 스크립트와는 전혀 관련이 없고 따라서 서버단과는 직접적인 관련이 없습니다.

특정 컴포넌트에서 실행중인 인스턴스에 접근 가능합니까?

모니커(monikers)의 도움으로 가능합니다. 하나의 워드 인스턴스로 다방향 레퍼런스를 얻고 싶다면 다음처럼 그 인스턴스를 만들 수 있습니다.

<?php
$word = new COM("C:\docs\word.doc");
?>

위처럼 하면 실행중인 인스턴스가 없을 때에는 새로운 인스턴스를 만들수 있고, 실행중인 인스턴스가 이미 존재하면 그 핸들을 넘겨주게 됩니다.

COM 객체로부터 받은 이벤트를 제어할 방법이 있습니까?

com_event_sink()를 사용하여 이벤트 싱크와 바인드를 정의할 수 있습니다. PHP가 이벤트 싱크 클래스의 뼈대을 생성하게 하기 위하여 com_print_typeinfo()를 사용할 수 있습니다.

하나 이상의 인터페이스를 갖는 COM 객체의 메쏘드를 불러 들이려고 하는데 문제에 봉착했습니다. 어떻게 해야 합니까?

답은 불만스럽겠지만 간단합니다. 확실치는 않지만 당신은 아무것도 할수없습니다. 어떤사람은 이 문제와 관련된 정보를 가지고 있습니다. » 저에게 알려주시라 :)

그래서 PHP와 COM이 작동하는군요. COM+는 어떤가요?

MTS와 MTMQ를 통해서 컴포넌트를 관리하는 프레임워크가 추가되면서 COM에서 COM+로 발전된것입니다. 그런데 PHP는 그런 컴포넌트를 지원하지 않습니다.

PHP가 COM 객체를 다룰수있다면, PHP로 컴포넌트 자원을 관리하는 MTS를 쓸수있지 않을까요?

PHP 자체로는 아직 트랜젝션을 관리할수 없습니다. 따라서 에러가 발생하더라도 롤백으로 초기화되지않습니다. 트랜젝션을 지원하는 컴포넌트를 사용한다면 트랜젝션 관리를 구현해서 써야합니다.

User Contributed Notes

microtrash at at gmail dot com 09-Jul-2008 08:29
In reference to question 1:

On one project I had a dll that exposed certain functions I wished to call from within PHP.  I created a PHP Module which had a matching function for each in the DLL.  The PHP Module did no processing, merely served as an adapter for the original DLL.
codeslinger at compsalot dot com 27-Jan-2007 02:18
in response to item #1 above:  "If this is a simple DLL there is no way yet to run it from PHP."

Answer: There are several different packages/add-ons to php that enable low level access to the windows api. One of the most popular of these packages is available at http://www.winbinder.org

Winbinder does have the ability to load a dll and call it's functions.  Winbinder also provides a large set of windows apis for creating windows and controls.  This is useful for creating stand-alone desktop apps.  It is a possible alternative to php-gtk.

I've had mixed results with Winbinder, some things work very well, others are buggy; source code is available.  It's worth a look, Evaluate carefully.
17-Jul-2005 01:56
Re: The Windows English CHM Sample with MS Word Automation.

I see that $Word->Release() is called after $Word->Quit().
This seems to be erroneous. Actually, $Word->Release() should not have to be called if proper COM garbage collection is taking place. Release() is a C++ construction not used in either Visual Basic or VBScript or even MS-JScript. Just to be sure, however, I ran the sample through php.exe (5.04 CLI on Windows XP SP1 with Office 2003) and got a RPC failed error on the line containing $Word->Release(). The rest of the sample works fine, and the results are as expected. So, I propose 2 scenarios:
      1.There is a problem with the documentation (more likely).
      2.There is a problem with PHP504 itself, as this problem is reproduced with any COM Automation call, not just Word.
junk.ghost@virgin dOtt net 05-Oct-2004 06:27
It may be obvious to everyone else but...

If you want to write your own COM DLL in MSVC++6 and you want to pass it a string, you need the following in your .idl file:

HRESULT function_name([in] BSTR parameter_name,
                      [retval, out] BSTR * retval);

retval is the result of your function as a string to be passed back.

BSTR is an unsigned short *, so if you want to use your string with STL string etc. you may need to convert parameters to and from char *.

The burden of my message is that from PHP

$comThing = new COM("comThing.comThing");
print $comThing->function_name("Jeremy");

Jeremy will be marshalled as wide chars, which match BSTR.
ALAN at DONTSPAMMEE;HOGAR dot COM 17-Aug-2002 06:39
ONE SAMPLE USING MS-XML3 PARSER ON WINDOWS
<?
  $xml= new COM("Microsoft.XMLDOM");
  $xml->async=false;
  $xml->load("STYLE.xml");
  $xsl = new COM("Microsoft.XMLDOM");
  $xsl->async=false;
  $xsl->load("STYLE.xsl");
  $response=$xml->transformNode($xsl);
  print $response;
?>


PHP 4에서 PHP 5로 이행하기

이 faq 섹션은 PHP 4에서 PHP 5로 이행을 돕습니다.

  1. PHP 4에서 PHP 5로 이행하기
  2. MySQL이 PHP 5에서 작동합니까? 사라진 것 같습니다.
  3. PHP 5가 완전히 새로운 OOP 모델이라고 들었는데, 기존의 OOP 코드가 작동합니까? 새 OOP 기능에 대한 정보는 어디에서 찾을 수 있습니까?
  4. 새 OOP 모델을 제외하고, PHP 5에서 무엇이 바뀌었습니까? 또, PHP 매뉴얼에 PHP 5 전용 버전이 존재합니까?
PHP 4에서 PHP 5로 이행하기

PHP 5가 많은 새로운 기능을 제공하지만, 가능한 처리 기능이 망가지지 않도록 이전 PHP 버전과 호환되도록 설계되었습니다.

매뉴얼에서 해당하는 PHP 5 이행 부록을 읽어보십시오. PHP 5로 이행하는 주제에 대한 많은 정보를 포함하고 있습니다.

MySQL이 PHP 5에서 작동합니까? 사라진 것 같습니다.

MySQL은 지원합니다. 바뀐 점은 MySQL 지원이 PHP 5에서 더 이상 기본으로 활성되지 않는 점입니다. 이는 PHP가 configure 줄에 --with-mysql 을 포함하지 않는다는 말이므로, PHP 컴파일 시에 수동으로 추가해야 합니다. 윈도우 사용자는 php.ini를 수정해서 php_mysql.dll DLL을 활성화 해야 합니다. PHP 4에서는 이러한 DLL이 존재하지 않았는데, 윈도우 PHP 바이너리에 포함되어 있었기 때문입니다.

또한, MySQL 클라이언트 라이브러리가 PHP에 들어있지 않습니다. 이 주제에 대한 자세한 내용은 해당 FAQ에서 다루고 있고, MySQL 설치에 관한 상세는 MySQL 섹션을 읽어보십시오. 예제 configure 줄은 --with-mysql=/usr 이고, 윈도우 사용자는 libmySQL.dll을 시스템에서 사용할 수 있게 하면 됩니다.

PHP 5가 완전히 새로운 OOP 모델이라고 들었는데, 기존의 OOP 코드가 작동합니까? 새 OOP 기능에 대한 정보는 어디에서 찾을 수 있습니까?

PHP 5 OOP 모델의 주 변경은 PHP 5가 젠드 엔진 2.0을 사용한다는 점입니다. 젠드 엔진 1.0(PHP 4)와 호환을 위해서 zend.ze1_compatibility_mode 지시어를 활성화할 수 있습니다.

새 OOP 모델은 OOP 언어 레퍼런스와 OOP 이행 부록 섹션에서 다루고 있습니다.

새 OOP 모델을 제외하고, PHP 5에서 무엇이 바뀌었습니까? 또, PHP 매뉴얼에 PHP 5 전용 버전이 존재합니까?

다른 변경점도 존재합니다. 상세는 5 이행 부록을 참고하십시오. 매뉴얼의 PHP 5 전용 버전은 존재하지 않습니다. 대부분의 PHP가 그대로 유지되고 있기 때문입니다.

User Contributed Notes

gfrank at neoservers dot com 25-Jun-2007 05:17
To get PHP5 and PHP4 to work at the same time you will also need to change all "php_value", "php_flag", "php_admin_flag", and "php_admin_value" occurences; otherwise, you can't set configuration file settings.
odnowa-sql at o2 dot no-spam pl 29-Apr-2007 02:13
You can run PHP4 and PHP5 as Apache2 modules at the same time. The trick is to hack the sources a little bit.
Change all occurences of application/x-httpd-php (and MIME type for sources) to application/x-httpd-php5 and then add that mime type to apache conf.


기타 질문

다른 분류에 넣을 수 없는 몇몇 질문이 존재합니다. 여기에서 확인할 수 있습니다.

  1. 윈도우에서 어떻게 bz2 압축 매뉴얼을 다룹니까?
  2. 함수 정의에서 인수 옆에 &가 무엇을 의미합니까? 예. asort
  3. register_globals을 어떻게 다룹니까?
윈도우에서 어떻게 bz2 압축 매뉴얼을 다룹니까?

bz2 파일을 다루는 압축 도구가 없다면, Redhat에서 명령줄 도구를 » 받을 수 있습니다. (아래에서 추가 정보를 확인하십시오)

명령줄 도구를 사용하기 싫다면, » Stuffit Expander, » UltimateZip, » 7-Zip, » Quick Zip 등의 무료 도구를 사용해 보십시오. » WinRAR이나 » Power Archiver 같은 도구를 가지고 있다면, 그걸로 bz2 파일을 풀 수 있습니다. Total Commander(이전엔 Windows Commandr)를 사용한다면, » Total Commander 사이트에서 bz2 플러그인을 무료로 받을 수 있습니다.

Redhat의 bzip2 명령줄 도구:

Win2k Sp2 사용자는 최신 버전 1.0.2를 받고, 다른 윈도우 사용자는 버전 1.00을 받습니다. 내려받기가 끝난 후 실행 파일을 bzip2.exe로 이름을 바꿉니다. 편하게 사용하기 위해서 path에 있는 디렉토리에 넣습니다. 예. C:\Windows C는 윈도우 설치 드라이브를 나타냅니다.

주의: lang는 언어를 나타내고 x는 형식을 나타냅니다, 예: pdf. php_manual_lang.x.bz2를 풀려면 간단히 다음 절차를 따릅니다:

  • 명령 프롬프트 창을 엽니다
  • 내려받은 php_manual_lang.x.bz2 파일을 저장한 폴더로 cd 합니다
  • bzip2 -d php_manual_lang.x.bz2 를 실행하면, 같은 폴더에 php_manual_lang.x가 풀립니다

많은 html 파일을 포함한 php_manual_lang.tar.bz2를 받았을 경우에도, 절차는 같습니다. 유일한 차이는 php_manual_lang.tar 파일을 얻는 점입니다. tar 형식은 대부분의 윈도우 압축기에서 다룰 수 있습니다. 예. » WinZip.

함수 정의에서 인수 옆에 &가 무엇을 의미합니까? 예. asort()

이는 인수가 참조로 전달됨을 의미하고, 함수가 문서에 따라 변경할 수 있음을 의미합니다. 변수를 이 방법으로만 전달할 수 있고, 함수 호출에서 &로 전달할 필요가 없습니다. (심지어 배제되었습니다)

register_globals을 어떻게 다룹니까?

register_globals의 보안 문제에 대한 정보를, register_globals 사용하기 보안장에서 읽어보십시오.

register_globals를 켜는 것에 의존하는 대신 자동 전역 사용을 권합니다.

register_globals가 꺼져있는 공유 호스트에서, 켜져있어야 하는 오래된 어플리케이션을 사용해야 할 경우나, 이 기능이 켜져있는 몇몇 호스팅 서버에서 보안 문제를 피하기 위해서 PHP에서 반대 설정을 흉내내야 할 경우가 있습니다. PHP의 설정을 바꾸는 방법으로 해결하는 것이 가장 좋은 방법이겠지만, 불가능할 경우 다음의 호환용 코드 조각을 사용할 수 있습니다.

Example #1 Register Globals 흉내내기

register_globals On을 흉내냅니다. variables_order 지시어를 변경하였다면, $superglobals도 따라서 변경하십시오.

<?php
// register_globals on 흉내내기
if (!ini_get('register_globals')) {
    $superglobals = array($_SERVER, $_ENV,
        $_FILES, $_COOKIE, $_POST, $_GET);
    if (isset($_SESSION)) {
        array_unshift($superglobals, $_SESSION);
    }
    foreach ($superglobals as $superglobal) {
        extract($superglobal, EXTR_SKIP);
    }
}
?>

register_globals Off를 흉내냅니다. 이 코드를 스크립트 처음이나, 세션을 사용한다면 session_start() 이후에 호출해야 하는 점을 명심하십시오.

<?php
// register_globals off 흉내내기
function unregister_GLOBALS()
{
    if (!ini_get('register_globals')) {
        return;
    }

    // Might want to change this perhaps to a nicer error
    if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
        die('GLOBALS overwrite attempt detected');
    }

    // Variables that shouldn't be unset
    $noUnset = array('GLOBALS',  '_GET',
                     '_POST',    '_COOKIE',
                     '_REQUEST', '_SERVER',
                     '_ENV',     '_FILES');

    $input = array_merge($_GET,    $_POST,
                         $_COOKIE, $_SERVER,
                         $_ENV,    $_FILES,
                         isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());

    foreach ($input as $k => $v) {
        if (!in_array($k, $noUnset) && isset($GLOBALS[$k])) {
            unset($GLOBALS[$k]);
        }
    }
}

unregister_GLOBALS();

?>

User Contributed Notes

doerr at apkk dot de 29-Dec-2010 02:41
If you only needed register_globals for get/post variables, the effictive solution for 5.3 is:
  import_request_variables("GPC", "");

But if the skripts relied on session_register() you'll have to do more:
- Replace all variables that appeared after session_register with _SESSION equivalents - so $myvar becomes $_SESSION['myvar']
- Take care if your variables appeared inside strings - 'Hello $user !' works, but 'Hello $_SESSION['user'] !' not - so you have to concatenate the string: 'Hello '.$_SESSION['user'] .' !'
- Session variables in function declarations (for whatever purpose) will not work - keeping the old (local) names will work in most cases.
- Finally, replace the session_register(..) line with session_start()
markus 19-Jun-2005 12:34
Considering the comment below. I think there's a way to avoid that "problem":

<?php
//
// $starttime is an example of a variable that we might need to define,
// even before, running the "register_globals OFF" emulator below.
//
list($msec, $sec) = explode(' ', microtime());
$starttime = ((float)$msec + (float)$sec);

//
// If register_globals is ON, ensure no unexpected globals are defined.
// ie. We'll try to emulate a register_globals OFF environment.
//
if( (bool)@ini_get('register_globals') )
{
    $superglobals = array($_ENV, $_GET, $_POST, $_COOKIE, $_FILES, $_SERVER);
    if( isset($_SESSION) )
    {
        array_unshift($superglobals, $_SESSION);
    }
    $knownglobals = array(
        //
        // Known PHP Reserved globals and superglobals:
        //
        '_ENV',        'HTTP_ENV_VARS',
        '_GET',        'HTTP_GET_VARS',
        '_POST',    'HTTP_POST_VARS',
        '_COOKIE',    'HTTP_COOKIE_VARS',
        '_FILES',    'HTTP_FILES_VARS',
        '_SERVER',    'HTTP_SERVER_VARS',
        '_SESSION',    'HTTP_SESSION_VARS',
        '_REQUEST',

        //
        // Global variables used by this code snippet:
        //
        'superglobals',
        'knownglobals',
        'superglobal',
        'global',
        'void',

        //
        // Known global variables defined before this code snippet is reached.
        //
        'starttime',
    );
    foreach( $superglobals as $superglobal )
    {
        foreach( $superglobal as $global => $void )
        {
            if( !in_array($global, $knownglobals) )
            {
                unset($GLOBALS[$global]);
            }
        }
    }
}
?>

Note the stuff related to the $_SESSION array depends on whether the PHP session has been started or not. You might want to call session_start() before this point (or set session.auto_start ON).

HTH+ :)
php at REMOVEMEkennel17 dot co dot uk 13-Apr-2005 05:22
Regarding simulating register_globals = off, note that it is impossible to adequately prevent $_SESSION variables from being globalised, as the array (and thus the globals) are created on a call to session_start().  You would therefore have to 'undo' this when you start a session as using it at the start of your script will have no effect.

To avoid potential problems, use a prefix that is unique for all session variables (e.g. 'SESS_'), and only access them via the $_SESSION array.  The prefix ensures that you don't have a naming clash (and therefore a security risk) with any non-session globals.


부록

  • PHP와 PHP 관련 프로젝트의 역사
    • PHP의 역사
    • PHP 관련 프로젝트의 역사
    • PHP 관련 서적
    • PHP에 관한 출판물
  • Migrating from PHP 7.0.x to PHP 7.1.x
    • New features
    • New functions
    • New global constants
    • Backward incompatible changes
    • Deprecated features in PHP 7.1.x
    • Changed functions
    • Other changes
  • Migrating from PHP 5.6.x to PHP 7.0.x
    • 이전 버전과 호환되지 않는 변경점
    • New features
    • Deprecated features in PHP 7.0.x
    • Changed functions
    • New functions
    • New Classes and Interfaces
    • New Global Constants
    • Changes in SAPI Modules
    • Removed Extensions and SAPIs
    • 기타 변경사항
  • Migrating from PHP 5.5.x to PHP 5.6.x
    • Backward incompatible changes
    • New features
    • Deprecated features in PHP 5.6.x
    • Changed functions
    • New functions
    • OpenSSL changes in PHP 5.6.x
    • Other changes to extensions
    • New global constants
  • Migrating from PHP 5.4.x to PHP 5.5.x
    • What has changed in PHP 5.5.x
    • Backward Incompatible Changes
    • New features
    • Deprecated features in PHP 5.5.x
    • Changed Functions
    • New Functions
    • New Classes and Interfaces
    • New Methods
    • Other changes to extensions
    • New Global Constants
    • Changes to INI file handling
    • Changes to PHP Internals
  • Migrating from PHP 5.3.x to PHP 5.4.x
    • What has changed in PHP 5.4.x
    • Backward Incompatible Changes
    • New features
    • Changes in SAPI modules
    • Deprecated features in PHP 5.4.x
    • Changed Functions
    • New Functions
    • New Classes and Interfaces
    • New Methods
    • Removed Extensions
    • Other changes to extensions
    • New Global Constants
    • Changes to INI file handling
    • Other changes
  • Migrating from PHP 5.2.x to PHP 5.3.x
    • What has changed in PHP 5.3.x
    • Backward Incompatible Changes
    • New features
    • Changes made to Windows support
    • Changes in SAPI modules
    • Deprecated features in PHP 5.3.x
    • Undeprecated features in PHP 5.3.x
    • New Parameters
    • New Functions
    • New stream wrappers
    • New stream filters
    • New Class Constants
    • New Methods
    • New Extensions
    • Removed Extensions
    • Other changes to extensions
    • New Classes
    • New Global Constants
    • Changes to INI file handling
    • Other changes
  • Migrating from PHP 5.1.x to PHP 5.2.x
    • What has changed in PHP 5.2.x
    • Backward Incompatible Changes
    • New Error Messages
    • Changes in PHP datetime support
    • New Parameters
    • New Functions
    • New Methods
    • Removed Extensions
    • New Extensions
    • New Classes
    • New Global Constants
    • New Class Constants
    • New INI Configuration Directives
    • Error Reporting
    • Other Enhancements
  • Migrating from PHP 5.0.x to PHP 5.1.x
    • Key PHP 5.1.x features
    • Changes in reference handling
    • Reading []
    • Integer values in function parameters
    • Class and object changes
    • Extensions
    • Date/time support
    • Changes in database support
    • Checking for E_STRICT
  • PHP 4에서 PHP 5.0.x로 이행
    • PHP 5.0.x부터 바뀐 것들
    • 이전 버전과 호환되지 않는 변경점
    • CLI와 CGI
    • 설정 파일 이행
    • 새로운 함수들
    • 새로운 지시어
    • 데이터베이스
    • 새로운 객체 모델
    • 오류 보고
  • Classes and Objects (PHP 4)
    • class
    • extends
    • Constructors
    • Scope Resolution Operator (::)
    • parent
    • Serializing objects - objects in sessions
    • The magic functions __sleep and __wakeup
    • References inside the constructor
    • Comparing objects
  • PHP에서 디버깅
    • PHP에서 디버깅에 대하여
  • Configure options
    • List of core configure options
  • php.ini directives
    • List of php.ini directives
    • List of php.ini sections
    • Description of core php.ini directives
  • 확장 목록/분류
    • 알파벳순
    • 자격
    • 상태
  • 함수 별칭 목록
  • 예약어 목록
    • 키워드 목록
    • 예약 클래스
    • 예약 상수
  • 리소스형 목록
  • List of Available Filters
    • String Filters
    • Conversion Filters
    • Compression Filters
    • Encryption Filters
  • 지원되는 소켓 전송 목록
    • 인터넷 도메인: TCP, UDP, SSL, TLS
    • 유닉스 도메인: Unix와 UDG
  • PHP 자료형 비교표
  • List of Parser Tokens
  • Userland Naming Guide
    • Global Namespace
    • Rules
    • Tips
  • 매뉴얼에 대하여
    • 형식
    • 사용자 주석에 대하여
    • 함수 선언(prototype)을 읽는 법
    • 매뉴얼에 실려있는 PHP 버전
    • PHP에 대한 더 많은 정보 찾는 법
    • 문서 향상을 돕는 법
    • 형식을 어떻게 만들었는가
    • 번역
  • Creative Commons Attribution 3.0
  • 인덱스 목록
    • 함수와 메소드 목록
    • 예제 목록
  • 변경점

User Contributed Notes

There are no user contributed notes for this page.


PHP와 PHP 관련 프로젝트의 역사

Table of Contents

  • PHP의 역사
  • PHP 관련 프로젝트의 역사
  • PHP 관련 서적
  • PHP에 관한 출판물

PHP는 최근 몇년간 출세가도를 달려왔다. 웹을 강화하는 가장 부각되는 언어중 하나로 성장하는 것은 쉬운 일이 아니었다. 오늘날 PHP가 어떻게 성장했는지에 대해 간단히 보고자 한다면, 계속 읽어나가자. 구버전의 PHP는 » PHP Museum에서 확인할수있다.

User Contributed Notes

curious at user dot com 21-Jan-2016 01:25
Can these notes be updated for PHP 6 / 7 ?

The history is great.
tuxedobob 03-Oct-2015 04:01
As the nationality of Zeev and Andi was mentioned, I was curious as to where Rasmus hailed from.

Wikipedia says he was born in Greenland and moved to Canada in 1980.
Davi_af06 at hotmail dot com 25-Sep-2014 08:59
Ola, lendo e gostando muito da historia da Linguagem PHP percebi que no titulo PHP 5 inicia o paragrafo falando que o PHP 3 foi lancado em 2004, nao entendi, esta correto ?
Abraco!
aowie1 at gmail dot com 12-Oct-2013 05:12
PHP is actually a recursive initialism. Not a recursive acronym.

The difference is that an acronym is reserved for initials that are pronounced as a word and not by their individual letters. The latter refers to an initialism.
arkebion at gmail dot com 28-Aug-2013 09:33
Some may say that history of a programming or scripting language is not important, but i think it is very crucial if you want to understand the architectural development and reasons behind it. So presentation can be important too, i think this is a more clear version of this document: http://line.do/history-of-php/r5q4x1
fontajos at phpeppershop dot com 02-Feb-2013 04:57
PHP grows and grows

A Netcraft survey from January 2013 shows that PHP is used on 244 million websites. That is a total of 39% of all servers monitored (2.1 million servers/vm's).

http://news.netcraft.com/archives/2013/01/31/php-just-grows-grows.html


PHP의 역사

PHP/FI

PHP는 PHP/FI라는 이름의 오래된 소프트웨어를 계승했다. PHP/FI는 1995년에 Rasmus Lerdorf에 의해 만들어졌다. 그의 온라인 이력서의 접속수를 추적하기 위한 단순한 펄 스크립트 구성물로 시작되었다. 그는 그 스트립트 구성물을 'Personal Home Page Tools'라고 명명했다. 좀더 많은 기능이 요구됨에 따라, Rasmus는 좀 더 많은 C 구현물을 작성했다. 이 구현물은 데이터베이스와 의사소통을 하고, 유저가 단순한 동적 웹 애플리케이션을 작성할수 있게 하였다. Rasmus는 모든 사람이 볼수 있도록 PHP/FI의 소스 코드를 » release하기로 결정했다. 그래서 모든 사람이 그것을 사용하고, 버그를 고칠수 있을뿐만 아니라 코드를 향상시킬수 있었다.

PHP/FI, Personal Home Page / Forms Interpreter의 약어, 는 오늘날 우리가 알고있는것처럼 PHP의 기본적인 기능의 일부를 포함하고 있다. 그것은 펄과 비슷하게 생긴 변수, 폼 변수의 자동 해석과 HTML 내장 문법을 갖고 있었다. 그 문법 자체는 좀더 제한적이고, 단순하고 약간은 일관성이 떨어지기는 하지만 펄과 비슷했다.

1997년까지, PHP/FI 2.0, C로 쓰여진 두번째 구현물,은 전세계에서 수천 사용자의 숭배자를 갖게 된다(추정치). 그리고 대략 50,000의 도메인에서 설치되었던것으로 보고되었고, 이수치는 인터넷 모든 도메인의 약 1%에 해당된다. 이 프로젝트에 많은 코드를 공헌한 몇명의 사람이 있었지만, 아직은 대체로 한사람의 프로젝트였다.

PHP/FI 2.0은 그 삶의 대부분을 베타 릴리즈로 보낸 이후, 1997년 11월에만 공식적으로 릴리즈되었다. 그후 얼마안되어 PHP 3.0의 첫번째 알파버전에 의해 계승되었다.

PHP 3

PHP 3.0은 오늘날 알려진 PHP와 매우 가깝게 닮아있는 첫번째 버전이었다. 그것은 1997년 Andi Gutmans 와 Zeev Suraski에 의해 의해 완전히 재작성되어 만들어졌다. 그들이 대학 프로젝트 수행시 이커머스(eCommerce) 응용프로그램을 개발하기 위해 기능이 떨어지는 PHP/FI 2.0을 발견한것이 계기가 되었다. PHP/FI의 기존 사용자 기반위에서 협력하고 개발을 시작할 의도를 가지고, Andi, Rasmus, Zeev는 PHP/FI 2.0의 공식적인 계승자로서 PHP 3.0을 발표하였고, PHP/FI 2.0의 개발을 대부분 중단하게 되었다.

PHP 3.0의 가장 큰 강점중의 하나는 강력한 확장 기능이었다. 엔드유저에게 다양한 데이터베이스접근를 위한 견고한 인프라구조, 여러가지 프로토콜과 API를 제공해주었고, 이런 PHP 3.0의 확장 기능은 수십명의 개발자가 개발에 참여하도록 유도했고, 그들은 새로운 확장 모듈을 개발해주었다. 확실하게, 이런 특징이 PHP 3.0의 엄청난 성공의 열쇠였다. PHP 3.0으로 유도하는 다른 핵심 기능이 객체 지향 문법 지원과 좀더 파워풀하고 일관성있는 언어 문법이었다.

모든 새로운 언어는 새로운 이름하에 릴리즈되었다. PHP/FI 2.0이라는 이름이 내포하는 제한된 개인 사용 목적에 대한 연루성을 제거하고, 그 이름을 'PHP'로 명명하였다. 이 단어는 PHP: Hypertext Preprocessor 의 재귀적 약어이다.

1998년 말까지, PHP는 수만명의 사용자가 설치(추정치)하였고 수십만의 웹사이트에 설치되었다고 보고되었다. 이런 급성장에서, PHP 3.0은 인터넷상의 웹서버중 거의 10%가 설치되었다.

PHP 3.0은 1998년 6월에 공식적으로 릴리즈되었다. 공개적인 테스트 이후 약 9개월만이었다.

PHP 4

1998년 겨울까지, PHP 3.0이 공식적으로 릴리즈된후 얼마안되어서, Andi Gutmans 와 Zeev Suraski는 PHP의 코어를 재작성하는 작업을 시작했다. 설계 목적은 복잡한 응용프로그램의 퍼포먼스를 향상시키고, PHP의 코드 베이스의 모듈화를 향상시키는것이었다. 그런 응용프로그램은 PHP 3.0의 새로운 기능과 다양하고 광범위한 서드파티 데이터베이스와 API의 지원으로 가능하게 되었다. 그러나, PHP 3.0은 그런 복잡한 응용 프로그램을 효율적으로 제어하도록 설계되지는 않았었다.

새로운 엔진, 'Zend Engine'로 명명됨(그들 Zeev와 Andi의 이름으로 구성됨), 은 이런 설계 목적에 성공적으로 부합하였다. 그리고 1999년 중반 처음으로 소개되었다. 이 엔진에 기반하였고, 광범위한 새 추가 기능과 같이 동작하는 PHP 4.0은 2000년 5월에 공식적으로 릴리즈되었다. 전임자인 PHP 3.0이 나온지 거의 2년만이다. 이 버전의 크게 향상된 퍼포먼스 뿐만아니라, PHP 4.0은 다른 핵심 기능도 제공하고 있다. 여러가지 웹서버에 대한 지원, HTTP 세션, 출력 버퍼링, 보안이 강화된 유저 입력 제어, 몇가지 새로운 언어 구조이다.

현재, PHP는 수십만명의 개발자들에 의해서 사용되고있고(추정치), 수백만 사이트에 설치되어있다고 보고되었다. 이 수치는 인터넷상에서 20% 이상의 도메인 수가 된다.

PHP 개발팀은 수십명의 개발자들을 포함하고, 그외에 PEAR, 문서 프로젝트와 같은 PHP 관련 프로젝트에서 수십명이 참여 중이다.

PHP 5

PHP 5는 긴 개발 기간과 많은 프리릴리즈를 거쳐 2004년 7월에 릴리즈 되었습니다. 주 변경은 핵심으로, 새로운 객체 모델과 수많은 새 기능을 가진 젠드 엔진 2.0으로 작동합니다.

User Contributed Notes

There are no user contributed notes for this page.


PHP 관련 프로젝트의 역사

PEAR

» PEAR, PHP Extension and Application Repository (원래, PHP Extension and Add-on Repository)는 PHP의 기초 클래스 버전이다. 앞으로 개발자들 사이에서 PHP와 C기반 PHP확장 모두를 제공하는 핵심 수단의 하나로 성장할 것이다.

PEAR는 Tel Aviv에서 2000년 1월에 개최된 PHP Developers' Meeting (PDM) 의 논의를 거쳐 탄생하게 되었다. Stig S. Bakken가 만들었고, 그의 첫번째 딸인 Malin Bakken에게 헌정되었다.

2000 초기 이래로, PEAR는 거대하고, 중요한 프로젝트로 성장하였고, 수많은 개발자들이 공통된, 재사용가능한 기능을 전체 PHP 커뮤니티의 이익을 위해 개발하고 있다. 오늘날 PEAR는 데이터베이스 접속, 컨텐츠 캐슁, 수학적 계산, 이커머스(eCommerce) 등등을 위한 광범위한 인프라구조 기초 클래스를 포함한다.

PEAR에 대한 자세한 정보는 » 매뉴얼에서 찾을 수 있습니다.

PHP 품질 보증 조직

» PHP 품질 보증 조직은 2000년 여름에 PHP 릴리즈가 제조 환경을 위해 충분히 테스트되지 않는다는 토론에 대응하여 설치되었다. 이 팀은 PHP 코드 베이스의 충분한 이해를 갖고있는 핵심 개발 그룹으로 구성되었다. 이 개발자들은 PHP를 지역화하고 PHP 내의 버그를 고치는데 그들 대부분의 시간을 보낸다. 추가로 다양한 플랫폼을 사용하여 이런 버그 수리에 대하여 많은 다른 팀 멤버도 피드백을 제공중이다.

PHP-GTK

» PHP-GTK는 클라이언트측 GUI 응용프로그램을 작성하기 위한 PHP 솔루션이다. Andrei Zmievski는 PHP-GTK를 계획하고 창조한 과정을 다음과 같이 회고한다:

GUI 프로그래밍은 내가 항상 관심을 갖어왔었고, C안에서 이 언어로 프로그램하는것은 조금은 지루하지만 Gtk+가 가장 좋은 툴킷이라는것을 발견하였다. PyGtk 과 GTK-Perl 구현을 관찰한 후에, 나는 PHP가 최소한 Gtk+와 인터페이스되어 만들어질수 있는지 확인하기로 결정했다. 2000년 8월에 시작하여서, 실험을 시작할수 있는 때가 되도록 좀더 많은 자유 시간을 갖기 시작했다. 나의 주요 정책은 PyGtk 구현이었는데 그것은 기능적으로 충분히 완벽하게 동작하게 하고 적절한 객체 지향 인터페이스를 갖는것이었다. James Henstridge,파이썬의 저자, 는 이 초기 단계에서 매우 도움이 되는 조언을 제공해주었다.

모든 Gtk+ 함수의 인터페이스를 손으로 쓰는것은 문제가 있었다. PyGtk가 동작하는 방식과 비슷하게 동작하도록 만들기 위해 나는 코드 생성기에 대한 아이디어에 골몰하게 되었고, 이 코드 생성기는 Gtk+ 클래스, 상수, 메소드 정보를 포함하는 .defs 파일 구성체를 읽어들이고 그들과 같이 PHP와 인터페이스화하는 C 코드를 생성하는 PHP 프로그램이다. 자동적으로 생성될수 없는 것은 손으로 .overrides 파일을 작성하였다.

코드 생성기와 인프라구조의 작업은 약간의 시간이 걸렸다. 왜냐하면, 나는 2000년 가을에 PHP-GTK에 거의 시간을 쓸수 없었기 때문이다. Frank Kromann에게 PHP-GTK를 보여준 후, 그는 흥미을 보였고 코드 생성기 작업과 윈32 구현에 대해 나를 도와주기 시작했다. 우리가 처음으로 Hello World 프로그램을 작성하고 그 프로그램을 시작으로 극도로 흥분하게 되었다. 소개용 환경의 프로젝트를 마치는데 약 두달이 걸렸고 2001년 3월 1일에 첫번째 버전을 릴리즈했다. 이 이야기는 SlashDot에서 바로 히트하게 되었다.

PHP-GTK가 확장될수 있을거라는 생각으로, 나는 그것에 관한 별도의 메일링리스트와 CVS 저장소를 설치한다. 또한 Colin Viebrock의 도움으로 gtk.php.net 웹사이트도 만들게 된다. 문서화도 또한 필요했고 James Moore는 이 작업에 도움을 주었다.

릴리즈되고 나서 PHP-GTK는 유행하게 되었다. 우리는 문서화 팀을 구성했고 이 매뉴얼은 계속 향상되고 있다.그래서 사람들은 PHP-GTK의 확장을 쓰기 시작했고 더 많은 흥미로운 응용프로그램을 작성하게 되었다.

User Contributed Notes

There are no user contributed notes for this page.


PHP 관련 서적

PHP가 성장함에 따라, 그것은 전세계적으로 유행하는 개발 플랫폼으로 인식되기 시작했다. 이런 추세를 확인하는 가장 재미있는 방법이 몇년동안 나온 PHP 관련 서적을 찾아보는것이다.

우리가 알기로는, PHP에 관련된 최초의 서적은 'PHP - tvorba interaktivnich internetovych aplikaci' - 1999년 4월에 체코에서 출판된 책이다, Jirka Kosek가 저자이다. 다음해에 Egon Schmid, Christian Cartus, and Richard Blume 가 저술한 독일 서적이 출판되었다. PHP에 관한 최초의 영문 서적은 그후 얼마 안되어서 Leon Atkinson에 의해 저술된 'Core PHP Programming' 이었는데, 이 책 모두 PHP 3.0을 다루고 있다.

이런 책들이 PHP 관련의 최초 서적이었고, 다수의 저자와 출판인들에 의해 수많은 책들이 출판되었다. 영어로는 40권이상, 독일어로는 50권 이상, 프랑스어로는 20권 이상이다! 추가로, 다른 많은 언어의 PHP 관련서적을 발견할수 있다. 스페인어, 한국어, 일본어, 이스라엘어가 포함된다.

확실히, 이렇게 수많은 책들, 다양한 저자에 의해 저술되고, 많은 출판인들에 의해 출판되고, 매우 많은 언어로 쓰여졌다는것은 PHP의 전세계적인 성공에 대한 강력한 반증이다.

User Contributed Notes

pettersoft at gmail dot com 03-Dec-2015 04:06
There are books in Swedish as well. I remember loaning a PHP book more than half a decade ago. Can't remember which PHP version it covered since I was only about 13-14 then. Frequent PHP programmer at home and .NET at work. PHP really opened up my eyes for programming and web development.


PHP에 관한 출판물

우리가 아는한, 잡지에서 PHP에 관한 최초의 기사는 1998년 봄의 Computerworld 체코 판에 출판되었다. 이 기사는 PHP 3.0을 다루고 있다. 이 기사는 많은 유명 잡지중에서 PHP 관련 시리즈물의 최초기사였다.

PHP에 관한 기사는 Dr. Dobbs, Linux Enterprise, Linux Magazine 등등에서 보이게 되었고, ASP기반 응용 프로그램을 윈도우의 PHP로 마이그레이션하는 기사는 마이크로소프트의 MSDN에도 보인다!

User Contributed Notes

craig dot cmehil at sap dot com 31-Aug-2005 02:39
For those moving into specific areas of PHP and interacting with other systems there are some great publications about SAP, http://www.sap.com, interacting with PHP found on SDN, http://sdn.sap.com.

https://forums.sdn.sap.com/thread.jspa?threadID=38415&tstart=0


Migrating from PHP 7.0.x to PHP 7.1.x

Table of Contents

  • New features
  • New functions
  • New global constants
  • Backward incompatible changes
  • Deprecated features in PHP 7.1.x
  • Changed functions
  • Other changes

This new minor version brings with it a number of new features and a few incompatibilities that should be tested for before switching PHP versions in production environments.

PHP 버전에 맞는 이행 가이드를 참고하십시오. 5.0.x, 5.1.x, 5.2.x, 5.3.x, 5.4.x, 5.5.x, 5.6.x and 7.0.x.

User Contributed Notes

There are no user contributed notes for this page.


New features

Nullable types

Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, null can be passed as an argument, or returned as a value, respectively.

<?php

function testReturn(): ?string
{
    return 'elePHPant';
}

var_dump(testReturn());

function testReturn(): ?string
{
    return null;
}

var_dump(testReturn());

function test(?string $name)
{
    var_dump($name);
}

test('elePHPant');
test(null);
test();

위 예제의 출력:

string(10) "elePHPant"
NULL
string(10) "elePHPant"
NULL
Uncaught Error: Too few arguments to function test(), 0 passed in...

Void functions

A void return type has been introduced. Functions declared with void as their return type must either omit their return statement altogether, or use an empty return statement. null is not a valid return value for a void function.

<?php
function swap(&$left, &$right) : void
{
    if ($left === $right) {
        return;
    }

    $tmp = $left;
    $left = $right;
    $right = $tmp;
}

$a = 1;
$b = 2;
var_dump(swap($a, $b), $a, $b);

위 예제의 출력:

null
int(2)
int(1)

Attempting to use a void function's return value simply evaluates to null, with no warnings emitted. The reason for this is because warnings would implicate the use of generic higher order functions.

Symmetric array destructuring

The shorthand array syntax ([]) may now be used to destructure arrays for assignments (including within foreach), as an alternative to the existing list() syntax, which is still supported.

<?php
$data = [
    [1, 'Tom'],
    [2, 'Fred'],
];

// list() style
list($id1, $name1) = $data[0];

// [] style
[$id1, $name1] = $data[0];

// list() style
foreach ($data as list($id, $name)) {
    // logic here with $id and $name
}

// [] style
foreach ($data as [$id, $name]) {
    // logic here with $id and $name
}

Class constant visibility

Support for specifying the visibility of class constants has been added.

<?php
class ConstDemo
{
    const PUBLIC_CONST_A = 1;
    public const PUBLIC_CONST_B = 2;
    protected const PROTECTED_CONST = 3;
    private const PRIVATE_CONST = 4;
}

iterable pseudo-type

A new pseudo-type (similar to callable) called iterable has been introduced. It may be used in parameter and return types, where it accepts either arrays or objects that implement the Traversable interface. With respect to subtyping, parameter types of child classes may broaden a parent's declaration of array or Traversable to iterable. With return types, child classes may narrow a parent's return type of iterable to array or an object that implements Traversable.

<?php
function iterator(iterable $iter)
{
    foreach ($iter as $val) {
        //
    }
}

Multi catch exception handling

Multiple exceptions per catch block may now be specified using the pipe character (|). This is useful for when different exceptions from different class hierarchies are handled the same.

<?php
try {
    // some code
} catch (FirstException | SecondException $e) {
    // handle first and second exceptions
}

Support for keys in list()

You can now specify keys in list(), or its new shorthand [] syntax. This enables destructuring of arrays with non-integer or non-sequential keys.

<?php
$data = [
    ["id" => 1, "name" => 'Tom'],
    ["id" => 2, "name" => 'Fred'],
];

// list() style
list("id" => $id1, "name" => $name1) = $data[0];

// [] style
["id" => $id1, "name" => $name1] = $data[0];

// list() style
foreach ($data as list("id" => $id, "name" => $name)) {
    // logic here with $id and $name
}

// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
    // logic here with $id and $name
}

Support for negative string offsets

Support for negative string offsets has been added to the string manipulation functions accepting offsets, as well as to string indexing with [] or {}. In such cases, a negative offset is interpreted as being an offset from the end of the string.

<?php
var_dump("abcdef"[-2]);
var_dump(strpos("aabbcc", "b", -3));

위 예제의 출력:

string (1) "e"
int(3)

Negative string and array offsets are now also supported in the simple variable parsing syntax inside of strings.

<?php
$string = 'bar';
echo "The last character of '$string' is '$string[-1]'.\n";
?>

위 예제의 출력:

The last character of 'bar' is 'r'.

Support for AEAD in ext/openssl

Support for AEAD (modes GCM and CCM) have been added by extending the openssl_encrypt() and openssl_decrypt() functions with additional parameters.

Convert callables to Closures with Closure::fromCallable()

A new static method has been introduced to the Closure class to allow for callables to be easily converted into Closure objects.

<?php
class Test
{
    public function exposeFunction()
    {
        return Closure::fromCallable([$this, 'privateFunction']);
    }

    private function privateFunction($param)
    {
        var_dump($param);
    }
}

$privFunc = (new Test)->exposeFunction();
$privFunc('some value');

위 예제의 출력:

string(10) "some value"

Asynchronous signal handling

A new function called pcntl_async_signals() has been introduced to enable asynchronous signal handling without using ticks (which introduce a lot of overhead).

<?php
pcntl_async_signals(true); // turn on async signals

pcntl_signal(SIGHUP,  function($sig) {
    echo "SIGHUP\n";
});

posix_kill(posix_getpid(), SIGHUP);

위 예제의 출력:

SIGHUP

HTTP/2 server push support in ext/curl

Support for server push has been added to the CURL extension (requires version 7.46 and above). This can be leveraged through the curl_multi_setopt() function with the new CURLMOPT_PUSHFUNCTION constant. The constants CURL_PUSH_OK and CURL_PUSH_DENY have also been added so that the execution of the server push callback can either be approved or denied.

User Contributed Notes

There are no user contributed notes for this page.


New functions

Closure

  • Closure::fromCallable()

CURL

  • curl_multi_errno()
  • curl_share_errno()
  • curl_share_strerror()

SPL

  • is_iterable()

PCNTL

  • pcntl_async_signals()

User Contributed Notes

There are no user contributed notes for this page.


New global constants

Core Predefined Constants

  • PHP_FD_SETSIZE

CURL

  • CURLMOPT_PUSHFUNCTION
  • CURL_PUSH_OK
  • CURL_PUSH_DENY

Data Filtering

  • FILTER_FLAG_EMAIL_UNICODE

Image Processing and GD

  • IMAGETYPE_WEBP

JSON

  • JSON_UNESCAPED_LINE_TERMINATORS

LDAP

  • LDAP_OPT_X_SASL_NOCANON
  • LDAP_OPT_X_SASL_USERNAME
  • LDAP_OPT_X_TLS_CACERTDIR
  • LDAP_OPT_X_TLS_CACERTFILE
  • LDAP_OPT_X_TLS_CERTFILE
  • LDAP_OPT_X_TLS_CIPHER_SUITE
  • LDAP_OPT_X_TLS_KEYFILE
  • LDAP_OPT_X_TLS_RANDOM_FILE
  • LDAP_OPT_X_TLS_CRLCHECK
  • LDAP_OPT_X_TLS_CRL_NONE
  • LDAP_OPT_X_TLS_CRL_PEER
  • LDAP_OPT_X_TLS_CRL_ALL
  • LDAP_OPT_X_TLS_DHFILE
  • LDAP_OPT_X_TLS_CRLFILE
  • LDAP_OPT_X_TLS_PROTOCOL_MIN
  • LDAP_OPT_X_TLS_PROTOCOL_SSL2
  • LDAP_OPT_X_TLS_PROTOCOL_SSL3
  • LDAP_OPT_X_TLS_PROTOCOL_TLS1_0
  • LDAP_OPT_X_TLS_PROTOCOL_TLS1_1
  • LDAP_OPT_X_TLS_PROTOCOL_TLS1_2
  • LDAP_OPT_X_TLS_PACKAGE
  • LDAP_OPT_X_KEEPALIVE_IDLE
  • LDAP_OPT_X_KEEPALIVE_PROBES
  • LDAP_OPT_X_KEEPALIVE_INTERVAL

PostgreSQL

  • PGSQL_NOTICE_LAST
  • PGSQL_NOTICE_ALL
  • PGSQL_NOTICE_CLEAR

SPL

  • MT_RAND_PHP

User Contributed Notes

There are no user contributed notes for this page.


Backward incompatible changes

Throw on passing too few function arguments

Previously, a warning would be emitted for invoking user-defined functions with too few arguments. Now, this warning has been promoted to an Error exception. This change only applies to user-defined functions, not internal functions. For example:

<?php
function test($param){}
test();

위 예제의 출력 예시:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function test(), 0 passed in %s on line %d and exactly 1 expected in %s:%d

Forbid dynamic calls to scope introspection functions

Dynamic calls for certain functions have been forbidden (in the form of $func() or array_map('extract', ...), etc). These functions either inspect or modify another scope, and present with them ambiguous and unreliable behavior. The functions are as follows:

  • assert() - with a string as the first argument
  • compact()
  • extract()
  • func_get_args()
  • func_get_arg()
  • func_num_args()
  • get_defined_vars()
  • mb_parse_str() - with one arg
  • parse_str() - with one arg
<?php
(function () {
    $func = 'func_num_args';
    $func();
})();

위 예제의 출력:

Warning: Cannot call func_num_args() dynamically in %s on line %d

Invalid class, interface, and trait names

The following names cannot be used to name classes, interfaces, or traits:

  • void
  • iterable

Numerical string conversions now respect scientific notation

Integer operations and conversions on numerical strings now respect scientific notation. This also includes the (int) cast operation, and the following functions: intval() (where the base is 10), settype(), decbin(), decoct(), and dechex().

Fixes to mt_rand() algorithm

mt_rand() will now default to using the fixed version of the Mersenne Twister algorithm. If deterministic output from mt_srand() was relied upon, then the MT_RAND_PHP with the ability to preserve the old (incorrect) implementation via an additional optional second parameter to mt_srand().

rand() aliased to mt_rand() and srand() aliased to mt_srand()

rand() and srand() have now been made aliases to mt_rand() and mt_srand(), respectively. This means that the output for the following functions have changes: rand(), shuffle(), str_shuffle(), and array_rand().

Disallow the ASCII delete control character in identifiers

The ASCII delete control character (0x7F) can no longer be used in identifiers that are not quoted.

error_log changes with syslog value

If the error_log ini setting is set to syslog, the PHP error levels are mapped to the syslog error levels. This brings finer differentiation in the error logs in contrary to the previous approach where all the errors are logged with the notice level only.

Do not call destructors on incomplete objects

Destructors are now never called for objects that throw an exception during the execution of their constructor. In previous versions this behavior depended on whether the object was referenced outside the construcor (e.g. by an exception backtrace).

call_user_func() handling of reference arguments

call_user_func() will now always generate a warning upon calls to functions that expect references as arguments. Previously this depended on whether the call was fully qualified.

Additionally, call_user_func() and call_user_func_array() will no longer abort the function call in this case. The "expected reference" warning will be emitted, but the call will proceed as usual.

The empty index operator is not supported for strings anymore

Applying the empty index operator to a string (e.g. $str[] = $x) throws a fatal error instead of converting silently to array.

Removed ini directives

The following ini directives have been removed:

  • session.entropy_file
  • session.entropy_length
  • session.hash_function
  • session.hash_bits_per_character

Array ordering when elements are automatically created during by reference assignments has changed

The order of the elements in an array has changed when those elements have been automatically created by referencing them in a by reference assignment. For example:

<?php
$array = [];
$array["a"] =& $array["b"];
$array["b"] = 1;
var_dump($array);
?>

Output of the above example in PHP 7.0:

array(2) {
  ["a"]=>
  &int(1)
  ["b"]=>
  &int(1)
}

Output of the above example in PHP 7.1:

array(2) {
  ["b"]=>
  &int(1)
  ["a"]=>
  &int(1)
}

Sort order of equal elements

The internal sorting algorithm has been improved, what may result in different sort order of elements, which compare as equal, than before.

Note:

Don't rely on the order of elements which compare as equal; it might change anytime.

User Contributed Notes

There are no user contributed notes for this page.


Deprecated features in PHP 7.1.x

ext/mcrypt

The mcrypt extension has been abandonware for nearly a decade now, and was also fairly complex to use. It has therefore been deprecated in favour of OpenSSL, where it will be removed from the core and into PECL in PHP 7.2.

Eval option for mb_ereg_replace() and mb_eregi_replace()

The e pattern modifier has been deprecated for the mb_ereg_replace() and mb_eregi_replace() functions.

User Contributed Notes

There are no user contributed notes for this page.


Changed functions

PHP Core

  • getopt() has an optional third parameter that exposes the index of the next element in the argument vector list to be processed. This is done via a by-ref parameter.
  • getenv() no longer requires its parameter. If the parameter is omitted, then the current environment variables will be returned as an associative array.
  • get_headers() now has an additional parameter to enable for the passing of custom stream contexts.
  • long2ip() now also accepts an integer as a parameter.
  • output_reset_rewrite_vars() no longer resets session URL rewrite variables.
  • parse_url() is now more restrictive and supports RFC3986.
  • unpack() now accepts an optional third parameter to specify the offset to begin unpacking from.

File System

  • tempnam() now emits a notice when falling back to the system's temp directory.

JSON

  • json_encode() now accepts a new option, JSON_UNESCAPED_LINE_TERMINATORS, to disable the escaping of U+2028 and U+2029 characters when JSON_UNESCAPED_UNICODE is supplied.

Multibyte String

  • mb_ereg() now rejects illegal byte sequences.
  • mb_ereg_replace() now rejects illegal byte sequences.

PDO

  • PDO::lastInsertId() for PostgreSQL will now trigger an error when nextval has not been called for the current session (the postgres connection).

PostgreSQL

  • pg_last_notice() now accepts an optional parameter to specify an operation. This can be done with one of the following new constants: PGSQL_NOTICE_LAST, PGSQL_NOTICE_ALL, or PGSQL_NOTICE_CLEAR.
  • pg_fetch_all() now accepts an optional second parameter to specify the result type (similar to the third parameter of pg_fetch_array()).
  • pg_select() now accepts an optional fourth parameter to specify the result type (similar to the third parameter of pg_fetch_array()).

User Contributed Notes

There are no user contributed notes for this page.


Other changes

Notices and warnings on arithmetic with invalid strings

New E_WARNING and E_NOTICE errors have been introduced when invalid strings are coerced using operators expecting numbers (+ - * / ** % << >> | & ^) or their assignment equivalents. An E_NOTICE is emitted when the string begins with a numeric value but contains trailing non-numeric characters, and an E_WARNING is emitted when the string does not contain a numeric value.

<?php
'1b' + 'something';

위 예제의 출력:

Notice: A non well formed numeric value encountered in %s on line %d
Warning: A non-numeric value encountered in %s on line %d

Warn on octal escape sequence overflow

Previously, 3-octet octal string escape sequences would overflow silently. Now, they will still overflow, but E_WARNING will be emitted.

<?php
var_dump("\500");

위 예제의 출력:

Warning: Octal escape sequence overflow \500 is greater than \377 in %s on line %d
string(1) "@"

Inconsistency fixes to $this

Whilst $this is considered a special variable in PHP, it lacked proper checks to ensure it wasn't used as a variable name or reassigned. This has now been rectified to ensure that $this cannot be a user-defined variable, reassigned to a different value, or be globalised.

Session ID generation without hashing

Session IDs will no longer be hashed upon generation. With this change brings about the removal of the following four ini settings:

  • session.entropy_file
  • session.entropy_length
  • session.hash_function
  • session.hash_bits_per_character

And the addition of the following two ini settings:

  • session.sid_length - defines the length of the session ID, defaulting to 32 characters for backwards compatibility)
  • session.sid_bits_per_character - defines the number of bits to be stored per character (i.e. increases the range of characters that can be used in the session ID), defaulting to 4 for backwards compatibility

User Contributed Notes

There are no user contributed notes for this page.


Migrating from PHP 5.6.x to PHP 7.0.x

Table of Contents

  • 이전 버전과 호환되지 않는 변경점
  • New features
  • Deprecated features in PHP 7.0.x
  • Changed functions
  • New functions
  • New Classes and Interfaces
  • New Global Constants
  • Changes in SAPI Modules
  • Removed Extensions and SAPIs
  • 기타 변경사항

Despite the fact that PHP 7.0 is a new major version, efforts have been made to make migration as painless as possible. This release focuses mainly on removing functionality deprecated in previous versions and improving language consistency.

There are a few incompatibilities and new features that should be considered, and code should be tested before switching PHP versions in production environments.

PHP 버전에 맞는 이행 가이드를 참고하십시오. 5.0.x, 5.1.x, 5.2.x, 5.3.x, 5.4.x, 5.5.x and 5.6.x.

User Contributed Notes

There are no user contributed notes for this page.


이전 버전과 호환되지 않는 변경점

Changes to error and exception handling

Many fatal and recoverable fatal errors have been converted to exceptions in PHP 7. These error exceptions inherit from the Error class, which itself implements the Throwable interface (the new base interface all exceptions inherit).

This means that custom error handlers may no longer be triggered because exceptions may be thrown instead (causing new fatal errors for uncaught Error exceptions).

A fuller description of how errors operate in PHP 7 can be found on the PHP 7 errors page. This migration guide will merely enumerate the changes that affect backward compatibility.

set_exception_handler() is no longer guaranteed to receive Exception objects

Code that implements an exception handler registered with set_exception_handler() using a type declaration of Exception will cause a fatal error when an Error object is thrown.

If the handler needs to work on both PHP 5 and 7, you should remove the type declaration from the handler, while code that is being migrated to work on PHP 7 exclusively can simply replace the Exception type declaration with Throwable instead.

<?php
// PHP 5 era code that will break.
function handler(Exception $e) { ... }
set_exception_handler('handler');

// PHP 5 and 7 compatible.
function handler($e) { ... }

// PHP 7 only.
function handler(Throwable $e) { ... }
?>

Internal constructors always throw exceptions on failure

Previously, some internal classes would return NULL or an unusable object when the constructor failed. All internal classes will now throw an Exception in this case in the same way that user classes already had to.

Parse errors throw ParseError

Parser errors now throw a ParseError object. Error handling for eval() should now include a catch block that can handle this error.

E_STRICT notices severity changes

All of the E_STRICT notices have been reclassified to other levels. E_STRICT constant is retained, so calls like error_reporting(E_ALL|E_STRICT) will not cause an error.

E_STRICT notices severity changes
Situation | New level/behaviour
Indexing by a resource | E_NOTICE
Abstract static methods | Notice removed, triggers no error
"Redefining" a constructor | Notice removed, triggers no error
Signature mismatch during inheritance | E_WARNING
Same (compatible) property in two used traits | Notice removed, triggers no error
Accessing static property non-statically | E_NOTICE
Only variables should be assigned by reference | E_NOTICE
Only variables should be passed by reference | E_NOTICE
Calling non-static methods statically | E_DEPRECATED

Changes to variable handling

PHP 7 now uses an abstract syntax tree when parsing source files. This has permitted many improvements to the language which were previously impossible due to limitations in the parser used in earlier versions of PHP, but has resulted in the removal of a few special cases for consistency reasons, which has resulted in backward compatibility breaks. These cases are detailed in this section.

Changes to the handling of indirect variables, properties, and methods

Indirect access to variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the previous mix of special cases. The table below shows how the order of evaluation has changed.

Old and new evaluation of indirect expressions
Expression | PHP 5 interpretation | PHP 7 interpretation
$$foo['bar']['baz'] | ${$foo['bar']['baz']} | ($$foo)['bar']['baz']
$foo->$bar['baz'] | $foo->{$bar['baz']} | ($foo->$bar)['baz']
$foo->$bar['baz']() | $foo->{$bar['baz']}() | ($foo->$bar)['baz']()
Foo::$bar['baz']() | Foo::{$bar['baz']}() | (Foo::$bar)['baz']()

Code that used the old right-to-left evaluation order must be rewritten to explicitly use that evaluation order with curly braces (see the above middle column). This will make the code both forwards compatible with PHP 7.x and backwards compatible with PHP 5.x.

Changes to list() handling

list() no longer assigns variables in reverse order

list() will now assign values to variables in the order they are defined, rather than reverse order. In general, this only affects the case where list() is being used in conjunction with the array [] operator, as shown below:

<?php
list($a[], $a[], $a[]) = [1, 2, 3];
var_dump($a);
?>

Output of the above example in PHP 5:

array(3) {
  [0]=>
  int(3)
  [1]=>
  int(2)
  [2]=>
  int(1)
}

Output of the above example in PHP 7:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

In general, it is recommended not to rely on the order in which list() assignments occur, as this is an implementation detail that may change again in the future.

Empty list() assignments have been removed

list() constructs can no longer be empty. The following are no longer allowed:

<?php
list() = $a;
list(,,) = $a;
list($x, list(), $y) = $a;
?>
list() cannot unpack strings

list() can no longer unpack string variables. str_split() should be used instead.

Array ordering when elements are automatically created during by reference assignments has changed

The order of the elements in an array has changed when those elements have been automatically created by referencing them in a by reference assignment. For example:

<?php
$array = [];
$array["a"] =& $array["b"];
$array["b"] = 1;
var_dump($array);
?>

Output of the above example in PHP 5:

array(2) {
  ["b"]=>
  &int(1)
  ["a"]=>
  &int(1)
}

Output of the above example in PHP 7:

array(2) {
  ["a"]=>
  &int(1)
  ["b"]=>
  &int(1)
}

global only accepts simple variables

Variable variables can no longer be used with the global keyword. The curly brace syntax can be used to emulate the previous behaviour if required:

<?php
function f() {
    // Valid in PHP 5 only.
    global $$foo->bar;

    // Valid in PHP 5 and 7.
    global ${$foo->bar};
}
?>

As a general principle, using anything other than a bare variable with global is discouraged.

Parentheses around function arguments no longer affect behaviour

In PHP 5, using redundant parentheses around a function argument could quiet strict standards warnings when the function argument was passed by reference. The warning will now always be issued.

<?php
function getArray() {
    return [1, 2, 3];
}

function squareArray(array &$a) {
    foreach ($a as &$v) {
        $v **= 2;
    }
}

// Generates a warning in PHP 7.
squareArray((getArray()));
?>

위 예제의 출력:

Notice: Only variables should be passed by reference in /tmp/test.php on line 13

Changes to foreach

Minor changes have been made to the behaviour of the foreach control structure, primarily around the handling of the internal array pointer and modification of the array being iterated over.

foreach no longer changes the internal array pointer

Prior to PHP 7, the internal array pointer was modified while an array was being iterated over with foreach. This is no longer the case, as shown in the following example:

<?php
$array = [0, 1, 2];
foreach ($array as &$val) {
    var_dump(current($array));
}
?>

Output of the above example in PHP 5:

int(1)
int(2)
bool(false)

Output of the above example in PHP 7:

int(0)
int(0)
int(0)

foreach by-value operates on a copy of the array

When used in the default by-value mode, foreach will now operate on a copy of the array being iterated rather than the array itself. This means that changes to the array made during iteration will not affect the values that are iterated.

foreach by-reference has improved iteration behaviour

When iterating by-reference, foreach will now do a better job of tracking changes to the array made during iteration. For example, appending to an array while iterating will now result in the appended values being iterated over as well:

<?php
$array = [0];
foreach ($array as &$val) {
    var_dump($val);
    $array[1] = 1;
}
?>

Output of the above example in PHP 5:

int(0)

Output of the above example in PHP 7:

int(0)
int(1)

Iteration of non-Traversable objects

Iterating over a non-Traversable object will now have the same behaviour as iterating over by-reference arrays. This results in the improved behaviour when modifying an array during iteration also being applied when properties are added to or removed from the object.

Changes to integer handling

Invalid octal literals

Previously, octal literals that contained invalid numbers were silently truncated (0128 was taken as 012). Now, an invalid octal literal will cause a parse error.

Negative bitshifts

Bitwise shifts by negative numbers will now throw an ArithmeticError:

<?php
var_dump(1 >> -1);
?>

Output of the above example in PHP 5:

int(0)

Output of the above example in PHP 7:

Fatal error: Uncaught ArithmeticError: Bit shift by negative number in /tmp/test.php:2
Stack trace:
#0 {main}
  thrown in /tmp/test.php on line 2

Out of range bitshifts

Bitwise shifts (in either direction) beyond the bit width of an integer will always result in 0. Previously, the behaviour of such shifts was architecture dependent.

Changes to Division By Zero

Previously, when 0 was used as the divisor for either the divide (/) or modulus (%) operators, an E_WARNING would be emitted and false would be returned. Now, the divide operator returns a float as either +INF, -INF, or NAN, as specified by IEEE 754. The modulus operator E_WARNING has been removed and will throw a DivisionByZeroError exception.

<?php
var_dump(3/0);
var_dump(0/0);
var_dump(0%0);
?>

Output of the above example in PHP 5:

Warning: Division by zero in %s on line %d
bool(false)

Warning: Division by zero in %s on line %d
bool(false)

Warning: Division by zero in %s on line %d
bool(false)

Output of the above example in PHP 7:

Warning: Division by zero in %s on line %d
float(INF)

Warning: Division by zero in %s on line %d
float(NAN)

PHP Fatal error:  Uncaught DivisionByZeroError: Modulo by zero in %s line %d

Changes to string handling

Hexadecimal strings are no longer considered numeric

Strings containing hexadecimal numbers are no longer considered to be numeric. For example:

<?php
var_dump("0x123" == "291");
var_dump(is_numeric("0x123"));
var_dump("0xe" + "0x1");
var_dump(substr("foo", "0x1"));
?>

Output of the above example in PHP 5:

bool(true)
bool(true)
int(15)
string(2) "oo"

Output of the above example in PHP 7:

bool(false)
bool(false)
int(0)

Notice: A non well formed numeric value encountered in /tmp/test.php on line 5
string(3) "foo"

filter_var() can be used to check if a string contains a hexadecimal number, and also to convert a string of that type to an integer:

<?php
$str = "0xffff";
$int = filter_var($str, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX);
if (false === $int) {
    throw new Exception("Invalid integer!");
}
var_dump($int); // int(65535)
?>

\u{ may cause errors

Due to the addition of the new Unicode codepoint escape syntax, strings containing a literal \u{ followed by an invalid sequence will cause a fatal error. To avoid this, the leading backslash should be escaped.

Removed functions

call_user_method() and call_user_method_array()

These functions were deprecated in PHP 4.1.0 in favour of call_user_func() and call_user_func_array(). You may also want to consider using variable functions and/or the ... operator.

All ereg* functions

All ereg functions were removed. PCRE is a recommended alternative.

mcrypt aliases

The deprecated mcrypt_generic_end() function has been removed in favour of mcrypt_generic_deinit().

Additionally, the deprecated mcrypt_ecb(), mcrypt_cbc(), mcrypt_cfb() and mcrypt_ofb() functions have been removed in favour of using mcrypt_decrypt() with the appropriate MCRYPT_MODE_* constant.

All ext/mysql functions

All ext/mysql functions were removed. For details about choosing a different MySQL API, see Choosing a MySQL API.

All ext/mssql functions

All ext/mssql functions were removed. For a list of alternatives, see the MSSQL Introduction.

intl aliases

The deprecated datefmt_set_timezone_id() and
IntlDateFormatter::setTimeZoneID() aliases have been
removed in favour of datefmt_set_timezone() and
IntlDateFormatter::setTimeZone(), respectively.

set_magic_quotes_runtime()

set_magic_quotes_runtime(), along with its alias magic_quotes_runtime(), have been removed. They were deprecated in PHP 5.3.0, and became effectively non-functional with the removal of magic quotes in PHP 5.4.0.

set_socket_blocking()

The deprecated set_socket_blocking() alias has been removed in favour of stream_set_blocking().

dl() in PHP-FPM

dl() can no longer be used in PHP-FPM. It remains functional in the CLI and embed SAPIs.

GD Type1 functions

Support for PostScript Type1 fonts has been removed from the GD extension, resulting in the removal of the following functions:

  • imagepsbbox()
  • imagepsencodefont()
  • imagepsextendfont()
  • imagepsfreefont()
  • imagepsloadfont()
  • imagepsslantfont()
  • imagepstext()

Using TrueType fonts and their associated functions is recommended instead.

Removed INI directives

Removed features

The following INI directives have been removed as their associated features have also been removed:

  • always_populate_raw_post_data
  • asp_tags

xsl.security_prefs

The xsl.security_prefs directive has been removed. Instead, the XsltProcessor::setSecurityPrefs() method should be called to control the security preferences on a per-processor basis.

Other backward incompatible changes

New objects cannot be assigned by reference

The result of the new statement can no longer be assigned to a variable by reference:

<?php
class C {}
$c =& new C;
?>

Output of the above example in PHP 5:

Deprecated: Assigning the return value of new by reference is deprecated in /tmp/test.php on line 3

Output of the above example in PHP 7:

Parse error: syntax error, unexpected 'new' (T_NEW) in /tmp/test.php on line 3

Invalid class, interface and trait names

The following names cannot be used to name classes, interfaces or traits:

  • bool
  • int
  • float
  • string
  • NULL
  • TRUE
  • FALSE

Furthermore, the following names should not be used. Although they will not generate an error in PHP 7.0, they are reserved for future use and should be considered deprecated.

  • resource
  • object
  • mixed
  • numeric

ASP and script PHP tags removed

Support for using ASP and script tags to delimit PHP code has been removed. The affected tags are:

Removed ASP and script tags
Opening tag | Closing tag
<% | %>
<%= | %>
<script language="php"> | </script>

Calls from incompatible context removed

Previously deprecated in PHP 5.6, static calls made to a non-static method with an incompatible context will now result in the called method having an undefined $this variable and a deprecation warning being issued.

<?php
class A {
    public function test() { var_dump($this); }
}

// Note: Does NOT extend A
class B {
    public function callNonStaticMethodOfA() { A::test(); }
}

(new B)->callNonStaticMethodOfA();
?>

PHP 5.4에서 위 예제의 출력:

Deprecated: Non-static method A::test() should not be called statically, assuming $this from incompatible context in /tmp/test.php on line 8
object(B)#1 (0) {
}

Output of the above example in PHP 7:

Deprecated: Non-static method A::test() should not be called statically in /tmp/test.php on line 8

Notice: Undefined variable: this in /tmp/test.php on line 3
NULL

yield is now a right associative operator

The yield construct no longer requires parentheses, and has been changed to a right associative operator with precedence between print and =>. This can result in changed behaviour:

<?php
echo yield -1;
// Was previously interpreted as
echo (yield) - 1;
// And is now interpreted as
echo yield (-1);

yield $foo or die;
// Was previously interpreted as
yield ($foo or die);
// And is now interpreted as
(yield $foo) or die;
?>

Parentheses can be used to disambiguate those cases.

Functions cannot have multiple parameters with the same name

It is no longer possible to define two or more function parameters with the same name. For example, the following function will trigger an E_COMPILE_ERROR:

<?php
function foo($a, $b, $unused, $unused) {
    //
}
?>

Functions inspecting arguments report the current parameter value

func_get_arg(), func_get_args(), debug_backtrace() and exception backtraces will no longer report the original value that was passed to a parameter, but will instead provide the current value (which might have been modified).

<?php
function foo($x) {
    $x++;
    var_dump(func_get_arg(0));
}
foo(1);?>

Output of the above example in PHP 5:

1

Output of the above example in PHP 7:

2

Switch statements cannot have multiple default blocks

It is no longer possible to define two or more default blocks in a switch statement. For example, the following will switch statement will trigger an E_COMPILE_ERROR:

<?php
switch (1) {
    default:
    break;
    default:
    break;
}
?>

$HTTP_RAW_POST_DATA removed

$HTTP_RAW_POST_DATA is no longer available. The php://input stream should be used instead.

# comments in INI files removed

Support for prefixing comments with # in INI files has been removed. ; (semi-colon) should be used instead. This change applies to php.ini, as well as files handled by parse_ini_file() and parse_ini_string().

JSON extension replaced with JSOND

The JSON extension has been replaced with JSOND, causing three minor BC breaks. Firstly, a number must not end in a decimal point (i.e. 34. must be changed to either 34.0 or 34). Secondly, when using scientific notation, the e exponent must not immediately follow a decimal point (i.e. 3.e3 must be changed to either 3.0e3 or 3e3). Finally, an empty string is no longer considered valid JSON.

Internal function failure on overflow

Previously, internal functions would silently truncate numbers produced from float-to-integer coercions when the float was too large to represent as an integer. Now, an E_WARNING will be emitted and NULL will be returned.

Fixes to custom session handler return values

Any predicate functions implemented by custom session handlers that return either FALSE or -1 will be fatal errors. If any value from these functions other than a boolean, -1, or 0 is returned, then it will fail and an E_WARNING will be emitted.

Sort order of equal elements

The internal sorting algorithm has been improved, what may result in different sort order of elements, which compare as equal, than before.

Note:

Don't rely on the order of elements which compare as equal; it might change anytime.

User Contributed Notes

viktor dot csiky at nospam dot nospam dot eu 21-Jul-2016 08:16
It is stated:

"foreach by-value operates on a copy of the array

When used in the default by-value mode, foreach will now operate on a copy of the array being iterated rather than the array itself. This means that changes to the array made during iteration will not affect the values that are iterated."

Please note that this is not exactly true. New foreach operates on a copy of the array, by-value or by-reference. It seems that in the latter case, the array copy is simply moved over (to) the original array before it is presumably destroyed.

As a consequence of this, you may not "dereference" an array containing values - e.g. for use with ReflectionMethod::invokeArgs() or the good ole' call_user_func().
Consider the snippet below:

<?php
function deref(Array $inputArray)
{
        $retVal = [];

        foreach ($inputArray as &$inputValue)
        {
            $retVal[] = $inputValue;
        }

        return $retVal;
}
?>

As of PHP 7.0, this *will no longer work*. You will get the usual suspect:

PHP Warning:  Parameter n to whatever() expected to be a reference, value given in baz.php on line x

You need to convert it to explicitly reference the original array:
<?php
function deref(Array $inputArray)
{
        $retVal = [];

        foreach ($inputArray as $inputKey => $inputValue)
        {
            $retVal[$inputKey] = &$inputArray[$inputKey];
        }

        return $retVal;
}
?>

PLEASE NOTE that this might have the unforeseen consequence of your code not working anymore in php versions less than 5.3 (that is, 5.2 and below).
maba at mb-systemhaus dot net 13-May-2016 06:52
NOTE:
the new variable handling in PHP 7 also has side effects on the COM .NET extension. Numeric variants (in the Windows application space) now must be quoted when passed over from PHP. This is the case when the receiving application expects a variable of type variant.

Here is an example:

<?php
  $word = new COM('Word.Application');

  // now load a document, ...

  // the following works in PHP 5 but will throw an exception in PHP 7
  $word->ActiveDocument->PrintOut(false, false, 0, $outfile);

  // the following works in PHP 7 as well, please note the quotes around the '0'
  $word->ActiveDocument->PrintOut(false, false, '0', $outfile);
?>
Frank 12-Apr-2016 12:40
[Editor's Note: that change is listed in the "Changed functions" section.]

The substr function has also been changed in a backward incompatible way.

<?php
substr("test",4);  # false in PHP 5,  "" in PHP 7
?>

In fact, this is the only thing we had to change in a number of places for our code base to run on PHP 7. It's definitely an improvement though, as the old behavior tended to cause bugs in border cases.
mossy2100 19-Dec-2015 02:29
Although $x/0 is technically not infinity in a purely mathematical sense, when you understand why the IEEE float includes a value for infinity, and returns infinity in this case, it makes sense that PHP would agree with this.

The reason is that programmers don't usually divide by 0 on purpose. A value of 0 as a divisor usually occurs due to underflow, i.e. a value which is too small to be represented as a float. So, for example, if you have values like:
$x = 1;
$y = 1e-15 * 1e-15;
$z = $x/$y;
Because $y will have underflowed to 0, the division operation will throw the division by zero warning, and $z will be set to INF. In a better computer, however, $y would not have the value 0 (it would be 1e-30) and $z would not have the value INF (it would be 1e30).

In other words, 0 is not only representative of an actual 0, but also a very small number which float cannot represent correctly (underflow), and INF does not only represent infinity, but also a very big number which float cannot represent correctly (overflow). We do the best we can within the limitations of floating point values, which are really just good approximations of the actual numbers being represented.

What does bother me is that division by zero is handled in two different ways depending on the operator. I would have preferred the new DivisionByZeroError exception to be thrown in all cases, for consistency and to enforce good programming practices.
tuxedobob 11-Dec-2015 06:23
As a programmer, I don't care whether 3/0 is INF or NaN. Both answers are (probably) equally useless, and tell me that something somewhere else is screwed up.
opitz dot alexander at googlemail dot com 05-Dec-2015 01:14
About the division by zero, please see discussion to IEEEE 754
For example: http://stackoverflow.com/questions/14682005/why-does-division-by-zero-in-ieee754-standard-results-in-infinite-value
me at fquff dot io 04-Dec-2015 03:45
[Editor's note: fixed limit on user request]

As a mathematician, 3/0 == +INF IS JUST WRONG. You can't just assume 3/0 == lim_{x->0+} 3/x, which is +INF indeed, because division IS NOT A CONTINUOUS FUNCTION in x == 0.

Also, 3/0 == +INF ("positive" infinity) while -3/0 == -INF ("negative" infinity) requires the assumption that 0 is a positive number, which is just as illogical as it looks like.

The fact that a warning is emitted is good, but it should definitely equals to NaN. ±INF is just illogical (and arithmetically wrong).

Except for this "detail", looks an amazing update, can't wait to test it even further!

Cheers,
P.


New features

Scalar type declarations

Scalar type declarations come in two flavours: coercive (default) and strict. The following types for parameters can now be enforced (either coercively or strictly): strings (string), integers (int), floating-point numbers (float), and booleans (bool). They augment the other types introduced in PHP 5: class names, interfaces, array and callable.

<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
    return array_sum($ints);
}

var_dump(sumOfInts(2, '3', 4.1));

위 예제의 출력:

int(9)

To enable strict mode, a single declare directive must be placed at the top of the file. This means that the strictness of typing for scalars is configured on a per-file basis. This directive not only affects the type declarations of parameters, but also a function's return type (see return type declarations, built-in PHP functions, and functions from loaded extensions.

Full documentation and examples of scalar type declarations can be found in the type declaration reference.

Return type declarations

PHP 7 adds support for return type declarations. Similarly to argument type declarations, return type declarations specify the type of the value that will be returned from a function. The same types are available for return type declarations as are available for argument type declarations.

<?php

function arraysSum(array ...$arrays): array
{
    return array_map(function(array $array): int {
        return array_sum($array);
    }, $arrays);
}

print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));

위 예제의 출력:

Array
(
    [0] => 6
    [1] => 15
    [2] => 24
)

Full documentation and examples of return type declarations can be found in the return type declarations. reference.

Null coalescing operator

The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>

Spaceship operator

The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b. Comparisons are performed according to PHP's usual type comparison rules.

<?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
?>

Constant arrays using define()

Array constants can now be defined with define(). In PHP 5.6, they could only be defined with const.

<?php
define('ANIMALS', [
    'dog',
    'cat',
    'bird'
]);

echo ANIMALS[1]; // outputs "cat"
?>

Anonymous classes

Support for anonymous classes has been added via new class. These can be used in place of full class definitions for throwaway objects:

<?php
interface Logger {
    public function log(string $msg);
}

class Application {
    private $logger;

    public function getLogger(): Logger {
         return $this->logger;
    }

    public function setLogger(Logger $logger) {
         $this->logger = $logger;
    }
}

$app = new Application;
$app->setLogger(new class implements Logger {
    public function log(string $msg) {
        echo $msg;
    }
});

var_dump($app->getLogger());
?>

위 예제의 출력:

object(class@anonymous)#2 (0) {
}

Full documentation can be found in the anonymous class reference.

Unicode codepoint escape syntax

This takes a Unicode codepoint in hexadecimal form, and outputs that codepoint in UTF-8 to a double-quoted string or a heredoc. Any valid codepoint is accepted, with leading 0's being optional.

echo "\u{aa}";
echo "\u{0000aa}";
echo "\u{9999}";

위 예제의 출력:

ª
ª (same as before but with optional leading 0's)
香

Closure::call()

Closure::call() is a more performant, shorthand way of temporarily binding an object scope to a closure and invoking it.

<?php
class A {private $x = 1;}

// Pre PHP 7 code
$getXCB = function() {return $this->x;};
$getX = $getXCB->bindTo(new A, 'A'); // intermediate closure
echo $getX();

// PHP 7+ code
$getX = function() {return $this->x;};
echo $getX->call(new A);

위 예제의 출력:

1
1

Filtered unserialize()

This feature seeks to provide better security when unserializing objects on untrusted data. It prevents possible code injections by enabling the developer to whitelist classes that can be unserialized.

<?php

// converts all objects into __PHP_Incomplete_Class object
$data = unserialize($foo, ["allowed_classes" => false]);

// converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2
$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]]);

// default behaviour (same as omitting the second argument) that accepts all classes
$data = unserialize($foo, ["allowed_classes" => true]);

IntlChar

The new IntlChar class seeks to expose additional ICU functionality. The class itself defines a number of static methods and constants that can be used to manipulate unicode characters.

<?php

printf('%x', IntlChar::CODEPOINT_MAX);
echo IntlChar::charName('@');
var_dump(IntlChar::ispunct('!'));

위 예제의 출력:

10ffff
COMMERCIAL AT
bool(true)

In order to use this class, the Intl extension must be installed.

Expectations

Expectations are a backwards compatible enhancement to the older assert() function. They allow for zero-cost assertions in production code, and provide the ability to throw custom exceptions when the assertion fails.

While the old API continues to be maintained for compatibility, assert() is now a language construct, allowing the first parameter to be an expression rather than just a string to be evaluated or a boolean value to be tested.

<?php
ini_set('assert.exception', 1);

class CustomError extends AssertionError {}

assert(false, new CustomError('Some error message'));
?>

위 예제의 출력:

Fatal error: Uncaught CustomError: Some error message

Full details on this feature, including how to configure it in both development and production environments, can be found in the expectations section of the assert() reference.

Group use declarations

Classes, functions and constants being imported from the same namespace can now be grouped together in a single use statement.

<?php
// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;

use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;

use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;

// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
?>

Generator Return Expressions

This feature builds upon the generator functionality introduced into PHP 5.5. It enables for a return statement to be used within a generator to enable for a final expression to be returned (return by reference is not allowed). This value can be fetched using the new Generator::getReturn() method, which may only be used once the generator has finished yielding values.

<?php

$gen = (function() {
    yield 1;
    yield 2;

    return 3;
})();

foreach ($gen as $val) {
    echo $val, PHP_EOL;
}

echo $gen->getReturn(), PHP_EOL;

위 예제의 출력:

1
2
3

Being able to explicitly return a final value from a generator is a handy ability to have. This is because it enables for a final value to be returned by a generator (from perhaps some form of coroutine computation) that can be specifically handled by the client code executing the generator. This is far simpler than forcing the client code to firstly check whether the final value has been yielded, and then if so, to handle that value specifically.

Generator delegation

Generators can now delegate to another generator, Traversable object or array automatically, without needing to write boilerplate in the outermost generator by using the yield from construct.

<?php
function gen()
{
    yield 1;
    yield 2;
    yield from gen2();
}

function gen2()
{
    yield 3;
    yield 4;
}

foreach (gen() as $val)
{
    echo $val, PHP_EOL;
}
?>

위 예제의 출력:

1
2
3
4

Integer division with intdiv()

The new intdiv() function performs an integer division of its operands and returns it.

<?php
var_dump(intdiv(10, 3));
?>

위 예제의 출력:

int(3)

Session options

session_start() now accepts an array of options that override the session configuration directives normally set in php.ini.

These options have also been expanded to support session.lazy_write, which is on by default and causes PHP to only overwrite any session file if the session data has changed, and read_and_close, which is an option that can only be passed to session_start() to indicate that the session data should be read and then the session should immediately be closed unchanged.

For example, to set session.cache_limiter to private and immediately close the session after reading it:

<?php
session_start([
    'cache_limiter' => 'private',
    'read_and_close' => true,
]);
?>

preg_replace_callback_array()

The new preg_replace_callback_array() function enables code to be written more cleanly when using the preg_replace_callback() function. Prior to PHP 7, callbacks that needed to be executed per regular expression required the callback function to be polluted with lots of branching.

Now, callbacks can be registered to each regular expression using an associative array, where the key is a regular expression and the value is a callback.

CSPRNG Functions

Two new functions have been added to generate cryptographically secure integers and strings in a cross platform way: random_bytes() and random_int().

list() can always unpack objects implementing ArrayAccess

Previously, list() was not guaranteed to operate correctly with objects implementing ArrayAccess. This has been fixed.

Other Features

  • Class member access on cloning has been added, e.g. (clone $foo)->bar().

User Contributed Notes

Maaz Rehman 11-Oct-2016 07:12
This is the most awaited major release of PHP in almost 11 years . It took 2 years in building and i must say it live up to its expectations . The hopes were really high. The major features that was invented in php 6 till then was added to php 5.4 . Thus php 6 never got released and was abandoned .
PHP7 new Features

1-Performance

The first biggest feature is not a feature , it is an improvement , The php 7 is really fast , roughly twice as faster than its previous version . There were numerous benchmarks that shows php7 is not just fast , it is very fast . The PHP 7 performance is equivalent to HIPHOP Virtual Machine(HVVM) made by Facebook which uses and leverages byte codes in interpretation. when you have to deploy two servers you can now use 1 , saves cost :)

2-Scalar Type Declaration

PHP 7 has 3 modes for handling data types.
              +None
              +coercive
              +Strict
+The none means leaving everything as is like in php 5.4,
 +The Coercive will juggles the type where ever it is declared but it will accept float in place of integer.
+The strict as is strict it would really get upset if you send float in place of integer. It displays a fatal error and ends the script.
              The datatypes are,
-int
-float
-string
-bool
-array
The return type declaration is also been introduced if you want to be explicit about what you want to return and you want to return something from function then you can use any of the 5 types mentioned above.

3-Space Ship Operator

It looks like "<==>", more like an alien ship seen in movies :)

returns -1 if left operand is less than the right one
return 0 if both operands are equal
returns 1 if left operand is greater than 1
This type of operator can be very handy when you are making a binary seach algorithm when you have to write three checks , well not any more we have spaceship for the rescue :)

4-Null Coalescing Operator

It returns value i.e not NULL.
$default=1;
$value=$a ?? $b ?? $default;
returns $default to $value.
It can be very useful when some thing is not up yet and we have to see second , third ,.. resource and if none is set, it is better to set default so that you can detect what happens and what you have to do from onwards.

5-Anonymous Classes

 AKA(inner class, nested class,quick class , lambda class ,...) more like a container i.e defined and instantiated at run time . if you require access to an enclosing instance's non-public fields and methods use this one.

6-Constant Arrays

Now we have the power to define and declare constants at if-else. Pretty cool hanh ?

7-Unicode Code Prints

Now you can insert unicode characters in echo. you can use emoji and emoticon in printing messages .
echo " \u{2764}"; //prints a black heart
I personally find it cool to have them B-)

8-Uniform Variable Syntax

if you dont have an idea of "variables variables" , this might be hard to understand.
$var['maaz']='baaz';
$baaz='var';
($$baaz)['maaz'];
/*
$baaz first resolves to $var['maaz'],prints "baaz" in php7 , gives a fatal error in php 5.4
*/

9-intdiv()

the function returns integer quotient
intdive(5,2); //returns 2
Deprecation

With great improvement come some deprecation of pointless features that have outlived its usefulness.

1-Some types of tags have been deprecated.
   <% ... %> asp styles tag have been deprecated. While <%= ... %> tags are not deprecated , it is still not a good idea to use them when clearly there is an alternative .
2-time zones warning is removed .
3-MYSQL API is to be deprecated by MYSQLi
4-Magic quotes have been deprecated.
5-It is extremely a very bad practice to call instance method statically , well not it has been deprecated .
Conclusion

 I hope that you've enjoyed learning about the new features and improvements in PHP 7.
I think it's a great step forward for PHP.
And one that will make your development experience much better.
Even if you don't need many of the new features,the speed improvement alone makes upgrading to PHP 7 worthwhile.
omarv_r at yahoo dot com 15-Aug-2016 08:38
lexx918, It is not working unexpectedly... lets see:

Calling to isset() method for a non existing property ($foo->bar in this case), automatically the magic method __isset() is invoked. So

$a = isset($foo->bar) ?: null; // __isset

is correct.

But when we try to evaluate $foo->bar, we are calling a getter. Because the bar property does not exist, the implicit calling is to magic method __get(). So

$a = $foo->bar ?? null; // __get

is correct too.
lexx918 at mail dot ru 02-Aug-2016 09:22
Sugar of ternary operator:
<?php
$a ?? 'b'
// allegedly equivalent to
isset($a) ? $a : 'b'
?>
.. in classes working unexpectedly:
<?php
class Foo {
    public function __get($p) { echo "__get" . PHP_EOL; }
    public function __isset($p) { echo "__isset" . PHP_EOL; }
}
$foo = new Foo;
$a = isset($foo->bar) ?: null; // __isset
$a = $foo->bar ?? null; // __get
?>
PawelD 20-Jul-2016 06:23
<?php
class foo { static $bar = 'baz'; }
var_dump('foo'::$bar);
?>

if < php7.0

then we will receive a syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)

but php7 returns string(3) "baz"

I think it's not documented anywhere


Deprecated features in PHP 7.0.x

PHP 4 style constructors

PHP 4 style constructors (methods that have the same name as the class they are defined in) are deprecated, and will be removed in the future. PHP 7 will emit E_DEPRECATED if a PHP 4 constructor is the only constructor defined within a class. Classes that implement a __construct() method are unaffected.

<?php
class foo {
    function foo() {
        echo 'I am the constructor';
    }
}
?>

위 예제의 출력:

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; foo has a deprecated constructor in example.php on line 3

Static calls to non-static methods

Static calls to methods that are not declared static are deprecated, and may be removed in the future.

<?php
class foo {
    function bar() {
        echo 'I am not static!';
    }
}

foo::bar();
?>

위 예제의 출력:

Deprecated: Non-static method foo::bar() should not be called statically in - on line 8
I am not static!

password_hash() salt option

The salt option for the password_hash() function has been deprecated to prevent developers from generating their own (usually insecure) salts. The function itself generates a cryptographically secure salt when no salt is provided by the developer - therefore custom salt generation should not be needed.

capture_session_meta SSL context option

The capture_session_meta SSL context option has been deprecated. SSL metadata is now available through the stream_get_meta_data() function.

LDAP deprecations

The following function has been deprecated:

  • ldap_sort()

User Contributed Notes

There are no user contributed notes for this page.


Changed functions

PHP Core

  • debug_zval_dump() now prints "int" instead of "long", and "float" instead of "double"
  • dirname() now optionally takes a second parameter, depth, to get the name of the directory depth levels up from the current directory.
  • getrusage() is now supported on Windows.
  • mktime() and gmmktime() functions no longer accept is_dst parameter.
  • preg_replace() function no longer supports "\e" (PREG_REPLACE_EVAL). preg_replace_callback() should be used instead.
  • setlocale() function no longer accepts category passed as string. LC_* constants must be used instead.
  • exec(), system() and passthru() functions have NULL byte protection now.
  • shmop_open() now returns a resource instead of an int, which has to be passed to shmop_size(), shmop_write(), shmop_read(), shmop_close() and shmop_delete().
  • substr() now returns an empty string, if string is equal to start characters long.
  • xml_set_object() now requires to manually unset the $parser when finished, to avoid memory leaks.

User Contributed Notes

There are no user contributed notes for this page.


New functions

Closure

  • Closure::call()

CSPRNG

  • random_bytes()
  • random_int()

Error Handling and Logging

  • error_clear_last()

Generator

  • Generator::getReturn()

GNU Multiple Precision

  • gmp_random_seed()

Math

  • intdiv()

PCRE

  • preg_replace_callback_array()

PHP Options/Info

  • gc_mem_caches()
  • get_resources()

POSIX

  • posix_setrlimit()

Reflection

  • ReflectionParameter::getType()
  • ReflectionParameter::hasType()
  • ReflectionFunctionAbstract::getReturnType()
  • ReflectionFunctionAbstract::hasReturnType()

Zip

  • ZipArchive::setCompressionIndex()
  • ZipArchive::setCompressionName()

Zlib Compression

  • inflate_add()
  • deflate_add()
  • inflate_init()
  • deflate_init()

User Contributed Notes

There are no user contributed notes for this page.


New Classes and Interfaces

Intl

  • IntlChar

Reflection

  • ReflectionGenerator
  • ReflectionType

Session Handling

  • SessionUpdateTimestampHandlerInterface

Exception Hierarchy

  • Throwable
  • Error
  • TypeError
  • ParseError
  • AssertionError
  • ArithmeticError
  • DivisionByZeroError

User Contributed Notes

There are no user contributed notes for this page.


New Global Constants

Core Predefined Constants

  • PHP_INT_MIN

GD

  • IMG_WEBP (as of PHP 7.0.10)

LibXML

  • LIBXML_BIGLINES

PCRE

  • PREG_JIT_STACKLIMIT_ERROR

POSIX

  • POSIX_RLIMIT_AS
  • POSIX_RLIMIT_CORE
  • POSIX_RLIMIT_CPU
  • POSIX_RLIMIT_DATA
  • POSIX_RLIMIT_FSIZE
  • POSIX_RLIMIT_LOCKS
  • POSIX_RLIMIT_MEMLOCK
  • POSIX_RLIMIT_MSGQUEUE
  • POSIX_RLIMIT_NICE
  • POSIX_RLIMIT_NOFILE
  • POSIX_RLIMIT_NPROC
  • POSIX_RLIMIT_RSS
  • POSIX_RLIMIT_RTPRIO
  • POSIX_RLIMIT_RTTIME
  • POSIX_RLIMIT_SIGPENDING
  • POSIX_RLIMIT_STACK
  • POSIX_RLIMIT_INFINITY

Zlib

  • ZLIB_ENCODING_RAW
  • ZLIB_ENCODING_DEFLATE
  • ZLIB_ENCODING_GZIP
  • ZLIB_FILTERED
  • ZLIB_HUFFMAN_ONLY
  • ZLIB_FIXED
  • ZLIB_RLE
  • ZLIB_DEFAULT_STRATEGY
  • ZLIB_BLOCK
  • ZLIB_FINISH
  • ZLIB_FULL_FLUSH
  • ZLIB_NO_FLUSH
  • ZLIB_PARTIAL_FLUSH
  • ZLIB_SYNC_FLUSH

User Contributed Notes

There are no user contributed notes for this page.


Changes in SAPI Modules

FPM

Unqualified listen ports now listen on both IPv4 and IPv6

In PHP 5, a listen directive with only a port number would listen on all interfaces, but only on IPv4. PHP 7 will now accept requests made via both IPv4 and IPv6.

This does not affect directives which include specific IP addresses; these will continue to only listen on that address and protocol.

User Contributed Notes

There are no user contributed notes for this page.


Removed Extensions and SAPIs

Removed Extensions

  • ereg
  • mssql
  • mysql
  • sybase_ct

Removed SAPIs

  • aolserver
  • apache
  • apache_hooks
  • apache2filter
  • caudium
  • continuity
  • isapi
  • milter
  • nsapi
  • phttpd
  • pi3web
  • roxen
  • thttpd
  • tux
  • webjames

User Contributed Notes

There are no user contributed notes for this page.


기타 변경사항

예약어 제약의 완화

클래스와 인터페이스 그리고 트레이트 내부에서 속성과 상수 그리고 메소드의 이름에 글로벌 예약어를 사용할 수 있게 되었습니다. 이제 앞으로 새로운 키워드가 도입 된 경우에도 하위 호환성을 유지하기 쉬워집니다. 그리고 API 명명 제약도 줄일 수 있을 것입니다.

이것은 Fluent interfaces를 이용한 내부 DSL을 만들 때 유용합니다.

<?php
// 'new', 'private', 'for'는 통상적 인 경우 사용할 수 없습니다
Project::new('Project Name')->private()->for('purpose here')->with('username here');
?>

유일한 예외는 class 키워드는 여전히 상수 이름으로 사용할 수 없습니다. 이것을 허용할 경우 클래스 이름 확인 구문(ClassName::class)과 충돌해 버리기 때문입니다.

date.timezone 경고 삭제

이전 버전에서는 INI 항목 date.timezone을 설정하지 않고 날짜 및 시간 관련 함수를 사용하려고 하면 경고가 발생했습니다. PHP 7에서는 이 경고가 발생하지 않습니다 (date.timezone의 기본은 여전히 UTC입니다).

User Contributed Notes

There are no user contributed notes for this page.


Migrating from PHP 5.5.x to PHP 5.6.x

Table of Contents

  • Backward incompatible changes
  • New features
  • Deprecated features in PHP 5.6.x
  • Changed functions
  • New functions
  • OpenSSL changes in PHP 5.6.x
  • Other changes to extensions
  • New global constants

Most improvements in PHP 5.6.x have no impact on existing code. There are a few incompatibilities and new features that should be considered, and code should be tested before switching PHP versions in production environments.

PHP 버전에 맞는 이행 가이드를 참고하십시오. 5.0.x, 5.1.x, 5.2.x, 5.3.x, 5.4.x and 5.5.x.

User Contributed Notes

There are no user contributed notes for this page.


Backward incompatible changes

Although most existing PHP 5 code should work without changes, please take note of some backward incompatible changes:

Array keys won't be overwritten when defining an array as a property of a class via an array literal

Previously, arrays declared as class properties which mixed explicit and implicit keys could have array elements silently overwritten if an explicit key was the same as a sequential implicit key. For example:

<?php
class C {
    const ONE = 1;
    public $array = [
        self::ONE => 'foo',
        'bar',
        'quux',
    ];
}

var_dump((new C)->array);
?>

PHP 5.4에서 위 예제의 출력:

array(2) {
  [0]=>
  string(3) "bar"
  [1]=>
  string(4) "quux"
}

PHP 5.4에서 위 예제의 출력:

array(3) {
  [1]=>
  string(3) "foo"
  [2]=>
  string(3) "bar"
  [3]=>
  string(4) "quux"
}

json_decode() strictness

json_decode() now rejects non-lowercase variants of the JSON literals true, false and null at all times, as per the JSON specification, and sets json_last_error() accordingly. Previously, inputs to json_decode() that consisted solely of one of these values in upper or mixed case were accepted.

This change will only affect cases where invalid JSON was being passed to json_decode(): valid JSON input is unaffected and will continue to be parsed normally.

Stream wrappers now verify peer certificates and host names by default when using SSL/TLS

All encrypted client streams now enable peer verification by default. By default, this will use OpenSSL's default CA bundle to verify the peer certificate. In most cases, no changes will need to be made to communicate with servers with valid SSL certificates, as distributors generally configure OpenSSL to use known good CA bundles.

The default CA bundle may be overridden on a global basis by setting either the openssl.cafile or openssl.capath configuration setting, or on a per request basis by using the cafile or capath context options.

While not recommended in general, it is possible to disable peer certificate verification for a request by setting the verify_peer context option to FALSE, and to disable peer name validation by setting the verify_peer_name context option to FALSE.

GMP resources are now objects

GMP resources are now objects. The functional API implemented in the GMP extension has not changed, and code should run unmodified unless it checks explicitly for a resource using is_resource() or similar.

Mcrypt functions now require valid keys and IVs

mcrypt_encrypt(), mcrypt_decrypt(), mcrypt_cbc(), mcrypt_cfb(), mcrypt_ecb(), mcrypt_generic() and mcrypt_ofb() will no longer accept keys or IVs with incorrect sizes, and block cipher modes that require IVs will now fail if an IV isn't provided.

cURL file uploads

Uploads using the @file syntax now require CURLOPT_SAFE_UPLOAD to be set to FALSE. CURLFile should be used instead.

User Contributed Notes

Someone 26-Sep-2016 10:04
Beware, now default_charset in PHP 5.6 is UTF-8, instead of blank value like in previous versions. So if you use 8-bit charset like iso-8859-1 then your non-ascii characters will be broken until you explicitly set default_charset to blank value or to your actual character set. HTML meta tag doesn't work anymore because HTTP headers have more proirity over them. And Apache AddDefaultCharset directive has lower proirity too.


New features

Constant expressions

It is now possible to provide a scalar expression involving numeric and string literals and/or constants in contexts where PHP previously expected a static value, such as constant and property declarations and default function arguments.

<?php
const ONE = 1;
const TWO = ONE * 2;

class C {
    const THREE = TWO + 1;
    const ONE_THIRD = ONE / self::THREE;
    const SENTENCE = 'The value of THREE is '.self::THREE;

    public function f($a = ONE + self::THREE) {
        return $a;
    }
}

echo (new C)->f()."\n";
echo C::SENTENCE;
?>

위 예제의 출력:

4
The value of THREE is 3

It is also now possible to define a constant array using the const keyword:

<?php
const ARR = ['a', 'b'];

echo ARR[0];
?>

위 예제의 출력:

a

Variadic functions via ...

Variadic functions can now be implemented using the ... operator, instead of relying on func_get_args().

<?php
function f($req, $opt = null, ...$params) {
    // $params is an array containing the remaining arguments.
    printf('$req: %d; $opt: %d; number of params: %d'."\n",
           $req, $opt, count($params));
}

f(1);
f(1, 2);
f(1, 2, 3);
f(1, 2, 3, 4);
f(1, 2, 3, 4, 5);
?>

위 예제의 출력:

$req: 1; $opt: 0; number of params: 0
$req: 1; $opt: 2; number of params: 0
$req: 1; $opt: 2; number of params: 1
$req: 1; $opt: 2; number of params: 2
$req: 1; $opt: 2; number of params: 3

Argument unpacking via ...

Arrays and Traversable objects can be unpacked into argument lists when calling functions by using the ... operator. This is also known as the splat operator in other languages, including Ruby.

<?php
function add($a, $b, $c) {
    return $a + $b + $c;
}

$operators = [2, 3];
echo add(1, ...$operators);
?>

위 예제의 출력:

6

Exponentiation via **

A right associative ** operator has been added to support exponentiation, along with a **= shorthand assignment operator.

<?php
printf("2 ** 3 ==      %d\n", 2 ** 3);
printf("2 ** 3 ** 2 == %d\n", 2 ** 3 ** 2);

$a = 2;
$a **= 3;
printf("a ==           %d\n", $a);
?>

위 예제의 출력:

2 ** 3 ==      8
2 ** 3 ** 2 == 512
a ==           8

use function and use const

The use operator has been extended to support importing functions and constants in addition to classes. This is achieved via the use function and use const constructs, respectively.

<?php
namespace Name\Space {
    const FOO = 42;
    function f() { echo __FUNCTION__."\n"; }
}

namespace {
    use const Name\Space\FOO;
    use function Name\Space\f;

    echo FOO."\n";
    f();
}
?>

위 예제의 출력:

42
Name\Space\f

phpdbg

PHP now includes an interactive debugger called phpdbg implemented as a SAPI module. For more information, please visit the » phpdbg documentation.

Default character encoding

default_charset is now used as the default character set for the htmlentities(), html_entity_decode() and htmlspecialchars() functions. Note that if the (now deprecated) iconv and mbstring encoding settings are set, they will take precedence over default_charset for iconv and mbstring functions, respectively.

The default value for this setting is UTF-8.

php://input is reusable

php://input may now be reopened and read as many times as required. This work has also resulted in a major reduction in the amount of memory required to deal with POST data.

Large file uploads

Files larger than 2 gigabytes in size are now accepted.

GMP supports operator overloading

GMP objects now support operator overloading and casting to scalar types. This allows for more expressive code using GMP:

<?php
$a = gmp_init(42);
$b = gmp_init(17);

if (version_compare(PHP_VERSION, '5.6', '<')) {
    echo gmp_intval(gmp_add($a, $b)), PHP_EOL;
    echo gmp_intval(gmp_add($a, 17)), PHP_EOL;
    echo gmp_intval(gmp_add(42, $b)), PHP_EOL;
} else {
    echo $a + $b, PHP_EOL;
    echo $a + 17, PHP_EOL;
    echo 42 + $b, PHP_EOL;
}
?>

위 예제의 출력:

59
59
59

hash_equals() for timing attack safe string comparison

The hash_equals() function has been added to compare two strings in constant time. This should be used to mitigate timing attacks; for instance, when testing crypt() password hashes (assuming that you are unable to use password_hash() and password_verify(), which aren't susceptible to timing attacks).

<?php
$expected  = crypt('12345', '$2a$07$usesomesillystringforsalt$');
$correct   = crypt('12345', '$2a$07$usesomesillystringforsalt$');
$incorrect = crypt('1234',  '$2a$07$usesomesillystringforsalt$');

var_dump(hash_equals($expected, $correct));
var_dump(hash_equals($expected, $incorrect));
?>

위 예제의 출력:

bool(true)
bool(false)

__debugInfo()

The __debugInfo() magic method has been added to allow objects to change the properties and values that are shown when the object is output using var_dump().

<?php
class C {
    private $prop;

    public function __construct($val) {
        $this->prop = $val;
    }

    public function __debugInfo() {
        return [
            'propSquared' => $this->prop ** 2,
        ];
    }
}

var_dump(new C(42));
?>

위 예제의 출력:

object(C)#1 (1) {
  ["propSquared"]=>
  int(1764)
}

gost-crypto hash algorithm

The gost-crypto hash algorithm has been added. This implements the GOST hash function using the CryptoPro S-box tables as specified by » RFC 4357, section 11.2.

SSL/TLS improvements

A wide range of improvements have been made to the SSL/TLS support in PHP 5.6. These include enabling peer verification by default, supporting certificate fingerprint matching, mitigating against TLS renegotiation attacks, and many new SSL context options to allow more fine grained control over protocol and verification settings when using encrypted streams.

These changes are described in more detail in the OpenSSL changes in PHP 5.6.x section of this migration guide.

pgsql async support

The pgsql extension now supports asynchronous connections and queries, thereby enabling non-blocking behaviour when interacting with PostgreSQL databases. Asynchronous connections may be established via the PGSQL_CONNECT_ASYNC constant, and the new pg_connect_poll(), pg_socket(), pg_consume_input() and pg_flush() functions may be used to handle asynchronous connections and queries.

User Contributed Notes

ciachn 06-Sep-2014 04:20
Having 2 ** 3 ** 2 = 512 is actually that is the exact correct behavior for a right-associative operator just as specified in the "**" documentation.
Anonymous 15-Jul-2014 09:58
Remember, that

    ($a ** $b) ** $c === $a ** ($b * $c)

Thats why exponent operator** is RIGHT associative.
ashnazg at php dot net 22-Apr-2014 12:07
Note the order of operations in that exponentiation operator, as it was opposite of what my first expectation was:

<?php
// what I had expected,
// evaluating left to right,
// since no parens were used to guide the order of operations
2 ** 3 ** 2 == 64; // (2 ** 3) ** 2 = 8 ** 2 = 64

// the given example,
// which appears to evaluate right to left
2 ** 3 ** 2 == 512; // 2 ** (3 ** 2) = 2 ** 9 = 512
?>
gmblar+php at gmail dot com 31-Jan-2014 11:36
<?php

function array_zip(...$arrays) {
    return array_merge(...array_map(NULL, ...$arrays));
}

$a = array(1, 4, 7);
$b = array(2, 5, 8);
$c = array(3, 6, 9);

var_dump(implode(', ', array_zip($a, $b, $c)));

// Output
string(25) "1, 2, 3, 4, 5, 6, 7, 8, 9"
tr0y 25-Jan-2014 04:02
It is also possible ( in 5.6.0alpha ) to typehint the ...-operator

function foo (stdclass ... $inbound) {
   var_dump($inbound);
}

// ok:
foo( (object)['foo' => 'bar'], (object)['bar' => 'foo'] );

// fails:
foo( 1, 2, 3, 4 );


Deprecated features in PHP 5.6.x

Calls from incompatible context

Methods called from an incompatible context are now deprecated, and will generate E_DEPRECATED errors when invoked instead of E_STRICT. Support for these calls will be removed in a future version of PHP.

An example of such a call is:

<?php
class A {
    function f() { echo get_class($this); }
}

class B {
    function f() { A::f(); }
}

(new B)->f();
?>

위 예제의 출력:

Deprecated: Non-static method A::f() should not be called statically, assuming $this from incompatible context in - on line 7
B

$HTTP_RAW_POST_DATA and always_populate_raw_post_data

always_populate_raw_post_data will now generate an E_DEPRECATED error when $HTTP_RAW_POST_DATA is populated. New code should use php://input instead of $HTTP_RAW_POST_DATA, which will be removed in a future release. You can opt in for the new behaviour (in which $HTTP_RAW_POST_DATA is never defined hence no E_DEPRECATED error will be generated) by setting always_populate_raw_post_data to -1.

iconv and mbstring encoding settings

The iconv and mbstring configuration options related to encoding have been deprecated in favour of default_charset. The deprecated options are:

  • iconv.input_encoding
  • iconv.output_encoding
  • iconv.internal_encoding
  • mbstring.http_input
  • mbstring.http_output
  • mbstring.internal_encoding

User Contributed Notes

ohcc at 163 dot com 06-Nov-2015 03:22
<?php
    ini_set('always_populate_raw_post_data',-1);
    $HTTP_RAW_POST_DATA = file_get_contents('php://input');
    echo $HTTP_RAW_POST_DATA;
?>


Changed functions

PHP Core

  • crypt() will now raise an E_NOTICE error if the salt parameter is omitted.
  • substr_compare() will now accept 0 for its length parameter.
  • unserialize() will now fail if passed serialised data that has been manipulated to attempt to instantiate an object without calling its constructor.

cURL

  • Uploads using the @file syntax are now only supported if the CURLOPT_SAFE_UPLOAD option is set to FALSE. CURLFile should be used instead.

Mcrypt

  • The source parameter of mcrypt_create_iv() now defaults to MCRYPT_DEV_URANDOM instead of MCRYPT_DEV_RANDOM.

OpenSSL

  • stream_socket_enable_crypto() now allows the crypto_type parameter to be optional if the stream's SSL context includes the new crypto_type option.

PostgreSQL

  • pg_insert(), pg_select(), pg_update() and pg_delete() are no longer experimental.
  • pg_send_execute(), pg_send_prepare(), pg_send_query() and pg_send_query_params() will no longer block until query write completion if the underlying socket stream for the database connection is set to non-blocking mode.

Reflection

  • ReflectionClass::newInstanceWithoutConstructor() now allows non-final internal classes to be instantiated.

XMLReader

  • XMLReader::getAttributeNs() and XMLReader::getAttributeNo() now return NULL if the attribute could not be found, like XMLReader::getAttribute().

User Contributed Notes

There are no user contributed notes for this page.


New functions

Date and Time

  • DateTimeImmutable::createFromMutable()

GMP

  • gmp_root()
  • gmp_rootrem()

Hash

  • hash_equals()

LDAP

  • ldap_escape()
  • ldap_modify_batch()

MySQLi

  • mysqli_get_links_stats()

OCI8

  • oci_get_implicit_resultset()

OpenSSL

  • openssl_get_cert_locations()
  • openssl_x509_fingerprint()
  • openssl_spki_new()
  • openssl_spki_verify()
  • openssl_spki_export_challenge()
  • openssl_spki_export()

PostgreSQL

  • pg_connect_poll()
  • pg_consume_input()
  • pg_flush()
  • pg_socket()

PDO_PGSQL

  • PDO::pgsqlGetNotify()
  • PDO::pgsqlGetPid()

Session

  • session_abort()
  • session_reset()

Zip

  • ZipArchive::setPassword()

User Contributed Notes

There are no user contributed notes for this page.


OpenSSL changes in PHP 5.6.x

Stream wrappers now verify peer certificates and host names by default when using SSL/TLS

All encrypted client streams now enable peer verification by default. By default, this will use OpenSSL's default CA bundle to verify the peer certificate. In most cases, no changes will need to be made to communicate with servers with valid SSL certificates, as distributors generally configure OpenSSL to use known good CA bundles.

The default CA bundle may be overridden on a global basis by setting either the openssl.cafile or openssl.capath configuration setting, or on a per request basis by using the cafile or capath context options.

While not recommended in general, it is possible to disable peer certificate verification for a request by setting the verify_peer context option to FALSE, and to disable peer name validation by setting the verify_peer_name context option to FALSE.

Certificate fingerprints

Support has been added for extracting and verifying certificate fingerprints. openssl_x509_fingerprint() has been added to extract a fingerprint from an X.509 certificate, and two SSL stream context options have been added: capture_peer_cert to capture the peer's X.509 certificate, and peer_fingerprint to assert that the peer's certificate should match the given fingerprint.

Default ciphers updated

The default ciphers used by PHP have been updated to a more secure list based on the » Mozilla cipher recommendations, with two additional exclusions: anonymous Diffie-Hellman ciphers, and RC4.

This list can be accessed via the new OPENSSL_DEFAULT_STREAM_CIPHERS constant, and can be overridden (as in previous PHP versions) by setting the ciphers context option.

Compression disabled by default

SSL/TLS compression has been disabled by default to mitigate the CRIME attack. PHP 5.4.13 added a disable_compression context option to allow compression to be disabled: this is now set to TRUE (that is, compression is disabled) by default.

Allow servers to prefer their cipher order

The honor_cipher_order SSL context option has been added to allow encrypted stream servers to mitigate BEAST vulnerabilities by preferring the server's ciphers to the client's.

Access the negotiated protocol and cipher

The protocol and cipher that were negotiated for an encrypted stream can now be accessed via stream_get_meta_data() or stream_context_get_options() when the capture_session_meta SSL context option is set to TRUE.

<?php
$ctx = stream_context_create(['ssl' => [
    'capture_session_meta' => TRUE
]]);

$html = file_get_contents('https://google.com/', FALSE, $ctx);
$meta = stream_context_get_options($ctx)['ssl']['session_meta'];
var_dump($meta);
?>

위 예제의 출력:

array(4) {
  ["protocol"]=>
  string(5) "TLSv1"
  ["cipher_name"]=>
  string(20) "ECDHE-RSA-AES128-SHA"
  ["cipher_bits"]=>
  int(128)
  ["cipher_version"]=>
  string(11) "TLSv1/SSLv3"
}

New options for perfect forward secrecy in encrypted stream servers

Encrypted client streams already support perfect forward secrecy, as it is generally controlled by the server. PHP encrypted server streams using certificates capable of perfect forward secrecy do not need to take any additional action to enable PFS; however a number of new SSL context options have been added to allow more control over PFS and deal with any compatibility issues that may arise.

ecdh_curve

This option allows the selection of a specific curve for use with ECDH ciphers. If not specified, prime256v1 will be used.

dh_param

A path to a file containing parametrs for Diffie-Hellman key exchange, such as that created by the following command:

openssl dhparam -out /path/to/my/certs/dh-2048.pem 2048
single_dh_use

If set to TRUE, a new key pair will be created when using Diffie-Hellman parameters, thereby improving forward secrecy.

single_ecdh_use

If set to TRUE, a new key pair will always be generated when ECDH cipher suites are negotiated. This improves forward secrecy.

SSL/TLS version selection

It is now possible to select specific versions of SSL and TLS via the crypto_method SSL context option or by specifying a specific transport when creating a stream wrapper (for example, by calling stream_socket_client() or stream_socket_server()).

The crypto_method SSL context option accepts a bitmask enumerating the protocols that are permitted, as does the crypto_type of stream_socket_enable_crypto().

Selected protocol versions and corresponding options
Protocol(s) | Client flag | Server flag | Transport
Any TLS or SSL version | STREAM_CRYPTO_METHOD_ANY_CLIENT | STREAM_CRYPTO_METHOD_ANY_SERVER | ssl://
Any TLS version | STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLS_SERVER | tls://
TLS 1.0 | STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_0_SERVER | tlsv1.0://
TLS 1.1 | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_SERVER | tlsv1.1://
TLS 1.2 | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_SERVER | tlsv1.2://
SSL 3 | STREAM_CRYPTO_METHOD_SSLv3_CLIENT | STREAM_CRYPTO_METHOD_SSLv3_SERVER | sslv3://
<?php

// Requiring TLS 1.0 or better when using file_get_contents():
$ctx = stream_context_create([
    'ssl' => [
        'crypto_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
    ],
]);
$html = file_get_contents('https://google.com/', false, $ctx);

// Requiring TLS 1.1 or 1.2:
$ctx = stream_context_create([
    'ssl' => [
        'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT |
                           STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
    ],
]);
$html = file_get_contents('https://google.com/', false, $ctx);

// Connecting using the tlsv1.2:// stream socket transport.
$sock = stream_socket_client('tlsv1.2://google.com:443/');

?>

openssl_get_cert_locations() added

The openssl_get_cert_locations() function has been added: it returns the default locations PHP will search when looking for CA bundles.

<?php
var_dump(openssl_get_cert_locations());
?>

위 예제의 출력:

array(8) {
  ["default_cert_file"]=>
  string(21) "/etc/pki/tls/cert.pem"
  ["default_cert_file_env"]=>
  string(13) "SSL_CERT_FILE"
  ["default_cert_dir"]=>
  string(18) "/etc/pki/tls/certs"
  ["default_cert_dir_env"]=>
  string(12) "SSL_CERT_DIR"
  ["default_private_dir"]=>
  string(20) "/etc/pki/tls/private"
  ["default_default_cert_area"]=>
  string(12) "/etc/pki/tls"
  ["ini_cafile"]=>
  string(0) ""
  ["ini_capath"]=>
  string(0) ""
}

SPKI support

Support has been added for generating, extracting and verifying signed public key and challenges (SPKAC). openssl_spki_new(), openssl_spki_verify(), openssl_spki_export_challenge(), and openssl_spki_export() have been added to create, verify export PEM public key and associated challenge from SPKAC's generated from a KeyGen HTML5 element.

openssl_spki_new

Generates a new SPKAC using private key, challenge string and hashing algorithm.

<?php
$pkey = openssl_pkey_new();
openssl_pkey_export($pkey, 'secret passphrase');

$spkac = openssl_spki_new($pkey, 'challenge string');
?>

위 예제의 출력:

SPKAC=MIIBXjCByDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA3L0IfUijj7+A8CPC8EmhcdNoe5fUAog7OrBdhn7EkxFButUp40P7+LiYiygYG1TmoI/a5EgsLU3s9twEz3hmgY9mYIqb/rb+SF8qlD/K6KVyUORC7Wlz1Df4L8O3DuRGzx6/+3jIW6cPBpfgH1sVuYS1vDBsP/gMMIxwTsKJ4P0CAwEAARYkYjViMzYxMTktNjY5YS00ZDljLWEyYzctMGZjNGFhMjVlMmE2MA0GCSqGSIb3DQEBAwUAA4GBAF7hu0ifzmjonhAak2FhhBRsKFDzXdKIkrWxVNe8e0bZzMrWOxFM/rqBgeH3/gtOUDRS5Fnzyq425UsTYbjfiKzxGeCYCQJb1KJ2V5Ij/mIJHZr53WYEXHQTNMGR8RPm7IxwVXVSHIgAfXsXZ9IXNbFbcaLRiSTr9/N4U+MXUWL7
openssl_spki_verify

Verifies provided SPKAC.

<?php
$pkey = openssl_pkey_new();
openssl_pkey_export($pkey, 'secret passphrase');

$spkac = openssl_spki_new($pkey, 'challenge string');
var_dump(openssl_spki_verify($spkac));
?>
openssl_spki_export_challenge

Exports associated challenge from provided SPKAC.

<?php
$pkey = openssl_pkey_new();
openssl_pkey_export($pkey, 'secret passphrase');

$spkac = openssl_spki_new($pkey, 'challenge string');
$challenge = openssl_spki_export_challenge($spkac):
echo $challenge;
?>

위 예제의 출력:

challenge string
openssl_spki_export

Exports the PEM formatted RSA public key from SPKAC.

<?php
$pkey = openssl_pkey_new();
openssl_pkey_export($pkey, 'secret passphrase');

$spkac = openssl_spki_new($pkey, 'challenge string');
echo openssl_spki_export($spkac);
?>

위 예제의 출력:

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcvQh9SKOPv4DwI8LwSaFx02h7
l9QCiDs6sF2GfsSTEUG61SnjQ/v4uJiLKBgbVOagj9rkSCwtTez23ATPeGaBj2Zg
ipv+tv5IXyqUP8ropXJQ5ELtbXPUN/gvw7cO5EbPHr/7eMhbpw8Gl+AfWxW5hLW8
MGw/+AwwjHBOwong/QIDAQAB
-----END PUBLIC KEY-----

User Contributed Notes

Victor 27-Oct-2015 06:36
To get back to the "old behavior" with self-signed ("snakeoil") certificates to possibly incorrect name, you need to set both options.

<?php
            $streamContext = stream_context_create([
                'ssl' => [
                    'verify_peer'      => false,
                    'verify_peer_name' => false
                ]
            ]);
            $contents = file_get_contents('https://url', false, $streamContext);
?>


Other changes to extensions

cURL

A number of constants marked obsolete in the cURL library have now been removed:

  • CURLOPT_CLOSEPOLICY
  • CURLCLOSEPOLICY_CALLBACK
  • CURLCLOSEPOLICY_LEAST_RECENTLY_USED
  • CURLCLOSEPOLICY_LEAST_TRAFFIC
  • CURLCLOSEPOLICY_OLDEST
  • CURLCLOSEPOLICY_SLOWEST

OCI8

  • Support for implicit result sets for Oracle Database 12c has been added via the new oci_get_implicit_resultset() function.
  • Using oci_execute($s, OCI_NO_AUTO_COMMIT) for a SELECT no longer unnecessarily initiates an internal ROLLBACK during connection close.
  • Added DTrace probes controlled by the --enable-dtrace configure option.
  • oci_internal_debug() is now a no-op.
  • The phpinfo() output format for OCI8 has changed.

Zip

A --with-libzip configure option has been added to use a system libzip installation. libzip version 0.11 is required, with 0.11.2 or later recommended.

MySQLi

A new mysqli.rollback_on_cached_plink option was added to rollback connections put back into the persistent connection pool.

User Contributed Notes

There are no user contributed notes for this page.


New global constants

GD

  • IMG_WEBP (as of PHP 5.6.25)

LDAP

  • LDAP_ESCAPE_DN
  • LDAP_ESCAPE_FILTER

OpenSSL

  • OPENSSL_DEFAULT_STREAM_CIPHERS
  • STREAM_CRYPTO_METHOD_ANY_CLIENT
  • STREAM_CRYPTO_METHOD_ANY_SERVER
  • STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT
  • STREAM_CRYPTO_METHOD_TLSv1_0_SERVER
  • STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT
  • STREAM_CRYPTO_METHOD_TLSv1_1_SERVER
  • STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
  • STREAM_CRYPTO_METHOD_TLSv1_2_SERVER

PostgreSQL

  • PGSQL_CONNECT_ASYNC
  • PGSQL_CONNECTION_AUTH_OK
  • PGSQL_CONNECTION_AWAITING_RESPONSE
  • PGSQL_CONNECTION_MADE
  • PGSQL_CONNECTION_SETENV
  • PGSQL_CONNECTION_SSL_STARTUP
  • PGSQL_CONNECTION_STARTED
  • PGSQL_DML_ESCAPE
  • PGSQL_POLLING_ACTIVE
  • PGSQL_POLLING_FAILED
  • PGSQL_POLLING_OK
  • PGSQL_POLLING_READING
  • PGSQL_POLLING_WRITING

User Contributed Notes

There are no user contributed notes for this page.


Migrating from PHP 5.4.x to PHP 5.5.x

Table of Contents

  • What has changed in PHP 5.5.x
  • Backward Incompatible Changes
  • New features
  • Deprecated features in PHP 5.5.x
  • Changed Functions
  • New Functions
  • New Classes and Interfaces
  • New Methods
  • Other changes to extensions
  • New Global Constants
  • Changes to INI file handling
  • Changes to PHP Internals

PHP 버전에 맞는 이행 가이드를 참고하십시오. 5.0.x, 5.1.x, 5.2.x, 5.3.x, 5.4.x, 5.6.x, and 7.0.x.

User Contributed Notes

There are no user contributed notes for this page.


What has changed in PHP 5.5.x

Most improvements in PHP 5.5.x have no impact on existing code. There are a few incompatibilities and new features that should be considered, and code should be tested before switching PHP versions in production environments.

For systems being upgraded from an older version of PHP, the relevant documentation is available at:

  • Upgrade Notes for PHP 5.4.x.
  • Upgrade Notes for PHP 5.3.x.
  • Upgrade Notes for PHP 5.2.x.
  • Upgrade Notes for PHP 5.1.x.
  • Migrating from PHP 4 to PHP 5.

User Contributed Notes

There are no user contributed notes for this page.


Backward Incompatible Changes

Although most existing PHP 5 code should work without changes, please take note of some backward incompatible changes:

Windows XP and 2003 support dropped

Support for Windows XP and 2003 has been dropped. Windows builds of PHP now require Windows Vista or newer.

Case insensitivity no longer locale specific

All case insensitive matching for function, class and constant names is now performed in a locale independent manner according to ASCII rules. This improves support for languages using the Latin alphabet with unusual collating rules, such as Turkish and Azeri.

This may cause issues for code that uses case insensitive matches for non-ASCII characters in multibyte character sets (including UTF-8), such as accented characters in many European languages. If you have a non-English, non-ASCII code base, then you will need to test that you are not inadvertently relying on this behaviour before deploying PHP 5.5 to production systems.

pack() and unpack() changes

Changes were made to pack() and unpack() to make them more compatible with Perl:

  • pack() now supports the "Z" format code, which behaves identically to "a".
  • unpack() now support the "Z" format code for NULL padded strings, and behaves as "a" did in previous versions: it will strip trailing NULL bytes.
  • unpack() now keeps trailing NULL bytes when the "a" format code is used.
  • unpack() now strips all trailing ASCII whitespace when the "A" format code is used.

Writing backward compatible code that uses the "a" format code with unpack() requires the use of version_compare(), due to the backward compatibility break.

For example:

<?php
// Old code:
$data = unpack('a5', $packed);

// New code:
if (version_compare(PHP_VERSION, '5.5.0-dev', '>=')) {
  $data = unpack('Z5', $packed);
} else {
  $data = unpack('a5', $packed);
}
?>

self, parent and static are now always case insensitive

Prior to PHP 5.5, cases existed where the self, parent, and static keywords were treated in a case sensitive fashion. These have now been resolved, and these keywords are always handled case insensitively: SELF::CONSTANT is now treated identically to self::CONSTANT.

PHP logo GUIDs removed

The GUIDs that previously resulted in PHP outputting various logos have been removed. This includes the removal of the functions to return those GUIDs. The removed functions are:

  • php_logo_guid()
  • php_egg_logo_guid()
  • php_real_logo_guid()
  • zend_logo_guid()

Internal execution changes

Extension authors should note that the zend_execute() function can no longer be overridden, and that numerous changes have been made to the execute_data struct and related function and method handling opcodes.

Most extension authors are unlikely to be affected, but those writing extensions that hook deeply into the Zend Engine should read the notes on these changes.

User Contributed Notes

There are no user contributed notes for this page.


New features

Generators added

Support for generators has been added via the yield keyword. Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface.

A simple example that reimplements the range() function as a generator (at least for positive step values):

<?php
function xrange($start, $limit, $step = 1) {
    for ($i = $start; $i <= $limit; $i += $step) {
        yield $i;
    }
}

echo 'Single digit odd numbers: ';

/*
 * Note that an array is never created or returned,
 * which saves memory.
 */
foreach (xrange(1, 9, 2) as $number) {
    echo "$number ";
}

echo "\n";
?>

위 예제의 출력:

Single digit odd numbers: 1 3 5 7 9

finally keyword added

try-catch blocks now support a finally block for code that should be run regardless of whether an exception has been thrown or not.

New password hashing API

A new password hashing API that makes it easier to securely hash and manage passwords using the same underlying library as crypt() in PHP has been added. See the documentation for password_hash() for more detail.

foreach now supports list()

The foreach control structure now supports unpacking nested arrays into separate variables via the list() construct. For example:

<?php
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a, $b)) {
    echo "A: $a; B: $b\n";
}
?>

위 예제의 출력:

A: 1; B: 2
A: 3; B: 4

Further documentation is available on the foreach manual page.

empty() supports arbitrary expressions

Passing an arbitrary expression instead of a variable to empty() is now supported. For example:

<?php
function always_false() {
    return false;
}

if (empty(always_false())) {
    echo "This will be printed.\n";
}

if (empty(true)) {
    echo "This will not be printed.\n";
}
?>

위 예제의 출력:

This will be printed.

array and string literal dereferencing

Array and string literals can now be dereferenced directly to access individual elements and characters:

<?php
echo 'Array dereferencing: ';
echo [1, 2, 3][0];
echo "\n";

echo 'String dereferencing: ';
echo 'PHP'[0];
echo "\n";
?>

위 예제의 출력:

Array dereferencing: 1
String dereferencing: P

Class name resolution via ::class

It is possible to use ClassName::class to get a fully qualified name of class ClassName. For example:

<?php
namespace Name\Space;
class ClassName {}

echo ClassName::class;

echo "\n";
?>

위 예제의 출력:

Name\Space\ClassName

OPcache extension added

The Zend Optimiser+ opcode cache has been added to PHP as the new OPcache extension. OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request. See the installation instructions for more detail on enabling and using OPcache.

foreach now supports non-scalar keys

foreach now supports keys of any type. While non-scalar keys cannot occur in native PHP arrays, it is possible for Iterator::key() to return a value of any type, and this will now be handled correctly.

Apache 2.4 handler supported on Windows

The Apache 2.4 handler SAPI is now supported on Windows.

Improvements to GD

Various improvements have been made to the GD extension, these include:

  • Flipping support using the new imageflip() function.
  • Advanced cropping support using the imagecrop() & imagecropauto() functions.
  • WebP read and write support using imagecreatefromwebp() & imagewebp() respectively.

User Contributed Notes

wallacemaxters at gmail dot com 14-Oct-2016 01:17
After PHP 5.5, foreach with reference in arbitrary array does not generate an error:

    foreach ([1, 2, 3, 4] as $key => &$value) {
        $value = $value . ' R$';
    }

before 5.4, will generate  "Cannot create references to elements of a temporary array expression"
k-gun @ github 22-Mar-2016 06:35
Array dereferencing with defaults for possibly empty sources;

<?php
// string(4) "def2"
$x = ([] ?: ["def1", "def2"])[1];
?>
Anonymous 15-Apr-2015 01:53
Yield is awesome... it's starts to look like a real language now ;-)

class myList {
    public $list;
    public function __construct($list) {
        $this->list = $list;
    }

    public function select(...$keys) {
        $keys_array = array_fill_keys($keys, null);
        $items = $this->list;
        foreach($items as $item) {
            yield array_merge($keys_array, array_intersect_key($item, $keys_array));
        }
    }
}
er dot ankitvishwakarma at gmail dot com 18-Sep-2014 01:48
All the new features are incredible. I had always been waiting for such things to be happened.
mykolas dot sirius at gmail dot com 02-Apr-2014 05:10
Finally, array and string literal dereferencing is here, bless you all!
yannicschne at gmail dot com 01-Apr-2014 08:34
The array and string dereferencing is awesome! Thanks for this.
Love this 24-Jan-2014 03:45
Thanks for making "empty" easier to use and for the array dereferencing!!
emanwebdev at gmail dot com 17-Dec-2013 07:51
Thank You for making empty() to support arbitrary expressions!
PHP Guy 08-Nov-2013 02:59
Great work.  Thank you for allowing Lists to return on foreach.


Deprecated features in PHP 5.5.x

ext/mysql deprecation

The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions.

preg_replace() /e modifier

The preg_replace() /e modifier is now deprecated. Instead, use the preg_replace_callback() function.

intl deprecations

IntlDateFormatter::setTimeZoneID() and
datefmt_set_timezone_id() are now deprecated. Instead,
use the IntlDateFormatter::setTimeZone() method and
datefmt_set_timezone() function, respectively.

mcrypt deprecations

The following functions have been deprecated:

  • mcrypt_cbc()
  • mcrypt_cfb()
  • mcrypt_ecb()
  • mcrypt_ofb()

User Contributed Notes

Gianluca 15-Mar-2016 10:26
In these wrappers all mysql and ereg functions are rewritten using mysqli and pcre
https://sourceforge.net/projects/mysqlwithmysqli/
https://sourceforge.net/projects/ereg-using-pcre/


Changed Functions

PHP Core

  • set_error_handler() can now be called with NULL as an argument to reset the error handler.
  • When called with NULL, set_error_handler() and set_exception_handler() now return the previous error or exception handler, respectively.
  • json_encode() now accepts depth parameter.
  • The behaviour of pack() and unpack() with the "a" and "A" format codes has changed. Detailed notes on these changes are available.

intl

  • MessageFormatter::format() and related functions now accept named arguments and mixed numeric and named arguments when PHP is linked to ICU 4.8 or later.
  • MessageFormatter::format() and related functions no longer error when an insufficient number of arguments have been provided. Instead, the placeholders will not be substituted.
  • MessageFormatter::format() and MessageFormatter::parse() are no longer limited to second precision when dealing with times.
  • IntlDateFormatter::__construct() and datefmt_create() now accept IntlTimeZone and DateTimeZone objects for the timezone argument, and IntlCalendar objects for the calendar argument. Furthermore, if the time zone is omitted and the calendar doesn't specify a time zone, PHP's default time zone from date_default_timezone_get() is now used instead of the default ICU time zone.
  • IntlDateFormatter::getCalendar() and datefmt_get_calendar() return false if the IntlDateFormatter object was created with an IntlCalendar instance instead of one of the IntlDateFormatter constants.
  • IntlDateFormatter::setCalendar() and datefmt_set_calendar() now accept IntlCalendar objects in addition to the IntlDateFormatter constants.
  • IntlDateFormatter::format() and datefmt_format() now accept IntlCalendar objects.

User Contributed Notes

There are no user contributed notes for this page.


New Functions

PHP Core

  • array_column()
  • boolval()
  • json_last_error_msg()
  • password_get_info()
  • password_hash()
  • password_needs_rehash()
  • password_verify()

Hash

  • hash_pbkdf2()

OpenSSL

  • openssl_pbkdf2()

cURL

  • curl_escape()
  • curl_file_create()
  • curl_multi_setopt()
  • curl_multi_strerror()
  • curl_pause()
  • curl_reset()
  • curl_share_close()
  • curl_share_init()
  • curl_share_setopt()
  • curl_strerror()
  • curl_unescape()

GD

  • imageaffinematrixconcat()
  • imageaffinematrixget()
  • imagecrop()
  • imagecropauto()
  • imageflip()
  • imagepalettetotruecolor()
  • imagescale()

MySQLi

  • mysqli_begin_transaction()
  • mysqli_release_savepoint()
  • mysqli_savepoint()

PostgreSQL

  • pg_escape_literal()
  • pg_escape_identifier()

Sockets

  • socket_sendmsg()
  • socket_recvmsg()
  • socket_cmsg_space()

CLI

  • cli_get_process_title()
  • cli_set_process_title()

Intl

  • datefmt_format_object()
  • datefmt_get_calendar_object()
  • datefmt_get_timezone()
  • datefmt_set_timezone()
  • datefmt_get_calendar_object()
  • intlcal_create_instance()
  • intlcal_get_keyword_values_for_locale()
  • intlcal_get_now()
  • intlcal_get_available_locales()
  • intlcal_get()
  • intlcal_get_time()
  • intlcal_set_time()
  • intlcal_add()
  • intlcal_set_time_zone()
  • intlcal_after()
  • intlcal_before()
  • intlcal_set()
  • intlcal_roll()
  • intlcal_clear()
  • intlcal_field_difference()
  • intlcal_get_actual_maximum()
  • intlcal_get_actual_minimum()
  • intlcal_get_day_of_week_type()
  • intlcal_get_first_day_of_week()
  • intlcal_get_greatest_minimum()
  • intlcal_get_least_maximum()
  • intlcal_get_locale()
  • intlcal_get_maximum()
  • intlcal_get_minimal_days_in_first_week()
  • intlcal_get_minimum()
  • intlcal_get_time_zone()
  • intlcal_get_type()
  • intlcal_get_weekend_transition()
  • intlcal_in_daylight_time()
  • intlcal_is_equivalent_to()
  • intlcal_is_lenient()
  • intlcal_is_set()
  • intlcal_is_weekend()
  • intlcal_set_first_day_of_week()
  • intlcal_set_lenient()
  • intlcal_equals()
  • intlcal_get_repeated_wall_time_option()
  • intlcal_get_skipped_wall_time_option()
  • intlcal_set_repeated_wall_time_option()
  • intlcal_set_skipped_wall_time_option()
  • intlcal_from_date_time()
  • intlcal_to_date_time()
  • intlcal_get_error_code()
  • intlcal_get_error_message()
  • intlgregcal_create_instance()
  • intlgregcal_set_gregorian_change()
  • intlgregcal_get_gregorian_change()
  • intlgregcal_is_leap_year()
  • intltz_create_time_zone()
  • intltz_create_default()
  • intltz_get_id()
  • intltz_get_gmt()
  • intltz_get_unknown()
  • intltz_create_enumeration()
  • intltz_count_equivalent_ids()
  • intltz_create_time_zone_id_enumeration()
  • intltz_get_canonical_id()
  • intltz_get_region()
  • intltz_get_tz_data_version()
  • intltz_get_equivalent_id()
  • intltz_use_daylight_time()
  • intltz_get_offset()
  • intltz_get_raw_offset()
  • intltz_has_same_rules()
  • intltz_get_display_name()
  • intltz_get_dst_savings()
  • intltz_from_date_time_zone()
  • intltz_to_date_time_zone()
  • intltz_get_error_code()
  • intltz_get_error_message()

SPL

  • SplFixedArray::__wakeup()

User Contributed Notes

There are no user contributed notes for this page.


New Classes and Interfaces

cURL

  • CURLFile

Date and Time

  • DateTimeImmutable
  • DateTimeInterface

Intl

  • IntlCalendar
  • IntlGregorianCalendar
  • IntlTimeZone
  • IntlBreakIterator
  • IntlRuleBasedBreakIterator
  • IntlCodePointBreakIterator

User Contributed Notes

There are no user contributed notes for this page.


New Methods

MySQLi

  • mysqli::begin_transaction()
  • mysqli::release_savepoint()
  • mysqli::savepoint()

Intl

  • IntlDateFormatter::formatObject()
  • IntlDateFormatter::getCalendarObject()
  • IntlDateFormatter::getTimeZone()
  • IntlDateFormatter::setTimeZone()

User Contributed Notes

There are no user contributed notes for this page.


Other changes to extensions

Intl

The intl extension now requires ICU 4.0 or later.

User Contributed Notes

There are no user contributed notes for this page.


New Global Constants

GD

  • IMG_AFFINE_TRANSLATE
  • IMG_AFFINE_SCALE
  • IMG_AFFINE_ROTATE
  • IMG_AFFINE_SHEAR_HORIZONTAL
  • IMG_AFFINE_SHEAR_VERTICAL
  • IMG_CROP_DEFAULT
  • IMG_CROP_TRANSPARENT
  • IMG_CROP_BLACK
  • IMG_CROP_WHITE
  • IMG_CROP_SIDES
  • IMG_FLIP_BOTH
  • IMG_FLIP_HORIZONTAL
  • IMG_FLIP_VERTICAL
  • IMG_BELL
  • IMG_BESSEL
  • IMG_BICUBIC
  • IMG_BICUBIC_FIXED
  • IMG_BLACKMAN
  • IMG_BOX
  • IMG_BSPLINE
  • IMG_CATMULLROM
  • IMG_GAUSSIAN
  • IMG_GENERALIZED_CUBIC
  • IMG_HERMITE
  • IMG_HAMMING
  • IMG_HANNING
  • IMG_MITCHELL
  • IMG_POWER
  • IMG_QUADRATIC
  • IMG_SINC
  • IMG_NEAREST_NEIGHBOUR
  • IMG_WEIGHTED4
  • IMG_TRIANGLE

JSON

  • JSON_ERROR_RECURSION
  • JSON_ERROR_INF_OR_NAN
  • JSON_ERROR_UNSUPPORTED_TYPE

MySQLi

The MYSQLI_SERVER_PUBLIC_KEY option has been added for use with mysqli_options().

User Contributed Notes

There are no user contributed notes for this page.


Changes to INI file handling

Intl

The intl.use_exceptions configuration directive has been added, which controls the behaviour of intl when global errors occur in conjunction with the already extant intl.error_level configuration directive.

MySQLnd

The mysqlnd.sha256_server_public_key configuration directive has been added to allow mysqli to use the new MySQL authentication protocol.

User Contributed Notes

There are no user contributed notes for this page.


Changes to PHP Internals

  • Extensions cannot override zend_execute() any more and should override zend_execute_ex() instead. EG(current_execute_data) is already initialized in zend_execute_ex(), so for compatibility extensions may need to use EG(current_execute_data)->prev_execute_data instead.
  • Removed EG(arg_types_stack), EX(fbc), EX(called_scope) and EX(current_object).
  • Added op_array->nested_calls, which is calculated at compile time.
  • Added EX(call_slots), which is an array to store information about syntaticaly nested calls (e.g. foo(bar())) and is preallocated together with execute_data.
  • Added EX(call), which is a pointer to a current calling function, and is an element of EX(call_slots).
  • Opcodes INIT_METHOD_CALL, ZEND_INIT_STATIC_METHOD_CALL, ZEND_INIT_FCALL_BY_NAME and ZEND_INIT_NS_FCALL_BY_NAME use result.num as an index in EX(call_slots).
  • Opcode ZEND_NEW uses extended_value as an index in EX(call_slots).
  • Opcodes ZEND_DO_FCALL and ZEND_DO_FCALL_BY_NAME use op2.num as an index in EX(call_slots).
  • Added op_array->used_stack, which is calculated at compile time; the corresponding stack space is preallocated together with execute_data. As a result, the ZEND_SEND* and ZEND_DO_FCALL* opcodes no longer need to check for stack overflow.
  • Removed execute_data->Ts field. The VM temporary variables are always allocated immediately before the execute_data structure, and are now accessed by their offset from the execute_data base pointer instead of via execute_data->Ts. The compiler stores new offsets in op_array->opcodes[*].op?.num. The EX_TMP_VAR() and EX_TMP_VAR_NUM() macros can be used to access temporary variables by offset or number. You can convert the number to an offset using EX_TMP_VAR_NUM(0, num) or offset to number using (EX_TMP_VAR_NUM(0,0)-EX_TMP_VAR(0,offset)).
  • Removed the execute_data->CVs field. The VM compiled variables are always allocated immediately after the execute_data structure, and are now accessed by the offset from the execute_data base pointer instead of via execute_data->CVs. You can use the EX_CV_NUM() macro to access compiled variables by number.

User Contributed Notes

There are no user contributed notes for this page.


Migrating from PHP 5.3.x to PHP 5.4.x

Table of Contents

  • What has changed in PHP 5.4.x
  • Backward Incompatible Changes
  • New features
  • Changes in SAPI modules
  • Deprecated features in PHP 5.4.x
  • Changed Functions
  • New Functions
  • New Classes and Interfaces
  • New Methods
  • Removed Extensions
  • Other changes to extensions
  • New Global Constants
  • Changes to INI file handling
  • Other changes

PHP 버전에 맞는 이행 가이드를 참고하십시오. 5.0.x, 5.1.x, 5.2.x, 5.3.x, 5.5.x and 5.6.x.

User Contributed Notes

There are no user contributed notes for this page.


What has changed in PHP 5.4.x

Most improvements in PHP 5.4.x have no impact on existing code. There are a few incompatibilities and new features that should be considered, and code should be tested before switching PHP versions in production environments.

For systems being upgraded from an older version of PHP, the relevant documentation is available at:

  • Upgrade Notes for PHP 5.3.x.
  • Upgrade Notes for PHP 5.2.x.
  • Upgrade Notes for PHP 5.1.x.
  • Migrating from PHP 4 to PHP 5.

User Contributed Notes

There are no user contributed notes for this page.


Backward Incompatible Changes

Although most existing PHP 5 code should work without changes, please take note of some backward incompatible changes:

  • Safe mode is no longer supported. Any applications that rely on safe mode may need adjustment, in terms of security.
  • Magic quotes has been removed. Applications relying on this feature may need to be updated, to avoid security issues. get_magic_quotes_gpc() and get_magic_quotes_runtime() now always return FALSE. set_magic_quotes_runtime() raises an E_CORE_ERROR level error on trying to enable Magic quotes.
  • The register_globals and register_long_arrays php.ini directives have been removed.
  • The mbstring.script_encoding directive has been removed. Use zend.script_encoding instead.
  • Call-time pass by reference has been removed.
  • The break and continue statements no longer accept variable arguments (e.g., break 1 + foo() * $bar;). Static arguments still work, such as break 2;. As a side effect of this change break 0; and continue 0; are no longer allowed.
  • In the date and time extension, the timezone can no longer be set using the TZ environment variable. Instead you have to specify a timezone using the date.timezone php.ini option or date_default_timezone_set() function. PHP will no longer attempt to guess the timezone, and will instead fall back to "UTC" and issue a E_WARNING.
  • Non-numeric string offsets - e.g. $a['foo'] where $a is a string - now return false on isset() and true on empty(), and produce a E_WARNING if you try to use them. Offsets of types double, bool and null produce a E_NOTICE. Numeric strings (e.g. $a['2']) still work as before. Note that offsets like '12.3' and '5 foobar' are considered non-numeric and produce a E_WARNING, but are converted to 12 and 5 respectively, for backward compatibility reasons. Note: Following code returns different result. $str='abc';var_dump(isset($str['x'])); // false for PHP 5.4 or later, but true for 5.3 or less
  • Converting an array to a string will now generate an E_NOTICE level error, but the result of the cast will still be the string "Array".
  • Turning NULL, FALSE, or an empty string into an object by adding a property will now emit an E_WARNING level error, instead of E_STRICT.
  • Parameter names that shadow super globals now cause a fatal error. This prohibits code like function foo($_GET, $_POST) {}.
  • The Salsa10 and Salsa20 hash algorithms have been removed.
  • array_combine() now returns array() instead of FALSE when two empty arrays are provided as parameters.
  • If you use htmlentities() with asian character sets, it works like htmlspecialchars() - this has always been the case in previous versions of PHP, but now an E_STRICT level error is emitted.
  • The third parameter of ob_start() has changed from boolean erase to integer flags. Note that code that explicitly set erase to FALSE will no longer behave as expected in PHP 5.4: please follow this example to write code that is compatible with PHP 5.3 and 5.4.

The following keywords are now reserved, and may not be used as names by functions, classes, etc.

  • trait
  • callable
  • insteadof

The following functions have been removed from PHP:

  • define_syslog_variables()
  • import_request_variables()
  • session_is_registered(), session_register() and session_unregister().
  • The aliases
mysqli_bind_param(), mysqli_bind_result(),
mysqli_client_encoding(), mysqli_fetch(),
mysqli_param_count(), mysqli_get_metadata(),
mysqli_send_long_data(), mysqli::client_encoding() and mysqli_stmt::stmt().

User Contributed Notes

contact at nouslisons dot com 12-Mar-2015 06:38
Be careful, in php 5.4. \e in string mean the escape character !
So you will have error for windows path:
$a = "c:\extend\toto.txt";

AND for date function:
echo date ("\t\h\e dmy");
you will have an escape character in your source instead "e"

The workaround I found is simple quote:
echo date ('\t\h\e dmy');
ky dot patterson at adlinkr dot com 24-Jul-2014 04:34
If you have content that is not 100% UTF-8 then TAKE NOTE:

Starting in PHP 5.4 htmlspecialchars() and htmlentities() assume CHARSET=ks_c_5601-1987 by default AND WILL RETURN BLANK IF YOUR INPUT IS NOT VALID UTF-8.

So if you have a lot of function calls that look like this:
<?php
echo htmlspecialchars($input);
// or
echo htmlentities($input);
?>
i.e. no charset and no flags -- and $input is ISO-8859 (or anything else apart from 7-bit ASCII or UTF-8) -- then PHP 5.4 and 5.5 will return an empty string, and you will be surprised and probably unhappy.

This is apparently a feature, not a bug.
blagdan at gmail dot com 30-Jan-2014 11:41
'<?if'... worked in 5.3 as equivalent of '<?php if'... In 5.4 it's converted to '<!--?if'...
anton at zebooka dot com 05-Sep-2013 12:18
It seems that starting of PHP 5.4 you can not override class method with different signature.

Example:
<?php
class A
{
    public function doSomething($a, $b)
    {
    }
}

class B extends A
{
    public function doSomething($c)
    {
    }
}
?>

PHP Strict standards:  Declaration of B::doSomething() should be compatible with A::doSomething(B $a) in Command line code on line 1
the dot mail dot bg at gmail dot com 23-Aug-2013 12:03
There should be samples given here. There are many ways to do such coversion:

Converting an array to a string will now generate an E_NOTICE level error, but the result of the cast will still be the string "Array".
luis at portanel dot com 08-Mar-2013 02:46
It's not a PHP version incompatibility itself, but it's important to know that Microsoft dropped the php_mssql.dll support for the "mssql_" funcitions since this version.

To connect to a MSSQL database since 5.4, one good alternative are the PDO drivers.
Chris 06-Jan-2013 12:00
Missing some chars like german umlauts after use of htmlspecialchars? That's because the third param encoding has changed it's default value in PHP 5.4 from ISO-8859-1 to UTF-8.

Possible solution #1:
Change your code from this ...
<?php htmlspecialchars( 'aou' ); ?>
... to this:
<?php htmlspecialchars ( 'aou' , ENT_COMPAT | ENT_HTML401 , 'ISO-8859-1' ); ?>

Possible solution #2:
Create a wrapper function and replace htmlspecialchars( to i.e. isohtmlspecialchars( with your IDE/editor/shell...

Example of a wrapper function:
<?php
function isohtmlspecialchars( $str ){
   return htmlspecialchars ( $str , ENT_COMPAT | ENT_HTML401 , 'ISO-8859-1' );
}
?>


New features

PHP 5.4.0 offers a wide range of new features:

  • Support for traits has been added.
  • Short array syntax has been added, e.g. $a = [1, 2, 3, 4]; or $a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];.
  • Function array dereferencing has been added, e.g. foo()[0].
  • Closures now support $this.
  • <?= is now always available, regardless of the short_open_tag php.ini option.
  • Class member access on instantiation has been added, e.g. (new Foo)->bar().
  • Class::{expr}() syntax is now supported.
  • Binary number format has been added, e.g. 0b001001101.
  • Improved parse error messages and improved incompatible arguments warnings.
  • The session extension can now track the upload progress of files.
  • Built-in development web server in CLI mode.

User Contributed Notes

Anonymous 21-Mar-2013 10:41
LDAP supports paged results.  See http://php.net/manual/en/function.ldap-control-paged-result.php for details.
dave1010 at gmail dot com 27-Jul-2012 08:01
As of PHP 5.4, the CLI (using readline) no longer dies on fatal errors (for example calling undefined functions).
Nick Garvey 22-Jun-2012 03:11
'callable' was implemented as a typehint in 5.4
Anonymous 22-Oct-2011 05:50
The keyword 'insteadof' is introduced with PHP 5.4 http://php.net/language.oop5.traits.php


Changes in SAPI modules

  • A new SAPI module named cli-server is now available.
  • Added CLI option --rz which shows information of the named Zend extension.
  • Added shortcut #inisetting=value to change php.ini settings at run-time in interactive readline CLI
  • Added apache compatible functions: apache_child_terminate(),
getallheaders(), apache_request_headers()
and apache_response_headers() for FastCGI SAPI.
  • PHP-FPM: Added the process.max setting, to control the number of processes that FPM can fork.

User Contributed Notes

There are no user contributed notes for this page.


Deprecated features in PHP 5.4.x

Deprecated functions:

  • mysql_list_dbs()

User Contributed Notes

HappyDog 19-Nov-2015 07:51
Actually, it appears that  mcrypt_generic_end() was deprecated in PHP 5.3, not PHP 5.4 (based on what ReflectionFunction::isDeprecated() reports in PHP 5.3, and also the documentation page for the function).


Changed Functions

Several functions were given new, optional parameters in PHP 5.4:

PHP Core:

  • Added the optional limit parameter to debug_backtrace() and debug_print_backtrace(), to limit the amount of stack frames returned.
  • is_link() now works properly for symbolic links on Windows Vista or later. Earlier systems do not support symbolic links.
  • parse_url() now recognizes the host when a scheme is omitted, and a leading component separator is present. As of PHP 5.4.7.

OpenSSL:

  • Added a no padding option to the openssl_encrypt() and openssl_decrypt() functions.

Intl:

  • idn_to_ascii() and idn_to_utf8() now take two extra parameters, one indicating the variant (IDNA 2003 or UTS #46) and another, passed by reference, to return details about the operation in case UTS #46 is chosen.

User Contributed Notes

There are no user contributed notes for this page.


New Functions

PHP 5.4 introduced some new functions:

PHP Core:

  • hex2bin()
  • http_response_code()
  • get_declared_traits()
  • getimagesizefromstring()
  • stream_set_chunk_size()
  • socket_import_stream()
  • trait_exists()
  • header_register_callback()

SPL:

  • class_uses()

Session:

  • session_status()
  • session_register_shutdown()

Mysqli:

  • mysqli_error_list()
  • mysqli_stmt_error_list()

Libxml:

  • libxml_set_external_entity_loader()

LDAP:

  • ldap_control_paged_result()
  • ldap_control_paged_result_response()

Intl:

  • transliterator_create()
  • transliterator_create_from_rules()
  • transliterator_create_inverse()
  • transliterator_get_error_code()
  • transliterator_get_error_message()
  • transliterator_list_ids()
  • transliterator_transliterate()

Zlib:

  • zlib_decode()
  • zlib_encode()

User Contributed Notes

There are no user contributed notes for this page.


New Classes and Interfaces

The following classes were introduced in PHP 5.4.0:

SPL:

  • CallbackFilterIterator
  • RecursiveCallbackFilterIterator

Reflection:

  • ReflectionZendExtension

Json:

  • JsonSerializable

Session:

  • SessionHandler
  • SessionHandlerInterface

Snmp:

  • SNMP

Intl:

  • Transliterator
  • Spoofchecker

User Contributed Notes

There are no user contributed notes for this page.


New Methods

Several new methods were introduced in 5.4.0:

XSL:

  • XsltProcessor::setSecurityPrefs()
  • XsltProcessor::getSecurityPrefs()

SPL:

  • RegexIterator::getRegex()
  • SplObjectStorage::getHash()
  • DirectoryIterator::getExtension()
  • SplDoublyLinkedList::serialize()
  • SplDoublyLinkedList::unserialize()
  • SplFileInfo::getExtension()
  • SplFileObject::fputcsv()
  • SplQueue::serialize()
  • SplQueue::unserialize()
  • SplStack::serialize()
  • SplStack::unserialize()
  • SplTempFileObject::fputcsv()

Reflection:

  • ReflectionExtension::isPersistent()
  • ReflectionExtension::isTemporary()
  • ReflectionClass::isCloneable()

Closure:

  • Closure::bind()
  • Closure::bindTo()

PDO_dblib:

  • PDO::newRowset()

StreamWrapper:

  • StreamWrapper::stream_metadata()
  • StreamWrapper::stream_truncate()

User Contributed Notes

There are no user contributed notes for this page.


Removed Extensions

These extensions have been moved to PECL and are no longer part of the PHP distribution. The PECL package versions of these extensions will be created according to user demand.

  • sqlite - Note that ext/sqlite3 and ext/pdo_sqlite are not affected

User Contributed Notes

There are no user contributed notes for this page.


Other changes to extensions

Changes in extension behavior, and new features:

  • mysqli - mysqli_result now implements Traversable
  • pdo_mysql - Removed support for linking with MySQL client libraries older than 4.1
  • The MySQL extensions mysql, mysqli and PDO_mysql use mysqlnd as the default library now. It is still possible to use libmysqlclient by specifying a path to the configure options.
  • mysqlnd - Added named pipes support

User Contributed Notes

There are no user contributed notes for this page.


New Global Constants

PHP Core:

  • ENT_DISALLOWED
  • ENT_HTML401
  • ENT_HTML5
  • ENT_SUBSTITUTE
  • ENT_XML1
  • ENT_XHTML
  • IPPROTO_IP
  • IPPROTO_IPV6
  • IPV6_MULTICAST_HOPS
  • IPV6_MULTICAST_IF
  • IPV6_MULTICAST_LOOP
  • IP_MULTICAST_IF
  • IP_MULTICAST_LOOP
  • IP_MULTICAST_TTL
  • MCAST_JOIN_GROUP
  • MCAST_LEAVE_GROUP
  • MCAST_BLOCK_SOURCE
  • MCAST_UNBLOCK_SOURCE
  • MCAST_JOIN_SOURCE_GROUP
  • MCAST_LEAVE_SOURCE_GROUP

Curl:

  • CURLOPT_MAX_RECV_SPEED_LARGE
  • CURLOPT_MAX_SEND_SPEED_LARGE

LibXML:

  • LIBXML_HTML_NODEFDTD
  • LIBXML_HTML_NOIMPLIED
  • LIBXML_PEDANTIC

OpenSSL:

  • OPENSSL_CIPHER_AES_128_CBC
  • OPENSSL_CIPHER_AES_192_CBC
  • OPENSSL_CIPHER_AES_256_CBC
  • OPENSSL_RAW_DATA
  • OPENSSL_ZERO_PADDING

Output buffering:

  • PHP_OUTPUT_HANDLER_CLEAN
  • PHP_OUTPUT_HANDLER_CLEANABLE
  • PHP_OUTPUT_HANDLER_DISABLED
  • PHP_OUTPUT_HANDLER_FINAL
  • PHP_OUTPUT_HANDLER_FLUSH
  • PHP_OUTPUT_HANDLER_FLUSHABLE
  • PHP_OUTPUT_HANDLER_REMOVABLE
  • PHP_OUTPUT_HANDLER_STARTED
  • PHP_OUTPUT_HANDLER_STDFLAGS
  • PHP_OUTPUT_HANDLER_WRITE

Sessions:

  • PHP_SESSION_ACTIVE
  • PHP_SESSION_DISABLED
  • PHP_SESSION_NONE

Streams:

  • STREAM_META_ACCESS
  • STREAM_META_GROUP
  • STREAM_META_GROUP_NAME
  • STREAM_META_OWNER
  • STREAM_META_OWNER_NAME
  • STREAM_META_TOUCH

Zlib:

  • ZLIB_ENCODING_DEFLATE
  • ZLIB_ENCODING_GZIP
  • ZLIB_ENCODING_RAW

Intl:

  • U_IDNA_DOMAIN_NAME_TOO_LONG_ERROR
  • IDNA_CHECK_BIDI
  • IDNA_CHECK_CONTEXTJ
  • IDNA_NONTRANSITIONAL_TO_ASCII
  • IDNA_NONTRANSITIONAL_TO_UNICODE
  • INTL_IDNA_VARIANT_2003
  • INTL_IDNA_VARIANT_UTS46
  • IDNA_ERROR_EMPTY_LABEL
  • IDNA_ERROR_LABEL_TOO_LONG
  • IDNA_ERROR_DOMAIN_NAME_TOO_LONG
  • IDNA_ERROR_LEADING_HYPHEN
  • IDNA_ERROR_TRAILING_HYPHEN
  • IDNA_ERROR_HYPHEN_3_4
  • IDNA_ERROR_LEADING_COMBINING_MARK
  • IDNA_ERROR_DISALLOWED
  • IDNA_ERROR_PUNYCODE
  • IDNA_ERROR_LABEL_HAS_DOT
  • IDNA_ERROR_INVALID_ACE_LABEL
  • IDNA_ERROR_BIDI
  • IDNA_ERROR_CONTEXTJ

Json:

  • JSON_PRETTY_PRINT
  • JSON_UNESCAPED_SLASHES
  • JSON_NUMERIC_CHECK
  • JSON_UNESCAPED_UNICODE
  • JSON_BIGINT_AS_STRING

User Contributed Notes

There are no user contributed notes for this page.


Changes to INI file handling

The following php.ini directives have been removed:

  • register_globals and register_long_arrays
  • magic_quotes_gpc, magic_quotes_runtime, and magic_quotes_sybase
  • allow_call_time_pass_reference
  • define_syslog_variables
  • highlight.bg
  • session.bug_compat_42 and session.bug_compat_warn
  • mbstring.script_encoding
  • y2k_compliance
  • safe_mode, safe_mode_gid, safe_mode_include_dir, safe_mode_exec_dir, safe_mode_allowed_env_vars, and safe_mode_protected_env_vars

The following php.ini directives have been added:

  • cli.pager and cli.prompt for CLI SAPI using readline in interactive mode.
  • cli_server.color to enable the built-in development web server to use ANSI color coding in terminal output.
  • max_input_vars - specifies how many GET/POST/COOKIE input variables may be accepted.
  • zend.multibyte - to control the new multibyte support.
  • zend.script_encoding - This value will be used unless a "declare(encoding=...)" directive appears at the top of the script.
  • zend.signal_check - to check for replaced signal handlers on shutdown.
  • session.upload_progress.enabled, session.upload_progress.cleanup, session.upload_progress.prefix, session.upload_progress.name, session.upload_progress.freq, session.upload_progress.min_freq
  • enable_post_data_reading - When it's disabled, the POST data is not read (and processed)
  • windows_show_crt_warning - This directive shows the Windows CRT warnings when enabled. These warnings were displayed by default until now.

The following php.ini. directives have been changed:

  • session.entropy_file now defaults to /dev/random or /dev/urandom depending on what has been guessed at compile time.
  • session.entropy_length now defaults to 32.

User Contributed Notes

There are no user contributed notes for this page.


Other changes

  • The default character set for htmlspecialchars() and htmlentities() is now UTF-8, instead of ISO-8859-1. Note that changing your output charset via the default_charset configuration setting does not affect htmlspecialchars/htmlentities unless you are passing "" (an empty string) as the encoding parameter to your htmlspecialchars()/htmlentities() calls. Generally we do not recommend doing this because you should be able to change your output charset without affecting the runtime charset used by these functions. The safest approach is to explicitly set the charset on each call to htmlspecialchars() and htmlentities().
  • E_ALL now includes E_STRICT level errors in the error_reporting configuration directive.
  • SNMP now has an OOP API. Functions now return FALSE on every error condition including SNMP-related (no such instance, end of MIB, etc). Thus, in particular, breaks previous behavior of get/walk functions returning an empty string on SNMP-related errors. Multi OID get/getnext/set queries are now supported. Dropped UCD-SNMP compatibility code, consider upgrading to net-snmp v5.3+, Net-SNMP v5.4+ is required for Windows version. In sake of adding support for IPv6 DNS name resolution of remote SNMP agent (peer) is done by extension now, not by Net-SNMP library anymore.
  • OpenSSL now supports AES.
  • CLI SAPI doesn't terminate any more on fatal errors when using interactive mode with readline support.
  • $_SERVER['REQUEST_TIME_FLOAT'] has been added to include microsecond precision.
  • Added new hash algorithms: fnv132, fnv164, joaat
  • Chained string offsets - e.g. $a[0][0] where $a is a string - now work.
  • Arrays cast from SimpleXMLElement now always contain all nodes instead of just the first matching node. All SimpleXMLElement children are now always printed when using var_dump(), var_export() and print_r().
  • It's now possible to enforce the class' __construct arguments in an abstract constructor in the base class.

User Contributed Notes

There are no user contributed notes for this page.


Migrating from PHP 5.2.x to PHP 5.3.x

Table of Contents

  • What has changed in PHP 5.3.x
  • Backward Incompatible Changes
  • New features
  • Changes made to Windows support
  • Changes in SAPI modules
  • Deprecated features in PHP 5.3.x
  • Undeprecated features in PHP 5.3.x
  • New Parameters
  • New Functions
  • New stream wrappers
  • New stream filters
  • New Class Constants
  • New Methods
  • New Extensions
  • Removed Extensions
  • Other changes to extensions
  • New Classes
  • New Global Constants
  • Changes to INI file handling
  • Other changes

PHP 버전에 맞는 이행 가이드를 참고하십시오. 5.0.x, 5.1.x, 5.2.x, 5.4.x, 5.5.x and 5.6.x.

User Contributed Notes

There are no user contributed notes for this page.


What has changed in PHP 5.3.x

Most improvements in PHP 5.3.x have no impact on existing code. There are a few incompatibilities and new features that should be considered, and code should be tested before switching PHP versions in production environments.

For systems being upgraded from an older version of PHP, the relevant documentation is available at:

  • Upgrade Notes for PHP 5.2.x.
  • Upgrade Notes for PHP 5.1.x.
  • Migrating from PHP 4 to PHP 5.

User Contributed Notes

There are no user contributed notes for this page.


Backward Incompatible Changes

Although most existing PHP 5 code should work without changes, please take note of some backward incompatible changes:

  • The newer internal parameter parsing API has been applied across all the extensions bundled with PHP 5.3.x. This parameter parsing API causes functions to return NULL when passed incompatible parameters. There are some exceptions to this rule, such as the get_class() function, which will continue to return FALSE on error.
  • clearstatcache() no longer clears the realpath cache by default.
  • realpath() is now fully platform-independent. Consequence of this is that invalid relative paths such as __FILE__ . "/../x" do not work anymore.
  • The call_user_func() family of functions now propagate $this even if the callee is a parent class.
  • The array functions natsort(),
natcasesort(), usort(),
uasort(), uksort(),
array_flip(), and array_unique() no
longer accept objects passed as arguments. To apply these functions to an
object, cast the object to an array first.
  • The behaviour of functions with by-reference parameters called by value has changed. Where previously the function would accept the by-value argument, a fatal error is now emitted. Any previous code passing constants or literals to functions expecting references, will need altering to assign the value to a variable before calling the function.
  • The new mysqlnd library necessitates the use of MySQL 4.1's newer 41-byte password format. Continued use of the old 16-byte passwords will cause mysql_connect() and similar functions to emit the error, "mysqlnd cannot connect to MySQL 4.1+ using old authentication."
  • The new mysqlnd library does not read mysql configuration files (my.cnf/my.ini), as the older libmysqlclient library does. If your code relies on settings in the configuration file, you can load it explicitly with the mysqli_options() function. Note that this means the PDO specific constants PDO::MYSQL_ATTR_READ_DEFAULT_FILE and PDO::MYSQL_ATTR_READ_DEFAULT_GROUP are not defined if MySQL support in PDO is compiled with mysqlnd.
  • The trailing / has been removed from the SplFileInfo class and other related directory classes.
  • The __toString() magic method can no longer accept arguments.
  • The magic methods
__get(),
__set(),
__isset(),
__unset(), and
__call() must always
be public and can no longer be static. Method signatures are now enforced.
  • The __call() magic method is now invoked on access to private and protected methods.
  • func_get_arg(), func_get_args() and func_num_args() can no longer be called from the outermost scope of a file that has been included by calling include or require from within a function in the calling file.
  • An emulation layer for the MHASH extension to wrap around the Hash extension have been added. However not all the algorithms are covered, notable the s2k hashing algorithm. This means that s2k hashing is no longer available as of PHP 5.3.0.

The following keywords are now reserved and may not be used in function, class, etc. names.

  • goto
  • namespace

User Contributed Notes

peterix at gmail dot com 09-Jun-2016 04:23
strlen changed in 5.3.0. It no longer returns 5 when called on Arrays.

If you abused this in your codebase, beware.
munaw at munaw dot com 25-Aug-2010 05:32
gd_info():
    Note:
    Previous to PHP 5.3.0, the JPEG Support attribute was named JPG Support.

Be aware of this, in case you verify GD image support by this attribute!
Chris Bolt 16-Sep-2009 04:03
call_user_func_array() no longer accepts null as a second parameter and calls the function. It now emits a warning and does not call the function.


New features

PHP 5.3.0 offers a wide range of new features:

  • Support for namespaces has been added.
  • Support for Late Static Bindings has been added.
  • Support for jump labels (limited goto) has been added.
  • Support for native Closures (Lambda/Anonymous functions) has been added.
  • There are two new magic methods, __callStatic() and __invoke().
  • Nowdoc syntax is now supported, similar to Heredoc syntax, but with single quotes.
  • It is now possible to use Heredocs to initialize static variables and class properties/constants.
  • Heredocs may now be declared using double quotes, complementing the Nowdoc syntax.
  • Constants can now be declared outside a class using the const keyword.
  • The ternary operator now has a shorthand form: ?:.
  • The HTTP stream wrapper now considers all status codes from 200 to 399 to be successful.
  • Dynamic access to static methods is now possible:
    <?php
    class C {
       public static     $foo = 123;
    }

    $a = "C";
    echo     $a::$foo;
    ?>

    위 예제의 출력:

    123
  • Exceptions can now be nested:
    <?php
    class MyCustomException extends Exception {}

    try {
        throw new     MyCustomException("Exceptional", 112);
    } catch (    Exception $e) {
            /* Note the use of the third parameter to pass $e
         * into the RuntimeException. */
            throw new RuntimeException("Rethrowing", 911, $e);
    }
    ?>
  • A garbage collector for circular references has been added, and is enabled by default.
  • The mail() function now supports logging of sent email via the mail.log configuration directive. (Note: This only applies to email sent through this function.)

User Contributed Notes

There are no user contributed notes for this page.


Changes made to Windows support

Changes to the Windows releases:

  • The minimum Windows version is now Windows XP SP3; Windows 98, ME, 2000 and NT4 are no longer supported.
  • Windows binaries now target i586 and later. i386 and i486 are not supported.
  • There is now experimental support for x64 versions of PHP on Windows.
  • There is now compiler support for Visual C++ 9 (VC9), using Visual Studio 2008. Snapshots and releases will now also be available for VC9. Old binaries using VC6 are still supported and released in the line with VC9.
  • The PDO_OCI php_pdo_oci8.dll library (for use with Oracle version 8 client libraries) is no longer being built. Instead, use php_pdo_oci.dll (note no '8') with Oracle 10 or 11 client libraries. Connection to other database versions is still supported.
  • For the OCI8 extension, a new library php_oci8_11g.dll is available in addition to php_oci8.dll. Only one can be enabled at any time. Use php_oci8.dll with Oracle 10.2 client libraries. Use php_oci8_11g.dll with Oracle 11 or later client libraries. Connection to other database versions is still supported.

Windows support has been added for the following functions:

  • checkdnsrr()
  • dns_get_record()
  • fnmatch()
  • getmxrr()
  • getopt()
  • imagecolorclosesthwb()
  • inet_ntop()
  • inet_pton()
  • link()
  • linkinfo()
  • mcrypt_create_iv()
  • readlink()
  • socket_create_pair() - This function was previously available on Windows, but was disabled as of PHP 4.3.0 due to a bug.
  • stream_socket_pair()
  • symlink()
  • time_nanosleep()
  • time_sleep_until()

Other changes:

  • Improved portability of the stat(), touch(), filemtime(), filesize() functions, and other related functions (100% portable for the available data).
  • It is now possible to create hard links on Windows using the link() function, and symbolic links using the symlink() function. Hard links are available as of Windows 2000, and symbolic links as of Windows Vista.
  • The Windows version of PHP now exposes a set of constants prefixed PHP_WINDOWS_*. A list of these constants and their usage can be found at 예약 상수.
Warning

Support for the ISAPI module has been dropped. Use the improved FastCGI SAPI module instead.

Note: A new dedicated site for PHP on Windows is now available, including downloads, release candidates, and snapshots in various flavors (thread-safe/not-thread-safe, VC6/VC9, x86/x64). The URL of this site is » http://windows.php.net/.

User Contributed Notes

Anonymous 12-Jan-2010 01:43
Due to the required getaddrinfo() function in Ws2_32.dll added to Windows XP SP2 and later.

The minimum Windows version is now Windows XP SP2; Windows XP SP1, 2000, 98, ME, and NT4 are no longer supported.

See: http://bugs.php.net/48761
philmee95 at gmail dot com 25-Jul-2009 10:48
Well worth the effort to set up fast cgi on windows. No more lockups with php or 40 procs running...and 1 killing IIS. Some PHP INI vars can't be overriden in your php files, so read up (like timeout). 8 months and no more locked up iis.
kevinpeno at gmail dot com 30-Jun-2009 11:53
Fast CGI is available for IIS6. Reading the IIS website (which is more appropriate since it is server specific) yields plenty of results regarding PHP setup under the fastCGI module. They even have an entire domain associated with PHP.

Note: links were denied because they were supposedly spam? Everything you need to know is linked at the beginning of this site:  http://php.iis.net/


Changes in SAPI modules

  • A new SAPI module named litespeed is now available.
  • FastCGI support in the CGI SAPI is now always enabled and can not be disabled. See sapi/cgi/CHANGES for more details.
  • A new CGI SAPI option, -T, can be used to measure repeated execution time of a script.
  • CGI/FastCGI now has support for .htaccess-style user-defined php.ini files.
  • The dl() function is now disabled by default, and is now available only under the CLI, CGI, and embed SAPIs.

User Contributed Notes

There are no user contributed notes for this page.


Deprecated features in PHP 5.3.x

PHP 5.3.0 introduces two new error levels: E_DEPRECATED and E_USER_DEPRECATED. The E_DEPRECATED error level is used to indicate that a function or feature has been deprecated. The E_USER_DEPRECATED level is intended for indicating deprecated features in user code, similarly to the E_USER_ERROR and E_USER_WARNING levels.

The following is a list of deprecated INI directives. Use of any of these INI directives will cause an E_DEPRECATED error to be thrown at startup.

  • define_syslog_variables
  • register_globals
  • register_long_arrays
  • safe_mode
  • magic_quotes_gpc
  • magic_quotes_runtime
  • magic_quotes_sybase
  • Comments starting with '#' are now deprecated in .INI files.

Deprecated functions:

  • call_user_method() (use call_user_func() instead)
  • call_user_method_array() (use call_user_func_array() instead)
  • define_syslog_variables()
  • dl()
  • ereg() (use preg_match() instead)
  • ereg_replace() (use preg_replace() instead)
  • eregi() (use preg_match() with the 'i' modifier instead)
  • eregi_replace() (use preg_replace() with the 'i' modifier instead)
  • mcrypt_generic_end()
  • set_magic_quotes_runtime() and its alias, magic_quotes_runtime()
  • session_register() (use the $_SESSION superglobal instead)
  • session_unregister() (use the $_SESSION superglobal instead)
  • session_is_registered() (use the $_SESSION superglobal instead)
  • set_socket_blocking() (use stream_set_blocking() instead)
  • split() (use preg_split() instead)
  • spliti() (use preg_split() with the 'i' modifier instead)
  • sql_regcase()
  • mysql_db_query() (use mysql_select_db() and mysql_query() instead)
  • mysql_escape_string() (use mysql_real_escape_string() instead)
  • Passing locale category names as strings is now deprecated. Use the LC_* family of constants instead.
  • The is_dst parameter to mktime(). Use the new timezone handling functions instead.

Deprecated features:

  • Assigning the return value of new by reference is now deprecated.
  • Call-time pass-by-reference is now deprecated.

User Contributed Notes

shad at shartley dot net 23-Sep-2010 12:20
Correction on my previous note.

sed -i 's/\$HTTP_\(.*\)_VARS/\$_\1/g' {} \;

will screw up multiple instances of $HTTP_*_VARS in the file.

You'll have to use an instance of sed like:

sed -i 's/\$HTTP_SERVER_VARS/\$_SERVER/g'
i.e.
find ./ -name "*.php" -exec sed -i 's/\$HTTP_POST_VARS/\$_POST/g' {} \;
find ./ -name "*.php" -exec sed -i 's/\$HTTP_GET_VARS/\$_GET/g' {} \;
...

for each of the deprecated variables you might have in your files.

It also occurred to me a simple php conversion script would also work:

$search=array(
    "/HTTP_SERVER_VARS/",
    "/HTTP_POST_VARS/",
    "/HTTP_ENV_VARS/",
    "/HTTP_GET_VARS/",
    "/HTTP_COOKIE_VARS/",
    "/HTTP_SESSION_VARS/",
    "/HTTP_POST_FILES/");
$replace=array(
    "_SERVER",
     "_POST",
    "_ENV",
    "_GET",
    "_COOKIES",
    "_SESSION","_FILES");
$content=file_get_contents("somefile.php");
$content=preg_replace($search,$replace,$content);
file_put_contents("somefile.php",$content);

add directory recursion functions, ect.
shad at shartley dot net 12-Sep-2010 01:37
If you previously defined register_long_arrays in php.ini and have a lot of code with $HTTP_*_VARS that need to be replaced the following may help on *.nix systems:

sed -i 's/\$HTTP_\(.*\)_VARS/\$_\1/g' filename.php

What this command does is replace all occurrences of $HTTP_*_VARS with their respective $_ counter parts, e.g. $HTTP_COOKIES_VAR to $_COOKIES

To replace alot of files, you might combine it with the find command:
find ./ -name "*.php" -exec sed -i 's/\$HTTP_\(.*\)_VARS/\$_\1/g' {} \;

For other deprecated variables like $HTTP_POST_FILES change your sed arguments:

sed -i 's/\$HTTP_POST_FILES/\$_FILES/g'

Deprecated variables created by the register_long_arrays directive as far as I know are:
$_POST
$_GET
$_SERVER
$_ENV
$_COOKIE
$_SESSION
Anonymous 26-Mar-2010 02:02
A backward compatibility break which is not documented:

Fatal error: Uncaught exception 'RuntimeException' with message 'Directory name must not be empty.'

This happens with new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $staticPath ) )


Undeprecated features in PHP 5.3.x

The is_a() function has been undeprecated by popular request; it will no longer issue an E_STRICT error.

User Contributed Notes

There are no user contributed notes for this page.


New Parameters

Several functions were given new, optional parameters in PHP 5.3:

PHP Core:

  • clearstatcache() - Added clear_realpath_cache and filename.
  • copy() - Added a stream context parameter, context.
  • fgetcsv() - Added escape.
  • ini_get_all() - Added details.
  • nl2br() - Added is_xhtml.
  • parse_ini_file() - Added scanner_mode.
  • round() - Added mode.
  • stream_context_create() - Added params.
  • strstr() and stristr() - Added before_needle.

json:

  • json_encode() - Added options.
  • json_decode() - Added depth.

Streams:

  • stream_select(),
stream_set_blocking(),
stream_set_timeout(), and
stream_set_write_buffer() now work with user-space
stream wrappers.

sybase_ct:

  • sybase_connect() - Added new.

New method parameters in PHP 5.3.0:

PHP Core:

  • Exception::__construct() - Added previous.

User Contributed Notes

There are no user contributed notes for this page.


New Functions

PHP 5.3 introduced some new functions:

PHP Core:

  • array_replace() - Replaces elements from passed arrays into one array.
  • array_replace_recursive() - Recursively replaces elements from passed arrays into one array.
  • class_alias() - Creates an alias for a user defined class.
  • forward_static_call() - Call a user function from a method context.
  • forward_static_call_array() - Call a user function from a method context, with the arguments contained in an array.
  • gc_collect_cycles() - Forces collection of any existing garbage cycles.
  • gc_disable() - Deactivates the circular reference collector.
  • gc_enable() - Activates the circular reference collector.
  • gc_enabled() - Returns the status of the circular reference collector.
  • get_called_class() - Return the name of the class a static method is called in.
  • gethostname() - Return the current host name for the local machine.
  • header_remove() - Removes an HTTP header previously set using the header() function.
  • lcfirst() - Make a string's first character lowercase.
  • parse_ini_string() - Parse a configuration string.
  • quoted_printable_encode() - Convert an 8 bit string to a quoted-printable string.
  • str_getcsv() - Parse a CSV string into an array.
  • stream_context_set_default() - Set the default stream context.
  • stream_supports_lock() - Return TRUE if the stream supports locking.
  • stream_context_get_params() - Retrieve parameters from a stream context.
  • streamWrapper::stream_cast() - Retrieve the underlying stream resource.
  • streamWrapper::stream_set_option() - Change stream options

Date/Time:

  • date_add() - Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object.
  • date_create_from_format() - Returns a new DateTime object formatted according to the given format.
  • date_diff() - Returns the difference between two DateTime objects.
  • date_get_last_errors() - Returns the warnings and errors from the last date/time operation.
  • date_parse_from_format() - Get information about a given date.
  • date_sub() - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object.
  • timezone_version_get() - Returns the version of the timezonedb.

GMP:

  • gmp_testbit() - Tests whether a bit is set.

Hash:

  • hash_copy() - Copy hashing context.

IMAP:

  • imap_gc() - Clears IMAP cache.
  • imap_utf8_to_mutf7() - Encode a UTF-8 string to modified UTF-7.
  • imap_mutf7_to_utf8() - Decode a modified UTF-7 string to UTF-8.

JSON:

  • json_last_error() - Returns the last JSON error that occurred.

MySQL Improved:

  • mysqli_fetch_all() - Fetches all result rows as an associative array, a numeric array, or both.
  • mysqli_get_connection_stats() - Returns statistics about the client connection.
  • mysqli_poll() - Poll connections.
  • mysqli_reap_async_query() - Get result from async query.

OpenSSL:

  • openssl_random_pseudo_bytes() - Returns a string of the given length specified, filled with pseudo-random bytes.

PCNTL:

  • pcntl_signal_dispatch() - Calls signal handlers for pending signals.
  • pcntl_sigprocmask() - Sets and retrieves blocked signals.
  • pcntl_sigtimedwait() - Wait for signals with a timeout.
  • pcntl_sigwaitinfo() - Wait for signals.

PCRE:

  • preg_filter() - Perform a regular expression search and replace, returning only results which matched the pattern.

Semaphore:

  • msg_queue_exists() - Check whether a message queue exists.
  • shm_has_var() - Checks whether a specific key exists inside a shared memory segment.

The following functions are now natively implemented, making them available on all operating systems which can run PHP:

  • acosh()
  • asinh()
  • atanh()
  • expm1()
  • log1p()

User Contributed Notes

There are no user contributed notes for this page.


New stream wrappers

The following stream wrappers were introduced in 5.3.0:

  • glob://
  • phar://

User Contributed Notes

There are no user contributed notes for this page.


New stream filters

The following stream filters were introduced in 5.3.0:

  • dechunk (reverses HTTP chunked encoding)
  • The bz2.decompress filter now supports concatenation.

User Contributed Notes

There are no user contributed notes for this page.


New Class Constants

Several new class constants were introduced in 5.3.0:

PDO_FIREBIRD:

  • PDO::FB_ATTR_DATE_FORMAT - Sets the format for dates.
  • PDO::FB_ATTR_TIME_FORMAT - Sets the format for time.
  • PDO::FB_ATTR_TIMESTAMP_FORMAT - Sets the format for timestamps.

User Contributed Notes

There are no user contributed notes for this page.


New Methods

Several new methods were introduced in 5.3.0:

Date/Time:

  • DateTime::add() - Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object.
  • DateTime::createFromFormat() - Returns a new DateTime object formatted according to the given format.
  • DateTime::diff() - Returns the difference between two DateTime objects.
  • DateTime::getLastErrors() - Returns the warnings and errors from the last date/time operation.
  • DateTime::sub() - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object.

Exception:

  • Exception::getPrevious() - Retrieves the previous exception.

DOM:

  • DOMNode::getLineNo() - Get the line number of a parsed node.

PDO_FIREBIRD:

  • PDO::setAttribute() - Sets an attribute.

Reflection:

  • ReflectionClass::getNamespaceName() - Returns the name of namespace where this class is defined.
  • ReflectionClass::getShortName() - Returns the short name of this class (without namespace part).
  • ReflectionClass::inNamespace() - Returns whether this class is defined in a namespace.
  • ReflectionFunction::getNamespaceName() - Returns the name of namespace where this function is defined.
  • ReflectionFunction::getShortName() - Returns the short name of the function (without namespace part).
  • ReflectionFunction::inNamespace() - Returns whether this function is defined in a namespace.
  • ReflectionProperty::setAccessible() - Sets whether non-public properties can be requested.

SPL:

  • SplObjectStorage::addAll() - Add all elements from another SplObjectStorage object.
  • SplObjectStorage::removeAll() - Remove all elements from another SplObjectStorage object.

XSL:

  • XSLTProcessor::setProfiling() - Sets the profiling output file.

User Contributed Notes

There are no user contributed notes for this page.


New Extensions

The following new extensions are added (by default) as of PHP 5.3.0:

  • Enchant - An abstraction layer above various spelling libraries
  • Fileinfo - An improved and more solid replacement, featuring full BC, for the Mimetype extension, which has been removed.
  • INTL - Internationalization extension. INTL is a wrapper around the » ICU library.
  • Phar - Implementation of PHP-Archive files.
  • SQLite3 - Support for SQLite version 3 databases.

mysqlnd is a new core library shipped with PHP. It is a PHP-specific replacement for libmysqlclient. mysqlnd will be used to build the mysql, mysqli and PDO_MySQL extensions if libmysqlclient isn't found on the system. It may also be used instead of libmysqlclient even when libmysqlclient is present. mysqlnd is recommended for all PHP installations for performance reasons.

User Contributed Notes

There are no user contributed notes for this page.


Removed Extensions

These extensions have been moved to PECL and are no longer part of the PHP distribution. The PECL package versions of these extensions will be created according to user demand.

  • dbase - No longer maintained
  • fbsql - No longer maintained
  • fdf - Maintained
  • ming - Maintained
  • msql - No longer maintained
  • ncurses - Maintained
  • sybase - Discontinued; use the sybase_ct extension instead
  • mhash - Discontinued; use the hash extension instead. hash has full mhash compatibility; all existing applications using the old functions will continue to work.

User Contributed Notes

Anonymous 16-Apr-2015 01:05
As mentioned mssql was discontinued. Summing that MDB2 doesn't support sqlsrv driver means that you are screwed if your codebase depends on MDB2 + mssql.
keith 27-Oct-2010 09:39
Hash does NOT fully implement mhash.  Notably Joomla has 2 lines in helper.php that use an unsupported function: mhash_keygen_s2k(,,,).

Other functions of mhash can be rewritten:

<?php
echo bin2hex(mhash(MHASH_RIPEMD160 ,"my_password"));

echo hash("ripemd160" ,"my_password");
?>
schwilch at gmx dot de 16-Jun-2010 02:39
MSsqlserverDriver does not support mssql-functions but sqlsrv-functions.
If you do not want to change all of your php-scripts for using other functions and another function-handling of db-connection-parameters you should use freeTDS for Windows (download for example at moodle.org) and php_dblib.dll.
I hope this helps others with MS-Systems.
Cam 28-Aug-2009 05:57
The MSSQL extension is no longer present in the 5.3.0 release, but this fact is not mentioned on this page. Perhaps that should be added.

It seems that the recommended solution is to use "SQL Server Driver for PHP" by Microsoft (http://www.microsoft.com/sqlserver/2005/en/us/php-driver.aspx). However, it is reported that the current release (v1.0) does not support PHP 5.3.0 (see http://forums.iis.net/t/1158918.aspx). Apparently, v1.1 will support PHP 5.3, but it is currently in CTP release.


Other changes to extensions

The following extensions can no longer be disabled during build configuration:

  • PCRE
  • Reflection
  • SPL

Changes in extension behaviour, and new features:

  • Date and Time - The TZ environment variable is no longer used to guess the timezone
  • cURL - cURL now supports SSH
  • Network - dns_check_record() now returns an extra "entries" index, containing the TXT elements.
  • Hash - The SHA-224 and salsa hash algorithms are now supported.
  • mbstring - Now supports CP850 encoding.
  • OCI8 - A call to oci_close() on a persistent connection, or a variable referencing a persistent connection going out of scope, will now roll back any uncommitted transaction. To avoid unexpected behavior, explicitly issue a commit or roll back as needed. The old behavior can be enabled with the INI directive oci8.old_oci_close_semantics. Database Resident Connection Pooling (DRCP) and Fast Application Notification (FAN) are now supported. Oracle External Authentication is now supported (except on Windows). The oci_bind_by_name() function now supports SQLT_AFC (aka the CHAR datatype).
  • OpenSSL - OpenSSL digest and cipher functions are now supported. It is also now possible to access the internal values of DSA, RSA and DH keys.
  • Session - Sessions will no longer store session-files in "/tmp" when open_basedir restrictions apply, unless "/tmp" is explicitly added to the list of allowed paths.
  • SOAP Now supports sending user supplied HTTP headers.
  • MySQLi Now supports persistent connections, by prepending the hostname with "p:".
  • Image Processing and GD The "JPG Support" index returned from gd_info() has been renamed to "JPEG Support".

User Contributed Notes

There are no user contributed notes for this page.


New Classes

The following classes were introduced in PHP 5.3.0:

Date/Time:

  • DateInterval
  • DatePeriod

Phar:

  • Phar
  • PharData
  • PharException
  • PharFileInfo

SPL:

  • FilesystemIterator
  • GlobIterator
  • MultipleIterator
  • RecursiveTreeIterator
  • SplDoublyLinkedList
  • SplFixedArray
  • SplHeap
  • SplMaxHeap
  • SplMinHeap
  • SplPriorityQueue
  • SplQueue
  • SplStack

User Contributed Notes

There are no user contributed notes for this page.


New Global Constants

PHP Core:

  • __DIR__
  • __NAMESPACE__
  • E_DEPRECATED
  • E_USER_DEPRECATED
  • INI_SCANNER_NORMAL
  • INI_SCANNER_RAW
  • PHP_MAXPATHLEN
  • PHP_WINDOWS_NT_DOMAIN_CONTROLLER
  • PHP_WINDOWS_NT_SERVER
  • PHP_WINDOWS_NT_WORKSTATION
  • PHP_WINDOWS_VERSION_BUILD
  • PHP_WINDOWS_VERSION_MAJOR
  • PHP_WINDOWS_VERSION_MINOR
  • PHP_WINDOWS_VERSION_PLATFORM
  • PHP_WINDOWS_VERSION_PRODUCTTYPE
  • PHP_WINDOWS_VERSION_SP_MAJOR
  • PHP_WINDOWS_VERSION_SP_MINOR
  • PHP_WINDOWS_VERSION_SUITEMASK

cURL:

  • CURLOPT_PROGRESSFUNCTION

GD:

  • IMG_FILTER_PIXELATE

JSON:

  • JSON_ERROR_CTRL_CHAR
  • JSON_ERROR_DEPTH
  • JSON_ERROR_NONE
  • JSON_ERROR_STATE_MISMATCH
  • JSON_ERROR_SYNTAX
  • JSON_FORCE_OBJECT
  • JSON_HEX_TAG
  • JSON_HEX_AMP
  • JSON_HEX_APOS
  • JSON_HEX_QUOT

LDAP:

  • LDAP_OPT_NETWORK_TIMEOUT

libxml:

  • LIBXML_LOADED_VERSION

PCRE:

  • PREG_BAD_UTF8_OFFSET_ERROR

PCNTL:

  • BUS_ADRALN
  • BUS_ADRERR
  • BUS_OBJERR
  • CLD_CONTIUNED
  • CLD_DUMPED
  • CLD_EXITED
  • CLD_KILLED
  • CLD_STOPPED
  • CLD_TRAPPED
  • FPE_FLTDIV
  • FPE_FLTINV
  • FPE_FLTOVF
  • FPE_FLTRES
  • FPE_FLTSUB
  • FPE_FLTUND
  • FPE_INTDIV
  • FPE_INTOVF
  • ILL_BADSTK
  • ILL_COPROC
  • ILL_ILLADR
  • ILL_ILLOPC
  • ILL_ILLOPN
  • ILL_ILLTRP
  • ILL_PRVOPC
  • ILL_PRVREG
  • POLL_ERR
  • POLL_HUP
  • POLL_IN
  • POLL_MSG
  • POLL_OUT
  • POLL_PRI
  • SEGV_ACCERR
  • SEGV_MAPERR
  • SI_ASYNCIO
  • SI_KERNEL
  • SI_MESGQ
  • SI_NOINFO
  • SI_QUEUE
  • SI_SIGIO
  • SI_TIMER
  • SI_TKILL
  • SI_USER
  • SIG_BLOCK
  • SIG_SETMASK
  • SIG_UNBLOCK
  • TRAP_BRKPT
  • TRAP_TRACE

User Contributed Notes

mlocati 01-Apr-2016 11:32
For these PHP versions:
- PHP 5.3
- PHP 5.4
- PHP 5.5 up to 5.5.26 (included)
- PHP 5.6 up to 5.6.10 (included)

the value of
<?php
PHP_WINDOWS_VERSION_MAJOR . '. ' . PHP_WINDOWS_VERSION_MINOR . '. ' . PHP_WINDOWS_VERSION_BUILD;
?>
is limited to 6.2.9200 (Windows 8) even if the OS is newer (Windows 8.1, Windows 10).

The reason of this problem is this change in the Windows API:
https://msdn.microsoft.com/en-us/library/windows/desktop/dn302074(v=vs.85).aspx

A fix for this has been added to PHP 5.5 from 5.5.27, and PHP 5.6 from 5.6.11 (and all versions of PHP 7 are ok).

For more details about this, see https://github.com/symfony/symfony/pull/18385#issuecomment-204406795
webmozart 07-Jan-2015 08:05
For someone that is looking for the possible values of PHP_WINDOWS_VERSION_MAJOR and PHP_WINDOWS_VERSION_MINOR (like I was), see here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724832.aspx


Changes to INI file handling

PHP 5.3.0 has significantly improved performance and parsing of INI files, and adds several new syntax features.

  • The standard php.ini files have been re-organized and renamed. php.ini-development contains settings recommded for use in development environments. php.ini-production contains settings recommended for use in production environments.
  • There is now support for two special sections: [PATH=/opt/httpd/www.example.com/] and [HOST=www.example.com]. Directives set in these sections cannot be overridden by user-defined INI files or at runtime. More information about these sections can be found here.
  • zend_extension_debug, zend_extension_debug_ts and zend_extension_ts have been removed. Use the zend_extension directive to load all Zend Extensions.
  • zend.ze1_compatibility_mode has been removed. If this INI directive is set to On, an E_ERROR error is emitted at startup.
  • It is now possible to use the full path to load modules using the extension directive.
  • "ini-variables" can now be used almost anywhere in a php.ini file.
  • open_basedir restrictions may now be tighted at runtime, and the directive is now PHP_INI_ALL.
  • It is now possible to use alphanumeric or variable indices in INI option arrays.
  • get_cfg_var() is now able to return "array" INI options.
  • Two new mail directives: mail.add_x_header and mail.log, have been added.

The following new ini directives have been added:

  • user_ini.filename and user_ini.cache_ttl have been added to control the use of user INI files.
  • exit_on_timeout has been added to force Apache 1.x children to exit if a PHP execution timeout occurs.
  • Added mbstring.http_output_conv_mimetype. This directive specifies the regex pattern of content types for which mb_output_handler() is activated.
  • Added request_order. Allows controlling which external variables will be available in $_REQUEST.

The following ini directives have new default values:

  • session.use_only_cookies is now set to "1" (enabled) by default.
  • oci8.default_prefetch has changed from "10" to "100".

User Contributed Notes

There are no user contributed notes for this page.


Other changes

  • SplFileInfo::getpathinfo() now returns information about the path name.
  • SplObjectStorage now has ArrayAccess support. It is now also possible to store associative information with objects in SplObjectStorage.
  • In the GD extension, there is now pixelation support available through the imagefilter() function.
  • var_dump() output now includes private object properties.
  • session_start() now returns FALSE when session startup fails.
  • property_exists() now checks the existence of a property independent of accessibility (like method_exists()).
  • Stream wrappers can now be used by include_path.
  • The initial parameter for array_reduce() can now be of any type.
  • The directory functions opendir(), scandir(), and dir() now use the default stream context if no explicit context is passed.
  • crypt() now has Blowfish and extended DES support, and crypt() features are now 100% portable. PHP has its own internal crypt implementation which drops into place when support for crypt or crypt_r is not found.
  • getopt() now accepts "long options" on all platforms. Optional values and = as a separator for short options are now supported.
  • fopen() has a new mode option (n), which passes O_NONBLOCK to the underlying open() system call. Note that this mode is not currently supported on Windows.
  • getimagesize() now supports icon files (.ico).
  • The mhash extension have moved to PECL, but the Hash extension have been modified to support mhash if PHP is compiled with --with-mhash. Note that the Hash extension does not require the mhash library to be available whether or not the mhash emulation is enabled.

User Contributed Notes

There are no user contributed notes for this page.


Migrating from PHP 5.1.x to PHP 5.2.x

Table of Contents

  • What has changed in PHP 5.2.x
  • Backward Incompatible Changes
  • New Error Messages
  • Changes in PHP datetime support
  • New Parameters
  • New Functions
  • New Methods
  • Removed Extensions
  • New Extensions
  • New Classes
  • New Global Constants
  • New Class Constants
  • New INI Configuration Directives
  • Error Reporting
  • Other Enhancements

PHP 버전에 맞는 이행 가이드를 참고하십시오. 5.0.x, 5.1.x, 5.3.x, 5.4.x, 5.5.x and 5.6.x.

User Contributed Notes

There are no user contributed notes for this page.


What has changed in PHP 5.2.x

Most improvements in PHP 5.2.x have no impact on existing code. There are a few incompatibilities and new error messages that should be considered, and code should be tested before switching PHP versions in production environments.

If the system is being upgraded from PHP 5.0.x, the manual section titled Upgrade Notes for PHP 5.1.x should also be read.

Similarly, if the system is being upgraded from PHP 4, the manual section titled Migrating from PHP 4 to PHP 5 should be read as well.

User Contributed Notes

There are no user contributed notes for this page.


Backward Incompatible Changes

Although most existing PHP 5 code should work without changes, you should pay attention to the following backward incompatible changes:

  • getrusage() returns NULL when passed incompatible arguments as of PHP 5.2.1.
  • ZipArchive::setCommentName() returns TRUE on success as of PHP 5.2.1.
  • ZipArchive::setCommentIndex() returns TRUE on success as of PHP 5.2.1.
  • SplFileObject::getFilename() returns the filename, not relative/path/to/file, as of PHP 5.2.1.
  • Changed priority of PHPRC environment variable on Win32 The PHPRC environment variable now takes priority over the path stored in the Windows registry.
  • CLI SAPI no longer checks cwd for php.ini or the php-cli.ini file In PHP 5.1.x an undocumented feature was added that made the CLI binary check the current working directory for a PHP configuration file, potentially leading to unpredictable behavior if an unexpected configuration file were read. This functionality was removed in 5.2.0, and PHP will no longer search CWD for the presence of php.ini or php-cli.ini files. See also the command line section of the manual.
  • Added a warning when performing modulus 0 operations In earlier versions of PHP, performing integer % 0 did not emit any warning messages, instead returning an unexpected return value of FALSE. As of PHP 5.2.0, this operation will emit an E_WARNING, as is the case in all other instances where division by zero is performed.
    <?php
    print 10 % 0;
    /* Warning:  Division by zero in filename on line n */
    ?>
  • Changed __toString() to be called wherever applicable. The magic method __toString() will now be called in a string context, that is, anywhere an object is used as a string. The fallback of returning a string that contains the object identifier was dropped in PHP 5.2.0. It became problematic because an object identifier cannot be considered unique. This change will mean that your application is flawed if you have relied on the object identifier as a return value. An attempt to use that value as a string will now result in a catchable fatal error.
    <?php
    class foo {}
    $foo = new foo;
    print     $foo;
    /* Catchable fatal error:  Object of class foo could
       not be converted to string in filename on line n */
    ?>
    Even with __toString(), objects cannot be used as array indices or keys. We may add built-in hash support for this at a later date, but as of PHP 5.2.x you will need to either provide your own hashing or use the new SPL function spl_object_hash(). Exceptions can not be thrown from __toString() methods.
    <?php
    class foo {
        public function     __toString() {
            throw new     Exception;
        }
    }

    try {
        print new     foo;
            /* Fatal error:  Method foo::__toString() must
           not throw an exception in filename on line n */
    } catch(Exception $e) {}
    ?>
  • Dropped abstract static class functions. Due to an oversight, PHP 5.0.x and 5.1.x allowed abstract static functions in classes. As of PHP 5.2.x, only interfaces can have them.
    <?php
    abstract class foo {
        abstract static function     bar();
            /* Strict Standards:  Static function foo::bar()
           should not be abstract in filename on line n */
    }
    ?>
  • Oracle extension requires at least Oracle 10 on Windows.
  • Added RFC2397 (data: stream) support. The introduction of the 'data' URL scheme has the potential to lead to a change of behavior under Windows. If you are working with a NTFS file system and making use of meta streams in your application, and if you just happen to be using a file with the name 'data:' that is accessed without any path information - it won't work any more. The fix is to use the 'file:' protocol when accessing it. See also » RFC 2397
    <?php
    /* when allow_url_include is OFF (default) */
    include "data:;base64,PD9waHAgcGhwaW5mbygpOz8+";
    /* Warning:  include(): URL file-access is disabled
       in the server configuration in filename on line n */
    ?>
  • Regression in glob() patterns In version 5.2.4 a security fix caused a regression for patterns of the form "/foo/*/bar/*". Since version 5.2.5 instead of raising a warning the glob() function will return FALSE when openbase_dir restrictions are violated.

User Contributed Notes

goellerk at bucks dot edu 07-Jun-2010 11:20
str_pad has been modified as well, to enforce UPPERCASE sensitivity on the pad_type declaration.

Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.

If entered as:
$foo = 10;
$wrong = str_pad($foo, 4,'0',str_pad_left);
print "wrong is '$wrong'<br>";

$right = str_pad($foo,4,'0',STR_PAD_LEFT);
print "right is '$right'<br>";

results:
wrong is '    '
right is '  10'
Alexander Schuch 04-Feb-2010 03:42
If the sole reason for having "abstract static methods" is to force the implementation of such a method in a child, consider using an interface for them. The abstract class implements the interface, and a child class extends the base class and defines the "abstract static methods".

<?php
interface I
{
static public function f();
}

abstract class C implements I
{
// more/other methods go here
}

class D extends C
{
static public function f()
{
echo 'I am f().';
}
}
?>
jbarker at erepublic dot com 22-Feb-2008 03:44
If any of your code relies on includes of URLS  a la allow_url_fopen, be aware that a new directive (allow_url_include) has been added, and that it defaults to Off.
Tachy 31-Jan-2008 05:04
$string="12345";
$rightstring1=substr($string,-3);
$rightstring2=substr($string,-8);
echo "Result1: ".$rightstring1."<BR>";
echo "Result2: ".$rightstring2."<BR>";

PHP5.1.x:
Result1: 345
Result2: 12345

PHP5.2.x
Result1: 345
Result2: <Empty>
php dot manual at frankkleine dot de 10-Nov-2007 01:17
Between PHP 5.2.3 and 5.2.4 another backward incompatible change was introduced: parent classes now can not access private properties of child classes with get_object_vars(). See the following example:

class Bar {
    public function dumpBar()  {
        var_dump(get_object_vars($this));
    }
}
class Foo extends Bar {
    public $public = 'public';
    protected $protected = 'protected';
    private $private = 'private';

    public function dump() {
        var_dump(get_object_vars($this));
    }
}

$foo = new Foo();
$foo->dump();
$foo->dumpBar();

The result with PHP < 5.2.4:
E:\php\tests>php get_object_vars.php
array(3) {
  ["public"]    => string(6) "public"
  ["protected"] => string(9) "protected"
  ["private"]   => string(7) "private"
}

array(3) {
  ["public"]    => string(6) "public"
  ["protected"] => string(9) "protected"
  ["private"]   => string(7) "private"
}

And the result with PHP >= 5.2.4:
E:\php-5.2.4-Win32>php ../php/tests/get_object_vars.php
array(3) {
  ["public"]    => string(6) "public"
  ["protected"] => string(9) "protected"
  ["private"]   => string(7) "private"
}

array(2) {
  ["public"]    => string(6) "public"
  ["protected"] => string(9) "protected"
}

As you can see the private property is missing now when dumped from the parent class Bar.
Erik Osterman 30-Mar-2007 04:44
It should be noted that if you provide a __toString method, you can cast the object to a string and use it as an array key (PHP 5.2.x).

e.g.   $array[ (string)$myObject ] = 'foobar';

This is an alternative to using spl_object_hash.


New Error Messages

Below are the new error messages that have not been discussed elsewhere in this document.

Example #1 In PHP Core

<?php
echo " ";
session_regenerate_id();
/*  Warning:  session_regenerate_id(): Cannot regenerate
    session id - headers already sent in filename on line n */

str_word_count("string", 4);
/* Warning:  str_word_count(): Invalid format value 4
   in filename on line n */

strripos("foo", "f", 4);
/* Notice:  strripos(): Offset is greater than the
   length of haystack string in filename on line n */

strrpos("foo", "f", 4);
/* Notice:  strrpos(): Offset is greater than the
   length of haystack string in filename on line n */

/* As of PHP 5.2.1, when allow_url_include is OFF (default) */
include "php://input";
/* Warning:  include(): URL file-access is disabled
   in the server configuration in filename on line n */
?>

Example #2 Object Oriented Code in PHP Core

<?php
interface foo {
}
class bar implements foo, foo {
}
/* Fatal error: Class bar cannot implement previously
   implemented interface foo in filename on line n */

class foo {
    public $bar;
    function __get($var)
    {
        return $this->bar;
    }
}

$foo = new foo;
$bar =& $foo->prop;
/* Notice: Indirect modification of overloaded property
   foo::$prop has no effect in filename on line n */

class foo implements iterator {
    public function current() {
    }
    public function next() {
    }
    public function key() {
    }
    public function valid() {
    }
    public function rewind() {
    }
}

$foo = new foo();
foreach($foo as &$ref) {}
/* Fatal error: An iterator cannot be used with foreach
   by reference in filename on line n */

class foo {
    private function __construct() {
    }
}
class bar extends foo {
    public function __construct() {
        parent::__construct();
        /* Fatal error:  Cannot call private
           foo::__construct() in filename on line n */
    }
}
new bar;

stream_filter_register("", "class");
/* Warning:  stream_filter_register(): Filter name
   cannot be empty in filename on line n */

stream_filter_register("filter", "");
/* Warning:  stream_filter_register(): Class name
   cannot be empty in filename on line n */
?>

Example #3 In the bzip2 Extension

<?php
bzopen("", "w");
/* Warning:  bzopen(): filename cannot be empty
   in filename on line n */

bzopen("foo", "a");
/* Warning:  bzopen(): 'a' is not a valid mode for
   bzopen(). Only 'w' and 'r' are supported in
   filename on line n */

$fp = fopen("foo", "w");
bzopen($fp, "r");
/* Warning:  bzopen(): cannot read from a stream
   opened in write only mode in filename on line n */
?>

Example #4 In the datetime Extension

<?php
strtotime("today", "now");
/* Warning:  strtotime() expects parameter 2 to be
   long, string given in filename on line n */

/* As of PHP 5.2.1 */
new DateTime(new stdclass);
/* Fatal error: Uncaught exception 'Exception' with
   message 'DateTime::__construct() expects parameter
   1 to be string, object given' in filename:n */
?>

Example #5 In the dBase Extension

<?php
dbase_open("foo", -1);
/* Warning: Invalid access mode -1 in filename on line n */

/* As of PHP 5.2.1 */
dbase_open("foo", null);
/* Warning: The filename cannot be empty in filename on line n */
?>

Example #6 In the mcrypt Extension

<?php
$key = "this is a secret key";
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td),
                        MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$encrypted_data = mcrypt_generic($td, "");
/* Warning: mcrypt_generic(): An empty string was
   passed in filename on line n */
?>

Example #7 In the oci8 Extension

<?php
oci_connect("user", "pass", "db", "bogus_charset");
/* Warning: Invalid character set name:
   bogus_charset in filename on line n */

$oci = oci_connect("user", "pass", "db");
oci_password_change($oci, "", "old", "new");
/* Warning: username cannot be empty in filename
   on line n */

oci_password_change($oci, "user", "", "new");
/* Warning: old password cannot be empty in filename
   on line n */

oci_password_change($oci, "user", "old", "");
/* Warning: new password cannot be empty in filename
   on line n */
?>

Example #8 In the SPL Extension

<?php
$obj = new SplFileObject(__FILE__);
$obj->fgetcsv("foo");
/* Warning:  SplFileObject::fgetcsv(): delimiter must
   be a character in filename on line n */

$obj->fgetcsv(",", "foo");
/* Warning:  SplFileObject::fgetcsv(): enclosure must
   be a character in filename on line n */
?>

Example #9 In the Semaphore (sysvmsg) extension

<?php
/* Warning:  maximum size of the message has to be
   greater then zero in filename on line n */
?>

Example #10 A 5.2.1+ Zip Example

<?php
$obj = new ZipArchive();
$obj->open('archive.zip');
$obj->setCommentName('', 'comment');
/* Notice:  ZipArchive::setCommentName(): Empty string
   as entry name in filename on line n */

/* As of PHP 5.2.1 */
$obj->getCommentName('');
/* Notice:  ZipArchive::getCommentName(): Empty string
   as entry name in filename on line n */
?>

User Contributed Notes

There are no user contributed notes for this page.


Changes in PHP datetime support

Since PHP 5.1.0, there has been an extension named date in the PHP core. This is the new implementation of PHP's datetime support. Although it will attempt to guess your system's timezone setting, you should set the timezone manually. You can do this in any of three ways:

  • in your php.ini using the date.timezone INI directive
  • on your system using the TZ environmental variable
  • from your script using the convenience function date_default_timezone_set()

All supported timezones are listed in the PHP Manual.

With the advent of PHP 5.2.x, there are object representations of the date and timezone, named DateTime and DateTimeZone respectively. The methods map to existing procedural date functions.

User Contributed Notes

There are no user contributed notes for this page.


New Parameters

Some functions were given new, optional, parameters in PHP 5.2.x:

PHP Core:

  • htmlentities() - added double_encode in PHP 5.2.3.
  • htmlspecialchars() - added double_encode in PHP 5.2.3.
  • base64_decode() - added strict
  • setcookie() - added httponly
  • setrawcookie() - added httponly
  • session_set_cookie_params() - added httponly
  • memory_get_usage() - added real_usage
  • get_loaded_extensions() - added zend_extensions in PHP 5.2.4

curl:

  • curl_multi_info_read() - added msgs_in_queue

datetime

  • date() - added "u" (milliseconds) format character in PHP 5.2.2

imap:

  • imap_open() - added n_retries
  • imap_reopen() - added n_retries

mbstring:

  • mb_strrpos() - added offset
    Warning

    The offset parameter was put in the position the encoding parameter used to be. Backward compatibility has been provided by allowing encoding to be specified as the third parameter. Using this backward compatibility mode is not recommended because it will be removed in a future release of PHP.

ming:

  • SWFMovie::streamMP3() - added skip in PHP 5.2.1

openssl:

  • openssl_verify() - added signature_algo

pgsql:

  • pg_escape_bytea() - added connection
  • pg_escape_string() - added connection

simplexml:

  • SimpleXMLElement::__construct() - added is_prefix
  • SimpleXMLElement::attributes() - added is_prefix
  • SimpleXMLElement::children() - added is_prefix
  • simplexml_load_file() - added is_prefix
  • simplexml_load_string() - added is_prefix

spl:

  • array iterator_to_array(Traversable it [, bool use_keys = true]) - added use_keys in PHP 5.2.1

xmlreader:

  • XMLReader::open() - added encoding and options
  • XMLReader::XML() - added encoding and options

XMLWriter:

  • xmlwriter_write_element() - the content became optional in PHP 5.2.3
  • xmlwriter_write_element_ns() - the content became optional in PHP 5.2.3

User Contributed Notes

There are no user contributed notes for this page.


New Functions

PHP 5.2.x introduced some new functions:

PHP Core:

  • array_fill_keys() - Create an array using the elements of the first parameter as keys, each initialized to val
  • error_get_last() - Get the last occurred error as associative array. Returns NULL if there hasn't been an error yet
  • image_type_to_extension() - Get file extension for image-type returned by getimagesize(), exif_read_data(), exif_thumbnail(), exif_imagetype()
  • memory_get_peak_usage() - Returns the peak allocated by PHP memory
  • sys_get_temp_dir() - Returns directory path used for temporary files. (Added in 5.2.1)
  • timezone_abbreviations_list() - Returns associative array containing DST, offset and the timezone name
  • timezone_identifiers_list() - Returns numerically indexed array with all timezone identifiers
  • timezone_name_from_abbr() - Returns the timezone name from abbreviation
  • stream_socket_shutdown() - Causes all or part of a full-duplex connection on the socket associated with stream to be shut down. As of PHP 5.2.1.

Image:

  • imagegrabscreen() - Grabs a screenshot of the whole screen. As of PHP 5.2.2.
  • imagegrabwindow() - Captures a window. As of PHP 5.2.2.

libXML:

  • libxml_disable_entity_loader() - Disable the ability to load external entities. As of PHP 5.2.11.

mbstring:

  • mb_stripos() - Finds position of first occurrence of a string within another, case insensitive
  • mb_stristr() - Finds first occurrence of a string within another, case insensitive
  • mb_strrchr() - Finds the last occurrence of a character in a string within another
  • mb_strrichr() - Finds the last occurrence of a character in a string within another, case insensitive
  • mb_strripos() - Finds position of last occurrence of a string within another, case insensitive
  • mb_strstr() - Finds first occurrence of a string within another

ming (As of PHP 5.2.1):

  • void ming_setSWFCompression(int num) - Sets output compression
  • void swfmovie::namedanchor(string name) - Creates anchor
  • void swfmovie::protect([string password]) - Protects

openssl:

  • openssl_csr_get_public_key() - Extracts public key from a CERT and prepares it for use
  • openssl_csr_get_subject() - Returns the subject of a CERT
  • openssl_pkey_get_details() - Returns an array with the key details (bits, pkey, type)

spl:

  • spl_object_hash() - Return hash id for given object
  • int iterator_apply(Traversable it, mixed function [, mixed params]) - Calls a function for every element in an iterator

pcre:

  • preg_last_error() - Returns the error code of the last regex execution

pgsql:

  • pg_field_table() - Returns the name of the table field belongs to, or table's oid if oid_only is TRUE

posix:

  • posix_initgroups() - Calculate the group access list for the user specified in name

gmp:

  • gmp_nextprime() - Finds next prime number

xmlwriter:

  • xmlwriter_full_end_element() - End current element - returns FALSE on error
  • xmlwriter_write_raw() - Write text - returns FALSE on error
  • xmlwriter_start_dtd_entity() - Create start DTD Entity - returns FALSE on error
  • xmlwriter_end_dtd_entity() - End current DTD Entity - returns FALSE on error
  • xmlwriter_write_dtd_entity() - Write full DTD Entity tag - returns FALSE on error

User Contributed Notes

There are no user contributed notes for this page.


New Methods

New methods were introduced in 5.2.0:

dom:

  • DOMDocument::registerNodeClass() - Register extended class used to create base node type
  • DOMElement::setIDAttribute() - Declares the attribute specified by name to be of type ID
  • DOMElement::setIDAttributeNode() - Declares the attribute specified by node to be of type ID
  • DOMElement::setIDAttributeNS() - Declares the attribute specified by local name and namespace URI to be of type ID
  • DOMNode::C14N()([bool exclusive [, bool with_comments [, array xpath [, array ns_prefixes]]]]) - Canonicalize nodes to a string
  • DOMNode::C14NFile()(string uri [, bool exclusive [, bool with_comments [, array xpath [, array ns_prefixes]]]]) - Canonicalize nodes to a file
  • DOMNode::getNodePath()() - Gets an xpath for a node

soap:

  • SoapServer::setObject()(object obj) - Sets object which will handle SOAP requests

spl:

  • int ArrayObject::asort()(void) - Sort the entries by values
  • int ArrayObject::ksort()(void) - Sort the entries by key
  • int ArrayObject::natcasesort()(void) - Sort the entries by key using case insensitive "natural order" algorithm.
  • int ArrayObject::natsort()(void) - Sort the entries by values using "natural order" algorithm.
  • int ArrayObject::uasort()(callback cmp_function) - Sort the entries by values using a user defined function
  • int ArrayObject::uksort()(callback cmp_function) - Sort the entries by key using a user defined function.
  • ArrayIterator AppendIterator::getArrayIterator()() - Get access to inner ArrayIterator
  • int AppendIterator::getIteratorIndex()() - Get index of iterator
  • bool CachingIterator::getCache()() - Return the cache
  • int CachingIterator::getFlags()() - Return the internal flags
  • bool CachingIterator::offsetExists()(mixed index) - Returns TRUE if the requested index exists
  • string CachingIterator::offsetGet()(mixed index) - Return the internal cache if used
  • void CachingIterator::offsetSet()(mixed index, mixed newval) - Set given index in cache
  • void CachingIterator::offsetUnset()(mixed index) - Unset given index in cache
  • void CachingIterator::setFlags()() - Set the internal flags
  • array("delimiter" =>, "enclosure" =>) SplFileObject::getCsvControl()(void) - Get the delimiter and enclosure character used in fgetcsv()
  • void SplFileObject::setCsvControl()([string delimiter = ',' [, string enclosure = '"']]) - Set the delimiter and enclosure character used in fgetcsv()

Tidy

  • tidyNode tidyNode::getParent()() - Returns the parent node of the current node (Added in PHP 5.2.2)

XMLReader

  • boolean XMLReader::setSchema() - Use W3C XSD schema to validate the document as it is processed. Activation is only possible before the first XMLReader::read()

zip:

  • ZipArchive::addEmptyDir()() - Creates an empty directory in the archive

User Contributed Notes

There are no user contributed notes for this page.


Removed Extensions

These extensions have been moved to PECL and are no longer part of the PHP distribution. The PECL package version of these extensions will be created according to user demand.

  • filePro
  • Hyperwave API

User Contributed Notes

There are no user contributed notes for this page.


New Extensions

The following are new extensions added (by default) as of PHP 5.2.0:

  • Filter - validates and filters data, and is designed for use with insecure data such as user input. This extension is enabled by default; the default mode RAW does not impact input data in any way.
  • JSON - implements the JavaScript Object Notation (JSON) data interchange format. This extension is enabled by default.
  • Zip - enables you to transparently read or write ZIP compressed archives and the files inside them.

User Contributed Notes

There are no user contributed notes for this page.


New Classes

The following classes were introduced in PHP 5.2.0:

  • DateTime
  • DateTimeZone
  • RegexIterator - extends FilterIterator; implements Iterator, Traversable, OuterIterator Constants:
    • RegexIterator::ALL_MATCHES
    • RegexIterator::GET_MATCH
    • RegexIterator::MATCH
    • RegexIterator::REPLACE
    • RegexIterator::SPLIT
    • RegexIterator::USE_KEY
    Properties:
    • public replacement
    Methods:
    • RegexIterator::__construct(Iterator it, string regex [, int mode [, int flags [, int preg_flags]]]) - Create an RegexIterator from another iterator and a regular expression
    • bool RegexIterator::accept() - Match (string)current() against regular expression
    • bool RegexIterator::getFlags() - Returns current operation flags
    • bool RegexIterator::getMode() - Returns current operation mode
    • bool RegexIterator::getPregFlags() - Returns current PREG flags (if in use or NULL)
    • bool RegexIterator::setFlags(int new_flags) - Set operation flags
    • bool RegexIterator::setMode(int new_mode) - Set new operation mode
    • bool RegexIterator::setPregFlags(int new_flags) - Set PREG flags
  • RecursiveRegexIterator Constants:
    • RecursiveRegexIterator::ALL_MATCHES
    • RecursiveRegexIterator::GET_MATCH
    • RecursiveRegexIterator::MATCH
    • RecursiveRegexIterator::REPLACE
    • RecursiveRegexIterator::SPLIT
    • RecursiveRegexIterator::USE_KEY
    Methods:
    • RecursiveRegexIterator::__construct(RecursiveIterator it, string regex [, int mode [, int flags [, int preg_flags]]]) - Create an RecursiveRegexIterator from another recursive iterator and a regular expression
    • RecursiveRegexIterator RecursiveRegexIterator::getChildren() - Return the inner iterator's children contained in a RecursiveRegexIterator
    • bool RecursiveRegexIterator::hasChildren() - Check whether the inner iterator's current element has children

User Contributed Notes

There are no user contributed notes for this page.


New Global Constants

PHP Core:

  • M_EULER
  • M_LNPI
  • M_SQRT3
  • M_SQRTPI
  • PATHINFO_FILENAME
  • PREG_BACKTRACK_LIMIT_ERROR
  • PREG_BAD_UTF8_ERROR
  • PREG_INTERNAL_ERROR
  • PREG_NO_ERROR
  • PREG_RECURSION_LIMIT_ERROR
  • UPLOAD_ERR_EXTENSION
  • STREAM_SHUT_RD
  • STREAM_SHUT_WR
  • STREAM_SHUT_RDWR

curl:

  • CURLE_FILESIZE_EXCEEDED
  • CURLE_FTP_SSL_FAILED
  • CURLE_LDAP_INVALID_URL
  • CURLFTPAUTH_DEFAULT
  • CURLFTPAUTH_SSL
  • CURLFTPAUTH_TLS
  • CURLFTPSSL_ALL
  • CURLFTPSSL_CONTROL
  • CURLFTPSSL_NONE
  • CURLFTPSSL_TRY
  • CURLOPT_FTP_SSL
  • CURLOPT_FTPSSLAUTH
  • CURLOPT_TCP_NODELAY Added in PHP 5.2.1.
  • CURLOPT_TIMEOUT_MS Added in PHP 5.2.3
  • CURLOPT_CONNECTTIMEOUT_MS Added in PHP 5.2.3

GMP:

  • GMP_VERSION Added in PHP 5.2.2.

ming:

  • SWFTEXTFIELD_USEFONT
  • SWFTEXTFIELD_AUTOSIZE
  • SWF_SOUND_NOT_COMPRESSED
  • SWF_SOUND_ADPCM_COMPRESSED
  • SWF_SOUND_MP3_COMPRESSED
  • SWF_SOUND_NOT_COMPRESSED_LE
  • SWF_SOUND_NELLY_COMPRESSED
  • SWF_SOUND_5KHZ
  • SWF_SOUND_11KHZ
  • SWF_SOUND_22KHZ
  • SWF_SOUND_44KHZ
  • SWF_SOUND_8BITS
  • SWF_SOUND_16BITS
  • SWF_SOUND_MONO
  • SWF_SOUND_STEREO

openssl:

  • OPENSSL_VERSION_NUMBER
  • OPENSSL_VERSION_TEXT

snmp:

  • SNMP_OID_OUTPUT_FULL
  • SNMP_OID_OUTPUT_NUMERIC

Semaphore:

  • MSG_EAGAIN
  • MSG_ENOMSG

User Contributed Notes

There are no user contributed notes for this page.


New Class Constants

pdo:

  • PDO::ATTR_DEFAULT_FETCH_MODE
  • PDO::FETCH_PROPS_LATE
  • PDO::FETCH_KEY_PAIR Fetches a 2 column result set into an associated array. (Added in PHP 5.2.3)

spl:

  • CachingIterator::FULL_CACHE
  • CachingIterator::TOSTRING_USE_INNER
  • SplFileObject::READ_AHEAD
  • SplFileObject::READ_CSV
  • SplFileObject::SKIP_EMPTY

User Contributed Notes

There are no user contributed notes for this page.


New INI Configuration Directives

New php.ini directives introduced in PHP 5.2.0:

  • allow_url_include This useful option makes it possible to differentiate between standard file operations on remote files, and the inclusion of remote files. While the former is usually desirable, the latter can be a security risk if used naively. Starting with PHP 5.2.0, you can allow remote file operations while disallowing the inclusion of remote files in local scripts. In fact, this is the default configuration.
  • pcre.backtrack_limit PCRE's backtracking limit.
  • pcre.recursion_limit PCRE's recursion limit. Please note that if you set this value to a high number you may consume all the available process stack and eventually crash PHP (due to reaching the stack size limit imposed by the Operating System).
  • session.cookie_httponly Marks the cookie as accessible only through the HTTP protocol. This means that the cookie won't be accessible by scripting languages, such as JavaScript. This setting can effectively help to reduce identity theft through XSS attacks (although it is not supported by all browsers).

New directives in PHP 5.2.2:

  • max_input_nesting_level Limits how deep input variables can be nested, default is 64.

User Contributed Notes

There are no user contributed notes for this page.


Error Reporting

Some of the existing E_ERROR conditions have been converted to something that can be caught with a user-defined error handler. If an E_RECOVERABLE_ERROR is not handled, it will behave in the same way as E_ERROR behaves in all versions of PHP. Errors of this type are logged as Catchable fatal error.

This change means that the value of the E_ALL error_reporting constant is now 6143, where the previous value was 2047. Because PHP constants have no meaning outside of PHP, in some cases the integer value is used instead so these will need to be adjusted. So for example by setting the error_reporting mode from either the httpd.conf or the .htaccess files, the value has to be changed accordingly. The same applies when the numeric values are used rather than the constants in PHP scripts.

As a side-effect of a change made to prevent duplicate error messages when track_errors is On, it is now necessary to return FALSE from user defined error handlers in order to populate $php_errormsg. This provides a fine-grain control over the levels of messages stored.

User Contributed Notes

There are no user contributed notes for this page.


Other Enhancements

  • Improved memory manager and increased default memory limit. The new memory manager allocates less memory and works faster than the previous incarnation. It allocates memory from the system in large blocks, and then manages the heap by itself. The memory_limit value in php.ini is checked, not for each emalloc() call (as before), but for actual blocks requested from the system. This means that memory_limit is far more accurate than it used to be, since the old memory manager didn't calculate all the memory overhead used by the malloc library. Thanks to this new-found accuracy memory usage may appear to have increased, although actually it has not. To accommodate this apparent increase, the default memory_limit setting was also increased - from 8 to 16 megabytes.
  • Added support for constructors in interfaces to force constructor signature checks in implementations. Starting with PHP 5.2.0, interfaces can have constructors. However, if you choose to declare a constructor in an interface, each class implementing that interface MUST include a constructor with a signature matching that of the base interface constructor. By 'signature' we mean the parameter and return type definitions, including any type hints and including whether the data is passed by reference or by value.

User Contributed Notes

There are no user contributed notes for this page.


Migrating from PHP 5.0.x to PHP 5.1.x

Table of Contents

  • Key PHP 5.1.x features
  • Changes in reference handling
  • Reading []
  • Integer values in function parameters
  • Class and object changes
  • Extensions
  • Date/time support
  • Changes in database support
  • Checking for E_STRICT

PHP 버전에 맞는 이행 가이드를 참고하십시오. 5.0.x, 5.2.x, 5.3.x, 5.4.x, 5.5.x and 5.6.x.

User Contributed Notes

There are no user contributed notes for this page.


Key PHP 5.1.x features

Some of the key features of PHP 5.1.x include:

  • A complete rewrite of date handling code, with improved timezone support.

  • Significant performance improvements compared to PHP 5.0.X.

  • PDO extension is now enabled by default.

  • Over 30 new functions in various extensions and built-in functionality.

  • Over 400 various bug fixes.

User Contributed Notes

There are no user contributed notes for this page.


Changes in reference handling

  • Overview

  • Code that worked under PHP 4.3.x, but now fails

  • Code that worked under PHP 4.3.x, but now throws an error

  • Code that failed under PHP 4.3.x, but now works

  • Code that should have worked under PHP 5.0.x

  • Warnings that came and went

Overview

From the PHP script writer's point of view, the change most likely to impact legacy code is in the way that references are handled in all PHP versions post-dating the PHP 4.4.0 release.

Until and including PHP 4.3, it was possible to send, assign or return variables by reference that should really be returned by value, such as a constant, a temporary value (e.g. the result of an expression), or the result of a function that had itself been returned by value, as here:

<?php
$foo = "123";

function return_value() {
    global $foo;
    return $foo;
}

$bar = &return_value();
?>

Although this code would usually work as expected under PHP 4.3, in the general case the result is undefined. The Zend Engine could not act correctly on these values as references. This bug could and did lead to various hard-to-reproduce memory corruption problems, particularly where the code base was large.

In PHP 4.4.0, PHP 5.0.4 and all subsequent PHP releases, the Engine was fixed to 'know' when the reference operation is being used on a value that should not be referenced. The actual value is now used in such cases, and a warning is emitted. The warning takes the form of an E_NOTICE in PHP 4.4.0 and up, and E_STRICT in PHP 5.0.4 and up.

Code that could potentially produce memory corruption can no longer do so. However, some legacy code might work differently as a result.

Code that worked under PHP 4.3, but now fails

<?php
function func(&$arraykey) {
    return $arraykey; // function returns by value!
}

$array = array('a', 'b', 'c');
foreach (array_keys($array) as $key) {
    $y = &func($array[$key]);
    $z[] =& $y;
}

var_dump($z);
?>
<

Running the above script under any version of PHP that pre-dates the reference fix would produce this output:

array(3) {
  [0]=>
  &string(1) "a"
  [1]=>
  &string(1) "b"
  [2]=>
  &string(1) "c"
}

Following the reference fix, the same code would result in:

array(3) {
  [0]=>
  &string(1) "c"
  [1]=>
  &string(1) "c"
  [2]=>
  &string(1) "c"
}

This is because, following the changes, func() assigns by value. The value of $y is re-assigned, and reference-binding is preserved from $z. Prior to the fix, the value was assigned by reference, leading $y to be re-bound on each assignment. The attempt to bind to a temporary value by reference was the cause of the memory corruption.

Such code can be made to work identically in both the pre-fix and the post-fix PHP versions. The signature of func() can be altered to return by reference, or the reference assignment can be removed from the result of func().

<?php
function func() {
    return 'function return';
}

$x = 'original value';
$y =& $x;
$y = &func();
echo $x;
?>

In PHP 4.3 $x would be 'original value', whereas after the changes it would be 'function return' - remember that where the function does not return by reference, the reference assignment is converted to a regular assignment. Again, this can be brought to a common base, either by forcing func() to return by reference or by eliminating the by-reference assignment.

Code that worked under PHP 4.3.x, but now throws an error

<?php
class Foo {

    function getThis() {
        return $this;
    }

    function destroyThis() {
        $baz =& $this->getThis();
    }
}

$bar = new Foo();
$bar->destroyThis();
var_dump($bar);
?>

In PHP 5.0.3, $bar evaluated to NULL instead of returning an object. That happened because getThis() returns by value, but the value here is assigned by reference. Although it now works in the expected way, this is actually invalid code which will throw an E_NOTICE under PHP 4.4 or an E_STRICT under PHP 5.0.4 and up.

Code that failed under PHP 4.3.x, but now works

<?php
function &f() {
    $x = "foo";
    var_dump($x);
    print "$x\n";
    return($a);
}

for ($i = 0; $i < 3; $i++) {
    $h = &f();
}
?>

In PHP 4.3 the third call to var_dump() produces NULL, due to the memory corruption caused by returning an uninitialized value by reference. This is valid code in PHP 5.0.4 and up, but threw errors in earlier releases of PHP.

<?php
$arr = array('a1' => array('alfa' => 'ok'));
$arr =& $arr['a1'];
echo '-'.$arr['alfa']."-\n";
?>

Until PHP 5.0.5, it wasn't possible to assign an array element by reference in this way. It now is.

Code that should have worked under PHP 5.0.x

There are a couple of instances of bugs reported under PHP 5.0 prior to the reference fixes which now 'work'. However, in both cases errors are thrown by PHP 5.1.x, because the code was invalid in the first place. Returning values by reference using self:: now works in the general case but throws an E_STRICT warning, and although your mileage may vary when assigning by reference to an overloaded object, you will still see an E_ERROR when you try it, even where the assignment itself appears to work.

Warnings that came and went

Nested calls to functions returning by reference are valid code under both PHP 4.3.x and PHP 5.1.x, but threw an unwarranted E_NOTICE or E_STRICT under the intervening PHP releases.

<?php
function & foo() {
    $var = 'ok';
    return $var;
}

function & bar() {
    return foo();
}

$a =& bar();
echo "$a\n";
?>

User Contributed Notes

There are no user contributed notes for this page.


Reading []

<?php
class XmlTest {

    function test_ref(&$test) {
        $test = "ok";
    }

    function test($test) { }

    function run() {
        $ar = array();
        $this->test_ref($ar[]);
        var_dump($ar);
        $this->test($ar[]);
    }
}

$o = new XmlTest();
$o->run();
?>

This should always have thrown a fatal E_ERROR, because [] cannot be used for reading in PHP. It is invalid code in PHP 4.4.2 and PHP 5.0.5 upward.

User Contributed Notes

There are no user contributed notes for this page.


Integer values in function parameters

With the advent of PHP 5.0.x, a new parameter parsing API was introduced which is used by a large number of PHP functions. In all versions of PHP between 5.0.x and 5.1.x, the handling of integer values was very strict and would reject non-well formed numeric values when a PHP function expected an integer. These checks have now been relaxed to support non-well formed numeric strings such as " 123" and "123 ", and will no longer fail as they did under PHP 5.0.x. However, to promote code safety and input validation, PHP functions will now emit an E_NOTICE when such strings are passed as integers.

User Contributed Notes

There are no user contributed notes for this page.


Class and object changes

  • instanceof, is_a(), is_subclass_of() and catch

  • Abstract private methods

  • Access modifiers in interfaces

  • Changes in inheritance rules

  • Class constants

instanceof, is_a(), is_subclass_of() and catch

In PHP 5.0, is_a() was deprecated and replaced by the instanceof operator. There were some issues with the initial implementation of instanceof, which relied on __autoload() to search for missing classes. If the class was not present, instanceof would throw a fatal E_ERROR due to the failure of __autoload() to discover that class. The same behaviour occurred in the catch operator and the is_subclass_of() function, for the same reason.

None of these functions or operators call __autoload() in PHP 5.1.x, and the class_exists() workarounds used in code written for PHP 5.0.x, while not problematic in any way, are no longer necessary.

Abstract private methods

Abstract private methods were supported between PHP 5.0.0 and PHP 5.0.4, but were then disallowed on the grounds that the behaviours of private and abstract are mutually exclusive.

Access modifiers in interfaces

Under PHP 5.0, function declarations in interfaces were treated in exactly the same way as function declarations in classes. This has not been the case since October 2004, at which point only the public access modifier was allowed in interface function declarations. Since April 2005 - which pre-dates the PHP 5.0b1 release - the static modifier has also been allowed. However, the protected and private modifiers will now throw an E_ERROR, as will abstract. Note that this change should not affect your existing code, as none of these modifiers makes sense in the context of interfaces anyway.

Changes in inheritance rules

Under PHP 5.0, it was possible to have a function declaration in a derived class that did not match the declaration of the same function in the base class, e.g.

This code will cause an E_STRICT error to be emitted under PHP 5.1.x.

<?php
class Base {
    function &return_by_ref() {
        $r = 1;
        return $r;
    }
}

class Derived extends Base {
    function return_by_ref() {
        return 1;
    }
}
?>

Class constants

Under PHP 5.0.x, the following code was valid:

Under PHP 5.1.x, redefinition of a class constant will throw a fatal E_ERROR.

<?php
class test {
    const foobar = 'foo';
    const foobar = 'bar';
}

?>

User Contributed Notes

There are no user contributed notes for this page.


Extensions

  • Extensions that are gone from the PHP core

  • Class constants in new PHP 5.1.x extensions

Extensions that are gone from the PHP core

One of the first things you're likely to notice when you download PHP 5.1.x is that several of the older extensions have disappeared. Those extensions that are still actively maintained are available in the PHP Extension Community Library (PECL), at » http://pecl.php.net/.

Removed extensions
Extension | Alternative/Status
ext/cpdf | pecl/pdflib
ext/dbx | pecl/dbx
ext/dio | pecl/dio
ext/fam | Not actively maintained
ext/ingres_ii | pecl/ingres
ext/ircg | Not actively maintained
ext/mcve | pecl/mcve
ext/mnogosearch | Not actively maintained
ext/oracle | ext/oci8 or ext/pdo_oci
ext/ovrimos | Not actively maintained
ext/pfpro | Not actively maintained
ext/w32api | » pecl/ffi
ext/yp | Not actively maintained
ext/activescript | » pecl/activescript

Modules in PECL that are not actively maintained (i.e. have not been supported for some time, have no active maintainer working on them currently, and do not have any PECL package releases), are still available in SVN at » http://svn.php.net/viewvc/pecl. However, unreleased PHP modules are by their nature unsupported, and your mileage may vary when attempting to install or use them.

Class constants in new PHP 5.1.x extensions

The Zend Engine 2.1 API allows extension developers to declare class constants in object oriented extensions. New extensions written for PHP 5.1.x, including SPL, PDO, XMLReader and date, have their constants in the format PDO::CLASS_CONSTANT rather than in the C format PDO_CLASS_CONSTANT in order to minimise pollution of the global namespace in PHP.

User Contributed Notes

There are no user contributed notes for this page.


Date/time support

Date/time support has been fully rewritten in PHP 5.1.x, and no longer uses the system settings to 'know' the timezone in operation. It will instead utilize, in the following order:

  • The timezone set using the date_default_timezone_set() function (if any)

  • The TZ environment variable (if non empty)

  • "magical" guess (if the operating system supports it)

  • If none of the above options succeeds, UTC

To ensure accuracy (and avoid an E_STRICT warning), you will need to define your timezone in your php.ini using the following format:

date.timezone = Europe/London

The supported timezones are listed, in this format, in the timezones appendix.

Also note that strtotime() now returns FALSE on failure, instead of -1.

User Contributed Notes

There are no user contributed notes for this page.


Changes in database support

  • PDO overview

  • Changes in MySQL support

  • Changes in SQLite support

PDO overview

PHP Data Objects (PDO) were introduced as a PECL extension under PHP 5.0, and became part of the core PHP distribution in PHP 5.1.x. The PDO extension provides a consistent interface for database access, and is used alongside database-specific PDO drivers. Each driver may also have database-specific functions of its own, but basic data access functionality such as issuing queries and fetching data is covered by PDO functions, using the driver named in PDO::__construct().

Note that the PDO extension, and its drivers, are intended to be built as shared extensions. This will enable straightforward driver upgrades from PECL, without forcing you to rebuild all of PHP.

At the point of the PHP 5.1.x release, PDO is more than ready for widespread testing and could be adopted in most situations. However, it is important to understand that PDO and its drivers are comparatively young and may be missing certain database-specific features; evaluate PDO carefully before you use it in new projects.

Legacy code will generally rely on the pre-existing database extensions, which are still maintained.

Changes in MySQL support

In PHP 4, MySQL 3 support was built-in. With the release of PHP 5.0 there were two MySQL extensions, named 'mysql' and 'mysqli', which were designed to support MySQL < 4.1 and MySQL 4.1 and up, respectively. With the introduction of PDO, which provides a very fast interface to all the database APIs supported by PHP, the PDO_MYSQL driver can support any of the current versions (MySQL 3, 4 or 5) in PHP code written for PDO, depending on the MySQL library version used during compilation. The older MySQL extensions remain in place for reasons of back compatibility, but are not enabled by default.

Changes in SQLite support

In PHP 5.0.x, SQLite 2 support was provided by the built-in sqlite extension, which was also available as a PECL extension in PHP 4.3 and PHP 4.4. With the introduction of PDO, the sqlite extension doubles up to act as a 'sqlite2' driver for PDO; it is due to this that the sqlite extension in PHP 5.1.x has a dependency upon the PDO extension.

PHP 5.1.x ships with a number of alternative interfaces to sqlite:

The sqlite extension provides the "classic" sqlite procedural/OO API that you may have used in prior versions of PHP. It also provides the PDO 'sqlite2' driver, which allows you to access legacy SQLite 2 databases using the PDO API.

PDO_SQLITE provides the 'sqlite' version 3 driver. SQLite version 3 is vastly superior to SQLite version 2, but the file formats of the two versions are not compatible.

If your SQLite-based project is already written and working against earlier PHP versions, then you can continue to use ext/sqlite without problems, but will need to explicitly enable both PDO and sqlite. New projects should use PDO and the 'sqlite' (version 3) driver, as this is faster than SQLite 2, has improved locking concurrency, and supports both prepared statements and binary columns natively.

You must enable PDO to use the SQLite extension. If you want to build the PDO extension as a shared extension, then the SQLite extension must also be built shared. The same holds true for any extension that provides a PDO driver

User Contributed Notes

There are no user contributed notes for this page.


Checking for E_STRICT

If you only have a single script to check, you can pick up E_STRICT errors using PHP's commandline lint facility:

php -d error_reporting=4095 -l script_to_check.php

For larger projects, the shell script below will achieve the same task:

#!/bin/sh

directory=$1

shift

# These extensions are checked
extensions="php inc"

check_file ()
{
  echo -ne "Doing PHP syntax check on $1 ..."

  # Options:
  ERRORS=`/www/php/bin/php -d display_errors=1 -d html_errors=0 -d error_prepend_string=" " -d error_append_string=" " -d error_reporting=4095 -l $1 | grep -v "No syntax errors detected"`

  if test -z "$ERRORS"; then
    echo -ne "OK."
  else
    echo -e "Errors found!\n$ERRORS"
  fi

  echo
}

# loop over remaining file args
for FILE in "$@" ; do
  for ext in $extensions; do
     if echo $FILE | grep "\.$ext$" > /dev/null; then
       if test -f $FILE; then
         check_file "$FILE"
       fi
     fi
  done
done

User Contributed Notes

designteam at casemumbai dot com 26-Nov-2008 07:39
Example usage:

<?php
//Errors will be printed on the screen with the following line
error_reporting(E_STRICT);

function change (&$var) {
  $var += 10;
}

$var = 1;
change(++$var);
echo "var=$var";
change($var = 5);
echo "var=$var";

?>

More examples:
http://en.wikipedia.org/wiki/E_STRICT


PHP 4에서 PHP 5.0.x로 이행

Table of Contents

  • PHP 5.0.x부터 바뀐 것들
  • 이전 버전과 호환되지 않는 변경점
  • CLI와 CGI
  • 설정 파일 이행
  • 새로운 함수들
  • 새로운 지시어
  • 데이터베이스
  • 새로운 객체 모델
  • 오류 보고

PHP 버전에 맞는 이행 가이드를 참고하십시오. 5.1.x, 5.2.x.

User Contributed Notes

There are no user contributed notes for this page.


PHP 5.0.x부터 바뀐 것들

PHP 5와 내장된 젠드 엔진 2는 PHP의 성능과 능력을 대폭 향상하면서, 기존의 코드를 가능한 그대로 사용할 수 있게 주의를 하였습니다. 그리하여, PHP 4의 코드를 PHP 5로 옮기는 일은 매우 쉽습니다. 기존 PHP 4 코드의 대부분은 아무런 변경 없이 실행할 수 있지만, 몇몇 차이점에 대해서 알고, 실제 환경에 적용할 때는 미리 테스트를 해 보아야 합니다.

User Contributed Notes

There are no user contributed notes for this page.


이전 버전과 호환되지 않는 변경점

기존 PHP 4 코드의 대부분은 변경 없이 작동하지만, 다음의 호환 되지 않는 변경점에는 주의해야 합니다:

  • 새 예약어가 있습니다.
  • strrpos()와 strripos()가 전체 문자열을 needle로 사용합니다.
  • 문자열 오프셋의 비정상적인 사용은 E_WARNING 대신 E_ERROR를 발생합니다. 비정상적인 사용의 예제: $str = 'abc'; unset($set[0]);
  • array_merge()가 배열만을 받아들이게 변경되었습니다. 배열이 아닌 값을 넘기면, 그러한 인수마다 E_WARNING을 발생합니다. 코드가 갑작스레 E_WARNING를 표시하기 시작할 수 있으므로 주의하십시오.
  • 더이상 Apahce2 SAPI에서 PATH_TRANSLATED 서버 변수를 자동으로 설정하지 않습니다. PHP 4에서는 아파치가 생성하지 않았을 때, SCRIPT_FILENAME 서버 변수와 동일하게 설정했었습니다. 이 변경은 » CGI 규격에 따르기 위한 점입니다. 자세한 정보는 » 버그 #23610를 확인하고, 매뉴얼에서 $_SERVER['PATH_TRANSLATED'] 설명을 참고하십시오. 이 내용은 PHP >= 4.3.2에도 영향이 있습니다.
  • Tokenizer 확장은 더 이상 T_ML_COMMENT 상수를 정의하지 않습니다. error_reporting을 E_ALL로 설정하면, PHP가 주의문을 생성합니다. T_ML_COMMENT는 사용되지 않았지만, PHP 4에서 정의되어 있었습니다. PHP 4와 PHP 5 둘 다, T_COMMENT 상수가 //와 /* */를 모두 인식합니다. 그러나 PHP 5에서 도입한, PHP가 처리하는 PHPDoc 스타일 주석 /** */은, T_DOC_COMMENT로 인식합니다.
  • variables_order가 "S"를 포함하고 있으면, $_SERVER는 argc와 argv를 가져야 합니다. 시스템이 $_SERVER를 생성하지 않게 설정하였다면, 물론 존재하지 않았습니다. 변경 후에는 variables_order 설정에 관계 없이, CLI 버전에서 argc와 argv를 사용할 수 있게 만듭니다. 또한, CLI 버전은 항상 전역의 $argc와 $argv도 가지게 되었습니다.
  • 프로퍼티가 없는 객체를 더 이상 "빈" 것으로 판단하지 않습니다.
  • 몇몇 경우에 클래스는 사용하기 전에 선언해야 합니다. 이는 PHP 5의 신기능(인터페이스 등)을 사용할 경우이며, 다른 경우엔 이전과 같습니다.
  • get_class(), get_parent_class(), get_class_methods()가 클래스/메쏘드 이름을 선언한 대로(대소문자 구분) 반환하므로, 이전의 동작(클래스/메쏘드 이름을 항상 소문자로 반환)에 의존하는 스크립트에 문제가 발생합니다. 해결법 중 하나는 모든 스크립트에서 위 함수들을 검색하여 strtolower()을 적용하는 방법입니다. 이 대소문자 구분은 마법 예약 상수 __CLASS__, __METHOD__, __FUNCTION__에도 적용됩니다. 반환되는 값은 정확히 선언한 대로(대소문자 구분) 입니다.
  • ip2long()에 잘못된 IP 주소를 인수로 넘겼을 때 이제 -1이 아닌 FALSE를 반환합니다.
  • 포함한 파일에 함수 정의가 있으면, return의 위치에 상관 없이 메인 파일에서 독립적으로 사용할 수 있습니다. 그 파일을 두번 포함하면, PHP 5는 함수가 이미 정의되어 있기에, 치명적인 오류를 발생합니다. PHP 4는 아무런 오류를 내지 않았었습니다. 포함하는 파일에서 포함 여부를 판단하고 리턴하는 대신, include_once를 사용하길 권장합니다.
  • include_once와 require_once은 윈도우에서 포함하는 파일의 경로를 정규화해서, A.php와 a.php는 한번만 포함하게 됩니다.

Example #1 strrpos()와 strripos()가 전체 문자열을 needle로 사용합니다.

<?php
var_dump(strrpos('ABCDEF','DEF')); //int(3)

var_dump(strrpos('ABCDEF','DAF')); //bool(false)
?>

Example #2 프로퍼티가 없는 객체를 더 이상 "빈" 것으로 판단하지 않습니다.

<?php
class test { }
$t = new test();

var_dump(empty($t)); // echo bool(false)

if ($t) {
      // 이 부분이 실행됩니다.
}
?>

Example #3 몇몇 경우에 클래스는 사용하기 전에 선언해야 합니다.

<?php

// 오류 없이 실행:
$a = new a();
class a {
}

// 오류 발생:
$a = new b();

interface c {
}
class b implements c {
}

?>

User Contributed Notes

michael dot darmousseh at gmail dot com 04-Feb-2010 02:53
Hack way to fix the array_merge problem so that it works with your existing php4 code

<?php
function array_merge5()
{
    $args = func_get_args();
    foreach($args as $key=>$arg)
        $args[$key] = (array) $arg;
    return call_user_func_array("array_merge", $args);
}
?>

just put it somewhere completely accessible to your codebase and change all of your calls to array_merge to array_merge5.
paul at oconnor-web dot net 10-Apr-2008 01:17
The __call function will also lowercase the method arguement:

<?php
   function __call($method, $args) {
     if ($method == 'Foo') {
       return true;
     } else {
       return false
     }
   }
?>

(= false)
Steven 20-Mar-2008 01:07
Three more that we discovered:

== 1. No longer can re-assign $this ==

The follwoing example works under PHP4 (it outputs "OK"), but produces a fatal error under PHP5:

<?php
  class a
  {
    var $text;
    function a() { $this->text = 'OK'; }
  }

  class b
  {
    var $text = 'NOT OK';
    function b() { $this = new a(); }
  }

  $myClass = new b();
  echo $myClass->text;
?>

== 2. No comments allowed after shorthand echo block ==

The follwoing example works under PHP4, but produces a sytax error under PHP5.

<?=//comment?>

== 3. Constructors return a reference as default ==

The follwoing example works under PHP4, but produces an E_NOTICE notice under PHP5.

<?php
  class MyClass { function MyClass() { echo('OK'); } }
  $myObj = null;
  $myObj &= new MyClass();
?>

Removing the ampersand solves the problem
Aggelos Orfanakos 08-Nov-2007 02:10
As with array_merge(), array_merge_recursive() returns NULL in PHP 5 if a non-array parameter is passed to it.
Sinured 10-Aug-2007 01:43
Not mentioned above: The PHP/FI 2 function style (old_function aka cfunction) is no longer supported as of PHP 5.
nami 26-Jul-2007 04:22
addition of the note on 07-Sep-2004 06:40

if you write down your code like this PHP5 will just work fine:

<?php
     $array_1 = array('key1'=>'oranges','key2'=>'apples');
     $array_2 = array('key3'=>'pears','key4'=>'tomatoes');
     $array_3 = array();
     $arr_gemerged = array_merge($array_1,$array_2,$array_3);
     echo "result:<br>";
     print_r($arr_gemerged);
     echo "<br>";
?>

---

so you have to declare array_3 as array() instead of NULL
kemal djakman 22-Jun-2007 03:11
The handling of accessing empty property of a class error has also changed:
<?php

class Foo {

    var $Bar = 'xxx';

    function F() {
        echo $this->$Bar;
    }
}

$Obj = new Foo();
$Obj->F();

?>

Notice the $ sign after object dereference opr?  $Bar is empty inside method F.   PHP4 would only generate a warning, PHP5 throws a fatal error
Amir Laher 29-Apr-2007 11:34
Some other things to be aware of:

some extra strictness:
* object members can no longer be accessed using array-member syntax
* function-calls with too many arguments will now cause errors.

Also, from PHP5.2, custom session handlers are affected:
* Best not to use global objects in custom session-handling functions. These would get destructed *before* the session is written (unless session_write_close() is called explicitly).
28-Feb-2006 11:03
is_a have been deprecated. You can simply replace all occurences with the new instanceOf operator, although this will break backwards-compatibility with php4.
dward . maidencreek.com 02-Nov-2004 09:54
Another change that we've had problems with while trying to use PHP4 code in PHP5 is how $this is carried across static method calls if they are not declared static in PHP5.  The main issue was that debug_backtrace() now shows the first class with -> instead of the second with :: in the backtrace element when the method in the second class was called statically (using ::) from a method in the first class.
07-Sep-2004 06:40
Be careful with array_merge in PHP5.1 not only a E_WARNING is thrown, but also the result is an empty array. So if you merge two select queries and the last one is empty you will end up with no array at all.

<?php
        $array_1 = array('key1'=>'oranges','key2'=>'apples');
        $array_2 = array('key3'=>'pears','key4'=>'tomatoes');
        $array_3 = null;
        $arr_gemerged = array_merge($array_1,$array_2,$array_3);
        print_r($arr_gemerged);
        echo "<br>";
?>

Result on php4:
Array ( [key1] => oranges [key2] => apples [key3] => pears [key4] => tomatoes )

Result on php5:
Warning: array_merge() [function.array-merge]: Argument #3 is not an array in /removed/by/danbrown/for/manual.php on line 7
john.g 26-Aug-2004 07:45
PATH_TRANSLATED is handy when using Apache's ModRewrite engine, as it gives you the name and path of the resulting file rather than the one that was requested by the user. Since PHP 5.0 and Apache 2 no longer support this variable, I created a workaround by adding an environment variable to my ModRewrite command:

Original:
RewriteRule ^/test/(.*)\.php(.*) /test/prefix_$1.php$2

Adjusted:
RewriteRule ^/test/(.*)\.php(.*) /test/prefix_$1.php$2 [E=TARGET:prefix_$1.php]

I could then find out the resulting file name through the super global $_ENV, for instance:

<?php
echo "The actual filename is ".$_ENV['REDIRECT_TARGET'];
?>

Note: The "REDIRECT_" prefix appears to be allocated automatically by ModRewrite.
cyberhorse 05-Aug-2004 07:07
clone() is a php function now.

if you create a subclass, it no longer uses samename methods in superclass as a constructor.


CLI와 CGI

PHP 5에서 CLI와 CGI 파일명이 변경되었습니다. PHP 5에서, CGI 버전은 php-cgi.exe로 변경되었고 (이전에는 php.exe) CLI 버전이 주 디렉토리에 위치합니다. (이전에는 cli/php.exe)

PHP 5에서, 새로운 모드가 존재합니다: php-win.exe. 이는 CLI 버전과 동일하지만, php-win은 아무것도 출력하지 않으며, 그렇기에 콘솔을 생성하지 않습니다. ("도스창"이 화면에 나타나지 않습니다) 이는 php-gtk와 비슷합니다.

PHP 5에서 CLI 버전은 어떠한 php.ini 지시어 설정과 관계 없이 항상 전역의 $argv와 $argc 변수를 가집니다. 심지어 register_argc_argv를 off로 설정해도 CLI에는 영향이 없습니다.

명령줄 레퍼런스를 참고하십시오.

User Contributed Notes

There are no user contributed notes for this page.


설정 파일 이행

ISAPI 모듈들의 이름이 php4xxx에서 php5xxx로 바뀌었기에, 설정 파일에 약간의 변경이 필요합니다. 또한 CLI와 CGI 파일명도 바뀌었으며, 이에 대한 자세한 내용은 해당 섹션을 참고하십시오.

아파치 설정 파일을 이행하는 것은 매우 쉽습니다. 아래의 예제를 보고 필요한 변경을 하십시오:

Example #1 PHP 5로 아파치 설정 파일을 이행하기.

# 이 줄을:
LoadModule php4_module /php/sapi/php4apache2.dll

# 이렇게 변경합니다:
LoadModule php5_module /php/php5apache2.dll

웹서버가 PHP를 CGI 모드로 실행한다면, CGI 버전의 이름이 php.exe에서 php-cgi.exe로 바뀐 점에 주의해야 합니다. 아파치에서는 다음과 같이 해야 합니다:

Example #2 PHP CGI 모드를 위한 아파치 설정 파일 이행하기.

# 이 줄을:
      Action application/x-httpd-php "/php/php.exe"

      # 이렇게 변경합니다:
      Action application/x-httpd-php "/php/php-cgi.exe"

다른 웹서버에서도 CGI나 ISAPI 모듈의 파일명을 변경해야 합니다.

User Contributed Notes

There are no user contributed notes for this page.


새로운 함수들

PHP 5에서 새로운 함수가 추가되었습니다. 그 목록입니다:

배열:

  • array_combine() - 하나의 배열을 키로, 하나의 배열을 값으로 가지는 배열을 생성합니다.
  • array_diff_uassoc() - 사용자가 제공하는 콜백 함수를 통한 추가적인 인덱스 확인을 포함하여 배열의 차이를 계산합니다.
  • array_udiff() - 데이터 비교에 콜백 함수를 사용하여 배열의 차이를 계산합니다.
  • array_udiff_assoc() - 추가적인 인덱스 확인을 통한 배열의 차이를 계산합니다. 데이터 비교는 콜백 함수를 사용합니다.
  • array_udiff_uassoc() - 추가적인 인덱스 확인을 통한 배열의 차이를 계산합니다. 데이터 비교는 콜백 함수를 사용합니다. 인덱스 확인도 콜백 함수를 사용합니다.
  • array_walk_recursive() - 배열의 모든 원소에 사용자 함수를 적용합니다.
  • array_uintersect_assoc() - 배열 교집합을 추가적인 인덱스 확인을 통해서 계산합니다. 데이터는 콜백 함수를 사용하여 비교됩니다.
  • array_uintersect_uassoc() - 배열 교집합을 추가적인 인덱스 확인을 통해서 계산합니다. 데이터와 인덱스 모두 별개의 콜백 함수를 사용하여 비교합니다.
  • array_uintersect() - 배열 교집합을 계산합니다. 데이터는 콜백 함수를 사용하여 비교합니다.

인터베이스:

  • ibase_affected_rows() - 이전의 질의에 영향을 받은 열의 수를 반환합니다.
  • ibase_backup() - 서비스 관리자에 백업 작업을 실행시키고, 바로 돌아옵니다.
  • ibase_commit_ret() - 트랜젝션을 열어둔 채로 작업을 기록합니다.
  • ibase_db_info() - 데이터베이스에 관한 통계를 요청합니다.
  • ibase_drop_db() - 데이터베이스를 지웁니다.
  • ibase_errcode() - 오류 코드를 반환합니다.
  • ibase_free_event_handler() - 등록되어 있는 이벤트 핸들러를 취소합니다.
  • ibase_gen_id() - Named generator를 증가하고 새로운 값을 반환합니다.
  • ibase_maintain_db() - 데이터베이스 서버에 관리 명령을 실행합니다.
  • ibase_name_result() - 이름을 결과셋에 할당합니다.
  • ibase_num_params() - 준비한 질의문의 인수 수를 반환합니다.
  • ibase_param_info() - 준비한 질의문의 인수에 대한 정보를 반환합니다.
  • ibase_restore() - 서비스 관리자에 복구 작업을 실행시키고, 바로 돌아옵니다.
  • ibase_rollback_ret() - 트랜젝션 내용은 유지한 채로 작업을 되돌립니다.
  • ibase_server_info() - 데이터베이스 서버에 관한 통계를 요청합니다.
  • ibase_service_attach() - 서비스 관리자에 접속합니다.
  • ibase_service_detach() - 서비스 관리자 연결을 끊습니다.
  • ibase_set_event_handler() - 이벤트가 발생했을 때 호출할 콜백 함수를 등록합니다.
  • ibase_wait_event() - 데이터베이스에 의해 이벤트가 발생하기를 기다립니다.

iconv:

  • iconv_mime_decode() - MIME 헤더 필드를 디코드합니다.
  • iconv_mime_decode_headers() - 복수의 MIME 헤더 필드를 한번에 디코드합니다.
  • iconv_mime_encode() - MIME 헤더 필드를 작성합니다.
  • iconv_strlen() - 문자열의 문자 수를 반환합니다.
  • iconv_strpos() - haystack에서 needle이 처음으로 나타나는 위치를 찾습니다.
  • iconv_strrpos() - haystack에서 needle이 마지막으로 나타나는 위치를 찾습니다.
  • iconv_substr() - 문자열의 부분을 잘라냅니다.

스트림:

  • stream_copy_to_stream() - 한 스트림의 데이터를 다른 스트림으로 복제합니다.
  • stream_get_line() - 스트림 자원에서 주어진 구분자로 나타나는 열을 얻습니다.
  • stream_socket_accept() - stream_socket_server()로 생성한 소켓에 연결을 받아들입니다.
  • stream_socket_client() - 인터넷이나 유닉스 도메인 소켓 연결을 엽니다.
  • stream_socket_get_name() - 로컬이나 원격 소켓의 이름을 얻습니다.
  • stream_socket_recvfrom() - 연결 여부에 관계 없이, 소켓에서 데이터를 받습니다.
  • stream_socket_sendto() - 연결 여부에 관계 없이, 소켓에 메세지를 보냅니다.
  • stream_socket_server() - 인터넷이나 유닉스 도메인 소켓을 생성합니다.

날짜와 시간 관련:

  • idate() - 지역 날짜/시간을 정수로 바꿉니다.
  • date_sunset() - 주어진 날짜와 위치의 일몰 시간.
  • date_sunrise() - 주어진 날짜와 위치의 일출 시간.
  • time_nanosleep() - 초와 나노초의 수만큼 지연합니다.

문자열:

  • str_split() - 문자열을 배열로 변환합니다.
  • strpbrk() - 문자열에서 문자 세트에 있는 문자를 검색합니다.
  • substr_compare() - 두 문자열을 offset부터 length까지 (대소문자 구분 없이:옵션) 이진 안전하게 비교합니다.

기타:

  • convert_uudecode() - uuencode된 문자열을 디코드합니다.
  • convert_uuencode() - 문자열을 uuencode합니다.
  • curl_copy_handle() - 모든 속성과 함께 cURL 핸들을 복제합니다.
  • dba_key_split() - 문자열 표현에서 배열 표현으로 키를 분리합니다.
  • dbase_get_header_info() - dBase 데이터베이스의 헤더 정보를 얻습니다.
  • dbx_fetch_row() - DBX_RESULT_UNBUFFERED 플래그를 설정한 질의 결과에서 열을 가져옵니다.
  • fbsql_set_password() - 주어진 사용자의 암호를 변경합니다.
  • file_put_contents() - 문자열을 파일로 씁니다.
  • ftp_alloc() - 업로드할 파일의 공간을 할당합니다.
  • get_declared_interfaces() - 정의된 모든 인터페이스를 배열로 반환합니다.
  • get_headers() - HTTP 요청의 응답으로 서버가 보낸 모든 헤더를 가져옵니다.
  • headers_list() - 보내진 (혹은 보내질) 응답 해더의 목록을 반환합니다.
  • http_build_query() - URL-encode한 쿼리 문자열을 생성합니다.
  • image_type_to_extension() -
getimagesize(),
exif_read_data(),
exif_thumbnail(),
exif_imagetype()이 반환한 그림 형식으로 파일
확장자를 얻습니다.
  • imagefilter() - 사용자 인수를 사용하여 그림에 필터를 적용합니다.
  • imap_getacl() - 주어진 편지함을 위한 ACL을 얻습니다.
  • ldap_sasl_bind() - SASL을 사용하여 LDAP 디렉토리에 바인드합니다.
  • mb_list_encodings() - 지원하는 모든 인코딩을 배열로 반환합니다.
  • pcntl_getpriority() - 어떠한 프로세스의 우선권을 얻습니다.
  • pcntl_wait() - waitpid() 시스템 호출에 따라서 자식 프로세스의 상태를 얻거나 기다립니다.
  • pg_version() - 클라이언트, 프로토콜, (가능할 경우) 서버의 버전을 배열로 반환합니다.
  • php_check_syntax() - 지정한 파일의 문법을 확인합니다.
  • php_strip_whitespace() - 주석과 공백을 제거한 소스를 반환합니다.
  • proc_nice() - 현재 프로세스의 우선권을 변경합니다.
  • pspell_config_data_dir() - 언어 데이터 파일의 위치를 변경합니다.
  • pspell_config_dict_dir() - 주 단어 목록의 위치를 변경합니다.
  • setrawcookie() - 값에 URL 인코딩을 하지 않은 쿠키를 전송합니다.
  • scandir() - 지정한 경로 안에 파일과 디렉토리 목록.
  • snmp_read_mib() - MIB 파일을 읽어서 활성 MIB 트리에 넣습니다.
  • sqlite_fetch_column_types() - 특정 테이블의 컬럼형을 배열로 반환합니다.

Note:

Tidy 확장도 API가 완전히 바뀌었습니다.

User Contributed Notes

There are no user contributed notes for this page.


새로운 지시어

PHP 5는 몇몇 새로운 php.ini 지시어를 가집니다. 그 목록입니다:

  • mail.force_extra_paramaters - 지정한 인수를 센드메일 바이너리에 추가적인 인수로 전달하도록 강제합니다. 이 인수는 항상 mail()의 다섯번째 인수를 교체합니다. 안전 모드에서도 동작합니다.
  • register_long_arrays - PHP가 배제된 긴 형태의 $HTTP_*_VARS를 등록할 지를 지정합니다.
  • session.hash_function - 해시 함수를 선택합니다. (MD5나 SHA-1)
  • session.hash_bits_per_character - 바이너리 해시 데이터를 읽을 수 있는 형태로 변환 할 때, 각 문자당 몇 비트를 저장할지 지정합니다 (4에서 6)
  • zend.ze1_compatibility_mode - 젠드 엔진 1 (PHP 4) 호환 모드를 활성화합니다.

User Contributed Notes

There are no user contributed notes for this page.


데이터베이스

PHP 5에서 데이터베이스에 관한 변경이 있습니다. (MySQL과 SQLite)

PHP 5에서, 라이센스 문제 등으로 인해 MySQL 클라이언트 라이브러리를 포함하지 않습니다. 자세한 정보는 FAQ 내용을 읽어보십시오.

또한, MySQL 4.1 이상에서 작동하도록 설계된 새로운 확장 모듈, MySQLi (향상된 MySQL)이 추가되었습니다.

PHP 5부터, SQLite 확장 모듈이 PHP에 포함됩니다. SQLite는 임베디드 SQL 데이터베이스 엔진으로, (MySQL이나 PostgreSQL처럼) 커다란 데이터베이스 서버에 접속하기 위한 클라이언트 라이브러리가 아닙니다. SQLite 라이브러리는 디스크의 데이터베이스 파일을 직접 읽고 씁니다.

User Contributed Notes

There are no user contributed notes for this page.


새로운 객체 모델

PHP 5에서 새로운 객체 모델이 존재합니다. PHP의 객체 조작은 완전히 새로 쓰여졌으며, 더 나은 성능과 많은 기능을 허용합니다. 이전 버전의 PHP에서, 객체는 기본형처럼(정수형이나 문자열 같이) 다루었습니다. 이런 방식의 단점은 변수가 할당되거나, 메쏘드의 인수로 넘겨질 때 모든 객체를 복사해야 했습니다. 새로운 방식으로, 객체는 값이 아닌 핸들을 참조하게 됩니다. (핸들을 객체 식별자로 생각할 수도 있습니다)

많은 PHP 프로그래머는 이전 객체 모델의 복사에 대해서 크게 신경 쓰지 않았기 때문에, 대부분의 PHP 어플리케이션은 매우 작은 변경만으로도 작동합니다.

새 객체 모델은 언어 레퍼런스에 문서가 있습니다.

PHP 5에서 클래스 이름을 가진 함수는 같은 클래스 안에서 정의되었을 때에만 생성자로 호출됩니다. PHP 4에서는 정의된 클래스가 부모 클래스였어도 호출됩니다.

PHP 4와 호환을 위해서 zend.ze1_compatibility_mode 지시어를 참고하십시오.

User Contributed Notes

zzo38 07-Oct-2007 05:06
You should be able to clone a object in compatibility of PHP4,PHP5 with:
<?php
 $x=unserialize(serialize($y));
?>
bdas at premiergroup dot uk dot com 25-May-2007 04:49
Since PHP5 upgraded PHP to an OOP language, they CHANGED the metaphor so that when you copy an object, you just get a pointer to it (as in C# and Java) and so therefore they needed to make a way to CLONE objects as well in case you need a REAL copy of the object.

Most cases, clone is not needed, simply because a real copy of an object is usually not mandatory.  In special cases, object cloning can be used to save time in porting.
quinn at strangecode dot com 19-May-2006 05:53
Here is another possible solution for migrating code to php 5 when using $this = 'something' reassignments. In my case, I had several classes  with methods that were self-instantiating with static calls. I was able to simply use a different variable: I changed $this to $_this and it worked the same because I copied an instance of the original object by reference using an instantiation factory method:

class DB {
    function &getInstance()
    {
        static $instance = null;

        if ($instance === null) {
            $instance = new DB();
        }

        return $instance;
    }
    ...

In every method needing access to this object I assigned it to a temporary variable by reference:

    function doSomething ()
    {
        $_this =& DB::getInstance();

        $_this->doSomethingElse();
        $_this->param['id'] = 123;
    }

Which allows method calls or saving data back to the original object.

I originally created classes like this so I didn't need to keep track of instantiations or global objects. I could just call DB::doSomething() and the object is created dynamically or referenced from an already existing object.


오류 보고

PHP 5부터 새 오류 보고 상수 E_STRICT가 2048 값으로 나왔습니다. 이는 코드에 대해서 상호 운용성과 상위 호환을 전제로 한 런타임 PHP 제안을 활성화합니다. 이로써 최신의 좋은 권고 방식으로 코딩할 수 있습니다. 예를 들면, STRICT 메세지는 배제된 함수에 대해서 경고를 해줍니다.

Note: E_ALL은 E_STRICT를 포함하지 않으므로, 기본값으로 활성화 되어 있지 않습니다.

User Contributed Notes

matthew dot flaschen at gatech dot edu 20-Jun-2013 06:52
E_ALL includes E_STRICT in 5.4.0 and up.
Anonymous 13-Aug-2012 10:28
According to the notes in the php.ini file, E_ALL *does* include E_STRICT; to exclude it you would use E_ALL & ~E_STRICT

This may be useful for someone who has upgraded and kept their old php.ini
Anonymous 31-Mar-2008 02:52
To enable full error reporting (recommended for development boxes) use:

use error_reporting(E_ALL | E_STRICT);

or in php.ini:

error_reporting  =  E_ALL | E_STRICT


Classes and Objects (PHP 4)

Table of Contents

  • class
  • extends
  • Constructors
  • Scope Resolution Operator (::)
  • parent
  • Serializing objects - objects in sessions
  • The magic functions __sleep and __wakeup
  • References inside the constructor
  • Comparing objects

User Contributed Notes

There are no user contributed notes for this page.


class

A class is a collection of variables and functions working with these variables. Variables are defined by var and functions by function. A class is defined using the following syntax:

<?php
class Cart {
    var $items;  // Items in our shopping cart

    // Add $num articles of $artnr to the cart

    function add_item($artnr, $num) {
        $this->items[$artnr] += $num;
    }

    // Take $num articles of $artnr out of the cart

    function remove_item($artnr, $num) {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } elseif ($this->items[$artnr] == $num) {
            unset($this->items[$artnr]);
            return true;
        } else {
            return false;
        }
    }
}
?>

This defines a class named Cart that consists of an associative array of articles in the cart and two functions to add and remove items from this cart.

Warning

You can NOT break up a class definition into multiple files. You also can NOT break a class definition into multiple PHP blocks, unless the break is within a method declaration. The following will not work:

<?php
class test {
?>
<?php
    function test() {
        print 'OK';
    }
}
?>

However, the following is allowed:

<?php
class test {
    function test() {
        ?>
        <?php
        print 'OK';
    }
}
?>

The following cautionary notes are valid for PHP 4.

Caution

The name stdClass is used internally by Zend and is reserved. You cannot have a class named stdClass in PHP.

Caution

The function names __sleep and __wakeup are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them. See below for more information.

Caution

PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).

<?php
class Cart {
    /* None of these will work in PHP 4. */
    var $todays_date = date("Y-m-d");
    var $name = $firstname;
    var $owner = 'Fred ' . 'Jones';
    /* Arrays containing constant values will, though. */
    var $items = array("VCR", "TV");
}

/* This is how it should be done. */
class Cart {
    var $todays_date;
    var $name;
    var $owner;
    var $items = array("VCR", "TV");

    function Cart() {
        $this->todays_date = date("Y-m-d");
        $this->name = $GLOBALS['firstname'];
        /* etc. . . */
    }
}
?>

Classes are types, that is, they are blueprints for actual variables. You have to create a variable of the desired type with the new operator.

<?php
$cart = new Cart;
$cart->add_item("10", 1);

$another_cart = new Cart;
$another_cart->add_item("0815", 3);
?>

This creates the objects $cart and $another_cart, both of the class Cart. The function add_item() of the $cart object is being called to add 1 item of article number 10 to the $cart. 3 items of article number 0815 are being added to $another_cart.

Both, $cart and $another_cart, have functions add_item(), remove_item() and a variable items. These are distinct functions and variables. You can think of the objects as something similar to directories in a filesystem. In a filesystem you can have two different files README.TXT, as long as they are in different directories. Just like with directories where you'll have to type the full pathname in order to reach each file from the toplevel directory, you have to specify the complete name of the function you want to call: in PHP terms, the toplevel directory would be the global namespace, and the pathname separator would be ->. Thus, the names $cart->items and $another_cart->items name two different variables. Note that the variable is named $cart->items, not $cart->$items, that is, a variable name in PHP has only a single dollar sign ($).

<?php
// correct, single $
$cart->items = array("10" => 1);

// invalid, because $cart->$items becomes $cart->""
$cart->$items = array("10" => 1);

// correct, but may or may not be what was intended:
// $cart->$myvar becomes $cart->items
$myvar = 'items';
$cart->$myvar = array("10" => 1);
?>

Within a class definition, you do not know under which name the object will be accessible in your program: at the time the Cart class was written, it was unknown whether the object would be named $cart, $another_cart, or something else later. Thus, you cannot write $cart->items within the Cart class itself. Instead, in order to be able to access its own functions and variables from within a class, one can use the pseudo-variable $this which can be read as 'my own' or 'current object'. Thus, '$this->items[$artnr] += $num' can be read as 'add $num to the $artnr counter of my own items array' or 'add $num to the $artnr counter of the items array within the current object'.

Note:

The $this pseudo-variable is not usually defined if the method in which it is hosted is called statically. This is not, however, a strict rule: $this is defined if a method is called statically from within another object. In this case, the value of $this is that of the calling object. This is illustrated in the following example:

<?php
class A
{
    function foo()
    {
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")\n";
        } else {
            echo "\$this is not defined.\n";
        }
    }
}

class B
{
    function bar()
    {
        A::foo();
    }
}

$a = new A();
$a->foo();
A::foo();
$b = new B();
$b->bar();
B::bar();
?>

위 예제의 출력:

$this is defined (a)
$this is not defined.
$this is defined (b)
$this is not defined.

Note:

There are some nice functions to handle classes and objects. You might want to take a look at the Class/Object Functions.

User Contributed Notes

ender911t at gmail dot com 22-Sep-2015 06:19
Overrides in child are called when calling a function from the parent

<?PHP
class Par
{
   var $test;
   function __construct($in)
   {
      $this->test = $in;
   }

   function getTest()
   {
      return $this->test;
   }

   function runTest()
   {
      return $this->getTest();
   }
}

class Chi extends Par
{
   var $i;
   function __construct($in)
   {
      $this->i = $in;
      parent::__construct($in."X");
   }

   function getTest()
   {
      return $this->i;
   }

   function runParentTestA()
   {
      return parent::runTest();
   }

   function runParentTestB()
   {
      return parent::getTest();
   }
}

$p = new Par("par");
$c = new Chi("chi");

echo $p->runTest()." == par";
echo $c->runTest()." == chi";
echo $c->runParentTestA()." == chi";
echo $c->runParentTestB()." == chiX";
?>
anthonymccuen at yahoo dot com 22-Mar-2013 12:57
You can also use "var" to declare multiple public instance variables like in C:

int main(int *argc, const char *argv[]) {
    int a, b, c, d, e, f;

    a = 50;
    // blah blah blah code here
    // ...

    return 0;
}

Same as PHP:

<?php
class MyClass {
    var $myvar, $anotherVar, $something, $str;
}
?>
masonswolf at gmail dot com 17-May-2012 08:38
You can invoke a class above its definitions in some cases, but there are very important exceptions to this behavior. If your class extends another class, and the interpreter doesn't see the extended class first, then it won't be added to the symbol table until the code has stepped through it in runtime. As a result, if you try to invoke it above the definition, you'll get a "class not found" fatal error.

And those suck.

So, to provide an example, the following will output a fatal error
<?php
Bar::bar();
exit;

class Bar extends Foo { } //will fatal because Foo not seen yet

class Foo {
    static function bar() { echo 'yep, this is Foo::bar'; }
}
?>

However, THIS code will work just fine:
<?php
Bar::bar();
exit;

class Foo {
    static function bar() { echo 'yep, this is Foo::bar'; }
}

class Bar extends Foo { } //will work because Foo came first
?>

Notice that if you include a file containing the class you will extend, and then extend the class in the same file as its invocation, you can also get the class not found fatal. This happens even if the 'include' call happens before the child class's definition.

Eg. the following will also output a fatal error
<?php
include_once('file_with_foo.php');

Bar::bar();
exit;

class Bar extends Foo { }
?>

Hope that clarifies things.
pov at fingerprint.fr 09-Dec-2011 10:22
I just discovered a behaviour that has to be mentioned.

Let's consider the following file

<?php
Foo::bar();
exit;

class Foo {
  function bar() { echo 'yep, this is Foo::bar'; }
}
?>

You may expect :
- an error on the Foo::bar call, because the class is not defined
- removing the code after the exit without side-effect

But it won't, and it will output the string !
It seems every class definition is executed at parse-time.

But without errors; if your file is
<?php
Foo::bar();
exit;

class Foo {
  function bar() { echo 'yep, this is Foo::bar'; }
}
class Foo {
  function bar() { echo 'yep, this is another Foo::bar'; }
}
?>
it will still output the first string, not doing any "Already exiting class" error !

So if you intend preventing a double include by doing in an included file :
<?php
if (class_exists('Foo')) { return; }

class Foo {
  static function register() { ... }
}
Foo::register();
?>
the Foo::register method won't be called !

Very strange development choice; I suppose it is due to ascending compatibility.
Hope it can help...
boukeversteegh at gmail dot com 27-Aug-2010 07:28
You can also instantiate objects using variables containing the name of the class:

<?php

$type = 'Foo';
$foo   = new $type();

print_r( $foo );
/* Prints:
Foo Object
(
)
*/
?>

The following works as well:
<?php

$somefoo    = new Foo();
$anotherfoo = new $somefoo();

# so you don't have to do:
$anotherfoo = new get_class( $somefoo)();
?>


extends

Often you need classes with similar variables and functions to another existing class. In fact, it is good practice to define a generic class which can be used in all your projects and adapt this class for the needs of each of your specific projects. To facilitate this, classes can be extensions of other classes. The extended or derived class has all variables and functions of the base class (this is called 'inheritance' despite the fact that nobody died) and what you add in the extended definition. It is not possible to subtract from a class, that is, to undefine any existing functions or variables. An extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword 'extends'.

<?php
class Named_Cart extends Cart {
    var $owner;

    function set_owner ($name) {
        $this->owner = $name;
    }
}
?>

This defines a class Named_Cart that has all variables and functions of Cart plus an additional variable $owner and an additional function set_owner(). You create a named cart the usual way and can now set and get the carts owner. You can still use normal cart functions on named carts:

<?php
$ncart = new Named_Cart;    // Create a named cart
$ncart->set_owner("kris");  // Name that cart
print $ncart->owner;        // print the cart owners name
$ncart->add_item("10", 1);  // (inherited functionality from cart)
?>

This is also called a "parent-child" relationship. You create a class, parent, and use extends to create a new class based on the parent class: the child class. You can even use this new child class and create another class based on this child class.

Note:

Classes must be defined before they are used! If you want the class Named_Cart to extend the class Cart, you will have to define the class Cart first. If you want to create another class called Yellow_named_cart based on the class Named_Cart you have to define Named_Cart first. To make it short: the order in which the classes are defined is important.

User Contributed Notes

vittorio dot zamparella at gmail dot com 31-Oct-2015 01:58
extending a PHP class with an included file "delays" the compilation:

<?php
require 'MySuperclass.php';
var_dump(class_exists('MyFirstClass')); // FALSE
class MyFirstClass extends MySuperclass {}
    // the "compilation" of this is delayed till execution
var_dump(class_exists('MyFirstClass')); // TRUE

class AnotherSuperclass { /* nothing */}
var_dump(class_exists('MySecondClass'));
    // returns TRUE even if it's called "before" definition
class MySecondClass extends AnotherSuperclass {}
?>

see also the examples at http://php.net/manual/en/function.class-exists.php

discoverd this when autoinitializing some static classes
Adel Abula adel at magebinary dot com 04-May-2015 10:56
You can always place the universal variables and functions into the deepest class. Then call or rewrite it in child classes. See example.

<?php

class ClassA
{
    public $var;
    function __construct()
    {
        $this->setVar();
        $this->getVar();
    }

    protected function getVar()
    {
        echo $this->var;
    }

    public function setVar()
    {
        $this->var = 1000;
    }
}

class ClassAChildA extends ClassA
{

    function __construct()
    {
        $this->setVar();
        $this->getVar();
    }

    public function setVar()
    {
        $this->var = 1001;
    }
}

class ClassAChildB extends ClassA
{
    function __construct()
    {
        $this->var = 1002;
        $this->getVar();
    }

    public function getVar()
    {
        echo $this->var;
    }
}
//prints 1000
$parent = new ClassA();

//prints 1001
$childA = new ClassAChildA();

//prints 1002
$childB = new ClassAChildB();
?>
geilt at esotech dot org 10-Feb-2012 03:27
I have found this the best way to grab a parents variables and such (that I love to set in the constructor, like standardized table names, etc.). I just call the parents constructor in the child constructor, then change variables inside the child constructor that needs to be customized for that class, or just leave it alone to make sure I understand I am using the parents constructor in the child constructor. (it's a good note and practice of mine.).

<?php
class MyParentClass
{
    $this->table_one = "my_table";
    $this->table_two = "variable_table";
}

class MyChildClass extends MyParentClass
{
    public function construct()
    {
        parent::__construct();

        $this->table_two = "newly_defined_table";

    }
    public function action()
   {
      $table1 = $this->table_one;
      $table2 = $this->table_two;

      return $table1 . " - " . $table2;
   }
}

$myChildClass = new MyChildClass;

echo $myChildClass->action();

//Displays my_table - newly_defined_table
?>
datahell at elxis dot org 23-Dec-2010 08:32
If you have a class that extends an other and both classes have a function with the same name then visibility to both classes should be the same, else you will have a fatal error.

An other interested part in the example bellow is that if visibility of the showName method is private then $this->name() will execute the showName method on class test. If it is public or protected it will execute method showName on class extendTest.

<?php
class test {

    public function __construct() {
    }

    public function name() {
        $this->xname('John');
    }

    private function showName($name) {
        echo 'my name in test is '.$name;
    }
}

class extendTest extends test {

    public function __construct() {
        parent::__construct();
    }

    private function showName($name) {
        echo 'my name in extendTest is '.$name;
    }
}

$test = new extendTest();
$test->name();
?>

result: my name in test is John

If we change visibility of the showName method to public or protected then the result of the above will be:
my name in extendTest is John
admin at dmsoft-wy dot com 28-Sep-2010 02:46
You don't need to include_once the parent class if it's already declared.  You do have to load the parent first and then the child.
bilalghouri at live dot com 14-Sep-2010 10:33
One thing I figured out after a long time about extending a parent class that, if the child class does not have any construct function, it will use its parent's construct.

for example:

<?php
class Main
{
    public $a;

    public function __construct()
    {
        echo '::Parent Class initiated::';

        $this -> a = 'we are in the parent class';
    }
}

class Child extends Main
{
    public function getA()
    {
        return $this -> a;
    }
}

$main = new Main();
$main -> child = new Child;

echo $main -> child -> getA();

//Output - ::Parent Class initiated::::Parent Class initiated::we are in the parent class
?>

However, If we have a constructor in the child class as well:

<?php
class Child extends Main
{
    public function __construct()
    {

    }
    public function getA()
    {
        return $this -> a;
    }
}
?>

Then :

<?php
$main = new Main();
$main -> child = new Child;

echo $main -> child -> getA();

// Output - ::Parent Class initiated::
?>

Note that the parent variable 'a' is not inherited by the child class if the constructor from the parent class isnt called.
This behaviour of extension made me waste a lot of my precious time, as I could not understand why some of my child classes were inheriting parent variables and some were not.
Hope this helps someone..
f4bi0_ at hotmail dot com 29-Jun-2010 08:19
<?php
// what if we want to extend more then one class?

Abstract class ExtensionBridge
{
    // array containing all the extended classes
    private $_exts = array();
    public $_this;

    function __construct(){$_this = $this;}

    public function addExt($object)
    {
        $this->_exts[]=$object;
    }

    public function __get($varname)
    {
        foreach($this->_exts as $ext)
        {
            if(property_exists($ext,$varname))
            return $ext->$varname;
        }
    }

    public function __call($method,$args)
    {
        foreach($this->_exts as $ext)
        {
            if(method_exists($ext,$method))
            return call_user_method_array($method,$ext,$args);
        }
        throw new Exception("This Method {$method} doesn't exists");
    }

}

class Ext1{
 private $name="";
 private $id="";
 public function setID($id){$this->id = $id;}
 public function setName($name){$this->name = $name;}
 public function getID(){return $this->id;}
 public function getName(){return $this->name;}
}

class Ext2{
 private $address="";
 private $country="";
 public function setAddress($address){$this->address = $address;}
 public function setCountry($country){$this->country = $country;}
 public function getAddress(){return $this->address;}
 public function getCountry(){return $this->country;}
}

class Extender extends ExtensionBridge
{
    function __construct()
    {
        parent::addExt(new Ext1());
        parent::addExt(new Ext2());
    }

    public function __toString()
    {
        return $this->getName().', from: '.$this->getCountry();
    }
}

$o = new Extender();
$o->setName("fabio");
$o->setCountry("brazil");
echo $o;
?>
don't hesitate to email me, happy coding! ;)
Frank 10-Jan-2010 06:46
Just a quick example of how PHP will handle a parent calling a function named in both the parent and the child class.  You would think it might use the function the way it is defined in the parent, but it does use the function that is defined in the child.

<?php
class One{
    function showOne(){
        echo 'Function One prints';
    }
    function hitFunction(){
        $this->showOne();
    }
}
class Two extends One{
    function showOne(){
        echo 'Function Two prints';
    }
}

$thistwo = new Two;
$thistwo->hitFunction(); //prints "Function Two prints"
?>
Edward_nl 03-Mar-2006 11:54
If you are using a child-class. Remember to call the constructor of the parent class aswell before you start using it. Otherwise you might get different results then you expected. It is stated in this document, but I got confused by the given example. So, here is my example:

<?php
error_reporting(E_ALL);

class test {
  var $var;

  function test() {
    $this->var = 3;
  }
}

class testing extends test {
   function testing() {
     parent::test();
   }

   function My_test() {
     return $this->var;
   }
}

$p = new testing();
echo $p->My_test();
// Returns 3
alan hogan 27-Nov-2005 02:48
Just a note:  It is possible to have a class inherit from multiple other classes, but only in a one-at-a-time linear hierarchy.

So this works, and C gets A and B functions:

<?php
class A {
  public function af() { print 'a';}
  public function bark() {print ' arf!';}
}
class B extends A {
  public function bf() { print 'b';}
}
class C extends B {
  public function cf() { print 'c';}
  public function bark() {print ' ahem...'; parent::bark();}
}

$c = new C;
$c->af(); $c->bf(); $c->cf();
print "<br />";
$c->bark();
/**results:**/
//abc
//ahem... arf!
?>

This does NOT work:

<?php
class A {
  public function af() { print 'a';}
  public function bark() {print ' arf!';}
}
class B {
  public function bf() { print 'b';}
}
class C extends B, A /*illegal*/ {
  public function cf() { print 'c';}
  public function bark() {print ' ahem...'; parent::bark();}
}

$c = new C;
$c->af(); $c->bf(); $c->cf();
print "<br />";
$c->bark();
//Parse Error
?>
Bash I. 19-Nov-2005 06:43
Here is a simple idea that I use when I need my abstract classes (the inherited classes) implemented before my functional classes.

<?php

    $_CLASSES = array_merge (
        glob ("classes/*/*.abstract.php"),
        glob ("classes/*/*.class.php")
    );

    foreach ($_CLASSES AS $_CLASS) {
        require ($_CLASS);
    }

?>
volte6 at nowhere dot com 31-Mar-2005 07:11
When declaring a class that relies upon another file ( because it extends the class defined in that file ), you should ALWAYS require_once() that file at the top.
This applies even when planning on looping through and including everything in the folder. Use require_once() in your loop, and at the top of the file that NEEDS the include.
tomnezvigin at comcast dot net 07-Mar-2005 02:19
This may seem obvious, but check this scenario. You have a class folder:

+ class
--classA.php
--classB.php
--classC.php
--mainClass.php

Here... classA, classB, classC all extend the mainClass.

If you try to create a function that automatically includes all of the classes in a folder, normally, they are included alphabetically.

When you try to instantiate classC, for example, you will get an error:

"Cannot inherit from undefined class mainClass"

EVEN IF you instantiate the mainClass before you instantiate all of the other classes.

In other words, make sure your primary class is included before all others.
Msquared 19-Nov-2004 03:48
Multiple inheritence is often more trouble than it's worth.  For example, you have a class foo that inherits from both class bar and class baz.  Classes bar and baz both have a fubar() method.  When you create a foo object and call its fubar() method, which fubar() method is called: bar's or baz's?

It seems to me that using aggregate to glue one class's methods and data to another object is a bit like Ruby's fixins, but I could be wrong...

[[Editor's note:
The aggregate_* functions have been dropped, as of PHP 5
-S
]]
efredin at redtempest dot com 03-Mar-2004 02:35
It is possible to override a method innherited from a parent class by simply re-defining the method (for those of us who enjoy using abstract classes).

<?php
class A
{
    var $foo;

    function A()
    {
        $this->foo = "asdf";
    }

    function bar()
    {
        echo $this->foo." : Running in A";
    }
}

class B extends A
{
    function bar()
    {
        echo $this->foo." : Running in B";
    }
}

$myClass = new B;
$myClass->bar();
?>
mazsolt at yahoo dot com 04-Jul-2003 05:49
Just a simple example about inheritance:

<?php
class a1{
  var $a=10;
  function a1($a){
     $this->a=$a;
  }
}

class a2 extends a1{
  var $x=11;
  function a2($x,$y){
     $this->x=$x;
     parent::a1($y); // or a1::a1($y) or $this->a1($y)
  }
}

class a3 extends a2{
  var $q=999;
}

$x=new a3(99,9);
echo $x->a,"<br>",$x->x,"<br> ",$x->q;
?>

The output will be:

9
99
999
calimero at creatixnet dot com 23-Jun-2003 05:58
Just a quick note to make things more clear : while multiple inheritance is not allowed, several levels of single inheritance  ARE ALLOWED indeed. Just test this example :

<?php
class A {
    var $name='A';

    function disp() {
        echo $this->name;
    }
}

class B extends A {
    var $name='B';
}

class C extends B {
    var $name='C';
}

$truc = new C() ;
$truc->disp(); // Will output C
?>

This is especially important to keep in mind while building a huge object hierarchy. for example :

+GenericObject
->+ Person
->->Employee
->+Computer
->->+WorkStation
->->-> PPC
->->-> Intel
->->+Server
->->->LDAPServer
->->->IntranetWebServer

.. and so on. Multiple level hierarchy relationship are possible in a tree-like structure (each child has one and only one parent, except for the root object).
quinton at free dot fr 10-Jun-2003 10:07
a nice example using extends and multiple classes  and constructors.

<?php

class CoreObject {
  var $name;

  function CoreObject($name){
    $this->_constructor($name);
  }

  function _constructor($name){
    $this->name = $name;
  }

  function show(){
    printf("%s::%s\n", $this->get_class(), $this->name);
  }

  function get_class(){
      return get_class($this);
  }
}

class Container extends CoreObject{
 var $members;
 function Container($name){
   $this->_constructor($name);
 }

 function &add(&$ref){
   $this->members[] = $ref;
   return ($ref);
 }

  function show(){
   parent::show();
   foreach($this->members as $item){
     $item->show();
   }
 }
 function apply(){
 }
}

class Person extends CoreObject{
  function Person($name){
    $this->_constructor($name);
  }
}

class Family extends Container {

 var $members;
 function Family($name){
   $this->_constructor($name);
 }
}

echo "<pre>\n";

$family = new Family('my family');
$family->add(new Person('father'));
$family->add(new Person('mother'));
$family->add(new Person('girl'));
$family->add(new Person('boy'));

$family->show();

print_r($family);

?>
inerte is my hotmail.com username 27-Sep-2002 10:36
[Editor's note: For an alternative to multiple inheritance, see the dynamic binding via object aggregation in the corresponding section of the manual.]

Multiple Inheritance is not supported but it is easy to emulate it:

<?php
class multipleInheritance
{
    function callClass($class_to_call)
    {
        return new $class_to_call();
    }
}

class A
{
    function insideA()
    {
        echo "I'm inside A!<br />";
    }
}

class B
{

    function insideB()
    {
        echo "I'm inside B!<br />";
    }
}

class C extends multipleInheritance
{
    function insideC()
    {
        $a = parent::callClass('A');
        $a->insideA();
        $b = parent::callClass('B');
        $b->insideB();
    }
}

$c = new C();
$c->insideC();
?>

---
This will succesfully echo:
I'm inside A!
I'm inside B!
schultz at rancon dot de 16-Aug-2002 07:37
This prints out 'ab'.  No need to create a new instance of a, therefor both methods still exists with same name.

<?php

class a {
  function samename(){
    echo 'a';
  }
}

class b extends a{
  function samename(){
    echo 'b';
  }
  function b(){
    a::samename();
    b::samename();
  }
}

$test_obj = new b();
?>
griffon9 at hotmail dot com 18-Jul-2002 01:42
Just to clarify something about inheritance. The following code :

<?php
class a
{
     function call()
     {
          $this->toto();
     }

     function toto()
     {
          echo('Toto of A');
     }
}

class b extends a
{
     function toto()
     {
          echo('Toto of B');
     }
}

$b=new b;
$b->call();

?>

...will correctly display "toto of B" (that is, the function declared in the parent is correctly calling the redefined function in the child)
php_AT_undeen_DOT_com 11-Dec-2001 08:31
if the class B that extends class A does not have a constuctor function (i.e. a function named B), then the constructor function of A will be used instead, you don't need to make a constructor in B just to call the constructor of A.

For example:

<?php
class A
{
  function A()
    {
      echo "HEY! I'm A!\n";

    }
}

class B extends A
{
}

$b = new B();
?>

produces the output:
HEY! I'm A!
bpotier at edreamers dot org 07-Nov-2001 09:08
Just one thing that may seem obvious but not mentionned in this page is that you need to include/require the file containing the parent class or else you'll get an error:

<?php
require(dirname(__FILE__).'/'.'myParent.php');
// ...
myChild extends myParent {
  // ...
}
// ...
?>


Constructors

Constructors are functions in a class that are automatically called when you create a new instance of a class with new. A function becomes a constructor, when it has the same name as the class. If a class has no constructor, the constructor of the base class will be called, if it exists.

<?php
class Auto_Cart extends Cart {
    function Auto_Cart() {
        $this->add_item("10", 1);
    }
}
?>

This defines a class Auto_Cart that is a Cart plus a constructor which initializes the cart with one item of article number "10" each time a new Auto_Cart is being made with "new". Constructors can take arguments and these arguments can be optional, which makes them much more useful. To be able to still use the class without parameters, all parameters to constructors should be made optional by providing default values.

<?php
class Constructor_Cart extends Cart {
    function Constructor_Cart($item = "10", $num = 1) {
        $this->add_item ($item, $num);
    }
}

// Shop the same old boring stuff.
$default_cart = new Constructor_Cart;

// Shop for real...
$different_cart = new Constructor_Cart("20", 17);
?>

You also can use the @ operator to mute errors occurring in the constructor, e.g. @new.

<?php
class A
{
    function A()
    {
        echo "I am the constructor of A.<br />\n";
    }

    function B()
    {
        echo "I am a regular function named B in class A.<br />\n";
        echo "I am not a constructor in A.<br />\n";
    }
}

class B extends A
{
}

// This will call B() as a constructor
$b = new B;
?>

The function B() in class A will suddenly become a constructor in class B, although it was never intended to be. PHP 4 does not care if the function is being defined in class B, or if it has been inherited.

Caution

PHP doesn't call constructors of the base class automatically from a constructor of a derived class. It is your responsibility to propagate the call to constructors upstream where appropriate.

Destructors are functions that are called automatically when an object is destroyed, either with unset() or by simply going out of scope. There are no destructors in PHP. You may use register_shutdown_function() instead to simulate most effects of destructors.

User Contributed Notes

There are no user contributed notes for this page.


Scope Resolution Operator (::)

Caution

The following is valid for PHP 4 and later only.

Sometimes it is useful to refer to functions and variables in base classes or to refer to functions in classes that have not yet any instances. The :: operator is being used for this.

<?php
class A {
    function example() {
        echo "I am the original function A::example().<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am the redefined function B::example().<br />\n";
        A::example();
    }
}

// there is no object of class A.
// this will print
//   I am the original function A::example().<br />
A::example();

// create an object of class B.
$b = new B;

// this will print
//   I am the redefined function B::example().<br />
//   I am the original function A::example().<br />
$b->example();
?>

The above example calls the function example() in class A, but there is no object of class A, so that we cannot write $a->example() or similar. Instead we call example() as a 'class function', that is, as a function of the class itself, not any object of that class.

There are class functions, but there are no class variables. In fact, there is no object at all at the time of the call. Thus, a class function may not use any object variables (but it can use local and global variables), and it may not use $this at all.

In the above example, class B redefines the function example(). The original definition in class A is shadowed and no longer available, unless you are referring specifically to the implementation of example() in class A using the ::-operator. Write A::example() to do this (in fact, you should be writing parent::example(), as shown in the next section).

In this context, there is a current object and it may have object variables. Thus, when used from WITHIN an object function, you may use $this and object variables.

User Contributed Notes

singh206 at gmail dot com 22-Jul-2008 08:12
Using PHP 4, I was unable to see an easy method for retrieving static constants in a library, as well as static functions.  There was no need to set variables, as the library is final, used for Configuration settings.

<?php
class StaticLibrary
{
    //static constants are set here
    function finalConstants(&$Vars)
    {
        $Vars['someVar']        = 'Hello';
        $Vars['anotherVar']     = 'World';
        $Vars['moreVars']       = 'Keep It Coming';
    }

    //usage: StaticLibrary::vars('someVar')
    function vars($v)
    {
               static $Vars;
             if (!is_array($Vars)) { $Vars = array(); StaticLibrary::finalConstants($Vars); } //setup access to the static contants in our Final library
             return($Vars[$v]); //return some var
      }

    //usage: StaticLibrary::staticFunction('Some string and I want to ')
    function staticFunction($str)
    {
            $str .= 'freak that string!';
            return $str;
    }
}
?>

  I appreciate the ideas below!!
philipp dot feigl at gmail dot com 10-Dec-2007 03:06
As already pointed out in a previous comment, there is no way to statically access variables, i wrote this small workaround

<?php
class A {
    var $MY_VIRTUAL_CONST = 1;
    function MY_VIRTUAL_CONST() { $vars = get_class_vars(__CLASS__); return $vars[strToUpper(__FUNCTION__)]; }
}

echo A::MY_VIRTUAL_CONST();
?>

This magically retrieves the correct value without creating an object instance and its easily copy & pasteable, as the methods content is completly generic.
Robert Chapin 25-Jun-2007 04:42
I revised this example to give a better feel for the scope and overloading rules in PHP 4.

<?php
class A {
    function example() {
        echo "I am the original function A::example().<br />\n";
        $this->other();
    }
    function other() {
        echo "I am the original function A::other().<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am the redefined function B::example().<br />\n";
        A::example();
    }
    function other() {
        echo "I am the redefined function B::other().<br />\n";
    }
}

// there is no object of class A.
// this will print
//   I am the original function A::example().<br />
//   PHP Fatal error:  Call to a member function on a non-object in test.php on line 5
A::example();

// create an object of class B.
$b = new B;

// this will print
//   I am the redefined function B::example().<br />
//   I am the original function A::example().<br />
//   I am the redefined function B::other().<br />
$b->example();
?>

Robert Chapin
Chapin Information Services
deadimp at gmail dot com 07-Feb-2007 06:04
I've noticed that you can't easily obtain a re-scoping of a function-to-object by external means, and it can only really be done by functions ["methods" to be proper] inside the class.
I dub this phenomon "inability to externally modifiy scope resolution of class method", 'cause I don't really know the technical name. (Maybe something else along the lines of "inability to combine scope resolution and member selector operators".)
Enough of this, on to the example:
<?php
class Base {
 var $a=5;
 function Func() {
  echo "Base::Func $this->a<br>";
 }
}
class Child extends Base {
 function Func() {
  $this->a=35;
  Base::Func();
  //$this->Base::Func(); //Won't work either
  echo "Child::Func $this->a<br>";
 }
}
$obj=new Child;
$obj->Func(); //That works, outputs as expected
$obj->Base::Func(); //ERROR - See below
?>
As for the error, I get the Hebrew "double colon", as Shachar Shemesh mentioned: "parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM"
I find it odd that I can't do this, and using C++ (where you are able to do this), I feel restricted in this sense.
And I can't think of any workarounds right now, aside from using this scope resolution in the class scope.

NOTE: I'm using PHP 5.0.5 right now (used to be using 4.x, but might've switched it in xampp)
Shachar Shemesh 09-Sep-2006 09:41
The PHP lexical analyzer gives this token the name "T_PAAMAYIM_NEKUDOTAYIM". It might as well have been written in Hebrew. Wait, it IS written in Hebrew. It means "double colon".

I'm writing this comment mostly so that people who are searching the web for what the #(!(@*&#$ errors such as "unexpected T_PAAMAYIM_NEKUDOTAYIM" or "missing T_PAAMAYIM_NEKUDOTAYIM" mean get to the right page in the PHP manual.

Shachar
docey 28-Jul-2006 08:38
after doing some research as the manual was somewhat
vague, i found out that using $this in a class-function is
not fatal as the manual implied, its not even a warning.

it simply comes with a notice saying: $this is undefined.

it appears the class-function is just as a global function.
and as such the static keyword holds its value after scope
changes.

however it seems to also holds its value after the class
gets instanced. take a simple counter function thats a
member of a class. make a few calls to the counter to
let it count to lets say 3. then instance the class and let
it count it again 3 times. you think the score is now 3,
infact its 6.

so one simple way to find out if a function is called from
a instanced class or not is to have the constructor set a
class variable and then check for it using isset. since in
the class-function scope the $this is unavailable isset
will return false. but when the class has been instanced
it will return true.

btw, i tested it with php4.4.2CLI.

hope this will clear somethings up.
rudy at sandbenders dot ca 14-Nov-2005 07:20
my last note is actually not 100% correct... as it is, trying to return a reference to an element of the class variables array won't work, even though the classVars function is declared as (possibly) returning a reference... the correct way to return a reference to one of your class variables would be

<?php

class theClass {
    ...
    function &classVars($name = NULL, ...) {
        static $myClassVars;
        ...
        $myRef =& $myClassVars[$name];

        return($myRef);
    }
}

?>

which works as expected when you're looking for a reference. This should only be necessary when you're trying to return a reference to a piece of the array as opposed to the whole array itself. Simply doing this

<?php

return($myClassVars);

?>

works as expected, ie: you can get a reference to the whole array without needing to explicitly create a reference to it before returning.
rudy at sandbenders dot ca 14-Nov-2005 07:05
Since re-posting a simplified version of the peterjoel.com solution to class variables in php4 was kinda lame, here's the modified solution to give you one function (per class) that allows you to use any number of 'class' variables...

<?php

class theClass {
    function &classVars($name = NULL, $value = NULL) {  // The classVars function is (possibly) returning a reference...
        static $myClassVars;  // Define a static var to hold our 'class' vars...

        if (!is_array($myClassVars)) {  // Initialize the array if it isn't already...
            $myClassVars = array();
        }

        if (!is_null($name) && !is_null($value)) {  // If we got two arguments, set the class var that was passed...
            $myClassVars[$name] = $value;
        }

        if (!is_null($name)) {  // If we got one argument, return the var it's referring to...
            return($myClassVars[$name]);
        } else {
            return(NULL);
        }
    }
}

// Setting class variables...

theClass::classVars("varOne", "valueOne");
theClass::classVars("varTwo", "valueTwo");

// Getting class variables...

$myRef = &theClass::classVars("varOne");
echo theClass::classVars("varTwo");

?>

You should be able to store anything in your class vars this way, and depending on how you call the function, you can have either a copy or a reference to the class var returned (see manual on 'returning by reference'...) Again, hope this helps someone...
rudy at sandbenders dot ca 14-Nov-2005 06:40
'emulating' class variables in php4... peter at textstore dot c() dot il  put me on the right track... I came up with the following and then before posting this realized that peterjoel.com has already posted almost the exact same thing... anyways, this is a little simpler and only involves one function instead of a getter/setter pair...

class myClass {
    function &myVar($arg = NULL) {
        static $myClassVar;
        if (!is_null($arg)) {
            $myClassVar = $arg;
        }
        return($myClassVar);
    }
}

// Setting the 'class' var...

myClass::myVar($myNewClassVar);

// Getting the 'class' var...

$classVarRef = &myClass::myVar();

This should let you set up 'class' vars that will hold anything you can reference... with a little creative array usage, you could modify this to allow any number of class vars without having to write the same above function for each one... hope someone finds it useful...
JDS <jeff at newtnotes dot com> 31-Aug-2005 04:10
Using an anonymous class instance via the scope resolution operator does not appear to call the constructor method of the class.  Thus, any "stuff" set up within a constructor method (the method called when creating a new instance of a class) will not get set up using anonymous classes.

So for example something like this

<?php
Class A{
    var $thing;
    function A(
        $this->thing = "hello";
    )
    function get ($var){
        return $this->$var;
    }
}

echo A::get('thing');
?>

will not print anything as A::thing does not get "set up" outside of the constructor.

You would have to create an instance of class A to get any value for A::thing.

For example:
<?php
$a = new A();
echo $a->get('thing');
?>
(prints "hello")

This is based on my observations and not from any official documentation.  I may be wrong about the details.
evert at collab dot nl 28-Aug-2005 10:21
A note to RichardBronosky's suggestion:

This is a very insecure method, never unserialize user data because they might be able upload a totally different class.
They can only specify properties, not methods but if they can research your code they might be able to do uninteded stuff or create a XSS attack.

So only unserialize user-data when you are in a controlled safe enviroment.

Evert
RichardBronosky (firstname at lastname dot com) 09-May-2005 11:33
In reply to tim dot ward at stivesdirect dot com:
I had a need to to do a very similar thing.  I wrote a class that will be used to create an object which can serialize itself into a cookie and be used to recover itself from the cookie later.  To do the recovery you must use the Scope Resolution Operator (::) to call a function as a class function (vs. and object function).  The recovery is very simple to initiate because there is only one line of code to call (e.g. $the_object = the_class::the_function(); )  But the function you call must do a ton of stuff, and call a few other functions within the class.  This is where the problem lies.

I would like to thank <brooke at jump dot net>, <pollita at php dot net>, and <MagicalTux at FF.ST> (from http://php.net/manual/en/function.get-class.php) for leading me to the solution.

<?php
class foo
{
   function get_object_from_cookie(...)
   {
      ...
      return unserialize($decrypted_cookie_data);
   }
   function recover_from_cookie()
   {
      ...prepare to recover...
      // The next line is the tricky part.
      $o = call_user_func(array(__CLASS__, 'get_object_from_cookie'), $encryption_secret, $cookie_name);
      $temp = $o->get_stuff();
      $o->other_stuff = $o->do_calculate($temp);
      $o->do_complicated_things();
      return $o;
   }
}
$bar = foo::recover_from_cookie();
...
?>

I hope that saves someone an hour.  It sure would have saved me one.
mark at branly dot com 07-Mar-2005 11:53
I didn't see where this issue was addressed so in regards to this post:

\\-------\\//-------//
when using "::" operator inside class functions, you can achieve quite interesting results. let's take this example:

  class cCat {
   function Miew(){
     // cCat does not have a member "kind", but cDog has, and we'll use it
     echo "I am ".$this->kind.", and I say MIEW\n";

     // here things are even stranger: does cCat class
     // support WhoAmI function? guess again...
     $this->WhoAmI();
   }
  }

  class cDog {
   var $kind = "DOG";
   function Bark(){
       // let's make this dog act like a cat:)
       cCat::Miew();
   }

   function WhoAmI(){
     echo "Yes, I'm really ".$this->kind."!";
   }
  }

  $dog = new cDog();
  echo $dog->Bark();

outputs:
I am DOG, and I say MIEW
Yes, I'm really DOG!

The interesting thing here is that cDog is not descendant of cCat nor vice versa, but cCat was able to use cDog member variable and function. When calling cCat::Miew() function, your $this variable is passed to that function, remaining cDog instance!

It looks like PHP doesn't check if some class is an ancestor of the class, calling function via '::'.

//------//\\--------\\

The problem here is not that PHP is not checking ancestry. The problem is that "$this" refers to the calling object $dog even though it is referenced inside the cCat class definition. Since a cCat is never instantiated, $this has not been scoped to it.

To further illustrate the point, if you never instatiate dog (or any object for that matter), $this will never be set and when you statically call the miew function, you will get the following output:

"I am , and I say MIEW"

This flexibility of member handling, PHP does not afford to methods and as such the second line of the miew function will generate a fatal error reporting that the method has been called from a non object.

If you did want to take it one step further in the whole ancestry guessing issue, try instatiating cCat and calling the miew function. You will get the same result as above *EXCEPT* the fatal error will inform you the function has not been defined.

So, there you have it--PHP DOES NOT guess at inheritance.
mark@thedarkside 25-Feb-2005 12:04
Carrying on from the previous note, it is possible to call a member function statically using a variable for the name of the class, i.e.:

<?php
class MyObject {
   function myfunc() {
      return "Hello! ($this)";  // '$this' to determine whether it is being called statically
   }
}

$type = "MyObject";
$object = new $type();

$message = $object->myfunc();
$message = MyObject->myfunc();
$message = call_user_func(array(&$type, "myfunc"));

O.K., so it's not using the scope resolution operator, but it does what you want... it calls 'myfunc' statically from the class named by $type.
Tom Wiltshire 24-Feb-2005 01:33
It's worth noting that you can't use a variable with this operator.
The following is legal:

    class MyObject {
        function myfunc() {
            return "Hello!";
        }
    }
    $type = "MyObject";
    $object = new $type();
    $message = $object->myfunc();

(Puts "hello" in $message)

You can also use the scope operator:

    $message = MyObject::myfunc();

However, the following is NOT legal:

    $message = $type::myfunc();

It's a pity, 'cos right now I've got a situation where it'd be really handy, but that's the way it goes!
stefano at obliquid dot it 08-Jul-2004 02:22
peter has a good point to suggest the use of static function variables instead of static class variables that PHP 4 does not support.

Unfortunately, also the first sentence of the manual is misleading "... refer to functions and variables in base classes or to refer to functions in classes that have not yet any instances. The :: operator is being used for this.".

You cannot use the :: operator on class variables since there are no static class variables. I lost some time trying to refer to class variable with this operator, so I think it may be useful to post a warning.

http://dev.obliquid.com
tango23 at inbox dot lv 06-May-2004 04:20
>> That's how to make class' static variable in PHP4.

Note, that nobody seems to have a way of actually making a global static server-wide object in PHP.

All the static/class var ways above work only within a single PHP page.

If you want to do something like:

class Logger
{
   ...
   function Logger($filepath)
   ...
}

in some global include file:
$logobj = new Logger("mylogfile");

and then in all of your pages:

$logobj->log("my message");

and there is no way you can make this logobj static (such that all your pages can refer to without making unncecessary copies).

Can't be done with using uninstantiated Logger::log() funcs either, because you are not setting the logfile filepath parameter anywhere persistent.

The only kludge seems to be to use the filepath as a global variable and use Logger:: syntax.
altherac at yahoo dot fr 14-Mar-2004 11:28
Using a class function is a good way to write a Singleton pattern in php 4 using a static variable.

The sample code below shows the pattern in action :

<?
    class Singleton
    {
        var $_info = null;

        function Singleton() {
            $this->_info = 'Original value';
        }

        function getInfo() {
            return $this->_info;
        }

        function setInfo($info) {
            $this->_info = $info;
        }

        function &getInstance() {
            static $instance = null;

            if (is_null($instance)) {
                $instance = new Singleton();
            }
            return $instance;
        }
    }

    $a = & Singleton::getInstance();
    $b = & Singleton::getInstance();
    $a->setInfo('Hi, my name is A');
    echo $b->getInfo();

?>

I have seen some Singleton pattern implementations that were using an external function to instanciate the object, but I don't think it is the best way to implement the pattern.

You may want to force the getInstance() function to be called in a static way with the :: operator by testing if $this is set and raising an error or returning a null value (depending on your design).
Rami Kayyali (rami at bluecubex dot com) 08-Nov-2003 05:20
Regarding the note by "gk at proliberty dot com". No, eval() isn't superior to call_user_func(), eval() has security issues and performance overhead.
To achieve the same results using your example with call_user_func, simply pass $this_class by reference. This is probably because call_user_func is passing a copy of $this_class.

<?php

function init(&$this_class, $current_class, $params= NULL ){
    $this_class->classFiles[] = __FILE__;

    $parent_class=get_parent_class($current_class);
    if( !empty($parent_class)){
        // call_user_func() NOW DOES CORRECTLY UPDATE $this_class->classFiles :)
        call_user_func(array($parent_class,'init'),
        &$this_class,$parent_class,$params);
    }

} // init()

?>

But please note, call-time pass-by-reference has been deprecared, or at least, PHP 4.3.2 says so.

Conclusion: Minimize using eval as much as possible, many projects had major security issues because they were using eval().
brooke at jump dot net 07-Nov-2003 12:27
Someone mentioned that eval was better than call_user_func because the latter would not result in updates to the objects. I'm not sure this is the right place for such a debate, but if it is, then the right way is:

call_user_func(array(&this, $method), $arg1, $arg2);

You put a reference to (instead of a copy of) this in the array which you pass on.
wikiz at studentas dot lt 15-Jun-2003 02:40
when using "::" operator inside class functions, you can achieve quite interesting results. let's take this example:

  class cCat {
    function Miew(){
      // cCat does not have a member "kind", but cDog has, and we'll use it
      echo "I am ".$this->kind.", and I say MIEW\n";

      // here things are even stranger: does cCat class
      // support WhoAmI function? guess again...
      $this->WhoAmI();
    }
  }

  class cDog {
    var $kind = "DOG";
    function Bark(){
       // let's make this dog act like a cat:)
       cCat::Miew();
    }

    function WhoAmI(){
      echo "Yes, I'm really ".$this->kind."!";
    }
  }

  $dog = new cDog();
  echo $dog->Bark();

outputs:
I am DOG, and I say MIEW
Yes, I'm really DOG!

The interesting thing here is that cDog is not descendant of cCat nor vice versa, but cCat was able to use cDog member variable and function. When calling cCat::Miew() function, your $this variable is passed to that function, remaining cDog instance!

It looks like PHP doesn't check if some class is an ancestor of the class, calling function via '::'.
gk at proliberty dot com 29-May-2003 03:33
Contrary to the comment above, I have found that call_user_func()
is inferior to eval() because call_user_func() does not correctly
update objects passed by reference.

In the example below, each of my subclasses calls init(), passing
a reference to the current object, whose class variables are
initialized by classes in the hierarchy. The class variable
$this->classFiles contains the paths of each file for classes in
the hierarchy. It is not updated correctly using call_user_func()
but eval() works fine.

<?php
///////////////////////////////////////////////////////
/*
  xobj( );
    constructor
    SUBCLASSES MUST NOT have any constructors
*/
/////////////////////////////////////////////////////////
function xobj( $params= NULL ){

    $current_class=get_class($this);
    $this->init($this, $current_class, $params);
    // $this->classFiles[0] is the file containing the final subclass
    $this->classFile=$this->classFiles[0];
    print_r($this->classFiles); exit;
    $this->_init($params);
}

///////////////////////////////////////////////////////
/*
  void init(&$this_class, $current_class, $params= NULL );
    initialization
    this class MUST be overridden to set $this->classFiles correctly:
    initialization requires __FILE__ to get the path of every class file
*/
/////////////////////////////////////////////////////////
function init(&$this_class, $current_class, $params= NULL ){
    $this_class->classFiles[] = __FILE__;

    $parent_class=get_parent_class($current_class);
    if( !empty($parent_class)){
        eval("$parent_class::init(\$this_class, \$parent_class, \$params);");
        // call_user_func() DOES NOT CORRECTLY UPDATE $this_class->classFiles:
        //call_user_func(array($parent_class,'init'),
$this_class,$parent_class,$params);
    }

} // init()

?>

RESULT, using eval():
Array
(
    [0] => /usr/local/apache/htdocs/common/php/xobj/xobj_subclass.php
    [1] => /usr/local/apache/htdocs/common/php/xobj/xobj.php
)

RESULT, using call_user_func():
Array
(
    [0] => /usr/local/apache/htdocs/common/php/xobj/xobj_subclass.php
)
pollita at php dot net 29-Dec-2002 04:47
While the method described by robinv at ecosse dot net to call a method of an arbitrarily named function will work:

eval("$classname::$methodname(\$param1,\$param2);");

There is another way to do so without using eval():

call_user_func(array($classname,$methodname),$param1,$param2);

This will do the same thing without the performance hit of eval and without the added security concerns.
peterjoel.com 18-Nov-2002 05:12
You can implement a getter/setter pair to emulate a class variable. The syntax isn't so weird:

<?

class A{
    function getVar($val=NULL){
        static $class_variable = 0;
        if($val != NULL){
            $class_variable = $val;
        }
        return $class_variable;
    }
    function setVar($val=0){
        return A::getVar($val);
    }
}

A::setVar(3);
print A::getVar();

?>
frederik at spam pandora dot be 13-Nov-2002 11:46
My approach to using class constants is by making them functions.
Instead of

$var CONSTANT = 1;

I use

function CONSTANT { return 1; }

Now, when using this:

$i = ConstantClass::CONSTANT();

You can do anything with it: comparing, addition, multiplication. The syntax is a bit weird, but it works perfectly.
pierrick at hydromel_no_spam_ dot net 24-Sep-2002 08:39
It's possible to access class member variables:

class Test {
    var $vA = "hello world";
}

$class_var = get_class_vars(Test);
echo $class_var["vA"];

It's sound to be an ellegant way to acces them, no ? ;)
steven at caltech dot co dot uk 28-Aug-2002 03:50
Re: jdominic@prodigy.net 08-Aug-2002 05:49

While it would be nice to have static instance variables you can simply create a method with the same name which returns the value you require. This means you don't have to instantiate an object when you only need its static content. For example:

class Colour {

      function default () { return "black"; }
}

var $backgroundColour = Colour::default ();
jdominic at prodigy dot net 08-Aug-2002 09:49
According to the documentation, there is no way to have a class variable, only class functions. There should be class variables, because there's no way to implement CONSTANTS. Something like this won't work:

<?
        class Employee {

        var $ACTIVE_EMPLOYEE = 1;

        //etc..
        }

        echo Employee::ACTIVE_EMPLOYEE;

        //Also tryed:

        echo Employee::$ACTIVE_EMPLOYEE

?>

returns:
Parse error: parse error, unexpected ';', expecting '(' in /home/x/public_html/Test.php on line 9

It forces you to create an instance of the class:

$emp = new Employee();
echo $emp->ACTIVE_EMPLOYEE;
carl at thep.lu.se 13-Mar-2002 06:20
Note that if you have two classes, 'foo' and 'bar', and a function in foo called with $instance_of_foo->func() calls bar::func2(), $this will be defined in func2, and it will point at an object of class foo. In fact, you can redefine $this to point to an object of any class before calling bar::func2() to have $this to be a reference to that object. This means that you can't have functions that can be called either as static (::) or member(->), since you won't be able to tell the difference. This is a quite unfortunate consequence of the combination of references not containing any type information and it not being possible to call an explicitly chosen base class member function (that is, if x extends y there's no clean way to call y::f() on an object of class x (The unclean way is to assign $this to $instance_of_x, call y::f(), and reset $this to its old value.)).
peter at textstore dot c() dot il 29-Jul-2001 01:38
And now, encapsulated in constructor. Isn't it nice? Ye, and it WILL work when   more objects of class are serialized & unserialized ONLY when serialized in array / hash by one serialize() call - as serialize() should be used. However, when unserialized, it doesn't affect actual instances' "static" variable - thus, there will be 2 or more static variables after unserialization... but you can still change __wakeup() and to access "real"(actual) class' static value's reference, thus you have to use special function for every static value, as in example above... Ye, PHP is good!@

<?
  class O {

    function O() {
        static $statValue=0;
        $this->statValue= &$statValue;
    }
  }

  $o1= &new O;
  $o2= &new O;

  echo "\$o1: ".$o1->statValue."<br>";
  echo "\$o2: ".$o2->statValue."<br>";

  $o1->statValue= 5;
  $o2->statValue= 10;

  echo "\$o1: ".$o1->statValue."<br>";
  echo "\$o2: ".$o2->statValue."<br>";
?>
peter at textstore dot c() dot il 29-Jul-2001 01:10
That's how to make class' static variable in PHP4. That means reference returned by function value() is the same for all instances of class O, thus they can share common data. This reference is common just for instances on same PHP page, of course.

<?  class O {
    function &value() {
     static $val=0;
     return $val;
    }
  }

  $o1= &new O;
  $o2= &new O;

  echo "\$o1: ".$o1->value()."<b"."r>";
  echo "\$o2: ".$o2->value()."<b"."r>";

  $ref1= &$o1->value();
  $ref1= 5;

  $ref2= &$o2->value();
  $ref2= 10;

  echo "\$o1: ".$o1->value()."<b"."r>";
  echo "\$o2: ".$o2->value()."<b"."r>";
?>


parent

You may find yourself writing code that refers to variables and functions in base classes. This is particularly true if your derived class is a refinement or specialisation of code in your base class.

Instead of using the literal name of the base class in your code, you should be using the special name parent, which refers to the name of your base class as given in the extends declaration of your class. By doing this, you avoid using the name of your base class in more than one place. Should your inheritance tree change during implementation, the change is easily made by simply changing the extends declaration of your class.

<?php
class A {
    function example() {
        echo "I am A::example() and provide basic functionality.<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am B::example() and provide additional functionality.<br />\n";
        parent::example();
    }
}

$b = new B;

// This will call B::example(), which will in turn call A::example().
$b->example();
?>

User Contributed Notes

Aman 01-May-2015 07:42
We can't use parent:: keyword to access property of the parent class like methods as variables are not overridable as method does

Consider the following code

<?php

class Fish
{
    public $common_name = 'Name';
}

class Trout extends Fish
{
    public function getInfo()
    {
       return parent::$common_name;
    }
}

$brook_trout = new Trout();
echo $brook_trout->getInfo();

?>

this will cause following error

 Fatal error: Access to undeclared static property: Fish::$common_name

Simply use $this keyword to access both own and parent's variable
matirank at hotmail dot com 29-Mar-2015 02:46
class ExtendClass
 {
     public function __construct($variable,$array){
         return null;
     }
 }

 class NameClass extends ExtendClass
 {
    public function __construct($variable=null,$array = array()) {
        parent::__construct($variable, $array);
    }
 }
exmodione at yahoo dot com 02-Feb-2014 12:08
I was originally impressed with this language, and I got curious about its object-oriented features so I started applying them - but this whole this/self/parent thing is ridiculously stupid - it's confusing - I've been coding in C++ and C# - PHP's OO sucks!  I also heard from an online source many people who use PHP avoid its OO features. Now I know why.  It's stupid.
greg-wilson at hotmail dot com 07-Mar-2012 03:24
You can access/modify variables of the parent class from multiple subclasses:

class Superclass{
    private static $arr = array();

    protected addArrayElement($element){
        self::$arr[] = $element;
    }

    protected printArray(){
        var_dump( self::$arr );
    }
}

class SubclassA extends Superclass{
    public function addToArray($elem){
        parent::addArrayElement($elem);
    }
}

class SubclassB extends Superclass{
    public function addToSameArray($elem){
        parent::addArrayElement($elem);
    }
}

$classA = new SubclassA;
$classA->addToArray("first");
$classB = new SubclassB;
$classB->addToSameArray("second");

calling '$classB->printArray()' results in both "first" and "second" being printed, demonstrating shared static access to a superclass variable from multiple extended classes.
pjm at pehjota dot com 21-Oct-2009 07:32
I've seen people in the comments below use this, but I've also seen some unecessary workarounds to achieve it. So I thought I'd clarify, even though it is shown in the documentation. You CAN reference non-static parent methods, as in other languages. For example...

<?php

abstract class ParentClass {
    public function doSomething() {
        echo "done.\n";
    }
}

class ChildClass extends ParentClass {
    public function doSomething() {
        echo "attempting something.\n";
        parent::doSomething();
    }
}

$obj = new ChildClass();
$obj->doSomething();

?>

This will output both "attempting something." and "done." As you might expect, it also works if the parent method is protected. Users of other languages may have tried defining static and non-static methods with the same name. This is why that doesn't work ("parent" can refer to both static and non-static methods, and there's no way to differentiate them).

However, you CANNOT use the "parent" keyword to reference the parent's variables. When in a class, "parent" will always look for static variables. For example, this results in an "Access to undeclared static property" fatal error:

<?php

abstract class ParentClass {
    public $var = 5;
}

class ChildClass extends ParentClass {
    public function setVar($val) {
        parent::$var = $val;
    }
    public function getVar() {
        return parent::$var;
    }
}

$obj = new ChildClass();
$obj->setVar(6);  // Results in fatal error.
echo $obj->getVar() . "\n";  // Results in fatal error.
echo $obj->var . "\n";  // Would work and reference the parent's $var unless the child has its own $var.

?>
lesion at autistici dot org 05-Jun-2009 12:27
take care of using parent::method with not declared method when using __call because in this case the string method will be lowercase:

<?php
class A {
    function __call( $method, $args ) {
      echo get_class( $this )  . ' ' . $method . "\n";
    }
}

class B extends A {
    function getTest() {
        parent::getTest();
    }
}

$a = new A();
$a->getTest();
$b = new B();
$b->getTest();

?>

output:

A getTest
B gettest
php at stock-consulting dot com 10-Oct-2008 06:40
It appears that parent refers to the base class of the subclass in which the method is defined, not the subclass in which the method is exceuted. This behaviour is consistent with other object-oriented languages. PHP's documentation is a bit unclear here, so I had to test it (PHP5.2.6).
nate at 8 03-Aug-2008 06:06
To simulate traditional C# like parent access, child classes can be defined in the parent's constructor and linked via explict definition. ($this->b->par = $this;)

<?php
class A {

  var $foo;
  var $b;

  function A(){
    $this->foo = "foo!";
    $this->b = new B;
    $this->b->par = $this;
    }

  function bar(){
    print("bar!");
    }

};

class B {

  var $par;

  function B(){
    }

  function bar(){
    print( $this->par->foo );
    $this->par->bar();
    }

};

$test = new A;
$test->b->bar();
?>

This returns:

foo!bar!

demonstrating both variable and function access via the parent.
10-Mar-2007 12:49
<?php
    class A {
      function X() {
        echo 'y';
      }
    }

    class B extends A{
      function X() {
        echo 'z';
      }

      function Y() {
        echo parent::X();
      }
    }

    class C extends B {
      function Z () {
        echo parent::Y();
      }
    }

$c = new C();
$c->Z();                  //output is 'y'
?>
KOmaSHOOTER at gmx dot de 20-Dec-2004 02:59
An example for using a memberfunction with another memberfunction.

The sequencing for the creating of the class is important.

<?php
class A
{
   function example()
   {
       echo "I am A::example() and provide basic functionality.<br>\n";
   }
}

class B extends A
{
   function example()
   {
       echo "I am B::example() and provide additional functionality.<br>\n";
       parent::example();
   }
}

class C extends A
{

   function example()
   {
          global $b;
       echo "I am c::example() and provide additional functionality.<br>\n";
       parent::example();
       $b->example();
   }
}
$b = new B();
$c = new C();
$c->example();
;?>

Result:

I am c::example() and provide additional functionality.
I am A::example() and provide basic functionality.
I am B::example() and provide additional functionality.
I am A::example() and provide basic functionality.
27-May-2004 07:40
About the constructors :

Yes, a good pratrice could be to use a method called by the real constructor. In PHP 5, constructor are unified with the special __construct method. So we could do :

<?php
class A {
    var $a = "null";
    function A() {
        $args = func_get_args();
        call_user_func_array(array(&$this, "__construct"), $args);
    }
    function __construct($a) {
        $this->a = $a;
        echo "A <br/>";
    }
}

class B extends A {
    var $b = "null";

    function __construct($a, $b) {
        $this->b = $b;
        echo "B <br/>";
        parent::__construct($a);
    }
    function display() {
        echo $this->a, " / ", $this->b, "<br/>";
    }
}

$b = new B("haha", "bobo");
$b->display();

?>

It will have the same behavior than standard php constructors, except that you can not pass arguments by reference.
ckrack at i-z dot de 05-May-2004 06:39
Note that we can use $this in the parent's method like we might expect.
It will be used as the instance of the extended class.

<?php
class foo {
    var $x;
    function foofoo()
    {
        $this->x = "foofoo";
        return;
    }
}
class bar extends foo {
    // we have var $x; from the parent already here.
    function barbar()
    {
        parent::foofoo();
        echo $this->x;
    }
}
$b = new bar;
$b->barbar(); // prints: "foofoo"
?>
bitmore.co.kr 09-Feb-2004 10:13
/* Original */
/*
class A {
    var $x;
    function A() { $this->x = 32; }
    function joke() { print "A::joke" . A::x . "<br>\n"; # SYNTAX ERROR!! }
}
class B extends A {
    var $x;
    function B() {
        parent::A();
        $this->x = 44;
    }
    function joke() {
        parent::joke();
        print "B::joke " . $this->x . "<br>\n";
    }
}
echo A::joke();
echo B::joke();
*/

/* Original */
/* Modify */
/*
class A {
    var $x;
    function A() { $this->x = 32; }
    function joke() {
        $this = new A;
        print "A::joke = " . $this->x . "<br>\n";
    }
}
class B extends A {
    var $x;
    function B() {
        parent::A();
        $this->x = 44;
    }
    function joke() {
        parent::joke();
        $this = new B;
        print "B::joke = " . $this->x . "<br>\n";
    }
}

echo A::joke();
echo B::joke();

//print Show
A::joke = 32
A::joke = 32
B::joke = 44
*/
James Sleeman 10-Jan-2004 03:27
It should be noted that when using parent:: you are able to call a method defined within the parent, or any class that the parent is extending.. for example.

<?php
  class A
  {
    function test()
    {
      echo 'Hello ';
    }
  }

  class B extends A
  {

  }

  class C extends B
  {
    function test()
    {
      parent::test();
      echo 'Bobby.';
    }
  }

  $D =& new C();
  $D->test();
?>

Outputs "Hello Bobby.", the fact that B does not define test() means that A's test() is called, it should also be noted that test() is called in the context of an 'A' class, so if you try and call parent:: in A's test() it will correctly fail.

It's also worth noting that parent:: also works fine in a static method, eg <?php  C::test(); ?> still outputs "Hello Bobby.", and you can even do <?php B::test(); ?> which correctly outputs "Hello ", even though we're calling a static method that lives in an ancestor class!
adrian dabrowski 15-Sep-2003 04:48
if you like to use the here suggested method of calling a parent constructer by using get_parent_class(), you will expirience undesired behaviour, if your parent is using the same technique.

class A {
  function A() {
     $me = get_class($this);
     echo "A:this is $me, I have no parent\n";
  }
}

class B extends A {
  function B() {
     $par = get_parent_class($this);
     $me = get_class($this);
     echo "B:this is $me, my parent is $par\n";

     parent::$par();
  }
}

class C extends B {
  function C() {
     $par = get_parent_class($this);
     $me = get_class($this);
     echo "C:this is $me, my parent is $par\n";

     parent::$par();
  }
}
new C();

this will not produce the output you probably expected:
C:this is c, my parent is b
B:this is b, my parent is a
A:this is a, i have no parent

...but insted you will get in serious troubles:
C:this is c, my parent is b
B:this is c, my parent is b

Fatal error:  Call to undefined function:  b() in .............. on line 16

$this is always pointing to the 'topmost' class - it seems like this is php's way to cope with polymorphic OOP.
hustbaer 24-Aug-2003 07:37
something on the note of 'minoc':

in this setup, the "$this->$par();" will lead to infinite recursion...

class foo extends bar {
  function foo() {

    $par = get_parent_class($this);
    $this->$par();

    // then normal constructor stuff.
    // ...
  }

  function bar()
  {
    echo "this makes no sense, but who cares :)";
  }
}

but the problem is easity fixed by going like so:

class foo extends bar {
  function foo() {

    $par = get_parent_class($this);
    parent::$par();

    // then normal constructor stuff.
    // ...
  }

  function bar()
  {
    echo "this makes no sense, but who cares :)";
  }
}
mazsolt at yahoo dot com 05-Jul-2003 12:04
There is a difference between PHP and C# managing the overloaded variables of the parent class:

<?php
class a{
var $a=1;
function display(){
echo $this->a;
}
}

class b extends a{
var $a=9;
}

$x=new b;
$x->display(); // will display 9
?>

The same code in C# will display the value from the parent class.

[C#]
class a{
private int a=1;
public int display(){
return this.a;
}
}

class b:a{
private int a=9;
}

b x = new b();
Response.Write(b.display()); // will display 1
horne at teradyne dot com 26-Mar-2003 05:09
I've been scouring through the comments but haven't been able to find an example of how to use variable overriding. The manual hints that it is possible, but there aren't any examples. See the code for what I want to do:

class A {
  var $x;

  function A() {
    $this->x = 32;
  }

  function joke() {
    print "A::joke" . A::x . "<br>\n"; # SYNTAX ERROR!!
  }
}

class B extends A {
  var $x;

  function B() {
    parent::A();
    $this->x = 44;
  }

  function joke() {
    parent::joke();
    print "B::joke " . $this->x . "<br>\n";
  }
}

I want the following output when I call B::joke:

A::joke 32
B::joke 44

what do I get if I replace the A::x with $this->x?

A::joke 44
B::joke 44

How do I set the variable $x in the base class? Or, how do I get $this to point to the part of the class that is owned by class A?

I've tried the following (quite invalid) syntaxes:
A::x
A::$x
${A::x}
A::{$this->x}

It'd be nice to have a concrete example of how to access (read and write) parent variable members, or explicitly say that it is not possible to do that.
lelegiuly at iol dot it 24-Mar-2003 05:18
Sorry but my example above it's correct only if there's one class that inherits by another class. If the hierarchical tree is deeper than 2 my example doesn't work. The result is a infinite loop cycle.
Why? Because the only instanced object is the B class (in my example).

$this alwais refer to instanced object.

See also the "sal at stodge dot org" notes in 4 October 2002.
lelegiuly at iol dot it 06-Mar-2003 05:46
If you like Java and you have to call parent constructor, this is my method:

class object {
   function super() {
     $par = get_parent_class($this);
     $this->$par();
   }
}
class A extends object {
   function A() {
     echo "A constructor\n";
   }
}
class B extends A {
   function B() {
     $this->super();
     echo "B constructor\n";
   }
}

Include the classes above, instance a new B class and this is the output:

A constructor

B constructor

In your web application every class will extend the object class.
Good luck, Emanuele :)
Dan 02-Mar-2003 02:03
I agree with "anonymous coward". That syntax seems silly in a constructor and only results in an extra function being called. The only time the parent will ever change is if you change the extends expression in your class definition.

However, the parent:: syntax is very useful when you need to add extra functionality to a method of a child class which is already defined in the parent class.

Example:

class foo {
   var $prop;

   function foo($prop = 1) {$this->prop = $prop;}

   function SetProp($prop) {$this->prop = $prop;}
}

class bar extends foo {
   var $lastprop;

   function bar($prop = NULL) {
      is_null($prop) ? $this->foo() : $this->foo($prop);
   }

   function SetProp($prop) {
      $this->lastprop = $this->prop;
      parent::SetProp($prop);
   }
}
anonymous at cowards dot com 07-Dec-2002 09:48
Um, reading through all the gyrations on getting C++ constructor
semantics from PHP, I am wondering if I am just blindly missing something
when I use:

class A {
    function A () {  echo "A Constructor\n"; }
}

class B extends A {
    function B () { $this->A (); echo "B Constructor\n"; }
}

class C extends B {
    function C () { $this->B (); echo "C Constructor\n"; }
}

$c = new C ();

produces:

A Constructor
B Constructor
C Constructor

I'm new to PHP, but it seems to me that this is simpler and cleaner
than the solutions discussed here, with the only disadvantage being that
when you change the name of your superclass you have to change one
line in your subclass.

What am I missing? Are there advanced PHP-OO considerations that
make this code undesireable??
aid at logic dot org dot uk 05-Dec-2002 09:27
With regard to the chaining of Constrcutures, surely this is a simple and replaible method..?

<?php
class A
  {
  function A()
    {
    print "Constructor A<br>\n" ;
    }
  }

class B extends A
  {
  function B()
    {
    parent::A () ;
    print "Constructor B<br>\n" ;
    }
  }

class C extends B
  {
  function C()
    {
    parent::B ();
    print "Constructor C<br>\n" ;
    }
  }

$o = new C;
?>
Which outputs,

Constructor A
Constructor B
Constructor C
sal at stodge dot org 04-Oct-2002 06:57
<p>When using the technique suggested by "<b>minoc at mindspring dot com</b>" on this page - USE EXTREME CAUTION.</p>

The problem is that if you inherit of a class that uses this kludge in the constructor you will end up with a recursive constructor - unless that new class has a constructor of it's own that by-passes the kludged constructor.

What I am saying, is you can use this technique as long as you can ensure that each class you inherit from a class containing this technique in the constructor has a constructor class of it's own that does not call the parent constructor...

On balance it is probably easier to specify the name of the constructor class explicitly!

If you make this mistake the symptoms are pretty easy to spot - PHP spirals into an infinitely recursive loop as soon as you attempt to construct a new class. Your web-server will returns an empty document.
mwwaygoo at hotmail dot com 20-Sep-2002 05:54
With the example given previous from johnremovethreewordsplatte@pobox.com

You should only do this if your initialisation is different (no point in extending if it isn't)

When you extend a class its constructor still remains valid and is executed when constructed.  Writing a new constructor in the extended class overwrites the old one - inheritance.

so the following will also work

class Superclass {
function Superclass($arg) {
$this->initialize($arg);
}
function initialize($arg) {
$this->setArg($arg);
}
}

class Subclass extends Superclass {
//initialize() is inherited:
//BUT so is constructor

function extra_stuff($arg) {
echo "extra stuff " . $arg;

}
}

THE constructor is the function who's name is the same as the class that it is defined within.
AND remains the constructor even in extended classes, until redefined.

Think of the constructor as a seperate function from all the others.  As if PHP looks at your class and makes a copy of the function of the same name as the class and calls it "Constructor".

You can also create a function within the extended class with the same name as the constructor in the parent class without re-writing the constructor.  Because you are not altering the "Constructor" function.
(BUT any redefined variables are used in the constructor, as expected)

ie
class One
{
  var $v = "one";

  function One()
  {
    print "Class One::Constructor value=" . $this->v . "<br />\n";
  }
}

class Two extends One
{
  var $v = "two";

  function one()
  {
    print "REDEFINED : Class Two::Function One value=" . $this->v . "<br />\n";
  }
}

$o = new One();
$o->one();
$p = new Two();
$p->one();

results in :-
Class One::Constructor value=one
Class One::Constructor value=one
Class One::Constructor value=two
REDEFINED : Class Two::Function One value=two
johnremovethreewordsplatte at pobox dot com 13-Aug-2002 02:16
Combining the above suggestions with the "Pull Up Constructor Body" refactoring from Fowler (http://www.refactoring.com/catalog/pullUpConstructorBody.html), I found an elegant way to inherit constructors in PHP. Both superclass and subclass constructors call an initialize() method, which can be inherited or overridden. For simple cases (which almost all of mine are), no calls to parent::anything are necessary using this technique. The resulting code is easy to read and understand:

class Superclass {
    function Superclass($arg) {
        $this->initialize($arg);
    }
    function initialize($arg) {
        $this->setArg($arg);
    }
}

class Subclass extends Superclass {
    function Subclass($arg) {
        //initialize() is inherited:
        $this->initialize($arg);
    }
}
gibby at onebox dot com 15-Jun-2002 01:25
Regarding the example above, I believe the result is misleading, if you're inferring that the $this in Class One's OneDo method refers to One.

If that wasn't your conclusion, then my apologies-- I misunderstood you.

But if it was, it might save trouble to note that $this is effectively an alias for $o (a Class Two object).  $v is available to $o, and that's how it's being picked up.  $this is $o in that example, not One (One is a class and has no implementation).

I hope that's helpful.

Brad
seedpod23 at hotmail dot com 29-May-2002 08:19
This example might illustrate something which was unclear to me when using the $parent::method() syntax.

I didn't see any documentation on whether or not $this (object variables) would be available in the parent class method so I wrote this example:

class One {

  var $v = "one";

  function OneDo() {
    print "One::OneDo " . $this->v . "<br>\n";
  }
}

class Two extends One {

  function CallParent() {
    parent::OneDo();
  }
}

$o = new Two();

$o->CallParent();

The output is "One::OneDo one", happy day!
php at earthside dot arg 09-Apr-2002 07:41
Here is the workaround I came up with based on the comments about using a 'constructor' method and the parent:: syntax...

#cat test.php
<?php
class foo {
  function foo() { $this->constructor(); }
  function constructor() {
    echo get_class($this)." (foo)\n";
  }
}
class bar extends foo {
  function bar() { $this->constructor(); }
  function constructor() {
    echo get_class($this)." (bar)\n";
    parent::constructor();
  }
}
class baz extends bar {
  function & baz() { $this->constructor(); }
  function & constructor() {
    echo get_class($this)." (baz)\n";
    parent::constructor();
  }
}
$o = new baz;
?>
#php test.php
X-Powered-By: PHP/4.1.2
Content-type: text/html

baz (baz)
baz (bar)
baz (foo)
#

--
'a' -> 'o' in email TLD
tim dot parkinson at ntlworld dot com 28-Jul-2001 02:37
There is an implication here that variables of the parent class are available.  You can't access variables using parent::
venome at gmx dot net 25-Jun-2001 05:57
If you have a complex class hierarchy, I find that it's a good idea to have a function constructor() in every class, and the 'real' php constructor only exists in the absolute base class.

From the basic constructor, you call $this->constructor(). Now descendants simply have to call parent::constructor() in their own constructor, which eliminates complicated parent calls.


Serializing objects - objects in sessions

serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP. unserialize() can use this string to recreate the original variable values. Using serialize to save an object will save all variables in an object. The functions in an object will not be saved, only the name of the class.

In order to be able to unserialize() an object, the class of that object needs to be defined. That is, if you have an object $a of class A on page1.php and serialize this, you'll get a string that refers to class A and contains all values of variabled contained in $a. If you want to be able to unserialize this on page2.php, recreating $a of class A, the definition of class A must be present in page2.php. This can be done for example by storing the class definition of class A in an include file and including this file in both page1.php and page2.php.

<?php
// classa.inc:

  class A {
      var $one = 1;

      function show_one() {
          echo $this->one;
      }
  }

// page1.php:

  include("classa.inc");

  $a = new A;
  $s = serialize($a);
  // store $s somewhere where page2.php can find it.
  $fp = fopen("store", "w");
  fwrite($fp, $s);
  fclose($fp);

// page2.php:

  // this is needed for the unserialize to work properly.
  include("classa.inc");

  $s = implode("", @file("store"));
  $a = unserialize($s);

  // now use the function show_one() of the $a object.
  $a->show_one();
?>

If you are using sessions and use session_register() to register objects, these objects are serialized automatically at the end of each PHP page, and are unserialized automatically on each of the following pages. This basically means that these objects can show up on any of your pages once they become part of your session.

It is strongly recommended that you include the class definitions of all such registered objects on all of your pages, even if you do not actually use these classes on all of your pages. If you don't and an object is being unserialized without its class definition being present, it will lose its class association and become an object of class __PHP_Incomplete_Class_Name without any functions available at all, that is, it will become quite useless.

So if in the example above $a became part of a session by running session_register("a"), you should include the file classa.inc on all of your pages, not only page1.php and page2.php.

User Contributed Notes

There are no user contributed notes for this page.


The magic functions __sleep and __wakeup

serialize() checks if your class has a function with the magic name __sleep. If so, that function is being run prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn't return anything then NULL is serialized and E_NOTICE is issued.

The intended use of __sleep is to commit pending data or perform similar cleanup tasks. Also, the function is useful if you have very large objects which need not be saved completely.

Conversely, unserialize() checks for the presence of a function with the magic name __wakeup. If present, this function can reconstruct any resources that object may have.

The intended use of __wakeup is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.

User Contributed Notes

Ninsuo 06-Dec-2014 10:50
If you want to serialize all but one properties, you can use:

<?php

class A
{

   protected $a = 1;
   protected $b = 2;
   protected $c = 3;
   protected $d = 4; // unwanted

    public function __sleep()
    {
        return array_diff(array_keys(get_object_vars($this)), array('d'));
    }

}
Ninsuo 06-Dec-2014 10:34
If you want to serialize all but one properties, you can use:

<?php

class MyClass
{
   protected $a;
   protected $b;
   protected $c;
   protected $d; // unwanted

    public function __sleep()
    {
        return array_diff(get_object_vars($this), array('d'));
    }

}
ranware2200 at yahoo dot com 27-Jan-2011 09:27
This is a simple example of how to implement serialization using the __sleep and __wakeup magic methods...

<?php
//student.class.php
class Student{
    private $full_name = '';
    private $score = 0;
    private $grades = array();

    public function __construct($full_name, $score, $grades)
    {
        $this->full_name = $full_name;
        $this->grades = $grades;
        $this->score = $score;
    }

    public function show()
    {
        echo $this->full_name;
    }

    function __sleep()
    {
        echo 'Going to sleep...';
        return array('full_name', 'grades', 'score');
    }

    function __wakeup()
    {
        echo 'Waking up...';
    }
}
?>

<?php
//a.php
include 'student.class.php';

$student = new Student('bla bla', 'a', array('a' => 90, 'b' => 100));
$student->show();
echo "<br />\n";
$s = serialize($student);
echo $s ."<br />\n";
$fp = fopen('./uploads/session.s', 'w');
fwrite($fp, $s);
fclose($fp);
?>

<?php
//b.php
include 'student.class.php';

$s = implode('', file("./uploads/session.s"));
echo $s ."<br />\n";
$a = unserialize($s);
$a->show();
?>


References inside the constructor

Creating references within the constructor can lead to confusing results. This tutorial-like section helps you to avoid problems.

<?php
class Foo {
    function Foo($name) {
        // create a reference inside the global array $globalref
        global $globalref;
        $globalref[] = &$this;
        // set name to passed value
        $this->setName($name);
        // and put it out
        $this->echoName();
    }

    function echoName() {
        echo "<br />", $this->name;
    }

    function setName($name) {
        $this->name = $name;
    }
}
?>

Let us check out if there is a difference between $bar1 which has been created using the copy = operator and $bar2 which has been created using the reference =& operator...

<?php
$bar1 = new Foo('set in constructor');
$bar1->echoName();
$globalref[0]->echoName();

/* output:
set in constructor
set in constructor
set in constructor */

$bar2 =& new Foo('set in constructor');
$bar2->echoName();
$globalref[1]->echoName();

/* output:
set in constructor
set in constructor
set in constructor */
?>

Apparently there is no difference, but in fact there is a very significant one: $bar1 and $globalref[0] are _NOT_ referenced, they are NOT the same variable. This is because "new" does not return a reference by default, instead it returns a copy.

Note: There is no performance loss (since PHP 4 and up use reference counting) returning copies instead of references. On the contrary it is most often better to simply work with copies instead of references, because creating references takes some time where creating copies virtually takes no time (unless none of them is a large array or object and one of them gets changed and the other(s) one(s) subsequently, then it would be wise to use references to change them all concurrently).

To prove what is written above let us watch the code below.
<?php
// now we will change the name. what do you expect?
// you could expect that both $bar1 and $globalref[0] change their names...
$bar1->setName('set from outside');

// as mentioned before this is not the case.
$bar1->echoName();
$globalref[0]->echoName();

/* output:
set from outside
set in constructor */

// let us see what is different with $bar2 and $globalref[1]
$bar2->setName('set from outside');

// luckily they are not only equal, they are the same variable
// thus $bar2->name and $globalref[1]->name are the same too
$bar2->echoName();
$globalref[1]->echoName();

/* output:
set from outside
set from outside */
?>

Another final example, try to understand it.

<?php
class A {
    function A($i) {
        $this->value = $i;
        // try to figure out why we do not need a reference here
        $this->b = new B($this);
    }

    function createRef() {
        $this->c = new B($this);
    }

    function echoValue() {
        echo "<br />","class ",get_class($this),': ',$this->value;
    }
}

class B {
    function B(&$a) {
        $this->a = &$a;
    }

    function echoValue() {
        echo "<br />","class ",get_class($this),': ',$this->a->value;
    }
}

// try to understand why using a simple copy here would yield
// in an undesired result in the *-marked line
$a =& new A(10);
$a->createRef();

$a->echoValue();
$a->b->echoValue();
$a->c->echoValue();

$a->value = 11;

$a->echoValue();
$a->b->echoValue(); // *
$a->c->echoValue();

?>

위 예제의 출력:

class A: 10
class B: 10
class B: 10
class A: 11
class B: 11
class B: 11

User Contributed Notes

There are no user contributed notes for this page.


Comparing objects

In PHP 4, objects are compared in a very simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class. Similar rules are applied when comparing two objects using the identity operator (===).

If we were to execute the code in the example below:

Example #1 Example of object comparison in PHP 4

<?php
function bool2str($bool) {
    if ($bool === false) {
            return 'FALSE';
    } else {
            return 'TRUE';
    }
}

function compareObjects(&$o1, &$o2) {
    echo 'o1 == o2 : '.bool2str($o1 == $o2)."\n";
    echo 'o1 != o2 : '.bool2str($o1 != $o2)."\n";
    echo 'o1 === o2 : '.bool2str($o1 === $o2)."\n";
    echo 'o1 !== o2 : '.bool2str($o1 !== $o2)."\n";
}

class Flag {
    var $flag;

    function Flag($flag=true) {
            $this->flag = $flag;
    }
}

class SwitchableFlag extends Flag {

    function turnOn() {
        $this->flag = true;
    }

    function turnOff() {
        $this->flag = false;
    }
}

$o = new Flag();
$p = new Flag(false);
$q = new Flag();

$r = new SwitchableFlag();

echo "Compare instances created with the same parameters\n";
compareObjects($o, $q);

echo "\nCompare instances created with different parameters\n";
compareObjects($o, $p);

echo "\nCompare an instance of a parent class with one from a subclass\n";
compareObjects($o, $r);
?>

위 예제의 출력:

Compare instances created with the same parameters
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE

Compare instances created with different parameters
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE

Compare an instance of a parent class with one from a subclass
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE
Which is the output we will expect to obtain given the comparison rules above. Only instances with the same values for their attributes and from the same class are considered equal and identical.

Even in the cases where we have object composition, the same comparison rules apply. In the example below we create a container class that stores an associative array of Flag objects.

Example #2 Compound object comparisons in PHP 4

<?php
class FlagSet {
    var $set;

    function FlagSet($flagArr = array()) {
        $this->set = $flagArr;
    }

    function addFlag($name, $flag) {
        $this->set[$name] = $flag;
    }

    function removeFlag($name) {
        if (array_key_exists($name, $this->set)) {
            unset($this->set[$name]);
        }
    }
}

$u = new FlagSet();
$u->addFlag('flag1', $o);
$u->addFlag('flag2', $p);
$v = new FlagSet(array('flag1'=>$q, 'flag2'=>$p));
$w = new FlagSet(array('flag1'=>$q));

echo "\nComposite objects u(o,p) and v(q,p)\n";
compareObjects($u, $v);

echo "\nu(o,p) and w(q)\n";
compareObjects($u, $w);
?>

위 예제의 출력:

Composite objects u(o,p) and v(q,p)
o1 == o2 : TRUE
o1 != o2 : FALSE
o1 === o2 : TRUE
o1 !== o2 : FALSE

u(o,p) and w(q)
o1 == o2 : FALSE
o1 != o2 : TRUE
o1 === o2 : FALSE
o1 !== o2 : TRUE

User Contributed Notes

There are no user contributed notes for this page.


PHP에서 디버깅

Table of Contents

  • PHP에서 디버깅에 대하여

User Contributed Notes

netyou 27-Sep-2015 04:29
I find it very useful to print out to the browsers console instead of just var_dumping:

function console_log( $data ){
  echo '<script>';
  echo 'console.log('. json_encode( $data ) .')';
  echo '</script>';
}

Usage:
$myvar = array(1,2,3);
console_log( $myvar ); // [1,2,3]
skatebiker at gmail dot com 05-Dec-2014 10:24
p_r() is a function for logging variable values.

In this example the function p_r() does only log when the URL parameter d=<nonzero> is set. Reset it by d=0.
When  the parameter is a valid filename (relative to the script's path) it will be logged to that file rather than to the browser.

[code]
@session_start();
// debug
if (isset($_GET['d']))
{
  $_SESSION['d'] = $_GET['d'];
}

if (@$_SESSION['d']) {

function p_r($exp)
{
  $res = "";
  $trace = debug_backtrace();
  $level = 0;
  $e = error_reporting(E_ALL&~E_NOTICE);
  $file = strrpos($trace[$level]['file'], "/");
  $file = substr($trace[$level]['file'],$file+1);
  $line = date("H:i:s"). " " . $file . ' ' . $trace[$level]['line'] . ' ' . $trace[$level+1]['function'] . "()";
  $e = error_reporting($e);
  if (!is_string($exp)) $exp = var_export($exp,1);
  if (substr($_SESSION["d"],-4)==".log") {
    file_put_contents ($_SESSION["d"],$line . ": ". $exp . "\n",  FILE_APPEND);
  } else {
    $res = $line . "\n<pre>".htmlentities($exp). "</pre>\n";
    echo $res;
  }
  return $res;
}

    // refresh to prevent timeout
  $a = $_SESSION['d'];
  $_SESSION['d'] = $a;
  error_reporting (E_ALL);

} else {
  function  p_r() {}
}  // end if debug

[/code]
skunkbad 17-Feb-2014 03:47
I am a firm believer in the Firephp debugger. It works with Firefox and Firebug to allow you to see the value of any string, array, or object. The best part of it is that it will not interrupt the actual browser output, so you can see the output as it was intended to be seen.

For those who prefer Google's Chome browser, there is something called ChromePHP which is similar, but the way Firephp displays the values in the console is better, plus Firebug itself is an almost priceless development tool.

So, if you're looking for a great debugger, check out Firephp. After you use it you will feel naked if it's not available.
Anonymous 02-Nov-2011 10:36
If anyone's trying to actually set up the official debugger from Zend (http://www.zend.com/en/products/studio/downloads) with PHP 5.3.8, you'll notice the zip only contains the nts (non-thread-safe) version of the debugger for PHP 5.3.x. Try as you might, it just doesn't seem to work with the tread-safe version of PHP 5.3.8, so for Windows at least I found you'll also need to have the NON-THREAD-SAFE version of PHP installed.
dcz at phpbb-seo dot com 18-Mar-2011 11:32
Here my little contribution for a simple yet handy debug function :
<?php
/**
* dbug (mixed $expression [, mixed $expression [, $... ]])
* Author : dcz
* Feel free to use as you wish at your own risk ;-)
*/
function dbug() {
    static $output = '', $doc_root;
    $args = func_get_args();
    if (!empty($args) && $args[0] === 'print') {
        $_output = $output;
        $output = '';
        return $_output;
    }
    // do not repeat the obvious (matter of taste)
    if (!isset($doc_root)) {
        $doc_root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
    }
    $backtrace = debug_backtrace();
    // you may want not to htmlspecialchars here
    $line = htmlspecialchars($backtrace[0]['line']);
    $file = htmlspecialchars(str_replace(array('\\', $doc_root), array('/', ''), $backtrace[0]['file']));
    $class = !empty($backtrace[1]['class']) ? htmlspecialchars($backtrace[1]['class']) . '::' : '';
    $function = !empty($backtrace[1]['function']) ? htmlspecialchars($backtrace[1]['function']) . '() ' : '';
    $output .= "<b>$class$function =&gt;$file #$line</b><pre>";
    ob_start();
    foreach ($args as $arg) {
        var_dump($arg);
    }
    $output .= htmlspecialchars(ob_get_contents(), ENT_COMPAT, 'UTF-8');
    ob_end_clean();
    $output .= '</pre>';
}
?>

usage :
<?php
dbug($scalar, $array, $object, $resource, CONSTANT);
//..
dbug($other);
//..
echo dbug('print'); // actually output the result of all previous calls
// looks like :
// class::method() =>/path/from/doc/root/file.php #line
// var_dump result

?>

I found it handy not to directly output result data because this makes it possible to debug variables before headers are sent (useful for pre sessions start code for example).
turaz dot w4l at gmail dot com 06-Aug-2009 01:14
I usually use this simple function in combo with a die(); in order to have on screen the value of a variable or array:

<?php
function debug_view ( $what ) {
    echo '<pre>';
    if ( is_array( $what ) )  {
        print_r ( $what );
    } else {
        var_dump ( $what );
    }
    echo '</pre>';
}
?>
Diego 18-Feb-2009 01:21
If you don't find a syntax error, you can comment out a block where you assume the error (or put it out of the document by [ctrl] + [X], but keep a copy on your HD for the case, your computer crashes) and check, if the syntax error is still there.
If not, it must be anywhere in your commented text; if yes, it must be somewhere else.
If you want to locate the error better, do it again with an other and/or smaller piece of code, till you get it.
online _ use _ only == hotmail.com 16-Mar-2006 12:41
I still find that printing out variable values at problem points in the code is one of the easiest ways for me to debug.  If you're interested in knowing the full contents of an object/array/scalar, then use

var_dump($var).


PHP에서 디버깅에 대하여

PHP 5.6은 phpdbg 인터랙티브 디버거를 탑재하고 있습니다. PHP 5.6 미만 버전은 내부 디버깅 기능을 가지고 있지 않습니다. 다음의 외부 디버거를 사용할 수 있습니다. » Zend IDE는 디버거를 포함하고 있고, 다음과 같은 많은 무료 디버거가 존재합니다. » http://www.php-debugger.com/dbg/의 DBG, » Advanced PHP Debugger (APD), » Xdebug.

User Contributed Notes

1UnitedPower 26-Jan-2014 04:14
PHP 5.6.0 alpha finally ships with an interactive debugger called phpdbg.

Release-Documentation: http://docs.php.net/manual/en/migration56.new-features.php#migration56.new-features.phpdbg

phpdbg project page: http://phpdbg.com/


Configure options

Table of Contents

  • List of core configure options

User Contributed Notes

jovcic ate gseis stop ucla stop edu 09-Jul-2005 03:26
If you'e compiling a shared extension (e.g. pspell) whose libs & headers are not in the usual place, use the following:

--with-pspell=shared,/my/custom/path/to/pspell/

Took me forever to figure out, since Google had no answer for me.
squeeNoSpam at NoSpamAddress dot com 14-Aug-2002 09:06
[Editor's note: The reason why some general options for the "configure" scripts are not documented is that said script is part of the GNU autoconf tools, and as such its general options are documented elsewhere, e.g.: http://www.gnu.org/manual/autoconf/index.html and http://www.airs.com/ian/configure/. Some are also documented in the INSTALL file that comes with the source distribution.]

The --prefix=PREFIX configure option isn't mentioned here. It apparently defaults to /usr/local, and sets the position of the php includes and libs. (in PREFIX/include/php and PREFIX/lib/php)
ceo at l-i-e dot com 14-Aug-2001 09:05
--enable-sigchild is only relevent to users of Oracle who are having <defunc> processes.


List of core configure options

Below is a partial list of configure options used by the PHP configure scripts when compiling in Unix-like environments. Most configure options are listed in their appropriate locations on the extension reference pages and not here. For a complete up-to-date list of configure options, run ./configure --help in your PHP source directory after running autoconf (see also the Installation chapter). You may also be interested in reading the » GNU configure documentation for information on additional configure options such as --prefix=PREFIX.

Note:

These are only used at compile time. If you want to alter PHP's runtime configuration, please see the chapter on Runtime Configuration.

  • Miscellaneous
  • PHP Behaviour
  • Server

Configure Options in PHP

Misc options

--enable-debug

Compile with debugging symbols.

--with-layout=TYPE

Sets how installed files will be laid out. Type is one of PHP (default) or GNU.

--with-pear=DIR

Install PEAR in DIR (default PREFIX/lib/php).

--without-pear

Do not install PEAR.

--enable-sigchild

Enable PHP's own SIGCHLD handler.

--disable-rpath

Disable passing additional runtime library search paths.

--enable-libgcc

Enable explicitly linking against libgcc.

--enable-php-streams

Include experimental PHP streams. Do not use unless you are testing the code!

--with-zlib-dir[=DIR]

Define the location of zlib install directory.

--with-tsrm-pthreads

Use POSIX threads (default).

--enable-shared[=PKGS]

Build shared libraries [default=yes].

--enable-static[=PKGS]

Build static libraries [default=yes].

--enable-fast-install[=PKGS]

Optimize for fast installation [default=yes].

--with-gnu-ld

Assume the C compiler uses GNU ld [default=no].

--disable-libtool-lock

Avoid locking (might break parallel builds).

--with-pic

Try to use only PIC/non-PIC objects [default=use both].

--enable-memory-limit

Compile with memory limit support. (not available since PHP 5.2.1 - always enabled)

--disable-url-fopen-wrapper

Disable the URL-aware fopen wrapper that allows accessing files via HTTP or FTP. (not available since PHP 5.2.5)

--enable-versioning

Export only required symbols. See INSTALL for more information.

PHP options

--enable-maintainer-mode

Enable make rules and dependencies not useful (and sometimes confusing) to the casual installer.

--with-config-file-path=PATH

Sets the path in which to look for php.ini, defaults to PREFIX/lib.

--enable-safe-mode

Enable safe mode by default.

Warning

이 기능은 PHP 5.3.0부터 배제되어, PHP 5.4.0부터 제거됩니다.

--with-exec-dir[=DIR]

Only allow executables in DIR when in safe mode defaults to /usr/local/php/bin.

Warning

이 기능은 PHP 5.3.0부터 배제되어, PHP 5.4.0부터 제거됩니다.

--enable-magic-quotes

Enable magic quotes by default.

Warning

이 기능은 PHP 5.3.0부터 배제되어, PHP 5.4.0부터 제거됩니다.

--disable-short-tags

Disable the short-form <? start tag by default.

--enable-zend-multibyte

Enables multibyte code in the language parser and scanner to be executed. When PHP is compiled with this option, it also enables the encoding directive in the declare construct.

Warning

이 기능은 PHP 5.3.0부터 배제되어, PHP 5.4.0부터 제거됩니다.

--with-libdir

Specifies the directory where the libraries to build PHP exists on a Unix system. For 64bit systems, its needed to specify this argument to the lib64 directory like: --with-libdir=lib64.

SAPI options

The following list contains the available SAPI&s (Server Application Programming Interface) for PHP.

--with-aolserver=DIR

Specify path to the installed AOLserver.

--with-apxs[=FILE]

Build shared Apache module. FILE is the optional pathname to the Apache apxs tool; defaults to apxs. Make sure you specify the version of apxs that is actually installed on your system and NOT the one that is in the apache source tarball.

--with-apache[=DIR]

Build a static Apache module. DIR is the top-level Apache build directory, defaults to /usr/local/apache.

--with-mod_charset

Enable transfer tables for mod_charset (Russian Apache).

--with-apxs2[=FILE]

Build shared Apache 2.0 module. FILE is the optional pathname to the Apache apxs tool; defaults to apxs.

--with-caudium=DIR

Build PHP as a Pike module for use with Caudium. DIR is the Caudium server dir, with the default value /usr/local/caudium/server.

--disable-cli

Disable building the CLI version of PHP (this forces --without-pear). More information is available in the section about Using PHP from the command line.

--enable-phpdbg

Enable phpdbg interactive debugger SAPI module support in PHP 5.6.x or later.

--enable-embed[=TYPE]

Enable building of the embedded SAPI library. TYPE is either shared or static, which defaults to shared.

--with-isapi=DIR

Build PHP as an ISAPI module for use with Zeus.

--with-nsapi=DIR

Specify path to the installed Netscape/iPlanet/SunONE Webserver.

--with-phttpd=DIR

No information yet.

--with-pi3web=DIR

Build PHP as a module for use with Pi3Web.

--with-roxen=DIR

Build PHP as a Pike module. DIR is the base Roxen directory, normally /usr/local/roxen/server.

--enable-roxen-zts

Build the Roxen module using Zend Thread Safety.

--with-servlet[=DIR]

Include servlet support. DIR is the base install directory for the JSDK. This SAPI requires the java extension must be built as a shared dl.

--with-thttpd=SRCDIR

Build PHP as thttpd module.

--with-tux=MODULEDIR

Build PHP as a TUX module (Linux only).

--with-webjames=SRCDIR

Build PHP as a WebJames module (RISC OS only)

--disable-cgi

Disable building CGI version of PHP.

As of PHP 5.3.0 this argument enables FastCGI which previously had to be enabled using --enable-fastcgi.

--enable-force-cgi-redirect

Enable the security check for internal server redirects. You should use this if you are running the CGI version with Apache.

As of PHP 5.3.0 this argument is enabled by default and no longer exists. To disable this, the cgi.force_redirect ini directive should be set to 0.

--enable-discard-path

If this is enabled, the PHP CGI binary can safely be placed outside of the web tree and people will not be able to circumvent .htaccess security.

As of PHP 5.3.0 this argument is disabled by default and no longer exists. To enable this feature the cgi.discard_path ini directive must be set to 1.

--enable-fastcgi

If this is enabled, the CGI module will be built with support for FastCGI also.

As of PHP 5.3.0 this argument no longer exists and is enabled by --enable-cgi instead.

--disable-path-info-check

If this is disabled, paths such as /info.php/test?a=b will fail to work. For more information see the » Apache Manual.

User Contributed Notes

There are no user contributed notes for this page.


php.ini directives

Table of Contents

  • List of php.ini directives
  • List of php.ini sections
  • Description of core php.ini directives

User Contributed Notes

There are no user contributed notes for this page.


List of php.ini directives

This list includes the php.ini directives you can set to configure your PHP setup.

The "Changeable" column shows the modes determining when and where a directive may be set. See the Changeable mode values section for their definitions.

Configuration options
이름 | 기본값 | 가변성 | 변경점
allow_call_time_pass_reference | "1" | PHP_INI_PERDIR | PHP_INI_ALL in PHP 4.0.0. Removed in PHP 5.4.0.
allow_url_fopen | "1" | PHP_INI_SYSTEM | PHP_INI_ALL in PHP <= 4.3.4.
allow_url_include | "0" | PHP_INI_SYSTEM | Available since PHP 5.2.0.
always_populate_raw_post_data | "0" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3. Available since PHP 4.1.0.
apc.cache_by_default | "1" | PHP_INI_ALL | PHP_INI_SYSTEM in APC <= 3.0.12. Available since APC 3.0.0.
apc.enabled | "1" | PHP_INI_SYSTEM | PHP_INI_SYSTEM in APC 2. PHP_INI_ALL in APC <= 3.0.12.
apc.enable_cli | "0" | PHP_INI_SYSTEM | Available since APC 3.0.7.
apc.file_update_protection | "2" | PHP_INI_SYSTEM | Available since APC 3.0.6.
apc.filters | NULL | PHP_INI_SYSTEM |
apc.gc_ttl | "3600" | PHP_INI_SYSTEM |
apc.include_once_override | "0" | PHP_INI_SYSTEM | Available since APC 3.0.12.
apc.localcache | "0" | PHP_INI_SYSTEM | Available since APC 3.0.14.
apc.localcache.size | "512" | PHP_INI_SYSTEM | Available since APC 3.0.14.
apc.max_file_size | "1M" | PHP_INI_SYSTEM | Available since APC 3.0.7.
apc.mmap_file_mask | NULL | PHP_INI_SYSTEM |
apc.num_files_hint | "1000" | PHP_INI_SYSTEM |
apc.optimization | "0" | PHP_INI_ALL | PHP_INI_SYSTEM in APC 2. Removed in APC 3.0.13.
apc.report_autofilter | "0" | PHP_INI_SYSTEM | Available since APC 3.0.11.
apc.rfc1867 | "0" | PHP_INI_SYSTEM | Available since APC 3.0.13.
apc.rfc1867_freq | "0" | PHP_INI_SYSTEM |
apc.rfc1867_name | "APC_UPLOAD_PROGRESS" | PHP_INI_SYSTEM |
apc.rfc1867_prefix | "upload_" | PHP_INI_SYSTEM |
apc.shm_segments | "1" | PHP_INI_SYSTEM |
apc.shm_size | "30" | PHP_INI_SYSTEM |
apc.slam_defense | "0" | PHP_INI_SYSTEM | Available since APC 3.0.0.
apc.stat | "1" | PHP_INI_SYSTEM | Available since APC 3.0.10.
apc.stat_ctime | "0" | PHP_INI_SYSTEM | Available since APC 3.0.13.
apc.ttl | "0" | PHP_INI_SYSTEM | Available since APC 3.0.0.
apc.user_entries_hint | "4096" | PHP_INI_SYSTEM | Available since APC 3.0.0.
apc.user_ttl | "0" | PHP_INI_SYSTEM | Available since APC 3.0.0.
apc.write_lock | "1" | PHP_INI_SYSTEM | Available since APC 3.0.11.
apd.bitmask | "0" | PHP_INI_ALL | Removed in apd 0.9.
apd.dumpdir | NULL | PHP_INI_ALL |
apd.statement_tracing | "0" | PHP_INI_ALL | Available since apd 0.9.
arg_separator.input | "&" | PHP_INI_PERDIR | Available since PHP 4.0.5.
arg_separator.output | "&" | PHP_INI_ALL | Available since PHP 4.0.5.
asp_tags | "0" | PHP_INI_PERDIR | PHP_INI_ALL in PHP 4.0.0.
assert.active | "1" | PHP_INI_ALL |
assert.bail | "0" | PHP_INI_ALL |
assert.callback | NULL | PHP_INI_ALL |
assert.exception | "0" | PHP_INI_ALL | Available since PHP 7.0.0.
assert.quiet_eval | "0" | PHP_INI_ALL |
assert.warning | "1" | PHP_INI_ALL |
auto_append_file | NULL | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3.
auto_detect_line_endings | "0" | PHP_INI_ALL | Available since PHP 4.3.0.
auto_globals_jit | "1" | PHP_INI_PERDIR | Available since PHP 5.0.0.
auto_prepend_file | NULL | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3.
axis2.client_home | "~/work/axisc/c/deply" | PHP_INI_ALL |
axis2.enable_exception | "1" | PHP_INI_ALL |
axis2.enable_trace | "1" | PHP_INI_ALL |
axis2.log_path | "/tmp" | PHP_INI_ALL |
bcmath.scale | "0" | PHP_INI_ALL |
bcompiler.enabled | "1" | PHP_INI_ALL | Available since bcompiler 0.8.
blenc.key_file | "/usr/local/etc/blenckeys" | PHP_INI_ALL |
browscap | NULL | PHP_INI_SYSTEM |
cgi.check_shebang_line | "1" | PHP_INI_SYSTEM | Available since PHP 5.2.1.
cgi.discard_path | "0" | PHP_INI_SYSTEM | Available since PHP 5.3.0.
cgi.fix_pathinfo | "1" | PHP_INI_SYSTEM | PHP_INI_ALL in PHP <= 5.2.0. Available since PHP 4.3.0.
cgi.force_redirect | "1" | PHP_INI_SYSTEM | PHP_INI_ALL in PHP <= 5.2.0. Available since PHP 4.2.0.
cgi.nph | "0" | PHP_INI_ALL | Available since PHP 4.3.5.
cgi.redirect_status_env | NULL | PHP_INI_SYSTEM | PHP_INI_ALL in PHP <= 5.2.0. Available since PHP 4.2.0.
cgi.rfc2616_headers | "0" | PHP_INI_ALL | Available since PHP 4.3.0.
child_terminate | "0" | PHP_INI_ALL | Available since PHP 4.0.5.
cli.pager | "" | PHP_INI_ALL | Available since PHP 5.4.0.
cli.prompt | "\\b \\> " | PHP_INI_ALL | Available since PHP 5.4.0.
cli_server.color | "0" | PHP_INI_ALL | Available since PHP 5.4.0.
coin_acceptor.autoreset | "On" | PHP_INI_ALL | Removed in coin_acceptor 0.2.
coin_acceptor.auto_initialize | "Off" | PHP_INI_ALL | Available since coin_acceptor 0.2.
coin_acceptor.auto_reset | "On" | PHP_INI_ALL | Available since coin_acceptor 0.2.
coin_acceptor.command_function | "Off" | PHP_INI_ALL | Available since coin_acceptor 0.3.
coin_acceptor.delay | "53132" | PHP_INI_ALL | Removed in coin_acceptor 0.2.
coin_acceptor.delay_coins | "53132" | PHP_INI_ALL | Available since coin_acceptor 0.2.
coin_acceptor.delay_prom | "55748" | PHP_INI_ALL | Available since coin_acceptor 0.2.
coin_acceptor.device | "/dev/ttyS1" | PHP_INI_ALL | Removed in coin_acceptor 0.2.
coin_acceptor.lock_on_close | "Off" | PHP_INI_ALL | Available since coin_acceptor 0.2.
coin_acceptor.start_unlocked | "On" | PHP_INI_ALL | Available since coin_acceptor 0.2.
com.allow_dcom | "0" | PHP_INI_SYSTEM | Available since PHP 4.0.5.
com.autoregister_casesensitive | "1" | PHP_INI_ALL | PHP_INI_SYSTEM in PHP 4. Available since PHP 4.1.0.
com.autoregister_typelib | "0" | PHP_INI_ALL | PHP_INI_SYSTEM in PHP 4. Available since PHP 4.1.0.
com.autoregister_verbose | "0" | PHP_INI_ALL | PHP_INI_SYSTEM in PHP 4. Available since PHP 4.1.0.
com.code_page | "" | PHP_INI_ALL | Available since PHP 5.0.0.
com.typelib_file | "" | PHP_INI_SYSTEM | Available since PHP 4.0.5.
curl.cainfo | NULL | PHP_INI_SYSTEM | Available since PHP 5.3.7.
daffodildb.default_host | "localhost" | PHP_INI_ALL |
daffodildb.default_password | "daffodil" | PHP_INI_ALL |
daffodildb.default_socket | NULL | PHP_INI_ALL |
daffodildb.default_user | "DAFFODIL" | PHP_INI_ALL |
daffodildb.port | "3456" | PHP_INI_ALL |
date.default_latitude | "31.7667" | PHP_INI_ALL | Available since PHP 5.0.0.
date.default_longitude | "35.2333" | PHP_INI_ALL | Available since PHP 5.0.0.
date.sunrise_zenith | "90.583333" | PHP_INI_ALL | Available since PHP 5.0.0.
date.sunset_zenith | "90.583333" | PHP_INI_ALL | Available since PHP 5.0.0.
date.timezone | "" | PHP_INI_ALL | Available since PHP 5.1.0.
dba.default_handler | "" | PHP_INI_ALL | Available since PHP 4.3.3.
dbx.colnames_case | "unchanged" | PHP_INI_SYSTEM | Available since PHP 4.3.0. Removed in PHP 5.1.0.
default_charset | "UTF-8" | PHP_INI_ALL | Default to "UTF-8" since PHP >= 5.6.0, empty for PHP < 5.6.0.
default_mimetype | "text/html" | PHP_INI_ALL |
default_socket_timeout | "60" | PHP_INI_ALL | Available since PHP 4.3.0.
define_syslog_variables | "0" | PHP_INI_ALL | Removed in PHP 5.4.0.
detect_unicode | "1" | PHP_INI_ALL | Available since PHP 5.1.0. Renamed to zend.detect_unicode since PHP 5.4.0.
disable_classes | "" | php.ini only | Available since PHP 4.3.2.
disable_functions | "" | php.ini only | Available since PHP 4.0.1.
display_errors | "1" | PHP_INI_ALL |
display_startup_errors | "0" | PHP_INI_ALL | Available since PHP 4.0.3.
docref_ext | "" | PHP_INI_ALL | Available since PHP 4.3.2.
docref_root | "" | PHP_INI_ALL | Available since PHP 4.3.0.
doc_root | NULL | PHP_INI_SYSTEM |
enable_dl | "1" | PHP_INI_SYSTEM | 이 배제된 기능은 추후 제거될 예정입니다.
enable_post_data_reading | On | PHP_INI_PERDIR | Available since PHP 5.4.0.
engine | "1" | PHP_INI_ALL | Available since PHP 4.0.5.
error_append_string | NULL | PHP_INI_ALL |
error_log | NULL | PHP_INI_ALL |
error_prepend_string | NULL | PHP_INI_ALL |
error_reporting | NULL | PHP_INI_ALL |
exif.decode_jis_intel | "JIS" | PHP_INI_ALL | Available since PHP 4.3.0.
exif.decode_jis_motorola | "JIS" | PHP_INI_ALL | Available since PHP 4.3.0.
exif.decode_unicode_intel | "UCS-2LE" | PHP_INI_ALL | Available since PHP 4.3.0.
exif.decode_unicode_motorola | "UCS-2BE" | PHP_INI_ALL | Available since PHP 4.3.0.
exif.encode_jis | "" | PHP_INI_ALL | Available since PHP 4.3.0.
exif.encode_unicode | "ISO-8859-15" | PHP_INI_ALL | Available since PHP 4.3.0.
exit_on_timeout | "" | PHP_INI_ALL | Available since PHP 5.3.0
expect.logfile | "" | PHP_INI_ALL |
expect.loguser | "1" | PHP_INI_ALL |
expect.timeout | "10" | PHP_INI_ALL |
expose_php | "1" | php.ini only |
extension | NULL | php.ini only |
extension_dir | "/path/to/php" | PHP_INI_SYSTEM |
fastcgi.impersonate | "0" | PHP_INI_SYSTEM | PHP_INI_ALL in PHP <= 5.2.0. Available since PHP 4.3.0.
fastcgi.logging | "1" | PHP_INI_SYSTEM | PHP_INI_ALL in PHP <= 5.2.0. Available since PHP 4.4.0.
fbsql.allow_persistent | "1" | PHP_INI_SYSTEM | Available since PHP 4.2.0.
fbsql.autocommit | "1" | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.batchSize | "1000" | PHP_INI_SYSTEM | Available since PHP 4.2.0. Removed in PHP 5.1.0.
fbsql.batchsize | "1000" | PHP_INI_ALL | Available since PHP 5.1.0.
fbsql.default_database | "" | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.default_database_password | "" | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.default_host | NULL | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.default_password | "" | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.default_user | "_SYSTEM" | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.generate_warnings | "0" | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.max_connections | "128" | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.max_links | "128" | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.max_persistent | "-1" | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.max_results | "128" | PHP_INI_SYSTEM | Available since PHP 4.0.6.
fbsql.show_timestamp_decimals | "0" | PHP_INI_SYSTEM | Available since PHP 5.1.5.
file_uploads | "1" | PHP_INI_SYSTEM | PHP_INI_ALL in PHP <= 4.2.3. Available since PHP 4.0.3.
filter.default | "unsafe_raw" | PHP_INI_PERDIR | PHP_INI_ALL in filter <= 0.9.4. Available since PHP 5.2.0.
filter.default_flags | NULL | PHP_INI_PERDIR | PHP_INI_ALL in filter <= 0.9.4. Available since PHP 5.2.0.
from | "" | PHP_INI_ALL |
gd.jpeg_ignore_warning | "1" | PHP_INI_ALL | Available since PHP 5.1.3.
geoip.custom_directory | NULL | PHP_INI_ALL | Available since geoip 1.0.1.
geoip.database_standard | "GeoIP.dat" | PHP_INI_ALL | Removed in geoip 1.0.1.
hidef.ini_path | (char*)default_ini_path | PHP_INI_SYSTEM |
highlight.bg | "#FFFFFF" | PHP_INI_ALL | Removed in PHP 5.4.0.
highlight.comment | "#FF8000" | PHP_INI_ALL |
highlight.default | "#0000BB" | PHP_INI_ALL |
highlight.html | "#000000" | PHP_INI_ALL |
highlight.keyword | "#007700" | PHP_INI_ALL |
highlight.string | "#DD0000" | PHP_INI_ALL |
html_errors | "1" | PHP_INI_ALL | PHP_INI_SYSTEM in PHP <= 4.2.3. Available since PHP 4.0.2.
htscanner.config_file | ".htaccess" | PHP_INI_SYSTEM |
htscanner.default_docroot | "/" | PHP_INI_SYSTEM |
htscanner.default_ttl | "300" | PHP_INI_SYSTEM | Available since htscanner 0.6.0.
htscanner.stop_on_error | "0" | PHP_INI_SYSTEM | Available since htscanner 0.7.0.
http.allowed_methods | "" | PHP_INI_ALL | Available since pecl_http 0.4.0. Removed in pecl_http 1.0.0.
http.allowed_methods_log | "" | PHP_INI_ALL | Available since pecl_http 0.12.0. Removed in pecl_http 1.0.0.
http.cache_log | "" | PHP_INI_ALL | Available since pecl_http 0.8.0. Removed in pecl_http 1.0.0.
http.composite_log | "" | PHP_INI_ALL | Available since pecl_http 0.12.0. Removed in pecl_http 1.0.0.
http.etag.mode | "MD5" | PHP_INI_ALL | Available since pecl_http 1.0.0.
http.etag_mode | "MD5" | PHP_INI_ALL | Available since pecl_http 0.12.0. Removed in pecl_http 1.0.0.
http.force_exit | "1" | PHP_INI_ALL | Available since pecl_http 0.18.0.
http.log.allowed_methods | "" | PHP_INI_ALL | Available since pecl_http 1.0.0.
http.log.cache | "" | PHP_INI_ALL | Available since pecl_http 1.0.0.
http.log.composite | "" | PHP_INI_ALL | Available since pecl_http 1.0.0.
http.log.not_found | "" | PHP_INI_ALL | Available since pecl_http 1.0.0.
http.log.redirect | "" | PHP_INI_ALL | Available since pecl_http 1.0.0.
http.ob_deflate_auto | "0" | PHP_INI_PERDIR | Available since pecl_http 0.21.0. Removed in pecl_http 1.0.0.
http.ob_deflate_flags | "0" | PHP_INI_ALL | Available since pecl_http 0.21.0. Removed in pecl_http 1.0.0.
http.ob_inflate_auto | "0" | PHP_INI_PERDIR | Available since pecl_http 0.21.0. Removed in pecl_http 1.0.0.
http.ob_inflate_flags | "0" | PHP_INI_ALL | Available since pecl_http 0.21.0. Removed in pecl_http 1.0.0.
http.only_exceptions | "0" | PHP_INI_ALL | Available since pecl_http 0.11.0.
http.persistent.handles.ident | "GLOBAL" | PHP_INI_ALL | Available since pecl_http 1.5.0.
http.persistent.handles.limit | "-1" | PHP_INI_SYSTEM | Available since pecl_http 1.5.0.
http.redirect_log | "" | PHP_INI_ALL | Available since pecl_http 0.12.0. Removed in pecl_http 1.0.0.
http.request.datashare.connect | "0" | PHP_INI_SYSTEM | Available since pecl_http 1.3.0.
http.request.datashare.cookie | "0" | PHP_INI_SYSTEM | Available since pecl_http 1.3.0.
http.request.datashare.dns | "1" | PHP_INI_SYSTEM | Available since pecl_http 1.3.0.
http.request.datashare.ssl | "0" | PHP_INI_SYSTEM | Available since pecl_http 1.3.0.
http.request.methods.allowed | "" | PHP_INI_ALL | Available since pecl_http 1.0.0.
http.request.methods.custom | "" | PHP_INI_PERDIR | Available since pecl_http 1.0.0.
http.send.deflate.start_auto | "0" | PHP_INI_PERDIR | Available since pecl_http 1.0.0.
http.send.deflate.start_flags | "0" | PHP_INI_ALL | Available since pecl_http 1.0.0.
http.send.inflate.start_auto | "0" | PHP_INI_PERDIR | Available since pecl_http 1.0.0.
http.send.inflate.start_flags | "0" | PHP_INI_ALL | Available since pecl_http 1.0.0.
http.send.not_found_404 | "1" | PHP_INI_ALL | Available since pecl_http 1.0.0.
ibase.allow_persistent | "1" | PHP_INI_SYSTEM |
ibase.dateformat | "%Y-%m-%d" | PHP_INI_ALL |
ibase.default_charset | NULL | PHP_INI_ALL | Available since PHP 5.0.0.
ibase.default_db | NULL | PHP_INI_SYSTEM | Available since PHP 5.0.0.
ibase.default_password | NULL | PHP_INI_ALL |
ibase.default_user | NULL | PHP_INI_ALL |
ibase.max_links | "-1" | PHP_INI_SYSTEM |
ibase.max_persistent | "-1" | PHP_INI_SYSTEM |
ibase.timeformat | "%H:%M:%S" | PHP_INI_ALL |
ibase.timestampformat | "%Y-%m-%d %H:%M:%S" | PHP_INI_ALL |
ibm_db2.binmode | "1" | PHP_INI_ALL |
ibm_db2.i5_all_pconnect | "0" | PHP_INI_SYSTEM | Available since ibm_db2 1.6.5.
ibm_db2.i5_allow_commit | "0" | PHP_INI_SYSTEM | Available since ibm_db2 1.4.9.
ibm_db2.i5_dbcs_alloc | "0" | PHP_INI_SYSTEM | Available since ibm_db2 1.5.0.
ibm_db2.instance_name | NULL | PHP_INI_SYSTEM | Available since ibm_db2 1.0.2.
ibm_db2.i5_ignore_userid | "0" | PHP_INI_SYSTEM | Available since ibm_db2 1.8.0.
iconv.input_encoding | "ISO-8859-1" | PHP_INI_ALL | Available since PHP 4.0.5.
iconv.internal_encoding | "ISO-8859-1" | PHP_INI_ALL | Available since PHP 4.0.5.
iconv.output_encoding | "ISO-8859-1" | PHP_INI_ALL | Available since PHP 4.0.5.
ifx.allow_persistent | "1" | PHP_INI_SYSTEM | Removed in PHP 5.2.1.
ifx.blobinfile | "1" | PHP_INI_ALL | Removed in PHP 5.2.1.
ifx.byteasvarchar | "0" | PHP_INI_ALL | Removed in PHP 5.2.1.
ifx.charasvarchar | "0" | PHP_INI_ALL | Removed in PHP 5.2.1.
ifx.default_host | NULL | PHP_INI_SYSTEM | Removed in PHP 5.2.1.
ifx.default_password | NULL | PHP_INI_SYSTEM | Removed in PHP 5.2.1.
ifx.default_user | NULL | PHP_INI_SYSTEM | Removed in PHP 5.2.1.
ifx.max_links | "-1" | PHP_INI_SYSTEM | Removed in PHP 5.2.1.
ifx.max_persistent | "-1" | PHP_INI_SYSTEM | Removed in PHP 5.2.1.
ifx.nullformat | "0" | PHP_INI_ALL | Removed in PHP 5.2.1.
ifx.textasvarchar | "0" | PHP_INI_ALL | Removed in PHP 5.2.1.
ignore_repeated_errors | "0" | PHP_INI_ALL | Available since PHP 4.3.0.
ignore_repeated_source | "0" | PHP_INI_ALL | Available since PHP 4.3.0.
ignore_user_abort | "0" | PHP_INI_ALL |
imlib2.font_cache_max_size | "524288" | PHP_INI_ALL |
imlib2.font_path | "/usr/share/php/fonts/" | PHP_INI_ALL |
implicit_flush | "0" | PHP_INI_ALL | PHP_INI_PERDIR in PHP <= 4.2.3.
include_path | ".;/path/to/php/pear" | PHP_INI_ALL |
intl.default_locale | "" | PHP_INI_ALL | Available since PHP 5.3.0.
intl.error_level | "0" | PHP_INI_ALL | Available since PHP 5.3.0.
intl.use_exceptions | "0" | PHP_INI_ALL | Available since PHP 5.5.0.
ingres.allow_persistent | "1" | PHP_INI_SYSTEM | Available since ingres 1.0.0
ingres.array_index_start | "1" | PHP_INI_ALL | Available since ingres 1.4.0.
ingres.auto | "1" | PHP_INI_ALL | Available since ingres 2.0.0.
ingres.blob_segment_length | "4096" | PHP_INI_ALL | Available since ingres 1.2.0.
ingres.cursor_mode | "0" | PHP_INI_ALL | Available since ingres 1.1.0.
ingres.default_database | NULL | PHP_INI_ALL | Available since ingres 1.0.0
ingres.default_password | NULL | PHP_INI_ALL | Available since ingres 1.0.0
ingres.default_user | NULL | PHP_INI_ALL | Available since ingres 1.0.0
ingres.describe | 1 | PHP_INI_ALL | Available since ingres 2.1.0
ingres.fetch_buffer_size | 100 | PHP_INI_ALL | Available since ingres 2.1.0
ingres.max_links | "-1" | PHP_INI_SYSTEM | Available since ingres 1.0.0
ingres.max_persistent | "-1" | PHP_INI_SYSTEM | Available since ingres 1.0.0
ingres.reuse_connection | "1" | PHP_INI_ALL | Available since ingres 2.0.0
ingres.scrollable | "1" | PHP_INI_ALL | Available since ingres 2.0.0.
ingres.trace | "0" | PHP_INI_ALL | Available since ingres 2.0.0.
ingres.trace_connect | "0" | PHP_INI_ALL | Available since ingres 1.2.1.
ingres.utf8 | "1" | PHP_INI_ALL | Available since ingres 2.0.0.
last_modified | "0" | PHP_INI_ALL | Available since PHP 4.0.5.
ldap.max_links | "-1" | PHP_INI_SYSTEM |
log_errors | "0" | PHP_INI_ALL |
log_errors_max_len | "1024" | PHP_INI_ALL | Available since PHP 4.3.0.
magic_quotes_gpc | "1" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3. Deprecated in PHP 5.3.0. Removed in PHP 5.4.0.
magic_quotes_runtime | "0" | PHP_INI_ALL | Removed in PHP 5.4.0.
magic_quotes_sybase | "0" | PHP_INI_ALL | Removed in PHP 5.4.0.
mail.add_x_header | "0" | PHP_INI_PERDIR | Available since PHP 5.3.0.
mail.force_extra_parameters | NULL | php.ini only | Available since PHP 5.0.0.
mail.log | "" | PHP_INI_PERDIR | Available since PHP 5.3.0 (PHP_INI_SYSTEM|PHP_INI_PERDIR)
maxdb.default_db | NULL | PHP_INI_ALL |
maxdb.default_host | NULL | PHP_INI_ALL |
maxdb.default_pw | NULL | PHP_INI_ALL |
maxdb.default_user | NULL | PHP_INI_ALL |
maxdb.long_readlen | "200" | PHP_INI_ALL |
max_execution_time | "30" | PHP_INI_ALL |
max_input_nesting_level | "64" | PHP_INI_PERDIR | Available since PHP 4.4.8 and PHP 5.2.3.
max_input_vars | 1000 | PHP_INI_PERDIR | Available since PHP 5.3.9.
max_input_time | "-1" | PHP_INI_PERDIR | Available since PHP 4.3.0.
mbstring.detect_order | NULL | PHP_INI_ALL | Available since PHP 4.0.6.
mbstring.encoding_translation | "0" | PHP_INI_PERDIR | Available since PHP 4.3.0.
mbstring.func_overload | "0" | PHP_INI_SYSTEM | PHP_INI_SYSTEM in PHP <= 4.2.3; PHP_INI_SYSTEM | PHP_INI_PERDIR from PHP 4.3 through 5.2.6. Available since PHP 4.2.0.
mbstring.http_input | "pass" | PHP_INI_ALL | Available since PHP 4.0.6.
mbstring.http_output | "pass" | PHP_INI_ALL | Available since PHP 4.0.6.
mbstring.internal_encoding | NULL | PHP_INI_ALL | Available since PHP 4.0.6.
mbstring.language | "neutral" | PHP_INI_ALL | Available since PHP 4.3.0.; PHP_INI_PERDIR in PHP <= 5.2.6
mbstring.script_encoding | NULL | PHP_INI_ALL | Available since PHP 4.3.0. Removed in PHP 5.4.0. Use zend.script_encoding instead.
mbstring.http_output_conv_mimetypes | "^(text/|application/xhtml\+xml)" | PHP_INI_ALL | Available since PHP 5.3.0.
mbstring.strict_detection | "0" | PHP_INI_ALL | Available since PHP 5.1.2.
mbstring.substitute_character | NULL | PHP_INI_ALL | Available since PHP 4.0.6.
mcrypt.algorithms_dir | NULL | PHP_INI_ALL | Available since PHP 4.0.2.
mcrypt.modes_dir | NULL | PHP_INI_ALL | Available since PHP 4.0.2.
memcache.allow_failover | "1" | PHP_INI_ALL | Available since memcache 2.0.2.
memcache.chunk_size | "8192" | PHP_INI_ALL | Available since memcache 2.0.2.
memcache.default_port | "11211" | PHP_INI_ALL | Available since memcache 2.0.2.
memcache.hash_function | "crc32" | PHP_INI_ALL | Available since memcache 2.2.0.
memcache.hash_strategy | "standard" | PHP_INI_ALL | Available since memcache 2.2.0.
memcache.max_failover_attempts | "20" | PHP_INI_ALL | Available since memcache 2.1.0.
memory_limit | "128M" | PHP_INI_ALL |
mime_magic.debug | "0" | PHP_INI_SYSTEM | Available since PHP 5.0.0.
mime_magic.magicfile | "/path/to/php/magic.mime" | PHP_INI_SYSTEM | Available since PHP 4.3.0.
mongo.allow_empty_keys | 0 | PHP_INI_ALL |
mongo.allow_persistent | 1 | PHP_INI_ALL | Removed in 1.2.0
mongo.chunk_size | 262144 | PHP_INI_ALL |
mongo.cmd | "$" | PHP_INI_ALL |
mongo.default_host | "localhost" | PHP_INI_ALL |
mongo.default_port | 27017 | PHP_INI_ALL |
mongo.is_master_interval | 15 | PHP_INI_ALL | Added in 1.2.10, before 1.3.0 the default value was 60.
mongo.long_as_object | 0 | PHP_INI_ALL |
mongo.native_long | 1 | PHP_INI_ALL | Before 1.5.0, the default value was 0.
mongo.ping_interval | 5 | PHP_INI_ALL | Added in 1.2.10
mongo.utf8 | 1 | PHP_INI_ALL |
msql.allow_persistent | "1" | PHP_INI_ALL |
msql.max_links | "-1" | PHP_INI_ALL |
msql.max_persistent | "-1" | PHP_INI_ALL |
mssql.allow_persistent | "1" | PHP_INI_SYSTEM |
mssql.batchsize | "0" | PHP_INI_ALL | Available since PHP 4.0.4.
mssql.charset | "" | PHP_INI_ALL | Available since PHP 5.1.2.
mssql.compatability_mode | "0" | PHP_INI_ALL |
mssql.connect_timeout | "5" | PHP_INI_ALL |
mssql.datetimeconvert | "1" | PHP_INI_ALL | Available since PHP 4.2.0.
mssql.max_links | "-1" | PHP_INI_SYSTEM |
mssql.max_persistent | "-1" | PHP_INI_SYSTEM |
mssql.max_procs | "-1" | PHP_INI_ALL | Available since PHP 4.3.0.
mssql.min_error_severity | "10" | PHP_INI_ALL |
mssql.min_message_severity | "10" | PHP_INI_ALL |
mssql.secure_connection | "0" | PHP_INI_SYSTEM | Available since PHP 4.3.0.
mssql.textlimit | "-1" | PHP_INI_ALL |
mssql.textsize | "-1" | PHP_INI_ALL |
mssql.timeout | "60" | PHP_INI_ALL | Available since PHP 4.1.0.
mysql.allow_local_infile | "1" | PHP_INI_SYSTEM |
mysql.allow_persistent | "1" | PHP_INI_SYSTEM |
mysql.max_persistent | "-1" | PHP_INI_SYSTEM |
mysql.max_links | "-1" | PHP_INI_SYSTEM |
mysql.trace_mode | "0" | PHP_INI_ALL | PHP 4.3.0부터 사용할 수 있습니다.
mysql.default_port | NULL | PHP_INI_ALL |
mysql.default_socket | NULL | PHP_INI_ALL | PHP 4.0.1부터 사용할 수 있습니다.
mysql.default_host | NULL | PHP_INI_ALL |
mysql.default_user | NULL | PHP_INI_ALL |
mysql.default_password | NULL | PHP_INI_ALL |
mysql.connect_timeout | "60" | PHP_INI_ALL | PHP <= 4.3.2에서는 PHP_INI_SYSTEM입니다. PHP 4.3.0부터 사용할 수 있습니다.
mysqli.allow_local_infile | "1" | PHP_INI_SYSTEM | Available since PHP 5.2.4.
mysqli.allow_persistent | "1" | PHP_INI_SYSTEM | Available since PHP 5.3.0.
mysqli.max_persistent | "-1" | PHP_INI_SYSTEM | Available since PHP 5.3.0.
mysqli.max_links | "-1" | PHP_INI_SYSTEM | Available since PHP 5.0.0.
mysqli.default_port | "3306" | PHP_INI_ALL | Available since PHP 5.0.0.
mysqli.default_socket | NULL | PHP_INI_ALL | Available since PHP 5.0.0.
mysqli.default_host | NULL | PHP_INI_ALL | Available since PHP 5.0.0.
mysqli.default_user | NULL | PHP_INI_ALL | Available since PHP 5.0.0.
mysqli.default_pw | NULL | PHP_INI_ALL | Available since PHP 5.0.0.
mysqli.reconnect | "0" | PHP_INI_SYSTEM | Available since PHP 4.3.5.
mysqli.rollback_on_cached_plink | TRUE | PHP_INI_SYSTEM | Available since PHP 5.6.0.
mysqli.cache_size | "2000" | PHP_INI_SYSTEM | Available since PHP 5.3.0.
mysqlnd.collect_memory_statistics | "0" | PHP_INI_SYSTEM | Available since PHP 5.3.0.
mysqlnd.collect_statistics | "1" | PHP_INI_ALL | Available since PHP 5.3.0.
mysqlnd.debug | "" | PHP_INI_SYSTEM | Available since PHP 5.3.0.
mysqlnd.log_mask | "0" | PHP_INI_ALL | Available since PHP 5.4.0.
mysqlnd.mempool_default_size | "16000" | PHP_INI_ALL | Available since PHP 5.4.0.
mysqlnd.net_cmd_buffer_size | "4096" | PHP_INI_ALL | Available since PHP 5.4.0.
mysqlnd.net_read_buffer_size | "32768" | PHP_INI_ALL | Available since PHP 5.3.0.
mysqlnd.net_read_timeout | "31536000" | PHP_INI_SYSTEM | Available since PHP 5.4.0.
mysqlnd.sha256_server_public_key | "" | PHP_INI_PERDIR | Available since PHP 5.5.0.
mysqlnd.trace_alloc | "" | PHP_INI_SYSTEM | Available since PHP 5.5.0.
mysqlnd_memcache.enable | 1 | PHP_INI_SYSTEM | Available since 1.0.0
mysqlnd_ms.enable | 0 | PHP_INI_SYSTEM |
mysqlnd_ms.force_config_usage | 0 | PHP_INI_SYSTEM |
mysqlnd_ms.ini_file | "" | PHP_INI_SYSTEM |
mysqlnd_ms.config_file | "" | PHP_INI_SYSTEM |
mysqlnd_ms.collect_statistics | 0 | PHP_INI_SYSTEM |
mysqlnd_ms.multi_master | 0 | PHP_INI_SYSTEM |
mysqlnd_ms.disable_rw_split | 0 | PHP_INI_SYSTEM |
mysqlnd_mux.enable | 0 | PHP_INI_SYSTEM |
mysqlnd_qc.enable_qc | 1 | PHP_INI_SYSTEM |
mysqlnd_qc.ttl | 30 | PHP_INI_ALL |
mysqlnd_qc.cache_by_default | 0 | PHP_INI_ALL |
mysqlnd_qc.cache_no_table | 0 | PHP_INI_ALL |
mysqlnd_qc.use_request_time | 0 | PHP_INI_ALL |
mysqlnd_qc.time_statistics | 1 | PHP_INI_ALL |
mysqlnd_qc.collect_statistics | 0 | PHP_INI_ALL |
mysqlnd_qc.collect_statistics_log_file | /tmp/mysqlnd_qc.stats | PHP_INI_SYSTEM |
mysqlnd_qc.collect_query_trace | 0 | PHP_INI_SYSTEM |
mysqlnd_qc.query_trace_bt_depth | 3 | PHP_INI_SYSTEM |
mysqlnd_qc.collect_normalized_query_trace | 0 | PHP_INI_SYSTEM |
mysqlnd_qc.ignore_sql_comments | 1 | PHP_INI_ALL |
mysqlnd_qc.slam_defense | 0 | PHP_INI_SYSTEM |
mysqlnd_qc.slam_defense_ttl | 30 | PHP_INI_SYSTEM |
mysqlnd_qc.std_data_copy | 0 | PHP_INI_SYSTEM |
mysqlnd_qc.apc_prefix | qc_ | PHP_INI_ALL |
mysqlnd_qc.memc_server | 127.0.0.1 | PHP_INI_ALL |
mysqlnd_qc.memc_port | 11211 | PHP_INI_ALL |
mysqlnd_qc.sqlite_data_file | :memory: | PHP_INI_ALL |
mysqlnd_uh.enable | 1 | PHP_INI_SYSTEM |
mysqlnd_uh.report_wrong_types | 1 | PHP_INI_ALL |
nsapi.read_timeout | "60" | PHP_INI_ALL | Available since PHP 4.3.3.
oci8.connection_class | "" | PHP_INI_ALL | Available since PHP 5.3 (PECL OCI8 1.3).
oci8.default_prefetch | "10" | PHP_INI_SYSTEM | Available since PHP 5.1.2.
oci8.events | Off | PHP_INI_SYSTEM | Available since PHP 5.3 (PECL OCI8 1.3).
oci8.max_persistent | "-1" | PHP_INI_SYSTEM | Available since PHP 5.1.2.
oci8.old_oci_close_semantics | "0" | PHP_INI_SYSTEM | Available since PHP 5.1.2.
oci8.persistent_timeout | "-1" | PHP_INI_SYSTEM | Available since PHP 5.1.2.
oci8.ping_interval | "60" | PHP_INI_SYSTEM | Available since PHP 5.1.2.
oci8.privileged_connect | "0" | PHP_INI_SYSTEM | Available since PHP 5.1.2.
oci8.statement_cache_size | "20" | PHP_INI_SYSTEM | Available since PHP 5.1.2.
odbc.allow_persistent | "1" | PHP_INI_SYSTEM |
odbc.check_persistent | "1" | PHP_INI_SYSTEM |
odbc.defaultbinmode | "1" | PHP_INI_ALL |
odbc.defaultlrl | "4096" | PHP_INI_ALL |
odbc.default_db | NULL | PHP_INI_ALL |
odbc.default_cursortype | "3" | PHP_INI_ALL | Available since PHP 5.3.0.
odbc.default_pw | NULL | PHP_INI_ALL |
odbc.default_user | NULL | PHP_INI_ALL |
odbc.max_links | "-1" | PHP_INI_SYSTEM |
odbc.max_persistent | "-1" | PHP_INI_SYSTEM |
odbtp.datetime_format | "object" | PHP_INI_ALL |
odbtp.detach_default_queries | "0" | PHP_INI_ALL |
odbtp.guid_format | "string" | PHP_INI_ALL | Available since odbtp 1.1.3.
odbtp.interface_file | "/usr/local/share/odbtp.conf" | PHP_INI_ALL |
odbtp.truncation_errors | "1" | PHP_INI_ALL |
opendirectory.default_separator | "/" | PHP_INI_ALL | Removed in opendirectory 0.2.2.
opendirectory.max_refs | "-1" | PHP_INI_ALL |
opendirectory.separator | "/" | PHP_INI_ALL | Available since opendirectory 0.2.2.
open_basedir | NULL | PHP_INI_ALL | PHP_INI_SYSTEM in PHP < 5.2.3.
oracle.allow_persistent | "-1" | PHP_INI_ALL | Removed in PHP 5.1.0.
oracle.max_links | "-1" | PHP_INI_ALL | Removed in PHP 5.1.0.
oracle.max_persistent | "-1" | PHP_INI_ALL | Removed in PHP 5.1.0.
output_buffering | "0" | PHP_INI_PERDIR |
output_handler | NULL | PHP_INI_PERDIR | Available since PHP 4.0.4.
pam.servicename | "php" | PHP_INI_ALL |
pcre.backtrack_limit | "1000000" | PHP_INI_ALL | Available since PHP 5.2.0.
pcre.recursion_limit | "100000" | PHP_INI_ALL | Available since PHP 5.2.0.
pcre.jit | "1" | PHP_INI_ALL | Available since PHP 7.0.0.
pdo.dsn.* |  | php.ini only | Available since PHP 5.1.0.
pdo_odbc.connection_pooling | "strict" | PHP_INI_ALL | Available since PHP 5.1.0.
pdo_odbc.db2_instance_name | NULL | PHP_INI_SYSTEM | Available since PHP 5.1.1. 이 배제된 기능은 추후 제거될 예정입니다.
pfpro.defaulthost | "test-payflow.verisign.com" | PHP_INI_ALL | Available since PHP 4.0.2. Removed in PHP 5.1.0.
pfpro.defaultport | "443" | PHP_INI_ALL | Available since PHP 4.0.2. Removed in PHP 5.1.0.
pfpro.defaulttimeout | "30" | PHP_INI_ALL | Available since PHP 4.0.2. Removed in PHP 5.1.0.
pfpro.proxyaddress | "" | PHP_INI_ALL | Available since PHP 4.0.2. Removed in PHP 5.1.0.
pfpro.proxylogon | "" | PHP_INI_ALL | Available since PHP 4.0.2. Removed in PHP 5.1.0.
pfpro.proxypassword | "" | PHP_INI_ALL | Available since PHP 4.0.2. Removed in PHP 5.1.0.
pfpro.proxyport | "" | PHP_INI_ALL | Available since PHP 4.0.2. Removed in PHP 5.1.0.
pgsql.allow_persistent | "1" | PHP_INI_SYSTEM |
pgsql.auto_reset_persistent | "0" | PHP_INI_SYSTEM | Available since PHP 4.2.0.
pgsql.ignore_notice | "0" | PHP_INI_ALL | Available since PHP 4.3.0.
pgsql.log_notice | "0" | PHP_INI_ALL | Available since PHP 4.3.0.
pgsql.max_links | "-1" | PHP_INI_SYSTEM |
pgsql.max_persistent | "-1" | PHP_INI_SYSTEM |
phar.cache_list | "" | PHP_INI_SYSTEM | Available since PHP 5.4.0.
phar.extract_list | "" | PHP_INI_ALL | Available since phar 1.1.0.
phar.readonly | "1" | PHP_INI_ALL |
phar.require_hash | "1" | PHP_INI_ALL |
post_max_size | "8M" | PHP_INI_PERDIR | PHP_INI_SYSTEM in PHP <= 4.2.3. Available since PHP 4.0.3.
precision | "14" | PHP_INI_ALL |
python.append_path | "" | PHP_INI_ALL |
python.prepend_path | "." | PHP_INI_ALL |
realpath_cache_size | "16K" | PHP_INI_SYSTEM | Available since PHP 5.1.0.
realpath_cache_ttl | "120" | PHP_INI_SYSTEM | Available since PHP 5.1.0.
register_argc_argv | "1" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3.
register_globals | "0" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3. Deprecated in PHP 5.3.0. Removed in PHP 5.4.0.
register_long_arrays | "1" | PHP_INI_PERDIR | Available since PHP 5.0.0. Deprecated in PHP 5.3.0. Removed in PHP 5.4.0.
report_memleaks | "1" | PHP_INI_ALL | Available since PHP 4.3.0.
report_zend_debug | "1" | PHP_INI_ALL | Available since PHP 5.0.0.
request_order | "" | PHP_INI_PERDIR | Added in PHP 5.3.0
runkit.internal_override | "0" | PHP_INI_SYSTEM |
runkit.superglobal | "" | PHP_INI_PERDIR |
safe_mode | "0" | PHP_INI_SYSTEM | Removed in PHP 5.4.0.
safe_mode_allowed_env_vars | "PHP_" | PHP_INI_SYSTEM | Removed in PHP 5.4.0.
safe_mode_exec_dir | "" | PHP_INI_SYSTEM | Removed in PHP 5.4.0.
safe_mode_gid | "0" | PHP_INI_SYSTEM | Available since PHP 4.1.0. Removed in PHP 5.4.0.
safe_mode_include_dir | NULL | PHP_INI_SYSTEM | Available since PHP 4.1.0. Removed in PHP 5.4.0.
safe_mode_protected_env_vars | "LD_LIBRARY_PATH" | PHP_INI_SYSTEM | Removed in PHP 5.4.0.
sendmail_from | NULL | PHP_INI_ALL |
sendmail_path | "/usr/sbin/sendmail -t -i" | PHP_INI_SYSTEM |
serialize_precision | "-1" | PHP_INI_ALL | Available since PHP 4.3.2. Until PHP 5.3.5, the default value was 100. Until PHP 7.1.0, the default value was 17.
session.auto_start | "0" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 5.4.19 and in PHP <= 5.5.1.
session.bug_compat_42 | "1" | PHP_INI_ALL | Available since PHP 4.3.0. Removed in PHP 5.4.0.
session.bug_compat_warn | "1" | PHP_INI_ALL | Available since PHP 4.3.0. Removed in PHP 5.4.0.
session.cache_expire | "180" | PHP_INI_ALL |
session.cache_limiter | "nocache" | PHP_INI_ALL |
session.cookie_domain | "" | PHP_INI_ALL |
session.cookie_httponly | "" | PHP_INI_ALL | Available since PHP 5.2.0.
session.cookie_lifetime | "0" | PHP_INI_ALL |
session.cookie_path | "/" | PHP_INI_ALL |
session.cookie_secure | "" | PHP_INI_ALL | Available since PHP 4.0.4.
session.entropy_file | "" | PHP_INI_ALL |
session.entropy_length | "0" | PHP_INI_ALL |
session.gc_divisor | "100" | PHP_INI_ALL | Available since PHP 4.3.2.
session.gc_maxlifetime | "1440" | PHP_INI_ALL |
session.gc_probability | "1" | PHP_INI_ALL |
session.hash_bits_per_character | "4" | PHP_INI_ALL | Available since PHP 5.0.0.
session.hash_function | "0" | PHP_INI_ALL | Available since PHP 5.0.0.
session.lazy_write | "1" | PHP_INI_ALL | Available since PHP 7.0.0.
session.name | "PHPSESSID" | PHP_INI_ALL |
session.referer_check | "" | PHP_INI_ALL |
session.save_handler | "files" | PHP_INI_ALL |
session.save_path | "" | PHP_INI_ALL |
session.serialize_handler | "php" | PHP_INI_ALL |
session.upload_progress.cleanup | "1" | PHP_INI_PERDIR | Available since PHP 5.4.0.
session.upload_progress.enabled | "1" | PHP_INI_PERDIR | Available since PHP 5.4.0.
session.upload_progress.freq | "1%" | PHP_INI_PERDIR | Available since PHP 5.4.0.
session.upload_progress.min_freq | "1" | PHP_INI_PERDIR | Available since PHP 5.4.0.
session.upload_progress.name | "PHP_SESSION_UPLOAD_PROGRESS" | PHP_INI_PERDIR | Available since PHP 5.4.0.
session.upload_progress.prefix | "upload_progress_" | PHP_INI_PERDIR | Available since PHP 5.4.0.
session.use_strict_mode | "0" | PHP_INI_ALL | Available since PHP 5.5.2.
session.use_cookies | "1" | PHP_INI_ALL |
session.use_only_cookies | "1" | PHP_INI_ALL | Available since PHP 4.3.0.
session.use_trans_sid | "0" | PHP_INI_ALL | PHP_INI_ALL in PHP <= 4.2.3. PHP_INI_PERDIR in PHP < 5. Available since PHP 4.0.3.
session_pgsql.create_table | "1" | PHP_INI_SYSTEM |
session_pgsql.db | "host=localhost dbname=php_session user=nobody" | PHP_INI_SYSTEM |
session_pgsql.disable | "0" | PHP_INI_SYSTEM |
session_pgsql.failover_mode | "0" | PHP_INI_SYSTEM |
session_pgsql.gc_interval | "3600" | PHP_INI_SYSTEM |
session_pgsql.keep_expired | "0" | PHP_INI_SYSTEM |
session_pgsql.sem_file_name | "/tmp/php_session_pgsql" | PHP_INI_SYSTEM |
session_pgsql.serializable | "0" | PHP_INI_SYSTEM |
session_pgsql.short_circuit | "0" | PHP_INI_SYSTEM |
session_pgsql.use_app_vars | "0" | PHP_INI_SYSTEM |
session_pgsql.vacuum_interval | "21600" | PHP_INI_SYSTEM |
short_open_tag | "1" | PHP_INI_PERDIR | PHP_INI_ALL in PHP 4.0.0. PHP_INI_PERDIR in PHP >= 4.0.1.
SMTP | "localhost" | PHP_INI_ALL |
smtp_port | "25" | PHP_INI_ALL | Available since PHP 4.3.0.
soap.wsdl_cache | "1" | PHP_INI_ALL | Available since PHP 5.1.5.
soap.wsdl_cache_dir | "/tmp" | PHP_INI_ALL | Available since PHP 5.0.0.
soap.wsdl_cache_enabled | "1" | PHP_INI_ALL | Available since PHP 5.0.0.
soap.wsdl_cache_limit | "5" | PHP_INI_ALL | Available since PHP 5.1.5.
soap.wsdl_cache_ttl | "86400" | PHP_INI_ALL | Available since PHP 5.0.0.
sql.safe_mode | "0" | PHP_INI_SYSTEM |
sqlite.assoc_case | "0" | PHP_INI_ALL | Available since PHP 5.0.0.
sqlite3.extension_dir | "" | PHP_INI_SYSTEM | Available since PHP 5.3.11.
sybase.allow_persistent | "1" | PHP_INI_ALL | PHP_INI_ALL in PHP <= 4.0.2. PHP_INI_SYSTEM in PHP <= 4.0.3.
sybase.interface_file | "" | PHP_INI_ALL |
sybase.max_links | "-1" | PHP_INI_ALL | PHP_INI_ALL in PHP <= 4.0.2. PHP_INI_SYSTEM in PHP <= 4.0.3.
sybase.max_persistent | "-1" | PHP_INI_ALL | PHP_INI_ALL in PHP <= 4.0.2. PHP_INI_SYSTEM in PHP <= 4.0.3.
sybase.min_error_severity | "10" | PHP_INI_ALL |
sybase.min_message_severity | "10" | PHP_INI_ALL |
sybct.deadlock_retry_count | "0" | PHP_INI_ALL | Available since PHP 4.3.0.
sybct.login_timeout | "-1" | PHP_INI_ALL | Available since PHP 4.0.2.
sybct.packet_size | "0" | PHP_INI_ALL | Available since PHP 4.3.5.
sybct.timeout | "0" | PHP_INI_ALL | Available since PHP 4.0.2.
sys_temp_dir | "" | PHP_INI_SYSTEM | Available since PHP 5.5.0.
sysvshm.init_mem | "10000" | PHP_INI_ALL |
tidy.clean_output | "0" | PHP_INI_USER | PHP_INI_PERDIR in PHP 5. Available since PHP 5.0.0.
tidy.default_config | "" | PHP_INI_SYSTEM | Available since PHP 5.0.0.
track_errors | "0" | PHP_INI_ALL |
unserialize_callback_func | NULL | PHP_INI_ALL | Available since PHP 4.2.0.
uploadprogress.file.filename_template | "/tmp/upt_%s.txt" | PHP_INI_ALL |
upload_max_filesize | "2M" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3.
max_file_uploads | 20 | PHP_INI_SYSTEM | Available since PHP 5.2.12.
upload_tmp_dir | NULL | PHP_INI_SYSTEM |
url_rewriter.tags | "a=href,area=href,frame=src,form=,fieldset=" | PHP_INI_ALL | Available since PHP 4.0.4.
user_agent | NULL | PHP_INI_ALL | Available since PHP 4.3.0.
user_dir | NULL | PHP_INI_SYSTEM |
user_ini.cache_ttl | "300" | PHP_INI_SYSTEM | Available since PHP 5.3.0.
user_ini.filename | ".user.ini" | PHP_INI_SYSTEM | Available since PHP 5.3.0.
valkyrie.auto_validate | "0" | PHP_INI_ALL |
valkyrie.config_path | NULL | PHP_INI_ALL |
variables_order | "EGPCS" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 5.0.5.
vld.active | "0" | PHP_INI_SYSTEM |
vld.execute | "1" | PHP_INI_SYSTEM | Available since vld 0.8.0.
vld.skip_append | "0" | PHP_INI_SYSTEM | Available since vld 0.8.0.
vld.skip_prepend | "0" | PHP_INI_SYSTEM | Available since vld 0.8.0.
windows.show_crt_warning | "0" | PHP_INI_ALL | Available since PHP 5.4.0.
xbithack | "0" | PHP_INI_ALL | Available since PHP 4.0.5.
xdebug.auto_profile | "0" | PHP_INI_ALL | Removed in Xdebug 2.0.0.
xdebug.auto_profile_mode | "0" | PHP_INI_ALL | Removed in Xdebug 2.0.0.
xdebug.auto_trace | "0" | PHP_INI_ALL |
xdebug.collect_includes | "1" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.collect_params | "0" | PHP_INI_ALL |
xdebug.collect_return | "0" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.collect_vars | "0" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.default_enable | "1" | PHP_INI_ALL | PHP_INI_SYSTEM in Xdebug 1.
xdebug.dump.COOKIE | NULL | PHP_INI_ALL |
xdebug.dump.ENV | NULL | PHP_INI_ALL |
xdebug.dump.FILES | NULL | PHP_INI_ALL |
xdebug.dump.GET | NULL | PHP_INI_ALL |
xdebug.dump.POST | NULL | PHP_INI_ALL |
xdebug.dump.REQUEST | NULL | PHP_INI_ALL |
xdebug.dump.SERVER | NULL | PHP_INI_ALL |
xdebug.dump.SESSION | NULL | PHP_INI_ALL |
xdebug.dump_globals | "1" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.dump_once | "1" | PHP_INI_ALL |
xdebug.dump_undefined | "0" | PHP_INI_ALL |
xdebug.extended_info | "1" | PHP_INI_SYSTEM | Available since Xdebug 2.0.0.
xdebug.idekey | "" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.manual_url | "http://www.php.net" | PHP_INI_ALL |
xdebug.max_nesting_level | "100" | PHP_INI_ALL |
xdebug.output_dir | "/tmp" | PHP_INI_PERDIR | PHP_INI_SYSTEM in Xdebug <= 1.2.0. Removed in Xdebug 2.0.0.
xdebug.profiler_aggregate | "0" | PHP_INI_PERDIR | Available since Xdebug 2.0.0.
xdebug.profiler_append | "0" | PHP_INI_PERDIR | Available since Xdebug 2.0.0.
xdebug.profiler_enable | "0" | PHP_INI_PERDIR | Available since Xdebug 2.0.0.
xdebug.profiler_enable_trigger | "0" | PHP_INI_PERDIR | Available since Xdebug 2.0.0.
xdebug.profiler_output_dir | "/tmp" | PHP_INI_PERDIR | Available since Xdebug 2.0.0.
xdebug.profiler_output_name | "cachegrind.out.%p" | PHP_INI_PERDIR | Available since Xdebug 2.0.0.
xdebug.remote_autostart | "0" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.remote_enable | "0" | PHP_INI_PERDIR |
xdebug.remote_handler | "dbgp" | PHP_INI_ALL |
xdebug.remote_host | "localhost" | PHP_INI_ALL |
xdebug.remote_log | "" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.remote_mode | "req" | PHP_INI_ALL |
xdebug.remote_port | "9000" | PHP_INI_ALL |
xdebug.show_exception_trace | "0" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.show_local_vars | "0" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.show_mem_delta | "0" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.trace_format | "0" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.trace_options | "0" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.trace_output_dir | "/tmp" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.trace_output_name | "trace.%c" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.var_display_max_children | "128" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.var_display_max_data | "512" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xdebug.var_display_max_depth | "3" | PHP_INI_ALL | Available since Xdebug 2.0.0.
xmlrpc_errors | "0" | PHP_INI_SYSTEM | Available since PHP 4.1.0.
xmlrpc_error_number | "0" | PHP_INI_ALL | Available since PHP 4.1.0.
xmms.path | "/usr/bin/xmms" | PHP_INI_ALL |
xmms.session | "0" | PHP_INI_ALL |
xsl.security_prefs | "44" | PHP_INI_ALL | Available since PHP 5.5.0.
y2k_compliance | "1" | PHP_INI_ALL | Removed in PHP 5.4.0.
yami.response.timeout | "5" | PHP_INI_ALL | Available since yami 1.0.1.
yaz.keepalive | "120" | PHP_INI_ALL |
yaz.log_mask | NULL | PHP_INI_ALL | Available since yaz 1.0.3.
zend.assertions | "1" | PHP_INI_ALL | Available since PHP 7.0.0.
zend.detect_unicode | "1" | PHP_INI_ALL | Available since PHP 5.4.0.
zend.enable_gc | "1" | PHP_INI_ALL | Available since PHP 5.3.0.
zend.multibyte | "0" | PHP_INI_PERDIR | Available since PHP 5.4.0.
zend.script_encoding | NULL | PHP_INI_ALL | Available since PHP 5.4.0
zend.signal_check | "0" | PHP_INI_SYSTEM | Available since PHP 5.4.0
zend.ze1_compatibility_mode | "0" | PHP_INI_ALL | Available since PHP 5.0.0. Removed in PHP 5.3.0.
zend_extension | NULL | php.ini only |
zend_extension_debug | NULL | php.ini only |
zend_extension_debug_ts | NULL | php.ini only |
zend_extension_ts | NULL | php.ini only |
zlib.output_compression | "0" | PHP_INI_ALL | Available since PHP 4.0.5.
zlib.output_compression_level | "-1" | PHP_INI_ALL | Available since PHP 4.3.0.
zlib.output_handler | "" | PHP_INI_ALL | Available since PHP 4.3.0.

User Contributed Notes

There are no user contributed notes for this page.


List of php.ini sections

This list includes the php.ini sections you can set to configure your PHP setup on a per Host or Path basis. These sections are optional.

These sections don't directly affect PHP. They are used to group other php.ini directives together and to get them to act upon a particular host or on a particular path.

These sections are used only in CGI/FastCGI mode and they can not set extension and zend_extension directives.

Sections
이름 | 가변성 | 변경점
[HOST=] | PHP_INI_SYSTEM | Added in PHP 5.3.0.
[PATH=] | PHP_INI_SYSTEM | Added in PHP 5.3.0.

위 설정 지시어에 대한 간단한 설명입니다.

[HOST=<host>]

This section allows you to define a set of php.ini directives that will take effect on the named host.

Example #1 Activate full on-screen error reporting for dev. domain

[HOST=dev.site.com]
error_reporting = E_ALL
display_errors = On

[PATH=<path>]

This section allows you to define a set of php.ini directives that will take effect when a script runs from the named path.

Example #2 Add security script for protected areas

[PATH=/home/site/public/secure]
auto_prepend_file=security.php

User Contributed Notes

z dot himdi at bita dot nl 04-Aug-2011 04:53
On Windows IIS 7 server I noticed that [PATH=] was not listened to. [HOST=] however worked.
leo 06-May-2011 12:21
Just developed the probably first php.ini hack to add commands which will only be parsed before PHP 5.3 when using mod_php.

<?php

# Disable eAccelerator by default
eaccelerator.enable = 0

# Now, to prevent incompatibilities with Zend Optimizer+, we
# want to enable eAccelerator only in PHP 5.2 or lower.
[HOST=*]
eaccelerator.enable = 1

?>

What is happening here? In PHP 5.3, php.ini sections to set up PHP on a per host basis have been introduced:

http://www.php.net/manual/en/ini.sections.php

These sections only work in CGI/FastCGI mode, however there still seems to be a difference in how the php.ini file is parsed in PHP 5.2 and PHP 5.3 when using mod_php: PHP 5.3 ignores everything below the line [HOST=*], whereas PHP 5.2 does not.

In the above example, PHP 5.3 only reads "eaccelerator.enable = 0" and then stops at the invalid command [HOST=*]. However, PHP 5.2 seems to ignore the invalid command and parses the whole configuration file, ending up with "eaccelerator.enable = 1".
stickman 05-Jan-2011 10:39
This doesn't seem to work with php-fpm.
The values defined under PATH or HOST will become the new master value for all the processes (as if they were redefined).
crash at lubyte dot de 06-Sep-2010 02:45
If you have a system which uses separated PHP inis for loading extensions (Debian, Fedora and most other distributions do so), [PATH=] or [HOST=] will prevent loading extensions defined in those ini files (extension= and zend_extension=).

I had some headache, when I added a hosts.ini to /etc/php5/cgi/conf.d (Debians default), where I wanted to define some defaults for a host name. The manual says extension= and zend_extension= aren't allowed in [HOST=] and [PATH=] (see above). I figured out that you can easily fix that with adding [PHP] after your definitions.

For example hosts.ini:
[HOST=dev.example.com]
display_errors = on

[PHP]

this will change the section back to [PHP] where extension= and zend_extension= is allowed.
robert dot johnson at icap dot com 12-Apr-2010 07:26
Adding PATH= applies only to the named path and does not include sub-directories, you have to add a PATH= value for each sub-directory.

Also some settings have no effect even though phpinfo recognises and displays the per-folder value as the 'local' value, and the default as the 'master' value.

'fastcgi.impersonate' always uses the master setting.
public at grik dot net 25-Sep-2009 12:00
In [HOST=...] one should use only the 1st server name from the list of server names.

I.E. if you have
server_name example.com, example2.com;
and will use www.example.com in a browser,
use [HOST=example.com] in php.ini for both addresses.

PHP looks not at the HOST request header (as I expected), but at the SERVER_NAME parameter (which by default is the 1st from the list of names, no matter what the HOST is).


Description of core php.ini directives

This list includes the core php.ini directives you can set to configure your PHP setup. Directives handled by extensions are listed and detailed at the extension documentation pages respectively; Information on the session directives for example can be found at the sessions page.

Note:

The defaults listed here are used when php.ini is not loaded; the values for the production and development php.ini may vary.

Httpd Options

Httpd Options
이름 | 기본값 | 가변성 | 변경점
async_send | "0" | PHP_INI_ALL | Available since PHP 4.2.0. Removed in PHP 4.3.0.

Language Options

Language and Misc Configuration Options
이름 | 기본값 | 가변성 | 변경점
short_open_tag | "1" | PHP_INI_PERDIR | PHP_INI_ALL in PHP 4.0.0. PHP_INI_PERDIR in PHP >= 4.0.1.
asp_tags | "0" | PHP_INI_PERDIR | PHP_INI_ALL in PHP 4.0.0. Removed in PHP 7.0.0.
precision | "14" | PHP_INI_ALL |
serialize_precision | "17" | PHP_INI_ALL | Available since PHP 4.3.2. Until PHP 5.3.5, the default value was 100.
y2k_compliance | "1" | PHP_INI_ALL | Removed in PHP 5.4.0.
allow_call_time_pass_reference | "1" | PHP_INI_PERDIR | PHP_INI_ALL in PHP 4.0.0. Removed in PHP 5.4.0.
disable_functions | "" | PHP_INI_SYSTEM only | Available since PHP 4.0.1.
disable_classes | "" | php.ini only | Available since PHP 4.3.2.
exit_on_timeout | "" | PHP_INI_ALL | Available since PHP 5.3.0.
expose_php | "1" | php.ini only |
zend.multibyte | "0" | PHP_INI_ALL | Available since PHP 5.4.0
zend.script_encoding | NULL | PHP_INI_ALL | Available since PHP 5.4.0
zend.detect-unicode | NULL | PHP_INI_ALL | Available since PHP 5.4.0
zend.signal_check | "0" | PHP_INI_SYSTEM | Available since PHP 5.4.0
zend.assertions | "1" | PHP_INI_ALL with restrictions | Available since PHP 7.0.0.
zend.ze1_compatibility_mode | "0" | PHP_INI_ALL | Available since PHP 5.0.0. Removed in PHP 5.3.0
detect_unicode | "1" | PHP_INI_ALL | Available since PHP 5.1.0. Renamed to zend.detect-unicode from PHP 5.4.0.

위 설정 지시어에 대한 간단한 설명입니다.

short_open_tag boolean

Tells PHP whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>).

Note:

This directive also affected the shorthand <?= before PHP 5.4.0, which is identical to <? echo. Use of this shortcut required short_open_tag to be on. Since PHP 5.4.0, <?= is always available.

asp_tags boolean
Enables the use of ASP-like <% %> tags in addition to the usual <?php ?> tags. This includes the variable-value printing shorthand of <%= $value %>. For more information, see Escaping from HTML.

Changelog for asp_tags
버전 | 설명
7.0.0 | Removed from PHP.

precision integer
The number of significant digits displayed in floating point numbers. -1 means that an enhanced algorithm for rounding such numbers will be used.
serialize_precision integer
The number of significant digits stored while serializing floating point numbers. -1 means that an enhanced algorithm for rounding such numbers will be used.
y2k_compliance boolean
Enforce year 2000 compliance (will cause problems with non-compliant browsers)
allow_call_time_pass_reference boolean

Whether to warn when arguments are passed by reference at function call time. The encouraged method of specifying which arguments should be passed by reference is in the function declaration. You're encouraged to try and turn this option Off and make sure your scripts work properly with it in order to ensure they will work with future versions of the language (you will receive a warning each time you use this feature).

Passing arguments by reference at function call time was deprecated for code-cleanliness reasons. A function can modify its arguments in an undocumented way if it didn't declare that the argument shall be passed by reference. To prevent side-effects it's better to specify which arguments are passed by reference in the function declaration only.

See also References Explained.

Changelog for allow_call_time_pass_reference
버전 | 설명
5.4.0 | Removed from PHP.
5.3.0 | Emits an E_DEPRECATED level error.
5.0.0 | Deprecated, and generates an E_COMPILE_WARNING level error.

expose_php boolean

Exposes to the world that PHP is installed on the server, which includes the PHP version within the HTTP header (e.g., X-Powered-By: PHP/5.3.7). Prior to PHP 5.5.0 the PHP logo guids are also exposed, thus appending them to the URL of your PHP script would display the appropriate logo (e.g., » http://www.php.net/?=PHPE9568F34-D428-11d2-A769-00AA001ACF42). This also affected the output of phpinfo(), as when disabled, the PHP logo and credits information would not be displayed.

Note:

Since PHP 5.5.0 these guids and the php_logo_guid() function have been removed from PHP and the guids are replaced with data URIs instead. Thus accessing the PHP logo via appending the guid to the URL no longer works. Similarly, turning expose_php off will not affect seeing the PHP logo in phpinfo().

See also php_logo_guid() and phpcredits().

disable_functions string

This directive allows you to disable certain functions for security reasons. It takes on a comma-delimited list of function names. disable_functions is not affected by Safe Mode.

Only internal functions can be disabled using this directive. User-defined functions are unaffected.

This directive must be set in php.ini For example, you cannot set this in httpd.conf.

disable_classes string
This directive allows you to disable certain classes for security reasons. It takes on a comma-delimited list of class names. disable_classes is not affected by Safe Mode. This directive must be set in php.ini For example, you cannot set this in httpd.conf.
zend.assertions integer
When set to 1, assertion code will be generated and executed (development mode). When set to 0, assertion code will be generated but it will be skipped (not executed) at runtime. When set to -1, assertion code will not be generated, making the assertions zero-cost (production mode).

Note:

If a process is started in production mode, zend.assertions cannot be changed at runtime, since the code for assertions was not generated.

If a process is started in development mode, zend.assertions cannot be set to -1 at runtime.

zend.ze1_compatibility_mode boolean

Enable compatibility mode with Zend Engine 1 (PHP 4). It affects the cloning, casting (objects with no properties cast to FALSE or 0), and comparing of objects. In this mode, objects are passed by value instead of reference by default.

See also the section titled Migrating from PHP 4 to PHP 5.

Warning

This feature has been DEPRECATED and REMOVED as of PHP 5.3.0.

zend.multibyte boolean

Enables parsing of source files in multibyte encodings. Enabling zend.multibyte is required to use character encodings like SJIS, BIG5, etc that contain special characters in multibyte string data. ISO-8859-1 compatible encodings like UTF-8, EUC, etc do not require this option.

Enabling zend.multibyte requires the mbstring extension to be available.

zend.script_encoding string

This value will be used unless a declare(encoding=...) directive appears at the top of the script. When ISO-8859-1 incompatible encoding is used, both zend.multibyte and zend.script_encoding must be used.

Literal strings will be transliterated from zend.script_enconding to mbstring.internal_encoding, as if mb_convert_encoding() would have been called.

zend.detect_unicode boolean

Check for BOM (Byte Order Mark) and see if the file contains valid multibyte characters. This detection is performed before processing of __halt_compiler(). Available only in Zend Multibyte mode.

zend.signal_check boolean

To check for replaced signal handlers on shutdown.

exit_on_timeout boolean

This is an Apache1 mod_php-only directive that forces an Apache child to exit if a PHP execution timeout occurred. Such a timeout causes an internal longjmp() call in Apache1 which can leave some extensions in an inconsistent state. By terminating the process any outstanding locks or memory will be cleaned up.

Resource Limits

Resource Limits
이름 | 기본값 | 가변성 | 변경점
memory_limit | "128M" | PHP_INI_ALL | "8M" before PHP 5.2.0, "16M" in PHP 5.2.0

위 설정 지시어에 대한 간단한 설명입니다.

memory_limit integer

This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to -1.

Prior to PHP 5.2.1, in order to use this directive it had to be enabled at compile time by using --enable-memory-limit in the configure line. This compile-time flag was also required to define the functions memory_get_usage() and memory_get_peak_usage() prior to 5.2.1.

integer 형이 사용될 때, 바이트로 값을 측정합니다. FAQ에 나와 있는 단축어를 사용할 수도 있습니다.

See also: max_execution_time.

Performance Tuning

Performance Tuning
이름 | 기본값 | 가변성 | 변경점
realpath_cache_size | "16K" | PHP_INI_SYSTEM | Available since PHP 5.1.0.
realpath_cache_ttl | "120" | PHP_INI_SYSTEM | Available since PHP 5.1.0.

위 설정 지시어에 대한 간단한 설명입니다.

realpath_cache_size integer

Determines the size of the realpath cache to be used by PHP. This value should be increased on systems where PHP opens many files, to reflect the quantity of the file operations performed.

The size represents the total number of bytes in the path strings stored, plus the size of the data associated with the cache entry. This means that in order to store longer paths in the cache, the cache size must be larger. This value does not directly control the number of distinct paths that can be cached.

The size required for the cache entry data is system dependent.

realpath_cache_ttl integer

Duration of time (in seconds) for which to cache realpath information for a given file or directory. For systems with rarely changing files, consider increasing the value.

Data Handling

Data Handling Configuration Options
이름 | 기본값 | 가변성 | 변경점
arg_separator.output | "&" | PHP_INI_ALL | Available since PHP 4.0.5.
arg_separator.input | "&" | PHP_INI_PERDIR | Available since PHP 4.0.5.
variables_order | "EGPCS" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 5.0.5.
request_order | "" | PHP_INI_PERDIR | Available since PHP 5.3.0
auto_globals_jit | "1" | PHP_INI_PERDIR | Available since PHP 5.0.0.
register_globals | "0" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3. Removed in PHP 5.4.0.
register_argc_argv | "1" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3.
register_long_arrays | "1" | PHP_INI_PERDIR | Available since PHP 5.0.0. Deprecated in PHP 5.3.0. Removed in PHP 5.4.0.
enable_post_data_reading | "1" | PHP_INI_PERDIR | Available since PHP 5.4.0
post_max_size | "8M" | PHP_INI_PERDIR | PHP_INI_SYSTEM in PHP <= 4.2.3. Available since PHP 4.0.3.
auto_prepend_file | NULL | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3.
auto_append_file | NULL | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3.
default_mimetype | "text/html" | PHP_INI_ALL |
default_charset | "UTF-8" | PHP_INI_ALL | Defaults to "UTF-8" since PHP >= 5.6.0; empty for PHP < 5.6.0.
always_populate_raw_post_data | "0" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3. Available since PHP 4.1.0. Removed in PHP 7.0.0.

위 설정 지시어에 대한 간단한 설명입니다.

arg_separator.output string

The separator used in PHP generated URLs to separate arguments.

arg_separator.input string

List of separator(s) used by PHP to parse input URLs into variables.

Note:

Every character in this directive is considered as separator!

variables_order string

Sets the order of the EGPCS (Environment, Get, Post, Cookie, and Server) variable parsing. For example, if variables_order is set to "SP" then PHP will create the superglobals $_SERVER and $_POST, but not create $_ENV, $_GET, and $_COOKIE. Setting to "" means no superglobals will be set.

If the deprecated register_globals directive is on, then variables_order also configures the order the ENV, GET, POST, COOKIE and SERVER variables are populated in global scope. So for example if variables_order is set to "EGPCS", register_globals is enabled, and both $_GET['action'] and $_POST['action'] are set, then $action will contain the value of $_POST['action'] as P comes after G in our example directive value.

Warning

In both the CGI and FastCGI SAPIs, $_SERVER is also populated by values from the environment; S is always equivalent to ES regardless of the placement of E elsewhere in this directive.

Note:

The content and order of $_REQUEST is also affected by this directive.

request_order string

This directive describes the order in which PHP registers GET, POST and Cookie variables into the _REQUEST array. Registration is done from left to right, newer values override older values.

If this directive is not set, variables_order is used for $_REQUEST contents.

Note that the default distribution php.ini files does not contain the 'C' for cookies, due to security concerns.

auto_globals_jit boolean

When enabled, the SERVER and ENV variables are created when they're first used (Just In Time) instead of when the script starts. If these variables are not used within a script, having this directive on will result in a performance gain.

The PHP directives register_globals, register_long_arrays, and register_argc_argv must be disabled for this directive to have any affect. Since PHP 5.1.3 it is not necessary to have register_argc_argv disabled.

Warning

Usage of SERVER and ENV variables is checked during the compile time so using them through e.g. variable variables will not cause their initialization.

register_globals boolean

Whether or not to register the EGPCS (Environment, GET, POST, Cookie, Server) variables as global variables.

As of » PHP 4.2.0, this directive defaults to off.

Please read the security chapter on Using register_globals for related information.

Please note that register_globals cannot be set at runtime (ini_set()). Although, you can use .htaccess if your host allows it as described above. An example .htaccess entry: php_flag register_globals off.

Note:

register_globals is affected by the variables_order directive.

Warning

이 기능은 PHP 5.3.0부터 배제되어, PHP 5.4.0부터 제거됩니다.

register_argc_argv boolean
Tells PHP whether to declare the argv & argc variables (that would contain the GET information). See also command line.
register_long_arrays boolean
Tells PHP whether or not to register the deprecated long $HTTP_*_VARS type predefined variables. When On (default), long predefined PHP variables like $HTTP_GET_VARS will be defined. If you're not using them, it's recommended to turn them off, for performance reasons. Instead, use the superglobal arrays, like $_GET. This directive became available in PHP 5.0.0.
Warning

이 기능은 PHP 5.3.0부터 배제되어, PHP 5.4.0부터 제거됩니다.

enable_post_data_reading boolean
Disabling this option causes $_POST and $_FILES not to be populated. The only way to read postdata will then be through the php://input stream wrapper. This can be useful to proxy requests or to process the POST data in a memory efficient fashion.
post_max_size integer
Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. Generally speaking, memory_limit should be larger than post_max_size. integer 형이 사용될 때, 바이트로 값을 측정합니다. FAQ에 나와 있는 단축어를 사용할 수도 있습니다. If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. This can be tracked in various ways, e.g. by passing the $_GET variable to the script processing the data, i.e. <form action="edit.php?processed=1">, and then checking if $_GET['processed'] is set.

Note:

PHP allows shortcuts for byte values, including K (kilo), M (mega) and G (giga). PHP will do the conversions automatically if you use any of these. Be careful not to exceed the 32 bit signed integer limit (if you're using 32bit versions) as it will cause your script to fail.

Changelog for post_max_size
버전 | 설명
5.3.4 | post_max_size = 0 will not disable the limit when the content type is application/x-www-form-urlencoded or is not registered with PHP.
5.3.2 , 5.2.12 | Allow unlimited post size by setting post_max_size to 0.

auto_prepend_file string

Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require function, so include_path is used.

The special value none disables auto-prepending.

auto_append_file string

Specifies the name of a file that is automatically parsed after the main file. The file is included as if it was called with the require function, so include_path is used.

The special value none disables auto-appending.

Note: If the script is terminated with exit(), auto-append will not occur.

default_mimetype string

By default, PHP will output a media type using the Content-Type header. To disable this, simply set it to be empty.

PHP's built-in default media type is set to text/html.

default_charset string

In PHP 5.6 onwards, "UTF-8" is the default value and its value is used as the default character encoding for htmlentities(), html_entity_decode() and htmlspecialchars() if the encoding parameter is omitted. The value of default_charset will also be used to set the default character set for iconv functions if the iconv.input_encoding, iconv.output_encoding and iconv.internal_encoding configuration options are unset, and for mbstring functions if the mbstring.http_input mbstring.http_output mbstring.internal_encoding configuration option is unset.

All versions of PHP will use this value as the charset within the default Content-Type header sent by PHP if the header isn't overridden by a call to header().

Setting default_charset to an empty value is not recommended.

input_encoding string

Available from PHP 5.6.0. This setting is used for multibyte modules such as mbstring and iconv. Default is empty.

output_encoding string

Available from PHP 5.6.0. This setting is used for multibyte modules such as mbstring and iconv. Default is empty.

internal_encoding string

Available from PHP 5.6.0. This setting is used for multibyte modules such as mbstring and iconv. Default is empty. If empty, default_charset is used.

always_populate_raw_post_data mixed
Warning

This feature was DEPRECATED in PHP 5.6.0, and REMOVED as of PHP 7.0.0.

If set to TRUE, PHP will always populate the $HTTP_RAW_POST_DATA containing the raw POST data. Otherwise, the variable is populated only when the MIME type of the data is unrecognised.

The preferred method for accessing raw POST data is php://input, and $HTTP_RAW_POST_DATA is deprecated in PHP 5.6.0 onwards. Setting always_populate_raw_post_data to -1 will opt into the new behaviour that will be implemented in a future version of PHP, in which $HTTP_RAW_POST_DATA is never defined.

Regardless of the setting, $HTTP_RAW_POST_DATA is not available with enctype="multipart/form-data".

See also: magic_quotes_gpc, magic_quotes_runtime, and magic_quotes_sybase.

Paths and Directories

Paths and Directories Configuration Options
이름 | 기본값 | 가변성 | 변경점
include_path | ".;/path/to/php/pear" | PHP_INI_ALL |
open_basedir | NULL | PHP_INI_ALL | PHP_INI_SYSTEM in PHP < 5.3.0
doc_root | NULL | PHP_INI_SYSTEM |
user_dir | NULL | PHP_INI_SYSTEM |
extension_dir | "/path/to/php" | PHP_INI_SYSTEM |
extension | NULL | php.ini only |
zend_extension | NULL | php.ini only |
zend_extension_debug | NULL | php.ini only | Available before PHP 5.3.0.
zend_extension_debug_ts | NULL | php.ini only | Available before PHP 5.3.0.
zend_extension_ts | NULL | php.ini only | Available before PHP 5.3.0.
cgi.check_shebang_line | "1" | PHP_INI_SYSTEM | Available since PHP 5.2.0.
cgi.discard_path | "0" | PHP_INI_SYSTEM | Available since PHP 5.3.0.
cgi.fix_pathinfo | "1" | PHP_INI_SYSTEM | Available since PHP 4.3.0. PHP_INI_ALL prior to PHP 5.2.1.
cgi.force_redirect | "1" | PHP_INI_SYSTEM | Available since PHP 4.2.0. PHP_INI_ALL prior to PHP 5.2.1.
cgi.nph | "0" | PHP_INI_SYSTEM | Available since PHP 5.3.0.
cgi.redirect_status_env | NULL | PHP_INI_SYSTEM | Available since PHP 4.2.0. PHP_INI_ALL prior to PHP 5.2.1.
cgi.rfc2616_headers | "0" | PHP_INI_ALL | Available since PHP 4.3.0.
fastcgi.impersonate | "0" | PHP_INI_SYSTEM | Available since PHP 4.3.0. PHP_INI_ALL prior to PHP 5.2.1.
fastcgi.logging | "1" | PHP_INI_SYSTEM | Available since PHP 4.3.0. PHP_INI_ALL prior to PHP 5.2.1.

위 설정 지시어에 대한 간단한 설명입니다.

include_path string

Specifies a list of directories where the require, include, fopen(), file(), readfile() and file_get_contents() functions look for files. The format is like the system's PATH environment variable: a list of directories separated with a colon in Unix or semicolon in Windows.

PHP considers each entry in the include path separately when looking for files to include. It will check the first path, and if it doesn't find it, check the next path, until it either locates the included file or returns with a warning or an error. You may modify or set your include path at runtime using set_include_path().

Example #1 Unix include_path

include_path=".:/php/includes"

Example #2 Windows include_path

include_path=".;c:\php\includes"

Using a . in the include path allows for relative includes as it means the current directory. However, it is more efficient to explicitly use include './file' than having PHP always check the current directory for every include.

Note:

ENV variables are also accessible in .ini files. As such it is possible to reference the home directory using ${LOGIN} and ${USER}.

Environment variables may vary between Server APIs as those environments may be different.

Example #3 Unix include_path using ${USER} env variable

include_path = ".:${USER}/pear/php"

open_basedir string

Limit the files that can be accessed by PHP to the specified directory-tree, including the file itself. This directive is NOT affected by whether Safe Mode is turned On or Off.

When a script tries to access the filesystem, for example using include, or fopen(), the location of the file is checked. When the file is outside the specified directory-tree, PHP will refuse to access it. All symbolic links are resolved, so it's not possible to avoid this restriction with a symlink. If the file doesn't exist then the symlink couldn't be resolved and the filename is compared to (a resolved) open_basedir .

open_basedir can affect more than just filesystem functions; for example if MySQL is configured to use mysqlnd drivers, LOAD DATA INFILE will be affected by open_basedir . Much of the extended functionality of PHP uses open_basedir in this way.

The special value . indicates that the working directory of the script will be used as the base-directory. This is, however, a little dangerous as the working directory of the script can easily be changed with chdir().

In httpd.conf, open_basedir can be turned off (e.g. for some virtual hosts) the same way as any other configuration directive with "php_admin_value open_basedir none".

Under Windows, separate the directories with a semicolon. On all other systems, separate the directories with a colon. As an Apache module, open_basedir paths from parent directories are now automatically inherited.

The restriction specified with open_basedir is a directory name since PHP 5.2.16 and 5.3.4. Previous versions used it as a prefix. This means that "open_basedir = /dir/incl" also allowed access to "/dir/include" and "/dir/incls" if they exist. When you want to restrict access to only the specified directory, end with a slash. For example: open_basedir = /dir/incl/

The default is to allow all files to be opened.

Note:

As of PHP 5.3.0 open_basedir can be tightened at run-time. This means that if open_basedir is set to /www/ in php.ini a script can tighten the configuration to /www/tmp/ at run-time with ini_set(). When listing several directories, you can use the PATH_SEPARATOR constant as a separator regardless of the operating system.

doc_root string

PHP's "root directory" on the server. Only used if non-empty. If PHP is configured with 안전 모드, no files outside this directory are served. If PHP was not compiled with FORCE_REDIRECT, you should set doc_root if you are running PHP as a CGI under any web server (other than IIS). The alternative is to use the cgi.force_redirect configuration below.

user_dir string

The base name of the directory used on a user's home directory for PHP files, for example public_html .

extension_dir string

In what directory PHP should look for dynamically loadable extensions. See also: enable_dl, and dl().

extension string

Which dynamically loadable extensions to load when PHP starts up.

zend_extension string

Name of dynamically loadable Zend extension (for example APD) to load when PHP starts up.

zend_extension_debug string

Variant of zend_extension for extensions compiled with debug info prior to PHP 5.3.0.

zend_extension_debug_ts string

Variant of zend_extension for extensions compiled with debug info and thread safety prior to PHP 5.3.0.

zend_extension_ts string

Variant of zend_extension for extensions compiled with thread safety prior to PHP 5.3.0.

cgi.check_shebang_line boolean

Controls whether CGI PHP checks for line starting with #! (shebang) at the top of the running script. This line might be needed if the script support running both as stand-alone script and via PHP CGI. PHP in CGI mode skips this line and ignores its content if this directive is turned on.

cgi.discard_path boolean

If this is enabled, the PHP CGI binary can safely be placed outside of the web tree and people will not be able to circumvent .htaccess security.

cgi.fix_pathinfo boolean

Provides real PATH_INFO/ PATH_TRANSLATED support for CGI. PHP's previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok what PATH_INFO is. For more information on PATH_INFO, see the CGI specs. Setting this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting of zero causes PHP to behave as before. It is turned on by default. You should fix your scripts to use SCRIPT_FILENAME rather than PATH_TRANSLATED.

cgi.force_redirect boolean

cgi.force_redirect is necessary to provide security running PHP as a CGI under most web servers. Left undefined, PHP turns this on by default. You can turn it off at your own risk.

Note:

Windows Users: When using IIS this option must be turned off. For OmniHTTPD or Xitami the same applies.

cgi.nph boolean

If cgi.nph is enabled it will force cgi to always sent Status: 200 with every request.

cgi.redirect_status_env string

If cgi.force_redirect is turned on, and you are not running under Apache or Netscape (iPlanet) web servers, you may need to set an environment variable name that PHP will look for to know it is OK to continue execution.

Note:

Setting this variable may cause security issues, know what you are doing first.

cgi.rfc2616_headers int

Tells PHP what type of headers to use when sending HTTP response code. If it's set to 0, PHP sends a » RFC 3875 "Status:" header that is supported by Apache and other web servers. When this option is set to 1, PHP will send » RFC 2616 compliant headers.

If this option is enabled, and you are running PHP in a CGI environment (e.g. PHP-FPM) you should not use standard RFC 2616 style HTTP status response headers, you should instead use their RFC 3875 equivalent e.g. instead of header("HTTP/1.0 404 Not found"); you should use header("Status: 404 Not Found");

Leave it set to 0 unless you know what you're doing.

fastcgi.impersonate string

FastCGI under IIS (on WINNT based OS) supports the ability to impersonate security tokens of the calling client. This allows IIS to define the security context that the request runs under. mod_fastcgi under Apache does not currently support this feature (03/17/2002) Set to 1 if running under IIS. Default is zero.

fastcgi.logging boolean

Turns on SAPI logging when using FastCGI. Default is to enable logging.

File Uploads

File Uploads Configuration Options
이름 | 기본값 | 가변성 | 변경점
file_uploads | "1" | PHP_INI_SYSTEM | PHP_INI_ALL in PHP <= 4.2.3. Available since PHP 4.0.3.
upload_tmp_dir | NULL | PHP_INI_SYSTEM |
max_input_nesting_level | 64 | PHP_INI_PERDIR | Available since PHP 5.3.9.
max_input_vars | 1000 | PHP_INI_PERDIR | Available since PHP 5.3.9.
upload_max_filesize | "2M" | PHP_INI_PERDIR | PHP_INI_ALL in PHP <= 4.2.3.
max_file_uploads | 20 | PHP_INI_SYSTEM | Available since PHP 5.2.12.

위 설정 지시어에 대한 간단한 설명입니다.

file_uploads boolean

Whether or not to allow HTTP file uploads. See also the upload_max_filesize, upload_tmp_dir, and post_max_size directives.

upload_tmp_dir string

The temporary directory used for storing files when doing file upload. Must be writable by whatever user PHP is running as. If not specified PHP will use the system's default.

If the directory specified here is not writable, PHP falls back to the system default temporary directory. If open_basedir is on, then the system default directory must be allowed for an upload to succeed.

upload_max_filesize integer

The maximum size of an uploaded file.

integer 형이 사용될 때, 바이트로 값을 측정합니다. FAQ에 나와 있는 단축어를 사용할 수도 있습니다.
max_file_uploads integer

The maximum number of files allowed to be uploaded simultaneously. Starting with PHP 5.3.4, upload fields left blank on submission do not count towards this limit.

General SQL

General SQL Configuration Options
이름 | 기본값 | 가변성 | 변경점
sql.safe_mode | "0" | PHP_INI_SYSTEM |

위 설정 지시어에 대한 간단한 설명입니다.

sql.safe_mode boolean

If turned on, database connection functions that specify default values will use those values in place of any user-supplied arguments. For details on the default values, see the documentation for the relevant connection functions.

Windows Specific

Windows Specific Configuration Options
이름 | 기본값 | 가변성 | 변경점
windows.show_crt_warning | "0" | PHP_INI_ALL | Available since PHP 5.4.0.

위 설정 지시어에 대한 간단한 설명입니다.

windows.show_crt_warning boolean

This directive shows the Windows CRT warnings when enabled. These warnings were displayed by default until PHP 5.4.0.

User Contributed Notes

fernandobasso dot br at gmail dot com 18-Oct-2016 05:06
This might help in case someone happens to maintain old applications with a charset other than utf-8.

According to the docs, you can override the default charset if you use `header()`.

Suppose php.ini sets the default_charset to "UTF-8", but you need a legacy charset, like ISO-8859-1.

Still,

<?php header('Content-Type: text/html; Charset=ISO-8859-1'); ?>

would not override the charset, just add it as well and the result
was a response header like (note the two charsets):

    Content-Type:"text/html; Charset=ISO-8859-1;CHARSET=ks_c_5601-1987"

I found it strange the default one as `charset` with a lowercase `c`
as opposed to my custom charset with an uppercase `C`.

What solved was to _override_ the charset using all lowercase letters
as well for the word "charset":

<?php header('content-type: text/html; charset=ISO-8859-1'); ?>

Then, the double charset from the response headers disappeared, and only the single, custom charset remained.
Mohammad Hossein Darvishanpour 17-Apr-2016 11:40
for upload :
file_uploads = On
upload_tmp_dir = " "
upload_max_filesize = 200M
max_file_uploads = 100      //Number
post_max_size = 8M

for error :
error_reporting = E_ALL
display_errors = Off
log_errors = On
error_log = error.log

for buffering :
output_buffering = on

for function :
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
,curl_exec,curl_multi_exec,parse_ini_file,show_source
dougal at gunters dot org 04-Mar-2016 02:11
It appears that if you use both the 'include_path' directives and 'open_basedir', that file searches will hit the include path *first*, before local files. But if 'open_basedir' is not in use, then local files are found first. For example, suppose you have code in '/var/www/myfile.php' which does:

<?php
  require_once('config.php');
?>

Further, assume that there is a local file '/var/www/config.php', and there is also a file '/var/local/php/config.php'.

Next, if your php.ini has:

  include_path = /var/local/php/

Normally, this would look for '/var/www/config.php' first, and if not found, then it would try '/var/local/php/config.php'.

But if you also have this in php.ini:

  open_basedir = /var/www/:/var/local/php/

Then the require would reverse the order of the search, and load '/var/local/php/config.php', even when the local 'config.php' file exists.

Furthermore, if include_path contains directories not in open_basedir, you can end up with a fatal error. For example, change the directive to:

  open_basedir = /var/www/:/var/local/includes/php/

Now the require will first find '/var/local/php/config.php' from the include_path, try to include it, but be unable to because of the open_basedir restrictions.
txyoji at yahoo dot com 22-Nov-2013 05:24
If you enable "open_basedir" option, it will disable the realpath_cache. This can be a significant performance hit.

https://bugs.php.net/bug.php?id=53263
michael dot buergi at gmx dot net 09-May-2013 03:18
the ini-setting "detect_unicode" is indeed "zend.detect_unicode" (according to the phpinfo of my 5.4.4)
Steven Hartland 05-Oct-2012 11:11
default_charset doesn't always set the charset for all content types, it appears to only do so for some known ones such as text/html.

If your using other content type's ensure you set the charset manually.

Tested on php 5.3.10 with application/json
iko at vision dot sk 01-May-2012 05:49
auto_globals_jit setting is also affecting $_REQUEST superglobal in 5.3 It is not explicitly stated in documentation.
kghbln 18-Nov-2011 12:09
Adding multiple directories to open_basedir:

open_basedir = "/var/www/htdocs/:/var/www/tmp/" adds both paths /var/www/htdocs/ and /var/www/tmp/. Do not forget the trailing slash, otherwise the last directory will be considered as a prefix (< 5.3.4).

On Windows you use ; as the seperator.
JL 09-Sep-2011 06:46
If you need to use a path in your include_path that has a space in it - I found that I could make it work (in windows anyway) by setting a path like this
L:\Information Technology\Resources\lib\
in the include path like this listed below

include_path = ".;L:\Information" " Technology\Resources\lib\"
AntonioK 10-Mar-2011 02:29
Note that there is no way to disable eval() work by using disable_functions directive, because eval() is a language construct and not a function.

Many people advise to disable such potentially-insecure functions like system(), exec(), passthru(), eval() and so on in php.ini when not running in safe mode, but eval() would still work even it listed in disable_functions.
rlammers at linuxmail dot org 09-Jan-2011 01:23
This is a possible solution for a problem which seems to be a php-ini-problem but is not.

If a $_POST is used with large fields e.g. textarea's with more than 120kb characters php returns a blank screen, even if the max_post_size is 8M.

This problem may be caused by an apache-module SecFilter.

Adding the following lines to the .htaccess solves the problem.
SecFilterEngine Off
SecFilterScanPOST Off

I know this is not a php-issue, but i'm still posting it here since it looks like it is a php-problem and I did not find any sites or forums offering this solution.
amolitor at molitor-design dot com 11-Feb-2010 12:23
Amusingly, the include_path logically includes the current directory of the running file as the last entry all the time anyways, so part of the business about shoving "." into the include_path is spurious -- it's "there" on the end all the time, at least in the 5.2.12 source (see main/fopen_wrappers.c around line 503).

This one had me goin' for a while.
mrok at mrok dot com 22-Mar-2009 05:29
Note that on some Unix systems (i.e. PHP 5.1.6 on Centos 5.2) include_path in php.ini should NOT be quoted.

For example, instead of

 include_path='.:/usr/share/php'

use

 include_path=.:/usr/share/php

Using quotes does not cause any error message, but all of your require_once() directives will fail (indicating that file could not be opened) - unless full path to target file is provided.
lanny at freemail dot hu 26-Dec-2006 12:05
register_long_arrays has a very odd behavior (at least in PHP 5.2):

With register_long_arrays=Off the $GLOBALS array will not contain [_SERVER] and [_REQUEST]. They are accessible as superglobals ($_SERVER, $_REQUEST), but they disappear from the $GLOBALS array!
leo at korfu dot cz 30-May-2006 12:45
"If the size of post data is greater than post_max_size..."

It seems that a more elegant way is comparison between post_max_size and $_SERVER['CONTENT_LENGTH']. Please note that the latter includes not only size of uploaded file plus post data but also multipart sequences. Leo
csg at DEL_THISdiatom dot de 02-Sep-2005 11:55
Starting with PHP 4.4.0 (at least PHP version 4.3.10 did have old, documented behaviour) interpretation of value of "session.save_path" did change in conjunction with "save_mode" and "open_basedir" enabled.

Documented ( http://de.php.net/manual/en/ref.session.php#ini.session.save-path ):
  Values of "session.save_path" should or may be  **without**  ending slash.
  For instance:
<?php
  // Valid only  *before* PHP 4.4.0:
  ini_set( "session.save_path", "/var/httpd/kunde/phptmp" );
?> will mean:
  The directory "/var/httpd/kunde/phptmp/" will be used to write data and therefore must be writable by the web server.

Starting with PHP 4.4.0 the server complains that "/var/httpd/kunde/" is not writable.
Solution: Add an ending slash in call of ini_set (or probably whereever you set "session.save_path"), e.g.:
<?php
  // Note the slash on ".....phptmp/":
  ini_set( "session.save_path", "/var/httpd/kunde/phptmp/" );
?>

Hope, that does help someone.
Manu 27-Jul-2005 02:02
Warning, if you change upload_max_filesize, please note that both post_max_size and memory_limit directives may have to change too.
david dot tulloh at infaze dot com dot au 27-Jun-2005 03:19
I did a little experimentation on the behaviour of auto_prepend_file as the order of access wasn't clear to me from the documentation.

1. Apache checks to see if the target file exists.
2. The prepend file is called
3. The target file is called

This means that you can use auto_prepend_file to change the target file before you access it however you can't generate new files.


확장 목록/분류

Table of Contents

  • 알파벳순
  • 자격
  • 상태

이 부록은 PHP 매뉴얼에 문서화된 150개가 넘는 확장을 몇몇 기준에 따라 분류한 것입니다.

User Contributed Notes

There are no user contributed notes for this page.


알파벳순

  • 아파치
  • APC
  • APD
  • 배열
  • BBCode
  • BC 수학
  • bcompiler
  • Bzip2
  • Cairo
  • Calendar
  • chdb
  • Classkit
  • 클래스/객체
  • COM
  • Crack
  • Ctype
  • CUBRID
  • cURL
  • Cyrus
  • Date/Time
  • DBA
  • dBase
  • DB++
  • dbx
  • Direct IO
  • 디렉토리
  • DOM
  • Eio
  • Enchant
  • Error Handling
  • Ev
  • Event
  • Program execution
  • Exif
  • Expect
  • FAM
  • FrontBase
  • FDF
  • Fileinfo
  • filePro
  • 파일시스템
  • Filter
  • FriBiDi
  • FTP
  • Function Handling
  • Gearman
  • Gender
  • GeoIP
  • Gettext
  • Gmagick
  • GMP
  • GnuPG
  • Gupnp
  • haru
  • Hash
  • htscanner
  • Hyperwave API
  • Firebird/InterBase
  • IBM DB2
  • iconv
  • ID3
  • Informix
  • IIS
  • GD
  • ImageMagick
  • IMAP
  • inclued
  • PHP Options/Info
  • Ingres
  • Inotify
  • intl
  • JSON
  • Judy
  • KADM5
  • KTaglib
  • Lapack
  • LDAP
  • Libevent
  • libxml
  • Lua
  • LZF
  • Mail
  • Mailparse
  • Math
  • MaxDB
  • Multibyte String
  • Mcrypt
  • MCVE
  • Memcache
  • Memcached
  • Memtrack
  • Mhash
  • Mimetype
  • Ming
  • Misc.
  • mnoGoSearch
  • Mongo
  • mqseries
  • Msession
  • mSQL
  • Mssql
  • MySQL (Original)
  • MySQLi
  • Mysqlnd
  • mysqlnd_memcache
  • mysqlnd_ms
  • mysqlnd_mux
  • mysqlnd_qc
  • mysqlnd_uh
  • Ncurses
  • Gopher
  • Network
  • Newt
  • YP/NIS
  • NSAPI
  • OAuth
  • OCI8
  • oggvorbis
  • OpenAL
  • OpenSSL
  • Output Control
  • Paradox
  • Parsekit
  • Password Hashing
  • PCNTL
  • PCRE
  • PDF
  • PDO
  • 4D (PDO)
  • CUBRID (PDO)
  • MS SQL Server (PDO)
  • Firebird (PDO)
  • IBM (PDO)
  • Informix (PDO)
  • MySQL (PDO)
  • Oracle (PDO)
  • ODBC and DB2 (PDO)
  • PostgreSQL (PDO)
  • SQLite (PDO)
  • MS SQL Server (PDO)
  • PostgreSQL
  • Phar
  • POSIX
  • Proctitle
  • PS
  • Pspell
  • pthreads
  • Quickhash
  • Radius
  • Rar
  • Readline
  • Recode
  • Reflection
  • POSIX Regex
  • RPM Reader
  • RRD
  • runkit
  • SAM
  • SCA
  • scream
  • SDO
  • SDO DAS XML
  • SDO-DAS-Relational
  • Semaphore
  • 세션
  • Session PgSQL
  • Shared Memory
  • SimpleXML
  • SNMP
  • SOAP
  • Sockets
  • Solr
  • Sphinx
  • SPL
  • SPL Types
  • SPPLUS
  • SQLite
  • SQLite3
  • SQLSRV
  • ssdeep
  • SSH2
  • Statistics
  • Stomp
  • Streams
  • Strings
  • SVM
  • SVN
  • Swish
  • Sybase
  • Sync
  • Taint
  • TCP
  • Tidy
  • Tokenizer
  • tokyo_tyrant
  • Trader
  • ODBC
  • URLs
  • V8js
  • Variable handling
  • Varnish
  • vpopmail
  • WDDX
  • Weakref
  • win32ps
  • win32service
  • WinCache
  • xattr
  • xdiff
  • Xhprof
  • XML Parser
  • XMLReader
  • XML-RPC
  • XMLWriter
  • XSL
  • Yaf
  • Yaml
  • YAZ
  • Zip
  • Zlib
  • Zookeeper

User Contributed Notes

There are no user contributed notes for this page.


자격

코어 확장

이 확장은 실제 확장이 아닙니다. PHP 코어의 일부분이며 컴파일 옵션으로 PHP 바이너리에서 제외할 수 없습니다.

  • 배열

  • 클래스/객체

  • Date/Time

  • 디렉토리

  • Error Handling

  • Program execution

  • 파일시스템

  • Filter

  • Function Handling

  • Hash

  • PHP Options/Info

  • Mail

  • Math

  • Misc.

  • Network

  • Output Control

  • Password Hashing

  • Phar

  • Reflection

  • POSIX Regex

  • 세션

  • SPL

  • Streams

  • Strings

  • Tokenizer

  • URLs

  • Variable handling

번들 확장

이 확장은 PHP에 번들됩니다.

  • 아파치

  • BC 수학

  • Calendar

  • COM

  • Ctype

  • DBA

  • Exif

  • Fileinfo

  • FTP

  • iconv

  • GD

  • intl

  • JSON

  • Multibyte String

  • NSAPI

  • PCNTL

  • PCRE

  • PDO

  • POSIX

  • Semaphore

  • Shared Memory

  • Sockets

  • SQLite3

  • XML-RPC

  • Zlib

외부 확장

이 확장들은 PHP에 번들되지만, 컴파일할 때는 외부 라이브러리가 필요합니다.

  • Bzip2

  • cURL

  • dBase

  • DOM

  • Enchant

  • FrontBase

  • Gettext

  • GMP

  • Firebird/InterBase

  • Informix

  • IMAP

  • LDAP

  • libxml

  • Mcrypt

  • Mhash

  • mSQL

  • Mssql

  • MySQL (Original)

  • MySQLi

  • Mysqlnd

  • OCI8

  • OpenSSL

  • MS SQL Server (PDO)

  • Firebird (PDO)

  • MySQL (PDO)

  • Oracle (PDO)

  • ODBC and DB2 (PDO)

  • PostgreSQL (PDO)

  • SQLite (PDO)

  • PostgreSQL

  • Pspell

  • Readline

  • Recode

  • SimpleXML

  • SNMP

  • SOAP

  • Sybase

  • Tidy

  • ODBC

  • WDDX

  • XML Parser

  • XMLReader

  • XMLWriter

  • XSL

  • Zip

PECL 확장

이 확장은 » PECL에서 받을 수 있습니다. 외부 라이브러리가 필요할 수도 있습니다. PHP 매뉴얼에 기재되지 않은 많은 PECL 확장이 존재합니다.

  • APC

  • APD

  • BBCode

  • bcompiler

  • Cairo

  • chdb

  • Classkit

  • Crack

  • CUBRID

  • Cyrus

  • DB++

  • dbx

  • Direct IO

  • Eio

  • Ev

  • Event

  • Expect

  • FAM

  • FDF

  • filePro

  • FriBiDi

  • Gearman

  • Gender

  • GeoIP

  • Gmagick

  • GnuPG

  • Gupnp

  • haru

  • htscanner

  • Hyperwave API

  • IBM DB2

  • ID3

  • IIS

  • ImageMagick

  • inclued

  • Ingres

  • Inotify

  • Judy

  • KADM5

  • KTaglib

  • Lapack

  • Libevent

  • Lua

  • LZF

  • Mailparse

  • MaxDB

  • MCVE

  • Memcache

  • Memcached

  • Memtrack

  • Mimetype

  • Ming

  • mnoGoSearch

  • Mongo

  • mqseries

  • Msession

  • mysqlnd_memcache

  • mysqlnd_ms

  • mysqlnd_mux

  • mysqlnd_qc

  • mysqlnd_uh

  • Ncurses

  • Gopher

  • Newt

  • YP/NIS

  • OAuth

  • oggvorbis

  • OpenAL

  • Paradox

  • Parsekit

  • PDF

  • 4D (PDO)

  • CUBRID (PDO)

  • IBM (PDO)

  • Informix (PDO)

  • MS SQL Server (PDO)

  • Proctitle

  • PS

  • pthreads

  • Quickhash

  • Radius

  • Rar

  • RPM Reader

  • RRD

  • runkit

  • SAM

  • SCA

  • scream

  • SDO

  • SDO DAS XML

  • SDO-DAS-Relational

  • Session PgSQL

  • Solr

  • Sphinx

  • SPL Types

  • SPPLUS

  • SQLite

  • SQLSRV

  • ssdeep

  • SSH2

  • Statistics

  • Stomp

  • SVM

  • SVN

  • Swish

  • Taint

  • TCP

  • tokyo_tyrant

  • Trader

  • V8js

  • Varnish

  • vpopmail

  • Weakref

  • win32ps

  • win32service

  • WinCache

  • xattr

  • xdiff

  • Xhprof

  • Yaf

  • Yaml

  • YAZ

  • Zookeeper

User Contributed Notes

There are no user contributed notes for this page.


상태

여기 있는 확장은 제품에 사용하지 적합하지 않습니다 - 너무 "낡은"(배제된) 확장이거나, "새로운"(실험적인) 확장입니다.

배제된 확장

이 확장은 다른 확장의 기능으로 인하여 배제될 확장입니다.

  • Memtrack

  • Mimetype

실험적 확장

이 확장의 동작-함수 이름과 확장에 대한 문서를 포함한 모든 것-은 PHP 다음 버전에서 아무런 설명 없이 변경될 수 있습니다. 이 확장으로 인한 문제는 스스로 책임져야 합니다.

  • bcompiler

  • DB++

  • haru

  • Hash

  • ImageMagick

  • Ming

  • Ncurses

  • Newt

  • Paradox

  • MS SQL Server (PDO)

  • Firebird (PDO)

  • Oracle (PDO)

  • PS

  • SCA

  • SDO-DAS-Relational

  • Swish

  • vpopmail

  • XML-RPC

User Contributed Notes

There are no user contributed notes for this page.


함수 별칭 목록

PHP에는 하나 이상의 이름으로 호출할 수 있는 함수들이 몇 존재 합니다. 어떤 경우에는 여러 이름들 중에서 추천하는 이름이 없는 경우도 있습니다. is_int()와 is_integer()는 동등하게 사용하는 사례입니다. 하지만, API 정비나 다른 이유들로 인해 함수의 이름을 변경하고, 기존의 이름은 하위호환성을 유지하기 위한 목적으로만 별칭으로 유지합니다. 이러한 별칭은 제거되거나 변경될 수 있기 때문에 이것을 사용하는 것은 일반적으로 좋지 않은 생각이며, 이식할 수 없는 스크립트를 만들게 됩니다. 이 목록은 오래된 스크립트를 새로운 문법으로 갱신하고자 하는 사람들을 돕기 위해 제공합니다.

별칭
별칭 | 주 함수 | 확장
_ | gettext() | Gettext
add | swfmovie_add() | Ming (flash)
add | swfsprite_add() | Ming (flash)
addaction | swfbutton_addAction() | Ming (flash)
addcolor | swfdisplayitem_addColor() | Ming (flash)
addentry | swfgradient_addEntry() | Ming (flash)
addfill | swfshape_addfill() | Ming (flash)
addshape | swfbutton_addShape() | Ming (flash)
addstring | swftext_addString() | Ming (flash)
addstring | swftextfield_addString() | Ming (flash)
align | swftextfield_align() | Ming (flash)
chop | rtrim() | Base syntax
close | closedir() | Base syntax
com_get | com_propget() | COM
com_propset | com_propput() | COM
com_set | com_propput() | COM
die | exit() | Miscellaneous functions
diskfreespace | disk_free_space() | Filesystem
doubleval | floatval() | Base syntax
drawarc | swfshape_drawarc() | Ming (flash)
drawcircle | swfshape_drawcircle() | Ming (flash)
drawcubic | swfshape_drawcubic() | Ming (flash)
drawcubicto | swfshape_drawcubicto() | Ming (flash)
drawcurve | swfshape_drawcurve() | Ming (flash)
drawcurveto | swfshape_drawcurveto() | Ming (flash)
drawglyph | swfshape_drawglyph() | Ming (flash)
drawline | swfshape_drawline() | Ming (flash)
drawlineto | swfshape_drawlineto() | Ming (flash)
fbsql | fbsql_db_query() | FrontBase
fputs | fwrite() | Base syntax
getascent | swffont_getAscent() | Ming (flash)
getascent | swftext_getAscent() | Ming (flash)
getdescent | swffont_getDescent() | Ming (flash)
getdescent | swftext_getDescent() | Ming (flash)
getheight | swfbitmap_getHeight() | Ming (flash)
getleading | swffont_getLeading() | Ming (flash)
getleading | swftext_getLeading() | Ming (flash)
getshape1 | swfmorph_getShape1() | Ming (flash)
getshape2 | swfmorph_getShape2() | Ming (flash)
getwidth | swfbitmap_getWidth() | Ming (flash)
getwidth | swffont_getWidth() | Ming (flash)
getwidth | swftext_getWidth() | Ming (flash)
gzputs | gzwrite() | Zlib
i18n_convert | mb_convert_encoding() | Multi-bytes Strings
i18n_discover_encoding | mb_detect_encoding() | Multi-bytes Strings
i18n_http_input | mb_http_input() | Multi-bytes Strings
i18n_http_output | mb_http_output() | Multi-bytes Strings
i18n_internal_encoding | mb_internal_encoding() | Multi-bytes Strings
i18n_ja_jp_hantozen | mb_convert_kana() | Multi-bytes Strings
i18n_mime_header_decode | mb_decode_mimeheader() | Multi-bytes Strings
i18n_mime_header_encode | mb_encode_mimeheader() | Multi-bytes Strings
imap_create | imap_createmailbox() | IMAP
imap_fetchtext | imap_body() | IMAP
imap_getmailboxes | imap_list_full() | IMAP
imap_getsubscribed | imap_lsub_full() | IMAP
imap_header | imap_headerinfo() | IMAP
imap_listmailbox | imap_list() | IMAP
imap_listsubscribed | imap_lsub() | IMAP
imap_rename | imap_renamemailbox() | IMAP
imap_scan | imap_listscan() | IMAP
imap_scanmailbox | imap_listscan() | IMAP
ini_alter | ini_set() | Base syntax
is_double | is_float() | Base syntax
is_integer | is_int() | Base syntax
is_long | is_int() | Base syntax
is_real | is_float() | Base syntax
is_writeable | is_writable() | Base syntax
join | implode() | Base syntax
key_exists | array_key_exists() | Base syntax
labelframe | swfmovie_labelFrame() | Ming (flash)
labelframe | swfsprite_labelFrame() | Ming (flash)
ldap_close | ldap_unbind() | LDAP
magic_quotes_runtime | set_magic_quotes_runtime() | Base syntax
mbstrcut | mb_strcut() | Multi-bytes Strings
mbstrlen | mb_strlen() | Multi-bytes Strings
mbstrpos | mb_strpos() | Multi-bytes Strings
mbstrrpos | mb_strrpos() | Multi-bytes Strings
mbsubstr | mb_substr() | Multi-bytes Strings
ming_setcubicthreshold | ming_setCubicThreshold() | Ming (flash)
ming_setscale | ming_setScale() | Ming (flash)
move | swfdisplayitem_move() | Ming (flash)
movepen | swfshape_movepen() | Ming (flash)
movepento | swfshape_movepento() | Ming (flash)
moveto | swfdisplayitem_moveTo() | Ming (flash)
moveto | swffill_moveTo() | Ming (flash)
moveto | swftext_moveTo() | Ming (flash)
msql | msql_db_query() | mSQL
msql_createdb | msql_create_db() | mSQL
msql_dbname | msql_result() | mSQL
msql_dropdb | msql_drop_db() | mSQL
msql_fieldflags | msql_field_flags() | mSQL
msql_fieldlen | msql_field_len() | mSQL
msql_fieldname | msql_field_name() | mSQL
msql_fieldtable | msql_field_table() | mSQL
msql_fieldtype | msql_field_type() | mSQL
msql_freeresult | msql_free_result() | mSQL
msql_listdbs | msql_list_dbs() | mSQL
msql_listfields | msql_list_fields() | mSQL
msql_listtables | msql_list_tables() | mSQL
msql_numfields | msql_num_fields() | mSQL
msql_numrows | msql_num_rows() | mSQL
msql_regcase | sql_regcase() | mSQL
msql_selectdb | msql_select_db() | mSQL
msql_tablename | msql_result() | mSQL
mssql_affected_rows | sybase_affected_rows() | Sybase
mssql_affected_rows | sybase_affected_rows() | Sybase
mssql_close | sybase_close() | Sybase
mssql_close | sybase_close() | Sybase
mssql_connect | sybase_connect() | Sybase
mssql_connect | sybase_connect() | Sybase
mssql_data_seek | sybase_data_seek() | Sybase
mssql_data_seek | sybase_data_seek() | Sybase
mssql_fetch_array | sybase_fetch_array() | Sybase
mssql_fetch_array | sybase_fetch_array() | Sybase
mssql_fetch_field | sybase_fetch_field() | Sybase
mssql_fetch_field | sybase_fetch_field() | Sybase
mssql_fetch_object | sybase_fetch_object() | Sybase
mssql_fetch_object | sybase_fetch_object() | Sybase
mssql_fetch_row | sybase_fetch_row() | Sybase
mssql_fetch_row | sybase_fetch_row() | Sybase
mssql_field_seek | sybase_field_seek() | Sybase
mssql_field_seek | sybase_field_seek() | Sybase
mssql_free_result | sybase_free_result() | Sybase
mssql_free_result | sybase_free_result() | Sybase
mssql_get_last_message | sybase_get_last_message() | Sybase
mssql_get_last_message | sybase_get_last_message() | Sybase
mssql_min_client_severity | sybase_min_client_severity() | Sybase
mssql_min_error_severity | sybase_min_error_severity() | Sybase
mssql_min_message_severity | sybase_min_message_severity() | Sybase
mssql_min_server_severity | sybase_min_server_severity() | Sybase
mssql_num_fields | sybase_num_fields() | Sybase
mssql_num_fields | sybase_num_fields() | Sybase
mssql_num_rows | sybase_num_rows() | Sybase
mssql_num_rows | sybase_num_rows() | Sybase
mssql_pconnect | sybase_pconnect() | Sybase
mssql_pconnect | sybase_pconnect() | Sybase
mssql_query | sybase_query() | Sybase
mssql_query | sybase_query() | Sybase
mssql_result | sybase_result() | Sybase
mssql_result | sybase_result() | Sybase
mssql_select_db | sybase_select_db() | Sybase
mssql_select_db | sybase_select_db() | Sybase
multcolor | swfdisplayitem_multColor() | Ming (flash)
mysql | mysql_db_query() | MySQL
mysql_createdb | mysql_create_db() | MySQL
mysql_db_name | mysql_result() | MySQL
mysql_dbname | mysql_result() | MySQL
mysql_dropdb | mysql_drop_db() | MySQL
mysql_fieldflags | mysql_field_flags() | MySQL
mysql_fieldlen | mysql_field_len() | MySQL
mysql_fieldname | mysql_field_name() | MySQL
mysql_fieldtable | mysql_field_table() | MySQL
mysql_fieldtype | mysql_field_type() | MySQL
mysql_freeresult | mysql_free_result() | MySQL
mysql_listdbs | mysql_list_dbs() | MySQL
mysql_listfields | mysql_list_fields() | MySQL
mysql_listtables | mysql_list_tables() | MySQL
mysql_numfields | mysql_num_fields() | MySQL
mysql_numrows | mysql_num_rows() | MySQL
mysql_selectdb | mysql_select_db() | MySQL
mysql_tablename | mysql_result() | MySQL
nextframe | swfmovie_nextFrame() | Ming (flash)
nextframe | swfsprite_nextFrame() | Ming (flash)
ociassignelem | OCI-Collection::assignElem | OCI8
ocibindbyname | oci_bind_by_name() | OCI8
ocicancel | oci_cancel() | OCI8
ocicloselob | OCI-Lob::close | OCI8
ocicollappend | OCI-Collection::append | OCI8
ocicollassign | OCI-Collection::assign | OCI8
ocicollmax | OCI-Collection::max | OCI8
ocicollsize | OCI-Collection::size | OCI8
ocicolltrim | OCI-Collection::trim | OCI8
ocicolumnisnull | oci_field_is_null() | OCI8
ocicolumnname | oci_field_name() | OCI8
ocicolumnprecision | oci_field_precision() | OCI8
ocicolumnscale | oci_field_scale() | OCI8
ocicolumnsize | oci_field_size() | OCI8
ocicolumntype | oci_field_type() | OCI8
ocicolumntyperaw | oci_field_type_raw() | OCI8
ocicommit | oci_commit() | OCI8
ocidefinebyname | oci_define_by_name() | OCI8
ocierror | oci_error() | OCI8
ociexecute | oci_execute() | OCI8
ocifetch | oci_fetch() | OCI8
ocifetchinto | oci_fetch_array(), oci_fetch_row(), oci_fetch_assoc(), oci_fetch_object() | OCI8
ocifetchstatement | oci_fetch_all() | OCI8
ocifreecollection | OCI-Collection::free | OCI8
ocifreecursor | oci_free_statement() | OCI8
ocifreedesc | oci_free_descriptor() | OCI8
ocifreestatement | oci_free_statement() | OCI8
ocigetelem | OCI-Collection::getElem | OCI8
ociinternaldebug | oci_internal_debug() | OCI8
ociloadlob | OCI-Lob::load | OCI8
ocilogon | oci_connect() | OCI8
ocinewcollection | oci_new_collection() | OCI8
ocinewcursor | oci_new_cursor() | OCI8
ocinewdescriptor | oci_new_descriptor() | OCI8
ocinlogon | oci_new_connect() | OCI8
ocinumcols | oci_num_fields() | OCI8
ociparse | oci_parse() | OCI8
ocipasswordchange | oci_password_change() | OCI8
ociplogon | oci_pconnect() | OCI8
ociresult | oci_result() | OCI8
ocirollback | oci_rollback() | OCI8
ocisavelob | OCI-Lob::save | OCI8
ocisavelobfile | OCI-Lob::import | OCI8
ociserverversion | oci_server_version() | OCI8
ocisetprefetch | oci_set_prefetch() | OCI8
ocistatementtype | oci_statement_type() | OCI8
ociwritelobtofile | OCI-Lob::export | OCI8
ociwritetemporarylob | OCI-Lob::writeTemporary | OCI8
odbc_do | odbc_exec() | ODBC
odbc_field_precision | odbc_field_len() | ODBC
output | swfmovie_output() | Ming (flash)
pdf_add_outline | pdf_add_bookmark() | PDF
pg_clientencoding | pg_client_encoding() | PostgreSQL
pg_setclientencoding | pg_set_client_encoding() | PostgreSQL
pos | current() | Base syntax
recode | recode_string() | Recode
remove | swfmovie_remove() | Ming (flash)
remove | swfsprite_remove() | Ming (flash)
rotate | swfdisplayitem_rotate() | Ming (flash)
rotateto | swfdisplayitem_rotateTo() | Ming (flash)
rotateto | swffill_rotateTo() | Ming (flash)
save | swfmovie_save() | Ming (flash)
savetofile | swfmovie_saveToFile() | Ming (flash)
scale | swfdisplayitem_scale() | Ming (flash)
scaleto | swfdisplayitem_scaleTo() | Ming (flash)
scaleto | swffill_scaleTo() | Ming (flash)
setaction | swfbutton_setAction() | Ming (flash)
setbackground | swfmovie_setBackground() | Ming (flash)
setbounds | swftextfield_setBounds() | Ming (flash)
setcolor | swftext_setColor() | Ming (flash)
setcolor | swftextfield_setColor() | Ming (flash)
setdepth | swfdisplayitem_setDepth() | Ming (flash)
setdimension | swfmovie_setDimension() | Ming (flash)
setdown | swfbutton_setDown() | Ming (flash)
setfont | swftext_setFont() | Ming (flash)
setfont | swftextfield_setFont() | Ming (flash)
setframes | swfmovie_setFrames() | Ming (flash)
setframes | swfsprite_setFrames() | Ming (flash)
setheight | swftext_setHeight() | Ming (flash)
setheight | swftextfield_setHeight() | Ming (flash)
sethit | swfbutton_setHit() | Ming (flash)
setindentation | swftextfield_setIndentation() | Ming (flash)
setleftfill | swfshape_setleftfill() | Ming (flash)
setleftmargin | swftextfield_setLeftMargin() | Ming (flash)
setline | swfshape_setline() | Ming (flash)
setlinespacing | swftextfield_setLineSpacing() | Ming (flash)
setmargins | swftextfield_setMargins() | Ming (flash)
setmatrix | swfdisplayitem_setMatrix() | Ming (flash)
setname | swfdisplayitem_setName() | Ming (flash)
setname | swftextfield_setName() | Ming (flash)
setover | swfbutton_setOver() | Ming (flash)
setrate | swfmovie_setRate() | Ming (flash)
setratio | swfdisplayitem_setRatio() | Ming (flash)
setrightfill | swfshape_setrightfill() | Ming (flash)
setrightmargin | swftextfield_setRightMargin() | Ming (flash)
setspacing | swftext_setSpacing() | Ming (flash)
setup | swfbutton_setUp() | Ming (flash)
show_source | highlight_file() | Base syntax
sizeof | count() | Base syntax
skewx | swfdisplayitem_skewX() | Ming (flash)
skewxto | swfdisplayitem_skewXTo() | Ming (flash)
skewxto | swffill_skewXTo() | Ming (flash)
skewy | swfdisplayitem_skewY() | Ming (flash)
skewyto | swfdisplayitem_skewYTo() | Ming (flash)
skewyto | swffill_skewYTo() | Ming (flash)
snmpwalkoid | snmprealwalk() | SNMP
strchr | strstr() | Base syntax
streammp3 | swfmovie_streamMp3() | Ming (flash)
swfaction | swfaction_init() | Ming (flash)
swfbitmap | swfbitmap_init() | Ming (flash)
swfbutton | swfbutton_init() | Ming (flash)
swffill | swffill_init() | Ming (flash)
swffont | swffont_init() | Ming (flash)
swfgradient | swfgradient_init() | Ming (flash)
swfmorph | swfmorph_init() | Ming (flash)
swfmovie | swfmovie_init() | Ming (flash)
swfshape | swfshape_init() | Ming (flash)
swfsprite | swfsprite_init() | Ming (flash)
swftext | swftext_init() | Ming (flash)
swftextfield | swftextfield_init() | Ming (flash)
xptr_new_context | xpath_new_context() |

User Contributed Notes

There are no user contributed notes for this page.


예약어 목록

Table of Contents

  • 키워드 목록
  • 예약 클래스
  • 예약 상수

다음은 PHP에서 기선언된 식별자 목록이다. 이 식별자 목록 중 어느것도 스크립트에서 식별자로 사용될수 없다. 이 목록은 키워드와 기선언 변수, 상수, 클래스 명도 포함한다. 이 목록은 철저하지도 완벽하지도 않다.

User Contributed Notes

SR 04-Oct-2007 07:53
[Editorial note: parent and self are reserved class names. Functions and constants with their names can be declared, but not userspace classes]

true, false and null are not listed because they are globally-defined constants, not reserved words.

No idea about parent or self, though.


키워드 목록

이 단어들은 PHP에서 특별한 의미를 갖는다. 이중 일부는 함수처럼 보여지기도하고 상수나 기타 등등 으로 보여질것이다-그러나 그렇지 않다. 실제로: 그들은 언어 구조이다. 다음 단어를 상수, 클래스명, 함수명, 메쏘드명으로 사용할수 없다. 이들을 변수명으로 사용하는 것은 괜찮을수도 있지만, 혼란스럽게 될것이다.

PHP 키워드
abstract (PHP 5부터) | and | array() | as | break
case | catch (PHP 5부터) | cfunction (PHP 4만) | class | clone (PHP 5부터)
const | continue | declare | default | do
else | elseif | enddeclare | endfor | endforeach
endif | endswitch | endwhile | extends | final (PHP 5부터)
for | foreach | function | global | goto (PHP 5.3부터)
if | implements (PHP 5부터) | interface (PHP 5부터) | instanceof (PHP 5부터)
namespace (PHP 5.3부터) | new | old_function (PHP 4만) | or | private (PHP 5부터)
protected (PHP 5부터) | public (PHP 5부터) | static | switch | throw (PHP 5부터)
try (PHP 5부터) | use | var | while | xor
컴파일시 상수
__CLASS__ | __DIR__ (PHP 5.3부터) | __FILE__ | __FUNCTION__ | __METHOD__
__NAMESPACE__ (PHP 5.3부터) |  |  |  |
언어 상수
die() | echo | empty() | exit() | eval()
include | include_once | isset() | list() | require
require_once | return | print | unset() |

User Contributed Notes

Thomas Hansen 15-Aug-2016 07:08
Please note that reserved words are still not allowed to be used as namespace or as part of it:

<?php
namespace MyNameSpace\List;

class Test
{
}
?>

This will fail with a Parse error:  syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING)
martindilling at gmail dot com 16-Apr-2013 07:20
RegEx to find all the keywords:

\b(
(a(bstract|nd|rray|s))|
(c(a(llable|se|tch)|l(ass|one)|on(st|tinue)))|
(d(e(clare|fault)|ie|o))|
(e(cho|lse(if)?|mpty|nd(declare|for(each)?|if|switch|while)|val|x(it|tends)))|
(f(inal|or(each)?|unction))|
(g(lobal|oto))|
(i(f|mplements|n(clude(_once)?|st(anceof|eadof)|terface)|sset))|
(n(amespace|ew))|
(p(r(i(nt|vate)|otected)|ublic))|
(re(quire(_once)?|turn))|
(s(tatic|witch))|
(t(hrow|r(ait|y)))|
(u(nset|se))|
(__halt_compiler|break|list|(x)?or|var|while)
)\b
Chris 05-Oct-2012 01:33
Here they are as arrays:

<?php
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');

$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');
?>

Along with get_defined_functions() and get_defined_constants(), this can be useful for checking eval() statements.
Bob 06-Sep-2009 11:02
There are some cases when you need to use a reserved keyword or language construct as a class method name. In this instance, there is very little chance of namespace conflicts (as the class itself acts as a namespace). If you try to define the method the old way, you will get an unexpected token error.

There is an unobtrusive, and very useful way to use a reserved keyword for a method name. For example, you want to define two class methods 'list' and 'unset' (these two are language builtins and normally not allowed for method names).

<?php
class MyClass
{
    // Define MyClass::unset() with a different name, e.g. 'rm'
    public function rm($arg)
    {
        /* code... */
    }
    // Define MyClass::list() with a different name, e.g. 'ls'
    public function ls($arg = null)
    {
        /* code... */
    }
    // Now define a __call() method (requires PHP > 5.2.3 to take effect)
    public function __call($func, $args)
    {
        switch ($func)
        {
            case 'list':
                return $this->ls((isset($args[0]))? $args[0]: null);
            break;
            case 'unset':
                return $this->rm($args[0]);
            break;
            default:
                trigger_error("Call to undefined method ".__CLASS__."::$func()", E_USER_ERROR);
            die ();
        }
    }
}
?>

The only caveat is that to use the long method names, you need PHP > 5.2.3. However, a nice feature is that if you are using an older version than 5.2.3, all of the __call() stuff is ignored and the class will behave as expected (in other words, it degrades gracefully).

You also need to be aware of the methods' expected arguments. MyClass::ls()'s argument is optional, so the extra isset() check is required. If your methods take more arguments, they will need to be manually dereferenced from the $args array, e.g. <?php return $this->my_func($args[0], $args[1], $args[2]);?> for 3 required arguments.

This is a nice trick, and can let you code better APIs for newer versions of PHP. However, if this script is to be run on older PHP installations, be very sure to use the short method names.


예약 클래스

이 섹션에서는 예약된 표준 클래스를 나열합니다. 다양한 확장이 정의하는 다른 클래스는 각 레퍼런스에 나와 있습니다.

표준 정의 클래스

이 클래스는 PHP 빌드에 포함되어있는 표준 함수 모음에 선언된다.

Directory
dir() 로 생성.
stdClass
object로 형변환 시 생성.
__PHP_Incomplete_Class
unserialize()로 생성될 수 있음.

PHP 5 기선언 클래스

여기 추가된 기선언 클래스는 PHP 5.0.0에서 소개되었다.

exception
php_user_filter

Closure

기선언 최종 클래스 Closure는 PHP 5.3.0에서 나왔습니다. 익명 함수의 내부 구현에 사용됩니다.

자세한 정보는, 해당 클래스 페이지를 참고하십시오.

특수 클래스

다음 식별자는 특별한 목적이 있기 때문에 클래스 이름으로 사용할 수 없습니다.

self
현재 클래스.
static
실행 시점에서 현재 클래스.
parent
부모 클래스.

User Contributed Notes

Ashley Dambra 20-Feb-2014 03:43
Here a simple class that allow to set anonymous function. It's an optimised class of stdClass.

<?php
class stdObject {
    public function __construct(array $arguments = array()) {
        if (!empty($arguments)) {
            foreach ($arguments as $property => $argument) {
                if ($argument instanceOf Closure) {
                    $this->{$property} = $argument;
                } else {
                    $this->{$property} = $argument;
                }
            }
        }
    }

    public function __call($method, $arguments) {
        if (isset($this->{$method}) && is_callable($this->{$method})) {
            return call_user_func_array($this->{$method}, $arguments);
        } else {
            throw new Exception("Fatal error: Call to undefined method stdObject::{$method}()");
        }
    }
}

$person = new stdObject(array(
    "name" => "nick",
    "age" => 23,
    "friends" => array("frank", "sally", "aaron"),
    "sayHi" => function() {
        return "Hello there";
    }
));

$person->sayHi2 = function() {
    return "Hello there 2";
};

$person->test = function() {
    return "test";
};

var_dump($person->name, $person->test(), $person->sayHi2());
?>
wyattstorch42 at outlook dot com 24-Dec-2013 10:15
If you call var_export() on an instance of stdClass, it attempts to export it using ::__set_state(), which, for some reason, is not implemented in stdClass.

However, casting an associative array to an object usually produces the same effect (at least, it does in my case). So I wrote an improved_var_export() function to convert instances of stdClass to (object) array () calls. If you choose to export objects of any other class, I'd advise you to implement ::__set_state().

<?php
/**
 * An implementation of var_export() that is compatible with instances
 * of stdClass.
 * @param mixed $variable The variable you want to export
 * @param bool $return If used and set to true, improved_var_export()
 *     will return the variable representation instead of outputting it.
 * @return mixed|null Returns the variable representation when the
 *     return parameter is used and evaluates to TRUE. Otherwise, this
 *     function will return NULL.
 */
function improved_var_export ($variable, $return = false) {
    if ($variable instanceof stdClass) {
        $result = '(object) '.improved_var_export(get_object_vars($variable), true);
    } else if (is_array($variable)) {
        $array = array ();
        foreach ($variable as $key => $value) {
            $array[] = var_export($key, true).' => '.improved_var_export($value, true);
        }
        $result = 'array ('.implode(', ', $array).')';
    } else {
        $result = var_export($variable, true);
    }

    if (!$return) {
        print $result;
        return null;
    } else {
        return $result;
    }
}

// Example usage:
$obj = new stdClass;
$obj->test = 'abc';
$obj->other = 6.2;
$obj->arr = array (1, 2, 3);

improved_var_export((object) array (
    'prop1' => true,
    'prop2' => $obj,
    'assocArray' => array (
        'apple' => 'good',
        'orange' => 'great'
    )
));

/* Output:
(object) array ('prop1' => true, 'prop2' => (object) array ('test' => 'abc', 'other' => 6.2, 'arr' => array (0 => 1, 1 => 2, 2 => 3)), 'assocArray' => array ('apple' => 'good', 'orange' => 'great'))
*/
?>

Note: This function spits out a single line of code, which is useful to save in a cache file to include/eval. It isn't formatted for readability. If you want to print a readable version for debugging purposes, then I would suggest print_r() or var_dump().
spark at limao dot com dot br 15-Aug-2011 11:21
if you want a Dynamic class you can extend from, add atributes AND methods on the fly you can use this:
<?php
    class Dynamic extends stdClass{
        public function __call($key,$params){
            if(!isset($this->{$key})) throw new Exception("Call to undefined method ".get_class($this)."::".$key."()");
            $subject = $this->{$key};
            call_user_func_array($subject,$params);
        }
    }
?>

this will accept both arrays, strings and Closures:
<?php
    $dynamic->myMethod = "thatFunction";
    $dynamic->hisMethod = array($instance,"aMethod");
    $dynamic->newMethod = array(SomeClass,"staticMethod");
    $dynamic->anotherMethod = function(){
        echo "Hey there";
    };
?>

then call them away =D
Typer85 at gmail dot com 17-Feb-2007 05:58
In response to the note below mine,

This list is not incomplete. Read the Manual folks, its listed there plain and simple:

"This section lists standard predefined classes. Miscellaneous extensions define other classes which are described in their reference."

Depending on what extensions you have, they might define their own classes. This list is just the standard classes as defined by PHP with no regards to extensions.
Simon Kissane 14-Sep-2004 09:54
The above list is incomplete. For example, on my PHP 5.0 configuration (yours may differ depending on the extensions you choose to install), the following classes are predefined:

== "Core" ==
stdClass
Exception
__PHP_Incomplete_Class
php_user_filter
Directory

== Reflection API ==
ReflectionException
Reflection
ReflectionFunction
ReflectionParameter
ReflectionMethod
ReflectionClass
ReflectionObject
ReflectionProperty
ReflectionExtension

== SQLLite ==
SQLiteDatabase
SQLiteResult
SQLiteUnbuffered
SQLiteException

== Standard PHP Lib. ==
RecursiveIteratorIterator
FilterIterator
ParentIterator
LimitIterator
CachingIterator
CachingRecursiveIterator
ArrayObject
ArrayIterator
DirectoryIterator
RecursiveDirectoryIterator

== SimpleXML ==
SimpleXMLElement
SimpleXMLIterator

== DOM/XSL/XPath extensions ==
DOMException
DOMStringList
DOMNameList
DOMImplementationList
DOMImplementationSource
DOMImplementation
DOMNode
DOMNameSpaceNode
DOMDocumentFragment
DOMDocument
DOMNodeList
DOMNamedNodeMap
DOMCharacterData
DOMAttr
DOMElement
DOMText
DOMComment
DOMTypeinfo
DOMUserDataHandler
DOMDomError
DOMErrorHandler
DOMLocator
DOMConfiguration
DOMCdataSection
DOMDocumentType
DOMNotation
DOMEntity
DOMEntityReference
DOMProcessingInstruction
DOMStringExtend
DOMXPath
XSLTProcessor
unknown 23-Jul-2002 07:29
It's handy to have a array of the reserved classes.....
var_dump (get_declared_classes ());


예약 상수

코어 예약 상수

이 상수들은 PHP 코어에 의해 선언된다. 이 상수들은 PHP, 젠드 엔진, SAPI 모듈에도 포함된다.

PHP_VERSION (string)
PHP_MAJOR_VERSION (integer)
PHP 5.2.7부터 사용할 수 있습니다.
PHP_MINOR_VERSION (integer)
PHP 5.2.7부터 사용할 수 있습니다.
PHP_RELEASE_VERSION (integer)
PHP 5.2.7부터 사용할 수 있습니다.
PHP_VERSION_ID (integer)
PHP 5.2.7부터 사용할 수 있습니다.
PHP_EXTRA_VERSION (string)
PHP 5.2.7부터 사용할 수 있습니다.
PHP_ZTS (integer)
PHP 5.2.7부터 사용할 수 있습니다.
PHP_DEBUG (integer)
PHP 5.2.7부터 사용할 수 있습니다.
PHP_MAXPATHLEN (integer)
PHP 5.3.0부터 사용할 수 있습니다.
PHP_OS (string)
PHP_SAPI (string)
PHP 4.2.0부터 사용할 수 있습니다. 참고: php_sapi_name()
PHP_EOL (string)
PHP 4.3.10과 PHP 5.0.2부터 사용할 수 있습니다.
PHP_INT_MAX (integer)
PHP 4.4.0과 PHP 5.0.5부터 사용할 수 있습니다.
PHP_INT_SIZE (integer)
PHP 4.4.0과 PHP 5.0.5부터 사용할 수 있습니다.
DEFAULT_INCLUDE_PATH (string)
PEAR_INSTALL_DIR (string)
PEAR_EXTENSION_DIR (string)
PHP_EXTENSION_DIR (string)
PHP_PREFIX (string)
PHP 4.3.0부터 사용할 수 있습니다.
PHP_BINDIR (string)
PHP_LIBDIR (string)
PHP_DATADIR (string)
PHP_SYSCONFDIR (string)
PHP_LOCALSTATEDIR (string)
PHP_CONFIG_FILE_PATH (string)
PHP_CONFIG_FILE_SCAN_DIR (string)
PHP_SHLIB_SUFFIX (string)
PHP 4.3.0부터 사용할 수 있습니다.
PHP_OUTPUT_HANDLER_START (integer)
PHP_OUTPUT_HANDLER_CONT (integer)
PHP_OUTPUT_HANDLER_END (integer)
E_ERROR (integer)
E_WARNING (integer)
E_PARSE (integer)
E_NOTICE (integer)
E_CORE_ERROR (integer)
E_CORE_WARNING (integer)
E_COMPILE_ERROR (integer)
E_COMPILE_WARNING (integer)
E_USER_ERROR (integer)
E_USER_WARNING (integer)
E_USER_NOTICE (integer)
E_DEPRECATED (integer)
PHP 5.3.0부터 존재
E_USER_DEPRECATED (integer)
PHP 5.3.0부터 존재
E_ALL (integer)
E_STRICT (integer)
PHP 5.0.0부터 사용할 수 있습니다.
__COMPILER_HALT_OFFSET__ (integer)
PHP 5.1.0부터 사용할 수 있습니다.

참고: 마법 상수.

표준 예약 상수

이 상수는 기본적으로 PHP에서 선언된다.

EXTR_OVERWRITE (integer)
EXTR_SKIP (integer)
EXTR_PREFIX_SAME (integer)
EXTR_PREFIX_ALL (integer)
EXTR_PREFIX_INVALID (integer)
EXTR_PREFIX_IF_EXISTS (integer)
EXTR_IF_EXISTS (integer)
SORT_ASC (integer)
SORT_DESC (integer)
SORT_REGULAR (integer)
SORT_NUMERIC (integer)
SORT_STRING (integer)
CASE_LOWER (integer)
CASE_UPPER (integer)
COUNT_NORMAL (integer)
COUNT_RECURSIVE (integer)
ASSERT_ACTIVE (integer)
ASSERT_CALLBACK (integer)
ASSERT_BAIL (integer)
ASSERT_WARNING (integer)
ASSERT_QUIET_EVAL (integer)
CONNECTION_ABORTED (integer)
CONNECTION_NORMAL (integer)
CONNECTION_TIMEOUT (integer)
INI_USER (integer)
INI_PERDIR (integer)
INI_SYSTEM (integer)
INI_ALL (integer)
M_E (float)
M_LOG2E (float)
M_LOG10E (float)
M_LN2 (float)
M_LN10 (float)
M_PI (float)
M_PI_2 (float)
M_PI_4 (float)
M_1_PI (float)
M_2_PI (float)
M_2_SQRTPI (float)
M_SQRT2 (float)
M_SQRT1_2 (float)
CRYPT_SALT_LENGTH (integer)
CRYPT_STD_DES (integer)
CRYPT_EXT_DES (integer)
CRYPT_MD5 (integer)
CRYPT_BLOWFISH (integer)
DIRECTORY_SEPARATOR (string)
SEEK_SET (integer)
SEEK_CUR (integer)
SEEK_END (integer)
LOCK_SH (integer)
LOCK_EX (integer)
LOCK_UN (integer)
LOCK_NB (integer)
HTML_SPECIALCHARS (integer)
HTML_ENTITIES (integer)
ENT_COMPAT (integer)
ENT_QUOTES (integer)
ENT_NOQUOTES (integer)
INFO_GENERAL (integer)
INFO_CREDITS (integer)
INFO_CONFIGURATION (integer)
INFO_MODULES (integer)
INFO_ENVIRONMENT (integer)
INFO_VARIABLES (integer)
INFO_LICENSE (integer)
INFO_ALL (integer)
CREDITS_GROUP (integer)
CREDITS_GENERAL (integer)
CREDITS_SAPI (integer)
CREDITS_MODULES (integer)
CREDITS_DOCS (integer)
CREDITS_FULLPAGE (integer)
CREDITS_QA (integer)
CREDITS_ALL (integer)
STR_PAD_LEFT (integer)
STR_PAD_RIGHT (integer)
STR_PAD_BOTH (integer)
PATHINFO_DIRNAME (integer)
PATHINFO_BASENAME (integer)
PATHINFO_EXTENSION (integer)
PATH_SEPARATOR (string)
CHAR_MAX (integer)
LC_CTYPE (integer)
LC_NUMERIC (integer)
LC_TIME (integer)
LC_COLLATE (integer)
LC_MONETARY (integer)
LC_ALL (integer)
LC_MESSAGES (integer)
ABDAY_1 (integer)
ABDAY_2 (integer)
ABDAY_3 (integer)
ABDAY_4 (integer)
ABDAY_5 (integer)
ABDAY_6 (integer)
ABDAY_7 (integer)
DAY_1 (integer)
DAY_2 (integer)
DAY_3 (integer)
DAY_4 (integer)
DAY_5 (integer)
DAY_6 (integer)
DAY_7 (integer)
ABMON_1 (integer)
ABMON_2 (integer)
ABMON_3 (integer)
ABMON_4 (integer)
ABMON_5 (integer)
ABMON_6 (integer)
ABMON_7 (integer)
ABMON_8 (integer)
ABMON_9 (integer)
ABMON_10 (integer)
ABMON_11 (integer)
ABMON_12 (integer)
MON_1 (integer)
MON_2 (integer)
MON_3 (integer)
MON_4 (integer)
MON_5 (integer)
MON_6 (integer)
MON_7 (integer)
MON_8 (integer)
MON_9 (integer)
MON_10 (integer)
MON_11 (integer)
MON_12 (integer)
AM_STR (integer)
PM_STR (integer)
D_T_FMT (integer)
D_FMT (integer)
T_FMT (integer)
T_FMT_AMPM (integer)
ERA (integer)
ERA_YEAR (integer)
ERA_D_T_FMT (integer)
ERA_D_FMT (integer)
ERA_T_FMT (integer)
ALT_DIGITS (integer)
INT_CURR_SYMBOL (integer)
CURRENCY_SYMBOL (integer)
CRNCYSTR (integer)
MON_DECIMAL_POINT (integer)
MON_THOUSANDS_SEP (integer)
MON_GROUPING (integer)
POSITIVE_SIGN (integer)
NEGATIVE_SIGN (integer)
INT_FRAC_DIGITS (integer)
FRAC_DIGITS (integer)
P_CS_PRECEDES (integer)
P_SEP_BY_SPACE (integer)
N_CS_PRECEDES (integer)
N_SEP_BY_SPACE (integer)
P_SIGN_POSN (integer)
N_SIGN_POSN (integer)
DECIMAL_POINT (integer)
RADIXCHAR (integer)
THOUSANDS_SEP (integer)
THOUSEP (integer)
GROUPING (integer)
YESEXPR (integer)
NOEXPR (integer)
YESSTR (integer)
NOSTR (integer)
CODESET (integer)
LOG_EMERG (integer)
LOG_ALERT (integer)
LOG_CRIT (integer)
LOG_ERR (integer)
LOG_WARNING (integer)
LOG_NOTICE (integer)
LOG_INFO (integer)
LOG_DEBUG (integer)
LOG_KERN (integer)
LOG_USER (integer)
LOG_MAIL (integer)
LOG_DAEMON (integer)
LOG_AUTH (integer)
LOG_SYSLOG (integer)
LOG_LPR (integer)
LOG_NEWS (integer)
LOG_UUCP (integer)
LOG_CRON (integer)
LOG_AUTHPRIV (integer)
LOG_LOCAL0 (integer)
LOG_LOCAL1 (integer)
LOG_LOCAL2 (integer)
LOG_LOCAL3 (integer)
LOG_LOCAL4 (integer)
LOG_LOCAL5 (integer)
LOG_LOCAL6 (integer)
LOG_LOCAL7 (integer)
LOG_PID (integer)
LOG_CONS (integer)
LOG_ODELAY (integer)
LOG_NDELAY (integer)
LOG_NOWAIT (integer)
LOG_PERROR (integer)

User Contributed Notes

Anonymous 28-Jan-2015 12:25
PHP_EOL can be used like that:

<?php

$data = 'some data'.PHP_EOL;
$fp = fopen('somefile', 'a');
fwrite($fp, $data);

?>

Which is the same of \r\n or \n depending on the OS.
You can put those lines in a while for example, and create a log file.
rdcapasso 09-Sep-2013 06:16
Volker's getOS() function needs to have the order of cases changed in the switch statement since "darwin" contains "win", which means that both "windows" and "darwin" will return self::OS_WIN. I've moved the 'dar' case above the 'win' case:

<?php
class System {

    const OS_UNKNOWN = 1;
    const OS_WIN = 2;
    const OS_LINUX = 3;
    const OS_OSX = 4;

    /**
     * @return int
     */
    static public function getOS() {
        switch (true) {
            case stristr(PHP_OS, 'DAR'): return self::OS_OSX;
            case stristr(PHP_OS, 'WIN'): return self::OS_WIN;
            case stristr(PHP_OS, 'LINUX'): return self::OS_LINUX;
            default : return self::OS_UNKNOWN;
        }
    }

}
?>


리소스형 목록

다음은 PHP 리소스를 생성, 사용, 파괴하는 함수 목록이다. 함수 is_resource()는 특정 변수가 리소스인지 확인하기 위해 사용할수 있고 get_resource_type()은 변수의 리소스형을 반환합니다.

리소스형
리소스형 이름 | 생성 함수 | 사용 함수 | 파괴 함수 | 정의
aspell | aspell_new() | aspell_check(), aspell_check_raw(), aspell_suggest() | None | Aspell 사전
bzip2 | bzopen() | bzerrno(), bzerror(), bzerrstr(), bzflush(), bzread(), bzwrite() | bzclose() | Bzip2 파일
COM | com_load() | com_invoke(), com_propget(), com_get(), com_propput(), com_set(), com_propput() | None | COM 객체 참조
VARIANT
cpdf | cpdf_open() | cpdf_page_init(), cpdf_finalize_page(), cpdf_finalize(), cpdf_output_buffer(), cpdf_save_to_file(), cpdf_set_current_page(), cpdf_begin_text(), cpdf_end_text(), cpdf_show(), cpdf_show_xy(), cpdf_text(), cpdf_set_font(), cpdf_set_leading(), cpdf_set_text_rendering(), cpdf_set_horiz_scaling(), cpdf_set_text_rise(), cpdf_set_text_matrix(), cpdf_set_text_pos(), cpdf_set_text_pos(), cpdf_set_word_spacing(), cpdf_continue_text(), cpdf_stringwidth(), cpdf_save(), cpdf_translate(), cpdf_restore(), cpdf_scale(), cpdf_rotate(), cpdf_setflat(), cpdf_setlinejoin(), cpdf_setlinecap(), cpdf_setmiterlimit(), cpdf_setlinewidth(), cpdf_setdash(), cpdf_moveto(), cpdf_rmoveto(), cpdf_curveto(), cpdf_lineto(), cpdf_rlineto(), cpdf_circle(), cpdf_arc(), cpdf_rect(), cpdf_closepath(), cpdf_stroke(), cpdf_closepath_fill_stroke(), cpdf_fill_stroke(), cpdf_clip(), cpdf_fill(), cpdf_setgray_fill(), cpdf_setgray_stroke(), cpdf_setgray(), cpdf_setrgbcolor_fill(), cpdf_setrgbcolor_stroke(), cpdf_setrgbcolor(), cpdf_add_outline(), cpdf_set_page_animation(), cpdf_import_jpeg(), cpdf_place_inline_image(), cpdf_add_annotation() | cpdf_close() | CPDF lib에 의한 PDF 문서
cpdf outline
curl | curl_copy_handle(), curl_init() | curl_copy_handle(), curl_errno(), curl_error(), curl_exec(), curl_getinfo(), curl_setopt() | curl_close() | Curl 세션
dbm | dbmopen() | dbmexists(), dbmfetch(), dbminsert(), dbmreplace(), dbmdelete(), dbmfirstkey(), dbmnextkey() | dbmclose() | DBM 데이터베이스 연결
dba | dba_open() | dba_delete(), dba_exists(), dba_fetch(), dba_firstkey(), dba_insert(), dba_nextkey(), dba_optimize(), dba_replace(), dba_sync() | dba_close() | DBA 데이터베이스 연결
dba persistent | dba_popen() | dba_delete(), dba_exists(), dba_fetch(), dba_firstkey(), dba_insert(), dba_nextkey(), dba_optimize(), dba_replace(), dba_sync() | None | DBA 데이터베이스 지속 연결
dbase | dbase_open() | dbase_pack(), dbase_add_record(), dbase_replace_record(), dbase_delete_record(), dbase_get_record(), dbase_get_record_with_names(), dbase_numfields(), dbase_numrecords() | dbase_close() | Dbase 데이터베이스 연결
dbx_link_object | dbx_connect() | dbx_query() | dbx_close() | dbx 접속
dbx_result_object | dbx_query() | None | dbx 결과
domxml attribute
domxml document
domxml node
xpath context
xpath object
fbsql link | fbsql_change_user(), fbsql_connect() | fbsql_autocommit(), fbsql_blob_size(), fbsql_clob_size(), fbsql_commit(), fbsql_change_user(), fbsql_create_blob(), fbsql_create_db(), fbsql_create_clob(), fbsql_data_seek(), fbsql_database_password(), fbsql_database(), fbsql_db_query(), fbsql_db_status(), fbsql_drop_db(), fbsql_errno(), fbsql_error(), fbsql_get_autostart_info(), fbsql_hostname(), fbsql_insert_id(), fbsql_list_dbs(), fbsql_password(), fbsql_read_blob(), fbsql_read_clob(), fbsql_rollback(), fbsql_select_db(), fbsql_set_password(), fbsql_set_transaction(), fbsql_start_db(), fbsql_stop_db(), fbsql_username() | fbsql_close() | fbsql 데이터베이스 연결
fbsql plink | fbsql_change_user(), fbsql_pconnect() | fbsql_autocommit(), fbsql_change_user(), fbsql_create_db(), fbsql_data_seek(), fbsql_db_query(), fbsql_drop_db(), fbsql_select_db(), fbsql_errno(), fbsql_error(), fbsql_insert_id(), fbsql_list_dbs() | None | fbsql 데이터베이스 지속 연결
fbsql result | fbsql_db_query(), fbsql_list_dbs(), fbsql_query(), fbsql_list_fields(), fbsql_list_tables(), fbsql_tablename() | fbsql_affected_rows(), fbsql_fetch_array(), fbsql_fetch_assoc(), fbsql_fetch_field(), fbsql_fetch_lengths(), fbsql_fetch_object(), fbsql_fetch_row(), fbsql_field_flags(), fbsql_field_name(), fbsql_field_len(), fbsql_field_seek(), fbsql_field_table(), fbsql_field_type(), fbsql_next_result(), fbsql_num_fields(), fbsql_num_rows(), fbsql_result(), fbsql_num_rows() | fbsql_free_result() | fbsql 결과
fdf | fdf_open() | fdf_create(), fdf_save(), fdf_get_value(), fdf_set_value(), fdf_next_field_name(), fdf_set_ap(), fdf_set_status(), fdf_get_status(), fdf_set_file(), fdf_get_file(), fdf_set_flags(), fdf_set_opt(), fdf_set_submit_form_action(), fdf_set_javascript_action() | fdf_close() | FDF 파일
ftp | ftp_connect(), ftp_ssl_connect() | ftp_login(), ftp_pwd(), ftp_cdup(), ftp_chdir(), ftp_mkdir(), ftp_rmdir(), ftp_nlist(), ftp_rawlist(), ftp_systype(), ftp_pasv(), ftp_get(), ftp_fget(), ftp_put(), ftp_fput(), ftp_size(), ftp_mdtm(), ftp_rename(), ftp_delete(), ftp_site(), ftp_alloc(), ftp_chmod(), ftp_exec(), ftp_get_option(), ftp_nb_continue(), ftp_nb_fget(), ftp_nb_fput(), ftp_nb_get(), ftp_nb_put(), ftp_raw(), ftp_set_option() | ftp_close() | FTP 스트림
gd | imagecreate(), imagecreatefromgd(), imagecreatefromgd2(), imagecreatefromgd2part(), imagecreatefromgif(), imagecreatefromjpeg(), imagecreatefrompng(), imagecreatefromwbmp(), imagecreatefromstring(), imagecreatefromxbm(), imagecreatefromxpm(), imagecreatefromtruecolor(), imagerotate() | imagearc(), imagechar(), imagecharup(), imagecolorallocate(), imagecolorat(), imagecolorclosest(), imagecolorexact(), imagecolorresolve(), imagegammacorrect(), imagegammacorrect(), imagecolorset(), imagecolorsforindex(), imagecolorstotal(), imagecolortransparent(), imagecopy(), imagecopyresized(), imagedashedline(), imagefill(), imagefilledpolygon(), imagefilledrectangle(), imagefilltoborder(), imagegif(), imagepng(), imagejpeg(), imagewbmp(), imageinterlace(), imageline(), imagepolygon(), imagepstext(), imagerectangle(), imagerotate(), imagesetpixel(), imagestring(), imagestringup(), imagesx(), imagesy(), imagettftext(), imagefilledarc(), imageellipse(), imagefilledellipse(), imagecolorclosestalpha(), imagecolorexactalpha(), imagecolorresolvealpha(), imagecopymerge(), imagecopymergegray(), imagecopyresampled(), imagetruecolortopalette(), imagesetbrush(), imagesettile(), imagesetthickness(), image2wbmp(), imagealphablending(), imageantialias(), imagecolorallocatealpha(), imagecolorclosesthwb(), imagecolordeallocate(), imagecolormatch(), imagefilter(), imagefttext(), imagegd(), imagegd2(), imageistruecolor(), imagelayereffect(), imagepalettecopy(), imagesavealpha(), imagesetstyle(), imagexbm() | imagedestroy() | GD 이미지
gd font | imageloadfont() | imagechar(), imagecharup(), imagefontheight() | None | GD 폰트
gd PS encoding
gd PS font | imagepsloadfont() | imagepstext(), imagepsslantfont(), imagepsextendfont(), imagepsencodefont(), imagepsbbox() | imagepsfreefont() | GD PS 폰트
GMP integer | gmp_init() | gmp_intval(), gmp_strval(), gmp_add(), gmp_sub(), gmp_mul(), gmp_div_q(), gmp_div_r(), gmp_div_qr(), gmp_div(), gmp_mod(), gmp_divexact(), gmp_cmp(), gmp_neg(), gmp_abs(), gmp_sign(), gmp_fact(), gmp_sqrt(), gmp_sqrtrm(), gmp_perfect_square(), gmp_pow(), gmp_powm(), gmp_prob_prime(), gmp_gcd(), gmp_gcdext(), gmp_invert(), gmp_legendre(), gmp_jacobi(), gmp_random(), gmp_and(), gmp_or(), gmp_xor(), gmp_setbit(), gmp_clrbit(), gmp_scan0(), gmp_scan1(), gmp_popcount(), gmp_hamdist() | None | GMP 수
hyperwave document | hw_cp(), hw_docbyanchor(), hw_getremote(), hw_getremotechildren() | hw_children(), hw_childrenobj(), hw_getparents(), hw_getparentsobj(), hw_getchildcoll(), hw_getchildcollobj(), hw_getremote(), hw_getsrcbydestobj(), hw_getandlock(), hw_gettext(), hw_getobjectbyquerycoll(), hw_getobjectbyquerycollobj(), hw_getchilddoccoll(), hw_getchilddoccollobj(), hw_getanchors(), hw_getanchorsobj(), hw_inscoll(), hw_pipedocument(), hw_unlock() | hw_deleteobject() | Hyperwave 객체
hyperwave link | hw_connect() | hw_children(), hw_childrenobj(), hw_cp(), hw_deleteobject(), hw_docbyanchor(), hw_docbyanchorobj(), hw_errormsg(), hw_edittext(), hw_error(), hw_getparents(), hw_getparentsobj(), hw_getchildcoll(), hw_getchildcollobj(), hw_getremote(), hw_getremotechildren(), hw_getsrcbydestobj(), hw_getobject(), hw_getandlock(), hw_gettext(), hw_getobjectbyquery(), hw_getobjectbyqueryobj(), hw_getobjectbyquerycoll(), hw_getobjectbyquerycollobj(), hw_getchilddoccoll(), hw_getchilddoccollobj(), hw_getanchors(), hw_getanchorsobj(), hw_mv(), hw_incollections(), hw_info(), hw_inscoll(), hw_insdoc(), hw_insertdocument(), hw_insertobject(), hw_mapid(), hw_modifyobject(), hw_pipedocument(), hw_unlock(), hw_who(), hw_getusername() | hw_close(), hw_free_document() | Hyperwave 서버 연결
hyperwave link persistent | hw_pconnect() | hw_children(), hw_childrenobj(), hw_cp(), hw_deleteobject(), hw_docbyanchor(), hw_docbyanchorobj(), hw_errormsg(), hw_edittext(), hw_error(), hw_getparents(), hw_getparentsobj(), hw_getchildcoll(), hw_getchildcollobj(), hw_getremote(), hw_getremotechildren(), hw_getsrcbydestobj(), hw_getobject(), hw_getandlock(), hw_gettext(), hw_getobjectbyquery(), hw_getobjectbyqueryobj(), hw_getobjectbyquerycoll(), hw_getobjectbyquerycollobj(), hw_getchilddoccoll(), hw_getchilddoccollobj(), hw_getanchors(), hw_getanchorsobj(), hw_mv(), hw_incollections(), hw_info(), hw_inscoll(), hw_insdoc(), hw_insertdocument(), hw_insertobject(), hw_mapid(), hw_modifyobject(), hw_pipedocument(), hw_unlock(), hw_who(), hw_getusername() | None | Hyperwave 서버 지속 연결
icap | icap_open() | icap_fetch_event(), icap_list_events(), icap_store_event(), icap_snooze(), icap_list_alarms(), icap_delete_event() | icap_close() | icap 서버 연결
imap | imap_open() | imap_append(), imap_body(), imap_check(), imap_createmailbox(), imap_delete(), imap_deletemailbox(), imap_expunge(), imap_fetchbody(), imap_fetchstructure(), imap_headerinfo(), imap_header(), imap_headers(), imap_listmailbox(), imap_getmailboxes(), imap_get_quota(), imap_status(), imap_listsubscribed(), imap_set_quota(), imap_set_quota(), imap_getsubscribed(), imap_mail_copy(), imap_mail_move(), imap_num_msg(), imap_num_recent(), imap_ping(), imap_renamemailbox(), imap_reopen(), imap_subscribe(), imap_undelete(), imap_unsubscribe(), imap_scanmailbox(), imap_mailboxmsginfo(), imap_fetchheader(), imap_uid(), imap_msgno(), imap_search(), imap_fetch_overview() | imap_close() | IMAP, POP3 서버 연결
imap chain persistent
imap persistent
ingres | ingres_connect() | ingres_query(), ingres_num_rows(), ingres_num_fields(), ingres_field_name(), ingres_field_type(), ingres_field_nullable(), ingres_field_length(), ingres_field_precision(), ingres_field_scale(), ingres_fetch_array(), ingres_fetch_row(), ingres_fetch_object(), ingres_rollback(), ingres_commit(), ingres_autocommit() | ingres_close() | ingresII base 연결
ingres persistent | ingres_pconnect() | ingres_query(), ingres_num_rows(), ingres_num_fields(), ingres_field_name(), ingres_field_type(), ingres_field_nullable(), ingres_field_length(), ingres_field_precision(), ingres_field_scale(), ingres_fetch_array(), ingres_fetch_row(), ingres_fetch_object(), ingres_rollback(), ingres_commit(), ingres_autocommit() | None | ingresII base 지속 연결
interbase blob
interbase link | ibase_connect() | ibase_query(), ibase_prepare(), ibase_trans() | ibase_close() | Interbase 데이터베이스 연결
interbase link persistent | ibase_pconnect() | ibase_query(), ibase_prepare(), ibase_trans() | None | Interbase 데이터베이스 지속 연결
interbase query | ibase_prepare() | ibase_execute() | ibase_free_query() | Interbase 질의
interbase result | ibase_query() | ibase_fetch_row(), ibase_fetch_object(), ibase_field_info(), ibase_num_fields() | ibase_free_result() | Interbase 결과
interbase transaction | ibase_trans() | ibase_commit() | ibase_rollback() | Interbase 트랜잭션
java
ldap link | ldap_connect(), ldap_search() | ldap_count_entries(), ldap_first_attribute(), ldap_first_entry(), ldap_get_attributes(), ldap_get_dn(), ldap_get_entries(), ldap_get_values(), ldap_get_values_len(), ldap_next_attribute(), ldap_next_entry() | ldap_close() | ldap 접속
ldap result | ldap_read() | ldap_add(), ldap_compare(), ldap_bind(), ldap_count_entries(), ldap_delete(), ldap_errno(), ldap_error(), ldap_first_attribute(), ldap_first_entry(), ldap_get_attributes(), ldap_get_dn(), ldap_get_entries(), ldap_get_values(), ldap_get_values_len(), ldap_get_option(), ldap_list(), ldap_modify(), ldap_mod_add(), ldap_mod_replace(), ldap_next_attribute(), ldap_next_entry(), ldap_mod_del(), ldap_set_option(), ldap_unbind() | ldap_free_result() | ldap 검색 결과
ldap result entry
mcal | mcal_open(), mcal_popen() | mcal_create_calendar(), mcal_rename_calendar(), mcal_rename_calendar(), mcal_delete_calendar(), mcal_fetch_event(), mcal_list_events(), mcal_append_event(), mcal_store_event(), mcal_delete_event(), mcal_list_alarms(), mcal_event_init(), mcal_event_set_category(), mcal_event_set_title(), mcal_event_set_description(), mcal_event_set_start(), mcal_event_set_end(), mcal_event_set_alarm(), mcal_event_set_class(), mcal_next_recurrence(), mcal_event_set_recur_none(), mcal_event_set_recur_daily(), mcal_event_set_recur_weekly(), mcal_event_set_recur_monthly_mday(), mcal_event_set_recur_monthly_wday(), mcal_event_set_recur_yearly(), mcal_fetch_current_stream_event(), mcal_event_add_attribute(), mcal_expunge() | mcal_close() | calendar 서버 연결
SWFAction
SWFBitmap
SWFButton
SWFDisplayItem
SWFFill
SWFFont
SWFGradient
SWFMorph
SWFMovie
SWFShape
SWFSprite
SWFText
SWFTextField
mnogosearch agent
mnogosearch result
msql link | msql_connect() | msql(), msql_create_db(), msql_createdb(), msql_drop_db(), msql_drop_db(), msql_select_db(), msql_select_db() | msql_close() | mSQL 데이터베이스 연결
msql link persistent | msql_pconnect() | msql(), msql_create_db(), msql_createdb(), msql_drop_db(), msql_drop_db(), msql_select_db(), msql_select_db() | None | mSQL 지속 연결
msql query | msql_db_query(), msql_list_dbs(), msql_list_fields(), msql_list_tables(), msql_query() | msql(), msql_affected_rows(), msql_data_seek(), msql_dbname(), msql_fetch_array(), msql_fetch_field(), msql_fetch_object(), msql_fetch_row(), msql_field_seek(), msql_field_table(), msql_field_flags(), msql_field_len(), msql_field_name(), msql_field_type(), msql_num_fields(), msql_num_rows(), msql_numfields(), msql_numrows(), msql_result() | msql_free_result(), msql_free_result() | mSQL 결과
mssql link | mssql_connect() | mssql_query(), mssql_select_db() | mssql_close() | 마이크로소프트 SQL 서버 데이터베이스 연결
mssql link persistent | mssql_pconnect() | mssql_query(), mssql_select_db() | None | 마이크로소프트 SQL 서버 지속 연결
mssql result | mssql_query() | mssql_data_seek(), mssql_fetch_array(), mssql_fetch_field(), mssql_fetch_object(), mssql_fetch_row(), mssql_field_length(), mssql_field_name(), mssql_field_seek(), mssql_field_type(), mssql_num_fields(), mssql_num_rows(), mssql_result() | mssql_free_result() | 마이크로소프트 SQL 서버 결과
mysql link | mysql_connect() | mysql_affected_rows(), mysql_change_user(), mysql_create_db(), mysql_data_seek(), mysql_db_name(), mysql_db_query(), mysql_drop_db(), mysql_errno(), mysql_error(), mysql_insert_id(), mysql_list_dbs(), mysql_list_fields(), mysql_list_tables(), mysql_query(), mysql_result(), mysql_select_db(), mysql_tablename(), mysql_get_host_info(), mysql_get_proto_info(), mysql_get_server_info() | mysql_close() | MySQL 데이터베이스 연결
mysql link persistent | mysql_pconnect() | mysql_affected_rows(), mysql_change_user(), mysql_create_db(), mysql_data_seek(), mysql_db_name(), mysql_db_query(), mysql_drop_db(), mysql_errno(), mysql_error(), mysql_insert_id(), mysql_list_dbs(), mysql_list_fields(), mysql_list_tables(), mysql_query(), mysql_result(), mysql_select_db(), mysql_tablename(), mysql_get_host_info(), mysql_get_proto_info(), mysql_get_server_info() | None | MySQL 데이터베이스 지속 연결
mysql result | mysql_db_query(), mysql_list_dbs(), mysql_list_fields(), mysql_list_processes(), mysql_list_tables(), mysql_query(), mysql_unbuffered_query() | mysql_data_seek(), mysql_db_name(), mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_field(), mysql_fetch_lengths(), mysql_fetch_object(), mysql_fetch_row(), mysql_fetch_row(), mysql_field_flags(), mysql_field_name(), mysql_field_len(), mysql_field_seek(), mysql_field_table(), mysql_field_type(), mysql_num_fields(), mysql_num_rows(), mysql_result(), mysql_tablename() | mysql_free_result() | MySQL 결과
oci8 collection
oci8 connection | ocilogon(), ociplogon(), ocinlogon() | ocicommit(), ociserverversion(), ocinewcursor(), ociparse(), ocierror() | ocilogoff() | 오라클 데이터베이스 연결
oci8 descriptor
oci8 server
oci8 session
oci8 statement | ocinewdescriptor() | ocirollback(), ocinewdescriptor(), ocirowcount(), ocidefinebyname(), ocibindbyname(), ociexecute(), ocinumcols(), ociresult(), ocifetch(), ocifetchinto(), ocifetchstatement(), ocicolumnisnull(), ocicolumnname(), ocicolumnsize(), ocicolumntype(), ocistatementtype(), ocierror() | ocifreestatement() | 오라클 커서
odbc link | odbc_connect() | odbc_autocommit(), odbc_commit(), odbc_error(), odbc_errormsg(), odbc_exec(), odbc_tables(), odbc_tableprivileges(), odbc_do(), odbc_prepare(), odbc_columns(), odbc_columnprivileges(), odbc_procedurecolumns(), odbc_specialcolumns(), odbc_rollback(), odbc_setoption(), odbc_gettypeinfo(), odbc_primarykeys(), odbc_foreignkeys(), odbc_procedures(), odbc_statistics() | odbc_close() | ODBC 데이터베이스 연결
odbc link persistent | odbc_pconnect() | odbc_autocommit(), odbc_commit(), odbc_error(), odbc_errormsg(), odbc_exec(), odbc_tables(), odbc_tableprivileges(), odbc_do(), odbc_prepare(), odbc_columns(), odbc_columnprivileges(), odbc_procedurecolumns(), odbc_specialcolumns(), odbc_rollback(), odbc_setoption(), odbc_gettypeinfo(), odbc_primarykeys(), odbc_foreignkeys(), odbc_procedures(), odbc_statistics() | None | ODBC 데이터베이스 지속 연결
odbc result | odbc_prepare() | odbc_binmode(), odbc_cursor(), odbc_execute(), odbc_fetch_into(), odbc_fetch_row(), odbc_field_name(), odbc_field_num(), odbc_field_type(), odbc_field_len(), odbc_field_precision(), odbc_field_scale(), odbc_longreadlen(), odbc_num_fields(), odbc_num_rows(), odbc_result(), odbc_result_all(), odbc_setoption() | odbc_free_result() | ODBC 결과
birdstep link
birdstep result
OpenSSL key | openssl_get_privatekey(), openssl_get_publickey() | openssl_sign(), openssl_seal(), openssl_open(), openssl_verify() | openssl_free_key() | OpenSSL 키
OpenSSL X.509 | openssl_x509_read() | openssl_x509_parse(), openssl_x509_checkpurpose() | openssl_x509_free() | 공개키
oracle Cursor | ora_open() | ora_bind(), ora_columnname(), ora_columnsize(), ora_columntype(), ora_error(), ora_errorcode(), ora_exec(), ora_fetch(), ora_fetch_into(), ora_getcolumn(), ora_numcols(), ora_numrows(), ora_parse() | ora_close() | 오라클 커서
oracle link | ora_logon() | ora_do(), ora_error(), ora_errorcode(), ora_rollback(), ora_commitoff(), ora_commiton(), ora_open(), ora_commit() | ora_logoff() | 오라클 데이터베이스 연결
oracle link persistent | ora_plogon() | ora_do(), ora_error(), ora_errorcode(), ora_rollback(), ora_commitoff(), ora_commiton(), ora_open(), ora_commit() | None | 오라클 데이터베이스 지속 연결
pdf document | pdf_new() | pdf_add_bookmark(), pdf_add_launchlink(), pdf_add_locallink(), pdf_add_note(), pdf_add_pdflink(), pdf_add_weblink(), pdf_arc(), pdf_attach_file(), pdf_begin_page(), pdf_circle(), pdf_clip(), pdf_closepath(), pdf_closepath_fill_stroke(), pdf_closepath_stroke(), pdf_concat(), pdf_continue_text(), pdf_curveto(), pdf_end_page(), pdf_endpath(), pdf_fill(), pdf_fill_stroke(), pdf_findfont(), pdf_get_buffer(), pdf_get_image_height(), pdf_get_image_width(), pdf_get_parameter(), pdf_get_value(), pdf_lineto(), pdf_moveto(), pdf_open_ccitt(), pdf_open_file(), pdf_open_image_file(), pdf_place_image(), pdf_rect(), pdf_restore(), pdf_rotate(), pdf_save(), pdf_scale(), pdf_setdash(), pdf_setflat(), pdf_setfont(), pdf_setgray(), pdf_setgray_fill(), pdf_setgray_stroke(), pdf_setlinecap(), pdf_setlinejoin(), pdf_setlinewidth(), pdf_setmiterlimit(), pdf_setpolydash(), pdf_setrgbcolor(), pdf_setrgbcolor_fill(), pdf_setrgbcolor_stroke(), pdf_set_border_color(), pdf_set_border_dash(), pdf_set_border_style(), pdf_set_char_spacing(), pdf_set_duration(), pdf_set_font(), pdf_set_horiz_scaling(), pdf_set_parameter(), pdf_set_text_pos(), pdf_set_text_rendering(), pdf_set_value(), pdf_set_word_spacing(), pdf_show(), pdf_show_boxed(), pdf_show_xy(), pdf_skew(), pdf_stringwidth(), pdf_stroke(), pdf_translate(), pdf_add_thumbnail(), pdf_arcn(), pdf_begin_pattern(), pdf_begin_template(), pdf_end_pattern(), pdf_end_template(), pdf_initgraphics(), pdf_makespotcolor(), pdf_set_info(), pdf_setcolor(), pdf_setmatrix(), pdf_open_memory_image() | pdf_close(), pdf_delete() | PDF 문서
pdf image | pdf_open_image(), pdf_open_image_file(), pdf_open_memory_image() | pdf_get_image_height(), pdf_get_image_width(), pdf_open_CCITT(), pdf_place_image() | pdf_close_image() | PDF 파일 안의 이미지
pdf object
pdf outline
pgsql large object | pg_lo_open() | pg_lo_open(), pg_lo_create(), pg_lo_read(), pg_lo_read_all(), pg_lo_seek(), pg_lo_tell(), pg_lo_unlink(), pg_lo_write() | pg_lo_close() | PostgreSQL 라지 오브젝트
pgsql link | pg_connect() | pg_affected_rows(), pg_query(), pg_send_query(), pg_get_result(), pg_connection_busy(), pg_connection_reset(), pg_connection_status(), pg_last_error(), pg_last_notice(), pg_lo_create(), pg_lo_export(), pg_lo_import(), pg_lo_open(), pg_lo_unlink(), pg_host(), pg_port(), pg_dbname(), pg_options(), pg_copy_from(), pg_copy_to(), pg_end_copy(), pg_put_line(), pg_tty(), pg_trace(), pg_untrace(), pg_set_client_encoding(), pg_client_encoding(), pg_metadata(), pg_convert(), pg_insert(), pg_select(), pg_delete(), pg_update() | pg_close() | PostgreSQL 데이터베이스 연결
pgsql link persistent | pg_pconnect() | pg_affected_rows(), pg_query(), pg_send_query(), pg_get_result(), pg_connection_busy(), pg_connection_reset(), pg_connection_status(), pg_last_error(), pg_last_notice(), pg_lo_create(), pg_lo_export(), pg_lo_import(), pg_lo_open(), pg_lo_unlink(), pg_host(), pg_port(), pg_dbname(), pg_options(), pg_copy_from(), pg_copy_to(), pg_end_copy(), pg_put_line(), pg_tty(), pg_trace(), pg_untrace(), pg_set_client_encoding(), pg_client_encoding(), pg_metadata(), pg_convert(), pg_insert(), pg_select(), pg_delete(), pg_update() | None | PostgreSQL 데이터베이스 지속 연결
pgsql result | pg_execute(), pg_query(), pg_query_params(), pg_get_result() | pg_fetch_array(), pg_fetch_object(), pg_fetch_result(), pg_fetch_row(), pg_field_is_null(), pg_field_name(), pg_field_num(), pg_field_prtlen(), pg_field_size(), pg_field_type(), pg_last_oid(), pg_num_fields(), pg_num_rows(), pg_result_error(), pg_result_status() | pg_free_result() | PostgreSQL 결과
pgsql string
printer
printer brush
printer font
printer pen
pspell | pspell_new(), pspell_new_config(), pspell_new_personal() | pspell_add_to_personal(), pspell_add_to_session(), pspell_check(), pspell_clear_session(), pspell_config_ignore(), pspell_config_mode(), pspell_config_personal(), pspell_config_repl(), pspell_config_runtogether(), pspell_config_save_repl(), pspell_save_wordlist(), pspell_store_replacement(), pspell_suggest() | None | pspell 사전
pspell config | pspell_config_create() | pspell_new_config() | None | pspell 설정
Sablotron XSLT | xslt_create() | xslt_closelog(), xslt_openlog(), xslt_run(), xslt_set_sax_handler(), xslt_errno(), xslt_error(), xslt_fetch_result(), xslt_free() | xslt_free() | XSLT 파서
shmop | shmop_open() | shmop_read(), shmop_write(), shmop_size(), shmop_delete() | shmop_close()
sockets file descriptor set | socket() | accept_connect(), bind(), connect(), listen(), read(), write() | close() | 소켓
sockets i/o vector
stream | opendir() | readdir(), rewinddir() | closedir() | 디렉토리 핸들
stream | fopen(), tmpfile() | feof(), fflush(), fgetc(), fgetcsv(), fgets(), fgetss(), flock(), fpassthru(), fputs(), fwrite(), fread(), fseek(), ftell(), fstat(), ftruncate(), set_file_buffer(), rewind() | fclose() | 파일 핸들
stream | popen(), fsockopen(), pfsockopen() | feof(), fflush(), fgetc(), fgetcsv(), fgets(), fgetss(), fpassthru(), fputs(), fwrite(), fread() | pclose() | 프로세스 핸들
socket | fflush(), fgetc(), fgetcsv(), fgets(), fgetss(), fpassthru(), fputs(), fwrite(), fread() | fclose() | 소켓 핸들
sybase-db link | sybase_connect() | sybase_query(), sybase_select_db() | sybase_close() | DB 라이브러리를 이용한 Sybase 데이터베이스 연결
sybase-db link persistent | sybase_pconnect() | sybase_query(), sybase_select_db() | None | DB 라이브러리를 이용한 Sybase 데이터베이스 지속 연결
sybase-db result | sybase_query(), sybase_unbuffered_query() | sybase_data_seek(), sybase_fetch_array(), sybase_fetch_field(), sybase_fetch_object(), sybase_fetch_row(), sybase_field_seek(), sybase_num_fields(), sybase_num_rows(), sybase_result() | sybase_free_result() | DB 라이브러리를 이용한 Sybase 결과
sybase-ct link | sybase_connect() | sybase_affected_rows(), sybase_query(), sybase_select_db() | sybase_close() | CT 라이브러리를 이용한 Sybase 데이터베이스 연결
sybase-ct link persistent | sybase_pconnect() | sybase_affected_rows(), sybase_query(), sybase_select_db() | None | CT 라이브러리를 이용한 Sybase 데이터베이스 지속 연결
sybase-ct result | sybase_query() | sybase_data_seek(), sybase_fetch_array(), sybase_fetch_field(), sybase_fetch_object(), sybase_fetch_row(), sybase_field_seek(), sybase_num_fields(), sybase_num_rows(), sybase_result() | sybase_free_result() | CT 라이브러리를 이용한 Sybase 결과
sysvsem | sem_get() | sem_acquire() | sem_release() | System V 세마포어
sysvshm | shm_attach() | shm_remove(), shm_put_var(), shm_get_var(), shm_remove_var() | shm_detach() | System V 공유 메모리
wddx | wddx_packet_start() | wddx_add_vars() | wddx_packet_end() | WDDX 패킷
xml | xml_parser_create(), xml_parser_create_ns() | xml_set_object(), xml_set_element_handler(), xml_set_character_data_handler(), xml_set_processing_instruction_handler(), xml_set_default_handler(), xml_set_unparsed_entity_decl_handler(), xml_set_notation_decl_handler(), xml_set_external_entity_ref_handler(), xml_parse(), xml_get_error_code(), xml_error_string(), xml_get_current_line_number(), xml_get_current_column_number(), xml_get_current_byte_index(), xml_parse_into_struct(), xml_parser_set_option(), xml_parser_get_option() | xml_parser_free() | XML 파서
zlib | gzopen() | gzeof(), gzgetc(), gzgets(), gzgetss(), gzpassthru(), gzputs(), gzread(), gzrewind(), gzseek(), gztell(), gzwrite() | gzclose() | gz-압축 파일

User Contributed Notes

csaba at alum dot mit dot edu 06-Nov-2009 07:25
On my PHP 5.2 under Windows Vista, the resource type listed as interbase link is actually Firebird/InterBase link
ian at eiloart dot comNOSPAM 09-Aug-2001 12:02
Actually, the ldap functions don't return or expect to be passed resources at all. So you can use an integer, and is_resource and get_resource_type aren't any use with ldap.


List of Available Filters

Table of Contents

  • String Filters
  • Conversion Filters
  • Compression Filters
  • Encryption Filters

The following is a list of a few built-in stream filters for use with stream_filter_append(). Your version of PHP may have more filters (or fewer) than those listed here.

It is worth noting a slight asymmetry between stream_filter_append() and stream_filter_prepend(). Every PHP stream contains a small read buffer where it stores blocks of data retrieved from the filesystem or other resource in order to process data in the most efficient manner. As soon as data is pulled from the resource into the stream's internal buffer, it is immediately processed through any attached filters whether the PHP application is actually ready for the data or not. If data is sitting in the read buffer when a filter is appended, this data will be immediately processed through that filter making the fact that it was sitting in the buffer seem transparent. However, if data is sitting in the read buffer when a filter is prepended, this data will NOT be processed through that filter. It will instead wait until the next block of data is retrieved from the resource.

For a list of filters installed in your version of PHP use stream_get_filters().

User Contributed Notes

There are no user contributed notes for this page.


String Filters

Each of these filters does precisely what their name implies and correspond to the behavior of a built-in php string handling function. For more information on a given filter, refer to the manual page for the corresponding function.

string.rot13 Use of this filter is equivalent to processing all stream data through the str_rot13() function.

Example #1 string.rot13

<?php
$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'string.rot13');
fwrite($fp, "This is a test.\n");
/* Outputs:  Guvf vf n grfg.   */
?>

string.toupper Use of this filter is equivalent to processing all stream data through the strtoupper() function.

Example #2 string.toupper

<?php
$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'string.toupper');
fwrite($fp, "This is a test.\n");
/* Outputs:  THIS IS A TEST.   */
?>

string.tolower Use of this filter is equivalent to processing all stream data through the strtolower() function.

Example #3 string.tolower

<?php
$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'string.tolower');
fwrite($fp, "This is a test.\n");
/* Outputs:  this is a test.   */
?>

string.strip_tags (since PHP 5.0.0) Use of this filter is equivalent to processing all stream data through the strip_tags() function. It accepts parameters in one of two forms: Either as a string containing a list of tags similar to the second parameter of the strip_tags() function, or as an array of tag names.

Example #4 string.strip_tags

<?php
$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'string.strip_tags', STREAM_FILTER_WRITE, "<b><i><u>");
fwrite($fp, "<b>bolded text</b> enlarged to a <h1>level 1 heading</h1>\n");
fclose($fp);
/* Outputs:  <b>bolded text</b> enlarged to a level 1 heading   */

$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'string.strip_tags', STREAM_FILTER_WRITE, array('b','i','u'));
fwrite($fp, "<b>bolded text</b> enlarged to a <h1>level 1 heading</h1>\n");
fclose($fp);
/* Outputs:  <b>bolded text</b> enlarged to a level 1 heading   */
?>

User Contributed Notes

There are no user contributed notes for this page.


Conversion Filters

Like the string.* filters, the convert.* filters perform actions similar to their names. The convert filters were added with PHP 5.0.0. For more information on a given filter, refer to the manual page for the corresponding function.

convert.base64-encode and convert.base64-decode Use of these filters are equivalent to processing all stream data through the base64_encode() and base64_decode() functions respectively. convert.base64-encode supports parameters given as an associative array. If line-length is given, the base64 output will be split into chunks of line-length characters each. If line-break-chars is given, each chunk will be delimited by the characters given. These parameters give the same effect as using base64_encode() with chunk_split().

Example #1 convert.base64-encode & convert.base64-decode

<?php
$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'convert.base64-encode');
fwrite($fp, "This is a test.\n");
fclose($fp);
/* Outputs:  VGhpcyBpcyBhIHRlc3QuCg==  */

$param = array('line-length' => 8, 'line-break-chars' => "\r\n");
$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'convert.base64-encode', STREAM_FILTER_WRITE, $param);
fwrite($fp, "This is a test.\n");
fclose($fp);
/* Outputs:  VGhpcyBp
          :  cyBhIHRl
          :  c3QuCg==  */

$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'convert.base64-decode');
fwrite($fp, "VGhpcyBpcyBhIHRlc3QuCg==");
fclose($fp);
/* Outputs:  This is a test.  */
?>

convert.quoted-printable-encode and convert.quoted-printable-decode Use of the decode version of this filter is equivalent to processing all stream data through the quoted_printable_decode() functions. There is no function equivalent to convert.quoted-printable-encode. convert.quoted-printable-encode supports parameters given as an associative array. In addition to the parameters supported by convert.base64-encode, convert.quoted-printable-encode also supports boolean arguments binary and force-encode-first. convert.base64-decode only supports the line-break-chars parameter as a type-hint for stripping from the encoded payload.

Example #2 convert.quoted-printable-encode & convert.quoted-printable-decode

<?php
$fp = fopen('php://output', 'w');
stream_filter_append($fp, 'convert.quoted-printable-encode');
fwrite($fp, "This is a test.\n");
/* Outputs:  =This is a test.=0A  */
?>

User Contributed Notes

There are no user contributed notes for this page.


Compression Filters

While the Compression Wrappers provide a way of creating gzip and bz2 compatible files on the local filesystem, they do not provide a means for generalized compression over network streams, nor do they provide a means to begin with a non-compressed stream and transition to a compressed one. For this, a compression filter may be applied to any stream resource at any time.

Note: Compression filters do not generate headers and trailers used by command line utilities such as gzip. They only compress and decompress the payload portions of compressed data streams.

zlib.deflate (compression) and zlib.inflate (decompression) are implementations of the compression methods described in » RFC 1951. The deflate filter takes up to three parameters passed as an associative array. level describes the compression strength to use (1-9). Higher numbers will generally yield smaller payloads at the cost of additional processing time. Two special compression levels also exist: 0 (for no compression at all), and -1 (zlib internal default -- currently 6). window is the base-2 log of the compression loopback window size. Higher values (up to 15 -- 32768 bytes) yield better compression at a cost of memory, while lower values (down to 9 -- 512 bytes) yield worse compression in a smaller memory footprint. Default window size is currently 15. memory is a scale indicating how much work memory should be allocated. Valid values range from 1 (minimal allocation) to 9 (maximum allocation). This memory allocation affects speed only and does not impact the size of the generated payload.

Note: Because compression level is the most commonly used parameter, it may be alternatively provided as a simple integer value (rather than an array element).

zlib.* compression filters are available with PHP as of version 5.1.0 if zlib support is enabled. They are also available as a backport in version 5.0.x by installing the » zlib_filter package from » PECL.

Example #1 zlib.deflate and zlib.inflate

<?php
$params = array('level' => 6, 'window' => 15, 'memory' => 9);

$original_text = "This is a test.\nThis is only a test.\nThis is not an important string.\n";
echo "The original text is " . strlen($original_text) . " characters long.\n";

$fp = fopen('test.deflated', 'w');
stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, $params);
fwrite($fp, $original_text);
fclose($fp);

echo "The compressed file is " . filesize('test.deflated') . " bytes long.\n";
echo "The original text was:\n";
/* Use readfile and zlib.inflate to decompress on the fly */
readfile('php://filter/zlib.inflate/resource=test.deflated');

/* Generates output:

The original text is 70 characters long.
The compressed file is 56 bytes long.
The original text was:
This is a test.
This is only a test.
This is not an important string.

 */
?>

Example #2 zlib.deflate simple

<?php
$original_text = "This is a test.\nThis is only a test.\nThis is not an important string.\n";
echo "The original text is " . strlen($original_text) . " characters long.\n";

$fp = fopen('test.deflated', 'w');
/* Here "6" indicates compression level 6 */
stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, 6);
fwrite($fp, $original_text);
fclose($fp);

echo "The compressed file is " . filesize('test.deflated') . " bytes long.\n";

/* Generates output:

The original text is 70 characters long.
The compressed file is 56 bytes long.

 */
?>

bzip2.compress and bzip2.decompress work in the same manner as the zlib filters described above. The bzip2.compress filter accepts up to two parameters given as elements of an associative array: blocks is an integer value from 1 to 9 specifying the number of 100kbyte blocks of memory to allocate for workspace. work is also an integer value ranging from 0 to 250 indicating how much effort to expend using the normal compression method before falling back on a slower, but more reliable method. Tuning this parameter effects only compression speed. Neither size of compressed output nor memory usage are changed by this setting. A work factor of 0 instructs the bzip library to use an internal default. The bzip2.decompress filter only accepts one parameter, which can be passed as either an ordinary boolean value, or as the small element of an associative array. small, when set to a TRUE value, instructs the bzip library to perform decompression in a minimal memory footprint at the cost of speed.

bzip2.* compression filters are available with PHP as of version 5.1.0 if bz2 support is enabled. They are also available as a backport in version 5.0.x by installing the » bz2_filter package from » PECL.

Example #3 bzip2.compress and bzip2.decompress

<?php
$param = array('blocks' => 9, 'work' => 0);

echo "The original file is " . filesize('LICENSE') . " bytes long.\n";

$fp = fopen('LICENSE.compressed', 'w');
stream_filter_append($fp, 'bzip2.compress', STREAM_FILTER_WRITE, $param);
fwrite($fp, file_get_contents('LICENSE'));
fclose($fp);

echo "The compressed file is " . filesize('LICENSE.compressed') . " bytes long.\n";

/* Generates output:

The original text is 3288 characters long.
The compressed file is 1488 bytes long.

 */
?>

User Contributed Notes

Anonymous 22-Aug-2015 03:12
To read a gzip encoded stream from http
<?php
$opts = [
    "http" => [
        "method" => "GET",
        "header" => [ "Accept-Encoding: gzip" ],
    ]
];
$ctx = stream_context_create($opts);
$f = fopen("http://php.net", "r", false, $ctx);
// check  stream_get_meta_data($f)["wrapper_data"] has "Content-Encoding: gzip"
stream_filter_append($f, "zlib.inflate", STREAM_FILTER_READ, ["window" => 30]);
echo stream_get_contents($f); // any stream processing
fclose($f);


Encryption Filters

mcrypt.* and mdecrypt.* provide symmetric encryption and decryption using libmcrypt. Both sets of filters support the same algorithms available to mcrypt extension in the form of mcrypt.ciphername where ciphername is the name of the cipher as it would be passed to mcrypt_module_open(). The following five filter parameters are also available:

mcrypt filter parameters
Parameter | Required? | Default | Sample Values
mode | Optional | cbc | cbc, cfb, ecb, nofb, ofb, stream
algorithms_dir | Optional | ini_get('mcrypt.algorithms_dir') | Location of algorithms modules
modes_dir | Optional | ini_get('mcrypt.modes_dir') | Location of modes modules
iv | Required | N/A | Typically 8, 16, or 32 bytes of binary data. Depends on cipher
key | Required | N/A | Typically 8, 16, or 32 bytes of binary data. Depends on cipher

Example #1 Encrypting file output using 3DES

<?php
$passphrase = 'My secret';

/* Turn a human readable passphrase
 * into a reproducable iv/key pair
 */
$iv = substr(md5('iv'.$passphrase, true), 0, 8);
$key = substr(md5('pass1'.$passphrase, true) .
               md5('pass2'.$passphrase, true), 0, 24);
$opts = array('iv'=>$iv, 'key'=>$key);

$fp = fopen('secret-file.enc', 'wb');
stream_filter_append($fp, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts);
fwrite($fp, 'Secret secret secret data');
fclose($fp);
?>

Example #2 Reading an encrypted file

<?php
$passphrase = 'My secret';

/* Turn a human readable passphrase
 * into a reproducable iv/key pair
 */
$iv = substr(md5('iv'.$passphrase, true), 0, 8);
$key = substr(md5('pass1'.$passphrase, true) .
               md5('pass2'.$passphrase, true), 0, 24);
$opts = array('iv'=>$iv, 'key'=>$key);

$fp = fopen('secret-file.enc', 'rb');
stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_READ, $opts);
$data = rtrim(stream_get_contents($fp));
fclose($fp);

echo $data;
?>

User Contributed Notes

There are no user contributed notes for this page.


지원되는 소켓 전송 목록

Table of Contents

  • 인터넷 도메인: TCP, UDP, SSL, TLS
  • 유닉스 도메인: Unix와 UDG

다음은 fsockopen()과 stream_socket_client()같은 스트림 기반 소켓 함수를 사용하기 위해 PHP가 내장하고 있는 다양한 URL 스타일의 소켓 전송 목록이다. 이런 전송들은 Sockets Extension에 적용되지 않는다.

설치된 PHP 버전안에 사용가능한 전송 리스트를 보려면 stream_get_transports()를 사용하라.

User Contributed Notes

There are no user contributed notes for this page.


인터넷 도메인: TCP, UDP, SSL, TLS

PHP 4, PHP 5, PHP 6. PHP 4.3.0부터 ssl:// & tls:// PHP 5.0.2부터 sslv2:// & sslv3://

Note: 전송이 표시되지 않으면 tcp://라고 가정할것이다.

  • 127.0.0.1
  • fe80::1
  • www.example.com
  • tcp://127.0.0.1
  • tcp://fe80::1
  • tcp://www.example.com
  • udp://www.example.com
  • ssl://www.example.com
  • sslv2://www.example.com
  • sslv3://www.example.com
  • tls://www.example.com

인터넷 도메인 소켓은 목표 주소에 더하여 포트 번호가 올것을 예상할수 있다. fsockopen()의 경우 이것은 두번째 인수에서 설정되고 따라서 전송 URL의 구성에 영향을 주지 않는다. stream_socket_client()와 이와 관련된 함수는 전통적인 URL을 갖는다. 포트 번호는 전송 URL의 끝에 콜론으로 구분되어 덧붙여진다.

  • tcp://127.0.0.1:80
  • tcp://[fe80::1]:80
  • tcp://www.example.com:80

Note: 포트 번호를 갖는 IPv6 숫자 주소
위 예제중 두번째는, IPv4의 호스트명 예제가 콜른과 포트번호의 추가와는 별개로 남겨져있는데 반하여, IPv6 주소는 대괄호: [fe80::1]에 둘러쌓여 있다. 이것은 IPv6 주소에서 사용되는 콜론과 포트번호를 구분하기 위해 사용되는 콜론을 구별하기 위한 것이다.

ssl:// 과 tls:// 전송은 (openssl 지원이 PHP안에 컴파일되었을때만 가용) SSL 암호화를 포함하는 tcp:// 전송의 확장이다. PHP 4.3.0 이후에 OpenSSL 지원은 PHP와 정적으로 컴파일되어야 한다. PHP 5.0.0 이후부터는 모듈이건 정적이건 아무거나 컴파일 되어 사용될수 있을것이다.

ssl://은 원격 호스트의 가용성과 설정에 의존하여 SSL V2나 SSL V3 접속을 허용합니다. sslv2://와 sslv3://는 명시적으로 SSL V2나 SSL V3를 선택합니다.

User Contributed Notes

christian at lantian dot eu 17-Sep-2013 09:46
@pablo dot livardo  :  I think that the problem you found is caused by the difference between the client/server encryption methods used.

The 465 port is used for SMTPS, and the server starts the encryption immediately it receives your connection. So, your code will work.

The 587 port is used for Submission (MSA or Mail Submission Agent) which works like the port 25. The server accepts your connection and doesn't activate the encryption. If you want an encrypted connection on the port 587, you must connect on it without encryption, you must start to dialog with the server (with EHLO) and after that you must ask the server to start the encrypted connection using the STARTTLS command. The server starts the encryption and now you can start as well the encryption on your client.

So, in few words, you can not use :

<?php $fp = fsockopen("tls://mail.example.com", 587, $errno, $errstr);  ?>

but you can use:

 <?php $fp = stream_socket_client("mail.example.com:587", $errno, $errstr); ?>

and after you send the STARTTLS command, you can enable the crypto:

<?php stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); ?>

P.S. My previous note on this page was totally wrong, so I ask the php.net admin to remove it.

:)
christian at lantian dot eu 17-Sep-2013 05:17
@pablo dot livardo  :  I think that the problem you found is caused by the difference between the client/server encryption methods used.

You tried to access the port 587 using TLS encryption, but the server uses the SSLv3 encryption on this port.

You can communicate with the Google server on port 465 because on this port the communication is encrypted using TLS and your client is configured to recognize the TLS.

So, if you want to connect on the Google server over the port 587, you simply must write:

<?php
$fp = fsockopen("ssl://mail.example.com", 587, $errno, $errstr);
?>

instead of:

<?php
$fp = fsockopen("tls://mail.example.com", 587, $errno, $errstr);
?>

:)
stefan at example dot com 20-Aug-2010 02:42
Actually, PHP is very able to start with an unencrypted connection and then switch to an encrypted one - refer to http://php.net/stream_socket_enable_crypto .
pablo dot livardo at gmail dot com 09-Dec-2009 06:56
I've been having a problem with a TLS connection.

<?php
$fp = fsockopen("tls://mail.example.com", 587, $errno, $errstr);
?>

Which gives me an error of:

SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number

I believe this is caused by PHP not being able to start with an unencrypted connection and then switch to encryption even though the functionality is built into OpenSSL.

For Google Mail users you can avoid this by using port 465 instead of 587.


유닉스 도메인: Unix와 UDG

unix://와 udg:// (udg://는 PHP 5부터)

  • unix:///tmp/mysock
  • udg:///tmp/mysock

unix://는 유닉스 도메인에 대한 소켓 스트림 접속 시도를 제공한다. udg://는 사용자 데이터그램 프로토콜을 사용하는 유닉스 도메인 소켓에 대한 대체 전송을 제공한다.

유닉스 도메인 소켓은 인터넷 도메인 소켓과는 달리 포트 번호를 쓰지 않는다. fsockopen()에서 portno 인수는 0이 되어야 한다.

User Contributed Notes

Matthew Fortune 16-Feb-2007 12:03
It appears that fsockopen prior to php5 did not need the unix:// qualifier when opening a unix domain socket:

php4: fsockopen("/tmp/mysocket"......);

php5: fsockopen("unix:///tmp/mysocket"......);

This caught me out when upgrading.


PHP 자료형 비교표

다음 테이블은 PHP 자료형 과 느슨한 비교와 엄격한 비교를 위한 비교 연산자의 동작을 설명한다. 이 부록은 자료형 조절에 관한 매뉴얼 섹션과도 관련이 있다. 다양한 유저 주석과 » BlueShoes의 보고서를 통해 영감을 얻을수 있다.

이 테이블을 이용하기 전에, 자료형과 각 자료형의 의미를 이해해야 한다. 예를 들면, "42"는 string형이고, 반면에 42는 integer형이다. FALSE는 boolean형이고, "false"는 string형이 되는것이다.

Note:

HTML 폼은 정수, 부동소수점, 논리형으로 전달하지 않는다. 문자열형으로 전달한다. 문자열이 숫자값인지 확인하기 위해서는 is_numeric()를 사용할수 있다.

Note:

$x가 선언되지 않았는데 단순히 if ($x)를 수행하면 E_NOTICE 레벨의 에러가 발생할것이다. 대신 empty()나 isset()을 사용하고 그 변수를 초기화시키도록 한다.

PHP 함수로 $x 비교하기
Expression | gettype() | empty() | is_null() | isset() | boolean : if($x)
$x = ""; | string | TRUE | FALSE | TRUE | FALSE
$x = null; | NULL | TRUE | TRUE | FALSE | FALSE
var $x; | NULL | TRUE | TRUE | FALSE | FALSE
$x is undefined | NULL | TRUE | TRUE | FALSE | FALSE
$x = array(); | array | TRUE | FALSE | TRUE | FALSE
$x = false; | boolean | TRUE | FALSE | TRUE | FALSE
$x = true; | boolean | FALSE | FALSE | TRUE | TRUE
$x = 1; | integer | FALSE | FALSE | TRUE | TRUE
$x = 42; | integer | FALSE | FALSE | TRUE | TRUE
$x = 0; | integer | TRUE | FALSE | TRUE | FALSE
$x = -1; | integer | FALSE | FALSE | TRUE | TRUE
$x = "1"; | string | FALSE | FALSE | TRUE | TRUE
$x = "0"; | string | TRUE | FALSE | TRUE | FALSE
$x = "-1"; | string | FALSE | FALSE | TRUE | TRUE
$x = "php"; | string | FALSE | FALSE | TRUE | TRUE
$x = "true"; | string | FALSE | FALSE | TRUE | TRUE
$x = "false"; | string | FALSE | FALSE | TRUE | TRUE

==로 느슨한 비교
 | TRUE | FALSE | 1 | 0 | -1 | "1" | "0" | "-1" | NULL | array() | "php" | ""
TRUE | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE
FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE | TRUE
1 | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE
0 | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE
-1 | TRUE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE
"1" | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE
"0" | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE
"-1" | TRUE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE
NULL | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE | TRUE
array() | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE | FALSE
"php" | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE
"" | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE

===로 엄격한 비교
 | TRUE | FALSE | 1 | 0 | -1 | "1" | "0" | "-1" | NULL | array() | "php" | ""
TRUE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE
FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE
1 | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE
0 | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE
-1 | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE
"1" | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE
"0" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE
"-1" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE
NULL | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE
array() | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE
"php" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE
"" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE

User Contributed Notes

engineer dot morozov at gmail dot com 07-Feb-2016 12:23
If you want to calculate how much more/less items by the criterion

Example:

in loop:
$counter += intval($item->isValid()) <=> 0.5;

if $counter >= 0 - valid items are more then invalid on $counter
if $counter  < 0 - valid items are less then invalid on $counter
trexx68 26-Apr-2015 02:11
If you want to view the truth tables colorized, just:

1. Save a local copy of this page as an .html file,
2. View the page source with any text editor.
3. Replace the opening <head> tag so it will include this:

<head>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

4. Replace the closing </body> tag with this:

  <script>
    $( "td:contains('FALSE')" ).css("color", "red");
    $( "td:contains('TRUE')" ).css("color", "green");
  </script>
</body>

5. Save the file, and view it on your browser.  Enjoy.
aravind dot a dot padmanabhan at gmail dot com 22-Apr-2015 11:31
please note that

$x = 0.0 ;
print empty($x); // returns 1 (true)

$x = "0.0";
print empty($x); // returns blank (false);
Anonymous 17-Oct-2014 03:19
The loose comparison chart is missing a few things.

array(1)==true returns true.

also:

(int)array(1) === 1 returns true

This is symmetric:

array(1) === (array)1 returns true

but the loose comparison

array(1)==1 still returns false.
blue dot hirano at gmail dot com 07-Sep-2014 11:29
The truth tables really ought to be colorized; they're very hard to read as they are right now (just big arrays of TRUE and FALSE).

Also, something to consider: clustering the values which compare similarly (like is done on qntm.org/equality) would make the table easier to read as well. (This can be done simply by hand by rearranging the order of headings to bring related values closer together).
lorenzo dot campagna at ymail dot com 22-Jan-2014 06:33
Someone can explain why this comparison result TRUE  when it should be FALSE?

<?php

 $a = "3abcdef";
 $b = 3;

 if($a == $b) {print("Match!");}    //Because of this comparison TRUE script will output:

?>
Anonymous 21-Nov-2013 10:43
I'm running PHP 5.5.3.

This is a correction to one of the previous posts.

<?php
$o = new stdClass();
$o->we = 12;

$o2 = new stdClass();
$o2->we = 12;

$o3 = clone $o2;
var_dump($o == $o2); //true
var_dump($o === $o2); //false
var_dump($o3 === $o2); //false
?>

Output is:

true, false, false
crazy888s at hotmail dot com 15-Dec-2012 03:14
PHP's loose comparisons can be a huge convenience when used properly! It's extremely helpful to just remember the following are always FALSE:

null, false, "", 0, "0", array()

If your application never depends on a particular "empty/false/null/0/not set" value type, you won't have to worry about 99% of the other weird cases listed here. You won't need empty() or isset(). And ALL variable types will always work as expected for statements like:

if(boolean && !string){...}
if(array){...}
if(!null || int){...}

Consider the same when working with your database values.
php at richardneill dot org 13-Apr-2012 04:45
[Editor's note: As of PHP 5.4.4 this is no longer true. Integral strings that overflow into floating point numbers will no longer be considered equal.]

Be wary of string-comparison where both strings might be interpreted as numbers.  Eg:

$x="123456789012345678901234567890"; $y="123456789012345678900000000000";
echo  ($x==$y)?"equal":"not_equal";   #Prints equal !!

Both strings are getting converted to floats, then losing precision, then becoming equal :-(

Using "===" or making either of the strings non-numeric will prevent this.
[This is on a 32-bit machine, on a 64-bit, you will have to make the strings longer to see the effect]
Jouriy LYSENKO 17-Jun-2011 11:18
If $var not declared.

In php 5.2 :
<?php if($var) ?> - work

in php 5.3 :
<?php if($var) ?> - dont work and generate error E_NOTICE
php at benizi dot com 15-Feb-2010 07:31
It's interesting to note that 'empty()' and 'boolean : if($x)'
are paired as logical opposites, as are 'is_null()' and 'isset()'.
edgar at goodforall dot eu 15-Dec-2009 03:55
Some function to write out your own comparisson table in tsv format. Can be easily modified to add more testcases and/or binary functions. It will test all comparables against each other with all functions.

<?php
$funcs = array(
        /* Testing equality */
        'eq' => '==',
        'ne' => '!=',
        'gt' => '>',
        'lt' => '<',
        'ne2' => '<>',
        'lte' => '<=',
        'gte' => '>=',
        /* Testing identity */
        'id' => '===',
        'nid' => '!=='
);
class Test {
        protected $a;
        public $b;
        public function __construct($a,$b){
                $this->a = $a;
                $this->b = $b;
        }
        public function getab(){
                return $this->a.",". $this->b;
        }

}
$tst1 = new Test(1,2);
$tst2 = new Test(1,2);
$tst3 = new Test(2,2);
$tst4 = new Test(1,1);

$arr1 = array(1,2,3);
$arr2 = array(2,3,4);
$arr3 = array('a','b','c','d');
$arr4 = array('a','b','c');
$arr5 = array();

$comp1 = array(
        'ints' => array(-1,0,1,2),
        'floats' => array(-1.1,0.0,1.1,2.0),
        'string' => array('str', 'str1', '', '1'),
        'bools' => array(true, false),
        'null' => array(null),
        'objects' => array($tst1,$tst2,$tst3,$tst4),
        'arrays' => array($arr1, $arr2, $arr3, $arr4, $arr5)
);
$fbody = array();

foreach($funcs as $name => $op){
        $fbody[$name] = create_function('$a,$b', 'return $a ' . $op . ' $b;');
}

$table = array(array('function', 'comp1', 'comp2', 'f comp1 comp2', 'type'));
/* Do comparisons */
$comp2  = array();
foreach($comp1 as $type => $val){
        $comp2[$type] = $val;
}

foreach($comp1 as $key1 => $val1){
        foreach($comp2 as $key2 => $val2){
                addTableEntry($key1, $key2, $val1, $val2);
        }
}
$out = '';
foreach($table as $row){
        $out .= sprintf("%-20s\t%-20s\t%-20s\t%-20s\t%-20s\n", $row[0], $row[1], $row[2], $row[3], $row[4]);
}

print $out;
exit;

function addTableEntry($n1, $n2, $comp1, $comp2){
        global $table, $fbody;
        foreach($fbody as $fname => $func){
                        foreach($comp1 as $val1){
  foreach($comp2 as $val2){
                                        $val = $func($val1,$val2);
                                                $table[] = array($fname, gettype($val1) . ' => ' . sprintval($val1), gettype($val2) .' => ' . sprintval($val2), gettype($val) . ' => ' . sprintval($val), gettype($val1) . "-" . gettype($val2) . '-' . $fname);
                                        }
                        }
        }
}

function sprintval($val){
        if(is_object($val)){
                return 'object-' . $val->getab();
        }
        if(is_array($val)){
                return implode(',', $val);
        }
        if(is_bool($val)){
                if($val){
                        return 'true';
                }
                return 'false';
        }
        return strval($val);
}

?>
info at shaelf dot ru 06-Jan-2008 10:51
Compare object
<?php
$o = new stdClass();
$o->we = 12;

$o2 = new stdClass();
$o2->we = 12;

$o3 = clone $o2;
var_dump($o == $o2); //true
var_dump($o === $o2); //false
var_dump($o3 === $o2); //true
?>
frank 15-Aug-2007 12:06
A comparison table for <=,<,=>,> would be nice...
Following are TRUE (tested PHP4&5):
NULL <= -1
NULL <= 0
NULL <= 1
!(NULL >= -1)
NULL >= 0
!(NULL >= 1)
That was a surprise for me (and it is not like SQL, I would like to have the option to have SQL semantics with NULL...).
Jan 29-Dec-2005 08:23
Note that php comparison is not transitive:

"php" == 0 => true
0 == null => true
null == "php" => false
jerryschwartz at comfortable dot com 26-Jul-2005 10:04
In some languages, a boolean is promoted to an integer (with a value of 1 or -1, typically) if used in an expression with an integer. I found that PHP has it both ways:

If you add a boolean with a value of true to an integer with a value of 3, the result will be 4 (because the boolean is cast as an integer).

On the other hand, if you test a boolean with a value of true for equality with an integer with a value of three, the result will be true (because the integer is cast as a boolean).

Surprisingly, at first glance, if you use either < or > as the comparison operator the result is always false (again, because the integer as cast as a boolean, and true is neither greater nor less than true).
tom 17-Jun-2005 11:27
<?php
if (strlen($_POST['var']) > 0) {
    // form value is ok
}
?>

When working with HTML forms this a good way to:

(A) let "0" post values through like select or radio values that correspond to array keys or checkbox booleans that would return FALSE with empty(), and;
(B) screen out $x = "" values, that would return TRUE with isset()!

Because HTML forms post values as strings, this is a good way to test variables!

[[Editor Note: This will create a PHP Error of level E_NOTICE if the checked variable (in this case $_POST['var']) is undefined. It may be used after (in conjuection with) isset() to prevent this.]]
aidan at php dot net 24-Jan-2005 04:00
The way PHP handles comparisons when multiple types are concerned is quite confusing.

For example:
"php" == 0

This is true, because the string is casted interally to an integer. Any string (that does not start with a number), when casted to an integer, will be 0.


List of Parser Tokens

Various parts of the PHP language are represented internally by types like T_SR. PHP outputs identifiers like this one in parse errors, like "Parse error: unexpected T_SR, expecting ',' or ';' in script.php on line 10."

You're supposed to know what T_SR means. For everybody who doesn't know that, here is a table with those identifiers, PHP-syntax and references to the appropriate places in the manual.

Note: Usage of T_* constants

All tokens listed below are also defined as PHP constants. Their value is automatically generated based on PHP's underlying parser infrastructure. This means that the concrete value of a token may change between two PHP versions. For example the T_FILE constant is 365 in PHP 5.3, while the same value refers now to T_TRAIT in PHP 5.4 and the value of T_FILE is 369. This means that your code should never rely directly on the original T_* values taken from PHP version X.Y.Z, to provide some compatibility across multiple PHP versions. Instead your code should utilize custom values (using big numbers like 10000) and an appropriate strategy that will work with both PHP versions and T_* values.

Tokens
Token | Syntax | Reference
T_ABSTRACT | abstract | 클래스 추상화 (available since PHP 5.0.0)
T_AND_EQUAL | &= | assignment operators
T_ARRAY | array() | array(), array syntax
T_ARRAY_CAST | (array) | type-casting
T_AS | as | foreach
T_BAD_CHARACTER |  | anything below ASCII 32 except \t (0x09), \n (0x0a) and \r (0x0d)
T_BOOLEAN_AND | && | logical operators
T_BOOLEAN_OR | || | logical operators
T_BOOL_CAST | (bool) or (boolean) | type-casting
T_BREAK | break | break
T_CALLABLE | callable | callable
T_CASE | case | switch
T_CATCH | catch | 예외 (available since PHP 5.0.0)
T_CHARACTER |  | not used anymore
T_CLASS | class | classes and objects
T_CLASS_C | __CLASS__ | magic constants
T_CLONE | clone | classes and objects
T_CLOSE_TAG | ?> or %> | escaping from HTML
T_COMMENT | // or #, and /* */ | comments
T_CONCAT_EQUAL | .= | assignment operators
T_CONST | const | class constants
T_CONSTANT_ENCAPSED_STRING | "foo" or 'bar' | string syntax
T_CONTINUE | continue | continue
T_CURLY_OPEN | {$ | complex variable parsed syntax
T_DEC | -- | incrementing/decrementing operators
T_DECLARE | declare | declare
T_DEFAULT | default | switch
T_DIR | __DIR__ | magic constants (available since PHP 5.3.0)
T_DIV_EQUAL | /= | assignment operators
T_DNUMBER | 0.12, etc. | floating point numbers
T_DOC_COMMENT | /** */ | PHPDoc style comments
T_DO | do | do..while
T_DOLLAR_OPEN_CURLY_BRACES | ${ | complex variable parsed syntax
T_DOUBLE_ARROW | => | array syntax
T_DOUBLE_CAST | (real), (double) or (float) | type-casting
T_DOUBLE_COLON | :: | see T_PAAMAYIM_NEKUDOTAYIM below
T_ECHO | echo | echo
T_ELLIPSIS | ... | function arguments (available since PHP 5.6.0)
T_ELSE | else | else
T_ELSEIF | elseif | elseif
T_EMPTY | empty | empty()
T_ENCAPSED_AND_WHITESPACE | " $a" | constant part of string with variables
T_ENDDECLARE | enddeclare | declare, alternative syntax
T_ENDFOR | endfor | for, alternative syntax
T_ENDFOREACH | endforeach | foreach, alternative syntax
T_ENDIF | endif | if, alternative syntax
T_ENDSWITCH | endswitch | switch, alternative syntax
T_ENDWHILE | endwhile | while, alternative syntax
T_END_HEREDOC |  | heredoc syntax
T_EVAL | eval() | eval()
T_EXIT | exit or die | exit(), die()
T_EXTENDS | extends | extends, classes and objects
T_FILE | __FILE__ | magic constants
T_FINAL | final | Final 키워드
T_FINALLY | finally | 예외 (available since PHP 5.5.0)
T_FOR | for | for
T_FOREACH | foreach | foreach
T_FUNCTION | function or cfunction | functions
T_FUNC_C | __FUNCTION__ | magic constants
T_GLOBAL | global | variable scope
T_GOTO | goto | (available since PHP 5.3.0)
T_HALT_COMPILER | __halt_compiler() | __halt_compiler (available since PHP 5.1.0)
T_IF | if | if
T_IMPLEMENTS | implements | 객체 인터페이스
T_INC | ++ | incrementing/decrementing operators
T_INCLUDE | include() | include
T_INCLUDE_ONCE | include_once() | include_once
T_INLINE_HTML |  | text outside PHP
T_INSTANCEOF | instanceof | type operators
T_INSTEADOF | insteadof | 트레이트 (available since PHP 5.4.0)
T_INT_CAST | (int) or (integer) | type-casting
T_INTERFACE | interface | 객체 인터페이스
T_ISSET | isset() | isset()
T_IS_EQUAL | == | comparison operators
T_IS_GREATER_OR_EQUAL | >= | comparison operators
T_IS_IDENTICAL | === | comparison operators
T_IS_NOT_EQUAL | != or <> | comparison operators
T_IS_NOT_IDENTICAL | !== | comparison operators
T_IS_SMALLER_OR_EQUAL | <= | comparison operators
T_SPACESHIP | <=> | comparison operators (available since PHP 7.0.0)
T_LINE | __LINE__ | magic constants
T_LIST | list() | list()
T_LNUMBER | 123, 012, 0x1ac, etc. | integers
T_LOGICAL_AND | and | logical operators
T_LOGICAL_OR | or | logical operators
T_LOGICAL_XOR | xor | logical operators
T_METHOD_C | __METHOD__ | magic constants
T_MINUS_EQUAL | -= | assignment operators
T_MOD_EQUAL | %= | assignment operators
T_MUL_EQUAL | *= | assignment operators
T_NAMESPACE | namespace | namespaces (available since PHP 5.3.0)
T_NS_C | __NAMESPACE__ | namespaces (available since PHP 5.3.0)
T_NS_SEPARATOR | \ | namespaces (available since PHP 5.3.0)
T_NEW | new | classes and objects
T_NUM_STRING | "$a[0]" | numeric array index inside string
T_OBJECT_CAST | (object) | type-casting
T_OBJECT_OPERATOR | -> | classes and objects
T_OPEN_TAG | <?php, <? or <% | escaping from HTML
T_OPEN_TAG_WITH_ECHO | <?= or <%= | escaping from HTML
T_OR_EQUAL | |= | assignment operators
T_PAAMAYIM_NEKUDOTAYIM | :: | ::. Also defined as T_DOUBLE_COLON.
T_PLUS_EQUAL | += | assignment operators
T_POW | ** | arithmetic operators (available since PHP 5.6.0)
T_POW_EQUAL | **= | assignment operators (available since PHP 5.6.0)
T_PRINT | print() | print
T_PRIVATE | private | classes and objects
T_PUBLIC | public | classes and objects
T_PROTECTED | protected | classes and objects
T_REQUIRE | require() | require
T_REQUIRE_ONCE | require_once() | require_once
T_RETURN | return | returning values
T_SL | << | bitwise operators
T_SL_EQUAL | <<= | assignment operators
T_SR | >> | bitwise operators
T_SR_EQUAL | >>= | assignment operators
T_START_HEREDOC | <<< | heredoc syntax
T_STATIC | static | variable scope
T_STRING | parent, self, etc. | identifiers, e.g. keywords like parent and self, function names, class names and more are matched. See also T_CONSTANT_ENCAPSED_STRING.
T_STRING_CAST | (string) | type-casting
T_STRING_VARNAME | "${a | complex variable parsed syntax
T_SWITCH | switch | switch
T_THROW | throw | 예외
T_TRAIT | trait | 트레이트 (available since PHP 5.4.0)
T_TRAIT_C | __TRAIT__ | __TRAIT__ (available since PHP 5.4.0)
T_TRY | try | 예외
T_UNSET | unset() | unset()
T_UNSET_CAST | (unset) | type-casting
T_USE | use | namespaces (available since PHP 5.3.0; reserved since PHP 4.0.0)
T_VAR | var | classes and objects
T_VARIABLE | $foo | variables
T_WHILE | while | while, do..while
T_WHITESPACE | \t \r\n |
T_XOR_EQUAL | ^= | assignment operators
T_YIELD | yield | generators (available since PHP 5.5.0)

See also token_name().

User Contributed Notes

fgm at osinet dot fr 22-Sep-2008 10:08
T_ENCAPSED_AND_WHITESPACED is returned when parsing strings with evaluated content, like "some $value" or this example from the Strings reference page:

<?php
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>

This last example is tokenized as:
T_ECHO
  echo
T_WHITESPACE
  %20 (a space character)
T_START_HEREDOC
  <<
T_ENCAPSED_AND_WHITESPACE
  My name is "
T_VARIABLE
  $name
T_ENCAPSED_AND_WHITESPACE
  ". I am printing some
T_VARIABLE
  $foo
T_OBJECT_OPERATOR
  ->
T_STRING
  foo
T_ENCAPSED_AND_WHITESPACE
  . Now, I am printing some
T_CURLY_OPEN
  {
T_VARIABLE
  $foo
T_OBJECT_OPERATOR
  ->
T_STRING
  bar
(terminal)
  [
T_LNUMBER
  1
(terminal)
  ]
(terminal)
  }
T_ENCAPSED_AND_WHITESPACE
  . This should print a capital 'A': \x41
T_END_HEREDOC
  EOT
(terminal)
  ;
nathan at unfinitydesign dot com 22-Sep-2008 02:52
T_ENCAPSED_AND_WHITESPACE is whitespace which intersects a group of tokens. For example, an "unexpected T_ENCAPSED_AND_WHITESPACE" error is produced by the following code:

<?php
$main_output_world = 'snakes!';
echo('There are' 10 $main_output_world);
?>

Note the missing concatenation operator between the two strings leads to the whitespace error that is so named above. The concatenation operator instructs PHP to ignore the whitespace between the two code tokens (the so named "encapsed" data"), rather than parse it as a token itself.

The correct code would be:

<?php
$main_output_world = 'snakes!';
echo('There are' . 10 . $main_output_world);
?>

Note the addition of the concatenation operator between each token.


Userland Naming Guide

Table of Contents

  • Global Namespace
  • Rules
  • Tips

The following is a guide for how to best choose names for identifiers in userland PHP code. When choosing names for any code that creates symbols in the global namespace, it is important to take into account the following guidelines to prevent future versions of PHP from clashing with your symbols.

User Contributed Notes

clancy hood at gmail dot com 11-Apr-2009 07:15
The specified permitted characters in variable names (including the first character) are somewhat more permissive than you might expect.

The specified regex is [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* (from ./functions.user-defined.php)

On running the following you can see those outside of [a-zA-Z0-9_] that work. Your browser/shell may not display all properly, and some editors (I tested in Textmate) may simply remove things it doesn't grok as whitespace.  However, there are some pretty sexy characters in there should you be looking for a unique namespace which is not too lengthy.  Using them can be inconvenient for others who may not have a suitable keyboard for single-stroke entry (mine is Spanish). Best used for a program's internally relevant methods - which are those we wish least to pollute a namespace.

<?php

    header('Content-type:text/plain; CHARSET=ks_c_5601-1987');

    function unichr($u) {
        return mb_convert_encoding('&#'.intval($u).';', 'UTF-8', 'HTML-ENTITIES');
    }

    for($i = hexdec("7f"); $i <= hexdec("ff"); $i++) echo unichr($i)."\n";

    // simple example

    function n(){
        echo "I'm ok!";
    }

    n();

?>


Global Namespace

Here is an overview of code constructs that go into the global namespace:

  • functions

  • classes

  • interfaces

  • constants (not class constants)

  • variables defined outside of functions/methods

User Contributed Notes

There are no user contributed notes for this page.


Rules

The following list gives an overview of which rights the PHP project reserves for itself, when choosing names for new internal identifiers. The definitive guide is the official » CODING STANDARDS:

  • PHP owns the top-level namespace but tries to find decent descriptive names and avoid any obvious clashes.

  • Function names use underscores between words, while class names use both the camelCase and PascalCase rules.

  • PHP will prefix any global symbols of an extension with the name of the extension. (In the past, there have been numerous exceptions to this rule.) Examples:

    • curl_close()

    • mysql_query()

    • PREG_SPLIT_DELIM_CAPTURE

    • new DOMDocument()

    • strpos() (example of a past mistake)

    • new SplFileObject()

  • Iterators and Exceptions are however simply postfixed with "Iterator" and "Exception." Examples:

    • ArrayIterator

    • LogicException

  • PHP reserves all symbols starting with __ as magical. It is recommended that you do not create symbols starting with __ in PHP unless you want to use documented magical functionality. Examples:

    • __get()

    • __autoload()

User Contributed Notes

There are no user contributed notes for this page.


Tips

In order to write future-proof code, it is recommended that you don't place many variables, functions or classes in the global namespace. This will prevent naming conflicts with 3rd party code as well as possible future additions to the language.

One common way to prevent naming conflicts of functions and classes is to add them to their own dedicated namespace.

<?php

namespace MyProject;

function my_function() {
    return true;
}

\MyProject\my_function();

This still needs you to keep track of already used namespaces, but once you have decided on a namespace you will be using you can add all functions and classes to it without having to think about conflicts again.

It is considered best practice to limit the number of variables added to the global scope in order to prevent naming conflicts with 3rd party code.

Note: Variable scoping

Because of PHP's scoping rules variables defined inside functions and methods are not in the global scope and as such cannot conflict with other variables defined in the global scope.

User Contributed Notes

There are no user contributed notes for this page.


매뉴얼에 대하여

Table of Contents

  • 형식
  • 사용자 주석에 대하여
  • 함수 선언(prototype)을 읽는 법
  • 매뉴얼에 실려있는 PHP 버전
  • PHP에 대한 더 많은 정보 찾는 법
  • 문서 향상을 돕는 법
  • 형식을 어떻게 만들었는가
  • 번역

User Contributed Notes

DaveRandom 16-Mar-2012 07:26
Since searching for _ (an underscore) in the function list ends up here, and _() is a valid alias for gettext() you may be looking for this page if you've ended up here:

http://php.net/manual/en/function.gettext.php


형식

PHP 매뉴얼은 여러 형식으로 제공됩니다. 제공하는 형식은 두가지 그룹으로 나눌 수 있습니다: 온라인에서 읽을 수 있는 형식, 내려받을 수 있는 패키지.

Note:

이 매뉴얼의 출력판을 만들 수 있지만, 추천하지 않습니다. 이런 출력물은 최신판에서 빠르게 뒤쳐지기 때문입니다.

매뉴얼은 » PHP.net 웹사이트와 여러 미러 사이트를 통하여 온라인으로 읽을 수 있습니다. 빠른 속도를 위해서는 가장 가까운 미러 사이트를 선택하십시오. PHP 매뉴얼 온라인판은 웹용과 출력용, 두가지 CSS 스타일 시트를 제공합니다:

오프라인 형식에 비해 온라인 매뉴얼이 가지는 장점은, 사용자 공헌 주석을 제공하고 원하는 매뉴얼 부분을 빠르게 찾을 수 있는 » URL 단축을 제공하는 점입니다. 확실한 단점은 온라인이여야 한다는 점입니다.

오프라인 매뉴얼은 몇가지 형식으로 제공됩니다. 가장 적합한 형식은 사용중인 운영체제와 개인적인 독서 방식에 따라 다릅니다. 어떻게 매뉴얼이 이렇게 많은 형식으로 만들어지는지 알고 싶다면, 이 부록의 '어떻게 우리가 형식을 만드는가' 섹션을 읽어보십시오.

매뉴얼에서 가장 호환성이 높은 형식은 HTML판입니다. HTML판은 하나의 HTML 파일로 만들어진 것과, 각 섹션을 개별 파일로 나눈 패키지(수천개 파일의 모음)를 제공합니다. HTML 형식은 압축판으로도 제공합니다. 압축판에서 파일을 꺼내려면 압축 해제 유틸리티가 필요합니다.

윈도우 플랫폼을 위해서, 개량한 HTML 형식을 사용하는 윈도우 HTML 도움말판을 제공합니다. 이 형식은 문서전체 검색, 전체 목차, 북마크를 제공합니다. 많은 유명한 윈도우 PHP 개발 환경이 쉬운 접근을 위하여 이 형식의 문서를 탑재하고 있습니다. 리눅스 데스크탑을 위한 CHM 뷰어도 있습니다. » xCHM이나 » GnoCHM을 확인해 보십시오.

자주 갱신되진 않지만, 추가적인 기능을 제공하는 » 개량 CHM판도 제공합니다. 도움말 페이지를 만드는데 사용하는 기술로 인하여 마이크로소프트 윈도우에서만 작동합니다.

User Contributed Notes

There are no user contributed notes for this page.


사용자 주석에 대하여

사용자 공헌 주석은 매뉴얼을 개발하는 데 중요한 역할을 합니다. 독자가 브라우저에서 예제, 경고, 자세한 설명을 제공해줌으로써, 매뉴얼에 대한 피드백을 받아 매뉴얼 본문을 수정할 수 있습니다. 주석이 매뉴얼 본문에 포함되기 전에는 제공한 내용을 온라인이나, 몇몇 오프라인 형식에서 확인할 수 있습니다.

Note:

사용자 공헌 주석은 온라인에 보여지기 전에 수정을 거치지 않기 때문에, 내용 및 예제 코드의 질, 심지어 내용의 정확성마저도 보장할 수 없습니다. (물론 매뉴얼 자체의 질이나 정확성에 대해서 보장을 하진 않습니다)

Note:

라이센스를 보호하기 위해서 사용자 공헌 주석은 PHP 매뉴얼의 부속물로 봅니다. 따라서 이 문서를 보호하는 라이센스와 동일한 보호를 받습니다. (현재 Creative Commons Attribution) 자세한 사항은 매뉴얼 저작권을 참고하십시오.

User Contributed Notes

There are no user contributed notes for this page.


함수 선언(prototype)을 읽는 법

각 함수는 빠른 레퍼런스를 위해 문서화되어있다. 매뉴얼을 읽고 이해하는 법을 알면 PHP 학습이 더욱 쉬워질 것이다. 예제나 복사/붙여넣기에 의지하기 보다는, 함수 선언(prototypes)을 읽는 법을 알아야 합니다. 시작해 봅시다:

Note: 필요 조건: 타입에 대한 기본적인 이해

PHP가 느슨한 타입 언어임에도 불구하고, 타입에 대한 기본적인 이해는 중요합니다. 타입은 중요한 의미를 가지고 있기 때문입니다.

함수 선언은 어떤 타입의 값이 반환되는지 알려줍니다. 첫번째 예제로 strlen()에 대한 선언을 사용합시다:

strlen

(PHP 4, PHP 5)
strlen -- 문자열의 길이를 구합니다.

설명
int strlen ( string $string )

문자열의 길이를 반환합니다.

함수 선언 해설
부분 | 설명
strlen | 함수 이름입니다.
(PHP 4, PHP 5, PHP 6) | strlen()은 PHP 4, PHP 5, PHP 6의 모든 버전에 있습니다.
int | 이 함수가 반환하는 값은 integer형입니다. (즉, 문자열의 길이가 숫자로 측정됩니다)
( string $string ) | 이 함수의 처음 (이 경우는 하나뿐인) 인수는 string이라는 이름을 가지는 string형입니다.

위 함수 선언을 일반적인 방법으로 다시 작성할 수 있습니다:

      반환형    함수이름    ( 인수형   인수이름 )

in_array()처럼, 많은 함수는 여러 인수를 가집니다. 프로토타입은 다음과 같습니다:

      bool in_array ( mixed $needle, array $haystack [, bool $strict])

이것이 무엇을 의미할까요? in_array()는 불값을 반환합니다. 성공시(haystack에서 needle을 찾았을 때)에는 TRUE를, 실패시(haystack에서 needle을 찾지 못했을 때)에는 FALSE입니다. 첫번째 인수는 needle이라는 이름을 가지고, 여러 타입이 될 수 있기 때문에, "mixed"라고 불립니다. (찾을 목표인) mixed needle은 스칼라 값(문자열, 정수, 실수)이나 배열이 될 수 있습니다. (찾아볼 배열) haystack은 두번째 인수입니다. 세번째 선택적인 인수 이름은 strict입니다. 모든 선택적인 인수는 [ 브라켓 ] 사이에 들어갑니다. 매뉴얼에 strict 인수의 기본값은 불 FALSE로 나와 있습니다. 인수가 어떻게 동작하는지에 대한 자세한 사항은 각 함수의 매뉴얼 페이지를 참고하십시오.

조금 복잡한 PHP 버전 정보를 가지는 함수도 있습니다. 예를 들어 html_entity_decode()를 봅시다:

(PHP 4 >= 4.3.0, PHP 5)

이 함수가 PHP 4.3.0 이후에 릴리즈된 버전에서만 사용할 수 있음을 나타냅니다.

User Contributed Notes

Anonymous 21-Mar-2015 05:37
the functioning for php was read wrong by a ton of people who documented that the strlen() function was actually strln even.
php dot devel at homelinkcs dot com 13-Jul-2005 02:50
More specifically, an ampersand (&) prepended to an argument name means that the argument will be passed by reference (http://www.php.net/manual/en/language.references.pass.php).
ceo at l-i-e dot com 10-Mar-2005 12:16
Another thing to watch for is the & in the argument list.

That generally means that the function is going to *CHANGE* the value you pass in, in some way, and you can't rely on it being the same as what you handed off to the function.


매뉴얼에 실려있는 PHP 버전

매뉴얼에는 과거, 현재, 미래의 PHP 버전에 대한 정보를 포함하고 있습니다. 버전에 따라 다른 행동은, 매뉴얼 안에 주석, 변경점 등으로 안내하고 있습니다. 문서에 포함된 가장 오래된 버전은 PHP 4.1.0이고, 가장 최근 버전은 PHP 6.x.x입니다.

가장 최근 개발 버전인 PHP CVS 버전에 대한 문서가 존재하기도 합니다. 이는 아직 릴리즈되지 않아 CVS 버전 관리 시스템이나 » 스냅샷을 통해서만 사용할 수 있습니다. 그러므로 "available in CVS"라고 나온 기능은 보통 사용할 수 없는 것입니다. 그러나, 이런 기능은 다음 PHP 안정 버전에서 사용할 수 있게 될 것입니다. CVS 버전을 받으려면, » 익명사용자 CVS 접근 페이지를 참고하십시오.

또한, 존재하지 않는 PHP 버전에 대한 문서도 있을 수 있습니다. (최종 안정판이 5.x.x인데 PHP 6.x.x로 표시된 것 등) 이들은 대부분, 문서 오류가 아닙니다. 매뉴얼에는 현재 PHP 릴리즈에서 사용할 수 없지만, 다음 PHP 버전에서 추가될 것으로 알려진 기능에 대한 설명이 자주 추가됩니다. 보통, PHP는 메이저 릴리즈에서만 새 기능을 추가하고, 나머지는 버그 수정판입니다. A.B.C 버전 형식을 사용하여, 메이저 릴리즈는 A나 B를 증가시키고, 마이너 릴리즈는 C를 증가시킵니다. 그러므로 최종 릴리즈가 PHP x.0.x일 때 PHP x.1.x부터 사용할 수 있다고 써 있는 경우가 자주 있습니다. 문서는 미래 시제가 아닌 현재 시제로 쓰여진다는 점에 주의하십시오.

PHP 매뉴얼은 종종 PHP 지시어의 "기본값" 목록을 제공합니다. 이 값은 php.ini-dist에 기초하고 있으며 php.ini-recommended가 아닙니다. 두 파일 모두 최신 버전의 PHP를 참조합니다. 이러한 값과 변경에 대해 자세한 사항은 PHP 지시어 부록을 참고하십시오.

User Contributed Notes

There are no user contributed notes for this page.


PHP에 대한 더 많은 정보 찾는 법

이 매뉴얼에서 일반적인 프로그래밍 연습을 위한 설명은 제공하지 않습니다. 초보 프로그래며는 이 매뉴얼만으로는 PHP 프로그램을 작성하는 방법을 배우기 어려울 것입니다. 대신, 초보자를 위한 기초 문서를 찾아보십시오.

PHP 프로그래밍을 위한 모든 부문의 많은 수의 토론용 메일링 리스트가 활성화되어 있습니다. 해결책을 찾을수 없는 문제에 빠져있다면, 이러한 메일링 리스트를 사용해 보십시오. 메일링 리스트를 포함한 지원 정보는 » PHP.net 지원 페이지를 참고하십시오. 또한, » PHP.net 링크 페이지에 PHP에 관련된 문서, 포럼, 코드를 제공하는 웹사이트 목록이 있습니다.

User Contributed Notes

There are no user contributed notes for this page.


문서 향상을 돕는 법

모두가 문서 향상을 돕는 세가지 방법이 있습니다.

어떤 언어던지 매뉴얼에서 오류를 발견한다면, » http://bugs.php.net/에 있는 버그 시스템을 사용하여 보고해 주십시오. 버그 분류는 "Documentation Problem"입니다. 매뉴얼 형식을 포함한 모든 문서 관련 문제를 버그로 보고할 수 있습니다.

Note:

버그 시스템에 도움을 요청하지 마십시오. 대신, 많은 » 지원 옵션을 사용하십시오.

공헌 주석으로 사용자가 다른 독자를 위한 추가적인 예제, 경고, 자세한 설명을 제공할 수 있습니다. 그러나 주석 시스템에 버그를 신고하지 말아주십시오. 자세한 사항은 '사용자 주석에 대하여'를 참고하십시오.

PHP 매뉴얼은 많은 언어로 번역되어 있습니다. 영어와 그 외의 외국어를 안다면 PHP 매뉴얼 번역팀의 작업을 도울 수 있습니다. 새로운 번역을 시작하거나, 현재 번역판을 돕고 싶다면 » http://doc.php.net/tutorial/를 읽어보십시오.

PHP 문서 프로젝트는 많은 매뉴얼 작성자가 상주하는 IRC 채널을 가지고 있습니다. irc.efnet.org의 #php.doc에서 PHP 문서를 향상하기 위한 대화를 할 수 있습니다.

User Contributed Notes

There are no user contributed notes for this page.


형식을 어떻게 만들었는가

이 매뉴얼은 » DocBook XML DTD를 사용하여 XML로 쓰여져 있고, 관리와 형식을 위해 » PhD([PH]P 기반 [D]ocBook 렌더러)를 사용합니다.

원본 형식에 XML을 사용하여 원본 파일에서 많은 출력 형식을 만들어냄으로써, 모든 형식에 대하여 하나의 원본 문서만 관리할 수 있습니다. 온라인 매뉴얼 형식화에는 » PhD를 사용합니다. 윈도우 HTML 도움말 형식을 작성하기 위해 » 마이크로소프트 HTML 도움말 워크샵을 사용합니다. 물론, 추가적인 변환과 형식화를 위해 PHP도 사용합니다.

PHP 매뉴얼은 다양한 언어와 형식으로 작성됩니다. 자세한 사항은 » http://www.php.net/docs.php을 참고하십시오. XML 원본 코드는 CVS에서 내려 받을 수 있고, » http://cvs.php.net/에서 볼 수 있습니다. 문서는 phpdco 모듈에 저장되어 있습니다.

User Contributed Notes

There are no user contributed notes for this page.


번역

PHP 매뉴얼은 다양한 형식뿐 아니라, 다양한 언어로도 제공됩니다. 매뉴얼 내용은 우선 영어로 씌여지고, 전세계에 있는 팀원이 모국어로 번역합니다. 특정 함수나 장에 대해 아직 번역이 되지 않았다면, 빌드 시스템이 그 문서에 대한 영어 버전을 연결합니다.

번역하는 사람은 » http://cvs.php.net/에 있는 XML 소스 코드를 모국어로 번역합니다. 빌드 시스템이 XML을 읽기 쉬운 형식으로 변환하기 때문에 생성된 형식(HTML이나 일반 텍스트 등)은 사용하지 않습니다.

Note:

문서 번역에 도움을 주려면, phpdoc 메일링 리스트에 가입하여 번역/문서팀을 만나보십시오; 빈 메일을 » phpdoc-subscribe@lists.php.net로 보내십시오. 메일링 리스트 주소는 phpdoc@lists.php.net입니다. 매뉴얼 번역에 관심이 있다는 메세지를 보내면 누군가가 새로운 언어 번역을 시작하거나, 해당 언어의 기존 팀을 만날 수 있도록 답장을 줄 것입니다.

현재 매뉴얼은, 전체적 혹은 부분적으로 10개 이상의 언어로 제공되고 있습니다.

모두 여기에서 받을 수 있습니다: » http://www.php.net/docs.php.

User Contributed Notes

There are no user contributed notes for this page.


Creative Commons Attribution 3.0

THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.

BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.

1. Definitions

  1. "Adaptation" means a work based upon the Work, or upon the Work and other pre-existing works, such as a translation, adaptation, derivative work, arrangement of music or other alterations of a literary or artistic work, or phonogram or performance and includes cinematographic adaptations or any other form in which the Work may be recast, transformed, or adapted including in any form recognizably derived from the original, except that a work that constitutes a Collection will not be considered an Adaptation for the purpose of this License. For the avoidance of doubt, where the Work is a musical work, performance or phonogram, the synchronization of the Work in timed-relation with a moving image ("synching") will be considered an Adaptation for the purpose of this License.
  2. "Collection" means a collection of literary or artistic works, such as encyclopedias and anthologies, or performances, phonograms or broadcasts, or other works or subject matter other than works listed in Section 1(f) below, which, by reason of the selection and arrangement of their contents, constitute intellectual creations, in which the Work is included in its entirety in unmodified form along with one or more other contributions, each constituting separate and independent works in themselves, which together are assembled into a collective whole. A work that constitutes a Collection will not be considered an Adaptation (as defined above) for the purposes of this License.
  3. "Distribute" means to make available to the public the original and copies of the Work or Adaptation, as appropriate, through sale or other transfer of ownership.
  4. "Licensor" means the individual, individuals, entity or entities that offer(s) the Work under the terms of this License.
  5. "Work" means the literary and/or artistic work offered under the terms of this License including without limitation any production in the literary, scientific and artistic domain, whatever may be the mode or form of its expression including digital form, such as a book, pamphlet and other writing; a lecture, address, sermon or other work of the same nature; a dramatic or dramatico-musical work; a choreographic work or entertainment in dumb show; a musical composition with or without words; a cinematographic work to which are assimilated works expressed by a process analogous to cinematography; a work of drawing, painting, architecture, sculpture, engraving or lithography; a photographic work to which are assimilated works expressed by a process analogous to photography; a work of applied art; an illustration, map, plan, sketch or three-dimensional work relative to geography, topography, architecture or science; a performance; a broadcast; a phonogram; a compilation of data to the extent it is protected as a copyrightable work; or a work performed by a variety or circus performer to the extent it is not otherwise considered a literary or artistic work.
  6. "You" means an individual or entity exercising rights under this License who has not previously violated the terms of this License with respect to the Work, or who has received express permission from the Licensor to exercise rights under this License despite a previous violation.
  7. "Publicly Perform" means to perform public recitations of the Work and to communicate to the public those public recitations, by any means or process, including by wire or wireless means or public digital performances; to make available to the public Works in such a way that members of the public may access these Works from a place and at a place individually chosen by them; to perform the Work to the public by any means or process and the communication to the public of the performances of the Work, including by public digital performance; to broadcast and rebroadcast the Work by any means including signs, sounds or images.
  8. "Reproduce" means to make copies of the Work by any means including without limitation by sound or visual recordings and the right of fixation and reproducing fixations of the Work, including storage of a protected performance or phonogram in digital form or other electronic medium.

2. Fair Dealing Rights. Nothing in this License is intended to reduce, limit, or restrict any uses free from copyright or rights arising from limitations or exceptions that are provided for in connection with the copyright protection under copyright law or other applicable laws.

3. License Grant. Subject to the terms and conditions of this License, Licensor hereby grants You a worldwide, royalty-free, non-exclusive, perpetual (for the duration of the applicable copyright) license to exercise the rights in the Work as stated below:

  1. to Reproduce the Work, to incorporate the Work into one or more Collections, and to Reproduce the Work as incorporated in the Collections;
  2. to create and Reproduce Adaptations provided that any such Adaptation, including any translation in any medium, takes reasonable steps to clearly label, demarcate or otherwise identify that changes were made to the original Work. For example, a translation could be marked "The original work was translated from English to Spanish," or a modification could indicate "The original work has been modified.";
  3. to Distribute and Publicly Perform the Work including as incorporated in Collections; and,
  4. to Distribute and Publicly Perform Adaptations.

  5. For the avoidance of doubt:

    1. Non-waivable Compulsory License Schemes. In those jurisdictions in which the right to collect royalties through any statutory or compulsory licensing scheme cannot be waived, the Licensor reserves the exclusive right to collect such royalties for any exercise by You of the rights granted under this License;
    2. Waivable Compulsory License Schemes. In those jurisdictions in which the right to collect royalties through any statutory or compulsory licensing scheme can be waived, the Licensor waives the exclusive right to collect such royalties for any exercise by You of the rights granted under this License; and,
    3. Voluntary License Schemes. The Licensor waives the right to collect royalties, whether individually or, in the event that the Licensor is a member of a collecting society that administers voluntary licensing schemes, via that society, from any exercise by You of the rights granted under this License.

The above rights may be exercised in all media and formats whether now known or hereafter devised. The above rights include the right to make such modifications as are technically necessary to exercise the rights in other media and formats. Subject to Section 8(f), all rights not expressly granted by Licensor are hereby reserved.

4. Restrictions. The license granted in Section 3 above is expressly made subject to and limited by the following restrictions:

  1. You may Distribute or Publicly Perform the Work only under the terms of this License. You must include a copy of, or the Uniform Resource Identifier (URI) for, this License with every copy of the Work You Distribute or Publicly Perform. You may not offer or impose any terms on the Work that restrict the terms of this License or the ability of the recipient of the Work to exercise the rights granted to that recipient under the terms of the License. You may not sublicense the Work. You must keep intact all notices that refer to this License and to the disclaimer of warranties with every copy of the Work You Distribute or Publicly Perform. When You Distribute or Publicly Perform the Work, You may not impose any effective technological measures on the Work that restrict the ability of a recipient of the Work from You to exercise the rights granted to that recipient under the terms of the License. This Section 4(a) applies to the Work as incorporated in a Collection, but this does not require the Collection apart from the Work itself to be made subject to the terms of this License. If You create a Collection, upon notice from any Licensor You must, to the extent practicable, remove from the Collection any credit as required by Section 4(b), as requested. If You create an Adaptation, upon notice from any Licensor You must, to the extent practicable, remove from the Adaptation any credit as required by Section 4(b), as requested.
  2. If You Distribute, or Publicly Perform the Work or any Adaptations or Collections, You must, unless a request has been made pursuant to Section 4(a), keep intact all copyright notices for the Work and provide, reasonable to the medium or means You are utilizing: (i) the name of the Original Author (or pseudonym, if applicable) if supplied, and/or if the Original Author and/or Licensor designate another party or parties (e.g., a sponsor institute, publishing entity, journal) for attribution ("Attribution Parties") in Licensor's copyright notice, terms of service or by other reasonable means, the name of such party or parties; (ii) the title of the Work if supplied; (iii) to the extent reasonably practicable, the URI, if any, that Licensor specifies to be associated with the Work, unless such URI does not refer to the copyright notice or licensing information for the Work; and (iv) , consistent with Section 3(b), in the case of an Adaptation, a credit identifying the use of the Work in the Adaptation (e.g., "French translation of the Work by Original Author," or "Screenplay based on original Work by Original Author"). The credit required by this Section 4 (b) may be implemented in any reasonable manner; provided, however, that in the case of a Adaptation or Collection, at a minimum such credit will appear, if a credit for all contributing authors of the Adaptation or Collection appears, then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors. For the avoidance of doubt, You may only use the credit required by this Section for the purpose of attribution in the manner set out above and, by exercising Your rights under this License, You may not implicitly or explicitly assert or imply any connection with, sponsorship or endorsement by the Original Author, Licensor and/or Attribution Parties, as appropriate, of You or Your use of the Work, without the separate, express prior written permission of the Original Author, Licensor and/or Attribution Parties.
  3. Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable law, if You Reproduce, Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or Collections, You must not distort, mutilate, modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author's honor or reputation. Licensor agrees that in those jurisdictions (e.g. Japan), in which any exercise of the right granted in Section 3(b) of this License (the right to make Adaptations) would be deemed to be a distortion, mutilation, modification or other derogatory action prejudicial to the Original Author's honor and reputation, the Licensor will waive or not assert, as appropriate, this Section, to the fullest extent permitted by the applicable national law, to enable You to reasonably exercise Your right under Section 3(b) of this License (right to make Adaptations) but not otherwise.

5. Representations, Warranties and Disclaimer

UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN WRITING, LICENSOR OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE WORK, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU.

6. Limitation on Liability. EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

7. Termination.

  1. This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License. Individuals or entities who have received Adaptations or Collections from You under this License, however, will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will survive any termination of this License.
  2. Subject to the above terms and conditions, the license granted here is perpetual (for the duration of the applicable copyright in the Work). Notwithstanding the above, Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time; provided, however that any such election will not serve to withdraw this License (or any other license that has been, or is required to be, granted under the terms of this License), and this License will continue in full force and effect unless terminated as stated above.

8. Miscellaneous

  1. Each time You Distribute or Publicly Perform the Work or a Collection, the Licensor offers to the recipient a license to the Work on the same terms and conditions as the license granted to You under this License.
  2. Each time You Distribute or Publicly Perform an Adaptation, Licensor offers to the recipient a license to the original Work on the same terms and conditions as the license granted to You under this License.
  3. If any provision of this License is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this License, and without further action by the parties to this agreement, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
  4. No term or provision of this License shall be deemed waived and no breach consented to unless such waiver or consent shall be in writing and signed by the party to be charged with such waiver or consent
  5. This License constitutes the entire agreement between the parties with respect to the Work licensed here. There are no understandings, agreements or representations with respect to the Work not specified here. Licensor shall not be bound by any additional provisions that may appear in any communication from You. This License may not be modified without the mutual written agreement of the Licensor and You.
  6. The rights granted under, and the subject matter referenced, in this License were drafted utilizing the terminology of the Berne Convention for the Protection of Literary and Artistic Works (as amended on September 28, 1979), the Rome Convention of 1961, the WIPO Copyright Treaty of 1996, the WIPO Performances and Phonograms Treaty of 1996 and the Universal Copyright Convention (as revised on July 24, 1971). These rights and subject matter take effect in the relevant jurisdiction in which the License terms are sought to be enforced according to the corresponding provisions of the implementation of those treaty provisions in the applicable national law. If the standard suite of rights granted under applicable copyright law includes additional rights not granted under this License, such additional rights are deemed to be included in the License; this License is not intended to restrict the license of any rights under applicable law.

User Contributed Notes

There are no user contributed notes for this page.


인덱스 목록

Table of Contents

  • 함수와 메소드 목록
  • 예제 목록

User Contributed Notes

There are no user contributed notes for this page.


함수와 메소드 목록

매뉴얼의 모든 함수와 메소드 목록

a b c d e f g h i j k l m n o p q r s t u v w x y z _

  • a
    • abs - 절대값
    • acos - 역코사인
    • acosh - 역쌍곡코사인
    • addcslashes - C 형식으로 문자열을 슬래시로 인용
    • addslashes - 문자열을 슬래시로 인용
    • apache_child_terminate - 이 요청 후에 아파치 프로세스를 종료한다
    • apache_getenv - 아파치 서브프로세스의 환경 변수를 가져옵니다
    • apache_get_modules - 적재된 아파치 모듈 목록을 얻습니다
    • apache_get_version - 아파치 버전을 가져옵니다
    • apache_lookup_uri - 지정한 URI에 부분 요청을 수행하고 그에 대한 정보를 반환
    • apache_note - 아파치의 요청 노트를 얻거나 설정한다
    • apache_request_headers - 모든 HTTP 요청 헤더를 가져옵니다
    • apache_reset_timeout - 아파치 쓰기 타이머 초기화
    • apache_response_headers - 모든 HTTP 응답 헤더를 가져옵니다
    • apache_setenv - 아파치의 서브프로세스의 환경 변수를 설정합니다
    • APCIterator::current - Get current item
    • APCIterator::getTotalCount - Get total count
    • APCIterator::getTotalHits - Get total cache hits
    • APCIterator::getTotalSize - Get total cache size
    • APCIterator::key - Get iterator key
    • APCIterator::next - Move pointer to next item
    • APCIterator::rewind - Rewinds iterator
    • APCIterator::valid - Checks if current position is valid
    • APCIterator::__construct - Constructs an APCIterator iterator object
    • APCUIterator::current - Get current item
    • APCUIterator::getTotalCount - Get total count
    • APCUIterator::getTotalHits - Get total cache hits
    • APCUIterator::getTotalSize - Get total cache size
    • APCUIterator::key - Get iterator key
    • APCUIterator::next - Move pointer to next item
    • APCUIterator::rewind - Rewinds iterator
    • APCUIterator::valid - Checks if current position is valid
    • APCUIterator::__construct - Constructs an APCUIterator iterator object
    • apcu_add - Cache a new variable in the data store
    • apcu_cache_info - Retrieves cached information from APCu's data store
    • apcu_cas - Updates an old value with a new value
    • apcu_clear_cache - Clears the APCu cache
    • apcu_dec - Decrease a stored number
    • apcu_delete - Removes a stored variable from the cache
    • apcu_entry - Atomically fetch or generate a cache entry
    • apcu_exists - Checks if entry exists
    • apcu_fetch - Fetch a stored variable from the cache
    • apcu_inc - Increase a stored number
    • apcu_sma_info - Retrieves APCu Shared Memory Allocation information
    • apcu_store - Cache a variable in the data store
    • apc_add - Cache a new variable in the data store
    • apc_bin_dump - Get a binary dump of the given files and user variables
    • apc_bin_dumpfile - Output a binary dump of cached files and user variables to a file
    • apc_bin_load - Load a binary dump into the APC file/user cache
    • apc_bin_loadfile - Load a binary dump from a file into the APC file/user cache
    • apc_cache_info - Retrieves cached information from APC's data store
    • apc_cas - Updates an old value with a new value
    • apc_clear_cache - Clears the APC cache
    • apc_compile_file - Stores a file in the bytecode cache, bypassing all filters.
    • apc_dec - Decrease a stored number
    • apc_define_constants - Defines a set of constants for retrieval and mass-definition
    • apc_delete - Removes a stored variable from the cache
    • apc_delete_file - Deletes files from the opcode cache
    • apc_exists - Checks if APC key exists
    • apc_fetch - Fetch a stored variable from the cache
    • apc_inc - Increase a stored number
    • apc_load_constants - Loads a set of constants from the cache
    • apc_sma_info - Retrieves APC's Shared Memory Allocation information
    • apc_store - Cache a variable in the data store
    • apd_breakpoint - Stops the interpreter and waits on a CR from the socket
    • apd_callstack - Returns the current call stack as an array
    • apd_clunk - Throw a warning and a callstack
    • apd_continue - Restarts the interpreter
    • apd_croak - Throw an error, a callstack and then exit
    • apd_dump_function_table - Outputs the current function table
    • apd_dump_persistent_resources - Return all persistent resources as an array
    • apd_dump_regular_resources - Return all current regular resources as an array
    • apd_echo - Echo to the debugging socket
    • apd_get_active_symbols - Get an array of the current variables names in the local scope
    • apd_set_pprof_trace - Starts the session debugging
    • apd_set_session - Changes or sets the current debugging level
    • apd_set_session_trace - Starts the session debugging
    • apd_set_session_trace_socket - Starts the remote session debugging
    • AppendIterator::append - Appends an iterator
    • AppendIterator::current - Gets the current value
    • AppendIterator::getArrayIterator - Gets the ArrayIterator
    • AppendIterator::getInnerIterator - Gets the inner iterator
    • AppendIterator::getIteratorIndex - Gets an index of iterators
    • AppendIterator::key - Gets the current key
    • AppendIterator::next - Moves to the next element
    • AppendIterator::rewind - Rewinds the Iterator
    • AppendIterator::valid - Checks validity of the current element
    • AppendIterator::__construct - Constructs an AppendIterator
    • array - 배열 생성
    • ArrayAccess::offsetExists - Whether an offset exists
    • ArrayAccess::offsetGet - Offset to retrieve
    • ArrayAccess::offsetSet - Assign a value to the specified offset
    • ArrayAccess::offsetUnset - Unset an offset
    • ArrayIterator::append - Append an element
    • ArrayIterator::asort - Sort array by values
    • ArrayIterator::count - Count elements
    • ArrayIterator::current - Return current array entry
    • ArrayIterator::getArrayCopy - Get array copy
    • ArrayIterator::getFlags - Get flags
    • ArrayIterator::key - Return current array key
    • ArrayIterator::ksort - Sort array by keys
    • ArrayIterator::natcasesort - Sort an array naturally, case insensitive
    • ArrayIterator::natsort - Sort an array naturally
    • ArrayIterator::next - Move to next entry
    • ArrayIterator::offsetExists - Check if offset exists
    • ArrayIterator::offsetGet - Get value for an offset
    • ArrayIterator::offsetSet - Set value for an offset
    • ArrayIterator::offsetUnset - Unset value for an offset
    • ArrayIterator::rewind - Rewind array back to the start
    • ArrayIterator::seek - Seek to position
    • ArrayIterator::serialize - Serialize
    • ArrayIterator::setFlags - Set behaviour flags
    • ArrayIterator::uasort - User defined sort
    • ArrayIterator::uksort - User defined sort
    • ArrayIterator::unserialize - Unserialize
    • ArrayIterator::valid - Check whether array contains more entries
    • ArrayIterator::__construct - Construct an ArrayIterator
    • ArrayObject::append - Appends the value
    • ArrayObject::asort - Sort the entries by value
    • ArrayObject::count - Get the number of public properties in the ArrayObject
    • ArrayObject::exchangeArray - Exchange the array for another one.
    • ArrayObject::getArrayCopy - Creates a copy of the ArrayObject.
    • ArrayObject::getFlags - Gets the behavior flags.
    • ArrayObject::getIterator - Create a new iterator from an ArrayObject instance
    • ArrayObject::getIteratorClass - Gets the iterator classname for the ArrayObject.
    • ArrayObject::ksort - Sort the entries by key
    • ArrayObject::natcasesort - Sort an array using a case insensitive "natural order" algorithm
    • ArrayObject::natsort - Sort entries using a "natural order" algorithm
    • ArrayObject::offsetExists - Returns whether the requested index exists
    • ArrayObject::offsetGet - Returns the value at the specified index
    • ArrayObject::offsetSet - Sets the value at the specified index to newval
    • ArrayObject::offsetUnset - Unsets the value at the specified index
    • ArrayObject::serialize - Serialize an ArrayObject
    • ArrayObject::setFlags - Sets the behavior flags.
    • ArrayObject::setIteratorClass - Sets the iterator classname for the ArrayObject.
    • ArrayObject::uasort - Sort the entries with a user-defined comparison function and maintain key association
    • ArrayObject::uksort - Sort the entries by keys using a user-defined comparison function
    • ArrayObject::unserialize - Unserialize an ArrayObject
    • ArrayObject::__construct - Construct a new array object
    • array_change_key_case - 배열 안의 모든 키를 변경
    • array_chunk - 배열을 조각으로 나누기
    • array_column - Return the values from a single column in the input array
    • array_combine - 키를 위한 배열과 값을 위한 배열을 사용하여 배열을 생성
    • array_count_values - 배열 값의 수를 셉니다
    • array_diff - 배열 차이를 계산
    • array_diff_assoc - 추가적인 인덱스 확인과 함께 배열 차이를 계산
    • array_diff_key - Computes the difference of arrays using keys for comparison
    • array_diff_uassoc - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
    • array_diff_ukey - Computes the difference of arrays using a callback function on the keys for comparison
    • array_fill - 값으로 배열 채우기
    • array_fill_keys - Fill an array with values, specifying keys
    • array_filter - 콜백 함수를 사용하여 배열 원소를 필터
    • array_flip - 배열 안의 모든 키를 각 키의 연관 값과 교체
    • array_intersect - 배열의 교집합을 계산
    • array_intersect_assoc - 인덱스 검사과 함께 배열의 교집합을 계산
    • array_intersect_key - Computes the intersection of arrays using keys for comparison
    • array_intersect_uassoc - Computes the intersection of arrays with additional index check, compares indexes by a callback function
    • array_intersect_ukey - Computes the intersection of arrays using a callback function on the keys for comparison
    • array_keys - 배열의 모든 키를 반환
    • array_key_exists - 주어진 키와 인덱스가 배열에 존재하는지 확인
    • array_map - Applies the callback to the elements of the given arrays
    • array_merge - 하나 이상의 배열을 병합
    • array_merge_recursive - 두개 이상의 배열을 재귀적으로 병합
    • array_multisort - 여러 배열이나 다차원 배열 정렬
    • array_pad - 지정한 길이만큼 특정 값으로 배열 채우기
    • array_pop - 배열의 마지막 원소 빼내기
    • array_product - Calculate the product of values in an array
    • array_push - 배열의 끝에 하나 이상의 원소를 넣는다
    • array_rand - 배열에서 하나 이상의 임의 원소를 가져옴
    • array_reduce - 콜백 함수를 사용하여 배열을 반복적으로 단일 값으로 축소
    • array_replace - Replaces elements from passed arrays into the first array
    • array_replace_recursive - Replaces elements from passed arrays into the first array recursively
    • array_reverse - 원소를 역순으로 가지는 배열을 반환
    • array_search - 주어진 값으로 배열을 검색하여 성공시 해당하는 키를 반환
    • array_shift - 배열의 맨 앞에 있는 원소를 시프트
    • array_slice - 배열의 일부를 추출
    • array_splice - 배열의 일부를 삭제하고, 그 위치를 다른 내용으로 대체
    • array_sum - 배열 값들의 합을 계산
    • array_udiff - 데이터 비교 콜백함수를 사용하여 배열간의 차이를 계산
    • array_udiff_assoc - Computes the difference of arrays with additional index check, compares data by a callback function
    • array_udiff_uassoc - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
    • array_uintersect - Computes the intersection of arrays, compares data by a callback function
    • array_uintersect_assoc - Computes the intersection of arrays with additional index check, compares data by a callback function
    • array_uintersect_uassoc - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
    • array_unique - 배열에서 중복된 값을 제거
    • array_unshift - 배열의 맨 앞에 하나 이상의 원소를 첨가
    • array_values - 배열의 모든 값을 반환
    • array_walk - 배열의 각 원소에 대해서 특정 함수를 적용
    • array_walk_recursive - Apply a user function recursively to every member of an array
    • arsort - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
    • asin - 역사인
    • asinh - 역쌍곡사인
    • asort - 배열을 정렬하고 인덱스 상관 관계를 유지
    • assert - Checks if assertion is FALSE
    • assert_options - Set/get the various assert flags
    • atan - 역탄젠트
    • atan2 - 두 변수의 역탄젠트
    • atanh - 역쌍곡탄젠트
  • b
    • base64_decode - MIME base64로 인코드한 데이터를 디코드합니다
    • base64_encode - 데이터를 MIME base64로 인코드
    • basename - Returns trailing name component of path
    • base_convert - 수를 임의의 진법으로 변환
    • bbcode_add_element - Adds a bbcode element
    • bbcode_add_smiley - Adds a smiley to the parser
    • bbcode_create - Create a BBCode Resource
    • bbcode_destroy - Close BBCode_container resource
    • bbcode_parse - Parse a string following a given rule set
    • bbcode_set_arg_parser - Attach another parser in order to use another rule set for argument parsing
    • bbcode_set_flags - Set or alter parser options
    • bcadd - 두 임의 정밀도 수 더하기
    • bccomp - 두 임의 정밀도 수 비교
    • bcdiv - 두 임의 정밀도 수 나누기
    • bcmod - 임의 정밀도 수의 나머지를 구합니다
    • bcmul - 두 임의 정밀도 수 곱하기
    • bcompiler_load - Reads and creates classes from a bz compressed file
    • bcompiler_load_exe - Reads and creates classes from a bcompiler exe file
    • bcompiler_parse_class - Reads the bytecodes of a class and calls back to a user function
    • bcompiler_read - Reads and creates classes from a filehandle
    • bcompiler_write_class - Writes a defined class as bytecodes
    • bcompiler_write_constant - Writes a defined constant as bytecodes
    • bcompiler_write_exe_footer - Writes the start pos, and sig to the end of a exe type file
    • bcompiler_write_file - Writes a php source file as bytecodes
    • bcompiler_write_footer - Writes the single character \x00 to indicate End of compiled data
    • bcompiler_write_function - Writes a defined function as bytecodes
    • bcompiler_write_functions_from_file - Writes all functions defined in a file as bytecodes
    • bcompiler_write_header - Writes the bcompiler header
    • bcompiler_write_included_filename - Writes an included file as bytecodes
    • bcpow - 임의 정밀도 수 거듭제곱
    • bcpowmod - 임의 정밀도 수를 거듭제곱하고, 지정한 제수로 나머지를 구합니다
    • bcscale - 모든 bc 수학 함수의 기본 scale 인수값 설정
    • bcsqrt - 임의 정밀도 수의 제곱근을 구합니다
    • bcsub - 임의 정밀도 수 빼기
    • bin2hex - 바이너리 데이터를 16진 표현으로 변환
    • bindec - 2진수를 10진수로
    • bindtextdomain - Sets the path for a domain
    • bind_textdomain_codeset - Specify the character encoding in which the messages from the DOMAIN message catalog will be returned
    • blenc_encrypt - Encrypt a PHP script with BLENC.
    • boolval - Get the boolean value of a variable
    • bson_decode - Deserializes a BSON object into a PHP array
    • bson_encode - Serializes a PHP variable into a BSON string
    • bzclose - bzip2 파일 닫기
    • bzcompress - 문자열을 bzip2 인코드 데이터로 압축
    • bzdecompress - bzip2 인코드 데이터의 압축 해제
    • bzerrno - bzip2 오류 번호 반환
    • bzerror - bzip2 오류 번호와 오류 문자열을 배열로 반환
    • bzerrstr - bzip2 오류 문자열을 반환
    • bzflush - 버퍼에 담긴 모든 데이터를 강제 기록
    • bzopen - bzip2 압축 파일 열기
    • bzread - 바이너리 안전 bzip2 파일 읽기
    • bzwrite - 바이너리 안전 bzip2 파일 쓰기
  • c
    • CachingIterator::count - The number of elements in the iterator
    • CachingIterator::current - Return the current element
    • CachingIterator::getCache - Retrieve the contents of the cache
    • CachingIterator::getFlags - Get flags used
    • CachingIterator::getInnerIterator - Returns the inner iterator
    • CachingIterator::hasNext - Check whether the inner iterator has a valid next element
    • CachingIterator::key - Return the key for the current element
    • CachingIterator::next - Move the iterator forward
    • CachingIterator::offsetExists - The offsetExists purpose
    • CachingIterator::offsetGet - The offsetGet purpose
    • CachingIterator::offsetSet - The offsetSet purpose
    • CachingIterator::offsetUnset - The offsetUnset purpose
    • CachingIterator::rewind - Rewind the iterator
    • CachingIterator::setFlags - The setFlags purpose
    • CachingIterator::valid - Check whether the current element is valid
    • CachingIterator::__construct - Construct a new CachingIterator object for the iterator.
    • CachingIterator::__toString - Return the string representation of the current element
    • Cairo::availableFonts - Retrieves the availables font types
    • Cairo::availableSurfaces - Retrieves all available surfaces
    • Cairo::statusToString - Retrieves the current status as string
    • Cairo::version - Retrives cairo's library version
    • Cairo::versionString - Retrieves cairo version as string
    • CairoContext::appendPath - Appends a path to current path
    • CairoContext::arc - Adds a circular arc
    • CairoContext::arcNegative - Adds a negative arc
    • CairoContext::clip - Establishes a new clip region
    • CairoContext::clipExtents - Computes the area inside the current clip
    • CairoContext::clipPreserve - Establishes a new clip region from the current clip
    • CairoContext::clipRectangleList - Retrieves the current clip as a list of rectangles
    • CairoContext::closePath - Closes the current path
    • CairoContext::copyPage - Emits the current page
    • CairoContext::copyPath - Creates a copy of the current path
    • CairoContext::copyPathFlat - Gets a flattened copy of the current path
    • CairoContext::curveTo - Adds a curve
    • CairoContext::deviceToUser - Transform a coordinate
    • CairoContext::deviceToUserDistance - Transform a distance
    • CairoContext::fill - Fills the current path
    • CairoContext::fillExtents - Computes the filled area
    • CairoContext::fillPreserve - Fills and preserve the current path
    • CairoContext::fontExtents - Get the font extents
    • CairoContext::getAntialias - Retrives the current antialias mode
    • CairoContext::getCurrentPoint - The getCurrentPoint purpose
    • CairoContext::getDash - The getDash purpose
    • CairoContext::getDashCount - The getDashCount purpose
    • CairoContext::getFillRule - The getFillRule purpose
    • CairoContext::getFontFace - The getFontFace purpose
    • CairoContext::getFontMatrix - The getFontMatrix purpose
    • CairoContext::getFontOptions - The getFontOptions purpose
    • CairoContext::getGroupTarget - The getGroupTarget purpose
    • CairoContext::getLineCap - The getLineCap purpose
    • CairoContext::getLineJoin - The getLineJoin purpose
    • CairoContext::getLineWidth - The getLineWidth purpose
    • CairoContext::getMatrix - The getMatrix purpose
    • CairoContext::getMiterLimit - The getMiterLimit purpose
    • CairoContext::getOperator - The getOperator purpose
    • CairoContext::getScaledFont - The getScaledFont purpose
    • CairoContext::getSource - The getSource purpose
    • CairoContext::getTarget - The getTarget purpose
    • CairoContext::getTolerance - The getTolerance purpose
    • CairoContext::glyphPath - The glyphPath purpose
    • CairoContext::hasCurrentPoint - The hasCurrentPoint purpose
    • CairoContext::identityMatrix - The identityMatrix purpose
    • CairoContext::inFill - The inFill purpose
    • CairoContext::inStroke - The inStroke purpose
    • CairoContext::lineTo - The lineTo purpose
    • CairoContext::mask - The mask purpose
    • CairoContext::maskSurface - The maskSurface purpose
    • CairoContext::moveTo - The moveTo purpose
    • CairoContext::newPath - The newPath purpose
    • CairoContext::newSubPath - The newSubPath purpose
    • CairoContext::paint - The paint purpose
    • CairoContext::paintWithAlpha - The paintWithAlpha purpose
    • CairoContext::pathExtents - The pathExtents purpose
    • CairoContext::popGroup - The popGroup purpose
    • CairoContext::popGroupToSource - The popGroupToSource purpose
    • CairoContext::pushGroup - The pushGroup purpose
    • CairoContext::pushGroupWithContent - The pushGroupWithContent purpose
    • CairoContext::rectangle - The rectangle purpose
    • CairoContext::relCurveTo - The relCurveTo purpose
    • CairoContext::relLineTo - The relLineTo purpose
    • CairoContext::relMoveTo - The relMoveTo purpose
    • CairoContext::resetClip - The resetClip purpose
    • CairoContext::restore - The restore purpose
    • CairoContext::rotate - The rotate purpose
    • CairoContext::save - The save purpose
    • CairoContext::scale - The scale purpose
    • CairoContext::selectFontFace - The selectFontFace purpose
    • CairoContext::setAntialias - The setAntialias purpose
    • CairoContext::setDash - The setDash purpose
    • CairoContext::setFillRule - The setFillRule purpose
    • CairoContext::setFontFace - The setFontFace purpose
    • CairoContext::setFontMatrix - The setFontMatrix purpose
    • CairoContext::setFontOptions - The setFontOptions purpose
    • CairoContext::setFontSize - The setFontSize purpose
    • CairoContext::setLineCap - The setLineCap purpose
    • CairoContext::setLineJoin - The setLineJoin purpose
    • CairoContext::setLineWidth - The setLineWidth purpose
    • CairoContext::setMatrix - The setMatrix purpose
    • CairoContext::setMiterLimit - The setMiterLimit purpose
    • CairoContext::setOperator - The setOperator purpose
    • CairoContext::setScaledFont - The setScaledFont purpose
    • CairoContext::setSource - The setSource purpose
    • CairoContext::setSourceRGB - The setSourceRGB purpose
    • CairoContext::setSourceRGBA - The setSourceRGBA purpose
    • CairoContext::setSourceSurface - The setSourceSurface purpose
    • CairoContext::setTolerance - The setTolerance purpose
    • CairoContext::showPage - The showPage purpose
    • CairoContext::showText - The showText purpose
    • CairoContext::status - The status purpose
    • CairoContext::stroke - The stroke purpose
    • CairoContext::strokeExtents - The strokeExtents purpose
    • CairoContext::strokePreserve - The strokePreserve purpose
    • CairoContext::textExtents - The textExtents purpose
    • CairoContext::textPath - The textPath purpose
    • CairoContext::transform - The transform purpose
    • CairoContext::translate - The translate purpose
    • CairoContext::userToDevice - The userToDevice purpose
    • CairoContext::userToDeviceDistance - The userToDeviceDistance purpose
    • CairoContext::__construct - Creates a new CairoContext
    • CairoFontFace::getType - Retrieves the font face type
    • CairoFontFace::status - Check for CairoFontFace errors
    • CairoFontFace::__construct - Creates a new CairoFontFace object
    • CairoFontOptions::equal - The equal purpose
    • CairoFontOptions::getAntialias - The getAntialias purpose
    • CairoFontOptions::getHintMetrics - The getHintMetrics purpose
    • CairoFontOptions::getHintStyle - The getHintStyle purpose
    • CairoFontOptions::getSubpixelOrder - The getSubpixelOrder purpose
    • CairoFontOptions::hash - The hash purpose
    • CairoFontOptions::merge - The merge purpose
    • CairoFontOptions::setAntialias - The setAntialias purpose
    • CairoFontOptions::setHintMetrics - The setHintMetrics purpose
    • CairoFontOptions::setHintStyle - The setHintStyle purpose
    • CairoFontOptions::setSubpixelOrder - The setSubpixelOrder purpose
    • CairoFontOptions::status - The status purpose
    • CairoFontOptions::__construct - The __construct purpose
    • CairoFormat::strideForWidth - Provides an appropiate stride to use
    • CairoGradientPattern::addColorStopRgb - The addColorStopRgb purpose
    • CairoGradientPattern::addColorStopRgba - The addColorStopRgba purpose
    • CairoGradientPattern::getColorStopCount - The getColorStopCount purpose
    • CairoGradientPattern::getColorStopRgba - The getColorStopRgba purpose
    • CairoGradientPattern::getExtend - The getExtend purpose
    • CairoGradientPattern::setExtend - The setExtend purpose
    • CairoImageSurface::createForData - The createForData purpose
    • CairoImageSurface::createFromPng - Creates a new CairoImageSurface form a png image file
    • CairoImageSurface::getData - Gets the image data as string
    • CairoImageSurface::getFormat - Get the image format
    • CairoImageSurface::getHeight - Retrieves the height of the CairoImageSurface
    • CairoImageSurface::getStride - The getStride purpose
    • CairoImageSurface::getWidth - Retrieves the width of the CairoImageSurface
    • CairoImageSurface::__construct - Creates a new CairoImageSurface
    • CairoLinearGradient::getPoints - The getPoints purpose
    • CairoLinearGradient::__construct - The __construct purpose
    • CairoMatrix::initIdentity - Creates a new identity matrix
    • CairoMatrix::initRotate - Creates a new rotated matrix
    • CairoMatrix::initScale - Creates a new scaling matrix
    • CairoMatrix::initTranslate - Creates a new translation matrix
    • CairoMatrix::invert - The invert purpose
    • CairoMatrix::multiply - The multiply purpose
    • CairoMatrix::rotate - The rotate purpose
    • CairoMatrix::scale - Applies scaling to a matrix
    • CairoMatrix::transformDistance - The transformDistance purpose
    • CairoMatrix::transformPoint - The transformPoint purpose
    • CairoMatrix::translate - The translate purpose
    • CairoMatrix::__construct - Creates a new CairoMatrix object
    • CairoPattern::getMatrix - The getMatrix purpose
    • CairoPattern::getType - The getType purpose
    • CairoPattern::setMatrix - The setMatrix purpose
    • CairoPattern::status - The status purpose
    • CairoPattern::__construct - The __construct purpose
    • CairoPdfSurface::setSize - The setSize purpose
    • CairoPdfSurface::__construct - The __construct purpose
    • CairoPsSurface::dscBeginPageSetup - The dscBeginPageSetup purpose
    • CairoPsSurface::dscBeginSetup - The dscBeginSetup purpose
    • CairoPsSurface::dscComment - The dscComment purpose
    • CairoPsSurface::getEps - The getEps purpose
    • CairoPsSurface::getLevels - The getLevels purpose
    • CairoPsSurface::levelToString - The levelToString purpose
    • CairoPsSurface::restrictToLevel - The restrictToLevel purpose
    • CairoPsSurface::setEps - The setEps purpose
    • CairoPsSurface::setSize - The setSize purpose
    • CairoPsSurface::__construct - The __construct purpose
    • CairoRadialGradient::getCircles - The getCircles purpose
    • CairoRadialGradient::__construct - The __construct purpose
    • CairoScaledFont::extents - The extents purpose
    • CairoScaledFont::getCtm - The getCtm purpose
    • CairoScaledFont::getFontFace - The getFontFace purpose
    • CairoScaledFont::getFontMatrix - The getFontMatrix purpose
    • CairoScaledFont::getFontOptions - The getFontOptions purpose
    • CairoScaledFont::getScaleMatrix - The getScaleMatrix purpose
    • CairoScaledFont::getType - The getType purpose
    • CairoScaledFont::glyphExtents - The glyphExtents purpose
    • CairoScaledFont::status - The status purpose
    • CairoScaledFont::textExtents - The textExtents purpose
    • CairoScaledFont::__construct - The __construct purpose
    • CairoSolidPattern::getRgba - The getRgba purpose
    • CairoSolidPattern::__construct - The __construct purpose
    • CairoSurface::copyPage - The copyPage purpose
    • CairoSurface::createSimilar - The createSimilar purpose
    • CairoSurface::finish - The finish purpose
    • CairoSurface::flush - The flush purpose
    • CairoSurface::getContent - The getContent purpose
    • CairoSurface::getDeviceOffset - The getDeviceOffset purpose
    • CairoSurface::getFontOptions - The getFontOptions purpose
    • CairoSurface::getType - The getType purpose
    • CairoSurface::markDirty - The markDirty purpose
    • CairoSurface::markDirtyRectangle - The markDirtyRectangle purpose
    • CairoSurface::setDeviceOffset - The setDeviceOffset purpose
    • CairoSurface::setFallbackResolution - The setFallbackResolution purpose
    • CairoSurface::showPage - The showPage purpose
    • CairoSurface::status - The status purpose
    • CairoSurface::writeToPng - The writeToPng purpose
    • CairoSurface::__construct - The __construct purpose
    • CairoSurfacePattern::getExtend - The getExtend purpose
    • CairoSurfacePattern::getFilter - The getFilter purpose
    • CairoSurfacePattern::getSurface - The getSurface purpose
    • CairoSurfacePattern::setExtend - The setExtend purpose
    • CairoSurfacePattern::setFilter - The setFilter purpose
    • CairoSurfacePattern::__construct - The __construct purpose
    • CairoSvgSurface::getVersions - Used to retrieve a list of supported SVG versions
    • CairoSvgSurface::restrictToVersion - The restrictToVersion purpose
    • CairoSvgSurface::versionToString - The versionToString purpose
    • CairoSvgSurface::__construct - The __construct purpose
    • cairo_create - Returns a new CairoContext object on the requested surface.
    • cairo_font_face_get_type - Description
    • cairo_font_options_create - Description
    • cairo_font_options_equal - Description
    • cairo_font_options_get_antialias - Description
    • cairo_font_options_get_hint_metrics - Description
    • cairo_font_options_get_hint_style - Description
    • cairo_font_options_get_subpixel_order - Description
    • cairo_font_options_hash - Description
    • cairo_font_options_merge - Description
    • cairo_font_options_set_antialias - Description
    • cairo_font_options_set_hint_metrics - Description
    • cairo_font_options_set_hint_style - Description
    • cairo_font_options_set_subpixel_order - Description
    • cairo_font_options_status - Description
    • cairo_format_stride_for_width - Description
    • cairo_image_surface_create - Description
    • cairo_image_surface_create_for_data - Description
    • cairo_image_surface_create_from_png - Description
    • cairo_image_surface_get_data - Description
    • cairo_image_surface_get_format - Description
    • cairo_image_surface_get_height - Description
    • cairo_image_surface_get_stride - Description
    • cairo_image_surface_get_width - Description
    • cairo_matrix_create_translate - 별칭: CairoMatrix::initTranslate
    • cairo_matrix_invert - Description
    • cairo_matrix_multiply - Description
    • cairo_matrix_rotate - Description
    • cairo_matrix_transform_distance - Description
    • cairo_matrix_transform_point - Description
    • cairo_matrix_translate - Description
    • cairo_pattern_add_color_stop_rgb - Description
    • cairo_pattern_add_color_stop_rgba - Description
    • cairo_pattern_create_for_surface - Description
    • cairo_pattern_create_linear - Description
    • cairo_pattern_create_radial - Description
    • cairo_pattern_create_rgb - Description
    • cairo_pattern_create_rgba - Description
    • cairo_pattern_get_color_stop_count - Description
    • cairo_pattern_get_color_stop_rgba - Description
    • cairo_pattern_get_extend - Description
    • cairo_pattern_get_filter - Description
    • cairo_pattern_get_linear_points - Description
    • cairo_pattern_get_matrix - Description
    • cairo_pattern_get_radial_circles - Description
    • cairo_pattern_get_rgba - Description
    • cairo_pattern_get_surface - Description
    • cairo_pattern_get_type - Description
    • cairo_pattern_set_extend - Description
    • cairo_pattern_set_filter - Description
    • cairo_pattern_set_matrix - Description
    • cairo_pattern_status - Description
    • cairo_pdf_surface_create - Description
    • cairo_pdf_surface_set_size - Description
    • cairo_ps_get_levels - Description
    • cairo_ps_level_to_string - Description
    • cairo_ps_surface_create - Description
    • cairo_ps_surface_dsc_begin_page_setup - Description
    • cairo_ps_surface_dsc_begin_setup - Description
    • cairo_ps_surface_dsc_comment - Description
    • cairo_ps_surface_get_eps - Description
    • cairo_ps_surface_restrict_to_level - Description
    • cairo_ps_surface_set_eps - Description
    • cairo_ps_surface_set_size - Description
    • cairo_scaled_font_create - Description
    • cairo_scaled_font_extents - Description
    • cairo_scaled_font_get_ctm - Description
    • cairo_scaled_font_get_font_face - Description
    • cairo_scaled_font_get_font_matrix - Description
    • cairo_scaled_font_get_font_options - Description
    • cairo_scaled_font_get_scale_matrix - Description
    • cairo_scaled_font_get_type - Description
    • cairo_scaled_font_glyph_extents - Description
    • cairo_scaled_font_status - Description
    • cairo_scaled_font_text_extents - Description
    • cairo_surface_copy_page - Description
    • cairo_surface_create_similar - Description
    • cairo_surface_finish - Description
    • cairo_surface_flush - Description
    • cairo_surface_get_content - Description
    • cairo_surface_get_device_offset - Description
    • cairo_surface_get_font_options - Description
    • cairo_surface_get_type - Description
    • cairo_surface_mark_dirty - Description
    • cairo_surface_mark_dirty_rectangle - Description
    • cairo_surface_set_device_offset - Description
    • cairo_surface_set_fallback_resolution - Description
    • cairo_surface_show_page - Description
    • cairo_surface_status - Description
    • cairo_surface_write_to_png - Description
    • cairo_svg_surface_create - Description
    • cairo_svg_surface_restrict_to_version - Description
    • cairo_svg_version_to_string - Description
    • calculhmac - Obtain a hmac key (needs 2 arguments)
    • calcul_hmac - Obtain a hmac key (needs 8 arguments)
    • CallbackFilterIterator::accept - Calls the callback with the current value, the current key and the inner iterator as arguments
    • CallbackFilterIterator::__construct - Create a filtered iterator from another iterator
    • call_user_func - Call the callback given by the first parameter
    • call_user_func_array - Call a callback with an array of parameters
    • call_user_method - 특정 객체의 사용자 메쏘드를 호출 [배제됨]
    • call_user_method_array - 인수 배열로 사용자 메소드를 호출 [배제됨]
    • cal_days_in_month - Return the number of days in a month for a given year and calendar
    • cal_from_jd - Converts from Julian Day Count to a supported calendar
    • cal_info - Returns information about a particular calendar
    • cal_to_jd - Converts from a supported calendar to Julian Day Count
    • ceil - 소수점 아래 올림
    • chdb::get - Gets the value associated with a key
    • chdb::__construct - Creates a chdb instance
    • chdb_create - Creates a chdb file
    • chdir - 디렉토리 변경
    • checkdate - 그레고리력 날짜를 확인합니다
    • checkdnsrr - Check DNS records corresponding to a given Internet host name or IP address
    • chgrp - Changes file group
    • chmod - Changes file mode
    • chop - 별칭: rtrim
    • chown - Changes file owner
    • chr - 특정 문자를 반환
    • chroot - 루트 디렉토리 변경
    • chunk_split - 문자열을 작은 조각으로 나눕니다
    • classkit_import - Import new class method definitions from a file
    • classkit_method_add - Dynamically adds a new method to a given class
    • classkit_method_copy - Copies a method from class to another
    • classkit_method_redefine - Dynamically changes the code of the given method
    • classkit_method_remove - Dynamically removes the given method
    • classkit_method_rename - Dynamically changes the name of the given method
    • class_alias - Creates an alias for a class
    • class_exists - 클래스가 정의되었는지 확인
    • class_implements - Return the interfaces which are implemented by the given class or interface
    • class_parents - Return the parent classes of the given class
    • class_uses - Return the traits used by the given class
    • clearstatcache - Clears file status cache
    • cli_get_process_title - Returns the current process title
    • cli_set_process_title - Sets the process title
    • closedir - 디렉토리 핸들을 닫습니다
    • closelog - Close connection to system logger
    • Closure::bind - Duplicates a closure with a specific bound object and class scope
    • Closure::bindTo - Duplicates the closure with a new bound object and class scope
    • Closure::call - Binds and calls the closure
    • Closure::__construct - Constructor that disallows instantiation
    • Collator::asort - Sort array maintaining index association
    • Collator::compare - Compare two Unicode strings
    • Collator::create - Create a collator
    • Collator::getAttribute - Get collation attribute value
    • Collator::getErrorCode - Get collator's last error code
    • Collator::getErrorMessage - Get text for collator's last error code
    • Collator::getLocale - Get the locale name of the collator
    • Collator::getSortKey - Get sorting key for a string
    • Collator::getStrength - Get current collation strength
    • Collator::setAttribute - Set collation attribute
    • Collator::setStrength - Set collation strength
    • Collator::sort - Sort array using specified collator
    • Collator::sortWithSortKeys - Sort array using specified collator and sort keys
    • Collator::__construct - Create a collator
    • Collectable::isGarbage - Determine whether an object has been marked as garbage
    • Collectable::setGarbage - Mark an object as garbage
    • compact - 변수와 그 값을 가지는 배열 생성
    • com_create_guid - Generate a globally unique identifier (GUID)
    • com_event_sink - Connect events from a COM object to a PHP object
    • com_get_active_object - Returns a handle to an already running instance of a COM object
    • com_load_typelib - Loads a Typelib
    • com_message_pump - Process COM messages, sleeping for up to timeoutms milliseconds
    • com_print_typeinfo - Print out a PHP class definition for a dispatchable interface
    • Cond::broadcast - Broadcast a Condition
    • Cond::create - Create a Condition
    • Cond::destroy - Destroy a Condition
    • Cond::signal - Signal a Condition
    • Cond::wait - Wait for Condition
    • connection_aborted - Check whether client disconnected
    • connection_status - Returns connection status bitfield
    • constant - 상수 값을 반환합니다
    • Constants for PDO_4D - Constants for PDO_4D
    • Context parameters - Context parameter listing
    • convert_cyr_string - 키릴 문자셋을 다른 키릴 문자셋으로 변환
    • convert_uudecode - Decode a uuencoded string
    • convert_uuencode - Uuencode a string
    • copy - Copies file
    • cos - 코사인
    • cosh - 쌍곡코사인
    • count - 배열의 모든 원소나, 객체의 프로퍼티 수를 셉니다
    • Countable::count - Count elements of an object
    • Counter::bumpValue - Change the current value of a counter.
    • Counter::getMeta - Return a piece of metainformation about a counter.
    • Counter::getNamed - Retrieve an existing named counter.
    • Counter::getValue - Get the current value of a counter.
    • Counter::resetValue - Reset the current value of a counter.
    • Counter::setCounterClass - Set the class returned by Counter::getNamed.
    • Counter::__construct - Creates an instance of a Counter which maintains a single numeric value.
    • counter_bump - Update the current value of the basic counter.
    • counter_bump_value - Change the current value of a counter resource.
    • counter_create - Creates a counter which maintains a single numeric value.
    • counter_get - Get the current value of the basic counter.
    • counter_get_meta - Return a piece of metainformation about a counter resource.
    • counter_get_named - Retrieve an existing named counter as a resource.
    • counter_get_value - Get the current value of a counter resource.
    • counter_reset - Reset the current value of the basic counter.
    • counter_reset_value - Reset the current value of a counter resource.
    • count_chars - 문자열 안에 사용한 문자에 대한 정보를 반환
    • crack_check - Performs an obscure check with the given password
    • crack_closedict - Closes an open CrackLib dictionary
    • crack_getlastmessage - Returns the message from the last obscure check
    • crack_opendict - Opens a new CrackLib dictionary
    • crc32 - 문자열의 crc32 값을 계산
    • create_function - Create an anonymous (lambda-style) function
    • crypt - 단방향 문자열 암호화(해시)
    • ctype_alnum - Check for alphanumeric character(s)
    • ctype_alpha - Check for alphabetic character(s)
    • ctype_cntrl - Check for control character(s)
    • ctype_digit - Check for numeric character(s)
    • ctype_graph - Check for any printable character(s) except space
    • ctype_lower - Check for lowercase character(s)
    • ctype_print - Check for printable character(s)
    • ctype_punct - Check for any printable character which is not whitespace or an alphanumeric character
    • ctype_space - Check for whitespace character(s)
    • ctype_upper - Check for uppercase character(s)
    • ctype_xdigit - Check for character(s) representing a hexadecimal digit
    • cubrid_affected_rows - Return the number of rows affected by the last SQL statement
    • cubrid_bind - Bind variables to a prepared statement as parameters
    • cubrid_client_encoding - Return the current CUBRID connection charset
    • cubrid_close - Close CUBRID connection
    • cubrid_close_prepare - Close the request handle
    • cubrid_close_request - Close the request handle
    • cubrid_column_names - Get the column names in result
    • cubrid_column_types - Get column types in result
    • cubrid_col_get - Get contents of collection type column using OID
    • cubrid_col_size - Get the number of elements in collection type column using OID
    • cubrid_commit - Commit a transaction
    • cubrid_connect - Open a connection to a CUBRID Server
    • cubrid_connect_with_url - Establish the environment for connecting to CUBRID server
    • cubrid_current_oid - Get OID of the current cursor location
    • cubrid_data_seek - Move the internal row pointer of the CUBRID result
    • cubrid_db_name - Get db name from results of cubrid_list_dbs
    • cubrid_disconnect - Close a database connection
    • cubrid_drop - Delete an instance using OID
    • cubrid_errno - Return the numerical value of the error message from previous CUBRID operation
    • cubrid_error - Get the error message
    • cubrid_error_code - Get error code for the most recent function call
    • cubrid_error_code_facility - Get the facility code of error
    • cubrid_error_msg - Get last error message for the most recent function call
    • cubrid_execute - Execute a prepared SQL statement
    • cubrid_fetch - Fetch the next row from a result set
    • cubrid_fetch_array - Fetch a result row as an associative array, a numeric array, or both
    • cubrid_fetch_assoc - Return the associative array that corresponds to the fetched row
    • cubrid_fetch_field - Get column information from a result and return as an object
    • cubrid_fetch_lengths - Return an array with the lengths of the values of each field from the current row
    • cubrid_fetch_object - Fetche the next row and returns it as an object
    • cubrid_fetch_row - Return a numerical array with the values of the current row
    • cubrid_field_flags - Return a string with the flags of the given field offset
    • cubrid_field_len - Get the maximum length of the specified field
    • cubrid_field_name - Return the name of the specified field index
    • cubrid_field_seek - Move the result set cursor to the specified field offset
    • cubrid_field_table - Return the name of the table of the specified field
    • cubrid_field_type - Return the type of the column corresponding to the given field offset
    • cubrid_free_result - Free the memory occupied by the result data
    • cubrid_get - Get a column using OID
    • cubrid_get_autocommit - Get auto-commit mode of the connection
    • cubrid_get_charset - Return the current CUBRID connection charset
    • cubrid_get_class_name - Get the class name using OID
    • cubrid_get_client_info - Return the client library version
    • cubrid_get_db_parameter - Returns the CUBRID database parameters
    • cubrid_get_query_timeout - Get the query timeout value of the request
    • cubrid_get_server_info - Return the CUBRID server version
    • cubrid_insert_id - Return the ID generated for the last updated AUTO_INCREMENT column
    • cubrid_is_instance - Check whether the instance pointed by OID exists
    • cubrid_list_dbs - Return an array with the list of all existing CUBRID databases
    • cubrid_load_from_glo - Read data from a GLO instance and save it in a file
    • cubrid_lob2_bind - Bind a lob object or a string as a lob object to a prepared statement as parameters.
    • cubrid_lob2_close - Close LOB object.
    • cubrid_lob2_export - Export the lob object to a file.
    • cubrid_lob2_import - Import BLOB/CLOB data from a file.
    • cubrid_lob2_new - Create a lob object.
    • cubrid_lob2_read - Read from BLOB/CLOB data.
    • cubrid_lob2_seek - Move the cursor of a lob object.
    • cubrid_lob2_seek64 - Move the cursor of a lob object.
    • cubrid_lob2_size - Get a lob object's size.
    • cubrid_lob2_size64 - Get a lob object's size.
    • cubrid_lob2_tell - Tell the cursor position of the LOB object.
    • cubrid_lob2_tell64 - Tell the cursor position of the LOB object.
    • cubrid_lob2_write - Write to a lob object.
    • cubrid_lob_close - Close BLOB/CLOB data
    • cubrid_lob_export - Export BLOB/CLOB data to file
    • cubrid_lob_get - Get BLOB/CLOB data
    • cubrid_lob_send - Read BLOB/CLOB data and send straight to browser
    • cubrid_lob_size - Get BLOB/CLOB data size
    • cubrid_lock_read - Set a read lock on the given OID
    • cubrid_lock_write - Set a write lock on the given OID
    • cubrid_move_cursor - Move the cursor in the result
    • cubrid_new_glo - Create a glo instance
    • cubrid_next_result - Get result of next query when executing multiple SQL statements
    • cubrid_num_cols - Return the number of columns in the result set
    • cubrid_num_fields - Return the number of columns in the result set
    • cubrid_num_rows - Get the number of rows in the result set
    • cubrid_pconnect - Open a persistent connection to a CUBRID server
    • cubrid_pconnect_with_url - Open a persistent connection to CUBRID server
    • cubrid_ping - Ping a server connection or reconnect if there is no connection
    • cubrid_prepare - Prepare a SQL statement for execution
    • cubrid_put - Update a column using OID
    • cubrid_query - Send a CUBRID query
    • cubrid_real_escape_string - Escape special characters in a string for use in an SQL statement
    • cubrid_result - Return the value of a specific field in a specific row
    • cubrid_rollback - Roll back a transaction
    • cubrid_save_to_glo - Save requested file in a GLO instance
    • cubrid_schema - Get the requested schema information
    • cubrid_send_glo - Read data from glo and send it to std output
    • cubrid_seq_drop - Delete an element from sequence type column using OID
    • cubrid_seq_insert - Insert an element to a sequence type column using OID
    • cubrid_seq_put - Update the element value of sequence type column using OID
    • cubrid_set_add - Insert a single element to set type column using OID
    • cubrid_set_autocommit - Set autocommit mode of the connection
    • cubrid_set_db_parameter - Sets the CUBRID database parameters
    • cubrid_set_drop - Delete an element from set type column using OID
    • cubrid_set_query_timeout - Set the timeout time of query execution
    • cubrid_unbuffered_query - Perform a query without fetching the results into memory
    • cubrid_version - Get the CUBRID PHP module's version
    • CURL context options - CURL context option listing
    • curl_close - cURL 세션을 닫음
    • curl_copy_handle - Copy a cURL handle along with all of its preferences
    • curl_errno - Return the last error number
    • curl_error - Return a string containing the last error for the current session
    • curl_escape - URL encodes the given string
    • curl_exec - Perform a cURL session
    • curl_file_create - Create a CURLFile object
    • curl_getinfo - Get information regarding a specific transfer
    • curl_init - Initialize a cURL session
    • curl_multi_add_handle - Add a normal cURL handle to a cURL multi handle
    • curl_multi_close - Close a set of cURL handles
    • curl_multi_exec - Run the sub-connections of the current cURL handle
    • curl_multi_getcontent - Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
    • curl_multi_info_read - Get information about the current transfers
    • curl_multi_init - Returns a new cURL multi handle
    • curl_multi_remove_handle - Remove a multi handle from a set of cURL handles
    • curl_multi_select - Wait for activity on any curl_multi connection
    • curl_multi_setopt - Set an option for the cURL multi handle
    • curl_multi_strerror - Return string describing error code
    • curl_pause - Pause and unpause a connection
    • curl_reset - Reset all options of a libcurl session handle
    • curl_setopt - Set an option for a cURL transfer
    • curl_setopt_array - Set multiple options for a cURL transfer
    • curl_share_close - Close a cURL share handle
    • curl_share_init - Initialize a cURL share handle
    • curl_share_setopt - Set an option for a cURL share handle.
    • curl_strerror - Return string describing the given error code
    • curl_unescape - Decodes the given URL encoded string
    • curl_version - Gets cURL version information
    • current - 배열의 현재 원소를 반환
    • cyrus_authenticate - Authenticate against a Cyrus IMAP server
    • cyrus_bind - Bind callbacks to a Cyrus IMAP connection
    • cyrus_close - Close connection to a Cyrus IMAP server
    • cyrus_connect - Connect to a Cyrus IMAP server
    • cyrus_query - Send a query to a Cyrus IMAP server
    • cyrus_unbind - Unbind ...
  • d
    • data:// - Data (RFC 2397)
    • date - 로컬 날짜/시간을 형식화합니다
    • DateInterval::createFromDateString - Sets up a DateInterval from the relative parts of the string
    • DateInterval::format - Formats the interval
    • DateInterval::__construct - Creates a new DateInterval object
    • DatePeriod::getDateInterval - Gets the interval
    • DatePeriod::getEndDate - Gets the end date
    • DatePeriod::getStartDate - Gets the start date
    • DatePeriod::__construct - Creates a new DatePeriod object
    • DateTime::add - Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
    • DateTime::createFromFormat - Parses a time string according to a specified format
    • DateTime::diff - Returns the difference between two DateTime objects
    • DateTime::format - Returns date formatted according to given format
    • DateTime::getLastErrors - Returns the warnings and errors
    • DateTime::getOffset - Returns the timezone offset
    • DateTime::getTimestamp - Gets the Unix timestamp
    • DateTime::getTimezone - Return time zone relative to given DateTime
    • DateTime::modify - Alters the timestamp
    • DateTime::setDate - Sets the date
    • DateTime::setISODate - Sets the ISO date
    • DateTime::setTime - Sets the time
    • DateTime::setTimestamp - Sets the date and time based on an Unix timestamp
    • DateTime::setTimezone - Sets the time zone for the DateTime object
    • DateTime::sub - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
    • DateTime::__construct - Returns new DateTime object
    • DateTime::__set_state - The __set_state handler
    • DateTime::__wakeup - The __wakeup handler
    • DateTimeImmutable::add - Adds an amount of days, months, years, hours, minutes and seconds
    • DateTimeImmutable::createFromFormat - Returns new DateTimeImmutable object formatted according to the specified format
    • DateTimeImmutable::createFromMutable - Returns new DateTimeImmutable object encapsulating the given DateTime object
    • DateTimeImmutable::getLastErrors - Returns the warnings and errors
    • DateTimeImmutable::modify - Creates a new object with modified timestamp
    • DateTimeImmutable::setDate - Sets the date
    • DateTimeImmutable::setISODate - Sets the ISO date
    • DateTimeImmutable::setTime - Sets the time
    • DateTimeImmutable::setTimestamp - Sets the date and time based on an Unix timestamp
    • DateTimeImmutable::setTimezone - Sets the time zone
    • DateTimeImmutable::sub - Subtracts an amount of days, months, years, hours, minutes and seconds
    • DateTimeImmutable::__construct - Returns new DateTimeImmutable object
    • DateTimeImmutable::__set_state - The __set_state handler
    • DateTimeZone::getLocation - Returns location information for a timezone
    • DateTimeZone::getName - Returns the name of the timezone
    • DateTimeZone::getOffset - Returns the timezone offset from GMT
    • DateTimeZone::getTransitions - Returns all transitions for the timezone
    • DateTimeZone::listAbbreviations - Returns associative array containing dst, offset and the timezone name
    • DateTimeZone::listIdentifiers - Returns a numerically indexed array containing all defined timezone identifiers
    • DateTimeZone::__construct - Creates new DateTimeZone object
    • date_add - 별칭: DateTime::add
    • date_create - 별칭: DateTime::__construct
    • date_create_from_format - 별칭: DateTime::createFromFormat
    • date_create_immutable - 별칭: DateTimeImmutable::__construct
    • date_create_immutable_from_format - 별칭: DateTimeImmutable::createFromFormat
    • date_date_set - 별칭: DateTime::setDate
    • date_default_timezone_get - Gets the default timezone used by all date/time functions in a script
    • date_default_timezone_set - Sets the default timezone used by all date/time functions in a script
    • date_diff - 별칭: DateTime::diff
    • date_format - 별칭: DateTime::format
    • date_get_last_errors - 별칭: DateTime::getLastErrors
    • date_interval_create_from_date_string - 별칭: DateInterval::createFromDateString
    • date_interval_format - 별칭: DateInterval::format
    • date_isodate_set - 별칭: DateTime::setISODate
    • date_modify - 별칭: DateTime::modify
    • date_offset_get - 별칭: DateTime::getOffset
    • date_parse - Returns associative array with detailed info about given date
    • date_parse_from_format - Get info about given date formatted according to the specified format
    • date_sub - 별칭: DateTime::sub
    • date_sunrise - Returns time of sunrise for a given day and location
    • date_sunset - Returns time of sunset for a given day and location
    • date_sun_info - Returns an array with information about sunset/sunrise and twilight begin/end
    • date_timestamp_get - 별칭: DateTime::getTimestamp
    • date_timestamp_set - 별칭: DateTime::setTimestamp
    • date_timezone_get - 별칭: DateTime::getTimezone
    • date_timezone_set - 별칭: DateTime::setTimezone
    • date_time_set - 별칭: DateTime::setTime
    • db2_autocommit - Returns or sets the AUTOCOMMIT state for a database connection
    • db2_bind_param - Binds a PHP variable to an SQL statement parameter
    • db2_client_info - Returns an object with properties that describe the DB2 database client
    • db2_close - Closes a database connection
    • db2_columns - Returns a result set listing the columns and associated metadata for a table
    • db2_column_privileges - Returns a result set listing the columns and associated privileges for a table
    • db2_commit - Commits a transaction
    • db2_connect - Returns a connection to a database
    • db2_conn_error - Returns a string containing the SQLSTATE returned by the last connection attempt
    • db2_conn_errormsg - Returns the last connection error message and SQLCODE value
    • db2_cursor_type - Returns the cursor type used by a statement resource
    • db2_escape_string - Used to escape certain characters
    • db2_exec - Executes an SQL statement directly
    • db2_execute - Executes a prepared SQL statement
    • db2_fetch_array - Returns an array, indexed by column position, representing a row in a result set
    • db2_fetch_assoc - Returns an array, indexed by column name, representing a row in a result set
    • db2_fetch_both - Returns an array, indexed by both column name and position, representing a row in a result set
    • db2_fetch_object - Returns an object with properties representing columns in the fetched row
    • db2_fetch_row - Sets the result set pointer to the next row or requested row
    • db2_field_display_size - Returns the maximum number of bytes required to display a column
    • db2_field_name - Returns the name of the column in the result set
    • db2_field_num - Returns the position of the named column in a result set
    • db2_field_precision - Returns the precision of the indicated column in a result set
    • db2_field_scale - Returns the scale of the indicated column in a result set
    • db2_field_type - Returns the data type of the indicated column in a result set
    • db2_field_width - Returns the width of the current value of the indicated column in a result set
    • db2_foreign_keys - Returns a result set listing the foreign keys for a table
    • db2_free_result - Frees resources associated with a result set
    • db2_free_stmt - Frees resources associated with the indicated statement resource
    • db2_get_option - Retrieves an option value for a statement resource or a connection resource
    • db2_last_insert_id - Returns the auto generated ID of the last insert query that successfully executed on this connection
    • db2_lob_read - Gets a user defined size of LOB files with each invocation
    • db2_next_result - Requests the next result set from a stored procedure
    • db2_num_fields - Returns the number of fields contained in a result set
    • db2_num_rows - Returns the number of rows affected by an SQL statement
    • db2_pclose - Closes a persistent database connection
    • db2_pconnect - Returns a persistent connection to a database
    • db2_prepare - Prepares an SQL statement to be executed
    • db2_primary_keys - Returns a result set listing primary keys for a table
    • db2_procedures - Returns a result set listing the stored procedures registered in a database
    • db2_procedure_columns - Returns a result set listing stored procedure parameters
    • db2_result - Returns a single column from a row in the result set
    • db2_rollback - Rolls back a transaction
    • db2_server_info - Returns an object with properties that describe the DB2 database server
    • db2_set_option - Set options for connection or statement resources
    • db2_special_columns - Returns a result set listing the unique row identifier columns for a table
    • db2_statistics - Returns a result set listing the index and statistics for a table
    • db2_stmt_error - Returns a string containing the SQLSTATE returned by an SQL statement
    • db2_stmt_errormsg - Returns a string containing the last SQL statement error message
    • db2_tables - Returns a result set listing the tables and associated metadata in a database
    • db2_table_privileges - Returns a result set listing the tables and associated privileges in a database
    • dbase_add_record - Adds a record to a database
    • dbase_close - Closes a database
    • dbase_create - Creates a database
    • dbase_delete_record - Deletes a record from a database
    • dbase_get_header_info - Gets the header info of a database
    • dbase_get_record - Gets a record from a database as an indexed array
    • dbase_get_record_with_names - Gets a record from a database as an associative array
    • dbase_numfields - Gets the number of fields of a database
    • dbase_numrecords - Gets the number of records in a database
    • dbase_open - Opens a database
    • dbase_pack - Packs a database
    • dbase_replace_record - Replaces a record in a database
    • dba_close - Close a DBA database
    • dba_delete - Delete DBA entry specified by key
    • dba_exists - Check whether key exists
    • dba_fetch - Fetch data specified by key
    • dba_firstkey - Fetch first key
    • dba_handlers - List all the handlers available
    • dba_insert - Insert entry
    • dba_key_split - Splits a key in string representation into array representation
    • dba_list - List all open database files
    • dba_nextkey - Fetch next key
    • dba_open - Open database
    • dba_optimize - Optimize database
    • dba_popen - Open database persistently
    • dba_replace - Replace or insert entry
    • dba_sync - Synchronize database
    • dbplus_add - Add a tuple to a relation
    • dbplus_aql - Perform AQL query
    • dbplus_chdir - Get/Set database virtual current directory
    • dbplus_close - Close a relation
    • dbplus_curr - Get current tuple from relation
    • dbplus_errcode - Get error string for given errorcode or last error
    • dbplus_errno - Get error code for last operation
    • dbplus_find - Set a constraint on a relation
    • dbplus_first - Get first tuple from relation
    • dbplus_flush - Flush all changes made on a relation
    • dbplus_freealllocks - Free all locks held by this client
    • dbplus_freelock - Release write lock on tuple
    • dbplus_freerlocks - Free all tuple locks on given relation
    • dbplus_getlock - Get a write lock on a tuple
    • dbplus_getunique - Get an id number unique to a relation
    • dbplus_info - Get information about a relation
    • dbplus_last - Get last tuple from relation
    • dbplus_lockrel - Request write lock on relation
    • dbplus_next - Get next tuple from relation
    • dbplus_open - Open relation file
    • dbplus_prev - Get previous tuple from relation
    • dbplus_rchperm - Change relation permissions
    • dbplus_rcreate - Creates a new DB++ relation
    • dbplus_rcrtexact - Creates an exact but empty copy of a relation including indices
    • dbplus_rcrtlike - Creates an empty copy of a relation with default indices
    • dbplus_resolve - Resolve host information for relation
    • dbplus_restorepos - Restore position
    • dbplus_rkeys - Specify new primary key for a relation
    • dbplus_ropen - Open relation file local
    • dbplus_rquery - Perform local (raw) AQL query
    • dbplus_rrename - Rename a relation
    • dbplus_rsecindex - Create a new secondary index for a relation
    • dbplus_runlink - Remove relation from filesystem
    • dbplus_rzap - Remove all tuples from relation
    • dbplus_savepos - Save position
    • dbplus_setindex - Set index
    • dbplus_setindexbynumber - Set index by number
    • dbplus_sql - Perform SQL query
    • dbplus_tcl - Execute TCL code on server side
    • dbplus_tremove - Remove tuple and return new current tuple
    • dbplus_undo - Undo
    • dbplus_undoprepare - Prepare undo
    • dbplus_unlockrel - Give up write lock on relation
    • dbplus_unselect - Remove a constraint from relation
    • dbplus_update - Update specified tuple in relation
    • dbplus_xlockrel - Request exclusive lock on relation
    • dbplus_xunlockrel - Free exclusive lock on relation
    • dbx_close - Close an open connection/database
    • dbx_compare - Compare two rows for sorting purposes
    • dbx_connect - Open a connection/database
    • dbx_error - Report the error message of the latest function call in the module
    • dbx_escape_string - Escape a string so it can safely be used in an sql-statement
    • dbx_fetch_row - Fetches rows from a query-result that had the DBX_RESULT_UNBUFFERED flag set
    • dbx_query - Send a query and fetch all results (if any)
    • dbx_sort - Sort a result from a dbx_query by a custom sort function
    • dcgettext - Overrides the domain for a single lookup
    • dcngettext - Plural version of dcgettext
    • debug_backtrace - Generates a backtrace
    • debug_print_backtrace - Prints a backtrace
    • debug_zval_dump - Dumps a string representation of an internal zend value to output
    • decbin - 10진수를 2진수로
    • dechex - 10진수를 16진수로
    • decoct - 10진수를 8진수로
    • define - Defines a named constant
    • defined - Checks whether a given named constant exists
    • define_syslog_variables - Initializes all syslog related variables
    • deflate_add - Incrementally deflate data
    • deflate_init - Initialize an incremental deflate context
    • deg2rad - 각도를 라디안으로 변환
    • delete - See unlink or unset
    • dgettext - Override the current domain
    • die - Equivalent to exit
    • dio_close - Closes the file descriptor given by fd
    • dio_fcntl - Performs a c library fcntl on fd
    • dio_open - Opens a file (creating it if necessary) at a lower level than the C library input/ouput stream functions allow.
    • dio_read - Reads bytes from a file descriptor
    • dio_seek - Seeks to pos on fd from whence
    • dio_stat - Gets stat information about the file descriptor fd
    • dio_tcsetattr - Sets terminal attributes and baud rate for a serial port
    • dio_truncate - Truncates file descriptor fd to offset bytes
    • dio_write - Writes data to fd with optional truncation at length
    • dir - Return an instance of the Directory class
    • DirectoryIterator::current - Return the current DirectoryIterator item.
    • DirectoryIterator::getATime - Get last access time of the current DirectoryIterator item
    • DirectoryIterator::getBasename - Get base name of current DirectoryIterator item.
    • DirectoryIterator::getCTime - Get inode change time of the current DirectoryIterator item
    • DirectoryIterator::getExtension - Gets the file extension
    • DirectoryIterator::getFilename - Return file name of current DirectoryIterator item.
    • DirectoryIterator::getGroup - Get group for the current DirectoryIterator item
    • DirectoryIterator::getInode - Get inode for the current DirectoryIterator item
    • DirectoryIterator::getMTime - Get last modification time of current DirectoryIterator item
    • DirectoryIterator::getOwner - Get owner of current DirectoryIterator item
    • DirectoryIterator::getPath - Get path of current Iterator item without filename
    • DirectoryIterator::getPathname - Return path and file name of current DirectoryIterator item
    • DirectoryIterator::getPerms - Get the permissions of current DirectoryIterator item
    • DirectoryIterator::getSize - Get size of current DirectoryIterator item
    • DirectoryIterator::getType - Determine the type of the current DirectoryIterator item
    • DirectoryIterator::isDir - Determine if current DirectoryIterator item is a directory
    • DirectoryIterator::isDot - Determine if current DirectoryIterator item is '.' or '..'
    • DirectoryIterator::isExecutable - Determine if current DirectoryIterator item is executable
    • DirectoryIterator::isFile - Determine if current DirectoryIterator item is a regular file
    • DirectoryIterator::isLink - Determine if current DirectoryIterator item is a symbolic link
    • DirectoryIterator::isReadable - Determine if current DirectoryIterator item can be read
    • DirectoryIterator::isWritable - Determine if current DirectoryIterator item can be written to
    • DirectoryIterator::key - Return the key for the current DirectoryIterator item
    • DirectoryIterator::next - Move forward to next DirectoryIterator item
    • DirectoryIterator::rewind - Rewind the DirectoryIterator back to the start
    • DirectoryIterator::seek - Seek to a DirectoryIterator item
    • DirectoryIterator::valid - Check whether current DirectoryIterator position is a valid file
    • DirectoryIterator::__construct - Constructs a new directory iterator from a path
    • DirectoryIterator::__toString - Get file name as a string
    • dirname - Returns a parent directory's path
    • diskfreespace - 별칭: disk_free_space
    • disk_free_space - Returns available space on filesystem or disk partition
    • disk_total_space - Returns the total size of a filesystem or disk partition
    • dl - Loads a PHP extension at runtime
    • dngettext - Plural version of dgettext
    • dns_check_record - 별칭: checkdnsrr
    • dns_get_mx - 별칭: getmxrr
    • dns_get_record - Fetch DNS Resource Records associated with a hostname
    • DOMAttr::isId - Checks if attribute is a defined ID
    • DOMAttr::__construct - Creates a new DOMAttr object
    • DOMCdataSection::__construct - Constructs a new DOMCdataSection object
    • DOMCharacterData::appendData - Append the string to the end of the character data of the node
    • DOMCharacterData::deleteData - Remove a range of characters from the node
    • DOMCharacterData::insertData - Insert a string at the specified 16-bit unit offset
    • DOMCharacterData::replaceData - Replace a substring within the DOMCharacterData node
    • DOMCharacterData::substringData - Extracts a range of data from the node
    • DOMComment::__construct - Creates a new DOMComment object
    • DOMDocument::createAttribute - Create new attribute
    • DOMDocument::createAttributeNS - Create new attribute node with an associated namespace
    • DOMDocument::createCDATASection - Create new cdata node
    • DOMDocument::createComment - Create new comment node
    • DOMDocument::createDocumentFragment - Create new document fragment
    • DOMDocument::createElement - Create new element node
    • DOMDocument::createElementNS - Create new element node with an associated namespace
    • DOMDocument::createEntityReference - Create new entity reference node
    • DOMDocument::createProcessingInstruction - Creates new PI node
    • DOMDocument::createTextNode - Create new text node
    • DOMDocument::getElementById - Searches for an element with a certain id
    • DOMDocument::getElementsByTagName - Searches for all elements with given local tag name
    • DOMDocument::getElementsByTagNameNS - Searches for all elements with given tag name in specified namespace
    • DOMDocument::importNode - Import node into current document
    • DOMDocument::load - Load XML from a file
    • DOMDocument::loadHTML - Load HTML from a string
    • DOMDocument::loadHTMLFile - Load HTML from a file
    • DOMDocument::loadXML - Load XML from a string
    • DOMDocument::normalizeDocument - Normalizes the document
    • DOMDocument::registerNodeClass - Register extended class used to create base node type
    • DOMDocument::relaxNGValidate - Performs relaxNG validation on the document
    • DOMDocument::relaxNGValidateSource - Performs relaxNG validation on the document
    • DOMDocument::save - Dumps the internal XML tree back into a file
    • DOMDocument::saveHTML - Dumps the internal document into a string using HTML formatting
    • DOMDocument::saveHTMLFile - Dumps the internal document into a file using HTML formatting
    • DOMDocument::saveXML - Dumps the internal XML tree back into a string
    • DOMDocument::schemaValidate - Validates a document based on a schema
    • DOMDocument::schemaValidateSource - Validates a document based on a schema
    • DOMDocument::validate - Validates the document based on its DTD
    • DOMDocument::xinclude - Substitutes XIncludes in a DOMDocument Object
    • DOMDocument::__construct - Creates a new DOMDocument object
    • DOMDocumentFragment::appendXML - Append raw XML data
    • DOMElement::getAttribute - Returns value of attribute
    • DOMElement::getAttributeNode - Returns attribute node
    • DOMElement::getAttributeNodeNS - Returns attribute node
    • DOMElement::getAttributeNS - Returns value of attribute
    • DOMElement::getElementsByTagName - Gets elements by tagname
    • DOMElement::getElementsByTagNameNS - Get elements by namespaceURI and localName
    • DOMElement::hasAttribute - Checks to see if attribute exists
    • DOMElement::hasAttributeNS - Checks to see if attribute exists
    • DOMElement::removeAttribute - Removes attribute
    • DOMElement::removeAttributeNode - Removes attribute
    • DOMElement::removeAttributeNS - Removes attribute
    • DOMElement::setAttribute - Adds new attribute
    • DOMElement::setAttributeNode - Adds new attribute node to element
    • DOMElement::setAttributeNodeNS - Adds new attribute node to element
    • DOMElement::setAttributeNS - Adds new attribute
    • DOMElement::setIdAttribute - Declares the attribute specified by name to be of type ID
    • DOMElement::setIdAttributeNode - Declares the attribute specified by node to be of type ID
    • DOMElement::setIdAttributeNS - Declares the attribute specified by local name and namespace URI to be of type ID
    • DOMElement::__construct - Creates a new DOMElement object
    • DOMEntityReference::__construct - Creates a new DOMEntityReference object
    • DOMImplementation::createDocument - Creates a DOMDocument object of the specified type with its document element
    • DOMImplementation::createDocumentType - Creates an empty DOMDocumentType object
    • DOMImplementation::hasFeature - Test if the DOM implementation implements a specific feature
    • DOMImplementation::__construct - Creates a new DOMImplementation object
    • DOMNamedNodeMap::getNamedItem - Retrieves a node specified by name
    • DOMNamedNodeMap::getNamedItemNS - Retrieves a node specified by local name and namespace URI
    • DOMNamedNodeMap::item - Retrieves a node specified by index
    • DOMNode::appendChild - Adds new child at the end of the children
    • DOMNode::C14N - Canonicalize nodes to a string
    • DOMNode::C14NFile - Canonicalize nodes to a file
    • DOMNode::cloneNode - Clones a node
    • DOMNode::getLineNo - Get line number for a node
    • DOMNode::getNodePath - Get an XPath for a node
    • DOMNode::hasAttributes - Checks if node has attributes
    • DOMNode::hasChildNodes - Checks if node has children
    • DOMNode::insertBefore - Adds a new child before a reference node
    • DOMNode::isDefaultNamespace - Checks if the specified namespaceURI is the default namespace or not
    • DOMNode::isSameNode - Indicates if two nodes are the same node
    • DOMNode::isSupported - Checks if feature is supported for specified version
    • DOMNode::lookupNamespaceURI - Gets the namespace URI of the node based on the prefix
    • DOMNode::lookupPrefix - Gets the namespace prefix of the node based on the namespace URI
    • DOMNode::normalize - Normalizes the node
    • DOMNode::removeChild - Removes child from list of children
    • DOMNode::replaceChild - Replaces a child
    • DOMNodelist::item - Retrieves a node specified by index
    • DOMProcessingInstruction::__construct - Creates a new DOMProcessingInstruction object
    • DOMText::isWhitespaceInElementContent - Indicates whether this text node contains whitespace
    • DOMText::splitText - Breaks this node into two nodes at the specified offset
    • DOMText::__construct - Creates a new DOMText object
    • DOMXPath::evaluate - Evaluates the given XPath expression and returns a typed result if possible
    • DOMXPath::query - Evaluates the given XPath expression
    • DOMXPath::registerNamespace - Registers the namespace with the DOMXPath object
    • DOMXPath::registerPhpFunctions - Register PHP functions as XPath functions
    • DOMXPath::__construct - Creates a new DOMXPath object
    • dom_import_simplexml - Gets a DOMElement object from a SimpleXMLElement object
    • doubleval - 별칭: floatval
    • Ds\Collection::clear - Removes all values.
    • Ds\Collection::copy - Returns a shallow copy of the collection.
    • Ds\Collection::isEmpty - Returns whether the collection is empty
    • Ds\Collection::toArray - Converts the collection to an array.
    • Ds\Deque::allocate - Allocates enough memory for a required capacity.
    • Ds\Deque::apply - Updates all values by applying a callback function to each value.
    • Ds\Deque::capacity - Returns the current capacity.
    • Ds\Deque::clear - Removes all values from the deque.
    • Ds\Deque::contains - Determines if the deque contains given values.
    • Ds\Deque::copy - Returns a shallow copy of the deque.
    • Ds\Deque::count - Returns the number of values in the collection.
    • Ds\Deque::filter - Creates a new deque using a callable to determine which values to include.
    • Ds\Deque::find - Attempts to find a value's index.
    • Ds\Deque::first - Returns the first value in the deque.
    • Ds\Deque::get - Returns the value at a given index.
    • Ds\Deque::insert - Inserts values at a given index.
    • Ds\Deque::isEmpty - Returns whether the deque is empty
    • Ds\Deque::join - Joins all values together as a string.
    • Ds\Deque::jsonSerialize - Returns a representation that can be converted to JSON.
    • Ds\Deque::last - Returns the last value.
    • Ds\Deque::map - Returns the result of applying a callback to each value.
    • Ds\Deque::merge - Returns the result of adding all given values to the deque.
    • Ds\Deque::pop - Removes and returns the last value.
    • Ds\Deque::push - Adds values to the end of the deque.
    • Ds\Deque::reduce - Reduces the deque to a single value using a callback function.
    • Ds\Deque::remove - Removes and returns a value by index.
    • Ds\Deque::reverse - Reverses the deque in-place.
    • Ds\Deque::reversed - Returns a reversed copy.
    • Ds\Deque::rotate - Rotates the deque by a given number of rotations.
    • Ds\Deque::set - Updates a value at a given index.
    • Ds\Deque::shift - Removes and returns the first value.
    • Ds\Deque::slice - Returns a sub-deque of a given range.
    • Ds\Deque::sort - Sorts the deque in-place.
    • Ds\Deque::sorted - Returns a sorted copy.
    • Ds\Deque::sum - Returns the sum of all values in the deque.
    • Ds\Deque::toArray - Converts the deque to an array.
    • Ds\Deque::unshift - Adds values to the front of the deque.
    • Ds\Deque::__construct - Creates a new instance.
    • Ds\Hashable::equals - Determines whether an object is equal to the current instance.
    • Ds\Hashable::hash - Returns a scalar value to be used as a hash value.
    • Ds\Map::allocate - Allocates enough memory for a required capacity.
    • Ds\Map::apply - Updates all values by applying a callback function to each value.
    • Ds\Map::capacity - Returns the current capacity.
    • Ds\Map::clear - Removes all values.
    • Ds\Map::copy - Returns a shallow copy of the map.
    • Ds\Map::count - Returns the number of values in the map.
    • Ds\Map::diff - Creates a new map using keys that aren't in another map.
    • Ds\Map::filter - Creates a new map using a callable to determine which pairs to include.
    • Ds\Map::first - Returns the first pair in the map.
    • Ds\Map::get - Returns the value for a given key.
    • Ds\Map::hasKey - Determines whether the map contains a given key.
    • Ds\Map::hasValue - Determines whether the map contains a given value.
    • Ds\Map::intersect - Creates a new map by intersecting keys with another map.
    • Ds\Map::isEmpty - Returns whether the map is empty
    • Ds\Map::jsonSerialize - Returns a representation that can be converted to JSON.
    • Ds\Map::keys - Returns a set of the map's keys.
    • Ds\Map::ksort - Sorts the map in-place by key.
    • Ds\Map::ksorted - Returns a copy, sorted by key.
    • Ds\Map::last - Returns the last pair of the map.
    • Ds\Map::map - Returns the result of applying a callback to each value.
    • Ds\Map::merge - Returns the result of adding all given associations.
    • Ds\Map::pairs - Returns a sequence containing all the pairs of the map.
    • Ds\Map::put - Associates a key with a value.
    • Ds\Map::putAll - Associates all key-value pairs of a traversable object or array.
    • Ds\Map::reduce - Reduces the map to a single value using a callback function.
    • Ds\Map::remove - Removes and returns a value by key.
    • Ds\Map::reverse - Reverses the map in-place.
    • Ds\Map::reversed - Returns a reversed copy.
    • Ds\Map::skip - Returns the pair at a given positional index.
    • Ds\Map::slice - Returns a subset of the map defined by a starting index and length.
    • Ds\Map::sort - Sorts the map in-place by value.
    • Ds\Map::sorted - Returns a copy, sorted by value.
    • Ds\Map::sum - Returns the sum of all values in the map.
    • Ds\Map::toArray - Converts the map to an array.
    • Ds\Map::union - Creates a new map using values from the current instance and another map.
    • Ds\Map::values - Returns a sequence of the map's values.
    • Ds\Map::xor - Creates a new map using keys of either the current instance or of another map, but not of both.
    • Ds\Map::__construct - Creates a new instance.
    • Ds\Pair::clear - Removes all values.
    • Ds\Pair::copy - Returns a shallow copy of the pair.
    • Ds\Pair::isEmpty - Returns whether the pair is empty
    • Ds\Pair::jsonSerialize - Returns a representation that can be converted to JSON.
    • Ds\Pair::toArray - Converts the pair to an array.
    • Ds\Pair::__construct - Creates a new instance.
    • Ds\PriorityQueue::allocate - Allocates enough memory for a required capacity.
    • Ds\PriorityQueue::capacity - Returns the current capacity.
    • Ds\PriorityQueue::clear - Removes all values.
    • Ds\PriorityQueue::copy - Returns a shallow copy of the queue.
    • Ds\PriorityQueue::count - Returns the number of values in the queue.
    • Ds\PriorityQueue::isEmpty - Returns whether the queue is empty
    • Ds\PriorityQueue::jsonSerialize - Returns a representation that can be converted to JSON.
    • Ds\PriorityQueue::peek - Returns the value at the front of the queue.
    • Ds\PriorityQueue::pop - Removes and returns the value with the highest priority.
    • Ds\PriorityQueue::push - Pushes values into the queue.
    • Ds\PriorityQueue::toArray - Converts the queue to an array.
    • Ds\PriorityQueue::__construct - Creates a new instance.
    • Ds\Queue::allocate - Allocates enough memory for a required capacity.
    • Ds\Queue::capacity - Returns the current capacity.
    • Ds\Queue::clear - Removes all values.
    • Ds\Queue::copy - Returns a shallow copy of the queue.
    • Ds\Queue::count - Returns the number of values in the queue.
    • Ds\Queue::isEmpty - Returns whether the queue is empty
    • Ds\Queue::jsonSerialize - Returns a representation that can be converted to JSON.
    • Ds\Queue::peek - Returns the value at the front of the queue.
    • Ds\Queue::pop - Removes and returns the value at the front of the queue.
    • Ds\Queue::push - Pushes values into the queue.
    • Ds\Queue::toArray - Converts the queue to an array.
    • Ds\Queue::__construct - Creates a new instance.
    • Ds\Sequence::allocate - Allocates enough memory for a required capacity.
    • Ds\Sequence::apply - Updates all values by applying a callback function to each value.
    • Ds\Sequence::capacity - Returns the current capacity.
    • Ds\Sequence::contains - Determines if the sequence contains given values.
    • Ds\Sequence::filter - Creates a new sequence using a callable to determine which values to include.
    • Ds\Sequence::find - Attempts to find a value's index.
    • Ds\Sequence::first - Returns the first value in the sequence.
    • Ds\Sequence::get - Returns the value at a given index.
    • Ds\Sequence::insert - Inserts values at a given index.
    • Ds\Sequence::join - Joins all values together as a string.
    • Ds\Sequence::last - Returns the last value.
    • Ds\Sequence::map - Returns the result of applying a callback to each value.
    • Ds\Sequence::merge - Returns the result of adding all given values to the sequence.
    • Ds\Sequence::pop - Removes and returns the last value.
    • Ds\Sequence::push - Adds values to the end of the sequence.
    • Ds\Sequence::reduce - Reduces the sequence to a single value using a callback function.
    • Ds\Sequence::remove - Removes and returns a value by index.
    • Ds\Sequence::reverse - Reverses the sequence in-place.
    • Ds\Sequence::reversed - Returns a reversed copy.
    • Ds\Sequence::rotate - Rotates the sequence by a given number of rotations.
    • Ds\Sequence::set - Updates a value at a given index.
    • Ds\Sequence::shift - Removes and returns the first value.
    • Ds\Sequence::slice - Returns a sub-sequence of a given range.
    • Ds\Sequence::sort - Sorts the sequence in-place.
    • Ds\Sequence::sorted - Returns a sorted copy.
    • Ds\Sequence::sum - Returns the sum of all values in the sequence.
    • Ds\Sequence::unshift - Adds values to the front of the sequence.
    • Ds\Set::add - Adds values to the set.
    • Ds\Set::allocate - Allocates enough memory for a required capacity.
    • Ds\Set::capacity - Returns the current capacity.
    • Ds\Set::clear - Removes all values.
    • Ds\Set::contains - Determines if the set contains all values.
    • Ds\Set::copy - Returns a shallow copy of the set.
    • Ds\Set::count - Returns the number of values in the set.
    • Ds\Set::diff - Creates a new set using values that aren't in another set.
    • Ds\Set::filter - Creates a new set using a callable to determine which values to include.
    • Ds\Set::first - Returns the first value in the set.
    • Ds\Set::get - Returns the value at a given index.
    • Ds\Set::intersect - Creates a new set by intersecting values with another set.
    • Ds\Set::isEmpty - Returns whether the set is empty
    • Ds\Set::join - Joins all values together as a string.
    • Ds\Set::jsonSerialize - Returns a representation that can be converted to JSON.
    • Ds\Set::last - Returns the last value in the set.
    • Ds\Set::merge - Returns the result of adding all given values to the set.
    • Ds\Set::reduce - Reduces the set to a single value using a callback function.
    • Ds\Set::remove - Removes all given values from the set.
    • Ds\Set::reverse - Reverses the set in-place.
    • Ds\Set::reversed - Returns a reversed copy.
    • Ds\Set::slice - Returns a sub-set of a given range.
    • Ds\Set::sort - Sorts the set in-place.
    • Ds\Set::sorted - Returns a sorted copy.
    • Ds\Set::sum - Returns the sum of all values in the set.
    • Ds\Set::toArray - Converts the set to an array.
    • Ds\Set::union - Creates a new set using values from the current instance and another set.
    • Ds\Set::xor - Creates a new set using values in either the current instance or in another set, but not in both.
    • Ds\Set::__construct - Creates a new instance.
    • Ds\Stack::allocate - Allocates enough memory for a required capacity.
    • Ds\Stack::capacity - Returns the current capacity.
    • Ds\Stack::clear - Removes all values.
    • Ds\Stack::copy - Returns a shallow copy of the stack.
    • Ds\Stack::count - Returns the number of values in the stack.
    • Ds\Stack::isEmpty - Returns whether the stack is empty
    • Ds\Stack::jsonSerialize - Returns a representation that can be converted to JSON.
    • Ds\Stack::peek - Returns the value at the top of the stack.
    • Ds\Stack::pop - Removes and returns the value at the top of the stack.
    • Ds\Stack::push - Pushes values onto the stack.
    • Ds\Stack::toArray - Converts the stack to an array.
    • Ds\Stack::__construct - Creates a new instance.
    • Ds\Vector::allocate - Allocates enough memory for a required capacity.
    • Ds\Vector::apply - Updates all values by applying a callback function to each value.
    • Ds\Vector::capacity - Returns the current capacity.
    • Ds\Vector::clear - Removes all values.
    • Ds\Vector::contains - Determines if the vector contains given values.
    • Ds\Vector::copy - Returns a shallow copy of the vector.
    • Ds\Vector::count - Returns the number of values in the collection.
    • Ds\Vector::filter - Creates a new vector using a callable to determine which values to include.
    • Ds\Vector::find - Attempts to find a value's index.
    • Ds\Vector::first - Returns the first value in the vector.
    • Ds\Vector::get - Returns the value at a given index.
    • Ds\Vector::insert - Inserts values at a given index.
    • Ds\Vector::isEmpty - Returns whether the vector is empty
    • Ds\Vector::join - Joins all values together as a string.
    • Ds\Vector::jsonSerialize - Returns a representation that can be converted to JSON.
    • Ds\Vector::last - Returns the last value.
    • Ds\Vector::map - Returns the result of applying a callback to each value.
    • Ds\Vector::merge - Returns the result of adding all given values to the vector.
    • Ds\Vector::pop - Removes and returns the last value.
    • Ds\Vector::push - Adds values to the end of the vector.
    • Ds\Vector::reduce - Reduces the vector to a single value using a callback function.
    • Ds\Vector::remove - Removes and returns a value by index.
    • Ds\Vector::reverse - Reverses the vector in-place.
    • Ds\Vector::reversed - Returns a reversed copy.
    • Ds\Vector::rotate - Rotates the vector by a given number of rotations.
    • Ds\Vector::set - Updates a value at a given index.
    • Ds\Vector::shift - Removes and returns the first value.
    • Ds\Vector::slice - Returns a sub-vector of a given range.
    • Ds\Vector::sort - Sorts the vector in-place.
    • Ds\Vector::sorted - Returns a sorted copy.
    • Ds\Vector::sum - Returns the sum of all values in the vector.
    • Ds\Vector::toArray - Converts the vector to an array.
    • Ds\Vector::unshift - Adds values to the front of the vector.
    • Ds\Vector::__construct - Creates a new instance.
  • e
    • each - 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진
    • easter_date - Get Unix timestamp for midnight on Easter of a given year
    • easter_days - Get number of days after March 21 on which Easter falls for a given year
    • echo - 하나 이상의 문자열을 출력
    • eio_busy - Artificially increase load. Could be useful in tests, benchmarking.
    • eio_cancel - Cancels a request
    • eio_chmod - Change file/direcrory permissions.
    • eio_chown - Change file/direcrory permissions.
    • eio_close - Close file
    • eio_custom - Execute custom request like any other eio_* call.
    • eio_dup2 - Duplicate a file descriptor
    • eio_event_loop - Polls libeio until all requests proceeded
    • eio_fallocate - Allows the caller to directly manipulate the allocated disk space for a file
    • eio_fchmod - Change file permissions.
    • eio_fchown - Change file ownership
    • eio_fdatasync - Synchronize a file's in-core state with storage device.
    • eio_fstat - Get file status
    • eio_fstatvfs - Get file system statistics
    • eio_fsync - Synchronize a file's in-core state with storage device
    • eio_ftruncate - Truncate a file
    • eio_futime - Change file last access and modification times
    • eio_get_event_stream - Get stream representing a variable used in internal communications with libeio.
    • eio_get_last_error - Returns string describing the last error associated with a request resource
    • eio_grp - Creates a request group.
    • eio_grp_add - Adds a request to the request group.
    • eio_grp_cancel - Cancels a request group
    • eio_grp_limit - Set group limit
    • eio_init - (Re-)initialize Eio
    • eio_link - Create a hardlink for file
    • eio_lstat - Get file status
    • eio_mkdir - Create directory
    • eio_mknod - Create a special or ordinary file.
    • eio_nop - Does nothing, except go through the whole request cycle.
    • eio_npending - Returns number of finished, but unhandled requests
    • eio_nready - Returns number of not-yet handled requests
    • eio_nreqs - Returns number of requests to be processed
    • eio_nthreads - Returns number of threads currently in use
    • eio_open - Opens a file
    • eio_poll - Can be to be called whenever there are pending requests that need finishing.
    • eio_read - Read from a file descriptor at given offset.
    • eio_readahead - Perform file readahead into page cache
    • eio_readdir - Reads through a whole directory
    • eio_readlink - Read value of a symbolic link.
    • eio_realpath - Get the canonicalized absolute pathname.
    • eio_rename - Change the name or location of a file.
    • eio_rmdir - Remove a directory
    • eio_seek - Repositions the offset of the open file associated with the fd argument to the argument offset according to the directive whence
    • eio_sendfile - Transfer data between file descriptors
    • eio_set_max_idle - Set maximum number of idle threads.
    • eio_set_max_parallel - Set maximum parallel threads
    • eio_set_max_poll_reqs - Set maximum number of requests processed in a poll.
    • eio_set_max_poll_time - Set maximum poll time
    • eio_set_min_parallel - Set minimum parallel thread number
    • eio_stat - Get file status
    • eio_statvfs - Get file system statistics
    • eio_symlink - Create a symbolic link
    • eio_sync - Commit buffer cache to disk
    • eio_syncfs - Calls Linux' syncfs syscall, if available
    • eio_sync_file_range - Sync a file segment with disk
    • eio_truncate - Truncate a file
    • eio_unlink - Delete a name and possibly the file it refers to
    • eio_utime - Change file last access and modification times.
    • eio_write - Write to file
    • empty - 변수가 비어있는지 검사합니다
    • EmptyIterator::current - The current() method
    • EmptyIterator::key - The key() method
    • EmptyIterator::next - The next() method
    • EmptyIterator::rewind - The rewind() method
    • EmptyIterator::valid - The valid() method
    • enchant_broker_describe - Enumerates the Enchant providers
    • enchant_broker_dict_exists - Whether a dictionary exists or not. Using non-empty tag
    • enchant_broker_free - Free the broker resource and its dictionnaries
    • enchant_broker_free_dict - Free a dictionary resource
    • enchant_broker_get_dict_path - Get the directory path for a given backend
    • enchant_broker_get_error - Returns the last error of the broker
    • enchant_broker_init - create a new broker object capable of requesting
    • enchant_broker_list_dicts - Returns a list of available dictionaries
    • enchant_broker_request_dict - create a new dictionary using a tag
    • enchant_broker_request_pwl_dict - creates a dictionary using a PWL file
    • enchant_broker_set_dict_path - Set the directory path for a given backend
    • enchant_broker_set_ordering - Declares a preference of dictionaries to use for the language
    • enchant_dict_add_to_personal - add a word to personal word list
    • enchant_dict_add_to_session - add 'word' to this spell-checking session
    • enchant_dict_check - Check whether a word is correctly spelled or not
    • enchant_dict_describe - Describes an individual dictionary
    • enchant_dict_get_error - Returns the last error of the current spelling-session
    • enchant_dict_is_in_session - whether or not 'word' exists in this spelling-session
    • enchant_dict_quick_check - Check the word is correctly spelled and provide suggestions
    • enchant_dict_store_replacement - Add a correction for a word
    • enchant_dict_suggest - Will return a list of values if any of those pre-conditions are not met
    • end - 배열 내부 포인터가 마지막 원소를 가리키게 설정
    • ereg - Regular expression match
    • eregi - Case insensitive regular expression match
    • eregi_replace - Replace regular expression case insensitive
    • ereg_replace - Replace regular expression
    • ErrorException::getSeverity - Gets the exception severity
    • ErrorException::__construct - Constructs the exception
    • error_clear_last - Clear the most recent error
    • error_get_last - Get the last occurred error
    • error_log - Send an error message to the defined error handling routines
    • error_reporting - Sets which PHP errors are reported
    • escapeshellarg - Escape a string to be used as a shell argument
    • escapeshellcmd - Escape shell metacharacters
    • Ev::backend - Returns an integer describing the backend used by libev.
    • Ev::depth - Returns recursion depth
    • Ev::embeddableBackends - Returns the set of backends that are embeddable in other event loops.
    • Ev::feedSignal - Feed a signal event info Ev
    • Ev::feedSignalEvent - Feed signal event into the default loop
    • Ev::iteration - Return the number of times the default event loop has polled for new events.
    • Ev::now - Returns the time when the last iteration of the default event loop has started.
    • Ev::nowUpdate - Establishes the current time by querying the kernel, updating the time returned by Ev::now in the progress.
    • Ev::recommendedBackends - Returns a bit mask of recommended backends for current platform.
    • Ev::resume - Resume previously suspended default event loop
    • Ev::run - Begin checking for events and calling callbacks for the default loop
    • Ev::sleep - Block the process for the given number of seconds.
    • Ev::stop - Stops the default event loop
    • Ev::supportedBackends - Returns the set of backends supported by current libev configuration.
    • Ev::suspend - Suspend the default event loop
    • Ev::time - Returns the current time in fractional seconds since the epoch.
    • Ev::verify - Performs internal consistency checks(for debugging)
    • eval - Evaluate a string as PHP code
    • EvCheck::createStopped - Create instance of a stopped EvCheck watcher
    • EvCheck::__construct - Constructs the EvCheck watcher object
    • EvChild::createStopped - Create instance of a stopped EvCheck watcher
    • EvChild::set - Configures the watcher
    • EvChild::__construct - Constructs the EvChild watcher object
    • EvEmbed::createStopped - Create stopped EvEmbed watcher object
    • EvEmbed::set - Configures the watcher
    • EvEmbed::sweep - Make a single, non-blocking sweep over the embedded loop.
    • EvEmbed::__construct - Constructs the EvEmbed object
    • Event::add - Makes event pending
    • Event::addSignal - Makes signal event pending
    • Event::addTimer - Makes timer event pending
    • Event::del - Makes event non-pending
    • Event::delSignal - Makes signal event non-pending
    • Event::delTimer - Makes timer event non-pending
    • Event::free - Make event non-pending and free resources allocated for this event.
    • Event::getSupportedMethods - Returns array with of the names of the methods supported in this version of Libevent
    • Event::pending - Detects whether event is pending or scheduled
    • Event::set - Re-configures event
    • Event::setPriority - Set event priority
    • Event::setTimer - Re-configures timer event
    • Event::signal - Constructs signal event object
    • Event::timer - Constructs timer event object
    • Event::__construct - Constructs Event object
    • EventBase::dispatch - Dispatch pending events
    • EventBase::exit - Stop dispatching events
    • EventBase::free - Free resources allocated for this event base
    • EventBase::getFeatures - Returns bitmask of features supported
    • EventBase::getMethod - Returns event method in use
    • EventBase::getTimeOfDayCached - Returns the current event base time
    • EventBase::gotExit - Checks if the event loop was told to exit
    • EventBase::gotStop - Checks if the event loop was told to exit
    • EventBase::loop - Dispatch pending events
    • EventBase::priorityInit - Sets number of priorities per event base
    • EventBase::reInit - Re-initialize event base(after a fork).
    • EventBase::stop - Tells event_base to stop dispatching events
    • EventBase::__construct - Constructs EventBase object
    • EventBuffer::add - Append data to the end of an event buffer
    • EventBuffer::addBuffer - Move all data from a buffer provided to the current instance of EventBuffer
    • EventBuffer::appendFrom - Moves the specified number of bytes from a source buffer to the end of the current buffer
    • EventBuffer::copyout - Copies out specified number of bytes from the front of the buffer
    • EventBuffer::drain - Removes specified number of bytes from the front of the buffer without copying it anywhere
    • EventBuffer::enableLocking - 설명
    • EventBuffer::expand - Reserves space in buffer
    • EventBuffer::freeze - Prevent calls that modify an event buffer from succeeding
    • EventBuffer::lock - Acquires a lock on buffer
    • EventBuffer::prepend - Prepend data to the front of the buffer
    • EventBuffer::prependBuffer - Moves all data from source buffer to the front of current buffer
    • EventBuffer::pullup - Linearizes data within buffer and returns it's contents as a string
    • EventBuffer::read - Read data from an evbuffer and drain the bytes read
    • EventBuffer::readFrom - Read data from a file onto the end of the buffer
    • EventBuffer::readLine - Extracts a line from the front of the buffer
    • EventBuffer::search - Scans the buffer for an occurrence of a string
    • EventBuffer::searchEol - Scans the buffer for an occurrence of an end of line
    • EventBuffer::substr - Substracts a portion of the buffer data
    • EventBuffer::unfreeze - Re-enable calls that modify an event buffer
    • EventBuffer::unlock - Releases lock acquired by EventBuffer::lock
    • EventBuffer::write - Write contents of the buffer to a file or socket
    • EventBuffer::__construct - Constructs EventBuffer object
    • EventBufferEvent::close - Closes file descriptor associated with the current buffer event
    • EventBufferEvent::connect - Connect buffer event's file descriptor to given address or UNIX socket
    • EventBufferEvent::connectHost - Connects to a hostname with optionally asyncronous DNS resolving
    • EventBufferEvent::createPair - Creates two buffer events connected to each other
    • EventBufferEvent::disable - Disable events read, write, or both on a buffer event.
    • EventBufferEvent::enable - Enable events read, write, or both on a buffer event.
    • EventBufferEvent::free - Free a buffer event
    • EventBufferEvent::getDnsErrorString - Returns string describing the last failed DNS lookup attempt
    • EventBufferEvent::getEnabled - Returns bitmask of events currently enabled on the buffer event
    • EventBufferEvent::getInput - Returns underlying input buffer associated with current buffer event
    • EventBufferEvent::getOutput - Returns underlying output buffer associated with current buffer event
    • EventBufferEvent::read - Read buffer's data
    • EventBufferEvent::readBuffer - Drains the entire contents of the input buffer and places them into buf
    • EventBufferEvent::setCallbacks - Assigns read, write and event(status) callbacks
    • EventBufferEvent::setPriority - Assign a priority to a bufferevent
    • EventBufferEvent::setTimeouts - Set the read and write timeout for a buffer event
    • EventBufferEvent::setWatermark - Adjusts read and/or write watermarks
    • EventBufferEvent::sslError - Returns most recent OpenSSL error reported on the buffer event
    • EventBufferEvent::sslFilter - Create a new SSL buffer event to send its data over another buffer event
    • EventBufferEvent::sslGetCipherInfo - Returns a textual description of the cipher.
    • EventBufferEvent::sslGetCipherName - Returns the current cipher name of the SSL connection.
    • EventBufferEvent::sslGetCipherVersion - Returns version of cipher used by current SSL connection.
    • EventBufferEvent::sslGetProtocol - Returns the name of the protocol used for current SSL connection.
    • EventBufferEvent::sslRenegotiate - Tells a bufferevent to begin SSL renegotiation.
    • EventBufferEvent::sslSocket - Creates a new SSL buffer event to send its data over an SSL on a socket
    • EventBufferEvent::write - Adds data to a buffer event's output buffer
    • EventBufferEvent::writeBuffer - Adds contents of the entire buffer to a buffer event's output buffer
    • EventBufferEvent::__construct - Constructs EventBufferEvent object
    • EventConfig::avoidMethod - Tells libevent to avoid specific event method
    • EventConfig::requireFeatures - Enters a required event method feature that the application demands
    • EventConfig::setMaxDispatchInterval - Prevents priority inversion
    • EventConfig::__construct - Constructs EventConfig object
    • EventDnsBase::addNameserverIp - Adds a nameserver to the DNS base
    • EventDnsBase::addSearch - Adds a domain to the list of search domains
    • EventDnsBase::clearSearch - Removes all current search suffixes
    • EventDnsBase::countNameservers - Gets the number of configured nameservers
    • EventDnsBase::loadHosts - Loads a hosts file (in the same format as /etc/hosts) from hosts file
    • EventDnsBase::parseResolvConf - Scans the resolv.conf-formatted file
    • EventDnsBase::setOption - Set the value of a configuration option
    • EventDnsBase::setSearchNdots - Set the 'ndots' parameter for searches
    • EventDnsBase::__construct - Constructs EventDnsBase object
    • EventHttp::accept - Makes an HTTP server accept connections on the specified socket stream or resource
    • EventHttp::addServerAlias - Adds a server alias to the HTTP server object
    • EventHttp::bind - Binds an HTTP server on the specified address and port
    • EventHttp::removeServerAlias - Removes server alias
    • EventHttp::setAllowedMethods - Sets the what HTTP methods are supported in requests accepted by this server, and passed to user callbacks
    • EventHttp::setCallback - Sets a callback for specified URI
    • EventHttp::setDefaultCallback - Sets default callback to handle requests that are not caught by specific callbacks
    • EventHttp::setMaxBodySize - Sets maximum request body size
    • EventHttp::setMaxHeadersSize - Sets maximum HTTP header size
    • EventHttp::setTimeout - Sets the timeout for an HTTP request
    • EventHttp::__construct - Constructs EventHttp object(the HTTP server)
    • EventHttpConnection::getBase - Returns event base associated with the connection
    • EventHttpConnection::getPeer - Gets the remote address and port associated with the connection
    • EventHttpConnection::makeRequest - Makes an HTTP request over the specified connection
    • EventHttpConnection::setCloseCallback - Set callback for connection close
    • EventHttpConnection::setLocalAddress - Sets the IP address from which HTTP connections are made
    • EventHttpConnection::setLocalPort - Sets the local port from which connections are made
    • EventHttpConnection::setMaxBodySize - Sets maximum body size for the connection
    • EventHttpConnection::setMaxHeadersSize - Sets maximum header size
    • EventHttpConnection::setRetries - Sets the retry limit for the connection
    • EventHttpConnection::setTimeout - Sets the timeout for the connection
    • EventHttpConnection::__construct - Constructs EventHttpConnection object
    • EventHttpRequest::addHeader - Adds an HTTP header to the headers of the request
    • EventHttpRequest::cancel - Cancels a pending HTTP request
    • EventHttpRequest::clearHeaders - Removes all output headers from the header list of the request
    • EventHttpRequest::closeConnection - Closes associated HTTP connection
    • EventHttpRequest::findHeader - Finds the value belonging a header
    • EventHttpRequest::free - Frees the object and removes associated events
    • EventHttpRequest::getBufferEvent - Returns EventBufferEvent object
    • EventHttpRequest::getCommand - Returns the request command(method)
    • EventHttpRequest::getConnection - Returns EventHttpConnection object
    • EventHttpRequest::getHost - Returns the request host
    • EventHttpRequest::getInputBuffer - Returns the input buffer
    • EventHttpRequest::getInputHeaders - Returns associative array of the input headers
    • EventHttpRequest::getOutputBuffer - Returns the output buffer of the request
    • EventHttpRequest::getOutputHeaders - Returns associative array of the output headers
    • EventHttpRequest::getResponseCode - Returns the response code
    • EventHttpRequest::getUri - Returns the request URI
    • EventHttpRequest::removeHeader - Removes an HTTP header from the headers of the request
    • EventHttpRequest::sendError - Send an HTML error message to the client
    • EventHttpRequest::sendReply - Send an HTML reply to the client
    • EventHttpRequest::sendReplyChunk - Send another data chunk as part of an ongoing chunked reply
    • EventHttpRequest::sendReplyEnd - Complete a chunked reply, freeing the request as appropriate
    • EventHttpRequest::sendReplyStart - Initiate a chunked reply
    • EventHttpRequest::__construct - Constructs EventHttpRequest object
    • EventListener::disable - Disables an event connect listener object
    • EventListener::enable - Enables an event connect listener object
    • EventListener::getBase - Returns event base associated with the event listener
    • EventListener::getSocketName - Retreives the current address to which the listener's socket is bound.
    • EventListener::setCallback - The setCallback purpose
    • EventListener::setErrorCallback - Set event listener's error callback
    • EventListener::__construct - Creates new connection listener associated with an event base
    • EventSslContext::__construct - Constructs an OpenSSL context for use with Event classes
    • EventUtil::getLastSocketErrno - Returns the most recent socket error number
    • EventUtil::getLastSocketError - Returns the most recent socket error
    • EventUtil::getSocketFd - Returns numeric file descriptor of a socket, or stream
    • EventUtil::getSocketName - Retreives the current address to which the socket is bound.
    • EventUtil::setSocketOption - Sets socket options
    • EventUtil::sslRandPoll - Generates entropy by means of OpenSSL's RAND_poll()
    • EventUtil::__construct - The abstract constructor
    • event_base_free - Destroy event base
    • event_base_loop - Handle events
    • event_base_loopbreak - Abort event loop
    • event_base_loopexit - Exit loop after a time
    • event_base_new - Create and initialize new event base
    • event_base_priority_init - Set the number of event priority levels
    • event_base_reinit - Reinitialize the event base after a fork
    • event_base_set - Associate event base with an event
    • event_buffer_base_set - Associate buffered event with an event base
    • event_buffer_disable - Disable a buffered event
    • event_buffer_enable - Enable a buffered event
    • event_buffer_fd_set - Change a buffered event file descriptor
    • event_buffer_free - Destroy buffered event
    • event_buffer_new - Create new buffered event
    • event_buffer_priority_set - Assign a priority to a buffered event
    • event_buffer_read - Read data from a buffered event
    • event_buffer_set_callback - Set or reset callbacks for a buffered event
    • event_buffer_timeout_set - Set read and write timeouts for a buffered event
    • event_buffer_watermark_set - Set the watermarks for read and write events
    • event_buffer_write - Write data to a buffered event
    • event_new - Create new event
    • event_priority_set - Assign a priority to an event.
    • event_timer_add - 별칭: event_add
    • event_timer_del - 별칭: event_del
    • event_timer_new - 별칭: event_new
    • event_timer_set - Prepare a timer event
    • EvFork::createStopped - Creates a stopped instance of EvFork watcher class
    • EvFork::__construct - Constructs the EvFork watcher object
    • EvIdle::createStopped - Creates instance of a stopped EvIdle watcher object
    • EvIdle::__construct - Constructs the EvIdle watcher object
    • EvIo::createStopped - Create stopped EvIo watcher object
    • EvIo::set - Configures the watcher
    • EvIo::__construct - Constructs EvIo watcher object
    • EvLoop::backend - Returns an integer describing the backend used by libev.
    • EvLoop::check - Creates EvCheck object associated with the current event loop instance
    • EvLoop::child - Creates EvChild object associated with the current event loop
    • EvLoop::defaultLoop - Returns or creates the default event loop.
    • EvLoop::embed - Creates an instance of EvEmbed watcher associated with the current EvLoop object.
    • EvLoop::fork - Creates EvFork watcher object associated with the current event loop instance
    • EvLoop::idle - Creates EvIdle watcher object associated with the current event loop instance
    • EvLoop::invokePending - Invoke all pending watchers while resetting their pending state
    • EvLoop::io - Create EvIo watcher object associated with the current event loop instance
    • EvLoop::loopFork - Must be called after a fork
    • EvLoop::now - Returns the current "event loop time"
    • EvLoop::nowUpdate - Establishes the current time by querying the kernel, updating the time returned by EvLoop::now in the progress.
    • EvLoop::periodic - Creates EvPeriodic watcher object associated with the current event loop instance
    • EvLoop::prepare - Creates EvPrepare watcher object associated with the current event loop instance
    • EvLoop::resume - Resume previously suspended default event loop
    • EvLoop::run - Begin checking for events and calling callbacks for the loop
    • EvLoop::signal - Creates EvSignal watcher object associated with the current event loop instance
    • EvLoop::stat - Creates EvStat watcher object associated with the current event loop instance
    • EvLoop::stop - Stops the event loop
    • EvLoop::suspend - Suspend the loop
    • EvLoop::timer - Creates EvTimer watcher object associated with the current event loop instance
    • EvLoop::verify - Performs internal consistency checks(for debugging)
    • EvLoop::__construct - Constructs the event loop object
    • EvPeriodic::again - Simply stops and restarts the periodic watcher again.
    • EvPeriodic::at - Returns the absolute time that this watcher is supposed to trigger next
    • EvPeriodic::createStopped - Create a stopped EvPeriodic watcher
    • EvPeriodic::set - Configures the watcher
    • EvPeriodic::__construct - Constructs EvPeriodic watcher object
    • EvPrepare::createStopped - Creates a stopped instance of EvPrepare watcher
    • EvPrepare::__construct - Constructs EvPrepare watcher object
    • EvSignal::createStopped - Create stopped EvSignal watcher object
    • EvSignal::set - Configures the watcher
    • EvSignal::__construct - Constructs EvPeriodic watcher object
    • EvStat::attr - Returns the values most recently detected by Ev
    • EvStat::createStopped - Create a stopped EvStat watcher object
    • EvStat::prev - Returns the previous set of values returned by EvStat::attr
    • EvStat::set - Configures the watcher
    • EvStat::stat - Initiates the stat call
    • EvStat::__construct - Constructs EvStat watcher object
    • EvTimer::again - Restarts the timer watcher
    • EvTimer::createStopped - Creates EvTimer stopped watcher object
    • EvTimer::set - Configures the watcher
    • EvTimer::__construct - Constructs an EvTimer watcher object
    • EvWatcher::clear - Clear watcher pending status
    • EvWatcher::feed - Feeds the given revents set into the event loop
    • EvWatcher::getLoop - Returns the loop responsible for the watcher
    • EvWatcher::invoke - Invokes the watcher callback with the given received events bit mask
    • EvWatcher::keepalive - Configures whether to keep the loop from returning
    • EvWatcher::setCallback - Sets new callback for the watcher
    • EvWatcher::start - Starts the watcher
    • EvWatcher::stop - Stops the watcher
    • EvWatcher::__construct - Abstract constructor of a watcher object
    • Examples with PDO_4D - Examples PDO_4D
    • Exception::getCode - Gets the Exception code
    • Exception::getFile - Gets the file in which the exception occurred
    • Exception::getLine - Gets the line in which the exception occurred
    • Exception::getMessage - Gets the Exception message
    • Exception::getPrevious - Returns previous Exception
    • Exception::getTrace - Gets the stack trace
    • Exception::getTraceAsString - Gets the stack trace as a string
    • Exception::__clone - Clone the exception
    • Exception::__construct - Construct the exception
    • Exception::__toString - String representation of the exception
    • exec - Execute an external program
    • exif_imagetype - Determine the type of an image
    • exif_read_data - Reads the EXIF headers from JPEG or TIFF
    • exif_tagname - Get the header name for an index
    • exif_thumbnail - Retrieve the embedded thumbnail of a TIFF or JPEG image
    • exit - Output a message and terminate the current script
    • exp - e의 누승을 계산
    • expect:// - Process Interaction Streams
    • expect_expectl - Waits until the output from a process matches one of the patterns, a specified time period has passed, or an EOF is seen
    • expect_popen - Execute command via Bourne shell, and open the PTY stream to the process
    • explode - 문자열을 문자열로 나눕니다
    • expm1 - Returns exp(number) - 1, computed in a way that is accurate even when the value of number is close to zero
    • extension_loaded - Find out whether an extension is loaded
    • extract - 배열에서 현재 심볼 테이블로 변수를 입력
    • ezmlm_hash - EZMLM에 필요한 해시값을 계산합니다
  • f
    • fam_cancel_monitor - Terminate monitoring
    • fam_close - Close FAM connection
    • fam_monitor_collection - Monitor a collection of files in a directory for changes
    • fam_monitor_directory - Monitor a directory for changes
    • fam_monitor_file - Monitor a regular file for changes
    • fam_next_event - Get next pending FAM event
    • fam_open - Open connection to FAM daemon
    • fam_pending - Check for pending FAM events
    • fam_resume_monitor - Resume suspended monitoring
    • fam_suspend_monitor - Temporarily suspend monitoring
    • FANNConnection::getFromNeuron - Returns the postions of starting neuron.
    • FANNConnection::getToNeuron - Returns the postions of terminating neuron
    • FANNConnection::getWeight - Returns the connection weight
    • FANNConnection::setWeight - Sets the connections weight
    • FANNConnection::__construct - The connection constructor
    • fann_cascadetrain_on_data - Trains on an entire dataset, for a period of time using the Cascade2 training algorithm
    • fann_cascadetrain_on_file - Trains on an entire dataset read from file, for a period of time using the Cascade2 training algorithm.
    • fann_clear_scaling_params - Clears scaling parameters
    • fann_copy - Creates a copy of a fann structure
    • fann_create_from_file - Constructs a backpropagation neural network from a configuration file
    • fann_create_shortcut - Creates a standard backpropagation neural network which is not fully connectected and has shortcut connections
    • fann_create_shortcut_array - Creates a standard backpropagation neural network which is not fully connectected and has shortcut connections
    • fann_create_sparse - Creates a standard backpropagation neural network, which is not fully connected
    • fann_create_sparse_array - Creates a standard backpropagation neural network, which is not fully connected using an array of layer sizes
    • fann_create_standard - Creates a standard fully connected backpropagation neural network
    • fann_create_standard_array - Creates a standard fully connected backpropagation neural network using an array of layer sizes
    • fann_create_train - Creates an empty training data struct
    • fann_create_train_from_callback - Creates the training data struct from a user supplied function
    • fann_descale_input - Scale data in input vector after get it from ann based on previously calculated parameters
    • fann_descale_output - Scale data in output vector after get it from ann based on previously calculated parameters
    • fann_descale_train - Descale input and output data based on previously calculated parameters
    • fann_destroy - Destroys the entire network and properly freeing all the associated memory
    • fann_destroy_train - Destructs the training data
    • fann_duplicate_train_data - Returns an exact copy of a fann train data
    • fann_get_activation_function - Returns the activation function
    • fann_get_activation_steepness - Returns the activation steepness for supplied neuron and layer number
    • fann_get_bias_array - Get the number of bias in each layer in the network
    • fann_get_bit_fail - The number of fail bits
    • fann_get_bit_fail_limit - Returns the bit fail limit used during training
    • fann_get_cascade_activation_functions - Returns the cascade activation functions
    • fann_get_cascade_activation_functions_count - Returns the number of cascade activation functions
    • fann_get_cascade_activation_steepnesses - Returns the cascade activation steepnesses
    • fann_get_cascade_activation_steepnesses_count - The number of activation steepnesses
    • fann_get_cascade_candidate_change_fraction - Returns the cascade candidate change fraction
    • fann_get_cascade_candidate_limit - Return the candidate limit
    • fann_get_cascade_candidate_stagnation_epochs - Returns the number of cascade candidate stagnation epochs
    • fann_get_cascade_max_cand_epochs - Returns the maximum candidate epochs
    • fann_get_cascade_max_out_epochs - Returns the maximum out epochs
    • fann_get_cascade_min_cand_epochs - Returns the minimum candidate epochs
    • fann_get_cascade_min_out_epochs - Returns the minimum out epochs
    • fann_get_cascade_num_candidates - Returns the number of candidates used during training
    • fann_get_cascade_num_candidate_groups - Returns the number of candidate groups
    • fann_get_cascade_output_change_fraction - Returns the cascade output change fraction
    • fann_get_cascade_output_stagnation_epochs - Returns the number of cascade output stagnation epochs
    • fann_get_cascade_weight_multiplier - Returns the weight multiplier
    • fann_get_connection_array - Get connections in the network
    • fann_get_connection_rate - Get the connection rate used when the network was created
    • fann_get_errno - Returns the last error number
    • fann_get_errstr - Returns the last errstr
    • fann_get_layer_array - Get the number of neurons in each layer in the network
    • fann_get_learning_momentum - Returns the learning momentum
    • fann_get_learning_rate - Returns the learning rate
    • fann_get_MSE - Reads the mean square error from the network
    • fann_get_network_type - Get the type of neural network it was created as
    • fann_get_num_input - Get the number of input neurons
    • fann_get_num_layers - Get the number of layers in the neural network
    • fann_get_num_output - Get the number of output neurons
    • fann_get_quickprop_decay - Returns the decay which is a factor that weights should decrease in each iteration during quickprop training
    • fann_get_quickprop_mu - Returns the mu factor
    • fann_get_rprop_decrease_factor - Returns the increase factor used during RPROP training
    • fann_get_rprop_delta_max - Returns the maximum step-size
    • fann_get_rprop_delta_min - Returns the minimum step-size
    • fann_get_rprop_delta_zero - Returns the initial step-size
    • fann_get_rprop_increase_factor - Returns the increase factor used during RPROP training
    • fann_get_sarprop_step_error_shift - Returns the sarprop step error shift
    • fann_get_sarprop_step_error_threshold_factor - Returns the sarprop step error threshold factor
    • fann_get_sarprop_temperature - Returns the sarprop temperature
    • fann_get_sarprop_weight_decay_shift - Returns the sarprop weight decay shift
    • fann_get_total_connections - Get the total number of connections in the entire network
    • fann_get_total_neurons - Get the total number of neurons in the entire network
    • fann_get_training_algorithm - Returns the training algorithm
    • fann_get_train_error_function - Returns the error function used during training
    • fann_get_train_stop_function - Returns the stop function used during training
    • fann_init_weights - Initialize the weights using Widrow + Nguyen’s algorithm
    • fann_length_train_data - Returns the number of training patterns in the train data
    • fann_merge_train_data - Merges the train data
    • fann_num_input_train_data - Returns the number of inputs in each of the training patterns in the train data
    • fann_num_output_train_data - Returns the number of outputs in each of the training patterns in the train data
    • fann_print_error - Prints the error string
    • fann_randomize_weights - Give each connection a random weight between min_weight and max_weight
    • fann_read_train_from_file - Reads a file that stores training data
    • fann_reset_errno - Resets the last error number
    • fann_reset_errstr - Resets the last error string
    • fann_reset_MSE - Resets the mean square error from the network
    • fann_run - Will run input through the neural network
    • fann_save - Saves the entire network to a configuration file
    • fann_save_train - Save the training structure to a file
    • fann_scale_input - Scale data in input vector before feed it to ann based on previously calculated parameters
    • fann_scale_input_train_data - Scales the inputs in the training data to the specified range
    • fann_scale_output - Scale data in output vector before feed it to ann based on previously calculated parameters
    • fann_scale_output_train_data - Scales the outputs in the training data to the specified range
    • fann_scale_train - Scale input and output data based on previously calculated parameters
    • fann_scale_train_data - Scales the inputs and outputs in the training data to the specified range
    • fann_set_activation_function - Sets the activation function for supplied neuron and layer
    • fann_set_activation_function_hidden - Sets the activation function for all of the hidden layers
    • fann_set_activation_function_layer - Sets the activation function for all the neurons in the supplied layer.
    • fann_set_activation_function_output - Sets the activation function for the output layer
    • fann_set_activation_steepness - Sets the activation steepness for supplied neuron and layer number
    • fann_set_activation_steepness_hidden - Sets the steepness of the activation steepness for all neurons in the all hidden layers
    • fann_set_activation_steepness_layer - Sets the activation steepness for all of the neurons in the supplied layer number
    • fann_set_activation_steepness_output - Sets the steepness of the activation steepness in the output layer
    • fann_set_bit_fail_limit - Set the bit fail limit used during training
    • fann_set_callback - Sets the callback function for use during training
    • fann_set_cascade_activation_functions - Sets the array of cascade candidate activation functions
    • fann_set_cascade_activation_steepnesses - Sets the array of cascade candidate activation steepnesses
    • fann_set_cascade_candidate_change_fraction - Sets the cascade candidate change fraction
    • fann_set_cascade_candidate_limit - Sets the candidate limit
    • fann_set_cascade_candidate_stagnation_epochs - Sets the number of cascade candidate stagnation epochs
    • fann_set_cascade_max_cand_epochs - Sets the max candidate epochs
    • fann_set_cascade_max_out_epochs - Sets the maximum out epochs
    • fann_set_cascade_min_cand_epochs - Sets the min candidate epochs
    • fann_set_cascade_min_out_epochs - Sets the minimum out epochs
    • fann_set_cascade_num_candidate_groups - Sets the number of candidate groups
    • fann_set_cascade_output_change_fraction - Sets the cascade output change fraction
    • fann_set_cascade_output_stagnation_epochs - Sets the number of cascade output stagnation epochs
    • fann_set_cascade_weight_multiplier - Sets the weight multiplier
    • fann_set_error_log - Sets where the errors are logged to
    • fann_set_input_scaling_params - Calculate input scaling parameters for future use based on training data
    • fann_set_learning_momentum - Sets the learning momentum
    • fann_set_learning_rate - Sets the learning rate
    • fann_set_output_scaling_params - Calculate output scaling parameters for future use based on training data
    • fann_set_quickprop_decay - Sets the quickprop decay factor
    • fann_set_quickprop_mu - Sets the quickprop mu factor
    • fann_set_rprop_decrease_factor - Sets the decrease factor used during RPROP training
    • fann_set_rprop_delta_max - Sets the maximum step-size
    • fann_set_rprop_delta_min - Sets the minimum step-size
    • fann_set_rprop_delta_zero - Sets the initial step-size
    • fann_set_rprop_increase_factor - Sets the increase factor used during RPROP training
    • fann_set_sarprop_step_error_shift - Sets the sarprop step error shift
    • fann_set_sarprop_step_error_threshold_factor - Sets the sarprop step error threshold factor
    • fann_set_sarprop_temperature - Sets the sarprop temperature
    • fann_set_sarprop_weight_decay_shift - Sets the sarprop weight decay shift
    • fann_set_scaling_params - Calculate input and output scaling parameters for future use based on training data
    • fann_set_training_algorithm - Sets the training algorithm
    • fann_set_train_error_function - Sets the error function used during training
    • fann_set_train_stop_function - Sets the stop function used during training
    • fann_set_weight - Set a connection in the network
    • fann_set_weight_array - Set connections in the network
    • fann_shuffle_train_data - Shuffles training data, randomizing the order
    • fann_subset_train_data - Returns an copy of a subset of the train data
    • fann_test - Test with a set of inputs, and a set of desired outputs
    • fann_test_data - Test a set of training data and calculates the MSE for the training data
    • fann_train - Train one iteration with a set of inputs, and a set of desired outputs
    • fann_train_epoch - Train one epoch with a set of training data
    • fann_train_on_data - Trains on an entire dataset for a period of time
    • fann_train_on_file - Trains on an entire dataset, which is read from file, for a period of time
    • fastcgi_finish_request - Flushes all response data to the client
    • fbsql_affected_rows - Get number of affected rows in previous FrontBase operation
    • fbsql_autocommit - Enable or disable autocommit
    • fbsql_blob_size - Get the size of a BLOB
    • fbsql_change_user - Change logged in user of the active connection
    • fbsql_clob_size - Get the size of a CLOB
    • fbsql_close - Close FrontBase connection
    • fbsql_commit - Commits a transaction to the database
    • fbsql_connect - Open a connection to a FrontBase Server
    • fbsql_create_blob - Create a BLOB
    • fbsql_create_clob - Create a CLOB
    • fbsql_create_db - Create a FrontBase database
    • fbsql_database - Get or set the database name used with a connection
    • fbsql_database_password - Sets or retrieves the password for a FrontBase database
    • fbsql_data_seek - Move internal result pointer
    • fbsql_db_query - Send a FrontBase query
    • fbsql_db_status - Get the status for a given database
    • fbsql_drop_db - Drop (delete) a FrontBase database
    • fbsql_errno - Returns the error number from previous operation
    • fbsql_error - Returns the error message from previous operation
    • fbsql_fetch_array - Fetch a result row as an associative array, a numeric array, or both
    • fbsql_fetch_assoc - Fetch a result row as an associative array
    • fbsql_fetch_field - Get column information from a result and return as an object
    • fbsql_fetch_lengths - Get the length of each output in a result
    • fbsql_fetch_object - Fetch a result row as an object
    • fbsql_fetch_row - Get a result row as an enumerated array
    • fbsql_field_flags - Get the flags associated with the specified field in a result
    • fbsql_field_len - Returns the length of the specified field
    • fbsql_field_name - Get the name of the specified field in a result
    • fbsql_field_seek - Set result pointer to a specified field offset
    • fbsql_field_table - Get name of the table the specified field is in
    • fbsql_field_type - Get the type of the specified field in a result
    • fbsql_free_result - Free result memory
    • fbsql_get_autostart_info - 설명
    • fbsql_hostname - Get or set the host name used with a connection
    • fbsql_insert_id - Get the id generated from the previous INSERT operation
    • fbsql_list_dbs - List databases available on a FrontBase server
    • fbsql_list_fields - List FrontBase result fields
    • fbsql_list_tables - List tables in a FrontBase database
    • fbsql_next_result - Move the internal result pointer to the next result
    • fbsql_num_fields - Get number of fields in result
    • fbsql_num_rows - Get number of rows in result
    • fbsql_password - Get or set the user password used with a connection
    • fbsql_pconnect - Open a persistent connection to a FrontBase Server
    • fbsql_query - Send a FrontBase query
    • fbsql_read_blob - Read a BLOB from the database
    • fbsql_read_clob - Read a CLOB from the database
    • fbsql_result - Get result data
    • fbsql_rollback - Rollback a transaction to the database
    • fbsql_rows_fetched - Get the number of rows affected by the last statement
    • fbsql_select_db - Select a FrontBase database
    • fbsql_set_characterset - Change input/output character set
    • fbsql_set_lob_mode - Set the LOB retrieve mode for a FrontBase result set
    • fbsql_set_password - Change the password for a given user
    • fbsql_set_transaction - Set the transaction locking and isolation
    • fbsql_start_db - Start a database on local or remote server
    • fbsql_stop_db - Stop a database on local or remote server
    • fbsql_tablename - 별칭: fbsql_table_name
    • fbsql_table_name - Get table name of field
    • fbsql_username - Get or set the username for the connection
    • fbsql_warnings - Enable or disable FrontBase warnings
    • fclose - Closes an open file pointer
    • fdf_add_doc_javascript - Adds javascript code to the FDF document
    • fdf_add_template - Adds a template into the FDF document
    • fdf_close - Close an FDF document
    • fdf_create - Create a new FDF document
    • fdf_enum_values - Call a user defined function for each document value
    • fdf_errno - Return error code for last fdf operation
    • fdf_error - Return error description for FDF error code
    • fdf_get_ap - Get the appearance of a field
    • fdf_get_attachment - Extracts uploaded file embedded in the FDF
    • fdf_get_encoding - Get the value of the /Encoding key
    • fdf_get_file - Get the value of the /F key
    • fdf_get_flags - Gets the flags of a field
    • fdf_get_opt - Gets a value from the opt array of a field
    • fdf_get_status - Get the value of the /STATUS key
    • fdf_get_value - Get the value of a field
    • fdf_get_version - Gets version number for FDF API or file
    • fdf_header - Sets FDF-specific output headers
    • fdf_next_field_name - Get the next field name
    • fdf_open - Open a FDF document
    • fdf_open_string - Read a FDF document from a string
    • fdf_remove_item - Sets target frame for form
    • fdf_save - Save a FDF document
    • fdf_save_string - Returns the FDF document as a string
    • fdf_set_ap - Set the appearance of a field
    • fdf_set_encoding - Sets FDF character encoding
    • fdf_set_file - Set PDF document to display FDF data in
    • fdf_set_flags - Sets a flag of a field
    • fdf_set_javascript_action - Sets an javascript action of a field
    • fdf_set_on_import_javascript - Adds javascript code to be executed when Acrobat opens the FDF
    • fdf_set_opt - Sets an option of a field
    • fdf_set_status - Set the value of the /STATUS key
    • fdf_set_submit_form_action - Sets a submit form action of a field
    • fdf_set_target_frame - Set target frame for form display
    • fdf_set_value - Set the value of a field
    • fdf_set_version - Sets version number for a FDF file
    • feof - Tests for end-of-file on a file pointer
    • fflush - Flushes the output to a file
    • fgetc - Gets character from file pointer
    • fgetcsv - Gets line from file pointer and parse for CSV fields
    • fgets - Gets line from file pointer
    • fgetss - Gets line from file pointer and strip HTML tags
    • file - Reads entire file into an array
    • file:// - Accessing local filesystem
    • fileatime - Gets last access time of file
    • filectime - Gets inode change time of file
    • filegroup - Gets file group
    • fileinode - Gets file inode
    • filemtime - Gets file modification time
    • fileowner - Gets file owner
    • fileperms - Gets file permissions
    • filepro - Read and verify the map file
    • filepro_fieldcount - Find out how many fields are in a filePro database
    • filepro_fieldname - Gets the name of a field
    • filepro_fieldtype - Gets the type of a field
    • filepro_fieldwidth - Gets the width of a field
    • filepro_retrieve - Retrieves data from a filePro database
    • filepro_rowcount - Find out how many rows are in a filePro database
    • filesize - Gets file size
    • FilesystemIterator::current - The current file
    • FilesystemIterator::getFlags - Get the handling flags
    • FilesystemIterator::key - Retrieve the key for the current file
    • FilesystemIterator::next - Move to the next file
    • FilesystemIterator::rewind - Rewinds back to the beginning
    • FilesystemIterator::setFlags - Sets handling flags
    • FilesystemIterator::__construct - Constructs a new filesystem iterator
    • filetype - Gets file type
    • file_exists - Checks whether a file or directory exists
    • file_get_contents - Reads entire file into a string
    • file_put_contents - Write a string to a file
    • FilterIterator::accept - Check whether the current element of the iterator is acceptable
    • FilterIterator::current - Get the current element value
    • FilterIterator::getInnerIterator - Get the inner iterator
    • FilterIterator::key - Get the current key
    • FilterIterator::next - Move the iterator forward
    • FilterIterator::rewind - Rewind the iterator
    • FilterIterator::valid - Check whether the current element is valid
    • FilterIterator::__construct - Construct a filterIterator
    • filter_has_var - Checks if variable of specified type exists
    • filter_id - Returns the filter ID belonging to a named filter
    • filter_input - Gets a specific external variable by name and optionally filters it
    • filter_input_array - Gets external variables and optionally filters them
    • filter_list - Returns a list of all supported filters
    • filter_var - Filters a variable with a specified filter
    • filter_var_array - Gets multiple variables and optionally filters them
    • finfo::buffer - 별칭: finfo_buffer()
    • finfo::file - 별칭: finfo_file()
    • finfo::set_flags - 별칭: finfo_set_flags()
    • finfo::__construct - 별칭: finfo_open
    • finfo_close - Close fileinfo resource
    • finfo_open - Create a new fileinfo resource
    • floatval - 변수의 float 값을 얻습니다
    • flock - Portable advisory file locking
    • floor - 소수점 아래 내림
    • flush - 출력 버퍼를 비웁니다
    • fmod - 인수의 나눗셈에 대한 부동소수점 나머지를 반환
    • fnmatch - Match filename against a pattern
    • fopen - Opens file or URL
    • forward_static_call - Call a static method
    • forward_static_call_array - Call a static method and pass the arguments as array
    • fpassthru - Output all remaining data on a file pointer
    • fprintf - 형식화한 문자열을 스트림에 기록
    • fputcsv - Format line as CSV and write to file pointer
    • fputs - 별칭: fwrite
    • fread - Binary-safe file read
    • frenchtojd - Converts a date from the French Republican Calendar to a Julian Day Count
    • fribidi_log2vis - Convert a logical string to a visual one
    • fscanf - Parses input from a file according to a format
    • fseek - Seeks on a file pointer
    • fsockopen - Open Internet or Unix domain socket connection
    • fstat - Gets information about a file using an open file pointer
    • ftell - Returns the current position of the file read/write pointer
    • ftok - Convert a pathname and a project identifier to a System V IPC key
    • ftp:// - Accessing FTP(s) URLs
    • FTP context options - FTP context option listing
    • ftp_alloc - Allocates space for a file to be uploaded
    • ftp_cdup - Changes to the parent directory
    • ftp_chdir - Changes the current directory on a FTP server
    • ftp_chmod - Set permissions on a file via FTP
    • ftp_close - Closes an FTP connection
    • ftp_connect - Opens an FTP connection
    • ftp_delete - Deletes a file on the FTP server
    • ftp_exec - Requests execution of a command on the FTP server
    • ftp_fget - Downloads a file from the FTP server and saves to an open file
    • ftp_fput - Uploads from an open file to the FTP server
    • ftp_get - Downloads a file from the FTP server
    • ftp_get_option - Retrieves various runtime behaviours of the current FTP stream
    • ftp_login - Logs in to an FTP connection
    • ftp_mdtm - Returns the last modified time of the given file
    • ftp_mkdir - Creates a directory
    • ftp_nb_continue - Continues retrieving/sending a file (non-blocking)
    • ftp_nb_fget - Retrieves a file from the FTP server and writes it to an open file (non-blocking)
    • ftp_nb_fput - Stores a file from an open file to the FTP server (non-blocking)
    • ftp_nb_get - Retrieves a file from the FTP server and writes it to a local file (non-blocking)
    • ftp_nb_put - Stores a file on the FTP server (non-blocking)
    • ftp_nlist - Returns a list of files in the given directory
    • ftp_pasv - Turns passive mode on or off
    • ftp_put - Uploads a file to the FTP server
    • ftp_pwd - Returns the current directory name
    • ftp_quit - 별칭: ftp_close
    • ftp_raw - Sends an arbitrary command to an FTP server
    • ftp_rawlist - Returns a detailed list of files in the given directory
    • ftp_rename - Renames a file or a directory on the FTP server
    • ftp_rmdir - Removes a directory
    • ftp_set_option - Set miscellaneous runtime FTP options
    • ftp_site - Sends a SITE command to the server
    • ftp_size - Returns the size of the given file
    • ftp_ssl_connect - Opens an Secure SSL-FTP connection
    • ftp_systype - Returns the system type identifier of the remote FTP server
    • ftruncate - Truncates a file to a given length
    • function_exists - Return TRUE if the given function has been defined
    • func_get_arg - Return an item from the argument list
    • func_get_args - Returns an array comprising a function's argument list
    • func_num_args - Returns the number of arguments passed to the function
    • fwrite - Binary-safe file write
  • g
    • gc_collect_cycles - Forces collection of any existing garbage cycles
    • gc_disable - Deactivates the circular reference collector
    • gc_enable - Activates the circular reference collector
    • gc_enabled - Returns status of the circular reference collector
    • gc_mem_caches - Reclaims memory used by the Zend Engine memory manager
    • gd_info - Retrieve information about the currently installed GD library
    • GearmanClient::addOptions - Add client options
    • GearmanClient::addServer - Add a job server to the client
    • GearmanClient::addServers - Add a list of job servers to the client
    • GearmanClient::addTask - Add a task to be run in parallel
    • GearmanClient::addTaskBackground - Add a background task to be run in parallel
    • GearmanClient::addTaskHigh - Add a high priority task to run in parallel
    • GearmanClient::addTaskHighBackground - Add a high priority background task to be run in parallel
    • GearmanClient::addTaskLow - Add a low priority task to run in parallel
    • GearmanClient::addTaskLowBackground - Add a low priority background task to be run in parallel
    • GearmanClient::addTaskStatus - Add a task to get status
    • GearmanClient::clearCallbacks - Clear all task callback functions
    • GearmanClient::clone - Create a copy of a GearmanClient object
    • GearmanClient::context - Get the application context
    • GearmanClient::data - Get the application data (deprecated)
    • GearmanClient::do - Run a single task and return a result [deprecated]
    • GearmanClient::doBackground - Run a task in the background
    • GearmanClient::doHigh - Run a single high priority task
    • GearmanClient::doHighBackground - Run a high priority task in the background
    • GearmanClient::doJobHandle - Get the job handle for the running task
    • GearmanClient::doLow - Run a single low priority task
    • GearmanClient::doLowBackground - Run a low priority task in the background
    • GearmanClient::doNormal - Run a single task and return a result
    • GearmanClient::doStatus - Get the status for the running task
    • GearmanClient::echo - Send data to all job servers to see if they echo it back [deprecated]
    • GearmanClient::error - Returns an error string for the last error encountered.
    • GearmanClient::getErrno - Get an errno value
    • GearmanClient::jobStatus - Get the status of a background job
    • GearmanClient::ping - Send data to all job servers to see if they echo it back
    • GearmanClient::removeOptions - Remove client options
    • GearmanClient::returnCode - Get the last Gearman return code
    • GearmanClient::runTasks - Run a list of tasks in parallel
    • GearmanClient::setClientCallback - Callback function when there is a data packet for a task (deprecated)
    • GearmanClient::setCompleteCallback - Set a function to be called on task completion
    • GearmanClient::setContext - Set application context
    • GearmanClient::setCreatedCallback - Set a callback for when a task is queued
    • GearmanClient::setData - Set application data (deprecated)
    • GearmanClient::setDataCallback - Callback function when there is a data packet for a task
    • GearmanClient::setExceptionCallback - Set a callback for worker exceptions
    • GearmanClient::setFailCallback - Set callback for job failure
    • GearmanClient::setOptions - Set client options
    • GearmanClient::setStatusCallback - Set a callback for collecting task status
    • GearmanClient::setTimeout - Set socket I/O activity timeout
    • GearmanClient::setWarningCallback - Set a callback for worker warnings
    • GearmanClient::setWorkloadCallback - Set a callback for accepting incremental data updates
    • GearmanClient::timeout - Get current socket I/O activity timeout value
    • GearmanClient::__construct - Create a GearmanClient instance
    • GearmanJob::complete - Send the result and complete status (deprecated)
    • GearmanJob::data - Send data for a running job (deprecated)
    • GearmanJob::exception - Send exception for running job (deprecated)
    • GearmanJob::fail - Send fail status (deprecated)
    • GearmanJob::functionName - Get function name
    • GearmanJob::handle - Get the job handle
    • GearmanJob::returnCode - Get last return code
    • GearmanJob::sendComplete - Send the result and complete status
    • GearmanJob::sendData - Send data for a running job
    • GearmanJob::sendException - Send exception for running job (exception)
    • GearmanJob::sendFail - Send fail status
    • GearmanJob::sendStatus - Send status
    • GearmanJob::sendWarning - Send a warning
    • GearmanJob::setReturn - Set a return value
    • GearmanJob::status - Send status (deprecated)
    • GearmanJob::unique - Get the unique identifier
    • GearmanJob::warning - Send a warning (deprecated)
    • GearmanJob::workload - Get workload
    • GearmanJob::workloadSize - Get size of work load
    • GearmanJob::__construct - Create a GearmanJob instance
    • GearmanTask::create - Create a task (deprecated)
    • GearmanTask::data - Get data returned for a task
    • GearmanTask::dataSize - Get the size of returned data
    • GearmanTask::function - Get associated function name (deprecated)
    • GearmanTask::functionName - Get associated function name
    • GearmanTask::isKnown - Determine if task is known
    • GearmanTask::isRunning - Test whether the task is currently running
    • GearmanTask::jobHandle - Get the job handle
    • GearmanTask::recvData - Read work or result data into a buffer for a task
    • GearmanTask::returnCode - Get the last return code
    • GearmanTask::sendData - Send data for a task (deprecated)
    • GearmanTask::sendWorkload - Send data for a task
    • GearmanTask::taskDenominator - Get completion percentage denominator
    • GearmanTask::taskNumerator - Get completion percentage numerator
    • GearmanTask::unique - Get the unique identifier for a task
    • GearmanTask::uuid - Get the unique identifier for a task (deprecated)
    • GearmanTask::__construct - Create a GearmanTask instance
    • GearmanWorker::addFunction - Register and add callback function
    • GearmanWorker::addOptions - Add worker options
    • GearmanWorker::addServer - Add a job server
    • GearmanWorker::addServers - Add job servers
    • GearmanWorker::clone - Create a copy of the worker
    • GearmanWorker::echo - Test job server response
    • GearmanWorker::error - Get the last error encountered
    • GearmanWorker::getErrno - Get errno
    • GearmanWorker::options - Get worker options
    • GearmanWorker::register - Register a function with the job server
    • GearmanWorker::removeOptions - Remove worker options
    • GearmanWorker::returnCode - Get last Gearman return code
    • GearmanWorker::setId - Give the worker an identifier so it can be tracked when asking gearmand for the list of available workers.
    • GearmanWorker::setOptions - Set worker options
    • GearmanWorker::setTimeout - Set socket I/O activity timeout
    • GearmanWorker::timeout - Get socket I/O activity timeout
    • GearmanWorker::unregister - Unregister a function name with the job servers
    • GearmanWorker::unregisterAll - Unregister all function names with the job servers
    • GearmanWorker::wait - Wait for activity from one of the job servers
    • GearmanWorker::work - Wait for and perform jobs
    • GearmanWorker::__construct - Create a GearmanWorker instance
    • Gender\Gender::connect - Connect to an external name dictionary.
    • Gender\Gender::country - Get textual country representation
    • Gender\Gender::get - Get gender of a name.
    • Gender\Gender::isNick - Check if the name0 is an alias of the name1.
    • Gender\Gender::similarNames - Get similar names.
    • Gender\Gender::__construct - Construct the Gender object.
    • Generator::current - Get the yielded value
    • Generator::key - Get the yielded key
    • Generator::next - Resume execution of the generator
    • Generator::rewind - Rewind the iterator
    • Generator::send - Send a value to the generator
    • Generator::throw - Throw an exception into the generator
    • Generator::valid - Check if the iterator has been closed
    • Generator::__wakeup - Serialize callback
    • geoip_asnum_by_name - Get the Autonomous System Numbers (ASN)
    • geoip_continent_code_by_name - Get the two letter continent code
    • geoip_country_code3_by_name - Get the three letter country code
    • geoip_country_code_by_name - Get the two letter country code
    • geoip_country_name_by_name - Get the full country name
    • geoip_database_info - Get GeoIP Database information
    • geoip_db_avail - Determine if GeoIP Database is available
    • geoip_db_filename - Returns the filename of the corresponding GeoIP Database
    • geoip_db_get_all_info - Returns detailed information about all GeoIP database types
    • geoip_domain_by_name - Get the second level domain name
    • geoip_id_by_name - Get the Internet connection type
    • geoip_isp_by_name - Get the Internet Service Provider (ISP) name
    • geoip_netspeedcell_by_name - Get the Internet connection speed
    • geoip_org_by_name - Get the organization name
    • geoip_record_by_name - Returns the detailed City information found in the GeoIP Database
    • geoip_region_by_name - Get the country code and region
    • geoip_region_name_by_code - Returns the region name for some country and region code combo
    • geoip_setup_custom_directory - Set a custom directory for the GeoIP database.
    • geoip_time_zone_by_country_and_region - Returns the time zone for some country and region code combo
    • getallheaders - 모든 HTTP 요청 헤더를 가져옵니다
    • getcwd - 현재 작업 디렉토리를 얻음
    • getdate - 날짜/시간 정보를 가져온다
    • getenv - 환경 변수 값을 얻습니다
    • gethostbyaddr - Get the Internet host name corresponding to a given IP address
    • gethostbyname - Get the IPv4 address corresponding to a given Internet host name
    • gethostbynamel - Get a list of IPv4 addresses corresponding to a given Internet host name
    • gethostname - Gets the host name
    • getimagesize - Get the size of an image
    • getimagesizefromstring - Get the size of an image from a string
    • getlastmod - 페이지를 수정한 최종 시간을 얻습니다
    • getmxrr - Get MX records corresponding to a given Internet host name
    • getmygid - Get PHP script owner's GID
    • getmyinode - Gets the inode of the current script
    • getmypid - Gets PHP's process ID
    • getmyuid - Gets PHP script owner's UID
    • getopt - Gets options from the command line argument list
    • getprotobyname - Get protocol number associated with protocol name
    • getprotobynumber - Get protocol name associated with protocol number
    • getrandmax - 가능한 최대 난수값을 표시
    • getrusage - Gets the current resource usages
    • getservbyname - Get port number associated with an Internet service and protocol
    • getservbyport - Get Internet service which corresponds to port and protocol
    • gettext - Lookup a message in the current domain
    • gettimeofday - Get current time
    • gettype - 변수의 자료형을 얻습니다
    • get_browser - Tells what the user's browser is capable of
    • get_called_class - "Late Static Binding" 클래스명
    • get_cfg_var - PHP 설정 옵션의 값 얻기
    • get_class - 객체의 클래스명을 반환
    • get_class_methods - 클래스 메쏘드명을 얻습니다
    • get_class_vars - 클래스의 기본 프로퍼티를 얻습니다
    • get_current_user - 현재 PHP 스크립트 소유자의 이름을 얻습니다
    • get_declared_classes - 선언된 클래스명을 배열로 반환
    • get_declared_interfaces - 선언된 모든 인터페이스의 배열을 반환
    • get_declared_traits - Returns an array of all declared traits
    • get_defined_constants - 모든 상수와 그 값의 연관 배열을 반환합니다
    • get_defined_functions - Returns an array of all defined functions
    • get_defined_vars - 모든 정의된 변수의 배열을 반환
    • get_extension_funcs - 모듈 함수명을 배열로 반환
    • get_headers - Fetches all the headers sent by the server in response to a HTTP request
    • get_html_translation_table - htmlspecialchars와 htmlentities에서 사용하는 변환표를 반환합니다
    • get_included_files - include나 require한 파일명의 배열을 반환
    • get_include_path - Gets the current include_path configuration option
    • get_loaded_extensions - 적재된 모든 모듈명을 배열로 반환합니다
    • get_magic_quotes_gpc - Gets the current configuration setting of magic_quotes_gpc
    • get_magic_quotes_runtime - Gets the current active configuration setting of magic_quotes_runtime
    • get_meta_tags - 파일에서 모든 메타 태그 내용 요소를 추출하여 배열로 반환
    • get_object_vars - 주어진 객체의 프로퍼티를 얻습니다
    • get_parent_class - 객체나 클래스의 부모 클래스명을 얻습니다
    • get_required_files - 별칭: get_included_files
    • get_resources - Returns active resources
    • get_resource_type - 자원형을 반환
    • glob - Find pathnames matching a pattern
    • glob:// - Find pathnames matching pattern
    • GlobIterator::count - Get the number of directories and files
    • GlobIterator::__construct - Construct a directory using glob
    • Gmagick::addimage - Adds new image to Gmagick object image list
    • Gmagick::addnoiseimage - Adds random noise to the image
    • Gmagick::annotateimage - Annotates an image with text
    • Gmagick::blurimage - Adds blur filter to image
    • Gmagick::borderimage - Surrounds the image with a border
    • Gmagick::charcoalimage - Simulates a charcoal drawing
    • Gmagick::chopimage - Removes a region of an image and trims
    • Gmagick::clear - Clears all resources associated to Gmagick object
    • Gmagick::commentimage - Adds a comment to your image
    • Gmagick::compositeimage - Composite one image onto another
    • Gmagick::cropimage - Extracts a region of the image
    • Gmagick::cropthumbnailimage - Creates a crop thumbnail
    • Gmagick::current - The current purpose
    • Gmagick::cyclecolormapimage - Displaces an image's colormap
    • Gmagick::deconstructimages - Returns certain pixel differences between images
    • Gmagick::despeckleimage - The despeckleimage purpose
    • Gmagick::destroy - The destroy purpose
    • Gmagick::drawimage - Renders the GmagickDraw object on the current image
    • Gmagick::edgeimage - Enhance edges within the image
    • Gmagick::embossimage - Returns a grayscale image with a three-dimensional effect
    • Gmagick::enhanceimage - Improves the quality of a noisy image
    • Gmagick::equalizeimage - Equalizes the image histogram
    • Gmagick::flipimage - Creates a vertical mirror image
    • Gmagick::flopimage - The flopimage purpose
    • Gmagick::frameimage - Adds a simulated three-dimensional border
    • Gmagick::gammaimage - Gamma-corrects an image
    • Gmagick::getcopyright - Returns the GraphicsMagick API copyright as a string
    • Gmagick::getfilename - The filename associated with an image sequence
    • Gmagick::getimagebackgroundcolor - Returns the image background color
    • Gmagick::getimageblueprimary - Returns the chromaticy blue primary point
    • Gmagick::getimagebordercolor - Returns the image border color
    • Gmagick::getimagechanneldepth - Gets the depth for a particular image channel
    • Gmagick::getimagecolors - Returns the color of the specified colormap index
    • Gmagick::getimagecolorspace - Gets the image colorspace
    • Gmagick::getimagecompose - Returns the composite operator associated with the image
    • Gmagick::getimagedelay - Gets the image delay
    • Gmagick::getimagedepth - Gets the depth of the image
    • Gmagick::getimagedispose - Gets the image disposal method
    • Gmagick::getimageextrema - Gets the extrema for the image
    • Gmagick::getimagefilename - Returns the filename of a particular image in a sequence
    • Gmagick::getimageformat - Returns the format of a particular image in a sequence
    • Gmagick::getimagegamma - Gets the image gamma
    • Gmagick::getimagegreenprimary - Returns the chromaticy green primary point
    • Gmagick::getimageheight - Returns the image height
    • Gmagick::getimagehistogram - Gets the image histogram
    • Gmagick::getimageindex - Gets the index of the current active image
    • Gmagick::getimageinterlacescheme - Gets the image interlace scheme
    • Gmagick::getimageiterations - Gets the image iterations
    • Gmagick::getimagematte - Check if the image has a matte channel
    • Gmagick::getimagemattecolor - Returns the image matte color
    • Gmagick::getimageprofile - Returns the named image profile.
    • Gmagick::getimageredprimary - Returns the chromaticity red primary point
    • Gmagick::getimagerenderingintent - Gets the image rendering intent
    • Gmagick::getimageresolution - Gets the image X and Y resolution
    • Gmagick::getimagescene - Gets the image scene
    • Gmagick::getimagesignature - Generates an SHA-256 message digest
    • Gmagick::getimagetype - Gets the potential image type.
    • Gmagick::getimageunits - Gets the image units of resolution
    • Gmagick::getimagewhitepoint - Returns the chromaticity white point
    • Gmagick::getimagewidth - Returns the width of the image
    • Gmagick::getpackagename - Returns the GraphicsMagick package name.
    • Gmagick::getquantumdepth - Returns the Gmagick quantum depth as a string.
    • Gmagick::getreleasedate - Returns the GraphicsMagick release date as a string.
    • Gmagick::getsamplingfactors - Gets the horizontal and vertical sampling factor.
    • Gmagick::getsize - Returns the size associated with the Gmagick object
    • Gmagick::getversion - Returns the GraphicsMagick API version
    • Gmagick::hasnextimage - Checks if the object has more images
    • Gmagick::haspreviousimage - Checks if the object has a previous image
    • Gmagick::implodeimage - Creates a new image as a copy
    • Gmagick::labelimage - Adds a label to an image.
    • Gmagick::levelimage - Adjusts the levels of an image
    • Gmagick::magnifyimage - Scales an image proportionally 2x
    • Gmagick::mapimage - Replaces the colors of an image with the closest color from a reference image.
    • Gmagick::medianfilterimage - Applies a digital filter
    • Gmagick::minifyimage - Scales an image proportionally to half its size
    • Gmagick::modulateimage - Control the brightness, saturation, and hue
    • Gmagick::motionblurimage - Simulates motion blur
    • Gmagick::newimage - Creates a new image
    • Gmagick::nextimage - Moves to the next image
    • Gmagick::normalizeimage - Enhances the contrast of a color image
    • Gmagick::oilpaintimage - Simulates an oil painting
    • Gmagick::previousimage - Move to the previous image in the object
    • Gmagick::profileimage - Adds or removes a profile from an image
    • Gmagick::quantizeimage - Analyzes the colors within a reference image
    • Gmagick::quantizeimages - The quantizeimages purpose
    • Gmagick::queryfontmetrics - Returns an array representing the font metrics
    • Gmagick::queryfonts - Returns the configured fonts
    • Gmagick::queryformats - Returns formats supported by Gmagick.
    • Gmagick::radialblurimage - Radial blurs an image
    • Gmagick::raiseimage - Creates a simulated 3d button-like effect
    • Gmagick::read - Reads image from filename
    • Gmagick::readimage - Reads image from filename
    • Gmagick::readimageblob - Reads image from a binary string
    • Gmagick::readimagefile - The readimagefile purpose
    • Gmagick::reducenoiseimage - Smooths the contours of an image
    • Gmagick::removeimage - Removes an image from the image list
    • Gmagick::removeimageprofile - Removes the named image profile and returns it
    • Gmagick::resampleimage - Resample image to desired resolution
    • Gmagick::resizeimage - Scales an image
    • Gmagick::rollimage - Offsets an image
    • Gmagick::rotateimage - Rotates an image
    • Gmagick::scaleimage - Scales the size of an image
    • Gmagick::separateimagechannel - Separates a channel from the image
    • Gmagick::setCompressionQuality - Sets the object's default compression quality
    • Gmagick::setfilename - Sets the filename before you read or write the image
    • Gmagick::setimagebackgroundcolor - Sets the image background color.
    • Gmagick::setimageblueprimary - Sets the image chromaticity blue primary point.
    • Gmagick::setimagebordercolor - Sets the image border color.
    • Gmagick::setimagechanneldepth - Sets the depth of a particular image channel
    • Gmagick::setimagecolorspace - Sets the image colorspace
    • Gmagick::setimagecompose - Sets the image composite operator
    • Gmagick::setimagedelay - Sets the image delay
    • Gmagick::setimagedepth - Sets the image depth
    • Gmagick::setimagedispose - Sets the image disposal method
    • Gmagick::setimagefilename - Sets the filename of a particular image in a sequence
    • Gmagick::setimageformat - Sets the format of a particular image
    • Gmagick::setimagegamma - Sets the image gamma
    • Gmagick::setimagegreenprimary - Sets the image chromaticity green primary point.
    • Gmagick::setimageindex - Set the iterator to the position in the image list specified with the index parameter
    • Gmagick::setimageinterlacescheme - Sets the interlace scheme of the image.
    • Gmagick::setimageiterations - Sets the image iterations.
    • Gmagick::setimageprofile - Adds a named profile to the Gmagick object
    • Gmagick::setimageredprimary - Sets the image chromaticity red primary point.
    • Gmagick::setimagerenderingintent - Sets the image rendering intent
    • Gmagick::setimageresolution - Sets the image resolution
    • Gmagick::setimagescene - Sets the image scene
    • Gmagick::setimagetype - Sets the image type
    • Gmagick::setimageunits - Sets the image units of resolution.
    • Gmagick::setimagewhitepoint - Sets the image chromaticity white point.
    • Gmagick::setsamplingfactors - Sets the image sampling factors.
    • Gmagick::setsize - Sets the size of the Gmagick object
    • Gmagick::shearimage - Creating a parallelogram
    • Gmagick::solarizeimage - Applies a solarizing effect to the image
    • Gmagick::spreadimage - Randomly displaces each pixel in a block
    • Gmagick::stripimage - Strips an image of all profiles and comments
    • Gmagick::swirlimage - Swirls the pixels about the center of the image
    • Gmagick::thumbnailimage - Changes the size of an image
    • Gmagick::trimimage - Remove edges from the image
    • Gmagick::write - 별칭: Gmagick::writeimage
    • Gmagick::writeimage - Writes an image to the specified filename
    • Gmagick::__construct - The Gmagick constructor
    • GmagickDraw::annotate - Draws text on the image
    • GmagickDraw::arc - Draws an arc
    • GmagickDraw::bezier - Draws a bezier curve
    • GmagickDraw::ellipse - Draws an ellipse on the image
    • GmagickDraw::getfillcolor - Returns the fill color
    • GmagickDraw::getfillopacity - Returns the opacity used when drawing
    • GmagickDraw::getfont - Returns the font
    • GmagickDraw::getfontsize - Returns the font pointsize
    • GmagickDraw::getfontstyle - Returns the font style
    • GmagickDraw::getfontweight - Returns the font weight
    • GmagickDraw::getstrokecolor - Returns the color used for stroking object outlines
    • GmagickDraw::getstrokeopacity - Returns the opacity of stroked object outlines
    • GmagickDraw::getstrokewidth - Returns the width of the stroke used to draw object outlines
    • GmagickDraw::gettextdecoration - Returns the text decoration
    • GmagickDraw::gettextencoding - Returns the code set used for text annotations
    • GmagickDraw::line - The line purpose
    • GmagickDraw::point - Draws a point
    • GmagickDraw::polygon - Draws a polygon
    • GmagickDraw::polyline - Draws a polyline
    • GmagickDraw::rectangle - Draws a rectangle
    • GmagickDraw::rotate - Applies the specified rotation to the current coordinate space
    • GmagickDraw::roundrectangle - Draws a rounded rectangle
    • GmagickDraw::scale - Adjusts the scaling factor
    • GmagickDraw::setfillcolor - Sets the fill color to be used for drawing filled objects.
    • GmagickDraw::setfillopacity - The setfillopacity purpose
    • GmagickDraw::setfont - Sets the fully-specified font to use when annotating with text.
    • GmagickDraw::setfontsize - Sets the font pointsize to use when annotating with text.
    • GmagickDraw::setfontstyle - Sets the font style to use when annotating with text
    • GmagickDraw::setfontweight - Sets the font weight
    • GmagickDraw::setstrokecolor - Sets the color used for stroking object outlines.
    • GmagickDraw::setstrokeopacity - Specifies the opacity of stroked object outlines.
    • GmagickDraw::setstrokewidth - Sets the width of the stroke used to draw object outlines.
    • GmagickDraw::settextdecoration - Specifies a decoration
    • GmagickDraw::settextencoding - Specifies the text code set
    • GmagickPixel::getcolor - Returns the color
    • GmagickPixel::getcolorcount - Returns the color count associated with this color
    • GmagickPixel::getcolorvalue - Gets the normalized value of the provided color channel
    • GmagickPixel::setcolor - Sets the color
    • GmagickPixel::setcolorvalue - Sets the normalized value of one of the channels
    • GmagickPixel::__construct - The GmagickPixel constructor
    • gmdate - Format a GMT/UTC date/time
    • gmmktime - Get Unix timestamp for a GMT date
    • gmp_abs - Absolute value
    • gmp_add - Add numbers
    • gmp_and - Bitwise AND
    • gmp_clrbit - Clear bit
    • gmp_cmp - Compare numbers
    • gmp_com - Calculates one's complement
    • gmp_div - 별칭: gmp_div_q
    • gmp_divexact - Exact division of numbers
    • gmp_div_q - Divide numbers
    • gmp_div_qr - Divide numbers and get quotient and remainder
    • gmp_div_r - Remainder of the division of numbers
    • gmp_export - Export to a binary string
    • gmp_fact - Factorial
    • gmp_gcd - Calculate GCD
    • gmp_gcdext - Calculate GCD and multipliers
    • gmp_hamdist - Hamming distance
    • gmp_import - Import from a binary string
    • gmp_init - Create GMP number
    • gmp_intval - Convert GMP number to integer
    • gmp_invert - Inverse by modulo
    • gmp_jacobi - Jacobi symbol
    • gmp_legendre - Legendre symbol
    • gmp_mod - Modulo operation
    • gmp_mul - Multiply numbers
    • gmp_neg - Negate number
    • gmp_nextprime - Find next prime number
    • gmp_or - Bitwise OR
    • gmp_perfect_square - Perfect square check
    • gmp_popcount - Population count
    • gmp_pow - Raise number into power
    • gmp_powm - Raise number into power with modulo
    • gmp_prob_prime - Check if number is "probably prime"
    • gmp_random - Random number
    • gmp_random_bits - Random number
    • gmp_random_range - Random number
    • gmp_random_seed - Sets the RNG seed
    • gmp_root - Take the integer part of nth root
    • gmp_rootrem - Take the integer part and remainder of nth root
    • gmp_scan0 - Scan for 0
    • gmp_scan1 - Scan for 1
    • gmp_setbit - Set bit
    • gmp_sign - Sign of number
    • gmp_sqrt - Calculate square root
    • gmp_sqrtrem - Square root with remainder
    • gmp_strval - Convert GMP number to string
    • gmp_sub - Subtract numbers
    • gmp_testbit - Tests if a bit is set
    • gmp_xor - Bitwise XOR
    • gmstrftime - Format a GMT/UTC time/date according to locale settings
    • gnupg_adddecryptkey - Add a key for decryption
    • gnupg_addencryptkey - Add a key for encryption
    • gnupg_addsignkey - Add a key for signing
    • gnupg_cleardecryptkeys - Removes all keys which were set for decryption before
    • gnupg_clearencryptkeys - Removes all keys which were set for encryption before
    • gnupg_clearsignkeys - Removes all keys which were set for signing before
    • gnupg_decrypt - Decrypts a given text
    • gnupg_decryptverify - Decrypts and verifies a given text
    • gnupg_encrypt - Encrypts a given text
    • gnupg_encryptsign - Encrypts and signs a given text
    • gnupg_export - Exports a key
    • gnupg_geterror - Returns the errortext, if a function fails
    • gnupg_getprotocol - Returns the currently active protocol for all operations
    • gnupg_import - Imports a key
    • gnupg_init - Initialize a connection
    • gnupg_keyinfo - Returns an array with information about all keys that matches the given pattern
    • gnupg_setarmor - Toggle armored output
    • gnupg_seterrormode - Sets the mode for error_reporting
    • gnupg_setsignmode - Sets the mode for signing
    • gnupg_sign - Signs a given text
    • gnupg_verify - Verifies a signed text
    • gopher_parsedir - Translate a gopher formatted directory entry into an associative array.
    • grapheme_extract - Function to extract a sequence of default grapheme clusters from a text buffer, which must be encoded in UTF-8.
    • grapheme_stripos - Find position (in grapheme units) of first occurrence of a case-insensitive string
    • grapheme_stristr - Returns part of haystack string from the first occurrence of case-insensitive needle to the end of haystack.
    • grapheme_strlen - Get string length in grapheme units
    • grapheme_strpos - Find position (in grapheme units) of first occurrence of a string
    • grapheme_strripos - Find position (in grapheme units) of last occurrence of a case-insensitive string
    • grapheme_strrpos - Find position (in grapheme units) of last occurrence of a string
    • grapheme_strstr - Returns part of haystack string from the first occurrence of needle to the end of haystack.
    • grapheme_substr - Return part of a string
    • gregoriantojd - Converts a Gregorian date to Julian Day Count
    • gupnp_context_get_host_ip - Get the IP address
    • gupnp_context_get_port - Get the port
    • gupnp_context_get_subscription_timeout - Get the event subscription timeout
    • gupnp_context_host_path - Start hosting
    • gupnp_context_new - Create a new context
    • gupnp_context_set_subscription_timeout - Sets the event subscription timeout
    • gupnp_context_timeout_add - Sets a function to be called at regular intervals
    • gupnp_context_unhost_path - Stop hosting
    • gupnp_control_point_browse_start - Start browsing
    • gupnp_control_point_browse_stop - Stop browsing
    • gupnp_control_point_callback_set - Set control point callback
    • gupnp_control_point_new - Create a new control point
    • gupnp_device_action_callback_set - Set device callback function
    • gupnp_device_info_get - Get info of root device
    • gupnp_device_info_get_service - Get the service with type
    • gupnp_root_device_get_available - Check whether root device is available
    • gupnp_root_device_get_relative_location - Get the relative location of root device.
    • gupnp_root_device_new - Create a new root device
    • gupnp_root_device_set_available - Set whether or not root_device is available
    • gupnp_root_device_start - Start main loop
    • gupnp_root_device_stop - Stop main loop
    • gupnp_service_action_get - Retrieves the specified action arguments
    • gupnp_service_action_return - Return successfully
    • gupnp_service_action_return_error - Return error code
    • gupnp_service_action_set - Sets the specified action return values
    • gupnp_service_freeze_notify - Freeze new notifications
    • gupnp_service_info_get - Get full info of service
    • gupnp_service_info_get_introspection - Get resource introspection of service
    • gupnp_service_introspection_get_state_variable - Returns the state variable data
    • gupnp_service_notify - Notifies listening clients
    • gupnp_service_proxy_action_get - Send action to the service and get value
    • gupnp_service_proxy_action_set - Send action to the service and set value
    • gupnp_service_proxy_add_notify - Sets up callback for variable change notification
    • gupnp_service_proxy_callback_set - Set service proxy callback for signal
    • gupnp_service_proxy_get_subscribed - Check whether subscription is valid to the service
    • gupnp_service_proxy_remove_notify - Cancels the variable change notification
    • gupnp_service_proxy_send_action - Send action with multiple parameters synchronously
    • gupnp_service_proxy_set_subscribed - (Un)subscribes to the service.
    • gupnp_service_thaw_notify - Sends out any pending notifications and stops queuing of new ones.
    • gzclose - Close an open gz-file pointer
    • gzcompress - Compress a string
    • gzdecode - Decodes a gzip compressed string
    • gzdeflate - Deflate a string
    • gzencode - Create a gzip compressed string
    • gzeof - Test for EOF on a gz-file pointer
    • gzfile - Read entire gz-file into an array
    • gzgetc - Get character from gz-file pointer
    • gzgets - Get line from file pointer
    • gzgetss - Get line from gz-file pointer and strip HTML tags
    • gzinflate - Inflate a deflated string
    • gzopen - Open gz-file
    • gzpassthru - Output all remaining data on a gz-file pointer
    • gzputs - 별칭: gzwrite
    • gzread - Binary-safe gz-file read
    • gzrewind - Rewind the position of a gz-file pointer
    • gzseek - Seek on a gz-file pointer
    • gztell - Tell gz-file pointer read/write position
    • gzuncompress - Uncompress a compressed string
    • gzwrite - Binary-safe gz-file write
  • h
    • HaruAnnotation::setBorderStyle - Set the border style of the annotation
    • HaruAnnotation::setHighlightMode - Set the highlighting mode of the annotation
    • HaruAnnotation::setIcon - Set the icon style of the annotation
    • HaruAnnotation::setOpened - Set the initial state of the annotation
    • HaruDestination::setFit - Set the appearance of the page to fit the window
    • HaruDestination::setFitB - Set the appearance of the page to fit the bounding box of the page within the window
    • HaruDestination::setFitBH - Set the appearance of the page to fit the width of the bounding box
    • HaruDestination::setFitBV - Set the appearance of the page to fit the height of the boudning box
    • HaruDestination::setFitH - Set the appearance of the page to fit the window width
    • HaruDestination::setFitR - Set the appearance of the page to fit the specified rectangle
    • HaruDestination::setFitV - Set the appearance of the page to fit the window height
    • HaruDestination::setXYZ - Set the appearance of the page
    • HaruDoc::addPage - Add new page to the document
    • HaruDoc::addPageLabel - Set the numbering style for the specified range of pages
    • HaruDoc::createOutline - Create a HaruOutline instance
    • HaruDoc::getCurrentEncoder - Get HaruEncoder currently used in the document
    • HaruDoc::getCurrentPage - Return current page of the document
    • HaruDoc::getEncoder - Get HaruEncoder instance for the specified encoding
    • HaruDoc::getFont - Get HaruFont instance
    • HaruDoc::getInfoAttr - Get current value of the specified document attribute
    • HaruDoc::getPageLayout - Get current page layout
    • HaruDoc::getPageMode - Get current page mode
    • HaruDoc::getStreamSize - Get the size of the temporary stream
    • HaruDoc::insertPage - Insert new page just before the specified page
    • HaruDoc::loadJPEG - Load a JPEG image
    • HaruDoc::loadPNG - Load PNG image and return HaruImage instance
    • HaruDoc::loadRaw - Load a RAW image
    • HaruDoc::loadTTC - Load the font with the specified index from TTC file
    • HaruDoc::loadTTF - Load TTF font file
    • HaruDoc::loadType1 - Load Type1 font
    • HaruDoc::output - Write the document data to the output buffer
    • HaruDoc::readFromStream - Read data from the temporary stream
    • HaruDoc::resetError - Reset error state of the document handle
    • HaruDoc::resetStream - Rewind the temporary stream
    • HaruDoc::save - Save the document into the specified file
    • HaruDoc::saveToStream - Save the document into a temporary stream
    • HaruDoc::setCompressionMode - Set compression mode for the document
    • HaruDoc::setCurrentEncoder - Set the current encoder for the document
    • HaruDoc::setEncryptionMode - Set encryption mode for the document
    • HaruDoc::setInfoAttr - Set the info attribute of the document
    • HaruDoc::setInfoDateAttr - Set the datetime info attributes of the document
    • HaruDoc::setOpenAction - Define which page is shown when the document is opened
    • HaruDoc::setPageLayout - Set how pages should be displayed
    • HaruDoc::setPageMode - Set how the document should be displayed
    • HaruDoc::setPagesConfiguration - Set the number of pages per set of pages
    • HaruDoc::setPassword - Set owner and user passwords for the document
    • HaruDoc::setPermission - Set permissions for the document
    • HaruDoc::useCNSEncodings - Enable Chinese simplified encodings
    • HaruDoc::useCNSFonts - Enable builtin Chinese simplified fonts
    • HaruDoc::useCNTEncodings - Enable Chinese traditional encodings
    • HaruDoc::useCNTFonts - Enable builtin Chinese traditional fonts
    • HaruDoc::useJPEncodings - Enable Japanese encodings
    • HaruDoc::useJPFonts - Enable builtin Japanese fonts
    • HaruDoc::useKREncodings - Enable Korean encodings
    • HaruDoc::useKRFonts - Enable builtin Korean fonts
    • HaruDoc::__construct - Construct new HaruDoc instance
    • HaruEncoder::getByteType - Get the type of the byte in the text
    • HaruEncoder::getType - Get the type of the encoder
    • HaruEncoder::getUnicode - Convert the specified character to unicode
    • HaruEncoder::getWritingMode - Get the writing mode of the encoder
    • HaruFont::getAscent - Get the vertical ascent of the font
    • HaruFont::getCapHeight - Get the distance from the baseline of uppercase letters
    • HaruFont::getDescent - Get the vertical descent of the font
    • HaruFont::getEncodingName - Get the name of the encoding
    • HaruFont::getFontName - Get the name of the font
    • HaruFont::getTextWidth - Get the total width of the text, number of characters, number of words and number of spaces
    • HaruFont::getUnicodeWidth - Get the width of the character in the font
    • HaruFont::getXHeight - Get the distance from the baseline of lowercase letters
    • HaruFont::measureText - Calculate the number of characters which can be included within the specified width
    • HaruImage::getBitsPerComponent - Get the number of bits used to describe each color component of the image
    • HaruImage::getColorSpace - Get the name of the color space
    • HaruImage::getHeight - Get the height of the image
    • HaruImage::getSize - Get size of the image
    • HaruImage::getWidth - Get the width of the image
    • HaruImage::setColorMask - Set the color mask of the image
    • HaruImage::setMaskImage - Set the image mask
    • HaruOutline::setDestination - Set the destination for the outline
    • HaruOutline::setOpened - Set the initial state of the outline
    • HaruPage::arc - Append an arc to the current path
    • HaruPage::beginText - Begin a text object and set the current text position to (0,0)
    • HaruPage::circle - Append a circle to the current path
    • HaruPage::closePath - Append a straight line from the current point to the start point of the path
    • HaruPage::concat - Concatenate current transformation matrix of the page and the specified matrix
    • HaruPage::createDestination - Create new HaruDestination instance
    • HaruPage::createLinkAnnotation - Create new HaruAnnotation instance
    • HaruPage::createTextAnnotation - Create new HaruAnnotation instance
    • HaruPage::createURLAnnotation - Create and return new HaruAnnotation instance
    • HaruPage::curveTo - Append a Bezier curve to the current path
    • HaruPage::curveTo2 - Append a Bezier curve to the current path
    • HaruPage::curveTo3 - Append a Bezier curve to the current path
    • HaruPage::drawImage - Show image at the page
    • HaruPage::ellipse - Append an ellipse to the current path
    • HaruPage::endPath - End current path object without filling and painting operations
    • HaruPage::endText - End current text object
    • HaruPage::eofill - Fill current path using even-odd rule
    • HaruPage::eoFillStroke - Fill current path using even-odd rule, then paint the path
    • HaruPage::fill - Fill current path using nonzero winding number rule
    • HaruPage::fillStroke - Fill current path using nonzero winding number rule, then paint the path
    • HaruPage::getCharSpace - Get the current value of character spacing
    • HaruPage::getCMYKFill - Get the current filling color
    • HaruPage::getCMYKStroke - Get the current stroking color
    • HaruPage::getCurrentFont - Get the currently used font
    • HaruPage::getCurrentFontSize - Get the current font size
    • HaruPage::getCurrentPos - Get the current position for path painting
    • HaruPage::getCurrentTextPos - Get the current position for text printing
    • HaruPage::getDash - Get the current dash pattern
    • HaruPage::getFillingColorSpace - Get the current filling color space
    • HaruPage::getFlatness - Get the flatness of the page
    • HaruPage::getGMode - Get the current graphics mode
    • HaruPage::getGrayFill - Get the current filling color
    • HaruPage::getGrayStroke - Get the current stroking color
    • HaruPage::getHeight - Get the height of the page
    • HaruPage::getHorizontalScaling - Get the current value of horizontal scaling
    • HaruPage::getLineCap - Get the current line cap style
    • HaruPage::getLineJoin - Get the current line join style
    • HaruPage::getLineWidth - Get the current line width
    • HaruPage::getMiterLimit - Get the value of miter limit
    • HaruPage::getRGBFill - Get the current filling color
    • HaruPage::getRGBStroke - Get the current stroking color
    • HaruPage::getStrokingColorSpace - Get the current stroking color space
    • HaruPage::getTextLeading - Get the current value of line spacing
    • HaruPage::getTextMatrix - Get the current text transformation matrix of the page
    • HaruPage::getTextRenderingMode - Get the current text rendering mode
    • HaruPage::getTextRise - Get the current value of text rising
    • HaruPage::getTextWidth - Get the width of the text using current fontsize, character spacing and word spacing
    • HaruPage::getTransMatrix - Get the current transformation matrix of the page
    • HaruPage::getWidth - Get the width of the page
    • HaruPage::getWordSpace - Get the current value of word spacing
    • HaruPage::lineTo - Draw a line from the current point to the specified point
    • HaruPage::measureText - Calculate the byte length of characters which can be included on one line of the specified width
    • HaruPage::moveTextPos - Move text position to the specified offset
    • HaruPage::moveTo - Set starting point for new drawing path
    • HaruPage::moveToNextLine - Move text position to the start of the next line
    • HaruPage::rectangle - Append a rectangle to the current path
    • HaruPage::setCharSpace - Set character spacing for the page
    • HaruPage::setCMYKFill - Set filling color for the page
    • HaruPage::setCMYKStroke - Set stroking color for the page
    • HaruPage::setDash - Set the dash pattern for the page
    • HaruPage::setFlatness - Set flatness for the page
    • HaruPage::setFontAndSize - Set font and fontsize for the page
    • HaruPage::setGrayFill - Set filling color for the page
    • HaruPage::setGrayStroke - Sets stroking color for the page
    • HaruPage::setHeight - Set height of the page
    • HaruPage::setHorizontalScaling - Set horizontal scaling for the page
    • HaruPage::setLineCap - Set the shape to be used at the ends of lines
    • HaruPage::setLineJoin - Set line join style for the page
    • HaruPage::setLineWidth - Set line width for the page
    • HaruPage::setMiterLimit - Set the current value of the miter limit of the page
    • HaruPage::setRGBFill - Set filling color for the page
    • HaruPage::setRGBStroke - Set stroking color for the page
    • HaruPage::setRotate - Set rotation angle of the page
    • HaruPage::setSize - Set size and direction of the page
    • HaruPage::setSlideShow - Set transition style for the page
    • HaruPage::setTextLeading - Set text leading (line spacing) for the page
    • HaruPage::setTextMatrix - Set the current text transformation matrix of the page
    • HaruPage::setTextRenderingMode - Set text rendering mode for the page
    • HaruPage::setTextRise - Set the current value of text rising
    • HaruPage::setWidth - Set width of the page
    • HaruPage::setWordSpace - Set word spacing for the page
    • HaruPage::showText - Print text at the current position of the page
    • HaruPage::showTextNextLine - Move the current position to the start of the next line and print the text
    • HaruPage::stroke - Paint current path
    • HaruPage::textOut - Print the text on the specified position
    • HaruPage::textRect - Print the text inside the specified region
    • hash - Generate a hash value (message digest)
    • hash_algos - Return a list of registered hashing algorithms
    • hash_copy - Copy hashing context
    • hash_equals - Timing attack safe string comparison
    • hash_file - Generate a hash value using the contents of a given file
    • hash_final - Finalize an incremental hash and return resulting digest
    • hash_hmac - Generate a keyed hash value using the HMAC method
    • hash_hmac_file - Generate a keyed hash value using the HMAC method and the contents of a given file
    • hash_init - Initialize an incremental hashing context
    • hash_pbkdf2 - Generate a PBKDF2 key derivation of a supplied password
    • hash_update - Pump data into an active hashing context
    • hash_update_file - Pump data into an active hashing context from a file
    • hash_update_stream - Pump data into an active hashing context from an open stream
    • header - Send a raw HTTP header
    • headers_list - Returns a list of response headers sent (or ready to send)
    • headers_sent - Checks if or where headers have been sent
    • header_register_callback - Call a header function
    • header_remove - Remove previously set headers
    • hebrev - 논리 히브리 텍스트를 표시 텍스트로 변환
    • hebrevc - 줄바꿈을 포함하여 논리 히브리 텍스트를 표시 텍스트로 변환
    • hex2bin - Decodes a hexadecimally encoded binary string
    • hexdec - 16진수를 10진수로
    • highlight_file - Syntax highlighting of a file
    • highlight_string - Syntax highlighting of a string
    • HRTime\PerformanceCounter::getElapsedTicks - Get elapsed ticks for all intervals.
    • HRTime\PerformanceCounter::getFrequency - Timer frequency in ticks per second
    • HRTime\PerformanceCounter::getLastElapsedTicks - Get elapsed ticks for the last interval.
    • HRTime\PerformanceCounter::isRunning - Whether the measurement is running.
    • HRTime\PerformanceCounter::start - Start time measurement
    • HRTime\PerformanceCounter::stop - Stop time measurement
    • HRTime\StopWatch::getElapsedTime - Get elapsed time for all intervals.
    • HRTime\StopWatch::getLastElapsedTime - Get elapsed time for the last interval.
    • htmlentities - 해당하는 모든 문자를 HTML 엔티티로 변환
    • htmlspecialchars - 특수 문자를 HTML 엔터티로 변환
    • htmlspecialchars_decode - Convert special HTML entities back to characters
    • html_entity_decode - 모든 HTML 엔티티를 해당하는 문자로 변환
    • http:// - Accessing HTTP(s) URLs
    • HTTP context options - HTTP context option listing
    • http_build_query - URL 인코드한 쿼리 문자열 생성
    • http_response_code - Get or Set the HTTP response code
    • hwapi_attribute_new - Creates instance of class hw_api_attribute
    • hwapi_content_new - Create new instance of class hw_api_content
    • hwapi_hgcsp - Returns object of class hw_api
    • hwapi_object_new - Creates a new instance of class hwapi_object_new
    • hw_api::checkin - Checks in an object
    • hw_api::checkout - Checks out an object
    • hw_api::children - Returns children of an object
    • hw_api::content - Returns content of an object
    • hw_api::copy - Copies physically
    • hw_api::dbstat - Returns statistics about database server
    • hw_api::dcstat - Returns statistics about document cache server
    • hw_api::dstanchors - Returns a list of all destination anchors
    • hw_api::dstofsrcanchor - Returns destination of a source anchor
    • hw_api::find - Search for objects
    • hw_api::ftstat - Returns statistics about fulltext server
    • hw_api::hwstat - Returns statistics about Hyperwave server
    • hw_api::identify - Log into Hyperwave Server
    • hw_api::info - Returns information about server configuration
    • hw_api::insert - Inserts a new object
    • hw_api::insertanchor - Inserts a new object of type anchor
    • hw_api::insertcollection - Inserts a new object of type collection
    • hw_api::insertdocument - Inserts a new object of type document
    • hw_api::link - Creates a link to an object
    • hw_api::lock - Locks an object
    • hw_api::move - Moves object between collections
    • hw_api::object - Retrieve attribute information
    • hw_api::objectbyanchor - Returns the object an anchor belongs to
    • hw_api::parents - Returns parents of an object
    • hw_api::remove - Delete an object
    • hw_api::replace - Replaces an object
    • hw_api::setcommittedversion - Commits version other than last version
    • hw_api::srcanchors - Returns a list of all source anchors
    • hw_api::srcsofdst - Returns source of a destination object
    • hw_api::unlock - Unlocks a locked object
    • hw_api::user - Returns the own user object
    • hw_api::userlist - Returns a list of all logged in users
    • hw_api_attribute::key - Returns key of the attribute
    • hw_api_attribute::langdepvalue - Returns value for a given language
    • hw_api_attribute::value - Returns value of the attribute
    • hw_api_attribute::values - Returns all values of the attribute
    • hw_api_content::mimetype - Returns mimetype
    • hw_api_content::read - Read content
    • hw_api_error::count - Returns number of reasons
    • hw_api_error::reason - Returns reason of error
    • hw_api_object::assign - Clones object
    • hw_api_object::attreditable - Checks whether an attribute is editable
    • hw_api_object::count - Returns number of attributes
    • hw_api_object::insert - Inserts new attribute
    • hw_api_object::remove - Removes attribute
    • hw_api_object::title - Returns the title attribute
    • hw_api_object::value - Returns value of attribute
    • hw_api_reason::description - Returns description of reason
    • hw_api_reason::type - Returns type of reason
    • hypot - Calculate the length of the hypotenuse of a right-angle triangle
  • i
    • ibase_add_user - Add a user to a security database
    • ibase_affected_rows - Return the number of rows that were affected by the previous query
    • ibase_backup - Initiates a backup task in the service manager and returns immediately
    • ibase_blob_add - Add data into a newly created blob
    • ibase_blob_cancel - Cancel creating blob
    • ibase_blob_close - Close blob
    • ibase_blob_create - Create a new blob for adding data
    • ibase_blob_echo - Output blob contents to browser
    • ibase_blob_get - Get len bytes data from open blob
    • ibase_blob_import - Create blob, copy file in it, and close it
    • ibase_blob_info - Return blob length and other useful info
    • ibase_blob_open - Open blob for retrieving data parts
    • ibase_close - Close a connection to an InterBase database
    • ibase_commit - Commit a transaction
    • ibase_commit_ret - Commit a transaction without closing it
    • ibase_connect - Open a connection to a database
    • ibase_db_info - Request statistics about a database
    • ibase_delete_user - Delete a user from a security database
    • ibase_drop_db - Drops a database
    • ibase_errcode - Return an error code
    • ibase_errmsg - Return error messages
    • ibase_execute - Execute a previously prepared query
    • ibase_fetch_assoc - Fetch a result row from a query as an associative array
    • ibase_fetch_object - Get an object from a InterBase database
    • ibase_fetch_row - Fetch a row from an InterBase database
    • ibase_field_info - Get information about a field
    • ibase_free_event_handler - Cancels a registered event handler
    • ibase_free_query - Free memory allocated by a prepared query
    • ibase_free_result - Free a result set
    • ibase_gen_id - Increments the named generator and returns its new value
    • ibase_maintain_db - Execute a maintenance command on the database server
    • ibase_modify_user - Modify a user to a security database
    • ibase_name_result - Assigns a name to a result set
    • ibase_num_fields - Get the number of fields in a result set
    • ibase_num_params - Return the number of parameters in a prepared query
    • ibase_param_info - Return information about a parameter in a prepared query
    • ibase_pconnect - Open a persistent connection to an InterBase database
    • ibase_prepare - Prepare a query for later binding of parameter placeholders and execution
    • ibase_query - Execute a query on an InterBase database
    • ibase_restore - Initiates a restore task in the service manager and returns immediately
    • ibase_rollback - Roll back a transaction
    • ibase_rollback_ret - Roll back a transaction without closing it
    • ibase_server_info - Request information about a database server
    • ibase_service_attach - Connect to the service manager
    • ibase_service_detach - Disconnect from the service manager
    • ibase_set_event_handler - Register a callback function to be called when events are posted
    • ibase_trans - Begin a transaction
    • ibase_wait_event - Wait for an event to be posted by the database
    • iconv - Convert string to requested character encoding
    • iconv_get_encoding - Retrieve internal configuration variables of iconv extension
    • iconv_mime_decode - Decodes a MIME header field
    • iconv_mime_decode_headers - Decodes multiple MIME header fields at once
    • iconv_mime_encode - Composes a MIME header field
    • iconv_set_encoding - Set current setting for character encoding conversion
    • iconv_strlen - Returns the character count of string
    • iconv_strpos - Finds position of first occurrence of a needle within a haystack
    • iconv_strrpos - Finds the last occurrence of a needle within a haystack
    • iconv_substr - Cut out part of a string
    • id3_get_frame_long_name - Get the long name of an ID3v2 frame
    • id3_get_frame_short_name - Get the short name of an ID3v2 frame
    • id3_get_genre_id - Get the id for a genre
    • id3_get_genre_list - Get all possible genre values
    • id3_get_genre_name - Get the name for a genre id
    • id3_get_tag - Get all information stored in an ID3 tag
    • id3_get_version - Get version of an ID3 tag
    • id3_remove_tag - Remove an existing ID3 tag
    • id3_set_tag - Update information stored in an ID3 tag
    • idate - Format a local time/date as integer
    • idn_to_ascii - Convert domain name to IDNA ASCII form.
    • idn_to_utf8 - Convert domain name from IDNA ASCII to Unicode.
    • ifxus_close_slob - Deletes the slob object
    • ifxus_create_slob - Creates an slob object and opens it
    • ifxus_free_slob - Deletes the slob object
    • ifxus_open_slob - Opens an slob object
    • ifxus_read_slob - Reads nbytes of the slob object
    • ifxus_seek_slob - Sets the current file or seek position
    • ifxus_tell_slob - Returns the current file or seek position
    • ifxus_write_slob - Writes a string into the slob object
    • ifx_affected_rows - Get number of rows affected by a query
    • ifx_blobinfile_mode - Set the default blob mode for all select queries
    • ifx_byteasvarchar - Set the default byte mode
    • ifx_close - Close Informix connection
    • ifx_connect - Open Informix server connection
    • ifx_copy_blob - Duplicates the given blob object
    • ifx_create_blob - Creates an blob object
    • ifx_create_char - Creates an char object
    • ifx_do - Execute a previously prepared SQL-statement
    • ifx_error - Returns error code of last Informix call
    • ifx_errormsg - Returns error message of last Informix call
    • ifx_fetch_row - Get row as an associative array
    • ifx_fieldproperties - List of SQL fieldproperties
    • ifx_fieldtypes - List of Informix SQL fields
    • ifx_free_blob - Deletes the blob object
    • ifx_free_char - Deletes the char object
    • ifx_free_result - Releases resources for the query
    • ifx_getsqlca - Get the contents of sqlca.sqlerrd[0..5] after a query
    • ifx_get_blob - Return the content of a blob object
    • ifx_get_char - Return the content of the char object
    • ifx_htmltbl_result - Formats all rows of a query into a HTML table
    • ifx_nullformat - Sets the default return value on a fetch row
    • ifx_num_fields - Returns the number of columns in the query
    • ifx_num_rows - Count the rows already fetched from a query
    • ifx_pconnect - Open persistent Informix connection
    • ifx_prepare - Prepare an SQL-statement for execution
    • ifx_query - Send Informix query
    • ifx_textasvarchar - Set the default text mode
    • ifx_update_blob - Updates the content of the blob object
    • ifx_update_char - Updates the content of the char object
    • ignore_user_abort - Set whether a client disconnect should abort script execution
    • iis_add_server - Creates a new virtual web server
    • iis_get_dir_security - Gets Directory Security
    • iis_get_script_map - Gets script mapping on a virtual directory for a specific extension
    • iis_get_server_by_comment - Return the instance number associated with the Comment
    • iis_get_server_by_path - Return the instance number associated with the Path
    • iis_get_server_rights - Gets server rights
    • iis_get_service_state - Returns the state for the service defined by ServiceId
    • iis_remove_server - Removes the virtual web server indicated by ServerInstance
    • iis_set_app_settings - Creates application scope for a virtual directory
    • iis_set_dir_security - Sets Directory Security
    • iis_set_script_map - Sets script mapping on a virtual directory
    • iis_set_server_rights - Sets server rights
    • iis_start_server - Starts the virtual web server
    • iis_start_service - Starts the service defined by ServiceId
    • iis_stop_server - Stops the virtual web server
    • iis_stop_service - Stops the service defined by ServiceId
    • image2wbmp - 브라우저나 파일로 출력 이미지
    • imageaffine - Return an image containing the affine transformed src image, using an optional clipping area
    • imageaffinematrixconcat - Concatenate two affine transformation matrices
    • imageaffinematrixget - Get an affine transformation matrix
    • imagealphablending - Set the blending mode for an image
    • imageantialias - Should antialias functions be used or not
    • imagearc - Draws an arc
    • imagechar - Draw a character horizontally
    • imagecharup - Draw a character vertically
    • imagecolorallocate - Allocate a color for an image
    • imagecolorallocatealpha - Allocate a color for an image
    • imagecolorat - Get the index of the color of a pixel
    • imagecolorclosest - Get the index of the closest color to the specified color
    • imagecolorclosestalpha - Get the index of the closest color to the specified color + alpha
    • imagecolorclosesthwb - Get the index of the color which has the hue, white and blackness
    • imagecolordeallocate - De-allocate a color for an image
    • imagecolorexact - Get the index of the specified color
    • imagecolorexactalpha - Get the index of the specified color + alpha
    • imagecolormatch - Makes the colors of the palette version of an image more closely match the true color version
    • imagecolorresolve - Get the index of the specified color or its closest possible alternative
    • imagecolorresolvealpha - Get the index of the specified color + alpha or its closest possible alternative
    • imagecolorset - Set the color for the specified palette index
    • imagecolorsforindex - Get the colors for an index
    • imagecolorstotal - Find out the number of colors in an image's palette
    • imagecolortransparent - Define a color as transparent
    • imageconvolution - Apply a 3x3 convolution matrix, using coefficient and offset
    • imagecopy - Copy part of an image
    • imagecopymerge - Copy and merge part of an image
    • imagecopymergegray - Copy and merge part of an image with gray scale
    • imagecopyresampled - Copy and resize part of an image with resampling
    • imagecopyresized - Copy and resize part of an image
    • imagecreate - Create a new palette based image
    • imagecreatefromgd - Create a new image from GD file or URL
    • imagecreatefromgd2 - Create a new image from GD2 file or URL
    • imagecreatefromgd2part - Create a new image from a given part of GD2 file or URL
    • imagecreatefromgif - 파일이나 URL에서 새 이미지를 생성
    • imagecreatefromjpeg - 파일이나 URL에서 새 이미지를 생성
    • imagecreatefrompng - 파일이나 URL에서 새 이미지를 생성
    • imagecreatefromstring - Create a new image from the image stream in the string
    • imagecreatefromwbmp - 파일이나 URL에서 새 이미지를 생성
    • imagecreatefromwebp - 파일이나 URL에서 새 이미지를 생성
    • imagecreatefromxbm - 파일이나 URL에서 새 이미지를 생성
    • imagecreatefromxpm - 파일이나 URL에서 새 이미지를 생성
    • imagecreatetruecolor - Create a new true color image
    • imagecrop - Crop an image to the given rectangle
    • imagecropauto - Crop an image automatically using one of the available modes
    • imagedashedline - Draw a dashed line
    • imagedestroy - Destroy an image
    • imageellipse - Draw an ellipse
    • imagefill - Flood fill
    • imagefilledarc - Draw a partial arc and fill it
    • imagefilledellipse - Draw a filled ellipse
    • imagefilledpolygon - Draw a filled polygon
    • imagefilledrectangle - Draw a filled rectangle
    • imagefilltoborder - Flood fill to specific color
    • imagefilter - Applies a filter to an image
    • imageflip - Flips an image using a given mode
    • imagefontheight - Get font height
    • imagefontwidth - Get font width
    • imageftbbox - Give the bounding box of a text using fonts via freetype2
    • imagefttext - Write text to the image using fonts using FreeType 2
    • imagegammacorrect - Apply a gamma correction to a GD image
    • imagegd - Output GD image to browser or file
    • imagegd2 - Output GD2 image to browser or file
    • imagegetclip - Get the clipping rectangle
    • imagegif - 브라우저나 파일로 출력 이미지
    • imagegrabscreen - Captures the whole screen
    • imagegrabwindow - Captures a window
    • imageinterlace - Enable or disable interlace
    • imageistruecolor - Finds whether an image is a truecolor image
    • imagejpeg - 브라우저나 파일로 출력 이미지
    • imagelayereffect - Set the alpha blending flag to use layering effects
    • imageline - Draw a line
    • imageloadfont - Load a new font
    • imageopenpolygon - Draws an open polygon
    • imagepalettecopy - Copy the palette from one image to another
    • imagepalettetotruecolor - Converts a palette based image to true color
    • imagepng - Output a PNG image to either the browser or a file
    • imagepolygon - Draws a polygon
    • imagepsbbox - Give the bounding box of a text rectangle using PostScript Type1 fonts
    • imagepsencodefont - Change the character encoding vector of a font
    • imagepsextendfont - Extend or condense a font
    • imagepsfreefont - Free memory used by a PostScript Type 1 font
    • imagepsloadfont - Load a PostScript Type 1 font from file
    • imagepsslantfont - Slant a font
    • imagepstext - Draws a text over an image using PostScript Type1 fonts
    • imagerectangle - Draw a rectangle
    • imageresolution - Get or set the resolution of the image
    • imagerotate - Rotate an image with a given angle
    • imagesavealpha - Set the flag to save full alpha channel information (as opposed to single-color transparency) when saving PNG images
    • imagescale - Scale an image using the given new width and height
    • imagesetbrush - Set the brush image for line drawing
    • imagesetclip - Set the clipping rectangle
    • imagesetinterpolation - Set the interpolation method
    • imagesetpixel - Set a single pixel
    • imagesetstyle - Set the style for line drawing
    • imagesetthickness - Set the thickness for line drawing
    • imagesettile - Set the tile image for filling
    • imagestring - Draw a string horizontally
    • imagestringup - Draw a string vertically
    • imagesx - Get image width
    • imagesy - Get image height
    • imagetruecolortopalette - Convert a true color image to a palette image
    • imagettfbbox - Give the bounding box of a text using TrueType fonts
    • imagettftext - Write text to the image using TrueType fonts
    • imagetypes - Return the image types supported by this PHP build
    • imagewbmp - 브라우저나 파일로 출력 이미지
    • imagewebp - Output a WebP image to browser or file
    • imagexbm - Output an XBM image to browser or file
    • image_type_to_extension - Get file extension for image type
    • image_type_to_mime_type - Get Mime-Type for image-type returned by getimagesize, exif_read_data, exif_thumbnail, exif_imagetype
    • Imagick::adaptiveBlurImage - Adds adaptive blur filter to image
    • Imagick::adaptiveResizeImage - Adaptively resize image with data dependent triangulation
    • Imagick::adaptiveSharpenImage - Adaptively sharpen the image
    • Imagick::adaptiveThresholdImage - Selects a threshold for each pixel based on a range of intensity
    • Imagick::addImage - Adds new image to Imagick object image list
    • Imagick::addNoiseImage - Adds random noise to the image
    • Imagick::affineTransformImage - Transforms an image
    • Imagick::animateImages - Animates an image or images
    • Imagick::annotateImage - Annotates an image with text
    • Imagick::appendImages - Append a set of images
    • Imagick::autoLevelImage - Description
    • Imagick::averageImages - Average a set of images
    • Imagick::blackThresholdImage - Forces all pixels below the threshold into black
    • Imagick::blueShiftImage - Description
    • Imagick::blurImage - Adds blur filter to image
    • Imagick::borderImage - Surrounds the image with a border
    • Imagick::brightnessContrastImage - Description
    • Imagick::charcoalImage - Simulates a charcoal drawing
    • Imagick::chopImage - Removes a region of an image and trims
    • Imagick::clampImage - Description
    • Imagick::clear - Clears all resources associated to Imagick object
    • Imagick::clipImage - Clips along the first path from the 8BIM profile
    • Imagick::clipImagePath - Description
    • Imagick::clipPathImage - Clips along the named paths from the 8BIM profile
    • Imagick::clone - Makes an exact copy of the Imagick object
    • Imagick::clutImage - Replaces colors in the image
    • Imagick::coalesceImages - Composites a set of images
    • Imagick::colorFloodfillImage - Changes the color value of any pixel that matches target
    • Imagick::colorizeImage - Blends the fill color with the image
    • Imagick::colorMatrixImage - Description
    • Imagick::combineImages - Combines one or more images into a single image
    • Imagick::commentImage - Adds a comment to your image
    • Imagick::compareImageChannels - Returns the difference in one or more images
    • Imagick::compareImageLayers - Returns the maximum bounding region between images
    • Imagick::compareImages - Compares an image to a reconstructed image
    • Imagick::compositeImage - Composite one image onto another
    • Imagick::contrastImage - Change the contrast of the image
    • Imagick::contrastStretchImage - Enhances the contrast of a color image
    • Imagick::convolveImage - Applies a custom convolution kernel to the image
    • Imagick::count - Description
    • Imagick::cropImage - Extracts a region of the image
    • Imagick::cropThumbnailImage - Creates a crop thumbnail
    • Imagick::current - Returns a reference to the current Imagick object
    • Imagick::cycleColormapImage - Displaces an image's colormap
    • Imagick::decipherImage - Deciphers an image
    • Imagick::deconstructImages - Returns certain pixel differences between images
    • Imagick::deleteImageArtifact - Delete image artifact
    • Imagick::deleteImageProperty - Description
    • Imagick::deskewImage - Removes skew from the image
    • Imagick::despeckleImage - Reduces the speckle noise in an image
    • Imagick::destroy - Destroys the Imagick object
    • Imagick::displayImage - Displays an image
    • Imagick::displayImages - Displays an image or image sequence
    • Imagick::distortImage - Distorts an image using various distortion methods
    • Imagick::drawImage - Renders the ImagickDraw object on the current image
    • Imagick::edgeImage - Enhance edges within the image
    • Imagick::embossImage - Returns a grayscale image with a three-dimensional effect
    • Imagick::encipherImage - Enciphers an image
    • Imagick::enhanceImage - Improves the quality of a noisy image
    • Imagick::equalizeImage - Equalizes the image histogram
    • Imagick::evaluateImage - Applies an expression to an image
    • Imagick::exportImagePixels - Exports raw image pixels
    • Imagick::extentImage - Set image size
    • Imagick::filter - Description
    • Imagick::flattenImages - Merges a sequence of images
    • Imagick::flipImage - Creates a vertical mirror image
    • Imagick::floodFillPaintImage - Changes the color value of any pixel that matches target
    • Imagick::flopImage - Creates a horizontal mirror image
    • Imagick::forwardFourierTransformImage - Description
    • Imagick::frameImage - Adds a simulated three-dimensional border
    • Imagick::functionImage - Applies a function on the image
    • Imagick::fxImage - Evaluate expression for each pixel in the image
    • Imagick::gammaImage - Gamma-corrects an image
    • Imagick::gaussianBlurImage - Blurs an image
    • Imagick::getColorspace - Gets the colorspace
    • Imagick::getCompression - Gets the object compression type
    • Imagick::getCompressionQuality - Gets the object compression quality
    • Imagick::getCopyright - Returns the ImageMagick API copyright as a string
    • Imagick::getFilename - The filename associated with an image sequence
    • Imagick::getFont - Gets font
    • Imagick::getFormat - Returns the format of the Imagick object
    • Imagick::getGravity - Gets the gravity
    • Imagick::getHomeURL - Returns the ImageMagick home URL
    • Imagick::getImage - Returns a new Imagick object
    • Imagick::getImageAlphaChannel - Gets the image alpha channel
    • Imagick::getImageArtifact - Get image artifact
    • Imagick::getImageAttribute - Description
    • Imagick::getImageBackgroundColor - Returns the image background color
    • Imagick::getImageBlob - Returns the image sequence as a blob
    • Imagick::getImageBluePrimary - Returns the chromaticy blue primary point
    • Imagick::getImageBorderColor - Returns the image border color
    • Imagick::getImageChannelDepth - Gets the depth for a particular image channel
    • Imagick::getImageChannelDistortion - Compares image channels of an image to a reconstructed image
    • Imagick::getImageChannelDistortions - Gets channel distortions
    • Imagick::getImageChannelExtrema - Gets the extrema for one or more image channels
    • Imagick::getImageChannelKurtosis - The getImageChannelKurtosis purpose
    • Imagick::getImageChannelMean - Gets the mean and standard deviation
    • Imagick::getImageChannelRange - Gets channel range
    • Imagick::getImageChannelStatistics - Returns statistics for each channel in the image
    • Imagick::getImageClipMask - Gets image clip mask
    • Imagick::getImageColormapColor - Returns the color of the specified colormap index
    • Imagick::getImageColors - Gets the number of unique colors in the image
    • Imagick::getImageColorspace - Gets the image colorspace
    • Imagick::getImageCompose - Returns the composite operator associated with the image
    • Imagick::getImageCompression - Gets the current image's compression type
    • Imagick::getImageCompressionQuality - Gets the current image's compression quality
    • Imagick::getImageDelay - Gets the image delay
    • Imagick::getImageDepth - Gets the image depth
    • Imagick::getImageDispose - Gets the image disposal method
    • Imagick::getImageDistortion - Compares an image to a reconstructed image
    • Imagick::getImageExtrema - Gets the extrema for the image
    • Imagick::getImageFilename - Returns the filename of a particular image in a sequence
    • Imagick::getImageFormat - Returns the format of a particular image in a sequence
    • Imagick::getImageGamma - Gets the image gamma
    • Imagick::getImageGeometry - Gets the width and height as an associative array
    • Imagick::getImageGravity - Gets the image gravity
    • Imagick::getImageGreenPrimary - Returns the chromaticy green primary point
    • Imagick::getImageHeight - Returns the image height
    • Imagick::getImageHistogram - Gets the image histogram
    • Imagick::getImageIndex - Gets the index of the current active image
    • Imagick::getImageInterlaceScheme - Gets the image interlace scheme
    • Imagick::getImageInterpolateMethod - Returns the interpolation method
    • Imagick::getImageIterations - Gets the image iterations
    • Imagick::getImageLength - Returns the image length in bytes
    • Imagick::getImageMagickLicense - Returns a string containing the ImageMagick license
    • Imagick::getImageMatte - Return if the image has a matte channel
    • Imagick::getImageMatteColor - Returns the image matte color
    • Imagick::getImageMimeType - Description
    • Imagick::getImageOrientation - Gets the image orientation
    • Imagick::getImagePage - Returns the page geometry
    • Imagick::getImagePixelColor - Returns the color of the specified pixel
    • Imagick::getImageProfile - Returns the named image profile
    • Imagick::getImageProfiles - Returns the image profiles
    • Imagick::getImageProperties - Returns the image properties
    • Imagick::getImageProperty - Returns the named image property
    • Imagick::getImageRedPrimary - Returns the chromaticity red primary point
    • Imagick::getImageRegion - Extracts a region of the image
    • Imagick::getImageRenderingIntent - Gets the image rendering intent
    • Imagick::getImageResolution - Gets the image X and Y resolution
    • Imagick::getImagesBlob - Returns all image sequences as a blob
    • Imagick::getImageScene - Gets the image scene
    • Imagick::getImageSignature - Generates an SHA-256 message digest
    • Imagick::getImageSize - Returns the image length in bytes
    • Imagick::getImageTicksPerSecond - Gets the image ticks-per-second
    • Imagick::getImageTotalInkDensity - Gets the image total ink density
    • Imagick::getImageType - Gets the potential image type
    • Imagick::getImageUnits - Gets the image units of resolution
    • Imagick::getImageVirtualPixelMethod - Returns the virtual pixel method
    • Imagick::getImageWhitePoint - Returns the chromaticity white point
    • Imagick::getImageWidth - Returns the image width
    • Imagick::getInterlaceScheme - Gets the object interlace scheme
    • Imagick::getIteratorIndex - Gets the index of the current active image
    • Imagick::getNumberImages - Returns the number of images in the object
    • Imagick::getOption - Returns a value associated with the specified key
    • Imagick::getPackageName - Returns the ImageMagick package name
    • Imagick::getPage - Returns the page geometry
    • Imagick::getPixelIterator - Returns a MagickPixelIterator
    • Imagick::getPixelRegionIterator - Get an ImagickPixelIterator for an image section
    • Imagick::getPointSize - Gets point size
    • Imagick::getQuantum - Description
    • Imagick::getQuantumDepth - Gets the quantum depth
    • Imagick::getQuantumRange - Returns the Imagick quantum range
    • Imagick::getRegistry - Description
    • Imagick::getReleaseDate - Returns the ImageMagick release date
    • Imagick::getResource - Returns the specified resource's memory usage
    • Imagick::getResourceLimit - Returns the specified resource limit
    • Imagick::getSamplingFactors - Gets the horizontal and vertical sampling factor
    • Imagick::getSize - Returns the size associated with the Imagick object
    • Imagick::getSizeOffset - Returns the size offset
    • Imagick::getVersion - Returns the ImageMagick API version
    • Imagick::haldClutImage - Replaces colors in the image
    • Imagick::hasNextImage - Checks if the object has more images
    • Imagick::hasPreviousImage - Checks if the object has a previous image
    • Imagick::identifyFormat - Description
    • Imagick::identifyImage - Identifies an image and fetches attributes
    • Imagick::implodeImage - Creates a new image as a copy
    • Imagick::importImagePixels - Imports image pixels
    • Imagick::inverseFourierTransformImage - Description
    • Imagick::labelImage - Adds a label to an image
    • Imagick::levelImage - Adjusts the levels of an image
    • Imagick::linearStretchImage - Stretches with saturation the image intensity
    • Imagick::liquidRescaleImage - Animates an image or images
    • Imagick::listRegistry - Description
    • Imagick::magnifyImage - Scales an image proportionally 2x
    • Imagick::mapImage - Replaces the colors of an image with the closest color from a reference image.
    • Imagick::matteFloodfillImage - Changes the transparency value of a color
    • Imagick::medianFilterImage - Applies a digital filter
    • Imagick::mergeImageLayers - Merges image layers
    • Imagick::minifyImage - Scales an image proportionally to half its size
    • Imagick::modulateImage - Control the brightness, saturation, and hue
    • Imagick::montageImage - Creates a composite image
    • Imagick::morphImages - Method morphs a set of images
    • Imagick::morphology - Description
    • Imagick::mosaicImages - Forms a mosaic from images
    • Imagick::motionBlurImage - Simulates motion blur
    • Imagick::negateImage - Negates the colors in the reference image
    • Imagick::newImage - Creates a new image
    • Imagick::newPseudoImage - Creates a new image
    • Imagick::nextImage - Moves to the next image
    • Imagick::normalizeImage - Enhances the contrast of a color image
    • Imagick::oilPaintImage - Simulates an oil painting
    • Imagick::opaquePaintImage - Changes the color value of any pixel that matches target
    • Imagick::optimizeImageLayers - Removes repeated portions of images to optimize
    • Imagick::orderedPosterizeImage - Performs an ordered dither
    • Imagick::paintFloodfillImage - Changes the color value of any pixel that matches target
    • Imagick::paintOpaqueImage - Change any pixel that matches color
    • Imagick::paintTransparentImage - Changes any pixel that matches color with the color defined by fill
    • Imagick::pingImage - Fetch basic attributes about the image
    • Imagick::pingImageBlob - Quickly fetch attributes
    • Imagick::pingImageFile - Get basic image attributes in a lightweight manner
    • Imagick::polaroidImage - Simulates a Polaroid picture
    • Imagick::posterizeImage - Reduces the image to a limited number of color level
    • Imagick::previewImages - Quickly pin-point appropriate parameters for image processing
    • Imagick::previousImage - Move to the previous image in the object
    • Imagick::profileImage - Adds or removes a profile from an image
    • Imagick::quantizeImage - Analyzes the colors within a reference image
    • Imagick::quantizeImages - Analyzes the colors within a sequence of images
    • Imagick::queryFontMetrics - Returns an array representing the font metrics
    • Imagick::queryFonts - Returns the configured fonts
    • Imagick::queryFormats - Returns formats supported by Imagick
    • Imagick::radialBlurImage - Radial blurs an image
    • Imagick::raiseImage - Creates a simulated 3d button-like effect
    • Imagick::randomThresholdImage - Creates a high-contrast, two-color image
    • Imagick::readImage - Reads image from filename
    • Imagick::readImageBlob - Reads image from a binary string
    • Imagick::readImageFile - Reads image from open filehandle
    • Imagick::readimages - Description
    • Imagick::recolorImage - Recolors image
    • Imagick::reduceNoiseImage - Smooths the contours of an image
    • Imagick::remapImage - Remaps image colors
    • Imagick::removeImage - Removes an image from the image list
    • Imagick::removeImageProfile - Removes the named image profile and returns it
    • Imagick::render - Renders all preceding drawing commands
    • Imagick::resampleImage - Resample image to desired resolution
    • Imagick::resetImagePage - Reset image page
    • Imagick::resizeImage - Scales an image
    • Imagick::rollImage - Offsets an image
    • Imagick::rotateImage - Rotates an image
    • Imagick::rotationalBlurImage - Description
    • Imagick::roundCorners - Rounds image corners
    • Imagick::sampleImage - Scales an image with pixel sampling
    • Imagick::scaleImage - Scales the size of an image
    • Imagick::segmentImage - Segments an image
    • Imagick::selectiveBlurImage - Description
    • Imagick::separateImageChannel - Separates a channel from the image
    • Imagick::sepiaToneImage - Sepia tones an image
    • Imagick::setBackgroundColor - Sets the object's default background color
    • Imagick::setColorspace - Set colorspace
    • Imagick::setCompression - Sets the object's default compression type
    • Imagick::setCompressionQuality - Sets the object's default compression quality
    • Imagick::setFilename - Sets the filename before you read or write the image
    • Imagick::setFirstIterator - Sets the Imagick iterator to the first image
    • Imagick::setFont - Sets font
    • Imagick::setFormat - Sets the format of the Imagick object
    • Imagick::setGravity - Sets the gravity
    • Imagick::setImage - Replaces image in the object
    • Imagick::setImageAlphaChannel - Sets image alpha channel
    • Imagick::setImageArtifact - Set image artifact
    • Imagick::setImageAttribute - Description
    • Imagick::setImageBackgroundColor - Sets the image background color
    • Imagick::setImageBias - Sets the image bias for any method that convolves an image
    • Imagick::setImageBiasQuantum - Description
    • Imagick::setImageBluePrimary - Sets the image chromaticity blue primary point
    • Imagick::setImageBorderColor - Sets the image border color
    • Imagick::setImageChannelDepth - Sets the depth of a particular image channel
    • Imagick::setImageClipMask - Sets image clip mask
    • Imagick::setImageColormapColor - Sets the color of the specified colormap index
    • Imagick::setImageColorspace - Sets the image colorspace
    • Imagick::setImageCompose - Sets the image composite operator
    • Imagick::setImageCompression - Sets the image compression
    • Imagick::setImageCompressionQuality - Sets the image compression quality
    • Imagick::setImageDelay - Sets the image delay
    • Imagick::setImageDepth - Sets the image depth
    • Imagick::setImageDispose - Sets the image disposal method
    • Imagick::setImageExtent - Sets the image size
    • Imagick::setImageFilename - Sets the filename of a particular image
    • Imagick::setImageFormat - Sets the format of a particular image
    • Imagick::setImageGamma - Sets the image gamma
    • Imagick::setImageGravity - Sets the image gravity
    • Imagick::setImageGreenPrimary - Sets the image chromaticity green primary point
    • Imagick::setImageIndex - Set the iterator position
    • Imagick::setImageInterlaceScheme - Sets the image compression
    • Imagick::setImageInterpolateMethod - Sets the image interpolate pixel method
    • Imagick::setImageIterations - Sets the image iterations
    • Imagick::setImageMatte - Sets the image matte channel
    • Imagick::setImageMatteColor - Sets the image matte color
    • Imagick::setImageOpacity - Sets the image opacity level
    • Imagick::setImageOrientation - Sets the image orientation
    • Imagick::setImagePage - Sets the page geometry of the image
    • Imagick::setImageProfile - Adds a named profile to the Imagick object
    • Imagick::setImageProperty - Sets an image property
    • Imagick::setImageRedPrimary - Sets the image chromaticity red primary point
    • Imagick::setImageRenderingIntent - Sets the image rendering intent
    • Imagick::setImageResolution - Sets the image resolution
    • Imagick::setImageScene - Sets the image scene
    • Imagick::setImageTicksPerSecond - Sets the image ticks-per-second
    • Imagick::setImageType - Sets the image type
    • Imagick::setImageUnits - Sets the image units of resolution
    • Imagick::setImageVirtualPixelMethod - Sets the image virtual pixel method
    • Imagick::setImageWhitePoint - Sets the image chromaticity white point
    • Imagick::setInterlaceScheme - Sets the image compression
    • Imagick::setIteratorIndex - Set the iterator position
    • Imagick::setLastIterator - Sets the Imagick iterator to the last image
    • Imagick::setOption - Set an option
    • Imagick::setPage - Sets the page geometry of the Imagick object
    • Imagick::setPointSize - Sets point size
    • Imagick::setProgressMonitor - Description
    • Imagick::setRegistry - Description
    • Imagick::setResolution - Sets the image resolution
    • Imagick::setResourceLimit - Sets the limit for a particular resource in megabytes
    • Imagick::setSamplingFactors - Sets the image sampling factors
    • Imagick::setSize - Sets the size of the Imagick object
    • Imagick::setSizeOffset - Sets the size and offset of the Imagick object
    • Imagick::setType - Sets the image type attribute
    • Imagick::shadeImage - Creates a 3D effect
    • Imagick::shadowImage - Simulates an image shadow
    • Imagick::sharpenImage - Sharpens an image
    • Imagick::shaveImage - Shaves pixels from the image edges
    • Imagick::shearImage - Creating a parallelogram
    • Imagick::sigmoidalContrastImage - Adjusts the contrast of an image
    • Imagick::sketchImage - Simulates a pencil sketch
    • Imagick::smushImages - Description
    • Imagick::solarizeImage - Applies a solarizing effect to the image
    • Imagick::sparseColorImage - Interpolates colors
    • Imagick::spliceImage - Splices a solid color into the image
    • Imagick::spreadImage - Randomly displaces each pixel in a block
    • Imagick::statisticImage - Description
    • Imagick::steganoImage - Hides a digital watermark within the image
    • Imagick::stereoImage - Composites two images
    • Imagick::stripImage - Strips an image of all profiles and comments
    • Imagick::subImageMatch - Description
    • Imagick::swirlImage - Swirls the pixels about the center of the image
    • Imagick::textureImage - Repeatedly tiles the texture image
    • Imagick::thresholdImage - Changes the value of individual pixels based on a threshold
    • Imagick::thumbnailImage - Changes the size of an image
    • Imagick::tintImage - Applies a color vector to each pixel in the image
    • Imagick::transformImage - Convenience method for setting crop size and the image geometry
    • Imagick::transformImageColorspace - Transforms an image to a new colorspace.
    • Imagick::transparentPaintImage - Paints pixels transparent
    • Imagick::transposeImage - Creates a vertical mirror image
    • Imagick::transverseImage - Creates a horizontal mirror image
    • Imagick::trimImage - Remove edges from the image
    • Imagick::uniqueImageColors - Discards all but one of any pixel color
    • Imagick::unsharpMaskImage - Sharpens an image
    • Imagick::valid - Checks if the current item is valid
    • Imagick::vignetteImage - Adds vignette filter to the image
    • Imagick::waveImage - Applies wave filter to the image
    • Imagick::whiteThresholdImage - Force all pixels above the threshold into white
    • Imagick::writeImage - Writes an image to the specified filename
    • Imagick::writeImageFile - Writes an image to a filehandle
    • Imagick::writeImages - Writes an image or image sequence
    • Imagick::writeImagesFile - Writes frames to a filehandle
    • Imagick::__construct - The Imagick constructor
    • Imagick::__toString - Returns the image as a string.
    • ImagickDraw::affine - Adjusts the current affine transformation matrix
    • ImagickDraw::annotation - Draws text on the image
    • ImagickDraw::arc - Draws an arc
    • ImagickDraw::bezier - Draws a bezier curve
    • ImagickDraw::circle - Draws a circle
    • ImagickDraw::clear - Clears the ImagickDraw
    • ImagickDraw::clone - Makes an exact copy of the specified ImagickDraw object
    • ImagickDraw::color - Draws color on image
    • ImagickDraw::comment - Adds a comment
    • ImagickDraw::composite - Composites an image onto the current image
    • ImagickDraw::destroy - Frees all associated resources
    • ImagickDraw::ellipse - Draws an ellipse on the image
    • ImagickDraw::getClipPath - Obtains the current clipping path ID
    • ImagickDraw::getClipRule - Returns the current polygon fill rule
    • ImagickDraw::getClipUnits - Returns the interpretation of clip path units
    • ImagickDraw::getFillColor - Returns the fill color
    • ImagickDraw::getFillOpacity - Returns the opacity used when drawing
    • ImagickDraw::getFillRule - Returns the fill rule
    • ImagickDraw::getFont - Returns the font
    • ImagickDraw::getFontFamily - Returns the font family
    • ImagickDraw::getFontSize - Returns the font pointsize
    • ImagickDraw::getFontStretch - Description
    • ImagickDraw::getFontStyle - Returns the font style
    • ImagickDraw::getFontWeight - Returns the font weight
    • ImagickDraw::getGravity - Returns the text placement gravity
    • ImagickDraw::getStrokeAntialias - Returns the current stroke antialias setting
    • ImagickDraw::getStrokeColor - Returns the color used for stroking object outlines
    • ImagickDraw::getStrokeDashArray - Returns an array representing the pattern of dashes and gaps used to stroke paths
    • ImagickDraw::getStrokeDashOffset - Returns the offset into the dash pattern to start the dash
    • ImagickDraw::getStrokeLineCap - Returns the shape to be used at the end of open subpaths when they are stroked
    • ImagickDraw::getStrokeLineJoin - Returns the shape to be used at the corners of paths when they are stroked
    • ImagickDraw::getStrokeMiterLimit - Returns the stroke miter limit
    • ImagickDraw::getStrokeOpacity - Returns the opacity of stroked object outlines
    • ImagickDraw::getStrokeWidth - Returns the width of the stroke used to draw object outlines
    • ImagickDraw::getTextAlignment - Returns the text alignment
    • ImagickDraw::getTextAntialias - Returns the current text antialias setting
    • ImagickDraw::getTextDecoration - Returns the text decoration
    • ImagickDraw::getTextEncoding - Returns the code set used for text annotations
    • ImagickDraw::getTextInterlineSpacing - Description
    • ImagickDraw::getTextInterwordSpacing - Description
    • ImagickDraw::getTextKerning - Description
    • ImagickDraw::getTextUnderColor - Returns the text under color
    • ImagickDraw::getVectorGraphics - Returns a string containing vector graphics
    • ImagickDraw::line - Draws a line
    • ImagickDraw::matte - Paints on the image's opacity channel
    • ImagickDraw::pathClose - Adds a path element to the current path
    • ImagickDraw::pathCurveToAbsolute - Draws a cubic Bezier curve
    • ImagickDraw::pathCurveToQuadraticBezierAbsolute - Draws a quadratic Bezier curve
    • ImagickDraw::pathCurveToQuadraticBezierRelative - Draws a quadratic Bezier curve
    • ImagickDraw::pathCurveToQuadraticBezierSmoothAbsolute - Draws a quadratic Bezier curve
    • ImagickDraw::pathCurveToQuadraticBezierSmoothRelative - Draws a quadratic Bezier curve
    • ImagickDraw::pathCurveToRelative - Draws a cubic Bezier curve
    • ImagickDraw::pathCurveToSmoothAbsolute - Draws a cubic Bezier curve
    • ImagickDraw::pathCurveToSmoothRelative - Draws a cubic Bezier curve
    • ImagickDraw::pathEllipticArcAbsolute - Draws an elliptical arc
    • ImagickDraw::pathEllipticArcRelative - Draws an elliptical arc
    • ImagickDraw::pathFinish - Terminates the current path
    • ImagickDraw::pathLineToAbsolute - Draws a line path
    • ImagickDraw::pathLineToHorizontalAbsolute - Draws a horizontal line path
    • ImagickDraw::pathLineToHorizontalRelative - Draws a horizontal line
    • ImagickDraw::pathLineToRelative - Draws a line path
    • ImagickDraw::pathLineToVerticalAbsolute - Draws a vertical line
    • ImagickDraw::pathLineToVerticalRelative - Draws a vertical line path
    • ImagickDraw::pathMoveToAbsolute - Starts a new sub-path
    • ImagickDraw::pathMoveToRelative - Starts a new sub-path
    • ImagickDraw::pathStart - Declares the start of a path drawing list
    • ImagickDraw::point - Draws a point
    • ImagickDraw::polygon - Draws a polygon
    • ImagickDraw::polyline - Draws a polyline
    • ImagickDraw::pop - Destroys the current ImagickDraw in the stack, and returns to the previously pushed ImagickDraw
    • ImagickDraw::popClipPath - Terminates a clip path definition
    • ImagickDraw::popDefs - Terminates a definition list
    • ImagickDraw::popPattern - Terminates a pattern definition
    • ImagickDraw::push - Clones the current ImagickDraw and pushes it to the stack
    • ImagickDraw::pushClipPath - Starts a clip path definition
    • ImagickDraw::pushDefs - Indicates that following commands create named elements for early processing
    • ImagickDraw::pushPattern - Indicates that subsequent commands up to a ImagickDraw::opPattern() command comprise the definition of a named pattern
    • ImagickDraw::rectangle - Draws a rectangle
    • ImagickDraw::render - Renders all preceding drawing commands onto the image
    • ImagickDraw::resetVectorGraphics - Description
    • ImagickDraw::rotate - Applies the specified rotation to the current coordinate space
    • ImagickDraw::roundRectangle - Draws a rounded rectangle
    • ImagickDraw::scale - Adjusts the scaling factor
    • ImagickDraw::setClipPath - Associates a named clipping path with the image
    • ImagickDraw::setClipRule - Set the polygon fill rule to be used by the clipping path
    • ImagickDraw::setClipUnits - Sets the interpretation of clip path units
    • ImagickDraw::setFillAlpha - Sets the opacity to use when drawing using the fill color or fill texture
    • ImagickDraw::setFillColor - Sets the fill color to be used for drawing filled objects
    • ImagickDraw::setFillOpacity - Sets the opacity to use when drawing using the fill color or fill texture
    • ImagickDraw::setFillPatternURL - Sets the URL to use as a fill pattern for filling objects
    • ImagickDraw::setFillRule - Sets the fill rule to use while drawing polygons
    • ImagickDraw::setFont - Sets the fully-specified font to use when annotating with text
    • ImagickDraw::setFontFamily - Sets the font family to use when annotating with text
    • ImagickDraw::setFontSize - Sets the font pointsize to use when annotating with text
    • ImagickDraw::setFontStretch - Sets the font stretch to use when annotating with text
    • ImagickDraw::setFontStyle - Sets the font style to use when annotating with text
    • ImagickDraw::setFontWeight - Sets the font weight
    • ImagickDraw::setGravity - Sets the text placement gravity
    • ImagickDraw::setResolution - Description
    • ImagickDraw::setStrokeAlpha - Specifies the opacity of stroked object outlines
    • ImagickDraw::setStrokeAntialias - Controls whether stroked outlines are antialiased
    • ImagickDraw::setStrokeColor - Sets the color used for stroking object outlines
    • ImagickDraw::setStrokeDashArray - Specifies the pattern of dashes and gaps used to stroke paths
    • ImagickDraw::setStrokeDashOffset - Specifies the offset into the dash pattern to start the dash
    • ImagickDraw::setStrokeLineCap - Specifies the shape to be used at the end of open subpaths when they are stroked
    • ImagickDraw::setStrokeLineJoin - Specifies the shape to be used at the corners of paths when they are stroked
    • ImagickDraw::setStrokeMiterLimit - Specifies the miter limit
    • ImagickDraw::setStrokeOpacity - Specifies the opacity of stroked object outlines
    • ImagickDraw::setStrokePatternURL - Sets the pattern used for stroking object outlines
    • ImagickDraw::setStrokeWidth - Sets the width of the stroke used to draw object outlines
    • ImagickDraw::setTextAlignment - Specifies a text alignment
    • ImagickDraw::setTextAntialias - Controls whether text is antialiased
    • ImagickDraw::setTextDecoration - Specifies a decoration
    • ImagickDraw::setTextEncoding - Specifies the text code set
    • ImagickDraw::setTextInterlineSpacing - Description
    • ImagickDraw::setTextInterwordSpacing - Description
    • ImagickDraw::setTextKerning - Description
    • ImagickDraw::setTextUnderColor - Specifies the color of a background rectangle
    • ImagickDraw::setVectorGraphics - Sets the vector graphics
    • ImagickDraw::setViewbox - Sets the overall canvas size
    • ImagickDraw::skewX - Skews the current coordinate system in the horizontal direction
    • ImagickDraw::skewY - Skews the current coordinate system in the vertical direction
    • ImagickDraw::translate - Applies a translation to the current coordinate system
    • ImagickDraw::__construct - The ImagickDraw constructor
    • ImagickKernel::addKernel - Description
    • ImagickKernel::addUnityKernel - Description
    • ImagickKernel::fromBuiltIn - Description
    • ImagickKernel::fromMatrix - Description
    • ImagickKernel::getMatrix - Description
    • ImagickKernel::scale - Description
    • ImagickKernel::separate - Description
    • ImagickPixel::clear - Clears resources associated with this object
    • ImagickPixel::destroy - Deallocates resources associated with this object
    • ImagickPixel::getColor - Returns the color
    • ImagickPixel::getColorAsString - Returns the color as a string
    • ImagickPixel::getColorCount - Returns the color count associated with this color
    • ImagickPixel::getColorQuantum - Description
    • ImagickPixel::getColorValue - Gets the normalized value of the provided color channel
    • ImagickPixel::getColorValueQuantum - Description
    • ImagickPixel::getHSL - Returns the normalized HSL color of the ImagickPixel object
    • ImagickPixel::getIndex - Description
    • ImagickPixel::isPixelSimilar - Check the distance between this color and another
    • ImagickPixel::isPixelSimilarQuantum - Description
    • ImagickPixel::isSimilar - Check the distance between this color and another
    • ImagickPixel::setColor - Sets the color
    • ImagickPixel::setColorCount - Description
    • ImagickPixel::setColorValue - Sets the normalized value of one of the channels
    • ImagickPixel::setColorValueQuantum - Description
    • ImagickPixel::setHSL - Sets the normalized HSL color
    • ImagickPixel::setIndex - Description
    • ImagickPixel::__construct - The ImagickPixel constructor
    • ImagickPixelIterator::clear - Clear resources associated with a PixelIterator
    • ImagickPixelIterator::destroy - Deallocates resources associated with a PixelIterator
    • ImagickPixelIterator::getCurrentIteratorRow - Returns the current row of ImagickPixel objects
    • ImagickPixelIterator::getIteratorRow - Returns the current pixel iterator row
    • ImagickPixelIterator::getNextIteratorRow - Returns the next row of the pixel iterator
    • ImagickPixelIterator::getPreviousIteratorRow - Returns the previous row
    • ImagickPixelIterator::newPixelIterator - Returns a new pixel iterator
    • ImagickPixelIterator::newPixelRegionIterator - Returns a new pixel iterator
    • ImagickPixelIterator::resetIterator - Resets the pixel iterator
    • ImagickPixelIterator::setIteratorFirstRow - Sets the pixel iterator to the first pixel row
    • ImagickPixelIterator::setIteratorLastRow - Sets the pixel iterator to the last pixel row
    • ImagickPixelIterator::setIteratorRow - Set the pixel iterator row
    • ImagickPixelIterator::syncIterator - Syncs the pixel iterator
    • ImagickPixelIterator::__construct - The ImagickPixelIterator constructor
    • imap_8bit - Convert an 8bit string to a quoted-printable string
    • imap_alerts - Returns all IMAP alert messages that have occurred
    • imap_append - Append a string message to a specified mailbox
    • imap_base64 - Decode BASE64 encoded text
    • imap_binary - Convert an 8bit string to a base64 string
    • imap_body - Read the message body
    • imap_bodystruct - Read the structure of a specified body section of a specific message
    • imap_check - Check current mailbox
    • imap_clearflag_full - Clears flags on messages
    • imap_close - Close an IMAP stream
    • imap_create - 별칭: imap_createmailbox
    • imap_createmailbox - Create a new mailbox
    • imap_delete - Mark a message for deletion from current mailbox
    • imap_deletemailbox - Delete a mailbox
    • imap_errors - Returns all of the IMAP errors that have occurred
    • imap_expunge - Delete all messages marked for deletion
    • imap_fetchbody - Fetch a particular section of the body of the message
    • imap_fetchheader - Returns header for a message
    • imap_fetchmime - Fetch MIME headers for a particular section of the message
    • imap_fetchstructure - Read the structure of a particular message
    • imap_fetchtext - 별칭: imap_body
    • imap_fetch_overview - Read an overview of the information in the headers of the given message
    • imap_gc - Clears IMAP cache
    • imap_getacl - Gets the ACL for a given mailbox
    • imap_getmailboxes - Read the list of mailboxes, returning detailed information on each one
    • imap_getsubscribed - List all the subscribed mailboxes
    • imap_get_quota - Retrieve the quota level settings, and usage statics per mailbox
    • imap_get_quotaroot - Retrieve the quota settings per user
    • imap_header - 별칭: imap_headerinfo
    • imap_headerinfo - Read the header of the message
    • imap_headers - Returns headers for all messages in a mailbox
    • imap_last_error - Gets the last IMAP error that occurred during this page request
    • imap_list - Read the list of mailboxes
    • imap_listmailbox - 별칭: imap_list
    • imap_listscan - Returns the list of mailboxes that matches the given text
    • imap_listsubscribed - 별칭: imap_lsub
    • imap_lsub - List all the subscribed mailboxes
    • imap_mail - Send an email message
    • imap_mailboxmsginfo - Get information about the current mailbox
    • imap_mail_compose - Create a MIME message based on given envelope and body sections
    • imap_mail_copy - Copy specified messages to a mailbox
    • imap_mail_move - Move specified messages to a mailbox
    • imap_mime_header_decode - Decode MIME header elements
    • imap_msgno - Gets the message sequence number for the given UID
    • imap_num_msg - Gets the number of messages in the current mailbox
    • imap_num_recent - Gets the number of recent messages in current mailbox
    • imap_open - Open an IMAP stream to a mailbox
    • imap_ping - Check if the IMAP stream is still active
    • imap_qprint - Convert a quoted-printable string to an 8 bit string
    • imap_rename - 별칭: imap_renamemailbox
    • imap_renamemailbox - Rename an old mailbox to new mailbox
    • imap_reopen - Reopen IMAP stream to new mailbox
    • imap_rfc822_parse_adrlist - Parses an address string
    • imap_rfc822_parse_headers - Parse mail headers from a string
    • imap_rfc822_write_address - Returns a properly formatted email address given the mailbox, host, and personal info
    • imap_savebody - Save a specific body section to a file
    • imap_scan - 별칭: imap_listscan
    • imap_scanmailbox - 별칭: imap_listscan
    • imap_search - This function returns an array of messages matching the given search criteria
    • imap_setacl - Sets the ACL for a given mailbox
    • imap_setflag_full - Sets flags on messages
    • imap_set_quota - Sets a quota for a given mailbox
    • imap_sort - Gets and sort messages
    • imap_status - Returns status information on a mailbox
    • imap_subscribe - Subscribe to a mailbox
    • imap_thread - Returns a tree of threaded message
    • imap_timeout - Set or fetch imap timeout
    • imap_uid - This function returns the UID for the given message sequence number
    • imap_undelete - Unmark the message which is marked deleted
    • imap_unsubscribe - Unsubscribe from a mailbox
    • imap_utf7_decode - Decodes a modified UTF-7 encoded string
    • imap_utf7_encode - Converts ISO-8859-1 string to modified UTF-7 text
    • imap_utf8 - Converts MIME-encoded text to UTF-8
    • implode - 문자열로 배열 원소를 결합
    • import_request_variables - GET/POST/쿠키 변수를 전역으로 가져옵니다
    • inclued_get_data - Get the inclued data
    • inet_ntop - Converts a packed internet address to a human readable representation
    • inet_pton - Converts a human readable IP address to its packed in_addr representation
    • InfiniteIterator::next - Moves the inner Iterator forward or rewinds it
    • InfiniteIterator::__construct - Constructs an InfiniteIterator
    • inflate_add - Incrementally inflate encoded data
    • inflate_init - Initialize an incremental inflate context
    • ingres_autocommit - Switch autocommit on or off
    • ingres_autocommit_state - Test if the connection is using autocommit
    • ingres_charset - Returns the installation character set
    • ingres_close - Close an Ingres database connection
    • ingres_commit - Commit a transaction
    • ingres_connect - Open a connection to an Ingres database
    • ingres_cursor - Get a cursor name for a given result resource
    • ingres_errno - Get the last Ingres error number generated
    • ingres_error - Get a meaningful error message for the last error generated
    • ingres_errsqlstate - Get the last SQLSTATE error code generated
    • ingres_escape_string - Escape special characters for use in a query
    • ingres_execute - Execute a prepared query
    • ingres_fetch_array - Fetch a row of result into an array
    • ingres_fetch_assoc - Fetch a row of result into an associative array
    • ingres_fetch_object - Fetch a row of result into an object
    • ingres_fetch_proc_return - Get the return value from a procedure call
    • ingres_fetch_row - Fetch a row of result into an enumerated array
    • ingres_field_length - Get the length of a field
    • ingres_field_name - Get the name of a field in a query result
    • ingres_field_nullable - Test if a field is nullable
    • ingres_field_precision - Get the precision of a field
    • ingres_field_scale - Get the scale of a field
    • ingres_field_type - Get the type of a field in a query result
    • ingres_free_result - Free the resources associated with a result identifier
    • ingres_next_error - Get the next Ingres error
    • ingres_num_fields - Get the number of fields returned by the last query
    • ingres_num_rows - Get the number of rows affected or returned by a query
    • ingres_pconnect - Open a persistent connection to an Ingres database
    • ingres_prepare - Prepare a query for later execution
    • ingres_query - Send an SQL query to Ingres
    • ingres_result_seek - Set the row position before fetching data
    • ingres_rollback - Roll back a transaction
    • ingres_set_environment - Set environment features controlling output options
    • ingres_unbuffered_query - Send an unbuffered SQL query to Ingres
    • ini_alter - 별칭: ini_set
    • ini_get - Gets the value of a configuration option
    • ini_get_all - 모든 설정 옵션을 얻습니다
    • ini_restore - 설정 옵션 값을 복구
    • ini_set - Sets the value of a configuration option
    • inotify_add_watch - Add a watch to an initialized inotify instance
    • inotify_init - Initialize an inotify instance
    • inotify_queue_len - Return a number upper than zero if there are pending events
    • inotify_read - Read events from an inotify instance
    • inotify_rm_watch - Remove an existing watch from an inotify instance
    • intdiv - Integer division
    • interface_exists - 인터페이스가 정의되었는지 확인
    • IntlBreakIterator::createCharacterInstance - Create break iterator for boundaries of combining character sequences
    • IntlBreakIterator::createCodePointInstance - Create break iterator for boundaries of code points
    • IntlBreakIterator::createLineInstance - Create break iterator for logically possible line breaks
    • IntlBreakIterator::createSentenceInstance - Create break iterator for sentence breaks
    • IntlBreakIterator::createTitleInstance - Create break iterator for title-casing breaks
    • IntlBreakIterator::createWordInstance - Create break iterator for word breaks
    • IntlBreakIterator::current - Get index of current position
    • IntlBreakIterator::first - Set position to the first character in the text
    • IntlBreakIterator::following - Advance the iterator to the first boundary following specified offset
    • IntlBreakIterator::getErrorCode - Get last error code on the object
    • IntlBreakIterator::getErrorMessage - Get last error message on the object
    • IntlBreakIterator::getLocale - Get the locale associated with the object
    • IntlBreakIterator::getPartsIterator - Create iterator for navigating fragments between boundaries
    • IntlBreakIterator::getText - Get the text being scanned
    • IntlBreakIterator::isBoundary - Tell whether an offset is a boundary?s offset
    • IntlBreakIterator::last - Set the iterator position to index beyond the last character
    • IntlBreakIterator::next - Advance the iterator the next boundary
    • IntlBreakIterator::preceding - Set the iterator position to the first boundary before an offset
    • IntlBreakIterator::previous - Set the iterator position to the boundary immediately before the current
    • IntlBreakIterator::setText - Set the text being scanned
    • IntlBreakIterator::__construct - Private constructor for disallowing instantiation
    • IntlCalendar::add - Add a (signed) amount of time to a field
    • IntlCalendar::after - Whether this object?s time is after that of the passed object
    • IntlCalendar::before - Whether this object?s time is before that of the passed object
    • IntlCalendar::clear - Clear a field or all fields
    • IntlCalendar::createInstance - Create a new IntlCalendar
    • IntlCalendar::equals - Compare time of two IntlCalendar objects for equality
    • IntlCalendar::fieldDifference - Calculate difference between given time and this object?s time
    • IntlCalendar::fromDateTime - Create an IntlCalendar from a DateTime object or string
    • IntlCalendar::get - Get the value for a field
    • IntlCalendar::getActualMaximum - The maximum value for a field, considering the object?s current time
    • IntlCalendar::getActualMinimum - The minimum value for a field, considering the object?s current time
    • IntlCalendar::getAvailableLocales - Get array of locales for which there is data
    • IntlCalendar::getDayOfWeekType - Tell whether a day is a weekday, weekend or a day that has a transition between the two
    • IntlCalendar::getErrorCode - Get last error code on the object
    • IntlCalendar::getErrorMessage - Get last error message on the object
    • IntlCalendar::getFirstDayOfWeek - Get the first day of the week for the calendar?s locale
    • IntlCalendar::getGreatestMinimum - Get the largest local minimum value for a field
    • IntlCalendar::getKeywordValuesForLocale - Get set of locale keyword values
    • IntlCalendar::getLeastMaximum - Get the smallest local maximum for a field
    • IntlCalendar::getLocale - Get the locale associated with the object
    • IntlCalendar::getMaximum - Get the global maximum value for a field
    • IntlCalendar::getMinimalDaysInFirstWeek - Get minimal number of days the first week in a year or month can have
    • IntlCalendar::getMinimum - Get the global minimum value for a field
    • IntlCalendar::getNow - Get number representing the current time
    • IntlCalendar::getRepeatedWallTimeOption - Get behavior for handling repeating wall time
    • IntlCalendar::getSkippedWallTimeOption - Get behavior for handling skipped wall time
    • IntlCalendar::getTime - Get time currently represented by the object
    • IntlCalendar::getTimeZone - Get the object?s timezone
    • IntlCalendar::getType - Get the calendar type
    • IntlCalendar::getWeekendTransition - Get time of the day at which weekend begins or ends
    • IntlCalendar::inDaylightTime - Whether the object?s time is in Daylight Savings Time
    • IntlCalendar::isEquivalentTo - Whether another calendar is equal but for a different time
    • IntlCalendar::isLenient - Whether date/time interpretation is in lenient mode
    • IntlCalendar::isSet - Whether a field is set
    • IntlCalendar::isWeekend - Whether a certain date/time is in the weekend
    • IntlCalendar::roll - Add value to field without carrying into more significant fields
    • IntlCalendar::set - Set a time field or several common fields at once
    • IntlCalendar::setFirstDayOfWeek - Set the day on which the week is deemed to start
    • IntlCalendar::setLenient - Set whether date/time interpretation is to be lenient
    • IntlCalendar::setMinimalDaysInFirstWeek - Set minimal number of days the first week in a year or month can have
    • IntlCalendar::setRepeatedWallTimeOption - Set behavior for handling repeating wall times at negative timezone offset transitions
    • IntlCalendar::setSkippedWallTimeOption - Set behavior for handling skipped wall times at positive timezone offset transitions
    • IntlCalendar::setTime - Set the calendar time in milliseconds since the epoch
    • IntlCalendar::setTimeZone - Set the timezone used by this calendar
    • IntlCalendar::toDateTime - Convert an IntlCalendar into a DateTime object
    • IntlCalendar::__construct - Private constructor for disallowing instantiation
    • IntlChar::charAge - Get the "age" of the code point
    • IntlChar::charDigitValue - Get the decimal digit value of a decimal digit character
    • IntlChar::charDirection - Get bidirectional category value for a code point
    • IntlChar::charFromName - Find Unicode character by name and return its code point value
    • IntlChar::charMirror - Get the "mirror-image" character for a code point
    • IntlChar::charName - Retrieve the name of a Unicode character
    • IntlChar::charType - Get the general category value for a code point
    • IntlChar::chr - Return Unicode character by code point value
    • IntlChar::digit - Get the decimal digit value of a code point for a given radix
    • IntlChar::enumCharNames - Enumerate all assigned Unicode characters within a range
    • IntlChar::enumCharTypes - Enumerate all code points with their Unicode general categories
    • IntlChar::foldCase - Perform case folding on a code point
    • IntlChar::forDigit - Get character representation for a given digit and radix
    • IntlChar::getBidiPairedBracket - Get the paired bracket character for a code point
    • IntlChar::getBlockCode - Get the Unicode allocation block containing a code point
    • IntlChar::getCombiningClass - Get the combining class of a code point
    • IntlChar::getFC_NFKC_Closure - Get the FC_NFKC_Closure property for a code point
    • IntlChar::getIntPropertyMaxValue - Get the max value for a Unicode property
    • IntlChar::getIntPropertyMinValue - Get the min value for a Unicode property
    • IntlChar::getIntPropertyValue - Get the value for a Unicode property for a code point
    • IntlChar::getNumericValue - Get the numeric value for a Unicode code point
    • IntlChar::getPropertyEnum - Get the property constant value for a given property name
    • IntlChar::getPropertyName - Get the Unicode name for a property
    • IntlChar::getPropertyValueEnum - Get the property value for a given value name
    • IntlChar::getPropertyValueName - Get the Unicode name for a property value
    • IntlChar::getUnicodeVersion - Get the Unicode version
    • IntlChar::hasBinaryProperty - Check a binary Unicode property for a code point
    • IntlChar::isalnum - Check if code point is an alphanumeric character
    • IntlChar::isalpha - Check if code point is a letter character
    • IntlChar::isbase - Check if code point is a base character
    • IntlChar::isblank - Check if code point is a "blank" or "horizontal space" character
    • IntlChar::iscntrl - Check if code point is a control character
    • IntlChar::isdefined - Check whether the code point is defined
    • IntlChar::isdigit - Check if code point is a digit character
    • IntlChar::isgraph - Check if code point is a graphic character
    • IntlChar::isIDIgnorable - Check if code point is an ignorable character
    • IntlChar::isIDPart - Check if code point is permissible in an identifier
    • IntlChar::isIDStart - Check if code point is permissible as the first character in an identifier
    • IntlChar::isISOControl - Check if code point is an ISO control code
    • IntlChar::isJavaIDPart - Check if code point is permissible in a Java identifier
    • IntlChar::isJavaIDStart - Check if code point is permissible as the first character in a Java identifier
    • IntlChar::isJavaSpaceChar - Check if code point is a space character according to Java
    • IntlChar::islower - Check if code point is a lowercase letter
    • IntlChar::isMirrored - Check if code point has the Bidi_Mirrored property
    • IntlChar::isprint - Check if code point is a printable character
    • IntlChar::ispunct - Check if code point is punctuation character
    • IntlChar::isspace - Check if code point is a space character
    • IntlChar::istitle - Check if code point is a titlecase letter
    • IntlChar::isUAlphabetic - Check if code point has the Alphabetic Unicode property
    • IntlChar::isULowercase - Check if code point has the Lowercase Unicode property
    • IntlChar::isupper - Check if code point has the general category "Lu" (uppercase letter)
    • IntlChar::isUUppercase - Check if code point has the Uppercase Unicode property
    • IntlChar::isUWhiteSpace - Check if code point has the White_Space Unicode property
    • IntlChar::isWhitespace - Check if code point is a whitespace character according to ICU
    • IntlChar::isxdigit - Check if code point is a hexadecimal digit
    • IntlChar::ord - Return Unicode code point value of character
    • IntlChar::tolower - Make Unicode character lowercase
    • IntlChar::totitle - Make Unicode character titlecase
    • IntlChar::toupper - Make Unicode character uppercase
    • IntlCodePointBreakIterator::getLastCodePoint - Get last code point passed over after advancing or receding the iterator
    • IntlDateFormatter::create - Create a date formatter
    • IntlDateFormatter::format - Format the date/time value as a string
    • IntlDateFormatter::formatObject - Formats an object
    • IntlDateFormatter::getCalendar - Get the calendar type used for the IntlDateFormatter
    • IntlDateFormatter::getCalendarObject - Get copy of formatter?s calendar object
    • IntlDateFormatter::getDateType - Get the datetype used for the IntlDateFormatter
    • IntlDateFormatter::getErrorCode - Get the error code from last operation
    • IntlDateFormatter::getErrorMessage - Get the error text from the last operation.
    • IntlDateFormatter::getLocale - Get the locale used by formatter
    • IntlDateFormatter::getPattern - Get the pattern used for the IntlDateFormatter
    • IntlDateFormatter::getTimeType - Get the timetype used for the IntlDateFormatter
    • IntlDateFormatter::getTimeZone - Get formatter?s timezone
    • IntlDateFormatter::getTimeZoneId - Get the timezone-id used for the IntlDateFormatter
    • IntlDateFormatter::isLenient - Get the lenient used for the IntlDateFormatter
    • IntlDateFormatter::localtime - Parse string to a field-based time value
    • IntlDateFormatter::parse - Parse string to a timestamp value
    • IntlDateFormatter::setCalendar - Sets the calendar type used by the formatter
    • IntlDateFormatter::setLenient - Set the leniency of the parser
    • IntlDateFormatter::setPattern - Set the pattern used for the IntlDateFormatter
    • IntlDateFormatter::setTimeZone - Sets formatter?s timezone
    • IntlDateFormatter::setTimeZoneId - Sets the time zone to use
    • IntlIterator::current - Get the current element
    • IntlIterator::key - Get the current key
    • IntlIterator::next - Move forward to the next element
    • IntlIterator::rewind - Rewind the iterator to the first element
    • IntlIterator::valid - Check if current position is valid
    • IntlPartsIterator::getBreakIterator - Get IntlBreakIterator backing this parts iterator
    • IntlRuleBasedBreakIterator::getBinaryRules - Get the binary form of compiled rules
    • IntlRuleBasedBreakIterator::getRules - Get the rule set used to create this object
    • IntlRuleBasedBreakIterator::getRuleStatus - Get the largest status value from the break rules that determined the current break position
    • IntlRuleBasedBreakIterator::getRuleStatusVec - Get the status values from the break rules that determined the current break position
    • IntlRuleBasedBreakIterator::__construct - Create iterator from ruleset
    • IntlTimeZone::countEquivalentIDs - Get the number of IDs in the equivalency group that includes the given ID
    • IntlTimeZone::createDefault - Create a new copy of the default timezone for this host
    • IntlTimeZone::createEnumeration - Get an enumeration over time zone IDs associated with the given country or offset
    • IntlTimeZone::createTimeZone - Create a timezone object for the given ID
    • IntlTimeZone::fromDateTimeZone - Create a timezone object from DateTimeZone
    • IntlTimeZone::getCanonicalID - Get the canonical system timezone ID or the normalized custom time zone ID for the given time zone ID
    • IntlTimeZone::getDisplayName - Get a name of this time zone suitable for presentation to the user
    • IntlTimeZone::getDSTSavings - Get the amount of time to be added to local standard time to get local wall clock time
    • IntlTimeZone::getEquivalentID - Get an ID in the equivalency group that includes the given ID
    • IntlTimeZone::getErrorCode - Get last error code on the object
    • IntlTimeZone::getErrorMessage - Get last error message on the object
    • IntlTimeZone::getGMT - Create GMT (UTC) timezone
    • IntlTimeZone::getID - Get timezone ID
    • IntlTimeZone::getOffset - Get the time zone raw and GMT offset for the given moment in time
    • IntlTimeZone::getRawOffset - Get the raw GMT offset (before taking daylight savings time into account
    • IntlTimeZone::getTZDataVersion - Get the timezone data version currently used by ICU
    • IntlTimeZone::hasSameRules - Check if this zone has the same rules and offset as another zone
    • IntlTimeZone::toDateTimeZone - Convert to DateTimeZone object
    • IntlTimeZone::useDaylightTime - Check if this time zone uses daylight savings time
    • intl_error_name - Get symbolic name for a given error code
    • intl_get_error_code - Get the last error code
    • intl_get_error_message - Get description of the last error
    • intl_is_failure - Check whether the given error code indicates failure
    • intval - 변수의 정수값을 얻습니다
    • in_array - 값이 배열 안에 존재하는지 확인
    • ip2long - Converts a string containing an (IPv4) Internet Protocol dotted address into a long integer
    • iptcembed - Embeds binary IPTC data into a JPEG image
    • iptcparse - Parse a binary IPTC block into single tags.
    • isset - 설정된 변수인지 확인
    • is_a - 객체가 이 클래스나 부모 클래스 중 하나인지 확인
    • is_array - 변수가 배열인지 확인
    • is_bool - 변수가 논리형인지 확인
    • is_callable - 변수 내용을 함수처럼 호출할 수 있는지 확인
    • is_dir - Tells whether the filename is a directory
    • is_double - 별칭: is_float
    • is_executable - Tells whether the filename is executable
    • is_file - Tells whether the filename is a regular file
    • is_finite - 값이 허용되는 유한수인지 확인
    • is_float - 변수의 자료형이 소수인지 확인합니다
    • is_infinite - 값이 무한인지 확인
    • is_int - 변수의 자료형이 정수인지 확인합니다
    • is_integer - 별칭: is_int
    • is_link - Tells whether the filename is a symbolic link
    • is_long - 별칭: is_int
    • is_nan - 값이 수가 아닌지 확인합니다
    • is_null - 변수가 NULL인지 확인합니다
    • is_numeric - 변수가 수나 수 문자열인지 확인합니다
    • is_object - 변수가 객체인지 확인합니다
    • is_readable - Tells whether a file exists and is readable
    • is_real - 별칭: is_float
    • is_resource - 변수가 자원인지 확인
    • is_scalar - 변수가 스칼라인지 확인
    • is_soap_fault - Checks if a SOAP call has failed
    • is_string - 변수의 자료형이 문자열인지 확인합니다
    • is_subclass_of - 이 클래스가 부모 클래스의 자식인지 확인
    • is_tainted - Checks whether a string is tainted
    • is_uploaded_file - Tells whether the file was uploaded via HTTP POST
    • is_writable - Tells whether the filename is writable
    • is_writeable - 별칭: is_writable
    • Iterator::current - Return the current element
    • Iterator::key - Return the key of the current element
    • Iterator::next - Move forward to next element
    • Iterator::rewind - Rewind the Iterator to the first element
    • Iterator::valid - Checks if current position is valid
    • IteratorAggregate::getIterator - Retrieve an external iterator
    • IteratorIterator::current - Get the current value
    • IteratorIterator::getInnerIterator - Get the inner iterator
    • IteratorIterator::key - Get the key of the current element
    • IteratorIterator::next - Forward to the next element
    • IteratorIterator::rewind - Rewind to the first element
    • IteratorIterator::valid - Checks if the iterator is valid
    • IteratorIterator::__construct - Create an iterator from anything that is traversable
    • iterator_apply - Call a function for every element in an iterator
    • iterator_count - Count the elements in an iterator
    • iterator_to_array - Copy the iterator into an array
  • j
    • jddayofweek - Returns the day of the week
    • jdmonthname - Returns a month name
    • jdtofrench - Converts a Julian Day Count to the French Republican Calendar
    • jdtogregorian - Converts Julian Day Count to Gregorian date
    • jdtojewish - Converts a Julian day count to a Jewish calendar date
    • jdtojulian - Converts a Julian Day Count to a Julian Calendar Date
    • jdtounix - Convert Julian Day to Unix timestamp
    • jewishtojd - Converts a date in the Jewish Calendar to Julian Day Count
    • join - 별칭: implode
    • jpeg2wbmp - Convert JPEG image file to WBMP image file
    • JsonSerializable::jsonSerialize - Specify data which should be serialized to JSON
    • json_decode - JSON 문자열 디코드
    • json_encode - 값의 JSON 표현을 반환
    • json_last_error - 발생한 마지막 오류 반환
    • json_last_error_msg - Returns the error string of the last json_encode() or json_decode() call
    • Judy::byCount - Locate the Nth index present in the Judy array
    • Judy::count - Count the number of elements in the Judy array
    • Judy::first - Search for the first index in the Judy array
    • Judy::firstEmpty - Search for the first absent index in the Judy array
    • Judy::free - Free the entire Judy array
    • Judy::getType - Return the type of the current Judy array
    • Judy::last - Search for the last index in the Judy array
    • Judy::lastEmpty - Search for the last absent index in the Judy array
    • Judy::memoryUsage - Return the memory used by the Judy array
    • Judy::next - Search for the next index in the Judy array
    • Judy::nextEmpty - Search for the next absent index in the Judy array
    • Judy::offsetExists - Whether a offset exists
    • Judy::offsetGet - Offset to retrieve
    • Judy::offsetSet - Offset to set
    • Judy::offsetUnset - Offset to unset
    • Judy::prev - Search for the previous index in the Judy array
    • Judy::prevEmpty - Search for the previous absent index in the Judy array
    • Judy::size - Return the size of the current Judy array
    • Judy::__construct - Construct a new Judy object
    • Judy::__destruct - Destruct a Judy object
    • judy_type - Return the type of a Judy array
    • judy_version - Return or print the current PHP Judy version
    • juliantojd - Converts a Julian Calendar date to Julian Day Count
  • k
    • kadm5_chpass_principal - Changes the principal's password
    • kadm5_create_principal - Creates a kerberos principal with the given parameters
    • kadm5_delete_principal - Deletes a kerberos principal
    • kadm5_destroy - Closes the connection to the admin server and releases all related resources
    • kadm5_flush - Flush all changes to the Kerberos database
    • kadm5_get_policies - Gets all policies from the Kerberos database
    • kadm5_get_principal - Gets the principal's entries from the Kerberos database
    • kadm5_get_principals - Gets all principals from the Kerberos database
    • kadm5_init_with_password - Opens a connection to the KADM5 library
    • kadm5_modify_principal - Modifies a kerberos principal with the given parameters
    • key - 배열에서 키를 가져옵니다
    • key_exists - 별칭: array_key_exists
    • krsort - 키에 의한 배열 역순 정렬
    • ksort - 키에 의한 배열 정렬
    • KTaglib_ID3v2_AttachedPictureFrame::getDescription - Returns a description for the picture in a picture frame
    • KTaglib_ID3v2_AttachedPictureFrame::getMimeType - Returns the mime type of the picture
    • KTaglib_ID3v2_AttachedPictureFrame::getType - Returns the type of the image
    • KTaglib_ID3v2_AttachedPictureFrame::savePicture - Saves the picture to a file
    • KTaglib_ID3v2_AttachedPictureFrame::setMimeType - Set's the mime type of the picture
    • KTaglib_ID3v2_AttachedPictureFrame::setPicture - Sets the frame picture to the given image
    • KTaglib_ID3v2_AttachedPictureFrame::setType - Set the type of the image
    • KTaglib_ID3v2_Frame::getSize - Returns the size of the frame in bytes
    • KTaglib_ID3v2_Frame::__toString - Returns a string representation of the frame
    • KTaglib_ID3v2_Tag::addFrame - Add a frame to the ID3v2 tag
    • KTaglib_ID3v2_Tag::getFrameList - Returns an array of ID3v2 frames, associated with the ID3v2 tag
    • KTaglib_MPEG_AudioProperties::getBitrate - Returns the bitrate of the MPEG file
    • KTaglib_MPEG_AudioProperties::getChannels - Returns the amount of channels of a MPEG file
    • KTaglib_MPEG_AudioProperties::getLayer - Returns the layer of a MPEG file
    • KTaglib_MPEG_AudioProperties::getLength - Returns the length of a MPEG file
    • KTaglib_MPEG_AudioProperties::getSampleBitrate - Returns the sample bitrate of a MPEG file
    • KTaglib_MPEG_AudioProperties::getVersion - Returns the version of a MPEG file
    • KTaglib_MPEG_AudioProperties::isCopyrighted - Returns the copyright status of an MPEG file
    • KTaglib_MPEG_AudioProperties::isOriginal - Returns if the file is marked as the original file
    • KTaglib_MPEG_AudioProperties::isProtectionEnabled - Returns if protection mechanisms of an MPEG file are enabled
    • KTaglib_MPEG_File::getAudioProperties - Returns an object that provides access to the audio properties
    • KTaglib_MPEG_File::getID3v1Tag - Returns an object representing an ID3v1 tag
    • KTaglib_MPEG_File::getID3v2Tag - Returns a ID3v2 object
    • KTaglib_MPEG_File::__construct - Opens a new file
    • KTaglib_Tag::getAlbum - Returns the title string from a ID3 tag
    • KTaglib_Tag::getArtist - Returns the artist string from a ID3 tag
    • KTaglib_Tag::getComment - Returns the comment from a ID3 tag
    • KTaglib_Tag::getGenre - Returns the genre from a ID3 tag
    • KTaglib_Tag::getTitle - Returns the title string from a ID3 tag
    • KTaglib_Tag::getTrack - Returns the track number from a ID3 tag
    • KTaglib_Tag::getYear - Returns the year from a ID3 tag
    • KTaglib_Tag::isEmpty - Returns true if the tag is empty
  • l
    • Lapack::eigenValues - This function returns the eigenvalues for a given square matrix
    • Lapack::identity - Return an identity matrix
    • Lapack::leastSquaresByFactorisation - Calculate the linear least squares solution of a matrix using QR factorisation
    • Lapack::leastSquaresBySVD - Solve the linear least squares problem, using SVD
    • Lapack::pseudoInverse - Calculate the inverse of a matrix
    • Lapack::singularValues - Calculated the singular values of a matrix
    • Lapack::solveLinearEquation - Solve a system of linear equations
    • lcfirst - Make a string's first character lowercase
    • lcg_value - 결합 선형 합동 생성기
    • lchgrp - Changes group ownership of symlink
    • lchown - Changes user ownership of symlink
    • ldap_8859_to_t61 - Translate 8859 characters to t61 characters
    • ldap_add - Add entries to LDAP directory
    • ldap_bind - Bind to LDAP directory
    • ldap_close - 별칭: ldap_unbind
    • ldap_compare - Compare value of attribute found in entry specified with DN
    • ldap_connect - Connect to an LDAP server
    • ldap_control_paged_result - Send LDAP pagination control
    • ldap_control_paged_result_response - Retrieve the LDAP pagination cookie
    • ldap_count_entries - Count the number of entries in a search
    • ldap_delete - Delete an entry from a directory
    • ldap_dn2ufn - Convert DN to User Friendly Naming format
    • ldap_err2str - Convert LDAP error number into string error message
    • ldap_errno - Return the LDAP error number of the last LDAP command
    • ldap_error - Return the LDAP error message of the last LDAP command
    • ldap_escape - Escape a string for use in an LDAP filter or DN
    • ldap_explode_dn - Splits DN into its component parts
    • ldap_first_attribute - Return first attribute
    • ldap_first_entry - Return first result id
    • ldap_first_reference - Return first reference
    • ldap_free_result - Free result memory
    • ldap_get_attributes - Get attributes from a search result entry
    • ldap_get_dn - Get the DN of a result entry
    • ldap_get_entries - Get all result entries
    • ldap_get_option - Get the current value for given option
    • ldap_get_values - Get all values from a result entry
    • ldap_get_values_len - Get all binary values from a result entry
    • ldap_list - Single-level search
    • ldap_modify - Modify an LDAP entry
    • ldap_modify_batch - Batch and execute modifications on an LDAP entry
    • ldap_mod_add - Add attribute values to current attributes
    • ldap_mod_del - Delete attribute values from current attributes
    • ldap_mod_replace - Replace attribute values with new ones
    • ldap_next_attribute - Get the next attribute in result
    • ldap_next_entry - Get next result entry
    • ldap_next_reference - Get next reference
    • ldap_parse_reference - Extract information from reference entry
    • ldap_parse_result - Extract information from result
    • ldap_read - Read an entry
    • ldap_rename - Modify the name of an entry
    • ldap_sasl_bind - Bind to LDAP directory using SASL
    • ldap_search - Search LDAP tree
    • ldap_set_option - Set the value of the given option
    • ldap_set_rebind_proc - Set a callback function to do re-binds on referral chasing
    • ldap_sort - Sort LDAP result entries on the client side
    • ldap_start_tls - Start TLS
    • ldap_t61_to_8859 - Translate t61 characters to 8859 characters
    • ldap_unbind - Unbind from LDAP directory
    • levenshtein - Calculate Levenshtein distance between two strings
    • libxml_clear_errors - Clear libxml error buffer
    • libxml_disable_entity_loader - Disable the ability to load external entities
    • libxml_get_errors - Retrieve array of errors
    • libxml_get_last_error - Retrieve last error from libxml
    • libxml_set_external_entity_loader - Changes the default external entity loader
    • libxml_set_streams_context - Set the streams context for the next libxml document load or write
    • libxml_use_internal_errors - Disable libxml errors and allow user to fetch error information as needed
    • LimitIterator::current - Get current element
    • LimitIterator::getInnerIterator - Get inner iterator
    • LimitIterator::getPosition - Return the current position
    • LimitIterator::key - Get current key
    • LimitIterator::next - Move the iterator forward
    • LimitIterator::rewind - Rewind the iterator to the specified starting offset
    • LimitIterator::seek - Seek to the given position
    • LimitIterator::valid - Check whether the current element is valid
    • LimitIterator::__construct - Construct a LimitIterator
    • link - Create a hard link
    • linkinfo - Gets information about a link
    • list - 배열처럼 변수에 할당
    • Locale::acceptFromHttp - Tries to find out best available locale based on HTTP "Accept-Language" header
    • Locale::canonicalize - Canonicalize the locale string
    • Locale::composeLocale - Returns a correctly ordered and delimited locale ID
    • Locale::filterMatches - Checks if a language tag filter matches with locale
    • Locale::getAllVariants - Gets the variants for the input locale
    • Locale::getDefault - Gets the default locale value from the INTL global 'default_locale'
    • Locale::getDisplayLanguage - Returns an appropriately localized display name for language of the inputlocale
    • Locale::getDisplayName - Returns an appropriately localized display name for the input locale
    • Locale::getDisplayRegion - Returns an appropriately localized display name for region of the input locale
    • Locale::getDisplayScript - Returns an appropriately localized display name for script of the input locale
    • Locale::getDisplayVariant - Returns an appropriately localized display name for variants of the input locale
    • Locale::getKeywords - Gets the keywords for the input locale
    • Locale::getPrimaryLanguage - Gets the primary language for the input locale
    • Locale::getRegion - Gets the region for the input locale
    • Locale::getScript - Gets the script for the input locale
    • Locale::lookup - Searches the language tag list for the best match to the language
    • Locale::parseLocale - Returns a key-value array of locale ID subtag elements.
    • Locale::setDefault - sets the default runtime locale
    • localeconv - Get numeric formatting information
    • localtime - Get the local time
    • log - 자연로그
    • log1p - Returns log(1 + number), computed in a way that is accurate even when the value of number is close to zero
    • log10 - 상용로그
    • log_cmd_delete - Callback When Deleting Documents
    • log_cmd_insert - Callback When Inserting Documents
    • log_cmd_update - Callback When Updating Documents
    • log_getmore - Callback When Retrieving Next Cursor Batch
    • log_killcursor - Callback When Executing KILLCURSOR operations
    • log_reply - Callback When Reading the MongoDB reply
    • log_write_batch - Callback When Writing Batches
    • long2ip - Converts an long integer address into a string in (IPv4) Internet standard dotted format
    • lstat - Gives information about a file or symbolic link
    • ltrim - 문자열 시작에서 공백(이나 다른 문자)를 제거
    • Lua::assign - Assign a PHP variable to Lua
    • Lua::call - Call Lua functions
    • Lua::eval - Evaluate a string as Lua code
    • Lua::getVersion - The getversion purpose
    • Lua::include - Parse a Lua script file
    • Lua::registerCallback - Register a PHP function to Lua
    • Lua::__construct - Lua constructor
    • LuaClosure::__invoke - invoke luaclosure
    • lzf_compress - LZF compression
    • lzf_decompress - LZF decompression
    • lzf_optimized_for - Determines what LZF extension was optimized for
  • m
    • magic_quotes_runtime - 별칭: set_magic_quotes_runtime
    • mail - 메일을 보냅니다
    • mailparse_determine_best_xfer_encoding - Gets the best way of encoding
    • mailparse_msg_create - Create a mime mail resource
    • mailparse_msg_extract_part - Extracts/decodes a message section
    • mailparse_msg_extract_part_file - Extracts/decodes a message section
    • mailparse_msg_extract_whole_part_file - Extracts a message section including headers without decoding the transfer encoding
    • mailparse_msg_free - Frees a MIME resource
    • mailparse_msg_get_part - Returns a handle on a given section in a mimemessage
    • mailparse_msg_get_part_data - Returns an associative array of info about the message
    • mailparse_msg_get_structure - Returns an array of mime section names in the supplied message
    • mailparse_msg_parse - Incrementally parse data into buffer
    • mailparse_msg_parse_file - Parses a file
    • mailparse_rfc822_parse_addresses - Parse RFC 822 compliant addresses
    • mailparse_stream_encode - Streams data from source file pointer, apply encoding and write to destfp
    • mailparse_uudecode_all - Scans the data from fp and extract each embedded uuencoded file
    • main - Dummy for main
    • max - 최고값을 찾습니다
    • maxdb_affected_rows - Gets the number of affected rows in a previous MaxDB operation
    • maxdb_autocommit - Turns on or off auto-commiting database modifications
    • maxdb_bind_param - 별칭: maxdb_stmt_bind_param
    • maxdb_bind_result - 별칭: maxdb_stmt_bind_result
    • maxdb_change_user - Changes the user of the specified database connection
    • maxdb_character_set_name - Returns the default character set for the database connection
    • maxdb_client_encoding - 별칭: maxdb_character_set_name
    • maxdb_close - Closes a previously opened database connection
    • maxdb_close_long_data - 별칭: maxdb_stmt_close_long_data
    • maxdb_commit - Commits the current transaction
    • maxdb_connect - Open a new connection to the MaxDB server
    • maxdb_connect_errno - Returns the error code from last connect call
    • maxdb_connect_error - Returns a string description of the last connect error
    • maxdb_data_seek - Adjusts the result pointer to an arbitary row in the result
    • maxdb_debug - Performs debugging operations
    • maxdb_disable_reads_from_master - Disable reads from master
    • maxdb_disable_rpl_parse - Disable RPL parse
    • maxdb_dump_debug_info - Dump debugging information into the log
    • maxdb_embedded_connect - Open a connection to an embedded MaxDB server
    • maxdb_enable_reads_from_master - Enable reads from master
    • maxdb_enable_rpl_parse - Enable RPL parse
    • maxdb_errno - Returns the error code for the most recent function call
    • maxdb_error - Returns a string description of the last error
    • maxdb_escape_string - 별칭: maxdb_real_escape_string
    • maxdb_execute - 별칭: maxdb_stmt_execute
    • maxdb_fetch - 별칭: maxdb_stmt_fetch
    • maxdb_fetch_array - Fetch a result row as an associative, a numeric array, or both
    • maxdb_fetch_assoc - Fetch a result row as an associative array
    • maxdb_fetch_field - Returns the next field in the result set
    • maxdb_fetch_fields - Returns an array of resources representing the fields in a result set
    • maxdb_fetch_field_direct - Fetch meta-data for a single field
    • maxdb_fetch_lengths - Returns the lengths of the columns of the current row in the result set
    • maxdb_fetch_object - Returns the current row of a result set as an object
    • maxdb_fetch_row - Get a result row as an enumerated array
    • maxdb_field_count - Returns the number of columns for the most recent query
    • maxdb_field_seek - Set result pointer to a specified field offset
    • maxdb_field_tell - Get current field offset of a result pointer
    • maxdb_free_result - Frees the memory associated with a result
    • maxdb_get_client_info - Returns the MaxDB client version as a string
    • maxdb_get_client_version - Get MaxDB client info
    • maxdb_get_host_info - Returns a string representing the type of connection used
    • maxdb_get_metadata - 별칭: maxdb_stmt_result_metadata
    • maxdb_get_proto_info - Returns the version of the MaxDB protocol used
    • maxdb_get_server_info - Returns the version of the MaxDB server
    • maxdb_get_server_version - Returns the version of the MaxDB server as an integer
    • maxdb_info - Retrieves information about the most recently executed query
    • maxdb_init - Initializes MaxDB and returns an resource for use with maxdb_real_connect
    • maxdb_insert_id - Returns the auto generated id used in the last query
    • maxdb_kill - Disconnects from a MaxDB server
    • maxdb_master_query - Enforce execution of a query on the master in a master/slave setup
    • maxdb_more_results - Check if there any more query results from a multi query
    • maxdb_multi_query - Performs a query on the database
    • maxdb_next_result - Prepare next result from multi_query
    • maxdb_num_fields - Get the number of fields in a result
    • maxdb_num_rows - Gets the number of rows in a result
    • maxdb_options - Set options
    • maxdb_param_count - 별칭: maxdb_stmt_param_count
    • maxdb_ping - Pings a server connection, or tries to reconnect if the connection has gone down
    • maxdb_prepare - Prepare an SQL statement for execution
    • maxdb_query - Performs a query on the database
    • maxdb_real_connect - Opens a connection to a MaxDB server
    • maxdb_real_escape_string - Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection
    • maxdb_real_query - Execute an SQL query
    • maxdb_report - Enables or disables internal report functions
    • maxdb_rollback - Rolls back current transaction
    • maxdb_rpl_parse_enabled - Check if RPL parse is enabled
    • maxdb_rpl_probe - RPL probe
    • maxdb_rpl_query_type - Returns RPL query type
    • maxdb_select_db - Selects the default database for database queries
    • maxdb_send_long_data - 별칭: maxdb_stmt_send_long_data
    • maxdb_send_query - Send the query and return
    • maxdb_server_end - Shut down the embedded server
    • maxdb_server_init - Initialize embedded server
    • maxdb_set_opt - 별칭: maxdb_options
    • maxdb_sqlstate - Returns the SQLSTATE error from previous MaxDB operation
    • maxdb_ssl_set - Used for establishing secure connections using SSL
    • maxdb_stat - Gets the current system status
    • maxdb_stmt_affected_rows - Returns the total number of rows changed, deleted, or inserted by the last executed statement
    • maxdb_stmt_bind_param - Binds variables to a prepared statement as parameters
    • maxdb_stmt_bind_result - Binds variables to a prepared statement for result storage
    • maxdb_stmt_close - Closes a prepared statement
    • maxdb_stmt_close_long_data - Ends a sequence of maxdb_stmt_send_long_data
    • maxdb_stmt_data_seek - Seeks to an arbitray row in statement result set
    • maxdb_stmt_errno - Returns the error code for the most recent statement call
    • maxdb_stmt_error - Returns a string description for last statement error
    • maxdb_stmt_execute - Executes a prepared Query
    • maxdb_stmt_fetch - Fetch results from a prepared statement into the bound variables
    • maxdb_stmt_free_result - Frees stored result memory for the given statement handle
    • maxdb_stmt_init - Initializes a statement and returns an resource for use with maxdb_stmt_prepare
    • maxdb_stmt_num_rows - Return the number of rows in statements result set
    • maxdb_stmt_param_count - Returns the number of parameter for the given statement
    • maxdb_stmt_prepare - Prepare an SQL statement for execution
    • maxdb_stmt_reset - Resets a prepared statement
    • maxdb_stmt_result_metadata - Returns result set metadata from a prepared statement
    • maxdb_stmt_send_long_data - Send data in blocks
    • maxdb_stmt_sqlstate - Returns SQLSTATE error from previous statement operation
    • maxdb_stmt_store_result - Transfers a result set from a prepared statement
    • maxdb_store_result - Transfers a result set from the last query
    • maxdb_thread_id - Returns the thread ID for the current connection
    • maxdb_thread_safe - Returns whether thread safety is given or not
    • maxdb_use_result - Initiate a result set retrieval
    • maxdb_warning_count - Returns the number of warnings from the last query for the given link
    • mb_check_encoding - Check if the string is valid for the specified encoding
    • mb_convert_case - Perform case folding on a string
    • mb_convert_encoding - Convert character encoding
    • mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
    • mb_convert_variables - Convert character code in variable(s)
    • mb_decode_mimeheader - Decode string in MIME header field
    • mb_decode_numericentity - Decode HTML numeric string reference to character
    • mb_detect_encoding - Detect character encoding
    • mb_detect_order - Set/Get character encoding detection order
    • mb_encode_mimeheader - Encode string for MIME header
    • mb_encode_numericentity - Encode character to HTML numeric string reference
    • mb_encoding_aliases - Get aliases of a known encoding type
    • mb_ereg - Regular expression match with multibyte support
    • mb_eregi - Regular expression match ignoring case with multibyte support
    • mb_eregi_replace - Replace regular expression with multibyte support ignoring case
    • mb_ereg_match - Regular expression match for multibyte string
    • mb_ereg_replace - Replace regular expression with multibyte support
    • mb_ereg_replace_callback - Perform a regular expresssion seach and replace with multibyte support using a callback
    • mb_ereg_search - Multibyte regular expression match for predefined multibyte string
    • mb_ereg_search_getpos - Returns start point for next regular expression match
    • mb_ereg_search_getregs - Retrieve the result from the last multibyte regular expression match
    • mb_ereg_search_init - Setup string and regular expression for a multibyte regular expression match
    • mb_ereg_search_pos - Returns position and length of a matched part of the multibyte regular expression for a predefined multibyte string
    • mb_ereg_search_regs - Returns the matched part of a multibyte regular expression
    • mb_ereg_search_setpos - Set start point of next regular expression match
    • mb_get_info - Get internal settings of mbstring
    • mb_http_input - Detect HTTP input character encoding
    • mb_http_output - Set/Get HTTP output character encoding
    • mb_internal_encoding - Set/Get internal character encoding
    • mb_language - Set/Get current language
    • mb_list_encodings - Returns an array of all supported encodings
    • mb_output_handler - Callback function converts character encoding in output buffer
    • mb_parse_str - Parse GET/POST/COOKIE data and set global variable
    • mb_preferred_mime_name - Get MIME charset string
    • mb_regex_encoding - Set/Get character encoding for multibyte regex
    • mb_regex_set_options - Set/Get the default options for mbregex functions
    • mb_send_mail - Send encoded mail
    • mb_split - Split multibyte string using regular expression
    • mb_strcut - Get part of string
    • mb_strimwidth - Get truncated string with specified width
    • mb_stripos - Finds position of first occurrence of a string within another, case insensitive
    • mb_stristr - Finds first occurrence of a string within another, case insensitive
    • mb_strlen - Get string length
    • mb_strpos - Find position of first occurrence of string in a string
    • mb_strrchr - Finds the last occurrence of a character in a string within another
    • mb_strrichr - Finds the last occurrence of a character in a string within another, case insensitive
    • mb_strripos - Finds position of last occurrence of a string within another, case insensitive
    • mb_strrpos - Find position of last occurrence of a string in a string
    • mb_strstr - Finds first occurrence of a string within another
    • mb_strtolower - Make a string lowercase
    • mb_strtoupper - Make a string uppercase
    • mb_strwidth - Return width of string
    • mb_substitute_character - Set/Get substitution character
    • mb_substr - Get part of string
    • mb_substr_count - Count the number of substring occurrences
    • mcrypt_cbc - Encrypts/decrypts data in CBC mode
    • mcrypt_cfb - Encrypts/decrypts data in CFB mode
    • mcrypt_create_iv - Creates an initialization vector (IV) from a random source
    • mcrypt_decrypt - Decrypts crypttext with given parameters
    • mcrypt_ecb - Deprecated: Encrypts/decrypts data in ECB mode
    • mcrypt_encrypt - Encrypts plaintext with given parameters
    • mcrypt_enc_get_algorithms_name - Returns the name of the opened algorithm
    • mcrypt_enc_get_block_size - Returns the blocksize of the opened algorithm
    • mcrypt_enc_get_iv_size - Returns the size of the IV of the opened algorithm
    • mcrypt_enc_get_key_size - Returns the maximum supported keysize of the opened mode
    • mcrypt_enc_get_modes_name - Returns the name of the opened mode
    • mcrypt_enc_get_supported_key_sizes - Returns an array with the supported keysizes of the opened algorithm
    • mcrypt_enc_is_block_algorithm - Checks whether the algorithm of the opened mode is a block algorithm
    • mcrypt_enc_is_block_algorithm_mode - Checks whether the encryption of the opened mode works on blocks
    • mcrypt_enc_is_block_mode - Checks whether the opened mode outputs blocks
    • mcrypt_enc_self_test - Runs a self test on the opened module
    • mcrypt_generic - This function encrypts data
    • mcrypt_generic_deinit - This function deinitializes an encryption module
    • mcrypt_generic_end - This function terminates encryption
    • mcrypt_generic_init - This function initializes all buffers needed for encryption
    • mcrypt_get_block_size - Gets the block size of the specified cipher
    • mcrypt_get_cipher_name - Gets the name of the specified cipher
    • mcrypt_get_iv_size - Returns the size of the IV belonging to a specific cipher/mode combination
    • mcrypt_get_key_size - Gets the key size of the specified cipher
    • mcrypt_list_algorithms - Gets an array of all supported ciphers
    • mcrypt_list_modes - Gets an array of all supported modes
    • mcrypt_module_close - Closes the mcrypt module
    • mcrypt_module_get_algo_block_size - Returns the blocksize of the specified algorithm
    • mcrypt_module_get_algo_key_size - Returns the maximum supported keysize of the opened mode
    • mcrypt_module_get_supported_key_sizes - Returns an array with the supported keysizes of the opened algorithm
    • mcrypt_module_is_block_algorithm - This function checks whether the specified algorithm is a block algorithm
    • mcrypt_module_is_block_algorithm_mode - Returns if the specified module is a block algorithm or not
    • mcrypt_module_is_block_mode - Returns if the specified mode outputs blocks or not
    • mcrypt_module_open - Opens the module of the algorithm and the mode to be used
    • mcrypt_module_self_test - This function runs a self test on the specified module
    • mcrypt_ofb - Encrypts/decrypts data in OFB mode
    • md5 - 문자열의 md5 해시를 계산
    • md5_file - 주어진 파일의 md5 해시를 계산
    • mdecrypt_generic - Decrypts data
    • Memcache::add - Add an item to the server
    • Memcache::addServer - Add a memcached server to connection pool
    • Memcache::close - Close memcached server connection
    • Memcache::connect - Open memcached server connection
    • Memcache::decrement - Decrement item's value
    • Memcache::delete - Delete item from the server
    • Memcache::flush - Flush all existing items at the server
    • Memcache::get - Retrieve item from the server
    • Memcache::getExtendedStats - Get statistics from all servers in pool
    • Memcache::getServerStatus - Returns server status
    • Memcache::getStats - Get statistics of the server
    • Memcache::getVersion - Return version of the server
    • Memcache::increment - Increment item's value
    • Memcache::pconnect - Open memcached server persistent connection
    • Memcache::replace - Replace value of the existing item
    • Memcache::set - Store data at the server
    • Memcache::setCompressThreshold - Enable automatic compression of large values
    • Memcache::setServerParams - Changes server parameters and status at runtime
    • Memcached::add - Add an item under a new key
    • Memcached::addByKey - Add an item under a new key on a specific server
    • Memcached::addServer - Add a server to the server pool
    • Memcached::addServers - Add multiple servers to the server pool
    • Memcached::append - Append data to an existing item
    • Memcached::appendByKey - Append data to an existing item on a specific server
    • Memcached::cas - Compare and swap an item
    • Memcached::casByKey - Compare and swap an item on a specific server
    • Memcached::decrement - Decrement numeric item's value
    • Memcached::decrementByKey - Decrement numeric item's value, stored on a specific server
    • Memcached::delete - Delete an item
    • Memcached::deleteByKey - Delete an item from a specific server
    • Memcached::deleteMulti - Delete multiple items
    • Memcached::deleteMultiByKey - Delete multiple items from a specific server
    • Memcached::fetch - Fetch the next result
    • Memcached::fetchAll - Fetch all the remaining results
    • Memcached::flush - Invalidate all items in the cache
    • Memcached::get - Retrieve an item
    • Memcached::getAllKeys - Gets the keys stored on all the servers
    • Memcached::getByKey - Retrieve an item from a specific server
    • Memcached::getDelayed - Request multiple items
    • Memcached::getDelayedByKey - Request multiple items from a specific server
    • Memcached::getMulti - Retrieve multiple items
    • Memcached::getMultiByKey - Retrieve multiple items from a specific server
    • Memcached::getOption - Retrieve a Memcached option value
    • Memcached::getResultCode - Return the result code of the last operation
    • Memcached::getResultMessage - Return the message describing the result of the last operation
    • Memcached::getServerByKey - Map a key to a server
    • Memcached::getServerList - Get the list of the servers in the pool
    • Memcached::getStats - Get server pool statistics
    • Memcached::getVersion - Get server pool version info
    • Memcached::increment - Increment numeric item's value
    • Memcached::incrementByKey - Increment numeric item's value, stored on a specific server
    • Memcached::isPersistent - Check if a persitent connection to memcache is being used
    • Memcached::isPristine - Check if the instance was recently created
    • Memcached::prepend - Prepend data to an existing item
    • Memcached::prependByKey - Prepend data to an existing item on a specific server
    • Memcached::quit - Close any open connections
    • Memcached::replace - Replace the item under an existing key
    • Memcached::replaceByKey - Replace the item under an existing key on a specific server
    • Memcached::resetServerList - Clears all servers from the server list
    • Memcached::set - Store an item
    • Memcached::setByKey - Store an item on a specific server
    • Memcached::setMulti - Store multiple items
    • Memcached::setMultiByKey - Store multiple items on a specific server
    • Memcached::setOption - Set a Memcached option
    • Memcached::setOptions - Set Memcached options
    • Memcached::setSaslAuthData - Set the credentials to use for authentication
    • Memcached::touch - Set a new expiration on an item
    • Memcached::touchByKey - Set a new expiration on an item on a specific server
    • Memcached::__construct - Create a Memcached instance
    • memcache_debug - Turn debug output on/off
    • memory_get_peak_usage - Returns the peak of memory allocated by PHP
    • memory_get_usage - Returns the amount of memory allocated to PHP
    • MessageFormatter::create - Constructs a new Message Formatter
    • MessageFormatter::format - Format the message
    • MessageFormatter::formatMessage - Quick format message
    • MessageFormatter::getErrorCode - Get the error code from last operation
    • MessageFormatter::getErrorMessage - Get the error text from the last operation
    • MessageFormatter::getLocale - Get the locale for which the formatter was created.
    • MessageFormatter::getPattern - Get the pattern used by the formatter
    • MessageFormatter::parse - Parse input string according to pattern
    • MessageFormatter::parseMessage - Quick parse input string
    • MessageFormatter::setPattern - Set the pattern used by the formatter
    • metaphone - 문자열의 메타폰 키를 계산
    • method_exists - 클래스 메쏘드가 존재하는지 확인
    • mhash - Computes hash
    • mhash_count - Gets the highest available hash ID
    • mhash_get_block_size - Gets the block size of the specified hash
    • mhash_get_hash_name - Gets the name of the specified hash
    • mhash_keygen_s2k - Generates a key
    • microtime - Return current Unix timestamp with microseconds
    • mime_content_type - Detect MIME Content-type for a file
    • min - 최소값을 찾습니다
    • ming_keypress - Returns the action flag for keyPress(char)
    • ming_setcubicthreshold - Set cubic threshold
    • ming_setscale - Set the global scaling factor.
    • ming_setswfcompression - Sets the SWF output compression
    • ming_useconstants - Use constant pool
    • ming_useswfversion - Sets the SWF version
    • mkdir - Makes directory
    • mktime - Get Unix timestamp for a date
    • money_format - Formats a number as a currency string
    • Mongo::connectUtil - Connects with a database server
    • Mongo::getPoolSize - Get pool size for connection pools
    • Mongo::getSlave - Returns the address being used by this for slaveOkay reads
    • Mongo::getSlaveOkay - Get slaveOkay setting for this connection
    • Mongo::poolDebug - Returns information about all connection pools.
    • Mongo::setPoolSize - Set the size for future connection pools.
    • Mongo::setSlaveOkay - Change slaveOkay setting for this connection
    • Mongo::switchSlave - Choose a new secondary for slaveOkay reads
    • Mongo::__construct - The __construct purpose
    • MongoBinData::__construct - Creates a new binary data object.
    • MongoBinData::__toString - The string representation of this binary data object.
    • MongoClient::close - Closes this connection
    • MongoClient::connect - Connects to a database server
    • MongoClient::dropDB - Drops a database [deprecated]
    • MongoClient::getConnections - Return info about all open connections
    • MongoClient::getHosts - Updates status for all associated hosts
    • MongoClient::getReadPreference - Get the read preference for this connection
    • MongoClient::getWriteConcern - Get the write concern for this connection
    • MongoClient::killCursor - Kills a specific cursor on the server
    • MongoClient::listDBs - Lists all of the databases available.
    • MongoClient::selectCollection - Gets a database collection
    • MongoClient::selectDB - Gets a database
    • MongoClient::setReadPreference - Set the read preference for this connection
    • MongoClient::setWriteConcern - Set the write concern for this connection
    • MongoClient::__construct - Creates a new database connection object
    • MongoClient::__get - Gets a database
    • MongoClient::__toString - String representation of this connection
    • MongoCode::__construct - Creates a new code object
    • MongoCode::__toString - Returns this code as a string
    • MongoCollection::aggregate - Perform an aggregation using the aggregation framework
    • MongoCollection::aggregateCursor - Execute an aggregation pipeline command and retrieve results through a cursor
    • MongoCollection::batchInsert - Inserts multiple documents into this collection
    • MongoCollection::count - Counts the number of documents in this collection
    • MongoCollection::createDBRef - Creates a database reference
    • MongoCollection::createIndex - Creates an index on the specified field(s) if it does not already exist.
    • MongoCollection::deleteIndex - Deletes an index from this collection
    • MongoCollection::deleteIndexes - Delete all indices for this collection
    • MongoCollection::distinct - Retrieve a list of distinct values for the given key across a collection.
    • MongoCollection::drop - Drops this collection
    • MongoCollection::ensureIndex - Creates an index on the specified field(s) if it does not already exist.
    • MongoCollection::find - Queries this collection, returning a MongoCursor for the result set
    • MongoCollection::findAndModify - Update a document and return it
    • MongoCollection::findOne - Queries this collection, returning a single element
    • MongoCollection::getDBRef - Fetches the document pointed to by a database reference
    • MongoCollection::getIndexInfo - Returns information about indexes on this collection
    • MongoCollection::getName - Returns this collection's name
    • MongoCollection::getReadPreference - Get the read preference for this collection
    • MongoCollection::getSlaveOkay - Get slaveOkay setting for this collection
    • MongoCollection::getWriteConcern - Get the write concern for this collection
    • MongoCollection::group - Performs an operation similar to SQL's GROUP BY command
    • MongoCollection::insert - Inserts a document into the collection
    • MongoCollection::parallelCollectionScan - Returns an array of cursors to iterator over a full collection in parallel
    • MongoCollection::remove - Remove records from this collection
    • MongoCollection::save - Saves a document to this collection
    • MongoCollection::setReadPreference - Set the read preference for this collection
    • MongoCollection::setSlaveOkay - Change slaveOkay setting for this collection
    • MongoCollection::setWriteConcern - Set the write concern for this database
    • MongoCollection::toIndexString - Converts keys specifying an index to its identifying string
    • MongoCollection::update - Update records based on a given criteria
    • MongoCollection::validate - Validates this collection
    • MongoCollection::__construct - Creates a new collection
    • MongoCollection::__get - Gets a collection
    • MongoCollection::__toString - String representation of this collection
    • MongoCommandCursor::batchSize - Limits the number of elements returned in one batch.
    • MongoCommandCursor::createFromDocument - Create a new command cursor from an existing command response document
    • MongoCommandCursor::current - Returns the current element
    • MongoCommandCursor::dead - Checks if there are results that have not yet been sent from the database
    • MongoCommandCursor::getReadPreference - Get the read preference for this command
    • MongoCommandCursor::info - Gets information about the cursor's creation and iteration
    • MongoCommandCursor::key - Returns the current result's index within the result set
    • MongoCommandCursor::next - Advances the cursor to the next result
    • MongoCommandCursor::rewind - Executes the command and resets the cursor to the start of the result set
    • MongoCommandCursor::setReadPreference - Set the read preference for this command
    • MongoCommandCursor::timeout - Sets a client-side timeout for this command
    • MongoCommandCursor::valid - Checks if the cursor is reading a valid result.
    • MongoCommandCursor::__construct - Create a new command cursor
    • MongoCursor::addOption - Adds a top-level key/value pair to a query
    • MongoCursor::awaitData - Sets whether this cursor will wait for a while for a tailable cursor to return more data
    • MongoCursor::batchSize - Limits the number of elements returned in one batch.
    • MongoCursor::count - Counts the number of results for this query
    • MongoCursor::current - Returns the current element
    • MongoCursor::dead - Checks if there are results that have not yet been sent from the database
    • MongoCursor::doQuery - Execute the query.
    • MongoCursor::explain - Return an explanation of the query, often useful for optimization and debugging
    • MongoCursor::fields - Sets the fields for a query
    • MongoCursor::getNext - Advances the cursor to the next result, and returns that result
    • MongoCursor::getReadPreference - Get the read preference for this query
    • MongoCursor::hasNext - Checks if there are any more elements in this cursor
    • MongoCursor::hint - Gives the database a hint about the query
    • MongoCursor::immortal - Sets whether this cursor will timeout
    • MongoCursor::info - Gets information about the cursor's creation and iteration
    • MongoCursor::key - Returns the current result's _id, or its index within the result set
    • MongoCursor::limit - Limits the number of results returned
    • MongoCursor::maxTimeMS - Sets a server-side timeout for this query
    • MongoCursor::next - Advances the cursor to the next result, and returns that result
    • MongoCursor::partial - If this query should fetch partial results from mongos if a shard is down
    • MongoCursor::reset - Clears the cursor
    • MongoCursor::rewind - Returns the cursor to the beginning of the result set
    • MongoCursor::setFlag - Sets arbitrary flags in case there is no method available the specific flag
    • MongoCursor::setReadPreference - Set the read preference for this query
    • MongoCursor::skip - Skips a number of results
    • MongoCursor::slaveOkay - Sets whether this query can be done on a secondary [deprecated]
    • MongoCursor::snapshot - Use snapshot mode for the query
    • MongoCursor::sort - Sorts the results by given fields
    • MongoCursor::tailable - Sets whether this cursor will be left open after fetching the last results
    • MongoCursor::timeout - Sets a client-side timeout for this query
    • MongoCursor::valid - Checks if the cursor is reading a valid result.
    • MongoCursor::__construct - Create a new cursor
    • MongoCursorException::getHost - The hostname of the server that encountered the error
    • MongoCursorInterface::batchSize - Limits the number of elements returned in one batch.
    • MongoCursorInterface::dead - Checks if there are results that have not yet been sent from the database
    • MongoCursorInterface::getReadPreference - Get the read preference for this query
    • MongoCursorInterface::info - Gets information about the cursor's creation and iteration
    • MongoCursorInterface::setReadPreference - Set the read preference for this query
    • MongoCursorInterface::timeout - Sets a client-side timeout for this query
    • MongoDate::toDateTime - Returns a DateTime object representing this date
    • MongoDate::__construct - Creates a new date.
    • MongoDate::__toString - Returns a string representation of this date
    • MongoDB::authenticate - Log in to this database
    • MongoDB::command - Execute a database command
    • MongoDB::createCollection - Creates a collection
    • MongoDB::createDBRef - Creates a database reference
    • MongoDB::drop - Drops this database
    • MongoDB::dropCollection - Drops a collection [deprecated]
    • MongoDB::execute - Runs JavaScript code on the database server [deprecated]
    • MongoDB::forceError - Creates a database error
    • MongoDB::getCollectionInfo - Returns information about collections in this database
    • MongoDB::getCollectionNames - Gets an array of names for all collections in this database
    • MongoDB::getDBRef - Fetches the document pointed to by a database reference
    • MongoDB::getGridFS - Fetches toolkit for dealing with files stored in this database
    • MongoDB::getProfilingLevel - Gets this database's profiling level
    • MongoDB::getReadPreference - Get the read preference for this database
    • MongoDB::getSlaveOkay - Get slaveOkay setting for this database
    • MongoDB::getWriteConcern - Get the write concern for this database
    • MongoDB::lastError - Check if there was an error on the most recent db operation performed
    • MongoDB::listCollections - Gets an array of MongoCollection objects for all collections in this database
    • MongoDB::prevError - Checks for the last error thrown during a database operation
    • MongoDB::repair - Repairs and compacts this database
    • MongoDB::resetError - Clears any flagged errors on the database
    • MongoDB::selectCollection - Gets a collection
    • MongoDB::setProfilingLevel - Sets this database's profiling level
    • MongoDB::setReadPreference - Set the read preference for this database
    • MongoDB::setSlaveOkay - Change slaveOkay setting for this database
    • MongoDB::setWriteConcern - Set the write concern for this database
    • MongoDB::__construct - Creates a new database
    • MongoDB::__get - Gets a collection
    • MongoDB::__toString - The name of this database
    • MongoDB context options - MongoDB context option listing
    • MongoDBRef::create - Creates a new database reference
    • MongoDBRef::get - Fetches the object pointed to by a reference
    • MongoDBRef::isRef - Checks if an array is a database reference
    • MongoDB\BSON\Binary::getData - Returns the Binary's data
    • MongoDB\BSON\Binary::getType - Returns the Binary's type
    • MongoDB\BSON\Binary::__construct - Construct a new Binary
    • MongoDB\BSON\Decimal128::__construct - Construct a new Decimal128
    • MongoDB\BSON\Decimal128::__toString - Returns the string representation of this Decimal128
    • MongoDB\BSON\fromJSON - Returns the BSON representation of a JSON value
    • MongoDB\BSON\fromPHP - Returns the BSON representation of a PHP value
    • MongoDB\BSON\Javascript::__construct - Construct a new Javascript
    • MongoDB\BSON\MaxKey::__construct - Construct a new MaxKey
    • MongoDB\BSON\MinKey::__construct - Construct a new MinKey
    • MongoDB\BSON\ObjectID::__construct - Construct a new ObjectID
    • MongoDB\BSON\ObjectID::__toString - Returns the hexidecimal representation of this ObjectID
    • MongoDB\BSON\Regex::getFlags - Returns the Regex's flags
    • MongoDB\BSON\Regex::getPattern - Returns the Regex's pattern
    • MongoDB\BSON\Regex::__construct - Construct a new Regex
    • MongoDB\BSON\Regex::__toString - Returns the string representation of this Regex
    • MongoDB\BSON\Serializable::bsonSerialize - Provides an array or document to serialize as BSON
    • MongoDB\BSON\Timestamp::__construct - Construct a new Timestamp
    • MongoDB\BSON\Timestamp::__toString - Returns the string representation of this Timestamp
    • MongoDB\BSON\toJSON - Returns the JSON representation of a BSON value
    • MongoDB\BSON\toPHP - Returns the PHP representation of a BSON value
    • MongoDB\BSON\Unserializable::bsonUnserialize - Constructs the object from a BSON array or document
    • MongoDB\BSON\UTCDateTime::toDateTime - Returns the DateTime representation of this UTCDateTime
    • MongoDB\BSON\UTCDateTime::__construct - Construct a new UTCDateTime
    • MongoDB\BSON\UTCDateTime::__toString - Returns the string representation of this UTCDateTime
    • MongoDB\Driver\BulkWrite::count - Count number of write operations in the bulk
    • MongoDB\Driver\BulkWrite::delete - Add a delete operation to the bulk
    • MongoDB\Driver\BulkWrite::insert - Add an insert operation to the bulk
    • MongoDB\Driver\BulkWrite::update - Add an update operation to the bulk
    • MongoDB\Driver\BulkWrite::__construct - Create a new BulkWrite
    • MongoDB\Driver\Command::__construct - Create a new Command
    • MongoDB\Driver\Cursor::getId - Returns the ID for this cursor
    • MongoDB\Driver\Cursor::getServer - Returns the server associated with this cursor
    • MongoDB\Driver\Cursor::isDead - Checks if the cursor is still open on the server
    • MongoDB\Driver\Cursor::setTypeMap - Sets a type map to use for BSON unserialization
    • MongoDB\Driver\Cursor::toArray - Returns an array containing all results for this cursor
    • MongoDB\Driver\Cursor::__construct - Create a new Cursor (not used)
    • MongoDB\Driver\CursorId::__construct - Create a new CursorId (not used)
    • MongoDB\Driver\CursorId::__toString - String representation of the cursor ID
    • MongoDB\Driver\Exception\WriteException::getWriteResult - Returns the WriteResult for the failed write operation
    • MongoDB\Driver\Manager::executeBulkWrite - Execute one or more write operations
    • MongoDB\Driver\Manager::executeCommand - Execute a database command
    • MongoDB\Driver\Manager::executeQuery - Execute a database query
    • MongoDB\Driver\Manager::getServers - Return the servers to which this manager is connected
    • MongoDB\Driver\Manager::selectServer - Select a server matching a read preference
    • MongoDB\Driver\Manager::__construct - Create new MongoDB Manager
    • MongoDB\Driver\Query::__construct - Construct new Query
    • MongoDB\Driver\ReadConcern::bsonSerialize - Returns an object for BSON serialization
    • MongoDB\Driver\ReadConcern::getLevel - Returns the ReadConcern's "level" option
    • MongoDB\Driver\ReadConcern::__construct - Construct immutable ReadConcern
    • MongoDB\Driver\ReadPreference::bsonSerialize - Returns an object for BSON serialization
    • MongoDB\Driver\ReadPreference::getMode - Returns the ReadPreference's "mode" option
    • MongoDB\Driver\ReadPreference::getTagSets - Returns the ReadPreference's "tagSets" option
    • MongoDB\Driver\ReadPreference::__construct - Construct immutable ReadPreference
    • MongoDB\Driver\Server::executeBulkWrite - Execute one or more write operations on this server
    • MongoDB\Driver\Server::executeCommand - Execute a database command on this server
    • MongoDB\Driver\Server::executeQuery - Execute a database query on this server
    • MongoDB\Driver\Server::getHost - Returns the hostname of this server
    • MongoDB\Driver\Server::getInfo - Returns an array of information about this server
    • MongoDB\Driver\Server::getLatency - Returns the latency of this server
    • MongoDB\Driver\Server::getPort - Returns the port on which this server is listening
    • MongoDB\Driver\Server::getTags - Returns an array of tags describing this server in a replica set
    • MongoDB\Driver\Server::getType - Returns an integer denoting the type of this server
    • MongoDB\Driver\Server::isArbiter - Checks if this server is an arbiter member of a replica set
    • MongoDB\Driver\Server::isHidden - Checks if this server is a hidden member of a replica set
    • MongoDB\Driver\Server::isPassive - Checks if this server is a passive member of a replica set
    • MongoDB\Driver\Server::isPrimary - Checks if this server is a primary member of a replica set
    • MongoDB\Driver\Server::isSecondary - Checks if this server is a secondary member of a replica set
    • MongoDB\Driver\Server::__construct - Create a new Server (not used)
    • MongoDB\Driver\WriteConcern::bsonSerialize - Returns an object for BSON serialization
    • MongoDB\Driver\WriteConcern::getJournal - Returns the WriteConcern's "journal" option
    • MongoDB\Driver\WriteConcern::getW - Returns the WriteConcern's "w" option
    • MongoDB\Driver\WriteConcern::getWtimeout - Returns the WriteConcern's "wtimeout" option
    • MongoDB\Driver\WriteConcern::__construct - Construct immutable WriteConcern
    • MongoDB\Driver\WriteConcernError::getCode - Returns the WriteConcernError's error code
    • MongoDB\Driver\WriteConcernError::getInfo - Returns additional metadata for the WriteConcernError
    • MongoDB\Driver\WriteConcernError::getMessage - Returns the WriteConcernError's error message
    • MongoDB\Driver\WriteError::getCode - Returns the WriteError's error code
    • MongoDB\Driver\WriteError::getIndex - Returns the index of the write operation corresponding to this WriteError
    • MongoDB\Driver\WriteError::getInfo - Returns additional metadata for the WriteError
    • MongoDB\Driver\WriteError::getMessage - Returns the WriteError's error message
    • MongoDB\Driver\WriteResult::getDeletedCount - Returns the number of documents deleted
    • MongoDB\Driver\WriteResult::getInsertedCount - Returns the number of documents inserted (excluding upserts)
    • MongoDB\Driver\WriteResult::getMatchedCount - Returns the number of documents selected for update
    • MongoDB\Driver\WriteResult::getModifiedCount - Returns the number of existing documents updated
    • MongoDB\Driver\WriteResult::getServer - Returns the server associated with this write result
    • MongoDB\Driver\WriteResult::getUpsertedCount - Returns the number of documents inserted by an upsert
    • MongoDB\Driver\WriteResult::getUpsertedIds - Returns an array of identifiers for upserted documents
    • MongoDB\Driver\WriteResult::getWriteConcernError - Returns any write concern error that occurred
    • MongoDB\Driver\WriteResult::getWriteErrors - Returns any write errors that occurred
    • MongoDB\Driver\WriteResult::isAcknowledged - Returns whether the write was acknowledged
    • MongoDeleteBatch::__construct - Description
    • MongoGridFS::delete - Remove a file and its chunks from the database
    • MongoGridFS::drop - Drops the files and chunks collections
    • MongoGridFS::find - Queries for files
    • MongoGridFS::findOne - Returns a single file matching the criteria
    • MongoGridFS::get - Retrieve a file from the database
    • MongoGridFS::put - Stores a file in the database
    • MongoGridFS::remove - Remove files and their chunks from the database
    • MongoGridFS::storeBytes - Stores a string of bytes in the database
    • MongoGridFS::storeFile - Stores a file in the database
    • MongoGridFS::storeUpload - Stores an uploaded file in the database
    • MongoGridFS::__construct - Creates new file collections
    • MongoGridFSCursor::current - Returns the current file
    • MongoGridFSCursor::getNext - Return the next file to which this cursor points, and advance the cursor
    • MongoGridFSCursor::key - Returns the current result's filename
    • MongoGridFSCursor::__construct - Create a new cursor
    • MongoGridFSFile::getBytes - Returns this file's contents as a string of bytes
    • MongoGridFSFile::getFilename - Returns this file's filename
    • MongoGridFSFile::getResource - Returns a resource that can be used to read the stored file
    • MongoGridFSFile::getSize - Returns this file's size
    • MongoGridFSFile::write - Writes this file to the filesystem
    • MongoGridfsFile::__construct - Create a new GridFS file
    • MongoId::getHostname - Gets the hostname being used for this machine's ids
    • MongoId::getInc - Gets the incremented value to create this id
    • MongoId::getPID - Gets the process ID
    • MongoId::getTimestamp - Gets the number of seconds since the epoch that this id was created
    • MongoId::isValid - Check if a value is a valid ObjectId
    • MongoId::__construct - Creates a new id
    • MongoId::__set_state - Create a dummy MongoId
    • MongoId::__toString - Returns a hexidecimal representation of this id
    • MongoInsertBatch::__construct - Description
    • MongoInt32::__construct - Creates a new 32-bit integer.
    • MongoInt32::__toString - Returns the string representation of this 32-bit integer.
    • MongoInt64::__construct - Creates a new 64-bit integer.
    • MongoInt64::__toString - Returns the string representation of this 64-bit integer.
    • MongoLog::getCallback - Gets the previously set callback function
    • MongoLog::getLevel - Gets the level(s) currently being logged
    • MongoLog::getModule - Gets the module(s) currently being logged
    • MongoLog::setCallback - Sets a callback function to be invoked for events
    • MongoLog::setLevel - Sets the level(s) to be logged
    • MongoLog::setModule - Sets the module(s) to be logged
    • MongoPool::getSize - Get pool size for connection pools
    • MongoPool::info - Returns information about all connection pools.
    • MongoPool::setSize - Set the size for future connection pools.
    • MongoRegex::__construct - Creates a new regular expression
    • MongoRegex::__toString - A string representation of this regular expression
    • MongoResultException::getDocument - Retrieve the full result document
    • MongoTimestamp::__construct - Creates a new timestamp.
    • MongoTimestamp::__toString - Returns a string representation of this timestamp
    • MongoUpdateBatch::__construct - Description
    • MongoWriteBatch::add - Adds a write operation to a batch
    • MongoWriteBatch::execute - Executes a batch of write operations
    • MongoWriteBatch::__construct - Creates a new batch of write operations
    • MongoWriteConcernException::getDocument - Get the error document
    • move_uploaded_file - Moves an uploaded file to a new location
    • mqseries_back - MQSeries MQBACK
    • mqseries_begin - MQseries MQBEGIN
    • mqseries_close - MQSeries MQCLOSE
    • mqseries_cmit - MQSeries MQCMIT
    • mqseries_conn - MQSeries MQCONN
    • mqseries_connx - MQSeries MQCONNX
    • mqseries_disc - MQSeries MQDISC
    • mqseries_get - MQSeries MQGET
    • mqseries_inq - MQSeries MQINQ
    • mqseries_open - MQSeries MQOPEN
    • mqseries_put - MQSeries MQPUT
    • mqseries_put1 - MQSeries MQPUT1
    • mqseries_set - MQSeries MQSET
    • mqseries_strerror - Returns the error message corresponding to a result code (MQRC).
    • msession_connect - Connect to msession server
    • msession_count - Get session count
    • msession_create - Create a session
    • msession_destroy - Destroy a session
    • msession_disconnect - Close connection to msession server
    • msession_find - Find all sessions with name and value
    • msession_get - Get value from session
    • msession_get_array - Get array of msession variables
    • msession_get_data - Get data session unstructured data
    • msession_inc - Increment value in session
    • msession_list - List all sessions
    • msession_listvar - List sessions with variable
    • msession_lock - Lock a session
    • msession_plugin - Call an escape function within the msession personality plugin
    • msession_randstr - Get random string
    • msession_set - Set value in session
    • msession_set_array - Set msession variables from an array
    • msession_set_data - Set data session unstructured data
    • msession_timeout - Set/get session timeout
    • msession_uniq - Get unique id
    • msession_unlock - Unlock a session
    • msg_get_queue - Create or attach to a message queue
    • msg_queue_exists - Check whether a message queue exists
    • msg_receive - Receive a message from a message queue
    • msg_remove_queue - Destroy a message queue
    • msg_send - Send a message to a message queue
    • msg_set_queue - Set information in the message queue data structure
    • msg_stat_queue - Returns information from the message queue data structure
    • msql - Alias of msql_db_query
    • msql_affected_rows - Returns number of affected rows
    • msql_close - Close mSQL connection
    • msql_connect - Open mSQL connection
    • msql_createdb - 별칭: msql_create_db
    • msql_create_db - Create mSQL database
    • msql_data_seek - Move internal row pointer
    • msql_dbname - 별칭: msql_result
    • msql_db_query - Send mSQL query
    • msql_drop_db - Drop (delete) mSQL database
    • msql_error - Returns error message of last msql call
    • msql_fetch_array - Fetch row as array
    • msql_fetch_field - Get field information
    • msql_fetch_object - Fetch row as object
    • msql_fetch_row - Get row as enumerated array
    • msql_fieldflags - Alias of msql_field_flags
    • msql_fieldlen - Alias of msql_field_len
    • msql_fieldname - Alias of msql_field_name
    • msql_fieldtable - Alias of msql_field_table
    • msql_fieldtype - Alias of msql_field_type
    • msql_field_flags - Get field flags
    • msql_field_len - Get field length
    • msql_field_name - Get the name of the specified field in a result
    • msql_field_seek - Set field offset
    • msql_field_table - Get table name for field
    • msql_field_type - Get field type
    • msql_free_result - Free result memory
    • msql_list_dbs - List mSQL databases on server
    • msql_list_fields - List result fields
    • msql_list_tables - List tables in an mSQL database
    • msql_numfields - Alias of msql_num_fields
    • msql_numrows - Alias of msql_num_rows
    • msql_num_fields - Get number of fields in result
    • msql_num_rows - Get number of rows in result
    • msql_pconnect - Open persistent mSQL connection
    • msql_query - Send mSQL query
    • msql_regcase - Alias of sql_regcase
    • msql_result - Get result data
    • msql_select_db - Select mSQL database
    • msql_tablename - Alias of msql_result
    • mssql_bind - Adds a parameter to a stored procedure or a remote stored procedure
    • mssql_close - Close MS SQL Server connection
    • mssql_connect - Open MS SQL server connection
    • mssql_data_seek - Moves internal row pointer
    • mssql_execute - Executes a stored procedure on a MS SQL server database
    • mssql_fetch_array - Fetch a result row as an associative array, a numeric array, or both
    • mssql_fetch_assoc - Returns an associative array of the current row in the result
    • mssql_fetch_batch - Returns the next batch of records
    • mssql_fetch_field - Get field information
    • mssql_fetch_object - Fetch row as object
    • mssql_fetch_row - Get row as enumerated array
    • mssql_field_length - Get the length of a field
    • mssql_field_name - Get the name of a field
    • mssql_field_seek - Seeks to the specified field offset
    • mssql_field_type - Gets the type of a field
    • mssql_free_result - Free result memory
    • mssql_free_statement - Free statement memory
    • mssql_get_last_message - Returns the last message from the server
    • mssql_guid_string - Converts a 16 byte binary GUID to a string
    • mssql_init - Initializes a stored procedure or a remote stored procedure
    • mssql_min_error_severity - Sets the minimum error severity
    • mssql_min_message_severity - Sets the minimum message severity
    • mssql_next_result - Move the internal result pointer to the next result
    • mssql_num_fields - Gets the number of fields in result
    • mssql_num_rows - Gets the number of rows in result
    • mssql_pconnect - Open persistent MS SQL connection
    • mssql_query - Send MS SQL query
    • mssql_result - Get result data
    • mssql_rows_affected - Returns the number of records affected by the query
    • mssql_select_db - Select MS SQL database
    • mt_getrandmax - 가능한 최대 난수 값을 표시
    • mt_rand - 향상된 난수를 생성
    • mt_srand - 향상된 난수 생성기를 초기화
    • MultipleIterator::attachIterator - Attaches iterator information
    • MultipleIterator::containsIterator - Checks if an iterator is attached
    • MultipleIterator::countIterators - Gets the number of attached iterator instances
    • MultipleIterator::current - Gets the registered iterator instances
    • MultipleIterator::detachIterator - Detaches an iterator
    • MultipleIterator::getFlags - Gets the flag information
    • MultipleIterator::key - Gets the registered iterator instances
    • MultipleIterator::next - Moves all attached iterator instances forward
    • MultipleIterator::rewind - Rewinds all attached iterator instances
    • MultipleIterator::setFlags - Sets flags
    • MultipleIterator::valid - Checks the validity of sub iterators
    • MultipleIterator::__construct - Constructs a new MultipleIterator
    • Mutex::create - Create a Mutex
    • Mutex::destroy - Destroy Mutex
    • Mutex::lock - Acquire Mutex
    • Mutex::trylock - Attempt to Acquire Mutex
    • Mutex::unlock - Release Mutex
    • mysqli::$affected_rows - Gets the number of affected rows in a previous MySQL operation
    • mysqli::$client_info - Get MySQL client info
    • mysqli::$connect_errno - Returns the error code from last connect call
    • mysqli::$connect_error - Returns a string description of the last connect error
    • mysqli::$errno - Returns the error code for the most recent function call
    • mysqli::$error - Returns a string description of the last error
    • mysqli::$error_list - Returns a list of errors from the last command executed
    • mysqli::$field_count - Returns the number of columns for the most recent query
    • mysqli::$host_info - Returns a string representing the type of connection used
    • mysqli::$info - Retrieves information about the most recently executed query
    • mysqli::$insert_id - Returns the auto generated id used in the last query
    • mysqli::$protocol_version - Returns the version of the MySQL protocol used
    • mysqli::$server_info - Returns the version of the MySQL server
    • mysqli::$server_version - Returns the version of the MySQL server as an integer
    • mysqli::$sqlstate - Returns the SQLSTATE error from previous MySQL operation
    • mysqli::$thread_id - Returns the thread ID for the current connection
    • mysqli::$warning_count - Returns the number of warnings from the last query for the given link
    • mysqli::autocommit - Turns on or off auto-committing database modifications
    • mysqli::begin_transaction - Starts a transaction
    • mysqli::change_user - Changes the user of the specified database connection
    • mysqli::character_set_name - Returns the default character set for the database connection
    • mysqli::close - Closes a previously opened database connection
    • mysqli::commit - Commits the current transaction
    • mysqli::debug - Performs debugging operations
    • mysqli::disable_reads_from_master - Disable reads from master
    • mysqli::dump_debug_info - Dump debugging information into the log
    • mysqli::get_charset - Returns a character set object
    • mysqli::get_client_info - Get MySQL client info
    • mysqli::get_connection_stats - Returns statistics about the client connection
    • mysqli::get_warnings - Get result of SHOW WARNINGS
    • mysqli::init - Initializes MySQLi and returns a resource for use with mysqli_real_connect()
    • mysqli::kill - Asks the server to kill a MySQL thread
    • mysqli::more_results - Check if there are any more query results from a multi query
    • mysqli::multi_query - Performs a query on the database
    • mysqli::next_result - Prepare next result from multi_query
    • mysqli::options - Set options
    • mysqli::ping - Pings a server connection, or tries to reconnect if the connection has gone down
    • mysqli::poll - Poll connections
    • mysqli::prepare - Prepare an SQL statement for execution
    • mysqli::query - Performs a query on the database
    • mysqli::real_connect - Opens a connection to a mysql server
    • mysqli::real_escape_string - Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection
    • mysqli::real_query - Execute an SQL query
    • mysqli::reap_async_query - Get result from async query
    • mysqli::refresh - Refreshes
    • mysqli::release_savepoint - Removes the named savepoint from the set of savepoints of the current transaction
    • mysqli::rollback - Rolls back current transaction
    • mysqli::rpl_query_type - Returns RPL query type
    • mysqli::savepoint - Set a named transaction savepoint
    • mysqli::select_db - Selects the default database for database queries
    • mysqli::send_query - Send the query and return
    • mysqli::set_charset - Sets the default client character set
    • mysqli::set_local_infile_default - Unsets user defined handler for load local infile command
    • mysqli::set_local_infile_handler - Set callback function for LOAD DATA LOCAL INFILE command
    • mysqli::set_opt - Alias of mysqli_options
    • mysqli::ssl_set - Used for establishing secure connections using SSL
    • mysqli::stat - Gets the current system status
    • mysqli::stmt_init - Initializes a statement and returns an object for use with mysqli_stmt_prepare
    • mysqli::store_result - Transfers a result set from the last query
    • mysqli::thread_safe - Returns whether thread safety is given or not
    • mysqli::use_result - Initiate a result set retrieval
    • mysqli::__construct - Open a new connection to the MySQL server
    • mysqli_bind_param - Alias for mysqli_stmt_bind_param
    • mysqli_bind_result - Alias for mysqli_stmt_bind_result
    • mysqli_client_encoding - Alias of mysqli_character_set_name
    • mysqli_connect - 별칭: mysqli::__construct
    • mysqli_disable_rpl_parse - Disable RPL parse
    • mysqli_driver::$report_mode - Enables or disables internal report functions
    • mysqli_driver::embedded_server_end - Stop embedded server
    • mysqli_driver::embedded_server_start - Initialize and start embedded server
    • mysqli_enable_reads_from_master - Enable reads from master
    • mysqli_enable_rpl_parse - Enable RPL parse
    • mysqli_escape_string - 별칭: mysqli_real_escape_string
    • mysqli_execute - Alias for mysqli_stmt_execute
    • mysqli_fetch - Alias for mysqli_stmt_fetch
    • mysqli_get_cache_stats - Returns client Zval cache statistics
    • mysqli_get_client_stats - Returns client per-process statistics
    • mysqli_get_client_version - Returns the MySQL client version as an integer
    • mysqli_get_links_stats - Return information about open and cached links
    • mysqli_get_metadata - Alias for mysqli_stmt_result_metadata
    • mysqli_master_query - Enforce execution of a query on the master in a master/slave setup
    • mysqli_param_count - Alias for mysqli_stmt_param_count
    • mysqli_report - 별칭: mysqli_driver->report_mode
    • mysqli_result::$current_field - Get current field offset of a result pointer
    • mysqli_result::$field_count - Get the number of fields in a result
    • mysqli_result::$lengths - Returns the lengths of the columns of the current row in the result set
    • mysqli_result::$num_rows - Gets the number of rows in a result
    • mysqli_result::data_seek - Adjusts the result pointer to an arbitrary row in the result
    • mysqli_result::fetch_all - Fetches all result rows as an associative array, a numeric array, or both
    • mysqli_result::fetch_array - Fetch a result row as an associative, a numeric array, or both
    • mysqli_result::fetch_assoc - Fetch a result row as an associative array
    • mysqli_result::fetch_field - Returns the next field in the result set
    • mysqli_result::fetch_fields - Returns an array of objects representing the fields in a result set
    • mysqli_result::fetch_field_direct - Fetch meta-data for a single field
    • mysqli_result::fetch_object - Returns the current row of a result set as an object
    • mysqli_result::fetch_row - Get a result row as an enumerated array
    • mysqli_result::field_seek - Set result pointer to a specified field offset
    • mysqli_result::free - Frees the memory associated with a result
    • mysqli_rpl_parse_enabled - Check if RPL parse is enabled
    • mysqli_rpl_probe - RPL probe
    • mysqli_send_long_data - Alias for mysqli_stmt_send_long_data
    • mysqli_slave_query - Force execution of a query on a slave in a master/slave setup
    • mysqli_stmt::$affected_rows - Returns the total number of rows changed, deleted, or inserted by the last executed statement
    • mysqli_stmt::$errno - Returns the error code for the most recent statement call
    • mysqli_stmt::$error - Returns a string description for last statement error
    • mysqli_stmt::$error_list - Returns a list of errors from the last statement executed
    • mysqli_stmt::$field_count - Returns the number of field in the given statement
    • mysqli_stmt::$insert_id - Get the ID generated from the previous INSERT operation
    • mysqli_stmt::$num_rows - Return the number of rows in statements result set
    • mysqli_stmt::$param_count - Returns the number of parameter for the given statement
    • mysqli_stmt::$sqlstate - Returns SQLSTATE error from previous statement operation
    • mysqli_stmt::attr_get - Used to get the current value of a statement attribute
    • mysqli_stmt::attr_set - Used to modify the behavior of a prepared statement
    • mysqli_stmt::bind_param - Binds variables to a prepared statement as parameters
    • mysqli_stmt::bind_result - Binds variables to a prepared statement for result storage
    • mysqli_stmt::close - Closes a prepared statement
    • mysqli_stmt::data_seek - Seeks to an arbitrary row in statement result set
    • mysqli_stmt::execute - Executes a prepared Query
    • mysqli_stmt::fetch - Fetch results from a prepared statement into the bound variables
    • mysqli_stmt::free_result - Frees stored result memory for the given statement handle
    • mysqli_stmt::get_result - Gets a result set from a prepared statement
    • mysqli_stmt::get_warnings - Get result of SHOW WARNINGS
    • mysqli_stmt::more_results - Check if there are more query results from a multiple query
    • mysqli_stmt::next_result - Reads the next result from a multiple query
    • mysqli_stmt::prepare - Prepare an SQL statement for execution
    • mysqli_stmt::reset - Resets a prepared statement
    • mysqli_stmt::result_metadata - Returns result set metadata from a prepared statement
    • mysqli_stmt::send_long_data - Send data in blocks
    • mysqli_stmt::store_result - Transfers a result set from a prepared statement
    • mysqli_stmt::__construct - Constructs a new mysqli_stmt object
    • mysqli_warning::next - The next purpose
    • mysqli_warning::__construct - The __construct purpose
    • MysqlndUhConnection::changeUser - Changes the user of the specified mysqlnd database connection
    • MysqlndUhConnection::charsetName - Returns the default character set for the database connection
    • MysqlndUhConnection::close - Closes a previously opened database connection
    • MysqlndUhConnection::connect - Open a new connection to the MySQL server
    • MysqlndUhConnection::endPSession - End a persistent connection
    • MysqlndUhConnection::escapeString - Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection
    • MysqlndUhConnection::getAffectedRows - Gets the number of affected rows in a previous MySQL operation
    • MysqlndUhConnection::getErrorNumber - Returns the error code for the most recent function call
    • MysqlndUhConnection::getErrorString - Returns a string description of the last error
    • MysqlndUhConnection::getFieldCount - Returns the number of columns for the most recent query
    • MysqlndUhConnection::getHostInformation - Returns a string representing the type of connection used
    • MysqlndUhConnection::getLastInsertId - Returns the auto generated id used in the last query.
    • MysqlndUhConnection::getLastMessage - Retrieves information about the most recently executed query
    • MysqlndUhConnection::getProtocolInformation - Returns the version of the MySQL protocol used
    • MysqlndUhConnection::getServerInformation - Returns the version of the MySQL server
    • MysqlndUhConnection::getServerStatistics - Gets the current system status
    • MysqlndUhConnection::getServerVersion - Returns the version of the MySQL server as an integer
    • MysqlndUhConnection::getSqlstate - Returns the SQLSTATE error from previous MySQL operation
    • MysqlndUhConnection::getStatistics - Returns statistics about the client connection.
    • MysqlndUhConnection::getThreadId - Returns the thread ID for the current connection
    • MysqlndUhConnection::getWarningCount - Returns the number of warnings from the last query for the given link
    • MysqlndUhConnection::init - Initialize mysqlnd connection
    • MysqlndUhConnection::killConnection - Asks the server to kill a MySQL thread
    • MysqlndUhConnection::listFields - List MySQL table fields
    • MysqlndUhConnection::listMethod - Wrapper for assorted list commands
    • MysqlndUhConnection::moreResults - Check if there are any more query results from a multi query
    • MysqlndUhConnection::nextResult - Prepare next result from multi_query
    • MysqlndUhConnection::ping - Pings a server connection, or tries to reconnect if the connection has gone down
    • MysqlndUhConnection::query - Performs a query on the database
    • MysqlndUhConnection::queryReadResultsetHeader - Read a result set header
    • MysqlndUhConnection::reapQuery - Get result from async query
    • MysqlndUhConnection::refreshServer - Flush or reset tables and caches
    • MysqlndUhConnection::restartPSession - Restart a persistent mysqlnd connection
    • MysqlndUhConnection::selectDb - Selects the default database for database queries
    • MysqlndUhConnection::sendClose - Sends a close command to MySQL
    • MysqlndUhConnection::sendQuery - Sends a query to MySQL
    • MysqlndUhConnection::serverDumpDebugInformation - Dump debugging information into the log for the MySQL server
    • MysqlndUhConnection::setAutocommit - Turns on or off auto-committing database modifications
    • MysqlndUhConnection::setCharset - Sets the default client character set
    • MysqlndUhConnection::setClientOption - Sets a client option
    • MysqlndUhConnection::setServerOption - Sets a server option
    • MysqlndUhConnection::shutdownServer - The shutdownServer purpose
    • MysqlndUhConnection::simpleCommand - Sends a basic COM_* command
    • MysqlndUhConnection::simpleCommandHandleResponse - Process a response for a basic COM_* command send to the client
    • MysqlndUhConnection::sslSet - Used for establishing secure connections using SSL
    • MysqlndUhConnection::stmtInit - Initializes a statement and returns a resource for use with mysqli_statement::prepare
    • MysqlndUhConnection::storeResult - Transfers a result set from the last query
    • MysqlndUhConnection::txCommit - Commits the current transaction
    • MysqlndUhConnection::txRollback - Rolls back current transaction
    • MysqlndUhConnection::useResult - Initiate a result set retrieval
    • MysqlndUhConnection::__construct - The __construct purpose
    • MysqlndUhPreparedStatement::execute - Executes a prepared Query
    • MysqlndUhPreparedStatement::prepare - Prepare an SQL statement for execution
    • MysqlndUhPreparedStatement::__construct - The __construct purpose
    • mysqlnd_memcache_get_config - Returns information about the plugin configuration
    • mysqlnd_memcache_set - Associate a MySQL connection with a Memcache connection
    • mysqlnd_ms_dump_servers - Returns a list of currently configured servers
    • mysqlnd_ms_fabric_select_global - Switch to global sharding server for a given table
    • mysqlnd_ms_fabric_select_shard - Switch to shard
    • mysqlnd_ms_get_last_gtid - Returns the latest global transaction ID
    • mysqlnd_ms_get_last_used_connection - Returns an array which describes the last used connection
    • mysqlnd_ms_get_stats - Returns query distribution and connection statistics
    • mysqlnd_ms_match_wild - Finds whether a table name matches a wildcard pattern or not
    • mysqlnd_ms_query_is_select - Find whether to send the query to the master, the slave or the last used MySQL server
    • mysqlnd_ms_set_qos - Sets the quality of service needed from the cluster
    • mysqlnd_ms_set_user_pick_server - Sets a callback for user-defined read/write splitting
    • mysqlnd_ms_xa_begin - Starts a distributed/XA transaction among MySQL servers
    • mysqlnd_ms_xa_commit - Commits a distributed/XA transaction among MySQL servers
    • mysqlnd_ms_xa_gc - Garbage collects unfinished XA transactions after severe errors
    • mysqlnd_ms_xa_rollback - Rolls back a distributed/XA transaction among MySQL servers
    • mysqlnd_qc_clear_cache - Flush all cache contents
    • mysqlnd_qc_get_available_handlers - Returns a list of available storage handler
    • mysqlnd_qc_get_cache_info - Returns information on the current handler, the number of cache entries and cache entries, if available
    • mysqlnd_qc_get_core_stats - Statistics collected by the core of the query cache
    • mysqlnd_qc_get_normalized_query_trace_log - Returns a normalized query trace log for each query inspected by the query cache
    • mysqlnd_qc_get_query_trace_log - Returns a backtrace for each query inspected by the query cache
    • mysqlnd_qc_set_cache_condition - Set conditions for automatic caching
    • mysqlnd_qc_set_is_select - Installs a callback which decides whether a statement is cached
    • mysqlnd_qc_set_storage_handler - Change current storage handler
    • mysqlnd_qc_set_user_handlers - Sets the callback functions for a user-defined procedural storage handler
    • mysqlnd_uh_convert_to_mysqlnd - Converts a MySQL connection handle into a mysqlnd connection handle
    • mysqlnd_uh_set_connection_proxy - Installs a proxy for mysqlnd connections
    • mysqlnd_uh_set_statement_proxy - Installs a proxy for mysqlnd statements
    • mysql_affected_rows - 최근 MySQL 작업으로 변경된 행 개수를 얻음
    • mysql_client_encoding - 문자셋을 반환
    • mysql_close - MySQL 접속을 닫음
    • mysql_connect - MySQL 서버에 접속
    • mysql_create_db - MySQL 데이터베이스를 생성
    • mysql_data_seek - 내부 결과 포인터를 이동
    • mysql_db_name - 결과 데이터를 반환
    • mysql_db_query - MySQL 질의를 전송
    • mysql_drop_db - MySQL 데이터베이스를 삭제
    • mysql_errno - 최근 MySQL 작업으로 발생한 에러 번호를 반환
    • mysql_error - 최근 실행된 MySQL 작업으로 발생한 에러 메시지를 반환
    • mysql_escape_string - mysql_query에서 문자열을 escape하기위해 사용
    • mysql_fetch_array - 연관 색인 및 숫자 색인으로 된 배열로 결과 행을 반환
    • mysql_fetch_assoc - 연관 배열로 결과 행을 반환
    • mysql_fetch_field - 결과로 부터 컬럼 정보를 얻어서 객체형태로 반환
    • mysql_fetch_lengths - 결과로부터 각 출력의 길이를 반환
    • mysql_fetch_object - 객체형으로 결과 행을 인출
    • mysql_fetch_row - 숫자 색인 배열로 결과를 반환
    • mysql_field_flags - 결과로부터 특정 필드와 관련된 플래그를 반환
    • mysql_field_len - 지정한 필드의 길이를 반환
    • mysql_field_name - 결과로부터 특정 필드의 이름을 반환
    • mysql_field_seek - 특정 필드의 오프셋으로 결과 포인터를 이동
    • mysql_field_table - 특정 필드가 속한 테이블명을 얻음
    • mysql_field_type - 결과로부터 특정 필드의 데이터 형(type) 정보를 반환
    • mysql_free_result - 질의결과를 메모리에서 해제
    • mysql_get_client_info - MySQL 클라이언트 정보를 반환
    • mysql_get_host_info - MySQL 호스트 정보를 반환
    • mysql_get_proto_info - MySQL 프로토콜 정보를 얻는다
    • mysql_get_server_info - MySQL 서버 정보를 반환
    • mysql_info - 가장 최근 질의에 대한 정보를 반환
    • mysql_insert_id - 이전의 INSERT 작업으로부터 생성된 ID를 반환
    • mysql_list_dbs - MySQL 서버에 있는 데이터베이스 목록을 반환
    • mysql_list_fields - MySQL 테이블 필드를 반환
    • mysql_list_processes - MySQL 프로세스 목록을 반환
    • mysql_list_tables - MySQL 데이터베이스에서 테이블 목록을 반환
    • mysql_num_fields - 결과로부터 필드의 개수를 반환
    • mysql_num_rows - 결과로부터 행의 개수를 반환
    • mysql_pconnect - MySQL 서버에 지속 접속 열기
    • mysql_ping - 서버 접속을 검사하고 접속이 없으면 재접속한다
    • mysql_query - MySQL 질의를 전송
    • mysql_real_escape_string - mysql_query에서 특수 문자열을 이스케이프하기위해 사용
    • mysql_result - 결과 데이터를 반환
    • mysql_select_db - MySQL 데이터베이스를 선택
    • mysql_set_charset - Sets the client character set
    • mysql_stat - 현재 시스템 상태를 반환
    • mysql_tablename - 필드가 존재하는 테이블명을 반환
    • mysql_thread_id - 현재 쓰레드 ID를 반환
    • mysql_unbuffered_query - 결과 행을 버퍼링하지도 인출하지도 않으면서 MySQL로 SQL 질의를 전송
    • m_checkstatus - Check to see if a transaction has completed
    • m_completeauthorizations - Number of complete authorizations in queue, returning an array of their identifiers
    • m_connect - Establish the connection to MCVE
    • m_connectionerror - Get a textual representation of why a connection failed
    • m_deletetrans - Delete specified transaction from MCVE_CONN structure
    • m_destroyconn - Destroy the connection and MCVE_CONN structure
    • m_destroyengine - Free memory associated with IP/SSL connectivity
    • m_getcell - Get a specific cell from a comma delimited response by column name
    • m_getcellbynum - Get a specific cell from a comma delimited response by column number
    • m_getcommadelimited - Get the RAW comma delimited data returned from MCVE
    • m_getheader - Get the name of the column in a comma-delimited response
    • m_initconn - Create and initialize an MCVE_CONN structure
    • m_initengine - Ready the client for IP/SSL Communication
    • m_iscommadelimited - Checks to see if response is comma delimited
    • m_maxconntimeout - The maximum amount of time the API will attempt a connection to MCVE
    • m_monitor - Perform communication with MCVE (send/receive data) Non-blocking
    • m_numcolumns - Number of columns returned in a comma delimited response
    • m_numrows - Number of rows returned in a comma delimited response
    • m_parsecommadelimited - Parse the comma delimited response so m_getcell, etc will work
    • m_responsekeys - Returns array of strings which represents the keys that can be used for response parameters on this transaction
    • m_responseparam - Get a custom response parameter
    • m_returnstatus - Check to see if the transaction was successful
    • m_setblocking - Set blocking/non-blocking mode for connection
    • m_setdropfile - Set the connection method to Drop-File
    • m_setip - Set the connection method to IP
    • m_setssl - Set the connection method to SSL
    • m_setssl_cafile - Set SSL CA (Certificate Authority) file for verification of server certificate
    • m_setssl_files - Set certificate key files and certificates if server requires client certificate verification
    • m_settimeout - Set maximum transaction time (per trans)
    • m_sslcert_gen_hash - Generate hash for SSL client certificate verification
    • m_transactionssent - Check to see if outgoing buffer is clear
    • m_transinqueue - Number of transactions in client-queue
    • m_transkeyval - Add key/value pair to a transaction. Replaces deprecated transparam()
    • m_transnew - Start a new transaction
    • m_transsend - Finalize and send the transaction
    • m_uwait - Wait x microsecs
    • m_validateidentifier - Whether or not to validate the passed identifier on any transaction it is passed to
    • m_verifyconnection - Set whether or not to PING upon connect to verify connection
    • m_verifysslcert - Set whether or not to verify the server ssl certificate
  • n
    • natcasesort - "자연순" 알고리즘으로 대소문자를 구분하지 않고 배열 정렬
    • natsort - "자연순" 알고리즘으로 배열 정렬
    • ncurses_addch - Add character at current position and advance cursor
    • ncurses_addchnstr - Add attributed string with specified length at current position
    • ncurses_addchstr - Add attributed string at current position
    • ncurses_addnstr - Add string with specified length at current position
    • ncurses_addstr - Output text at current position
    • ncurses_assume_default_colors - Define default colors for color 0
    • ncurses_attroff - Turn off the given attributes
    • ncurses_attron - Turn on the given attributes
    • ncurses_attrset - Set given attributes
    • ncurses_baudrate - Returns baudrate of terminal
    • ncurses_beep - Let the terminal beep
    • ncurses_bkgd - Set background property for terminal screen
    • ncurses_bkgdset - Control screen background
    • ncurses_border - Draw a border around the screen using attributed characters
    • ncurses_bottom_panel - Moves a visible panel to the bottom of the stack
    • ncurses_can_change_color - Checks if terminal color definitions can be changed
    • ncurses_cbreak - Switch off input buffering
    • ncurses_clear - Clear screen
    • ncurses_clrtobot - Clear screen from current position to bottom
    • ncurses_clrtoeol - Clear screen from current position to end of line
    • ncurses_color_content - Retrieves RGB components of a color
    • ncurses_color_set - Set active foreground and background colors
    • ncurses_curs_set - Set cursor state
    • ncurses_define_key - Define a keycode
    • ncurses_def_prog_mode - Saves terminals (program) mode
    • ncurses_def_shell_mode - Saves terminals (shell) mode
    • ncurses_delay_output - Delay output on terminal using padding characters
    • ncurses_delch - Delete character at current position, move rest of line left
    • ncurses_deleteln - Delete line at current position, move rest of screen up
    • ncurses_delwin - Delete a ncurses window
    • ncurses_del_panel - Remove panel from the stack and delete it (but not the associated window)
    • ncurses_doupdate - Write all prepared refreshes to terminal
    • ncurses_echo - Activate keyboard input echo
    • ncurses_echochar - Single character output including refresh
    • ncurses_end - Stop using ncurses, clean up the screen
    • ncurses_erase - Erase terminal screen
    • ncurses_erasechar - Returns current erase character
    • ncurses_filter - Set LINES for iniscr() and newterm() to 1
    • ncurses_flash - Flash terminal screen (visual bell)
    • ncurses_flushinp - Flush keyboard input buffer
    • ncurses_getch - Read a character from keyboard
    • ncurses_getmaxyx - Returns the size of a window
    • ncurses_getmouse - Reads mouse event
    • ncurses_getyx - Returns the current cursor position for a window
    • ncurses_halfdelay - Put terminal into halfdelay mode
    • ncurses_has_colors - Checks if terminal has color capabilities
    • ncurses_has_ic - Check for insert- and delete-capabilities
    • ncurses_has_il - Check for line insert- and delete-capabilities
    • ncurses_has_key - Check for presence of a function key on terminal keyboard
    • ncurses_hide_panel - Remove panel from the stack, making it invisible
    • ncurses_hline - Draw a horizontal line at current position using an attributed character and max. n characters long
    • ncurses_inch - Get character and attribute at current position
    • ncurses_init - Initialize ncurses
    • ncurses_init_color - Define a terminal color
    • ncurses_init_pair - Define a color pair
    • ncurses_insch - Insert character moving rest of line including character at current position
    • ncurses_insdelln - Insert lines before current line scrolling down (negative numbers delete and scroll up)
    • ncurses_insertln - Insert a line, move rest of screen down
    • ncurses_insstr - Insert string at current position, moving rest of line right
    • ncurses_instr - Reads string from terminal screen
    • ncurses_isendwin - Ncurses is in endwin mode, normal screen output may be performed
    • ncurses_keyok - Enable or disable a keycode
    • ncurses_keypad - Turns keypad on or off
    • ncurses_killchar - Returns current line kill character
    • ncurses_longname - Returns terminals description
    • ncurses_meta - Enables/Disable 8-bit meta key information
    • ncurses_mouseinterval - Set timeout for mouse button clicks
    • ncurses_mousemask - Sets mouse options
    • ncurses_mouse_trafo - Transforms coordinates
    • ncurses_move - Move output position
    • ncurses_move_panel - Moves a panel so that its upper-left corner is at [startx, starty]
    • ncurses_mvaddch - Move current position and add character
    • ncurses_mvaddchnstr - Move position and add attributed string with specified length
    • ncurses_mvaddchstr - Move position and add attributed string
    • ncurses_mvaddnstr - Move position and add string with specified length
    • ncurses_mvaddstr - Move position and add string
    • ncurses_mvcur - Move cursor immediately
    • ncurses_mvdelch - Move position and delete character, shift rest of line left
    • ncurses_mvgetch - Move position and get character at new position
    • ncurses_mvhline - Set new position and draw a horizontal line using an attributed character and max. n characters long
    • ncurses_mvinch - Move position and get attributed character at new position
    • ncurses_mvvline - Set new position and draw a vertical line using an attributed character and max. n characters long
    • ncurses_mvwaddstr - Add string at new position in window
    • ncurses_napms - Sleep
    • ncurses_newpad - Creates a new pad (window)
    • ncurses_newwin - Create a new window
    • ncurses_new_panel - Create a new panel and associate it with window
    • ncurses_nl - Translate newline and carriage return / line feed
    • ncurses_nocbreak - Switch terminal to cooked mode
    • ncurses_noecho - Switch off keyboard input echo
    • ncurses_nonl - Do not translate newline and carriage return / line feed
    • ncurses_noqiflush - Do not flush on signal characters
    • ncurses_noraw - Switch terminal out of raw mode
    • ncurses_pair_content - Retrieves foreground and background colors of a color pair
    • ncurses_panel_above - Returns the panel above panel
    • ncurses_panel_below - Returns the panel below panel
    • ncurses_panel_window - Returns the window associated with panel
    • ncurses_pnoutrefresh - Copies a region from a pad into the virtual screen
    • ncurses_prefresh - Copies a region from a pad into the virtual screen
    • ncurses_putp - Apply padding information to the string and output it
    • ncurses_qiflush - Flush on signal characters
    • ncurses_raw - Switch terminal into raw mode
    • ncurses_refresh - Refresh screen
    • ncurses_replace_panel - Replaces the window associated with panel
    • ncurses_resetty - Restores saved terminal state
    • ncurses_reset_prog_mode - Resets the prog mode saved by def_prog_mode
    • ncurses_reset_shell_mode - Resets the shell mode saved by def_shell_mode
    • ncurses_savetty - Saves terminal state
    • ncurses_scrl - Scroll window content up or down without changing current position
    • ncurses_scr_dump - Dump screen content to file
    • ncurses_scr_init - Initialize screen from file dump
    • ncurses_scr_restore - Restore screen from file dump
    • ncurses_scr_set - Inherit screen from file dump
    • ncurses_show_panel - Places an invisible panel on top of the stack, making it visible
    • ncurses_slk_attr - Returns current soft label key attribute
    • ncurses_slk_attroff - Turn off the given attributes for soft function-key labels
    • ncurses_slk_attron - Turn on the given attributes for soft function-key labels
    • ncurses_slk_attrset - Set given attributes for soft function-key labels
    • ncurses_slk_clear - Clears soft labels from screen
    • ncurses_slk_color - Sets color for soft label keys
    • ncurses_slk_init - Initializes soft label key functions
    • ncurses_slk_noutrefresh - Copies soft label keys to virtual screen
    • ncurses_slk_refresh - Copies soft label keys to screen
    • ncurses_slk_restore - Restores soft label keys
    • ncurses_slk_set - Sets function key labels
    • ncurses_slk_touch - Forces output when ncurses_slk_noutrefresh is performed
    • ncurses_standend - Stop using 'standout' attribute
    • ncurses_standout - Start using 'standout' attribute
    • ncurses_start_color - Initializes color functionality
    • ncurses_termattrs - Returns a logical OR of all attribute flags supported by terminal
    • ncurses_termname - Returns terminals (short)-name
    • ncurses_timeout - Set timeout for special key sequences
    • ncurses_top_panel - Moves a visible panel to the top of the stack
    • ncurses_typeahead - Specify different filedescriptor for typeahead checking
    • ncurses_ungetch - Put a character back into the input stream
    • ncurses_ungetmouse - Pushes mouse event to queue
    • ncurses_update_panels - Refreshes the virtual screen to reflect the relations between panels in the stack
    • ncurses_use_default_colors - Assign terminal default colors to color id -1
    • ncurses_use_env - Control use of environment information about terminal size
    • ncurses_use_extended_names - Control use of extended names in terminfo descriptions
    • ncurses_vidattr - Display the string on the terminal in the video attribute mode
    • ncurses_vline - Draw a vertical line at current position using an attributed character and max. n characters long
    • ncurses_waddch - Adds character at current position in a window and advance cursor
    • ncurses_waddstr - Outputs text at current postion in window
    • ncurses_wattroff - Turns off attributes for a window
    • ncurses_wattron - Turns on attributes for a window
    • ncurses_wattrset - Set the attributes for a window
    • ncurses_wborder - Draws a border around the window using attributed characters
    • ncurses_wclear - Clears window
    • ncurses_wcolor_set - Sets windows color pairings
    • ncurses_werase - Erase window contents
    • ncurses_wgetch - Reads a character from keyboard (window)
    • ncurses_whline - Draws a horizontal line in a window at current position using an attributed character and max. n characters long
    • ncurses_wmouse_trafo - Transforms window/stdscr coordinates
    • ncurses_wmove - Moves windows output position
    • ncurses_wnoutrefresh - Copies window to virtual screen
    • ncurses_wrefresh - Refresh window on terminal screen
    • ncurses_wstandend - End standout mode for a window
    • ncurses_wstandout - Enter standout mode for a window
    • ncurses_wvline - Draws a vertical line in a window at current position using an attributed character and max. n characters long
    • newt_bell - Send a beep to the terminal
    • newt_button - Create a new button
    • newt_button_bar - This function returns a grid containing the buttons created.
    • newt_centered_window - Open a centered window of the specified size
    • newt_checkbox - 설명
    • newt_checkbox_get_value - Retreives value of checkox resource
    • newt_checkbox_set_flags - Configures checkbox resource
    • newt_checkbox_set_value - Sets the value of the checkbox
    • newt_checkbox_tree - 설명
    • newt_checkbox_tree_add_item - Adds new item to the checkbox tree
    • newt_checkbox_tree_find_item - Finds an item in the checkbox tree
    • newt_checkbox_tree_get_current - Returns checkbox tree selected item
    • newt_checkbox_tree_get_entry_value - 설명
    • newt_checkbox_tree_get_multi_selection - 설명
    • newt_checkbox_tree_get_selection - 설명
    • newt_checkbox_tree_multi - 설명
    • newt_checkbox_tree_set_current - 설명
    • newt_checkbox_tree_set_entry - 설명
    • newt_checkbox_tree_set_entry_value - 설명
    • newt_checkbox_tree_set_width - 설명
    • newt_clear_key_buffer - Discards the contents of the terminal's input buffer without waiting for additional input
    • newt_cls - 설명
    • newt_compact_button - 설명
    • newt_component_add_callback - 설명
    • newt_component_takes_focus - 설명
    • newt_create_grid - 설명
    • newt_cursor_off - 설명
    • newt_cursor_on - 설명
    • newt_delay - 설명
    • newt_draw_form - 설명
    • newt_draw_root_text - Displays the string text at the position indicated
    • newt_entry - 설명
    • newt_entry_get_value - 설명
    • newt_entry_set - 설명
    • newt_entry_set_filter - 설명
    • newt_entry_set_flags - 설명
    • newt_finished - Uninitializes newt interface
    • newt_form - Create a form
    • newt_form_add_component - Adds a single component to the form
    • newt_form_add_components - Add several components to the form
    • newt_form_add_hot_key - 설명
    • newt_form_destroy - Destroys a form
    • newt_form_get_current - 설명
    • newt_form_run - Runs a form
    • newt_form_set_background - 설명
    • newt_form_set_height - 설명
    • newt_form_set_size - 설명
    • newt_form_set_timer - 설명
    • newt_form_set_width - 설명
    • newt_form_watch_fd - 설명
    • newt_get_screen_size - Fills in the passed references with the current size of the terminal
    • newt_grid_add_components_to_form - 설명
    • newt_grid_basic_window - 설명
    • newt_grid_free - 설명
    • newt_grid_get_size - 설명
    • newt_grid_h_close_stacked - 설명
    • newt_grid_h_stacked - 설명
    • newt_grid_place - 설명
    • newt_grid_set_field - 설명
    • newt_grid_simple_window - 설명
    • newt_grid_v_close_stacked - 설명
    • newt_grid_v_stacked - 설명
    • newt_grid_wrapped_window - 설명
    • newt_grid_wrapped_window_at - 설명
    • newt_init - Initialize newt
    • newt_label - 설명
    • newt_label_set_text - 설명
    • newt_listbox - 설명
    • newt_listbox_append_entry - 설명
    • newt_listbox_clear - 설명
    • newt_listbox_clear_selection - 설명
    • newt_listbox_delete_entry - 설명
    • newt_listbox_get_current - 설명
    • newt_listbox_get_selection - 설명
    • newt_listbox_insert_entry - 설명
    • newt_listbox_item_count - 설명
    • newt_listbox_select_item - 설명
    • newt_listbox_set_current - 설명
    • newt_listbox_set_current_by_key - 설명
    • newt_listbox_set_data - 설명
    • newt_listbox_set_entry - 설명
    • newt_listbox_set_width - 설명
    • newt_listitem - 설명
    • newt_listitem_get_data - 설명
    • newt_listitem_set - 설명
    • newt_open_window - Open a window of the specified size and position
    • newt_pop_help_line - Replaces the current help line with the one from the stack
    • newt_pop_window - Removes the top window from the display
    • newt_push_help_line - Saves the current help line on a stack, and displays the new line
    • newt_radiobutton - 설명
    • newt_radio_get_current - 설명
    • newt_redraw_help_line - 설명
    • newt_reflow_text - 설명
    • newt_refresh - Updates modified portions of the screen
    • newt_resize_screen - 설명
    • newt_resume - Resume using the newt interface after calling newt_suspend
    • newt_run_form - Runs a form
    • newt_scale - 설명
    • newt_scale_set - 설명
    • newt_scrollbar_set - 설명
    • newt_set_help_callback - 설명
    • newt_set_suspend_callback - Set a callback function which gets invoked when user presses the suspend key
    • newt_suspend - Tells newt to return the terminal to its initial state
    • newt_textbox - 설명
    • newt_textbox_get_num_lines - 설명
    • newt_textbox_reflowed - 설명
    • newt_textbox_set_height - 설명
    • newt_textbox_set_text - 설명
    • newt_vertical_scrollbar - 설명
    • newt_wait_for_key - Doesn't return until a key has been pressed
    • newt_win_choice - 설명
    • newt_win_entries - 설명
    • newt_win_menu - 설명
    • newt_win_message - 설명
    • newt_win_messagev - 설명
    • newt_win_ternary - 설명
    • next - 배열의 내부 배열 포인터를 전진
    • ngettext - Plural version of gettext
    • nl2br - 문자열의 모든 줄바꿈 앞에 HTML 줄바꿈 태그를 삽입
    • nl_langinfo - Query language and locale information
    • NoRewindIterator::current - Get the current value
    • NoRewindIterator::getInnerIterator - Get the inner iterator
    • NoRewindIterator::key - Get the current key
    • NoRewindIterator::next - Forward to the next element
    • NoRewindIterator::rewind - Prevents the rewind operation on the inner iterator.
    • NoRewindIterator::valid - Validates the iterator
    • NoRewindIterator::__construct - Construct a NoRewindIterator
    • Normalizer::isNormalized - Checks if the provided string is already in the specified normalization form.
    • Normalizer::normalize - Normalizes the input provided and returns the normalized string
    • nsapi_request_headers - Fetch all HTTP request headers
    • nsapi_response_headers - Fetch all HTTP response headers
    • nsapi_virtual - Perform an NSAPI sub-request
    • nthmac - Obtain a nthmac key (needs 2 arguments)
    • NumberFormatter::create - Create a number formatter
    • NumberFormatter::format - Format a number
    • NumberFormatter::formatCurrency - Format a currency value
    • NumberFormatter::getAttribute - Get an attribute
    • NumberFormatter::getErrorCode - Get formatter's last error code.
    • NumberFormatter::getErrorMessage - Get formatter's last error message.
    • NumberFormatter::getLocale - Get formatter locale
    • NumberFormatter::getPattern - Get formatter pattern
    • NumberFormatter::getSymbol - Get a symbol value
    • NumberFormatter::getTextAttribute - Get a text attribute
    • NumberFormatter::parse - Parse a number
    • NumberFormatter::parseCurrency - Parse a currency number
    • NumberFormatter::setAttribute - Set an attribute
    • NumberFormatter::setPattern - Set formatter pattern
    • NumberFormatter::setSymbol - Set a symbol value
    • NumberFormatter::setTextAttribute - Set a text attribute
    • number_format - Format a number with grouped thousands
  • o
    • OAuth::disableDebug - Turn off verbose debugging
    • OAuth::disableRedirects - Turn off redirects
    • OAuth::disableSSLChecks - Turn off SSL checks
    • OAuth::enableDebug - Turn on verbose debugging
    • OAuth::enableRedirects - Turn on redirects
    • OAuth::enableSSLChecks - Turn on SSL checks
    • OAuth::fetch - Fetch an OAuth protected resource
    • OAuth::generateSignature - Generate a signature
    • OAuth::getAccessToken - Fetch an access token
    • OAuth::getCAPath - Gets CA information
    • OAuth::getLastResponse - Get the last response
    • OAuth::getLastResponseHeaders - Get headers for last response
    • OAuth::getLastResponseInfo - Get HTTP information about the last response
    • OAuth::getRequestHeader - Generate OAuth header string signature
    • OAuth::getRequestToken - Fetch a request token
    • OAuth::setAuthType - Set authorization type
    • OAuth::setCAPath - Set CA path and info
    • OAuth::setNonce - Set the nonce for subsequent requests
    • OAuth::setRequestEngine - The setRequestEngine purpose
    • OAuth::setRSACertificate - Set the RSA certificate
    • OAuth::setSSLChecks - Tweak specific SSL checks for requests.
    • OAuth::setTimestamp - Set the timestamp
    • OAuth::setToken - Sets the token and secret
    • OAuth::setVersion - Set the OAuth version
    • OAuth::__construct - Create a new OAuth object
    • OAuth::__destruct - The destructor
    • OAuthProvider::addRequiredParameter - Add required parameters
    • OAuthProvider::callconsumerHandler - Calls the consumerNonceHandler callback
    • OAuthProvider::callTimestampNonceHandler - Calls the timestampNonceHandler callback
    • OAuthProvider::calltokenHandler - Calls the tokenNonceHandler callback
    • OAuthProvider::checkOAuthRequest - Check an oauth request
    • OAuthProvider::consumerHandler - Set the consumerHandler handler callback
    • OAuthProvider::generateToken - Generate a random token
    • OAuthProvider::is2LeggedEndpoint - is2LeggedEndpoint
    • OAuthProvider::isRequestTokenEndpoint - Sets isRequestTokenEndpoint
    • OAuthProvider::removeRequiredParameter - Remove a required parameter
    • OAuthProvider::reportProblem - Report a problem
    • OAuthProvider::setParam - Set a parameter
    • OAuthProvider::setRequestTokenPath - Set request token path
    • OAuthProvider::timestampNonceHandler - Set the timestampNonceHandler handler callback
    • OAuthProvider::tokenHandler - Set the tokenHandler handler callback
    • OAuthProvider::__construct - Constructs a new OAuthProvider object
    • oauth_get_sbs - Generate a Signature Base String
    • oauth_urlencode - Encode a URI to RFC 3986
    • ob_clean - 출력 버퍼를 지웁니다
    • ob_end_clean - 출력 버퍼를 지우고 출력 버퍼링을 종료
    • ob_end_flush - 출력 버퍼를 전송하고 출력 버퍼링을 종료
    • ob_flush - 출력 버퍼를 전송합니다
    • ob_get_clean - 현재 버퍼 내용을 얻고 현재 출력 버퍼를 삭제
    • ob_get_contents - 출력 버퍼의 내용을 반환
    • ob_get_flush - Flush the output buffer, return it as a string and turn off output buffering
    • ob_get_length - 출력 버퍼의 길이를 반환
    • ob_get_level - 출력 버퍼링 메커니즘의 중첩 단계를 반환
    • ob_get_status - 출력 버퍼의 상태를 얻습니다
    • ob_gzhandler - gzip 출력 버퍼를 위한 ob_start 콜백 함수
    • ob_iconv_handler - Convert character encoding as output buffer handler
    • ob_implicit_flush - 즉시 출력의 여부를 결정
    • ob_list_handlers - List all output handlers in use
    • ob_start - 출력 버퍼링을 켭니다
    • ob_tidyhandler - ob_start callback function to repair the buffer
    • OCI-Collection::append - Appends element to the collection
    • OCI-Collection::assign - Assigns a value to the collection from another existing collection
    • OCI-Collection::assignElem - Assigns a value to the element of the collection
    • OCI-Collection::free - Frees the resources associated with the collection object
    • OCI-Collection::getElem - Returns value of the element
    • OCI-Collection::max - Returns the maximum number of elements in the collection
    • OCI-Collection::size - Returns size of the collection
    • OCI-Collection::trim - Trims elements from the end of the collection
    • OCI-Lob::append - Appends data from the large object to another large object
    • OCI-Lob::close - Closes LOB descriptor
    • OCI-Lob::eof - Tests for end-of-file on a large object's descriptor
    • OCI-Lob::erase - Erases a specified portion of the internal LOB data
    • OCI-Lob::export - Exports LOB's contents to a file
    • OCI-Lob::flush - Flushes/writes buffer of the LOB to the server
    • OCI-Lob::free - Frees resources associated with the LOB descriptor
    • OCI-Lob::getBuffering - Returns current state of buffering for the large object
    • OCI-Lob::import - Imports file data to the LOB
    • OCI-Lob::load - Returns large object's contents
    • OCI-Lob::read - Reads part of the large object
    • OCI-Lob::rewind - Moves the internal pointer to the beginning of the large object
    • OCI-Lob::save - Saves data to the large object
    • OCI-Lob::saveFile - 별칭: OCI-Lob::import
    • OCI-Lob::seek - Sets the internal pointer of the large object
    • OCI-Lob::setBuffering - Changes current state of buffering for the large object
    • OCI-Lob::size - Returns size of large object
    • OCI-Lob::tell - Returns the current position of internal pointer of large object
    • OCI-Lob::truncate - Truncates large object
    • OCI-Lob::write - Writes data to the large object
    • OCI-Lob::writeTemporary - Writes a temporary large object
    • OCI-Lob::writeToFile - 별칭: OCI-Lob::export
    • ocibindbyname - 별칭: oci_bind_by_name
    • ocicancel - 별칭: oci_cancel
    • ocicloselob - 별칭: OCI-Lob::close
    • ocicollappend - 별칭: OCI-Collection::append
    • ocicollassign - 별칭: OCI-Collection::assign
    • ocicollassignelem - 별칭: OCI-Collection::assignElem
    • ocicollgetelem - 별칭: OCI-Collection::getElem
    • ocicollmax - 별칭: OCI-Collection::max
    • ocicollsize - 별칭: OCI-Collection::size
    • ocicolltrim - 별칭: OCI-Collection::trim
    • ocicolumnisnull - 별칭: oci_field_is_null
    • ocicolumnname - 별칭: oci_field_name
    • ocicolumnprecision - 별칭: oci_field_precision
    • ocicolumnscale - 별칭: oci_field_scale
    • ocicolumnsize - 별칭: oci_field_size
    • ocicolumntype - 별칭: oci_field_type
    • ocicolumntyperaw - 별칭: oci_field_type_raw
    • ocicommit - 별칭: oci_commit
    • ocidefinebyname - 별칭: oci_define_by_name
    • ocierror - 별칭: oci_error
    • ociexecute - 별칭: oci_execute
    • ocifetch - 별칭: oci_fetch
    • ocifetchinto - Obsolete variant of oci_fetch_array, oci_fetch_object, oci_fetch_assoc and oci_fetch_row
    • ocifetchstatement - 별칭: oci_fetch_all
    • ocifreecollection - 별칭: OCI-Collection::free
    • ocifreecursor - 별칭: oci_free_statement
    • ocifreedesc - 별칭: OCI-Lob::free
    • ocifreestatement - 별칭: oci_free_statement
    • ociinternaldebug - 별칭: oci_internal_debug
    • ociloadlob - 별칭: OCI-Lob::load
    • ocilogoff - 별칭: oci_close
    • ocilogon - 별칭: oci_connect
    • ocinewcollection - 별칭: oci_new_collection
    • ocinewcursor - 별칭: oci_new_cursor
    • ocinewdescriptor - 별칭: oci_new_descriptor
    • ocinlogon - 별칭: oci_new_connect
    • ocinumcols - 별칭: oci_num_fields
    • ociparse - 별칭: oci_parse
    • ociplogon - 별칭: oci_pconnect
    • ociresult - 별칭: oci_result
    • ocirollback - 별칭: oci_rollback
    • ocirowcount - 별칭: oci_num_rows
    • ocisavelob - 별칭: OCI-Lob::save
    • ocisavelobfile - 별칭: OCI-Lob::import
    • ociserverversion - 별칭: oci_server_version
    • ocisetprefetch - 별칭: oci_set_prefetch
    • ocistatementtype - 별칭: oci_statement_type
    • ociwritelobtofile - 별칭: OCI-Lob::export
    • ociwritetemporarylob - 별칭: OCI-Lob::writeTemporary
    • oci_bind_array_by_name - Binds a PHP array to an Oracle PL/SQL array parameter
    • oci_bind_by_name - Binds a PHP variable to an Oracle placeholder
    • oci_cancel - Cancels reading from cursor
    • oci_client_version - Returns the Oracle client library version
    • oci_close - Closes an Oracle connection
    • oci_commit - Commits the outstanding database transaction
    • oci_connect - Connect to an Oracle database
    • oci_define_by_name - Associates a PHP variable with a column for query fetches
    • oci_error - Returns the last error found
    • oci_execute - Executes a statement
    • oci_fetch - Fetches the next row from a query into internal buffers
    • oci_fetch_all - Fetches multiple rows from a query into a two-dimensional array
    • oci_fetch_array - Returns the next row from a query as an associative or numeric array
    • oci_fetch_assoc - Returns the next row from a query as an associative array
    • oci_fetch_object - Returns the next row from a query as an object
    • oci_fetch_row - Returns the next row from a query as a numeric array
    • oci_field_is_null - Checks if a field in the currently fetched row is NULL
    • oci_field_name - Returns the name of a field from the statement
    • oci_field_precision - Tell the precision of a field
    • oci_field_scale - Tell the scale of the field
    • oci_field_size - Returns field's size
    • oci_field_type - Returns a field's data type name
    • oci_field_type_raw - Tell the raw Oracle data type of the field
    • oci_free_descriptor - Frees a descriptor
    • oci_free_statement - Frees all resources associated with statement or cursor
    • oci_get_implicit_resultset - Returns the next child statement resource from a parent statement resource that has Oracle Database 12c Implicit Result Sets
    • oci_internal_debug - Enables or disables internal debug output
    • oci_lob_copy - Copies large object
    • oci_lob_is_equal - Compares two LOB/FILE locators for equality
    • oci_new_collection - Allocates new collection object
    • oci_new_connect - Connect to the Oracle server using a unique connection
    • oci_new_cursor - Allocates and returns a new cursor (statement handle)
    • oci_new_descriptor - Initializes a new empty LOB or FILE descriptor
    • oci_num_fields - Returns the number of result columns in a statement
    • oci_num_rows - Returns number of rows affected during statement execution
    • oci_parse - Prepares an Oracle statement for execution
    • oci_password_change - Changes password of Oracle's user
    • oci_pconnect - Connect to an Oracle database using a persistent connection
    • oci_result - Returns field's value from the fetched row
    • oci_rollback - Rolls back the outstanding database transaction
    • oci_server_version - Returns the Oracle Database version
    • oci_set_action - Sets the action name
    • oci_set_client_identifier - Sets the client identifier
    • oci_set_client_info - Sets the client information
    • oci_set_edition - Sets the database edition
    • oci_set_module_name - Sets the module name
    • oci_set_prefetch - Sets number of rows to be prefetched by queries
    • oci_statement_type - Returns the type of a statement
    • octdec - 8진수를 10진수로
    • odbc_autocommit - Toggle autocommit behaviour
    • odbc_binmode - Handling of binary column data
    • odbc_close - Close an ODBC connection
    • odbc_close_all - Close all ODBC connections
    • odbc_columnprivileges - Lists columns and associated privileges for the given table
    • odbc_columns - Lists the column names in specified tables
    • odbc_commit - Commit an ODBC transaction
    • odbc_connect - Connect to a datasource
    • odbc_cursor - Get cursorname
    • odbc_data_source - Returns information about a current connection
    • odbc_do - 별칭: odbc_exec
    • odbc_error - Get the last error code
    • odbc_errormsg - Get the last error message
    • odbc_exec - Prepare and execute an SQL statement
    • odbc_execute - Execute a prepared statement
    • odbc_fetch_array - Fetch a result row as an associative array
    • odbc_fetch_into - Fetch one result row into array
    • odbc_fetch_object - Fetch a result row as an object
    • odbc_fetch_row - Fetch a row
    • odbc_field_len - Get the length (precision) of a field
    • odbc_field_name - Get the columnname
    • odbc_field_num - Return column number
    • odbc_field_precision - 별칭: odbc_field_len
    • odbc_field_scale - Get the scale of a field
    • odbc_field_type - Datatype of a field
    • odbc_foreignkeys - Retrieves a list of foreign keys
    • odbc_free_result - Free resources associated with a result
    • odbc_gettypeinfo - Retrieves information about data types supported by the data source
    • odbc_longreadlen - Handling of LONG columns
    • odbc_next_result - Checks if multiple results are available
    • odbc_num_fields - Number of columns in a result
    • odbc_num_rows - Number of rows in a result
    • odbc_pconnect - Open a persistent database connection
    • odbc_prepare - Prepares a statement for execution
    • odbc_primarykeys - Gets the primary keys for a table
    • odbc_procedurecolumns - Retrieve information about parameters to procedures
    • odbc_procedures - Get the list of procedures stored in a specific data source
    • odbc_result - Get result data
    • odbc_result_all - Print result as HTML table
    • odbc_rollback - Rollback a transaction
    • odbc_setoption - Adjust ODBC settings
    • odbc_specialcolumns - Retrieves special columns
    • odbc_statistics - Retrieve statistics about a table
    • odbc_tableprivileges - Lists tables and the privileges associated with each table
    • odbc_tables - Get the list of table names stored in a specific data source
    • ogg:// - Audio streams
    • opcache_compile_file - Compiles and caches a PHP script without executing it
    • opcache_get_configuration - Get configuration information about the cache
    • opcache_get_status - Get status information about the cache
    • opcache_invalidate - Invalidates a cached script
    • opcache_is_script_cached - Tells whether a script is cached in OPCache
    • opcache_reset - Resets the contents of the opcode cache
    • openal_buffer_create - Generate OpenAL buffer
    • openal_buffer_data - Load a buffer with data
    • openal_buffer_destroy - Destroys an OpenAL buffer
    • openal_buffer_get - Retrieve an OpenAL buffer property
    • openal_buffer_loadwav - Load a .wav file into a buffer
    • openal_context_create - Create an audio processing context
    • openal_context_current - Make the specified context current
    • openal_context_destroy - Destroys a context
    • openal_context_process - Process the specified context
    • openal_context_suspend - Suspend the specified context
    • openal_device_close - Close an OpenAL device
    • openal_device_open - Initialize the OpenAL audio layer
    • openal_listener_get - Retrieve a listener property
    • openal_listener_set - Set a listener property
    • openal_source_create - Generate a source resource
    • openal_source_destroy - Destroy a source resource
    • openal_source_get - Retrieve an OpenAL source property
    • openal_source_pause - Pause the source
    • openal_source_play - Start playing the source
    • openal_source_rewind - Rewind the source
    • openal_source_set - Set source property
    • openal_source_stop - Stop playing the source
    • openal_stream - Begin streaming on a source
    • opendir - 디렉토리 핸들을 엽니다
    • openlog - Open connection to system logger
    • openssl_cipher_iv_length - Gets the cipher iv length
    • openssl_csr_export - Exports a CSR as a string
    • openssl_csr_export_to_file - Exports a CSR to a file
    • openssl_csr_get_public_key - Returns the public key of a CERT
    • openssl_csr_get_subject - Returns the subject of a CERT
    • openssl_csr_new - Generates a CSR
    • openssl_csr_sign - Sign a CSR with another certificate (or itself) and generate a certificate
    • openssl_decrypt - Decrypts data
    • openssl_dh_compute_key - Computes shared secret for public value of remote DH key and local DH key
    • openssl_digest - Computes a digest
    • openssl_encrypt - Encrypts data
    • openssl_error_string - Return openSSL error message
    • openssl_free_key - Free key resource
    • openssl_get_cert_locations - Retrieve the available certificate locations
    • openssl_get_cipher_methods - Gets available cipher methods
    • openssl_get_md_methods - Gets available digest methods
    • openssl_get_privatekey - 별칭: openssl_pkey_get_private
    • openssl_get_publickey - 별칭: openssl_pkey_get_public
    • openssl_open - Open sealed data
    • openssl_pbkdf2 - Generates a PKCS5 v2 PBKDF2 string, defaults to SHA-1
    • openssl_pkcs7_decrypt - Decrypts an S/MIME encrypted message
    • openssl_pkcs7_encrypt - Encrypt an S/MIME message
    • openssl_pkcs7_sign - Sign an S/MIME message
    • openssl_pkcs7_verify - Verifies the signature of an S/MIME signed message
    • openssl_pkcs12_export - Exports a PKCS#12 Compatible Certificate Store File to variable.
    • openssl_pkcs12_export_to_file - Exports a PKCS#12 Compatible Certificate Store File
    • openssl_pkcs12_read - Parse a PKCS#12 Certificate Store into an array
    • openssl_pkey_export - Gets an exportable representation of a key into a string
    • openssl_pkey_export_to_file - Gets an exportable representation of a key into a file
    • openssl_pkey_free - Frees a private key
    • openssl_pkey_get_details - Returns an array with the key details
    • openssl_pkey_get_private - Get a private key
    • openssl_pkey_get_public - Extract public key from certificate and prepare it for use
    • openssl_pkey_new - Generates a new private key
    • openssl_private_decrypt - Decrypts data with private key
    • openssl_private_encrypt - Encrypts data with private key
    • openssl_public_decrypt - Decrypts data with public key
    • openssl_public_encrypt - Encrypts data with public key
    • openssl_random_pseudo_bytes - Generate a pseudo-random string of bytes
    • openssl_seal - Seal (encrypt) data
    • openssl_sign - Generate signature
    • openssl_spki_export - Exports a valid PEM formatted public key signed public key and challenge
    • openssl_spki_export_challenge - Exports the challenge assoicated with a signed public key and challenge
    • openssl_spki_new - Generate a new signed public key and challenge
    • openssl_spki_verify - Verifies a signed public key and challenge
    • openssl_verify - Verify signature
    • openssl_x509_checkpurpose - Verifies if a certificate can be used for a particular purpose
    • openssl_x509_check_private_key - Checks if a private key corresponds to a certificate
    • openssl_x509_export - Exports a certificate as a string
    • openssl_x509_export_to_file - Exports a certificate to file
    • openssl_x509_fingerprint - Calculates the fingerprint, or digest, of a given X.509 certificate
    • openssl_x509_free - Free certificate resource
    • openssl_x509_parse - Parse an X509 certificate and return the information as an array
    • openssl_x509_read - Parse an X.509 certificate and return a resource identifier for it
    • ord - 문자의 아스키 값을 반환
    • OuterIterator::getInnerIterator - Returns the inner iterator for the current entry.
    • output_add_rewrite_var - Add URL rewriter values
    • output_reset_rewrite_vars - Reset URL rewriter values
    • override_function - Overrides built-in functions
  • p
    • pack - Pack data into binary string
    • ParentIterator::accept - Determines acceptability
    • ParentIterator::getChildren - Return the inner iterator's children contained in a ParentIterator
    • ParentIterator::hasChildren - Check whether the inner iterator's current element has children
    • ParentIterator::next - Move the iterator forward
    • ParentIterator::rewind - Rewind the iterator
    • ParentIterator::__construct - Constructs a ParentIterator
    • parsekit_compile_file - Compile a PHP file and return the resulting op array
    • parsekit_compile_string - Compile a string of PHP code and return the resulting op array
    • parsekit_func_arginfo - Return information regarding function argument(s)
    • parse_ini_file - Parse a configuration file
    • parse_ini_string - Parse a configuration string
    • parse_str - 문자열을 변수로 해석
    • parse_url - URL을 해석해서 구성요소를 반환
    • passthru - Execute an external program and display raw output
    • password_get_info - Returns information about the given hash
    • password_hash - Creates a password hash
    • password_needs_rehash - Checks if the given hash matches the given options
    • password_verify - Verifies that a password matches a hash
    • pathinfo - Returns information about a file path
    • pclose - Closes process file pointer
    • pcntl_alarm - Set an alarm clock for delivery of a signal
    • pcntl_errno - 별칭: pcntl_strerror
    • pcntl_exec - Executes specified program in current process space
    • pcntl_fork - Forks the currently running process
    • pcntl_getpriority - Get the priority of any process
    • pcntl_get_last_error - Retrieve the error number set by the last pcntl function which failed
    • pcntl_setpriority - Change the priority of any process
    • pcntl_signal - Installs a signal handler
    • pcntl_signal_dispatch - Calls signal handlers for pending signals
    • pcntl_sigprocmask - Sets and retrieves blocked signals
    • pcntl_sigtimedwait - Waits for signals, with a timeout
    • pcntl_sigwaitinfo - Waits for signals
    • pcntl_strerror - Retrieve the system error message associated with the given errno
    • pcntl_wait - Waits on or returns the status of a forked child
    • pcntl_waitpid - Waits on or returns the status of a forked child
    • pcntl_wexitstatus - Returns the return code of a terminated child
    • pcntl_wifexited - Checks if status code represents a normal exit
    • pcntl_wifsignaled - Checks whether the status code represents a termination due to a signal
    • pcntl_wifstopped - Checks whether the child process is currently stopped
    • pcntl_wstopsig - Returns the signal which caused the child to stop
    • pcntl_wtermsig - Returns the signal which caused the child to terminate
    • PDF_activate_item - Activate structure element or other content item
    • PDF_add_annotation - Add annotation [deprecated]
    • PDF_add_bookmark - Add bookmark for current page [deprecated]
    • PDF_add_launchlink - Add launch annotation for current page [deprecated]
    • PDF_add_locallink - Add link annotation for current page [deprecated]
    • PDF_add_nameddest - Create named destination
    • PDF_add_note - Set annotation for current page [deprecated]
    • PDF_add_outline - Add bookmark for current page [deprecated]
    • PDF_add_pdflink - Add file link annotation for current page [deprecated]
    • PDF_add_table_cell - Add a cell to a new or existing table
    • PDF_add_textflow - Create Textflow or add text to existing Textflow
    • PDF_add_thumbnail - Add thumbnail for current page
    • PDF_add_weblink - Add weblink for current page [deprecated]
    • PDF_arc - Draw a counterclockwise circular arc segment
    • PDF_arcn - Draw a clockwise circular arc segment
    • PDF_attach_file - Add file attachment for current page [deprecated]
    • PDF_begin_document - Create new PDF file
    • PDF_begin_font - Start a Type 3 font definition
    • PDF_begin_glyph - Start glyph definition for Type 3 font
    • PDF_begin_item - Open structure element or other content item
    • PDF_begin_layer - Start layer
    • PDF_begin_page - Start new page [deprecated]
    • PDF_begin_page_ext - Start new page
    • PDF_begin_pattern - Start pattern definition
    • PDF_begin_template - Start template definition [deprecated]
    • PDF_begin_template_ext - Start template definition
    • PDF_circle - Draw a circle
    • PDF_clip - Clip to current path
    • PDF_close - Close pdf resource [deprecated]
    • PDF_closepath - Close current path
    • PDF_closepath_fill_stroke - Close, fill and stroke current path
    • PDF_closepath_stroke - Close and stroke path
    • PDF_close_image - Close image
    • PDF_close_pdi - Close the input PDF document [deprecated]
    • PDF_close_pdi_page - Close the page handle
    • PDF_concat - Concatenate a matrix to the CTM
    • PDF_continue_text - Output text in next line
    • PDF_create_3dview - Create 3D view
    • PDF_create_action - Create action for objects or events
    • PDF_create_annotation - Create rectangular annotation
    • PDF_create_bookmark - Create bookmark
    • PDF_create_field - Create form field
    • PDF_create_fieldgroup - Create form field group
    • PDF_create_gstate - Create graphics state object
    • PDF_create_pvf - Create PDFlib virtual file
    • PDF_create_textflow - Create textflow object
    • PDF_curveto - Draw Bezier curve
    • PDF_define_layer - Create layer definition
    • PDF_delete - Delete PDFlib object
    • PDF_delete_pvf - Delete PDFlib virtual file
    • PDF_delete_table - Delete table object
    • PDF_delete_textflow - Delete textflow object
    • PDF_encoding_set_char - Add glyph name and/or Unicode value
    • PDF_endpath - End current path
    • PDF_end_document - Close PDF file
    • PDF_end_font - Terminate Type 3 font definition
    • PDF_end_glyph - Terminate glyph definition for Type 3 font
    • PDF_end_item - Close structure element or other content item
    • PDF_end_layer - Deactivate all active layers
    • PDF_end_page - Finish page
    • PDF_end_page_ext - Finish page
    • PDF_end_pattern - Finish pattern
    • PDF_end_template - Finish template
    • PDF_fill - Fill current path
    • PDF_fill_imageblock - Fill image block with variable data
    • PDF_fill_pdfblock - Fill PDF block with variable data
    • PDF_fill_stroke - Fill and stroke path
    • PDF_fill_textblock - Fill text block with variable data
    • PDF_findfont - Prepare font for later use [deprecated]
    • PDF_fit_image - Place image or template
    • PDF_fit_pdi_page - Place imported PDF page
    • PDF_fit_table - Place table on page
    • PDF_fit_textflow - Format textflow in rectangular area
    • PDF_fit_textline - Place single line of text
    • PDF_get_apiname - Get name of unsuccessfull API function
    • PDF_get_buffer - Get PDF output buffer
    • PDF_get_errmsg - Get error text
    • PDF_get_errnum - Get error number
    • PDF_get_font - Get font [deprecated]
    • PDF_get_fontname - Get font name [deprecated]
    • PDF_get_fontsize - Font handling [deprecated]
    • PDF_get_image_height - Get image height [deprecated]
    • PDF_get_image_width - Get image width [deprecated]
    • PDF_get_majorversion - Get major version number [deprecated]
    • PDF_get_minorversion - Get minor version number [deprecated]
    • PDF_get_parameter - Get string parameter
    • PDF_get_pdi_parameter - Get PDI string parameter [deprecated]
    • PDF_get_pdi_value - Get PDI numerical parameter [deprecated]
    • PDF_get_value - Get numerical parameter
    • PDF_info_font - Query detailed information about a loaded font
    • PDF_info_matchbox - Query matchbox information
    • PDF_info_table - Retrieve table information
    • PDF_info_textflow - Query textflow state
    • PDF_info_textline - Perform textline formatting and query metrics
    • PDF_initgraphics - Reset graphic state
    • PDF_lineto - Draw a line
    • PDF_load_3ddata - Load 3D model
    • PDF_load_font - Search and prepare font
    • PDF_load_iccprofile - Search and prepare ICC profile
    • PDF_load_image - Open image file
    • PDF_makespotcolor - Make spot color
    • PDF_moveto - Set current point
    • PDF_new - Create PDFlib object
    • PDF_open_ccitt - Open raw CCITT image [deprecated]
    • PDF_open_file - Create PDF file [deprecated]
    • PDF_open_gif - Open GIF image [deprecated]
    • PDF_open_image - Use image data [deprecated]
    • PDF_open_image_file - Read image from file [deprecated]
    • PDF_open_jpeg - Open JPEG image [deprecated]
    • PDF_open_memory_image - Open image created with PHP's image functions [not supported]
    • PDF_open_pdi - Open PDF file [deprecated]
    • PDF_open_pdi_document - Prepare a pdi document
    • PDF_open_pdi_page - Prepare a page
    • PDF_open_tiff - Open TIFF image [deprecated]
    • PDF_pcos_get_number - Get value of pCOS path with type number or boolean
    • PDF_pcos_get_stream - Get contents of pCOS path with type stream, fstream, or string
    • PDF_pcos_get_string - Get value of pCOS path with type name, string, or boolean
    • PDF_place_image - Place image on the page [deprecated]
    • PDF_place_pdi_page - Place PDF page [deprecated]
    • PDF_process_pdi - Process imported PDF document
    • PDF_rect - Draw rectangle
    • PDF_restore - Restore graphics state
    • PDF_resume_page - Resume page
    • PDF_rotate - Rotate coordinate system
    • PDF_save - Save graphics state
    • PDF_scale - Scale coordinate system
    • PDF_setcolor - Set fill and stroke color
    • PDF_setdash - Set simple dash pattern
    • PDF_setdashpattern - Set dash pattern
    • PDF_setflat - Set flatness
    • PDF_setfont - Set font
    • PDF_setgray - Set color to gray [deprecated]
    • PDF_setgray_fill - Set fill color to gray [deprecated]
    • PDF_setgray_stroke - Set stroke color to gray [deprecated]
    • PDF_setlinecap - Set linecap parameter
    • PDF_setlinejoin - Set linejoin parameter
    • PDF_setlinewidth - Set line width
    • PDF_setmatrix - Set current transformation matrix
    • PDF_setmiterlimit - Set miter limit
    • PDF_setpolydash - Set complicated dash pattern [deprecated]
    • PDF_setrgbcolor - Set fill and stroke rgb color values [deprecated]
    • PDF_setrgbcolor_fill - Set fill rgb color values [deprecated]
    • PDF_setrgbcolor_stroke - Set stroke rgb color values [deprecated]
    • PDF_set_border_color - Set border color of annotations [deprecated]
    • PDF_set_border_dash - Set border dash style of annotations [deprecated]
    • PDF_set_border_style - Set border style of annotations [deprecated]
    • PDF_set_char_spacing - Set character spacing [deprecated]
    • PDF_set_duration - Set duration between pages [deprecated]
    • PDF_set_gstate - Activate graphics state object
    • PDF_set_horiz_scaling - Set horizontal text scaling [deprecated]
    • PDF_set_info - Fill document info field
    • PDF_set_info_author - Fill the author document info field [deprecated]
    • PDF_set_info_creator - Fill the creator document info field [deprecated]
    • PDF_set_info_keywords - Fill the keywords document info field [deprecated]
    • PDF_set_info_subject - Fill the subject document info field [deprecated]
    • PDF_set_info_title - Fill the title document info field [deprecated]
    • PDF_set_layer_dependency - Define relationships among layers
    • PDF_set_leading - Set distance between text lines [deprecated]
    • PDF_set_parameter - Set string parameter
    • PDF_set_text_matrix - Set text matrix [deprecated]
    • PDF_set_text_pos - Set text position
    • PDF_set_text_rendering - Determine text rendering [deprecated]
    • PDF_set_text_rise - Set text rise [deprecated]
    • PDF_set_value - Set numerical parameter
    • PDF_set_word_spacing - Set spacing between words [deprecated]
    • PDF_shading - Define blend
    • PDF_shading_pattern - Define shading pattern
    • PDF_shfill - Fill area with shading
    • PDF_show - Output text at current position
    • PDF_show_boxed - Output text in a box [deprecated]
    • PDF_show_xy - Output text at given position
    • PDF_skew - Skew the coordinate system
    • PDF_stringwidth - Return width of text
    • PDF_stroke - Stroke path
    • PDF_suspend_page - Suspend page
    • PDF_translate - Set origin of coordinate system
    • PDF_utf8_to_utf16 - Convert string from UTF-8 to UTF-16
    • PDF_utf16_to_utf8 - Convert string from UTF-16 to UTF-8
    • PDF_utf32_to_utf16 - Convert string from UTF-32 to UTF-16
    • PDO::beginTransaction - Initiates a transaction
    • PDO::commit - Commits a transaction
    • PDO::cubrid_schema - Get the requested schema information
    • PDO::errorCode - Fetch the SQLSTATE associated with the last operation on the database handle
    • PDO::errorInfo - Fetch extended error information associated with the last operation on the database handle
    • PDO::exec - Execute an SQL statement and return the number of affected rows
    • PDO::getAttribute - Retrieve a database connection attribute
    • PDO::getAvailableDrivers - Return an array of available PDO drivers
    • PDO::inTransaction - Checks if inside a transaction
    • PDO::lastInsertId - Returns the ID of the last inserted row or sequence value
    • PDO::pgsqlCopyFromArray - Copy data from PHP array into table
    • PDO::pgsqlCopyFromFile - Copy data from file into table
    • PDO::pgsqlCopyToArray - Copy data from database table into PHP array
    • PDO::pgsqlCopyToFile - Copy data from table into file
    • PDO::pgsqlGetNotify - Get asynchronous notification
    • PDO::pgsqlGetPid - Get the server PID
    • PDO::pgsqlLOBCreate - 새 라지 오브젝트를 생성합니다
    • PDO::pgsqlLOBOpen - 존재하는 라지 오브젝트 스트림을 엽니다
    • PDO::pgsqlLOBUnlink - 라지 오브젝트를 제거합니다
    • PDO::prepare - Prepares a statement for execution and returns a statement object
    • PDO::query - Executes an SQL statement, returning a result set as a PDOStatement object
    • PDO::quote - Quotes a string for use in a query.
    • PDO::rollBack - Rolls back a transaction
    • PDO::setAttribute - Set an attribute
    • PDO::sqliteCreateAggregate - Registers an aggregating User Defined Function for use in SQL statements
    • PDO::sqliteCreateCollation - Registers a User Defined Function for use as a collating function in SQL statements
    • PDO::sqliteCreateFunction - Registers a User Defined Function for use in SQL statements
    • PDO::__construct - Creates a PDO instance representing a connection to a database
    • PDOStatement::bindColumn - Bind a column to a PHP variable
    • PDOStatement::bindParam - Binds a parameter to the specified variable name
    • PDOStatement::bindValue - Binds a value to a parameter
    • PDOStatement::closeCursor - Closes the cursor, enabling the statement to be executed again.
    • PDOStatement::columnCount - Returns the number of columns in the result set
    • PDOStatement::debugDumpParams - Dump an SQL prepared command
    • PDOStatement::errorCode - Fetch the SQLSTATE associated with the last operation on the statement handle
    • PDOStatement::errorInfo - Fetch extended error information associated with the last operation on the statement handle
    • PDOStatement::execute - Executes a prepared statement
    • PDOStatement::fetch - Fetches the next row from a result set
    • PDOStatement::fetchAll - Returns an array containing all of the result set rows
    • PDOStatement::fetchColumn - Returns a single column from the next row of a result set
    • PDOStatement::fetchObject - Fetches the next row and returns it as an object.
    • PDOStatement::getAttribute - Retrieve a statement attribute
    • PDOStatement::getColumnMeta - Returns metadata for a column in a result set
    • PDOStatement::nextRowset - Advances to the next rowset in a multi-rowset statement handle
    • PDOStatement::rowCount - Returns the number of rows affected by the last SQL statement
    • PDOStatement::setAttribute - Set a statement attribute
    • PDOStatement::setFetchMode - Set the default fetch mode for this statement
    • PDO_4D DSN - Connecting to 4D SQL server
    • PDO_CUBRID DSN - Connecting to CUBRID databases
    • PDO_DBLIB DSN - Connecting to Microsoft SQL Server and Sybase databases
    • PDO_FIREBIRD DSN - Connecting to Firebird databases
    • PDO_IBM DSN - Connecting to IBM databases
    • PDO_INFORMIX DSN - Connecting to Informix databases
    • PDO_MYSQL DSN - Connecting to MySQL databases
    • PDO_OCI DSN - Connecting to Oracle databases
    • PDO_ODBC DSN - Connecting to ODBC or DB2 databases
    • PDO_PGSQL DSN - PostgreSQL 데이터베이스에 접속하기
    • PDO_SQLITE DSN - Connecting to SQLite databases
    • PDO_SQLSRV DSN - Connecting to MS SQL Server and SQL Azure databases
    • pfsockopen - Open persistent Internet or Unix domain socket connection
    • pg_affected_rows - 영향 받은 레코드(튜플) 수를 반환
    • pg_cancel_query - 비동기 질의 취소
    • pg_client_encoding - 클라이언트 인코딩을 얻음
    • pg_close - Closes a PostgreSQL connection
    • pg_connect - Open a PostgreSQL connection
    • pg_connection_busy - Get connection is busy or not
    • pg_connection_reset - Reset connection (reconnect)
    • pg_connection_status - Get connection status
    • pg_connect_poll - Poll the status of an in-progress asynchronous PostgreSQL connection attempt.
    • pg_consume_input - Reads input on the connection
    • pg_convert - Convert associative array values into suitable for SQL statement
    • pg_copy_from - Insert records into a table from an array
    • pg_copy_to - Copy a table to an array
    • pg_dbname - Get the database name
    • pg_delete - Deletes records
    • pg_end_copy - Sync with PostgreSQL backend
    • pg_escape_bytea - Escape a string for insertion into a bytea field
    • pg_escape_identifier - Escape a identifier for insertion into a text field
    • pg_escape_literal - Escape a literal for insertion into a text field
    • pg_escape_string - Escape a string for query
    • pg_execute - Sends a request to execute a prepared statement with given parameters, and waits for the result.
    • pg_fetch_all - Fetches all rows from a result as an array
    • pg_fetch_all_columns - Fetches all rows in a particular result column as an array
    • pg_fetch_array - Fetch a row as an array
    • pg_fetch_assoc - Fetch a row as an associative array
    • pg_fetch_object - Fetch a row as an object
    • pg_fetch_result - Returns values from a result resource
    • pg_fetch_row - Get a row as an enumerated array
    • pg_field_is_null - Test if a field is SQL NULL
    • pg_field_name - Returns the name of a field
    • pg_field_num - Returns the field number of the named field
    • pg_field_prtlen - Returns the printed length
    • pg_field_size - Returns the internal storage size of the named field
    • pg_field_table - Returns the name or oid of the tables field
    • pg_field_type - Returns the type name for the corresponding field number
    • pg_field_type_oid - Returns the type ID (OID) for the corresponding field number
    • pg_flush - Flush outbound query data on the connection
    • pg_free_result - Free result memory
    • pg_get_notify - Gets SQL NOTIFY message
    • pg_get_pid - Gets the backend's process ID
    • pg_get_result - Get asynchronous query result
    • pg_host - Returns the host name associated with the connection
    • pg_insert - Insert array into table
    • pg_last_error - Get the last error message string of a connection
    • pg_last_notice - Returns the last notice message from PostgreSQL server
    • pg_last_oid - Returns the last row's OID
    • pg_lo_close - Close a large object
    • pg_lo_create - Create a large object
    • pg_lo_export - Export a large object to file
    • pg_lo_import - Import a large object from file
    • pg_lo_open - Open a large object
    • pg_lo_read - Read a large object
    • pg_lo_read_all - Reads an entire large object and send straight to browser
    • pg_lo_seek - Seeks position within a large object
    • pg_lo_tell - Returns current seek position a of large object
    • pg_lo_truncate - Truncates a large object
    • pg_lo_unlink - Delete a large object
    • pg_lo_write - Write to a large object
    • pg_meta_data - Get meta data for table
    • pg_num_fields - Returns the number of fields in a result
    • pg_num_rows - Returns the number of rows in a result
    • pg_options - Get the options associated with the connection
    • pg_parameter_status - Looks up a current parameter setting of the server.
    • pg_pconnect - Open a persistent PostgreSQL connection
    • pg_ping - Ping database connection
    • pg_port - Return the port number associated with the connection
    • pg_prepare - Submits a request to create a prepared statement with the given parameters, and waits for completion.
    • pg_put_line - Send a NULL-terminated string to PostgreSQL backend
    • pg_query - Execute a query
    • pg_query_params - Submits a command to the server and waits for the result, with the ability to pass parameters separately from the SQL command text.
    • pg_result_error - Get error message associated with result
    • pg_result_error_field - Returns an individual field of an error report.
    • pg_result_seek - Set internal row offset in result resource
    • pg_result_status - Get status of query result
    • pg_select - Select records
    • pg_send_execute - Sends a request to execute a prepared statement with given parameters, without waiting for the result(s).
    • pg_send_prepare - Sends a request to create a prepared statement with the given parameters, without waiting for completion.
    • pg_send_query - Sends asynchronous query
    • pg_send_query_params - Submits a command and separate parameters to the server without waiting for the result(s).
    • pg_set_client_encoding - Set the client encoding
    • pg_set_error_verbosity - Determines the verbosity of messages returned by pg_last_error and pg_result_error.
    • pg_socket - Get a read only handle to the socket underlying a PostgreSQL connection
    • pg_trace - Enable tracing a PostgreSQL connection
    • pg_transaction_status - Returns the current in-transaction status of the server.
    • pg_tty - Return the TTY name associated with the connection
    • pg_unescape_bytea - Unescape binary for bytea type
    • pg_untrace - Disable tracing of a PostgreSQL connection
    • pg_update - Update table
    • pg_version - Returns an array with client, protocol and server version (when available)
    • phar:// - PHP Archive
    • Phar::addEmptyDir - Add an empty directory to the phar archive
    • Phar::addFile - Add a file from the filesystem to the phar archive
    • Phar::addFromString - Add a file from a string to the phar archive
    • Phar::apiVersion - Returns the api version
    • Phar::buildFromDirectory - Construct a phar archive from the files within a directory.
    • Phar::buildFromIterator - Construct a phar archive from an iterator.
    • Phar::canCompress - Returns whether phar extension supports compression using either zlib or bzip2
    • Phar::canWrite - Returns whether phar extension supports writing and creating phars
    • Phar::compress - Compresses the entire Phar archive using Gzip or Bzip2 compression
    • Phar::compressAllFilesBZIP2 - Compresses all files in the current Phar archive using Bzip2 compression
    • Phar::compressAllFilesGZ - Compresses all files in the current Phar archive using Gzip compression
    • Phar::compressFiles - Compresses all files in the current Phar archive
    • Phar::convertToData - Convert a phar archive to a non-executable tar or zip file
    • Phar::convertToExecutable - Convert a phar archive to another executable phar archive file format
    • Phar::copy - Copy a file internal to the phar archive to another new file within the phar
    • Phar::count - Returns the number of entries (files) in the Phar archive
    • Phar::createDefaultStub - Create a phar-file format specific stub
    • Phar::decompress - Decompresses the entire Phar archive
    • Phar::decompressFiles - Decompresses all files in the current Phar archive
    • Phar::delete - Delete a file within a phar archive
    • Phar::delMetadata - Deletes the global metadata of the phar
    • Phar::extractTo - Extract the contents of a phar archive to a directory
    • Phar::getMetadata - Returns phar archive meta-data
    • Phar::getModified - Return whether phar was modified
    • Phar::getSignature - Return MD5/SHA1/SHA256/SHA512/OpenSSL signature of a Phar archive
    • Phar::getStub - Return the PHP loader or bootstrap stub of a Phar archive
    • Phar::getSupportedCompression - Return array of supported compression algorithms
    • Phar::getSupportedSignatures - Return array of supported signature types
    • Phar::getVersion - Return version info of Phar archive
    • Phar::hasMetadata - Returns whether phar has global meta-data
    • Phar::interceptFileFuncs - instructs phar to intercept fopen, file_get_contents, opendir, and all of the stat-related functions
    • Phar::isBuffering - Used to determine whether Phar write operations are being buffered, or are flushing directly to disk
    • Phar::isCompressed - Returns Phar::GZ or PHAR::BZ2 if the entire phar archive is compressed (.tar.gz/tar.bz and so on)
    • Phar::isFileFormat - Returns true if the phar archive is based on the tar/phar/zip file format depending on the parameter
    • Phar::isValidPharFilename - Returns whether the given filename is a valid phar filename
    • Phar::isWritable - Returns true if the phar archive can be modified
    • Phar::loadPhar - Loads any phar archive with an alias
    • Phar::mapPhar - Reads the currently executed file (a phar) and registers its manifest
    • Phar::mount - Mount an external path or file to a virtual location within the phar archive
    • Phar::mungServer - Defines a list of up to 4 $_SERVER variables that should be modified for execution
    • Phar::offsetExists - determines whether a file exists in the phar
    • Phar::offsetGet - Gets a PharFileInfo object for a specific file
    • Phar::offsetSet - set the contents of an internal file to those of an external file
    • Phar::offsetUnset - remove a file from a phar
    • Phar::running - Returns the full path on disk or full phar URL to the currently executing Phar archive
    • Phar::setAlias - Set the alias for the Phar archive
    • Phar::setDefaultStub - Used to set the PHP loader or bootstrap stub of a Phar archive to the default loader
    • Phar::setMetadata - Sets phar archive meta-data
    • Phar::setSignatureAlgorithm - set the signature algorithm for a phar and apply it.
    • Phar::setStub - Used to set the PHP loader or bootstrap stub of a Phar archive
    • Phar::startBuffering - Start buffering Phar write operations, do not modify the Phar object on disk
    • Phar::stopBuffering - Stop buffering write requests to the Phar archive, and save changes to disk
    • Phar::uncompressAllFiles - Uncompresses all files in the current Phar archive
    • Phar::unlinkArchive - Completely remove a phar archive from disk and from memory
    • Phar::webPhar - mapPhar for web-based phars. front controller for web applications
    • Phar::__construct - Construct a Phar archive object
    • Phar context options - Phar context option listing
    • PharData::addEmptyDir - Add an empty directory to the tar/zip archive
    • PharData::addFile - Add a file from the filesystem to the tar/zip archive
    • PharData::addFromString - Add a file from the filesystem to the tar/zip archive
    • PharData::buildFromDirectory - Construct a tar/zip archive from the files within a directory.
    • PharData::buildFromIterator - Construct a tar or zip archive from an iterator.
    • PharData::compress - Compresses the entire tar/zip archive using Gzip or Bzip2 compression
    • PharData::compressFiles - Compresses all files in the current tar/zip archive
    • PharData::convertToData - Convert a phar archive to a non-executable tar or zip file
    • PharData::convertToExecutable - Convert a non-executable tar/zip archive to an executable phar archive
    • PharData::copy - Copy a file internal to the phar archive to another new file within the phar
    • PharData::decompress - Decompresses the entire Phar archive
    • PharData::decompressFiles - Decompresses all files in the current zip archive
    • PharData::delete - Delete a file within a tar/zip archive
    • PharData::delMetadata - Deletes the global metadata of a zip archive
    • PharData::extractTo - Extract the contents of a tar/zip archive to a directory
    • PharData::isWritable - Returns true if the tar/zip archive can be modified
    • PharData::offsetSet - set the contents of a file within the tar/zip to those of an external file or string
    • PharData::offsetUnset - remove a file from a tar/zip archive
    • PharData::setAlias - dummy function (Phar::setAlias is not valid for PharData)
    • PharData::setDefaultStub - dummy function (Phar::setDefaultStub is not valid for PharData)
    • PharData::setStub - dummy function (Phar::setStub is not valid for PharData)
    • PharData::__construct - Construct a non-executable tar or zip archive object
    • PharException - The PharException class provides a phar-specific exception class for try/catch blocks.
    • PharFileInfo::chmod - Sets file-specific permission bits
    • PharFileInfo::compress - Compresses the current Phar entry with either zlib or bzip2 compression
    • PharFileInfo::decompress - Decompresses the current Phar entry within the phar
    • PharFileInfo::delMetadata - Deletes the metadata of the entry
    • PharFileInfo::getCompressedSize - Returns the actual size of the file (with compression) inside the Phar archive
    • PharFileInfo::getCRC32 - Returns CRC32 code or throws an exception if CRC has not been verified
    • PharFileInfo::getMetadata - Returns file-specific meta-data saved with a file
    • PharFileInfo::getPharFlags - Returns the Phar file entry flags
    • PharFileInfo::hasMetadata - Returns the metadata of the entry
    • PharFileInfo::isCompressed - Returns whether the entry is compressed
    • PharFileInfo::isCompressedBZIP2 - Returns whether the entry is compressed using bzip2
    • PharFileInfo::isCompressedGZ - Returns whether the entry is compressed using gz
    • PharFileInfo::isCRCChecked - Returns whether file entry has had its CRC verified
    • PharFileInfo::setCompressedBZIP2 - Compresses the current Phar entry within the phar using Bzip2 compression
    • PharFileInfo::setCompressedGZ - Compresses the current Phar entry within the phar using gz compression
    • PharFileInfo::setMetadata - Sets file-specific meta-data saved with a file
    • PharFileInfo::setUncompressed - Uncompresses the current Phar entry within the phar, if it is compressed
    • PharFileInfo::__construct - Construct a Phar entry object
    • php:// - Accessing various I/O streams
    • phpcredits - Prints out the credits for PHP
    • phpinfo - Outputs information about PHP's configuration
    • phpversion - Gets the current PHP version
    • php_check_syntax - Check the PHP syntax of (and execute) the specified file
    • php_ini_loaded_file - Retrieve a path to the loaded php.ini file
    • php_ini_scanned_files - Return a list of .ini files parsed from the additional ini dir
    • php_logo_guid - Gets the logo guid
    • php_sapi_name - 웹 서버와 PHP 사이의 인터페이스 형식을 반환
    • php_strip_whitespace - Return source with stripped comments and whitespace
    • php_uname - Returns information about the operating system PHP is running on
    • php_user_filter::filter - Called when applying the filter
    • php_user_filter::onClose - Called when closing the filter
    • php_user_filter::onCreate - Called when creating the filter
    • pi - 파이 값을 얻는다
    • png2wbmp - Convert PNG image file to WBMP image file
    • Pool::collect - Collect references to completed tasks
    • Pool::resize - Resize the Pool
    • Pool::shutdown - Shutdown all Workers
    • Pool::submit - Submits an object for execution
    • Pool::submitTo - Submits an object for execution
    • Pool::__construct - Creates a new Pool of Workers
    • popen - Opens process file pointer
    • pos - 별칭: current
    • posix_access - Determine accessibility of a file
    • posix_ctermid - Get path name of controlling terminal
    • posix_errno - 별칭: posix_get_last_error
    • posix_getcwd - Pathname of current directory
    • posix_getegid - Return the effective group ID of the current process
    • posix_geteuid - Return the effective user ID of the current process
    • posix_getgid - Return the real group ID of the current process
    • posix_getgrgid - Return info about a group by group id
    • posix_getgrnam - Return info about a group by name
    • posix_getgroups - Return the group set of the current process
    • posix_getlogin - Return login name
    • posix_getpgid - Get process group id for job control
    • posix_getpgrp - Return the current process group identifier
    • posix_getpid - Return the current process identifier
    • posix_getppid - Return the parent process identifier
    • posix_getpwnam - Return info about a user by username
    • posix_getpwuid - Return info about a user by user id
    • posix_getrlimit - Return info about system resource limits
    • posix_getsid - Get the current sid of the process
    • posix_getuid - Return the real user ID of the current process
    • posix_get_last_error - Retrieve the error number set by the last posix function that failed
    • posix_initgroups - Calculate the group access list
    • posix_isatty - Determine if a file descriptor is an interactive terminal
    • posix_kill - Send a signal to a process
    • posix_mkfifo - Create a fifo special file (a named pipe)
    • posix_mknod - Create a special or ordinary file (POSIX.1)
    • posix_setegid - Set the effective GID of the current process
    • posix_seteuid - Set the effective UID of the current process
    • posix_setgid - Set the GID of the current process
    • posix_setpgid - Set process group id for job control
    • posix_setrlimit - Set system resource limits
    • posix_setsid - Make the current process a session leader
    • posix_setuid - Set the UID of the current process
    • posix_strerror - Retrieve the system error message associated with the given errno
    • posix_times - Get process times
    • posix_ttyname - Determine terminal device name
    • posix_uname - Get system name
    • pow - 거듭제곱 표현
    • preg_filter - Perform a regular expression search and replace
    • preg_grep - 패턴 매치한 부분의 배열을 반환
    • preg_last_error - Returns the error code of the last PCRE regex execution
    • preg_match - 정규표현식 매치를 수행
    • preg_match_all - 전역 정규 표현식 매치를 수행합니다
    • preg_quote - 정규 표현식 문자를 인용
    • preg_replace - 정규 표현식 검색과 치환을 수행
    • preg_replace_callback - 콜백을 이용한 정규 표현식 검색과 치환을 수행
    • preg_replace_callback_array - Perform a regular expression search and replace using callbacks
    • preg_split - 정규 표현식에 따라 문자열을 나눔
    • prev - 내부 배열 포인터를 후진
    • print - 문자열을 출력
    • printf - 형식화한 문자열을 출력
    • print_r - 변수에 관한 정보를 사람이 읽기 편하게 출력
    • proc_close - Close a process opened by proc_open and return the exit code of that process
    • proc_get_status - Get information about a process opened by proc_open
    • proc_nice - Change the priority of the current process
    • proc_open - Execute a command and open file pointers for input/output
    • proc_terminate - Kills a process opened by proc_open
    • property_exists - 객체나 클래스가 프로퍼티를 가졌는지 확인
    • pspell_add_to_personal - Add the word to a personal wordlist
    • pspell_add_to_session - Add the word to the wordlist in the current session
    • pspell_check - Check a word
    • pspell_clear_session - Clear the current session
    • pspell_config_create - Create a config used to open a dictionary
    • pspell_config_data_dir - location of language data files
    • pspell_config_dict_dir - Location of the main word list
    • pspell_config_ignore - Ignore words less than N characters long
    • pspell_config_mode - Change the mode number of suggestions returned
    • pspell_config_personal - Set a file that contains personal wordlist
    • pspell_config_repl - Set a file that contains replacement pairs
    • pspell_config_runtogether - Consider run-together words as valid compounds
    • pspell_config_save_repl - Determine whether to save a replacement pairs list along with the wordlist
    • pspell_new - Load a new dictionary
    • pspell_new_config - Load a new dictionary with settings based on a given config
    • pspell_new_personal - Load a new dictionary with personal wordlist
    • pspell_save_wordlist - Save the personal wordlist to a file
    • pspell_store_replacement - Store a replacement pair for a word
    • pspell_suggest - Suggest spellings of a word
    • ps_add_bookmark - Add bookmark to current page
    • ps_add_launchlink - Adds link which launches file
    • ps_add_locallink - Adds link to a page in the same document
    • ps_add_note - Adds note to current page
    • ps_add_pdflink - Adds link to a page in a second pdf document
    • ps_add_weblink - Adds link to a web location
    • ps_arc - Draws an arc counterclockwise
    • ps_arcn - Draws an arc clockwise
    • ps_begin_page - Start a new page
    • ps_begin_pattern - Start a new pattern
    • ps_begin_template - Start a new template
    • ps_circle - Draws a circle
    • ps_clip - Clips drawing to current path
    • ps_close - Closes a PostScript document
    • ps_closepath - Closes path
    • ps_closepath_stroke - Closes and strokes path
    • ps_close_image - Closes image and frees memory
    • ps_continue_text - Continue text in next line
    • ps_curveto - Draws a curve
    • ps_delete - Deletes all resources of a PostScript document
    • ps_end_page - End a page
    • ps_end_pattern - End a pattern
    • ps_end_template - End a template
    • ps_fill - Fills the current path
    • ps_fill_stroke - Fills and strokes the current path
    • ps_findfont - Loads a font
    • ps_get_buffer - Fetches the full buffer containig the generated PS data
    • ps_get_parameter - Gets certain parameters
    • ps_get_value - Gets certain values
    • ps_hyphenate - Hyphenates a word
    • ps_include_file - Reads an external file with raw PostScript code
    • ps_lineto - Draws a line
    • ps_makespotcolor - Create spot color
    • ps_moveto - Sets current point
    • ps_new - Creates a new PostScript document object
    • ps_open_file - Opens a file for output
    • ps_open_image - Reads an image for later placement
    • ps_open_image_file - Opens image from file
    • ps_open_memory_image - Takes an GD image and returns an image for placement in a PS document
    • ps_place_image - Places image on the page
    • ps_rect - Draws a rectangle
    • ps_restore - Restore previously save context
    • ps_rotate - Sets rotation factor
    • ps_save - Save current context
    • ps_scale - Sets scaling factor
    • ps_setcolor - Sets current color
    • ps_setdash - Sets appearance of a dashed line
    • ps_setflat - Sets flatness
    • ps_setfont - Sets font to use for following output
    • ps_setgray - Sets gray value
    • ps_setlinecap - Sets appearance of line ends
    • ps_setlinejoin - Sets how contected lines are joined
    • ps_setlinewidth - Sets width of a line
    • ps_setmiterlimit - Sets the miter limit
    • ps_setoverprintmode - Sets overprint mode
    • ps_setpolydash - Sets appearance of a dashed line
    • ps_set_border_color - Sets color of border for annotations
    • ps_set_border_dash - Sets length of dashes for border of annotations
    • ps_set_border_style - Sets border style of annotations
    • ps_set_info - Sets information fields of document
    • ps_set_parameter - Sets certain parameters
    • ps_set_text_pos - Sets position for text output
    • ps_set_value - Sets certain values
    • ps_shading - Creates a shading for later use
    • ps_shading_pattern - Creates a pattern based on a shading
    • ps_shfill - Fills an area with a shading
    • ps_show - Output text
    • ps_show2 - Output a text at current position
    • ps_show_boxed - Output text in a box
    • ps_show_xy - Output text at given position
    • ps_show_xy2 - Output text at position
    • ps_stringwidth - Gets width of a string
    • ps_string_geometry - Gets geometry of a string
    • ps_stroke - Draws the current path
    • ps_symbol - Output a glyph
    • ps_symbol_name - Gets name of a glyph
    • ps_symbol_width - Gets width of a glyph
    • ps_translate - Sets translation
    • putenv - Sets the value of an environment variable
    • px_close - Closes a paradox database
    • px_create_fp - Create a new paradox database
    • px_date2string - Converts a date into a string.
    • px_delete - Deletes resource of paradox database
    • px_delete_record - Deletes record from paradox database
    • px_get_field - Returns the specification of a single field
    • px_get_info - Return lots of information about a paradox file
    • px_get_parameter - Gets a parameter
    • px_get_record - Returns record of paradox database
    • px_get_schema - Returns the database schema
    • px_get_value - Gets a value
    • px_insert_record - Inserts record into paradox database
    • px_new - Create a new paradox object
    • px_numfields - Returns number of fields in a database
    • px_numrecords - Returns number of records in a database
    • px_open_fp - Open paradox database
    • px_put_record - Stores record into paradox database
    • px_retrieve_record - Returns record of paradox database
    • px_set_blob_file - Sets the file where blobs are read from
    • px_set_parameter - Sets a parameter
    • px_set_tablename - Sets the name of a table (deprecated)
    • px_set_targetencoding - Sets the encoding for character fields (deprecated)
    • px_set_value - Sets a value
    • px_timestamp2string - Converts the timestamp into a string.
    • px_update_record - Updates record in paradox database
  • q
    • QuickHashIntHash::add - This method adds a new entry to the hash
    • QuickHashIntHash::delete - This method deletes am entry from the hash
    • QuickHashIntHash::exists - This method checks whether a key is part of the hash
    • QuickHashIntHash::get - This method retrieves a value from the hash by its key
    • QuickHashIntHash::getSize - Returns the number of elements in the hash
    • QuickHashIntHash::loadFromFile - This factory method creates a hash from a file
    • QuickHashIntHash::loadFromString - This factory method creates a hash from a string
    • QuickHashIntHash::saveToFile - This method stores an in-memory hash to disk
    • QuickHashIntHash::saveToString - This method returns a serialized version of the hash
    • QuickHashIntHash::set - This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist
    • QuickHashIntHash::update - This method updates an entry in the hash with a new value
    • QuickHashIntHash::__construct - Creates a new QuickHashIntHash object
    • QuickHashIntSet::add - This method adds a new entry to the set
    • QuickHashIntSet::delete - This method deletes an entry from the set
    • QuickHashIntSet::exists - This method checks whether a key is part of the set
    • QuickHashIntSet::getSize - Returns the number of elements in the set
    • QuickHashIntSet::loadFromFile - This factory method creates a set from a file
    • QuickHashIntSet::loadFromString - This factory method creates a set from a string
    • QuickHashIntSet::saveToFile - This method stores an in-memory set to disk
    • QuickHashIntSet::saveToString - This method returns a serialized version of the set
    • QuickHashIntSet::__construct - Creates a new QuickHashIntSet object
    • QuickHashIntStringHash::add - This method adds a new entry to the hash
    • QuickHashIntStringHash::delete - This method deletes am entry from the hash
    • QuickHashIntStringHash::exists - This method checks whether a key is part of the hash
    • QuickHashIntStringHash::get - This method retrieves a value from the hash by its key
    • QuickHashIntStringHash::getSize - Returns the number of elements in the hash
    • QuickHashIntStringHash::loadFromFile - This factory method creates a hash from a file
    • QuickHashIntStringHash::loadFromString - This factory method creates a hash from a string
    • QuickHashIntStringHash::saveToFile - This method stores an in-memory hash to disk
    • QuickHashIntStringHash::saveToString - This method returns a serialized version of the hash
    • QuickHashIntStringHash::set - This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist
    • QuickHashIntStringHash::update - This method updates an entry in the hash with a new value
    • QuickHashIntStringHash::__construct - Creates a new QuickHashIntStringHash object
    • QuickHashStringIntHash::add - This method adds a new entry to the hash
    • QuickHashStringIntHash::delete - This method deletes am entry from the hash
    • QuickHashStringIntHash::exists - This method checks whether a key is part of the hash
    • QuickHashStringIntHash::get - This method retrieves a value from the hash by its key
    • QuickHashStringIntHash::getSize - Returns the number of elements in the hash
    • QuickHashStringIntHash::loadFromFile - This factory method creates a hash from a file
    • QuickHashStringIntHash::loadFromString - This factory method creates a hash from a string
    • QuickHashStringIntHash::saveToFile - This method stores an in-memory hash to disk
    • QuickHashStringIntHash::saveToString - This method returns a serialized version of the hash
    • QuickHashStringIntHash::set - This method updates an entry in the hash with a new value, or adds a new one if the entry doesn't exist
    • QuickHashStringIntHash::update - This method updates an entry in the hash with a new value
    • QuickHashStringIntHash::__construct - Creates a new QuickHashStringIntHash object
    • quoted_printable_decode - 인용된 인쇄용 문자열을 8비트 문자열로 변환
    • quoted_printable_encode - Convert a 8 bit string to a quoted-printable string
    • quotemeta - 메타 문자를 인용
  • r
    • rad2deg - 라디안 수를 각도로 변환
    • radius_acct_open - Creates a Radius handle for accounting
    • radius_add_server - Adds a server
    • radius_auth_open - Creates a Radius handle for authentication
    • radius_close - Frees all ressources
    • radius_config - Causes the library to read the given configuration file
    • radius_create_request - Create accounting or authentication request
    • radius_cvt_addr - Converts raw data to IP-Address
    • radius_cvt_int - Converts raw data to integer
    • radius_cvt_string - Converts raw data to string
    • radius_demangle - Demangles data
    • radius_demangle_mppe_key - Derives mppe-keys from mangled data
    • radius_get_attr - Extracts an attribute
    • radius_get_tagged_attr_data - Extracts the data from a tagged attribute
    • radius_get_tagged_attr_tag - Extracts the tag from a tagged attribute
    • radius_get_vendor_attr - Extracts a vendor specific attribute
    • radius_put_addr - Attaches an IP address attribute
    • radius_put_attr - Attaches a binary attribute
    • radius_put_int - Attaches an integer attribute
    • radius_put_string - Attaches a string attribute
    • radius_put_vendor_addr - Attaches a vendor specific IP address attribute
    • radius_put_vendor_attr - Attaches a vendor specific binary attribute
    • radius_put_vendor_int - Attaches a vendor specific integer attribute
    • radius_put_vendor_string - Attaches a vendor specific string attribute
    • radius_request_authenticator - Returns the request authenticator
    • radius_salt_encrypt_attr - Salt-encrypts a value
    • radius_send_request - Sends the request and waites for a reply
    • radius_server_secret - Returns the shared secret
    • radius_strerror - Returns an error message
    • rand - 정수 난수를 생성
    • random_bytes - Generates cryptographically secure pseudo-random bytes
    • random_int - Generates cryptographically secure pseudo-random integers
    • range - 원소의 범위를 가지는 배열 생성
    • rar:// - RAR
    • RarArchive::close - Close RAR archive and free all resources
    • RarArchive::getComment - Get comment text from the RAR archive
    • RarArchive::getEntries - Get full list of entries from the RAR archive
    • RarArchive::getEntry - Get entry object from the RAR archive
    • RarArchive::isBroken - Test whether an archive is broken (incomplete)
    • RarArchive::isSolid - Check whether the RAR archive is solid
    • RarArchive::open - Open RAR archive
    • RarArchive::setAllowBroken - Whether opening broken archives is allowed
    • RarArchive::__toString - Get text representation
    • RarEntry::extract - Extract entry from the archive
    • RarEntry::getAttr - Get attributes of the entry
    • RarEntry::getCrc - Get CRC of the entry
    • RarEntry::getFileTime - Get entry last modification time
    • RarEntry::getHostOs - Get entry host OS
    • RarEntry::getMethod - Get pack method of the entry
    • RarEntry::getName - Get name of the entry
    • RarEntry::getPackedSize - Get packed size of the entry
    • RarEntry::getStream - Get file handler for entry
    • RarEntry::getUnpackedSize - Get unpacked size of the entry
    • RarEntry::getVersion - Get minimum version of RAR program required to unpack the entry
    • RarEntry::isDirectory - Test whether an entry represents a directory
    • RarEntry::isEncrypted - Test whether an entry is encrypted
    • RarEntry::__toString - Get text representation of entry
    • RarException::isUsingExceptions - Check whether error handling with exceptions is in use
    • RarException::setUsingExceptions - Activate and deactivate error handling with exceptions
    • rar_wrapper_cache_stats - Cache hits and misses for the URL wrapper
    • rawurldecode - URL 인코드된 문자열을 디코드
    • rawurlencode - RFC 1738에 따른 URL 인코드
    • readdir - 디렉토리 핸들에서 엔트리를 읽습니다
    • readfile - Outputs a file
    • readgzfile - Output a gz-file
    • readline - Reads a line
    • readline_add_history - Adds a line to the history
    • readline_callback_handler_install - Initializes the readline callback interface and terminal, prints the prompt and returns immediately
    • readline_callback_handler_remove - Removes a previously installed callback handler and restores terminal settings
    • readline_callback_read_char - Reads a character and informs the readline callback interface when a line is received
    • readline_clear_history - Clears the history
    • readline_completion_function - Registers a completion function
    • readline_info - Gets/sets various internal readline variables
    • readline_list_history - Lists the history
    • readline_on_new_line - Inform readline that the cursor has moved to a new line
    • readline_read_history - Reads the history
    • readline_redisplay - Redraws the display
    • readline_write_history - Writes the history
    • readlink - Returns the target of a symbolic link
    • read_exif_data - 별칭: exif_read_data
    • realpath - Returns canonicalized absolute pathname
    • realpath_cache_get - Get realpath cache entries
    • realpath_cache_size - Get realpath cache size
    • recode - 별칭: recode_string
    • recode_file - Recode from file to file according to recode request
    • recode_string - Recode a string according to a recode request
    • RecursiveArrayIterator::getChildren - Returns an iterator for the current entry if it is an array or an object.
    • RecursiveArrayIterator::hasChildren - Returns whether current entry is an array or an object.
    • RecursiveCachingIterator::getChildren - Return the inner iterator's children as a RecursiveCachingIterator
    • RecursiveCachingIterator::hasChildren - Check whether the current element of the inner iterator has children
    • RecursiveCachingIterator::__construct - Construct
    • RecursiveCallbackFilterIterator::getChildren - Return the inner iterator's children contained in a RecursiveCallbackFilterIterator
    • RecursiveCallbackFilterIterator::hasChildren - Check whether the inner iterator's current element has children
    • RecursiveCallbackFilterIterator::__construct - Create a RecursiveCallbackFilterIterator from a RecursiveIterator
    • RecursiveDirectoryIterator::getChildren - Returns an iterator for the current entry if it is a directory
    • RecursiveDirectoryIterator::getSubPath - Get sub path
    • RecursiveDirectoryIterator::getSubPathname - Get sub path and name
    • RecursiveDirectoryIterator::hasChildren - Returns whether current entry is a directory and not '.' or '..'
    • RecursiveDirectoryIterator::key - Return path and filename of current dir entry
    • RecursiveDirectoryIterator::next - Move to next entry
    • RecursiveDirectoryIterator::rewind - Rewind dir back to the start
    • RecursiveDirectoryIterator::__construct - Constructs a RecursiveDirectoryIterator
    • RecursiveFilterIterator::getChildren - Return the inner iterator's children contained in a RecursiveFilterIterator
    • RecursiveFilterIterator::hasChildren - Check whether the inner iterator's current element has children
    • RecursiveFilterIterator::__construct - Create a RecursiveFilterIterator from a RecursiveIterator
    • RecursiveIterator::getChildren - Returns an iterator for the current entry.
    • RecursiveIterator::hasChildren - Returns if an iterator can be created fot the current entry.
    • RecursiveIteratorIterator::beginChildren - Begin children
    • RecursiveIteratorIterator::beginIteration - Begin Iteration
    • RecursiveIteratorIterator::callGetChildren - Get children
    • RecursiveIteratorIterator::callHasChildren - Has children
    • RecursiveIteratorIterator::current - Access the current element value
    • RecursiveIteratorIterator::endChildren - End children
    • RecursiveIteratorIterator::endIteration - End Iteration
    • RecursiveIteratorIterator::getDepth - Get the current depth of the recursive iteration
    • RecursiveIteratorIterator::getInnerIterator - Get inner iterator
    • RecursiveIteratorIterator::getMaxDepth - Get max depth
    • RecursiveIteratorIterator::getSubIterator - The current active sub iterator
    • RecursiveIteratorIterator::key - Access the current key
    • RecursiveIteratorIterator::next - Move forward to the next element
    • RecursiveIteratorIterator::nextElement - Next element
    • RecursiveIteratorIterator::rewind - Rewind the iterator to the first element of the top level inner iterator
    • RecursiveIteratorIterator::setMaxDepth - Set max depth
    • RecursiveIteratorIterator::valid - Check whether the current position is valid
    • RecursiveIteratorIterator::__construct - Construct a RecursiveIteratorIterator
    • RecursiveRegexIterator::getChildren - Returns an iterator for the current entry.
    • RecursiveRegexIterator::hasChildren - Returns whether an iterator can be obtained for the current entry.
    • RecursiveRegexIterator::__construct - Creates a new RecursiveRegexIterator.
    • RecursiveTreeIterator::beginChildren - Begin children
    • RecursiveTreeIterator::beginIteration - Begin iteration
    • RecursiveTreeIterator::callGetChildren - Get children
    • RecursiveTreeIterator::callHasChildren - Has children
    • RecursiveTreeIterator::current - Get current element
    • RecursiveTreeIterator::endChildren - End children
    • RecursiveTreeIterator::endIteration - End iteration
    • RecursiveTreeIterator::getEntry - Get current entry
    • RecursiveTreeIterator::getPostfix - Get the postfix
    • RecursiveTreeIterator::getPrefix - Get the prefix
    • RecursiveTreeIterator::key - Get the key of the current element
    • RecursiveTreeIterator::next - Move to next element
    • RecursiveTreeIterator::nextElement - Next element
    • RecursiveTreeIterator::rewind - Rewind iterator
    • RecursiveTreeIterator::setPrefixPart - Set a part of the prefix
    • RecursiveTreeIterator::valid - Check validity
    • RecursiveTreeIterator::__construct - Construct a RecursiveTreeIterator
    • Reflection::export - Exports
    • Reflection::getModifierNames - Gets modifier names
    • ReflectionClass::export - Exports a class
    • ReflectionClass::getConstant - Gets defined constant
    • ReflectionClass::getConstants - Gets constants
    • ReflectionClass::getConstructor - Gets the constructor of the class
    • ReflectionClass::getDefaultProperties - Gets default properties
    • ReflectionClass::getDocComment - Gets doc comments
    • ReflectionClass::getEndLine - Gets end line
    • ReflectionClass::getExtension - Gets a ReflectionExtension object for the extension which defined the class
    • ReflectionClass::getExtensionName - Gets the name of the extension which defined the class
    • ReflectionClass::getFileName - Gets the filename of the file in which the class has been defined
    • ReflectionClass::getInterfaceNames - Gets the interface names
    • ReflectionClass::getInterfaces - Gets the interfaces
    • ReflectionClass::getMethod - Gets a ReflectionMethod for a class method.
    • ReflectionClass::getMethods - Gets an array of methods
    • ReflectionClass::getModifiers - Gets modifiers
    • ReflectionClass::getName - Gets class name
    • ReflectionClass::getNamespaceName - Gets namespace name
    • ReflectionClass::getParentClass - Gets parent class
    • ReflectionClass::getProperties - Gets properties
    • ReflectionClass::getProperty - Gets a ReflectionProperty for a class's property
    • ReflectionClass::getShortName - Gets short name
    • ReflectionClass::getStartLine - Gets starting line number
    • ReflectionClass::getStaticProperties - Gets static properties
    • ReflectionClass::getStaticPropertyValue - Gets static property value
    • ReflectionClass::getTraitAliases - Returns an array of trait aliases
    • ReflectionClass::getTraitNames - Returns an array of names of traits used by this class
    • ReflectionClass::getTraits - Returns an array of traits used by this class
    • ReflectionClass::hasConstant - Checks if constant is defined
    • ReflectionClass::hasMethod - Checks if method is defined
    • ReflectionClass::hasProperty - Checks if property is defined
    • ReflectionClass::implementsInterface - Implements interface
    • ReflectionClass::inNamespace - Checks if in namespace
    • ReflectionClass::isAbstract - Checks if class is abstract
    • ReflectionClass::isAnonymous - Checks if class is anonymous
    • ReflectionClass::isCloneable - Returns whether this class is cloneable
    • ReflectionClass::isFinal - Checks if class is final
    • ReflectionClass::isInstance - Checks class for instance
    • ReflectionClass::isInstantiable - Checks if the class is instantiable
    • ReflectionClass::isInterface - Checks if the class is an interface
    • ReflectionClass::isInternal - Checks if class is defined internally by an extension, or the core
    • ReflectionClass::isIterateable - Checks if iterateable
    • ReflectionClass::isSubclassOf - Checks if a subclass
    • ReflectionClass::isTrait - Returns whether this is a trait
    • ReflectionClass::isUserDefined - Checks if user defined
    • ReflectionClass::newInstance - Creates a new class instance from given arguments.
    • ReflectionClass::newInstanceArgs - Creates a new class instance from given arguments.
    • ReflectionClass::newInstanceWithoutConstructor - Creates a new class instance without invoking the constructor.
    • ReflectionClass::setStaticPropertyValue - Sets static property value
    • ReflectionClass::__construct - Constructs a ReflectionClass
    • ReflectionClass::__toString - Returns the string representation of the ReflectionClass object.
    • ReflectionExtension::export - Export
    • ReflectionExtension::getClasses - Gets classes
    • ReflectionExtension::getClassNames - Gets class names
    • ReflectionExtension::getConstants - Gets constants
    • ReflectionExtension::getDependencies - Gets dependencies
    • ReflectionExtension::getFunctions - Gets extension functions
    • ReflectionExtension::getINIEntries - Gets extension ini entries
    • ReflectionExtension::getName - Gets extension name
    • ReflectionExtension::getVersion - Gets extension version
    • ReflectionExtension::info - Print extension info
    • ReflectionExtension::isPersistent - Returns whether this extension is persistent
    • ReflectionExtension::isTemporary - Returns whether this extension is temporary
    • ReflectionExtension::__clone - Clones
    • ReflectionExtension::__construct - Constructs a ReflectionExtension
    • ReflectionExtension::__toString - To string
    • ReflectionFunction::export - Exports function
    • ReflectionFunction::getClosure - Returns a dynamically created closure for the function
    • ReflectionFunction::invoke - Invokes function
    • ReflectionFunction::invokeArgs - Invokes function args
    • ReflectionFunction::isDisabled - Checks if function is disabled
    • ReflectionFunction::__construct - Constructs a ReflectionFunction object
    • ReflectionFunction::__toString - To string
    • ReflectionFunctionAbstract::getClosureScopeClass - Returns the scope associated to the closure
    • ReflectionFunctionAbstract::getClosureThis - Returns this pointer bound to closure
    • ReflectionFunctionAbstract::getDocComment - Gets doc comment
    • ReflectionFunctionAbstract::getEndLine - Gets end line number
    • ReflectionFunctionAbstract::getExtension - Gets extension info
    • ReflectionFunctionAbstract::getExtensionName - Gets extension name
    • ReflectionFunctionAbstract::getFileName - Gets file name
    • ReflectionFunctionAbstract::getName - Gets function name
    • ReflectionFunctionAbstract::getNamespaceName - Gets namespace name
    • ReflectionFunctionAbstract::getNumberOfParameters - Gets number of parameters
    • ReflectionFunctionAbstract::getNumberOfRequiredParameters - Gets number of required parameters
    • ReflectionFunctionAbstract::getParameters - Gets parameters
    • ReflectionFunctionAbstract::getReturnType - Gets the specified return type of a function
    • ReflectionFunctionAbstract::getShortName - Gets function short name
    • ReflectionFunctionAbstract::getStartLine - Gets starting line number
    • ReflectionFunctionAbstract::getStaticVariables - Gets static variables
    • ReflectionFunctionAbstract::hasReturnType - Checks if the function has a specified return type
    • ReflectionFunctionAbstract::inNamespace - Checks if function in namespace
    • ReflectionFunctionAbstract::isClosure - Checks if closure
    • ReflectionFunctionAbstract::isDeprecated - Checks if deprecated
    • ReflectionFunctionAbstract::isGenerator - Returns whether this function is a generator
    • ReflectionFunctionAbstract::isInternal - Checks if is internal
    • ReflectionFunctionAbstract::isUserDefined - Checks if user defined
    • ReflectionFunctionAbstract::isVariadic - Checks if the function is variadic
    • ReflectionFunctionAbstract::returnsReference - Checks if returns reference
    • ReflectionFunctionAbstract::__clone - Clones function
    • ReflectionFunctionAbstract::__toString - To string
    • ReflectionGenerator::getExecutingFile - Gets the file name of the currently executing generator
    • ReflectionGenerator::getExecutingGenerator - Gets the executing Generator object
    • ReflectionGenerator::getExecutingLine - Gets the currently executing line of the generator
    • ReflectionGenerator::getFunction - Gets the function name of the generator
    • ReflectionGenerator::getThis - Gets the $this value of the generator
    • ReflectionGenerator::getTrace - Gets the trace of the executing generator
    • ReflectionGenerator::__construct - Constructs a ReflectionGenerator object
    • ReflectionMethod::export - Export a reflection method.
    • ReflectionMethod::getClosure - Returns a dynamically created closure for the method
    • ReflectionMethod::getDeclaringClass - Gets declaring class for the reflected method.
    • ReflectionMethod::getModifiers - Gets the method modifiers
    • ReflectionMethod::getPrototype - Gets the method prototype (if there is one).
    • ReflectionMethod::invoke - Invoke
    • ReflectionMethod::invokeArgs - Invoke args
    • ReflectionMethod::isAbstract - Checks if method is abstract
    • ReflectionMethod::isConstructor - Checks if method is a constructor
    • ReflectionMethod::isDestructor - Checks if method is a destructor
    • ReflectionMethod::isFinal - Checks if method is final
    • ReflectionMethod::isPrivate - Checks if method is private
    • ReflectionMethod::isProtected - Checks if method is protected
    • ReflectionMethod::isPublic - Checks if method is public
    • ReflectionMethod::isStatic - Checks if method is static
    • ReflectionMethod::setAccessible - Set method accessibility
    • ReflectionMethod::__construct - Constructs a ReflectionMethod
    • ReflectionMethod::__toString - Returns the string representation of the Reflection method object.
    • ReflectionObject::export - Export
    • ReflectionObject::__construct - Constructs a ReflectionObject
    • ReflectionParameter::allowsNull - Checks if null is allowed
    • ReflectionParameter::canBePassedByValue - Returns whether this parameter can be passed by value
    • ReflectionParameter::export - Exports
    • ReflectionParameter::getClass - Get the type hinted class
    • ReflectionParameter::getDeclaringClass - Gets declaring class
    • ReflectionParameter::getDeclaringFunction - Gets declaring function
    • ReflectionParameter::getDefaultValue - Gets default parameter value
    • ReflectionParameter::getDefaultValueConstantName - Returns the default value's constant name if default value is constant or null
    • ReflectionParameter::getName - Gets parameter name
    • ReflectionParameter::getPosition - Gets parameter position
    • ReflectionParameter::getType - Gets a parameter's type
    • ReflectionParameter::hasType - Checks if parameter has a type
    • ReflectionParameter::isArray - Checks if parameter expects an array
    • ReflectionParameter::isCallable - Returns whether parameter MUST be callable
    • ReflectionParameter::isDefaultValueAvailable - Checks if a default value is available
    • ReflectionParameter::isDefaultValueConstant - Returns whether the default value of this parameter is constant
    • ReflectionParameter::isOptional - Checks if optional
    • ReflectionParameter::isPassedByReference - Checks if passed by reference
    • ReflectionParameter::isVariadic - Checks if the parameter is variadic
    • ReflectionParameter::__clone - Clone
    • ReflectionParameter::__construct - Construct
    • ReflectionParameter::__toString - To string
    • ReflectionProperty::export - Export
    • ReflectionProperty::getDeclaringClass - Gets declaring class
    • ReflectionProperty::getDocComment - Gets the property doc comment
    • ReflectionProperty::getModifiers - Gets modifiers
    • ReflectionProperty::getName - Gets property name
    • ReflectionProperty::getValue - Gets value
    • ReflectionProperty::isDefault - Checks if property is a default property
    • ReflectionProperty::isPrivate - Checks if property is private
    • ReflectionProperty::isProtected - Checks if property is protected
    • ReflectionProperty::isPublic - Checks if property is public
    • ReflectionProperty::isStatic - Checks if property is static
    • ReflectionProperty::setAccessible - Set property accessibility
    • ReflectionProperty::setValue - Set property value
    • ReflectionProperty::__clone - Clone
    • ReflectionProperty::__construct - Construct a ReflectionProperty object
    • ReflectionProperty::__toString - To string
    • ReflectionType::allowsNull - Checks if null is allowed
    • ReflectionType::isBuiltin - Checks if it is a built-in type
    • ReflectionType::__toString - To string
    • ReflectionZendExtension::export - Export
    • ReflectionZendExtension::getAuthor - Gets author
    • ReflectionZendExtension::getCopyright - Gets copyright
    • ReflectionZendExtension::getName - Gets name
    • ReflectionZendExtension::getURL - Gets URL
    • ReflectionZendExtension::getVersion - Gets version
    • ReflectionZendExtension::__clone - Clone handler
    • ReflectionZendExtension::__construct - Constructor
    • ReflectionZendExtension::__toString - To string handler
    • Reflector::export - Exports
    • Reflector::__toString - To string
    • RegexIterator::accept - Get accept status
    • RegexIterator::getFlags - Get flags
    • RegexIterator::getMode - Returns operation mode.
    • RegexIterator::getPregFlags - Returns the regular expression flags.
    • RegexIterator::getRegex - Returns current regular expression
    • RegexIterator::setFlags - Sets the flags.
    • RegexIterator::setMode - Sets the operation mode.
    • RegexIterator::setPregFlags - Sets the regular expression flags.
    • RegexIterator::__construct - Create a new RegexIterator
    • register_shutdown_function - Register a function for execution on shutdown
    • register_tick_function - Register a function for execution on each tick
    • rename - Renames a file or directory
    • rename_function - Renames orig_name to new_name in the global function table
    • reset - 배열의 내부 포인터를 첫 원소로 설정
    • ResourceBundle::count - Get number of elements in the bundle
    • ResourceBundle::create - Create a resource bundle
    • ResourceBundle::get - Get data from the bundle
    • ResourceBundle::getErrorCode - Get bundle's last error code.
    • ResourceBundle::getErrorMessage - Get bundle's last error message.
    • ResourceBundle::getLocales - Get supported locales
    • restore_error_handler - Restores the previous error handler function
    • restore_exception_handler - Restores the previously defined exception handler function
    • restore_include_path - Restores the value of the include_path configuration option
    • rewind - Rewind the position of a file pointer
    • rewinddir - 디렉토리 핸들을 되감습니다
    • rmdir - Removes directory
    • round - 소수 반올림
    • rpm_close - Closes an RPM file
    • rpm_get_tag - Retrieves a header tag from an RPM file
    • rpm_is_valid - Tests a filename for validity as an RPM file
    • rpm_open - Opens an RPM file
    • rpm_version - Returns a string representing the current version of the rpmreader extension
    • RRDCreator::addArchive - Adds RRA - archive of data values for each data source.
    • RRDCreator::addDataSource - Adds data source definition for RRD database.
    • RRDCreator::save - Saves the RRD database to a file
    • RRDCreator::__construct - Creates new RRDCreator instance
    • rrdc_disconnect - Close any outstanding connection to rrd caching daemon
    • RRDGraph::save - Saves the result of query into image
    • RRDGraph::saveVerbose - Saves the RRD database query into image and returns the verbose information about generated graph.
    • RRDGraph::setOptions - Sets the options for rrd graph export
    • RRDGraph::__construct - Creates new RRDGraph instance
    • RRDUpdater::update - Update the RRD database file
    • RRDUpdater::__construct - Creates new RRDUpdater instance
    • rrd_create - Creates rrd database file
    • rrd_error - Gets latest error message.
    • rrd_fetch - Fetch the data for graph as array.
    • rrd_first - Gets the timestamp of the first sample from rrd file.
    • rrd_graph - Creates image from a data.
    • rrd_info - Gets information about rrd file
    • rrd_last - Gets unix timestamp of the last sample.
    • rrd_lastupdate - Gets information about last updated data.
    • rrd_restore - Restores the RRD file from XML dump.
    • rrd_tune - Tunes some RRD database file header options.
    • rrd_update - Updates the RRD database.
    • rrd_version - Gets information about underlying rrdtool library
    • rrd_xport - Exports the information about RRD database.
    • rsort - 역순으로 배열 정렬
    • rtrim - 문자열 마지막의 공백(이나 다른 문자)을 제거
    • runkit_class_adopt - Convert a base class to an inherited class, add ancestral methods when appropriate
    • runkit_class_emancipate - Convert an inherited class to a base class, removes any method whose scope is ancestral
    • runkit_constant_add - Similar to define(), but allows defining in class definitions as well
    • runkit_constant_redefine - Redefine an already defined constant
    • runkit_constant_remove - Remove/Delete an already defined constant
    • runkit_function_add - Add a new function, similar to create_function
    • runkit_function_copy - Copy a function to a new function name
    • runkit_function_redefine - Replace a function definition with a new implementation
    • runkit_function_remove - Remove a function definition
    • runkit_function_rename - Change a function's name
    • runkit_import - Process a PHP file importing function and class definitions, overwriting where appropriate
    • runkit_lint - Check the PHP syntax of the specified php code
    • runkit_lint_file - Check the PHP syntax of the specified file
    • runkit_method_add - Dynamically adds a new method to a given class
    • runkit_method_copy - Copies a method from class to another
    • runkit_method_redefine - Dynamically changes the code of the given method
    • runkit_method_remove - Dynamically removes the given method
    • runkit_method_rename - Dynamically changes the name of the given method
    • runkit_return_value_used - Determines if the current functions return value will be used
    • Runkit_Sandbox - Runkit Sandbox Class -- PHP Virtual Machine
    • runkit_sandbox_output_handler - Specify a function to capture and/or process output from a runkit sandbox
    • Runkit_Sandbox_Parent - Runkit Anti-Sandbox Class
    • runkit_superglobals - Return numerically indexed array of registered superglobals
  • s
    • SAMConnection::commit - Commits (completes) the current unit of work.
    • SAMConnection::connect - Establishes a connection to a Messaging Server
    • SAMConnection::disconnect - Disconnects from a Messaging Server
    • SAMConnection::errno - Contains the unique numeric error code of the last executed SAM operation.
    • SAMConnection::error - Contains the text description of the last failed SAM operation.
    • SAMConnection::isConnected - Queries whether a connection is established to a Messaging Server
    • SAMConnection::peek - Read a message from a queue without removing it from the queue.
    • SAMConnection::peekAll - Read one or more messages from a queue without removing it from the queue.
    • SAMConnection::receive - Receive a message from a queue or subscription.
    • SAMConnection::remove - Remove a message from a queue.
    • SAMConnection::rollback - Cancels (rolls back) an in-flight unit of work.
    • SAMConnection::send - Send a message to a queue or publish an item to a topic.
    • SAMConnection::setDebug - Turn on or off additional debugging output.
    • SAMConnection::subscribe - Create a subscription to a specified topic.
    • SAMConnection::unsubscribe - Cancel a subscription to a specified topic.
    • SAMConnection::__construct - Creates a new connection to a Messaging Server
    • SAMMessage::body - The body of the message.
    • SAMMessage::header - The header properties of the message.
    • SAMMessage::__construct - Creates a new Message object
    • SCA::createDataObject - create an SDO
    • SCA::getService - Obtain a proxy for a service
    • scandir - 지정한 경로 안의 파일과 디렉토리 목록
    • SCA_LocalProxy::createDataObject - create an SDO
    • SCA_SoapProxy::createDataObject - create an SDO
    • SDO_DAS_ChangeSummary::beginLogging - Begin change logging
    • SDO_DAS_ChangeSummary::endLogging - End change logging
    • SDO_DAS_ChangeSummary::getChangedDataObjects - Get the changed data objects from a change summary
    • SDO_DAS_ChangeSummary::getChangeType - Get the type of change made to an SDO_DataObject
    • SDO_DAS_ChangeSummary::getOldContainer - Get the old container for a deleted SDO_DataObject
    • SDO_DAS_ChangeSummary::getOldValues - Get the old values for a given changed SDO_DataObject
    • SDO_DAS_ChangeSummary::isLogging - Test to see whether change logging is switched on
    • SDO_DAS_DataFactory::addPropertyToType - Adds a property to a type
    • SDO_DAS_DataFactory::addType - Add a new type to a model
    • SDO_DAS_DataFactory::getDataFactory - Get a data factory instance
    • SDO_DAS_DataObject::getChangeSummary - Get a data object's change summary
    • SDO_DAS_Relational::applyChanges - Applies the changes made to a data graph back to the database.
    • SDO_DAS_Relational::createRootDataObject - Returns the special root object in an otherwise empty data graph. Used when creating a data graph from scratch.
    • SDO_DAS_Relational::executePreparedQuery - Executes an SQL query passed as a prepared statement, with a list of values to substitute for placeholders, and return the results as a normalised data graph.
    • SDO_DAS_Relational::executeQuery - Executes a given SQL query against a relational database and returns the results as a normalised data graph.
    • SDO_DAS_Relational::__construct - Creates an instance of a Relational Data Access Service
    • SDO_DAS_Setting::getListIndex - Get the list index for a changed many-valued property
    • SDO_DAS_Setting::getPropertyIndex - Get the property index for a changed property
    • SDO_DAS_Setting::getPropertyName - Get the property name for a changed property
    • SDO_DAS_Setting::getValue - Get the old value for the changed property
    • SDO_DAS_Setting::isSet - Test whether a property was set prior to being modified
    • SDO_DAS_XML::addTypes - To load a second or subsequent schema file to a SDO_DAS_XML object
    • SDO_DAS_XML::create - To create SDO_DAS_XML object for a given schema file
    • SDO_DAS_XML::createDataObject - Creates SDO_DataObject for a given namespace URI and type name
    • SDO_DAS_XML::createDocument - Creates an XML Document object from scratch, without the need to load a document from a file or string.
    • SDO_DAS_XML::loadFile - Returns SDO_DAS_XML_Document object for a given path to xml instance document
    • SDO_DAS_XML::loadString - Returns SDO_DAS_XML_Document for a given xml instance string
    • SDO_DAS_XML::saveFile - Saves the SDO_DAS_XML_Document object to a file
    • SDO_DAS_XML::saveString - Saves the SDO_DAS_XML_Document object to a string
    • SDO_DAS_XML_Document::getRootDataObject - Returns the root SDO_DataObject
    • SDO_DAS_XML_Document::getRootElementName - Returns root element's name
    • SDO_DAS_XML_Document::getRootElementURI - Returns root element's URI string
    • SDO_DAS_XML_Document::setEncoding - Sets the given string as encoding
    • SDO_DAS_XML_Document::setXMLDeclaration - Sets the xml declaration
    • SDO_DAS_XML_Document::setXMLVersion - Sets the given string as xml version
    • SDO_DataFactory::create - Create an SDO_DataObject
    • SDO_DataObject::clear - Clear an SDO_DataObject's properties
    • SDO_DataObject::createDataObject - Create a child SDO_DataObject
    • SDO_DataObject::getContainer - Get a data object's container
    • SDO_DataObject::getSequence - Get the sequence for a data object
    • SDO_DataObject::getTypeName - Return the name of the type for a data object.
    • SDO_DataObject::getTypeNamespaceURI - Return the namespace URI of the type for a data object.
    • SDO_Exception::getCause - Get the cause of the exception.
    • SDO_List::insert - Insert into a list
    • SDO_Model_Property::getContainingType - Get the SDO_Model_Type which contains this property
    • SDO_Model_Property::getDefault - Get the default value for the property
    • SDO_Model_Property::getName - Get the name of the SDO_Model_Property
    • SDO_Model_Property::getType - Get the SDO_Model_Type of the property
    • SDO_Model_Property::isContainment - Test to see if the property defines a containment relationship
    • SDO_Model_Property::isMany - Test to see if the property is many-valued
    • SDO_Model_ReflectionDataObject::export - Get a string describing the SDO_DataObject.
    • SDO_Model_ReflectionDataObject::getContainmentProperty - Get the property which defines the containment relationship to the data object
    • SDO_Model_ReflectionDataObject::getInstanceProperties - Get the instance properties of the SDO_DataObject
    • SDO_Model_ReflectionDataObject::getType - Get the SDO_Model_Type for the SDO_DataObject
    • SDO_Model_ReflectionDataObject::__construct - Construct an SDO_Model_ReflectionDataObject
    • SDO_Model_Type::getBaseType - Get the base type for this type
    • SDO_Model_Type::getName - Get the name of the type
    • SDO_Model_Type::getNamespaceURI - Get the namespace URI of the type
    • SDO_Model_Type::getProperties - Get the SDO_Model_Property objects defined for the type
    • SDO_Model_Type::getProperty - Get an SDO_Model_Property of the type
    • SDO_Model_Type::isAbstractType - Test to see if this SDO_Model_Type is an abstract data type
    • SDO_Model_Type::isDataType - Test to see if this SDO_Model_Type is a primitive data type
    • SDO_Model_Type::isInstance - Test for an SDO_DataObject being an instance of this SDO_Model_Type
    • SDO_Model_Type::isOpenType - Test to see if this type is an open type
    • SDO_Model_Type::isSequencedType - Test to see if this is a sequenced type
    • SDO_Sequence::getProperty - Return the property for the specified sequence index.
    • SDO_Sequence::insert - Insert into a sequence
    • SDO_Sequence::move - Move an item to another sequence position
    • SeekableIterator::seek - Seeks to a position
    • sem_acquire - Acquire a semaphore
    • sem_get - Get a semaphore id
    • sem_release - Release a semaphore
    • sem_remove - Remove a semaphore
    • Serializable::serialize - String representation of object
    • Serializable::unserialize - Constructs the object
    • serialize - 값의 저장 표현을 생성
    • SessionHandler::close - Close the session
    • SessionHandler::create_sid - Return a new session ID
    • SessionHandler::destroy - Destroy a session
    • SessionHandler::gc - Cleanup old sessions
    • SessionHandler::open - Initialize session
    • SessionHandler::read - Read session data
    • SessionHandler::write - Write session data
    • SessionHandlerInterface::close - Close the session
    • SessionHandlerInterface::destroy - Destroy a session
    • SessionHandlerInterface::gc - Cleanup old sessions
    • SessionHandlerInterface::open - Initialize session
    • SessionHandlerInterface::read - Read session data
    • SessionHandlerInterface::write - Write session data
    • session_abort - Discard session array changes and finish session
    • session_cache_expire - 현재 캐시 만료를 반환
    • session_cache_limiter - 현재 캐시 한정을 얻거나 설정
    • session_commit - 별칭: session_write_close
    • session_create_id - Create new session id
    • session_decode - 문자열에서 세션 데이터를 해독
    • session_destroy - 세션에 등록된 모든 데이터 파괴
    • session_encode - 현재 세션 데이터를 문자열로 인코드
    • session_gc - Perform session data garbage collection
    • session_get_cookie_params - 세션 쿠키 인수를 얻습니다
    • session_id - 현재 세션 id를 얻거나 설정
    • session_is_registered - 전역 변수가 세션에 등록되었는지 확인
    • session_module_name - 현재 세션 모듈을 얻거나 설정
    • session_name - 현재 세션 이름을 얻거나 설정
    • session_pgsql_add_error - Increments error counts and sets last error message
    • session_pgsql_get_error - Returns number of errors and last error message
    • session_pgsql_get_field - Get custom field value
    • session_pgsql_reset - Reset connection to session database servers
    • session_pgsql_set_field - Set custom field value
    • session_pgsql_status - Get current save handler status
    • session_regenerate_id - 현재 세션 id를 새로 생성해서 갱신
    • session_register - 하나 이상의 전역 변수를 현재 세션에 등록
    • session_register_shutdown - Session shutdown function
    • session_reset - Re-initialize session array with original values
    • session_save_path - 현재 세션 저장 경로를 얻거나 설정
    • session_set_cookie_params - 세션 쿠키 인수 설정
    • session_set_save_handler - 사용자 단계 세션 저장 함수 설정
    • session_start - 세션 데이터 초기화
    • session_status - Returns the current session status
    • session_unregister - 현재 세션에셔 전역 변수를 등록 해제
    • session_unset - 모든 세션 변수 해제
    • session_write_close - 세션 데이터를 쓰고 세션 종료
    • setcookie - Send a cookie
    • setlocale - Set locale information
    • setproctitle - Set the process title
    • setrawcookie - Send a cookie without urlencoding the cookie value
    • setthreadtitle - Set the thread title
    • settype - 변수의 자료형을 설정
    • set_error_handler - Sets a user-defined error handler function
    • set_exception_handler - Sets a user-defined exception handler function
    • set_file_buffer - 별칭: stream_set_write_buffer
    • set_include_path - Sets the include_path configuration option
    • set_magic_quotes_runtime - Sets the current active configuration setting of magic_quotes_runtime
    • set_socket_blocking - 별칭: stream_set_blocking
    • set_time_limit - 최대 실행 시간을 제한
    • sha1 - 문자열의 sha1 해시를 계산
    • sha1_file - 파일의 sha1 해시를 계산
    • shell_exec - Execute command via shell and return the complete output as a string
    • shmop_close - Close shared memory block
    • shmop_delete - Delete shared memory block
    • shmop_open - Create or open shared memory block
    • shmop_read - Read data from shared memory block
    • shmop_size - Get size of shared memory block
    • shmop_write - Write data into shared memory block
    • shm_attach - Creates or open a shared memory segment
    • shm_detach - Disconnects from shared memory segment
    • shm_get_var - Returns a variable from shared memory
    • shm_has_var - Check whether a specific entry exists
    • shm_put_var - Inserts or updates a variable in shared memory
    • shm_remove - Removes shared memory from Unix systems
    • shm_remove_var - Removes a variable from shared memory
    • show_source - 별칭: highlight_file
    • shuffle - 배열을 섞습니다
    • signeurlpaiement - Obtain the payment url (needs 2 arguments)
    • similar_text - 두 문자열 사이의 유사성을 계산
    • SimpleXMLElement::addAttribute - Adds an attribute to the SimpleXML element
    • SimpleXMLElement::addChild - Adds a child element to the XML node
    • SimpleXMLElement::asXML - Return a well-formed XML string based on SimpleXML element
    • SimpleXMLElement::attributes - Identifies an element's attributes
    • SimpleXMLElement::children - Finds children of given node
    • SimpleXMLElement::count - Counts the children of an element
    • SimpleXMLElement::getDocNamespaces - Returns namespaces declared in document
    • SimpleXMLElement::getName - Gets the name of the XML element
    • SimpleXMLElement::getNamespaces - Returns namespaces used in document
    • SimpleXMLElement::registerXPathNamespace - Creates a prefix/ns context for the next XPath query
    • SimpleXMLElement::saveXML - 별칭: SimpleXMLElement::asXML
    • SimpleXMLElement::xpath - Runs XPath query on XML data
    • SimpleXMLElement::__construct - Creates a new SimpleXMLElement object
    • SimpleXMLElement::__toString - Returns the string content
    • simplexml_import_dom - Get a SimpleXMLElement object from a DOM node.
    • simplexml_load_file - Interprets an XML file into an object
    • simplexml_load_string - Interprets a string of XML into an object
    • sin - 사인
    • sinh - 쌍곡사인
    • sizeof - 별칭: count
    • sleep - Delay execution
    • snmp2_get - Fetch an SNMP object
    • snmp2_getnext - Fetch the SNMP object which follows the given object id
    • snmp2_real_walk - Return all objects including their respective object ID within the specified one
    • snmp2_set - Set the value of an SNMP object
    • snmp2_walk - Fetch all the SNMP objects from an agent
    • snmp3_get - Fetch an SNMP object
    • snmp3_getnext - Fetch the SNMP object which follows the given object id
    • snmp3_real_walk - Return all objects including their respective object ID within the specified one
    • snmp3_set - Set the value of an SNMP object
    • snmp3_walk - Fetch all the SNMP objects from an agent
    • SNMP::close - Close SNMP session
    • SNMP::get - Fetch an SNMP object
    • SNMP::getErrno - Get last error code
    • SNMP::getError - Get last error message
    • SNMP::getnext - Fetch an SNMP object which follows the given object id
    • SNMP::set - Set the value of an SNMP object
    • SNMP::setSecurity - Configures security-related SNMPv3 session parameters
    • SNMP::walk - Fetch SNMP object subtree
    • SNMP::__construct - Creates SNMP instance representing session to remote SNMP agent
    • snmpget - Fetch an SNMP object
    • snmpgetnext - Fetch the SNMP object which follows the given object id
    • snmprealwalk - Return all objects including their respective object ID within the specified one
    • snmpset - Set the value of an SNMP object
    • snmpwalk - Fetch all the SNMP objects from an agent
    • snmpwalkoid - Query for a tree of information about a network entity
    • snmp_get_quick_print - Fetches the current value of the UCD library's quick_print setting
    • snmp_get_valueretrieval - Return the method how the SNMP values will be returned
    • snmp_read_mib - Reads and parses a MIB file into the active MIB tree
    • snmp_set_enum_print - Return all values that are enums with their enum value instead of the raw integer
    • snmp_set_oid_numeric_print - Set the OID output format
    • snmp_set_oid_output_format - Set the OID output format
    • snmp_set_quick_print - Set the value of quick_print within the UCD SNMP library
    • snmp_set_valueretrieval - Specify the method how the SNMP values will be returned
    • SoapClient::SoapClient - SoapClient constructor
    • SoapClient::__call - Calls a SOAP function (deprecated)
    • SoapClient::__construct - SoapClient constructor
    • SoapClient::__doRequest - Performs a SOAP request
    • SoapClient::__getFunctions - Returns list of available SOAP functions
    • SoapClient::__getLastRequest - Returns last SOAP request
    • SoapClient::__getLastRequestHeaders - Returns the SOAP headers from the last request
    • SoapClient::__getLastResponse - Returns last SOAP response
    • SoapClient::__getLastResponseHeaders - Returns the SOAP headers from the last response
    • SoapClient::__getTypes - Returns a list of SOAP types
    • SoapClient::__setCookie - The __setCookie purpose
    • SoapClient::__setLocation - Sets the location of the Web service to use
    • SoapClient::__setSoapHeaders - Sets SOAP headers for subsequent calls
    • SoapClient::__soapCall - Calls a SOAP function
    • SoapFault::SoapFault - SoapFault constructor
    • SoapFault::__construct - SoapFault constructor
    • SoapFault::__toString - Obtain a string representation of a SoapFault
    • SoapHeader::SoapHeader - SoapHeader constructor
    • SoapHeader::__construct - SoapHeader constructor
    • SoapParam::SoapParam - SoapParam constructor
    • SoapParam::__construct - SoapParam constructor
    • SoapServer::addFunction - Adds one or more functions to handle SOAP requests
    • SoapServer::addSoapHeader - Add a SOAP header to the response
    • SoapServer::fault - Issue SoapServer fault indicating an error
    • SoapServer::getFunctions - Returns list of defined functions
    • SoapServer::handle - Handles a SOAP request
    • SoapServer::setClass - Sets the class which handles SOAP requests
    • SoapServer::setObject - Sets the object which will be used to handle SOAP requests
    • SoapServer::setPersistence - Sets SoapServer persistence mode
    • SoapServer::SoapServer - SoapServer constructor
    • SoapServer::__construct - SoapServer constructor
    • SoapVar::SoapVar - SoapVar constructor
    • SoapVar::__construct - SoapVar constructor
    • Socket context options - Socket context option listing
    • socket_accept - Accepts a connection on a socket
    • socket_bind - Binds a name to a socket
    • socket_clear_error - Clears the error on the socket or the last error code
    • socket_close - Closes a socket resource
    • socket_cmsg_space - Calculate message buffer size
    • socket_connect - Initiates a connection on a socket
    • socket_create - Create a socket (endpoint for communication)
    • socket_create_listen - Opens a socket on port to accept connections
    • socket_create_pair - Creates a pair of indistinguishable sockets and stores them in an array
    • socket_getopt - 별칭: socket_get_option
    • socket_getpeername - Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type
    • socket_getsockname - Queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type
    • socket_get_option - Gets socket options for the socket
    • socket_get_status - 별칭: stream_get_meta_data
    • socket_import_stream - Import a stream
    • socket_last_error - Returns the last error on the socket
    • socket_listen - Listens for a connection on a socket
    • socket_read - Reads a maximum of length bytes from a socket
    • socket_recv - Receives data from a connected socket
    • socket_recvfrom - Receives data from a socket whether or not it is connection-oriented
    • socket_recvmsg - Read a message
    • socket_select - Runs the select() system call on the given arrays of sockets with a specified timeout
    • socket_send - Sends data to a connected socket
    • socket_sendmsg - Send a message
    • socket_sendto - Sends a message to a socket, whether it is connected or not
    • socket_setopt - 별칭: socket_set_option
    • socket_set_block - Sets blocking mode on a socket resource
    • socket_set_blocking - 별칭: stream_set_blocking
    • socket_set_nonblock - Sets nonblocking mode for file descriptor fd
    • socket_set_option - Sets socket options for the socket
    • socket_set_timeout - 별칭: stream_set_timeout
    • socket_shutdown - Shuts down a socket for receiving, sending, or both
    • socket_strerror - Return a string describing a socket error
    • socket_write - Write to a socket
    • SolrClient::addDocument - Adds a document to the index
    • SolrClient::addDocuments - Adds a collection of SolrInputDocument instances to the index
    • SolrClient::commit - Finalizes all add/deletes made to the index
    • SolrClient::deleteById - Delete by Id
    • SolrClient::deleteByIds - Deletes by Ids
    • SolrClient::deleteByQueries - Removes all documents matching any of the queries
    • SolrClient::deleteByQuery - Deletes all documents matching the given query
    • SolrClient::getById - Get Document By Id. Utilizes Solr Realtime Get (RTG).
    • SolrClient::getByIds - Get Documents by their Ids. Utilizes Solr Realtime Get (RTG).
    • SolrClient::getDebug - Returns the debug data for the last connection attempt
    • SolrClient::getOptions - Returns the client options set internally
    • SolrClient::optimize - Defragments the index
    • SolrClient::ping - Checks if Solr server is still up
    • SolrClient::query - Sends a query to the server
    • SolrClient::request - Sends a raw update request
    • SolrClient::rollback - Rollbacks all add/deletes made to the index since the last commit
    • SolrClient::setResponseWriter - Sets the response writer used to prepare the response from Solr
    • SolrClient::setServlet - Changes the specified servlet type to a new value
    • SolrClient::system - Retrieve Solr Server information
    • SolrClient::threads - Checks the threads status
    • SolrClient::__construct - Constructor for the SolrClient object
    • SolrClient::__destruct - Destructor for SolrClient
    • SolrClientException::getInternalInfo - Returns internal information where the Exception was thrown
    • SolrCollapseFunction::getField - Returns the field that is being collapsed on.
    • SolrCollapseFunction::getHint - Returns collapse hint
    • SolrCollapseFunction::getMax - Returns max parameter
    • SolrCollapseFunction::getMin - Returns min parameter
    • SolrCollapseFunction::getNullPolicy - Returns null policy
    • SolrCollapseFunction::getSize - Returns size parameter
    • SolrCollapseFunction::setField - Sets the field to collapse on
    • SolrCollapseFunction::setHint - Sets collapse hint
    • SolrCollapseFunction::setMax - Selects the group heads by the max value of a numeric field or function query.
    • SolrCollapseFunction::setMin - Sets the initial size of the collapse data structures when collapsing on a numeric field only
    • SolrCollapseFunction::setNullPolicy - Sets the NULL Policy
    • SolrCollapseFunction::setSize - Sets the initial size of the collapse data structures when collapsing on a numeric field only.
    • SolrCollapseFunction::__construct - Constructor
    • SolrCollapseFunction::__toString - Returns a string representing the constructed collapse function
    • SolrDisMaxQuery::addBigramPhraseField - Adds a Phrase Bigram Field (pf2 parameter)
    • SolrDisMaxQuery::addBoostQuery - Adds a boost query field with value and optional boost (bq parameter)
    • SolrDisMaxQuery::addPhraseField - Adds a Phrase Field (pf parameter)
    • SolrDisMaxQuery::addQueryField - Add a query field with optional boost (qf parameter)
    • SolrDisMaxQuery::addTrigramPhraseField - Adds a Trigram Phrase Field (pf3 parameter)
    • SolrDisMaxQuery::addUserField - Adds a field to User Fields Parameter (uf)
    • SolrDisMaxQuery::removeBigramPhraseField - Removes phrase bigram field (pf2 parameter)
    • SolrDisMaxQuery::removeBoostQuery - Removes a boost query partial by field name (bq)
    • SolrDisMaxQuery::removePhraseField - Removes a Phrase Field (pf parameter)
    • SolrDisMaxQuery::removeQueryField - Removes a Query Field (qf parameter)
    • SolrDisMaxQuery::removeTrigramPhraseField - Removes a Trigram Phrase Field (pf3 parameter)
    • SolrDisMaxQuery::removeUserField - Removes a field from The User Fields Parameter (uf)
    • SolrDisMaxQuery::setBigramPhraseFields - Sets Bigram Phrase Fields and their boosts (and slops) using pf2 parameter
    • SolrDisMaxQuery::setBigramPhraseSlop - Sets Bigram Phrase Slop (ps2 parameter)
    • SolrDisMaxQuery::setBoostFunction - Sets a Boost Function (bf parameter).
    • SolrDisMaxQuery::setBoostQuery - Directly Sets Boost Query Parameter (bq)
    • SolrDisMaxQuery::setMinimumMatch - Set Minimum "Should" Match (mm)
    • SolrDisMaxQuery::setPhraseFields - Sets Phrase Fields and their boosts (and slops) using pf2 parameter
    • SolrDisMaxQuery::setPhraseSlop - Sets the default slop on phrase queries (ps parameter)
    • SolrDisMaxQuery::setQueryAlt - Set Query Alternate (q.alt parameter)
    • SolrDisMaxQuery::setQueryPhraseSlop - specifies the amount of slop permitted on phrase queries explicitly included in the user's query string (qf parameter)
    • SolrDisMaxQuery::setTieBreaker - Sets Tie Breaker parameter (tie parameter)
    • SolrDisMaxQuery::setTrigramPhraseFields - Directly Sets Trigram Phrase Fields (pf3 parameter)
    • SolrDisMaxQuery::setTrigramPhraseSlop - Sets Trigram Phrase Slop (ps3 parameter)
    • SolrDisMaxQuery::setUserFields - Sets User Fields parameter (uf)
    • SolrDisMaxQuery::useDisMaxQueryParser - Switch QueryParser to be DisMax Query Parser
    • SolrDisMaxQuery::useEDisMaxQueryParser - Switch QueryParser to be EDisMax
    • SolrDisMaxQuery::__construct - Class Constructor
    • SolrDocument::addField - Adds a field to the document
    • SolrDocument::clear - Drops all the fields in the document
    • SolrDocument::current - Retrieves the current field
    • SolrDocument::deleteField - Removes a field from the document
    • SolrDocument::fieldExists - Checks if a field exists in the document
    • SolrDocument::getField - Retrieves a field by name
    • SolrDocument::getFieldCount - Returns the number of fields in this document
    • SolrDocument::getFieldNames - Returns an array of fields names in the document
    • SolrDocument::getInputDocument - Returns a SolrInputDocument equivalent of the object
    • SolrDocument::key - Retrieves the current key
    • SolrDocument::merge - Merges source to the current SolrDocument
    • SolrDocument::next - Moves the internal pointer to the next field
    • SolrDocument::offsetExists - Checks if a particular field exists
    • SolrDocument::offsetGet - Retrieves a field
    • SolrDocument::offsetSet - Adds a field to the document
    • SolrDocument::offsetUnset - Removes a field
    • SolrDocument::reset - This is an alias to SolrDocument::clear()
    • SolrDocument::rewind - Resets the internal pointer to the beginning
    • SolrDocument::serialize - Used for custom serialization
    • SolrDocument::sort - Sorts the fields in the document
    • SolrDocument::toArray - Returns an array representation of the document
    • SolrDocument::unserialize - Custom serialization of SolrDocument objects
    • SolrDocument::valid - Checks if the current position internally is still valid
    • SolrDocument::__clone - Creates a copy of a SolrDocument object
    • SolrDocument::__construct - Constructor
    • SolrDocument::__destruct - Destructor
    • SolrDocument::__get - Access the field as a property
    • SolrDocument::__isset - Checks if a field exists
    • SolrDocument::__set - Adds another field to the document
    • SolrDocument::__unset - Removes a field from the document
    • SolrDocumentField::__construct - Constructor
    • SolrDocumentField::__destruct - Destructor
    • SolrException::getInternalInfo - Returns internal information where the Exception was thrown
    • SolrGenericResponse::__construct - Constructor
    • SolrGenericResponse::__destruct - Destructor
    • SolrIllegalArgumentException::getInternalInfo - Returns internal information where the Exception was thrown
    • SolrIllegalOperationException::getInternalInfo - Returns internal information where the Exception was thrown
    • SolrInputDocument::addField - Adds a field to the document
    • SolrInputDocument::clear - Resets the input document
    • SolrInputDocument::deleteField - Removes a field from the document
    • SolrInputDocument::fieldExists - Checks if a field exists
    • SolrInputDocument::getBoost - Retrieves the current boost value for the document
    • SolrInputDocument::getField - Retrieves a field by name
    • SolrInputDocument::getFieldBoost - Retrieves the boost value for a particular field
    • SolrInputDocument::getFieldCount - Returns the number of fields in the document
    • SolrInputDocument::getFieldNames - Returns an array containing all the fields in the document
    • SolrInputDocument::merge - Merges one input document into another
    • SolrInputDocument::reset - This is an alias of SolrInputDocument::clear
    • SolrInputDocument::setBoost - Sets the boost value for this document
    • SolrInputDocument::setFieldBoost - Sets the index-time boost value for a field
    • SolrInputDocument::sort - Sorts the fields within the document
    • SolrInputDocument::toArray - Returns an array representation of the input document
    • SolrInputDocument::__clone - Creates a copy of a SolrDocument
    • SolrInputDocument::__construct - Constructor
    • SolrInputDocument::__destruct - Destructor
    • SolrModifiableParams::__construct - Constructor
    • SolrModifiableParams::__destruct - Destructor
    • SolrObject::getPropertyNames - Returns an array of all the names of the properties
    • SolrObject::offsetExists - Checks if the property exists
    • SolrObject::offsetGet - Used to retrieve a property
    • SolrObject::offsetSet - Sets the value for a property
    • SolrObject::offsetUnset - Unsets the value for the property
    • SolrObject::__construct - Creates Solr object
    • SolrObject::__destruct - Destructor
    • SolrParams::add - This is an alias for SolrParams::addParam
    • SolrParams::addParam - Adds a parameter to the object
    • SolrParams::get - This is an alias for SolrParams::getParam
    • SolrParams::getParam - Returns a parameter value
    • SolrParams::getParams - Returns an array of non URL-encoded parameters
    • SolrParams::getPreparedParams - Returns an array of URL-encoded parameters
    • SolrParams::serialize - Used for custom serialization
    • SolrParams::set - An alias of SolrParams::setParam
    • SolrParams::setParam - Sets the parameter to the specified value
    • SolrParams::toString - Returns all the name-value pair parameters in the object
    • SolrParams::unserialize - Used for custom serialization
    • SolrPingResponse::getResponse - Returns the response from the server
    • SolrPingResponse::__construct - Constructor
    • SolrPingResponse::__destruct - Destructor
    • SolrQuery::addExpandFilterQuery - Overrides main filter query, determines which documents to include in the main group.
    • SolrQuery::addExpandSortField - Orders the documents within the expanded groups (expand.sort parameter).
    • SolrQuery::addFacetDateField - Maps to facet.date
    • SolrQuery::addFacetDateOther - Adds another facet.date.other parameter
    • SolrQuery::addFacetField - Adds another field to the facet
    • SolrQuery::addFacetQuery - Adds a facet query
    • SolrQuery::addField - Specifies which fields to return in the result
    • SolrQuery::addFilterQuery - Specifies a filter query
    • SolrQuery::addGroupField - Add a field to be used to group results.
    • SolrQuery::addGroupFunction - Allows grouping results based on the unique values of a function query (group.func parameter).
    • SolrQuery::addGroupQuery - Allows grouping of documents that match the given query.
    • SolrQuery::addGroupSortField - Add a group sort field (group.sort parameter).
    • SolrQuery::addHighlightField - Maps to hl.fl
    • SolrQuery::addMltField - Sets a field to use for similarity
    • SolrQuery::addMltQueryField - Maps to mlt.qf
    • SolrQuery::addSortField - Used to control how the results should be sorted
    • SolrQuery::addStatsFacet - Requests a return of sub results for values within the given facet
    • SolrQuery::addStatsField - Maps to stats.field parameter
    • SolrQuery::collapse - Collapses the result set to a single document per group
    • SolrQuery::getExpand - Returns true if group expanding is enabled
    • SolrQuery::getExpandFilterQueries - Returns the expand filter queries
    • SolrQuery::getExpandQuery - Returns the expand query expand.q parameter
    • SolrQuery::getExpandRows - Returns The number of rows to display in each group (expand.rows)
    • SolrQuery::getExpandSortFields - Returns an array of fields
    • SolrQuery::getFacet - Returns the value of the facet parameter
    • SolrQuery::getFacetDateEnd - Returns the value for the facet.date.end parameter
    • SolrQuery::getFacetDateFields - Returns all the facet.date fields
    • SolrQuery::getFacetDateGap - Returns the value of the facet.date.gap parameter
    • SolrQuery::getFacetDateHardEnd - Returns the value of the facet.date.hardend parameter
    • SolrQuery::getFacetDateOther - Returns the value for the facet.date.other parameter
    • SolrQuery::getFacetDateStart - Returns the lower bound for the first date range for all date faceting on this field
    • SolrQuery::getFacetFields - Returns all the facet fields
    • SolrQuery::getFacetLimit - Returns the maximum number of constraint counts that should be returned for the facet fields
    • SolrQuery::getFacetMethod - Returns the value of the facet.method parameter
    • SolrQuery::getFacetMinCount - Returns the minimum counts for facet fields should be included in the response
    • SolrQuery::getFacetMissing - Returns the current state of the facet.missing parameter
    • SolrQuery::getFacetOffset - Returns an offset into the list of constraints to be used for pagination
    • SolrQuery::getFacetPrefix - Returns the facet prefix
    • SolrQuery::getFacetQueries - Returns all the facet queries
    • SolrQuery::getFacetSort - Returns the facet sort type
    • SolrQuery::getFields - Returns the list of fields that will be returned in the response
    • SolrQuery::getFilterQueries - Returns an array of filter queries
    • SolrQuery::getGroup - Returns true if grouping is enabled
    • SolrQuery::getGroupCachePercent - Returns group cache percent value
    • SolrQuery::getGroupFacet - Returns the group.facet parameter value
    • SolrQuery::getGroupFields - Returns group fields (group.field parameter values)
    • SolrQuery::getGroupFormat - Returns the group.format value
    • SolrQuery::getGroupFunctions - Returns group functions (group.func parameter values)
    • SolrQuery::getGroupLimit - Returns the group.limit value
    • SolrQuery::getGroupMain - Returns the group.main value
    • SolrQuery::getGroupNGroups - Returns the group.ngroups value
    • SolrQuery::getGroupOffset - Returns the group.offset value
    • SolrQuery::getGroupQueries - Returns all the group.query parameter values
    • SolrQuery::getGroupSortFields - Returns the group.sort value
    • SolrQuery::getGroupTruncate - Returns the group.truncate value
    • SolrQuery::getHighlight - Returns the state of the hl parameter
    • SolrQuery::getHighlightAlternateField - Returns the highlight field to use as backup or default
    • SolrQuery::getHighlightFields - Returns all the fields that Solr should generate highlighted snippets for
    • SolrQuery::getHighlightFormatter - Returns the formatter for the highlighted output
    • SolrQuery::getHighlightFragmenter - Returns the text snippet generator for highlighted text
    • SolrQuery::getHighlightFragsize - Returns the number of characters of fragments to consider for highlighting
    • SolrQuery::getHighlightHighlightMultiTerm - Returns whether or not to enable highlighting for range/wildcard/fuzzy/prefix queries
    • SolrQuery::getHighlightMaxAlternateFieldLength - Returns the maximum number of characters of the field to return
    • SolrQuery::getHighlightMaxAnalyzedChars - Returns the maximum number of characters into a document to look for suitable snippets
    • SolrQuery::getHighlightMergeContiguous - Returns whether or not the collapse contiguous fragments into a single fragment
    • SolrQuery::getHighlightRegexMaxAnalyzedChars - Returns the maximum number of characters from a field when using the regex fragmenter
    • SolrQuery::getHighlightRegexPattern - Returns the regular expression for fragmenting
    • SolrQuery::getHighlightRegexSlop - Returns the deviation factor from the ideal fragment size
    • SolrQuery::getHighlightRequireFieldMatch - Returns if a field will only be highlighted if the query matched in this particular field
    • SolrQuery::getHighlightSimplePost - Returns the text which appears after a highlighted term
    • SolrQuery::getHighlightSimplePre - Returns the text which appears before a highlighted term
    • SolrQuery::getHighlightSnippets - Returns the maximum number of highlighted snippets to generate per field
    • SolrQuery::getHighlightUsePhraseHighlighter - Returns the state of the hl.usePhraseHighlighter parameter
    • SolrQuery::getMlt - Returns whether or not MoreLikeThis results should be enabled
    • SolrQuery::getMltBoost - Returns whether or not the query will be boosted by the interesting term relevance
    • SolrQuery::getMltCount - Returns the number of similar documents to return for each result
    • SolrQuery::getMltFields - Returns all the fields to use for similarity
    • SolrQuery::getMltMaxNumQueryTerms - Returns the maximum number of query terms that will be included in any generated query
    • SolrQuery::getMltMaxNumTokens - Returns the maximum number of tokens to parse in each document field that is not stored with TermVector support
    • SolrQuery::getMltMaxWordLength - Returns the maximum word length above which words will be ignored
    • SolrQuery::getMltMinDocFrequency - Returns the treshold frequency at which words will be ignored which do not occur in at least this many docs
    • SolrQuery::getMltMinTermFrequency - Returns the frequency below which terms will be ignored in the source document
    • SolrQuery::getMltMinWordLength - Returns the minimum word length below which words will be ignored
    • SolrQuery::getMltQueryFields - Returns the query fields and their boosts
    • SolrQuery::getQuery - Returns the main query
    • SolrQuery::getRows - Returns the maximum number of documents
    • SolrQuery::getSortFields - Returns all the sort fields
    • SolrQuery::getStart - Returns the offset in the complete result set
    • SolrQuery::getStats - Returns whether or not stats is enabled
    • SolrQuery::getStatsFacets - Returns all the stats facets that were set
    • SolrQuery::getStatsFields - Returns all the statistics fields
    • SolrQuery::getTerms - Returns whether or not the TermsComponent is enabled
    • SolrQuery::getTermsField - Returns the field from which the terms are retrieved
    • SolrQuery::getTermsIncludeLowerBound - Returns whether or not to include the lower bound in the result set
    • SolrQuery::getTermsIncludeUpperBound - Returns whether or not to include the upper bound term in the result set
    • SolrQuery::getTermsLimit - Returns the maximum number of terms Solr should return
    • SolrQuery::getTermsLowerBound - Returns the term to start at
    • SolrQuery::getTermsMaxCount - Returns the maximum document frequency
    • SolrQuery::getTermsMinCount - Returns the minimum document frequency to return in order to be included
    • SolrQuery::getTermsPrefix - Returns the term prefix
    • SolrQuery::getTermsReturnRaw - Whether or not to return raw characters
    • SolrQuery::getTermsSort - Returns an integer indicating how terms are sorted
    • SolrQuery::getTermsUpperBound - Returns the term to stop at
    • SolrQuery::getTimeAllowed - Returns the time in milliseconds allowed for the query to finish
    • SolrQuery::removeExpandFilterQuery - Removes an expand filter query
    • SolrQuery::removeExpandSortField - Removes an expand sort field from the expand.sort parameter.
    • SolrQuery::removeFacetDateField - Removes one of the facet date fields
    • SolrQuery::removeFacetDateOther - Removes one of the facet.date.other parameters
    • SolrQuery::removeFacetField - Removes one of the facet.date parameters
    • SolrQuery::removeFacetQuery - Removes one of the facet.query parameters
    • SolrQuery::removeField - Removes a field from the list of fields
    • SolrQuery::removeFilterQuery - Removes a filter query
    • SolrQuery::removeHighlightField - Removes one of the fields used for highlighting
    • SolrQuery::removeMltField - Removes one of the moreLikeThis fields
    • SolrQuery::removeMltQueryField - Removes one of the moreLikeThis query fields
    • SolrQuery::removeSortField - Removes one of the sort fields
    • SolrQuery::removeStatsFacet - Removes one of the stats.facet parameters
    • SolrQuery::removeStatsField - Removes one of the stats.field parameters
    • SolrQuery::setEchoHandler - Toggles the echoHandler parameter
    • SolrQuery::setEchoParams - Determines what kind of parameters to include in the response
    • SolrQuery::setExpand - Enables/Disables the Expand Component
    • SolrQuery::setExpandQuery - Sets the expand.q parameter
    • SolrQuery::setExpandRows - Sets the number of rows to display in each group (expand.rows). Server Default 5
    • SolrQuery::setExplainOther - Sets the explainOther common query parameter
    • SolrQuery::setFacet - Maps to the facet parameter. Enables or disables facetting
    • SolrQuery::setFacetDateEnd - Maps to facet.date.end
    • SolrQuery::setFacetDateGap - Maps to facet.date.gap
    • SolrQuery::setFacetDateHardEnd - Maps to facet.date.hardend
    • SolrQuery::setFacetDateStart - Maps to facet.date.start
    • SolrQuery::setFacetEnumCacheMinDefaultFrequency - Sets the minimum document frequency used for determining term count
    • SolrQuery::setFacetLimit - Maps to facet.limit
    • SolrQuery::setFacetMethod - Specifies the type of algorithm to use when faceting a field
    • SolrQuery::setFacetMinCount - Maps to facet.mincount
    • SolrQuery::setFacetMissing - Maps to facet.missing
    • SolrQuery::setFacetOffset - Sets the offset into the list of constraints to allow for pagination
    • SolrQuery::setFacetPrefix - Specifies a string prefix with which to limits the terms on which to facet
    • SolrQuery::setFacetSort - Determines the ordering of the facet field constraints
    • SolrQuery::setGroup - Enable/Disable result grouping (group parameter)
    • SolrQuery::setGroupCachePercent - Enables caching for result grouping
    • SolrQuery::setGroupFacet - Sets group.facet parameter
    • SolrQuery::setGroupFormat - Sets the group format, result structure (group.format parameter).
    • SolrQuery::setGroupLimit - Specifies the number of results to return for each group. The server default value is 1.
    • SolrQuery::setGroupMain - If true, the result of the first field grouping command is used as the main result list in the response, using group.format=simple.
    • SolrQuery::setGroupNGroups - If true, Solr includes the number of groups that have matched the query in the results.
    • SolrQuery::setGroupOffset - Sets the group.offset parameter.
    • SolrQuery::setGroupTruncate - If true, facet counts are based on the most relevant document of each group matching the query.
    • SolrQuery::setHighlight - Enables or disables highlighting
    • SolrQuery::setHighlightAlternateField - Specifies the backup field to use
    • SolrQuery::setHighlightFormatter - Specify a formatter for the highlight output
    • SolrQuery::setHighlightFragmenter - Sets a text snippet generator for highlighted text
    • SolrQuery::setHighlightFragsize - The size of fragments to consider for highlighting
    • SolrQuery::setHighlightHighlightMultiTerm - Use SpanScorer to highlight phrase terms
    • SolrQuery::setHighlightMaxAlternateFieldLength - Sets the maximum number of characters of the field to return
    • SolrQuery::setHighlightMaxAnalyzedChars - Specifies the number of characters into a document to look for suitable snippets
    • SolrQuery::setHighlightMergeContiguous - Whether or not to collapse contiguous fragments into a single fragment
    • SolrQuery::setHighlightRegexMaxAnalyzedChars - Specify the maximum number of characters to analyze
    • SolrQuery::setHighlightRegexPattern - Specify the regular expression for fragmenting
    • SolrQuery::setHighlightRegexSlop - Sets the factor by which the regex fragmenter can stray from the ideal fragment size
    • SolrQuery::setHighlightRequireFieldMatch - Require field matching during highlighting
    • SolrQuery::setHighlightSimplePost - Sets the text which appears after a highlighted term
    • SolrQuery::setHighlightSimplePre - Sets the text which appears before a highlighted term
    • SolrQuery::setHighlightSnippets - Sets the maximum number of highlighted snippets to generate per field
    • SolrQuery::setHighlightUsePhraseHighlighter - Whether to highlight phrase terms only when they appear within the query phrase
    • SolrQuery::setMlt - Enables or disables moreLikeThis
    • SolrQuery::setMltBoost - Set if the query will be boosted by the interesting term relevance
    • SolrQuery::setMltCount - Set the number of similar documents to return for each result
    • SolrQuery::setMltMaxNumQueryTerms - Sets the maximum number of query terms included
    • SolrQuery::setMltMaxNumTokens - Specifies the maximum number of tokens to parse
    • SolrQuery::setMltMaxWordLength - Sets the maximum word length
    • SolrQuery::setMltMinDocFrequency - Sets the mltMinDoc frequency
    • SolrQuery::setMltMinTermFrequency - Sets the frequency below which terms will be ignored in the source docs
    • SolrQuery::setMltMinWordLength - Sets the minimum word length
    • SolrQuery::setOmitHeader - Exclude the header from the returned results
    • SolrQuery::setQuery - Sets the search query
    • SolrQuery::setRows - Specifies the maximum number of rows to return in the result
    • SolrQuery::setShowDebugInfo - Flag to show debug information
    • SolrQuery::setStart - Specifies the number of rows to skip
    • SolrQuery::setStats - Enables or disables the Stats component
    • SolrQuery::setTerms - Enables or disables the TermsComponent
    • SolrQuery::setTermsField - Sets the name of the field to get the Terms from
    • SolrQuery::setTermsIncludeLowerBound - Include the lower bound term in the result set
    • SolrQuery::setTermsIncludeUpperBound - Include the upper bound term in the result set
    • SolrQuery::setTermsLimit - Sets the maximum number of terms to return
    • SolrQuery::setTermsLowerBound - Specifies the Term to start from
    • SolrQuery::setTermsMaxCount - Sets the maximum document frequency
    • SolrQuery::setTermsMinCount - Sets the minimum document frequency
    • SolrQuery::setTermsPrefix - Restrict matches to terms that start with the prefix
    • SolrQuery::setTermsReturnRaw - Return the raw characters of the indexed term
    • SolrQuery::setTermsSort - Specifies how to sort the returned terms
    • SolrQuery::setTermsUpperBound - Sets the term to stop at
    • SolrQuery::setTimeAllowed - The time allowed for search to finish
    • SolrQuery::__construct - Constructor
    • SolrQuery::__destruct - Destructor
    • SolrQueryResponse::__construct - Constructor
    • SolrQueryResponse::__destruct - Destructor
    • SolrResponse::getDigestedResponse - Returns the XML response as serialized PHP data
    • SolrResponse::getHttpStatus - Returns the HTTP status of the response
    • SolrResponse::getHttpStatusMessage - Returns more details on the HTTP status
    • SolrResponse::getRawRequest - Returns the raw request sent to the Solr server
    • SolrResponse::getRawRequestHeaders - Returns the raw request headers sent to the Solr server
    • SolrResponse::getRawResponse - Returns the raw response from the server
    • SolrResponse::getRawResponseHeaders - Returns the raw response headers from the server
    • SolrResponse::getRequestUrl - Returns the full URL the request was sent to
    • SolrResponse::getResponse - Returns a SolrObject representing the XML response from the server
    • SolrResponse::setParseMode - Sets the parse mode
    • SolrResponse::success - Was the request a success
    • SolrServerException::getInternalInfo - Returns internal information where the Exception was thrown
    • SolrUpdateResponse::__construct - Constructor
    • SolrUpdateResponse::__destruct - Destructor
    • SolrUtils::digestXmlResponse - Parses an response XML string into a SolrObject
    • SolrUtils::escapeQueryChars - Escapes a lucene query string
    • SolrUtils::getSolrVersion - Returns the current version of the Solr extension
    • SolrUtils::queryPhrase - Prepares a phrase from an unescaped lucene string
    • solr_get_version - Returns the current version of the Apache Solr extension
    • sort - 배열 정렬
    • soundex - 문자열의 soundex 키를 계산
    • SphinxClient::addQuery - Add query to multi-query batch
    • SphinxClient::buildExcerpts - Build text snippets
    • SphinxClient::buildKeywords - Extract keywords from query
    • SphinxClient::close - Closes previously opened persistent connection
    • SphinxClient::escapeString - Escape special characters
    • SphinxClient::getLastError - Get the last error message
    • SphinxClient::getLastWarning - Get the last warning
    • SphinxClient::open - Opens persistent connection to the server
    • SphinxClient::query - Execute search query
    • SphinxClient::resetFilters - Clear all filters
    • SphinxClient::resetGroupBy - Clear all group-by settings
    • SphinxClient::runQueries - Run a batch of search queries
    • SphinxClient::setArrayResult - Change the format of result set array
    • SphinxClient::setConnectTimeout - Set connection timeout
    • SphinxClient::setFieldWeights - Set field weights
    • SphinxClient::setFilter - Add new integer values set filter
    • SphinxClient::setFilterFloatRange - Add new float range filter
    • SphinxClient::setFilterRange - Add new integer range filter
    • SphinxClient::setGeoAnchor - Set anchor point for a geosphere distance calculations
    • SphinxClient::setGroupBy - Set grouping attribute
    • SphinxClient::setGroupDistinct - Set attribute name for per-group distinct values count calculations
    • SphinxClient::setIDRange - Set a range of accepted document IDs
    • SphinxClient::setIndexWeights - Set per-index weights
    • SphinxClient::setLimits - Set offset and limit of the result set
    • SphinxClient::setMatchMode - Set full-text query matching mode
    • SphinxClient::setMaxQueryTime - Set maximum query time
    • SphinxClient::setOverride - Sets temporary per-document attribute value overrides
    • SphinxClient::setRankingMode - Set ranking mode
    • SphinxClient::setRetries - Set retry count and delay
    • SphinxClient::setSelect - Set select clause
    • SphinxClient::setServer - Set searchd host and port
    • SphinxClient::setSortMode - Set matches sorting mode
    • SphinxClient::status - Queries searchd status
    • SphinxClient::updateAttributes - Update document attributes
    • SphinxClient::__construct - Create a new SphinxClient object
    • SplDoublyLinkedList::add - Add/insert a new value at the specified index
    • SplDoublyLinkedList::bottom - Peeks at the node from the beginning of the doubly linked list
    • SplDoublyLinkedList::count - Counts the number of elements in the doubly linked list.
    • SplDoublyLinkedList::current - Return current array entry
    • SplDoublyLinkedList::getIteratorMode - Returns the mode of iteration
    • SplDoublyLinkedList::isEmpty - Checks whether the doubly linked list is empty.
    • SplDoublyLinkedList::key - Return current node index
    • SplDoublyLinkedList::next - Move to next entry
    • SplDoublyLinkedList::offsetExists - Returns whether the requested $index exists
    • SplDoublyLinkedList::offsetGet - Returns the value at the specified $index
    • SplDoublyLinkedList::offsetSet - Sets the value at the specified $index to $newval
    • SplDoublyLinkedList::offsetUnset - Unsets the value at the specified $index
    • SplDoublyLinkedList::pop - Pops a node from the end of the doubly linked list
    • SplDoublyLinkedList::prev - Move to previous entry
    • SplDoublyLinkedList::push - Pushes an element at the end of the doubly linked list
    • SplDoublyLinkedList::rewind - Rewind iterator back to the start
    • SplDoublyLinkedList::serialize - Serializes the storage
    • SplDoublyLinkedList::setIteratorMode - Sets the mode of iteration
    • SplDoublyLinkedList::shift - Shifts a node from the beginning of the doubly linked list
    • SplDoublyLinkedList::top - Peeks at the node from the end of the doubly linked list
    • SplDoublyLinkedList::unserialize - Unserializes the storage
    • SplDoublyLinkedList::unshift - Prepends the doubly linked list with an element
    • SplDoublyLinkedList::valid - Check whether the doubly linked list contains more nodes
    • SplDoublyLinkedList::__construct - Constructs a new doubly linked list
    • SplEnum::getConstList - Returns all consts (possible values) as an array.
    • SplFileInfo::getATime - Gets last access time of the file
    • SplFileInfo::getBasename - Gets the base name of the file
    • SplFileInfo::getCTime - Gets the inode change time
    • SplFileInfo::getExtension - Gets the file extension
    • SplFileInfo::getFileInfo - Gets an SplFileInfo object for the file
    • SplFileInfo::getFilename - Gets the filename
    • SplFileInfo::getGroup - Gets the file group
    • SplFileInfo::getInode - Gets the inode for the file
    • SplFileInfo::getLinkTarget - Gets the target of a link
    • SplFileInfo::getMTime - Gets the last modified time
    • SplFileInfo::getOwner - Gets the owner of the file
    • SplFileInfo::getPath - Gets the path without filename
    • SplFileInfo::getPathInfo - Gets an SplFileInfo object for the path
    • SplFileInfo::getPathname - Gets the path to the file
    • SplFileInfo::getPerms - Gets file permissions
    • SplFileInfo::getRealPath - Gets absolute path to file
    • SplFileInfo::getSize - Gets file size
    • SplFileInfo::getType - Gets file type
    • SplFileInfo::isDir - Tells if the file is a directory
    • SplFileInfo::isExecutable - Tells if the file is executable
    • SplFileInfo::isFile - Tells if the object references a regular file
    • SplFileInfo::isLink - Tells if the file is a link
    • SplFileInfo::isReadable - Tells if file is readable
    • SplFileInfo::isWritable - Tells if the entry is writable
    • SplFileInfo::openFile - Gets an SplFileObject object for the file
    • SplFileInfo::setFileClass - Sets the class used with SplFileInfo::openFile
    • SplFileInfo::setInfoClass - Sets the class used with SplFileInfo::getFileInfo and SplFileInfo::getPathInfo
    • SplFileInfo::__construct - Construct a new SplFileInfo object
    • SplFileInfo::__toString - Returns the path to the file as a string
    • SplFileObject::current - Retrieve current line of file
    • SplFileObject::eof - Reached end of file
    • SplFileObject::fflush - Flushes the output to the file
    • SplFileObject::fgetc - Gets character from file
    • SplFileObject::fgetcsv - Gets line from file and parse as CSV fields
    • SplFileObject::fgets - Gets line from file
    • SplFileObject::fgetss - Gets line from file and strip HTML tags
    • SplFileObject::flock - Portable file locking
    • SplFileObject::fpassthru - Output all remaining data on a file pointer
    • SplFileObject::fputcsv - Write a field array as a CSV line
    • SplFileObject::fread - Read from file
    • SplFileObject::fscanf - Parses input from file according to a format
    • SplFileObject::fseek - Seek to a position
    • SplFileObject::fstat - Gets information about the file
    • SplFileObject::ftell - Return current file position
    • SplFileObject::ftruncate - Truncates the file to a given length
    • SplFileObject::fwrite - Write to file
    • SplFileObject::getChildren - No purpose
    • SplFileObject::getCsvControl - Get the delimiter, enclosure and escape character for CSV
    • SplFileObject::getCurrentLine - Alias of SplFileObject::fgets
    • SplFileObject::getFlags - Gets flags for the SplFileObject
    • SplFileObject::getMaxLineLen - Get maximum line length
    • SplFileObject::hasChildren - SplFileObject does not have children
    • SplFileObject::key - Get line number
    • SplFileObject::next - Read next line
    • SplFileObject::rewind - Rewind the file to the first line
    • SplFileObject::seek - Seek to specified line
    • SplFileObject::setCsvControl - Set the delimiter, enclosure and escape character for CSV
    • SplFileObject::setFlags - Sets flags for the SplFileObject
    • SplFileObject::setMaxLineLen - Set maximum line length
    • SplFileObject::valid - Not at EOF
    • SplFileObject::__construct - Construct a new file object.
    • SplFileObject::__toString - Alias of SplFileObject::current
    • SplFixedArray::count - Returns the size of the array
    • SplFixedArray::current - Return current array entry
    • SplFixedArray::fromArray - Import a PHP array in a SplFixedArray instance
    • SplFixedArray::getSize - Gets the size of the array
    • SplFixedArray::key - Return current array index
    • SplFixedArray::next - Move to next entry
    • SplFixedArray::offsetExists - Returns whether the requested index exists
    • SplFixedArray::offsetGet - Returns the value at the specified index
    • SplFixedArray::offsetSet - Sets a new value at a specified index
    • SplFixedArray::offsetUnset - Unsets the value at the specified $index
    • SplFixedArray::rewind - Rewind iterator back to the start
    • SplFixedArray::setSize - Change the size of an array
    • SplFixedArray::toArray - Returns a PHP array from the fixed array
    • SplFixedArray::valid - Check whether the array contains more elements
    • SplFixedArray::__construct - Constructs a new fixed array
    • SplFixedArray::__wakeup - Reinitialises the array after being unserialised
    • SplHeap::compare - Compare elements in order to place them correctly in the heap while sifting up.
    • SplHeap::count - Counts the number of elements in the heap.
    • SplHeap::current - Return current node pointed by the iterator
    • SplHeap::extract - Extracts a node from top of the heap and sift up.
    • SplHeap::insert - Inserts an element in the heap by sifting it up.
    • SplHeap::isEmpty - Checks whether the heap is empty.
    • SplHeap::key - Return current node index
    • SplHeap::next - Move to the next node
    • SplHeap::recoverFromCorruption - Recover from the corrupted state and allow further actions on the heap.
    • SplHeap::rewind - Rewind iterator back to the start (no-op)
    • SplHeap::top - Peeks at the node from the top of the heap
    • SplHeap::valid - Check whether the heap contains more nodes
    • SplHeap::__construct - Constructs a new empty heap
    • split - Split string into array by regular expression
    • spliti - Split string into array by regular expression case insensitive
    • SplMaxHeap::compare - Compare elements in order to place them correctly in the heap while sifting up.
    • SplMinHeap::compare - Compare elements in order to place them correctly in the heap while sifting up.
    • SplObjectStorage::addAll - Adds all objects from another storage
    • SplObjectStorage::attach - Adds an object in the storage
    • SplObjectStorage::contains - Checks if the storage contains a specific object
    • SplObjectStorage::count - Returns the number of objects in the storage
    • SplObjectStorage::current - Returns the current storage entry
    • SplObjectStorage::detach - Removes an object from the storage
    • SplObjectStorage::getHash - Calculate a unique identifier for the contained objects
    • SplObjectStorage::getInfo - Returns the data associated with the current iterator entry
    • SplObjectStorage::key - Returns the index at which the iterator currently is
    • SplObjectStorage::next - Move to the next entry
    • SplObjectStorage::offsetExists - Checks whether an object exists in the storage
    • SplObjectStorage::offsetGet - Returns the data associated with an object
    • SplObjectStorage::offsetSet - Associates data to an object in the storage
    • SplObjectStorage::offsetUnset - Removes an object from the storage
    • SplObjectStorage::removeAll - Removes objects contained in another storage from the current storage
    • SplObjectStorage::removeAllExcept - Removes all objects except for those contained in another storage from the current storage
    • SplObjectStorage::rewind - Rewind the iterator to the first storage element
    • SplObjectStorage::serialize - Serializes the storage
    • SplObjectStorage::setInfo - Sets the data associated with the current iterator entry
    • SplObjectStorage::unserialize - Unserializes a storage from its string representation
    • SplObjectStorage::valid - Returns if the current iterator entry is valid
    • SplObserver::update - Receive update from subject
    • SplPriorityQueue::compare - Compare priorities in order to place elements correctly in the heap while sifting up.
    • SplPriorityQueue::count - Counts the number of elements in the queue.
    • SplPriorityQueue::current - Return current node pointed by the iterator
    • SplPriorityQueue::extract - Extracts a node from top of the heap and shift up.
    • SplPriorityQueue::insert - Inserts an element in the queue by sifting it up.
    • SplPriorityQueue::isEmpty - Checks whether the queue is empty.
    • SplPriorityQueue::key - Return current node index
    • SplPriorityQueue::next - Move to the next node
    • SplPriorityQueue::recoverFromCorruption - Recover from the corrupted state and allow further actions on the queue.
    • SplPriorityQueue::rewind - Rewind iterator back to the start (no-op)
    • SplPriorityQueue::setExtractFlags - Sets the mode of extraction
    • SplPriorityQueue::top - Peeks at the node from the top of the queue
    • SplPriorityQueue::valid - Check whether the queue contains more nodes
    • SplPriorityQueue::__construct - Constructs a new empty queue
    • SplQueue::dequeue - Dequeues a node from the queue
    • SplQueue::enqueue - Adds an element to the queue.
    • SplQueue::setIteratorMode - Sets the mode of iteration
    • SplQueue::__construct - Constructs a new queue implemented using a doubly linked list
    • SplStack::setIteratorMode - Sets the mode of iteration
    • SplStack::__construct - Constructs a new stack implemented using a doubly linked list
    • SplSubject::attach - Attach an SplObserver
    • SplSubject::detach - Detach an observer
    • SplSubject::notify - Notify an observer
    • SplTempFileObject::__construct - Construct a new temporary file object
    • SplType::__construct - Creates a new value of some type
    • spl_autoload - Default implementation for __autoload()
    • spl_autoload_call - Try all registered __autoload() function to load the requested class
    • spl_autoload_extensions - Register and return default file extensions for spl_autoload
    • spl_autoload_functions - Return all registered __autoload() functions
    • spl_autoload_register - Register given function as __autoload() implementation
    • spl_autoload_unregister - Unregister given function as __autoload() implementation
    • spl_classes - Return available SPL classes
    • spl_object_hash - Return hash id for given object
    • Spoofchecker::areConfusable - Checks if a given text contains any confusable characters
    • Spoofchecker::isSuspicious - Checks if a given text contains any suspicious characters
    • Spoofchecker::setAllowedLocales - Locales to use when running checks
    • Spoofchecker::setChecks - Set the checks to run
    • Spoofchecker::__construct - Constructor
    • sprintf - 형식화한 문자열을 반환
    • SQL acceptable by 4D - PDO and SQL 4D
    • SQLite3::busyTimeout - Sets the busy connection handler
    • SQLite3::changes - Returns the number of database rows that were changed (or inserted or deleted) by the most recent SQL statement
    • SQLite3::close - Closes the database connection
    • SQLite3::createAggregate - Registers a PHP function for use as an SQL aggregate function
    • SQLite3::createCollation - Registers a PHP function for use as an SQL collating function
    • SQLite3::createFunction - Registers a PHP function for use as an SQL scalar function
    • SQLite3::enableExceptions - Enable throwing exceptions
    • SQLite3::escapeString - Returns a string that has been properly escaped
    • SQLite3::exec - Executes a result-less query against a given database
    • SQLite3::lastErrorCode - Returns the numeric result code of the most recent failed SQLite request
    • SQLite3::lastErrorMsg - Returns English text describing the most recent failed SQLite request
    • SQLite3::lastInsertRowID - Returns the row ID of the most recent INSERT into the database
    • SQLite3::loadExtension - Attempts to load an SQLite extension library
    • SQLite3::open - Opens an SQLite database
    • SQLite3::openBlob - Opens a stream resource to read a BLOB
    • SQLite3::prepare - Prepares an SQL statement for execution
    • SQLite3::query - Executes an SQL query
    • SQLite3::querySingle - Executes a query and returns a single result
    • SQLite3::version - Returns the SQLite3 library version as a string constant and as a number
    • SQLite3::__construct - Instantiates an SQLite3 object and opens an SQLite 3 database
    • SQLite3Result::columnName - Returns the name of the nth column
    • SQLite3Result::columnType - Returns the type of the nth column
    • SQLite3Result::fetchArray - Fetches a result row as an associative or numerically indexed array or both
    • SQLite3Result::finalize - Closes the result set
    • SQLite3Result::numColumns - Returns the number of columns in the result set
    • SQLite3Result::reset - Resets the result set back to the first row
    • SQLite3Stmt::bindParam - Binds a parameter to a statement variable
    • SQLite3Stmt::bindValue - Binds the value of a parameter to a statement variable
    • SQLite3Stmt::clear - Clears all current bound parameters
    • SQLite3Stmt::close - Closes the prepared statement
    • SQLite3Stmt::execute - Executes a prepared statement and returns a result set object
    • SQLite3Stmt::paramCount - Returns the number of parameters within the prepared statement
    • SQLite3Stmt::readOnly() - Returns whether a statement is definitely read only
    • SQLite3Stmt::reset - Resets the prepared statement
    • sqlite_array_query - Execute a query against a given database and returns an array
    • sqlite_busy_timeout - Set busy timeout duration, or disable busy handlers
    • sqlite_changes - Returns the number of rows that were changed by the most recent SQL statement
    • sqlite_close - Closes an open SQLite database
    • sqlite_column - Fetches a column from the current row of a result set
    • sqlite_create_aggregate - Register an aggregating UDF for use in SQL statements
    • sqlite_create_function - Registers a "regular" User Defined Function for use in SQL statements
    • sqlite_current - Fetches the current row from a result set as an array
    • sqlite_error_string - Returns the textual description of an error code
    • sqlite_escape_string - Escapes a string for use as a query parameter
    • sqlite_exec - Executes a result-less query against a given database
    • sqlite_factory - Opens an SQLite database and returns an SQLiteDatabase object
    • sqlite_fetch_all - Fetches all rows from a result set as an array of arrays
    • sqlite_fetch_array - Fetches the next row from a result set as an array
    • sqlite_fetch_column_types - Return an array of column types from a particular table
    • sqlite_fetch_object - Fetches the next row from a result set as an object
    • sqlite_fetch_single - Fetches the first column of a result set as a string
    • sqlite_fetch_string - 별칭: sqlite_fetch_single
    • sqlite_field_name - Returns the name of a particular field
    • sqlite_has_more - Finds whether or not more rows are available
    • sqlite_has_prev - Returns whether or not a previous row is available
    • sqlite_key - Returns the current row index
    • sqlite_last_error - Returns the error code of the last error for a database
    • sqlite_last_insert_rowid - Returns the rowid of the most recently inserted row
    • sqlite_libencoding - Returns the encoding of the linked SQLite library
    • sqlite_libversion - Returns the version of the linked SQLite library
    • sqlite_next - Seek to the next row number
    • sqlite_num_fields - Returns the number of fields in a result set
    • sqlite_num_rows - Returns the number of rows in a buffered result set
    • sqlite_open - Opens an SQLite database and create the database if it does not exist
    • sqlite_popen - Opens a persistent handle to an SQLite database and create the database if it does not exist
    • sqlite_prev - Seek to the previous row number of a result set
    • sqlite_query - Executes a query against a given database and returns a result handle
    • sqlite_rewind - Seek to the first row number
    • sqlite_seek - Seek to a particular row number of a buffered result set
    • sqlite_single_query - Executes a query and returns either an array for one single column or the value of the first row
    • sqlite_udf_decode_binary - Decode binary data passed as parameters to an UDF
    • sqlite_udf_encode_binary - Encode binary data before returning it from an UDF
    • sqlite_unbuffered_query - Execute a query that does not prefetch and buffer all data
    • sqlite_valid - Returns whether more rows are available
    • sqlsrv_begin_transaction - Begins a database transaction
    • sqlsrv_cancel - Cancels a statement
    • sqlsrv_client_info - Returns information about the client and specified connection
    • sqlsrv_close - Closes an open connection and releases resourses associated with the connection
    • sqlsrv_commit - Commits a transaction that was begun with sqlsrv_begin_transaction
    • sqlsrv_configure - Changes the driver error handling and logging configurations
    • sqlsrv_connect - Opens a connection to a Microsoft SQL Server database
    • sqlsrv_errors - Returns error and warning information about the last SQLSRV operation performed
    • sqlsrv_execute - Executes a statement prepared with sqlsrv_prepare
    • sqlsrv_fetch - Makes the next row in a result set available for reading
    • sqlsrv_fetch_array - Returns a row as an array
    • sqlsrv_fetch_object - Retrieves the next row of data in a result set as an object
    • sqlsrv_field_metadata - Retrieves metadata for the fields of a statement prepared by sqlsrv_prepare or sqlsrv_query
    • sqlsrv_free_stmt - Frees all resources for the specified statement
    • sqlsrv_get_config - Returns the value of the specified configuration setting
    • sqlsrv_get_field - Gets field data from the currently selected row
    • sqlsrv_has_rows - Indicates whether the specified statement has rows
    • sqlsrv_next_result - Makes the next result of the specified statement active
    • sqlsrv_num_fields - Retrieves the number of fields (columns) on a statement
    • sqlsrv_num_rows - Retrieves the number of rows in a result set
    • sqlsrv_prepare - Prepares a query for execution
    • sqlsrv_query - Prepares and executes a query.
    • sqlsrv_rollback - Rolls back a transaction that was begun with sqlsrv_begin_transaction
    • sqlsrv_rows_affected - Returns the number of rows modified by the last INSERT, UPDATE, or DELETE query executed
    • sqlsrv_send_stream_data - Sends data from parameter streams to the server
    • sqlsrv_server_info - Returns information about the server
    • SQL types with PDO_4D and PHP - SQL types with PDO_4D and PHP
    • sql_regcase - Make regular expression for case insensitive match
    • sqrt - 평방 제곱근
    • srand - 난수 생성기를 초기화
    • sscanf - 문자열을 형식에 따라 해석
    • ssdeep_fuzzy_compare - Calculates the match score between two fuzzy hash signatures
    • ssdeep_fuzzy_hash - Create a fuzzy hash from a string
    • ssdeep_fuzzy_hash_filename - Create a fuzzy hash from a file
    • ssh2:// - Secure Shell 2
    • ssh2_auth_agent - Authenticate over SSH using the ssh agent
    • ssh2_auth_hostbased_file - Authenticate using a public hostkey
    • ssh2_auth_none - Authenticate as "none"
    • ssh2_auth_password - Authenticate over SSH using a plain password
    • ssh2_auth_pubkey_file - Authenticate using a public key
    • ssh2_connect - Connect to an SSH server
    • ssh2_exec - Execute a command on a remote server
    • ssh2_fetch_stream - Fetch an extended data stream
    • ssh2_fingerprint - Retrieve fingerprint of remote server
    • ssh2_methods_negotiated - Return list of negotiated methods
    • ssh2_publickey_add - Add an authorized publickey
    • ssh2_publickey_init - Initialize Publickey subsystem
    • ssh2_publickey_list - List currently authorized publickeys
    • ssh2_publickey_remove - Remove an authorized publickey
    • ssh2_scp_recv - Request a file via SCP
    • ssh2_scp_send - Send a file via SCP
    • ssh2_sftp - Initialize SFTP subsystem
    • ssh2_sftp_chmod - Changes file mode
    • ssh2_sftp_lstat - Stat a symbolic link
    • ssh2_sftp_mkdir - Create a directory
    • ssh2_sftp_readlink - Return the target of a symbolic link
    • ssh2_sftp_realpath - Resolve the realpath of a provided path string
    • ssh2_sftp_rename - Rename a remote file
    • ssh2_sftp_rmdir - Remove a directory
    • ssh2_sftp_stat - Stat a file on a remote filesystem
    • ssh2_sftp_symlink - Create a symlink
    • ssh2_sftp_unlink - Delete a file
    • ssh2_shell - Request an interactive shell
    • ssh2_tunnel - Open a tunnel through a remote server
    • SSL context options - SSL context option listing
    • stat - Gives information about a file
    • stats_absolute_deviation - Returns the absolute deviation of an array of values
    • stats_cdf_beta - CDF function for BETA Distribution. Calculates any one parameter of the beta distribution given values for the others.
    • stats_cdf_binomial - Calculates any one parameter of the binomial distribution given values for the others.
    • stats_cdf_cauchy - Not documented
    • stats_cdf_chisquare - Calculates any one parameter of the chi-square distribution given values for the others.
    • stats_cdf_exponential - Not documented
    • stats_cdf_f - Calculates any one parameter of the F distribution given values for the others.
    • stats_cdf_gamma - Calculates any one parameter of the gamma distribution given values for the others.
    • stats_cdf_laplace - Not documented
    • stats_cdf_logistic - Not documented
    • stats_cdf_negative_binomial - Calculates any one parameter of the negative binomial distribution given values for the others.
    • stats_cdf_noncentral_chisquare - Calculates any one parameter of the non-central chi-square distribution given values for the others.
    • stats_cdf_noncentral_f - Calculates any one parameter of the Non-central F distribution given values for the others.
    • stats_cdf_poisson - Calculates any one parameter of the Poisson distribution given values for the others.
    • stats_cdf_t - Calculates any one parameter of the T distribution given values for the others.
    • stats_cdf_uniform - Not documented
    • stats_cdf_weibull - Not documented
    • stats_covariance - Computes the covariance of two data sets
    • stats_dens_beta - Not documented
    • stats_dens_cauchy - Not documented
    • stats_dens_chisquare - Not documented
    • stats_dens_exponential - Not documented
    • stats_dens_f - 설명
    • stats_dens_gamma - Not documented
    • stats_dens_laplace - Not documented
    • stats_dens_logistic - Not documented
    • stats_dens_negative_binomial - Not documented
    • stats_dens_normal - Not documented
    • stats_dens_pmf_binomial - Not documented
    • stats_dens_pmf_hypergeometric - 설명
    • stats_dens_pmf_poisson - Not documented
    • stats_dens_t - Not documented
    • stats_dens_weibull - Not documented
    • stats_den_uniform - Not documented
    • stats_harmonic_mean - Returns the harmonic mean of an array of values
    • stats_kurtosis - Computes the kurtosis of the data in the array
    • stats_rand_gen_beta - Generates beta random deviate
    • stats_rand_gen_chisquare - Generates random deviate from the distribution of a chisquare with "df" degrees of freedom random variable.
    • stats_rand_gen_exponential - Generates a single random deviate from an exponential distribution with mean "av"
    • stats_rand_gen_f - Generates a random deviate
    • stats_rand_gen_funiform - Generates uniform float between low (exclusive) and high (exclusive)
    • stats_rand_gen_gamma - Generates random deviates from a gamma distribution
    • stats_rand_gen_ibinomial - Generates a single random deviate from a binomial distribution whose number of trials is "n" (n >= 0) and whose probability of an event in each trial is "pp" ([0;1]). Method : algorithm BTPE
    • stats_rand_gen_ibinomial_negative - Generates a single random deviate from a negative binomial distribution. Arguments : n - the number of trials in the negative binomial distribution from which a random deviate is to be generated (n > 0), p - the probability of an event (0 < p < 1)).
    • stats_rand_gen_int - Generates random integer between 1 and 2147483562
    • stats_rand_gen_ipoisson - Generates a single random deviate from a Poisson distribution with mean "mu" (mu >= 0.0).
    • stats_rand_gen_iuniform - Generates integer uniformly distributed between LOW (inclusive) and HIGH (inclusive)
    • stats_rand_gen_noncenral_chisquare - Generates random deviate from the distribution of a noncentral chisquare with "df" degrees of freedom and noncentrality parameter "xnonc". d must be >= 1.0, xnonc must >= 0.0
    • stats_rand_gen_noncentral_f - Generates a random deviate from the noncentral F (variance ratio) distribution with "dfn" degrees of freedom in the numerator, and "dfd" degrees of freedom in the denominator, and noncentrality parameter "xnonc". Method : directly generates ratio of noncentral numerator chisquare variate to central denominator chisquare variate.
    • stats_rand_gen_noncentral_t - Generates a single random deviate from a noncentral T distribution
    • stats_rand_gen_normal - Generates a single random deviate from a normal distribution with mean, av, and standard deviation, sd (sd >= 0). Method : Renames SNORM from TOMS as slightly modified by BWB to use RANF instead of SUNIF.
    • stats_rand_gen_t - Generates a single random deviate from a T distribution
    • stats_rand_get_seeds - Not documented
    • stats_rand_phrase_to_seeds - generate two seeds for the RGN random number generator
    • stats_rand_ranf - Returns a random floating point number from a uniform distribution over 0 - 1 (endpoints of this interval are not returned) using the current generator
    • stats_rand_setall - Not documented
    • stats_skew - Computes the skewness of the data in the array
    • stats_standard_deviation - Returns the standard deviation
    • stats_stat_binomial_coef - Not documented
    • stats_stat_correlation - Not documented
    • stats_stat_gennch - Not documented
    • stats_stat_independent_t - Not documented
    • stats_stat_innerproduct - 설명
    • stats_stat_noncentral_t - Calculates any one parameter of the noncentral t distribution give values for the others.
    • stats_stat_paired_t - Not documented
    • stats_stat_percentile - Not documented
    • stats_stat_powersum - Not documented
    • stats_variance - Returns the population variance
    • Stomp::abort - Rolls back a transaction in progress
    • Stomp::ack - Acknowledges consumption of a message
    • Stomp::begin - Starts a transaction
    • Stomp::commit - Commits a transaction in progress
    • Stomp::error - Gets the last stomp error
    • Stomp::getReadTimeout - Gets read timeout
    • Stomp::getSessionId - Gets the current stomp session ID
    • Stomp::hasFrame - Indicates whether or not there is a frame ready to read
    • Stomp::readFrame - Reads the next frame
    • Stomp::send - Sends a message
    • Stomp::setReadTimeout - Sets read timeout
    • Stomp::subscribe - Registers to listen to a given destination
    • Stomp::unsubscribe - Removes an existing subscription
    • Stomp::__construct - Opens a connection
    • Stomp::__destruct - Closes stomp connection
    • StompException::getDetails - Get exception details
    • StompFrame::__construct - Constructor
    • stomp_connect_error - Returns a string description of the last connect error
    • stomp_version - Gets the current stomp extension version
    • strcasecmp - 대소문자 구분 없는 바이너리 안전 문자열 비교
    • strchr - 별칭: strstr
    • strcmp - 바이너리 안전 문자열 비교
    • strcoll - 로케일 기반 문자열 비교
    • strcspn - 마스크에 매칭하지 않는 처음 세그먼트의 길이를 찾습니다
    • streamWrapper::dir_closedir - Close directory handle
    • streamWrapper::dir_opendir - Open directory handle
    • streamWrapper::dir_readdir - Read entry from directory handle
    • streamWrapper::dir_rewinddir - Rewind directory handle
    • streamWrapper::mkdir - Create a directory
    • streamWrapper::rename - Renames a file or directory
    • streamWrapper::rmdir - Removes a directory
    • streamWrapper::stream_cast - Retrieve the underlaying resource
    • streamWrapper::stream_close - Close a resource
    • streamWrapper::stream_eof - Tests for end-of-file on a file pointer
    • streamWrapper::stream_flush - Flushes the output
    • streamWrapper::stream_lock - Advisory file locking
    • streamWrapper::stream_metadata - Change stream metadata
    • streamWrapper::stream_open - Opens file or URL
    • streamWrapper::stream_read - Read from stream
    • streamWrapper::stream_seek - Seeks to specific location in a stream
    • streamWrapper::stream_set_option - Change stream options
    • streamWrapper::stream_stat - Retrieve information about a file resource
    • streamWrapper::stream_tell - Retrieve the current position of a stream
    • streamWrapper::stream_truncate - Truncate stream
    • streamWrapper::stream_write - Write to stream
    • streamWrapper::unlink - Delete a file
    • streamWrapper::url_stat - Retrieve information about a file
    • streamWrapper::__construct - Constructs a new stream wrapper
    • streamWrapper::__destruct - Destructs an existing stream wrapper
    • stream_bucket_append - Append bucket to brigade
    • stream_bucket_make_writeable - Return a bucket object from the brigade for operating on
    • stream_bucket_new - Create a new bucket for use on the current stream
    • stream_bucket_prepend - Prepend bucket to brigade
    • stream_context_create - Creates a stream context
    • stream_context_get_default - Retrieve the default stream context
    • stream_context_get_options - Retrieve options for a stream/wrapper/context
    • stream_context_get_params - Retrieves parameters from a context
    • stream_context_set_default - Set the default stream context
    • stream_context_set_option - Sets an option for a stream/wrapper/context
    • stream_context_set_params - Set parameters for a stream/wrapper/context
    • stream_copy_to_stream - Copies data from one stream to another
    • stream_encoding - Set character set for stream encoding
    • stream_filter_append - Attach a filter to a stream
    • stream_filter_prepend - Attach a filter to a stream
    • stream_filter_register - Register a user defined stream filter
    • stream_filter_remove - Remove a filter from a stream
    • stream_get_contents - Reads remainder of a stream into a string
    • stream_get_filters - Retrieve list of registered filters
    • stream_get_line - Gets line from stream resource up to a given delimiter
    • stream_get_meta_data - Retrieves header/meta data from streams/file pointers
    • stream_get_transports - Retrieve list of registered socket transports
    • stream_get_wrappers - Retrieve list of registered streams
    • stream_is_local - Checks if a stream is a local stream
    • stream_notification_callback - A callback function for the notification context parameter
    • stream_register_wrapper - 별칭: stream_wrapper_register
    • stream_resolve_include_path - Resolve filename against the include path
    • stream_select - Runs the equivalent of the select() system call on the given arrays of streams with a timeout specified by tv_sec and tv_usec
    • stream_set_blocking - Set blocking/non-blocking mode on a stream
    • stream_set_chunk_size - Set the stream chunk size
    • stream_set_read_buffer - Set read file buffering on the given stream
    • stream_set_timeout - Set timeout period on a stream
    • stream_set_write_buffer - Sets write file buffering on the given stream
    • stream_socket_accept - Accept a connection on a socket created by stream_socket_server
    • stream_socket_client - Open Internet or Unix domain socket connection
    • stream_socket_enable_crypto - Turns encryption on/off on an already connected socket
    • stream_socket_get_name - Retrieve the name of the local or remote sockets
    • stream_socket_pair - Creates a pair of connected, indistinguishable socket streams
    • stream_socket_recvfrom - Receives data from a socket, connected or not
    • stream_socket_sendto - Sends a message to a socket, whether it is connected or not
    • stream_socket_server - Create an Internet or Unix domain server socket
    • stream_socket_shutdown - Shutdown a full-duplex connection
    • stream_supports_lock - Tells whether the stream supports locking.
    • stream_wrapper_register - Register a URL wrapper implemented as a PHP class
    • stream_wrapper_restore - Restores a previously unregistered built-in wrapper
    • stream_wrapper_unregister - Unregister a URL wrapper
    • strftime - Format a local time/date according to locale settings
    • stripcslashes - addcslashes로 인용한 문자열을 되돌립니다
    • stripos - 대소문자를 구분 없이 문자열이 처음 나타나는 위치를 탐색
    • stripslashes - 따옴표 처리한 문자열을 풉니다
    • strip_tags - 문자열에서 HTML과 PHP 태그를 제거
    • stristr - 대소문자를 구분하지 않는 strstr
    • strlen - 문자열 길이를 얻습니다
    • strnatcasecmp - "자연순" 알고리즘을 이용한 대소문자 구분 없는 문자열 비교
    • strnatcmp - "자연순" 알고리즘을 이용한 문자열 비교
    • strncasecmp - 대소문자 구분 없는 처음 n 문자의 바이너리 안전 문자열 비교
    • strncmp - 처음 n 문자의 바이너리 안전 문자열 비교
    • strpbrk - Search a string for any of a set of characters
    • strpos - 문자열이 처음 나타나는 위치를 찾습니다
    • strptime - Parse a time/date generated with strftime
    • strrchr - 문자열에서 문자가 마지막으로 나오는 부분을 찾습니다
    • strrev - 문자열 뒤집기
    • strripos - 문자열에서 대소문자 구분 없이 문자열의 마지막 위치를 찾습니다
    • strrpos - 문자열에서 마지막 문자의 위치를 찾습니다
    • strspn - 마스크에 매치하는 초기 세그먼트의 길이를 찾는다
    • strstr - 문자열이 처음으로 나오는 부분을 찾습니다
    • strtok - 문자열을 토큰화
    • strtolower - 문자열을 소문자로 만듭니다
    • strtotime - Parse about any English textual datetime description into a Unix timestamp
    • strtoupper - 문자열을 대문자로 만듭니다
    • strtr - Translate characters or replace substrings
    • strval - 변수의 문자열 값을 얻습니다
    • str_getcsv - Parse a CSV string into an array
    • str_ireplace - 대소문자 구분 없는 str_replace
    • str_pad - 문자열을 지정한 길이가 되도록 다른 문자열로 채웁니다
    • str_repeat - 문자열을 반복
    • str_replace - 발견한 모든 검색 문자열을 치환 문자열로 교체
    • str_rot13 - 문자열에 rot13 변환을 수행
    • str_shuffle - 문자열을 무작위로 섞습니다
    • str_split - 문자열을 배열로 변환
    • str_word_count - 문자열에서 사용한 단어에 대한 정보를 반환
    • substr - Return part of a string
    • substr_compare - Binary safe comparison of two strings from an offset, up to length characters
    • substr_count - Count the number of substring occurrences
    • substr_replace - Replace text within a portion of a string
    • SVM::crossvalidate - Test training params on subsets of the training data.
    • SVM::getOptions - Return the current training parameters
    • SVM::setOptions - Set training parameters
    • SVM::train - Create a SVMModel based on training data
    • SVM::__construct - Construct a new SVM object
    • SVMModel::checkProbabilityModel - Returns true if the model has probability information
    • SVMModel::getLabels - Get the labels the model was trained on
    • SVMModel::getNrClass - Returns the number of classes the model was trained with
    • SVMModel::getSvmType - Get the SVM type the model was trained with
    • SVMModel::getSvrProbability - Get the sigma value for regression types
    • SVMModel::load - Load a saved SVM Model
    • SVMModel::predict - Predict a value for previously unseen data
    • SVMModel::predict_probability - Return class probabilities for previous unseen data
    • SVMModel::save - Save a model to a file
    • SVMModel::__construct - Construct a new SVMModel
    • svn_add - Schedules the addition of an item in a working directory
    • svn_auth_get_parameter - Retrieves authentication parameter
    • svn_auth_set_parameter - Sets an authentication parameter
    • svn_blame - Get the SVN blame for a file
    • svn_cat - Returns the contents of a file in a repository
    • svn_checkout - Checks out a working copy from the repository
    • svn_cleanup - Recursively cleanup a working copy directory, finishing incomplete operations and removing locks
    • svn_client_version - Returns the version of the SVN client libraries
    • svn_commit - Sends changes from the local working copy to the repository
    • svn_delete - Delete items from a working copy or repository.
    • svn_diff - Recursively diffs two paths
    • svn_export - Export the contents of a SVN directory
    • svn_fs_abort_txn - Abort a transaction, returns true if everything is okay, false otherwise
    • svn_fs_apply_text - Creates and returns a stream that will be used to replace
    • svn_fs_begin_txn2 - Create a new transaction
    • svn_fs_change_node_prop - Return true if everything is ok, false otherwise
    • svn_fs_check_path - Determines what kind of item lives at path in a given repository fsroot
    • svn_fs_contents_changed - Return true if content is different, false otherwise
    • svn_fs_copy - Copies a file or a directory, returns true if all is ok, false otherwise
    • svn_fs_delete - Deletes a file or a directory, return true if all is ok, false otherwise
    • svn_fs_dir_entries - Enumerates the directory entries under path; returns a hash of dir names to file type
    • svn_fs_file_contents - Returns a stream to access the contents of a file from a given version of the fs
    • svn_fs_file_length - Returns the length of a file from a given version of the fs
    • svn_fs_is_dir - Return true if the path points to a directory, false otherwise
    • svn_fs_is_file - Return true if the path points to a file, false otherwise
    • svn_fs_make_dir - Creates a new empty directory, returns true if all is ok, false otherwise
    • svn_fs_make_file - Creates a new empty file, returns true if all is ok, false otherwise
    • svn_fs_node_created_rev - Returns the revision in which path under fsroot was created
    • svn_fs_node_prop - Returns the value of a property for a node
    • svn_fs_props_changed - Return true if props are different, false otherwise
    • svn_fs_revision_prop - Fetches the value of a named property
    • svn_fs_revision_root - Get a handle on a specific version of the repository root
    • svn_fs_txn_root - Creates and returns a transaction root
    • svn_fs_youngest_rev - Returns the number of the youngest revision in the filesystem
    • svn_import - Imports an unversioned path into a repository
    • svn_log - Returns the commit log messages of a repository URL
    • svn_ls - Returns list of directory contents in repository URL, optionally at revision number
    • svn_mkdir - Creates a directory in a working copy or repository
    • svn_repos_create - Create a new subversion repository at path
    • svn_repos_fs - Gets a handle on the filesystem for a repository
    • svn_repos_fs_begin_txn_for_commit - Create a new transaction
    • svn_repos_fs_commit_txn - Commits a transaction and returns the new revision
    • svn_repos_hotcopy - Make a hot-copy of the repos at repospath; copy it to destpath
    • svn_repos_open - Open a shared lock on a repository.
    • svn_repos_recover - Run recovery procedures on the repository located at path.
    • svn_revert - Revert changes to the working copy
    • svn_status - Returns the status of working copy files and directories
    • svn_update - Update working copy
    • SWFAction::__construct - Creates a new SWFAction
    • SWFBitmap::getHeight - Returns the bitmap's height
    • SWFBitmap::getWidth - Returns the bitmap's width
    • SWFBitmap::__construct - Loads Bitmap object
    • SWFButton::addAction - Adds an action
    • SWFButton::addASound - Associates a sound with a button transition
    • SWFButton::addShape - Adds a shape to a button
    • SWFButton::setAction - Sets the action
    • SWFButton::setDown - Alias for addShape(shape, SWFBUTTON_DOWN)
    • SWFButton::setHit - Alias for addShape(shape, SWFBUTTON_HIT)
    • SWFButton::setMenu - enable track as menu button behaviour
    • SWFButton::setOver - Alias for addShape(shape, SWFBUTTON_OVER)
    • SWFButton::setUp - Alias for addShape(shape, SWFBUTTON_UP)
    • SWFButton::__construct - Creates a new Button
    • SWFDisplayItem::addAction - Adds this SWFAction to the given SWFSprite instance
    • SWFDisplayItem::addColor - Adds the given color to this item's color transform
    • SWFDisplayItem::endMask - Another way of defining a MASK layer
    • SWFDisplayItem::getRot - 설명
    • SWFDisplayItem::getX - 설명
    • SWFDisplayItem::getXScale - 설명
    • SWFDisplayItem::getXSkew - 설명
    • SWFDisplayItem::getY - 설명
    • SWFDisplayItem::getYScale - 설명
    • SWFDisplayItem::getYSkew - 설명
    • SWFDisplayItem::move - Moves object in relative coordinates
    • SWFDisplayItem::moveTo - Moves object in global coordinates
    • SWFDisplayItem::multColor - Multiplies the item's color transform
    • SWFDisplayItem::remove - Removes the object from the movie
    • SWFDisplayItem::rotate - Rotates in relative coordinates
    • SWFDisplayItem::rotateTo - Rotates the object in global coordinates
    • SWFDisplayItem::scale - Scales the object in relative coordinates
    • SWFDisplayItem::scaleTo - Scales the object in global coordinates
    • SWFDisplayItem::setDepth - Sets z-order
    • SWFDisplayItem::setMaskLevel - Defines a MASK layer at level
    • SWFDisplayItem::setMatrix - Sets the item's transform matrix
    • SWFDisplayItem::setName - Sets the object's name
    • SWFDisplayItem::setRatio - Sets the object's ratio
    • SWFDisplayItem::skewX - Sets the X-skew
    • SWFDisplayItem::skewXTo - Sets the X-skew
    • SWFDisplayItem::skewY - Sets the Y-skew
    • SWFDisplayItem::skewYTo - Sets the Y-skew
    • SWFFill::moveTo - Moves fill origin
    • SWFFill::rotateTo - Sets fill's rotation
    • SWFFill::scaleTo - Sets fill's scale
    • SWFFill::skewXTo - Sets fill x-skew
    • SWFFill::skewYTo - Sets fill y-skew
    • SWFFont::getAscent - Returns the ascent of the font, or 0 if not available
    • SWFFont::getDescent - Returns the descent of the font, or 0 if not available
    • SWFFont::getLeading - Returns the leading of the font, or 0 if not available
    • SWFFont::getShape - Returns the glyph shape of a char as a text string
    • SWFFont::getUTF8Width - Calculates the width of the given string in this font at full height
    • SWFFont::getWidth - Returns the string's width
    • SWFFont::__construct - Loads a font definition
    • SWFFontChar::addChars - Adds characters to a font for exporting font
    • SWFFontChar::addUTF8Chars - Adds characters to a font for exporting font
    • SWFGradient::addEntry - Adds an entry to the gradient list
    • SWFGradient::__construct - Creates a gradient object
    • SWFMorph::getShape1 - Gets a handle to the starting shape
    • SWFMorph::getShape2 - Gets a handle to the ending shape
    • SWFMorph::__construct - Creates a new SWFMorph object
    • SWFMovie::add - Adds any type of data to a movie
    • SWFMovie::addExport - 설명
    • SWFMovie::addFont - 설명
    • SWFMovie::importChar - 설명
    • SWFMovie::importFont - 설명
    • SWFMovie::labelFrame - Labels a frame
    • SWFMovie::nextFrame - Moves to the next frame of the animation
    • SWFMovie::output - Dumps your lovingly prepared movie out
    • SWFMovie::remove - Removes the object instance from the display list
    • SWFMovie::save - Saves the SWF movie in a file
    • SWFMovie::saveToFile - 설명
    • SWFMovie::setbackground - Sets the background color
    • SWFMovie::setDimension - Sets the movie's width and height
    • SWFMovie::setFrames - Sets the total number of frames in the animation
    • SWFMovie::setRate - Sets the animation's frame rate
    • SWFMovie::startSound - 설명
    • SWFMovie::stopSound - 설명
    • SWFMovie::streamMP3 - Streams a MP3 file
    • SWFMovie::writeExports - 설명
    • SWFMovie::__construct - Creates a new movie object, representing an SWF version 4 movie
    • SWFPrebuiltClip::__construct - Returns a SWFPrebuiltClip object
    • SWFShape::addFill - Adds a solid fill to the shape
    • SWFShape::drawArc - Draws an arc of radius r centered at the current location, from angle startAngle to angle endAngle measured clockwise from 12 o'clock
    • SWFShape::drawCircle - Draws a circle of radius r centered at the current location, in a counter-clockwise fashion
    • SWFShape::drawCubic - Draws a cubic bezier curve using the current position and the three given points as control points
    • SWFShape::drawCubicTo - Draws a cubic bezier curve using the current position and the three given points as control points
    • SWFShape::drawCurve - Draws a curve (relative)
    • SWFShape::drawCurveTo - Draws a curve
    • SWFShape::drawGlyph - Draws the first character in the given string into the shape using the glyph definition from the given font
    • SWFShape::drawLine - Draws a line (relative)
    • SWFShape::drawLineTo - Draws a line
    • SWFShape::movePen - Moves the shape's pen (relative)
    • SWFShape::movePenTo - Moves the shape's pen
    • SWFShape::setLeftFill - Sets left rasterizing color
    • SWFShape::setLine - Sets the shape's line style
    • SWFShape::setRightFill - Sets right rasterizing color
    • SWFShape::__construct - Creates a new shape object
    • SWFSound::__construct - Returns a new SWFSound object from given file
    • SWFSoundInstance::loopCount - 설명
    • SWFSoundInstance::loopInPoint - 설명
    • SWFSoundInstance::loopOutPoint - 설명
    • SWFSoundInstance::noMultiple - 설명
    • SWFSprite::add - Adds an object to a sprite
    • SWFSprite::labelFrame - Labels frame
    • SWFSprite::nextFrame - Moves to the next frame of the animation
    • SWFSprite::remove - Removes an object to a sprite
    • SWFSprite::setFrames - Sets the total number of frames in the animation
    • SWFSprite::startSound - 설명
    • SWFSprite::stopSound - 설명
    • SWFSprite::__construct - Creates a movie clip (a sprite)
    • SWFText::addString - Draws a string
    • SWFText::addUTF8String - Writes the given text into this SWFText object at the current pen position, using the current font, height, spacing, and color
    • SWFText::getAscent - Returns the ascent of the current font at its current size, or 0 if not available
    • SWFText::getDescent - Returns the descent of the current font at its current size, or 0 if not available
    • SWFText::getLeading - Returns the leading of the current font at its current size, or 0 if not available
    • SWFText::getUTF8Width - calculates the width of the given string in this text objects current font and size
    • SWFText::getWidth - Computes string's width
    • SWFText::moveTo - Moves the pen
    • SWFText::setColor - Sets the current text color
    • SWFText::setFont - Sets the current font
    • SWFText::setHeight - Sets the current font height
    • SWFText::setSpacing - Sets the current font spacing
    • SWFText::__construct - Creates a new SWFText object
    • SWFTextField::addChars - adds characters to a font that will be available within a textfield
    • SWFTextField::addString - Concatenates the given string to the text field
    • SWFTextField::align - Sets the text field alignment
    • SWFTextField::setBounds - Sets the text field width and height
    • SWFTextField::setColor - Sets the color of the text field
    • SWFTextField::setFont - Sets the text field font
    • SWFTextField::setHeight - Sets the font height of this text field font
    • SWFTextField::setIndentation - Sets the indentation of the first line
    • SWFTextField::setLeftMargin - Sets the left margin width of the text field
    • SWFTextField::setLineSpacing - Sets the line spacing of the text field
    • SWFTextField::setMargins - Sets the margins width of the text field
    • SWFTextField::setName - Sets the variable name
    • SWFTextField::setPadding - Sets the padding of this textfield
    • SWFTextField::setRightMargin - Sets the right margin width of the text field
    • SWFTextField::__construct - Creates a text field object
    • SWFVideoStream::getNumFrames - Returns the number of frames in the video
    • SWFVideoStream::setDimension - Sets video dimension
    • SWFVideoStream::__construct - Returns a SWFVideoStream object
    • Swish::getMetaList - Get the list of meta entries for the index
    • Swish::getPropertyList - Get the list of properties for the index
    • Swish::prepare - Prepare a search query
    • Swish::query - Execute a query and return results object
    • Swish::__construct - Construct a Swish object
    • SwishResult::getMetaList - Get a list of meta entries
    • SwishResult::stem - Stems the given word
    • SwishResults::getParsedWords - Get an array of parsed words
    • SwishResults::getRemovedStopwords - Get an array of stopwords removed from the query
    • SwishResults::nextResult - Get the next search result
    • SwishResults::seekResult - Set current seek pointer to the given position
    • SwishSearch::execute - Execute the search and get the results
    • SwishSearch::resetLimit - Reset the search limits
    • SwishSearch::setLimit - Set the search limits
    • SwishSearch::setPhraseDelimiter - Set the phrase delimiter
    • SwishSearch::setSort - Set the sort order
    • SwishSearch::setStructure - Set the structure flag in the search object
    • sybase_affected_rows - Gets number of affected rows in last query
    • sybase_close - Closes a Sybase connection
    • sybase_connect - Opens a Sybase server connection
    • sybase_data_seek - Moves internal row pointer
    • sybase_deadlock_retry_count - Sets the deadlock retry count
    • sybase_fetch_array - Fetch row as array
    • sybase_fetch_assoc - Fetch a result row as an associative array
    • sybase_fetch_field - Get field information from a result
    • sybase_fetch_object - Fetch a row as an object
    • sybase_fetch_row - Get a result row as an enumerated array
    • sybase_field_seek - Sets field offset
    • sybase_free_result - Frees result memory
    • sybase_get_last_message - Returns the last message from the server
    • sybase_min_client_severity - Sets minimum client severity
    • sybase_min_error_severity - Sets minimum error severity
    • sybase_min_message_severity - Sets minimum message severity
    • sybase_min_server_severity - Sets minimum server severity
    • sybase_num_fields - Gets the number of fields in a result set
    • sybase_num_rows - Get number of rows in a result set
    • sybase_pconnect - Open persistent Sybase connection
    • sybase_query - Sends a Sybase query
    • sybase_result - Get result data
    • sybase_select_db - Selects a Sybase database
    • sybase_set_message_handler - Sets the handler called when a server message is raised
    • sybase_unbuffered_query - Send a Sybase query and do not block
    • symlink - Creates a symbolic link
    • SyncEvent::fire - Fires/sets the event
    • SyncEvent::reset - Resets a manual event
    • SyncEvent::wait - Waits for the event to be fired/set
    • SyncEvent::__construct - Constructs a new SyncEvent object
    • SyncMutex::lock - Waits for an exclusive lock
    • SyncMutex::unlock - Unlocks the mutex
    • SyncMutex::__construct - Constructs a new SyncMutex object
    • SyncReaderWriter::readlock - Waits for a read lock
    • SyncReaderWriter::readunlock - Releases a read lock
    • SyncReaderWriter::writelock - Waits for an exclusive write lock
    • SyncReaderWriter::writeunlock - Releases a write lock
    • SyncReaderWriter::__construct - Constructs a new SyncReaderWriter object
    • SyncSemaphore::lock - Decreases the count of the semaphore or waits
    • SyncSemaphore::unlock - Increases the count of the semaphore
    • SyncSemaphore::__construct - Constructs a new SyncSemaphore object
    • syslog - Generate a system log message
    • system - Execute an external program and display the output
    • sys_getloadavg - Gets system load average
    • sys_get_temp_dir - Returns directory path used for temporary files
  • t
    • taint - Taint a string
    • tan - 탄젠트
    • tanh - 쌍곡탄젠트
    • tcpwrap_check - Performs a tcpwrap check
    • tempnam - Create file with unique file name
    • textdomain - Sets the default domain
    • Thread::detach - Execution
    • Thread::getCreatorId - Identification
    • Thread::getCurrentThread - Identification
    • Thread::getCurrentThreadId - Identification
    • Thread::getThreadId - Identification
    • Thread::globally - Execution
    • Thread::isJoined - State Detection
    • Thread::isStarted - State Detection
    • Thread::join - Synchronization
    • Thread::kill - Execution
    • Thread::start - Execution
    • Threaded::chunk - Manipulation
    • Threaded::count - Manipulation
    • Threaded::extend - Runtime Manipulation
    • Threaded::from - Creation
    • Threaded::getTerminationInfo - Error Detection
    • Threaded::isRunning - State Detection
    • Threaded::isTerminated - State Detection
    • Threaded::isWaiting - State Detection
    • Threaded::lock - Synchronization
    • Threaded::merge - Manipulation
    • Threaded::notify - Synchronization
    • Threaded::pop - Manipulation
    • Threaded::run - Execution
    • Threaded::shift - Manipulation
    • Threaded::synchronized - Synchronization
    • Threaded::unlock - Synchronization
    • Threaded::wait - Synchronization
    • Throwable::getCode - Gets the exception code
    • Throwable::getFile - Gets the file in which the exception occurred
    • Throwable::getLine - Gets the line on which the object was instantiated
    • Throwable::getMessage - Gets the message
    • Throwable::getPrevious - Returns the previous Throwable
    • Throwable::getTrace - Gets the stack trace
    • Throwable::getTraceAsString - Gets the stack trace as a string
    • Throwable::__toString - Gets a string representation of the thrown object
    • tidy::$errorBuffer - Return warnings and errors which occurred parsing the specified document
    • tidy::body - Returns a tidyNode object starting from the <body> tag of the tidy parse tree
    • tidy::cleanRepair - Execute configured cleanup and repair operations on parsed markup
    • tidy::diagnose - Run configured diagnostics on parsed and repaired markup
    • tidy::getConfig - Get current Tidy configuration
    • tidy::getHtmlVer - Get the Detected HTML version for the specified document
    • tidy::getOpt - Returns the value of the specified configuration option for the tidy document
    • tidy::getOptDoc - Returns the documentation for the given option name
    • tidy::getRelease - Get release date (version) for Tidy library
    • tidy::getStatus - Get status of specified document
    • tidy::head - Returns a tidyNode object starting from the <head> tag of the tidy parse tree
    • tidy::html - Returns a tidyNode object starting from the <html> tag of the tidy parse tree
    • tidy::isXhtml - Indicates if the document is a XHTML document
    • tidy::isXml - Indicates if the document is a generic (non HTML/XHTML) XML document
    • tidy::parseFile - Parse markup in file or URI
    • tidy::parseString - Parse a document stored in a string
    • tidy::repairFile - Repair a file and return it as a string
    • tidy::repairString - Repair a string using an optionally provided configuration file
    • tidy::root - Returns a tidyNode object representing the root of the tidy parse tree
    • tidy::__construct - Constructs a new tidy object
    • tidyNode::getParent - Returns the parent node of the current node
    • tidyNode::hasChildren - Checks if a node has children
    • tidyNode::hasSiblings - Checks if a node has siblings
    • tidyNode::isAsp - Checks if this node is ASP
    • tidyNode::isComment - Checks if a node represents a comment
    • tidyNode::isHtml - Checks if a node is part of a HTML document
    • tidyNode::isJste - Checks if this node is JSTE
    • tidyNode::isPhp - Checks if a node is PHP
    • tidyNode::isText - Checks if a node represents text (no markup)
    • tidy_access_count - Returns the Number of Tidy accessibility warnings encountered for specified document
    • tidy_config_count - Returns the Number of Tidy configuration errors encountered for specified document
    • tidy_error_count - Returns the Number of Tidy errors encountered for specified document
    • tidy_get_output - Return a string representing the parsed tidy markup
    • tidy_load_config - Load an ASCII Tidy configuration file with the specified encoding
    • tidy_reset_config - Restore Tidy configuration to default values
    • tidy_save_config - Save current settings to named file
    • tidy_setopt - Updates the configuration settings for the specified tidy document
    • tidy_set_encoding - Set the input/output character encoding for parsing markup
    • tidy_warning_count - Returns the Number of Tidy warnings encountered for specified document
    • time - Return current Unix timestamp
    • timezone_abbreviations_list - 별칭: DateTimeZone::listAbbreviations
    • timezone_identifiers_list - 별칭: DateTimeZone::listIdentifiers
    • timezone_location_get - 별칭: DateTimeZone::getLocation
    • timezone_name_from_abbr - Returns the timezone name from abbreviation
    • timezone_name_get - 별칭: DateTimeZone::getName
    • timezone_offset_get - 별칭: DateTimeZone::getOffset
    • timezone_open - 별칭: DateTimeZone::__construct
    • timezone_transitions_get - 별칭: DateTimeZone::getTransitions
    • timezone_version_get - Gets the version of the timezonedb
    • time_nanosleep - Delay for a number of seconds and nanoseconds
    • time_sleep_until - Make the script sleep until the specified time
    • tmpfile - Creates a temporary file
    • token_get_all - Split given source into PHP tokens
    • token_name - Get the symbolic name of a given PHP token
    • TokyoTyrant::add - Adds to a numeric key
    • TokyoTyrant::connect - Connect to a database
    • TokyoTyrant::connectUri - Connects to a database
    • TokyoTyrant::copy - Copies the database
    • TokyoTyrant::ext - Execute a remote script
    • TokyoTyrant::fwmKeys - Returns the forward matching keys
    • TokyoTyrant::get - The get purpose
    • TokyoTyrant::getIterator - Get an iterator
    • TokyoTyrant::num - Number of records in the database
    • TokyoTyrant::out - Removes records
    • TokyoTyrant::put - Puts values
    • TokyoTyrant::putCat - Concatenates to a record
    • TokyoTyrant::putKeep - Puts a record
    • TokyoTyrant::putNr - Puts value
    • TokyoTyrant::putShl - Concatenates to a record
    • TokyoTyrant::restore - Restore the database
    • TokyoTyrant::setMaster - Set the replication master
    • TokyoTyrant::size - Returns the size of the value
    • TokyoTyrant::stat - Get statistics
    • TokyoTyrant::sync - Synchronize the database
    • TokyoTyrant::tune - Tunes connection values
    • TokyoTyrant::vanish - Empties the database
    • TokyoTyrant::__construct - Construct a new TokyoTyrant object
    • TokyoTyrantIterator::current - Get the current value
    • TokyoTyrantIterator::key - Returns the current key
    • TokyoTyrantIterator::next - Move to next key
    • TokyoTyrantIterator::rewind - Rewinds the iterator
    • TokyoTyrantIterator::valid - Rewinds the iterator
    • TokyoTyrantIterator::__construct - Construct an iterator
    • TokyoTyrantQuery::addCond - Adds a condition to the query
    • TokyoTyrantQuery::count - Counts records
    • TokyoTyrantQuery::current - Returns the current element
    • TokyoTyrantQuery::hint - Get the hint string of the query
    • TokyoTyrantQuery::key - Returns the current key
    • TokyoTyrantQuery::metaSearch - Retrieve records with multiple queries
    • TokyoTyrantQuery::next - Moves the iterator to next entry
    • TokyoTyrantQuery::out - Removes records based on query
    • TokyoTyrantQuery::rewind - Rewinds the iterator
    • TokyoTyrantQuery::search - Searches records
    • TokyoTyrantQuery::setLimit - Limit results
    • TokyoTyrantQuery::setOrder - Orders results
    • TokyoTyrantQuery::valid - Checks the validity of current item
    • TokyoTyrantQuery::__construct - Construct a new query
    • TokyoTyrantTable::add - Adds a record
    • TokyoTyrantTable::genUid - Generate unique id
    • TokyoTyrantTable::get - Get a row
    • TokyoTyrantTable::getIterator - Get an iterator
    • TokyoTyrantTable::getQuery - Get a query object
    • TokyoTyrantTable::out - Remove records
    • TokyoTyrantTable::put - Store a row
    • TokyoTyrantTable::putCat - Concatenates to a row
    • TokyoTyrantTable::putKeep - Put a new record
    • TokyoTyrantTable::putNr - Puts value
    • TokyoTyrantTable::putShl - Concatenates to a record
    • TokyoTyrantTable::setIndex - Sets index
    • touch - Sets access and modification time of file
    • trader_acos - Vector Trigonometric ACos
    • trader_ad - Chaikin A/D Line
    • trader_add - Vector Arithmetic Add
    • trader_adosc - Chaikin A/D Oscillator
    • trader_adx - Average Directional Movement Index
    • trader_adxr - Average Directional Movement Index Rating
    • trader_apo - Absolute Price Oscillator
    • trader_aroon - Aroon
    • trader_aroonosc - Aroon Oscillator
    • trader_asin - Vector Trigonometric ASin
    • trader_atan - Vector Trigonometric ATan
    • trader_atr - Average True Range
    • trader_avgprice - Average Price
    • trader_bbands - Bollinger Bands
    • trader_beta - Beta
    • trader_bop - Balance Of Power
    • trader_cci - Commodity Channel Index
    • trader_cdl2crows - Two Crows
    • trader_cdl3blackcrows - Three Black Crows
    • trader_cdl3inside - Three Inside Up/Down
    • trader_cdl3linestrike - Three-Line Strike
    • trader_cdl3outside - Three Outside Up/Down
    • trader_cdl3starsinsouth - Three Stars In The South
    • trader_cdl3whitesoldiers - Three Advancing White Soldiers
    • trader_cdlabandonedbaby - Abandoned Baby
    • trader_cdladvanceblock - Advance Block
    • trader_cdlbelthold - Belt-hold
    • trader_cdlbreakaway - Breakaway
    • trader_cdlclosingmarubozu - Closing Marubozu
    • trader_cdlconcealbabyswall - Concealing Baby Swallow
    • trader_cdlcounterattack - Counterattack
    • trader_cdldarkcloudcover - Dark Cloud Cover
    • trader_cdldoji - Doji
    • trader_cdldojistar - Doji Star
    • trader_cdldragonflydoji - Dragonfly Doji
    • trader_cdlengulfing - Engulfing Pattern
    • trader_cdleveningdojistar - Evening Doji Star
    • trader_cdleveningstar - Evening Star
    • trader_cdlgapsidesidewhite - Up/Down-gap side-by-side white lines
    • trader_cdlgravestonedoji - Gravestone Doji
    • trader_cdlhammer - Hammer
    • trader_cdlhangingman - Hanging Man
    • trader_cdlharami - Harami Pattern
    • trader_cdlharamicross - Harami Cross Pattern
    • trader_cdlhighwave - High-Wave Candle
    • trader_cdlhikkake - Hikkake Pattern
    • trader_cdlhikkakemod - Modified Hikkake Pattern
    • trader_cdlhomingpigeon - Homing Pigeon
    • trader_cdlidentical3crows - Identical Three Crows
    • trader_cdlinneck - In-Neck Pattern
    • trader_cdlinvertedhammer - Inverted Hammer
    • trader_cdlkicking - Kicking
    • trader_cdlkickingbylength - Kicking - bull/bear determined by the longer marubozu
    • trader_cdlladderbottom - Ladder Bottom
    • trader_cdllongleggeddoji - Long Legged Doji
    • trader_cdllongline - Long Line Candle
    • trader_cdlmarubozu - Marubozu
    • trader_cdlmatchinglow - Matching Low
    • trader_cdlmathold - Mat Hold
    • trader_cdlmorningdojistar - Morning Doji Star
    • trader_cdlmorningstar - Morning Star
    • trader_cdlonneck - On-Neck Pattern
    • trader_cdlpiercing - Piercing Pattern
    • trader_cdlrickshawman - Rickshaw Man
    • trader_cdlrisefall3methods - Rising/Falling Three Methods
    • trader_cdlseparatinglines - Separating Lines
    • trader_cdlshootingstar - Shooting Star
    • trader_cdlshortline - Short Line Candle
    • trader_cdlspinningtop - Spinning Top
    • trader_cdlstalledpattern - Stalled Pattern
    • trader_cdlsticksandwich - Stick Sandwich
    • trader_cdltakuri - Takuri (Dragonfly Doji with very long lower shadow)
    • trader_cdltasukigap - Tasuki Gap
    • trader_cdlthrusting - Thrusting Pattern
    • trader_cdltristar - Tristar Pattern
    • trader_cdlunique3river - Unique 3 River
    • trader_cdlupsidegap2crows - Upside Gap Two Crows
    • trader_cdlxsidegap3methods - Upside/Downside Gap Three Methods
    • trader_ceil - Vector Ceil
    • trader_cmo - Chande Momentum Oscillator
    • trader_correl - Pearson's Correlation Coefficient (r)
    • trader_cos - Vector Trigonometric Cos
    • trader_cosh - Vector Trigonometric Cosh
    • trader_dema - Double Exponential Moving Average
    • trader_div - Vector Arithmetic Div
    • trader_dx - Directional Movement Index
    • trader_ema - Exponential Moving Average
    • trader_errno - Get error code
    • trader_exp - Vector Arithmetic Exp
    • trader_floor - Vector Floor
    • trader_get_compat - Get compatibility mode
    • trader_get_unstable_period - Get unstable period
    • trader_ht_dcperiod - Hilbert Transform - Dominant Cycle Period
    • trader_ht_dcphase - Hilbert Transform - Dominant Cycle Phase
    • trader_ht_phasor - Hilbert Transform - Phasor Components
    • trader_ht_sine - Hilbert Transform - SineWave
    • trader_ht_trendline - Hilbert Transform - Instantaneous Trendline
    • trader_ht_trendmode - Hilbert Transform - Trend vs Cycle Mode
    • trader_kama - Kaufman Adaptive Moving Average
    • trader_linearreg - Linear Regression
    • trader_linearreg_angle - Linear Regression Angle
    • trader_linearreg_intercept - Linear Regression Intercept
    • trader_linearreg_slope - Linear Regression Slope
    • trader_ln - Vector Log Natural
    • trader_log10 - Vector Log10
    • trader_ma - Moving average
    • trader_macd - Moving Average Convergence/Divergence
    • trader_macdext - MACD with controllable MA type
    • trader_macdfix - Moving Average Convergence/Divergence Fix 12/26
    • trader_mama - MESA Adaptive Moving Average
    • trader_mavp - Moving average with variable period
    • trader_max - Highest value over a specified period
    • trader_maxindex - Index of highest value over a specified period
    • trader_medprice - Median Price
    • trader_mfi - Money Flow Index
    • trader_midpoint - MidPoint over period
    • trader_midprice - Midpoint Price over period
    • trader_min - Lowest value over a specified period
    • trader_minindex - Index of lowest value over a specified period
    • trader_minmax - Lowest and highest values over a specified period
    • trader_minmaxindex - Indexes of lowest and highest values over a specified period
    • trader_minus_di - Minus Directional Indicator
    • trader_minus_dm - Minus Directional Movement
    • trader_mom - Momentum
    • trader_mult - Vector Arithmetic Mult
    • trader_natr - Normalized Average True Range
    • trader_obv - On Balance Volume
    • trader_plus_di - Plus Directional Indicator
    • trader_plus_dm - Plus Directional Movement
    • trader_ppo - Percentage Price Oscillator
    • trader_roc - Rate of change : ((price/prevPrice)-1)*100
    • trader_rocp - Rate of change Percentage: (price-prevPrice)/prevPrice
    • trader_rocr - Rate of change ratio: (price/prevPrice)
    • trader_rocr100 - Rate of change ratio 100 scale: (price/prevPrice)*100
    • trader_rsi - Relative Strength Index
    • trader_sar - Parabolic SAR
    • trader_sarext - Parabolic SAR - Extended
    • trader_set_compat - Set compatibility mode
    • trader_set_unstable_period - Set unstable period
    • trader_sin - Vector Trigonometric Sin
    • trader_sinh - Vector Trigonometric Sinh
    • trader_sma - Simple Moving Average
    • trader_sqrt - Vector Square Root
    • trader_stddev - Standard Deviation
    • trader_stoch - Stochastic
    • trader_stochf - Stochastic Fast
    • trader_stochrsi - Stochastic Relative Strength Index
    • trader_sub - Vector Arithmetic Subtraction
    • trader_sum - Summation
    • trader_t3 - Triple Exponential Moving Average (T3)
    • trader_tan - Vector Trigonometric Tan
    • trader_tanh - Vector Trigonometric Tanh
    • trader_tema - Triple Exponential Moving Average
    • trader_trange - True Range
    • trader_trima - Triangular Moving Average
    • trader_trix - 1-day Rate-Of-Change (ROC) of a Triple Smooth EMA
    • trader_tsf - Time Series Forecast
    • trader_typprice - Typical Price
    • trader_ultosc - Ultimate Oscillator
    • trader_var - Variance
    • trader_wclprice - Weighted Close Price
    • trader_willr - Williams' %R
    • trader_wma - Weighted Moving Average
    • trait_exists - Checks if the trait exists
    • Transliterator::create - Create a transliterator
    • Transliterator::createFromRules - Create transliterator from rules
    • Transliterator::createInverse - Create an inverse transliterator
    • Transliterator::getErrorCode - Get last error code
    • Transliterator::getErrorMessage - Get last error message
    • Transliterator::listIDs - Get transliterator IDs
    • Transliterator::transliterate - Transliterate a string
    • Transliterator::__construct - Private constructor to deny instantiation
    • trigger_error - Generates a user-level error/warning/notice message
    • trim - Strip whitespace (or other characters) from the beginning and end of a string
  • u
    • uasort - 사용자 정의 비교 함수로 배열을 정렬하고 인덱스 연관성을 유지
    • ucfirst - Make a string's first character uppercase
    • UConverter::convert - Convert string from one charset to another
    • UConverter::fromUCallback - Default "from" callback function
    • UConverter::getAliases - Get the aliases of the given name
    • UConverter::getAvailable - Get the available canonical converter names
    • UConverter::getDestinationEncoding - Get the destination encoding
    • UConverter::getDestinationType - Get the destination converter type
    • UConverter::getErrorCode - Get last error code on the object
    • UConverter::getErrorMessage - Get last error message on the object
    • UConverter::getSourceEncoding - Get the source encoding
    • UConverter::getSourceType - Get the source convertor type
    • UConverter::getStandards - Get standards associated to converter names
    • UConverter::getSubstChars - Get substitution chars
    • UConverter::reasonText - Get string representation of the callback reason
    • UConverter::setDestinationEncoding - Set the destination encoding
    • UConverter::setSourceEncoding - Set the source encoding
    • UConverter::setSubstChars - Set the substitution chars
    • UConverter::toUCallback - Default "to" callback function
    • UConverter::transcode - Convert string from one charset to another
    • UConverter::__construct - Create UConverter object
    • ucwords - Uppercase the first character of each word in a string
    • udm_add_search_limit - Add various search limits
    • udm_alloc_agent - Allocate mnoGoSearch session
    • udm_alloc_agent_array - Allocate mnoGoSearch session
    • udm_api_version - Get mnoGoSearch API version
    • udm_cat_list - Get all the categories on the same level with the current one
    • udm_cat_path - Get the path to the current category
    • udm_check_charset - Check if the given charset is known to mnogosearch
    • udm_clear_search_limits - Clear all mnoGoSearch search restrictions
    • udm_crc32 - Return CRC32 checksum of given string
    • udm_errno - Get mnoGoSearch error number
    • udm_error - Get mnoGoSearch error message
    • udm_find - Perform search
    • udm_free_agent - Free mnoGoSearch session
    • udm_free_ispell_data - Free memory allocated for ispell data
    • udm_free_res - Free mnoGoSearch result
    • udm_get_doc_count - Get total number of documents in database
    • udm_get_res_field - Fetch a result field
    • udm_get_res_param - Get mnoGoSearch result parameters
    • udm_hash32 - Return Hash32 checksum of given string
    • udm_load_ispell_data - Load ispell data
    • udm_set_agent_param - Set mnoGoSearch agent session parameters
    • UI\Area::onDraw - Draw Callback
    • UI\Area::onKey - Key Callback
    • UI\Area::onMouse - Mouse Callback
    • UI\Area::redraw - Redraw Area
    • UI\Area::scrollTo - Area Scroll
    • UI\Area::setSize - Set Size
    • UI\Control::destroy - Destroy Control
    • UI\Control::disable - Disable Control
    • UI\Control::enable - Enable Control
    • UI\Control::getParent - Get Parent Control
    • UI\Control::getTopLevel - Get Top Level
    • UI\Control::hide - Hide Control
    • UI\Control::isEnabled - Determine if Control is enabled
    • UI\Control::isVisible - Determine if Control is visible
    • UI\Control::setParent - Set Parent Control
    • UI\Control::show - Control Show
    • UI\Controls\Box::append - Append Control
    • UI\Controls\Box::delete - Delete Control
    • UI\Controls\Box::getOrientation - Get Orientation
    • UI\Controls\Box::isPadded - Padding Detection
    • UI\Controls\Box::setPadded - Set Padding
    • UI\Controls\Box::__construct - Construct a new Box
    • UI\Controls\Button::getText - Get Text
    • UI\Controls\Button::onClick - Click Handler
    • UI\Controls\Button::setText - Set Text
    • UI\Controls\Button::__construct - Construct a new Button
    • UI\Controls\Check::getText - Get Text
    • UI\Controls\Check::isChecked - Checked Detection
    • UI\Controls\Check::onToggle - Toggle Callback
    • UI\Controls\Check::setChecked - Set Checked
    • UI\Controls\Check::setText - Set Text
    • UI\Controls\Check::__construct - Construct a new Check
    • UI\Controls\ColorButton::getColor - Get Color
    • UI\Controls\ColorButton::onChange - Change Handler
    • UI\Controls\ColorButton::setColor - Set Color
    • UI\Controls\Combo::append - Append Option
    • UI\Controls\Combo::getSelected - Get Selected Option
    • UI\Controls\Combo::onSelected - Selected Handler
    • UI\Controls\Combo::setSelected - Set Selected Option
    • UI\Controls\EditableCombo::append - Append Option
    • UI\Controls\EditableCombo::getText - Get Text
    • UI\Controls\EditableCombo::onChange - Change Handler
    • UI\Controls\EditableCombo::setText - Set Text
    • UI\Controls\Entry::getText - Get Text
    • UI\Controls\Entry::isReadOnly - Detect Read Only
    • UI\Controls\Entry::onChange - Change Handler
    • UI\Controls\Entry::setReadOnly - Set Read Only
    • UI\Controls\Entry::setText - Set Text
    • UI\Controls\Entry::__construct - Construct a new Entry
    • UI\Controls\Form::append - Append Control
    • UI\Controls\Form::delete - Delete Control
    • UI\Controls\Form::isPadded - Padding Detection
    • UI\Controls\Form::setPadded - Set Padding
    • UI\Controls\Grid::append - Append Control
    • UI\Controls\Grid::isPadded - Padding Detection
    • UI\Controls\Grid::setPadded - Set Padding
    • UI\Controls\Group::append - Append Control
    • UI\Controls\Group::getTitle - Get Title
    • UI\Controls\Group::hasMargin - Margin Detection
    • UI\Controls\Group::setMargin - Set Margin
    • UI\Controls\Group::setTitle - Set Title
    • UI\Controls\Group::__construct - Construct a new Group
    • UI\Controls\Label::getText - Get Text
    • UI\Controls\Label::setText - Set Text
    • UI\Controls\Label::__construct - Construct a new Label
    • UI\Controls\MultilineEntry::append - Append Text
    • UI\Controls\MultilineEntry::getText - Get Text
    • UI\Controls\MultilineEntry::isReadOnly - Read Only Detection
    • UI\Controls\MultilineEntry::onChange - Change Handler
    • UI\Controls\MultilineEntry::setReadOnly - Set Read Only
    • UI\Controls\MultilineEntry::setText - Set Text
    • UI\Controls\MultilineEntry::__construct - Construct a new Multiline Entry
    • UI\Controls\Picker::__construct - Construct a new Picker
    • UI\Controls\Progress::getValue - Get Value
    • UI\Controls\Progress::setValue - Set Value
    • UI\Controls\Radio::append - Append Option
    • UI\Controls\Radio::getSelected - Get Selected Option
    • UI\Controls\Radio::onSelected - Selected Handler
    • UI\Controls\Radio::setSelected - Set Selected Option
    • UI\Controls\Separator::__construct - Construct a new Separator
    • UI\Controls\Slider::getValue - Get Value
    • UI\Controls\Slider::onChange - Change Handler
    • UI\Controls\Slider::setValue - Set Value
    • UI\Controls\Slider::__construct - Construct a new Slider
    • UI\Controls\Spin::getValue - Get Value
    • UI\Controls\Spin::onChange - Change Handler
    • UI\Controls\Spin::setValue - Set Value
    • UI\Controls\Spin::__construct - Construct a new Spin
    • UI\Controls\Tab::append - Append Page
    • UI\Controls\Tab::delete - Delete Page
    • UI\Controls\Tab::hasMargin - Margin Detection
    • UI\Controls\Tab::insertAt - Insert Page
    • UI\Controls\Tab::pages - Page Count
    • UI\Controls\Tab::setMargin - Set Margin
    • UI\Draw\Brush::getColor - Get Color
    • UI\Draw\Brush::setColor - Set Color
    • UI\Draw\Brush::__construct - Construct a new Brush
    • UI\Draw\Brush\Gradient::addStop - Stop Manipulation
    • UI\Draw\Brush\Gradient::delStop - Stop Manipulation
    • UI\Draw\Brush\Gradient::setStop - Stop Manipulation
    • UI\Draw\Brush\LinearGradient::__construct - Construct a Linear Gradient
    • UI\Draw\Brush\RadialGradient::__construct - Construct a new Radial Gradient
    • UI\Draw\Color::getChannel - Color Manipulation
    • UI\Draw\Color::setChannel - Color Manipulation
    • UI\Draw\Color::__construct - Construct new Color
    • UI\Draw\Matrix::invert - Invert Matrix
    • UI\Draw\Matrix::isInvertible - Invertible Detection
    • UI\Draw\Matrix::multiply - Multiply Matrix
    • UI\Draw\Matrix::rotate - Rotate Matrix
    • UI\Draw\Matrix::scale - Scale Matrix
    • UI\Draw\Matrix::skew - Skew Matrix
    • UI\Draw\Matrix::translate - Translate Matrix
    • UI\Draw\Path::addRectangle - Draw a Rectangle
    • UI\Draw\Path::arcTo - Draw an Arc
    • UI\Draw\Path::bezierTo - Draw Bezier Curve
    • UI\Draw\Path::closeFigure - Close Figure
    • UI\Draw\Path::end - Finalize Path
    • UI\Draw\Path::lineTo - Draw a Line
    • UI\Draw\Path::newFigure - Draw Figure
    • UI\Draw\Path::newFigureWithArc - Draw Figure with Arc
    • UI\Draw\Path::__construct - Construct a new Path
    • UI\Draw\Pen::clip - Clip a Path
    • UI\Draw\Pen::fill - Fill a Path
    • UI\Draw\Pen::restore - Restore
    • UI\Draw\Pen::save - Save
    • UI\Draw\Pen::stroke - Stroke a Path
    • UI\Draw\Pen::transform - Matrix Transform
    • UI\Draw\Pen::write - Draw Text at Point
    • UI\Draw\Stroke::getCap - Get Line Cap
    • UI\Draw\Stroke::getJoin - Get Line Join
    • UI\Draw\Stroke::getMiterLimit - Get Miter Limit
    • UI\Draw\Stroke::getThickness - Get Thickness
    • UI\Draw\Stroke::setCap - Set Line Cap
    • UI\Draw\Stroke::setJoin - Set Line Join
    • UI\Draw\Stroke::setMiterLimit - Set Miter Limit
    • UI\Draw\Stroke::setThickness - Set Thickness
    • UI\Draw\Stroke::__construct - Construct a new Stroke
    • UI\Draw\Text\Font::getAscent - Font Metrics
    • UI\Draw\Text\Font::getDescent - Font Metrics
    • UI\Draw\Text\Font::getLeading - Font Metrics
    • UI\Draw\Text\Font::getUnderlinePosition - Font Metrics
    • UI\Draw\Text\Font::getUnderlineThickness - Font Metrics
    • UI\Draw\Text\Font::__construct - Construct a new Font
    • UI\Draw\Text\Font\Descriptor::getFamily - Get Font Family
    • UI\Draw\Text\Font\Descriptor::getItalic - Style Detection
    • UI\Draw\Text\Font\Descriptor::getSize - Size Detection
    • UI\Draw\Text\Font\Descriptor::getStretch - Style Detection
    • UI\Draw\Text\Font\Descriptor::getWeight - Weight Detection
    • UI\Draw\Text\Font\Descriptor::__construct - Construct a new Font Descriptor
    • UI\Draw\Text\Font\fontFamilies - Retrieve Font Families
    • UI\Draw\Text\Layout::setColor - Set Color
    • UI\Draw\Text\Layout::setWidth - Set Width
    • UI\Draw\Text\Layout::__construct - Construct a new Text Layout
    • UI\Executor::kill - Stop Executor
    • UI\Executor::onExecute - Execution Callback
    • UI\Executor::setInterval - Interval Manipulation
    • UI\Executor::__construct - Construct a new Executor
    • UI\Menu::append - Append Menu Item
    • UI\Menu::appendAbout - Append About Menu Item
    • UI\Menu::appendCheck - Append Checkable Menu Item
    • UI\Menu::appendPreferences - Append Preferences Menu Item
    • UI\Menu::appendQuit - Append Quit Menu Item
    • UI\Menu::appendSeparator - Append Menu Item Separator
    • UI\Menu::__construct - Construct a new Menu
    • UI\MenuItem::disable - Disable Menu Item
    • UI\MenuItem::enable - Enable Menu Item
    • UI\MenuItem::isChecked - Detect Checked
    • UI\MenuItem::onClick - On Click Callback
    • UI\MenuItem::setChecked - Set Checked
    • UI\Point::at - Size Coercion
    • UI\Point::getX - Retrieves X
    • UI\Point::getY - Retrieves Y
    • UI\Point::setX - Set X
    • UI\Point::setY - Set Y
    • UI\Point::__construct - Construct a new Point
    • UI\quit - Quit UI Loop
    • UI\run - Enter UI Loop
    • UI\Size::getHeight - Retrieves Height
    • UI\Size::getWidth - Retrives Width
    • UI\Size::of - Point Coercion
    • UI\Size::setHeight - Set Height
    • UI\Size::setWidth - Set Width
    • UI\Size::__construct - Construct a new Size
    • UI\Window::add - Add a Control
    • UI\Window::error - Show Error Box
    • UI\Window::getSize - Get Window Size
    • UI\Window::getTitle - Get Title
    • UI\Window::hasBorders - Border Detection
    • UI\Window::hasMargin - Margin Detection
    • UI\Window::isFullScreen - Full Screen Detection
    • UI\Window::msg - Show Message Box
    • UI\Window::onClosing - Closing Callback
    • UI\Window::open - Open Dialog
    • UI\Window::save - Save Dialog
    • UI\Window::setBorders - Border Use
    • UI\Window::setFullScreen - Full Screen Use
    • UI\Window::setMargin - Margin Use
    • UI\Window::setSize - Set Size
    • UI\Window::setTitle - Window Title
    • UI\Window::__construct - Construct a new Window
    • uksort - 사용자 정의 비교 함수를 사용하여 키에 의한 배열 정렬
    • umask - Changes the current umask
    • uniqid - Generate a unique ID
    • unixtojd - Convert Unix timestamp to Julian Day
    • unlink - Deletes a file
    • unpack - Unpack data from binary string
    • unregister_tick_function - De-register a function for execution on each tick
    • unserialize - 저장 표현에서 PHP 값을 작성
    • unset - 주어진 변수를 제거
    • untaint - Untaint strings
    • uopz_backup - Backup a function
    • uopz_compose - Compose a class
    • uopz_copy - Copy a function
    • uopz_delete - Delete a function
    • uopz_extend - Extend a class at runtime
    • uopz_flags - Get or set flags on function or class
    • uopz_function - Creates a function at runtime
    • uopz_implement - Implements an interface at runtime
    • uopz_overload - Overload a VM opcode
    • uopz_redefine - Redefine a constant
    • uopz_rename - Rename a function at runtime
    • uopz_restore - Restore a previously backed up function
    • uopz_undefine - Undefine a constant
    • urldecode - URL 인코드된 문자열을 디코드
    • urlencode - 문자열을 URL 인코드
    • user_error - Alias of trigger_error
    • use_soap_error_handler - Set whether to use the SOAP error handler
    • usleep - Delay execution in microseconds
    • usort - 사용자 정의 비교 함수를 사용하여 값에 의한 배열 정렬
    • utf8_decode - Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1
    • utf8_encode - Encodes an ISO-8859-1 string to UTF-8
  • v
    • V8Js::executeString - Execute a string as Javascript code
    • V8Js::getExtensions - Return an array of registered extensions
    • V8Js::getPendingException - Return pending uncaught Javascript exception
    • V8Js::registerExtension - Register Javascript extensions for V8Js
    • V8Js::__construct - Construct a new V8Js object
    • V8JsException::getJsFileName - The getJsFileName purpose
    • V8JsException::getJsLineNumber - The getJsLineNumber purpose
    • V8JsException::getJsSourceLine - The getJsSourceLine purpose
    • V8JsException::getJsTrace - The getJsTrace purpose
    • variant_abs - Returns the absolute value of a variant
    • variant_add - "Adds" two variant values together and returns the result
    • variant_and - Performs a bitwise AND operation between two variants
    • variant_cast - Convert a variant into a new variant object of another type
    • variant_cat - concatenates two variant values together and returns the result
    • variant_cmp - Compares two variants
    • variant_date_from_timestamp - Returns a variant date representation of a Unix timestamp
    • variant_date_to_timestamp - Converts a variant date/time value to Unix timestamp
    • variant_div - Returns the result from dividing two variants
    • variant_eqv - Performs a bitwise equivalence on two variants
    • variant_fix - Returns the integer portion of a variant
    • variant_get_type - Returns the type of a variant object
    • variant_idiv - Converts variants to integers and then returns the result from dividing them
    • variant_imp - Performs a bitwise implication on two variants
    • variant_int - Returns the integer portion of a variant
    • variant_mod - Divides two variants and returns only the remainder
    • variant_mul - Multiplies the values of the two variants
    • variant_neg - Performs logical negation on a variant
    • variant_not - Performs bitwise not negation on a variant
    • variant_or - Performs a logical disjunction on two variants
    • variant_pow - Returns the result of performing the power function with two variants
    • variant_round - Rounds a variant to the specified number of decimal places
    • variant_set - Assigns a new value for a variant object
    • variant_set_type - Convert a variant into another type "in-place"
    • variant_sub - Subtracts the value of the right variant from the left variant value
    • variant_xor - Performs a logical exclusion on two variants
    • VarnishAdmin::auth - Authenticate on a varnish instance
    • VarnishAdmin::ban - Ban URLs using a VCL expression
    • VarnishAdmin::banUrl - Ban an URL using a VCL expression
    • VarnishAdmin::clearPanic - Clear varnish instance panic messages
    • VarnishAdmin::connect - Connect to a varnish instance administration interface
    • VarnishAdmin::disconnect - Disconnect from a varnish instance administration interface
    • VarnishAdmin::getPanic - Get the last panic message on a varnish instance
    • VarnishAdmin::getParams - Fetch current varnish instance configuration parameters
    • VarnishAdmin::isRunning - Check if the varnish slave process is currently running
    • VarnishAdmin::setCompat - Set the class compat configuration param
    • VarnishAdmin::setHost - Set the class host configuration param
    • VarnishAdmin::setIdent - Set the class ident configuration param
    • VarnishAdmin::setParam - Set configuration param on the current varnish instance
    • VarnishAdmin::setPort - Set the class port configuration param
    • VarnishAdmin::setSecret - Set the class secret configuration param
    • VarnishAdmin::setTimeout - Set the class timeout configuration param
    • VarnishAdmin::start - Start varnish worker process
    • VarnishAdmin::stop - Stop varnish worker process
    • VarnishAdmin::__construct - VarnishAdmin constructor
    • VarnishLog::getLine - Get next log line
    • VarnishLog::getTagName - Get the log tag string representation by its index
    • VarnishLog::__construct - Varnishlog constructor
    • VarnishStat::getSnapshot - Get the current varnish instance statistics snapshot
    • VarnishStat::__construct - VarnishStat constructor
    • var_dump - 변수에 대한 정보를 덤프
    • var_export - 변수를 처리가능한 문자열 표현으로 출력하거나 반환합니다
    • version_compare - Compares two "PHP-standardized" version number strings
    • vfprintf - Write a formatted string to a stream
    • virtual - 아파치 하위 요청을 실행합니다
    • vpopmail_add_alias_domain - Add an alias for a virtual domain
    • vpopmail_add_alias_domain_ex - Add alias to an existing virtual domain
    • vpopmail_add_domain - Add a new virtual domain
    • vpopmail_add_domain_ex - Add a new virtual domain
    • vpopmail_add_user - Add a new user to the specified virtual domain
    • vpopmail_alias_add - Insert a virtual alias
    • vpopmail_alias_del - Deletes all virtual aliases of a user
    • vpopmail_alias_del_domain - Deletes all virtual aliases of a domain
    • vpopmail_alias_get - Get all lines of an alias for a domain
    • vpopmail_alias_get_all - Get all lines of an alias for a domain
    • vpopmail_auth_user - Attempt to validate a username/domain/password
    • vpopmail_del_domain - Delete a virtual domain
    • vpopmail_del_domain_ex - Delete a virtual domain
    • vpopmail_del_user - Delete a user from a virtual domain
    • vpopmail_error - Get text message for last vpopmail error
    • vpopmail_passwd - Change a virtual user's password
    • vpopmail_set_user_quota - Sets a virtual user's quota
    • vprintf - Output a formatted string
    • vsprintf - Return a formatted string
  • w
    • wddx_add_vars - Add variables to a WDDX packet with the specified ID
    • wddx_deserialize - Unserializes a WDDX packet
    • wddx_packet_end - Ends a WDDX packet with the specified ID
    • wddx_packet_start - Starts a new WDDX packet with structure inside it
    • wddx_serialize_value - Serialize a single value into a WDDX packet
    • wddx_serialize_vars - Serialize variables into a WDDX packet
    • WeakMap::count - Counts the number of live entries in the map
    • WeakMap::current - Returns the current value under iteration
    • WeakMap::key - Returns the current key under iteration.
    • WeakMap::next - Advances to the next map element
    • WeakMap::offsetExists - Checks whether a certain object is in the map
    • WeakMap::offsetGet - Returns the value pointed to by a certain object
    • WeakMap::offsetSet - Updates the map with a new key-value pair
    • WeakMap::offsetUnset - Removes an entry from the map
    • WeakMap::rewind - Rewinds the iterator to the beginning of the map
    • WeakMap::valid - Returns whether the iterator is still on a valid map element
    • WeakMap::__construct - Constructs a new map
    • Weakref::acquire - Acquires a strong reference on that object
    • Weakref::get - Returns the object pointed to by the weak reference
    • Weakref::release - Releases a previously acquired reference
    • Weakref::valid - Checks whether the object referenced still exists
    • Weakref::__construct - Constructs a new weak reference
    • win32_continue_service - Resumes a paused service
    • win32_create_service - Creates a new service entry in the SCM database
    • win32_delete_service - Deletes a service entry from the SCM database
    • win32_get_last_control_message - Returns the last control message that was sent to this service
    • win32_pause_service - Pauses a service
    • win32_ps_list_procs - List running processes
    • win32_ps_stat_mem - Stat memory utilization
    • win32_ps_stat_proc - Stat process
    • win32_query_service_status - Queries the status of a service
    • win32_set_service_status - Update the service status
    • win32_start_service - Starts a service
    • win32_start_service_ctrl_dispatcher - Registers the script with the SCM, so that it can act as the service with the given name
    • win32_stop_service - Stops a service
    • wincache_fcache_fileinfo - Retrieves information about files cached in the file cache
    • wincache_fcache_meminfo - Retrieves information about file cache memory usage
    • wincache_lock - Acquires an exclusive lock on a given key
    • wincache_ocache_fileinfo - Retrieves information about files cached in the opcode cache
    • wincache_ocache_meminfo - Retrieves information about opcode cache memory usage
    • wincache_refresh_if_changed - Refreshes the cache entries for the cached files
    • wincache_rplist_fileinfo - Retrieves information about resolve file path cache
    • wincache_rplist_meminfo - Retrieves information about memory usage by the resolve file path cache
    • wincache_scache_info - Retrieves information about files cached in the session cache
    • wincache_scache_meminfo - Retrieves information about session cache memory usage
    • wincache_ucache_add - Adds a variable in user cache only if variable does not already exist in the cache
    • wincache_ucache_cas - Compares the variable with old value and assigns new value to it
    • wincache_ucache_clear - Deletes entire content of the user cache
    • wincache_ucache_dec - Decrements the value associated with the key
    • wincache_ucache_delete - Deletes variables from the user cache
    • wincache_ucache_exists - Checks if a variable exists in the user cache
    • wincache_ucache_get - Gets a variable stored in the user cache
    • wincache_ucache_inc - Increments the value associated with the key
    • wincache_ucache_info - Retrieves information about data stored in the user cache
    • wincache_ucache_meminfo - Retrieves information about user cache memory usage
    • wincache_ucache_set - Adds a variable in user cache and overwrites a variable if it already exists in the cache
    • wincache_unlock - Releases an exclusive lock on a given key
    • wordwrap - Wraps a string to a given number of characters
    • Worker::getStacked - Stack Analysis
    • Worker::isShutdown - State Detection
    • Worker::isWorking - State Detection
    • Worker::shutdown - Synchronization
    • Worker::stack - Stacking
    • Worker::unstack - Stacking
  • x
    • xattr_get - Get an extended attribute
    • xattr_list - Get a list of extended attributes
    • xattr_remove - Remove an extended attribute
    • xattr_set - Set an extended attribute
    • xattr_supported - Check if filesystem supports extended attributes
    • xdiff_file_bdiff - Make binary diff of two files
    • xdiff_file_bdiff_size - Read a size of file created by applying a binary diff
    • xdiff_file_bpatch - Patch a file with a binary diff
    • xdiff_file_diff - Make unified diff of two files
    • xdiff_file_diff_binary - Alias of xdiff_file_bdiff
    • xdiff_file_merge3 - Merge 3 files into one
    • xdiff_file_patch - Patch a file with an unified diff
    • xdiff_file_patch_binary - Alias of xdiff_file_bpatch
    • xdiff_file_rabdiff - Make binary diff of two files using the Rabin's polynomial fingerprinting algorithm
    • xdiff_string_bdiff - Make binary diff of two strings
    • xdiff_string_bdiff_size - Read a size of file created by applying a binary diff
    • xdiff_string_bpatch - Patch a string with a binary diff
    • xdiff_string_diff - Make unified diff of two strings
    • xdiff_string_diff_binary - Alias of xdiff_string_bdiff
    • xdiff_string_merge3 - Merge 3 strings into one
    • xdiff_string_patch - Patch a string with an unified diff
    • xdiff_string_patch_binary - Alias of xdiff_string_bpatch
    • xdiff_string_rabdiff - Make binary diff of two strings using the Rabin's polynomial fingerprinting algorithm
    • xhprof_disable - Stops xhprof profiler
    • xhprof_enable - Start xhprof profiler
    • xhprof_sample_disable - Stops xhprof sample profiler
    • xhprof_sample_enable - Start XHProf profiling in sampling mode
    • XMLDiff\Base::diff - Produce diff of two XML documents
    • XMLDiff\Base::merge - Produce new XML document based on diff
    • XMLDiff\Base::__construct - Constructor
    • XMLDiff\DOM::diff - Diff two DOMDocument objects
    • XMLDiff\DOM::merge - Produce merged DOMDocument
    • XMLDiff\File::diff - Diff two XML files
    • XMLDiff\File::merge - Produce merged XML document
    • XMLDiff\Memory::diff - Diff two XML documents
    • XMLDiff\Memory::merge - Produce merged XML document
    • XMLReader::close - Close the XMLReader input
    • XMLReader::expand - Returns a copy of the current node as a DOM object
    • XMLReader::getAttribute - Get the value of a named attribute
    • XMLReader::getAttributeNo - Get the value of an attribute by index
    • XMLReader::getAttributeNs - Get the value of an attribute by localname and URI
    • XMLReader::getParserProperty - Indicates if specified property has been set
    • XMLReader::isValid - Indicates if the parsed document is valid
    • XMLReader::lookupNamespace - Lookup namespace for a prefix
    • XMLReader::moveToAttribute - Move cursor to a named attribute
    • XMLReader::moveToAttributeNo - Move cursor to an attribute by index
    • XMLReader::moveToAttributeNs - Move cursor to a named attribute
    • XMLReader::moveToElement - Position cursor on the parent Element of current Attribute
    • XMLReader::moveToFirstAttribute - Position cursor on the first Attribute
    • XMLReader::moveToNextAttribute - Position cursor on the next Attribute
    • XMLReader::next - Move cursor to next node skipping all subtrees
    • XMLReader::open - Set the URI containing the XML to parse
    • XMLReader::read - Move to next node in document
    • XMLReader::readInnerXML - Retrieve XML from current node
    • XMLReader::readOuterXML - Retrieve XML from current node, including itself
    • XMLReader::readString - Reads the contents of the current node as a string
    • XMLReader::setParserProperty - Set parser options
    • XMLReader::setRelaxNGSchema - Set the filename or URI for a RelaxNG Schema
    • XMLReader::setRelaxNGSchemaSource - Set the data containing a RelaxNG Schema
    • XMLReader::setSchema - Validate document against XSD
    • XMLReader::XML - Set the data containing the XML to parse
    • xmlrpc_decode - Decodes XML into native PHP types
    • xmlrpc_decode_request - Decodes XML into native PHP types
    • xmlrpc_encode - Generates XML for a PHP value
    • xmlrpc_encode_request - Generates XML for a method request
    • xmlrpc_get_type - Gets xmlrpc type for a PHP value
    • xmlrpc_is_fault - Determines if an array value represents an XMLRPC fault
    • xmlrpc_parse_method_descriptions - Decodes XML into a list of method descriptions
    • xmlrpc_server_add_introspection_data - Adds introspection documentation
    • xmlrpc_server_call_method - Parses XML requests and call methods
    • xmlrpc_server_create - Creates an xmlrpc server
    • xmlrpc_server_destroy - Destroys server resources
    • xmlrpc_server_register_introspection_callback - Register a PHP function to generate documentation
    • xmlrpc_server_register_method - Register a PHP function to resource method matching method_name
    • xmlrpc_set_type - Sets xmlrpc type, base64 or datetime, for a PHP string value
    • XMLWriter::endAttribute - End attribute
    • XMLWriter::endCData - End current CDATA
    • XMLWriter::endComment - Create end comment
    • XMLWriter::endDocument - End current document
    • XMLWriter::endDTD - End current DTD
    • XMLWriter::endDTDAttlist - End current DTD AttList
    • XMLWriter::endDTDElement - End current DTD element
    • XMLWriter::endDTDEntity - End current DTD Entity
    • XMLWriter::endElement - End current element
    • XMLWriter::endPI - End current PI
    • XMLWriter::flush - Flush current buffer
    • XMLWriter::fullEndElement - End current element
    • XMLWriter::openMemory - Create new xmlwriter using memory for string output
    • XMLWriter::openURI - Create new xmlwriter using source uri for output
    • XMLWriter::outputMemory - Returns current buffer
    • XMLWriter::setIndent - Toggle indentation on/off
    • XMLWriter::setIndentString - Set string used for indenting
    • XMLWriter::startAttribute - Create start attribute
    • XMLWriter::startAttributeNS - Create start namespaced attribute
    • XMLWriter::startCData - Create start CDATA tag
    • XMLWriter::startComment - Create start comment
    • XMLWriter::startDocument - Create document tag
    • XMLWriter::startDTD - Create start DTD tag
    • XMLWriter::startDTDAttlist - Create start DTD AttList
    • XMLWriter::startDTDElement - Create start DTD element
    • XMLWriter::startDTDEntity - Create start DTD Entity
    • XMLWriter::startElement - Create start element tag
    • XMLWriter::startElementNS - Create start namespaced element tag
    • XMLWriter::startPI - Create start PI tag
    • XMLWriter::text - Write text
    • XMLWriter::writeAttribute - Write full attribute
    • XMLWriter::writeAttributeNS - Write full namespaced attribute
    • XMLWriter::writeCData - Write full CDATA tag
    • XMLWriter::writeComment - Write full comment tag
    • XMLWriter::writeDTD - Write full DTD tag
    • XMLWriter::writeDTDAttlist - Write full DTD AttList tag
    • XMLWriter::writeDTDElement - Write full DTD element tag
    • XMLWriter::writeDTDEntity - Write full DTD Entity tag
    • XMLWriter::writeElement - Write full element tag
    • XMLWriter::writeElementNS - Write full namespaced element tag
    • XMLWriter::writePI - Writes a PI
    • XMLWriter::writeRaw - Write a raw XML text
    • xml_error_string - Get XML parser error string
    • xml_get_current_byte_index - Get current byte index for an XML parser
    • xml_get_current_column_number - Get current column number for an XML parser
    • xml_get_current_line_number - Get current line number for an XML parser
    • xml_get_error_code - Get XML parser error code
    • xml_parse - Start parsing an XML document
    • xml_parser_create - Create an XML parser
    • xml_parser_create_ns - Create an XML parser with namespace support
    • xml_parser_free - Free an XML parser
    • xml_parser_get_option - Get options from an XML parser
    • xml_parser_set_option - Set options in an XML parser
    • xml_parse_into_struct - Parse XML data into an array structure
    • xml_set_character_data_handler - Set up character data handler
    • xml_set_default_handler - Set up default handler
    • xml_set_element_handler - Set up start and end element handlers
    • xml_set_end_namespace_decl_handler - Set up end namespace declaration handler
    • xml_set_external_entity_ref_handler - Set up external entity reference handler
    • xml_set_notation_decl_handler - Set up notation declaration handler
    • xml_set_object - Use XML Parser within an object
    • xml_set_processing_instruction_handler - Set up processing instruction (PI) handler
    • xml_set_start_namespace_decl_handler - Set up start namespace declaration handler
    • xml_set_unparsed_entity_decl_handler - Set up unparsed entity declaration handler
    • XSLTProcessor::getParameter - Get value of a parameter
    • XsltProcessor::getSecurityPrefs - Get security preferences
    • XSLTProcessor::hasExsltSupport - Determine if PHP has EXSLT support
    • XSLTProcessor::importStylesheet - Import stylesheet
    • XSLTProcessor::registerPHPFunctions - Enables the ability to use PHP functions as XSLT functions
    • XSLTProcessor::removeParameter - Remove parameter
    • XSLTProcessor::setParameter - Set value for a parameter
    • XSLTProcessor::setProfiling - Sets profiling output file
    • XsltProcessor::setSecurityPrefs - Set security preferences
    • XSLTProcessor::transformToDoc - Transform to a DOMDocument
    • XSLTProcessor::transformToUri - Transform to URI
    • XSLTProcessor::transformToXML - Transform to XML
    • XSLTProcessor::__construct - Creates a new XSLTProcessor object
  • y
    • Yaf_Action_Abstract::execute - Action entry point
    • Yaf_Action_Abstract::getController - Retrieve controller object
    • Yaf_Application::app - Retrieve an Application instance
    • Yaf_Application::bootstrap - Call bootstrap
    • Yaf_Application::clearLastError - Clear the last error info
    • Yaf_Application::environ - Retrive environ
    • Yaf_Application::execute - Execute a callback
    • Yaf_Application::getAppDirectory - Get the application directory
    • Yaf_Application::getConfig - Retrive the config instance
    • Yaf_Application::getDispatcher - Get Yaf_Dispatcher instance
    • Yaf_Application::getLastErrorMsg - Get message of the last occurred error
    • Yaf_Application::getLastErrorNo - Get code of last occurred error
    • Yaf_Application::getModules - Get defined module names
    • Yaf_Application::run - Start Yaf_Application
    • Yaf_Application::setAppDirectory - Change the application directory
    • Yaf_Application::__clone - Yaf_Application can not be cloned
    • Yaf_Application::__construct - Yaf_Application constructor
    • Yaf_Application::__destruct - The __destruct purpose
    • Yaf_Application::__sleep - Yaf_Application can not be serialized
    • Yaf_Application::__wakeup - Yaf_Application can not be unserialized
    • Yaf_Config_Abstract::get - Getter
    • Yaf_Config_Abstract::readonly - Find a config whether readonly
    • Yaf_Config_Abstract::set - Setter
    • Yaf_Config_Abstract::toArray - Cast to array
    • Yaf_Config_Ini::count - The count purpose
    • Yaf_Config_Ini::current - The current purpose
    • Yaf_Config_Ini::key - The key purpose
    • Yaf_Config_Ini::next - The next purpose
    • Yaf_Config_Ini::offsetExists - The offsetExists purpose
    • Yaf_Config_Ini::offsetGet - The offsetGet purpose
    • Yaf_Config_Ini::offsetSet - The offsetSet purpose
    • Yaf_Config_Ini::offsetUnset - The offsetUnset purpose
    • Yaf_Config_Ini::readonly - The readonly purpose
    • Yaf_Config_Ini::rewind - The rewind purpose
    • Yaf_Config_Ini::toArray - Returns a PHP array
    • Yaf_Config_Ini::valid - The valid purpose
    • Yaf_Config_Ini::__construct - Yaf_Config_Ini constructor
    • Yaf_Config_Ini::__get - The __get purpose
    • Yaf_Config_Ini::__isset - The __isset purpose
    • Yaf_Config_Ini::__set - The __set purpose
    • Yaf_Config_Simple::count - The count purpose
    • Yaf_Config_Simple::current - The current purpose
    • Yaf_Config_Simple::key - The key purpose
    • Yaf_Config_Simple::next - The next purpose
    • Yaf_Config_Simple::offsetExists - The offsetExists purpose
    • Yaf_Config_Simple::offsetGet - The offsetGet purpose
    • Yaf_Config_Simple::offsetSet - The offsetSet purpose
    • Yaf_Config_Simple::offsetUnset - The offsetUnset purpose
    • Yaf_Config_Simple::readonly - The readonly purpose
    • Yaf_Config_Simple::rewind - The rewind purpose
    • Yaf_Config_Simple::toArray - Returns a PHP array
    • Yaf_Config_Simple::valid - The valid purpose
    • Yaf_Config_Simple::__construct - The __construct purpose
    • Yaf_Config_Simple::__get - The __get purpose
    • Yaf_Config_Simple::__isset - The __isset purpose
    • Yaf_Config_Simple::__set - The __set purpose
    • Yaf_Controller_Abstract::display - The display purpose
    • Yaf_Controller_Abstract::forward - foward to another action
    • Yaf_Controller_Abstract::getInvokeArg - The getInvokeArg purpose
    • Yaf_Controller_Abstract::getInvokeArgs - The getInvokeArgs purpose
    • Yaf_Controller_Abstract::getModuleName - Get module name
    • Yaf_Controller_Abstract::getRequest - Retrieve current request object
    • Yaf_Controller_Abstract::getResponse - Retrieve current response object
    • Yaf_Controller_Abstract::getView - Retrieve the view engine
    • Yaf_Controller_Abstract::getViewpath - The getViewpath purpose
    • Yaf_Controller_Abstract::init - Controller initializer
    • Yaf_Controller_Abstract::initView - The initView purpose
    • Yaf_Controller_Abstract::redirect - Redirect to a URL
    • Yaf_Controller_Abstract::render - Render view template
    • Yaf_Controller_Abstract::setViewpath - The setViewpath purpose
    • Yaf_Controller_Abstract::__clone - Yaf_Controller_Abstract can not be cloned
    • Yaf_Controller_Abstract::__construct - Yaf_Controller_Abstract constructor
    • Yaf_Dispatcher::autoRender - Switch on/off autorendering
    • Yaf_Dispatcher::catchException - Switch on/off exception catching
    • Yaf_Dispatcher::disableView - Disable view rendering
    • Yaf_Dispatcher::dispatch - Dispatch a request
    • Yaf_Dispatcher::enableView - enable view rendering
    • Yaf_Dispatcher::flushInstantly - Switch on/off the instant flushing
    • Yaf_Dispatcher::getApplication - Retrive the application
    • Yaf_Dispatcher::getInstance - Retrive the dispatcher instance
    • Yaf_Dispatcher::getRequest - Retrive the request instance
    • Yaf_Dispatcher::getRouter - Retrive router instance
    • Yaf_Dispatcher::initView - Initialize view and return it
    • Yaf_Dispatcher::registerPlugin - Register a plugin
    • Yaf_Dispatcher::returnResponse - The returnResponse purpose
    • Yaf_Dispatcher::setDefaultAction - Change default action name
    • Yaf_Dispatcher::setDefaultController - Change default controller name
    • Yaf_Dispatcher::setDefaultModule - Change default module name
    • Yaf_Dispatcher::setErrorHandler - Set error handler
    • Yaf_Dispatcher::setRequest - The setRequest purpose
    • Yaf_Dispatcher::setView - Set a custom view engine
    • Yaf_Dispatcher::throwException - Switch on/off exception throwing
    • Yaf_Dispatcher::__clone - Yaf_Dispatcher can not be cloned
    • Yaf_Dispatcher::__construct - Yaf_Dispatcher constructor
    • Yaf_Dispatcher::__sleep - Yaf_Dispatcher can not be serialized
    • Yaf_Dispatcher::__wakeup - Yaf_Dispatcher can not be unserialized
    • Yaf_Exception::getPrevious - The getPrevious purpose
    • Yaf_Exception::__construct - The __construct purpose
    • Yaf_Loader::autoload - The autoload purpose
    • Yaf_Loader::clearLocalNamespace - The clearLocalNamespace purpose
    • Yaf_Loader::getInstance - The getInstance purpose
    • Yaf_Loader::getLibraryPath - get the library path
    • Yaf_Loader::getLocalNamespace - The getLocalNamespace purpose
    • Yaf_Loader::import - The import purpose
    • Yaf_Loader::isLocalName - The isLocalName purpose
    • Yaf_Loader::registerLocalNamespace - register local class prefix
    • Yaf_Loader::setLibraryPath - Change the library path
    • Yaf_Loader::__clone - The __clone purpose
    • Yaf_Loader::__construct - The __construct purpose
    • Yaf_Loader::__sleep - The __sleep purpose
    • Yaf_Loader::__wakeup - The __wakeup purpose
    • Yaf_Plugin_Abstract::dispatchLoopShutdown - The dispatchLoopShutdown purpose
    • Yaf_Plugin_Abstract::dispatchLoopStartup - The dispatchLoopStartup purpose
    • Yaf_Plugin_Abstract::postDispatch - The postDispatch purpose
    • Yaf_Plugin_Abstract::preDispatch - The preDispatch purpose
    • Yaf_Plugin_Abstract::preResponse - The preResponse purpose
    • Yaf_Plugin_Abstract::routerShutdown - The routerShutdown purpose
    • Yaf_Plugin_Abstract::routerStartup - RouterStartup hook
    • Yaf_Registry::del - Remove an item from registry
    • Yaf_Registry::get - Retrieve an item from registry
    • Yaf_Registry::has - Check whether an item exists
    • Yaf_Registry::set - Add an item into registry
    • Yaf_Registry::__clone - The __clone purpose
    • Yaf_Registry::__construct - Yaf_Registry implements singleton
    • Yaf_Request_Abstract::getActionName - The getActionName purpose
    • Yaf_Request_Abstract::getBaseUri - The getBaseUri purpose
    • Yaf_Request_Abstract::getControllerName - The getControllerName purpose
    • Yaf_Request_Abstract::getEnv - Retrieve ENV varialbe
    • Yaf_Request_Abstract::getException - The getException purpose
    • Yaf_Request_Abstract::getLanguage - The getLanguage purpose
    • Yaf_Request_Abstract::getMethod - The getMethod purpose
    • Yaf_Request_Abstract::getModuleName - The getModuleName purpose
    • Yaf_Request_Abstract::getParam - The getParam purpose
    • Yaf_Request_Abstract::getParams - The getParams purpose
    • Yaf_Request_Abstract::getRequestUri - The getRequestUri purpose
    • Yaf_Request_Abstract::getServer - Retrieve SERVER variable
    • Yaf_Request_Abstract::isCli - The isCli purpose
    • Yaf_Request_Abstract::isDispatched - The isDispatched purpose
    • Yaf_Request_Abstract::isGet - The isGet purpose
    • Yaf_Request_Abstract::isHead - The isHead purpose
    • Yaf_Request_Abstract::isOptions - The isOptions purpose
    • Yaf_Request_Abstract::isPost - The isPost purpose
    • Yaf_Request_Abstract::isPut - The isPut purpose
    • Yaf_Request_Abstract::isRouted - The isRouted purpose
    • Yaf_Request_Abstract::isXmlHttpRequest - The isXmlHttpRequest purpose
    • Yaf_Request_Abstract::setActionName - The setActionName purpose
    • Yaf_Request_Abstract::setBaseUri - set base URI
    • Yaf_Request_Abstract::setControllerName - The setControllerName purpose
    • Yaf_Request_Abstract::setDispatched - The setDispatched purpose
    • Yaf_Request_Abstract::setModuleName - The setModuleName purpose
    • Yaf_Request_Abstract::setParam - The setParam purpose
    • Yaf_Request_Abstract::setRequestUri - The setRequestUri purpose
    • Yaf_Request_Abstract::setRouted - The setRouted purpose
    • Yaf_Request_Http::get - Retrieve variable from client
    • Yaf_Request_Http::getCookie - Retrieve Cookie variable
    • Yaf_Request_Http::getFiles - The getFiles purpose
    • Yaf_Request_Http::getPost - Retrieve POST variable
    • Yaf_Request_Http::getQuery - Fetch a query parameter
    • Yaf_Request_Http::getRequest - The getRequest purpose
    • Yaf_Request_Http::isXmlHttpRequest - Whether a Ajax Request
    • Yaf_Request_Http::__clone - The __clone purpose
    • Yaf_Request_Http::__construct - Constructor of Yaf_Request_Http
    • Yaf_Request_Simple::get - The get purpose
    • Yaf_Request_Simple::getCookie - The getCookie purpose
    • Yaf_Request_Simple::getFiles - The getFiles purpose
    • Yaf_Request_Simple::getPost - The getPost purpose
    • Yaf_Request_Simple::getQuery - The getQuery purpose
    • Yaf_Request_Simple::getRequest - The getRequest purpose
    • Yaf_Request_Simple::isXmlHttpRequest - The isXmlHttpRequest purpose
    • Yaf_Request_Simple::__clone - The __clone purpose
    • Yaf_Request_Simple::__construct - Constructor of Yaf_Request_Simple
    • Yaf_Response_Abstract::appendBody - append to body
    • Yaf_Response_Abstract::clearBody - The clearBody purpose
    • Yaf_Response_Abstract::clearHeaders - The clearHeaders purpose
    • Yaf_Response_Abstract::getBody - Retrieve a exists content
    • Yaf_Response_Abstract::getHeader - The getHeader purpose
    • Yaf_Response_Abstract::prependBody - The prependBody purpose
    • Yaf_Response_Abstract::response - send response
    • Yaf_Response_Abstract::setAllHeaders - The setAllHeaders purpose
    • Yaf_Response_Abstract::setBody - Set content to response
    • Yaf_Response_Abstract::setHeader - The setHeader purpose
    • Yaf_Response_Abstract::setRedirect - The setRedirect purpose
    • Yaf_Response_Abstract::__clone - The __clone purpose
    • Yaf_Response_Abstract::__construct - The __construct purpose
    • Yaf_Response_Abstract::__destruct - The __destruct purpose
    • Yaf_Response_Abstract::__toString - The __toString purpose
    • Yaf_Router::addConfig - Add config-defined routes into Router
    • Yaf_Router::addRoute - Add new Route into Router
    • Yaf_Router::getCurrentRoute - Get the effective route name
    • Yaf_Router::getRoute - Retrieve a route by name
    • Yaf_Router::getRoutes - Retrieve registered routes
    • Yaf_Router::route - The route purpose
    • Yaf_Router::__construct - Yaf_Router constructor
    • Yaf_Route_Interface::assemble - assemble a request
    • Yaf_Route_Interface::route - route a request
    • Yaf_Route_Map::assemble - Assemble a url
    • Yaf_Route_Map::route - The route purpose
    • Yaf_Route_Map::__construct - The __construct purpose
    • Yaf_Route_Regex::assemble - Assemble a url
    • Yaf_Route_Regex::route - The route purpose
    • Yaf_Route_Regex::__construct - Yaf_Route_Regex constructor
    • Yaf_Route_Rewrite::assemble - Assemble a url
    • Yaf_Route_Rewrite::route - The route purpose
    • Yaf_Route_Rewrite::__construct - Yaf_Route_Rewrite constructor
    • Yaf_Route_Simple::assemble - Assemble a url
    • Yaf_Route_Simple::route - Route a request
    • Yaf_Route_Simple::__construct - Yaf_Route_Simple constructor
    • Yaf_Route_Static::assemble - Assemble a url
    • Yaf_Route_Static::match - The match purpose
    • Yaf_Route_Static::route - Route a request
    • Yaf_Route_Supervar::assemble - Assemble a url
    • Yaf_Route_Supervar::route - The route purpose
    • Yaf_Route_Supervar::__construct - The __construct purpose
    • Yaf_Session::count - The count purpose
    • Yaf_Session::current - The current purpose
    • Yaf_Session::del - The del purpose
    • Yaf_Session::getInstance - The getInstance purpose
    • Yaf_Session::has - The has purpose
    • Yaf_Session::key - The key purpose
    • Yaf_Session::next - The next purpose
    • Yaf_Session::offsetExists - The offsetExists purpose
    • Yaf_Session::offsetGet - The offsetGet purpose
    • Yaf_Session::offsetSet - The offsetSet purpose
    • Yaf_Session::offsetUnset - The offsetUnset purpose
    • Yaf_Session::rewind - The rewind purpose
    • Yaf_Session::start - The start purpose
    • Yaf_Session::valid - The valid purpose
    • Yaf_Session::__clone - The __clone purpose
    • Yaf_Session::__construct - Constructor of Yaf_Session
    • Yaf_Session::__get - The __get purpose
    • Yaf_Session::__isset - The __isset purpose
    • Yaf_Session::__set - The __set purpose
    • Yaf_Session::__sleep - The __sleep purpose
    • Yaf_Session::__unset - The __unset purpose
    • Yaf_Session::__wakeup - The __wakeup purpose
    • Yaf_View_Interface::assign - Assign value to View engine
    • Yaf_View_Interface::display - Render and output a template
    • Yaf_View_Interface::getScriptPath - The getScriptPath purpose
    • Yaf_View_Interface::render - Render a template
    • Yaf_View_Interface::setScriptPath - The setScriptPath purpose
    • Yaf_View_Simple::assign - Assign values
    • Yaf_View_Simple::assignRef - The assignRef purpose
    • Yaf_View_Simple::clear - Clear Assigned values
    • Yaf_View_Simple::display - Render and display
    • Yaf_View_Simple::eval - Render template
    • Yaf_View_Simple::getScriptPath - Get templates directory
    • Yaf_View_Simple::render - Render template
    • Yaf_View_Simple::setScriptPath - Set tempaltes directory
    • Yaf_View_Simple::__construct - Constructor of Yaf_View_Simple
    • Yaf_View_Simple::__get - Retrieve assigned variable
    • Yaf_View_Simple::__isset - The __isset purpose
    • Yaf_View_Simple::__set - Set value to engine
    • yaml_emit - Returns the YAML representation of a value
    • yaml_emit_file - Send the YAML representation of a value to a file
    • yaml_parse - Parse a YAML stream
    • yaml_parse_file - Parse a YAML stream from a file
    • yaml_parse_url - Parse a Yaml stream from a URL
    • Yar_Client::setOpt - Set calling contexts
    • Yar_Client::__call - Call service
    • Yar_Client::__construct - Create a client
    • Yar_Client_Exception::getType - The getType purpose
    • Yar_Concurrent_Client::call - Register a concurrent call
    • Yar_Concurrent_Client::loop - Send all calls
    • Yar_Concurrent_Client::reset - Clean all registered calls
    • Yar_Server::handle - Start RPC Server
    • Yar_Server::__construct - Register a server
    • Yar_Server_Exception::getType - The getType purpose
    • yaz_addinfo - Returns additional error information
    • yaz_ccl_conf - Configure CCL parser
    • yaz_ccl_parse - Invoke CCL Parser
    • yaz_close - Close YAZ connection
    • yaz_connect - Prepares for a connection to a Z39.50 server
    • yaz_database - Specifies the databases within a session
    • yaz_element - Specifies Element-Set Name for retrieval
    • yaz_errno - Returns error number
    • yaz_error - Returns error description
    • yaz_es - Prepares for an Extended Service Request
    • yaz_es_result - Inspects Extended Services Result
    • yaz_get_option - Returns value of option for connection
    • yaz_hits - Returns number of hits for last search
    • yaz_itemorder - Prepares for Z39.50 Item Order with an ILL-Request package
    • yaz_present - Prepares for retrieval (Z39.50 present)
    • yaz_range - Specifies a range of records to retrieve
    • yaz_record - Returns a record
    • yaz_scan - Prepares for a scan
    • yaz_scan_result - Returns Scan Response result
    • yaz_schema - Specifies schema for retrieval
    • yaz_search - Prepares for a search
    • yaz_set_option - Sets one or more options for connection
    • yaz_sort - Sets sorting criteria
    • yaz_syntax - Specifies the preferred record syntax for retrieval
    • yaz_wait - Wait for Z39.50 requests to complete
    • yp_all - Traverse the map and call a function on each entry
    • yp_cat - Return an array containing the entire map
    • yp_errno - Returns the error code of the previous operation
    • yp_err_string - Returns the error string associated with the given error code
    • yp_first - Returns the first key-value pair from the named map
    • yp_get_default_domain - Fetches the machine's default NIS domain
    • yp_master - Returns the machine name of the master NIS server for a map
    • yp_match - Returns the matched line
    • yp_next - Returns the next key-value pair in the named map
    • yp_order - Returns the order number for a map
  • z
    • zend_logo_guid - Gets the Zend guid
    • zend_thread_id - Returns a unique identifier for the current thread
    • zend_version - 현재 젠드 엔진의 버전을 얻습니다
    • ZipArchive::addEmptyDir - Add a new directory
    • ZipArchive::addFile - Adds a file to a ZIP archive from the given path
    • ZipArchive::addFromString - Add a file to a ZIP archive using its contents
    • ZipArchive::addGlob - Add files from a directory by glob pattern
    • ZipArchive::addPattern - Add files from a directory by PCRE pattern
    • ZipArchive::close - Close the active archive (opened or newly created)
    • ZipArchive::deleteIndex - delete an entry in the archive using its index
    • ZipArchive::deleteName - delete an entry in the archive using its name
    • ZipArchive::extractTo - Extract the archive contents
    • ZipArchive::getArchiveComment - Returns the Zip archive comment
    • ZipArchive::getCommentIndex - Returns the comment of an entry using the entry index
    • ZipArchive::getCommentName - Returns the comment of an entry using the entry name
    • ZipArchive::getExternalAttributesIndex - Retrieve the external attributes of an entry defined by its index
    • ZipArchive::getExternalAttributesName - Retrieve the external attributes of an entry defined by its name
    • ZipArchive::getFromIndex - Returns the entry contents using its index
    • ZipArchive::getFromName - Returns the entry contents using its name
    • ZipArchive::getNameIndex - Returns the name of an entry using its index
    • ZipArchive::getStatusString - Returns the status error message, system and/or zip messages
    • ZipArchive::getStream - Get a file handler to the entry defined by its name (read only).
    • ZipArchive::locateName - Returns the index of the entry in the archive
    • ZipArchive::open - Open a ZIP file archive
    • ZipArchive::renameIndex - Renames an entry defined by its index
    • ZipArchive::renameName - Renames an entry defined by its name
    • ZipArchive::setArchiveComment - Set the comment of a ZIP archive
    • ZipArchive::setCommentIndex - Set the comment of an entry defined by its index
    • ZipArchive::setCommentName - Set the comment of an entry defined by its name
    • ZipArchive::setCompressionIndex - Set the compression method of an entry defined by its index
    • ZipArchive::setCompressionName - Set the compression method of an entry defined by its name
    • ZipArchive::setExternalAttributesIndex - Set the external attributes of an entry defined by its index
    • ZipArchive::setExternalAttributesName - Set the external attributes of an entry defined by its name
    • ZipArchive::setPassword - Set the password for the active archive
    • ZipArchive::statIndex - Get the details of an entry defined by its index.
    • ZipArchive::statName - Get the details of an entry defined by its name.
    • ZipArchive::unchangeAll - Undo all changes done in the archive
    • ZipArchive::unchangeArchive - Revert all global changes done in the archive.
    • ZipArchive::unchangeIndex - Revert all changes done to an entry at the given index
    • ZipArchive::unchangeName - Revert all changes done to an entry with the given name.
    • zip_close - Close a ZIP file archive
    • zip_entry_close - Close a directory entry
    • zip_entry_compressedsize - Retrieve the compressed size of a directory entry
    • zip_entry_compressionmethod - Retrieve the compression method of a directory entry
    • zip_entry_filesize - Retrieve the actual file size of a directory entry
    • zip_entry_name - Retrieve the name of a directory entry
    • zip_entry_open - Open a directory entry for reading
    • zip_entry_read - Read from an open directory entry
    • zip_open - Open a ZIP file archive
    • zip_read - Read next entry in a ZIP file archive
    • zlib:// - Compression Streams
    • zlib_decode - Uncompress any raw/gzip/zlib encoded data
    • zlib_encode - Compress data with the specified encoding
    • zlib_get_coding_type - Returns the coding type used for output compression
    • ZMQ::__construct - ZMQ constructor
    • ZMQContext::getOpt - Get context option
    • ZMQContext::getSocket - Create a new socket
    • ZMQContext::isPersistent - Whether the context is persistent
    • ZMQContext::setOpt - Set a socket option
    • ZMQContext::__construct - Construct a new ZMQContext object
    • ZMQDevice::getIdleTimeout - Get the idle timeout
    • ZMQDevice::getTimerTimeout - Get the timer timeout
    • ZMQDevice::run - Run the new device
    • ZMQDevice::setIdleCallback - Set the idle callback function
    • ZMQDevice::setIdleTimeout - Set the idle timeout
    • ZMQDevice::setTimerCallback - Set the timer callback function
    • ZMQDevice::setTimerTimeout - Set the timer timeout
    • ZMQDevice::__construct - Construct a new device
    • ZMQPoll::add - Add item to the poll set
    • ZMQPoll::clear - Clear the poll set
    • ZMQPoll::count - Count items in the poll set
    • ZMQPoll::getLastErrors - Get poll errors
    • ZMQPoll::poll - Poll the items
    • ZMQPoll::remove - Remove item from poll set
    • ZMQSocket::bind - Bind the socket
    • ZMQSocket::connect - Connect the socket
    • ZMQSocket::disconnect - Disconnect a socket
    • ZMQSocket::getEndpoints - Get list of endpoints
    • ZMQSocket::getPersistentId - Get the persistent id
    • ZMQSocket::getSocketType - Get the socket type
    • ZMQSocket::getSockOpt - Get socket option
    • ZMQSocket::isPersistent - Whether the socket is persistent
    • ZMQSocket::recv - Receives a message
    • ZMQSocket::recvMulti - Receives a multipart message
    • ZMQSocket::send - Sends a message
    • ZMQSocket::sendmulti - Sends a multipart message
    • ZMQSocket::setSockOpt - Set a socket option
    • ZMQSocket::unbind - Unbind the socket
    • ZMQSocket::__construct - Construct a new ZMQSocket
  • _
    • __autoload - Attempt to load undefined class
    • __halt_compiler - Halts the compiler execution

User Contributed Notes

pn at bptv dot eu 13-Jul-2016 06:02
There are (07/13/2016 [mm/dd/yyyy]) exactly 9455 Functions listed above.
DIA 14-Apr-2016 06:42
Total number of functions listed above: 9450.
(04/14/2016 [mm/dd/yyyy])
M.A.Y 02-Jan-2016 04:39
Total number of php functions/methods listed above are 9634.
Today's date is 1/1/2016. (mm/dd/yy)
counter at county dot co 07-Apr-2015 11:17
In case anyone was wondering, the total function/method count in this list as of this date is exactly 9457


예제 목록

매뉴얼의 모든 예제 목록

  • Example#0 - 소개용 예제
  • Example#1 - 첫번째 PHP 스크립트: hello.php
  • Example#2 - PHP에서 시스템 정보 얻기
  • Example#3 - 변수 출력하기 (배열 원소)
  • Example#4 - 제어문과 함수를 사용하는 예제
  • Example#5 - HTML과 PHP 모드 섞기
  • Example#6 - 간단한 HTML 폼
  • Example#7 - 폼에서 온 데이터 출력하기
  • Example#8 - Installation Instructions (Apache Shared Module Version) for PHP
  • Example#9 - Installation Instructions (Static Module Installation for Apache) for PHP
  • Example#10 - Example commands for restarting Apache
  • Example#11 - 설치 안내 (아파치 2 공유 모듈 버전)
  • Example#12 - Partial lighttpd.conf
  • Example#13 - Spawning FastCGI Responders
  • Example#14 - Connecting to remote php-fastcgi instances
  • Example#15 - OpenBSD Package Install Example
  • Example#16 - Debian Install Example with Apache 2
  • Example#17 - Stopping and starting Apache once PHP is installed
  • Example#18 - Methods for listing additional PHP 5 packages
  • Example#19 - Install PHP with MySQL, cURL
  • Example#20 - Recommended WinCache configuration
  • Example#21 - Recommended WinCache configuration
  • Example#22 - Command line to configure IIS and PHP
  • Example#23 - PHP 5 package structure
  • Example#24 - CGI and FastCGI settings in php.ini
  • Example#25 - Configuring FastCGI extension to handle PHP requests
  • Example#26 - Configuring file access permissions
  • Example#27 - Configuring FastCGI and PHP recycling
  • Example#28 - Configuring FastCGI timeout settings
  • Example#29 - Changing the location of php.ini file
  • Example#30 - CGI and FastCGI settings in php.ini
  • Example#31 - Creating IIS FastCGI process pool
  • Example#32 - Creating handler mapping for PHP requests
  • Example#33 - Determining the account used as IIS anonymous identity
  • Example#34 - Configuring file access permissions
  • Example#35 - Set index.php as a default document in IIS
  • Example#36 - Configuring FastCGI and PHP recycling
  • Example#37 - Configuring FastCGI timeout settings
  • Example#38 - Changing the location of php.ini file
  • Example#39 - PHP as an Apache 1.3.x module
  • Example#40 - PHP and Apache 1.3.x as CGI
  • Example#41 - PHP and Apache 2.x as handler
  • Example#42 -
  • Example#43 - PHP and Apache 2.x as CGI
  • Example#44 - Configure Apache to run PHP as FastCGI
  • Example#45 - ISAPI configuration of Sambar
  • Example#46 - Enable Bzip2 extension for PHP-Windows
  • Example#47 - Registry changes
  • Example#48 - Passing environment variables and PHP settings to a pool
  • Example#49 - set PHP settings in nginx.conf
  • Example#50 - php.ini Environment Variables
  • Example#51 - php.ini example
  • Example#52 - Apache configuration example
  • Example#53 - 복잡한 벗어나기
  • Example#54 - PHP 시작과 끝 태그
  • Example#55 - Integer literals
  • Example#56 - Integer overflow on a 32-bit system
  • Example#57 - Integer overflow on a 64-bit system
  • Example#58 - Invalid example
  • Example#59 - Valid example
  • Example#60 - Heredoc string quoting example
  • Example#61 - Heredoc in arguments example
  • Example#62 - Using Heredoc to initialize static values
  • Example#63 - Using double quotes in Heredoc
  • Example#64 - Nowdoc string quoting example
  • Example#65 - Static data example
  • Example#66 - Simple syntax example
  • Example#67 - Negative numeric indices
  • Example#68 - Some string examples
  • Example#69 - Differences between PHP 5.3 and PHP 5.4
  • Example#70 - A simple array
  • Example#71 - Type Casting and Overwriting example
  • Example#72 - Mixed integer and string keys
  • Example#73 - Indexed arrays without key
  • Example#74 - Keys not on all elements
  • Example#75 - Accessing array elements
  • Example#76 - Array dereferencing
  • Example#77 - Using array()
  • Example#78 - Collection
  • Example#79 - Changing element in the loop
  • Example#80 - One-based index
  • Example#81 - Filling an array
  • Example#82 - Sorting an array
  • Example#83 - Recursive and multi-dimensional arrays
  • Example#84 - Callback function examples
  • Example#85 - Callback example using a Closure
  • Example#86 - 초기화되지 않은 변수의 기본값
  • Example#87 - global 사용하기
  • Example#88 - global 대신 $GLOBALS 사용하기
  • Example#89 - 자동 전역과 영역을 보여주는 예제
  • Example#90 - 정적 변수의 필요성을 보여주는 예제
  • Example#91 - 정적 변수의 사용 예제
  • Example#92 - 재귀 함수에서 정적 변수
  • Example#93 - 정적 변수 선언하기
  • Example#94 - 단순한 HTML 폼
  • Example#95 - 단순한 POST HTML 폼으로 부터 데이터에 접근하기
  • Example#96 - 좀더 복잡해진 폼 변수들
  • Example#97 - setcookie예제 코드
  • Example#98 - 상수명으로 적합하거나 부적합한 이름들
  • Example#99 - 상수 정의하기
  • Example#100 - const 키워드를 사용해서 상수 정의하기
  • Example#101 - 결합성
  • Example#102 - 표준 배열 비교의 모사
  • Example#103 - 기본값 할당하기
  • Example#104 - 명확하지 않은 삼항 작동
  • Example#105 - 문자 변수에 계산 연산
  • Example#106 - 논리 연산자 설명
  • Example#107 - 비열 비교하기
  • Example#108 - 클래스에 instanceof 사용하기
  • Example#109 - 상속 클래스에 instanceof 사용하기
  • Example#110 - 객체가 클래스의 인스턴스가 아닌지 확인하기 위해 instanceof 사용하기
  • Example#111 - 클래스에 instanceof 사용하기
  • Example#112 - 다른 변수와 instanceof 사용하기
  • Example#113 - PHP 5.0 intanceof에서 클래스명 찾기와 치명적인 오류 피하기
  • Example#114 - switch 구조
  • Example#115 - 문자열을 사용하는 switch 구조
  • Example#116 - PHP 각 코드 섹션의 분석표만들기(Profile)
  • Example#117 - 스크립트의 인코딩 선언하기
  • Example#118 - 기본적인 include 사용예
  • Example#119 - 함수 내에서 인클루드하기
  • Example#120 - HTTP로 include하기
  • Example#121 - include의 반환값 비교하기
  • Example#122 - include와 return문
  • Example#123 - PHP 파일을 포함하여 문자열로 변환하기 위해 출력 버퍼링 사용하기
  • Example#124 - include_once는 윈도우에서 대소문자를 구분하지 못합니다.
  • Example#125 - goto example
  • Example#126 - goto loop example
  • Example#127 - This will not work
  • Example#128 - 함수 사용을 설명하기 위한 가상 코드
  • Example#129 - 조건적인 함수
  • Example#130 - 함수 안의 함수
  • Example#131 - 재귀 함수
  • Example#132 - 함수에 배열 넘겨주기
  • Example#133 - 참조에 의해 함수 인수 전달하기
  • Example#134 - 함수에 기본 인수 사용하기
  • Example#135 - 스칼라 형이 아닌 기본값 사용하기
  • Example#136 - 기본 함수 인수가 잘못 사용된 예
  • Example#137 - 기본 함수 인수의 정확한 사용예
  • Example#138 - Basic class type declaration
  • Example#139 - Basic interface type declaration
  • Example#140 - Nullable type declaration
  • Example#141 - Strict typing
  • Example#142 - Weak typing
  • Example#143 - Catching TypeError
  • Example#144 - 가변 인수에 접근하기 위한 ... 사용하기
  • Example#145 - 인수 제공을 위한 ... 사용하기
  • Example#146 - Type hinted variable arguments
  • Example#147 - PHP 5.5 이전의 가변 길이 인수 접근하기
  • Example#148 - return의 사용예
  • Example#149 - 여러 값을 취하기 위해 배열을 돌려줌
  • Example#150 - 함수에서 참조 돌려주기
  • Example#151 - Basic return type declaration
  • Example#152 - Strict mode in action
  • Example#153 - Returning an object
  • Example#154 - 가변 변수 사용예
  • Example#155 - 가변 메쏘드 사용예
  • Example#156 - Variable method example with static properties
  • Example#157 - Complex callables
  • Example#158 - Anonymous function example
  • Example#159 - Anonymous function variable assignment example
  • Example#160 - Inheriting variables from the parent scope
  • Example#161 - Closures and scoping
  • Example#162 - 간단한 클래스 정의
  • Example#163 - $this 에 대한 몇가지 예제
  • Example#164 - 인스턴스 생성
  • Example#165 - 객체 할당
  • Example#166 - 새로운 객체 생성
  • Example#167 - 간단한 클래스 상속
  • Example#168 - 클래스명 확인
  • Example#169 - 프로퍼티 선언
  • Example#170 - nowdoc을 이용한 프로퍼티 초기화 예제
  • Example#171 - 상수의 정의와 사용
  • Example#172 - 정적 데이터 예제
  • Example#173 - 자동로딩 예제
  • Example#174 - 다른 자동로딩 예제
  • Example#175 - 5.3.0 이후의 자동로딩 예외 핸들링
  • Example#176 - 5.3.0 이후의 자동로딩 예외 핸들링 - 사용자정의 exception 을 찾을수 없음
  • Example#177 - 생성자 사용하기
  • Example#178 - 네임스페이스 클래스의 생성자
  • Example#179 - 소멸자 예제
  • Example#180 - 프로퍼티 선언
  • Example#181 - 메서드 선언
  • Example#182 - 같은 타입의 객체에서의 private 멤버에 대한 접근
  • Example#183 - 상속 예제
  • Example#184 - 클래스 정의 외부에서 ::
  • Example#185 - 클래스 정의 내부에서 ::
  • Example#186 - 부모 메서드 호출하기
  • Example#187 - 정적 프로퍼티 예제
  • Example#188 - 정적 메서드 예제
  • Example#189 - 추상 클래스 예제
  • Example#190 - 추상 클래스 예제
  • Example#191 - 인터페이스 예제
  • Example#192 - 확장가능한 인터페이스
  • Example#193 - 인터페이스 다중 상속
  • Example#194 - 인터페이스 상수
  • Example#195 - 트레이트 예제
  • Example#196 - 우선순위 예제
  • Example#197 - 다른 우선순위 예제
  • Example#198 - 다중 트레이트 사용법
  • Example#199 - 충돌 해결 예제
  • Example#200 - 메서드 가시성 변경 예제
  • Example#201 - Traits Composed from Traits
  • Example#202 - 추상 메서드에 의한 요구사항 표현
  • Example#203 - 정적 변수
  • Example#204 - 정적 메서드
  • Example#205 - 프로퍼티 정의
  • Example#206 - 충돌 해결
  • Example#207 - __get(), __set(), __isset() 과 __unset() 메서드로 프로퍼티 오버로딩하기
  • Example#208 - __call() 과 __callStatic() 로 메서드 오버로딩하기
  • Example#209 - 간단한 객체 순회
  • Example#210 - Iterator 를 구현한 객체 순회
  • Example#211 - IteratorAggregate 를 구현한 객체 순회
  • Example#212 - Sleep 과 wakeup
  • Example#213 - 간단한 예제
  • Example#214 - Using __invoke()
  • Example#215 - __set_state() 의 사용 (PHP 5.1.0 이후)
  • Example#216 - Using __debugInfo()
  • Example#217 - Final 메소드 예제
  • Example#218 - Final 클래스 예제
  • Example#219 - Cloning an object
  • Example#220 - PHP 5의 객체 비교 예제
  • Example#221 - 타입 힌트 예제
  • Example#222 - self:: 사용법
  • Example#223 - static:: 간단한 사용법
  • Example#224 - static:: 비정적 컨텍스트에서의 사용법
  • Example#225 - 전달(Forwarding)과 비전달(non-forwarding) 호출
  • Example#226 - References and Objects
  • Example#227 - Namespace syntax example
  • Example#228 - Declaring a single namespace
  • Example#229 - Declaring a single namespace
  • Example#230 - Declaring a single namespace with hierarchy
  • Example#231 - Declaring multiple namespaces, simple combination syntax
  • Example#232 - Declaring multiple namespaces, bracketed syntax
  • Example#233 - Declaring multiple namespaces and unnamespaced code
  • Example#234 - Declaring multiple namespaces and unnamespaced code
  • Example#235 - Accessing global classes, functions and constants from within a namespace
  • Example#236 - Dynamically accessing elements
  • Example#237 - Dynamically accessing namespaced elements
  • Example#238 - __NAMESPACE__ example, namespaced code
  • Example#239 - __NAMESPACE__ example, global code
  • Example#240 - using __NAMESPACE__ for dynamic name construction
  • Example#241 - the namespace operator, inside a namespace
  • Example#242 - the namespace operator, in global code
  • Example#243 - importing/aliasing with the use operator
  • Example#244 - importing/aliasing with the use operator, multiple use statements combined
  • Example#245 - Importing and dynamic names
  • Example#246 - Importing and fully qualified names
  • Example#247 - Illegal importing rule
  • Example#248 - Using global space specification
  • Example#249 - Accessing global classes inside a namespace
  • Example#250 - global functions/constants fallback inside a namespace
  • Example#251 - Name resolutions illustrated
  • Example#252 - Accessing global classes outside a namespace
  • Example#253 - Accessing global classes outside a namespace
  • Example#254 - Accessing internal classes in namespaces
  • Example#255 - Accessing internal classes, functions or constants in namespaces
  • Example#256 - Fully Qualified names
  • Example#257 - Qualified names
  • Example#258 - Unqualified class names
  • Example#259 - Unqualified function or constant names
  • Example#260 - Dangers of using namespaced names inside a double-quoted string
  • Example#261 - Undefined constants
  • Example#262 - Undefined constants
  • Example#263 - 내장 Exception 클래스
  • Example#264 - Exception 클래스 확장하기 (PHP 5.3.0+)
  • Example#265 - 예외 던지기
  • Example#266 - finally 블록과 예외 다루기
  • Example#267 - 중첩 예외
  • Example#268 - Implementing range as a generator
  • Example#269 - A simple example of yielding values
  • Example#270 - Yielding a key/value pair
  • Example#271 - Yielding NULLs
  • Example#272 - Yielding values by reference
  • Example#273 - yield from with iterator_to_array
  • Example#274 - Basic use of yield from
  • Example#275 - yield from and return values
  • Example#276 - 정의되지 않은 변수에 참조 사용하기
  • Example#277 - 함수 안에서 전역 변수 참조하기
  • Example#278 - 참조와 foreach 구문
  • Example#279 - $GLOBALS example
  • Example#280 - $_SERVER example
  • Example#281 - $_GET example
  • Example#282 - $_POST example
  • Example#283 - $_ENV example
  • Example#284 - $_COOKIE example
  • Example#285 - $php_errormsg example
  • Example#286 - $http_response_header example
  • Example#287 - $argc example
  • Example#288 - $argv example
  • Example#289 - Exception::getMessage example
  • Example#290 - Exception::getPrevious example
  • Example#291 - Exception::getCode example
  • Example#292 - Exception::getFile example
  • Example#293 - Exception::getLine example
  • Example#294 - Exception::getTrace example
  • Example#295 - Exception::getTraceAsString example
  • Example#296 - Exception::__toString example
  • Example#297 - set_error_handler 을 사용하면 ErrorException 의 오류 메시지를 변경할 수 있습니다.
  • Example#298 - ErrorException::getSeverity example
  • Example#299 - 간단한 사용법
  • Example#300 - 기초 사용법
  • Example#301 - 간단한 사용법
  • Example#302 - ArrayAccess::offsetExists example
  • Example#303 - 기초 사용법
  • Example#304 - Closure::bind example
  • Example#305 - Closure::bindTo example
  • Example#306 - Closure::call example
  • Example#307 - Generator::key example
  • Example#308 - Using Generator::send to inject values
  • Example#309 - Throwing an exception into a generator
  • Example#310 - Basic bindto usage example
  • Example#311 - Fetch a page and send POST data
  • Example#312 - Ignore redirects but fetch headers and content
  • Example#313 - Fetch a page and send POST data
  • Example#314 - Detecting which URL we ended up on after redirects
  • Example#315 - php://temp/maxmemory
  • Example#316 - php://filter/resource=<stream to be filtered>
  • Example#317 - php://filter/read=<filter list to apply to read chain>
  • Example#318 - php://filter/write=<filter list to apply to write chain>
  • Example#319 - php://memory and php://temp are not reusable
  • Example#320 - Print data:// contents
  • Example#321 - Fetch the media type
  • Example#322 - Basic usage
  • Example#323 - Opening a stream from an active connection
  • Example#324 - This $session variable must be kept available!
  • Example#325 - Traversing a RAR archive
  • Example#326 - Opening an encrypted file (header encryption)
  • Example#327 - 공격을 유발하는 나쁜 변수 체크 방법
  • Example#328 - 파일시스템 공격
  • Example#329 - 좀더 안전한 파일명 체크
  • Example#330 - 좀더 안전한 파일명 체크
  • Example#331 - null 바이트에 취약한 스크립트
  • Example#332 - 올바른 입력 확인
  • Example#333 - 해시 패스워드 필드 사용하기
  • Example#334 - 결과셋을 페이지로 나눔 ... 그리고 슈퍼유저 만들기 (PostgreSQL)
  • Example#335 - 글을 출력함 ... 그리고 패스워드도 (모든 데이터베이스 서버)
  • Example#336 - 패스워드 재설정에서 ... 더 많은 권한 얻기 (모든 데이터베이스 서버)
  • Example#337 - 데이터베이스 호스트 OS 공격하기 (MSSQL 서버)
  • Example#338 - 페이지 질의를 작성하는 더 안전한 방법
  • Example#339 - Attacking Variables with a custom HTML page
  • Example#340 - Exploiting common debugging variables
  • Example#341 - Finding dangerous variables with E_ALL
  • Example#342 - Example misuse with register_globals = on
  • Example#343 - Example use of sessions with register_globals on or off
  • Example#344 - Detecting simple variable poisoning
  • Example#345 - Dangerous Variable Usage
  • Example#346 - Disabling magic quotes server side
  • Example#347 - Disabling magic quotes at runtime
  • Example#348 - 다른 언어처럼 보이도록 PHP 은닉하기
  • Example#349 - PHP 확장자에 대해 알려지지 않은 파일타입 사용하기
  • Example#350 - PHP 확장자에 대한 HTML 타입 사용하기
  • Example#351 - Basic HTTP 인증 예제
  • Example#352 - Digest HTTP 인증 예제
  • Example#353 - 새 이름/패스워드를 강제하는 HTTP 인증 예제
  • Example#354 - 간단한 XForms 검색 폼
  • Example#355 - XForm을 사용하여 $_POST 생성하기
  • Example#356 - 파일 업로드 폼
  • Example#357 - 파일 업로드 확인하기
  • Example#358 - 파일 배열 업로드하기
  • Example#359 - 복수 파일 전송하기
  • Example#360 - HTTP PUT 파일 저장하기
  • Example#361 - 원격 페이지의 제목을 가져오기
  • Example#362 - 원격 서버에 데이터 저장하기
  • Example#363 - Example showing the difference to the CGI SAPI:
  • Example#364 -
  • Example#365 - Printing built in (and loaded) PHP and Zend modules
  • Example#366 - Getting a syntax error when using double quotes
  • Example#367 - Using single quotes to prevent the shell's variable substitution
  • Example#368 - Using the -B, -R and -E options to count the number of lines of a project.
  • Example#369 - Using -v to get the SAPI name and the version of PHP and Zend
  • Example#370 - --ini example
  • Example#371 - basic --rf usage
  • Example#372 - --rc example
  • Example#373 - --re example
  • Example#374 - --ri example
  • Example#375 - Execute PHP script as shell script
  • Example#376 - Script intended to be run from command line (script.php)
  • Example#377 - Batch file to run a command line PHP script (script.bat)
  • Example#378 - Executing code using the interactive shell
  • Example#379 - Tab completion
  • Example#380 - Setting php.ini settings in the interactive shell
  • Example#381 - Starting the web server
  • Example#382 - Starting with a specific document root directory
  • Example#383 - Using a Router Script
  • Example#384 - Checking for CLI Web Server Use
  • Example#385 - Handling Unsupported File Types
  • Example#386 - Accessing the CLI Web Server From Remote Machines
  • Example#387 - Creating a new zval container
  • Example#388 - Displaying zval information
  • Example#389 - Increasing refcount of a zval
  • Example#390 - Decreasing zval refcount
  • Example#391 - Creating a array zval
  • Example#392 - Adding already existing element to an array
  • Example#393 - Removing an element from an array
  • Example#394 - Adding the array itself as an element of it self
  • Example#395 - Unsetting $a
  • Example#396 - Memory usage example
  • Example#397 - GC performance influences
  • Example#398 - Running the above script
  • Example#399 - Recompiling PHP to enable GC benchmarking
  • Example#400 - GC statistics
  • Example#401 - all_probes.d for tracing all PHP Static Probes with DTrace
  • Example#402 - all_probes.stp for tracing all PHP Static Probes with SystemTap
  • Example#403 - An apc.rfc1867 example
  • Example#404 - A apc_add example
  • Example#405 - apc_bin_load example
  • Example#406 - A apc_cache_info example
  • Example#407 - apc_cas example
  • Example#408 - apc_dec example
  • Example#409 - apc_define_constants example
  • Example#410 - apc_delete_file example
  • Example#411 - A apc_delete example
  • Example#412 - apc_exists example
  • Example#413 - A apc_fetch example
  • Example#414 - apc_inc example
  • Example#415 - apc_load_constants example
  • Example#416 - A apc_sma_info example
  • Example#417 - A apc_store example
  • Example#418 - A APCIterator::__construct example
  • Example#419 - An apc.rfc1867 example
  • Example#420 - A apcu_add example
  • Example#421 - A apcu_cache_info example
  • Example#422 - apcu_cas example
  • Example#423 - apcu_dec example
  • Example#424 - A apcu_delete example
  • Example#425 - An apcu_entry example
  • Example#426 - apcu_exists example
  • Example#427 - A apcu_fetch example
  • Example#428 - apcu_inc example
  • Example#429 - A apcu_sma_info example
  • Example#430 - A apcu_store example
  • Example#431 - A APCUIterator::__construct example
  • Example#432 - Typical session using tcplisten
  • Example#433 - apd_callstack example
  • Example#434 - apd_clunk example
  • Example#435 - apd_continue example
  • Example#436 - apd_croak example
  • Example#437 - apd_dump_function_table example
  • Example#438 - apd_dump_persistent_resources example
  • Example#439 - apd_dump_regular_resources example
  • Example#440 - apd_echo example
  • Example#441 - apd_get_active_symbols example
  • Example#442 - apd_set_pprof_trace example
  • Example#443 - apd_set_session_trace_socket example
  • Example#444 - apd_set_session_trace example
  • Example#445 - apd_set_session example
  • Example#446 - override_function example
  • Example#447 - rename_function example
  • Example#448 - bcompiler_load_exe example
  • Example#449 - bcompiler_load example
  • Example#450 - bcompiler_parse_class example
  • Example#451 - bcompiler_read example
  • Example#452 - bcompiler_write_class example
  • Example#453 - bcompiler_write_constant example
  • Example#454 - bcompiler_write_exe_footer example
  • Example#455 - bcompiler_write_file example
  • Example#456 - bcompiler_write_footer example
  • Example#457 - bcompiler_write_function example
  • Example#458 - bcompiler_write_functions_from_file example
  • Example#459 - bcompiler_write_header example
  • Example#460 - blenc_encrypt example
  • Example#461 - Using error handling in a script
  • Example#462 - debug_backtrace example
  • Example#463 - debug_print_backtrace example
  • Example#464 - An error_clear_last example
  • Example#465 - An error_get_last example
  • Example#466 - error_log examples
  • Example#467 - error_reporting examples
  • Example#468 - restore_error_handler example
  • Example#469 - restore_exception_handler example
  • Example#470 - Error handling with set_error_handler and trigger_error
  • Example#471 - set_exception_handler example
  • Example#472 - trigger_error example
  • Example#473 - Getting the data within the PHP application itself (function)
  • Example#474 - Example use of gengraph.php
  • Example#475 - Listing data via inclued dumps (configuration)
  • Example#476 - inclued_get_data example
  • Example#477 - Creating large array in a function
  • Example#478 - Output Control example
  • Example#479 - Output rewrite example
  • Example#480 - ob_end_clean 예제
  • Example#481 - ob_end_flush 예제
  • Example#482 - 간단한 ob_get_clean 예제
  • Example#483 - 간단한 ob_get_contents 예제
  • Example#484 - ob_get_flush example
  • Example#485 - 간단한 ob_get_length 예제
  • Example#486 - ob_gzhandler 예제
  • Example#487 - ob_list_handlers example
  • Example#488 - 사용자 정의 콜백 함수 예제
  • Example#489 - PHP 5.3과 5.4에서 호환되는 지울 수 없는 출력 버퍼 생성하기
  • Example#490 - output_add_rewrite_var example
  • Example#491 - output_reset_rewrite_vars example
  • Example#492 - assert_options example
  • Example#493 - Handle a failed assertion with a custom handler
  • Example#494 - Using a custom handler to print a description
  • Example#495 - Expectations without a custom exception
  • Example#496 - Expectations with a custom exception
  • Example#497 - cli_get_process_title example
  • Example#498 - cli_set_process_title example
  • Example#499 - dl examples
  • Example#500 - extension_loaded example
  • Example#501 - A gc_enabled example
  • Example#502 - get_current_user 예제
  • Example#503 - get_defined_constants 예제
  • Example#504 - XML 함수 출력
  • Example#505 - get_include_path example
  • Example#506 - get_included_files 예제
  • Example#507 - get_loaded_extensions 예제
  • Example#508 - get_magic_quotes_gpc example
  • Example#509 - get_magic_quotes_runtime example
  • Example#510 - Unfiltered get_resources
  • Example#511 - Filtered get_resources
  • Example#512 - getenv 예제
  • Example#513 - getlastmod 예제
  • Example#514 - getopt example: The basics
  • Example#515 - getopt example: Introducing long options
  • Example#516 - getopt example: Passing multiple options as one
  • Example#517 - getopt example: Using optind
  • Example#518 - getrusage example
  • Example#519 - ini_get_all 예제
  • Example#520 - details 끄기
  • Example#521 - A few ini_get examples
  • Example#522 - ini_restore 예제
  • Example#523 - Setting an ini option
  • Example#524 - A memory_get_usage example
  • Example#525 - php_ini_loaded_file example
  • Example#526 - A simple example to list the returned ini files
  • Example#527 - php_logo_guid example
  • Example#528 - php_sapi_name 예제
  • Example#529 - Some php_uname examples
  • Example#530 - A few OS related constant examples
  • Example#531 - Prints the general credits
  • Example#532 - Prints the core developers and the documentation group
  • Example#533 - Printing all the credits
  • Example#534 - phpinfo Example
  • Example#535 - phpversion example
  • Example#536 - PHP_VERSION_ID example and usage
  • Example#537 - Setting an environment variable
  • Example#538 - restore_include_path example
  • Example#539 - set_include_path example
  • Example#540 - Adding to the include path
  • Example#541 - set_magic_quotes_runtime example
  • Example#542 - sys_get_temp_dir example
  • Example#543 - version_compare examples
  • Example#544 - zend_logo_guid example
  • Example#545 - zend_thread_id example
  • Example#546 - zend_version 예제
  • Example#547 - Custom Superglobals with runkit.superglobal=_FOO,_BAR in php.ini
  • Example#548 - Instantiating a restricted sandbox
  • Example#549 - Working with variables in a sandbox
  • Example#550 - Calling sandbox functions
  • Example#551 - Passing arguments to sandbox functions
  • Example#552 - Working with variables in a sandbox
  • Example#553 - Accessing parental variables
  • Example#554 - A runkit_class_adopt example
  • Example#555 - A runkit_class_emancipate example
  • Example#556 - A runkit_function_add example
  • Example#557 - A runkit_function_copy example
  • Example#558 - A runkit_function_redefine example
  • Example#559 - runkit_import example
  • Example#560 - runkit_method_add example
  • Example#561 - runkit_method_copy example
  • Example#562 - runkit_method_redefine example
  • Example#563 - runkit_method_remove example
  • Example#564 - runkit_method_rename example
  • Example#565 - runkit_return_value_used example
  • Example#566 - Feeding output to a variable
  • Example#567 - Enabling and disabling scream at runtime
  • Example#568 - uopz_backup example
  • Example#569 - uopz_compose example
  • Example#570 - uopz_copy example
  • Example#571 - uopz_delete example
  • Example#572 - uopz_delete class example
  • Example#573 - uopz_extend example
  • Example#574 - uopz_flags example
  • Example#575 - uopz_function example
  • Example#576 - uopz_function class example
  • Example#577 - uopz_implement example
  • Example#578 - uopz_overload example
  • Example#579 - uopz_redefine example
  • Example#580 - uopz_rename example
  • Example#581 - uopz_rename class example
  • Example#582 - uopz_restore example
  • Example#583 - uopz_undefine example
  • Example#584 - Weakref usage example
  • Example#585 - WeakRef usage example
  • Example#586 - Weakref::acquire example
  • Example#587 - Nested acquire/release example
  • Example#588 - Weakref::__construct example
  • Example#589 - Weakref::release example
  • Example#590 - Weakmap usage example
  • Example#591 - wincache.ignorelist example
  • Example#592 - Authentication configuration for wincache.php
  • Example#593 - Enabling WinCache session handler
  • Example#594 - Enabling WinCache functions reroutes
  • Example#595 - Reroute.ini file content
  • Example#596 - A wincache_fcache_fileinfo example
  • Example#597 - A wincache_fcache_meminfo example
  • Example#598 - Using wincache_lock
  • Example#599 - A wincache_ocache_fileinfo example
  • Example#600 - A wincache_ocache_meminfo example
  • Example#601 - A wincache_refresh_if_changed example
  • Example#602 - A wincache_rplist_fileinfo example
  • Example#603 - A wincache_rplist_meminfo example
  • Example#604 - A wincache_scache_info example
  • Example#605 - A wincache_scache_meminfo example
  • Example#606 - wincache_ucache_add with key as a string
  • Example#607 - wincache_ucache_add with key as an array
  • Example#608 - Using wincache_ucache_cas
  • Example#609 - using wincache_ucache_clear
  • Example#610 - Using wincache_ucache_dec
  • Example#611 - Using wincache_ucache_delete with key as a string
  • Example#612 - Usingwincache_ucache_delete with key as an array
  • Example#613 - Using wincache_ucache_delete with key as an array where some elements cannot be deleted
  • Example#614 - Using wincache_ucache_exists
  • Example#615 - wincache_ucache_get with key as a string
  • Example#616 - wincache_ucache_get with key as an array
  • Example#617 - Using wincache_ucache_inc
  • Example#618 - Using wincache_ucache_info
  • Example#619 - A wincache_ucache_meminfo example
  • Example#620 - wincache_ucache_set with key as a string
  • Example#621 - wincache_ucache_set with key as an array
  • Example#622 - Using wincache_unlock
  • Example#623 - Xhprof example with the optional GUI
  • Example#624 - xhprof_disable example
  • Example#625 - xhprof_enable examples
  • Example#626 - xhprof_sample_disable example
  • Example#627 - id3_get_frame_long_name example
  • Example#628 - id3_get_frame_short_name example
  • Example#629 - id3_get_genre_id example
  • Example#630 - id3_get_genre_list example
  • Example#631 - id3_get_genre_name example
  • Example#632 - id3_get_tag example
  • Example#633 - id3_get_tag example
  • Example#634 - id3_get_version example
  • Example#635 - id3_remove_tag example
  • Example#636 - id3_set_tag example
  • Example#637 - Opens a new MP3 file and read the title
  • Example#638 - Reading an OGG/Vorbis file
  • Example#639 - Encode an audio file to OGG/Vorbis
  • Example#640 - KADM5 extension overview example
  • Example#641 - Example of changing principal's password
  • Example#642 - Example of principal's creation
  • Example#643 - kadm5_delete_principal example
  • Example#644 - kadm5_get_policies example
  • Example#645 - kadm5_get_principal example
  • Example#646 - kadm5_get_principals example
  • Example#647 - KADM5 initialization example
  • Example#648 - Example of modifying principal
  • Example#649 - Using CHAP passwords
  • Example#650 - radius_acct_open example
  • Example#651 - radius_add_server example
  • Example#652 - radius_auth_open example
  • Example#653 - radius_create_request example
  • Example#654 - radius_cvt_addr example
  • Example#655 - radius_cvt_int example
  • Example#656 - radius_cvt_string example
  • Example#657 - radius_get_attr example
  • Example#658 - radius_get_tagged_attr_data example
  • Example#659 - radius_get_tagged_attr_tag example
  • Example#660 - radius_get_vendor_attr example
  • Example#661 - radius_put_attr example
  • Example#662 - radius_put_int example
  • Example#663 - radius_put_string example
  • Example#664 - radius_put_vendor_attr example
  • Example#665 - Writing a string with a specified color to the screen
  • Example#666 - ncurses_getmouse example
  • Example#667 - Writing a string with a specified color to the screen
  • Example#668 - Writing a string with a specified color to the screen
  • Example#669 - ncurses_mousemask example
  • Example#670 - Writing a string with a specified color to the screen
  • Example#671 - Newt Usage Example
  • Example#672 - A newt_button example
  • Example#673 - A newt_draw_root_text example
  • Example#674 - A newt_form_add_component example
  • Example#675 - A newt_form_add_components example
  • Example#676 - A newt_form example
  • Example#677 - A newt_get_screen_size example
  • Example#678 - A newt_win_entries example
  • Example#679 - Readline Callback Interface Example
  • Example#680 - readline Example
  • Example#681 - Small bzip2 Example
  • Example#682 - 데이터 압축하기
  • Example#683 - 문자열 압축 풀기
  • Example#684 - bzerror 예제
  • Example#685 - bzopen 예제
  • Example#686 - bzread 예제
  • Example#687 - bzwrite 예제
  • Example#688 - Using an external file
  • Example#689 - Using a file within a phar archive
  • Example#690 - Converting a phar archive from phar to tar file format
  • Example#691 - phar.extract_list usage example
  • Example#692 - phar.cache_list usage example
  • Example#693 - A Phar::addEmptyDir example
  • Example#694 - A Phar::addFile example
  • Example#695 - A Phar::addFromString example
  • Example#696 - A Phar::apiVersion example
  • Example#697 - A Phar::buildFromDirectory example
  • Example#698 - A Phar::buildFromIterator with SplFileInfo
  • Example#699 - A Phar::buildFromIterator with other iterators
  • Example#700 - A Phar::canCompress example
  • Example#701 - A Phar::canWrite example
  • Example#702 - A Phar::compress example
  • Example#703 - A Phar::compressAllFilesBZIP2 example
  • Example#704 - A Phar::compressAllFilesGZ example
  • Example#705 - A Phar::compressFiles example
  • Example#706 - A Phar::__construct example
  • Example#707 - A Phar::convertToData example
  • Example#708 - A Phar::convertToExecutable example
  • Example#709 - A Phar::copy example
  • Example#710 - A Phar::count example
  • Example#711 - A Phar::createDefaultStub example
  • Example#712 - A Phar::decompress example
  • Example#713 - A Phar::decompressFiles example
  • Example#714 - A Phar::delMetaData example
  • Example#715 - A Phar::delete example
  • Example#716 - A Phar::extractTo example
  • Example#717 - A Phar::getMetadata example
  • Example#718 - A Phar::getStub example
  • Example#719 - A Phar::hasMetadata example
  • Example#720 - A Phar::interceptFileFuncs example
  • Example#721 - A Phar::interceptFileFuncs example
  • Example#722 - A Phar::isBuffering example
  • Example#723 - A Phar::isCompressed example
  • Example#724 - A Phar::loadPhar example
  • Example#725 - A Phar::mapPhar example
  • Example#726 - A Phar::mount example
  • Example#727 - A Phar::mungServer example
  • Example#728 - A Phar::offsetExists example
  • Example#729 - Phar::offsetGet example
  • Example#730 - A Phar::offsetSet example
  • Example#731 - A Phar::offsetUnset example
  • Example#732 - A Phar::running example
  • Example#733 - A Phar::setAlias example
  • Example#734 - A Phar::setDefaultStub example
  • Example#735 - A Phar::setMetadata example
  • Example#736 - A Phar::setStub example
  • Example#737 - A Phar::startBuffering example
  • Example#738 - A Phar::stopBuffering example
  • Example#739 - A Phar::uncompressAllFiles example
  • Example#740 - A Phar::unlinkArchive example
  • Example#741 - A Phar::webPhar example
  • Example#742 - A PharData::addEmptyDir example
  • Example#743 - A PharData::addFile example
  • Example#744 - A PharData::addFromString example
  • Example#745 - A PharData::buildFromDirectory example
  • Example#746 - A PharData::buildFromIterator with SplFileInfo
  • Example#747 - A PharData::buildFromIterator with other iterators
  • Example#748 - A PharData::compress example
  • Example#749 - A PharData::compressFiles example
  • Example#750 - A PharData::__construct example
  • Example#751 - A PharData::convertToData example
  • Example#752 - A PharData::convertToExecutable example
  • Example#753 - A PharData::copy example
  • Example#754 - A PharData::decompress example
  • Example#755 - A PharData::decompressFiles example
  • Example#756 - A PharData::delMetaData example
  • Example#757 - A PharData::delete example
  • Example#758 - A PharData::extractTo example
  • Example#759 - A PharData::offsetSet example
  • Example#760 - A PharData::offsetUnset example
  • Example#761 - A Phar::setMetadata example
  • Example#762 - A PharFileInfo::chmod example
  • Example#763 - A PharFileInfo::compress example
  • Example#764 - A PharFileInfo::__construct example
  • Example#765 - A PharFileInfo::decompress example
  • Example#766 - A PharFileInfo::delMetaData example
  • Example#767 - A PharFileInfo::getCRC32 example
  • Example#768 - A PharFileInfo::getCompressedSize example
  • Example#769 - A PharFileInfo::getMetadata example
  • Example#770 - A PharFileInfo::getPharFlags example
  • Example#771 - A PharFileInfo::isCRCChecked example
  • Example#772 - A PharFileInfo::isCompressed example
  • Example#773 - A PharFileInfo::isCompressedBZIP2 example
  • Example#774 - A PharFileInfo::isCompressedGZ example
  • Example#775 - A PharFileInfo::setCompressedBZIP2 example
  • Example#776 - A PharFileInfo::setCompressedGZ example
  • Example#777 - A PharFileInfo::setMetadata example
  • Example#778 - A PharFileInfo::setUncompressed example
  • Example#779 - Rar installation
  • Example#780 - On-the-fly decompression
  • Example#781 - RAR extension filesystem extraction example
  • Example#782 - 객체 기반 형식
  • Example#783 - 절차식 형식
  • Example#784 - 객체 기반 형식
  • Example#785 - 절차식 형식
  • Example#786 - 객체 기반 형식
  • Example#787 - 절차식 형식
  • Example#788 - 객체 기반 형식
  • Example#789 - 절차식 형식
  • Example#790 - 객체 기반 형식
  • Example#791 - 절차식 형식
  • Example#792 - 객체 기반 형식
  • Example#793 - 절차식 형식
  • Example#794 - 객체 기반 형식
  • Example#795 - 절차식 형식
  • Example#796 - Volume Callback
  • Example#797 - 객체 기반 형식
  • Example#798 - 절차식 형식
  • Example#799 - RarArchive::__toString example
  • Example#800 - RarEntry::extract example
  • Example#801 - How to extract all files in archive:
  • Example#802 - RarEntry::getAttr example
  • Example#803 - RarEntry::getHostOs example (version >= 2.0.0)
  • Example#804 - RarEntry::getHostOs example (version <= 1.0.0)
  • Example#805 - RarEntry::getMethod example
  • Example#806 - RarEntry::getName example
  • Example#807 - RarEntry::getPackedSize example
  • Example#808 - RarEntry::getStream example
  • Example#809 - RarEntry::getUnpackedSize example
  • Example#810 - RarEntry::getVersion example
  • Example#811 - RarException::isUsingExceptions example
  • Example#812 - RarException::setUsingExceptions example
  • Example#813 - Create a Zip archive
  • Example#814 - Dump the archive details and listing
  • Example#815 - Zip stream wrapper, read an OpenOffice meta info
  • Example#816 - Zip Usage Example
  • Example#817 - Create a new directory in an archive
  • Example#818 - Open and add
  • Example#819 - Add an entry to a new archive
  • Example#820 - Add file to a directory inside an archive
  • Example#821 - ZipArchive::addGlob example
  • Example#822 - ZipArchive::addPattern example
  • Example#823 - Delete file from archive using its index
  • Example#824 - Deleting a file and directory from an archive, using names
  • Example#825 - Extract all entries
  • Example#826 - Extract two entries
  • Example#827 - Dump an archive comment
  • Example#828 - Dump an entry comment
  • Example#829 - Dump an entry comment
  • Example#830 - Extract all entries with Unix rights
  • Example#831 - Get the file contents
  • Example#832 - Get the file contents
  • Example#833 - Convert an image from a zip entry
  • Example#834 - ZipArchive::getNameIndex example
  • Example#835 - Get the entry contents with fread and store it
  • Example#836 - Same as the previous example but with fopen and the zip stream wrapper
  • Example#837 - Stream wrapper and image, can be used with the xml function as well
  • Example#838 - Create an archive and then use it with ZipArchive::locateName
  • Example#839 - Open and extract
  • Example#840 - Create an archive
  • Example#841 - Rename one entry
  • Example#842 - Rename one entry
  • Example#843 - Create an archive and set a comment
  • Example#844 - Open an archive and set a comment for an entry
  • Example#845 - Open an archive and set a comment for an entry
  • Example#846 - Add files with different compression methods to an archive
  • Example#847 - Add files with different compression methods to an archive
  • Example#848 - Archive a file, with its Unix rights
  • Example#849 - Dump the stat info of an entry
  • Example#850 - Dump the stat info of an entry
  • Example#851 - Small Zlib Example
  • Example#852 - Working with the incremental compression and decompression API as of PHP 7.0.0
  • Example#853 - gzclose example
  • Example#854 - gzcompress example
  • Example#855 - gzdeflate example
  • Example#856 - Creating a gzip file
  • Example#857 - gzeof example
  • Example#858 - gzfile example
  • Example#859 - gzgetc example
  • Example#860 - gzgets example
  • Example#861 - gzgetss example
  • Example#862 - gzinflate example
  • Example#863 - gzopen Example
  • Example#864 - gzpassthru example
  • Example#865 - gzread example
  • Example#866 - gzseek example
  • Example#867 - gzuncompress example
  • Example#868 - gzwrite example
  • Example#869 - zlib_encode example
  • Example#870 - CrackLib example
  • Example#871 - random_bytes example
  • Example#872 - random_int example
  • Example#873 - hash_algos example
  • Example#874 - hash_copy example
  • Example#875 - example
  • Example#876 - Using hash_file
  • Example#877 - hash_final example
  • Example#878 - hash_hmac_file example
  • Example#879 - hash_hmac example
  • Example#880 - Incremental hashing example
  • Example#881 - hash_pbkdf2 example, basic usage
  • Example#882 - hash_update_stream example
  • Example#883 - A hash example
  • Example#884 - Calculate pre PHP-5.4 tiger hashes with PHP-5.4 and higher
  • Example#885 - Encrypt an input value with AES with a 256-bit key under 2.4.x and higher in CBC mode
  • Example#886 - mcrypt_create_iv Example
  • Example#887 - mcrypt_enc_get_algorithms_name example
  • Example#888 - mcrypt_enc_get_modes_name example
  • Example#889 - mcrypt_enc_get_supported_key_sizes example
  • Example#890 - mcrypt_encrypt Example
  • Example#891 - mcrypt_get_block_size example
  • Example#892 - mcrypt_get_cipher_name Example
  • Example#893 - mcrypt_get_iv_size Example
  • Example#894 - mcrypt_get_key_size Example
  • Example#895 - mcrypt_list_algorithms Example
  • Example#896 - mcrypt_list_modes Example
  • Example#897 - mcrypt_module_open Examples
  • Example#898 - Using mcrypt_module_open in encryption
  • Example#899 - mcrypt_module_self_test example
  • Example#900 - mdecrypt_generic Example
  • Example#901 - Computes the MD5 digest and hmac and print it out as hex
  • Example#902 - Traversing all hashes
  • Example#903 - mhash_get_block_size Example
  • Example#904 - mhash_get_hash_name Example
  • Example#905 - openssl_cipher_iv_length example
  • Example#906 - Creating a self-signed-certificate
  • Example#907 - openssl_csr_sign example - signing a CSR (how to implement your own CA)
  • Example#908 - openssl_error_string example
  • Example#909 - openssl_get_cert_locations example
  • Example#910 - openssl_get_cipher_methods example
  • Example#911 - openssl_get_md_methods example
  • Example#912 - openssl_open example
  • Example#913 - openssl_pkcs12_read example
  • Example#914 - openssl_pkcs7_decrypt example
  • Example#915 - openssl_pkcs7_encrypt example
  • Example#916 - openssl_pkcs7_sign example
  • Example#917 - openssl_random_pseudo_bytes example
  • Example#918 - openssl_seal example
  • Example#919 - openssl_sign example
  • Example#920 - openssl_sign example
  • Example#921 - openssl_spki_export_challenge example
  • Example#922 - openssl_spki_export_challenge example from <keygen>
  • Example#923 - openssl_spki_export example
  • Example#924 - openssl_spki_export example from <keygen>
  • Example#925 - openssl_spki_new example
  • Example#926 - openssl_spki_verify example
  • Example#927 - openssl_spki_verify example from <keygen>
  • Example#928 - openssl_verify example
  • Example#929 - openssl_verify example
  • Example#930 - password_hash example
  • Example#931 - password_hash example setting cost manually
  • Example#932 - password_hash example setting salt manually
  • Example#933 - password_hash example finding a good cost
  • Example#934 - Usage of password_needs_rehash
  • Example#935 - password_verify example
  • Example#936 - DBA example
  • Example#937 - Traversing a database
  • Example#938 - dba_handlers Example
  • Example#939 - dbx_close example
  • Example#940 - dbx_compare example
  • Example#941 - dbx_connect example
  • Example#942 - dbx_error example
  • Example#943 - dbx_escape_string example
  • Example#944 - How to handle the returned value
  • Example#945 - lists each field's name and type
  • Example#946 - outputs the content of data property into HTML table
  • Example#947 - How to handle UNBUFFERED queries
  • Example#948 - How to handle the returned value
  • Example#949 - dbx_sort example
  • Example#950 - DSN-less connections
  • Example#951 - odbc_execute and odbc_prepare example
  • Example#952 - odbc_fetch_into examples
  • Example#953 - odbc_next_result
  • Example#954 - odbc_execute and odbc_prepare example
  • Example#955 - odbc_result examples
  • Example#956 - odbc_setoption examples
  • Example#957 - using PDO::ATTR_DRIVER_NAME
  • Example#958 - Connecting to MySQL
  • Example#959 - Handling connection errors
  • Example#960 - Closing a connection
  • Example#961 - Persistent connections
  • Example#962 - Executing a batch in a transaction
  • Example#963 - Repeated inserts using prepared statements
  • Example#964 - Repeated inserts using prepared statements
  • Example#965 - Fetching data using prepared statements
  • Example#966 - Calling a stored procedure with an output parameter
  • Example#967 - Calling a stored procedure with an input/output parameter
  • Example#968 - Invalid use of placeholder
  • Example#969 - Create a PDO instance and set the error mode
  • Example#970 - Create a PDO instance and set the error mode from the constructor
  • Example#971 - Displaying an image from a database
  • Example#972 - Inserting an image into a database
  • Example#973 - Inserting an image into a database: Oracle
  • Example#974 - Roll back a transaction
  • Example#975 - Committing a basic transaction
  • Example#976 - Committing a DDL transaction
  • Example#977 - Create a PDO instance via driver invocation
  • Example#978 - Create a PDO instance via URI invocation
  • Example#979 - Create a PDO instance using an alias
  • Example#980 - Retrieving an SQLSTATE code
  • Example#981 - Displaying errorInfo() fields for a PDO_ODBC connection to a DB2 database
  • Example#982 - Issuing a DELETE statement
  • Example#983 - Retrieving database connection attributes
  • Example#984 - A PDO::getAvailableDrivers example
  • Example#985 - Prepare an SQL statement with named parameters
  • Example#986 - Prepare an SQL statement with question mark parameters
  • Example#987 - Demonstrate PDO::query
  • Example#988 - Quoting a normal string
  • Example#989 - Quoting a dangerous string
  • Example#990 - Quoting a complex string
  • Example#991 - Roll back a transaction
  • Example#992 - Binding result set output to PHP variables
  • Example#993 - Execute a prepared statement with named placeholders
  • Example#994 - Execute a prepared statement with question mark placeholders
  • Example#995 - Call a stored procedure with an INOUT parameter
  • Example#996 - Execute a prepared statement with named placeholders
  • Example#997 - Execute a prepared statement with question mark placeholders
  • Example#998 - A PDOStatement::closeCursor example
  • Example#999 - Counting columns
  • Example#1000 - PDOStatement::debugDumpParams example with named parameters
  • Example#1001 - PDOStatement::debugDumpParams example with unnamed parameters
  • Example#1002 - Retrieving an SQLSTATE code
  • Example#1003 - Displaying errorInfo() fields for a PDO_ODBC connection to a DB2 database
  • Example#1004 - Execute a prepared statement with a bound variable and value
  • Example#1005 - Execute a prepared statement with an array of insert values (named parameters)
  • Example#1006 - Execute a prepared statement with an array of insert values (placeholders)
  • Example#1007 - Execute a prepared statement with question mark placeholders
  • Example#1008 - Execute a prepared statement using array for IN clause
  • Example#1009 - Fetching rows using different fetch styles
  • Example#1010 - Fetching rows with a scrollable cursor
  • Example#1011 - Construction order
  • Example#1012 - Fetch all remaining rows in a result set
  • Example#1013 - Fetching all values of a single column from a result set
  • Example#1014 - Grouping all values by a single column
  • Example#1015 - Instantiating a class for each result
  • Example#1016 - Calling a function for each result
  • Example#1017 - Return first column of the next row
  • Example#1018 - Retrieving column metadata
  • Example#1019 - Fetching multiple rowsets returned from a stored procedure
  • Example#1020 - Return the number of deleted rows
  • Example#1021 - Counting rows returned by a SELECT statement
  • Example#1022 - Setting the fetch mode
  • Example#1023 - Insert LOBs in CUBRID PDO
  • Example#1024 - Fetch LOBs in CUBRID PDO
  • Example#1025 - Insert set in CUBRID PDO with default data type.
  • Example#1026 - Specify data type when insert set in CUBRID PDO
  • Example#1027 - PDO_CUBRID DSN examples
  • Example#1028 - A PDO::cubrid_schema example
  • Example#1029 - PDO_DBLIB DSN examples
  • Example#1030 - PDO_FIREBIRD DSN example with path
  • Example#1031 - PDO_FIREBIRD DSN example with port and path
  • Example#1032 - PDO_FIREBIRD DSN example with localhost and path to employee.fdb on Debian system
  • Example#1033 - PDO_IBM DSN example using db2cli.ini
  • Example#1034 - PDO_IBM DSN example using a connection string
  • Example#1035 - PDO_INFORMIX DSN example using odbc.ini
  • Example#1036 - PDO_INFORMIX DSN example using a connection string
  • Example#1037 - Forcing queries to be buffered in mysql
  • Example#1038 - Setting the connection character set to UTF-8 prior to PHP 5.3.6
  • Example#1039 - PDO_MYSQL DSN examples
  • Example#1040 - PDO_SQLSRV DSN examples
  • Example#1041 - PDO_OCI DSN examples
  • Example#1042 - PDO_ODBC DSN example (ODBC driver manager)
  • Example#1043 - PDO_ODBC DSN example (IBM DB2 uncataloged connection)
  • Example#1044 - PDO_ODBC DSN example (Microsoft Access uncataloged connection)
  • Example#1045 - PDO_PGSQL DSN 예제
  • Example#1046 - PDO::pgsqlLOBCreate 예제
  • Example#1047 - PDO::pgsqlLOBOpen 예제
  • Example#1048 - PDO::pgsqlLOBUnlink 예제
  • Example#1049 - PDO_SQLITE DSN examples
  • Example#1050 - max_length aggregation function example
  • Example#1051 - PDO::sqliteCreateCollation example
  • Example#1052 - PDO::sqliteCreateFunction example
  • Example#1053 - DSN examples for PDO_4D
  • Example#1054 - Basic example with PDO_4D
  • Example#1055 - Accessing 4D language from pdo_4d
  • Example#1056 - Escaping 4D table names
  • Example#1057 - Example of Data Retrieval
  • Example#1058 - Example of Data Insertion
  • Example#1059 - cubrid_bind example
  • Example#1060 - cubrid_bind BLOB/CLOB example
  • Example#1061 - cubrid_bind BLOB/CLOB example
  • Example#1062 - cubrid_close_prepare example
  • Example#1063 - cubrid_close_request example
  • Example#1064 - cubrid_col_get example
  • Example#1065 - cubrid_col_size example
  • Example#1066 - cubrid_column_names example
  • Example#1067 - cubrid_column_types example
  • Example#1068 - cubrid_commit example
  • Example#1069 - cubrid_connect_with_url url without properties example
  • Example#1070 - cubrid_connect_with_url url with properties example
  • Example#1071 - cubrid_connect example
  • Example#1072 - cubrid_current_oid example
  • Example#1073 - cubrid_disconnect example
  • Example#1074 - cubrid_drop example
  • Example#1075 - cubrid_error_code_facility example
  • Example#1076 - cubrid_error_code example
  • Example#1077 - cubrid_error_msg example
  • Example#1078 - cubrid_execute example
  • Example#1079 - cubrid_fetch example
  • Example#1080 - cubrid_free_result example
  • Example#1081 - cubrid_get_charset example
  • Example#1082 - cubrid_get_class_name example
  • Example#1083 - cubrid_get_client_info example
  • Example#1084 - cubrid_get_db_parameter example
  • Example#1085 - cubrid_get_query_timeout example
  • Example#1086 - cubrid_get_server_info example
  • Example#1087 - cubrid_get example
  • Example#1088 - cubrid_insert_id example
  • Example#1089 - cubrid_is_instance example
  • Example#1090 - cubrid_lob_close example
  • Example#1091 - cubrid_lob_export example
  • Example#1092 - cubrid_lob_get example
  • Example#1093 - cubrid_lob_send example
  • Example#1094 - cubrid_lob_size example
  • Example#1095 - cubrid_lob2_bind example
  • Example#1096 - cubrid_lob2_export example
  • Example#1097 - cubrid_lob2_export example
  • Example#1098 - cubrid_lob2_read example 1
  • Example#1099 - cubrid_lob2_read example 2
  • Example#1100 - cubrid_lob2_seek64 example
  • Example#1101 - cubrid_lob2_seek example
  • Example#1102 - cubrid_lob2_write example 1
  • Example#1103 - cubrid_lob2_write example 2
  • Example#1104 - cubrid_lock_read example
  • Example#1105 - cubrid_lock_write example
  • Example#1106 - cubrid_move_cursor example
  • Example#1107 - cubrid_next_result example
  • Example#1108 - cubrid_num_cols example
  • Example#1109 - cubrid_num_rows example
  • Example#1110 - cubrid_pconnect_with_url url without properties example
  • Example#1111 - cubrid_pconnect_with_url url with properties example
  • Example#1112 - cubrid_connect example
  • Example#1113 - cubrid_prepare example
  • Example#1114 - cubrid_put example
  • Example#1115 - cubrid_rollback example
  • Example#1116 - cubrid_schema example
  • Example#1117 - cubrid_seq_drop example
  • Example#1118 - cubrid_seq_insert example
  • Example#1119 - cubrid_seq_put example
  • Example#1120 - cubrid_set_add example
  • Example#1121 - cubrid_get_db_parameter example
  • Example#1122 - cubrid_set_drop example
  • Example#1123 - cubrid_version example
  • Example#1124 - cubrid_affected_rows example
  • Example#1125 - cubrid_client_encoding example
  • Example#1126 - cubrid_close example
  • Example#1127 - cubrid_data_seek example
  • Example#1128 - cubrid_db_name example
  • Example#1129 - cubrid_errno example
  • Example#1130 - cubrid_error example
  • Example#1131 - cubrid_fetch_array example
  • Example#1132 - cubrid_fetch_assoc example
  • Example#1133 - cubrid_fetch_field example
  • Example#1134 - cubrid_fetch_lengths example
  • Example#1135 - cubrid_fetch_object example
  • Example#1136 - cubrid_fetch_row example
  • Example#1137 - cubrid_field_flags example
  • Example#1138 - cubrid_field_len example
  • Example#1139 - cubrid_field_name example
  • Example#1140 - cubrid_field_seek example
  • Example#1141 - cubrid_field_table example
  • Example#1142 - cubrid_field_type example
  • Example#1143 - cubrid_list_dbs example
  • Example#1144 - cubrid_num_fields example
  • Example#1145 - cubrid_ping example
  • Example#1146 - Invalid Query
  • Example#1147 - Valid Query
  • Example#1148 - cubrid_real_escape_string example
  • Example#1149 - cubrid_result example
  • Example#1150 - cubrid_unbuffered_query example
  • Example#1151 - cubrid_load_from_glo example
  • Example#1152 - cubrid_new_glo example
  • Example#1153 - cubrid_save_to_glo example
  • Example#1154 - cubrid_send_glo example
  • Example#1155 - Inserting a record in a dBase database
  • Example#1156 - Closing a dBase database file
  • Example#1157 - Creating a dBase database file
  • Example#1158 - Showing header information for a dBase database file
  • Example#1159 - Listing all the registered members in the database
  • Example#1160 - dbase_numfields Example
  • Example#1161 - Looping over all the records of the database
  • Example#1162 - Opening a dBase database file
  • Example#1163 - Emptying a dBase database
  • Example#1164 - Updating a record in the database
  • Example#1165 - ibase_blob_get example
  • Example#1166 - ibase_blob_import example
  • Example#1167 - ibase_connect example
  • Example#1168 - ibase_execute example
  • Example#1169 - ibase_fetch_object example
  • Example#1170 - ibase_field_info example
  • Example#1171 - ibase_name_result example
  • Example#1172 - ibase_num_fields example
  • Example#1173 - ibase_query example
  • Example#1174 - ibase_set_event_handler example
  • Example#1175 - fbsql_close example
  • Example#1176 - fbsql_connect example
  • Example#1177 - fbsql_create_blob example
  • Example#1178 - fbsql_create_clob example
  • Example#1179 - fbsql_create_db example
  • Example#1180 - fbsql_data_seek example
  • Example#1181 - fbsql_create_clob example
  • Example#1182 - fbsql_errno Example
  • Example#1183 - fbsql_error Example
  • Example#1184 - fbsql_fetch_array example
  • Example#1185 - fbsql_fetch_assoc example
  • Example#1186 - fbsql_fetch_field example
  • Example#1187 - fbsql_fetch_object example
  • Example#1188 - fbsql_field_name example
  • Example#1189 - fbsql_field_type example
  • Example#1190 - fbsql_list_dbs example
  • Example#1191 - fbsql_list_fields example
  • Example#1192 - fbsql_next_result example
  • Example#1193 - fbsql_num_rows example
  • Example#1194 - fbsql_query example
  • Example#1195 - fbsql_query example
  • Example#1196 - fbsql_read_blob example
  • Example#1197 - fbsql_read_clob example
  • Example#1198 - fbsql_table_name example
  • Example#1199 - Retrieving the AUTOCOMMIT value for a connection
  • Example#1200 - Setting the AUTOCOMMIT value for a connection
  • Example#1201 - Binding PHP variables to a prepared statement
  • Example#1202 - Calling stored procedures with IN and OUT parameters
  • Example#1203 - Inserting a binary large object (BLOB) directly from a file
  • Example#1204 - A db2_client_info example
  • Example#1205 - Closing a connection
  • Example#1206 - Retrieving an SQLSTATE value for a failed connection attempt
  • Example#1207 - Retrieving the error message returned by a failed connection attempt
  • Example#1208 - Creating a cataloged connection
  • Example#1209 - Creating an uncataloged connection
  • Example#1210 - Creating a connection with autocommit off by default
  • Example#1211 - i5/OS best performance
  • Example#1212 - Using trusted context
  • Example#1213 - A db2_escape_string example
  • Example#1214 - Creating a table with db2_exec
  • Example#1215 - Executing a SELECT statement with a scrollable cursor
  • Example#1216 - Returning XML data as an SQL ResultSet
  • Example#1217 - Performing a "JOIN" with XML data
  • Example#1218 - Returning SQL data as part of a larger XML document
  • Example#1219 - Preparing and executing an SQL statement with parameter markers
  • Example#1220 - Calling a stored procedure with an OUT parameter
  • Example#1221 - Returning XML data as an SQL ResultSet
  • Example#1222 - Performing a "JOIN" with XML data
  • Example#1223 - Returning SQL data as part of a larger XML document
  • Example#1224 - Iterating through a forward-only cursor
  • Example#1225 - Retrieving specific rows with db2_fetch_array from a scrollable cursor
  • Example#1226 - Iterating through a forward-only cursor
  • Example#1227 - Retrieving specific rows with db2_fetch_assoc from a scrollable cursor
  • Example#1228 - Iterating through a forward-only cursor
  • Example#1229 - Retrieving specific rows with db2_fetch_both from a scrollable cursor
  • Example#1230 - A db2_fetch_object example
  • Example#1231 - Iterating through a result set
  • Example#1232 - i5/OS recommended alternatives to db2_fetch_row/db2_result
  • Example#1233 - Setting and retrieving parameters through a connection resource
  • Example#1234 - A db2_last_insert_id example
  • Example#1235 - Iterating through different types of data
  • Example#1236 - Calling a stored procedure that returns multiple result sets
  • Example#1237 - Retrieving the number of fields in a result set
  • Example#1238 - Closing a persistent connection
  • Example#1239 - A db2_pconnect example
  • Example#1240 - Using trusted context
  • Example#1241 - Preparing and executing an SQL statement with parameter markers
  • Example#1242 - A db2_result example
  • Example#1243 - Rolling back a DELETE statement
  • Example#1244 - A db2_server_info example
  • Example#1245 - Setting one parameter with a connection resource
  • Example#1246 - Setting multiple parameters with a connection resource
  • Example#1247 - Setting multiple parameters with an invalid key
  • Example#1248 - Setting multiple parameters with an invalid value
  • Example#1249 - Setting multiple parameters with a connection resource and the wrong type
  • Example#1250 - Setting multiple parameters with the wrong resource
  • Example#1251 - Putting it all together
  • Example#1252 - i5/OS cursors are read-only
  • Example#1253 - Informix affected rows
  • Example#1254 - Closing a Informix connection
  • Example#1255 - Connect to a Informix database
  • Example#1256 - ifx_do Example
  • Example#1257 - ifx_errormsg example
  • Example#1258 - Informix fetch rows
  • Example#1259 - Informix SQL fieldproperties
  • Example#1260 - Fieldnames and SQL fieldtypes
  • Example#1261 - Retrieve Informix sqlca.sqlerrd[x] values
  • Example#1262 - Informix results as HTML table
  • Example#1263 - ifx_num_fields Example
  • Example#1264 - Show all rows of the "orders" table as a HTML table
  • Example#1265 - Insert some values into the "catalog" table
  • Example#1266 - Example usage of PassEnv for Ingres
  • Example#1267 - Simple Ingres Example
  • Example#1268 - ingres_charset - Get the installation character set
  • Example#1269 - Open a connection to an Ingres database
  • Example#1270 - Get cursor name for a query resource
  • Example#1271 - Get the last Ingres error number generated
  • Example#1272 - Get a message for the last error generated
  • Example#1273 - Get the last SQLSTATE error code generated
  • Example#1274 - Escape special characters for use in a query
  • Example#1275 - Fetch a row of result into an array
  • Example#1276 - Fetch a row into an associative array
  • Example#1277 - Fetch a row into an object
  • Example#1278 - Get the return value from a procedure call
  • Example#1279 - Fetch a row of result into an enumerated array
  • Example#1280 - Free a result resource
  • Example#1281 - Send a simple select
  • Example#1282 - Passing query parameters to ingres_query
  • Example#1283 - Inserting a BLOB with parameter types
  • Example#1284 - Position the cursor on the 3rd row
  • Example#1285 - Set date_format to ISO4
  • Example#1286 - Set timezone to HONG-KONG
  • Example#1287 - Issue a simple un-buffered select
  • Example#1288 - Passing query parameters to ingres_unbuffered_query
  • Example#1289 - Inserting a BLOB with parameter types
  • Example#1290 - MaxDB extension overview example
  • Example#1291 - Example for use of SELECT INTO statements
  • Example#1292 - Example fore using database procedures
  • Example#1293 - 객체 기반 형식
  • Example#1294 - 절차식 형식
  • Example#1295 - 객체 기반 형식
  • Example#1296 - 절차식 형식
  • Example#1297 - 객체 기반 형식
  • Example#1298 - 절차식 형식
  • Example#1299 - 객체 기반 형식
  • Example#1300 - 절차식 형식
  • Example#1301 - 객체 기반 형식
  • Example#1302 - 절차식 형식
  • Example#1303 - maxdb_connect_errno sample
  • Example#1304 - maxdb_connect_error sample
  • Example#1305 - 객체 기반 형식
  • Example#1306 - 절차식 형식
  • Example#1307 - 객체 기반 형식
  • Example#1308 - 절차식 형식
  • Example#1309 - 절차식 형식
  • Example#1310 - 객체 기반 형식
  • Example#1311 - 절차식 형식
  • Example#1312 - 객체 기반 형식
  • Example#1313 - 절차식 형식
  • Example#1314 - 객체 기반 형식
  • Example#1315 - 절차식 형식
  • Example#1316 - 객체 기반 형식
  • Example#1317 - 절차식 형식
  • Example#1318 - 객체 기반 형식
  • Example#1319 - 절차식 형식
  • Example#1320 - 객체 기반 형식
  • Example#1321 - 절차식 형식
  • Example#1322 - 객체 기반 형식
  • Example#1323 - 절차식 형식
  • Example#1324 - 객체 기반 형식
  • Example#1325 - 절차식 형식
  • Example#1326 - 객체 기반 형식
  • Example#1327 - 절차식 형식
  • Example#1328 - 객체 기반 형식
  • Example#1329 - 절차식 형식
  • Example#1330 - 객체 기반 형식
  • Example#1331 - 절차식 형식
  • Example#1332 - 객체 기반 형식
  • Example#1333 - 절차식 형식
  • Example#1334 - 객체 기반 형식
  • Example#1335 - 절차식 형식
  • Example#1336 - maxdb_get_client_info
  • Example#1337 - maxdb_get_client_version
  • Example#1338 - 객체 기반 형식
  • Example#1339 - 절차식 형식
  • Example#1340 - 객체 기반 형식
  • Example#1341 - 절차식 형식
  • Example#1342 - 객체 기반 형식
  • Example#1343 - 절차식 형식
  • Example#1344 - 객체 기반 형식
  • Example#1345 - 절차식 형식
  • Example#1346 - 객체 기반 형식
  • Example#1347 - 절차식 형식
  • Example#1348 - 객체 기반 형식
  • Example#1349 - 절차식 형식
  • Example#1350 - 객체 기반 형식
  • Example#1351 - 절차식 형식
  • Example#1352 - 객체 기반 형식
  • Example#1353 - 절차식 형식
  • Example#1354 - 객체 기반 형식
  • Example#1355 - 절차식 형식
  • Example#1356 - 객체 기반 형식
  • Example#1357 - 절차식 형식
  • Example#1358 - 객체 기반 형식
  • Example#1359 - 절차식 형식
  • Example#1360 - 객체 기반 형식
  • Example#1361 - 절차식 형식
  • Example#1362 - 객체 기반 형식
  • Example#1363 - 절차식 형식
  • Example#1364 - 객체 기반 형식
  • Example#1365 - 절차식 형식
  • Example#1366 - 객체 기반 형식
  • Example#1367 - 절차식 형식
  • Example#1368 - 절차식 형식
  • Example#1369 - 객체 기반 형식
  • Example#1370 - 절차식 형식
  • Example#1371 - 객체 기반 형식
  • Example#1372 - 절차식 형식
  • Example#1373 - 객체 기반 형식
  • Example#1374 - 절차식 형식
  • Example#1375 - 객체 기반 형식
  • Example#1376 - 절차식 형식
  • Example#1377 - 객체 기반 형식
  • Example#1378 - 절차식 형식
  • Example#1379 - 객체 기반 형식
  • Example#1380 - 절차식 형식
  • Example#1381 - 절차식 형식 (SELECT INTO)
  • Example#1382 - 절차식 형식 (DB procedure)
  • Example#1383 - 객체 기반 형식 (extended syntax)
  • Example#1384 - 객체 기반 형식
  • Example#1385 - 절차식 형식
  • Example#1386 - 객체 기반 형식
  • Example#1387 - 절차식 형식
  • Example#1388 - 객체 기반 형식
  • Example#1389 - 절차식 형식
  • Example#1390 - 객체 기반 형식
  • Example#1391 - 절차식 형식
  • Example#1392 - 객체 기반 형식
  • Example#1393 - 절차식 형식
  • Example#1394 - 객체 기반 형식
  • Example#1395 - 절차식 형식
  • Example#1396 - 객체 기반 형식
  • Example#1397 - 절차식 형식
  • Example#1398 - 객체 기반 형식
  • Example#1399 - 절차식 형식
  • Example#1400 - 객체 기반 형식
  • Example#1401 - 절차식 형식
  • Example#1402 - 객체 기반 형식
  • Example#1403 - 절차식 형식
  • Example#1404 - 객체 기반 형식
  • Example#1405 - 절차식 형식
  • Example#1406 - 객체 기반 형식
  • Example#1407 - 절차식 형식
  • Example#1408 - 객체 기반 형식
  • Example#1409 - 절차식 형식
  • Example#1410 - 객체 기반 형식
  • Example#1411 - 절차식 형식
  • Example#1412 - 객체 기반 형식
  • Example#1413 - 절차식 형식
  • Example#1414 -
  • Example#1415 -
  • Example#1416 -
  • Example#1417 -
  • Example#1418 -
  • Example#1419 -
  • Example#1420 -
  • Example#1421 -
  • Example#1422 -
  • Example#1423 -
  • Example#1424 -
  • Example#1425 -
  • Example#1426 -
  • Example#1427 -
  • Example#1428 -
  • Example#1429 - Connection URI read preferences with query string syntax
  • Example#1430 - Setting read preferences with array syntax for tag sets
  • Example#1431 - Passing a WriteConcern to a write operation
  • Example#1432 - Connection string WriteConcerns
  • Example#1433 - MongoDB::setWriteConcern and MongoCollection::setWriteConcern
  • Example#1434 - Unacknowledged WriteConcern, followed with Acknowledged Write
  • Example#1435 - Acknowledged Writes
  • Example#1436 - Majority Acknowledged Write
  • Example#1437 - Acknowledged and Journaled Write
  • Example#1438 - Connect to MongoDB Instance with SSL Encryption
  • Example#1439 - Connect to MongoDB Instance with SSL Encryption, verifying it is who we think it is
  • Example#1440 - Connect to MongoDB Instance that Requires Client Certificates
  • Example#1441 - Authenticating with X.509 certificates
  • Example#1442 - Authenticating against the "admin" database
  • Example#1443 - Authenticating against normal databases
  • Example#1444 - ReplicaSet seed list
  • Example#1445 - Wrong replica set name duplication
  • Example#1446 - Correct use of two replica set names
  • Example#1447 -
  • Example#1448 -
  • Example#1449 -
  • Example#1450 -
  • Example#1451 -
  • Example#1452 -
  • Example#1453 - Inheriting read preferences from the database level down to the cursor
  • Example#1454 -
  • Example#1455 -
  • Example#1456 -
  • Example#1457 -
  • Example#1458 -
  • Example#1459 -
  • Example#1460 -
  • Example#1461 - MongoClient basic usage
  • Example#1462 - MongoClient::close example
  • Example#1463 - MongoClient::__construct replica set example
  • Example#1464 - Connecting to a domain socket
  • Example#1465 - MongoClient::__construct authentication example
  • Example#1466 - MongoClient::__construct read preference example
  • Example#1467 - MongoClient::__construct options example
  • Example#1468 - MongoClient::__construct read preference example
  • Example#1469 - MongoClient::getConnections example
  • Example#1470 - MongoClient::getReadPreference return value example
  • Example#1471 - MongoClient::getWriteConcern return value example
  • Example#1472 - MongoClient::killCursor example
  • Example#1473 - MongoClient::listDBs example
  • Example#1474 - MongoClient::selectCollection example
  • Example#1475 - MongoClient::setReadPreference tag set array syntax example
  • Example#1476 - MongoClient::setWriteConcern example
  • Example#1477 - Selecting a database
  • Example#1478 - MongoDB::command "distinct" example
  • Example#1479 - MongoDB::command "distinct" example
  • Example#1480 - MongoDB::command MapReduce example
  • Example#1481 - MongoDB::command "geoNear" example
  • Example#1482 - MongoDB::createCollection capped collection example
  • Example#1483 - MongoDB::createDBRef example
  • Example#1484 - MongoDB::createDBRef example
  • Example#1485 - MongoDB::drop example
  • Example#1486 - Simple MongoDB::execute example
  • Example#1487 - Parameter MongoDB::execute example
  • Example#1488 - Scope example
  • Example#1489 - MongoDB::getCollectionInfo example
  • Example#1490 - MongoDB::getCollectionNames example
  • Example#1491 - MongoDB::getDBRef example
  • Example#1492 - MongoDB::getGridFS example
  • Example#1493 - MongoDB::getReadPreference return value example
  • Example#1494 - MongoDB::getWriteConcern return value example
  • Example#1495 - MongoDB::lastError NULL error example
  • Example#1496 - MongoDB::lastError duplicate key example
  • Example#1497 - MongoDB::listCollections example
  • Example#1498 - MongoDB::repair example
  • Example#1499 - MongoDB::setReadPreference tag set array syntax example
  • Example#1500 - MongoDB::setWriteConcern example
  • Example#1501 - MongoCollection::aggregate example
  • Example#1502 - MongoCollection::aggregate example
  • Example#1503 - MongoCollection::aggregate example
  • Example#1504 - MongoCollection::aggregate with command options
  • Example#1505 - MongoCollection::aggregateCursor example
  • Example#1506 - MongoCollection::aggregateCursor example with different initial batch size
  • Example#1507 - MongoCollection::batchInsert example
  • Example#1508 - MongoCollection::batchInsert example with ignoring errors
  • Example#1509 - MongoCollection::count example
  • Example#1510 - MongoCollection::createDBRef example
  • Example#1511 - MongoCollection::createIndex example
  • Example#1512 - Geospatial Indexing
  • Example#1513 - Drop duplicates example
  • Example#1514 - MongoCollection::deleteIndex example
  • Example#1515 - MongoCollection::deleteIndexes example
  • Example#1516 - MongoCollection::distinct example
  • Example#1517 - MongoCollection::distinct example on a embedded document
  • Example#1518 - MongoCollection::drop example
  • Example#1519 - MongoCollection::ensureIndex example
  • Example#1520 - Geospatial Indexing
  • Example#1521 - Drop duplicates example
  • Example#1522 - MongoCollection::find example
  • Example#1523 - MongoCollection::find example
  • Example#1524 - MongoCollection::find example using $where
  • Example#1525 - MongoCollection::find example using $in
  • Example#1526 - Getting results as an array
  • Example#1527 - MongoCollection::findAndModify example
  • Example#1528 - MongoCollection::findAndModify error handling
  • Example#1529 - MongoCollection::findOne document by its id.
  • Example#1530 - MongoCollection::findOne document by some condition.
  • Example#1531 - MongoCollection::getDBRef example
  • Example#1532 - MongoCollection::getIndexInfo example
  • Example#1533 - MongoCollection::getName example
  • Example#1534 - MongoCollection::getReadPreference return value example
  • Example#1535 - MongoCollection::getWriteConcern return value example
  • Example#1536 - MongoCollection::group example
  • Example#1537 - MongoCollection::group example
  • Example#1538 - Passing a keys function
  • Example#1539 - MongoCollection::insert _id example
  • Example#1540 - MongoCollection::insert acknowledged write example
  • Example#1541 - MongoCollection::parallelCollectionScan example
  • Example#1542 - MongoCollection::remove with justOne example
  • Example#1543 - MongoCollection::save example
  • Example#1544 - MongoCollection::setReadPreference tag set array syntax example
  • Example#1545 - MongoDB::setWriteConcern example
  • Example#1546 - MongoCollection::toIndexString example
  • Example#1547 - MongoCollection::__toString example
  • Example#1548 - MongoCollection::update
  • Example#1549 - MongoCollection::update upsert examples
  • Example#1550 - MongoCollection::update multiple example
  • Example#1551 - MongoCursor basic usage
  • Example#1552 - Iterating over MongoCursor
  • Example#1553 - Adding options to MongoCursor
  • Example#1554 - Adding a comment with MongoCursor::addOption example
  • Example#1555 - MongoCursor::addOption example
  • Example#1556 - MongoCursor::awaitData example
  • Example#1557 - MongoCursor::batchSize and combinations with MongoCursor::limit
  • Example#1558 - MongoCursor::count example
  • Example#1559 - MongoCursor::doQuery example
  • Example#1560 - MongoCursor::explain example
  • Example#1561 - MongoCursor::getReadPreference return value example
  • Example#1562 - MongoCursor::info example
  • Example#1563 - MongoCursor::maxTimeMS example
  • Example#1564 - MongoCursor::setFlag example
  • Example#1565 - MongoCursor::setReadPreference tag set array syntax example
  • Example#1566 - MongoCursor::slaveOkay example
  • Example#1567 - MongoCursor::sort example
  • Example#1568 - MongoCursor::tailable example
  • Example#1569 - MongoCursor::timeout example
  • Example#1570 - Adding options to MongoCommandCursor
  • Example#1571 - MongoCommandCursor::batchSize
  • Example#1572 - MongoCommandCursor example
  • Example#1573 - MongoCommandCursor::createFromDocument
  • Example#1574 - MongoCommandCursor::getReadPreference return value example
  • Example#1575 - MongoCommandCursor::info example
  • Example#1576 - MongoCommandCursor::rewind
  • Example#1577 - MongoCommandCursor::setReadPreference tag set array syntax example
  • Example#1578 - MongoCommandCursor::timeout example
  • Example#1579 - MongoId::__construct example
  • Example#1580 - Parameter example
  • Example#1581 - MongoId::__toString example
  • Example#1582 - MongoCode::__construct example
  • Example#1583 - Using MongoCode with $where
  • Example#1584 - MongoCode::__toString example
  • Example#1585 - Storing dates with MongoDate
  • Example#1586 - MongoDate::__construct example
  • Example#1587 - MongoDate::toDateTime example
  • Example#1588 - Regular expression pattern
  • Example#1589 - MongoRegex::__construct example
  • Example#1590 - MongoRegex::__toString example
  • Example#1591 -
  • Example#1592 - Linking documents
  • Example#1593 - Creating MongoDBRef links
  • Example#1594 - MongoDBRef::create example
  • Example#1595 - MongoCollection::createDBRef example
  • Example#1596 - MongoGridFS::findOne example
  • Example#1597 - MongoGridFS::storeBytes with additional metadata
  • Example#1598 - MongoGridFS::storeFile with additional metadata
  • Example#1599 - MongoGridFS::storeUpload HTML form example
  • Example#1600 - MongoGridFSFile::getBytes example
  • Example#1601 - MongoGridFSFile::getResource example
  • Example#1602 - MongoGridFSFile::write example
  • Example#1603 - MongoWriteBatch example
  • Example#1604 - MongoWriteBatch::add example
  • Example#1605 - MongoWriteBatch::add example
  • Example#1606 - MongoWriteBatch::add example
  • Example#1607 - MongoWriteBatch::add example
  • Example#1608 - MongoWriteBatch::add example
  • Example#1609 - MongoWriteBatch::add example
  • Example#1610 - MongoLog::setCallback example
  • Example#1611 - Changing pool size
  • Example#1612 - Mongo::setPoolSize example
  • Example#1613 - Changing pool size
  • Example#1614 - Mongo::setPoolSize example
  • Example#1615 - MongoResultException::getDocument example
  • Example#1616 - Catching MongoDuplicateKeyException
  • Example#1617 - Catching MongoProtocolException
  • Example#1618 - MongoDB\Driver\Manager::__construct basic example
  • Example#1619 - MongoDB\Driver\Manager::__construct basic examples
  • Example#1620 - MongoDB\Driver\Manager::executeBulkWrite example
  • Example#1621 - MongoDB\Driver\Manager::executeCommand with a command returning a single result document
  • Example#1622 - MongoDB\Driver\Manager::executeCommand with a command returning a cursor
  • Example#1623 - MongoDB\Driver\Manager::executeQuery example
  • Example#1624 - MongoDB\Driver\Manager::getServers example
  • Example#1625 - Composing MongoDB\Driver\Command to provide a helper to create collections
  • Example#1626 - MongoDB\Driver\Command::__construct example
  • Example#1627 - MongoDB\Driver\Query::__construct example
  • Example#1628 - Mixed write operations are grouped by type
  • Example#1629 - Ordered write operations causing an error
  • Example#1630 - MongoDB\Driver\BulkWrite::__construct example
  • Example#1631 - MongoDB\Driver\BulkWrite::count example
  • Example#1632 - MongoDB\Driver\BulkWrite::delete example
  • Example#1633 - MongoDB\Driver\BulkWrite::insert example
  • Example#1634 - MongoDB\Driver\BulkWrite::update example
  • Example#1635 - MongoDB\Driver\WriteConcern::bsonSerialize with majority write concern
  • Example#1636 - MongoDB\Driver\WriteConcern::bsonSerialize with wtimeout and journal
  • Example#1637 - MongoDB\Driver\WriteConcern::__construct example
  • Example#1638 - MongoDB\Driver\WriteConcern::getJournal example
  • Example#1639 - MongoDB\Driver\WriteConcern::getW example
  • Example#1640 - MongoDB\Driver\WriteConcern::getWtimeout example
  • Example#1641 - MongoDB\Driver\ReadPreference::bsonSerialize with primary read preference
  • Example#1642 - MongoDB\Driver\ReadPreference::bsonSerialize with secondary read preference and tag sets
  • Example#1643 - MongoDB\Driver\ReadPreference::__construct example
  • Example#1644 - MongoDB\Driver\ReadPreference::getMode example
  • Example#1645 - MongoDB\Driver\ReadPreference::getTagSets example
  • Example#1646 - MongoDB\Driver\ReadConcern::bsonSerialize with empty read concern
  • Example#1647 - MongoDB\Driver\ReadConcern::bsonSerialize with local read concern
  • Example#1648 - MongoDB\Driver\ReadConcern::__construct example
  • Example#1649 - MongoDB\Driver\ReadConcern::getLevel example
  • Example#1650 - MongoDB\Driver\Cursor::getId example
  • Example#1651 - MongoDB\Driver\Cursor::getServer example
  • Example#1652 - MongoDB\Driver\Cursor::isDead example
  • Example#1653 - MongoDB\Driver\Cursor::setTypeMap example
  • Example#1654 - MongoDB\Driver\Cursor::toArray example
  • Example#1655 - MongoDB\Driver\CursorId::__toString example
  • Example#1656 - MongoDB\Driver\Server::getHost example
  • Example#1657 - MongoDB\Driver\Server::getInfo example
  • Example#1658 - MongoDB\Driver\Server::getLatency example
  • Example#1659 - MongoDB\Driver\Server::getPort example
  • Example#1660 - MongoDB\Driver\WriteConcernError::getCode example
  • Example#1661 - MongoDB\Driver\WriteConcernError::getInfo example
  • Example#1662 - MongoDB\Driver\WriteConcernError::getMessage example
  • Example#1663 - MongoDB\Driver\WriteError::getCode example
  • Example#1664 - MongoDB\Driver\WriteError::getIndex example
  • Example#1665 - MongoDB\Driver\WriteError::getMessage example
  • Example#1666 - MongoDB\Driver\WriteResult::getDeletedCount example
  • Example#1667 - MongoDB\Driver\WriteResult::getInsertedCount example
  • Example#1668 - MongoDB\Driver\WriteResult::getMatchedCount example
  • Example#1669 - MongoDB\Driver\WriteResult::getModifiedCount example
  • Example#1670 - MongoDB\Driver\WriteResult::getServer example
  • Example#1671 - MongoDB\Driver\WriteResult::getUpsertedCount example
  • Example#1672 - MongoDB\Driver\WriteResult::getUpsertedIds example
  • Example#1673 - MongoDB\Driver\WriteResult::getWriteConcernError example
  • Example#1674 - MongoDB\Driver\WriteResult::getWriteErrors with a single error
  • Example#1675 - MongoDB\Driver\WriteResult::getWriteErrors with multiple errors
  • Example#1676 - MongoDB\Driver\WriteResult::isAcknowledged with acknowledged write concern
  • Example#1677 - MongoDB\Driver\WriteResult::isAcknowledged with unacknowledged write concern
  • Example#1678 - MongoDB\BSON\fromJSON example
  • Example#1679 - MongoDB\BSON\fromPHP example
  • Example#1680 - MongoDB\BSON\toJSON example
  • Example#1681 - MongoDB\BSON\toPHP example
  • Example#1682 - MongoDB\BSON\Binary::__construct example
  • Example#1683 - MongoDB\BSON\Binary::getData example
  • Example#1684 - MongoDB\BSON\Binary::getType example
  • Example#1685 - MongoDB\BSON\Decimal128::__construct example
  • Example#1686 - MongoDB\BSON\Decimal128::__toString example
  • Example#1687 - MongoDB\BSON\Javascript::__construct example
  • Example#1688 - MongoDB\BSON\MaxKey::__construct example
  • Example#1689 - MongoDB\BSON\MinKey::__construct example
  • Example#1690 - MongoDB\BSON\ObjectID::__construct example
  • Example#1691 - MongoDB\BSON\ObjectID::__toString example
  • Example#1692 - MongoDB\BSON\Regex::__construct example
  • Example#1693 - MongoDB\BSON\Regex::getFlags example
  • Example#1694 - MongoDB\BSON\Regex::getPattern example
  • Example#1695 - MongoDB\BSON\Regex::__toString example
  • Example#1696 - MongoDB\BSON\Timestamp::__construct example
  • Example#1697 - MongoDB\BSON\Timestamp::__toString example
  • Example#1698 - MongoDB\BSON\UTCDateTime::__construct example
  • Example#1699 - MongoDB\BSON\UTCDatetime::toDateTime example
  • Example#1700 - MongoDB\BSON\UTCDateTime::__toString example
  • Example#1701 - MongoDB\BSON\Serializable::bsonSerialize returning an associative array for root document
  • Example#1702 - MongoDB\BSON\Serializable::bsonSerialize returning a sequential array for root document
  • Example#1703 - MongoDB\BSON\Serializable::bsonSerialize returning an associative array for document field
  • Example#1704 - MongoDB\BSON\Serializable::bsonSerialize returning a sequential array for document field
  • Example#1705 - MongoDB\BSON\Unserializable::bsonUnserialize example
  • Example#1706 - MongoDB\Driver\Exception\WriteException::getWriteResult example
  • Example#1707 - mSQL usage example
  • Example#1708 - msql_fetch_array example
  • Example#1709 - msql_fetch_object example
  • Example#1710 - msql_fetch_row example
  • Example#1711 - msql_query example
  • Example#1712 - mssql_bind example
  • Example#1713 - mssql_close example
  • Example#1714 - mssql_connect example
  • Example#1715 - mssql_data_seek example
  • Example#1716 - mssql_execute example
  • Example#1717 - mssql_fetch_array example
  • Example#1718 - mssql_fetch_assoc example
  • Example#1719 - mssql_fetch_batch example
  • Example#1720 - mssql_fetch_field example
  • Example#1721 - mssql_fetch_object example
  • Example#1722 - mssql_fetch_row example
  • Example#1723 - mssql_field_length example
  • Example#1724 - mssql_field_name example
  • Example#1725 - Using mssql_field_seek on the example for mssql_fetch_field
  • Example#1726 - mssql_field_type example
  • Example#1727 - mssql_free_result example
  • Example#1728 - mssql_free_statement example
  • Example#1729 - mssql_get_last_message example
  • Example#1730 - mssql_guid_string example
  • Example#1731 - mssql_init example
  • Example#1732 - mssql_min_error_severity example
  • Example#1733 - mssql_min_message_severity example
  • Example#1734 - mssql_next_result example
  • Example#1735 - mssql_num_fields example
  • Example#1736 - mssql_num_rows example
  • Example#1737 - mssql_pconnect using the new_link parameter
  • Example#1738 - mssql_query example
  • Example#1739 - mssql_result example
  • Example#1740 - Faster alternative to above example
  • Example#1741 - mssql_rows_affected example
  • Example#1742 - mssql_select_db example
  • Example#1743 - Escaping the database name with square brackets
  • Example#1744 - Comparing the three MySQL APIs
  • Example#1745 - Configure commands for using mysqlnd or libmysqlclient
  • Example#1746 - Unbuffered query example: mysqli
  • Example#1747 - Unbuffered query example: pdo_mysql
  • Example#1748 - Unbuffered query example: mysql
  • Example#1749 - Problems with setting the character set with SQL
  • Example#1750 - Setting the character set example: mysqli
  • Example#1751 - Setting the character set example: pdo_mysql
  • Example#1752 - Setting the character set example: mysql
  • Example#1753 - Easy migration from the old mysql extension
  • Example#1754 - Object-oriented and procedural interface
  • Example#1755 - Bad coding style
  • Example#1756 - Special meaning of localhost
  • Example#1757 - Setting defaults
  • Example#1758 - Connecting to MySQL
  • Example#1759 - Navigation through buffered results
  • Example#1760 - Navigation through unbuffered results
  • Example#1761 - Text protocol returns strings by default
  • Example#1762 - Native data types with mysqlnd and connection option
  • Example#1763 - First stage: prepare
  • Example#1764 - Second stage: bind and execute
  • Example#1765 - INSERT prepared once, executed multiple times
  • Example#1766 - Less round trips using multi-INSERT SQL
  • Example#1767 - Native datatypes
  • Example#1768 - Output variable binding
  • Example#1769 - Using mysqli_result to fetch results
  • Example#1770 - Buffered result set for flexible read out
  • Example#1771 - Calling a stored procedure
  • Example#1772 - Using session variables
  • Example#1773 - Fetching results from stored procedures
  • Example#1774 - Stored Procedures and Prepared Statements
  • Example#1775 - Stored Procedures and Prepared Statements using bind API
  • Example#1776 - Multiple Statements
  • Example#1777 - SQL Injection
  • Example#1778 - Setting auto commit mode with SQL and through the API
  • Example#1779 - Commit and rollback
  • Example#1780 - Accessing result set meta data
  • Example#1781 - Prepared statements metadata
  • Example#1782 - MySQLi extension overview example
  • Example#1783 - $mysqli->affected_rows example
  • Example#1784 - mysqli::autocommit example
  • Example#1785 - $mysqli->begin_transaction example
  • Example#1786 - mysqli::change_user example
  • Example#1787 - mysqli::character_set_name example
  • Example#1788 - mysqli_get_client_info
  • Example#1789 - mysqli_get_client_version
  • Example#1790 - mysqli::commit example
  • Example#1791 - $mysqli->connect_errno example
  • Example#1792 - $mysqli->connect_error example
  • Example#1793 - mysqli::__construct example
  • Example#1794 - Generating a Trace File
  • Example#1795 - $mysqli->errno example
  • Example#1796 - $mysqli->error_list example
  • Example#1797 - $mysqli->error example
  • Example#1798 - $mysqli->field_count example
  • Example#1799 - mysqli::get_charset example
  • Example#1800 - mysqli_get_client_info
  • Example#1801 - A mysqli_get_client_stats example
  • Example#1802 - mysqli_get_client_version
  • Example#1803 - A mysqli_get_connection_stats example
  • Example#1804 - $mysqli->host_info example
  • Example#1805 - $mysqli->protocol_version example
  • Example#1806 - $mysqli->server_info example
  • Example#1807 - $mysqli->server_version example
  • Example#1808 - $mysqli->info example
  • Example#1809 - $mysqli->insert_id example
  • Example#1810 - mysqli::kill example
  • Example#1811 - mysqli::multi_query example
  • Example#1812 - mysqli::ping example
  • Example#1813 - A mysqli_poll example
  • Example#1814 - mysqli::prepare example
  • Example#1815 - mysqli::query example
  • Example#1816 - mysqli::real_connect example
  • Example#1817 - mysqli::real_escape_string example
  • Example#1818 - mysqli::rollback example
  • Example#1819 - mysqli::select_db example
  • Example#1820 - mysqli::set_charset example
  • Example#1821 - mysqli::set_local_infile_handler example
  • Example#1822 - $mysqli->sqlstate example
  • Example#1823 - mysqli::stat example
  • Example#1824 - $mysqli->thread_id example
  • Example#1825 - mysqli::use_result example
  • Example#1826 - $mysqli->warning_count example
  • Example#1827 - 객체 기반 형식
  • Example#1828 - 절차식 형식
  • Example#1829 - 객체 기반 형식
  • Example#1830 - 절차식 형식
  • Example#1831 - 객체 기반 형식
  • Example#1832 - 절차식 형식
  • Example#1833 - 객체 기반 형식
  • Example#1834 - 절차식 형식
  • Example#1835 - 객체 기반 형식
  • Example#1836 - 절차식 형식
  • Example#1837 - 객체 기반 형식
  • Example#1838 - 절차식 형식
  • Example#1839 - 객체 기반 형식
  • Example#1840 - 절차식 형식
  • Example#1841 - 객체 기반 형식
  • Example#1842 - 절차식 형식
  • Example#1843 - 객체 기반 형식
  • Example#1844 - 절차식 형식
  • Example#1845 - 객체 기반 형식
  • Example#1846 - 절차식 형식
  • Example#1847 - 객체 기반 형식
  • Example#1848 - 절차식 형식
  • Example#1849 - 객체 기반 형식
  • Example#1850 - 절차식 형식
  • Example#1851 - 객체 기반 형식
  • Example#1852 - 절차식 형식
  • Example#1853 - 객체 기반 형식
  • Example#1854 - 절차식 형식
  • Example#1855 - 객체 기반 형식
  • Example#1856 - 객체 기반 형식
  • Example#1857 - 절차식 형식
  • Example#1858 - 객체 기반 형식
  • Example#1859 - 절차식 형식
  • Example#1860 - 객체 기반 형식
  • Example#1861 - 절차식 형식
  • Example#1862 - 객체 기반 형식
  • Example#1863 - 절차식 형식
  • Example#1864 - 객체 기반 형식
  • Example#1865 - 절차식 형식
  • Example#1866 - 객체 기반 형식
  • Example#1867 - 절차식 형식
  • Example#1868 - A mysqli_result example comparing iterator usage
  • Example#1869 - 객체 기반 형식
  • Example#1870 - 절차식 형식
  • Example#1871 - 객체 기반 형식
  • Example#1872 - 절차식 형식
  • Example#1873 - 객체 기반 형식
  • Example#1874 - 절차식 형식
  • Example#1875 - 객체 기반 형식
  • Example#1876 - 절차식 형식
  • Example#1877 - 객체 기반 형식
  • Example#1878 - 절차식 형식
  • Example#1879 - 객체 기반 형식
  • Example#1880 - 절차식 형식
  • Example#1881 - 객체 기반 형식
  • Example#1882 - 절차식 형식
  • Example#1883 - 객체 기반 형식
  • Example#1884 - 절차식 형식
  • Example#1885 - 객체 기반 형식
  • Example#1886 - 절차식 형식
  • Example#1887 - 객체 기반 형식
  • Example#1888 - 절차식 형식
  • Example#1889 - mysqli_connect example
  • Example#1890 - MySQL extension overview example
  • Example#1891 - mysql_affected_rows 예제
  • Example#1892 - 트랜젝션을 이용한 mysql_affected_rows 예제
  • Example#1893 - mysql_client_encoding 예제
  • Example#1894 - mysql_close 예제
  • Example#1895 - mysql_connect 예제
  • Example#1896 - hostname:port 문법을 사용한 mysql_connect 예제
  • Example#1897 - ":/path/to/socket" 문법을 사용한 mysql_connect 예제
  • Example#1898 - mysql_create_db 예제
  • Example#1899 - mysql_data_seek 예제
  • Example#1900 - mysql_db_name 예제
  • Example#1901 - mysql_db_query 예제
  • Example#1902 - mysql_drop_db 예제
  • Example#1903 - mysql_errno 예제
  • Example#1904 - mysql_error 예제
  • Example#1905 - mysql_escape_string 예제
  • Example#1906 - 별명(alias)으로 지정한 중복된 필드 이름으로 질의
  • Example#1907 - MYSQL_NUM을 이용한 mysql_fetch_array 예제
  • Example#1908 - MYSQL_ASSOC를 이용한 mysql_fetch_array 예제
  • Example#1909 - MYSQL_BOTH를 이용한 mysql_fetch_array 예제
  • Example#1910 - mysql_fetch_assoc 예제
  • Example#1911 - mysql_fetch_field 예제
  • Example#1912 - mysql_fetch_lengths 예제
  • Example#1913 - mysql_fetch_object 예제
  • Example#1914 - mysql_fetch_object 예제
  • Example#1915 - mysql_fetch_row를 이용한 하나의 행 인출 예제
  • Example#1916 - mysql_field_flags 예제
  • Example#1917 - mysql_field_len 예제
  • Example#1918 - mysql_field_name 예제
  • Example#1919 - mysql_field_table 예제
  • Example#1920 - mysql_field_type 예제
  • Example#1921 - mysql_free_result 예제
  • Example#1922 - mysql_get_client_info 예제
  • Example#1923 - mysql_get_host_info 예제
  • Example#1924 - mysql_get_proto_info 예제
  • Example#1925 - mysql_get_server_info 예제
  • Example#1926 - 관련된 MySQL 진술문
  • Example#1927 - mysql_insert_id 예제
  • Example#1928 - mysql_list_dbs 예제
  • Example#1929 - mysql_list_fields의 기능을 대체할 예제
  • Example#1930 - mysql_list_processes 예제
  • Example#1931 - mysql_list_tables 예제
  • Example#1932 - mysql_num_fields 예제
  • Example#1933 - mysql_num_rows 예제
  • Example#1934 - mysql_ping 예제
  • Example#1935 - 잘못된 질의
  • Example#1936 - 유효한 질의
  • Example#1937 - mysql_real_escape_string 예제
  • Example#1938 - SQL 인젝션 공격(Injection Attack)의 예
  • Example#1939 - "Best Practice" 질의
  • Example#1940 - mysql_result 예제
  • Example#1941 - mysql_select_db 예제
  • Example#1942 - mysql_stat 예제
  • Example#1943 - mysql_stat 다른 예제
  • Example#1944 - mysql_tablename 예제
  • Example#1945 - mysql_thread_id 예제
  • Example#1946 - Enabling the plugin (php.ini)
  • Example#1947 - Minimal plugin-specific configuration file (mysqlnd_ms_plugin.ini)
  • Example#1948 - Recommended minimal plugin-specific config (mysqlnd_ms_plugin.ini)
  • Example#1949 - Using one server as a master and as a slave (testing only!)
  • Example#1950 - Plugin specific configuration file (mysqlnd_ms_plugin.ini)
  • Example#1951 - Opening a load balanced connection
  • Example#1952 - Executing statements
  • Example#1953 - Plugin config with one slave and one master
  • Example#1954 - Pitfall: connection state and SQL user variables
  • Example#1955 - Plugin config with one slave and one master
  • Example#1956 - SQL hints to prevent connection switches
  • Example#1957 - Fighting replication lag
  • Example#1958 - Table creation on a slave
  • Example#1959 - Plugin config with one slave and one master
  • Example#1960 - Using SQL hints for transactions
  • Example#1961 - Transaction aware load balancing: trx_stickiness setting
  • Example#1962 - Transaction aware
  • Example#1963 - General pattern for XA transactions
  • Example#1964 - Local and global transactions are mutually exclusive
  • Example#1965 - Transaction coordinator state store
  • Example#1966 - Session consistency: read your writes
  • Example#1967 - Requesting session consistency
  • Example#1968 - Maximum age/slave lag
  • Example#1969 - Limiting slave lag
  • Example#1970 - Fail over not set
  • Example#1971 - No slave within time limit
  • Example#1972 - Create counter table on master
  • Example#1973 - Plugin config: SQL for client-side GTID injection
  • Example#1974 - Transparent global transaction ID injection
  • Example#1975 - Plugin config: SQL for fetching GTID
  • Example#1976 - Obtaining GTID after injection
  • Example#1977 - Plugin config: Checking for a certain GTID
  • Example#1978 - Session consistency service level and GTID combined
  • Example#1979 - Plugin config: using MySQL 5.6.5-m8 built-in GTID feature
  • Example#1980 - Recap: quality of service to request read your writes
  • Example#1981 - Plugin config: no special entries for caching
  • Example#1982 - Caching a slave request
  • Example#1983 - Read your writes and caching combined
  • Example#1984 - Manual failover, automatic optional
  • Example#1985 - Manual failover
  • Example#1986 - Cluster node groups
  • Example#1987 - Manual partitioning using SQL hints
  • Example#1988 - Plugin config: Fabric hosts instead of MySQL servers
  • Example#1989 - Manual partitioning using SQL hints
  • Example#1990 - Provoking a connection error
  • Example#1991 - Connection error on query execution
  • Example#1992 - Provoking a connection error
  • Example#1993 - Most basic failover
  • Example#1994 - Provoking a transient error
  • Example#1995 - Transient error retry loop
  • Example#1996 - Enabling the plugin (php.ini)
  • Example#1997 - Basic plugin configuration (mysqlnd_ms_plugin.ini) for MySQL Replication
  • Example#1998 - Multiple primaries - multi master (php.ini)
  • Example#1999 - Primary copy with multiple primaries and paritioning
  • Example#2000 - Multiple primaries - multi master (php.ini)
  • Example#2001 - Synchronous update anywhere cluster
  • Example#2002 - General pattern for XA transactions
  • Example#2003 - Converting a PHP array (hash) into JSON format
  • Example#2004 - Using section names example
  • Example#2005 - List of anonymous slaves
  • Example#2006 - Master list using symbolic names
  • Example#2007 - Keywords to configure a server
  • Example#2008 - New roundrobin filter, old functionality
  • Example#2009 - The user filter replaces mysqlnd_ms_set_user_pick_server
  • Example#2010 - Common error message in case of configuration file issues (upto version 1.5.0)
  • Example#2011 - Improved configuration file validation since 1.5.0
  • Example#2012 - Possibly more precise error due to mysqlnd_ms.force_config_usage=1
  • Example#2013 - Minimum pluging configuration for use with MySQL Fabric
  • Example#2014 - Optional timeout for communication with Fabric
  • Example#2015 - Warnings about the violation of transaction boundaries
  • Example#2016 - Invalid filter sequence
  • Example#2017 - Random load balancing with random filter
  • Example#2018 - Random once load balancing with random filter
  • Example#2019 - Referencing error
  • Example#2020 - Assigning a weight for load balancing
  • Example#2021 - roundrobin filter
  • Example#2022 - Setting a callback
  • Example#2023 - Using a callback
  • Example#2024 - Returning random masters and slaves
  • Example#2025 - Manual partitioning
  • Example#2026 - Global limit on slave lag
  • Example#2027 - Optional master failover when failing to connect to slave (PECL/mysqlnd_ms < 1.4.0)
  • Example#2028 - New syntax since 1.4.0
  • Example#2029 - Disabling lazy connection
  • Example#2030 - String escaping on a lazy connection handle
  • Example#2031 - Master on write for consistent reads
  • Example#2032 - Using master to execute transactions
  • Example#2033 - No automatic failover, error handling pitfall
  • Example#2034 - Retry loop for transient errors
  • Example#2035 - SQL definition for the MySQL state store transaction table
  • Example#2036 - SQL definition for the MySQL state store transaction table
  • Example#2037 - SQL definition for the MySQL state store garbage collection table
  • Example#2038 - Using section names example
  • Example#2039 - List-like syntax
  • Example#2040 - Verify plugin activity in a non-threaded deployment model
  • Example#2041 - Recording statistics during shutdown
  • Example#2042 - Example demonstrating the usage of mysqlnd_ms constants
  • Example#2043 - mysqlnd_ms_dump_servers example
  • Example#2044 - mysqlnd_ms_get_last_gtid example
  • Example#2045 - mysqlnd_ms_get_last_used_connection example
  • Example#2046 - mysqlnd_ms_get_stats example
  • Example#2047 - mysqlnd_ms_match_wild example
  • Example#2048 - mysqlnd_ms_query_is_select example
  • Example#2049 - mysqlnd_ms_set_qos example
  • Example#2050 - mysqlnd_ms_set_user_pick_server example
  • Example#2051 - Enabling the plugin (php.ini)
  • Example#2052 - Using the MYSQLND_QC_ENABLE_SWITCH SQL hint
  • Example#2053 - Using the MYSQLND_QC_DISABLE_SWITCH SQL hint
  • Example#2054 - Example showing which type of statements are not cached
  • Example#2055 - Enabling caching for all statements using the mysqlnd_qc.cache_no_table ini setting
  • Example#2056 - Setting the TTL with the mysqlnd_qc.ttl ini setting
  • Example#2057 - Setting TTL with SQL hints
  • Example#2058 - Setting a callback with mysqlnd_qc_set_is_select
  • Example#2059 - Enabling the slam defense mechanism
  • Example#2060 - Collecting a query trace
  • Example#2061 - Setting the backtrace depth with the mysqlnd_qc.query_trace_bt_depth ini setting
  • Example#2062 - Collecting statistics data with the mysqlnd_qc.time_statistics ini setting
  • Example#2063 - Example mysqlnd_qc_get_cache_info usage
  • Example#2064 - Example mysqlnd_qc_get_normalized_query_trace_log usage
  • Example#2065 - Using a user-defined storage handler
  • Example#2066 - Using SQL hint constants
  • Example#2067 - Example mysqlnd_qc_set_cache_condition usage
  • Example#2068 - mysqlnd_qc_get_available_handlers example
  • Example#2069 - mysqlnd_qc_get_cache_info example
  • Example#2070 - mysqlnd_qc_get_core_stats example
  • Example#2071 - mysqlnd_qc_get_normalized_query_trace_log example
  • Example#2072 - mysqlnd_qc_get_query_trace_log example
  • Example#2073 - mysqlnd_qc_set_cache_condition example
  • Example#2074 - mysqlnd_qc_set_is_select example
  • Example#2075 - mysqlnd_qc_set_storage_handler example
  • Example#2076 - Enabling the plugin (php.ini)
  • Example#2077 - Pseudo-code: what a built-in class does
  • Example#2078 - Installing a proxy
  • Example#2079 - Proxy registration, mysqlnd_uh.enable=1
  • Example#2080 - Proxy installation disabled
  • Example#2081 - Connection proxy
  • Example#2082 - Prepared statement proxy
  • Example#2083 - Basic Monitoring
  • Example#2084 - MysqlndUhConnection::changeUser example
  • Example#2085 - MysqlndUhConnection::charsetName example
  • Example#2086 - MysqlndUhConnection::close example
  • Example#2087 - MysqlndUhConnection::connect example
  • Example#2088 - MysqlndUhConnection::endPSession example
  • Example#2089 - MysqlndUhConnection::escapeString example
  • Example#2090 - MysqlndUhConnection::getAffectedRows example
  • Example#2091 - MysqlndUhConnection::getErrorNumber example
  • Example#2092 - MysqlndUhConnection::getErrorString example
  • Example#2093 - MysqlndUhConnection::getFieldCount example
  • Example#2094 - MysqlndUhConnection::getHostInformation example
  • Example#2095 - MysqlndUhConnection::getLastInsertId example
  • Example#2096 - MysqlndUhConnection::getLastMessage example
  • Example#2097 - MysqlndUhConnection::getProtocolInformation example
  • Example#2098 - MysqlndUhConnection::getServerInformation example
  • Example#2099 - MysqlndUhConnection::getServerStatistics example
  • Example#2100 - MysqlndUhConnection::getServerVersion example
  • Example#2101 - MysqlndUhConnection::getSqlstate example
  • Example#2102 - MysqlndUhConnection::getStatistics example
  • Example#2103 - MysqlndUhConnection::getThreadId example
  • Example#2104 - MysqlndUhConnection::getWarningCount example
  • Example#2105 - MysqlndUhConnection::init example
  • Example#2106 - MysqlndUhConnection::kill example
  • Example#2107 - MysqlndUhConnection::listFields example
  • Example#2108 - MysqlndUhConnection::listMethod example
  • Example#2109 - MysqlndUhConnection::moreResults example
  • Example#2110 - MysqlndUhConnection::nextResult example
  • Example#2111 - MysqlndUhConnection::ping example
  • Example#2112 - MysqlndUhConnection::query example
  • Example#2113 - MysqlndUhConnection::queryReadResultsetHeader example
  • Example#2114 - MysqlndUhConnection::reapQuery example
  • Example#2115 - MysqlndUhConnection::refreshServer example
  • Example#2116 - MysqlndUhConnection::restartPSession example
  • Example#2117 - MysqlndUhConnection::selectDb example
  • Example#2118 - MysqlndUhConnection::sendClose example
  • Example#2119 - MysqlndUhConnection::sendQuery example
  • Example#2120 - MysqlndUhConnection::serverDumpDebugInformation example
  • Example#2121 - MysqlndUhConnection::setAutocommit example
  • Example#2122 - MysqlndUhConnection::setCharset example
  • Example#2123 - MysqlndUhConnection::setClientOption example
  • Example#2124 - MysqlndUhConnection::setServerOption example
  • Example#2125 - MysqlndUhConnection::simpleCommand example
  • Example#2126 - MysqlndUhConnection::simpleCommandHandleResponse example
  • Example#2127 - MysqlndUhConnection::sslSet example
  • Example#2128 - MysqlndUhConnection::stmtInit example
  • Example#2129 - MysqlndUhConnection::storeResult example
  • Example#2130 - MysqlndUhConnection::txCommit example
  • Example#2131 - MysqlndUhConnection::txRollback example
  • Example#2132 - MysqlndUhConnection::useResult example
  • Example#2133 - MysqlndUhPreparedStatement::execute example
  • Example#2134 - MysqlndUhPreparedStatement::prepare example
  • Example#2135 - mysqlnd_uh_convert_to_mysqlnd example
  • Example#2136 - mysqlnd_uh_set_connection_proxy example
  • Example#2137 - Enabling the plugin (php.ini)
  • Example#2138 - SQL table used for the Quickstart
  • Example#2139 - Basic example.
  • Example#2140 - mysqlnd_memcache_get_config example
  • Example#2141 - mysqlnd_memcache_set example with var_dump as a simple debugging callback.
  • Example#2142 - Basic query
  • Example#2143 - Inserting with bind variables
  • Example#2144 - Binding in the WHERE clause of a query
  • Example#2145 - Inserting and fetching a CLOB
  • Example#2146 - Using a PL/SQL stored function
  • Example#2147 - Using a PL/SQL stored procedure
  • Example#2148 - Calling a PL/SQL function that returns a REF CURSOR
  • Example#2149 - user_oci8_probes.d for tracing all user-level PHP OCI8 Static Probes with DTrace
  • Example#2150 - oci_bind_array_by_name example
  • Example#2151 - Inserting data with oci_bind_by_name
  • Example#2152 - Binding once for multiple executions
  • Example#2153 - Binding with a foreach loop
  • Example#2154 - Binding in a WHERE clause
  • Example#2155 - Binding with a LIKE clause
  • Example#2156 - Binding with REGEXP_LIKE
  • Example#2157 - Binding Multiple Values in an IN Clause
  • Example#2158 - Binding a ROWID returned by a query
  • Example#2159 - Binding a ROWID on INSERT
  • Example#2160 - Binding for a PL/SQL stored function
  • Example#2161 - Binding parameters for a PL/SQL stored procedure
  • Example#2162 - Binding a CLOB column
  • Example#2163 - Binding a PL/SQL BOOLEAN
  • Example#2164 - oci_client_version example
  • Example#2165 - Closing a connection
  • Example#2166 - Database connections are not closed until all references are closed
  • Example#2167 - Closing a connection opened more than once
  • Example#2168 - Connections are closed when variables go out of scope
  • Example#2169 - oci_commit example
  • Example#2170 - Basic oci_connect using Easy Connect syntax
  • Example#2171 - Basic oci_connect using a Network Connect name
  • Example#2172 - oci_connect with an explicit character set
  • Example#2173 - Using multiple calls to oci_connect
  • Example#2174 - oci_define_by_name example
  • Example#2175 - oci_define_by_name with case sensitive column names
  • Example#2176 - oci_define_by_name with LOB columns
  • Example#2177 - oci_define_by_name with an explicit type
  • Example#2178 - Displaying the Oracle error message after a connection error
  • Example#2179 - Displaying the Oracle error message after a parsing error
  • Example#2180 - Displaying the Oracle error message, the problematic statement, and the position of the problem of an execution error
  • Example#2181 - oci_execute for queries
  • Example#2182 - oci_execute without specifying a mode example
  • Example#2183 - oci_execute with OCI_NO_AUTO_COMMIT example
  • Example#2184 - oci_execute with different commit modes example
  • Example#2185 - oci_execute with OCI_DESCRIBE_ONLY example
  • Example#2186 - oci_fetch_all example
  • Example#2187 - oci_fetch_all example with OCI_FETCHSTATEMENT_BY_ROW
  • Example#2188 - oci_fetch_all with OCI_NUM
  • Example#2189 - oci_fetch_array with OCI_BOTH
  • Example#2190 - oci_fetch_array with OCI_NUM
  • Example#2191 - oci_fetch_array with OCI_ASSOC
  • Example#2192 - oci_fetch_array with OCI_RETURN_NULLS
  • Example#2193 - oci_fetch_array with OCI_RETURN_LOBS
  • Example#2194 - oci_fetch_array with case sensitive column names
  • Example#2195 - oci_fetch_array with columns having duplicate names
  • Example#2196 - oci_fetch_array with DATE columns
  • Example#2197 - oci_fetch_array with REF CURSOR
  • Example#2198 - Pagination with oci_fetch_array using a LIMIT-like query
  • Example#2199 - oci_fetch_array with Oracle Database 12c Implicit Result Sets
  • Example#2200 - oci_fetch_assoc Example
  • Example#2201 - oci_fetch_object example
  • Example#2202 - oci_fetch_object with case sensitive column names
  • Example#2203 - oci_fetch_object with LOBs
  • Example#2204 - oci_fetch_row Example
  • Example#2205 - oci_fetch with defined variables
  • Example#2206 - oci_fetch with oci_result
  • Example#2207 - oci_field_name example
  • Example#2208 - oci_field_name example
  • Example#2209 - oci_field_precision Example
  • Example#2210 - oci_field_scale Example
  • Example#2211 - oci_field_size example
  • Example#2212 - oci_field_type_raw Example
  • Example#2213 - oci_field_type example
  • Example#2214 - Fetching Implicit Result Sets in a loop
  • Example#2215 - Getting child statement handles individually
  • Example#2216 - Explicitly setting the Prefetch Count
  • Example#2217 - Implicit Result Set example without using oci_get_implicit_resultset
  • Example#2218 - oci_new_connect example
  • Example#2219 - Binding a REF CURSOR in an Oracle stored procedure call
  • Example#2220 - oci_new_descriptor example
  • Example#2221 - oci_new_descriptor example
  • Example#2222 - oci_num_fields example
  • Example#2223 - oci_num_rows example
  • Example#2224 - oci_parse example for SQL statements
  • Example#2225 - oci_parse example for PL/SQL statements
  • Example#2226 - oci_password_change example changing the password of an already connected user
  • Example#2227 - oci_password_change example of connecting and changing the password in one step
  • Example#2228 - Basic oci_pconnect Example using Easy Connect syntax
  • Example#2229 - oci_fetch with oci_result
  • Example#2230 - oci_rollback example
  • Example#2231 - Rolling back to a SAVEPOINT example
  • Example#2232 - oci_server_version example
  • Example#2233 - Setting the action
  • Example#2234 - Setting the client identifier to the application user
  • Example#2235 - Setting the client information
  • Example#2236 - Two scripts can use different versions of myfunc() at the same time
  • Example#2237 - Setting the module name
  • Example#2238 - Changing the default prefetch value for a query
  • Example#2239 - Changing the default prefetch for a REF CURSOR fetch
  • Example#2240 - Setting the prefetch value when passing a REF CURSOR back to Oracle
  • Example#2241 - oci_statement_type example
  • Example#2242 - Creating a Paradox database with two fields
  • Example#2243 - Turn a paradox date into a human readable form
  • Example#2244 - Set the date/time fields in a paradox database to the current date/time
  • Example#2245 - Opening a Paradox database
  • Example#2246 - Opening a Paradox database
  • Example#2247 - Turn a paradox timestamp into a human readable form
  • Example#2248 - PostgreSQL extension overview example
  • Example#2249 - pg_affected_rows 예제
  • Example#2250 - pg_cancel_query 예제
  • Example#2251 - pg_client_encoding 예제
  • Example#2252 - pg_close example
  • Example#2253 - Using pg_connect
  • Example#2254 - pg_connection_busy example
  • Example#2255 - pg_connection_reset example
  • Example#2256 - pg_connection_status example
  • Example#2257 - pg_convert example
  • Example#2258 - pg_copy_from example
  • Example#2259 - pg_copy_to example
  • Example#2260 - pg_dbname example
  • Example#2261 - pg_delete example
  • Example#2262 - pg_end_copy example
  • Example#2263 - pg_escape_bytea example
  • Example#2264 - pg_escape_identifier example
  • Example#2265 - pg_escape_literal example
  • Example#2266 - pg_escape_string example
  • Example#2267 - Using pg_execute
  • Example#2268 - pg_fetch_all_columns example
  • Example#2269 - PostgreSQL fetch all
  • Example#2270 - pg_fetch_array example
  • Example#2271 - pg_fetch_assoc example
  • Example#2272 - pg_fetch_object example
  • Example#2273 - pg_fetch_result example
  • Example#2274 - pg_fetch_row example
  • Example#2275 - pg_field_is_null example
  • Example#2276 - Getting information about fields
  • Example#2277 - Getting information about fields
  • Example#2278 - Getting information about fields
  • Example#2279 - Getting information about fields
  • Example#2280 - Getting table information about a field
  • Example#2281 - Getting information about fields
  • Example#2282 - Getting information about fields
  • Example#2283 - pg_free_result example
  • Example#2284 - PostgreSQL NOTIFY message
  • Example#2285 - PostgreSQL backend PID
  • Example#2286 - pg_get_result example
  • Example#2287 - pg_host example
  • Example#2288 - pg_insert example
  • Example#2289 - pg_last_error example
  • Example#2290 - pg_last_notice example
  • Example#2291 - pg_last_oid example
  • Example#2292 - pg_lo_close example
  • Example#2293 - pg_lo_create example
  • Example#2294 - pg_lo_export example
  • Example#2295 - pg_lo_import example
  • Example#2296 - pg_lo_open example
  • Example#2297 - pg_lo_read_all example
  • Example#2298 - pg_lo_read example
  • Example#2299 - pg_lo_seek example
  • Example#2300 - pg_lo_tell example
  • Example#2301 - pg_lo_truncate example
  • Example#2302 - pg_lo_unlink example
  • Example#2303 - pg_lo_write example
  • Example#2304 - Getting table metadata
  • Example#2305 - pg_num_fields example
  • Example#2306 - pg_num_rows example
  • Example#2307 - pg_options example
  • Example#2308 - pg_parameter_status example
  • Example#2309 - Using pg_pconnect
  • Example#2310 - pg_ping example
  • Example#2311 - pg_port example
  • Example#2312 - Using pg_prepare
  • Example#2313 - pg_put_line example
  • Example#2314 - Using pg_query_params
  • Example#2315 - pg_query example
  • Example#2316 - Using pg_query with multiple statements
  • Example#2317 - pg_result_error_field example
  • Example#2318 - pg_result_error example
  • Example#2319 - pg_result_seek example
  • Example#2320 - pg_result_status example
  • Example#2321 - pg_select example
  • Example#2322 - Using pg_send_execute
  • Example#2323 - Using pg_send_prepare
  • Example#2324 - Using pg_send_query_params
  • Example#2325 - pg_send_query example
  • Example#2326 - pg_set_client_encoding example
  • Example#2327 - pg_set_error_verbosity example
  • Example#2328 - pg_trace example
  • Example#2329 - pg_transaction_status example
  • Example#2330 - pg_tty example
  • Example#2331 - pg_unescape_bytea example
  • Example#2332 - pg_untrace example
  • Example#2333 - pg_update example
  • Example#2334 - pg_version example
  • Example#2335 - 절차식 형식
  • Example#2336 - Object-oriented style
  • Example#2337 - 절차식 형식
  • Example#2338 - 객체 기반 형식
  • Example#2339 - 절차식 형식
  • Example#2340 - 객체 기반 형식
  • Example#2341 - sqlite_close example
  • Example#2342 - max_length aggregation function example
  • Example#2343 - sqlite_create_function example
  • Example#2344 - Example of using the PHP function
  • Example#2345 - Procedural example
  • Example#2346 - Object-oriented example
  • Example#2347 - sqlite_factory example
  • Example#2348 - Procedural example
  • Example#2349 - Object-oriented example
  • Example#2350 - Procedural example
  • Example#2351 - Object-oriented example
  • Example#2352 - Procedural example
  • Example#2353 - Object-oriented example
  • Example#2354 - A sqlite_fetch_single example
  • Example#2355 - Procedural example
  • Example#2356 - Object-oriented example
  • Example#2357 - sqlite_open example
  • Example#2358 - binary-safe max_length aggregation function example
  • Example#2359 - SQLite3::changes example
  • Example#2360 - SQLite3::close example
  • Example#2361 - SQLite3::__construct example
  • Example#2362 - SQLite3::createCollation example
  • Example#2363 - SQLite3::createFunction example
  • Example#2364 - SQLite3::enableExceptions example
  • Example#2365 - SQLite3::exec example
  • Example#2366 - SQLite3::loadExtension example
  • Example#2367 - SQLite3::open example
  • Example#2368 - SQLite3::openBlob example
  • Example#2369 - SQLite3::prepare example
  • Example#2370 - SQLite3::query example
  • Example#2371 - SQLite3::querySingle example
  • Example#2372 - SQLite3::version example
  • Example#2373 - SQLite3Stmt::bindValue example
  • Example#2374 - sqlsrv_begin_transaction example
  • Example#2375 - sqlsrv_cancel example
  • Example#2376 - sqlsrv_client_info example
  • Example#2377 - sqlsrv_close example
  • Example#2378 - sqlsrv_commit example
  • Example#2379 - Connect using Windows Authentication.
  • Example#2380 - Connect by specifying a user name and password.
  • Example#2381 - Connect on a specifed port.
  • Example#2382 - functionname example
  • Example#2383 - sqlsrv_execute example
  • Example#2384 - Retrieving an associative array.
  • Example#2385 - Retrieving a numeric array.
  • Example#2386 - sqlsrv_fetch_object example
  • Example#2387 - sqlsrv_fetch example
  • Example#2388 - sqlsrv_field_metadata example
  • Example#2389 - sqlsrv_free_stmt example
  • Example#2390 - sqlsrv_get_field example
  • Example#2391 - sqlsrv_has_rows example
  • Example#2392 - sqlsrv_next_result example
  • Example#2393 - sqlsrv_num_fields example
  • Example#2394 - sqlsrv_num_rows example
  • Example#2395 - sqlsrv_prepare example
  • Example#2396 - sqlsrv_query example
  • Example#2397 - sqlsrv_rollback example
  • Example#2398 - sqlsrv_rows_affected example
  • Example#2399 - sqlsrv_send_stream_data example
  • Example#2400 - sqlsrv_server_info example
  • Example#2401 - Delete-Query
  • Example#2402 - sybase_connect example
  • Example#2403 - Identical fieldnames
  • Example#2404 - sybase_fetch_object return as Foo
  • Example#2405 - sybase_set_message_handler callback function
  • Example#2406 - sybase_set_message_handler callback to a class
  • Example#2407 - sybase_set_message_handler unhandled messages
  • Example#2408 - sybase_unbuffered_query example
  • Example#2409 - Putting and getting a key-value pair
  • Example#2410 - TokyoTyrant::add example
  • Example#2411 - TokyoTyrant::connect example
  • Example#2412 - TokyoTyrant::connectUri example
  • Example#2413 - TokyoTyrant::copy example
  • Example#2414 - TokyoTyrant::ext example
  • Example#2415 - TokyoTyrant::fwmKeys example
  • Example#2416 - TokyoTyrant::get example
  • Example#2417 - TokyoTyrant::getIterator example
  • Example#2418 - TokyoTyrant::num example
  • Example#2419 - TokyoTyrant::out example
  • Example#2420 - TokyoTyrant::put example
  • Example#2421 - TokyoTyrant::putCat example
  • Example#2422 - tokyotyrant::putKeep example
  • Example#2423 - TokyoTyrant::putNr example
  • Example#2424 - TokyoTyrant::putShl example
  • Example#2425 - TokyoTyrant::setMaster example
  • Example#2426 - TokyoTyrant::size example
  • Example#2427 - TokyoTyrant::stat example
  • Example#2428 - TokyoTyrant::vanish example
  • Example#2429 - TokyoTyrantTable::genUid example
  • Example#2430 - TokyoTyrantTable::get example
  • Example#2431 - TokyoTyrantTable::getIterator example
  • Example#2432 - TokyoTyrantTable::getQuery example
  • Example#2433 - TokyoTyrantTable::out example
  • Example#2434 - TokyoTyrantTable::put example
  • Example#2435 - TokyoTyrantTable::putCat example
  • Example#2436 - TokyoTyrantTable::putKeep example
  • Example#2437 - TokyoTyrantQuery::addCond example
  • Example#2438 - TokyoTyrantQuery::__construct example
  • Example#2439 - TokyoTyrantQuery::count example
  • Example#2440 - TokyoTyrantQuery iterator example
  • Example#2441 - TokyoTyrantQuery::hint example
  • Example#2442 - TokyoTyrantQuery iterator example
  • Example#2443 - TokyoTyrantQuery::metaSearch example
  • Example#2444 - TokyoTyrantQuery iterator example
  • Example#2445 - TokyoTyrantQuery::out example
  • Example#2446 - TokyoTyrantQuery iterator example
  • Example#2447 - TokyoTyrantQuery::search example
  • Example#2448 - TokyoTyrantQuery iterator example
  • Example#2449 - TokyoTyrantIterator::__construct example
  • Example#2450 - cal_days_in_month example
  • Example#2451 - cal_from_jd example
  • Example#2452 - cal_info example
  • Example#2453 - easter_date example
  • Example#2454 - easter_days example
  • Example#2455 - Calendar functions
  • Example#2456 - jdtojewish Example
  • Example#2457 - DateTime::add/sub add intervals which cover elapsed time
  • Example#2458 - DateTime::modify and strtotime increment or decrement individual component values
  • Example#2459 - Adding or subtracting times can over- or underflow dates
  • Example#2460 - DateTime::add example
  • Example#2461 - Further DateTime::add examples
  • Example#2462 - Beware when adding months
  • Example#2463 - DateTime::__construct example
  • Example#2464 - Intricacies of DateTime::__construct
  • Example#2465 - DateTime::createFromFormat example
  • Example#2466 - Intricacies of DateTime::createFromFormat
  • Example#2467 - Format string with literal characters
  • Example#2468 - DateTime::getLastErrors example
  • Example#2469 - DateTime::modify example
  • Example#2470 - Beware when adding or subtracting months
  • Example#2471 - DateTime::setDate example
  • Example#2472 - Values exceeding ranges are added to their parent values
  • Example#2473 - DateTime::setISODate example
  • Example#2474 - Values exceeding ranges are added to their parent values
  • Example#2475 - Finding the month a week is in
  • Example#2476 - DateTime::setTime example
  • Example#2477 - Values exceeding ranges are added to their parent values
  • Example#2478 - DateTime::setTimestamp example
  • Example#2479 - DateTime::setTimestamp alternative in PHP 5.2
  • Example#2480 - DateTime::setTimeZone example
  • Example#2481 - DateTime::sub example
  • Example#2482 - Further DateTime::sub examples
  • Example#2483 - Beware when subtracting months
  • Example#2484 - Creating an immutable date time object
  • Example#2485 - DateTime::diff example
  • Example#2486 - DateTime object comparison
  • Example#2487 - DateTime::format example
  • Example#2488 - DateTime::getOffset example
  • Example#2489 - DateTime::getTimestamp example
  • Example#2490 - DateTime::getTimezone example
  • Example#2491 - Catching errors when instantiating DateTimeZone
  • Example#2492 - DateTimeZone::getLocation example
  • Example#2493 - DateTimeZone::getOffset examples
  • Example#2494 - A timezone_transitions_get example
  • Example#2495 - A timezone_abbreviations_list example
  • Example#2496 - A timezone_identifiers_list example
  • Example#2497 - DateInterval example
  • Example#2498 - Parsing valid date intervals
  • Example#2499 - DateInterval example
  • Example#2500 - DateInterval and carry over points
  • Example#2501 - DateInterval and DateTime::diff with the %a and %d modifiers
  • Example#2502 - DatePeriod example
  • Example#2503 - DatePeriod example with DatePeriod::EXCLUDE_START_DATE
  • Example#2504 - DatePeriod::getDateInterval example
  • Example#2505 - DatePeriod::getEndDate example
  • Example#2506 - DatePeriod::getEndDate without an end date
  • Example#2507 - DatePeriod::getStartDate example
  • Example#2508 - checkdate 예제
  • Example#2509 - Getting the default timezone
  • Example#2510 - Getting the abbreviation of a timezone
  • Example#2511 - Getting the default timezone
  • Example#2512 - date_parse_from_format example
  • Example#2513 - A date_parse example
  • Example#2514 - date_parse with relative formats
  • Example#2515 - A date_sun_info example
  • Example#2516 - date_sunrise example
  • Example#2517 - date_sunset example
  • Example#2518 - date 예제코드
  • Example#2519 - date의 회피 문자
  • Example#2520 - date와 mktime 예제
  • Example#2521 - date 형식화
  • Example#2522 - getdate 예제
  • Example#2523 - gettimeofday example
  • Example#2524 - gmdate example
  • Example#2525 - gmmktime basic example
  • Example#2526 - gmstrftime example
  • Example#2527 - idate example
  • Example#2528 - localtime example
  • Example#2529 - Timing script execution with microtime
  • Example#2530 - Timing script execution in PHP 5
  • Example#2531 - microtime and REQUEST_TIME_FLOAT (as of PHP 5.4.0)
  • Example#2532 - mktime basic example
  • Example#2533 - mktime example
  • Example#2534 - Last day of a month
  • Example#2535 - strftime locale examples
  • Example#2536 - ISO 8601:1988 week number example
  • Example#2537 - Cross platform compatible example using the %e modifier
  • Example#2538 - Display all known and unknown formats.
  • Example#2539 - strptime example
  • Example#2540 - A strtotime example
  • Example#2541 - Checking for failure
  • Example#2542 - time example
  • Example#2543 - A timezone_name_from_abbr example
  • Example#2544 - Getting the timezonedb version
  • Example#2545 - Measure several code blocks execution and get the total
  • Example#2546 - Closing an open file descriptor
  • Example#2547 - Setting and clearing a lock
  • Example#2548 - Opening a file descriptor
  • Example#2549 - Positioning in a file
  • Example#2550 - Setting the baud rate on a serial port
  • Example#2551 - chdir 예제
  • Example#2552 - closedir 예제
  • Example#2553 - dir example
  • Example#2554 - getcwd 예제
  • Example#2555 - opendir 예제
  • Example#2556 - 디렉토리 안의 모든 파일 목록
  • Example#2557 - .과 ..을 제외한 현재 디렉토리의 모든 파일 목록
  • Example#2558 - 간단한 scandir 예제
  • Example#2559 - scandir의 PHP 4 대체
  • Example#2560 - A finfo_buffer example
  • Example#2561 - A finfo_file example
  • Example#2562 - 객체 기반 형식
  • Example#2563 - 절차식 형식
  • Example#2564 - mime_content_type Example
  • Example#2565 - basename example
  • Example#2566 - Changing a file's group
  • Example#2567 - Simple chown usage
  • Example#2568 - clearstatcache example
  • Example#2569 - copy example
  • Example#2570 - dirname example
  • Example#2571 - disk_free_space example
  • Example#2572 - disk_total_space example
  • Example#2573 - A simple fclose example
  • Example#2574 - Handling timeouts with feof
  • Example#2575 - feof example with an invalid file pointer
  • Example#2576 - File write example using fflush
  • Example#2577 - A fgetc example
  • Example#2578 - Read and print the entire contents of a CSV file
  • Example#2579 - Reading a file line by line
  • Example#2580 - Reading a PHP file line-by-line
  • Example#2581 - Testing whether a file exists
  • Example#2582 - Get and output the source of the homepage of a website
  • Example#2583 - Searching within the include_path
  • Example#2584 - Reading a section of a file
  • Example#2585 - Using stream contexts
  • Example#2586 - Simple usage example
  • Example#2587 - Using flags
  • Example#2588 - file example
  • Example#2589 - fileatime example
  • Example#2590 - A filectime example
  • Example#2591 - Finding the group of a file
  • Example#2592 - Comparing the inode of a file with the current file
  • Example#2593 - filemtime example
  • Example#2594 - Finding the owner of a file
  • Example#2595 - Display permissions as an octal value
  • Example#2596 - Display full permissions
  • Example#2597 - filesize example
  • Example#2598 - filetype example
  • Example#2599 - flock example
  • Example#2600 - flock using the LOCK_NB option
  • Example#2601 - Checking a color name against a shell wildcard pattern
  • Example#2602 - fopen examples
  • Example#2603 - Using fpassthru with binary files
  • Example#2604 - fputcsv example
  • Example#2605 - A simple fread example
  • Example#2606 - Binary fread example
  • Example#2607 - Remote fread examples
  • Example#2608 - fscanf Example
  • Example#2609 - Contents of users.txt
  • Example#2610 - fseek example
  • Example#2611 - fstat example
  • Example#2612 - ftell example
  • Example#2613 - File truncation example
  • Example#2614 - A simple fwrite example
  • Example#2615 - Convenient way how glob can replace opendir and friends.
  • Example#2616 - is_dir example
  • Example#2617 - is_executable example
  • Example#2618 - is_file example
  • Example#2619 - Create and confirm if a file is a symbolic link
  • Example#2620 - is_readable example
  • Example#2621 - is_uploaded_file example
  • Example#2622 - is_writable example
  • Example#2623 - Changing the group of a symbolic link
  • Example#2624 - Changing the owner of a symbolic link
  • Example#2625 - Creating a simple hard link
  • Example#2626 - linkinfo example
  • Example#2627 - Comparison of stat and lstat
  • Example#2628 - mkdir example
  • Example#2629 - mkdir using the recursive parameter
  • Example#2630 - Uploading multiple files
  • Example#2631 - Contents of sample.ini
  • Example#2632 - parse_ini_file example
  • Example#2633 - parse_ini_file parsing a php.ini file
  • Example#2634 - pathinfo Example
  • Example#2635 - pathinfo example showing difference between null and no extension
  • Example#2636 - pathinfo example for a dot-file
  • Example#2637 - pclose example
  • Example#2638 - popen example
  • Example#2639 - popen example
  • Example#2640 - Forcing a download using readfile
  • Example#2641 - readlink example
  • Example#2642 - realpath_cache_get example
  • Example#2643 - realpath_cache_size example
  • Example#2644 - realpath example
  • Example#2645 - realpath on Windows
  • Example#2646 - Example with rename
  • Example#2647 - rewind overwriting example
  • Example#2648 - rmdir example
  • Example#2649 - stat example
  • Example#2650 - Using stat information together with touch
  • Example#2651 - Create a symbolic link
  • Example#2652 - tempnam example
  • Example#2653 - tmpfile example
  • Example#2654 - touch example
  • Example#2655 - touch using the time parameter
  • Example#2656 - umask example
  • Example#2657 - Basic unlink usage
  • Example#2658 - Example usage of inotify
  • Example#2659 - Setting the path to magic.mime
  • Example#2660 - setproctitle example
  • Example#2661 - setthreadtitle example
  • Example#2662 - Checks if system administrator has signed the file
  • Example#2663 - Prints names of all extended attributes of file
  • Example#2664 - Removes all extended attributes of a file
  • Example#2665 - Sets extended attributes on .wav file
  • Example#2666 - xattr_supported example
  • Example#2667 - xdiff_file_bdiff_size example
  • Example#2668 - xdiff_file_bdiff example
  • Example#2669 - xdiff_file_bpatch example
  • Example#2670 - xdiff_file_diff_binary example
  • Example#2671 - xdiff_file_diff example
  • Example#2672 - xdiff_file_merge3 example
  • Example#2673 - xdiff_file_patch_binary example
  • Example#2674 - xdiff_file_patch example
  • Example#2675 - Patch reversing example
  • Example#2676 - xdiff_file_rabdiff example
  • Example#2677 - xdiff_string_bdiff_size example
  • Example#2678 - xdiff_string_diff example
  • Example#2679 - xdiff_string_patch example
  • Example#2680 - Enchant Usage Example
  • Example#2681 - List the backends provided by the given broker
  • Example#2682 - A enchant_broker_dict_exists example
  • Example#2683 - List all available dictionaries for one broker
  • Example#2684 - A enchant_broker_request_dict example
  • Example#2685 - Adding a word to a PWL
  • Example#2686 - A enchant_dict_describe example
  • Example#2687 - A enchant_dict_quick_check example
  • Example#2688 - A enchant_dict_suggest example
  • Example#2689 - Usage example.
  • Example#2690 - Using Gender\Gender::country
  • Example#2691 - bindtextdomain example
  • Example#2692 - gettext-check
  • Example#2693 - ngettext example
  • Example#2694 - iconv_get_encoding example
  • Example#2695 - iconv_mime_decode_headers example
  • Example#2696 - iconv_mime_decode example
  • Example#2697 - iconv_mime_encode example
  • Example#2698 - iconv_set_encoding example
  • Example#2699 - iconv example
  • Example#2700 - ob_iconv_handler example:
  • Example#2701 - Example of using the procedural API
  • Example#2702 - Example of using the object-oriented API
  • Example#2703 - FRENCH_COLLATION rules
  • Example#2704 - ALTERNATE_HANDLING rules
  • Example#2705 - CASE_FIRST rules
  • Example#2706 - CASE_LEVEL rules
  • Example#2707 - collator_asortexample
  • Example#2708 - collator_compareexample
  • Example#2709 - Collator::__construct example
  • Example#2710 - collator_create example
  • Example#2711 - collator_get_attribute example
  • Example#2712 - collator_get_error_code example
  • Example#2713 - collator_get_error_message example
  • Example#2714 - collator_get_locale example
  • Example#2715 - collator_get_sort_keyexample
  • Example#2716 - collator_get_strength example
  • Example#2717 - collator_set_attribute example
  • Example#2718 - collator_set_strength example
  • Example#2719 - collator_sort_with_sort_keys example
  • Example#2720 - collator_sort example
  • Example#2721 - numfmt_create example
  • Example#2722 - NumberFormatter::create example
  • Example#2723 - numfmt_format_currency example
  • Example#2724 - OO example
  • Example#2725 - numfmt_format example
  • Example#2726 - OO example
  • Example#2727 - numfmt_get_attribute example
  • Example#2728 - OO example
  • Example#2729 - numfmt_get_error_code example
  • Example#2730 - OO example
  • Example#2731 - numfmt_get_error_message example
  • Example#2732 - OO example
  • Example#2733 - numfmt_get_locale example
  • Example#2734 - numfmt_get_pattern example
  • Example#2735 - OO example
  • Example#2736 - numfmt_get_symbol example
  • Example#2737 - OO example
  • Example#2738 - numfmt_get_text_attribute example
  • Example#2739 - OO example
  • Example#2740 - numfmt_parse_currency example
  • Example#2741 - OO example
  • Example#2742 - numfmt_parse example
  • Example#2743 - OO example
  • Example#2744 - numfmt_set_attribute example
  • Example#2745 - OO example
  • Example#2746 - numfmt_set_pattern example
  • Example#2747 - OO example
  • Example#2748 - numfmt_set_symbol example
  • Example#2749 - OO example
  • Example#2750 - numfmt_set_text_attribute example
  • Example#2751 - OO example
  • Example#2752 - locale_accept_from_http example
  • Example#2753 - OO example
  • Example#2754 - locale_compose example
  • Example#2755 - OO example
  • Example#2756 - locale_filter_matches example
  • Example#2757 - OO example
  • Example#2758 - locale_get_all_variants example
  • Example#2759 - OO example
  • Example#2760 - locale_get_default example
  • Example#2761 - OO example
  • Example#2762 - locale_get_display_language example
  • Example#2763 - OO example
  • Example#2764 - locale_get_display_name example
  • Example#2765 - OO example
  • Example#2766 - locale_get_display_region example
  • Example#2767 - OO example
  • Example#2768 - locale_get_display_script example
  • Example#2769 - OO example
  • Example#2770 - locale_get_display_variant example
  • Example#2771 - OO example
  • Example#2772 - locale_get_keywords example
  • Example#2773 - OO example
  • Example#2774 - locale_get_primary_language example
  • Example#2775 - OO example
  • Example#2776 - locale_get_region example
  • Example#2777 - OO example
  • Example#2778 - locale_get_script example
  • Example#2779 - OO example
  • Example#2780 - locale_lookup example
  • Example#2781 - OO example
  • Example#2782 - locale_parse example
  • Example#2783 - OO example
  • Example#2784 - locale_set_default example
  • Example#2785 - OO example
  • Example#2786 - normalizer_is_normalized example
  • Example#2787 - OO example
  • Example#2788 - normalizer_normalize example
  • Example#2789 - OO example
  • Example#2790 - msgfmt_create example
  • Example#2791 - OO example
  • Example#2792 - msgfmt_format_message example
  • Example#2793 - OO example
  • Example#2794 - msgfmt_format example
  • Example#2795 - OO example
  • Example#2796 - msgfmt_get_error_code example
  • Example#2797 - OO example
  • Example#2798 - msgfmt_get_error_message example
  • Example#2799 - OO example
  • Example#2800 - msgfmt_get_locale example
  • Example#2801 - OO example
  • Example#2802 - msgfmt_get_pattern example
  • Example#2803 - OO example
  • Example#2804 - msgfmt_parse_message example
  • Example#2805 - OO example
  • Example#2806 - msgfmt_parse example
  • Example#2807 - OO example
  • Example#2808 - msgfmt_set_pattern example
  • Example#2809 - OO example
  • Example#2810 - IntlCalendar::add
  • Example#2811 - IntlCalendar::after
  • Example#2812 - IntlCalendar::clear examples
  • Example#2813 - IntlCalendar::createInstance
  • Example#2814 - IntlCalendar::equals
  • Example#2815 - IntlCalendar::fieldDifference
  • Example#2816 - IntlCalendar::fromDateTime
  • Example#2817 - IntlCalendar::get
  • Example#2818 - IntlCalendar::getActualMaximum
  • Example#2819 - IntlCalendar::getAvailableLocales()
  • Example#2820 - IntlCalendar::getDayOfWeekType
  • Example#2821 - IntlCalendar::getErrorCode and IntlCalendar::getErrorMessage
  • Example#2822 - IntlCalendar::getErrorMessage
  • Example#2823 - IntlCalendar::getFirstDayOfWeek
  • Example#2824 - IntlCalendar::getKeyworkValuesForLocale
  • Example#2825 - Maxima examples
  • Example#2826 - IntlCalendar::getLocale
  • Example#2827 - IntlCalendar::getMinimalDaysInFirstWeek
  • Example#2828 - IntlCalendar::getNow
  • Example#2829 - IntlCalendar::getRepeatedWallTimeOption
  • Example#2830 - IntlCalendar::getSkippedWallTimeOption
  • Example#2831 - IntlCalendar::getTime
  • Example#2832 - IntlCalendar::getTimeZone
  • Example#2833 - IntlCalendar::getType
  • Example#2834 - IntlCalendar::inDaylightTime
  • Example#2835 - IntlCalendar::isEquivalentTo
  • Example#2836 - IntlCalendar::isLenient
  • Example#2837 - IntlCalendar::isWeekend
  • Example#2838 - IntlCalendar::roll
  • Example#2839 - IntlCalendar::set
  • Example#2840 - IntlCalendar::setFirstDayOfWeek
  • Example#2841 - IntlCalendar::setTime
  • Example#2842 - IntlCalendar::setTimeZone
  • Example#2843 - IntlCalendar::toDateTime
  • Example#2844 - datefmt_create example
  • Example#2845 - OO example
  • Example#2846 - datefmt_format example
  • Example#2847 - OO example
  • Example#2848 - With IntlCalendar object
  • Example#2849 - IntlDateFormatter::formatObject examples
  • Example#2850 - datefmt_get_calendar example
  • Example#2851 - OO example
  • Example#2852 - datefmt_get_datetype example
  • Example#2853 - OO example
  • Example#2854 - datefmt_get_error_code example
  • Example#2855 - OO example
  • Example#2856 - datefmt_get_error_message example
  • Example#2857 - OO example
  • Example#2858 - datefmt_get_locale example
  • Example#2859 - OO example
  • Example#2860 - datefmt_get_pattern example
  • Example#2861 - OO example
  • Example#2862 - datefmt_get_timetype example
  • Example#2863 - OO example
  • Example#2864 - datefmt_get_timezone_id example
  • Example#2865 - OO example
  • Example#2866 - IntlDateFormatter::getCalendarObject example
  • Example#2867 - IntlDateFormatter::getTimeZone examples
  • Example#2868 - datefmt_is_lenient example
  • Example#2869 - OO example
  • Example#2870 - datefmt_localtime example
  • Example#2871 - OO example
  • Example#2872 - OO example
  • Example#2873 - datefmt_parse example
  • Example#2874 - datefmt_set_calendar example
  • Example#2875 - OO example
  • Example#2876 - Example with IntlCalendar argument
  • Example#2877 - datefmt_set_lenient example
  • Example#2878 - OO example
  • Example#2879 - datefmt_set_pattern example
  • Example#2880 - OO example
  • Example#2881 - datefmt_set_timezone_id example
  • Example#2882 - OO example
  • Example#2883 - IntlDateFormatter::setTimeZone examples
  • Example#2884 - resourcebundle_count example
  • Example#2885 - OO example
  • Example#2886 - resourcebundle_create example
  • Example#2887 - ResourceBundle::create example
  • Example#2888 - resourcebundle_get_error_code example
  • Example#2889 - OO example
  • Example#2890 - resourcebundle_get_error_message example
  • Example#2891 - OO example
  • Example#2892 - resourcebundle_get example
  • Example#2893 - OO example
  • Example#2894 - resourcebundle_locales example
  • Example#2895 - OO example
  • Example#2896 - Converting escaped UTF-16 code units
  • Example#2897 - grapheme_extract example
  • Example#2898 - grapheme_stripos example
  • Example#2899 - grapheme_stristr example
  • Example#2900 - grapheme_strlen example
  • Example#2901 - grapheme_strpos example
  • Example#2902 - grapheme_strripos example
  • Example#2903 - grapheme_strrpos example
  • Example#2904 - grapheme_strstr example
  • Example#2905 - grapheme_substr example
  • Example#2906 - idn_to_ascii example
  • Example#2907 - idn_to_utf8 example
  • Example#2908 - Testing different code points
  • Example#2909 - Testing different code points
  • Example#2910 - Testing different code points
  • Example#2911 - Testing different code points
  • Example#2912 - Testing different code points
  • Example#2913 - Testing different code points
  • Example#2914 - Testing different code points
  • Example#2915 - Testing different code points
  • Example#2916 - Testing different code points
  • Example#2917 - Enumerating over a sample range of code points
  • Example#2918 - Enumerating over a sample range of code points
  • Example#2919 - Testing different code points
  • Example#2920 - Testing different code points
  • Example#2921 - Testing different code points
  • Example#2922 - Testing different code points
  • Example#2923 - Testing different code points
  • Example#2924 - Testing different properties
  • Example#2925 - Testing different properties
  • Example#2926 - Testing different properties
  • Example#2927 - Testing different code points
  • Example#2928 - Testing different properties
  • Example#2929 - Testing different properties
  • Example#2930 - Testing different properties
  • Example#2931 - Testing different properties
  • Example#2932 - Testing different properties
  • Example#2933 - Testing different properties
  • Example#2934 - Testing different code points
  • Example#2935 - Testing different code points
  • Example#2936 - Testing different code points
  • Example#2937 - Testing different code points
  • Example#2938 - Testing different code points
  • Example#2939 - Testing different code points
  • Example#2940 - Testing different code points
  • Example#2941 - Testing different code points
  • Example#2942 - Testing different code points
  • Example#2943 - Testing different code points
  • Example#2944 - Testing different code points
  • Example#2945 - Testing different code points
  • Example#2946 - Testing different code points
  • Example#2947 - Testing different code points
  • Example#2948 - Testing different code points
  • Example#2949 - Testing different code points
  • Example#2950 - Testing different code points
  • Example#2951 - Testing different code points
  • Example#2952 - Testing different code points
  • Example#2953 - Testing different code points
  • Example#2954 - Testing different code points
  • Example#2955 - Testing different code points
  • Example#2956 - Testing different code points
  • Example#2957 - Testing different code points
  • Example#2958 - Testing different code points
  • Example#2959 - Testing different code points
  • Example#2960 - Testing different code points
  • Example#2961 - Testing different code points
  • Example#2962 - Testing different code points
  • Example#2963 - Testing different code points
  • Example#2964 - Testing different code points
  • Example#2965 - Testing different code points
  • Example#2966 - intl_error_name example
  • Example#2967 - intl_get_error_code example
  • Example#2968 - intl_get_error_message example
  • Example#2969 - intl_is_failure example
  • Example#2970 - php.ini setting examples
  • Example#2971 - php.ini setting for EUC-JP users
  • Example#2972 - php.ini setting for SJIS users
  • Example#2973 - Disable HTTP input conversion in php.ini
  • Example#2974 - php.ini setting example
  • Example#2975 - Script example
  • Example#2976 - mb_convert_case example
  • Example#2977 - mb_convert_case example with non-Latin UTF-8 text
  • Example#2978 - mb_convert_encoding example
  • Example#2979 - mb_convert_kana example
  • Example#2980 - mb_convert_variables example
  • Example#2981 - convmap example
  • Example#2982 - convmap example escapes JavaScript string
  • Example#2983 - mb_detect_encoding example
  • Example#2984 - mb_detect_order examples
  • Example#2985 - Example showing useless detect orders
  • Example#2986 - mb_encode_mimeheader example
  • Example#2987 - convmap example
  • Example#2988 - mb_encode_numericentity example
  • Example#2989 - mb_encoding_aliases example
  • Example#2990 - mb_ereg_replace_callback example
  • Example#2991 - mb_ereg_replace_callback using anonymous function supported in PHP 5.3.0 or later
  • Example#2992 - mb_internal_encoding example
  • Example#2993 - mb_list_encodings example
  • Example#2994 - mb_output_handler example
  • Example#2995 - mb_preferred_mime_name example
  • Example#2996 - mb_strimwidth example
  • Example#2997 - mb_strtolower example
  • Example#2998 - mb_strtolower example with non-Latin UTF-8 text
  • Example#2999 - mb_strtoupper example
  • Example#3000 - mb_strtoupper example with non-Latin UTF-8 text
  • Example#3001 - mb_substitute_character example
  • Example#3002 - mb_substr_count example
  • Example#3003 - pspell_add_to_personal
  • Example#3004 - pspell_check Example
  • Example#3005 - pspell_add_to_personal Example
  • Example#3006 - pspell_config_create
  • Example#3007 - pspell_config_ignore
  • Example#3008 - pspell_config_mode Example
  • Example#3009 - pspell_config_personal
  • Example#3010 - pspell_config_repl
  • Example#3011 - pspell_config_runtogether
  • Example#3012 - pspell_new_config
  • Example#3013 - pspell_new_personal
  • Example#3014 - pspell_new
  • Example#3015 - pspell_add_to_personal
  • Example#3016 - pspell_store_replacement
  • Example#3017 - pspell_suggest example
  • Example#3018 - Basic recode_file example
  • Example#3019 - Basic recode_string example
  • Example#3020 - Cairo Example
  • Example#3021 - cairo_create example
  • Example#3022 - cairo_font_face_get_type example
  • Example#3023 - cairo_font_face_status example
  • Example#3024 - cairo_font_options_create example
  • Example#3025 - cairo_font_options_equal example
  • Example#3026 - cairo_font_options_get_antialias example
  • Example#3027 - cairo_font_options_get_hint_metrics example
  • Example#3028 - cairo_font_options_get_hint_style example
  • Example#3029 - cairo_font_options_get_subpixel_order example
  • Example#3030 - cairo_font_options_hash example
  • Example#3031 - cairo_font_options_merge example
  • Example#3032 - cairo_font_options_set_antialias example
  • Example#3033 - cairo_font_options_set_hint_metrics example
  • Example#3034 - cairo_font_options_set_hint_style example
  • Example#3035 - cairo_font_options_set_subpixel_order example
  • Example#3036 - cairo_font_options_status example
  • Example#3037 - cairo_format_stride_for_width example
  • Example#3038 - cairo_image_surface_create_for_data example
  • Example#3039 - cairo_image_surface_create_from_png example
  • Example#3040 - cairo_image_surface_create example
  • Example#3041 - cairo_image_surface_get_data example
  • Example#3042 - cairo_image_surface_get_format example
  • Example#3043 - cairo_image_surface_get_height example
  • Example#3044 - cairo_image_surface_get_stride example
  • Example#3045 - cairo_image_surface_get_width example
  • Example#3046 - cairo_matrix_invert example
  • Example#3047 - cairo_matrix_multiply example
  • Example#3048 - cairo_matrix_rotate example
  • Example#3049 - cairo_matrix_transform_distance example
  • Example#3050 - cairo_matrix_transform_point example
  • Example#3051 - cairo_matrix_translate example
  • Example#3052 - cairo_pattern_add_color_stop_rgb example
  • Example#3053 - cairo_pattern_add_color_stop_rgba example
  • Example#3054 - cairo_pattern_create_for_surface example
  • Example#3055 - cairo_pattern_create_linear example
  • Example#3056 - cairo_pattern_create_radial example
  • Example#3057 - cairo_pattern_create_rgb example
  • Example#3058 - cairo_pattern_create_rgba example
  • Example#3059 - cairo_pattern_get_color_stop_count example
  • Example#3060 - cairo_pattern_get_color_stop_rgba example
  • Example#3061 - cairo_pattern_get_extend example
  • Example#3062 - cairo_pattern_get_filter example
  • Example#3063 - cairo_pattern_get_linear_points example
  • Example#3064 - cairo_pattern_get_matrix example
  • Example#3065 - cairo_pattern_get_radial_circles example
  • Example#3066 - cairo_pattern_get_rgba example
  • Example#3067 - cairo_pattern_get_surface example
  • Example#3068 - cairo_pattern_get_type example
  • Example#3069 - cairo_pattern_set_extend example
  • Example#3070 - cairo_pattern_set_filter example
  • Example#3071 - cairo_pattern_set_matrix example
  • Example#3072 - cairo_pattern_status example
  • Example#3073 - cairo_pdf_surface_create example
  • Example#3074 - cairo_pdf_surface_set_size example
  • Example#3075 - cairo_ps_get_levels example
  • Example#3076 - cairo_ps_level_to_string example
  • Example#3077 - cairo_ps_surface_create example
  • Example#3078 - cairo_ps_surface_dsc_begin_page_setup example
  • Example#3079 - cairo_ps_surface_dsc_begin_setup example
  • Example#3080 - cairo_ps_surface_dsc_comment example
  • Example#3081 - cairo_ps_surface_get_eps example
  • Example#3082 - cairo_ps_surface_restrict_to_level example
  • Example#3083 - cairo_ps_surface_set_eps example
  • Example#3084 - cairo_ps_surface_set_size example
  • Example#3085 - cairo_scaled_font_create example
  • Example#3086 - cairo_scaled_font_extents example
  • Example#3087 - cairo_scaled_font_get_ctm example
  • Example#3088 - cairo_scaled_font_get_font_face example
  • Example#3089 - cairo_scaled_font_get_font_matrix example
  • Example#3090 - cairo_scaled_font_get_font_options example
  • Example#3091 - cairo_scaled_font_get_scale_matrix example
  • Example#3092 - cairo_scaled_font_get_type example
  • Example#3093 - cairo_scaled_font_glyph_extents example
  • Example#3094 - cairo_scaled_font_status example
  • Example#3095 - cairo_scaled_font_text_extents example
  • Example#3096 - cairo_surface_copy_page example
  • Example#3097 - cairo_surface_create_similar example
  • Example#3098 - cairo_surface_finish example
  • Example#3099 - cairo_surface_flush example
  • Example#3100 - cairo_surface_get_content example
  • Example#3101 - cairo_surface_get_device_offset example
  • Example#3102 - cairo_surface_get_font_options example
  • Example#3103 - cairo_surface_get_type example
  • Example#3104 - cairo_surface_mark_dirty_rectangle example
  • Example#3105 - cairo_surface_mark_dirty example
  • Example#3106 - cairo_surface_set_device_offset example
  • Example#3107 - cairo_surface_set_fallback_resolution example
  • Example#3108 - cairo_surface_show_page example
  • Example#3109 - cairo_surface_status example
  • Example#3110 - cairo_surface_write_to_png example
  • Example#3111 - cairo_svg_surface_create example
  • Example#3112 - cairo_svg_surface_restrict_to_version example
  • Example#3113 - cairo_svg_version_to_string example
  • Example#3114 - 객체 기반 형식
  • Example#3115 - 절차식 형식
  • Example#3116 - 객체 기반 형식
  • Example#3117 - 절차식 형식
  • Example#3118 - 객체 기반 형식
  • Example#3119 - 절차식 형식
  • Example#3120 - 객체 기반 형식
  • Example#3121 - 절차식 형식
  • Example#3122 - 객체 기반 형식
  • Example#3123 - 절차식 형식
  • Example#3124 - 객체 기반 형식
  • Example#3125 - 절차식 형식
  • Example#3126 - 객체 기반 형식
  • Example#3127 - 절차식 형식
  • Example#3128 - 객체 기반 형식
  • Example#3129 - 절차식 형식
  • Example#3130 - 객체 기반 형식
  • Example#3131 - 절차식 형식
  • Example#3132 - 객체 기반 형식
  • Example#3133 - 절차식 형식
  • Example#3134 - 객체 기반 형식
  • Example#3135 - 절차식 형식
  • Example#3136 - 객체 기반 형식
  • Example#3137 - 절차식 형식
  • Example#3138 - 객체 기반 형식
  • Example#3139 - 절차식 형식
  • Example#3140 - CairoContext::__construct example
  • Example#3141 - 객체 기반 형식
  • Example#3142 - 절차식 형식
  • Example#3143 - 객체 기반 형식
  • Example#3144 - 절차식 형식
  • Example#3145 - 객체 기반 형식
  • Example#3146 - 절차식 형식
  • Example#3147 - 객체 기반 형식
  • Example#3148 - 절차식 형식
  • Example#3149 - 객체 기반 형식
  • Example#3150 - 절차식 형식
  • Example#3151 - 객체 기반 형식
  • Example#3152 - 절차식 형식
  • Example#3153 - 객체 기반 형식
  • Example#3154 - 절차식 형식
  • Example#3155 - 객체 기반 형식
  • Example#3156 - 절차식 형식
  • Example#3157 - 객체 기반 형식
  • Example#3158 - 절차식 형식
  • Example#3159 - 객체 기반 형식
  • Example#3160 - 절차식 형식
  • Example#3161 - 객체 기반 형식
  • Example#3162 - 절차식 형식
  • Example#3163 - 객체 기반 형식
  • Example#3164 - 절차식 형식
  • Example#3165 - 객체 기반 형식
  • Example#3166 - 절차식 형식
  • Example#3167 - 객체 기반 형식
  • Example#3168 - 절차식 형식
  • Example#3169 - 객체 기반 형식
  • Example#3170 - 절차식 형식
  • Example#3171 - 객체 기반 형식
  • Example#3172 - 절차식 형식
  • Example#3173 - 객체 기반 형식
  • Example#3174 - 절차식 형식
  • Example#3175 - 객체 기반 형식
  • Example#3176 - 절차식 형식
  • Example#3177 - 객체 기반 형식
  • Example#3178 - 절차식 형식
  • Example#3179 - 객체 기반 형식
  • Example#3180 - 절차식 형식
  • Example#3181 - 객체 기반 형식
  • Example#3182 - 절차식 형식
  • Example#3183 - 객체 기반 형식
  • Example#3184 - 절차식 형식
  • Example#3185 - 객체 기반 형식
  • Example#3186 - 절차식 형식
  • Example#3187 - 객체 기반 형식
  • Example#3188 - 절차식 형식
  • Example#3189 - 객체 기반 형식
  • Example#3190 - 절차식 형식
  • Example#3191 - 객체 기반 형식
  • Example#3192 - 절차식 형식
  • Example#3193 - 객체 기반 형식
  • Example#3194 - 절차식 형식
  • Example#3195 - 객체 기반 형식
  • Example#3196 - 절차식 형식
  • Example#3197 - 객체 기반 형식
  • Example#3198 - 절차식 형식
  • Example#3199 - 객체 기반 형식
  • Example#3200 - 절차식 형식
  • Example#3201 - 객체 기반 형식
  • Example#3202 - 절차식 형식
  • Example#3203 - 객체 기반 형식
  • Example#3204 - 절차식 형식
  • Example#3205 - 객체 기반 형식
  • Example#3206 - 절차식 형식
  • Example#3207 - 객체 기반 형식
  • Example#3208 - 절차식 형식
  • Example#3209 - 객체 기반 형식
  • Example#3210 - 절차식 형식
  • Example#3211 - 객체 기반 형식
  • Example#3212 - 절차식 형식
  • Example#3213 - 객체 기반 형식
  • Example#3214 - 절차식 형식
  • Example#3215 - 객체 기반 형식
  • Example#3216 - 절차식 형식
  • Example#3217 - 객체 기반 형식
  • Example#3218 - 절차식 형식
  • Example#3219 - 객체 기반 형식
  • Example#3220 - 절차식 형식
  • Example#3221 - 객체 기반 형식
  • Example#3222 - 절차식 형식
  • Example#3223 - 객체 기반 형식
  • Example#3224 - 절차식 형식
  • Example#3225 - 객체 기반 형식
  • Example#3226 - 절차식 형식
  • Example#3227 - 객체 기반 형식
  • Example#3228 - 절차식 형식
  • Example#3229 - 객체 기반 형식
  • Example#3230 - 절차식 형식
  • Example#3231 - 객체 기반 형식
  • Example#3232 - 절차식 형식
  • Example#3233 - 객체 기반 형식
  • Example#3234 - 절차식 형식
  • Example#3235 - 객체 기반 형식
  • Example#3236 - 절차식 형식
  • Example#3237 - 객체 기반 형식
  • Example#3238 - 절차식 형식
  • Example#3239 - 객체 기반 형식
  • Example#3240 - 절차식 형식
  • Example#3241 - 객체 기반 형식
  • Example#3242 - 절차식 형식
  • Example#3243 - 객체 기반 형식
  • Example#3244 - 절차식 형식
  • Example#3245 - 객체 기반 형식
  • Example#3246 - 절차식 형식
  • Example#3247 - 객체 기반 형식
  • Example#3248 - 절차식 형식
  • Example#3249 - 객체 기반 형식
  • Example#3250 - 절차식 형식
  • Example#3251 - 객체 기반 형식
  • Example#3252 - 절차식 형식
  • Example#3253 - 객체 기반 형식
  • Example#3254 - 절차식 형식
  • Example#3255 - 객체 기반 형식
  • Example#3256 - 절차식 형식
  • Example#3257 - 객체 기반 형식
  • Example#3258 - 절차식 형식
  • Example#3259 - 객체 기반 형식
  • Example#3260 - 절차식 형식
  • Example#3261 - 객체 기반 형식
  • Example#3262 - 절차식 형식
  • Example#3263 - 객체 기반 형식
  • Example#3264 - 절차식 형식
  • Example#3265 - 객체 기반 형식
  • Example#3266 - 절차식 형식
  • Example#3267 - 객체 기반 형식
  • Example#3268 - 절차식 형식
  • Example#3269 - 객체 기반 형식
  • Example#3270 - 절차식 형식
  • Example#3271 - 객체 기반 형식
  • Example#3272 - 절차식 형식
  • Example#3273 - 객체 기반 형식
  • Example#3274 - 절차식 형식
  • Example#3275 - 객체 기반 형식
  • Example#3276 - 절차식 형식
  • Example#3277 - 객체 기반 형식
  • Example#3278 - 절차식 형식
  • Example#3279 - 객체 기반 형식
  • Example#3280 - 절차식 형식
  • Example#3281 - 객체 기반 형식
  • Example#3282 - 절차식 형식
  • Example#3283 - 객체 기반 형식
  • Example#3284 - 절차식 형식
  • Example#3285 - 객체 기반 형식
  • Example#3286 - 절차식 형식
  • Example#3287 - 객체 기반 형식
  • Example#3288 - 절차식 형식
  • Example#3289 - 객체 기반 형식
  • Example#3290 - 절차식 형식
  • Example#3291 - 객체 기반 형식
  • Example#3292 - 절차식 형식
  • Example#3293 - 객체 기반 형식
  • Example#3294 - 절차식 형식
  • Example#3295 - 객체 기반 형식
  • Example#3296 - 절차식 형식
  • Example#3297 - 객체 기반 형식
  • Example#3298 - 절차식 형식
  • Example#3299 - 객체 기반 형식
  • Example#3300 - 절차식 형식
  • Example#3301 - 객체 기반 형식
  • Example#3302 - 절차식 형식
  • Example#3303 - 객체 기반 형식
  • Example#3304 - 절차식 형식
  • Example#3305 - 객체 기반 형식
  • Example#3306 - 절차식 형식
  • Example#3307 - 객체 기반 형식
  • Example#3308 - 절차식 형식
  • Example#3309 - 객체 기반 형식
  • Example#3310 - 절차식 형식
  • Example#3311 - 객체 기반 형식
  • Example#3312 - 절차식 형식
  • Example#3313 - 객체 기반 형식
  • Example#3314 - 절차식 형식
  • Example#3315 - CairoSurface::createSimilar example
  • Example#3316 - CairoSurface::finish example
  • Example#3317 - CairoSurface::flush example
  • Example#3318 - CairoSurface::getContent example
  • Example#3319 - CairoSurface::getDeviceOffset example
  • Example#3320 - 객체 기반 형식
  • Example#3321 - 절차식 형식
  • Example#3322 - CairoSurface::getType example
  • Example#3323 - CairoSurface::markDirty example
  • Example#3324 - CairoSurface::markDirtyRectangle example
  • Example#3325 - CairoSurface::setDeviceOffset example
  • Example#3326 - CairoSurface::setFallbackResolution example
  • Example#3327 - 객체 기반 형식
  • Example#3328 - 절차식 형식
  • Example#3329 - 객체 기반 형식
  • Example#3330 - 절차식 형식
  • Example#3331 - CairoSurface::writeToPng example
  • Example#3332 - CairoSvgSurface::__construct example
  • Example#3333 - CairoSvgSurface::getVersions example
  • Example#3334 - 절차식 형식
  • Example#3335 - CairoSvgSurface::restrictToVersion example
  • Example#3336 - CairoSvgSurface::versionToString example
  • Example#3337 - CairoImageSurface::__construct example
  • Example#3338 - CairoImageSurface::createForData example
  • Example#3339 - CairoImageSurface::createFromPng example
  • Example#3340 - CairoImageSurface::getData example
  • Example#3341 - CairoImageSurface::getFormat example
  • Example#3342 - CairoImageSurface::getHeight example
  • Example#3343 - CairoImageSurface::getStride example
  • Example#3344 - CairoImageSurface::getWidth example
  • Example#3345 - CairoPdfSurface::__construct example
  • Example#3346 - CairoPdfSurface::setSize example
  • Example#3347 - CairoPsSurface::__construct example
  • Example#3348 - CairoPsSurface::dscBeginPageSetup example
  • Example#3349 - CairoPsSurface::dscBeginSetup example
  • Example#3350 - CairoPsSurface::dscComment example
  • Example#3351 - CairoPsSurface::getEps example
  • Example#3352 - CairoPsSurface::getLevels example
  • Example#3353 - CairoPsSurface::levelToString example
  • Example#3354 - CairoPsSurface::restrictToLevel example
  • Example#3355 - CairoPsSurface::setEps example
  • Example#3356 - CairoPsSurface::setSize example
  • Example#3357 - CairoFontFace::__construct example
  • Example#3358 - CairoFontFace::getType example
  • Example#3359 - 객체 기반 형식
  • Example#3360 - 절차식 형식
  • Example#3361 - CairoFontOptions::__construct example
  • Example#3362 - CairoFontOptions::equal example
  • Example#3363 - 객체 기반 형식
  • Example#3364 - 절차식 형식
  • Example#3365 - CairoFontOptions::getHintMetrics example
  • Example#3366 - CairoFontOptions::getHintStyle example
  • Example#3367 - CairoFontOptions::getSubpixelOrder example
  • Example#3368 - CairoFontOptions::hash example
  • Example#3369 - CairoFontOptions::merge example
  • Example#3370 - 객체 기반 형식
  • Example#3371 - 절차식 형식
  • Example#3372 - CairoFontOptions::setHintMetrics example
  • Example#3373 - CairoFontOptions::setHintStyle example
  • Example#3374 - CairoFontOptions::setSubpixelOrder example
  • Example#3375 - 객체 기반 형식
  • Example#3376 - 절차식 형식
  • Example#3377 - CairoScaledFont::__construct example
  • Example#3378 - CairoScaledFont::extents example
  • Example#3379 - CairoScaledFont::getCtm example
  • Example#3380 - 객체 기반 형식
  • Example#3381 - 절차식 형식
  • Example#3382 - 객체 기반 형식
  • Example#3383 - 절차식 형식
  • Example#3384 - 객체 기반 형식
  • Example#3385 - 절차식 형식
  • Example#3386 - CairoScaledFont::getScaleMatrix example
  • Example#3387 - CairoScaledFont::getType example
  • Example#3388 - CairoScaledFont::glyphExtents example
  • Example#3389 - 객체 기반 형식
  • Example#3390 - 절차식 형식
  • Example#3391 - 객체 기반 형식
  • Example#3392 - 절차식 형식
  • Example#3393 - CairoPattern::__construct example
  • Example#3394 - 객체 기반 형식
  • Example#3395 - 절차식 형식
  • Example#3396 - CairoPattern::getType example
  • Example#3397 - 객체 기반 형식
  • Example#3398 - 절차식 형식
  • Example#3399 - 객체 기반 형식
  • Example#3400 - 절차식 형식
  • Example#3401 - CairoGradientPattern::addColorStopRgb example
  • Example#3402 - CairoGradientPattern::addColorStopRgba example
  • Example#3403 - CairoGradientPattern::getColorStopCount example
  • Example#3404 - CairoGradientPattern::getColorStopRgba example
  • Example#3405 - CairoGradientPattern::getExtend example
  • Example#3406 - CairoGradientPattern::setExtend example
  • Example#3407 - CairoSolidPattern::__construct example
  • Example#3408 - CairoSolidPattern::getRgba example
  • Example#3409 - CairoSurfacePattern::__construct example
  • Example#3410 - CairoSurfacePattern::getExtend example
  • Example#3411 - CairoSurfacePattern::getFilter example
  • Example#3412 - CairoSurfacePattern::getSurface example
  • Example#3413 - CairoSurfacePattern::setExtend example
  • Example#3414 - CairoSurfacePattern::setFilter example
  • Example#3415 - CairoLinearGradient::__construct example
  • Example#3416 - CairoLinearGradient::getPoints example
  • Example#3417 - CairoRadialGradient::__construct example
  • Example#3418 - CairoRadialGradient::getCircles example
  • Example#3419 - CairoFormat::strideForWidth example
  • Example#3420 - 객체 기반 형식
  • Example#3421 - 절차식 형식
  • Example#3422 - 객체 기반 형식
  • Example#3423 - 절차식 형식
  • Example#3424 - 객체 기반 형식
  • Example#3425 - 절차식 형식
  • Example#3426 - 객체 기반 형식
  • Example#3427 - 절차식 형식
  • Example#3428 - 객체 기반 형식
  • Example#3429 - 절차식 형식
  • Example#3430 - CairoMatrix::invert example
  • Example#3431 - CairoMatrix::multiply example
  • Example#3432 - 객체 기반 형식
  • Example#3433 - 절차식 형식
  • Example#3434 - 객체 기반 형식
  • Example#3435 - 절차식 형식
  • Example#3436 - CairoMatrix::transformDistance example
  • Example#3437 - CairoMatrix::transformPoint example
  • Example#3438 - 객체 기반 형식
  • Example#3439 - 절차식 형식
  • Example#3440 - exif_imagetype example
  • Example#3441 - exif_read_data example
  • Example#3442 - exif_tagname example
  • Example#3443 - exif_thumbnail example
  • Example#3444 - PNG creation with PHP
  • Example#3445 - Adding watermarks to images using alpha channels
  • Example#3446 - Using imagecopymerge to create a translucent watermark
  • Example#3447 - Using gd_info
  • Example#3448 - getimagesize and MIME types
  • Example#3449 - getimagesize example
  • Example#3450 - getimagesize (URL)
  • Example#3451 - getimagesize() returning IPTC
  • Example#3452 - getimagesizefromstring example
  • Example#3453 - image_type_to_extension example
  • Example#3454 - image_type_to_mime_type example
  • Example#3455 - image2wbmp example
  • Example#3456 - imageaffinematrixconcat example
  • Example#3457 - imageaffinematrixget example
  • Example#3458 - imagealphablending usage example
  • Example#3459 - A comparison of two lines, one with anti-aliasing switched on
  • Example#3460 - Drawing a circle with imagearc
  • Example#3461 - imagechar example
  • Example#3462 - imagecharup example
  • Example#3463 - imagecolorallocate example
  • Example#3464 - Example of using imagecolorallocatealpha
  • Example#3465 - Access distinct RGB values
  • Example#3466 - Human-readable RGB values using imagecolorsforindex
  • Example#3467 - Search for a set of colors in an image
  • Example#3468 - Search for a set of colors in an image
  • Example#3469 - Example of using imagecolorclosesthwb
  • Example#3470 - Using imagecolordeallocate
  • Example#3471 - Get colors from the GD logo
  • Example#3472 - Get colors from the GD logo
  • Example#3473 - imagecolormatch example
  • Example#3474 - Using imagecoloresolve to get colors from an image
  • Example#3475 - Using imagecoloresolvealpha to get colors from an image
  • Example#3476 - imagecolorset example
  • Example#3477 - imagecolorsforindex example
  • Example#3478 - Getting total number of colors in an image using imagecolorstotal
  • Example#3479 - imagecolortransparent example
  • Example#3480 - Embossing the PHP.net logo
  • Example#3481 - Gaussian blur
  • Example#3482 - Cropping the PHP.net logo
  • Example#3483 - Merging two copies of the PHP.net logo with 75% transparency
  • Example#3484 - imagecopymergegray usage
  • Example#3485 - Simple example
  • Example#3486 - Resampling an image proportionally
  • Example#3487 - Resizing an image
  • Example#3488 - Creating a new GD image stream and outputting an image.
  • Example#3489 - imagecreatefromgd2 example
  • Example#3490 - imagecreatefromgd2part example
  • Example#3491 - imagecreatefromgd example
  • Example#3492 - Example to handle an error during loading of a GIF
  • Example#3493 - Example to handle an error during loading of a JPEG
  • Example#3494 - Example to handle an error during loading of a PNG
  • Example#3495 - imagecreatefromstring example
  • Example#3496 - Example to handle an error during loading of a WBMP
  • Example#3497 - Convert an WebP image to a jpeg image using imagecreatefromwebp
  • Example#3498 - Convert an XBM image to a png image using imagecreatefromxbm
  • Example#3499 - Creating an image instance using imagecreatefromxpm
  • Example#3500 - Creating a new GD image stream and outputting an image.
  • Example#3501 - imagecrop example
  • Example#3502 - Proper handling of auto-cropping
  • Example#3503 - imagedashedline example
  • Example#3504 - Alternative to imagedashedline
  • Example#3505 - Using imagedestroy example
  • Example#3506 - imageellipse example
  • Example#3507 - imagefill example
  • Example#3508 - Creating a 3D looking pie
  • Example#3509 - imagefilledellipse example
  • Example#3510 - imagefilledpolygon example
  • Example#3511 - imagefilledrectangle usage
  • Example#3512 - Filling an ellipse with a color
  • Example#3513 - imagefilter grayscale example
  • Example#3514 - imagefilter brightness example
  • Example#3515 - imagefilter colorize example
  • Example#3516 - imagefilter negate example
  • Example#3517 - imagefilter pixelate example
  • Example#3518 - Flips an image vertically
  • Example#3519 - Flips the image horizontally
  • Example#3520 - Using imagefontheight on built-in fonts
  • Example#3521 - Using imagefontheight together with imageloadfont
  • Example#3522 - Using imagefontwidth on built-in fonts
  • Example#3523 - Using imagefontwidth together with imageloadfont
  • Example#3524 - imageftbbox example
  • Example#3525 - imagefttext example
  • Example#3526 - imagegammacorrect usage
  • Example#3527 - Outputting a GD2 image
  • Example#3528 - Saving a GD2 image
  • Example#3529 - Outputting a GD image
  • Example#3530 - Saving a GD image
  • Example#3531 - imagegetclip example
  • Example#3532 - Outputting an image using imagegif
  • Example#3533 - Converting a PNG image to GIF using imagegif
  • Example#3534 - imagegrabscreen example
  • Example#3535 - imagegrabwindow example
  • Example#3536 - Turn on interlacing using imageinterlace
  • Example#3537 - Simple detection of true color image instances using imageistruecolor
  • Example#3538 - Outputting a JPEG image to the browser
  • Example#3539 - Saving a JPEG image to a file
  • Example#3540 - Outputting the image at 75% quality to the browser
  • Example#3541 - imagelayereffect example
  • Example#3542 - Drawing a thick line
  • Example#3543 - imageloadfont usage example
  • Example#3544 - imageopenpolygon example
  • Example#3545 - imagepalettecopy example
  • Example#3546 - Converts any image resource to true color
  • Example#3547 - imagepolygon example
  • Example#3548 - imagepsbbox usage
  • Example#3549 - imagepsencodefont example
  • Example#3550 - imagepsextendfont example
  • Example#3551 - imagepsfreefont example
  • Example#3552 - imagepsloadfont example
  • Example#3553 - imagepsslantfont example
  • Example#3554 - imagepstext usage
  • Example#3555 - Simple imagerectangle example
  • Example#3556 - Setting and getting the resolution of an image
  • Example#3557 - Rotate an image 180 degrees
  • Example#3558 - imagesavealpha example
  • Example#3559 - imagesetbrush example
  • Example#3560 - imagesetinterpolation example
  • Example#3561 - imagesetpixel example
  • Example#3562 - imagesetstyle example
  • Example#3563 - imagesetthickness example
  • Example#3564 - imagesettile example
  • Example#3565 - imagestring example
  • Example#3566 - imagestringup example
  • Example#3567 - Using imagesx
  • Example#3568 - Using imagesy
  • Example#3569 - Converting a true color image to a palette-based image
  • Example#3570 - imagettfbbox example
  • Example#3571 - imagettftext example
  • Example#3572 - Checking for PNG support
  • Example#3573 - Outputting a WBMP image
  • Example#3574 - Saving the WBMP image
  • Example#3575 - Outputting the image with a different foreground
  • Example#3576 - Saving an WebP file
  • Example#3577 - Saving an XBM file
  • Example#3578 - Saving an XBM file with a different foreground color
  • Example#3579 - Embedding IPTC data into a JPEG
  • Example#3580 - iptcparse() used together with getimagesize
  • Example#3581 - jpeg2wbmp example
  • Example#3582 - png2wbmp example
  • Example#3583 - Gmagick Example
  • Example#3584 - Gmagick::despeckleimage example
  • Example#3585 - Gmagick::setCompressionQuality
  • Example#3586 - Creating a thumbnail in Imagick
  • Example#3587 - Make a thumbnail of all JPG files in a directory
  • Example#3588 - Creating a reflection of an image
  • Example#3589 - Filling text with gradient
  • Example#3590 - Read in GIF image and resize all frames
  • Example#3591 - Create a PHP logo
  • Example#3592 - Using Imagick::adaptiveBlurImage:
  • Example#3593 - Using Imagick::adaptiveResizeImage
  • Example#3594 - A Imagick::adaptiveSharpenImage example
  • Example#3595 - Imagick::adaptiveThresholdImage
  • Example#3596 - Imagick::addNoiseImage
  • Example#3597 - Imagick::affineTransformImage
  • Example#3598 - Using Imagick::annotateImage:
  • Example#3599 - Imagick::appendImages example
  • Example#3600 - Imagick::autoLevelImage
  • Example#3601 - Imagick::blackThresholdImage
  • Example#3602 - Imagick::blueShiftImage
  • Example#3603 - Using Imagick::blurImage:
  • Example#3604 - Imagick::borderImage
  • Example#3605 - Imagick::brightnessContrastImage
  • Example#3606 - Imagick::charcoalImage
  • Example#3607 - Using Imagick::chopImage:
  • Example#3608 - Imagick object cloning in different versions of imagick
  • Example#3609 - Using Imagick::clutImage:
  • Example#3610 - Imagick::colorizeImage
  • Example#3611 - Imagick::colorMatrixImage
  • Example#3612 - Using Imagick::commentImage:
  • Example#3613 - Using Imagick::compareImageLayers
  • Example#3614 - Using Imagick::compareImages:
  • Example#3615 - Using Imagick::compositeImage:
  • Example#3616 - Imagick::contrastImage
  • Example#3617 - Imagick::convolveImage
  • Example#3618 - Imagick::cropImage
  • Example#3619 - Imagick::deskewImage
  • Example#3620 - Imagick::despeckleImage
  • Example#3621 - Using Imagick::distortImage:
  • Example#3622 - Imagick::edgeImage
  • Example#3623 - Imagick::embossImage
  • Example#3624 - Imagick::enhanceImage
  • Example#3625 - Imagick::equalizeImage
  • Example#3626 - Using Imagick::evaluateImage
  • Example#3627 - Using Imagick::exportImagePixels
  • Example#3628 - Imagick::filter
  • Example#3629 - Imagick::flipImage
  • Example#3630 - Imagick::floodfillPaintImage example
  • Example#3631 - Imagick::flopImage
  • Example#3632 - Imagick::forwardFourierTransformImage
  • Example#3633 - Imagick::frameImage
  • Example#3634 - Create a sinusoidal gradient
  • Example#3635 - Create a gradient from the polynomial (4x^2 - 4x + 1)
  • Example#3636 - Create a complex gradient from the polynomial (4x^2 - 4x^2 + 1) modulated by a sinusoidal gradient
  • Example#3637 - Imagick::fxImage
  • Example#3638 - Imagick::gammaImage
  • Example#3639 - Imagick::gaussianBlurImage
  • Example#3640 - Imagick::getImageGeometry
  • Example#3641 - Generates Imagick::getImageHistogram
  • Example#3642 - Using Imagick::getImageLength:
  • Example#3643 - Using Imagick::getImageProperties:
  • Example#3644 - Using Imagick::getImageProperty:
  • Example#3645 - Using Imagick::getIteratorIndex:
  • Example#3646 - Imagick::getPixelIterator
  • Example#3647 - Imagick::getPixelRegionIterator example
  • Example#3648 - Imagick::haldClutImage
  • Example#3649 - Imagick::identifyFormat
  • Example#3650 - Example Result Format
  • Example#3651 - Imagick::implodeImage
  • Example#3652 - Imagick::importImagePixels example
  • Example#3653 - Imagick::levelImage
  • Example#3654 - Imagick::linearStretchImage
  • Example#3655 - Imagick::magnifyImage
  • Example#3656 - Imagick::medianFilterImage
  • Example#3657 - Imagick::mergeImageLayers
  • Example#3658 - Imagick::modulateImage
  • Example#3659 - Convolve Imagick::morphology
  • Example#3660 - Correlate Imagick::morphology
  • Example#3661 - Erode Imagick::morphology
  • Example#3662 - Erode Intensity Imagick::morphology
  • Example#3663 - Dilate Imagick::morphology
  • Example#3664 - Dilate intensity Imagick::morphology
  • Example#3665 - Distance with Chebyshev kernel Imagick::morphology
  • Example#3666 - Distance with Manhattan kernel Imagick::morphology
  • Example#3667 - Distance with ocatagonal kernel Imagick::morphology
  • Example#3668 - Distance with Euclidean kernel Imagick::morphology
  • Example#3669 - Edge Imagick::morphology
  • Example#3670 - Open Imagick::morphology
  • Example#3671 - Open intensity Imagick::morphology
  • Example#3672 - Close Imagick::morphology
  • Example#3673 - Close Intensity Imagick::morphology
  • Example#3674 - Smooth Imagick::morphology
  • Example#3675 - Edge in Imagick::morphology
  • Example#3676 - Edge out Imagick::morphology
  • Example#3677 - The 'TopHat' method, or more specifically 'White Top Hat', returns the pixels that were removed by a Opening of the shape, that is the pixels that were removed to round off the points, and the connecting bridged between shapes. Imagick::morphology
  • Example#3678 - The 'BottomHat' method, also known as 'Black TopHat' is the pixels that a Closing of the shape adds to the image. That is the the pixels that were used to fill in the 'holes', 'gaps', and 'bridges'. Imagick::morphology
  • Example#3679 - Hit and Miss Imagick::morphology
  • Example#3680 - Thinning Imagick::morphology
  • Example#3681 - Thicken Imagick::morphology
  • Example#3682 - Thick to generate a convex hull Imagick::morphology
  • Example#3683 - Iterative morphology Imagick::morphology
  • Example#3684 - Helper functon to get an image silhouette Imagick::morphology
  • Example#3685 - Imagick::motionBlurImage
  • Example#3686 - Imagick::negateImage
  • Example#3687 - Using Imagick::newImage:
  • Example#3688 - Imagick::newPseudoImage
  • Example#3689 - Imagick::normalizeImage
  • Example#3690 - Imagick::oilPaintImage
  • Example#3691 - Using Imagick::optimizeImageLayers
  • Example#3692 - Imagick::orderedPosterizeImage
  • Example#3693 - Using Imagick::pingImageBlob
  • Example#3694 - Using Imagick::pingImageFile
  • Example#3695 - A Imagick::polaroidImage example
  • Example#3696 - Imagick::posterizeImage
  • Example#3697 - Imagick::quantizeImage
  • Example#3698 - Using Imagick::queryFontMetrics:
  • Example#3699 - Imagick::queryFonts
  • Example#3700 - Imagick::queryFormats
  • Example#3701 - Imagick::radialBlurImage
  • Example#3702 - Imagick::raiseImage
  • Example#3703 - Imagick::randomThresholdImage
  • Example#3704 - Imagick::readImageBlob
  • Example#3705 - Imagick::recolorImage
  • Example#3706 - Imagick::reduceNoiseImage
  • Example#3707 - Imagick::resampleImage
  • Example#3708 - Imagick::resizeImage
  • Example#3709 - Imagick::rollImage
  • Example#3710 - Imagick::rotateImage
  • Example#3711 - Imagick::rotationalBlurImage
  • Example#3712 - Using Imagick::roundCorners:
  • Example#3713 - Imagick::scaleImage
  • Example#3714 - Imagick::segmentImage
  • Example#3715 - Imagick::selectiveBlurImage
  • Example#3716 - Imagick::separateImageChannel
  • Example#3717 - Imagick::sepiaToneImage
  • Example#3718 - Imagick::setCompressionQuality
  • Example#3719 - A Imagick::setFont example
  • Example#3720 - A Imagick::setImage example
  • Example#3721 - Imagick::setImageArtifact
  • Example#3722 - Imagick::setImageBias
  • Example#3723 - Imagick::setImageClipMask
  • Example#3724 - Imagick::setImageCompressionQuality
  • Example#3725 - Modify animated Gif with Imagick::setImageDelay
  • Example#3726 - Basic Imagick::setImageIterations usage
  • Example#3727 - A Imagick::setImageOpacity example
  • Example#3728 - Imagick::setImageOrientation
  • Example#3729 - Using Imagick::setImageProperty:
  • Example#3730 - Imagick::setImageResolution
  • Example#3731 - Modify animated Gif with Imagick::setImageTicksPerSecond
  • Example#3732 - Using Imagick::setIteratorIndex:
  • Example#3733 - Attempt to reach '$extent' sizeImagick::setOption
  • Example#3734 - Imagick::setOption
  • Example#3735 - Imagick::setOption
  • Example#3736 - A Imagick::setPointSize example
  • Example#3737 - Imagick::setProgressMonitor
  • Example#3738 - Imagick::setSamplingFactors
  • Example#3739 - Imagick::shadeImage
  • Example#3740 - Imagick::shadowImage
  • Example#3741 - Imagick::sharpenImage
  • Example#3742 - Imagick::shaveImage
  • Example#3743 - Imagick::shearImage
  • Example#3744 - Create a gradient image using Imagick::sigmoidalContrastImage suitable for blending two images together smoothly, with the blending defined by $contrast and $the midpoint
  • Example#3745 - Imagick::sketchImage
  • Example#3746 - Imagick::smushImages
  • Example#3747 - Imagick::solarizeImage
  • Example#3748 - SPARSECOLORMETHOD_BARYCENTRIC Imagick::sparseColorImage
  • Example#3749 - SPARSECOLORMETHOD_BILINEAR Imagick::sparseColorImage
  • Example#3750 - SPARSECOLORMETHOD_SPEPARDS Imagick::sparseColorImage
  • Example#3751 - SPARSECOLORMETHOD_VORONOI Imagick::sparseColorImage
  • Example#3752 - SPARSECOLORMETHOD_BARYCENTRIC Imagick::sparseColorImage
  • Example#3753 - createGradientImage is used by other examples. Imagick::sparseColorImage
  • Example#3754 - Imagick::spliceImage
  • Example#3755 - Imagick::spreadImage
  • Example#3756 - Imagick::statisticImage
  • Example#3757 - Imagick::subImageMatch
  • Example#3758 - Imagick::swirlImage
  • Example#3759 - Imagick::textureImage
  • Example#3760 - Imagick::thresholdImage
  • Example#3761 - Imagick::thumbnailImage
  • Example#3762 - Imagick::tintImage
  • Example#3763 - Using Imagick::transformImage:
  • Example#3764 - Imagick::transformImageColorspace example
  • Example#3765 - Imagick::transformImageColorspace
  • Example#3766 - Imagick::transparentPaintImage
  • Example#3767 - Imagick::transposeImage
  • Example#3768 - Imagick::transverseImage
  • Example#3769 - Using Imagick::trimImage:
  • Example#3770 - Imagick::uniqueImageColors
  • Example#3771 - Imagick::unsharpMaskImage
  • Example#3772 - Imagick::vignetteImage
  • Example#3773 - WaveImage can be quite slow Imagick::waveImage
  • Example#3774 - Imagick::whiteThresholdImage
  • Example#3775 - ImagickDraw::affine
  • Example#3776 - ImagickDraw::arc
  • Example#3777 - ImagickDraw::bezier
  • Example#3778 - ImagickDraw::circle
  • Example#3779 - ImagickDraw::composite
  • Example#3780 - ImagickDraw::ellipse
  • Example#3781 - ImagickDraw::line
  • Example#3782 - ImagickDraw::matte
  • Example#3783 - ImagickDraw::pathCurveToQuadraticBezierAbsolute
  • Example#3784 - ImagickDraw::pathCurveToQuadraticBezierSmoothAbsolute
  • Example#3785 - ImagickDraw::pathCurveToQuadraticBezierSmoothRelative
  • Example#3786 - ImagickDraw::pathStart
  • Example#3787 - ImagickDraw::point
  • Example#3788 - ImagickDraw::polygon
  • Example#3789 - ImagickDraw::polyline
  • Example#3790 - ImagickDraw::popDefs
  • Example#3791 - ImagickDraw::push
  • Example#3792 - ImagickDraw::pushPattern
  • Example#3793 - ImagickDraw::rectangle
  • Example#3794 - ImagickDraw::rotate
  • Example#3795 - ImagickDraw::roundRectangle
  • Example#3796 - ImagickDraw::scale
  • Example#3797 - ImagickDraw::setClipPath
  • Example#3798 - ImagickDraw::setClipRule
  • Example#3799 - ImagickDraw::setClipUnits
  • Example#3800 - ImagickDraw::setFillAlpha
  • Example#3801 - ImagickDraw::setFillColor
  • Example#3802 - ImagickDraw::setFillOpacity
  • Example#3803 - ImagickDraw::setFillRule
  • Example#3804 - ImagickDraw::setFont
  • Example#3805 - ImagickDraw::setFontFamily
  • Example#3806 - ImagickDraw::setFontSize
  • Example#3807 - ImagickDraw::setFontStretch
  • Example#3808 - ImagickDraw::setFontStyle
  • Example#3809 - ImagickDraw::setFontWeight
  • Example#3810 - ImagickDraw::setGravity
  • Example#3811 - ImagickDraw::setStrokeAlpha
  • Example#3812 - ImagickDraw::setStrokeAntialias
  • Example#3813 - ImagickDraw::setStrokeColor
  • Example#3814 - ImagickDraw::setStrokeDashArray
  • Example#3815 - ImagickDraw::setStrokeDashOffset
  • Example#3816 - ImagickDraw::setStrokeLineCap
  • Example#3817 - ImagickDraw::setStrokeLineJoin
  • Example#3818 - ImagickDraw::setStrokeMiterLimit
  • Example#3819 - ImagickDraw::setStrokeOpacity
  • Example#3820 - ImagickDraw::setStrokeWidth
  • Example#3821 - ImagickDraw::setTextAlignment
  • Example#3822 - ImagickDraw::setTextAntialias
  • Example#3823 - ImagickDraw::setTextDecoration
  • Example#3824 - ImagickDraw::setTextUnderColor
  • Example#3825 - ImagickDraw::setVectorGraphics
  • Example#3826 - ImagickDraw::setViewBox
  • Example#3827 - ImagickDraw::skewX
  • Example#3828 - ImagickDraw::skewY
  • Example#3829 - ImagickDraw::translate
  • Example#3830 - ImagickPixel::construct
  • Example#3831 - Basic Imagick::getColor usage
  • Example#3832 - Basic Imagick::getColorAsString usage
  • Example#3833 - ImagickPixel getColorCount
  • Example#3834 - Basic Imagick::getColorValue usage
  • Example#3835 - ImagickPixel::getColorValueQuantum
  • Example#3836 - Basic Imagick::getHSL example
  • Example#3837 - ImagickPixel::isSimilar
  • Example#3838 - ImagickPixel::setColor
  • Example#3839 - Basic Imagick::setColorValue usage
  • Example#3840 - ImagickPixel::setColorValueQuantum
  • Example#3841 - Use ImagickPixel::setHSL to modify a color
  • Example#3842 - ImagickPixelIterator::clear
  • Example#3843 - ImagickPixelIterator::construct
  • Example#3844 - ImagickPixelIterator::getNextIteratorRow
  • Example#3845 - ImagickPixelIterator::resetIterator
  • Example#3846 - ImagickPixelIterator::setIteratorRow
  • Example#3847 - ImagickKernel::addKernel
  • Example#3848 - ImagickKernel::addUnityKernel
  • Example#3849 - ImagickKernel::addUnityKernel
  • Example#3850 - ImagickKernel::fromBuiltin
  • Example#3851 - ImagickKernel::fromMatrix
  • Example#3852 - ImagickKernel::getMatrix
  • Example#3853 - ImagickKernel::scale
  • Example#3854 - ImagickKernel::separate
  • Example#3855 - imap_append example
  • Example#3856 - imap_check example
  • Example#3857 - imap_createmailbox example
  • Example#3858 - imap_delete example
  • Example#3859 - imap_fetch_overview example
  • Example#3860 - imap_gc example
  • Example#3861 - imap_get_quota example
  • Example#3862 - imap_get_quota 4.3 or greater example
  • Example#3863 - imap_get_quotaroot example
  • Example#3864 - imap_getacl example
  • Example#3865 - imap_getmailboxes example
  • Example#3866 - imap_list example
  • Example#3867 - imap_mail_compose example
  • Example#3868 - imap_mailboxmsginfo example
  • Example#3869 - imap_mime_header_decode example
  • Example#3870 - Different use of imap_open
  • Example#3871 - imap_open example
  • Example#3872 - imap_ping Example
  • Example#3873 - imap_reopen example
  • Example#3874 - imap_rfc822_parse_adrlist example
  • Example#3875 - imap_rfc822_write_address example
  • Example#3876 - imap_search example
  • Example#3877 - imap_set_quota example
  • Example#3878 - imap_setflag_full example
  • Example#3879 - imap_status example
  • Example#3880 - imap_thread Example
  • Example#3881 - imap_timeout example
  • Example#3882 - 해시를 계산하고 사용자를 추가합니다.
  • Example#3883 - 메일 보내기.
  • Example#3884 - 추가 헤더와 함께 메일 보내기
  • Example#3885 - 추가적인 명령줄 인수와 함께 메일 전송하기.
  • Example#3886 - HTML 이메일 보내기
  • Example#3887 - mailparse_determine_best_xfer_encoding example
  • Example#3888 - mailparse_rfc822_parse_addresses example
  • Example#3889 - mailparse_stream_encode example
  • Example#3890 - mailparse_uudecode_all example
  • Example#3891 - bcadd 예제
  • Example#3892 - bccomp 예제
  • Example#3893 - bcdiv 예제
  • Example#3894 - bcmod 예제
  • Example#3895 - bcmul 예제
  • Example#3896 - bcpow 예제
  • Example#3897 - bcscale 예제
  • Example#3898 - bcsqrt 예제
  • Example#3899 - bcsub 예제
  • Example#3900 - Factorial function using GMP
  • Example#3901 - gmp_abs example
  • Example#3902 - gmp_add example
  • Example#3903 - gmp_and example
  • Example#3904 - gmp_clrbit example
  • Example#3905 - gmp_cmp example
  • Example#3906 - gmp_com example
  • Example#3907 - gmp_div_q example
  • Example#3908 - Division of GMP numbers
  • Example#3909 - gmp_div_r example
  • Example#3910 - gmp_divexact example
  • Example#3911 - gmp_export example
  • Example#3912 - gmp_fact example
  • Example#3913 - gmp_gcd example
  • Example#3914 - Solving a linear Diophantine equation
  • Example#3915 - gmp_hamdist example
  • Example#3916 - gmp_import example
  • Example#3917 - Creating GMP number
  • Example#3918 - gmp_intval example
  • Example#3919 - gmp_invert example
  • Example#3920 - gmp_jacobi example
  • Example#3921 - gmp_legendre example
  • Example#3922 - gmp_mod example
  • Example#3923 - gmp_mul example
  • Example#3924 - gmp_neg example
  • Example#3925 - gmp_nextprime example
  • Example#3926 - gmp_or example
  • Example#3927 - gmp_perfect_square example
  • Example#3928 - gmp_popcount example
  • Example#3929 - gmp_pow example
  • Example#3930 - gmp_powm example
  • Example#3931 - gmp_prob_prime example
  • Example#3932 - gmp_random_bits example
  • Example#3933 - gmp_random_range example
  • Example#3934 - gmp_random_seed example
  • Example#3935 - gmp_random example
  • Example#3936 - gmp_scan0 example
  • Example#3937 - gmp_scan1 example
  • Example#3938 - gmp_setbit example - 0 index
  • Example#3939 - gmp_setbit example - 1 index
  • Example#3940 - gmp_setbit example - clearing a bit
  • Example#3941 - gmp_sign example
  • Example#3942 - gmp_sqrt example
  • Example#3943 - gmp_sqrtrem example
  • Example#3944 - Converting a GMP number to a string
  • Example#3945 - gmp_sub example
  • Example#3946 - gmp_testbit example
  • Example#3947 - gmp_xor example
  • Example#3948 - Using Lapack::eigenValues:
  • Example#3949 - Using Lapack::leastSquaresByFactorisation:
  • Example#3950 - Using Lapack::leastSquaresBySVD:
  • Example#3951 - Using Lapack::pseudoInverse:
  • Example#3952 - Using Lapack::singularValues:
  • Example#3953 - Using Lapack::singularValues:
  • Example#3954 - abs 예제
  • Example#3955 - base_convert 예제
  • Example#3956 - bindec 예제
  • Example#3957 - ceil 예제
  • Example#3958 - cos 예제
  • Example#3959 - decbin 예제
  • Example#3960 - dechex 예제
  • Example#3961 - decoct 예제
  • Example#3962 - deg2rad 예제
  • Example#3963 - exp 예제
  • Example#3964 - floor 예제
  • Example#3965 - fmod 사용하기
  • Example#3966 - hexdec 예제
  • Example#3967 - intdiv example
  • Example#3968 - is_nan 예제
  • Example#3969 - max의 사용 예제
  • Example#3970 - min의 사용 예제
  • Example#3971 - mt_rand 예제
  • Example#3972 - mt_srand 예제
  • Example#3973 - octdec 예제
  • Example#3974 - pi 예제
  • Example#3975 - pow의 몇몇 예제
  • Example#3976 - rad2deg 예제
  • Example#3977 - rand 예제
  • Example#3978 - round 예제
  • Example#3979 - sin 예제
  • Example#3980 - sqrt 예제
  • Example#3981 - srand 예제
  • Example#3982 - tan 예제
  • Example#3983 - Evaluating a FDF document
  • Example#3984 - Adding JavaScript code to a FDF
  • Example#3985 - Populating a PDF document
  • Example#3986 - Storing an uploaded file
  • Example#3987 - Detecting all fieldnames in a FDF
  • Example#3988 - Accessing the form data
  • Example#3989 - Accessing the form data
  • Example#3990 - Retrieving FDF as a string
  • Example#3991 - Passing FDF data to a second form
  • Example#3992 - gnupg clearsign example (procedural)
  • Example#3993 - gnupg clearsign example (OO)
  • Example#3994 - keylistiterator
  • Example#3995 - Procedural gnupg_adddecryptkey example
  • Example#3996 - OO gnupg_adddecryptkey example
  • Example#3997 - Procedural gnupg_addencryptkey example
  • Example#3998 - OO gnupg_addencryptkey example
  • Example#3999 - Procedural gnupg_addsignkey example
  • Example#4000 - OO gnupg_addsignkey example
  • Example#4001 - Procedural gnupg_cleardecryptkeys example
  • Example#4002 - OO gnupg_cleardecryptkeys example
  • Example#4003 - Procedural gnupg_clearencryptkeys example
  • Example#4004 - OO gnupg_clearencryptkeys example
  • Example#4005 - Procedural gnupg_clearsignkeys example
  • Example#4006 - OO gnupg_clearsignkeys example
  • Example#4007 - Procedural gnupg_decrypt example
  • Example#4008 - OO gnupg_encrypt example
  • Example#4009 - Procedural gnupg_decryptverify example
  • Example#4010 - OO gnupg_decryptverify example
  • Example#4011 - Procedural gnupg_encrypt example
  • Example#4012 - OO gnupg_encrypt example
  • Example#4013 - Procedural gnupg_encryptsign example
  • Example#4014 - OO gnupg_encryptsign example
  • Example#4015 - Procedural gnupg_export example
  • Example#4016 - OO gnupg_export example
  • Example#4017 - Procedural gnupg_geterror example
  • Example#4018 - OO gnupg_geterror example
  • Example#4019 - Procedural gnupg_getprotocol example
  • Example#4020 - OO gnupg_getprotocol example
  • Example#4021 - Procedural gnupg_import example
  • Example#4022 - OO gnupg_import example
  • Example#4023 - Procedural gnupg_init example
  • Example#4024 - OO gnupg initializer example
  • Example#4025 - Procedural gnupg_keyinfo example
  • Example#4026 - OO gnupg_keyinfo example
  • Example#4027 - Procedural gnupg_setarmor example
  • Example#4028 - OO gnupg_setarmor example
  • Example#4029 - Procedural gnupg_seterrormode example
  • Example#4030 - OO gnupg_seterrormode example
  • Example#4031 - Procedural gnupg_setsignmode example
  • Example#4032 - OO gnupg_setsignmode example
  • Example#4033 - Procedural gnupg_sign example
  • Example#4034 - OO gnupg_sign example
  • Example#4035 - Procedural gnupg_verify example
  • Example#4036 - OO gnupg_verify example
  • Example#4037 - Fancy "Hello world"
  • Example#4038 - swfaction example
  • Example#4039 - swfaction example
  • Example#4040 - swfaction example
  • Example#4041 - swfsprite example
  • Example#4042 - ming_useswfversion example
  • Example#4043 - Importing a DBL file
  • Example#4044 - Using an alpha mask
  • Example#4045 - Usual interactions with buttons
  • Example#4046 - Drag example
  • Example#4047 - swfdisplayitem::multcolor example
  • Example#4048 - swfdisplayitem::rotateto example
  • Example#4049 - swfdisplayitem::setname example
  • Example#4050 - swfgradient example
  • Example#4051 - swfmorph example
  • Example#4052 - Displaying your $movie in a browser
  • Example#4053 - Streaming example
  • Example#4054 - SWFShape::addFill example
  • Example#4055 - swfshape example
  • Example#4056 - swfshape::setline example
  • Example#4057 - swftext example
  • Example#4058 - Hello World example from PDFlib distribution for PHP 4
  • Example#4059 - Hello World example from PDFlib distribution for PHP 5
  • Example#4060 - Creating and using a pattern
  • Example#4061 - Creating and using a template
  • Example#4062 - Hyphennate a text
  • Example#4063 - Drawing a rectangle
  • Example#4064 - Creating and using a spot color
  • Example#4065 - Rotation of the coordinate system
  • Example#4066 - Placing text at a given position
  • Example#4067 - Drawing a dashed line
  • Example#4068 - Translation of the coordinate system
  • Example#4069 - Basic RPMReader Example
  • Example#4070 - rpm_close example
  • Example#4071 - rpm_get_tag example
  • Example#4072 - rpm_is_valid example
  • Example#4073 - rpm_open example
  • Example#4074 - rpm_version example
  • Example#4075 - Incorrect requests
  • Example#4076 - Calling request from a request callback
  • Example#4077 - Calling request from a request callback
  • Example#4078 - Using eio with libevent
  • Example#4079 - Cancelling a request
  • Example#4080 - Calling eio_chmod
  • Example#4081 - Making a custom request
  • Example#4082 - Grouping requests
  • Example#4083 - Using eio with libevent extension
  • Example#4084 - Using eio with event extension
  • Example#4085 - eio_cancel example
  • Example#4086 - eio_custom example
  • Example#4087 - eio_event_loop example
  • Example#4088 - eio_lstat example
  • Example#4089 - Using eio with libevent
  • Example#4090 - Grouping requests
  • Example#4091 - eio_grp example
  • Example#4092 - eio_link example
  • Example#4093 - eio_lstat example
  • Example#4094 - eio_mkdir example
  • Example#4095 - eio_mknod example
  • Example#4096 - eio_nreqs example
  • Example#4097 - eio_open example
  • Example#4098 - eio_poll example
  • Example#4099 - eio_read example
  • Example#4100 - eio_readdir example
  • Example#4101 - eio_readlink example
  • Example#4102 - eio_realpath example
  • Example#4103 - eio_rename example
  • Example#4104 - eio_rmdir example
  • Example#4105 - eio_stat example
  • Example#4106 - eio_statvfs example
  • Example#4107 - eio_symlink example
  • Example#4108 - Simple timers
  • Example#4109 - Periodic timer. Tick each 10.5 seconds
  • Example#4110 - Periodic timer. Use reschedule callback
  • Example#4111 - Periodic timer. Tick every 10.5 seconds starting at now
  • Example#4112 - Wait until STDIN is readable
  • Example#4113 - Use some async I/O to access a socket
  • Example#4114 - Embedding one loop into another
  • Example#4115 - Embedding loop created with kqueue backend into the default loop
  • Example#4116 - Handle SIGTERM signal
  • Example#4117 - Monitor changes of /var/log/messages
  • Example#4118 - Monotor changes of /var/log/messages. Avoid missing updates by means of one second delay
  • Example#4119 - Process status changes
  • Example#4120 - Using reschedule callback
  • Example#4121 - Embedding loop created with kqueue backend into the default loop
  • Example#4122 - Embedding one loop into another
  • Example#4123 - Embedding loop created with kqueue backend into the default loop
  • Example#4124 - Embedding loop created with kqueue backend into the default loop
  • Example#4125 - Periodic timer. Use reschedule callback
  • Example#4126 - Periodic timer. Tick every 10.5 seconds starting at now
  • Example#4127 - Hourly watcher
  • Example#4128 - Handle SIGTERM signal
  • Example#4129 - Monitor changes of /var/log/messages
  • Example#4130 - Monitor changes of /var/log/messages
  • Example#4131 - Simple timers
  • Example#4132 - Monotor changes of /var/log/messages. Avoid missing updates by means of one second delay
  • Example#4133 - Register an I/O watcher for some UDP socket but do not keep the event loop from running just because of that watcher.
  • Example#4134 - Expect Usage Example
  • Example#4135 - Another Expect Usage Example
  • Example#4136 - expect_expectl example
  • Example#4137 - expect_popen example
  • Example#4138 - polling STDIN using basic API
  • Example#4139 - polling STDIN using buffered event API
  • Example#4140 - Process Control Example
  • Example#4141 - pcntl_fork example
  • Example#4142 - pcntl_signal_dispatch example
  • Example#4143 - pcntl_signal example
  • Example#4144 - pcntl_sigprocmask example
  • Example#4145 - pcntl_sigwaitinfo example
  • Example#4146 - posix_access example
  • Example#4147 - posix_ctermid example
  • Example#4148 - posix_get_last_error example
  • Example#4149 - posix_getcwd example
  • Example#4150 - posix_getegid example
  • Example#4151 - posix_geteuid example
  • Example#4152 - posix_getgid example
  • Example#4153 - Example use of posix_getgrgid
  • Example#4154 - Example use of posix_getgrnam
  • Example#4155 - Example use of posix_getgroups
  • Example#4156 - Example use of posix_getlogin
  • Example#4157 - Example use of posix_getpgid
  • Example#4158 - Example use of posix_getpid
  • Example#4159 - Example use of posix_getppid
  • Example#4160 - Example use of posix_getpwnam
  • Example#4161 - Example use of posix_getpwuid
  • Example#4162 - Example use of posix_getrlimit
  • Example#4163 - Example use of posix_getsid
  • Example#4164 - Example use of posix_getuid
  • Example#4165 - A posix_mknod example
  • Example#4166 - posix_setegid example
  • Example#4167 - posix_setgid example
  • Example#4168 - posix_setuid example
  • Example#4169 - posix_strerror example
  • Example#4170 - Example use of posix_times
  • Example#4171 - Example use of posix_uname
  • Example#4172 - escapeshellarg example
  • Example#4173 - escapeshellcmd example
  • Example#4174 - An exec example
  • Example#4175 - Using proc_open to set the process priority to high
  • Example#4176 - A proc_open example
  • Example#4177 - A shell_exec example
  • Example#4178 - system example
  • Example#4179 - Fetch a chunk of the property table
  • Example#4180 - Counting the properties of an object
  • Example#4181 - Runtime inheritance
  • Example#4182 - Thread safe objects from closures
  • Example#4183 - Detecting fatal errors in Threads
  • Example#4184 - Detect the state of the referenced object
  • Example#4185 - Detect the state of the referenced object
  • Example#4186 - Detect the state of the referenced object
  • Example#4187 - Locking Object Properties
  • Example#4188 - Merging into the property table of a threaded object
  • Example#4189 - Notifications and Waiting
  • Example#4190 - Popping the last item from the property table of a threaded object
  • Example#4191 - Shifting the first item from the property table of a threaded object
  • Example#4192 - Synchronizing
  • Example#4193 - Locking the property table of a threaded object
  • Example#4194 - Notifications and Waiting
  • Example#4195 - Return the identity of the Thread or Process that created the referenced Thread
  • Example#4196 - Return the currently executing Thread
  • Example#4197 - Return the identity of the currently executing Thread
  • Example#4198 - Return the identity of the referenced Thread
  • Example#4199 - Execute in the global scope
  • Example#4200 - Detect the state of the referenced Thread
  • Example#4201 - Tell if the referenced Thread was started
  • Example#4202 - Join with the referenced Thread
  • Example#4203 - Kill the referenced Thread
  • Example#4204 - Starting Threads
  • Example#4205 - Returns the number of objects currently waiting to be executed by the referenced Worker
  • Example#4206 - Detect the state of a Worker
  • Example#4207 - Detect the state of a Worker
  • Example#4208 - Shutdown the referenced Worker
  • Example#4209 - Stacking objects for execution in Workers
  • Example#4210 - Removing objects from the stack of Workers
  • Example#4211 - protected method example: protected methods can only be executed by one Thread at a time.
  • Example#4212 - private method example: private methods may only be executed by the Threaded object during execution
  • Example#4213 - Creating Pools
  • Example#4214 - Creating Pools
  • Example#4215 - Submitting Tasks
  • Example#4216 - Submitting Tasks
  • Example#4217 - Mutex Creation and Destruction
  • Example#4218 - Mutex Creation and Destruction
  • Example#4219 - Mutex Locking and Unlocking
  • Example#4220 - Mutex Locking and Unlocking
  • Example#4221 - Mutex Locking and Unlocking
  • Example#4222 - Condition Broadcasting
  • Example#4223 - Condition Creation and Destruction
  • Example#4224 - Condition Creation and Destruction
  • Example#4225 - Condition Signalling
  • Example#4226 - Waiting for Conditions
  • Example#4227 -
  • Example#4228 - Shared Memory Operations Overview
  • Example#4229 - Closing shared memory block
  • Example#4230 - Deleting shared memory block
  • Example#4231 - Create a new shared memory block
  • Example#4232 - Reading shared memory block
  • Example#4233 - Getting the size of the shared memory block
  • Example#4234 - Writing to shared memory block
  • Example#4235 - SyncMutex::__construct named mutex with lock timeout example
  • Example#4236 - SyncMutex::__construct unnamed mutex example
  • Example#4237 - SyncMutex::lock example
  • Example#4238 - SyncMutex::unlock example
  • Example#4239 - SyncSemaphore::__construct example
  • Example#4240 - SyncSemaphore::lock example
  • Example#4241 - SyncSemaphore::unlock example
  • Example#4242 - SyncEvent::__construct example
  • Example#4243 - SyncEvent::fire example
  • Example#4244 - SyncEvent::reset example
  • Example#4245 - SyncEvent::wait example
  • Example#4246 - SyncReaderWriter::__construct example
  • Example#4247 - SyncReaderWriter::readlock example
  • Example#4248 - SyncReaderWriter::readunlock example
  • Example#4249 - SyncReaderWriter::writelock example
  • Example#4250 - SyncReaderWriter::writeunlock example
  • Example#4251 - A geoip_asnum_by_name example
  • Example#4252 - A geoip_continent_code_by_name example
  • Example#4253 - A geoip_country_code_by_name example
  • Example#4254 - A geoip_country_code3_by_name example
  • Example#4255 - A geoip_country_name_by_name example
  • Example#4256 - A geoip_database_info example
  • Example#4257 - A geoip_db_avail example
  • Example#4258 - A geoip_db_filename example
  • Example#4259 - A geoip_db_get_all_info example
  • Example#4260 - A geoip_db_get_all_info example
  • Example#4261 - A geoip_domain_by_name example
  • Example#4262 - A geoip_id_by_name example
  • Example#4263 - A geoip_isp_by_name example
  • Example#4264 - A geoip_netspeedcell_by_name example
  • Example#4265 - A geoip_org_by_name example
  • Example#4266 - A geoip_record_by_name example
  • Example#4267 - A geoip_region_by_name example
  • Example#4268 - A geoip_region_name_by_code example using region code for US/Canada
  • Example#4269 - A geoip_region_name_by_code example using FIPS codes
  • Example#4270 - A geoip_setup_custom_directory example
  • Example#4271 - A geoip_time_zone_by_country_and_region example using region code for US/Canada
  • Example#4272 - A geoip_time_zone_by_country_and_region example using FIPS codes
  • Example#4273 - xor.data file
  • Example#4274 - Simple train
  • Example#4275 - Simple test
  • Example#4276 - fann_create_train_from_callback example
  • Example#4277 - fann_read_train_from_file example
  • Example#4278 - JsonSerializable::jsonSerialize example returning an array
  • Example#4279 - JsonSerializable::jsonSerialize example returning an associative array
  • Example#4280 - JsonSerializable::jsonSerialize example returning an integer
  • Example#4281 - JsonSerializable::jsonSerialize example returning a string
  • Example#4282 - json_decode 예제
  • Example#4283 - 또다른 예제
  • Example#4284 - json_decode를 사용하는 일반적인 실수
  • Example#4285 - json_encode 예제
  • Example#4286 - json_last_error 예제
  • Example#4287 - Judy array example
  • Example#4288 - Lua::assignexample
  • Example#4289 - Lua::callexample
  • Example#4290 - Lua::evalexample
  • Example#4291 - Lua::registerCallbackexample
  • Example#4292 - LuaClosure::__invokeexample
  • Example#4293 - constant 예제
  • Example#4294 - Defining Constants
  • Example#4295 - Checking Constants
  • Example#4296 - eval example - simple text merge
  • Example#4297 - exit example
  • Example#4298 - exit status example
  • Example#4299 - Shutdown functions and destructors run regardless
  • Example#4300 - Listing all information about the users browser
  • Example#4301 - A __halt_compiler example
  • Example#4302 - highlight_string example
  • Example#4303 - A ignore_user_abort example
  • Example#4304 - pack example
  • Example#4305 - php_strip_whitespace example
  • Example#4306 - sleep example
  • Example#4307 - A sys_getloadavg example
  • Example#4308 - time_nanosleep example
  • Example#4309 - A time_sleep_until example
  • Example#4310 - uniqid Example
  • Example#4311 - unpack example
  • Example#4312 - unpack example with a repeater
  • Example#4313 - unpack example with unnamed keys
  • Example#4314 - usleep example
  • Example#4315 - parsekit_compile_file example
  • Example#4316 - parsekit_compile_string example
  • Example#4317 - parsekit_func_arginfo example
  • Example#4318 - SplDoublyLinkedList::__construct example
  • Example#4319 - SplStack::__construct example
  • Example#4320 - SplQueue::__construct example
  • Example#4321 - Efficiently handling tasks with SplQueue
  • Example#4322 - SplFixedArray usage example
  • Example#4323 - SplFixedArray::__construct example
  • Example#4324 - SplFixedArray::count example
  • Example#4325 - SplFixedArray::fromArray example
  • Example#4326 - SplFixedArray::getSize example
  • Example#4327 - SplFixedArray::setSize example
  • Example#4328 - SplFixedArray::toArray example
  • Example#4329 - SplObjectStorage as a set
  • Example#4330 - SplObjectStorage as a map
  • Example#4331 - SplObjectStorage::addAll example
  • Example#4332 - SplObjectStorage::attach example
  • Example#4333 - SplObjectStorage::contains example
  • Example#4334 - SplObjectStorage::count example
  • Example#4335 - SplObjectStorage::current example
  • Example#4336 - SplObjectStorage::detach example
  • Example#4337 - SplObjectStorage::getHash example
  • Example#4338 - SplObjectStorage::getInfo example
  • Example#4339 - SplObjectStorage::key example
  • Example#4340 - SplObjectStorage::next example
  • Example#4341 - SplObjectStorage::offsetExists example
  • Example#4342 - SplObjectStorage::offsetGet example
  • Example#4343 - SplObjectStorage::offsetSet example
  • Example#4344 - SplObjectStorage::offsetUnset example
  • Example#4345 - SplObjectStorage::removeAll example
  • Example#4346 - SplObjectStorage::removeAllExcept example
  • Example#4347 - SplObjectStorage::rewind example
  • Example#4348 - SplObjectStorage::serialize example
  • Example#4349 - SplObjectStorage::setInfo example
  • Example#4350 - SplObjectStorage::unserialize example
  • Example#4351 - SplObjectStorage::valid example
  • Example#4352 - AppendIterator::append example
  • Example#4353 - Iterating AppendIterator with foreach
  • Example#4354 - Iterating AppendIterator with the AppendIterator API
  • Example#4355 - AppendIterator::getInnerIterator example
  • Example#4356 - AppendIterator.getIteratorIndex basic example
  • Example#4357 - AppendIterator::key basic example
  • Example#4358 - ArrayIterator::current example
  • Example#4359 - ArrayIterator::key example
  • Example#4360 - ArrayIterator::next example
  • Example#4361 - ArrayIterator::rewind example
  • Example#4362 - ArrayIterator::valid example
  • Example#4363 - CachingIterator::getCache example
  • Example#4364 - Available callback arguments
  • Example#4365 - Callback basic examples
  • Example#4366 - A DirectoryIterator::__construct example
  • Example#4367 - A DirectoryIterator::current example
  • Example#4368 - A DirectoryIterator::getATime example
  • Example#4369 - A DirectoryIterator::getBasename example
  • Example#4370 - DirectoryIterator::getCTime example
  • Example#4371 - DirectoryIterator::getExtension example
  • Example#4372 -
  • Example#4373 - A DirectoryIterator::getFilename example
  • Example#4374 - DirectoryIterator::getGroup example
  • Example#4375 - DirectoryIterator::getInode example
  • Example#4376 - A DirectoryIterator::getMTime example
  • Example#4377 - DirectoryIterator::getOwner example
  • Example#4378 - DirectoryIterator::getPath example
  • Example#4379 - DirectoryIterator::getPathname example
  • Example#4380 - DirectoryIterator::getPerms example
  • Example#4381 - DirectoryIterator::getSize example
  • Example#4382 - DirectoryIterator::getType example
  • Example#4383 - DirectoryIterator::isDir example
  • Example#4384 - A DirectoryIterator::isDot example
  • Example#4385 - DirectoryIterator::isExecutable example
  • Example#4386 - DirectoryIterator::isFile example
  • Example#4387 - A DirectoryIterator::isLink example
  • Example#4388 - DirectoryIterator::isReadable example
  • Example#4389 - DirectoryIterator::isWritable example
  • Example#4390 - A DirectoryIterator::key example
  • Example#4391 - DirectoryIterator::next example
  • Example#4392 - DirectoryIterator::rewind example
  • Example#4393 - DirectoryIterator::seek example
  • Example#4394 - A DirectoryIterator::__toString example
  • Example#4395 - A DirectoryIterator::valid example
  • Example#4396 - FilesystemIterator::__construct example
  • Example#4397 - FilesystemIterator::current example
  • Example#4398 - FilesystemIterator::key example
  • Example#4399 - FilesystemIterator::next example
  • Example#4400 - FilesystemIterator::rewind example
  • Example#4401 - FilesystemIterator::key example
  • Example#4402 - FilterIterator::accept example
  • Example#4403 - GlobIterator example
  • Example#4404 - GlobIterator::count example
  • Example#4405 - InfiniteIterator::__construct example
  • Example#4406 - LimitIterator usage example
  • Example#4407 - LimitIterator::__construct example
  • Example#4408 - LimitIterator::getPosition example
  • Example#4409 - Iterating a MultipleIterator
  • Example#4410 - NoRewindIterator::__construct example
  • Example#4411 - NoRewindIterator::rewind example
  • Example#4412 - RecursiveArrayIterator::getChildren example
  • Example#4413 - RecursiveArrayIterator::hasChildren example
  • Example#4414 - Available callback arguments
  • Example#4415 - Recursive callback basic example
  • Example#4416 - RecursiveCallbackFilterIterator::hasChildren basic usage
  • Example#4417 - RecursiveDirectoryIterator example
  • Example#4418 - Basic RecursiveFilterIterator example
  • Example#4419 - RecursiveFilterIterator example
  • Example#4420 - Iterating a RecursiveIteratorIterator
  • Example#4421 - RecursiveRegexIterator::__construct example
  • Example#4422 - RecursiveRegexIterator::getChildren example
  • Example#4423 - RecursiveRegexIterator::hasChildren example
  • Example#4424 - RegexIterator::accept example
  • Example#4425 - RegexIterator::__construct example
  • Example#4426 - RegexIterator::getFlags example
  • Example#4427 - RegexIterator::getMode example
  • Example#4428 - RegexIterator::getPregFlags example
  • Example#4429 - RegexIterator::setFlags example
  • Example#4430 - RegexIterator::setMode example
  • Example#4431 - RegexIterator::setPregFlags example
  • Example#4432 - Countable::count example
  • Example#4433 - Basic usage
  • Example#4434 - SeekableIterator::seek example
  • Example#4435 - class_implements example
  • Example#4436 - class_parents example
  • Example#4437 - class_uses example
  • Example#4438 - iterator_apply example
  • Example#4439 - iterator_count example
  • Example#4440 - iterator_count modifies position
  • Example#4441 - iterator_count in foreach loops
  • Example#4442 - iterator_to_array example
  • Example#4443 - spl_autoload_register as a replacement for an __autoload function
  • Example#4444 - spl_autoload_register example where the class is not loaded
  • Example#4445 - spl_classes example
  • Example#4446 - A spl_object_hash example
  • Example#4447 - SplFileInfo::__construct example
  • Example#4448 - SplFileInfo::getBasename example
  • Example#4449 - SplFileInfo::getCTime example
  • Example#4450 - SplFileInfo::getExtension example
  • Example#4451 -
  • Example#4452 - SplFileInfo::getFilename example
  • Example#4453 - SplFileInfo::getGroup example
  • Example#4454 - SplFileInfo::getLinkTarget example
  • Example#4455 - SplFileInfo::getOwner example
  • Example#4456 - SplFileInfo::getPath example
  • Example#4457 - SplFileInfo::getPathInfo example
  • Example#4458 - SplFileInfo::getPathname example
  • Example#4459 - SplFileInfo::getPerms example
  • Example#4460 - SplFileInfo::getRealPath example
  • Example#4461 - SplFileInfo::getType example
  • Example#4462 - SplFileInfo::isDir example
  • Example#4463 - SplFileInfo::isExecutable example
  • Example#4464 - SplFileInfo::isFile example
  • Example#4465 - SplFileInfo::isLink example
  • Example#4466 - SplFileInfo::isReadable example
  • Example#4467 - SplFileInfo::openFile example
  • Example#4468 - SplFileInfo::setFileClass example
  • Example#4469 - SplFileInfo::setFileClass example
  • Example#4470 - SplFileInfo::__toString example
  • Example#4471 - SplFileObject::__construct example
  • Example#4472 - SplFileObject::current example
  • Example#4473 - SplFileObject::eof example
  • Example#4474 - SplFileObject::fflush example
  • Example#4475 - SplFileObject::fgetc example
  • Example#4476 - SplFileObject::fgetcsv example
  • Example#4477 - SplFileObject::READ_CSV example
  • Example#4478 - SplFileObject::fgets example
  • Example#4479 - SplFileObject::fgetss example
  • Example#4480 - SplFileObject::flock example
  • Example#4481 - SplFileObject::fpassthru example
  • Example#4482 - SplFileObject::fputcsv example
  • Example#4483 - SplFileObject::fread example
  • Example#4484 - SplFileObject::fscanf example
  • Example#4485 - SplFileObject::fseek example
  • Example#4486 - SplFileObject::fstat example
  • Example#4487 - SplFileObject::ftell example
  • Example#4488 - SplFileObject::ftruncate example
  • Example#4489 - SplFileObject::fwrite example
  • Example#4490 - SplFileObject::getCsvControl example
  • Example#4491 - SplFileObject::getFlags example
  • Example#4492 - SplFileObject::getMaxLineLen example
  • Example#4493 - SplFileObject::key example
  • Example#4494 - SplFileObject::key example with SplFileObject::setMaxLineLen
  • Example#4495 - SplFileObject::next example
  • Example#4496 - SplFileObject::rewind example
  • Example#4497 - SplFileObject::seek example
  • Example#4498 - SplFileObject::setCsvControl example
  • Example#4499 - SplFileObject::setFlags example
  • Example#4500 - SplFileObject::setMaxLineLen example
  • Example#4501 - SplFileObject::valid example
  • Example#4502 - SplTempFileObject example
  • Example#4503 - ArrayObject::append example
  • Example#4504 - ArrayObject::asort example
  • Example#4505 - ArrayObject::__construct example
  • Example#4506 - ArrayObject::count example
  • Example#4507 - ArrayObject::exchangeArray example
  • Example#4508 - ArrayObject::getArrayCopy example
  • Example#4509 - ArrayObject::getFlags example
  • Example#4510 - ArrayObject::getIterator example
  • Example#4511 - ArrayObject::getIteratorClass example
  • Example#4512 - ArrayObject::ksort example
  • Example#4513 - ArrayObject::natcasesort example
  • Example#4514 - ArrayObject::natsort example
  • Example#4515 - ArrayObject::offsetExists example
  • Example#4516 - ArrayObject::offsetGet example
  • Example#4517 - ArrayObject::offsetSet example
  • Example#4518 - ArrayObject::offsetUnset example
  • Example#4519 - ArrayObject::serialize example
  • Example#4520 - ArrayObject::setFlags example
  • Example#4521 - ArrayObject::setIteratorClass example
  • Example#4522 - ArrayObject::uasort example
  • Example#4523 - ArrayObject::uksort example
  • Example#4524 - SplInt usage example
  • Example#4525 - SplFloat usage example
  • Example#4526 - SplEnum usage example
  • Example#4527 - SplEnum::getConstList example
  • Example#4528 - SplBool usage example
  • Example#4529 - SplString usage example
  • Example#4530 - Using file_get_contents to retrieve data from multiple sources
  • Example#4531 - Making a POST request to an https server
  • Example#4532 - Writing data to a compressed file
  • Example#4533 - A Stream for reading/writing global variables
  • Example#4534 - Listing files from tar archives
  • Example#4535 - stream_bucket_prepend examples
  • Example#4536 - Using stream_context_create
  • Example#4537 - Using stream_context_get_default
  • Example#4538 - stream_context_get_options example
  • Example#4539 - stream_context_get_params example
  • Example#4540 - stream_context_set_default example
  • Example#4541 - A stream_copy_to_stream example
  • Example#4542 - Controlling where filters are applied
  • Example#4543 - Filter for capitalizing characters on foo-bar.txt stream
  • Example#4544 - Registering a generic filter class to match multiple filter names.
  • Example#4545 - Dynamicly refiltering a stream
  • Example#4546 - stream_get_contents example
  • Example#4547 - Using stream_get_filters
  • Example#4548 - stream_get_meta_data example
  • Example#4549 - Using stream_get_transports
  • Example#4550 - stream_get_wrappers example
  • Example#4551 - Checking for the existence of a stream wrapper
  • Example#4552 - stream_is_local example
  • Example#4553 - stream_notification_callback example
  • Example#4554 - Simple progressbar for commandline download client
  • Example#4555 - stream_resolve_include_path example
  • Example#4556 - stream_select Example
  • Example#4557 - stream_set_timeout example
  • Example#4558 - stream_set_write_buffer example
  • Example#4559 - stream_socket_client example
  • Example#4560 - Using UDP connection
  • Example#4561 - stream_socket_enable_crypto example
  • Example#4562 - A stream_socket_pair example
  • Example#4563 - stream_socket_recvfrom example
  • Example#4564 - stream_socket_sendto Example
  • Example#4565 - Using TCP server sockets
  • Example#4566 - Using UDP server sockets
  • Example#4567 - A stream_socket_shutdown example
  • Example#4568 - How to register a stream wrapper
  • Example#4569 - Basic Tidy usage
  • Example#4570 - tidy::getBody example
  • Example#4571 - tidy::cleanrepair example
  • Example#4572 - tidy::__construct example
  • Example#4573 - tidy::diagnose example
  • Example#4574 - tidy_get_error_buffer example
  • Example#4575 - tidy::getConfig example
  • Example#4576 - tidy_getopt example
  • Example#4577 - Print all options along with their documentation and default value
  • Example#4578 - tidy::getStatus example
  • Example#4579 - tidy::head example
  • Example#4580 - tidy::html example
  • Example#4581 - tidy::parseFile example
  • Example#4582 - tidy::parseString example
  • Example#4583 - tidy::repairFile example
  • Example#4584 - tidy::repairString example
  • Example#4585 - tidy::root example
  • Example#4586 - tidyNode::hasChildren example
  • Example#4587 - tidyNode::hasChildren example
  • Example#4588 - tidyNode::hasSiblings example
  • Example#4589 - Extract ASP code from a mixed HTML document
  • Example#4590 - Extract comments from a mixed HTML document
  • Example#4591 - Extract HTML code from a mixed HTML document
  • Example#4592 - Extract JSTE code from a mixed HTML document
  • Example#4593 - Extract PHP code from a mixed HTML document
  • Example#4594 - Extract text from a mixed HTML document
  • Example#4595 - ob_tidyhandler example
  • Example#4596 - tidy_access_count example
  • Example#4597 - tidy_config_count example
  • Example#4598 - tidy_error_count example
  • Example#4599 - tidy_get_output example
  • Example#4600 - tidy_setopt example
  • Example#4601 - tidy_warning_count example
  • Example#4602 - Strip comments with the tokenizer
  • Example#4603 - token_get_all example
  • Example#4604 - token_get_all incorrect usage example
  • Example#4605 - token_get_all on a class using a reserved word example
  • Example#4606 - token_name example
  • Example#4607 - base64_decode 예제
  • Example#4608 - base64_encode 예제
  • Example#4609 - get_headers example
  • Example#4610 - get_headers using HEAD example
  • Example#4611 - get_meta_tags가 해석하는 것
  • Example#4612 - get_meta_tags가 반환하는 것
  • Example#4613 - http_build_query의 간단한 사용법
  • Example#4614 - http_build_query와 숫자 인덱스 요소.
  • Example#4615 - http_build_query와 복잡한 배열
  • Example#4616 - http_build_query에 객체 사용하기
  • Example#4617 - parse_url 예제
  • Example#4618 - rawurldecode 예제
  • Example#4619 - FTP URL에 패스워드 포함하기
  • Example#4620 - rawurlencode 예제 2
  • Example#4621 - urldecode 예제
  • Example#4622 - urlencode 예제
  • Example#4623 - urlencode와 htmlentities 예제
  • Example#4624 - Basic Javascript execution
  • Example#4625 - Yaml Example
  • Example#4626 - Parse callback example
  • Example#4627 - Emit callback example
  • Example#4628 - yaml_emit example
  • Example#4629 - yaml_parse example
  • Example#4630 - A classic Application directory layout
  • Example#4631 - Entry
  • Example#4632 - Rewrite rule
  • Example#4633 - Application config
  • Example#4634 - Default controller
  • Example#4635 - Default view template
  • Example#4636 - Run the Applicatioin
  • Example#4637 - An PHP array example
  • Example#4638 - An ini file example
  • Example#4639 - A Bootstrapexample
  • Example#4640 - Yaf_Application::bootstrapexample
  • Example#4641 - Yaf_Application::clearLastErrorexample
  • Example#4642 - A ini config file example
  • Example#4643 - Yaf_Application::__constructexample
  • Example#4644 - Yaf_Application::__constructexample
  • Example#4645 - Yaf_Application::environexample
  • Example#4646 - Yaf_Application::executeexample
  • Example#4647 - Yaf_Application::getConfigexample
  • Example#4648 - Yaf_Application::getDispatcherexample
  • Example#4649 - Yaf_Application::getLastErrorMsgexample
  • Example#4650 - Yaf_Application::getLastErrorNoexample
  • Example#4651 - Yaf_Application::getModulesexample
  • Example#4652 - Bootstrap example
  • Example#4653 - Yaf_Dispatcher::autoRenderexample
  • Example#4654 - Yaf_Dispatcher::catchExceptionexample
  • Example#4655 - Yaf_Dispatcher::registerPluginexample
  • Example#4656 - A custom View engineexample
  • Example#4657 - Yaf_Dispatcher::setViewexample
  • Example#4658 - Yaf_Dispatcher::throwexceptionexample
  • Example#4659 - Yaf_Dispatcher::throwexceptionexample
  • Example#4660 - Yaf_Config_Iniexample
  • Example#4661 - define action in a separate file
  • Example#4662 - Dummy_action.php
  • Example#4663 - Yaf_Controller_Abstract::forwardexample
  • Example#4664 - Yaf_Action_Abstract::executeexample
  • Example#4665 - Yaf_Action_Abstract::executeexample
  • Example#4666 - Yaf_View_Simple::assignexample
  • Example#4667 - templateexample
  • Example#4668 - Yaf_View_Simple::assignRefexample
  • Example#4669 - templateexample
  • Example#4670 - Yaf_View_Simple::clearexample
  • Example#4671 - Yaf_View_Simple::__constructorexample
  • Example#4672 - Yaf_View_Simple::__setexample
  • Example#4673 - Config example
  • Example#4674 - Register localnamespace
  • Example#4675 - Load class example
  • Example#4676 - Load namespace class example
  • Example#4677 - MVC class loading example
  • Example#4678 - MVC class distinctions
  • Example#4679 - MVC loading example
  • Example#4680 - Yaf_Loader::registerLocalNamespaceexample
  • Example#4681 - Plugin example
  • Example#4682 - Yaf_Plugin_Abstract::routerShutdownexample
  • Example#4683 - Yaf_Response_Abstract::appendBodyexample
  • Example#4684 - Yaf_Response_Abstract::getBodyexample
  • Example#4685 - Yaf_Response_Abstract::prependBodyexample
  • Example#4686 - Yaf_Response_Abstract::responseexample
  • Example#4687 - Yaf_Response_Abstract::setBodyexample
  • Example#4688 - Yaf_Route_Map::assembleexample
  • Example#4689 - Yaf_Route_Mapexample
  • Example#4690 - Yaf_Route_Mapexample
  • Example#4691 - Yaf_Route_Mapexample
  • Example#4692 - Yaf_Route_Regex::assembleexample
  • Example#4693 - Yaf_Route_Regexexample
  • Example#4694 - Yaf_Route_Regex(as of 2.3.0)example
  • Example#4695 - Yaf_Route_Regex and named capture ground(as of 2.3.0)example
  • Example#4696 - Yaf_Route_Regexexample
  • Example#4697 - Yaf_Route_Rewrite::assembleexample
  • Example#4698 - Yaf_Route_Rewriteexample
  • Example#4699 - Yaf_Route_Rewriteexample
  • Example#4700 - Yaf_Route_Rewrite(as of 2.3.0)example
  • Example#4701 - Rewrite rule for Apache
  • Example#4702 - Rewrite rule for Apache
  • Example#4703 - Rewrite rule for Lighttpd
  • Example#4704 - Rewrite rule for Nginx
  • Example#4705 - Yaf_Route_Static(default route)example
  • Example#4706 - application.iniexample
  • Example#4707 - Yaf_Dispatcher::autoConfigexample
  • Example#4708 - Yaf_Dispatcher::autoRenderexample
  • Example#4709 - Register some routes in Bootstrap
  • Example#4710 - plugin Dummy.php (under application.directory/plugins)
  • Example#4711 - Yaf_Route_Simple::assembleexample
  • Example#4712 - Yaf_Route_Simple::routeexample
  • Example#4713 - Yaf_Route_Simple::routeexample
  • Example#4714 - Yaf_Route_Static::assembleexample
  • Example#4715 - Yaf_Route_Static::routeexample
  • Example#4716 - Yaf_Route_Supervar::assembleexample
  • Example#4717 - Yaf_Route_Supervarexample
  • Example#4718 - Taintexample
  • Example#4719 - Creating a chdb file
  • Example#4720 - Loading and looking up a chdb file
  • Example#4721 - chdb::get example
  • Example#4722 - chdb_create example
  • Example#4723 - PHP cURL 모듈로 example.com 페이지를 받아와서 저장하기
  • Example#4724 - 새로운 cURL 세션을 초기화하여 웹 페이지를 가져오는 방법
  • Example#4725 - Copying a cURL handle
  • Example#4726 - curl_errno example
  • Example#4727 - curl_error example
  • Example#4728 - curl_escape example
  • Example#4729 - Fetching a web page
  • Example#4730 - curl_getinfo example
  • Example#4731 - curl_getinfo example with opt parameter
  • Example#4732 - Initializing a new cURL session and fetching a web page
  • Example#4733 - curl_multi_add_handle example
  • Example#4734 - curl_multi_close example
  • Example#4735 - curl_multi_exec example
  • Example#4736 - A curl_multi_info_read example
  • Example#4737 - curl_multi_init example
  • Example#4738 - curl_multi_strerror example
  • Example#4739 - curl_reset example
  • Example#4740 - Initializing a new cURL session and fetching a web page
  • Example#4741 - Our own implementation of curl_setopt_array
  • Example#4742 - Initializing a new cURL session and fetching a web page
  • Example#4743 - Uploading file (deprecated as of PHP 5.5.0)
  • Example#4744 - curl_share_setopt example
  • Example#4745 - curl_share_init example
  • Example#4746 - curl_share_setopt example
  • Example#4747 - curl_errno example
  • Example#4748 - curl_escape example
  • Example#4749 - curl_version example
  • Example#4750 - Simple HTTP client
  • Example#4751 - HTTP client using asynchronous DNS resolver
  • Example#4752 - Echo server
  • Example#4753 - SSL echo server
  • Example#4754 - Signal handler
  • Example#4755 - Use libevent's loop to process requests of `eio' extension
  • Example#4756 - Miscellaneous
  • Example#4757 - Simple HTTP server
  • Example#4758 - Simple HTTPS server
  • Example#4759 - OpenSSL connection
  • Example#4760 - EventHttpConnection::makeRequest example
  • Example#4761 - Connection listener based on a UNIX domain socket
  • Example#4762 - Simple SMTP server
  • Example#4763 - Handling SIGTERM signal
  • Example#4764 - Event::addSignal example
  • Example#4765 - Event::addTimer example
  • Example#4766 - EventBase::getFeatures example
  • Example#4767 - EventBase::getMethod example
  • Example#4768 - EventBuffer::search example
  • Example#4769 - EventBufferEvent::connect example
  • Example#4770 - Connect to UNIX domain socket which presumably is served by a server, read response from the server and output it to the console
  • Example#4771 - EventBufferEvent::connectHost example
  • Example#4772 - Buffer event's read callback
  • Example#4773 - EventBufferEvent::getOutput example
  • Example#4774 - EventBufferEvent::sslError example
  • Example#4775 - Simple SMTP server
  • Example#4776 - EventConfig::avoidMethod example
  • Example#4777 - EventConfig::__construct example
  • Example#4778 - EventConfig::requireFeatures example
  • Example#4779 - EventHttp::accept example
  • Example#4780 - EventHttp::addServerAlias example
  • Example#4781 - EventHttp::bind example
  • Example#4782 - Simple HTTP server
  • Example#4783 - EventHttp::setCallback example
  • Example#4784 - EventHttp::setDefaultCallback example
  • Example#4785 - EventHttpConnection::makeRequest example
  • Example#4786 - EventHttpConnection::setCloseCallback example
  • Example#4787 - EventHttpRequest::__construct example
  • Example#4788 - EventHttpRequest::sendError example
  • Example#4789 - EventListener::__construct example
  • Example#4790 - EventSslContext::__construct example
  • Example#4791 - FTP example
  • Example#4792 - ftp_alloc example
  • Example#4793 - ftp_cdup example
  • Example#4794 - ftp_chdir example
  • Example#4795 - ftp_chmod example
  • Example#4796 - ftp_close example
  • Example#4797 - ftp_connect example
  • Example#4798 - ftp_delete example
  • Example#4799 - ftp_exec example
  • Example#4800 - ftp_fget example
  • Example#4801 - ftp_fput example
  • Example#4802 - ftp_get_option example
  • Example#4803 - ftp_get example
  • Example#4804 - ftp_login example
  • Example#4805 - ftp_mdtm example
  • Example#4806 - ftp_mkdir example
  • Example#4807 - ftp_nb_continue example
  • Example#4808 - ftp_nb_fget example
  • Example#4809 - ftp_nb_fput example
  • Example#4810 - ftp_nb_get example
  • Example#4811 - Resuming a download with ftp_nb_get
  • Example#4812 - Resuming a download at position 100 to a new file with ftp_nb_get
  • Example#4813 - ftp_nb_put example
  • Example#4814 - Resuming an upload with ftp_nb_put
  • Example#4815 - ftp_nlist example
  • Example#4816 - ftp_pasv example
  • Example#4817 - ftp_put example
  • Example#4818 - ftp_pwd example
  • Example#4819 - Using ftp_raw to login to an FTP server manually.
  • Example#4820 - ftp_rawlist example
  • Example#4821 - ftp_rename example
  • Example#4822 - ftp_rmdir example
  • Example#4823 - ftp_set_option example
  • Example#4824 - Sending a SITE command to an ftp server
  • Example#4825 - ftp_size example
  • Example#4826 - ftp_ssl_connect example
  • Example#4827 - ftp_systype example
  • Example#4828 - Basic Gearman client and worker
  • Example#4829 - Basic Gearman client and worker, background
  • Example#4830 - Basic Gearman client and worker, submitting tasks
  • Example#4831 - Adding two job servers
  • Example#4832 - Add two job servers
  • Example#4833 - Basic submission of two tasks
  • Example#4834 - Basic submission of two tasks with passing application context
  • Example#4835 - Two tasks, one background and one not
  • Example#4836 - A high priority task along with two normal tasks
  • Example#4837 - A low priority task along with two normal tasks
  • Example#4838 - Monitor completion of multiple background tasks
  • Example#4839 - Simple job submission with immediate return
  • Example#4840 - Submitting a job and retrieving incremental status
  • Example#4841 - Submit and monitor a background job
  • Example#4842 - Simple job submission with immediate return
  • Example#4843 - Submitting a job and retrieving incremental status
  • Example#4844 - Get the status of a long running job
  • Example#4845 - Monitor the status of a long running background job
  • Example#4846 - Simple worker making use of extra application context data
  • Example#4847 - Add alternate Gearman servers
  • Example#4848 - Add two job servers
  • Example#4849 - GearmanWorker::setId example
  • Example#4850 - A simple worker with a 5 second timeout
  • Example#4851 - Running worker in non-blocking mode
  • Example#4852 - GearmanWorker::work example
  • Example#4853 - Hypothetical output from gopher://gopher.example.com/
  • Example#4854 - Using gopher_parsedir
  • Example#4855 - Search for all UPnP devices and services.
  • Example#4856 - Implementing light server
  • Example#4857 - Implementing light client
  • Example#4858 - Create new UPnP context and get IP address of the host
  • Example#4859 - Create new UPnP context and get port number
  • Example#4860 - Create new UPnP context and set host path
  • Example#4861 - Create new UPnP context
  • Example#4862 - Create new UPnP context and set callback
  • Example#4863 - Create new UPnP context and start browsing
  • Example#4864 - Create new UPnP context and start browsing
  • Example#4865 - Create new UPnP context and get device info service
  • Example#4866 - Create new UPnP context and get device info service
  • Example#4867 - Retrieve an object
  • Example#4868 - LDAP search example
  • Example#4869 - Complete example with authenticated bind
  • Example#4870 - Using LDAP Bind
  • Example#4871 - Using LDAP Bind Anonymously
  • Example#4872 - Complete example of password check
  • Example#4873 - Example of connecting to LDAP server.
  • Example#4874 - Example of connecting securely to LDAP server.
  • Example#4875 - LDAP pagination
  • Example#4876 - LDAP pagination
  • Example#4877 - ldap-count-entries example
  • Example#4878 - Enumerating all LDAP error messages
  • Example#4879 - Generating and catching an error
  • Example#4880 - Show the list of attributes held for a particular directory entry
  • Example#4881 - Check protocol version
  • Example#4882 - List all values of the "mail" attribute for a directory entry
  • Example#4883 - Produce a list of all organizational units of an organization
  • Example#4884 - Add a telephone number to a contact
  • Example#4885 - Rename a user
  • Example#4886 - Add two e-mail addresses to a user
  • Example#4887 - Change a user's password
  • Example#4888 - Change a user's password (Active Directory)
  • Example#4889 - ldap_parse_result example
  • Example#4890 - LDAP search
  • Example#4891 - Set protocol version
  • Example#4892 - Set server controls
  • Example#4893 - LDAP sort
  • Example#4894 - memcache extension overview example
  • Example#4895 - Using memcache session handler
  • Example#4896 - Memcache::add example
  • Example#4897 - Memcache::addServer example
  • Example#4898 - Memcache::close example
  • Example#4899 - Memcache::connect example
  • Example#4900 - Memcache::decrement example
  • Example#4901 - Memcache::delete example
  • Example#4902 - Memcache::flush example
  • Example#4903 - Memcache::get example
  • Example#4904 - Memcache::getExtendedStats example
  • Example#4905 - Memcache::getServerStatus example
  • Example#4906 - Memcache::getVersion example
  • Example#4907 - Memcache::increment example
  • Example#4908 - Memcache::pconnect example
  • Example#4909 - Memcache::replace example
  • Example#4910 - Memcache::set example
  • Example#4911 - Memcache::set example
  • Example#4912 - Memcache::setCompressThreshold example
  • Example#4913 - Memcache::setServerParams example
  • Example#4914 - Result callback example
  • Example#4915 - Read-through callback example
  • Example#4916 - Memcached::addServer example
  • Example#4917 - Memcached::addServers example
  • Example#4918 - Memcached::append example
  • Example#4919 - Memcached::cas example
  • Example#4920 - Creating a Memcached object
  • Example#4921 - Memcached::decrement example
  • Example#4922 - Memcached::delete example
  • Example#4923 - Memcached::fetch example
  • Example#4924 - Memcached::getDelayed example
  • Example#4925 - Memcached::flush example
  • Example#4926 - Memcached::get example #1
  • Example#4927 - Memcached::get example #2
  • Example#4928 - Memcached::getDelayed example
  • Example#4929 - Memcached::getMulti example
  • Example#4930 - Memcached::GET_PRESERVE_ORDER example
  • Example#4931 - Retrieving Memcached options
  • Example#4932 - Memcached::getResultCode example
  • Example#4933 - Memcached::getResultMessage example
  • Example#4934 - Memcached::getServerByKey example
  • Example#4935 - Memcached::getServerList example
  • Example#4936 - Memcached::getStats example
  • Example#4937 - Memcached::getVersion example
  • Example#4938 - Memcached::increment example
  • Example#4939 - Memcached::prepend example
  • Example#4940 - Memcached::set example
  • Example#4941 - Memcached::setByKey example
  • Example#4942 - Memcached::setMulti example
  • Example#4943 - Setting a Memcached option
  • Example#4944 - Setting Memcached options
  • Example#4945 - mqseries_back example
  • Example#4946 - mqseries_begin example
  • Example#4947 - mqseries_close example
  • Example#4948 - mqseries_cmit example
  • Example#4949 - mqseries_conn example
  • Example#4950 - mqseries_connx example
  • Example#4951 - mqseries_connx example using SSL connection & OCSP Responder URL
  • Example#4952 - mqseries_disc example
  • Example#4953 - mqseries_get example
  • Example#4954 - mqseries_inq example
  • Example#4955 - mqseries_open example
  • Example#4956 - mqseries_put example
  • Example#4957 - mqseries_strerror example
  • Example#4958 - define_syslog_variables example
  • Example#4959 - Using dns_get_record
  • Example#4960 - Using dns_get_record and DNS_ANY
  • Example#4961 - fsockopen Example
  • Example#4962 - Using UDP connection
  • Example#4963 - A simple gethostbyaddr example
  • Example#4964 - A simple gethostbyname example
  • Example#4965 - gethostbynamel example
  • Example#4966 - A simple gethostname example
  • Example#4967 - getprotobyname example
  • Example#4968 - getservbyname example
  • Example#4969 - header_register_callback example
  • Example#4970 - Unsetting specific header.
  • Example#4971 - Unsetting all previously set headers.
  • Example#4972 - Download dialog
  • Example#4973 - Caching directives
  • Example#4974 - Examples using headers_list
  • Example#4975 - Examples using headers_sent
  • Example#4976 - Using http_response_code in a web server environment
  • Example#4977 - Using http_response_code in a CLI environment
  • Example#4978 - inet_ntop Example
  • Example#4979 - inet_pton Example
  • Example#4980 - ip2long Example
  • Example#4981 - Displaying an IP address
  • Example#4982 - setcookie send example
  • Example#4983 - setcookie delete example
  • Example#4984 - setcookie and arrays
  • Example#4985 - Using syslog
  • Example#4986 - Procedural usage of rrd
  • Example#4987 - OO usage of rrd
  • Example#4988 - RRDGraph::setOptions example
  • Example#4989 - Set multiple color options
  • Example#4990 - RRDUpdater::update examples
  • Example#4991 - Creating a connection and connecting to a remote WebSphere MQSeries Messaging Server
  • Example#4992 - Creating a connection and connecting to a remote WebSphere Application Server
  • Example#4993 - Creating a connection and connecting to an MQTT server
  • Example#4994 - Creating a message with a simple text body
  • Example#4995 - Setting a text format property using the default syntax
  • Example#4996 - Setting a property using a type hint
  • Example#4997 - Retrieving a property from a message header
  • Example#4998 - Adding a message to a queue and receiving a response
  • Example#4999 - Creating a durable subscription to a topic
  • Example#5000 - Subscribing to a topic using a WebSphere Platform Messaging (WPM) server
  • Example#5001 - Receiving published data using a durable subscription
  • Example#5002 - Deleting a durable subscription to a topic
  • Example#5003 - Handling an error from a method that returns no result
  • Example#5004 - Handling an error from a method that returns a result
  • Example#5005 - Committing the current unit of work
  • Example#5006 - Creating a connection to a Messaging Server using the IBM MQSeries protocol (WMQ)
  • Example#5007 - Creating a connection with application transaction control and default host and port values
  • Example#5008 - Creating a connection to a Messaging Server using the IBM WebSphere Platform Messaging protocol (WPM)
  • Example#5009 - Creating a connection object and connecting to a Messaging Server
  • Example#5010 - Disconnecting from a Messaging Server
  • Example#5011 - Using the error number and description properties
  • Example#5012 - Using the error number and description properties
  • Example#5013 - Checking whether there us a connection to a Messaging Server
  • Example#5014 - Retrieve the next message from a queue without removing it
  • Example#5015 - Retrieve a specific message from a queue without removing it from the queue
  • Example#5016 - Retrieve all messages in a queue without removing them
  • Example#5017 - Retrieve all messages from a queue with a matching correlation id
  • Example#5018 - Receiving a message from a queue
  • Example#5019 - Receiving a message from a queue with options
  • Example#5020 - Receiving a message from a subscription
  • Example#5021 - Removing a message from a queue by message id
  • Example#5022 - Cancelling an in-flight unit of work
  • Example#5023 - Send a message to a queue
  • Example#5024 - Publish a message to a topic
  • Example#5025 - Send a request and receive a response
  • Example#5026 - Turn on debugging output
  • Example#5027 - Turn off debugging output
  • Example#5028 - Subscribe to a topic
  • Example#5029 - Delete a subscription
  • Example#5030 - Setting a text string into the body of a message
  • Example#5031 - Creating a message
  • Example#5032 - Creating a message with a simple text payload
  • Example#5033 - Setting a text format property using the default syntax
  • Example#5034 - Setting a text format property using a type hint
  • Example#5035 - Setting properties as the sender of a message
  • Example#5036 - Retreiving property values from a message
  • Example#5037 - snmp_get_quick_print example
  • Example#5038 - Using snmp_get_valueretrieval
  • Example#5039 - Using snmp_read_mib
  • Example#5040 - Using snmp_set_enum_print
  • Example#5041 - Using snmprealwalk
  • Example#5042 - Using snmp_set_quick_print
  • Example#5043 - Using snmp_set_valueretrieval
  • Example#5044 - Using snmp2_get
  • Example#5045 - Using snmp2_get_next
  • Example#5046 - Using snmp2_real_walk
  • Example#5047 - Using snmp2_set
  • Example#5048 - Using snmp2_set for setting BITS SNMP object id
  • Example#5049 - snm2_pwalk Example
  • Example#5050 - Using snmp3_get
  • Example#5051 - Using snmp3_getnext
  • Example#5052 - Using snmp3_real_walk
  • Example#5053 - Using snmp3_set
  • Example#5054 - Using snmp3_set for setting BITS SNMP object id
  • Example#5055 - snmp3_walk Example
  • Example#5056 - Using snmpget
  • Example#5057 - Using snmpgetnext
  • Example#5058 - Using snmprealwalk
  • Example#5059 - Using snmpset
  • Example#5060 - Using snmpset for setting BITS SNMP object id
  • Example#5061 - snmpwalk Example
  • Example#5062 - snmpwalkoid Example
  • Example#5063 - SNMP::close example
  • Example#5064 - Fetching sysLocation
  • Example#5065 - Single SNMP object
  • Example#5066 - Multiple SNMP objects
  • Example#5067 - SNMP::getErrno example
  • Example#5068 - SNMP::getError example
  • Example#5069 - Single SNMP object
  • Example#5070 - Miltiple SNMP objects
  • Example#5071 - Set single SNMP object id
  • Example#5072 - Set multiple values using single SNMP::set call
  • Example#5073 - Using SNMP::set for setting BITS SNMP object id
  • Example#5074 - SNMP::setSecurity example
  • Example#5075 - SNMP::walk example
  • Example#5076 - suffix_as_key example
  • Example#5077 - Socket example: Simple TCP/IP server
  • Example#5078 - Socket example: Simple TCP/IP client
  • Example#5079 - Using socket_bind to set the source address
  • Example#5080 - socket_create_pair example
  • Example#5081 - socket_create_pair IPC example
  • Example#5082 - socket_get_option example
  • Example#5083 - socket_import_stream example
  • Example#5084 - socket_last_error example
  • Example#5085 - socket_recv example
  • Example#5086 - socket_recvfrom example
  • Example#5087 - Using NULL with socket_select
  • Example#5088 - Understanding socket_select's result
  • Example#5089 - socket_select example
  • Example#5090 - socket_sendto Example
  • Example#5091 - socket_set_block example
  • Example#5092 - socket_set_nonblock example
  • Example#5093 - socket_set_option example
  • Example#5094 - socket_strerror example
  • Example#5095 - Authenticating with a ssh agent
  • Example#5096 - Authentication using a public hostkey
  • Example#5097 - Retrieving a list of authentication methods
  • Example#5098 - Authenticating with a password
  • Example#5099 - Authentication using a public key
  • Example#5100 - ssh2_connect example
  • Example#5101 - Executing a command
  • Example#5102 - Opening a shell and retrieving the stderr stream associated with it
  • Example#5103 - Checking the fingerprint against a known value
  • Example#5104 - Determining what methods were negotiated
  • Example#5105 - Adding a publickey with ssh2_publickey_add
  • Example#5106 - Listing authorized keys with ssh2_publickey_list
  • Example#5107 - Downloading a file via SCP
  • Example#5108 - Uploading a file via SCP
  • Example#5109 - Changing the mode of a file on a remote server
  • Example#5110 - Stating a symbolic link via SFTP
  • Example#5111 - Creating a directory on a remote server
  • Example#5112 - Reading a symbolic link
  • Example#5113 - Resolving a pathname
  • Example#5114 - Renaming a file via sftp
  • Example#5115 - Removing a directory on a remote server
  • Example#5116 - Stating a file via SFTP
  • Example#5117 - Creating a symbolic link
  • Example#5118 - Deleting a file
  • Example#5119 - Opening a file via SFTP
  • Example#5120 - Requesting an interactive shell
  • Example#5121 - Opening a tunnel to an arbitrary host
  • Example#5122 - 객체 기반 형식
  • Example#5123 - 절차식 형식
  • Example#5124 - stomp_connect_error example
  • Example#5125 - stomp_version example
  • Example#5126 - 객체 기반 형식
  • Example#5127 - 절차식 형식
  • Example#5128 - 객체 기반 형식
  • Example#5129 - 절차식 형식
  • Example#5130 - 객체 기반 형식
  • Example#5131 - 절차식 형식
  • Example#5132 - 객체 기반 형식
  • Example#5133 - 절차식 형식
  • Example#5134 - 객체 기반 형식
  • Example#5135 - 절차식 형식
  • Example#5136 - 객체 기반 형식
  • Example#5137 - 절차식 형식
  • Example#5138 - 객체 기반 형식
  • Example#5139 - 절차식 형식
  • Example#5140 - 객체 기반 형식
  • Example#5141 - 절차식 형식
  • Example#5142 - 객체 기반 형식
  • Example#5143 - 절차식 형식
  • Example#5144 - Train from array
  • Example#5145 - Train from a file
  • Example#5146 - svn_add example
  • Example#5147 - Default authentication example
  • Example#5148 - svn_blame example
  • Example#5149 - Basic example
  • Example#5150 - Basic example
  • Example#5151 - Basic example
  • Example#5152 - Basic example
  • Example#5153 - Basic example
  • Example#5154 - Basic example
  • Example#5155 - Diffing two revisions of a repository path
  • Example#5156 - Portably diffing two local files
  • Example#5157 - svn_export example
  • Example#5158 - Basic example
  • Example#5159 - svn_log example
  • Example#5160 - svn_ls example
  • Example#5161 - Basic example
  • Example#5162 - Basic example
  • Example#5163 - Deny all connections from localhost
  • Example#5164 - Ban an URL
  • Example#5165 - Get statistic snapshot
  • Example#5166 - Read varnish shared memory log
  • Example#5167 - VarnishAdmin::__construct example
  • Example#5168 - Parallel searching using Yaz
  • Example#5169 - CCL configuration
  • Example#5170 - CCL Parsing
  • Example#5171 - Record Update
  • Example#5172 - Array for GRS-1 record
  • Example#5173 - Working with MARCXML
  • Example#5174 - PHP function that scans titles
  • Example#5175 - Query Examples
  • Example#5176 - Sort Criterias
  • Example#5177 - Example for NIS errors
  • Example#5178 - Example for the NIS first
  • Example#5179 - Example for the default domain
  • Example#5180 - Example for the NIS master
  • Example#5181 - Example for NIS match
  • Example#5182 - Example for NIS next
  • Example#5183 - Example for the NIS order
  • Example#5184 - A ZMQContext example
  • Example#5185 - A ZMQContext example
  • Example#5186 - A ZMQContext example
  • Example#5187 - A ZMQSocket example
  • Example#5188 - A send/recv example
  • Example#5189 - A ZMQPoll example
  • Example#5190 -
  • Example#5191 - udm_api_version example
  • Example#5192 - udm_cat_listexample
  • Example#5193 - Specifying path to the current category in the following format: '> Root > Sport > Auto > Ferrari'
  • Example#5194 - udm_load_ispell_data example
  • Example#5195 - udm_load_ispell_data example
  • Example#5196 - solr_get_version example
  • Example#5197 - Contents of the BootStrap file
  • Example#5198 - Adding a document to the index
  • Example#5199 - Merging one document into another document
  • Example#5200 - Searching for documents - SolrObject responses
  • Example#5201 - Searching for documents - SolrDocument responses
  • Example#5202 - Simple TermsComponent example - basic
  • Example#5203 - Simple TermsComponent example - using a prefix
  • Example#5204 - Simple TermsComponent example - specifying a minimum frequency
  • Example#5205 - Simple Facet Example
  • Example#5206 - Simple Facet Example - with optional field override for mincount
  • Example#5207 - Facet Date Example
  • Example#5208 - Connecting to SSL-Enabled Server
  • Example#5209 - Collapsing a SolrQuery
  • Example#5210 - SolrDocument::toArray example
  • Example#5211 - SolrObject::__construct example
  • Example#5212 - SolrObject::offsetUnset example
  • Example#5213 - SolrClient::addDocument example
  • Example#5214 - SolrClient::addDocument example 2
  • Example#5215 - SolrClient::addDocuments example
  • Example#5216 - SolrClient::__construct example
  • Example#5217 - SolrQuery::deleteByQuery example
  • Example#5218 - SolrClient::getById example
  • Example#5219 - SolrClient::getByIds example
  • Example#5220 - SolrClient::ping example
  • Example#5221 - SolrClient::query example
  • Example#5222 - SolrClient::request example
  • Example#5223 - SolrClient::setResponseWriter example
  • Example#5224 - SolrParams::setParam example
  • Example#5225 - SolrQuery::addFacetField example
  • Example#5226 - SolrQuery::addFacetField example
  • Example#5227 - SolrQuery::addFilterQuery example
  • Example#5228 - SolrQuery::addGroupSortField example
  • Example#5229 - SolrQuery::collapse example
  • Example#5230 - SolrQuery::setExpand example
  • Example#5231 - SolrDisMaxQuery::addBigramPhraseField example
  • Example#5232 - SolrDisMaxQuery::addBoostQuery example
  • Example#5233 - SolrDisMaxQuery::addPhraseField example
  • Example#5234 - SolrDisMaxQuery::addQueryField example
  • Example#5235 - SolrDisMaxQuery::addTrigramPhraseField example
  • Example#5236 - SolrDisMaxQuery::addUserField example
  • Example#5237 - SolrDisMaxQuery::__construct example
  • Example#5238 - SolrDisMaxQuery::removeBigramPhraseField example
  • Example#5239 - SolrDisMaxQuery::removeBoostQuery example
  • Example#5240 - SolrDisMaxQuery::removePhraseField example
  • Example#5241 - SolrDisMaxQuery::removeQueryField example
  • Example#5242 - SolrDisMaxQuery::removeTrigramPhraseField example
  • Example#5243 - SolrDisMaxQuery::removeUserField example
  • Example#5244 - SolrDisMaxQuery::setBigramPhraseFields example
  • Example#5245 - SolrDisMaxQuery::setBigramPhraseSlop example
  • Example#5246 - SolrDisMaxQuery::setBoostFunction example
  • Example#5247 - SolrDisMaxQuery::setBoostQuery example
  • Example#5248 - SolrDisMaxQuery::setMinimumMatch example
  • Example#5249 - SolrDisMaxQuery::setPhraseFields example
  • Example#5250 - SolrDisMaxQuery::setPhraseSlop example
  • Example#5251 - SolrDisMaxQuery::setQueryAlt example
  • Example#5252 - SolrDisMaxQuery::setQueryPhraseSlop example
  • Example#5253 - SolrDisMaxQuery::setTieBreaker example
  • Example#5254 - SolrDisMaxQuery::setTrigramPhraseFields example
  • Example#5255 - SolrDisMaxQuery::setTrigramPhraseSlop example
  • Example#5256 - SolrDisMaxQuery::setUserFields example
  • Example#5257 - SolrDisMaxQuery::useDisMaxQueryParser example
  • Example#5258 - SolrDisMaxQuery::useEDisMaxQueryParser example
  • Example#5259 - SolrCollapseFunction::__construct example
  • Example#5260 - SolrCollapseFunction::setHint example
  • Example#5261 - SolrCollapseFunction::setMax example
  • Example#5262 - Basic usage example
  • Example#5263 - Basic search query
  • Example#5264 - A Swish::__construct example
  • Example#5265 - Basic Swish::getMetaList example
  • Example#5266 - Basic Swish::getPropertyList example
  • Example#5267 - Basic Swish::prepare example
  • Example#5268 - Basic Swish::query example
  • Example#5269 - Basic SwishResult::stem example
  • Example#5270 - Basic SwishResults::getParsedWords example
  • Example#5271 - Basic SwishResults::nextResult example
  • Example#5272 - Basic SwishResults::seekResult example
  • Example#5273 - Basic SwishSearch::execute example
  • Example#5274 - Basic SwishSearch::resetLimit example
  • Example#5275 - Basic SwishSearch::setLimit example
  • Example#5276 - Basic SwishSearch::setPhraseDelimiter example
  • Example#5277 - Basic SwishSearch::setSort example
  • Example#5278 - Basic SwishSearch::setStructure example
  • Example#5279 - .htaccess를 이용해서 PHP 파싱을 끄기
  • Example#5280 - apache_get_modules 예제
  • Example#5281 - apache_get_version 예제
  • Example#5282 - apache_getenv 예제
  • Example#5283 - apache_lookup_uri 예제
  • Example#5284 - PHP와 펄 사이에 정보 넘기기
  • Example#5285 - access.log에 값 기록하기
  • Example#5286 - apache_request_headers 예제
  • Example#5287 - apache_response_headers 예제
  • Example#5288 - apache_setenv를 사용하여 아파치 환경 변수 설정하기
  • Example#5289 - getallheaders 예제
  • Example#5290 - nsapi_request_headers example
  • Example#5291 - $_SESSION으로 변수 등록하기.
  • Example#5292 - $_SESSION으로 변수 제거하기와 register_globals 비활성화.
  • Example#5293 - 한 사용자의 히트 수 세기
  • Example#5294 - Example information
  • Example#5295 - session_cache_expire 예제
  • Example#5296 - session_cache_limiter 예제
  • Example#5297 - session_create_id example with session_regenerate_id
  • Example#5298 - $_SESSION으로 세션 파괴하기
  • Example#5299 - session_gc example for task managers like cron
  • Example#5300 - session_gc example for user accessible script
  • Example#5301 - session_name 예제
  • Example#5302 - session_regenerate_id 예제
  • Example#5303 - session_set_save_handler 예제
  • Example#5304 - 세션 예제: page1.php
  • Example#5305 - 세션 예제: page2.php
  • Example#5306 - Using SessionHandler to add encryption to internal PHP save handlers.
  • Example#5307 - Example using SessionHandlerInterface
  • Example#5308 - bbcode_add_smiley usage example
  • Example#5309 - bbcode_create example
  • Example#5310 - bbcode_set_arg_parser usage example
  • Example#5311 - bbcode_set_flags usage example
  • Example#5312 - Examples of valid patterns
  • Example#5313 - Examples of invalid patterns
  • Example#5314 - Example comparing preg_filter with preg_replace
  • Example#5315 - preg_grep 예제
  • Example#5316 - preg_last_error example
  • Example#5317 - 어떤 텍스트로부터 모든 전화번호 가져오기
  • Example#5318 - HTML 태그 찾기 (greedy)
  • Example#5319 - 이름 있는 서브패턴 사용하기
  • Example#5320 - 문자열 "php" 찾기
  • Example#5321 - 단어 "Web" 찾기
  • Example#5322 - URL에서 도메인 이름 얻기
  • Example#5323 - 이름 있는 서브패턴 사용하기
  • Example#5324 - preg_quote 예제
  • Example#5325 - 텍스트 안의 단어를 기울임꼴로
  • Example#5326 - preg_replace_callback_array example
  • Example#5327 - preg_replace_callback과 create_function
  • Example#5328 - preg_replace_callback 예제
  • Example#5329 - BB 코드를 다루기 위해 재귀 구조를 사용하는 preg_replace_callback
  • Example#5330 - 수가 따라오는 역참조 사용하기
  • Example#5331 - preg_replace와 인덱스 배열 사용하기
  • Example#5332 - 여러 값 치환하기
  • Example#5333 - 'e' 변경자 사용하기
  • Example#5334 - 공백 제거하기
  • Example#5335 - count 인수 사용하기
  • Example#5336 - preg_split 예제 : 검색 문자열의 일부만을 얻기
  • Example#5337 - 문자열을 구성 문자로 나누기.
  • Example#5338 - 매치와 시작위치로 문자열을 나누기.
  • Example#5339 - Regular Expression Examples
  • Example#5340 - ereg_replace example
  • Example#5341 - ereg_replace example
  • Example#5342 - Replace URLs with links
  • Example#5343 - ereg example
  • Example#5344 - Highlight search results
  • Example#5345 - eregi example
  • Example#5346 - split example
  • Example#5347 - split example
  • Example#5348 - spliti example
  • Example#5349 - sql_regcase example
  • Example#5350 - addcslashes 예제
  • Example#5351 - addslashes 예제
  • Example#5352 - chr 예제
  • Example#5353 - chunk_split 예제
  • Example#5354 - convert_uudecode example
  • Example#5355 - convert_uuencode example
  • Example#5356 - count_chars 예제
  • Example#5357 - crc32 체크섬 표시하기
  • Example#5358 - crypt 예제
  • Example#5359 - htpasswd와 crypt 사용하기
  • Example#5360 - 다양한 암호화 형식으로 crypt 사용하기
  • Example#5361 - echo 예제
  • Example#5362 - explode 예제
  • Example#5363 - limit 인수 예제
  • Example#5364 - fprintf: 0을 채운 정수
  • Example#5365 - fprintf: 통화 형식화
  • Example#5366 - 변환표 예제
  • Example#5367 - hex2bin example
  • Example#5368 - HTML 엔티티 디코딩
  • Example#5369 - htmlentities 예제
  • Example#5370 - A htmlspecialchars_decode example
  • Example#5371 - htmlspecialchars 예제
  • Example#5372 - implode 예제
  • Example#5373 - lcfirst example
  • Example#5374 - levenshtein example
  • Example#5375 - localeconv example
  • Example#5376 - ltrim 용례
  • Example#5377 - md5_file 용례
  • Example#5378 - md5 예제
  • Example#5379 - money_format Example
  • Example#5380 - nl2br 사용하기
  • Example#5381 - is_xhtml 인수를 사용해서 유효한 HTML 생성하기
  • Example#5382 - number_format Example
  • Example#5383 - ord 예제
  • Example#5384 - parse_str 사용하기
  • Example#5385 - print 예제
  • Example#5386 - rtrim 사용 예제
  • Example#5387 - setlocale Examples
  • Example#5388 - setlocale Examples for Windows
  • Example#5389 - sha1_file 예제
  • Example#5390 - sha1 예제
  • Example#5391 - Soundex 예제
  • Example#5392 - 인수 교환
  • Example#5393 - 인수 교환
  • Example#5394 - 인수 교환
  • Example#5395 - 인수 교환
  • Example#5396 - printf: 다양한 예제
  • Example#5397 - printf: 문자열 지정어
  • Example#5398 - sprintf: 0으로 채운 정수
  • Example#5399 - sprintf: 통화 표현하기
  • Example#5400 - sprintf: 과학 표현
  • Example#5401 - sscanf 예제
  • Example#5402 - sscanf - 선택 인수 사용하기
  • Example#5403 - str_ireplace 예제
  • Example#5404 - str_pad 예제
  • Example#5405 - str_repeat 예제
  • Example#5406 - str_replace 예제
  • Example#5407 - str_rot13 예제
  • Example#5408 - str_shuffle 예제
  • Example#5409 - str_split 용례
  • Example#5410 - str_word_count 예제
  • Example#5411 - strcasecmp 예제
  • Example#5412 - strip_tags 예제
  • Example#5413 - stripos 예제
  • Example#5414 - stripslashes 예제
  • Example#5415 - 배열에 stripslashes 사용하기
  • Example#5416 - stristr 예제
  • Example#5417 - 문자열의 발견 여부 시험하기
  • Example#5418 - "문자열"이 아닌 needle 사용하기
  • Example#5419 - strlen 예제
  • Example#5420 - strpbrk example
  • Example#5421 - === 사용하기
  • Example#5422 - !== 사용하기
  • Example#5423 - offset 사용하기
  • Example#5424 - strrchr 예제
  • Example#5425 - strrev를 사용하여 문자열 뒤집기
  • Example#5426 - 간단한 strripos 예제
  • Example#5427 - strspn 예제
  • Example#5428 - strstr 예제
  • Example#5429 - strtok 예제
  • Example#5430 - strtok 이전 동작
  • Example#5431 - strtok 새 동작
  • Example#5432 - strtolower 예제
  • Example#5433 - strtoupper 예제
  • Example#5434 - strtr example
  • Example#5435 - strtr example with two arguments
  • Example#5436 - strtr behavior comparison
  • Example#5437 - A substr_compare example
  • Example#5438 - A substr_count example
  • Example#5439 - Simple substr_replace examples
  • Example#5440 - Using substr_replace to replace multiple strings at once
  • Example#5441 - Using a negative start
  • Example#5442 - Using a negative length
  • Example#5443 - Basic substr usage
  • Example#5444 - substr casting behaviour
  • Example#5445 -
  • Example#5446 - Usage example of trim
  • Example#5447 - Trimming array values with trim
  • Example#5448 - ucfirst example
  • Example#5449 - ucwords example
  • Example#5450 - ucwords example with custom delimiter
  • Example#5451 - vfprintf: zero-padded integers
  • Example#5452 - vprintf: zero-padded integers
  • Example#5453 - vsprintf: zero-padded integers
  • Example#5454 - wordwrap example
  • Example#5455 - wordwrap example
  • Example#5456 - wordwrap example
  • Example#5457 - array_change_key_case 예제
  • Example#5458 - array_chunk 예제
  • Example#5459 - Get the column of first names from a recordset
  • Example#5460 - Get the column of last names from a recordset, indexed by the "id" column
  • Example#5461 - Get the column of usernames from the public "username" property of an object
  • Example#5462 - Get the column of names from the private "name" property of an object using the magic __get method.
  • Example#5463 - 간단한 array_combine 예제
  • Example#5464 - array_count_values 예제
  • Example#5465 - array_diff_assoc 예제
  • Example#5466 - array_diff_assoc 예제
  • Example#5467 - array_diff_key example
  • Example#5468 - array_diff_uassoc example
  • Example#5469 - array_diff_ukey example
  • Example#5470 - array_diff 예제
  • Example#5471 - array_fill_keys example
  • Example#5472 - array_fill 예제
  • Example#5473 - array_filter 예제
  • Example#5474 - callback 없는 array_filter
  • Example#5475 - array_flip 예제
  • Example#5476 - array_flip 예제 : 충돌
  • Example#5477 - array_intersect_assoc 예제코드
  • Example#5478 - array_intersect_key example
  • Example#5479 - array_intersect_uassoc example
  • Example#5480 - array_intersect_ukey example
  • Example#5481 - array_intersect 예제
  • Example#5482 - array_key_exists 예제
  • Example#5483 - array_key_exists 대 isset
  • Example#5484 - array_keys 예제
  • Example#5485 - array_map example
  • Example#5486 - array_map using a lambda function (as of PHP 5.3.0)
  • Example#5487 - array_map - using more arrays
  • Example#5488 - Creating an array of arrays
  • Example#5489 - array_map - with string keys
  • Example#5490 - array_merge_recursive 예제
  • Example#5491 - array_merge 예제
  • Example#5492 - 간단한 array_merge 예제
  • Example#5493 - array_merge PHP 5 예제
  • Example#5494 - 여러 배열 정렬하기
  • Example#5495 - 다차원 배열 정렬하기
  • Example#5496 - 데이터베이스 결과 정렬하기
  • Example#5497 - 대소문자 구분 없는 정렬
  • Example#5498 - array_pad 예제
  • Example#5499 - array_pop 예제
  • Example#5500 - array_product examples
  • Example#5501 - array_push 예제
  • Example#5502 - array_rand 예제
  • Example#5503 - array_reduce 예제
  • Example#5504 - array_replace_recursive example
  • Example#5505 - array_replace_recursive and recursive behavior
  • Example#5506 - array_replace example
  • Example#5507 - array_reverse 예제
  • Example#5508 - array_search 예제
  • Example#5509 - array_shift 예제
  • Example#5510 - array_slice 예제
  • Example#5511 - array_splice 예제
  • Example#5512 - array_splice 예제
  • Example#5513 - array_sum 예제
  • Example#5514 - array_udiff_assoc example
  • Example#5515 - array_udiff_uassoc example
  • Example#5516 - stdClass 객체를 이용한 array_udiff 예제
  • Example#5517 - DateTime 객체를 이용한 array_udiff 예제
  • Example#5518 - array_uintersect_assoc example
  • Example#5519 - array_uintersect_uassoc example
  • Example#5520 - array_uintersect example
  • Example#5521 - array_unique 예제
  • Example#5522 - array_unique와 자료형
  • Example#5523 - array_unshift 예제
  • Example#5524 - array_values 예제
  • Example#5525 - array_walk_recursive example
  • Example#5526 - array_walk 예제
  • Example#5527 - array 예제
  • Example#5528 - 자동 인덱스 array
  • Example#5529 - 1-기반 인덱스 array
  • Example#5530 - 겹따옴표 안에서 배열 접근하기
  • Example#5531 - arsort 예제
  • Example#5532 - asort 예제
  • Example#5533 - compact 예제
  • Example#5534 - count 예제
  • Example#5535 - 재귀적인 count 예제
  • Example#5536 - current의 용례와 관련 함수
  • Example#5537 - each 예제
  • Example#5538 - each로 배열 따라가기
  • Example#5539 - end 예제
  • Example#5540 - extract 예제
  • Example#5541 - in_array 예제
  • Example#5542 - in_array에 strict 예제
  • Example#5543 - in_array에 needle로 배열
  • Example#5544 - key 예제
  • Example#5545 - krsort 예제
  • Example#5546 - ksort 예제
  • Example#5547 - list 예제
  • Example#5548 - list 용례
  • Example#5549 - 중첩 list 사용하기
  • Example#5550 - 배열 인덱스로 list 사용하기
  • Example#5551 - natcasesort 예제
  • Example#5552 - natsort 예제
  • Example#5553 - next의 용례와 관련 함수
  • Example#5554 - prev 용례와 관련 함수
  • Example#5555 - range 예제
  • Example#5556 - reset 예제
  • Example#5557 - rsort 예제
  • Example#5558 - shuffle 예제
  • Example#5559 - sort 예제
  • Example#5560 - uksort 예제
  • Example#5561 - usort 예제
  • Example#5562 - 다차원 배열을 사용하는 usort 예제
  • Example#5563 - 객체의 멤버 함수를 사용하는 usort 예제
  • Example#5564 - classes.inc
  • Example#5565 - test_script.php
  • Example#5566 - call_user_method_array 대체
  • Example#5567 - call_user_method 대체
  • Example#5568 - class_alias example
  • Example#5569 - class_exists 예제
  • Example#5570 - autoload 인수 예제
  • Example#5571 - get_called_class 사용하기
  • Example#5572 - get_class_methods 예제
  • Example#5573 - get_class_vars 예제
  • Example#5574 - get_class 사용하기
  • Example#5575 - 슈퍼클래스에서 get_class 사용하기
  • Example#5576 - get_declared_classes 예제
  • Example#5577 - get_declared_interfaces 예제
  • Example#5578 - get_object_vars의 사용
  • Example#5579 - get_parent_class 사용하기
  • Example#5580 - interface_exists 예제
  • Example#5581 - is_a 예제
  • Example#5582 - PHP 5에서 instanceof 연산자 사용하기
  • Example#5583 - is_subclass_of 예제
  • Example#5584 - method_exists 예제
  • Example#5585 - 정적 method_exists 예제
  • Example#5586 - property_exists 예제
  • Example#5587 - classkit_import example
  • Example#5588 - classkit_method_add example
  • Example#5589 - classkit_method_copy example
  • Example#5590 - classkit_method_redefine example
  • Example#5591 - classkit_method_remove example
  • Example#5592 - classkit_method_rename example
  • Example#5593 - A ctype_alnum example (using the default locale)
  • Example#5594 - A ctype_alpha example (using the default locale)
  • Example#5595 - A ctype_cntrl example
  • Example#5596 - A ctype_digit example
  • Example#5597 - A ctype_digit example comparing strings with integers
  • Example#5598 - A ctype_graph example
  • Example#5599 - A ctype_lower example (using the default locale)
  • Example#5600 - A ctype_print example
  • Example#5601 - A ctype_punct example
  • Example#5602 - A ctype_space example
  • Example#5603 - A ctype_upper example (using the default locale)
  • Example#5604 - A ctype_xdigit example
  • Example#5605 - Vector
  • Example#5606 - Map
  • Example#5607 - Ds\Collection::clear example
  • Example#5608 - Ds\Collection::copy example
  • Example#5609 - Ds\Collection::isEmpty example
  • Example#5610 - Ds\Collection::toArray example
  • Example#5611 - Ds\Hashable::hash example
  • Example#5612 - Ds\Sequence::allocate example
  • Example#5613 - Ds\Sequence::apply example
  • Example#5614 - Ds\Sequence::capacity example
  • Example#5615 - Ds\Sequence::contains example
  • Example#5616 - Ds\Sequence::filter example using callback function
  • Example#5617 - Ds\Sequence::filter example without a callback function
  • Example#5618 - Ds\Sequence::find example
  • Example#5619 - Ds\Sequence::first example
  • Example#5620 - Ds\Sequence::get example
  • Example#5621 - Ds\Sequence::get example using array syntax
  • Example#5622 - Ds\Sequence::insert example
  • Example#5623 - Ds\Sequence::join example using a separator string
  • Example#5624 - Ds\Sequence::join example without a separator string
  • Example#5625 - Ds\Sequence::last example
  • Example#5626 - Ds\Sequence::map example
  • Example#5627 - Ds\Sequence::merge example
  • Example#5628 - Ds\Sequence::pop example
  • Example#5629 - Ds\Sequence::push example
  • Example#5630 - Ds\Sequence::reduce with initial value example
  • Example#5631 - Ds\Sequence::reduce without an initial value example
  • Example#5632 - Ds\Sequence::remove example
  • Example#5633 - Ds\Sequence::reverse example
  • Example#5634 - Ds\Sequence::reversed example
  • Example#5635 - Ds\Sequence::rotate example
  • Example#5636 - Ds\Sequence::set example
  • Example#5637 - Ds\Sequence::set example using array syntax
  • Example#5638 - Ds\Sequence::shift example
  • Example#5639 - Ds\Sequence::slice example
  • Example#5640 - Ds\Sequence::sort example
  • Example#5641 - Ds\Sequence::sort example using a comparator
  • Example#5642 - Ds\Sequence::sorted example
  • Example#5643 - Ds\Sequence::sorted example using a comparator
  • Example#5644 - Ds\Sequence::sum integer example
  • Example#5645 - Ds\Sequence::sum float example
  • Example#5646 - Ds\Sequence::unshift example
  • Example#5647 - Ds\Vector::allocate example
  • Example#5648 - Ds\Vector::apply example
  • Example#5649 - Ds\Vector::capacity example
  • Example#5650 - Ds\Vector::clear example
  • Example#5651 - Ds\Vector::__construct example
  • Example#5652 - Ds\Vector::contains example
  • Example#5653 - Ds\Vector::copy example
  • Example#5654 - Ds\Vector::filter example using callback function
  • Example#5655 - Ds\Vector::filter example without a callback function
  • Example#5656 - Ds\Vector::find example
  • Example#5657 - Ds\Vector::first example
  • Example#5658 - Ds\Vector::get example
  • Example#5659 - Ds\Vector::get example using array syntax
  • Example#5660 - Ds\Vector::insert example
  • Example#5661 - Ds\Vector::isEmpty example
  • Example#5662 - Ds\Vector::join example using a separator string
  • Example#5663 - Ds\Vector::join example without a separator string
  • Example#5664 - Ds\Vector::last example
  • Example#5665 - Ds\Vector::map example
  • Example#5666 - Ds\Vector::merge example
  • Example#5667 - Ds\Vector::pop example
  • Example#5668 - Ds\Vector::push example
  • Example#5669 - Ds\Vector::reduce with initial value example
  • Example#5670 - Ds\Vector::reduce without an initial value example
  • Example#5671 - Ds\Vector::remove example
  • Example#5672 - Ds\Vector::reverse example
  • Example#5673 - Ds\Vector::reversed example
  • Example#5674 - Ds\Vector::rotate example
  • Example#5675 - Ds\Vector::set example
  • Example#5676 - Ds\Vector::set example using array syntax
  • Example#5677 - Ds\Vector::shift example
  • Example#5678 - Ds\Vector::slice example
  • Example#5679 - Ds\Vector::sort example
  • Example#5680 - Ds\Vector::sort example using a comparator
  • Example#5681 - Ds\Vector::sorted example
  • Example#5682 - Ds\Vector::sorted example using a comparator
  • Example#5683 - Ds\Vector::sum integer example
  • Example#5684 - Ds\Vector::sum float example
  • Example#5685 - Ds\Vector::toArray example
  • Example#5686 - Ds\Vector::unshift example
  • Example#5687 - Ds\Deque::allocate example
  • Example#5688 - Ds\Deque::apply example
  • Example#5689 - Ds\Deque::capacity example
  • Example#5690 - Ds\Deque::clear example
  • Example#5691 - Ds\Deque::__construct example
  • Example#5692 - Ds\Deque::contains example
  • Example#5693 - Ds\Deque::copy example
  • Example#5694 - Ds\Deque::filter example using callback function
  • Example#5695 - Ds\Deque::filter example without a callback function
  • Example#5696 - Ds\Deque::find example
  • Example#5697 - Ds\Deque::first example
  • Example#5698 - Ds\Deque::get example
  • Example#5699 - Ds\Deque::get example using array syntax
  • Example#5700 - Ds\Deque::insert example
  • Example#5701 - Ds\Deque::isEmpty example
  • Example#5702 - Ds\Deque::join example using a separator string
  • Example#5703 - Ds\Deque::join example without a separator string
  • Example#5704 - Ds\Deque::last example
  • Example#5705 - Ds\Deque::map example
  • Example#5706 - Ds\Deque::merge example
  • Example#5707 - Ds\Deque::pop example
  • Example#5708 - Ds\Deque::push example
  • Example#5709 - Ds\Deque::reduce with initial value example
  • Example#5710 - Ds\Deque::reduce without an initial value example
  • Example#5711 - Ds\Deque::remove example
  • Example#5712 - Ds\Deque::reverse example
  • Example#5713 - Ds\Deque::reversed example
  • Example#5714 - Ds\Deque::rotate example
  • Example#5715 - Ds\Deque::set example
  • Example#5716 - Ds\Deque::set example using array syntax
  • Example#5717 - Ds\Deque::shift example
  • Example#5718 - Ds\Deque::slice example
  • Example#5719 - Ds\Deque::sort example
  • Example#5720 - Ds\Deque::sort example using a comparator
  • Example#5721 - Ds\Deque::sorted example
  • Example#5722 - Ds\Deque::sorted example using a comparator
  • Example#5723 - Ds\Deque::sum integer example
  • Example#5724 - Ds\Deque::sum float example
  • Example#5725 - Ds\Deque::toArray example
  • Example#5726 - Ds\Deque::unshift example
  • Example#5727 - Ds\Map::allocate example
  • Example#5728 - Ds\Map::apply example
  • Example#5729 - Ds\Map::capacity example
  • Example#5730 - Ds\Map::clear example
  • Example#5731 - Ds\Map::__construct example
  • Example#5732 - Ds\Map::copy example
  • Example#5733 - Ds\Map::diff example
  • Example#5734 - Ds\Map::filter example using callback function
  • Example#5735 - Ds\Map::filter example without a callback function
  • Example#5736 - Ds\Map::first example
  • Example#5737 - Ds\Map::get example
  • Example#5738 - Ds\Map::get example using array syntax
  • Example#5739 - Ds\Map::hasKey example
  • Example#5740 - Ds\Map::hasValue example
  • Example#5741 - Ds\Map::intersect example
  • Example#5742 - Ds\Map::isEmpty example
  • Example#5743 - Ds\Map::keys example
  • Example#5744 - Ds\Map::ksort example
  • Example#5745 - Ds\Map::ksort example using a comparator
  • Example#5746 - Ds\Map::ksorted example
  • Example#5747 - Ds\Map::ksorted example using a comparator
  • Example#5748 - Ds\Map::last example
  • Example#5749 - Ds\Map::map example
  • Example#5750 - Ds\Map::merge example
  • Example#5751 - Ds\Map::pairs example
  • Example#5752 - Ds\Map::put example
  • Example#5753 - Ds\Map::put example using objects as keys
  • Example#5754 - Ds\Map::putAll example
  • Example#5755 - Ds\Map::reduce with initial value example
  • Example#5756 - Ds\Map::reduce without an initial value example
  • Example#5757 - Ds\Map::remove example
  • Example#5758 - Ds\Map::reverse example
  • Example#5759 - Ds\Map::reversed example
  • Example#5760 - Ds\Map::skip example
  • Example#5761 - Ds\Map::slice example
  • Example#5762 - Ds\Map::sort example
  • Example#5763 - Ds\Map::sort example using a comparator
  • Example#5764 - Ds\Map::sort example
  • Example#5765 - Ds\Map::sort example using a comparator
  • Example#5766 - Ds\Map::sum integer example
  • Example#5767 - Ds\Map::sum float example
  • Example#5768 - Ds\Map::toArray example
  • Example#5769 - Ds\Map::union example
  • Example#5770 - Ds\Map::values example
  • Example#5771 - Ds\Map::xor example
  • Example#5772 - Ds\Pair::clear example
  • Example#5773 - Ds\Pair::copy example
  • Example#5774 - Ds\Pair::isEmpty example
  • Example#5775 - Ds\Pair::toArray example
  • Example#5776 - Ds\Set::add example using integers
  • Example#5777 - Ds\Set::add example using objects
  • Example#5778 - Ds\Set::allocate example
  • Example#5779 - Ds\Set::capacity example
  • Example#5780 - Ds\Set::clear example
  • Example#5781 - Ds\Set::__construct example
  • Example#5782 - Ds\Set::contains example
  • Example#5783 - Ds\Set::copy example
  • Example#5784 - Ds\Set::diff example
  • Example#5785 - Ds\Set::filter example using callback function
  • Example#5786 - Ds\Set::filter example without a callback function
  • Example#5787 - Ds\Set::first example
  • Example#5788 - Ds\Set::get example
  • Example#5789 - Ds\Set::get example using array syntax
  • Example#5790 - Ds\Set::intersect example
  • Example#5791 - Ds\Set::isEmpty example
  • Example#5792 - Ds\Set::join example using a separator string
  • Example#5793 - Ds\Set::join example without a separator string
  • Example#5794 - Ds\Set::last example
  • Example#5795 - Ds\Set::merge example
  • Example#5796 - Ds\Set::reduce with initial value example
  • Example#5797 - Ds\Set::reduce without an initial value example
  • Example#5798 - Ds\Set::remove example
  • Example#5799 - Ds\Set::reverse example
  • Example#5800 - Ds\Set::reversed example
  • Example#5801 - Ds\Set::slice example
  • Example#5802 - Ds\Set::sort example
  • Example#5803 - Ds\Set::sort example using a comparator
  • Example#5804 - Ds\Set::sorted example
  • Example#5805 - Ds\Set::sorted example using a comparator
  • Example#5806 - Ds\Set::sum integer example
  • Example#5807 - Ds\Set::sum float example
  • Example#5808 - Ds\Set::toArray example
  • Example#5809 - Ds\Set::union example
  • Example#5810 - Ds\Set::xor example
  • Example#5811 - Ds\Stack::clear example
  • Example#5812 - Ds\Stack::__construct example
  • Example#5813 - Ds\Stack::copy example
  • Example#5814 - Ds\Stack::isEmpty example
  • Example#5815 - Ds\Stack::peek example
  • Example#5816 - Ds\Stack::pop example
  • Example#5817 - Ds\Stack::push example
  • Example#5818 - Ds\Stack::toArray example
  • Example#5819 - Ds\Queue::allocate example
  • Example#5820 - Ds\Queue::capacity example
  • Example#5821 - Ds\Queue::clear example
  • Example#5822 - Ds\Queue::__construct example
  • Example#5823 - Ds\Queue::copy example
  • Example#5824 - Ds\Queue::isEmpty example
  • Example#5825 - Ds\Queue::peek example
  • Example#5826 - Ds\Queue::pop example
  • Example#5827 - Ds\Queue::push example
  • Example#5828 - Ds\Queue::toArray example
  • Example#5829 - Ds\PriorityQueue::allocate example
  • Example#5830 - Ds\PriorityQueue::capacity example
  • Example#5831 - Ds\PriorityQueue::clear example
  • Example#5832 - Ds\PriorityQueue::__construct example
  • Example#5833 - Ds\PriorityQueue::copy example
  • Example#5834 - Ds\PriorityQueue::isEmpty example
  • Example#5835 - Ds\PriorityQueue::peek example
  • Example#5836 - Ds\PriorityQueue::pop example
  • Example#5837 - Ds\PriorityQueue::push example
  • Example#5838 - Ds\PriorityQueue::toArray example
  • Example#5839 - Configuring the default filter to act like htmlspecialchars
  • Example#5840 - Configuring the default filter to act like htmlspecialchars
  • Example#5841 - Validating email addresses with filter_var
  • Example#5842 - Validating IP addresses with filter_var
  • Example#5843 - Passing options to filter_var
  • Example#5844 - Sanitizing and validating email addresses
  • Example#5845 - Configuring a default filter
  • Example#5846 - A filter_input_array example
  • Example#5847 - A filter_input example
  • Example#5848 - A filter_list example
  • Example#5849 - A filter_var_array example
  • Example#5850 - A filter_var example
  • Example#5851 - call_user_func_array example
  • Example#5852 - call_user_func_array using namespace name
  • Example#5853 - Using lambda function
  • Example#5854 - Passing values by reference
  • Example#5855 - call_user_func example and references
  • Example#5856 - call_user_func example
  • Example#5857 - call_user_func using namespace name
  • Example#5858 - Using a class method with call_user_func
  • Example#5859 - Using lambda function with call_user_func
  • Example#5860 - Creating an anonymous function with create_function
  • Example#5861 - Making a general processing function with create_function
  • Example#5862 - Using anonymous functions as callback functions
  • Example#5863 - forward_static_call_array example
  • Example#5864 - forward_static_call example
  • Example#5865 - func_get_arg example
  • Example#5866 - func_get_arg example before and after PHP 5.3
  • Example#5867 - func_get_arg example of byref and byval arguments
  • Example#5868 - func_get_args example
  • Example#5869 - func_get_args example before and after PHP 5.3
  • Example#5870 - func_get_args example of byref and byval arguments
  • Example#5871 - func_num_args example
  • Example#5872 - func_num_args example before and after PHP 5.3
  • Example#5873 - function_exists example
  • Example#5874 - get_defined_functions example
  • Example#5875 - register_shutdown_function example
  • Example#5876 - register_tick_function example
  • Example#5877 - Quickhash Example
  • Example#5878 - Quickhash ArrayAccess Example
  • Example#5879 - Quickhash Iterator Example
  • Example#5880 - Quickhash String Values Example
  • Example#5881 - QuickHashIntSet::add example
  • Example#5882 - QuickHashIntSet::__construct example
  • Example#5883 - QuickHashIntSet::delete example
  • Example#5884 - QuickHashIntSet::exists example
  • Example#5885 - QuickHashIntSet::getSize example
  • Example#5886 - QuickHashIntSet::loadFromFile example
  • Example#5887 - QuickHashIntSet::loadFromString example
  • Example#5888 - QuickHashIntSet::saveToFile example
  • Example#5889 - QuickHashIntSet::saveToString example
  • Example#5890 - QuickHashIntHash::add example
  • Example#5891 - QuickHashIntHash::__construct example
  • Example#5892 - QuickHashIntHash::delete example
  • Example#5893 - QuickHashIntHash::exists example
  • Example#5894 - QuickHashIntHash::get example
  • Example#5895 - QuickHashIntHash::getSize example
  • Example#5896 - QuickHash IntHash file format
  • Example#5897 - QuickHash IntHash file format
  • Example#5898 - QuickHashIntHash::loadFromFile example
  • Example#5899 - QuickHashIntHash::loadFromString example
  • Example#5900 - QuickHashIntHash::saveToFile example
  • Example#5901 - QuickHashIntHash::saveToString example
  • Example#5902 - QuickHashIntHash::set example
  • Example#5903 - QuickHashIntHash::update example
  • Example#5904 - QuickHashStringIntHash::add example
  • Example#5905 - QuickHashStringIntHash::__construct example
  • Example#5906 - QuickHashStringIntHash::delete example
  • Example#5907 - QuickHashStringIntHash::get example
  • Example#5908 - QuickHashStringIntHash::getSize example
  • Example#5909 - QuickHash StringIntHash file format
  • Example#5910 - QuickHash IntHash file format
  • Example#5911 - QuickHashStringIntHash::loadFromFile example
  • Example#5912 - QuickHashStringIntHash::loadFromString example
  • Example#5913 - QuickHashStringIntHash::saveToFile example
  • Example#5914 - QuickHashStringIntHash::saveToString example
  • Example#5915 - QuickHashStringIntHash::set example
  • Example#5916 - QuickHashStringIntHash::update example
  • Example#5917 - QuickHashIntStringHash::add example
  • Example#5918 - QuickHashIntStringHash::__construct example
  • Example#5919 - QuickHashIntStringHash::delete example
  • Example#5920 - QuickHashIntStringHash::get example
  • Example#5921 - QuickHashIntStringHash::getSize example
  • Example#5922 - QuickHash IntString file format
  • Example#5923 - QuickHash IntString file format
  • Example#5924 - QuickHashIntStringHash::loadFromFile example
  • Example#5925 - QuickHashIntStringHash::loadFromString example
  • Example#5926 - QuickHashIntStringHash::saveToFile example
  • Example#5927 - QuickHashIntStringHash::saveToString example
  • Example#5928 - QuickHashIntStringHash::set example
  • Example#5929 - QuickHashIntStringHash::update example
  • Example#5930 - Reflection Example from Shell (a Terminal)
  • Example#5931 - Extending the built-in classes
  • Example#5932 - Basic usage ReflectionClass
  • Example#5933 - Basic usage of ReflectionClass::export
  • Example#5934 - Basic usage of ReflectionClass::getConstructor
  • Example#5935 - ReflectionClass::getDefaultProperties example
  • Example#5936 - ReflectionClass::getDocComment example
  • Example#5937 - ReflectionClass::getEndLine example
  • Example#5938 - Basic usage of ReflectionClass::getExtension
  • Example#5939 - Basic usage of ReflectionClass::getExtensionName
  • Example#5940 - ReflectionClass::getInterfaceNames example
  • Example#5941 - ReflectionClass::getInterfaces example
  • Example#5942 - Basic usage of ReflectionClass::getMethod
  • Example#5943 - Basic usage of ReflectionClass::getMethods
  • Example#5944 - Filtering results from ReflectionClass::getMethods
  • Example#5945 - ReflectionClass::getName example
  • Example#5946 - ReflectionClass::getNamespaceName example
  • Example#5947 - ReflectionClass::getProperties filtering example
  • Example#5948 - Basic usage of ReflectionClass::getProperty
  • Example#5949 - ReflectionClass::getShortName example
  • Example#5950 - Basic usage of ReflectionClass::getStaticPropertyValue
  • Example#5951 - ReflectionClass::hasConstant example
  • Example#5952 - ReflectionClass::hasMethod example
  • Example#5953 - ReflectionClass::hasProperty example
  • Example#5954 - ReflectionClass::inNamespace example
  • Example#5955 - ReflectionClass::isAbstract example
  • Example#5956 - ReflectionClass::isAnonymous example
  • Example#5957 - Basic usage of ReflectionClass::isCloneable
  • Example#5958 - ReflectionClass::isFinal example
  • Example#5959 - ReflectionClass::isInstance related examples
  • Example#5960 - ReflectionClass::isInstantiable example
  • Example#5961 - Basic usage of ReflectionClass::isInterface
  • Example#5962 - Basic usage of ReflectionClass::isInternal
  • Example#5963 - ReflectionClass::isIterateable example
  • Example#5964 - Basic usage of ReflectionClass::newInstanceArgs
  • Example#5965 - ReflectionClass::__toString example
  • Example#5966 - ReflectionExtension example
  • Example#5967 - ReflectionExtension::getClasses example
  • Example#5968 - ReflectionExtension::getClassNames example
  • Example#5969 - ReflectionExtension::getConstants example
  • Example#5970 - ReflectionExtension::getDependencies example
  • Example#5971 - ReflectionExtension::getFunctions example
  • Example#5972 - ReflectionExtension::getINIEntries example
  • Example#5973 - ReflectionExtension::getName example
  • Example#5974 - ReflectionExtension::getVersion example
  • Example#5975 - ReflectionExtension::info example
  • Example#5976 - ReflectionFunction::__construct example
  • Example#5977 - ReflectionFunction::invoke example
  • Example#5978 - ReflectionFunction::invokeArgs example
  • Example#5979 - ReflectionFunction::invokeArgs with references example
  • Example#5980 - ReflectionFunction::__toString example
  • Example#5981 - ReflectionFunctionAbstract::getReturnType example
  • Example#5982 - Usage on built-in functions
  • Example#5983 - ReflectionFunctionAbstract::hasReturnType example
  • Example#5984 - Usage on built-in functions
  • Example#5985 - ReflectionFunctionAbstract::isClosure example
  • Example#5986 - ReflectionFunctionAbstract::isDeprecated example
  • Example#5987 - ReflectionMethod::__construct example
  • Example#5988 - ReflectionMethod::getDeclaringClass example
  • Example#5989 - ReflectionMethod::getModifiers example
  • Example#5990 - ReflectionMethod::getPrototype example
  • Example#5991 - ReflectionMethod::invoke example
  • Example#5992 - ReflectionMethod::invokeArgs example
  • Example#5993 - ReflectionMethod::__toString example
  • Example#5994 - Using the ReflectionParameter class
  • Example#5995 - Using the ReflectionParameter class
  • Example#5996 - Getting the class that declared the method
  • Example#5997 - Getting
  • Example#5998 - ReflectionParameter::getType example
  • Example#5999 - ReflectionParameter::hasType example
  • Example#6000 - ReflectionProperty::__construct example
  • Example#6001 - Getting value from private and protected properties using ReflectionProperty class
  • Example#6002 - ReflectionProperty::getDocComment example
  • Example#6003 - ReflectionProperty::getValue example
  • Example#6004 - ReflectionClass::isDefault example
  • Example#6005 - ReflectionProperty::setValue example
  • Example#6006 - ReflectionType::allowsNull example
  • Example#6007 - ReflectionType::isBuiltin example
  • Example#6008 - ReflectionType::__toString example
  • Example#6009 - ReflectionGenerator::__construct example
  • Example#6010 - ReflectionGenerator::getExecutingFile example
  • Example#6011 - ReflectionGenerator::getExecutingGenerator example
  • Example#6012 - ReflectionGenerator::getExecutingLine example
  • Example#6013 - ReflectionGenerator::getFunction example
  • Example#6014 - ReflectionGenerator::getThis example
  • Example#6015 - ReflectionGenerator::getTrace example
  • Example#6016 - boolval examples
  • Example#6017 - debug_zval_dump example
  • Example#6018 -
  • Example#6019 -
  • Example#6020 - 간단한 empty / isset 비교입니다.
  • Example#6021 - floatval 예제
  • Example#6022 - get_defined_vars 예제
  • Example#6023 - get_resource_type 예제
  • Example#6024 - gettype 예제
  • Example#6025 - import_request_variables 예제
  • Example#6026 - intval 예제
  • Example#6027 - 변수가 배열인지 확인하기
  • Example#6028 - is_bool 예제
  • Example#6029 -
  • Example#6030 - is_float 예제
  • Example#6031 - is_int 예제
  • Example#6032 - is_null 예제
  • Example#6033 - is_numeric 예제
  • Example#6034 - is_object 예제
  • Example#6035 - is_resource 예제
  • Example#6036 - is_scalar 예제
  • Example#6037 - is_string 예제
  • Example#6038 - isset 예제
  • Example#6039 - print_r 예제
  • Example#6040 - return 인수 예제
  • Example#6041 - serialize 예제
  • Example#6042 - settype 예제
  • Example#6043 - PHP5의 __toString 마법 메쏘드를 사용하는 strval 예제
  • Example#6044 - unserialize 예제
  • Example#6045 - unserialize_callback_func 예제
  • Example#6046 - unset 예제
  • Example#6047 - (unset) 캐스팅 사용하기
  • Example#6048 - var_dump 예제
  • Example#6049 - var_export 예제
  • Example#6050 - PHP 5.1.0부터 클래스 내보내기
  • Example#6051 - __set_state 사용하기 (PHP 5.1.0부터)
  • Example#6052 -
  • Example#6053 - OAuth::fetch example
  • Example#6054 - OAuth::getAccessToken example
  • Example#6055 - OAuth::getRequestToken example
  • Example#6056 - OAuth::setRequestEngine example
  • Example#6057 - An OAuth::setRsaCertificate example
  • Example#6058 - OAuth::setToken example
  • Example#6059 - OAuthProvider::__construct example
  • Example#6060 - Example OAuthProvider::consumerHandler callback
  • Example#6061 - OAuthProvider::generateToken example
  • Example#6062 - OAuthProvider::is2LeggedEndpoint example
  • Example#6063 - Example OAuthProvider::timestampNonceHandler callback
  • Example#6064 - Example OAuthProvider::tokenHandler callback
  • Example#6065 - A sample SCA component
  • Example#6066 - The structure of an SCA for PHP component
  • Example#6067 - Obtaining a proxy for a local PHP class
  • Example#6068 - Obtaining a proxy for a web service
  • Example#6069 - Calling services
  • Example#6070 - Obtaining a proxy using getService
  • Example#6071 - Making calls on the proxy
  • Example#6072 - StockQuote Service
  • Example#6073 - Generated WSDL
  • Example#6074 - Generated WSDL
  • Example#6075 - location attribute
  • Example#6076 - method with two arguments
  • Example#6077 - types section illustrating named parameters
  • Example#6078 - A Component that uses Data Structures
  • Example#6079 - An SCA::getService example
  • Example#6080 - is_soap_fault example
  • Example#6081 - SOAP's standard method for error reporting is exceptions
  • Example#6082 - SoapClient::__doRequest example
  • Example#6083 - SoapClient::__getFunctions example
  • Example#6084 - SoapClient::__getLastRequest() example
  • Example#6085 - SoapClient::__getLastRequestHeaders() example
  • Example#6086 - SoapClient::__getLastResponse() example
  • Example#6087 - SoapClient::__getLastResponse() example
  • Example#6088 - SoapClient::__getTypes example
  • Example#6089 - SoapClient::__setLocation example
  • Example#6090 - SoapClient::__setSoapHeaders example
  • Example#6091 - Set Multiple Headers
  • Example#6092 - SoapClient::__soapCall example
  • Example#6093 - SoapClient::SoapClient example
  • Example#6094 - SoapServer::addFunction example
  • Example#6095 - SoapServer::getFunctions example
  • Example#6096 - SoapServer::handle example
  • Example#6097 - SoapServer::setPersistence example
  • Example#6098 - SoapServer::SoapServer example
  • Example#6099 - Some examples
  • Example#6100 - Some examples
  • Example#6101 - SoapHeader::SoapHeader example
  • Example#6102 - SoapParam::SoapParam example
  • Example#6103 - SoapVar::SoapVar example
  • Example#6104 - Yar Server Example
  • Example#6105 - Access the server in borwser(GET request)
  • Example#6106 - Yar Client Example
  • Example#6107 - Yar Concurrent Client Example
  • Example#6108 - Yar_Server::__construct example
  • Example#6109 - Yar_Server::handle example
  • Example#6110 - Yar_Client::__call example
  • Example#6111 - Yar_Client::__construct example
  • Example#6112 - Yar_Client::setOpt example
  • Example#6113 - Yar_Concurrent_Client::call example
  • Example#6114 - Yar_Concurrent_Client::loop example
  • Example#6115 - Yar_Concurrent_Client::reset example
  • Example#6116 - Yar_Server_Exception::getType example
  • Example#6117 - Yar_Client_Exception::getType example
  • Example#6118 - XMLRPC client functions example
  • Example#6119 - XML-RPC type example
  • Example#6120 - A xmlrpc_set_type example
  • Example#6121 - For Each in ASP
  • Example#6122 - while() ... Next() in PHP 4
  • Example#6123 - foreach in PHP 5
  • Example#6124 - COM example (1)
  • Example#6125 - COM example (2)
  • Example#6126 - DOTNET example
  • Example#6127 - Variant example, PHP 4.x style
  • Example#6128 - Variant example, PHP 5 style
  • Example#6129 - COM event sink example
  • Example#6130 - Statistics about the current PHP process
  • Example#6131 - Statistics about global memory utilization
  • Example#6132 - Registering a PHP script to run as a service
  • Example#6133 - Unregistering a service
  • Example#6134 - Running as a service
  • Example#6135 - A win32_create_service example
  • Example#6136 - A win32_delete_service example
  • Example#6137 - A win32_start_service_ctrl_dispatcher example
  • Example#6138 - book.xml
  • Example#6139 - Creating a new DOMAttr object
  • Example#6140 - DOMAttr::isId() Example
  • Example#6141 - Creating a new DOMCdataSection object
  • Example#6142 - Creating a new DOMComment
  • Example#6143 - Creating a new DOMDocument
  • Example#6144 - Creating a new element and inserting it as root
  • Example#6145 - Passing text containing an unescaped & as value
  • Example#6146 - Creating a new element and inserting it as root
  • Example#6147 - A namespace prefix example
  • Example#6148 - DOMDocument::getElementById() Example
  • Example#6149 - Basic Usage Example
  • Example#6150 - Get all the XInclude elements
  • Example#6151 - DOMDocument::importNode example
  • Example#6152 - Creating a Document
  • Example#6153 - Creating a Document
  • Example#6154 - Creating a Document
  • Example#6155 - Creating a Document
  • Example#6156 - Static invocation of loadXML
  • Example#6157 - Adding a new method to DOMElement to ease our code
  • Example#6158 - Retrieving elements as custom class
  • Example#6159 - Retrieving owner document
  • Example#6160 - Saving a DOM tree into a file
  • Example#6161 - Saving a HTML tree into a string
  • Example#6162 - Saving a HTML tree into a file
  • Example#6163 - Saving a DOM tree into a string
  • Example#6164 - Example of DTD validation
  • Example#6165 - DOMDocument::xinclude() example
  • Example#6166 - Appending XML data to your document
  • Example#6167 - Creating a new DOMElement
  • Example#6168 - Setting an attribute
  • Example#6169 - Creating a new DOMEntityReference
  • Example#6170 - Creating a document with an attached DTD
  • Example#6171 - Testing your DOM Implementation
  • Example#6172 - Adding a child
  • Example#6173 - DOMNode::getLineNo example
  • Example#6174 - DOMNode::getNodePath example
  • Example#6175 - Removing a child
  • Example#6176 - Traversing all the entries of the table
  • Example#6177 - Creating a new DOMProcessingInstruction object
  • Example#6178 - Creating a new DOMText
  • Example#6179 - Getting the count of all the english books
  • Example#6180 - Getting all the english books
  • Example#6181 - book.xml
  • Example#6182 - DOMXPath::registerPHPFunctions with php:functionString
  • Example#6183 - DOMXPath::registerPHPFunctions with php:function
  • Example#6184 - Import SimpleXML into DOM with dom_import_simplexml
  • Example#6185 - A libxml_get_errors example
  • Example#6186 - libxml_set_external_entity_loader example
  • Example#6187 - A libxml_set_streams_context example
  • Example#6188 - A libxml_use_internal_errors example
  • Example#6189 - Access via property name
  • Example#6190 - Access via property name as array index
  • Example#6191 - Data Object iteration
  • Example#6192 - Access many-valued property by name
  • Example#6193 - Many-valued element access
  • Example#6194 - Many-valued property iteration
  • Example#6195 - Chained property access
  • Example#6196 - XPath navigation
  • Example#6197 - XPath querying
  • Example#6198 - Creating child data objects
  • Example#6199 - Unset a primitive property
  • Example#6200 - Unset a data object
  • Example#6201 - Unset a referenced data object
  • Example#6202 - Access via property index
  • Example#6203 - Getting the SDO_Sequence interface
  • Example#6204 - Get/set sequence values
  • Example#6205 - Sequence iteration
  • Example#6206 - Sequence versus Data Object
  • Example#6207 - Adding to a sequence
  • Example#6208 - Removing from a sequence
  • Example#6209 - Reflecting on a Data Object
  • Example#6210 - Accessing the type information
  • Example#6211 - A SDO_DAS_DataFactory::addPropertyToType example
  • Example#6212 - A SDO_DAS_DataFactory::addType example
  • Example#6213 - Creating a data object
  • Example#6214 - Retrieving a data object
  • Example#6215 - Updating a data object
  • Example#6216 - Deleting a data object
  • Example#6217 - One company, one department - Create
  • Example#6218 - One company, one department - Retrieve and Update
  • Example#6219 - One company, two departments - Retrieve and Delete
  • Example#6220 - One company, one department, one employee - Create
  • Example#6221 - One company, one department, one employee - Retrieve and update
  • Example#6222 - One company, two departments, two employees - Retrieve and delete
  • Example#6223 - Retrieving a data object using executePreparedQuery
  • Example#6224 - Loading, altering, and saving an XML document
  • Example#6225 - Creating a new XML document
  • Example#6226 - Setting XML document properties
  • Example#6227 - Using an open type
  • Example#6228 - Finding out what you can from the document
  • Example#6229 - Printing the SDO model
  • Example#6230 - XML 문자열을 가진 포함 파일 example.php
  • Example#6231 - <plot> 가져오기.
  • Example#6232 - <line> 가져오기.
  • Example#6233 - SimpleXML에서 다중 요소에 접근하기.
  • Example#6234 - 속성 사용하기
  • Example#6235 - 요소와 속성을 텍스트와 비교하기.
  • Example#6236 - 두 요소 비교하기.
  • Example#6237 - XPath 사용하기.
  • Example#6238 - 값 설정하기.
  • Example#6239 - 요소와 속성 추가하기.
  • Example#6240 - DOM 상호 작용.
  • Example#6241 - Loading broken XML string
  • Example#6242 - Add attributes and children to a SimpleXML element
  • Example#6243 - Add attributes and children to a SimpleXML element
  • Example#6244 - Get XML
  • Example#6245 - Using asXML() on SimpleXMLElement::xpath results
  • Example#6246 - Interpret an XML string
  • Example#6247 - Traversing a children() pseudo-array
  • Example#6248 - Using namespaces
  • Example#6249 - Create a SimpleXMLElement object
  • Example#6250 - Create a SimpleXMLElement object from a URL
  • Example#6251 - Counting the number of children
  • Example#6252 - Get document namespaces
  • Example#6253 - Working with multiple namespaces
  • Example#6254 - Get XML element names
  • Example#6255 - Get document namespaces in use
  • Example#6256 - Setting a namespace prefix to use in an XPath query
  • Example#6257 - Get string content
  • Example#6258 - Xpath
  • Example#6259 - Importing DOM
  • Example#6260 - Interpret an XML document
  • Example#6261 - Interpret an XML string
  • Example#6262 - Serializing a single value with WDDX
  • Example#6263 - Using incremental packets with WDDX
  • Example#6264 - wddx_serialize_vars example
  • Example#6265 - Show XML Element Structure
  • Example#6266 - Map XML to HTML
  • Example#6267 - External Entity Example
  • Example#6268 - xmltest.xml
  • Example#6269 - xmltest2.xml
  • Example#6270 - xml_parse_into_struct example
  • Example#6271 - moldb.xml - small database of molecular information
  • Example#6272 - parsemoldb.php - parses moldb.xml into an array of molecular objects
  • Example#6273 - xml_set_object example
  • Example#6274 - Validating XML
  • Example#6275 - collection.xml
  • Example#6276 - collection.xsl
  • Example#6277 - fruits.xml
  • Example#6278 - fruits.xsl
  • Example#6279 - Collating and printing errors
  • Example#6280 - Creating an XSLTProcessor
  • Example#6281 - Testing EXSLT support
  • Example#6282 - Simple PHP Function call from a stylesheet
  • Example#6283 - Changing the owner before the transformation
  • Example#6284 - Example profiling output
  • Example#6285 - Transforming to a DOMDocument
  • Example#6286 - Transforming to a HTML file
  • Example#6287 - Transforming to a string
  • Example#6288 - Leak Detection in Action
  • Example#6289 - Accessor macros for per-module globals
  • Example#6290 - "counter"'s basic interface
  • Example#6291 - "counter"'s extended interface
  • Example#6292 - "counter"'s objective interface
  • Example#6293 - An example config.m4 file
  • Example#6294 - Sample configure output
  • Example#6295 - counter's config.m4 file
  • Example#6296 - An example config.w32 file
  • Example#6297 - counter's config.w32 file
  • Example#6298 - Files in the counter extension, in no particular order
  • Example#6299 - zend_module declaration in the counter extension
  • Example#6300 - zend_module definition in PHP 5.3
  • Example#6301 - Counter extension module definition
  • Example#6302 - The wrong way to store the basic counter interface's value
  • Example#6303 - The counter module's globals
  • Example#6304 - The counter module's global structure declaration
  • Example#6305 - Accessor macros for per-module globals
  • Example#6306 - The right way to store the basic counter interface's value
  • Example#6307 - counter's PHP_MINFO function
  • Example#6308 - Example macros for invoking pdo_SKEL_error
  • Example#6309 - Using pdo_parse_params
  • Example#6310 - Implementing preparer for drivers that don't support native prepared statements
  • Example#6311 - simple stream example that displays the PHP home page
  • Example#6312 - How to accept a stream as a parameter
  • Example#6313 - How to return a stream from a function
  • Example#6314 - The default config.m4.
  • Example#6315 - A simple extension.
  • Example#6316 - A test file for first_module.so.
  • Example#6317 - Internal declaration of zend_function_entry.
  • Example#6318 - Internal declaration of zend_module_entry.
  • Example#6319 - PHP's implementation of variable arguments in fsockopen().
  • Example#6320 - PHP/Zend zval type definition.
  • Example#6321 - Testing for referenced parameter passing.
  • Example#6322 - Creating variables with different scopes.
  • Example#6323 - Creation of a long.
  • Example#6324 - Adding an element to an associative array.
  • Example#6325 - Adding an element to an indexed array.
  • Example#6326 - Source code and screenshot for output in phpinfo.
  • Example#6327 - Printing execution information.
  • Example#6328 - Calling user functions.
  • Example#6329 - 숨겨진 HTML 폼의 엘리먼트(element)
  • Example#6330 - 유저에 의해 조장되는 데이터
  • Example#6331 - URL 안에서
  • Example#6332 - PHP로 자바스크립트 생성하기
  • Example#6333 - Register Globals 흉내내기
  • Example#6334 - In PHP Core
  • Example#6335 - Object Oriented Code in PHP Core
  • Example#6336 - In the bzip2 Extension
  • Example#6337 - In the datetime Extension
  • Example#6338 - In the dBase Extension
  • Example#6339 - In the mcrypt Extension
  • Example#6340 - In the oci8 Extension
  • Example#6341 - In the SPL Extension
  • Example#6342 - In the Semaphore (sysvmsg) extension
  • Example#6343 - A 5.2.1+ Zip Example
  • Example#6344 - strrpos와 strripos가 전체 문자열을 needle로 사용합니다.
  • Example#6345 - 프로퍼티가 없는 객체를 더 이상 "빈" 것으로 판단하지 않습니다.
  • Example#6346 - 몇몇 경우에 클래스는 사용하기 전에 선언해야 합니다.
  • Example#6347 - PHP 5로 아파치 설정 파일을 이행하기.
  • Example#6348 - PHP CGI 모드를 위한 아파치 설정 파일 이행하기.
  • Example#6349 - Example of object comparison in PHP 4
  • Example#6350 - Compound object comparisons in PHP 4
  • Example#6351 - Activate full on-screen error reporting for dev. domain
  • Example#6352 - Add security script for protected areas
  • Example#6353 - Unix include_path
  • Example#6354 - Windows include_path
  • Example#6355 - Unix include_path using ${USER} env variable
  • Example#6356 - string.rot13
  • Example#6357 - string.toupper
  • Example#6358 - string.tolower
  • Example#6359 - string.strip_tags
  • Example#6360 - convert.base64-encode & convert.base64-decode
  • Example#6361 - convert.quoted-printable-encode & convert.quoted-printable-decode
  • Example#6362 - zlib.deflate and zlib.inflate
  • Example#6363 - zlib.deflate simple
  • Example#6364 - bzip2.compress and bzip2.decompress
  • Example#6365 - Encrypting file output using 3DES
  • Example#6366 - Reading an encrypted file

User Contributed Notes

There are no user contributed notes for this page.


변경점

번들 확장 함수에 다음과 같은 변경이 이루어졌습니다.

Version | Function | Description
PHP 7.2.0 | imageantialias | imageantialias is now generally available. Formerly it was only available if PHP was compiled with the bundled version of the GD library.
imagelayereffect | Added IMG_EFFECT_MULTIPLY (requires system libgd >= 2.1.1 or the bundled libgd).
PHP 7.1.0 | mb_ereg | mb_ereg will now set regs to an empty array, if nothing matched. Formerly, regs was not modified in that case.
mb_eregi | mb_eregi will now set regs to an empty array, if nothing matched. Formerly, regs was not modified in that case.
PHP 7.0.0 | shmop_close | The type of shmid has been changed from int to resource.
shmop_delete | The type of shmid has been changed from int to resource.
shmop_open | The return type of shmop_open has been changed from int to resource.
shmop_read | The type of shmid has been changed from int to resource.
shmop_size | The type of shmid has been changed from int to resource.
shmop_write | The type of shmid has been changed from int to resource.
PHP 5.3.19 | imagecreatefromxpm | imagecreatefromxpm is available on Windows.
PHP 5.3.3 | imap_fetch_overview | Add the update property.
PHP 5 | empty | PHP 5부터, 프로퍼티가 없는 객체는 빈 것으로 간주되지 않습니다.
PHP 4 | empty | PHP 4부터, 문자열 "0"은 빈 것으로 간주합니다.
dbase 7.0.0 | dbase_add_record | dbase_identifier is now a resource instead of an int.
dbase_close | dbase_identifier is now a resource instead of an int.
dbase_create | The return value is now a resource instead of an int.
dbase_delete_record | dbase_identifier is now a resource instead of an int.
dbase_get_header_info | dbase_identifier is now a resource instead of an int.
dbase_get_record | dbase_identifier is now a resource instead of an int.
dbase_get_record_with_names | dbase_identifier is now a resource instead of an int.
dbase_numfields | dbase_identifier is now a resource instead of an int.
dbase_numrecords | dbase_identifier is now a resource instead of an int.
dbase_open | The return value is now a resource instead of an int.
dbase_pack | dbase_identifier is now a resource instead of an int.
dbase_replace_record | dbase_identifier is now a resource instead of an int.
7.2.0 | imagegd | imagegd now allows to output truecolor images. Formerly, these have been implicitly converted to palette.
proc_nice | This function is now available on Windows.
7.1.0 | getimagesize | Added WebP support.
getopt | Added the optind parameter.
grapheme_extract | Support for negative starts has been added.
grapheme_stripos | Support for negative offsets has been added.
grapheme_strpos | Support for negative offsets has been added.
iconv_strpos | Support for negative offsets has been added.
mb_ereg_replace | The e modifier has been deprecated.
mb_ereg_search_setpos | Support for negative positions has been added.
mb_eregi_replace | The e modifier has been deprecated.
mb_strimwidth | Support for negative starts and widths has been added.
mb_stripos | Support for negative offsets has been added.
mb_strpos | Support for negative offsets has been added.
output_add_rewrite_var | Before PHP 7.1.0, rewrite vars set by output_add_rewrite_var use the same Session module trans sid output buffer. Since PHP 7.1.0, dedicated output buffer is used, url_rewriter.tags is used solely for output functions, url_rewriter.hosts is addedd.
output_reset_rewrite_vars | Before PHP 7.1.0, rewrite vars set by output_add_rewrite_var use the same Session module trans sid output buffer. Since PHP 7.1.0, dedicated output buffer is used and output_reset_rewrite_vars only removes rewrite vars defined by output_add_rewrite_var.
substr_count | Support for negative offsets and lengths has been added.
7.0.7 | curl_multi_setopt | Introduced CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, CURLMOPT_MAX_HOST_CONNECTIONS, CURLMOPT_MAX_PIPELINE_LENGTH and CURLMOPT_MAX_TOTAL_CONNECTIONS.
curl_setopt | Introduced CURL_HTTP_VERSION_2, CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE, CURL_HTTP_VERSION_2TLS, CURL_REDIR_POST_301, CURL_REDIR_POST_302, CURL_REDIR_POST_303, CURL_REDIR_POST_ALL, CURL_VERSION_KERBEROS5, CURL_VERSION_PSL, CURL_VERSION_UNIX_SOCKETS, CURLAUTH_NEGOTIATE, CURLAUTH_NTLM_WB, CURLFTP_CREATE_DIR, CURLFTP_CREATE_DIR_NONE, CURLFTP_CREATE_DIR_RETRY, CURLHEADER_SEPARATE, CURLHEADER_UNIFIED, CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, CURLMOPT_MAX_HOST_CONNECTIONS, CURLMOPT_MAX_PIPELINE_LENGTH, CURLMOPT_MAX_TOTAL_CONNECTIONS, CURLOPT_CONNECT_TO, CURLOPT_DEFAULT_PROTOCOL, CURLOPT_DNS_INTERFACE, CURLOPT_DNS_LOCAL_IP4, CURLOPT_DNS_LOCAL_IP6, CURLOPT_EXPECT_100_TIMEOUT_MS, CURLOPT_HEADEROPT, CURLOPT_LOGIN_OPTIONS, CURLOPT_PATH_AS_IS, CURLOPT_PINNEDPUBLICKEY, CURLOPT_PIPEWAIT, CURLOPT_PROXY_SERVICE_NAME, CURLOPT_PROXYHEADER, CURLOPT_SASL_IR, CURLOPT_SERVICE_NAME, CURLOPT_SSL_ENABLE_ALPN, CURLOPT_SSL_ENABLE_NPN, CURLOPT_SSL_FALSESTART, CURLOPT_SSL_VERIFYSTATUS, CURLOPT_STREAM_WEIGHT, CURLOPT_TCP_FASTOPEN, CURLOPT_TFTP_NO_OPTIONS, CURLOPT_UNIX_SOCKET_PATH, CURLOPT_XOAUTH2_BEARER, CURLPROTO_SMB, CURLPROTO_SMBS, CURLPROXY_HTTP_1_0, CURLSSH_AUTH_AGENT and CURLSSLOPT_NO_REVOKE.
7.0.0 | assert | assert is now a language construct and not a function. assertion can now be an expression. The second parameter is now interpreted either as an exception (if a Throwable object is given), or as the description supported from PHP 5.4.8 onwards.
define | array values are allowed.
dl | dl is disabled in PHP-FPM.
getrusage | This function is now supported on Windows.
gmmktime | is_dst parameter has been removed.
imagepsbbox | T1Lib support was removed from PHP, thrus removing this function.
imagepsencodefont | T1Lib support was removed from PHP, thrus removing this function.
imagepsextendfont | T1Lib support was removed from PHP, thrus removing this function.
imagepsfreefont | T1Lib support was removed from PHP, thrus removing this function.
imagepsloadfont | T1Lib support was removed from PHP, thrus removing this function.
imagepsslantfont | T1Lib support was removed from PHP, thrus removing this function.
imagepstext | T1Lib support was removed from PHP, thrus removing this function.
mktime | is_dst parameter has been removed.
set_exception_handler | The type of parameter passed into exception_handler changed from Exception to Throwable
setlocale | Support for the category parameter passed as a string has been removed. Only LC_* constants can be used as of this version.
substr | If string is equal to start characters long, an empty string will be returned. Prior to this version, FALSE was returned in this case.
token_get_all | Added the optional flags parameter along with the TOKEN_PARSE flag.
IntlDateFormatter::setTimeZoneId | This function has been removed.
6.0.0 | chr | 입력으로 codepoint를 사용합니다.
5.6.25, 7.0.10 | imagetypes | IMG_WEBP added.
SplFileObject::getCsvControl | Added the escape character to the returned array.
5.6.16 | mysqli::real_connect | Added the MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT flag for MySQL Native Driver
5.6.12 | gd_info | WebP Support added.
5.6.3 | pack | The "q", "Q", "J" and "P" codes were added to enable working with 64-bit numbers.
5.6.1 | sem_acquire | The $nowait parameter was added.
5.6.0 | curl_setopt | Removed CURLOPT_CLOSEPOLICY and associated values.
curl_setopt | CURLOPT_SAFE_UPLOAD is now TRUE by default.
escapeshellarg | The default value for the encoding parameter was changed to be the value of the default_charset configuration option.
escapeshellcmd | The default value for the encoding parameter was changed to be the value of the default_charset configuration option.
hash_algos | Support for gost-crypto was added. This implements the GOST hash function using the CryptoPro S-box tables as specified by RFC 4357, section 11.2.
mb_regex_encoding | Default encoding is changed to UTF-8. It was EUC-JP Previously.
mcrypt_create_iv | MCRYPT_DEV_URANDOM is now the default value of source.
mcrypt_decrypt | Invalid key and iv sizes are no longer accepted. mcrypt_decrypt will now throw a warning and return FALSE if the inputs are invalid. Previously keys and IVs were padded with '\0' bytes to the next valid size.
mcrypt_encrypt | Invalid key and iv sizes are no longer accepted. mcrypt_encrypt will now throw a warning and return FALSE if the inputs are invalid. Previously keys and IVs were padded with '\0' bytes to the next valid size.
pg_connect | Support for giving the PGSQL_CONNECT_ASYNC constant as the connect_type was added.
pg_convert | No longer experimental. Boolean/NULL data types are supported. Unknown/unsupported data types are escaped without validation. pg_convert could be used with any data types.
pg_delete | No longer experimental. Added PGSQL_DML_ESCAPE constant, TRUE/FALSE and NULL data type support.
pg_insert | Unless PGSQL_DML_STRING is passed, the function now returns the connection resource instead of TRUE on success.
pg_insert | No longer experimental. Added PGSQL_DML_ESCAPE constant, TRUE/FALSE and NULL data type support.
pg_lo_seek | Added PostgreSQL 9.3's 64bit large object support. Both client and server must support PostgreSQL 9.3 and PHP must be 64bit build to use 64bit large object.
pg_lo_tell | Added PostgreSQL 9.3's 64bit large object support. Both client and server must support PostgreSQL 9.3 and PHP must be 64bit build to use 64bit large object.
pg_lo_truncate | Added truncate function. It supports PostgreSQL 9.3's 64bit large object. Both client and server must support PostgreSQL 9.3 and PHP must be 64bit build to use 64bit large object.
pg_meta_data | No longer experimental. Added "is enum" as default attribute. extended flag is added.
pg_select | No longer experimental. Added PGSQL_DML_ESCAPE constant, TRUE/FALSE and NULL data type support.
pg_update | No longer experimental. Added PGSQL_DML_ESCAPE constant, TRUE/FALSE and NULL data type support.
stream_socket_enable_crypto | The crypto_type is now optional.
stream_socket_enable_crypto | Introduce STREAM_CRYPTO_METHOD_ANY_CLIENT, STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT, STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, STREAM_CRYPTO_METHOD_ANY_SERVER, STREAM_CRYPTO_METHOD_TLSv1_0_SERVER, STREAM_CRYPTO_METHOD_TLSv1_1_SERVER, STREAM_CRYPTO_METHOD_TLSv1_2_SERVER.
substr_compare | length may now be 0.
ReflectionClass::newInstanceWithoutConstructor | All internal classes can now be instantiated except for those declared final.
XMLReader::getAttributeNo | XMLReader::getAttributeNo now returns NULL if the attribute doesn't exist.
XMLReader::getAttributeNs | XMLReader::getAttributeNS now returns NULL if the attribute doesn't exist.
5.5.21, 5.6.5 | SplFileObject::fputcsv | Added the escape parameter.
5.5.19/5.6.3 | imagescale | The calculation of the height if new_height was negative or omitted was fixed; prior to these versions, the aspect ratio was not maintained correctly.
5.5.8 | DatePeriod::__construct | end type changed to DateTimeImmutable. Previously, DateTime.
5.5.3/5.4.19 | pg_delete | Direct SQL injection to table_name and Indirect SQL injection to identifiers are fixed.
pg_insert | Direct SQL injection to table_name and Indirect SQL injection to identifiers are fixed.
pg_select | Direct SQL injection to table_name and Indirect SQL injection to identifiers are fixed.
pg_update | Direct SQL injection to table_name and Indirect SQL injection to identifiers are fixed.
5.5.2 | DOMDocument::schemaValidate | Added the flags parameter
DOMDocument::schemaValidateSource | Added the flags parameter
5.5.1 | hex2bin | A warning is thrown if the input string is invalid hexadecimal string.
pg_unescape_bytea | A warning is thrown if the input string is invalid.
5.5.0/PECL 3.0.0 | IntlDateFormatter::create | An IntlCalendar object is allowed for calendar. Objects of type IntlTimeZone and DateTimeZone are allowed for timezone. Invalid timezone identifiers (including empty strings) are no longer allowed for timezone. If NULL is given for timezone, the timezone identifier given by date_default_timezone_get will be used instead of ICU?s default.
IntlDateFormatter::format | Support for providing IntlCalendar objects to the value parameter was added.
IntlDateFormatter::setCalendar | It became possible to pass an IntlCalendar object.
5.5.0 | DatePeriod::__construct | start type changed to DateTimeImmutable. Previously, DateTime.
curl_getinfo | Introduced CURLINFO_RESPONSE_CODE, CURLINFO_HTTP_CONNECTCODE, CURLINFO_HTTPAUTH_AVAIL, CURLINFO_PROXYAUTH_AVAIL, CURLINFO_OS_ERRNO, CURLINFO_NUM_CONNECTS, CURLINFO_SSL_ENGINES, CURLINFO_COOKIELIST, CURLINFO_FTP_ENTRY_PATH, CURLINFO_APPCONNECT_TIME, CURLINFO_CONDITION_UNMET, CURLINFO_RTSP_CLIENT_CSEQ, CURLINFO_RTSP_CSEQ_RECV, CURLINFO_RTSP_SERVER_CSEQ and CURLINFO_RTSP_SESSION_ID.
curl_setopt | Introduced CURLOPT_SHARE.
curl_setopt | Added the cURL resource as the first argument to the CURLOPT_PROGRESSFUNCTION callback.
imagerotate | The ignore_transparent is now silently ignored.
imagerotate | This function is now affected by the interpolation method set by the imagesetinterpolation function.
ip2long | Prior to this version, on Windows ip2long would sometimes return a valid number even if passed a value which was not an (IPv4) Internet Protocol dotted address.
iterator_to_array | iterator_to_array gained support for key types other than integer and string when the use_keys parameter is enabled.
pack | The "Z" code was added with equivalent functionality to "a" for Perl compatibility.
php_logo_guid | php_logo_guid has been removed from PHP.
phpinfo | Logo GUIDs were replaced with data URIs, and so turning off expose_php now has no effect on the result of phpinfo(). Credits are also now embedded within the output itself instead of linked.
set_error_handler | error_handler now accepts NULL.
set_exception_handler | Previously, if NULL was passed then this function returned TRUE. It returns the previous handler since PHP 5.5.0.
setcookie | A Max-Age attribute is now included in the Set-Cookie header sent to the client.
setrawcookie | A Max-Age attribute is now included in the Set-Cookie header sent to the client.
unpack | Changes were made to bring this function into line with Perl: The "a" code now retains trailing NULL bytes. The "A" code now strips all trailing ASCII whitespace (spaces, tabs, newlines, carriage returns, and NULL bytes). The "Z" code was added for NULL-padded strings, and removes trailing NULL bytes.
zend_logo_guid | zend_logo_guid has been removed from PHP.
IntlDateFormatter::setTimeZoneId | This function has been deprecated.
mysqli::commit | Added flags and name parameters.
mysqli::options | The MYSQLI_SERVER_PUBLIC_KEY and MYSQLI_SERVER_PUBLIC_KEY options were added.
mysqli::rollback | Added flags and name parameters.
SoapClient::SoapClient | New ssl_method option.
5.4.43, 5.5.27, 5.6.11 | escapeshellarg | Exclamation marks are replaced by spaces.
escapeshellcmd | Exclamation marks are replaced by spaces.
5.4.32, 5.5.16 | ucwords | Added the delimiters parameter.
5.4.8 | assert | The description parameter was added. The description is also now provided to a callback function in ASSERT_CALLBACK mode as the fourth argument.
mb_strcut | Passing NULL as length extracts all bytes to the end of the string. Prior to this version NULL was treated the same as 0.
mb_substr | Passing NULL as length extracts all characters to the end of the string. Prior to this version NULL was treated the same as 0.
5.4.7 | curl_getinfo | Introduced CURLINFO_PRIMARY_IP, CURLINFO_PRIMARY_PORT, CURLINFO_LOCAL_IP and CURLINFO_LOCAL_PORT.
5.4.4 | hex2bin | A warning is thrown if the input string is of odd length. In PHP 5.4.0 the string was silently accepted, but the last byte was truncated.
5.4.0/PECL 2.0.0b1 | idn_to_ascii | Added the parameters variant and idna_info; UTS #46 support.
idn_to_utf8 | Added the parameters variant and idna_info; UTS #46 support.
5.4.0 | DOMDocument::loadHTML | Added options parameter.
DOMDocument::loadHTMLFile | Added options parameter.
date_default_timezone_get | The timezone is no longer guessed from information available through the operating system as the guessed timezone can not be relied on.
date_default_timezone_get | The TZ environment variable is no longer used to guess the timezone.
debug_backtrace | Added the optional parameter limit.
debug_print_backtrace | Added the optional parameter limit.
define_syslog_variables | This function was removed from PHP.
dns_get_record | Added raw parameter.
error_reporting | E_STRICT became part of E_ALL.
filter_input_array | Added add_empty parameter.
filter_var_array | Added add_empty parameter.
get_magic_quotes_gpc | Always returns FALSE because the magic quotes feature was removed from PHP.
get_magic_quotes_runtime | Always returns FALSE because the magic quotes feature was removed from PHP.
gzcompress | Added encoding parameter.
gzdeflate | Added encoding parameter.
gzencode | FORCE_DEFLATE now generates RFC 1950 compliant output.
hash | The tiger algorithm now uses big-endian byte ordering. See also example below.
hash_algos | Support for joaat, fnv132 and fnv164 was added. Support for Salsa10 and Salsa20 was removed.
htmlspecialchars_decode | The constants ENT_HTML401, ENT_XML1, ENT_XHTML and ENT_HTML5 were added.
iconv | Since this version, the function returns FALSE on illegal characters, unless //IGNORE is specified in output charset. Before, it returned partial output string.
imagecolorset | The alpha parameter was added.
imagegd | Added support for passing a stream resource to to.
imagegd2 | Added support for passing a stream resource to to.
imagegif | Added support for passing a stream resource to to.
imagejpeg | Added support for passing a stream resource to to.
imagejpeg | Disallowed passing an empty string to to to skip this argument.
imagepng | Added support for passing a stream resource to to.
imagewbmp | Added support for passing a stream resource to to.
imagewebp | Added support for passing a stream resource to to.
mb_encode_numericentity | Added is_hex parameter.
mysqli_get_cache_stats | The mysqli_get_cache_stats was removed.
number_format | This function now supports multiple bytes in dec_point and thousands_sep. Only the first byte of each separator was used in older versions.
ob_start | ob_start의 세번째 인수가 erase로 불리는 boolean 인수(FALSE로 설정할 경우 스크립트가 종료될 때까지 출력 버퍼의 삭제를 방지)에서 flags로 불리는 integer 인수로 변경되었습니다. 불행히도, 이 결과 PHP 5.4.0 이전에 세번째 인수를 사용하던 코드와 API 호환성이 깨졌습니다. 모든 버전에서 호환성을 가지게 하기 위해서는, 플래그 예제를 참고하십시오.
ob_start | 1 chunk 크기는 출력 버퍼에 1 바이트가 전달되게 바뀌었습니다.
openssl_decrypt | The raw_output was changed to options.
openssl_encrypt | The raw_output was changed to options.
snmp_set_valueretrieval | Constants SNMP_VALUE_PLAIN or SNMP_VALUE_LIBRARY may be combined with SNMP_VALUE_OBJECT resulting different way of representing contents of value array element in return value of GET-function. If no SNMP_VALUE_{PLAIN,LIBRARY} constant is accompanying SNMP_VALUE_OBJECT, SNMP_VALUE_LIBRARY is used. Prior to 5.4.0 SNMP_VALUE_OBJECT effecively meant SNMP_VALUE_OBJECT|SNMP_VALUE_PLAIN.
Phar::setStub | Added len parameter.
PharData::setStub | Added len parameter.
SoapClient::SoapClient | New keep_alive option.
5.4 | grapheme_substr | If length is given and positive and the argument string is shorter than the specified length, all the rest of the string according to start parameter is returned. Previously, FALSE was returned.
5.3.9 | DateTime::createFromFormat | The + format specifier has been added.
dl | dl is enabled in PHP-FPM, albeit discouraged.
5.3.7 | curl_getinfo | Introduced CURLINFO_REDIRECT_URL.
5.3.6 | DateTime::modify | Absolute date/time statements now take effect. Previously, only relative parts were used.
DOMDocument::saveHTML | The node parameter was added.
debug_backtrace | The parameter provide_object changed to options and additional option DEBUG_BACKTRACE_IGNORE_ARGS is added.
debug_print_backtrace | Added the optional parameter options.
PDO_MYSQL DSN | Prior to version 5.3.6, charset was ignored.
5.3.4 | sleep | Before PHP 5.3.4, on Windows, sleep always returns NULL when sleep has occurred, regardless of whether the sleep was interrupted or not.
IntlDateFormatter::format | Support for providing DateTime objects to the value parameter was added.
mysqli_driver::$report_mode | Changing the reporting mode is now be per-request, rather than per-process.
5.3.3 | DOMXPath::evaluate | The registerNodeNS parameter was added.
DOMXPath::query | The registerNodeNS parameter was added.
ldap_sasl_bind | Support on Windows was added.
openssl_decrypt | The iv parameter was added.
openssl_encrypt | The iv parameter was added.
5.3.2 (PECL OCI8 1.4) | oci_set_prefetch | Before this release, rows must be >= 1.
5.3.2 | gmp_init | The base was extended from 2 to 36, to 2 to 62 and -2 to -36.
gmp_strval | The base was extended from 2 to 36, to 2 to 62 and -2 to -36.
imap_append | Added INTERNALDATE support to imap_append.
imap_open | params added
5.3.1 | ibase_query | On success the function now returns TRUE if there were no affected rows, where it previously returned 0 (a zero followed by an empty space).
5.3.0 (PECL OCI8 1.3.4) | oci_set_prefetch | Before this release, prefetching was limited to the lesser of rows rows and 1024 * rows bytes. The byte size restriction has now been removed.
5.3.0 | DateTime::__construct | If time contains an invalid date/time format, then an exception is now thrown. Previously an error was emitted.
DateTime::modify | 반환값을 NULL에서 DateTime으로 변경.
DateTime::setDate | 반환값을 NULL에서 DateTime으로 변경.
DateTime::setISODate | 반환값을 NULL에서 DateTime으로 변경.
DateTime::setTime | 반환값을 NULL에서 DateTime으로 변경.
DateTime::setTimezone | 반환값을 NULL에서 DateTime으로 변경.
DateTimeZone::getTransitions | The optional timestamp_begin and timestamp_end were added.
DateTimeZone::listIdentifiers | Added the optional what and country parameters.
DirectoryIterator::__construct | Throws UnexpectedValueException if the path cannot be opened.
acosh | 모든 플랫폼에서 이 함수를 사용할 수 있게 되었습니다.
asinh | 모든 플랫폼에서 이 함수를 사용할 수 있게 되었습니다.
atanh | 모든 플랫폼에서 이 함수를 사용할 수 있게 되었습니다.
call_user_func | The interpretation of object oriented keywords like parent and self has changed. Previously, calling them using the double colon syntax would emit an E_STRICT warning because they were interpreted as static.
call_user_func_array | The interpretation of object oriented keywords like parent and self has changed. Previously, calling them using the double colon syntax would emit an E_STRICT warning because they were interpreted as static.
checkdnsrr | This function is now available on Windows platforms.
crypt | 시스템에서 지원하지 않는 MD5 암호화, 표준 DES, 확장 DES, Blowfish 알고리즘에 대해서 PHP의 구현을 가집니다.
curl_getinfo | Introduced CURLINFO_CERTINFO.
curl_setopt | Introduced CURLOPT_PROGRESSFUNCTION.
date_default_timezone_set | Now throws E_WARNING rather than E_STRICT.
define_syslog_variables | This function now throws an E_DEPRECATED notice.
dl | dl is now disabled in some SAPIs due to stability issues. The only SAPIs that allow dl are CLI and Embed. Use the Extension Loading Directives instead.
dns_get_record | This function is now available on Windows platforms.
dns_get_record | Prior to this release, if the authns parameter was given, the addtl parameter was also required.
error_reporting | E_DEPRECATED and E_USER_DEPRECATED introduced.
exif_imagetype | Added icon support.
expm1 | This function is now available on all platforms
func_get_arg | This function can now be used in parameter lists.
func_get_arg | If this function is called from the outermost scope of a file which has been included by calling include or require from within a function in the calling file, it now generates a warning and returns FALSE.
func_get_args | If this function is called from the outermost scope of a file which has been included by calling include or require from within a function in the calling file, it now generates a warning and returns FALSE.
func_get_args | This function can now be used in parameter lists.
func_num_args | This function can now be used in parameter lists.
func_num_args | If this function is called from the outermost scope of a file which has been included by calling include or require from within a function in the calling file, it now generates a warning and returns -1.
gd_info | JPG Support attribute renamed to JPEG Support.
get_cfg_var | get_cfg_var가 "array" ini 옵션을 반환할 수 있도록 수정되었습니다.
getimagesize | Added icon support.
getmxrr | This function is now available on Windows platforms.
getopt | Parameter longopts is available on all systems.
getopt | Added support for optional values (specified with "::").
getopt | Added support for "=" as argument/value separator.
getopt | This function is no longer system dependent, and now works on Windows, too.
hash_algos | Support for md2, ripemd256, ripemd320, salsa10, salsa20, snefru256 and sha224 was added
imagecolorclosesthwb | This function is now available on Windows
imagefilter | Pixelation support (IMG_FILTER_PIXELATE) was added.
inet_ntop | This function is now available on Windows platforms.
inet_pton | This function is now available on Windows platforms.
ini_get | Previously, the empty string was returned if the configuration option didn't exist. now, FALSE is returned instead.
ini_get_all | details 추가.
log1p | This function is now available on all platforms
mb_get_info | The entry "http_output_conv_mimetypes" was made available.
mcrypt_create_iv | MCRYPT_DEV_RANDOM and MCRYPT_DEV_URANDOM became available on Windows platforms.
mcrypt_create_iv | It is no longer required to call srand first. This is now done automatically.
mktime | mktime now throws E_DEPRECATED notice if the is_dst parameter is used.
mysql_db_query | 이 함수는 E_DEPRECATED 주의를 발생합니다.
mysql_escape_string | E_DEPRECATED 주의가 발생합니다.
mysqli_get_cache_stats | The mysqli_get_cache_stats was added as stub.
nl2br | 선택적인 is_xhtml 인수 추가.
openssl_seal | The method parameter was added.
pg_lo_create | The optional object_id was added.
pg_lo_import | The optional object_id was added.
register_tick_function | Ticks are now supported on threaded web server modules.
setlocale | This function now throws an E_DEPRECATED notice if a string is passed to the category parameter instead of one of the LC_* constants.
shm_attach | This function now returns a 자원 instead of an integer.
socket_create_pair | This function is now re-enabled on Windows platforms.
spl_autoload_register | The prepend parameter was added.
spl_autoload_register | Namespaces support was introduced.
stream_context_create | Added the optional params argument.
stream_socket_pair | This function is now available on Windows platforms.
stristr | 선택적인 before_needle 인수 추가
strstr | 선택적인 before_needle 인수 추가.
strtotime | Prior to PHP 5.3.0, relative time formats supplied to the time argument of strtotime such as this week, previous week, last week, and next week were interpreted to mean a 7 day period relative to the current date/time, rather than a week period of Monday through Sunday.
strtotime | Prior to PHP 5.3.0, 24:00 was not a valid format and strtotime returned FALSE.
sybase_connect | The new parameter was added.
time_nanosleep | This function is now available on Windows platforms.
time_sleep_until | This function is now available on Windows platforms.
mysqli::__construct | Added the ability of persistent connections.
mysqli::options | The MYSQLI_OPT_INT_AND_FLOAT_NATIVE, MYSQLI_OPT_NET_CMD_BUFFER_SIZE, MYSQLI_OPT_NET_READ_BUFFER_SIZE, and MYSQLI_OPT_SSL_VERIFY_SERVER_CERT options were added.
mysqli::query | Added the ability of async queries.
ReflectionFunction::__construct | name can now be a closure.
SplFileObject::setCsvControl | Added the escape parameter.
SplObjectStorage::attach | Added the data parameter.
XMLReader::expand | The parameter basenode was added.
5.2.15 | mysqli_driver::$report_mode | Changing the reporting mode is now be per-request, rather than per-process.
5.2.10 | curl_setopt | Introduced CURLOPT_PROTOCOLS, and CURLOPT_REDIR_PROTOCOLS.
ip2long | Prior to this version, ip2long would sometimes return a valid number even if passed a value which was not an (IPv4) Internet Protocol dotted address.
5.2.8 | XSLTProcessor::importStylesheet | Accepts SimpleXMLElement again which was broken since PHP 5.2.6.
5.2.7 | error_log | The possible value of 4 was added to message_type.
strtotime | In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.
5.2.5 | addcslashes | 회피 시퀀스 \v와 \f 추가
debug_backtrace | Added the optional parameter provide_object.
imagefilter | Alpha support for IMG_FILTER_COLORIZE was added.
5.2.4 | checkdnsrr | TXT type was added.
curl_getinfo | Introduced CURLINFO_PRIVATE.
curl_setopt | Introduced CURLOPT_PRIVATE.
get_loaded_extensions | 선택적인 zend_extensions 인수 추가
ldap_first_attribute | The ber_identifier was removed. This is now handled automatically by PHP.
ldap_next_attribute | The ber_identifier was removed. This is now handled automatically by PHP.
stream_wrapper_register | Added the flags parameter.
5.2.3 | getimagesize | Read errors generated by this function downgraded to E_NOTICE from E_WARNING.
htmlentities | double_encode 인수 추가.
htmlspecialchars | double_encode 인수 추가.
json_decode | 중첩 제한이 20에서 128로 증가
XMLWriter::writeElement | The content parameter became optional.
XMLWriter::writeElementNS | The content parameter became optional.
PDOStatement::getColumnMeta | table field
5.2.2 - 5.2.6 | substr | If the start parameter indicates the position of a negative truncation or beyond, false is returned. Other versions get the string from start.
5.2.2 | DOMDocument::registerNodeClass | Prior to 5.2.2, a previously registered extendedclass had to be unregistered before being able to register a new class extending the same baseclass.
ftp_ssl_connect | The function was changed to return FALSE when it can't use an SSL connection, instead of fallbacking to a non-SSL one as previously.
phpinfo | The "Loaded Configuration File" information was added, when before only "Configuration File (php.ini) Path" existed.
proc_terminate | Previous versions used to destroy the given process resource.
token_get_all | Line numbers are returned in element 2
5.2.1부터 | mt_srand | PHP의 Mersenne Twister 구현이 Richard Wagner의 새로운 초기화 알고리즘을 사용합니다. 동일한 초기화 값을 주어도, 이전 버전처럼 동일한 값을 생성하지 않습니다. 이 동작은 다시 변경되지 않을 것이지만, 여기에 의존하는 것은 안전하지 않은 것으로 간주됩니다.
5.2.1 | iterator_to_array | The use_keys parameter was added.
json_encode | JSON 인코드 기본 자료형 지원 추가
memory_get_peak_usage | Compiling with --enable-memory-limit is no longer required for this function to exist.
memory_get_usage | Compiling with --enable-memory-limit is no longer required for this function to exist.
proc_open | Added the bypass_shell option to the other_options parameter.
5.2.0 | base64_decode | strict 추가
curl_multi_info_read | msgs_in_queue was added.
error_reporting | E_RECOVERABLE_ERROR introduced.
imagettftext | It is now possible to specify an hexadecimal entity in text.
imap_open | n_retries added
imap_reopen | n_retries added
mb_strrpos | Added the optional parameter offset.
memory_get_peak_usage | real_usage was added.
memory_get_usage | real_usage was added.
openssl_verify | The signature_alg parameter was added.
pg_escape_bytea | connection added
pg_escape_string | connection added
set_error_handler | The error handler must return FALSE to populate $php_errormsg.
setcookie | The httponly parameter was added.
setrawcookie | The httponly parameter was added.
snmp_set_oid_numeric_print | Since PHP 5.2.0.
PDOStatement::execute | The keys from input_parameters must match the ones declared in the SQL. Before PHP 5.2.0 this was silently ignored.
SoapServer::SoapServer | Added the typemap option.
XMLReader::open | encoding and options were added.
XMLReader::XML | encoding and options were added.
5.1.3 | DirectoryIterator::__construct | Throws RuntimeException if the path is an empty string.
curl_getinfo | Introduced CURLINFO_HEADER_OUT.
get_headers | This function now uses the default stream context, which can be set/changed with the stream_context_set_default function.
http_build_query | 대괄호를 이스케이프 합니다.
imagecolorallocate | Returns FALSE if the allocation failed. Previously -1 was returned.
imagecolorallocatealpha | Returns FALSE if the allocation failed. Previously -1 was returned.
imagepng | Added the filters parameter.
mb_get_info | The entries "mail_charset", "mail_header_encoding", and "mail_body_encoding" were made available.
SoapClient::__doRequest | The one_way parameter was added.
XMLReader::getAttribute | Return NULL if no attribute found. Previously, returned an empty string.
5.1.2 | date_default_timezone_set | The function started to validate the timezone_identifier parameter.
header | This function now prevents more than one header to be sent at once as a protection against header injection attacks.
http_build_query | arg_separator 인수 추가.
imagepng | Added the quality parameter.
parse_url | component 인수 추가
5.1.1 | date | format 인수 지정에 사용할 수 있는 유용한 표준 날짜/시간 상수가 있습니다.
debug_backtrace | Added the current object as a possible return element.
gmdate | There are useful constants of standard date/time formats that can be used to specify the format parameter.
5.1.0 | DirectoryIterator::__construct | Throws RuntimeException on error. Previously, threw Exception.
DOMDocument::save | Added the options parameter
DOMDocument::saveXML | Added the options parameter
class_implements | Added the option to pass the class parameter as a string. Added the autoload parameter.
class_parents | Added the option to pass the class parameter as a string. Added the autoload parameter.
ctype_digit | Before PHP 5.1.0, this function returned TRUE when text was an empty string.
curl_setopt | Introduced CURLOPT_AUTOREFERER, CURLOPT_BINARYTRANSFER, CURLOPT_FTPSSLAUTH, CURLOPT_PROXYAUTH, and CURLOPT_TIMECONDITION.
date | 시간대 오류시 E_STRICT와 E_NOTICE가 발생합니다.
date | 유효한 타임스탬프 범위는 보통 Fri, 13 Dec 1901 20:45:54 GMT 에서 Tue, 19 Jan 2038 03:14:07 GMT 입니다. (이 날짜들은 32비트 부호 있는 정수의 최소값과 최대값에 해당합니다) 그러나, PHP 5.1.0 이전에는 몇몇 시스템에서 이 범위가 01-01-1970에서 19-01-2038로 제한되었습니다. (윈도우 등)
date_sunrise | 시간대 오류시 E_STRICT와 E_NOTICE가 발생합니다.
date_sunset | 시간대 오류시 E_STRICT와 E_NOTICE가 발생합니다.
explode | 음수 limit 지원 추가
gettimeofday | The return_float parameter was added.
gmdate | The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).
gmmktime | As of PHP 5.1.0, the is_dst parameter became deprecated. As a result, the new timezone handling features should be used instead.
idate | 시간대 오류시 E_STRICT와 E_NOTICE가 발생합니다.
imagerotate | ignore_transparent was added.
localtime | 시간대 오류시 E_STRICT와 E_NOTICE가 발생합니다.
md5_file | 스트림 API를 사용합니다. 즉, md5_file('http://example.com/..')처럼 래퍼를 사용할 수 있습니다.
mktime | When called with no arguments, mktime throws E_STRICT notice. Use the time function instead.
mktime | The is_dst parameter became deprecated. Made the function return FALSE on error, instead of -1. Fixed the function to accept the year, month and day to be all passed as zero.
mktime | 시간대 오류시 E_STRICT와 E_NOTICE가 발생합니다.
mssql_connect | The new_link parameter was added
openssl_pkcs7_verify | The content parameter was added.
preg_replace | count 인수 추가
preg_replace_callback | count 인수 추가
sha1_file | 스트림 API를 사용합니다. 즉, sha1_file('http://example.com/..')처럼 래퍼를 사용할 수 있습니다.
str_word_count | charlist 인수 추가
stream_copy_to_stream | Added the offset parameter
stream_filter_append | Prior to PHP 5.1.0, this function returns TRUE on success 실패 시 FALSE를 반환합니다.
stream_filter_prepend | Prior to PHP 5.1.0, this function returns TRUE on success 실패 시 FALSE를 반환합니다.
stream_get_contents | The offset was added.
strftime | 시간대 오류시 E_STRICT와 E_NOTICE가 발생합니다.
strtotime | Now returns FALSE on failure, instead of -1.
strtotime | 시간대 오류시 E_STRICT와 E_NOTICE가 발생합니다.
substr_compare | Added the possibility to use a negative offset.
substr_count | Added the offset and the length parameters
var_export | __set_state 마법 메쏘드를 사용하여 클래스와 클래스를 가진 배열을 내보낼 수 있게 되었습니다.
XSLTProcessor::registerPHPFunctions | The restrict parameter was added.
5.0.5 | php_check_syntax | This function was removed from PHP.
5.0.4 | msql_fetch_array | A bug was fixed when retrieving data from columns containing NULL values. Such columns were not placed into the resulting array.
msql_fetch_object | A bug was fixed when retrieving data from columns containing NULL values. Such columns were not placed into the resulting array.
msql_fetch_row | A bug was fixed when retrieving data from columns containing NULL values. Such columns were not placed into the resulting array.
5.0.3 | php_check_syntax | Calling exit after php_check_syntax resulted in a Segfault.
5.0.2 | strtotime | In PHP 5 up to 5.0.2, "now" and other relative times are wrongly computed from today's midnight. This differs from other versions where it is correctly computed from current time.
5.0.1 | php_check_syntax | error_message is passed by reference.
5.0.0 | curl_setopt | Introduced CURLOPT_FTP_USE_EPRT, CURLOPT_NOSIGNAL, CURLOPT_UNRESTRICTED_AUTH, CURLOPT_BUFFERSIZE, CURLOPT_HTTPAUTH, CURLOPT_PROXYPORT, CURLOPT_PROXYTYPE, CURLOPT_SSLCERTTYPE, and CURLOPT_HTTP200ALIASES.
error_reporting | E_STRICT introduced (not part of E_ALL).
get_defined_constants | categorize 인수가 추가되었습니다.
get_defined_vars | 반환하는 결과 배열에 $GLOBALS 변수가 포함됩니다.
html_entity_decode | 멀티바이트 문자셋 지원 추가.
md5 | raw_output 인수 추가.
md5_file | raw_output 인수 추가
mysql_fetch_object | 다른 객체로 반환할 수 있는 기능 추가
sha1 | raw_output 인수 추가.
sha1_file | raw_output 인수 추가
str_ireplace | count 인수 추가.
str_replace | count 인수 추가.
strip_tags | strip_tags는 바이너리 안전입니다
strtotime | Microseconds began to be allowed, but they are ignored.

User Contributed Notes

There are no user contributed notes for this page.


HTML 도움말 에디션

  • About This Edition
  • Using PHP Manual CHM Edition
    • The HTML Help Toolbar
    • The Navigation Pane
    • Keyboard Shortcuts
  • The Full Text Search
    • General searching rules
    • Searching for words or phrases
    • Defining search terms
    • Using nested expressions when searching
  • Integrating the PHP Manual

User Contributed Notes

There are no user contributed notes for this page.


About This Edition

The Windows HTML Help Edition of the PHP Manual is aimed at developers, who want the manual at hand every time, and would like to read manual pages and user notes without waiting for a remote web page to download. This edition gives you the full content of the manual plus the user notes, with some great features provided by the viewer application by the scripts we use to generate this format and by other contributors in the form of skins.

This distribution of the PHP Manual contains many files, including the main manual file php_manual_en.chm. You can view the manual by double clicking on this file (as you already guessed). You may also see a php_manual_en.chw file in the directory, it's generated the first time you use the index tab, and it contains an index with the manual's pages.

For those interested in technical details about Microsoft HTML Help, » http://www.helpware.net/ is a great resource. For more information and positioning of Windows HTML Help in the Microsoft help formats and services history, see this » WinWriters article.

User Contributed Notes

There are no user contributed notes for this page.


Using PHP Manual CHM Edition

Table of Contents

  • The HTML Help Toolbar
  • The Navigation Pane
  • Keyboard Shortcuts

When you double click on a CHM, a HTML Help Viewer window opens with three panes. On the left side of the window is the Navigation pane. It contains four navigational tabs: the Contents, Index, Search and Favorites tabs. On the right side of the window is the Topic pane. It displays the selected help topic, or the default help topic on startup. The help pages are all separate HTML files, your Internet Explorer displays the pages in the Topic pane. The third pane is the toolbar, which is located below the help window title bar. You can adjust the horizontal layout with dragging the vertical border between the Navigation pane and the Topic pane.

Note, that when you open the CHM the first time, it may appear in a strange position and size. From then, Windows remembers the window position, size and the last opened tab. All these including your favorites are stored in a file named hh.dat. If you move your file to another directory, all these remembered settings will be lost (including your favorites listing!). If you replace your CHM file with a new one, with the same name, this is not a problem though. The file names and path values are stored in hh.dat.

User Contributed Notes

There are no user contributed notes for this page.


The HTML Help Toolbar

The HTML Help Toolbar contains the following icons, when you view the PHP Manual:

  • Hide/Show - Hides/shows the navigation pane.
  • Locate - Locates a topic in the contents tab (in case you reached a topic with search, index or favorites, and would like to see where it is in the table of contents tree view).
  • Back - Goes back in the history.
  • Forward - Goes forward in the history.
  • Refresh - Reloads the current page.
  • Home - Opens the startup page of the CHM.
  • Print - Opens the print dialog box.
  • Options - Provides a dropdown with more options.
Using this toolbar is really easy, many of the toolbar icons are strongly related to their Internet Explorer equivalents.

User Contributed Notes

There are no user contributed notes for this page.


The Navigation Pane

The Navigation Pane contains four navigational tabs: the Contents, Index, Search and Favorites tabs. These all provide different methods to access a topic in the help file.

  • The contents tab reveals a tree like table of contents, where you can navigate to the desired topic by opening and closing parts of the tree. You can even access the deepest sections using this TOC. If you right click on an item here and choose "Print...", you'll have the option to print the selected topic or the selected topic and all subtopics. However printing is not the initial goal of this representation of the manual (the PDF version is suitable for printing).
  • The index tab lists a huge number of "keywords" (actually manual page titles). You can pick one, or start to type a keyword, see the list adjusting to your input, and then click on a topic, or press enter if the selected topic is the one you would like to see.
  • The search tab shows one of the most powerful features of the HTML Help Viewer, the full text search (also known as FTS). Here you can type in your search expression, optionally connecting words with AND/OR/NEAR/NOT. Before you click 'List topics', you can also set some options at the bottom. After the search results are displayed, you'll realize that manual text and user note results are separated. To sort out the two types, click on the 'Location' column header. Now from the search results list, you can pick a topic, and see it's page. The keywords you searched for are highlighted on the page.
  • The favorites tab enables you to store your most used topics in a list, easily accessible. Here you can see the list, add the current topic to it, remove one, or display a selected item. You can also specify your own title for the favorite, by typing it into the 'Current topic:' box. The user note page titles are prepended with a 'N:' string so you can distinguish them from normal manual pages in the favorites listing.

User Contributed Notes

There are no user contributed notes for this page.


Keyboard Shortcuts

There are many keyboard shortcuts you can use to increase your productivity. Many users find it more appropriate to use these keyboard shortcuts to access topics, instead of clicking.

General Help Viewer Shortcuts
To | Press
Close the Help Viewer | ALT+F4
Display the Options menu | ALT+O
Hide or show the Navigation pane | ALT+O, and then T
Print a topic | ALT+O, and then P
Move back to the previous topic | ALT+LEFT ARROW
Move forward to the next topic (provided you have viewed it just previously) | ALT+RIGHT ARROW
Turn on or off search highlighting. | ALT+O, and then O
Return to the help file home page | ALT+O, and then H
Switch between the Navigation pane and the Topic pane | F6
Scroll through all the links in a topic or through all the options on a Navigation pane tab | TAB
Display context (right click) menu | SHIFT+F10
Contents Tab Shortcuts
To | Press
Display the Contents tab | ALT+C
Open and close a book or folder | PLUS SIGN and MINUS SIGN, or LEFT ARROW and RIGHT ARROW
Select a topic | DOWN ARROW and UP ARROW
Display the selected topic | ENTER
Index Tab Shortcuts
To | Press
Display the Index tab | ALT+N
Type a keyword to search for | ALT+W, and then type the word
Select a keyword in the list | UP ARROW and DOWN ARROW
Display the associated topic | ENTER or ALT+D
Search Tab shortcuts
To | Press
Display the Search tab | ALT+S
Type a keyword to search for | ALT+W, and then type the word
Start a search | ALT+L
Select a topic in the results list | ALT+T, and then UP ARROW and DOWN ARROW
Display the selected topic | ENTER or ALT+D
Search for a keyword in the result list of a prior search | ALT+U
Search for words similar to the keyword. For example, to find words like "running" and "runs" for the keyword "run" | ALT+M
Only search through topic titles | ALT+R
Favorites Tab Shortcuts
To | Press
Display the Favorites tab | ALT+I
Add the currently displayed topic to the Favorites list | ALT+A
Select a topic in the Favorites list | ALT+P, and then UP ARROW and DOWN ARROW
Display the selected topic | ENTER or ALT+D
Remove the selected topic from the list | ALT+R
Topic Pane Shortcuts
To | Press
Index Lookup | Select the words you would like to look up and press F1
Find on page | CTRL+F, and then adjust options
Refresh page | F5

User Contributed Notes

There are no user contributed notes for this page.


The Full Text Search

Table of Contents

  • General searching rules
  • Searching for words or phrases
  • Defining search terms
  • Using nested expressions when searching

This edition of the PHP Manual includes an advanced full text search possibility provided by the viewer application. This enables users to search through every word in the help file to find a match. For example, if a user does a full-text search on the word "Apache", every topic that contains the word "Apache" will be listed. Advanced full-text search allows a user to search using boolean, wildcard, and nested expressions. A user can also limit the search to previous results, match similar words, or search topic titles only.

The usage of the search feature is fairly straightforward. Click on the search tab, type in the desired words and press ENTER (or click on "List Topics"). Then you'll receive a list of matches from the PHP Manual (and the notes). You can use the button with a right arrow on it to add boolean operators to your search (or you can type them in). As you see the results, you can use the column headings (Title, Location and Rank) to sort the topic list. The default order is by Rank. You can also adjust some parameters at the bottom of this tab.

The words found are highlighted on the current page in the Topic pane by default. You can turn off this feature by choosing the "Search Highlight Off" menu item in the Options menu. To can get this feature back similarly. If you are viewing a long topic, only the first 500 instances of a search word or phrase will be highlighted, due to a limitation of the viewer.

User Contributed Notes

There are no user contributed notes for this page.


General searching rules

A basic search consists of the word or phrase you want to find. You can use wildcard expressions, nested expressions, boolean operators, similar word matches, a previous results list, or topic titles to further define your search.

The basic rules for formulating queries are as follows:

  • Searches are not case-sensitive, so you can type your search in uppercase or lowercase characters.
  • You may search for any combination of letters (a-z) and numbers (0-9). You cannot search for single letters (a, b, c, etc.) and common words, like: an, and, as, at, be, but, by, do, for, from, have, he, in, it, not, of, on, or, she, that, the, there, they, this, to, we, which, with, you.
  • Punctuation marks such as the period (.), colon (:), semicolon (;), comma (,), and hyphen (-) are ignored during a search.
  • Group the elements of your search using double quotes or parentheses to set apart each element. You cannot search for quotation marks.

Note:

If you are searching for a file name with an extension, you should group the entire string in double quotes, ("filename.ext"). Otherwise, the period will break the file name into two separate terms. The default operation between terms is AND, so you will create the logical equivalent to "filename AND ext."

User Contributed Notes

There are no user contributed notes for this page.


Searching for words or phrases

You can search for words or phrases and use wildcard expressions. Wildcard expressions allow you to search for one or more characters using a question mark or asterisk. The table below describes the results of these different kinds of searches.

Word and phrase searches
Search for | Example | Results
A single word | select | Topics that contain the word "select". (You will also find its grammatical variations, such as "selector" and "selection".)
A phrase | "new operator" or new operator | Topics that contain the literal phrase "new operator" and all its grammatical variations. Without the quotation marks, the query is equivalent to specifying "new AND operator," which will find topics containing both of the individual words, instead of the phrase.
Wildcard expressions | esc* or HT?? | Results for the first example include topics that contain the terms "ESC", "escape", "escalation", and so on. The asterisk cannot be the only character in the term. Results for the second example include topics that contain the terms "HTTP", "HTML" and so on. The question mark cannot be the only character in the term.

Note:

Select the Match similar words check box to include minor grammatical variations for the phrase you search. This feature only locates variations of the word with common suffixes. For example, a search on the word "add" will find "added," but it will not find "additive". This option is independent of other options or syntax. If you do a titles-only search, variations in titles will be matched. If you use quotes (or any other query operator) any variation of the word can appear; for example, "stemmed search" will also match "stemming search".

User Contributed Notes

There are no user contributed notes for this page.


Defining search terms

The AND, OR, NOT, and NEAR operators enable you to precisely define your search by creating a relationship between search terms. The following table shows how you can use each of these operators. If no operator is specified, AND is used. For example, the query "server cgi security" is equivalent to "server AND cgi AND security".

Search operators
Search for | Example | Results
Both terms in the same topic | http AND apache | Topics containing both the words "http" and "apache".
Either term in a topic | ming OR swflib | Topics containing either the word "ming" or the word "swflib" or both.
The first term without the second term | xml NOT expat | Topics containing the word "xml" but not the word "expat".
Both terms in the same topic, close together | database NEAR security | Topics containing the word "database" within eight words of the word "security".

Note:

The |, &, and ! characters can also be used in place of OR AND and NOT respectively. This may not work if you use a very old version of the viewer.

User Contributed Notes

There are no user contributed notes for this page.


Using nested expressions when searching

Nested expressions allow you to create complex searches for information. For example, "html AND ((smtp OR pop3) NEAR mail)" finds topics containing the word "html" along with the words "smtp" and "mail" close together, or containing "html" along with the words "pop3" and "mail" close together.

The basic rules for searching help topics using nested expressions are as follows:

  • You can use parentheses to nest expressions within a query. The expressions in parentheses are evaluated before the rest of the query.
  • If a query does not contain a nested expression, it is evaluated from left to right. For example: "mail NOT pop3 OR smtp" finds topics containing the word "mail" without the word "pop3" or topics containing the word "smtp". On the other hand, "mail NOT (pop3 OR smtp)" finds topics containing the word "mail" without either of the words "pop3" or "smtp".
  • You cannot nest expressions more than five levels deep.

User Contributed Notes

There are no user contributed notes for this page.


Integrating the PHP Manual

Note:

The information provided here is mainly aimed at IDE authors, or advanced users who would like to integrate this CHM file to their favorite IDEs or other production environment components.

There are several editors out there with support for CHM integration, but you may need to know a few things about what the CHM contains to successfully integrate the manual into your environment.

The CHM is built using XSL style sheets from XML sources. This is unique currently in the PHP Manual family, as all other versions are generated using DSSSL style sheets. This also means, that non-intentional rendering differences may come up. A special conversion script is run on the XSLT output, adding several cute features, and packing up the manual with the preferences files and skin examples.

If you never worked with CHMs, you can think of them as compressed files with OS supported access for files inside and some added search and index support. However CHMs can only be viewed using the HTML Help Viewer, you can directly access files inside them using a special URL prefix, the CHM file name and the file you request from inside. As all the help content is stored in HTML files, you can show pages of the CHM in Internet Explorer.

Assume that you placed your php_manual_en.chm to c:\phpmanual the index file in the manual (which you see the first time) can be accessed with the following URL: mk:@MSITStore:C:\phpmanual\php_manual_en.chm::/_index.html. Here mk:@MSITStore: is the special "protocol", C:\phpmanual\php_manual_en.chm is the CHM with it's full path. The /_index.html part is the path to the index file inside the CHM and :: is just what you need to put between the CHM path and this file path.

Note:

All files are in the root directory of the CHM, unlike the previous CHM versions which included a language directory. Images, style sheets and other supplemental files have names starting with an underscore (like the main index shown above), to avoid name collisions.

The names of generated files follow the same rules as the online manual, except that the extension is .html and not .php. The most important is that function documentation files are named function.FUNCNAME.html where FUNCNAME is the function name, with all underscores converted to hyphens. Some examples are function.echo.html, function.mysql-close.html, function.imagecopy.html.

Using all this information, you can show a manual page for a function requested by a user. A simple example is included in the distribution, named php_quickref.hta. This is a » HTML Application to demonstrate the simple process of showing a manual page for a function. The quickRef() function defined therein does the job.

If you would like to integrate the manual into an IDE without direct support for the PHP manual (actually the underscore to hyphen conversion), you can use the included _function.html file to access a function page. This file is simply a redirector, and can be parameterized via the URL, as _function.html#mysql_close. This page will redirect you to the mysql_close function page (function.mysql-close.html) automatically. You can provide the full path of this file if your IDE supports context sensitive help, and provide the IDE specified string as the parameter. An example for this is the UltraEdit 9 intergation (see the edition's website).

The index of the manual (accessible via the index tab on the navigation pane) can also be used for integrational purposes. All the HTML pages are included in the index with their titles as index terms (including function description pages).

If you are a desktop application developer and would like to tightly integrate the CHM into your program (such as displaying the TOC tree in your IDE's help box), you can find more information at » http://www.helpware.net/, as well as links to other useful resources. The official site for HTML Help is at » http://www.microsoft.com/download/en/details.aspx?id=21138.

User Contributed Notes

There are no user contributed notes for this page.