Jump to content

jump to a different part of script


tronicsmasta

Recommended Posts

hey guys,

 

how can i jump to a different part of the script...

 

instead of using 1 large if else statment, id like to jump to a particular line or section like you would html and the achor/id tags...

 

eg: if ($var == "0") {

// code to jump to line # or anchor

} else {

// continue with code

}

 

thank you!

 

Quinton

Link to comment
Share on other sites

You can use functions to hold the code:

<?php
function pieceofcode() {
//
// put your code here
//
}

if (somecondition) {
    pieceofcode();
} else {
//
//   other stuff
//
}
?>

 

Ken

 

when calling functions, do you have to put the variables in the ()

 

eg:

 

function ($var1,$var2) {

//code

echo $var1;

echo $var2;

}

 

the reason I ask is because I put all my code into functions by the order they are called making sure that if a functionA() is called that functionA() is above that section of code...

Link to comment
Share on other sites

when calling functions, do you have to put the variables in the ()

 

Yes, if you need to pass in variables defined outside of your function. Read the link I posted.

 

i tried reading through those examples but they dont clear cut and dry explain how to call variables from 1 function to another... :(

 

I am trying to pull varA from functionA into functionB

 

eg:

 

function funcA() {
$varA = 1;
echo "varA is $varA";
}

function funcB() {
$varB = 2;
echo "varB is $varB";
}

function funcC() {
if ($varA == "1") {
	 varA();
     varB();
     } else {
	Echo "I don't know what I am doing!";
}

}

funcC();

 

How and where do I put those variables so I can pass them from 1 function to another :)

Link to comment
Share on other sites

Variables declared within a function only exist within said function, to get them out you must use return.

 

<?php

  function a() {
    $varA = 'foo';
    return $varA;
  }

  function b() {
    $varB = 'bar';
    return $varB;
  }

  $x = a();
  $y = b();

  echo $x.$y;

?>

   

Link to comment
Share on other sites

Variables declared within a function only exist within said function, to get them out you must use return.

 

<?php

  function a() {
    $varA = 'foo';
    return $varA;
  }

  function b() {
    $varB = 'bar';
    return $varB;
  }

  $x = a();
  $y = b();

  echo $x.$y;

?>

   

 

ahh... that makes much more sense... now,

when I return $varA, it becomes global if I understand that right, and I should be able to pull it into function b() no?

 

can I call a function with just a() or do i have to set a varible equal to it to call it ($x = a();)

 

thanks again!

Link to comment
Share on other sites

when I return $varA, it becomes global if I understand that right, and I should be able to pull it into function b() no?

 

Not exactly. It will end up in the current scope depending on where you call your function.

 

can I call a function with just a() or do i have to set a varible equal to it to call it ($x = a()

 

No, you do not have to assign a functions return value to a variable. Take a look at this example.

 

<?php

  function foo() {
    return true;
  }

  if (foo()) {
    echo 'foo() returned true';
  } else {
    echo 'foo() returned false';
  }

?>

 

You use built in functions all the time, user defined functions work exactly the same. eg;

 

<?php

  if (isset($_GET['a'])) {
    echo 'isset() returned true';
  } else {
    echo 'isset() returned false';
  }

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.