Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
If we need to check whether the variable is set or not, we can easily check it by function isset()
But what about is_zero?
But
isset($myvar)
doesn't accomplish my task. If we use isset($myvar) then PHP would return false always and If I use
if(empty($myvar))
then I get Notice:
Notice: Undefined index: $myvar
I thought it must be there in PHP, because sometimes we need to store the zero value variables also.
I tried this:
if(is_zero($myvar))
echo $myvar;
But got this error:
Fatal error: Call to undefined function is_zero()
Maybe a bug in PHP ?
is_zero is not a PHP function, you can write your own:
function is_zero($input)
{
if($input === 0)
return true;
else
return false;
}
if there is a possibility that $input is not defined earlier, you can add & in front of $input or # in front of is_zero to hide a possible notice error.
function is_zero(&$input)
{
if($input === 0)
return true;
else
return false;
}
or
if(#is_zero($variable))
You can easily check if a variable is zero by adding a exclamation mark in front of the variable like this:
if(!$myVar)
{
}
empty() will not show you Notice: Undefined index: $myvar.
If the variable is not set or if its value is zero , it returns 1
so you should have got error while printing it
echo $myvar
If the variable is not set you cannot print it
so for checking is set and is zero use this
if(empty($myvar)){
if(isset($myvar))
echo $myvar;
else
echo "variable not set";
}
Check empty() function: It checks whether the value is 0 also.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a php function that returns true or false and works fine when I say:
if (is_valid_locale($locale)) {
// do something
}
Yet, when I am trying to find something that does not return a valid locale with this:
if (!is_valid_locale($locale)) {
// do something else
}
When I echo out the function, it does spit out "f" for false. So the function works.
I tried other things such as:
if (is_valid_locale($locale)==false) {
// do something else
}
However it still did not catch.
Am I doing something wrong?
If the function returns f, it doesn't return a boolean value.
As per convention, a function call is_something() should return a boolean value, so you should fix the function is_valid_locale() to return a boolean instead of trying to cope with what it's returning.
As soon as you've fixed that, your code should work as expected.
I think you should use === ,try this:
if (is_valid_locale($locale)===false) {
// do something else
}
Or if when you echo out the function, it does spit out "f" for false. Then try this:
if (is_valid_locale($locale)==='f') {
// do something else
}
If echoing the functions result gives you 'f' instead of nothing, you should change it to == 'f'.
Anyway, check what is the type of function result by using
gettype(is_valid_locale($locale))
In php False is equivalent to 0 not 'f'. Recheck you functions code or try.
if (is_valid_locale($locale)=='f') {
// do something else
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
this is my code and i am using in edit.php for editing my database but when i open this page directly in browser i am getting error Notice: Undefined index: OPRID
but now i have open page this path
Optr_Edit.php?OPRID=<?=$objResult["OPRID"];?>
its right yaa wrong... tell mee
<?
include ('connection.php');
$strSQL = "SELECT * FROM OPERATOR WHERE OPRID = '".$_GET["OPRID"]."' ";
$objParse = oci_parse ($ora_conn, $strSQL);
oci_execute ($objParse,OCI_DEFAULT);
$objResult = oci_fetch_array($objParse);
if(!$objResult)
{
echo "Not found OPRID=".$_GET["OPRID"];
} else {
....
?>
http://www.yourdomain.com/path/edit.php?OPRID=hello
Using this URL, $_GET["OPRID"] would contain "hello". If you don't pass anything in via the URL like that i.e. http://www.yourdomain.com/path/edit.php then $_GET["OPRID"] will not be set.
$GET contains the CGI parameters when the php module is called from another html form. If you just open the php file directly, there won't be any parameters unless you supply them by using mypage.php?OPRID=XX
GET parameters are parameters from the URL.
For example:
http://www.example.com?OPRID=test
You can now get the value "test" via the $_GET dictionary:
print $_GET["OPRID"];
The output will be
test
You have to call it like:
edit.php?OPRID=<your value here>
then the $_GET array looks like:
array('OPRID',<your value here>);
You can check if an index exists with
if(isset($_GET['OPRID']))
{
//DO STUFF
}
else
{
//ECHO: NO OPRID FOUND
}
Please make sure you escape the values from $_GET, because you an attacker could easily type in:
edit.php?OPRID=1"or"1"="1
or worse. Read more about that: SQL Injection
You can access your GET, POST, COOKIE SERVER and ENV arrays by utilising build in function filter_input()
php.net Link
Using this function you will not get an errors or notices logged and your variables will always have a value and be sanitized.
EDIT:
Instead of using
$_GET["OPRID"]
several times you can use
$oprid = filter_input(INPUT_GET, 'OPRID');
once in your script. After this line you have a variable $oprid with a defined value which will be null if the index OPRID in $_GET was empty not set.
Excerpt from php.net:
Return Values:
Value of the requested variable on success, FALSE if the filter fails, or NULL if the variable_name variable is not set. If the flag FILTER_NULL_ON_FAILURE is used, it returns FALSE if the variable is not set and NULL if the filter fails.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Ive asked a similar question before but i was really unclear so ive decided to use a more concrete example.
Does php save the result of the variable or does it save the procedure to run it? Why im wondering is if i store a function in it, does it store the return value or just copies the procedure
say:
function foo($something)
{
for loop
{
echo 'Something';
}
return $something;
}
$b = foo(5);
from what i encountered just assigning the value executes the function. Which i dont want because i dont want to go through double the for loops and do double what could be inside.
In PHP you can have both (either store result, or function's code)
if you write:
function foo()
{
return 5;
}
$a = foo();
this will mean - execute function foo and store result into $a
if you write:
$a = function()
{
return 5;
};
$a();
this will mean - store function's code into variable $a, then execute function stored in $a
PHP is a strict programming language, meaning that expressions are always completely evaluated. The line
$b = foo(5);
computes the value for foo(5) before the assignment; PHP does not leave it as a thunk to be evaluated when or if the variable $b is used.
If you want to you can achieve something similar to a thunk by creating a closure, like this:
$b = function() { return foo(5); };
This will not evaluate foo(5) until its value is needed, and then to get the value you must call the closure as $b().
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
function myCheck($in)
{ return isset($in); }
$var1='Something';
$var2='$var1';
$var3='$varNonExitant';
What I'm trying to achive is to use myCheck to evaluate the existance of the content like this:
myCheck($var2) return true;
myCheck($var3) return false;
isset() is not really a function: it's a language construct. As such, it's allowed to do some magic that's not available to regular functions, such as being fed with non-existing variables.
To sum up: you cannot replicate it with a custom function.
Edit:
As DaveRandom pointed out in a comment below, all you can do is come close by checking if a variable isset for example:
function variable_isset(&$variable = NULL) {
return isset($variable);
}
This approach offers two drawbacks though:
This works by passing the unset variable by reference, thus creating it when called. As it's NULL it is still not set.
It'll trigger an Undefined variable notice if the variable does not exist, ruining the whole concept of gracefully handling optional variables.
Most likely this should not be needed. So question remains why you can not use isset in the first place which would be much more needed to give you better guidance.
When you cyall myCheck($abc), with set $abc = 123, it gets myCheck(123). It isn't any valid argument for isset.
You have to give the function a string with variable name:
function isset_global($variable_name)
{
return isset($GLOBALS[$variable_name]);
}
Of course, I am also wondering, why to do this, but it answers the question as long as you check a global variable.
This question already has answers here:
Weird PHP error: 'Can't use function return value in write context'
(12 answers)
Closed 9 years ago.
This code:
if(!empty(trim($_POST['post']))){ }
return this error:
Fatal error: Can't use function return value in write context in ...
How can I resolve and avoid to do 2 checks ( trim and then empty ) ?
I want to check if POST is not only a blank space.
you cant use functions inside isset , empty statements. just assign the result of trim to a variable.
$r = trim($_POST['blop']);
if(!empty($r))....
edit: Prior to PHP 5.5
if (trim($_POST['post'])) {
Is functionally equivalent to what you appear to be trying to do. There's no need to call !empty
if (trim($_POST['post']) !== "") {
// this is the same
}
In the documentation it actually explains this problem specifically, then gives you an alternate solution. You can use
trim($name) == false.
In PHP, functions isset() and empty() are ment to test variables.
That means this
if(empty("someText")) { ... }
or this
if(isset(somefunction(args))) { ... }
doesn't make any sence, since result of a function is always defined, e.t.c.
These functions serve to tell wether a variable is defined or not, so argument must me a variable, or array with index, then it checks the index (works on objects too), like
if(!empty($_POST["mydata"])) {
//do something
} else {
echo "Wrong input";
}