Author Topic: best way to learn php/mysql?  (Read 2585 times)

0 Members and 1 Guest are viewing this topic.

Offline pneudralics

  • Enthusiast
  • Posts: 240
    • View Profile
Re: best way to learn php/mysql?
« Reply #15 on: June 14, 2009, 01:34:47 AM »
Best way to learn is to just do it! The more you play with it the more you'll learn. I think I'm still a newbie but when I'm finished with a project I look back and sometimes wonder how the heck I did it.
« Last Edit: June 14, 2009, 01:38:50 AM by pneudralics »

Offline Daniel0

  • Administrator
  • 'Insane!'
  • *
  • Posts: 11,817
  • Gender: Male
  • ^bb|[^b]{2}$
    • View Profile
Re: best way to learn php/mysql?
« Reply #16 on: June 14, 2009, 03:59:42 AM »
Best way to learn is to just do it! The more you play with it the more you'll learn. I think I'm still a newbie but when I'm finished with a project I look back and sometimes wonder how the heck I did it.

I guarantee you, in half a year you'll think what you did sucks. If you don't something is wrong and you haven't learned anything in the mean time.

Offline pneudralics

  • Enthusiast
  • Posts: 240
    • View Profile
Re: best way to learn php/mysql?
« Reply #17 on: June 14, 2009, 12:31:13 PM »
Best way to learn is to just do it! The more you play with it the more you'll learn. I think I'm still a newbie but when I'm finished with a project I look back and sometimes wonder how the heck I did it.

I guarantee you, in half a year you'll think what you did sucks. If you don't something is wrong and you haven't learned anything in the mean time.

I'm pretty sure in a half of year I'll probably think what I did sucks. I'm still learning. When I look at my codes I know what I did. I meant to say something more along the lines of I'm amazed I've come this far from just reading books and getting help here.

Offline jxrd

  • Enthusiast
  • Posts: 225
    • View Profile
Re: best way to learn php/mysql?
« Reply #18 on: June 14, 2009, 12:44:00 PM »
Lol, Daniel0; your post about doing stuff just for the sake of it got me thinking...so I had a go at making an explode function like you suggested.

:D

<?php
	
class 
explode
	
{
	
	
var 
$string$buffer = array();
	
	

	
	
function 
__construct()
	
	
{
	
	
	
return 
true;
	
	
}
	
	
function 
explode($delimiter$string)
	
	
{
	
	
	
$this->string $string;
	
	
	

	
	
	
$string = array();
	
	
	

	
	
	
foreach(
$this->tokenise() as $key => $value)
	
	
	
{
	
	
	
	
if(
$value == $delimiter)
	
	
	
	
{
	
	
	
	
	
array_push($stringimplode($this->buffer));
	
	
	
	
	
$this->buffer = array();
	
	
	
	
}
	
	
	
	
else
	
	
	
	
{
	
	
	
	
	
array_push($this->buffer$value);
	
	
	
	
}
	
	
	
}
	
	
	

	
	
	
return 
array_merge($string, array(implode($this->buffer)));
	
	
}
	
	
function 
tokenise()
	
	
{
	
	
	
$array = array();
	
	
	

	
	
	
for(
$i 0$i strlen($this->string); $i++)
	
	
	
{
	
	
	
	
array_push($arraysubstr($this->string$i1));
	
	
	
}
	
	
	

	
	
	
return 
$array;
	
	
}
	
}
	

	
$explode = new explode;
	
$string 'hello world :)';
	

	
print_r($explode->explode(' '$string));
?>

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: best way to learn php/mysql?
« Reply #19 on: June 14, 2009, 02:45:50 PM »
Best way to learn is to just do it! The more you play with it the more you'll learn. I think I'm still a newbie but when I'm finished with a project I look back and sometimes wonder how the heck I did it.

I guarantee you, in half a year you'll think what you did sucks. If you don't something is wrong and you haven't learned anything in the mean time.

I'm pretty sure in a half of year I'll probably think what I did sucks. I'm still learning. When I look at my codes I know what I did. I meant to say something more along the lines of I'm amazed I've come this far from just reading books and getting help here.

I've been coding PHP for I think around 3 years now, and I still look back at things sometimes and I'm like "Wtf?!"





jxrd, I have a question...  Why did you wrap it in a class?



I'm procrastinating what I should be doing (x.x), so I decided to give it a go:


//array explode ( string $delimiter , string $string [, int $limit ] )
//I don't feel like implementing $limit
//Also, I would use better names than $d and $s if I were going to use this for real lol.
function splode($d$s) {
	
$len strlen($s);
	
$offset 0;
	
$out = array();
	
//while(($place = strpos($s, $d, $offset)) !== false) {
	
while(
$offset $len && ($place strpos($s$d$offset)) !== false) {
	
	
$out[] = substr($s$offset$place-$offset);
	
	
$offset $place+1;
	
}
	
//The place = blah !== false will not get the last match...
	
if(
$offset $len) {
	
	
$out[] = substr($s$offset$len-$offset);
	
}
	
return 
$out;
}
print_r(explode(' ''This is a string with spaces!'));
print_r(splode(' ''This is a string with spaces!'));



