Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo text directly after variable

Tags:

php

echo

I have 2 variables named $min and $sec.

If $min = 5 and $sec = 15 I want to print this "5m 15s", echo "$min m $sec s";

But that gives "5 m 15 s". How do I get rid of the blank space?

like image 840
user1671375 Avatar asked Dec 02 '22 20:12

user1671375


2 Answers

You can use braces to do this, same as many shells:

$min = 7;
$sec = 2;
echo "${min}m ${sec}s";

The output of that is:

7m 2s

The braces serve to delineate the variable name from the following text in situations where the "greedy" nature of variables would cause problems.

So, while $minm tries to give you the contents of the minm variable, ${min}m will give you the contents of the min variable followed by the literal m.

like image 72
paxdiablo Avatar answered Dec 24 '22 04:12

paxdiablo


echo $min."m ".$sec."s"; is one way of doing it

like image 32
William N Avatar answered Dec 24 '22 03:12

William N