properties as array [duplicate] - php

This question already has answers here:
PHP syntax for dereferencing function result
(22 answers)
Closed 9 years ago.
I want to do something like this without using extra variables:
class className {
public static function func(){
return array('true','val2');
}
}
if(className::func()[0]) {
echo 'doğru';
} else {
echo 'Yanlış';
}

className::func()[0] is called array dereferencing, and is not valid syntax in all PHP versions. It will be is available starting in PHP 5.4, currently in beta, released March 2012. For earlier PHP version, you will need to use an extra variable somewhere to store the array returned from className::func().
See the PHP 5.4 Array documentation for implementation details.

Array Deferencing is not currently available in PHP. It is on the table for PHP 5.4.
Until then, you would need the extra variable:
$arr = className::func();
if($arr[0]){
echo 'doğru';
}else{
echo 'Yanlış';
}

Well,you can return an object in your method instead.
something like:
class className{
public static function func(){
return (object)array('true'=>'true','val2'=>'val2');
}
}
echo className::func()->true;//Extra variables go away =)

As the others noted, you currently cannot do it this way. If you really cannot use an temporary variable (although I don't see a reason not to use one) you could use
if(current(className::func())) // or next() or reset()
But make sure you read the documentation about these functions to treat empty arrays properly.
Reference: current

Related

Is it good practice to alter the $GLOBALS array of PHP directly? [duplicate]

This question already has answers here:
Stop using `global` in PHP
(6 answers)
Closed 7 years ago.
The $GLOBALS array of PHP provides access to all global variables, like
<?php
$foo = 'hello';
function myFunc() {
echo $GLOBALS['foo']; // prints "hello"
}
?>
Now I have to work on some code from other people that directly adds elements to the array to have it available globally (without having an according variable), like so:
<?php
function doSomething() {
// $newData is NOT existing anywhere!
$GLOBALS['newData'] = 'Hello World!';
// now $GLOBALS['newData'] is available anywhere else w/o actual variable
}
?>
Until now I never saw this specific usage of the $GLOBALS array and was wondering if it is considered "safe" or "good"? The PHP manual makes no statement about writing to this array, only about reading from it.
Opinion perhaps, but I would think it's better instead to use define instead of the $GLOBALS array if you're looking to define a constant for use in other parts of your application.
PHP:define - Manual
<?php
function doSomething() {
$_POST['__newData'] = 'Hello World!';
}
doSomething();
echo $_POST['__newData'];
?>

Calling a function before it is declared [duplicate]

This question already has answers here:
Does function definition order matter?
(7 answers)
Closed 1 year ago.
The following code runs in PHP
<?php
$foo = "Chocolate milkshake";
go($foo);
function go($param) {
echo $param;
}
?>
// Output: chocolate milkshake
See this Demo http://codepad.viper-7.com/ToApZa
This code runs without errors and prints specified output, why?
I thought this "function hoisting" only occurred in JavaScript
It doesn't matter where you declare your functions in PHP in most cases, as you've just proved :)
Take a look at this page for more details. The key point:
Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.

'&' sign before function name in PHP [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something, the second a copy of something.
In first case, when the caller modify the returned value, something will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.

Using a callback function of a different class in array_map --PHP [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
setting scope of array_map php
I have a function called cube1() in a class called customExceptions.
In another PHP script I need to use array_map(), and for the callback function I want to use the cube1() function in the customExceptions class.
What is the syntax to do this? This seems a really basic question but I could't find a simple straight forward answer.
<?php
class customExceptions{
static public function cube1($i){
return $i*$i*$i;
}
}
$arr = array(1,2,3,4);
print_r($arr);
$arr2 = array_map(array('customExceptions', 'cube1'), $arr);
print_r($arr2);
?>
Shouldn't this work?
customExceptions::cube1(array_map());

One line code to get one value from function that returns an array [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Parse error on explode('-','foo-bar')[0] (for instance)
In PHP there are functions that return an array, for example:
$a = parse_url("https://stackoverflow.com/q/9461027/87015");
echo $a["host"]; // stackoverflow.com
My question is how to combine the above two statements into a single statement. This (is something that works in JavaScript but) does not work:
echo parse_url("https://stackoverflow.com/q/9461027/87015")["host"];
Note: function array dereferencing is available since PHP 5.4; the above code works as-is.
You can do a little trick instead, write a simple function
function readArr($array, $index) {
return $array[$index];
}
Then use it like this
echo readArr(parse_url("http://stackoverflow.com/questions/9458303/how-can-i-change-the-color-white-from-a-uiimage-to-transparent"),"host");
Honestly, the best way of writing your above code is:
$a = parse_url("http://stackoverflow.com/q/9461027/87015");
echo $a["scheme"];
echo $a["host"];
Isn't that what I originally posted?
Yes. Depending on context you may want a better name than $a (perhaps $url), but that is the best way to write it. Adding a function is not an attractive option because when you revisit your code or when someone reads your code, they have to find the obscure function and figure out why on earth it exists. Leave it in the native language in this case.
Alternate code:
You can, however, combine the echo statements:
$a = parse_url("http://stackoverflow.com/q/9461027/87015");
echo $a['scheme'], $a['host'];

Categories