PHP and printing out whitespace? - php

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

Related

Adding functions to single quote

Help me
How can I insert a function inside a single quote?
<php
echo ' Hello world <?php function(); ?> ';
what's the correct syntax ?
If you have to use single quotes then you would concatenate (.) the function:
echo ' Hello world ' . function();
You have to cancatenate it.
echo 'Hello '. yourFunction() . 'world !';

Show HTML and PHP code in one line

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

Insert a space between 2 echo

How do I add a space in the returned text between sample1 and sample2? Here is what I have so far:
if ($eventid!="") {
echo $get_event['sample1'], $get_event['sample2'];
}
It's really simple, just echo a space between the variables...
<?php if($eventid!=""){echo $get_event['sample1'] , ' ', $get_event['sample2']; }
Indentation always make things cleaner and easier:
if (!empty($eventid)) {
echo $get_event['sample1'] . ' ' . $get_event['sample2'];
}
On PHP, you need to use a dot (.) to concatenate strings... as you can see on the Strings Operators documentation:
http://php.net/manual/en/language.operators.string.php

PHP echo does not work

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}";

Echoing double "

How can i echo " i mean if i want to show: "Patrik", i would do:
<?php echo " "Patrik" "; ?>
but as you know you cant do this
Escape double quotes with \ when full string itself is in double quotes:
<?php echo " \"Patrik\" "; ?>
Or:
<?php echo ' "Patrik" '; ?>
More Info:
PHP: Double quotes vs Single quotes
http://pt.php.net/manual/en/language.types.string.php
<?php echo '"Patrik"'; ?> or.. <?php echo "\"Patrik\""; ?>
<?
echo " \"Patrik\" ";
// or
echo '"Patrick"';
?>
if it's HTML or XML, you can use " as substitute for the double-quote.

Categories