|
|
|
|
![]() ![]() |
Nov 19 2007, 02:28 AM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 290 Joined: 5-October 07 From: Random Places Member No.: 51,171 ![]() |
You probably are thinking at this moment why I am posting some tricks with variables. Well, there are many useful things a variable can do for us. For one they can actually be used to create simple games. They also can be used to produce a random result in a page. Another great use is shoutboxes.
Well here are some cool variable effects. Adding Multiple Strings We can use strings to create great effects. Example: CODE <?php $var1="Hello person."; $var2=str_replace("person","world",$var1); echo $var1." ".$var2; ?> We used str_replace to show two sentences with strings. Neat huh. Random Integers This is one of the sweetest features of PHP. the rand() function. Here is a neat trick using this function. Example CODE <?php function exVar() { $num=rand(1,100); if ($num<100) { echo "The number is less than 100."; } else { echo "The number is 100."; } } exVar() ?> That will run the function and tell you if it is 100 or less. Great for guessing games. MySQL Connect With variables we make connecting to a MySQL database clean and simple Most mysql connection codes are like this. CODE <?php $localhost="localhost"; $name="username"; $password="password"; $database="db"; $connect=mysql_connect($localhost,$name,$password) or die(mysql_error()); mysql_db_select($database,$connect); ?> Simple huh! Variables make it look simpler and neater. Superglobals This is another awesome feature of PHP and variables. The $_POST and $_GET variables. We can use these to receive user input. We also can use the $_GET global to host a small website in ONE FILE. $_POST is very widely used in registration scripts and chatboxes. Example of $_GET CODE <?php if ($_GET['var'] == 'var') { //Post some html/php stuff here } else { //post the main page code here } ?> Example of $_POST CODE <?php $name=$_POST['var']; echo $name; ?> <form action="page.php" method="post"> <input type="text" name="var" /> <input type="submit" value="Submit" /> </form> The first code gives us a one file site using the $_GET variable and If statements. Our second code processes the form and prints the user input. Thank you for reading my tutorial. |
|
|
|
Nov 19 2007, 12:41 PM
Post
#2
|
|
|
Member [Level 2] ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 79 Joined: 18-October 07 Member No.: 51,724 |
Nice one there. Would you mind having me on your MSN so that we can chat about PHP here? I need some training with PHP as i am beginner only. So as if i have any queries, perhaps you can help me?
|
|
|
|
Jan 4 2008, 08:33 PM
Post
#3
|
|
|
Newbie ![]() Group: Members Posts: 5 Joined: 31-December 07 From: Rahim Yar Khan, Punjab, Pakistan Member No.: 55,536 |
Assalam-o-Alaikum!
(Peace be upon You) Hey it was great! |
|
|
|
Jan 6 2008, 04:18 AM
Post
#4
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 60 Joined: 25-February 07 From: Somewhere Your Not! Well... Maybe your here. Who Knows? Member No.: 39,197 |
Yeap another cool thing is this
CODE <?php $msg["welcome"] = "Welcome to our site"; $msg["GoodBye"] = "Take it easy... come back and see us"; echo $msg["welcome"]; ?> this is a good way to keep your message or your errors on a sepearte file to make it easy to call upon an error... although i did something completly diffrent than that above. This post has been edited by cwconline: Jan 6 2008, 04:19 AM |
|
|
|
Jan 6 2008, 04:19 AM
Post
#5
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 60 Joined: 25-February 07 From: Somewhere Your Not! Well... Maybe your here. Who Knows? Member No.: 39,197 |
Assalam-o-Alaikum! (Peace be upon You) Hey it was great! It's easy to save... just save it as a .php file so in words file.php |
|
|
|
Jan 10 2008, 10:43 PM
Post
#6
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 25 Joined: 8-January 08 From: PR Member No.: 55,961 |
The $_GET example isn't really clear... at all.
I'm not sure how to use it, so if anyone wouldn't mind explaining, I would greatly appreciate it. =] Oh, I would also like to add that I read somewhere that some $_GET commands can be dangerous because people can inject Javascript snippets to steal cookies or bother people. |
|
|
|
Jan 11 2008, 07:12 AM
Post
#7
|
|
|
apt-get moo ![]() Group: [MODERATOR] Posts: 2,153 Joined: 28-May 05 From: Devon, England Member No.: 7,593 ![]() |
$_GET is used to get information out of the address, which is passed to the PHP script. For example, look at the address for this topic:
http://www.trap17.com/forums/index.php?showtopic=53286 The ? indicates that PHP variables will follow. showtopic is the name of the variable, the = indicates its value is next, and 53286 is the value for that variable. You can access these variables with $_GET['variable_name']. So, in this example, in the PHP script you could use $_GET['showtopic'] to get the ID of a topic to look for. You could then use that value in a MySQL lookup to get the topic information. QUOTE Oh, I would also like to add that I read somewhere that some $_GET commands can be dangerous because people can inject Javascript snippets to steal cookies or bother people. Yes. If you are not careful and have made glaring security mistakes with your code, then people can start inserting whatever code they like - thus allowing them to run JavaScript or other stuff. Generally, as long as you use the right functions to sanitise any inputs, you will be fine. |
|
|
|
Jan 11 2008, 07:37 AM
Post
#8
|
|
|
|||[ n00b King ]||| ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 688 Joined: 20-June 07 From: Auckland Member No.: 45,102 |
This is one reason I try and not use any $_GET in my code. If I can I will always try and use $_POST so I wont have to deal with people trying to include their own code in there. I guess $_GET does have an advantage as it can be bookedmarked unlike $_POST. I think more experience will be able to tell you what you can and cant use. I probably will need to use $_GET sometime in the future but it hasnt come up just yet in my current project.
edit: Thinking about it abit I cant see how anyone would be able to inject some javascripte code into the URL. I mean unless you parse out that varname+value pair in the URL in your code it would be outputed right? Unless I'm missing something else obvious here how can someone do that? This post has been edited by sonesay: Jan 11 2008, 07:41 AM |
|
|
|
Jan 11 2008, 09:43 AM
Post
#9
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 786 Joined: 13-April 07 From: mreža Member No.: 41,558 |
Well honestly I have expected more from such name of tutorial however this is more or less just basic calculations with variables nothing more. I would also suggest everyone to check out sessions as this is one of the most usuful parts of php. And important thing to note is that when you are using any variable i the php you should also chec whether that variable is set due to the fact that php is not type strict language you must check whether variable is set and this must be obligatory job in the cases when you are passing one variable from one script to another. Also I would like to say a bit more what does type strict means. (Just for the record I think about php very highly however this is one thing I hate it.) Type stricts means that when you have some variable you must declare it with type for example int bool string as object char long double float and so on. However you do not have to do this in the php and because of that there are so |