comparing Session-Value PHProblem - php

if ($_SESSION['user_email']!=$_SESSION['ship_user_email'])
{
$to= $_SESSION['ship_user_email'];
mail($to,$subject,$mail_html,$headers);
}
there is problem in comparing values of both session. value is different but code not working.

print the values of both the session variables and check if there are leading or trailing spaces in the them.
Also check the condition with the '!==' as said by Kerry.
like this
if ( $_SESSION['user_email'] !== $_SESSION['ship_user_email'] )
{
$to= $_SESSION['ship_user_email'];
mail($to,$subject,$mail_html,$headers);
}
also check every step of your code by writing an echo statement and printing out the values of variables. This will help you debug and understand the code properly.

Try using the "!==" operator instead.

you can use <> this operator.

Related

How to match a variable with value in double array

i have a problem like the following sample code :
$code='100'; //$maybe $code='0' or $code='1' ... i just set a number as an sample
$xx=array(
'0'=>array(a,b,c),
'1'=>array(d,e,f),
........
'100'=>array(aa,bb,cc)
);
I want find $code in array :
if($code==$xx['$code']){
echo $xx['code'][0]; //if i want get the value 'aa'
}
But it seems like $xx['$code'] doesn't work.
Do anyone know the right way to solve it?
Firstly you need to use array_key_exists for getting within if condition and then after you can use it simply like as
if(array_key_exists($code,$xx)){
echo $xx[$code][0];
}
or can simply use isset instead like as
if(isset($xx[$code])){
echo $xx[$code][0];
}
you shouldn't use single quotations here,
when filling displaying a variable either use without quotations or put it between double "" try $xx[$code] or $xx["$code"]
Use isset OR !empty to check key exist in array or not. It will also check that key has valid value.
if(isset($xx[$code])){
echo $xx[$code][0];
}
OR
if(!empty($xx[$code])){
echo $xx[$code][0];
}
if($code==$xx['$code']){
echo $xx[$code][0]; //if i want get the value 'aa'
}
If you use '$code' the content of $code will not be checked as '' interpret everything as a string and won't look a variables in it.
You can't sace 'code' either, as code is only the name of the variable you use.

Setting a value from a selection with get

