Question about is_null

Discussion in 'PHP' started by ziplock122949, Apr 20, 2008.

  1. #1
    I was wondering if the is_null fuction can be used in the following code. If someone were to link out.php?sid=1 and no "id" was input for the following code, I would like the output for link2=.$link2[$id] to be the same as link1=.$link1[$sid]

    Out.php
    <?php
      $id = $_GET['id'];
      $sid = $_GET['sid'];
      $link1 = array(
        "1" => "http://www.example-affiliate.com",
        "2" => "http://www.test-affiliate.com",
        );
      $link2 = array(
        "1" => "http://www.example.com/deeplink.html",
        "2" => "http://www.test.com/deeplink.html",
    );
    
      header("Location: out2.php?link1=.$link1[$sid]&link2=.$link2[$id]");
      exit;
    ?> 
    PHP:
    if I were to replace the last 2 lines of code with the following code would it work?

      
      if ($id==is_null) {
        header("Location: out2.php?link1=.$link1[$sid]&link2=.$link1[$sid]");
      }
      else {
        header("Location: out2.php?link1=.$link1[$sid]&link2=.$link2[$id]");
      }
      exit;
    PHP:
     
    ziplock122949, Apr 20, 2008 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    I prefer empty myself.

    if(!empty($_GET['id']))
    {
       //do stuff
    }
    Code (markup):
     
    joebert, Apr 20, 2008 IP
  3. ziplock122949

    ziplock122949 Active Member

    Messages:
    159
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    which one is faster? And the code looks correct (besides me needing to remove the dots in the outbound link) ?
     
    ziplock122949, Apr 20, 2008 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    joebert, Apr 21, 2008 IP
  5. qurve

    qurve Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    is null is a function, it will return a boolean so comparing something to it's reference won't do what you're expecting

    
    $var = null;
    echo is_null($var) // true
    $var = '';
    echo is_null($var) // false
    
    Code (markup):
    In your case you might actually want to do two things:

    1. array_key_exists() to make sure the parameter you're looking for was passed at all
    2. If it was passed, use strlen() to make sure it has length.
     
    qurve, Apr 21, 2008 IP
  6. ziplock122949

    ziplock122949 Active Member

    Messages:
    159
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Ok, i put the following code in and it works

    <?php
      $id = $_GET['id'];
      $sid = $_GET['sid'];
      $link1 = array(
        "1" => "http://www.example-affiliate.com",
        "2" => "http://www.test-affiliate.com",
        );
      $link2 = array(
        "1" => "http://www.example.com/deeplink.html",
        "2" => "http://www.test.com/deeplink.html",
    );
    
    	if( empty($_GET['id'])) {
    		header("Location: out2.php?link1=$link1[$sid]&link2=$link1[$sid]");
    	}
      	else {
    		header("Location: out2.php?link1=$link1[$sid]&link2=$link2[$id]");
    	}
      exit;
    
    ?> 
    PHP:
    The more I learn about php the more I want to do. I figure that sometimes the out2.php page is not needed. From what i am gathering, ! is "not" so I tried the following code but it does nothing. When a link is input, it just outputs a blank page with the page source blank too. What have I done wrong?

    <?php
      $id = $_GET['id'];
      $sid = $_GET['sid'];
      $link1 = array(
        "1" => "http://www.example-affiliate.com",
        "2" => "http://www.test-affiliate.com",
        );
      $link2 = array(
        "1" => "http://www.example.com/deeplink.html",
        "2" => "http://www.test.com/deeplink.html",
    );
    	
    	if( empty($_GET['id']) && !empty($_GET['sid']) {
    		header("Location: outt2.php?link1=$link1[$sid]&link2=$link1[$sid]");
    	}  	elseif( !empty($_GET['id']) && !empty($_GET['sid']) {
    		header("Location: outt2.php?link1=$link1[$sid]&link2=$link2[$id]");
    	}	elseif( !empty($_GET['id']) && empty($_GET['sid']) {
    		header("Location: ".$link2[$id]);
    	}	else { 
    		header("Location: index.php");
    	}
      exit;
    
    
    ?> 
    PHP:
     
    ziplock122949, Apr 22, 2008 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    <?php
    	// Check if they exist, have values, and force them to be numbers
    	$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
    	$sid = empty($_GET['sid']) ? 0 : intval($_GET['sid']);
    	if(!$id && !$sid)
    	{// No sense in going any further if neither exist
    		header('Location: index.php');
    		exit;
    	}
    	
    	// List of valid indexes for $sid
    	$link1 = array(
    		1 => 'http://www.example-affiliate.com',
    		2 => 'http://www.test-affiliate.com',
    	);
    	// List of valid indexes for $id
    	$link2 = array(
    		1 => 'http://www.example.com/deeplink.html',
    		2 => 'http://www.test.com/deeplink.html',
    	);
    	
    	// Check if there's valid entries in the lists
    	$id = isset($link2[$id]) ? $link2[$id] : 0;
    	$sid = isset($link1[$sid]) ? $link1[$sid] : 0;
    	if(!$id && !$sid)
    	{// No sense in going any further if unknown numbers were given
    		header('Location: index.php');
    		exit;
    	}
    	
    	// Reuseable
    	$header = 'Location: outt2.php?link1=%s&link2=%s';
    	
    	// Where to ?
    	if(!$id && $sid)
    	{
    		$header = sprintf($header, $sid, $sid);
    	}
    	else if($id && !$sid)
    	{
    		$header = "Location: $id";
    	}
    	else
    	{
    		$header = sprintf($header, $sid, $id);
    	}
    	
    	// Home James !
    	header($header);
    ?>
    PHP:
     
    joebert, Apr 22, 2008 IP
  8. ziplock122949

    ziplock122949 Active Member

    Messages:
    159
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Thanks. Some of that brings me back a couple of years to the matlab coding that I had to do for my engineering classes.
     
    ziplock122949, Apr 23, 2008 IP