Solved

Casting a query string value into an integer

Posted on 2011-09-02
16
465 Views
Last Modified: 2012-08-13
In understanding my problem, let me give a quick overview of what I’m trying to do along with the table and fields. I’m making a movie organizer that will store movies in a single table (yes I know I could put this into 3NF) that has a movie id, title, genre, and etc. for fields. I’m created a create, edit, and delete page that will perform their duties respectively. I created a variable by type casting the value in the query string to an integer (string by default). However, when I use this variable (the movie id) in the update query, the query fails with an error code of zero. Look below at some of the main points of what I’m describing.

The Query String
http://localhost/PersonalMovieOrganizer/edit?2

Type Casting Value into Integer
$querystring = $_SERVER[‘QUERY_STRING’];
$MovieID = (int)querystring;

I can see the true value of the variable by doing this:
echo getType($MovieID) // Outputs integer as type.

Now I know I come from a strongly-typed background, but this should be pretty straightforward using the variable in the update query. What is really odd is that if I assign the variable a value like this

$MovieID = 2;

it works like a charm. So what am I missing here? Basically I want to update and delete a record based on this value from the query string, but because the 'MovieID' field value in the table is an integer, I need to type cast the query string value into an integer as well.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="Stylesheet" type="text/css" href="../content/admin.css"/>
        <script type="text/javascript" src="../scripts/jquery.js"></script>
        <script type="text/javascript" src="../scripts/jquery.validate.min.js"></script>
        <script type="text/javascript" src="../scripts/validation.js"></script>
    </head>
    <body>
        <div id="content_main">
            <table>
                <tr>
                    <td class="nav_link"><a href="index.html">Home</a></td>
                    <td>|</td>
                    <td class="nav_link"><a href="add_movie.php">Add Movie(s)</a></td>
                    <td>|</td>
                    <td class="nav_link"><a href="movie_crud.php">View Movie(s)</a></td>
                </tr>
            </table>
        
            <?php
                error_reporting(E_ALL | E_STRICT);
                ini_set('display_errors', 'On');
                
                // Make a connection to the database.
                $DBConnect = mysql_connect("localhost", "root", "*******");
                
                // Determine if the connection fails. If it does, display an error message.
                if($DBConnect === false)
                {
                    echo "<p class='message'>Unable to connect to the server.</p>";
                    echo "<p class='message'>Error Code: " . mysql_errno($DBConnect)
                        . ": " . mysql_error($DBConnect) . "</p>";
                }
                else
                {
                    $DBName = "movies_test_db";
                    if(!mysql_select_db($DBName, $DBConnect))
                    {
                        echo "<p class='message'>Unable to select database..</p>";
                        echo "<p class='message'>Error Code: " . mysql_errno($DBConnect)
                            . ": " .mysql_error($DBConnect) . "</p>";
                    }
                    else
                    {
                        // If the connection is successful, fetch data from the table
                        $TableName = "movies";
                        $querystring = $_SERVER['QUERY_STRING'];
                        
                        // ISSUE:
                        // This is where I'm running into trouble. The casting
                        // operation is successful, but fails in the update query.
                        $MovieID = (int)($querystring);
                        
                        //This works, but it is not how I want to update the record.
                        //$MovieID = 2;
                        echo "Query string length: " . count($MovieID) . "<br />";
                        echo "The value returned: " . $MovieID . "<br />";
                        echo "The data type of the value: " . getType($MovieID);
                        
                        $SQLstring = "SELECT * FROM $TableName WHERE MovieID = $MovieID";
                        $QueryResult = mysql_query($SQLstring, $DBConnect);
                        
                        // Determine if there are any movies in the database.
                        // If no movies exists, display an error message.
                        if(mysql_num_rows($QueryResult) === 0)
                        {
                            echo "<p class='message'>The query has failed.</p>";
                            echo "<p class='message'>Error Code: " . mysql_errno($DBConnect)
                                . ": " . mysql_error($DBConnect) . "</p>";
                        }
                        else
                        {
                            echo "<form name='edit_movie' action='edit.php' method='post'>";
                            echo "<table>";
                            $row = mysql_fetch_assoc($QueryResult);
                            
                            echo "<tr>";
                            echo "<td>Movie ID:</td>";
                            echo "<td><input type='input' name='txtMovieID' readonly='readonly' value='{$row['MovieID']}'/></td>";
                            echo "</tr>";
                            
                            echo "<tr>";
                            echo "<td>Movie Title:</td>";
                            echo "<td><input type='input' name='txtMovieTitle' value='{$row['Title']}'/></td>";
                            echo "</tr>";
                                
                            echo "<tr>";
                            echo "<td>Release Year:</td>";
                            echo "<td><input type='input' name='txtMovieYear' value='{$row['ReleaseYear']}'/></td>";
                            echo "</tr>";
                                
                            echo "<tr>";
                            echo "<td>Genre:</td>";
                            echo "<td>";
                            echo "<select name='cboGenre'>";
                            echo "<option value='Action & Adventure'>Action & Adventure</option>";
                            echo "<option value='Anime'>Anime</option>";
                            echo "<option value='Children & Family'>Children & Family</option>";
                            echo "<option value='Classics'>Classics</option>";
                            echo "<option value='Comedies'>Comedies</option>";
                            echo "<option value='Crime'>Crime</option>";
                            echo "<option value='Documentaries'>Documentaries</option>";
                            echo "<option value='Dramas'>Dramas</option>";
                            echo "<option value='Foreign'>Foreign</option>";
                            echo "<option value='Horror'>Horror</option>";
                            echo "<option value='Independent'>Independent</option>";
                            echo "<option value='Music'>Music</option>";
                            echo "<option value='Musicals'>Musicals</option>";
                            echo "<option value='Romance'>Romance</option>";
                            echo "<option value='Sci-Fi & Fantasy'>Sci-Fi & Fantasy</option>";
                            echo "<option value='Special Interests'>Special Interests</option>";
                            echo "<option value='Sports & Fitness'>Sports & Fitness</option>";
                            echo "<option value='TV Shows'>TV Shows</option>";
                            echo "<option value='Thrillers'>Thrillers</option>";
                            echo "</select>";
                            echo "</td>";
                            echo "</tr>";
                                
                            echo "<tr>";
                            echo "<td>Actors:</td>";
                            echo "<td><input type='input' name='txtLeadActor' value='{$row['Actors']}'/></td>";
                            echo "</tr>";
                                
                            echo "<tr>";
                            echo "<td>Awards:</td>";
                            echo "<td><input type='input' name='txtAwards' value='{$row['Awards']}'/></td>";
                            echo "</tr>";
                                
                            echo "<tr>";
                            echo "<td>Price:</td>";
                            echo "<td><input type='type' name='txtPrice' value='{$row['Price']}'/></td>";
                            echo "</tr>";
                            
                            echo "</table>";
                            echo "<p><input type='submit' name='btnSubmit' value='Save Record'/></p>";
                            //echo "</form>";
                            
                            if(isset($_POST['btnSubmit']))
                            {
                                $Title = $_POST['txtMovieTitle'];
                                $Year = $_POST['txtMovieYear'];
                                $Genre = $_POST['cboGenre'];
                                $Actors = $_POST['txtLeadActor'];
                                $Awards = $_POST['txtAwards'];
                                $Price = $_POST['txtPrice'];
                                
                                $SQLstring = "UPDATE $TableName 
                                    SET Title = '$Title', ReleaseYear = '$Year', 
                                        Genre = '$Genre', Actors = '$Actors', 
                                        Awards = '$Awards', Price = $Price
                                    WHERE MovieID = $MovieID";
                                $QueryResult = mysql_query($SQLstring, $DBConnect);
                                if($QueryResult === false)
                                {
                                    echo "<p class='message'>Unable to update the record.</p>";
                                    echo "<p class='message'>Error Code: " . mysql_errno($DBConnect)
                                            . ": " . mysql_error($DBConnect) . "</p>";
                                }
                                else
                                {
                                    echo "<p class='message'>The record was successfully updated.</p>";
                                }
                            }
                            echo "</form>";
                        }
                    }
                }
            ?>
    </div>
    </body>
