Jump to content

best way to learn php/mysql?


Snap40

Recommended Posts

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.

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.

<?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 :D Proud of myself lol.

 

And yeah...numeric indexes would be easy for foreach. However...not if the indexes don't increment by 1 :P

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.