etrader Posted August 15, 2011 Share Posted August 15, 2011 I have a simple function reading a two-component array as function arr($word) { include 'array1.php"; echo $something[$word]; } How I can make a condition for this function to load different array files as array1.php or array2.php or array3.php. The name of file comes from a string. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/ Share on other sites More sharing options...
JonnoTheDev Posted August 15, 2011 Share Posted August 15, 2011 <?php function arr($word, $include) { include_once($include); echo $something[$word]; } arr('test', 'array1.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1257761 Share on other sites More sharing options...
etrader Posted August 16, 2011 Author Share Posted August 16, 2011 Thanks, but I do not want to add the second variable into the function, since the function is widely used in the text. I get the string from url as $q=$_GET[[$q]; now I want to include a given file for each value of $q. The text frequently contains $arr('something'). I want to display different message by $arr('something') depending on the value of $q. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1257967 Share on other sites More sharing options...
phpSensei Posted August 16, 2011 Share Posted August 16, 2011 OP you ment like this? <?php function arr($word){ foreach($files as $file){ include($file); } } $files = array('array1.php','$array2.php','array3.php'); arr($files); ?> Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1257969 Share on other sites More sharing options...
phpSensei Posted August 16, 2011 Share Posted August 16, 2011 or did you mean this <?php $q = $_GET['q']; function arr($word){ switch($word){ case 'something1': include('array1.php'); break; case 'something2': include('array2.php'); break; } } arr($q); ?> Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1257973 Share on other sites More sharing options...
etrader Posted August 16, 2011 Author Share Posted August 16, 2011 EXACTLY! I was thinking of switch but I was not sure if I can use it within a function. Thanks a million Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1257980 Share on other sites More sharing options...
etrader Posted August 16, 2011 Author Share Posted August 16, 2011 Sorry, but it did not resolve the issue. You defined the case by "something", but I want to define it by $q. For example, $q can be "modern" or "classic"; I want to include modern.php or classic.php Indeed, my problem is how to bring $q into the function. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1257984 Share on other sites More sharing options...
phpSensei Posted August 16, 2011 Share Posted August 16, 2011 Etrader, the script can't guess what to include, you to tell it to include based on certain predefined values.. otheriwse the only thing you can do is include($q); Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1257988 Share on other sites More sharing options...
etrader Posted August 16, 2011 Author Share Posted August 16, 2011 Yes, but how I can bring $q which is a string into a function. My main problem is that. How to use $q inside function. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1257996 Share on other sites More sharing options...
JonnoTheDev Posted August 16, 2011 Share Posted August 16, 2011 The variable name used as a function parameter will take the value of whatever is passed into it <?php function arr($word) { switch($word) { case 'modern': include('array1.php'); break; case 'classic': include('array2.php'); break; } } $q = $_GET['q']; arr($q); ?> Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258043 Share on other sites More sharing options...
etrader Posted August 16, 2011 Author Share Posted August 16, 2011 Let me clarify what I need <?php $q = $_GET['q']; function arr($word) { switch($q) { case 'modern': include('modern.php'); break; case 'classic': include('classic.php'); break; } } arr($word); ?> My problem is how to make switch for $q inside a function for $word. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258062 Share on other sites More sharing options...
trq Posted August 16, 2011 Share Posted August 16, 2011 You'll need to pass $q into the function. Not that doing so makes any sense in this context. Tell us, why are you passing $word into your function and then not using it? Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258063 Share on other sites More sharing options...
etrader Posted August 16, 2011 Author Share Posted August 16, 2011 I use $word. Actually, the array file has a structure of $term = array( "word1" => "modern1", "word2" => "modern2" ); and function will be function arr($word) { switch($q) { case 'modern': include('modern.php'); break; case 'classic': include('classic.php'); break; } echo $term[$word]; } I want to use arra('something') in the text by reading corresponding array file depending on the value of $q. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258080 Share on other sites More sharing options...
trq Posted August 16, 2011 Share Posted August 16, 2011 Sorry but nothing in your last reply made any sense. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258082 Share on other sites More sharing options...
trq Posted August 16, 2011 Share Posted August 16, 2011 And now you have introduced yet another non-existent variable into your function. You might want to take a look at functions. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258083 Share on other sites More sharing options...
etrader Posted August 16, 2011 Author Share Posted August 16, 2011 I get $q by $q = $_GET['q']; but I am looking for a way to bring it into the function to make the switch based on the value of $q Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258086 Share on other sites More sharing options...
phpSensei Posted August 16, 2011 Share Posted August 16, 2011 edit: The codes i provided did that. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258087 Share on other sites More sharing options...
etrader Posted August 16, 2011 Author Share Posted August 16, 2011 Thanks phpSensei, but switch in your code works with $word not $q I want to switch based on the value of $q The problem is that $q is meaningless within function Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258090 Share on other sites More sharing options...
trq Posted August 16, 2011 Share Posted August 16, 2011 As has been said (and covered in the link I provided you) you will need to pass $q into the function. What part of that do you not understand? Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258093 Share on other sites More sharing options...
KevinM1 Posted August 16, 2011 Share Posted August 16, 2011 Okay, time for a crash course in functions: The first thing you need to know is that functions have their own scope. This means that whatever is going on outside of the function will NOT be present inside the function. At least, not unless you explicitly pass those exterior values into the function. How are values passed into a function? The correct way to do it is to use the function's argument list. What is the argument list? Functions have a signature. The signature contains the function name and argument list. You may have looked at the PHP manual and seen something like: function name($arg1, $arg2) The part within parentheses is the argument list. So, if you need to use $q, you need to pass it into your function: function arr($q, $word) // <-- function definition - blueprint for the function, NOT code that executes immediately { /* do stuff with $q and $word */ } $q = $_GET['q']; arr($q, $word); // <-- function invocation, where arr finally runs, using the values you passed in Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258095 Share on other sites More sharing options...
phpSensei Posted August 16, 2011 Share Posted August 16, 2011 YAY Crash course, they are my favorite, its like an unexpected sexy party. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258099 Share on other sites More sharing options...
etrader Posted August 16, 2011 Author Share Posted August 16, 2011 Thanks Nightslyr! You are an excellent teacher and I learned a lot. However, it did not resolve my issue. In this case, I need to include $q in $arr() within the text. I try to keep the format of $arr('some words'). I was looking for a trip to bring the string ($q) into the function; but it seems it is not possible. Maybe it is better to put three functions within the switch cases. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258103 Share on other sites More sharing options...
trq Posted August 16, 2011 Share Posted August 16, 2011 You could drag the variable in via the global keyword but this completely breaks the encapsulation provided by the function in the first place. In other words, if this is really what you want, don't use a function. Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258104 Share on other sites More sharing options...
KevinM1 Posted August 16, 2011 Share Posted August 16, 2011 Thanks Nightslyr! You are an excellent teacher and I learned a lot. However, it did not resolve my issue. In this case, I need to include $q in $arr() within the text. I try to keep the format of $arr('some words'). I was looking for a trip to bring the string ($q) into the function; but it seems it is not possible. Maybe it is better to put three functions within the switch cases. Maybe you should give us a clear idea as to what you're trying to do. All I can gather is that you want $q to come in from the address bar, and you want to use it as a switch to do something. The rest has been a series of "That's not what I'm trying to do." Quote Link to comment https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/#findComment-1258109 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.