PDA

View Full Version : Quick PHP Question



Tintinnabulate
25-06-2009, 08:06 AM
I wanna have something like this on my page


<?php

$rangeba4 = 'value';
$rangeb80x60 = '';
$rangeb20x30 = '';
$rangeb20x24 = '';
$rangeb70x50 = '';
$rangeb18x24 = '';
$rangeba2 = '';
$rangeb16x20 = '';
$rangeb18x24 = '';
$rangeba20x8 = '';
$rangeba16x12 = '';
$rangeba3 = '';
$rangeb14x11 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';

?>

But all the variables will be different and theres going to be like 100 in total. If I then fetch it all over the page, will it slow the website down / crash anyones browser?

BoyBetterKnow
25-06-2009, 08:10 AM
I wanna have something like this on my page


<?php

$rangeba4 = 'value';
$rangeb80x60 = '';
$rangeb20x30 = '';
$rangeb20x24 = '';
$rangeb70x50 = '';
$rangeb18x24 = '';
$rangeba2 = '';
$rangeb16x20 = '';
$rangeb18x24 = '';
$rangeba20x8 = '';
$rangeba16x12 = '';
$rangeba3 = '';
$rangeb14x11 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';

?>But all the variables will be different and theres going to be like 100 in total. If I then fetch it all over the page, will it slow the website down / crash anyones browser?

Lol, it takes alot to crash a browser... I crashed mine when I accidently coded my PHP to loop :(

But anyway, you are asking "If I use the code here, will it slow my website down?" as apposed to ?

Tintinnabulate
25-06-2009, 08:12 AM
I mean will it affect the speed of the site because I will need to fetch the variables alot all over the page.

I am new to PHP you see.

BoyBetterKnow
25-06-2009, 08:24 AM
When you say you need to fetch alot of variables over the page, I assume you are talking about getting them from the database...

Just setting the variables like you have done in the sample code will not slow it down.

If you were literally just doing like


$variable = 'text';

etc. and then echoing each variable, it would slow it down a bit but it wouldn't be significantly slower.. It won't be noticeable really..

It's hard for me to tell. What information are you fetching? and are you going to be echoing/printing it.

Tintinnabulate
25-06-2009, 08:27 AM
Thanks for trying to help, I explained it to someone on MSN and they helped me out :) +rep for your help.

BoyBetterKnow
25-06-2009, 08:29 AM
Thanks for trying to help, I explained it to someone on MSN and they helped me out :) +rep for your help.

Ha, I didn't manage to help. Sorry for not understanding what you want to do ;) Too early haha.

Tintinnabulate
25-06-2009, 08:33 AM
I wasnt being clear so its not your fault.

What I meant was having this on top of my page


<?php

$rangeba4 = 'value';
$rangeb80x60 = '';
$rangeb20x30 = '';
$rangeb20x24 = '';
$rangeb70x50 = '';
$rangeb18x24 = '';
$rangeba2 = '';
$rangeb16x20 = '';
$rangeb18x24 = '';
$rangeba20x8 = '';
$rangeba16x12 = '';
$rangeba3 = '';
$rangeb14x11 = '';
$rangeb12x12 = '';
$rangeb12x10 = '';
$rangeb10x10 = '';
$rangeb10x8 = '';
$rangeb9x7 = '';
$rangeb8x6 = '';
$rangeb7x5 = '';
$rangeb6x4 = '';
$rangeb5x4 = '';
$rangeb5x3half = '';
$rangeb3halfx3half = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';
$rangeba4 = '';

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="EN"><head>

and using this code all over the page


<?= $rangeba4 ?>

BoyBetterKnow
25-06-2009, 08:36 AM
oh okay. Surely it would be easier, instead of defining a variable as whatever. Just echo the whatever rather than the variable that u defined as whatever.

lol

Tintinnabulate
25-06-2009, 08:37 AM
Sorry that just confused me. I am new to PHP, like my day 1, didnt understand a word up there.
<?= $rangeba4 ?> - isnt that just getting the data from the top of the page?
And echo? :S

BoyBetterKnow
25-06-2009, 08:41 AM
Sorry that just confused me. I am new to PHP, like my day 1, didnt understand a word up there.
<?= $rangeba4 ?> - isnt that just getting the data from the top of the page?
And echo? :S

echo is where u display it.

Basically you want to do this:



<?php

$vairableOne = 'Value';
$variableTwo = 'Value';
$variableThree = 'Value';

?>


then some html blardy



<html>
<div>
<?php echo $variableOne; ?>


Right, echo means like DISPLAY. So that's display $variableOne.

Surely it would be easier to not set the varaibles ($variableOne etc.).

and just echo the values out in the html.

<?php echo 'Value'; ?>

This would work if you were putting the values in yourself. However, if you are fetching the results from a database then it'd be different.

Tintinnabulate
25-06-2009, 08:43 AM
I am putting the values myself yep.
Could I please ask why is it better to use
<?php echo $variableOne; ?>
instead of
<?= $rangeba4 ?>

Thanks.

BoyBetterKnow
25-06-2009, 08:50 AM
I am putting the values myself yep.
Could I please ask why is it better to use
<?php echo $variableOne; ?>
instead of
<?= $rangeba4 ?>

Thanks.

Haha sorry it's not better ;) I'm not even sure if shorter variable names make it faster haha. I never looked into it.

Right okay, if you are putting the values in yourself and displaying them, you mayaswell not define the variables at all.

Just do



<?php
echo 'value';
?>


where u want the value to go. Rather than putting the variable as value and then echoing the variable. If you get me ?

Tintinnabulate
25-06-2009, 08:53 AM
I understand but I am doing it for my site where I need to show the same value alot in different places. And its easier to have it as a variable because then if I change it, it changes all of them at once :)

BoyBetterKnow
25-06-2009, 08:56 AM
I understand but I am doing it for my site where I need to show the same value alot in different places. And its easier to have it as a variable because then if I change it, it changes all of them at once :)

Ahh see I didn't know you were doing this ;)

Yehh it won't slow it down that much. I'm trying to think whether an array would be appropriate, but it'll be too hard to grasp....

Also, since your new to php, when you want to display variables after each other you can do:


echo $variable.$variable2.$variable3;

The . is ownage.

Tintinnabulate
25-06-2009, 08:57 AM
Thank you :)

Dentafrice
29-06-2009, 03:24 AM
About the naming, it just makes it easier to name things for what they are for.

For example, $username for username, $password for password.



$site_name = "TestSite!";
$site_slogan = "We're a test site!";
That's better then using:



$wsdfsdlj = "TestSite!";
$esskldfjs = "We're a test site!";


Then if you want to echo them out, just do:



<?php echo $site_name; ?>

Want to hide these adverts? Register an account for free!