![]() |
|
PHP GUIDE - Printable Version +- BPC Squad Bank Paypal And Cards | Carders Forum | Carding Forum (https://www.bpcforums.biz) +-- Forum: Coders Section (https://www.bpcforums.biz/Forum-coders-section) +--- Forum: PHP Programming (https://www.bpcforums.biz/Forum-php-programming) +--- Thread: PHP GUIDE (/Thread-php-guide) |
PHP GUIDE - redred - 08-17-2016 PHP BEGINNERS GUIDE All the basics you need to get started! Contents
Introduction What's PHP? PHP: Hypertext Preprocessor (PHP) is a free, open source, scripting language that is made primarily for the creation of dynamic websites. It is compatible with almost all modern servers and PHP scripts are always executed on the server. I would suggest a simple understanding of HTML which you can get a short taste of in this thread. If you want a more in depth look into CSS and other aspects of site creation (which I recommend you do before diving head first into PHP) take a visit to the w3schools HTML tutorial. This can be accessed here. I will not be covering any HTML elements within this tutorial. What can it do? PHP has a huge collection of uses such as:
Getting Started Installing To get started with learning PHP you first need to install or buy a web server to host the files on. For this tutorial we will be using XAMPP, this is a free tool which allows for an easy set up of a local web server. Download and install XAMPP from here. Once you have XAMPP open up the control panel and click on start for Apache and MySQL, these are the only 2 modules we will be needing. Other than this you will also need a text editor for creating the PHP files. The two most common editors are Sublime Text and Notepad++. I personally use Sublime Text however both are good choice and you can get Sublime Text from here and Notepad++ from here. Now that's all done we should probably check if your XAMPP is working. To do this we will make a very simple script just to check. PHP Code: <?phpCongratulations! You're now one step closer to becoming a PHP pro! Before continuing (as requested by Kedox) I'd suggest reading this. It is the PSR-2: Coding Style Guide and will stop you from getting into bad habits while creating scripts. Making functional code is for the users benefit, you should make beautiful code for not just other developers who see your work, but also for yourself. Basic Syntax PHP Tags All scripts start and end with these tags: PHP Code: <?phpHere is an example where I have used a function called echo to print out "Hey" on to a web page. PHP Code: <?phpEcho PHP has a built in function called "echo" which has the job of outputting text. It would be a mistake to call it a function as it is actually a language construct, this means it does not require parentheses. As with all PHP statements, the echo construct must always end with a semi-colon ";" so it is clear where each line ends. We've already had two examples in this tutorial where we have used echo so go back and have a look at how they were written. All the outputs of echo are in plain HTML meaning you can use HTML tags within it. PHP Code: <?phpComments A comment is a line or section of code that is not executed as part of the program. Comments serve the role of communicating to others and your self how certain bits of your code work or what they are used for. The first kind of comment is a single line comment. To begin one of these you simply do // and anything on that line after the slashes will not be executed. The other option is a multi-line comment, these are started with /* and end with a */. Anything between this two will not be treated as code. Let's have some examples of comments so you can understand exactly how they look in practice. PHP Code: <?phpThat's all the most basic syntax out of the way. These were beginner constructs that you will need to know but don't fall into any other categories of the tutorial. It is also important to note that these constructs can't be changed. However, now you should be able to write a basic PHP script. Variables Regular Variables Variables are containers which we use for storing information. PHP variables work a bit differently to other languages so I suggest you pay close attention here. A variable starts with a dollar sign ($) which is then followed by the name of the variable. That is then followed by a = sign and the value is entered after that. After the value you need a semi-colon as usual. Let's have an example. PHP Code: <?php
You can also use variables in other functions or constructs instead of directly typing in data. This comes in handy when you will be using a value more than once and don't want to have to retype it and/or when the value of it will be changing often. For example: PHP Code: <?phpConstants Now I did make a rant thread (disguised as a tutorial) on constants a while ago and I do think they aren't used enough by beginners these days. But you don't care about that. A constant is similar to a variable except it is defined once then cannot be undefined or have its value changed. The same rules for making a constant variable name apply here. To create a constant you use the define() function. PHP Code: define(name, value, case-insensitive) Parameters:
PHP Code: <?phpExample using case sensitive: PHP Code: <?phpYou can also define constants by using the const variable. This works in exactly the same way but is always case-sensitive and is defined a little differently. PHP Code: <?phpData Types A data type is the type of information stored within a variable. PHP has 8 main data types listed here:
PHP Code: <?phpNext is a string where is a "string" of characters. It can be any text within a set of quotation marks (double or single). You can also join two strings together using a dot concatenation operator. PHP Code: <?phpA float (floating point mumber) is a numeric variable like an integer except it includes a decimal. PHP Code: <?phpA boolean is a variable that can be represented in two possible states: TRUE or FALSE. PHP Code: <?phpThe other variables are a bit advanced for now and will be covered later in the tutorial. It's good to remember that most variables can be combined with each other and we will go into more detail about this later. Variable Scope The scope of a variable is the part of the script your variable can be referenced or used. There are three types of scope: local, global and static. A variable declared outside of a function has a global scope but can only be used outside of a function. Variables declared inside of a function with have a local scope and can only be used inside of this function. We will go into parsing local variables into global later using the return function. PHP Code: <?phpArithmetic Arthimetic operators work with numeric values to perform common arthimetic operations.
PHP Code: <?phpAnd if you are confused to what exponentiation is, it is simply what you get when you raise $a to the power $b. For example: PHP Code: <?phpPHP Code: <?phpPHP Code: <?phpAssignment Assignment operators work with numeric values to write values to variables. PHP Code: <?phpPHP Code: <?phpComparison Comparison operators allow you to compare two variables. They will return a boolean answer of TRUE or FALSE. They are usually used inside of control structures which we will explain more about later. As for now, here is a list of all the comparison operators:
Logical operators are used to combine conditional statements. Bellow is a list of all of them:
Arrays Numeric Arrays An array is a special type of variable that is capable of storing multiple values. Numeric arrays give a numeric index to all of the values within the array. The first index of an array is always 0 and there are two ways to define them: PHP Code: <?phpPHP Code: <?phpAssociative Arrays Associative arrays are similar to numeric arrays. However, the values in the array are given named keys that you assign to them. There are two ways to create an associative arrays: PHP Code: <?phpPHP Code: <?phpMulti-Dimensional Arrays A multi-dimensional array is an array that contains one or more arrays. The dimension indicates the amount of indices you would need to select an element within in. For a two dimensional array you would need two indices to access it and for a three dimensional one you would need 3 and so on. Arrays more than 3 layers deep are very difficult to handle so I would suggest staying away from them. The arrays inside of the multi-dimensional arrays can be either numeric or associative. Let's create a two dimensional array: PHP Code: <?phpPHP Code: <?phpControl Structures If Else Statements Conditional statements perform different things for different states. The if else statement runs a piece of code if a condition is true and other code if the statement is false. The if can also be done by its self, if the statement is not true then nothing will happen and the program will simply continue. Let's have a look at an example: PHP Code: <?phpPHP Code: <?phpElseif Statements You can use an elseif statement as a continuation of your already existing if statement. They specify a new condition to test and can be used as many times as needed. Here is the syntax for them: PHP Code: <?phpPHP Code: <?phpWhile Loop A loop allows you to run the same snippet of code multiple times with relative ease. This can be amade extremely helpful tool however is where many beginners start to make logic errors. The first loop I will show you is the while loop, it is very similar to the if statement but instead if checking a condition once, it will continue to do if until the condition is false. The syntax is very simple: PHP Code: <?phpNow let's have a look at an actual code example: PHP Code: <?phpPHP Code: <?phpPHP Code: <?phpFor/Foreach Loop A for loop is used when you know how many times you will need to loop the code in advance. Within them you first need to initialise a loop counter, then you need a condition to be checked so the program knows when to exit the for loop. You also need an increment to change the counter variable every iteration of the loop. PHP Code: <?phpPHP Code: <?phpPHP Code: <?phpSwitch Statement A switch statement allows for, what would be, a long list of elseif statement but in a more optimised and clean manner. You give the switch a variable and then make a case statement for each value you want to check for. You end each case with a break statement and you can also use default as something a lot like the else command. Let's have a look at a quick example of a switch statement. PHP Code: <?phpTernary Conditionals Ternary conditionals can allow for more compact and easy to read code by letting you do simple if/else statements on the fly. They have a syntax like this: PHP Code: <?phpPHP Code: <?phpInclude and Require The include control structure allows you to insert a php file into the contents of another. These can be useful when a script (such as one connecting to a database) needs to be run a large amount of times. Copying out all the code would require a large amount of time and wasted space. With include you can simply run a single line to take care of something like connections. They have a syntax of: PHP Code: <?phpFunctions User Defined A function is a block of code that needs to be used multiple times throughout your code. They aren't executed upon loading of a script but instead when they are called upon. A user defined function starts with the term function followed by the name, brackets and curly brackets. The code for the function goes inside the curly brackets. To call upon the function you use the name followed by brackets. PHP Code: <?phpParameters The brackets that come after the function name are used for parameters. They allow you to pass values through a function. When defining your function you must put the name that you wish to referace the variable by into the parentheses. When calling your function you use this space for what you wish the parsed value to be. Multiple parameters can be used and are separated by commas. Let's see an example where a function multiplies two numbers. PHP Code: <?phpReturn A function can return a value using the return statement. The statement stops the function and send a value back to the calling code. Multiple values cannot be returned so if you wish to do this I would suggest storing the variables you wish to return anymore than 1 value. Let's use our parameters example and add a return statement. PHP Code: <?phpPredefined Variables Explanation PHP comes with a wide collection of built in variables that allow you to do various things. These include things such as PHP Forms, GET, POST, $_COOKIE and a variety more. I feel these are a more intermediate level subject so I will be posting this thread with the following section containing links to the documentation for all the most important of these pre-defined variables. For a complete list of all the pre-defined variables you should check the docs here. List I'd recommend having a look at all of these but only once you get your head around all of the more basics concepts of php.
What Next? So you've reached the end of my PHP beginner mega tutorial and for this I congratulate you. My suggestion is that you find a project and stick with it, by doing this you'll be able to build your knowledge to a greater extent but in a way it will stick with you. Your first project is going to be sh*t. No exceptions. Don't go out trying to make the next Facebook right away but instead try a smaller simpler idea, it will still suck but it will be a much more enjoyable learning experience. Useful Links
Final Notes Thanks for reading my tutorial on PHP. This is a guide I wrote a while ago and I'd like to post it here. RE: PHP GUIDE - gsergiog11 - 10-15-2016 the best tutorialllllllllllllllllllllllllllllllllllllllll v: |