Check Order status | Verified Sales | Escrow Service | Advertise
westernunion carding gold carding
revolut carding electronics carding
paypal carding payoneer carding
advertise on cracking forums

Programming and Problem Solving in PHP Tutorial #2: Conditional Basics

Prince

[ Verified Seller ]
Staff member
Trusted Seller
Joined
10 yrs. 6 mth. 9 days
Messages
5,386
Reaction score
18,373
Age
44
Wallet
11,590$
The first thing to notice is that in the last tutorial the code had a lot of semi-colons ";" this is a symbol used by the interpreter to determine where the end of each command is. Generally speaking(although there are exceptions which we will touch on) every line should end with a semi-colon, you can also stack two commands onto a single line like this:

Code:
Please, Log in or Register to view codes content!
If anyone was expecting that last echo to say world, im sorry to disapoint but thats just a little toooooo cliche. the previous segment of code, to the interpreter, is identical to:
Code:
Please, Log in or Register to view codes content!
and

Code:
Please, Log in or Register to view codes content!
note that although you CAN stack commands onto one line, unless you have a specific reason to you should avoid doing such, even though it will execute fine on the computer end, if you or anyone else has to actually maintain the code it can make things a lot more painful.

Another convention that Im going to do everything I can to drill into you guys is proper indentation. I dont give 2 fucks if you think you can obfuscate your code by leaving it unindented and sloppy, trust me a half retarted monkey could reverse just about any of the obfuscation ive seen in skiddy php shells and scripts, all it serves to do is make you look like you dont know what the fuck you're doing and like you don't belong anywhere near a modern computer. When you have a script of hundreds, thousands, or even millions of lines of code that you have to maintain and manage, INDENTATION IS KEY. say it again because its that important. INDENTATION IS KEY. Until we touch on conditional statements later in this tutorial I won't be able to give much of a concrete example so this is all ill say on indentation for now.

You may have noticed in the code examples that they always begin with <?php and end with ?>, these tags tell the php interpreter what to process(since you can splice php code among html you can have a script entirely in html with a single <?php include 'menu.php'; ?> inside the html, so these tags exist to tell the interpreter exactly what to process and what to ignore.) There is short tags available as well which let you use simply <? and ?>, this is a setting that needs to be changed in the php.ini configuration file, but regardless, <?php ?> should work no matter what.

One more thing to catch up on before we dig any deeper. PHP is a recursive acronym, it stands for PHP Hypertext Preprocessor. The key word we're looking for here is pre-processor. If any of you have written in C or C++ you may be familiar with the concept of a preprocessor, its what takes all the #include files(like #include <iostream> and #include "stdlib.h") and embeds those libraries into your current file. PHP also has this feature available so you can include libraries and scripts into new applications you write(this may seem like a somewhat complicated concept at the moment but trust me its pretty simple and makes scripts way more reuseable) with this in mind we have 4 ways of including external scripts into our main script

For more information on Preprocessors:
Please, Log in or Register to view URLs content!


Code:
Please, Log in or Register to view codes content!
as you can see they're all quite similar ill start first with the differences between include and require

basically if you require a file and its unavailable, the program will stop execution and throw you an error saying that you're a real jerkface telling it to look for files that don't exist. If you use include it will continue execution and proceed but without the code that you intended to include so it very well may break down if any of that code is directly referenced or you may end up with a website that only half renders, etc... but the key difference between require and include is that with require if its not there its not loading.

now to touch on require_once and include_once, they both have the same differences that require and include have. the _once indicates that if in your script you have several references to a file and require or include it in more than one spot, to reduce overhead it will check and only actually load that file into memory once and use that location for all future references, instead of loading the same library into memory multiple times.

That being said, I almost exclusively use require_once, it ensures that overhead is limited and that my script won't errenously proceed without its requirements. However you should assess each situation and determine which is the most appropriate for your specific needs.

OK so we're caught up and I think we're ready to move onto something new. For once we'll get into some real actual coding... This next part is going to be broken into two parts, as PHP is primarily used in two ways: as a web application scripting language and as a command line scripting language. So we'll run through a few examples of how to obtain user input through both the command line as well as through using html forms on a web page.

We'll start off by doing it using the command line which may seem a little bit more complicated but by and large is a lot less work than creating a web page to read the info and send it off to be processed.
Code:
Please, Log in or Register to view codes content!
so what we've done here is created a variable named $input, we then prompt the user to enter their name(notice the print "Enter your name: " doesn't have the \n character in it. This means that when the user enters text it will appear directly to the right of the prompt instead of the line below it) Then we come to the line: $input = trim(fgets(STDIN)); in this line we are assigning the result of trim(fgets(STDIN)) to the variable $input. trim() is a function used to remove whitespace and endline characters as well as a few others. The reason for this is if they enter "Harold" and hit enter, the value of input will be "Harold\n" so if you print $input it will jump a line, trim removes the \n and leaves us with just the data we want. fgets(STDIN) extracts the data from the standard input buffer(in this case whatever was typed on the keyboard) Thats it, this is really all you need to know to get started with command line scripts and user input.

