Top

My first PHP


PHP Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  0.0 (of 0 scores)
Date Added  Dec 12, 2008
Last Updated  Dec 12, 2008
Tags  lamer 

Introduction

Code:
 <html>
<body>

<form action="welcome.php" method="post">
Age: <input type="text" name="age" />
<input type="submit" />
</form>



That's the HTML of it, I guess...I'm new to this so please go easy on me...That goes before the actual code.

Grab the Code

<? if($age >= "18");  {
 echo "Hello, you are $age "; }
else{
 echo "Sorry, you are not the correct age."; }
?>
</body>
</html>

Comments

  (14)  RSS
DangerOfFate
Comments: 44
 
PHP Snippet:  My first PHP
Posted on Dec 12, 2008 6:55 pm
lol failure....
Hawkee
Comments: 1,035
 
PHP Snippet:  My first PHP
Posted on Dec 12, 2008 7:06 pm
You should just put all the code together into the snippet. There's no reason to separate it like this. Also, always use <?php instead of <? to avoid compatibility problems with certain servers.
DangerOfFate
Comments: 44
 
PHP Snippet:  My first PHP
Posted on Dec 12, 2008 7:29 pm
Thanks Hawkee.
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  My first PHP
Posted on Dec 12, 2008 9:11 pm
Also not all servers have.... uhmmm can't think of it of the top of my head, but they won't allow $age they require you to do $age = $_POST['age'];.


Not bad for a first script :) Hope to see more, and if your stuck on a problem, yell out on the forums and we'll all help out =)
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  My first PHP
Posted on Dec 12, 2008 11:01 pm
And also, i don't think you should put your own work down....

Quote:
lol failure....


and

Quote:
Tags lamer


Sure its not the best script out there, but its your first PHP script. I know my first php script was

Code:
<?php echo "Hello World"; ?>
you wouldn't believe how much problems i had, firstly i had <?php hello world ?> i spent hours trying to get it to work, i ended up asking RussellReal and he told me i was missing the " and the ; but he wasn't going to tell me where.

We all start off at the same place, it doesn't matter what the quality of the script is, its worth the knowledge of what you gain from creating this script.
DangerOfFate
Comments: 44
 
PHP Snippet:  My first PHP
Posted on Dec 12, 2008 11:23 pm
heh..Thanks..I'm just a pessimist, I guess.
Hawkee
Comments: 1,035
 
PHP Snippet:  My first PHP
Posted on Dec 13, 2008 12:03 am
Danger, why don't you edit the snippet to reflect our suggestions?
jonesy44
Comments: 1,853
 
PHP Snippet:  My first PHP
Posted on Dec 13, 2008 6:46 am
Nice to see another PHP scripter out there :L lacks a few on this site hehe.

I must be bored or something.. but this would incorporate all your HTML & PHP together;

Code:
<?php
echo '<html>
      <body>';

if (!$_POST) {
  echo '<form action="' .$_SERVER["PHP_SELF"]. '" method="post">
        Age: <input type="text" name="age" />
        <input type="submit" />
        </form>';
}
else {
  $age = $_POST["age"];
  if ($age < 18) {
    echo 'You are too young';
  }
  else {
    echo 'Welcome';
  }
}
?>
DangerOfFate
Comments: 44
 
PHP Snippet:  My first PHP
Posted on Dec 13, 2008 1:45 pm
D:....I'm -trying- to code a PHP calculator of a sort, just an input field for someone to type like...2+2 and it'll return 4 when the action is submitted.
jonesy44
Comments: 1,853
 
PHP Snippet:  My first PHP
Posted on Dec 13, 2008 2:14 pm
Set a variable with the post, it should parse all symbols.. i think, never really had the need to try it out before..
jcbones
Comments: 2
 
PHP Snippet:  My first PHP
Posted on Dec 19, 2008 11:20 pm
Nah, PHP won't parse symbols like that, it will just print it back to the page.

Try this instead...

Code:
<?php
$form = "<form action=\"test.php\" method=\"post\"><input type=\"text\" value=\"\" name=\"one\"/><br/><input type=\"text\" value=\"\" name=\"two\"/><br/><input type=\"text\" value=\"\" name=\"three\"/><br/><input type=\"submit\" value=\"submit\" name=\"submit\"/></form>";

if($_POST["submit"])
{
  if($_POST["two"] == "+") {$value = $_POST["one"] + $_POST["three"]; echo $_POST["one"] ." + " . $_POST["three"] . " = " . $value;}
elseif($_POST["two"] == "-") {$value = $_POST["one"] - $_POST["three"]; echo $_POST["one"] . " - " . $_POST["three"] . " = " . $value;}
elseif($_POST["two"] == "*") {$value = $_POST["one"] * $_POST["three"]; echo $_POST["one"] . " * " . $_POST["three"] . " = " . $value;}
elseif($_POST["two"] == "/") {$value = $_POST["one"] / $_POST["three"]; echo $_POST["one"] . " / " . $_POST["three"] . " = " . $value;}
else echo "You have entered an unknown operator!";



}

else echo stripslashes($form);


?>
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  My first PHP
Posted on Dec 20, 2008 12:00 am
or have it as a dropdown/select box for the equation? :P
gilbertsavier
Comments: 1
 
PHP Snippet:  My first PHP
Posted on Jun 30, 2009 2:05 am
Hi,

I need a code that lets my customers upload a image file from their computer and then that file does 2 things

1. gets uploaded to upload folder on my server
2. a copy of that file gets e-mailed to me

this is what I have so far, any improvements/additions on this code would really be appreciated...

1.//GET IMAGE INFO
2.
3. $dir = 'images/';
4. $file_name = $_FILES['file']['name'];
5. $file_tmp = $_FILES['file']['tmp_name'];
6.
7. //CHECK FOR UPLOADED FILE
8. if(is_uploaded_file($file_tmp)){
9. //MOVE TO DIR
10. move_uploaded_file($file_tmp, $dir.$file_name);
11. }

Thanks & regards,
Lokananth
jonesy44
Comments: 1,853
 
PHP Snippet:  My first PHP
Posted on Jun 30, 2009 9:56 am
Wrong place to ask dude. Try Forums :)

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom