Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1606 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

PHP Question

Thread title: PHP Question
Closed Thread    
    Thread tools Search this thread Display Modes  
03-05-2006, 11:13 AM
#1
Spawny is offline Spawny
Spawny's Avatar
Status: Request a custom title
Join date: Aug 2005
Location: Great Yarmouth, England
Expertise:
Software:
 
Posts: 1,321
iTrader: 2 / 100%
 

Spawny is on a distinguished road

Send a message via AIM to Spawny Send a message via MSN to Spawny

  Old  PHP Question

I am only just getting the hang of PHP, and i was getting confused about variables.

What is the point of them? I mean i know they are used to store data but wouldn't it be easier to just type in what you want?

Also, can you make an include file like "variables.inc.php" and then put all the variables you need in it, and then include them into the php file using

PHP Code:
<?php
include 'variables.inc.php'
?>
And then you can use them in the script after this, can you do it?

Thank in advance for helping me.

03-05-2006, 11:58 AM
#2
Bas is offline Bas
Status: I love this place
Join date: Jan 2005
Location: The Netherlands
Expertise: Frontend, vBulletin
Software: Coda, Photoshop
 
Posts: 607
iTrader: -1 / 0%
 

Bas is on a distinguished road

Send a message via MSN to Bas

  Old

Variables are used for things that you use more than one time, lets say you need to know where a script is installed, you'll use a variable like this:
PHP Code:
$site_root 'http://www.website.com/script/'
now, when you use links in your code you just paste the $site_root before it and when you change the variable it changes everywhere. This is perhaps not the best example but I think you get the point.

Including a page and using the variables in it is possible, yes.

03-05-2006, 12:03 PM
#3
jono1 is offline jono1
jono1's Avatar
Status: Non-conformist
Join date: Jul 2005
Location: Canberra, Australia.
Expertise:
Software:
 
Posts: 1,172
iTrader: 0 / 0%
 

jono1 is on a distinguished road

  Old

Or you use variables to handle data that isn't always the same - such as user input.

03-05-2006, 05:19 PM
#4
freedom is offline freedom
freedom's Avatar
Status: Request a custom title
Join date: Dec 2005
Location: LIC (NYC), NY
Expertise:
Software:
 
Posts: 1,026
iTrader: 0 / 0%
 

freedom is on a distinguished road

  Old

Originally Posted by Spawny
I am only just getting the hang of PHP, and i was getting confused about variables.

What is the point of them? I mean i know they are used to store data but wouldn't it be easier to just type in what you want?

Also, can you make an include file like "variables.inc.php" and then put all the variables you need in it, and then include them into the php file using

PHP Code:
<?php
include 'variables.inc.php'
?>
And then you can use them in the script after this, can you do it?

Thank in advance for helping me.
Yup. I tend to use require() or require_once() though.

03-05-2006, 05:45 PM
#5
sketchie is offline sketchie
sketchie's Avatar
Status: Senior Member
Join date: Jul 2005
Location:
Expertise:
Software:
 
Posts: 835
iTrader: 1 / 100%
 

sketchie is on a distinguished road

  Old

Variables are used for lots of things

counting loops:
PHP Code:
foreach($x=1$x 10$x++)
{
echo 
$x.'<br/>';

Storing valuable data
Imagine you have a code that's long and complecated and is necessary to keep perfect, you could easily make a mistake typing it out a second time, so using a short variable name makes it quicker.

Function parametres
when writing a function, you can input parametres which when become variables inside the function.
PHP Code:
function dostuff($name$age)
{
    if (
$name == "mitch") return "You're awesome";
    else return 
"nice..";
}
echo 
dostuff("Mitch""16"); 
Storing Data from a Database
Obviously you can keep things in variables in a database (though arrays are more efficient).

Configuration uses
Say if you made a commercial script for people to use, often there is a configuration document with a few variables (IE: for connecting to a database etc). If they weren't stored in variables you'd have to type out this information all by yourself one at a time. Bit time consuming..

Storing User input
When using forms, or data from URLs they have to be stored in arrays - (or variables depending on whether register globals are turned on or not). Without them you wouldn't be able to receive information from the user.

03-05-2006, 10:05 PM
#6
Spawny is offline Spawny
Spawny's Avatar
Status: Request a custom title
Join date: Aug 2005
Location: Great Yarmouth, England
Expertise:
Software:
 
Posts: 1,321
iTrader: 2 / 100%
 

Spawny is on a distinguished road

Send a message via AIM to Spawny Send a message via MSN to Spawny

  Old

Thanks, everyones posts have helped me a lot.

03-06-2006, 11:02 AM
#7
hakeem is offline hakeem
hakeem's Avatar
Status: I'm new around here
Join date: Feb 2006
Location:
Expertise:
Software:
 
Posts: 3
iTrader: 0 / 0%
 

hakeem is on a distinguished road

  Old

Variable is the easiest and one quick way to hold a physical memory location in your RAM Module, to be used later within your program to hold a VARIABLE value. Once declared and initialised it locates the memory and store an initial value in it. Now you can keep on changing its value and accessing it on different positions in your code according to your requirements. Like sketchie mentioned above in iterations, functions, arrays, as constants, classes (as attributes), databases and program configuration.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed