php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69695 It should be possible to use a foreach statement to build cases in a switch
Submitted: 2015-05-23 23:10 UTC Modified: 2015-05-24 02:57 UTC
From: joshua dot and dot delaine at gmail dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 5.6.1 OS: OpenSuSE 13.2
Private report: No CVE-ID: None
 [2015-05-23 23:10 UTC] joshua dot and dot delaine at gmail dot com
Description:
------------
I am co-author of a custom CMS and I have just recently started to go back and revise it. I wanted to streamline the code to make the CMS not care what the database structure it has to work with is. There are a couple functions to get tables and get id fields and so-on, which rely on switches. This is necessary for clean code, to say the least. As it stands, I have to know in advance the table names, id fields, and description fields to manually modify these functions for each install of the CMS. It would be far superior if I could use an array generated by querying the database for tables, only knowing the name of the database. Additionally, as long as a coherent field name system was adhered to, with specific key character groups in the names, it would be possible to get the id and description fields with a slightly more complicated database query on the tables returned to get the fields that either ended in, for example "id" or "desc" to list all the id and discription column names in an array to be used to generate the cases for the respective switches in those functions as well.


---
From manual page: http://www.php.net/control-structures.switch
---


Test script:
---------------
The following link shows what one of the current functions looks like vs what I'd like to be able to do. If this is already possible, please forgive me. While a host on linux, I write code in Adobe Dreamweaver CS6 on a Windows box. It may have outdated syntax checking.

http://pastebin.com/qkyM7TS1

Actual result:
--------------
When I try to code it, Dreamweaver CS6 syntax checking breaks. It won't do any more checking until the supposed syntax error is corrected. I have not yet tried to put a test of the preferred method on my test server.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-24 02:29 UTC] joshua dot and dot delaine at gmail dot com
Note: I have since tested on my test server just running the PHP command from the command line. I get 

PHP Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting case (T_CASE) or default (T_DEFAULT) or '}' in /srv/www/htdocs/test.php on line 30

So Dreamweaver was right about this not working. Sadly, I know I could iterate multiple switches, but clearly I can't iterate cases inside a switch. I bet I couldn't even do it via an include.
 [2015-05-24 02:31 UTC] joshua dot and dot delaine at gmail dot com
-PHP Version: 5.6.9 +PHP Version: 5.6.1
 [2015-05-24 02:31 UTC] joshua dot and dot delaine at gmail dot com
Changed version to correct version for my server's install.
 [2015-05-24 02:57 UTC] joshua dot and dot delaine at gmail dot com
-Status: Open +Status: Closed
 [2015-05-24 02:57 UTC] joshua dot and dot delaine at gmail dot com
I'm starting to think that my feature request may actually be impractical based on how PHP syntax works. Looking at this, along with alternat attempts to do the same thing makes me think it can't be made to work without breaking a LOT of things first. It's one thing to make switches output differing HTML based on an array, or javascript or even CSS. It's another thing entirely to make it where a foreach spits out part of one and the rest is just there. It would confuse the hell out of the parser. Additionally, trying to put the whole of the switch inside the foreach and write different parts of it based on if it's on the first item of the array can't work either. Nevermind this. I'm sorry I posted it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 15 23:01:33 2024 UTC