note 69005 added to function.define

From: Date: Sat, 19 Aug 2006 12:47:54 +0000
Subject: note 69005 added to function.define
Groups: php.notes 
Request: Send a blank email to php-notes+get-116062@lists.php.net to get a copy of this message
I was looking for a way to store multiple constants without having to call the define() function more times than necessary. So, I came up with this. define_array(); It feeds data from an array, and calls the define function multiple times. A good way to save space and save time. <? /* * define_array(); * Define a set of items from an array. * * Copyright 2006 Thomas Oldbury. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * */ function define_array($array, $case = false) { foreach($array as $key => $value) { if(!defined($key)) { define($key, $value, $case); } else { trigger_error("Cannot assign $value to $key because it is already defined", E_USER_WARNING); } } } $defines = array( "T_GLOBAL_KEYWORDS" => "...", "T_GLOBAL_DESCRIPTION" => "...", "T_GLOBAL_COPYRIGHT" => "...", "M_SQL_SERVER" => "...", "M_SQL_USERNAME" => "...", "M_SQL_PASSWORD" => "...", "M_SQL_PERSIST" => false ); define_array($defines); ?> Hope this helps someone as it helped me. -tom ---- Server IP: 83.138.144.80 Probable Submitter: 81.86.162.169 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.define.php Edit -- https://master.php.net/note/edit/69005 Del: integrated -- https://master.php.net/note/delete/69005/integrated Del: useless -- https://master.php.net/note/delete/69005/useless Del: bad code -- https://master.php.net/note/delete/69005/bad+code Del: spam -- https://master.php.net/note/delete/69005/spam Del: non-english -- https://master.php.net/note/delete/69005/non-english Del: in docs -- https://master.php.net/note/delete/69005/in+docs Del: other reasons-- https://master.php.net/note/delete/69005 Reject -- https://master.php.net/note/reject/69005 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116062) next »