我怎样才能从函数内部获得价值?

时间:2021-10-07 06:51:32

Is it possible to get those values inside of function and use those outside of function here is my code:

是否有可能在函数内部获取这些值并使用函数之外的函数是我的代码:

<?php
function cart() {
  foreach($_SESSION as $name => $value){
    if ($value>0) {
      if (substr($name, 0, 5)=='cart_') {
        $id = substr($name, 5, (strlen($name)-5));
        $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));

        while ($get_row = mysql_fetch_assoc($get)) {
          $sub = $get_row['price']*$value;
          echo $get_row['name'].' x '.$value.' @ &pound;'.number_format($get_row['price'], 2).' = &pound;'.number_format($sub, 2).'<a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a><br />';
        }
      }      
      $total += $sub ;
    }
  }
}
?>

now my question is how can i get value of $total? i want to use that value out side fo function, i have 2 function i cart and 1 discount i tried return $total; (inside of function) for example

现在我的问题是如何才能获得$ total的价值?我想使用该值的功能,我有2个功能我购物车和1折扣我试过返回$ total; (例如,在函数内部)

$final = cart () - discount ();
echo $final;

its echo out buth function echo code which r inside of function dont do any mathmatical operation.

它的echo outh函数回声代码,函数内部不进行任何数学运算。

4 个解决方案

#1


3  

You need to "return" the value. See the entry in the PHP manual for this. Basically, return means "exit this function now". Optionally, you can also provide some data that the function can return.

您需要“返回”该值。请参阅PHP手册中的条目。基本上,返回意味着“立即退出此功能”。或者,您还可以提供函数可以返回的一些数据。

Just use the return statement:

只需使用return语句:

<?php
  function cart()
  {
      foreach ($_SESSION as $name => $value) {
          if ($value > 0) {
              if (substr($name, 0, 5) == 'cart_') {
                  $id = substr($name, 5, (strlen($name) - 5));
                  $get = mysql_query('SELECT id, name, price FROM products WHERE id=' . mysql_real_escape_string((int)$id));
                  while ($get_row = mysql_fetch_assoc($get)) {
                      $sub = $get_row['price'] * $value;
                      echo $get_row['name'] . ' x ' . $value . ' @ &pound;' . number_format($get_row['price'], 2) . ' = &pound;' . number_format($sub, 2) . '<a href="cart.php?remove=' . $id . '">[-]</a> <a href="cart.php?add=' . $id . '">[+]</a> <a href="cart.php?delete=' . $id . '">[Delete]</a><br />';
                  }
              }
              $total += $sub;
          }
      }

      return $total;
  }
?>

#2


0  

Write return $total; inside the function

写回报$ total;在功能里面

#3


0  

If you put return $total at the very end of the function (right before the last curly brace), you should be able to use the result.

如果你在函数的最后放置return $ total(在最后一个大括号之前),你应该能够使用结果。

#4


0  

  1. Your can use global scope. I.e.

    您可以使用全局范围。即

    $s = 1;
    function eee()
    {
     global $s;
     $s++;
    }
    echo $s;
    
  2. Your can got var/return var... if your need return more that 1 value - use array

    你可以得到var / return var ...如果你需要返回更多的1值 - 使用数组

    function eee( $s)
    {
      return $s++;
    }
    eee($s=1);
    echo $s;
    

Prefer 2'd. Cause global scope operationing - may cause problems, when app become's large.

喜欢2。导致全局范围操作 - 当应用程序变大时可能会导致问题。

#1


3  

You need to "return" the value. See the entry in the PHP manual for this. Basically, return means "exit this function now". Optionally, you can also provide some data that the function can return.

您需要“返回”该值。请参阅PHP手册中的条目。基本上,返回意味着“立即退出此功能”。或者,您还可以提供函数可以返回的一些数据。

Just use the return statement:

只需使用return语句:

<?php
  function cart()
  {
      foreach ($_SESSION as $name => $value) {
          if ($value > 0) {
              if (substr($name, 0, 5) == 'cart_') {
                  $id = substr($name, 5, (strlen($name) - 5));
                  $get = mysql_query('SELECT id, name, price FROM products WHERE id=' . mysql_real_escape_string((int)$id));
                  while ($get_row = mysql_fetch_assoc($get)) {
                      $sub = $get_row['price'] * $value;
                      echo $get_row['name'] . ' x ' . $value . ' @ &pound;' . number_format($get_row['price'], 2) . ' = &pound;' . number_format($sub, 2) . '<a href="cart.php?remove=' . $id . '">[-]</a> <a href="cart.php?add=' . $id . '">[+]</a> <a href="cart.php?delete=' . $id . '">[Delete]</a><br />';
                  }
              }
              $total += $sub;
          }
      }

      return $total;
  }
?>

#2


0  

Write return $total; inside the function

写回报$ total;在功能里面

#3


0  

If you put return $total at the very end of the function (right before the last curly brace), you should be able to use the result.

如果你在函数的最后放置return $ total(在最后一个大括号之前),你应该能够使用结果。

#4


0  

  1. Your can use global scope. I.e.

    您可以使用全局范围。即

    $s = 1;
    function eee()
    {
     global $s;
     $s++;
    }
    echo $s;
    
  2. Your can got var/return var... if your need return more that 1 value - use array

    你可以得到var / return var ...如果你需要返回更多的1值 - 使用数组

    function eee( $s)
    {
      return $s++;
    }
    eee($s=1);
    echo $s;
    

Prefer 2'd. Cause global scope operationing - may cause problems, when app become's large.

喜欢2。导致全局范围操作 - 当应用程序变大时可能会导致问题。