0

I'm getting an error:

Notice: Undefined variable: price

With this code:

<?php

$price[1] = 100;

$store[1] = "apple";

function check ($m) {

  if ($m == "apple") {

    $z = $price[1];

  }

  return $z;

}


?>

<?= check( $store[1] ) ?>

Instead of making $z to be equal to price[1], it tries to set it as the non existent price.

How to correctly define it?

rockyraw
  • 1,125
  • 2
  • 15
  • 36
  • 2
    `$price` is undefined in the scope of your function. – Don't Panic Jan 21 '16 at 23:09
  • So what should I do if I want the function to acknowledge it? I don't want to define it inside the function. I'm defining a big set of prices at the start of my code, from a database. – rockyraw Jan 21 '16 at 23:11
  • What would be useful is to see if your arrays are going to contain more values. The way you are attempting to do this at the moment is *probably* the hard way, especially if `$price[2]` will contain a price for the product in `$store[2]`, etc. – random_user_name Jan 21 '16 at 23:29

4 Answers4

2

$price is undefined in the scope of your function. If you need your function to have access to that variable, there are a few ways to do it. The most straightforward way is to add $price as an another parameter to your function.

function check ($m, $price) { ...

And then use your $price array as the second argument when you call check():

<?= check( $store[1], $price) ?>

Keep in mind that the $price inside your function is not the same variable that exists outside the function, but a copy of it.

You can learn more about variable scope in the PHP documentation here. It would be best to avoid using global unless it is totally necessary for some reason. In this case, it should not be necessary.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • I see. The problem is that in the real case, I want to echo one price, out of several possible prices (`$a_price[1]`,`$b_price[1]`,`$c_price[1]`), which shall be chosen according to the store. So this means I need to create a function with 4 variables? a little messy :/ – rockyraw Jan 21 '16 at 23:20
  • It seems to me like that probably shouldn't be necessary, but it's difficult to tell without knowing quite a bit more about your database setup and how you're getting the prices for items in each store. – Don't Panic Jan 21 '16 at 23:22
1

You're asking about a specific issue - which is simple to solve - but really you are trying to accomplish something else that would be far simpler to solve than the code sample you provided.

Given your code, plus some "assumptions" of where you might be going, I would recommend something more like so:

$price[1] = 100;
$price[2] = 150;
$price[5] = 225;

$store[1] = "apple";
$store[2] = "orange";
$store[5] = "kiwi";

// Option 1: Global in the variables.  Nothing wrong with it here...
function check ( $m ) {
   global $price, $store;

   $index = array_search( $m, $store );

   return ( isset( $price[ $index ] ) ) ? $price[ $index ] : 0;
}

// usage:
echo check( $store[1] );



// Option 2: Pass in all the variables you need
function check ( $m, $price, $store ) {

   $index = array_search( $m, $store );

   return ( isset( $price[ $index ] ) ) ? $price[ $index ] : 0;    
}

// usage:
echo check( $store[1], $price, $store );



// Option 3: Since you seem to know the index already, just pass THAT in
function check ( $index, $price, $store ) {
   return ( isset( $price[ $index ] ) ) ? $price[ $index ] : 0;    
}

// usage:
echo check( 1, $price, $store );



// Option 4: Globals, plus since you seem to know the index already, just pass THAT in
function check ( $index ) {
   global $store, $price;
   return ( isset( $price[ $index ] ) ) ? $price[ $index ] : 0;    
}


// usage:
echo check( 1 );
random_user_name
  • 25,694
  • 7
  • 76
  • 115
  • Good observation. I simply added that `global` line and it saved me a lot of work, as I do have multiple arrays. I have to choose this answer as it best addresses my real motivation. thanks! – rockyraw Jan 22 '16 at 00:25
0

Variables are not defined inside functions, you need to pass all variables that you need inside a function as arguments such as:

$price[1] = 100;
$store[1] = "apple";
function check ($m, $price) {
  if ($m == "apple") {
    $z = $price[1];
  }
  return $z;
}
//use the function
check( $store[1], $price )
Akam
  • 1,089
  • 16
  • 24
  • All of these answers neglect the *simply aspect* of the OP is trying to match *the product from an array to the a price from an array*, and there's probably a FAR simpler method to do this. – random_user_name Jan 21 '16 at 23:28
0

Try this:

function check ($m) {
  global $price;
  (...)
fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • using global is not recommended. – Akam Jan 21 '16 at 23:21
  • @Akam - why not? This is perfectly valid / legitimate method to make this work. – random_user_name Jan 21 '16 at 23:27
  • Hi, @cale_b. I don't know what Akam's reasoning is, so I don't intend to speak for him, and I do agree with you that this is one way to do it. I personally try to avoid `global` because it introduces the possibility of unintended side effects. It's harmless as it is being used in this example. But if the function is changed later it can easily inadvertently modify the main application data. – Don't Panic Jan 21 '16 at 23:45
  • @Don'tPanic - agreed. And since it's outside of scope to provide a "robust" method with classes, etc, we have to work with what we have! I tend to avoid global as well, but see it in quality, production code, and therefore feel it can be used in some cases. – random_user_name Jan 21 '16 at 23:59