Daniel0 Posted June 14, 2009 Share Posted June 14, 2009 Well, foreach is easy to do using a for loop, but only for enumerated arrays. foreach is a language construct too though, so you can't really implement it yourself. The string and array functions should be possible without too much trouble though. You could try str_rot13, str_replace, trim or perhaps ucwords. Quote Link to comment https://forums.phpfreaks.com/topic/161965-best-way-to-learn-phpmysql/page/2/#findComment-855734 Share on other sites More sharing options...
corbin Posted June 14, 2009 Share Posted June 14, 2009 Gah... I'm a slow poster... People have posted twice since this ;p. isset is a language construct. Hrmmm, I'm not sure how you could get around using isset though without causing PHP warnings though. You tried to implement foreach() in C++? Man, you live on the edge! How would you have done that? Make an array_walk type function? Passing pointers to functions into functions would get murky quickly due to the strictness of function pointer passing in C++, although I guess you could use templating to make that easier maybe. Or do you mean you actually tried to recreate the language construct foreach in C++? I actually like the datatypes in C++. I mean, when it comes down to coding speed, I code in PHP about 2938908923589234 times faster, but something about the rigidity and what not I like. Quote Link to comment https://forums.phpfreaks.com/topic/161965-best-way-to-learn-phpmysql/page/2/#findComment-855736 Share on other sites More sharing options...
jxrd Posted June 14, 2009 Share Posted June 14, 2009 <?php function rot13_2($str) { $alpha = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ); $buffer = null; for($i = 0; isset($str[$i]); $i++) { $alphanumeric = false; $charcode = 0; foreach($alpha as $key => $value) { if($value == $str[$i]) { $alphanumeric = true; $charcode = $key; } } if($alphanumeric) { $charcode = ($charcode <= 13) ? $charcode + 13 : $charcode - 13; $buffer .= $alpha[$charcode]; } else { $buffer .= $str[$i]; } } return $buffer; } $str = rot13_2('hello'); echo $str.'<br />'; echo rot13_2($str); ?> str_rot13 Proud of myself lol. And yeah...numeric indexes would be easy for foreach. However...not if the indexes don't increment by 1 For example, array(0 => 'something', 5 => 'something else); would be pretty hard to do... And yeah...I tried. And failed lol. Oh wait no, it was some sort of implode function. I can't find it now...I had another go though.... #include <iostream> using namespace std; string implode(string array) { string buffer; int count = sizeof(array) / typeid(array[0]); for(int i = 0; i < count; i++) { buffer += array[i]; } return buffer; } int main() { char* array[3] = {"a", "b", "c"}; cout << implode(array); return 0; } Does not work at all lol. Quote Link to comment https://forums.phpfreaks.com/topic/161965-best-way-to-learn-phpmysql/page/2/#findComment-855757 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.