hisoka Posted April 17, 2016 Share Posted April 17, 2016 (edited) In this little code : <?php if (isset($_GET['animal'])) { $string =$_GET['animal']; $replacement = 'cat'; if (strpos($string, 'mouse') !==false) { echo preg_replace('/.$string./' , $replacement, 'mouse'); } } ?> the result is mouse Now when I replace '/.$string./' with "/".$string."/" I got cat as a result . I do not know what is the difference between "/".$string."/" and '/.$string./' and what are both dots in '/.$string./' and "/".$string."/" used for ? Edited April 17, 2016 by hisoka Link to comment Share on other sites More sharing options...
Ingolme Posted April 17, 2016 Share Posted April 17, 2016 You should read this article: http://php.net/manual/en/language.types.string.php This has nothing to do with preg_replace and everything to do with the way PHP parses strings. Link to comment Share on other sites More sharing options...
hisoka Posted April 17, 2016 Author Share Posted April 17, 2016 (edited) Thank you for the link I will reformulate my question : why is "/".$string."/" in four double quotes and not between two double quotes like this : "/.$string./" ?? Could you please explain to me why the $string in '/.$string./' is between two dots ? in other words what is the role of the two dots ? Edited April 17, 2016 by hisoka Link to comment Share on other sites More sharing options...
Ingolme Posted April 17, 2016 Share Posted April 17, 2016 That's literally the string "$string" surrounded by dots and slashes. It is just text, it does not mean anything. I don't know where you found that code but it is wrong. It does not do anything. Link to comment Share on other sites More sharing options...
hisoka Posted April 17, 2016 Author Share Posted April 17, 2016 you mean this "/".$string."/" is wrong ? or the whole code ? if the whole code is wrong , why it run, normally , without showing any error ?? Link to comment Share on other sites More sharing options...
Ingolme Posted April 17, 2016 Share Posted April 17, 2016 This is correct: It's concatenating the value of the variable with other strings: "/".$string."/" This is not correct. Everything between the quotation marks are literally part of the string. There's no variable. '/.$string./' Link to comment Share on other sites More sharing options...
hisoka Posted April 18, 2016 Author Share Posted April 18, 2016 (edited) This is not correct. Everything between the quotation marks are literally part of the string. There's no variable. I understood it very well What I could not understand is here : "/".$string."/" Why we use 4 green double quotes instead of two green double quotes ? or what is the role of the 4 green double quotes ? Edited April 18, 2016 by hisoka Link to comment Share on other sites More sharing options...
Ingolme Posted April 18, 2016 Share Posted April 18, 2016 Each pair of double quotes contains a string between them which the syntax highlighter is showing in green. They are two separate independent strings. This is very basic PHP. Please read the PHP chapter about strings carefully: http://php.net/manual/en/language.types.string.php Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now