PHP code inside echo - php

I have following code which displays the price in Magento.
<?php echo $this->getPriceHtml($_product); ?>
I have to put this code inside echo instead and I can't get it to work.
I am trying
echo "$this->getPriceHtml ($_product)";
It just displays () on the page.
I tried other combinations and I can't come up with anything else.
What am I doing wrong?

Using single quotes will prevent $vars to be interpreted:
echo '$this->getPriceHtml ($_product)';
Or escape the $ sign:
echo "\$this->getPriceHtml (\$_product)";
http://php.net/manual/en/language.types.string.php
Or if by echo-ing you mean that you want to get something like
The price is 123.00
then do:
echo "The price is {$this->getPriceHtml($_product)}";
or even :
echo sprintf("The price is %s", $this->getPriceHtml($_product));

why not you are using this ?
$_product->getFinalPrice() or
if you want in formatted order then why you are not using this
number_format($pr->getPrice(), 2)
and if you want price with currency format then you can use this also
Mage::helper('core')->currency($pr->getPrice());
Hope it will help you.
:)

Related

WordPress: Echo Javascript for Google Ads Conversion Page

I'm trying to add some Javascript coming from Google for Ad Conversions on the thank you page. So this script should only appear on one page. Trying to use an if statement in PHP to echo the code but running into errors. This is what I currently have (with the Google ID replaced with something generic):
<?php if ( is_page( 'thankyou' )) {
echo '<script>';
echo 'gtag('event', 'conversion', {'send_to': 'AW-12345678901234567890123456'});'
echo '</script>';
?>
The first and third echo's are fine but the middle one is not. What might be the proper fix?
Your second echo line breaks your string.
You have 2 options to fix this:
1. Change the inner quotes to double quotes
echo 'gtag("event", "conversion", {"send_to": "AW-12345678901234567890123456"});';
2. Break out of the quotes.
You can use back slashes to not break out of your main string.
echo 'gtag(\'event\', \'conversion\', {\'send_to\': \'AW-12345678901234567890123456\'});';
I have also noticed on the second echo you were missing a semi colon.
Source: PHP-Strings
You need to escape the single quotes in the brackets of gtag().
Try echo 'gtag(\'event\', \'conversion\', {\'send_to\': \'AW-12345678901234567890123456\'});' instead.

How to print out the name of the variable and its value to browser?

I have just started learning PHP, so please forgive me for asking a question like this.
I have a variable $myVar = "hello"; and I want to print out the name of the variable and its value to the browser.
I tried
echo "$myVar: $myVar"; //this simple prints out the value of $myVar twice
also tried
echo '$myVar'+ ":$myVar"; //somehow the number 0 was printed out to the browser
also tried
echo '$myVar: $myVar'; //this just prints out the string literal itself
I understand the difference between double quotes and single quotes, but just can't seem to get this simple example working.
You can simply do this:
echo "\$myVar: $myVar";
Use backslash \ to display the variable name as it is.

How do i multiply variables under echo quotations

I'm using echo to print a database info using html tags. So the entire statement is under quotation marks " ".
The user is buying some things so i have their price (for example in $price variable, really doesn't matter) and their number (in $number variable) and i'm trying to print out the total price of the bought item. It just prints out $price*$number expectedly, but i'm not sure what to try to get it to work the way i want it to.
I'm sure its rather simple i just can't think of a solution right now and its really bugging me :D
You should use string concatenation:
echo "text text " . ($number * $price) . " text text"

How do I mix php inside php strings?

I'm trying to mix <?php echo do_shortcode('[...]') with a field from Advanced Custom Fields within Wordpress.
So basically what I'm trying to do is give the user a text field in the page edit screen where she can paste in the ID of a youtube vide. This field will then update my do_shortcode to display the correct video.
I'm not sure what I'm doing wrong considering I've done this several times before and been succesful. I do have a feeling I'm not escaping the string correctly?
<?php echo do_shortcode('[video_lightbox_youtube video_id="' . the_field("youtube_video") . '" width="640" height="480" anchor="Play Video"]'); ?>
Anyone able to lead me in the right direction? :)
EDIT
The code above returns q_cHD1lcEOo with multiple spaces in front of it as well as this: "Error! You must specify a value for the Video ID, Width, Height parameters to use this shortcode!" That's why I was thinking I'm not escaping it correctly as these are all specified.
I'll add that if I remove the_field("...") and replace it with just an ID it displays perfectly.
SECOND EDIT
Since I was not supposed to echo it, I was using the wrong function to get the field. Instead of using the_field() which prints the value, I was supposed to use get_field() to simply return it to the string.
Your question is somewhat unclear, but I'm also 20 hours without sleep.
Anyways, as far as mixing PHP within a PHP string, there's numerous ways to do it..
You can use concatenation or { } within the string itself.
For example, say we want to echo out the property of an object within a string.
We could do the following
echo "This is my property " . $object->property;
Or, we can do this
echo "This is my property {$object->property}";
You can even do cool things like access associative arrays within strings like so
echo "This is my property {$object->property['cool']}";
Hopefully this leads you in the ride direction.
At first glance it looks like you should be using get_field instead of the_field. the_field will print without being prompted, whereas get_field will return its value, which is what you want.
I see you've also mentioned whitespace at the start, you should consider wrapping the function in trim.
See below:
<?php echo do_shortcode('[video_lightbox_youtube video_id="' . trim(get_field("youtube_video")) . '" width="640" height="480" anchor="Play Video"]'); ?>

how to convert an int/value into a curreny/price?

im using drupal,
I have a field, where users specify a price.
Ive printed the price field in my tpl.
<?php print $node->my_form['amt'] ?>
but the price comes out like 0.050, How do I get this to display £0.50?
See money_format() or see sprintf() or the printf variant:
<?php printf('£%01.2f', $node->my_form['amt']) ?>
Output: £0.05
Demo
See money_format().
you might want to check the details here http://www.drupalcommerce.org/faq/currency-format and make sure you have a currency formatter configured/chosen.
Depending also on how you're geting your prices, you might want to see this issue http://www.drupalcommerce.org/node/633 with the solution.

Categories