I really was not expecting anyone to reply so soon, as my idea is(was...) that most talkphp members were on the left side of the Atlantic Ocean therefore by time I've sent this thread through, I thought everyone was still spleeping.
As You can (easily) imagine my routine was reading the result of a mySQL query "SHOW COLUMNS FROM table" and then I was using this routine to desintegrate column 2: example: int(7) into: $type=int and $length=7, as I want to develop a simple form generator class(by my own).
In the meanwhile I changed approach and went by this road:
$this->query = "SELECT COLUMN_NAME, DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE, CHARACTER_MAXIMUM_LENGTH, COLUMN_COMMENT from information_schema.columns where table_name = '$table'";
This sql instruction gives me directly the fields that I was looking for.
Regarding Your precious advice, those lines execute its purpose but I must confess that it's difficult for me to "read" Your php lines, as I have never done anything like that before. Looks complicated...from rookie my point of view. Some instructions how to read them in plain English would be nice...
Salathe doesn't sleep. He's out there, maintaining the balance.
It might be harder to read if you're not first familiar with the ternary operator. It's a really handy shorthand for a basic IF statement.
/* in english */ if (statement evaluates true) ? 'then do this' : 'otherwise do this';
/* in PHP */
// echo's "$somevar is set" if true echo (isset($somevar)) ? '$somevar is set' : '$somevar is not set';
// sets $isSet to true, if true $isSet = (isset($somevar)) ? true : false;
// sets $answer to "$somevar is less than or equal to $othervar" if true $answer = ($somevar <= $othervar) ? '$somevar is less than or equal to $othervar' : '$othervar is less than $somevar';
So what Salathe has done;
If $pos = strpos($text0, '(')) doesn't return false, than that means '(' was found somewhere in $text0 and the statement evaluates to true. If it evaluates to true, the ternary executes the code found after the ? and before the :, which in this case returns a substr of $text0, from the start of the string to the position where the first ( was found. If it evaluates to false, it executes the code found after the : and before the end of the block, which in this case would return a value of FALSE.
The Following User Says Thank You to delayedinsanity For This Useful Post:
So, first PHP creates '$pos' and assigns the result of 'strpos' to it, then it functions like a regular IF, so if $pos is not equal to FALSE, or $pos not of the same type (which is what the !== comparison operator means, this is used because it will return an int on success and a bool false if it failed) then assign result of substr to $text2, else assign it FALSE.
As you can see using the ternary is smaller but at the same time can be tricky to read, especially if you nest them, which you shouldnt do, not unless you want to give yourself a headache, look at this snippet posted by salathe in another post: