Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 5 of 5

This is a discussion on Defining a constant in the Shared & Semi-Dedicated forum
Guys, I defined a group of constant in a data.php. define(MON1,"Jan"); define(MON2,"Feb"); define(MON3,"Mar"); define(MON4,"Apr"); . . . Is there any way that I can use ...

  1. #1
    JPC Member
    Join Date
    Mar 2003
    Posts
    13

    Defining a constant

    Guys,

    I defined a group of constant in a data.php.
    define(MON1,"Jan");
    define(MON2,"Feb");
    define(MON3,"Mar");
    define(MON4,"Apr");
    .
    .
    .

    Is there any way that I can use some code to retrive the data dynamically?

    I tried to do this but it didn't work.
    $varname = "MON";
    for ($i=1; $i<$max; $++){
    print $varname.$i;}

    There are many other way to get the solution. But I want to understand deeper the concept. If you have any idea, please let me know. Thanks!

    Johnny:>

  2. #2
    Jag Veteran
    Join Date
    Sep 2002
    Posts
    650
    Ok, about the concept:
    Code:
    print $varname.$i;
    prints a value of a $varname followed by the value of $i. What I think you are trying to achieve is dynamic creation of a variable name. Here's how it looks like:
    Code:
    $test = "a";
    $str = "test";
    print $$str;
    You can also take a look at eval function at http://www.php.net/manual/en/function.eval.php

  3. #3
    JPC Member
    Join Date
    Mar 2003
    Posts
    13
    I'm trying to do is for constant, not for variable.

    I want to use the code to create this:
    Jan
    Feb
    Mar
    Apr


    not this:
    MON1
    MON2
    MON3
    MON4

    Welcome any input.
    Thanks!
    Johnny:>

  4. #4
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    Constants aren't designed to be manipulated. You'd be better off using an array to do what you are trying to do:

    $months[0] = "Jan";
    $months[1] = "Feb";
    etc.

    Then you can loop through them as you were originally trying to do.

    Constants are designed to be substitutes for values. For example, say you have a function in a script that produces some sort of output. You wnat to provide two forms of output, raw and formatted. In this case you could set up constants:

    define(DATA_RAW, 1);
    define(DATA_FORMATTED, 2);

    Then you create a function that takes an argument with the type of output you want

    function getData($format) {
    ...
    }

    To call this function you could pass in a 1 or a 2 for $format, but such numbers can be confusing to keep straight, especially when you strat having multiple arguments and many options. However, with constants it is eaier to keep track of and makes your conde more readable:

    $data = getData(DATA_FORMATTED);

    Hope this helps.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  5. #5
    JPC Member
    Join Date
    Mar 2003
    Posts
    13
    Thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •