What am I doing wrong here?
$array = array('sky'=>'blue', 'grass'=>'green', 'sun'=>'yellow');
$key = array_search('green', $array);
echo $key;
error: Parse error: syntax error, unexpected T_DOUBLE_ARROW in /Applications/XAMPP/xamppfiles/htdocs/search-array.php on line 2
$array = array('sky'=>'blue', 'grass'=>'green', 'sun'=>'yellow');
$key = array_search('green', $array);
echo $key;
The source you posted works perfectly and returns 'grass'. Are you sure it is not another snippet in your application?
You may want to try finding if you closed the array right and if you have commas where they are needed. If that doesn't resolve it for perfectly functional syntax:
error_reporting(E_ALL^E_NOTICE);
Long shot, but worth a mention, I feel:
I've had the experience that sometimes, some FTP programs error out silently if they don't manage to transfer the whole file. If you're experiencing the error on a webserver and you pasted us the local code (and only then), that might be what's causing your problem; e.g. if your remote file looks like this due to an incomplete transfer:
$array = array('sky'=>'blue', 'grass'=>'green', 'sun'=>
I've gotten some odd errors over time that I couldn't explain that then boiled down to an incomplete transferred file.
I'd recommend that whenever you encounter a parse error you can't find, try re-uploading the file. If it still occurs, chances are you overlooked something.
(Needless to say that if this does happen to you, you should probably look into a better FTP client. :) Mind, I don't take this advice, I like mine too much, this is its only shortfall.)
Related
The following code
<?php
echo ((12+1)%12)."<br/>";
echo ((12+1) % 12)."<br/>";
?>
leads to an unexpected result (13,1) instead of (1,1) on phpfiddle.org but it runs as expected on my server.
http://phpfiddle.org/lite/code/qdb-s4t
Is this an error in their sandbox or does it have to do with different php versions? How is the case without spaces interpreted?
I was just looking at some code for quite a long time and couldn't understand what was the difference.
i know i could use fmod or other sandboxes like http://ideone.com/.
PHPFiddle is just a website that is attempting to provide an easy way to execute PHP code samples from the browser. This isn't going to give you native behavior, simply because the code is going to be processed by JavaScript first using whatever logic the people at PHPFiddle seem fit. This leads to the possibility of bugs that have nothing to do with PHP and that is what is going on here. If you turn those same lines of codes into full strings, you will see the output still isn't even correct.
PHP part:
$php = $_POST['php'];
//$php = "print \"hello world\";";
if ($php != null){
if (strlen($php) < 400){
echo $php;
eval($php);
//eval("print \"hello world\";");
}else die ("Evaluated Expression Exceeds Maximum Length");
}
LSL part:
string php = "print \"hello world\";";
Now I added the commented out bits into PHP to show that it works. And then when the LSL script sends to PHP it returns:
print \"hello world\"; -- this line is from, 'echo $php;'
<b>Parse error</b>: syntax error, unexpected '"', expecting identifier
(T_STRING) in <b>xxxxxx.php(141) : eval()'d code</b> on line <b>1</b><br />
-- this is the error.
And it is something to do with the the way the two scripts are sending data. I thought maybe had something to do with $php = $_POST['php']; so changed it to $php = $_POST[php]; With no change to the result. I then tried changing print \"hello world\"; to print 'hello world'; It then just returns the error : T_ENCAPSED_AND_WHITESPACE.
I did not supply the full source here. Only the section that was having an issue. It is supplied in a example state. The output is the same as the actual error result, that is being seen in the source. Usage of eval is required for the lsl script and php. In that the code is dynamically being reconfigured by both and sent to one another. Essentially giving the two the ability to code into one another. This is for a game in Second Life.
So if anyone knows of an actual way to pass the required data to and from the scripts. I could use some advice. Or a smack in the head if I missed something simple.
With the kind poke from mario on turning off magic_quotes. I then found what the data was doing in the source. I then ended up researching and using the following : eval(stripslashes($php)); Which completely solves the issue. And based on marios poke.
It had nothing to do with escape data. Didn't think so as echo reported that fine. And it was indeed a slap me in the head error too.
stripslashes — Un-quotes a quoted string
Will vote this as best answer and also a best answer for mario. Wish he would have done his as a answer over comment. So could have voted it.
Basically, php -l does not detect any syntax error, given this code:
<?php
date®;
?>
Obviously, it's an error when you execute it.
Is there any alternative or additional linter to use for PHP?
EDIT:
Thanks alot guys. Apparently it is a valid constant name, as the documentation suggests ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*).
It only errors because you haven't a constant definition for date®
If, somewhere in another script file before an include of your test file you have:
define('date®','Value');
this would be valid (albeit meaningless) and would run without error
So syntactically this file is valid, and a lint check duly reports it as such
What you are looking for is php_check_syntax (http://php.net/manual/en/function.php-check-syntax.php).
Let me correct my answer. It will not detect the latter code as an error (as noted in the comments, this isn't erroneous code). Though, you should stick with the latter function if you want to check file for errors.
I'm trying to get some PHP example code to work on PHP version 5.3.4, Apache 2.2.17 on Windows.
The example says I need PHP 4.0 and above with CURL and contains:
<?
$function = $_GET['function-if-exist'];
$test = "Test";
?>
<? =$test ?>
I don't understand why I'm getting the following errors:
My PHP doesn't understand <? and wants <?PHP instead.
My PHP doesn't like <? =$test ?> and wants something like
<?PHP echo $test ?>
$function = $_GET['function-if-exist']; causes the error "Undefined index" but presumably works for the folks that developed it.
Can anyone help me understand why their code is not working for me?
1) <? is the "short tag". Most servers are configured to not allow short tags. This can be changed in php.ini.
2) Again, short tags. Also I think you can't have a space before the =, but the main problem is the short tags setting.
3) $_GET accesses the query string, so when loading your script you need myscript.php?function-if-exist=something
It is more ideal to check if the parameter is set before continuing to prevent errors being thrown, e.g.
if(isset($_GET['function-if-exist']))
{
$functionexists = $_GET['function-if-exist'];
}
the short tag notation is disabled in your php.ini
you need to remove the space before your equal sign
your _get array contains not the expected index, what url do you enter to access the page?
I don't understand why I'm getting the following errors:
My PHP doesn't understand
To be able to use short tags you will have to enable them via config ... http://www.tomjepson.co.uk/tutorials/35/enabling-short-tags-in-php.html
My PHP doesn't like and wants something like
Once you switch on the short tags you will be able to echo using ... important the equals signs must be touching the ? not variable.
$function = $_GET['function-if-exist']; causes the error "Undefined index" but presumably works for the folks that developed it.
The $_GET is populated according to what is in the url. To get a value in $_GET['function-if-exist'] the url accessing the script should be something like mydemo.php?function-if-exist=hello
Hope this helps you
Quick answers to 1 and 2 are enable the short_open_tag option into the php.ini file, for the last one is set the error_reporting to a less strict mode.
The reasons of not to adopt such measures are:
the short tag clashes with the xml declaration and is disabled on different host, if you need to manipulate xml or if you need to write portable code is better to resort to the long tag syntax. You lose the ability to echoing data with = but it is a small annoyance to me.
Warning and notices, as php forgive a lot the programmer for missing variables declaration are a blessing for debug. Keep then raised and you will address a lot of mispellings.
Are you sure that function-if-exist is a correct index for your hash? I would check the index first the access them. If the index don't exists is a probable hint that something is going wrong with your code and you should check the reason of the missing.
Better to stop now, as anyone can write a book on this topic, and several ones already done ;)
I've got PHP and HTML code stored in a database table. When I get this data, I need to echo the HTML and process the PHP. I thought I could use eval() for this, which works, if I do this eval("echo 'dlsj'; ?> EVALED "); I get "dlsjEVALED" printed out.
The problem is, I get a fatal error when I run longer scripts. Things like:
Parse error: syntax error, unexpected '<' in /home/content.php(18) : eval()'d code on line 1
Best advice - never store php and html code in your database. And avoid eval() like the plague.
I can't really tell what's wrong with your code, as you haven't provided enough information. But even if I did have some advice, I don't think I could give it in good conscience.
You should redesign your whole application so that it doesn't require storing such things in the database. I can't imagine why it would be necessary.
just right der...........
eval('?>' . $content .'<?php');
You need to re-open php mode after the EVALED. Apparently you have to do this with <? rather than the full <?php.
As a rule eval is to be avoided. But rules are made to be broken. There's a thread at When is eval evil in php? that gives some less dogmatic advice.
Depending on what you want to do, it might be suitable to use a template file that you source, with text that will vary stored in a local variable prior to sourcing the template.
As for storing code to be executed in the DB... this does happen in some frameworks like Drupal to provide convenient extensibility, but then Drupal is pretty thoroughly scoured for security weaknesses.
Also if you're writing self-modifying code then you need to use eval(). Not sure if anyone has done that in php but it would certainly be interesting.
I would guess that you're trying to eval() something that contains an opening <?php tag. And that leads to the error at hand.
$contents = htmlentities($contents);
echo html_entity_decode(eval($contents));