Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
In the javascript world we can run a function through a Ternary comparison, I wanted to see if this applies to PHP, it seems it does only to an extent.
This is not for production use nor will it be actually used. This is merely a topic to see PHP's in-depth extent of the ternary comparison.
<?
$a = 1;
$b = 1;
function doThis($a){
print "$a";
}
$a == $b ? ( doThis('TRUE') ):( print "FALSE" );
?>
The above code works perfectly, however, is it possible to run multiple functions and or operations within ()?
Such as?
$a == $b ? ( doThis('TRUE'), doThis('THAT') ):( print "FALSE" );
or even?
$a == $b ? ( function(){ print "33"; doThis("TRUE") } ):( print "FALSE" );
You can have the ternary return a closure that would perform the requested function
$func = $a==$b?function(){ print "33"; doThis("TRUE"); }:function(){ print "FALSE"}); );
$func();
or borrowing from javascript you can create a IIFE (Immediately Invoked Function Expression)
$a==$b?call_user_func(function(){print "33"; doThis("TRUE");}):
call_user_func(function(){print "FALSE"; });
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Does PHP have keyword or feature that shortens code and repetition like jquery $(this)?
For example
// How to avoid typing same variable twice
echo isset($_POST['foo']) ? $_POST['foo'] : '';
In Jquery
<script>
// Avoid typing $("#button-element") again
$("#button-element").click(function() {
$(this).css("border-color", "#000");
});
</script>
No. And that code wouldn't work in JS either.
In JS, this is -- roughly speaking -- the object that the current function was called as a method on. It is not "the last thing I mentioned". The JS equivalent of your second code:
_POST['foo'] ? this : ''
would return an object if _POST['foo'] were set. It would not return the value of _POST['foo'].
There is no variable like the one you're looking for in any language I've ever used.
of course it does! you need to construct this across a class.
It works about the same as in javascript ES6 class.
$number = 4;
$This = new demoThis($number);
$This->multiplyThis(4);
class demoThis {
private $number;
private $factor;
public $result;
public function __construct($number) {
$this->number = $number;
}
function multiplyThis($factor) {
$this->factor = $factor;
$this->result = $this->number * $this->factor;
return $this->result;
}
}
echo isset($_POST['foo']) ? $This->result : '';
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How it will print hello - If hello is true, then the function must print hello, but if the function doesn’t receive hello or hello is false the function must print bye.
<?php
function showMessage($hello=false){
echo ($hello)?'hello':'bye';
}
?>
So if you don't want any condition you can add default value bye to para,eter. And simply echo it
<?php
function showMessage($hello="bye"){
echo $hello;
}
?>
Basically ($hello)?'hello':'bye'; is the shorthand for:
if ($hello == true) {
echo 'hello';
} else {
echo 'bye';
}
Reference: http://php.net/manual/en/control-structures.if.php
You are using ternary operator inside function, which will check the type of variable true or false. By default $hello variable type will be false.
So below code will be check if variable type is true then prine 'hello' else ternary operator will be print 'bye'.
It is same as like below
if($hello==true){
echo 'hello';
}else{
echo 'bye';
}
The reason why showMessage('abc') now prints 'hello' is because the ($hello) will evaluate to true as a non-empty string.
I guess what you are looking for is the type comparison operator ===. It will check whether the argument passed is actually a boolean value.
function showMessage($hello=false) {
echo ($hello === true)?'hello':'bye';
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I find myself doing the following quite a bit:
if (isset($objs[$key]){
$objs[$key] += val;
} else {
$objs[$key] = $val;
}
Is there a better way to do that?
Another way could be:
$objs[$key] = isset($objs[$key]) ? $objs[$key] + $val : $val
Is there a better way?
The easiest way to simplify could be a function taking the array by reference and deduplicate the code.
If you are using php 7 you can also make use the new ?? operator (assuming all values are integers):
$arr[$key] = ($arr[$key] ?? 0) + $value;
However, if you know the shape of the array you should base your actions on that. Create the array using array_fill_keys or similiar in that case.
If you are always adding numbers... you could do something like this to keep a single line of code for each entry which may or may not be preferable for you to your existing solution.
$objs = array();
addvalue($objs, "dog", 2);
addvalue($objs, "dog", 5);
// $objs['dog'] will = 7
function addvalue(& $var, $key, $value){
if (isset($var[$key])){
$var[$key] += $value;
} else {
$var[$key] = $value;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am not sure if this is even possible or if it makes sense but I want to set a variable to contain this loop:
<?php
do{
$x = $x + 1;
} while ($x<=5);
?>
Can this be accomplished with a variable variable?
I suspect you're looking to create a function:
function fun($x) {
do {
$x = $x + 1;
} while ($x <= 5);
return $x;
}
Then in your code you can simply write:
$x = fun($x);
Do you mean like this?
<?php
$varVar = rand(0,10);
for ($i=1; $i<=$varVar; $i++)
{/*the loop*/}
?>
I'm not really sure what you mean, unless you're after something similar to the block syntax of Objective-C?
I'm presuming you're new to PHP, and if so, are you sure you're not looking for functions? What you've done above will make the value of $x equal 5, if you wanted to do this with a function it would look like:
<?php
function doSomething($count) {
do {
// do something here
} while ($x <= $count);
}
doSomething(5);
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can someone please remind me how you call this type of variable
${'varname_'.$x}
Thanks!
It's called variable variables!
This should do it:
$varname = 'varname_'.$x;
echo $$varname;
variable varibales!!!
$var = "varname$x";
for($x=0;$x<10;$x++)
{
echo $$var;
}
edit ====
SHOULD BE:
for($x=0;$x<10;$x++)
{
$var = "varname$x";
echo $$var;
}
Variable variables. Your example could be used like so:
$varname_0 = 'Hello World!';
$x = 0;
echo ${'varname_'.$x}; // 'Hello World!'