views:

118

answers:

4

I have an issue with php where code works on on computer but wont work on another

    function appendParam(&$req, $name, $value) {
 if (req == null) {
  return;
 }
 if (name == null) {
  return;
 }
 if (value != null) {
  $req[$name] = $value;
 }
}

The above works on one computer and is capable of checking req and name against null properly and the variables is the if condition don't need dollar signs (when i put the dollar signs in they break on this computer)

but i need to use the following code on another computer to get the same end result

    function appendParam(&$req, $name, $value) {
    if ($value != null) {
  $req[$name] = $value;
 } 
    if ($name == null) {
  return;
 }
    if ($req == null) {
  return;
 }
}

on this other computer it isn't capable of checking name or req against null (it breaks) and i need the dollar signs on the variables in the if condition.

As a side note it also seems that this computer can't read from an array index that isn't already initialized.

Any help is appreciated

A: 

Could it be one of the computers is using an older version of PHP?

Zack
I thought that it could be the reason but after checking, they are both using the same version
ChronoXIII
+3  A: 

The 2 machines might have different warning levels or error reporting levels.

I'm confused, surely you should have the $ prefix before any php variable name otherwise you're not actually checking on the variable? What are you trying to achieve by not using the $ prefix

David Archer
i want to use the dollar prefix, but it stops on the first line that has a variable with a dollar sign in the if condition
ChronoXIII
whats your calling code look like. and like @daniel said,can you give some examples and error messages
David Archer
I believe you're right. Without the $/sigil, PHP think's the identifier is a constant, but if no such constant exists, it can "fall back" to the variable that would've been referred to if the "$" was there. However, this generates some warning/notice level (forget the exact level) and may not always work as expected...
Peter
So from my understanding with out the dollar sign it uses a variable if a constant isn't available but even if its throwing a warning its not stopping the code since this is actually working. It passes req and name check.
ChronoXIII
SOLUTION BY david archer: use empty function to check nulls instead of directly checking with equal signs.
ChronoXIII
+2  A: 

This code makes very little sense to me:

    function appendParam(&$req, $name, $value) {
    if ($value != null) {
                $req[$name] = $value;
        }       
    if ($name == null) {
                return;
        }
    if ($req == null) {
                return;
        }
}

It should be written like so:

function appendParam(&$req, $name, $value)
{
 if(empty($name) || empty($value) || empty($req))
  return;

 $req[$name] = $value;
}

When you say it "breaks" can you give a specific example/error you're getting and how you're using the function? I'm thinking you're using it incorrectly.

Daniel
Your code won't always have the same effect as his. In his case if value is not null, then req can be set. Your will bail if req is null even if value is set
David Archer
i understand that i should check name and req first, but it won't work (with or without the dollar sign). Is there an actual difference between checking the value against null and calling it in the empty function?The error is it doesn't get past the name check if it ever gets there, just stops. It also doesn't seem to write out to the log when this occurs.
ChronoXIII
yes there is a difference, see http://php.net/manual/en/function.empty.php
David Archer
@chronoXiii, see @peter's comment to my answer. You should always use the $, your problem is something else
David Archer
@davidarcher, your empty function over checking variables against null works, so now i can actually check name and req on both computers. Now my only issue seems to be the dollar signs.THX
ChronoXIII
well, it was @daniels answer to do that (+1 to him). Now I suggest you experiment. try "echo"ing e.g $req, $value, $name (with the $) as you enter the function. See what you get
David Archer
I've tried that, they contain a value inside the function
ChronoXIII
Can you paste some sample input to the question. The logic is slightly different between your two functions
David Archer
I forgot i could just do !($empty ($value)), :Pnow I'm capable of running the same code on both computersthx for everyone's help
ChronoXIII
How do i give david archer proper credit? Do i just comment the solution in his answer and then checkmark?
ChronoXIII
!($empty ($value)) ? do you mean !(empty($value)) ?
David Archer
oh right sorry typo on the comment :P, thx againits !(empty($value))
ChronoXIII
+1  A: 
AlberT
In addition to my post and referring @Daniel too, it should be noticed that thefunction proposed is better than the starting one, but it is still having a bad argument checking policy.I mean that silently returning null if a required argument is empty is not a good check and, above all, is is not a good way to make a possible wrong function call to be aware of the error.Take into account the idea of using assertions instead, custom errors by mean of trigger_error(), or a more complex error handling mechanism such as exceptions ...My 2 cents :)
AlberT