Skip to content

Commit 8a65c98

Browse files
committed
[phpstorm-stubs] WI-41091 Improve documentation for substr, addcslashes, and header functions with clarified examples, annotations, and descriptions
1 parent d955c00 commit 8a65c98

File tree

2 files changed

+101
-51
lines changed

2 files changed

+101
-51
lines changed

standard/standard_1.php

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -501,31 +501,23 @@ function strcoll(string $string1, string $string2): int {}
501501
function money_format(string $format, float $number): ?string {}
502502

503503
/**
504-
* Return part of a string or false on failure. For PHP8.0+ only string is returned
504+
* Returns the portion of string specified by the offset and length parameters.
505505
* @link https://php.net/manual/en/function.substr.php
506506
* @param string $string <p>
507507
* The input string.
508508
* </p>
509509
* @param int $offset <p>
510-
* If start is non-negative, the returned string
511-
* will start at the start'th position in
512-
* string, counting from zero. For instance,
513-
* in the string 'abcdef', the character at
514-
* position 0 is 'a', the
515-
* character at position 2 is
516-
* 'c', and so forth.
510+
* If offset is non-negative, the returned string will start at the offset'th position in string, counting from zero.
511+
* For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.
517512
* </p>
518513
* <p>
519-
* If start is negative, the returned string
520-
* will start at the start'th character
521-
* from the end of string.
514+
* If offset is negative, the returned string will start at the offset'th character from the end of string.
522515
* </p>
523516
* <p>
524-
* If string is less than or equal to
525-
* start characters long, false will be returned.
517+
* If string is less than offset characters long, an empty string will be returned.
526518
* </p>
527519
* <p>
528-
* Using a negative start
520+
* Using a negative offset
529521
* </p>
530522
* <pre>
531523
* <?php
@@ -535,24 +527,22 @@ function money_format(string $format, float $number): ?string {}
535527
* ?>
536528
* </pre>
537529
* @param int|null $length [optional] <p>
538-
* If length is given and is positive, the string
539-
* returned will contain at most length characters
540-
* beginning from start (depending on the length of
541-
* string).
530+
* If length is given and is positive, the string returned will contain at most length characters beginning from offset
531+
* (depending on the length of string).
542532
* </p>
543533
* <p>
544-
* If length is given and is negative, then that many
545-
* characters will be omitted from the end of string
546-
* (after the start position has been calculated when a
547-
* start is negative). If
548-
* start denotes a position beyond this truncation,
549-
* an empty string will be returned.
534+
* If length is given and is negative, then that many characters will be omitted from the end of string.
535+
* If offset denotes the position of this truncation or beyond, an empty string will be returned.
550536
* </p>
551537
* <p>
552-
* If length is given and is 0,
553-
* false or null an empty string will be returned.
538+
* If length is given and is 0, an empty string will be returned.
554539
* </p>
540+
* <p>
541+
* Starting from PHP 8.0 if length is omitted or null, the substring starting from offset until the end of the string will be returned.
542+
* </p>
543+
* <p>
555544
* Using a negative length:
545+
* </p>
556546
* <pre>
557547
* <?php
558548
* $rest = substr("abcdef", 0, -1); // returns "abcde"
@@ -561,6 +551,25 @@ function money_format(string $format, float $number): ?string {}
561551
* $rest = substr("abcdef", -3, -1); // returns "de"
562552
* ?>
563553
* </pre>
554+
* @return string|false Returns the extracted part of string, or an empty string. (FALSE prior PHP 8.0)
555+
* <p>
556+
* Basic usage:
557+
* </p>
558+
* <code>
559+
* echo substr('abcdef', 1), PHP_EOL; // bcdef
560+
* echo substr("abcdef", 1, null), PHP_EOL; // bcdef; prior to PHP 8.0.0, empty string was returned
561+
* echo substr('abcdef', 1, 3), PHP_EOL; // bcd
562+
* echo substr('abcdef', 0, 4), PHP_EOL; // abcd
563+
* echo substr('abcdef', 0, 8), PHP_EOL; // abcdef
564+
* echo substr('abcdef', -1, 1), PHP_EOL; // f
565+
*
566+
* // Accessing single characters in a string
567+
* // can also be achieved using "square brackets"
568+
* $string = 'abcdef';
569+
* echo $string[0], PHP_EOL; // a
570+
* echo $string[3], PHP_EOL; // d
571+
* echo $string[strlen($string)-1], PHP_EOL; // f
572+
* </code>
564573
*/
565574
#[Pure]
566575
#[LanguageLevelTypeAware(["8.0" => "string"], default: "string|false")]
@@ -699,17 +708,12 @@ function addslashes(string $string): string {}
699708
* The string to be escaped.
700709
* </p>
701710
* @param string $characters <p>
702-
* A list of characters to be escaped. If
703-
* charlist contains characters
704-
* \n, \r etc., they are
705-
* converted in C-like style, while other non-alphanumeric characters
706-
* with ASCII codes lower than 32 and higher than 126 converted to
707-
* octal representation.
711+
* A list of characters to be escaped. If characters contains characters \n, \r etc., they are converted in C-like style,
712+
* while other non-alphanumeric characters with ASCII codes lower than 32 and higher than 126 converted to octal representation.
708713
* </p>
709714
* <p>
710-
* When you define a sequence of characters in the charlist argument
711-
* make sure that you know what characters come between the
712-
* characters that you set as the start and end of the range.
715+
* When you define a sequence of characters in the characters argument make sure that you know what characters come
716+
* between the characters that you set as the start and end of the range.
713717
* </p>
714718
* <pre>
715719
* <?php
@@ -733,14 +737,23 @@ function addslashes(string $string): string {}
733737
* ?>
734738
* </pre>
735739
* <p>
736-
* Be careful if you choose to escape characters 0, a, b, f, n, r,
737-
* t and v. They will be converted to \0, \a, \b, \f, \n, \r, \t
738-
* and \v.
739-
* In PHP \0 (NULL), \r (carriage return), \n (newline), \f (form feed),
740-
* \v (vertical tab) and \t (tab) are predefined escape sequences,
741-
* while in C all of these are predefined escape sequences.
740+
* Be careful if you choose to escape characters 0, a, b, f, n, r, t and v.
741+
* They will be converted to \0, \a, \b, \f, \n, \r, \t and \v, all of which are predefined escape sequences in C.
742+
* Many of these sequences are also defined in other C-derived languages, including PHP, meaning that you may not get
743+
* the desired result if you use the output of addcslashes() to generate code in those languages with these characters
744+
* defined in characters.
742745
* </p>
743746
* @return string the escaped string.
747+
* <p>
748+
* Example usage:
749+
* </p>
750+
* <code>
751+
* <?php
752+
* $not_escaped = "PHP isThirty\nYears Old!\tYay to the Elephant!\n";
753+
* $escaped = addcslashes($not_escaped, "\0..\37!@\177..\377");
754+
* echo $escaped;
755+
* ?>
756+
* </code>
744757
*/
745758
#[Pure]
746759
function addcslashes(string $string, string $characters): string {}

