mardi 1 août 2017

PHP - Variable returns NULL when setting in if statement

I'm somewhat of an amateur php programmer and am looking for help with an if statement that is not working as I intend it to.

<?php

var_dump($_GET['name']);
var_dump($_GET['id']);
var_dump($search);
var_dump($param);
var_dump($raw);
var_dump($json);

// for debugging
error_reporting(E_ALL);
ini_set('display_errors', 'on');

// choose between either the name or id parameter
if(isset($_GET['name'])) {
    global $search;
    $search = $_GET['name'];
}
elseif(isset($_GET['id'])) {
    global $search;
    $search = $_GET['id'];
}

// build parameters for either name OR id
if (isset($_GET['name'])) {
    $param = http_build_query(array(
        'name' => $_GET['name'],
        'getMembers' => 'yes',
        'rand' => rand(),
    ));
}
elseif (isset($_GET['id'])) {
    $param = http_build_query(array(
        'id' => $_GET['id'],
        'getMembers' => 'yes',
        'rand' => rand(),
    ));
}

// get raw json from server
$raw = file_get_contents("http://ift.tt/2uTLn6n?".$param);

// decode the raw json response
$json = json_decode($raw);

... ?>

Notice how I've dumped the vars on the third line for debugging purposes. $search, $param, $raw, and $json all return NULL every time. I'm thinking it has something to do with the if statements, but I can't figure out what for the life of me. Help would be much appreciated. Cheers!

Aucun commentaire:

Enregistrer un commentaire