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 606679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:16:47+00:00 2026-05-13T17:16:47+00:00

I’ve been dabbling in writing a PHP Class for a few weeks now and

  • 0

I’ve been dabbling in writing a PHP Class for a few weeks now and I like to think I’ve got a handle on he basics but I’m a little stumped.

As a simplified example of what I’m doing:

I have declared and instantiated a public variable ($myURL) in my class (someClass) and in a external file (config.php) to the class filled the variable with a URL (http://demo.com).

In a function (make_array()) within the same class, I use the declared variable ($myURL) in a while() loop to build an associative array of data which I use outside the class to build a listed output (output.php).

Once I’ve set the variable using a class function (set_myurl($url)) in the external file (config.php) I can access it quite easily outside of the class file by using echo to print the set URL to the page.

The problem comes when I’m looping through the array and I want to set the predefined variable ($myURL) to various sub-arrays within the associative array that I’m building with the make_array() function in my class file. The make_array() function takes a MSSQL $result and loops the rows creating the array inserting the URL if one does not exist and the second argument ($add_my_url) is set to true.

Within the function the defined variable becomes empty but remains set as I’ve tested it with the built in PHP functions empty(), isset() and is_null() in an effort to troubleshoot the problem.

I wonder if anyone has any knowledge or experience of this happening to them.

A brief idea of what I’m doing

<?php
class someClass {
var $myURL = '';

public function set_myurl($url){
$this->myURL = $url;
}

public function make_array($db_result_array, $add_my_url = false){
$new_array = array();
while($row = $db_result_array){         
// build array
$array = array(
'Amount' => $row['Amount'],
'Description' => $row['Description'],
'URL' => ($add_my_url ? (!$row['URL'] ? $this->myURL : $row['URL']) : $row['URL'])
);
// merge array  
$new_array[] = $array;          
}       

return $new_array;
}

}
?>

<?php
// config.php
$myClass = new someClass;
$myClass->set_myurl('http://demo.com');

// this works and displays: http://demo.com
echo $myClass->myURL;
?>

I know this example is really abstract but I didn’t want to post the full 2,000 lines of PHP.

If you need more clarification, please do ask. I’m quite confident that I’ve narrowed the problem down to this function and this while() loop.

Thanks.

  • 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-13T17:16:47+00:00Added an answer on May 13, 2026 at 5:16 pm

    I believe something is wrong with your ternary condition, try this:

    class someClass
    {
        public $myURL = '';
    
        public function set_myurl($url)
        {
            $this->myURL = $url;
        }
    
        public function make_array($db_result_array, $add_my_url = false)
        {
            $new_array = array();
    
            while ($row = $db_result_array)
            {
                // build array
                $array = array
                (
                    'Amount' => $row['Amount'],
                    'Description' => $row['Description'],
                    //'URL' => ($add_my_url ? (!$row['URL'] ? $this->myURL : $row['URL']) : $row['URL'])
                );
    
                if ($add_my_url === true)
                {
                    $array['URL'] = $row['URL'];
    
                    if (empty($array['URL']))
                    {
                        $array['URL'] = $this->myURL;
                    }
                }
    
                // merge array  
                $new_array[] = $array;          
            }    
    
            return $new_array;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.