CEMETECH
Leading The Way To The Future
Login [Register]
Username:
Password:
Autologin:

Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 112 users online: 0 members, 75 guests and 37 bots.
Members: None.
Bots: VoilaBot (1), Spinn3r (1), MSN/Bing (1), Magpie Crawler (5), VoilaBot (10), Googlebot (19).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
Author Message
kpa4941


New Member


Joined: 23 Nov 2010
Posts: 84
Location: Carrollton, Texas

Posted: 25 Jun 2011 04:57:59 pm    Post subject: Can there be a default array value in php

Say I have an array that looks like this:

Code:
$array = array(0,1,2,3);


Is it possible to have a default array value so that if you attempt to access a value outside of the array, it returns an error message?


This code would return the default value since the array element 10 doesn't exist.

Code:
echo $array[10];


Code:
// Default value to return
echo "The value doesn't exist.";

_________________
--kpa4941


Last edited by kpa4941 on 26 Jun 2011 01:14:19 am; edited 1 time in total
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55759
Location: Earth, Sol, Milky Way

Posted: 26 Jun 2011 12:45:38 am    Post subject:

You could implement it like this:


Code:
$default_array_dne = "[MISSING VALUE]";

function StrictArrayGet($array,$key) {
    global $default_array_dne;
    return array_key_exists($key,$array)?$array[$key]:$default_array_dne;
}

_________________


Back to top
kpa4941


New Member


Joined: 23 Nov 2010
Posts: 84
Location: Carrollton, Texas

Posted: 26 Jun 2011 01:18:36 am    Post subject:

Ok, I see how that would work, but is there such thing as a default array though, kind of like switch/case?

If not, I guess that this would be the best solution.
_________________
--kpa4941
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55759
Location: Earth, Sol, Milky Way

Posted: 26 Jun 2011 01:54:53 am    Post subject:

kpa4941 wrote:
Ok, I see how that would work, but is there such thing as a default array though, kind of like switch/case?

If not, I guess that this would be the best solution.
You could restructure it as a conditional instead of a function, but I don't think PHP has a more elegant way to do this (at least that I know of).
_________________


Back to top
kpa4941


New Member


Joined: 23 Nov 2010
Posts: 84
Location: Carrollton, Texas

Posted: 26 Jun 2011 04:27:31 pm    Post subject:

Alright well thanks for the help. It would be nice if PHP had something like that in the future.

I think that I am going to go with your function.
_________________
--kpa4941
Back to top
KermMartian


Site Admin


Joined: 14 Mar 2005
Posts: 55759
Location: Earth, Sol, Milky Way

Posted: 26 Jun 2011 06:34:40 pm    Post subject:

kpa4941 wrote:
Alright well thanks for the help. It would be nice if PHP had something like that in the future.

I think that I am going to go with your function.
Excellent, I hope that it serves you well. You should suggest this to the PHP developers!
_________________


Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are GMT - 5 Hours

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.030791 seconds.