nV News Forums

 
 

nV News Forums (http://www.nvnews.net/vbulletin/index.php)
-   Software Development (http://www.nvnews.net/vbulletin/forumdisplay.php?f=53)
-   -   Official Learn PHP/MySQL Thread (http://www.nvnews.net/vbulletin/showthread.php?t=117006)

ViN86 07-29-08 09:58 PM

Official Learn PHP/MySQL Thread
 
Well, I would like to introduce everyone here to the wonderful world of PHP and MySQL. Since DOC and some others said they would like to learn about this very simple language, I would like to teach him and the others about it and how easy it is to use. :D

Let me first say that I am not an expert PHP programmer. However, I have written many web applications using PHP integrated with MySQL. I also have a good knowledge of the basics of the language. I will cover security (SQL injection, XSS, etc.) but again, I am not an expert. That being said, I am open to suggestions and new methods. So feel free to contribute freely to the separate feedback thread, and please do not threadcrap.

The Feedback thread is here: http://www.nvnews.net/vbulletin/showthread.php?t=117028

This will require some basic knowlege of programming. Preferrably, C/C++/C# programming background would be ideal, but minimal programming knowledge will work just fine.

So, consider today to be Part 1 of this educational series that has no defined number of parts.


Part 1: Installing and Using PHP/MySQL/PHPMyAdmin - The Easy Way

I currently use a local Apache/PHP/MySQL server to do all my development. I will introduce you all to a couple tools that I use for development.

First, I like to use a program called WAMP (Windows Apache MySQL PHP) for my development. I always work offline (as should you) to protect the code. I like using WAMP because it's very simple. They have new releases quite often and the releases always contain the most up-to-date versions available.

WAMP Server can be downloaded free from http://www.wampserver.com/en/ and is licensed under the GPL.

Once the program has been downloaded, install the program. All the default settings will be good for just starting. If you use Firefox, make sure you set Firefox as the default browser (it will notify you). Once the program has been installed, you can start the server by going to the Start Menu and selecting WampServer -> Start WampServer.

When it is loaded, you will see the following in your taskbar when you left-click on the icon. Right-clicking on the icon will bring up a menu to Exit the program.

http://img237.imageshack.us/img237/8...enuwampdf8.gif

This toolbar gives you access to all the required settings and everything else you may need for running/managing your local server. On the top are localhost and phpMyAdmin. The localhost option will open a browser/browser-tab and bring you to your local Apache server. The phpMyAdmin option will open your browser and bring you to the phpMyAdmin main menu. This program is a web-based application that allows you to easily (read: graphically) manipulate your MySQL database.

The www directory option will open up explorer and bring you to where your files are stored for the Apache server. This is where you will place your files to be hosted (ie .php, .html., etc.). The Apache/PHP/MySQL options below that are where you can enable/disable plugins and change settings for your server. Below that are the options to Start/Stop/Restart the services. NOTE: Apache and MySQL will run as services on your machine, and can be viewed in the Services window in Administrative Options in the Control Panel.

Finally, the Put Online option is to host the server live (online). This will accept incoming connection requests on port 80, assuming that your router is correctly configured and your ISP allows it. For now, keep this option unchecked, since you will be doing local development.

Ok, now that we have an Apache/PHP/MySQL server up and running, now we need something to write the PHP code. Preferrably, a program that has colorized fonts for functions/variables/reserved words/etc.

Personally, I prefer to use Notepad++. This program is labeled as Free Software (no licenses apply) and can be found at http://notepad-plus.sourceforge.net/uk/site.htm.

Notepad++ is a great text editor program. I set it to be the default .php file handler and you can do the same if you want. I do all my development in a text editor. I rarely use a WYSIWYG graphical editor (like Dreamweaver) so I won't really cover it here.

Now that you have your required tools installed, you are ready to begin writing PHP scripts. To test your install, left-click on the WampServer icon and your browser should open with a screen that looks like the following:

http://img375.imageshack.us/img375/8290/95788353pa9.jpg

Well, that concludes this part. The next part will deal with the basics of PHP, including writing PHP scripts, adding them to your local Apache server, and some basics of PHP including variable declaration and retrieval, and how to output using PHP.

ViN86 07-31-08 11:38 PM

Re: Official Learn PHP/MySQL Thread
 
1 Attachment(s)
Part 2: PHP Basics - Writing Scripts, Viewing Scripts, and Storing/Retrieving Variables

This part will deal with the basics of PHP. In this lesson, you will learn how to write PHP scripts, view them on the server, store variables, and retrieve variables.

First, let's talk about what exactly PHP is. PHP is a server-side scripting language. It stands for PHP Hypertext Preprocessor (yes, it's a recursive acronym). The concept behind PHP is very simple. First, the script is written and a .php extension is added to the file. Then, when a client requests the file, the server recognizes the extension of the requested file. The file is then interpreted by the server, then output to the client. This means that the code contained in a PHP file is essentially "compiled" before it is output to the client, and the code is not viewable. However, the server does not do this to all the text in the PHP file. Instead, it only interprets the text that is surrounded by PHP tags. Therefore, it is possible to insert PHP code anywhere in the page. As long as the text is surrounded by PHP tags then it will be interpreted by the server.

PHP tags look like the following:
Code:

<?php
    //PHP code goes here
?>

The <?php tag tells the server that PHP code follows, and the ?> tag tells the server that it's the end of the PHP code. The // characters inform the server that that line of text is a comment, and that text is not interpreted by the server. In PHP, the server does not interpret single lines of code. Instead, it reads in the text until it reaches a semicolon ;. Therefore, statements may be more than one line, but must be terminated with a semicolon. Otherwise, PHP will report an error.

Before we start, let's cover the basics of variables and output in PHP. If you have experience with other programming languages such as C, C++, C#, etc., you know that when you declare variables you need to declare a variable type. Well, you will be delighted to know that while PHP has this ability, it is not necessary. PHP determines the optimal variable type based on the data being passed to the variable.

I will cover more on variable types, changing variable types, operators, and control structures in the next lesson. For now, let's look at how we declare variables and retrieve variables. After that, we will create a script, upload it to the server, and view the output.

All variables in PHP are preceded by the $ sign. PHP variable names may only consist of numbers, letters, and underscore characters (A-Z,a-z,0-9,_). Underscores and capitalization may be used to make variables with multiple words easier to read (ie newVariable, new_variable). The following code declares multiple variables.

Code:

<?php
    $intro = "Hello World!";
    $variable1 = "This is";
    $variable2 = 1;
    $variable3 = "new ";
    $variable4 = "variable.";
?>

In the above code, 5 variables are declared. Each of these variables has a different value. But what good is storing values if we can't retrieve them, right? To printout a variable, there are a couple different methods. For this lesson, we will use the echo command. Observe the following code.

Code:

<?php
    $variable = "This ";
    echo $variable;
    $variable = "is ";
    echo $variable;
    $variable = "the output.";
    echo $variable;
?>

This code would output This is the output. on the screen. The echo command prints out the variable in the line. It does not start a new line. To write a new line, you must use HTML and the <br> tag, which forms a new line. PHP can be written in line with HTML, as the following code demonstrates.

Code:

<html>
    <head>
          <title>My PHP Script</title>
    </head>
    <body>
            <?php
                    $variable1 = "Hello ";
                    $variable2 = "World!";
                    echo $variable1;
                    echo "<br>";
                    echo $variable2;
            ?>
    </body>
</html>

This will print out Hello and World! on two separate lines. When using the echo command, you can output values together using a period between variables. This tells the server to print each variable. We will use this in the following script.

Now let's create our first PHP script. First, load WampServer and make sure the icon in the system tray is white, indicating all processes are loaded. Open Notepad++ and type the following into the text editor.

Code:

<html>
    <head>
          <title>My PHP Script</title>
    </head>
    <body>
            <?php
                    $variable1 = "This is the first PHP script.";
                    $variable2 = "It is good.";
                    echo $variable1."<br>".$variable2;
            ?>
    </body>
</html>

Now you have to save it to your www directory, which is where WAMP looks to load files. Left-click on the Wamp icon and select the www directory option. This will open an explorer window and show where your www directory is located. This is typically "C:\wamp\www". In Notepad++, click File->Save As. Navigate to the www directory and type lesson2 in the filename box. In the Save as type: dropbox select the PHP option and make sure the extension is .php. Now click Save.

Now, left-click on the Wamp icon and select the localhost option. This will open a browser showing you your local server. Now, click the address bar and type lesson2.php at the end of the URL and press Enter (the URL should be http://localhost/lesson2.php).

The following should appear:
Quote:

This is the first PHP script.
It is good.
There you go, you have created your first PHP script. This concludes Part 2. The next part will deal with variable types, changing the variable type, operators, and control structures.

NOTE: I have attached the .php file so you can see it.

ViN86 11-23-09 10:43 PM

Re: Official Learn PHP/MySQL Thread
 
Part 3 - Variable Types and Operators

First off, I'd like to apologize for having been away from this tutorial for so long. I know a lot of people out there would like to learn PHP and have been talking about the thread, so sorry for the delay. This lesson will consist of mostly just information regarding variable types, getting/changing the variable type, and operators.

Let's start with variable types. For those out there accustomed to other languages (C/C++, C#, etc.) this will be mostly review. The following is a list of variable types in PHP:
  • Boolean - Represents a boolean value. Either 0 (false) or 1 (true)
  • Integer - Number of the set {... -2, -1, 0, 1, 2, ...}
  • Double - (aka Float) Number with a decimal point
  • String - A list of characters, these are typically words, sentences, etc.
  • Array - A vector or matrix consisting of other types of variables
  • Object - Defined by a class, typically contains values and functions (I will go into object-oriented programming and PHP later)
  • Resource - Special variable, holds a reference to an external resource

Now, for the regular programmers out there, you are probably used to declaring the variable type before setting the values. PHP is much easier and tries to guess the variable type you want. Many times PHP is correct, however sometimes it is useful to declare variables before using them. PHP does allow type casting. To do so, just place the type you want the variable to be in parentheses before you set the value. Below are some examples.

Code:

<?php

$val1 = 123;  // PHP sets $val1 as an integer
$val2 = (string) $val1;  // We instruct PHP to set $val2 as a string with $val1 as the value

?>

There are multiple casts allowed, including (int)/(integer), (bool)/(boolean), (float)/(double), (string), (binary), (array), (object), and (unset) which sets the variable to NULL. For strings, we can also put the value in quotes. For example, in the above, we could have done the following:

Code:

<?php

$val1 = 123;
$val2 = "$val1";

?>

PHP will interpret $val1 and recognize it as a string and store it into $val2. This is a useful feature, as it allows us to have variable variable names. (this was very useful in some of my projects).

Code:

<?php

$val1 = "var2";  // Creates $val1 string with value "val2"
$$var1 = 123;  // Creates variable $val2 with value 123

?>

There are also functions available to return string or integer values of a variable. These functions are intval and strval. They will return the integer or string equivalent of a value. We could also do the above via the following.

Code:

<?php

$val1 = 123;  // Stores 123 (integer) into variable $val1
$val2 = strval($val1);  // Stores string equivalent of $val1, "123" into variable $val2

?>

Finally, if we want to return the variable type, we can use the gettype function. This function returns a string denoting the variable type of the variable passed to it.

Code:

<?php

$val1 = "this will return string";
echo(gettype($val1));

?>

If we put the following in a PHP file and put it in our www folder in WAMP, the browser will output the following:

Code:

string
This is useful for checking variable types. Typically, PHP is very good at interpreting what you want to do. For example if we set a string value to 123, then add it to an int, we do get an int that is the sum of the two numbers.

Code:

<?php

$int = 123;
$stringint = (string) 123;

echo gettype($stringint);
echo "<br />";
echo($int+$stringint);

?>

The output will read:

Code:

string
246

Finally, I will cover operators in PHP. These are used to manipulate variables (e.g. numbers) as well as compare values and control logical statements. The mathematical operators are as follows:

Math Operators
  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus (returns the remainder)
  • ++ Increment +1
  • -- Increment -1

Assignment Operators
  • = Assign to value
  • += Add then assign (x+=y is the same as x=x+y)
  • -= Subtract then assign
  • *= Multiply then assign
  • /= Divide then assign
  • %= Take modulus then assign
  • .= Append and assign (good for strings)

Comparison Operators
  • == Equals (or equivalent to)
  • != Not equal to
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to

Logic Operators
  • || Or
  • && And
  • ! Not

These are the operators. If you don't understand their usage at the moment, we will cover them. Well that concludes this lesson. I know that I have not really explained arrays very well. Arrays are a very important variable type and are capable of storing almost everything you could ever need in a very simple and logical form.

Next lecture will cover arrays. After that, I will go over IF statements, FOR and WHILE loops, and how to create your own functions in PHP. Hopefully the next lecture won't be 15 months away :lol:


All times are GMT -5. The time now is 06:48 AM.

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.