Jump to content
xisto Community
Sign in to follow this  
iGuest

php tutorial - basics...up to mysql/file commands

Recommended Posts

the best way to start php is probably not adjusting a script IMO - that misses out a stage. - the very first stage :) - here...

Why use php? because it allows things to be generated very quickly - without lots of html pages. It enables users you choose to allow, to input their own content to the pages you want them to.

1) save these files as anyfilename.php - pick your names, upload them to a php enabled server and go to the url they are at...

2) understand <?, ?>, .php, //////, /* */, and ;

<??>
THis tells the web browser when to expect php coding. So... when writing php, this needs to encase all php code - or it will be formatted as html.

Naming a filename .php allows for <? and ?> to be expected - or it will show as html.

<?//// hello - this line will be overlooked/* hello these lines will be overlooked*/?>

both of these 'hello's' will not be formatted as php - these 2 bits of code exclude the contained text.

<?print "hello"?>

print tells the php to print the contained text - if a variable is used then ""'s are not usually needed, if "" are used then the content will be shown as html. The above WILL NOT WORK. Each line of php code needs to be ended with a ;.

3) printing basic html

<?print 'hello world';?>

open the file - it will show the words hello world with no formatting.

4) printing and setting variables

<?$hello = 'hello world';///setting variablesprint $hello;///printing variables ?>


5) if else with variables

<?$a = $_GET['a'];///setting variables  (3)if (isset($a)) {print '<a href="?a=0">Click here</a><br>';///printing html (2)print "a is set to:".$a;} else {print '<a href="?a=1">Click here</a><br>';///printing html (2)print "a is empty";}?>


for a form... adjusting my last script from above...


6) create the form html - save as form.php.

<?$form = <<<HTML<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Form!</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body><form action="?a=1" method="post" name="" id="">  <table width="50%" border="0" align="center" cellpadding="4" cellspacing="0">    <tr>       <td width="22%">Subject</td>      <td width="78%"><input name="value" type="text" id="value"></td>    </tr>  </table></form></body></html><br>HTML;?>

that basically mates a text box to enter something into - which posts to ?a=1

then save the following as anythingyouwant.php .... load it...and should work as a simple form.
<?include 'form.php';///include the form we just made - so that you can print it when you need to.$a = $_GET['a'];///setting variables  - has the script been submitted yet? - get the value from the urlif (isset($a)) {///the script has been submitted$value = $_POST['value'];///retrieve the posted data.print '<a href="/?">Click here</a><br>';///printing html - return to script not submittedprint "value is set to:".$value;} else {////form isnt submitted - so print it.print "a is empty<br>";print "therefore include the form<br><br>";///printing html (2)print $form;}?> 


should work... unless i missed off a ; or something silly :P (let me know below...if i have)


if you want to check the field to make it required - and show a form with a message if its not filled in, then this would be the edited version.....

<?include 'form.php';///include the form - so that you can print it when you need to.$a = $_GET['a'];///setting variables  - has the script been submitted yet?if (isset($a)) {///the script has been submitted$value = $_POST['value'];///retrieve the posted data.if(isset($value)) {/////////FIELD SET?////////////print '<a href="/?">Click here</a><br>';///printing html - return to script not submittedprint "value is set to:".$value;} else {//////FEILD NOT SET - show form again, with message to fill it out///////print "The required fields are empty<br>";print "therefore include the form<br><br>";///printing html (2)print $form;}} else {////form isnt submitted - so print it.print "a is empty<br>";print "therefore include the form<br><br>";///printing html (2)print $form;}?>

then learn mysql and your sorted :P


-----------------------
see database thread

Share this post


Link to post
Share on other sites

Wow..... That tutorial is awesome !! Nice, simple, and efficient.I'm just a bit lost at the } else {well.... I'm basicly confused at what the '}' or '{' characters do... :/

Share this post


Link to post
Share on other sites

Cosmic,

 

anything within { } is what happens as a result of the previous statement.

 

That is to say

 

when you write

<?if($a == 1) {///anything within these tags happens WHEN $a = 1}if($a == 1) {///anything within these tags happens WHEN $a = 1} else {/* anything within these tags happens when the IF statement returns false, or $a !==  (does not equal) 1 */}?>

Share this post


Link to post
Share on other sites

yeah i do - i dunno why i do it like that, just a bad habit of mineand I use different length slashes without text after them, between sections so that i can check which tag closes which more easily

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.