I am using a code snippet with goto function in PHP
Here is my code
function goto_test()
{
$a = 3;
if($a == 3)
{
goto x;
}
else
{
if($a == 2)
echo "Thuthmekri";
else if($a==1)
goto y;
}
echo "Thothamon";
x:
{
echo "There was a brown crow";
}
y:
{
echo "There was a white crow";
}
}
The issue is that it is displaying
There was a brown crowThere was a white crow
Now it seems when its getting goto x;
its running to the goto
label x;
and running it.
But why the label y
is also getting executed? Shouldnot it be left over, when $a=3
?
EDIT:
Note-- People are asking me, why I am usig Goto. The code i pasted was just to make u all inderstand a scenario i have faced. My actual situation code is a vast one. And it seemed that i need to use goto in that case.
May be if i put my original code ober here. then people will understand the situation. May be seeing the actual position, people will stop downvoting me
if($this->session->userdata('discount_on')==true && $this->session->userdata('discount_on')==2)
{
if($this->session->userdata('discount_over')==true && $this->session->userdata('discount_over')==1)
{
if($this->session->userdata('discount_type')==1)
{
$tot_disc = round(floatval($total_amount * floatval($discount_info[0]['discount_amount']/100)),2);
}
else if($this->session->userdata('discount_type')==2)
{
$tot_discount = round(floatval($r_cur * (floatval($discount_info[0]['discount_amount']))),2);
}
$total_amount = round(floatval($total_amount - $tot_discount),2);
}
}
if( $this->session->userdata('discount_type')==true && $this->session->userdata('discount_type')==3)
{
if($this->cart->total() < $discount_info[0]['discount_amount'])
{
goto ship;
}
else
{
if($discount_info[0]['discount_country']==$country)
{
$shipping_charge = 0.00;
}
else if($discount_info[0]['discount_country']=="0")
{
$shipping_charge = 0.00;
}
else if($discount_info[0]['discount_country']=="-1"
&&
$primary_country[0]['country_id']!=$country)
{
$shipping_charge = 0.00;
}
else
{
goto ship;
}
}
}
else
{
goto ship;
}
ship:
{
$primary_total_amount = round(floatval($total_amount/$r_cur),2);
$shipping_info = $this->autoload_model->get_data_from_table("td_shippingcharge","*",
"td_shippingcharge.shippingcharge_country = '$country'
AND td_shippingcharge.shippingcharge_type = '2'
AND td_shippingcharge.s_range <= '$primary_total_amount'
AND td_shippingcharge.e_range > '$primary_total_amount'")->result_array();
if(count($shipping_info)>0) /* if shipping details of particular country with range of item exists-----*/
{
$main_shipping = $shipping_info[0]['shippingcharge_price'];
if($shipping_info[0]['shippingcharge_country']==$primary_country[0]['country_id'])
/*------ if the country selected is primary country,
then provincial shipping charge will be calculated------*/
{
$province_shipping = $this->autoload_model->get_data_from_table("td_provinceship","*","td_provinceship.state_id = '$state'
AND td_provinceship.provinceship_status = '1'
AND td_provinceship.shippingcharge_id = '".$shipping_info[0]['shippingcharge_id']."'")->result_array();
if(count($province_shipping)>0)/*-- in provincial shipping charge amount exists and enabled----*/
{
$provincecharge = $province_shipping[0]['provinceship_amount'];
}
else
$provincesharge = 0.00;
$shipping_charge = floatval($main_shipping) + floatval($provincecharge);
}
else
$shipping_charge = floatval($main_shipping);
}
else /*--- shipping details of rest of the countries with range of item exists ---*/
{
$restshipping_info = $this->autoload_model->get_data_from_table("td_shippingcharge","*",
"td_shippingcharge.shippingcharge_country = '-1'
AND
td_shippingcharge.shippingcharge_type = '2'
AND
td_shippingcharge.s_range <= '$primary_total_amount'
AND
td_shippingcharge.e_range > '$primary_total_amount'")->result_array();
if(count($restshipping_info)>0)
{
$shipping_charge = $restshipping_info[0]['shippingcharge_price'];
}
else
$shipping_charge = 0.00;
}
}
$_SESSION['primary_del_charge'] = $shipping_charge;
$_SESSION['del_charge'] = round(floatval($r_cur * $_SESSION['primary_del_charge']),2);