Variable in php echo - 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);
?>

Related

how to echo array using foreach in echo

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'> ";
?>

PHP: How can I put HTML Code with PHP Code within into a PHP Variable?

I have a question about " and ' in PHP. I have to put a complete <li> element into a PHP variable but this doesn't work, the output is completely false...
$list =
"
<li class=\"<?php if($info[1] < 700) {echo \"half\";} else {echo \"full\";} ?>\">
<div class=\"onet-display\">
<?php if ($image = $post->get('image.src')): ?>
<a class=\"onet-display-block\" href=\"<?= $view->url('#blog/id', ['id' => $post->id]) ?>\"><img class=\"onet-thumb\" src=\"<?= $image ?>\" alt=\"<?= $post->get('image.alt') ?>\"></a>
<?php endif ?>
<h1 class=\"onet-thumb-title\"><?= $post->title ?></h1>
<div class=\"uk-margin\"><?= $post->excerpt ?: $post->content ?></div>
</div>
</li>
";
Is it because there is PHP Content in the HTML Code? How can I solve this?
Can someone help me and explain why this doesn't work?
<?php ... <?php
Since your string contains PHP tags, I suppose you expect them to be evaluated. The opening PHP tag within another PHP tag is interpreted as a part of the PHP code. For example, the following outputs <?php echo time();:
<?php echo "<?php echo time();";
There are several ways to build a PHP string from PHP expressions.
Concatenation
You can create functions returning strings and concatenate the calls to them with other strings:
function some_function() {
return time();
}
$html = "<li " . some_function() . ">";
or use sprintf:
$html = sprintf('<li %s>', some_function());
eval
Another way is to use eval, but I wouldn't recommend it as it allows execution of arbitrary PHP code and may cause unexpected behavior.
Output Buffering
If you are running PHP as a template engine, you can use the output control functions, e.g.:
<?php ob_start(); ?>
<li data-time="<?= time() ?>"> ...</li>
<?php
$html = ob_get_contents();
ob_end_clean();
echo $html;
Output
<li data-time="1483433793"> ...</li>
Here Document Syntax
If, however, the string is supposed to be assigned as is, use the here document syntax:
$html = <<<'HTML'
<li data-time="{$variable_will_NOT_be_parsed}">...</li>
HTML;
or
$html = <<<HTML
<li data-time="{$variable_WILL_be_parsed}">...</li>
HTML;
You want to store some html into a variable.
Your source should (if not yet) start with
<?php
Then you start building the contents of $list.
Starting from your code the nearest fix is to build $list by appending strings:
<?php
$list = "<li class=";
if($info[1] < 700)
{
$list .= "\"half\""; // RESULT: <li class="half"
}
else
{
$list .= "\"full\""; // RESULT: <li class="full"
}
// ...and so on...
Now a couple things to note:
$list .= "... is a short form of $list = $list . "...
Where the . dot operator joins two strings.
Second thing you may make code easier to read by mixing single and double quotes:
Use double quotes in PHP and single quotes in the generated HTML:
<?php
$list = "<li class=";
if($info[1] < 700)
{
$list .= "'half'"; // RESULT: <li class='half'
}
else
{
$list .= "'full'"; // RESULT: <li class='full'
}
// ...and so on...
This way you don't need to escape every double quote
i think you have to work like this
$test = "PHP Text";
$list = "<strong>here ist Html</strong>" . $test . "<br />";
echo $list;

How to echo lots of HTML between an IF statement

