Jump to content

[SOLVED] Question about switches and functions


Kairu

Recommended Posts

Alright. In some incredibly long code, I have around five switches including the default and around.... twenty or more functions.

My question is..... Difficult to explain. Let me show you some sample code first.

[code]<?php
$DBUser = "U"
$DBPass = "P"

include "config.php"

function edit()
{
echo'<html>
<body>
<form action="New.php?id=view" method="POST">
<p>Username: <input type="text" name="Username" size="20" maxlength="20"></p>
<p>Message:&nbsp;&nbsp; <input type="text" name="Message" size="20" maxlength="100"></p>
<p><input type="submit" value="Submit" name="Submit"><input type="reset" value="Reset" name="Reset"></p>
</form>
</body>
</html>';
}

function view()
{
mysql_connect('localhost:3306', $DBUser, $DBPass);

mysql_select_db('gaia_image');

mysql_query("INSERT INTO `box` ( `ID` , `Username` , `Message` , `Timestamp` )
VALUES (
'1', '" . $_POST['Username'] . "', '" . $_POST['Message'] . "', NOW()
);");
}

function image()
{
header ('Content-Type: image/gif');

mysql_connect('localhost:3306', $DBUser, $DBPass);

mysql_select_db('gaia_image');

$result = mysql_query('SELECT *
FROM `box`
WHERE ID = 1
ORDER BY `box`.`Timestamp` DESC');
$txt = array();
while($row = mysql_fetch_array($result))
{
for ($i=0; $i<=1; $i++)
{
array_push($txt, str_replace("\r", '', str_replace("\n", '', $row[$i + 1])));
}
}

$image = imagecreatefromstring(file_get_contents('http://thedarkrealm.no-ip.org/shoutboxes/background.jpg'));

$text_color = imagecolorallocate($image, 55, 55, 55);


$img = (floor(imagesy($image)/imagefontheight(2)) * imagefontheight(2)) - imagefontheight(2);

for ($i=0; $i <= count($txt); $i++)
{
$text = $txt[(($i*2)+3)] . $txt[($i*2)] . ": " . $txt[(($i*2)+1)];
$text = wordwrap($text, floor((imagesx($image)-21)/imagefontwidth(2)), "\n    ", 1);
$text = explode("\n", $text);

for ($j = count($text) - 1; $j >= 0; $j--)
{
imagestring($image, 2, 7, $img, $text[$j], $text_color);
$img = $img - imagefontheight(2);
}
}

imagegif($image);
}
?>[/code]

Alright. At the top, I declare two variables, $DBUser and $DBPass. Would these two variables be accessible to all three functions within the switch? Also, would the include statement pass on to the functions?

I am currently calling config.php in almost every function.... as well as declaring the DB user and pass in every function that uses them. Is their an easier way?
Someone else already answered, but I hope my answer can help explain theirs. :p

Lets say you have a variable called $myvar in config.php which is set to "Is cool!"

If you had page2.php and it was
[code=php:0]
<?
include("config.php");

function echomyvar() {
echo "Myvar" . $myvar;
}
?>
[/code]

That would just echo "Myvar," but if you used:
[code=php:0]
<?
include("config.php");

function echomyvar() {
global $myvar;
echo "Myvar" . $myvar;
}
?>
[/code]
It would echo "Myvar is cool!"

And once a file is included, you should not need to include it again, unless theres unusual circumstances.

I hope that made sense... But basically PHP variables have something called a scope (http://us3.php.net/global) and the default scope is local which means the variable is accessible in where it was created, such as in the main body of a page, or in a function or class so on, and with out certain modifiers, such as global $var;, these variables cannot be accessed by other scopes...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.