Variable not exist php

How to avoid undefined variable in php when variable does not exist?

Question: I want to get variable rating_idex in my php file so if is user click button #add-review it should pass in ajax variable and it will get array in php file and send review to the database, but it is not working and I don’t see solution This is my php code when I can get the variable and send them to the database: When I am trying to echo ($rating_index) it give me feedback that Variable does not exist so it is something with ajax but can’t find solution, thanks in advance for any solutions Solution 1: Instead of try reason being you didn’t actually declared Solution 2: if I’m not wrong you want to pass the PHP variable in javascript? I am out of ideas on how to gracefully output something using a short function instead of typing this every time: How can I check if a reference exists or not using php? Solution 1: Prepend the variable/parameter being tested with the error control operator «@».

Читайте также:  Html css box with title

How to avoid undefined variable in php when variable does not exist?

I only touch php on the very basic stuff.

But I always like to avoid errors/notices when making wordpress themes.

I have a simple function below that I made to list my taxonomy terms.

As you can see I have a $active variable.

This active variable is undefined when the my get_queried_object()->slug does not match $term->slug

How can I avoid my active variable from being undefined. So it is defined but empty.

They only way my brain could work it out is by doing this.

$active = null; if (get_queried_object()->slug == $term->slug) $active = 'class="active"'; 
if (get_queried_object()->slug == $term->slug) < $active = 'class="active"'; >else

Is this the most efficient way of doing this or is there an alternative php method?

There is no alternative php method, but for readability you should not declare/init your variables in a if block, e.g. :

You can also use ternary operator (but not really readable) :

$active = (get_queried_object()->slug == $term->slug) ? 'class="active"' : ''; 

The 2nd one will be the more efficient way :

if (get_queried_object()->slug == $term->slug) < $active = 'class="active"'; >else

The second approach is more common to see in my experience. You could of course shorten this using a ternary operator, similar to:

$active = (get_queried_object()->slug == $term->slug) ? 'class="active"' : ''; // if ^ ^ do this ^ else do this 

Some consider this to be more confusing though. I guess that boils down to personal preference.

You could also use the ternary operator as it’s a bit shorter:

$active = ( get_queried_object()->slug == $term->slug ? 'class="active"' : '' ); 

Php if variable not exists Code Example, “php if variable not exists” Code Answer if exist php php by TC5550 on May 25 2020 Comment 3 xxxxxxxxxx 1 if (isset($var)) < 2 // Code here 3 >Add a Grepper Answer Answers related to “php if variable not exists” php function exists php check if function exists if exists in string php php check if variable is true or …

How do I conditionally output variables that do not exist in php?

So I have a variable and a function that doesn’t work to demonstrate my thought process.

 $variable = "This is a string!"; function is($var) < if( isset ( $var ) ) return true; return false; >if( is ( $variable ) ) echo "This variable exists!"; else echo "This variable does not exist!"; 

Because $variable exists, the function will work correctly but there is an issue when $variable has not been set or defined.

 function is($var) < if( isset ( $var ) ) return true; return false; >if( is ( $variable ) ) echo "This variable exists!"; else echo "This variable does not exist!"; 
 Notice: Undefined variable: variable on line x This variable does not exist! 

Because the variable is not defined yet, when it is being referenced PHP will return a notice saying this variable you attempted to reference is undefined. This is an issue because the function always creates a notice when the variable is not properly set, which is was trying to avoid in the first place.

So I tried passing the variable name as a string without the reference.

 $variable = "This is a string"; function is($string) < if(isset($$string)) // Variable Variables return $$string; return ""; >echo is("variable"); 

But this still did not work. I am out of ideas on how to gracefully output something using a short function instead of typing this every time:

 echo (isset($variable) ? $variable : ""); 

How can I check if a reference exists or not using php?

Prepend the variable/parameter being tested with the error control operator «@». This suppresses any errors in the expression — such as the variable not existing:

you just need to add @ to your variable to avoid showing up errors

you can change your code like this:

$variable = "This is a string!"; function is($var) < if( isset ( $var ) ) return true; return false; >if( is ( @$variable ) ) echo "This variable exists!"; else echo "This variable does not exist!"; 

you can find that i’ve changed if( is ( $variable ) ) to this if( is ( @$variable ) )

You need to check isset($variable) before calling the function with the var. Maybe not as convenient as you want, but that’s how it works.

Passing js variable to php using ajax does not work

