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.
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 want to make a logic to check multiple conditions without using if else condition numbers of time.
I have 4 variables and i have to check whether it is empty or not.
if one of the variable is blank than want to do something.
I have function like this..
function searchResult($genre, $subject, $type, $grade){
// checking conditions here
}
please suggest me simple method.
You can use func_get_args, array_filter and func_num_args as well:
function searchResult($genre, $subject, $type, $grade){
if (count(array_filter(func_get_args())) < func_num_args()) {
echo 'One or many arguments are empty.. do something';
}
}
Pay attention to the array_filter function:
If no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed.
function searchResult($genre, $subject, $type, $grade){
if((!empty($genre)) && (!empty($subject)) && (!empty($type)) )
{
echo "not empty";
}
else
{
echo "empty";
}
}
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
I have two variables in php/magento as in below
$currentA = $advert->getA();
$currentB = $advert->getB();
I want to make sure that atleast one of these have a value....Basically a validation to make sure atleast one of these have a value. Am I doing it correct?
$currentA = $advert->getA();
$currentB = $advert->getB();
if (!($currentA != '' || $currentB !== '')) {
echo "do something";
}
It is more complicated than that. Like SQL fields, php variables may also be NULL and generate warning when accessed for data.
So use empty(var) because that tests for all of the possible empty conditions and doesn't give warnings if the variable has been declared without a value.
if (!(empty($currentA) || empty($currentB))) {
echo "do something";
}
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float) "
0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
You wouldn't need the ! because if either of these have a value, it will return true, and the ! operator checks if this condition is false, so it will work opposite of when it's supposed to. You should try
if ($currentA || $currentB) {
echo "do something";
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Im wondering if i can do and/or together. I'm trying to make a code that checks if both variables are wrong or if only one of them is wrong and the other one is right then I want to show a message like: "one of the "variables" are wrong.
Can anyone help me out here?
In addition to Tim Whites answer, if you don't want it nested, you could also use:
if($a == false AND $b == false) { echo "Both variables are false"; }
elseif($a == false OR $b == false) { echo "One variable is false"; }
if ($a==false OR $b==false) {
if($a==false AND $b==false) { echo "Both variables are false"; }
else { echo "On of the variables is false"; }
}
Something like that will do.
You could just use multiple IF statements, to check if 1 variable is wrong or both are wrong. Like:
if ($variable1==false && $variable2==false){
}
else{
if ($variable1==false || $variable2==false){
}
}