I know this is a basic php question, but I'm having trouble showing variables inside an echo of HTML. Below is what I'm trying to achieve.
<?php
$variable = 'testing';
echo <span class="label-[variable]">[variable]</span>
?>
should output:
<span class="label-testing">testing</span>
<?php
$variable = 'testing';
echo <span class="label-[variable]">[variable]</span>
?>
Should be
<?php
$variable = 'testing';
echo '<span class="label-' . $variable .'">' . $variable . '</span>';
?>
If your HTML contains double quotes, then you can wrap it in single quotes:
echo '<span class="label-' . $variable . '">' . $variable . '</span>';
Additionally, you can do it inline by escaping the double quotes:
echo "<span class=\"label-$variable\">$variable</span>";
Using double quotes also allows for special characters such as \n and \t.
The documentation is your friend.
I know this is an old question, but what's wrong with something like this?
<?php
$variable = 'testing';
?>
<span class="label-<? echo variable ?>"><? echo variable ?></span>
One of the great features of php is the ability to be called and quit using php at any time during html.
Here is what I'd do:
<?php
// FIRST WRITE THE PHP PART
$variable = 'testing';
// THEN START USE HTML
// (call it when it's needed later)
?>
<span class="label-testing"><?php echo $variable; ?></span>
If you are saying that $variable is the name of the variable you wish to print, you want this:
echo "<span class=\"label-$variable\">${$variable}</span>";
The ${$variable} syntax tells PHP to print the variable named $variable. PHP calls these variable variables.
So for example:
$itemName = 'pencil';
$variable = 'itemName';
echo "<span class=\"label-$variable\">${$variable}</span>";
//Prints: <span class="label-itemName">pencil</span>"
It's TOO basic question to be asked here...
<?php
$variable = 'testing';
echo '<span class="label-[' . $variable . ']">[' . $variable . ']</span>;
?>
Something like this should work.
<?PHP
$variable = 'testing';
echo "<span class=\"label-$variable\">$variable</span>";
?>
The type of quotes on the echo are very important though. If you'd rather use single quotes on the echo, it'd be more like this.
<?PHP
$variable = 'testing';
echo '<span class="label-' . $variable . '">' . $variable . '</span>';
?>
This should allow you to echo back the variable.
<?php
$variable = 'testing';
echo '<span class="label-'.$variable.'">'.$variable.'</span>';
?>
This works too and is much sweeter:
<?php
$variable = 'testing';
echo "<span class='label-$variable'>$variable</span>";
?>
Order of double and single quote matters, double quotes on the outside. Be careful if you use this syntax, if the variable name is followed by normal characters things might go horribly wrong. Curly braces around variable names are required then.
Example:
<?php
$max_image_with = 1000;
echo "<div style='width:${max_image_width}px;'>";
?>
Documentation here:
http://php.net/manual/en/language.types.string.php#language.types.string.parsing
PHP Code... It's Working
<?php
$st_a = '<i class="fa fa-check-circle" ></i> Active';
?>
To Display in HTML
<h1> Sample Heading <?php echo $st_a; ?></h1>
Related
I have an array $category_slugs
how to output this using echo?
I have an error in code bellow:
echo "<div class='transition ".foreach($category_slugs as $slug){echo $slug;echo ' ';}."' data-category='transition'> " ?>
Thanks
You can't use foreach inside a echo. To achieve what you are trying you can use implode function, it concatenates the values of an array on a string.
echo "<div class='transition " .implode(' ', $category_slugs). "' data-category='transition'> " ?>
try this code
<div class="transition <?php foreach($category_slugs as $slug){echo $slug . ' ';} ?>" data-category="transition">
The concatenation operator ('.'), which returns the concatenation of its right and left arguments.
from php manual
But this code not a string.
foreach($category_slugs as $slug){echo $slug;echo ' ';}
So you will get a error.
This manual may help you Escaping from HTML.
Sorry for my bad english, I hope it will help you.
try this
$category_slugs =array('a','b','c');
foreach($category_slugs as $slug){
echo "<div class='transition ".$slug.' '."' data-category='transition'></div> " ;
}
Wrap <div> in a for loop, like so
<?php
$slug_string = "";
foreach($category_slugs as $slug){
$slug_string .= $slug_string." ";
}
echo "<div class='transition ".$slug_string."' data-category='transition'> ";
?>
I need to do something like this:
header("Content-Type: text/plain");
echo <<<EOT
<?php echo 'arbitrary code using ' . $variables . ' and such.';
echo 'finished';
?>
EOT;
The problem is, PHP still interprets the inline PHP as code and tries to execute it. I would like just to see the code printed in the window.
Use Nowdoc, notice the quotes around 'EOT':
echo <<<'EOT'
<?php echo 'arbitrary code using ' . $variables . ' and such.';
echo 'finished';
?>
EOT;
Or use a single quoted string, obviously escaping single quotes in the string:
echo '
<?php echo \'arbitrary code using \' . $variables . \' and such.\';
echo \'finished\';
?>';
You could use highlight_string function, it receives a string containing your php code and outputs html with the syntax highlight colors.
ex:
<?php highlight_string("<?php echo 'hi'; ?>");?>
You have also the function highlight_file, same thing, but receives a string with the file location.
Doc:
http://php.net/manual/en/function.highlight-string.php
http://php.net/manual/en/function.highlight-file.php
I would really need help with a primitive thing.
I need this:
<?php echo do_shortcode('[cycloneslider id="<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
echo $my_meta['odkaz']; ?>"]'); ?>
I do not know how exactly do it.
I made it by this solution:
<?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE);?>
<?php echo do_shortcode('[cycloneslider id="'.$my_meta['odkaz'].' "]'); ?>
Is it right or..Could you edit it to right way?
I suppose you don't see clear your own code.
If that happens, then adopt a more verbose way of programming and create variables that describe more the values:
<?php
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
$idCycloneSlider = $my_meta['odkaz'];
$doShortCodeResult = do_shortcode('[cycloneslider id="'.$idCycloneSlider.'"]');
echo $doShortCodeResult;
?>
That way we see clearly what happens in each line and the string concatenation is more simple to do.
Concatenate string and variables with the . operator:
$var = "Hello, I am made up of " . $some . $variables . " and strings";
You can write it using a string interpolation:
<?php
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
echo do_shortcode("[cycloneslider id=\"{$my_meta['odkaz']}\"]");
?>
Or using a heredoc if extra spaces are ok:
<?php
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
$shortcode = <<<TEXT
[cycloneslider id="{$my_meta['odkaz']}"]
TEXT;
echo do_shortcode($shortcode);
?>
I'm trying to do a simple task, insert a second function name under "onmouseover" but probably something is wrong with the syntax.
echo '<div onmouseover="changeText(); <?php if($title==""){echo changeViz();}; ?> ">';
I probably need to escape some quotes and add some, but i can't figure out the correct solution.
Thanks
Nothing it's working. Let me give you the complete code...:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); <?php if($title[$i]==""){echo changeViz();}; ?>">';
You are nesting <?php ?> inside existing php code, which is a syntax error. Instead, concatenate in the javascript function changeViz() as a quoted string.
This version uses a ternary operator to duplicate the if() statement you had originally.
echo '<div onmouseover="changeText(); ' . ($title == '' ? 'changeViz();' : '') . '">';
The ternary operation here will concatenate changeViz(); onto the echo string if $title == "", or otherwise just concatenate on an empty string.
Update after seeing full code:
You have the quote escaping correct in the first part.
echo '<div class="frame" onmouseover="changeText(\'' . $text[$i] . '\'); ' . ($title == '' ? 'changeViz();' : '') . '">';
You can make your code much more readable if you do not try to do everything in one line:
$onmouseover_action = "changeText(); ";
if($title==""){
$onmouseover_action .= "changeViz(); ";
}
echo '<div onmouseover="'.$onmouseover_action.'">';
It makes your code easier to maintain, and you have less need to comment it, because it describes itself quite much better.
Try this:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); '. ($title[$i]=="")?'changeViz();':'').'">';
<?php
$total=3;
echo '
<div class="idsdiv">.$total.<div> ';
?>
i want to appear $total variable number in the link.why is this script not working?
Enclose the whole string with double quotes to embed variables inside:
echo "<div class=\"idsdiv\">$total<div>";
You need to use double quotes around your HTML and single quotes around your attributes or do this...
echo '<div class="idsdiv">' . $total . '<div> ';
PHP doesn't process variable names in strings enclosed in single quotes.
<?php
$total=3;
echo '<div class="idsdiv">',$total,'<div>';
?>
Look at the string section of php.net (http://php.net/string) they talk about how to use each of the types. One of quote being the ' where nothing is parsed.
<?php $total=3;
echo "<div class=\"idsdiv\">$total<div> ";
?>
you had errors with your quotes
You can print HTML without printing it, like so:
<?php
$total = 3;
?>
<div class="idsdiv"><?php echo $total; ?></div>
When I still did PHP, I found it much easier to manage than escaping tons of quotes and things like that.
You can even do it inside of an if block too:
<?php
if ($foo == 'bar') {
?>
<div>Foo is bar</div>
<?php
}
?>
The method I like is
<?php
$total=3;
echo "<div class='idsdiv'><a href='profile.php?id={$total}'>{$total}</a><div>";
?>
It is my method, but there are plenty of ways to do it. Maybe even too many. If you want more information you can always refer to the documentation.
<?php
$total=3;
echo '<div class="idsdiv">'.$total.'<div>';
?>
You're quote are missing before and after the $total