PHP nested IF ELSE Short hand [duplicate] - php

This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 6 years ago.
I'm trying to display a correct results based on the value of the variable but I'm using a short hand,
I have tried the following but its seems to ignore the first check even if the value is correct.
(!empty($national_ID_number)) ?
$national_ID_number :
(!empty($foreign_ID_number)) ?
$foreign_ID_number :
$temporal_permit_number)
Thanks

It's populating correctly for me with this code, you seem to have added additional bracket.
$national_ID_number = '';
$foreign_ID_number = 2;
$temporal_permit_number = 3;
$finalID = (!empty($national_ID_number)) ? $national_ID_number : (!empty($foreign_ID_number ) ? $foreign_ID_number : $temporal_permit_number);
echo $finalID;

I just went for a bit deep research - apparently each condition needs to be inside brackets and the below fix works fine.
(!empty($national_ID_number)) ?
$national_ID_number :
((!empty($foreign_ID_number)) ?
$foreign_ID_number :
$temporal_permit_number));
Reference and PHP Operator precedence

Related

How to declare string variable? [duplicate]

This question already has answers here:
What is the proper way to declare variables in php?
(5 answers)
Closed 9 months ago.
The first appearance to this variable $totpro in my code is this way
$totpro = $totpro + $row['profitloss'];
I want to use it to sum all profits, however, I receive this warning message on running
Warning: Undefined variable $totpro
but if I put this code before the previous code it runs with no problems
$totpro = "0";
I don't like using that code to declare the function, it tried
String $totpro
but unexpectedly it didn't work. Now tell me how to define $totpro without to have to use $totpro = "0";
If you are summing numbers, the initial declaration should set the value to 0 (i.e. a number):
$totpro = 0;
You tried "0", which is a string. Technically this will work, but it is not the best way.

A Code to $_REQUEST in Codeigniter? [duplicate]

This question already has answers here:
How to receive post/get request in codeigniter
(2 answers)
Closed 5 years ago.
In Codeigniter,
$something = isset($_POST['something']) ? $_POST['something'] : NULL; equal to $this->input->post('something');
$something = isset($_GET['something']) ? $_GET['something'] : NULL; equal to $this->input->get('something');
Is there equal code to $something = isset($_REQUEST['something']) ? $_REQUEST['something'] : NULL; ?
for REQUEST method in Codeigniter you have to use like describe below.
$this->input->get_post('some_data');
Let me know if it not works.
You can use bellow methods
$this->input->get_post('some_data'); Read more
$this->input->post_get('some_data'); Read more

AND/OR not working in if/else statement [duplicate]

This question already has answers here:
PHP If statement always firing as true
(2 answers)
Closed 5 years ago.
I'm having issues with AND/OR in an if/else PHP statement. This is one of the codes in particular:
if(is_page_template('page-home.php') || ('zaadvokati.php')) {
$class_trans = 'class="trans-color standard-menu"';
}else{
$class_trans = 'class="not-page-home standard-menu"';
}
If it's like this - with || / OR , it loads every паге template with the first class_trans option and not just the 2 templates specified, and if you put AND instead of OR, then it loads just the first one ('page-home.php') with the first class_trans and doesn't load the second one ('zaadvokati.php') with it. I suppose that, for some reason, it doesn't recognize the second template, but I don't know why.
Can you help?
Thanks!
You need to reiterate the function in the condition. try this:
if(is_page_template('page-home.php') || is_page_template('zaadvokati.php')) {
$class_trans = 'class="trans-color standard-menu"';
}else{
$class_trans = 'class="not-page-home standard-menu"';
}

Issue with isset() inside nested ternary operator [duplicate]

This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 2 years ago.
I am trying to show a input field value using if-else login and I want to use single line ternary operator for it but its always returning the error. However if I use if-else it is working fine. My code is :
Working Fine
if(isset($_COOKIE['m_name'])){
echo $_COOKIE['m_name'];
}else if(isset($this->userdata)){
echo $this->userdata[0]->first_name;
}else{
echo 'No Data';
}
Returning Notice Always
echo isset($_COOKIE['m_name'])? $_COOKIE['m_name']: isset($this->userdata)?$this->userdata[0]->first_name:'No Value';
Error :
Notice: Trying to get property of non-object in /home/.... on line...
I don't know what i am missing?
Any help would be greatly appreciated.
I'm not sure, but I think it should be
echo (isset($_COOKIE['m_name'])? $_COOKIE['m_name']: (isset($this->userdata)?$this->userdata[0]->first_name:'No Value'));

php variable from variable [duplicate]

This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 9 years ago.
I have a variable and I want to create a variable with that. I get the variable from database and put it together with some text and then I want another variable.
For exampel
$a = $ . "txt" . $d;
Try with this. It will create a variable from another one.
$a = ${'txt'.$d}
P.s. This is a question asked a couple of times. You might have found the answer simply by searching the issue on google.

Categories