When ever I first got into C++, I didn't know that the std::string class had a method designed for tokenizing a string, so I once recreating explode() in C++.  It was essentially the same as this haha.
Why doesn't anyone ever say hi, hey, or whad up world?

Offline jxrd

  • Enthusiast
  • Posts: 225
    • View Profile
Re: best way to learn php/mysql?
« Reply #20 on: June 14, 2009, 02:54:03 PM »
jxrd, I have a question...  Why did you wrap it in a class?
Idk, I needed to use two functions...thought it would be appropriate.

I guess I'm just not as resourceful as you :P

But...I bet you can't do strlen() !!!!!

<?php
	
function 
str_len($str)
	
{
	
	
$str preg_split('/./'$str);
	
	

	
	
return 
count($str) - 1;
	
}
	

	
echo 
str_len('7 chars');
?>

Offline Daniel0

  • Administrator
  • 'Insane!'
  • *
  • Posts: 11,817
  • Gender: Male
  • ^bb|[^b]{2}$
    • View Profile
Re: best way to learn php/mysql?
« Reply #21 on: June 14, 2009, 02:54:18 PM »
Heh... you cheated and used built-in functions. Here you go with a with an explode() implementation that doesn't use any of PHP's built-in functions whatsoever.

function myStrlen($string)
{
	
for (
$length 0; isset($string[$length]); $length++) {}
	

	
return 
$length;
}

function 
myExplode($delimiter$string$limit null)
{
	
$parts = array();
	

	
for (
$i 0$j 0$length myStrlen($string); $i $length$i++) {
	
	
if (
$string[$i] == $delimiter && ($limit === null || $limit $j+1)) {
	
	
	
$j++;
	
	
	
continue;
	
	
}
	
	

	
	
if (!isset(
$parts[$j])) {
	
	
	
$parts[$j] = $string[$i];
	
	
}
	
	
else {
	
	
	
$parts[$j] .= $string[$i];
	
	
}
	
}
	

	
return 
$parts;
}

Offline jxrd

  • Enthusiast
  • Posts: 225
    • View Profile
Re: best way to learn php/mysql?
« Reply #22 on: June 14, 2009, 02:56:02 PM »
Oh, well, you didn't say we couldn't use built in functions...

Btw you used isset()!! Although, is that a language construct or a function..?

Suggest another; this is fun :D

Offline Daniel0

  • Administrator
  • 'Insane!'
  • *
  • Posts: 11,817
  • Gender: Male
  • ^bb|[^b]{2}$
    • View Profile
Re: best way to learn php/mysql?
« Reply #23 on: June 14, 2009, 02:57:22 PM »
isset() is a language construct, so I still win :P

Here you have a few others:
function myImplode($glue, array $pieces)
{
	
$string '';
	
$maxIndex myStrlen($pieces)-1// myStrlen() will work for arrays because of the way it's written
	
foreach (
$pieces as $i => $piece) {
	
	
$string .= $piece;
	
	

	
	
if (
$i != $maxIndex) {
	
	
	
$string .= $glue;
	
	
}
	
}
	

	
return 
$string;
}

function 
myStrpos($haystack$needle$offset 0)
{
	
for (
$i $offset$length myStrlen($haystack); $i $length$i++) {
	
	
if (
$haystack[$i] == $needle) {
	
	
	
return 
$i;
	
	
}
	
}
	

	
return 
false;
}

function 
myStrrpos($haystack$needle$offset 0)
{
	
for (
$i myStrlen($haystack) - $offset$i >= 0$i--) {
	
	
if (
$haystack[$i] == $needle) {
	
	
	
return 
$i;
	
	
}
	
}
	

	
return 
false;
}
« Last Edit: June 14, 2009, 02:59:07 PM by Daniel0 »

Offline jxrd

  • Enthusiast
  • Posts: 225
    • View Profile
Re: best way to learn php/mysql?
« Reply #24 on: June 14, 2009, 03:00:50 PM »
Lol

How would you do foreach()?

I tried it in C++ the other day...didn't do too well. C++ arrays confuse me...with their vectors and stuff.

My initial thoughts would be var_export()...but obviously that's a built in function.

Appologies for off-topic-ness btw :P
« Last Edit: June 14, 2009, 03:01:22 PM by jxrd »

Offline Daniel0

  • Administrator
  • 'Insane!'
  • *
  • Posts: 11,817
  • Gender: Male
  • ^bb|[^b]{2}$
    • View Profile
Re: best way to learn php/mysql?
« Reply #25 on: June 14, 2009, 03:07:59 PM »
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().

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: best way to learn php/mysql?
« Reply #26 on: June 14, 2009, 03:10:05 PM »
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.
Why doesn't anyone ever say hi, hey, or whad up world?

Offline jxrd

  • Enthusiast
  • Posts: 225
    • View Profile
Re: best way to learn php/mysql?
« Reply #27 on: June 14, 2009, 03:36:10 PM »
<?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 0counti++)
    {
        
buffer += array[i];
    }

    return 
buffer;
}

int main()
{
    
char* array[3] = {"a""b""c"};

    
cout << implode(array);

    return 
0;
}

Does not work at all lol.