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:
which one is faster? And the code looks correct (besides me needing to remove the dots in the outbound link) ?
Read the user-contributed notes for each function and decide which is a better fit for how you think. http://www.php.net/is_null http://www.php.net/empty
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: array_key_exists() to make sure the parameter you're looking for was passed at all If it was passed, use strlen() to make sure it has length.
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:
<?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:
Thanks. Some of that brings me back a couple of years to the matlab coding that I had to do for my engineering classes.