Welcome, Guest |
Welcome to Makestation! We are a creative arts/indie discussion community — Your center for creative arts discussion, unleashed!
Please note that you must log in to participate in discussions on the forum. If you do not have an account, you can create one here. We hope you enjoy the forum!
|
|
tc4me
December 17th, 2024
Blablablabla
|
|
tc4me
December 17th, 2024
Blablablabla
|
|
tc4me
December 17th, 2024
Blablablabla
|
|
tc4me
December 17th, 2024
Blablablabla
|
|
tc4me
December 17th, 2024
Blablablabla
|
View all updates
|
Online Users |
There are currently 473 online users. » 0 Member(s) | 470 Guest(s) Applebot, Bing, Google
|
|
|
Comments |
Posted by: Damian B. - August 13th, 2013 at 8:11 PM - Forum: Community Related
- Replies (9)
|
|
I'm pretty sure there's a problem with the plugin? Or is it custom made?
Because i didn't get a notice although someone did add a comment on my profile.
Thanks!
|
|
|
PHP - Part 4 |
Posted by: Darth-Apple - August 13th, 2013 at 4:29 PM - Forum: Software
- No Replies
|
|
How to Program PHP
Tuturial, part 4
So far, we've explored several fundamental concepts in programming. We've explored the echo statement, and how a PHP file needs to be structured in order for the server to run it. In the second tutorial, an overview was given on how to use variables and arrays. In the third tutorial, functions were introduced. Functions are an extremely fundamental concept in PHP, but it can take quite a while to get used to them. If you're still a little confused, keep reading. All of this will become more clear as you continue working through these tutorials.
In this tutorial, I will introduce the concept of conditional statements. Conditional statements allow us to run different parts of our code, depending on whether a certain condition is met. For example, what if you had a script that denies registration and leaves a message for the user if the user is under 13? Many forums for example are required to deny registration to users under 13 due to COPPA restrictions in US law, so this is a popular real-world example.
Now, you have some code that echoes "sorry, but users must be at least 13 years of age to register," but you need to actually check to see if the user is at least 13 years of age before you execute that code. How exactly would you do that?
PHP has a set of "conditionals" that allow you to check for certain conditions before executing a block of code.
Code: if ($condition == 1) {
// execute this code
// this IF statement only executes the code in between the curly brackets, and it will only execute this code if the $condition variable is equal to 1.
echo "executing the IF statement code block now. ";
}
The code above won't actually do anything, but it does demonstrate that the code between the curly braces will only execute if the $condition variable is equal to one.
TIP: Notice carefully how there were two equal signs used in the IF statement for ($condition == 1). Why did we need two instead of one? Turns out = and == are different operators in PHP. if you just use =, it will try to set the $condition variable to 1 which obviously is not what we want. If you use ==, instead of trying to assign a value to the variable, it will check its existing value, which is obviously what we want. Generally, you will always use == instead of = in conditionals for PHP.
Now, the code inside of the IF statement will only execute if the $condition variable is equal to 1. What if we want to execute a separate block of code if the $condition variable IS NOT equal to 1? In this case, we'll use an IF statement, and then and ELSE statement.
Code: if ($condition == 1) {
// execute this code
// this IF statement only executes the code in between the curly brackets, and it will only execute this code if the $condition variable is equal to 1.
echo "executing the IF statement code block now. ";
}
else {
// this ELSE block will ONLY execute if the IF block does not execute. Basically, if $condition is equal to 1, PHP will execute the code inside of the IF block. If $condition is equal to anything OTHER than 1, it will execute the code in the ELSE block instead.
echo "executing ELSE block code now!";
}
As you can see, we've made use of the "else" block above. Simply put, the code in the "else" block will only run if the "if" block fails to run. If $condition is equal to anything other than 1, the else block code will run. If the $condition variable is equal to 1, the "if" block code will run. In no circumstance will both blocks of code ever run together.
So, what if it turns out that you need to execute a block of code if $condition = 1, a separate block of code if $condition = 2, and yet another separate block of code if $condition is equal to anything else? Turns out PHP has the "IF ELSE" block for that.
Code: if ($condition == 1) {
// execute this code
// this IF statement only executes the code in between the curly brackets, and it will only execute this code if the $condition variable is equal to 1.
echo "choice 1 has been selected! ";
}
if else ($condition == 2) {
// this block of code ONLY executes if the $condition variable is equal to 2.
echo "choice 2 has been selected! ";
}
else {
// this ELSE block will ONLY execute if the IF block does not execute. Basically, if $condition is equal to 1, PHP will execute the code inside of the IF block. If $condition is equal to anything OTHER than 1, it will execute the code in the ELSE block instead.
echo "Oops! We don't recognize your choice selection. An error has occurred. ";
}
As you can see, the first IF block only executes if $condition is equal to 1. The second "IF ELSE" block only executes if $condition is equal to 2. The last "ELSE" block only executes if neither of the previous two blocks of code executed. In other words, "ELSE" executes if your network of IF and IF else statements never "found a match".
There are actually other conditional statements in PHP as well. The "switch" statement is fairly popular, and there are try and catch statements as well. These aren't very important to the purpose of our tutorials and aren't essential to learning the basics of PHP, so we won't attempt to go over these in detail.
Anyway, back to the original script concept for denying user registration to users under thirteen...
Code: if ($age < 13) {
echo "Sorry, but users must be at least 13 years of age to register. ";
// the < operator is the "less than" operator for PHP.
}
else {
processRegistration (); // runs a function that processes the user registration.
// This only runs if the user is 13 or over. If the user was under 13, the IF block above would have run instead of this block of code. Remember, in no circumstance will the "ELSE" block ever run if the IF block has run.
}
If you would like to play around with these scripts, copy and paste the code below and give it a try.
Code: <?php
// play around with the following variable
$age = 12;
if ($age < 13) {
echo "Sorry, but users must be at least 13 years of age to register. ";
// the < operator is the "less than" operator for PHP.
}
else {
processRegistration (); // runs a function that processes the user registration.
// This only runs if the user is 13 or over. If the user was under 13, the IF block above would have run instead of this block of code. Remember, in no circumstance will the "ELSE" block ever run if the IF block has run.
}
?>
In the next tutorial, we will explore how HTML forms are set up and how we can design the first interactive applications. The fun stuff will be beginning soon.
Stay tuned!
Next Tutorial: Let the fun stuff begin; forms and PHP introduction
Directory of PHP Tutorials
|
|
|
What are your goals |
Posted by: Tyler - August 13th, 2013 at 5:19 AM - Forum: General Discussion
- Replies (3)
|
|
What are your goals for your websites, forums, blogs ect?
I'd love to hit 50 members and than 100, I think 100 people is still a good amount even though some of these forums hit thousands, I'd be happy with 100. I'd also like to hit 1,000 posts. (my goals for now) if my forums is successful I'd love to hit 1,000 members and 100k posts. I'd also like to learn more on how to make my forums better (as this was my first time making a forums)
|
|
|
|