A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearLooking for a better method of checking a variable repeatedly - page 1
User InfoPosts
Looking for a better method of checking a variable repeatedly#1
I recently created a site that simply has a variable and checks it against various possible values and gives an appropriate response. The program goes through using a lot of else if statements.

I(m sure there is a better method of doing this, but not sure what to use. I(m still learning PHP really.

Here(s the source code to give you a better idea:

http://github.com/r3morse/isitup/blob/a7a972bcf125d1f058a44406a467438d46aa4b16/functions.php

posted date: 2009-04-09 05:33:00


Re: Looking for a better method of checking a variable repeatedly#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-09 05:33:01


Re: Looking for a better method of checking a variable repeatedly#3
Probably switch is what you(re after, it is equivalent to if... else if... .http://be.php.net/switch

posted date: 2009-04-09 05:37:00


Re: Looking for a better method of checking a variable repeatedly#4
Try State or Chain-of-responsibility

posted date: 2009-04-09 05:41:00


Re: Looking for a better method of checking a variable repeatedly#5
Thanks, that looks perfect!

posted date: 2009-04-09 05:43:00


Re: Looking for a better method of checking a variable repeatedly#6
If every code need a different operation, i dont think you can disguise the else..if structure..You could use a switchswitch($code){case "200": //then you code break;case "401": //other code break;}and so on.. but i dont think you(ll gain something relevant..Other way can be store alle the code inside an array, indexed like $code => $message..but only if you just print something for each code.

posted date: 2009-04-09 05:47:00


Re: Looking for a better method of checking a variable repeatedly#7
another possibility would be to define an object with for the appropriate actions and dispatch.like ...<?php class Dispatcher { public function action_404($code) { return (foo bar ( . $code; } public function action_200($code) { return (far boo ( . $code; } public function action_301($code) { return $this->action_200($code); } /* and so on */ public function unknownAction($code) { return (don\(t know ( . $code; } } $code = (int)$_REQUEST[(code(]; $methodName = (action_( . $code; $dispatcher = new Dispatcher(); if (method_exists($dispatcher, $methodName)) { $result = $dispatcher->$methodName($code); } else { $result = $dispatcher->unknownAction($code); } echo $result;?>doesn(t really makes sense in your case tough.

posted date: 2009-04-09 06:05:00


Re: Looking for a better method of checking a variable repeatedly#8
Thanks all, it looks like it's going to say as it is for now then.

posted date: 2009-04-09 11:17:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.