Jump to content
xisto Community
Sign in to follow this  
Sten

Going To Try Learning Php

Recommended Posts

As some of you may know, I'm not exactly the worlds greates at PHP, infact I don't know much at all. I mainly like to design websites (the graphical side of it), not code any scripts. I only know x/html and css.So... I've decided to take a go at learning PHP, because as you also might know, I love Joomla and I want to start making components and that for it, I have the basic idea of how it all works, but I've only ever made templates for it before.I have this book here, I borrowed off a friend about a year ago and I'm not sure if he hinted I can just have it or what, lol. Today I've learned how to pass values from forms or something like that, $user = $_POST['name']; something like that, am I right?I also know basics like variables and ifs, elseifs and else and that (oh and echo/print) but I can't do anything advanced. I don't have trouble understanding it, I just have trouble remembering it and know where to shove things and how to shove things in somewhere.So basically, what do you think is the best way to learn PHP? I'm finding the book good but as I said above, I need to get it into my head. I've been trying like writing it straight out of the book, then re-writing it changing bits and then re-writing it again with the book closed, it seems to work if I do it about 5 times, but I was told it's not a good way, so what else should I try?So how should I go about learning it and how long should it take to be able to get started on making Joomla components? I want to make a forms component where people just select what type of field they want, and add the properties and that since I've never actually seen one like that. I also want to make a comments component.

Share this post


Link to post
Share on other sites

Well I have recently made a site (and de-bugging it now) that sounds similar to Joomla. Basically it allows users to make very advanced dynamic websites without knowing any PHP and just HTML, however I don't know if this is a site for you, because it is aimed towards programmers that don't want/need to learn PHP and just want to get lots of stuff done at once easily. Anyway after my "project" I found that I was able to program PHP very well. Maybe you should think about a project or projects to do on your own. In my opinion it is way harder to work of someone else’s code (Joomla est.) while you are learning to program then it is to make your own.

After that you should know PHP well. By now you are probably thinking something like "well I want it to be part of Joomla" or "how will making my own project(s) help?" Well first of once you know PHP you can do work on Joomla. Making your own project makes your learn a lot faster then trying to copy someone else’s things 5 times, because you need to actually think about it rather then keep focused on copying it down. I would suggest that you learn the basics from a book/website (as in basics I mean individual strings not all at once because you will get confused with all that stuff that doesn't apply to you).

Here is an example:<br>

//My example book code<?php$var=$_GET['var'];if(is_numeric($var)){//Numberif($var==1){//Check Valueecho('var is 1!');}else{//If value is not 1echo('var is not 1! It is: '.$var);}}else{//Value is not a number at all.echo('var is not a number!');}?>
This may be what you copy down but how much of it do you learn? You just learn to copy that’s all. The best way to learn would be to break down the code on your own for example if you want to learn how to echo a var simple type:
<?php$var=$_GET['var'];echo('var is not 1! It is: '.$var);?>
Now mess around with it a bit and you soon find that you can add .' That was the variable' just before the ); and your script will echo the last part also. You may also notice you can type {$var} rather then '.$var.'
This is a basic example but do you get what I am saying? From this you are not just copying stuff down you are actually learning PHP. For a big list of function visit php.net
Once you master a few function simply tie them together and see how they. Keep at it and you will be programming in PHP in no time.

Good luck :) and hope this helps,
Sparkx

Share this post


Link to post
Share on other sites

