Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
please help - IF ELSE problem
Old 03-21-2005, 10:26 AM please help - IF ELSE problem
flish's Avatar
Novice Talker

Posts: 6
Location: Bucharest, Romania
Trades: 0
Hello

I have a IF ELSE problem

I need to display pictures of some products. Some products have like 10 pictures and other products only 1 or 2.
What i did is:
IF the picture exists then concat to get the full path to the image.
ELSE display a background image.
$picture1 is a string containing the name of the gif file
$string1 is a string containing the full path to the gif file

if ($picture1 != "")
{
$string1="img/" . $picture1;
} else {
$string1="img/back.GIF";
}

Problem is it only executes the ELSE statement. So now I get all my pictures as "back.GIF"
What am I doing wrong?
flish is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-21-2005, 11:23 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
flish,

First, your IF condition doesn't check to see "if the file exists," but only if the string that should point to the image is equal to the empty string. If that is the condition you want to check, I would suggest you use the empty() function. Also, you can integrate $picture1 into the string without concatenation (if you prefer - it looks cleaner).

PHP Code:

if( empty( $picture1 ) )
{
    
$string1 "img/back.gif";
}
else
{
    
$string1 "img/$picture1";

Or, since you are doing an if/else assignment, the tertiary operator especially lends itself here.

PHP Code:

$string1 
= empty( $picture1 ) ? "img/back.gif" "img/$picture1"
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-21-2005, 01:37 PM
flish's Avatar
Novice Talker

Posts: 6
Location: Bucharest, Romania
Trades: 0
Thank you very much. It worked perfectly. I still dont know what I did wrong but practice makes perfect

"First, your IF condition doesn't check to see "if the file exists," but only if the string that should point to the image is equal to the empty string" - Exactly what I had in mind, only my english is not very good
flish is offline
Reply With Quote
View Public Profile
 
Old 03-21-2005, 01:54 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
flish, no problems. The only problem with checking for an empty string in the manner you did was that it does not catch the use case when the string is equal to null. That's why the empty() function is so useful in that case because it checks for both contingencies.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-21-2005, 02:25 PM
Bon Bon's Avatar
Average Talker

Posts: 27
Location: Middlesbrough, England, UK
Trades: 0
Using functions is actually a lot slower than doing things without using functions.

If I was checking for an empty string or something this is what I would do:
PHP Code:
<?php

$string1 
= ($picture1 == TRUE) ? 'img/' $picture1 'img/back.gif';

?>
Because the variable is not inside quotes this will also speed things up. At the end of the day you want things to be as fast as possible so that your processor is not wasting its time doing pointless things.
__________________
Matt

Last edited by Bon Bon; 03-21-2005 at 02:32 PM..
Bon Bon is offline
Reply With Quote
View Public Profile Visit Bon Bon's homepage!
 
Old 03-21-2005, 04:13 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Quote:
Originally Posted by Bon Bon
Using functions is actually a lot slower than doing things without using functions.

I am not sure the basis for your assertion, but I would offer in respectful opposition that in tasks where the number of instructions is limited (and let's face it, most PHP scripts use to build web pages are exactly that), the amount of time saved by avoiding functions is so miniscule that it is rendered irrelevant.

Functions made available by the PHP scripting language itself can be assumed to be optimized for their purposes and their cost minimal.

Again, I would be interested in any references you have showing functions to be inherently slower than in-line code.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-21-2005, 04:16 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Unless you have a crazy amount of page requests, the function call overhead will make no difference at all. If you were batch processing millions of images, then I can understand, but for one, you won't notice the difference.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-21-2005, 04:36 PM
Bon Bon's Avatar
Average Talker

Posts: 27
Location: Middlesbrough, England, UK
Trades: 0
I am a PHP hack freak and just thought that it would be useful to show alternative ways of writing PHP. The more good programming that somebody is likely to see means that they are likely to produce even better programming once one fully understands the language. When I see better programming I pick up on it and it works in a circle.

If you take an old box and turn it into a web server, the last thing you want to be doing is writing unoptimised PHP programming as it will make an overall difference if you are to host a website visited by many people a day. I run an old and a new box and I often compare the differences between the two to see what is making a difference and what is not making a difference.

The only functions that do not make a difference to the speed of PHP are the language constructed functions that cannot be avoided in order to do what you want to do. Functions are there to make your job as a programmer a lot easier at the end of the day and do slow down the processing of PHP if heavily used to a point where they are looped tens of thousands of times (which I have yet to see in a program but still it might one day happen). I think we should all agree that if there is a more optimised way of doing something, it should at least be done in the correct way.
__________________
Matt
Bon Bon is offline
Reply With Quote
View Public Profile Visit Bon Bon's homepage!
 
Reply     « Reply to please help - IF ELSE problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30624 seconds with 12 queries