views:

139

answers:

6

I relogin to my server in dreamhost and test some scripts.And I found I couldn't use str_split. Message of Undefined function was given.I checked the version in the server and its PHP Version is 5.2.12.And I just wonder which version is required?Thanks.

Testcode:

<?php
$arr = str_split("lsdjflsdjflsdjflsdjfl");
print_r($arr);
?>

Message:

Fatal error: Call to undefined function:  str_split() in /test.php on line 3

Edit @Justin Johnson

I checked the server's system directory,and I found there are two versions of PHP in Dreamhost.In user's webroot,file will be parsed by PHP5 and that's why I got php 5.2.12 by putting a phpinfo.php in the webroot.And if php files are ran in command line directly using php test.php,another php version which is 4.x worked.That's the reason I got an error.When I use

/usr/local/php5/bin/php test.php

Everything is fine.

+1  A: 

The version required is PHP 5 or later. So theoretically your program should work.

Chathuranga Chandrasekara
A: 

Could be anything in your code. How do we know its not a 10 line script or 2000 line script?

Kevin
+1  A: 

If you can't get str_split to work, just use a string as an array:

$stuff = "abcdefghijkl";
echo $stuff[3];

will produce

d

This method is fastest, anyway. I don't know if it suits your needs, but if it does, I hope it helps!

mattbasta
Yeah...But it cannot be used in foreach.
SpawnCxy
@SpawnCxy But you don't need foreach to iterate a string!
Col. Shrapnel
@Col. Shrapnel I'm not iterating a string,I need to deal with every character in it.Obviously I cannot do it one by one manually,you know.
SpawnCxy
@SpawnCxy to deal with every character in the string, you **have** to iterate it. And foreach is not the only one loop operator in PHP.
Col. Shrapnel
@SpawnCxy Just use a plain old `for` loop: `$c=strlen($stuff); for($i=0;$i<$c;$i++) { ... }` It should work just as well for dealing with every character.
mattbasta
+2  A: 
$s="abc";
$i=0;
while(isset($s[$i])) echo $s[$i++]." ";

see?

Col. Shrapnel
Got you!My bad.Just forgive my ignorance.
SpawnCxy
+1, but you should add more explanation to this answer so that it doesn't depend on the comment thread of a separate answer.
Justin Johnson
+2  A: 

According to dreamhost wiki, you need to switch to php5 manually from control panel, if you created your domain before 2008 sept.

http://wiki.dreamhost.com/Installing_PHP5#Using_DreamHost.27s_PHP_5

PHP 5 was added to all plans by DreamHost as of June 2005. As of September 2008, support for PHP4 was discontinued, so you can no longer switch back to PHP 4 from PHP 5 from the panel.

If you haven't switched to PHP 5 yet, you can do this in the Control Panel. But, again, you will not be able to switch back to PHP 4 after switching to PHP 5.

Here's how to switch from PHP 4 to PHP 5:

  1. Log into the DreamHost Control Panel.
  2. Click Domains, then Manage Domains.
  3. Click the wrench icon next to the domain you want to activate PHP 5 on (under the Web Hosting column).
  4. Select PHP 5.x.x from the dropdown menu.
  5. Click Change fully hosted settings now! at the bottom of the section.
  6. Repeat steps 3-5 for each additional domain you want to activate.

you could also check your php version with

<?php  
   phpinfo();
?>
S.Mark
Thanks for your notice.
SpawnCxy
+2  A: 

First off: The PHP documentation will always say what version is required for every function on that function's documentation page directly under the function name.

It is possible that an .htaccess file is somewhere in your path and is causing a previous version (<5) of PHP to be used. To double (or triple) check to make sure that you are running in the proper PHP version, place this code above the line where you call str_split

echo "version:", phpversion(), 
    "<br/>\nstr_split exists? ", 
    function_exists("str_split") ? "true" : "false";

However, as shown by Col. Shrapnel, it is not necessary to convert a string to an array of individual characters in order to iterate over the characters of that string. Strings can also be iterated over using traditional iteration methods, thus making the call to str_split unnecessary and wasteful (unless you need to segment the string into fixed length chunks, e.g.: str_split($s, 3))

foreach ( str_split($s) as $c ) { 
    // do something with character $c
}

can be replaced by

$s = "lsdjflsdjflsdjflsdjfl";

for ( $i=0; isset($s[$i]); ++$i ) {
    // do something with character $s[$i]." ";
}

which is equally, if not more clear.

Justin Johnson