I want to get variable rating_idex in my php file so if is user click button #add-review it should pass in ajax variable and it will get array in php file and send review to the database, but it is not working and I don’t see solution

$('#add-review').click(function() < var user_name = $('#reviewer-name').val(); var user_review = $('#review').val(); console.log(user_name); console.log(rating_index); console.log(user_review); if(user_name == '' || user_review == '') < alert("Please Fill Both Field"); return false; >else < $.ajax(< url:"rating-data.php", method:"GET", data:< rating_index: rating_index, user_name: user_name, user_review: user_review >, success:function(data) < $('#review_modal').modal('hide'); load_rating_data(); console.log(data); >>) > >); 

This is my php code when I can get the variable and send them to the database:

 $_GET["user_name"], ':user_rating' => $_GET["rating_index"], ':user_review' => $_GET["user_review"], ':datetime' => time() ); $query = " INSERT INTO review_table (user_name, user_rating, user_review, datetime) VALUES (:user_name, :user_rating, :user_review, :datetime) "; $query_run = mysqli_query($conn, $query); if($query_run) < echo "Your Review & Rating Successfully Submitted"; >else < echo ' '; echo mysqli_error($conn); > > ?> 

When I am trying to echo ($rating_index) it give me feedback that Variable does not exist so it is something with ajax but can’t find solution, thanks in advance for any solutions

Instead of echo ($rating_index); try echo ($_GET[«rating_index»]); reason being you didn’t actually declared $rating_index

if I’m not wrong you want to pass the PHP variable in javascript? if yes you cant pass the PHP variable in js like this.

you can pass your PHP variable like this but in only the .php file not in the .js

Check if a variable is set in PHP, After 10 years of PHP programming, just now I found out that isset() is not good at all, according to PHP isset() manual, isset() returns false when variable ‘is set’ and has a null value. Well, I was working with an array and I fixed this by array_key_exists() , just after about 2 hours of reading and testing codes.

Источник

Check whether a variable exists and not empty in PHP

These functions are useful when you want to perform a data validation whether in PHP or WordPress.

isset()

isset() determines if a variable exists.

Things to remember

  • It actually checks if the declared variable is declared and is everything else but null.
  • isset will return false if you check against a variable with null value.
    • A null character (“\0”) is not same as the PHP null constant.

    Example

     $a = "test"; $b = "anothertest"; var_dump(isset($a)); // TRUE var_dump(isset($a, $b)); // TRUE unset ($a); var_dump(isset($a)); // FALSE var_dump(isset($a, $b)); // FALSE $foo = NULL; var_dump(isset($foo)); // FALSE 

    isset() for elements in arrays

     $a = array ('test' => 1, 'hello' => NULL, 'pie' => array('a' => 'apple')); var_dump(isset($a['test'])); // TRUE var_dump(isset($a['foo'])); // FALSE var_dump(isset($a['hello'])); // FALSE // Checking deeper array values var_dump(isset($a['pie']['a'])); // TRUE var_dump(isset($a['pie']['b'])); // FALSE 

    empty()

    empty() function checks whether a variable is empty.

    Things to remember

    • It will return true if the variable doesn’t exist or the value is equal to false.
    • If empty() returns true, it doesn’t always mean that the variable doesn’t exist.

    Example

     $var = 0; // Evaluates to true because $var is empty if (empty($var))

    If you evaluate the variable above with isset(), it’ll return true.

     $var = 0; // Evaluates as true because $var is set if (isset($var))

    Источник

    PHP isset() Function

    Check whether a variable is empty. Also check whether the variable is set/declared:

    $a = 0;
    // True because $a is set
    if (isset($a)) echo «Variable ‘a’ is set.
    «;
    >

    $b = null;
    // False because $b is NULL
    if (isset($b)) echo «Variable ‘b’ is set.»;
    >
    ?>

    Definition and Usage

    The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL.

    This function returns true if the variable exists and is not NULL, otherwise it returns false.

    Note: If multiple variables are supplied, then this function will return true only if all of the variables are set.

    Tip: A variable can be unset with the unset() function.

    Syntax

    Parameter Values

    Parameter Description
    variable Required. Specifies the variable to check
    . Optional. Another variable to check

    Technical Details

    Return Value: TRUE if variable exists and is not NULL, FALSE otherwise
    Return Type: Boolean
    PHP Version: 4.0+
    PHP Changelog: PHP 5.4: Non-numeric offsets of strings now returns FALSE

    ❮ PHP Variable Handling Reference

    Источник

Оцените статью