PHP array returns null

Active3 hr before
Viewed126 times

6 Answers


..where $val is returning NULL. Now I know there is data in $dateMap and $keys from doing a var_dump() on them... ,Asking for help, clarification, or responding to other answers.,Please be sure to answer the question. Provide details and share your research!,To learn more, see our tips on writing great answers.

See this part:

array(21) {
      ["01"] => array(5) {
            ["01-SEP-11"] => string(40)...

Look at the key - it's "01" in quotes. That means it's a string, so you would have to do


to get at that key. If you're passing in

load more v

Casting a variable to null using (unset) $var will not remove the variable or unset its value. It will only return a null value. , it has not been set to any value yet. , it has not been set to any value yet. , The special null value represents a variable with no value. null is the only possible value of type null.

load more v

Note: by using the word global variable i mean that it is usable inside a particular controller only. Should be accessible by all its functions

public $question = array(); // my global variable for this controller only

function actionCreate()


   $db = new ExamMaster();

   if (isset($_POST['ExamMaster'])) {

      //selecting em_id from exam_master table

      $db - > attributes = $_POST['ExamMaster'];

      $crs = $_POST['ExamMaster']['course_id'];

      $sub = $_POST['ExamMaster']['subject_id'];

      $model = QuestionBankMaster::model() - > findAll(array(

         'select' => array('question_id,question_entered', 'RAND()as rand'), //RAND() is used for random selection

         'condition' => 'course_id = :cid AND subject_id = :sid',

         'params' => array(':cid' => $crs, ':sid' => $sub),

         'order' => 'rand', // we have to use order by RAND()

         'limit' => 3,


      $i = 0;

      foreach($model as $m)


         $object = new ExamQuestionMaster();

         $object - > em_id = $examID;

         $object - > question_id = $m['question_id'];

         $object - > save();

         $this - > question[$i] = $m['question_entered'];



      // return $this->question;

      $this - > redirect(array('displayquestion', 'crs' => $crs, 'sub' => $sub));

   } //if closed

   $this - > render('create', array('db' => $db, 'courseList' => $courseList, 'subjectList' => $subjectList, 'examList' => $examList));


I am redirecting to the following function:—

function actionDisplayquestion($crs, $sub)


   //selecting the subject name from subject table and course name from course table

   $sname = Subject::model() - > find(array(

      'select' => 'subject_name',

      'condition' => 'subject_id = :sid',

      'params' => array(':sid' => $sub),


   $cname = Course::model() - > find(array(

      'select' => 'course_name',

      'condition' => 'course_id = :cid',

      'params' => array(':cid' => $crs),


   echo var_dump($this - > $question); // It returns null???

   $this - > render('displayquestion', array('course' => $cname, 'subject' => $sname));

And here is the view of Displayquestion


echo CHtml::link('Home',array('Site/Index')) . '<br/>';

echo 'Subject: '. $subject['subject_name'];

echo '</br>';

echo 'Course: '. $course['course_name'];

echo '</br>';

echo '<table class="myqustion">';


//echo $limit;

for($i=0; $i<$limit; $i++)


  echo '<tr><td>';

  echo '<b>Question.'. $i+1 .'</b>' .$this->question[$i];

  echo '</td></tr>';

  echo '<tr><td>';

  echo '</br>';

  echo '<hr>'; 

  echo '</br>';



echo '</table>';


load more v

This drove me crazy for hours now... Please tell me why the $ppk_fields array is empty in the ppk_field_callback function? Note that it returns the correct values in the ppk_default_id function but that is not a callback function. Thank you!, @TomJNowell inside the ppk_field_callback function, no where else. Why is it even when it's set as global i can't access the array values? – mrKC.988 Nov 15 '17 at 1:28 ,But, the global variable $ppk_fields is undefined, nobody defined it. You have to declare the variable as a global every time you use it, and that includes creation and destruction. No exceptions.,Connect and share knowledge within a single location that is structured and easy to search.

The problem here is that you never created $ppk_fields as a global variable.

$ppk_fields = [
   'field 1' => [
      'title' => 'Fieald One',
      'type' => 'text',
      'section' => 'section_1',
      'default' => 'default'
load more v

with empty(), your code will also return null if $array[$key] exists but contains an empty-like value, like false, 0, empty array and such,Making statements based on opinion; back them up with references or personal experience., Reconnecting with a previous professor then asking right away for a reference letter ,Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up.

For the question title, there indeed a null coalescing operator introduced in PHP7:

return $array[$key] ?? null;
load more v

But in cases like the sample I posted, psalm should totally not assume current() to never return null. The parent ArrayIteratror::current() also naturally returns null when it goes over the 0th element of the sample array.,IIRC, current() will return null too when next() is called but valid() returns false (for example, when the iterator don't have anymore elements),I would not expect the NullableReturnStatement error: According to the PHP docs, Iterator::current() can return mixed which IMHO includes null - especially considering the sample code where parent::current() of ArrayIterator would definitely return null in this sample snippet.,so making Iterator::current() nullable in general is bad too because then every call-site (including the ones using foreach) would need to check for a bogus null value.

< ? php

class FooIterator extends ArrayIterator {
   function current() {
      $p = parent::current();
      return $p === 'foo' ? null : $p;

$fi = new FooIterator([null, 1, 'foo']);
print_r(iterator_to_array($fi, true));
load more v

Other "returns-array" queries related to "PHP array returns null"