</html>

Open in new window

0
Comment
Question by:Keith1985
  • 8
  • 7
16 Comments
 
LVL 14

Expert Comment

by:svgmuc
ID: 36475365
Better use

$MovieID = intval(querystring);
0
 

Author Comment

by:Keith1985
ID: 36475474
Thanks svgmuc, but it still fails. I'm still getting an error code of 0.
0
 

Author Comment

by:Keith1985
ID: 36475521
Here is a question, am I using the '$MovieID' variable in the update query properly? The reason I ask is because it works in the select query. Please also note that I usually work with the ASP.NET MVC framework with C# where I use a parameter in a edit method for the id, then write a LINQ query with the parameter value. Basically, PHP is very new to me.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36475806
Suggest you use var_dump($_SERVER["QUERY_STRING"] and see what is in there.  Please post the output of that dump here, thanks.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36475837
Also, when you post that output, please post the output of var_dump($_GET) as well.  I will show you how those relate and how to use the external variables in your script.  Check and make sure you do not have register_globals set to on.  You can find this in the output of this script:

<?php phpinfo();

If register_globals is on, please read this article to understand why you want it to be off.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7317-Register-Globals-a-bad-idea-from-day-one.html
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36475848
Also, rereading your last post, if you're already programming in C# it will be easy for you to pick up PHP.  Grab a copy of Eli White's excellent  book and look it over.  It has all kinds of good information for new and advanced PHP programmers.
http://www.amazon.com/PHP-5-Practice-Elliott-White/dp/0672328887
0
 

Author Comment

by:Keith1985
ID: 36475849
@ Ray

Here is what outputs from the var_dump.

string(1) "2"
int(2)
echo var_dump($querystring);
echo "<br />";
echo var_dump($MovieID);

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36475866
Please show me how you start the script (what is in the URL?), thanks...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Keith1985
ID: 36475901
@ Ray

I forgot to var_dump $_GET in my last example. Here is the output.
string(1) "2"
int(2)
array(1) { [2]=> string(0) "" }

Also, register globals are off.
echo var_dump($querystring);
echo "<br />";
echo var_dump($MovieID);
echo "<br />";
echo var_dump($_GET);

Open in new window

0
 

Author Comment

by:Keith1985
ID: 36475908
@Ray

Here is the script that calls the edit page. Scroll down to the table.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="Stylesheet" type="text/css" href="../content/admin.css"/>
        <script type="text/javascript" src="../scripts/jquery.js"></script>
        <script type="text/javascript" src="../scripts/jquery.validate.min.js"></script>
        <script type="text/javascript" src="../scripts/validation.js"></script>
    </head>
    <body>
        <div id="content_main">
            <table>
                <tr>
                    <td class="nav_link"><a href="index.html">Home</a></td>
                    <td>|</td>
                    <td class="nav_link"><a href="add_movie.php">Add Movie(s)</a></td>
                    <td>|</td>
                    <td class="nav_link"><a href="movie_crud.php">View Movie(s)</a></td>
                </tr>
            </table>
            
            <div id="add_movie_form">
                <form id="add_movie" action="add_movie.php" method="post">
                    <p>Add Movie - Admin</p>
                    <table>
                        <tr>
                            <td class="textbox_label">Movie Title</td>
                            <td><input type="text" name="txtMovieTitle" class="required"/></td>
                        </tr>
                        <tr>
                            <td class="textbox_label">Year</td>
                            <td><input type="text" name="txtMovieYear" class="required"/></td>
                        </tr>
                        <tr>
                            <td class="textbox_label">Genre</td>
                            <td>
                                <select name="cboGenre">
                                    <option value="Action & Adventure">Action & Adventure</option>
                                    <option value="Anime">Anime</option>
                                    <option value="Children & Family">Children & Family</option>
                                    <option value="Classics">Classics</option>
                                    <option value="Comedies">Comedies</option>
                                    <option value="Crime">Crime</option>
                                    <option value="Documentaries">Documentaries</option>
                                    <option value="Dramas">Dramas</option>
                                    <option value="Foreign">Foreign</option>
                                    <option value="Horror">Horror</option>
                                    <option value="Independent">Independent</option>
                                    <option value="Music">Music</option>
                                    <option value="Musicals">Musicals</option>
                                    <option value="Romance">Romance</option>
                                    <option value="Sci-Fi & Fantasy">Sci-Fi & Fantasy</option>
                                    <option value="Special Interests">Special Interests</option>
                                    <option value="Sports & Fitness">Sports & Fitness</option>
                                    <option value="TV Shows">TV Shows</option>
                                    <option value="Thrillers">Thrillers</option>
                                </select>
                             </td>                      
                        </tr>
                        <tr>
                            <td class="textbox_label">Lead Actor</td>
                            <td><input type="text" name="txtLeadActor" class="required"/></td>
                        </tr>
                        <tr>
                            <td class="textbox_label">Awards</td>
                            <td><input type="text" name="txtAwards"/></td>
                        </tr>
                        <tr>
                            <td class="textbox_label">Price</td>
                            <td><input type="text" name="txtPrice" class="required"/></td>
                        </tr>
                        <tr>
                            <td><input type="submit" name="btnSubmit" value="Submit"/></td>
                            <td><input type="reset" name="btnReset" value="Clear"/></td>
                        </tr>
                    </table>
                </form>
            </div>
            
        <?php
            if(isset($_POST['btnSubmit']))
            {
                // Determine if the fields are set with data.
                if(!empty($_POST['txtMovieTitle']) && !empty($_POST['txtMovieYear'])
                    && !empty($_POST['txtLeadActor']) && !empty($_POST['txtAwards'])
                    && !empty($_POST['txtPrice']))
                {
                    
                // Make a connection variable.
                $DBConnect = mysql_connect("localhost", "root", "*******");
                
                // If the connection fails, diplay an error message.
                if($DBConnect === false)
                {
                    echo "<p class='message'>The connection to the database has failed. Please try again.</p>";
                    echo "<p class='message'>Error Code: " . mysql_errno($DBConnect)
                        . ": " . mysql_error($DBConnect) . "</p>";
                }
                else
                {
                    // If the connection is successful, create the name of the database.
                    $DBName = "movies_test_db";
                    
                    // If selecting the database is unsuccessful, create the database.
                    if(!mysql_select_db($DBName, $DBConnect))
                    {
                        $SQLstring = "CREATE DATABASE $DBName";
                        $QueryResult = mysql_query($SQLstring, $DBConnect);
                        
                        // If the query fails, display an error message.
                        if($QueryResult === false)
                        {
                            echo "<p class='message'>Unable to create database '$DBName'.</p>";
                            echo "<p class='message'>Error CodeL " . mysql_errno($DBConnect)
                                . ": " . mysql_error($DBConnect) . "</p>";
                        }
                        else
                        {
                            echo "<p class='message'>The database '$DBName' was successfully created.</p>";
                        }
                    }
                    
                    // This will only run if the database is successfully selected.
                    mysql_select_db($DBName, $DBConnect);
                    
                    // Create the 'Movies' table if it doesn't already exist.
                    $TableName = "movies";
                    $SQLstring = "SHOW TABLES LIKE $TableName";
                    $QueryResult = mysql_query($SQLstring, $DBConnect);
                    
                    // If there are no rows in the query result, create the table.
                    if(mysql_num_rows($QueryResult) == 0)
                    {
                        $SQLstring = "CREATE TABLE $TableName
                        (
                            MovieID     INT             NOT NULL    AUTO_INCREMENT      PRIMARY KEY,
                            Title       VARCHAR(50)     NOT NULL,
                            ReleaseYear CHAR(4)         NOT NULL,
                            Genre       VARCHAR(50)     NOT NULL,
                            Actors      VARCHAR(200)    NOT NULL,
                            Awards      VARCHAR(100)    NULL,
                            Price       DECIMAL         NOT NULL
                        )";
                        
                        // Execute the query.
                        $QueryResult = mysql_query($SQLstring, $DBConnect);
                        
                        // If the query fails, display an error message.
                        if($QueryResult === false)
                        {
                            echo "<p class='message'>Unable to create the '$TableName' table.</p>";
                            echo "<p class='message'>Error Code: " . mysql_errno($DBConnect)
                                . ": " . mysql_error($DBConnect) . "</p>";
                        }
                        
                        // Insert the new movie into the database.
                        $MovieTitle = stripslashes($_POST['txtMovieTitle']);
                        $MovieYear = stripslashes($_POST['txtMovieYear']);
                        $MovieGenre = stripslashes($_POST['cboGenre']);
                        $MovieActors = stripslashes($_POST['txtLeadActor']);
                        $MovieAwards = stripslashes($_POST['txtAwards']);
                        $MoviePrice = stripslashes($_POST['txtPrice']);
                        
                        $SQLstring = "INSERT INTO $TableName
                            VALUES(NULL, '$MovieTitle', '$MovieYear', '$MovieGenre',
                            '$MovieActors', '$MovieAwards', '$MoviePrice')";
                        $QueryResult = mysql_query($SQLstring, $DBConnect);
                        
                        // If the query fails, display an error message.
                        if($QueryResult === false)
                        {
                            echo "<p class='message'>Unable to insert the values into the '$TableName' table.</p>";
                            echo "<p class='message'>Error Code: " . mysql_errno($DBConnect)
                                . ": " . mysql_error($DBConnect) . "</p>";
                        }
                        else
                        {
                            echo "<p class='message'>$MovieTitle has been added to the database.</p>";
                        }
                    }
                }
                // Close the database connection.
                mysql_close($DBConnect);
            }
        }
        ?>
            
        </div>
    </body>
</html>

Open in new window

0
 

Author Comment

by:Keith1985
ID: 36475935
@Ray

My apologies. I provided the wrong code. Here is the correct page.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="Stylesheet" type="text/css" href="../content/admin.css"/>
        <script type="text/javascript" src="../scripts/jquery.js"></script>
        <script type="text/javascript" src="../scripts/jquery.validate.min.js"></script>
        <script type="text/javascript" src="../scripts/validation.js"></script>
    </head>
    <body>
        <div id="content_main">
            <table>
                <tr>
                    <td class="nav_link"><a href="index.html">Home</a></td>
                    <td>|</td>
                    <td class="nav_link"><a href="add_movie.php">Add Movie(s)</a></td>
                    <td>|</td>
                    <td class="nav_link"><a href="movie_crud.php">View Movie(s)</a></td>
                </tr>
            </table>
        
            <?php
                // Make a connection to the database.
                $DBConnect = @mysql_connect("localhost", "root", "*******");
                
                // Determine if the connection fails. If it does, display an error message.
                if($DBConnect === false)
                {
                    echo "<p class='message'>An error has occured. Please try again.</p>";
                    echo "<p class='message'>If the problem persists, please contact
                        technical support.</p>";
                }
                else
                {
                    $DBName = "movies_test_db";
                    if(!mysql_select_db($DBName, $DBConnect))
                    {
                        echo "<p class='message'>An error has occured. Please try again.</p>";
                        echo "<p class='message'>If the problem perists, please contact
                            technical support.</p>";
                    }
                    else
                    {
                        // If the connection is successful, fetch data from the table.
                        $TableName = "Movies";
                        $SQLstring = "SELECT * FROM $TableName";
                        $QueryResult = mysql_query($SQLstring, $DBConnect);
                    
                        // Determine if there are any movies in the database.
                        // If no movies exists, display an error message.
                        if(mysql_num_rows($QueryResult) == 0)
                        {
                            echo "<p class='message'>There are currently no movies available.</p>";
                            echo "<p class='message'>Please check back soon.</p>";
                        }
                        else
                        {
                            echo "<table>";
                            echo "<tr>";
                            echo "<th class='movie_table_heading'>Movie Title</th>";
                            echo "<th class='movie_table_heading'>Year</th>";
                            echo "<th class='movie_table_heading'>Genre</th>";
                            echo "<th class='movie_table_heading'>Actors</th>";
                            echo "<th class='movie_table_heading'>Awards</th>";
                            echo "<th class='movie_table_heading'>Price</th>";
                            echo "<th class='movie_table_heading'>Actions</th>";
                            echo "</tr>";
                        
                            while(($row = mysql_fetch_assoc($QueryResult)) !== false)
                            {
                                $Price = $row['Price'];
                                $FormattedPrice = number_format($Price, 2);
                                echo "<tr>";
                                echo "<td class='movie_table_item'>{$row['Title']}</a></td>";
                                echo "<td class='movie_table_item'>{$row['ReleaseYear']}</td>";
                                echo "<td class='movie_table_item'>{$row['Genre']}</td>";
                                echo "<td class='movie_table_item'>{$row['Actors']}</td>";
                                echo "<td class='movie_table_item'>{$row['Awards']}</td>";
                                printf("<td class='movie_table_item'>$%s</td>", $FormattedPrice);
                                echo "<td class='movie_table_item'>";
                                echo "<a href='edit.php?{$row['MovieID']}' class='action_link'</a>Edit";
                                echo "<a href='delete.php?{$row['MovieID']}' class='action_link'</a>Delete";
                                echo "</td>";
                                echo "</tr>";
                            }
                            echo "</table>";
                        }
                        mysql_free_result($QueryResult);
                    }
                    mysql_close($DBConnect);
                }
            ?>
    </div>
    </body>
</html>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36475938
See if this helps get things going correctly.  Please read about the PHP "SuperGlobal" arrays.
http://us3.php.net/manual/en/reserved.variables.get.php

When you want to get information about a movie by its id you might have a URL that looked something like this:

/path/to/script.php?MovieId=2

Inside that script you would find a variable named $_GET["MovieId"]

You can use a statement like this to find the variable from the URL argument

$MovieID = (int) $_GET["MovieId"];

The variable names and array index names are case-sensitive.

You might want to add LIMIT 1 to your UPDATE queries, or else it will cause a table scan.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36476005
Lines 83 and 84.
echo "<a href='edit.php?MovieId={$row['MovieID']}' class='action_link'</a>Edit";
echo "<a href='delete.php?MovieId={$row['MovieID']}' class='action_link'</a>Delete";

Open in new window

Then things should work out OK with the $_GET array.
0
 

Author Comment

by:Keith1985
ID: 36476132
@Ray

You won't believe this, but I forgot to supply the 'MovieID' in the form action. I noticed something looked odd in the url, and now it works like a charm. Simple mistake, but frustrating to say the least. Thanks you very much for your insights. I'm still in college, but I noticed that if I don't explore other technologies or languages on my own, I won't make it far as a programmer. My school actually taught with VB.NET being the primary language, but I eased my way into C# pretty easily. However, with PHP being a loosely-typed language, a little bit of work has to be done to see a type of a variable. In the VS IDE, I simply get compile-time checking.

I will also check out your link on PHP practices. I'm normally an OO guy when writing code, but I decided not to implement OO in my PHP code just yet. I'm learning basic fundamentals first, then implement some OO later. I'm also interested in checking out the Zend framework since I'm already familiar with the MVC pattern. Thanks again Ray.
0
 

Author Closing Comment

by:Keith1985
ID: 36476136
Yes this is the correct solution to the problem.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36476174
If you want to work as a programmer, you will have some "A" languages and some "B" languages.  I have about 3 "A" languages in which I am highly proficient and about two dozen "B" languages in which I have written many scripts, but hardly enough to teach the language to others.  This article explains why.
http://norvig.com/21-days.html

But that said, try to get as many "B" languages as you can.  And while you cannot completely dilute the loose-typing of PHP, you can be aware and at least get better error checking if you add error_reporting(E_ALL) to the top of your scripts.

PHP5 has pretty good OOP implementations.  See http://php.net/manual/en/language.oop5.php

Let me save you a day of your life.  In PHP the array keys are loosely typed.  So an array that is indexed by integers with three elements, having keys numbered 0,1,2 will have position number 1 overwritten when you try to typecast a number to be character string and use it as an associative key like this: $array["1"] = $thing.  This behavior amazed many of us in the Washington, DC PHP Users Group.  Once you understand that is just the way it works, you can deal with the anomaly.  But it took me hours to figure out wtf was going on!

Anyway, I'm glad we got through a teachable moment with good results.  All the best, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now