Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/244843-different-cases-of-a-function/
Share on other sites

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.

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.

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);
?>

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.

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.

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

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.

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."

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.