Why does <?= work only sometimes where <?php works? [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 2 years ago.
Inside the body of an html document I have something like this:
<?=str_replace(' ', '_', $result[0]['something'])?>
This works perfectly fine. In the same document I have this:
<?php if(!empty($result[0]['something'])) { echo "Hello"; } else { echo " "; }?>
Which also works fine, but it slightly bothers me that I am using <?= in one place and <?php in another. When I try to change the if code to become:
<?=if(!empty($result[0]['something'])) { echo "Hello"; } else { echo " "; }?>
or
<?= if(!empty($result[0]['something'])) { echo "Hello"; } else { echo " "; }?>
Both result in a Parse error: syntax error, unexpected 'if' (T_IF) in....
I've attempted to find some documentation on the respective differences between <?php and <?= as a php opening tag but all I get is data on short tags - which this is not. Can someone explain this behavior for me?

<?= is like <?php echo. You can't echo an if statement.

Related

I can't get to echo images with php [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am trying to echo multiple images but i get the error:
"PHP Parse error: syntax error, unexpected '?>' in
/public_html/View/Pages/Home.php on line 17".
Can you give me any guidance on how to fix it? code below
<?php
for($i=0;$i<$length;$i++) {
echo ?><img class="meme-image" src="<?php $meme["$imd_id" == "$i"]->$path?>"><?php
}
?>
Try By this.
<?php
for($i=0;$i<$length;$i++) {
echo '<img class="meme-image" src='.$meme[$imd_id == $i]->$path.' ">';
}
?>
Note : You do not require to close php tag. instead of that insert this in php echo.and also you must read this For best practice.

PHP loop is not throwing the correct link [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
for my issue i'll try to be as brief as possible
what am trying to do is reference a page with a specific id in the HTML anchor link with PHP as the below
<body>
<?php $linkName = "Second Page"; ?>
<?php $id = 5; ?>
<?php echo $linkName?><br>
and it works fine
now what am trying to do is to make the $id part more dynamic by making looping the number from 1 to 10 and also providing 10 links
the code is
</head>
<body>
<?php $linkName = "Second Page"; ?>
<?php $id = 5; ?>
<?php
for ($i=0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=<?php echo $i;?'>Link1</a>";
};
?>
</body>
however what i did notice as the below images indicates when i hover on the links i noticed that i refer to a strange link
and when i cliched on it it takes me to the following link with an id that i did not want as below
http://localhost/PHP_Course/secondPage.php?id=%3C?php%20echo%201;?
i tried researching the subject and i tried escaping the quotation but it does not seem to resolve the problem
Any help please ??
<?php and ?> tags indicate to the PHP preprocessor that anything inside them is code and needs to be parsed, everything outside is just text PHP doesn't touch.
Inside the <?php tag, "<?php" string has no special meaning, so is printed. You do not need to open and close tags all the time, try this:
</head>
<body>
<?php
$linkName = "Second Page";
$id = 5;
for ($i = 0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=$i;'>Link1</a>";
};
?>
</body>
You're echoing a string in PHP, and using <?php... inside that string.
Solution:
echo "<a href='secondPage.php?id=" . $i . "'>Link1</a>";
id=$i will also work, because you can include variables directly in double-quoted strings.
You're echoing the PHP code itself as a string. You don't need to put PHP code inside of PHP code. Just concatenate the values you want to echo:
echo 'Link1';
because you already started an echo statement so you don't need to add another PHP starting and ending tags. just check my code below and try it.
<?php
for ($i=0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=".$i."'>Link1</a>";
} ;
?>

php can't find syntax error in echo line [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
So I am getting the following parse error when this file loads:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
expecting identifier (T_STRING) or variable (T_VARIABLE) or number
(T_NUM_STRING)
I have tried everything to work out why and I narrowed it down to the
href=\"start.php?id=<?php echo $res['id'] ?>\"
section of the code. I am sure I left out a ' or " but unsure where as
it all makes sense to me. Can anyone with a keener eye see where I am
going wrong? Thank you.
My code:
<td>
<?php if($res['ndaSent'] == "No") {
echo "<span class=\"buttonTestDisabled\"> Start Test</span>";}
else {
echo "<a class=\"buttonTest\" href=\"start.php?id=<?php echo $res['id']
?>\">Start Test</a> ";}
?>
</td>
Take a look at that. You can't use echo command inside of an echo " " line. Try using ' inside and not \ may help you too from confusion.
<?php if($res['ndaSent'] == "No") {
echo "<span class='buttonTestDisabled'> Start Test</span>";
}else{
echo "<a class='buttonTest' href='start.php?id=".$res['id'].">Start Test</a>';}
?>
</td>

Unable to print PHP tags inside [duplicate]

This question already has answers here:
How to display raw HTML code on an HTML page
(30 answers)
Closed 6 years ago.
I want to print PHP declaration tag <?php ?> inside HTML output using echo
For example my code is below:
$var = '<?php echo "hello world"; ?>';
echo $var;
But it gives me empty result, when I add something after the $var in echo as:
echo $var . "<b>hello world<b>";
This gives output:
hello world
But It should give result as <?php echo "hello world" ?> Hello world
How it can be possible?
For browser it's just a html tag, because it starts with < and ends with >. Browser views it as invalid html tag and displays nothing.
Try using echo htmlentities($var)."<b>hello world</b>";

how to display <li></li> tag only after login [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I am trying to display the logout button only if any user is logged in.
following is the code:
<?php
if(isset($_SESSION['user']))
{ echo '<li><a href='logout.php'>Logout</li>'; }
?>
But it is giving error.
Error:Parse error: syntax error, unexpected 'logout' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\index.php on line 42
Help me to display this link.
You can't use unescaped ' characters in string literals delimited by ' characters. Either:
Use " instead
Escape the '
Drop out of PHP mode and just write the HTML as normal template code
Such:
<?php
if(isset($_SESSION['user'])) {
?>
<li><a href='logout.php'>Logout</li>
<?php
}
?>
You are closing your quotes and opening them again. Try this:
<?php
if(isset($_SESSION['user'])) {
echo '<li><a href="logout.php">Logout</li>';
}
?>
Note the double quotes.
Alternatively, escape the quotes like this:
<?php
if(isset($_SESSION['user'])) {
echo '<li><a href=\'logout.php\'>Logout</li>';
}
?>

Categories