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>";
} ;
?>
Related
This question already has answers here:
Escaping quotation marks in PHP
(7 answers)
Closed 1 year ago.
This is my line of code in php which shows the page numbers, which are automatically generated based on the content in the table. Since all the page numbers have the same color I want to display the current active page, I wanted to do it with a condition in the class but due to the anchor tag already being in the "" of echo, and the class starting with ', once i put the ' for condition, the class is ended sooner and the condition is not made.
Is there a way to bypass this or use something else for inside of the ' '?
Or is there another way to show the current active link?
echo "<a class='{% if page.url. =='?page=$x' %}active{% endif %}' href='?page=$x'> $x </a>";
And if anyone needs some more code, this is the whole php code for printing all the pages based on the number of items in the table
for($x = 1; $x <= $totalPages + 1; $x++){
?>
<div class='page'>
<?php
echo "<a class='{% if page.url. =='?page=$x' %}{% endif %}' href='?page=$x'> $x </a>";
?>
</div>
<?php
}
Use a backslash inside echo quotes.
echo "<a class=\"myclass\">";
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 2 years ago.
<?php
$request_uri = $_SERVER['REQUEST_URI'] ;
$path=explode("?",$request_uri);
$pname=basename($path[0]);
if ($pname == "blood-facts-for-kids.html") { $p1 = 'Human Body Facts'; $p1u = 'https://www.factsjustforkids.com/human-body-facts.html'; $p2 = 'Blood Facts'; $p2u = 'https://www.factsjustforkids.com/human-body-facts/blood-facts-for-kids.html'; }
echo '<script type="application/ld+json">{"#context":"https://schema.org/","#type":"BreadcrumbList","itemListElement":[{"#type":"ListItem","position":1,"name":"{$p1}","item":"{$p1u}"},{"#type":"ListItem","position":2,"name":"{$p2}","item":"{$p2u}"}]}</script>';
?>
I'm having issues getting the variables to appear in my echo. Everything works as it should, the variables are set IF the web page name is correct and if I echo out the variables by themselves using
echo "{$p1}, {$p1u}, {$p2}, {$p2u},";
The correct data is shown. I'm obviously doing something wrong in the echo code.
For reference, this is a crude method to inject structured data dynamically.
Either use echo with double quotes "":
echo "<script type=\"application/ld+json\">{\"#context\":\"https://schema.org/\",\"#type\":\"BreadcrumbList\",\"itemListElement\":[{\"#type\":\"ListItem\",\"position\":1,\"name\":\"{$p1}\",\"item\":\"{$p1u}\"},{\"#type\":\"ListItem\",\"position\":2,\"name\":\"{$p2}\",\"item\":\"{$p2u}\"}]}</script>";
or use concatenation:
echo '<script type="application/ld+json">{"#context":"https://schema.org/","#type":"BreadcrumbList","itemListElement":[{"#type":"ListItem","position":1,"name":"' . $p1 . '","item":"' . $p1u . '"},{"#type":"ListItem","position":2,"name":"' . $p2 . '","item":"' . $p2u . '"}]}</script>';
Notice that with double-quotes, you need to escape any other double quote inside the string.
You can use
echo $variable_name //to display the variable
And if you want to display it in a HTML tag then
<p>Your age is <?php echo $age ?>.</p>
Im providing a link for more detailed information
https://www.dummies.com/programming/php/how-to-display-php-variable-values/
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.
This question already has answers here:
How to display HTML tags as plain text [duplicate]
(11 answers)
Closed 7 years ago.
I want to use a while loop to echo out an xml structure including the respective tags. In the example below, however, the tag needs to be printed so the document can the be copied elsewhere:
<?php
while ($row = mysqli_fetch_array($execute)) {
echo "<name>".$row['name']."</name>";
}
?>
In this instance the while loop executes as expected but the opening and closing tags are not printed as plain text. I need to have them printed on screen so that I can copy the produced text elsewhere.
You can use < for < and > for >
Like this:
<?php
while ($row = mysqli_fetch_array($execute)) {
echo "<name>".$row['name']."</name>";
}
?>
Or like this:
<?php
while ($row = mysqli_fetch_array($execute)) {
echo htmlspecialchars("<name>".$row['name']."</name>");
}
?>
Replace < by < and > by >.
Use htmlentities for replace plain text to html entities.
<?php
while ($row = mysqli_fetch_array($execute)) {
echo htmlentities("<name>".$row['name']."</name>");
}
Instead of having to remember all HTML entities, you can ask PHP to convert this for you automatically:
<?php
while ($row = mysqli_fetch_array($execute)) {
echo htmlentities("<name>".$row['name']."</name>");
}
?>
If i understand question:
use htmlspecialchars() function;
But you can copy it from page source (ctr+u in FF and Chrome) or execute php in OS shell and save result to file.
Please consider wrapping up your data in the built-in htmlentities and htmlspecialchars functions of php modules.
<?php
while ($row = mysqli_fetch_array($execute)) {
$var = "<name>".$row['name']."</name>";
$answer = htmlspecialchars(htmlentities($var));
echo $answer ;
}
?>
By default, the browser will try to render the tags in your string, as a normal HTML. You should prevent this default action. You can do two things:
One: is to use htmlentities function, that will produce replace the characters used in HTML tags to their HTML entities.
<?php
while ($row = mysqli_fetch_array($execute)) {
echo htmlentities("<name>".$row['name']."</name>");
}
?>
Two: You can use <pre> $str </pre> tag, that assumes your text is already preformated, and browser will not change any of your characters.
<pre><?php
while ($row = mysqli_fetch_array($execute)) {
echo "<name>".$row['name']."</name>";
}
?></pre>
This question already has answers here:
PHP Dynamic Variable Name
(2 answers)
Closed 8 years ago.
I have a for script:
<?php for($i=1; $i<10; $i++):?>
<?php if($this->item->nuotrauka{$i}):?>
<div class="vobsmall">
<img src="<?php echo $this->item->nuotrauka{$i}; ?>"/>
<?php echo $this->item->nuotrauka{$i}; ?>
</div>
<?php endif; ?>
<?php endfor; ?>
There's 9 variables like $this->item->nuotrauka1, $this->item->nuotrauka2 etc.
As you can see here I'm trying to call these variables using {i} as number of object variable. Hovewer the code above returns single char from variable and not full variable as I want. How should I write {i} here to get what I want ?
Try this:
echo $this->item->{"nuotrauka".$i};