PHP assignment with a default value - php

What's a nicer way to do the following, that doesn't call f() twice?
$x = f() ? f() : 'default';

In PHP 5.3, you can also do:
$a = f() ?: 'default';
See the manual on ?: operator.

This seems to work fine:
$x = f() or $x = 'default';

function f()
{
// conditions
return $if_something ? $if_something : 'default';
}
$x = f();

$x = ($result = foo()) ? $result : 'default';
test

You could save it to a variable. Testcase:
function test() {
echo 'here';
return 1;
}
$t = test();
$x = $t ? $t : 0;
echo $x;

Related

Reset global variable in php after function return

$a = 0;
function a(){
global $a;
$success = b();
if ($success) {
return $a;
$a = 0;
}
}
function b(){
global $a;
$a += 9;
return true;
}
In function a if function b is success then i want to return $a and then set $a to 0
how can i do this ?
You could try creating a temporary variable and then set $a before returning.
$temp = $a;
$a = 0;
Return $temp;

Advanced php formatting need help understand layout

Here is the function:
function simplePresent($e) {
$w = ($f = preg_match)('/ey|se|d |[sI]$|We/', $a = $e[0]);
be == ($b = $e[1])
?
$b = $w ? $a == I ? am : are : is
:
$w ?: $b = $b == have ? has : $b .= $f('/[h-z]$/', $b) ? es : s;
return "$a $b $e[2]";
}
this is a solution on codefights. it handles formatting under defined parameters. I am trying to understand the function assignment to the variables $f and $w. Also the the use of variables with no quotes. lastly the nested question marks and colons. How is this functioning?
Thanks
The function preg_match() is being assigned to the variable $f and reused later in the code. $w is simply the result of preg_match(). For example:
<?php
$w = ($f = 'sprintf')("foo");
// same as this:
$f = 'sprintf';
$w = $f("foo");
// which is the same as this:
$w = sprintf('foo');
The unquoted values will first be interpreted as constants, and the undefined constants will be interpreted as strings. Here's how a proper constant definition looks:
<?php
define("foo", "bar");
echo foo;
echo bar;
// PHP Notice: Use of undefined constant bar - assumed 'bar'
// same as this:
echo "bar";
echo "bar";
The question marks and colons are part of a ternary statement. This is a useful shorthand, but PHP recommends against stacking them together as is done there (the result is "non-obvious.") A simple one looks like this:
<?php
echo ($foo == "foo" ? "equal" : "inequal");
$a = $b ? $c : $d;
// this is the same as:
if ($foo == "foo") {
echo "equal";
} else {
echo "inequal";
}
if ($b) {
$a = $c;
} else {
$a = $d;
}

Do you know a nice PHP hack to do inline conditional assignation?

I really enjoy || operator in JavaScript, where we can do inline conditional assignation.
var a = 0;
var b = 42;
var test = a || b || 'default value';
console.log(test); // 42
This is clear to read, and don't take too many lines.
In PHP, this logical operator return booleans:
$a = 0;
$b = 42;
$test = $a || $b || 'default value';
print_r($test); // bool(true)
Of course, we can do inline assignation using ternaries:
$test = $a ? $a : $b ? $b : 'default';
print_r($test); // int(42)
But this make code ambiguous, this is not that easy to read.
So here my question come:
Do you know a nice PHP hack to do inline conditional assignation ?
In PHP 5.3+ you can do this:
$test = $a ?: ($b ?: 'default value');
This will work as long as you don't need to short-circuit side effects:
function either_or() {
$nargs = func_num_args();
if ($nargs == 0) {
return false;
}
$args = func_get_args();
for ($i = 0; $i < $nargs-1; $i++) {
if ($args[$i]) {
return $args[$i];
}
}
return $args[$nargs-1];
}
$test = either_or($a, $b, "Default value");

PHP: Return a value from a Function and change the variable for the while Loop that it is in

Is it possible to execute a function, then return a new value for Var and therefore when it tries to loop again it checks the condition and detects the changes?
Let's say I have this function:
function changeZ(){
$z=1
return $z;
}
and this is my while loop:
$a = 0;
$z = 10;
while($a<$z){
changeZ();
$a++;
}
How should I modify the codes such that
the function changeZ() returns a new value for the variable $z
and therefore when the loop checks for condition (1<1), it returns a false and stop looping.
You can pass by reference or return the value
$z = 100;
change($z);
function change(&$var) {
$var = 1;
}
$z = 100;
$z = change($z);
function change($var) {
return $var * 100 - 50;// Logic with $z obviously can be whatever
}
The $z in your function and the $z you use in the loop are not the same guys. So you have to set the value of the loop-z to the return value of your function...
$a = 0;
$z = 10;
while($a<$z){
$z = changeZ();
$a++;
}
$z = changeZ();
but as commented, you appear to definitely going about this wrong.
$a = 0;
$z = 10;
while($a<$z){
$z = changeZ();
$a++;
}
Since your function returns a value you should set your variable to contain the returned value. This will do what you asked resulting in the loop running only once.
Another thing you can do is pass the variable into a function like so
function changeZ($in){
$out = $in-1;
return $out;
}
$a = 0;
$z = 10;
while($a<$z){
$z = changeZ($z);
$a++;
}
This will result in the loop running 5 times as one number goes up the other goes down
0<10
1<9
2<8
3<7
4<6
you can use this for your purpose for storing the returned data from function into an array.
function changeZ(){
$z = $row['result']; //your data from database
return $z;
}
$a = 0;
$z = 10;
$returned_value=new array();
while($a<$z){
$returned_value[] = changeZ();
$a++;
}
You have to put this
$a = 0;
$z = 10;
while($a<$z) {
$z = changeZ();
$a++;
}

Conflicting static vars with non-static ones in changing values

Consider the following code:
class A {
private $a;
function f() {
A::a = 0; //error
$this->a = 0; //error
$self::a = 0; //error
}
}
How can I change $a in f()?
You were close. Either:
self::$a = 0; //or
A::$a = 0;
If it's static, or:
$this->a = 0;
If it's not.
I believe the syntax is:
self::$a
obviously we all have been tricked by the title of the question, though here is how you change $a's value.
<?php
class A {
private $a;
function f() {
//A::a = 0; //error
$this->a = 10; //ok
//$self::a = 0; //error
}
function display() {
echo 'a : ' . $this->a . '<br>';
}
}
$a = new A();
$a->f();
$a->display();
?>
Output:
a : 10
if you want $a to be static use the following:
<?php
class A {
private static $a;
function f() {
//A::$a = 0; //ok
//$this->a = 10; //Strict Standards warning: conflicting the $a with the static $a property
self::$a = 0; //ok
}
function display() {
echo 'a : ' . self::$a . '<br>';
}
}
$a = new A();
$a->f();
$a->display();
// notice that you can't use A::$a = 10; here since $a is private
?>

Categories