PHP : Function - Return 2 variables (Boolean + $variable) - php

Issue Resolved : Here is the solution :
function testCoreq()
{
$coreqTest = makeCoreq();
if(empty($coreqTest))
{
return array(true);
break;
}
else
{
foreach ($coreqTest as $ctest)
{
if($ctest['value'] == "true")
{
return array(true);
break;
}
else
{
return array(false,$ctest['coreqID']);
}
}
}
}
if(testCoreq()[0])
{
//do something
}
else
{
return testCoreq()[1]
}
I'm doing a school project and hit kind of a bump.
I created a function and i want it to either return "true" (boolean) or "false" (boolean) + a variable.
I searched the net quite a bit but wan't able to find a simple way to do this .
Is there any way to this this ? //Thanks
The function is working properly but when it is returning the variable - it is also assuming that the function is returning "true" when i want it to return false + the value like :
else
{
return $ctest['coreqID'];
return false;
}
Here is the code :
function testCoreq()
{
$coreqTest = makeCoreq();
if(empty($coreqTest))
{
return true;
break;
}
else
{
foreach ($coreqTest as $ctest)
{
if($ctest['value'] == "true")
{
return true;
break;
}
else
{
return $ctest['coreqID'];
}
}
}
}
I am using it like this:
if (testCoreq())
{
// do something
}
else
{
// return the variable
}
but even if the first statement is false , then it is returning the variable - it is assuming the function is true.

You can try to return -1 on true and other for false to decrease amount of returning values. Next option is to return array of values. The other option would be to pass reference variable in the function.

Related

PHP if statement, does not validate, only first if runs

I have 3 stages to my if statement.
If is front page return true.
If is not front page but object is set and has certain value return true.
If not return false.
It worked as two seperate statement,
1) if is front page return true if not false
2) if object is set and has given value return true, objects is not set type return false.
When I try to put them as one statement I get true if front page but this rest doesn't return.
Code is below. Is their anything wrong with the statement? Thanks
<?php
$object = get_object();
if(is_front())
{
return 'true';
}
elseif (!(is_front() & !empty($object))) //is not front but object has been set check value
{
confirm_value();
}
else (!(is_front() & empty($object))) //if not front and object is not set
{
return 'false';
}
function confirm_value() {
$value = load($object); //load object
if($value->id($id)) //check value of id
{
return 'true';
}
else
{
return 'false';
}
}
?>
working code below
<?php
$object = get_object();
if(is_front())
{
return 'true';
}
elseif (!(is_front() && !empty($object))) //is not front but object has been set check value
{
return confirm_value();
}
else
{
return 'false';
}
function confirm_value() {
$value = load($object); //load object
if($value->id($id)) //check value of id
{
return 'true';
}
else
{
return 'false';
}
}
?>
Two problems I see:
1 - you are using a single & when you should be using &&.
2
confirm_value();
should be
return confirm_value();
I don't know the aim of code above but:
Use return confirm_value();
Else statement do not have condition. If you want check condition you should continue using elseif
Should using && instead of &. Because of & is bitwise operator. It will same result with && when all conditions are boolean. But when 1
& 2 == false

use the functions without its parameters

I am using 2 regex functions here and I wanna make another function which returns false when the 2 regex are both false and if not, then true.
The problem here is when I wanna use the 2 regex functions in the third one, I have to give them parameters, which is not necessary I think, because the third function will only return a simple true or false. I get an undefined variable whenever I give parameters to the 2 regex functions in the 3rd one.
I tried using global variables which works but since its a bad practice I am looking for a better solution.
Code:
function regex1($input)
{
$regex= "/^[A-Za-z0-9 ]*$/";
if (!preg_match($regex, $input))
{
return false;
}
else
{
return true;
}
}
function regex2($input)
{
$regex= "/^[A-Za-z0-9 ]*$/";
if (!preg_match($regex, $input))
{
return false;
}
else
{
return true;
}
}
function checkBoth()
{
if (regex1($input) === false || regex2($input) === false)
{
return false;
}
else
{
return true;
}
}
EDIT:
The checkBoth function I am using in my other file like this together with the other 2 regex functions:
if (!regex1($input))
{
// show error at the same time
}
if (!regex2($input))
{
// show error at the same time
}
if(checkBoth())
{
// success
}
function regex2($input,$secondVar=false)
{....
Later in code in place where you need just add:
if($secondVar !== false){
// do whatever...
}
If you can't user "false" you can just empty string '' or any other value that will not appear there.

Return an empty but tell php it means `return false`

Is it possible to return an array, but also tell php it's supposed to mean false?
Example:
if ($res = a_function()) {
// all good
}
else {
echo getErrorByNumber($res['err_no']);
}
a_function:
function a_function() {
// do fancy stuff
if (xy) return true;
return array('err_no' => 1);
}
I guess its not possible, since php will always take an array for return true, right?
Lot's of ways. Probably the preferred one, compare to true with type checking ===:
if(($res = a_function()) === true) {
// all good
}
else {
echo getErrorByNumber($res['err_no']);
}
A non-empty array will always be true:
if($res = a_function() && !is_array($res)) {
// all good
}
else {
echo getErrorByNumber($res['err_no']);
}
Or flip it around:
if(is_array($res)) { //or isset($res['err_no'])
echo getErrorByNumber($res['err_no']);
}
else {
// all good
}
I would solve this problem with a byref parameter:
function foo(&$errors)
{
if (allWentWell())
{
$errors = null;
return true;
}
else
{
$errors = array('err_no' => 007);
return false;
}
}
// call the function
if (foo($errors))
{
}
else
{
echo getErrorByNumber($errors['err_no']);
}
This way you do not have to distinguish between different possible return types and you will not run into type juggling problems. It is also more readable, you know what's inside the $errors variable without documentation. I wrote a small article explaining why mixed-typed return values can be so dangerous.

"array_key_exists()" not working properly

Code
$descriptionArr = array( "uk/page"=>"", "uk/page-two"=>"description of page 2");
function getDescription($uri){
if (array_key_exists($uri, $descriptionArr)) {
return $descriptionArr[$uri];
} else {
return false;
}
}
Situation
When i call the function with argument "uk/page-two" it returns the description
When I call the function with argument "uk/page" it returns false instead of the empty string
Issue
I would like it to return the empty string and only return false when the argument passed does not exist as key in the array.
This should work:
$descriptionArr = array( "uk/page"=>"", "uk/page-two"=>"description of page 2");
function getDescription($uri, $descriptionArr){
if (false !== array_key_exists($uri, $descriptionArr)) {
return $descriptionArr[$uri];
} else {
return false;
}
}
You can change your function to the following:
function getDescription($uri) {
if (isset($descriptionArr[$uri])) {
return $descriptionArr[$uri];
} else {
return false;
}
}

Test only evaluating first element of array

I loop through the values of a form, to check that each field has 4 digits. My problem is currently it validates true or false only on the match for the first field $card1...
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++)
if (! preg_match ($regex,$cards[$i]))
{
return false;
}
else
{
return true;
}
}
You're returning (by using return ...) something in the first iteration every time (boolean condition with an else).
You need to put the return true outside the loop statement:
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++) {
if (! preg_match ($regex,$cards[$i])) {
return false;
}
}
return true;
}
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++)
if (! preg_match ($regex,$cards[$i]))
{
return false;
}
return true;
}

Categories