-1

I was trying to print the value of a variable from a function, but it is showing undefined variable value. Please help me and tell me how to solve this problem; I'm pretty new to PHP coding. I tried the function as public, but it shows some other error.

<?php

    if(isset($_POST['btn_submit']))
    {
        $num1 = $_POST['cards1'];
        $num2 = $_POST['cards2'];
        $num1=substr($num1,0,(strlen($num1)-1));
        $num2=substr($num2,0,(strlen($num2)-1));
    }

    function get_value() {

        switch ($num2) {

            case 'A' :
                $value = 11;
                break;
            case 'J':
            case 'Q':
            case 'K':
                $value = 10;
                break;
            default:
                $value = $num2;
                break;

        }

            //return $value;
    }   
    echo '<script type="text/javascript">alert("Out Put is'.$value.' ");</script>';

        //$total_value =  $num1+$num2;      


  ?>
TylerH
  • 20,799
  • 66
  • 75
  • 101
YMO
  • 3
  • 6

1 Answers1

0

$num2 is local to your function. You need to pass it in as a parameter:

function get_value($num2) {

*** YOUR CODE

}
GGio
  • 7,563
  • 11
  • 44
  • 81
  • still showing undefined variable error for value – YMO Jun 12 '14 at 13:10
  • did you pass in $num2 into the function? – GGio Jun 12 '14 at 13:13
  • yes i did function get_value($num2) { switch ($num2) { case 'A' : $value = 11; break; case 'J': case 'Q': case 'K': $value = 10; break; default: $value = $num2; break; } //return $value; } – YMO Jun 12 '14 at 13:14
  • remove comment from here - `//return $value;` You should return the value and then use it here `echo '';` – prava Jun 12 '14 at 13:16