Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:14:52+00:00 2026-05-16T11:14:52+00:00

I have a couple of simple PHP functions I am using. One to detect

  • 0

I have a couple of simple PHP functions I am using. One to detect wether the user is on an iPhone, and one to resize images if they are.

<?php 

/* User agent function */
function userAgent(){
    $browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    if ($browser == true)  { $var = 1; }
    return $var;
}

/* Image resize function */
function imageResize($width, $height) { 

    $var = userAgent($var);

    if($var == 1){
        $width = round($width / 2); 
        $height = round($height / 2); 
    }else{
        $width = round($width);
        $height = round($height);
    }

    echo "width=\"$width\" height=\"$height\""; 
} 

?>

The problem is, if I manually change the $var to 0 or 1 in the userAgent() function, the images do not resize, but if I change the $var to == 0 in the imageResize() function, they do.

Why is the variable not carrying across from the first, to the second function, or am I doing something else wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:14:53+00:00Added an answer on May 16, 2026 at 11:14 am

    strpos never returns true. You will need to test for $browser != false, otherwise your function will never recognize an iPhone.

    However, your code is unnecessarily complicated, and returning an integer is not really useful here. I would recommend a simplified rewrite:

    // Make it return `true` or `false` instead of `1`, also make it universal
    function isAgent($string)
     {
        return (strpos($_SERVER['HTTP_USER_AGENT'],$string) !== false);
     }
    
    
    /* Image resize function */
    function imageResize($width, $height) { 
    
        if(isAgent("iPhone")){
            $width = round($width / 2); 
            $height = round($height / 2); 
        }else{
            $width = round($width);
            $height = round($height);
        }
        // Consider using CSS
        echo "style=\"width: {$width}px; height: {$height}px;\""; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of simple forms that send an html-only email. Most clients
I recently made this simple navigation where you have a couple links that have
I have made a simple web-page including a couple of static pages, a css
I have a couple of links on some images. The code is very simply
I have couple resource DLLs that I currently load when application starts using following
I have a PHP webservice that returns an XML output, and one of the
I am having a couple of problems involving PHP & AJAX using jQuery. To
I have couple of hyperlinks on my website. What I want is to remove
I have couple of dozen pieces of data that I need to save and
I have couple of formulas and data coming from database. I want to refresh

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.