PHP Tutor #1 – The Need and Some Structure!

with No Comments

My HTML apprentice, Marie, wants to learn php. She has a logical command of her thoughts, and a degree in accounting, from the University of Cebu. I figured she would take to the abstract thinking of php fast enough because accounting uses abstract thinking (algebra) and because Marie picked up HTML faster than any other new student I have had (a student starting from scratch).

This is an ongoing Tutor for PHP for people with no other knowledge – so it is very simple, and points out the pitfalls and mistakes most Newbies make early on (and some experienced programmers make too!). Each of these Tutorials will cover very small steps in learning PHP with a focus on using the PHP manual, and some ideas on how you can use certain program constructs and functions in your code.

Overwhelmed

I forgot the overwhelming fear factor of a new person gets when looking at a piece of coding, like a function, for the first time. I needed to make some minor modifications on a function, and Marie took a look at it. Wow, it was almost as if her logical brain shut down and went to sleep.

Here is the function:

  /*
   * human_check_tukod
   *
   * This provides support for evaluating if a human made the input.
   */

  function human_check_tukod( $hct ) {
    extract( $hct );
    $email_code = substr( $_POST[ $hidden_answer ], 0, 2);
    $md5_ans = substr( trim( $_POST[ $hidden_answer ] ), 2);
    $human_sin = trim( $_POST[ $human_answer ] ) + $security;
    $md = md5( $human_sin );
    if ( $md != $md5_ans ) {
      sleep(5);
      die( $non_human );
    }else{
      return $email_code;
    }
  }

The Code

If you don’t understand it, that is great! Don’t worry about that code, it is only a part of something bigger. All that function does is grab the first four characters of a string (that was input on a contact form), to return, IF the rest of the string matches a known md5. If you do not know what an md5 is, that is great too… At this point you should know nothing about php at all!

The Point of View

To an experienced programmer, he only looks at one small statement at a time. Pretty much that is just one line that ends in a semicolon (;). When he looks at that line, he looks at it from the inner most part to the outer most, like this…

$md5_ans = substr( trim( $_POST[ $hidden_answer ] ), 2 );

Like Algebra

As in Algebra, each of these steps have to be solved in a specific order, from the inner to the outer like this…

  1. $hidden_answer

  1. $_POST[ $hidden_answer ]

  1. trim( $_POST[ $hidden_answer ] )

  1. substr( trim( $_POST[ $hidden_answer ] ), 2 )

  1. $md5_ans = substr(trim( $_POST[ $hidden_answer ] ), 2 );

Unknowns

The fact you do not know what each of these do is not important. That is what the PHP manual is for. Google becomes your friend when you simply start your query with “PHP”! For example:

  • PHP trim

or

  • PHP substr

PHP also works with ideas, like say I would like a “sub string” or part of a string you could Google:

  • PHP sub string

or

  • PHP part of a string

And Google will get you to substr in the PHP Manual.

Conclusion

This post is only an introduction, the only thing you should have learned is that in php you need to start at the smallest part and build up from there. Much like the structure of Algebra. So you have to solve larger problems, by solving the smaller ones first.  Think of PHP as a whole lot of little things that are not too hard on there own.

Leave a Reply