Home > Net >  How to make strlen() take the string from user input
How to make strlen() take the string from user input

Time:01-22

i tried with $_GET (it's a school themes counter so it need to count the words or the character)

<!-- made by TheNea -->
<form  action="Temi.php" method="get">
        <input type="text" name="theme" id="theme_counter" size="30cm" placeholder="inserisci qui il tuo tema (non preoccuparti della formattazione)">
        <input type="submit" >
    </form>
</body>

<?php

function(){
    $string = $_GET['theme'];
    $length = strlen($string);
    echo $length;
    
};
?>
<!-- Made by Fgamervisa -->

CodePudding user response:

Your function is never called.

You can use call it with () :

(function(){
    // your code
})();

Or use named function :

function myFunction() {
    // your code
}

myFunction();

But, in your specific case, you probably only want to do :

if (isset($_GET['theme'])) {
    echo strlen($_GET['theme']);
}

CodePudding user response:

First and foremostYour code should be written in a file named Temi.php because the action of your form points to this file.

For counting words, php offer built-in function str_word_count($mystring) Check official documentation.

Then you have to check if your variable has been set or not! Why ? because when you execute the file first time there is no theme in global variable GET, theme will be added after you click on the submit button, so you have to check its existence by using the isset function, and if it exists, you can move to the next step.

if (isset($_GET['theme'])) { 
    print 'Length = '. strlen($_GET['theme']).'<br>'; 
    print str_word_count($_GET['theme']).' Words <br>'; 
}
  •  Tags:  
  • Related