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 Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/ 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... Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534912 Share on other sites More sharing options...
tronicsmasta Posted May 7, 2008 Author Share Posted May 7, 2008 that stinks! Thanks! Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534916 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. Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534921 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 Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534922 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. Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534926 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! Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534929 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 Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534932 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... Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534954 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. Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534957 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 Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534965 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; ?> Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534972 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! Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534974 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'; } ?> Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534977 Share on other sites More sharing options...
ThYGrEaTCoDeR201 Posted May 7, 2008 Share Posted May 7, 2008 I suggest you use the switch(). Link to comment https://forums.phpfreaks.com/topic/104487-jump-to-a-different-part-of-script/#findComment-534995 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.