tronicsmasta Posted May 7, 2008 Share Posted May 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 No, there's no "goto" structure in PHP. D: You can just write the code inside the if block... Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 7, 2008 Author Share Posted May 7, 2008 that stinks! Thanks! Quote Link to comment Share on other sites More sharing options...
trq Posted May 7, 2008 Share Posted May 7, 2008 that stinks! Thanks! No, it promotes good design. Take a look at functions if you would like to be able to execute blocks of code on call. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 7, 2008 Share Posted May 7, 2008 You can use functions to hold the code: <?php function pieceofcode() { // // put your code here // } if (somecondition) { pieceofcode(); } else { // // other stuff // } ?> Ken Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 I was just about to suggest that, Ken. Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 7, 2008 Author Share Posted May 7, 2008 Ken, that makes sooo much more sense than the PHP site examples do. Thanks Ken and DarkWater! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 O_O I love the PHP site examples for most things. xD Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 7, 2008 Author Share Posted May 7, 2008 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... Quote Link to comment Share on other sites More sharing options...
trq Posted May 7, 2008 Share Posted May 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 7, 2008 Author Share Posted May 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
trq Posted May 7, 2008 Share Posted May 7, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 7, 2008 Author Share Posted May 7, 2008 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! Quote Link to comment Share on other sites More sharing options...
trq Posted May 7, 2008 Share Posted May 7, 2008 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'; } ?> Quote Link to comment Share on other sites More sharing options...
ThYGrEaTCoDeR201 Posted May 7, 2008 Share Posted May 7, 2008 I suggest you use the switch(). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.