Using of variable in nested IF ELSE (PHP) [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I have a problem in using of array as a variable in nested if..else.. conditions. Its used to create custom web-service APIs. Here is the structure of code:
if (condition) {
// code to be executed in case of true
if (condition) {
// code to be executed in case of true
$result['key1'] = $variable1;
$result['key2'] = $variable2;
$result['key3'] = $variable3;
} else {
// code to be executed in case of false
}
} else {
$result['error'] = 'Something went wrong!!!';
}
echo json_encode($result); // line 121
On execution of code it displays the following error:
Notice: Undefined variable: result in C:\xampp.. on line 121

Variables need to be declared before using it inside functions like json_encode()! Do it this way:
$result = array();
if (condition) {
// code to be executed in case of true
if (condition) {
// code to be executed in case of true
$result['key1'] = $variable1;
$result['key2'] = $variable2;
$result['key3'] = $variable3;
} else {
// code to be executed in case of false
}
} else {
$result['error'] = 'Something went wrong!!!';
}
echo json_encode($result);

U need to declare your variable first if you are directly entering the value to an array.
$result = array();
if (condition) {
// code to be executed in case of true
if (condition) {
// code to be executed in case of true
$result['key1'] = $variable1;
$result['key2'] = $variable2;
$result['key3'] = $variable3;
} else {
// code to be executed in case of false
}
} else {
$result['error'] = 'Something went wrong!!!';
}
echo json_encode($result); // line 121

Related

PHP fixing undefined error with echo instead? But does not work [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 2 years ago.
I am inserting and updating some values through a form with radioboxes and textarea.
Basically the query works fine but if user does not select any radio boxes or leaves the text area empty, I want an error message to pop up, instead of real php errors of undefined.... blah blah.
Sorry for this weird layout, but this text editor is super problematic with editing..
$var1 = $_POST['var_1'];
$var2 = $_POST['var_2'];
$var3 = $_POST['var_3'];
if(isset($_POST['button_name'])) {
if(query1 && query2) {
echo "Successfully inserted and updated"</h3>";
} elseif(empty($var1) && empty($var2) && empty($var3)) {
echo "not selected. Please Try Again"</h3>";
} else {
echo "failed to sent cause of query issue";
}
} else {
echo "nothing was selected";
}
First check if the variables are defined, then set them to their values:
$var1, $var2, $var3;
if(issset($_POST['var_1']) && isset($_POST['var_2']) && isset($_POST['var_3']) && isset($_POST['button_name'])) {
$var1 = $_POST['var_1'];
$var2 = $_POST['var_2'];
$var3 = $_POST['var_3'];
} else {
echo "Your custom error message";
}
Alternatively, you can use try and catch:
try {
$var1 = $_POST['var_1'];
$var2 = $_POST['var_2'];
$var3 = $_POST['var_3'];
$_POST['button_name'];
} catch {
echo "Your custom error message";
}

How can I check if an object method exists? [duplicate]

This question already has answers here:
Is there a way to check if a function exists within a class?
(3 answers)
Check if method exists in the same class
(4 answers)
Closed 3 years ago.
I want to use some code only if the method getProductgroup exists.
My first approach:
if(isset($item->getProductgroup())){
$productgroupValidation = 0;
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup){
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
I got the error message:
Compile Error: Cannot use isset() on the result of an expression (you
can use "null !== expression" instead)
if(($item->getProductgroup())!==NULL){
$productgroupValidation = 0;
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup){
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
But like this I also get an error message:
Attempted to call an undefined method named "getProductgroup" of class
"App\Entity\Documents".
You can use the function method_exists to check if the method is existing in a class or not. for example
if(method_exists('CLASS_NAME', 'METHOD_NAME') )
echo "it does exist!";
else
echo "nope, it is not there...";
In your code try
if(method_exists($item, 'getProductgroup')){
$productgroupValidation = 0;
if(method_exists($item->getProductgroup(), 'getUuid'))
{
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup)
{
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
}
}

getting an "undefined variable" on isset [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
So i've got a rather complex bit of code that i'm trying to make work (actually; it works fine on my test server but not so much my live server. so i'm trying to make it rework) -- it keeps telling me that a variable i'm asking it to check with isset is not a defined variable. I'm probably over thinking this whole method and there's probably a simplier way. but i need you guys to bash me over the head with it apparently
include('bootstra.php');
include('includes/parts/head.php');
if(!isset($_SESSION['user']['uid']))
{
echo $html;
include('includes/parts/login.php');
if(isset($_GET['mode']))
{
$file = $_GET['mode'];
$path = "includes/parts/{$file}.php";
if (file_exists($path)) {
require_once($path);
} else {
die("The Page REquested could not be found!");
}
}
else
{
include('includes/parts/frontpage.php');
}
}
else
{
if(!isset($_SESSION['x']) || !isset($_SESSION['y']))
{
if(isset($_GET['x']) && isset($_GET['y']))
{
$_SESSION['x'] = $_GET['x'];
$_SESSION['y'] = $_GET['y'];
}
else
{
$_SESSION['x'] = 0;
$_SESSION['y'] = 0;
}
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
if($_SESSION['x'] != $_GET['x'] || $_SESSION['y'] != $_GET['y'] )
{
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
echo $html;
echo $base->displayMap($_GET['x'], $_GET['y']);
include('includes/context_menu.php');
}
}
}
Here is the exact error:
Notice: Undefined index: x in /home/****/public_html/reddactgame/index.php on line 27
Change it
<pre>
if( !isset($_SESSION['x']) || !isset($_SESSION['y']) )
</pre>
to
<pre>
if( !( isset($_SESSION['x']) && isset($_SESSION['y']) ) )
</pre>
There is no error, that is a Warning...
It is saying that $_SESSION['x'] was never setted, so, when you do that isset it tells you that it was never declared.
Example:
This gives you the same warning
empty($x);
This does not give you warning
$x = 0;
empty($x);
EDIT
I see this is a common question, so here is a better explanation.

Undefined variable: job [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
It is display $job as undefined. What is wrong with my switch statement?
in a switch case we have to pass only one variable but how can i append the array here.
list_questatus is also values of array
if(isset($_POST['list_queuestatus']))
{
$selected= $_POST['list_queuestatus'];
switch($selected[$job]){
case 'r':
if($state[$jid]=="r"){
$jobs[$j]=$jid;
}
break;
}
}
$j=0;
$jobs=array();
for($i = 5; $i < count($output); ++$i) {
$jid=trim(substr($output[$i],0,30));
$jobid[$jid]=trim(substr($output[$i],0,30));
$username[$jid]=trim(substr($output[$i],30,15));
$queue[$jid]=trim(substr($output[$i],47,15));
$jobname[$jid]=trim(substr($output[$i],63,15));
$sessionid[$jid]=trim(substr($output[$i],79,8));
$nds[$jid]=trim(substr($output[$i],88,4));
$tsk[$jid]=trim(substr($output[$i],93,5));
$reqmem[$jid]=trim(substr($output[$i],99,6));
$reqtime[$jid]=trim(substr($output[$i],106,5));
$state[$jid]=trim(substr($output[$i],112,2));
$elaptime[$jid]=trim(substr($output[$i],114,8));
if(isset($_POST['list_queuestatus']))
{
$selected= $_POST['list_queuestatus'];
switch($selected[$job]){
case 'R':
if($state[$jid]=="R"){
$jobs[$j]=$jid;
}
break;
case 'Q':
if($state[$jid]=="Q"){
$jobs[$j]=$jid;
}
break;
case 'H':
if($state[$jid]=="H"){
$jobs[$j]=$jid;
}
break;
case 'S':
if($state[$jid]=="S"){
$jobs[$j]=$jid;
}
break;
case 'W':
if($state[$jid]=="W"){
$jobs[$j]=$jid;
}
break;
case 'E':
if($state[$jid]=="E"){
$jobs[$j]=$jid;
}
break;
case 'A':
$jobs[$j]=$jid;
}
}
else{
$jobs[$j]=$jid;
break;
}
}
$selected='';
function get_options($select)
{
$list_queuestatus=array("--Select Status--"=>"SS","All"=>"A","Running"=>"R","Queued"=>"Q","Held"=>"H","Suspened"=>"S","Waiting"=>"W","Error"=>"E");
$options='';
while(list($k,$v)=each($list_queuestatus))
{
if($select==$v)
{
$options.='<option value"'.$v.'"selected>'.$k.'</option>';
}
else{
$options.='<option value"'.$v.'">'.$k.'</option>';
}
}
return $options;
}
echo '<form action="main.php?page=queuestatus&';
echo '" method="POST">';
echo '<select name="list_queuestatus" onchange="this.form.submit();">';
echo get_options($selected);
echo '</select>';
echo '</form>';
Please use $_POST instead of $_post. And please declare the $job variable before it used or the outer scope.
It seems that that the job is not a variable its a index for the array. So please use 'job' instead of $job.
Hope this will resolve your issue.

Undefined variable: value in PHP [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I was trying to print the value of a variable from a function, but it is showing undefined variable value. Please help me and tell me how to solve this problem; I'm pretty new
to PHP coding. I tried the function as public, but it shows some other error.
<?php
if(isset($_POST['btn_submit']))
{
$num1 = $_POST['cards1'];
$num2 = $_POST['cards2'];
$num1=substr($num1,0,(strlen($num1)-1));
$num2=substr($num2,0,(strlen($num2)-1));
}
function get_value() {
switch ($num2) {
case 'A' :
$value = 11;
break;
case 'J':
case 'Q':
case 'K':
$value = 10;
break;
default:
$value = $num2;
break;
}
//return $value;
}
echo '<script type="text/javascript">alert("Out Put is'.$value.' ");</script>';
//$total_value = $num1+$num2;
?>
$num2 is local to your function. You need to pass it in as a parameter:
function get_value($num2) {
*** YOUR CODE
}

Categories