Hey everyone. I have a couple of questions related to PHP. I realized I need to learn how to code in baby steps until I get the gist of it. This is how I learn best, knowing every detail so I can break it down in my head. Which is what I came to realize a lot of books and courses don't explain.....Ok so.. It's why questions. Why does this do that and why are there 3 brackets...etc.

1. $clean[“text”]=str_replace("malicious", " ", $_POST[text]);

Why are there three " above and why are they positioed in that spot?

    2.         <?php
    if($_POST){

        $password = $_POST['password'];
        $user = $_POST['name'];

        //you should also encrypt the password in your database, MD5 is the simplest

        $password = md5($password);


        //If inserted user record successfully you will get ID from the database for that user
        // U use that to log in the user with that ID or whatever you need to do it. often redirect like


        if($last_insertId){
            //redirect
            //header('Location:loggedIn.php');
            //Instead of echoing, you can output the username in HTML



            echo 'Registered!';
        }

    }
?>

Why are there two } brackets? Are those closing brackets? Like in HTML if you have <div class="header1"> <div class="header2"> You need </div></div> to make sure the code is correct and doesn't break anything?

Edited 10 Months Ago by Chris920

Hi, about this:

$clean["text"]=str_replace("malicious", " ", $_POST[text]);

In all cases these quotes are used to define a string, in:

$clean["text"]

You are defining an index key on an array defined in the $clean variable, the other two, instead, are arguments of the str_replace() function.

In this case, unless text is a constant, then when executing $_POST[text] you will get a Notice: Use of undefined constant text - assumed 'text', in practice the PHP engine will try to serve it as a string index key: $_POST['text']

See:

By the way: str_replace() is not the way to sanitize input, look at the filter extension:

Edited 10 Months Ago by cereal

If you're willing to continue learning PHP.
I'm not master in it, and I don't live up to heels of PHP giants over here.
But I've done some coding here and there and understand all basic, and majority of medior principles of PHP and programming overall.

So, if you're still dedicated to learning PHP, hit me up on Private Messaging, where I could explain everything to you.

Ok just making sure I understand this:

$clean["text"]=str_replace("malicious", " ", $_POST[text]);

The reason " " are in the place they are is to define $_POST[text]...and the reason it defines text is because "text" is in the [] brackets of post...If it was
$clean["abc"]=str_replace("malicious", " ", $_POST[abc]);

It would define ABC instead?

As for the brackets I was talking about the } brackets. Are they closing brackets?

Aeonix: Going to send you a PM...We can use cloud9...The real time IDE where we can both code in so if I mess up you can correct it.

The reason " " are in the place they are is to define $_POST[text]...and the reason it defines text is because "text" is in the [] brackets of post...If it was

$clean["abc"]=str_replace("malicious", " ", $_POST[abc]);

It would define ABC instead?

Hmm, no:

$clean["abc"]

and:

$clean["ABC"]

would generate two different index keys, for example, you can do:

$clean = array(
            "abc" => 123,
            "ABC" => "hello"
        );

var_dump($clean);

And you would get:

array(2) {
  ["abc"]=>
  int(123)
  ["ABC"]=>
  string(5) "hello"
}

if, instead, you try to define two index keys with the same name, then you will end to overwrite the first index, for example, you can do:

$clean = array(
            "abc" => 123,
            "abc" => "hello"
        );

var_dump($clean);

And you get:

array(1) {
  ["abc"]=>
  string(5) "hello"
}

In reference to the str_replace() function you have to define four arguments:

str_replace(ARG_1, ARGU_2, ARG_3, ARG_4)

ok? The last is optional, so I won't talk about it. Now, the first three arguments can take strings or arrays, to simplifly we will look only at strings.

So, ARG_1 is the needle, a string you want to match, in your case "malicious"; the second argument (ARG_2) defines what you want to use to replace the malicious string, in your case " ", which is an empty space; ARG_3 instead is the string in which you want to apply the replacement, i.e. the haystack, so as example:

$string = "I want two oranges";
$replace = str_replace("two", "some", $string);
echo $replace;

# outputs
I want some oranges

Did you understyood?

This article has been dead for over six months. Start a new discussion instead.