Jump to content


Check out our Community Blogs

uniqueideaman

Member Since 12 Oct 2016
Offline Last Active Yesterday, 12:43 PM
-----

Topics I've Started

Why Associative Array Creation Has "=>" And Numerical Array Not ?

21 May 2017 - 04:25 AM

 
 
Associative Array
 
<html>
   <body>
      
      <?php
         /* First method to associate create array. */
[b]         $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);[/b]
         
         echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
         echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
         echo "Salary of zara is ".  $salaries['zara']. "<br />";
         
         /* Second method to create array. */
         $salaries['mohammad'] = "high";
         $salaries['qadir'] = "medium";
         $salaries['zara'] = "low";
         
         echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
         echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
         echo "Salary of zara is ".  $salaries['zara']. "<br />";
      ?>
   
   </body>
</html>
 
 
Numerical Array
 
 
<?php      
/* First method to create array. */
$numbers = array( one, two, three, four, five);
         
foreach( $numbers as $value ) {
    echo "Value is $value <br />";
    }
         
    /* Second method to create array. */
    $numbers[0] = "one";
    $numbers[1] = "two";
    $numbers[2] = "three";
    $numbers[3] = "four";
    $numbers[4] = "five";
         
foreach( $numbers as $value ) {
    echo "Value is $value <br />";
    }
?>
      
   </body>
</html>
 
 
Look at both code's bold parts on how each different type of arrays create an array.
 
The Associative Array:
/* First method to associate create array. */
         $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
 
 
The Numerical Array:
         /* First method to create array. */
         $numbers = array( 1, 2, 3, 4, 5);
 
Why one uses "=>" and one not ? Are they not both procedural style ?
Look:
 

Numerically Indexed Arrays

20 May 2017 - 03:23 AM

Fellow Php Buds,
 
Do you mind telling me, according to you:
 
1. How many variables are listed below ? And, 
2. How many numerically Indexed Arrays ?
 
$employee_array[0] = "Bob";
$employee_array[1] = "Sally";
$employee_array[2] = "Charlie";
$employee_array[3] = "Clare";
 
 
Thank You

How Come Variable Not Assigned And Php Knows Where To Look For Value ?

06 May 2017 - 04:21 AM

Good Weekend Folks!
 
 
I have a question.
I do not understand how php knows how to define this variable $value in the following example since it has not been declared. How does it know what that variable represents since no  reference or pointer has been assigned to it ? Strange!
 
 
 
<html>
   <body>
   
      <?php
         /* First method to create array. */
         $numbers = array( 1, 2, 3, 4, 5);
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
         
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
      ?>
      
   </body>
</html>
 
 
I thought "array_values()" was supposed to be there in the above example but "$value" is there instead.
What is the difference between the 2 ?
 
The following seems to be in pdo, which I haven't started learning just yet:
 

Magic Constants Case Sensitive or case In-sensitive ?

27 April 2017 - 05:58 AM

Experts,
 
I do not understand this atall.
Here:
 
It says:
"PHP Magic constants
PHP provides a large number of predefined constants to any script which it runs.
 
There are five magical constants that change depending on where they are used. For example, the value of __LINE__ depends on the line that it's used on in your script. These special constants are case-insensitive and are as follows −".
 
 
__CLASS__
 
The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
 
__METHOD__
 
The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
 
 
Seems like contradiction to me. Firs they say case-insensitive. Then in their 2 examples they mention they are case-sensitive. What is going on ? I need some good explanation with a min of 2 good examples to understand any of this.

Include a specific error, task, problem, or question in your title

26 April 2017 - 03:59 PM

Hiya,
 
 
On the above link, it is stated:
 
PHP Code:
$numberedString = "1234567890"; // 10 numbers from 1 to 0
 
$fivePos = strpos($numberedString, "5");
echo "The position of 5 in our string was $fivePos";
Display:
The position of 5 in our string was 4
Notice that the position is 4, which may seem confusing at first, until you realize that PHP starts counting from 0.
 
The number 1 - Position 0 - No match
The number 2 - Position 1 - No match
The number 3 - Position 2 - No match
The number 4 - Position 3 - No match
The number 5 - Position 4 - Match
Although we only searched for a single character, you can use this function to search for a string with any number of characters. Also, it is important to note that this function will return the position of the start of the first match. So if we had searched the same string for "567890" we would again find a match and position 4 because that is where the match starts.
 
I do not understand the last part and so can someone show an example:
So if we had searched the same string for "567890" we would again find a match and position 4 because that is where the match starts.

Oh, I now understand. It meant the string "567890" starts from position 4.

On previous example, it was searching for the single character 4's position. This time, it is searching where the string starts.

 

Ok. I understand that with strpos, you can only find the first match of a character or string but not any further than that if there were more than one match. And to fix this, I can use the incrementer to set the next starting point. Like so:

 
$numberedString = "1234567890123456789012345678901234567890";
 
$fivePos = strpos($numberedString, "5");
echo "The position of 5 in our string was $fivePos";
$fivePos2 = strpos($numberedString, "5", $fivePos + 1);
echo "<br />The position of the second 5 was $fivePos2";
 
So, naturally strpos has 2 parameters if you want to find the pos of only one match or the first match. Right ?
And, if you want to find all the matches' positions then you add the final matched position on the 3rd parameter. Right ?