Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have in a foreach loop:
echo "<span style=\"" . myCss($value) . "\">lol</span>";
Which turns into (in source):
<span style="">lol</span>color: #999999;background-color: transparent;font-weight:normal;text-decoration: none;<span style="">...
Why? how to prevent the browser? Same for Chrome and Firefox. Note there is a reason for it being in-line, an I want to avoid doing it via javascript.
Try this
echo "<span style='" . myCss($value) . "'>lol</span>";
How about a little separation of PHP and HTML:
<span style="<?php echo myCss($value); ?>">lol</span>
Notice I encapsulate the PHP within the quotes, rather than echo the entire line. In a foreach loop it would look something like:
<?php
foreach($array as $key => $value){
?>
<span style="<?php echo myCss($value); ?>">lol</span>
<?php
}
?>
This separation of PHP and HTML has been the standard practice everywhere that I have worked, and I personally find it to be much more transparent.
Without seeing your function and the values of the variables, I an only assume that there are characters in the echoed result that mess up the html. You should always use htmlspecialschars() when you output to html:
echo "<span style=\"" . htmlspecialschars(myCss($value)) . "\">lol</span>";
Although you would probably use it in your function.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to display an image using a variable "$images" that contains the URL parsed form an API.
This is my code:
echo "<td>""<img src='",$image,"'>""</td>\n";
I assume there is a typo I cannot detect because I get a blank screen when running this.
echo "<td>"."<img src='".$image."'>"."</td>";
Or
echo "<td><img src='".$image."'></td>";
You were missing the dots/commas after/before the td tags
use . not ,
<img src='".$image."'>
PHP requires that you chain your strings together using a .
E.g.
echo 'Test' . ' ' . 'Hello'; // Test Hello
Or simply :
echo "<td><img src='$image'></td>";
Check documentation.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So within my php file I have multiple sets of opening and closing php tags. It basically looks something like this:
<input id="myID" type="text" value="<?php
$value="x";
echo $value;
?>">
<input id="yourID" type="text" value="<?php echo $someValue; ?>"
Is there anyway to refer to $value in the second set of php brackets? I tried using GLOBAL['index'] to refer to $value, but I get an undefined index error.
Any help or guidance is appreciated.
Just use echo $value.
The variable context doesn't change just because you have re-opened the PHP tag. <?php and ?> are just flags for the parser, and have no bearing on what your code does inside of them.
Since you're just getting started, I also recommend looking into a templating engine such as Smarty. This will help you separate application logic from your output. Also, be sure to use htmlspecialchars() around any arbitrary data used in the context of HTML, to ensure that reserved characters are escaped, and that you aren't creating any XSS attack points.
PHP's variable scope is function-level. Closing a <?php ... ?> code block doesn't change your scope level.
e.g.
<html>
<?php $x = 'foo'; ?>
<body>
<?php echo $x; ?>
would output foo as expected. If you were using functions, then it'd be a different matter:
function foo() {
$x = 'foo';
}
foo();
echo $x; // undefined variable
and that wouldn't change no matter how many/few <?php ... ?> code blocks you were using.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
It's a basic question, but I've googled all sorts of variations of it & nothing that quite answers me. I want to display one of two images. One if the criteria is met & one if it is not met. This code works. If criteria is met, it does display the image. But there is no alternative
<? if(stripslashes($getfeedbackQryRow['CompanyID'])=='344'){?><img src='img1.png' alt='Todays Bite'><? }?>
Then I put this together, but it's still wrong
<?PHP
if ($getfeedbackQryRow['CompanyID']) == '344') {
print ("<IMG SRC =/img1.png>");
}
else {
print ("<IMG SRC =img2.png>");
}
?>
This one looks like it should be right, but it throws an error... It's probably a syntax issue. Does anyone know what I'm doing wrong?
You have an extra ")"
if ($getfeedbackQryRow['CompanyID']) == '344') {
Remove the ")" here ['CompanyID'])
if ($getfeedbackQryRow['CompanyID']) == '344') {
// ^------- this is causing your error
You should do something simple like this:
$image_name = $getfeedbackQryRow['CompanyId'] == '344' ? 'img1.png' : 'img2.png';
echo '<img src="' . $image_name . '">';
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
im having trouble using an image search within php tags, is it possible?
My code bellow:
<?php
echo "<div id=reviewname>Name: ".$dname['name']."</div>";
echo "<div id=reviewseat>Seat: ".$dseat['seat']."</div>";
echo "<div id=srating>Sound: ".$dsrating['s_rating']."</div>";
echo "<div id=crating>Comfort: ".$dcrating['c_rating']."</div>";
echo "<div id=vrating>View: ".$dvrating['v_rating']."</div><br /><br/>";
echo "<div id=reviewcomment> Comments: ".$dcomment['comment']."</div>";
?>
I would like within these div's to be an image src example being:
echo "<div id=srating><img src="images/soundimg.png" alt="Sound" height="35" width="35"></div>Sound: ".$dsrating['s_rating']."</div>";
The above does not work, is it a syntactical issue?
Thanks
You need to escape the quotes inside the echo statement
echo "<div id=srating><img src=\"images/soundimg.png\" alt=\"Sound\" height=\"35\" width=\"35\"></div>Sound: ".$dsrating['s_rating']."</div>";
If not escaping, you can also use single quotes for attribute values.
echo "<div id=srating><img src='images/soundimg.png' alt='Sound' height='35' width='35'></div>Sound: ".$dsrating['s_rating']."</div>";
If you need to echo and in string u need to put " quotes, you can use single quote around ur string.
echo '<div id=srating><img src="images/soundimg.png" alt="Sound" height="35" width="35"></div>Sound: '.$dsrating['s_rating'].'</div>';
same for single quote.
But if you need both single & double quotes you can use any method and use \' or \"
Or you can have a very useful too, heredoc syntax.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
how can I print out a variable name in text in php?
I want to accomplish something as simple as having this:
echo "|" . "$myvariable" . "|";
print out this:
|$myvariable|
instead of this:
||
how can I accomplish this?
Use single quotes so that the variables don't get interpolated:
echo "|" . '$myvariable' . "|";
As others pointed out, use single quotes to show the variable name "as is".
echo "|" . '$myvariable' . "|";
Be sure to read this part of the manual.