...

View Full Version : Understanding PHP



Albinal
06-04-2007, 10:18 AM
I have some problems understanding things in PHP and would really appreciate some help.

I'm not a programmer but I have a basic knowledge of programming. I can do basic Javascript/ActionScript and ASP and understand IF statements and variables but database connection, loops and arrays are beyond my comprehension.

I made a website in flash for an animation I did (www.robinvspirate.com). The flash form sends data to a PHP script which has this in it:

<?

$sName=$_GET["sName"];
$sContact=$_GET["sContact"];
$sComments=$_GET["sComments"];

$to .= "me@myemailaddress.com";
$subject .= "ROBINvsPIRATE on-line contact form";

//Main body

$msg .= "Name: $sName\n\n";
$msg .= "E-mail address: $sContact\n\n";
$msg .= "Comments/message: $sComments\n\n";

mail($to, $subject, $msg, "From: ROBINvsPIRATE.com[/email]\nReply-To: $sContact\n");
?>

I tested it before making the site live and it worked. I had a form submitted on the 2nd which had undefined in all fields. I tested it today and it worked.

Why would it work for me and not for someone else? How could it stop and start working without me touching it? I don't understand this.

Secondly, I wanted to make a basic image viewer (www.albinal.com/squad.php) where the image and the next/previous links changed based on the selection.

I used this code:
<?
$imageID=$_GET["imageID"];

if ($imageID == "")
{
$imageID = 1;
}

echo '<p align="center"><img src="images/vector_art/small_va'. $imageID .'.gif" style="border:1px solid #000000;" alt=""></p>';


$TotalImages = 8;
$next = $imageID = $imageID + 1;
$previous = $imageID = $imageID - 1;


?>

The next link works fine but the previous link doesn't. Unless I swap these lines around:

$next = $imageID = $imageID + 1;
$previous = $imageID = $imageID - 1;

Then the previous link will work but the next doesn't. This seems to suggest that the $imageID variable is changing based upon what the other variables do, which confuses me.

Sorry for the long post but if someone can help with this then I think my understanding will be much better.

Many thanks.

Pennimus
06-04-2007, 10:37 AM
I tested it before making the site live and it worked. I had a form submitted on the 2nd which had undefined in all fields. I tested it today and it worked.

Why would it work for me and not for someone else? How could it stop and start working without me touching it? I don't understand this.


Is it just possible that the other person might well have submitted the form without any information entered into it?



$next = $imageID = $imageID + 1;
$previous = $imageID = $imageID - 1;


I've never seen this before, where you've got two equals symbols. That looks wrong (although it could be something obscure).

But, to me that looks like with the first line you're adding 1 to $imageID itself, not just declaring that $next equals $imageID +1.

Try:




$next = $imageID + 1;
$previous = $imageID - 1;

Albinal
06-04-2007, 11:21 AM
If you submit a blank form then the fields come through blank. The only time I've ever expereinced "undefined" fields was when something wasn't working and the values weren't getting passed.

Could something server side have gone wrong?

The second bit of code you posted seems to work better. I was trying to increment $ImageID which was a mistake. Many thanks!

aedrin
06-04-2007, 05:41 PM
I've never seen this before, where you've got two equals symbols. That looks wrong (although it could be something obscure).

It's actually a little programming 'trick'.

What PHP does when evaluating a statement is something along the lines of this:

1. It starts with the original.

$variable = $variable2 = 5;

2. Evaluates the first expression it knows of (depending on certain rules).

$variable = ($variable2 = 5);

PHP replaces the expression with the end result (now contents of $variable2):


$variable = 5;

3. Repeat 2


($variable = 5);


5;

Side Note: This is why you can do:


mysql_query("etc.") or die("error");

Because PHP replaces the function with its end result (no matter whether you assign it to something or not. So if your query returns an invalid result (null, false or whichever), you get this:


false or die("error");

False obviously doesn't go anywhere, so it is forced to do the or part of it. It's basically a shortcut to:



if (mysql_query("etc.") === FALSE) {
die("error");
}



$previous = $imageID = $imageID - 1

Can also be written as:


$previous = --$imageID



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum