What php configuration variable disallows concatenated array indexes? - php

E.G.
if(!isset($am_states[$lot.'_-40C'])){
or
$am_states[$temp."_".$states[$i]['temperature']] = $states[$i]['temperature'];
Whenever I have arrays with concatenated string as array-keys php returns an error:
Parse error: syntax error, unexpected '.', expecting ']'
So I am assuming something is wrong with the server configuration although I am sure i changed something on my local configuration.
Last time i changed the configuration was when i setup my apache/mysql/php installation
that is PHP Version 5.3.1,Apache/2.2.14,MYSQL5.1.41 (default from xampp1.7.3)
so I was using this syntaxes 7 months ago and they were working properly. It just now that they produce errors.
Anyone can help?

Are you positive about the PHP version you're using? The following test (using PHP 5.3.6 (cli)) works without issue. Perhaps you could post a more complete example?
#!/usr/bin/env php
<?php
$states = array(
array('temperature' => 40),
array('temperature' => 50),
array('temperature' => 60)
);
$temp = 'test';
$i = 2;
$am_states[$temp . "_" . $states[$i]['temperature']] = $states[$i]['temperature'];
var_dump($am_states);
The output of this script is:
array(1) {
["test_60"]=>
int(60)
}

Related

XAMPP PHP Named Arguments gives " syntax error, unexpected ':', expecting ')'"

In an include file I declare a function
function patch_163_output_row_header(
string $title,
string $label,
$help,
bool $read_only=false,
bool $required=false,
string $label_cell
)
and when I compile this using the XAMPP PHP compiler (8.1.6 as far as I can tell) it is OK
In another file I try to use the function
patch_163_output_row_header(
title:$title,
label:$label,
help:$help,
read_only:$read_only,
required:$required,
label_cell:$label_cell
);
when I try to compile I get the error mentioned above on the"title" line.
I can see no obvious issues - title does not seem to be a reserved word?
The strange thing then is that these two files are part of a website and when they are executed by the same XAMPP stack (presumably using the same PHP compiler?) there are no errors reported and they work.

Embedding PHP inside PHP

I have recently put up a server, and I am making changes gradually. One of the changes is the contents of a php page. I commonly use a php "shell" (just a textarea and the php just evals what is written). I have made some changes and want to use
fwrite($file, "<?php php here ?>");
but i am getting an error. I believe I know the problem, and it is because i have nested php like
<?php
$a = fopen('../php.php', 'w');
fwrite($a, "
<?php
eval($_POST['phptorun']);
?>
");
fclose($a);
?>
Is there any way I can get it to just put that php code into the file php.php?
The error i was getting is:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/html/Tyler/replace.php on line 5
Edit:
I forgot to say a couple of things. The server is a LAMP (Linux Apache MySQL PHP), running on my Raspberry Pi 2, model B. If you want to visit my server, it is at 71.204.114.18
Try to escape the $ symbol:
<?php
$a = fopen('../php.php', 'w');
fwrite($a, "
<?php
eval(\$_POST['phptorun']);
?>
");
fclose($a);
?>

Parse error: syntax error, unexpected '[' in whatsapp api with php 5.3

My script is working really fine on my xampp php version 5.6. Now I tried to upload it on the server php version 5.3.3 , it says
PHP Parse error: syntax error, unexpected '['
The line which its mocking about is this one:
$this->pending_nodes[$number] = [];
The whole code block looks like this:
public function addPendingNode(ProtocolNode $node){
$from = $node->getAttribute("from");
if(strpos($from,Constants::WHATSAPP_SERVER) !== false)
$number = ExtractNumber($node->getAttribute("from"));
else
$number = ExtractNumber($node->getAttribute("participant"));
if(!isset($this->pending_nodes[$number]))
$this->pending_nodes[$number] = [];
$this->pending_nodes[$number][] = $node;
}
I am thankful for any help, I cannot seach [ with google and have no idea where it could come from, since on xampp its working fine.
Short Array syntax was introduced in PHP 5.4 only.
Change
$this->pending_nodes[$number] = [];
to
$this->pending_nodes[$number] = array();
or upgrade to a PHP version that is not End of Life.

sprintf bug with php & apache in windows?

I've run into a strange problem on a WAMP server setup (PHP version 5.3.0, Apache 2.2.11). When using sprintf to output a number, I occasionally get erroneous characters in the output string.
Example: (not trimmed from anything, this is the only code in the script)
$dt1 = new DateTime('now');
$dt2 = new DateTime('now - 10 min');
$interval = $dt1->diff($dt2);
$number = 10.0;
$string = sprintf("%.1f", $number);
echo "number: $number, string: $string\n";
If I run this at the command prompt with PHP CLI, I get the expected output:
number: 10, string: 10.0
However, if I serve it using Apache, in the browser I get
number: 10, string: :.0
with a colon where '10' should be. (Note that ':' is the next ascii character in sequence after '9', if $number is 0-9, everything works. Numbers greater than 10 appear to use ascii equivalents - so 11 is ';', 12 is '<', etc.)
The strangest part is that the first four lines in the above code sample seem to affect the results. Logically, those statements should have no impact, but if I comment them out or remove them the problem goes away.
Any ideas? Anyone else able to replicate this?
Notes:
I've tried php 5.3.1 and 5.3.2, both behave the same way
The above script works fine, even in the browser, for 5-6 page refreshes after restarting Apache. Then the error, as described, returns
Try adding this above the code setlocale(LC_ALL, 'en_US');
Try this:
Change echo "number: $number, string: $string\n"; to:
for ($i = 0, $n = strlen($string); $i < $n; $i++) {
echo ord($string[$i]).' ';
}
It will basically give you the numeric character code for each byte in the string. Note that I said byte. If it's a character set problem, or a problem with Apache mangling bytes, you should see that here. Expected output is: 49 48 46 48. If you instead see 58 46 48, then you indeed may have found a bug with php and should submit a bug report. You also should try upgrading your php version (5.3.2 is out)...

Which PHP version is required for str_split?

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.
Rather than use str_split, it's usually much easier to iterate through the characters of the string directly:
$s="abc";
$i=0;
while(isset($s[$i])) {
echo $s[$i++]." ";
}
see?
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.
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:
Log into the DreamHost Control Panel.
Click Domains, then Manage Domains.
Click the wrench icon next to the domain you want to activate PHP 5
on (under the Web Hosting column).
Select PHP 5.x.x from the dropdown menu.
Click Change fully hosted settings now! at the bottom of the
section.
Repeat steps 3-5 for each additional domain you want to
activate.
you could also check your php version with
<?php
phpinfo();
?>
The version required is PHP 5 or later. So theoretically your program should work.
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!
Could be anything in your code. How do we know its not a 10 line script or 2000 line script?
You can use preg_split() to split an array into single characters, but it will return an extra empty string at the begining and the end.
$a = preg_split("//","abcdefg");
echo json_encode($a);
prints:
["","a","b","c","d","e","f","g",""]

Categories