Beginners PHP - Variables

9 posts Page 1 of 1
Contributors
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Beginners PHP - Variables
smashapps
hey guys,

I see quite a few PHP tutorials here but not many aiming at the very most basics. I just want to talk a little about variables.

The different types of PHP variables are:
Boolean
Integer
Float/Double
String
+ a few more but these are the most common
Code: Select all
<?php
	$name = "SmashApps"; //String
	$age = 19; //Integer
	$birthday = 19.02; //Double
	$LikesCoffee = True; //Boolean
?>
In PHP when you create your variables you don't need to declare the type because this is done automatically. If you have an integer and change it's value to a string, that data type is automatically changed.

You can manually specify the variable type when you declare it by adding this:
Code: Select all
<?php
$name = (String)"SmashApps";
?>
You are the type in brackets before the value.

In PHP you can return the variable type like so:
Code: Select all
<?php
$name = "SmashApps";
echo "The variable \$name has a value of $name and it's type is: "; echo gettype($first);
?>
It would look like this in the browser:
The variable $name has a value of SmashApps and it's type is: String
If you are new to PHP and are using it to push a lot of HTML data, try avoid doing that as it can slow down how long it takes to load the page.

For example I have a style in my HTML Head in my PHP file, instead of using echo ""; on each line do this:
Code: Select all
<!DOCTYPE html>
<head>
	<title>Hello World!</title>
	<style type="text/css">
	body {
		font-family:"Calibri";
	}
	</style>
</head>
<body>
<?php
echo "Hello world!";
?>
</body>
</html>
PHP is becoming more popular now since it's trying hard to be more Object-Orientated, also because it's a server side language none of the PHP code will be displayed in the client's browser.

If you want to program with PHP you will need a lamp/wamp server, most popular one for windows is xampp and is free to download: Click here to download Xampp

I recommend Xampp 1.8.2 not 1.8.3 since it's still in Beta and crashes a lot. If you want to use Xampp on your USB or External Harddrive download the .zip version of Xampp and install it to your portable device.

When working with PHP files you will need to start the Apache server, and the MySql server if you want to use databases. The website files go into c:/xampp/htdocs/, this will depend on what drive you install it on though but it's always xampp/htdocs/

To view the files in the browser you will need to navigate to locahost in the browser, also Apache runs on ports 80 and 443 so you will need to be aware of that, if you have an IIS server this usually runs on port 80 too, close IIS or change the conf file of Apache to run on another port, also a lot of programs run on port 443 too, namely vmware. Just close it if you have anything running on that port.

I know this tutorial was for variables but thought I would try and give more information aimed at helping people new the PHP. If you liked my tutorial let me know, if there was anything I was wrong about or need to change anything please let me know :) thanks for reading guys
Last edited by smashapps on Sun Feb 09, 2014 2:49 am, edited 1 time in total.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
benji_19994
VIP - Donator
VIP - Donator
Posts: 156
Joined: Mon Apr 16, 2012 3:13 pm

Re: Beginners PHP - Variables
benji_19994
+Rep for you nice tutorial
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Beginners PHP - Variables
smashapps
Thanks #benji_19994 and everyone else who gave me a +rep! I love dem reps. lol
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: Beginners PHP - Variables
Shim
It would be worth it if you could show examples for integer, boolean etc?
Find my programs on Softpedia
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Beginners PHP - Variables
smashapps
Updated thread
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: Beginners PHP - Variables
Shim
Thanks, PHP is good in this thing; we don't need to set the type of Variable! +REP
Find my programs on Softpedia
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Beginners PHP - Variables
smashapps
Thanks #Shim
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: Beginners PHP - Variables
XTechVB
Good tutorial! this is very useful for beginners and apparently for me :D i didn't know that you can declare variables with a specific type (String)"SmashApps";
You can find me on Facebook or on Skype mihai_92b
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Re: Beginners PHP - Variables
smashapps
Yeah I learnt it in my PHP class on campus! I will definitely be writing more PHP tutorials, #benji_19994 and I are both writing them together.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
9 posts Page 1 of 1
Return to “Tutorials”