use of undefined constant is not remedied by isset [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the PHP error message “Notice: Use of undefined constant” mean?
I got "use of undefined constant" message when load this function:
function awp_get_options($i){
$i[selects][] = 'special_effects';
$i[radios][] = array('js_library','sack');
$i[selects][] = 'effects';
$i[checkboxes][] = 'no_load_library';
return $i;
}
I changed it to:
function awp_get_options($i){
if(isset( $i[selects] )){
$i[selects][] = 'special_effects';
$i[selects][] = 'effects';
}
if(isset( $i[radios] ))
$i[radios][] = array('js_library','sack');
if(isset( $i[radios] ))
$i[checkboxes][] = 'no_load_library';
return $i;
}
It still says use of undefined constant. How to correct this code?

function awp_get_options($i){
$i['selects'][] = 'special_effects';
$i['radios'][] = array('js_library','sack');
$i['selects'][] = 'effects';
$i['checkboxes'][] = 'no_load_library';
return $i;
}
The rest is up to what type is $i given to a function. Other is correct now.

add quotes to your array keys like:
$i[selects]
to
$i["selects"]
//or
$i['selects']

Related

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.

PHP for loop variable [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.
I have made this for loop to get a variable amount of columns selected from the database. As I'm running this, I get an error saying :
Notice: Undefined variable: kolom_1
Notice: Undefined variable: kolom_2
Notice: Undefined variable: kolom_3
Notice: Undefined variable: kolom_4
Notice: Undefined variable: kolom_5
Notice: Undefined variable: kolom_6
But I have it all placed in a for loop, why does he not recognize them? I am not getting what I'm doing wrong.
function lijst_ophalen($data, $from){
$totaal = count($data);
for ($i=1; $i<$totaal; $i++){
$kolom_[$i] = $this->mysqli->real_escape_string($data['kolom_' . $i . '']);
if($kolom_[$i]!="") $kolom_[$i] = "{$kolom_[$i]},"; else $kolom_[$i]="";
if($kolom_[$i]==$totaal) $kolom_[$i] = "{$kolom_[$i]}";
}
$from_table = "";
$categorie = "";
if($from == "bv"){
$from_table = "klanten_algemene_gegevens_bv";
$categorie = "";
}
if(($from == "1manszaak") || ($from == "vof")){
$from_table = "klanten_algemene_gegevens_vof_1manszaak";
if($from == "1manszaak"){
$categorie = "1manszaak";
}
if($from == "vof"){
$categorie = "vof";
}
$categorie = "WHERE soort_onderneming = '{$categorie}'";
}
if($from == "ib"){
$from_table = "klanten_ib";
$categorie = "";
}
$result = $this->mysqli->query(
<<<EOT
SELECT
{$kolom_1}
{$kolom_2}
{$kolom_3}
{$kolom_4}
{$kolom_5}
{$kolom_6}
FROM {$from_table}
{$categorie}
EOT
);
if($result){
$waardes = array();
while ($row = $result->fetch_assoc()) {
$waardes[]=$row;
}
return $waardes;
}
}
When you are initializing / filling data into the $kolom_ you are creating an' array instead of your (properly) intended series of different variables.
$kolom_[$i] <--- ARRAY
And lower down you are calling a series of variables $kolom_1, which doesn't exist because you created an' array and not a series of variables.
To avoid the error you can simply change your
$kolom_1
$kolom_2 (and so on)
calls into
$kolom_[1]
$kolom_[2] (and so on)
and you should be set to go.

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
}

What wrong with undefined index o? [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I got error in my data viewing page, what is the correction for my php script
Here is my php script:
Notice: Undefined index: o in /home/tz005/public_html/COMP1687/view_data.php on line 64
<?php
$con=mysqli_connect("mysql.cms.gre.ac.uk","tz005","punceg5L","mdb_tz005");
if (is_numeric($_GET['o']))
{
$o=$_GET['o'];
}else {
$o=0;
}
if ($o >=1){
$prev=$o-1;
} else{
$prev=0;
}
You need to add an isset() function to check if $_GET['o'] even exists.
Read more about isset() Here
use this:
<?php
$con=mysqli_connect("mysql.cms.gre.ac.uk","tz005","punceg5L","mdb_tz005");
if (isset($_GET['o'] && is_numeric($_GET['o']))
{
$o=$_GET['o'];
}else {
$o=0;
}
if ($o >=1){
$prev=$o-1;
} else{
$prev=0;
}

Categories