Show where is equal to 0 (PHP) - php

I have a form, and I want to validate it with PHP, for that I going to make just one if the problem is I want to detect where is the variable that equal to 0
My code
if(isset($_POST["submit"])){
$name = $_POST["name"];
$lname = $_POST["lname"];
if($name == "" || $lname == ""){
echo "Please, Enter All informations";
}
else {
$namepr = preg_match("/^[A-Za-z]{3,}$/",$name);
$lnamepr = preg_match("/^[A-Za-z]{3,}$/",$lname);
if($namepr == 0 || $lnamepr == 0){
<!-- here I want to select the variable that equal to 0 -->
}
else {
echo "Your name is : ".$name."<br>Your Last name is : ".$lname
}
}
}
my question is in the comment part, I want to show the variable equal to 0, I have two variable, I want to select the variable equal to 0

For your issue, you can use a ternary operator:
if($namepr == 0 || $lnamepr == 0){
$myVar = ($namepr == 0 ? $namepr : $lnamepr);
// do something with $myVar
}
or of course, a standard if block:
if($namepr == 0 || $lnamepr == 0){
if($namepr == 0){
$myVar = $namepr;
} else {
$myVar = $lnamepr;
}
// do something with $myVar
}

Related

PHP Elseif always returning false, multiple conditions

I'm trying to make an helper php-function that's going to return true/false if the user got right access level. The access level is set when the user logs in. The problem is that the function always return false. The function is located in a "class" php-file that's included(with include_once) on the page I want to use it.
I'm kinda new to php, but the if conditions seems to be right.
I have tested to log in as admin and "economy", but it doesn't return true.
I also have tried to echoing the value that is sent as an parameter and even checking that the access level is right(by echoing the value) before the elseif statement.
const AccessLevelUser = 0;
const AccessLevelAdmin = 1;
const AccessLevelManager = 2;
const AccessLevelEconomy = 3;
public static function hasAccessLevel($requiredAccess) {
//file_put_contents('logg.txt', $logg);
if( !isset($_SESSION['accesslevel']) ) {
header("location:index.php?location=login");
exit;
}elseif ( $requiredAccess == AccessLevelAdmin && $_SESSION['accesslevel'] == AccessLevelAdmin ) {
echo "Admin True";
return true;
}elseif( $requiredAccess == AccessLevelEconomy && ($_SESSION['accesslevel'] == 3 || $_SESSION['accesslevel'] == 1) ) {
echo "Economy True";
return true;
}elseif( $requiredAccess == AccessLevelManager && ($_SESSION['accesslevel'] == 2 || $_SESSION['accesslevel'] == 1) ) {
echo "Manager True";
return true;
}elseif( $requiredAccess == AccessLevelUser && ($_SESSION['accesslevel'] == 0 || $_SESSION['accesslevel'] == 1) ) {
echo "User True";
return true;
}else{
echo "FALSE!";
return false;
}
}

php i need null to be null, zero to be zero

my table field status is NULL[default], or it is 0, or 1. then i assign to PHP var $status. when value is NULL i want to display no icon, when value is 0, display a gray check image, when value is 1, display a green check image.
trouble is, NULL value shows a gray check image, 0 does not show a check image. somehow NULL and 0 are alike but only in one direction. what i mean is, regardless of how i conditionally test if var is null, not null, null but not zero, they get interpreted wrongly. it is confusing. there must be a simple straight foward way to keep NULL and 0 separate and distinct. i grab the value:
$status = $Card['status']; //from above array.
if ($status == 1) {
$status = '1';
} else if ($status == 0) {
$status = '0';
} else if ($status === NULL) {
$status = 'NULL';
}
then to display the images either gray, green, or none at all i am trying this:
if ($status == '1') {
echo "<img src='../images/status_check_green.png' />";
} else if ($status == '0') {
echo "<img src='../images/porc_check_gray.png' />";
} else if ($status == 'NULL') {
echo "<img src='' />";
}
}
i know i do not need the '' around the values, but i am trying to literalize everything to force valid comparisons. likely no need for someone to try unraveling my code; but to elucidate how to keep NULL and 0 separate. it's like i am missing something fundamental here. btw, when i stuff a js var with the PHP var, it gets the correct value; they just don't follow the comparison like i need them to.
ideas?
HI your issue is procedure order or not type checking the 0 ( depending if you want to catch false )
if ($status == 1) {
$status = '1';
} else if ($status == 0) {
$status = '0'; //<--- this runs on null because (null == 0) is true
} else if ($status === NULL) {
$status = 'NULL'; //<--- this block is un-reachable
}
Because your not type checking with === of 0 null will return true for that condition.
See this sandbox with and example using $status = null;
http://sandbox.onlinephpfunctions.com/code/1f3dd9d83d0026aa0f682b61bed2ba858ae285aa
Outputs:
'0'
If you change it to this
if ($status == 1) {
$status = '1';
} else if ($status === 0) {
$status = '0';
} else if ($status === NULL) {
$status = 'NULL';
}
As you can see here using the same setting for $status
http://sandbox.onlinephpfunctions.com/code/d86b0c60c06338d2d6ee1c1fa9d3fa7e08a22663
Outputs
'NULL'
The other way to fix it would be to switch them so the more specific one is first.
if ($status == 1) {
$status = '1';
} else if ($status === NULL) {
$status = 'NULL';
} else if ($status == 0) { //I would prefer if(!$status){ but I'm lazy
$status = '0';
}
Then 0 would catch false as well as 0 but not null as the block above it will catch it first. You can see this last one here
http://sandbox.onlinephpfunctions.com/code/3028f5826dcede29b14dd8cfc03618ea5830c12c
Which also outputs
'NULL'
Cheers!

