php - variable in string [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to save a string containg a variable to another variable
$bg-color = '#aaa';
$bg_style = 'background: {$block_bg_color_top};';
<div class="block" style="<?php echo $bg_style; ?>">
</div>
I'm trying to echo the code in the style tag like this.
<div class="block" style="background: #aaa;">
</div>

You are trying to evaluate a variable in your background: ... string. But variables inside single quotes aren't evaluated; they have to be in double quotes. Also, you don't need the { and } in this context. Change your code like this:
$bg_style = "background: $block_bg_color_top;";
You could also do $bg_style = 'background: ' . $block_bg_color_top . ';';. In this instance, the two statements are equivalent.
I'm assuming you set $block_bg_color_top somewhere; it's not in the code you posted. You tried to define another variable, $block-bg, but that is not a valid variable name, as explained below.
This is not strictly part of the answer, but please note that you also have a syntax error in your code. This line will result in an error:
$bg-color = '#aaa';
You will get the error:
PHP Parse error: syntax error, unexpected '=' in ...
This is because $bg-color is not a valid variable name; it looks to the parser like you are subtracting color from $bg, but you can't assign to the result of an expression, just like you couldn't do $x - 5 = 7;.

What you're trying to do is the following :
$bg-color = '#aaa';
$bg-style = 'background: '.$bg-color;
<div class="block" style="<?php echo $bg_style; ?>">
</div>

I think this is what you want:
$bg_color = '#aaa';
$bg_style = 'background: ' . $bg_color . ';';

Related

Trying to make this entire line of code into a hyperlink [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
echo <a href = 'test.php'> "CategoryID: " . $row["CategoryID"]. " - Category Name: ".$row["CategoryName"]. </a> "<br>";
This is what i have an is not working properly.
This:
echo "<a href = 'test.php'>CategoryID: {$row['CategoryID']} - Category Name: {$row['CategoryName']}</a><br />";
I am using the { and } as they allow you to include an array in a string and ignore the concatenation which I find harder to read.
I find it funny that you can loop through a MySQL array but can't echo a simple string :P
Some links (teach a man to fish...):
W3Schools
PHP documentation
Codecademy
Tutorials Point
Try this:
<?php
$link = "";
$link = sprintf("<a href = 'test.php'>CategoryID: %d - Category Name: %s </a><br />", $row['CategoryID'], $row['CategoryName']);
echo $link;
?>
Assuming that $row['CategoryID'] is an integer and $row['CategoryName'] is a string.

better way with Simple HTML Dom Parser [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
my HTML code is THE CODE IS REPEATED 16 times :
<div class="headline_image">
<a ga-cat="slideshow-view" ga-action="view-1" href="mylink"><img src="http://dd4994.jpg" width="420" height="323" align="right" alt="my text "/></a>
</div>
I WANT TO GET all the imgs links and text also href what i did :
for ($x = 0; $x <= 15; $x++) {
$imglink = $html->find('div[class=headline_image] img', $x)->getAttribute('src');
$mytext = $html->find('div[class=headline_image] img', $x)->getAttribute('alt');
$postlink = $html->find('div[class=headline_image] a', $x)->getAttribute('href');
echo '<br/>';
echo $mytext;
echo '<br/>';
print_r($postlink);
echo '<br/>';
}
the code is slow any changes ?
You slow down your code by using too much anonymous objects. It means you don't put the result of the function into a variable, rather just use it "on the go". This needs to run your function again and again slowing down your project.
Because you can use the function find to return an array, I advice you to do so before the for loop.
$imgarray = $html->find('div[class=headline_image] img', $x);
This way you run $html->find exactly once, and not sixteen times. In the for loop you can use it as an array and work with the results: $imgarray[$x]. You make the same for $anchorarray and your code will speed up, you'll see.
Alternative solution is using PHP DOM $childNodes on the container in which this 16 item can be found (or the body element). This will return the sixteen div elements in which you can navigate by calling $firstChild for the <a> element and $firstChild again for the <img> element. Probably this is more secure in case you want to make changes to the website (like adding more content to the end etc.)
Hey Daniel i changed the code to :
$imgarray = $html->find('div[class=headline_image] img');
$linkarray = $html->find('div[class=headline_image] a');
for ($x = 0; $x <= 15; $x++) {
echo $imgarray[$x]->getAttribute('src');
echo '<br/>';
echo $imgarray[$x]->getAttribute('alt');
echo '<br/>';
echo $linkarray[$x]->getAttribute('href');
echo '<br/>';
}
In general the proper way to iterate looks like this:
foreach($html->find('div') as $div){
echo $div;
}

How do i assign a variable with the output of an echo? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I got an echo of an class value but would like to assign this value to an Variable:
<p class="<?echo $test="listprice"?>"></p>
I thought about something like this:
<?$testing = echo $test="listprice"?>
But this doesnt seem to work. Is it possible to get the class echo?
When you $test = "listprice"; you are already assigning it to the variable $test. To assign it to the variable $testing if you really want to then you would do this:
$testing = $test;
// display the new variable
echo $testing;
try to separate assignment and output:
<?
$test="listprice";
?>
<p class="<? echo $test; ?>"></p>
Use short open tag of php <? ?>
<p class="<?=$test="listprice"?>">Something</p>
More about
it should be
<?php
echo $test = "listprice";
echo $name = "ankit";
?>

echo inside an echo - does it work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
It's been a long time since I have done php so sorry for the silly question.
This is my current code, I'm trying to save the URL as a variable so that I can insert it into the echo, but it doesn't seem to work as nothing appears:
<?php ob_start();
echo get_post_meta($post->ID, 'oldurl', true);
$old_url = ob_get_contents();
ob_end_clean();
?>
<?php echo do_shortcode('[fbcomments][fbcomments url="$old_url" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
I have echoed $old_url and can see that it has the correct value, but how do I insert the value into the echo do_shortcode with url="$old_url"?
This doesn't work either:
<?php echo do_shortcode('[fbcomments][fbcomments url="echo $old_url;" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
You'll need to switch your quotes around. Single quotes print everything out as-is. Double-quotes will process the variables. Also, echo is not needed within an echo.
<?php echo do_shortcode("[fbcomments][fbcomments url='$old_url' width='375' count='off' num='3' countmsg='wonderful comments!']"); ?>
Another way to do it without switching your quotes is to break out of the statement:
<?php echo do_shortcode('[fbcomments][fbcomments url="'.$old_url.'" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Variables are not replaced in single quotes ...
<?php echo do_shortcode('[fbcomments][fbcomments url="' . $old_url . '" width="375" count="off" num="3" countmsg="wonderful comments!"]'); ?>
Singles quotes doesn't allow variables parsing.
For example :
$var = 'Hello';
echo 'The content of my var is : $var';
// Will output : "The content of my var is : $var"
echo "The content of my var is : $var";
// Will output : "The content of my var is : Hello"
So you have to use double quotes or use the concatenate operator : .

add html as option to a variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code :
$variable_name = bla bla bla;
I want to add to this variable this code :
<a class="screenshot" rel="<?php echo $baseurl.$this->row->image; ?>" >
<div class="photo"></div>
</a>
Can somebody PLEASE help?
Thank you
You assign it to your variable like any other string. However, since you are already in php mode, you have to remove <?php ?>, and use string contatenation operator . to include $baseurl.$this->row->image
$variable_name = '<a class="screenshot" rel="' . $baseurl.$this->row->image . '" >
<div class="photo"></div>
</a>';

Categories