Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/writing-the-code-for-your-class-pag-177/page.php on line 81

Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/writing-the-code-for-your-class-pag-177/page.php on line 81

Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/writing-the-code-for-your-class-pag-177/page.php on line 81

Deprecated: strpos(): Passing null to parameter #2 ($needle) of type string is deprecated in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/writing-the-code-for-your-class-pag-177/page.php on line 81

public function IsURLCurrentPage($url)
  {
    if(strpos($_SERVER['PHP_SELF'],$url)===false)
    {
      return false;
    }
    else
    {
      return true;
    }
  }

PHP Version 8.1.17

    Well, if you look at https://www.php.net/strpos , it shows you that the 2nd argument to strpos() is what it calls the "needle". Therefore, it appears that you are passing a null value to your IsURLCurrentPage() function; so you need to debug why that is in the code that calls this function.

    You might want to consider modifying the function declaration to require that the argument be a string, which should then raise an exception if the calling code passes a null (or other non-string value) to it:

    public function IsURLCurrentPage(string $url)
    {
        // ... rest of function
    }
    

    Alternatively, if you think it should be okay to pass a null to that function, then you should test that the incoming value is not null before you try to use it, probably doing a return false; if it is null.

      page.php

      class Page
      {
        // class Page's attributes
        public $content;
        public $title = "TLA Consulting Pty Ltd";
        public $keywords = "TLA Consulting, Three Letter Abbreviation, 
                           some of my best friends are search engines";
        public $buttons = array("Home"   => "home.php", 
                              "Contact"  => "contact.php", 
                              "Services" => "services.php", 
                              "Site Map" => "map.php"
                          );
      
        // class Page's operations
        public function __set($name, $value)
        {
          $this->$name = $value;
        }
      
        public function Display()
        {
          echo "<html>\n<head>\n";
          $this -> DisplayTitle();
          $this -> DisplayKeywords();
          $this -> DisplayStyles();
          echo "</head>\n<body>\n";
          $this -> DisplayHeader();
          $this -> DisplayMenu($this->buttons);
          echo $this->content;
          $this -> DisplayFooter();
          echo "</body>\n</html>\n";
        }
      
        public function DisplayTitle()
        {
          echo "<title>".$this->title."</title>";
        }
      
        public function DisplayKeywords()
        {
          echo "<meta name='keywords' content='".$this->keywords."'/>";
        }
      
        public function DisplayStyles()
        { 
          ?>   
          <link href="styles.css" type="text/css" rel="stylesheet">
          <?php
        }
      
        public function DisplayHeader()
        { 
          ?>   
          <!-- page header -->
          <header>    
            <img src="logo.gif" alt="TLA logo" height="70" width="70" /> 
            <h1>TLA Consulting</h1>
          </header>
          <?php
        }
      
        public function DisplayMenu($buttons)
        {
          echo "<!-- menu -->
          <nav>";
      
          foreach ($buttons  as  list($name,$url)){
          /*while (list($name, $url) = each($buttons)) {
            $this->DisplayButton($name, $url, 
                     !$this->IsURLCurrentPage($url));
          }*/
          $this->DisplayButton($name, $url,
                     !$this->IsURLCurrentPage($url));
        }
          echo "</nav>\n";
        }
      
        public function IsURLCurrentPage($url)
        {
          if(strpos($_SERVER['PHP_SELF'],$url)===false)
          {
            return false;
          }
          else
          {
            return true;
          }
        }
      
        public function DisplayButton($name,$url,$active=true)
        {
          if ($active) { ?>
            <div class="menuitem">
              <a href="<?=$url?>">
              <img src="s-logo.gif" alt="" height="20" width="20" />
              <span class="menutext"><?=$name?></span>
              </a>
            </div>
            <?php
          } else { ?>
            <div class="menuitem">
            <img src="side-logo.gif">
            <span class="menutext"><?=$name?></span>
            </div>
            <?php
          }  
        }
      
        public function DisplayFooter()
        {
          ?>
          <!-- page footer -->
          <footer>
            <p>&copy; TLA Consulting Pty Ltd.<br />
            Please see our 
            <a href="legal.php">legal information page</a>.</p>
          </footer>
          <?php
        }
      }
      home.php

      <?php
      require("page.php");

      $homepage = new Page();

      $homepage->content ="<!-- page content -->
      <section>
      <h2>Welcome to the home of TLA Consulting.</h2>
      <p>Please take some time to get to know us.</p>
      <p>We specialize in serving your business needs
      and hope to hear from you soon.</p>
      </section>";
      $homepage->Display();
      ?>

        Hey, it's your code, you're the one who's supposed to know what it's supposed to do. NogDog's given an answer, and posting a huge chunk of code without explanation isn't very useful as a reply.

        But, going through it, I see

         foreach ($buttons  as  list($name,$url)){
            /*while (list($name, $url) = each($buttons)) {
              $this->DisplayButton($name, $url, 
                       !$this->IsURLCurrentPage($url));
            }*/

        Which takes you back to another of your questions, where you'll find an answer.
        https://board.phpbuilder.com/d/10404191-while-listname-url-eachbuttons-each-is-deprecated

        I am learning from a book of php but there are some code I don't understand I am a beginner with php
        I like to understand all php code but it is not mine is from one book
        PHP and Mysql Web Development fitth edition
        new PHP 7 coverage
        Luke Welling
        Laura THomson

        addison wesley

        i put this string $url in this function an return this error

        public function IsURLCurrentPage( string $url)
          {
            if(strpos($_SERVER['PHP_SELF'],$url)===false)
            {
              return false;
            }
            else
            {
              return true;
            }
          }

        Fatal error: Uncaught TypeError: Page::IsURLCurrentPage(): Argument #1 ($url) must be of type string, null given, called in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/writing-the-code-for-your-class-pag-177/page.php on line 74 and defined in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/writing-the-code-for-your-class-pag-177/page.php:79 Stack trace: #0 /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/writing-the-code-for-your-class-pag-177/page.php(74): Page->IsURLCurrentPage(NULL) #1 /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/writing-the-code-for-your-class-pag-177/page.php(29): Page->DisplayMenu(Array) #2 /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/writing-the-code-for-your-class-pag-177/home.php(13): Page->Display() #3 {main} thrown in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/writing-the-code-for-your-class-pag-177/page.php on line 79

          I can not find the solution but thank you for help me, I hope when I finish the book I going to improve my skills with php

            Write a Reply...