Checking Multiple $_GET variables if equals a string

if(isset($_GET['a']) || isset($_GET['b']) || isset($_GET['c'])){
if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x"){
echo "YES";
} else {
echo "NO";
}
}
in this php code, i'm trying to check if one of those requests isset and if one of them value == 'x' or not, But the 2nd part if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x") doesn't work as intended at all, I wrapped it inside () hoping it would work, In this condition, do i have to separate it as i did inthe isset() part? or is there a better method to do that?
This is likely what you are looking for
UPDATE - I just changed || to && for the last condition in case you were quick to try it out.
if( (isset($_GET['a']) && $_GET['a'] == "x") || (isset($_GET['b']) && $_GET['b'] == "x") || (isset($_GET['c']) && $_GET['c'] == "x")){
echo "YES";
} else {
echo "NO";
}
If you have to write a lot of conditionals you could use one of the following:
Using a foreach and a conditional:
$either_abc_is_x = function() {
$keys = ['a','b','c'];
foreach($keys as $key)
if(isset($_GET[$key]) && $_GET[$key] == "x")
return true;
return false;
};
echo $either_abc_is_x() ? 'YES' : 'NO';
Using a an array filter with a conditional:
$get_abc_keys_equal_to_x = array_filter(['a','b','c'], function($v) {
return isset($_GET[$v]) && $_GET[$v] == 'x';
});
echo $get_abc_keys_equal_to_x ? 'YES' : 'NO';
Array gymnastics:
$either_abc_is_x = isset($_GET) && in_array('x', array_intersect_key($_GET, array_flip(['a','b','c'])));
echo $either_abc_is_x ? 'YES' : 'NO';

TYPO3 userFunc give GET Parameter to php file

I am including a php file via userFunc and want to give the get parameter "?session_start=1" and use this in the function.
TS:
includeLibs.voucher = fileadmin/php/shop_init.php
lib.phpscript = USER
lib.phpscript.userFunc = voucher->init
lib.phpscript.session_start_var = GP:session_start
php file
<?php
class voucher {
public function init($content ="", $conf){
$session_start = $conf['session_start_var']?t3lib_div::_GP($conf['session_start_var']):false;
$lang_id = ($GLOBALS['TSFE']->sys_language_uid == 0 ? "&lang=de" : "&lang=en");
if($_GET["session_start"] == "" && $_GET["tx"] == "" && strpos($_SERVER['HTTP_USER_AGENT'], 'Safari')){
if($_GET["type"] == 1)
die(header("Location:http://xx.pro/post?shop=1&s=1&referer=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$lang_id"));
elseif($_GET["type"] == 2)
die(header("Location:http://xx.pro/post?shop=2&s=1$lang_id"));
elseif($_GET["type"] == 3)
die(header("Location:http://xx.pro/post?shop=3&s=1$lang_id"));
else
die(header("Location:http://xx.pro/post?s=1$lang_id"));
}
return "<iframe id='frame-one' scrolling='no' frameborder='0' style='".($_GET["type"] == "" ? "height:1640px;" : "height:1640px;")." width:100%;' src='http://xx.pro/post/index.php?view=".$_GET["view"]."&tx=".$_GET["tx"]."&st=".$_GET["st"]."&amt=".$_GET["amt"]."&type=".$_GET["type"]."$lang_id' ></iframe>";
}
}
?>
when I echo $session_start the output = GP:session_start and not 1
thx in advance
I think you assign the GP wrong, try it like this:
includeLibs.voucher = fileadmin/php/shop_init.php
lib.phpscript = USER
lib.phpscript.userFunc = voucher->init
lib.phpscript.userFunc.session_start_var = TEXT
lib.phpscript.userFunc.session_start_var.data = GP:session_start

Using OR/|| in else if in PHP not working correctly

So, I'm currently using a elseif() in PHP but I'm having a problem with the ||.
My code is as follows:
elseif($token_24_check == 0 || $token_48_check == 0)
{
echo "<script>alert('Please enter a valid token!');</script>";
}
The problem is, it does not check either $token_24_check == 0 or $token_48_check == 0 but it checks both of them. So if one of them is false it still returns false.
Does anyone know how to fix this?
UPDATE
Here's the full code from the first line (if()) to the last line (else)
if(empty($token))
{
echo "<script>alert('Please enter a token!');</script>";
}
elseif($user_check != 0)
{
echo '<script>alert("You can only redeem 1 token.\nPlease wait until your other token has expired!");</script>';
}
elseif($token_24_check == 0 || $token_48_check == 0)
{
echo "<script>alert('Please enter a valid token!');</script>";
}
elseif($token_check != 0)
{
echo "<script>alert('Token already used! Please use a new token');</script>";
}
else
echo "<script>alert('Token successfully redeemed!');</script>";
}
As your code is written you are using else if which should be elseif
However...
as the code is written you are making a comparison with else in there try:
if($token_24_check == 0 || $token_48_check == 0)
{
echo "<script>alert('Please enter a valid token!');</script>";
}
try reversing the if/elseif
$token_24_check = 0; $token_48_check = 1; # this
# or this
$token_24_check = 1; $token_48_check = 0; # this fires the ==0
if($token_24_check == 0 || $token_48_check == 0)
{
echo "<script>alert('== 0');</script>";
}
elseif($token_24_check >= 1 || $token_48_check >= 1)
{
echo "<script>alert('>= 1');</script>";
}
Fixed it by using a different tag.
Instead of using || I used && which I should've using in the first place.
Now it checks if both conditions are 0 and will then echo the string.
Thanks everyone for your help and tips. Much appreciated!

Categories