function triggers eventhough without terminator - php

I have this sample functions in php.
<?php
function sample_function($atts)
{
echo $atts."<br/>";
}
sample_function("James");
function another_function(){
echo 'this is another function <br/>';
}
another_function()
?>
The above code outputs
James
this is another function
I was expecting some kind of syntax error, unexpected end of file but it did not output error.
My php version is 5.6.15
I was wondering if this is a kind of bug in this php version?
Or I don't know that this is possible in php .Please give some information .Thanks!

The closing tag of a block of php includes semicolon at the last lines so you don't need to put semicolon there That is why your function automatically triggers, add something after that function line it will not work and tell you there is an error

Related

How do I get this PHP function to work with usort()?

I copied and pasted the piece of code below from http://php.net/manual/en/function.usort.php :
<?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";
}
?>
At first it gave some error about the $b variable. Now the new incessant error is:
Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\testsort.php on line 4
I'm thoroughly confused as to the problem here.
When I copied and pasted the code, some strange characters were part of it. These were invisible within notepad but revealed by VS Code IDE. Removing them allowed the code piece work.

PHP execution path (parser)

See the following snippet:
<?php
die("----die----");
sfafsadffas
echo "foo";
echo "bar";
?>
The result is:
Parse error: parse error in test.php on line 6.
Which is a bit unexpected, I would have thought we should get ----die----.
Now see the following:
<?php
die("----die----");
echo "foo";
echo "bar";
sfafsadffas
?>
The result is:
----die----
What exactly is going on here?
The echo is indirectly effecting your program: besides printing to screen/browser the echo is doing flush to the output buffer which tries to print all the output that was "aggregated" so far. Since there's a syntax error before the flush it will be discovered at this point. You can reproduce the same error by doing:
<?php
die("----die----");
sfafsadffas
flush();
?>
But, if you'll flush the output buffer before the parser reaches the syntax error - the die will be executed.
<?php
die("----die----");
flush();
sfafsadffas
?>
will output ----die---- and then the parser will stop execution because it died and you won't reach the line that causes a syntax error.
The first thing PHP does when executing a file is to run a syntax check looking for obvious errors. This includes missing ; semicolons, mismatched braces {}, etc.
The first snippet fails this first test:
<?php
die("----die----");
sfafsadffas // <-- no semicolon between this line of code and the next,
// so you get a 'parse error' before the file even gets run
echo "foo";
echo "bar";
?>
The second snippet survives the syntax check because of a little "feature" of PHP: the parser counts ?> as a semicolon. So when you run:
<?php
die("----die----");
echo "foo";
echo "bar";
sfafsadffas
?>
The syntax check says "OK" and PHP proceeds to run the file. And the file would in fact be perfectly valid if sfafsadffas was defined sometime earlier in the file.
And then because of your die, the code never gets down to line 7.
The first fails, because the parser see's the invalid text and errors out.
The second may or may not fail depending on your PHP interpreter, it may not fail because the pre-processor (or parser) is ignoring extraneous data at the end of the file.
the sfafsadffas is considered as a constant here.
In example 1, put a semicolon after sfafsadffas and you are using a constant a normal way so it works.
In example 2, You do no need to have a semicolon separate last line of code see this
Example 1:
It should not execute. Because the coding will run line by line. The invalid text will stop remain executions. Finally nothing will execute and execution will be fail.
Example 2:
By the reason of line by line execution the both echo statements will execute ,but finally the execution status will be fail.
Throws Parse error
Parse error: syntax error, unexpected $end on line 7
Please check
http://writecodeonline.com/php/

Issue with dynamically generated javascript link with PHP

example link
returns PHP error:
syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';'
Any ideas? (Hope this question makes sense. I'm trying to fix someone else's code.)
Check that there's not a "<?" or "<?php" at the beginning of the file. This code assumes you're not already within a PHP block. If you are, then you'll have to adjust (remove it). Or, you'll need to add close ?> somewhere before this html, and a <?php after it...
Sorry about that guys, the problem was <?php echo val;?> instead of <?php echo $val;?>. Sometimes the eyes betray the mind...

Parse error: syntax error, unexpected T_SL on line 23

I am getting this error:
Parse error: syntax error, unexpected
T_SL on line 23
Here is line 23:
$selectorder = <<<ORDER
Here it is in context:
$grid->setUrl('myfirstgrid.php');
$selectorder = <<<ORDER
function(rowid, selected)
{
if(rowid != null) {
alert("selected: "+rowid);
}
}
ORDER;
$grid->setGridEvent('onSelectRow', $selectorder);
What is causing this error?
I personally don't know what <<< does and have never used it, I got it from a tutorial. I tried to google it, but you can't google characters like that :(
Check for whitespace after <<<ORDER. There should be no blank characters.
<<< is for heredoc: See manual
Make sure that there is no SPACE/INDENTATION before ending ORDER;
PHP Heredoc does not get on well with the % symbol, and the following also causes Parse error: syntax error, unexpected T_SL:
<?php
$var=<<<%%SHRUBBERY%%
Nih!
%%SHRUBBERY%%;
?>
Also make sure that you have 3 '<<<'. Omitting one will throw this error. Also if your using NOWDOCs, make sure your hosting provider has php 5.3 installed. Plus if your php environment is below 5.3, do not use double quotes or single quotes.
It's called "Heredoc syntax", and it lets you specify large strings without using quotes. In this case, it looks like you're using it to put JavaScript code into a variable. Since you started the string with <<<ORDER, you should be able to finish it with ORDER;, as you have — but you need to make sure that ORDER; occurs at the start of a line, with no whitespace before it.

T_STRING error on line that just says <?php

So I'm writing a script in codeigniter, and I get the following error message:
Parse error: syntax error, unexpected T_STRING in /home/globalar/public_html/givinghusband.com/system/application/controllers/sizes.php on line 1
the only problem: the only thing on that line is this:
<?php
So I'm quite mysterified as to what's going on here? Have I typed PHP wrong or what?
There could be a problem with your editor when it updated the file. I just had this problem and the editor removed all line breaks.
If you are uncertain try opening your php file in another editor or use the Cpanel file manager to take a peak.
Or you may have an unterminated quote or statement on some previous line without an ending semicolon (;) . Check all files that are included before this one.
a bare <?php in the file leads to a parse error in php (don't ask). Try adding a whitespace or a newline after it
Sorry.A bit late but might helpful for others.Just looked at your question.Just use
<?
?>
instead of :
<?php
?>
and remove whitespaces/line breaks between your php open tag and class name .It will resolve this conflict.Ta

Categories