in my website's registration form, the following php code controls user first name field :
if (!isset($value[4])){
$return = $register_msg['_register_some_msg'];
}
what does this code do ? does it have anything with checking the string length ?
currently it forces the first name of minimum 3 characters, but as i searched seems that strlen() Function checks the length of string which doesn't exists there.
Yes, this code is checking string length.
It appears you have two arrays.
$value is a numerical array, and you check if the 5th element is set. (index starts on 0).
$register_msg is an associative array, instead of numbers as the index, it uses keys. Here you assign the value of key _register_some_msg to your return variable.
The other option is that $value is a string, and it's using bad practice to check each letter by referencing them as an array key:
<?php
$x = 'abcd';
echo $x[3];
//output: d
Which is just bad. use strlen() instead http://php.net/manual/en/function.strlen.php
The isset() method here just verify if $value[4] (your object, or string, or other) exists or not.
Related
At the moment i try to create a function in php which simply reads an int value of a field of an array and compares it to a max value.
If this max value is reached, it shall set the value of the field to zero, jump to the next field in the array and increase the int value stored inside.
If this value also reached the max value do the same as above.
If the above condition isn´t true it shall just increase the stored value in the array.
My Code looks like this:
if($sign_counter[0] === (count($pw_signs) - 1)){
$counter = 0;
while($sign_counter[$counter] === (count($pw_signs) - 1)){
$sign_counter[$counter] = "0";
$counter++;
}
$sign_counter[$counter]++;
}
else{
$sign_counter[0]++;
}
I allready tested this part of the function several times with different values on my website and browser. I also checked if the values were stored correctly in the array and inside needed variables.
Thats how my array looks like:
$sign_counter = array("38", "2");
For example:
$sign_counter array to store int values
(count($pw_signs) - 1) always equals 38 (because there are 39 fields in the counted array)
$counter used to determine the field position inside the array
Now if i store the value "38" in the first field and "2" in the second field of the array the code should detect, that the max value is reached in the first field, set the value of the field to 0, then jump to the next field and increase its value by 1.
Instead of what i want to achieve the code just increases the value of the first field of the array.
It looks like the while loop just ignores it's own condition but the values itself don't seem to be the problem.
I don't really get why the while loop behaves like this.
Do i completly miss something here?
Would appreciate any help.
Greetings Sleepy
The problem is that you're storing the values as strings, not numbers, and you're using the === operator, which doesn't perform type coercion between different types. count($pw_signs) - 1 will always be a number, not a string, all the === tests will fail.
Get rid of the quotes around all the numbers and it should work as desired. And if the source of the values is external, convert them to numbers with intval() before storing into the array.
How do I get the path info (php) when the path is of the form: www.example.com/post/* (* being a variable number that matches the post id) and compare it to www.example.com/post to make it TRUE.
In other words, if pathinfo is www.example.com/post/1068 (or 1069, or 1070, or whatever the id number) then the statements that follow are executed. If not, then they are skipped.
The snippet is destined to a template file. If this requires a pre-process function, please say so. Thank you.
I am new at this so please do not send me to some other post that reads like Chinese! :)
Thank you.
You could explode and filter for empty values and then use the last occurrence, something rough like this:
$url = "www.example.com/post/1068";
$id = array_pop(array_filter(explode('/', $url)));
$validIds = array("1068", "1069", "1070");
if (in_array($id, $validIds)) {
//do something;
}
explode will separate your string in an array using "/" as a separator (in this case)
array_filter will filter empty values in order to make sure that the last value will be your id
array_pop will retreive the last value of the given array
in_array will compare a value with an array and check if the value is within the given array and return true/false depending whether it is or not inside the array.
A script I'm using takes in a string of field name/value pairs, splits them, and creates a query from them. The string is formatted like this:
var1==value1,var2==value2...
The values will be submitted by users on the frontend of the website. So, if a user selects a value for var1 and var4 but not 2 and 3 I would need the string to look like this:
var1==value1,var4==value4
Getting the user-submitted data isn't a problem. What is the best way to add in the field name and == only if the associated value is not blank?
if (isset($varname))
isset — Determine if a variable is set and is not NULL
if(empty($varname)))
empty — Determine whether a variable is empty
if(is_null($varname)))
is_null — Finds whether a variable is NULL
In other words, it only returns true when the variable is null. is_null() is the opposite of isset(), except you can use isset() on unknown variables.
You could do something like
$pairs = "var1==stuff,var3==morestuff"
if(strpos($pairs, "var2==") !== false){
$pairs .= "var2==defaultvalue";
}
And you could do that for every var# you want. This would be able to check if the
About the strpos : How do I check if a string contains a specific word in PHP?
Just whip over the submitted form fields looking for anything with a value:
foreach($_POST as $key => $value){
if($value != ''){
// do stuff
}
}
also add a check to skip the submit = submit bit :)
In PHP, can a value from the global $_POST array be something else than an array or a string?
The goal is to not have to check if everything is something else than an array or string in a script. If I know what type a variable has, I don't have to do some weird validation. If I expect a string, I don't have to cast everything to a string to make sure it is one.
$_POST["key"] = true;
var_dump($_POST["key"]); // bool(true)
The values set by the environment are strings though.
As per the documentation http://php.net/manual/en/reserved.variables.post.php
An associative array of variables passed to the current script via the HTTP POST method.
So all the data sent is in associative array, in key value pair. There are Numbers (int, float etc), Strings, Arrays (of numbers or strings) and Objects data types.
Using the rule of elimination we can remove the Object from the supported data type, and the remaining left are strings, numbers and array.
Now, if you see the form, the input fields are taking strings, there is no indication that the value entered in the input field is number or string. So to be on safe side all the values which are posted are in strings. The array of elements also have the string values.
When you get the value in $_POST it is simply an array and you can override it any time
$_POST['username'] = 1;
var_dump($_POST['username']); // int (1)
I hope this make some sense
You can cast it to whatever you wish ^^
intval($_POST['INTEGER']);
or simply
(int)$_POST['int']
A string is an array of characters, correct? If so, then why does count() not produce the same result as strlen() on a string?
Unless it is an object, count casts its argument to an array, and
count((array) "example")
= count(array("example"))
= 1
A string is an array of characters in C, C++ and Java. In PHP, it is not.
Remember that PHP is a very loose language, you can probobly get a character from a PHP string with the []-selector, but it still dosn't make it an array.
count() counts the number of entries in an Array.
$test = array(1,2,3);
echo count($test);
Output: 3
Why would you want to use count() on a string when strlen() can do that? Are you not sure if your input is a string or an array? Then use is_array() to check that.
How exactly a string is being handled internally by a specific programming language, must not necessarily mean you can handle it equally to therefore "related" data types. What you describe may be possible in plain C. However PHP is not C and so is not following the same characteristics.
Strings are just a series of charactes, and count only counts number of elements in an array.
using $string[$index]; its just a shortcut kinda of thing to help you find Nth character,
you could use count(explode('',$string)); which presumably is what strlen does
so lesson for today is
count == array length
strlen == string length
count gets the number of elements in an array, srtlen gets the length of a string. This is in the docs:
http://php.net/manual/en/function.strlen.php
count is more preferabaly user for array value count
strlen its count only the no of char in str.....