Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to end a variable name in string without space

Tags:

php

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

like image 758
nist Avatar asked Jun 10 '12 09:06

nist


People also ask

How do you not have a space between strings and variables in Python?

Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

Can string variables have spaces?

Naming rules. Variables can only contain letters, numbers, and underscores. Variable names can start with a letter or an underscore, but can not start with a number. Spaces are not allowed in variable names, so we use underscores instead of spaces.

Can variable names contain spaces?

Variable names cannot contain spaces. A # character in the first position of a variable name defines a scratch variable.

Can a variable name end with a digit?

A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables) There is no limit on the length of the variable name.


1 Answers

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);
like image 62
Quentin Avatar answered Nov 11 '22 13:11

Quentin