0

Why does this code give a Undefined variable : $category_id

// declare a variable and an array
$category_id;
$posts = [];

// get four categories
$categories = Category::take(4)->get();

foreach($categories as $category){
  //foreach of the four categories, get the category id
  $category_id = $category->$category_id;

  //add three posts of each of those categories to the $posts array
  $posts[] = Category::where('category_id', $category_id)->take(3)->get();

}

Althought I defined the variable on the top as $category_id;. why does the error persist?
Is it something to do with the $category->$category_id skewing the code?

4
  • Yes... $category_id is basically null .. has no value. Commented Dec 10, 2015 at 5:49
  • the $category->$category_id; has a value, but the error is on the " $category_id = " s $category_id. Why is that? Commented Dec 10, 2015 at 5:58
  • On which line are you getting the above error !!, things seem to fine in the code Commented Dec 10, 2015 at 6:01
  • $category_id = $category->$category_id; this line!! Commented Dec 10, 2015 at 6:02

3 Answers 3

3
$category_id = $category->$category_id;

is most likely wrong, you probably mean

$category_id = $category->category_id;

and you should (again: most likely) initialize the variable by assigning a literal value or constant or similar, e.g.

// declare a variable and an array
$category_id=null;

(edit: or leave that initialisation out completely. In the code snippet you've posted, $category_id is used only when it obviously has been assigned a value within the loop and it looks like you're not preserving the variable for use after the foreach loop.)

Sign up to request clarification or add additional context in comments.

Comments

0

I never declare a variable without anything following like yours above before.

what about:

$category_id = null;

4 Comments

i want an explanation why this is happening.
I m not sure if it is legal to declare a variable by just write its name, nothing else?
"$category_id = null;" - But then again what would $category->{null} be?
now i see the problem. you need to have initial value for the first id: $category_id = $category->$category_id; why the second has $, is it a typo?
0
<form method="post" action="convert1.php">
hour  :  <input type="hour" name="fhour"><br><br>
minute:<input type="minute" name="fminute"><br><br>
second: <input type="second" name="fsecond"><br><br>
month:<input type="text" name="fmonth"><br><br>
day:<input type="day" name="fday"><br><br>
year : <input type="text" name="fyear"><br><br>
date :  <input type="date" name="fdate"><br><br>

<input type="submit" name="submit" value="submit" >
</form>


<?php
if(isset($_POST['submit']))
    //print_r($_POST);
{



    $hour=$_REQUEST['fhour'];
    $minute=$_REQUEST['fminute'];
    $second=$_REQUEST['fsecond'];
    $month=$_REQUEST['fmonth'];
    $day=$_REQUEST['fday'];
    $year=$_REQUEST['fyear'];
    $date=$_REQUEST['fdate'];

    $timestamp = mktime($fhour, $minute, $second, $month, $day, $year,$date);
    //date('r', $timestamp);
    echo 'timestamp in second='.$timestamp;
    echo $minute=$timestamp/60;
    echo '<br>';
    echo $second=$timestamp/24;
    echo'<br>';
    echo $milisecond=$timestamp*100;
    echo '<br>';

}   
?>
please solution

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.