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.
Related
This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 4 years ago.
How to define array in php like array1[] array2[] using for loop like instead of a1 or a2 I can write ai and its in for loop is it possible?
Like
for($i=0;$i<10;$i++)
{
$ai[$i]="hello'";
}
so I would like to say as like
a1[1]=Hello
You can access variables dynamically using this:
${"a$i"}[$i] = 'hello';
You could use a dynamic var
$ai.$i[$i] = 'Hello';
or
$ai.$i[$your_index] = 'Hello';
but be careful when doing this .. in this case it is difficult to keep in control of the variables .. is easy to make a mistake
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}");
This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
i have a problem.
I want in php to replace all 'blablabla' with 'bla'(because i hate blablabla). Here's my code:
<?php
$string = 'Dracula always says BlaBlaBla but says he never says BlaBlaBla';
$result = answer to replace here
?>
Thanks
Use srt_replace function to do that like this :
str_replace("BlaBlaBla","bla",$string);
You can use something like this:
str_replace("BlaBlaBla","bla",$string)
Also you can find it here in the docs: http://php.net/manual/en/function.str-replace.php
This question already has answers here:
json_decode to array
(12 answers)
Closed 8 years ago.
i have tried many ways to split the string though am not that good in this programing language
my string looks like this
{"success":true," URL ":"WWW.face-book.com", "rank":9, "details":null}
how do i separate my strings in such a way that the output looks like-
URL: WWW.face-book.com
rank: 9
You use json_decode($yourJsonStringHere); and youll get an Object of the class "stdClass" with public properties for all your keys.
So you can basically do the following:
$jsonObj=json_decode($jsonString);
echo $jsonObj->URL;
echo $jsonObj->rank;
echo $jsonObj->success;
echo $jsonObj->details;
See http://php.net/manual/en/function.json-decode.php for more info on the function itself.
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.