I think the best way to learn anything is exactly the way you're doing it: hands-on. Most of us who shun the whole go-to-college wave for technical education find the same knowledge accessible on the Internet. You will seldom retain everything you've read regardless of how many times through you read a book. A better approach to this would be to take anywhere from half of a chapter (if you must use a book) to a full chapter in your PHP learning material. Don't study and don't read it. Type out the code yourself in the code listings (don't copy/paste).

 

Use common sense to understand why what you're typing must be done in the outlined structure. By the time you get it loaded onto a PHP supported server and view it in your web browser, you will have understood what you're typing did, why it did and things you can do to modify your results and tailor it to your specific needs. These findings are the endless paragraph explanations you'll only read about but never find out for yourself if all you do is read the book. Bottom line being, you'll learn more by applying a little about PHP than reading a lot about PHP.

 

There's an unlimited number of tutorials for any programming language, trade or profession if you just keep an eye out. For beginners, I would highly recommend W3Schools (PHP): http://www.w3schools.com/php/default.asp

 

Also, a few other tips to keep the overall learning process easy! Use plenty of whitespace. This will help you to logically understand your own PHP code. I personally wouldn't write code so close together when the amount of whitespace being used between assignments, etc won't affect what you're trying to write.

 

For example, a rewrite of sparkx's code would be:

 

<?php

// Get the contents of var query string (http://forums.xisto.com/no_longer_exists/)

$var = $_GET['var'];

 

// Is it numeric?

if(is_numeric($var)) {

 

// Does it have a value of 1?

if($var == 1){

echo('var is 1!');

} else {

echo('var is not 1! It is: ' . $var);

}

} else {

echo('var is not a number!');

}

?>

 

You will be doing yourself a favor in the interim by generously spacing out your code and documenting them. Also, find yourself a good text editor that supports syntax highlighting for PHP code. My favorite to use on Windows platforms is Notepad++ which is an Open Source Software (OSS) you can download by searching with Google or Sourceforge.net.

 

Here's another example for accepting and printing form input:

 

<?php

 

//

// Check if form has been submitted

//

 

if(isset($_POST)) {

 

//

// Collect data from POST variables

//

 

$get_fname = $_POST['first_name'];

$get_lname = $_POST['last_name'];

$get_bio = $_POST['biography'];

 

//

// Print a summary of posted data

//

 

print "

<h1>Summary</h1>

<p>

First Name: {$get_fname}<br />

Last Name: {$get_lname}<br />

Biography:<br />

{$get_bio}

</p>

";

}

?>

<!--

The action specified in this form element uses $_SERVER['PHP_SELF'].

This is the equivalent as the filename of this script.

-->

<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="post">

<p>

First Name:<br />

<input type="text" name="first_name" />

</p>

 

<p>

First Name:<br />

<input type="text" name="first_name" />

</p>

 

<p>

Biography:<br />

<textarea cols="75" rows="7" name="biography" />

</p>

 

<p><input type="submit" value="Submit" /></p>

</form>

 

This PHP script is fairly easy to understand if you use some common sense when reading PHP functions. How do we find if the $_POST variable (or any of its variables) are set? Let's ask the same question using PHP.

 

// Is the $_POST variable set?

isset($_POST);

 

If it is, then let's do something!

 

if(isset($_POST)) {

// Code to be executed if the $_POST variable is set

}

 

Also, keep in mind that while using any conditional statements, loops or functions that require the use of brackets, try to indent them on the level they're used to make it easier for you to read and go back to troubleshoot if you should run into problems.

 

//

// Conditional statements indented on a single level

//

 

if($condition) {

// Execute these statements

}

 

 

//

// Conditional statements indented on multiple level

//

 

if($condition) {

// Execute these statements

if($condition) {

// Execute these statements

if($condition) {

// Execute these statements

}

}

if($condition) {

// Execute these statements

if($condition) {

// Execute these statements

}

}

if($condition) {

// Execute these statements

if($condition) {

// Execute these statements

}

}

}

 

Let me know if that helps any!

Edited by Mr. Matt (see edit history)

Share this post


Link to post
Share on other sites

The best and easiest route to learning a language I have found, computer or vocal lanugage, is to just look over it a lot. Do tutorials. Do them a few times and look at your coding a lot. Eventually it'll stick in your head. By the way, http://www.w3schools.com/php/default.asp (w3schools) is a good site for tutorials, as well as tizag. Both I used when learning PHP.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.