This question already has answers here:
PHP Constants Containing Arrays?
(16 answers)
Closed 5 years ago.
Is there a possibility to make a group of constants and get them exactly like an array instead of writing each constant independently ?
something like
echo MYCONST[0]; or
echo MYCONST['name'];
What you are looking for is called Constant array :
it is now possible to do this with define() but only for PHP7
. However you can do this on PHP5.6 by using the const keyword and it is not possible to perform this in lower PHP versions
here is an example :
<?php
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[1]; // outputs "cat"
define('MYCONST', [
'key' => "value"
]);
echo MYCONST['key']; // outputs "value"
1. If you are using PHP7 , you can define a constant as an array.
define('MYCONST', array("someValue1","someValue2"));
2. Version below PHP7 , you can store a string as a constant, whether it can be JSON string or serialized string.
define('MYCONST', json_encode(array("someValue1","someValue2")));
define('MYCONST', serialize(array("someValue1","someValue2")));
For accessing the constant in version below PHP7 (If it is JSON or serialized string), you must json_decode or unserialize respectively.
Related
This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page
This question already has an answer here:
How can I access an object property? [closed]
(1 answer)
Closed 7 years ago.
I have a stdclass object as below in PHP :-
$sample = (object) array(
"sname" => "test"
,"bselection" => "12345"
,"bind" => "1"
);
I need the output as below -
test123451
Please advise how I can I get the output as above.
Since you casted it as an object upon declaration, just access it like any other normal object, thru -> arrow operator:
echo "{$sample->sname}{$sample->bselection}{$sample->bind}";
Several versions will work as well using . or ,:
echo $sample->sname,$sample->bselection,$sample->bind;
echo $sample->sname.$sample->bselection.$sample->bind;
Try this.
You can simple access an object using -> sign and dot . sign to concatenate it.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to declare an array as constant
Is it possible to use an array as a class constant in PHP?
I.e
const MYARRAY = array('123', '234');
If not why?
No, you can't.
But you could declare it as a static property.
public static $MYARRAY = array('123', '234');
---------------Update-----------------------------
Array const is available from PHP 5.6.
php.net/manual/en/migration56.new-features.php
UPDATE:
This is now available in PHP 5.6 https://php.net/manual/en/migration56.new-features.php
No you can't assign an Array to PHP constant.
In http://www.php.net/manual/en/language.constants.syntax.php
Constants may only evaluate to scalar values
This is the reason.
Scalar values for examples are int, float, string
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does “=>” mean in PHP?
Reference - What does this symbol mean in PHP?
I see this symbol in a lot of PHP code.
I can't figure out what it means or what it does..
Is it really an operator ?
Do you mean =>? If so, it's for initializing array keys (or indexes if you prefer). Like this:
$values = array(
'foo' => 'bar'
);
This will initiazlie an array with a key named foo with a value of bar.
Read more on about arrays at php.net.
http://php.net/manual/en/language.operators.php
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does “=>” mean in PHP?
I'm learning about arrays and am not certain what => means or does.
Assignment of a value to a named key.
http://www.php.net/manual/en/language.operators.assignment.php
$example = array('color' => 'blue');
echo $example['color']; //prints blue
I believe it is just syntax for writing array literals that behave like maps / dictionaries.
$mydict = array('hello' => 'world');
The above is an array with one element. But instead if indexing that element with an integer index, you use the key 'hello':
echo $mydict['hello']