standard/standard_4.php

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,13 @@ function restore_include_path() {}
604604
* setcookie will fail and return false. If
605605
* setcookie successfully runs, it will return true.
606606
* This does not indicate whether the user accepted the cookie.
607+
* Example:
608+
* <code>
609+
* $value = 'something from somewhere';
610+
* setcookie("TestCookie", $value);
611+
* setcookie("TestCookie", $value, time()+3600);
612+
* setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", true);
613+
* </code>
607614
*/
608615
function setcookie(string $name, string $value = "", int $expires_or_options = 0, string $path = "", string $domain = "", bool $secure = false, bool $httponly = false): bool {}
609616

@@ -626,6 +633,14 @@ function setcookie(string $name, string $value = "", int $expires_or_options = 0
626633
* @return bool If output exists prior to calling this function, setcookie will fail and return false. If
627634
* setcookie successfully runs, it will return true.
628635
* This does not indicate whether the user accepted the cookie.
636+
* Example:
637+
* <code>
638+
* $value = 'something from somewhere';
639+
* setcookie("TestCookie", $value);
640+
* setcookie("TestCookie", $value, time()+3600);
641+
* setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", true);
642+
* </code>
643+
*
629644
* @since 7.3
630645
*/
631646
function setcookie(string $name, string $value = '', array $options = []): bool {}
@@ -674,13 +689,23 @@ function setrawcookie(string $name, $value = '', array $options = []): bool {}
674689
* The header string.
675690
* </p>
676691
* <p>
677-
* There are two special-case header calls. The first is a header
678-
* that starts with the string "HTTP/" (case is not
679-
* significant), which will be used to figure out the HTTP status
680-
* code to send. For example, if you have configured Apache to
681-
* use a PHP script to handle requests for missing files (using
682-
* the ErrorDocument directive), you may want to
683-
* make sure that your script generates the proper status code.
692+
* There are two special-case header calls. The first is a header that starts with the string "HTTP/"
693+
* (case is not significant), which will be used to figure out the HTTP status code to send. For example,
694+
* if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive),
695+
* you may want to make sure that your script generates the proper status code.
696+
* </p>
697+
* <p>
698+
* Example:
699+
* <code>
700+
* <?php
701+
* // This example illustrates the "HTTP/" special case
702+
* // Better alternatives in typical use cases include:
703+
* // 1. header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
704+
* // (to override http status messages for clients that are still using HTTP/1.0)
705+
* // 2. http_response_code(404); (to use the default message)
706+
* header("HTTP/1.1 404 Not Found");
707+
* ?>
708+
* </code>
684709
* </p>
685710
* <p>
686711
* The second special case is the "Location:" header. Not only does
@@ -689,15 +714,27 @@ function setrawcookie(string $name, $value = '', array $options = []): bool {}
689714
* unless the 201 or
690715
* a 3xx status code has already been set.
691716
* </p>
717+
* <p>Example</p>
718+
* <code>
719+
* header("Location: http://www.example.com/");
720+
* exit;
721+
* </code>
692722
* @param bool $replace [optional] <p>
693723
* The optional replace parameter indicates
694724
* whether the header should replace a previous similar header, or
695725
* add a second header of the same type. By default it will replace,
696726
* but if you pass in false as the second argument you can force
697-
* multiple headers of the same type. For example:
698-
* </p>
727+
* multiple headers of the same type.
728+
* </p>
729+
* <p>For example:</p>
730+
* <code>
731+
* <?php
732+
* header('WWW-Authenticate: Negotiate');
733+
* header('WWW-Authenticate: NTLM', false);
734+
* ?>
735+
* </code>
699736
* @param int $response_code <p>
700-
* Forces the HTTP response code to the specified value.
737+
* Forces the HTTP response code to the specified value. Note that this parameter only has an effect if the header is not empty.
701738
* </p>
702739
* @return void
703740
*/

0 commit comments

Comments
 (0)