How to Concatenate String and Variable with DEFINE in PHP [duplicate] - php

This question already has an answer here:
PHP: Constant name from a variable
(1 answer)
Closed 4 years ago.
I have defined a Constant viz
define('M_3', 'March');
if I
echo M_3;
The output is fine i.e. March.
But if do like this
$m3 = 3;
echo M_.$m3;
The output is M_3
Kindly let know where I am mistaken.

In order to call a constant with a variable you can use this:
echo constant("M_{$m3}");

Related

How to declare string variable? [duplicate]

This question already has answers here:
What is the proper way to declare variables in php?
(5 answers)
Closed 9 months ago.
The first appearance to this variable $totpro in my code is this way
$totpro = $totpro + $row['profitloss'];
I want to use it to sum all profits, however, I receive this warning message on running
Warning: Undefined variable $totpro
but if I put this code before the previous code it runs with no problems
$totpro = "0";
I don't like using that code to declare the function, it tried
String $totpro
but unexpectedly it didn't work. Now tell me how to define $totpro without to have to use $totpro = "0";
If you are summing numbers, the initial declaration should set the value to 0 (i.e. a number):
$totpro = 0;
You tried "0", which is a string. Technically this will work, but it is not the best way.

Update a URL using a variable - PHP [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 4 years ago.
I would like to assign a variable into the PHP code, which will change my URL. For example
$page = 'http://www.example.com/search-products?type=buildings&q=small&go=Go';
Where q=small i would like to change to say q=big (using a variable)
I have assigned a variable within PHP but i am unable to get it to work?
for example
$q= 'big';
$page = 'http://www.example.com/search-products?type=buildings&q=$q&go=Go';
The url does not however update - Any help would be appreciated
Use strings with " and not with ' if you're using variables in it.
$page = "http://www.example.com/search-products?type=buildings&q={$parameter}&go=Go";
Check this :
$page = "http://www.example.com/search-products?type=buildings&q={$q}&go=Go";
Note: Single quotes don't work in this case.
If you use Single quotes, you see something like this :
echo 'q={$q}';
//Output => q={$q}

How can I use a pre defined CONSTANT inside an array? [duplicate]

This question already has answers here:
Can I use string concatenation to define a class CONST in PHP?
(5 answers)
Closed 5 years ago.
I want the code to look like this:
define("CONSTANT","World !!");
$array=array(" Hello CONSTANT");
echo $array[0];
And the output to look like this:
Hello World !!
How can I do this?
To put a constant inside an array, simple put it without the quotes:
$array=array(" Hello ".CONSTANT);
Read here for more informations: http://php.net/manual/en/language.constants.php.

php variable from variable [duplicate]

This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 9 years ago.
I have a variable and I want to create a variable with that. I get the variable from database and put it together with some text and then I want another variable.
For exampel
$a = $ . "txt" . $d;
Try with this. It will create a variable from another one.
$a = ${'txt'.$d}
P.s. This is a question asked a couple of times. You might have found the answer simply by searching the issue on google.

How could I construct a variable name from another variable? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Construct a PHP variable name based on other variable values and static text
$value = '200';
$_200 = 'other';
How could I echo the contents of the second variable, getting its name from the first variable? So basically read the value of $value, prepend an _ and use it as a variable name.
Same as always.
echo ${'_'.$value};

Categories