Meaning of "<<<HTML" in code example [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
I am trying to include some javascript for the google analytics ecommerce tracking.
In the example code I see something like this, in which I have replaced the variables with my own.
<?php
function getTransactionJs(&$order) {
return <<<HTML
ga('ecommerce:addTransaction', {
'id': '{$order->ord_order_numner}',
'affiliation': 'Marcella',
'revenue': '{$order->total_payment}',
'shipping': '0',
'tax': '0'
});
HTML;
}
echo getTransactionJs($order);
?>
However I met with a syntax error. May I ask what is the meaning of
return <<<HTML
Thanks in advance!

It's a way to define a string on multiple lines. Your string begin juste after the <<<HTML and ends at HTML; The HTML word can be replaced by whatever you want.
See Heredoc syntax.

It’s PHP’s Heredoc syntax. I’m not a fan of it though.

As suggested read the manual, but consider not to use this kind of syntax.
To make your life easier - in most cases the problem is with text indenting.
HTML;
Must be at the start of the line not indented!

Related

I do not understand this PHP Code, what is it doing? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
How to read php
i'm learning php, I wrote this. (It is from a book)
<?php if(!empty($data)): ?>
<ul>
<?php foreach ($data as $dataprint): ?>
<li><?= $dataprint ?></li>
<?php endforeach ?>
</ul>
I am unable to understand some of the code in this section.
1. Why are there colons on line 1 and 3?
2. What does the '<?= $dataprint ?> ) and why does it not have the standard 'PHP' word?
3. why is there an equal mark in the next?
colons - this is a shorthand version of statements, you should not really learn them at such an early stage. First you need to learn the full versions.
<?= $variable ?> is a short version of <?php echo $variable ?> but means exactly the same.
Also, the shordhand versions from question one are actually not considered a good practice, because when you nest them they are hard to read.
Colons on lines 1 and 3 are an alternative way of doing the below code
if (!empty($data)) {
...
}
It makes it a lot cleaner when outputting HTML.
(See http://php.net/manual/en/control-structures.alternative-syntax.php)
<?= is a shorthand way of writing <?php echo, again it's just an alternative way of doing things.
(See http://php.net/manual/en/function.echo.php)
You can write loops in php many type...
1)eg: if(condition){...code...}else{...code...} or if(condition): means, in place of "{" you can use ":" and for ending the loop use "endif"
2) Your point no 2 is written in shortcode in php for that you have to enabled shortcode in php.ini

Please Explain meaning of {} syntax for me [duplicate]

This question already has answers here:
Whats the difference between {$var} and $var?
(5 answers)
Closed 5 years ago.
I am a newbie and try to learn PHP especially INSERT INTO statement. I'm know that if we insert value for String we must use '' syntax. But i don't understand meaning of {} syntax for "title" and"link". Any guys can explain for me. Thanks a lot.
On Sql side there is no difference between '$title' or '{$title}' . but in php {$title} is a more complete syntax of $title, that allows one to use :
"this is post {$title}s"
"{$object->data}"
"{$array['data']}"
"{$array['data']->obj->plop['test']}"
The curly braces "escape" the PHP variable and are not passed to MySQL. With a simple variable like $title it doesn't make a difference but with something like $post['title'] it does. for more information check this

Why do I always get the error "Too few arguments" whenever I try to insert contents into my template using sprintf in Php? [duplicate]

This question already has answers here:
[PHP]: Error -> Too few arguments in sprintf();
(2 answers)
Closed 6 years ago.
I have a language.inc file which has a email template that looks like this:
$lang["email_templete"] = '
<style type="text/css">
*....more stylings here...*
</style>
<div>
<table>
*....more elements....*
%s
</table>
</div>
';
My problem is that whenever I'm trying to insert contents into my template using sprintf, it always hits the error "Too few arguments". I've tried removing all the excessive html codes and replaced it with a more simple html code and it works. Do you have any clue on this problem?
function add_to_email_template($pTargetEmail, $pSubject, $pContent) {
global $lang;
$vMessage = sprintf($lang["email_templete"], $pContent);
send_email($pTargetEmail, $pSubject, $vMessage);
}
Read the manual page for sprintf() and you'll see that the % sign is a format modifier of which your HTML and CSS code is filled with.
If you want to include % inside the string without getting it parsed, you need to escape it with itself such as %% will output %.
Note that it would probably be much simpler to user variables in your string definition than to use sprintf() the way you do, such as:
$variable = 'abc';
$myHTML = "Here is my variable: $variable";

PHP VSW <<< (Weird Syntax with Three carets) [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I've stumbled across some code that looks like it's alternate syntax for assigning something into a variable in PHP.
$VSWTags = <<<VSW
BUNCHOFHTMLANDSTUFFHERE
VSW;
This is in a Drupal site we have here at work, and I searched on PHP.net for the VSW and for the <<< I couldn't find anything. When I played with it in the console I saw that it basically is just support for bare words or something of that nature?
php > $s = <<<VSW <<< > I dont know what this is doing
<<< > but its cool
<<< > VSW;
php > print $s;
I dont know what this is doing
but its cool
php >
Does anyone know where the documentation is for this or what it's called?
This is HEREDOC - very usefull for large strings.

How to use strings with variables in codeigniter's language files? [duplicate]

This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
for a localized website I want to create different language files.
But my main problem before starting the localiuation is, that i probably have strings with variables.
My theory is that i can use placeholders within my language files like:
$lang['somekey'] = "Hello Mr. %s, how are you?";
Is there a clean and nice way to parse those variables or do i have to develop a function for that?
Thanks.
I have the same problem and do it simply by using,
echo sprintf($this->lang->line('somekey'),'XYZ');//load language library before use
Read sprintf()
you can use codeigniter i18n with PHP .sprintf() to achieve what you want. load up the codeigniter non-variable strings (with those format stuff), then pass it on to .sprintf() for formatting and assignment of values. it should replace the %s part.
it's similar to this question. .sprintf() works like .printf(), only that it returns the string rather than printing it.

Categories