Obtaining input from a web form takes a few more lines of code so i'll write out an example first then well go through it piece by piece.
Code:
Please, Log in or Register to view codes content!
NOTE**: the line of an if statement does NOT require a semi-colon, just if(condition){} each line within the {} requires a semi-colon but the if statement itself does not.

Theres a few things in this code that we haven't taken a look at yet so I'll give a brief overview here but we'll go more into detail on them later.

We see here says: if(isset($_GET['sentinel']))

What we're really telling the computer is that if the variable $_GET['sentinel'] exists then we want to execute the following indented block of code, in this case a single print statement. If that variable does not exist we want to skip that line and instead execute the other block of indented code which presents an html form to the user asking for their name. This is what we refer to as a control structure, the concept of if/else clauses allows us to create several alternative courses of action that will be executed depending on which conditions are met, in this case the condition is the existence of the $_GET['sentinel'] variable. We call these kinds of operations "Conditionals" or "Conditional Statements". Why do we care if $_GET['sentinel'] exists? lets take a closer look at the second block of code


Code:
Please, Log in or Register to view codes content!
if you're familiar with html most of this should be pretty straight forward, by using the GET method we're basically saying "Send these variables in the clear via the URL" and you'll notice when you submit the form the URL changes to something like:
Please, Log in or Register to view URLs content!

Trailing from the URL are the values: name=Brian&sentinel=1 notice in the Form we have two input fields name and sentinel that correspond with these, so when the form is submitted two variables are being created: $_GET['sentinel'] and $_GET['name'] if these don't exist we can safely assume the user has not submitted the form, therefore we want to display the form to get them to fill it out. However if these variables do exist we can reasonable assume they have filled out the form and instead want to display the results back to them. Technically there are 3 input types, the submit is one as well but fuck submit thats irrelevant.

If you want to submit form data but have it not appear in the URL(say someones logging into a website, sending the username and password clearly in the URL is obviously a bad idea) then you can use the POST method instead of GET, all you have to do is substitute method="GET" for method="POST" and prefix your variables with $_GET instead of $_POST. This is also an example of a self-contained script. We wrote the input and processing stages all to one file. But what if we need to send the data from a form to a different file? the action="" determines where to send the request, we have sent it to $_SERVER['PHP_SELF'] which is a variable that contains the name of the file we're using so basically it says "send it back to me, ill reload and re-evaluate" but you can instead put in any file name you like.

So now we've got an introduction into some basic user input, as well as conditional statements. But conditionals, as we've seen them thusfar, seem kind of limited don't they? right now we can only choose between two options, if this do this otherwise do that. For some problems this will be sufficient but its not hard to see this being insufficient for more complex problems. So lets look at the If/Else claus a little more thoroughly and look into a concept known as nesting.

Nesting is the concept of inserting one block of code into another block of code, for example the form in the previous example was nested under the else statement and the print was nested under the if statement. Lets write a simple script to determine if its morning or afternoon, as well as if its a weekday or if its the weekend:

Code:
Please, Log in or Register to view codes content!
Alright so a few things we can ignore here are basically the date() function, it's beyond the scope of this tutorial but I've included in the comments exactly what data is being sent to those variables to give you an idea of what its doing.

In this code we have an if else block embedded inside of the if else block, this way we can create even more potential flows of execution. A few other things that we havn't seen before are || and == we'll discuss || and some other corresponding operators later on but for now lets focus on ==. Remember in the previous tutorial when I said to make a note of one = sign meaning assignment? e.g. $var1 = 5; assigns the value of 5 to $var1, when we use two equal signs its to compare the values of two items, so:
if($day == "Saturday" || $day == "Sunday")
is checking to see if $day is equal to the value Saturday or if $day is equal to the value Sunday(|| is the logical or operator, if $day is equal to either of those values, the condition returns true and the block of code is executed, only if each condition is wrong will it continue on to the else) so basically the first thing we're checking is if its a weekend or a weekday, then outputting those results, from there we check if its morning or afternoon then output that as well. You might notice that the outer blocks use {} brackets to contain their blocks whereas the internal if/else statements don't. This is a matter of preference but to break things down:

if there is ONE and only ONE line of code, you may omit the brackets(still be sure to indent so that its readily apparant, if you dont indent it will still run perfectly fine but could be a bit boggling to read) if there is more than one line of code you MUST i repeat MUST use {} brackets to contain it. Heres why:

say you have 3 or 4 conditions you want to check but only have an if/else and nothing to say this or this or this or this(note i dont mean this || this || this i mean if this else if this else if this else this)
 
Paid adv. expire in 2 months
CLICK to buy Advertisement !
westernunion carding Verified & Trusted WesternUnion | MoneyGram | Bank - Transferring [299$ BTC for 2000$ WU]
electronics carding Verified & Trusted Electronics Carding, Carding iPhone, Samsung Carding, MacBook Carding, Laptops Carding
Top Bottom