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;
}
}
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 1 year ago.
Improve this question
I am looking for smart solution, maybe you could help me out. Currently I am doing an own Authentication (Separate Class) system for my Webshop project. My problem is, that I need conditional statement inside foreach loop, to return the code (see below). Any suggestions?
My code currently look like this
public function regiAuth($email, $password, $firstname, $lastname)
{
$authContainer = [$email, $password, $firstname, $lastname];
foreach ($authContainer as $a) {
return !empty($_POST[$a]);
}
}
And I want to result this (With &&)
return !empty($_POST[$email]) && !empty($_POST[$password]) &&
!empty($_POST[$firstname]) && !empty($_POST[$lastname])
I believe you could do simply by do
foreach ($authContainer as $a) {
if (empty($_POST[$a])
return false;
}
return true;
instead of checking if all of them are full, you look if there is at least one empty.
it is a good practice to stop iteration if you find one element that is not as expected, imagine if you had an array of hundreads of assertions to do.
making an full if statement would look like this, here it checks if the $_POST are not empty. && means that both have to true or false
foreach ($authContainer as $a) {
if((!$_POST[$email]) && (!$_POST[$password])){
return false;
}
}
return true;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to do something that I'm sure is simple but I can't gather how to do it from the documentation.
All I want to do is:
function something() {
// Do some stuff with cURL
set $object->name;
set $object->corporation;
// some if statement
set $object->other; //if cURL finds "other" exists
return $object;
}
And be able to do this:
$result = something();
$name = $result->name;
etc.
Does anyone know the best way to do this? I use procedural style and have very little know-how with objects hence I'm trying to learn.
function foo(){
$result = new stdClass();
$result->name = "Joe";
$result->other = "bar";
return $result;
}
$object = foo();
echo $object->name; // Should display "Joe"
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 7 years ago.
Improve this question
I have
$room=$_POST['room'];
$lastMTime=$_POST['lastMTime'];
function _getLastMsg($rName, $lMT){
$r=$rName.'.xml';
$rXML=simplexml_load_file($r) or die('Not found');
$rLMT=$rXML->lastMT;
if($lMT<$rLMT){
//some code here
}
}
How do I echo every $rXML->username that has $rLMT > $lMT?
I want to get all usernames that have been submitted after $lMT.
may be you can try this code
<?php
$var = [11, 10, 20, 30, 40];
for($i=0; $i<count($var)-1; $i++) {
if($var[$i]>$var[($i+1)]) {
echo "var $var[$i] > var ".$var[($i+1)];
}
}
?>
First of all make sure that all these vars should not be an array instead. When you have multiple variables which are handled in the same way usually it is a clear sign that they should be an array instead.
If not - you can use compact function to make variable names into array with their names and values, read the manual on it. Other way to access variable value by its name is variable variables - $$
Using the variables into your example you can do this
$i = 0;
while ($i <= 3) {
$varName = 'var'.$i;
if ($$varName > $var) {
echo $$varName;
}
$i++;
}
But that is no right way to iterate over an array. There exists a million other ways to iterate over a bunch of variables and print them, all of them are not the "right" way to do it. In your example code there is no array, where there should be one. Like this
$var = 11;
$vars = [10, 20, 30, 40];
# iterate and echo each element of $vars array)
foreach ($vars as $current) {
if ($current > $var) {
echo $var;
}
}
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 to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to define a link based of a $_GET variable, but it's saying there's an error on a line that doesn't exist...
<?php
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
?>
<?php
if ($ref != "") {
$link = "http://site.com/page.php?ref=$ref";
} else {
$link = "http://site.com/page.php";
}
?>
Anyone see what's up? I was pretty sure it was fine.
I've tried it multiple different ways, with isset etc... same result.
You are missing a closing }:
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
}
By the way, this code is quite redundant. empty() will also check whether the variable is set, so you don't need isset().
You can also use the ternary operator, which is for cases like this:
$ref = empty($_GET['ref']) ? null : $_GET['ref'];
And later check with:
if (!is_null($ref)) {
//whatever
}
Otherwise, in your code, when execution reaches if ($ref != "") {, the variable $ref might not even exist - this will throw an E_NOTICE, which you might not even see, depending on your settings.