My first simple PHP script

Hello lovely people :-*

I have been learning PHP for a few weeks now. I am so excited lol.
I wrote my first simple PHP script, which checks the required fields.

Could you please check and let me know if it is correct or i should change something??
I would appreciate any advice.

[php]

<?php //PHP CHECK AND DISPLY FROM STARS if(isset($_POST['submit'])){ if(empty($_POST['name'])){ echo "

Name field is required

"; } if(empty($_POST['email'])){ echo"

Email field is required

"; } } if(isset($_POST['submit'])){ if(!empty($_POST['name']) && !empty($_POST['email'])){ echo "Hello "."".htmlentities($_POST['name']).""." Thank you for contacting. This is your email:
"; echo "".htmlentities($_POST['email']).""; } } else{ echo""; } //PHP CHECK AND DISPLY FROM ENDS // HTML FROM STARTS echo""; echo"Name:
"; echo" Email:
"; echo" "; //FORM ENDS ?>

[/php]

Thanks
KG

Hello

I’m very glad to see your enthusiasm with the PHP programming language and hope you enjoy it for years to come. Upon looking at your code, it’s not bad for a beginner. There are a few things that you can condense to make it look better and avoid repetition. Below is your code fixed up a little. Please note that different people will have different styles of code and if you like my style you can stick with it or if you see something else on the internet, use it; it’s what ever makes you comfortable.

[php]<?php

if($_POST[‘submit’] && $_POST[‘submit’] != NULL)
{
if(!empty($_POST[‘name’]) && !empty($_POST[‘email’]))
{
echo “Hello “.””.htmlentities($_POST[‘name’]).""." Thank you for contacting. This is your email:
";
echo “”.htmlentities($_POST[‘email’])."";
}
else
{
$err = (empty($_POST[‘name’])) ? ‘

Name field is required!

’ : ‘’;
$err .= (empty($_POST[‘email’])) ? ‘

Email field is required!

’ : ‘’;
echo $err;
}
}

?>[/php]

The rest of your code looks fine how I see it. My code should work the same as your code but it’s a little bit more organized and easier on the eyes. Let me know if you need other assistance!

Cheers!

Hello sensei OpzMaster ;D

Thank you so much for your quick reply. Definitely I can see your code is much neater. I wish I had 30% of your knowledge about PHP lol. No seriously i am working really hard to learn it. I keep forgetting what i learn :frowning: I need a lot of practice. I know there a many websites that teaches php but i can’t find any website that provides with good PHP exercises. IF could please tell me where is the best place to learn and PRACTICE php??

Again thank you sir for you help.
I’ll be back soon for more help lol

The best way to learn is practice. Think of what you want to do, attempt, debug, try again etc. The PHP Manual at PHP.Net is a great reference source for functions. If you still get stuck after that, post here and we’ll point you in the right direction.

Hello again!

I went through your codes a few times, i never seen this before, i think its little advanced for me. Could you explain the last part please. why do we have dot [size=18pt].[/size] in fornt of second eaqul sign?, why do we have question mark [size=18pt]?[/size] after empty() fuction? and these [size=18pt]: ’ '[/size] at the end?

[php]

else
{
    $err = (empty($_POST['name'])) ? '<p style="color:red">Name field is required!</p>' :  ' ';
    $err .= (empty($_POST['email'])) ? '<p style="color:red">Email field is required!</p>' : ' ';
    echo $err;
}

[/php]

the . tells the script to add it onto the end of an already existing variable.

Hello,

Basically those two lines are called ternary conditional operators. Basically it’s just like the if/else but a little bit cleaner. Sorry for providing something more advanced than you might have liked, but once you understand what it’s used for, you may like it much more than the one-liner if/else. Here’s it broken down…

This:
[php]if($cond == TRUE)
{
echo ‘Do This…’;
}
else
{
echo ‘Do That…’;
}[/php]

…can be written in the ternary conditional operator shorthand:
[php]echo ($cond == TRUE) ? ‘Do This…’ : ‘Do That…’;[/php]

Basically the syntax for the ternary conditional operator is:
php ? [IF TRUE] : [IF FALSE][/php]

Hope this explains it.

Cheers!

Sponsor our Newsletter | Privacy Policy | Terms of Service