I want to show this sentence:
?>
<h2>Date <?php echo "$day"."/"."$month"."/"."$year"."<br>"; ?></h2>
<?php
I have tried:
echo '<h2>Date '$day'.'/'.'$month'.'/'.'$year'.'<br>' </h2>';
echo '<h2>Date <?php echo \"$day/\".\"/\".\"$month\".\"/\".\"$year\".\"<br>\"; ?></h2>';
Without success.
If a line contains both HTML and PHP code, what is the proper format to display it?
You have a "dot mess".
You can concatinate PHP variables and strings like
<?pho echo 'string' . $var . 'string' . $var1 . $var2; // and so on ?>
So correct solutions would be
<?php
echo '<h2>Date ' . $day . '/'. $month .'/' . $year .'<br></h2>';
echo '<h2>Date ' , $day , '/', $month ,'/', $year ,'<br></h2>';
echo "<h2>Date $day/$month/$year<br></h2>";
echo "<h2>Date {$day}/{$month}/{$year}<br></h2>";
?>
You can even use commas , for echoing strings and variables that's a bit faster. Cause PHP does not concatinate all parts together to a single string and echos it but echos every part separatly (so less operations are needed).
Note that if you put a string in double quotes " you can use $var or {$var} inside the string cause PHP is looking vor PHP variables inside a double quoted string and replaces them by their containing value.
But the fastest solution is the , separated version.
or
As lonesomeday mentioned there is also a C like solution possible in PHP
<?php printf('<h2>Date %s/%s/%s<br></h2>', $day, $month, $year); ?>
or (but not recommended by me)
The complicated and none readable version
<h2>Date <?php echo $day; ?> / <?php echo $month; ?> / <?php echo $year; ?><br></h2>
And the shorted version of the above code (just for completeness, short tags have to be enabled in PHP versions lower than 5.4 to work)
<h2>Date <?= $day; ?> / <?= $month; ?> / <?= $year; ?><br></h2>
For even more information about string concatination read the offical PHP doc site about String Operations.
The nicest way to do this is probably with printf, which substitutes variables into your string according to placeholders:
printf('<h2>Date %s/%s/%s<br></h2>', $day, $month, $year);
?>
<h2>Date <?php echo $day."/".$month."/".$year."<br>"; ?></h2>
<?php
You don't need quotes when using variables.
Try
echo '<h2>Date ' . $day . '/'. $month .'/' . $year .'</h2>';
Try this...
This will display the code as it is in your browser
<?php
echo htmlspecialchars('<h2>Date <?php echo \"$day/\".\"/\".\"$month\".\"/\".\"$year\".\"<br>\"; ?></h2>');
?>
O/P:
<h2>Date <?php echo \"$day/\".\"/\".\"$month\".\"/\".\"$year\".\"<br>\"; ?></h2>
Can you try this,
echo "<h2>Date $day / $month / $year <br></h2>";
I think this is your answer
<h2>Date <?php echo "\$day"."/"."\$month"."/"."\$year"."<br>"; ?></h2>
or
<?php echo '<h2>Date '.$day.'/'.$month.'/'.$year.'<br></h2>'; ?>
try this
echo '<h2>Date '$day'.'/'.'$month'.'/'.'$year'.'<br>' </h2>';
echo '<h2>Date <?php echo \"$day\"."/".\"$month\"."/".\"$year\".\"<br>\"; ?></h2>';
Related
I have problem with comma. I have a code
<?php echo get_the_subtitle($post); ?>, <?php echo get_the_date(); ?>
and i need show comma ',' only when
<?php echo get_the_subtitle($post); ?>
have a subtiitle.
Example: subtitle, date. If no have a subtitle not show coma. I do something like this <?php echo get_the_subtitle($post, ','); ?> but comma is before subtitle not aftre ;/
The documentation states that the second parameter of this function is the text before and the third parameter is the text after the subtitle.
So you should do something like this:
<?php echo get_the_subtitle($post, '', ','); ?>
<?php
$subtitle = get_the_subtitle($post);
echo $subtitle ? $subtitle . ', ' : '';
echo get_the_date();
?>
You need check get_the_subtitle($post) function returns something or not. If it returns something you should echo it with comma or it does not return something you should echo only date.
You could do this:
echo (get_the_subtitle($post) != '') ? get_the_subtitle($post) .','. get_the_date() : get_the_date();
If the subtitle isn't blank, echo the subtitle and the date (with the comma), else just echo the date.
Currently i'm trying to send a time and a date from php to my angular controller by using a ng-click.
$time is a date
$today is a datetime
Can someone explain me why this works
<button ng-click="getClickInfo('<?php echo $time; ?>', '<?php echo $today; ?>')";</button>
But when i try to achieve the same result within an echo like this, it gives me different and incorrect output
<?php echo '<button ng-click="getClickInfo(' . $time . ', ' . $today . ')";></button>'; ?>
I tried to search on the internet for a solution but i couldn't really find a topic about it. Hope someone can explain me what is happening in this scenario. Redirection to any articles on this topic would be really helpfull for me.
Output:
01:00 // incorrect output
01/01/1970 // incorrect output
20.30 // desired output
22-04-2016 // desired output
You have to quote the javascript function arguments, otherwise you get unpredictable results
The ctach here is that the HTML ng-click attribute must be enclosed in double quotes and the attribute value (your function) must not contain double quotes, because it would break the HTML
Furthermore, the ; is not needed, you're putting it outside the ng-click HTML attribute value, that is not valid HTML
Here's a fix, note that quotes are escaped with backslash inside PHP strings:
<?php echo '<button ng-click="getClickInfo(\'' . $time . '\', \'' . $today . '\')"></button>' ?>
Here's a more readable way to do it, I would recommend this approach:
<?php echo '<button ng-click="getClickInfo(' . "'$time', '$today'" . ')";></button>' ?>
I wrote a short test code in PHP7:
<?php
$str1=' bigapple ';
echo strlen($str1);
trim($str1) ;//or trim($str1," ")
echo strlen($str1);
?>
But whenever I use trim on $str1 ,the strlen would be return 10.
Can someone tell me the reason why? I've been searching it but find nothing.
You need to store trim string to any variable or you need to print trim string as following:
echo strlen(trim($str1));
You have not assigned the trimmed value in another variable. Try as below :
<?php
$str1=' bigapple ';
echo strlen($str1).'<br>';
$str2 = trim($str1);
echo strlen($str2).'<br>';
?>
I am trying to print a variable between curly braces as
Product_number{product_version}
I tried
echo "$product_number{$product_version}";
But that does not work. I don't understand why :(
try using double braces:
echo "$product_number{{$product_version}}";
You can also do:
echo "$product_number{".$product_version."}";
{ followed by $ is treated specially. It is mainly used when you want to append a string immediately at the end of a variable's value:
$v = 'hack';
echo "I {$v}ed it";
echo $product_number . "{" . $product_version . "}";
Escape the "{":
echo "$product_number\{$product_version}";
I was wondering how can I print out whitespace in the following code below.
<?php echo "$first_name", "$last_name" ; ?>
Just, well, add whitespace to your output?
<?php echo "$first_name $last_name with some whitespace" ; ?>
Sometimes, it the obvious answer is right - even in PHP!
If you want to concatenate several strings without the double quotes (e.g. when using single quotes):
$string = $first_name . ' ' . $last_name; // The dot is a concatenation operator
<?php
echo "$first_name $last_name";
?>
Notice that when you use ' any var aren't display, with " does.
So, good is:
<?php echo "$first_name $last_name" ; ?>
bad
<?php echo '$first_name $last_name' ; ?>
If you want to keep using commas:
<?php echo $first_name, ' ', $last_name; ?>