At the moment my PHP code is like this:
<?php
some code
?>
lots of HTML
<?php
some more code
?>
I now want to include large chunks of HTML depending upon the values of certain PHP variables so like this:
<?php
if ($requiresSignature===true) {
echo "some HTML";
echo "some more HTML";
}
?>
Using echo is fine for a few lines of HTML but is there an easier way when I've got maybe 500 lines of HTML so I don't have to type echo in front of each line?
You can do it this way
<?php
if ($requiresSignature===true) {
?>
<b>some HTML</b>
<b>some more HTML</b>
<?php
}
?>
For this usage, the heredoc or nowdoc functionalities of php are the best options, in my humble opinion.
Heredoc
Heredoc is like echo "Foo bar"; but intended for a large chunk of text, spanning multiple lines.
Like this:
echo <<<FOO
<h1>Foo bar</h1>
<p>Lorem ipsum dolor sit tenet conseqteur...</p>
<i>Created by $name</i>
FOO;
This syntax is also available for setting variables, class properties, class constants and static variables (since php 5.3). The FOO part, you can set yourself. Just remember to close the Heredoc with the same ending on a line by itself (with absolutely no indentation), ended with a semicolon.
E.g.
$foo = <<<BAR
This is an example text.
Spanning multiple lines.
BAR;
Nowdoc
Think of Nowdoc as the ' equivalent of ". That is, no variable substitution is performed inside a Nowdoc statement, just like none is performed inside a 'single quoted string'.
The syntax is like this:
echo <<<'EXAMPLE'
This
is
a
test
EXAMPLE;
In conclusion I would do like this:
if ($requiresSignature===true) {
echo <<<HTML
Some html<br/>
And even more <b class="html">html</b>
HTML;
}
echo '
some html
some html
some html
';
I think that's what you're looking for
Try this
<?php
if (true){
?>
pure html code here
<?php
else{
?>
pure html code here
Try this
<?php
$Html = '';
if ($requiresSignature===true) {
$Html .="some HTML";
$Html .="some more HTML";
echo $Html;
}
?>
You can use heredoc syntax: See more detail in here
echo <<<"MYHTML"
html
lots of html
MYHTML;
this work for you
<?php if( var == true ): ?>
<p>Your HTML</p>
<?php endif; ?>
It's useful, try this code:
<?php
if ($requiresSignature===true) {
$var = "some HTML";
$var .= "some more HTML";
echo $var;
}
?>
**OR**
<?php if ($requiresSignature===true) { ?>
HTML CODE
<?php } ?>
Try this method,
<?php
$Html = "";
if ($requiresSignature===true) {
$Html .="some HTML";
$Html .="some more HTML";
echo $Html;
}
?>
You Don' Need to write echo to each line.
You Should use :
echo '
some html
some more html
some more html
';
PHP is a HTML embed language.You can try it every where of your php page.
<?php if ($requiresSignature===true): ?>
<p>"some more HTML"</p>
<?php endif;?>
...but is there an easier way when I've got maybe 500 lines of HTML so I don't have to type echo in front of each line? It turns out there is, so yes and here is how:
<?php
$myString = "PHP is so cool...";
if($requiredSignature === true):
?>
<div class='too-much-html-markup'>
All the RAW HTML MARKUP here would only be displayed if (and only if)
the condition above evaluates to true so i don't have to worry about
any kind of echoing. However, i can still go ahead and echo some
content here if i still choose like this: <?php echo $myString; ?>
And everything will still work out just fine.
</div>
<?php else: ?>
<div class='still-some-raw-html-in-else-clause'>
Again this is a raw Markup and will only be rendered if the IF
condition above evaluates to FALSE!
</div>
<?php endif; //<== NOW I END THE CONDITIONAL LOGIC WITH endif KEYWORD ?>
variables output in the plain HTML, consider doing so like this:
<?= $variable ?>
or
<?php echo $variable; ?>

PHP echo variable and html together

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>

Where is the problem in this PHP code?

There is a problem in this code I can not detected
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
Do you find you have a solution???
Thank you very much.
My guess is that your problem is that it isn't writing out the data in $rows['Link'] ... if that is the case, then your solution is to change it to {$rows['Link']} ... actually, you'll probably want to change both, since it looks like you started doing string concatenation and then switched halfway through.
So:
<?php echo "<a href ='$rows['Link']'> .$rows['UploadName']</a> "; ?>
becomes:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a> "; ?>
See: The PHP Manual on Variable Parsing in strings
It should be:
<?php echo "<a href ='{$rows['Link']}'>{$rows['UploadName']}</a>"; ?>
Or:
<?php echo "<a href ='{$rows['Link']}'>" . $rows['UploadName'] . "</a>"; ?>
There's a problem in parsing variables in the string. Use curl braces:
<?php echo "<a href ='{$rows['Link']}'> .{$rows['UploadName']}</a> "; ?>
Take a look to this php.net page, under "variable parsing".
More alternatives:
<?php echo '' . $rows['UploadName'] . ''; ?>
or
<?=('' . $rows['UploadName'] . '')?>
Another alternative (that I tend to prefer, given I know that both 'Link' and 'UploadName' are valid indices of $row.
<?=$rows['UploadName']?>
I'm not sure what that does for readability for most people, but on color-coded IDEs, it tends to help, because the HTML isn't just seen as one giant ugly single-colored string.

Categories