PHP Měření času microtime
Jak změřit čas trvání vykonání nějaké funkce, nebo provedení části kódu v PHP
$start_time = microtime(true);
// do some work
$end_time = microtime(true);
// Calculate execution time
$execution_time = ($end_time - $start_time);
echo " Execution time = ".$execution_time." sec";
Date: 11.06.2020 - 09:1477LW NO topic_id
AD
Další témata ....(Topics)
// IF variable is not defined (or has a falsey value) THEN set it to an empty object.
// ELSE do nothing (technically speaking, variable gets assigned to itself)
var variable = variable || {} ;
function test(){
console.log(1);
}
// when called
test(); // 1
// or
var a = test;
a(); // 1
// or autocall if code executed
var test2 = function(){
console.log(2);
}
// test2 is undefined because the function test2 has no return value
// return string
function stringTest(param) {
return 'Hello ' + ' ' + param;
}
console.log(stringTest("Alice")); // Hello Alice
// return number
function numberTest(param) {
return 6 + param;
}
console.log(numberTest(5)); // 11
// or autocall if code executed
// function is like other variables
// parenthesis is for made groups or call functions.
// Immediately-Invoked Function Expression,
// or IIFE for short. It executes immediately after it’s created.
(function(){
// all your code here
var foo = function() {};
window.onload = foo;
// ...
})();
// foo is unreachable here (it’s undefined)
//////////////////////////////
// IIFE Immediately Invoked Funtion Expression
var greeting = function(name) {
return 'Hello ' + ' ' + name;
}('John');
console.log(greeting); // Hello John
console.log(typeof greeting); // string
console.log(greeting('Suzan')); // Uncaught TypeError: greeting is not a function
///////////////////////
var val = (function(){
var a = 0; // in the scope of this function
return function(x){
a += x;
return a;
};
})();
alert(val(10)); //10
alert(val(11)); //21
///////////////////////
// create the structure
var testHash = {
a : 1,
b : function(){
console.log(4);
},
c: function(param){
var num = param +5;
return num;
}
}
console.log(testHash.a); // 1
testHash.b(); // 4
testHash['b'](); // 4
testHash.c(7); // 12
/////////////////////////
// or function like class
function TestClass(n) {
this.some_property = n;
this.some_method = function() {
console.log(this.some_property);
};
}
var foo = new TestClass(3);
var bar = new TestClass(4);
foo.some_method(); // 3
bar.some_property += 2;
bar.some_method(); // 6
// console.log(some_property); // Uncaught ReferenceError: some_property is not defined
//////////////////////////
function Car(brand, year) {
return {
brand: brand,
year: year
}
}
var honda = Car("Honda", 1999);
console.log(honda.brand); // Honda
console.log(honda.year); // 1999
honda.brand = "Honda new Generation";
honda.year = 2021;
console.log(honda.brand); // Honda new Generation
console.log(honda.year); // 2021
var mazda = Car("Mazda",1987);
console.log(honda.brand); // Honda new Generation
console.log(mazda.brand); // Mazda
//////////////////////////////////////
// simplified code
var TEST_CLASS = TEST_CLASS || {};
TEST_CLASS.pokus = TEST_CLASS.pokus || (function() {
// -------------------------------------------------------------------------
// Private static variables and methods
// -------------------------------------------------------------------------
var privateStaticNumber = 5;
var privateStaticString = "My privateStaticString: ";
var privateStaticArray = [];
function privateStaticMethod(){
console.log('privateStaticMethod');
}
// CONSTRUCTOR CTOR
return function(config) {
// ----- private variables -----
var me = this,
isPaused = false;
this.privateStaticNumber = config.firstParam;
this.privateStaticString = privateStaticString + config.secondParam; // += produce: undefined I am from secondParam
this.privateStaticArray = config.thirtParam;
// ----- private methods -----
function getMode (mode, speed) {
// document.getElementById(mode).addEventListener('click', function () { snakeSpeed = speed; });
}
// ----- public variables -----
me.correct = 0;
me.wrong = 0;
// ----- public methods -----
me.setString = function(val) {
this.privateStaticString = val;
};
me.getString = function() {
return this.privateStaticString;
};
me.publicMethod = function(param){
console.log(param + ' from me.publicMethod()');
};
}; // end CTOR
})();
// end TEST_CLASS.pokus
var _me = _me || {};
var _me_test = new TEST_CLASS.pokus({firstParam:8, secondParam:" I am from secondParam", thirtParam:[1,3,9]});
// TEST_CLASS.pokus.privateStaticMethod(); // is not a function
// TEST_CLASS.pokus.publicMethod("Hola!"); // TEST_CLASS.pokus.publicMethod is not a function
// TEST_CLASS.publicMethod("Hola hej!"); // TEST_CLASS.publicMethod is not a function
// TEST_CLASS.pokus.publicMethod(" Hello!"); // is not a function
_me_test.publicMethod(" Hello!"); // Hello! from me.publicMethod()
console.log( _me_test.getString()); // My privateStaticString: I am from secondParam
console.log( _me_test.isPaused); // undefined
Date: 04.11.2020 - 23:14Toogle DIV javascript example code:
Jak skrýt, nebo zobrazit kontejner DIV:Nezapomeňte přiřadit DIVu ID, aby k němu bylo možno přistupovat pomocí getElementById a vše fungovalo i v jiných prohlížečích (Opera, Firefox, Google Chrome) a ne jen v Internet Exploreru.
<div id="idSomeDiv"> text div </div>
<input type="button" size="200" value="Skryj-zobraz"
onclick="var dH = document.getElementById('idSomeDiv');
if(dH.style.visibility == 'hidden') {
dH.style.visibility = 'visible';
} else {
dH.style.visibility = 'hidden';
}">
Foreach fore each in javascript loop cycle example source code
Example code foreach for JavaScript.
var arr = new Array(1,2,3,4,5,6);// numbers or string put into array
for (var key in arr) {
window.alert("My array"
+ key
+ " = "
+ arr[key]);
}
// next example
arr.forEach(function(elem){
console.log(elem);
});
// next example
for (var i = 0; i < arr.length; i++) {
alert (arr[i]);
}
// next example for GECKO MOZILLA FIREFOX
var soucet = 0;
var obj = {var1: 5, var2: 13, var3: 8};
for each (var item in obj) {
soucet += item;
}
alert(soucet); // vysledek "26", coz je soucet 5+13+8
Each in jQuery get all document inputs type text and set value 0 if checkbox checked
function fc(th){
if( th.checked ){
$("input:text").each(
function() {
this.value = 0;
this.disabled = true;
}
);
}else{
$("input:text").each(
function() {
this.disabled = false;
}
);
}
<input type="checkbox" id="idCheck" onchange="fc(this);"> Checkbox
<input type="text" size="1" id="id1" value="0" />
<input type="text" size="1" id="id2" value="0" />
Někdy potřebujeme získat url adresu pro menu, nebo odkazy atd.
pozor funkce je empty() a ne emptyempty
pozor funkce je empty() a ne emptyempty
$currentURL = (!empty($_SERVER['HTTPS']))
? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']
: "//".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
Jeden znak: a, b nebo c | [abc] | [abc] | a bb ccc |
Znak kromě: a, b nebo c | [^abc] | [^abc] | d, ff, mmm ale ne a, bb, ccc |
Znak od až po: a-z | [a-z] | [a-z] | jez, mez, bez ale ne keřík - neumí diakritiku |
Znak není v rozsahu: a-z | [^a-z] | vybere prázdné mezery i pismena s diakritikou | |
Znak v rozsahu: a-z nebo A-Z | [a-zA-Z] | neumí diakritiku | |
Jakýkoliv jednotlivý znak | . | bere vše i diakritiku, kromě znaku konce řádku | |
Znak mezery | \s | ^\r
\t\f\v a mezery | |
Znak není mezera | \S | vše kromě mezer viz výš | |
Číslice | \d | pouze číslice | |
Libovolné jiné než číslice | \D | vše výjma číslic | |
Jakýkoliv znak písmena | \w | \w+ | celá slova pokud neobsahují diakritku či nepísmena |
Jakýkoli znak jiný než písmeno | \W | \W+ | *-+#@ ale ne _ podtržítko |
Vše co bude v závorce (group) | (...) | (he) | všechna he ze slov |
Buď a nebo b | (a|b) | (a|b) | vybere vešechna a i b ze slov |
Nula nebo jeden znak a | a? | bao? | bao , ba, ale ne samotne b |
Nula nebo více | a* | bao* | ba, bao ale ne b, bax, bac |
Jeden nebo více | a+ | a až nekonečno aaaa i a uprostřed slova | |
Přesně 3 znaky a | a{3} | aaa aaaa jen první 3 znaky, ale ne aa | |
3 nebo více znaků a | a{3,} | aaa až nekonečno aaaaaaaa | |
Mezi 3 a 6 znaky a | a{3,6} | aaa aaaaaa ale ne aa | |
Začátek řetězce | ^ | ^\w+ | např. slovo je začátek nového řádku |
Konec řetězce | $ | \w+$ | (veme konci) poslední slovo na konci |
Hranice slova | \b | d\b | (veme d na konci slov) hrad led |
Není hranice slova | \B | a\B | a v ab, ale ne v ba |
Nový řádek |
| \r
?| | cross platform newline / nalezne nový řádek napříč platformami |
Tab - tabulátor | \t | ||
Carriage return CR | \r | návrat vozíku https://cs.wikipedia.org/wiki/Carriage_return | |
Editace: 11.6.2020 - 09:18
Počet článků v kategorii: 77
Url:php-mereni-casu