php intvalue 用法,范围解析操作符(::)

//You may use and modify this code but please keep this short copyright notice in tact.

//If you modify the code you may comment the changes you make and append your own copyright

//notice to mine. This code is not to be redistributed individually for sale but please use it as part

//of your projects and applications - free or non-free.

//Static workaround for php4 - even works with arrays - the trick is accessing the arrays.

//I used the format s_varname for my methods that employ this workaround. That keeps it

//similar to working with actual variables as much as possible.

//The s_ prefix immediately identifies it as a static variable workaround method while

//I'm looking thorugh my code.function &s_foo($value=null,$remove=null)

{

static$s_var;//Declare the static variable.    The name here doesn't matter - only the name of the method matters.if($remove)

{

if(is_array($value))

{

if(is_array($s_var))

{

foreach($valueas$key=>$data)

{

unset($s_var[$key]);

}

}

}

else

{//You can't just use unset() here because the static state of the variable will bring back the value next time you call the method.$s_var=null;

unset($s_var);

}//Make sure that you don't set the value over again.$value=null;

}

if($value)

{

if(is_array($value))

{

if(is_array($s_var))

{//$s_var = array_merge($s_var, $value);        //Doesn't overwrite values. This adds them - a property of the array_merge() function.foreach($valueas$key=>$data)

{$s_var[$key] =$data;//Overwrites values.}

}

else

{$s_var=$value;

}

}

else

{$s_var=$value;

}

}

return$s_var;

}

}

echo"Working with non-array values.
";

echo"Before Setting: ".StaticSample::s_foo();

echo"
";

echo"While Setting: ".StaticSample::s_foo("VALUE HERE");

echo"
";

echo"After Setting: ".StaticSample::s_foo();

echo"
";

echo"While Removing: ".StaticSample::s_foo(null,1);

echo"
";

echo"After Removing: ".StaticSample::s_foo();

echo"


";

echo"Working with array values
";$array= array(0=>"cat",1=>"dog",2=>"monkey");

echo"Set an array value: ";print_r(StaticSample::s_foo($array));

echo"
";//Here you need to get all the values in the array then sort through or choose the one(s) you want.$all_elements=StaticSample::s_foo();$middle_element=$all_elements[1];

echo"The middle element: ".$middle_element;

echo"
";$changed_array= array(1=>"big dog",3=>"bat","bird"=>"flamingo");

echo"Changing the value: ";print_r(StaticSample::s_foo($changed_array));

echo"
";//All you have to do here is create an array with the keys you want to erase in it.

//If you want to erase all keys then don't pass any array to the method.$element_to_erase= array(3=>null);

echo"Erasing the fourth element: ";$elements_left=StaticSample::s_foo($element_to_erase,1);print_r($elements_left);

echo"
";

echo"Enjoy!";?>

THE END
< <上一篇
下一篇>>