I just need help with a few things. Firstly how would I get the data of a selection when posted.Like
$selection=$_GET['area'];
if ($selection="time"){
echo "Not working yet please come back at a later update.";
}
and also on that note, why is it displaying it even when there is no data sent over the form. I thank any and all help.
It must be == operator you have used = operator which is the assiging operator.You have to use == or === operator for comparisons.For more check comparison operators in php
$selection = trim($_GET['area']);
if ($selection == "time"){
echo "Not working yet please come back at a later update.";
}
The operator "=" is not proper.
You should use "===" to check equality.
<?php
$selection=$_GET['area'];
if ($selection === 'time'){
echo 'Not working yet please come back at a later update.';
}
?>
Always use == or === operators to check conditions
$selection=$_GET['area'];
if ($selection=="time"){
echo "Not working yet please come back at a later update.";
}
you forgot one = your code should be
$selection = $_GET['area'];
if ( $selection == 'time' ) {
echo 'Now working yet please comde back at a later update';
}
using =you are assigning 'time' to $selection which always returnes true
use == instead.
it should be like this.
if ($selection=="time"){
$selection="time"
this assigns $selection variable a value "time".
You should use
if ($selection=="time")
instead of
if ($selection="time")

Php test empty string

I have a bit of php code that I'm not understanding why it is acting as it is. I have a variable called contactId that I want to test to see if it is empty. However even if it is empty it evaluates to true. Code is below. Thanks in advance.
print "*".$contactId."*<br/>";
if($contactId != '')
{
//queryContact($contactId);
print "Contact Present<br/>";
}
result returned to screen is:
**
Contact Present
If you want to see exactly what your string is, simply use var_dump(), like this, for instance:
var_dump($contactId)
instead of
print "*".$contactId."*<br/>";
Couple of things you can try:
if (!empty($contactId)) {
// I have a contact Id
}
// Or
if (strlen($contactId) > 0) {
// I have a contact id
}
In my experience I have often used the latter of the two solutions because there have been instances where I would expect a variable to have the value of 0, which is valid in some contexts. For example, if I have a drink search site and want to indicate if an ingredient is non-alcoholic I would assign it a value of 0 (i.e. IngredientId = 7, Alcoholic = 0).
Do it with if (isset($contactId)) {}.
You likely want:
if (strlen($contactId))
You'll want to learn the difference between '' and null, and between == and ===. See here: http://php.net/manual/en/language.operators.comparison.php
and here: http://us3.php.net/manual/en/language.types.null.php
In future, use if(!empty($str)) { echo "string is not empty"}.

PHP GET Question: How do I set a variable value only if it's not in the query string?

I know how to get the value from the query string if the parameter exists:
$hop = $_GET['hop'];
But I also need to set a default value IF it's not in the query string. I tried this and it didn't work:
$hop = $_GET['hop'];
if ($hop = " ") {
$hop = 'hardvalue';
};
Please help me handle the case where the query string has and does not have the "hop" parameter, and if it's present but not defined:
example.com/?hop=xyz
&
example.com/
&
example.com/?hop=
PS I don't know what I'm doing, so if you explain to me, please also include the exact code for me to add to my PHP page.
use array_key_exists
if (array_key_exists('hop', $_GET))
{
// the key hop was passed on the query string.
// NOTE it still can be empty if it was passed as ?hop=&nextParam=1
}
else
{
//the key hop was not passed on the query string.
}
Thought about it a bit more and decided it should be a bit more robust:
$hop = 'hardvalue';
if (array_key_exists('hop', $_GET)) {
if (!empty($_GET['hop'])) { $hop = $_GET['hop']; }
}
You already got the fiddly solutions. When working with URL or form parameters, you often want to treat the empty string or zeros as absent values too. Then you can use this alternative syntax:
$hop = $_GET["hop"] or $hop = "hardvalue";
It works because of the higher precedence of = over or, and is easier to read with extra spaces.
Starting from PHP 5.3 it's also possible to use:
$hop = $_GET["hop"] ?: "hardvalue";
The advantage here is that this syntax doesn't slurp up php notices, which are useful for debugging.
Actually, I would use
$hop = !empty($_GET['hop']) ? $_GET['hop'] : 'default';
Using empty() instead of isset() takes care of your third scenario, where the parameter is present but not defined.
Also, in if ($hop = " ") the = would need to be changed to ==. = assigns, == tests equality. The way you have it, the if-statement will always run, no matter what $hop equaled.

Why do I need the isset() function in php?

I am trying to understand the difference between this:
if (isset($_POST['Submit'])) {
//do something
}
and
if ($_POST['Submit']) {
//do something
}
It seems to me that if the $_POST['Submit'] variable is true, then it is set. Why would I need the isset() function in this case?
Because
$a = array("x" => "0");
if ($a["x"])
echo "This branch is not executed";
if (isset($a["x"]))
echo "But this will";
(See also http://hk.php.net/manual/en/function.isset.php and http://hk.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting)
isset will return TRUE if it exists and is not NULL otherwise it is FALSE.
You basically want to check if the $_POST[] variable has been submitted at all, regardless of value. If you do not use isset(), certain submissions like submit=0 will fail.
In your 2nd example, PHP will issue a notice (on E_NOTICE or stricter) if that key is not set for $_POST.
Also see this question on Stack Overflow.
The code
if($_POST['Submit'])
{
//some code
}
will not work in WAMP (works on xampp)
on WAMP you will have to use
if (isset($_POST['Submit'])) {
//do something
}
try it. :)
if user do not enter a value so $_post[] return NULL that we say in the description of isset:"
isset will return TRUE if it exists and is not NULL otherwise it is FALSE.,but in here isset return the true
"

Categories