nist
nist

Reputation: 1721

How to end a variable name in string without space

How can I end a variable name in a string without using space or any other special character?

Example is there anything I can put between $num and st to output 1st instead of 1 st

$num = 1;
echo "$num st";

Without using the dot opperator for concatination

Upvotes: 7

Views: 4944

Answers (2)

Quentin
Quentin

Reputation: 943108

Wrap the name of the variable with braces.

$num = 1;
echo "${num}st";

Or use printf syntax instead of simple interpolation:

$num = 1;
printf("%dst", $num);

Upvotes: 15

Jeroen
Jeroen

Reputation: 13257

Use the concatenating . character:

echo $num . 'st';

Upvotes: 0

Related Questions